5dbcaa574418d154d35b146dd0ac77dd45c85b1e
[deb_dpdk.git] / drivers / bus / fslmc / qbman / qbman_sys.h
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of Freescale Semiconductor nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 /* qbman_sys_decl.h and qbman_sys.h are the two platform-specific files in the
29  * driver. They are only included via qbman_private.h, which is itself a
30  * platform-independent file and is included by all the other driver source.
31  *
32  * qbman_sys_decl.h is included prior to all other declarations and logic, and
33  * it exists to provide compatibility with any linux interfaces our
34  * single-source driver code is dependent on (eg. kmalloc). Ie. this file
35  * provides linux compatibility.
36  *
37  * This qbman_sys.h header, on the other hand, is included *after* any common
38  * and platform-neutral declarations and logic in qbman_private.h, and exists to
39  * implement any platform-specific logic of the qbman driver itself. Ie. it is
40  * *not* to provide linux compatibility.
41  */
42
43 /* Trace the 3 different classes of read/write access to QBMan. #undef as
44  * required.
45  */
46 #undef QBMAN_CCSR_TRACE
47 #undef QBMAN_CINH_TRACE
48 #undef QBMAN_CENA_TRACE
49
50 static inline void word_copy(void *d, const void *s, unsigned int cnt)
51 {
52         uint32_t *dd = d;
53         const uint32_t *ss = s;
54
55         while (cnt--)
56                 *(dd++) = *(ss++);
57 }
58
59 /* Currently, the CENA support code expects each 32-bit word to be written in
60  * host order, and these are converted to hardware (little-endian) order on
61  * command submission. However, 64-bit quantities are must be written (and read)
62  * as two 32-bit words with the least-significant word first, irrespective of
63  * host endianness.
64  */
65 static inline void u64_to_le32_copy(void *d, const uint64_t *s,
66                                     unsigned int cnt)
67 {
68         uint32_t *dd = d;
69         const uint32_t *ss = (const uint32_t *)s;
70
71         while (cnt--) {
72                 /* TBD: the toolchain was choking on the use of 64-bit types up
73                  * until recently so this works entirely with 32-bit variables.
74                  * When 64-bit types become usable again, investigate better
75                  * ways of doing this.
76                  */
77 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
78                 *(dd++) = ss[1];
79                 *(dd++) = ss[0];
80                 ss += 2;
81 #else
82                 *(dd++) = *(ss++);
83                 *(dd++) = *(ss++);
84 #endif
85         }
86 }
87
88 static inline void u64_from_le32_copy(uint64_t *d, const void *s,
89                                       unsigned int cnt)
90 {
91         const uint32_t *ss = s;
92         uint32_t *dd = (uint32_t *)d;
93
94         while (cnt--) {
95 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
96                 dd[1] = *(ss++);
97                 dd[0] = *(ss++);
98                 dd += 2;
99 #else
100                 *(dd++) = *(ss++);
101                 *(dd++) = *(ss++);
102 #endif
103         }
104 }
105
106 /* Convert a host-native 32bit value into little endian */
107 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
108 static inline uint32_t make_le32(uint32_t val)
109 {
110         return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
111                 ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
112 }
113
114 static inline uint32_t make_le24(uint32_t val)
115 {
116         return (((val & 0xff) << 16) | (val & 0xff00) |
117                 ((val & 0xff0000) >> 16));
118 }
119
120 static inline void make_le32_n(uint32_t *val, unsigned int num)
121 {
122         while (num--) {
123                 *val = make_le32(*val);
124                 val++;
125         }
126 }
127
128 #else
129 #define make_le32(val) (val)
130 #define make_le24(val) (val)
131 #define make_le32_n(val, len) do {} while (0)
132 #endif
133
134         /******************/
135         /* Portal access  */
136         /******************/
137 struct qbman_swp_sys {
138         /* On GPP, the sys support for qbman_swp is here. The CENA region isi
139          * not an mmap() of the real portal registers, but an allocated
140          * place-holder, because the actual writes/reads to/from the portal are
141          * marshalled from these allocated areas using QBMan's "MC access
142          * registers". CINH accesses are atomic so there's no need for a
143          * place-holder.
144          */
145         uint8_t *cena;
146         uint8_t __iomem *addr_cena;
147         uint8_t __iomem *addr_cinh;
148         uint32_t idx;
149         enum qbman_eqcr_mode eqcr_mode;
150 };
151
152 /* P_OFFSET is (ACCESS_CMD,0,12) - offset within the portal
153  * C is (ACCESS_CMD,12,1) - is inhibited? (0==CENA, 1==CINH)
154  * SWP_IDX is (ACCESS_CMD,16,10) - Software portal index
155  * P is (ACCESS_CMD,28,1) - (0==special portal, 1==any portal)
156  * T is (ACCESS_CMD,29,1) - Command type (0==READ, 1==WRITE)
157  * E is (ACCESS_CMD,31,1) - Command execute (1 to issue, poll for 0==complete)
158  */
159
160 static inline void qbman_cinh_write(struct qbman_swp_sys *s, uint32_t offset,
161                                     uint32_t val)
162 {
163         __raw_writel(val, s->addr_cinh + offset);
164 #ifdef QBMAN_CINH_TRACE
165         pr_info("qbman_cinh_write(%p:%d:0x%03x) 0x%08x\n",
166                 s->addr_cinh, s->idx, offset, val);
167 #endif
168 }
169
170 static inline uint32_t qbman_cinh_read(struct qbman_swp_sys *s, uint32_t offset)
171 {
172         uint32_t reg = __raw_readl(s->addr_cinh + offset);
173 #ifdef QBMAN_CINH_TRACE
174         pr_info("qbman_cinh_read(%p:%d:0x%03x) 0x%08x\n",
175                 s->addr_cinh, s->idx, offset, reg);
176 #endif
177         return reg;
178 }
179
180 static inline void *qbman_cena_write_start(struct qbman_swp_sys *s,
181                                            uint32_t offset)
182 {
183         void *shadow = s->cena + offset;
184
185 #ifdef QBMAN_CENA_TRACE
186         pr_info("qbman_cena_write_start(%p:%d:0x%03x) %p\n",
187                 s->addr_cena, s->idx, offset, shadow);
188 #endif
189         QBMAN_BUG_ON(offset & 63);
190         dcbz(shadow);
191         return shadow;
192 }
193
194 static inline void *qbman_cena_write_start_wo_shadow(struct qbman_swp_sys *s,
195                                                      uint32_t offset)
196 {
197 #ifdef QBMAN_CENA_TRACE
198         pr_info("qbman_cena_write_start(%p:%d:0x%03x)\n",
199                 s->addr_cena, s->idx, offset);
200 #endif
201         QBMAN_BUG_ON(offset & 63);
202         return (s->addr_cena + offset);
203 }
204
205 static inline void qbman_cena_write_complete(struct qbman_swp_sys *s,
206                                              uint32_t offset, void *cmd)
207 {
208         const uint32_t *shadow = cmd;
209         int loop;
210 #ifdef QBMAN_CENA_TRACE
211         pr_info("qbman_cena_write_complete(%p:%d:0x%03x) %p\n",
212                 s->addr_cena, s->idx, offset, shadow);
213         hexdump(cmd, 64);
214 #endif
215         for (loop = 15; loop >= 1; loop--)
216                 __raw_writel(shadow[loop], s->addr_cena +
217                                          offset + loop * 4);
218         lwsync();
219                 __raw_writel(shadow[0], s->addr_cena + offset);
220         dcbf(s->addr_cena + offset);
221 }
222
223 static inline void qbman_cena_write_complete_wo_shadow(struct qbman_swp_sys *s,
224                                                        uint32_t offset)
225 {
226 #ifdef QBMAN_CENA_TRACE
227         pr_info("qbman_cena_write_complete(%p:%d:0x%03x)\n",
228                 s->addr_cena, s->idx, offset);
229         hexdump(cmd, 64);
230 #endif
231         dcbf(s->addr_cena + offset);
232 }
233
234 static inline uint32_t qbman_cena_read_reg(struct qbman_swp_sys *s,
235                                            uint32_t offset)
236 {
237         return __raw_readl(s->addr_cena + offset);
238 }
239
240 static inline void *qbman_cena_read(struct qbman_swp_sys *s, uint32_t offset)
241 {
242         uint32_t *shadow = (uint32_t *)(s->cena + offset);
243         unsigned int loop;
244 #ifdef QBMAN_CENA_TRACE
245         pr_info("qbman_cena_read(%p:%d:0x%03x) %p\n",
246                 s->addr_cena, s->idx, offset, shadow);
247 #endif
248
249         for (loop = 0; loop < 16; loop++)
250                 shadow[loop] = __raw_readl(s->addr_cena + offset
251                                         + loop * 4);
252 #ifdef QBMAN_CENA_TRACE
253         hexdump(shadow, 64);
254 #endif
255         return shadow;
256 }
257
258 static inline void *qbman_cena_read_wo_shadow(struct qbman_swp_sys *s,
259                                               uint32_t offset)
260 {
261 #ifdef QBMAN_CENA_TRACE
262         pr_info("qbman_cena_read(%p:%d:0x%03x) %p\n",
263                 s->addr_cena, s->idx, offset, shadow);
264 #endif
265
266 #ifdef QBMAN_CENA_TRACE
267         hexdump(shadow, 64);
268 #endif
269         return s->addr_cena + offset;
270 }
271
272 static inline void qbman_cena_invalidate(struct qbman_swp_sys *s,
273                                          uint32_t offset)
274 {
275         dccivac(s->addr_cena + offset);
276 }
277
278 static inline void qbman_cena_invalidate_prefetch(struct qbman_swp_sys *s,
279                                                   uint32_t offset)
280 {
281         dccivac(s->addr_cena + offset);
282         prefetch_for_load(s->addr_cena + offset);
283 }
284
285 static inline void qbman_cena_prefetch(struct qbman_swp_sys *s,
286                                        uint32_t offset)
287 {
288         prefetch_for_load(s->addr_cena + offset);
289 }
290
291         /******************/
292         /* Portal support */
293         /******************/
294
295 /* The SWP_CFG portal register is special, in that it is used by the
296  * platform-specific code rather than the platform-independent code in
297  * qbman_portal.c. So use of it is declared locally here.
298  */
299 #define QBMAN_CINH_SWP_CFG   0xd00
300
301 /* For MC portal use, we always configure with
302  * DQRR_MF is (SWP_CFG,20,3) - DQRR max fill (<- 0x4)
303  * EST is (SWP_CFG,16,3) - EQCR_CI stashing threshold (<- 0x2)
304  * RPM is (SWP_CFG,12,2) - RCR production notification mode (<- 0x3)
305  * DCM is (SWP_CFG,10,2) - DQRR consumption notification mode (<- 0x2)
306  * EPM is (SWP_CFG,8,2) - EQCR production notification mode (<- 0x2)
307  * SD is (SWP_CFG,5,1) - memory stashing drop enable (<- TRUE)
308  * SP is (SWP_CFG,4,1) - memory stashing priority (<- TRUE)
309  * SE is (SWP_CFG,3,1) - memory stashing enable (<- TRUE)
310  * DP is (SWP_CFG,2,1) - dequeue stashing priority (<- TRUE)
311  * DE is (SWP_CFG,1,1) - dequeue stashing enable (<- TRUE)
312  * EP is (SWP_CFG,0,1) - EQCR_CI stashing priority (<- TRUE)
313  */
314 static inline uint32_t qbman_set_swp_cfg(uint8_t max_fill, uint8_t wn,
315                                          uint8_t est, uint8_t rpm, uint8_t dcm,
316                                         uint8_t epm, int sd, int sp, int se,
317                                         int dp, int de, int ep)
318 {
319         uint32_t reg;
320
321         reg = e32_uint8_t(20, (uint32_t)(3 + (max_fill >> 3)), max_fill) |
322                 e32_uint8_t(16, 3, est) |
323                 e32_uint8_t(12, 2, rpm) | e32_uint8_t(10, 2, dcm) |
324                 e32_uint8_t(8, 2, epm) | e32_int(5, 1, sd) |
325                 e32_int(4, 1, sp) | e32_int(3, 1, se) | e32_int(2, 1, dp) |
326                 e32_int(1, 1, de) | e32_int(0, 1, ep) | e32_uint8_t(14, 1, wn);
327         return reg;
328 }
329
330 static inline int qbman_swp_sys_init(struct qbman_swp_sys *s,
331                                      const struct qbman_swp_desc *d,
332                                      uint8_t dqrr_size)
333 {
334         uint32_t reg;
335
336         s->addr_cena = d->cena_bar;
337         s->addr_cinh = d->cinh_bar;
338         s->idx = (uint32_t)d->idx;
339         s->cena = (void *)get_zeroed_page(GFP_KERNEL);
340         if (!s->cena) {
341                 pr_err("Could not allocate page for cena shadow\n");
342                 return -1;
343         }
344         s->eqcr_mode = d->eqcr_mode;
345         QBMAN_BUG_ON(d->idx < 0);
346 #ifdef QBMAN_CHECKING
347         /* We should never be asked to initialise for a portal that isn't in
348          * the power-on state. (Ie. don't forget to reset portals when they are
349          * decommissioned!)
350          */
351         reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
352         QBMAN_BUG_ON(reg);
353 #endif
354         if (s->eqcr_mode == qman_eqcr_vb_array)
355                 reg = qbman_set_swp_cfg(dqrr_size, 0, 0, 3, 2, 3, 1, 1, 1, 1,
356                                         1, 1);
357         else
358                 reg = qbman_set_swp_cfg(dqrr_size, 0, 2, 3, 2, 2, 1, 1, 1, 1,
359                                         1, 1);
360         qbman_cinh_write(s, QBMAN_CINH_SWP_CFG, reg);
361         reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
362         if (!reg) {
363                 pr_err("The portal %d is not enabled!\n", s->idx);
364                 kfree(s->cena);
365                 return -1;
366         }
367         return 0;
368 }
369
370 static inline void qbman_swp_sys_finish(struct qbman_swp_sys *s)
371 {
372         free_page((unsigned long)s->cena);
373 }
374
375 static inline void *
376 qbman_cena_write_start_wo_shadow_fast(struct qbman_swp_sys *s,
377                                       uint32_t offset)
378 {
379 #ifdef QBMAN_CENA_TRACE
380         pr_info("qbman_cena_write_start(%p:%d:0x%03x)\n",
381                 s->addr_cena, s->idx, offset);
382 #endif
383         QBMAN_BUG_ON(offset & 63);
384         return (s->addr_cena + offset);
385 }