swiotlb.c 27 KB

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