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