dmapool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * DMA Pool allocator
  3. *
  4. * Copyright 2001 David Brownell
  5. * Copyright 2007 Intel Corporation
  6. * Author: Matthew Wilcox <willy@linux.intel.com>
  7. *
  8. * This software may be redistributed and/or modified under the terms of
  9. * the GNU General Public License ("GPL") version 2 as published by the
  10. * Free Software Foundation.
  11. *
  12. * This allocator returns small blocks of a given size which are DMA-able by
  13. * the given device. It uses the dma_alloc_coherent page allocator to get
  14. * new pages, then splits them up into blocks of the required size.
  15. * Many older drivers still have their own code to do this.
  16. *
  17. * The current design of this allocator is fairly simple. The pool is
  18. * represented by the 'struct dma_pool' which keeps a doubly-linked list of
  19. * allocated pages. Each page in the page_list is split into blocks of at
  20. * least 'size' bytes.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/kernel.h>
  26. #include <linux/list.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <linux/poison.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/string.h>
  34. #include <linux/types.h>
  35. #include <linux/wait.h>
  36. struct dma_pool { /* the pool */
  37. struct list_head page_list;
  38. spinlock_t lock;
  39. size_t blocks_per_page;
  40. size_t size;
  41. struct device *dev;
  42. size_t allocation;
  43. char name[32];
  44. wait_queue_head_t waitq;
  45. struct list_head pools;
  46. };
  47. struct dma_page { /* cacheable header for 'allocation' bytes */
  48. struct list_head page_list;
  49. void *vaddr;
  50. dma_addr_t dma;
  51. unsigned in_use;
  52. unsigned long bitmap[0];
  53. };
  54. #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
  55. static DEFINE_MUTEX(pools_lock);
  56. static ssize_t
  57. show_pools(struct device *dev, struct device_attribute *attr, char *buf)
  58. {
  59. unsigned temp;
  60. unsigned size;
  61. char *next;
  62. struct dma_page *page;
  63. struct dma_pool *pool;
  64. next = buf;
  65. size = PAGE_SIZE;
  66. temp = scnprintf(next, size, "poolinfo - 0.1\n");
  67. size -= temp;
  68. next += temp;
  69. mutex_lock(&pools_lock);
  70. list_for_each_entry(pool, &dev->dma_pools, pools) {
  71. unsigned pages = 0;
  72. unsigned blocks = 0;
  73. list_for_each_entry(page, &pool->page_list, page_list) {
  74. pages++;
  75. blocks += page->in_use;
  76. }
  77. /* per-pool info, no real statistics yet */
  78. temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
  79. pool->name,
  80. blocks, pages * pool->blocks_per_page,
  81. pool->size, pages);
  82. size -= temp;
  83. next += temp;
  84. }
  85. mutex_unlock(&pools_lock);
  86. return PAGE_SIZE - size;
  87. }
  88. static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
  89. /**
  90. * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
  91. * @name: name of pool, for diagnostics
  92. * @dev: device that will be doing the DMA
  93. * @size: size of the blocks in this pool.
  94. * @align: alignment requirement for blocks; must be a power of two
  95. * @allocation: returned blocks won't cross this boundary (or zero)
  96. * Context: !in_interrupt()
  97. *
  98. * Returns a dma allocation pool with the requested characteristics, or
  99. * null if one can't be created. Given one of these pools, dma_pool_alloc()
  100. * may be used to allocate memory. Such memory will all have "consistent"
  101. * DMA mappings, accessible by the device and its driver without using
  102. * cache flushing primitives. The actual size of blocks allocated may be
  103. * larger than requested because of alignment.
  104. *
  105. * If allocation is nonzero, objects returned from dma_pool_alloc() won't
  106. * cross that size boundary. This is useful for devices which have
  107. * addressing restrictions on individual DMA transfers, such as not crossing
  108. * boundaries of 4KBytes.
  109. */
  110. struct dma_pool *dma_pool_create(const char *name, struct device *dev,
  111. size_t size, size_t align, size_t allocation)
  112. {
  113. struct dma_pool *retval;
  114. if (align == 0) {
  115. align = 1;
  116. } else if (align & (align - 1)) {
  117. return NULL;
  118. }
  119. if (size == 0)
  120. return NULL;
  121. if ((size % align) != 0)
  122. size = ALIGN(size, align);
  123. if (allocation == 0) {
  124. if (PAGE_SIZE < size)
  125. allocation = size;
  126. else
  127. allocation = PAGE_SIZE;
  128. /* FIXME: round up for less fragmentation */
  129. } else if (allocation < size)
  130. return NULL;
  131. if (!
  132. (retval =
  133. kmalloc_node(sizeof *retval, GFP_KERNEL, dev_to_node(dev))))
  134. return retval;
  135. strlcpy(retval->name, name, sizeof retval->name);
  136. retval->dev = dev;
  137. INIT_LIST_HEAD(&retval->page_list);
  138. spin_lock_init(&retval->lock);
  139. retval->size = size;
  140. retval->allocation = allocation;
  141. retval->blocks_per_page = allocation / size;
  142. init_waitqueue_head(&retval->waitq);
  143. if (dev) {
  144. int ret;
  145. mutex_lock(&pools_lock);
  146. if (list_empty(&dev->dma_pools))
  147. ret = device_create_file(dev, &dev_attr_pools);
  148. else
  149. ret = 0;
  150. /* note: not currently insisting "name" be unique */
  151. if (!ret)
  152. list_add(&retval->pools, &dev->dma_pools);
  153. else {
  154. kfree(retval);
  155. retval = NULL;
  156. }
  157. mutex_unlock(&pools_lock);
  158. } else
  159. INIT_LIST_HEAD(&retval->pools);
  160. return retval;
  161. }
  162. EXPORT_SYMBOL(dma_pool_create);
  163. static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
  164. {
  165. struct dma_page *page;
  166. int mapsize;
  167. mapsize = pool->blocks_per_page;
  168. mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
  169. mapsize *= sizeof(long);
  170. page = kmalloc(mapsize + sizeof *page, mem_flags);
  171. if (!page)
  172. return NULL;
  173. page->vaddr = dma_alloc_coherent(pool->dev,
  174. pool->allocation,
  175. &page->dma, mem_flags);
  176. if (page->vaddr) {
  177. memset(page->bitmap, 0xff, mapsize); /* bit set == free */
  178. #ifdef CONFIG_DEBUG_SLAB
  179. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  180. #endif
  181. list_add(&page->page_list, &pool->page_list);
  182. page->in_use = 0;
  183. } else {
  184. kfree(page);
  185. page = NULL;
  186. }
  187. return page;
  188. }
  189. static inline int is_page_busy(int blocks, unsigned long *bitmap)
  190. {
  191. while (blocks > 0) {
  192. if (*bitmap++ != ~0UL)
  193. return 1;
  194. blocks -= BITS_PER_LONG;
  195. }
  196. return 0;
  197. }
  198. static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
  199. {
  200. dma_addr_t dma = page->dma;
  201. #ifdef CONFIG_DEBUG_SLAB
  202. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  203. #endif
  204. dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
  205. list_del(&page->page_list);
  206. kfree(page);
  207. }
  208. /**
  209. * dma_pool_destroy - destroys a pool of dma memory blocks.
  210. * @pool: dma pool that will be destroyed
  211. * Context: !in_interrupt()
  212. *
  213. * Caller guarantees that no more memory from the pool is in use,
  214. * and that nothing will try to use the pool after this call.
  215. */
  216. void dma_pool_destroy(struct dma_pool *pool)
  217. {
  218. mutex_lock(&pools_lock);
  219. list_del(&pool->pools);
  220. if (pool->dev && list_empty(&pool->dev->dma_pools))
  221. device_remove_file(pool->dev, &dev_attr_pools);
  222. mutex_unlock(&pools_lock);
  223. while (!list_empty(&pool->page_list)) {
  224. struct dma_page *page;
  225. page = list_entry(pool->page_list.next,
  226. struct dma_page, page_list);
  227. if (is_page_busy(pool->blocks_per_page, page->bitmap)) {
  228. if (pool->dev)
  229. dev_err(pool->dev,
  230. "dma_pool_destroy %s, %p busy\n",
  231. pool->name, page->vaddr);
  232. else
  233. printk(KERN_ERR
  234. "dma_pool_destroy %s, %p busy\n",
  235. pool->name, page->vaddr);
  236. /* leak the still-in-use consistent memory */
  237. list_del(&page->page_list);
  238. kfree(page);
  239. } else
  240. pool_free_page(pool, page);
  241. }
  242. kfree(pool);
  243. }
  244. EXPORT_SYMBOL(dma_pool_destroy);
  245. /**
  246. * dma_pool_alloc - get a block of consistent memory
  247. * @pool: dma pool that will produce the block
  248. * @mem_flags: GFP_* bitmask
  249. * @handle: pointer to dma address of block
  250. *
  251. * This returns the kernel virtual address of a currently unused block,
  252. * and reports its dma address through the handle.
  253. * If such a memory block can't be allocated, %NULL is returned.
  254. */
  255. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  256. dma_addr_t *handle)
  257. {
  258. unsigned long flags;
  259. struct dma_page *page;
  260. int map, block;
  261. size_t offset;
  262. void *retval;
  263. spin_lock_irqsave(&pool->lock, flags);
  264. restart:
  265. list_for_each_entry(page, &pool->page_list, page_list) {
  266. int i;
  267. /* only cachable accesses here ... */
  268. for (map = 0, i = 0;
  269. i < pool->blocks_per_page; i += BITS_PER_LONG, map++) {
  270. if (page->bitmap[map] == 0)
  271. continue;
  272. block = ffz(~page->bitmap[map]);
  273. if ((i + block) < pool->blocks_per_page) {
  274. clear_bit(block, &page->bitmap[map]);
  275. offset = (BITS_PER_LONG * map) + block;
  276. offset *= pool->size;
  277. goto ready;
  278. }
  279. }
  280. }
  281. page = pool_alloc_page(pool, GFP_ATOMIC);
  282. if (!page) {
  283. if (mem_flags & __GFP_WAIT) {
  284. DECLARE_WAITQUEUE(wait, current);
  285. __set_current_state(TASK_INTERRUPTIBLE);
  286. __add_wait_queue(&pool->waitq, &wait);
  287. spin_unlock_irqrestore(&pool->lock, flags);
  288. schedule_timeout(POOL_TIMEOUT_JIFFIES);
  289. spin_lock_irqsave(&pool->lock, flags);
  290. __remove_wait_queue(&pool->waitq, &wait);
  291. goto restart;
  292. }
  293. retval = NULL;
  294. goto done;
  295. }
  296. clear_bit(0, &page->bitmap[0]);
  297. offset = 0;
  298. ready:
  299. page->in_use++;
  300. retval = offset + page->vaddr;
  301. *handle = offset + page->dma;
  302. #ifdef CONFIG_DEBUG_SLAB
  303. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  304. #endif
  305. done:
  306. spin_unlock_irqrestore(&pool->lock, flags);
  307. return retval;
  308. }
  309. EXPORT_SYMBOL(dma_pool_alloc);
  310. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  311. {
  312. unsigned long flags;
  313. struct dma_page *page;
  314. spin_lock_irqsave(&pool->lock, flags);
  315. list_for_each_entry(page, &pool->page_list, page_list) {
  316. if (dma < page->dma)
  317. continue;
  318. if (dma < (page->dma + pool->allocation))
  319. goto done;
  320. }
  321. page = NULL;
  322. done:
  323. spin_unlock_irqrestore(&pool->lock, flags);
  324. return page;
  325. }
  326. /**
  327. * dma_pool_free - put block back into dma pool
  328. * @pool: the dma pool holding the block
  329. * @vaddr: virtual address of block
  330. * @dma: dma address of block
  331. *
  332. * Caller promises neither device nor driver will again touch this block
  333. * unless it is first re-allocated.
  334. */
  335. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  336. {
  337. struct dma_page *page;
  338. unsigned long flags;
  339. int map, block;
  340. page = pool_find_page(pool, dma);
  341. if (!page) {
  342. if (pool->dev)
  343. dev_err(pool->dev,
  344. "dma_pool_free %s, %p/%lx (bad dma)\n",
  345. pool->name, vaddr, (unsigned long)dma);
  346. else
  347. printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  348. pool->name, vaddr, (unsigned long)dma);
  349. return;
  350. }
  351. block = dma - page->dma;
  352. block /= pool->size;
  353. map = block / BITS_PER_LONG;
  354. block %= BITS_PER_LONG;
  355. #ifdef CONFIG_DEBUG_SLAB
  356. if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
  357. if (pool->dev)
  358. dev_err(pool->dev,
  359. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  360. pool->name, vaddr, (unsigned long long)dma);
  361. else
  362. printk(KERN_ERR
  363. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  364. pool->name, vaddr, (unsigned long long)dma);
  365. return;
  366. }
  367. if (page->bitmap[map] & (1UL << block)) {
  368. if (pool->dev)
  369. dev_err(pool->dev,
  370. "dma_pool_free %s, dma %Lx already free\n",
  371. pool->name, (unsigned long long)dma);
  372. else
  373. printk(KERN_ERR
  374. "dma_pool_free %s, dma %Lx already free\n",
  375. pool->name, (unsigned long long)dma);
  376. return;
  377. }
  378. memset(vaddr, POOL_POISON_FREED, pool->size);
  379. #endif
  380. spin_lock_irqsave(&pool->lock, flags);
  381. page->in_use--;
  382. set_bit(block, &page->bitmap[map]);
  383. if (waitqueue_active(&pool->waitq))
  384. wake_up_locked(&pool->waitq);
  385. /*
  386. * Resist a temptation to do
  387. * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  388. * Better have a few empty pages hang around.
  389. */
  390. spin_unlock_irqrestore(&pool->lock, flags);
  391. }
  392. EXPORT_SYMBOL(dma_pool_free);
  393. /*
  394. * Managed DMA pool
  395. */
  396. static void dmam_pool_release(struct device *dev, void *res)
  397. {
  398. struct dma_pool *pool = *(struct dma_pool **)res;
  399. dma_pool_destroy(pool);
  400. }
  401. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  402. {
  403. return *(struct dma_pool **)res == match_data;
  404. }
  405. /**
  406. * dmam_pool_create - Managed dma_pool_create()
  407. * @name: name of pool, for diagnostics
  408. * @dev: device that will be doing the DMA
  409. * @size: size of the blocks in this pool.
  410. * @align: alignment requirement for blocks; must be a power of two
  411. * @allocation: returned blocks won't cross this boundary (or zero)
  412. *
  413. * Managed dma_pool_create(). DMA pool created with this function is
  414. * automatically destroyed on driver detach.
  415. */
  416. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  417. size_t size, size_t align, size_t allocation)
  418. {
  419. struct dma_pool **ptr, *pool;
  420. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  421. if (!ptr)
  422. return NULL;
  423. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  424. if (pool)
  425. devres_add(dev, ptr);
  426. else
  427. devres_free(ptr);
  428. return pool;
  429. }
  430. EXPORT_SYMBOL(dmam_pool_create);
  431. /**
  432. * dmam_pool_destroy - Managed dma_pool_destroy()
  433. * @pool: dma pool that will be destroyed
  434. *
  435. * Managed dma_pool_destroy().
  436. */
  437. void dmam_pool_destroy(struct dma_pool *pool)
  438. {
  439. struct device *dev = pool->dev;
  440. dma_pool_destroy(pool);
  441. WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
  442. }
  443. EXPORT_SYMBOL(dmam_pool_destroy);