blob: 1c6e4891302897b7103f5ffcb698a8133295d8ff [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07002/*
3 * Copyright (C) 2014 Freescale Semiconductor
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07004 */
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/* Trace the 3 different classes of read/write access to QBMan. #undef as
22 * required. */
Simon Glassc06c1be2020-05-10 11:40:08 -060023#include <linux/bug.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060024#include <linux/printk.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070025#undef QBMAN_CCSR_TRACE
26#undef QBMAN_CINH_TRACE
27#undef QBMAN_CENA_TRACE
28
29/* Temporarily define this to get around the fact that cache enabled mapping is
30 * not working right now. Will remove this after uboot could map the cache
31 * enabled portal memory.
32 */
33#define QBMAN_CINH_ONLY
34
35static inline void word_copy(void *d, const void *s, unsigned int cnt)
36{
37 uint32_t *dd = d;
38 const uint32_t *ss = s;
39
40 while (cnt--)
41 *(dd++) = *(ss++);
42}
43
44/* Currently, the CENA support code expects each 32-bit word to be written in
45 * host order, and these are converted to hardware (little-endian) order on
46 * command submission. However, 64-bit quantities are must be written (and read)
47 * as two 32-bit words with the least-significant word first, irrespective of
48 * host endianness. */
49static inline void u64_to_le32_copy(void *d, const uint64_t *s,
50 unsigned int cnt)
51{
52 uint32_t *dd = d;
53 const uint32_t *ss = (const uint32_t *)s;
54
55 while (cnt--) {
56 /* TBD: the toolchain was choking on the use of 64-bit types up
57 * until recently so this works entirely with 32-bit variables.
58 * When 64-bit types become usable again, investigate better
59 * ways of doing this. */
60#if defined(__BIG_ENDIAN)
61 *(dd++) = ss[1];
62 *(dd++) = ss[0];
63 ss += 2;
64#else
65 *(dd++) = *(ss++);
66 *(dd++) = *(ss++);
67#endif
68 }
69}
70static inline void u64_from_le32_copy(uint64_t *d, const void *s,
71 unsigned int cnt)
72{
73 const uint32_t *ss = s;
74 uint32_t *dd = (uint32_t *)d;
75
76 while (cnt--) {
77#if defined(__BIG_ENDIAN)
78 dd[1] = *(ss++);
79 dd[0] = *(ss++);
80 dd += 2;
81#else
82 *(dd++) = *(ss++);
83 *(dd++) = *(ss++);
84#endif
85 }
86}
87
88/* Convert a host-native 32bit value into little endian */
89#if defined(__BIG_ENDIAN)
90static inline uint32_t make_le32(uint32_t val)
91{
92 return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
93 ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
94}
95#else
96#define make_le32(val) (val)
97#endif
98static inline void make_le32_n(uint32_t *val, unsigned int num)
99{
100 while (num--) {
101 *val = make_le32(*val);
102 val++;
103 }
104}
105
106 /******************/
107 /* Portal access */
108 /******************/
109struct qbman_swp_sys {
110 /* On GPP, the sys support for qbman_swp is here. The CENA region isi
111 * not an mmap() of the real portal registers, but an allocated
112 * place-holder, because the actual writes/reads to/from the portal are
113 * marshalled from these allocated areas using QBMan's "MC access
114 * registers". CINH accesses are atomic so there's no need for a
115 * place-holder. */
116 void *cena;
117 void __iomem *addr_cena;
118 void __iomem *addr_cinh;
119};
120
121/* P_OFFSET is (ACCESS_CMD,0,12) - offset within the portal
122 * C is (ACCESS_CMD,12,1) - is inhibited? (0==CENA, 1==CINH)
123 * SWP_IDX is (ACCESS_CMD,16,10) - Software portal index
124 * P is (ACCESS_CMD,28,1) - (0==special portal, 1==any portal)
125 * T is (ACCESS_CMD,29,1) - Command type (0==READ, 1==WRITE)
126 * E is (ACCESS_CMD,31,1) - Command execute (1 to issue, poll for 0==complete)
127 */
128
129static inline void qbman_cinh_write(struct qbman_swp_sys *s, uint32_t offset,
130 uint32_t val)
131{
132 __raw_writel(val, s->addr_cinh + offset);
133#ifdef QBMAN_CINH_TRACE
134 pr_info("qbman_cinh_write(%p:0x%03x) 0x%08x\n",
135 s->addr_cinh, offset, val);
136#endif
137}
138
139static inline uint32_t qbman_cinh_read(struct qbman_swp_sys *s, uint32_t offset)
140{
141 uint32_t reg = __raw_readl(s->addr_cinh + offset);
142
143#ifdef QBMAN_CINH_TRACE
144 pr_info("qbman_cinh_read(%p:0x%03x) 0x%08x\n",
145 s->addr_cinh, offset, reg);
146#endif
147 return reg;
148}
149
150static inline void *qbman_cena_write_start(struct qbman_swp_sys *s,
151 uint32_t offset)
152{
153 void *shadow = s->cena + offset;
154
155#ifdef QBMAN_CENA_TRACE
156 pr_info("qbman_cena_write_start(%p:0x%03x) %p\n",
157 s->addr_cena, offset, shadow);
158#endif
159 BUG_ON(offset & 63);
160 dcbz(shadow);
161 return shadow;
162}
163
164static inline void qbman_cena_write_complete(struct qbman_swp_sys *s,
165 uint32_t offset, void *cmd)
166{
167 const uint32_t *shadow = cmd;
168 int loop;
169
170#ifdef QBMAN_CENA_TRACE
171 pr_info("qbman_cena_write_complete(%p:0x%03x) %p\n",
172 s->addr_cena, offset, shadow);
173 hexdump(cmd, 64);
174#endif
175 for (loop = 15; loop >= 0; loop--)
176#ifdef QBMAN_CINH_ONLY
177 __raw_writel(shadow[loop], s->addr_cinh +
178 offset + loop * 4);
179#else
180 __raw_writel(shadow[loop], s->addr_cena +
181 offset + loop * 4);
182#endif
183}
184
185static inline void *qbman_cena_read(struct qbman_swp_sys *s, uint32_t offset)
186{
187 uint32_t *shadow = s->cena + offset;
188 unsigned int loop;
189
190#ifdef QBMAN_CENA_TRACE
191 pr_info("qbman_cena_read(%p:0x%03x) %p\n",
192 s->addr_cena, offset, shadow);
193#endif
194
195 for (loop = 0; loop < 16; loop++)
196#ifdef QBMAN_CINH_ONLY
197 shadow[loop] = __raw_readl(s->addr_cinh + offset
198 + loop * 4);
199#else
200 shadow[loop] = __raw_readl(s->addr_cena + offset
201 + loop * 4);
202#endif
203#ifdef QBMAN_CENA_TRACE
204 hexdump(shadow, 64);
205#endif
206 return shadow;
207}
208
209static inline void qbman_cena_invalidate_prefetch(struct qbman_swp_sys *s,
210 uint32_t offset)
211{
212}
213
214 /******************/
215 /* Portal support */
216 /******************/
217
218/* The SWP_CFG portal register is special, in that it is used by the
219 * platform-specific code rather than the platform-independent code in
220 * qbman_portal.c. So use of it is declared locally here. */
221#define QBMAN_CINH_SWP_CFG 0xd00
222
223/* For MC portal use, we always configure with
224 * DQRR_MF is (SWP_CFG,20,3) - DQRR max fill (<- 0x4)
225 * EST is (SWP_CFG,16,3) - EQCR_CI stashing threshold (<- 0x0)
226 * RPM is (SWP_CFG,12,2) - RCR production notification mode (<- 0x3)
227 * DCM is (SWP_CFG,10,2) - DQRR consumption notification mode (<- 0x2)
228 * EPM is (SWP_CFG,8,2) - EQCR production notification mode (<- 0x3)
229 * SD is (SWP_CFG,5,1) - memory stashing drop enable (<- FALSE)
230 * SP is (SWP_CFG,4,1) - memory stashing priority (<- TRUE)
231 * SE is (SWP_CFG,3,1) - memory stashing enable (<- 0x0)
232 * DP is (SWP_CFG,2,1) - dequeue stashing priority (<- TRUE)
233 * DE is (SWP_CFG,1,1) - dequeue stashing enable (<- 0x0)
234 * EP is (SWP_CFG,0,1) - EQCR_CI stashing priority (<- FALSE)
235 */
236static inline uint32_t qbman_set_swp_cfg(uint8_t max_fill, uint8_t wn,
237 uint8_t est, uint8_t rpm, uint8_t dcm,
238 uint8_t epm, int sd, int sp, int se,
239 int dp, int de, int ep)
240{
241 uint32_t reg;
242
Priyanka Jain9de71602016-12-07 12:04:05 +0530243 reg = e32_uint8_t(20, (uint32_t)(3 + (max_fill >> 3)), max_fill) |
244 e32_uint8_t(16, 3, est) | e32_uint8_t(12, 2, rpm) |
245 e32_uint8_t(10, 2, dcm) | e32_uint8_t(8, 2, epm) |
246 e32_int(5, 1, sd) | e32_int(4, 1, sp) | e32_int(3, 1, se) |
247 e32_int(2, 1, dp) | e32_int(1, 1, de) | e32_int(0, 1, ep) |
248 e32_uint8_t(14, 1, wn);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700249 return reg;
250}
251
252static inline int qbman_swp_sys_init(struct qbman_swp_sys *s,
Priyanka Jain9de71602016-12-07 12:04:05 +0530253 const struct qbman_swp_desc *d,
254 uint8_t dqrr_size)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700255{
256 uint32_t reg;
257
258 s->addr_cena = d->cena_bar;
259 s->addr_cinh = d->cinh_bar;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500260 s->cena = (void *)valloc(CFG_SYS_PAGE_SIZE);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700261 if (!s->cena) {
262 printf("Could not allocate page for cena shadow\n");
263 return -1;
264 }
Tom Rini6a5dccc2022-11-16 13:10:41 -0500265 memset((void *)s->cena, 0x00, CFG_SYS_PAGE_SIZE);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700266
267#ifdef QBMAN_CHECKING
268 /* We should never be asked to initialise for a portal that isn't in
269 * the power-on state. (Ie. don't forget to reset portals when they are
270 * decommissioned!)
271 */
272 reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
273 BUG_ON(reg);
274#endif
275#ifdef QBMAN_CINH_ONLY
Priyanka Jain9de71602016-12-07 12:04:05 +0530276 reg = qbman_set_swp_cfg(dqrr_size, 1, 0, 3, 2, 3, 0, 1, 0, 1, 0, 0);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700277#else
Priyanka Jain9de71602016-12-07 12:04:05 +0530278 reg = qbman_set_swp_cfg(dqrr_size, 0, 0, 3, 2, 3, 0, 1, 0, 1, 0, 0);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700279#endif
280 qbman_cinh_write(s, QBMAN_CINH_SWP_CFG, reg);
281 reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
282 if (!reg) {
283 printf("The portal is not enabled!\n");
284 free(s->cena);
285 return -1;
286 }
287 return 0;
288}
289
290static inline void qbman_swp_sys_finish(struct qbman_swp_sys *s)
291{
292 free((void *)s->cena);
293}