-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnvm_util.h
297 lines (218 loc) · 6.13 KB
/
nvm_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef __NVM_UTIL_H__
#define __NVM_UTIL_H__
//#ifndef __CUDACC__
//#define __device__
//#define __host__
//#endif
#include <nvm_types.h>
#include <stddef.h>
#include <stdint.h>
#include <cuda.h>
#ifdef __DIS_CLUSTER__
#include <sisci_types.h>
#include <sisci_api.h>
#endif
/* Convenience macro for creating a bit mask */
#define _NVM_MASK(num_bits) \
((1ULL << (num_bits)) - 1)
#define _NVM_MASK_PART(hi, lo) \
(_NVM_MASK((hi) + 1) - _NVM_MASK(lo))
#if defined( __NO_COHERENCE__ ) && defined( __DIS_CLUSTER__ )
#ifdef __CUDACC__
__host__ __device__
#endif
static inline
void _nvm_cache_flush(void* ptr, size_t size)
{
#ifndef __CUDA_ARCH__
sci_error_t err;
SCICacheSync(NULL, ptr, size, SCI_FLAG_CACHE_FLUSH, &err);
#endif
}
#define nvm_cache_flush(ptr, size) _nvm_cache_flush(ptr, size)
#else
#define nvm_cache_flush(ptr, size)
#endif
#if defined( __NO_COHERENCE__ ) && defined( __DIS_CLUSTER__ )
#ifdef __CUDACC__
__host__ __device__
#endif
static inline
void _nvm_cache_invalidate(void* ptr, size_t size)
{
#ifndef __CUDA_ARCH__
sci_error_t err;
SCICacheSync(NULL, ptr, size, SCI_FLAG_CACHE_FLUSH | SCI_FLAG_CACHE_INVALIDATE, &err);
#endif
}
#define nvm_cache_invalidate(ptr, size) _nvm_cache_invalidate(ptr, size)
#else
#define nvm_cache_invalidate(ptr, size)
#endif
#if defined( __DIS_CLUSTER__ )
#ifdef __CUDACC__
__host__ __device__
#endif
static inline
void _nvm_wcb_flush()
{
#ifndef __CUDA_ARCH__
SCIFlush(NULL, 0);
#endif
}
#define nvm_wcb_flush() _nvm_wcb_flush()
#else
#define nvm_wcb_flush()
#endif
/* Extract specific bits */
#define _RB(v, hi, lo) \
( ( (v) & _NVM_MASK_PART((hi), (lo)) ) >> (lo) )
/* Set specifics bits */
#define _WB(v, hi, lo) \
( ( (v) << (lo) ) & _NVM_MASK_PART((hi), (lo)) )
/* Offset to a register */
#define _REG(p, offs, bits) \
((volatile uint##bits##_t *) (((volatile unsigned char*) ((volatile void*) (p))) + (offs)))
/*
* Calculate block number from page number.
*/
#define NVM_PAGE_TO_BLOCK(page_size, block_size, pageno) \
(((page_size) * (pageno)) / (block_size))
/*
* Calculate page number from block number.
*/
#define NVM_BLOCK_TO_PAGE(page_size, block_size, blockno) \
(((block_size) * (blockno)) / (page_size))
/*
* Create mask to clear away address offset.
*/
#define NVM_PAGE_MASK(page_size) \
~((page_size) - 1)
/*
* Round address down to nearest page alignment.
*/
#define NVM_ADDR_MASK(addr, page_size) \
(((uint64_t) (addr)) & NVM_PAGE_MASK((page_size)))
/*
* Align size to page boundary.
*/
#define NVM_PAGE_ALIGN(size, page_size) \
(((size) + (page_size) - 1) & NVM_PAGE_MASK((page_size)))
/*
* Calculate page-aligned offset into address.
*/
#define NVM_ADDR_OFFSET(addr, page_size, pageno) \
(((uint64_t) (addr)) + ((page_size) * (pageno)))
/*
* Calculate page-aligned offset into pointer.
*/
#define NVM_PTR_OFFSET(ptr, page_size, pageno) \
((void*) (((unsigned char*) (ptr)) + ((page_size) * (pageno))))
/*
* Align size to controller pages.
*/
#define NVM_CTRL_ALIGN(ctrl_ptr, size) \
NVM_PAGE_ALIGN((size), (ctrl_ptr)->page_size)
/*
* Convert size to number of controller pages.
*/
#define NVM_CTRL_PAGES(ctrl_ptr, size) \
(NVM_CTRL_ALIGN((ctrl_ptr), (size)) / (ctrl_ptr)->page_size)
/*
* Align size to page size.
*/
#define NVM_DMA_ALIGN(dma_ptr, size) \
NVM_PAGE_ALIGN((size), (dma_ptr)->page_size)
/*
* Calculate controller page-aligned offset into DMA handle pointer.
*/
#define NVM_DMA_OFFSET(dma_ptr, pageno) \
NVM_PTR_OFFSET((dma_ptr)->vaddr, (dma_ptr)->page_size, (pageno))
/*
* Calculate number of pages needed for a
* submission queue (SQ) with a given size.
*/
#define NVM_SQ_PAGES(ctrl_ptr, qs) \
((((uint16_t) ((qs) - 1))* sizeof(nvm_cmd_t)) / (ctrl_ptr)->page_size + 1)
/*
* Calculate number of pages needed for a
* completion queue (CQ) with a given size.
*/
#define NVM_CQ_PAGES(ctrl_ptr, qs) \
((((uint16_t) ((qs) - 1)) * sizeof(nvm_cpl_t)) / (ctrl_ptr)->page_size + 1)
/*
* Number of submission queue entries aligned to a page size.
*/
#define NVM_SQ_SIZE(ctrl_ptr, num_pages) \
((ctrl_ptr)->page_size / sizeof(nvm_cmd_t))
/*
* Number of completion queue entries aligned to a page size.
*/
#define NVM_CQ_SIZE(ctrl_ptr, num_pages) \
((ctrl_ptr)->page_size / sizeof(nvm_cpl_t))
/* Standard fields in a command */
#define NVM_CMD_CID(p) _REG(p, 2, 16)
#define NVM_CMD_NSID(p) _REG(p, 1, 32)
/* Standard fields in a completion */
#define NVM_CPL_CID(p) _REG(p, 12, 16)
#define NVM_CPL_SQHD(p) _REG(p, 8, 16)
#define NVM_CPL_SQID(p) _REG(p, 10, 16)
#define NVM_CPL_SF(p) _REG(p, 14, 16)
#define NVM_CPL_STATUS(p) NVM_CPL_SF(p)
/* Convenience macro for creating a default CID based on submission queue */
#define NVM_DEFAULT_CID(sq) ((uint16_t) ((sq)->tail + (!(sq)->phase) * (sq)->qs))
#ifdef __cplusplus
extern "C" {
#endif
/*
* Get controller associated with admin queue-pair reference.
*/
const nvm_ctrl_t* nvm_ctrl_from_aq_ref(nvm_aq_ref ref);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* Get controller associated with DMA window
*/
const nvm_ctrl_t* nvm_ctrl_from_dma(const nvm_dma_t* dma);
#ifdef __cplusplus
}
#endif
#if defined( __DIS_CLUSTER__ )
#ifdef __cplusplus
extern "C" {
#endif
/*
* Get cluster node identifier from map.
*/
uint32_t nvm_dis_node_from_dma(const nvm_dma_t* dma);
/*
* Get cluster node identifier from controller.
*/
uint32_t nvm_dis_node_from_ctrl(const nvm_ctrl_t* ctrl);
#ifdef __cplusplus
}
#endif
#endif
__forceinline__ __device__ uint32_t lane_id()
{
uint32_t ret;
asm volatile ("mov.u32 %0, %laneid;" : "=r"(ret));
return ret;
}
__forceinline__ __device__ unsigned warp_id()
{
// this is not equal to threadIdx.x / 32
unsigned ret;
asm volatile ("mov.u32 %0, %warpid;" : "=r"(ret));
return ret;
}
__forceinline__ __device__ uint32_t get_smid() {
uint32_t ret;
asm ("mov.u32 %0, %smid;" : "=r"(ret) );
return ret;
}
#endif /* __NVM_UTIL_H__ */