dmapool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include <linux/device.h>
  2. #include <linux/mm.h>
  3. #include <asm/io.h> /* Needed for i386 to build */
  4. #include <linux/dma-mapping.h>
  5. #include <linux/dmapool.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/poison.h>
  9. #include <linux/sched.h>
  10. /*
  11. * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
  12. * small blocks are easily used by drivers for bus mastering controllers.
  13. * This should probably be sharing the guts of the slab allocator.
  14. */
  15. struct dma_pool { /* the pool */
  16. struct list_head page_list;
  17. spinlock_t lock;
  18. size_t blocks_per_page;
  19. size_t size;
  20. struct device *dev;
  21. size_t allocation;
  22. char name[32];
  23. wait_queue_head_t waitq;
  24. struct list_head pools;
  25. };
  26. struct dma_page { /* cacheable header for 'allocation' bytes */
  27. struct list_head page_list;
  28. void *vaddr;
  29. dma_addr_t dma;
  30. unsigned in_use;
  31. unsigned long bitmap[0];
  32. };
  33. #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
  34. static DEFINE_MUTEX(pools_lock);
  35. static ssize_t
  36. show_pools(struct device *dev, struct device_attribute *attr, char *buf)
  37. {
  38. unsigned temp;
  39. unsigned size;
  40. char *next;
  41. struct dma_page *page;
  42. struct dma_pool *pool;
  43. next = buf;
  44. size = PAGE_SIZE;
  45. temp = scnprintf(next, size, "poolinfo - 0.1\n");
  46. size -= temp;
  47. next += temp;
  48. mutex_lock(&pools_lock);
  49. list_for_each_entry(pool, &dev->dma_pools, pools) {
  50. unsigned pages = 0;
  51. unsigned blocks = 0;
  52. list_for_each_entry(page, &pool->page_list, page_list) {
  53. pages++;
  54. blocks += page->in_use;
  55. }
  56. /* per-pool info, no real statistics yet */
  57. temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
  58. pool->name,
  59. blocks, pages * pool->blocks_per_page,
  60. pool->size, pages);
  61. size -= temp;
  62. next += temp;
  63. }
  64. mutex_unlock(&pools_lock);
  65. return PAGE_SIZE - size;
  66. }
  67. static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
  68. /**
  69. * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
  70. * @name: name of pool, for diagnostics
  71. * @dev: device that will be doing the DMA
  72. * @size: size of the blocks in this pool.
  73. * @align: alignment requirement for blocks; must be a power of two
  74. * @allocation: returned blocks won't cross this boundary (or zero)
  75. * Context: !in_interrupt()
  76. *
  77. * Returns a dma allocation pool with the requested characteristics, or
  78. * null if one can't be created. Given one of these pools, dma_pool_alloc()
  79. * may be used to allocate memory. Such memory will all have "consistent"
  80. * DMA mappings, accessible by the device and its driver without using
  81. * cache flushing primitives. The actual size of blocks allocated may be
  82. * larger than requested because of alignment.
  83. *
  84. * If allocation is nonzero, objects returned from dma_pool_alloc() won't
  85. * cross that size boundary. This is useful for devices which have
  86. * addressing restrictions on individual DMA transfers, such as not crossing
  87. * boundaries of 4KBytes.
  88. */
  89. struct dma_pool *dma_pool_create(const char *name, struct device *dev,
  90. size_t size, size_t align, size_t allocation)
  91. {
  92. struct dma_pool *retval;
  93. if (align == 0)
  94. align = 1;
  95. if (size == 0)
  96. return NULL;
  97. else if (size < align)
  98. size = align;
  99. else if ((size % align) != 0) {
  100. size += align + 1;
  101. size &= ~(align - 1);
  102. }
  103. if (allocation == 0) {
  104. if (PAGE_SIZE < size)
  105. allocation = size;
  106. else
  107. allocation = PAGE_SIZE;
  108. /* FIXME: round up for less fragmentation */
  109. } else if (allocation < size)
  110. return NULL;
  111. if (!
  112. (retval =
  113. kmalloc_node(sizeof *retval, GFP_KERNEL, dev_to_node(dev))))
  114. return retval;
  115. strlcpy(retval->name, name, sizeof retval->name);
  116. retval->dev = dev;
  117. INIT_LIST_HEAD(&retval->page_list);
  118. spin_lock_init(&retval->lock);
  119. retval->size = size;
  120. retval->allocation = allocation;
  121. retval->blocks_per_page = allocation / size;
  122. init_waitqueue_head(&retval->waitq);
  123. if (dev) {
  124. int ret;
  125. mutex_lock(&pools_lock);
  126. if (list_empty(&dev->dma_pools))
  127. ret = device_create_file(dev, &dev_attr_pools);
  128. else
  129. ret = 0;
  130. /* note: not currently insisting "name" be unique */
  131. if (!ret)
  132. list_add(&retval->pools, &dev->dma_pools);
  133. else {
  134. kfree(retval);
  135. retval = NULL;
  136. }
  137. mutex_unlock(&pools_lock);
  138. } else
  139. INIT_LIST_HEAD(&retval->pools);
  140. return retval;
  141. }
  142. EXPORT_SYMBOL(dma_pool_create);
  143. static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
  144. {
  145. struct dma_page *page;
  146. int mapsize;
  147. mapsize = pool->blocks_per_page;
  148. mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
  149. mapsize *= sizeof(long);
  150. page = kmalloc(mapsize + sizeof *page, mem_flags);
  151. if (!page)
  152. return NULL;
  153. page->vaddr = dma_alloc_coherent(pool->dev,
  154. pool->allocation,
  155. &page->dma, mem_flags);
  156. if (page->vaddr) {
  157. memset(page->bitmap, 0xff, mapsize); /* bit set == free */
  158. #ifdef CONFIG_DEBUG_SLAB
  159. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  160. #endif
  161. list_add(&page->page_list, &pool->page_list);
  162. page->in_use = 0;
  163. } else {
  164. kfree(page);
  165. page = NULL;
  166. }
  167. return page;
  168. }
  169. static inline int is_page_busy(int blocks, unsigned long *bitmap)
  170. {
  171. while (blocks > 0) {
  172. if (*bitmap++ != ~0UL)
  173. return 1;
  174. blocks -= BITS_PER_LONG;
  175. }
  176. return 0;
  177. }
  178. static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
  179. {
  180. dma_addr_t dma = page->dma;
  181. #ifdef CONFIG_DEBUG_SLAB
  182. memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
  183. #endif
  184. dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
  185. list_del(&page->page_list);
  186. kfree(page);
  187. }
  188. /**
  189. * dma_pool_destroy - destroys a pool of dma memory blocks.
  190. * @pool: dma pool that will be destroyed
  191. * Context: !in_interrupt()
  192. *
  193. * Caller guarantees that no more memory from the pool is in use,
  194. * and that nothing will try to use the pool after this call.
  195. */
  196. void dma_pool_destroy(struct dma_pool *pool)
  197. {
  198. mutex_lock(&pools_lock);
  199. list_del(&pool->pools);
  200. if (pool->dev && list_empty(&pool->dev->dma_pools))
  201. device_remove_file(pool->dev, &dev_attr_pools);
  202. mutex_unlock(&pools_lock);
  203. while (!list_empty(&pool->page_list)) {
  204. struct dma_page *page;
  205. page = list_entry(pool->page_list.next,
  206. struct dma_page, page_list);
  207. if (is_page_busy(pool->blocks_per_page, page->bitmap)) {
  208. if (pool->dev)
  209. dev_err(pool->dev,
  210. "dma_pool_destroy %s, %p busy\n",
  211. pool->name, page->vaddr);
  212. else
  213. printk(KERN_ERR
  214. "dma_pool_destroy %s, %p busy\n",
  215. pool->name, page->vaddr);
  216. /* leak the still-in-use consistent memory */
  217. list_del(&page->page_list);
  218. kfree(page);
  219. } else
  220. pool_free_page(pool, page);
  221. }
  222. kfree(pool);
  223. }
  224. EXPORT_SYMBOL(dma_pool_destroy);
  225. /**
  226. * dma_pool_alloc - get a block of consistent memory
  227. * @pool: dma pool that will produce the block
  228. * @mem_flags: GFP_* bitmask
  229. * @handle: pointer to dma address of block
  230. *
  231. * This returns the kernel virtual address of a currently unused block,
  232. * and reports its dma address through the handle.
  233. * If such a memory block can't be allocated, null is returned.
  234. */
  235. void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
  236. dma_addr_t *handle)
  237. {
  238. unsigned long flags;
  239. struct dma_page *page;
  240. int map, block;
  241. size_t offset;
  242. void *retval;
  243. spin_lock_irqsave(&pool->lock, flags);
  244. restart:
  245. list_for_each_entry(page, &pool->page_list, page_list) {
  246. int i;
  247. /* only cachable accesses here ... */
  248. for (map = 0, i = 0;
  249. i < pool->blocks_per_page; i += BITS_PER_LONG, map++) {
  250. if (page->bitmap[map] == 0)
  251. continue;
  252. block = ffz(~page->bitmap[map]);
  253. if ((i + block) < pool->blocks_per_page) {
  254. clear_bit(block, &page->bitmap[map]);
  255. offset = (BITS_PER_LONG * map) + block;
  256. offset *= pool->size;
  257. goto ready;
  258. }
  259. }
  260. }
  261. page = pool_alloc_page(pool, GFP_ATOMIC);
  262. if (!page) {
  263. if (mem_flags & __GFP_WAIT) {
  264. DECLARE_WAITQUEUE(wait, current);
  265. __set_current_state(TASK_INTERRUPTIBLE);
  266. __add_wait_queue(&pool->waitq, &wait);
  267. spin_unlock_irqrestore(&pool->lock, flags);
  268. schedule_timeout(POOL_TIMEOUT_JIFFIES);
  269. spin_lock_irqsave(&pool->lock, flags);
  270. __remove_wait_queue(&pool->waitq, &wait);
  271. goto restart;
  272. }
  273. retval = NULL;
  274. goto done;
  275. }
  276. clear_bit(0, &page->bitmap[0]);
  277. offset = 0;
  278. ready:
  279. page->in_use++;
  280. retval = offset + page->vaddr;
  281. *handle = offset + page->dma;
  282. #ifdef CONFIG_DEBUG_SLAB
  283. memset(retval, POOL_POISON_ALLOCATED, pool->size);
  284. #endif
  285. done:
  286. spin_unlock_irqrestore(&pool->lock, flags);
  287. return retval;
  288. }
  289. EXPORT_SYMBOL(dma_pool_alloc);
  290. static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
  291. {
  292. unsigned long flags;
  293. struct dma_page *page;
  294. spin_lock_irqsave(&pool->lock, flags);
  295. list_for_each_entry(page, &pool->page_list, page_list) {
  296. if (dma < page->dma)
  297. continue;
  298. if (dma < (page->dma + pool->allocation))
  299. goto done;
  300. }
  301. page = NULL;
  302. done:
  303. spin_unlock_irqrestore(&pool->lock, flags);
  304. return page;
  305. }
  306. /**
  307. * dma_pool_free - put block back into dma pool
  308. * @pool: the dma pool holding the block
  309. * @vaddr: virtual address of block
  310. * @dma: dma address of block
  311. *
  312. * Caller promises neither device nor driver will again touch this block
  313. * unless it is first re-allocated.
  314. */
  315. void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
  316. {
  317. struct dma_page *page;
  318. unsigned long flags;
  319. int map, block;
  320. page = pool_find_page(pool, dma);
  321. if (!page) {
  322. if (pool->dev)
  323. dev_err(pool->dev,
  324. "dma_pool_free %s, %p/%lx (bad dma)\n",
  325. pool->name, vaddr, (unsigned long)dma);
  326. else
  327. printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
  328. pool->name, vaddr, (unsigned long)dma);
  329. return;
  330. }
  331. block = dma - page->dma;
  332. block /= pool->size;
  333. map = block / BITS_PER_LONG;
  334. block %= BITS_PER_LONG;
  335. #ifdef CONFIG_DEBUG_SLAB
  336. if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
  337. if (pool->dev)
  338. dev_err(pool->dev,
  339. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  340. pool->name, vaddr, (unsigned long long)dma);
  341. else
  342. printk(KERN_ERR
  343. "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
  344. pool->name, vaddr, (unsigned long long)dma);
  345. return;
  346. }
  347. if (page->bitmap[map] & (1UL << block)) {
  348. if (pool->dev)
  349. dev_err(pool->dev,
  350. "dma_pool_free %s, dma %Lx already free\n",
  351. pool->name, (unsigned long long)dma);
  352. else
  353. printk(KERN_ERR
  354. "dma_pool_free %s, dma %Lx already free\n",
  355. pool->name, (unsigned long long)dma);
  356. return;
  357. }
  358. memset(vaddr, POOL_POISON_FREED, pool->size);
  359. #endif
  360. spin_lock_irqsave(&pool->lock, flags);
  361. page->in_use--;
  362. set_bit(block, &page->bitmap[map]);
  363. if (waitqueue_active(&pool->waitq))
  364. wake_up_locked(&pool->waitq);
  365. /*
  366. * Resist a temptation to do
  367. * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
  368. * Better have a few empty pages hang around.
  369. */
  370. spin_unlock_irqrestore(&pool->lock, flags);
  371. }
  372. EXPORT_SYMBOL(dma_pool_free);
  373. /*
  374. * Managed DMA pool
  375. */
  376. static void dmam_pool_release(struct device *dev, void *res)
  377. {
  378. struct dma_pool *pool = *(struct dma_pool **)res;
  379. dma_pool_destroy(pool);
  380. }
  381. static int dmam_pool_match(struct device *dev, void *res, void *match_data)
  382. {
  383. return *(struct dma_pool **)res == match_data;
  384. }
  385. /**
  386. * dmam_pool_create - Managed dma_pool_create()
  387. * @name: name of pool, for diagnostics
  388. * @dev: device that will be doing the DMA
  389. * @size: size of the blocks in this pool.
  390. * @align: alignment requirement for blocks; must be a power of two
  391. * @allocation: returned blocks won't cross this boundary (or zero)
  392. *
  393. * Managed dma_pool_create(). DMA pool created with this function is
  394. * automatically destroyed on driver detach.
  395. */
  396. struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
  397. size_t size, size_t align, size_t allocation)
  398. {
  399. struct dma_pool **ptr, *pool;
  400. ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
  401. if (!ptr)
  402. return NULL;
  403. pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
  404. if (pool)
  405. devres_add(dev, ptr);
  406. else
  407. devres_free(ptr);
  408. return pool;
  409. }
  410. EXPORT_SYMBOL(dmam_pool_create);
  411. /**
  412. * dmam_pool_destroy - Managed dma_pool_destroy()
  413. * @pool: dma pool that will be destroyed
  414. *
  415. * Managed dma_pool_destroy().
  416. */
  417. void dmam_pool_destroy(struct dma_pool *pool)
  418. {
  419. struct device *dev = pool->dev;
  420. dma_pool_destroy(pool);
  421. WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
  422. }
  423. EXPORT_SYMBOL(dmam_pool_destroy);