dmapool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. #include <linux/sched.h>
  11. /*
  12. * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
  13. * small blocks are easily used by drivers for bus mastering controllers.
  14. * This should probably be sharing the guts of the slab allocator.
  15. */
  16. struct dma_pool { /* the pool */
  17. struct list_head page_list;
  18. spinlock_t lock;
  19. size_t blocks_per_page;
  20. size_t size;
  21. struct device *dev;
  22. size_t allocation;
  23. char name [32];
  24. wait_queue_head_t waitq;
  25. struct list_head pools;
  26. };
  27. struct dma_page { /* cacheable header for 'allocation' bytes */
  28. struct list_head page_list;
  29. void *vaddr;
  30. dma_addr_t dma;
  31. unsigned in_use;
  32. unsigned long bitmap [0];
  33. };
  34. #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
  35. static DEFINE_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. mutex_lock(&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. mutex_unlock(&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_node (sizeof *retval, GFP_KERNEL, dev_to_node(dev))))
  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. int ret;
  125. mutex_lock(&pools_lock);
  126. if (list_empty (&dev->dma_pools))
  127. ret = device_create_file (dev, &dev_attr_pools);
  128. else
  129. ret = 0;
  130. /* note: not currently insisting "name" be unique */
  131. if (!ret)
  132. list_add (&retval->pools, &dev->dma_pools);
  133. else {
  134. kfree(retval);
  135. retval = NULL;
  136. }
  137. mutex_unlock(&pools_lock);
  138. } else
  139. INIT_LIST_HEAD (&retval->pools);
  140. return retval;
  141. }
  142. static struct dma_page *
  143. pool_alloc_page (struct dma_pool *pool, gfp_t mem_flags)
  144. {
  145. struct dma_page *page;
  146. int mapsize;
  147. mapsize = pool->blocks_per_page;
  148. mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
  149. mapsize *= sizeof (long);
  150. page = kmalloc(mapsize + sizeof *page, mem_flags);
  151. if (!page)
  152. return NULL;
  153. page->vaddr = dma_alloc_coherent (pool->dev,
  154. pool->allocation,
  155. &page->dma,
  156. mem_flags);
  157. if (page->vaddr) {
  158. memset (page->bitmap, 0xff, mapsize); // bit set == free
  159. #ifdef CONFIG_DEBUG_SLAB
  160. memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
  161. #endif
  162. list_add (&page->page_list, &pool->page_list);
  163. page->in_use = 0;
  164. } else {
  165. kfree (page);
  166. page = NULL;
  167. }
  168. return page;
  169. }
  170. static inline int
  171. is_page_busy (int blocks, unsigned long *bitmap)
  172. {
  173. while (blocks > 0) {
  174. if (*bitmap++ != ~0UL)
  175. return 1;
  176. blocks -= BITS_PER_LONG;
  177. }
  178. return 0;
  179. }
  180. static void
  181. pool_free_page (struct dma_pool *pool, struct dma_page *page)
  182. {
  183. dma_addr_t dma = page->dma;
  184. #ifdef CONFIG_DEBUG_SLAB
  185. memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
  186. #endif
  187. dma_free_coherent (pool->dev, pool->allocation, page->vaddr, dma);
  188. list_del (&page->page_list);
  189. kfree (page);
  190. }
  191. /**
  192. * dma_pool_destroy - destroys a pool of dma memory blocks.
  193. * @pool: dma pool that will be destroyed
  194. * Context: !in_interrupt()
  195. *
  196. * Caller guarantees that no more memory from the pool is in use,
  197. * and that nothing will try to use the pool after this call.
  198. */
  199. void
  200. dma_pool_destroy (struct dma_pool *pool)
  201. {
  202. mutex_lock(&pools_lock);
  203. list_del (&pool->pools);
  204. if (pool->dev && list_empty (&pool->dev->dma_pools))
  205. device_remove_file (pool->dev, &dev_attr_pools);
  206. mutex_unlock(&pools_lock);
  207. while (!list_empty (&pool->page_list)) {
  208. struct dma_page *page;
  209. page = list_entry (pool->page_list.next,
  210. struct dma_page, page_list);
  211. if (is_page_busy (pool->blocks_per_page, page->bitmap)) {
  212. if (pool->dev)
  213. dev_err(pool->dev, "dma_pool_destroy %s, %p busy\n",
  214. pool->name, page->vaddr);
  215. else
  216. printk (KERN_ERR "dma_pool_destroy %s, %p busy\n",
  217. pool->name, page->vaddr);
  218. /* leak the still-in-use consistent memory */
  219. list_del (&page->page_list);
  220. kfree (page);
  221. } else
  222. pool_free_page (pool, page);
  223. }
  224. kfree (pool);
  225. }
  226. /**
  227. * dma_pool_alloc - get a block of consistent memory
  228. * @pool: dma pool that will produce the block
  229. * @mem_flags: GFP_* bitmask
  230. * @handle: pointer to dma address of block
  231. *
  232. * This returns the kernel virtual address of a currently unused block,
  233. * and reports its dma address through the handle.
  234. * If such a memory block can't be allocated, null is returned.
  235. */
  236. void *
  237. dma_pool_alloc (struct dma_pool *pool, gfp_t mem_flags, dma_addr_t *handle)
  238. {
  239. unsigned long flags;
  240. struct dma_page *page;
  241. int map, block;
  242. size_t offset;
  243. void *retval;
  244. restart:
  245. spin_lock_irqsave (&pool->lock, flags);
  246. list_for_each_entry(page, &pool->page_list, page_list) {
  247. int i;
  248. /* only cachable accesses here ... */
  249. for (map = 0, i = 0;
  250. i < pool->blocks_per_page;
  251. i += BITS_PER_LONG, map++) {
  252. if (page->bitmap [map] == 0)
  253. continue;
  254. block = ffz (~ page->bitmap [map]);
  255. if ((i + block) < pool->blocks_per_page) {
  256. clear_bit (block, &page->bitmap [map]);
  257. offset = (BITS_PER_LONG * map) + block;
  258. offset *= pool->size;
  259. goto ready;
  260. }
  261. }
  262. }
  263. if (!(page = pool_alloc_page (pool, GFP_ATOMIC))) {
  264. if (mem_flags & __GFP_WAIT) {
  265. DECLARE_WAITQUEUE (wait, current);
  266. __set_current_state(TASK_INTERRUPTIBLE);
  267. add_wait_queue (&pool->waitq, &wait);
  268. spin_unlock_irqrestore (&pool->lock, flags);
  269. schedule_timeout (POOL_TIMEOUT_JIFFIES);
  270. remove_wait_queue (&pool->waitq, &wait);
  271. goto restart;
  272. }
  273. retval = NULL;
  274. goto done;
  275. }
  276. clear_bit (0, &page->bitmap [0]);
  277. offset = 0;
  278. ready:
  279. page->in_use++;
  280. retval = offset + page->vaddr;
  281. *handle = offset + page->dma;
  282. #ifdef CONFIG_DEBUG_SLAB
  283. memset (retval, POOL_POISON_ALLOCATED, pool->size);
  284. #endif
  285. done:
  286. spin_unlock_irqrestore (&pool->lock, flags);
  287. return retval;
  288. }
  289. static struct dma_page *
  290. pool_find_page (struct dma_pool *pool, dma_addr_t dma)
  291. {
  292. unsigned long flags;
  293. struct dma_page *page;
  294. spin_lock_irqsave (&pool->lock, flags);
  295. list_for_each_entry(page, &pool->page_list, page_list) {
  296. if (dma < page->dma)
  297. continue;
  298. if (dma < (page->dma + pool->allocation))
  299. goto done;
  300. }
  301. page = NULL;
  302. done:
  303. spin_unlock_irqrestore (&pool->lock, flags);
  304. return page;
  305. }
  306. /**
  307. * dma_pool_free - put block back into dma pool
  308. * @pool: the dma pool holding the block
  309. * @vaddr: virtual address of block
  310. * @dma: dma address of block
  311. *
  312. * Caller promises neither device nor driver will again touch this block
  313. * unless it is first re-allocated.
  314. */
  315. void
  316. dma_pool_free (struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  317. {
  318. struct dma_page *page;
  319. unsigned long flags;
  320. int map, block;
  321. if ((page = pool_find_page(pool, dma)) == NULL) {
  322. if (pool->dev)
  323. dev_err(pool->dev, "dma_pool_free %s, %p/%lx (bad dma)\n",
  324. pool->name, vaddr, (unsigned long) dma);
  325. else
  326. printk (KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  327. pool->name, vaddr, (unsigned long) dma);
  328. return;
  329. }
  330. block = dma - page->dma;
  331. block /= pool->size;
  332. map = block / BITS_PER_LONG;
  333. block %= BITS_PER_LONG;
  334. #ifdef CONFIG_DEBUG_SLAB
  335. if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
  336. if (pool->dev)
  337. dev_err(pool->dev, "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  338. pool->name, vaddr, (unsigned long long) dma);
  339. else
  340. printk (KERN_ERR "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  341. pool->name, vaddr, (unsigned long long) dma);
  342. return;
  343. }
  344. if (page->bitmap [map] & (1UL << block)) {
  345. if (pool->dev)
  346. dev_err(pool->dev, "dma_pool_free %s, dma %Lx already free\n",
  347. pool->name, (unsigned long long)dma);
  348. else
  349. printk (KERN_ERR "dma_pool_free %s, dma %Lx already free\n",
  350. pool->name, (unsigned long long)dma);
  351. return;
  352. }
  353. memset (vaddr, POOL_POISON_FREED, pool->size);
  354. #endif
  355. spin_lock_irqsave (&pool->lock, flags);
  356. page->in_use--;
  357. set_bit (block, &page->bitmap [map]);
  358. if (waitqueue_active (&pool->waitq))
  359. wake_up (&pool->waitq);
  360. /*
  361. * Resist a temptation to do
  362. * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  363. * Better have a few empty pages hang around.
  364. */
  365. spin_unlock_irqrestore (&pool->lock, flags);
  366. }
  367. /*
  368. * Managed DMA pool
  369. */
  370. static void dmam_pool_release(struct device *dev, void *res)
  371. {
  372. struct dma_pool *pool = *(struct dma_pool **)res;
  373. dma_pool_destroy(pool);
  374. }
  375. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  376. {
  377. return *(struct dma_pool **)res == match_data;
  378. }
  379. /**
  380. * dmam_pool_create - Managed dma_pool_create()
  381. * @name: name of pool, for diagnostics
  382. * @dev: device that will be doing the DMA
  383. * @size: size of the blocks in this pool.
  384. * @align: alignment requirement for blocks; must be a power of two
  385. * @allocation: returned blocks won't cross this boundary (or zero)
  386. *
  387. * Managed dma_pool_create(). DMA pool created with this function is
  388. * automatically destroyed on driver detach.
  389. */
  390. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  391. size_t size, size_t align, size_t allocation)
  392. {
  393. struct dma_pool **ptr, *pool;
  394. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  395. if (!ptr)
  396. return NULL;
  397. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  398. if (pool)
  399. devres_add(dev, ptr);
  400. else
  401. devres_free(ptr);
  402. return pool;
  403. }
  404. /**
  405. * dmam_pool_destroy - Managed dma_pool_destroy()
  406. * @pool: dma pool that will be destroyed
  407. *
  408. * Managed dma_pool_destroy().
  409. */
  410. void dmam_pool_destroy(struct dma_pool *pool)
  411. {
  412. struct device *dev = pool->dev;
  413. dma_pool_destroy(pool);
  414. WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
  415. }
  416. EXPORT_SYMBOL (dma_pool_create);
  417. EXPORT_SYMBOL (dma_pool_destroy);
  418. EXPORT_SYMBOL (dma_pool_alloc);
  419. EXPORT_SYMBOL (dma_pool_free);
  420. EXPORT_SYMBOL (dmam_pool_create);
  421. EXPORT_SYMBOL (dmam_pool_destroy);