cvmx-bootmem.c 20 KB

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