aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/iommu/Kconfig5
-rw-r--r--drivers/iommu/amd_iommu.c69
-rw-r--r--drivers/iommu/amd_iommu_types.h1
-rw-r--r--drivers/iommu/dmar.c8
-rw-r--r--drivers/iommu/intel-iommu.c101
-rw-r--r--drivers/iommu/intel-svm.c2
-rw-r--r--drivers/iommu/io-pgtable-arm-v7s.c5
-rw-r--r--drivers/iommu/io-pgtable-arm.c18
-rw-r--r--drivers/iommu/iommu.c7
-rw-r--r--drivers/iommu/qcom_iommu.c6
-rw-r--r--drivers/iommu/tegra-gart.c15
-rw-r--r--include/linux/intel-iommu.h1
12 files changed, 122 insertions, 116 deletions
diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index df171cb85822..c76157e57f6b 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -23,7 +23,7 @@ config IOMMU_IO_PGTABLE
23config IOMMU_IO_PGTABLE_LPAE 23config IOMMU_IO_PGTABLE_LPAE
24 bool "ARMv7/v8 Long Descriptor Format" 24 bool "ARMv7/v8 Long Descriptor Format"
25 select IOMMU_IO_PGTABLE 25 select IOMMU_IO_PGTABLE
26 depends on HAS_DMA && (ARM || ARM64 || (COMPILE_TEST && !GENERIC_ATOMIC64)) 26 depends on ARM || ARM64 || (COMPILE_TEST && !GENERIC_ATOMIC64)
27 help 27 help
28 Enable support for the ARM long descriptor pagetable format. 28 Enable support for the ARM long descriptor pagetable format.
29 This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page 29 This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page
@@ -42,7 +42,7 @@ config IOMMU_IO_PGTABLE_LPAE_SELFTEST
42config IOMMU_IO_PGTABLE_ARMV7S 42config IOMMU_IO_PGTABLE_ARMV7S
43 bool "ARMv7/v8 Short Descriptor Format" 43 bool "ARMv7/v8 Short Descriptor Format"
44 select IOMMU_IO_PGTABLE 44 select IOMMU_IO_PGTABLE
45 depends on HAS_DMA && (ARM || ARM64 || COMPILE_TEST) 45 depends on ARM || ARM64 || COMPILE_TEST
46 help 46 help
47 Enable support for the ARM Short-descriptor pagetable format. 47 Enable support for the ARM Short-descriptor pagetable format.
48 This supports 32-bit virtual and physical addresses mapped using 48 This supports 32-bit virtual and physical addresses mapped using
@@ -376,7 +376,6 @@ config QCOM_IOMMU
376 # Note: iommu drivers cannot (yet?) be built as modules 376 # Note: iommu drivers cannot (yet?) be built as modules
377 bool "Qualcomm IOMMU Support" 377 bool "Qualcomm IOMMU Support"
378 depends on ARCH_QCOM || (COMPILE_TEST && !GENERIC_ATOMIC64) 378 depends on ARCH_QCOM || (COMPILE_TEST && !GENERIC_ATOMIC64)
379 depends on HAS_DMA
380 select IOMMU_API 379 select IOMMU_API
381 select IOMMU_IO_PGTABLE_LPAE 380 select IOMMU_IO_PGTABLE_LPAE
382 select ARM_DMA_USE_IOMMU 381 select ARM_DMA_USE_IOMMU
diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
index 8fb8c737fffe..8cb28def43e3 100644
--- a/drivers/iommu/amd_iommu.c
+++ b/drivers/iommu/amd_iommu.c
@@ -544,7 +544,7 @@ static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
544static void iommu_print_event(struct amd_iommu *iommu, void *__evt) 544static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
545{ 545{
546 struct device *dev = iommu->iommu.dev; 546 struct device *dev = iommu->iommu.dev;
547 int type, devid, domid, flags; 547 int type, devid, pasid, flags, tag;
548 volatile u32 *event = __evt; 548 volatile u32 *event = __evt;
549 int count = 0; 549 int count = 0;
550 u64 address; 550 u64 address;
@@ -552,7 +552,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
552retry: 552retry:
553 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; 553 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
554 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; 554 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
555 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK; 555 pasid = PPR_PASID(*(u64 *)&event[0]);
556 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; 556 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
557 address = (u64)(((u64)event[3]) << 32) | event[2]; 557 address = (u64)(((u64)event[3]) << 32) | event[2];
558 558
@@ -567,7 +567,7 @@ retry:
567 } 567 }
568 568
569 if (type == EVENT_TYPE_IO_FAULT) { 569 if (type == EVENT_TYPE_IO_FAULT) {
570 amd_iommu_report_page_fault(devid, domid, address, flags); 570 amd_iommu_report_page_fault(devid, pasid, address, flags);
571 return; 571 return;
572 } else { 572 } else {
573 dev_err(dev, "AMD-Vi: Event logged ["); 573 dev_err(dev, "AMD-Vi: Event logged [");
@@ -575,10 +575,9 @@ retry:
575 575
576 switch (type) { 576 switch (type) {
577 case EVENT_TYPE_ILL_DEV: 577 case EVENT_TYPE_ILL_DEV:
578 dev_err(dev, "ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x " 578 dev_err(dev, "ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
579 "address=0x%016llx flags=0x%04x]\n",
580 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 579 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
581 address, flags); 580 pasid, address, flags);
582 dump_dte_entry(devid); 581 dump_dte_entry(devid);
583 break; 582 break;
584 case EVENT_TYPE_DEV_TAB_ERR: 583 case EVENT_TYPE_DEV_TAB_ERR:
@@ -588,34 +587,38 @@ retry:
588 address, flags); 587 address, flags);
589 break; 588 break;
590 case EVENT_TYPE_PAGE_TAB_ERR: 589 case EVENT_TYPE_PAGE_TAB_ERR:
591 dev_err(dev, "PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x " 590 dev_err(dev, "PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
592 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
593 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 591 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
594 domid, address, flags); 592 pasid, address, flags);
595 break; 593 break;
596 case EVENT_TYPE_ILL_CMD: 594 case EVENT_TYPE_ILL_CMD:
597 dev_err(dev, "ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); 595 dev_err(dev, "ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
598 dump_command(address); 596 dump_command(address);
599 break; 597 break;
600 case EVENT_TYPE_CMD_HARD_ERR: 598 case EVENT_TYPE_CMD_HARD_ERR:
601 dev_err(dev, "COMMAND_HARDWARE_ERROR address=0x%016llx " 599 dev_err(dev, "COMMAND_HARDWARE_ERROR address=0x%016llx flags=0x%04x]\n",
602 "flags=0x%04x]\n", address, flags); 600 address, flags);
603 break; 601 break;
604 case EVENT_TYPE_IOTLB_INV_TO: 602 case EVENT_TYPE_IOTLB_INV_TO:
605 dev_err(dev, "IOTLB_INV_TIMEOUT device=%02x:%02x.%x " 603 dev_err(dev, "IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%016llx]\n",
606 "address=0x%016llx]\n",
607 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 604 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
608 address); 605 address);
609 break; 606 break;
610 case EVENT_TYPE_INV_DEV_REQ: 607 case EVENT_TYPE_INV_DEV_REQ:
611 dev_err(dev, "INVALID_DEVICE_REQUEST device=%02x:%02x.%x " 608 dev_err(dev, "INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
612 "address=0x%016llx flags=0x%04x]\n",
613 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 609 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
614 address, flags); 610 pasid, address, flags);
611 break;
612 case EVENT_TYPE_INV_PPR_REQ:
613 pasid = ((event[0] >> 16) & 0xFFFF)
614 | ((event[1] << 6) & 0xF0000);
615 tag = event[1] & 0x03FF;
616 dev_err(dev, "INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
617 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
618 pasid, address, flags);
615 break; 619 break;
616 default: 620 default:
617 dev_err(dev, KERN_ERR "UNKNOWN event[0]=0x%08x event[1]=0x%08x " 621 dev_err(dev, "UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
618 "event[2]=0x%08x event[3]=0x%08x\n",
619 event[0], event[1], event[2], event[3]); 622 event[0], event[1], event[2], event[3]);
620 } 623 }
621 624
@@ -1911,15 +1914,6 @@ static void do_detach(struct iommu_dev_data *dev_data)
1911 struct amd_iommu *iommu; 1914 struct amd_iommu *iommu;
1912 u16 alias; 1915 u16 alias;
1913 1916
1914 /*
1915 * First check if the device is still attached. It might already
1916 * be detached from its domain because the generic
1917 * iommu_detach_group code detached it and we try again here in
1918 * our alias handling.
1919 */
1920 if (!dev_data->domain)
1921 return;
1922
1923 iommu = amd_iommu_rlookup_table[dev_data->devid]; 1917 iommu = amd_iommu_rlookup_table[dev_data->devid];
1924 alias = dev_data->alias; 1918 alias = dev_data->alias;
1925 1919
@@ -1939,8 +1933,8 @@ static void do_detach(struct iommu_dev_data *dev_data)
1939} 1933}
1940 1934
1941/* 1935/*
1942 * If a device is not yet associated with a domain, this function does 1936 * If a device is not yet associated with a domain, this function makes the
1943 * assigns it visible for the hardware 1937 * device visible in the domain
1944 */ 1938 */
1945static int __attach_device(struct iommu_dev_data *dev_data, 1939static int __attach_device(struct iommu_dev_data *dev_data,
1946 struct protection_domain *domain) 1940 struct protection_domain *domain)
@@ -2061,8 +2055,8 @@ static bool pci_pri_tlp_required(struct pci_dev *pdev)
2061} 2055}
2062 2056
2063/* 2057/*
2064 * If a device is not yet associated with a domain, this function 2058 * If a device is not yet associated with a domain, this function makes the
2065 * assigns it visible for the hardware 2059 * device visible in the domain
2066 */ 2060 */
2067static int attach_device(struct device *dev, 2061static int attach_device(struct device *dev,
2068 struct protection_domain *domain) 2062 struct protection_domain *domain)
@@ -2124,9 +2118,6 @@ static void __detach_device(struct iommu_dev_data *dev_data)
2124 */ 2118 */
2125 WARN_ON(!irqs_disabled()); 2119 WARN_ON(!irqs_disabled());
2126 2120
2127 if (WARN_ON(!dev_data->domain))
2128 return;
2129
2130 domain = dev_data->domain; 2121 domain = dev_data->domain;
2131 2122
2132 spin_lock(&domain->lock); 2123 spin_lock(&domain->lock);
@@ -2148,6 +2139,15 @@ static void detach_device(struct device *dev)
2148 dev_data = get_dev_data(dev); 2139 dev_data = get_dev_data(dev);
2149 domain = dev_data->domain; 2140 domain = dev_data->domain;
2150 2141
2142 /*
2143 * First check if the device is still attached. It might already
2144 * be detached from its domain because the generic
2145 * iommu_detach_group code detached it and we try again here in
2146 * our alias handling.
2147 */
2148 if (WARN_ON(!dev_data->domain))
2149 return;
2150
2151 /* lock device table */ 2151 /* lock device table */
2152 spin_lock_irqsave(&amd_iommu_devtable_lock, flags); 2152 spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
2153 __detach_device(dev_data); 2153 __detach_device(dev_data);
@@ -2793,6 +2793,7 @@ static void cleanup_domain(struct protection_domain *domain)
2793 while (!list_empty(&domain->dev_list)) { 2793 while (!list_empty(&domain->dev_list)) {
2794 entry = list_first_entry(&domain->dev_list, 2794 entry = list_first_entry(&domain->dev_list,
2795 struct iommu_dev_data, list); 2795 struct iommu_dev_data, list);
2796 BUG_ON(!entry->domain);
2796 __detach_device(entry); 2797 __detach_device(entry);
2797 } 2798 }
2798 2799
diff --git a/drivers/iommu/amd_iommu_types.h b/drivers/iommu/amd_iommu_types.h
index 1c9b080276c9..986cbe0cc189 100644
--- a/drivers/iommu/amd_iommu_types.h
+++ b/drivers/iommu/amd_iommu_types.h
@@ -133,6 +133,7 @@
133#define EVENT_TYPE_CMD_HARD_ERR 0x6 133#define EVENT_TYPE_CMD_HARD_ERR 0x6
134#define EVENT_TYPE_IOTLB_INV_TO 0x7 134#define EVENT_TYPE_IOTLB_INV_TO 0x7
135#define EVENT_TYPE_INV_DEV_REQ 0x8 135#define EVENT_TYPE_INV_DEV_REQ 0x8
136#define EVENT_TYPE_INV_PPR_REQ 0x9
136#define EVENT_DEVID_MASK 0xffff 137#define EVENT_DEVID_MASK 0xffff
137#define EVENT_DEVID_SHIFT 0 138#define EVENT_DEVID_SHIFT 0
138#define EVENT_DOMID_MASK 0xffff 139#define EVENT_DOMID_MASK 0xffff
diff --git a/drivers/iommu/dmar.c b/drivers/iommu/dmar.c
index 460bed4fc5b1..4321f7704b23 100644
--- a/drivers/iommu/dmar.c
+++ b/drivers/iommu/dmar.c
@@ -1618,17 +1618,13 @@ irqreturn_t dmar_fault(int irq, void *dev_id)
1618 int reg, fault_index; 1618 int reg, fault_index;
1619 u32 fault_status; 1619 u32 fault_status;
1620 unsigned long flag; 1620 unsigned long flag;
1621 bool ratelimited;
1622 static DEFINE_RATELIMIT_STATE(rs, 1621 static DEFINE_RATELIMIT_STATE(rs,
1623 DEFAULT_RATELIMIT_INTERVAL, 1622 DEFAULT_RATELIMIT_INTERVAL,
1624 DEFAULT_RATELIMIT_BURST); 1623 DEFAULT_RATELIMIT_BURST);
1625 1624
1626 /* Disable printing, simply clear the fault when ratelimited */
1627 ratelimited = !__ratelimit(&rs);
1628
1629 raw_spin_lock_irqsave(&iommu->register_lock, flag); 1625 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1630 fault_status = readl(iommu->reg + DMAR_FSTS_REG); 1626 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1631 if (fault_status && !ratelimited) 1627 if (fault_status && __ratelimit(&rs))
1632 pr_err("DRHD: handling fault status reg %x\n", fault_status); 1628 pr_err("DRHD: handling fault status reg %x\n", fault_status);
1633 1629
1634 /* TBD: ignore advanced fault log currently */ 1630 /* TBD: ignore advanced fault log currently */
@@ -1638,6 +1634,8 @@ irqreturn_t dmar_fault(int irq, void *dev_id)
1638 fault_index = dma_fsts_fault_record_index(fault_status); 1634 fault_index = dma_fsts_fault_record_index(fault_status);
1639 reg = cap_fault_reg_offset(iommu->cap); 1635 reg = cap_fault_reg_offset(iommu->cap);
1640 while (1) { 1636 while (1) {
1637 /* Disable printing, simply clear the fault when ratelimited */
1638 bool ratelimited = !__ratelimit(&rs);
1641 u8 fault_reason; 1639 u8 fault_reason;
1642 u16 source_id; 1640 u16 source_id;
1643 u64 guest_addr; 1641 u64 guest_addr;
diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index 749d8f235346..d79e3ebbe437 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -485,37 +485,14 @@ static int dmar_forcedac;
485static int intel_iommu_strict; 485static int intel_iommu_strict;
486static int intel_iommu_superpage = 1; 486static int intel_iommu_superpage = 1;
487static int intel_iommu_ecs = 1; 487static int intel_iommu_ecs = 1;
488static int intel_iommu_pasid28;
489static int iommu_identity_mapping; 488static int iommu_identity_mapping;
490 489
491#define IDENTMAP_ALL 1 490#define IDENTMAP_ALL 1
492#define IDENTMAP_GFX 2 491#define IDENTMAP_GFX 2
493#define IDENTMAP_AZALIA 4 492#define IDENTMAP_AZALIA 4
494 493
495/* Broadwell and Skylake have broken ECS support — normal so-called "second 494#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap))
496 * level" translation of DMA requests-without-PASID doesn't actually happen 495#define pasid_enabled(iommu) (ecs_enabled(iommu) && ecap_pasid(iommu->ecap))
497 * unless you also set the NESTE bit in an extended context-entry. Which of
498 * course means that SVM doesn't work because it's trying to do nested
499 * translation of the physical addresses it finds in the process page tables,
500 * through the IOVA->phys mapping found in the "second level" page tables.
501 *
502 * The VT-d specification was retroactively changed to change the definition
503 * of the capability bits and pretend that Broadwell/Skylake never happened...
504 * but unfortunately the wrong bit was changed. It's ECS which is broken, but
505 * for some reason it was the PASID capability bit which was redefined (from
506 * bit 28 on BDW/SKL to bit 40 in future).
507 *
508 * So our test for ECS needs to eschew those implementations which set the old
509 * PASID capabiity bit 28, since those are the ones on which ECS is broken.
510 * Unless we are working around the 'pasid28' limitations, that is, by putting
511 * the device into passthrough mode for normal DMA and thus masking the bug.
512 */
513#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
514 (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))
515/* PASID support is thus enabled if ECS is enabled and *either* of the old
516 * or new capability bits are set. */
517#define pasid_enabled(iommu) (ecs_enabled(iommu) && \
518 (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))
519 496
520int intel_iommu_gfx_mapped; 497int intel_iommu_gfx_mapped;
521EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped); 498EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
@@ -578,11 +555,6 @@ static int __init intel_iommu_setup(char *str)
578 printk(KERN_INFO 555 printk(KERN_INFO
579 "Intel-IOMMU: disable extended context table support\n"); 556 "Intel-IOMMU: disable extended context table support\n");
580 intel_iommu_ecs = 0; 557 intel_iommu_ecs = 0;
581 } else if (!strncmp(str, "pasid28", 7)) {
582 printk(KERN_INFO
583 "Intel-IOMMU: enable pre-production PASID support\n");
584 intel_iommu_pasid28 = 1;
585 iommu_identity_mapping |= IDENTMAP_GFX;
586 } else if (!strncmp(str, "tboot_noforce", 13)) { 558 } else if (!strncmp(str, "tboot_noforce", 13)) {
587 printk(KERN_INFO 559 printk(KERN_INFO
588 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n"); 560 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
@@ -1606,6 +1578,18 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1606 iommu_flush_dev_iotlb(domain, addr, mask); 1578 iommu_flush_dev_iotlb(domain, addr, mask);
1607} 1579}
1608 1580
1581/* Notification for newly created mappings */
1582static inline void __mapping_notify_one(struct intel_iommu *iommu,
1583 struct dmar_domain *domain,
1584 unsigned long pfn, unsigned int pages)
1585{
1586 /* It's a non-present to present mapping. Only flush if caching mode */
1587 if (cap_caching_mode(iommu->cap))
1588 iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
1589 else
1590 iommu_flush_write_buffer(iommu);
1591}
1592
1609static void iommu_flush_iova(struct iova_domain *iovad) 1593static void iommu_flush_iova(struct iova_domain *iovad)
1610{ 1594{
1611 struct dmar_domain *domain; 1595 struct dmar_domain *domain;
@@ -2340,18 +2324,47 @@ static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2340 return 0; 2324 return 0;
2341} 2325}
2342 2326
2327static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2328 struct scatterlist *sg, unsigned long phys_pfn,
2329 unsigned long nr_pages, int prot)
2330{
2331 int ret;
2332 struct intel_iommu *iommu;
2333
2334 /* Do the real mapping first */
2335 ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
2336 if (ret)
2337 return ret;
2338
2339 /* Notify about the new mapping */
2340 if (domain_type_is_vm(domain)) {
2341 /* VM typed domains can have more than one IOMMUs */
2342 int iommu_id;
2343 for_each_domain_iommu(iommu_id, domain) {
2344 iommu = g_iommus[iommu_id];
2345 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2346 }
2347 } else {
2348 /* General domains only have one IOMMU */
2349 iommu = domain_get_iommu(domain);
2350 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2351 }
2352
2353 return 0;
2354}
2355
2343static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn, 2356static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2344 struct scatterlist *sg, unsigned long nr_pages, 2357 struct scatterlist *sg, unsigned long nr_pages,
2345 int prot) 2358 int prot)
2346{ 2359{
2347 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot); 2360 return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2348} 2361}
2349 2362
2350static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn, 2363static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2351 unsigned long phys_pfn, unsigned long nr_pages, 2364 unsigned long phys_pfn, unsigned long nr_pages,
2352 int prot) 2365 int prot)
2353{ 2366{
2354 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot); 2367 return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
2355} 2368}
2356 2369
2357static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn) 2370static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
@@ -2533,7 +2546,7 @@ static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
2533 struct device_domain_info *info = NULL; 2546 struct device_domain_info *info = NULL;
2534 struct dmar_domain *domain = NULL; 2547 struct dmar_domain *domain = NULL;
2535 struct intel_iommu *iommu; 2548 struct intel_iommu *iommu;
2536 u16 req_id, dma_alias; 2549 u16 dma_alias;
2537 unsigned long flags; 2550 unsigned long flags;
2538 u8 bus, devfn; 2551 u8 bus, devfn;
2539 2552
@@ -2541,8 +2554,6 @@ static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
2541 if (!iommu) 2554 if (!iommu)
2542 return NULL; 2555 return NULL;
2543 2556
2544 req_id = ((u16)bus << 8) | devfn;
2545
2546 if (dev_is_pci(dev)) { 2557 if (dev_is_pci(dev)) {
2547 struct pci_dev *pdev = to_pci_dev(dev); 2558 struct pci_dev *pdev = to_pci_dev(dev);
2548 2559
@@ -2656,9 +2667,9 @@ static int iommu_domain_identity_map(struct dmar_domain *domain,
2656 */ 2667 */
2657 dma_pte_clear_range(domain, first_vpfn, last_vpfn); 2668 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
2658 2669
2659 return domain_pfn_mapping(domain, first_vpfn, first_vpfn, 2670 return __domain_mapping(domain, first_vpfn, NULL,
2660 last_vpfn - first_vpfn + 1, 2671 first_vpfn, last_vpfn - first_vpfn + 1,
2661 DMA_PTE_READ|DMA_PTE_WRITE); 2672 DMA_PTE_READ|DMA_PTE_WRITE);
2662} 2673}
2663 2674
2664static int domain_prepare_identity_map(struct device *dev, 2675static int domain_prepare_identity_map(struct device *dev,
@@ -3625,14 +3636,6 @@ static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
3625 if (ret) 3636 if (ret)
3626 goto error; 3637 goto error;
3627 3638
3628 /* it's a non-present to present mapping. Only flush if caching mode */
3629 if (cap_caching_mode(iommu->cap))
3630 iommu_flush_iotlb_psi(iommu, domain,
3631 mm_to_dma_pfn(iova_pfn),
3632 size, 0, 1);
3633 else
3634 iommu_flush_write_buffer(iommu);
3635
3636 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT; 3639 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
3637 start_paddr += paddr & ~PAGE_MASK; 3640 start_paddr += paddr & ~PAGE_MASK;
3638 return start_paddr; 3641 return start_paddr;
@@ -3819,12 +3822,6 @@ static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nele
3819 return 0; 3822 return 0;
3820 } 3823 }
3821 3824
3822 /* it's a non-present to present mapping. Only flush if caching mode */
3823 if (cap_caching_mode(iommu->cap))
3824 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
3825 else
3826 iommu_flush_write_buffer(iommu);
3827
3828 return nelems; 3825 return nelems;
3829} 3826}
3830 3827
diff --git a/drivers/iommu/intel-svm.c b/drivers/iommu/intel-svm.c
index e8cd984cf9c8..45f6e581cd56 100644
--- a/drivers/iommu/intel-svm.c
+++ b/drivers/iommu/intel-svm.c
@@ -319,7 +319,7 @@ int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_
319 } else 319 } else
320 pasid_max = 1 << 20; 320 pasid_max = 1 << 20;
321 321
322 if ((flags & SVM_FLAG_SUPERVISOR_MODE)) { 322 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
323 if (!ecap_srs(iommu->ecap)) 323 if (!ecap_srs(iommu->ecap))
324 return -EINVAL; 324 return -EINVAL;
325 } else if (pasid) { 325 } else if (pasid) {
diff --git a/drivers/iommu/io-pgtable-arm-v7s.c b/drivers/iommu/io-pgtable-arm-v7s.c
index 10e4a3d11c02..50e3a9fcf43e 100644
--- a/drivers/iommu/io-pgtable-arm-v7s.c
+++ b/drivers/iommu/io-pgtable-arm-v7s.c
@@ -898,8 +898,7 @@ static int __init arm_v7s_do_selftests(void)
898 898
899 /* Full unmap */ 899 /* Full unmap */
900 iova = 0; 900 iova = 0;
901 i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG); 901 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
902 while (i != BITS_PER_LONG) {
903 size = 1UL << i; 902 size = 1UL << i;
904 903
905 if (ops->unmap(ops, iova, size) != size) 904 if (ops->unmap(ops, iova, size) != size)
@@ -916,8 +915,6 @@ static int __init arm_v7s_do_selftests(void)
916 return __FAIL(ops); 915 return __FAIL(ops);
917 916
918 iova += SZ_16M; 917 iova += SZ_16M;
919 i++;
920 i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
921 } 918 }
922 919
923 free_io_pgtable_ops(ops); 920 free_io_pgtable_ops(ops);
diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c
index 39c2a056da21..010a254305dd 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -231,12 +231,17 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
231 struct io_pgtable_cfg *cfg) 231 struct io_pgtable_cfg *cfg)
232{ 232{
233 struct device *dev = cfg->iommu_dev; 233 struct device *dev = cfg->iommu_dev;
234 int order = get_order(size);
235 struct page *p;
234 dma_addr_t dma; 236 dma_addr_t dma;
235 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO); 237 void *pages;
236 238
237 if (!pages) 239 VM_BUG_ON((gfp & __GFP_HIGHMEM));
240 p = alloc_pages_node(dev_to_node(dev), gfp | __GFP_ZERO, order);
241 if (!p)
238 return NULL; 242 return NULL;
239 243
244 pages = page_address(p);
240 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { 245 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) {
241 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 246 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
242 if (dma_mapping_error(dev, dma)) 247 if (dma_mapping_error(dev, dma))
@@ -256,7 +261,7 @@ out_unmap:
256 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 261 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
257 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 262 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
258out_free: 263out_free:
259 free_pages_exact(pages, size); 264 __free_pages(p, order);
260 return NULL; 265 return NULL;
261} 266}
262 267
@@ -266,7 +271,7 @@ static void __arm_lpae_free_pages(void *pages, size_t size,
266 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) 271 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
267 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 272 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
268 size, DMA_TO_DEVICE); 273 size, DMA_TO_DEVICE);
269 free_pages_exact(pages, size); 274 free_pages((unsigned long)pages, get_order(size));
270} 275}
271 276
272static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 277static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
@@ -1120,8 +1125,7 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1120 1125
1121 /* Full unmap */ 1126 /* Full unmap */
1122 iova = 0; 1127 iova = 0;
1123 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); 1128 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1124 while (j != BITS_PER_LONG) {
1125 size = 1UL << j; 1129 size = 1UL << j;
1126 1130
1127 if (ops->unmap(ops, iova, size) != size) 1131 if (ops->unmap(ops, iova, size) != size)
@@ -1138,8 +1142,6 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1138 return __FAIL(ops, i); 1142 return __FAIL(ops, i);
1139 1143
1140 iova += SZ_1G; 1144 iova += SZ_1G;
1141 j++;
1142 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1143 } 1145 }
1144 1146
1145 free_io_pgtable_ops(ops); 1147 free_io_pgtable_ops(ops);
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index d2aa23202bb9..63b37563db7e 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -116,9 +116,11 @@ static void __iommu_detach_group(struct iommu_domain *domain,
116static int __init iommu_set_def_domain_type(char *str) 116static int __init iommu_set_def_domain_type(char *str)
117{ 117{
118 bool pt; 118 bool pt;
119 int ret;
119 120
120 if (!str || strtobool(str, &pt)) 121 ret = kstrtobool(str, &pt);
121 return -EINVAL; 122 if (ret)
123 return ret;
122 124
123 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA; 125 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
124 return 0; 126 return 0;
@@ -322,7 +324,6 @@ static struct kobj_type iommu_group_ktype = {
322 324
323/** 325/**
324 * iommu_group_alloc - Allocate a new group 326 * iommu_group_alloc - Allocate a new group
325 * @name: Optional name to associate with group, visible in sysfs
326 * 327 *
327 * This function is called by an iommu driver to allocate a new iommu 328 * This function is called by an iommu driver to allocate a new iommu
328 * group. The iommu group represents the minimum granularity of the iommu. 329 * group. The iommu group represents the minimum granularity of the iommu.
diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c
index 65b9c99707f8..fe88a4880d3a 100644
--- a/drivers/iommu/qcom_iommu.c
+++ b/drivers/iommu/qcom_iommu.c
@@ -885,16 +885,14 @@ static int qcom_iommu_device_remove(struct platform_device *pdev)
885 885
886static int __maybe_unused qcom_iommu_resume(struct device *dev) 886static int __maybe_unused qcom_iommu_resume(struct device *dev)
887{ 887{
888 struct platform_device *pdev = to_platform_device(dev); 888 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
889 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
890 889
891 return qcom_iommu_enable_clocks(qcom_iommu); 890 return qcom_iommu_enable_clocks(qcom_iommu);
892} 891}
893 892
894static int __maybe_unused qcom_iommu_suspend(struct device *dev) 893static int __maybe_unused qcom_iommu_suspend(struct device *dev)
895{ 894{
896 struct platform_device *pdev = to_platform_device(dev); 895 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
897 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
898 896
899 qcom_iommu_disable_clocks(qcom_iommu); 897 qcom_iommu_disable_clocks(qcom_iommu);
900 898
diff --git a/drivers/iommu/tegra-gart.c b/drivers/iommu/tegra-gart.c
index b62f790ad1ba..89ec24c6952c 100644
--- a/drivers/iommu/tegra-gart.c
+++ b/drivers/iommu/tegra-gart.c
@@ -72,6 +72,8 @@ struct gart_domain {
72 72
73static struct gart_device *gart_handle; /* unique for a system */ 73static struct gart_device *gart_handle; /* unique for a system */
74 74
75static bool gart_debug;
76
75#define GART_PTE(_pfn) \ 77#define GART_PTE(_pfn) \
76 (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT)) 78 (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
77 79
@@ -271,6 +273,7 @@ static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
271 struct gart_device *gart = gart_domain->gart; 273 struct gart_device *gart = gart_domain->gart;
272 unsigned long flags; 274 unsigned long flags;
273 unsigned long pfn; 275 unsigned long pfn;
276 unsigned long pte;
274 277
275 if (!gart_iova_range_valid(gart, iova, bytes)) 278 if (!gart_iova_range_valid(gart, iova, bytes))
276 return -EINVAL; 279 return -EINVAL;
@@ -282,6 +285,14 @@ static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
282 spin_unlock_irqrestore(&gart->pte_lock, flags); 285 spin_unlock_irqrestore(&gart->pte_lock, flags);
283 return -EINVAL; 286 return -EINVAL;
284 } 287 }
288 if (gart_debug) {
289 pte = gart_read_pte(gart, iova);
290 if (pte & GART_ENTRY_PHYS_ADDR_VALID) {
291 spin_unlock_irqrestore(&gart->pte_lock, flags);
292 dev_err(gart->dev, "Page entry is in-use\n");
293 return -EBUSY;
294 }
295 }
285 gart_set_pte(gart, iova, GART_PTE(pfn)); 296 gart_set_pte(gart, iova, GART_PTE(pfn));
286 FLUSH_GART_REGS(gart); 297 FLUSH_GART_REGS(gart);
287 spin_unlock_irqrestore(&gart->pte_lock, flags); 298 spin_unlock_irqrestore(&gart->pte_lock, flags);
@@ -302,7 +313,7 @@ static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
302 gart_set_pte(gart, iova, 0); 313 gart_set_pte(gart, iova, 0);
303 FLUSH_GART_REGS(gart); 314 FLUSH_GART_REGS(gart);
304 spin_unlock_irqrestore(&gart->pte_lock, flags); 315 spin_unlock_irqrestore(&gart->pte_lock, flags);
305 return 0; 316 return bytes;
306} 317}
307 318
308static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain, 319static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
@@ -515,7 +526,9 @@ static void __exit tegra_gart_exit(void)
515 526
516subsys_initcall(tegra_gart_init); 527subsys_initcall(tegra_gart_init);
517module_exit(tegra_gart_exit); 528module_exit(tegra_gart_exit);
529module_param(gart_debug, bool, 0644);
518 530
531MODULE_PARM_DESC(gart_debug, "Enable GART debugging");
519MODULE_DESCRIPTION("IOMMU API for GART in Tegra20"); 532MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
520MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>"); 533MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
521MODULE_ALIAS("platform:tegra-gart"); 534MODULE_ALIAS("platform:tegra-gart");
diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h
index ef169d67df92..1df940196ab2 100644
--- a/include/linux/intel-iommu.h
+++ b/include/linux/intel-iommu.h
@@ -121,7 +121,6 @@
121#define ecap_srs(e) ((e >> 31) & 0x1) 121#define ecap_srs(e) ((e >> 31) & 0x1)
122#define ecap_ers(e) ((e >> 30) & 0x1) 122#define ecap_ers(e) ((e >> 30) & 0x1)
123#define ecap_prs(e) ((e >> 29) & 0x1) 123#define ecap_prs(e) ((e >> 29) & 0x1)
124#define ecap_broken_pasid(e) ((e >> 28) & 0x1)
125#define ecap_dis(e) ((e >> 27) & 0x1) 124#define ecap_dis(e) ((e >> 27) & 0x1)
126#define ecap_nest(e) ((e >> 26) & 0x1) 125#define ecap_nest(e) ((e >> 26) & 0x1)
127#define ecap_mts(e) ((e >> 25) & 0x1) 126#define ecap_mts(e) ((e >> 25) & 0x1)