swiotlb.c 26 KB

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