New upstream version 17.11-rc3
[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_sys.h"
30 #include <fsl_qbman_portal.h>
31
32 uint32_t qman_version;
33 #define QMAN_REV_4000   0x04000000
34 #define QMAN_REV_4100   0x04010000
35 #define QMAN_REV_4101   0x04010001
36
37 /* All QBMan command and result structures use this "valid bit" encoding */
38 #define QB_VALID_BIT ((uint32_t)0x80)
39
40 /* Management command result codes */
41 #define QBMAN_MC_RSLT_OK      0xf0
42
43 /* QBMan DQRR size is set at runtime in qbman_portal.c */
44
45 #define QBMAN_EQCR_SIZE 8
46
47 static inline uint8_t qm_cyc_diff(uint8_t ringsize, uint8_t first,
48                                   uint8_t last)
49 {
50         /* 'first' is included, 'last' is excluded */
51         if (first <= last)
52                 return last - first;
53         return (2 * ringsize) + last - first;
54 }
55
56 /* --------------------- */
57 /* portal data structure */
58 /* --------------------- */
59
60 struct qbman_swp {
61         struct qbman_swp_desc desc;
62         /* The qbman_sys (ie. arch/OS-specific) support code can put anything it
63          * needs in here.
64          */
65         struct qbman_swp_sys sys;
66         /* Management commands */
67         struct {
68 #ifdef QBMAN_CHECKING
69                 enum swp_mc_check {
70                         swp_mc_can_start, /* call __qbman_swp_mc_start() */
71                         swp_mc_can_submit, /* call __qbman_swp_mc_submit() */
72                         swp_mc_can_poll, /* call __qbman_swp_mc_result() */
73                 } check;
74 #endif
75                 uint32_t valid_bit; /* 0x00 or 0x80 */
76         } mc;
77         /* Push dequeues */
78         uint32_t sdq;
79         /* Volatile dequeues */
80         struct {
81                 /* VDQCR supports a "1 deep pipeline", meaning that if you know
82                  * the last-submitted command is already executing in the
83                  * hardware (as evidenced by at least 1 valid dequeue result),
84                  * you can write another dequeue command to the register, the
85                  * hardware will start executing it as soon as the
86                  * already-executing command terminates. (This minimises latency
87                  * and stalls.) With that in mind, this "busy" variable refers
88                  * to whether or not a command can be submitted, not whether or
89                  * not a previously-submitted command is still executing. In
90                  * other words, once proof is seen that the previously-submitted
91                  * command is executing, "vdq" is no longer "busy".
92                  */
93                 atomic_t busy;
94                 uint32_t valid_bit; /* 0x00 or 0x80 */
95                 /* We need to determine when vdq is no longer busy. This depends
96                  * on whether the "busy" (last-submitted) dequeue command is
97                  * targeting DQRR or main-memory, and detected is based on the
98                  * presence of the dequeue command's "token" showing up in
99                  * dequeue entries in DQRR or main-memory (respectively).
100                  */
101                 struct qbman_result *storage; /* NULL if DQRR */
102         } vdq;
103         /* DQRR */
104         struct {
105                 uint32_t next_idx;
106                 uint32_t valid_bit;
107                 uint8_t dqrr_size;
108                 int reset_bug;
109         } dqrr;
110         struct {
111                 uint32_t pi;
112                 uint32_t pi_vb;
113                 uint32_t ci;
114                 int available;
115         } eqcr;
116 };
117
118 /* -------------------------- */
119 /* portal management commands */
120 /* -------------------------- */
121
122 /* Different management commands all use this common base layer of code to issue
123  * commands and poll for results. The first function returns a pointer to where
124  * the caller should fill in their MC command (though they should ignore the
125  * verb byte), the second function commits merges in the caller-supplied command
126  * verb (which should not include the valid-bit) and submits the command to
127  * hardware, and the third function checks for a completed response (returns
128  * non-NULL if only if the response is complete).
129  */
130 void *qbman_swp_mc_start(struct qbman_swp *p);
131 void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, uint8_t cmd_verb);
132 void *qbman_swp_mc_result(struct qbman_swp *p);
133
134 /* Wraps up submit + poll-for-result */
135 static inline void *qbman_swp_mc_complete(struct qbman_swp *swp, void *cmd,
136                                           uint8_t cmd_verb)
137 {
138         int loopvar = 1000;
139
140         qbman_swp_mc_submit(swp, cmd, cmd_verb);
141         do {
142                 cmd = qbman_swp_mc_result(swp);
143         } while (!cmd && loopvar--);
144         QBMAN_BUG_ON(!loopvar);
145
146         return cmd;
147 }
148
149 /* ---------------------- */
150 /* Descriptors/cachelines */
151 /* ---------------------- */
152
153 /* To avoid needless dynamic allocation, the driver API often gives the caller
154  * a "descriptor" type that the caller can instantiate however they like.
155  * Ultimately though, it is just a cacheline of binary storage (or something
156  * smaller when it is known that the descriptor doesn't need all 64 bytes) for
157  * holding pre-formatted pieces of hardware commands. The performance-critical
158  * code can then copy these descriptors directly into hardware command
159  * registers more efficiently than trying to construct/format commands
160  * on-the-fly. The API user sees the descriptor as an array of 32-bit words in
161  * order for the compiler to know its size, but the internal details are not
162  * exposed. The following macro is used within the driver for converting *any*
163  * descriptor pointer to a usable array pointer. The use of a macro (instead of
164  * an inline) is necessary to work with different descriptor types and to work
165  * correctly with const and non-const inputs (and similarly-qualified outputs).
166  */
167 #define qb_cl(d) (&(d)->donot_manipulate_directly[0])