New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / dpaa / include / fsl_bman.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2008-2012 Freescale Semiconductor, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * * Neither the name of the above-listed copyright holders nor the
17  * names of any contributors may be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  *   GPL LICENSE SUMMARY
21  *
22  * ALTERNATIVELY, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") as published by the Free Software
24  * Foundation, either version 2 of that License or (at your option) any
25  * later version.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #ifndef __FSL_BMAN_H
41 #define __FSL_BMAN_H
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /* This wrapper represents a bit-array for the depletion state of the 64 Bman
48  * buffer pools.
49  */
50 struct bman_depletion {
51         u32 state[2];
52 };
53
54 static inline void bman_depletion_init(struct bman_depletion *c)
55 {
56         c->state[0] = c->state[1] = 0;
57 }
58
59 static inline void bman_depletion_fill(struct bman_depletion *c)
60 {
61         c->state[0] = c->state[1] = ~0;
62 }
63
64 /* --- Bman data structures (and associated constants) --- */
65
66 /* Represents s/w corenet portal mapped data structures */
67 struct bm_rcr_entry;    /* RCR (Release Command Ring) entries */
68 struct bm_mc_command;   /* MC (Management Command) command */
69 struct bm_mc_result;    /* MC result */
70
71 /* Code-reduction, define a wrapper for 48-bit buffers. In cases where a buffer
72  * pool id specific to this buffer is needed (BM_RCR_VERB_CMD_BPID_MULTI,
73  * BM_MCC_VERB_ACQUIRE), the 'bpid' field is used.
74  */
75 struct bm_buffer {
76         union {
77                 struct {
78 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
79                         u8 __reserved1;
80                         u8 bpid;
81                         u16 hi; /* High 16-bits of 48-bit address */
82                         u32 lo; /* Low 32-bits of 48-bit address */
83 #else
84                         u32 lo;
85                         u16 hi;
86                         u8 bpid;
87                         u8 __reserved;
88 #endif
89                 };
90                 struct {
91 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
92                         u64 __notaddress:16;
93                         u64 addr:48;
94 #else
95                         u64 addr:48;
96                         u64 __notaddress:16;
97 #endif
98                 };
99                 u64 opaque;
100         };
101 } __attribute__((aligned(8)));
102 static inline u64 bm_buffer_get64(const struct bm_buffer *buf)
103 {
104         return buf->addr;
105 }
106
107 static inline dma_addr_t bm_buf_addr(const struct bm_buffer *buf)
108 {
109         return (dma_addr_t)buf->addr;
110 }
111
112 #define bm_buffer_set64(buf, v) \
113         do { \
114                 struct bm_buffer *__buf931 = (buf); \
115                 __buf931->hi = upper_32_bits(v); \
116                 __buf931->lo = lower_32_bits(v); \
117         } while (0)
118
119 /* See 1.5.3.5.4: "Release Command" */
120 struct bm_rcr_entry {
121         union {
122                 struct {
123                         u8 __dont_write_directly__verb;
124                         u8 bpid; /* used with BM_RCR_VERB_CMD_BPID_SINGLE */
125                         u8 __reserved1[62];
126                 };
127                 struct bm_buffer bufs[8];
128         };
129 } __packed;
130 #define BM_RCR_VERB_VBIT                0x80
131 #define BM_RCR_VERB_CMD_MASK            0x70    /* one of two values; */
132 #define BM_RCR_VERB_CMD_BPID_SINGLE     0x20
133 #define BM_RCR_VERB_CMD_BPID_MULTI      0x30
134 #define BM_RCR_VERB_BUFCOUNT_MASK       0x0f    /* values 1..8 */
135
136 /* See 1.5.3.1: "Acquire Command" */
137 /* See 1.5.3.2: "Query Command" */
138 struct bm_mcc_acquire {
139         u8 bpid;
140         u8 __reserved1[62];
141 } __packed;
142 struct bm_mcc_query {
143         u8 __reserved2[63];
144 } __packed;
145 struct bm_mc_command {
146         u8 __dont_write_directly__verb;
147         union {
148                 struct bm_mcc_acquire acquire;
149                 struct bm_mcc_query query;
150         };
151 } __packed;
152 #define BM_MCC_VERB_VBIT                0x80
153 #define BM_MCC_VERB_CMD_MASK            0x70    /* where the verb contains; */
154 #define BM_MCC_VERB_CMD_ACQUIRE         0x10
155 #define BM_MCC_VERB_CMD_QUERY           0x40
156 #define BM_MCC_VERB_ACQUIRE_BUFCOUNT    0x0f    /* values 1..8 go here */
157
158 /* See 1.5.3.3: "Acquire Response" */
159 /* See 1.5.3.4: "Query Response" */
160 struct bm_pool_state {
161         u8 __reserved1[32];
162         /* "availability state" and "depletion state" */
163         struct {
164                 u8 __reserved1[8];
165                 /* Access using bman_depletion_***() */
166                 struct bman_depletion state;
167         } as, ds;
168 };
169
170 struct bm_mc_result {
171         union {
172                 struct {
173                         u8 verb;
174                         u8 __reserved1[63];
175                 };
176                 union {
177                         struct {
178                                 u8 __reserved1;
179                                 u8 bpid;
180                                 u8 __reserved2[62];
181                         };
182                         struct bm_buffer bufs[8];
183                 } acquire;
184                 struct bm_pool_state query;
185         };
186 } __packed;
187 #define BM_MCR_VERB_VBIT                0x80
188 #define BM_MCR_VERB_CMD_MASK            BM_MCC_VERB_CMD_MASK
189 #define BM_MCR_VERB_CMD_ACQUIRE         BM_MCC_VERB_CMD_ACQUIRE
190 #define BM_MCR_VERB_CMD_QUERY           BM_MCC_VERB_CMD_QUERY
191 #define BM_MCR_VERB_CMD_ERR_INVALID     0x60
192 #define BM_MCR_VERB_CMD_ERR_ECC         0x70
193 #define BM_MCR_VERB_ACQUIRE_BUFCOUNT    BM_MCC_VERB_ACQUIRE_BUFCOUNT /* 0..8 */
194
195 /* Portal and Buffer Pools */
196 /* Represents a managed portal */
197 struct bman_portal;
198
199 /* This object type represents Bman buffer pools. */
200 struct bman_pool;
201
202 /* This struct specifies parameters for a bman_pool object. */
203 struct bman_pool_params {
204         /* index of the buffer pool to encapsulate (0-63), ignored if
205          * BMAN_POOL_FLAG_DYNAMIC_BPID is set.
206          */
207         u32 bpid;
208         /* bit-mask of BMAN_POOL_FLAG_*** options */
209         u32 flags;
210         /* depletion-entry/exit thresholds, if BMAN_POOL_FLAG_THRESH is set. NB:
211          * this is only allowed if BMAN_POOL_FLAG_DYNAMIC_BPID is used *and*
212          * when run in the control plane (which controls Bman CCSR). This array
213          * matches the definition of bm_pool_set().
214          */
215         u32 thresholds[4];
216 };
217
218 /* Flags to bman_new_pool() */
219 #define BMAN_POOL_FLAG_NO_RELEASE    0x00000001 /* can't release to pool */
220 #define BMAN_POOL_FLAG_ONLY_RELEASE  0x00000002 /* can only release to pool */
221 #define BMAN_POOL_FLAG_DYNAMIC_BPID  0x00000008 /* (de)allocate bpid */
222 #define BMAN_POOL_FLAG_THRESH        0x00000010 /* set depletion thresholds */
223
224 /* Flags to bman_release() */
225 #define BMAN_RELEASE_FLAG_NOW        0x00000008 /* issue immediate release */
226
227
228 /**
229  * bman_get_portal_index - get portal configuration index
230  */
231 int bman_get_portal_index(void);
232
233 /**
234  * bman_rcr_is_empty - Determine if portal's RCR is empty
235  *
236  * For use in situations where a cpu-affine caller needs to determine when all
237  * releases for the local portal have been processed by Bman but can't use the
238  * BMAN_RELEASE_FLAG_WAIT_SYNC flag to do this from the final bman_release().
239  * The function forces tracking of RCR consumption (which normally doesn't
240  * happen until release processing needs to find space to put new release
241  * commands), and returns zero if the ring still has unprocessed entries,
242  * non-zero if it is empty.
243  */
244 int bman_rcr_is_empty(void);
245
246 /**
247  * bman_alloc_bpid_range - Allocate a contiguous range of BPIDs
248  * @result: is set by the API to the base BPID of the allocated range
249  * @count: the number of BPIDs required
250  * @align: required alignment of the allocated range
251  * @partial: non-zero if the API can return fewer than @count BPIDs
252  *
253  * Returns the number of buffer pools allocated, or a negative error code. If
254  * @partial is non zero, the allocation request may return a smaller range of
255  * BPs than requested (though alignment will be as requested). If @partial is
256  * zero, the return value will either be 'count' or negative.
257  */
258 int bman_alloc_bpid_range(u32 *result, u32 count, u32 align, int partial);
259 static inline int bman_alloc_bpid(u32 *result)
260 {
261         int ret = bman_alloc_bpid_range(result, 1, 0, 0);
262
263         return (ret > 0) ? 0 : ret;
264 }
265
266 /**
267  * bman_release_bpid_range - Release the specified range of buffer pool IDs
268  * @bpid: the base BPID of the range to deallocate
269  * @count: the number of BPIDs in the range
270  *
271  * This function can also be used to seed the allocator with ranges of BPIDs
272  * that it can subsequently allocate from.
273  */
274 void bman_release_bpid_range(u32 bpid, unsigned int count);
275 static inline void bman_release_bpid(u32 bpid)
276 {
277         bman_release_bpid_range(bpid, 1);
278 }
279
280 int bman_reserve_bpid_range(u32 bpid, unsigned int count);
281 static inline int bman_reserve_bpid(u32 bpid)
282 {
283         return bman_reserve_bpid_range(bpid, 1);
284 }
285
286 void bman_seed_bpid_range(u32 bpid, unsigned int count);
287
288 int bman_shutdown_pool(u32 bpid);
289
290 /**
291  * bman_new_pool - Allocates a Buffer Pool object
292  * @params: parameters specifying the buffer pool ID and behaviour
293  *
294  * Creates a pool object for the given @params. A portal and the depletion
295  * callback field of @params are only used if the BMAN_POOL_FLAG_DEPLETION flag
296  * is set. NB, the fields from @params are copied into the new pool object, so
297  * the structure provided by the caller can be released or reused after the
298  * function returns.
299  */
300 struct bman_pool *bman_new_pool(const struct bman_pool_params *params);
301
302 /**
303  * bman_free_pool - Deallocates a Buffer Pool object
304  * @pool: the pool object to release
305  */
306 void bman_free_pool(struct bman_pool *pool);
307
308 /**
309  * bman_get_params - Returns a pool object's parameters.
310  * @pool: the pool object
311  *
312  * The returned pointer refers to state within the pool object so must not be
313  * modified and can no longer be read once the pool object is destroyed.
314  */
315 const struct bman_pool_params *bman_get_params(const struct bman_pool *pool);
316
317 /**
318  * bman_release - Release buffer(s) to the buffer pool
319  * @pool: the buffer pool object to release to
320  * @bufs: an array of buffers to release
321  * @num: the number of buffers in @bufs (1-8)
322  * @flags: bit-mask of BMAN_RELEASE_FLAG_*** options
323  *
324  */
325 int bman_release(struct bman_pool *pool, const struct bm_buffer *bufs, u8 num,
326                  u32 flags);
327
328 /**
329  * bman_acquire - Acquire buffer(s) from a buffer pool
330  * @pool: the buffer pool object to acquire from
331  * @bufs: array for storing the acquired buffers
332  * @num: the number of buffers desired (@bufs is at least this big)
333  *
334  * Issues an "Acquire" command via the portal's management command interface.
335  * The return value will be the number of buffers obtained from the pool, or a
336  * negative error code if a h/w error or pool starvation was encountered.
337  */
338 int bman_acquire(struct bman_pool *pool, struct bm_buffer *bufs, u8 num,
339                  u32 flags);
340
341 /**
342  * bman_query_pools - Query all buffer pool states
343  * @state: storage for the queried availability and depletion states
344  */
345 int bman_query_pools(struct bm_pool_state *state);
346
347 /**
348  * bman_query_free_buffers - Query how many free buffers are in buffer pool
349  * @pool: the buffer pool object to query
350  *
351  * Return the number of the free buffers
352  */
353 u32 bman_query_free_buffers(struct bman_pool *pool);
354
355 /**
356  * bman_update_pool_thresholds - Change the buffer pool's depletion thresholds
357  * @pool: the buffer pool object to which the thresholds will be set
358  * @thresholds: the new thresholds
359  */
360 int bman_update_pool_thresholds(struct bman_pool *pool, const u32 *thresholds);
361
362 /**
363  * bm_pool_set_hw_threshold - Change the buffer pool's thresholds
364  * @pool: Pool id
365  * @low_thresh: low threshold
366  * @high_thresh: high threshold
367  */
368 int bm_pool_set_hw_threshold(u32 bpid, const u32 low_thresh,
369                              const u32 high_thresh);
370
371 #ifdef __cplusplus
372 }
373 #endif
374
375 #endif /* __FSL_BMAN_H */