cvmx-bootmem.c 17 KB

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