swiotlb.c 27 KB

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