genalloc.c 22 KB

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