blob: fcb84f7aa49b96c0fd21fc90f65d77f58ac3ef7f [file] [log] [blame]
Grygorii Strashko432f66f2019-02-05 17:31:22 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * TI K3 AM65x NAVSS Ring accelerator Manager (RA) subsystem driver
4 *
5 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
6 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <malloc.h>
11#include <asm/dma-mapping.h>
12#include <asm/bitops.h>
13#include <dm.h>
14#include <dm/read.h>
15#include <dm/uclass.h>
16#include <linux/compat.h>
17#include <linux/soc/ti/k3-navss-ringacc.h>
18#include <linux/soc/ti/ti_sci_protocol.h>
19
20#define set_bit(bit, bitmap) __set_bit(bit, bitmap)
21#define clear_bit(bit, bitmap) __clear_bit(bit, bitmap)
22#define dma_free_coherent(dev, size, cpu_addr, dma_handle) \
23 dma_free_coherent(cpu_addr)
24#define dma_zalloc_coherent(dev, size, dma_handle, flag) \
25({ \
26 void *ring_mem_virt; \
27 ring_mem_virt = dma_alloc_coherent((size), \
28 (unsigned long *)(dma_handle)); \
29 if (ring_mem_virt) \
30 memset(ring_mem_virt, 0, (size)); \
31 ring_mem_virt; \
32})
33
34static LIST_HEAD(k3_nav_ringacc_list);
35
36static void ringacc_writel(u32 v, void __iomem *reg)
37{
38 pr_debug("WRITEL(32): v(%08X)-->reg(%p)\n", v, reg);
39 writel(v, reg);
40}
41
42static u32 ringacc_readl(void __iomem *reg)
43{
44 u32 v;
45
46 v = readl(reg);
47 pr_debug("READL(32): v(%08X)<--reg(%p)\n", v, reg);
48 return v;
49}
50
51#define KNAV_RINGACC_CFG_RING_SIZE_ELCNT_MASK GENMASK(19, 0)
52
53/**
54 * struct k3_nav_ring_rt_regs - The RA Control/Status Registers region
55 */
56struct k3_nav_ring_rt_regs {
57 u32 resv_16[4];
58 u32 db; /* RT Ring N Doorbell Register */
59 u32 resv_4[1];
60 u32 occ; /* RT Ring N Occupancy Register */
61 u32 indx; /* RT Ring N Current Index Register */
62 u32 hwocc; /* RT Ring N Hardware Occupancy Register */
63 u32 hwindx; /* RT Ring N Current Index Register */
64};
65
66#define KNAV_RINGACC_RT_REGS_STEP 0x1000
67
68/**
69 * struct k3_nav_ring_fifo_regs - The Ring Accelerator Queues Registers region
70 */
71struct k3_nav_ring_fifo_regs {
72 u32 head_data[128]; /* Ring Head Entry Data Registers */
73 u32 tail_data[128]; /* Ring Tail Entry Data Registers */
74 u32 peek_head_data[128]; /* Ring Peek Head Entry Data Regs */
75 u32 peek_tail_data[128]; /* Ring Peek Tail Entry Data Regs */
76};
77
78/**
79 * struct k3_ringacc_proxy_gcfg_regs - RA Proxy Global Config MMIO Region
80 */
81struct k3_ringacc_proxy_gcfg_regs {
82 u32 revision; /* Revision Register */
83 u32 config; /* Config Register */
84};
85
86#define K3_RINGACC_PROXY_CFG_THREADS_MASK GENMASK(15, 0)
87
88/**
89 * struct k3_ringacc_proxy_target_regs - RA Proxy Datapath MMIO Region
90 */
91struct k3_ringacc_proxy_target_regs {
92 u32 control; /* Proxy Control Register */
93 u32 status; /* Proxy Status Register */
94 u8 resv_512[504];
95 u32 data[128]; /* Proxy Data Register */
96};
97
98#define K3_RINGACC_PROXY_TARGET_STEP 0x1000
99#define K3_RINGACC_PROXY_NOT_USED (-1)
100
101enum k3_ringacc_proxy_access_mode {
102 PROXY_ACCESS_MODE_HEAD = 0,
103 PROXY_ACCESS_MODE_TAIL = 1,
104 PROXY_ACCESS_MODE_PEEK_HEAD = 2,
105 PROXY_ACCESS_MODE_PEEK_TAIL = 3,
106};
107
108#define KNAV_RINGACC_FIFO_WINDOW_SIZE_BYTES (512U)
109#define KNAV_RINGACC_FIFO_REGS_STEP 0x1000
110#define KNAV_RINGACC_MAX_DB_RING_CNT (127U)
111
112/**
113 * struct k3_nav_ring_ops - Ring operations
114 */
115struct k3_nav_ring_ops {
116 int (*push_tail)(struct k3_nav_ring *ring, void *elm);
117 int (*push_head)(struct k3_nav_ring *ring, void *elm);
118 int (*pop_tail)(struct k3_nav_ring *ring, void *elm);
119 int (*pop_head)(struct k3_nav_ring *ring, void *elm);
120};
121
122/**
123 * struct k3_nav_ring - RA Ring descriptor
124 *
125 * @rt - Ring control/status registers
126 * @fifos - Ring queues registers
127 * @proxy - Ring Proxy Datapath registers
128 * @ring_mem_dma - Ring buffer dma address
129 * @ring_mem_virt - Ring buffer virt address
130 * @ops - Ring operations
131 * @size - Ring size in elements
132 * @elm_size - Size of the ring element
133 * @mode - Ring mode
134 * @flags - flags
135 * @free - Number of free elements
136 * @occ - Ring occupancy
137 * @windex - Write index (only for @K3_NAV_RINGACC_RING_MODE_RING)
138 * @rindex - Read index (only for @K3_NAV_RINGACC_RING_MODE_RING)
139 * @ring_id - Ring Id
140 * @parent - Pointer on struct @k3_nav_ringacc
141 * @use_count - Use count for shared rings
142 * @proxy_id - RA Ring Proxy Id (only if @K3_NAV_RINGACC_RING_USE_PROXY)
143 */
144struct k3_nav_ring {
145 struct k3_nav_ring_rt_regs __iomem *rt;
146 struct k3_nav_ring_fifo_regs __iomem *fifos;
147 struct k3_ringacc_proxy_target_regs __iomem *proxy;
148 dma_addr_t ring_mem_dma;
149 void *ring_mem_virt;
150 struct k3_nav_ring_ops *ops;
151 u32 size;
152 enum k3_nav_ring_size elm_size;
153 enum k3_nav_ring_mode mode;
154 u32 flags;
155#define KNAV_RING_FLAG_BUSY BIT(1)
156#define K3_NAV_RING_FLAG_SHARED BIT(2)
157 u32 free;
158 u32 occ;
159 u32 windex;
160 u32 rindex;
161 u32 ring_id;
162 struct k3_nav_ringacc *parent;
163 u32 use_count;
164 int proxy_id;
165};
166
167/**
168 * struct k3_nav_ringacc - Rings accelerator descriptor
169 *
170 * @dev - pointer on RA device
171 * @proxy_gcfg - RA proxy global config registers
172 * @proxy_target_base - RA proxy datapath region
173 * @num_rings - number of ring in RA
174 * @rm_gp_range - general purpose rings range from tisci
175 * @dma_ring_reset_quirk - DMA reset w/a enable
176 * @num_proxies - number of RA proxies
177 * @rings - array of rings descriptors (struct @k3_nav_ring)
178 * @list - list of RAs in the system
179 * @tisci - pointer ti-sci handle
180 * @tisci_ring_ops - ti-sci rings ops
181 * @tisci_dev_id - ti-sci device id
182 */
183struct k3_nav_ringacc {
184 struct udevice *dev;
185 struct k3_ringacc_proxy_gcfg_regs __iomem *proxy_gcfg;
186 void __iomem *proxy_target_base;
187 u32 num_rings; /* number of rings in Ringacc module */
188 unsigned long *rings_inuse;
189 struct ti_sci_resource *rm_gp_range;
190 bool dma_ring_reset_quirk;
191 u32 num_proxies;
192 unsigned long *proxy_inuse;
193
194 struct k3_nav_ring *rings;
195 struct list_head list;
196
197 const struct ti_sci_handle *tisci;
198 const struct ti_sci_rm_ringacc_ops *tisci_ring_ops;
199 u32 tisci_dev_id;
200};
201
202static long k3_nav_ringacc_ring_get_fifo_pos(struct k3_nav_ring *ring)
203{
204 return KNAV_RINGACC_FIFO_WINDOW_SIZE_BYTES -
205 (4 << ring->elm_size);
206}
207
208static void *k3_nav_ringacc_get_elm_addr(struct k3_nav_ring *ring, u32 idx)
209{
210 return (idx * (4 << ring->elm_size) + ring->ring_mem_virt);
211}
212
213static int k3_nav_ringacc_ring_push_mem(struct k3_nav_ring *ring, void *elem);
214static int k3_nav_ringacc_ring_pop_mem(struct k3_nav_ring *ring, void *elem);
215
216static struct k3_nav_ring_ops k3_nav_mode_ring_ops = {
217 .push_tail = k3_nav_ringacc_ring_push_mem,
218 .pop_head = k3_nav_ringacc_ring_pop_mem,
219};
220
221static int k3_nav_ringacc_ring_push_io(struct k3_nav_ring *ring, void *elem);
222static int k3_nav_ringacc_ring_pop_io(struct k3_nav_ring *ring, void *elem);
223static int k3_nav_ringacc_ring_push_head_io(struct k3_nav_ring *ring,
224 void *elem);
225static int k3_nav_ringacc_ring_pop_tail_io(struct k3_nav_ring *ring,
226 void *elem);
227
228static struct k3_nav_ring_ops k3_nav_mode_msg_ops = {
229 .push_tail = k3_nav_ringacc_ring_push_io,
230 .push_head = k3_nav_ringacc_ring_push_head_io,
231 .pop_tail = k3_nav_ringacc_ring_pop_tail_io,
232 .pop_head = k3_nav_ringacc_ring_pop_io,
233};
234
235static int k3_ringacc_ring_push_head_proxy(struct k3_nav_ring *ring,
236 void *elem);
237static int k3_ringacc_ring_push_tail_proxy(struct k3_nav_ring *ring,
238 void *elem);
239static int k3_ringacc_ring_pop_head_proxy(struct k3_nav_ring *ring, void *elem);
240static int k3_ringacc_ring_pop_tail_proxy(struct k3_nav_ring *ring, void *elem);
241
242static struct k3_nav_ring_ops k3_nav_mode_proxy_ops = {
243 .push_tail = k3_ringacc_ring_push_tail_proxy,
244 .push_head = k3_ringacc_ring_push_head_proxy,
245 .pop_tail = k3_ringacc_ring_pop_tail_proxy,
246 .pop_head = k3_ringacc_ring_pop_head_proxy,
247};
248
249struct udevice *k3_nav_ringacc_get_dev(struct k3_nav_ringacc *ringacc)
250{
251 return ringacc->dev;
252}
253
254struct k3_nav_ring *k3_nav_ringacc_request_ring(struct k3_nav_ringacc *ringacc,
255 int id, u32 flags)
256{
257 int proxy_id = K3_RINGACC_PROXY_NOT_USED;
258
259 if (id == K3_NAV_RINGACC_RING_ID_ANY) {
260 /* Request for any general purpose ring */
261 struct ti_sci_resource_desc *gp_rings =
262 &ringacc->rm_gp_range->desc[0];
263 unsigned long size;
264
265 size = gp_rings->start + gp_rings->num;
266 id = find_next_zero_bit(ringacc->rings_inuse,
267 size, gp_rings->start);
268 if (id == size)
269 goto error;
270 } else if (id < 0) {
271 goto error;
272 }
273
274 if (test_bit(id, ringacc->rings_inuse) &&
275 !(ringacc->rings[id].flags & K3_NAV_RING_FLAG_SHARED))
276 goto error;
277 else if (ringacc->rings[id].flags & K3_NAV_RING_FLAG_SHARED)
278 goto out;
279
280 if (flags & K3_NAV_RINGACC_RING_USE_PROXY) {
281 proxy_id = find_next_zero_bit(ringacc->proxy_inuse,
282 ringacc->num_proxies, 0);
283 if (proxy_id == ringacc->num_proxies)
284 goto error;
285 }
286
287 if (!try_module_get(ringacc->dev->driver->owner))
288 goto error;
289
290 if (proxy_id != K3_RINGACC_PROXY_NOT_USED) {
291 set_bit(proxy_id, ringacc->proxy_inuse);
292 ringacc->rings[id].proxy_id = proxy_id;
293 pr_debug("Giving ring#%d proxy#%d\n",
294 id, proxy_id);
295 } else {
296 pr_debug("Giving ring#%d\n", id);
297 }
298
299 set_bit(id, ringacc->rings_inuse);
300out:
301 ringacc->rings[id].use_count++;
302 return &ringacc->rings[id];
303
304error:
305 return NULL;
306}
307
308static void k3_ringacc_ring_reset_sci(struct k3_nav_ring *ring)
309{
310 struct k3_nav_ringacc *ringacc = ring->parent;
311 int ret;
312
313 ret = ringacc->tisci_ring_ops->config(
314 ringacc->tisci,
315 TI_SCI_MSG_VALUE_RM_RING_COUNT_VALID,
316 ringacc->tisci_dev_id,
317 ring->ring_id,
318 0,
319 0,
320 ring->size,
321 0,
322 0,
323 0);
324 if (ret)
325 dev_err(ringacc->dev, "TISCI reset ring fail (%d) ring_idx %d\n",
326 ret, ring->ring_id);
327}
328
329void k3_nav_ringacc_ring_reset(struct k3_nav_ring *ring)
330{
331 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
332 return;
333
334 ring->occ = 0;
335 ring->free = 0;
336 ring->rindex = 0;
337 ring->windex = 0;
338
339 k3_ringacc_ring_reset_sci(ring);
340}
341
342static void k3_ringacc_ring_reconfig_qmode_sci(struct k3_nav_ring *ring,
343 enum k3_nav_ring_mode mode)
344{
345 struct k3_nav_ringacc *ringacc = ring->parent;
346 int ret;
347
348 ret = ringacc->tisci_ring_ops->config(
349 ringacc->tisci,
350 TI_SCI_MSG_VALUE_RM_RING_MODE_VALID,
351 ringacc->tisci_dev_id,
352 ring->ring_id,
353 0,
354 0,
355 0,
356 mode,
357 0,
358 0);
359 if (ret)
360 dev_err(ringacc->dev, "TISCI reconf qmode fail (%d) ring_idx %d\n",
361 ret, ring->ring_id);
362}
363
364void k3_nav_ringacc_ring_reset_dma(struct k3_nav_ring *ring, u32 occ)
365{
366 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
367 return;
368
369 if (!ring->parent->dma_ring_reset_quirk)
370 return;
371
372 if (!occ)
373 occ = ringacc_readl(&ring->rt->occ);
374
375 if (occ) {
376 u32 db_ring_cnt, db_ring_cnt_cur;
377
378 pr_debug("%s %u occ: %u\n", __func__,
379 ring->ring_id, occ);
380 /* 2. Reset the ring */
381 k3_ringacc_ring_reset_sci(ring);
382
383 /*
384 * 3. Setup the ring in ring/doorbell mode
385 * (if not already in this mode)
386 */
387 if (ring->mode != K3_NAV_RINGACC_RING_MODE_RING)
388 k3_ringacc_ring_reconfig_qmode_sci(
389 ring, K3_NAV_RINGACC_RING_MODE_RING);
390 /*
391 * 4. Ring the doorbell 2**22 – ringOcc times.
392 * This will wrap the internal UDMAP ring state occupancy
393 * counter (which is 21-bits wide) to 0.
394 */
395 db_ring_cnt = (1U << 22) - occ;
396
397 while (db_ring_cnt != 0) {
398 /*
399 * Ring the doorbell with the maximum count each
400 * iteration if possible to minimize the total
401 * of writes
402 */
403 if (db_ring_cnt > KNAV_RINGACC_MAX_DB_RING_CNT)
404 db_ring_cnt_cur = KNAV_RINGACC_MAX_DB_RING_CNT;
405 else
406 db_ring_cnt_cur = db_ring_cnt;
407
408 writel(db_ring_cnt_cur, &ring->rt->db);
409 db_ring_cnt -= db_ring_cnt_cur;
410 }
411
412 /* 5. Restore the original ring mode (if not ring mode) */
413 if (ring->mode != K3_NAV_RINGACC_RING_MODE_RING)
414 k3_ringacc_ring_reconfig_qmode_sci(ring, ring->mode);
415 }
416
417 /* 2. Reset the ring */
418 k3_nav_ringacc_ring_reset(ring);
419}
420
421static void k3_ringacc_ring_free_sci(struct k3_nav_ring *ring)
422{
423 struct k3_nav_ringacc *ringacc = ring->parent;
424 int ret;
425
426 ret = ringacc->tisci_ring_ops->config(
427 ringacc->tisci,
428 TI_SCI_MSG_VALUE_RM_ALL_NO_ORDER,
429 ringacc->tisci_dev_id,
430 ring->ring_id,
431 0,
432 0,
433 0,
434 0,
435 0,
436 0);
437 if (ret)
438 dev_err(ringacc->dev, "TISCI ring free fail (%d) ring_idx %d\n",
439 ret, ring->ring_id);
440}
441
442int k3_nav_ringacc_ring_free(struct k3_nav_ring *ring)
443{
444 struct k3_nav_ringacc *ringacc;
445
446 if (!ring)
447 return -EINVAL;
448
449 ringacc = ring->parent;
450
451 pr_debug("%s flags: 0x%08x\n", __func__, ring->flags);
452
453 if (!test_bit(ring->ring_id, ringacc->rings_inuse))
454 return -EINVAL;
455
456 if (--ring->use_count)
457 goto out;
458
459 if (!(ring->flags & KNAV_RING_FLAG_BUSY))
460 goto no_init;
461
462 k3_ringacc_ring_free_sci(ring);
463
464 dma_free_coherent(ringacc->dev,
465 ring->size * (4 << ring->elm_size),
466 ring->ring_mem_virt, ring->ring_mem_dma);
467 ring->flags &= ~KNAV_RING_FLAG_BUSY;
468 ring->ops = NULL;
469 if (ring->proxy_id != K3_RINGACC_PROXY_NOT_USED) {
470 clear_bit(ring->proxy_id, ringacc->proxy_inuse);
471 ring->proxy = NULL;
472 ring->proxy_id = K3_RINGACC_PROXY_NOT_USED;
473 }
474
475no_init:
476 clear_bit(ring->ring_id, ringacc->rings_inuse);
477
478 module_put(ringacc->dev->driver->owner);
479
480out:
481 return 0;
482}
483
484u32 k3_nav_ringacc_get_ring_id(struct k3_nav_ring *ring)
485{
486 if (!ring)
487 return -EINVAL;
488
489 return ring->ring_id;
490}
491
492static int k3_nav_ringacc_ring_cfg_sci(struct k3_nav_ring *ring)
493{
494 struct k3_nav_ringacc *ringacc = ring->parent;
495 u32 ring_idx;
496 int ret;
497
498 if (!ringacc->tisci)
499 return -EINVAL;
500
501 ring_idx = ring->ring_id;
502 ret = ringacc->tisci_ring_ops->config(
503 ringacc->tisci,
504 TI_SCI_MSG_VALUE_RM_ALL_NO_ORDER,
505 ringacc->tisci_dev_id,
506 ring_idx,
507 lower_32_bits(ring->ring_mem_dma),
508 upper_32_bits(ring->ring_mem_dma),
509 ring->size,
510 ring->mode,
511 ring->elm_size,
512 0);
513 if (ret)
514 dev_err(ringacc->dev, "TISCI config ring fail (%d) ring_idx %d\n",
515 ret, ring_idx);
516
517 return ret;
518}
519
520int k3_nav_ringacc_ring_cfg(struct k3_nav_ring *ring,
521 struct k3_nav_ring_cfg *cfg)
522{
523 struct k3_nav_ringacc *ringacc = ring->parent;
524 int ret = 0;
525
526 if (!ring || !cfg)
527 return -EINVAL;
528 if (cfg->elm_size > K3_NAV_RINGACC_RING_ELSIZE_256 ||
529 cfg->mode > K3_NAV_RINGACC_RING_MODE_QM ||
530 cfg->size & ~KNAV_RINGACC_CFG_RING_SIZE_ELCNT_MASK ||
531 !test_bit(ring->ring_id, ringacc->rings_inuse))
532 return -EINVAL;
533
534 if (ring->use_count != 1)
535 return 0;
536
537 ring->size = cfg->size;
538 ring->elm_size = cfg->elm_size;
539 ring->mode = cfg->mode;
540 ring->occ = 0;
541 ring->free = 0;
542 ring->rindex = 0;
543 ring->windex = 0;
544
545 if (ring->proxy_id != K3_RINGACC_PROXY_NOT_USED)
546 ring->proxy = ringacc->proxy_target_base +
547 ring->proxy_id * K3_RINGACC_PROXY_TARGET_STEP;
548
549 switch (ring->mode) {
550 case K3_NAV_RINGACC_RING_MODE_RING:
551 ring->ops = &k3_nav_mode_ring_ops;
552 break;
553 case K3_NAV_RINGACC_RING_MODE_QM:
554 /*
555 * In Queue mode elm_size can be 8 only and each operation
556 * uses 2 element slots
557 */
558 if (cfg->elm_size != K3_NAV_RINGACC_RING_ELSIZE_8 ||
559 cfg->size % 2)
560 goto err_free_proxy;
561 case K3_NAV_RINGACC_RING_MODE_MESSAGE:
562 if (ring->proxy)
563 ring->ops = &k3_nav_mode_proxy_ops;
564 else
565 ring->ops = &k3_nav_mode_msg_ops;
566 break;
567 default:
568 ring->ops = NULL;
569 ret = -EINVAL;
570 goto err_free_proxy;
571 };
572
573 ring->ring_mem_virt =
574 dma_zalloc_coherent(ringacc->dev,
575 ring->size * (4 << ring->elm_size),
576 &ring->ring_mem_dma, GFP_KERNEL);
577 if (!ring->ring_mem_virt) {
578 dev_err(ringacc->dev, "Failed to alloc ring mem\n");
579 ret = -ENOMEM;
580 goto err_free_ops;
581 }
582
583 ret = k3_nav_ringacc_ring_cfg_sci(ring);
584
585 if (ret)
586 goto err_free_mem;
587
588 ring->flags |= KNAV_RING_FLAG_BUSY;
589 ring->flags |= (cfg->flags & K3_NAV_RINGACC_RING_SHARED) ?
590 K3_NAV_RING_FLAG_SHARED : 0;
591
592 return 0;
593
594err_free_mem:
595 dma_free_coherent(ringacc->dev,
596 ring->size * (4 << ring->elm_size),
597 ring->ring_mem_virt,
598 ring->ring_mem_dma);
599err_free_ops:
600 ring->ops = NULL;
601err_free_proxy:
602 ring->proxy = NULL;
603 return ret;
604}
605
606u32 k3_nav_ringacc_ring_get_size(struct k3_nav_ring *ring)
607{
608 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
609 return -EINVAL;
610
611 return ring->size;
612}
613
614u32 k3_nav_ringacc_ring_get_free(struct k3_nav_ring *ring)
615{
616 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
617 return -EINVAL;
618
619 if (!ring->free)
620 ring->free = ring->size - ringacc_readl(&ring->rt->occ);
621
622 return ring->free;
623}
624
625u32 k3_nav_ringacc_ring_get_occ(struct k3_nav_ring *ring)
626{
627 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
628 return -EINVAL;
629
630 return ringacc_readl(&ring->rt->occ);
631}
632
633u32 k3_nav_ringacc_ring_is_full(struct k3_nav_ring *ring)
634{
635 return !k3_nav_ringacc_ring_get_free(ring);
636}
637
638enum k3_ringacc_access_mode {
639 K3_RINGACC_ACCESS_MODE_PUSH_HEAD,
640 K3_RINGACC_ACCESS_MODE_POP_HEAD,
641 K3_RINGACC_ACCESS_MODE_PUSH_TAIL,
642 K3_RINGACC_ACCESS_MODE_POP_TAIL,
643 K3_RINGACC_ACCESS_MODE_PEEK_HEAD,
644 K3_RINGACC_ACCESS_MODE_PEEK_TAIL,
645};
646
647static int k3_ringacc_ring_cfg_proxy(struct k3_nav_ring *ring,
648 enum k3_ringacc_proxy_access_mode mode)
649{
650 u32 val;
651
652 val = ring->ring_id;
653 val |= mode << 16;
654 val |= ring->elm_size << 24;
655 ringacc_writel(val, &ring->proxy->control);
656 return 0;
657}
658
659static int k3_nav_ringacc_ring_access_proxy(
660 struct k3_nav_ring *ring, void *elem,
661 enum k3_ringacc_access_mode access_mode)
662{
663 void __iomem *ptr;
664
665 ptr = (void __iomem *)&ring->proxy->data;
666
667 switch (access_mode) {
668 case K3_RINGACC_ACCESS_MODE_PUSH_HEAD:
669 case K3_RINGACC_ACCESS_MODE_POP_HEAD:
670 k3_ringacc_ring_cfg_proxy(ring, PROXY_ACCESS_MODE_HEAD);
671 break;
672 case K3_RINGACC_ACCESS_MODE_PUSH_TAIL:
673 case K3_RINGACC_ACCESS_MODE_POP_TAIL:
674 k3_ringacc_ring_cfg_proxy(ring, PROXY_ACCESS_MODE_TAIL);
675 break;
676 default:
677 return -EINVAL;
678 }
679
680 ptr += k3_nav_ringacc_ring_get_fifo_pos(ring);
681
682 switch (access_mode) {
683 case K3_RINGACC_ACCESS_MODE_POP_HEAD:
684 case K3_RINGACC_ACCESS_MODE_POP_TAIL:
685 pr_debug("proxy:memcpy_fromio(x): --> ptr(%p), mode:%d\n",
686 ptr, access_mode);
687 memcpy_fromio(elem, ptr, (4 << ring->elm_size));
688 ring->occ--;
689 break;
690 case K3_RINGACC_ACCESS_MODE_PUSH_TAIL:
691 case K3_RINGACC_ACCESS_MODE_PUSH_HEAD:
692 pr_debug("proxy:memcpy_toio(x): --> ptr(%p), mode:%d\n",
693 ptr, access_mode);
694 memcpy_toio(ptr, elem, (4 << ring->elm_size));
695 ring->free--;
696 break;
697 default:
698 return -EINVAL;
699 }
700
701 pr_debug("proxy: free%d occ%d\n",
702 ring->free, ring->occ);
703 return 0;
704}
705
706static int k3_ringacc_ring_push_head_proxy(struct k3_nav_ring *ring, void *elem)
707{
708 return k3_nav_ringacc_ring_access_proxy(
709 ring, elem, K3_RINGACC_ACCESS_MODE_PUSH_HEAD);
710}
711
712static int k3_ringacc_ring_push_tail_proxy(struct k3_nav_ring *ring, void *elem)
713{
714 return k3_nav_ringacc_ring_access_proxy(
715 ring, elem, K3_RINGACC_ACCESS_MODE_PUSH_TAIL);
716}
717
718static int k3_ringacc_ring_pop_head_proxy(struct k3_nav_ring *ring, void *elem)
719{
720 return k3_nav_ringacc_ring_access_proxy(
721 ring, elem, K3_RINGACC_ACCESS_MODE_POP_HEAD);
722}
723
724static int k3_ringacc_ring_pop_tail_proxy(struct k3_nav_ring *ring, void *elem)
725{
726 return k3_nav_ringacc_ring_access_proxy(
727 ring, elem, K3_RINGACC_ACCESS_MODE_POP_HEAD);
728}
729
730static int k3_nav_ringacc_ring_access_io(
731 struct k3_nav_ring *ring, void *elem,
732 enum k3_ringacc_access_mode access_mode)
733{
734 void __iomem *ptr;
735
736 switch (access_mode) {
737 case K3_RINGACC_ACCESS_MODE_PUSH_HEAD:
738 case K3_RINGACC_ACCESS_MODE_POP_HEAD:
739 ptr = (void __iomem *)&ring->fifos->head_data;
740 break;
741 case K3_RINGACC_ACCESS_MODE_PUSH_TAIL:
742 case K3_RINGACC_ACCESS_MODE_POP_TAIL:
743 ptr = (void __iomem *)&ring->fifos->tail_data;
744 break;
745 default:
746 return -EINVAL;
747 }
748
749 ptr += k3_nav_ringacc_ring_get_fifo_pos(ring);
750
751 switch (access_mode) {
752 case K3_RINGACC_ACCESS_MODE_POP_HEAD:
753 case K3_RINGACC_ACCESS_MODE_POP_TAIL:
754 pr_debug("memcpy_fromio(x): --> ptr(%p), mode:%d\n",
755 ptr, access_mode);
756 memcpy_fromio(elem, ptr, (4 << ring->elm_size));
757 ring->occ--;
758 break;
759 case K3_RINGACC_ACCESS_MODE_PUSH_TAIL:
760 case K3_RINGACC_ACCESS_MODE_PUSH_HEAD:
761 pr_debug("memcpy_toio(x): --> ptr(%p), mode:%d\n",
762 ptr, access_mode);
763 memcpy_toio(ptr, elem, (4 << ring->elm_size));
764 ring->free--;
765 break;
766 default:
767 return -EINVAL;
768 }
769
770 pr_debug("free%d index%d occ%d index%d\n",
771 ring->free, ring->windex, ring->occ, ring->rindex);
772 return 0;
773}
774
775static int k3_nav_ringacc_ring_push_head_io(struct k3_nav_ring *ring,
776 void *elem)
777{
778 return k3_nav_ringacc_ring_access_io(
779 ring, elem, K3_RINGACC_ACCESS_MODE_PUSH_HEAD);
780}
781
782static int k3_nav_ringacc_ring_push_io(struct k3_nav_ring *ring, void *elem)
783{
784 return k3_nav_ringacc_ring_access_io(
785 ring, elem, K3_RINGACC_ACCESS_MODE_PUSH_TAIL);
786}
787
788static int k3_nav_ringacc_ring_pop_io(struct k3_nav_ring *ring, void *elem)
789{
790 return k3_nav_ringacc_ring_access_io(
791 ring, elem, K3_RINGACC_ACCESS_MODE_POP_HEAD);
792}
793
794static int k3_nav_ringacc_ring_pop_tail_io(struct k3_nav_ring *ring, void *elem)
795{
796 return k3_nav_ringacc_ring_access_io(
797 ring, elem, K3_RINGACC_ACCESS_MODE_POP_HEAD);
798}
799
800static int k3_nav_ringacc_ring_push_mem(struct k3_nav_ring *ring, void *elem)
801{
802 void *elem_ptr;
803
804 elem_ptr = k3_nav_ringacc_get_elm_addr(ring, ring->windex);
805
806 memcpy(elem_ptr, elem, (4 << ring->elm_size));
807
808 ring->windex = (ring->windex + 1) % ring->size;
809 ring->free--;
810 ringacc_writel(1, &ring->rt->db);
811
812 pr_debug("ring_push_mem: free%d index%d\n",
813 ring->free, ring->windex);
814
815 return 0;
816}
817
818static int k3_nav_ringacc_ring_pop_mem(struct k3_nav_ring *ring, void *elem)
819{
820 void *elem_ptr;
821
822 elem_ptr = k3_nav_ringacc_get_elm_addr(ring, ring->rindex);
823
824 memcpy(elem, elem_ptr, (4 << ring->elm_size));
825
826 ring->rindex = (ring->rindex + 1) % ring->size;
827 ring->occ--;
828 ringacc_writel(-1, &ring->rt->db);
829
830 pr_debug("ring_pop_mem: occ%d index%d pos_ptr%p\n",
831 ring->occ, ring->rindex, elem_ptr);
832 return 0;
833}
834
835int k3_nav_ringacc_ring_push(struct k3_nav_ring *ring, void *elem)
836{
837 int ret = -EOPNOTSUPP;
838
839 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
840 return -EINVAL;
841
842 pr_debug("ring_push%d: free%d index%d\n",
843 ring->ring_id, ring->free, ring->windex);
844
845 if (k3_nav_ringacc_ring_is_full(ring))
846 return -ENOMEM;
847
848 if (ring->ops && ring->ops->push_tail)
849 ret = ring->ops->push_tail(ring, elem);
850
851 return ret;
852}
853
854int k3_nav_ringacc_ring_push_head(struct k3_nav_ring *ring, void *elem)
855{
856 int ret = -EOPNOTSUPP;
857
858 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
859 return -EINVAL;
860
861 pr_debug("ring_push_head: free%d index%d\n",
862 ring->free, ring->windex);
863
864 if (k3_nav_ringacc_ring_is_full(ring))
865 return -ENOMEM;
866
867 if (ring->ops && ring->ops->push_head)
868 ret = ring->ops->push_head(ring, elem);
869
870 return ret;
871}
872
873int k3_nav_ringacc_ring_pop(struct k3_nav_ring *ring, void *elem)
874{
875 int ret = -EOPNOTSUPP;
876
877 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
878 return -EINVAL;
879
880 if (!ring->occ)
881 ring->occ = k3_nav_ringacc_ring_get_occ(ring);
882
883 pr_debug("ring_pop%d: occ%d index%d\n",
884 ring->ring_id, ring->occ, ring->rindex);
885
886 if (!ring->occ)
887 return -ENODATA;
888
889 if (ring->ops && ring->ops->pop_head)
890 ret = ring->ops->pop_head(ring, elem);
891
892 return ret;
893}
894
895int k3_nav_ringacc_ring_pop_tail(struct k3_nav_ring *ring, void *elem)
896{
897 int ret = -EOPNOTSUPP;
898
899 if (!ring || !(ring->flags & KNAV_RING_FLAG_BUSY))
900 return -EINVAL;
901
902 if (!ring->occ)
903 ring->occ = k3_nav_ringacc_ring_get_occ(ring);
904
905 pr_debug("ring_pop_tail: occ%d index%d\n",
906 ring->occ, ring->rindex);
907
908 if (!ring->occ)
909 return -ENODATA;
910
911 if (ring->ops && ring->ops->pop_tail)
912 ret = ring->ops->pop_tail(ring, elem);
913
914 return ret;
915}
916
917static int k3_nav_ringacc_probe_dt(struct k3_nav_ringacc *ringacc)
918{
919 struct udevice *dev = ringacc->dev;
920 struct udevice *tisci_dev = NULL;
921 int ret;
922
923 ringacc->num_rings = dev_read_u32_default(dev, "ti,num-rings", 0);
924 if (!ringacc->num_rings) {
925 dev_err(dev, "ti,num-rings read failure %d\n", ret);
926 return -EINVAL;
927 }
928
929 ringacc->dma_ring_reset_quirk =
930 dev_read_bool(dev, "ti,dma-ring-reset-quirk");
931
932 ret = uclass_get_device_by_name(UCLASS_FIRMWARE, "dmsc", &tisci_dev);
933 if (ret) {
934 pr_debug("TISCI RA RM get failed (%d)\n", ret);
935 ringacc->tisci = NULL;
936 return -ENODEV;
937 }
938 ringacc->tisci = (struct ti_sci_handle *)
939 (ti_sci_get_handle_from_sysfw(tisci_dev));
940
941 ret = dev_read_u32_default(dev, "ti,sci", 0);
942 if (!ret) {
943 dev_err(dev, "TISCI RA RM disabled\n");
944 ringacc->tisci = NULL;
945 return ret;
946 }
947
948 ret = dev_read_u32(dev, "ti,sci-dev-id", &ringacc->tisci_dev_id);
949 if (ret) {
950 dev_err(dev, "ti,sci-dev-id read failure %d\n", ret);
951 ringacc->tisci = NULL;
952 return ret;
953 }
954
955 ringacc->rm_gp_range = devm_ti_sci_get_of_resource(
956 ringacc->tisci, dev,
957 ringacc->tisci_dev_id,
958 "ti,sci-rm-range-gp-rings");
959 if (IS_ERR(ringacc->rm_gp_range))
960 ret = PTR_ERR(ringacc->rm_gp_range);
961
962 return 0;
963}
964
965static int k3_nav_ringacc_probe(struct udevice *dev)
966{
967 struct k3_nav_ringacc *ringacc;
968 void __iomem *base_fifo, *base_rt;
969 int ret, i;
970
971 ringacc = dev_get_priv(dev);
972 if (!ringacc)
973 return -ENOMEM;
974
975 ringacc->dev = dev;
976
977 ret = k3_nav_ringacc_probe_dt(ringacc);
978 if (ret)
979 return ret;
980
981 base_rt = (uint32_t *)devfdt_get_addr_name(dev, "rt");
982 pr_debug("rt %p\n", base_rt);
983 if (IS_ERR(base_rt))
984 return PTR_ERR(base_rt);
985
986 base_fifo = (uint32_t *)devfdt_get_addr_name(dev, "fifos");
987 pr_debug("fifos %p\n", base_fifo);
988 if (IS_ERR(base_fifo))
989 return PTR_ERR(base_fifo);
990
991 ringacc->proxy_gcfg = (struct k3_ringacc_proxy_gcfg_regs __iomem *)
992 devfdt_get_addr_name(dev, "proxy_gcfg");
993 if (IS_ERR(ringacc->proxy_gcfg))
994 return PTR_ERR(ringacc->proxy_gcfg);
995 ringacc->proxy_target_base =
996 (struct k3_ringacc_proxy_gcfg_regs __iomem *)
997 devfdt_get_addr_name(dev, "proxy_target");
998 if (IS_ERR(ringacc->proxy_target_base))
999 return PTR_ERR(ringacc->proxy_target_base);
1000
1001 ringacc->num_proxies = ringacc_readl(&ringacc->proxy_gcfg->config) &
1002 K3_RINGACC_PROXY_CFG_THREADS_MASK;
1003
1004 ringacc->rings = devm_kzalloc(dev,
1005 sizeof(*ringacc->rings) *
1006 ringacc->num_rings,
1007 GFP_KERNEL);
1008 ringacc->rings_inuse = devm_kcalloc(dev,
1009 BITS_TO_LONGS(ringacc->num_rings),
1010 sizeof(unsigned long), GFP_KERNEL);
1011 ringacc->proxy_inuse = devm_kcalloc(dev,
1012 BITS_TO_LONGS(ringacc->num_proxies),
1013 sizeof(unsigned long), GFP_KERNEL);
1014
1015 if (!ringacc->rings || !ringacc->rings_inuse || !ringacc->proxy_inuse)
1016 return -ENOMEM;
1017
1018 for (i = 0; i < ringacc->num_rings; i++) {
1019 ringacc->rings[i].rt = base_rt +
1020 KNAV_RINGACC_RT_REGS_STEP * i;
1021 ringacc->rings[i].fifos = base_fifo +
1022 KNAV_RINGACC_FIFO_REGS_STEP * i;
1023 ringacc->rings[i].parent = ringacc;
1024 ringacc->rings[i].ring_id = i;
1025 ringacc->rings[i].proxy_id = K3_RINGACC_PROXY_NOT_USED;
1026 }
1027 dev_set_drvdata(dev, ringacc);
1028
1029 ringacc->tisci_ring_ops = &ringacc->tisci->ops.rm_ring_ops;
1030
1031 list_add_tail(&ringacc->list, &k3_nav_ringacc_list);
1032
1033 dev_info(dev, "Ring Accelerator probed rings:%u, gp-rings[%u,%u] sci-dev-id:%u\n",
1034 ringacc->num_rings,
1035 ringacc->rm_gp_range->desc[0].start,
1036 ringacc->rm_gp_range->desc[0].num,
1037 ringacc->tisci_dev_id);
1038 dev_info(dev, "dma-ring-reset-quirk: %s\n",
1039 ringacc->dma_ring_reset_quirk ? "enabled" : "disabled");
1040 dev_info(dev, "RA Proxy rev. %08x, num_proxies:%u\n",
1041 ringacc_readl(&ringacc->proxy_gcfg->revision),
1042 ringacc->num_proxies);
1043 return 0;
1044}
1045
1046static const struct udevice_id knav_ringacc_ids[] = {
1047 { .compatible = "ti,am654-navss-ringacc" },
1048 {},
1049};
1050
1051U_BOOT_DRIVER(k3_navss_ringacc) = {
1052 .name = "k3-navss-ringacc",
1053 .id = UCLASS_MISC,
1054 .of_match = knav_ringacc_ids,
1055 .probe = k3_nav_ringacc_probe,
1056 .priv_auto_alloc_size = sizeof(struct k3_nav_ringacc),
1057};