/*
 * SPDX-FileCopyrightText: Copyright (c) 1993-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.
 */

#include "core/core.h"
#include "core/locks.h"
#include "core/system.h"
#include "os/os.h"
#include "tls/tls.h"
#include "gpu_mgr/gpu_mgr.h"
#include "gpu/gpu.h"

static NvBool s_bRmLocksAllocated = NV_FALSE;

NV_STATUS
rmLocksAlloc(OBJSYS *pSys)
{
    NV_STATUS status;

    s_bRmLocksAllocated = NV_FALSE;

    // legacy lock model : RM system semaphore
    status = osAllocRmSema(&pSys->pSema);
    if (status != NV_OK)
        return status;

    // RM_BASIC_LOCK_MODEL : GPU lock info (ISR/DPC synchronization)
    status = rmGpuLockInfoInit();
    if (status != NV_OK)
    {
        osFreeRmSema(&pSys->pSema);
        return status;
    }
    rmInitLockMetering();

    s_bRmLocksAllocated = NV_TRUE;

    return status;
}

void
rmLocksFree(OBJSYS *pSys)
{
    if (s_bRmLocksAllocated)
    {
        rmDestroyLockMetering();
        rmGpuLockInfoDestroy();
        osFreeRmSema(pSys->pSema);

        s_bRmLocksAllocated = NV_FALSE;
    }
}

/*!
 * @brief Acquires all of the locks necessary to execute RM code safely
 *
 * Other threads and client APIs will be blocked from executing while the locks
 * are held, so the locks should not be held longer than necessary.  The locks
 * should not be held across long HW delays.
 *
 * @returns NV_OK if locks are acquired successfully
 *          NV_ERR_INVALID_LOCK_STATE if locks cannot be acquired
 */
NV_STATUS
rmLocksAcquireAll(NvU32 module)
{
    OBJSYS    *pSys = SYS_GET_INSTANCE();

    if (osAcquireRmSemaForced(pSys->pSema) != NV_OK)
    {
        NV_PRINTF(LEVEL_ERROR, "Failed to acquire the RM lock!\n");
        return NV_ERR_INVALID_LOCK_STATE;
    }

    if (rmapiLockAcquire(API_LOCK_FLAGS_NONE, module) != NV_OK)
    {
        NV_PRINTF(LEVEL_ERROR, "Failed to acquire the API lock!\n");
        osReleaseRmSema(pSys->pSema, NULL);
        return NV_ERR_INVALID_LOCK_STATE;
    }

    if (rmGpuLocksAcquire(GPUS_LOCK_FLAGS_NONE, module) != NV_OK)
    {
        NV_PRINTF(LEVEL_ERROR, "Failed to acquire the GPU lock!\n");
        rmapiLockRelease();
        osReleaseRmSema(pSys->pSema, NULL);
        return NV_ERR_INVALID_LOCK_STATE;
    }

    return NV_OK;
}

/*!
 * @brief Releases the locks acquired by rmLocksAcquireAll
 */
void
rmLocksReleaseAll(void)
{
    OBJSYS    *pSys = SYS_GET_INSTANCE();

    rmGpuLocksRelease(GPUS_LOCK_FLAGS_NONE, NULL);
    rmapiLockRelease();
    osReleaseRmSema(pSys->pSema, NULL);
}


NV_STATUS
workItemLocksAcquire(NvU32 gpuInstance, OsQueueWorkItemFlags flags, OsQueueWorkItemFlags *pReleaseLocks, NvU32 *pGpuMask)
{
    OBJSYS   *pSys = SYS_GET_INSTANCE();
    OBJGPU   *pGpu;
    NvU32     grp;
    NV_STATUS status = NV_OK;

    *pReleaseLocks = (OsQueueWorkItemFlags){0};
    *pGpuMask = 0;

    if (flags.bSetExpandedGpuVisibility)
    {
        gpumgrThreadEnableExpandedGpuVisibility();
        pReleaseLocks->bSetExpandedGpuVisibility = NV_TRUE;
    }

    if (flags.bLockSema)
    {
        status = osAcquireRmSema(pSys->pSema);
        if (status != NV_OK)
            goto done;

        pReleaseLocks->bLockSema = NV_TRUE;
    }

    if (flags.apiLock != WORKITEM_FLAGS_API_LOCK_NOT_ACQUIRED)
    {
        status = rmapiLockAcquire(flags.apiLock == WORKITEM_FLAGS_API_LOCK_READ_ONLY ?
                                      RMAPI_LOCK_FLAGS_READ :
                                      RMAPI_LOCK_FLAGS_WRITE,
                                  RM_LOCK_MODULES_WORKITEM);
        if (status != NV_OK)
            goto done;

        pReleaseLocks->apiLock = flags.apiLock;
    }

    if (flags.bLockGpus ||
        flags.bLockGpuGroupDevice ||
        flags.bLockGpuGroupSubdevice)
    {
        NvU32 gpuLockFlags = GPUS_LOCK_FLAGS_NONE;

        if (flags.bLockGpus)
            grp = GPU_LOCK_GRP_ALL;
        else if (flags.bLockGpuGroupDevice)
            grp = GPU_LOCK_GRP_DEVICE;
        else // (flags.bLockGpuGroupSubdevice)
            grp = GPU_LOCK_GRP_SUBDEVICE;

        status = rmGpuGroupLockAcquire(gpuInstance, grp, gpuLockFlags,
                                       RM_LOCK_MODULES_WORKITEM, pGpuMask);
        if (status != NV_OK)
            goto done;

        // All of these call into the same function, just share the flag
        pReleaseLocks->bLockGpus = NV_TRUE;

        pGpu = gpumgrGetGpu(gpuInstance);
        if (pGpu == NULL)
        {
            status = NV_ERR_INVALID_ARGUMENT;
            goto done;
        }

        if (flags.bFullGpuSanity)
        {
            if (!FULL_GPU_SANITY_CHECK(pGpu) ||
                !pGpu->getProperty(pGpu, PDB_PROP_GPU_STATE_INITIALIZED))
            {
                status = NV_ERR_INVALID_STATE;
                NV_PRINTF(LEVEL_ERROR,
                          "GPU isn't full power! gpuInstance = 0x%x.\n",
                          gpuInstance);
                goto done;
            }
        }
    }

done:
    if (status != NV_OK)
    {
        workItemLocksRelease(*pReleaseLocks, *pGpuMask);
        *pReleaseLocks = (OsQueueWorkItemFlags){0};
    }
    return status;
}

void
workItemLocksRelease(OsQueueWorkItemFlags releaseLocks, NvU32 gpuMask)
{
    OBJSYS *pSys = SYS_GET_INSTANCE();

    if (releaseLocks.bLockGpus)
    {
        rmGpuGroupLockRelease(gpuMask, GPUS_LOCK_FLAGS_NONE);
    }

    if (releaseLocks.apiLock != WORKITEM_FLAGS_API_LOCK_NOT_ACQUIRED)
    {
        rmapiLockRelease();
    }

    if (releaseLocks.bLockSema)
    {
        osReleaseRmSema(pSys->pSema, NULL);
    }

    if (releaseLocks.bSetExpandedGpuVisibility)
    {
        gpumgrThreadDisableExpandedGpuVisibility();
    }
}

//
// rmGpuGroupLockGetMask
//
// Given a GPU group ID this function returns the MASK for all GPUS in that group
// We skip the lookup for GPU_LOCK_GRP_MASK as that implies that the caller is aware of the mask
//
NV_STATUS
rmGpuGroupLockGetMask(NvU32 gpuInst, GPU_LOCK_GRP_ID gpuGrpId, GPU_MASK *pGpuMask)
{
    switch (gpuGrpId)
    {
        case GPU_LOCK_GRP_SUBDEVICE:
            *pGpuMask = NVBIT(gpuInst);
            break;

        case GPU_LOCK_GRP_DEVICE:
            *pGpuMask = gpumgrGetGrpMaskFromGpuInst(gpuInst);
            break;

        case GPU_LOCK_GRP_MASK:
            break;

        case GPU_LOCK_GRP_ALL:
            *pGpuMask = GPUS_LOCK_ALL;
            break;

        default:
            NV_ASSERT_FAILED("Unexpected gpuGrpId in gpu lock get mask");
            return NV_ERR_INVALID_ARGUMENT;
    }
    return NV_OK;
}


void threadPriorityStateAlloc(void)          {}
void threadPriorityStateFree(void)           {}
void threadPriorityThrottle(void)            {}
void threadPriorityBoost(NvU64 *p, NvU64 *o) {}
void threadPriorityRestore(void)             {}

