7aa1d4f66b816b844e2732b8e40388fe2462786e
[deb_dpdk.git] / drivers / bus / fslmc / qbman / qbman_portal.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
29 #include "qbman_private.h"
30 #include <fsl_qbman_portal.h>
31
32 /* All QBMan command and result structures use this "valid bit" encoding */
33 #define QB_VALID_BIT ((uint32_t)0x80)
34
35 /* Management command result codes */
36 #define QBMAN_MC_RSLT_OK      0xf0
37
38 /* QBMan DQRR size is set at runtime in qbman_portal.c */
39
40 #define QBMAN_EQCR_SIZE 8
41
42 static inline u8 qm_cyc_diff(u8 ringsize, u8 first, u8 last)
43 {
44         /* 'first' is included, 'last' is excluded */
45         if (first <= last)
46                 return last - first;
47         return (2 * ringsize) + last - first;
48 }
49
50 /* --------------------- */
51 /* portal data structure */
52 /* --------------------- */
53
54 struct qbman_swp {
55         struct qbman_swp_desc desc;
56         /* The qbman_sys (ie. arch/OS-specific) support code can put anything it
57          * needs in here.
58          */
59         struct qbman_swp_sys sys;
60         /* Management commands */
61         struct {
62 #ifdef QBMAN_CHECKING
63                 enum swp_mc_check {
64                         swp_mc_can_start, /* call __qbman_swp_mc_start() */
65                         swp_mc_can_submit, /* call __qbman_swp_mc_submit() */
66                         swp_mc_can_poll, /* call __qbman_swp_mc_result() */
67                 } check;
68 #endif
69                 uint32_t valid_bit; /* 0x00 or 0x80 */
70         } mc;
71         /* Push dequeues */
72         uint32_t sdq;
73         /* Volatile dequeues */
74         struct {
75                 /* VDQCR supports a "1 deep pipeline", meaning that if you know
76                  * the last-submitted command is already executing in the
77                  * hardware (as evidenced by at least 1 valid dequeue result),
78                  * you can write another dequeue command to the register, the
79                  * hardware will start executing it as soon as the
80                  * already-executing command terminates. (This minimises latency
81                  * and stalls.) With that in mind, this "busy" variable refers
82                  * to whether or not a command can be submitted, not whether or
83                  * not a previously-submitted command is still executing. In
84                  * other words, once proof is seen that the previously-submitted
85                  * command is executing, "vdq" is no longer "busy".
86                  */
87                 atomic_t busy;
88                 uint32_t valid_bit; /* 0x00 or 0x80 */
89                 /* We need to determine when vdq is no longer busy. This depends
90                  * on whether the "busy" (last-submitted) dequeue command is
91                  * targeting DQRR or main-memory, and detected is based on the
92                  * presence of the dequeue command's "token" showing up in
93                  * dequeue entries in DQRR or main-memory (respectively).
94                  */
95                 struct qbman_result *storage; /* NULL if DQRR */
96         } vdq;
97         /* DQRR */
98         struct {
99                 uint32_t next_idx;
100                 uint32_t valid_bit;
101                 uint8_t dqrr_size;
102                 int reset_bug;
103         } dqrr;
104         struct {
105                 uint32_t pi;
106                 uint32_t pi_vb;
107                 uint32_t ci;
108                 int available;
109         } eqcr;
110 };
111
112 /* -------------------------- */
113 /* portal management commands */
114 /* -------------------------- */
115
116 /* Different management commands all use this common base layer of code to issue
117  * commands and poll for results. The first function returns a pointer to where
118  * the caller should fill in their MC command (though they should ignore the
119  * verb byte), the second function commits merges in the caller-supplied command
120  * verb (which should not include the valid-bit) and submits the command to
121  * hardware, and the third function checks for a completed response (returns
122  * non-NULL if only if the response is complete).
123  */
124 void *qbman_swp_mc_start(struct qbman_swp *p);
125 void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, uint32_t cmd_verb);
126 void *qbman_swp_mc_result(struct qbman_swp *p);
127
128 /* Wraps up submit + poll-for-result */
129 static inline void *qbman_swp_mc_complete(struct qbman_swp *swp, void *cmd,
130                                           uint32_t cmd_verb)
131 {
132         int loopvar;
133
134         qbman_swp_mc_submit(swp, cmd, cmd_verb);
135         DBG_POLL_START(loopvar);
136         do {
137                 DBG_POLL_CHECK(loopvar);
138                 cmd = qbman_swp_mc_result(swp);
139         } while (!cmd);
140         return cmd;
141 }
142
143 /* ------------ */
144 /* qb_attr_code */
145 /* ------------ */
146
147 /* This struct locates a sub-field within a QBMan portal (CENA) cacheline which
148  * is either serving as a configuration command or a query result. The
149  * representation is inherently little-endian, as the indexing of the words is
150  * itself little-endian in nature and DPAA2 QBMan is little endian for anything
151  * that crosses a word boundary too (64-bit fields are the obvious examples).
152  */
153 struct qb_attr_code {
154         unsigned int word; /* which uint32_t[] array member encodes the field */
155         unsigned int lsoffset; /* encoding offset from ls-bit */
156         unsigned int width; /* encoding width. (bool must be 1.) */
157 };
158
159 /* Some pre-defined codes */
160 extern struct qb_attr_code code_generic_verb;
161 extern struct qb_attr_code code_generic_rslt;
162
163 /* Macros to define codes */
164 #define QB_CODE(a, b, c) { a, b, c}
165 #define QB_CODE_NULL \
166         QB_CODE((unsigned int)-1, (unsigned int)-1, (unsigned int)-1)
167
168 /* Rotate a code "ms", meaning that it moves from less-significant bytes to
169  * more-significant, from less-significant words to more-significant, etc. The
170  * "ls" version does the inverse, from more-significant towards
171  * less-significant.
172  */
173 static inline void qb_attr_code_rotate_ms(struct qb_attr_code *code,
174                                           unsigned int bits)
175 {
176         code->lsoffset += bits;
177         while (code->lsoffset > 31) {
178                 code->word++;
179                 code->lsoffset -= 32;
180         }
181 }
182
183 static inline void qb_attr_code_rotate_ls(struct qb_attr_code *code,
184                                           unsigned int bits)
185 {
186         /* Don't be fooled, this trick should work because the types are
187          * unsigned. So the case that interests the while loop (the rotate has
188          * gone too far and the word count needs to compensate for it), is
189          * manifested when lsoffset is negative. But that equates to a really
190          * large unsigned value, starting with lots of "F"s. As such, we can
191          * continue adding 32 back to it until it wraps back round above zero,
192          * to a value of 31 or less...
193          */
194         code->lsoffset -= bits;
195         while (code->lsoffset > 31) {
196                 code->word--;
197                 code->lsoffset += 32;
198         }
199 }
200
201 /* Implement a loop of code rotations until 'expr' evaluates to FALSE (0). */
202 #define qb_attr_code_for_ms(code, bits, expr) \
203                 for (; expr; qb_attr_code_rotate_ms(code, bits))
204 #define qb_attr_code_for_ls(code, bits, expr) \
205                 for (; expr; qb_attr_code_rotate_ls(code, bits))
206
207 /* decode a field from a cacheline */
208 static inline uint32_t qb_attr_code_decode(const struct qb_attr_code *code,
209                                            const uint32_t *cacheline)
210 {
211         return d32_uint32_t(code->lsoffset, code->width, cacheline[code->word]);
212 }
213
214 static inline uint64_t qb_attr_code_decode_64(const struct qb_attr_code *code,
215                                               const uint64_t *cacheline)
216 {
217         return cacheline[code->word / 2];
218 }
219
220 /* encode a field to a cacheline */
221 static inline void qb_attr_code_encode(const struct qb_attr_code *code,
222                                        uint32_t *cacheline, uint32_t val)
223 {
224         cacheline[code->word] =
225                 r32_uint32_t(code->lsoffset, code->width, cacheline[code->word])
226                 | e32_uint32_t(code->lsoffset, code->width, val);
227 }
228
229 static inline void qb_attr_code_encode_64(const struct qb_attr_code *code,
230                                           uint64_t *cacheline, uint64_t val)
231 {
232         cacheline[code->word / 2] = val;
233 }
234
235 /* Small-width signed values (two's-complement) will decode into medium-width
236  * positives. (Eg. for an 8-bit signed field, which stores values from -128 to
237  * +127, a setting of -7 would appear to decode to the 32-bit unsigned value
238  * 249. Likewise -120 would decode as 136.) This function allows the caller to
239  * "re-sign" such fields to 32-bit signed. (Eg. -7, which was 249 with an 8-bit
240  * encoding, will become 0xfffffff9 if you cast the return value to uint32_t).
241  */
242 static inline int32_t qb_attr_code_makesigned(const struct qb_attr_code *code,
243                                               uint32_t val)
244 {
245         QBMAN_BUG_ON(val >= (1u << code->width));
246         /* code->width should never exceed the width of val. If it does then a
247          * different function with larger val size must be used to translate
248          * from unsigned to signed
249          */
250         QBMAN_BUG_ON(code->width > sizeof(val) * CHAR_BIT);
251         /* If the high bit was set, it was encoding a negative */
252         if (val >= 1u << (code->width - 1))
253                 return (int32_t)0 - (int32_t)(((uint32_t)1 << code->width) -
254                         val);
255         /* Otherwise, it was encoding a positive */
256         return (int32_t)val;
257 }
258
259 /* ---------------------- */
260 /* Descriptors/cachelines */
261 /* ---------------------- */
262
263 /* To avoid needless dynamic allocation, the driver API often gives the caller
264  * a "descriptor" type that the caller can instantiate however they like.
265  * Ultimately though, it is just a cacheline of binary storage (or something
266  * smaller when it is known that the descriptor doesn't need all 64 bytes) for
267  * holding pre-formatted pieces of hardware commands. The performance-critical
268  * code can then copy these descriptors directly into hardware command
269  * registers more efficiently than trying to construct/format commands
270  * on-the-fly. The API user sees the descriptor as an array of 32-bit words in
271  * order for the compiler to know its size, but the internal details are not
272  * exposed. The following macro is used within the driver for converting *any*
273  * descriptor pointer to a usable array pointer. The use of a macro (instead of
274  * an inline) is necessary to work with different descriptor types and to work
275  * correctly with const and non-const inputs (and similarly-qualified outputs).
276  */
277 #define qb_cl(d) (&(d)->dont_manipulate_directly[0])