blob: a0b36a9d5df149e6370fb3b6be0d5894bae72bd7 [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 Wilka5f95152012-09-22 20:01:16 -040011
Konrad Rzeszutek Wilkb8b0f552012-08-21 14:49:34 -040012#include <asm/xen/swiotlb-xen.h>
Konrad Rzeszutek Wilkfc2341d2012-07-27 20:16:00 -040013#ifdef CONFIG_X86_64
14#include <asm/iommu.h>
15#include <asm/dma.h>
16#endif
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040017#include <linux/export.h>
Konrad Rzeszutek Wilka5f95152012-09-22 20:01:16 -040018
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050019int xen_swiotlb __read_mostly;
20
21static struct dma_map_ops xen_swiotlb_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020022 .alloc = xen_swiotlb_alloc_coherent,
23 .free = xen_swiotlb_free_coherent,
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050024 .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
25 .sync_single_for_device = xen_swiotlb_sync_single_for_device,
26 .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
27 .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
28 .map_sg = xen_swiotlb_map_sg_attrs,
29 .unmap_sg = xen_swiotlb_unmap_sg_attrs,
30 .map_page = xen_swiotlb_map_page,
31 .unmap_page = xen_swiotlb_unmap_page,
32 .dma_supported = xen_swiotlb_dma_supported,
33};
34
35/*
36 * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary
37 *
38 * This returns non-zero if we are forced to use xen_swiotlb (by the boot
39 * option).
40 */
41int __init pci_xen_swiotlb_detect(void)
42{
43
Konrad Rzeszutek Wilk988f0e22012-07-27 20:10:58 -040044 if (!xen_pv_domain())
45 return 0;
46
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050047 /* If running as PV guest, either iommu=soft, or swiotlb=force will
48 * activate this IOMMU. If running as PV privileged, activate it
Justin P. Mattock70f23fd2011-05-10 10:16:21 +020049 * irregardless.
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050050 */
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +010051 if (xen_initial_domain() || swiotlb || swiotlb_force == SWIOTLB_FORCE)
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050052 xen_swiotlb = 1;
53
54 /* If we are running under Xen, we MUST disable the native SWIOTLB.
55 * Don't worry about swiotlb_force flag activating the native, as
56 * the 'swiotlb' flag is the only one turning it on. */
Konrad Rzeszutek Wilk988f0e22012-07-27 20:10:58 -040057 swiotlb = 0;
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050058
Konrad Rzeszutek Wilkfc2341d2012-07-27 20:16:00 -040059#ifdef CONFIG_X86_64
60 /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0
61 * (so no iommu=X command line over-writes).
62 * Considering that PV guests do not want the *native SWIOTLB* but
63 * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here.
64 */
65 if (max_pfn > MAX_DMA32_PFN)
66 no_iommu = 1;
67#endif
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050068 return xen_swiotlb;
69}
70
71void __init pci_xen_swiotlb_init(void)
72{
73 if (xen_swiotlb) {
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040074 xen_swiotlb_init(1, true /* early */);
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050075 dma_ops = &xen_swiotlb_dma_ops;
Konrad Rzeszutek Wilk74226b82010-08-19 13:34:58 -040076
Stefano Stabellini92c0fd12013-11-04 18:11:54 +000077#ifdef CONFIG_PCI
Konrad Rzeszutek Wilk74226b82010-08-19 13:34:58 -040078 /* Make sure ACS will be enabled */
79 pci_request_acs();
Stefano Stabellini92c0fd12013-11-04 18:11:54 +000080#endif
Konrad Rzeszutek Wilkbbbe5732010-02-09 14:30:55 -050081 }
82}
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040083
84int pci_xen_swiotlb_init_late(void)
85{
86 int rc;
87
88 if (xen_swiotlb)
89 return 0;
90
91 rc = xen_swiotlb_init(1, false /* late */);
92 if (rc)
93 return rc;
94
95 dma_ops = &xen_swiotlb_dma_ops;
Stefano Stabellini92c0fd12013-11-04 18:11:54 +000096#ifdef CONFIG_PCI
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -040097 /* Make sure ACS will be enabled */
98 pci_request_acs();
Stefano Stabellini92c0fd12013-11-04 18:11:54 +000099#endif
Konrad Rzeszutek Wilkb8277602012-08-23 14:36:15 -0400100
101 return 0;
102}
103EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
104
Konrad Rzeszutek Wilk5cb3a262010-08-26 13:58:01 -0400105IOMMU_INIT_FINISH(pci_xen_swiotlb_detect,
Konrad Rzeszutek Wilk6d7083e2012-08-13 11:00:08 -0400106 NULL,
Konrad Rzeszutek Wilk5cb3a262010-08-26 13:58:01 -0400107 pci_xen_swiotlb_init,
Konrad Rzeszutek Wilk6d7083e2012-08-13 11:00:08 -0400108 NULL);