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