swiotlb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Dynamic DMA mapping support.
  3. *
  4. * This implementation is a fallback for platforms that do not support
  5. * I/O TLBs (aka DMA address translation hardware).
  6. * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
  7. * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
  8. * Copyright (C) 2000, 2003 Hewlett-Packard Co
  9. * David Mosberger-Tang <davidm@hpl.hp.com>
  10. *
  11. * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
  12. * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
  13. * unnecessary i-cache flushing.
  14. * 04/07/.. ak Better overflow handling. Assorted fixes.
  15. * 05/09/10 linville Add support for syncing ranges, support syncing for
  16. * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
  17. */
  18. #include <linux/cache.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/ctype.h>
  26. #include <asm/io.h>
  27. #include <asm/dma.h>
  28. #include <asm/scatterlist.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/iommu-helper.h>
  32. #define OFFSET(val,align) ((unsigned long) \
  33. ( (val) & ( (align) - 1)))
  34. #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
  35. #define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
  36. /*
  37. * Maximum allowable number of contiguous slabs to map,
  38. * must be a power of 2. What is the appropriate value ?
  39. * The complexity of {map,unmap}_single is linearly dependent on this value.
  40. */
  41. #define IO_TLB_SEGSIZE 128
  42. /*
  43. * log of the size of each IO TLB slab. The number of slabs is command line
  44. * controllable.
  45. */
  46. #define IO_TLB_SHIFT 11
  47. #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
  48. /*
  49. * Minimum IO TLB size to bother booting with. Systems with mainly
  50. * 64bit capable cards will only lightly use the swiotlb. If we can't
  51. * allocate a contiguous 1MB, we're probably in trouble anyway.
  52. */
  53. #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
  54. /*
  55. * Enumeration for sync targets
  56. */
  57. enum dma_sync_target {
  58. SYNC_FOR_CPU = 0,
  59. SYNC_FOR_DEVICE = 1,
  60. };
  61. int swiotlb_force;
  62. /*
  63. * Used to do a quick range check in swiotlb_unmap_single and
  64. * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
  65. * API.
  66. */
  67. static char *io_tlb_start, *io_tlb_end;
  68. /*
  69. * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
  70. * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
  71. */
  72. static unsigned long io_tlb_nslabs;
  73. /*
  74. * When the IOMMU overflows we return a fallback buffer. This sets the size.
  75. */
  76. static unsigned long io_tlb_overflow = 32*1024;
  77. void *io_tlb_overflow_buffer;
  78. /*
  79. * This is a free list describing the number of free entries available from
  80. * each index
  81. */
  82. static unsigned int *io_tlb_list;
  83. static unsigned int io_tlb_index;
  84. /*
  85. * We need to save away the original address corresponding to a mapped entry
  86. * for the sync operations.
  87. */
  88. static unsigned char **io_tlb_orig_addr;
  89. /*
  90. * Protect the above data structures in the map and unmap calls
  91. */
  92. static DEFINE_SPINLOCK(io_tlb_lock);
  93. static int __init
  94. setup_io_tlb_npages(char *str)
  95. {
  96. if (isdigit(*str)) {
  97. io_tlb_nslabs = simple_strtoul(str, &str, 0);
  98. /* avoid tail segment of size < IO_TLB_SEGSIZE */
  99. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  100. }
  101. if (*str == ',')
  102. ++str;
  103. if (!strcmp(str, "force"))
  104. swiotlb_force = 1;
  105. return 1;
  106. }
  107. __setup("swiotlb=", setup_io_tlb_npages);
  108. /* make io_tlb_overflow tunable too? */
  109. /*
  110. * Statically reserve bounce buffer space and initialize bounce buffer data
  111. * structures for the software IO TLB used to implement the DMA API.
  112. */
  113. void __init
  114. swiotlb_init_with_default_size(size_t default_size)
  115. {
  116. unsigned long i, bytes;
  117. if (!io_tlb_nslabs) {
  118. io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  119. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  120. }
  121. bytes = io_tlb_nslabs << IO_TLB_SHIFT;
  122. /*
  123. * Get IO TLB memory from the low pages
  124. */
  125. io_tlb_start = alloc_bootmem_low_pages(bytes);
  126. if (!io_tlb_start)
  127. panic("Cannot allocate SWIOTLB buffer");
  128. io_tlb_end = io_tlb_start + bytes;
  129. /*
  130. * Allocate and initialize the free list array. This array is used
  131. * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  132. * between io_tlb_start and io_tlb_end.
  133. */
  134. io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
  135. for (i = 0; i < io_tlb_nslabs; i++)
  136. io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  137. io_tlb_index = 0;
  138. io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
  139. /*
  140. * Get the overflow emergency buffer
  141. */
  142. io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
  143. if (!io_tlb_overflow_buffer)
  144. panic("Cannot allocate SWIOTLB overflow buffer!\n");
  145. printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
  146. virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
  147. }
  148. void __init
  149. swiotlb_init(void)
  150. {
  151. swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
  152. }
  153. /*
  154. * Systems with larger DMA zones (those that don't support ISA) can
  155. * initialize the swiotlb later using the slab allocator if needed.
  156. * This should be just like above, but with some error catching.
  157. */
  158. int
  159. swiotlb_late_init_with_default_size(size_t default_size)
  160. {
  161. unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
  162. unsigned int order;
  163. if (!io_tlb_nslabs) {
  164. io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  165. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  166. }
  167. /*
  168. * Get IO TLB memory from the low pages
  169. */
  170. order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
  171. io_tlb_nslabs = SLABS_PER_PAGE << order;
  172. bytes = io_tlb_nslabs << IO_TLB_SHIFT;
  173. while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
  174. io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
  175. order);
  176. if (io_tlb_start)
  177. break;
  178. order--;
  179. }
  180. if (!io_tlb_start)
  181. goto cleanup1;
  182. if (order != get_order(bytes)) {
  183. printk(KERN_WARNING "Warning: only able to allocate %ld MB "
  184. "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
  185. io_tlb_nslabs = SLABS_PER_PAGE << order;
  186. bytes = io_tlb_nslabs << IO_TLB_SHIFT;
  187. }
  188. io_tlb_end = io_tlb_start + bytes;
  189. memset(io_tlb_start, 0, bytes);
  190. /*
  191. * Allocate and initialize the free list array. This array is used
  192. * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  193. * between io_tlb_start and io_tlb_end.
  194. */
  195. io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
  196. get_order(io_tlb_nslabs * sizeof(int)));
  197. if (!io_tlb_list)
  198. goto cleanup2;
  199. for (i = 0; i < io_tlb_nslabs; i++)
  200. io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  201. io_tlb_index = 0;
  202. io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
  203. get_order(io_tlb_nslabs * sizeof(char *)));
  204. if (!io_tlb_orig_addr)
  205. goto cleanup3;
  206. memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
  207. /*
  208. * Get the overflow emergency buffer
  209. */
  210. io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
  211. get_order(io_tlb_overflow));
  212. if (!io_tlb_overflow_buffer)
  213. goto cleanup4;
  214. printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
  215. "0x%lx\n", bytes >> 20,
  216. virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
  217. return 0;
  218. cleanup4:
  219. free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
  220. sizeof(char *)));
  221. io_tlb_orig_addr = NULL;
  222. cleanup3:
  223. free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
  224. sizeof(int)));
  225. io_tlb_list = NULL;
  226. cleanup2:
  227. io_tlb_end = NULL;
  228. free_pages((unsigned long)io_tlb_start, order);
  229. io_tlb_start = NULL;
  230. cleanup1:
  231. io_tlb_nslabs = req_nslabs;
  232. return -ENOMEM;
  233. }
  234. static int
  235. address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
  236. {
  237. return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
  238. }
  239. static int is_swiotlb_buffer(char *addr)
  240. {
  241. return addr >= io_tlb_start && addr < io_tlb_end;
  242. }
  243. /*
  244. * Allocates bounce buffer and returns its kernel virtual address.
  245. */
  246. static void *
  247. map_single(struct device *hwdev, char *buffer, size_t size, int dir)
  248. {
  249. unsigned long flags;
  250. char *dma_addr;
  251. unsigned int nslots, stride, index, wrap;
  252. int i;
  253. unsigned long start_dma_addr;
  254. unsigned long mask;
  255. unsigned long offset_slots;
  256. unsigned long max_slots;
  257. mask = dma_get_seg_boundary(hwdev);
  258. start_dma_addr = virt_to_bus(io_tlb_start) & mask;
  259. offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  260. max_slots = mask + 1
  261. ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
  262. : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
  263. /*
  264. * For mappings greater than a page, we limit the stride (and
  265. * hence alignment) to a page size.
  266. */
  267. nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  268. if (size > PAGE_SIZE)
  269. stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
  270. else
  271. stride = 1;
  272. BUG_ON(!nslots);
  273. /*
  274. * Find suitable number of IO TLB entries size that will fit this
  275. * request and allocate a buffer from that IO TLB pool.
  276. */
  277. spin_lock_irqsave(&io_tlb_lock, flags);
  278. index = ALIGN(io_tlb_index, stride);
  279. if (index >= io_tlb_nslabs)
  280. index = 0;
  281. wrap = index;
  282. do {
  283. while (iommu_is_span_boundary(index, nslots, offset_slots,
  284. max_slots)) {
  285. index += stride;
  286. if (index >= io_tlb_nslabs)
  287. index = 0;
  288. if (index == wrap)
  289. goto not_found;
  290. }
  291. /*
  292. * If we find a slot that indicates we have 'nslots' number of
  293. * contiguous buffers, we allocate the buffers from that slot
  294. * and mark the entries as '0' indicating unavailable.
  295. */
  296. if (io_tlb_list[index] >= nslots) {
  297. int count = 0;
  298. for (i = index; i < (int) (index + nslots); i++)
  299. io_tlb_list[i] = 0;
  300. for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
  301. io_tlb_list[i] = ++count;
  302. dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
  303. /*
  304. * Update the indices to avoid searching in the next
  305. * round.
  306. */
  307. io_tlb_index = ((index + nslots) < io_tlb_nslabs
  308. ? (index + nslots) : 0);
  309. goto found;
  310. }
  311. index += stride;
  312. if (index >= io_tlb_nslabs)
  313. index = 0;
  314. } while (index != wrap);
  315. not_found:
  316. spin_unlock_irqrestore(&io_tlb_lock, flags);
  317. return NULL;
  318. found:
  319. spin_unlock_irqrestore(&io_tlb_lock, flags);
  320. /*
  321. * Save away the mapping from the original address to the DMA address.
  322. * This is needed when we sync the memory. Then we sync the buffer if
  323. * needed.
  324. */
  325. for (i = 0; i < nslots; i++)
  326. io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
  327. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
  328. memcpy(dma_addr, buffer, size);
  329. return dma_addr;
  330. }
  331. /*
  332. * dma_addr is the kernel virtual address of the bounce buffer to unmap.
  333. */
  334. static void
  335. unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
  336. {
  337. unsigned long flags;
  338. int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  339. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  340. char *buffer = io_tlb_orig_addr[index];
  341. /*
  342. * First, sync the memory before unmapping the entry
  343. */
  344. if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
  345. /*
  346. * bounce... copy the data back into the original buffer * and
  347. * delete the bounce buffer.
  348. */
  349. memcpy(buffer, dma_addr, size);
  350. /*
  351. * Return the buffer to the free list by setting the corresponding
  352. * entries to indicate the number of contigous entries available.
  353. * While returning the entries to the free list, we merge the entries
  354. * with slots below and above the pool being returned.
  355. */
  356. spin_lock_irqsave(&io_tlb_lock, flags);
  357. {
  358. count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
  359. io_tlb_list[index + nslots] : 0);
  360. /*
  361. * Step 1: return the slots to the free list, merging the
  362. * slots with superceeding slots
  363. */
  364. for (i = index + nslots - 1; i >= index; i--)
  365. io_tlb_list[i] = ++count;
  366. /*
  367. * Step 2: merge the returned slots with the preceding slots,
  368. * if available (non zero)
  369. */
  370. for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
  371. io_tlb_list[i] = ++count;
  372. }
  373. spin_unlock_irqrestore(&io_tlb_lock, flags);
  374. }
  375. static void
  376. sync_single(struct device *hwdev, char *dma_addr, size_t size,
  377. int dir, int target)
  378. {
  379. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  380. char *buffer = io_tlb_orig_addr[index];
  381. buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
  382. switch (target) {
  383. case SYNC_FOR_CPU:
  384. if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
  385. memcpy(buffer, dma_addr, size);
  386. else
  387. BUG_ON(dir != DMA_TO_DEVICE);
  388. break;
  389. case SYNC_FOR_DEVICE:
  390. if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
  391. memcpy(dma_addr, buffer, size);
  392. else
  393. BUG_ON(dir != DMA_FROM_DEVICE);
  394. break;
  395. default:
  396. BUG();
  397. }
  398. }
  399. void *
  400. swiotlb_alloc_coherent(struct device *hwdev, size_t size,
  401. dma_addr_t *dma_handle, gfp_t flags)
  402. {
  403. dma_addr_t dev_addr;
  404. void *ret;
  405. int order = get_order(size);
  406. u64 dma_mask = DMA_32BIT_MASK;
  407. if (hwdev && hwdev->coherent_dma_mask)
  408. dma_mask = hwdev->coherent_dma_mask;
  409. ret = (void *)__get_free_pages(flags, order);
  410. if (ret && !is_buffer_dma_capable(dma_mask, virt_to_bus(ret), size)) {
  411. /*
  412. * The allocated memory isn't reachable by the device.
  413. * Fall back on swiotlb_map_single().
  414. */
  415. free_pages((unsigned long) ret, order);
  416. ret = NULL;
  417. }
  418. if (!ret) {
  419. /*
  420. * We are either out of memory or the device can't DMA
  421. * to GFP_DMA memory; fall back on
  422. * swiotlb_map_single(), which will grab memory from
  423. * the lowest available address range.
  424. */
  425. ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
  426. if (!ret)
  427. return NULL;
  428. }
  429. memset(ret, 0, size);
  430. dev_addr = virt_to_bus(ret);
  431. /* Confirm address can be DMA'd by device */
  432. if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
  433. printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
  434. (unsigned long long)dma_mask,
  435. (unsigned long long)dev_addr);
  436. /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
  437. unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
  438. return NULL;
  439. }
  440. *dma_handle = dev_addr;
  441. return ret;
  442. }
  443. void
  444. swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
  445. dma_addr_t dma_handle)
  446. {
  447. WARN_ON(irqs_disabled());
  448. if (!is_swiotlb_buffer(vaddr))
  449. free_pages((unsigned long) vaddr, get_order(size));
  450. else
  451. /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
  452. unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
  453. }
  454. static void
  455. swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
  456. {
  457. /*
  458. * Ran out of IOMMU space for this operation. This is very bad.
  459. * Unfortunately the drivers cannot handle this operation properly.
  460. * unless they check for dma_mapping_error (most don't)
  461. * When the mapping is small enough return a static buffer to limit
  462. * the damage, or panic when the transfer is too big.
  463. */
  464. printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
  465. "device %s\n", size, dev ? dev->bus_id : "?");
  466. if (size > io_tlb_overflow && do_panic) {
  467. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  468. panic("DMA: Memory would be corrupted\n");
  469. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
  470. panic("DMA: Random memory would be DMAed\n");
  471. }
  472. }
  473. /*
  474. * Map a single buffer of the indicated size for DMA in streaming mode. The
  475. * physical address to use is returned.
  476. *
  477. * Once the device is given the dma address, the device owns this memory until
  478. * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
  479. */
  480. dma_addr_t
  481. swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
  482. int dir, struct dma_attrs *attrs)
  483. {
  484. dma_addr_t dev_addr = virt_to_bus(ptr);
  485. void *map;
  486. BUG_ON(dir == DMA_NONE);
  487. /*
  488. * If the pointer passed in happens to be in the device's DMA window,
  489. * we can safely return the device addr and not worry about bounce
  490. * buffering it.
  491. */
  492. if (!address_needs_mapping(hwdev, dev_addr, size) && !swiotlb_force)
  493. return dev_addr;
  494. /*
  495. * Oh well, have to allocate and map a bounce buffer.
  496. */
  497. map = map_single(hwdev, ptr, size, dir);
  498. if (!map) {
  499. swiotlb_full(hwdev, size, dir, 1);
  500. map = io_tlb_overflow_buffer;
  501. }
  502. dev_addr = virt_to_bus(map);
  503. /*
  504. * Ensure that the address returned is DMA'ble
  505. */
  506. if (address_needs_mapping(hwdev, dev_addr, size))
  507. panic("map_single: bounce buffer is not DMA'ble");
  508. return dev_addr;
  509. }
  510. EXPORT_SYMBOL(swiotlb_map_single_attrs);
  511. dma_addr_t
  512. swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
  513. {
  514. return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
  515. }
  516. /*
  517. * Unmap a single streaming mode DMA translation. The dma_addr and size must
  518. * match what was provided for in a previous swiotlb_map_single call. All
  519. * other usages are undefined.
  520. *
  521. * After this call, reads by the cpu to the buffer are guaranteed to see
  522. * whatever the device wrote there.
  523. */
  524. void
  525. swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
  526. size_t size, int dir, struct dma_attrs *attrs)
  527. {
  528. char *dma_addr = bus_to_virt(dev_addr);
  529. BUG_ON(dir == DMA_NONE);
  530. if (is_swiotlb_buffer(dma_addr))
  531. unmap_single(hwdev, dma_addr, size, dir);
  532. else if (dir == DMA_FROM_DEVICE)
  533. dma_mark_clean(dma_addr, size);
  534. }
  535. EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
  536. void
  537. swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
  538. int dir)
  539. {
  540. return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
  541. }
  542. /*
  543. * Make physical memory consistent for a single streaming mode DMA translation
  544. * after a transfer.
  545. *
  546. * If you perform a swiotlb_map_single() but wish to interrogate the buffer
  547. * using the cpu, yet do not wish to teardown the dma mapping, you must
  548. * call this function before doing so. At the next point you give the dma
  549. * address back to the card, you must first perform a
  550. * swiotlb_dma_sync_for_device, and then the device again owns the buffer
  551. */
  552. static void
  553. swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
  554. size_t size, int dir, int target)
  555. {
  556. char *dma_addr = bus_to_virt(dev_addr);
  557. BUG_ON(dir == DMA_NONE);
  558. if (is_swiotlb_buffer(dma_addr))
  559. sync_single(hwdev, dma_addr, size, dir, target);
  560. else if (dir == DMA_FROM_DEVICE)
  561. dma_mark_clean(dma_addr, size);
  562. }
  563. void
  564. swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
  565. size_t size, int dir)
  566. {
  567. swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
  568. }
  569. void
  570. swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
  571. size_t size, int dir)
  572. {
  573. swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
  574. }
  575. /*
  576. * Same as above, but for a sub-range of the mapping.
  577. */
  578. static void
  579. swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
  580. unsigned long offset, size_t size,
  581. int dir, int target)
  582. {
  583. char *dma_addr = bus_to_virt(dev_addr) + offset;
  584. BUG_ON(dir == DMA_NONE);
  585. if (is_swiotlb_buffer(dma_addr))
  586. sync_single(hwdev, dma_addr, size, dir, target);
  587. else if (dir == DMA_FROM_DEVICE)
  588. dma_mark_clean(dma_addr, size);
  589. }
  590. void
  591. swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
  592. unsigned long offset, size_t size, int dir)
  593. {
  594. swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
  595. SYNC_FOR_CPU);
  596. }
  597. void
  598. swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
  599. unsigned long offset, size_t size, int dir)
  600. {
  601. swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
  602. SYNC_FOR_DEVICE);
  603. }
  604. void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
  605. struct dma_attrs *);
  606. /*
  607. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  608. * This is the scatter-gather version of the above swiotlb_map_single
  609. * interface. Here the scatter gather list elements are each tagged with the
  610. * appropriate dma address and length. They are obtained via
  611. * sg_dma_{address,length}(SG).
  612. *
  613. * NOTE: An implementation may be able to use a smaller number of
  614. * DMA address/length pairs than there are SG table elements.
  615. * (for example via virtual mapping capabilities)
  616. * The routine returns the number of addr/length pairs actually
  617. * used, at most nents.
  618. *
  619. * Device ownership issues as mentioned above for swiotlb_map_single are the
  620. * same here.
  621. */
  622. int
  623. swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
  624. int dir, struct dma_attrs *attrs)
  625. {
  626. struct scatterlist *sg;
  627. void *addr;
  628. dma_addr_t dev_addr;
  629. int i;
  630. BUG_ON(dir == DMA_NONE);
  631. for_each_sg(sgl, sg, nelems, i) {
  632. addr = SG_ENT_VIRT_ADDRESS(sg);
  633. dev_addr = virt_to_bus(addr);
  634. if (swiotlb_force ||
  635. address_needs_mapping(hwdev, dev_addr, sg->length)) {
  636. void *map = map_single(hwdev, addr, sg->length, dir);
  637. if (!map) {
  638. /* Don't panic here, we expect map_sg users
  639. to do proper error handling. */
  640. swiotlb_full(hwdev, sg->length, dir, 0);
  641. swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
  642. attrs);
  643. sgl[0].dma_length = 0;
  644. return 0;
  645. }
  646. sg->dma_address = virt_to_bus(map);
  647. } else
  648. sg->dma_address = dev_addr;
  649. sg->dma_length = sg->length;
  650. }
  651. return nelems;
  652. }
  653. EXPORT_SYMBOL(swiotlb_map_sg_attrs);
  654. int
  655. swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
  656. int dir)
  657. {
  658. return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
  659. }
  660. /*
  661. * Unmap a set of streaming mode DMA translations. Again, cpu read rules
  662. * concerning calls here are the same as for swiotlb_unmap_single() above.
  663. */
  664. void
  665. swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
  666. int nelems, int dir, struct dma_attrs *attrs)
  667. {
  668. struct scatterlist *sg;
  669. int i;
  670. BUG_ON(dir == DMA_NONE);
  671. for_each_sg(sgl, sg, nelems, i) {
  672. if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
  673. unmap_single(hwdev, bus_to_virt(sg->dma_address),
  674. sg->dma_length, dir);
  675. else if (dir == DMA_FROM_DEVICE)
  676. dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
  677. }
  678. }
  679. EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
  680. void
  681. swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
  682. int dir)
  683. {
  684. return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
  685. }
  686. /*
  687. * Make physical memory consistent for a set of streaming mode DMA translations
  688. * after a transfer.
  689. *
  690. * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
  691. * and usage.
  692. */
  693. static void
  694. swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
  695. int nelems, int dir, int target)
  696. {
  697. struct scatterlist *sg;
  698. int i;
  699. BUG_ON(dir == DMA_NONE);
  700. for_each_sg(sgl, sg, nelems, i) {
  701. if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
  702. sync_single(hwdev, bus_to_virt(sg->dma_address),
  703. sg->dma_length, dir, target);
  704. else if (dir == DMA_FROM_DEVICE)
  705. dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
  706. }
  707. }
  708. void
  709. swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
  710. int nelems, int dir)
  711. {
  712. swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
  713. }
  714. void
  715. swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
  716. int nelems, int dir)
  717. {
  718. swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
  719. }
  720. int
  721. swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
  722. {
  723. return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
  724. }
  725. /*
  726. * Return whether the given device DMA address mask can be supported
  727. * properly. For example, if your device can only drive the low 24-bits
  728. * during bus mastering, then you would pass 0x00ffffff as the mask to
  729. * this function.
  730. */
  731. int
  732. swiotlb_dma_supported(struct device *hwdev, u64 mask)
  733. {
  734. return virt_to_bus(io_tlb_end - 1) <= mask;
  735. }
  736. EXPORT_SYMBOL(swiotlb_map_single);
  737. EXPORT_SYMBOL(swiotlb_unmap_single);
  738. EXPORT_SYMBOL(swiotlb_map_sg);
  739. EXPORT_SYMBOL(swiotlb_unmap_sg);
  740. EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
  741. EXPORT_SYMBOL(swiotlb_sync_single_for_device);
  742. EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
  743. EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
  744. EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
  745. EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
  746. EXPORT_SYMBOL(swiotlb_dma_mapping_error);
  747. EXPORT_SYMBOL(swiotlb_alloc_coherent);
  748. EXPORT_SYMBOL(swiotlb_free_coherent);
  749. EXPORT_SYMBOL(swiotlb_dma_supported);