Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'iommu-fixes-v4.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu

Pull IOMMU fixes from Joerg Roedel:
"These fixes are all for the AMD IOMMU driver:

- A regression with HSA caused by the conversion of the driver to
default domains. The fixes make sure that an HSA device can still
be attached to an IOMMUv2 domain and that these domains also allow
non-IOMMUv2 capable devices.

- Fix iommu=pt mode which did not work because the dma_ops where set
to nommu_ops, which breaks devices that can only do 32bit DMA.

- Fix an issue with non-PCI devices not working, because there are no
dma_ops for them. This issue was discovered recently as new AMD
x86 platforms have non-PCI devices too"

* tag 'iommu-fixes-v4.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu:
iommu/amd: Allow non-ATS devices in IOMMUv2 domains
iommu/amd: Set global dma_ops if swiotlb is disabled
iommu/amd: Use swiotlb in passthrough mode
iommu/amd: Allow non-IOMMUv2 devices in IOMMUv2 domains
iommu/amd: Use iommu core for passthrough mode
iommu/amd: Use iommu_attach_group()

+51 -81
+29 -69
drivers/iommu/amd_iommu.c
··· 76 76 * Domain for untranslated devices - only allocated 77 77 * if iommu=pt passed on kernel cmd line. 78 78 */ 79 - static struct protection_domain *pt_domain; 80 - 81 79 static const struct iommu_ops amd_iommu_ops; 82 80 83 81 static ATOMIC_NOTIFIER_HEAD(ppr_notifier); ··· 94 96 struct protection_domain *domain; /* Domain the device is bound to */ 95 97 u16 devid; /* PCI Device ID */ 96 98 bool iommu_v2; /* Device can make use of IOMMUv2 */ 97 - bool passthrough; /* Default for device is pt_domain */ 99 + bool passthrough; /* Device is identity mapped */ 98 100 struct { 99 101 bool enabled; 100 102 int qdep; ··· 114 116 struct kmem_cache *amd_iommu_irq_cache; 115 117 116 118 static void update_domain(struct protection_domain *domain); 117 - static int alloc_passthrough_domain(void); 118 119 static int protection_domain_init(struct protection_domain *domain); 119 120 120 121 /**************************************************************************** ··· 2164 2167 dev_data = get_dev_data(dev); 2165 2168 2166 2169 if (domain->flags & PD_IOMMUV2_MASK) { 2167 - if (!dev_data->iommu_v2 || !dev_data->passthrough) 2170 + if (!dev_data->passthrough) 2168 2171 return -EINVAL; 2169 2172 2170 - if (pdev_iommuv2_enable(pdev) != 0) 2171 - return -EINVAL; 2173 + if (dev_data->iommu_v2) { 2174 + if (pdev_iommuv2_enable(pdev) != 0) 2175 + return -EINVAL; 2172 2176 2173 - dev_data->ats.enabled = true; 2174 - dev_data->ats.qdep = pci_ats_queue_depth(pdev); 2175 - dev_data->pri_tlp = pci_pri_tlp_required(pdev); 2177 + dev_data->ats.enabled = true; 2178 + dev_data->ats.qdep = pci_ats_queue_depth(pdev); 2179 + dev_data->pri_tlp = pci_pri_tlp_required(pdev); 2180 + } 2176 2181 } else if (amd_iommu_iotlb_sup && 2177 2182 pci_enable_ats(pdev, PAGE_SHIFT) == 0) { 2178 2183 dev_data->ats.enabled = true; ··· 2220 2221 do_detach(head); 2221 2222 2222 2223 spin_unlock_irqrestore(&domain->lock, flags); 2223 - 2224 - /* 2225 - * If we run in passthrough mode the device must be assigned to the 2226 - * passthrough domain if it is detached from any other domain. 2227 - * Make sure we can deassign from the pt_domain itself. 2228 - */ 2229 - if (dev_data->passthrough && 2230 - (dev_data->domain == NULL && domain != pt_domain)) 2231 - __attach_device(dev_data, pt_domain); 2232 2224 } 2233 2225 2234 2226 /* ··· 2239 2249 __detach_device(dev_data); 2240 2250 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 2241 2251 2242 - if (domain->flags & PD_IOMMUV2_MASK) 2252 + if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2) 2243 2253 pdev_iommuv2_disable(to_pci_dev(dev)); 2244 2254 else if (dev_data->ats.enabled) 2245 2255 pci_disable_ats(to_pci_dev(dev)); ··· 2277 2287 2278 2288 BUG_ON(!dev_data); 2279 2289 2280 - if (dev_data->iommu_v2) 2290 + if (iommu_pass_through || dev_data->iommu_v2) 2281 2291 iommu_request_dm_for_dev(dev); 2282 2292 2283 2293 /* Domains are initialized for this device - have a look what we ended up with */ 2284 2294 domain = iommu_get_domain_for_dev(dev); 2285 - if (domain->type == IOMMU_DOMAIN_IDENTITY) { 2295 + if (domain->type == IOMMU_DOMAIN_IDENTITY) 2286 2296 dev_data->passthrough = true; 2287 - dev->archdata.dma_ops = &nommu_dma_ops; 2288 - } else { 2297 + else 2289 2298 dev->archdata.dma_ops = &amd_iommu_dma_ops; 2290 - } 2291 2299 2292 2300 out: 2293 2301 iommu_completion_wait(iommu); ··· 2850 2862 2851 2863 int __init amd_iommu_init_dma_ops(void) 2852 2864 { 2865 + swiotlb = iommu_pass_through ? 1 : 0; 2853 2866 iommu_detected = 1; 2854 - swiotlb = 0; 2867 + 2868 + /* 2869 + * In case we don't initialize SWIOTLB (actually the common case 2870 + * when AMD IOMMU is enabled), make sure there are global 2871 + * dma_ops set as a fall-back for devices not handled by this 2872 + * driver (for example non-PCI devices). 2873 + */ 2874 + if (!swiotlb) 2875 + dma_ops = &nommu_dma_ops; 2855 2876 2856 2877 amd_iommu_stats_init(); 2857 2878 ··· 2942 2945 kfree(domain); 2943 2946 2944 2947 return NULL; 2945 - } 2946 - 2947 - static int alloc_passthrough_domain(void) 2948 - { 2949 - if (pt_domain != NULL) 2950 - return 0; 2951 - 2952 - /* allocate passthrough domain */ 2953 - pt_domain = protection_domain_alloc(); 2954 - if (!pt_domain) 2955 - return -ENOMEM; 2956 - 2957 - pt_domain->mode = PAGE_MODE_NONE; 2958 - 2959 - return 0; 2960 2948 } 2961 2949 2962 2950 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type) ··· 3204 3222 * 3205 3223 *****************************************************************************/ 3206 3224 3207 - int __init amd_iommu_init_passthrough(void) 3208 - { 3209 - struct iommu_dev_data *dev_data; 3210 - struct pci_dev *dev = NULL; 3211 - int ret; 3212 - 3213 - ret = alloc_passthrough_domain(); 3214 - if (ret) 3215 - return ret; 3216 - 3217 - for_each_pci_dev(dev) { 3218 - if (!check_device(&dev->dev)) 3219 - continue; 3220 - 3221 - dev_data = get_dev_data(&dev->dev); 3222 - dev_data->passthrough = true; 3223 - 3224 - attach_device(&dev->dev, pt_domain); 3225 - } 3226 - 3227 - amd_iommu_stats_init(); 3228 - 3229 - pr_info("AMD-Vi: Initialized for Passthrough Mode\n"); 3230 - 3231 - return 0; 3232 - } 3233 - 3234 3225 /* IOMMUv2 specific functions */ 3235 3226 int amd_iommu_register_ppr_notifier(struct notifier_block *nb) 3236 3227 { ··· 3318 3363 struct amd_iommu *iommu; 3319 3364 int qdep; 3320 3365 3321 - BUG_ON(!dev_data->ats.enabled); 3366 + /* 3367 + There might be non-IOMMUv2 capable devices in an IOMMUv2 3368 + * domain. 3369 + */ 3370 + if (!dev_data->ats.enabled) 3371 + continue; 3322 3372 3323 3373 qdep = dev_data->ats.qdep; 3324 3374 iommu = amd_iommu_rlookup_table[dev_data->devid];
+1 -9
drivers/iommu/amd_iommu_init.c
··· 2026 2026 return true; 2027 2027 } 2028 2028 2029 - static int amd_iommu_init_dma(void) 2030 - { 2031 - if (iommu_pass_through) 2032 - return amd_iommu_init_passthrough(); 2033 - else 2034 - return amd_iommu_init_dma_ops(); 2035 - } 2036 - 2037 2029 /**************************************************************************** 2038 2030 * 2039 2031 * AMD IOMMU Initialization State Machine ··· 2065 2073 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN; 2066 2074 break; 2067 2075 case IOMMU_INTERRUPTS_EN: 2068 - ret = amd_iommu_init_dma(); 2076 + ret = amd_iommu_init_dma_ops(); 2069 2077 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS; 2070 2078 break; 2071 2079 case IOMMU_DMA_OPS:
+21 -3
drivers/iommu/amd_iommu_v2.c
··· 132 132 133 133 static void free_device_state(struct device_state *dev_state) 134 134 { 135 + struct iommu_group *group; 136 + 135 137 /* 136 138 * First detach device from domain - No more PRI requests will arrive 137 139 * from that device after it is unbound from the IOMMUv2 domain. 138 140 */ 139 - iommu_detach_device(dev_state->domain, &dev_state->pdev->dev); 141 + group = iommu_group_get(&dev_state->pdev->dev); 142 + if (WARN_ON(!group)) 143 + return; 144 + 145 + iommu_detach_group(dev_state->domain, group); 146 + 147 + iommu_group_put(group); 140 148 141 149 /* Everything is down now, free the IOMMUv2 domain */ 142 150 iommu_domain_free(dev_state->domain); ··· 739 731 int amd_iommu_init_device(struct pci_dev *pdev, int pasids) 740 732 { 741 733 struct device_state *dev_state; 734 + struct iommu_group *group; 742 735 unsigned long flags; 743 736 int ret, tmp; 744 737 u16 devid; ··· 785 776 if (ret) 786 777 goto out_free_domain; 787 778 788 - ret = iommu_attach_device(dev_state->domain, &pdev->dev); 789 - if (ret != 0) 779 + group = iommu_group_get(&pdev->dev); 780 + if (!group) 790 781 goto out_free_domain; 782 + 783 + ret = iommu_attach_group(dev_state->domain, group); 784 + if (ret != 0) 785 + goto out_drop_group; 786 + 787 + iommu_group_put(group); 791 788 792 789 spin_lock_irqsave(&state_lock, flags); 793 790 ··· 808 793 spin_unlock_irqrestore(&state_lock, flags); 809 794 810 795 return 0; 796 + 797 + out_drop_group: 798 + iommu_group_put(group); 811 799 812 800 out_free_domain: 813 801 iommu_domain_free(dev_state->domain);