1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun /* Glue code to lib/swiotlb-xen.c */
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/dma-map-ops.h>
6*4882a593Smuzhiyun #include <linux/pci.h>
7*4882a593Smuzhiyun #include <xen/swiotlb-xen.h>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <asm/xen/hypervisor.h>
10*4882a593Smuzhiyun #include <xen/xen.h>
11*4882a593Smuzhiyun #include <asm/iommu_table.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <asm/xen/swiotlb-xen.h>
15*4882a593Smuzhiyun #ifdef CONFIG_X86_64
16*4882a593Smuzhiyun #include <asm/iommu.h>
17*4882a593Smuzhiyun #include <asm/dma.h>
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun #include <linux/export.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun int xen_swiotlb __read_mostly;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * This returns non-zero if we are forced to use xen_swiotlb (by the boot
27*4882a593Smuzhiyun * option).
28*4882a593Smuzhiyun */
pci_xen_swiotlb_detect(void)29*4882a593Smuzhiyun int __init pci_xen_swiotlb_detect(void)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun if (!xen_pv_domain())
33*4882a593Smuzhiyun return 0;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* If running as PV guest, either iommu=soft, or swiotlb=force will
36*4882a593Smuzhiyun * activate this IOMMU. If running as PV privileged, activate it
37*4882a593Smuzhiyun * irregardless.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun if (xen_initial_domain() || swiotlb || swiotlb_force == SWIOTLB_FORCE)
40*4882a593Smuzhiyun xen_swiotlb = 1;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* If we are running under Xen, we MUST disable the native SWIOTLB.
43*4882a593Smuzhiyun * Don't worry about swiotlb_force flag activating the native, as
44*4882a593Smuzhiyun * the 'swiotlb' flag is the only one turning it on. */
45*4882a593Smuzhiyun swiotlb = 0;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #ifdef CONFIG_X86_64
48*4882a593Smuzhiyun /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0
49*4882a593Smuzhiyun * (so no iommu=X command line over-writes).
50*4882a593Smuzhiyun * Considering that PV guests do not want the *native SWIOTLB* but
51*4882a593Smuzhiyun * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here.
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun if (max_pfn > MAX_DMA32_PFN)
54*4882a593Smuzhiyun no_iommu = 1;
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun return xen_swiotlb;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
pci_xen_swiotlb_init(void)59*4882a593Smuzhiyun void __init pci_xen_swiotlb_init(void)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun if (xen_swiotlb) {
62*4882a593Smuzhiyun xen_swiotlb_init(1, true /* early */);
63*4882a593Smuzhiyun dma_ops = &xen_swiotlb_dma_ops;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #ifdef CONFIG_PCI
66*4882a593Smuzhiyun /* Make sure ACS will be enabled */
67*4882a593Smuzhiyun pci_request_acs();
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
pci_xen_swiotlb_init_late(void)72*4882a593Smuzhiyun int pci_xen_swiotlb_init_late(void)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun int rc;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (xen_swiotlb)
77*4882a593Smuzhiyun return 0;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun rc = xen_swiotlb_init(1, false /* late */);
80*4882a593Smuzhiyun if (rc)
81*4882a593Smuzhiyun return rc;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun dma_ops = &xen_swiotlb_dma_ops;
84*4882a593Smuzhiyun #ifdef CONFIG_PCI
85*4882a593Smuzhiyun /* Make sure ACS will be enabled */
86*4882a593Smuzhiyun pci_request_acs();
87*4882a593Smuzhiyun #endif
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun IOMMU_INIT_FINISH(pci_xen_swiotlb_detect,
94*4882a593Smuzhiyun NULL,
95*4882a593Smuzhiyun pci_xen_swiotlb_init,
96*4882a593Smuzhiyun NULL);
97