1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2019-2023 Broadcom
* All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include "tfp.h"
#include "dpool.h"
int dpool_init(struct dpool *dpool,
uint32_t start_index,
uint32_t size,
uint8_t max_alloc_size,
void *user_data,
int (*move_callback)(void *, uint64_t, uint32_t))
{
uint32_t i;
int rc;
struct tfp_calloc_parms parms;
parms.nitems = size;
parms.size = sizeof(struct dpool_entry);
parms.alignment = 0;
rc = tfp_calloc(&parms);
if (rc)
return rc;
dpool->entry = parms.mem_va;
dpool->start_index = start_index;
dpool->size = size;
dpool->max_alloc_size = max_alloc_size;
dpool->user_data = user_data;
dpool->move_callback = move_callback;
/*
* Init entries
*/
for (i = 0; i < size; i++) {
dpool->entry[i].flags = 0;
dpool->entry[i].index = start_index;
dpool->entry[i].entry_data = 0UL;
start_index++;
}
return 0;
}
static int dpool_move(struct dpool *dpool,
uint32_t dst_index,
uint32_t src_index)
{
uint32_t size;
uint32_t i;
if (DP_IS_FREE(dpool->entry[dst_index].flags)) {
size = DP_FLAGS_SIZE(dpool->entry[src_index].flags);
dpool->entry[dst_index].flags = dpool->entry[src_index].flags;
dpool->entry[dst_index].entry_data = dpool->entry[src_index].entry_data;
if (dpool->move_callback != NULL) {
dpool->move_callback(dpool->user_data,
dpool->entry[src_index].entry_data,
dst_index + dpool->start_index);
}
dpool->entry[src_index].flags = 0;
dpool->entry[src_index].entry_data = 0UL;
for (i = 1; i < size; i++) {
dpool->entry[dst_index + i].flags = size;
dpool->entry[src_index + i].flags = 0;
}
} else {
return -1;
}
return 0;
}
int dpool_defrag(struct dpool *dpool,
uint32_t entry_size,
uint8_t defrag)
{
struct dpool_free_list *free_list;
struct dpool_adj_list *adj_list;
struct tfp_calloc_parms parms;
uint32_t count;
uint32_t index;
uint32_t used;
uint32_t i;
uint32_t size;
uint32_t largest_free_index = 0;
uint32_t largest_free_size;
uint32_t max;
uint32_t max_index;
uint32_t max_size = 0;
int rc;
parms.nitems = 1;
parms.size = sizeof(struct dpool_free_list);
parms.alignment = 0;
rc = tfp_calloc(&parms);
if (rc)
return rc;
free_list = (struct dpool_free_list *)parms.mem_va;
if (free_list == NULL) {
TFP_DRV_LOG(ERR, "dpool free list allocation failed\n");
return -ENOMEM;
}
parms.nitems = 1;
parms.size = sizeof(struct dpool_adj_list);
parms.alignment = 0;
rc = tfp_calloc(&parms);
if (rc)
return rc;
adj_list = (struct dpool_adj_list *)parms.mem_va;
if (adj_list == NULL) {
TFP_DRV_LOG(ERR, "dpool adjacent list allocation failed\n");
return -ENOMEM;
}
while (1) {
/*
* Create list of free entries
*/
free_list->size = 0;
largest_free_size = 0;
largest_free_index = 0;
count = 0;
index = 0;
for (i = 0; i < dpool->size; i++) {
if (DP_IS_FREE(dpool->entry[i].flags)) {
if (count == 0)
index = i;
count++;
} else if (count > 0) {
free_list->entry[free_list->size].index = index;
free_list->entry[free_list->size].size = count;
if (count > largest_free_size) {
largest_free_index = free_list->size;
largest_free_size = count;
}
free_list->size++;
count = 0;
}
}
if (free_list->size == 0)
largest_free_size = count;
/*
* If using defrag to fit and there's a large enough
* space then we are done.
*/
if (defrag == DP_DEFRAG_TO_FIT &&
largest_free_size >= entry_size)
goto done;
/*
* Create list of entries adjacent to free entries
*/
count = 0;
adj_list->size = 0;
used = 0;
for (i = 0; i < dpool->size; ) {
if (DP_IS_USED(dpool->entry[i].flags)) {
used++;
if (count > 0) {
adj_list->entry[adj_list->size].index = i;
adj_list->entry[adj_list->size].size =
DP_FLAGS_SIZE(dpool->entry[i].flags);
adj_list->entry[adj_list->size].left = count;
if (adj_list->size > 0 && used == 1)
adj_list->entry[adj_list->size - 1].right = count;
adj_list->size++;
}
count = 0;
i += DP_FLAGS_SIZE(dpool->entry[i].flags);
} else {
used = 0;
count++;
i++;
}
}
/*
* Using the size of the largest free space available
* select the adjacency list entry of that size with
* the largest left + right + size count. If there
* are no entries of that size then decrement the size
* and try again.
*/
max = 0;
max_index = 0;
max_size = 0;
for (size = largest_free_size; size > 0; size--) {
for (i = 0; i < adj_list->size; i++) {
if (adj_list->entry[i].size == size &&
((size +
adj_list->entry[i].left +
adj_list->entry[i].right) > max)) {
max = size +
adj_list->entry[i].left +
adj_list->entry[i].right;
max_size = size;
max_index = adj_list->entry[i].index;
}
}
if (max)
break;
}
/*
* If the max entry is smaller than the largest_free_size
* find the first entry in the free list that it cn fit in to.
*/
if (max_size < largest_free_size) {
for (i = 0; i < free_list->size; i++) {
if (free_list->entry[i].size >= max_size) {
largest_free_index = i;
break;
}
}
}
/*
* If we have a contender then move it to the new spot.
*/
if (max) {
rc = dpool_move(dpool,
free_list->entry[largest_free_index].index,
max_index);
if (rc) {
tfp_free(free_list);
tfp_free(adj_list);
return rc;
}
} else {
break;
}
}
done:
tfp_free(free_list);
tfp_free(adj_list);
return largest_free_size;
}
uint32_t dpool_alloc(struct dpool *dpool,
uint32_t size,
uint8_t defrag)
{
uint32_t i;
uint32_t j;
uint32_t count = 0;
uint32_t first_entry_index;
int rc;
if (size > dpool->max_alloc_size || size == 0)
return DP_INVALID_INDEX;
/*
* Defrag requires EM move support.
*/
if (defrag != DP_DEFRAG_NONE &&
dpool->move_callback == NULL)
return DP_INVALID_INDEX;
while (1) {
/*
* find <size> consecutive free entries
*/
for (i = 0; i < dpool->size; i++) {
if (DP_IS_FREE(dpool->entry[i].flags)) {
if (count == 0)
first_entry_index = i;
count++;
if (count == size) {
for (j = 0; j < size; j++) {
dpool->entry[j + first_entry_index].flags = size;
if (j == 0)
dpool->entry[j + first_entry_index].flags |=
DP_FLAGS_START;
}
dpool->entry[i].entry_data = 0UL;
return (first_entry_index + dpool->start_index);
}
} else {
count = 0;
}
}
/*
* If defragging then do it to it
*/
if (defrag != DP_DEFRAG_NONE) {
rc = dpool_defrag(dpool, size, defrag);
if (rc < 0)
return DP_INVALID_INDEX;
} else {
break;
}
/*
* If the defrag created enough space then try the
* alloc again else quit.
*/
if ((uint32_t)rc < size)
break;
}
return DP_INVALID_INDEX;
}
int dpool_free(struct dpool *dpool,
uint32_t index)
{
uint32_t i;
int start = (index - dpool->start_index);
uint32_t size;
if (start < 0)
return -1;
if (DP_IS_START(dpool->entry[start].flags)) {
size = DP_FLAGS_SIZE(dpool->entry[start].flags);
if (size > dpool->max_alloc_size || size == 0)
return -1;
for (i = start; i < (start + size); i++)
dpool->entry[i].flags = 0;
return 0;
}
return -1;
}
void dpool_free_all(struct dpool *dpool)
{
uint32_t i;
for (i = 0; i < dpool->size; i++)
dpool_free(dpool, dpool->entry[i].index);
}
int dpool_set_entry_data(struct dpool *dpool,
uint32_t index,
uint64_t entry_data)
{
int start = (index - dpool->start_index);
if (start < 0)
return -1;
if (DP_IS_START(dpool->entry[start].flags)) {
dpool->entry[start].entry_data = entry_data;
return 0;
}
return -1;
}
|