dmapool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. spin_lock_irqsave(&pool->lock, flags);
  276. restart:
  277. list_for_each_entry(page, &pool->page_list, page_list) {
  278. if (page->offset < pool->allocation)
  279. goto ready;
  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. ready:
  297. page->in_use++;
  298. offset = page->offset;
  299. page->offset = *(int *)(page->vaddr + offset);
  300. retval = offset + page->vaddr;
  301. *handle = offset + page->dma;
  302. #ifdef DMAPOOL_DEBUG
  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. unsigned int offset;
  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. offset = vaddr - page->vaddr;
  352. #ifdef DMAPOOL_DEBUG
  353. if ((dma - page->dma) != offset) {
  354. if (pool->dev)
  355. dev_err(pool->dev,
  356. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  357. pool->name, vaddr, (unsigned long long)dma);
  358. else
  359. printk(KERN_ERR
  360. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  361. pool->name, vaddr, (unsigned long long)dma);
  362. return;
  363. }
  364. {
  365. unsigned int chain = page->offset;
  366. while (chain < pool->allocation) {
  367. if (chain != offset) {
  368. chain = *(int *)(page->vaddr + chain);
  369. continue;
  370. }
  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. spin_lock_irqsave(&pool->lock, flags);
  385. page->in_use--;
  386. *(int *)vaddr = page->offset;
  387. page->offset = offset;
  388. if (waitqueue_active(&pool->waitq))
  389. wake_up_locked(&pool->waitq);
  390. /*
  391. * Resist a temptation to do
  392. * if (!is_page_busy(page)) pool_free_page(pool, page);
  393. * Better have a few empty pages hang around.
  394. */
  395. spin_unlock_irqrestore(&pool->lock, flags);
  396. }
  397. EXPORT_SYMBOL(dma_pool_free);
  398. /*
  399. * Managed DMA pool
  400. */
  401. static void dmam_pool_release(struct device *dev, void *res)
  402. {
  403. struct dma_pool *pool = *(struct dma_pool **)res;
  404. dma_pool_destroy(pool);
  405. }
  406. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  407. {
  408. return *(struct dma_pool **)res == match_data;
  409. }
  410. /**
  411. * dmam_pool_create - Managed dma_pool_create()
  412. * @name: name of pool, for diagnostics
  413. * @dev: device that will be doing the DMA
  414. * @size: size of the blocks in this pool.
  415. * @align: alignment requirement for blocks; must be a power of two
  416. * @allocation: returned blocks won't cross this boundary (or zero)
  417. *
  418. * Managed dma_pool_create(). DMA pool created with this function is
  419. * automatically destroyed on driver detach.
  420. */
  421. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  422. size_t size, size_t align, size_t allocation)
  423. {
  424. struct dma_pool **ptr, *pool;
  425. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  426. if (!ptr)
  427. return NULL;
  428. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  429. if (pool)
  430. devres_add(dev, ptr);
  431. else
  432. devres_free(ptr);
  433. return pool;
  434. }
  435. EXPORT_SYMBOL(dmam_pool_create);
  436. /**
  437. * dmam_pool_destroy - Managed dma_pool_destroy()
  438. * @pool: dma pool that will be destroyed
  439. *
  440. * Managed dma_pool_destroy().
  441. */
  442. void dmam_pool_destroy(struct dma_pool *pool)
  443. {
  444. struct device *dev = pool->dev;
  445. dma_pool_destroy(pool);
  446. WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
  447. }
  448. EXPORT_SYMBOL(dmam_pool_destroy);