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