swiotlb.c 27 KB

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