blob: b152640d83884c2c4e80ca701bc158bb89eef726 [file] [log] [blame]
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -05001/* Glue code to lib/swiotlb-xen.c */
2
3#include <linux/dma-mapping.h>
Konrad Rzeszutek Wilk74226b82010-08-19 13:34:58 -04004#include <linux/pci.h>
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -05005#include <xen/swiotlb-xen.h>
6
7#include <asm/xen/hypervisor.h>
8#include <xen/xen.h>
Konrad Rzeszutek Wilk5cb3a262010-08-26 13:58:01 -04009#include <asm/iommu_table.h>
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050010
Konrad Rzeszutek Wilkfc2341d2012-07-27 20:16:00 -040011#ifdef CONFIG_X86_64
12#include <asm/iommu.h>
13#include <asm/dma.h>
14#endif
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040015#include <linux/export.h>
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050016int xen_swiotlb __read_mostly;
17
18static struct dma_map_ops xen_swiotlb_dma_ops = {
19 .mapping_error = xen_swiotlb_dma_mapping_error,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020020 .alloc = xen_swiotlb_alloc_coherent,
21 .free = xen_swiotlb_free_coherent,
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050022 .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
23 .sync_single_for_device = xen_swiotlb_sync_single_for_device,
24 .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
25 .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
26 .map_sg = xen_swiotlb_map_sg_attrs,
27 .unmap_sg = xen_swiotlb_unmap_sg_attrs,
28 .map_page = xen_swiotlb_map_page,
29 .unmap_page = xen_swiotlb_unmap_page,
30 .dma_supported = xen_swiotlb_dma_supported,
31};
32
33/*
34 * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary
35 *
36 * This returns non-zero if we are forced to use xen_swiotlb (by the boot
37 * option).
38 */
39int __init pci_xen_swiotlb_detect(void)
40{
41
Konrad Rzeszutek Wilk988f0e22012-07-27 20:10:58 -040042 if (!xen_pv_domain())
43 return 0;
44
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050045 /* If running as PV guest, either iommu=soft, or swiotlb=force will
46 * activate this IOMMU. If running as PV privileged, activate it
Justin P. Mattock70f23fd2011-05-10 10:16:21 +020047 * irregardless.
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050048 */
Konrad Rzeszutek Wilk988f0e22012-07-27 20:10:58 -040049 if ((xen_initial_domain() || swiotlb || swiotlb_force))
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050050 xen_swiotlb = 1;
51
52 /* If we are running under Xen, we MUST disable the native SWIOTLB.
53 * Don't worry about swiotlb_force flag activating the native, as
54 * the 'swiotlb' flag is the only one turning it on. */
Konrad Rzeszutek Wilk988f0e22012-07-27 20:10:58 -040055 swiotlb = 0;
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050056
Konrad Rzeszutek Wilkfc2341d2012-07-27 20:16:00 -040057#ifdef CONFIG_X86_64
58 /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0
59 * (so no iommu=X command line over-writes).
60 * Considering that PV guests do not want the *native SWIOTLB* but
61 * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here.
62 */
63 if (max_pfn > MAX_DMA32_PFN)
64 no_iommu = 1;
65#endif
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050066 return xen_swiotlb;
67}
68
69void __init pci_xen_swiotlb_init(void)
70{
71 if (xen_swiotlb) {
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040072 xen_swiotlb_init(1, true /* early */);
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050073 dma_ops = &xen_swiotlb_dma_ops;
Konrad Rzeszutek Wilk74226b82010-08-19 13:34:58 -040074
75 /* Make sure ACS will be enabled */
76 pci_request_acs();
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050077 }
78}
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040079
80int pci_xen_swiotlb_init_late(void)
81{
82 int rc;
83
84 if (xen_swiotlb)
85 return 0;
86
87 rc = xen_swiotlb_init(1, false /* late */);
88 if (rc)
89 return rc;
90
91 dma_ops = &xen_swiotlb_dma_ops;
92 /* Make sure ACS will be enabled */
93 pci_request_acs();
94
95 return 0;
96}
97EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
98
Konrad Rzeszutek Wilk5cb3a262010-08-26 13:58:01 -040099IOMMU_INIT_FINISH(pci_xen_swiotlb_detect,
100 0,
101 pci_xen_swiotlb_init,
102 0);