dmapool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 bool 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. if (unlikely(!pool))
  240. return;
  241. mutex_lock(&pools_reg_lock);
  242. mutex_lock(&pools_lock);
  243. list_del(&pool->pools);
  244. if (pool->dev && list_empty(&pool->dev->dma_pools))
  245. empty = true;
  246. mutex_unlock(&pools_lock);
  247. if (empty)
  248. device_remove_file(pool->dev, &dev_attr_pools);
  249. mutex_unlock(&pools_reg_lock);
  250. while (!list_empty(&pool->page_list)) {
  251. struct dma_page *page;
  252. page = list_entry(pool->page_list.next,
  253. struct dma_page, page_list);
  254. if (is_page_busy(page)) {
  255. if (pool->dev)
  256. dev_err(pool->dev,
  257. "dma_pool_destroy %s, %p busy\n",
  258. pool->name, page->vaddr);
  259. else
  260. printk(KERN_ERR
  261. "dma_pool_destroy %s, %p busy\n",
  262. pool->name, page->vaddr);
  263. /* leak the still-in-use consistent memory */
  264. list_del(&page->page_list);
  265. kfree(page);
  266. } else
  267. pool_free_page(pool, page);
  268. }
  269. kfree(pool);
  270. }
  271. EXPORT_SYMBOL(dma_pool_destroy);
  272. /**
  273. * dma_pool_alloc - get a block of consistent memory
  274. * @pool: dma pool that will produce the block
  275. * @mem_flags: GFP_* bitmask
  276. * @handle: pointer to dma address of block
  277. *
  278. * This returns the kernel virtual address of a currently unused block,
  279. * and reports its dma address through the handle.
  280. * If such a memory block can't be allocated, %NULL is returned.
  281. */
  282. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  283. dma_addr_t *handle)
  284. {
  285. unsigned long flags;
  286. struct dma_page *page;
  287. size_t offset;
  288. void *retval;
  289. might_sleep_if(gfpflags_allow_blocking(mem_flags));
  290. spin_lock_irqsave(&pool->lock, flags);
  291. list_for_each_entry(page, &pool->page_list, page_list) {
  292. if (page->offset < pool->allocation)
  293. goto ready;
  294. }
  295. /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
  296. spin_unlock_irqrestore(&pool->lock, flags);
  297. page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO));
  298. if (!page)
  299. return NULL;
  300. spin_lock_irqsave(&pool->lock, flags);
  301. list_add(&page->page_list, &pool->page_list);
  302. ready:
  303. page->in_use++;
  304. offset = page->offset;
  305. page->offset = *(int *)(page->vaddr + offset);
  306. retval = offset + page->vaddr;
  307. *handle = offset + page->dma;
  308. #ifdef DMAPOOL_DEBUG
  309. {
  310. int i;
  311. u8 *data = retval;
  312. /* page->offset is stored in first 4 bytes */
  313. for (i = sizeof(page->offset); i < pool->size; i++) {
  314. if (data[i] == POOL_POISON_FREED)
  315. continue;
  316. if (pool->dev)
  317. dev_err(pool->dev,
  318. "dma_pool_alloc %s, %p (corrupted)\n",
  319. pool->name, retval);
  320. else
  321. pr_err("dma_pool_alloc %s, %p (corrupted)\n",
  322. pool->name, retval);
  323. /*
  324. * Dump the first 4 bytes even if they are not
  325. * POOL_POISON_FREED
  326. */
  327. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
  328. data, pool->size, 1);
  329. break;
  330. }
  331. }
  332. if (!(mem_flags & __GFP_ZERO))
  333. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  334. #endif
  335. spin_unlock_irqrestore(&pool->lock, flags);
  336. if (mem_flags & __GFP_ZERO)
  337. memset(retval, 0, pool->size);
  338. return retval;
  339. }
  340. EXPORT_SYMBOL(dma_pool_alloc);
  341. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  342. {
  343. struct dma_page *page;
  344. list_for_each_entry(page, &pool->page_list, page_list) {
  345. if (dma < page->dma)
  346. continue;
  347. if ((dma - page->dma) < pool->allocation)
  348. return page;
  349. }
  350. return NULL;
  351. }
  352. /**
  353. * dma_pool_free - put block back into dma pool
  354. * @pool: the dma pool holding the block
  355. * @vaddr: virtual address of block
  356. * @dma: dma address of block
  357. *
  358. * Caller promises neither device nor driver will again touch this block
  359. * unless it is first re-allocated.
  360. */
  361. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  362. {
  363. struct dma_page *page;
  364. unsigned long flags;
  365. unsigned int offset;
  366. spin_lock_irqsave(&pool->lock, flags);
  367. page = pool_find_page(pool, dma);
  368. if (!page) {
  369. spin_unlock_irqrestore(&pool->lock, flags);
  370. if (pool->dev)
  371. dev_err(pool->dev,
  372. "dma_pool_free %s, %p/%lx (bad dma)\n",
  373. pool->name, vaddr, (unsigned long)dma);
  374. else
  375. printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  376. pool->name, vaddr, (unsigned long)dma);
  377. return;
  378. }
  379. offset = vaddr - page->vaddr;
  380. #ifdef DMAPOOL_DEBUG
  381. if ((dma - page->dma) != offset) {
  382. spin_unlock_irqrestore(&pool->lock, flags);
  383. if (pool->dev)
  384. dev_err(pool->dev,
  385. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  386. pool->name, vaddr, (unsigned long long)dma);
  387. else
  388. printk(KERN_ERR
  389. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  390. pool->name, vaddr, (unsigned long long)dma);
  391. return;
  392. }
  393. {
  394. unsigned int chain = page->offset;
  395. while (chain < pool->allocation) {
  396. if (chain != offset) {
  397. chain = *(int *)(page->vaddr + chain);
  398. continue;
  399. }
  400. spin_unlock_irqrestore(&pool->lock, flags);
  401. if (pool->dev)
  402. dev_err(pool->dev, "dma_pool_free %s, dma %Lx "
  403. "already free\n", pool->name,
  404. (unsigned long long)dma);
  405. else
  406. printk(KERN_ERR "dma_pool_free %s, dma %Lx "
  407. "already free\n", pool->name,
  408. (unsigned long long)dma);
  409. return;
  410. }
  411. }
  412. memset(vaddr, POOL_POISON_FREED, pool->size);
  413. #endif
  414. page->in_use--;
  415. *(int *)vaddr = page->offset;
  416. page->offset = offset;
  417. /*
  418. * Resist a temptation to do
  419. * if (!is_page_busy(page)) pool_free_page(pool, page);
  420. * Better have a few empty pages hang around.
  421. */
  422. spin_unlock_irqrestore(&pool->lock, flags);
  423. }
  424. EXPORT_SYMBOL(dma_pool_free);
  425. /*
  426. * Managed DMA pool
  427. */
  428. static void dmam_pool_release(struct device *dev, void *res)
  429. {
  430. struct dma_pool *pool = *(struct dma_pool **)res;
  431. dma_pool_destroy(pool);
  432. }
  433. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  434. {
  435. return *(struct dma_pool **)res == match_data;
  436. }
  437. /**
  438. * dmam_pool_create - Managed dma_pool_create()
  439. * @name: name of pool, for diagnostics
  440. * @dev: device that will be doing the DMA
  441. * @size: size of the blocks in this pool.
  442. * @align: alignment requirement for blocks; must be a power of two
  443. * @allocation: returned blocks won't cross this boundary (or zero)
  444. *
  445. * Managed dma_pool_create(). DMA pool created with this function is
  446. * automatically destroyed on driver detach.
  447. */
  448. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  449. size_t size, size_t align, size_t allocation)
  450. {
  451. struct dma_pool **ptr, *pool;
  452. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  453. if (!ptr)
  454. return NULL;
  455. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  456. if (pool)
  457. devres_add(dev, ptr);
  458. else
  459. devres_free(ptr);
  460. return pool;
  461. }
  462. EXPORT_SYMBOL(dmam_pool_create);
  463. /**
  464. * dmam_pool_destroy - Managed dma_pool_destroy()
  465. * @pool: dma pool that will be destroyed
  466. *
  467. * Managed dma_pool_destroy().
  468. */
  469. void dmam_pool_destroy(struct dma_pool *pool)
  470. {
  471. struct device *dev = pool->dev;
  472. WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
  473. }
  474. EXPORT_SYMBOL(dmam_pool_destroy);