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