Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic DMA mapping support. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/dmar.h> |
| 11 | #include <asm/iommu.h> |
| 12 | #include <asm/machvec.h> |
| 13 | #include <linux/dma-mapping.h> |
| 14 | |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 15 | #include <asm/system.h> |
| 16 | |
| 17 | #ifdef CONFIG_DMAR |
| 18 | |
| 19 | #include <linux/kernel.h> |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/page.h> |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 22 | |
| 23 | dma_addr_t bad_dma_address __read_mostly; |
| 24 | EXPORT_SYMBOL(bad_dma_address); |
| 25 | |
| 26 | static int iommu_sac_force __read_mostly; |
| 27 | |
| 28 | int no_iommu __read_mostly; |
| 29 | #ifdef CONFIG_IOMMU_DEBUG |
| 30 | int force_iommu __read_mostly = 1; |
| 31 | #else |
| 32 | int force_iommu __read_mostly; |
| 33 | #endif |
| 34 | |
Fenghua Yu | aed5d5f | 2009-04-30 17:57:11 -0700 | [diff] [blame^] | 35 | int iommu_pass_through; |
| 36 | |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 37 | /* Dummy device used for NULL arguments (normally ISA). Better would |
| 38 | be probably a smaller DMA mask, but this is bug-to-bug compatible |
| 39 | to i386. */ |
| 40 | struct device fallback_dev = { |
Kay Sievers | 48ef2bb | 2009-01-06 10:44:40 -0800 | [diff] [blame] | 41 | .init_name = "fallback device", |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 42 | .coherent_dma_mask = DMA_BIT_MASK(32), |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 43 | .dma_mask = &fallback_dev.coherent_dma_mask, |
| 44 | }; |
| 45 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 46 | extern struct dma_map_ops intel_dma_ops; |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 47 | |
| 48 | static int __init pci_iommu_init(void) |
| 49 | { |
| 50 | if (iommu_detected) |
| 51 | intel_iommu_init(); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* Must execute after PCI subsystem */ |
| 57 | fs_initcall(pci_iommu_init); |
| 58 | |
| 59 | void pci_iommu_shutdown(void) |
| 60 | { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | void __init |
| 65 | iommu_dma_init(void) |
| 66 | { |
| 67 | return; |
| 68 | } |
| 69 | |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 70 | int iommu_dma_supported(struct device *dev, u64 mask) |
| 71 | { |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 72 | struct dma_map_ops *ops = platform_dma_get_ops(dev); |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 73 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 74 | if (ops->dma_supported) |
| 75 | return ops->dma_supported(dev, mask); |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 76 | |
| 77 | /* Copied from i386. Doesn't make much sense, because it will |
| 78 | only work for pci_alloc_coherent. |
| 79 | The caller just has to use GFP_DMA in this case. */ |
Yang Hongyang | 2f4f27d | 2009-04-06 19:01:18 -0700 | [diff] [blame] | 80 | if (mask < DMA_BIT_MASK(24)) |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | /* Tell the device to use SAC when IOMMU force is on. This |
| 84 | allows the driver to use cheaper accesses in some cases. |
| 85 | |
| 86 | Problem with this is that if we overflow the IOMMU area and |
| 87 | return DAC as fallback address the device may not handle it |
| 88 | correctly. |
| 89 | |
| 90 | As a special case some controllers have a 39bit address |
| 91 | mode that is as efficient as 32bit (aic79xx). Don't force |
| 92 | SAC for these. Assume all masks <= 40 bits are of this |
| 93 | type. Normally this doesn't make any difference, but gives |
| 94 | more gentle handling of IOMMU overflow. */ |
Yang Hongyang | 50cf156 | 2009-04-06 19:01:14 -0700 | [diff] [blame] | 95 | if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) { |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 96 | dev_info(dev, "Force SAC with mask %lx\n", mask); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | return 1; |
| 101 | } |
| 102 | EXPORT_SYMBOL(iommu_dma_supported); |
| 103 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 104 | void __init pci_iommu_alloc(void) |
| 105 | { |
| 106 | dma_ops = &intel_dma_ops; |
| 107 | |
| 108 | dma_ops->sync_single_for_cpu = machvec_dma_sync_single; |
| 109 | dma_ops->sync_sg_for_cpu = machvec_dma_sync_sg; |
| 110 | dma_ops->sync_single_for_device = machvec_dma_sync_single; |
| 111 | dma_ops->sync_sg_for_device = machvec_dma_sync_sg; |
| 112 | dma_ops->dma_supported = iommu_dma_supported; |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * The order of these functions is important for |
| 116 | * fall-back/fail-over reasons |
| 117 | */ |
| 118 | detect_intel_iommu(); |
| 119 | |
| 120 | #ifdef CONFIG_SWIOTLB |
| 121 | pci_swiotlb_init(); |
| 122 | #endif |
| 123 | } |
| 124 | |
Fenghua Yu | 62fdd76 | 2008-10-17 12:14:13 -0700 | [diff] [blame] | 125 | #endif |