swiotlb.c 30 KB

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