1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023 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 "gpu/gpu.h"
25 #include "gpu/bus/kern_bus.h"
26 #include "gpu/subdevice/subdevice.h"
27 #include "vgpu/rpc.h"
28 
29 /*!
30  * @brief Setup BAR2 in physical mode.
31  *
32  * 1. Setup Bar2 in physical mode.
33  * 2. Create CPU Mapping for BAR2.
34  * 3. Use PA mode to setup RPC buffers in FB memory.
35  *
36  * @param[in] pGpu        OBJGPU pointer
37  * @param[in] pKernelBus  KernelBus pointer
38  *
39  * @returns NV_OK on success.
40  */
41 NV_STATUS
kbusBar2BootStrapInPhysicalMode_VGPUSTUB(OBJGPU * pGpu,KernelBus * pKernelBus)42 kbusBar2BootStrapInPhysicalMode_VGPUSTUB
43 (
44     OBJGPU    *pGpu,
45     KernelBus *pKernelBus
46 )
47 {
48     NV_STATUS status = NV_OK;
49 
50     if (!IS_VGPU_GSP_PLUGIN_OFFLOAD_ENABLED(pGpu))
51     {
52         return NV_ERR_NOT_SUPPORTED;
53     }
54 
55     if (kbusIsPhysicalBar2InitPagetableEnabled(pKernelBus))
56     {
57         //
58         // Bind in physical mode so that we can allocate
59         //  RPC buffers in BAR2 PA mode.
60         //
61         status = kbusBindBar2_HAL(pGpu, pKernelBus, BAR2_MODE_PHYSICAL);
62 
63         if (NV_OK != status)
64         {
65             NV_PRINTF(LEVEL_ERROR, "Unable to bind BAR2 to physical mode.\n");
66             return status;
67         }
68     }
69     else
70     {
71         return NV_ERR_NOT_SUPPORTED;
72     }
73 
74     status = kbusSetupBar2CpuAperture_HAL(pGpu, pKernelBus, GPU_GFID_PF);
75     NV_ASSERT_OR_GOTO(status == NV_OK, cleanup);
76 
77     pKernelBus->bIsBar2SetupInPhysicalMode = NV_TRUE;
78 
79 cleanup:
80     if (status != NV_OK)
81     {
82         kbusTeardownBar2CpuAperture_HAL(pGpu, pKernelBus, GPU_GFID_PF);
83     }
84 
85     return status;
86 }
87 
88 /*!
89  * @brief Initialize pciBarSizes[], set pKernelBus->bPciBarSizesValid
90  *
91  * @param[in] pGpu
92  * @param[in] pKernelBus
93  *
94  * @returns void
95  */
96 NV_STATUS
kbusInitBarsSize_VGPUSTUB(OBJGPU * pGpu,KernelBus * pKernelBus)97 kbusInitBarsSize_VGPUSTUB(OBJGPU *pGpu, KernelBus *pKernelBus)
98 {
99     if (!pKernelBus->bPciBarSizesValid)
100     {
101         pKernelBus->pciBarSizes[0] = kbusGetVfBar0SizeBytes(pGpu, pKernelBus);
102         pKernelBus->pciBarSizes[1] = pGpu->fbLength;
103         pKernelBus->pciBarSizes[2] = BUS_BAR2_APERTURE_MB;
104 
105         pKernelBus->bPciBarSizesValid = NV_TRUE;
106     }
107     return NV_OK;
108 }
109 
110 NV_STATUS
subdeviceCtrlCmdBusSetP2pMapping_VGPUSTUB(Subdevice * pSubdevice,NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS * pParams)111 subdeviceCtrlCmdBusSetP2pMapping_VGPUSTUB
112 (
113     Subdevice *pSubdevice,
114     NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS *pParams
115 )
116 {
117     NV_STATUS status = NV_OK;
118     OBJGPU *pGpu = GPU_RES_GET_GPU(pSubdevice);
119     OBJGPU *pRemoteGpu = gpumgrGetGpuFromId(pParams->remoteGpuId);
120     CALL_CONTEXT *pCallContext = resservGetTlsCallContext();
121     RS_RES_CONTROL_PARAMS_INTERNAL *pControlParams = pCallContext->pControlParams;
122     NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS shimParams = {0};
123 
124     NV_CHECK_OR_RETURN(LEVEL_ERROR, pRemoteGpu != NULL, NV_ERR_INVALID_ARGUMENT);
125     NV_CHECK_OR_RETURN(LEVEL_ERROR, pParams->bUseUuid == NV_FALSE, NV_ERR_NOT_SUPPORTED);
126     NV_CHECK_OR_RETURN(LEVEL_ERROR,
127                        pParams->connectionType != NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING_CONNECTION_TYPE_INVALID,
128                        NV_ERR_INVALID_ARGUMENT);
129 
130     portMemCopy(&shimParams, sizeof(NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS),
131                 pParams, sizeof(NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS));
132 
133     portMemCopy(shimParams.remoteGpuUuid, sizeof(shimParams.remoteGpuUuid),
134                 pRemoteGpu->gpuUuid.uuid, sizeof(pRemoteGpu->gpuUuid.uuid));
135 
136     shimParams.bUseUuid = NV_TRUE;
137 
138     NV_RM_RPC_CONTROL(pGpu,
139                       pControlParams->hClient,
140                       pControlParams->hObject,
141                       pControlParams->cmd,
142                       &shimParams,
143                       sizeof(shimParams),
144                       status);
145 
146     return status;
147 }
148 
149 NV_STATUS
subdeviceCtrlCmdBusUnsetP2pMapping_VGPUSTUB(Subdevice * pSubdevice,NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS * pParams)150 subdeviceCtrlCmdBusUnsetP2pMapping_VGPUSTUB
151 (
152     Subdevice *pSubdevice,
153     NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS *pParams
154 )
155 {
156     NV_STATUS status = NV_OK;
157     OBJGPU *pGpu = GPU_RES_GET_GPU(pSubdevice);
158     OBJGPU *pRemoteGpu = gpumgrGetGpuFromId(pParams->remoteGpuId);
159     CALL_CONTEXT *pCallContext = resservGetTlsCallContext();
160     RS_RES_CONTROL_PARAMS_INTERNAL *pControlParams = pCallContext->pControlParams;
161     NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS shimParams = {0};
162 
163     NV_CHECK_OR_RETURN(LEVEL_ERROR, pRemoteGpu != NULL, NV_ERR_INVALID_ARGUMENT);
164     NV_CHECK_OR_RETURN(LEVEL_ERROR, pParams->bUseUuid == NV_FALSE, NV_ERR_NOT_SUPPORTED);
165     NV_CHECK_OR_RETURN(LEVEL_ERROR,
166                        pParams->connectionType != NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING_CONNECTION_TYPE_INVALID,
167                        NV_ERR_INVALID_ARGUMENT);
168 
169     portMemCopy(&shimParams, sizeof(NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS),
170                 pParams, sizeof(NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS));
171 
172     portMemCopy(shimParams.remoteGpuUuid, sizeof(shimParams.remoteGpuUuid),
173                 pRemoteGpu->gpuUuid.uuid, sizeof(pRemoteGpu->gpuUuid.uuid));
174 
175     shimParams.bUseUuid = NV_TRUE;
176 
177     NV_RM_RPC_CONTROL(pGpu,
178                       pControlParams->hClient,
179                       pControlParams->hObject,
180                       pControlParams->cmd,
181                       &shimParams,
182                       sizeof(shimParams),
183                       status);
184 
185     return status;
186 }
187