dmapool.c 10 KB

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