1 /*
2 * Copyright 2020 Broadcom
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT brcm_iproc_pax_dma_v1
8
9 #include <zephyr/arch/cpu.h>
10 #include <zephyr/cache.h>
11 #include <errno.h>
12 #include <zephyr/init.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/linker/sections.h>
15 #include <soc.h>
16 #include <string.h>
17 #include <zephyr/toolchain.h>
18 #include <zephyr/types.h>
19 #include <zephyr/drivers/dma.h>
20 #include <zephyr/drivers/pcie/endpoint/pcie_ep.h>
21 #include "dma_iproc_pax_v1.h"
22
23 #define LOG_LEVEL CONFIG_DMA_LOG_LEVEL
24 #include <zephyr/logging/log.h>
25 #include <zephyr/irq.h>
26 LOG_MODULE_REGISTER(dma_iproc_pax);
27
28 /* Driver runtime data for PAX DMA and RM */
29 static struct dma_iproc_pax_data pax_dma_data;
30
reset_pkt_id(struct dma_iproc_pax_ring_data * ring)31 static inline uint32_t reset_pkt_id(struct dma_iproc_pax_ring_data *ring)
32 {
33 return ring->pkt_id = 0x0;
34 }
35
36 /**
37 * @brief Opaque/packet id allocator, range 0 to 31
38 */
alloc_pkt_id(struct dma_iproc_pax_ring_data * ring)39 static inline uint32_t alloc_pkt_id(struct dma_iproc_pax_ring_data *ring)
40 {
41 ring->pkt_id = (ring->pkt_id + 1) % 32;
42 return ring->pkt_id;
43 }
44
curr_pkt_id(struct dma_iproc_pax_ring_data * ring)45 static inline uint32_t curr_pkt_id(struct dma_iproc_pax_ring_data *ring)
46 {
47 return ring->pkt_id;
48 }
49
curr_toggle_val(struct dma_iproc_pax_ring_data * ring)50 static inline uint32_t curr_toggle_val(struct dma_iproc_pax_ring_data *ring)
51 {
52 return ring->curr.toggle;
53 }
54
55 /**
56 * @brief Populate header descriptor
57 */
rm_write_header_desc(void * desc,uint32_t toggle,uint32_t opq,uint32_t bdcount)58 static inline void rm_write_header_desc(void *desc, uint32_t toggle,
59 uint32_t opq, uint32_t bdcount)
60 {
61 struct rm_header *r = (struct rm_header *)desc;
62
63 r->opq = opq;
64 /* DMA descriptor count init value */
65 r->bdcount = bdcount;
66 r->prot = 0x0;
67 /* No packet extension, start and end set to '1' */
68 r->start = 1;
69 r->end = 1;
70 r->toggle = toggle;
71 /* RM header type */
72 r->type = PAX_DMA_TYPE_RM_HEADER;
73 }
74
75 /**
76 * @brief Fill RM header descriptor for next transfer
77 * with invalid toggle
78 */
rm_write_header_next_desc(void * desc,struct dma_iproc_pax_ring_data * r,uint32_t opq,uint32_t bdcount)79 static inline void rm_write_header_next_desc(void *desc,
80 struct dma_iproc_pax_ring_data *r,
81 uint32_t opq, uint32_t bdcount)
82 {
83 /* Toggle bit is invalid until next payload configured */
84 rm_write_header_desc(desc, (r->curr.toggle == 0) ? 1 : 0, opq, bdcount);
85 }
86
rm_header_set_bd_count(void * desc,uint32_t bdcount)87 static inline void rm_header_set_bd_count(void *desc, uint32_t bdcount)
88 {
89 struct rm_header *r = (struct rm_header *)desc;
90
91 /* DMA descriptor count */
92 r->bdcount = bdcount;
93 }
94
rm_header_set_toggle(void * desc,uint32_t toggle)95 static inline void rm_header_set_toggle(void *desc, uint32_t toggle)
96 {
97 struct rm_header *r = (struct rm_header *)desc;
98
99 r->toggle = toggle;
100 }
101
102 /**
103 * @brief Populate dma header descriptor
104 */
rm_write_dma_header_desc(void * desc,struct dma_iproc_pax_payload * pl)105 static inline void rm_write_dma_header_desc(void *desc,
106 struct dma_iproc_pax_payload *pl)
107 {
108 struct dma_header_desc *hdr = (struct dma_header_desc *)desc;
109
110 hdr->length = pl->xfer_sz;
111 hdr->opcode = pl->direction;
112 /* DMA header type */
113 hdr->type = PAX_DMA_TYPE_DMA_DESC;
114 }
115
116 /**
117 * @brief Populate axi address descriptor
118 */
rm_write_axi_addr_desc(void * desc,struct dma_iproc_pax_payload * pl)119 static inline void rm_write_axi_addr_desc(void *desc,
120 struct dma_iproc_pax_payload *pl)
121 {
122 struct axi_addr_desc *axi = (struct axi_addr_desc *)desc;
123
124 axi->axi_addr = pl->axi_addr;
125 axi->type = PAX_DMA_TYPE_DMA_DESC;
126
127 }
128
129 /**
130 * @brief Populate pci address descriptor
131 */
rm_write_pci_addr_desc(void * desc,struct dma_iproc_pax_payload * pl)132 static inline void rm_write_pci_addr_desc(void *desc,
133 struct dma_iproc_pax_payload *pl)
134 {
135 struct pci_addr_desc *pci = (struct pci_addr_desc *)desc;
136
137 pci->pcie_addr = pl->pci_addr >> PAX_DMA_PCI_ADDR_ALIGNMT_SHIFT;
138 pci->type = PAX_DMA_TYPE_DMA_DESC;
139 }
140
141 /**
142 * @brief Return's pointer to the descriptor memory to be written next,
143 * skip next pointer descriptor address.
144 */
next_desc_addr(struct dma_iproc_pax_ring_data * ring)145 static void *next_desc_addr(struct dma_iproc_pax_ring_data *ring)
146 {
147 struct next_ptr_desc *nxt;
148 uintptr_t curr;
149
150 curr = (uintptr_t)ring->curr.write_ptr + PAX_DMA_RM_DESC_BDWIDTH;
151 /* if hit next table ptr, skip to next location, flip toggle */
152 nxt = (struct next_ptr_desc *)curr;
153 if (nxt->type == PAX_DMA_TYPE_NEXT_PTR) {
154 LOG_DBG("hit next_ptr@0x%lx:T%d, next_table@0x%lx\n",
155 curr, nxt->toggle, (uintptr_t)nxt->addr);
156 uintptr_t last = (uintptr_t)ring->bd +
157 PAX_DMA_RM_DESC_RING_SIZE * PAX_DMA_NUM_BD_BUFFS;
158 ring->curr.toggle = (ring->curr.toggle == 0) ? 1 : 0;
159 /* move to next addr, wrap around if hits end */
160 curr += PAX_DMA_RM_DESC_BDWIDTH;
161 if (curr == last) {
162 curr = (uintptr_t)ring->bd;
163 LOG_DBG("hit end of desc:0x%lx, wrap to 0x%lx\n",
164 last, curr);
165 }
166 }
167 ring->curr.write_ptr = (void *)curr;
168 return (void *)curr;
169 }
170
171 /**
172 * @brief Populate next ptr descriptor
173 */
rm_write_next_table_desc(void * desc,void * next_ptr,uint32_t toggle)174 static void rm_write_next_table_desc(void *desc, void *next_ptr,
175 uint32_t toggle)
176 {
177 struct next_ptr_desc *nxt = (struct next_ptr_desc *)desc;
178
179 nxt->addr = (uintptr_t)next_ptr;
180 nxt->type = PAX_DMA_TYPE_NEXT_PTR;
181 nxt->toggle = toggle;
182 }
183
prepare_ring(struct dma_iproc_pax_ring_data * ring)184 static void prepare_ring(struct dma_iproc_pax_ring_data *ring)
185 {
186 uintptr_t curr, next, last;
187 uint32_t toggle;
188 int buff_count = PAX_DMA_NUM_BD_BUFFS;
189
190 /* zero out descriptor area */
191 memset(ring->bd, 0x0, PAX_DMA_RM_DESC_RING_SIZE * PAX_DMA_NUM_BD_BUFFS);
192 memset(ring->cmpl, 0x0, PAX_DMA_RM_CMPL_RING_SIZE);
193
194 /* opaque/packet id value */
195 rm_write_header_desc(ring->bd, 0x0, reset_pkt_id(ring),
196 PAX_DMA_RM_DESC_BDCOUNT);
197 /* start with first buffer, valid toggle is 0x1 */
198 toggle = 0x1;
199 curr = (uintptr_t)ring->bd;
200 next = curr + PAX_DMA_RM_DESC_RING_SIZE;
201 last = curr + PAX_DMA_RM_DESC_RING_SIZE * PAX_DMA_NUM_BD_BUFFS;
202 do {
203 /* Place next_table desc as last BD entry on each buffer */
204 rm_write_next_table_desc(PAX_DMA_NEXT_TBL_ADDR((void *)curr),
205 (void *)next, toggle);
206
207 /* valid toggle flips for each buffer */
208 toggle = toggle ? 0x0 : 0x1;
209 curr += PAX_DMA_RM_DESC_RING_SIZE;
210 next += PAX_DMA_RM_DESC_RING_SIZE;
211 /* last entry, chain back to first buffer */
212 if (next == last) {
213 next = (uintptr_t)ring->bd;
214 }
215
216 } while (--buff_count);
217
218 dma_mb();
219
220 /* start programming from first RM header */
221 ring->curr.write_ptr = ring->bd;
222 /* valid toggle starts with 1 after reset */
223 ring->curr.toggle = 1;
224 /* completion read offset */
225 ring->curr.cmpl_rd_offs = 0;
226
227 /* init sync data for the ring */
228 ring->curr.sync_data.signature = PAX_DMA_WRITE_SYNC_SIGNATURE;
229 ring->curr.sync_data.ring = ring->idx;
230 /* pkt id for active dma xfer */
231 ring->curr.sync_data.opaque = 0x0;
232 /* pkt count for active dma xfer */
233 ring->curr.sync_data.total_pkts = 0x0;
234 }
235
init_rm(struct dma_iproc_pax_data * pd)236 static int init_rm(struct dma_iproc_pax_data *pd)
237 {
238 int ret = -ETIMEDOUT, timeout = 1000;
239
240 k_mutex_lock(&pd->dma_lock, K_FOREVER);
241 /* Wait for Ring Manager ready */
242 do {
243 LOG_DBG("Waiting for RM HW init\n");
244 if ((sys_read32(RM_COMM_REG(pd, RM_COMM_MAIN_HW_INIT_DONE)) &
245 RM_COMM_MAIN_HW_INIT_DONE_MASK)) {
246 ret = 0;
247 break;
248 }
249 k_sleep(K_MSEC(1));
250 } while (--timeout);
251 k_mutex_unlock(&pd->dma_lock);
252
253 if (!timeout) {
254 LOG_WRN("RM HW Init timedout!\n");
255 } else {
256 LOG_INF("PAX DMA RM HW Init Done\n");
257 }
258
259 return ret;
260 }
261
rm_cfg_start(struct dma_iproc_pax_data * pd)262 static void rm_cfg_start(struct dma_iproc_pax_data *pd)
263 {
264 uint32_t val;
265
266 k_mutex_lock(&pd->dma_lock, K_FOREVER);
267
268 /* set config done 0, enable toggle mode */
269 val = sys_read32(RM_COMM_REG(pd, RM_COMM_CONTROL));
270 val &= ~RM_COMM_CONTROL_CONFIG_DONE;
271 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CONTROL));
272
273 val &= ~(RM_COMM_CONTROL_MODE_MASK << RM_COMM_CONTROL_MODE_SHIFT);
274 val |= (RM_COMM_CONTROL_MODE_TOGGLE << RM_COMM_CONTROL_MODE_SHIFT);
275 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CONTROL));
276
277 /* Disable MSI */
278 sys_write32(RM_COMM_MSI_DISABLE_VAL,
279 RM_COMM_REG(pd, RM_COMM_MSI_DISABLE));
280 /* Enable Line interrupt */
281 val = sys_read32(RM_COMM_REG(pd, RM_COMM_CONTROL));
282 val |= RM_COMM_CONTROL_LINE_INTR_EN;
283 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CONTROL));
284
285 /* Enable AE_TIMEOUT */
286 sys_write32(RM_COMM_AE_TIMEOUT_VAL, RM_COMM_REG(pd,
287 RM_COMM_AE_TIMEOUT));
288 val = sys_read32(RM_COMM_REG(pd, RM_COMM_CONTROL));
289 val |= RM_COMM_CONTROL_AE_TIMEOUT_EN;
290 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CONTROL));
291
292 /* AE (Acceleration Engine) grouping to group '0' */
293 val = sys_read32(RM_COMM_REG(pd, RM_AE0_AE_CONTROL));
294 val &= ~RM_AE_CTRL_AE_GROUP_MASK;
295 sys_write32(val, RM_COMM_REG(pd, RM_AE0_AE_CONTROL));
296 val |= RM_AE_CONTROL_ACTIVE;
297 sys_write32(val, RM_COMM_REG(pd, RM_AE0_AE_CONTROL));
298
299 /* AXI read/write channel enable */
300 val = sys_read32(RM_COMM_REG(pd, RM_COMM_AXI_CONTROL));
301 val |= (RM_COMM_AXI_CONTROL_RD_CH_EN | RM_COMM_AXI_CONTROL_WR_CH_EN);
302 sys_write32(val, RM_COMM_REG(pd, RM_COMM_AXI_CONTROL));
303
304 /* Tune RM control programming for 4 rings */
305 sys_write32(RM_COMM_TIMER_CONTROL0_VAL,
306 RM_COMM_REG(pd, RM_COMM_TIMER_CONTROL_0));
307 sys_write32(RM_COMM_TIMER_CONTROL1_VAL,
308 RM_COMM_REG(pd, RM_COMM_TIMER_CONTROL_1));
309 sys_write32(RM_COMM_RM_BURST_LENGTH,
310 RM_COMM_REG(pd, RM_COMM_RM_BURST_LENGTH));
311
312 /* Set Sequence max count to the max supported value */
313 val = sys_read32(RM_COMM_REG(pd, RM_COMM_MASK_SEQUENCE_MAX_COUNT));
314 val = (val | RING_MASK_SEQ_MAX_COUNT_MASK);
315 sys_write32(val, RM_COMM_REG(pd, RM_COMM_MASK_SEQUENCE_MAX_COUNT));
316
317 k_mutex_unlock(&pd->dma_lock);
318 }
319
rm_ring_clear_stats(struct dma_iproc_pax_data * pd,enum ring_idx idx)320 static void rm_ring_clear_stats(struct dma_iproc_pax_data *pd,
321 enum ring_idx idx)
322 {
323 /* Read ring Tx, Rx, and Outstanding counts to clear */
324 sys_read32(RM_RING_REG(pd, idx, RING_NUM_REQ_RECV_LS));
325 sys_read32(RM_RING_REG(pd, idx, RING_NUM_REQ_RECV_MS));
326 sys_read32(RM_RING_REG(pd, idx, RING_NUM_REQ_TRANS_LS));
327 sys_read32(RM_RING_REG(pd, idx, RING_NUM_REQ_TRANS_MS));
328 sys_read32(RM_RING_REG(pd, idx, RING_NUM_REQ_OUTSTAND));
329 }
330
rm_cfg_finish(struct dma_iproc_pax_data * pd)331 static void rm_cfg_finish(struct dma_iproc_pax_data *pd)
332 {
333 uint32_t val;
334
335 k_mutex_lock(&pd->dma_lock, K_FOREVER);
336
337 /* set Ring config done */
338 val = sys_read32(RM_COMM_REG(pd, RM_COMM_CONTROL));
339 val |= RM_COMM_CONTROL_CONFIG_DONE;
340 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CONTROL));
341
342 k_mutex_unlock(&pd->dma_lock);
343 }
344
345 /* Activate/Deactivate rings */
set_ring_active(struct dma_iproc_pax_data * pd,enum ring_idx idx,bool active)346 static inline void set_ring_active(struct dma_iproc_pax_data *pd,
347 enum ring_idx idx,
348 bool active)
349 {
350 uint32_t val;
351
352 val = sys_read32(RM_RING_REG(pd, idx, RING_CONTROL));
353 if (active) {
354 val |= RING_CONTROL_ACTIVE;
355 } else {
356 val &= ~RING_CONTROL_ACTIVE;
357 }
358 sys_write32(val, RM_RING_REG(pd, idx, RING_CONTROL));
359 }
360
init_ring(struct dma_iproc_pax_data * pd,enum ring_idx idx)361 static int init_ring(struct dma_iproc_pax_data *pd, enum ring_idx idx)
362 {
363 uint32_t val;
364 uintptr_t desc = (uintptr_t)pd->ring[idx].bd;
365 uintptr_t cmpl = (uintptr_t)pd->ring[idx].cmpl;
366 int timeout = 5000, ret = 0;
367
368 k_mutex_lock(&pd->dma_lock, K_FOREVER);
369
370 /* Read cmpl write ptr incase previous dma stopped */
371 sys_read32(RM_RING_REG(pd, idx, RING_CMPL_WRITE_PTR));
372
373 /* Inactivate ring */
374 sys_write32(0x0, RM_RING_REG(pd, idx, RING_CONTROL));
375
376 /* Flush ring before loading new descriptor */
377 sys_write32(RING_CONTROL_FLUSH, RM_RING_REG(pd, idx, RING_CONTROL));
378 do {
379 if (sys_read32(RM_RING_REG(pd, idx, RING_FLUSH_DONE)) &
380 RING_FLUSH_DONE_MASK) {
381 break;
382 }
383 k_busy_wait(1);
384 } while (--timeout);
385
386 if (!timeout) {
387 LOG_WRN("Ring %d flush timedout!\n", idx);
388 ret = -ETIMEDOUT;
389 goto err;
390 }
391
392 /* clear ring after flush */
393 sys_write32(0x0, RM_RING_REG(pd, idx, RING_CONTROL));
394
395 /* ring group id set to '0' */
396 val = sys_read32(RM_COMM_REG(pd, RM_COMM_CTRL_REG(idx)));
397 val &= ~RING_COMM_CTRL_AE_GROUP_MASK;
398 sys_write32(val, RM_COMM_REG(pd, RM_COMM_CTRL_REG(idx)));
399
400 /* DDR update control, set timeout value */
401 val = RING_DDR_CONTROL_COUNT(RING_DDR_CONTROL_COUNT_VAL) |
402 RING_DDR_CONTROL_TIMER(RING_DDR_CONTROL_TIMER_VAL) |
403 RING_DDR_CONTROL_ENABLE;
404
405 sys_write32(val, RM_RING_REG(pd, idx, RING_CMPL_WR_PTR_DDR_CONTROL));
406
407 val = (uint32_t)((uintptr_t)desc >> PAX_DMA_RING_BD_ALIGN_ORDER);
408 sys_write32(val, RM_RING_REG(pd, idx, RING_BD_START_ADDR));
409 val = (uint32_t)((uintptr_t)cmpl >> PAX_DMA_RING_CMPL_ALIGN_ORDER);
410 sys_write32(val, RM_RING_REG(pd, idx, RING_CMPL_START_ADDR));
411 val = sys_read32(RM_RING_REG(pd, idx, RING_BD_READ_PTR));
412
413 /* keep ring inactive after init to avoid BD poll */
414 set_ring_active(pd, idx, false);
415 rm_ring_clear_stats(pd, idx);
416 err:
417 k_mutex_unlock(&pd->dma_lock);
418
419 return ret;
420 }
421
poll_on_write_sync(const struct device * dev,struct dma_iproc_pax_ring_data * ring)422 static int poll_on_write_sync(const struct device *dev,
423 struct dma_iproc_pax_ring_data *ring)
424 {
425 const struct dma_iproc_pax_cfg *cfg = dev->config;
426 struct dma_iproc_pax_write_sync_data sync_rd, *recv, *sent;
427 uint64_t pci_addr;
428 uint32_t *pci32, *axi32;
429 uint32_t zero_init = 0, timeout = PAX_DMA_MAX_SYNC_WAIT;
430 int ret;
431
432 recv = &sync_rd;
433 sent = &(ring->curr.sync_data);
434 /* form host pci sync address */
435 pci32 = (uint32_t *)&pci_addr;
436 pci32[0] = ring->sync_pci.addr_lo;
437 pci32[1] = ring->sync_pci.addr_hi;
438 axi32 = (uint32_t *)&sync_rd;
439
440 do {
441 ret = pcie_ep_xfer_data_memcpy(cfg->pcie_dev, pci_addr,
442 (uintptr_t *)axi32, 4,
443 PCIE_OB_LOWMEM, HOST_TO_DEVICE);
444
445 if (memcmp((void *)recv, (void *)sent, 4) == 0) {
446 /* clear the sync word */
447 ret = pcie_ep_xfer_data_memcpy(cfg->pcie_dev, pci_addr,
448 (uintptr_t *)&zero_init,
449 4, PCIE_OB_LOWMEM,
450 DEVICE_TO_HOST);
451 dma_mb();
452 ret = 0;
453 break;
454 }
455 k_busy_wait(1);
456 } while (--timeout);
457
458 if (!timeout) {
459 LOG_DBG("[ring %d]: not recvd write sync!\n", ring->idx);
460 ret = -ETIMEDOUT;
461 }
462
463 return ret;
464 }
465
process_cmpl_event(const struct device * dev,enum ring_idx idx,uint32_t pl_len)466 static int process_cmpl_event(const struct device *dev,
467 enum ring_idx idx, uint32_t pl_len)
468 {
469 struct dma_iproc_pax_data *pd = dev->data;
470 uint32_t wr_offs, rd_offs, ret = DMA_STATUS_COMPLETE;
471 struct dma_iproc_pax_ring_data *ring = &(pd->ring[idx]);
472 struct cmpl_pkt *c;
473 uint32_t is_outstanding;
474
475 /* cmpl read offset, unprocessed cmpl location */
476 rd_offs = ring->curr.cmpl_rd_offs;
477
478 wr_offs = sys_read32(RM_RING_REG(pd, idx,
479 RING_CMPL_WRITE_PTR));
480
481 /* Update read ptr to "processed" */
482 ring->curr.cmpl_rd_offs = wr_offs;
483
484 /*
485 * Ensure consistency of completion descriptor
486 * The completion desc is updated by RM via AXI stream
487 * CPU need to ensure the memory operations are completed
488 * before reading cmpl area, by a "dsb"
489 * If Dcache enabled, need to invalidate the cachelines to
490 * read updated cmpl desc. The cache API also issues dsb.
491 */
492 dma_mb();
493
494 /* Decode cmpl pkt id to verify */
495 c = (struct cmpl_pkt *)((uintptr_t)ring->cmpl +
496 PAX_DMA_CMPL_DESC_SIZE * PAX_DMA_CURR_CMPL_IDX(wr_offs));
497
498 LOG_DBG("RING%d WR_PTR:%d opq:%d, rm_status:%x dma_status:%x\n",
499 idx, wr_offs, c->opq, c->rm_status, c->dma_status);
500
501 is_outstanding = sys_read32(RM_RING_REG(pd, idx,
502 RING_NUM_REQ_OUTSTAND));
503 if ((ring->curr.opq != c->opq) && (is_outstanding != 0)) {
504 LOG_ERR("RING%d: pkt id should be %d, rcvd %d outst=%d\n",
505 idx, ring->curr.opq, c->opq, is_outstanding);
506 ret = -EIO;
507 }
508 /* check for completion AE timeout */
509 if (c->rm_status == RM_COMPLETION_AE_TIMEOUT) {
510 LOG_ERR("RING%d WR_PTR:%d rm_status:%x AE Timeout!\n",
511 idx, wr_offs, c->rm_status);
512 /* TBD: Issue full card reset to restore operations */
513 LOG_ERR("Needs Card Reset to recover!\n");
514 ret = -ETIMEDOUT;
515 }
516
517 if (ring->dma_callback) {
518 ring->dma_callback(dev, ring->callback_arg, idx, ret);
519 }
520
521 return ret;
522 }
523
524 #ifdef CONFIG_DMA_IPROC_PAX_POLL_MODE
peek_ring_cmpl(const struct device * dev,enum ring_idx idx,uint32_t pl_len)525 static int peek_ring_cmpl(const struct device *dev,
526 enum ring_idx idx, uint32_t pl_len)
527 {
528 struct dma_iproc_pax_data *pd = dev->data;
529 uint32_t wr_offs, rd_offs, timeout = PAX_DMA_MAX_POLL_WAIT;
530 struct dma_iproc_pax_ring_data *ring = &(pd->ring[idx]);
531
532 /* cmpl read offset, unprocessed cmpl location */
533 rd_offs = ring->curr.cmpl_rd_offs;
534
535 /* poll write_ptr until cmpl received for all buffers */
536 do {
537 wr_offs = sys_read32(RM_RING_REG(pd, idx,
538 RING_CMPL_WRITE_PTR));
539 if (PAX_DMA_GET_CMPL_COUNT(wr_offs, rd_offs) >= pl_len)
540 break;
541 k_busy_wait(1);
542 } while (--timeout);
543
544 if (timeout == 0) {
545 LOG_ERR("RING%d timeout, rcvd %d, expected %d!\n",
546 idx, PAX_DMA_GET_CMPL_COUNT(wr_offs, rd_offs), pl_len);
547 /* More debug info on current dma instance */
548 LOG_ERR("WR_PTR:%x RD_PTR%x\n", wr_offs, rd_offs);
549 return -ETIMEDOUT;
550 }
551
552 return process_cmpl_event(dev, idx, pl_len);
553 }
554 #else
rm_isr(const struct device * dev)555 static void rm_isr(const struct device *dev)
556 {
557 uint32_t status, err_stat, idx;
558 struct dma_iproc_pax_data *pd = dev->data;
559
560 /* read and clear interrupt status */
561 status = sys_read32(RM_COMM_REG(pd, RM_COMM_MSI_INTR_INTERRUPT_STATUS));
562 sys_write32(status, RM_COMM_REG(pd,
563 RM_COMM_MSI_INTERRUPT_STATUS_CLEAR));
564
565 /* read and clear DME/AE error interrupts */
566 err_stat = sys_read32(RM_COMM_REG(pd,
567 RM_COMM_DME_INTERRUPT_STATUS_MASK));
568 sys_write32(err_stat,
569 RM_COMM_REG(pd, RM_COMM_DME_INTERRUPT_STATUS_CLEAR));
570 err_stat =
571 sys_read32(RM_COMM_REG(pd,
572 RM_COMM_AE_INTERFACE_GROUP_0_INTERRUPT_MASK));
573 sys_write32(err_stat,
574 RM_COMM_REG(pd,
575 RM_COMM_AE_INTERFACE_GROUP_0_INTERRUPT_CLEAR));
576
577 /* alert waiting thread to process, for each completed ring */
578 for (idx = PAX_DMA_RING0; idx < PAX_DMA_RINGS_MAX; idx++) {
579 if (status & (0x1 << idx)) {
580 k_sem_give(&pd->ring[idx].alert);
581 }
582 }
583 }
584 #endif
585
dma_iproc_pax_init(const struct device * dev)586 static int dma_iproc_pax_init(const struct device *dev)
587 {
588 const struct dma_iproc_pax_cfg *cfg = dev->config;
589 struct dma_iproc_pax_data *pd = dev->data;
590 int r;
591 uintptr_t mem_aligned;
592
593 if (!device_is_ready(cfg->pcie_dev)) {
594 LOG_ERR("PCIe device not ready");
595 return -ENODEV;
596 }
597
598 pd->dma_base = cfg->dma_base;
599 pd->rm_comm_base = cfg->rm_comm_base;
600 pd->used_rings = (cfg->use_rings < PAX_DMA_RINGS_MAX) ?
601 cfg->use_rings : PAX_DMA_RINGS_MAX;
602
603 LOG_DBG("dma base:0x%x, rm comm base:0x%x, needed rings %d\n",
604 pd->dma_base, pd->rm_comm_base, pd->used_rings);
605
606 /* dma/rm access lock */
607 k_mutex_init(&pd->dma_lock);
608
609 /* Ring Manager H/W init */
610 if (init_rm(pd)) {
611 return -ETIMEDOUT;
612 }
613
614 /* common rm config */
615 rm_cfg_start(pd);
616
617 /* individual ring config */
618 for (r = 0; r < pd->used_rings; r++) {
619 /* per-ring mutex lock */
620 k_mutex_init(&pd->ring[r].lock);
621 /* Init alerts */
622 k_sem_init(&pd->ring[r].alert, 0, 1);
623
624 pd->ring[r].idx = r;
625 pd->ring[r].ring_base = cfg->rm_base +
626 PAX_DMA_RING_ADDR_OFFSET(r);
627 LOG_DBG("RING%d,VERSION:0x%x\n", pd->ring[r].idx,
628 sys_read32(RM_RING_REG(pd, r, RING_VER)));
629
630 /* Allocate for 2 BD buffers + cmpl buffer + payload struct */
631 pd->ring[r].ring_mem = (void *)((uintptr_t)cfg->bd_memory_base +
632 r *
633 PAX_DMA_PER_RING_ALLOC_SIZE);
634 if (!pd->ring[r].ring_mem) {
635 LOG_ERR("RING%d failed to alloc desc memory!\n", r);
636 return -ENOMEM;
637 }
638 /* Find 8K aligned address within allocated region */
639 mem_aligned = ((uintptr_t)pd->ring[r].ring_mem +
640 PAX_DMA_RING_ALIGN - 1) &
641 ~(PAX_DMA_RING_ALIGN - 1);
642
643 pd->ring[r].cmpl = (void *)mem_aligned;
644 pd->ring[r].bd = (void *)(mem_aligned +
645 PAX_DMA_RM_CMPL_RING_SIZE);
646 pd->ring[r].payload = (void *)((uintptr_t)pd->ring[r].bd +
647 PAX_DMA_RM_DESC_RING_SIZE *
648 PAX_DMA_NUM_BD_BUFFS);
649
650 LOG_DBG("Ring%d,allocated Mem:0x%p Size %d\n",
651 pd->ring[r].idx,
652 pd->ring[r].ring_mem,
653 PAX_DMA_PER_RING_ALLOC_SIZE);
654 LOG_DBG("Ring%d,BD:0x%p, CMPL:0x%p, PL:0x%p\n",
655 pd->ring[r].idx,
656 pd->ring[r].bd,
657 pd->ring[r].cmpl,
658 pd->ring[r].payload);
659
660 /* Prepare ring desc table */
661 prepare_ring(&(pd->ring[r]));
662
663 /* initialize ring */
664 init_ring(pd, r);
665 }
666
667 /* set ring config done */
668 rm_cfg_finish(pd);
669
670 #ifndef CONFIG_DMA_IPROC_PAX_POLL_MODE
671 /* Register and enable RM interrupt */
672 IRQ_CONNECT(DT_INST_IRQN(0),
673 DT_INST_IRQ(0, priority),
674 rm_isr,
675 DEVICE_DT_INST_GET(0),
676 0);
677 irq_enable(DT_INST_IRQN(0));
678 #else
679 LOG_INF("%s PAX DMA rings in poll mode!\n", dev->name);
680 #endif
681 LOG_INF("%s RM setup %d rings\n", dev->name, pd->used_rings);
682
683 return 0;
684 }
685
686 #ifdef CONFIG_DMA_IPROC_PAX_POLL_MODE
set_pkt_count(const struct device * dev,enum ring_idx idx,uint32_t pl_len)687 static void set_pkt_count(const struct device *dev,
688 enum ring_idx idx,
689 uint32_t pl_len)
690 {
691 /* Nothing needs to be programmed here in poll mode */
692 }
693
wait_for_pkt_completion(const struct device * dev,enum ring_idx idx,uint32_t pl_len)694 static int wait_for_pkt_completion(const struct device *dev,
695 enum ring_idx idx,
696 uint32_t pl_len)
697 {
698 /* poll for completion */
699 return peek_ring_cmpl(dev, idx, pl_len + 1);
700 }
701 #else
set_pkt_count(const struct device * dev,enum ring_idx idx,uint32_t pl_len)702 static void set_pkt_count(const struct device *dev,
703 enum ring_idx idx,
704 uint32_t pl_len)
705 {
706 struct dma_iproc_pax_data *pd = dev->data;
707 uint32_t val;
708
709 /* program packet count for interrupt assertion */
710 val = sys_read32(RM_RING_REG(pd, idx,
711 RING_CMPL_WR_PTR_DDR_CONTROL));
712 val &= ~RING_DDR_CONTROL_COUNT_MASK;
713 val |= RING_DDR_CONTROL_COUNT(pl_len);
714 sys_write32(val, RM_RING_REG(pd, idx,
715 RING_CMPL_WR_PTR_DDR_CONTROL));
716 }
717
wait_for_pkt_completion(const struct device * dev,enum ring_idx idx,uint32_t pl_len)718 static int wait_for_pkt_completion(const struct device *dev,
719 enum ring_idx idx,
720 uint32_t pl_len)
721 {
722 struct dma_iproc_pax_data *pd = dev->data;
723 struct dma_iproc_pax_ring_data *ring;
724
725 ring = &(pd->ring[idx]);
726
727 /* wait for sg dma completion alert */
728 if (k_sem_take(&ring->alert, K_MSEC(PAX_DMA_TIMEOUT)) != 0) {
729 LOG_ERR("PAX DMA [ring %d] Timeout!\n", idx);
730 return -ETIMEDOUT;
731 }
732
733 return process_cmpl_event(dev, idx, pl_len);
734 }
735 #endif
736
dma_iproc_pax_do_xfer(const struct device * dev,enum ring_idx idx,struct dma_iproc_pax_payload * pl,uint32_t pl_len)737 static int dma_iproc_pax_do_xfer(const struct device *dev,
738 enum ring_idx idx,
739 struct dma_iproc_pax_payload *pl,
740 uint32_t pl_len)
741 {
742 struct dma_iproc_pax_data *pd = dev->data;
743 const struct dma_iproc_pax_cfg *cfg = dev->config;
744 int ret = 0, cnt;
745 struct dma_iproc_pax_ring_data *ring;
746 void *hdr;
747 uint32_t toggle_bit;
748 struct dma_iproc_pax_payload sync_pl;
749 struct dma_iproc_pax_addr64 sync;
750
751 ring = &(pd->ring[idx]);
752 pl = ring->payload;
753
754 /*
755 * Host sync buffer isn't ready at zephyr/driver init-time
756 * Read the host address location once at first DMA write
757 * on that ring.
758 */
759 if ((ring->sync_pci.addr_lo == 0x0) &&
760 (ring->sync_pci.addr_hi == 0x0)) {
761 /* populate sync data location */
762 LOG_DBG("sync addr loc 0x%x\n", cfg->scr_addr_loc);
763
764 sync.addr_lo = sys_read32(cfg->scr_addr_loc
765 + 4);
766 sync.addr_hi = sys_read32(cfg->scr_addr_loc);
767 ring->sync_pci.addr_lo = sync.addr_lo + idx * 4;
768 ring->sync_pci.addr_hi = sync.addr_hi;
769 LOG_DBG("ring:%d,sync addr:0x%x.0x%x\n", idx,
770 ring->sync_pci.addr_hi,
771 ring->sync_pci.addr_lo);
772 }
773
774 /* account extra sync packet */
775 ring->curr.sync_data.opaque = ring->curr.opq;
776 ring->curr.sync_data.total_pkts = pl_len;
777 memcpy((void *)&ring->sync_loc,
778 (void *)&(ring->curr.sync_data), 4);
779 sync_pl.pci_addr = ring->sync_pci.addr_lo |
780 (uint64_t)ring->sync_pci.addr_hi << 32;
781 sync_pl.axi_addr = (uintptr_t)&ring->sync_loc;
782
783 sync_pl.xfer_sz = 4; /* 4-bytes */
784 sync_pl.direction = CARD_TO_HOST;
785
786 /* Get descriptor write pointer for first header */
787 hdr = (void *)ring->curr.write_ptr;
788 /* current toggle bit */
789 toggle_bit = ring->curr.toggle;
790 /* current opq value for cmpl check */
791 ring->curr.opq = curr_pkt_id(ring);
792
793 /* DMA desc count for first payload */
794 rm_header_set_bd_count(hdr, PAX_DMA_RM_DESC_BDCOUNT);
795
796 /* Form dma descriptors for total sg payload */
797 for (cnt = 0; cnt < pl_len; cnt++) {
798 rm_write_dma_header_desc(next_desc_addr(ring), pl + cnt);
799 rm_write_axi_addr_desc(next_desc_addr(ring), pl + cnt);
800 rm_write_pci_addr_desc(next_desc_addr(ring), pl + cnt);
801 /* Toggle may flip, program updated toggle value */
802 rm_write_header_desc(next_desc_addr(ring),
803 curr_toggle_val(ring),
804 curr_pkt_id(ring),
805 PAX_DMA_RM_DESC_BDCOUNT);
806 }
807
808 /* Append write sync payload descriptors */
809 rm_write_dma_header_desc(next_desc_addr(ring), &sync_pl);
810 rm_write_axi_addr_desc(next_desc_addr(ring), &sync_pl);
811 rm_write_pci_addr_desc(next_desc_addr(ring), &sync_pl);
812
813 /* RM header for next transfer, RM wait on (invalid) toggle bit */
814 rm_write_header_next_desc(next_desc_addr(ring), ring, alloc_pkt_id(ring),
815 PAX_DMA_RM_DESC_BDCOUNT);
816
817 set_pkt_count(dev, idx, pl_len + 1);
818
819 /* Ensure memory write before toggle flip */
820 dma_mb();
821
822 /* set toggle to valid in first header */
823 rm_header_set_toggle(hdr, toggle_bit);
824 /* activate the ring */
825 set_ring_active(pd, idx, true);
826
827 ret = wait_for_pkt_completion(dev, idx, pl_len + 1);
828 if (ret) {
829 goto err_ret;
830 }
831
832 ret = poll_on_write_sync(dev, ring);
833 k_mutex_lock(&ring->lock, K_FOREVER);
834 ring->ring_active = 0;
835 k_mutex_unlock(&ring->lock);
836
837 err_ret:
838 ring->ring_active = 0;
839 /* deactivate the ring until next active transfer */
840 set_ring_active(pd, idx, false);
841
842 return ret;
843 }
844
dma_iproc_pax_configure(const struct device * dev,uint32_t channel,struct dma_config * cfg)845 static int dma_iproc_pax_configure(const struct device *dev, uint32_t channel,
846 struct dma_config *cfg)
847 {
848 struct dma_iproc_pax_data *pd = dev->data;
849 struct dma_iproc_pax_ring_data *ring;
850 uint32_t xfer_sz;
851 int ret = 0;
852 #ifdef CONFIG_DMA_IPROC_PAX_DEBUG
853 uint32_t *pci_addr32;
854 uint32_t *axi_addr32;
855 #endif
856
857 if (channel >= PAX_DMA_RINGS_MAX) {
858 LOG_ERR("Invalid ring/channel %d\n", channel);
859 return -EINVAL;
860 }
861
862 ring = &(pd->ring[channel]);
863 k_mutex_lock(&ring->lock, K_FOREVER);
864
865 if (cfg->block_count > 1) {
866 /* Scatter/gather list handling is not supported */
867 ret = -ENOTSUP;
868 goto err;
869 }
870
871 if (ring->ring_active) {
872 ret = -EBUSY;
873 goto err;
874 }
875
876 ring->ring_active = 1;
877
878 if (cfg->channel_direction == MEMORY_TO_PERIPHERAL) {
879 #ifdef CONFIG_DMA_IPROC_PAX_DEBUG
880 axi_addr32 = (uint32_t *)&cfg->head_block->source_address;
881 pci_addr32 = (uint32_t *)&cfg->head_block->dest_address;
882 #endif
883 ring->payload->direction = CARD_TO_HOST;
884 ring->payload->pci_addr = cfg->head_block->dest_address;
885 ring->payload->axi_addr = cfg->head_block->source_address;
886 } else if (cfg->channel_direction == PERIPHERAL_TO_MEMORY) {
887 #ifdef CONFIG_DMA_IPROC_PAX_DEBUG
888 axi_addr32 = (uint32_t *)&cfg->head_block->dest_address;
889 pci_addr32 = (uint32_t *)&cfg->head_block->source_address;
890 #endif
891 ring->payload->direction = HOST_TO_CARD;
892 ring->payload->pci_addr = cfg->head_block->source_address;
893 ring->payload->axi_addr = cfg->head_block->dest_address;
894 } else {
895 ring->ring_active = 0;
896 ret = -ENOTSUP;
897 goto err;
898 }
899
900 xfer_sz = cfg->head_block->block_size;
901
902 #ifdef CONFIG_DMA_IPROC_PAX_DEBUG
903 if (xfer_sz > PAX_DMA_MAX_SIZE) {
904 LOG_ERR("Unsupported size: %d\n", xfer_size);
905 ring->ring_active = 0;
906 ret = -EINVAL;
907 goto err;
908 }
909
910 if (xfer_sz % PAX_DMA_MIN_SIZE) {
911 LOG_ERR("Unaligned size 0x%x\n", xfer_size);
912 ring->ring_active = 0;
913 ret = -EINVAL;
914 goto err;
915 }
916
917 if (pci_addr32[0] % PAX_DMA_ADDR_ALIGN) {
918 LOG_ERR("Unaligned Host addr: 0x%x.0x%x\n",
919 pci_addr32[1], pci_addr32[0]);
920 ring->ring_active = 0;
921 ret = -EINVAL;
922 goto err;
923 }
924
925 if (axi_addr32[0] % PAX_DMA_ADDR_ALIGN) {
926 LOG_ERR("Unaligned Card addr: 0x%x.0x%x\n",
927 axi_addr32[1], axi_addr32[0]);
928 ring->ring_active = 0;
929 ret = -EINVAL;
930 goto err;
931 }
932 #endif
933 ring->payload->xfer_sz = xfer_sz;
934
935 ring->dma_callback = cfg->dma_callback;
936 ring->callback_arg = cfg->user_data;
937 err:
938 k_mutex_unlock(&ring->lock);
939
940 return ret;
941 }
942
dma_iproc_pax_transfer_start(const struct device * dev,uint32_t channel)943 static int dma_iproc_pax_transfer_start(const struct device *dev,
944 uint32_t channel)
945 {
946 int ret = 0;
947 struct dma_iproc_pax_data *pd = dev->data;
948 struct dma_iproc_pax_ring_data *ring;
949
950 if (channel >= PAX_DMA_RINGS_MAX) {
951 LOG_ERR("Invalid ring %d\n", channel);
952 return -EINVAL;
953 }
954 ring = &(pd->ring[channel]);
955 /* do dma transfer of single buffer */
956 ret = dma_iproc_pax_do_xfer(dev, channel, ring->payload, 1);
957
958 return ret;
959 }
960
dma_iproc_pax_transfer_stop(const struct device * dev,uint32_t channel)961 static int dma_iproc_pax_transfer_stop(const struct device *dev,
962 uint32_t channel)
963 {
964 return 0;
965 }
966
967 static const struct dma_driver_api pax_dma_driver_api = {
968 .config = dma_iproc_pax_configure,
969 .start = dma_iproc_pax_transfer_start,
970 .stop = dma_iproc_pax_transfer_stop,
971 };
972
973 static const struct dma_iproc_pax_cfg pax_dma_cfg = {
974 .dma_base = DT_INST_REG_ADDR_BY_NAME(0, dme_regs),
975 .rm_base = DT_INST_REG_ADDR_BY_NAME(0, rm_ring_regs),
976 .rm_comm_base = DT_INST_REG_ADDR_BY_NAME(0, rm_comm_regs),
977 .use_rings = DT_INST_PROP(0, dma_channels),
978 .bd_memory_base = (void *)DT_INST_PROP_BY_IDX(0, bd_memory, 0),
979 .scr_addr_loc = DT_INST_PROP(0, scr_addr_loc),
980 .pcie_dev = DEVICE_DT_GET(DT_INST_PHANDLE(0, pcie_ep)),
981 };
982
983 DEVICE_DT_INST_DEFINE(0,
984 &dma_iproc_pax_init,
985 NULL,
986 &pax_dma_data,
987 &pax_dma_cfg,
988 POST_KERNEL,
989 CONFIG_DMA_INIT_PRIORITY,
990 &pax_dma_driver_api);
991