swiotlb.c 28 KB

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