dmapool.c 13 KB

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