1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 1993-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "core/locks.h"
25 #include "gpu/ce/kernel_ce.h"
26 #include "gpu/ce/kernel_ce_private.h"
27 #include "gpu/subdevice/subdevice.h"
28 #include "gpu_mgr/gpu_mgr.h"
29 #include "vgpu/rpc.h"
30 
31 //
32 // CE RM Device Controls
33 //
34 
35 //
36 // NV2080_CTRL_CMD_CE_GET_CAPS passes userspace pointer for copyin/out.
37 // NV2080_CTRL_CMD_CE_GET_CAPS_V2 stores data inline.
38 //
39 
40 //
41 // Lock Requirements:
42 //      Assert that API lock held on entry
43 //
44 NV_STATUS
45 subdeviceCtrlCmdCeGetCaps_IMPL
46 (
47     Subdevice *pSubdevice,
48     NV2080_CTRL_CE_GET_CAPS_PARAMS *pCeCapsParams
49 )
50 {
51     OBJGPU     *pGpu = GPU_RES_GET_GPU(pSubdevice);
52     KernelCE    *pKCe;
53     NvU32       ceNumber;
54     NV_STATUS   status = NV_OK;
55     RM_ENGINE_TYPE rmEngineType;
56 
57     LOCK_ASSERT_AND_RETURN(rmapiLockIsOwner());
58 
59     // sanity check array size
60     if (pCeCapsParams->capsTblSize != NV2080_CTRL_CE_CAPS_TBL_SIZE)
61     {
62         NV_PRINTF(LEVEL_ERROR, "size mismatch: client 0x%x rm 0x%x\n",
63                   pCeCapsParams->capsTblSize, NV2080_CTRL_CE_CAPS_TBL_SIZE);
64         return NV_ERR_INVALID_ARGUMENT;
65     }
66 
67     rmEngineType = gpuGetRmEngineType(pCeCapsParams->ceEngineType);
68 
69     if (!RM_ENGINE_TYPE_IS_COPY(rmEngineType))
70     {
71         return NV_ERR_NOT_SUPPORTED;
72     }
73 
74     //
75     // vGPU:
76     //
77     // Since vGPU does all real hardware management in the
78     // host, if we are in guest OS (where IS_VIRTUAL(pGpu) is true),
79     // do an RPC to the host to get blacklist information from host RM
80     //
81     if (IS_VIRTUAL(pGpu))
82     {
83         CALL_CONTEXT *pCallContext = resservGetTlsCallContext();
84         RmCtrlParams *pRmCtrlParams = pCallContext->pControlParams;
85         NV2080_CTRL_CE_GET_CAPS_V2_PARAMS ceCapsv2Params = { 0 };
86 
87         ceCapsv2Params.ceEngineType = pCeCapsParams->ceEngineType;
88 
89         NV_RM_RPC_CONTROL(pGpu, pRmCtrlParams->hClient,
90                           pRmCtrlParams->hObject,
91                           NV2080_CTRL_CMD_CE_GET_CAPS_V2,
92                           &ceCapsv2Params,
93                           sizeof(ceCapsv2Params),
94                           status);
95 
96         if (status == NV_OK)
97         {
98             portMemCopy(NvP64_VALUE(pCeCapsParams->capsTbl),
99                         (sizeof(NvU8) * NV2080_CTRL_CE_CAPS_TBL_SIZE),
100                         ceCapsv2Params.capsTbl,
101                         (sizeof(NvU8) * NV2080_CTRL_CE_CAPS_TBL_SIZE));
102         }
103         return status;
104     }
105 
106     NV_ASSERT_OK_OR_RETURN(ceIndexFromType(pGpu, RES_GET_CLIENT_HANDLE(pSubdevice), rmEngineType, &ceNumber));
107 
108     pKCe = GPU_GET_KCE(pGpu, ceNumber);
109 
110     // Return an unsupported error for not present or stubbed CEs as they are
111     // not supposed to be user visible and cannot be allocated anyway.
112     if (!pKCe)
113     {
114         return NV_ERR_NOT_SUPPORTED;
115     }
116 
117     // now fill in caps for this CE
118     return kceGetDeviceCaps(pGpu, pKCe, rmEngineType, NvP64_VALUE(pCeCapsParams->capsTbl));
119 }
120