PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/arch/mips/include/asm/octeon/cvmx-bootmem.h

https://bitbucket.org/CodingWithClass/quantum
C++ Header | 375 lines | 60 code | 42 blank | 273 comment | 0 complexity | 4f608beb78c3ef4b484654a779785a08 MD5 | raw file
Possible License(s): GPL-2.0
  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. /* 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. /**
  164. * Allocate a block of memory from the free list that was passed
  165. * to the application by the bootloader, and assign it a name in the
  166. * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
  167. * Named blocks can later be freed.
  168. *
  169. * @size: Size in bytes of block to allocate
  170. * @alignment: Alignment required - must be power of 2
  171. * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
  172. *
  173. * Returns a pointer to block of memory, NULL on error
  174. */
  175. extern void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment,
  176. char *name);
  177. /**
  178. * Allocate a block of memory from the free list that was passed
  179. * to the application by the bootloader, and assign it a name in the
  180. * global named block table. (part of the cvmx_bootmem_descriptor_t structure)
  181. * Named blocks can later be freed.
  182. *
  183. * @size: Size in bytes of block to allocate
  184. * @address: Physical address to allocate memory at. If this
  185. * memory is not available, the allocation fails.
  186. * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN
  187. * bytes
  188. *
  189. * Returns a pointer to block of memory, NULL on error
  190. */
  191. extern void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address,
  192. char *name);
  193. /**
  194. * Allocate a block of memory from a specific range of the free list
  195. * that was passed to the application by the bootloader, and assign it
  196. * a name in the global named block table. (part of the
  197. * cvmx_bootmem_descriptor_t structure) Named blocks can later be
  198. * freed. If request cannot be satisfied within the address range
  199. * specified, NULL is returned
  200. *
  201. * @size: Size in bytes of block to allocate
  202. * @min_addr: minimum address of range
  203. * @max_addr: maximum address of range
  204. * @align: Alignment of memory to be allocated. (must be a power of 2)
  205. * @name: name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
  206. *
  207. * Returns a pointer to block of memory, NULL on error
  208. */
  209. extern void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
  210. uint64_t max_addr, uint64_t align,
  211. char *name);
  212. extern int cvmx_bootmem_free_named(char *name);
  213. /**
  214. * Finds a named bootmem block by name.
  215. *
  216. * @name: name of block to free
  217. *
  218. * Returns pointer to named block descriptor on success
  219. * 0 on failure
  220. */
  221. struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name);
  222. /**
  223. * Allocates a block of physical memory from the free list, at
  224. * (optional) requested address and alignment.
  225. *
  226. * @req_size: size of region to allocate. All requests are rounded up
  227. * to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
  228. *
  229. * @address_min: Minimum address that block can occupy.
  230. *
  231. * @address_max: Specifies the maximum address_min (inclusive) that
  232. * the allocation can use.
  233. *
  234. * @alignment: Requested alignment of the block. If this alignment
  235. * cannot be met, the allocation fails. This must be a
  236. * power of 2. (Note: Alignment of
  237. * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
  238. * internally enforced. Requested alignments of less than
  239. * CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
  240. * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
  241. *
  242. * @flags: Flags to control options for the allocation.
  243. *
  244. * Returns physical address of block allocated, or -1 on failure
  245. */
  246. int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
  247. uint64_t address_max, uint64_t alignment,
  248. uint32_t flags);
  249. /**
  250. * Allocates a named block of physical memory from the free list, at
  251. * (optional) requested address and alignment.
  252. *
  253. * @param size size of region to allocate. All requests are rounded
  254. * up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE
  255. * bytes size
  256. * @param min_addr Minimum address that block can occupy.
  257. * @param max_addr Specifies the maximum address_min (inclusive) that
  258. * the allocation can use.
  259. * @param alignment Requested alignment of the block. If this
  260. * alignment cannot be met, the allocation fails.
  261. * This must be a power of 2. (Note: Alignment of
  262. * CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
  263. * internally enforced. Requested alignments of less
  264. * than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
  265. * CVMX_BOOTMEM_ALIGNMENT_SIZE.)
  266. * @param name name to assign to named block
  267. * @param flags Flags to control options for the allocation.
  268. *
  269. * @return physical address of block allocated, or -1 on failure
  270. */
  271. int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
  272. uint64_t max_addr,
  273. uint64_t alignment,
  274. char *name, uint32_t flags);
  275. /**
  276. * Finds a named memory block by name.
  277. * Also used for finding an unused entry in the named block table.
  278. *
  279. * @name: Name of memory block to find. If NULL pointer given, then
  280. * finds unused descriptor, if available.
  281. *
  282. * @flags: Flags to control options for the allocation.
  283. *
  284. * Returns Pointer to memory block descriptor, NULL if not found.
  285. * If NULL returned when name parameter is NULL, then no memory
  286. * block descriptors are available.
  287. */
  288. struct cvmx_bootmem_named_block_desc *
  289. cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags);
  290. /**
  291. * Frees a named block.
  292. *
  293. * @name: name of block to free
  294. * @flags: flags for passing options
  295. *
  296. * Returns 0 on failure
  297. * 1 on success
  298. */
  299. int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags);
  300. /**
  301. * Frees a block to the bootmem allocator list. This must
  302. * be used with care, as the size provided must match the size
  303. * of the block that was allocated, or the list will become
  304. * corrupted.
  305. *
  306. * IMPORTANT: This is only intended to be used as part of named block
  307. * frees and initial population of the free memory list.
  308. * *
  309. *
  310. * @phy_addr: physical address of block
  311. * @size: size of block in bytes.
  312. * @flags: flags for passing options
  313. *
  314. * Returns 1 on success,
  315. * 0 on failure
  316. */
  317. int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags);
  318. /**
  319. * Locks the bootmem allocator. This is useful in certain situations
  320. * where multiple allocations must be made without being interrupted.
  321. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  322. *
  323. */
  324. void cvmx_bootmem_lock(void);
  325. /**
  326. * Unlocks the bootmem allocator. This is useful in certain situations
  327. * where multiple allocations must be made without being interrupted.
  328. * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
  329. *
  330. */
  331. void cvmx_bootmem_unlock(void);
  332. extern struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void);
  333. #endif /* __CVMX_BOOTMEM_H__ */