1 /***********************license start***************
2 * Copyright (c) 2003-2010 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 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
47 * Simple allocate only memory allocator. Used to allocate memory at application
50 * <hr>$Revision: 52119 $<hr>
53 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
54 #include <linux/module.h>
55 #include <asm/octeon/cvmx.h>
56 #include <asm/octeon/cvmx-bootmem.h>
58 #if !defined(__FreeBSD__) || !defined(_KERNEL)
59 #include "executive-config.h"
62 #include "cvmx-bootmem.h"
64 typedef uint32_t cvmx_spinlock_t;
69 #define ULL unsigned long long
71 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
74 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
76 #define ALIGN_ADDR_UP(addr, align) (((addr) + (~(align))) & (align))
79 * This is the physical location of a cvmx_bootmem_desc_t
80 * structure in Octeon's memory. Note that dues to addressing
81 * limits or runtime environment it might not be possible to
82 * create a C pointer to this structure.
84 static CVMX_SHARED uint64_t cvmx_bootmem_desc_addr = 0;
87 * This macro returns the size of a member of a structure.
88 * Logically it is the same as "sizeof(s::field)" in C++, but
89 * C lacks the "::" operator.
91 #define SIZEOF_FIELD(s, field) sizeof(((s*)NULL)->field)
94 * This macro returns a member of the cvmx_bootmem_desc_t
95 * structure. These members can't be directly addressed as
96 * they might be in memory not directly reachable. In the case
97 * where bootmem is compiled with LINUX_HOST, the structure
98 * itself might be located on a remote Octeon. The argument
99 * "field" is the member name of the cvmx_bootmem_desc_t to read.
100 * Regardless of the type of the field, the return type is always
103 #define CVMX_BOOTMEM_DESC_GET_FIELD(field) \
104 __cvmx_bootmem_desc_get(cvmx_bootmem_desc_addr, \
105 offsetof(cvmx_bootmem_desc_t, field), \
106 SIZEOF_FIELD(cvmx_bootmem_desc_t, field))
109 * This macro writes a member of the cvmx_bootmem_desc_t
110 * structure. These members can't be directly addressed as
111 * they might be in memory not directly reachable. In the case
112 * where bootmem is compiled with LINUX_HOST, the structure
113 * itself might be located on a remote Octeon. The argument
114 * "field" is the member name of the cvmx_bootmem_desc_t to write.
116 #define CVMX_BOOTMEM_DESC_SET_FIELD(field, value) \
117 __cvmx_bootmem_desc_set(cvmx_bootmem_desc_addr, \
118 offsetof(cvmx_bootmem_desc_t, field), \
119 SIZEOF_FIELD(cvmx_bootmem_desc_t, field), value)
122 * This macro returns a member of the
123 * cvmx_bootmem_named_block_desc_t structure. These members can't
124 * be directly addressed as they might be in memory not directly
125 * reachable. In the case where bootmem is compiled with
126 * LINUX_HOST, the structure itself might be located on a remote
127 * Octeon. The argument "field" is the member name of the
128 * cvmx_bootmem_named_block_desc_t to read. Regardless of the type
129 * of the field, the return type is always a uint64_t. The "addr"
130 * parameter is the physical address of the structure.
132 #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \
133 __cvmx_bootmem_desc_get(addr, \
134 offsetof(cvmx_bootmem_named_block_desc_t, field), \
135 SIZEOF_FIELD(cvmx_bootmem_named_block_desc_t, field))
138 * This macro writes a member of the cvmx_bootmem_named_block_desc_t
139 * structure. These members can't be directly addressed as
140 * they might be in memory not directly reachable. In the case
141 * where bootmem is compiled with LINUX_HOST, the structure
142 * itself might be located on a remote Octeon. The argument
143 * "field" is the member name of the
144 * cvmx_bootmem_named_block_desc_t to write. The "addr" parameter
145 * is the physical address of the structure.
147 #define CVMX_BOOTMEM_NAMED_SET_FIELD(addr, field, value) \
148 __cvmx_bootmem_desc_set(addr, \
149 offsetof(cvmx_bootmem_named_block_desc_t, field), \
150 SIZEOF_FIELD(cvmx_bootmem_named_block_desc_t, field), value)
153 * This function is the implementation of the get macros defined
154 * for individual structure members. The argument are generated
155 * by the macros inorder to read only the needed memory.
157 * @param base 64bit physical address of the complete structure
158 * @param offset Offset from the beginning of the structure to the member being
160 * @param size Size of the structure member.
162 * @return Value of the structure member promoted into a uint64_t.
164 static inline uint64_t __cvmx_bootmem_desc_get(uint64_t base, int offset, int size)
166 base = (1ull << 63) | (base + offset);
170 return cvmx_read64_uint32(base);
172 return cvmx_read64_uint64(base);
179 * This function is the implementation of the set macros defined
180 * for individual structure members. The argument are generated
181 * by the macros in order to write only the needed memory.
183 * @param base 64bit physical address of the complete structure
184 * @param offset Offset from the beginning of the structure to the member being
186 * @param size Size of the structure member.
187 * @param value Value to write into the structure
189 static inline void __cvmx_bootmem_desc_set(uint64_t base, int offset, int size, uint64_t value)
191 base = (1ull << 63) | (base + offset);
195 cvmx_write64_uint32(base, value);
198 cvmx_write64_uint64(base, value);
206 * This function retrieves the string name of a named block. It is
207 * more complicated than a simple memcpy() since the named block
208 * descriptor may not be directly accessable.
210 * @param addr Physical address of the named block descriptor
211 * @param str String to receive the named block string name
212 * @param len Length of the string buffer, which must match the length
213 * stored in the bootmem descriptor.
215 static void CVMX_BOOTMEM_NAMED_GET_NAME(uint64_t addr, char *str, int len)
217 #ifndef CVMX_BUILD_FOR_LINUX_HOST
220 addr |= (1ull << 63);
221 addr += offsetof(cvmx_bootmem_named_block_desc_t, name);
223 *ptr++ = cvmx_read64_uint8(addr++);
226 extern void octeon_remote_read_mem(void *buffer, uint64_t physical_address, int length);
227 addr += offsetof(cvmx_bootmem_named_block_desc_t, name);
228 octeon_remote_read_mem(str, addr, len);
234 * This function stores the string name of a named block. It is
235 * more complicated than a simple memcpy() since the named block
236 * descriptor may not be directly accessable.
238 * @param addr Physical address of the named block descriptor
239 * @param str String to store into the named block string name
240 * @param len Length of the string buffer, which must match the length
241 * stored in the bootmem descriptor.
243 static void CVMX_BOOTMEM_NAMED_SET_NAME(uint64_t addr, const char *str, int len)
245 #ifndef CVMX_BUILD_FOR_LINUX_HOST
247 addr |= (1ull << 63);
248 addr += offsetof(cvmx_bootmem_named_block_desc_t, name);
252 cvmx_write64_uint8(addr++, *str++);
254 cvmx_write64_uint8(addr++, 0);
257 extern void octeon_remote_write_mem(uint64_t physical_address, const void *buffer, int length);
259 addr += offsetof(cvmx_bootmem_named_block_desc_t, name);
260 octeon_remote_write_mem(addr, str, len-1);
261 octeon_remote_write_mem(addr+len-1, &zero, 1);
265 /* See header file for descriptions of functions */
267 /* Wrapper functions are provided for reading/writing the size and next block
268 ** values as these may not be directly addressible (in 32 bit applications, for instance.)
270 /* Offsets of data elements in bootmem list, must match cvmx_bootmem_block_header_t */
271 #define NEXT_OFFSET 0
272 #define SIZE_OFFSET 8
273 static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)
275 cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);
277 static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)
279 cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);
281 static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)
283 return(cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63)));
285 static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)
287 return(cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63)));
291 * Check the version information on the bootmem descriptor
294 * Exact major version to check against. A zero means
295 * check that the version supports named blocks.
297 * @return Zero if the version is correct. Negative if the version is
298 * incorrect. Failures also cause a message to be displayed.
300 static int __cvmx_bootmem_check_version(int exact_match)
303 #ifdef CVMX_BUILD_FOR_LINUX_HOST
304 if (!cvmx_bootmem_desc_addr)
305 cvmx_bootmem_desc_addr = cvmx_read64_uint64(0x24100);
307 major_version = CVMX_BOOTMEM_DESC_GET_FIELD(major_version);
308 if ((major_version > 3) || (exact_match && major_version != exact_match))
310 cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: %d.%d at addr: 0x%llx\n",
311 major_version, (int)CVMX_BOOTMEM_DESC_GET_FIELD(minor_version),
312 (ULL)cvmx_bootmem_desc_addr);
320 * Get the low level bootmem descriptor lock. If no locking
321 * is specified in the flags, then nothing is done.
323 * @param flags CVMX_BOOTMEM_FLAG_NO_LOCKING means this functions should do
324 * nothing. This is used to support nested bootmem calls.
326 static inline void __cvmx_bootmem_lock(uint32_t flags)
328 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
330 #ifndef CVMX_BUILD_FOR_LINUX_HOST
331 /* Unfortunately we can't use the normal cvmx-spinlock code as the
332 memory for the bootmem descriptor may be not accessable by a C
333 pointer. We use a 64bit XKPHYS address to access the memory
335 uint64_t lock_addr = (1ull << 63) | (cvmx_bootmem_desc_addr + offsetof(cvmx_bootmem_desc_t, lock));
338 __asm__ __volatile__(
340 "1: ll %[tmp], 0(%[addr])\n"
341 " bnez %[tmp], 1b \n"
343 " sc %[tmp], 0(%[addr])\n"
344 " beqz %[tmp], 1b \n"
348 : [addr] "r" (lock_addr)
355 * Release the low level bootmem descriptor lock. If no locking
356 * is specified in the flags, then nothing is done.
358 * @param flags CVMX_BOOTMEM_FLAG_NO_LOCKING means this functions should do
359 * nothing. This is used to support nested bootmem calls.
361 static inline void __cvmx_bootmem_unlock(uint32_t flags)
363 if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
365 #ifndef CVMX_BUILD_FOR_LINUX_HOST
366 /* Unfortunately we can't use the normal cvmx-spinlock code as the
367 memory for the bootmem descriptor may be not accessable by a C
368 pointer. We use a 64bit XKPHYS address to access the memory
370 uint64_t lock_addr = (1ull << 63) | (cvmx_bootmem_desc_addr + offsetof(cvmx_bootmem_desc_t, lock));
373 __asm__ __volatile__("sw $0, 0(%[addr])\n"
374 :: [addr] "r" (lock_addr)
381 /* Some of the cvmx-bootmem functions dealing with C pointers are not supported
382 when we are compiling for CVMX_BUILD_FOR_LINUX_HOST. This ifndef removes
383 these functions when they aren't needed */
384 #ifndef CVMX_BUILD_FOR_LINUX_HOST
385 /* This functions takes an address range and adjusts it as necessary to
386 ** match the ABI that is currently being used. This is required to ensure
387 ** that bootmem_alloc* functions only return valid pointers for 32 bit ABIs */
388 static int __cvmx_validate_mem_range(uint64_t *min_addr_ptr, uint64_t *max_addr_ptr)
391 #if defined(__linux__) && defined(CVMX_ABI_N32)
393 extern uint64_t linux_mem32_min;
394 extern uint64_t linux_mem32_max;
395 /* For 32 bit Linux apps, we need to restrict the allocations to the range
396 ** of memory configured for access from userspace. Also, we need to add mappings
397 ** for the data structures that we access.*/
399 /* Narrow range requests to be bounded by the 32 bit limits. octeon_phy_mem_block_alloc()
400 ** will reject inconsistent req_size/range requests, so we don't repeat those checks here.
401 ** If max unspecified, set to 32 bit maximum. */
402 *min_addr_ptr = MIN(MAX(*min_addr_ptr, linux_mem32_min), linux_mem32_max);
404 *max_addr_ptr = linux_mem32_max;
406 *max_addr_ptr = MAX(MIN(*max_addr_ptr, linux_mem32_max), linux_mem32_min);
408 #elif defined(CVMX_ABI_N32)
410 uint32_t max_phys = 0x0FFFFFFF; /* Max physical address when 1-1 mappings not used */
411 #if CVMX_USE_1_TO_1_TLB_MAPPINGS
412 max_phys = 0x7FFFFFFF;
414 /* We are are running standalone simple executive, so we need to limit the range
415 ** that we allocate from */
417 /* Narrow range requests to be bounded by the 32 bit limits. octeon_phy_mem_block_alloc()
418 ** will reject inconsistent req_size/range requests, so we don't repeat those checks here.
419 ** If max unspecified, set to 32 bit maximum. */
420 *min_addr_ptr = MIN(MAX(*min_addr_ptr, 0x0), max_phys);
422 *max_addr_ptr = max_phys;
424 *max_addr_ptr = MAX(MIN(*max_addr_ptr, max_phys), 0x0);
432 void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment, uint64_t min_addr, uint64_t max_addr)
436 __cvmx_validate_mem_range(&min_addr, &max_addr);
437 address = cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);
440 return cvmx_phys_to_ptr(address);
444 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
445 EXPORT_SYMBOL(cvmx_bootmem_alloc_range);
448 void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address, uint64_t alignment)
450 return cvmx_bootmem_alloc_range(size, alignment, address, address + size);
454 void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment)
456 return cvmx_bootmem_alloc_range(size, alignment, 0, 0);
458 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
459 EXPORT_SYMBOL(cvmx_bootmem_alloc);
462 void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr, uint64_t max_addr, uint64_t align, const char *name)
466 __cvmx_validate_mem_range(&min_addr, &max_addr);
467 addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr, align, name, 0);
469 return cvmx_phys_to_ptr(addr);
474 void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address, const char *name)
476 return(cvmx_bootmem_alloc_named_range(size, address, address + size, 0, name));
478 void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, const char *name)
480 return(cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name));
483 int cvmx_bootmem_free_named(const char *name)
485 return(cvmx_bootmem_phy_named_block_free(name, 0));
489 const cvmx_bootmem_named_block_desc_t *cvmx_bootmem_find_named_block(const char *name)
491 /* FIXME: Returning a single static object is probably a bad thing */
492 static cvmx_bootmem_named_block_desc_t desc;
493 uint64_t named_addr = cvmx_bootmem_phy_named_block_find(name, 0);
496 desc.base_addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_addr, base_addr);
497 desc.size = CVMX_BOOTMEM_NAMED_GET_FIELD(named_addr, size);
498 strncpy(desc.name, name, sizeof(desc.name));
499 desc.name[sizeof(desc.name)-1] = 0;
506 void cvmx_bootmem_print_named(void)
508 cvmx_bootmem_phy_named_block_print();
511 int cvmx_bootmem_init(uint64_t mem_desc_addr)
513 /* Verify that the size of cvmx_spinlock_t meets our assumptions */
514 if (sizeof(cvmx_spinlock_t) != 4)
516 cvmx_dprintf("ERROR: Unexpected size of cvmx_spinlock_t\n");
519 if (!cvmx_bootmem_desc_addr)
520 cvmx_bootmem_desc_addr = mem_desc_addr;
525 uint64_t cvmx_bootmem_available_mem(uint64_t min_block_size)
527 return(cvmx_bootmem_phy_available_mem(min_block_size));
534 /*********************************************************************
535 ** The cvmx_bootmem_phy* functions below return 64 bit physical addresses,
536 ** and expose more features that the cvmx_bootmem_functions above. These are
537 ** required for full memory space access in 32 bit applications, as well as for
538 ** using some advance features.
539 ** Most applications should not need to use these.
544 int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min, uint64_t address_max, uint64_t alignment, uint32_t flags)
549 uint64_t prev_addr = 0; /* points to previous list entry, NULL current entry is head of list */
550 uint64_t new_ent_addr = 0;
551 uint64_t desired_min_addr;
552 uint64_t alignment_mask = ~(alignment - 1);
555 cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
556 (ULL)req_size, (ULL)address_min, (ULL)address_max, (ULL)alignment);
559 if (__cvmx_bootmem_check_version(0))
562 /* Do a variety of checks to validate the arguments. The allocator code will later assume
563 ** that these checks have been made. We validate that the requested constraints are not
564 ** self-contradictory before we look through the list of available memory
567 /* 0 is not a valid req_size for this allocator */
571 /* Round req_size up to mult of minimum alignment bytes */
572 req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) & ~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
574 /* Convert !0 address_min and 0 address_max to special case of range that specifies an exact
575 ** memory block to allocate. Do this before other checks and adjustments so that this tranformation will be validated */
576 if (address_min && !address_max)
577 address_max = address_min + req_size;
578 else if (!address_min && !address_max)
579 address_max = ~0ull; /* If no limits given, use max limits */
584 /* Enforce minimum alignment (this also keeps the minimum free block
585 ** req_size the same as the alignment req_size */
586 if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)
588 alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;
590 alignment_mask = ~(alignment - 1);
592 /* Adjust address minimum based on requested alignment (round up to meet alignment). Do this here so we can
593 ** reject impossible requests up front. (NOP for address_min == 0) */
595 address_min = (address_min + (alignment - 1)) & ~(alignment - 1);
598 /* Reject inconsistent args. We have adjusted these, so this may fail due to our internal changes
599 ** even if this check would pass for the values the user supplied. */
600 if (req_size > address_max - address_min)
603 /* Walk through the list entries - first fit found is returned */
605 __cvmx_bootmem_lock(flags);
606 head_addr = CVMX_BOOTMEM_DESC_GET_FIELD(head_addr);
607 ent_addr = head_addr;
610 uint64_t usable_base, usable_max;
611 uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
613 if (cvmx_bootmem_phy_get_next(ent_addr) && ent_addr > cvmx_bootmem_phy_get_next(ent_addr))
615 cvmx_dprintf("Internal bootmem_alloc() error: ent: 0x%llx, next: 0x%llx\n",
616 (ULL)ent_addr, (ULL)cvmx_bootmem_phy_get_next(ent_addr));
620 /* Determine if this is an entry that can satisify the request */
621 /* Check to make sure entry is large enough to satisfy request */
622 usable_base = ALIGN_ADDR_UP(MAX(address_min, ent_addr), alignment_mask);
623 usable_max = MIN(address_max, ent_addr + ent_size);
624 /* We should be able to allocate block at address usable_base */
626 desired_min_addr = usable_base;
628 /* Determine if request can be satisfied from the current entry */
629 if ((((ent_addr + ent_size) > usable_base && ent_addr < address_max))
630 && req_size <= usable_max - usable_base)
632 /* We have found an entry that has room to satisfy the request, so allocate it from this entry */
634 /* If end CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from the end of this block
635 ** rather than the beginning */
636 if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC)
638 desired_min_addr = usable_max - req_size;
639 /* Align desired address down to required alignment */
640 desired_min_addr &= alignment_mask;
643 /* Match at start of entry */
644 if (desired_min_addr == ent_addr)
646 if (req_size < ent_size)
648 /* big enough to create a new block from top portion of block */
649 new_ent_addr = ent_addr + req_size;
650 cvmx_bootmem_phy_set_next(new_ent_addr, cvmx_bootmem_phy_get_next(ent_addr));
651 cvmx_bootmem_phy_set_size(new_ent_addr, ent_size - req_size);
653 /* Adjust next pointer as following code uses this */
654 cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
657 /* adjust prev ptr or head to remove this entry from list */
660 cvmx_bootmem_phy_set_next(prev_addr, cvmx_bootmem_phy_get_next(ent_addr));
664 /* head of list being returned, so update head ptr */
665 CVMX_BOOTMEM_DESC_SET_FIELD(head_addr, cvmx_bootmem_phy_get_next(ent_addr));
667 __cvmx_bootmem_unlock(flags);
668 return(desired_min_addr);
672 /* block returned doesn't start at beginning of entry, so we know
673 ** that we will be splitting a block off the front of this one. Create a new block
674 ** from the beginning, add to list, and go to top of loop again.
676 ** create new block from high portion of block, so that top block
677 ** starts at desired addr
679 new_ent_addr = desired_min_addr;
680 cvmx_bootmem_phy_set_next(new_ent_addr, cvmx_bootmem_phy_get_next(ent_addr));
681 cvmx_bootmem_phy_set_size(new_ent_addr, cvmx_bootmem_phy_get_size(ent_addr) - (desired_min_addr - ent_addr));
682 cvmx_bootmem_phy_set_size(ent_addr, desired_min_addr - ent_addr);
683 cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
684 /* Loop again to handle actual alloc from new block */
687 prev_addr = ent_addr;
688 ent_addr = cvmx_bootmem_phy_get_next(ent_addr);
691 /* We didn't find anything, so return error */
692 __cvmx_bootmem_unlock(flags);
698 int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)
701 uint64_t prev_addr = 0; /* zero is invalid */
705 cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n", (ULL)phy_addr, (ULL)size);
707 if (__cvmx_bootmem_check_version(0))
710 /* 0 is not a valid size for this allocator */
715 __cvmx_bootmem_lock(flags);
716 cur_addr = CVMX_BOOTMEM_DESC_GET_FIELD(head_addr);
717 if (cur_addr == 0 || phy_addr < cur_addr)
719 /* add at front of list - special case with changing head ptr */
720 if (cur_addr && phy_addr + size > cur_addr)
721 goto bootmem_free_done; /* error, overlapping section */
722 else if (phy_addr + size == cur_addr)
724 /* Add to front of existing first block */
725 cvmx_bootmem_phy_set_next(phy_addr, cvmx_bootmem_phy_get_next(cur_addr));
726 cvmx_bootmem_phy_set_size(phy_addr, cvmx_bootmem_phy_get_size(cur_addr) + size);
727 CVMX_BOOTMEM_DESC_SET_FIELD(head_addr, phy_addr);
732 /* New block before first block */
733 cvmx_bootmem_phy_set_next(phy_addr, cur_addr); /* OK if cur_addr is 0 */
734 cvmx_bootmem_phy_set_size(phy_addr, size);
735 CVMX_BOOTMEM_DESC_SET_FIELD(head_addr, phy_addr);
738 goto bootmem_free_done;
741 /* Find place in list to add block */
742 while (cur_addr && phy_addr > cur_addr)
744 prev_addr = cur_addr;
745 cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
750 /* We have reached the end of the list, add on to end, checking
751 ** to see if we need to combine with last block
753 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) == phy_addr)
755 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(prev_addr) + size);
759 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
760 cvmx_bootmem_phy_set_size(phy_addr, size);
761 cvmx_bootmem_phy_set_next(phy_addr, 0);
764 goto bootmem_free_done;
768 /* insert between prev and cur nodes, checking for merge with either/both */
770 if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) == phy_addr)
772 /* Merge with previous */
773 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(prev_addr) + size);
774 if (phy_addr + size == cur_addr)
776 /* Also merge with current */
777 cvmx_bootmem_phy_set_size(prev_addr, cvmx_bootmem_phy_get_size(cur_addr) + cvmx_bootmem_phy_get_size(prev_addr));
778 cvmx_bootmem_phy_set_next(prev_addr, cvmx_bootmem_phy_get_next(cur_addr));
781 goto bootmem_free_done;
783 else if (phy_addr + size == cur_addr)
785 /* Merge with current */
786 cvmx_bootmem_phy_set_size(phy_addr, cvmx_bootmem_phy_get_size(cur_addr) + size);
787 cvmx_bootmem_phy_set_next(phy_addr, cvmx_bootmem_phy_get_next(cur_addr));
788 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
790 goto bootmem_free_done;
793 /* It is a standalone block, add in between prev and cur */
794 cvmx_bootmem_phy_set_size(phy_addr, size);
795 cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
796 cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
803 __cvmx_bootmem_unlock(flags);
810 void cvmx_bootmem_phy_list_print(void)
814 addr = CVMX_BOOTMEM_DESC_GET_FIELD(head_addr);
815 cvmx_dprintf("\n\n\nPrinting bootmem block list, descriptor: 0x%llx, head is 0x%llx\n",
816 (ULL)cvmx_bootmem_desc_addr, (ULL)addr);
817 cvmx_dprintf("Descriptor version: %d.%d\n",
818 (int)CVMX_BOOTMEM_DESC_GET_FIELD(major_version),
819 (int)CVMX_BOOTMEM_DESC_GET_FIELD(minor_version));
820 if (CVMX_BOOTMEM_DESC_GET_FIELD(major_version) > 3)
822 cvmx_dprintf("Warning: Bootmem descriptor version is newer than expected\n");
826 cvmx_dprintf("mem list is empty!\n");
830 cvmx_dprintf("Block address: 0x%08qx, size: 0x%08qx, next: 0x%08qx\n",
832 (ULL)cvmx_bootmem_phy_get_size(addr),
833 (ULL)cvmx_bootmem_phy_get_next(addr));
834 addr = cvmx_bootmem_phy_get_next(addr);
836 cvmx_dprintf("\n\n");
841 uint64_t cvmx_bootmem_phy_available_mem(uint64_t min_block_size)
845 uint64_t available_mem = 0;
847 __cvmx_bootmem_lock(0);
848 addr = CVMX_BOOTMEM_DESC_GET_FIELD(head_addr);
851 if (cvmx_bootmem_phy_get_size(addr) >= min_block_size)
852 available_mem += cvmx_bootmem_phy_get_size(addr);
853 addr = cvmx_bootmem_phy_get_next(addr);
855 __cvmx_bootmem_unlock(0);
856 return(available_mem);
862 uint64_t cvmx_bootmem_phy_named_block_find(const char *name, uint32_t flags)
867 cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);
869 __cvmx_bootmem_lock(flags);
870 if (!__cvmx_bootmem_check_version(3))
873 uint64_t named_block_array_addr = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_array_addr);
874 int num_blocks = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_num_blocks);
875 int name_length = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_name_len);
876 uint64_t named_addr = named_block_array_addr;
877 for (i = 0; i < num_blocks; i++)
879 uint64_t named_size = CVMX_BOOTMEM_NAMED_GET_FIELD(named_addr, size);
880 if (name && named_size)
882 char name_tmp[name_length];
883 CVMX_BOOTMEM_NAMED_GET_NAME(named_addr, name_tmp, name_length);
884 if (!strncmp(name, name_tmp, name_length - 1))
890 else if (!name && !named_size)
895 named_addr += sizeof(cvmx_bootmem_named_block_desc_t);
898 __cvmx_bootmem_unlock(flags);
902 int cvmx_bootmem_phy_named_block_free(const char *name, uint32_t flags)
904 uint64_t named_block_addr;
906 if (__cvmx_bootmem_check_version(3))
909 cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);
912 /* Take lock here, as name lookup/block free/name free need to be atomic */
913 __cvmx_bootmem_lock(flags);
915 named_block_addr = cvmx_bootmem_phy_named_block_find(name, CVMX_BOOTMEM_FLAG_NO_LOCKING);
916 if (named_block_addr)
918 uint64_t named_addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_addr, base_addr);
919 uint64_t named_size = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_addr, size);
921 cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s, base: 0x%llx, size: 0x%llx\n",
922 name, (ULL)named_addr, (ULL)named_size);
924 __cvmx_bootmem_phy_free(named_addr, named_size, CVMX_BOOTMEM_FLAG_NO_LOCKING);
925 /* Set size to zero to indicate block not used. */
926 CVMX_BOOTMEM_NAMED_SET_FIELD(named_block_addr, size, 0);
928 __cvmx_bootmem_unlock(flags);
929 return(!!named_block_addr); /* 0 on failure, 1 on success */
936 int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr, uint64_t max_addr, uint64_t alignment, const char *name, uint32_t flags)
938 int64_t addr_allocated;
939 uint64_t named_block_desc_addr;
942 cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: 0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
950 if (__cvmx_bootmem_check_version(3))
953 /* Take lock here, as name lookup/block alloc/name add need to be atomic */
955 __cvmx_bootmem_lock(flags);
957 named_block_desc_addr = cvmx_bootmem_phy_named_block_find(name, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
958 if (named_block_desc_addr)
960 __cvmx_bootmem_unlock(flags);
964 /* Get pointer to first available named block descriptor */
965 named_block_desc_addr = cvmx_bootmem_phy_named_block_find(NULL, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
966 if (!named_block_desc_addr)
968 __cvmx_bootmem_unlock(flags);
972 /* Round size up to mult of minimum alignment bytes
973 ** We need the actual size allocated to allow for blocks to be coallesced
974 ** when they are freed. The alloc routine does the same rounding up
975 ** on all allocations. */
976 size = (size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) & ~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
978 addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
979 if (addr_allocated >= 0)
981 CVMX_BOOTMEM_NAMED_SET_FIELD(named_block_desc_addr, base_addr, addr_allocated);
982 CVMX_BOOTMEM_NAMED_SET_FIELD(named_block_desc_addr, size, size);
983 CVMX_BOOTMEM_NAMED_SET_NAME(named_block_desc_addr, name, CVMX_BOOTMEM_DESC_GET_FIELD(named_block_name_len));
986 __cvmx_bootmem_unlock(flags);
987 return(addr_allocated);
993 void cvmx_bootmem_phy_named_block_print(void)
998 uint64_t named_block_array_addr = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_array_addr);
999 int num_blocks = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_num_blocks);
1000 int name_length = CVMX_BOOTMEM_DESC_GET_FIELD(named_block_name_len);
1001 uint64_t named_block_addr = named_block_array_addr;
1004 cvmx_dprintf("cvmx_bootmem_phy_named_block_print, desc addr: 0x%llx\n",
1005 (ULL)cvmx_bootmem_desc_addr);
1007 if (__cvmx_bootmem_check_version(3))
1009 cvmx_dprintf("List of currently allocated named bootmem blocks:\n");
1010 for (i = 0; i < num_blocks; i++)
1012 uint64_t named_size = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_addr, size);
1015 char name_tmp[name_length];
1016 uint64_t named_addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_addr, base_addr);
1017 CVMX_BOOTMEM_NAMED_GET_NAME(named_block_addr, name_tmp, name_length);
1019 cvmx_dprintf("Name: %s, address: 0x%08qx, size: 0x%08qx, index: %d\n",
1020 name_tmp, (ULL)named_addr, (ULL)named_size, i);
1022 named_block_addr += sizeof(cvmx_bootmem_named_block_desc_t);
1026 cvmx_dprintf("No named bootmem blocks exist.\n");
1032 /* Real physical addresses of memory regions */
1033 #define OCTEON_DDR0_BASE (0x0ULL)
1034 #define OCTEON_DDR0_SIZE (0x010000000ULL)
1035 #define OCTEON_DDR1_BASE (OCTEON_IS_MODEL(OCTEON_CN6XXX) ? 0x20000000ULL : 0x410000000ULL)
1036 #define OCTEON_DDR1_SIZE (0x010000000ULL)
1037 #define OCTEON_DDR2_BASE (OCTEON_IS_MODEL(OCTEON_CN6XXX) ? 0x30000000ULL : 0x20000000ULL)
1038 #define OCTEON_DDR2_SIZE (OCTEON_IS_MODEL(OCTEON_CN6XXX) ? 0x7d0000000ULL : 0x3e0000000ULL)
1039 #define OCTEON_MAX_PHY_MEM_SIZE (OCTEON_IS_MODEL(OCTEON_CN63XX) ? 32*1024*1024*1024ULL : 16*1024*1024*1024ULL)
1040 int64_t cvmx_bootmem_phy_mem_list_init(uint64_t mem_size, uint32_t low_reserved_bytes, cvmx_bootmem_desc_t *desc_buffer)
1042 uint64_t cur_block_addr;
1047 cvmx_dprintf("cvmx_bootmem_phy_mem_list_init (arg desc ptr: %p, cvmx_bootmem_desc: 0x%llx)\n",
1048 desc_buffer, (ULL)cvmx_bootmem_desc_addr);
1051 /* Descriptor buffer needs to be in 32 bit addressable space to be compatible with
1052 ** 32 bit applications */
1055 cvmx_dprintf("ERROR: no memory for cvmx_bootmem descriptor provided\n");
1059 if (mem_size > OCTEON_MAX_PHY_MEM_SIZE)
1061 mem_size = OCTEON_MAX_PHY_MEM_SIZE;
1062 cvmx_dprintf("ERROR: requested memory size too large, truncating to maximum size\n");
1065 if (cvmx_bootmem_desc_addr)
1068 /* Initialize cvmx pointer to descriptor */
1069 #ifndef CVMX_BUILD_FOR_LINUX_HOST
1070 cvmx_bootmem_init(cvmx_ptr_to_phys(desc_buffer));
1072 cvmx_bootmem_init((unsigned long)desc_buffer);
1075 /* Fill the bootmem descriptor */
1076 CVMX_BOOTMEM_DESC_SET_FIELD(lock, 0);
1077 CVMX_BOOTMEM_DESC_SET_FIELD(flags, 0);
1078 CVMX_BOOTMEM_DESC_SET_FIELD(head_addr, 0);
1079 CVMX_BOOTMEM_DESC_SET_FIELD(major_version, CVMX_BOOTMEM_DESC_MAJ_VER);
1080 CVMX_BOOTMEM_DESC_SET_FIELD(minor_version, CVMX_BOOTMEM_DESC_MIN_VER);
1081 CVMX_BOOTMEM_DESC_SET_FIELD(app_data_addr, 0);
1082 CVMX_BOOTMEM_DESC_SET_FIELD(app_data_size, 0);
1084 /* Set up global pointer to start of list, exclude low 64k for exception vectors, space for global descriptor */
1085 cur_block_addr = (OCTEON_DDR0_BASE + low_reserved_bytes);
1087 if (mem_size <= OCTEON_DDR0_SIZE)
1089 __cvmx_bootmem_phy_free(cur_block_addr, mem_size - low_reserved_bytes, 0);
1093 __cvmx_bootmem_phy_free(cur_block_addr, OCTEON_DDR0_SIZE - low_reserved_bytes, 0);
1095 mem_size -= OCTEON_DDR0_SIZE;
1097 /* Add DDR2 block next if present */
1098 if (mem_size > OCTEON_DDR1_SIZE)
1100 __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, OCTEON_DDR1_SIZE, 0);
1101 __cvmx_bootmem_phy_free(OCTEON_DDR2_BASE, mem_size - OCTEON_DDR1_SIZE, 0);
1105 __cvmx_bootmem_phy_free(OCTEON_DDR1_BASE, mem_size, 0);
1110 /* Initialize the named block structure */
1111 CVMX_BOOTMEM_DESC_SET_FIELD(named_block_name_len, CVMX_BOOTMEM_NAME_LEN);
1112 CVMX_BOOTMEM_DESC_SET_FIELD(named_block_num_blocks, CVMX_BOOTMEM_NUM_NAMED_BLOCKS);
1113 CVMX_BOOTMEM_DESC_SET_FIELD(named_block_array_addr, 0);
1115 /* Allocate this near the top of the low 256 MBytes of memory */
1116 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);
1118 CVMX_BOOTMEM_DESC_SET_FIELD(named_block_array_addr, addr);
1121 cvmx_dprintf("cvmx_bootmem_phy_mem_list_init: named_block_array_addr: 0x%llx)\n",
1126 cvmx_dprintf("FATAL ERROR: unable to allocate memory for bootmem descriptor!\n");
1129 for (i=0; i<CVMX_BOOTMEM_NUM_NAMED_BLOCKS; i++)
1131 CVMX_BOOTMEM_NAMED_SET_FIELD(addr, base_addr, 0);
1132 CVMX_BOOTMEM_NAMED_SET_FIELD(addr, size, 0);
1133 addr += sizeof(cvmx_bootmem_named_block_desc_t);
1140 void cvmx_bootmem_lock(void)
1142 __cvmx_bootmem_lock(0);
1145 void cvmx_bootmem_unlock(void)
1147 __cvmx_bootmem_unlock(0);
1150 #ifndef CVMX_BUILD_FOR_LINUX_HOST
1151 void *__cvmx_bootmem_internal_get_desc_ptr(void)
1153 return cvmx_phys_to_ptr(cvmx_bootmem_desc_addr);