swiotlb.c 25 KB

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