1
2
3
4
5
6
7#include "qbman_portal.h"
8
9
10#define QBMAN_MC_ACQUIRE 0x30
11#define QBMAN_WQCHAN_CONFIGURE 0x46
12
13
14#define QBMAN_CINH_SWP_EQAR 0x8c0
15#define QBMAN_CINH_SWP_DCAP 0xac0
16#define QBMAN_CINH_SWP_SDQCR 0xb00
17#define QBMAN_CINH_SWP_RAR 0xcc0
18
19
20#define QBMAN_CENA_SWP_EQCR(n) (0x000 + ((uint32_t)(n) << 6))
21#define QBMAN_CENA_SWP_DQRR(n) (0x200 + ((uint32_t)(n) << 6))
22#define QBMAN_CENA_SWP_RCR(n) (0x400 + ((uint32_t)(n) << 6))
23#define QBMAN_CENA_SWP_CR 0x600
24#define QBMAN_CENA_SWP_RR(vb) (0x700 + ((uint32_t)(vb) >> 1))
25#define QBMAN_CENA_SWP_VDQCR 0x780
26
27
28#define QBMAN_IDX_FROM_DQRR(p) (((unsigned long)p & 0xff) >> 6)
29
30
31
32
33
34struct qb_attr_code code_generic_verb = QB_CODE(0, 0, 7);
35struct qb_attr_code code_generic_rslt = QB_CODE(0, 8, 8);
36
37
38
39
40
41
42
43struct qb_attr_code code_sdqcr_dct = QB_CODE(0, 24, 2);
44struct qb_attr_code code_sdqcr_fc = QB_CODE(0, 29, 1);
45struct qb_attr_code code_sdqcr_tok = QB_CODE(0, 16, 8);
46#define CODE_SDQCR_DQSRC(n) QB_CODE(0, n, 1)
47enum qbman_sdqcr_dct {
48 qbman_sdqcr_dct_null = 0,
49 qbman_sdqcr_dct_prio_ics,
50 qbman_sdqcr_dct_active_ics,
51 qbman_sdqcr_dct_active
52};
53enum qbman_sdqcr_fc {
54 qbman_sdqcr_fc_one = 0,
55 qbman_sdqcr_fc_up_to_3 = 1
56};
57
58
59
60
61
62
63
64struct qbman_swp *qbman_swp_init(const struct qbman_swp_desc *d)
65{
66 int ret;
67 struct qbman_swp *p = malloc(sizeof(struct qbman_swp));
68
69 if (!p)
70 return NULL;
71 p->desc = d;
72#ifdef QBMAN_CHECKING
73 p->mc.check = swp_mc_can_start;
74#endif
75 p->mc.valid_bit = QB_VALID_BIT;
76 p->sdq = 0;
77 qb_attr_code_encode(&code_sdqcr_dct, &p->sdq, qbman_sdqcr_dct_prio_ics);
78 qb_attr_code_encode(&code_sdqcr_fc, &p->sdq, qbman_sdqcr_fc_up_to_3);
79 qb_attr_code_encode(&code_sdqcr_tok, &p->sdq, 0xbb);
80 atomic_set(&p->vdq.busy, 1);
81 p->vdq.valid_bit = QB_VALID_BIT;
82 p->dqrr.next_idx = 0;
83 p->dqrr.valid_bit = QB_VALID_BIT;
84 ret = qbman_swp_sys_init(&p->sys, d);
85 if (ret) {
86 free(p);
87 printf("qbman_swp_sys_init() failed %d\n", ret);
88 return NULL;
89 }
90 qbman_cinh_write(&p->sys, QBMAN_CINH_SWP_SDQCR, p->sdq);
91 return p;
92}
93
94
95
96
97
98
99
100
101
102void *qbman_swp_mc_start(struct qbman_swp *p)
103{
104 void *ret;
105 int *return_val;
106#ifdef QBMAN_CHECKING
107 BUG_ON(p->mc.check != swp_mc_can_start);
108#endif
109 ret = qbman_cena_write_start(&p->sys, QBMAN_CENA_SWP_CR);
110#ifdef QBMAN_CHECKING
111 return_val = (int *)ret;
112 if (!(*return_val))
113 p->mc.check = swp_mc_can_submit;
114#endif
115 return ret;
116}
117
118void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, uint32_t cmd_verb)
119{
120 uint32_t *v = cmd;
121#ifdef QBMAN_CHECKING
122 BUG_ON(p->mc.check != swp_mc_can_submit);
123#endif
124 lwsync();
125
126
127
128
129 BUG_ON((*v & cmd_verb) != *v);
130 *v = cmd_verb | p->mc.valid_bit;
131 qbman_cena_write_complete(&p->sys, QBMAN_CENA_SWP_CR, cmd);
132
133#ifdef QBMAN_CHECKING
134 p->mc.check = swp_mc_can_poll;
135#endif
136}
137
138void *qbman_swp_mc_result(struct qbman_swp *p)
139{
140 uint32_t *ret, verb;
141#ifdef QBMAN_CHECKING
142 BUG_ON(p->mc.check != swp_mc_can_poll);
143#endif
144 ret = qbman_cena_read(&p->sys, QBMAN_CENA_SWP_RR(p->mc.valid_bit));
145
146 verb = ret[0] & ~QB_VALID_BIT;
147 if (!verb)
148 return NULL;
149#ifdef QBMAN_CHECKING
150 p->mc.check = swp_mc_can_start;
151#endif
152 p->mc.valid_bit ^= QB_VALID_BIT;
153 return ret;
154}
155
156
157
158
159
160
161static struct qb_attr_code code_eq_cmd = QB_CODE(0, 0, 2);
162static struct qb_attr_code code_eq_orp_en = QB_CODE(0, 2, 1);
163static struct qb_attr_code code_eq_tgt_id = QB_CODE(2, 0, 24);
164
165static struct qb_attr_code code_eq_qd_en = QB_CODE(0, 4, 1);
166static struct qb_attr_code code_eq_qd_bin = QB_CODE(4, 0, 16);
167static struct qb_attr_code code_eq_qd_pri = QB_CODE(4, 16, 4);
168static struct qb_attr_code code_eq_rsp_stash = QB_CODE(5, 16, 1);
169static struct qb_attr_code code_eq_rsp_lo = QB_CODE(6, 0, 32);
170
171enum qbman_eq_cmd_e {
172
173 qbman_eq_cmd_empty,
174
175 qbman_eq_cmd_respond,
176
177 qbman_eq_cmd_respond_reject
178};
179
180void qbman_eq_desc_clear(struct qbman_eq_desc *d)
181{
182 memset(d, 0, sizeof(*d));
183}
184
185void qbman_eq_desc_set_no_orp(struct qbman_eq_desc *d, int respond_success)
186{
187 uint32_t *cl = qb_cl(d);
188
189 qb_attr_code_encode(&code_eq_orp_en, cl, 0);
190 qb_attr_code_encode(&code_eq_cmd, cl,
191 respond_success ? qbman_eq_cmd_respond :
192 qbman_eq_cmd_respond_reject);
193}
194
195void qbman_eq_desc_set_response(struct qbman_eq_desc *d,
196 dma_addr_t storage_phys,
197 int stash)
198{
199 uint32_t *cl = qb_cl(d);
200
201 qb_attr_code_encode_64(&code_eq_rsp_lo, (uint64_t *)cl, storage_phys);
202 qb_attr_code_encode(&code_eq_rsp_stash, cl, !!stash);
203}
204
205
206void qbman_eq_desc_set_qd(struct qbman_eq_desc *d, uint32_t qdid,
207 uint32_t qd_bin, uint32_t qd_prio)
208{
209 uint32_t *cl = qb_cl(d);
210
211 qb_attr_code_encode(&code_eq_qd_en, cl, 1);
212 qb_attr_code_encode(&code_eq_tgt_id, cl, qdid);
213 qb_attr_code_encode(&code_eq_qd_bin, cl, qd_bin);
214 qb_attr_code_encode(&code_eq_qd_pri, cl, qd_prio);
215}
216
217#define EQAR_IDX(eqar) ((eqar) & 0x7)
218#define EQAR_VB(eqar) ((eqar) & 0x80)
219#define EQAR_SUCCESS(eqar) ((eqar) & 0x100)
220
221int qbman_swp_enqueue(struct qbman_swp *s, const struct qbman_eq_desc *d,
222 const struct qbman_fd *fd)
223{
224 uint32_t *p;
225 const uint32_t *cl = qb_cl(d);
226 uint32_t eqar = qbman_cinh_read(&s->sys, QBMAN_CINH_SWP_EQAR);
227 debug("EQAR=%08x\n", eqar);
228 if (!EQAR_SUCCESS(eqar))
229 return -EBUSY;
230 p = qbman_cena_write_start(&s->sys,
231 QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)));
232 word_copy(&p[1], &cl[1], 7);
233 word_copy(&p[8], fd, sizeof(*fd) >> 2);
234 lwsync();
235
236 p[0] = cl[0] | EQAR_VB(eqar);
237 qbman_cena_write_complete(&s->sys,
238 QBMAN_CENA_SWP_EQCR(EQAR_IDX(eqar)),
239 p);
240 return 0;
241}
242
243
244
245
246
247
248static struct qb_attr_code code_pull_dct = QB_CODE(0, 0, 2);
249static struct qb_attr_code code_pull_dt = QB_CODE(0, 2, 2);
250static struct qb_attr_code code_pull_rls = QB_CODE(0, 4, 1);
251static struct qb_attr_code code_pull_stash = QB_CODE(0, 5, 1);
252static struct qb_attr_code code_pull_numframes = QB_CODE(0, 8, 4);
253static struct qb_attr_code code_pull_token = QB_CODE(0, 16, 8);
254static struct qb_attr_code code_pull_dqsource = QB_CODE(1, 0, 24);
255static struct qb_attr_code code_pull_rsp_lo = QB_CODE(2, 0, 32);
256
257enum qb_pull_dt_e {
258 qb_pull_dt_channel,
259 qb_pull_dt_workqueue,
260 qb_pull_dt_framequeue
261};
262
263void qbman_pull_desc_clear(struct qbman_pull_desc *d)
264{
265 memset(d, 0, sizeof(*d));
266}
267
268void qbman_pull_desc_set_storage(struct qbman_pull_desc *d,
269 struct ldpaa_dq *storage,
270 dma_addr_t storage_phys,
271 int stash)
272{
273 uint32_t *cl = qb_cl(d);
274
275
276
277 *(void **)&cl[4] = storage;
278 if (!storage) {
279 qb_attr_code_encode(&code_pull_rls, cl, 0);
280 return;
281 }
282 qb_attr_code_encode(&code_pull_rls, cl, 1);
283 qb_attr_code_encode(&code_pull_stash, cl, !!stash);
284 qb_attr_code_encode_64(&code_pull_rsp_lo, (uint64_t *)cl, storage_phys);
285}
286
287void qbman_pull_desc_set_numframes(struct qbman_pull_desc *d, uint8_t numframes)
288{
289 uint32_t *cl = qb_cl(d);
290
291 BUG_ON(!numframes || (numframes > 16));
292 qb_attr_code_encode(&code_pull_numframes, cl,
293 (uint32_t)(numframes - 1));
294}
295
296void qbman_pull_desc_set_token(struct qbman_pull_desc *d, uint8_t token)
297{
298 uint32_t *cl = qb_cl(d);
299
300 qb_attr_code_encode(&code_pull_token, cl, token);
301}
302
303void qbman_pull_desc_set_fq(struct qbman_pull_desc *d, uint32_t fqid)
304{
305 uint32_t *cl = qb_cl(d);
306
307 qb_attr_code_encode(&code_pull_dct, cl, 1);
308 qb_attr_code_encode(&code_pull_dt, cl, qb_pull_dt_framequeue);
309 qb_attr_code_encode(&code_pull_dqsource, cl, fqid);
310}
311
312int qbman_swp_pull(struct qbman_swp *s, struct qbman_pull_desc *d)
313{
314 uint32_t *p;
315 uint32_t *cl = qb_cl(d);
316
317 if (!atomic_dec_and_test(&s->vdq.busy)) {
318 atomic_inc(&s->vdq.busy);
319 return -EBUSY;
320 }
321 s->vdq.storage = *(void **)&cl[4];
322 s->vdq.token = qb_attr_code_decode(&code_pull_token, cl);
323 p = qbman_cena_write_start(&s->sys, QBMAN_CENA_SWP_VDQCR);
324 word_copy(&p[1], &cl[1], 3);
325 lwsync();
326
327 p[0] = cl[0] | s->vdq.valid_bit;
328 s->vdq.valid_bit ^= QB_VALID_BIT;
329 qbman_cena_write_complete(&s->sys, QBMAN_CENA_SWP_VDQCR, p);
330 return 0;
331}
332
333
334
335
336
337static struct qb_attr_code code_dqrr_verb = QB_CODE(0, 0, 8);
338static struct qb_attr_code code_dqrr_response = QB_CODE(0, 0, 7);
339static struct qb_attr_code code_dqrr_stat = QB_CODE(0, 8, 8);
340
341#define QBMAN_DQRR_RESPONSE_DQ 0x60
342#define QBMAN_DQRR_RESPONSE_FQRN 0x21
343#define QBMAN_DQRR_RESPONSE_FQRNI 0x22
344#define QBMAN_DQRR_RESPONSE_FQPN 0x24
345#define QBMAN_DQRR_RESPONSE_FQDAN 0x25
346#define QBMAN_DQRR_RESPONSE_CDAN 0x26
347#define QBMAN_DQRR_RESPONSE_CSCN_MEM 0x27
348#define QBMAN_DQRR_RESPONSE_CGCU 0x28
349#define QBMAN_DQRR_RESPONSE_BPSCN 0x29
350#define QBMAN_DQRR_RESPONSE_CSCN_WQ 0x2a
351
352
353
354
355
356const struct ldpaa_dq *qbman_swp_dqrr_next(struct qbman_swp *s)
357{
358 uint32_t verb;
359 uint32_t response_verb;
360 uint32_t flags;
361 const struct ldpaa_dq *dq;
362 const uint32_t *p;
363
364 dq = qbman_cena_read(&s->sys, QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
365 p = qb_cl(dq);
366 verb = qb_attr_code_decode(&code_dqrr_verb, p);
367
368
369
370
371
372
373
374
375 if ((verb & QB_VALID_BIT) != s->dqrr.valid_bit) {
376 qbman_cena_invalidate_prefetch(&s->sys,
377 QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
378 return NULL;
379 }
380
381
382 s->dqrr.next_idx++;
383 s->dqrr.next_idx &= QBMAN_DQRR_SIZE - 1;
384
385
386 if (!s->dqrr.next_idx)
387 s->dqrr.valid_bit ^= QB_VALID_BIT;
388
389
390
391 flags = ldpaa_dq_flags(dq);
392 response_verb = qb_attr_code_decode(&code_dqrr_response, &verb);
393 if ((response_verb == QBMAN_DQRR_RESPONSE_DQ) &&
394 (flags & LDPAA_DQ_STAT_VOLATILE) &&
395 (flags & LDPAA_DQ_STAT_EXPIRED))
396 atomic_inc(&s->vdq.busy);
397
398 qbman_cena_invalidate_prefetch(&s->sys,
399 QBMAN_CENA_SWP_DQRR(s->dqrr.next_idx));
400 return dq;
401}
402
403
404void qbman_swp_dqrr_consume(struct qbman_swp *s, const struct ldpaa_dq *dq)
405{
406 qbman_cinh_write(&s->sys, QBMAN_CINH_SWP_DCAP, QBMAN_IDX_FROM_DQRR(dq));
407}
408
409
410
411
412
413void qbman_dq_entry_set_oldtoken(struct ldpaa_dq *dq,
414 unsigned int num_entries,
415 uint8_t oldtoken)
416{
417 memset(dq, oldtoken, num_entries * sizeof(*dq));
418}
419
420int qbman_dq_entry_has_newtoken(struct qbman_swp *s,
421 const struct ldpaa_dq *dq,
422 uint8_t newtoken)
423{
424
425
426
427
428
429
430 static struct qb_attr_code code_dqrr_tok_detect =
431 QB_CODE(0, DQRR_TOK_OFFSET, 8);
432
433
434
435
436 uint32_t *p = qb_cl((struct ldpaa_dq *)dq);
437 uint32_t token;
438
439 token = qb_attr_code_decode(&code_dqrr_tok_detect, &p[1]);
440 if (token != newtoken)
441 return 0;
442
443
444
445
446 make_le32_n(p, 16);
447
448
449
450
451
452
453
454
455
456
457 if (s->vdq.storage == dq) {
458 s->vdq.storage = NULL;
459 atomic_inc(&s->vdq.busy);
460 }
461 return 1;
462}
463
464
465
466
467
468static inline int __qbman_dq_entry_is_x(const struct ldpaa_dq *dq, uint32_t x)
469{
470 const uint32_t *p = qb_cl(dq);
471 uint32_t response_verb = qb_attr_code_decode(&code_dqrr_response, p);
472
473 return response_verb == x;
474}
475
476int qbman_dq_entry_is_DQ(const struct ldpaa_dq *dq)
477{
478 return __qbman_dq_entry_is_x(dq, QBMAN_DQRR_RESPONSE_DQ);
479}
480
481
482
483
484
485
486
487uint32_t ldpaa_dq_flags(const struct ldpaa_dq *dq)
488{
489 const uint32_t *p = qb_cl(dq);
490
491 return qb_attr_code_decode(&code_dqrr_stat, p);
492}
493
494const struct dpaa_fd *ldpaa_dq_fd(const struct ldpaa_dq *dq)
495{
496 const uint32_t *p = qb_cl(dq);
497
498 return (const struct dpaa_fd *)&p[8];
499}
500
501
502
503
504
505
506
507static struct qb_attr_code code_release_set_me = QB_CODE(0, 5, 1);
508static struct qb_attr_code code_release_bpid = QB_CODE(0, 16, 16);
509
510void qbman_release_desc_clear(struct qbman_release_desc *d)
511{
512 uint32_t *cl;
513
514 memset(d, 0, sizeof(*d));
515 cl = qb_cl(d);
516 qb_attr_code_encode(&code_release_set_me, cl, 1);
517}
518
519void qbman_release_desc_set_bpid(struct qbman_release_desc *d, uint32_t bpid)
520{
521 uint32_t *cl = qb_cl(d);
522
523 qb_attr_code_encode(&code_release_bpid, cl, bpid);
524}
525
526#define RAR_IDX(rar) ((rar) & 0x7)
527#define RAR_VB(rar) ((rar) & 0x80)
528#define RAR_SUCCESS(rar) ((rar) & 0x100)
529
530int qbman_swp_release(struct qbman_swp *s, const struct qbman_release_desc *d,
531 const uint64_t *buffers, unsigned int num_buffers)
532{
533 uint32_t *p;
534 const uint32_t *cl = qb_cl(d);
535 uint32_t rar = qbman_cinh_read(&s->sys, QBMAN_CINH_SWP_RAR);
536 debug("RAR=%08x\n", rar);
537 if (!RAR_SUCCESS(rar))
538 return -EBUSY;
539 BUG_ON(!num_buffers || (num_buffers > 7));
540
541 p = qbman_cena_write_start(&s->sys,
542 QBMAN_CENA_SWP_RCR(RAR_IDX(rar)));
543
544 u64_to_le32_copy(&p[2], buffers, num_buffers);
545 lwsync();
546
547
548 p[0] = cl[0] | RAR_VB(rar) | num_buffers;
549 qbman_cena_write_complete(&s->sys,
550 QBMAN_CENA_SWP_RCR(RAR_IDX(rar)),
551 p);
552 return 0;
553}
554
555
556
557
558
559
560static struct qb_attr_code code_acquire_bpid = QB_CODE(0, 16, 16);
561static struct qb_attr_code code_acquire_num = QB_CODE(1, 0, 3);
562static struct qb_attr_code code_acquire_r_num = QB_CODE(1, 0, 3);
563
564int qbman_swp_acquire(struct qbman_swp *s, uint32_t bpid, uint64_t *buffers,
565 unsigned int num_buffers)
566{
567 uint32_t *p;
568 uint32_t verb, rslt, num;
569
570 BUG_ON(!num_buffers || (num_buffers > 7));
571
572
573 p = qbman_swp_mc_start(s);
574
575 if (!p)
576 return -EBUSY;
577
578
579 qb_attr_code_encode(&code_acquire_bpid, p, bpid);
580 qb_attr_code_encode(&code_acquire_num, p, num_buffers);
581
582
583 p = qbman_swp_mc_complete(s, p, p[0] | QBMAN_MC_ACQUIRE);
584
585
586 verb = qb_attr_code_decode(&code_generic_verb, p);
587 rslt = qb_attr_code_decode(&code_generic_rslt, p);
588 num = qb_attr_code_decode(&code_acquire_r_num, p);
589 BUG_ON(verb != QBMAN_MC_ACQUIRE);
590
591
592 if (unlikely(rslt != QBMAN_MC_RSLT_OK)) {
593 printf("Acquire buffers from BPID 0x%x failed, code=0x%02x\n",
594 bpid, rslt);
595 return -EIO;
596 }
597 BUG_ON(num > num_buffers);
598
599 u64_from_le32_copy(buffers, &p[2], num);
600 return (int)num;
601}
602