genalloc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Basic general purpose allocator for managing special purpose
  3. * memory, for example, memory that is not managed by the regular
  4. * kmalloc/kfree interface. Uses for this includes on-device special
  5. * memory, uncached memory etc.
  6. *
  7. * It is safe to use the allocator in NMI handlers and other special
  8. * unblockable contexts that could otherwise deadlock on locks. This
  9. * is implemented by using atomic operations and retries on any
  10. * conflicts. The disadvantage is that there may be livelocks in
  11. * extreme cases. For better scalability, one allocator can be used
  12. * for each CPU.
  13. *
  14. * The lockless operation only works if there is enough memory
  15. * available. If new memory is added to the pool a lock has to be
  16. * still taken. So any user relying on locklessness has to ensure
  17. * that sufficient memory is preallocated.
  18. *
  19. * The basic atomic operation of this allocator is cmpxchg on long.
  20. * On architectures that don't have NMI-safe cmpxchg implementation,
  21. * the allocator can NOT be used in NMI handler. So code uses the
  22. * allocator in NMI handler should depend on
  23. * CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  24. *
  25. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  26. *
  27. * This source code is licensed under the GNU General Public License,
  28. * Version 2. See the file COPYING for more details.
  29. */
  30. #include <linux/slab.h>
  31. #include <linux/export.h>
  32. #include <linux/bitmap.h>
  33. #include <linux/rculist.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/genalloc.h>
  36. #include <linux/of_device.h>
  37. #include <linux/vmalloc.h>
  38. static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
  39. {
  40. return chunk->end_addr - chunk->start_addr + 1;
  41. }
  42. static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
  43. {
  44. unsigned long val, nval;
  45. nval = *addr;
  46. do {
  47. val = nval;
  48. if (val & mask_to_set)
  49. return -EBUSY;
  50. cpu_relax();
  51. } while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
  52. return 0;
  53. }
  54. static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
  55. {
  56. unsigned long val, nval;
  57. nval = *addr;
  58. do {
  59. val = nval;
  60. if ((val & mask_to_clear) != mask_to_clear)
  61. return -EBUSY;
  62. cpu_relax();
  63. } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
  64. return 0;
  65. }
  66. /*
  67. * bitmap_set_ll - set the specified number of bits at the specified position
  68. * @map: pointer to a bitmap
  69. * @start: a bit position in @map
  70. * @nr: number of bits to set
  71. *
  72. * Set @nr bits start from @start in @map lock-lessly. Several users
  73. * can set/clear the same bitmap simultaneously without lock. If two
  74. * users set the same bit, one user will return remain bits, otherwise
  75. * return 0.
  76. */
  77. static int bitmap_set_ll(unsigned long *map, int start, int nr)
  78. {
  79. unsigned long *p = map + BIT_WORD(start);
  80. const int size = start + nr;
  81. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  82. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  83. while (nr - bits_to_set >= 0) {
  84. if (set_bits_ll(p, mask_to_set))
  85. return nr;
  86. nr -= bits_to_set;
  87. bits_to_set = BITS_PER_LONG;
  88. mask_to_set = ~0UL;
  89. p++;
  90. }
  91. if (nr) {
  92. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  93. if (set_bits_ll(p, mask_to_set))
  94. return nr;
  95. }
  96. return 0;
  97. }
  98. /*
  99. * bitmap_clear_ll - clear the specified number of bits at the specified position
  100. * @map: pointer to a bitmap
  101. * @start: a bit position in @map
  102. * @nr: number of bits to set
  103. *
  104. * Clear @nr bits start from @start in @map lock-lessly. Several users
  105. * can set/clear the same bitmap simultaneously without lock. If two
  106. * users clear the same bit, one user will return remain bits,
  107. * otherwise return 0.
  108. */
  109. static int bitmap_clear_ll(unsigned long *map, int start, int nr)
  110. {
  111. unsigned long *p = map + BIT_WORD(start);
  112. const int size = start + nr;
  113. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  114. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  115. while (nr - bits_to_clear >= 0) {
  116. if (clear_bits_ll(p, mask_to_clear))
  117. return nr;
  118. nr -= bits_to_clear;
  119. bits_to_clear = BITS_PER_LONG;
  120. mask_to_clear = ~0UL;
  121. p++;
  122. }
  123. if (nr) {
  124. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  125. if (clear_bits_ll(p, mask_to_clear))
  126. return nr;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * gen_pool_create - create a new special memory pool
  132. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  133. * @nid: node id of the node the pool structure should be allocated on, or -1
  134. *
  135. * Create a new special memory pool that can be used to manage special purpose
  136. * memory not managed by the regular kmalloc/kfree interface.
  137. */
  138. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  139. {
  140. struct gen_pool *pool;
  141. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  142. if (pool != NULL) {
  143. spin_lock_init(&pool->lock);
  144. INIT_LIST_HEAD(&pool->chunks);
  145. pool->min_alloc_order = min_alloc_order;
  146. pool->algo = gen_pool_first_fit;
  147. pool->data = NULL;
  148. pool->name = NULL;
  149. }
  150. return pool;
  151. }
  152. EXPORT_SYMBOL(gen_pool_create);
  153. /**
  154. * gen_pool_add_virt - add a new chunk of special memory to the pool
  155. * @pool: pool to add new memory chunk to
  156. * @virt: virtual starting address of memory chunk to add to pool
  157. * @phys: physical starting address of memory chunk to add to pool
  158. * @size: size in bytes of the memory chunk to add to pool
  159. * @nid: node id of the node the chunk structure and bitmap should be
  160. * allocated on, or -1
  161. *
  162. * Add a new chunk of special memory to the specified pool.
  163. *
  164. * Returns 0 on success or a -ve errno on failure.
  165. */
  166. int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  167. size_t size, int nid)
  168. {
  169. struct gen_pool_chunk *chunk;
  170. int nbits = size >> pool->min_alloc_order;
  171. int nbytes = sizeof(struct gen_pool_chunk) +
  172. BITS_TO_LONGS(nbits) * sizeof(long);
  173. chunk = vzalloc_node(nbytes, nid);
  174. if (unlikely(chunk == NULL))
  175. return -ENOMEM;
  176. chunk->phys_addr = phys;
  177. chunk->start_addr = virt;
  178. chunk->end_addr = virt + size - 1;
  179. atomic_long_set(&chunk->avail, size);
  180. spin_lock(&pool->lock);
  181. list_add_rcu(&chunk->next_chunk, &pool->chunks);
  182. spin_unlock(&pool->lock);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(gen_pool_add_virt);
  186. /**
  187. * gen_pool_virt_to_phys - return the physical address of memory
  188. * @pool: pool to allocate from
  189. * @addr: starting address of memory
  190. *
  191. * Returns the physical address on success, or -1 on error.
  192. */
  193. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  194. {
  195. struct gen_pool_chunk *chunk;
  196. phys_addr_t paddr = -1;
  197. rcu_read_lock();
  198. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  199. if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
  200. paddr = chunk->phys_addr + (addr - chunk->start_addr);
  201. break;
  202. }
  203. }
  204. rcu_read_unlock();
  205. return paddr;
  206. }
  207. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  208. /**
  209. * gen_pool_destroy - destroy a special memory pool
  210. * @pool: pool to destroy
  211. *
  212. * Destroy the specified special memory pool. Verifies that there are no
  213. * outstanding allocations.
  214. */
  215. void gen_pool_destroy(struct gen_pool *pool)
  216. {
  217. struct list_head *_chunk, *_next_chunk;
  218. struct gen_pool_chunk *chunk;
  219. int order = pool->min_alloc_order;
  220. int bit, end_bit;
  221. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  222. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  223. list_del(&chunk->next_chunk);
  224. end_bit = chunk_size(chunk) >> order;
  225. bit = find_next_bit(chunk->bits, end_bit, 0);
  226. BUG_ON(bit < end_bit);
  227. vfree(chunk);
  228. }
  229. kfree_const(pool->name);
  230. kfree(pool);
  231. }
  232. EXPORT_SYMBOL(gen_pool_destroy);
  233. /**
  234. * gen_pool_alloc - allocate special memory from the pool
  235. * @pool: pool to allocate from
  236. * @size: number of bytes to allocate from the pool
  237. *
  238. * Allocate the requested number of bytes from the specified pool.
  239. * Uses the pool allocation function (with first-fit algorithm by default).
  240. * Can not be used in NMI handler on architectures without
  241. * NMI-safe cmpxchg implementation.
  242. */
  243. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  244. {
  245. return gen_pool_alloc_algo(pool, size, pool->algo, pool->data);
  246. }
  247. EXPORT_SYMBOL(gen_pool_alloc);
  248. /**
  249. * gen_pool_alloc_algo - allocate special memory from the pool
  250. * @pool: pool to allocate from
  251. * @size: number of bytes to allocate from the pool
  252. * @algo: algorithm passed from caller
  253. * @data: data passed to algorithm
  254. *
  255. * Allocate the requested number of bytes from the specified pool.
  256. * Uses the pool allocation function (with first-fit algorithm by default).
  257. * Can not be used in NMI handler on architectures without
  258. * NMI-safe cmpxchg implementation.
  259. */
  260. unsigned long gen_pool_alloc_algo(struct gen_pool *pool, size_t size,
  261. genpool_algo_t algo, void *data)
  262. {
  263. struct gen_pool_chunk *chunk;
  264. unsigned long addr = 0;
  265. int order = pool->min_alloc_order;
  266. int nbits, start_bit, end_bit, remain;
  267. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  268. BUG_ON(in_nmi());
  269. #endif
  270. if (size == 0)
  271. return 0;
  272. nbits = (size + (1UL << order) - 1) >> order;
  273. rcu_read_lock();
  274. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  275. if (size > atomic_long_read(&chunk->avail))
  276. continue;
  277. start_bit = 0;
  278. end_bit = chunk_size(chunk) >> order;
  279. retry:
  280. start_bit = algo(chunk->bits, end_bit, start_bit,
  281. nbits, data, pool, chunk->start_addr);
  282. if (start_bit >= end_bit)
  283. continue;
  284. remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
  285. if (remain) {
  286. remain = bitmap_clear_ll(chunk->bits, start_bit,
  287. nbits - remain);
  288. BUG_ON(remain);
  289. goto retry;
  290. }
  291. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  292. size = nbits << order;
  293. atomic_long_sub(size, &chunk->avail);
  294. break;
  295. }
  296. rcu_read_unlock();
  297. return addr;
  298. }
  299. EXPORT_SYMBOL(gen_pool_alloc_algo);
  300. /**
  301. * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
  302. * @pool: pool to allocate from
  303. * @size: number of bytes to allocate from the pool
  304. * @dma: dma-view physical address return value. Use %NULL if unneeded.
  305. *
  306. * Allocate the requested number of bytes from the specified pool.
  307. * Uses the pool allocation function (with first-fit algorithm by default).
  308. * Can not be used in NMI handler on architectures without
  309. * NMI-safe cmpxchg implementation.
  310. *
  311. * Return: virtual address of the allocated memory, or %NULL on failure
  312. */
  313. void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
  314. {
  315. unsigned long vaddr;
  316. if (!pool)
  317. return NULL;
  318. vaddr = gen_pool_alloc(pool, size);
  319. if (!vaddr)
  320. return NULL;
  321. if (dma)
  322. *dma = gen_pool_virt_to_phys(pool, vaddr);
  323. return (void *)vaddr;
  324. }
  325. EXPORT_SYMBOL(gen_pool_dma_alloc);
  326. /**
  327. * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
  328. * DMA usage
  329. * @pool: pool to allocate from
  330. * @size: number of bytes to allocate from the pool
  331. * @dma: dma-view physical address return value. Use %NULL if unneeded.
  332. *
  333. * Allocate the requested number of zeroed bytes from the specified pool.
  334. * Uses the pool allocation function (with first-fit algorithm by default).
  335. * Can not be used in NMI handler on architectures without
  336. * NMI-safe cmpxchg implementation.
  337. *
  338. * Return: virtual address of the allocated zeroed memory, or %NULL on failure
  339. */
  340. void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
  341. {
  342. void *vaddr = gen_pool_dma_alloc(pool, size, dma);
  343. if (vaddr)
  344. memset(vaddr, 0, size);
  345. return vaddr;
  346. }
  347. EXPORT_SYMBOL(gen_pool_dma_zalloc);
  348. /**
  349. * gen_pool_free - free allocated special memory back to the pool
  350. * @pool: pool to free to
  351. * @addr: starting address of memory to free back to pool
  352. * @size: size in bytes of memory to free
  353. *
  354. * Free previously allocated special memory back to the specified
  355. * pool. Can not be used in NMI handler on architectures without
  356. * NMI-safe cmpxchg implementation.
  357. */
  358. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  359. {
  360. struct gen_pool_chunk *chunk;
  361. int order = pool->min_alloc_order;
  362. int start_bit, nbits, remain;
  363. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  364. BUG_ON(in_nmi());
  365. #endif
  366. nbits = (size + (1UL << order) - 1) >> order;
  367. rcu_read_lock();
  368. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  369. if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
  370. BUG_ON(addr + size - 1 > chunk->end_addr);
  371. start_bit = (addr - chunk->start_addr) >> order;
  372. remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
  373. BUG_ON(remain);
  374. size = nbits << order;
  375. atomic_long_add(size, &chunk->avail);
  376. rcu_read_unlock();
  377. return;
  378. }
  379. }
  380. rcu_read_unlock();
  381. BUG();
  382. }
  383. EXPORT_SYMBOL(gen_pool_free);
  384. /**
  385. * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
  386. * @pool: the generic memory pool
  387. * @func: func to call
  388. * @data: additional data used by @func
  389. *
  390. * Call @func for every chunk of generic memory pool. The @func is
  391. * called with rcu_read_lock held.
  392. */
  393. void gen_pool_for_each_chunk(struct gen_pool *pool,
  394. void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
  395. void *data)
  396. {
  397. struct gen_pool_chunk *chunk;
  398. rcu_read_lock();
  399. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
  400. func(pool, chunk, data);
  401. rcu_read_unlock();
  402. }
  403. EXPORT_SYMBOL(gen_pool_for_each_chunk);
  404. /**
  405. * addr_in_gen_pool - checks if an address falls within the range of a pool
  406. * @pool: the generic memory pool
  407. * @start: start address
  408. * @size: size of the region
  409. *
  410. * Check if the range of addresses falls within the specified pool. Returns
  411. * true if the entire range is contained in the pool and false otherwise.
  412. */
  413. bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
  414. size_t size)
  415. {
  416. bool found = false;
  417. unsigned long end = start + size - 1;
  418. struct gen_pool_chunk *chunk;
  419. rcu_read_lock();
  420. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) {
  421. if (start >= chunk->start_addr && start <= chunk->end_addr) {
  422. if (end <= chunk->end_addr) {
  423. found = true;
  424. break;
  425. }
  426. }
  427. }
  428. rcu_read_unlock();
  429. return found;
  430. }
  431. /**
  432. * gen_pool_avail - get available free space of the pool
  433. * @pool: pool to get available free space
  434. *
  435. * Return available free space of the specified pool.
  436. */
  437. size_t gen_pool_avail(struct gen_pool *pool)
  438. {
  439. struct gen_pool_chunk *chunk;
  440. size_t avail = 0;
  441. rcu_read_lock();
  442. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  443. avail += atomic_long_read(&chunk->avail);
  444. rcu_read_unlock();
  445. return avail;
  446. }
  447. EXPORT_SYMBOL_GPL(gen_pool_avail);
  448. /**
  449. * gen_pool_size - get size in bytes of memory managed by the pool
  450. * @pool: pool to get size
  451. *
  452. * Return size in bytes of memory managed by the pool.
  453. */
  454. size_t gen_pool_size(struct gen_pool *pool)
  455. {
  456. struct gen_pool_chunk *chunk;
  457. size_t size = 0;
  458. rcu_read_lock();
  459. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  460. size += chunk_size(chunk);
  461. rcu_read_unlock();
  462. return size;
  463. }
  464. EXPORT_SYMBOL_GPL(gen_pool_size);
  465. /**
  466. * gen_pool_set_algo - set the allocation algorithm
  467. * @pool: pool to change allocation algorithm
  468. * @algo: custom algorithm function
  469. * @data: additional data used by @algo
  470. *
  471. * Call @algo for each memory allocation in the pool.
  472. * If @algo is NULL use gen_pool_first_fit as default
  473. * memory allocation function.
  474. */
  475. void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
  476. {
  477. rcu_read_lock();
  478. pool->algo = algo;
  479. if (!pool->algo)
  480. pool->algo = gen_pool_first_fit;
  481. pool->data = data;
  482. rcu_read_unlock();
  483. }
  484. EXPORT_SYMBOL(gen_pool_set_algo);
  485. /**
  486. * gen_pool_first_fit - find the first available region
  487. * of memory matching the size requirement (no alignment constraint)
  488. * @map: The address to base the search on
  489. * @size: The bitmap size in bits
  490. * @start: The bitnumber to start searching at
  491. * @nr: The number of zeroed bits we're looking for
  492. * @data: additional data - unused
  493. * @pool: pool to find the fit region memory from
  494. */
  495. unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
  496. unsigned long start, unsigned int nr, void *data,
  497. struct gen_pool *pool, unsigned long start_addr)
  498. {
  499. return bitmap_find_next_zero_area(map, size, start, nr, 0);
  500. }
  501. EXPORT_SYMBOL(gen_pool_first_fit);
  502. /**
  503. * gen_pool_first_fit_align - find the first available region
  504. * of memory matching the size requirement (alignment constraint)
  505. * @map: The address to base the search on
  506. * @size: The bitmap size in bits
  507. * @start: The bitnumber to start searching at
  508. * @nr: The number of zeroed bits we're looking for
  509. * @data: data for alignment
  510. * @pool: pool to get order from
  511. */
  512. unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
  513. unsigned long start, unsigned int nr, void *data,
  514. struct gen_pool *pool, unsigned long start_addr)
  515. {
  516. struct genpool_data_align *alignment;
  517. unsigned long align_mask, align_off;
  518. int order;
  519. alignment = data;
  520. order = pool->min_alloc_order;
  521. align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
  522. align_off = (start_addr & (alignment->align - 1)) >> order;
  523. return bitmap_find_next_zero_area_off(map, size, start, nr,
  524. align_mask, align_off);
  525. }
  526. EXPORT_SYMBOL(gen_pool_first_fit_align);
  527. /**
  528. * gen_pool_fixed_alloc - reserve a specific region
  529. * @map: The address to base the search on
  530. * @size: The bitmap size in bits
  531. * @start: The bitnumber to start searching at
  532. * @nr: The number of zeroed bits we're looking for
  533. * @data: data for alignment
  534. * @pool: pool to get order from
  535. */
  536. unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size,
  537. unsigned long start, unsigned int nr, void *data,
  538. struct gen_pool *pool, unsigned long start_addr)
  539. {
  540. struct genpool_data_fixed *fixed_data;
  541. int order;
  542. unsigned long offset_bit;
  543. unsigned long start_bit;
  544. fixed_data = data;
  545. order = pool->min_alloc_order;
  546. offset_bit = fixed_data->offset >> order;
  547. if (WARN_ON(fixed_data->offset & ((1UL << order) - 1)))
  548. return size;
  549. start_bit = bitmap_find_next_zero_area(map, size,
  550. start + offset_bit, nr, 0);
  551. if (start_bit != offset_bit)
  552. start_bit = size;
  553. return start_bit;
  554. }
  555. EXPORT_SYMBOL(gen_pool_fixed_alloc);
  556. /**
  557. * gen_pool_first_fit_order_align - find the first available region
  558. * of memory matching the size requirement. The region will be aligned
  559. * to the order of the size specified.
  560. * @map: The address to base the search on
  561. * @size: The bitmap size in bits
  562. * @start: The bitnumber to start searching at
  563. * @nr: The number of zeroed bits we're looking for
  564. * @data: additional data - unused
  565. * @pool: pool to find the fit region memory from
  566. */
  567. unsigned long gen_pool_first_fit_order_align(unsigned long *map,
  568. unsigned long size, unsigned long start,
  569. unsigned int nr, void *data, struct gen_pool *pool,
  570. unsigned long start_addr)
  571. {
  572. unsigned long align_mask = roundup_pow_of_two(nr) - 1;
  573. return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
  574. }
  575. EXPORT_SYMBOL(gen_pool_first_fit_order_align);
  576. /**
  577. * gen_pool_best_fit - find the best fitting region of memory
  578. * macthing the size requirement (no alignment constraint)
  579. * @map: The address to base the search on
  580. * @size: The bitmap size in bits
  581. * @start: The bitnumber to start searching at
  582. * @nr: The number of zeroed bits we're looking for
  583. * @data: additional data - unused
  584. * @pool: pool to find the fit region memory from
  585. *
  586. * Iterate over the bitmap to find the smallest free region
  587. * which we can allocate the memory.
  588. */
  589. unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
  590. unsigned long start, unsigned int nr, void *data,
  591. struct gen_pool *pool, unsigned long start_addr)
  592. {
  593. unsigned long start_bit = size;
  594. unsigned long len = size + 1;
  595. unsigned long index;
  596. index = bitmap_find_next_zero_area(map, size, start, nr, 0);
  597. while (index < size) {
  598. int next_bit = find_next_bit(map, size, index + nr);
  599. if ((next_bit - index) < len) {
  600. len = next_bit - index;
  601. start_bit = index;
  602. if (len == nr)
  603. return start_bit;
  604. }
  605. index = bitmap_find_next_zero_area(map, size,
  606. next_bit + 1, nr, 0);
  607. }
  608. return start_bit;
  609. }
  610. EXPORT_SYMBOL(gen_pool_best_fit);
  611. static void devm_gen_pool_release(struct device *dev, void *res)
  612. {
  613. gen_pool_destroy(*(struct gen_pool **)res);
  614. }
  615. static int devm_gen_pool_match(struct device *dev, void *res, void *data)
  616. {
  617. struct gen_pool **p = res;
  618. /* NULL data matches only a pool without an assigned name */
  619. if (!data && !(*p)->name)
  620. return 1;
  621. if (!data || !(*p)->name)
  622. return 0;
  623. return !strcmp((*p)->name, data);
  624. }
  625. /**
  626. * gen_pool_get - Obtain the gen_pool (if any) for a device
  627. * @dev: device to retrieve the gen_pool from
  628. * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
  629. *
  630. * Returns the gen_pool for the device if one is present, or NULL.
  631. */
  632. struct gen_pool *gen_pool_get(struct device *dev, const char *name)
  633. {
  634. struct gen_pool **p;
  635. p = devres_find(dev, devm_gen_pool_release, devm_gen_pool_match,
  636. (void *)name);
  637. if (!p)
  638. return NULL;
  639. return *p;
  640. }
  641. EXPORT_SYMBOL_GPL(gen_pool_get);
  642. /**
  643. * devm_gen_pool_create - managed gen_pool_create
  644. * @dev: device that provides the gen_pool
  645. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  646. * @nid: node selector for allocated gen_pool, %NUMA_NO_NODE for all nodes
  647. * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
  648. *
  649. * Create a new special memory pool that can be used to manage special purpose
  650. * memory not managed by the regular kmalloc/kfree interface. The pool will be
  651. * automatically destroyed by the device management code.
  652. */
  653. struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
  654. int nid, const char *name)
  655. {
  656. struct gen_pool **ptr, *pool;
  657. const char *pool_name = NULL;
  658. /* Check that genpool to be created is uniquely addressed on device */
  659. if (gen_pool_get(dev, name))
  660. return ERR_PTR(-EINVAL);
  661. if (name) {
  662. pool_name = kstrdup_const(name, GFP_KERNEL);
  663. if (!pool_name)
  664. return ERR_PTR(-ENOMEM);
  665. }
  666. ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
  667. if (!ptr)
  668. goto free_pool_name;
  669. pool = gen_pool_create(min_alloc_order, nid);
  670. if (!pool)
  671. goto free_devres;
  672. *ptr = pool;
  673. pool->name = pool_name;
  674. devres_add(dev, ptr);
  675. return pool;
  676. free_devres:
  677. devres_free(ptr);
  678. free_pool_name:
  679. kfree_const(pool_name);
  680. return ERR_PTR(-ENOMEM);
  681. }
  682. EXPORT_SYMBOL(devm_gen_pool_create);
  683. #ifdef CONFIG_OF
  684. /**
  685. * of_gen_pool_get - find a pool by phandle property
  686. * @np: device node
  687. * @propname: property name containing phandle(s)
  688. * @index: index into the phandle array
  689. *
  690. * Returns the pool that contains the chunk starting at the physical
  691. * address of the device tree node pointed at by the phandle property,
  692. * or NULL if not found.
  693. */
  694. struct gen_pool *of_gen_pool_get(struct device_node *np,
  695. const char *propname, int index)
  696. {
  697. struct platform_device *pdev;
  698. struct device_node *np_pool, *parent;
  699. const char *name = NULL;
  700. struct gen_pool *pool = NULL;
  701. np_pool = of_parse_phandle(np, propname, index);
  702. if (!np_pool)
  703. return NULL;
  704. pdev = of_find_device_by_node(np_pool);
  705. if (!pdev) {
  706. /* Check if named gen_pool is created by parent node device */
  707. parent = of_get_parent(np_pool);
  708. pdev = of_find_device_by_node(parent);
  709. of_node_put(parent);
  710. of_property_read_string(np_pool, "label", &name);
  711. if (!name)
  712. name = np_pool->name;
  713. }
  714. if (pdev)
  715. pool = gen_pool_get(&pdev->dev, name);
  716. of_node_put(np_pool);
  717. return pool;
  718. }
  719. EXPORT_SYMBOL_GPL(of_gen_pool_get);
  720. #endif /* CONFIG_OF */