swiotlb.c 26 KB

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