Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 : : *
3 : : * Copyright 2008-2012 Freescale Semiconductor, Inc.
4 : : * Copyright 2024-2026 NXP
5 : : *
6 : : */
7 : :
8 : : #ifndef __FSL_BMAN_H
9 : : #define __FSL_BMAN_H
10 : :
11 : : #include <rte_compat.h>
12 : :
13 : : #ifdef __cplusplus
14 : : extern "C" {
15 : : #endif
16 : :
17 : : /* This wrapper represents a bit-array for the depletion state of the 64 Bman
18 : : * buffer pools.
19 : : */
20 : : struct bman_depletion {
21 : : u32 state[2];
22 : : };
23 : :
24 : : /* Number of buffer pools the depletion mask above, and the BMan portal SCN
25 : : * registers, can describe. BPIDs are limited to this range.
26 : : */
27 : : #define BMAN_MAX_POOLS 64U
28 : :
29 : : static inline void bman_depletion_init(struct bman_depletion *c)
30 : : {
31 : 0 : c->state[0] = c->state[1] = 0;
32 : 0 : }
33 : :
34 : : static inline void bman_depletion_fill(struct bman_depletion *c)
35 : : {
36 : 0 : c->state[0] = c->state[1] = ~0;
37 : : }
38 : :
39 : : /* --- Bman data structures (and associated constants) --- */
40 : :
41 : : /* Represents s/w corenet portal mapped data structures */
42 : : struct bm_rcr_entry; /* RCR (Release Command Ring) entries */
43 : : struct bm_mc_command; /* MC (Management Command) command */
44 : : struct bm_mc_result; /* MC result */
45 : :
46 : : /* Code-reduction, define a wrapper for 48-bit buffers. In cases where a buffer
47 : : * pool id specific to this buffer is needed (BM_RCR_VERB_CMD_BPID_MULTI,
48 : : * BM_MCC_VERB_ACQUIRE), the 'bpid' field is used.
49 : : */
50 : : struct __rte_packed_begin bm_hw_buf_desc {
51 : : uint8_t rsv;
52 : : uint8_t bpid;
53 : : rte_be16_t hi; /* High 16-bits of 48-bit address */
54 : : rte_be32_t lo; /* Low 32-bits of 48-bit address */
55 : : } __rte_packed_end;
56 : :
57 : : struct __rte_aligned(8) bm_buffer {
58 : : union {
59 : : struct {
60 : : #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
61 : : u8 __reserved1;
62 : : u8 bpid;
63 : : u16 hi; /* High 16-bits of 48-bit address */
64 : : u32 lo; /* Low 32-bits of 48-bit address */
65 : : #else
66 : : u32 lo;
67 : : u16 hi;
68 : : u8 bpid;
69 : : u8 __reserved;
70 : : #endif
71 : : };
72 : : struct {
73 : : #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
74 : : u64 __notaddress:16;
75 : : u64 addr:48;
76 : : #else
77 : : u64 addr:48;
78 : : u64 __notaddress:16;
79 : : #endif
80 : : };
81 : : struct bm_hw_buf_desc be_desc;
82 : : u64 opaque;
83 : : };
84 : : };
85 : :
86 : : static inline u64 bm_buffer_get64(const struct bm_buffer *buf)
87 : : {
88 : : return buf->addr;
89 : : }
90 : :
91 : : static inline dma_addr_t bm_buf_addr(const struct bm_buffer *buf)
92 : : {
93 : : return (dma_addr_t)buf->addr;
94 : : }
95 : :
96 : : #ifndef BIT_SIZE
97 : : #define BIT_SIZE(t) (sizeof(t) * 8)
98 : : #endif
99 : : #define MAX_U48 \
100 : : ((((uint64_t)UINT16_MAX) << BIT_SIZE(uint32_t)) | UINT32_MAX)
101 : : #define U48_BY_HI16_LO32(hi, lo) \
102 : : (((uint64_t)(hi) << BIT_SIZE(uint32_t)) | (lo))
103 : :
104 : : #define bm_buffer_set64(buf, v) \
105 : : do { \
106 : : struct bm_buffer *__buf931 = (buf); \
107 : : __buf931->hi = upper_32_bits(v); \
108 : : __buf931->lo = lower_32_bits(v); \
109 : : } while (0)
110 : :
111 : : /*
112 : : * Write the whole 8-byte descriptor in one store. Besides the 48-bit
113 : : * address this clears the reserved byte and the bpid field, so no
114 : : * uninitialized stack content can be published to the hardware ring.
115 : : * Callers that need a bpid must set it after this macro.
116 : : */
117 : : #define bm_buffer_set64_to_be(buf, v) \
118 : : do { \
119 : : struct bm_buffer *__buf931 = (buf); \
120 : : \
121 : : __buf931->opaque = cpu_to_be64((uint64_t)(v) & MAX_U48); \
122 : : } while (0)
123 : :
124 : : #define bm_buffer_get64_from_be(buf) \
125 : : ({ \
126 : : uint64_t hi, lo; \
127 : : struct bm_buffer *__buf931 = (buf); \
128 : : \
129 : : hi = be16_to_cpu(__buf931->be_desc.hi); \
130 : : lo = be32_to_cpu(__buf931->be_desc.lo); \
131 : : U48_BY_HI16_LO32(hi, lo); \
132 : : })
133 : :
134 : : #define FSL_BM_BURST_MAX 8
135 : :
136 : : /* See 1.5.3.5.4: "Release Command" */
137 : : struct __rte_packed_begin bm_rcr_entry {
138 : : union {
139 : : struct {
140 : : u8 __dont_write_directly__verb;
141 : : u8 bpid; /* used with BM_RCR_VERB_CMD_BPID_SINGLE */
142 : : u8 __reserved1[62];
143 : : };
144 : : struct bm_buffer bufs[FSL_BM_BURST_MAX];
145 : : };
146 : : } __rte_packed_end;
147 : : #define BM_RCR_VERB_VBIT 0x80
148 : : #define BM_RCR_VERB_CMD_MASK 0x70 /* one of two values; */
149 : : #define BM_RCR_VERB_CMD_BPID_SINGLE 0x20
150 : : #define BM_RCR_VERB_CMD_BPID_MULTI 0x30
151 : : #define BM_RCR_VERB_BUFCOUNT_MASK 0x0f /* values 1..8 */
152 : :
153 : : /* See 1.5.3.1: "Acquire Command" */
154 : : /* See 1.5.3.2: "Query Command" */
155 : : struct __rte_packed_begin bm_mcc_acquire {
156 : : u8 bpid;
157 : : u8 __reserved1[62];
158 : : } __rte_packed_end;
159 : : struct __rte_packed_begin bm_mcc_query {
160 : : u8 __reserved2[63];
161 : : } __rte_packed_end;
162 : : struct __rte_packed_begin bm_mc_command {
163 : : u8 __dont_write_directly__verb;
164 : : union {
165 : : struct bm_mcc_acquire acquire;
166 : : struct bm_mcc_query query;
167 : : };
168 : : } __rte_packed_end;
169 : : #define BM_MCC_VERB_VBIT 0x80
170 : : #define BM_MCC_VERB_CMD_MASK 0x70 /* where the verb contains; */
171 : : #define BM_MCC_VERB_CMD_ACQUIRE 0x10
172 : : #define BM_MCC_VERB_CMD_QUERY 0x40
173 : : #define BM_MCC_VERB_ACQUIRE_BUFCOUNT 0x0f /* values 1..8 go here */
174 : :
175 : : /* See 1.5.3.3: "Acquire Response" */
176 : : /* See 1.5.3.4: "Query Response" */
177 : : struct bm_pool_state {
178 : : u8 __reserved1[32];
179 : : /* "availability state" and "depletion state" */
180 : : struct {
181 : : u8 __reserved1[8];
182 : : /* Access using bman_depletion_***() */
183 : : struct bman_depletion state;
184 : : } as, ds;
185 : : };
186 : :
187 : : struct __rte_packed_begin bm_mc_result {
188 : : union {
189 : : struct {
190 : : u8 verb;
191 : : u8 __reserved1[63];
192 : : };
193 : : union {
194 : : struct {
195 : : u8 __reserved1;
196 : : u8 bpid;
197 : : u8 __reserved2[62];
198 : : };
199 : : struct bm_buffer bufs[FSL_BM_BURST_MAX];
200 : : } acquire;
201 : : struct bm_pool_state query;
202 : : };
203 : : } __rte_packed_end;
204 : : #define BM_MCR_VERB_VBIT 0x80
205 : : #define BM_MCR_VERB_CMD_MASK BM_MCC_VERB_CMD_MASK
206 : : #define BM_MCR_VERB_CMD_ACQUIRE BM_MCC_VERB_CMD_ACQUIRE
207 : : #define BM_MCR_VERB_CMD_QUERY BM_MCC_VERB_CMD_QUERY
208 : : #define BM_MCR_VERB_CMD_ERR_INVALID 0x60
209 : : #define BM_MCR_VERB_CMD_ERR_ECC 0x70
210 : : #define BM_MCR_VERB_ACQUIRE_BUFCOUNT BM_MCC_VERB_ACQUIRE_BUFCOUNT /* 0..8 */
211 : :
212 : : /* Portal and Buffer Pools */
213 : : /* Represents a managed portal */
214 : : struct bman_portal;
215 : :
216 : : /* This object type represents Bman buffer pools. */
217 : : struct bman_pool;
218 : :
219 : : /* This struct specifies parameters for a bman_pool object. */
220 : : struct bman_pool_params {
221 : : /* index of the buffer pool to encapsulate (0-63), ignored if
222 : : * BMAN_POOL_FLAG_DYNAMIC_BPID is set.
223 : : */
224 : : u32 bpid;
225 : : /* bit-mask of BMAN_POOL_FLAG_*** options */
226 : : u32 flags;
227 : : /* depletion-entry/exit thresholds, if BMAN_POOL_FLAG_THRESH is set. NB:
228 : : * this is only allowed if BMAN_POOL_FLAG_DYNAMIC_BPID is used *and*
229 : : * when run in the control plane (which controls Bman CCSR). This array
230 : : * matches the definition of bm_pool_set().
231 : : */
232 : : u32 thresholds[4];
233 : : };
234 : :
235 : : /* Flags to bman_new_pool() */
236 : : #define BMAN_POOL_FLAG_NO_RELEASE 0x00000001 /* can't release to pool */
237 : : #define BMAN_POOL_FLAG_ONLY_RELEASE 0x00000002 /* can only release to pool */
238 : : #define BMAN_POOL_FLAG_DYNAMIC_BPID 0x00000008 /* (de)allocate bpid */
239 : : #define BMAN_POOL_FLAG_THRESH 0x00000010 /* set depletion thresholds */
240 : :
241 : : /* Flags to bman_release() */
242 : : #define BMAN_RELEASE_FLAG_NOW 0x00000008 /* issue immediate release */
243 : :
244 : :
245 : : /**
246 : : * bman_get_portal_index - get portal configuration index
247 : : */
248 : : int bman_get_portal_index(void);
249 : :
250 : : /**
251 : : * bman_rcr_is_empty - Determine if portal's RCR is empty
252 : : *
253 : : * For use in situations where a cpu-affine caller needs to determine when all
254 : : * releases for the local portal have been processed by Bman but can't use the
255 : : * BMAN_RELEASE_FLAG_WAIT_SYNC flag to do this from the final bman_release().
256 : : * The function forces tracking of RCR consumption (which normally doesn't
257 : : * happen until release processing needs to find space to put new release
258 : : * commands), and returns zero if the ring still has unprocessed entries,
259 : : * non-zero if it is empty.
260 : : */
261 : : int bman_rcr_is_empty(void);
262 : :
263 : : /**
264 : : * bman_alloc_bpid_range - Allocate a contiguous range of BPIDs
265 : : * @result: is set by the API to the base BPID of the allocated range
266 : : * @count: the number of BPIDs required
267 : : * @align: required alignment of the allocated range
268 : : * @partial: non-zero if the API can return fewer than @count BPIDs
269 : : *
270 : : * Returns the number of buffer pools allocated, or a negative error code. If
271 : : * @partial is non zero, the allocation request may return a smaller range of
272 : : * BPs than requested (though alignment will be as requested). If @partial is
273 : : * zero, the return value will either be 'count' or negative.
274 : : */
275 : : int bman_alloc_bpid_range(u32 *result, u32 count, u32 align, int partial);
276 : : static inline int bman_alloc_bpid(u32 *result)
277 : : {
278 : 0 : int ret = bman_alloc_bpid_range(result, 1, 0, 0);
279 : :
280 : : return (ret > 0) ? 0 : ret;
281 : : }
282 : :
283 : : /**
284 : : * bman_release_bpid_range - Release the specified range of buffer pool IDs
285 : : * @bpid: the base BPID of the range to deallocate
286 : : * @count: the number of BPIDs in the range
287 : : *
288 : : * This function can also be used to seed the allocator with ranges of BPIDs
289 : : * that it can subsequently allocate from.
290 : : */
291 : : void bman_release_bpid_range(u32 bpid, unsigned int count);
292 : : static inline void bman_release_bpid(u32 bpid)
293 : : {
294 : 0 : bman_release_bpid_range(bpid, 1);
295 : 0 : }
296 : :
297 : : int bman_reserve_bpid_range(u32 bpid, unsigned int count);
298 : : static inline int bman_reserve_bpid(u32 bpid)
299 : : {
300 : : return bman_reserve_bpid_range(bpid, 1);
301 : : }
302 : :
303 : : void bman_seed_bpid_range(u32 bpid, unsigned int count);
304 : :
305 : : int bman_shutdown_pool(u32 bpid);
306 : :
307 : : /**
308 : : * bman_new_pool - Allocates a Buffer Pool object
309 : : * @params: parameters specifying the buffer pool ID and behaviour
310 : : *
311 : : * Creates a pool object for the given @params. A portal and the depletion
312 : : * callback field of @params are only used if the BMAN_POOL_FLAG_DEPLETION flag
313 : : * is set. NB, the fields from @params are copied into the new pool object, so
314 : : * the structure provided by the caller can be released or reused after the
315 : : * function returns.
316 : : */
317 : : __rte_internal
318 : : struct bman_pool *bman_new_pool(const struct bman_pool_params *params);
319 : :
320 : : /**
321 : : * bman_free_pool - Deallocates a Buffer Pool object
322 : : * @pool: the pool object to release
323 : : */
324 : : __rte_internal
325 : : void bman_free_pool(struct bman_pool *pool);
326 : :
327 : : __rte_internal
328 : : void bman_free_bpid(u8 bpid, u32 flags);
329 : :
330 : : /**
331 : : * bman_get_params - Returns a pool object's parameters.
332 : : * @pool: the pool object
333 : : *
334 : : * The returned pointer refers to state within the pool object so must not be
335 : : * modified and can no longer be read once the pool object is destroyed.
336 : : */
337 : : __rte_internal
338 : : const struct bman_pool_params *bman_get_params(const struct bman_pool *pool);
339 : :
340 : : /**
341 : : * bman_release - Release buffer(s) to the buffer pool
342 : : * @pool: the buffer pool object to release to
343 : : * @bufs: an array of buffers to release
344 : : * @num: the number of buffers in @bufs (1-8)
345 : : * @flags: bit-mask of BMAN_RELEASE_FLAG_*** options
346 : : *
347 : : */
348 : : __rte_internal
349 : : int bman_release(struct bman_pool *pool, const struct bm_buffer *bufs, u8 num,
350 : : u32 flags);
351 : : __rte_internal
352 : : int bman_release_fast(struct bman_pool *pool, const uint64_t *bufs,
353 : : uint8_t num);
354 : :
355 : : /**
356 : : * bman_acquire - Acquire buffer(s) from a buffer pool
357 : : * @pool: the buffer pool object to acquire from
358 : : * @bufs: array for storing the acquired buffers
359 : : * @num: the number of buffers desired (@bufs is at least this big)
360 : : *
361 : : * Issues an "Acquire" command via the portal's management command interface.
362 : : * The return value will be the number of buffers obtained from the pool, or a
363 : : * negative error code if a h/w error or pool starvation was encountered.
364 : : */
365 : : __rte_internal
366 : : int bman_acquire(struct bman_pool *pool, struct bm_buffer *bufs, u8 num,
367 : : u32 flags);
368 : : __rte_internal
369 : : int bman_acquire_fast(struct bman_pool *pool, uint64_t *bufs, uint8_t num);
370 : :
371 : : /**
372 : : * bman_query_pools - Query all buffer pool states
373 : : * @state: storage for the queried availability and depletion states
374 : : */
375 : : int bman_query_pools(struct bm_pool_state *state);
376 : :
377 : : /**
378 : : * bman_query_free_buffers - Query how many free buffers are in buffer pool
379 : : * @pool: the buffer pool object to query
380 : : *
381 : : * Return the number of the free buffers
382 : : */
383 : : __rte_internal
384 : : u32 bman_query_free_buffers(struct bman_pool *pool);
385 : :
386 : : /**
387 : : * bman_update_pool_thresholds - Change the buffer pool's depletion thresholds
388 : : * @pool: the buffer pool object to which the thresholds will be set
389 : : * @thresholds: the new thresholds
390 : : */
391 : : int bman_update_pool_thresholds(struct bman_pool *pool, const u32 *thresholds);
392 : :
393 : : /**
394 : : * bm_pool_set_hw_threshold - Change the buffer pool's thresholds
395 : : * @pool: Pool id
396 : : * @low_thresh: low threshold
397 : : * @high_thresh: high threshold
398 : : */
399 : : int bm_pool_set_hw_threshold(u32 bpid, const u32 low_thresh,
400 : : const u32 high_thresh);
401 : :
402 : : #ifdef __cplusplus
403 : : }
404 : : #endif
405 : :
406 : : #endif /* __FSL_BMAN_H */
|