blob: 5069ef560d830e303f203e54dbe4d6407721f1e2 [file] [log] [blame]
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +01001/* Glue code to lib/swiotlb.c */
2
3#include <linux/pci.h>
4#include <linux/cache.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -04005#include <linux/init.h>
Jeremy Fitzhardinge8ce79962008-12-16 12:17:35 -08006#include <linux/swiotlb.h>
7#include <linux/bootmem.h>
Rolf Eike Beerd6bd3a32006-09-29 01:59:48 -07008#include <linux/dma-mapping.h>
9
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090010#include <asm/iommu.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010011#include <asm/swiotlb.h>
12#include <asm/dma.h>
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040013#include <asm/xen/swiotlb-xen.h>
14#include <asm/iommu_table.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010015int swiotlb __read_mostly;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010016
Akinobu Mita9c5a3622014-06-04 16:06:50 -070017void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020018 dma_addr_t *dma_handle, gfp_t flags,
19 struct dma_attrs *attrs)
FUJITA Tomonori03967c52008-10-23 23:14:29 +090020{
21 void *vaddr;
22
Joerg Roedel186dfc92015-06-10 17:49:41 +020023 /*
24 * Don't print a warning when the first allocation attempt fails.
25 * swiotlb_alloc_coherent() will print a warning when the DMA
26 * memory allocation ultimately failed.
27 */
28 flags |= __GFP_NOWARN;
29
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020030 vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags,
31 attrs);
FUJITA Tomonori03967c52008-10-23 23:14:29 +090032 if (vaddr)
33 return vaddr;
34
35 return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
36}
37
Akinobu Mita9c5a3622014-06-04 16:06:50 -070038void x86_swiotlb_free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020039 void *vaddr, dma_addr_t dma_addr,
40 struct dma_attrs *attrs)
41{
Akinobu Mita9c5a3622014-06-04 16:06:50 -070042 if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr)))
43 swiotlb_free_coherent(dev, size, vaddr, dma_addr);
44 else
45 dma_generic_free_coherent(dev, size, vaddr, dma_addr, attrs);
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020046}
47
Jaswinder Singh Rajputff6c6fed2009-04-12 23:24:21 +053048static struct dma_map_ops swiotlb_dma_ops = {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010049 .mapping_error = swiotlb_dma_mapping_error,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020050 .alloc = x86_swiotlb_alloc_coherent,
51 .free = x86_swiotlb_free_coherent,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010052 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
53 .sync_single_for_device = swiotlb_sync_single_for_device,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010054 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
55 .sync_sg_for_device = swiotlb_sync_sg_for_device,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090056 .map_sg = swiotlb_map_sg_attrs,
57 .unmap_sg = swiotlb_unmap_sg_attrs,
FUJITA Tomonori4cf37bb2009-01-05 23:47:22 +090058 .map_page = swiotlb_map_page,
59 .unmap_page = swiotlb_unmap_page,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010060 .dma_supported = NULL,
61};
62
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090063/*
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040064 * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
FUJITA Tomonorib18485e2009-11-12 00:03:28 +090065 *
66 * This returns non-zero if we are forced to use swiotlb (by the boot
67 * option).
68 */
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040069int __init pci_swiotlb_detect_override(void)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010070{
FUJITA Tomonori273bee22009-11-25 08:46:28 +090071 int use_swiotlb = swiotlb | swiotlb_force;
72
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040073 if (swiotlb_force)
74 swiotlb = 1;
75
76 return use_swiotlb;
77}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040078IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
79 pci_xen_swiotlb_detect,
80 pci_swiotlb_init,
81 pci_swiotlb_late_init);
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040082
83/*
84 * if 4GB or more detected (and iommu=off not set) return 1
85 * and set swiotlb to 1.
86 */
87int __init pci_swiotlb_detect_4gb(void)
88{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010089 /* don't initialize swiotlb if iommu=off (no_iommu=1) */
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080090#ifdef CONFIG_X86_64
Igor Mammedovec941c52015-12-04 14:07:06 +010091 if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
David Woodhouse19943b02009-08-04 16:19:20 +010092 swiotlb = 1;
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080093#endif
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -040094 return swiotlb;
FUJITA Tomonori186a2502009-12-15 20:47:56 +090095}
Konrad Rzeszutek Wilkc116c542010-08-26 13:58:00 -040096IOMMU_INIT(pci_swiotlb_detect_4gb,
97 pci_swiotlb_detect_override,
98 pci_swiotlb_init,
99 pci_swiotlb_late_init);
FUJITA Tomonori186a2502009-12-15 20:47:56 +0900100
101void __init pci_swiotlb_init(void)
102{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100103 if (swiotlb) {
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900104 swiotlb_init(0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100105 dma_ops = &swiotlb_dma_ops;
FUJITA Tomonoria3b28ee2009-11-14 20:46:36 +0900106 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100107}
Konrad Rzeszutek Wilkefa631c2010-08-26 13:57:59 -0400108
109void __init pci_swiotlb_late_init(void)
110{
111 /* An IOMMU turned us off. */
112 if (!swiotlb)
113 swiotlb_free();
114 else {
115 printk(KERN_INFO "PCI-DMA: "
116 "Using software bounce buffering for IO (SWIOTLB)\n");
117 swiotlb_print_info();
118 }
119}