dmapool.c 13 KB

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