1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 1993-2020 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 #ifndef __PRELUDE_H__
24 #define __PRELUDE_H__
25 
26 /* ------------------------ C library --------------------------------------- */
27 #include <stddef.h>                 // NULL
28 
29 /* ------------------------ SDK includes ------------------------------------ */
30 
31 #include "nvtypes.h"
32 #include "nvrangetypes.h"
33 #include "nvstatus.h"
34 #include "nvmisc.h"
35 #include "nvlimits.h"
36 #include "nvos.h"
37 
38 #include "nvctassert.h"
39 
40 /* ------------------------ RM library and utils ---------------------------- */
41 #include "nvport/nvport.h"
42 #include "nvoc/runtime.h"
43 #include "core/printf.h"
44 #include "core/strict.h"
45 #include "utils/nvassert.h"
46 
47 /* ------------------------ Code-generation --------------------------------- */
48 #include "rmconfig.h"               // RMCONFIG header generated by config/rmconfig.pl
49 #include "g_rmconfig_private.h"     // resman-private hal setup such as: IsGK104(), etc.
50 #include "g_nvh_state.h"            // pass enable/disable state to NVOC headers
51 #include "g_odb.h"
52 #include "g_hal.h"
53 
54 #include "rmcd.h"
55 
56 /* ------------------------ Common types ------------------------------------ */
57 typedef NvU64 RmPhysAddr;           // A physical address should be 64 bits
58 
59 typedef struct THREAD_STATE_NODE THREAD_STATE_NODE; // FW declare thread state
60 
61 /* ------------------------ Utility Macros ---------------------------------- */
62 
63 //
64 // Power of 2 alignment.
65 //    (Will give unexpected results if 'gran' is not a power of 2.)
66 //    (v - v + gran) ensures that gran is upcasted to match v before
67 //    the ~ operation, without explicitly having to typecast it.
68 //
69 #define RM_ALIGN_DOWN(v, gran)          ((v) & ~(((v) - (v) + (gran)) - 1))
70 #define RM_ALIGN_UP(v, gran)            (((v) + ((gran) - 1)) & ~(((v) - (v) + (gran))-1))
71 #define RM_IS_ALIGNED(v, gran)          ((((gran) - 1) & (v)) == 0)
72 
73 #define RM_ALIGN_PTR_DOWN(p, gran)      ((void *) RM_ALIGN_DOWN(((NvUPtr)p), (gran)))
74 #define RM_ALIGN_PTR_UP(p, gran)        ((void *) RM_ALIGN_UP(((NvUPtr)p), (gran)))
75 
76 #define RM_PAGE_ALIGN_DOWN(value)       RM_ALIGN_DOWN((value), RM_PAGE_SIZE)
77 #define RM_PAGE_ALIGN_UP(value)         RM_ALIGN_UP((value), RM_PAGE_SIZE)
78 
79 #define NV_DELTA(a, b)                  (NV_MAX((a), (b)) - NV_MIN((a), (b)))       // Okay for unsigned or signed
80 
81 #define NV_ROUNDUP(a,b)                 ((NV_CEIL(a,b))*(b))
82 #define NV_ROUND_TO_QUANTA(a, quanta)   (((quanta) == 0) ? (a): ((((a) + ((quanta) >> 1)) / (quanta)) *  (quanta)))
83 #define NV_FLOOR_TO_QUANTA(a, quanta)   (((a) / (quanta)) *  (quanta))
84 #define NV_ARRAY_ELEMENTS(x)            ((sizeof(x)/sizeof((x)[0])))
85 #define NV_BYTESWAP16(a)                ((((a) & 0xff00)>>8)      |  \
86                                          (((a) & 0x00ff)<<8))
87 #define NV_BYTESWAP32(a)                ((((a) & 0xff000000)>>24) |  \
88                                          (((a) & 0x00ff0000)>>8)  |  \
89                                          (((a) & 0x0000ff00)<<8)  |  \
90                                          (((a) & 0x000000ff)<<24))
91 #define NV_TO_LOWER(c)                  (((c)>='A'&&(c)<='Z')?(c)+('a'-'A'):(c))
92 #define NV_TO_UPPER(c)                  (((c)>='a'&&(c)<='z')?((c)-'a'+'A'):(c))
93 
94 /*!
95  *  Creates a byte mask for a word at given offset.
96  *  offset = 0   0xffffff00
97  *  offset = 1   0xffff00ff
98  *  offset = 2   0xff00ffff
99  *  offset = 3   0x00ffffff
100  *
101  *  @param[in]  offset  Offset for the mask.
102  */
103 #define NV_BYTE_MASK(offset)            (~(0xff << ((offset)<<3)))
104 
105 //
106 // note: the following trick fails if (z-1) * y > max_int
107 //
108 // since the calculation contains (x % z) * y,
109 // and the maximum value of (x % z) is (z-1).
110 //
111 // selecting the smaller of x and y to be y reduces the chances
112 // of problems, but for big enough z, the problem will return...
113 //
114 #define OVERFLOW_CAREFUL_MUL_DIV(x, y, z) \
115     ((x) > (y)) ? (((x) / (z)) * (y) + (((x) % (z)) * (y)) / (z)) : (((y) / (z)) * (x) + (((y) % (z)) * (x)) / (z))
116 
117 #define MASK_BITS(n)                    (~(0xFFFFFFFF << (n)))
118 
119 #endif /* __PRELUDE_H__ */
120