292ec6a9803186779fc7ad62e8c301b6e9fd2d44
[deb_dpdk.git] / drivers / bus / fslmc / qbman / qbman_private.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 /* Perform extra checking */
30 #define QBMAN_CHECKING
31
32 /* To maximise the amount of logic that is common between the Linux driver and
33  * other targets (such as the embedded MC firmware), we pivot here between the
34  * inclusion of two platform-specific headers.
35  *
36  * The first, qbman_sys_decl.h, includes any and all required system headers as
37  * well as providing any definitions for the purposes of compatibility. The
38  * second, qbman_sys.h, is where platform-specific routines go.
39  *
40  * The point of the split is that the platform-independent code (including this
41  * header) may depend on platform-specific declarations, yet other
42  * platform-specific routines may depend on platform-independent definitions.
43  */
44
45 #include "qbman_sys_decl.h"
46
47 /* When things go wrong, it is a convenient trick to insert a few FOO()
48  * statements in the code to trace progress. TODO: remove this once we are
49  * hacking the code less actively.
50  */
51 #define FOO() fsl_os_print("FOO: %s:%d\n", __FILE__, __LINE__)
52
53 /* Any time there is a register interface which we poll on, this provides a
54  * "break after x iterations" scheme for it. It's handy for debugging, eg.
55  * where you don't want millions of lines of log output from a polling loop
56  * that won't, because such things tend to drown out the earlier log output
57  * that might explain what caused the problem. (NB: put ";" after each macro!)
58  * TODO: we should probably remove this once we're done sanitising the
59  * simulator...
60  */
61 #define DBG_POLL_START(loopvar) (loopvar = 10)
62 #define DBG_POLL_CHECK(loopvar) \
63 do { \
64         if (!(loopvar--)) \
65                 QBMAN_BUG_ON(NULL == "DBG_POLL_CHECK"); \
66 } while (0)
67
68 /* For CCSR or portal-CINH registers that contain fields at arbitrary offsets
69  * and widths, these macro-generated encode/decode/isolate/remove inlines can
70  * be used.
71  *
72  * Eg. to "d"ecode a 14-bit field out of a register (into a "uint16_t" type),
73  * where the field is located 3 bits "up" from the least-significant bit of the
74  * register (ie. the field location within the 32-bit register corresponds to a
75  * mask of 0x0001fff8), you would do;
76  *                uint16_t field = d32_uint16_t(3, 14, reg_value);
77  *
78  * Or to "e"ncode a 1-bit boolean value (input type is "int", zero is FALSE,
79  * non-zero is TRUE, so must convert all non-zero inputs to 1, hence the "!!"
80  * operator) into a register at bit location 0x00080000 (19 bits "in" from the
81  * LS bit), do;
82  *                reg_value |= e32_int(19, 1, !!field);
83  *
84  * If you wish to read-modify-write a register, such that you leave the 14-bit
85  * field as-is but have all other fields set to zero, then "i"solate the 14-bit
86  * value using;
87  *                reg_value = i32_uint16_t(3, 14, reg_value);
88  *
89  * Alternatively, you could "r"emove the 1-bit boolean field (setting it to
90  * zero) but leaving all other fields as-is;
91  *                reg_val = r32_int(19, 1, reg_value);
92  *
93  */
94 #ifdef __LP64__
95 #define MAKE_MASK32(width) ((uint32_t)(( 1ULL << width) - 1))
96 #else
97 #define MAKE_MASK32(width) (width == 32 ? 0xffffffff : \
98                                  (uint32_t)((1 << width) - 1))
99 #endif
100 #define DECLARE_CODEC32(t) \
101 static inline uint32_t e32_##t(uint32_t lsoffset, uint32_t width, t val) \
102 { \
103         QBMAN_BUG_ON(width > (sizeof(t) * 8)); \
104         return ((uint32_t)val & MAKE_MASK32(width)) << lsoffset; \
105 } \
106 static inline t d32_##t(uint32_t lsoffset, uint32_t width, uint32_t val) \
107 { \
108         QBMAN_BUG_ON(width > (sizeof(t) * 8)); \
109         return (t)((val >> lsoffset) & MAKE_MASK32(width)); \
110 } \
111 static inline uint32_t i32_##t(uint32_t lsoffset, uint32_t width, \
112                                 uint32_t val) \
113 { \
114         QBMAN_BUG_ON(width > (sizeof(t) * 8)); \
115         return e32_##t(lsoffset, width, d32_##t(lsoffset, width, val)); \
116 } \
117 static inline uint32_t r32_##t(uint32_t lsoffset, uint32_t width, \
118                                 uint32_t val) \
119 { \
120         QBMAN_BUG_ON(width > (sizeof(t) * 8)); \
121         return ~(MAKE_MASK32(width) << lsoffset) & val; \
122 }
123 DECLARE_CODEC32(uint32_t)
124 DECLARE_CODEC32(uint16_t)
125 DECLARE_CODEC32(uint8_t)
126 DECLARE_CODEC32(int)
127
128         /*********************/
129         /* Debugging assists */
130         /*********************/
131
132 static inline void __hexdump(unsigned long start, unsigned long end,
133                              unsigned long p, size_t sz, const unsigned char *c)
134 {
135         while (start < end) {
136                 unsigned int pos = 0;
137                 char buf[64];
138                 int nl = 0;
139
140                 pos += sprintf(buf + pos, "%08lx: ", start);
141                 do {
142                         if ((start < p) || (start >= (p + sz)))
143                                 pos += sprintf(buf + pos, "..");
144                         else
145                                 pos += sprintf(buf + pos, "%02x", *(c++));
146                         if (!(++start & 15)) {
147                                 buf[pos++] = '\n';
148                                 nl = 1;
149                         } else {
150                                 nl = 0;
151                                 if (!(start & 1))
152                                         buf[pos++] = ' ';
153                                 if (!(start & 3))
154                                         buf[pos++] = ' ';
155                         }
156                 } while (start & 15);
157                 if (!nl)
158                         buf[pos++] = '\n';
159                 buf[pos] = '\0';
160                 pr_info("%s", buf);
161         }
162 }
163
164 static inline void hexdump(const void *ptr, size_t sz)
165 {
166         unsigned long p = (unsigned long)ptr;
167         unsigned long start = p & ~(unsigned long)15;
168         unsigned long end = (p + sz + 15) & ~(unsigned long)15;
169         const unsigned char *c = ptr;
170
171         __hexdump(start, end, p, sz, c);
172 }
173
174 #include "qbman_sys.h"