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