swiotlb.c 29 KB

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