aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-10-15 01:23:49 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-10-15 01:23:49 -0400
commit23971bdffff5f7c904131dfb41c186711dc2c418 (patch)
tree0cd5e379932a72af47dba17f8958908e7dd029cd
parentc0fa2373f8cfed90437d8d7b17e0b1a84009a10a (diff)
parent09b5269a1b3d47525d7c25efeb16f5407ef82ea2 (diff)
Merge tag 'iommu-updates-v3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu
Pull IOMMU updates from Joerg Roedel: "This pull-request includes: - change in the IOMMU-API to convert the former iommu_domain_capable function to just iommu_capable - various fixes in handling RMRR ranges for the VT-d driver (one fix requires a device driver core change which was acked by Greg KH) - the AMD IOMMU driver now assigns and deassigns complete alias groups to fix issues with devices using the wrong PCI request-id - MMU-401 support for the ARM SMMU driver - multi-master IOMMU group support for the ARM SMMU driver - various other small fixes all over the place" * tag 'iommu-updates-v3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (41 commits) iommu/vt-d: Work around broken RMRR firmware entries iommu/vt-d: Store bus information in RMRR PCI device path iommu/vt-d: Only remove domain when device is removed driver core: Add BUS_NOTIFY_REMOVED_DEVICE event iommu/amd: Fix devid mapping for ivrs_ioapic override iommu/irq_remapping: Fix the regression of hpet irq remapping iommu: Fix bus notifier breakage iommu/amd: Split init_iommu_group() from iommu_init_device() iommu: Rework iommu_group_get_for_pci_dev() iommu: Make of_device_id array const amd_iommu: do not dereference a NULL pointer address. iommu/omap: Remove omap_iommu unused owner field iommu: Remove iommu_domain_has_cap() API function IB/usnic: Convert to use new iommu_capable() API function vfio: Convert to use new iommu_capable() API function kvm: iommu: Convert to use new iommu_capable() API function iommu/tegra: Convert to iommu_capable() API function iommu/msm: Convert to iommu_capable() API function iommu/vt-d: Convert to iommu_capable() API function iommu/fsl: Convert to iommu_capable() API function ...
-rw-r--r--Documentation/devicetree/bindings/iommu/arm,smmu.txt1
-rw-r--r--drivers/base/core.c3
-rw-r--r--drivers/infiniband/hw/usnic/usnic_uiom.c2
-rw-r--r--drivers/iommu/amd_iommu.c117
-rw-r--r--drivers/iommu/amd_iommu_init.c21
-rw-r--r--drivers/iommu/amd_iommu_types.h21
-rw-r--r--drivers/iommu/arm-smmu.c220
-rw-r--r--drivers/iommu/dmar.c25
-rw-r--r--drivers/iommu/exynos-iommu.c51
-rw-r--r--drivers/iommu/fsl_pamu_domain.c5
-rw-r--r--drivers/iommu/intel-iommu.c16
-rw-r--r--drivers/iommu/intel_irq_remapping.c7
-rw-r--r--drivers/iommu/iommu.c208
-rw-r--r--drivers/iommu/irq_remapping.c11
-rw-r--r--drivers/iommu/irq_remapping.h2
-rw-r--r--drivers/iommu/msm_iommu.c7
-rw-r--r--drivers/iommu/omap-iommu.c27
-rw-r--r--drivers/iommu/omap-iommu.h1
-rw-r--r--drivers/iommu/tegra-gart.c9
-rw-r--r--drivers/iommu/tegra-smmu.c9
-rw-r--r--drivers/vfio/vfio_iommu_type1.c4
-rw-r--r--include/linux/device.h11
-rw-r--r--include/linux/dmar.h8
-rw-r--r--include/linux/iommu.h25
-rw-r--r--virt/kvm/iommu.c6
25 files changed, 461 insertions, 356 deletions
diff --git a/Documentation/devicetree/bindings/iommu/arm,smmu.txt b/Documentation/devicetree/bindings/iommu/arm,smmu.txt
index 2d0f7cd867ea..06760503a819 100644
--- a/Documentation/devicetree/bindings/iommu/arm,smmu.txt
+++ b/Documentation/devicetree/bindings/iommu/arm,smmu.txt
@@ -14,6 +14,7 @@ conditions.
14 "arm,smmu-v1" 14 "arm,smmu-v1"
15 "arm,smmu-v2" 15 "arm,smmu-v2"
16 "arm,mmu-400" 16 "arm,mmu-400"
17 "arm,mmu-401"
17 "arm,mmu-500" 18 "arm,mmu-500"
18 19
19 depending on the particular implementation and/or the 20 depending on the particular implementation and/or the
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 28b808c73e8e..14d162952c3b 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1211,6 +1211,9 @@ void device_del(struct device *dev)
1211 */ 1211 */
1212 if (platform_notify_remove) 1212 if (platform_notify_remove)
1213 platform_notify_remove(dev); 1213 platform_notify_remove(dev);
1214 if (dev->bus)
1215 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1216 BUS_NOTIFY_REMOVED_DEVICE, dev);
1214 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1217 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1215 cleanup_device_parent(dev); 1218 cleanup_device_parent(dev);
1216 kobject_del(&dev->kobj); 1219 kobject_del(&dev->kobj);
diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.c b/drivers/infiniband/hw/usnic/usnic_uiom.c
index 801a1d6937e4..417de1f32960 100644
--- a/drivers/infiniband/hw/usnic/usnic_uiom.c
+++ b/drivers/infiniband/hw/usnic/usnic_uiom.c
@@ -507,7 +507,7 @@ int usnic_uiom_attach_dev_to_pd(struct usnic_uiom_pd *pd, struct device *dev)
507 if (err) 507 if (err)
508 goto out_free_dev; 508 goto out_free_dev;
509 509
510 if (!iommu_domain_has_cap(pd->domain, IOMMU_CAP_CACHE_COHERENCY)) { 510 if (!iommu_capable(dev->bus, IOMMU_CAP_CACHE_COHERENCY)) {
511 usnic_err("IOMMU of %s does not support cache coherency\n", 511 usnic_err("IOMMU of %s does not support cache coherency\n",
512 dev_name(dev)); 512 dev_name(dev));
513 err = -EINVAL; 513 err = -EINVAL;
diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
index ecb0109a5360..505a9adac2d5 100644
--- a/drivers/iommu/amd_iommu.c
+++ b/drivers/iommu/amd_iommu.c
@@ -88,6 +88,27 @@ int amd_iommu_max_glx_val = -1;
88static struct dma_map_ops amd_iommu_dma_ops; 88static struct dma_map_ops amd_iommu_dma_ops;
89 89
90/* 90/*
91 * This struct contains device specific data for the IOMMU
92 */
93struct iommu_dev_data {
94 struct list_head list; /* For domain->dev_list */
95 struct list_head dev_data_list; /* For global dev_data_list */
96 struct list_head alias_list; /* Link alias-groups together */
97 struct iommu_dev_data *alias_data;/* The alias dev_data */
98 struct protection_domain *domain; /* Domain the device is bound to */
99 u16 devid; /* PCI Device ID */
100 bool iommu_v2; /* Device can make use of IOMMUv2 */
101 bool passthrough; /* Default for device is pt_domain */
102 struct {
103 bool enabled;
104 int qdep;
105 } ats; /* ATS state */
106 bool pri_tlp; /* PASID TLB required for
107 PPR completions */
108 u32 errata; /* Bitmap for errata to apply */
109};
110
111/*
91 * general struct to manage commands send to an IOMMU 112 * general struct to manage commands send to an IOMMU
92 */ 113 */
93struct iommu_cmd { 114struct iommu_cmd {
@@ -114,8 +135,9 @@ static struct iommu_dev_data *alloc_dev_data(u16 devid)
114 if (!dev_data) 135 if (!dev_data)
115 return NULL; 136 return NULL;
116 137
138 INIT_LIST_HEAD(&dev_data->alias_list);
139
117 dev_data->devid = devid; 140 dev_data->devid = devid;
118 atomic_set(&dev_data->bind, 0);
119 141
120 spin_lock_irqsave(&dev_data_list_lock, flags); 142 spin_lock_irqsave(&dev_data_list_lock, flags);
121 list_add_tail(&dev_data->dev_data_list, &dev_data_list); 143 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
@@ -260,17 +282,13 @@ static bool check_device(struct device *dev)
260 return true; 282 return true;
261} 283}
262 284
263static int init_iommu_group(struct device *dev) 285static void init_iommu_group(struct device *dev)
264{ 286{
265 struct iommu_group *group; 287 struct iommu_group *group;
266 288
267 group = iommu_group_get_for_dev(dev); 289 group = iommu_group_get_for_dev(dev);
268 290 if (!IS_ERR(group))
269 if (IS_ERR(group)) 291 iommu_group_put(group);
270 return PTR_ERR(group);
271
272 iommu_group_put(group);
273 return 0;
274} 292}
275 293
276static int __last_alias(struct pci_dev *pdev, u16 alias, void *data) 294static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
@@ -340,7 +358,6 @@ static int iommu_init_device(struct device *dev)
340 struct pci_dev *pdev = to_pci_dev(dev); 358 struct pci_dev *pdev = to_pci_dev(dev);
341 struct iommu_dev_data *dev_data; 359 struct iommu_dev_data *dev_data;
342 u16 alias; 360 u16 alias;
343 int ret;
344 361
345 if (dev->archdata.iommu) 362 if (dev->archdata.iommu)
346 return 0; 363 return 0;
@@ -362,12 +379,9 @@ static int iommu_init_device(struct device *dev)
362 return -ENOTSUPP; 379 return -ENOTSUPP;
363 } 380 }
364 dev_data->alias_data = alias_data; 381 dev_data->alias_data = alias_data;
365 }
366 382
367 ret = init_iommu_group(dev); 383 /* Add device to the alias_list */
368 if (ret) { 384 list_add(&dev_data->alias_list, &alias_data->alias_list);
369 free_dev_data(dev_data);
370 return ret;
371 } 385 }
372 386
373 if (pci_iommuv2_capable(pdev)) { 387 if (pci_iommuv2_capable(pdev)) {
@@ -455,6 +469,15 @@ int __init amd_iommu_init_devices(void)
455 goto out_free; 469 goto out_free;
456 } 470 }
457 471
472 /*
473 * Initialize IOMMU groups only after iommu_init_device() has
474 * had a chance to populate any IVRS defined aliases.
475 */
476 for_each_pci_dev(pdev) {
477 if (check_device(&pdev->dev))
478 init_iommu_group(&pdev->dev);
479 }
480
458 return 0; 481 return 0;
459 482
460out_free: 483out_free:
@@ -1368,6 +1391,9 @@ static int iommu_map_page(struct protection_domain *dom,
1368 count = PAGE_SIZE_PTE_COUNT(page_size); 1391 count = PAGE_SIZE_PTE_COUNT(page_size);
1369 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL); 1392 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
1370 1393
1394 if (!pte)
1395 return -ENOMEM;
1396
1371 for (i = 0; i < count; ++i) 1397 for (i = 0; i < count; ++i)
1372 if (IOMMU_PTE_PRESENT(pte[i])) 1398 if (IOMMU_PTE_PRESENT(pte[i]))
1373 return -EBUSY; 1399 return -EBUSY;
@@ -2122,35 +2148,29 @@ static void do_detach(struct iommu_dev_data *dev_data)
2122static int __attach_device(struct iommu_dev_data *dev_data, 2148static int __attach_device(struct iommu_dev_data *dev_data,
2123 struct protection_domain *domain) 2149 struct protection_domain *domain)
2124{ 2150{
2151 struct iommu_dev_data *head, *entry;
2125 int ret; 2152 int ret;
2126 2153
2127 /* lock domain */ 2154 /* lock domain */
2128 spin_lock(&domain->lock); 2155 spin_lock(&domain->lock);
2129 2156
2130 if (dev_data->alias_data != NULL) { 2157 head = dev_data;
2131 struct iommu_dev_data *alias_data = dev_data->alias_data;
2132 2158
2133 /* Some sanity checks */ 2159 if (head->alias_data != NULL)
2134 ret = -EBUSY; 2160 head = head->alias_data;
2135 if (alias_data->domain != NULL &&
2136 alias_data->domain != domain)
2137 goto out_unlock;
2138 2161
2139 if (dev_data->domain != NULL && 2162 /* Now we have the root of the alias group, if any */
2140 dev_data->domain != domain)
2141 goto out_unlock;
2142 2163
2143 /* Do real assignment */ 2164 ret = -EBUSY;
2144 if (alias_data->domain == NULL) 2165 if (head->domain != NULL)
2145 do_attach(alias_data, domain); 2166 goto out_unlock;
2146
2147 atomic_inc(&alias_data->bind);
2148 }
2149 2167
2150 if (dev_data->domain == NULL) 2168 /* Attach alias group root */
2151 do_attach(dev_data, domain); 2169 do_attach(head, domain);
2152 2170
2153 atomic_inc(&dev_data->bind); 2171 /* Attach other devices in the alias group */
2172 list_for_each_entry(entry, &head->alias_list, alias_list)
2173 do_attach(entry, domain);
2154 2174
2155 ret = 0; 2175 ret = 0;
2156 2176
@@ -2298,6 +2318,7 @@ static int attach_device(struct device *dev,
2298 */ 2318 */
2299static void __detach_device(struct iommu_dev_data *dev_data) 2319static void __detach_device(struct iommu_dev_data *dev_data)
2300{ 2320{
2321 struct iommu_dev_data *head, *entry;
2301 struct protection_domain *domain; 2322 struct protection_domain *domain;
2302 unsigned long flags; 2323 unsigned long flags;
2303 2324
@@ -2307,15 +2328,14 @@ static void __detach_device(struct iommu_dev_data *dev_data)
2307 2328
2308 spin_lock_irqsave(&domain->lock, flags); 2329 spin_lock_irqsave(&domain->lock, flags);
2309 2330
2310 if (dev_data->alias_data != NULL) { 2331 head = dev_data;
2311 struct iommu_dev_data *alias_data = dev_data->alias_data; 2332 if (head->alias_data != NULL)
2333 head = head->alias_data;
2312 2334
2313 if (atomic_dec_and_test(&alias_data->bind)) 2335 list_for_each_entry(entry, &head->alias_list, alias_list)
2314 do_detach(alias_data); 2336 do_detach(entry);
2315 }
2316 2337
2317 if (atomic_dec_and_test(&dev_data->bind)) 2338 do_detach(head);
2318 do_detach(dev_data);
2319 2339
2320 spin_unlock_irqrestore(&domain->lock, flags); 2340 spin_unlock_irqrestore(&domain->lock, flags);
2321 2341
@@ -2415,6 +2435,7 @@ static int device_change_notifier(struct notifier_block *nb,
2415 case BUS_NOTIFY_ADD_DEVICE: 2435 case BUS_NOTIFY_ADD_DEVICE:
2416 2436
2417 iommu_init_device(dev); 2437 iommu_init_device(dev);
2438 init_iommu_group(dev);
2418 2439
2419 /* 2440 /*
2420 * dev_data is still NULL and 2441 * dev_data is still NULL and
@@ -3158,7 +3179,6 @@ static void cleanup_domain(struct protection_domain *domain)
3158 entry = list_first_entry(&domain->dev_list, 3179 entry = list_first_entry(&domain->dev_list,
3159 struct iommu_dev_data, list); 3180 struct iommu_dev_data, list);
3160 __detach_device(entry); 3181 __detach_device(entry);
3161 atomic_set(&entry->bind, 0);
3162 } 3182 }
3163 3183
3164 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 3184 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
@@ -3384,20 +3404,20 @@ static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3384 return paddr; 3404 return paddr;
3385} 3405}
3386 3406
3387static int amd_iommu_domain_has_cap(struct iommu_domain *domain, 3407static bool amd_iommu_capable(enum iommu_cap cap)
3388 unsigned long cap)
3389{ 3408{
3390 switch (cap) { 3409 switch (cap) {
3391 case IOMMU_CAP_CACHE_COHERENCY: 3410 case IOMMU_CAP_CACHE_COHERENCY:
3392 return 1; 3411 return true;
3393 case IOMMU_CAP_INTR_REMAP: 3412 case IOMMU_CAP_INTR_REMAP:
3394 return irq_remapping_enabled; 3413 return (irq_remapping_enabled == 1);
3395 } 3414 }
3396 3415
3397 return 0; 3416 return false;
3398} 3417}
3399 3418
3400static const struct iommu_ops amd_iommu_ops = { 3419static const struct iommu_ops amd_iommu_ops = {
3420 .capable = amd_iommu_capable,
3401 .domain_init = amd_iommu_domain_init, 3421 .domain_init = amd_iommu_domain_init,
3402 .domain_destroy = amd_iommu_domain_destroy, 3422 .domain_destroy = amd_iommu_domain_destroy,
3403 .attach_dev = amd_iommu_attach_device, 3423 .attach_dev = amd_iommu_attach_device,
@@ -3405,7 +3425,6 @@ static const struct iommu_ops amd_iommu_ops = {
3405 .map = amd_iommu_map, 3425 .map = amd_iommu_map,
3406 .unmap = amd_iommu_unmap, 3426 .unmap = amd_iommu_unmap,
3407 .iova_to_phys = amd_iommu_iova_to_phys, 3427 .iova_to_phys = amd_iommu_iova_to_phys,
3408 .domain_has_cap = amd_iommu_domain_has_cap,
3409 .pgsize_bitmap = AMD_IOMMU_PGSIZES, 3428 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
3410}; 3429};
3411 3430
@@ -4235,7 +4254,7 @@ static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4235 return 0; 4254 return 0;
4236} 4255}
4237 4256
4238static int setup_hpet_msi(unsigned int irq, unsigned int id) 4257static int alloc_hpet_msi(unsigned int irq, unsigned int id)
4239{ 4258{
4240 struct irq_2_irte *irte_info; 4259 struct irq_2_irte *irte_info;
4241 struct irq_cfg *cfg; 4260 struct irq_cfg *cfg;
@@ -4274,6 +4293,6 @@ struct irq_remap_ops amd_iommu_irq_ops = {
4274 .compose_msi_msg = compose_msi_msg, 4293 .compose_msi_msg = compose_msi_msg,
4275 .msi_alloc_irq = msi_alloc_irq, 4294 .msi_alloc_irq = msi_alloc_irq,
4276 .msi_setup_irq = msi_setup_irq, 4295 .msi_setup_irq = msi_setup_irq,
4277 .setup_hpet_msi = setup_hpet_msi, 4296 .alloc_hpet_msi = alloc_hpet_msi,
4278}; 4297};
4279#endif 4298#endif
diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c
index 3783e0b44df6..b0522f15730f 100644
--- a/drivers/iommu/amd_iommu_init.c
+++ b/drivers/iommu/amd_iommu_init.c
@@ -712,7 +712,7 @@ static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
712 set_iommu_for_device(iommu, devid); 712 set_iommu_for_device(iommu, devid);
713} 713}
714 714
715static int __init add_special_device(u8 type, u8 id, u16 devid, bool cmd_line) 715static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
716{ 716{
717 struct devid_map *entry; 717 struct devid_map *entry;
718 struct list_head *list; 718 struct list_head *list;
@@ -731,6 +731,8 @@ static int __init add_special_device(u8 type, u8 id, u16 devid, bool cmd_line)
731 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n", 731 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
732 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id); 732 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
733 733
734 *devid = entry->devid;
735
734 return 0; 736 return 0;
735 } 737 }
736 738
@@ -739,7 +741,7 @@ static int __init add_special_device(u8 type, u8 id, u16 devid, bool cmd_line)
739 return -ENOMEM; 741 return -ENOMEM;
740 742
741 entry->id = id; 743 entry->id = id;
742 entry->devid = devid; 744 entry->devid = *devid;
743 entry->cmd_line = cmd_line; 745 entry->cmd_line = cmd_line;
744 746
745 list_add_tail(&entry->list, list); 747 list_add_tail(&entry->list, list);
@@ -754,7 +756,7 @@ static int __init add_early_maps(void)
754 for (i = 0; i < early_ioapic_map_size; ++i) { 756 for (i = 0; i < early_ioapic_map_size; ++i) {
755 ret = add_special_device(IVHD_SPECIAL_IOAPIC, 757 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
756 early_ioapic_map[i].id, 758 early_ioapic_map[i].id,
757 early_ioapic_map[i].devid, 759 &early_ioapic_map[i].devid,
758 early_ioapic_map[i].cmd_line); 760 early_ioapic_map[i].cmd_line);
759 if (ret) 761 if (ret)
760 return ret; 762 return ret;
@@ -763,7 +765,7 @@ static int __init add_early_maps(void)
763 for (i = 0; i < early_hpet_map_size; ++i) { 765 for (i = 0; i < early_hpet_map_size; ++i) {
764 ret = add_special_device(IVHD_SPECIAL_HPET, 766 ret = add_special_device(IVHD_SPECIAL_HPET,
765 early_hpet_map[i].id, 767 early_hpet_map[i].id,
766 early_hpet_map[i].devid, 768 &early_hpet_map[i].devid,
767 early_hpet_map[i].cmd_line); 769 early_hpet_map[i].cmd_line);
768 if (ret) 770 if (ret)
769 return ret; 771 return ret;
@@ -978,10 +980,17 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
978 PCI_SLOT(devid), 980 PCI_SLOT(devid),
979 PCI_FUNC(devid)); 981 PCI_FUNC(devid));
980 982
981 set_dev_entry_from_acpi(iommu, devid, e->flags, 0); 983 ret = add_special_device(type, handle, &devid, false);
982 ret = add_special_device(type, handle, devid, false);
983 if (ret) 984 if (ret)
984 return ret; 985 return ret;
986
987 /*
988 * add_special_device might update the devid in case a
989 * command-line override is present. So call
990 * set_dev_entry_from_acpi after add_special_device.
991 */
992 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
993
985 break; 994 break;
986 } 995 }
987 default: 996 default:
diff --git a/drivers/iommu/amd_iommu_types.h b/drivers/iommu/amd_iommu_types.h
index 8e43b7cba133..cec51a8ba844 100644
--- a/drivers/iommu/amd_iommu_types.h
+++ b/drivers/iommu/amd_iommu_types.h
@@ -418,27 +418,6 @@ struct protection_domain {
418}; 418};
419 419
420/* 420/*
421 * This struct contains device specific data for the IOMMU
422 */
423struct iommu_dev_data {
424 struct list_head list; /* For domain->dev_list */
425 struct list_head dev_data_list; /* For global dev_data_list */
426 struct iommu_dev_data *alias_data;/* The alias dev_data */
427 struct protection_domain *domain; /* Domain the device is bound to */
428 atomic_t bind; /* Domain attach reference count */
429 u16 devid; /* PCI Device ID */
430 bool iommu_v2; /* Device can make use of IOMMUv2 */
431 bool passthrough; /* Default for device is pt_domain */
432 struct {
433 bool enabled;
434 int qdep;
435 } ats; /* ATS state */
436 bool pri_tlp; /* PASID TLB required for
437 PPR completions */
438 u32 errata; /* Bitmap for errata to apply */
439};
440
441/*
442 * For dynamic growth the aperture size is split into ranges of 128MB of 421 * For dynamic growth the aperture size is split into ranges of 128MB of
443 * DMA address space each. This struct represents one such range. 422 * DMA address space each. This struct represents one such range.
444 */ 423 */
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index a83cc2a2a2ca..60558f794922 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -24,7 +24,7 @@
24 * - v7/v8 long-descriptor format 24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU 25 * - Non-secure access to the SMMU
26 * - 4k and 64k pages, with contiguous pte hints. 26 * - 4k and 64k pages, with contiguous pte hints.
27 * - Up to 42-bit addressing (dependent on VA_BITS) 27 * - Up to 48-bit addressing (dependent on VA_BITS)
28 * - Context fault reporting 28 * - Context fault reporting
29 */ 29 */
30 30
@@ -59,7 +59,7 @@
59 59
60/* SMMU global address space */ 60/* SMMU global address space */
61#define ARM_SMMU_GR0(smmu) ((smmu)->base) 61#define ARM_SMMU_GR0(smmu) ((smmu)->base)
62#define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize) 62#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
63 63
64/* 64/*
65 * SMMU global address space with conditional offset to access secure 65 * SMMU global address space with conditional offset to access secure
@@ -224,7 +224,7 @@
224 224
225/* Translation context bank */ 225/* Translation context bank */
226#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) 226#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
227#define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize) 227#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
228 228
229#define ARM_SMMU_CB_SCTLR 0x0 229#define ARM_SMMU_CB_SCTLR 0x0
230#define ARM_SMMU_CB_RESUME 0x8 230#define ARM_SMMU_CB_RESUME 0x8
@@ -326,6 +326,16 @@
326 326
327#define FSYNR0_WNR (1 << 4) 327#define FSYNR0_WNR (1 << 4)
328 328
329static int force_stage;
330module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR);
331MODULE_PARM_DESC(force_stage,
332 "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
333
334enum arm_smmu_arch_version {
335 ARM_SMMU_V1 = 1,
336 ARM_SMMU_V2,
337};
338
329struct arm_smmu_smr { 339struct arm_smmu_smr {
330 u8 idx; 340 u8 idx;
331 u16 mask; 341 u16 mask;
@@ -349,7 +359,7 @@ struct arm_smmu_device {
349 359
350 void __iomem *base; 360 void __iomem *base;
351 unsigned long size; 361 unsigned long size;
352 unsigned long pagesize; 362 unsigned long pgshift;
353 363
354#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) 364#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
355#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) 365#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
@@ -360,7 +370,7 @@ struct arm_smmu_device {
360 370
361#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) 371#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
362 u32 options; 372 u32 options;
363 int version; 373 enum arm_smmu_arch_version version;
364 374
365 u32 num_context_banks; 375 u32 num_context_banks;
366 u32 num_s2_context_banks; 376 u32 num_s2_context_banks;
@@ -370,8 +380,9 @@ struct arm_smmu_device {
370 u32 num_mapping_groups; 380 u32 num_mapping_groups;
371 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS); 381 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
372 382
373 unsigned long input_size; 383 unsigned long s1_input_size;
374 unsigned long s1_output_size; 384 unsigned long s1_output_size;
385 unsigned long s2_input_size;
375 unsigned long s2_output_size; 386 unsigned long s2_output_size;
376 387
377 u32 num_global_irqs; 388 u32 num_global_irqs;
@@ -426,17 +437,17 @@ static void parse_driver_options(struct arm_smmu_device *smmu)
426 } while (arm_smmu_options[++i].opt); 437 } while (arm_smmu_options[++i].opt);
427} 438}
428 439
429static struct device *dev_get_master_dev(struct device *dev) 440static struct device_node *dev_get_dev_node(struct device *dev)
430{ 441{
431 if (dev_is_pci(dev)) { 442 if (dev_is_pci(dev)) {
432 struct pci_bus *bus = to_pci_dev(dev)->bus; 443 struct pci_bus *bus = to_pci_dev(dev)->bus;
433 444
434 while (!pci_is_root_bus(bus)) 445 while (!pci_is_root_bus(bus))
435 bus = bus->parent; 446 bus = bus->parent;
436 return bus->bridge->parent; 447 return bus->bridge->parent->of_node;
437 } 448 }
438 449
439 return dev; 450 return dev->of_node;
440} 451}
441 452
442static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, 453static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
@@ -461,15 +472,17 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
461} 472}
462 473
463static struct arm_smmu_master_cfg * 474static struct arm_smmu_master_cfg *
464find_smmu_master_cfg(struct arm_smmu_device *smmu, struct device *dev) 475find_smmu_master_cfg(struct device *dev)
465{ 476{
466 struct arm_smmu_master *master; 477 struct arm_smmu_master_cfg *cfg = NULL;
478 struct iommu_group *group = iommu_group_get(dev);
467 479
468 if (dev_is_pci(dev)) 480 if (group) {
469 return dev->archdata.iommu; 481 cfg = iommu_group_get_iommudata(group);
482 iommu_group_put(group);
483 }
470 484
471 master = find_smmu_master(smmu, dev->of_node); 485 return cfg;
472 return master ? &master->cfg : NULL;
473} 486}
474 487
475static int insert_smmu_master(struct arm_smmu_device *smmu, 488static int insert_smmu_master(struct arm_smmu_device *smmu,
@@ -545,7 +558,7 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
545{ 558{
546 struct arm_smmu_device *smmu; 559 struct arm_smmu_device *smmu;
547 struct arm_smmu_master *master = NULL; 560 struct arm_smmu_master *master = NULL;
548 struct device_node *dev_node = dev_get_master_dev(dev)->of_node; 561 struct device_node *dev_node = dev_get_dev_node(dev);
549 562
550 spin_lock(&arm_smmu_devices_lock); 563 spin_lock(&arm_smmu_devices_lock);
551 list_for_each_entry(smmu, &arm_smmu_devices, list) { 564 list_for_each_entry(smmu, &arm_smmu_devices, list) {
@@ -729,7 +742,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
729 742
730 /* CBAR */ 743 /* CBAR */
731 reg = cfg->cbar; 744 reg = cfg->cbar;
732 if (smmu->version == 1) 745 if (smmu->version == ARM_SMMU_V1)
733 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT; 746 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
734 747
735 /* 748 /*
@@ -744,7 +757,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
744 } 757 }
745 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); 758 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
746 759
747 if (smmu->version > 1) { 760 if (smmu->version > ARM_SMMU_V1) {
748 /* CBA2R */ 761 /* CBA2R */
749#ifdef CONFIG_64BIT 762#ifdef CONFIG_64BIT
750 reg = CBA2R_RW64_64BIT; 763 reg = CBA2R_RW64_64BIT;
@@ -755,7 +768,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
755 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); 768 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
756 769
757 /* TTBCR2 */ 770 /* TTBCR2 */
758 switch (smmu->input_size) { 771 switch (smmu->s1_input_size) {
759 case 32: 772 case 32:
760 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); 773 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
761 break; 774 break;
@@ -817,14 +830,14 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
817 * TTBCR 830 * TTBCR
818 * We use long descriptor, with inner-shareable WBWA tables in TTBR0. 831 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
819 */ 832 */
820 if (smmu->version > 1) { 833 if (smmu->version > ARM_SMMU_V1) {
821 if (PAGE_SIZE == SZ_4K) 834 if (PAGE_SIZE == SZ_4K)
822 reg = TTBCR_TG0_4K; 835 reg = TTBCR_TG0_4K;
823 else 836 else
824 reg = TTBCR_TG0_64K; 837 reg = TTBCR_TG0_64K;
825 838
826 if (!stage1) { 839 if (!stage1) {
827 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT; 840 reg |= (64 - smmu->s2_input_size) << TTBCR_T0SZ_SHIFT;
828 841
829 switch (smmu->s2_output_size) { 842 switch (smmu->s2_output_size) {
830 case 32: 843 case 32:
@@ -847,7 +860,7 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
847 break; 860 break;
848 } 861 }
849 } else { 862 } else {
850 reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT; 863 reg |= (64 - smmu->s1_input_size) << TTBCR_T0SZ_SHIFT;
851 } 864 }
852 } else { 865 } else {
853 reg = 0; 866 reg = 0;
@@ -914,7 +927,7 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
914 goto out_unlock; 927 goto out_unlock;
915 928
916 cfg->cbndx = ret; 929 cfg->cbndx = ret;
917 if (smmu->version == 1) { 930 if (smmu->version == ARM_SMMU_V1) {
918 cfg->irptndx = atomic_inc_return(&smmu->irptndx); 931 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
919 cfg->irptndx %= smmu->num_context_irqs; 932 cfg->irptndx %= smmu->num_context_irqs;
920 } else { 933 } else {
@@ -1151,9 +1164,10 @@ static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1151 struct arm_smmu_device *smmu = smmu_domain->smmu; 1164 struct arm_smmu_device *smmu = smmu_domain->smmu;
1152 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1165 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1153 1166
1167 /* Devices in an IOMMU group may already be configured */
1154 ret = arm_smmu_master_configure_smrs(smmu, cfg); 1168 ret = arm_smmu_master_configure_smrs(smmu, cfg);
1155 if (ret) 1169 if (ret)
1156 return ret; 1170 return ret == -EEXIST ? 0 : ret;
1157 1171
1158 for (i = 0; i < cfg->num_streamids; ++i) { 1172 for (i = 0; i < cfg->num_streamids; ++i) {
1159 u32 idx, s2cr; 1173 u32 idx, s2cr;
@@ -1174,6 +1188,10 @@ static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
1174 struct arm_smmu_device *smmu = smmu_domain->smmu; 1188 struct arm_smmu_device *smmu = smmu_domain->smmu;
1175 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1189 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1176 1190
1191 /* An IOMMU group is torn down by the first device to be removed */
1192 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1193 return;
1194
1177 /* 1195 /*
1178 * We *must* clear the S2CR first, because freeing the SMR means 1196 * We *must* clear the S2CR first, because freeing the SMR means
1179 * that it can be re-allocated immediately. 1197 * that it can be re-allocated immediately.
@@ -1195,12 +1213,17 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1195 struct arm_smmu_device *smmu, *dom_smmu; 1213 struct arm_smmu_device *smmu, *dom_smmu;
1196 struct arm_smmu_master_cfg *cfg; 1214 struct arm_smmu_master_cfg *cfg;
1197 1215
1198 smmu = dev_get_master_dev(dev)->archdata.iommu; 1216 smmu = find_smmu_for_device(dev);
1199 if (!smmu) { 1217 if (!smmu) {
1200 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); 1218 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1201 return -ENXIO; 1219 return -ENXIO;
1202 } 1220 }
1203 1221
1222 if (dev->archdata.iommu) {
1223 dev_err(dev, "already attached to IOMMU domain\n");
1224 return -EEXIST;
1225 }
1226
1204 /* 1227 /*
1205 * Sanity check the domain. We don't support domains across 1228 * Sanity check the domain. We don't support domains across
1206 * different SMMUs. 1229 * different SMMUs.
@@ -1223,11 +1246,14 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1223 } 1246 }
1224 1247
1225 /* Looks ok, so add the device to the domain */ 1248 /* Looks ok, so add the device to the domain */
1226 cfg = find_smmu_master_cfg(smmu_domain->smmu, dev); 1249 cfg = find_smmu_master_cfg(dev);
1227 if (!cfg) 1250 if (!cfg)
1228 return -ENODEV; 1251 return -ENODEV;
1229 1252
1230 return arm_smmu_domain_add_master(smmu_domain, cfg); 1253 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1254 if (!ret)
1255 dev->archdata.iommu = domain;
1256 return ret;
1231} 1257}
1232 1258
1233static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) 1259static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
@@ -1235,9 +1261,12 @@ static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1235 struct arm_smmu_domain *smmu_domain = domain->priv; 1261 struct arm_smmu_domain *smmu_domain = domain->priv;
1236 struct arm_smmu_master_cfg *cfg; 1262 struct arm_smmu_master_cfg *cfg;
1237 1263
1238 cfg = find_smmu_master_cfg(smmu_domain->smmu, dev); 1264 cfg = find_smmu_master_cfg(dev);
1239 if (cfg) 1265 if (!cfg)
1240 arm_smmu_domain_remove_master(smmu_domain, cfg); 1266 return;
1267
1268 dev->archdata.iommu = NULL;
1269 arm_smmu_domain_remove_master(smmu_domain, cfg);
1241} 1270}
1242 1271
1243static bool arm_smmu_pte_is_contiguous_range(unsigned long addr, 1272static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
@@ -1379,6 +1408,7 @@ static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1379 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn, 1408 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
1380 prot, stage); 1409 prot, stage);
1381 phys += next - addr; 1410 phys += next - addr;
1411 pfn = __phys_to_pfn(phys);
1382 } while (pmd++, addr = next, addr < end); 1412 } while (pmd++, addr = next, addr < end);
1383 1413
1384 return ret; 1414 return ret;
@@ -1431,9 +1461,11 @@ static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1431 1461
1432 if (cfg->cbar == CBAR_TYPE_S2_TRANS) { 1462 if (cfg->cbar == CBAR_TYPE_S2_TRANS) {
1433 stage = 2; 1463 stage = 2;
1464 input_mask = (1ULL << smmu->s2_input_size) - 1;
1434 output_mask = (1ULL << smmu->s2_output_size) - 1; 1465 output_mask = (1ULL << smmu->s2_output_size) - 1;
1435 } else { 1466 } else {
1436 stage = 1; 1467 stage = 1;
1468 input_mask = (1ULL << smmu->s1_input_size) - 1;
1437 output_mask = (1ULL << smmu->s1_output_size) - 1; 1469 output_mask = (1ULL << smmu->s1_output_size) - 1;
1438 } 1470 }
1439 1471
@@ -1443,7 +1475,6 @@ static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1443 if (size & ~PAGE_MASK) 1475 if (size & ~PAGE_MASK)
1444 return -EINVAL; 1476 return -EINVAL;
1445 1477
1446 input_mask = (1ULL << smmu->input_size) - 1;
1447 if ((phys_addr_t)iova & ~input_mask) 1478 if ((phys_addr_t)iova & ~input_mask)
1448 return -ERANGE; 1479 return -ERANGE;
1449 1480
@@ -1526,20 +1557,19 @@ static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1526 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK); 1557 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
1527} 1558}
1528 1559
1529static int arm_smmu_domain_has_cap(struct iommu_domain *domain, 1560static bool arm_smmu_capable(enum iommu_cap cap)
1530 unsigned long cap)
1531{ 1561{
1532 struct arm_smmu_domain *smmu_domain = domain->priv;
1533 struct arm_smmu_device *smmu = smmu_domain->smmu;
1534 u32 features = smmu ? smmu->features : 0;
1535
1536 switch (cap) { 1562 switch (cap) {
1537 case IOMMU_CAP_CACHE_COHERENCY: 1563 case IOMMU_CAP_CACHE_COHERENCY:
1538 return features & ARM_SMMU_FEAT_COHERENT_WALK; 1564 /*
1565 * Return true here as the SMMU can always send out coherent
1566 * requests.
1567 */
1568 return true;
1539 case IOMMU_CAP_INTR_REMAP: 1569 case IOMMU_CAP_INTR_REMAP:
1540 return 1; /* MSIs are just memory writes */ 1570 return true; /* MSIs are just memory writes */
1541 default: 1571 default:
1542 return 0; 1572 return false;
1543 } 1573 }
1544} 1574}
1545 1575
@@ -1549,17 +1579,19 @@ static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1549 return 0; /* Continue walking */ 1579 return 0; /* Continue walking */
1550} 1580}
1551 1581
1582static void __arm_smmu_release_pci_iommudata(void *data)
1583{
1584 kfree(data);
1585}
1586
1552static int arm_smmu_add_device(struct device *dev) 1587static int arm_smmu_add_device(struct device *dev)
1553{ 1588{
1554 struct arm_smmu_device *smmu; 1589 struct arm_smmu_device *smmu;
1590 struct arm_smmu_master_cfg *cfg;
1555 struct iommu_group *group; 1591 struct iommu_group *group;
1592 void (*releasefn)(void *) = NULL;
1556 int ret; 1593 int ret;
1557 1594
1558 if (dev->archdata.iommu) {
1559 dev_warn(dev, "IOMMU driver already assigned to device\n");
1560 return -EINVAL;
1561 }
1562
1563 smmu = find_smmu_for_device(dev); 1595 smmu = find_smmu_for_device(dev);
1564 if (!smmu) 1596 if (!smmu)
1565 return -ENODEV; 1597 return -ENODEV;
@@ -1571,7 +1603,6 @@ static int arm_smmu_add_device(struct device *dev)
1571 } 1603 }
1572 1604
1573 if (dev_is_pci(dev)) { 1605 if (dev_is_pci(dev)) {
1574 struct arm_smmu_master_cfg *cfg;
1575 struct pci_dev *pdev = to_pci_dev(dev); 1606 struct pci_dev *pdev = to_pci_dev(dev);
1576 1607
1577 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 1608 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
@@ -1587,11 +1618,20 @@ static int arm_smmu_add_device(struct device *dev)
1587 */ 1618 */
1588 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, 1619 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1589 &cfg->streamids[0]); 1620 &cfg->streamids[0]);
1590 dev->archdata.iommu = cfg; 1621 releasefn = __arm_smmu_release_pci_iommudata;
1591 } else { 1622 } else {
1592 dev->archdata.iommu = smmu; 1623 struct arm_smmu_master *master;
1624
1625 master = find_smmu_master(smmu, dev->of_node);
1626 if (!master) {
1627 ret = -ENODEV;
1628 goto out_put_group;
1629 }
1630
1631 cfg = &master->cfg;
1593 } 1632 }
1594 1633
1634 iommu_group_set_iommudata(group, cfg, releasefn);
1595 ret = iommu_group_add_device(group, dev); 1635 ret = iommu_group_add_device(group, dev);
1596 1636
1597out_put_group: 1637out_put_group:
@@ -1601,14 +1641,11 @@ out_put_group:
1601 1641
1602static void arm_smmu_remove_device(struct device *dev) 1642static void arm_smmu_remove_device(struct device *dev)
1603{ 1643{
1604 if (dev_is_pci(dev))
1605 kfree(dev->archdata.iommu);
1606
1607 dev->archdata.iommu = NULL;
1608 iommu_group_remove_device(dev); 1644 iommu_group_remove_device(dev);
1609} 1645}
1610 1646
1611static const struct iommu_ops arm_smmu_ops = { 1647static const struct iommu_ops arm_smmu_ops = {
1648 .capable = arm_smmu_capable,
1612 .domain_init = arm_smmu_domain_init, 1649 .domain_init = arm_smmu_domain_init,
1613 .domain_destroy = arm_smmu_domain_destroy, 1650 .domain_destroy = arm_smmu_domain_destroy,
1614 .attach_dev = arm_smmu_attach_dev, 1651 .attach_dev = arm_smmu_attach_dev,
@@ -1616,7 +1653,6 @@ static const struct iommu_ops arm_smmu_ops = {
1616 .map = arm_smmu_map, 1653 .map = arm_smmu_map,
1617 .unmap = arm_smmu_unmap, 1654 .unmap = arm_smmu_unmap,
1618 .iova_to_phys = arm_smmu_iova_to_phys, 1655 .iova_to_phys = arm_smmu_iova_to_phys,
1619 .domain_has_cap = arm_smmu_domain_has_cap,
1620 .add_device = arm_smmu_add_device, 1656 .add_device = arm_smmu_add_device,
1621 .remove_device = arm_smmu_remove_device, 1657 .remove_device = arm_smmu_remove_device,
1622 .pgsize_bitmap = (SECTION_SIZE | 1658 .pgsize_bitmap = (SECTION_SIZE |
@@ -1702,10 +1738,6 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1702 u32 id; 1738 u32 id;
1703 1739
1704 dev_notice(smmu->dev, "probing hardware configuration...\n"); 1740 dev_notice(smmu->dev, "probing hardware configuration...\n");
1705
1706 /* Primecell ID */
1707 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1708 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1709 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); 1741 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1710 1742
1711 /* ID0 */ 1743 /* ID0 */
@@ -1716,6 +1748,13 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1716 return -ENODEV; 1748 return -ENODEV;
1717 } 1749 }
1718#endif 1750#endif
1751
1752 /* Restrict available stages based on module parameter */
1753 if (force_stage == 1)
1754 id &= ~(ID0_S2TS | ID0_NTS);
1755 else if (force_stage == 2)
1756 id &= ~(ID0_S1TS | ID0_NTS);
1757
1719 if (id & ID0_S1TS) { 1758 if (id & ID0_S1TS) {
1720 smmu->features |= ARM_SMMU_FEAT_TRANS_S1; 1759 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1721 dev_notice(smmu->dev, "\tstage 1 translation\n"); 1760 dev_notice(smmu->dev, "\tstage 1 translation\n");
@@ -1732,8 +1771,7 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1732 } 1771 }
1733 1772
1734 if (!(smmu->features & 1773 if (!(smmu->features &
1735 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 | 1774 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1736 ARM_SMMU_FEAT_TRANS_NESTED))) {
1737 dev_err(smmu->dev, "\tno translation support!\n"); 1775 dev_err(smmu->dev, "\tno translation support!\n");
1738 return -ENODEV; 1776 return -ENODEV;
1739 } 1777 }
@@ -1779,12 +1817,12 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1779 1817
1780 /* ID1 */ 1818 /* ID1 */
1781 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); 1819 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1782 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K; 1820 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
1783 1821
1784 /* Check for size mismatch of SMMU address space from mapped region */ 1822 /* Check for size mismatch of SMMU address space from mapped region */
1785 size = 1 << 1823 size = 1 <<
1786 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); 1824 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
1787 size *= (smmu->pagesize << 1); 1825 size *= 2 << smmu->pgshift;
1788 if (smmu->size != size) 1826 if (smmu->size != size)
1789 dev_warn(smmu->dev, 1827 dev_warn(smmu->dev,
1790 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n", 1828 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
@@ -1803,28 +1841,21 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1803 /* ID2 */ 1841 /* ID2 */
1804 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); 1842 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1805 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); 1843 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1844 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
1806 1845
1807 /* 1846 /* Stage-2 input size limited due to pgd allocation (PTRS_PER_PGD) */
1808 * Stage-1 output limited by stage-2 input size due to pgd
1809 * allocation (PTRS_PER_PGD).
1810 */
1811 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
1812#ifdef CONFIG_64BIT 1847#ifdef CONFIG_64BIT
1813 smmu->s1_output_size = min_t(unsigned long, VA_BITS, size); 1848 smmu->s2_input_size = min_t(unsigned long, VA_BITS, size);
1814#else 1849#else
1815 smmu->s1_output_size = min(32UL, size); 1850 smmu->s2_input_size = min(32UL, size);
1816#endif 1851#endif
1817 } else {
1818 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT,
1819 size);
1820 }
1821 1852
1822 /* The stage-2 output mask is also applied for bypass */ 1853 /* The stage-2 output mask is also applied for bypass */
1823 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); 1854 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1824 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); 1855 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
1825 1856
1826 if (smmu->version == 1) { 1857 if (smmu->version == ARM_SMMU_V1) {
1827 smmu->input_size = 32; 1858 smmu->s1_input_size = 32;
1828 } else { 1859 } else {
1829#ifdef CONFIG_64BIT 1860#ifdef CONFIG_64BIT
1830 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; 1861 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
@@ -1832,7 +1863,7 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1832#else 1863#else
1833 size = 32; 1864 size = 32;
1834#endif 1865#endif
1835 smmu->input_size = size; 1866 smmu->s1_input_size = size;
1836 1867
1837 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) || 1868 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1838 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) || 1869 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
@@ -1843,15 +1874,30 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1843 } 1874 }
1844 } 1875 }
1845 1876
1846 dev_notice(smmu->dev, 1877 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1847 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n", 1878 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1848 smmu->input_size, smmu->s1_output_size, 1879 smmu->s1_input_size, smmu->s1_output_size);
1849 smmu->s2_output_size); 1880
1881 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1882 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1883 smmu->s2_input_size, smmu->s2_output_size);
1884
1850 return 0; 1885 return 0;
1851} 1886}
1852 1887
1888static const struct of_device_id arm_smmu_of_match[] = {
1889 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1890 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1891 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
1892 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
1893 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1894 { },
1895};
1896MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1897
1853static int arm_smmu_device_dt_probe(struct platform_device *pdev) 1898static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1854{ 1899{
1900 const struct of_device_id *of_id;
1855 struct resource *res; 1901 struct resource *res;
1856 struct arm_smmu_device *smmu; 1902 struct arm_smmu_device *smmu;
1857 struct device *dev = &pdev->dev; 1903 struct device *dev = &pdev->dev;
@@ -1866,6 +1912,9 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1866 } 1912 }
1867 smmu->dev = dev; 1913 smmu->dev = dev;
1868 1914
1915 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1916 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1917
1869 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1918 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1870 smmu->base = devm_ioremap_resource(dev, res); 1919 smmu->base = devm_ioremap_resource(dev, res);
1871 if (IS_ERR(smmu->base)) 1920 if (IS_ERR(smmu->base))
@@ -1930,7 +1979,7 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1930 1979
1931 parse_driver_options(smmu); 1980 parse_driver_options(smmu);
1932 1981
1933 if (smmu->version > 1 && 1982 if (smmu->version > ARM_SMMU_V1 &&
1934 smmu->num_context_banks != smmu->num_context_irqs) { 1983 smmu->num_context_banks != smmu->num_context_irqs) {
1935 dev_err(dev, 1984 dev_err(dev,
1936 "found only %d context interrupt(s) but %d required\n", 1985 "found only %d context interrupt(s) but %d required\n",
@@ -2011,17 +2060,6 @@ static int arm_smmu_device_remove(struct platform_device *pdev)
2011 return 0; 2060 return 0;
2012} 2061}
2013 2062
2014#ifdef CONFIG_OF
2015static struct of_device_id arm_smmu_of_match[] = {
2016 { .compatible = "arm,smmu-v1", },
2017 { .compatible = "arm,smmu-v2", },
2018 { .compatible = "arm,mmu-400", },
2019 { .compatible = "arm,mmu-500", },
2020 { },
2021};
2022MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2023#endif
2024
2025static struct platform_driver arm_smmu_driver = { 2063static struct platform_driver arm_smmu_driver = {
2026 .driver = { 2064 .driver = {
2027 .owner = THIS_MODULE, 2065 .owner = THIS_MODULE,
diff --git a/drivers/iommu/dmar.c b/drivers/iommu/dmar.c
index 06d268abe951..c5c61cabd6e3 100644
--- a/drivers/iommu/dmar.c
+++ b/drivers/iommu/dmar.c
@@ -155,6 +155,7 @@ dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
155 if (event == BUS_NOTIFY_ADD_DEVICE) { 155 if (event == BUS_NOTIFY_ADD_DEVICE) {
156 for (tmp = dev; tmp; tmp = tmp->bus->self) { 156 for (tmp = dev; tmp; tmp = tmp->bus->self) {
157 level--; 157 level--;
158 info->path[level].bus = tmp->bus->number;
158 info->path[level].device = PCI_SLOT(tmp->devfn); 159 info->path[level].device = PCI_SLOT(tmp->devfn);
159 info->path[level].function = PCI_FUNC(tmp->devfn); 160 info->path[level].function = PCI_FUNC(tmp->devfn);
160 if (pci_is_root_bus(tmp->bus)) 161 if (pci_is_root_bus(tmp->bus))
@@ -177,17 +178,33 @@ static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus,
177 int i; 178 int i;
178 179
179 if (info->bus != bus) 180 if (info->bus != bus)
180 return false; 181 goto fallback;
181 if (info->level != count) 182 if (info->level != count)
182 return false; 183 goto fallback;
183 184
184 for (i = 0; i < count; i++) { 185 for (i = 0; i < count; i++) {
185 if (path[i].device != info->path[i].device || 186 if (path[i].device != info->path[i].device ||
186 path[i].function != info->path[i].function) 187 path[i].function != info->path[i].function)
187 return false; 188 goto fallback;
188 } 189 }
189 190
190 return true; 191 return true;
192
193fallback:
194
195 if (count != 1)
196 return false;
197
198 i = info->level - 1;
199 if (bus == info->path[i].bus &&
200 path[0].device == info->path[i].device &&
201 path[0].function == info->path[i].function) {
202 pr_info(FW_BUG "RMRR entry for device %02x:%02x.%x is broken - applying workaround\n",
203 bus, path[0].device, path[0].function);
204 return true;
205 }
206
207 return false;
191} 208}
192 209
193/* Return: > 0 if match found, 0 if no match found, < 0 if error happens */ 210/* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
@@ -247,7 +264,7 @@ int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment,
247 264
248 for_each_active_dev_scope(devices, count, index, tmp) 265 for_each_active_dev_scope(devices, count, index, tmp)
249 if (tmp == &info->dev->dev) { 266 if (tmp == &info->dev->dev) {
250 rcu_assign_pointer(devices[index].dev, NULL); 267 RCU_INIT_POINTER(devices[index].dev, NULL);
251 synchronize_rcu(); 268 synchronize_rcu();
252 put_device(tmp); 269 put_device(tmp);
253 return 1; 270 return 1;
diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c
index d037e87a1fe5..74233186f6f7 100644
--- a/drivers/iommu/exynos-iommu.c
+++ b/drivers/iommu/exynos-iommu.c
@@ -32,7 +32,7 @@
32typedef u32 sysmmu_iova_t; 32typedef u32 sysmmu_iova_t;
33typedef u32 sysmmu_pte_t; 33typedef u32 sysmmu_pte_t;
34 34
35/* We does not consider super section mapping (16MB) */ 35/* We do not consider super section mapping (16MB) */
36#define SECT_ORDER 20 36#define SECT_ORDER 20
37#define LPAGE_ORDER 16 37#define LPAGE_ORDER 16
38#define SPAGE_ORDER 12 38#define SPAGE_ORDER 12
@@ -307,7 +307,7 @@ static void show_fault_information(const char *name,
307 307
308static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id) 308static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
309{ 309{
310 /* SYSMMU is in blocked when interrupt occurred. */ 310 /* SYSMMU is in blocked state when interrupt occurred. */
311 struct sysmmu_drvdata *data = dev_id; 311 struct sysmmu_drvdata *data = dev_id;
312 enum exynos_sysmmu_inttype itype; 312 enum exynos_sysmmu_inttype itype;
313 sysmmu_iova_t addr = -1; 313 sysmmu_iova_t addr = -1;
@@ -567,8 +567,8 @@ static void sysmmu_tlb_invalidate_entry(struct device *dev, sysmmu_iova_t iova,
567 /* 567 /*
568 * L2TLB invalidation required 568 * L2TLB invalidation required
569 * 4KB page: 1 invalidation 569 * 4KB page: 1 invalidation
570 * 64KB page: 16 invalidation 570 * 64KB page: 16 invalidations
571 * 1MB page: 64 invalidation 571 * 1MB page: 64 invalidations
572 * because it is set-associative TLB 572 * because it is set-associative TLB
573 * with 8-way and 64 sets. 573 * with 8-way and 64 sets.
574 * 1MB page can be cached in one of all sets. 574 * 1MB page can be cached in one of all sets.
@@ -714,7 +714,7 @@ static int exynos_iommu_domain_init(struct iommu_domain *domain)
714 if (!priv->lv2entcnt) 714 if (!priv->lv2entcnt)
715 goto err_counter; 715 goto err_counter;
716 716
717 /* w/a of System MMU v3.3 to prevent caching 1MiB mapping */ 717 /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
718 for (i = 0; i < NUM_LV1ENTRIES; i += 8) { 718 for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
719 priv->pgtable[i + 0] = ZERO_LV2LINK; 719 priv->pgtable[i + 0] = ZERO_LV2LINK;
720 priv->pgtable[i + 1] = ZERO_LV2LINK; 720 priv->pgtable[i + 1] = ZERO_LV2LINK;
@@ -861,14 +861,14 @@ static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *priv,
861 pgtable_flush(sent, sent + 1); 861 pgtable_flush(sent, sent + 1);
862 862
863 /* 863 /*
864 * If pretched SLPD is a fault SLPD in zero_l2_table, FLPD cache 864 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
865 * may caches the address of zero_l2_table. This function 865 * FLPD cache may cache the address of zero_l2_table. This
866 * replaces the zero_l2_table with new L2 page table to write 866 * function replaces the zero_l2_table with new L2 page table
867 * valid mappings. 867 * to write valid mappings.
868 * Accessing the valid area may cause page fault since FLPD 868 * Accessing the valid area may cause page fault since FLPD
869 * cache may still caches zero_l2_table for the valid area 869 * cache may still cache zero_l2_table for the valid area
870 * instead of new L2 page table that have the mapping 870 * instead of new L2 page table that has the mapping
871 * information of the valid area 871 * information of the valid area.
872 * Thus any replacement of zero_l2_table with other valid L2 872 * Thus any replacement of zero_l2_table with other valid L2
873 * page table must involve FLPD cache invalidation for System 873 * page table must involve FLPD cache invalidation for System
874 * MMU v3.3. 874 * MMU v3.3.
@@ -963,27 +963,27 @@ static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
963/* 963/*
964 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu: 964 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
965 * 965 *
966 * System MMU v3.x have an advanced logic to improve address translation 966 * System MMU v3.x has advanced logic to improve address translation
967 * performance with caching more page table entries by a page table walk. 967 * performance with caching more page table entries by a page table walk.
968 * However, the logic has a bug that caching fault page table entries and System 968 * However, the logic has a bug that while caching faulty page table entries,
969 * MMU reports page fault if the cached fault entry is hit even though the fault 969 * System MMU reports page fault if the cached fault entry is hit even though
970 * entry is updated to a valid entry after the entry is cached. 970 * the fault entry is updated to a valid entry after the entry is cached.
971 * To prevent caching fault page table entries which may be updated to valid 971 * To prevent caching faulty page table entries which may be updated to valid
972 * entries later, the virtual memory manager should care about the w/a about the 972 * entries later, the virtual memory manager should care about the workaround
973 * problem. The followings describe w/a. 973 * for the problem. The following describes the workaround.
974 * 974 *
975 * Any two consecutive I/O virtual address regions must have a hole of 128KiB 975 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
976 * in maximum to prevent misbehavior of System MMU 3.x. (w/a of h/w bug) 976 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
977 * 977 *
978 * Precisely, any start address of I/O virtual region must be aligned by 978 * Precisely, any start address of I/O virtual region must be aligned with
979 * the following sizes for System MMU v3.1 and v3.2. 979 * the following sizes for System MMU v3.1 and v3.2.
980 * System MMU v3.1: 128KiB 980 * System MMU v3.1: 128KiB
981 * System MMU v3.2: 256KiB 981 * System MMU v3.2: 256KiB
982 * 982 *
983 * Because System MMU v3.3 caches page table entries more aggressively, it needs 983 * Because System MMU v3.3 caches page table entries more aggressively, it needs
984 * more w/a. 984 * more workarounds.
985 * - Any two consecutive I/O virtual regions must be have a hole of larger size 985 * - Any two consecutive I/O virtual regions must have a hole of size larger
986 * than or equal size to 128KiB. 986 * than or equal to 128KiB.
987 * - Start address of an I/O virtual region must be aligned by 128KiB. 987 * - Start address of an I/O virtual region must be aligned by 128KiB.
988 */ 988 */
989static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova, 989static int exynos_iommu_map(struct iommu_domain *domain, unsigned long l_iova,
@@ -1061,7 +1061,8 @@ static size_t exynos_iommu_unmap(struct iommu_domain *domain,
1061 goto err; 1061 goto err;
1062 } 1062 }
1063 1063
1064 *ent = ZERO_LV2LINK; /* w/a for h/w bug in Sysmem MMU v3.3 */ 1064 /* workaround for h/w bug in System MMU v3.3 */
1065 *ent = ZERO_LV2LINK;
1065 pgtable_flush(ent, ent + 1); 1066 pgtable_flush(ent, ent + 1);
1066 size = SECT_SIZE; 1067 size = SECT_SIZE;
1067 goto done; 1068 goto done;
diff --git a/drivers/iommu/fsl_pamu_domain.c b/drivers/iommu/fsl_pamu_domain.c
index 56feed7cec15..c828f80d48b0 100644
--- a/drivers/iommu/fsl_pamu_domain.c
+++ b/drivers/iommu/fsl_pamu_domain.c
@@ -411,8 +411,7 @@ static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
411 return get_phys_addr(dma_domain, iova); 411 return get_phys_addr(dma_domain, iova);
412} 412}
413 413
414static int fsl_pamu_domain_has_cap(struct iommu_domain *domain, 414static bool fsl_pamu_capable(enum iommu_cap cap)
415 unsigned long cap)
416{ 415{
417 return cap == IOMMU_CAP_CACHE_COHERENCY; 416 return cap == IOMMU_CAP_CACHE_COHERENCY;
418} 417}
@@ -1080,6 +1079,7 @@ static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
1080} 1079}
1081 1080
1082static const struct iommu_ops fsl_pamu_ops = { 1081static const struct iommu_ops fsl_pamu_ops = {
1082 .capable = fsl_pamu_capable,
1083 .domain_init = fsl_pamu_domain_init, 1083 .domain_init = fsl_pamu_domain_init,
1084 .domain_destroy = fsl_pamu_domain_destroy, 1084 .domain_destroy = fsl_pamu_domain_destroy,
1085 .attach_dev = fsl_pamu_attach_device, 1085 .attach_dev = fsl_pamu_attach_device,
@@ -1089,7 +1089,6 @@ static const struct iommu_ops fsl_pamu_ops = {
1089 .domain_get_windows = fsl_pamu_get_windows, 1089 .domain_get_windows = fsl_pamu_get_windows,
1090 .domain_set_windows = fsl_pamu_set_windows, 1090 .domain_set_windows = fsl_pamu_set_windows,
1091 .iova_to_phys = fsl_pamu_iova_to_phys, 1091 .iova_to_phys = fsl_pamu_iova_to_phys,
1092 .domain_has_cap = fsl_pamu_domain_has_cap,
1093 .domain_set_attr = fsl_pamu_set_domain_attr, 1092 .domain_set_attr = fsl_pamu_set_domain_attr,
1094 .domain_get_attr = fsl_pamu_get_domain_attr, 1093 .domain_get_attr = fsl_pamu_get_domain_attr,
1095 .add_device = fsl_pamu_add_device, 1094 .add_device = fsl_pamu_add_device,
diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index 5619f264862d..a27d6cb1a793 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -3865,8 +3865,7 @@ static int device_notifier(struct notifier_block *nb,
3865 if (iommu_dummy(dev)) 3865 if (iommu_dummy(dev))
3866 return 0; 3866 return 0;
3867 3867
3868 if (action != BUS_NOTIFY_UNBOUND_DRIVER && 3868 if (action != BUS_NOTIFY_REMOVED_DEVICE)
3869 action != BUS_NOTIFY_DEL_DEVICE)
3870 return 0; 3869 return 0;
3871 3870
3872 /* 3871 /*
@@ -4415,17 +4414,14 @@ static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
4415 return phys; 4414 return phys;
4416} 4415}
4417 4416
4418static int intel_iommu_domain_has_cap(struct iommu_domain *domain, 4417static bool intel_iommu_capable(enum iommu_cap cap)
4419 unsigned long cap)
4420{ 4418{
4421 struct dmar_domain *dmar_domain = domain->priv;
4422
4423 if (cap == IOMMU_CAP_CACHE_COHERENCY) 4419 if (cap == IOMMU_CAP_CACHE_COHERENCY)
4424 return dmar_domain->iommu_snooping; 4420 return domain_update_iommu_snooping(NULL) == 1;
4425 if (cap == IOMMU_CAP_INTR_REMAP) 4421 if (cap == IOMMU_CAP_INTR_REMAP)
4426 return irq_remapping_enabled; 4422 return irq_remapping_enabled == 1;
4427 4423
4428 return 0; 4424 return false;
4429} 4425}
4430 4426
4431static int intel_iommu_add_device(struct device *dev) 4427static int intel_iommu_add_device(struct device *dev)
@@ -4464,6 +4460,7 @@ static void intel_iommu_remove_device(struct device *dev)
4464} 4460}
4465 4461
4466static const struct iommu_ops intel_iommu_ops = { 4462static const struct iommu_ops intel_iommu_ops = {
4463 .capable = intel_iommu_capable,
4467 .domain_init = intel_iommu_domain_init, 4464 .domain_init = intel_iommu_domain_init,
4468 .domain_destroy = intel_iommu_domain_destroy, 4465 .domain_destroy = intel_iommu_domain_destroy,
4469 .attach_dev = intel_iommu_attach_device, 4466 .attach_dev = intel_iommu_attach_device,
@@ -4471,7 +4468,6 @@ static const struct iommu_ops intel_iommu_ops = {
4471 .map = intel_iommu_map, 4468 .map = intel_iommu_map,
4472 .unmap = intel_iommu_unmap, 4469 .unmap = intel_iommu_unmap,
4473 .iova_to_phys = intel_iommu_iova_to_phys, 4470 .iova_to_phys = intel_iommu_iova_to_phys,
4474 .domain_has_cap = intel_iommu_domain_has_cap,
4475 .add_device = intel_iommu_add_device, 4471 .add_device = intel_iommu_add_device,
4476 .remove_device = intel_iommu_remove_device, 4472 .remove_device = intel_iommu_remove_device,
4477 .pgsize_bitmap = INTEL_IOMMU_PGSIZES, 4473 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
diff --git a/drivers/iommu/intel_irq_remapping.c b/drivers/iommu/intel_irq_remapping.c
index 0df41f6264f5..7c80661b35c1 100644
--- a/drivers/iommu/intel_irq_remapping.c
+++ b/drivers/iommu/intel_irq_remapping.c
@@ -438,8 +438,7 @@ static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
438 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE); 438 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
439 439
440 /* Set interrupt-remapping table pointer */ 440 /* Set interrupt-remapping table pointer */
441 iommu->gcmd |= DMA_GCMD_SIRTP; 441 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
442 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
443 442
444 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, 443 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
445 readl, (sts & DMA_GSTS_IRTPS), sts); 444 readl, (sts & DMA_GSTS_IRTPS), sts);
@@ -1139,7 +1138,7 @@ static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1139 return ret; 1138 return ret;
1140} 1139}
1141 1140
1142static int intel_setup_hpet_msi(unsigned int irq, unsigned int id) 1141static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id)
1143{ 1142{
1144 int ret = -1; 1143 int ret = -1;
1145 struct intel_iommu *iommu; 1144 struct intel_iommu *iommu;
@@ -1170,5 +1169,5 @@ struct irq_remap_ops intel_irq_remap_ops = {
1170 .compose_msi_msg = intel_compose_msi_msg, 1169 .compose_msi_msg = intel_compose_msi_msg,
1171 .msi_alloc_irq = intel_msi_alloc_irq, 1170 .msi_alloc_irq = intel_msi_alloc_irq,
1172 .msi_setup_irq = intel_msi_setup_irq, 1171 .msi_setup_irq = intel_msi_setup_irq,
1173 .setup_hpet_msi = intel_setup_hpet_msi, 1172 .alloc_hpet_msi = intel_alloc_hpet_msi,
1174}; 1173};
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 0639b9274b11..ed8b04867b1f 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -30,6 +30,7 @@
30#include <linux/notifier.h> 30#include <linux/notifier.h>
31#include <linux/err.h> 31#include <linux/err.h>
32#include <linux/pci.h> 32#include <linux/pci.h>
33#include <linux/bitops.h>
33#include <trace/events/iommu.h> 34#include <trace/events/iommu.h>
34 35
35static struct kset *iommu_group_kset; 36static struct kset *iommu_group_kset;
@@ -519,6 +520,9 @@ int iommu_group_id(struct iommu_group *group)
519} 520}
520EXPORT_SYMBOL_GPL(iommu_group_id); 521EXPORT_SYMBOL_GPL(iommu_group_id);
521 522
523static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
524 unsigned long *devfns);
525
522/* 526/*
523 * To consider a PCI device isolated, we require ACS to support Source 527 * To consider a PCI device isolated, we require ACS to support Source
524 * Validation, Request Redirection, Completer Redirection, and Upstream 528 * Validation, Request Redirection, Completer Redirection, and Upstream
@@ -529,6 +533,86 @@ EXPORT_SYMBOL_GPL(iommu_group_id);
529 */ 533 */
530#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) 534#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
531 535
536/*
537 * For multifunction devices which are not isolated from each other, find
538 * all the other non-isolated functions and look for existing groups. For
539 * each function, we also need to look for aliases to or from other devices
540 * that may already have a group.
541 */
542static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
543 unsigned long *devfns)
544{
545 struct pci_dev *tmp = NULL;
546 struct iommu_group *group;
547
548 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
549 return NULL;
550
551 for_each_pci_dev(tmp) {
552 if (tmp == pdev || tmp->bus != pdev->bus ||
553 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
554 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
555 continue;
556
557 group = get_pci_alias_group(tmp, devfns);
558 if (group) {
559 pci_dev_put(tmp);
560 return group;
561 }
562 }
563
564 return NULL;
565}
566
567/*
568 * Look for aliases to or from the given device for exisiting groups. The
569 * dma_alias_devfn only supports aliases on the same bus, therefore the search
570 * space is quite small (especially since we're really only looking at pcie
571 * device, and therefore only expect multiple slots on the root complex or
572 * downstream switch ports). It's conceivable though that a pair of
573 * multifunction devices could have aliases between them that would cause a
574 * loop. To prevent this, we use a bitmap to track where we've been.
575 */
576static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
577 unsigned long *devfns)
578{
579 struct pci_dev *tmp = NULL;
580 struct iommu_group *group;
581
582 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
583 return NULL;
584
585 group = iommu_group_get(&pdev->dev);
586 if (group)
587 return group;
588
589 for_each_pci_dev(tmp) {
590 if (tmp == pdev || tmp->bus != pdev->bus)
591 continue;
592
593 /* We alias them or they alias us */
594 if (((pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
595 pdev->dma_alias_devfn == tmp->devfn) ||
596 ((tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
597 tmp->dma_alias_devfn == pdev->devfn)) {
598
599 group = get_pci_alias_group(tmp, devfns);
600 if (group) {
601 pci_dev_put(tmp);
602 return group;
603 }
604
605 group = get_pci_function_alias_group(tmp, devfns);
606 if (group) {
607 pci_dev_put(tmp);
608 return group;
609 }
610 }
611 }
612
613 return NULL;
614}
615
532struct group_for_pci_data { 616struct group_for_pci_data {
533 struct pci_dev *pdev; 617 struct pci_dev *pdev;
534 struct iommu_group *group; 618 struct iommu_group *group;
@@ -557,7 +641,7 @@ static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
557 struct group_for_pci_data data; 641 struct group_for_pci_data data;
558 struct pci_bus *bus; 642 struct pci_bus *bus;
559 struct iommu_group *group = NULL; 643 struct iommu_group *group = NULL;
560 struct pci_dev *tmp; 644 u64 devfns[4] = { 0 };
561 645
562 /* 646 /*
563 * Find the upstream DMA alias for the device. A device must not 647 * Find the upstream DMA alias for the device. A device must not
@@ -591,76 +675,21 @@ static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
591 } 675 }
592 676
593 /* 677 /*
594 * Next we need to consider DMA alias quirks. If one device aliases 678 * Look for existing groups on device aliases. If we alias another
595 * to another, they should be grouped together. It's theoretically 679 * device or another device aliases us, use the same group.
596 * possible that aliases could create chains of devices where each
597 * device aliases another device. If we then factor in multifunction
598 * ACS grouping requirements, each alias could incorporate a new slot
599 * with multiple functions, each with aliases. This is all extremely
600 * unlikely as DMA alias quirks are typically only used for PCIe
601 * devices where we usually have a single slot per bus. Furthermore,
602 * the alias quirk is usually to another function within the slot
603 * (and ACS multifunction is not supported) or to a different slot
604 * that doesn't physically exist. The likely scenario is therefore
605 * that everything on the bus gets grouped together. To reduce the
606 * problem space, share the IOMMU group for all devices on the bus
607 * if a DMA alias quirk is present on the bus.
608 */ 680 */
609 tmp = NULL; 681 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
610 for_each_pci_dev(tmp) { 682 if (group)
611 if (tmp->bus != pdev->bus || 683 return group;
612 !(tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN))
613 continue;
614
615 pci_dev_put(tmp);
616 tmp = NULL;
617
618 /* We have an alias quirk, search for an existing group */
619 for_each_pci_dev(tmp) {
620 struct iommu_group *group_tmp;
621
622 if (tmp->bus != pdev->bus)
623 continue;
624
625 group_tmp = iommu_group_get(&tmp->dev);
626 if (!group) {
627 group = group_tmp;
628 continue;
629 }
630
631 if (group_tmp) {
632 WARN_ON(group != group_tmp);
633 iommu_group_put(group_tmp);
634 }
635 }
636
637 return group ? group : iommu_group_alloc();
638 }
639
640 /*
641 * Non-multifunction devices or multifunction devices supporting
642 * ACS get their own group.
643 */
644 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
645 return iommu_group_alloc();
646 684
647 /* 685 /*
648 * Multifunction devices not supporting ACS share a group with other 686 * Look for existing groups on non-isolated functions on the same
649 * similar devices in the same slot. 687 * slot and aliases of those funcions, if any. No need to clear
688 * the search bitmap, the tested devfns are still valid.
650 */ 689 */
651 tmp = NULL; 690 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
652 for_each_pci_dev(tmp) { 691 if (group)
653 if (tmp == pdev || tmp->bus != pdev->bus || 692 return group;
654 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
655 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
656 continue;
657
658 group = iommu_group_get(&tmp->dev);
659 if (group) {
660 pci_dev_put(tmp);
661 return group;
662 }
663 }
664 693
665 /* No shared group found, allocate new */ 694 /* No shared group found, allocate new */
666 return iommu_group_alloc(); 695 return iommu_group_alloc();
@@ -770,18 +799,26 @@ static int iommu_bus_notifier(struct notifier_block *nb,
770 return 0; 799 return 0;
771} 800}
772 801
773static struct notifier_block iommu_bus_nb = { 802static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
774 .notifier_call = iommu_bus_notifier,
775};
776
777static void iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
778{ 803{
804 int err;
805 struct notifier_block *nb;
779 struct iommu_callback_data cb = { 806 struct iommu_callback_data cb = {
780 .ops = ops, 807 .ops = ops,
781 }; 808 };
782 809
783 bus_register_notifier(bus, &iommu_bus_nb); 810 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
784 bus_for_each_dev(bus, NULL, &cb, add_iommu_group); 811 if (!nb)
812 return -ENOMEM;
813
814 nb->notifier_call = iommu_bus_notifier;
815
816 err = bus_register_notifier(bus, nb);
817 if (err) {
818 kfree(nb);
819 return err;
820 }
821 return bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
785} 822}
786 823
787/** 824/**
@@ -805,9 +842,7 @@ int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
805 bus->iommu_ops = ops; 842 bus->iommu_ops = ops;
806 843
807 /* Do IOMMU specific setup for this bus-type */ 844 /* Do IOMMU specific setup for this bus-type */
808 iommu_bus_init(bus, ops); 845 return iommu_bus_init(bus, ops);
809
810 return 0;
811} 846}
812EXPORT_SYMBOL_GPL(bus_set_iommu); 847EXPORT_SYMBOL_GPL(bus_set_iommu);
813 848
@@ -817,6 +852,15 @@ bool iommu_present(struct bus_type *bus)
817} 852}
818EXPORT_SYMBOL_GPL(iommu_present); 853EXPORT_SYMBOL_GPL(iommu_present);
819 854
855bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
856{
857 if (!bus->iommu_ops || !bus->iommu_ops->capable)
858 return false;
859
860 return bus->iommu_ops->capable(cap);
861}
862EXPORT_SYMBOL_GPL(iommu_capable);
863
820/** 864/**
821 * iommu_set_fault_handler() - set a fault handler for an iommu domain 865 * iommu_set_fault_handler() - set a fault handler for an iommu domain
822 * @domain: iommu domain 866 * @domain: iommu domain
@@ -947,16 +991,6 @@ phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
947} 991}
948EXPORT_SYMBOL_GPL(iommu_iova_to_phys); 992EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
949 993
950int iommu_domain_has_cap(struct iommu_domain *domain,
951 unsigned long cap)
952{
953 if (unlikely(domain->ops->domain_has_cap == NULL))
954 return 0;
955
956 return domain->ops->domain_has_cap(domain, cap);
957}
958EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
959
960static size_t iommu_pgsize(struct iommu_domain *domain, 994static size_t iommu_pgsize(struct iommu_domain *domain,
961 unsigned long addr_merge, size_t size) 995 unsigned long addr_merge, size_t size)
962{ 996{
diff --git a/drivers/iommu/irq_remapping.c b/drivers/iommu/irq_remapping.c
index 33c439524080..74a1767c89b5 100644
--- a/drivers/iommu/irq_remapping.c
+++ b/drivers/iommu/irq_remapping.c
@@ -12,6 +12,7 @@
12#include <asm/processor.h> 12#include <asm/processor.h>
13#include <asm/x86_init.h> 13#include <asm/x86_init.h>
14#include <asm/apic.h> 14#include <asm/apic.h>
15#include <asm/hpet.h>
15 16
16#include "irq_remapping.h" 17#include "irq_remapping.h"
17 18
@@ -345,10 +346,16 @@ static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
345 346
346int setup_hpet_msi_remapped(unsigned int irq, unsigned int id) 347int setup_hpet_msi_remapped(unsigned int irq, unsigned int id)
347{ 348{
348 if (!remap_ops || !remap_ops->setup_hpet_msi) 349 int ret;
350
351 if (!remap_ops || !remap_ops->alloc_hpet_msi)
349 return -ENODEV; 352 return -ENODEV;
350 353
351 return remap_ops->setup_hpet_msi(irq, id); 354 ret = remap_ops->alloc_hpet_msi(irq, id);
355 if (ret)
356 return -EINVAL;
357
358 return default_setup_hpet_msi(irq, id);
352} 359}
353 360
354void panic_if_irq_remap(const char *msg) 361void panic_if_irq_remap(const char *msg)
diff --git a/drivers/iommu/irq_remapping.h b/drivers/iommu/irq_remapping.h
index 90c4dae5a46b..fde250f86e60 100644
--- a/drivers/iommu/irq_remapping.h
+++ b/drivers/iommu/irq_remapping.h
@@ -80,7 +80,7 @@ struct irq_remap_ops {
80 int (*msi_setup_irq)(struct pci_dev *, unsigned int, int, int); 80 int (*msi_setup_irq)(struct pci_dev *, unsigned int, int, int);
81 81
82 /* Setup interrupt remapping for an HPET MSI */ 82 /* Setup interrupt remapping for an HPET MSI */
83 int (*setup_hpet_msi)(unsigned int, unsigned int); 83 int (*alloc_hpet_msi)(unsigned int, unsigned int);
84}; 84};
85 85
86extern struct irq_remap_ops intel_irq_remap_ops; 86extern struct irq_remap_ops intel_irq_remap_ops;
diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c
index 49f41d6e02f1..6e3dcc289d59 100644
--- a/drivers/iommu/msm_iommu.c
+++ b/drivers/iommu/msm_iommu.c
@@ -603,10 +603,9 @@ fail:
603 return ret; 603 return ret;
604} 604}
605 605
606static int msm_iommu_domain_has_cap(struct iommu_domain *domain, 606static bool msm_iommu_capable(enum iommu_cap cap)
607 unsigned long cap)
608{ 607{
609 return 0; 608 return false;
610} 609}
611 610
612static void print_ctx_regs(void __iomem *base, int ctx) 611static void print_ctx_regs(void __iomem *base, int ctx)
@@ -675,6 +674,7 @@ fail:
675} 674}
676 675
677static const struct iommu_ops msm_iommu_ops = { 676static const struct iommu_ops msm_iommu_ops = {
677 .capable = msm_iommu_capable,
678 .domain_init = msm_iommu_domain_init, 678 .domain_init = msm_iommu_domain_init,
679 .domain_destroy = msm_iommu_domain_destroy, 679 .domain_destroy = msm_iommu_domain_destroy,
680 .attach_dev = msm_iommu_attach_dev, 680 .attach_dev = msm_iommu_attach_dev,
@@ -682,7 +682,6 @@ static const struct iommu_ops msm_iommu_ops = {
682 .map = msm_iommu_map, 682 .map = msm_iommu_map,
683 .unmap = msm_iommu_unmap, 683 .unmap = msm_iommu_unmap,
684 .iova_to_phys = msm_iommu_iova_to_phys, 684 .iova_to_phys = msm_iommu_iova_to_phys,
685 .domain_has_cap = msm_iommu_domain_has_cap,
686 .pgsize_bitmap = MSM_IOMMU_PGSIZES, 685 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
687}; 686};
688 687
diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
index e202b0c24120..36278870e84a 100644
--- a/drivers/iommu/omap-iommu.c
+++ b/drivers/iommu/omap-iommu.c
@@ -26,6 +26,7 @@
26#include <linux/of.h> 26#include <linux/of.h>
27#include <linux/of_iommu.h> 27#include <linux/of_iommu.h>
28#include <linux/of_irq.h> 28#include <linux/of_irq.h>
29#include <linux/of_platform.h>
29 30
30#include <asm/cacheflush.h> 31#include <asm/cacheflush.h>
31 32
@@ -892,19 +893,11 @@ static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
892 goto err_enable; 893 goto err_enable;
893 flush_iotlb_all(obj); 894 flush_iotlb_all(obj);
894 895
895 if (!try_module_get(obj->owner)) {
896 err = -ENODEV;
897 goto err_module;
898 }
899
900 spin_unlock(&obj->iommu_lock); 896 spin_unlock(&obj->iommu_lock);
901 897
902 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); 898 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
903 return obj; 899 return obj;
904 900
905err_module:
906 if (obj->refcount == 1)
907 iommu_disable(obj);
908err_enable: 901err_enable:
909 obj->refcount--; 902 obj->refcount--;
910 spin_unlock(&obj->iommu_lock); 903 spin_unlock(&obj->iommu_lock);
@@ -925,8 +918,6 @@ static void omap_iommu_detach(struct omap_iommu *obj)
925 if (--obj->refcount == 0) 918 if (--obj->refcount == 0)
926 iommu_disable(obj); 919 iommu_disable(obj);
927 920
928 module_put(obj->owner);
929
930 obj->iopgd = NULL; 921 obj->iopgd = NULL;
931 922
932 spin_unlock(&obj->iommu_lock); 923 spin_unlock(&obj->iommu_lock);
@@ -1006,7 +997,7 @@ static int omap_iommu_remove(struct platform_device *pdev)
1006 return 0; 997 return 0;
1007} 998}
1008 999
1009static struct of_device_id omap_iommu_of_match[] = { 1000static const struct of_device_id omap_iommu_of_match[] = {
1010 { .compatible = "ti,omap2-iommu" }, 1001 { .compatible = "ti,omap2-iommu" },
1011 { .compatible = "ti,omap4-iommu" }, 1002 { .compatible = "ti,omap4-iommu" },
1012 { .compatible = "ti,dra7-iommu" }, 1003 { .compatible = "ti,dra7-iommu" },
@@ -1091,6 +1082,11 @@ omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1091 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; 1082 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1092 int ret = 0; 1083 int ret = 0;
1093 1084
1085 if (!arch_data || !arch_data->name) {
1086 dev_err(dev, "device doesn't have an associated iommu\n");
1087 return -EINVAL;
1088 }
1089
1094 spin_lock(&omap_domain->lock); 1090 spin_lock(&omap_domain->lock);
1095 1091
1096 /* only a single device is supported per domain for now */ 1092 /* only a single device is supported per domain for now */
@@ -1239,6 +1235,7 @@ static int omap_iommu_add_device(struct device *dev)
1239{ 1235{
1240 struct omap_iommu_arch_data *arch_data; 1236 struct omap_iommu_arch_data *arch_data;
1241 struct device_node *np; 1237 struct device_node *np;
1238 struct platform_device *pdev;
1242 1239
1243 /* 1240 /*
1244 * Allocate the archdata iommu structure for DT-based devices. 1241 * Allocate the archdata iommu structure for DT-based devices.
@@ -1253,13 +1250,19 @@ static int omap_iommu_add_device(struct device *dev)
1253 if (!np) 1250 if (!np)
1254 return 0; 1251 return 0;
1255 1252
1253 pdev = of_find_device_by_node(np);
1254 if (WARN_ON(!pdev)) {
1255 of_node_put(np);
1256 return -EINVAL;
1257 }
1258
1256 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL); 1259 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1257 if (!arch_data) { 1260 if (!arch_data) {
1258 of_node_put(np); 1261 of_node_put(np);
1259 return -ENOMEM; 1262 return -ENOMEM;
1260 } 1263 }
1261 1264
1262 arch_data->name = kstrdup(dev_name(dev), GFP_KERNEL); 1265 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
1263 dev->archdata.iommu = arch_data; 1266 dev->archdata.iommu = arch_data;
1264 1267
1265 of_node_put(np); 1268 of_node_put(np);
diff --git a/drivers/iommu/omap-iommu.h b/drivers/iommu/omap-iommu.h
index 1275a822934b..4f1b68c08c15 100644
--- a/drivers/iommu/omap-iommu.h
+++ b/drivers/iommu/omap-iommu.h
@@ -28,7 +28,6 @@ struct iotlb_entry {
28 28
29struct omap_iommu { 29struct omap_iommu {
30 const char *name; 30 const char *name;
31 struct module *owner;
32 void __iomem *regbase; 31 void __iomem *regbase;
33 struct device *dev; 32 struct device *dev;
34 void *isr_priv; 33 void *isr_priv;
diff --git a/drivers/iommu/tegra-gart.c b/drivers/iommu/tegra-gart.c
index b10a8ecede8e..a6d76abf2c06 100644
--- a/drivers/iommu/tegra-gart.c
+++ b/drivers/iommu/tegra-gart.c
@@ -303,13 +303,13 @@ static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
303 return pa; 303 return pa;
304} 304}
305 305
306static int gart_iommu_domain_has_cap(struct iommu_domain *domain, 306static bool gart_iommu_capable(enum iommu_cap cap)
307 unsigned long cap)
308{ 307{
309 return 0; 308 return false;
310} 309}
311 310
312static const struct iommu_ops gart_iommu_ops = { 311static const struct iommu_ops gart_iommu_ops = {
312 .capable = gart_iommu_capable,
313 .domain_init = gart_iommu_domain_init, 313 .domain_init = gart_iommu_domain_init,
314 .domain_destroy = gart_iommu_domain_destroy, 314 .domain_destroy = gart_iommu_domain_destroy,
315 .attach_dev = gart_iommu_attach_dev, 315 .attach_dev = gart_iommu_attach_dev,
@@ -317,7 +317,6 @@ static const struct iommu_ops gart_iommu_ops = {
317 .map = gart_iommu_map, 317 .map = gart_iommu_map,
318 .unmap = gart_iommu_unmap, 318 .unmap = gart_iommu_unmap,
319 .iova_to_phys = gart_iommu_iova_to_phys, 319 .iova_to_phys = gart_iommu_iova_to_phys,
320 .domain_has_cap = gart_iommu_domain_has_cap,
321 .pgsize_bitmap = GART_IOMMU_PGSIZES, 320 .pgsize_bitmap = GART_IOMMU_PGSIZES,
322}; 321};
323 322
@@ -416,7 +415,7 @@ static const struct dev_pm_ops tegra_gart_pm_ops = {
416 .resume = tegra_gart_resume, 415 .resume = tegra_gart_resume,
417}; 416};
418 417
419static struct of_device_id tegra_gart_of_match[] = { 418static const struct of_device_id tegra_gart_of_match[] = {
420 { .compatible = "nvidia,tegra20-gart", }, 419 { .compatible = "nvidia,tegra20-gart", },
421 { }, 420 { },
422}; 421};
diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c
index 3ded3894623c..3afdf43f732a 100644
--- a/drivers/iommu/tegra-smmu.c
+++ b/drivers/iommu/tegra-smmu.c
@@ -780,10 +780,9 @@ static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
780 return PFN_PHYS(pfn); 780 return PFN_PHYS(pfn);
781} 781}
782 782
783static int smmu_iommu_domain_has_cap(struct iommu_domain *domain, 783static bool smmu_iommu_capable(enum iommu_cap cap)
784 unsigned long cap)
785{ 784{
786 return 0; 785 return false;
787} 786}
788 787
789static int smmu_iommu_attach_dev(struct iommu_domain *domain, 788static int smmu_iommu_attach_dev(struct iommu_domain *domain,
@@ -949,6 +948,7 @@ static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
949} 948}
950 949
951static const struct iommu_ops smmu_iommu_ops = { 950static const struct iommu_ops smmu_iommu_ops = {
951 .capable = smmu_iommu_capable,
952 .domain_init = smmu_iommu_domain_init, 952 .domain_init = smmu_iommu_domain_init,
953 .domain_destroy = smmu_iommu_domain_destroy, 953 .domain_destroy = smmu_iommu_domain_destroy,
954 .attach_dev = smmu_iommu_attach_dev, 954 .attach_dev = smmu_iommu_attach_dev,
@@ -956,7 +956,6 @@ static const struct iommu_ops smmu_iommu_ops = {
956 .map = smmu_iommu_map, 956 .map = smmu_iommu_map,
957 .unmap = smmu_iommu_unmap, 957 .unmap = smmu_iommu_unmap,
958 .iova_to_phys = smmu_iommu_iova_to_phys, 958 .iova_to_phys = smmu_iommu_iova_to_phys,
959 .domain_has_cap = smmu_iommu_domain_has_cap,
960 .pgsize_bitmap = SMMU_IOMMU_PGSIZES, 959 .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
961}; 960};
962 961
@@ -1260,7 +1259,7 @@ static const struct dev_pm_ops tegra_smmu_pm_ops = {
1260 .resume = tegra_smmu_resume, 1259 .resume = tegra_smmu_resume,
1261}; 1260};
1262 1261
1263static struct of_device_id tegra_smmu_of_match[] = { 1262static const struct of_device_id tegra_smmu_of_match[] = {
1264 { .compatible = "nvidia,tegra30-smmu", }, 1263 { .compatible = "nvidia,tegra30-smmu", },
1265 { }, 1264 { },
1266}; 1265};
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 583ccdb2c58f..4a9d666f1e91 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -723,14 +723,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
723 list_add(&group->next, &domain->group_list); 723 list_add(&group->next, &domain->group_list);
724 724
725 if (!allow_unsafe_interrupts && 725 if (!allow_unsafe_interrupts &&
726 !iommu_domain_has_cap(domain->domain, IOMMU_CAP_INTR_REMAP)) { 726 !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
727 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n", 727 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
728 __func__); 728 __func__);
729 ret = -EPERM; 729 ret = -EPERM;
730 goto out_detach; 730 goto out_detach;
731 } 731 }
732 732
733 if (iommu_domain_has_cap(domain->domain, IOMMU_CAP_CACHE_COHERENCY)) 733 if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
734 domain->prot |= IOMMU_CACHE; 734 domain->prot |= IOMMU_CACHE;
735 735
736 /* 736 /*
diff --git a/include/linux/device.h b/include/linux/device.h
index a608e237f0a8..ce1f21608b16 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -181,13 +181,14 @@ extern int bus_unregister_notifier(struct bus_type *bus,
181 * with the device lock held in the core, so be careful. 181 * with the device lock held in the core, so be careful.
182 */ 182 */
183#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 183#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
184#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ 184#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device to be removed */
185#define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be 185#define BUS_NOTIFY_REMOVED_DEVICE 0x00000003 /* device removed */
186#define BUS_NOTIFY_BIND_DRIVER 0x00000004 /* driver about to be
186 bound */ 187 bound */
187#define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */ 188#define BUS_NOTIFY_BOUND_DRIVER 0x00000005 /* driver bound to device */
188#define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be 189#define BUS_NOTIFY_UNBIND_DRIVER 0x00000006 /* driver about to be
189 unbound */ 190 unbound */
190#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound 191#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007 /* driver is unbound
191 from the device */ 192 from the device */
192 193
193extern struct kset *bus_get_kset(struct bus_type *bus); 194extern struct kset *bus_get_kset(struct bus_type *bus);
diff --git a/include/linux/dmar.h b/include/linux/dmar.h
index 1deece46a0ca..593fff99e6bf 100644
--- a/include/linux/dmar.h
+++ b/include/linux/dmar.h
@@ -56,13 +56,19 @@ struct dmar_drhd_unit {
56 struct intel_iommu *iommu; 56 struct intel_iommu *iommu;
57}; 57};
58 58
59struct dmar_pci_path {
60 u8 bus;
61 u8 device;
62 u8 function;
63};
64
59struct dmar_pci_notify_info { 65struct dmar_pci_notify_info {
60 struct pci_dev *dev; 66 struct pci_dev *dev;
61 unsigned long event; 67 unsigned long event;
62 int bus; 68 int bus;
63 u16 seg; 69 u16 seg;
64 u16 level; 70 u16 level;
65 struct acpi_dmar_pci_path path[]; 71 struct dmar_pci_path path[];
66} __attribute__((packed)); 72} __attribute__((packed));
67 73
68extern struct rw_semaphore dmar_global_lock; 74extern struct rw_semaphore dmar_global_lock;
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 7b02bcc85b9e..e6a7c9ff72f2 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -57,8 +57,11 @@ struct iommu_domain {
57 struct iommu_domain_geometry geometry; 57 struct iommu_domain_geometry geometry;
58}; 58};
59 59
60#define IOMMU_CAP_CACHE_COHERENCY 0x1 60enum iommu_cap {
61#define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */ 61 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
62 transactions */
63 IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
64};
62 65
63/* 66/*
64 * Following constraints are specifc to FSL_PAMUV1: 67 * Following constraints are specifc to FSL_PAMUV1:
@@ -95,7 +98,6 @@ enum iommu_attr {
95 * @map: map a physically contiguous memory region to an iommu domain 98 * @map: map a physically contiguous memory region to an iommu domain
96 * @unmap: unmap a physically contiguous memory region from an iommu domain 99 * @unmap: unmap a physically contiguous memory region from an iommu domain
97 * @iova_to_phys: translate iova to physical address 100 * @iova_to_phys: translate iova to physical address
98 * @domain_has_cap: domain capabilities query
99 * @add_device: add device to iommu grouping 101 * @add_device: add device to iommu grouping
100 * @remove_device: remove device from iommu grouping 102 * @remove_device: remove device from iommu grouping
101 * @domain_get_attr: Query domain attributes 103 * @domain_get_attr: Query domain attributes
@@ -103,6 +105,7 @@ enum iommu_attr {
103 * @pgsize_bitmap: bitmap of supported page sizes 105 * @pgsize_bitmap: bitmap of supported page sizes
104 */ 106 */
105struct iommu_ops { 107struct iommu_ops {
108 bool (*capable)(enum iommu_cap);
106 int (*domain_init)(struct iommu_domain *domain); 109 int (*domain_init)(struct iommu_domain *domain);
107 void (*domain_destroy)(struct iommu_domain *domain); 110 void (*domain_destroy)(struct iommu_domain *domain);
108 int (*attach_dev)(struct iommu_domain *domain, struct device *dev); 111 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
@@ -112,8 +115,6 @@ struct iommu_ops {
112 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova, 115 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
113 size_t size); 116 size_t size);
114 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); 117 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
115 int (*domain_has_cap)(struct iommu_domain *domain,
116 unsigned long cap);
117 int (*add_device)(struct device *dev); 118 int (*add_device)(struct device *dev);
118 void (*remove_device)(struct device *dev); 119 void (*remove_device)(struct device *dev);
119 int (*device_group)(struct device *dev, unsigned int *groupid); 120 int (*device_group)(struct device *dev, unsigned int *groupid);
@@ -143,6 +144,7 @@ struct iommu_ops {
143 144
144extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops); 145extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
145extern bool iommu_present(struct bus_type *bus); 146extern bool iommu_present(struct bus_type *bus);
147extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
146extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus); 148extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
147extern struct iommu_group *iommu_group_get_by_id(int id); 149extern struct iommu_group *iommu_group_get_by_id(int id);
148extern void iommu_domain_free(struct iommu_domain *domain); 150extern void iommu_domain_free(struct iommu_domain *domain);
@@ -155,8 +157,6 @@ extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
155extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, 157extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
156 size_t size); 158 size_t size);
157extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova); 159extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
158extern int iommu_domain_has_cap(struct iommu_domain *domain,
159 unsigned long cap);
160extern void iommu_set_fault_handler(struct iommu_domain *domain, 160extern void iommu_set_fault_handler(struct iommu_domain *domain,
161 iommu_fault_handler_t handler, void *token); 161 iommu_fault_handler_t handler, void *token);
162 162
@@ -251,6 +251,11 @@ static inline bool iommu_present(struct bus_type *bus)
251 return false; 251 return false;
252} 252}
253 253
254static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
255{
256 return false;
257}
258
254static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) 259static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
255{ 260{
256 return NULL; 261 return NULL;
@@ -305,12 +310,6 @@ static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_ad
305 return 0; 310 return 0;
306} 311}
307 312
308static inline int iommu_domain_has_cap(struct iommu_domain *domain,
309 unsigned long cap)
310{
311 return 0;
312}
313
314static inline void iommu_set_fault_handler(struct iommu_domain *domain, 313static inline void iommu_set_fault_handler(struct iommu_domain *domain,
315 iommu_fault_handler_t handler, void *token) 314 iommu_fault_handler_t handler, void *token)
316{ 315{
diff --git a/virt/kvm/iommu.c b/virt/kvm/iommu.c
index e723bb91aa34..e51d9f9b995f 100644
--- a/virt/kvm/iommu.c
+++ b/virt/kvm/iommu.c
@@ -191,8 +191,7 @@ int kvm_assign_device(struct kvm *kvm,
191 return r; 191 return r;
192 } 192 }
193 193
194 noncoherent = !iommu_domain_has_cap(kvm->arch.iommu_domain, 194 noncoherent = !iommu_capable(&pci_bus_type, IOMMU_CAP_CACHE_COHERENCY);
195 IOMMU_CAP_CACHE_COHERENCY);
196 195
197 /* Check if need to update IOMMU page table for guest memory */ 196 /* Check if need to update IOMMU page table for guest memory */
198 if (noncoherent != kvm->arch.iommu_noncoherent) { 197 if (noncoherent != kvm->arch.iommu_noncoherent) {
@@ -254,8 +253,7 @@ int kvm_iommu_map_guest(struct kvm *kvm)
254 } 253 }
255 254
256 if (!allow_unsafe_assigned_interrupts && 255 if (!allow_unsafe_assigned_interrupts &&
257 !iommu_domain_has_cap(kvm->arch.iommu_domain, 256 !iommu_capable(&pci_bus_type, IOMMU_CAP_INTR_REMAP)) {
258 IOMMU_CAP_INTR_REMAP)) {
259 printk(KERN_WARNING "%s: No interrupt remapping support," 257 printk(KERN_WARNING "%s: No interrupt remapping support,"
260 " disallowing device assignment." 258 " disallowing device assignment."
261 " Re-enble with \"allow_unsafe_assigned_interrupts=1\"" 259 " Re-enble with \"allow_unsafe_assigned_interrupts=1\""