1 /***********************license start*************** 2 * Author: Cavium Networks 3 * 4 * Contact: support@caviumnetworks.com 5 * This file is part of the OCTEON SDK 6 * 7 * Copyright (c) 2003-2008 Cavium Networks 8 * 9 * This file is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License, Version 2, as 11 * published by the Free Software Foundation. 12 * 13 * This file is distributed in the hope that it will be useful, but 14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty 15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or 16 * NONINFRINGEMENT. See the GNU General Public License for more 17 * details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this file; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 * or visit http://www.gnu.org/licenses/. 23 * 24 * This file may also be available under a different license from Cavium. 25 * Contact Cavium Networks for more information 26 ***********************license end**************************************/ 27 28 /* 29 * Simple allocate only memory allocator. Used to allocate memory at 30 * application start time. 31 */ 32 33 #ifndef __CVMX_BOOTMEM_H__ 34 #define __CVMX_BOOTMEM_H__ 35 /* Must be multiple of 8, changing breaks ABI */ 36 #define CVMX_BOOTMEM_NAME_LEN 128 37 38 /* Can change without breaking ABI */ 39 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64 40 41 /* minimum alignment of bootmem alloced blocks */ 42 #define CVMX_BOOTMEM_ALIGNMENT_SIZE (16ull) 43 44 /* Flags for cvmx_bootmem_phy_mem* functions */ 45 /* Allocate from end of block instead of beginning */ 46 #define CVMX_BOOTMEM_FLAG_END_ALLOC (1 << 0) 47 48 /* Don't do any locking. */ 49 #define CVMX_BOOTMEM_FLAG_NO_LOCKING (1 << 1) 50 51 /* First bytes of each free physical block of memory contain this structure, 52 * which is used to maintain the free memory list. Since the bootloader is 53 * only 32 bits, there is a union providing 64 and 32 bit versions. The 54 * application init code converts addresses to 64 bit addresses before the 55 * application starts. 56 */ 57 struct cvmx_bootmem_block_header { 58 /* 59 * Note: these are referenced from assembly routines in the 60 * bootloader, so this structure should not be changed 61 * without changing those routines as well. 62 */ 63 uint64_t next_block_addr; 64 uint64_t size; 65 66 }; 67 68 /* 69 * Structure for named memory blocks. Number of descriptors available 70 * can be changed without affecting compatibility, but name length 71 * changes require a bump in the bootmem descriptor version Note: This 72 * structure must be naturally 64 bit aligned, as a single memory 73 * image will be used by both 32 and 64 bit programs. 74 */ 75 struct cvmx_bootmem_named_block_desc { 76 /* Base address of named block */ 77 uint64_t base_addr; 78 /* 79 * Size actually allocated for named block (may differ from 80 * requested). 81 */ 82 uint64_t size; 83 /* name of named block */ 84 char name[CVMX_BOOTMEM_NAME_LEN]; 85 }; 86 87 /* Current descriptor versions */ 88 /* CVMX bootmem descriptor major version */ 89 #define CVMX_BOOTMEM_DESC_MAJ_VER 3 90 91 /* CVMX bootmem descriptor minor version */ 92 #define CVMX_BOOTMEM_DESC_MIN_VER 0 93 94 /* First three members of cvmx_bootmem_desc_t are left in original 95 * positions for backwards compatibility. 96 */ 97 struct cvmx_bootmem_desc { 98 /* spinlock to control access to list */ 99 uint32_t lock; 100 /* flags for indicating various conditions */ 101 uint32_t flags; 102 uint64_t head_addr; 103 104 /* Incremented when incompatible changes made */ 105 uint32_t major_version; 106 107 /* 108 * Incremented changed when compatible changes made, reset to 109 * zero when major incremented. 110 */ 111 uint32_t minor_version; 112 113 uint64_t app_data_addr; 114 uint64_t app_data_size; 115 116 /* number of elements in named blocks array */ 117 uint32_t named_block_num_blocks; 118 119 /* length of name array in bootmem blocks */ 120 uint32_t named_block_name_len; 121 /* address of named memory block descriptors */ 122 uint64_t named_block_array_addr; 123 124 }; 125 126 /** 127 * Initialize the boot alloc memory structures. This is 128 * normally called inside of cvmx_user_app_init() 129 * 130 * @mem_desc_ptr: Address of the free memory list 131 */ 132 extern int cvmx_bootmem_init(void *mem_desc_ptr); 133 134 /** 135 * Allocate a block of memory from the free list that was passed 136 * to the application by the bootloader. 137 * This is an allocate-only algorithm, so freeing memory is not possible. 138 * 139 * @size: Size in bytes of block to allocate 140 * @alignment: Alignment required - must be power of 2 141 * 142 * Returns pointer to block of memory, NULL on error 143 */ 144 extern void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment); 145 146 /** 147 * Allocate a block of memory from the free list that was 148 * passed to the application by the bootloader at a specific 149 * address. This is an allocate-only algorithm, so 150 * freeing memory is not possible. Allocation will fail if 151 * memory cannot be allocated at the specified address. 152 * 153 * @size: Size in bytes of block to allocate 154 * @address: Physical address to allocate memory at. If this memory is not 155 * available, the allocation fails. 156 * @alignment: Alignment required - must be power of 2 157 * Returns pointer to block of memory, NULL on error 158 */ 159 extern void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address, 160 uint64_t alignment); 161 162 /** 163 * Allocate a block of memory from the free list that was 164 * passed to the application by the bootloader within a specified 165 * address range. This is an allocate-only algorithm, so 166 * freeing memory is not possible. Allocation will fail if 167 * memory cannot be allocated in the requested range. 168 * 169 * @size: Size in bytes of block to allocate 170 * @min_addr: defines the minimum address of the range 171 * @max_addr: defines the maximum address of the range 172 * @alignment: Alignment required - must be power of 2 173 * Returns pointer to block of memory, NULL on error 174 */ 175 extern void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment, 176 uint64_t min_addr, uint64_t max_addr); 177 178 /** 179 * Frees a previously allocated named bootmem block. 180 * 181 * @name: name of block to free 182 * 183 * Returns 0 on failure, 184 * !0 on success 185 */ 186 187 188 /** 189 * Allocate a block of memory from the free list that was passed 190 * to the application by the bootloader, and assign it a name in the 191 * global named block table. (part of the cvmx_bootmem_descriptor_t structure) 192 * Named blocks can later be freed. 193 * 194 * @size: Size in bytes of block to allocate 195 * @alignment: Alignment required - must be power of 2 196 * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes 197 * 198 * Returns a pointer to block of memory, NULL on error 199 */ 200 extern void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, 201 char *name); 202 203 204 205 /** 206 * Allocate a block of memory from the free list that was passed 207 * to the application by the bootloader, and assign it a name in the 208 * global named block table. (part of the cvmx_bootmem_descriptor_t structure) 209 * Named blocks can later be freed. 210 * 211 * @size: Size in bytes of block to allocate 212 * @address: Physical address to allocate memory at. If this 213 * memory is not available, the allocation fails. 214 * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN 215 * bytes 216 * 217 * Returns a pointer to block of memory, NULL on error 218 */ 219 extern void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address, 220 char *name); 221 222 223 224 /** 225 * Allocate a block of memory from a specific range of the free list 226 * that was passed to the application by the bootloader, and assign it 227 * a name in the global named block table. (part of the 228 * cvmx_bootmem_descriptor_t structure) Named blocks can later be 229 * freed. If request cannot be satisfied within the address range 230 * specified, NULL is returned 231 * 232 * @size: Size in bytes of block to allocate 233 * @min_addr: minimum address of range 234 * @max_addr: maximum address of range 235 * @align: Alignment of memory to be allocated. (must be a power of 2) 236 * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes 237 * 238 * Returns a pointer to block of memory, NULL on error 239 */ 240 extern void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr, 241 uint64_t max_addr, uint64_t align, 242 char *name); 243 244 extern int cvmx_bootmem_free_named(char *name); 245 246 /** 247 * Finds a named bootmem block by name. 248 * 249 * @name: name of block to free 250 * 251 * Returns pointer to named block descriptor on success 252 * 0 on failure 253 */ 254 struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name); 255 256 /** 257 * Allocates a block of physical memory from the free list, at 258 * (optional) requested address and alignment. 259 * 260 * @req_size: size of region to allocate. All requests are rounded up 261 * to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size 262 * 263 * @address_min: Minimum address that block can occupy. 264 * 265 * @address_max: Specifies the maximum address_min (inclusive) that 266 * the allocation can use. 267 * 268 * @alignment: Requested alignment of the block. If this alignment 269 * cannot be met, the allocation fails. This must be a 270 * power of 2. (Note: Alignment of 271 * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and 272 * internally enforced. Requested alignments of less than 273 * CVMX_BOOTMEM_ALIGNMENT_SIZE are set to 274 * CVMX_BOOTMEM_ALIGNMENT_SIZE.) 275 * 276 * @flags: Flags to control options for the allocation. 277 * 278 * Returns physical address of block allocated, or -1 on failure 279 */ 280 int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min, 281 uint64_t address_max, uint64_t alignment, 282 uint32_t flags); 283 284 /** 285 * Allocates a named block of physical memory from the free list, at 286 * (optional) requested address and alignment. 287 * 288 * @param size size of region to allocate. All requests are rounded 289 * up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE 290 * bytes size 291 * @param min_addr Minimum address that block can occupy. 292 * @param max_addr Specifies the maximum address_min (inclusive) that 293 * the allocation can use. 294 * @param alignment Requested alignment of the block. If this 295 * alignment cannot be met, the allocation fails. 296 * This must be a power of 2. (Note: Alignment of 297 * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and 298 * internally enforced. Requested alignments of less 299 * than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to 300 * CVMX_BOOTMEM_ALIGNMENT_SIZE.) 301 * @param name name to assign to named block 302 * @param flags Flags to control options for the allocation. 303 * 304 * @return physical address of block allocated, or -1 on failure 305 */ 306 int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr, 307 uint64_t max_addr, 308 uint64_t alignment, 309 char *name, uint32_t flags); 310 311 /** 312 * Finds a named memory block by name. 313 * Also used for finding an unused entry in the named block table. 314 * 315 * @name: Name of memory block to find. If NULL pointer given, then 316 * finds unused descriptor, if available. 317 * 318 * @flags: Flags to control options for the allocation. 319 * 320 * Returns Pointer to memory block descriptor, NULL if not found. 321 * If NULL returned when name parameter is NULL, then no memory 322 * block descriptors are available. 323 */ 324 struct cvmx_bootmem_named_block_desc * 325 cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags); 326 327 /** 328 * Frees a named block. 329 * 330 * @name: name of block to free 331 * @flags: flags for passing options 332 * 333 * Returns 0 on failure 334 * 1 on success 335 */ 336 int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags); 337 338 /** 339 * Frees a block to the bootmem allocator list. This must 340 * be used with care, as the size provided must match the size 341 * of the block that was allocated, or the list will become 342 * corrupted. 343 * 344 * IMPORTANT: This is only intended to be used as part of named block 345 * frees and initial population of the free memory list. 346 * * 347 * 348 * @phy_addr: physical address of block 349 * @size: size of block in bytes. 350 * @flags: flags for passing options 351 * 352 * Returns 1 on success, 353 * 0 on failure 354 */ 355 int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags); 356 357 /** 358 * Locks the bootmem allocator. This is useful in certain situations 359 * where multiple allocations must be made without being interrupted. 360 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag. 361 * 362 */ 363 void cvmx_bootmem_lock(void); 364 365 /** 366 * Unlocks the bootmem allocator. This is useful in certain situations 367 * where multiple allocations must be made without being interrupted. 368 * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag. 369 * 370 */ 371 void cvmx_bootmem_unlock(void); 372 373 extern struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void); 374 375 #endif /* __CVMX_BOOTMEM_H__ */ 376