swiotlb.c 21 KB

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