genalloc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * This code is based on the buddy allocator found in the sym53c8xx_2
  8. * driver Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>,
  9. * and adapted for general purpose use.
  10. *
  11. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  12. *
  13. * This source code is licensed under the GNU General Public License,
  14. * Version 2. See the file COPYING for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/stddef.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/genalloc.h>
  25. #include <asm/page.h>
  26. struct gen_pool *gen_pool_create(int nr_chunks, int max_chunk_shift,
  27. unsigned long (*fp)(struct gen_pool *),
  28. unsigned long data)
  29. {
  30. struct gen_pool *poolp;
  31. unsigned long tmp;
  32. int i;
  33. /*
  34. * This is really an arbitrary limit, +10 is enough for
  35. * IA64_GRANULE_SHIFT, aka 16MB. If anyone needs a large limit
  36. * this can be increased without problems.
  37. */
  38. if ((max_chunk_shift > (PAGE_SHIFT + 10)) ||
  39. ((max_chunk_shift < ALLOC_MIN_SHIFT) && max_chunk_shift))
  40. return NULL;
  41. if (!max_chunk_shift)
  42. max_chunk_shift = PAGE_SHIFT;
  43. poolp = kmalloc(sizeof(struct gen_pool), GFP_KERNEL);
  44. if (!poolp)
  45. return NULL;
  46. memset(poolp, 0, sizeof(struct gen_pool));
  47. poolp->h = kmalloc(sizeof(struct gen_pool_link) *
  48. (max_chunk_shift - ALLOC_MIN_SHIFT + 1),
  49. GFP_KERNEL);
  50. if (!poolp->h) {
  51. printk(KERN_WARNING "gen_pool_alloc() failed to allocate\n");
  52. kfree(poolp);
  53. return NULL;
  54. }
  55. memset(poolp->h, 0, sizeof(struct gen_pool_link) *
  56. (max_chunk_shift - ALLOC_MIN_SHIFT + 1));
  57. spin_lock_init(&poolp->lock);
  58. poolp->get_new_chunk = fp;
  59. poolp->max_chunk_shift = max_chunk_shift;
  60. poolp->private = data;
  61. for (i = 0; i < nr_chunks; i++) {
  62. tmp = poolp->get_new_chunk(poolp);
  63. printk(KERN_INFO "allocated %lx\n", tmp);
  64. if (!tmp)
  65. break;
  66. gen_pool_free(poolp, tmp, (1 << poolp->max_chunk_shift));
  67. }
  68. return poolp;
  69. }
  70. EXPORT_SYMBOL(gen_pool_create);
  71. /*
  72. * Simple power of two buddy-like generic allocator.
  73. * Provides naturally aligned memory chunks.
  74. */
  75. unsigned long gen_pool_alloc(struct gen_pool *poolp, int size)
  76. {
  77. int j, i, s, max_chunk_size;
  78. unsigned long a, flags;
  79. struct gen_pool_link *h = poolp->h;
  80. max_chunk_size = 1 << poolp->max_chunk_shift;
  81. if (size > max_chunk_size)
  82. return 0;
  83. size = max(size, 1 << ALLOC_MIN_SHIFT);
  84. i = fls(size - 1);
  85. s = 1 << i;
  86. j = i -= ALLOC_MIN_SHIFT;
  87. spin_lock_irqsave(&poolp->lock, flags);
  88. while (!h[j].next) {
  89. if (s == max_chunk_size) {
  90. struct gen_pool_link *ptr;
  91. spin_unlock_irqrestore(&poolp->lock, flags);
  92. ptr = (struct gen_pool_link *)poolp->get_new_chunk(poolp);
  93. spin_lock_irqsave(&poolp->lock, flags);
  94. h[j].next = ptr;
  95. if (h[j].next)
  96. h[j].next->next = NULL;
  97. break;
  98. }
  99. j++;
  100. s <<= 1;
  101. }
  102. a = (unsigned long) h[j].next;
  103. if (a) {
  104. h[j].next = h[j].next->next;
  105. /*
  106. * This should be split into a seperate function doing
  107. * the chunk split in order to support custom
  108. * handling memory not physically accessible by host
  109. */
  110. while (j > i) {
  111. j -= 1;
  112. s >>= 1;
  113. h[j].next = (struct gen_pool_link *) (a + s);
  114. h[j].next->next = NULL;
  115. }
  116. }
  117. spin_unlock_irqrestore(&poolp->lock, flags);
  118. return a;
  119. }
  120. EXPORT_SYMBOL(gen_pool_alloc);
  121. /*
  122. * Counter-part of the generic allocator.
  123. */
  124. void gen_pool_free(struct gen_pool *poolp, unsigned long ptr, int size)
  125. {
  126. struct gen_pool_link *q;
  127. struct gen_pool_link *h = poolp->h;
  128. unsigned long a, b, flags;
  129. int i, s, max_chunk_size;
  130. max_chunk_size = 1 << poolp->max_chunk_shift;
  131. if (size > max_chunk_size)
  132. return;
  133. size = max(size, 1 << ALLOC_MIN_SHIFT);
  134. i = fls(size - 1);
  135. s = 1 << i;
  136. i -= ALLOC_MIN_SHIFT;
  137. a = ptr;
  138. spin_lock_irqsave(&poolp->lock, flags);
  139. while (1) {
  140. if (s == max_chunk_size) {
  141. ((struct gen_pool_link *)a)->next = h[i].next;
  142. h[i].next = (struct gen_pool_link *)a;
  143. break;
  144. }
  145. b = a ^ s;
  146. q = &h[i];
  147. while (q->next && q->next != (struct gen_pool_link *)b)
  148. q = q->next;
  149. if (!q->next) {
  150. ((struct gen_pool_link *)a)->next = h[i].next;
  151. h[i].next = (struct gen_pool_link *)a;
  152. break;
  153. }
  154. q->next = q->next->next;
  155. a = a & b;
  156. s <<= 1;
  157. i++;
  158. }
  159. spin_unlock_irqrestore(&poolp->lock, flags);
  160. }
  161. EXPORT_SYMBOL(gen_pool_free);