cvmx-bootmem.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: [email protected]
  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
  129. * passed to the application by the bootloader at a specific
  130. * address. This is an allocate-only algorithm, so
  131. * freeing memory is not possible. Allocation will fail if
  132. * memory cannot be allocated at the specified address.
  133. *
  134. * @size: Size in bytes of block to allocate
  135. * @address: Physical address to allocate memory at. If this memory is not
  136. * available, the allocation fails.
  137. * @alignment: Alignment required - must be power of 2
  138. * Returns pointer to block of memory, NULL on error
  139. */
  140. extern void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
  141. uint64_t alignment);
  142. /**
  143. * Frees a previously allocated named bootmem block.
  144. *
  145. * @name: name of block to free
  146. *
  147. * Returns 0 on failure,
  148. * !0 on success
  149. */
  150. /**
  151. * Allocate a block of memory from the free list that was passed
  152. * to the application by the bootloader, and assign it a name in the
  153. * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
  154. * Named blocks can later be freed.
  155. *
  156. * @size: Size in bytes of block to allocate
  157. * @alignment: Alignment required - must be power of 2
  158. * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
  159. *
  160. * Returns a pointer to block of memory, NULL on error
  161. */
  162. extern void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment,
  163. char *name);
  164. /**
  165. * Allocate a block of memory from a specific range of the free list
  166. * that was passed to the application by the bootloader, and assign it
  167. * a name in the global named block table. (part of the
  168. * cvmx_bootmem_descriptor_t structure) Named blocks can later be
  169. * freed. If request cannot be satisfied within the address range
  170. * specified, NULL is returned
  171. *
  172. * @size: Size in bytes of block to allocate
  173. * @min_addr: minimum address of range
  174. * @max_addr: maximum address of range
  175. * @align: Alignment of memory to be allocated. (must be a power of 2)
  176. * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
  177. *
  178. * Returns a pointer to block of memory, NULL on error
  179. */
  180. extern void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
  181. uint64_t max_addr, uint64_t align,
  182. char *name);
  183. /**
  184. * Allocate if needed a block of memory from a specific range of the
  185. * free list that was passed to the application by the bootloader, and
  186. * assign it a name in the global named block table. (part of the
  187. * cvmx_bootmem_descriptor_t structure) Named blocks can later be
  188. * freed. If the requested name block is already allocated, return
  189. * the pointer to block of memory. If request cannot be satisfied
  190. * within the address range specified, NULL is returned
  191. *
  192. * @param size Size in bytes of block to allocate
  193. * @param min_addr minimum address of range
  194. * @param max_addr maximum address of range
  195. * @param align Alignment of memory to be allocated. (must be a power of 2)
  196. * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
  197. * @param init Initialization function
  198. *
  199. * The initialization function is optional, if omitted the named block
  200. * is initialized to all zeros when it is created, i.e. once.
  201. *
  202. * @return pointer to block of memory, NULL on error
  203. */
  204. void *cvmx_bootmem_alloc_named_range_once(uint64_t size,
  205. uint64_t min_addr,
  206. uint64_t max_addr,
  207. uint64_t align,
  208. char *name,
  209. void (*init) (void *));
  210. extern int cvmx_bootmem_free_named(char *name);
  211. /**
  212. * Finds a named bootmem block by name.
  213. *
  214. * @name: name of block to free
  215. *
  216. * Returns pointer to named block descriptor on success
  217. * 0 on failure
  218. */
  219. struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name);
  220. /**
  221. * Allocates a block of physical memory from the free list, at
  222. * (optional) requested address and alignment.
  223. *
  224. * @req_size: size of region to allocate. All requests are rounded up
  225. * to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
  226. *
  227. * @address_min: Minimum address that block can occupy.
  228. *
  229. * @address_max: Specifies the maximum address_min (inclusive) that
  230. * the allocation can use.
  231. *
  232. * @alignment: Requested alignment of the block. If this alignment
  233. * cannot be met, the allocation fails. This must be a
  234. * power of 2. (Note: Alignment of
  235. * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
  236. * internally enforced. Requested alignments of less than
  237. * CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
  238. * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
  239. *
  240. * @flags: Flags to control options for the allocation.
  241. *
  242. * Returns physical address of block allocated, or -1 on failure
  243. */
  244. int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
  245. uint64_t address_max, uint64_t alignment,
  246. uint32_t flags);
  247. /**
  248. * Allocates a named block of physical memory from the free list, at
  249. * (optional) requested address and alignment.
  250. *
  251. * @param size size of region to allocate. All requests are rounded
  252. * up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE
  253. * bytes size
  254. * @param min_addr Minimum address that block can occupy.
  255. * @param max_addr Specifies the maximum address_min (inclusive) that
  256. * the allocation can use.
  257. * @param alignment Requested alignment of the block. If this
  258. * alignment cannot be met, the allocation fails.
  259. * This must be a power of 2. (Note: Alignment of
  260. * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
  261. * internally enforced. Requested alignments of less
  262. * than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
  263. * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
  264. * @param name name to assign to named block
  265. * @param flags Flags to control options for the allocation.
  266. *
  267. * @return physical address of block allocated, or -1 on failure
  268. */
  269. int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
  270. uint64_t max_addr,
  271. uint64_t alignment,
  272. char *name, uint32_t flags);
  273. /**
  274. * Frees a block to the bootmem allocator list. This must
  275. * be used with care, as the size provided must match the size
  276. * of the block that was allocated, or the list will become
  277. * corrupted.
  278. *
  279. * IMPORTANT: This is only intended to be used as part of named block
  280. * frees and initial population of the free memory list.
  281. * *
  282. *
  283. * @phy_addr: physical address of block
  284. * @size: size of block in bytes.
  285. * @flags: flags for passing options
  286. *
  287. * Returns 1 on success,
  288. * 0 on failure
  289. */
  290. int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags);
  291. /**
  292. * Locks the bootmem allocator. This is useful in certain situations
  293. * where multiple allocations must be made without being interrupted.
  294. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  295. *
  296. */
  297. void cvmx_bootmem_lock(void);
  298. /**
  299. * Unlocks the bootmem allocator. This is useful in certain situations
  300. * where multiple allocations must be made without being interrupted.
  301. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  302. *
  303. */
  304. void cvmx_bootmem_unlock(void);
  305. extern struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void);
  306. #endif /* __CVMX_BOOTMEM_H__ */