forked from aws-neuron/aws-neuron-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuron_ring.c
More file actions
460 lines (378 loc) · 10.9 KB
/
neuron_ring.c
File metadata and controls
460 lines (378 loc) · 10.9 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2020, Amazon.com, Inc. or its affiliates. All Rights Reserved
*/
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
#include <linux/string.h>
#include <linux/types.h>
#include "udma/udma.h"
#include "v1/address_map.h"
#include "neuron_trace.h"
#include "neuron_device.h"
#include "neuron_dma.h"
#include "neuron_mempool.h"
static struct ndma_eng *ndmar_acquire_engine(struct neuron_device *nd, u32 eng_id)
{
if (eng_id >= NUM_DMA_ENG_PER_DEVICE)
return NULL;
mutex_lock(&nd->ndma_engine[eng_id].lock);
return &nd->ndma_engine[eng_id];
}
static void ndmar_release_engine(struct ndma_eng *eng)
{
mutex_unlock(&eng->nd->ndma_engine[eng->eng_id].lock);
}
static struct ndma_queue *ndmar_get_queue(struct ndma_eng *eng, u32 qid)
{
return &eng->queues[qid];
}
static struct ndma_ring *ndmar_get_ring(struct ndma_queue *queue)
{
return &queue->ring_info;
}
u32 ndmar_ring_get_desc_count(u32 v)
{
if (v < 32) {
return 64;
}
v += UDMA_MAX_NUM_CDESC_PER_CACHE_LINE;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
/**
* ndmar_ring_set_mem_chunk() - Set memory chunk backing the queue's physical memory(descriptor ring buffer)
* @eng: dma engine
* @qid: dma queue id in the engine for which the mc is being set.
* @mc: backing memory chunk
* @port: which axi port(0 or 1) to access the DRAM(for performance)
* @queue_type: type of the queue(rx, tx, or completion)
*/
static void ndmar_ring_set_mem_chunk(struct ndma_eng *eng, u32 qid, struct mem_chunk *mc, u32 port,
enum neuron_dma_queue_type queue_type)
{
struct ndma_queue *queue = ndmar_get_queue(eng, qid);
struct ndma_ring *ring = ndmar_get_ring(queue);
switch (queue_type) {
case NEURON_DMA_QUEUE_TYPE_TX:
ring->tx_mc = mc;
ring->tx.ptr = mc->va;
if (mc->mem_location == MEM_LOC_HOST) {
ring->tx.addr = virt_to_phys(ring->tx.ptr) | PCIEX8_0_BASE;
} else {
ring->tx.addr = mc->pa;
if (port) {
ring->tx.addr |= P_1_BASE;
}
}
break;
case NEURON_DMA_QUEUE_TYPE_RX:
ring->rx_mc = mc;
ring->rx.ptr = mc->va;
if (mc->mem_location == MEM_LOC_HOST) {
ring->rx.addr = virt_to_phys(ring->rx.ptr) | PCIEX8_0_BASE;
} else {
ring->rx.addr = mc->pa;
if (port) {
ring->rx.addr |= P_1_BASE;
}
}
break;
case NEURON_DMA_QUEUE_TYPE_COMPLETION:
ring->has_compl = true;
ring->rxc_mc = mc;
ring->rxc.ptr = mc->va;
if (mc->mem_location == MEM_LOC_HOST) {
ring->rxc.addr = virt_to_phys(ring->rx.ptr) | PCIEX8_0_BASE;
} else {
ring->rxc.addr = mc->pa;
if (port) {
ring->rxc.addr |= P_1_BASE;
}
}
break;
default:
break;
}
}
int ndmar_queue_init(struct neuron_device *nd, u32 eng_id, u32 qid, u32 tx_desc_count,
u32 rx_desc_count, struct mem_chunk *tx_mc, struct mem_chunk *rx_mc,
struct mem_chunk *rxc_mc, u32 port)
{
int ret = -1;
struct ndma_eng *eng;
struct ndma_queue *queue;
struct ndma_ring *ring;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
queue = ndmar_get_queue(eng, qid);
ring = ndmar_get_ring(queue);
queue->eng_id = eng_id;
queue->qid = qid;
ring->qid = qid;
trace_dma_queue_init(nd, eng_id, qid, tx_desc_count, rx_desc_count, tx_mc, rx_mc, rxc_mc,
port);
ndmar_ring_set_mem_chunk(eng, qid, tx_mc, port, NEURON_DMA_QUEUE_TYPE_TX);
ndmar_ring_set_mem_chunk(eng, qid, rx_mc, port, NEURON_DMA_QUEUE_TYPE_RX);
if (rxc_mc)
ndmar_ring_set_mem_chunk(eng, qid, rxc_mc, port, NEURON_DMA_QUEUE_TYPE_COMPLETION);
ret = udma_m2m_init_queue(&eng->udma, qid, tx_desc_count, rx_desc_count, false, &ring->tx,
&ring->rx, rxc_mc != NULL ? &ring->rxc : NULL);
ndmar_release_engine(eng);
return ret;
}
int ndmar_ack_completed(struct neuron_device *nd, u32 eng_id, u32 qid, u32 count)
{
struct ndma_eng *eng;
struct udma_q *rxq, *txq;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
trace_dma_ack_completed(nd, eng_id, qid, count);
udma_q_handle_get(&eng->udma, qid, UDMA_TX, &txq);
udma_q_handle_get(&eng->udma, qid, UDMA_RX, &rxq);
udma_cdesc_ack(rxq, count);
udma_cdesc_ack(txq, count);
ndmar_release_engine(eng);
return 0;
}
int ndmar_queue_copy_start(struct neuron_device *nd, u32 eng_id, u32 qid, u32 tx_desc_count,
u32 rx_desc_count)
{
int ret = -1;
struct ndma_eng *eng;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
trace_dma_queue_copy_start(nd, eng_id, qid, tx_desc_count, rx_desc_count);
ret = udma_m2m_copy_start(&eng->udma, qid, tx_desc_count, rx_desc_count);
if (ret)
pr_err("M2M copy start failed - %d\n", ret);
ndmar_release_engine(eng);
return ret;
}
int ndmar_queue_release(struct neuron_device *nd, u32 eng_id, u32 qid)
{
trace_dma_queue_release(nd, eng_id, qid);
// inf1 does not need any special handling
return 0;
}
int ndmar_h2t_ring_alloc(struct neuron_device *nd, int nc_id)
{
int ret = 0;
struct ndma_eng *eng;
struct ndma_queue *queue;
struct ndma_ring *ring;
int eng_id = DMA_ENG_IDX_H2T + (nc_id * V1_DMA_ENG_PER_NC);
int ndesc = DMA_H2T_DESC_COUNT;
u32 ring_size = ndmar_ring_get_desc_count(ndesc) * sizeof(union udma_desc);
int qid = MAX_DMA_RINGS - 1;
struct mem_chunk *rx_mc = NULL, *tx_mc = NULL;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
eng->used_for_h2t = true;
queue = &eng->queues[MAX_DMA_RINGS - 1];
queue->qid = qid;
queue->eng_id = eng_id;
ring = &queue->ring_info;
ring->qid = qid;
ring->size = ring_size;
ring->has_compl = false;
ret = mc_alloc(&nd->mpset, &rx_mc, ring_size, MEM_LOC_HOST, 0, 0, nc_id);
if (ret) {
pr_err("can't allocate rx queue for H2T - size %d\n", ring_size);
goto error;
}
ret = mc_alloc(&nd->mpset, &tx_mc, ring_size, MEM_LOC_HOST, 0, 0, nc_id);
if (ret) {
pr_err("can't allocate tx queue for H2T - size %d\n", ring_size);
goto error;
}
ndmar_ring_set_mem_chunk(eng, qid, tx_mc, 0, NEURON_DMA_QUEUE_TYPE_TX);
ndmar_ring_set_mem_chunk(eng, qid, rx_mc, 0, NEURON_DMA_QUEUE_TYPE_RX);
mutex_init(&eng->h2t_ring_lock);
ndmar_release_engine(eng);
return 0;
error:
ndmar_release_engine(eng);
if (rx_mc)
mc_free(&rx_mc);
if (tx_mc)
mc_free(&tx_mc);
return ret;
}
static int ndmar_h2t_ring_init(struct ndma_eng *eng)
{
int ret = -1;
struct ndma_queue *queue;
struct ndma_ring *ring;
int qid = MAX_DMA_RINGS - 1;
int ndesc = DMA_H2T_DESC_COUNT;
u32 alloced_desc = ndmar_ring_get_desc_count(ndesc);
queue = &eng->queues[MAX_DMA_RINGS - 1];
ring = &queue->ring_info;
ret = udma_m2m_init_queue(&eng->udma, qid, alloced_desc, alloced_desc, true, &ring->tx,
&ring->rx, NULL);
return ret;
}
int ndmar_eng_set_state(struct neuron_device *nd, int eng_id, u32 state)
{
struct ndma_eng *eng;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
udma_state_set(&eng->udma, state);
ndmar_release_engine(eng);
return 0;
}
int ndmar_eng_get_state(struct neuron_device *nd, int eng_id, struct neuron_dma_eng_state *state)
{
struct ndma_eng *eng;
struct udma *udma;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
udma = &eng->udma;
state->tx_state = udma_state_get(udma, UDMA_TX);
state->rx_state = udma_state_get(udma, UDMA_RX);
state->max_queues = udma->num_of_queues_max;
state->num_queues = udma->num_of_queues;
state->revision_id = udma->rev_id;
ndmar_release_engine(eng);
return 0;
}
int ndmar_queue_get_descriptor_mc(struct neuron_device *nd, u8 eng_id, u8 qid,
struct mem_chunk **tx, struct mem_chunk **rx, u32 *tx_size,
u32 *rx_size)
{
struct ndma_eng *eng;
struct ndma_queue *q;
struct udma *udma;
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
q = &eng->queues[qid];
udma = &eng->udma;
*tx_size = udma->udma_q_m2s[qid].size;
*rx_size = udma->udma_q_s2m[qid].size;
*rx = q->ring_info.rx_mc;
*tx = q->ring_info.tx_mc;
ndmar_release_engine(eng);
return 0;
}
int ndmar_eng_init(struct neuron_device *nd, int eng_id)
{
struct ndma_eng *eng;
char udma_name[UDMA_INSTANCE_NAME_LEN];
int ret = 0;
void __iomem *udma_base[NUM_DMA_ENG_PER_DEVICE];
void __iomem *tdma_base[NUM_DMA_ENG_PER_DEVICE];
const u64 teng_udma_base[] = { P_0_APB_TENG_0_UDMA_0_RELBASE, P_0_APB_TENG_1_UDMA_0_RELBASE,
P_0_APB_TENG_2_UDMA_0_RELBASE,
P_0_APB_TENG_3_UDMA_0_RELBASE };
const u64 teng_tdma_base[] = { P_0_APB_TENG_0_TDMA_0_RELBASE, P_0_APB_TENG_1_TDMA_0_RELBASE,
P_0_APB_TENG_2_TDMA_0_RELBASE,
P_0_APB_TENG_3_TDMA_0_RELBASE };
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
trace_dma_engine_init(nd, eng_id);
udma_base[eng_id] = (void __iomem *)(nd->npdev.bar0) +
teng_udma_base[eng_id / V1_DMA_ENG_PER_NC] +
((eng_id % V1_DMA_ENG_PER_NC) * P_0_APB_TENG_0_UDMA_0_SIZE);
tdma_base[eng_id] = (void __iomem *)(nd->npdev.bar0) +
teng_tdma_base[eng_id / V1_DMA_ENG_PER_NC] +
((eng_id % V1_DMA_ENG_PER_NC) * P_0_APB_TENG_0_TDMA_0_SIZE);
snprintf(udma_name, UDMA_INSTANCE_NAME_LEN, "UDMA_ENG_%d", eng_id);
ret = udma_m2m_init_engine(&eng->udma, udma_base[eng_id], DMA_MAX_Q_MAX, udma_name, 0);
if (ret) {
pr_err("UDMA ENG:%d init failed\n", eng_id);
goto done;
}
ret = tdma_init_engine(tdma_base[eng_id]);
if (ret) {
pr_err("TDMA ENG:%d init failed\n", eng_id);
goto done;
}
if (eng->used_for_h2t) {
// Reinitialize the h2t queue
ret = ndmar_h2t_ring_init(eng);
if (ret)
pr_err("could not reinitialize the h2t queue\n");
}
done:
ndmar_release_engine(eng);
return ret;
}
void ndmar_preinit(struct neuron_device *nd)
{
int i;
struct ndma_eng *eng;
for (i = 0; i < NUM_DMA_ENG_PER_DEVICE; i++) {
eng = &nd->ndma_engine[i];
eng->nd = nd;
eng->eng_id = i;
mutex_init(&eng->lock);
}
}
int ndmar_init(struct neuron_device *nd)
{
int ret = 0;
int nc_id = 0;
for (nc_id = 0; nc_id < V1_NC_PER_DEVICE; nc_id++) {
struct ndma_eng *eng;
int eng_id = DMA_ENG_IDX_H2T + (nc_id * V1_DMA_ENG_PER_NC);
ret = ndmar_h2t_ring_alloc(nd, nc_id);
if (ret) {
pr_err("H2T ring allocation failed - %d\n", ret);
return ret;
}
ret = ndmar_eng_init(nd, eng_id);
if (ret) {
pr_err("H2T engine init failed - %d\n", ret);
return ret;
}
eng = ndmar_acquire_engine(nd, eng_id);
if (eng == NULL)
return -EINVAL;
ret = ndmar_h2t_ring_init(eng);
ndmar_release_engine(eng);
if (ret) {
pr_err("H2T ring init failed - %d\n", ret);
return ret;
}
}
return ret;
}
static void ndmar_h2t_ring_free(struct neuron_device *nd, int eng_id)
{
struct ndma_eng *eng;
struct ndma_queue *queue;
struct ndma_ring *ring;
eng = ndmar_acquire_engine(nd, eng_id);
BUG_ON(eng == NULL);
queue = &eng->queues[MAX_DMA_RINGS - 1];
ring = &queue->ring_info;
if (ring->tx_mc)
mc_free(&ring->tx_mc);
if (ring->rx_mc)
mc_free(&ring->rx_mc);
if (ring->rxc_mc)
mc_free(&ring->rxc_mc);
ndmar_release_engine(eng);
}
void ndmar_close(struct neuron_device *nd)
{
int nc_id;
for (nc_id = 0; nc_id < V1_NC_PER_DEVICE; nc_id++) {
ndmar_h2t_ring_free(nd, DMA_ENG_IDX_H2T + (nc_id * V1_DMA_ENG_PER_NC));
}
return;
}