/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/******************************************************************************
 *
 *   File: profiler_device_event.c
 *
 *   Description:
 *       Upon successful allocation of this class, a client is granted
 *       permission to collect device and context profiling event counters.
 *
 *****************************************************************************/

#define NVOC_PROFILER_DEVICE_EVENT_H_PRIVATE_ACCESS_ALLOWED



#include "core/core.h"
#include "gpu/gpu.h"
#include "kernel/gpu/hwpm/profiler_device_event.h"
#include "class/clb0cd.h"
#include "os/os.h"
#include "rmapi/client.h"

NV_STATUS
profilerDeviceEventConstruct_IMPL
(
    ProfilerDeviceEvent *profilerDeviceEvent,
    CALL_CONTEXT *pCallContext,
    RS_RES_ALLOC_PARAMS_INTERNAL *pParams
)
{
    NVB0CD_ALLOC_PARAMETERS *pUserParams = pParams->pAllocParams;
    OBJSYS *pSys = SYS_GET_INSTANCE();
    RmClient *pRmClient = dynamicCast(pCallContext->pClient, RmClient);
    NV_STATUS status;

    osRmCapInitDescriptor(&profilerDeviceEvent->dupedCapDescriptor);

    status = osRmCapAcquire(pSys->pOsRmCaps,
                            NV_RM_CAP_SYS_PROFILER_DEVICE,
                            pUserParams->capDescriptor,
                            &profilerDeviceEvent->dupedCapDescriptor);

    //
    // On platforms where capability isn't implemented,
    // enforce the admin-only check.
    //
    if (status == NV_ERR_NOT_SUPPORTED)
    {
        if (!rmclientIsAdmin(pRmClient, pCallContext->secInfo.privLevel))
        {
            NV_PRINTF(LEVEL_ERROR, "insufficient permissions\n");
            return NV_ERR_INSUFFICIENT_PERMISSIONS;
        }
    }

    return status;
}

void
profilerDeviceEventDestruct_IMPL
(
    ProfilerDeviceEvent *profilerDeviceEvent
)
{
    osRmCapRelease(profilerDeviceEvent->dupedCapDescriptor);
}


NvBool
profilerDeviceEventCanCopy_IMPL
(
    ProfilerDeviceEvent *profilerDeviceEvent
)
{
    //
    // Returning NV_FALSE here prevents duping of this object,
    // since nv-caps doesn't support duping yet, please revisit duping if needed in the future.
    //
    return NV_FALSE;
}

