cvmx-bootmem.c 23 KB

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