interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / third_party / nvidia-open-580.173.02 / src / common / sdk / nvidia / inc / nvmisc.h

1000 lines · 42372 bytes Raw Blame History
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 1993-2024 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/*
25 * nvmisc.h
26 */
27#ifndef __NV_MISC_H
28#define __NV_MISC_H
29
30#ifdef __cplusplus
31extern "C" {
32#endif //__cplusplus
33
34#include "nvtypes.h"
35
36// Miscellaneous macros useful for bit field manipulations.
37#ifndef NVBIT
38#define NVBIT(b) (1U<<(b))
39#endif
40#ifndef NVBIT_TYPE
41#define NVBIT_TYPE(b, t) (((t)1U)<<(b))
42#endif
43#ifndef NVBIT32
44#define NVBIT32(b) NVBIT_TYPE(b, NvU32)
45#endif
46#ifndef NVBIT64
47#define NVBIT64(b) NVBIT_TYPE(b, NvU64)
48#endif
49
50//Concatenate 2 32bit values to a 64bit value
51#define NV_CONCAT_32_TO_64(hi, lo) ((((NvU64)hi) << 32) | ((NvU64)lo))
52
53// Helper macro's for 32 bit bitmasks
54#define NV_BITMASK32_ELEMENT_SIZE (sizeof(NvU32) << 3)
55#define NV_BITMASK32_IDX(chId) (((chId) & ~(0x1F)) >> 5)
56#define NV_BITMASK32_OFFSET(chId) ((chId) & (0x1F))
57#define NV_BITMASK32_SET(pChannelMask, chId) \
58 (pChannelMask)[NV_BITMASK32_IDX(chId)] |= NVBIT(NV_BITMASK32_OFFSET(chId))
59#define NV_BITMASK32_GET(pChannelMask, chId) \
60 ((pChannelMask)[NV_BITMASK32_IDX(chId)] & NVBIT(NV_BITMASK32_OFFSET(chId)))
61
62
63// Index of the 'on' bit (assuming that there is only one).
64// Even if multiple bits are 'on', result is in range of 0-31.
65#define BIT_IDX_32(n) \
66 (((((n) & 0xFFFF0000U) != 0U) ? 0x10U: 0U) | \
67 ((((n) & 0xFF00FF00U) != 0U) ? 0x08U: 0U) | \
68 ((((n) & 0xF0F0F0F0U) != 0U) ? 0x04U: 0U) | \
69 ((((n) & 0xCCCCCCCCU) != 0U) ? 0x02U: 0U) | \
70 ((((n) & 0xAAAAAAAAU) != 0U) ? 0x01U: 0U) )
71
72// Index of the 'on' bit (assuming that there is only one).
73// Even if multiple bits are 'on', result is in range of 0-63.
74#define BIT_IDX_64(n) \
75 (((((n) & 0xFFFFFFFF00000000ULL) != 0U) ? 0x20U: 0U) | \
76 ((((n) & 0xFFFF0000FFFF0000ULL) != 0U) ? 0x10U: 0U) | \
77 ((((n) & 0xFF00FF00FF00FF00ULL) != 0U) ? 0x08U: 0U) | \
78 ((((n) & 0xF0F0F0F0F0F0F0F0ULL) != 0U) ? 0x04U: 0U) | \
79 ((((n) & 0xCCCCCCCCCCCCCCCCULL) != 0U) ? 0x02U: 0U) | \
80 ((((n) & 0xAAAAAAAAAAAAAAAAULL) != 0U) ? 0x01U: 0U) )
81
82/*!
83 * DRF MACRO README:
84 *
85 * Glossary:
86 * DRF: Device, Register, Field
87 * FLD: Field
88 * REF: Reference
89 *
90 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA 0xDEADBEEF
91 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_GAMMA 27:0
92 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA 31:28
93 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_ZERO 0x00000000
94 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_ONE 0x00000001
95 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_TWO 0x00000002
96 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_THREE 0x00000003
97 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_FOUR 0x00000004
98 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_FIVE 0x00000005
99 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_SIX 0x00000006
100 * #define NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA_SEVEN 0x00000007
101 *
102 *
103 * Device = _DEVICE_OMEGA
104 * This is the common "base" that a group of registers in a manual share
105 *
106 * Register = _REGISTER_ALPHA
107 * Register for a given block of defines is the common root for one or more fields and constants
108 *
109 * Field(s) = _FIELD_GAMMA, _FIELD_ZETA
110 * These are the bit ranges for a given field within the register
111 * Fields are not required to have defined constant values (enumerations)
112 *
113 * Constant(s) = _ZERO, _ONE, _TWO, ...
114 * These are named values (enums) a field can contain; the width of the constants should not be larger than the field width
115 *
116 * MACROS:
117 *
118 * DRF_SHIFT:
119 * Bit index of the lower bound of a field
120 * DRF_SHIFT(NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA) == 28
121 *
122 * DRF_SHIFT_RT:
123 * Bit index of the higher bound of a field
124 * DRF_SHIFT_RT(NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA) == 31
125 *
126 * DRF_MASK:
127 * Produces a mask of 1-s equal to the width of a field
128 * DRF_MASK(NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA) == 0xF (four 1s starting at bit 0)
129 *
130 * DRF_SHIFTMASK:
131 * Produces a mask of 1s equal to the width of a field at the location of the field
132 * DRF_SHIFTMASK(NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA) == 0xF0000000
133 *
134 * DRF_DEF:
135 * Shifts a field constant's value to the correct field offset
136 * DRF_DEF(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, _THREE) == 0x30000000
137 *
138 * DRF_NUM:
139 * Shifts a number to the location of a particular field
140 * DRF_NUM(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, 3) == 0x30000000
141 * NOTE: If the value passed in is wider than the field, the value's high bits will be truncated
142 *
143 * DRF_SIZE:
144 * Provides the width of the field in bits
145 * DRF_SIZE(NV_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA) == 4
146 *
147 * DRF_VAL:
148 * Provides the value of an input within the field specified
149 * DRF_VAL(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, 0xABCD1234) == 0xA
150 * This is sort of like the inverse of DRF_NUM
151 *
152 * DRF_IDX...:
153 * These macros are similar to the above but for fields that accept an index argumment
154 *
155 * FLD_SET_DRF:
156 * Set the field bits in a given value with the given field constant
157 * NvU32 x = 0x00001234;
158 * x = FLD_SET_DRF(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, _THREE, x);
159 * x == 0x30001234;
160 *
161 * FLD_SET_DRF_NUM:
162 * Same as FLD_SET_DRF but instead of using a field constant a literal/variable is passed in
163 * NvU32 x = 0x00001234;
164 * x = FLD_SET_DRF_NUM(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, 0xF, x);
165 * x == 0xF0001234;
166 *
167 * FLD_IDX...:
168 * These macros are similar to the above but for fields that accept an index argumment
169 *
170 * FLD_TEST_DRF:
171 * Test if location specified by drf in 'v' has the same value as NV_drfc
172 * FLD_TEST_DRF(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, _THREE, 0x3000ABCD) == NV_TRUE
173 *
174 * FLD_TEST_DRF_NUM:
175 * Test if locations specified by drf in 'v' have the same value as n
176 * FLD_TEST_DRF_NUM(_DEVICE_OMEGA, _REGISTER_ALPHA, _FIELD_ZETA, 0x3, 0x3000ABCD) == NV_TRUE
177 *
178 * REF_DEF:
179 * Like DRF_DEF but maintains full symbol name (use in cases where "NV" is not prefixed to the field)
180 * REF_DEF(SOME_OTHER_PREFIX_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA, _THREE) == 0x30000000
181 *
182 * REF_VAL:
183 * Like DRF_VAL but maintains full symbol name (use in cases where "NV" is not prefixed to the field)
184 * REF_VAL(SOME_OTHER_PREFIX_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA, 0xABCD1234) == 0xA
185 *
186 * REF_NUM:
187 * Like DRF_NUM but maintains full symbol name (use in cases where "NV" is not prefixed to the field)
188 * REF_NUM(SOME_OTHER_PREFIX_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA, 0xA) == 0xA00000000
189 *
190 * FLD_SET_REF_NUM:
191 * Like FLD_SET_DRF_NUM but maintains full symbol name (use in cases where "NV" is not prefixed to the field)
192 * NvU32 x = 0x00001234;
193 * x = FLD_SET_REF_NUM(SOME_OTHER_PREFIX_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA, 0xF, x);
194 * x == 0xF0001234;
195 *
196 * FLD_TEST_REF:
197 * Like FLD_TEST_DRF but maintains full symbol name (use in cases where "NV" is not prefixed to the field)
198 * FLD_TEST_REF(SOME_OTHER_PREFIX_DEVICE_OMEGA_REGISTER_ALPHA_FIELD_ZETA, _THREE, 0x3000ABCD) == NV_TRUE
199 *
200 * Other macros:
201 * There a plethora of other macros below that extend the above (notably Multi-Word (MW), 64-bit, and some
202 * reg read/write variations). I hope these are self explanatory. If you have a need to use them, you
203 * probably have some knowledge of how they work.
204 */
205
206// tegra mobile uses nvmisc_macros.h and can't access nvmisc.h... and sometimes both get included.
207#ifndef _NVMISC_MACROS_H
208// Use Coverity Annotation to mark issues as false positives/ignore when using single bit defines.
209#define DRF_ISBIT(bitval,drf) \
210 ( /* coverity[identical_branches] */ \
211 (bitval != 0) ? drf )
212#define DEVICE_BASE(d) (0?d) // what's up with this name? totally non-parallel to the macros below
213#define DEVICE_EXTENT(d) (1?d) // what's up with this name? totally non-parallel to the macros below
214#ifdef NV_MISRA_COMPLIANCE_REQUIRED
215#ifdef MISRA_14_3
216#define DRF_BASE(drf) (drf##_LOW_FIELD)
217#define DRF_EXTENT(drf) (drf##_HIGH_FIELD)
218#define DRF_SHIFT(drf) ((drf##_LOW_FIELD) % 32U)
219#define DRF_SHIFT_RT(drf) ((drf##_HIGH_FIELD) % 32U)
220#define DRF_SIZE(drf) ((drf##_HIGH_FIELD)-(drf##_LOW_FIELD)+1U)
221#define DRF_MASK(drf) (0xFFFFFFFFU >> (31U - ((drf##_HIGH_FIELD) % 32U) + ((drf##_LOW_FIELD) % 32U)))
222#else
223#define DRF_BASE(drf) (NV_FALSE?drf) // much better
224#define DRF_EXTENT(drf) (NV_TRUE?drf) // much better
225#define DRF_SHIFT(drf) (((NvU32)DRF_BASE(drf)) % 32U)
226#define DRF_SHIFT_RT(drf) (((NvU32)DRF_EXTENT(drf)) % 32U)
227#define DRF_SIZE(drf) (DRF_EXTENT(drf)-DRF_BASE(drf)+1U)
228#define DRF_MASK(drf) (0xFFFFFFFFU>>(31U - DRF_SHIFT_RT(drf) + DRF_SHIFT(drf)))
229#endif
230#define DRF_DEF(d,r,f,c) (((NvU32)(NV ## d ## r ## f ## c))<<DRF_SHIFT(NV ## d ## r ## f))
231#define DRF_NUM(d,r,f,n) ((((NvU32)(n))&DRF_MASK(NV ## d ## r ## f))<<DRF_SHIFT(NV ## d ## r ## f))
232#else
233#define DRF_BASE(drf) (0?drf) // much better
234#define DRF_EXTENT(drf) (1?drf) // much better
235#define DRF_SHIFT(drf) ((DRF_ISBIT(0,drf)) % 32)
236#define DRF_SHIFT_RT(drf) ((DRF_ISBIT(1,drf)) % 32)
237#define DRF_SIZE(drf) (DRF_EXTENT(drf)-DRF_BASE(drf)+1U)
238#define DRF_MASK(drf) (0xFFFFFFFFU>>(31-((DRF_ISBIT(1,drf)) % 32)+((DRF_ISBIT(0,drf)) % 32)))
239#define DRF_DEF(d,r,f,c) ((NV ## d ## r ## f ## c)<<DRF_SHIFT(NV ## d ## r ## f))
240#define DRF_NUM(d,r,f,n) (((n)&DRF_MASK(NV ## d ## r ## f))<<DRF_SHIFT(NV ## d ## r ## f))
241#endif
242#define DRF_SHIFTMASK(drf) (DRF_MASK(drf)<<(DRF_SHIFT(drf)))
243
244#define DRF_VAL(d,r,f,v) (((v)>>DRF_SHIFT(NV ## d ## r ## f))&DRF_MASK(NV ## d ## r ## f))
245#endif
246
247// Signed version of DRF_VAL, which takes care of extending sign bit.
248#define DRF_VAL_SIGNED(d,r,f,v) (((DRF_VAL(d,r,f,(v)) ^ (NVBIT(DRF_SIZE(NV ## d ## r ## f)-1U)))) - (NVBIT(DRF_SIZE(NV ## d ## r ## f)-1U)))
249#define DRF_IDX_DEF(d,r,f,i,c) ((NV ## d ## r ## f ## c)<<DRF_SHIFT(NV##d##r##f(i)))
250#define DRF_IDX_OFFSET_DEF(d,r,f,i,o,c) ((NV ## d ## r ## f ## c)<<DRF_SHIFT(NV##d##r##f(i,o)))
251#define DRF_IDX_NUM(d,r,f,i,n) (((n)&DRF_MASK(NV##d##r##f(i)))<<DRF_SHIFT(NV##d##r##f(i)))
252#define DRF_IDX_VAL(d,r,f,i,v) (((v)>>DRF_SHIFT(NV##d##r##f(i)))&DRF_MASK(NV##d##r##f(i)))
253#define DRF_IDX_OFFSET_VAL(d,r,f,i,o,v) (((v)>>DRF_SHIFT(NV##d##r##f(i,o)))&DRF_MASK(NV##d##r##f(i,o)))
254// Fractional version of DRF_VAL which reads Fx.y fixed point number (x.y)*z
255#define DRF_VAL_FRAC(d,r,x,y,v,z) ((DRF_VAL(d,r,x,(v))*z) + ((DRF_VAL(d,r,y,v)*z) / (1<<DRF_SIZE(NV##d##r##y))))
256
257//
258// 64 Bit Versions
259//
260#define DRF_SHIFT64(drf) ((DRF_ISBIT(0,drf)) % 64)
261#define DRF_MASK64(drf) (NV_U64_MAX>>(63-((DRF_ISBIT(1,drf)) % 64)+((DRF_ISBIT(0,drf)) % 64)))
262#define DRF_SHIFTMASK64(drf) (DRF_MASK64(drf)<<(DRF_SHIFT64(drf)))
263
264#define DRF_DEF64(d,r,f,c) (((NvU64)(NV ## d ## r ## f ## c))<<DRF_SHIFT64(NV ## d ## r ## f))
265#define DRF_NUM64(d,r,f,n) ((((NvU64)(n))&DRF_MASK64(NV ## d ## r ## f))<<DRF_SHIFT64(NV ## d ## r ## f))
266#define DRF_VAL64(d,r,f,v) ((((NvU64)(v))>>DRF_SHIFT64(NV ## d ## r ## f))&DRF_MASK64(NV ## d ## r ## f))
267
268#define DRF_VAL_SIGNED64(d,r,f,v) (((DRF_VAL64(d,r,f,(v)) ^ (NVBIT64(DRF_SIZE(NV ## d ## r ## f)-1)))) - (NVBIT64(DRF_SIZE(NV ## d ## r ## f)-1)))
269#define DRF_IDX_DEF64(d,r,f,i,c) (((NvU64)(NV ## d ## r ## f ## c))<<DRF_SHIFT64(NV##d##r##f(i)))
270#define DRF_IDX_OFFSET_DEF64(d,r,f,i,o,c) ((NvU64)(NV ## d ## r ## f ## c)<<DRF_SHIFT64(NV##d##r##f(i,o)))
271#define DRF_IDX_NUM64(d,r,f,i,n) ((((NvU64)(n))&DRF_MASK64(NV##d##r##f(i)))<<DRF_SHIFT64(NV##d##r##f(i)))
272#define DRF_IDX_VAL64(d,r,f,i,v) ((((NvU64)(v))>>DRF_SHIFT64(NV##d##r##f(i)))&DRF_MASK64(NV##d##r##f(i)))
273#define DRF_IDX_OFFSET_VAL64(d,r,f,i,o,v) (((NvU64)(v)>>DRF_SHIFT64(NV##d##r##f(i,o)))&DRF_MASK64(NV##d##r##f(i,o)))
274
275#define FLD_SET_DRF64(d,r,f,c,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f)) | DRF_DEF64(d,r,f,c))
276#define FLD_SET_DRF_NUM64(d,r,f,n,v) ((((NvU64)(v)) & ~DRF_SHIFTMASK64(NV##d##r##f)) | DRF_NUM64(d,r,f,n))
277#define FLD_IDX_SET_DRF64(d,r,f,i,c,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f(i))) | DRF_IDX_DEF64(d,r,f,i,c))
278#define FLD_IDX_OFFSET_SET_DRF64(d,r,f,i,o,c,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f(i,o))) | DRF_IDX_OFFSET_DEF64(d,r,f,i,o,c))
279#define FLD_IDX_SET_DRF_DEF64(d,r,f,i,c,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f(i))) | DRF_IDX_DEF64(d,r,f,i,c))
280#define FLD_IDX_SET_DRF_NUM64(d,r,f,i,n,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f(i))) | DRF_IDX_NUM64(d,r,f,i,n))
281#define FLD_SET_DRF_IDX64(d,r,f,c,i,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(NV##d##r##f)) | DRF_DEF64(d,r,f,c(i)))
282
283#define FLD_TEST_DRF64(d,r,f,c,v) (DRF_VAL64(d, r, f, (v)) == NV##d##r##f##c)
284#define FLD_TEST_DRF_AND64(d,r,f,c,v) (DRF_VAL64(d, r, f, (v)) & NV##d##r##f##c)
285#define FLD_TEST_DRF_NUM64(d,r,f,n,v) (DRF_VAL64(d, r, f, (v)) == (n))
286#define FLD_IDX_TEST_DRF64(d,r,f,i,c,v) (DRF_IDX_VAL64(d, r, f, i, (v)) == NV##d##r##f##c)
287#define FLD_IDX_OFFSET_TEST_DRF64(d,r,f,i,o,c,v) (DRF_IDX_OFFSET_VAL64(d, r, f, i, o, (v)) == NV##d##r##f##c)
288
289#define REF_DEF64(drf,d) (((drf ## d)&DRF_MASK64(drf))<<DRF_SHIFT64(drf))
290#define REF_VAL64(drf,v) (((NvU64)(v)>>DRF_SHIFT64(drf))&DRF_MASK64(drf))
291#if defined(NV_MISRA_COMPLIANCE_REQUIRED) && defined(MISRA_14_3)
292#define REF_NUM64(drf,n) (((NvU64)(n)&(0xFFFFFFFFFFFFFFFFU>>(63U-((drf##_HIGH_FIELD) % 63U)+((drf##_LOW_FIELD) % 63U)))) << ((drf##_LOW_FIELD) % 63U))
293#else
294#define REF_NUM64(drf,n) (((NvU64)(n)&DRF_MASK64(drf))<<DRF_SHIFT64(drf))
295#endif
296#define FLD_TEST_REF64(drf,c,v) (REF_VAL64(drf, v) == drf##c)
297#define FLD_TEST_REF_AND64(drf,c,v) (REF_VAL64(drf, v) & drf##c)
298#define FLD_SET_REF_NUM64(drf,n,v) (((NvU64)(v) & ~DRF_SHIFTMASK64(drf)) | REF_NUM64(drf,n))
299
300//
301// 32 Bit Versions
302//
303
304#ifdef NV_MISRA_COMPLIANCE_REQUIRED
305#define FLD_SET_DRF(d,r,f,c,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_DEF(d,r,f,c))
306#define FLD_SET_DRF_NUM(d,r,f,n,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_NUM(d,r,f,n))
307#define FLD_IDX_SET_DRF(d,r,f,i,c,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_DEF(d,r,f,i,c))
308#define FLD_IDX_OFFSET_SET_DRF(d,r,f,i,o,c,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f(i,o))) | DRF_IDX_OFFSET_DEF(d,r,f,i,o,c))
309#define FLD_IDX_SET_DRF_DEF(d,r,f,i,c,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_DEF(d,r,f,i,c))
310#define FLD_IDX_SET_DRF_NUM(d,r,f,i,n,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_NUM(d,r,f,i,n))
311#define FLD_SET_DRF_IDX(d,r,f,c,i,v) (((NvU32)(v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_DEF(d,r,f,c(i)))
312
313#define FLD_TEST_DRF(d,r,f,c,v) ((DRF_VAL(d, r, f, (v)) == (NvU32)(NV##d##r##f##c)))
314#define FLD_TEST_DRF_AND(d,r,f,c,v) ((DRF_VAL(d, r, f, (v)) & (NvU32)(NV##d##r##f##c)) != 0U)
315#define FLD_TEST_DRF_NUM(d,r,f,n,v) ((DRF_VAL(d, r, f, (v)) == (NvU32)(n)))
316#define FLD_IDX_TEST_DRF(d,r,f,i,c,v) ((DRF_IDX_VAL(d, r, f, i, (v)) == (NvU32)(NV##d##r##f##c)))
317#define FLD_IDX_OFFSET_TEST_DRF(d,r,f,i,o,c,v) ((DRF_IDX_OFFSET_VAL(d, r, f, i, o, (v)) == (NvU32)(NV##d##r##f##c)))
318#else
319#define FLD_SET_DRF(d,r,f,c,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_DEF(d,r,f,c))
320#define FLD_SET_DRF_NUM(d,r,f,n,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_NUM(d,r,f,n))
321#define FLD_IDX_SET_DRF(d,r,f,i,c,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_DEF(d,r,f,i,c))
322#define FLD_IDX_OFFSET_SET_DRF(d,r,f,i,o,c,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f(i,o))) | DRF_IDX_OFFSET_DEF(d,r,f,i,o,c))
323#define FLD_IDX_SET_DRF_DEF(d,r,f,i,c,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_DEF(d,r,f,i,c))
324#define FLD_IDX_SET_DRF_NUM(d,r,f,i,n,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f(i))) | DRF_IDX_NUM(d,r,f,i,n))
325#define FLD_SET_DRF_IDX(d,r,f,c,i,v) (((v) & ~DRF_SHIFTMASK(NV##d##r##f)) | DRF_DEF(d,r,f,c(i)))
326
327#define FLD_TEST_DRF(d,r,f,c,v) ((DRF_VAL(d, r, f, (v)) == NV##d##r##f##c))
328#define FLD_TEST_DRF_AND(d,r,f,c,v) ((DRF_VAL(d, r, f, (v)) & NV##d##r##f##c))
329#define FLD_TEST_DRF_NUM(d,r,f,n,v) ((DRF_VAL(d, r, f, (v)) == (n)))
330#define FLD_IDX_TEST_DRF(d,r,f,i,c,v) ((DRF_IDX_VAL(d, r, f, i, (v)) == NV##d##r##f##c))
331#define FLD_IDX_OFFSET_TEST_DRF(d,r,f,i,o,c,v) ((DRF_IDX_OFFSET_VAL(d, r, f, i, o, (v)) == NV##d##r##f##c))
332#endif
333
334#define REF_DEF(drf,d) (((drf ## d)&DRF_MASK(drf))<<DRF_SHIFT(drf))
335#define REF_VAL(drf,v) (((v)>>DRF_SHIFT(drf))&DRF_MASK(drf))
336#if defined(NV_MISRA_COMPLIANCE_REQUIRED) && defined(MISRA_14_3)
337#define REF_NUM(drf,n) (((n)&(0xFFFFFFFFU>>(31U-((drf##_HIGH_FIELD) % 32U)+((drf##_LOW_FIELD) % 32U)))) << ((drf##_LOW_FIELD) % 32U))
338#else
339#define REF_NUM(drf,n) (((n)&DRF_MASK(drf))<<DRF_SHIFT(drf))
340#endif
341#define FLD_TEST_REF(drf,c,v) (REF_VAL(drf, (v)) == drf##c)
342#define FLD_TEST_REF_AND(drf,c,v) (REF_VAL(drf, (v)) & drf##c)
343#define FLD_SET_REF_NUM(drf,n,v) (((v) & ~DRF_SHIFTMASK(drf)) | REF_NUM(drf,n))
344
345#define CR_DRF_DEF(d,r,f,c) ((CR ## d ## r ## f ## c)<<DRF_SHIFT(CR ## d ## r ## f))
346#define CR_DRF_NUM(d,r,f,n) (((n)&DRF_MASK(CR ## d ## r ## f))<<DRF_SHIFT(CR ## d ## r ## f))
347#define CR_DRF_VAL(d,r,f,v) (((v)>>DRF_SHIFT(CR ## d ## r ## f))&DRF_MASK(CR ## d ## r ## f))
348
349// Multi-word (MW) field manipulations. For multi-word structures (e.g., Fermi SPH),
350// fields may have bit numbers beyond 32. To avoid errors using "classic" multi-word macros,
351// all the field extents are defined as "MW(X)". For example, MW(127:96) means
352// the field is in bits 0-31 of word number 3 of the structure.
353//
354// DRF_VAL_MW() macro is meant to be used for native endian 32-bit aligned 32-bit word data,
355// not for byte stream data.
356//
357// DRF_VAL_BS() macro is for byte stream data used in fbQueryBIOS_XXX().
358//
359#define DRF_EXPAND_MW(drf) drf // used to turn "MW(a:b)" into "a:b"
360#define DRF_PICK_MW(drf,v) ((v)? DRF_EXPAND_##drf) // picks low or high bits
361#define DRF_WORD_MW(drf) (DRF_PICK_MW(drf,0)/32) // which word in a multi-word array
362#define DRF_BASE_MW(drf) (DRF_PICK_MW(drf,0)%32) // which start bit in the selected word?
363#define DRF_EXTENT_MW(drf) (DRF_PICK_MW(drf,1)%32) // which end bit in the selected word
364#define DRF_SHIFT_MW(drf) (DRF_PICK_MW(drf,0)%32)
365#define DRF_MASK_MW(drf) (0xFFFFFFFFU>>((31-(DRF_EXTENT_MW(drf))+(DRF_BASE_MW(drf)))%32))
366#define DRF_SHIFTMASK_MW(drf) ((DRF_MASK_MW(drf))<<(DRF_SHIFT_MW(drf)))
367#define DRF_SIZE_MW(drf) (DRF_EXTENT_MW(drf)-DRF_BASE_MW(drf)+1)
368
369#define DRF_DEF_MW(d,r,f,c) ((NV##d##r##f##c) << DRF_SHIFT_MW(NV##d##r##f))
370#define DRF_NUM_MW(d,r,f,n) (((n)&DRF_MASK_MW(NV##d##r##f))<<DRF_SHIFT_MW(NV##d##r##f))
371//
372// DRF_VAL_MW is the ONLY multi-word macro which supports spanning. No other MW macro supports spanning currently
373//
374#define DRF_VAL_MW_1WORD(d,r,f,v) ((((v)[DRF_WORD_MW(NV##d##r##f)])>>DRF_SHIFT_MW(NV##d##r##f))&DRF_MASK_MW(NV##d##r##f))
375#define DRF_SPANS(drf) ((DRF_PICK_MW(drf,0)/32) != (DRF_PICK_MW(drf,1)/32))
376#define DRF_WORD_MW_LOW(drf) (DRF_PICK_MW(drf,0)/32)
377#define DRF_WORD_MW_HIGH(drf) (DRF_PICK_MW(drf,1)/32)
378#define DRF_MASK_MW_LOW(drf) (0xFFFFFFFFU)
379#define DRF_MASK_MW_HIGH(drf) (0xFFFFFFFFU>>(31-(DRF_EXTENT_MW(drf))))
380#define DRF_SHIFT_MW_LOW(drf) (DRF_PICK_MW(drf,0)%32)
381#define DRF_SHIFT_MW_HIGH(drf) (0)
382#define DRF_MERGE_SHIFT(drf) ((32-((DRF_PICK_MW(drf,0)%32)))%32)
383#define DRF_VAL_MW_2WORD(d,r,f,v) (((((v)[DRF_WORD_MW_LOW(NV##d##r##f)])>>DRF_SHIFT_MW_LOW(NV##d##r##f))&DRF_MASK_MW_LOW(NV##d##r##f)) | \
384 (((((v)[DRF_WORD_MW_HIGH(NV##d##r##f)])>>DRF_SHIFT_MW_HIGH(NV##d##r##f))&DRF_MASK_MW_HIGH(NV##d##r##f)) << DRF_MERGE_SHIFT(NV##d##r##f)))
385#define DRF_VAL_MW(d,r,f,v) ( DRF_SPANS(NV##d##r##f) ? DRF_VAL_MW_2WORD(d,r,f,v) : DRF_VAL_MW_1WORD(d,r,f,v) )
386
387#define DRF_IDX_DEF_MW(d,r,f,i,c) ((NV##d##r##f##c)<<DRF_SHIFT_MW(NV##d##r##f(i)))
388#define DRF_IDX_NUM_MW(d,r,f,i,n) (((n)&DRF_MASK_MW(NV##d##r##f(i)))<<DRF_SHIFT_MW(NV##d##r##f(i)))
389#define DRF_IDX_VAL_MW(d,r,f,i,v) ((((v)[DRF_WORD_MW(NV##d##r##f(i))])>>DRF_SHIFT_MW(NV##d##r##f(i)))&DRF_MASK_MW(NV##d##r##f(i)))
390
391//
392// Logically OR all DRF_DEF constants indexed from zero to s (semiinclusive).
393// Caution: Target variable v must be pre-initialized.
394//
395#define FLD_IDX_OR_DRF_DEF(d,r,f,c,s,v) \
396do \
397{ NvU32 idx; \
398 for (idx = 0; idx < (NV ## d ## r ## f ## s); ++idx)\
399 { \
400 v |= DRF_IDX_DEF(d,r,f,idx,c); \
401 } \
402} while(0)
403
404
405#define FLD_MERGE_MW(drf,n,v) (((v)[DRF_WORD_MW(drf)] & ~DRF_SHIFTMASK_MW(drf)) | n)
406#define FLD_ASSIGN_MW(drf,n,v) ((v)[DRF_WORD_MW(drf)] = FLD_MERGE_MW(drf, n, v))
407#define FLD_IDX_MERGE_MW(drf,i,n,v) (((v)[DRF_WORD_MW(drf(i))] & ~DRF_SHIFTMASK_MW(drf(i))) | n)
408#define FLD_IDX_ASSIGN_MW(drf,i,n,v) ((v)[DRF_WORD_MW(drf(i))] = FLD_MERGE_MW(drf(i), n, v))
409
410#define FLD_SET_DRF_MW(d,r,f,c,v) FLD_MERGE_MW(NV##d##r##f, DRF_DEF_MW(d,r,f,c), v)
411#define FLD_SET_DRF_NUM_MW(d,r,f,n,v) FLD_ASSIGN_MW(NV##d##r##f, DRF_NUM_MW(d,r,f,n), v)
412#define FLD_SET_DRF_DEF_MW(d,r,f,c,v) FLD_ASSIGN_MW(NV##d##r##f, DRF_DEF_MW(d,r,f,c), v)
413#define FLD_IDX_SET_DRF_MW(d,r,f,i,c,v) FLD_IDX_MERGE_MW(NV##d##r##f, i, DRF_IDX_DEF_MW(d,r,f,i,c), v)
414#define FLD_IDX_SET_DRF_DEF_MW(d,r,f,i,c,v) FLD_IDX_MERGE_MW(NV##d##r##f, i, DRF_IDX_DEF_MW(d,r,f,i,c), v)
415#define FLD_IDX_SET_DRF_NUM_MW(d,r,f,i,n,v) FLD_IDX_ASSIGN_MW(NV##d##r##f, i, DRF_IDX_NUM_MW(d,r,f,i,n), v)
416
417#define FLD_TEST_DRF_MW(d,r,f,c,v) ((DRF_VAL_MW(d, r, f, (v)) == NV##d##r##f##c))
418#define FLD_TEST_DRF_NUM_MW(d,r,f,n,v) ((DRF_VAL_MW(d, r, f, (v)) == n))
419#define FLD_IDX_TEST_DRF_MW(d,r,f,i,c,v) ((DRF_IDX_VAL_MW(d, r, f, i, (v)) == NV##d##r##f##c))
420
421#define DRF_VAL_BS(d,r,f,v) ( DRF_SPANS(NV##d##r##f) ? DRF_VAL_BS_2WORD(d,r,f,(v)) : DRF_VAL_BS_1WORD(d,r,f,(v)) )
422
423//------------------------------------------------------------------------//
424// //
425// Common defines for engine register reference wrappers //
426// //
427// New engine addressing can be created like: //
428// \#define ENG_REG_PMC(o,d,r) NV##d##r //
429// \#define ENG_IDX_REG_CE(o,d,i,r) CE_MAP(o,r,i) //
430// //
431// See FB_FBPA* for more examples //
432//------------------------------------------------------------------------//
433
434#define ENG_RD_REG(g,o,d,r) GPU_REG_RD32(g, ENG_REG##d(o,d,r))
435#define ENG_WR_REG(g,o,d,r,v) GPU_REG_WR32(g, ENG_REG##d(o,d,r), (v))
436#define ENG_RD_DRF(g,o,d,r,f) ((GPU_REG_RD32(g, ENG_REG##d(o,d,r))>>GPU_DRF_SHIFT(NV ## d ## r ## f))&GPU_DRF_MASK(NV ## d ## r ## f))
437#define ENG_WR_DRF_DEF(g,o,d,r,f,c) GPU_REG_WR32(g, ENG_REG##d(o,d,r),(GPU_REG_RD32(g,ENG_REG##d(o,d,r))&~(GPU_DRF_MASK(NV##d##r##f)<<GPU_DRF_SHIFT(NV##d##r##f)))|GPU_DRF_DEF(d,r,f,c))
438#define ENG_WR_DRF_NUM(g,o,d,r,f,n) GPU_REG_WR32(g, ENG_REG##d(o,d,r),(GPU_REG_RD32(g,ENG_REG##d(o,d,r))&~(GPU_DRF_MASK(NV##d##r##f)<<GPU_DRF_SHIFT(NV##d##r##f)))|GPU_DRF_NUM(d,r,f,n))
439#define ENG_TEST_DRF_DEF(g,o,d,r,f,c) (ENG_RD_DRF(g, o, d, r, f) == NV##d##r##f##c)
440
441#define ENG_RD_IDX_DRF(g,o,d,r,f,i) ((GPU_REG_RD32(g, ENG_REG##d(o,d,r(i)))>>GPU_DRF_SHIFT(NV ## d ## r ## f))&GPU_DRF_MASK(NV ## d ## r ## f))
442#define ENG_TEST_IDX_DRF_DEF(g,o,d,r,f,c,i) (ENG_RD_IDX_DRF(g, o, d, r, f, (i)) == NV##d##r##f##c)
443
444#define ENG_IDX_RD_REG(g,o,d,i,r) GPU_REG_RD32(g, ENG_IDX_REG##d(o,d,i,r))
445#define ENG_IDX_WR_REG(g,o,d,i,r,v) GPU_REG_WR32(g, ENG_IDX_REG##d(o,d,i,r), (v))
446
447#define ENG_IDX_RD_DRF(g,o,d,i,r,f) ((GPU_REG_RD32(g, ENG_IDX_REG##d(o,d,i,r))>>GPU_DRF_SHIFT(NV ## d ## r ## f))&GPU_DRF_MASK(NV ## d ## r ## f))
448
449//
450// DRF_READ_1WORD_BS() and DRF_READ_1WORD_BS_HIGH() do not read beyond the bytes that contain
451// the requested value. Reading beyond the actual data causes a page fault panic when the
452// immediately following page happened to be protected or not mapped.
453//
454#define DRF_VAL_BS_1WORD(d,r,f,v) ((DRF_READ_1WORD_BS(d,r,f,v)>>DRF_SHIFT_MW(NV##d##r##f))&DRF_MASK_MW(NV##d##r##f))
455#define DRF_VAL_BS_2WORD(d,r,f,v) (((DRF_READ_4BYTE_BS(NV##d##r##f,v)>>DRF_SHIFT_MW_LOW(NV##d##r##f))&DRF_MASK_MW_LOW(NV##d##r##f)) | \
456 (((DRF_READ_1WORD_BS_HIGH(d,r,f,v)>>DRF_SHIFT_MW_HIGH(NV##d##r##f))&DRF_MASK_MW_HIGH(NV##d##r##f)) << DRF_MERGE_SHIFT(NV##d##r##f)))
457
458#define DRF_READ_1BYTE_BS(drf,v) ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW(drf)*4]))
459#define DRF_READ_2BYTE_BS(drf,v) (DRF_READ_1BYTE_BS(drf,v)| \
460 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW(drf)*4+1])<<8))
461#define DRF_READ_3BYTE_BS(drf,v) (DRF_READ_2BYTE_BS(drf,v)| \
462 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW(drf)*4+2])<<16))
463#define DRF_READ_4BYTE_BS(drf,v) (DRF_READ_3BYTE_BS(drf,v)| \
464 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW(drf)*4+3])<<24))
465
466#define DRF_READ_1BYTE_BS_HIGH(drf,v) ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW_HIGH(drf)*4]))
467#define DRF_READ_2BYTE_BS_HIGH(drf,v) (DRF_READ_1BYTE_BS_HIGH(drf,v)| \
468 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW_HIGH(drf)*4+1])<<8))
469#define DRF_READ_3BYTE_BS_HIGH(drf,v) (DRF_READ_2BYTE_BS_HIGH(drf,v)| \
470 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW_HIGH(drf)*4+2])<<16))
471#define DRF_READ_4BYTE_BS_HIGH(drf,v) (DRF_READ_3BYTE_BS_HIGH(drf,v)| \
472 ((NvU32)(((const NvU8*)(v))[DRF_WORD_MW_HIGH(drf)*4+3])<<24))
473
474// Calculate 2^n - 1 and avoid shift counter overflow
475//
476// On Windows amd64, 64 << 64 => 1
477//
478#define NV_TWO_N_MINUS_ONE(n) (((1ULL<<(n/2))<<((n+1)/2))-1)
479
480//
481// Create a 64b bitmask with n bits set
482// This is the same as ((1ULL<<n) - 1), but it doesn't overflow for n=64
483//
484// ...
485// n=-1, 0x0000000000000000
486// n=0, 0x0000000000000000
487// n=1, 0x0000000000000001
488// ...
489// n=63, 0x7FFFFFFFFFFFFFFF
490// n=64, 0xFFFFFFFFFFFFFFFF
491// n=65, 0xFFFFFFFFFFFFFFFF
492// n=66, 0xFFFFFFFFFFFFFFFF
493// ...
494//
495#define NV_BITMASK64(n) ((n<1) ? 0ULL : (NV_U64_MAX>>((n>64) ? 0 : (64-n))))
496
497#define DRF_READ_1WORD_BS(d,r,f,v) \
498 ((DRF_EXTENT_MW(NV##d##r##f)<8)?DRF_READ_1BYTE_BS(NV##d##r##f,(v)): \
499 ((DRF_EXTENT_MW(NV##d##r##f)<16)?DRF_READ_2BYTE_BS(NV##d##r##f,(v)): \
500 ((DRF_EXTENT_MW(NV##d##r##f)<24)?DRF_READ_3BYTE_BS(NV##d##r##f,(v)): \
501 DRF_READ_4BYTE_BS(NV##d##r##f,(v)))))
502
503#define DRF_READ_1WORD_BS_HIGH(d,r,f,v) \
504 ((DRF_EXTENT_MW(NV##d##r##f)<8)?DRF_READ_1BYTE_BS_HIGH(NV##d##r##f,(v)): \
505 ((DRF_EXTENT_MW(NV##d##r##f)<16)?DRF_READ_2BYTE_BS_HIGH(NV##d##r##f,(v)): \
506 ((DRF_EXTENT_MW(NV##d##r##f)<24)?DRF_READ_3BYTE_BS_HIGH(NV##d##r##f,(v)): \
507 DRF_READ_4BYTE_BS_HIGH(NV##d##r##f,(v)))))
508
509#define LOWESTBIT(x) ( (x) & (((x) - 1U) ^ (x)) )
510// Destructive operation on n32
511#define HIGHESTBIT(n32) \
512{ \
513 HIGHESTBITIDX_32(n32); \
514 n32 = NVBIT(n32); \
515}
516#define ONEBITSET(x) ( ((x) != 0U) && (((x) & ((x) - 1U)) == 0U) )
517
518// Destructive operation on n32
519#define NUMSETBITS_32(n32) \
520{ \
521 n32 = n32 - ((n32 >> 1) & 0x55555555); \
522 n32 = (n32 & 0x33333333) + ((n32 >> 2) & 0x33333333); \
523 n32 = (((n32 + (n32 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; \
524}
525
526/*!
527 * Calculate number of bits set in a 32-bit unsigned integer.
528 * Pure typesafe alternative to @ref NUMSETBITS_32.
529 */
530static NV_FORCEINLINE NvU32
531nvPopCount32(const NvU32 x)
532{
533 NvU32 temp = x;
534 temp = temp - ((temp >> 1) & 0x55555555U);
535 temp = (temp & 0x33333333U) + ((temp >> 2) & 0x33333333U);
536 temp = (((temp + (temp >> 4)) & 0x0F0F0F0FU) * 0x01010101U) >> 24;
537 return temp;
538}
539
540/*!
541 * Calculate number of bits set in a 64-bit unsigned integer.
542 */
543static NV_FORCEINLINE NvU32
544nvPopCount64(const NvU64 x)
545{
546 NvU64 temp = x;
547 temp = temp - ((temp >> 1) & 0x5555555555555555ULL);
548 temp = (temp & 0x3333333333333333ULL) + ((temp >> 2) & 0x3333333333333333ULL);
549 temp = (temp + (temp >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
550 temp = (temp * 0x0101010101010101ULL) >> 56;
551 return (NvU32)temp;
552}
553
554/*!
555 * Determine how many bits are set below a bit index within a mask.
556 * This assigns a dense ordering to the set bits in the mask.
557 *
558 * For example the mask 0xCD contains 5 set bits:
559 * nvMaskPos32(0xCD, 0) == 0
560 * nvMaskPos32(0xCD, 2) == 1
561 * nvMaskPos32(0xCD, 3) == 2
562 * nvMaskPos32(0xCD, 6) == 3
563 * nvMaskPos32(0xCD, 7) == 4
564 */
565static NV_FORCEINLINE NvU32
566nvMaskPos32(const NvU32 mask, const NvU32 bitIdx)
567{
568 return nvPopCount32(mask & (NVBIT32(bitIdx) - 1U));
569}
570
571// Destructive operation on n32
572#define LOWESTBITIDX_32(n32) \
573{ \
574 n32 = BIT_IDX_32(LOWESTBIT(n32));\
575}
576
577// Destructive operation on n64
578#define LOWESTBITIDX_64(n64) \
579{ \
580 n64 = BIT_IDX_64(LOWESTBIT(n64));\
581}
582
583// Destructive operation on n32
584#define HIGHESTBITIDX_32(n32) \
585{ \
586 NvU32 count = 0; \
587 while (n32 >>= 1) \
588 { \
589 count++; \
590 } \
591 n32 = count; \
592}
593
594// Destructive operation on n64
595#define HIGHESTBITIDX_64(n64) \
596{ \
597 NvU64 count = 0; \
598 while (n64 >>= 1) \
599 { \
600 count++; \
601 } \
602 n64 = count; \
603}
604
605// Destructive operation on n32
606#define ROUNDUP_POW2(n32) \
607{ \
608 n32--; \
609 n32 |= n32 >> 1; \
610 n32 |= n32 >> 2; \
611 n32 |= n32 >> 4; \
612 n32 |= n32 >> 8; \
613 n32 |= n32 >> 16; \
614 n32++; \
615}
616
617/*!
618 * Round up a 32-bit unsigned integer to the next power of 2.
619 * Pure typesafe alternative to @ref ROUNDUP_POW2.
620 *
621 * param[in] x must be in range [0, 2^31] to avoid overflow.
622 */
623static NV_FORCEINLINE NvU32
624nvNextPow2_U32(const NvU32 x)
625{
626 NvU32 y = x;
627 y--;
628 y |= y >> 1;
629 y |= y >> 2;
630 y |= y >> 4;
631 y |= y >> 8;
632 y |= y >> 16;
633 y++;
634 return y;
635}
636
637
638static NV_FORCEINLINE NvU32
639nvPrevPow2_U32(const NvU32 x )
640{
641 NvU32 y = x;
642 y |= (y >> 1);
643 y |= (y >> 2);
644 y |= (y >> 4);
645 y |= (y >> 8);
646 y |= (y >> 16);
647 return y - (y >> 1);
648}
649
650static NV_FORCEINLINE NvU64
651nvPrevPow2_U64(const NvU64 x )
652{
653 NvU64 y = x;
654 y |= (y >> 1);
655 y |= (y >> 2);
656 y |= (y >> 4);
657 y |= (y >> 8);
658 y |= (y >> 16);
659 y |= (y >> 32);
660 return y - (y >> 1);
661}
662
663// Destructive operation on n64
664#define ROUNDUP_POW2_U64(n64) \
665{ \
666 n64--; \
667 n64 |= n64 >> 1; \
668 n64 |= n64 >> 2; \
669 n64 |= n64 >> 4; \
670 n64 |= n64 >> 8; \
671 n64 |= n64 >> 16; \
672 n64 |= n64 >> 32; \
673 n64++; \
674}
675
676#define NV_SWAP_U8(a,b) \
677{ \
678 NvU8 temp; \
679 temp = a; \
680 a = b; \
681 b = temp; \
682}
683
684#define NV_SWAP_U32(a,b) \
685{ \
686 NvU32 temp; \
687 temp = a; \
688 a = b; \
689 b = temp; \
690}
691
692/*!
693 * @brief Macros allowing simple iteration over bits set in a given mask.
694 *
695 * @param[in] maskWidth bit-width of the mask (allowed: 8, 16, 32, 64)
696 *
697 * @param[in,out] index lvalue that is used as a bit index in the loop
698 * (can be declared as any NvU* or NvS* variable)
699 * @param[in] mask expression, loop will iterate over set bits only
700 */
701#define FOR_EACH_INDEX_IN_MASK(maskWidth,index,mask) \
702{ \
703 NvU##maskWidth lclMsk = (NvU##maskWidth)(mask); \
704 for ((index) = 0U; lclMsk != 0U; (index)++, lclMsk >>= 1U)\
705 { \
706 if (((NvU##maskWidth)NVBIT64(0) & lclMsk) == 0U) \
707 { \
708 continue; \
709 }
710#define FOR_EACH_INDEX_IN_MASK_END \
711 } \
712}
713
714/*!
715 * Returns the position of nth set bit in the given mask.
716 *
717 * Returns -1 if mask has fewer than n bits set.
718 *
719 * n is 0 indexed and has valid values 0..31 inclusive, so "zeroth" set bit is
720 * the first set LSB.
721 *
722 * Example, if mask = 0x000000F0u and n = 1, the return value will be 5.
723 * Example, if mask = 0x000000F0u and n = 4, the return value will be -1.
724 */
725static NV_FORCEINLINE NvS32
726nvGetNthSetBitIndex32(NvU32 mask, NvU32 n)
727{
728 NvU32 seenSetBitsCount = 0;
729 NvS32 index;
730 FOR_EACH_INDEX_IN_MASK(32, index, mask)
731 {
732 if (seenSetBitsCount == n)
733 {
734 return index;
735 }
736 ++seenSetBitsCount;
737 }
738 FOR_EACH_INDEX_IN_MASK_END;
739
740 return -1;
741}
742
743//
744// Size to use when declaring variable-sized arrays
745//
746#define NV_ANYSIZE_ARRAY 1
747
748//
749// Returns ceil(a/b)
750//
751#define NV_CEIL(a,b) (((a)+(b)-1)/(b))
752
753// Clearer name for NV_CEIL
754#ifndef NV_DIV_AND_CEIL
755#define NV_DIV_AND_CEIL(a, b) NV_CEIL(a,b)
756#endif
757
758#ifndef NV_MIN
759#define NV_MIN(a, b) (((a) < (b)) ? (a) : (b))
760#endif
761
762#ifndef NV_MAX
763#define NV_MAX(a, b) (((a) > (b)) ? (a) : (b))
764#endif
765
766//
767// Returns absolute value of provided integer expression
768//
769#define NV_ABS(a) ((a)>=0?(a):(-(a)))
770
771//
772// Returns 1 if input number is positive, 0 if 0 and -1 if negative. Avoid
773// macro parameter as function call which will have side effects.
774//
775#define NV_SIGN(s) ((NvS8)(((s) > 0) - ((s) < 0)))
776
777//
778// Returns 1 if input number is >= 0 or -1 otherwise. This assumes 0 has a
779// positive sign.
780//
781#define NV_ZERO_SIGN(s) ((NvS8)((((s) >= 0) * 2) - 1))
782
783// Returns the offset (in bytes) of 'member' in struct 'type'.
784#ifndef NV_OFFSETOF
785 #if defined(__GNUC__) && (__GNUC__ > 3)
786 #define NV_OFFSETOF(type, member) ((NvUPtr) __builtin_offsetof(type, member))
787 #else
788 #define NV_OFFSETOF(type, member) ((NvUPtr) &(((type *)0)->member))
789 #endif
790#endif
791
792// Given a pointer and the member it is of the parent struct, return a pointer to the parent struct
793#define NV_CONTAINEROF(ptr, type, member) ((type *) (((NvUPtr) ptr) - NV_OFFSETOF(type, member)))
794
795//
796// Performs a rounded division of b into a (unsigned). For SIGNED version of
797// NV_ROUNDED_DIV() macro check the comments in bug 769777.
798//
799#define NV_UNSIGNED_ROUNDED_DIV(a,b) (((a) + ((b) / 2U)) / (b))
800
801/*!
802 * Performs a ceiling division of b into a (unsigned). A "ceiling" division is
803 * a division is one with rounds up result up if a % b != 0.
804 *
805 * @param[in] a Numerator
806 * @param[in] b Denominator
807 *
808 * @return a / b + a % b != 0 ? 1 : 0.
809 */
810#define NV_UNSIGNED_DIV_CEIL(a, b) (((a) + (b - 1)) / (b))
811
812/*!
813 * Performs subtraction where a negative difference is raised to zero.
814 * Can be used to avoid underflowing an unsigned subtraction.
815 *
816 * @param[in] a Minuend
817 * @param[in] b Subtrahend
818 *
819 * @return a > b ? a - b : 0.
820 */
821#define NV_SUBTRACT_NO_UNDERFLOW(a, b) ((a)>(b) ? (a)-(b) : 0)
822
823/*!
824 * Performs a rounded right-shift of 32-bit unsigned value "a" by "shift" bits.
825 * Will round result away from zero.
826 *
827 * @param[in] a 32-bit unsigned value to shift.
828 * @param[in] shift Number of bits by which to shift.
829 *
830 * @return Resulting shifted value rounded away from zero.
831 */
832#define NV_RIGHT_SHIFT_ROUNDED(a, shift) \
833 (((a) >> (shift)) + !!((NVBIT((shift) - 1) & (a)) == NVBIT((shift) - 1)))
834
835//
836// Power of 2 alignment.
837// (Will give unexpected results if 'gran' is not a power of 2.)
838//
839#ifndef NV_ALIGN_DOWN
840//
841// Notably using v - v + gran ensures gran gets promoted to the same type as v if gran has a smaller type.
842// Otherwise, if aligning a NVU64 with NVU32 granularity, the top 4 bytes get zeroed.
843//
844#define NV_ALIGN_DOWN(v, gran) ((v) & ~((v) - (v) + (gran) - 1))
845#endif
846
847#ifndef NV_ALIGN_UP
848//
849// Notably using v - v + gran ensures gran gets promoted to the same type as v if gran has a smaller type.
850// Otherwise, if aligning a NVU64 with NVU32 granularity, the top 4 bytes get zeroed.
851//
852#define NV_ALIGN_UP(v, gran) (((v) + ((gran) - 1)) & ~((v) - (v) + (gran) - 1))
853#endif
854
855#ifndef NV_ALIGN_DOWN64
856#define NV_ALIGN_DOWN64(v, gran) ((v) & ~(((NvU64)gran) - 1))
857#endif
858
859#ifndef NV_ALIGN_UP64
860#define NV_ALIGN_UP64(v, gran) (((v) + ((gran) - 1)) & ~(((NvU64)gran)-1))
861#endif
862
863#ifndef NV_IS_ALIGNED
864#define NV_IS_ALIGNED(v, gran) (0U == ((v) & ((gran) - 1U)))
865#endif
866
867#ifndef NV_IS_ALIGNED64
868#define NV_IS_ALIGNED64(v, gran) (0U == ((v) & (((NvU64)gran) - 1U)))
869#endif
870
871#ifndef NVMISC_MEMSET
872static NV_FORCEINLINE void *NVMISC_MEMSET(void *s, NvU8 c, NvLength n)
873{
874 NvU8 *b = (NvU8 *) s;
875 NvLength i;
876
877 for (i = 0; i < n; i++)
878 {
879 b[i] = c;
880 }
881
882 return s;
883}
884#endif
885
886#ifndef NVMISC_MEMCPY
887static NV_FORCEINLINE void *NVMISC_MEMCPY(void *dest, const void *src, NvLength n)
888{
889 NvU8 *destByte = (NvU8 *) dest;
890 const NvU8 *srcByte = (const NvU8 *) src;
891 NvLength i;
892
893 for (i = 0; i < n; i++)
894 {
895 destByte[i] = srcByte[i];
896 }
897
898 return dest;
899}
900#endif
901
902static NV_FORCEINLINE char *NVMISC_STRNCPY(char *dest, const char *src, NvLength n)
903{
904 NvLength i;
905
906 for (i = 0; i < n; i++)
907 {
908 dest[i] = src[i];
909 if (src[i] == '\0')
910 {
911 break;
912 }
913 }
914
915 for (; i < n; i++)
916 {
917 dest[i] = '\0';
918 }
919
920 return dest;
921}
922
923/*!
924 * Convert a void* to an NvUPtr. This is used when MISRA forbids us from doing a direct cast.
925 *
926 * @param[in] ptr Pointer to be converted
927 *
928 * @return Resulting NvUPtr
929 */
930static NV_FORCEINLINE NvUPtr NV_PTR_TO_NVUPTR(void *ptr)
931{
932 union
933 {
934 NvUPtr v;
935 void *p;
936 } uAddr;
937
938 uAddr.p = ptr;
939 return uAddr.v;
940}
941
942/*!
943 * Convert an NvUPtr to a void*. This is used when MISRA forbids us from doing a direct cast.
944 *
945 * @param[in] ptr Pointer to be converted
946 *
947 * @return Resulting void *
948 */
949static NV_FORCEINLINE void *NV_NVUPTR_TO_PTR(NvUPtr address)
950{
951 union
952 {
953 NvUPtr v;
954 void *p;
955 } uAddr;
956
957 uAddr.v = address;
958 return uAddr.p;
959}
960
961// Get bit at pos (k) from x
962#define NV_BIT_GET(k, x) (((x) >> (k)) & 1)
963// Get bit at pos (n) from (hi) if >= 64, otherwise from (lo). This is paired with NV_BIT_SET_128 which sets the bit.
964#define NV_BIT_GET_128(n, lo, hi) (((n) < 64) ? NV_BIT_GET((n), (lo)) : NV_BIT_GET((n) - 64, (hi)))
965//
966// Set the bit at pos (b) for U64 which is < 128. Since the (b) can be >= 64, we need 2 U64 to store this.
967// Use (lo) if (b) is less than 64, and (hi) if >= 64.
968//
969#define NV_BIT_SET_128(b, lo, hi) { nvAssert( (b) < 128 ); if ( (b) < 64 ) (lo) |= NVBIT64(b); else (hi) |= NVBIT64( b & 0x3F ); }
970//
971// Clear the bit at pos (b) for U64 which is < 128.
972// Use (lo) if (b) is less than 64, and (hi) if >= 64.
973//
974#define NV_BIT_CLEAR_128(b, lo, hi) { nvAssert( (b) < 128 ); if ( (b) < 64 ) (lo) &= ~NVBIT64(b); else (hi) &= ~NVBIT64( b & 0x3F ); }
975
976// Get the number of elements the specified fixed-size array
977#define NV_ARRAY_ELEMENTS(x) ((sizeof(x)/sizeof((x)[0])))
978
979#if !defined(NVIDIA_UNDEF_LEGACY_BIT_MACROS)
980//
981// Deprecated macros whose definition can be removed once the code base no longer references them.
982// Use the NVBIT* macros instead of these macros.
983//
984#ifndef BIT
985#define BIT(b) (1U<<(b))
986#endif
987#ifndef BIT32
988#define BIT32(b) ((NvU32)1U<<(b))
989#endif
990#ifndef BIT64
991#define BIT64(b) ((NvU64)1U<<(b))
992#endif
993#endif
994
995#ifdef __cplusplus
996}
997#endif //__cplusplus
998
999#endif // __NV_MISC_H
1000