dmapool.c 13 KB

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