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: 49448 $<hr>
54 #ifndef __CVMX_BOOTMEM_H__
55 #define __CVMX_BOOTMEM_H__
61 #define CVMX_BOOTMEM_NAME_LEN 128 /* Must be multiple of 8, changing breaks ABI */
62 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64 /* Can change without breaking ABI */
63 #define CVMX_BOOTMEM_ALIGNMENT_SIZE (16ull) /* minimum alignment of bootmem alloced blocks */
65 /* Flags for cvmx_bootmem_phy_mem* functions */
66 #define CVMX_BOOTMEM_FLAG_END_ALLOC (1 << 0) /* Allocate from end of block instead of beginning */
67 #define CVMX_BOOTMEM_FLAG_NO_LOCKING (1 << 1) /* Don't do any locking. */
70 /* First bytes of each free physical block of memory contain this structure,
71 * which is used to maintain the free memory list. Since the bootloader is
72 * only 32 bits, there is a union providing 64 and 32 bit versions. The
73 * application init code converts addresses to 64 bit addresses before the
78 /* Note: these are referenced from assembly routines in the bootloader, so this structure
79 ** should not be changed without changing those routines as well. */
80 uint64_t next_block_addr;
83 } cvmx_bootmem_block_header_t;
86 /* Structure for named memory blocks
87 ** Number of descriptors
88 ** available can be changed without affecting compatiblity,
89 ** but name length changes require a bump in the bootmem
91 ** Note: This structure must be naturally 64 bit aligned, as a single
92 ** memory image will be used by both 32 and 64 bit programs.
94 struct cvmx_bootmem_named_block_desc
96 uint64_t base_addr; /**< Base address of named block */
97 uint64_t size; /**< Size actually allocated for named block (may differ from requested) */
98 char name[CVMX_BOOTMEM_NAME_LEN]; /**< name of named block */
101 typedef struct cvmx_bootmem_named_block_desc cvmx_bootmem_named_block_desc_t;
103 /* Current descriptor versions */
104 #define CVMX_BOOTMEM_DESC_MAJ_VER 3 /* CVMX bootmem descriptor major version */
105 #define CVMX_BOOTMEM_DESC_MIN_VER 0 /* CVMX bootmem descriptor minor version */
107 /* First three members of cvmx_bootmem_desc_t are left in original
108 ** positions for backwards compatibility.
112 uint32_t lock; /**< spinlock to control access to list */
113 uint32_t flags; /**< flags for indicating various conditions */
116 uint32_t major_version; /**< incremented changed when incompatible changes made */
117 uint32_t minor_version; /**< incremented changed when compatible changes made, reset to zero when major incremented */
118 uint64_t app_data_addr;
119 uint64_t app_data_size;
121 uint32_t named_block_num_blocks; /**< number of elements in named blocks array */
122 uint32_t named_block_name_len; /**< length of name array in bootmem blocks */
123 uint64_t named_block_array_addr; /**< address of named memory block descriptors */
125 } cvmx_bootmem_desc_t;
129 * Initialize the boot alloc memory structures. This is
130 * normally called inside of cvmx_user_app_init()
132 * @param mem_desc_addr Address of the free memory list
135 extern int cvmx_bootmem_init(uint64_t mem_desc_addr);
139 * Allocate a block of memory from the free list that was passed
140 * to the application by the bootloader.
141 * This is an allocate-only algorithm, so freeing memory is not possible.
143 * @param size Size in bytes of block to allocate
144 * @param alignment Alignment required - must be power of 2
146 * @return pointer to block of memory, NULL on error
148 extern void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment);
151 * Allocate a block of memory from the free list that was
152 * passed to the application by the bootloader at a specific
153 * address. This is an allocate-only algorithm, so
154 * freeing memory is not possible. Allocation will fail if
155 * memory cannot be allocated at the specified address.
157 * @param size Size in bytes of block to allocate
158 * @param address Physical address to allocate memory at. If this memory is not
159 * available, the allocation fails.
160 * @param alignment Alignment required - must be power of 2
161 * @return pointer to block of memory, NULL on error
163 extern void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address, uint64_t alignment);
168 * Allocate a block of memory from the free list that was
169 * passed to the application by the bootloader within a specified
170 * address range. This is an allocate-only algorithm, so
171 * freeing memory is not possible. Allocation will fail if
172 * memory cannot be allocated in the requested range.
174 * @param size Size in bytes of block to allocate
175 * @param min_addr defines the minimum address of the range
176 * @param max_addr defines the maximum address of the range
177 * @param alignment Alignment required - must be power of 2
178 * @return pointer to block of memory, NULL on error
180 extern void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment, uint64_t min_addr, uint64_t max_addr);
184 * Allocate a block of memory from the free list that was passed
185 * to the application by the bootloader, and assign it a name in the
186 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
187 * Named blocks can later be freed.
189 * @param size Size in bytes of block to allocate
190 * @param alignment Alignment required - must be power of 2
191 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
193 * @return pointer to block of memory, NULL on error
195 extern void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, const char *name);
200 * Allocate a block of memory from the free list that was passed
201 * to the application by the bootloader, and assign it a name in the
202 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
203 * Named blocks can later be freed.
205 * @param size Size in bytes of block to allocate
206 * @param address Physical address to allocate memory at. If this memory is not
207 * available, the allocation fails.
208 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
210 * @return pointer to block of memory, NULL on error
212 extern void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address, const char *name);
217 * Allocate a block of memory from a specific range of the free list that was passed
218 * to the application by the bootloader, and assign it a name in the
219 * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
220 * Named blocks can later be freed.
221 * If request cannot be satisfied within the address range specified, NULL is returned
223 * @param size Size in bytes of block to allocate
224 * @param min_addr minimum address of range
225 * @param max_addr maximum address of range
226 * @param align Alignment of memory to be allocated. (must be a power of 2)
227 * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
229 * @return pointer to block of memory, NULL on error
231 extern void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr, uint64_t max_addr, uint64_t align, const char *name);
234 * Frees a previously allocated named bootmem block.
236 * @param name name of block to free
238 * @return 0 on failure,
241 extern int cvmx_bootmem_free_named(const char *name);
245 * Finds a named bootmem block by name.
247 * @param name name of block to free
249 * @return pointer to named block descriptor on success
252 const cvmx_bootmem_named_block_desc_t *cvmx_bootmem_find_named_block(const char *name);
257 * Returns the size of available memory in bytes, only
258 * counting blocks that are at least as big as the minimum block
261 * @param min_block_size
262 * Minimum block size to count in total.
264 * @return Number of bytes available for allocation that meet the block size requirement
266 uint64_t cvmx_bootmem_available_mem(uint64_t min_block_size);
271 * Prints out the list of named blocks that have been allocated
272 * along with their addresses and sizes.
273 * This is primarily used for debugging purposes
275 void cvmx_bootmem_print_named(void);
279 * Allocates a block of physical memory from the free list, at (optional) requested address and alignment.
281 * @param req_size size of region to allocate. All requests are rounded up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
283 * Minimum address that block can occupy.
285 * Specifies the maximum address_min (inclusive) that the allocation can use.
286 * @param alignment Requested alignment of the block. If this alignment cannot be met, the allocation fails.
287 * This must be a power of 2.
288 * (Note: Alignment of CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and internally enforced. Requested alignments of
289 * less than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to CVMX_BOOTMEM_ALIGNMENT_SIZE.)
290 * @param flags Flags to control options for the allocation.
292 * @return physical address of block allocated, or -1 on failure
294 int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min, uint64_t address_max, uint64_t alignment, uint32_t flags);
299 * Allocates a named block of physical memory from the free list, at (optional) requested address and alignment.
301 * @param size size of region to allocate. All requests are rounded up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
303 * Minimum address that block can occupy.
305 * Specifies the maximum address_min (inclusive) that the allocation can use.
306 * @param alignment Requested alignment of the block. If this alignment cannot be met, the allocation fails.
307 * This must be a power of 2.
308 * (Note: Alignment of CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and internally enforced. Requested alignments of
309 * less than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to CVMX_BOOTMEM_ALIGNMENT_SIZE.)
310 * @param name name to assign to named block
311 * @param flags Flags to control options for the allocation.
313 * @return physical address of block allocated, or -1 on failure
315 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);
319 * Finds a named memory block by name.
320 * Also used for finding an unused entry in the named block table.
322 * @param name Name of memory block to find.
323 * If NULL pointer given, then finds unused descriptor, if available.
324 * @param flags Flags to control options for the allocation.
326 * @return Physical address of the memory block descriptor, zero if not
327 * found. If zero returned when name parameter is NULL, then no
328 * memory block descriptors are available.
330 uint64_t cvmx_bootmem_phy_named_block_find(const char *name, uint32_t flags);
334 * Returns the size of available memory in bytes, only
335 * counting blocks that are at least as big as the minimum block
338 * @param min_block_size
339 * Minimum block size to count in total.
341 * @return Number of bytes available for allocation that meet the block size requirement
343 uint64_t cvmx_bootmem_phy_available_mem(uint64_t min_block_size);
346 * Frees a named block.
348 * @param name name of block to free
349 * @param flags flags for passing options
351 * @return 0 on failure
354 int cvmx_bootmem_phy_named_block_free(const char *name, uint32_t flags);
357 * Frees a block to the bootmem allocator list. This must
358 * be used with care, as the size provided must match the size
359 * of the block that was allocated, or the list will become
362 * IMPORTANT: This is only intended to be used as part of named block
363 * frees and initial population of the free memory list.
366 * @param phy_addr physical address of block
367 * @param size size of block in bytes.
368 * @param flags flags for passing options
370 * @return 1 on success,
373 int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags);
377 * Prints the list of currently allocated named blocks
380 void cvmx_bootmem_phy_named_block_print(void);
384 * Prints the list of available memory.
387 void cvmx_bootmem_phy_list_print(void);
392 * This function initializes the free memory list used by cvmx_bootmem.
393 * This must be called before any allocations can be done.
395 * @param mem_size Total memory available, in bytes
396 * @param low_reserved_bytes
397 * Number of bytes to reserve (leave out of free list) at address 0x0.
399 * Buffer for the bootmem descriptor. This must be a 32 bit addressable
402 * @return 1 on success
405 int64_t cvmx_bootmem_phy_mem_list_init(uint64_t mem_size, uint32_t low_reserved_bytes, cvmx_bootmem_desc_t *desc_buffer);
408 * Locks the bootmem allocator. This is useful in certain situations
409 * where multiple allocations must be made without being interrupted.
410 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
413 void cvmx_bootmem_lock(void);
416 * Unlocks the bootmem allocator. This is useful in certain situations
417 * where multiple allocations must be made without being interrupted.
418 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
421 void cvmx_bootmem_unlock(void);
424 * Internal use function to get the current descriptor pointer */
425 void *__cvmx_bootmem_internal_get_desc_ptr(void);
431 #endif /* __CVMX_BOOTMEM_H__ */