1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
45 * Simple allocate only memory allocator. Used to allocate memory at application
48 * <hr>$Revision: 41586 $<hr>
53 #include "cvmx-spinlock.h"
54 #include "cvmx-bootmem.h"
61 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
64 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
66 #define ALIGN_ADDR_UP(addr, align) (((addr) + (~(align))) & (align))
68 static CVMX_SHARED cvmx_bootmem_desc_t *cvmx_bootmem_desc = NULL;
70 /* See header file for descriptions of functions */
72 /* Wrapper functions are provided for reading/writing the size and next block
73 ** values as these may not be directly addressible (in 32 bit applications, for instance.)
75 /* Offsets of data elements in bootmem list, must match cvmx_bootmem_block_header_t */
78 static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)
80 cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);
82 static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)
84 cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);
86 static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)
88 return(cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63)));
90 static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)
92 return(cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63)));
96 /* This functions takes an address range and adjusts it as necessary to
97 ** match the ABI that is currently being used. This is required to ensure
98 ** that bootmem_alloc* functions only return valid pointers for 32 bit ABIs */
99 static int __cvmx_validate_mem_range(uint64_t *min_addr_ptr, uint64_t *max_addr_ptr)
102 #if defined(__linux__) && defined(CVMX_ABI_N32)
104 extern uint64_t linux_mem32_min;
105 extern uint64_t linux_mem32_max;
106 /* For 32 bit Linux apps, we need to restrict the allocations to the range
107 ** of memory configured for access from userspace. Also, we need to add mappings
108 ** for the data structures that we access.*/
110 /* Narrow range requests to be bounded by the 32 bit limits. octeon_phy_mem_block_alloc()
111 ** will reject inconsistent req_size/range requests, so we don't repeat those checks here.
112 ** If max unspecified, set to 32 bit maximum. */
113 *min_addr_ptr = MIN(MAX(*min_addr_ptr, linux_mem32_min), linux_mem32_max);
115 *max_addr_ptr = linux_mem32_max;
117 *max_addr_ptr = MAX(MIN(*max_addr_ptr, linux_mem32_max), linux_mem32_min);
119 #elif defined(CVMX_ABI_N32)
121 uint32_t max_phys = 0x0FFFFFFF; /* Max physical address when 1-1 mappings not used */
122 #if CVMX_USE_1_TO_1_TLB_MAPPINGS
123 max_phys = 0x7FFFFFFF;
125 /* We are are running standalone simple executive, so we need to limit the range
126 ** that we allocate from */
128 /* Narrow range requests to be bounded by the 32 bit limits. octeon_phy_mem_block_alloc()
129 ** will reject inconsistent req_size/range requests, so we don't repeat those checks here.
130 ** If max unspecified, set to 32 bit maximum. */
131 *min_addr_ptr = MIN(MAX(*min_addr_ptr, 0x0), max_phys);
133 *max_addr_ptr = max_phys;
135 *max_addr_ptr = MAX(MIN(*max_addr_ptr, max_phys), 0x0);
143 void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment, uint64_t min_addr, uint64_t max_addr)
147 __cvmx_validate_mem_range(&min_addr, &max_addr);
148 address = cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);
151 return cvmx_phys_to_ptr(address);
156 void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address, uint64_t alignment)
158 return cvmx_bootmem_alloc_range(size, alignment, address, address + size);
162 void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment)
164 return cvmx_bootmem_alloc_range(size, alignment, 0, 0);
167 void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr, uint64_t max_addr, uint64_t align, char *name)
171 __cvmx_validate_mem_range(&min_addr, &max_addr);
172 addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr, align, name, 0);
174 return cvmx_phys_to_ptr(addr);
179 void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address, char *name)
181 return(cvmx_bootmem_alloc_named_range(size, address, address + size, 0, name));
183 void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, char *name)
185 return(cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name));
188 int cvmx_bootmem_free_named(char *name)
190 return(cvmx_bootmem_phy_named_block_free(name, 0));
193 cvmx_bootmem_named_block_desc_t * cvmx_bootmem_find_named_block(char *name)
195 return(cvmx_bootmem_phy_named_block_find(name, 0));
198 void cvmx_bootmem_print_named(void)
200 cvmx_bootmem_phy_named_block_print();
203 #if defined(__linux__) && defined(CVMX_ABI_N32)
204 cvmx_bootmem_named_block_desc_t *linux32_named_block_array_ptr;
207 int cvmx_bootmem_init(void *mem_desc_ptr)
209 /* Verify that the size of cvmx_spinlock_t meets our assumptions */
210 if (sizeof(cvmx_spinlock_t) != 4)
212 cvmx_dprintf("ERROR: Unexpected size of cvmx_spinlock_t\n");
216 /* Here we set the global pointer to the bootmem descriptor block. This pointer will
217 ** be used directly, so we will set it up to be directly usable by the application.
218 ** It is set up as follows for the various runtime/ABI combinations:
219 ** Linux 64 bit: Set XKPHYS bit
220 ** Linux 32 bit: use mmap to create mapping, use virtual address
221 ** CVMX 64 bit: use physical address directly
222 ** CVMX 32 bit: use physical address directly
223 ** Note that the CVMX environment assumes the use of 1-1 TLB mappings so that the physical addresses
224 ** can be used directly
226 if (!cvmx_bootmem_desc)
228 #if defined(CVMX_BUILD_FOR_LINUX_USER) && defined(CVMX_ABI_N32)
230 /* For 32 bit, we need to use mmap to create a mapping for the bootmem descriptor */
231 int dm_fd = open("/dev/mem", O_RDWR);
234 cvmx_dprintf("ERROR opening /dev/mem for boot descriptor mapping\n");
238 base_ptr = mmap(NULL,
239 sizeof(cvmx_bootmem_desc_t) + sysconf(_SC_PAGESIZE),
240 PROT_READ | PROT_WRITE,
243 ((off_t)mem_desc_ptr) & ~(sysconf(_SC_PAGESIZE) - 1));
245 if (MAP_FAILED == base_ptr)
247 cvmx_dprintf("Error mapping bootmem descriptor!\n");
252 /* Adjust pointer to point to bootmem_descriptor, rather than start of page it is in */
253 cvmx_bootmem_desc = (cvmx_bootmem_desc_t*)((char*)base_ptr + (((off_t)mem_desc_ptr) & (sysconf(_SC_PAGESIZE) - 1)));
255 /* Also setup mapping for named memory block desc. while we are at it. Here we must keep another
256 ** pointer around, as the value in the bootmem descriptor is shared with other applications. */
257 base_ptr = mmap(NULL,
258 sizeof(cvmx_bootmem_named_block_desc_t) * cvmx_bootmem_desc->named_block_num_blocks + sysconf(_SC_PAGESIZE),
259 PROT_READ | PROT_WRITE,
262 ((off_t)cvmx_bootmem_desc->named_block_array_addr) & ~(sysconf(_SC_PAGESIZE) - 1));
266 if (MAP_FAILED == base_ptr)
268 cvmx_dprintf("Error mapping named block descriptor!\n");
272 /* Adjust pointer to point to named block array, rather than start of page it is in */
273 linux32_named_block_array_ptr = (cvmx_bootmem_named_block_desc_t*)((char*)base_ptr + (((off_t)cvmx_bootmem_desc->named_block_array_addr) & (sysconf(_SC_PAGESIZE) - 1)));
275 #elif (defined(CVMX_BUILD_FOR_LINUX_KERNEL) || defined(CVMX_BUILD_FOR_LINUX_USER)) && defined(CVMX_ABI_64)
277 cvmx_bootmem_desc = cvmx_phys_to_ptr(CAST64(mem_desc_ptr));
279 cvmx_bootmem_desc = (cvmx_bootmem_desc_t*)mem_desc_ptr;
288 uint64_t cvmx_bootmem_available_mem(uint64_t min_block_size)
290 return(cvmx_bootmem_phy_available_mem(min_block_size));
297 /*********************************************************************
298 ** The cvmx_bootmem_phy* functions below return 64 bit physical addresses,
299 ** and expose more features that the cvmx_bootmem_functions above. These are
300 ** required for full memory space access in 32 bit applications, as well as for
301 ** using some advance features.
302 ** Most applications should not need to use these.
307 int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min, uint64_t address_max, uint64_t alignment, uint32_t flags)
312 uint64_t prev_addr = 0; /* points to previous list entry, NULL current entry is head of list */
313 uint64_t new_ent_addr = 0;
314 uint64_t desired_min_addr;
315 uint64_t alignment_mask = ~(alignment - 1);
318 cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
319 (unsigned long long)req_size, (unsigned long long)address_min, (unsigned long long)address_max, (unsigned long long)alignment);
322 if (cvmx_bootmem_desc->major_version > 3)
324 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
325 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
329 /* Do a variety of checks to validate the arguments. The allocator code will later assume
330 ** that these checks have been made. We validate that the requested constraints are not
331 ** self-contradictory before we look through the list of available memory
334 /* 0 is not a valid req_size for this allocator */
338 /* Round req_size up to mult of minimum alignment bytes */
339 req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) & ~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
341 /* Convert !0 address_min and 0 address_max to special case of range that specifies an exact
342 ** memory block to allocate. Do this before other checks and adjustments so that this tranformation will be validated */
343 if (address_min && !address_max)
344 address_max = address_min + req_size;
345 else if (!address_min && !address_max)
346 address_max = ~0ull; /* If no limits given, use max limits */
351 /* Enforce minimum alignment (this also keeps the minimum free block
352 ** req_size the same as the alignment req_size */
353 if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)
355 alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;
357 alignment_mask = ~(alignment - 1);
359 /* Adjust address minimum based on requested alignment (round up to meet alignment). Do this here so we can
360 ** reject impossible requests up front. (NOP for address_min == 0) */
362 address_min = (address_min + (alignment - 1)) & ~(alignment - 1);
365 /* Reject inconsistent args. We have adjusted these, so this may fail due to our internal changes
366 ** even if this check would pass for the values the user supplied. */
367 if (req_size > address_max - address_min)
370 /* Walk through the list entries - first fit found is returned */
372 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
373 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
374 head_addr = cvmx_bootmem_desc->head_addr;
375 ent_addr = head_addr;
378 uint64_t usable_base, usable_max;
379 uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
381 if (cvmx_bootmem_phy_get_next(ent_addr) && ent_addr > cvmx_bootmem_phy_get_next(ent_addr))
383 cvmx_dprintf("Internal bootmem_alloc() error: ent: 0x%llx, next: 0x%llx\n",
384 (unsigned long long)ent_addr, (unsigned long long)cvmx_bootmem_phy_get_next(ent_addr));
388 /* Determine if this is an entry that can satisify the request */
389 /* Check to make sure entry is large enough to satisfy request */
390 usable_base = ALIGN_ADDR_UP(MAX(address_min, ent_addr), alignment_mask);
391 usable_max = MIN(address_max, ent_addr + ent_size);
392 /* We should be able to allocate block at address usable_base */
394 desired_min_addr = usable_base;
396 /* Determine if request can be satisfied from the current entry */
397 if ((((ent_addr + ent_size) > usable_base && ent_addr < address_max))
398 && req_size <= usable_max - usable_base)
400 /* We have found an entry that has room to satisfy the request, so allocate it from this entry */
402 /* If end CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from the end of this block
403 ** rather than the beginning */
404 if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC)
406 desired_min_addr = usable_max - req_size;
407 /* Align desired address down to required alignment */
408 desired_min_addr &= alignment_mask;
411 /* Match at start of entry */
412 if (desired_min_addr == ent_addr)
414 if (req_size < ent_size)
416 /* big enough to create a new block from top portion of block */
417 new_ent_addr = ent_addr + req_size;
418 cvmx_bootmem_phy_set_next(new_ent_addr, cvmx_bootmem_phy_get_next(ent_addr));
419 cvmx_bootmem_phy_set_size(new_ent_addr, ent_size - req_size);
421 /* Adjust next pointer as following code uses this */
422 cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
425 /* adjust prev ptr or head to remove this entry from list */
428 cvmx_bootmem_phy_set_next(prev_addr, cvmx_bootmem_phy_get_next(ent_addr));
432 /* head of list being returned, so update head ptr */
433 cvmx_bootmem_desc->head_addr = cvmx_bootmem_phy_get_next(ent_addr);
435 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
436 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
437 return(desired_min_addr);
441 /* block returned doesn't start at beginning of entry, so we know
442 ** that we will be splitting a block off the front of this one. Create a new block
443 ** from the beginning, add to list, and go to top of loop again.
445 ** create new block from high portion of block, so that top block
446 ** starts at desired addr
448 new_ent_addr = desired_min_addr;
449 cvmx_bootmem_phy_set_next(new_ent_addr, cvmx_bootmem_phy_get_next(ent_addr));
450 cvmx_bootmem_phy_set_size(new_ent_addr, cvmx_bootmem_phy_get_size(ent_addr) - (desired_min_addr - ent_addr));
451 cvmx_bootmem_phy_set_size(ent_addr, desired_min_addr - ent_addr);
452 cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
453 /* Loop again to handle actual alloc from new block */
456 prev_addr = ent_addr;
457 ent_addr = cvmx_bootmem_phy_get_next(ent_addr);
460 /* We didn't find anything, so return error */
461 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
462 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
468 int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)
471 uint64_t prev_addr = 0; /* zero is invalid */
475 cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n", (unsigned long long)phy_addr, (unsigned long long)size);
477 if (cvmx_bootmem_desc->major_version > 3)
479 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
480 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
484 /* 0 is not a valid size for this allocator */
489 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
490 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
491 cur_addr = cvmx_bootmem_desc->head_addr;
492 if (cur_addr == 0 || phy_addr < cur_addr)
494 /* add at front of list - special case with changing head ptr */
495 if (cur_addr && phy_addr + size > cur_addr)
496 goto bootmem_free_done; /* error, overlapping section */
497 else if (phy_addr + size == cur_addr)
499 /* Add to front of existing first block */
500 cvmx_bootmem_phy_set_next(phy_addr, cvmx_bootmem_phy_get_next(cur_addr));
501 cvmx_bootmem_phy_set_size(phy_addr, cvmx_bootmem_phy_get_size(cur_addr) + size);
502 cvmx_bootmem_desc->head_addr = phy_addr;
507 /* New block before first block */
508 cvmx_bootmem_phy_set_next(phy_addr, cur_addr); /* OK if cur_addr is 0 */
509 cvmx_bootmem_phy_set_size(phy_addr, size);
510 cvmx_bootmem_desc->head_addr = phy_addr;
513 goto bootmem_free_done;
516 /* Find place in list to add block */
517 while (cur_addr && phy_addr > cur_addr)
519 prev_addr = cur_addr;
520 cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
525 /* We have reached the end of the list, add on to end, checking
526 ** to see if we need to combine with last block
528 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) == phy_addr)
530 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(prev_addr) + size);
534 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
535 cvmx_bootmem_phy_set_size(phy_addr, size);
536 cvmx_bootmem_phy_set_next(phy_addr, 0);
539 goto bootmem_free_done;
543 /* insert between prev and cur nodes, checking for merge with either/both */
545 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) == phy_addr)
547 /* Merge with previous */
548 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(prev_addr) + size);
549 if (phy_addr + size == cur_addr)
551 /* Also merge with current */
552 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(cur_addr) + cvmx_bootmem_phy_get_size(prev_addr));
553 cvmx_bootmem_phy_set_next(prev_addr, cvmx_bootmem_phy_get_next(cur_addr));
556 goto bootmem_free_done;
558 else if (phy_addr + size == cur_addr)
560 /* Merge with current */
561 cvmx_bootmem_phy_set_size(phy_addr, cvmx_bootmem_phy_get_size(cur_addr) + size);
562 cvmx_bootmem_phy_set_next(phy_addr, cvmx_bootmem_phy_get_next(cur_addr));
563 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
565 goto bootmem_free_done;
568 /* It is a standalone block, add in between prev and cur */
569 cvmx_bootmem_phy_set_size(phy_addr, size);
570 cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
571 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
578 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
579 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
586 void cvmx_bootmem_phy_list_print(void)
590 addr = cvmx_bootmem_desc->head_addr;
591 cvmx_dprintf("\n\n\nPrinting bootmem block list, descriptor: %p, head is 0x%llx\n",
592 cvmx_bootmem_desc, (unsigned long long)addr);
593 cvmx_dprintf("Descriptor version: %d.%d\n", (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version);
594 if (cvmx_bootmem_desc->major_version > 3)
596 cvmx_dprintf("Warning: Bootmem descriptor version is newer than expected\n");
600 cvmx_dprintf("mem list is empty!\n");
604 cvmx_dprintf("Block address: 0x%08qx, size: 0x%08qx, next: 0x%08qx\n",
605 (unsigned long long)addr,
606 (unsigned long long)cvmx_bootmem_phy_get_size(addr),
607 (unsigned long long)cvmx_bootmem_phy_get_next(addr));
608 addr = cvmx_bootmem_phy_get_next(addr);
610 cvmx_dprintf("\n\n");
615 uint64_t cvmx_bootmem_phy_available_mem(uint64_t min_block_size)
619 uint64_t available_mem = 0;
621 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
622 addr = cvmx_bootmem_desc->head_addr;
625 if (cvmx_bootmem_phy_get_size(addr) >= min_block_size)
626 available_mem += cvmx_bootmem_phy_get_size(addr);
627 addr = cvmx_bootmem_phy_get_next(addr);
629 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
630 return(available_mem);
636 cvmx_bootmem_named_block_desc_t * cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags)
639 cvmx_bootmem_named_block_desc_t *named_block_array_ptr;
643 cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);
645 /* Lock the structure to make sure that it is not being changed while we are
648 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
649 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
651 #if defined(__linux__) && !defined(CONFIG_OCTEON_U_BOOT)
653 /* Need to use mmapped named block pointer in 32 bit linux apps */
654 extern cvmx_bootmem_named_block_desc_t *linux32_named_block_array_ptr;
655 named_block_array_ptr = linux32_named_block_array_ptr;
657 /* Use XKPHYS for 64 bit linux */
658 named_block_array_ptr = (cvmx_bootmem_named_block_desc_t *)cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);
661 /* Simple executive case. (and u-boot)
662 ** This could be in the low 1 meg of memory that is not 1-1 mapped, so we need use XKPHYS/KSEG0 addressing for it */
663 named_block_array_ptr = CASTPTR(cvmx_bootmem_named_block_desc_t, CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,cvmx_bootmem_desc->named_block_array_addr));
667 cvmx_dprintf("cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n", named_block_array_ptr);
669 if (cvmx_bootmem_desc->major_version == 3)
671 for (i = 0; i < cvmx_bootmem_desc->named_block_num_blocks; i++)
673 if ((name && named_block_array_ptr[i].size && !strncmp(name, named_block_array_ptr[i].name, cvmx_bootmem_desc->named_block_name_len - 1))
674 || (!name && !named_block_array_ptr[i].size))
676 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
677 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
679 return(&(named_block_array_ptr[i]));
685 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
686 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
688 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
689 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
694 int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags)
696 cvmx_bootmem_named_block_desc_t *named_block_ptr;
698 if (cvmx_bootmem_desc->major_version != 3)
700 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
701 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
705 cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);
708 /* Take lock here, as name lookup/block free/name free need to be atomic */
709 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
711 named_block_ptr = cvmx_bootmem_phy_named_block_find(name, CVMX_BOOTMEM_FLAG_NO_LOCKING);
715 cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s, base: 0x%llx, size: 0x%llx\n", name, (unsigned long long)named_block_ptr->base_addr, (unsigned long long)named_block_ptr->size);
717 __cvmx_bootmem_phy_free(named_block_ptr->base_addr, named_block_ptr->size, CVMX_BOOTMEM_FLAG_NO_LOCKING);
718 named_block_ptr->size = 0;
719 /* Set size to zero to indicate block not used. */
722 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
724 return(!!named_block_ptr); /* 0 on failure, 1 on success */
731 int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr, uint64_t max_addr, uint64_t alignment, char *name, uint32_t flags)
733 int64_t addr_allocated;
734 cvmx_bootmem_named_block_desc_t *named_block_desc_ptr;
737 cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: 0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
738 (unsigned long long)size,
739 (unsigned long long)min_addr,
740 (unsigned long long)max_addr,
741 (unsigned long long)alignment,
744 if (cvmx_bootmem_desc->major_version != 3)
746 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
747 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
752 /* Take lock here, as name lookup/block alloc/name add need to be atomic */
754 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
755 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
757 /* Get pointer to first available named block descriptor */
758 named_block_desc_ptr = cvmx_bootmem_phy_named_block_find(NULL, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
760 /* Check to see if name already in use, return error if name
761 ** not available or no more room for blocks.
763 if (cvmx_bootmem_phy_named_block_find(name, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr)
765 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
766 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
771 /* Round size up to mult of minimum alignment bytes
772 ** We need the actual size allocated to allow for blocks to be coallesced
773 ** when they are freed. The alloc routine does the same rounding up
774 ** on all allocations. */
775 size = (size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) & ~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
777 addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
778 if (addr_allocated >= 0)
780 named_block_desc_ptr->base_addr = addr_allocated;
781 named_block_desc_ptr->size = size;
782 strncpy(named_block_desc_ptr->name, name, cvmx_bootmem_desc->named_block_name_len);
783 named_block_desc_ptr->name[cvmx_bootmem_desc->named_block_name_len - 1] = 0;
786 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
787 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
789 return(addr_allocated);
795 void cvmx_bootmem_phy_named_block_print(void)
800 #if defined(__linux__) && !defined(CONFIG_OCTEON_U_BOOT)
802 /* Need to use mmapped named block pointer in 32 bit linux apps */
803 extern cvmx_bootmem_named_block_desc_t *linux32_named_block_array_ptr;
804 cvmx_bootmem_named_block_desc_t *named_block_array_ptr = linux32_named_block_array_ptr;
806 /* Use XKPHYS for 64 bit linux */
807 cvmx_bootmem_named_block_desc_t *named_block_array_ptr = (cvmx_bootmem_named_block_desc_t *)cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);
810 /* Simple executive case. (and u-boot)
811 ** This could be in the low 1 meg of memory that is not 1-1 mapped, so we need use XKPHYS/KSEG0 addressing for it */
812 cvmx_bootmem_named_block_desc_t *named_block_array_ptr = CASTPTR(cvmx_bootmem_named_block_desc_t, CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,cvmx_bootmem_desc->named_block_array_addr));
815 cvmx_dprintf("cvmx_bootmem_phy_named_block_print, desc addr: %p\n", cvmx_bootmem_desc);
817 if (cvmx_bootmem_desc->major_version != 3)
819 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: %p\n",
820 (int)cvmx_bootmem_desc->major_version, (int)cvmx_bootmem_desc->minor_version, cvmx_bootmem_desc);
823 cvmx_dprintf("List of currently allocated named bootmem blocks:\n");
824 for (i = 0; i < cvmx_bootmem_desc->named_block_num_blocks; i++)
826 if (named_block_array_ptr[i].size)
829 cvmx_dprintf("Name: %s, address: 0x%08qx, size: 0x%08qx, index: %d\n",
830 named_block_array_ptr[i].name,
831 (unsigned long long)named_block_array_ptr[i].base_addr,
832 (unsigned long long)named_block_array_ptr[i].size,
839 cvmx_dprintf("No named bootmem blocks exist.\n");
845 /* Real physical addresses of memory regions */
846 #define OCTEON_DDR0_BASE (0x0ULL)
847 #define OCTEON_DDR0_SIZE (0x010000000ULL)
848 #define OCTEON_DDR1_BASE (0x410000000ULL)
849 #define OCTEON_DDR1_SIZE (0x010000000ULL)
850 #define OCTEON_DDR2_BASE (0x020000000ULL)
851 #define OCTEON_DDR2_SIZE (0x3e0000000ULL)
852 #define OCTEON_MAX_PHY_MEM_SIZE (16*1024*1024*1024ULL)
853 int64_t cvmx_bootmem_phy_mem_list_init(uint64_t mem_size, uint32_t low_reserved_bytes, cvmx_bootmem_desc_t *desc_buffer)
855 uint64_t cur_block_addr;
859 cvmx_dprintf("cvmx_bootmem_phy_mem_list_init (arg desc ptr: %p, cvmx_bootmem_desc: %p)\n", desc_buffer, cvmx_bootmem_desc);
862 /* Descriptor buffer needs to be in 32 bit addressable space to be compatible with
863 ** 32 bit applications */
866 cvmx_dprintf("ERROR: no memory for cvmx_bootmem descriptor provided\n");
870 if (mem_size > OCTEON_MAX_PHY_MEM_SIZE)
872 mem_size = OCTEON_MAX_PHY_MEM_SIZE;
873 cvmx_dprintf("ERROR: requested memory size too large, truncating to maximum size\n");
876 if (cvmx_bootmem_desc)
879 /* Initialize cvmx pointer to descriptor */
880 cvmx_bootmem_init(desc_buffer);
882 /* Set up global pointer to start of list, exclude low 64k for exception vectors, space for global descriptor */
883 memset(cvmx_bootmem_desc, 0x0, sizeof(cvmx_bootmem_desc_t));
884 /* Set version of bootmem descriptor */
885 cvmx_bootmem_desc->major_version = CVMX_BOOTMEM_DESC_MAJ_VER;
886 cvmx_bootmem_desc->minor_version = CVMX_BOOTMEM_DESC_MIN_VER;
888 cur_block_addr = cvmx_bootmem_desc->head_addr = (OCTEON_DDR0_BASE + low_reserved_bytes);
890 cvmx_bootmem_desc->head_addr = 0;
892 if (mem_size <= OCTEON_DDR0_SIZE)
894 __cvmx_bootmem_phy_free(cur_block_addr, mem_size - low_reserved_bytes, 0);
898 __cvmx_bootmem_phy_free(cur_block_addr, OCTEON_DDR0_SIZE - low_reserved_bytes, 0);
900 mem_size -= OCTEON_DDR0_SIZE;
902 /* Add DDR2 block next if present */
903 if (mem_size > OCTEON_DDR1_SIZE)
905 __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, OCTEON_DDR1_SIZE, 0);
906 __cvmx_bootmem_phy_free(OCTEON_DDR2_BASE, mem_size - OCTEON_DDR1_SIZE, 0);
910 __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, mem_size, 0);
915 /* Initialize the named block structure */
916 cvmx_bootmem_desc->named_block_name_len = CVMX_BOOTMEM_NAME_LEN;
917 cvmx_bootmem_desc->named_block_num_blocks = CVMX_BOOTMEM_NUM_NAMED_BLOCKS;
918 cvmx_bootmem_desc->named_block_array_addr = 0;
920 /* Allocate this near the top of the low 256 MBytes of memory */
921 addr = cvmx_bootmem_phy_alloc(CVMX_BOOTMEM_NUM_NAMED_BLOCKS * sizeof(cvmx_bootmem_named_block_desc_t),0, 0x10000000, 0 ,CVMX_BOOTMEM_FLAG_END_ALLOC);
923 cvmx_bootmem_desc->named_block_array_addr = addr;
926 cvmx_dprintf("cvmx_bootmem_phy_mem_list_init: named_block_array_addr: 0x%llx)\n", (unsigned long long)cvmx_bootmem_desc->named_block_array_addr);
928 if (!cvmx_bootmem_desc->named_block_array_addr)
930 cvmx_dprintf("FATAL ERROR: unable to allocate memory for bootmem descriptor!\n");
933 memset((void *)(unsigned long)cvmx_bootmem_desc->named_block_array_addr, 0x0, CVMX_BOOTMEM_NUM_NAMED_BLOCKS * sizeof(cvmx_bootmem_named_block_desc_t));
939 void cvmx_bootmem_lock(void)
941 cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
944 void cvmx_bootmem_unlock(void)
946 cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
949 void *__cvmx_bootmem_internal_get_desc_ptr(void)
951 return(cvmx_bootmem_desc);