New upstream version 17.11.1
[deb_dpdk.git] / drivers / bus / dpaa / base / qbman / qman.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2008-2016 Freescale Semiconductor Inc.
8  * Copyright 2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include "qman_priv.h"
42
43 /***************************/
44 /* Portal register assists */
45 /***************************/
46 #define QM_REG_EQCR_PI_CINH     0x3000
47 #define QM_REG_EQCR_CI_CINH     0x3040
48 #define QM_REG_EQCR_ITR         0x3080
49 #define QM_REG_DQRR_PI_CINH     0x3100
50 #define QM_REG_DQRR_CI_CINH     0x3140
51 #define QM_REG_DQRR_ITR         0x3180
52 #define QM_REG_DQRR_DCAP        0x31C0
53 #define QM_REG_DQRR_SDQCR       0x3200
54 #define QM_REG_DQRR_VDQCR       0x3240
55 #define QM_REG_DQRR_PDQCR       0x3280
56 #define QM_REG_MR_PI_CINH       0x3300
57 #define QM_REG_MR_CI_CINH       0x3340
58 #define QM_REG_MR_ITR           0x3380
59 #define QM_REG_CFG              0x3500
60 #define QM_REG_ISR              0x3600
61 #define QM_REG_IIR              0x36C0
62 #define QM_REG_ITPR             0x3740
63
64 /* Cache-enabled register offsets */
65 #define QM_CL_EQCR              0x0000
66 #define QM_CL_DQRR              0x1000
67 #define QM_CL_MR                0x2000
68 #define QM_CL_EQCR_PI_CENA      0x3000
69 #define QM_CL_EQCR_CI_CENA      0x3040
70 #define QM_CL_DQRR_PI_CENA      0x3100
71 #define QM_CL_DQRR_CI_CENA      0x3140
72 #define QM_CL_MR_PI_CENA        0x3300
73 #define QM_CL_MR_CI_CENA        0x3340
74 #define QM_CL_CR                0x3800
75 #define QM_CL_RR0               0x3900
76 #define QM_CL_RR1               0x3940
77
78 /* BTW, the drivers (and h/w programming model) already obtain the required
79  * synchronisation for portal accesses via lwsync(), hwsync(), and
80  * data-dependencies. Use of barrier()s or other order-preserving primitives
81  * simply degrade performance. Hence the use of the __raw_*() interfaces, which
82  * simply ensure that the compiler treats the portal registers as volatile (ie.
83  * non-coherent).
84  */
85
86 /* Cache-inhibited register access. */
87 #define __qm_in(qm, o)          be32_to_cpu(__raw_readl((qm)->ci  + (o)))
88 #define __qm_out(qm, o, val)    __raw_writel((cpu_to_be32(val)), \
89                                              (qm)->ci + (o))
90 #define qm_in(reg)              __qm_in(&portal->addr, QM_REG_##reg)
91 #define qm_out(reg, val)        __qm_out(&portal->addr, QM_REG_##reg, val)
92
93 /* Cache-enabled (index) register access */
94 #define __qm_cl_touch_ro(qm, o) dcbt_ro((qm)->ce + (o))
95 #define __qm_cl_touch_rw(qm, o) dcbt_rw((qm)->ce + (o))
96 #define __qm_cl_in(qm, o)       be32_to_cpu(__raw_readl((qm)->ce + (o)))
97 #define __qm_cl_out(qm, o, val) \
98         do { \
99                 u32 *__tmpclout = (qm)->ce + (o); \
100                 __raw_writel(cpu_to_be32(val), __tmpclout); \
101                 dcbf(__tmpclout); \
102         } while (0)
103 #define __qm_cl_invalidate(qm, o) dccivac((qm)->ce + (o))
104 #define qm_cl_touch_ro(reg) __qm_cl_touch_ro(&portal->addr, QM_CL_##reg##_CENA)
105 #define qm_cl_touch_rw(reg) __qm_cl_touch_rw(&portal->addr, QM_CL_##reg##_CENA)
106 #define qm_cl_in(reg)       __qm_cl_in(&portal->addr, QM_CL_##reg##_CENA)
107 #define qm_cl_out(reg, val) __qm_cl_out(&portal->addr, QM_CL_##reg##_CENA, val)
108 #define qm_cl_invalidate(reg)\
109         __qm_cl_invalidate(&portal->addr, QM_CL_##reg##_CENA)
110
111 /* Cache-enabled ring access */
112 #define qm_cl(base, idx)        ((void *)base + ((idx) << 6))
113
114 /* Cyclic helper for rings. FIXME: once we are able to do fine-grain perf
115  * analysis, look at using the "extra" bit in the ring index registers to avoid
116  * cyclic issues.
117  */
118 static inline u8 qm_cyc_diff(u8 ringsize, u8 first, u8 last)
119 {
120         /* 'first' is included, 'last' is excluded */
121         if (first <= last)
122                 return last - first;
123         return ringsize + last - first;
124 }
125
126 /* Portal modes.
127  *   Enum types;
128  *     pmode == production mode
129  *     cmode == consumption mode,
130  *     dmode == h/w dequeue mode.
131  *   Enum values use 3 letter codes. First letter matches the portal mode,
132  *   remaining two letters indicate;
133  *     ci == cache-inhibited portal register
134  *     ce == cache-enabled portal register
135  *     vb == in-band valid-bit (cache-enabled)
136  *     dc == DCA (Discrete Consumption Acknowledgment), DQRR-only
137  *   As for "enum qm_dqrr_dmode", it should be self-explanatory.
138  */
139 enum qm_eqcr_pmode {            /* matches QCSP_CFG::EPM */
140         qm_eqcr_pci = 0,        /* PI index, cache-inhibited */
141         qm_eqcr_pce = 1,        /* PI index, cache-enabled */
142         qm_eqcr_pvb = 2         /* valid-bit */
143 };
144
145 enum qm_dqrr_dmode {            /* matches QCSP_CFG::DP */
146         qm_dqrr_dpush = 0,      /* SDQCR  + VDQCR */
147         qm_dqrr_dpull = 1       /* PDQCR */
148 };
149
150 enum qm_dqrr_pmode {            /* s/w-only */
151         qm_dqrr_pci,            /* reads DQRR_PI_CINH */
152         qm_dqrr_pce,            /* reads DQRR_PI_CENA */
153         qm_dqrr_pvb             /* reads valid-bit */
154 };
155
156 enum qm_dqrr_cmode {            /* matches QCSP_CFG::DCM */
157         qm_dqrr_cci = 0,        /* CI index, cache-inhibited */
158         qm_dqrr_cce = 1,        /* CI index, cache-enabled */
159         qm_dqrr_cdc = 2         /* Discrete Consumption Acknowledgment */
160 };
161
162 enum qm_mr_pmode {              /* s/w-only */
163         qm_mr_pci,              /* reads MR_PI_CINH */
164         qm_mr_pce,              /* reads MR_PI_CENA */
165         qm_mr_pvb               /* reads valid-bit */
166 };
167
168 enum qm_mr_cmode {              /* matches QCSP_CFG::MM */
169         qm_mr_cci = 0,          /* CI index, cache-inhibited */
170         qm_mr_cce = 1           /* CI index, cache-enabled */
171 };
172
173 /* ------------------------- */
174 /* --- Portal structures --- */
175
176 #define QM_EQCR_SIZE            8
177 #define QM_DQRR_SIZE            16
178 #define QM_MR_SIZE              8
179
180 struct qm_eqcr {
181         struct qm_eqcr_entry *ring, *cursor;
182         u8 ci, available, ithresh, vbit;
183 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
184         u32 busy;
185         enum qm_eqcr_pmode pmode;
186 #endif
187 };
188
189 struct qm_dqrr {
190         const struct qm_dqrr_entry *ring, *cursor;
191         u8 pi, ci, fill, ithresh, vbit;
192 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
193         enum qm_dqrr_dmode dmode;
194         enum qm_dqrr_pmode pmode;
195         enum qm_dqrr_cmode cmode;
196 #endif
197 };
198
199 struct qm_mr {
200         const struct qm_mr_entry *ring, *cursor;
201         u8 pi, ci, fill, ithresh, vbit;
202 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
203         enum qm_mr_pmode pmode;
204         enum qm_mr_cmode cmode;
205 #endif
206 };
207
208 struct qm_mc {
209         struct qm_mc_command *cr;
210         struct qm_mc_result *rr;
211         u8 rridx, vbit;
212 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
213         enum {
214                 /* Can be _mc_start()ed */
215                 qman_mc_idle,
216                 /* Can be _mc_commit()ed or _mc_abort()ed */
217                 qman_mc_user,
218                 /* Can only be _mc_retry()ed */
219                 qman_mc_hw
220         } state;
221 #endif
222 };
223
224 #define QM_PORTAL_ALIGNMENT ____cacheline_aligned
225
226 struct qm_addr {
227         void __iomem *ce;       /* cache-enabled */
228         void __iomem *ci;       /* cache-inhibited */
229 };
230
231 struct qm_portal {
232         struct qm_addr addr;
233         struct qm_eqcr eqcr;
234         struct qm_dqrr dqrr;
235         struct qm_mr mr;
236         struct qm_mc mc;
237 } QM_PORTAL_ALIGNMENT;
238
239 /* Bit-wise logic to wrap a ring pointer by clearing the "carry bit" */
240 #define EQCR_CARRYCLEAR(p) \
241         (void *)((unsigned long)(p) & (~(unsigned long)(QM_EQCR_SIZE << 6)))
242
243 extern dma_addr_t rte_mem_virt2iova(const void *addr);
244
245 /* Bit-wise logic to convert a ring pointer to a ring index */
246 static inline u8 EQCR_PTR2IDX(struct qm_eqcr_entry *e)
247 {
248         return ((uintptr_t)e >> 6) & (QM_EQCR_SIZE - 1);
249 }
250
251 /* Increment the 'cursor' ring pointer, taking 'vbit' into account */
252 static inline void EQCR_INC(struct qm_eqcr *eqcr)
253 {
254         /* NB: this is odd-looking, but experiments show that it generates fast
255          * code with essentially no branching overheads. We increment to the
256          * next EQCR pointer and handle overflow and 'vbit'.
257          */
258         struct qm_eqcr_entry *partial = eqcr->cursor + 1;
259
260         eqcr->cursor = EQCR_CARRYCLEAR(partial);
261         if (partial != eqcr->cursor)
262                 eqcr->vbit ^= QM_EQCR_VERB_VBIT;
263 }
264
265 static inline struct qm_eqcr_entry *qm_eqcr_start_no_stash(struct qm_portal
266                                                                  *portal)
267 {
268         register struct qm_eqcr *eqcr = &portal->eqcr;
269
270 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
271         DPAA_ASSERT(!eqcr->busy);
272 #endif
273         if (!eqcr->available)
274                 return NULL;
275
276 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
277         eqcr->busy = 1;
278 #endif
279
280         return eqcr->cursor;
281 }
282
283 static inline struct qm_eqcr_entry *qm_eqcr_start_stash(struct qm_portal
284                                                                 *portal)
285 {
286         register struct qm_eqcr *eqcr = &portal->eqcr;
287         u8 diff, old_ci;
288
289 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
290         DPAA_ASSERT(!eqcr->busy);
291 #endif
292         if (!eqcr->available) {
293                 old_ci = eqcr->ci;
294                 eqcr->ci = qm_cl_in(EQCR_CI) & (QM_EQCR_SIZE - 1);
295                 diff = qm_cyc_diff(QM_EQCR_SIZE, old_ci, eqcr->ci);
296                 eqcr->available += diff;
297                 if (!diff)
298                         return NULL;
299         }
300 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
301         eqcr->busy = 1;
302 #endif
303         return eqcr->cursor;
304 }
305
306 static inline void qm_eqcr_abort(struct qm_portal *portal)
307 {
308         __maybe_unused register struct qm_eqcr *eqcr = &portal->eqcr;
309
310 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
311         DPAA_ASSERT(eqcr->busy);
312         eqcr->busy = 0;
313 #endif
314 }
315
316 static inline struct qm_eqcr_entry *qm_eqcr_pend_and_next(
317                                         struct qm_portal *portal, u8 myverb)
318 {
319         register struct qm_eqcr *eqcr = &portal->eqcr;
320
321 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
322         DPAA_ASSERT(eqcr->busy);
323         DPAA_ASSERT(eqcr->pmode != qm_eqcr_pvb);
324 #endif
325         if (eqcr->available == 1)
326                 return NULL;
327         eqcr->cursor->__dont_write_directly__verb = myverb | eqcr->vbit;
328         dcbf(eqcr->cursor);
329         EQCR_INC(eqcr);
330         eqcr->available--;
331         return eqcr->cursor;
332 }
333
334 #define EQCR_COMMIT_CHECKS(eqcr) \
335 do { \
336         DPAA_ASSERT(eqcr->busy); \
337         DPAA_ASSERT(eqcr->cursor->orp == (eqcr->cursor->orp & 0x00ffffff)); \
338         DPAA_ASSERT(eqcr->cursor->fqid == (eqcr->cursor->fqid & 0x00ffffff)); \
339 } while (0)
340
341 static inline void qm_eqcr_pci_commit(struct qm_portal *portal, u8 myverb)
342 {
343         register struct qm_eqcr *eqcr = &portal->eqcr;
344
345 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
346         EQCR_COMMIT_CHECKS(eqcr);
347         DPAA_ASSERT(eqcr->pmode == qm_eqcr_pci);
348 #endif
349         eqcr->cursor->__dont_write_directly__verb = myverb | eqcr->vbit;
350         EQCR_INC(eqcr);
351         eqcr->available--;
352         dcbf(eqcr->cursor);
353         hwsync();
354         qm_out(EQCR_PI_CINH, EQCR_PTR2IDX(eqcr->cursor));
355 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
356         eqcr->busy = 0;
357 #endif
358 }
359
360 static inline void qm_eqcr_pce_prefetch(struct qm_portal *portal)
361 {
362         __maybe_unused register struct qm_eqcr *eqcr = &portal->eqcr;
363
364 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
365         DPAA_ASSERT(eqcr->pmode == qm_eqcr_pce);
366 #endif
367         qm_cl_invalidate(EQCR_PI);
368         qm_cl_touch_rw(EQCR_PI);
369 }
370
371 static inline void qm_eqcr_pce_commit(struct qm_portal *portal, u8 myverb)
372 {
373         register struct qm_eqcr *eqcr = &portal->eqcr;
374
375 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
376         EQCR_COMMIT_CHECKS(eqcr);
377         DPAA_ASSERT(eqcr->pmode == qm_eqcr_pce);
378 #endif
379         eqcr->cursor->__dont_write_directly__verb = myverb | eqcr->vbit;
380         EQCR_INC(eqcr);
381         eqcr->available--;
382         dcbf(eqcr->cursor);
383         lwsync();
384         qm_cl_out(EQCR_PI, EQCR_PTR2IDX(eqcr->cursor));
385 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
386         eqcr->busy = 0;
387 #endif
388 }
389
390 static inline void qm_eqcr_pvb_commit(struct qm_portal *portal, u8 myverb)
391 {
392         register struct qm_eqcr *eqcr = &portal->eqcr;
393         struct qm_eqcr_entry *eqcursor;
394
395 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
396         EQCR_COMMIT_CHECKS(eqcr);
397         DPAA_ASSERT(eqcr->pmode == qm_eqcr_pvb);
398 #endif
399         lwsync();
400         eqcursor = eqcr->cursor;
401         eqcursor->__dont_write_directly__verb = myverb | eqcr->vbit;
402         dcbf(eqcursor);
403         EQCR_INC(eqcr);
404         eqcr->available--;
405 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
406         eqcr->busy = 0;
407 #endif
408 }
409
410 static inline u8 qm_eqcr_cci_update(struct qm_portal *portal)
411 {
412         register struct qm_eqcr *eqcr = &portal->eqcr;
413         u8 diff, old_ci = eqcr->ci;
414
415         eqcr->ci = qm_in(EQCR_CI_CINH) & (QM_EQCR_SIZE - 1);
416         diff = qm_cyc_diff(QM_EQCR_SIZE, old_ci, eqcr->ci);
417         eqcr->available += diff;
418         return diff;
419 }
420
421 static inline void qm_eqcr_cce_prefetch(struct qm_portal *portal)
422 {
423         __maybe_unused register struct qm_eqcr *eqcr = &portal->eqcr;
424
425         qm_cl_touch_ro(EQCR_CI);
426 }
427
428 static inline u8 qm_eqcr_cce_update(struct qm_portal *portal)
429 {
430         register struct qm_eqcr *eqcr = &portal->eqcr;
431         u8 diff, old_ci = eqcr->ci;
432
433         eqcr->ci = qm_cl_in(EQCR_CI) & (QM_EQCR_SIZE - 1);
434         qm_cl_invalidate(EQCR_CI);
435         diff = qm_cyc_diff(QM_EQCR_SIZE, old_ci, eqcr->ci);
436         eqcr->available += diff;
437         return diff;
438 }
439
440 static inline u8 qm_eqcr_get_ithresh(struct qm_portal *portal)
441 {
442         register struct qm_eqcr *eqcr = &portal->eqcr;
443
444         return eqcr->ithresh;
445 }
446
447 static inline void qm_eqcr_set_ithresh(struct qm_portal *portal, u8 ithresh)
448 {
449         register struct qm_eqcr *eqcr = &portal->eqcr;
450
451         eqcr->ithresh = ithresh;
452         qm_out(EQCR_ITR, ithresh);
453 }
454
455 static inline u8 qm_eqcr_get_avail(struct qm_portal *portal)
456 {
457         register struct qm_eqcr *eqcr = &portal->eqcr;
458
459         return eqcr->available;
460 }
461
462 static inline u8 qm_eqcr_get_fill(struct qm_portal *portal)
463 {
464         register struct qm_eqcr *eqcr = &portal->eqcr;
465
466         return QM_EQCR_SIZE - 1 - eqcr->available;
467 }
468
469 #define DQRR_CARRYCLEAR(p) \
470         (void *)((unsigned long)(p) & (~(unsigned long)(QM_DQRR_SIZE << 6)))
471
472 static inline u8 DQRR_PTR2IDX(const struct qm_dqrr_entry *e)
473 {
474         return ((uintptr_t)e >> 6) & (QM_DQRR_SIZE - 1);
475 }
476
477 static inline const struct qm_dqrr_entry *DQRR_INC(
478                                                 const struct qm_dqrr_entry *e)
479 {
480         return DQRR_CARRYCLEAR(e + 1);
481 }
482
483 static inline void qm_dqrr_set_maxfill(struct qm_portal *portal, u8 mf)
484 {
485         qm_out(CFG, (qm_in(CFG) & 0xff0fffff) |
486                 ((mf & (QM_DQRR_SIZE - 1)) << 20));
487 }
488
489 static inline const struct qm_dqrr_entry *qm_dqrr_current(
490                                                 struct qm_portal *portal)
491 {
492         register struct qm_dqrr *dqrr = &portal->dqrr;
493
494         if (!dqrr->fill)
495                 return NULL;
496         return dqrr->cursor;
497 }
498
499 static inline u8 qm_dqrr_cursor(struct qm_portal *portal)
500 {
501         register struct qm_dqrr *dqrr = &portal->dqrr;
502
503         return DQRR_PTR2IDX(dqrr->cursor);
504 }
505
506 static inline u8 qm_dqrr_next(struct qm_portal *portal)
507 {
508         register struct qm_dqrr *dqrr = &portal->dqrr;
509
510         DPAA_ASSERT(dqrr->fill);
511         dqrr->cursor = DQRR_INC(dqrr->cursor);
512         return --dqrr->fill;
513 }
514
515 static inline u8 qm_dqrr_pci_update(struct qm_portal *portal)
516 {
517         register struct qm_dqrr *dqrr = &portal->dqrr;
518         u8 diff, old_pi = dqrr->pi;
519
520 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
521         DPAA_ASSERT(dqrr->pmode == qm_dqrr_pci);
522 #endif
523         dqrr->pi = qm_in(DQRR_PI_CINH) & (QM_DQRR_SIZE - 1);
524         diff = qm_cyc_diff(QM_DQRR_SIZE, old_pi, dqrr->pi);
525         dqrr->fill += diff;
526         return diff;
527 }
528
529 static inline void qm_dqrr_pce_prefetch(struct qm_portal *portal)
530 {
531         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
532
533 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
534         DPAA_ASSERT(dqrr->pmode == qm_dqrr_pce);
535 #endif
536         qm_cl_invalidate(DQRR_PI);
537         qm_cl_touch_ro(DQRR_PI);
538 }
539
540 static inline u8 qm_dqrr_pce_update(struct qm_portal *portal)
541 {
542         register struct qm_dqrr *dqrr = &portal->dqrr;
543         u8 diff, old_pi = dqrr->pi;
544
545 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
546         DPAA_ASSERT(dqrr->pmode == qm_dqrr_pce);
547 #endif
548         dqrr->pi = qm_cl_in(DQRR_PI) & (QM_DQRR_SIZE - 1);
549         diff = qm_cyc_diff(QM_DQRR_SIZE, old_pi, dqrr->pi);
550         dqrr->fill += diff;
551         return diff;
552 }
553
554 static inline void qm_dqrr_pvb_update(struct qm_portal *portal)
555 {
556         register struct qm_dqrr *dqrr = &portal->dqrr;
557         const struct qm_dqrr_entry *res = qm_cl(dqrr->ring, dqrr->pi);
558
559 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
560         DPAA_ASSERT(dqrr->pmode == qm_dqrr_pvb);
561 #endif
562         /* when accessing 'verb', use __raw_readb() to ensure that compiler
563          * inlining doesn't try to optimise out "excess reads".
564          */
565         if ((__raw_readb(&res->verb) & QM_DQRR_VERB_VBIT) == dqrr->vbit) {
566                 dqrr->pi = (dqrr->pi + 1) & (QM_DQRR_SIZE - 1);
567                 if (!dqrr->pi)
568                         dqrr->vbit ^= QM_DQRR_VERB_VBIT;
569                 dqrr->fill++;
570         }
571 }
572
573 static inline void qm_dqrr_cci_consume(struct qm_portal *portal, u8 num)
574 {
575         register struct qm_dqrr *dqrr = &portal->dqrr;
576
577 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
578         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cci);
579 #endif
580         dqrr->ci = (dqrr->ci + num) & (QM_DQRR_SIZE - 1);
581         qm_out(DQRR_CI_CINH, dqrr->ci);
582 }
583
584 static inline void qm_dqrr_cci_consume_to_current(struct qm_portal *portal)
585 {
586         register struct qm_dqrr *dqrr = &portal->dqrr;
587
588 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
589         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cci);
590 #endif
591         dqrr->ci = DQRR_PTR2IDX(dqrr->cursor);
592         qm_out(DQRR_CI_CINH, dqrr->ci);
593 }
594
595 static inline void qm_dqrr_cce_prefetch(struct qm_portal *portal)
596 {
597         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
598
599 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
600         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cce);
601 #endif
602         qm_cl_invalidate(DQRR_CI);
603         qm_cl_touch_rw(DQRR_CI);
604 }
605
606 static inline void qm_dqrr_cce_consume(struct qm_portal *portal, u8 num)
607 {
608         register struct qm_dqrr *dqrr = &portal->dqrr;
609
610 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
611         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cce);
612 #endif
613         dqrr->ci = (dqrr->ci + num) & (QM_DQRR_SIZE - 1);
614         qm_cl_out(DQRR_CI, dqrr->ci);
615 }
616
617 static inline void qm_dqrr_cce_consume_to_current(struct qm_portal *portal)
618 {
619         register struct qm_dqrr *dqrr = &portal->dqrr;
620
621 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
622         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cce);
623 #endif
624         dqrr->ci = DQRR_PTR2IDX(dqrr->cursor);
625         qm_cl_out(DQRR_CI, dqrr->ci);
626 }
627
628 static inline void qm_dqrr_cdc_consume_1(struct qm_portal *portal, u8 idx,
629                                          int park)
630 {
631         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
632
633 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
634         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
635 #endif
636         DPAA_ASSERT(idx < QM_DQRR_SIZE);
637         qm_out(DQRR_DCAP, (0 << 8) |    /* S */
638                 ((park ? 1 : 0) << 6) | /* PK */
639                 idx);                   /* DCAP_CI */
640 }
641
642 static inline void qm_dqrr_cdc_consume_1ptr(struct qm_portal *portal,
643                                             const struct qm_dqrr_entry *dq,
644                                         int park)
645 {
646         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
647         u8 idx = DQRR_PTR2IDX(dq);
648
649 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
650         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
651 #endif
652         DPAA_ASSERT(idx < QM_DQRR_SIZE);
653         qm_out(DQRR_DCAP, (0 << 8) |            /* DQRR_DCAP::S */
654                 ((park ? 1 : 0) << 6) |         /* DQRR_DCAP::PK */
655                 idx);                           /* DQRR_DCAP::DCAP_CI */
656 }
657
658 static inline void qm_dqrr_cdc_consume_n(struct qm_portal *portal, u16 bitmask)
659 {
660         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
661
662 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
663         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
664 #endif
665         qm_out(DQRR_DCAP, (1 << 8) |            /* DQRR_DCAP::S */
666                 ((u32)bitmask << 16));          /* DQRR_DCAP::DCAP_CI */
667         dqrr->ci = qm_in(DQRR_CI_CINH) & (QM_DQRR_SIZE - 1);
668         dqrr->fill = qm_cyc_diff(QM_DQRR_SIZE, dqrr->ci, dqrr->pi);
669 }
670
671 static inline u8 qm_dqrr_cdc_cci(struct qm_portal *portal)
672 {
673         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
674
675 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
676         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
677 #endif
678         return qm_in(DQRR_CI_CINH) & (QM_DQRR_SIZE - 1);
679 }
680
681 static inline void qm_dqrr_cdc_cce_prefetch(struct qm_portal *portal)
682 {
683         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
684
685 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
686         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
687 #endif
688         qm_cl_invalidate(DQRR_CI);
689         qm_cl_touch_ro(DQRR_CI);
690 }
691
692 static inline u8 qm_dqrr_cdc_cce(struct qm_portal *portal)
693 {
694         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
695
696 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
697         DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
698 #endif
699         return qm_cl_in(DQRR_CI) & (QM_DQRR_SIZE - 1);
700 }
701
702 static inline u8 qm_dqrr_get_ci(struct qm_portal *portal)
703 {
704         register struct qm_dqrr *dqrr = &portal->dqrr;
705
706 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
707         DPAA_ASSERT(dqrr->cmode != qm_dqrr_cdc);
708 #endif
709         return dqrr->ci;
710 }
711
712 static inline void qm_dqrr_park(struct qm_portal *portal, u8 idx)
713 {
714         __maybe_unused register struct qm_dqrr *dqrr = &portal->dqrr;
715
716 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
717         DPAA_ASSERT(dqrr->cmode != qm_dqrr_cdc);
718 #endif
719         qm_out(DQRR_DCAP, (0 << 8) |            /* S */
720                 (1 << 6) |                      /* PK */
721                 (idx & (QM_DQRR_SIZE - 1)));    /* DCAP_CI */
722 }
723
724 static inline void qm_dqrr_park_current(struct qm_portal *portal)
725 {
726         register struct qm_dqrr *dqrr = &portal->dqrr;
727
728 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
729         DPAA_ASSERT(dqrr->cmode != qm_dqrr_cdc);
730 #endif
731         qm_out(DQRR_DCAP, (0 << 8) |            /* S */
732                 (1 << 6) |                      /* PK */
733                 DQRR_PTR2IDX(dqrr->cursor));    /* DCAP_CI */
734 }
735
736 static inline void qm_dqrr_sdqcr_set(struct qm_portal *portal, u32 sdqcr)
737 {
738         qm_out(DQRR_SDQCR, sdqcr);
739 }
740
741 static inline u32 qm_dqrr_sdqcr_get(struct qm_portal *portal)
742 {
743         return qm_in(DQRR_SDQCR);
744 }
745
746 static inline void qm_dqrr_vdqcr_set(struct qm_portal *portal, u32 vdqcr)
747 {
748         qm_out(DQRR_VDQCR, vdqcr);
749 }
750
751 static inline u32 qm_dqrr_vdqcr_get(struct qm_portal *portal)
752 {
753         return qm_in(DQRR_VDQCR);
754 }
755
756 static inline u8 qm_dqrr_get_ithresh(struct qm_portal *portal)
757 {
758         register struct qm_dqrr *dqrr = &portal->dqrr;
759
760         return dqrr->ithresh;
761 }
762
763 static inline void qm_dqrr_set_ithresh(struct qm_portal *portal, u8 ithresh)
764 {
765         qm_out(DQRR_ITR, ithresh);
766 }
767
768 static inline u8 qm_dqrr_get_maxfill(struct qm_portal *portal)
769 {
770         return (qm_in(CFG) & 0x00f00000) >> 20;
771 }
772
773 /* -------------- */
774 /* --- MR API --- */
775
776 #define MR_CARRYCLEAR(p) \
777         (void *)((unsigned long)(p) & (~(unsigned long)(QM_MR_SIZE << 6)))
778
779 static inline u8 MR_PTR2IDX(const struct qm_mr_entry *e)
780 {
781         return ((uintptr_t)e >> 6) & (QM_MR_SIZE - 1);
782 }
783
784 static inline const struct qm_mr_entry *MR_INC(const struct qm_mr_entry *e)
785 {
786         return MR_CARRYCLEAR(e + 1);
787 }
788
789 static inline void qm_mr_finish(struct qm_portal *portal)
790 {
791         register struct qm_mr *mr = &portal->mr;
792
793         if (mr->ci != MR_PTR2IDX(mr->cursor))
794                 pr_crit("Ignoring completed MR entries\n");
795 }
796
797 static inline const struct qm_mr_entry *qm_mr_current(struct qm_portal *portal)
798 {
799         register struct qm_mr *mr = &portal->mr;
800
801         if (!mr->fill)
802                 return NULL;
803         return mr->cursor;
804 }
805
806 static inline u8 qm_mr_next(struct qm_portal *portal)
807 {
808         register struct qm_mr *mr = &portal->mr;
809
810         DPAA_ASSERT(mr->fill);
811         mr->cursor = MR_INC(mr->cursor);
812         return --mr->fill;
813 }
814
815 static inline void qm_mr_cci_consume(struct qm_portal *portal, u8 num)
816 {
817         register struct qm_mr *mr = &portal->mr;
818
819 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
820         DPAA_ASSERT(mr->cmode == qm_mr_cci);
821 #endif
822         mr->ci = (mr->ci + num) & (QM_MR_SIZE - 1);
823         qm_out(MR_CI_CINH, mr->ci);
824 }
825
826 static inline void qm_mr_cci_consume_to_current(struct qm_portal *portal)
827 {
828         register struct qm_mr *mr = &portal->mr;
829
830 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
831         DPAA_ASSERT(mr->cmode == qm_mr_cci);
832 #endif
833         mr->ci = MR_PTR2IDX(mr->cursor);
834         qm_out(MR_CI_CINH, mr->ci);
835 }
836
837 static inline void qm_mr_set_ithresh(struct qm_portal *portal, u8 ithresh)
838 {
839         qm_out(MR_ITR, ithresh);
840 }
841
842 /* ------------------------------ */
843 /* --- Management command API --- */
844 static inline int qm_mc_init(struct qm_portal *portal)
845 {
846         register struct qm_mc *mc = &portal->mc;
847
848         mc->cr = portal->addr.ce + QM_CL_CR;
849         mc->rr = portal->addr.ce + QM_CL_RR0;
850         mc->rridx = (__raw_readb(&mc->cr->__dont_write_directly__verb) &
851                         QM_MCC_VERB_VBIT) ?  0 : 1;
852         mc->vbit = mc->rridx ? QM_MCC_VERB_VBIT : 0;
853 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
854         mc->state = qman_mc_idle;
855 #endif
856         return 0;
857 }
858
859 static inline void qm_mc_finish(struct qm_portal *portal)
860 {
861         __maybe_unused register struct qm_mc *mc = &portal->mc;
862
863 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
864         DPAA_ASSERT(mc->state == qman_mc_idle);
865         if (mc->state != qman_mc_idle)
866                 pr_crit("Losing incomplete MC command\n");
867 #endif
868 }
869
870 static inline struct qm_mc_command *qm_mc_start(struct qm_portal *portal)
871 {
872         register struct qm_mc *mc = &portal->mc;
873
874 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
875         DPAA_ASSERT(mc->state == qman_mc_idle);
876         mc->state = qman_mc_user;
877 #endif
878         dcbz_64(mc->cr);
879         return mc->cr;
880 }
881
882 static inline void qm_mc_commit(struct qm_portal *portal, u8 myverb)
883 {
884         register struct qm_mc *mc = &portal->mc;
885         struct qm_mc_result *rr = mc->rr + mc->rridx;
886
887 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
888         DPAA_ASSERT(mc->state == qman_mc_user);
889 #endif
890         lwsync();
891         mc->cr->__dont_write_directly__verb = myverb | mc->vbit;
892         dcbf(mc->cr);
893         dcbit_ro(rr);
894 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
895         mc->state = qman_mc_hw;
896 #endif
897 }
898
899 static inline struct qm_mc_result *qm_mc_result(struct qm_portal *portal)
900 {
901         register struct qm_mc *mc = &portal->mc;
902         struct qm_mc_result *rr = mc->rr + mc->rridx;
903
904 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
905         DPAA_ASSERT(mc->state == qman_mc_hw);
906 #endif
907         /* The inactive response register's verb byte always returns zero until
908          * its command is submitted and completed. This includes the valid-bit,
909          * in case you were wondering.
910          */
911         if (!__raw_readb(&rr->verb)) {
912                 dcbit_ro(rr);
913                 return NULL;
914         }
915         mc->rridx ^= 1;
916         mc->vbit ^= QM_MCC_VERB_VBIT;
917 #ifdef RTE_LIBRTE_DPAA_HWDEBUG
918         mc->state = qman_mc_idle;
919 #endif
920         return rr;
921 }
922
923 /* Portal interrupt register API */
924 static inline void qm_isr_set_iperiod(struct qm_portal *portal, u16 iperiod)
925 {
926         qm_out(ITPR, iperiod);
927 }
928
929 static inline u32 __qm_isr_read(struct qm_portal *portal, enum qm_isr_reg n)
930 {
931 #if defined(RTE_ARCH_ARM64)
932         return __qm_in(&portal->addr, QM_REG_ISR + (n << 6));
933 #else
934         return __qm_in(&portal->addr, QM_REG_ISR + (n << 2));
935 #endif
936 }
937
938 static inline void __qm_isr_write(struct qm_portal *portal, enum qm_isr_reg n,
939                                   u32 val)
940 {
941 #if defined(RTE_ARCH_ARM64)
942         __qm_out(&portal->addr, QM_REG_ISR + (n << 6), val);
943 #else
944         __qm_out(&portal->addr, QM_REG_ISR + (n << 2), val);
945 #endif
946 }