dmapool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DMA Pool allocator
  4. *
  5. * Copyright 2001 David Brownell
  6. * Copyright 2007 Intel Corporation
  7. * Author: Matthew Wilcox <willy@linux.intel.com>
  8. *
  9. * This allocator returns small blocks of a given size which are DMA-able by
  10. * the given device. It uses the dma_alloc_coherent page allocator to get
  11. * new pages, then splits them up into blocks of the required size.
  12. * Many older drivers still have their own code to do this.
  13. *
  14. * The current design of this allocator is fairly simple. The pool is
  15. * represented by the 'struct dma_pool' which keeps a doubly-linked list of
  16. * allocated pages. Each page in the page_list is split into blocks of at
  17. * least 'size' bytes. Free blocks are tracked in an unsorted singly-linked
  18. * list of free blocks across all pages. Used blocks aren't tracked, but we
  19. * keep a count of how many are currently allocated from each page.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/export.h>
  27. #include <linux/mutex.h>
  28. #include <linux/poison.h>
  29. #include <linux/sched.h>
  30. #include <linux/sched/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/stat.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/types.h>
  36. #include <linux/wait.h>
  37. #ifdef CONFIG_SLUB_DEBUG_ON
  38. #define DMAPOOL_DEBUG 1
  39. #endif
  40. struct dma_block {
  41. struct dma_block *next_block;
  42. dma_addr_t dma;
  43. };
  44. struct dma_pool { /* the pool */
  45. struct list_head page_list;
  46. spinlock_t lock;
  47. struct dma_block *next_block;
  48. size_t nr_blocks;
  49. size_t nr_active;
  50. size_t nr_pages;
  51. struct device *dev;
  52. unsigned int size;
  53. unsigned int allocation;
  54. unsigned int boundary;
  55. char name[32];
  56. struct list_head pools;
  57. };
  58. struct dma_page { /* cacheable header for 'allocation' bytes */
  59. struct list_head page_list;
  60. void *vaddr;
  61. dma_addr_t dma;
  62. };
  63. static DEFINE_MUTEX(pools_lock);
  64. static DEFINE_MUTEX(pools_reg_lock);
  65. static ssize_t pools_show(struct device *dev, struct device_attribute *attr, char *buf)
  66. {
  67. struct dma_pool *pool;
  68. unsigned size;
  69. size = sysfs_emit(buf, "poolinfo - 0.1\n");
  70. mutex_lock(&pools_lock);
  71. list_for_each_entry(pool, &dev->dma_pools, pools) {
  72. /* per-pool info, no real statistics yet */
  73. size += sysfs_emit_at(buf, size, "%-16s %4zu %4zu %4u %2zu\n",
  74. pool->name, pool->nr_active,
  75. pool->nr_blocks, pool->size,
  76. pool->nr_pages);
  77. }
  78. mutex_unlock(&pools_lock);
  79. return size;
  80. }
  81. static DEVICE_ATTR_RO(pools);
  82. #ifdef DMAPOOL_DEBUG
  83. static void pool_check_block(struct dma_pool *pool, struct dma_block *block,
  84. gfp_t mem_flags)
  85. {
  86. u8 *data = (void *)block;
  87. int i;
  88. for (i = sizeof(struct dma_block); i < pool->size; i++) {
  89. if (data[i] == POOL_POISON_FREED)
  90. continue;
  91. dev_err(pool->dev, "%s %s, %p (corrupted)\n", __func__,
  92. pool->name, block);
  93. /*
  94. * Dump the first 4 bytes even if they are not
  95. * POOL_POISON_FREED
  96. */
  97. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
  98. data, pool->size, 1);
  99. break;
  100. }
  101. if (!want_init_on_alloc(mem_flags))
  102. memset(block, POOL_POISON_ALLOCATED, pool->size);
  103. }
  104. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  105. {
  106. struct dma_page *page;
  107. list_for_each_entry(page, &pool->page_list, page_list) {
  108. if (dma < page->dma)
  109. continue;
  110. if ((dma - page->dma) < pool->allocation)
  111. return page;
  112. }
  113. return NULL;
  114. }
  115. static bool pool_block_err(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  116. {
  117. struct dma_block *block = pool->next_block;
  118. struct dma_page *page;
  119. page = pool_find_page(pool, dma);
  120. if (!page) {
  121. dev_err(pool->dev, "%s %s, %p/%pad (bad dma)\n",
  122. __func__, pool->name, vaddr, &dma);
  123. return true;
  124. }
  125. while (block) {
  126. if (block != vaddr) {
  127. block = block->next_block;
  128. continue;
  129. }
  130. dev_err(pool->dev, "%s %s, dma %pad already free\n",
  131. __func__, pool->name, &dma);
  132. return true;
  133. }
  134. memset(vaddr, POOL_POISON_FREED, pool->size);
  135. return false;
  136. }
  137. static void pool_init_page(struct dma_pool *pool, struct dma_page *page)
  138. {
  139. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  140. }
  141. #else
  142. static void pool_check_block(struct dma_pool *pool, struct dma_block *block,
  143. gfp_t mem_flags)
  144. {
  145. }
  146. static bool pool_block_err(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  147. {
  148. if (want_init_on_free())
  149. memset(vaddr, 0, pool->size);
  150. return false;
  151. }
  152. static void pool_init_page(struct dma_pool *pool, struct dma_page *page)
  153. {
  154. }
  155. #endif
  156. static struct dma_block *pool_block_pop(struct dma_pool *pool)
  157. {
  158. struct dma_block *block = pool->next_block;
  159. if (block) {
  160. pool->next_block = block->next_block;
  161. pool->nr_active++;
  162. }
  163. return block;
  164. }
  165. static void pool_block_push(struct dma_pool *pool, struct dma_block *block,
  166. dma_addr_t dma)
  167. {
  168. block->dma = dma;
  169. block->next_block = pool->next_block;
  170. pool->next_block = block;
  171. }
  172. /**
  173. * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
  174. * @name: name of pool, for diagnostics
  175. * @dev: device that will be doing the DMA
  176. * @size: size of the blocks in this pool.
  177. * @align: alignment requirement for blocks; must be a power of two
  178. * @boundary: returned blocks won't cross this power of two boundary
  179. * Context: not in_interrupt()
  180. *
  181. * Given one of these pools, dma_pool_alloc()
  182. * may be used to allocate memory. Such memory will all have "consistent"
  183. * DMA mappings, accessible by the device and its driver without using
  184. * cache flushing primitives. The actual size of blocks allocated may be
  185. * larger than requested because of alignment.
  186. *
  187. * If @boundary is nonzero, objects returned from dma_pool_alloc() won't
  188. * cross that size boundary. This is useful for devices which have
  189. * addressing restrictions on individual DMA transfers, such as not crossing
  190. * boundaries of 4KBytes.
  191. *
  192. * Return: a dma allocation pool with the requested characteristics, or
  193. * %NULL if one can't be created.
  194. */
  195. struct dma_pool *dma_pool_create(const char *name, struct device *dev,
  196. size_t size, size_t align, size_t boundary)
  197. {
  198. struct dma_pool *retval;
  199. size_t allocation;
  200. bool empty;
  201. if (!dev)
  202. return NULL;
  203. if (align == 0)
  204. align = 1;
  205. else if (align & (align - 1))
  206. return NULL;
  207. if (size == 0 || size > INT_MAX)
  208. return NULL;
  209. if (size < sizeof(struct dma_block))
  210. size = sizeof(struct dma_block);
  211. size = ALIGN(size, align);
  212. allocation = max_t(size_t, size, PAGE_SIZE);
  213. if (!boundary)
  214. boundary = allocation;
  215. else if ((boundary < size) || (boundary & (boundary - 1)))
  216. return NULL;
  217. boundary = min(boundary, allocation);
  218. retval = kzalloc(sizeof(*retval), GFP_KERNEL);
  219. if (!retval)
  220. return retval;
  221. strscpy(retval->name, name, sizeof(retval->name));
  222. retval->dev = dev;
  223. INIT_LIST_HEAD(&retval->page_list);
  224. spin_lock_init(&retval->lock);
  225. retval->size = size;
  226. retval->boundary = boundary;
  227. retval->allocation = allocation;
  228. INIT_LIST_HEAD(&retval->pools);
  229. /*
  230. * pools_lock ensures that the ->dma_pools list does not get corrupted.
  231. * pools_reg_lock ensures that there is not a race between
  232. * dma_pool_create() and dma_pool_destroy() or within dma_pool_create()
  233. * when the first invocation of dma_pool_create() failed on
  234. * device_create_file() and the second assumes that it has been done (I
  235. * know it is a short window).
  236. */
  237. mutex_lock(&pools_reg_lock);
  238. mutex_lock(&pools_lock);
  239. empty = list_empty(&dev->dma_pools);
  240. list_add(&retval->pools, &dev->dma_pools);
  241. mutex_unlock(&pools_lock);
  242. if (empty) {
  243. int err;
  244. err = device_create_file(dev, &dev_attr_pools);
  245. if (err) {
  246. mutex_lock(&pools_lock);
  247. list_del(&retval->pools);
  248. mutex_unlock(&pools_lock);
  249. mutex_unlock(&pools_reg_lock);
  250. kfree(retval);
  251. return NULL;
  252. }
  253. }
  254. mutex_unlock(&pools_reg_lock);
  255. return retval;
  256. }
  257. EXPORT_SYMBOL(dma_pool_create);
  258. static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
  259. {
  260. unsigned int next_boundary = pool->boundary, offset = 0;
  261. struct dma_block *block, *first = NULL, *last = NULL;
  262. pool_init_page(pool, page);
  263. while (offset + pool->size <= pool->allocation) {
  264. if (offset + pool->size > next_boundary) {
  265. offset = next_boundary;
  266. next_boundary += pool->boundary;
  267. continue;
  268. }
  269. block = page->vaddr + offset;
  270. block->dma = page->dma + offset;
  271. block->next_block = NULL;
  272. if (last)
  273. last->next_block = block;
  274. else
  275. first = block;
  276. last = block;
  277. offset += pool->size;
  278. pool->nr_blocks++;
  279. }
  280. last->next_block = pool->next_block;
  281. pool->next_block = first;
  282. list_add(&page->page_list, &pool->page_list);
  283. pool->nr_pages++;
  284. }
  285. static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
  286. {
  287. struct dma_page *page;
  288. page = kmalloc(sizeof(*page), mem_flags);
  289. if (!page)
  290. return NULL;
  291. page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
  292. &page->dma, mem_flags);
  293. if (!page->vaddr) {
  294. kfree(page);
  295. return NULL;
  296. }
  297. return page;
  298. }
  299. /**
  300. * dma_pool_destroy - destroys a pool of dma memory blocks.
  301. * @pool: dma pool that will be destroyed
  302. * Context: !in_interrupt()
  303. *
  304. * Caller guarantees that no more memory from the pool is in use,
  305. * and that nothing will try to use the pool after this call.
  306. */
  307. void dma_pool_destroy(struct dma_pool *pool)
  308. {
  309. struct dma_page *page, *tmp;
  310. bool empty, busy = false;
  311. if (unlikely(!pool))
  312. return;
  313. mutex_lock(&pools_reg_lock);
  314. mutex_lock(&pools_lock);
  315. list_del(&pool->pools);
  316. empty = list_empty(&pool->dev->dma_pools);
  317. mutex_unlock(&pools_lock);
  318. if (empty)
  319. device_remove_file(pool->dev, &dev_attr_pools);
  320. mutex_unlock(&pools_reg_lock);
  321. if (pool->nr_active) {
  322. dev_err(pool->dev, "%s %s busy\n", __func__, pool->name);
  323. busy = true;
  324. }
  325. list_for_each_entry_safe(page, tmp, &pool->page_list, page_list) {
  326. if (!busy)
  327. dma_free_coherent(pool->dev, pool->allocation,
  328. page->vaddr, page->dma);
  329. list_del(&page->page_list);
  330. kfree(page);
  331. }
  332. kfree(pool);
  333. }
  334. EXPORT_SYMBOL(dma_pool_destroy);
  335. /**
  336. * dma_pool_alloc - get a block of consistent memory
  337. * @pool: dma pool that will produce the block
  338. * @mem_flags: GFP_* bitmask
  339. * @handle: pointer to dma address of block
  340. *
  341. * Return: the kernel virtual address of a currently unused block,
  342. * and reports its dma address through the handle.
  343. * If such a memory block can't be allocated, %NULL is returned.
  344. */
  345. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  346. dma_addr_t *handle)
  347. {
  348. struct dma_block *block;
  349. struct dma_page *page;
  350. unsigned long flags;
  351. might_alloc(mem_flags);
  352. spin_lock_irqsave(&pool->lock, flags);
  353. block = pool_block_pop(pool);
  354. if (!block) {
  355. /*
  356. * pool_alloc_page() might sleep, so temporarily drop
  357. * &pool->lock
  358. */
  359. spin_unlock_irqrestore(&pool->lock, flags);
  360. page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO));
  361. if (!page)
  362. return NULL;
  363. spin_lock_irqsave(&pool->lock, flags);
  364. pool_initialise_page(pool, page);
  365. block = pool_block_pop(pool);
  366. }
  367. spin_unlock_irqrestore(&pool->lock, flags);
  368. *handle = block->dma;
  369. pool_check_block(pool, block, mem_flags);
  370. if (want_init_on_alloc(mem_flags))
  371. memset(block, 0, pool->size);
  372. return block;
  373. }
  374. EXPORT_SYMBOL(dma_pool_alloc);
  375. /**
  376. * dma_pool_free - put block back into dma pool
  377. * @pool: the dma pool holding the block
  378. * @vaddr: virtual address of block
  379. * @dma: dma address of block
  380. *
  381. * Caller promises neither device nor driver will again touch this block
  382. * unless it is first re-allocated.
  383. */
  384. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  385. {
  386. struct dma_block *block = vaddr;
  387. unsigned long flags;
  388. spin_lock_irqsave(&pool->lock, flags);
  389. if (!pool_block_err(pool, vaddr, dma)) {
  390. pool_block_push(pool, block, dma);
  391. pool->nr_active--;
  392. }
  393. spin_unlock_irqrestore(&pool->lock, flags);
  394. }
  395. EXPORT_SYMBOL(dma_pool_free);
  396. /*
  397. * Managed DMA pool
  398. */
  399. static void dmam_pool_release(struct device *dev, void *res)
  400. {
  401. struct dma_pool *pool = *(struct dma_pool **)res;
  402. dma_pool_destroy(pool);
  403. }
  404. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  405. {
  406. return *(struct dma_pool **)res == match_data;
  407. }
  408. /**
  409. * dmam_pool_create - Managed dma_pool_create()
  410. * @name: name of pool, for diagnostics
  411. * @dev: device that will be doing the DMA
  412. * @size: size of the blocks in this pool.
  413. * @align: alignment requirement for blocks; must be a power of two
  414. * @allocation: returned blocks won't cross this boundary (or zero)
  415. *
  416. * Managed dma_pool_create(). DMA pool created with this function is
  417. * automatically destroyed on driver detach.
  418. *
  419. * Return: a managed dma allocation pool with the requested
  420. * characteristics, or %NULL if one can't be created.
  421. */
  422. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  423. size_t size, size_t align, size_t allocation)
  424. {
  425. struct dma_pool **ptr, *pool;
  426. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  427. if (!ptr)
  428. return NULL;
  429. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  430. if (pool)
  431. devres_add(dev, ptr);
  432. else
  433. devres_free(ptr);
  434. return pool;
  435. }
  436. EXPORT_SYMBOL(dmam_pool_create);
  437. /**
  438. * dmam_pool_destroy - Managed dma_pool_destroy()
  439. * @pool: dma pool that will be destroyed
  440. *
  441. * Managed dma_pool_destroy().
  442. */
  443. void dmam_pool_destroy(struct dma_pool *pool)
  444. {
  445. struct device *dev = pool->dev;
  446. WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
  447. }
  448. EXPORT_SYMBOL(dmam_pool_destroy);