1 #ifndef _G_RS_RESOURCE_NVOC_H_
2 #define _G_RS_RESOURCE_NVOC_H_
3 #include "nvoc/runtime.h"
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 /*
10  * SPDX-FileCopyrightText: Copyright (c) 2015-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
11  * SPDX-License-Identifier: MIT
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  */
31 
32 #include "g_rs_resource_nvoc.h"
33 
34 #ifndef _RS_RESOURCE_H_
35 #define _RS_RESOURCE_H_
36 
37 #include "nvport/nvport.h"
38 #include "resserv/resserv.h"
39 #include "nvoc/object.h"
40 #include "resserv/rs_access_map.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 struct RsSession;
47 
48 #ifndef __NVOC_CLASS_RsSession_TYPEDEF__
49 #define __NVOC_CLASS_RsSession_TYPEDEF__
50 typedef struct RsSession RsSession;
51 #endif /* __NVOC_CLASS_RsSession_TYPEDEF__ */
52 
53 #ifndef __nvoc_class_id_RsSession
54 #define __nvoc_class_id_RsSession 0x830d90
55 #endif /* __nvoc_class_id_RsSession */
56 
57 
58 
59 /**
60  * @defgroup RsResource
61  * @addtogroup RsResource
62  * @{*/
63 
64 #define ALLOC_STATE_INTERNAL_CLIENT_HANDLE   NVBIT(5)
65 
66 /*
67  * Locking operations for lock-metering
68  */
69 #define RS_LOCK_TRACE_INVALID     1
70 #define RS_LOCK_TRACE_ACQUIRE     1
71 #define RS_LOCK_TRACE_RELEASE     2
72 #define RS_LOCK_TRACE_ALLOC       3
73 #define RS_LOCK_TRACE_FREE        4
74 #define RS_LOCK_TRACE_CTRL        5
75 #define RS_LOCK_TRACE_MAP         6
76 #define RS_LOCK_TRACE_UNMAP       7
77 
78 /**
79  * Context information for top-level, resource-level, and client-level locking
80  * operations
81  */
82 struct RS_LOCK_INFO
83 {
84     struct RsClient *pClient;              ///< Pointer to client that was locked (if any)
85     struct RsClient *pSecondClient;        ///< Pointer to second client, for dual-client locking
86     RsResourceRef *pContextRef;     ///< User-defined reference
87     struct RsSession *pSession;            ///< Session object to be locked, if any
88     NvU32 flags;                    ///< RS_LOCK_FLAGS_*
89     NvU32 state;                    ///< RS_LOCK_STATE_*
90     NvU32 gpuMask;
91     NvU8  traceOp;                  ///< RS_LOCK_TRACE_* operation for lock-metering
92     NvU32 traceClassId;             ///< Class of initial resource that was locked for lock metering
93 };
94 
95 struct RS_RES_ALLOC_PARAMS_INTERNAL
96 {
97     NvHandle hClient;       ///< [in] The handle of the resource's client
98     NvHandle hParent;       ///< [in] The handle of the resource's parent. This may be a client or another resource.
99     NvHandle hResource;     ///< [inout] Server will assign a handle if this is 0, or else try the value provided
100     NvU32 externalClassId;  ///< [in] External class ID of resource
101     NvHandle hDomain;       ///< UNUSED
102 
103     // Internal use only
104     RS_LOCK_INFO           *pLockInfo;        ///< [inout] Locking flags and state
105     struct RsClient               *pClient;          ///< [out] Cached client
106     RsResourceRef          *pResourceRef;     ///< [out] Cached resource reference
107     NvU32                   allocFlags;       ///< [in] Allocation flags
108     NvU32                   allocState;       ///< [inout] Allocation state
109     API_SECURITY_INFO      *pSecInfo;
110 
111     void                   *pAllocParams;     ///< [in] Copied-in allocation parameters
112 
113     // ... Dupe alloc
114     struct RsClient               *pSrcClient;       ///< The client that is sharing the resource
115     RsResourceRef          *pSrcRef;          ///< Reference to the resource that will be shared
116 
117     RS_ACCESS_MASK         *pRightsRequested; ///< [in] Access rights requested on the new resource
118     // Buffer for storing contents of user mask. Do not use directly, use pRightsRequested instead.
119     RS_ACCESS_MASK          rightsRequestedCopy;
120 
121     RS_ACCESS_MASK         *pRightsRequired;  ///< [in] Access rights required to alloc this object type
122 };
123 
124 struct RS_RES_DUP_PARAMS_INTERNAL
125 {
126     NvHandle            hClientSrc;            ///< [in] The handle of the source resource's client
127     NvHandle            hResourceSrc;          ///< [in] The handle of the source resource.
128     NvHandle            hClientDst;            ///< [in] The handle of the destination resource's client (may be different from source client)
129     NvHandle            hParentDst;            ///< [in] The handle of the destination resource's parent.
130     NvHandle            hResourceDst;          ///< [inout] The handle of the destination resource. Generated if 0.
131     void               *pShareParams;          ///< [in] Copied-in sharing parameters
132     NvU32               flags;                 ///< [in] Flags to denote special cases ( Bug: 2859347 to track removal)
133     // Internal use only
134     struct RsClient           *pSrcClient;
135     RsResourceRef      *pSrcRef;
136     API_SECURITY_INFO  *pSecInfo;              ///< [in] Security info
137     RS_LOCK_INFO       *pLockInfo;             ///< [inout] Locking flags and state
138 };
139 
140 struct RS_RES_SHARE_PARAMS_INTERNAL
141 {
142     NvHandle            hClient;            ///< [in] The handle of the owner's client
143     NvHandle            hResource;          ///< [in] The handle of the resource.
144     RS_SHARE_POLICY    *pSharePolicy;       ///< [in] The policy to share with
145 
146     // Internal use only
147     API_SECURITY_INFO  *pSecInfo;           ///< [in] Security info
148     RS_LOCK_INFO       *pLockInfo;          ///< [inout] Locking flags and state
149 };
150 
151 #define RS_IS_COPY_CTOR(pParams) ((pParams)->pSrcRef != NULL)
152 
153 struct RS_RES_FREE_PARAMS_INTERNAL
154 {
155     NvHandle           hClient;         ///< [in] The handle of the resource's client
156     NvHandle           hResource;       ///< [in] The handle of the resource
157     NvBool             bInvalidateOnly; ///< [in] Free the resource, but don't release its handle
158     NvHandle           hDomain;         ///< UNUSED
159 
160     // Internal use only
161     NvBool             bHiPriOnly;      ///< [in] Only free if this is a high priority resources
162     NvBool             bDisableOnly;    ///< [in] Disable the target instead of freeing it (only applies to clients)
163     RS_LOCK_INFO      *pLockInfo;       ///< [inout] Locking flags and state
164     NvU32              freeFlags;       ///< [in] Flags for the free operation
165     NvU32              freeState;       ///< [inout] Free state
166     RsResourceRef     *pResourceRef;    ///< [inout] Cached RsResourceRef
167     NV_STATUS          status;          ///< [out] Status of free operation
168     API_SECURITY_INFO *pSecInfo;        ///< [in] Security info
169 };
170 
171 struct NVOC_EXPORTED_METHOD_DEF;
172 struct OBJGPU;
173 
174 #ifndef __NVOC_CLASS_OBJGPU_TYPEDEF__
175 #define __NVOC_CLASS_OBJGPU_TYPEDEF__
176 typedef struct OBJGPU OBJGPU;
177 #endif /* __NVOC_CLASS_OBJGPU_TYPEDEF__ */
178 
179 #ifndef __nvoc_class_id_OBJGPU
180 #define __nvoc_class_id_OBJGPU 0x7ef3cb
181 #endif /* __nvoc_class_id_OBJGPU */
182 
183 
184 struct OBJGPUGRP;
185 
186 #ifndef __NVOC_CLASS_OBJGPUGRP_TYPEDEF__
187 #define __NVOC_CLASS_OBJGPUGRP_TYPEDEF__
188 typedef struct OBJGPUGRP OBJGPUGRP;
189 #endif /* __NVOC_CLASS_OBJGPUGRP_TYPEDEF__ */
190 
191 #ifndef __nvoc_class_id_OBJGPUGRP
192 #define __nvoc_class_id_OBJGPUGRP 0xe40531
193 #endif /* __nvoc_class_id_OBJGPUGRP */
194 
195 
196 
197 //
198 // RS_RES_CONTROL_PARAMS
199 //
200 // This structure encapsulates data sent to the cmd-specific rmctrl
201 // handlers.  Along with the arguments supplied by the requesting
202 // client (hClient, hObject, cmd, pParams, paramSize).
203 //
204 struct RS_RES_CONTROL_PARAMS_INTERNAL
205 {
206     NvHandle                hClient;          // client-specified NV01_ROOT object handle
207     NvHandle                hObject;          // client-specified object handle
208     NvU32                   cmd;              // client-specified command #
209     NvU32                   flags;            // flags related to control call execution
210     void                   *pParams;          // client-specified params (in kernel space)
211     NvU32                   paramsSize;       // client-specified size of pParams in bytes
212 
213     NvHandle                hParent;          // handle of hObject parent
214     struct OBJGPU                 *pGpu;             // ptr to OBJGPU struct if applicable
215     struct OBJGPUGRP              *pGpuGrp;          // ptr to OBJGPUGRP struct if applicable
216     RsResourceRef          *pResourceRef;     // ptr to RsResourceRef if object is managed by
217                                               // Resource Server
218     API_SECURITY_INFO       secInfo;          // information on privilege level and pointer location (user/kernel)
219     RS_LOCK_INFO           *pLockInfo;        ///< [inout] Locking flags and state
220     RS_CONTROL_COOKIE      *pCookie;
221     NvBool                  bInternal;        // True if control call was not issued from an external client
222     NvBool                  bDeferredApi;     // Indicates ctrl is being dispatched via deferred API
223 
224     struct RS_RES_CONTROL_PARAMS_INTERNAL *pLegacyParams; // RS-TODO removeme
225 };
226 
227 struct RS_RES_DTOR_PARAMS
228 {
229     CALL_CONTEXT                 *pFreeContext;
230     RS_RES_FREE_PARAMS_INTERNAL  *pFreeParams;
231 };
232 
233 /**
234  * Base class for all resources. Mostly a pure virtual interface which
235  * should be overridden to implement resource specific behavior.
236  */
237 #ifdef NVOC_RS_RESOURCE_H_PRIVATE_ACCESS_ALLOWED
238 #define PRIVATE_FIELD(x) x
239 #else
240 #define PRIVATE_FIELD(x) NVOC_PRIVATE_FIELD(x)
241 #endif
242 struct RsResource {
243     const struct NVOC_RTTI *__nvoc_rtti;
244     struct Object __nvoc_base_Object;
245     struct Object *__nvoc_pbase_Object;
246     struct RsResource *__nvoc_pbase_RsResource;
247     NvBool (*__resCanCopy__)(struct RsResource *);
248     NV_STATUS (*__resIsDuplicate__)(struct RsResource *, NvHandle, NvBool *);
249     void (*__resPreDestruct__)(struct RsResource *);
250     NV_STATUS (*__resControlLookup__)(struct RsResource *, struct RS_RES_CONTROL_PARAMS_INTERNAL *, const struct NVOC_EXPORTED_METHOD_DEF **);
251     NV_STATUS (*__resControl__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
252     NV_STATUS (*__resControlFilter__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
253     NV_STATUS (*__resControlSerialization_Prologue__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
254     void (*__resControlSerialization_Epilogue__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
255     NV_STATUS (*__resControl_Prologue__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
256     void (*__resControl_Epilogue__)(struct RsResource *, struct CALL_CONTEXT *, struct RS_RES_CONTROL_PARAMS_INTERNAL *);
257     NV_STATUS (*__resMap__)(struct RsResource *, struct CALL_CONTEXT *, RS_CPU_MAP_PARAMS *, RsCpuMapping *);
258     NV_STATUS (*__resUnmap__)(struct RsResource *, struct CALL_CONTEXT *, RsCpuMapping *);
259     NV_STATUS (*__resMapTo__)(struct RsResource *, RS_RES_MAP_TO_PARAMS *);
260     NV_STATUS (*__resUnmapFrom__)(struct RsResource *, RS_RES_UNMAP_FROM_PARAMS *);
261     NvU32 (*__resGetRefCount__)(struct RsResource *);
262     NvBool (*__resAccessCallback__)(struct RsResource *, struct RsClient *, void *, RsAccessRight);
263     NvBool (*__resShareCallback__)(struct RsResource *, struct RsClient *, RsResourceRef *, RS_SHARE_POLICY *);
264     void (*__resAddAdditionalDependants__)(struct RsClient *, struct RsResource *, RsResourceRef *);
265     RsResourceRef *pResourceRef;
266     struct RS_RES_DTOR_PARAMS dtorParams;
267     NvBool bConstructed;
268 };
269 
270 #ifndef __NVOC_CLASS_RsResource_TYPEDEF__
271 #define __NVOC_CLASS_RsResource_TYPEDEF__
272 typedef struct RsResource RsResource;
273 #endif /* __NVOC_CLASS_RsResource_TYPEDEF__ */
274 
275 #ifndef __nvoc_class_id_RsResource
276 #define __nvoc_class_id_RsResource 0xd551cb
277 #endif /* __nvoc_class_id_RsResource */
278 
279 extern const struct NVOC_CLASS_DEF __nvoc_class_def_RsResource;
280 
281 #define __staticCast_RsResource(pThis) \
282     ((pThis)->__nvoc_pbase_RsResource)
283 
284 #ifdef __nvoc_rs_resource_h_disabled
285 #define __dynamicCast_RsResource(pThis) ((RsResource*)NULL)
286 #else //__nvoc_rs_resource_h_disabled
287 #define __dynamicCast_RsResource(pThis) \
288     ((RsResource*)__nvoc_dynamicCast(staticCast((pThis), Dynamic), classInfo(RsResource)))
289 #endif //__nvoc_rs_resource_h_disabled
290 
291 
292 NV_STATUS __nvoc_objCreateDynamic_RsResource(RsResource**, Dynamic*, NvU32, va_list);
293 
294 NV_STATUS __nvoc_objCreate_RsResource(RsResource**, Dynamic*, NvU32, struct CALL_CONTEXT * arg_pCallContext, struct RS_RES_ALLOC_PARAMS_INTERNAL * arg_pParams);
295 #define __objCreate_RsResource(ppNewObj, pParent, createFlags, arg_pCallContext, arg_pParams) \
296     __nvoc_objCreate_RsResource((ppNewObj), staticCast((pParent), Dynamic), (createFlags), arg_pCallContext, arg_pParams)
297 
298 #define resCanCopy(pResource) resCanCopy_DISPATCH(pResource)
299 #define resIsDuplicate(pResource, hMemory, pDuplicate) resIsDuplicate_DISPATCH(pResource, hMemory, pDuplicate)
300 #define resPreDestruct(pResource) resPreDestruct_DISPATCH(pResource)
301 #define resControlLookup(pResource, pParams, ppEntry) resControlLookup_DISPATCH(pResource, pParams, ppEntry)
302 #define resControl(pResource, pCallContext, pParams) resControl_DISPATCH(pResource, pCallContext, pParams)
303 #define resControlFilter(pResource, pCallContext, pParams) resControlFilter_DISPATCH(pResource, pCallContext, pParams)
304 #define resControlSerialization_Prologue(pResource, pCallContext, pParams) resControlSerialization_Prologue_DISPATCH(pResource, pCallContext, pParams)
305 #define resControlSerialization_Epilogue(pResource, pCallContext, pParams) resControlSerialization_Epilogue_DISPATCH(pResource, pCallContext, pParams)
306 #define resControl_Prologue(pResource, pCallContext, pParams) resControl_Prologue_DISPATCH(pResource, pCallContext, pParams)
307 #define resControl_Epilogue(pResource, pCallContext, pParams) resControl_Epilogue_DISPATCH(pResource, pCallContext, pParams)
308 #define resMap(pResource, pCallContext, pParams, pCpuMapping) resMap_DISPATCH(pResource, pCallContext, pParams, pCpuMapping)
309 #define resUnmap(pResource, pCallContext, pCpuMapping) resUnmap_DISPATCH(pResource, pCallContext, pCpuMapping)
310 #define resMapTo(pResource, pParams) resMapTo_DISPATCH(pResource, pParams)
311 #define resUnmapFrom(pResource, pParams) resUnmapFrom_DISPATCH(pResource, pParams)
312 #define resGetRefCount(pResource) resGetRefCount_DISPATCH(pResource)
313 #define resAccessCallback(pResource, pInvokingClient, pAllocParams, accessRight) resAccessCallback_DISPATCH(pResource, pInvokingClient, pAllocParams, accessRight)
314 #define resShareCallback(pResource, pInvokingClient, pParentRef, pSharePolicy) resShareCallback_DISPATCH(pResource, pInvokingClient, pParentRef, pSharePolicy)
315 #define resAddAdditionalDependants(pClient, pResource, pReference) resAddAdditionalDependants_DISPATCH(pClient, pResource, pReference)
316 NvBool resCanCopy_IMPL(struct RsResource *pResource);
317 
318 static inline NvBool resCanCopy_DISPATCH(struct RsResource *pResource) {
319     return pResource->__resCanCopy__(pResource);
320 }
321 
322 NV_STATUS resIsDuplicate_IMPL(struct RsResource *pResource, NvHandle hMemory, NvBool *pDuplicate);
323 
324 static inline NV_STATUS resIsDuplicate_DISPATCH(struct RsResource *pResource, NvHandle hMemory, NvBool *pDuplicate) {
325     return pResource->__resIsDuplicate__(pResource, hMemory, pDuplicate);
326 }
327 
328 void resPreDestruct_IMPL(struct RsResource *pResource);
329 
330 static inline void resPreDestruct_DISPATCH(struct RsResource *pResource) {
331     pResource->__resPreDestruct__(pResource);
332 }
333 
334 NV_STATUS resControlLookup_IMPL(struct RsResource *pResource, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams, const struct NVOC_EXPORTED_METHOD_DEF **ppEntry);
335 
336 static inline NV_STATUS resControlLookup_DISPATCH(struct RsResource *pResource, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams, const struct NVOC_EXPORTED_METHOD_DEF **ppEntry) {
337     return pResource->__resControlLookup__(pResource, pParams, ppEntry);
338 }
339 
340 NV_STATUS resControl_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
341 
342 static inline NV_STATUS resControl_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
343     return pResource->__resControl__(pResource, pCallContext, pParams);
344 }
345 
346 NV_STATUS resControlFilter_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
347 
348 static inline NV_STATUS resControlFilter_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
349     return pResource->__resControlFilter__(pResource, pCallContext, pParams);
350 }
351 
352 NV_STATUS resControlSerialization_Prologue_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
353 
354 static inline NV_STATUS resControlSerialization_Prologue_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
355     return pResource->__resControlSerialization_Prologue__(pResource, pCallContext, pParams);
356 }
357 
358 void resControlSerialization_Epilogue_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
359 
360 static inline void resControlSerialization_Epilogue_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
361     pResource->__resControlSerialization_Epilogue__(pResource, pCallContext, pParams);
362 }
363 
364 NV_STATUS resControl_Prologue_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
365 
366 static inline NV_STATUS resControl_Prologue_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
367     return pResource->__resControl_Prologue__(pResource, pCallContext, pParams);
368 }
369 
370 void resControl_Epilogue_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams);
371 
372 static inline void resControl_Epilogue_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_CONTROL_PARAMS_INTERNAL *pParams) {
373     pResource->__resControl_Epilogue__(pResource, pCallContext, pParams);
374 }
375 
376 NV_STATUS resMap_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, RS_CPU_MAP_PARAMS *pParams, RsCpuMapping *pCpuMapping);
377 
378 static inline NV_STATUS resMap_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, RS_CPU_MAP_PARAMS *pParams, RsCpuMapping *pCpuMapping) {
379     return pResource->__resMap__(pResource, pCallContext, pParams, pCpuMapping);
380 }
381 
382 NV_STATUS resUnmap_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, RsCpuMapping *pCpuMapping);
383 
384 static inline NV_STATUS resUnmap_DISPATCH(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, RsCpuMapping *pCpuMapping) {
385     return pResource->__resUnmap__(pResource, pCallContext, pCpuMapping);
386 }
387 
388 NV_STATUS resMapTo_IMPL(struct RsResource *pResource, RS_RES_MAP_TO_PARAMS *pParams);
389 
390 static inline NV_STATUS resMapTo_DISPATCH(struct RsResource *pResource, RS_RES_MAP_TO_PARAMS *pParams) {
391     return pResource->__resMapTo__(pResource, pParams);
392 }
393 
394 NV_STATUS resUnmapFrom_IMPL(struct RsResource *pResource, RS_RES_UNMAP_FROM_PARAMS *pParams);
395 
396 static inline NV_STATUS resUnmapFrom_DISPATCH(struct RsResource *pResource, RS_RES_UNMAP_FROM_PARAMS *pParams) {
397     return pResource->__resUnmapFrom__(pResource, pParams);
398 }
399 
400 NvU32 resGetRefCount_IMPL(struct RsResource *pResource);
401 
402 static inline NvU32 resGetRefCount_DISPATCH(struct RsResource *pResource) {
403     return pResource->__resGetRefCount__(pResource);
404 }
405 
406 NvBool resAccessCallback_IMPL(struct RsResource *pResource, struct RsClient *pInvokingClient, void *pAllocParams, RsAccessRight accessRight);
407 
408 static inline NvBool resAccessCallback_DISPATCH(struct RsResource *pResource, struct RsClient *pInvokingClient, void *pAllocParams, RsAccessRight accessRight) {
409     return pResource->__resAccessCallback__(pResource, pInvokingClient, pAllocParams, accessRight);
410 }
411 
412 NvBool resShareCallback_IMPL(struct RsResource *pResource, struct RsClient *pInvokingClient, RsResourceRef *pParentRef, RS_SHARE_POLICY *pSharePolicy);
413 
414 static inline NvBool resShareCallback_DISPATCH(struct RsResource *pResource, struct RsClient *pInvokingClient, RsResourceRef *pParentRef, RS_SHARE_POLICY *pSharePolicy) {
415     return pResource->__resShareCallback__(pResource, pInvokingClient, pParentRef, pSharePolicy);
416 }
417 
418 void resAddAdditionalDependants_IMPL(struct RsClient *pClient, struct RsResource *pResource, RsResourceRef *pReference);
419 
420 static inline void resAddAdditionalDependants_DISPATCH(struct RsClient *pClient, struct RsResource *pResource, RsResourceRef *pReference) {
421     pResource->__resAddAdditionalDependants__(pClient, pResource, pReference);
422 }
423 
424 NV_STATUS resConstruct_IMPL(struct RsResource *arg_pResource, struct CALL_CONTEXT *arg_pCallContext, struct RS_RES_ALLOC_PARAMS_INTERNAL *arg_pParams);
425 
426 #define __nvoc_resConstruct(arg_pResource, arg_pCallContext, arg_pParams) resConstruct_IMPL(arg_pResource, arg_pCallContext, arg_pParams)
427 void resDestruct_IMPL(struct RsResource *pResource);
428 
429 #define __nvoc_resDestruct(pResource) resDestruct_IMPL(pResource)
430 NV_STATUS resSetFreeParams_IMPL(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_FREE_PARAMS_INTERNAL *pParams);
431 
432 #ifdef __nvoc_rs_resource_h_disabled
433 static inline NV_STATUS resSetFreeParams(struct RsResource *pResource, struct CALL_CONTEXT *pCallContext, struct RS_RES_FREE_PARAMS_INTERNAL *pParams) {
434     NV_ASSERT_FAILED_PRECOMP("RsResource was disabled!");
435     return NV_ERR_NOT_SUPPORTED;
436 }
437 #else //__nvoc_rs_resource_h_disabled
438 #define resSetFreeParams(pResource, pCallContext, pParams) resSetFreeParams_IMPL(pResource, pCallContext, pParams)
439 #endif //__nvoc_rs_resource_h_disabled
440 
441 NV_STATUS resGetFreeParams_IMPL(struct RsResource *pResource, struct CALL_CONTEXT **ppCallContext, struct RS_RES_FREE_PARAMS_INTERNAL **ppParams);
442 
443 #ifdef __nvoc_rs_resource_h_disabled
444 static inline NV_STATUS resGetFreeParams(struct RsResource *pResource, struct CALL_CONTEXT **ppCallContext, struct RS_RES_FREE_PARAMS_INTERNAL **ppParams) {
445     NV_ASSERT_FAILED_PRECOMP("RsResource was disabled!");
446     return NV_ERR_NOT_SUPPORTED;
447 }
448 #else //__nvoc_rs_resource_h_disabled
449 #define resGetFreeParams(pResource, ppCallContext, ppParams) resGetFreeParams_IMPL(pResource, ppCallContext, ppParams)
450 #endif //__nvoc_rs_resource_h_disabled
451 
452 #undef PRIVATE_FIELD
453 
454 
455 /* @} */
456 
457 struct OBJGPU;
458 
459 #ifndef __NVOC_CLASS_OBJGPU_TYPEDEF__
460 #define __NVOC_CLASS_OBJGPU_TYPEDEF__
461 typedef struct OBJGPU OBJGPU;
462 #endif /* __NVOC_CLASS_OBJGPU_TYPEDEF__ */
463 
464 #ifndef __nvoc_class_id_OBJGPU
465 #define __nvoc_class_id_OBJGPU 0x7ef3cb
466 #endif /* __nvoc_class_id_OBJGPU */
467 
468 
469 
470 /**
471  * @defgroup RsCpuMapping
472  * @addtogroup RsCpuMapping
473  * @{*/
474 struct RsCpuMapping
475 {
476     NvU64 offset;
477     NvU64 length;
478     NvU32 flags;
479     NvP64 pLinearAddress;
480     RsResourceRef *pContextRef;      ///< Context resource that may be needed for the mapping
481     void *pContext;                  ///< Additional context data for the mapping
482     NvU32 processId;
483 
484     RS_CPU_MAPPING_PRIVATE *pPrivate; ///< Opaque struct allocated and freed by resserv on behalf of the user
485 };
486 MAKE_LIST(RsCpuMappingList, RsCpuMapping);
487 
488 /**
489  * CPU mapping parameters
490  */
491 struct RS_CPU_MAP_PARAMS
492 {
493     NvHandle                hClient;
494     NvHandle                hDevice;
495     NvHandle                hMemory;
496     NvU64                   offset;         ///< [in] Offset into the resource
497     NvU64                   length;         ///< [in] Size of the region to map
498     NvP64                  *ppCpuVirtAddr;
499     NvU32                   flags;          ///< [in] Resource-specific flags
500 
501     // Passed from RM into CpuMapping
502     NvU32                   protect;        ///< [in] Protection flags
503     NvBool                  bKernel;
504 
505     /// [in] hContext Handle of resource that provides a context for the mapping (e.g., subdevice for channel map)
506     NvHandle hContext;
507 
508     RS_LOCK_INFO   *pLockInfo;              ///< [inout] Locking flags and state
509     API_SECURITY_INFO *pSecInfo;            ///< [in] Security Info
510 };
511 
512 /**
513  * CPU unmapping params for resource server tests
514  */
515 struct RS_CPU_UNMAP_PARAMS
516 {
517     NvHandle                hClient;
518     NvHandle                hDevice;
519     NvHandle                hMemory;
520     NvP64                   pLinearAddress; ///< [in] Address of mapped memory
521     NvU32                   flags;          ///< [in] Resource-specific flags
522     NvU32                   processId;
523     NvBool                  bTeardown;      ///< [in] Unmap operation is due to client teardown
524 
525     /// [in] hContext Handle of resource that provides a context for the mapping (e.g., subdevice for channel map)
526     NvHandle                hContext;
527 
528     // RM-only
529     void                   *pProcessHandle;
530 
531     NvBool        (*fnFilter)(RsCpuMapping*);   ///< [in] Mapping-filter function
532     RS_LOCK_INFO   *pLockInfo;                  ///< [inout] Locking flags and state
533     API_SECURITY_INFO *pSecInfo;                ///< [in] Security Info
534 };
535 
536 /**
537  * CPU mapping back-reference
538  */
539 struct RS_CPU_MAPPING_BACK_REF
540 {
541     RsCpuMapping *pCpuMapping;  ///< Mapping linked to this backref
542     RsResourceRef *pBackRef;    ///< Resource reference with mapping
543 };
544 MAKE_LIST(RsCpuMappingBackRefList, RS_CPU_MAPPING_BACK_REF);
545 /* @} */
546 
547 /**
548  * @defgroup RsInterMapping
549  * @addtogroup RsInterMapping
550  * @{*/
551 struct RS_INTER_MAP_PARAMS
552 {
553     NvHandle        hClient;
554     NvHandle        hMapper;
555     NvHandle        hMappable;
556     NvHandle        hDevice;
557     NvU64           offset;
558     NvU64           length;
559     NvU32           flags;
560     NvU64           dmaOffset;              ///< [inout] RS-TODO rename this
561     void           *pMemDesc;               ///< [out]
562 
563     // Internal use only
564     RS_LOCK_INFO   *pLockInfo;              ///< [inout] Locking flags and state
565     API_SECURITY_INFO *pSecInfo;            ///< [in] Security Info
566 
567     RS_INTER_MAP_PRIVATE *pPrivate;         ///< Opaque struct controlled by caller
568 };
569 
570 struct RS_INTER_UNMAP_PARAMS
571 {
572     NvHandle        hClient;
573     NvHandle        hMapper;
574     NvHandle        hMappable;
575     NvHandle        hDevice;
576     NvU32           flags;
577     NvU64           dmaOffset;              ///< [in] RS-TODO rename this
578     void           *pMemDesc;               ///< MEMORY_DESCRIPTOR *
579 
580     // Internal use only
581     RS_LOCK_INFO   *pLockInfo;              ///< [inout] Locking flags and state
582     API_SECURITY_INFO *pSecInfo;            ///< [in] Security Info
583 
584     RS_INTER_UNMAP_PRIVATE *pPrivate;       ///< Opaque struct controlled by caller
585 };
586 
587 /**
588  * Inter-mapping information
589  * Used to keep track of inter-mappings and unmap them on free
590  */
591 struct RsInterMapping
592 {
593     // RsResourceRef *pMapperRef     ///< (Implied) the resource that created and owns this mapping (this resource)
594     RsResourceRef *pMappableRef;     ///< The resource being mapped by the mapper (e.g. hMemory)
595     RsResourceRef *pContextRef;      ///< A resource used to provide additional context for the mapping (e.g. hDevice)
596     NvU32 flags;                     ///< Flags passed when mapping, same flags also passed when unmapping
597     NvU64 dmaOffset;
598     void *pMemDesc;
599 };
600 MAKE_LIST(RsInterMappingList, RsInterMapping);
601 
602 /**
603  * Inter-mapping back-reference
604  */
605 struct RS_INTER_MAPPING_BACK_REF
606 {
607     RsResourceRef *pMapperRef;       ///< Resource reference with mapping
608     RsInterMapping *pMapping;        ///< Pointer to the inter-mapping linked to this backref
609 };
610 MAKE_LIST(RsInterMappingBackRefList, RS_INTER_MAPPING_BACK_REF);
611 /* @} */
612 
613 typedef struct RS_RESOURCE_DESC RS_RESOURCE_DESC;
614 RS_RESOURCE_DESC *RsResInfoByExternalClassId(NvU32);
615 NvU32 RsResInfoGetInternalClassId(const RS_RESOURCE_DESC *);
616 
617 /**
618  * A reference to a resource that has been allocated in RM.
619  */
620 struct RsResourceRef
621 {
622     struct RsClient   *pClient;            ///< Pointer to the client that owns the ref
623     struct RsResource *pResource;          ///< Pointer to the actual resource
624     NvHandle    hResource;          ///< Resource handle
625     struct RsResourceRef *pParentRef; ///< Parent resource reference
626     RsIndex     childRefMap;        ///< Child reference multi-map: { internalClassId -> { handle -> RsResourceRef } }
627 
628     /**
629      * Cached reference multi-map: { internalClassId -> { handle -> RsResourceRef } }
630      *
631      * The resource reference cache is a one-way association between this resource reference and
632      * any other resource reference. Resource server does not populate the cache so it is up to the
633      * resource implementation to manage it. clientRefIter can be used to iterate this cache.
634      */
635     RsIndex     cachedRefMap;
636 
637     /**
638      * Dependants reference multi-map: { internalClassId -> { handle -> RsResourceRef } }
639      *
640      * A map of all resources that strongly depend on this resource.
641      */
642     RsIndex     depRefMap;
643 
644     /**
645      * Dependants back-reference multi-map: { internalClassId -> { handle -> RsResourceRef } }
646      *
647      * AKA dependencies map
648      *
649      * A map of all resources that this resource strongly depends on.
650      */
651     RsIndex     depBackRefMap;
652 
653     /**
654      * Policy under which this resource can be shared with other clients
655      */
656     RsShareList     sharePolicyList;
657     NvBool          bSharePolicyListModified;
658 
659     /**
660      * A mask of the access rights that the owner client has on this object.
661      */
662     RS_ACCESS_MASK accessMask;
663 
664     const RS_RESOURCE_DESC *pResourceDesc; ///< Cached pointer to the resource descriptor
665     NvU32       internalClassId;    ///< Internal resource class id
666     NvU32       externalClassId;    ///< External resource class id
667     NvU32       depth;              ///< The depth of this reference in the resource graph
668     NvBool      bInvalidated;       ///< Reference has been freed but not removed yet
669 
670     RsCpuMappingList cpuMappings;   ///< List of CPU mappings to the resource from this resource reference
671     RsCpuMappingBackRefList backRefs;  ///< List of references that have this reference as a mapping context
672 
673     RsInterMappingList interMappings;        ///< List of inter-resource mappings created by this resource
674     RsInterMappingBackRefList interBackRefs; ///< List of inter-resource mappings this resource has been mapped into
675 
676     struct RsSession *pSession;          ///< If set, this ref depends on a shared session
677     struct RsSession *pDependantSession; ///< If set, this ref is depended on by a shared session
678 
679     ListNode   freeNode;        ///< Links to the client's pendingFreeList
680 };
681 MAKE_MAP(RsRefMap, RsResourceRef);
682 MAKE_INTRUSIVE_LIST(RsRefFreeList, RsResourceRef, freeNode);
683 
684 
685 // Iterator data structure to save state while walking through a list
686 struct RS_ITERATOR
687 {
688     union
689     {
690         RsRefMapIter      mapIt; ///< Map iterator for all resource references under a client
691         RsIndexIter idxIt; ///< Index iterator for child references of a resource reference
692     };
693 
694     struct RsClient *pClient;
695     RsResourceRef *pScopeRef;    ///< Reference to the resource that limits the scope of iteration
696     NvU32 internalClassId;
697     RsResourceRef *pResourceRef; ///< Resource ref that is being iterated over
698     NvU8 type;                   ///< RS_ITERATE_*
699     NvBool bExactMatch;          ///< If true, internalClassId must match exactly; if false, also match classes derived from the internalClassId
700 };
701 
702 // Iterator data structure to save state while walking through a resource tree in pre-order
703 struct RS_ORDERED_ITERATOR
704 {
705     NvS8 depth; ///< Depth of index stack; special value of -1 implies that the scope reference should be iterated over as well
706     RsIndexIter idxIt[RS_MAX_RESOURCE_DEPTH+1]; ///< Stack of index iterators for child references of a resource reference
707 
708     struct RsClient *pClient;
709     RsResourceRef *pScopeRef;    ///< Reference to the resource that limits the scope of iteration
710     NvU32 internalClassId;
711     NvBool bExactMatch;          ///< If true, internalClassId must match exactly; if false, also match classes derived from the internalClassId
712 
713     RsResourceRef *pResourceRef; ///< Resource ref that is being iterated over
714 };
715 
716 /**
717  * Macro for looking up a reference from a resource
718  */
719 #define RES_GET_REF(pResource) (staticCast((pResource), RsResource)->pResourceRef)
720 
721 /**
722  * Macro for looking up a resource handle from a resource
723  */
724 #define RES_GET_HANDLE(pResource) (RES_GET_REF(pResource)->hResource)
725 
726 /**
727  * Macro for looking up a resource's external class from a resource
728  */
729 #define RES_GET_EXT_CLASS_ID(pResource) (RES_GET_REF(pResource)->externalClassId)
730 
731 /**
732  * Macro for looking up a resource's parent handle from a resource
733  */
734 #define RES_GET_PARENT_HANDLE(pResource) (RES_GET_REF(pResource)->pParentRef->hResource)
735 
736 /**
737  * Macro for looking up a client from a resource
738  */
739 #define RES_GET_CLIENT(pResource) (RES_GET_REF(pResource)->pClient)
740 
741 /**
742  * Macro for looking up a client handle from a resource
743  */
744 #define RES_GET_CLIENT_HANDLE(pResource) (RES_GET_REF(pResource)->pClient->hClient)
745 
746 /**
747  * Find a CPU mapping owned by a resource reference
748  *
749  * @param[in]   pResourceRef
750  * @param[in]   pAddress The CPU virtual address of the mapping to search for
751  * @param[out]  ppMapping The returned mapping
752  */
753 NV_STATUS refFindCpuMapping(RsResourceRef *pResourceRef, NvP64 pAddress, RsCpuMapping **ppMapping);
754 
755 /**
756  * Find a CPU mapping owned by a resource reference
757  *
758  * @param[in]   pResourceRef
759  * @param[in]   pAddress The CPU virtual address of the mapping to search for
760  * @param[in]   fnFilter A user-provided filtering function that determines which mappings to ignore.
761  *                       If fnFilter is provided, then we will only return mappings for which fnFilter(mapping) returns NV_TRUE
762  *                       All mappings will be searched over if fnFilter is NULL.
763  * @param[out]  ppMapping The returned mapping
764  * @param[in]   fnFilter A user-provided filtering function that determines which mappings to ignore.
765  *                       If fnFilter is provided, then we will only return mappings for which fnFilter(mapping) returns NV_TRUE
766  *                       All mappings will be searched over if fnFilter is NULL.
767  */
768 NV_STATUS refFindCpuMappingWithFilter(RsResourceRef *pResourceRef, NvP64 pAddress, NvBool (*fnFilter)(RsCpuMapping*), RsCpuMapping **ppMapping);
769 
770 /**
771  * Find the first child object of given type
772  *
773  * @param[in]   pParentRef
774  * @param[in]   internalClassId
775  * @param[in]   bExactMatch If true, internalClassId must match exactly; if false, also match classes derived from the internalClassId
776  * @param[out]  pResourceRef The returned RsResourceRef (Optional)
777  */
778 NV_STATUS refFindChildOfType(RsResourceRef *pParentRef, NvU32 internalClassId, NvBool bExactMatch, RsResourceRef **ppResourceRef);
779 
780 /**
781  * Traverse up the reference parent-child hierarchy to find an ancestor reference of a given type
782  *
783  * @param[in]   pDescendantRef
784  * @param[in]   internalClassId
785  * @param[out]  ppAncestorRef The returned RsResourceRef (Optional)
786  */
787 NV_STATUS refFindAncestorOfType(RsResourceRef *pDescendantRef, NvU32 internalClassId, RsResourceRef **ppAncestorRef);
788 
789 /**
790  * Traverse up the reference parent-child hierarchy to find if a ref is a descendant of a given ancestor ref
791  *
792  * @param[in]  pDescendantRef The node to start searching from (not included in the search)
793  * @param[in]  pAncestorRef The node to search for in the parent-child hierarchy
794  */
795 NvBool refHasAncestor(RsResourceRef *pDescendantRef, RsResourceRef *pAncestorRef);
796 
797 /**
798  * Add a new mapping to a reference's mapping list
799  * @param[in] pResourceRef The reference to add a mapping to
800  * @param[in] pMapParams The parameters used to initialize the mapping
801  * @param[in] pContextRef A reference to a resource that provides a context for the mapping
802  * @param[out] ppMapping Pointer to the allocated mapping [optional]
803  */
804 NV_STATUS refAddMapping(RsResourceRef *pResourceRef, RS_CPU_MAP_PARAMS *pMapParams,
805                         RsResourceRef *pContextRef, RsCpuMapping **ppMapping);
806 
807 /**
808  * Remove an existing mapping from a reference's mapping list and remove back-references to the mapping.
809  * @param[in] pResourceRef The reference to add a mapping to
810  * @param[in] pMapping Pointer to the allocated mapping
811  */
812 void refRemoveMapping(RsResourceRef *pResourceRef, RsCpuMapping *pMapping);
813 
814 /**
815  * Allocate the user-controlled private pointer within the RsCpuMapping struct.
816  * Resserv will call this function to alloc the private struct when the mapping is created
817  * @param[in] pMapParams The parameters which were used to create the mapping
818  * @param[inout] pMapping Pointer to the mapping whose private struct should be allocated
819  */
820 NV_STATUS refAllocCpuMappingPrivate(RS_CPU_MAP_PARAMS *pMapParams, RsCpuMapping *pMapping);
821 
822 /**
823  * Free the user-controlled private pointer within the RsCpuMapping struct.
824  * Resserv will call this function to free the private struct when the mapping is removed
825  * @param[inout] pMapping Pointer to the mapping whose private struct should be freed
826  */
827 void refFreeCpuMappingPrivate(RsCpuMapping *pMapping);
828 
829 /**
830  * Add a dependency between this resource reference and a dependent reference.
831  * If this reference is freed, the dependent will be invalidated and torn down.
832  *
833  * @note Dependencies are implicit between a parent resource reference and child resource reference
834  * @note No circular dependency checking is performed
835  */
836 NV_STATUS refAddDependant(RsResourceRef *pResourceRef, RsResourceRef *pDependantRef);
837 
838 /**
839  * Remove the dependency between this resource reference and a dependent resource reference.
840  */
841 NV_STATUS refRemoveDependant(RsResourceRef *pResourceRef, RsResourceRef *pDependantRef);
842 
843 /**
844  * Find, Add, or Remove an inter-mapping between two resources to the Mapper's list of inter-mappings
845  * Inter-mappings are stored in the Mapper, and are matched by both the MappableRef and offset.
846  *
847  * @param[in] pMapperRef The reference which owns the inter-mapping
848  * @param[in] pMappableRef The reference which was mapped from to create the inter-mapping
849  *                         If NULL, will be ignored while matching inter-mappings
850  * @param[in] dmaOffset The offset value assigned while mapping, used to identify mappings
851  * @param[in] pContextRef A reference used during mapping and locking for additional context, used to identify mappings
852  * @param[inout] ppMapping Writes the resulting inter-mapping, if successfully created (Add) or found (Find)
853  * @param[in] pMapping The inter-mapping to remove (Remove)
854  */
855 NV_STATUS refFindInterMapping(RsResourceRef *pMapperRef, RsResourceRef *pMappableRef, RsResourceRef *pContextRef, NvU64 dmaOffset, RsInterMapping **ppMapping);
856 NV_STATUS refAddInterMapping(RsResourceRef *pMapperRef, RsResourceRef *pMappableRef, RsResourceRef *pContextRef, RsInterMapping **ppMapping);
857 void      refRemoveInterMapping(RsResourceRef *pMapperRef, RsInterMapping *pMapping);
858 
859 /**
860  * Store a resource reference in another reference's cache.
861  * @param[in]   pParentRef The resource reference that owns the cache
862  * @param[in]   pResourceRef The resource reference to store in the cache
863  */
864 NV_STATUS refCacheRef(RsResourceRef *pParentRef, RsResourceRef *pResourceRef);
865 
866 /**
867  * Remove a resource reference from another reference's cache
868  * @param[in]   pParentRef The resource reference that owns the cache
869  * @param[in]   pResourceRef The resource reference to de-index
870  */
871 NV_STATUS refUncacheRef(RsResourceRef *pParentRef, RsResourceRef *pResourceRef);
872 
873 /**
874  * Determine whether a reference is queued for removal
875  * @param[in]   pResourceRef
876  * @param[in]   pClient
877  */
878 NvBool    refPendingFree(RsResourceRef *pResourceRef, struct RsClient *pClient);
879 
880 
881 #ifdef __cplusplus
882 }
883 #endif
884 
885 #endif
886 
887 #ifdef __cplusplus
888 } // extern "C"
889 #endif
890 #endif // _G_RS_RESOURCE_NVOC_H_
891