cvmx-bootmem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. #include <linux/export.h>
  32. #include <linux/kernel.h>
  33. #include <asm/octeon/cvmx.h>
  34. #include <asm/octeon/cvmx-spinlock.h>
  35. #include <asm/octeon/cvmx-bootmem.h>
  36. /*#define DEBUG */
  37. static struct cvmx_bootmem_desc *cvmx_bootmem_desc;
  38. /* See header file for descriptions of functions */
  39. /*
  40. * This macro returns a member of the
  41. * cvmx_bootmem_named_block_desc_t structure. These members can't
  42. * be directly addressed as they might be in memory not directly
  43. * reachable. In the case where bootmem is compiled with
  44. * LINUX_HOST, the structure itself might be located on a remote
  45. * Octeon. The argument "field" is the member name of the
  46. * cvmx_bootmem_named_block_desc_t to read. Regardless of the type
  47. * of the field, the return type is always a uint64_t. The "addr"
  48. * parameter is the physical address of the structure.
  49. */
  50. #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \
  51. __cvmx_bootmem_desc_get(addr, \
  52. offsetof(struct cvmx_bootmem_named_block_desc, field), \
  53. sizeof_field(struct cvmx_bootmem_named_block_desc, field))
  54. /*
  55. * This function is the implementation of the get macros defined
  56. * for individual structure members. The argument are generated
  57. * by the macros inorder to read only the needed memory.
  58. *
  59. * @param base 64bit physical address of the complete structure
  60. * @param offset Offset from the beginning of the structure to the member being
  61. * accessed.
  62. * @param size Size of the structure member.
  63. *
  64. * @return Value of the structure member promoted into a uint64_t.
  65. */
  66. static inline uint64_t __cvmx_bootmem_desc_get(uint64_t base, int offset,
  67. int size)
  68. {
  69. base = (1ull << 63) | (base + offset);
  70. switch (size) {
  71. case 4:
  72. return cvmx_read64_uint32(base);
  73. case 8:
  74. return cvmx_read64_uint64(base);
  75. default:
  76. return 0;
  77. }
  78. }
  79. /*
  80. * Wrapper functions are provided for reading/writing the size and
  81. * next block values as these may not be directly addressible (in 32
  82. * bit applications, for instance.) Offsets of data elements in
  83. * bootmem list, must match cvmx_bootmem_block_header_t.
  84. */
  85. #define NEXT_OFFSET 0
  86. #define SIZE_OFFSET 8
  87. static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)
  88. {
  89. cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);
  90. }
  91. static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)
  92. {
  93. cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);
  94. }
  95. static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)
  96. {
  97. return cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63));
  98. }
  99. static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)
  100. {
  101. return cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63));
  102. }
  103. /*
  104. * Allocate a block of memory from the free list that was
  105. * passed to the application by the bootloader within a specified
  106. * address range. This is an allocate-only algorithm, so
  107. * freeing memory is not possible. Allocation will fail if
  108. * memory cannot be allocated in the requested range.
  109. *
  110. * @size: Size in bytes of block to allocate
  111. * @min_addr: defines the minimum address of the range
  112. * @max_addr: defines the maximum address of the range
  113. * @alignment: Alignment required - must be power of 2
  114. * Returns pointer to block of memory, NULL on error
  115. */
  116. static void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,
  117. uint64_t min_addr, uint64_t max_addr)
  118. {
  119. int64_t address;
  120. address =
  121. cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);
  122. if (address > 0)
  123. return cvmx_phys_to_ptr(address);
  124. else
  125. return NULL;
  126. }
  127. void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
  128. uint64_t alignment)
  129. {
  130. return cvmx_bootmem_alloc_range(size, alignment, address,
  131. address + size);
  132. }
  133. void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
  134. uint64_t max_addr, uint64_t align,
  135. char *name)
  136. {
  137. int64_t addr;
  138. addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
  139. align, name, 0);
  140. if (addr >= 0)
  141. return cvmx_phys_to_ptr(addr);
  142. else
  143. return NULL;
  144. }
  145. void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, char *name)
  146. {
  147. return cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name);
  148. }
  149. EXPORT_SYMBOL(cvmx_bootmem_alloc_named);
  150. void cvmx_bootmem_lock(void)
  151. {
  152. cvmx_spinlock_lock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
  153. }
  154. void cvmx_bootmem_unlock(void)
  155. {
  156. cvmx_spinlock_unlock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
  157. }
  158. int cvmx_bootmem_init(void *mem_desc_ptr)
  159. {
  160. /* Here we set the global pointer to the bootmem descriptor
  161. * block. This pointer will be used directly, so we will set
  162. * it up to be directly usable by the application. It is set
  163. * up as follows for the various runtime/ABI combinations:
  164. *
  165. * Linux 64 bit: Set XKPHYS bit
  166. * Linux 32 bit: use mmap to create mapping, use virtual address
  167. * CVMX 64 bit: use physical address directly
  168. * CVMX 32 bit: use physical address directly
  169. *
  170. * Note that the CVMX environment assumes the use of 1-1 TLB
  171. * mappings so that the physical addresses can be used
  172. * directly
  173. */
  174. if (!cvmx_bootmem_desc) {
  175. #if defined(CVMX_ABI_64)
  176. /* Set XKPHYS bit */
  177. cvmx_bootmem_desc = cvmx_phys_to_ptr(CAST64(mem_desc_ptr));
  178. #else
  179. cvmx_bootmem_desc = (struct cvmx_bootmem_desc *) mem_desc_ptr;
  180. #endif
  181. }
  182. return 0;
  183. }
  184. /*
  185. * The cvmx_bootmem_phy* functions below return 64 bit physical
  186. * addresses, and expose more features that the cvmx_bootmem_functions
  187. * above. These are required for full memory space access in 32 bit
  188. * applications, as well as for using some advance features. Most
  189. * applications should not need to use these.
  190. */
  191. int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
  192. uint64_t address_max, uint64_t alignment,
  193. uint32_t flags)
  194. {
  195. uint64_t head_addr;
  196. uint64_t ent_addr;
  197. /* points to previous list entry, NULL current entry is head of list */
  198. uint64_t prev_addr = 0;
  199. uint64_t new_ent_addr = 0;
  200. uint64_t desired_min_addr;
  201. #ifdef DEBUG
  202. cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, "
  203. "min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
  204. (unsigned long long)req_size,
  205. (unsigned long long)address_min,
  206. (unsigned long long)address_max,
  207. (unsigned long long)alignment);
  208. #endif
  209. if (cvmx_bootmem_desc->major_version > 3) {
  210. cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
  211. "version: %d.%d at addr: %p\n",
  212. (int)cvmx_bootmem_desc->major_version,
  213. (int)cvmx_bootmem_desc->minor_version,
  214. cvmx_bootmem_desc);
  215. goto error_out;
  216. }
  217. /*
  218. * Do a variety of checks to validate the arguments. The
  219. * allocator code will later assume that these checks have
  220. * been made. We validate that the requested constraints are
  221. * not self-contradictory before we look through the list of
  222. * available memory.
  223. */
  224. /* 0 is not a valid req_size for this allocator */
  225. if (!req_size)
  226. goto error_out;
  227. /* Round req_size up to mult of minimum alignment bytes */
  228. req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) &
  229. ~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
  230. /*
  231. * Convert !0 address_min and 0 address_max to special case of
  232. * range that specifies an exact memory block to allocate. Do
  233. * this before other checks and adjustments so that this
  234. * tranformation will be validated.
  235. */
  236. if (address_min && !address_max)
  237. address_max = address_min + req_size;
  238. else if (!address_min && !address_max)
  239. address_max = ~0ull; /* If no limits given, use max limits */
  240. /*
  241. * Enforce minimum alignment (this also keeps the minimum free block
  242. * req_size the same as the alignment req_size.
  243. */
  244. if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)
  245. alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;
  246. /*
  247. * Adjust address minimum based on requested alignment (round
  248. * up to meet alignment). Do this here so we can reject
  249. * impossible requests up front. (NOP for address_min == 0)
  250. */
  251. if (alignment)
  252. address_min = ALIGN(address_min, alignment);
  253. /*
  254. * Reject inconsistent args. We have adjusted these, so this
  255. * may fail due to our internal changes even if this check
  256. * would pass for the values the user supplied.
  257. */
  258. if (req_size > address_max - address_min)
  259. goto error_out;
  260. /* Walk through the list entries - first fit found is returned */
  261. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  262. cvmx_bootmem_lock();
  263. head_addr = cvmx_bootmem_desc->head_addr;
  264. ent_addr = head_addr;
  265. for (; ent_addr;
  266. prev_addr = ent_addr,
  267. ent_addr = cvmx_bootmem_phy_get_next(ent_addr)) {
  268. uint64_t usable_base, usable_max;
  269. uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
  270. if (cvmx_bootmem_phy_get_next(ent_addr)
  271. && ent_addr > cvmx_bootmem_phy_get_next(ent_addr)) {
  272. cvmx_dprintf("Internal bootmem_alloc() error: ent: "
  273. "0x%llx, next: 0x%llx\n",
  274. (unsigned long long)ent_addr,
  275. (unsigned long long)
  276. cvmx_bootmem_phy_get_next(ent_addr));
  277. goto error_out;
  278. }
  279. /*
  280. * Determine if this is an entry that can satisfy the
  281. * request Check to make sure entry is large enough to
  282. * satisfy request.
  283. */
  284. usable_base =
  285. ALIGN(max(address_min, ent_addr), alignment);
  286. usable_max = min(address_max, ent_addr + ent_size);
  287. /*
  288. * We should be able to allocate block at address
  289. * usable_base.
  290. */
  291. desired_min_addr = usable_base;
  292. /*
  293. * Determine if request can be satisfied from the
  294. * current entry.
  295. */
  296. if (!((ent_addr + ent_size) > usable_base
  297. && ent_addr < address_max
  298. && req_size <= usable_max - usable_base))
  299. continue;
  300. /*
  301. * We have found an entry that has room to satisfy the
  302. * request, so allocate it from this entry. If end
  303. * CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from
  304. * the end of this block rather than the beginning.
  305. */
  306. if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC) {
  307. desired_min_addr = usable_max - req_size;
  308. /*
  309. * Align desired address down to required
  310. * alignment.
  311. */
  312. desired_min_addr &= ~(alignment - 1);
  313. }
  314. /* Match at start of entry */
  315. if (desired_min_addr == ent_addr) {
  316. if (req_size < ent_size) {
  317. /*
  318. * big enough to create a new block
  319. * from top portion of block.
  320. */
  321. new_ent_addr = ent_addr + req_size;
  322. cvmx_bootmem_phy_set_next(new_ent_addr,
  323. cvmx_bootmem_phy_get_next(ent_addr));
  324. cvmx_bootmem_phy_set_size(new_ent_addr,
  325. ent_size -
  326. req_size);
  327. /*
  328. * Adjust next pointer as following
  329. * code uses this.
  330. */
  331. cvmx_bootmem_phy_set_next(ent_addr,
  332. new_ent_addr);
  333. }
  334. /*
  335. * adjust prev ptr or head to remove this
  336. * entry from list.
  337. */
  338. if (prev_addr)
  339. cvmx_bootmem_phy_set_next(prev_addr,
  340. cvmx_bootmem_phy_get_next(ent_addr));
  341. else
  342. /*
  343. * head of list being returned, so
  344. * update head ptr.
  345. */
  346. cvmx_bootmem_desc->head_addr =
  347. cvmx_bootmem_phy_get_next(ent_addr);
  348. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  349. cvmx_bootmem_unlock();
  350. return desired_min_addr;
  351. }
  352. /*
  353. * block returned doesn't start at beginning of entry,
  354. * so we know that we will be splitting a block off
  355. * the front of this one. Create a new block from the
  356. * beginning, add to list, and go to top of loop
  357. * again.
  358. *
  359. * create new block from high portion of
  360. * block, so that top block starts at desired
  361. * addr.
  362. */
  363. new_ent_addr = desired_min_addr;
  364. cvmx_bootmem_phy_set_next(new_ent_addr,
  365. cvmx_bootmem_phy_get_next
  366. (ent_addr));
  367. cvmx_bootmem_phy_set_size(new_ent_addr,
  368. cvmx_bootmem_phy_get_size
  369. (ent_addr) -
  370. (desired_min_addr -
  371. ent_addr));
  372. cvmx_bootmem_phy_set_size(ent_addr,
  373. desired_min_addr - ent_addr);
  374. cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
  375. /* Loop again to handle actual alloc from new block */
  376. }
  377. error_out:
  378. /* We didn't find anything, so return error */
  379. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  380. cvmx_bootmem_unlock();
  381. return -1;
  382. }
  383. int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)
  384. {
  385. uint64_t cur_addr;
  386. uint64_t prev_addr = 0; /* zero is invalid */
  387. int retval = 0;
  388. #ifdef DEBUG
  389. cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n",
  390. (unsigned long long)phy_addr, (unsigned long long)size);
  391. #endif
  392. if (cvmx_bootmem_desc->major_version > 3) {
  393. cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
  394. "version: %d.%d at addr: %p\n",
  395. (int)cvmx_bootmem_desc->major_version,
  396. (int)cvmx_bootmem_desc->minor_version,
  397. cvmx_bootmem_desc);
  398. return 0;
  399. }
  400. /* 0 is not a valid size for this allocator */
  401. if (!size)
  402. return 0;
  403. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  404. cvmx_bootmem_lock();
  405. cur_addr = cvmx_bootmem_desc->head_addr;
  406. if (cur_addr == 0 || phy_addr < cur_addr) {
  407. /* add at front of list - special case with changing head ptr */
  408. if (cur_addr && phy_addr + size > cur_addr)
  409. goto bootmem_free_done; /* error, overlapping section */
  410. else if (phy_addr + size == cur_addr) {
  411. /* Add to front of existing first block */
  412. cvmx_bootmem_phy_set_next(phy_addr,
  413. cvmx_bootmem_phy_get_next
  414. (cur_addr));
  415. cvmx_bootmem_phy_set_size(phy_addr,
  416. cvmx_bootmem_phy_get_size
  417. (cur_addr) + size);
  418. cvmx_bootmem_desc->head_addr = phy_addr;
  419. } else {
  420. /* New block before first block. OK if cur_addr is 0 */
  421. cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
  422. cvmx_bootmem_phy_set_size(phy_addr, size);
  423. cvmx_bootmem_desc->head_addr = phy_addr;
  424. }
  425. retval = 1;
  426. goto bootmem_free_done;
  427. }
  428. /* Find place in list to add block */
  429. while (cur_addr && phy_addr > cur_addr) {
  430. prev_addr = cur_addr;
  431. cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
  432. }
  433. if (!cur_addr) {
  434. /*
  435. * We have reached the end of the list, add on to end,
  436. * checking to see if we need to combine with last
  437. * block
  438. */
  439. if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
  440. phy_addr) {
  441. cvmx_bootmem_phy_set_size(prev_addr,
  442. cvmx_bootmem_phy_get_size
  443. (prev_addr) + size);
  444. } else {
  445. cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
  446. cvmx_bootmem_phy_set_size(phy_addr, size);
  447. cvmx_bootmem_phy_set_next(phy_addr, 0);
  448. }
  449. retval = 1;
  450. goto bootmem_free_done;
  451. } else {
  452. /*
  453. * insert between prev and cur nodes, checking for
  454. * merge with either/both.
  455. */
  456. if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
  457. phy_addr) {
  458. /* Merge with previous */
  459. cvmx_bootmem_phy_set_size(prev_addr,
  460. cvmx_bootmem_phy_get_size
  461. (prev_addr) + size);
  462. if (phy_addr + size == cur_addr) {
  463. /* Also merge with current */
  464. cvmx_bootmem_phy_set_size(prev_addr,
  465. cvmx_bootmem_phy_get_size(cur_addr) +
  466. cvmx_bootmem_phy_get_size(prev_addr));
  467. cvmx_bootmem_phy_set_next(prev_addr,
  468. cvmx_bootmem_phy_get_next(cur_addr));
  469. }
  470. retval = 1;
  471. goto bootmem_free_done;
  472. } else if (phy_addr + size == cur_addr) {
  473. /* Merge with current */
  474. cvmx_bootmem_phy_set_size(phy_addr,
  475. cvmx_bootmem_phy_get_size
  476. (cur_addr) + size);
  477. cvmx_bootmem_phy_set_next(phy_addr,
  478. cvmx_bootmem_phy_get_next
  479. (cur_addr));
  480. cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
  481. retval = 1;
  482. goto bootmem_free_done;
  483. }
  484. /* It is a standalone block, add in between prev and cur */
  485. cvmx_bootmem_phy_set_size(phy_addr, size);
  486. cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
  487. cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
  488. }
  489. retval = 1;
  490. bootmem_free_done:
  491. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  492. cvmx_bootmem_unlock();
  493. return retval;
  494. }
  495. /*
  496. * Finds a named memory block by name.
  497. * Also used for finding an unused entry in the named block table.
  498. *
  499. * @name: Name of memory block to find. If NULL pointer given, then
  500. * finds unused descriptor, if available.
  501. *
  502. * @flags: Flags to control options for the allocation.
  503. *
  504. * Returns Pointer to memory block descriptor, NULL if not found.
  505. * If NULL returned when name parameter is NULL, then no memory
  506. * block descriptors are available.
  507. */
  508. static struct cvmx_bootmem_named_block_desc *
  509. cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags)
  510. {
  511. unsigned int i;
  512. struct cvmx_bootmem_named_block_desc *named_block_array_ptr;
  513. #ifdef DEBUG
  514. cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);
  515. #endif
  516. /*
  517. * Lock the structure to make sure that it is not being
  518. * changed while we are examining it.
  519. */
  520. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  521. cvmx_bootmem_lock();
  522. /* Use XKPHYS for 64 bit linux */
  523. named_block_array_ptr = (struct cvmx_bootmem_named_block_desc *)
  524. cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);
  525. #ifdef DEBUG
  526. cvmx_dprintf
  527. ("cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n",
  528. named_block_array_ptr);
  529. #endif
  530. if (cvmx_bootmem_desc->major_version == 3) {
  531. for (i = 0;
  532. i < cvmx_bootmem_desc->named_block_num_blocks; i++) {
  533. if ((name && named_block_array_ptr[i].size
  534. && !strncmp(name, named_block_array_ptr[i].name,
  535. cvmx_bootmem_desc->named_block_name_len
  536. - 1))
  537. || (!name && !named_block_array_ptr[i].size)) {
  538. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  539. cvmx_bootmem_unlock();
  540. return &(named_block_array_ptr[i]);
  541. }
  542. }
  543. } else {
  544. cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
  545. "version: %d.%d at addr: %p\n",
  546. (int)cvmx_bootmem_desc->major_version,
  547. (int)cvmx_bootmem_desc->minor_version,
  548. cvmx_bootmem_desc);
  549. }
  550. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  551. cvmx_bootmem_unlock();
  552. return NULL;
  553. }
  554. void *cvmx_bootmem_alloc_named_range_once(uint64_t size, uint64_t min_addr,
  555. uint64_t max_addr, uint64_t align,
  556. char *name,
  557. void (*init) (void *))
  558. {
  559. int64_t addr;
  560. void *ptr;
  561. uint64_t named_block_desc_addr;
  562. named_block_desc_addr = (uint64_t)
  563. cvmx_bootmem_phy_named_block_find(name,
  564. (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
  565. if (named_block_desc_addr) {
  566. addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_desc_addr,
  567. base_addr);
  568. return cvmx_phys_to_ptr(addr);
  569. }
  570. addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
  571. align, name,
  572. (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
  573. if (addr < 0)
  574. return NULL;
  575. ptr = cvmx_phys_to_ptr(addr);
  576. if (init)
  577. init(ptr);
  578. else
  579. memset(ptr, 0, size);
  580. return ptr;
  581. }
  582. EXPORT_SYMBOL(cvmx_bootmem_alloc_named_range_once);
  583. struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name)
  584. {
  585. return cvmx_bootmem_phy_named_block_find(name, 0);
  586. }
  587. EXPORT_SYMBOL(cvmx_bootmem_find_named_block);
  588. /*
  589. * Frees a named block.
  590. *
  591. * @name: name of block to free
  592. * @flags: flags for passing options
  593. *
  594. * Returns 0 on failure
  595. * 1 on success
  596. */
  597. static int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags)
  598. {
  599. struct cvmx_bootmem_named_block_desc *named_block_ptr;
  600. if (cvmx_bootmem_desc->major_version != 3) {
  601. cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
  602. "%d.%d at addr: %p\n",
  603. (int)cvmx_bootmem_desc->major_version,
  604. (int)cvmx_bootmem_desc->minor_version,
  605. cvmx_bootmem_desc);
  606. return 0;
  607. }
  608. #ifdef DEBUG
  609. cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);
  610. #endif
  611. /*
  612. * Take lock here, as name lookup/block free/name free need to
  613. * be atomic.
  614. */
  615. cvmx_bootmem_lock();
  616. named_block_ptr =
  617. cvmx_bootmem_phy_named_block_find(name,
  618. CVMX_BOOTMEM_FLAG_NO_LOCKING);
  619. if (named_block_ptr) {
  620. #ifdef DEBUG
  621. cvmx_dprintf("cvmx_bootmem_phy_named_block_free: "
  622. "%s, base: 0x%llx, size: 0x%llx\n",
  623. name,
  624. (unsigned long long)named_block_ptr->base_addr,
  625. (unsigned long long)named_block_ptr->size);
  626. #endif
  627. __cvmx_bootmem_phy_free(named_block_ptr->base_addr,
  628. named_block_ptr->size,
  629. CVMX_BOOTMEM_FLAG_NO_LOCKING);
  630. named_block_ptr->size = 0;
  631. /* Set size to zero to indicate block not used. */
  632. }
  633. cvmx_bootmem_unlock();
  634. return named_block_ptr != NULL; /* 0 on failure, 1 on success */
  635. }
  636. int cvmx_bootmem_free_named(char *name)
  637. {
  638. return cvmx_bootmem_phy_named_block_free(name, 0);
  639. }
  640. int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
  641. uint64_t max_addr,
  642. uint64_t alignment,
  643. char *name,
  644. uint32_t flags)
  645. {
  646. int64_t addr_allocated;
  647. struct cvmx_bootmem_named_block_desc *named_block_desc_ptr;
  648. #ifdef DEBUG
  649. cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: "
  650. "0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
  651. (unsigned long long)size,
  652. (unsigned long long)min_addr,
  653. (unsigned long long)max_addr,
  654. (unsigned long long)alignment,
  655. name);
  656. #endif
  657. if (cvmx_bootmem_desc->major_version != 3) {
  658. cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
  659. "%d.%d at addr: %p\n",
  660. (int)cvmx_bootmem_desc->major_version,
  661. (int)cvmx_bootmem_desc->minor_version,
  662. cvmx_bootmem_desc);
  663. return -1;
  664. }
  665. /*
  666. * Take lock here, as name lookup/block alloc/name add need to
  667. * be atomic.
  668. */
  669. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  670. cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
  671. /* Get pointer to first available named block descriptor */
  672. named_block_desc_ptr =
  673. cvmx_bootmem_phy_named_block_find(NULL,
  674. flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
  675. /*
  676. * Check to see if name already in use, return error if name
  677. * not available or no more room for blocks.
  678. */
  679. if (cvmx_bootmem_phy_named_block_find(name,
  680. flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr) {
  681. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  682. cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
  683. return -1;
  684. }
  685. /*
  686. * Round size up to mult of minimum alignment bytes We need
  687. * the actual size allocated to allow for blocks to be
  688. * coalesced when they are freed. The alloc routine does the
  689. * same rounding up on all allocations.
  690. */
  691. size = ALIGN(size, CVMX_BOOTMEM_ALIGNMENT_SIZE);
  692. addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr,
  693. alignment,
  694. flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
  695. if (addr_allocated >= 0) {
  696. named_block_desc_ptr->base_addr = addr_allocated;
  697. named_block_desc_ptr->size = size;
  698. strncpy(named_block_desc_ptr->name, name,
  699. cvmx_bootmem_desc->named_block_name_len);
  700. named_block_desc_ptr->name[cvmx_bootmem_desc->named_block_name_len - 1] = 0;
  701. }
  702. if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
  703. cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
  704. return addr_allocated;
  705. }
  706. struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void)
  707. {
  708. return cvmx_bootmem_desc;
  709. }