swiotlb.c 27 KB

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