genalloc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Basic general purpose allocator for managing special purpose memory
  3. * not managed by the regular kmalloc/kfree interface.
  4. * Uses for this includes on-device special memory, uncached memory
  5. * etc.
  6. *
  7. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  8. *
  9. * This source code is licensed under the GNU General Public License,
  10. * Version 2. See the file COPYING for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/genalloc.h>
  16. /**
  17. * gen_pool_create - create a new special memory pool
  18. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  19. * @nid: node id of the node the pool structure should be allocated on, or -1
  20. *
  21. * Create a new special memory pool that can be used to manage special purpose
  22. * memory not managed by the regular kmalloc/kfree interface.
  23. */
  24. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  25. {
  26. struct gen_pool *pool;
  27. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  28. if (pool != NULL) {
  29. rwlock_init(&pool->lock);
  30. INIT_LIST_HEAD(&pool->chunks);
  31. pool->min_alloc_order = min_alloc_order;
  32. }
  33. return pool;
  34. }
  35. EXPORT_SYMBOL(gen_pool_create);
  36. /**
  37. * gen_pool_add_virt - add a new chunk of special memory to the pool
  38. * @pool: pool to add new memory chunk to
  39. * @virt: virtual starting address of memory chunk to add to pool
  40. * @phys: physical starting address of memory chunk to add to pool
  41. * @size: size in bytes of the memory chunk to add to pool
  42. * @nid: node id of the node the chunk structure and bitmap should be
  43. * allocated on, or -1
  44. *
  45. * Add a new chunk of special memory to the specified pool.
  46. *
  47. * Returns 0 on success or a -ve errno on failure.
  48. */
  49. int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  50. size_t size, int nid)
  51. {
  52. struct gen_pool_chunk *chunk;
  53. int nbits = size >> pool->min_alloc_order;
  54. int nbytes = sizeof(struct gen_pool_chunk) +
  55. (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
  56. chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
  57. if (unlikely(chunk == NULL))
  58. return -ENOMEM;
  59. spin_lock_init(&chunk->lock);
  60. chunk->phys_addr = phys;
  61. chunk->start_addr = virt;
  62. chunk->end_addr = virt + size;
  63. write_lock(&pool->lock);
  64. list_add(&chunk->next_chunk, &pool->chunks);
  65. write_unlock(&pool->lock);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(gen_pool_add_virt);
  69. /**
  70. * gen_pool_virt_to_phys - return the physical address of memory
  71. * @pool: pool to allocate from
  72. * @addr: starting address of memory
  73. *
  74. * Returns the physical address on success, or -1 on error.
  75. */
  76. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  77. {
  78. struct list_head *_chunk;
  79. struct gen_pool_chunk *chunk;
  80. read_lock(&pool->lock);
  81. list_for_each(_chunk, &pool->chunks) {
  82. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  83. if (addr >= chunk->start_addr && addr < chunk->end_addr)
  84. return chunk->phys_addr + addr - chunk->start_addr;
  85. }
  86. read_unlock(&pool->lock);
  87. return -1;
  88. }
  89. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  90. /**
  91. * gen_pool_destroy - destroy a special memory pool
  92. * @pool: pool to destroy
  93. *
  94. * Destroy the specified special memory pool. Verifies that there are no
  95. * outstanding allocations.
  96. */
  97. void gen_pool_destroy(struct gen_pool *pool)
  98. {
  99. struct list_head *_chunk, *_next_chunk;
  100. struct gen_pool_chunk *chunk;
  101. int order = pool->min_alloc_order;
  102. int bit, end_bit;
  103. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  104. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  105. list_del(&chunk->next_chunk);
  106. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  107. bit = find_next_bit(chunk->bits, end_bit, 0);
  108. BUG_ON(bit < end_bit);
  109. kfree(chunk);
  110. }
  111. kfree(pool);
  112. return;
  113. }
  114. EXPORT_SYMBOL(gen_pool_destroy);
  115. /**
  116. * gen_pool_alloc - allocate special memory from the pool
  117. * @pool: pool to allocate from
  118. * @size: number of bytes to allocate from the pool
  119. *
  120. * Allocate the requested number of bytes from the specified pool.
  121. * Uses a first-fit algorithm.
  122. */
  123. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  124. {
  125. struct list_head *_chunk;
  126. struct gen_pool_chunk *chunk;
  127. unsigned long addr, flags;
  128. int order = pool->min_alloc_order;
  129. int nbits, start_bit, end_bit;
  130. if (size == 0)
  131. return 0;
  132. nbits = (size + (1UL << order) - 1) >> order;
  133. read_lock(&pool->lock);
  134. list_for_each(_chunk, &pool->chunks) {
  135. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  136. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  137. spin_lock_irqsave(&chunk->lock, flags);
  138. start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, 0,
  139. nbits, 0);
  140. if (start_bit >= end_bit) {
  141. spin_unlock_irqrestore(&chunk->lock, flags);
  142. continue;
  143. }
  144. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  145. bitmap_set(chunk->bits, start_bit, nbits);
  146. spin_unlock_irqrestore(&chunk->lock, flags);
  147. read_unlock(&pool->lock);
  148. return addr;
  149. }
  150. read_unlock(&pool->lock);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(gen_pool_alloc);
  154. /**
  155. * gen_pool_free - free allocated special memory back to the pool
  156. * @pool: pool to free to
  157. * @addr: starting address of memory to free back to pool
  158. * @size: size in bytes of memory to free
  159. *
  160. * Free previously allocated special memory back to the specified pool.
  161. */
  162. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  163. {
  164. struct list_head *_chunk;
  165. struct gen_pool_chunk *chunk;
  166. unsigned long flags;
  167. int order = pool->min_alloc_order;
  168. int bit, nbits;
  169. nbits = (size + (1UL << order) - 1) >> order;
  170. read_lock(&pool->lock);
  171. list_for_each(_chunk, &pool->chunks) {
  172. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  173. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  174. BUG_ON(addr + size > chunk->end_addr);
  175. spin_lock_irqsave(&chunk->lock, flags);
  176. bit = (addr - chunk->start_addr) >> order;
  177. while (nbits--)
  178. __clear_bit(bit++, chunk->bits);
  179. spin_unlock_irqrestore(&chunk->lock, flags);
  180. break;
  181. }
  182. }
  183. BUG_ON(nbits > 0);
  184. read_unlock(&pool->lock);
  185. }
  186. EXPORT_SYMBOL(gen_pool_free);