33 #include <linux/kernel.h>
34 #include <linux/module.h>
58 cvmx_write64_uint64((addr +
SIZE_OFFSET) | (1ull << 63), size);
63 cvmx_write64_uint64((addr +
NEXT_OFFSET) | (1ull << 63), next);
68 return cvmx_read64_uint64((addr +
SIZE_OFFSET) | (1ull << 63));
73 return cvmx_read64_uint64((addr +
NEXT_OFFSET) | (1ull << 63));
84 return cvmx_phys_to_ptr(address);
110 return cvmx_phys_to_ptr(addr);
165 if (!cvmx_bootmem_desc) {
166 #if defined(CVMX_ABI_64)
168 cvmx_bootmem_desc = cvmx_phys_to_ptr(
CAST64(mem_desc_ptr));
170 cvmx_bootmem_desc = (
struct cvmx_bootmem_desc *) mem_desc_ptr;
198 cvmx_dprintf(
"cvmx_bootmem_phy_alloc: req_size: 0x%llx, "
199 "min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
200 (
unsigned long long)req_size,
201 (
unsigned long long)address_min,
202 (
unsigned long long)address_max,
203 (
unsigned long long)alignment);
208 "version: %d.%d at addr: %p\n",
237 if (address_min && !address_max)
238 address_max = address_min + req_size;
239 else if (!address_min && !address_max)
256 address_min =
ALIGN(address_min, alignment);
263 if (req_size > address_max - address_min)
270 head_addr = cvmx_bootmem_desc->
head_addr;
273 prev_addr = ent_addr,
274 ent_addr = cvmx_bootmem_phy_get_next(ent_addr)) {
276 uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
278 if (cvmx_bootmem_phy_get_next(ent_addr)
279 && ent_addr > cvmx_bootmem_phy_get_next(ent_addr)) {
281 "0x%llx, next: 0x%llx\n",
282 (
unsigned long long)ent_addr,
284 cvmx_bootmem_phy_get_next(ent_addr));
294 ALIGN(
max(address_min, ent_addr), alignment);
295 usable_max =
min(address_max, ent_addr + ent_size);
301 desired_min_addr = usable_base;
306 if (!((ent_addr + ent_size) > usable_base
307 && ent_addr < address_max
308 && req_size <= usable_max - usable_base))
317 desired_min_addr = usable_max - req_size;
322 desired_min_addr &= ~(alignment - 1);
326 if (desired_min_addr == ent_addr) {
327 if (req_size < ent_size) {
332 new_ent_addr = ent_addr + req_size;
333 cvmx_bootmem_phy_set_next(new_ent_addr,
334 cvmx_bootmem_phy_get_next(ent_addr));
335 cvmx_bootmem_phy_set_size(new_ent_addr,
343 cvmx_bootmem_phy_set_next(ent_addr,
352 cvmx_bootmem_phy_set_next(prev_addr,
353 cvmx_bootmem_phy_get_next(ent_addr));
360 cvmx_bootmem_phy_get_next(ent_addr);
362 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
364 return desired_min_addr;
377 new_ent_addr = desired_min_addr;
378 cvmx_bootmem_phy_set_next(new_ent_addr,
379 cvmx_bootmem_phy_get_next
381 cvmx_bootmem_phy_set_size(new_ent_addr,
382 cvmx_bootmem_phy_get_size
386 cvmx_bootmem_phy_set_size(ent_addr,
387 desired_min_addr - ent_addr);
388 cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
393 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
405 cvmx_dprintf(
"__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n",
406 (
unsigned long long)phy_addr, (
unsigned long long)size);
410 "version: %d.%d at addr: %p\n",
424 if (cur_addr == 0 || phy_addr < cur_addr) {
426 if (cur_addr && phy_addr + size > cur_addr)
427 goto bootmem_free_done;
428 else if (phy_addr + size == cur_addr) {
430 cvmx_bootmem_phy_set_next(phy_addr,
431 cvmx_bootmem_phy_get_next
433 cvmx_bootmem_phy_set_size(phy_addr,
434 cvmx_bootmem_phy_get_size
440 cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
441 cvmx_bootmem_phy_set_size(phy_addr, size);
445 goto bootmem_free_done;
449 while (cur_addr && phy_addr > cur_addr) {
450 prev_addr = cur_addr;
451 cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
460 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
462 cvmx_bootmem_phy_set_size(prev_addr,
463 cvmx_bootmem_phy_get_size
466 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
467 cvmx_bootmem_phy_set_size(phy_addr, size);
468 cvmx_bootmem_phy_set_next(phy_addr, 0);
471 goto bootmem_free_done;
477 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
480 cvmx_bootmem_phy_set_size(prev_addr,
481 cvmx_bootmem_phy_get_size
483 if (phy_addr + size == cur_addr) {
485 cvmx_bootmem_phy_set_size(prev_addr,
486 cvmx_bootmem_phy_get_size(cur_addr) +
487 cvmx_bootmem_phy_get_size(prev_addr));
488 cvmx_bootmem_phy_set_next(prev_addr,
489 cvmx_bootmem_phy_get_next(cur_addr));
492 goto bootmem_free_done;
493 }
else if (phy_addr + size == cur_addr) {
495 cvmx_bootmem_phy_set_size(phy_addr,
496 cvmx_bootmem_phy_get_size
498 cvmx_bootmem_phy_set_next(phy_addr,
499 cvmx_bootmem_phy_get_next
501 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
503 goto bootmem_free_done;
507 cvmx_bootmem_phy_set_size(phy_addr, size);
508 cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
509 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
515 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
528 cvmx_dprintf(
"cvmx_bootmem_phy_named_block_find: %s\n", name);
543 (
"cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n",
544 named_block_array_ptr);
549 if ((name && named_block_array_ptr[i].
size
550 && !
strncmp(name, named_block_array_ptr[i].name,
553 || (!name && !named_block_array_ptr[i].
size)) {
554 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
557 return &(named_block_array_ptr[
i]);
562 "version: %d.%d at addr: %p\n",
567 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
578 cvmx_dprintf(
"ERROR: Incompatible bootmem descriptor version: "
579 "%d.%d at addr: %p\n",
586 cvmx_dprintf(
"cvmx_bootmem_phy_named_block_free: %s\n", name);
598 if (named_block_ptr) {
601 "%s, base: 0x%llx, size: 0x%llx\n",
603 (
unsigned long long)named_block_ptr->
base_addr,
604 (
unsigned long long)named_block_ptr->
size);
607 named_block_ptr->
size,
609 named_block_ptr->
size = 0;
614 return named_block_ptr !=
NULL;
623 int64_t addr_allocated;
627 cvmx_dprintf(
"cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: "
628 "0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
629 (
unsigned long long)size,
630 (
unsigned long long)min_addr,
631 (
unsigned long long)max_addr,
632 (
unsigned long long)alignment,
636 cvmx_dprintf(
"ERROR: Incompatible bootmem descriptor version: "
637 "%d.%d at addr: %p\n",
652 named_block_desc_ptr =
654 flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
661 flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr) {
662 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
678 flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
679 if (addr_allocated >= 0) {
680 named_block_desc_ptr->
base_addr = addr_allocated;
687 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
689 return addr_allocated;