swiotlb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Dynamic DMA mapping support.
  3. *
  4. * This implementation is for IA-64 and EM64T 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. */
  18. #include <linux/cache.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/ctype.h>
  26. #include <asm/io.h>
  27. #include <asm/dma.h>
  28. #include <asm/scatterlist.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #define OFFSET(val,align) ((unsigned long) \
  32. ( (val) & ( (align) - 1)))
  33. #define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
  34. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  35. /*
  36. * Maximum allowable number of contiguous slabs to map,
  37. * must be a power of 2. What is the appropriate value ?
  38. * The complexity of {map,unmap}_single is linearly dependent on this value.
  39. */
  40. #define IO_TLB_SEGSIZE 128
  41. /*
  42. * log of the size of each IO TLB slab. The number of slabs is command line
  43. * controllable.
  44. */
  45. #define IO_TLB_SHIFT 11
  46. #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
  47. /*
  48. * Minimum IO TLB size to bother booting with. Systems with mainly
  49. * 64bit capable cards will only lightly use the swiotlb. If we can't
  50. * allocate a contiguous 1MB, we're probably in trouble anyway.
  51. */
  52. #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
  53. /*
  54. * Enumeration for sync targets
  55. */
  56. enum dma_sync_target {
  57. SYNC_FOR_CPU = 0,
  58. SYNC_FOR_DEVICE = 1,
  59. };
  60. int swiotlb_force;
  61. /*
  62. * Used to do a quick range check in swiotlb_unmap_single and
  63. * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
  64. * API.
  65. */
  66. static char *io_tlb_start, *io_tlb_end;
  67. /*
  68. * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
  69. * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
  70. */
  71. static unsigned long io_tlb_nslabs;
  72. /*
  73. * When the IOMMU overflows we return a fallback buffer. This sets the size.
  74. */
  75. static unsigned long io_tlb_overflow = 32*1024;
  76. void *io_tlb_overflow_buffer;
  77. /*
  78. * This is a free list describing the number of free entries available from
  79. * each index
  80. */
  81. static unsigned int *io_tlb_list;
  82. static unsigned int io_tlb_index;
  83. /*
  84. * We need to save away the original address corresponding to a mapped entry
  85. * for the sync operations.
  86. */
  87. static unsigned char **io_tlb_orig_addr;
  88. /*
  89. * Protect the above data structures in the map and unmap calls
  90. */
  91. static DEFINE_SPINLOCK(io_tlb_lock);
  92. static int __init
  93. setup_io_tlb_npages(char *str)
  94. {
  95. if (isdigit(*str)) {
  96. io_tlb_nslabs = simple_strtoul(str, &str, 0);
  97. /* avoid tail segment of size < IO_TLB_SEGSIZE */
  98. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  99. }
  100. if (*str == ',')
  101. ++str;
  102. if (!strcmp(str, "force"))
  103. swiotlb_force = 1;
  104. return 1;
  105. }
  106. __setup("swiotlb=", setup_io_tlb_npages);
  107. /* make io_tlb_overflow tunable too? */
  108. /*
  109. * Statically reserve bounce buffer space and initialize bounce buffer data
  110. * structures for the software IO TLB used to implement the DMA API.
  111. */
  112. void
  113. swiotlb_init_with_default_size (size_t default_size)
  114. {
  115. unsigned long i;
  116. if (!io_tlb_nslabs) {
  117. io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  118. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  119. }
  120. /*
  121. * Get IO TLB memory from the low pages
  122. */
  123. io_tlb_start = alloc_bootmem_low_pages(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
  124. if (!io_tlb_start)
  125. panic("Cannot allocate SWIOTLB buffer");
  126. io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
  127. /*
  128. * Allocate and initialize the free list array. This array is used
  129. * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  130. * between io_tlb_start and io_tlb_end.
  131. */
  132. io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
  133. for (i = 0; i < io_tlb_nslabs; i++)
  134. io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  135. io_tlb_index = 0;
  136. io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
  137. /*
  138. * Get the overflow emergency buffer
  139. */
  140. io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
  141. printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
  142. virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
  143. }
  144. void
  145. swiotlb_init (void)
  146. {
  147. swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
  148. }
  149. /*
  150. * Systems with larger DMA zones (those that don't support ISA) can
  151. * initialize the swiotlb later using the slab allocator if needed.
  152. * This should be just like above, but with some error catching.
  153. */
  154. int
  155. swiotlb_late_init_with_default_size (size_t default_size)
  156. {
  157. unsigned long i, req_nslabs = io_tlb_nslabs;
  158. unsigned int order;
  159. if (!io_tlb_nslabs) {
  160. io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  161. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  162. }
  163. /*
  164. * Get IO TLB memory from the low pages
  165. */
  166. order = get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
  167. io_tlb_nslabs = SLABS_PER_PAGE << order;
  168. while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
  169. io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
  170. order);
  171. if (io_tlb_start)
  172. break;
  173. order--;
  174. }
  175. if (!io_tlb_start)
  176. goto cleanup1;
  177. if (order != get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT))) {
  178. printk(KERN_WARNING "Warning: only able to allocate %ld MB "
  179. "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
  180. io_tlb_nslabs = SLABS_PER_PAGE << order;
  181. }
  182. io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
  183. memset(io_tlb_start, 0, io_tlb_nslabs * (1 << IO_TLB_SHIFT));
  184. /*
  185. * Allocate and initialize the free list array. This array is used
  186. * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  187. * between io_tlb_start and io_tlb_end.
  188. */
  189. io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
  190. get_order(io_tlb_nslabs * sizeof(int)));
  191. if (!io_tlb_list)
  192. goto cleanup2;
  193. for (i = 0; i < io_tlb_nslabs; i++)
  194. io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  195. io_tlb_index = 0;
  196. io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
  197. get_order(io_tlb_nslabs * sizeof(char *)));
  198. if (!io_tlb_orig_addr)
  199. goto cleanup3;
  200. memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
  201. /*
  202. * Get the overflow emergency buffer
  203. */
  204. io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
  205. get_order(io_tlb_overflow));
  206. if (!io_tlb_overflow_buffer)
  207. goto cleanup4;
  208. printk(KERN_INFO "Placing %ldMB software IO TLB between 0x%lx - "
  209. "0x%lx\n", (io_tlb_nslabs * (1 << IO_TLB_SHIFT)) >> 20,
  210. virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
  211. return 0;
  212. cleanup4:
  213. free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
  214. sizeof(char *)));
  215. io_tlb_orig_addr = NULL;
  216. cleanup3:
  217. free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
  218. sizeof(int)));
  219. io_tlb_list = NULL;
  220. io_tlb_end = NULL;
  221. cleanup2:
  222. free_pages((unsigned long)io_tlb_start, order);
  223. io_tlb_start = NULL;
  224. cleanup1:
  225. io_tlb_nslabs = req_nslabs;
  226. return -ENOMEM;
  227. }
  228. static inline int
  229. address_needs_mapping(struct device *hwdev, dma_addr_t addr)
  230. {
  231. dma_addr_t mask = 0xffffffff;
  232. /* If the device has a mask, use it, otherwise default to 32 bits */
  233. if (hwdev && hwdev->dma_mask)
  234. mask = *hwdev->dma_mask;
  235. return (addr & ~mask) != 0;
  236. }
  237. /*
  238. * Allocates bounce buffer and returns its kernel virtual address.
  239. */
  240. static void *
  241. map_single(struct device *hwdev, char *buffer, size_t size, int dir)
  242. {
  243. unsigned long flags;
  244. char *dma_addr;
  245. unsigned int nslots, stride, index, wrap;
  246. int i;
  247. /*
  248. * For mappings greater than a page, we limit the stride (and
  249. * hence alignment) to a page size.
  250. */
  251. nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  252. if (size > PAGE_SIZE)
  253. stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
  254. else
  255. stride = 1;
  256. BUG_ON(!nslots);
  257. /*
  258. * Find suitable number of IO TLB entries size that will fit this
  259. * request and allocate a buffer from that IO TLB pool.
  260. */
  261. spin_lock_irqsave(&io_tlb_lock, flags);
  262. {
  263. wrap = index = ALIGN(io_tlb_index, stride);
  264. if (index >= io_tlb_nslabs)
  265. wrap = index = 0;
  266. do {
  267. /*
  268. * If we find a slot that indicates we have 'nslots'
  269. * number of contiguous buffers, we allocate the
  270. * buffers from that slot and mark the entries as '0'
  271. * indicating unavailable.
  272. */
  273. if (io_tlb_list[index] >= nslots) {
  274. int count = 0;
  275. for (i = index; i < (int) (index + nslots); i++)
  276. io_tlb_list[i] = 0;
  277. for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
  278. io_tlb_list[i] = ++count;
  279. dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
  280. /*
  281. * Update the indices to avoid searching in
  282. * the next round.
  283. */
  284. io_tlb_index = ((index + nslots) < io_tlb_nslabs
  285. ? (index + nslots) : 0);
  286. goto found;
  287. }
  288. index += stride;
  289. if (index >= io_tlb_nslabs)
  290. index = 0;
  291. } while (index != wrap);
  292. spin_unlock_irqrestore(&io_tlb_lock, flags);
  293. return NULL;
  294. }
  295. found:
  296. spin_unlock_irqrestore(&io_tlb_lock, flags);
  297. /*
  298. * Save away the mapping from the original address to the DMA address.
  299. * This is needed when we sync the memory. Then we sync the buffer if
  300. * needed.
  301. */
  302. io_tlb_orig_addr[index] = buffer;
  303. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
  304. memcpy(dma_addr, buffer, size);
  305. return dma_addr;
  306. }
  307. /*
  308. * dma_addr is the kernel virtual address of the bounce buffer to unmap.
  309. */
  310. static void
  311. unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
  312. {
  313. unsigned long flags;
  314. int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  315. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  316. char *buffer = io_tlb_orig_addr[index];
  317. /*
  318. * First, sync the memory before unmapping the entry
  319. */
  320. if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
  321. /*
  322. * bounce... copy the data back into the original buffer * and
  323. * delete the bounce buffer.
  324. */
  325. memcpy(buffer, dma_addr, size);
  326. /*
  327. * Return the buffer to the free list by setting the corresponding
  328. * entries to indicate the number of contigous entries available.
  329. * While returning the entries to the free list, we merge the entries
  330. * with slots below and above the pool being returned.
  331. */
  332. spin_lock_irqsave(&io_tlb_lock, flags);
  333. {
  334. count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
  335. io_tlb_list[index + nslots] : 0);
  336. /*
  337. * Step 1: return the slots to the free list, merging the
  338. * slots with superceeding slots
  339. */
  340. for (i = index + nslots - 1; i >= index; i--)
  341. io_tlb_list[i] = ++count;
  342. /*
  343. * Step 2: merge the returned slots with the preceding slots,
  344. * if available (non zero)
  345. */
  346. for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
  347. io_tlb_list[i] = ++count;
  348. }
  349. spin_unlock_irqrestore(&io_tlb_lock, flags);
  350. }
  351. static void
  352. sync_single(struct device *hwdev, char *dma_addr, size_t size,
  353. int dir, int target)
  354. {
  355. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  356. char *buffer = io_tlb_orig_addr[index];
  357. switch (target) {
  358. case SYNC_FOR_CPU:
  359. if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
  360. memcpy(buffer, dma_addr, size);
  361. else
  362. BUG_ON(dir != DMA_TO_DEVICE);
  363. break;
  364. case SYNC_FOR_DEVICE:
  365. if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
  366. memcpy(dma_addr, buffer, size);
  367. else
  368. BUG_ON(dir != DMA_FROM_DEVICE);
  369. break;
  370. default:
  371. BUG();
  372. }
  373. }
  374. void *
  375. swiotlb_alloc_coherent(struct device *hwdev, size_t size,
  376. dma_addr_t *dma_handle, gfp_t flags)
  377. {
  378. unsigned long dev_addr;
  379. void *ret;
  380. int order = get_order(size);
  381. /*
  382. * XXX fix me: the DMA API should pass us an explicit DMA mask
  383. * instead, or use ZONE_DMA32 (ia64 overloads ZONE_DMA to be a ~32
  384. * bit range instead of a 16MB one).
  385. */
  386. flags |= GFP_DMA;
  387. ret = (void *)__get_free_pages(flags, order);
  388. if (ret && address_needs_mapping(hwdev, virt_to_phys(ret))) {
  389. /*
  390. * The allocated memory isn't reachable by the device.
  391. * Fall back on swiotlb_map_single().
  392. */
  393. free_pages((unsigned long) ret, order);
  394. ret = NULL;
  395. }
  396. if (!ret) {
  397. /*
  398. * We are either out of memory or the device can't DMA
  399. * to GFP_DMA memory; fall back on
  400. * swiotlb_map_single(), which will grab memory from
  401. * the lowest available address range.
  402. */
  403. dma_addr_t handle;
  404. handle = swiotlb_map_single(NULL, NULL, size, DMA_FROM_DEVICE);
  405. if (swiotlb_dma_mapping_error(handle))
  406. return NULL;
  407. ret = phys_to_virt(handle);
  408. }
  409. memset(ret, 0, size);
  410. dev_addr = virt_to_phys(ret);
  411. /* Confirm address can be DMA'd by device */
  412. if (address_needs_mapping(hwdev, dev_addr)) {
  413. printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016lx\n",
  414. (unsigned long long)*hwdev->dma_mask, dev_addr);
  415. panic("swiotlb_alloc_coherent: allocated memory is out of "
  416. "range for device");
  417. }
  418. *dma_handle = dev_addr;
  419. return ret;
  420. }
  421. void
  422. swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
  423. dma_addr_t dma_handle)
  424. {
  425. if (!(vaddr >= (void *)io_tlb_start
  426. && vaddr < (void *)io_tlb_end))
  427. free_pages((unsigned long) vaddr, get_order(size));
  428. else
  429. /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
  430. swiotlb_unmap_single (hwdev, dma_handle, size, DMA_TO_DEVICE);
  431. }
  432. static void
  433. swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
  434. {
  435. /*
  436. * Ran out of IOMMU space for this operation. This is very bad.
  437. * Unfortunately the drivers cannot handle this operation properly.
  438. * unless they check for dma_mapping_error (most don't)
  439. * When the mapping is small enough return a static buffer to limit
  440. * the damage, or panic when the transfer is too big.
  441. */
  442. printk(KERN_ERR "DMA: Out of SW-IOMMU space for %lu bytes at "
  443. "device %s\n", size, dev ? dev->bus_id : "?");
  444. if (size > io_tlb_overflow && do_panic) {
  445. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  446. panic("DMA: Memory would be corrupted\n");
  447. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
  448. panic("DMA: Random memory would be DMAed\n");
  449. }
  450. }
  451. /*
  452. * Map a single buffer of the indicated size for DMA in streaming mode. The
  453. * physical address to use is returned.
  454. *
  455. * Once the device is given the dma address, the device owns this memory until
  456. * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
  457. */
  458. dma_addr_t
  459. swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
  460. {
  461. unsigned long dev_addr = virt_to_phys(ptr);
  462. void *map;
  463. BUG_ON(dir == DMA_NONE);
  464. /*
  465. * If the pointer passed in happens to be in the device's DMA window,
  466. * we can safely return the device addr and not worry about bounce
  467. * buffering it.
  468. */
  469. if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)
  470. return dev_addr;
  471. /*
  472. * Oh well, have to allocate and map a bounce buffer.
  473. */
  474. map = map_single(hwdev, ptr, size, dir);
  475. if (!map) {
  476. swiotlb_full(hwdev, size, dir, 1);
  477. map = io_tlb_overflow_buffer;
  478. }
  479. dev_addr = virt_to_phys(map);
  480. /*
  481. * Ensure that the address returned is DMA'ble
  482. */
  483. if (address_needs_mapping(hwdev, dev_addr))
  484. panic("map_single: bounce buffer is not DMA'ble");
  485. return dev_addr;
  486. }
  487. /*
  488. * Since DMA is i-cache coherent, any (complete) pages that were written via
  489. * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
  490. * flush them when they get mapped into an executable vm-area.
  491. */
  492. static void
  493. mark_clean(void *addr, size_t size)
  494. {
  495. unsigned long pg_addr, end;
  496. pg_addr = PAGE_ALIGN((unsigned long) addr);
  497. end = (unsigned long) addr + size;
  498. while (pg_addr + PAGE_SIZE <= end) {
  499. struct page *page = virt_to_page(pg_addr);
  500. set_bit(PG_arch_1, &page->flags);
  501. pg_addr += PAGE_SIZE;
  502. }
  503. }
  504. /*
  505. * Unmap a single streaming mode DMA translation. The dma_addr and size must
  506. * match what was provided for in a previous swiotlb_map_single call. All
  507. * other usages are undefined.
  508. *
  509. * After this call, reads by the cpu to the buffer are guaranteed to see
  510. * whatever the device wrote there.
  511. */
  512. void
  513. swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
  514. int dir)
  515. {
  516. char *dma_addr = phys_to_virt(dev_addr);
  517. BUG_ON(dir == DMA_NONE);
  518. if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
  519. unmap_single(hwdev, dma_addr, size, dir);
  520. else if (dir == DMA_FROM_DEVICE)
  521. mark_clean(dma_addr, size);
  522. }
  523. /*
  524. * Make physical memory consistent for a single streaming mode DMA translation
  525. * after a transfer.
  526. *
  527. * If you perform a swiotlb_map_single() but wish to interrogate the buffer
  528. * using the cpu, yet do not wish to teardown the dma mapping, you must
  529. * call this function before doing so. At the next point you give the dma
  530. * address back to the card, you must first perform a
  531. * swiotlb_dma_sync_for_device, and then the device again owns the buffer
  532. */
  533. static inline void
  534. swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
  535. size_t size, int dir, int target)
  536. {
  537. char *dma_addr = phys_to_virt(dev_addr);
  538. BUG_ON(dir == DMA_NONE);
  539. if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
  540. sync_single(hwdev, dma_addr, size, dir, target);
  541. else if (dir == DMA_FROM_DEVICE)
  542. mark_clean(dma_addr, size);
  543. }
  544. void
  545. swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
  546. size_t size, int dir)
  547. {
  548. swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
  549. }
  550. void
  551. swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
  552. size_t size, int dir)
  553. {
  554. swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
  555. }
  556. /*
  557. * Same as above, but for a sub-range of the mapping.
  558. */
  559. static inline void
  560. swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
  561. unsigned long offset, size_t size,
  562. int dir, int target)
  563. {
  564. char *dma_addr = phys_to_virt(dev_addr) + offset;
  565. BUG_ON(dir == DMA_NONE);
  566. if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
  567. sync_single(hwdev, dma_addr, size, dir, target);
  568. else if (dir == DMA_FROM_DEVICE)
  569. mark_clean(dma_addr, size);
  570. }
  571. void
  572. swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
  573. unsigned long offset, size_t size, int dir)
  574. {
  575. swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
  576. SYNC_FOR_CPU);
  577. }
  578. void
  579. swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
  580. unsigned long offset, size_t size, int dir)
  581. {
  582. swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
  583. SYNC_FOR_DEVICE);
  584. }
  585. /*
  586. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  587. * This is the scatter-gather version of the above swiotlb_map_single
  588. * interface. Here the scatter gather list elements are each tagged with the
  589. * appropriate dma address and length. They are obtained via
  590. * sg_dma_{address,length}(SG).
  591. *
  592. * NOTE: An implementation may be able to use a smaller number of
  593. * DMA address/length pairs than there are SG table elements.
  594. * (for example via virtual mapping capabilities)
  595. * The routine returns the number of addr/length pairs actually
  596. * used, at most nents.
  597. *
  598. * Device ownership issues as mentioned above for swiotlb_map_single are the
  599. * same here.
  600. */
  601. int
  602. swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
  603. int dir)
  604. {
  605. void *addr;
  606. unsigned long dev_addr;
  607. int i;
  608. BUG_ON(dir == DMA_NONE);
  609. for (i = 0; i < nelems; i++, sg++) {
  610. addr = SG_ENT_VIRT_ADDRESS(sg);
  611. dev_addr = virt_to_phys(addr);
  612. if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
  613. void *map = map_single(hwdev, addr, sg->length, dir);
  614. sg->dma_address = virt_to_bus(map);
  615. if (!map) {
  616. /* Don't panic here, we expect map_sg users
  617. to do proper error handling. */
  618. swiotlb_full(hwdev, sg->length, dir, 0);
  619. swiotlb_unmap_sg(hwdev, sg - i, i, dir);
  620. sg[0].dma_length = 0;
  621. return 0;
  622. }
  623. } else
  624. sg->dma_address = dev_addr;
  625. sg->dma_length = sg->length;
  626. }
  627. return nelems;
  628. }
  629. /*
  630. * Unmap a set of streaming mode DMA translations. Again, cpu read rules
  631. * concerning calls here are the same as for swiotlb_unmap_single() above.
  632. */
  633. void
  634. swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
  635. int dir)
  636. {
  637. int i;
  638. BUG_ON(dir == DMA_NONE);
  639. for (i = 0; i < nelems; i++, sg++)
  640. if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
  641. unmap_single(hwdev, (void *) phys_to_virt(sg->dma_address), sg->dma_length, dir);
  642. else if (dir == DMA_FROM_DEVICE)
  643. mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
  644. }
  645. /*
  646. * Make physical memory consistent for a set of streaming mode DMA translations
  647. * after a transfer.
  648. *
  649. * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
  650. * and usage.
  651. */
  652. static inline void
  653. swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sg,
  654. int nelems, int dir, int target)
  655. {
  656. int i;
  657. BUG_ON(dir == DMA_NONE);
  658. for (i = 0; i < nelems; i++, sg++)
  659. if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
  660. sync_single(hwdev, (void *) sg->dma_address,
  661. sg->dma_length, dir, target);
  662. }
  663. void
  664. swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
  665. int nelems, int dir)
  666. {
  667. swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
  668. }
  669. void
  670. swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
  671. int nelems, int dir)
  672. {
  673. swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
  674. }
  675. int
  676. swiotlb_dma_mapping_error(dma_addr_t dma_addr)
  677. {
  678. return (dma_addr == virt_to_phys(io_tlb_overflow_buffer));
  679. }
  680. /*
  681. * Return whether the given device DMA address mask can be supported
  682. * properly. For example, if your device can only drive the low 24-bits
  683. * during bus mastering, then you would pass 0x00ffffff as the mask to
  684. * this function.
  685. */
  686. int
  687. swiotlb_dma_supported (struct device *hwdev, u64 mask)
  688. {
  689. return (virt_to_phys (io_tlb_end) - 1) <= mask;
  690. }
  691. EXPORT_SYMBOL(swiotlb_init);
  692. EXPORT_SYMBOL(swiotlb_map_single);
  693. EXPORT_SYMBOL(swiotlb_unmap_single);
  694. EXPORT_SYMBOL(swiotlb_map_sg);
  695. EXPORT_SYMBOL(swiotlb_unmap_sg);
  696. EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
  697. EXPORT_SYMBOL(swiotlb_sync_single_for_device);
  698. EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
  699. EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
  700. EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
  701. EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
  702. EXPORT_SYMBOL(swiotlb_dma_mapping_error);
  703. EXPORT_SYMBOL(swiotlb_alloc_coherent);
  704. EXPORT_SYMBOL(swiotlb_free_coherent);
  705. EXPORT_SYMBOL(swiotlb_dma_supported);