dmapool.c 14 KB

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