genalloc.c 22 KB

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