cvmx-bootmem.h 14 KB

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