dmapool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 DEFINE_MUTEX(pools_reg_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. bool empty = false;
  123. if (align == 0)
  124. align = 1;
  125. else if (align & (align - 1))
  126. return NULL;
  127. if (size == 0)
  128. return NULL;
  129. else if (size < 4)
  130. size = 4;
  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. 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_LIST_HEAD(&retval->pools);
  149. /*
  150. * pools_lock ensures that the ->dma_pools list does not get corrupted.
  151. * pools_reg_lock ensures that there is not a race between
  152. * dma_pool_create() and dma_pool_destroy() or within dma_pool_create()
  153. * when the first invocation of dma_pool_create() failed on
  154. * device_create_file() and the second assumes that it has been done (I
  155. * know it is a short window).
  156. */
  157. mutex_lock(&pools_reg_lock);
  158. mutex_lock(&pools_lock);
  159. if (list_empty(&dev->dma_pools))
  160. empty = true;
  161. list_add(&retval->pools, &dev->dma_pools);
  162. mutex_unlock(&pools_lock);
  163. if (empty) {
  164. int err;
  165. err = device_create_file(dev, &dev_attr_pools);
  166. if (err) {
  167. mutex_lock(&pools_lock);
  168. list_del(&retval->pools);
  169. mutex_unlock(&pools_lock);
  170. mutex_unlock(&pools_reg_lock);
  171. kfree(retval);
  172. return NULL;
  173. }
  174. }
  175. mutex_unlock(&pools_reg_lock);
  176. return retval;
  177. }
  178. EXPORT_SYMBOL(dma_pool_create);
  179. static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
  180. {
  181. unsigned int offset = 0;
  182. unsigned int next_boundary = pool->boundary;
  183. do {
  184. unsigned int next = offset + pool->size;
  185. if (unlikely((next + pool->size) >= next_boundary)) {
  186. next = next_boundary;
  187. next_boundary += pool->boundary;
  188. }
  189. *(int *)(page->vaddr + offset) = next;
  190. offset = next;
  191. } while (offset < pool->allocation);
  192. }
  193. static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
  194. {
  195. struct dma_page *page;
  196. page = kmalloc(sizeof(*page), mem_flags);
  197. if (!page)
  198. return NULL;
  199. page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
  200. &page->dma, mem_flags);
  201. if (page->vaddr) {
  202. #ifdef DMAPOOL_DEBUG
  203. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  204. #endif
  205. pool_initialise_page(pool, page);
  206. page->in_use = 0;
  207. page->offset = 0;
  208. } else {
  209. kfree(page);
  210. page = NULL;
  211. }
  212. return page;
  213. }
  214. static inline int is_page_busy(struct dma_page *page)
  215. {
  216. return page->in_use != 0;
  217. }
  218. static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
  219. {
  220. dma_addr_t dma = page->dma;
  221. #ifdef DMAPOOL_DEBUG
  222. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  223. #endif
  224. dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
  225. list_del(&page->page_list);
  226. kfree(page);
  227. }
  228. /**
  229. * dma_pool_destroy - destroys a pool of dma memory blocks.
  230. * @pool: dma pool that will be destroyed
  231. * Context: !in_interrupt()
  232. *
  233. * Caller guarantees that no more memory from the pool is in use,
  234. * and that nothing will try to use the pool after this call.
  235. */
  236. void dma_pool_destroy(struct dma_pool *pool)
  237. {
  238. bool empty = false;
  239. mutex_lock(&pools_reg_lock);
  240. mutex_lock(&pools_lock);
  241. list_del(&pool->pools);
  242. if (pool->dev && list_empty(&pool->dev->dma_pools))
  243. empty = true;
  244. mutex_unlock(&pools_lock);
  245. if (empty)
  246. device_remove_file(pool->dev, &dev_attr_pools);
  247. mutex_unlock(&pools_reg_lock);
  248. while (!list_empty(&pool->page_list)) {
  249. struct dma_page *page;
  250. page = list_entry(pool->page_list.next,
  251. struct dma_page, page_list);
  252. if (is_page_busy(page)) {
  253. if (pool->dev)
  254. dev_err(pool->dev,
  255. "dma_pool_destroy %s, %p busy\n",
  256. pool->name, page->vaddr);
  257. else
  258. printk(KERN_ERR
  259. "dma_pool_destroy %s, %p busy\n",
  260. pool->name, page->vaddr);
  261. /* leak the still-in-use consistent memory */
  262. list_del(&page->page_list);
  263. kfree(page);
  264. } else
  265. pool_free_page(pool, page);
  266. }
  267. kfree(pool);
  268. }
  269. EXPORT_SYMBOL(dma_pool_destroy);
  270. /**
  271. * dma_pool_alloc - get a block of consistent memory
  272. * @pool: dma pool that will produce the block
  273. * @mem_flags: GFP_* bitmask
  274. * @handle: pointer to dma address of block
  275. *
  276. * This returns the kernel virtual address of a currently unused block,
  277. * and reports its dma address through the handle.
  278. * If such a memory block can't be allocated, %NULL is returned.
  279. */
  280. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  281. dma_addr_t *handle)
  282. {
  283. unsigned long flags;
  284. struct dma_page *page;
  285. size_t offset;
  286. void *retval;
  287. might_sleep_if(mem_flags & __GFP_WAIT);
  288. spin_lock_irqsave(&pool->lock, flags);
  289. list_for_each_entry(page, &pool->page_list, page_list) {
  290. if (page->offset < pool->allocation)
  291. goto ready;
  292. }
  293. /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
  294. spin_unlock_irqrestore(&pool->lock, flags);
  295. page = pool_alloc_page(pool, mem_flags);
  296. if (!page)
  297. return NULL;
  298. spin_lock_irqsave(&pool->lock, flags);
  299. list_add(&page->page_list, &pool->page_list);
  300. ready:
  301. page->in_use++;
  302. offset = page->offset;
  303. page->offset = *(int *)(page->vaddr + offset);
  304. retval = offset + page->vaddr;
  305. *handle = offset + page->dma;
  306. #ifdef DMAPOOL_DEBUG
  307. {
  308. int i;
  309. u8 *data = retval;
  310. /* page->offset is stored in first 4 bytes */
  311. for (i = sizeof(page->offset); i < pool->size; i++) {
  312. if (data[i] == POOL_POISON_FREED)
  313. continue;
  314. if (pool->dev)
  315. dev_err(pool->dev,
  316. "dma_pool_alloc %s, %p (corrupted)\n",
  317. pool->name, retval);
  318. else
  319. pr_err("dma_pool_alloc %s, %p (corrupted)\n",
  320. pool->name, retval);
  321. /*
  322. * Dump the first 4 bytes even if they are not
  323. * POOL_POISON_FREED
  324. */
  325. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
  326. data, pool->size, 1);
  327. break;
  328. }
  329. }
  330. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  331. #endif
  332. spin_unlock_irqrestore(&pool->lock, flags);
  333. return retval;
  334. }
  335. EXPORT_SYMBOL(dma_pool_alloc);
  336. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  337. {
  338. struct dma_page *page;
  339. list_for_each_entry(page, &pool->page_list, page_list) {
  340. if (dma < page->dma)
  341. continue;
  342. if (dma < (page->dma + pool->allocation))
  343. return page;
  344. }
  345. return NULL;
  346. }
  347. /**
  348. * dma_pool_free - put block back into dma pool
  349. * @pool: the dma pool holding the block
  350. * @vaddr: virtual address of block
  351. * @dma: dma address of block
  352. *
  353. * Caller promises neither device nor driver will again touch this block
  354. * unless it is first re-allocated.
  355. */
  356. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  357. {
  358. struct dma_page *page;
  359. unsigned long flags;
  360. unsigned int offset;
  361. spin_lock_irqsave(&pool->lock, flags);
  362. page = pool_find_page(pool, dma);
  363. if (!page) {
  364. spin_unlock_irqrestore(&pool->lock, flags);
  365. if (pool->dev)
  366. dev_err(pool->dev,
  367. "dma_pool_free %s, %p/%lx (bad dma)\n",
  368. pool->name, vaddr, (unsigned long)dma);
  369. else
  370. printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  371. pool->name, vaddr, (unsigned long)dma);
  372. return;
  373. }
  374. offset = vaddr - page->vaddr;
  375. #ifdef DMAPOOL_DEBUG
  376. if ((dma - page->dma) != offset) {
  377. spin_unlock_irqrestore(&pool->lock, flags);
  378. if (pool->dev)
  379. dev_err(pool->dev,
  380. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  381. pool->name, vaddr, (unsigned long long)dma);
  382. else
  383. printk(KERN_ERR
  384. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  385. pool->name, vaddr, (unsigned long long)dma);
  386. return;
  387. }
  388. {
  389. unsigned int chain = page->offset;
  390. while (chain < pool->allocation) {
  391. if (chain != offset) {
  392. chain = *(int *)(page->vaddr + chain);
  393. continue;
  394. }
  395. spin_unlock_irqrestore(&pool->lock, flags);
  396. if (pool->dev)
  397. dev_err(pool->dev, "dma_pool_free %s, dma %Lx "
  398. "already free\n", pool->name,
  399. (unsigned long long)dma);
  400. else
  401. printk(KERN_ERR "dma_pool_free %s, dma %Lx "
  402. "already free\n", pool->name,
  403. (unsigned long long)dma);
  404. return;
  405. }
  406. }
  407. memset(vaddr, POOL_POISON_FREED, pool->size);
  408. #endif
  409. page->in_use--;
  410. *(int *)vaddr = page->offset;
  411. page->offset = offset;
  412. /*
  413. * Resist a temptation to do
  414. * if (!is_page_busy(page)) pool_free_page(pool, page);
  415. * Better have a few empty pages hang around.
  416. */
  417. spin_unlock_irqrestore(&pool->lock, flags);
  418. }
  419. EXPORT_SYMBOL(dma_pool_free);
  420. /*
  421. * Managed DMA pool
  422. */
  423. static void dmam_pool_release(struct device *dev, void *res)
  424. {
  425. struct dma_pool *pool = *(struct dma_pool **)res;
  426. dma_pool_destroy(pool);
  427. }
  428. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  429. {
  430. return *(struct dma_pool **)res == match_data;
  431. }
  432. /**
  433. * dmam_pool_create - Managed dma_pool_create()
  434. * @name: name of pool, for diagnostics
  435. * @dev: device that will be doing the DMA
  436. * @size: size of the blocks in this pool.
  437. * @align: alignment requirement for blocks; must be a power of two
  438. * @allocation: returned blocks won't cross this boundary (or zero)
  439. *
  440. * Managed dma_pool_create(). DMA pool created with this function is
  441. * automatically destroyed on driver detach.
  442. */
  443. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  444. size_t size, size_t align, size_t allocation)
  445. {
  446. struct dma_pool **ptr, *pool;
  447. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  448. if (!ptr)
  449. return NULL;
  450. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  451. if (pool)
  452. devres_add(dev, ptr);
  453. else
  454. devres_free(ptr);
  455. return pool;
  456. }
  457. EXPORT_SYMBOL(dmam_pool_create);
  458. /**
  459. * dmam_pool_destroy - Managed dma_pool_destroy()
  460. * @pool: dma pool that will be destroyed
  461. *
  462. * Managed dma_pool_destroy().
  463. */
  464. void dmam_pool_destroy(struct dma_pool *pool)
  465. {
  466. struct device *dev = pool->dev;
  467. WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
  468. }
  469. EXPORT_SYMBOL(dmam_pool_destroy);