New upstream version 18.08
[deb_dpdk.git] / drivers / bus / fslmc / qbman / qbman_sys.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
4  *
5  */
6 /* qbman_sys_decl.h and qbman_sys.h are the two platform-specific files in the
7  * driver. They are only included via qbman_private.h, which is itself a
8  * platform-independent file and is included by all the other driver source.
9  *
10  * qbman_sys_decl.h is included prior to all other declarations and logic, and
11  * it exists to provide compatibility with any linux interfaces our
12  * single-source driver code is dependent on (eg. kmalloc). Ie. this file
13  * provides linux compatibility.
14  *
15  * This qbman_sys.h header, on the other hand, is included *after* any common
16  * and platform-neutral declarations and logic in qbman_private.h, and exists to
17  * implement any platform-specific logic of the qbman driver itself. Ie. it is
18  * *not* to provide linux compatibility.
19  */
20
21 #include "qbman_sys_decl.h"
22
23 #define CENA_WRITE_ENABLE 0
24 #define CINH_WRITE_ENABLE 1
25
26 /* Debugging assists */
27 static inline void __hexdump(unsigned long start, unsigned long end,
28                              unsigned long p, size_t sz, const unsigned char *c)
29 {
30         while (start < end) {
31                 unsigned int pos = 0;
32                 char buf[64];
33                 int nl = 0;
34
35                 pos += sprintf(buf + pos, "%08lx: ", start);
36                 do {
37                         if ((start < p) || (start >= (p + sz)))
38                                 pos += sprintf(buf + pos, "..");
39                         else
40                                 pos += sprintf(buf + pos, "%02x", *(c++));
41                         if (!(++start & 15)) {
42                                 buf[pos++] = '\n';
43                                 nl = 1;
44                         } else {
45                                 nl = 0;
46                                 if (!(start & 1))
47                                         buf[pos++] = ' ';
48                                 if (!(start & 3))
49                                         buf[pos++] = ' ';
50                         }
51                 } while (start & 15);
52                 if (!nl)
53                         buf[pos++] = '\n';
54                 buf[pos] = '\0';
55                 pr_info("%s", buf);
56         }
57 }
58
59 static inline void hexdump(const void *ptr, size_t sz)
60 {
61         unsigned long p = (unsigned long)ptr;
62         unsigned long start = p & ~15;
63         unsigned long end = (p + sz + 15) & ~15;
64         const unsigned char *c = ptr;
65
66         __hexdump(start, end, p, sz, c);
67 }
68
69 /* Currently, the CENA support code expects each 32-bit word to be written in
70  * host order, and these are converted to hardware (little-endian) order on
71  * command submission. However, 64-bit quantities are must be written (and read)
72  * as two 32-bit words with the least-significant word first, irrespective of
73  * host endianness.
74  */
75 static inline void u64_to_le32_copy(void *d, const uint64_t *s,
76                                     unsigned int cnt)
77 {
78         uint32_t *dd = d;
79         const uint32_t *ss = (const uint32_t *)s;
80
81         while (cnt--) {
82                 /* TBD: the toolchain was choking on the use of 64-bit types up
83                  * until recently so this works entirely with 32-bit variables.
84                  * When 64-bit types become usable again, investigate better
85                  * ways of doing this.
86                  */
87 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
88                 *(dd++) = ss[1];
89                 *(dd++) = ss[0];
90                 ss += 2;
91 #else
92                 *(dd++) = *(ss++);
93                 *(dd++) = *(ss++);
94 #endif
95         }
96 }
97
98 static inline void u64_from_le32_copy(uint64_t *d, const void *s,
99                                       unsigned int cnt)
100 {
101         const uint32_t *ss = s;
102         uint32_t *dd = (uint32_t *)d;
103
104         while (cnt--) {
105 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
106                 dd[1] = *(ss++);
107                 dd[0] = *(ss++);
108                 dd += 2;
109 #else
110                 *(dd++) = *(ss++);
111                 *(dd++) = *(ss++);
112 #endif
113         }
114 }
115
116         /******************/
117         /* Portal access  */
118         /******************/
119 struct qbman_swp_sys {
120         /* On GPP, the sys support for qbman_swp is here. The CENA region isi
121          * not an mmap() of the real portal registers, but an allocated
122          * place-holder, because the actual writes/reads to/from the portal are
123          * marshalled from these allocated areas using QBMan's "MC access
124          * registers". CINH accesses are atomic so there's no need for a
125          * place-holder.
126          */
127         uint8_t *cena;
128         uint8_t __iomem *addr_cena;
129         uint8_t __iomem *addr_cinh;
130         uint32_t idx;
131         enum qbman_eqcr_mode eqcr_mode;
132 };
133
134 /* P_OFFSET is (ACCESS_CMD,0,12) - offset within the portal
135  * C is (ACCESS_CMD,12,1) - is inhibited? (0==CENA, 1==CINH)
136  * SWP_IDX is (ACCESS_CMD,16,10) - Software portal index
137  * P is (ACCESS_CMD,28,1) - (0==special portal, 1==any portal)
138  * T is (ACCESS_CMD,29,1) - Command type (0==READ, 1==WRITE)
139  * E is (ACCESS_CMD,31,1) - Command execute (1 to issue, poll for 0==complete)
140  */
141
142 static inline void qbman_cinh_write(struct qbman_swp_sys *s, uint32_t offset,
143                                     uint32_t val)
144 {
145         __raw_writel(val, s->addr_cinh + offset);
146 #ifdef QBMAN_CINH_TRACE
147         pr_info("qbman_cinh_write(%p:%d:0x%03x) 0x%08x\n",
148                 s->addr_cinh, s->idx, offset, val);
149 #endif
150 }
151
152 static inline uint32_t qbman_cinh_read(struct qbman_swp_sys *s, uint32_t offset)
153 {
154         uint32_t reg = __raw_readl(s->addr_cinh + offset);
155 #ifdef QBMAN_CINH_TRACE
156         pr_info("qbman_cinh_read(%p:%d:0x%03x) 0x%08x\n",
157                 s->addr_cinh, s->idx, offset, reg);
158 #endif
159         return reg;
160 }
161
162 static inline void *qbman_cena_write_start(struct qbman_swp_sys *s,
163                                            uint32_t offset)
164 {
165         void *shadow = s->cena + offset;
166
167 #ifdef QBMAN_CENA_TRACE
168         pr_info("qbman_cena_write_start(%p:%d:0x%03x) %p\n",
169                 s->addr_cena, s->idx, offset, shadow);
170 #endif
171         QBMAN_BUG_ON(offset & 63);
172         dcbz(shadow);
173         return shadow;
174 }
175
176 static inline void *qbman_cena_write_start_wo_shadow(struct qbman_swp_sys *s,
177                                                      uint32_t offset)
178 {
179 #ifdef QBMAN_CENA_TRACE
180         pr_info("qbman_cena_write_start(%p:%d:0x%03x)\n",
181                 s->addr_cena, s->idx, offset);
182 #endif
183         QBMAN_BUG_ON(offset & 63);
184 #ifdef RTE_ARCH_64
185         return (s->addr_cena + offset);
186 #else
187         return (s->addr_cinh + offset);
188 #endif
189 }
190
191 static inline void qbman_cena_write_complete(struct qbman_swp_sys *s,
192                                              uint32_t offset, void *cmd)
193 {
194         const uint32_t *shadow = cmd;
195         int loop;
196 #ifdef QBMAN_CENA_TRACE
197         pr_info("qbman_cena_write_complete(%p:%d:0x%03x) %p\n",
198                 s->addr_cena, s->idx, offset, shadow);
199         hexdump(cmd, 64);
200 #endif
201 #ifdef RTE_ARCH_64
202         for (loop = 15; loop >= 1; loop--)
203                 __raw_writel(shadow[loop], s->addr_cena +
204                                          offset + loop * 4);
205         lwsync();
206                 __raw_writel(shadow[0], s->addr_cena + offset);
207 #else
208         for (loop = 15; loop >= 1; loop--)
209                 __raw_writel(shadow[loop], s->addr_cinh +
210                                          offset + loop * 4);
211         lwsync();
212         __raw_writel(shadow[0], s->addr_cinh + offset);
213 #endif
214         dcbf(s->addr_cena + offset);
215 }
216
217 static inline void qbman_cena_write_complete_wo_shadow(struct qbman_swp_sys *s,
218                                                        uint32_t offset)
219 {
220 #ifdef QBMAN_CENA_TRACE
221         pr_info("qbman_cena_write_complete(%p:%d:0x%03x)\n",
222                 s->addr_cena, s->idx, offset);
223 #endif
224         dcbf(s->addr_cena + offset);
225 }
226
227 static inline uint32_t qbman_cena_read_reg(struct qbman_swp_sys *s,
228                                            uint32_t offset)
229 {
230         return __raw_readl(s->addr_cena + offset);
231 }
232
233 static inline void *qbman_cena_read(struct qbman_swp_sys *s, uint32_t offset)
234 {
235         uint32_t *shadow = (uint32_t *)(s->cena + offset);
236         unsigned int loop;
237 #ifdef QBMAN_CENA_TRACE
238         pr_info("qbman_cena_read(%p:%d:0x%03x) %p\n",
239                 s->addr_cena, s->idx, offset, shadow);
240 #endif
241
242 #ifdef RTE_ARCH_64
243         for (loop = 0; loop < 16; loop++)
244                 shadow[loop] = __raw_readl(s->addr_cena + offset
245                                         + loop * 4);
246 #else
247         for (loop = 0; loop < 16; loop++)
248                 shadow[loop] = __raw_readl(s->addr_cinh + offset
249                                         + loop * 4);
250 #endif
251 #ifdef QBMAN_CENA_TRACE
252         hexdump(shadow, 64);
253 #endif
254         return shadow;
255 }
256
257 static inline void *qbman_cena_read_wo_shadow(struct qbman_swp_sys *s,
258                                               uint32_t offset)
259 {
260 #ifdef QBMAN_CENA_TRACE
261         pr_info("qbman_cena_read(%p:%d:0x%03x)\n",
262                 s->addr_cena, s->idx, offset);
263 #endif
264         return s->addr_cena + offset;
265 }
266
267 static inline void qbman_cena_invalidate(struct qbman_swp_sys *s,
268                                          uint32_t offset)
269 {
270         dccivac(s->addr_cena + offset);
271 }
272
273 static inline void qbman_cena_invalidate_prefetch(struct qbman_swp_sys *s,
274                                                   uint32_t offset)
275 {
276         dccivac(s->addr_cena + offset);
277         prefetch_for_load(s->addr_cena + offset);
278 }
279
280 static inline void qbman_cena_prefetch(struct qbman_swp_sys *s,
281                                        uint32_t offset)
282 {
283         prefetch_for_load(s->addr_cena + offset);
284 }
285
286         /******************/
287         /* Portal support */
288         /******************/
289
290 /* The SWP_CFG portal register is special, in that it is used by the
291  * platform-specific code rather than the platform-independent code in
292  * qbman_portal.c. So use of it is declared locally here.
293  */
294 #define QBMAN_CINH_SWP_CFG   0xd00
295 #define QBMAN_CINH_SWP_CFG   0xd00
296 #define SWP_CFG_DQRR_MF_SHIFT 20
297 #define SWP_CFG_EST_SHIFT     16
298 #define SWP_CFG_WN_SHIFT      14
299 #define SWP_CFG_RPM_SHIFT     12
300 #define SWP_CFG_DCM_SHIFT     10
301 #define SWP_CFG_EPM_SHIFT     8
302 #define SWP_CFG_SD_SHIFT      5
303 #define SWP_CFG_SP_SHIFT      4
304 #define SWP_CFG_SE_SHIFT      3
305 #define SWP_CFG_DP_SHIFT      2
306 #define SWP_CFG_DE_SHIFT      1
307 #define SWP_CFG_EP_SHIFT      0
308
309 static inline uint32_t qbman_set_swp_cfg(uint8_t max_fill, uint8_t wn,
310                                          uint8_t est, uint8_t rpm, uint8_t dcm,
311                                         uint8_t epm, int sd, int sp, int se,
312                                         int dp, int de, int ep)
313 {
314         uint32_t reg;
315
316         reg = (max_fill << SWP_CFG_DQRR_MF_SHIFT |
317                 est << SWP_CFG_EST_SHIFT |
318                 wn << SWP_CFG_WN_SHIFT |
319                 rpm << SWP_CFG_RPM_SHIFT |
320                 dcm << SWP_CFG_DCM_SHIFT |
321                 epm << SWP_CFG_EPM_SHIFT |
322                 sd << SWP_CFG_SD_SHIFT |
323                 sp << SWP_CFG_SP_SHIFT |
324                 se << SWP_CFG_SE_SHIFT |
325                 dp << SWP_CFG_DP_SHIFT |
326                 de << SWP_CFG_DE_SHIFT |
327                 ep << SWP_CFG_EP_SHIFT);
328
329         return reg;
330 }
331
332 static inline int qbman_swp_sys_init(struct qbman_swp_sys *s,
333                                      const struct qbman_swp_desc *d,
334                                      uint8_t dqrr_size)
335 {
336         uint32_t reg;
337 #ifdef RTE_ARCH_64
338         uint8_t wn = CENA_WRITE_ENABLE;
339 #else
340         uint8_t wn = CINH_WRITE_ENABLE;
341 #endif
342
343         s->addr_cena = d->cena_bar;
344         s->addr_cinh = d->cinh_bar;
345         s->idx = (uint32_t)d->idx;
346         s->cena = malloc(4096);
347         if (!s->cena) {
348                 pr_err("Could not allocate page for cena shadow\n");
349                 return -1;
350         }
351         s->eqcr_mode = d->eqcr_mode;
352         QBMAN_BUG_ON(d->idx < 0);
353 #ifdef QBMAN_CHECKING
354         /* We should never be asked to initialise for a portal that isn't in
355          * the power-on state. (Ie. don't forget to reset portals when they are
356          * decommissioned!)
357          */
358         reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
359         QBMAN_BUG_ON(reg);
360 #endif
361         if (s->eqcr_mode == qman_eqcr_vb_array)
362                 reg = qbman_set_swp_cfg(dqrr_size, wn, 0, 3, 2, 3, 1, 1, 1, 1,
363                                         1, 1);
364         else
365                 reg = qbman_set_swp_cfg(dqrr_size, wn, 1, 3, 2, 2, 1, 1, 1, 1,
366                                         1, 1);
367         qbman_cinh_write(s, QBMAN_CINH_SWP_CFG, reg);
368         reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
369         if (!reg) {
370                 pr_err("The portal %d is not enabled!\n", s->idx);
371                 free(s->cena);
372                 return -1;
373         }
374         return 0;
375 }
376
377 static inline void qbman_swp_sys_finish(struct qbman_swp_sys *s)
378 {
379         free(s->cena);
380 }