dmapool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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/export.h>
  30. #include <linux/mutex.h>
  31. #include <linux/poison.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/stat.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/string.h>
  37. #include <linux/types.h>
  38. #include <linux/wait.h>
  39. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
  40. #define DMAPOOL_DEBUG 1
  41. #endif
  42. struct dma_pool { /* the pool */
  43. struct list_head page_list;
  44. spinlock_t lock;
  45. size_t size;
  46. struct device *dev;
  47. size_t allocation;
  48. size_t boundary;
  49. char name[32];
  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. static DEFINE_MUTEX(pools_lock);
  60. static ssize_t
  61. show_pools(struct device *dev, struct device_attribute *attr, char *buf)
  62. {
  63. unsigned temp;
  64. unsigned size;
  65. char *next;
  66. struct dma_page *page;
  67. struct dma_pool *pool;
  68. next = buf;
  69. size = PAGE_SIZE;
  70. temp = scnprintf(next, size, "poolinfo - 0.1\n");
  71. size -= temp;
  72. next += temp;
  73. mutex_lock(&pools_lock);
  74. list_for_each_entry(pool, &dev->dma_pools, pools) {
  75. unsigned pages = 0;
  76. unsigned blocks = 0;
  77. spin_lock_irq(&pool->lock);
  78. list_for_each_entry(page, &pool->page_list, page_list) {
  79. pages++;
  80. blocks += page->in_use;
  81. }
  82. spin_unlock_irq(&pool->lock);
  83. /* per-pool info, no real statistics yet */
  84. temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
  85. pool->name, blocks,
  86. pages * (pool->allocation / pool->size),
  87. pool->size, pages);
  88. size -= temp;
  89. next += temp;
  90. }
  91. mutex_unlock(&pools_lock);
  92. return PAGE_SIZE - size;
  93. }
  94. static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
  95. /**
  96. * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
  97. * @name: name of pool, for diagnostics
  98. * @dev: device that will be doing the DMA
  99. * @size: size of the blocks in this pool.
  100. * @align: alignment requirement for blocks; must be a power of two
  101. * @boundary: returned blocks won't cross this power of two boundary
  102. * Context: !in_interrupt()
  103. *
  104. * Returns a dma allocation pool with the requested characteristics, or
  105. * null if one can't be created. Given one of these pools, dma_pool_alloc()
  106. * may be used to allocate memory. Such memory will all have "consistent"
  107. * DMA mappings, accessible by the device and its driver without using
  108. * cache flushing primitives. The actual size of blocks allocated may be
  109. * larger than requested because of alignment.
  110. *
  111. * If @boundary is nonzero, objects returned from dma_pool_alloc() won't
  112. * cross that size boundary. This is useful for devices which have
  113. * addressing restrictions on individual DMA transfers, such as not crossing
  114. * boundaries of 4KBytes.
  115. */
  116. struct dma_pool *dma_pool_create(const char *name, struct device *dev,
  117. size_t size, size_t align, size_t boundary)
  118. {
  119. struct dma_pool *retval;
  120. size_t allocation;
  121. if (align == 0) {
  122. align = 1;
  123. } else if (align & (align - 1)) {
  124. return NULL;
  125. }
  126. if (size == 0) {
  127. return NULL;
  128. } else if (size < 4) {
  129. size = 4;
  130. }
  131. if ((size % align) != 0)
  132. size = ALIGN(size, align);
  133. allocation = max_t(size_t, size, PAGE_SIZE);
  134. if (!boundary) {
  135. boundary = allocation;
  136. } else if ((boundary < size) || (boundary & (boundary - 1))) {
  137. return NULL;
  138. }
  139. retval = kmalloc_node(sizeof(*retval), GFP_KERNEL, dev_to_node(dev));
  140. if (!retval)
  141. return retval;
  142. strlcpy(retval->name, name, sizeof(retval->name));
  143. retval->dev = dev;
  144. INIT_LIST_HEAD(&retval->page_list);
  145. spin_lock_init(&retval->lock);
  146. retval->size = size;
  147. retval->boundary = boundary;
  148. retval->allocation = allocation;
  149. INIT_LIST_HEAD(&retval->pools);
  150. mutex_lock(&pools_lock);
  151. if (list_empty(&dev->dma_pools) &&
  152. device_create_file(dev, &dev_attr_pools)) {
  153. kfree(retval);
  154. return NULL;
  155. } else
  156. list_add(&retval->pools, &dev->dma_pools);
  157. mutex_unlock(&pools_lock);
  158. return retval;
  159. }
  160. EXPORT_SYMBOL(dma_pool_create);
  161. static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
  162. {
  163. unsigned int offset = 0;
  164. unsigned int next_boundary = pool->boundary;
  165. do {
  166. unsigned int next = offset + pool->size;
  167. if (unlikely((next + pool->size) >= next_boundary)) {
  168. next = next_boundary;
  169. next_boundary += pool->boundary;
  170. }
  171. *(int *)(page->vaddr + offset) = next;
  172. offset = next;
  173. } while (offset < pool->allocation);
  174. }
  175. static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
  176. {
  177. struct dma_page *page;
  178. page = kmalloc(sizeof(*page), mem_flags);
  179. if (!page)
  180. return NULL;
  181. page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
  182. &page->dma, mem_flags);
  183. if (page->vaddr) {
  184. #ifdef DMAPOOL_DEBUG
  185. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  186. #endif
  187. pool_initialise_page(pool, page);
  188. page->in_use = 0;
  189. page->offset = 0;
  190. } else {
  191. kfree(page);
  192. page = NULL;
  193. }
  194. return page;
  195. }
  196. static inline int is_page_busy(struct dma_page *page)
  197. {
  198. return page->in_use != 0;
  199. }
  200. static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
  201. {
  202. dma_addr_t dma = page->dma;
  203. #ifdef DMAPOOL_DEBUG
  204. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  205. #endif
  206. dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
  207. list_del(&page->page_list);
  208. kfree(page);
  209. }
  210. /**
  211. * dma_pool_destroy - destroys a pool of dma memory blocks.
  212. * @pool: dma pool that will be destroyed
  213. * Context: !in_interrupt()
  214. *
  215. * Caller guarantees that no more memory from the pool is in use,
  216. * and that nothing will try to use the pool after this call.
  217. */
  218. void dma_pool_destroy(struct dma_pool *pool)
  219. {
  220. mutex_lock(&pools_lock);
  221. list_del(&pool->pools);
  222. if (pool->dev && list_empty(&pool->dev->dma_pools))
  223. device_remove_file(pool->dev, &dev_attr_pools);
  224. mutex_unlock(&pools_lock);
  225. while (!list_empty(&pool->page_list)) {
  226. struct dma_page *page;
  227. page = list_entry(pool->page_list.next,
  228. struct dma_page, page_list);
  229. if (is_page_busy(page)) {
  230. if (pool->dev)
  231. dev_err(pool->dev,
  232. "dma_pool_destroy %s, %p busy\n",
  233. pool->name, page->vaddr);
  234. else
  235. printk(KERN_ERR
  236. "dma_pool_destroy %s, %p busy\n",
  237. pool->name, page->vaddr);
  238. /* leak the still-in-use consistent memory */
  239. list_del(&page->page_list);
  240. kfree(page);
  241. } else
  242. pool_free_page(pool, page);
  243. }
  244. kfree(pool);
  245. }
  246. EXPORT_SYMBOL(dma_pool_destroy);
  247. /**
  248. * dma_pool_alloc - get a block of consistent memory
  249. * @pool: dma pool that will produce the block
  250. * @mem_flags: GFP_* bitmask
  251. * @handle: pointer to dma address of block
  252. *
  253. * This returns the kernel virtual address of a currently unused block,
  254. * and reports its dma address through the handle.
  255. * If such a memory block can't be allocated, %NULL is returned.
  256. */
  257. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  258. dma_addr_t *handle)
  259. {
  260. unsigned long flags;
  261. struct dma_page *page;
  262. size_t offset;
  263. void *retval;
  264. might_sleep_if(mem_flags & __GFP_WAIT);
  265. spin_lock_irqsave(&pool->lock, flags);
  266. list_for_each_entry(page, &pool->page_list, page_list) {
  267. if (page->offset < pool->allocation)
  268. goto ready;
  269. }
  270. /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
  271. spin_unlock_irqrestore(&pool->lock, flags);
  272. page = pool_alloc_page(pool, mem_flags);
  273. if (!page)
  274. return NULL;
  275. spin_lock_irqsave(&pool->lock, flags);
  276. list_add(&page->page_list, &pool->page_list);
  277. ready:
  278. page->in_use++;
  279. offset = page->offset;
  280. page->offset = *(int *)(page->vaddr + offset);
  281. retval = offset + page->vaddr;
  282. *handle = offset + page->dma;
  283. #ifdef DMAPOOL_DEBUG
  284. {
  285. int i;
  286. u8 *data = retval;
  287. /* page->offset is stored in first 4 bytes */
  288. for (i = sizeof(page->offset); i < pool->size; i++) {
  289. if (data[i] == POOL_POISON_FREED)
  290. continue;
  291. if (pool->dev)
  292. dev_err(pool->dev,
  293. "dma_pool_alloc %s, %p (corrupted)\n",
  294. pool->name, retval);
  295. else
  296. pr_err("dma_pool_alloc %s, %p (corrupted)\n",
  297. pool->name, retval);
  298. /*
  299. * Dump the first 4 bytes even if they are not
  300. * POOL_POISON_FREED
  301. */
  302. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
  303. data, pool->size, 1);
  304. break;
  305. }
  306. }
  307. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  308. #endif
  309. spin_unlock_irqrestore(&pool->lock, flags);
  310. return retval;
  311. }
  312. EXPORT_SYMBOL(dma_pool_alloc);
  313. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  314. {
  315. struct dma_page *page;
  316. list_for_each_entry(page, &pool->page_list, page_list) {
  317. if (dma < page->dma)
  318. continue;
  319. if (dma < (page->dma + pool->allocation))
  320. return page;
  321. }
  322. return NULL;
  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. spin_lock_irqsave(&pool->lock, flags);
  339. page = pool_find_page(pool, dma);
  340. if (!page) {
  341. spin_unlock_irqrestore(&pool->lock, flags);
  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. spin_unlock_irqrestore(&pool->lock, flags);
  355. if (pool->dev)
  356. dev_err(pool->dev,
  357. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  358. pool->name, vaddr, (unsigned long long)dma);
  359. else
  360. printk(KERN_ERR
  361. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  362. pool->name, vaddr, (unsigned long long)dma);
  363. return;
  364. }
  365. {
  366. unsigned int chain = page->offset;
  367. while (chain < pool->allocation) {
  368. if (chain != offset) {
  369. chain = *(int *)(page->vaddr + chain);
  370. continue;
  371. }
  372. spin_unlock_irqrestore(&pool->lock, flags);
  373. if (pool->dev)
  374. dev_err(pool->dev, "dma_pool_free %s, dma %Lx "
  375. "already free\n", pool->name,
  376. (unsigned long long)dma);
  377. else
  378. printk(KERN_ERR "dma_pool_free %s, dma %Lx "
  379. "already free\n", pool->name,
  380. (unsigned long long)dma);
  381. return;
  382. }
  383. }
  384. memset(vaddr, POOL_POISON_FREED, pool->size);
  385. #endif
  386. page->in_use--;
  387. *(int *)vaddr = page->offset;
  388. page->offset = offset;
  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. WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
  445. }
  446. EXPORT_SYMBOL(dmam_pool_destroy);