Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 1 | /* Glue code to lib/swiotlb-xen.c */ |
| 2 | |
| 3 | #include <linux/dma-mapping.h> |
Konrad Rzeszutek Wilk | 74226b8 | 2010-08-19 13:34:58 -0400 | [diff] [blame] | 4 | #include <linux/pci.h> |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 5 | #include <xen/swiotlb-xen.h> |
| 6 | |
| 7 | #include <asm/xen/hypervisor.h> |
| 8 | #include <xen/xen.h> |
Konrad Rzeszutek Wilk | 5cb3a26 | 2010-08-26 13:58:01 -0400 | [diff] [blame] | 9 | #include <asm/iommu_table.h> |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 10 | |
Konrad Rzeszutek Wilk | a5f9515 | 2012-09-22 20:01:16 -0400 | [diff] [blame] | 11 | |
Konrad Rzeszutek Wilk | b8b0f55 | 2012-08-21 14:49:34 -0400 | [diff] [blame] | 12 | #include <asm/xen/swiotlb-xen.h> |
Konrad Rzeszutek Wilk | fc2341d | 2012-07-27 20:16:00 -0400 | [diff] [blame] | 13 | #ifdef CONFIG_X86_64 |
| 14 | #include <asm/iommu.h> |
| 15 | #include <asm/dma.h> |
| 16 | #endif |
Konrad Rzeszutek Wilk | b827760 | 2012-08-23 14:36:15 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Konrad Rzeszutek Wilk | a5f9515 | 2012-09-22 20:01:16 -0400 | [diff] [blame] | 18 | |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 19 | int xen_swiotlb __read_mostly; |
| 20 | |
| 21 | static struct dma_map_ops xen_swiotlb_dma_ops = { |
| 22 | .mapping_error = xen_swiotlb_dma_mapping_error, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 23 | .alloc = xen_swiotlb_alloc_coherent, |
| 24 | .free = xen_swiotlb_free_coherent, |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 25 | .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu, |
| 26 | .sync_single_for_device = xen_swiotlb_sync_single_for_device, |
| 27 | .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu, |
| 28 | .sync_sg_for_device = xen_swiotlb_sync_sg_for_device, |
| 29 | .map_sg = xen_swiotlb_map_sg_attrs, |
| 30 | .unmap_sg = xen_swiotlb_unmap_sg_attrs, |
| 31 | .map_page = xen_swiotlb_map_page, |
| 32 | .unmap_page = xen_swiotlb_unmap_page, |
| 33 | .dma_supported = xen_swiotlb_dma_supported, |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary |
| 38 | * |
| 39 | * This returns non-zero if we are forced to use xen_swiotlb (by the boot |
| 40 | * option). |
| 41 | */ |
| 42 | int __init pci_xen_swiotlb_detect(void) |
| 43 | { |
| 44 | |
Konrad Rzeszutek Wilk | 988f0e2 | 2012-07-27 20:10:58 -0400 | [diff] [blame] | 45 | if (!xen_pv_domain()) |
| 46 | return 0; |
| 47 | |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 48 | /* If running as PV guest, either iommu=soft, or swiotlb=force will |
| 49 | * activate this IOMMU. If running as PV privileged, activate it |
Justin P. Mattock | 70f23fd | 2011-05-10 10:16:21 +0200 | [diff] [blame] | 50 | * irregardless. |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 51 | */ |
Konrad Rzeszutek Wilk | 988f0e2 | 2012-07-27 20:10:58 -0400 | [diff] [blame] | 52 | if ((xen_initial_domain() || swiotlb || swiotlb_force)) |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 53 | xen_swiotlb = 1; |
| 54 | |
| 55 | /* If we are running under Xen, we MUST disable the native SWIOTLB. |
| 56 | * Don't worry about swiotlb_force flag activating the native, as |
| 57 | * the 'swiotlb' flag is the only one turning it on. */ |
Konrad Rzeszutek Wilk | 988f0e2 | 2012-07-27 20:10:58 -0400 | [diff] [blame] | 58 | swiotlb = 0; |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 59 | |
Konrad Rzeszutek Wilk | fc2341d | 2012-07-27 20:16:00 -0400 | [diff] [blame] | 60 | #ifdef CONFIG_X86_64 |
| 61 | /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0 |
| 62 | * (so no iommu=X command line over-writes). |
| 63 | * Considering that PV guests do not want the *native SWIOTLB* but |
| 64 | * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here. |
| 65 | */ |
| 66 | if (max_pfn > MAX_DMA32_PFN) |
| 67 | no_iommu = 1; |
| 68 | #endif |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 69 | return xen_swiotlb; |
| 70 | } |
| 71 | |
| 72 | void __init pci_xen_swiotlb_init(void) |
| 73 | { |
| 74 | if (xen_swiotlb) { |
Konrad Rzeszutek Wilk | b827760 | 2012-08-23 14:36:15 -0400 | [diff] [blame] | 75 | xen_swiotlb_init(1, true /* early */); |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 76 | dma_ops = &xen_swiotlb_dma_ops; |
Konrad Rzeszutek Wilk | 74226b8 | 2010-08-19 13:34:58 -0400 | [diff] [blame] | 77 | |
Stefano Stabellini | 92c0fd1 | 2013-11-04 18:11:54 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_PCI |
Konrad Rzeszutek Wilk | 74226b8 | 2010-08-19 13:34:58 -0400 | [diff] [blame] | 79 | /* Make sure ACS will be enabled */ |
| 80 | pci_request_acs(); |
Stefano Stabellini | 92c0fd1 | 2013-11-04 18:11:54 +0000 | [diff] [blame] | 81 | #endif |
Konrad Rzeszutek Wilk | bbbe573 | 2010-02-09 14:30:55 -0500 | [diff] [blame] | 82 | } |
| 83 | } |
Konrad Rzeszutek Wilk | b827760 | 2012-08-23 14:36:15 -0400 | [diff] [blame] | 84 | |
| 85 | int pci_xen_swiotlb_init_late(void) |
| 86 | { |
| 87 | int rc; |
| 88 | |
| 89 | if (xen_swiotlb) |
| 90 | return 0; |
| 91 | |
| 92 | rc = xen_swiotlb_init(1, false /* late */); |
| 93 | if (rc) |
| 94 | return rc; |
| 95 | |
| 96 | dma_ops = &xen_swiotlb_dma_ops; |
Stefano Stabellini | 92c0fd1 | 2013-11-04 18:11:54 +0000 | [diff] [blame] | 97 | #ifdef CONFIG_PCI |
Konrad Rzeszutek Wilk | b827760 | 2012-08-23 14:36:15 -0400 | [diff] [blame] | 98 | /* Make sure ACS will be enabled */ |
| 99 | pci_request_acs(); |
Stefano Stabellini | 92c0fd1 | 2013-11-04 18:11:54 +0000 | [diff] [blame] | 100 | #endif |
Konrad Rzeszutek Wilk | b827760 | 2012-08-23 14:36:15 -0400 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late); |
| 105 | |
Konrad Rzeszutek Wilk | 5cb3a26 | 2010-08-26 13:58:01 -0400 | [diff] [blame] | 106 | IOMMU_INIT_FINISH(pci_xen_swiotlb_detect, |
Konrad Rzeszutek Wilk | 6d7083e | 2012-08-13 11:00:08 -0400 | [diff] [blame] | 107 | NULL, |
Konrad Rzeszutek Wilk | 5cb3a26 | 2010-08-26 13:58:01 -0400 | [diff] [blame] | 108 | pci_xen_swiotlb_init, |
Konrad Rzeszutek Wilk | 6d7083e | 2012-08-13 11:00:08 -0400 | [diff] [blame] | 109 | NULL); |