swiotlb.c 26 KB

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