swiotlb.c 27 KB

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