• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef __NVHW_DRF_H__
23 #define __NVHW_DRF_H__
24 
25 /* Helpers common to all DRF accessors. */
26 #define DRF_LO(drf)    (0 ? drf)
27 #define DRF_HI(drf)    (1 ? drf)
28 #define DRF_BITS(drf)  (DRF_HI(drf) - DRF_LO(drf) + 1)
29 #define DRF_MASK(drf)  (~0ULL >> (64 - DRF_BITS(drf)))
30 #define DRF_SMASK(drf) (DRF_MASK(drf) << DRF_LO(drf))
31 
32 /* Helpers for DRF-MW accessors. */
33 #define DRF_MX_MW(drf)      drf
34 #define DRF_MX(drf)         DRF_MX_##drf
35 #define DRF_MW(drf)         DRF_MX(drf)
36 #define DRF_MW_SPANS(o,drf) (DRF_LW_IDX((o),drf) != DRF_HW_IDX((o),drf))
37 #define DRF_MW_SIZE(o)      (sizeof((o)[0]) * 8)
38 
39 #define DRF_LW_IDX(o,drf)   (DRF_LO(DRF_MW(drf)) / DRF_MW_SIZE(o))
40 #define DRF_LW_LO(o,drf)    (DRF_LO(DRF_MW(drf)) % DRF_MW_SIZE(o))
41 #define DRF_LW_HI(o,drf)    (DRF_MW_SPANS((o),drf) ? (DRF_MW_SIZE(o) - 1) : DRF_HW_HI((o),drf))
42 #define DRF_LW_BITS(o,drf)  (DRF_LW_HI((o),drf) - DRF_LW_LO((o),drf) + 1)
43 #define DRF_LW_MASK(o,drf)  (~0ULL >> (64 - DRF_LW_BITS((o),drf)))
44 #define DRF_LW_SMASK(o,drf) (DRF_LW_MASK((o),drf) << DRF_LW_LO((o),drf))
45 #define DRF_LW_GET(o,drf)   (((o)[DRF_LW_IDX((o),drf)] >> DRF_LW_LO((o),drf)) & DRF_LW_MASK((o),drf))
46 #define DRF_LW_VAL(o,drf,v) (((v) & DRF_LW_MASK((o),drf)) << DRF_LW_LO((o),drf))
47 #define DRF_LW_CLR(o,drf)   ((o)[DRF_LW_IDX((o),drf)] & ~DRF_LW_SMASK((o),drf))
48 #define DRF_LW_SET(o,drf,v) (DRF_LW_CLR((o),drf) | DRF_LW_VAL((o),drf,(v)))
49 
50 #define DRF_HW_IDX(o,drf)   (DRF_HI(DRF_MW(drf)) / DRF_MW_SIZE(o))
51 #define DRF_HW_LO(o,drf)    0
52 #define DRF_HW_HI(o,drf)    (DRF_HI(DRF_MW(drf)) % DRF_MW_SIZE(o))
53 #define DRF_HW_BITS(o,drf)  (DRF_HW_HI((o),drf) - DRF_HW_LO((o),drf) + 1)
54 #define DRF_HW_MASK(o,drf)  (~0ULL >> (64 - DRF_HW_BITS((o),drf)))
55 #define DRF_HW_SMASK(o,drf) (DRF_HW_MASK((o),drf) << DRF_HW_LO((o),drf))
56 #define DRF_HW_GET(o,drf)   ((o)[DRF_HW_IDX(o,drf)] & DRF_HW_SMASK((o),drf))
57 #define DRF_HW_VAL(o,drf,v) (((long long)(v) >> DRF_LW_BITS((o),drf)) & DRF_HW_SMASK((o),drf))
58 #define DRF_HW_CLR(o,drf)   ((o)[DRF_HW_IDX((o),drf)] & ~DRF_HW_SMASK((o),drf))
59 #define DRF_HW_SET(o,drf,v) (DRF_HW_CLR((o),drf) | DRF_HW_VAL((o),drf,(v)))
60 
61 /* DRF accessors. */
62 #define NVVAL_X(drf,v) (((v) & DRF_MASK(drf)) << DRF_LO(drf))
63 #define NVVAL_N(X,d,r,f,  v) NVVAL_X(d##_##r##_##f, (v))
64 #define NVVAL_I(X,d,r,f,i,v) NVVAL_X(d##_##r##_##f(i), (v))
65 #define NVVAL_(X,_1,_2,_3,_4,_5,IMPL,...) IMPL
66 #define NVVAL(A...) NVVAL_(X, ##A, NVVAL_I, NVVAL_N)(X, ##A)
67 
68 #define NVDEF_N(X,d,r,f,  v) NVVAL_X(d##_##r##_##f, d##_##r##_##f##_##v)
69 #define NVDEF_I(X,d,r,f,i,v) NVVAL_X(d##_##r##_##f(i), d##_##r##_##f##_##v)
70 #define NVDEF_(X,_1,_2,_3,_4,_5,IMPL,...) IMPL
71 #define NVDEF(A...) NVDEF_(X, ##A, NVDEF_I, NVDEF_N)(X, ##A)
72 
73 #define NVVAL_GET_X(o,drf) (((o) >> DRF_LO(drf)) & DRF_MASK(drf))
74 #define NVVAL_GET_N(X,o,d,r,f  ) NVVAL_GET_X(o, d##_##r##_##f)
75 #define NVVAL_GET_I(X,o,d,r,f,i) NVVAL_GET_X(o, d##_##r##_##f(i))
76 #define NVVAL_GET_(X,_1,_2,_3,_4,_5,IMPL,...) IMPL
77 #define NVVAL_GET(A...) NVVAL_GET_(X, ##A, NVVAL_GET_I, NVVAL_GET_N)(X, ##A)
78 
79 #define NVVAL_SET_X(o,drf,v) (((o) & ~DRF_SMASK(drf)) | NVVAL_X(drf, (v)))
80 #define NVVAL_SET_N(X,o,d,r,f,  v) NVVAL_SET_X(o, d##_##r##_##f, (v))
81 #define NVVAL_SET_I(X,o,d,r,f,i,v) NVVAL_SET_X(o, d##_##r##_##f(i), (v))
82 #define NVVAL_SET_(X,_1,_2,_3,_4,_5,_6,IMPL,...) IMPL
83 #define NVVAL_SET(A...) NVVAL_SET_(X, ##A, NVVAL_SET_I, NVVAL_SET_N)(X, ##A)
84 
85 #define NVDEF_SET_N(X,o,d,r,f,  v)                                             \
86 	NVVAL_SET_X(o, d##_##r##_##f,    d##_##r##_##f##_##v)
87 #define NVDEF_SET_I(X,o,d,r,f,i,v)                                             \
88 	NVVAL_SET_X(o, d##_##r##_##f(i), d##_##r##_##f##_##v)
89 #define NVDEF_SET_(X,_1,_2,_3,_4,_5,_6,IMPL,...) IMPL
90 #define NVDEF_SET(A...) NVDEF_SET_(X, ##A, NVDEF_SET_I, NVDEF_SET_N)(X, ##A)
91 
92 /* DRF-MW accessors. */
93 #define NVVAL_MW_GET_X(o,drf)                                                  \
94 	((DRF_MW_SPANS((o),drf) ?                                              \
95 	  (DRF_HW_GET((o),drf) << DRF_LW_BITS((o),drf)) : 0) | DRF_LW_GET((o),drf))
96 #define NVVAL_MW_GET_N(X,o,d,r,f  ) NVVAL_MW_GET_X((o), d##_##r##_##f)
97 #define NVVAL_MW_GET_I(X,o,d,r,f,i) NVVAL_MW_GET_X((o), d##_##r##_##f(i))
98 #define NVVAL_MW_GET_(X,_1,_2,_3,_4,_5,IMPL,...) IMPL
99 #define NVVAL_MW_GET(A...) NVVAL_MW_GET_(X, ##A, NVVAL_MW_GET_I, NVVAL_MW_GET_N)(X, ##A)
100 
101 #define NVVAL_MW_SET_X(o,drf,v) do {                                           \
102 	(o)[DRF_LW_IDX((o),drf)] = DRF_LW_SET((o),drf,(v));                    \
103 	if (DRF_MW_SPANS((o),drf))                                             \
104 		(o)[DRF_HW_IDX((o),drf)] = DRF_HW_SET((o),drf,(v));            \
105 } while(0)
106 #define NVVAL_MW_SET_N(X,o,d,r,f,  v) NVVAL_MW_SET_X((o), d##_##r##_##f, (v))
107 #define NVVAL_MW_SET_I(X,o,d,r,f,i,v) NVVAL_MW_SET_X((o), d##_##r##_##f(i), (v))
108 #define NVVAL_MW_SET_(X,_1,_2,_3,_4,_5,_6,IMPL,...) IMPL
109 #define NVVAL_MW_SET(A...)                                                     \
110 	NVVAL_MW_SET_(X, ##A, NVVAL_MW_SET_I, NVVAL_MW_SET_N)(X, ##A)
111 
112 #define NVDEF_MW_SET_N(X,o,d,r,f,  v)                                          \
113 	NVVAL_MW_SET_X(o, d##_##r##_##f,    d##_##r##_##f##_##v)
114 #define NVDEF_MW_SET_I(X,o,d,r,f,i,v)                                          \
115 	NVVAL_MW_SET_X(o, d##_##r##_##f(i), d##_##r##_##f##_##v)
116 #define NVDEF_MW_SET_(X,_1,_2,_3,_4,_5,_6,IMPL,...) IMPL
117 #define NVDEF_MW_SET(A...)                                                     \
118 	NVDEF_MW_SET_(X, ##A, NVDEF_MW_SET_I, NVDEF_MW_SET_N)(X, ##A)
119 #endif
120