1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
12 #define DP_MAX_FREE_SIZE 0x8000 /* 32K */
14 #define DP_INVALID_INDEX 0xffffffff
16 #define DP_FLAGS_START 0x80000000
17 #define DP_IS_START(flags) ((flags) & DP_FLAGS_START)
19 #define DP_FLAGS_SIZE_SHIFT 0
20 #define DP_FLAGS_SIZE_MASK 0x07
22 #define DP_FLAGS_SIZE(flags) (((flags) >> DP_FLAGS_SIZE_SHIFT) & DP_FLAGS_SIZE_MASK)
24 #define DP_IS_FREE(flags) (((flags) & DP_FLAGS_SIZE_MASK) == 0)
25 #define DP_IS_USED(flags) ((flags) & DP_FLAGS_SIZE_MASK)
27 #define DP_DEFRAG_NONE 0x0
28 #define DP_DEFRAG_ALL 0x1
29 #define DP_DEFRAG_TO_FIT 0x2
34 * Each entry includes an index in to the dpool entry array
35 * and the size of dpool array entry.
37 struct dpool_free_list_entry {
39 * Index in to dpool entry array
43 * The size of the entry in the dpool entry array
51 * Used internally to record free entries in the dpool entry array.
52 * Each entry represents a single or multiple contiguous entries
53 * in the dpool entry array.
55 * Used only during the defrag operation.
57 struct dpool_free_list {
59 * Number of entries in the free list
63 * List of unused entries in the dpool entry array
65 struct dpool_free_list_entry entry[DP_MAX_FREE_SIZE];
71 * Each entry includes and index in to the dpool entry array,
72 * the size of the entry and the counts of free entries to the
73 * right and left off that entry.
75 struct dpool_adj_list_entry {
77 * Index in to dpool entry array
81 * The size of the entry in the dpool entry array
85 * Number of free entries directly to the left of
90 * Number of free entries directly to the right of
99 * A list of references to entries in the dpool entry array that
100 * have free entries to the left and right. Since we pack to the
101 * left entries will always have a non zero left out.
103 * Used only during the defrag operation.
105 struct dpool_adj_list {
107 * Number of entries in the adj list
111 * List of entries in the dpool entry array that have
112 * free entries directly to their left and right.
114 struct dpool_adj_list_entry entry[DP_MAX_FREE_SIZE];
120 * Each entry includes flags and the FW index.
131 * Used to manage resource pool. Includes the start FW index, the
132 * size of the entry array and the entry array it's self.
135 uint32_t start_index;
137 uint8_t max_alloc_size;
139 int (*move_callback)(void *user_data,
142 struct dpool_entry *entry;
148 * Initialize the dpool
151 * Pointer to a dpool structure that includes an entry field
152 * that points to the entry array. The user is responsible for
153 * allocating memory for the dpool struct and the entry array.
156 * The base index to use.
159 * The number of entries
161 * [in] max_alloc_size
162 * The number of entries
165 * Pointer to user data. Will be passed in callbacks.
168 * Pointer to move EM entry callback.
175 int dpool_init(struct dpool *dpool,
176 uint32_t start_index,
178 uint8_t max_alloc_size,
180 int (*move_callback)(void *, uint64_t, uint32_t));
185 * Request a FW index of size and if necessary de-fragment the dpool
192 * The size of the requested allocation.
195 * Operation to apply when there is insufficient space:
197 * DP_DEFRAG_NONE (0x0) - Don't do anything.
198 * DP_DEFRAG_ALL (0x1) - Defrag until there is nothing left
200 * DP_DEFRAG_TO_FIT (0x2) - Defrag until there is just enough space
201 * to insert the requested allocation.
204 * - FW index on success
205 * - DP_INVALID_INDEX on failure
208 uint32_t dpool_alloc(struct dpool *dpool,
213 * dpool_set_entry_data
215 * Set the entry data field. This will be passed to callbacks.
227 * - FW index on success
228 * - DP_INVALID_INDEX on failure
231 int dpool_set_entry_data(struct dpool *dpool,
233 uint64_t entry_data);
238 * Free allocated entry. The is responsible for the dpool and dpool
239 * entry array memory.
245 * FW index to free up.
252 int dpool_free(struct dpool *dpool,
268 void dpool_free_all(struct dpool *dpool);
273 * Debug/util function to dump the dpool array.
279 void dpool_dump(struct dpool *dpool);
284 * De-fragment the dpool array and apply the specified defrag strategy.
290 * If using the DP_DEFRAG_TO_FIT strategy defrag will stop when there's
291 * at least entry_size space available.
296 * DP_DEFRAG_ALL (0x1) - Defrag until there is nothing left
298 * DP_DEFRAG_TO_FIT (0x2) - Defrag until there is just enough space
299 * to insert the requested allocation.
303 * > 0 - The size of the largest free space
305 int dpool_defrag(struct dpool *dpool,
309 #endif /* _DPOOL_H_ */