1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 Marvell International Ltd.
8 * Simple allocate only memory allocator. Used to allocate memory at application
12 #ifndef __CVMX_BOOTMEM_H__
13 #define __CVMX_BOOTMEM_H__
15 /* Must be multiple of 8, changing breaks ABI */
16 #define CVMX_BOOTMEM_NAME_LEN 128
17 /* Can change without breaking ABI */
18 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64
19 /* minimum alignment of bootmem alloced blocks */
20 #define CVMX_BOOTMEM_ALIGNMENT_SIZE (16ull)
22 /* Flags for cvmx_bootmem_phy_mem* functions */
23 /* Allocate from end of block instead of beginning */
24 #define CVMX_BOOTMEM_FLAG_END_ALLOC (1 << 0)
25 #define CVMX_BOOTMEM_FLAG_NO_LOCKING (1 << 1) /* Don't do any locking. */
27 /* Real physical addresses of memory regions */
28 #define OCTEON_DDR0_BASE (0x0ULL)
29 #define OCTEON_DDR0_SIZE (0x010000000ULL)
30 #define OCTEON_DDR1_BASE ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
31 ? 0x20000000ULL : 0x410000000ULL)
32 #define OCTEON_DDR1_SIZE (0x010000000ULL)
33 #define OCTEON_DDR2_BASE ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
34 ? 0x30000000ULL : 0x20000000ULL)
35 #define OCTEON_DDR2_SIZE ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
36 ? 0x7d0000000ULL : 0x3e0000000ULL)
37 #define OCTEON_MAX_PHY_MEM_SIZE ((OCTEON_IS_MODEL(OCTEON_CN68XX)) \
38 ? 128 * 1024 * 1024 * 1024ULL \
39 : (OCTEON_IS_OCTEON2()) \
40 ? 32 * 1024 * 1024 * 1024ull \
41 : (OCTEON_IS_OCTEON3()) \
42 ? 512 * 1024 * 1024 * 1024ULL \
43 : 16 * 1024 * 1024 * 1024ULL)
46 * First bytes of each free physical block of memory contain this structure,
47 * which is used to maintain the free memory list. Since the bootloader is
48 * only 32 bits, there is a union providing 64 and 32 bit versions. The
49 * application init code converts addresses to 64 bit addresses before the
52 struct cvmx_bootmem_block_header {
53 /* Note: these are referenced from assembly routines in the bootloader,
54 * so this structure should not be changed without changing those
63 * Structure for named memory blocks
64 * Number of descriptors
65 * available can be changed without affecting compatibility,
66 * but name length changes require a bump in the bootmem
68 * Note: This structure must be naturally 64 bit aligned, as a single
69 * memory image will be used by both 32 and 64 bit programs.
71 struct cvmx_bootmem_named_block_desc {
72 u64 base_addr; /* Base address of named block */
74 * Size actually allocated for named block (may differ from requested)
77 char name[CVMX_BOOTMEM_NAME_LEN]; /* name of named block */
80 /* Current descriptor versions */
81 /* CVMX bootmem descriptor major version */
82 #define CVMX_BOOTMEM_DESC_MAJ_VER 3
83 /* CVMX bootmem descriptor minor version */
84 #define CVMX_BOOTMEM_DESC_MIN_VER 0
87 * First three members of cvmx_bootmem_desc_t are left in original
88 * positions for backwards compatibility.
90 struct cvmx_bootmem_desc {
91 /* Linux compatible proxy for __BIG_ENDIAN */
92 u32 lock; /* spinlock to control access to list */
93 u32 flags; /* flags for indicating various conditions */
96 /* incremented changed when incompatible changes made */
99 * incremented changed when compatible changes made, reset to
100 * zero when major incremented
106 /* number of elements in named blocks array */
107 u32 named_block_num_blocks;
108 /* length of name array in bootmem blocks */
109 u32 named_block_name_len;
110 /* address of named memory block descriptors */
111 u64 named_block_array_addr;
115 * Initialize the boot alloc memory structures. This is
116 * normally called inside of cvmx_user_app_init()
118 * @param mem_desc_addr Address of the free memory list
121 int cvmx_bootmem_init(u64 mem_desc_addr);
124 * Allocate a block of memory from the free list that was passed
125 * to the application by the bootloader.
126 * This is an allocate-only algorithm, so freeing memory is not possible.
128 * @param size Size in bytes of block to allocate
129 * @param alignment Alignment required - must be power of 2
131 * @return pointer to block of memory, NULL on error
133 void *cvmx_bootmem_alloc(u64 size, u64 alignment);
136 * Allocate a block of memory from the free list that was passed
137 * to the application by the bootloader from a specific node.
138 * This is an allocate-only algorithm, so freeing memory is not possible.
140 * @param node The node to allocate memory from
141 * @param size Size in bytes of block to allocate
142 * @param alignment Alignment required - must be power of 2
144 * @return pointer to block of memory, NULL on error
146 void *cvmx_bootmem_alloc_node(u64 node, u64 size, u64 alignment);
149 * Allocate a block of memory from the free list that was
150 * passed to the application by the bootloader at a specific
151 * address. This is an allocate-only algorithm, so
152 * freeing memory is not possible. Allocation will fail if
153 * memory cannot be allocated at the specified address.
155 * @param size Size in bytes of block to allocate
156 * @param address Physical address to allocate memory at. If this
157 * memory is not available, the allocation fails.
158 * @param alignment Alignment required - must be power of 2
159 * @return pointer to block of memory, NULL on error
161 void *cvmx_bootmem_alloc_address(u64 size, u64 address,
165 * Allocate a block of memory from the free list that was
166 * passed to the application by the bootloader within a specified
167 * address range. This is an allocate-only algorithm, so
168 * freeing memory is not possible. Allocation will fail if
169 * memory cannot be allocated in the requested range.
171 * @param size Size in bytes of block to allocate
172 * @param min_addr defines the minimum address of the range
173 * @param max_addr defines the maximum address of the range
174 * @param alignment Alignment required - must be power of 2
175 * @return pointer to block of memory, NULL on error
177 void *cvmx_bootmem_alloc_range(u64 size, u64 alignment,
178 u64 min_addr, u64 max_addr);
181 * Allocate a block of memory from the free list that was passed
182 * to the application by the bootloader, and assign it a name in the
183 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
184 * Named blocks can later be freed.
186 * @param size Size in bytes of block to allocate
187 * @param alignment Alignment required - must be power of 2
188 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
190 * @return pointer to block of memory, NULL on error
192 void *cvmx_bootmem_alloc_named(u64 size, u64 alignment,
196 * Allocate a block of memory from the free list that was passed
197 * to the application by the bootloader, and assign it a name in the
198 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
199 * Named blocks can later be freed.
201 * @param size Size in bytes of block to allocate
202 * @param alignment Alignment required - must be power of 2
203 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
204 * @param flags Flags to control options for the allocation.
206 * @return pointer to block of memory, NULL on error
208 void *cvmx_bootmem_alloc_named_flags(u64 size, u64 alignment,
209 const char *name, u32 flags);
212 * Allocate a block of memory from the free list that was passed
213 * to the application by the bootloader, and assign it a name in the
214 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
215 * Named blocks can later be freed.
217 * @param size Size in bytes of block to allocate
218 * @param address Physical address to allocate memory at. If this
219 * memory is not available, the allocation fails.
220 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
222 * @return pointer to block of memory, NULL on error
224 void *cvmx_bootmem_alloc_named_address(u64 size, u64 address,
228 * Allocate a block of memory from a specific range of the free list
229 * that was passed to the application by the bootloader, and assign it
230 * a name in the global named block table. (part of the
231 * cvmx_bootmem_descriptor_t structure) Named blocks can later be
232 * freed. If request cannot be satisfied within the address range
233 * specified, NULL is returned
235 * @param size Size in bytes of block to allocate
236 * @param min_addr minimum address of range
237 * @param max_addr maximum address of range
238 * @param align Alignment of memory to be allocated. (must be a power of 2)
239 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
241 * @return pointer to block of memory, NULL on error
243 void *cvmx_bootmem_alloc_named_range(u64 size, u64 min_addr,
244 u64 max_addr, u64 align,
248 * Allocate if needed a block of memory from a specific range of the
249 * free list that was passed to the application by the bootloader, and
250 * assign it a name in the global named block table. (part of the
251 * cvmx_bootmem_descriptor_t structure) Named blocks can later be
252 * freed. If the requested name block is already allocated, return
253 * the pointer to block of memory. If request cannot be satisfied
254 * within the address range specified, NULL is returned
256 * @param size Size in bytes of block to allocate
257 * @param min_addr minimum address of range
258 * @param max_addr maximum address of range
259 * @param align Alignment of memory to be allocated. (must be a power of 2)
260 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
261 * @param init Initialization function
263 * The initialization function is optional, if omitted the named block
264 * is initialized to all zeros when it is created, i.e. once.
266 * @return pointer to block of memory, NULL on error
268 void *cvmx_bootmem_alloc_named_range_once(u64 size,
273 void (*init)(void *));
276 * Allocate all free memory starting at the start address. This is used to
277 * prevent any free blocks from later being allocated within the reserved space.
278 * Note that any memory allocated with this function cannot be later freed.
280 * @param start_addr Starting address to reserve
281 * @param size Size in bytes to reserve starting at start_addr
282 * @param name Name to assign to reserved blocks
283 * @param flags Flags to use when reserving memory
285 * @return 0 on failure,
288 int cvmx_bootmem_reserve_memory(u64 start_addr, u64 size,
289 const char *name, u32 flags);
292 * Frees a previously allocated named bootmem block.
294 * @param name name of block to free
296 * @return 0 on failure,
299 int cvmx_bootmem_free_named(const char *name);
302 * Finds a named bootmem block by name.
304 * @param name name of block to free
306 * @return pointer to named block descriptor on success
309 const struct cvmx_bootmem_named_block_desc *
310 cvmx_bootmem_find_named_block(const char *name);
313 * Returns the size of available memory in bytes, only
314 * counting blocks that are at least as big as the minimum block
317 * @param min_block_size
318 * Minimum block size to count in total.
320 * @return Number of bytes available for allocation that meet the
321 * block size requirement
323 u64 cvmx_bootmem_available_mem(u64 min_block_size);
326 * Prints out the list of named blocks that have been allocated
327 * along with their addresses and sizes.
328 * This is primarily used for debugging purposes
330 void cvmx_bootmem_print_named(void);
333 * Allocates a block of physical memory from the free list, at
334 * (optional) requested address and alignment.
336 * @param req_size size of region to allocate. All requests are
337 * rounded up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
339 * @param address_min Minimum address that block can occupy.
341 * @param address_max Specifies the maximum address_min (inclusive)
342 * that the allocation can use.
344 * @param alignment Requested alignment of the block. If this
345 * alignment cannot be met, the allocation fails.
346 * This must be a power of 2. (Note: Alignment of
347 * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
348 * internally enforced. Requested alignments of less
349 * than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
350 * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
351 * @param flags Flags to control options for the allocation.
353 * @return physical address of block allocated, or -1 on failure
355 s64 cvmx_bootmem_phy_alloc(u64 req_size, u64 address_min, u64 address_max,
356 u64 alignment, u32 flags);
359 * Allocates a named block of physical memory from the free list, at
360 * (optional) requested address and alignment.
362 * @param size size of region to allocate. All requests are rounded
363 * up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
365 * @param min_addr Minimum address that block can occupy.
367 * @param max_addr Specifies the maximum address_min (inclusive) that
368 * the allocation can use.
370 * @param alignment Requested alignment of the block. If this
371 * alignment cannot be met, the allocation fails.
372 * This must be a power of 2. (Note: Alignment of
373 * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
374 * internally enforced. Requested alignments of less
375 * than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
376 * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
378 * @param name name to assign to named block
380 * @param flags Flags to control options for the allocation.
382 * @return physical address of block allocated, or -1 on failure
384 s64 cvmx_bootmem_phy_named_block_alloc(u64 size, u64 min_addr, u64 max_addr,
385 u64 alignment, const char *name,
389 * Finds a named memory block by name.
390 * Also used for finding an unused entry in the named block table.
392 * @param name Name of memory block to find. If NULL pointer given,
393 * then finds unused descriptor, if available.
395 * @param flags Flags to control options for the allocation.
397 * @return Physical address of the memory block descriptor, zero if not
398 * found. If zero returned when name parameter is NULL, then no
399 * memory block descriptors are available.
401 u64 cvmx_bootmem_phy_named_block_find(const char *name, u32 flags);
404 * Returns the size of available memory in bytes, only
405 * counting blocks that are at least as big as the minimum block
408 * @param min_block_size
409 * Minimum block size to count in total.
411 * @return Number of bytes available for allocation that meet the
412 * block size requirement
414 u64 cvmx_bootmem_phy_available_mem(u64 min_block_size);
417 * Frees a named block.
419 * @param name name of block to free
420 * @param flags flags for passing options
422 * @return 0 on failure
425 int cvmx_bootmem_phy_named_block_free(const char *name, u32 flags);
428 * Frees a block to the bootmem allocator list. This must
429 * be used with care, as the size provided must match the size
430 * of the block that was allocated, or the list will become
433 * IMPORTANT: This is only intended to be used as part of named block
434 * frees and initial population of the free memory list.
437 * @param phy_addr physical address of block
438 * @param size size of block in bytes.
439 * @param flags flags for passing options
441 * @return 1 on success,
444 int __cvmx_bootmem_phy_free(u64 phy_addr, u64 size, u32 flags);
447 * Prints the list of currently allocated named blocks
450 void cvmx_bootmem_phy_named_block_print(void);
453 * Prints the list of available memory.
456 void cvmx_bootmem_phy_list_print(void);
459 * This function initializes the free memory list used by cvmx_bootmem.
460 * This must be called before any allocations can be done.
462 * @param mem_size Total memory available, in bytes
464 * @param low_reserved_bytes Number of bytes to reserve (leave out of
465 * free list) at address 0x0.
467 * @param desc_buffer Buffer for the bootmem descriptor. This must be
468 * a 32 bit addressable address.
470 * @return 1 on success
473 s64 cvmx_bootmem_phy_mem_list_init(u64 mem_size, u32 low_reserved_bytes,
474 struct cvmx_bootmem_desc *desc_buffer);
477 * This function initializes the free memory list used by cvmx_bootmem.
478 * This must be called before any allocations can be done.
480 * @param nodemask Nodemask - one bit per node (bit0->node0, bit1->node1,...)
482 * @param mem_size[] Array of memory sizes in MBytes per node ([0]->node0,...)
484 * @param low_reserved_bytes Number of bytes to reserve (leave out of
485 * free list) at address 0x0.
487 * @param desc_buffer Buffer for the bootmem descriptor. This must be
488 * a 32 bit addressable address.
490 * @return 1 on success
493 s64 cvmx_bootmem_phy_mem_list_init_multi(u8 nodemask, u32 mem_size[],
494 u32 low_reserved_bytes,
495 struct cvmx_bootmem_desc *desc_buffer);
497 * Locks the bootmem allocator. This is useful in certain situations
498 * where multiple allocations must be made without being interrupted.
499 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
502 void cvmx_bootmem_lock(void);
505 * Unlocks the bootmem allocator. This is useful in certain situations
506 * where multiple allocations must be made without being interrupted.
507 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
510 void cvmx_bootmem_unlock(void);
513 * Internal use function to get the current descriptor pointer
515 void *__cvmx_bootmem_internal_get_desc_ptr(void);
518 * Internal use. This is userd to get a pointer to a physical
519 * address. For linux n32 the physical address in mmaped to a virtual
520 * address and the virtual address is returned. For n64 the address
521 * is converted to an xkphys address and the xkhpys address is
524 void *__cvmx_phys_addr_to_ptr(u64 phys, int size);
525 const struct cvmx_bootmem_named_block_desc *
526 __cvmx_bootmem_find_named_block_flags(const char *name, u32 flags);
527 void *cvmx_bootmem_alloc_named_range_flags(u64 size, u64 min_addr,
528 u64 max_addr, u64 align,
529 const char *name, u32 flags);
530 u64 cvmx_bootmem_phy_alloc_range(u64 size, u64 alignment,
531 u64 min_addr, u64 max_addr);
533 #endif /* __CVMX_BOOTMEM_H__ */