cvmx-bootmem.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 compatiblity, 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. /* spinlock to control access to list */
  87. uint32_t lock;
  88. /* flags for indicating various conditions */
  89. uint32_t flags;
  90. uint64_t head_addr;
  91. /* Incremented when incompatible changes made */
  92. uint32_t major_version;
  93. /*
  94. * Incremented changed when compatible changes made, reset to
  95. * zero when major incremented.
  96. */
  97. uint32_t minor_version;
  98. uint64_t app_data_addr;
  99. uint64_t app_data_size;
  100. /* number of elements in named blocks array */
  101. uint32_t named_block_num_blocks;
  102. /* length of name array in bootmem blocks */
  103. uint32_t named_block_name_len;
  104. /* address of named memory block descriptors */
  105. uint64_t named_block_array_addr;
  106. };
  107. /**
  108. * Initialize the boot alloc memory structures. This is
  109. * normally called inside of cvmx_user_app_init()
  110. *
  111. * @mem_desc_ptr: Address of the free memory list
  112. */
  113. extern int cvmx_bootmem_init(void *mem_desc_ptr);
  114. /**
  115. * Allocate a block of memory from the free list that was passed
  116. * to the application by the bootloader.
  117. * This is an allocate-only algorithm, so freeing memory is not possible.
  118. *
  119. * @size: Size in bytes of block to allocate
  120. * @alignment: Alignment required - must be power of 2
  121. *
  122. * Returns pointer to block of memory, NULL on error
  123. */
  124. extern void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment);
  125. /**
  126. * Allocate a block of memory from the free list that was
  127. * passed to the application by the bootloader at a specific
  128. * address. This is an allocate-only algorithm, so
  129. * freeing memory is not possible. Allocation will fail if
  130. * memory cannot be allocated at the specified address.
  131. *
  132. * @size: Size in bytes of block to allocate
  133. * @address: Physical address to allocate memory at. If this memory is not
  134. * available, the allocation fails.
  135. * @alignment: Alignment required - must be power of 2
  136. * Returns pointer to block of memory, NULL on error
  137. */
  138. extern void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
  139. uint64_t alignment);
  140. /**
  141. * Allocate a block of memory from the free list that was
  142. * passed to the application by the bootloader within a specified
  143. * address range. This is an allocate-only algorithm, so
  144. * freeing memory is not possible. Allocation will fail if
  145. * memory cannot be allocated in the requested range.
  146. *
  147. * @size: Size in bytes of block to allocate
  148. * @min_addr: defines the minimum address of the range
  149. * @max_addr: defines the maximum address of the range
  150. * @alignment: Alignment required - must be power of 2
  151. * Returns pointer to block of memory, NULL on error
  152. */
  153. extern void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,
  154. uint64_t min_addr, uint64_t max_addr);
  155. /**
  156. * Frees a previously allocated named bootmem block.
  157. *
  158. * @name: name of block to free
  159. *
  160. * Returns 0 on failure,
  161. * !0 on success
  162. */
  163. extern int cvmx_bootmem_free_named(char *name);
  164. /**
  165. * Finds a named bootmem block by name.
  166. *
  167. * @name: name of block to free
  168. *
  169. * Returns pointer to named block descriptor on success
  170. * 0 on failure
  171. */
  172. struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name);
  173. /**
  174. * Allocates a block of physical memory from the free list, at
  175. * (optional) requested address and alignment.
  176. *
  177. * @req_size: size of region to allocate. All requests are rounded up
  178. * to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
  179. *
  180. * @address_min: Minimum address that block can occupy.
  181. *
  182. * @address_max: Specifies the maximum address_min (inclusive) that
  183. * the allocation can use.
  184. *
  185. * @alignment: Requested alignment of the block. If this alignment
  186. * cannot be met, the allocation fails. This must be a
  187. * power of 2. (Note: Alignment of
  188. * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
  189. * internally enforced. Requested alignments of less than
  190. * CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
  191. * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
  192. *
  193. * @flags: Flags to control options for the allocation.
  194. *
  195. * Returns physical address of block allocated, or -1 on failure
  196. */
  197. int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
  198. uint64_t address_max, uint64_t alignment,
  199. uint32_t flags);
  200. /**
  201. * Finds a named memory block by name.
  202. * Also used for finding an unused entry in the named block table.
  203. *
  204. * @name: Name of memory block to find. If NULL pointer given, then
  205. * finds unused descriptor, if available.
  206. *
  207. * @flags: Flags to control options for the allocation.
  208. *
  209. * Returns Pointer to memory block descriptor, NULL if not found.
  210. * If NULL returned when name parameter is NULL, then no memory
  211. * block descriptors are available.
  212. */
  213. struct cvmx_bootmem_named_block_desc *
  214. cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags);
  215. /**
  216. * Frees a named block.
  217. *
  218. * @name: name of block to free
  219. * @flags: flags for passing options
  220. *
  221. * Returns 0 on failure
  222. * 1 on success
  223. */
  224. int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags);
  225. /**
  226. * Frees a block to the bootmem allocator list. This must
  227. * be used with care, as the size provided must match the size
  228. * of the block that was allocated, or the list will become
  229. * corrupted.
  230. *
  231. * IMPORTANT: This is only intended to be used as part of named block
  232. * frees and initial population of the free memory list.
  233. * *
  234. *
  235. * @phy_addr: physical address of block
  236. * @size: size of block in bytes.
  237. * @flags: flags for passing options
  238. *
  239. * Returns 1 on success,
  240. * 0 on failure
  241. */
  242. int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags);
  243. /**
  244. * Locks the bootmem allocator. This is useful in certain situations
  245. * where multiple allocations must be made without being interrupted.
  246. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  247. *
  248. */
  249. void cvmx_bootmem_lock(void);
  250. /**
  251. * Unlocks the bootmem allocator. This is useful in certain situations
  252. * where multiple allocations must be made without being interrupted.
  253. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  254. *
  255. */
  256. void cvmx_bootmem_unlock(void);
  257. #endif /* __CVMX_BOOTMEM_H__ */