swiotlb.c 30 KB

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