dmapool.c 13 KB

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