1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2021 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 #pragma once
24 
25 #include <nvtypes.h>
26 
27 //
28 // This file was generated with FINN, an NVIDIA coding tool.
29 // Source file:      rs_access.finn
30 //
31 
32 
33 
34 
35 #include "nvtypes.h"
36 #include "nvmisc.h"
37 
38 
39 /****************************************************************************/
40 /*                       Access right definitions                           */
41 /****************************************************************************/
42 
43 //
44 // The meaning of each access right is documented in
45 //   resman/docs/rmapi/resource_server/rm_capabilities.adoc
46 //
47 // RS_ACCESS_COUNT is the number of access rights that have been defined
48 // and are in use. All integers in the range [0, RS_ACCESS_COUNT) should
49 // represent valid access rights.
50 //
51 // When adding a new access right, don't forget to update
52 //   1) The descriptions in the resman/docs/rmapi/resource_server/rm_capabilities.adoc
53 //   2) RS_ACCESS_COUNT, defined below
54 //   3) The declaration of g_rsAccessMetadata in rs_access_rights.c
55 //   4) The list of access rights in drivers/common/chip-config/Chipcontrols.pm
56 //   5) Any relevant access right callbacks
57 //
58 
59 #define RS_ACCESS_DUP_OBJECT 0U
60 #define RS_ACCESS_NICE       1U
61 #define RS_ACCESS_DEBUG      2U
62 #define RS_ACCESS_COUNT      3U
63 
64 
65 /****************************************************************************/
66 /*                     Access right data structures                         */
67 /****************************************************************************/
68 
69 /*!
70  * @brief A type that can be used to represent any access right.
71  */
72 typedef NvU16 RsAccessRight;
73 
74 /*!
75  * @brief An internal type used to represent one limb in an access right mask.
76  */
77 typedef NvU32 RsAccessLimb;
78 #define SDK_RS_ACCESS_LIMB_BITS 32
79 
80 /*!
81  * @brief The number of limbs in the RS_ACCESS_MASK struct.
82  */
83 #define SDK_RS_ACCESS_MAX_LIMBS 1
84 
85 /*!
86  * @brief The maximum number of possible access rights supported by the
87  *        current data structure definition.
88  *
89  * You probably want RS_ACCESS_COUNT instead, which is the number of actual
90  * access rights defined.
91  */
92 #define SDK_RS_ACCESS_MAX_COUNT (0x20) /* finn: Evaluated from "(SDK_RS_ACCESS_LIMB_BITS * SDK_RS_ACCESS_MAX_LIMBS)" */
93 
94 /**
95  * @brief A struct representing a set of access rights.
96  *
97  * Note that the values of bit positions larger than RS_ACCESS_COUNT is
98  * undefined, and should not be assumed to be 0 (see RS_ACCESS_MASK_FILL).
99  */
100 typedef struct RS_ACCESS_MASK {
101     RsAccessLimb limbs[SDK_RS_ACCESS_MAX_LIMBS];
102 } RS_ACCESS_MASK;
103 
104 /**
105  * @brief A struct representing auxiliary information about each access right.
106  */
107 typedef struct RS_ACCESS_INFO {
108     NvU32 flags;
109 } RS_ACCESS_INFO;
110 
111 
112 /****************************************************************************/
113 /*                           Access right macros                            */
114 /****************************************************************************/
115 
116 #define SDK_RS_ACCESS_LIMB_INDEX(index) ((index) / SDK_RS_ACCESS_LIMB_BITS)
117 #define SDK_RS_ACCESS_LIMB_POS(index)   ((index) % SDK_RS_ACCESS_LIMB_BITS)
118 
119 #define SDK_RS_ACCESS_LIMB_ELT(pAccessMask, index) \
120     ((pAccessMask)->limbs[SDK_RS_ACCESS_LIMB_INDEX(index)])
121 #define SDK_RS_ACCESS_OFFSET_MASK(index) \
122     NVBIT_TYPE(SDK_RS_ACCESS_LIMB_POS(index), RsAccessLimb)
123 
124 /*!
125  * @brief Checks that accessRight represents a valid access right.
126  *
127  * The valid range of access rights is [0, RS_ACCESS_COUNT).
128  *
129  * @param[in] accessRight The access right value to check
130  *
131  * @return true if accessRight is valid
132  * @return false otherwise
133  */
134 #define RS_ACCESS_BOUNDS_CHECK(accessRight) \
135     (accessRight < RS_ACCESS_COUNT)
136 
137 /*!
138  * @brief Test whether an access right is present in a set
139  *
140  * @param[in] pAccessMask The set of access rights to read
141  * @param[in] index The access right to examine
142  *
143  * @return NV_TRUE if the access right specified by index was present in the set,
144  *         and NV_FALSE otherwise
145  */
146 #define RS_ACCESS_MASK_TEST(pAccessMask, index) \
147     (RS_ACCESS_BOUNDS_CHECK(index) && \
148         (SDK_RS_ACCESS_LIMB_ELT(pAccessMask, index) & SDK_RS_ACCESS_OFFSET_MASK(index)) != 0)
149 
150 /*!
151  * @brief Add an access right to a mask
152  *
153  * @param[in] pAccessMask The set of access rights to modify
154  * @param[in] index The access right to set
155  */
156 #define RS_ACCESS_MASK_ADD(pAccessMask, index) \
157     do \
158     { \
159         if (RS_ACCESS_BOUNDS_CHECK(index)) { \
160             SDK_RS_ACCESS_LIMB_ELT(pAccessMask, index) |= SDK_RS_ACCESS_OFFSET_MASK(index); \
161         } \
162     } while (NV_FALSE)
163 
164 /*!
165  * @brief Remove an access right from a mask
166  *
167  * @param[in] pAccessMask The set of access rights to modify
168  * @param[in] index The access right to unset
169  */
170 #define RS_ACCESS_MASK_REMOVE(pAccessMask, index) \
171     do \
172     { \
173         if (RS_ACCESS_BOUNDS_CHECK(index)) { \
174             SDK_RS_ACCESS_LIMB_ELT(pAccessMask, index) &= ~SDK_RS_ACCESS_OFFSET_MASK(index); \
175         } \
176     } while (NV_FALSE)
177 
178 /*!
179  * @brief Performs an in-place union between two access right masks
180  *
181  * @param[in,out] pMaskOut The access rights mask to be updated
182  * @param[in] pMaskIn The set of access rights to be added to pMaskOut
183  */
184 #define RS_ACCESS_MASK_UNION(pMaskOut, pMaskIn) \
185     do \
186     { \
187         NvLength limb; \
188         for (limb = 0; limb < SDK_RS_ACCESS_MAX_LIMBS; limb++) \
189         { \
190             SDK_RS_ACCESS_LIMB_ELT(pMaskOut, limb) |= SDK_RS_ACCESS_LIMB_ELT(pMaskIn, limb); \
191         } \
192     } while (NV_FALSE)
193 
194 /*!
195  * @brief Performs an in-place subtract of one mask's rights from another
196  *
197  * @param[in,out] pMaskOut The access rights mask to be updated
198  * @param[in] pMaskIn The set of access rights to be removed from pMaskOut
199  */
200 #define RS_ACCESS_MASK_SUBTRACT(pMaskOut, pMaskIn) \
201     do \
202     { \
203         NvLength limb; \
204         for (limb = 0; limb < SDK_RS_ACCESS_MAX_LIMBS; limb++) \
205         { \
206             SDK_RS_ACCESS_LIMB_ELT(pMaskOut, limb) &= ~SDK_RS_ACCESS_LIMB_ELT(pMaskIn, limb); \
207         } \
208     } while (NV_FALSE)
209 
210 /*!
211  * @brief Removes all rights from an access rights mask
212  *
213  * @param[in,out] pAccessMask The access rights mask to be updated
214  */
215 #define RS_ACCESS_MASK_CLEAR(pAccessMask) \
216     do \
217     { \
218         portMemSet(pAccessMask, 0, sizeof(*pAccessMask)); \
219     } while (NV_FALSE)
220 
221 /*!
222  * @brief Adds all rights to an access rights mask
223  *
224  * @param[in,out] pAccessMask The access rights mask to be updated
225  */
226 #define RS_ACCESS_MASK_FILL(pAccessMask) \
227     do \
228     { \
229         portMemSet(pAccessMask, 0xff, sizeof(*pAccessMask)); \
230     } while (NV_FALSE)
231 
232 
233 /****************************************************************************/
234 /*                           Share definitions                              */
235 /****************************************************************************/
236 
237 //
238 // The usage of Share Policy and the meaning of each share type is documented in
239 //   resman/docs/rmapi/resource_server/rm_capabilities.adoc
240 //
241 #define RS_SHARE_TYPE_NONE              (0U)
242 #define RS_SHARE_TYPE_ALL               (1U)
243 #define RS_SHARE_TYPE_OS_SECURITY_TOKEN (2U)
244 #define RS_SHARE_TYPE_CLIENT            (3U)
245 #define RS_SHARE_TYPE_PID               (4U)
246 #define RS_SHARE_TYPE_SMC_PARTITION     (5U)
247 #define RS_SHARE_TYPE_GPU               (6U)
248 #define RS_SHARE_TYPE_FM_CLIENT         (7U)
249 // Must be last. Update when a new SHARE_TYPE is added
250 #define RS_SHARE_TYPE_MAX               (8U)
251 
252 
253 //
254 // Use Revoke to remove an existing policy from the list.
255 // Allow is based on OR logic, Require is based on AND logic.
256 // To share a right, at least one Allow (non-Require) must match, and all Require must pass.
257 // If Compose is specified, policies will be added to the list. Otherwise, they will replace the list.
258 //
259 #define RS_SHARE_ACTION_FLAG_REVOKE      NVBIT(0)
260 #define RS_SHARE_ACTION_FLAG_REQUIRE     NVBIT(1)
261 #define RS_SHARE_ACTION_FLAG_COMPOSE     NVBIT(2)
262 
263 /****************************************************************************/
264 /*                       Share flag data structures                         */
265 /****************************************************************************/
266 
267 typedef struct RS_SHARE_POLICY {
268     NvU32          target;
269     RS_ACCESS_MASK accessMask;
270     NvU16          type;                         ///< RS_SHARE_TYPE_
271     NvU8           action;                        ///< RS_SHARE_ACTION_
272 } RS_SHARE_POLICY;
273