dmapool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include <linux/device.h>
  2. #include <linux/mm.h>
  3. #include <asm/io.h> /* Needed for i386 to build */
  4. #include <linux/dma-mapping.h>
  5. #include <linux/dmapool.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/poison.h>
  9. #include <linux/sched.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 DEFINE_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. mutex_lock(&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. mutex_unlock(&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 *dma_pool_create(const char *name, struct device *dev,
  90. size_t size, size_t align, size_t allocation)
  91. {
  92. struct dma_pool *retval;
  93. if (align == 0)
  94. align = 1;
  95. if (size == 0)
  96. return NULL;
  97. else if (size < align)
  98. size = align;
  99. else if ((size % align) != 0) {
  100. size += align + 1;
  101. size &= ~(align - 1);
  102. }
  103. if (allocation == 0) {
  104. if (PAGE_SIZE < size)
  105. allocation = size;
  106. else
  107. allocation = PAGE_SIZE;
  108. /* FIXME: round up for less fragmentation */
  109. } else if (allocation < size)
  110. return NULL;
  111. if (!
  112. (retval =
  113. 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. EXPORT_SYMBOL(dma_pool_create);
  143. static struct dma_page *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, mem_flags);
  156. if (page->vaddr) {
  157. memset(page->bitmap, 0xff, mapsize); /* bit set == free */
  158. #ifdef CONFIG_DEBUG_SLAB
  159. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  160. #endif
  161. list_add(&page->page_list, &pool->page_list);
  162. page->in_use = 0;
  163. } else {
  164. kfree(page);
  165. page = NULL;
  166. }
  167. return page;
  168. }
  169. static inline int is_page_busy(int blocks, unsigned long *bitmap)
  170. {
  171. while (blocks > 0) {
  172. if (*bitmap++ != ~0UL)
  173. return 1;
  174. blocks -= BITS_PER_LONG;
  175. }
  176. return 0;
  177. }
  178. static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
  179. {
  180. dma_addr_t dma = page->dma;
  181. #ifdef CONFIG_DEBUG_SLAB
  182. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  183. #endif
  184. dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
  185. list_del(&page->page_list);
  186. kfree(page);
  187. }
  188. /**
  189. * dma_pool_destroy - destroys a pool of dma memory blocks.
  190. * @pool: dma pool that will be destroyed
  191. * Context: !in_interrupt()
  192. *
  193. * Caller guarantees that no more memory from the pool is in use,
  194. * and that nothing will try to use the pool after this call.
  195. */
  196. void dma_pool_destroy(struct dma_pool *pool)
  197. {
  198. mutex_lock(&pools_lock);
  199. list_del(&pool->pools);
  200. if (pool->dev && list_empty(&pool->dev->dma_pools))
  201. device_remove_file(pool->dev, &dev_attr_pools);
  202. mutex_unlock(&pools_lock);
  203. while (!list_empty(&pool->page_list)) {
  204. struct dma_page *page;
  205. page = list_entry(pool->page_list.next,
  206. struct dma_page, page_list);
  207. if (is_page_busy(pool->blocks_per_page, page->bitmap)) {
  208. if (pool->dev)
  209. dev_err(pool->dev,
  210. "dma_pool_destroy %s, %p busy\n",
  211. pool->name, page->vaddr);
  212. else
  213. printk(KERN_ERR
  214. "dma_pool_destroy %s, %p busy\n",
  215. pool->name, page->vaddr);
  216. /* leak the still-in-use consistent memory */
  217. list_del(&page->page_list);
  218. kfree(page);
  219. } else
  220. pool_free_page(pool, page);
  221. }
  222. kfree(pool);
  223. }
  224. EXPORT_SYMBOL(dma_pool_destroy);
  225. /**
  226. * dma_pool_alloc - get a block of consistent memory
  227. * @pool: dma pool that will produce the block
  228. * @mem_flags: GFP_* bitmask
  229. * @handle: pointer to dma address of block
  230. *
  231. * This returns the kernel virtual address of a currently unused block,
  232. * and reports its dma address through the handle.
  233. * If such a memory block can't be allocated, null is returned.
  234. */
  235. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  236. dma_addr_t *handle)
  237. {
  238. unsigned long flags;
  239. struct dma_page *page;
  240. int map, block;
  241. size_t offset;
  242. void *retval;
  243. restart:
  244. spin_lock_irqsave(&pool->lock, flags);
  245. list_for_each_entry(page, &pool->page_list, page_list) {
  246. int i;
  247. /* only cachable accesses here ... */
  248. for (map = 0, i = 0;
  249. i < pool->blocks_per_page; i += BITS_PER_LONG, map++) {
  250. if (page->bitmap[map] == 0)
  251. continue;
  252. block = ffz(~page->bitmap[map]);
  253. if ((i + block) < pool->blocks_per_page) {
  254. clear_bit(block, &page->bitmap[map]);
  255. offset = (BITS_PER_LONG * map) + block;
  256. offset *= pool->size;
  257. goto ready;
  258. }
  259. }
  260. }
  261. page = pool_alloc_page(pool, GFP_ATOMIC);
  262. if (!page) {
  263. if (mem_flags & __GFP_WAIT) {
  264. DECLARE_WAITQUEUE(wait, current);
  265. __set_current_state(TASK_INTERRUPTIBLE);
  266. add_wait_queue(&pool->waitq, &wait);
  267. spin_unlock_irqrestore(&pool->lock, flags);
  268. schedule_timeout(POOL_TIMEOUT_JIFFIES);
  269. remove_wait_queue(&pool->waitq, &wait);
  270. goto restart;
  271. }
  272. retval = NULL;
  273. goto done;
  274. }
  275. clear_bit(0, &page->bitmap[0]);
  276. offset = 0;
  277. ready:
  278. page->in_use++;
  279. retval = offset + page->vaddr;
  280. *handle = offset + page->dma;
  281. #ifdef CONFIG_DEBUG_SLAB
  282. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  283. #endif
  284. done:
  285. spin_unlock_irqrestore(&pool->lock, flags);
  286. return retval;
  287. }
  288. EXPORT_SYMBOL(dma_pool_alloc);
  289. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  290. {
  291. unsigned long flags;
  292. struct dma_page *page;
  293. spin_lock_irqsave(&pool->lock, flags);
  294. list_for_each_entry(page, &pool->page_list, page_list) {
  295. if (dma < page->dma)
  296. continue;
  297. if (dma < (page->dma + pool->allocation))
  298. goto done;
  299. }
  300. page = NULL;
  301. done:
  302. spin_unlock_irqrestore(&pool->lock, flags);
  303. return page;
  304. }
  305. /**
  306. * dma_pool_free - put block back into dma pool
  307. * @pool: the dma pool holding the block
  308. * @vaddr: virtual address of block
  309. * @dma: dma address of block
  310. *
  311. * Caller promises neither device nor driver will again touch this block
  312. * unless it is first re-allocated.
  313. */
  314. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  315. {
  316. struct dma_page *page;
  317. unsigned long flags;
  318. int map, block;
  319. page = pool_find_page(pool, dma);
  320. if (!page) {
  321. if (pool->dev)
  322. dev_err(pool->dev,
  323. "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,
  338. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  339. pool->name, vaddr, (unsigned long long)dma);
  340. else
  341. printk(KERN_ERR
  342. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  343. pool->name, vaddr, (unsigned long long)dma);
  344. return;
  345. }
  346. if (page->bitmap[map] & (1UL << block)) {
  347. if (pool->dev)
  348. dev_err(pool->dev,
  349. "dma_pool_free %s, dma %Lx already free\n",
  350. pool->name, (unsigned long long)dma);
  351. else
  352. printk(KERN_ERR
  353. "dma_pool_free %s, dma %Lx already free\n",
  354. pool->name, (unsigned long long)dma);
  355. return;
  356. }
  357. memset(vaddr, POOL_POISON_FREED, pool->size);
  358. #endif
  359. spin_lock_irqsave(&pool->lock, flags);
  360. page->in_use--;
  361. set_bit(block, &page->bitmap[map]);
  362. if (waitqueue_active(&pool->waitq))
  363. wake_up(&pool->waitq);
  364. /*
  365. * Resist a temptation to do
  366. * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  367. * Better have a few empty pages hang around.
  368. */
  369. spin_unlock_irqrestore(&pool->lock, flags);
  370. }
  371. EXPORT_SYMBOL(dma_pool_free);
  372. /*
  373. * Managed DMA pool
  374. */
  375. static void dmam_pool_release(struct device *dev, void *res)
  376. {
  377. struct dma_pool *pool = *(struct dma_pool **)res;
  378. dma_pool_destroy(pool);
  379. }
  380. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  381. {
  382. return *(struct dma_pool **)res == match_data;
  383. }
  384. /**
  385. * dmam_pool_create - Managed dma_pool_create()
  386. * @name: name of pool, for diagnostics
  387. * @dev: device that will be doing the DMA
  388. * @size: size of the blocks in this pool.
  389. * @align: alignment requirement for blocks; must be a power of two
  390. * @allocation: returned blocks won't cross this boundary (or zero)
  391. *
  392. * Managed dma_pool_create(). DMA pool created with this function is
  393. * automatically destroyed on driver detach.
  394. */
  395. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  396. size_t size, size_t align, size_t allocation)
  397. {
  398. struct dma_pool **ptr, *pool;
  399. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  400. if (!ptr)
  401. return NULL;
  402. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  403. if (pool)
  404. devres_add(dev, ptr);
  405. else
  406. devres_free(ptr);
  407. return pool;
  408. }
  409. EXPORT_SYMBOL(dmam_pool_create);
  410. /**
  411. * dmam_pool_destroy - Managed dma_pool_destroy()
  412. * @pool: dma pool that will be destroyed
  413. *
  414. * Managed dma_pool_destroy().
  415. */
  416. void dmam_pool_destroy(struct dma_pool *pool)
  417. {
  418. struct device *dev = pool->dev;
  419. dma_pool_destroy(pool);
  420. WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
  421. }
  422. EXPORT_SYMBOL(dmam_pool_destroy);