dmapool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include <linux/device.h>
  2. #include <linux/mm.h>
  3. #include <asm/io.h> /* Needed for i386 to build */
  4. #include <asm/scatterlist.h> /* Needed for i386 to build */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/dmapool.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/poison.h>
  10. /*
  11. * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
  12. * small blocks are easily used by drivers for bus mastering controllers.
  13. * This should probably be sharing the guts of the slab allocator.
  14. */
  15. struct dma_pool { /* the pool */
  16. struct list_head page_list;
  17. spinlock_t lock;
  18. size_t blocks_per_page;
  19. size_t size;
  20. struct device *dev;
  21. size_t allocation;
  22. char name [32];
  23. wait_queue_head_t waitq;
  24. struct list_head pools;
  25. };
  26. struct dma_page { /* cacheable header for 'allocation' bytes */
  27. struct list_head page_list;
  28. void *vaddr;
  29. dma_addr_t dma;
  30. unsigned in_use;
  31. unsigned long bitmap [0];
  32. };
  33. #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
  34. static DECLARE_MUTEX (pools_lock);
  35. static ssize_t
  36. show_pools (struct device *dev, struct device_attribute *attr, char *buf)
  37. {
  38. unsigned temp;
  39. unsigned size;
  40. char *next;
  41. struct dma_page *page;
  42. struct dma_pool *pool;
  43. next = buf;
  44. size = PAGE_SIZE;
  45. temp = scnprintf(next, size, "poolinfo - 0.1\n");
  46. size -= temp;
  47. next += temp;
  48. down (&pools_lock);
  49. list_for_each_entry(pool, &dev->dma_pools, pools) {
  50. unsigned pages = 0;
  51. unsigned blocks = 0;
  52. list_for_each_entry(page, &pool->page_list, page_list) {
  53. pages++;
  54. blocks += page->in_use;
  55. }
  56. /* per-pool info, no real statistics yet */
  57. temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
  58. pool->name,
  59. blocks, pages * pool->blocks_per_page,
  60. pool->size, pages);
  61. size -= temp;
  62. next += temp;
  63. }
  64. up (&pools_lock);
  65. return PAGE_SIZE - size;
  66. }
  67. static DEVICE_ATTR (pools, S_IRUGO, show_pools, NULL);
  68. /**
  69. * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
  70. * @name: name of pool, for diagnostics
  71. * @dev: device that will be doing the DMA
  72. * @size: size of the blocks in this pool.
  73. * @align: alignment requirement for blocks; must be a power of two
  74. * @allocation: returned blocks won't cross this boundary (or zero)
  75. * Context: !in_interrupt()
  76. *
  77. * Returns a dma allocation pool with the requested characteristics, or
  78. * null if one can't be created. Given one of these pools, dma_pool_alloc()
  79. * may be used to allocate memory. Such memory will all have "consistent"
  80. * DMA mappings, accessible by the device and its driver without using
  81. * cache flushing primitives. The actual size of blocks allocated may be
  82. * larger than requested because of alignment.
  83. *
  84. * If allocation is nonzero, objects returned from dma_pool_alloc() won't
  85. * cross that size boundary. This is useful for devices which have
  86. * addressing restrictions on individual DMA transfers, such as not crossing
  87. * boundaries of 4KBytes.
  88. */
  89. struct dma_pool *
  90. dma_pool_create (const char *name, struct device *dev,
  91. size_t size, size_t align, size_t allocation)
  92. {
  93. struct dma_pool *retval;
  94. if (align == 0)
  95. align = 1;
  96. if (size == 0)
  97. return NULL;
  98. else if (size < align)
  99. size = align;
  100. else if ((size % align) != 0) {
  101. size += align + 1;
  102. size &= ~(align - 1);
  103. }
  104. if (allocation == 0) {
  105. if (PAGE_SIZE < size)
  106. allocation = size;
  107. else
  108. allocation = PAGE_SIZE;
  109. // FIXME: round up for less fragmentation
  110. } else if (allocation < size)
  111. return NULL;
  112. if (!(retval = kmalloc (sizeof *retval, SLAB_KERNEL)))
  113. return retval;
  114. strlcpy (retval->name, name, sizeof retval->name);
  115. retval->dev = dev;
  116. INIT_LIST_HEAD (&retval->page_list);
  117. spin_lock_init (&retval->lock);
  118. retval->size = size;
  119. retval->allocation = allocation;
  120. retval->blocks_per_page = allocation / size;
  121. init_waitqueue_head (&retval->waitq);
  122. if (dev) {
  123. down (&pools_lock);
  124. if (list_empty (&dev->dma_pools))
  125. device_create_file (dev, &dev_attr_pools);
  126. /* note: not currently insisting "name" be unique */
  127. list_add (&retval->pools, &dev->dma_pools);
  128. up (&pools_lock);
  129. } else
  130. INIT_LIST_HEAD (&retval->pools);
  131. return retval;
  132. }
  133. static struct dma_page *
  134. pool_alloc_page (struct dma_pool *pool, gfp_t mem_flags)
  135. {
  136. struct dma_page *page;
  137. int mapsize;
  138. mapsize = pool->blocks_per_page;
  139. mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
  140. mapsize *= sizeof (long);
  141. page = (struct dma_page *) kmalloc (mapsize + sizeof *page, mem_flags);
  142. if (!page)
  143. return NULL;
  144. page->vaddr = dma_alloc_coherent (pool->dev,
  145. pool->allocation,
  146. &page->dma,
  147. mem_flags);
  148. if (page->vaddr) {
  149. memset (page->bitmap, 0xff, mapsize); // bit set == free
  150. #ifdef CONFIG_DEBUG_SLAB
  151. memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
  152. #endif
  153. list_add (&page->page_list, &pool->page_list);
  154. page->in_use = 0;
  155. } else {
  156. kfree (page);
  157. page = NULL;
  158. }
  159. return page;
  160. }
  161. static inline int
  162. is_page_busy (int blocks, unsigned long *bitmap)
  163. {
  164. while (blocks > 0) {
  165. if (*bitmap++ != ~0UL)
  166. return 1;
  167. blocks -= BITS_PER_LONG;
  168. }
  169. return 0;
  170. }
  171. static void
  172. pool_free_page (struct dma_pool *pool, struct dma_page *page)
  173. {
  174. dma_addr_t dma = page->dma;
  175. #ifdef CONFIG_DEBUG_SLAB
  176. memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
  177. #endif
  178. dma_free_coherent (pool->dev, pool->allocation, page->vaddr, dma);
  179. list_del (&page->page_list);
  180. kfree (page);
  181. }
  182. /**
  183. * dma_pool_destroy - destroys a pool of dma memory blocks.
  184. * @pool: dma pool that will be destroyed
  185. * Context: !in_interrupt()
  186. *
  187. * Caller guarantees that no more memory from the pool is in use,
  188. * and that nothing will try to use the pool after this call.
  189. */
  190. void
  191. dma_pool_destroy (struct dma_pool *pool)
  192. {
  193. down (&pools_lock);
  194. list_del (&pool->pools);
  195. if (pool->dev && list_empty (&pool->dev->dma_pools))
  196. device_remove_file (pool->dev, &dev_attr_pools);
  197. up (&pools_lock);
  198. while (!list_empty (&pool->page_list)) {
  199. struct dma_page *page;
  200. page = list_entry (pool->page_list.next,
  201. struct dma_page, page_list);
  202. if (is_page_busy (pool->blocks_per_page, page->bitmap)) {
  203. if (pool->dev)
  204. dev_err(pool->dev, "dma_pool_destroy %s, %p busy\n",
  205. pool->name, page->vaddr);
  206. else
  207. printk (KERN_ERR "dma_pool_destroy %s, %p busy\n",
  208. pool->name, page->vaddr);
  209. /* leak the still-in-use consistent memory */
  210. list_del (&page->page_list);
  211. kfree (page);
  212. } else
  213. pool_free_page (pool, page);
  214. }
  215. kfree (pool);
  216. }
  217. /**
  218. * dma_pool_alloc - get a block of consistent memory
  219. * @pool: dma pool that will produce the block
  220. * @mem_flags: GFP_* bitmask
  221. * @handle: pointer to dma address of block
  222. *
  223. * This returns the kernel virtual address of a currently unused block,
  224. * and reports its dma address through the handle.
  225. * If such a memory block can't be allocated, null is returned.
  226. */
  227. void *
  228. dma_pool_alloc (struct dma_pool *pool, gfp_t mem_flags, dma_addr_t *handle)
  229. {
  230. unsigned long flags;
  231. struct dma_page *page;
  232. int map, block;
  233. size_t offset;
  234. void *retval;
  235. restart:
  236. spin_lock_irqsave (&pool->lock, flags);
  237. list_for_each_entry(page, &pool->page_list, page_list) {
  238. int i;
  239. /* only cachable accesses here ... */
  240. for (map = 0, i = 0;
  241. i < pool->blocks_per_page;
  242. i += BITS_PER_LONG, map++) {
  243. if (page->bitmap [map] == 0)
  244. continue;
  245. block = ffz (~ page->bitmap [map]);
  246. if ((i + block) < pool->blocks_per_page) {
  247. clear_bit (block, &page->bitmap [map]);
  248. offset = (BITS_PER_LONG * map) + block;
  249. offset *= pool->size;
  250. goto ready;
  251. }
  252. }
  253. }
  254. if (!(page = pool_alloc_page (pool, SLAB_ATOMIC))) {
  255. if (mem_flags & __GFP_WAIT) {
  256. DECLARE_WAITQUEUE (wait, current);
  257. current->state = TASK_INTERRUPTIBLE;
  258. add_wait_queue (&pool->waitq, &wait);
  259. spin_unlock_irqrestore (&pool->lock, flags);
  260. schedule_timeout (POOL_TIMEOUT_JIFFIES);
  261. remove_wait_queue (&pool->waitq, &wait);
  262. goto restart;
  263. }
  264. retval = NULL;
  265. goto done;
  266. }
  267. clear_bit (0, &page->bitmap [0]);
  268. offset = 0;
  269. ready:
  270. page->in_use++;
  271. retval = offset + page->vaddr;
  272. *handle = offset + page->dma;
  273. #ifdef CONFIG_DEBUG_SLAB
  274. memset (retval, POOL_POISON_ALLOCATED, pool->size);
  275. #endif
  276. done:
  277. spin_unlock_irqrestore (&pool->lock, flags);
  278. return retval;
  279. }
  280. static struct dma_page *
  281. pool_find_page (struct dma_pool *pool, dma_addr_t dma)
  282. {
  283. unsigned long flags;
  284. struct dma_page *page;
  285. spin_lock_irqsave (&pool->lock, flags);
  286. list_for_each_entry(page, &pool->page_list, page_list) {
  287. if (dma < page->dma)
  288. continue;
  289. if (dma < (page->dma + pool->allocation))
  290. goto done;
  291. }
  292. page = NULL;
  293. done:
  294. spin_unlock_irqrestore (&pool->lock, flags);
  295. return page;
  296. }
  297. /**
  298. * dma_pool_free - put block back into dma pool
  299. * @pool: the dma pool holding the block
  300. * @vaddr: virtual address of block
  301. * @dma: dma address of block
  302. *
  303. * Caller promises neither device nor driver will again touch this block
  304. * unless it is first re-allocated.
  305. */
  306. void
  307. dma_pool_free (struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  308. {
  309. struct dma_page *page;
  310. unsigned long flags;
  311. int map, block;
  312. if ((page = pool_find_page (pool, dma)) == 0) {
  313. if (pool->dev)
  314. dev_err(pool->dev, "dma_pool_free %s, %p/%lx (bad dma)\n",
  315. pool->name, vaddr, (unsigned long) dma);
  316. else
  317. printk (KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  318. pool->name, vaddr, (unsigned long) dma);
  319. return;
  320. }
  321. block = dma - page->dma;
  322. block /= pool->size;
  323. map = block / BITS_PER_LONG;
  324. block %= BITS_PER_LONG;
  325. #ifdef CONFIG_DEBUG_SLAB
  326. if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
  327. if (pool->dev)
  328. dev_err(pool->dev, "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  329. pool->name, vaddr, (unsigned long long) dma);
  330. else
  331. printk (KERN_ERR "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  332. pool->name, vaddr, (unsigned long long) dma);
  333. return;
  334. }
  335. if (page->bitmap [map] & (1UL << block)) {
  336. if (pool->dev)
  337. dev_err(pool->dev, "dma_pool_free %s, dma %Lx already free\n",
  338. pool->name, (unsigned long long)dma);
  339. else
  340. printk (KERN_ERR "dma_pool_free %s, dma %Lx already free\n",
  341. pool->name, (unsigned long long)dma);
  342. return;
  343. }
  344. memset (vaddr, POOL_POISON_FREED, pool->size);
  345. #endif
  346. spin_lock_irqsave (&pool->lock, flags);
  347. page->in_use--;
  348. set_bit (block, &page->bitmap [map]);
  349. if (waitqueue_active (&pool->waitq))
  350. wake_up (&pool->waitq);
  351. /*
  352. * Resist a temptation to do
  353. * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  354. * Better have a few empty pages hang around.
  355. */
  356. spin_unlock_irqrestore (&pool->lock, flags);
  357. }
  358. EXPORT_SYMBOL (dma_pool_create);
  359. EXPORT_SYMBOL (dma_pool_destroy);
  360. EXPORT_SYMBOL (dma_pool_alloc);
  361. EXPORT_SYMBOL (dma_pool_free);