diff options
author | Joerg Roedel <joerg.roedel@amd.com> | 2011-12-01 09:49:45 -0500 |
---|---|---|
committer | Joerg Roedel <joerg.roedel@amd.com> | 2011-12-12 08:54:58 -0500 |
commit | 5abcdba4fa535c29f736455e37229ee97e0e7f5d (patch) | |
tree | 228892cf94773b3215fa588fe8e5fc617afa7591 | |
parent | 400a28a05f2cc1a311acb4ff6ac64d8402d21678 (diff) |
iommu/amd: Put IOMMUv2 capable devices in pt_domain
If the device starts to use IOMMUv2 features the dma handles
need to stay valid. The only sane way to do this is to use a
identity mapping for the device and not translate it by the
iommu. This is implemented with this patch. Since this lifts
the device-isolation there is also a new kernel parameter
which allows to disable that feature.
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
-rw-r--r-- | Documentation/kernel-parameters.txt | 5 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu.c | 94 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu_init.c | 4 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu_types.h | 4 |
4 files changed, 91 insertions, 16 deletions
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 81c287fad79d..22152ed148a5 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt | |||
@@ -329,6 +329,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. | |||
329 | is a lot of faster | 329 | is a lot of faster |
330 | off - do not initialize any AMD IOMMU found in | 330 | off - do not initialize any AMD IOMMU found in |
331 | the system | 331 | the system |
332 | force_isolation - Force device isolation for all | ||
333 | devices. The IOMMU driver is not | ||
334 | allowed anymore to lift isolation | ||
335 | requirements as needed. This option | ||
336 | does not override iommu=pt | ||
332 | 337 | ||
333 | amijoy.map= [HW,JOY] Amiga joystick support | 338 | amijoy.map= [HW,JOY] Amiga joystick support |
334 | Map of devices attached to JOY0DAT and JOY1DAT | 339 | Map of devices attached to JOY0DAT and JOY1DAT |
diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 661e2bb4ac15..7ccfc80ceb7a 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c | |||
@@ -67,6 +67,7 @@ struct iommu_cmd { | |||
67 | }; | 67 | }; |
68 | 68 | ||
69 | static void update_domain(struct protection_domain *domain); | 69 | static void update_domain(struct protection_domain *domain); |
70 | static int __init alloc_passthrough_domain(void); | ||
70 | 71 | ||
71 | /**************************************************************************** | 72 | /**************************************************************************** |
72 | * | 73 | * |
@@ -147,6 +148,24 @@ static struct iommu_dev_data *get_dev_data(struct device *dev) | |||
147 | return dev->archdata.iommu; | 148 | return dev->archdata.iommu; |
148 | } | 149 | } |
149 | 150 | ||
151 | static bool pci_iommuv2_capable(struct pci_dev *pdev) | ||
152 | { | ||
153 | static const int caps[] = { | ||
154 | PCI_EXT_CAP_ID_ATS, | ||
155 | PCI_PRI_CAP, | ||
156 | PCI_PASID_CAP, | ||
157 | }; | ||
158 | int i, pos; | ||
159 | |||
160 | for (i = 0; i < 3; ++i) { | ||
161 | pos = pci_find_ext_capability(pdev, caps[i]); | ||
162 | if (pos == 0) | ||
163 | return false; | ||
164 | } | ||
165 | |||
166 | return true; | ||
167 | } | ||
168 | |||
150 | /* | 169 | /* |
151 | * In this function the list of preallocated protection domains is traversed to | 170 | * In this function the list of preallocated protection domains is traversed to |
152 | * find the domain for a specific device | 171 | * find the domain for a specific device |
@@ -204,6 +223,7 @@ static bool check_device(struct device *dev) | |||
204 | 223 | ||
205 | static int iommu_init_device(struct device *dev) | 224 | static int iommu_init_device(struct device *dev) |
206 | { | 225 | { |
226 | struct pci_dev *pdev = to_pci_dev(dev); | ||
207 | struct iommu_dev_data *dev_data; | 227 | struct iommu_dev_data *dev_data; |
208 | u16 alias; | 228 | u16 alias; |
209 | 229 | ||
@@ -228,6 +248,13 @@ static int iommu_init_device(struct device *dev) | |||
228 | dev_data->alias_data = alias_data; | 248 | dev_data->alias_data = alias_data; |
229 | } | 249 | } |
230 | 250 | ||
251 | if (pci_iommuv2_capable(pdev)) { | ||
252 | struct amd_iommu *iommu; | ||
253 | |||
254 | iommu = amd_iommu_rlookup_table[dev_data->devid]; | ||
255 | dev_data->iommu_v2 = iommu->is_iommu_v2; | ||
256 | } | ||
257 | |||
231 | dev->archdata.iommu = dev_data; | 258 | dev->archdata.iommu = dev_data; |
232 | 259 | ||
233 | return 0; | 260 | return 0; |
@@ -1762,7 +1789,7 @@ static void __detach_device(struct iommu_dev_data *dev_data) | |||
1762 | * passthrough domain if it is detached from any other domain. | 1789 | * passthrough domain if it is detached from any other domain. |
1763 | * Make sure we can deassign from the pt_domain itself. | 1790 | * Make sure we can deassign from the pt_domain itself. |
1764 | */ | 1791 | */ |
1765 | if (iommu_pass_through && | 1792 | if (dev_data->passthrough && |
1766 | (dev_data->domain == NULL && domain != pt_domain)) | 1793 | (dev_data->domain == NULL && domain != pt_domain)) |
1767 | __attach_device(dev_data, pt_domain); | 1794 | __attach_device(dev_data, pt_domain); |
1768 | } | 1795 | } |
@@ -1820,18 +1847,20 @@ static struct protection_domain *domain_for_device(struct device *dev) | |||
1820 | static int device_change_notifier(struct notifier_block *nb, | 1847 | static int device_change_notifier(struct notifier_block *nb, |
1821 | unsigned long action, void *data) | 1848 | unsigned long action, void *data) |
1822 | { | 1849 | { |
1823 | struct device *dev = data; | ||
1824 | u16 devid; | ||
1825 | struct protection_domain *domain; | ||
1826 | struct dma_ops_domain *dma_domain; | 1850 | struct dma_ops_domain *dma_domain; |
1851 | struct protection_domain *domain; | ||
1852 | struct iommu_dev_data *dev_data; | ||
1853 | struct device *dev = data; | ||
1827 | struct amd_iommu *iommu; | 1854 | struct amd_iommu *iommu; |
1828 | unsigned long flags; | 1855 | unsigned long flags; |
1856 | u16 devid; | ||
1829 | 1857 | ||
1830 | if (!check_device(dev)) | 1858 | if (!check_device(dev)) |
1831 | return 0; | 1859 | return 0; |
1832 | 1860 | ||
1833 | devid = get_device_id(dev); | 1861 | devid = get_device_id(dev); |
1834 | iommu = amd_iommu_rlookup_table[devid]; | 1862 | iommu = amd_iommu_rlookup_table[devid]; |
1863 | dev_data = get_dev_data(dev); | ||
1835 | 1864 | ||
1836 | switch (action) { | 1865 | switch (action) { |
1837 | case BUS_NOTIFY_UNBOUND_DRIVER: | 1866 | case BUS_NOTIFY_UNBOUND_DRIVER: |
@@ -1840,7 +1869,7 @@ static int device_change_notifier(struct notifier_block *nb, | |||
1840 | 1869 | ||
1841 | if (!domain) | 1870 | if (!domain) |
1842 | goto out; | 1871 | goto out; |
1843 | if (iommu_pass_through) | 1872 | if (dev_data->passthrough) |
1844 | break; | 1873 | break; |
1845 | detach_device(dev); | 1874 | detach_device(dev); |
1846 | break; | 1875 | break; |
@@ -2436,8 +2465,9 @@ static int amd_iommu_dma_supported(struct device *dev, u64 mask) | |||
2436 | */ | 2465 | */ |
2437 | static void prealloc_protection_domains(void) | 2466 | static void prealloc_protection_domains(void) |
2438 | { | 2467 | { |
2439 | struct pci_dev *dev = NULL; | 2468 | struct iommu_dev_data *dev_data; |
2440 | struct dma_ops_domain *dma_dom; | 2469 | struct dma_ops_domain *dma_dom; |
2470 | struct pci_dev *dev = NULL; | ||
2441 | u16 devid; | 2471 | u16 devid; |
2442 | 2472 | ||
2443 | for_each_pci_dev(dev) { | 2473 | for_each_pci_dev(dev) { |
@@ -2446,6 +2476,16 @@ static void prealloc_protection_domains(void) | |||
2446 | if (!check_device(&dev->dev)) | 2476 | if (!check_device(&dev->dev)) |
2447 | continue; | 2477 | continue; |
2448 | 2478 | ||
2479 | dev_data = get_dev_data(&dev->dev); | ||
2480 | if (!amd_iommu_force_isolation && dev_data->iommu_v2) { | ||
2481 | /* Make sure passthrough domain is allocated */ | ||
2482 | alloc_passthrough_domain(); | ||
2483 | dev_data->passthrough = true; | ||
2484 | attach_device(&dev->dev, pt_domain); | ||
2485 | pr_info("AMD-Vi: Using passthough domain for device %s\n", | ||
2486 | dev_name(&dev->dev)); | ||
2487 | } | ||
2488 | |||
2449 | /* Is there already any domain for it? */ | 2489 | /* Is there already any domain for it? */ |
2450 | if (domain_for_device(&dev->dev)) | 2490 | if (domain_for_device(&dev->dev)) |
2451 | continue; | 2491 | continue; |
@@ -2476,6 +2516,7 @@ static struct dma_map_ops amd_iommu_dma_ops = { | |||
2476 | 2516 | ||
2477 | static unsigned device_dma_ops_init(void) | 2517 | static unsigned device_dma_ops_init(void) |
2478 | { | 2518 | { |
2519 | struct iommu_dev_data *dev_data; | ||
2479 | struct pci_dev *pdev = NULL; | 2520 | struct pci_dev *pdev = NULL; |
2480 | unsigned unhandled = 0; | 2521 | unsigned unhandled = 0; |
2481 | 2522 | ||
@@ -2485,7 +2526,12 @@ static unsigned device_dma_ops_init(void) | |||
2485 | continue; | 2526 | continue; |
2486 | } | 2527 | } |
2487 | 2528 | ||
2488 | pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops; | 2529 | dev_data = get_dev_data(&pdev->dev); |
2530 | |||
2531 | if (!dev_data->passthrough) | ||
2532 | pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops; | ||
2533 | else | ||
2534 | pdev->dev.archdata.dma_ops = &nommu_dma_ops; | ||
2489 | } | 2535 | } |
2490 | 2536 | ||
2491 | return unhandled; | 2537 | return unhandled; |
@@ -2612,6 +2658,20 @@ out_err: | |||
2612 | return NULL; | 2658 | return NULL; |
2613 | } | 2659 | } |
2614 | 2660 | ||
2661 | static int __init alloc_passthrough_domain(void) | ||
2662 | { | ||
2663 | if (pt_domain != NULL) | ||
2664 | return 0; | ||
2665 | |||
2666 | /* allocate passthrough domain */ | ||
2667 | pt_domain = protection_domain_alloc(); | ||
2668 | if (!pt_domain) | ||
2669 | return -ENOMEM; | ||
2670 | |||
2671 | pt_domain->mode = PAGE_MODE_NONE; | ||
2672 | |||
2673 | return 0; | ||
2674 | } | ||
2615 | static int amd_iommu_domain_init(struct iommu_domain *dom) | 2675 | static int amd_iommu_domain_init(struct iommu_domain *dom) |
2616 | { | 2676 | { |
2617 | struct protection_domain *domain; | 2677 | struct protection_domain *domain; |
@@ -2798,21 +2858,23 @@ static struct iommu_ops amd_iommu_ops = { | |||
2798 | 2858 | ||
2799 | int __init amd_iommu_init_passthrough(void) | 2859 | int __init amd_iommu_init_passthrough(void) |
2800 | { | 2860 | { |
2801 | struct amd_iommu *iommu; | 2861 | struct iommu_dev_data *dev_data; |
2802 | struct pci_dev *dev = NULL; | 2862 | struct pci_dev *dev = NULL; |
2863 | struct amd_iommu *iommu; | ||
2803 | u16 devid; | 2864 | u16 devid; |
2865 | int ret; | ||
2804 | 2866 | ||
2805 | /* allocate passthrough domain */ | 2867 | ret = alloc_passthrough_domain(); |
2806 | pt_domain = protection_domain_alloc(); | 2868 | if (ret) |
2807 | if (!pt_domain) | 2869 | return ret; |
2808 | return -ENOMEM; | ||
2809 | |||
2810 | pt_domain->mode |= PAGE_MODE_NONE; | ||
2811 | 2870 | ||
2812 | for_each_pci_dev(dev) { | 2871 | for_each_pci_dev(dev) { |
2813 | if (!check_device(&dev->dev)) | 2872 | if (!check_device(&dev->dev)) |
2814 | continue; | 2873 | continue; |
2815 | 2874 | ||
2875 | dev_data = get_dev_data(&dev->dev); | ||
2876 | dev_data->passthrough = true; | ||
2877 | |||
2816 | devid = get_device_id(&dev->dev); | 2878 | devid = get_device_id(&dev->dev); |
2817 | 2879 | ||
2818 | iommu = amd_iommu_rlookup_table[devid]; | 2880 | iommu = amd_iommu_rlookup_table[devid]; |
diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c index d1e5067852d4..7c3fd572a23b 100644 --- a/drivers/iommu/amd_iommu_init.c +++ b/drivers/iommu/amd_iommu_init.c | |||
@@ -146,6 +146,8 @@ u32 amd_iommu_max_pasids __read_mostly = ~0; | |||
146 | 146 | ||
147 | bool amd_iommu_v2_present __read_mostly; | 147 | bool amd_iommu_v2_present __read_mostly; |
148 | 148 | ||
149 | bool amd_iommu_force_isolation __read_mostly; | ||
150 | |||
149 | /* | 151 | /* |
150 | * The ACPI table parsing functions set this variable on an error | 152 | * The ACPI table parsing functions set this variable on an error |
151 | */ | 153 | */ |
@@ -1642,6 +1644,8 @@ static int __init parse_amd_iommu_options(char *str) | |||
1642 | amd_iommu_unmap_flush = true; | 1644 | amd_iommu_unmap_flush = true; |
1643 | if (strncmp(str, "off", 3) == 0) | 1645 | if (strncmp(str, "off", 3) == 0) |
1644 | amd_iommu_disabled = true; | 1646 | amd_iommu_disabled = true; |
1647 | if (strncmp(str, "force_isolation", 15) == 0) | ||
1648 | amd_iommu_force_isolation = true; | ||
1645 | } | 1649 | } |
1646 | 1650 | ||
1647 | return 1; | 1651 | return 1; |
diff --git a/drivers/iommu/amd_iommu_types.h b/drivers/iommu/amd_iommu_types.h index 7e81094ab73b..96c652fae0e5 100644 --- a/drivers/iommu/amd_iommu_types.h +++ b/drivers/iommu/amd_iommu_types.h | |||
@@ -330,6 +330,8 @@ struct iommu_dev_data { | |||
330 | struct protection_domain *domain; /* Domain the device is bound to */ | 330 | struct protection_domain *domain; /* Domain the device is bound to */ |
331 | atomic_t bind; /* Domain attach reverent count */ | 331 | atomic_t bind; /* Domain attach reverent count */ |
332 | u16 devid; /* PCI Device ID */ | 332 | u16 devid; /* PCI Device ID */ |
333 | bool iommu_v2; /* Device can make use of IOMMUv2 */ | ||
334 | bool passthrough; /* Default for device is pt_domain */ | ||
333 | struct { | 335 | struct { |
334 | bool enabled; | 336 | bool enabled; |
335 | int qdep; | 337 | int qdep; |
@@ -575,6 +577,8 @@ extern u32 amd_iommu_max_pasids; | |||
575 | 577 | ||
576 | extern bool amd_iommu_v2_present; | 578 | extern bool amd_iommu_v2_present; |
577 | 579 | ||
580 | extern bool amd_iommu_force_isolation; | ||
581 | |||
578 | /* takes bus and device/function and returns the device id | 582 | /* takes bus and device/function and returns the device id |
579 | * FIXME: should that be in generic PCI code? */ | 583 | * FIXME: should that be in generic PCI code? */ |
580 | static inline u16 calc_devid(u8 bus, u8 devfn) | 584 | static inline u16 calc_devid(u8 bus, u8 devfn) |