--- /dev/null
+From foo@baz Tue Feb 2 02:02:22 PM CET 2021
+From: Filippo Sironi <sironi@amazon.de>
+Date: Tue, 2 Feb 2021 01:04:45 +0100
+Subject: iommu/vt-d: Don't dereference iommu_device if IOMMU_API is not built
+To: <gregkh@linuxfoundation.org>
+Cc: <stable@vger.kernel.org>, <samjonas@amazon.com>, <dwmw@amazon.co.uk>, <sironi@amazon.de>, Bartosz Golaszewski <bgolaszewski@baylibre.com>, Joerg Roedel <jroedel@suse.de>
+Message-ID: <20210202000445.711-2-sironi@amazon.de>
+
+From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
+
+commit 9def3b1a07c41e21c68a0eb353e3e569fdd1d2b1 upstream.
+
+Since commit c40aaaac1018 ("iommu/vt-d: Gracefully handle DMAR units
+with no supported address widths") dmar.c needs struct iommu_device to
+be selected. We can drop this dependency by not dereferencing struct
+iommu_device if IOMMU_API is not selected and by reusing the information
+stored in iommu->drhd->ignored instead.
+
+This fixes the following build error when IOMMU_API is not selected:
+
+drivers/iommu/dmar.c: In function ‘free_iommu’:
+drivers/iommu/dmar.c:1139:41: error: ‘struct iommu_device’ has no member named ‘ops’
+ 1139 | if (intel_iommu_enabled && iommu->iommu.ops) {
+ ^
+
+Fixes: c40aaaac1018 ("iommu/vt-d: Gracefully handle DMAR units with no supported address widths")
+Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
+Acked-by: Lu Baolu <baolu.lu@linux.intel.com>
+Acked-by: David Woodhouse <dwmw@amazon.co.uk>
+Link: https://lore.kernel.org/r/20201013073055.11262-1-brgl@bgdev.pl
+Signed-off-by: Joerg Roedel <jroedel@suse.de>
+[ - context change due to moving drivers/iommu/dmar.c to
+ drivers/iommu/intel/dmar.c
+ - set the drhr in the iommu like in upstream commit b1012ca8dc4f
+ ("iommu/vt-d: Skip TE disabling on quirky gfx dedicated iommu") ]
+Signed-off-by: Filippo Sironi <sironi@amazon.de>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/iommu/dmar.c | 3 ++-
+ include/linux/intel-iommu.h | 2 ++
+ 2 files changed, 4 insertions(+), 1 deletion(-)
+
+--- a/drivers/iommu/dmar.c
++++ b/drivers/iommu/dmar.c
+@@ -1120,6 +1120,7 @@ static int alloc_iommu(struct dmar_drhd_
+ }
+
+ drhd->iommu = iommu;
++ iommu->drhd = drhd;
+
+ return 0;
+
+@@ -1134,7 +1135,7 @@ error:
+
+ static void free_iommu(struct intel_iommu *iommu)
+ {
+- if (intel_iommu_enabled && iommu->iommu.ops) {
++ if (intel_iommu_enabled && !iommu->drhd->ignored) {
+ iommu_device_unregister(&iommu->iommu);
+ iommu_device_sysfs_remove(&iommu->iommu);
+ }
+--- a/include/linux/intel-iommu.h
++++ b/include/linux/intel-iommu.h
+@@ -434,6 +434,8 @@ struct intel_iommu {
+ struct iommu_device iommu; /* IOMMU core code handle */
+ int node;
+ u32 flags; /* Software defined flags */
++
++ struct dmar_drhd_unit *drhd;
+ };
+
+ static inline void __iommu_flush_cache(
--- /dev/null
+From foo@baz Tue Feb 2 02:02:22 PM CET 2021
+From: Filippo Sironi <sironi@amazon.de>
+Date: Tue, 2 Feb 2021 01:04:44 +0100
+Subject: iommu/vt-d: Gracefully handle DMAR units with no supported address widths
+To: <gregkh@linuxfoundation.org>
+Cc: <stable@vger.kernel.org>, <samjonas@amazon.com>, <dwmw@amazon.co.uk>, <sironi@amazon.de>, Joerg Roedel <jroedel@suse.de>
+Message-ID: <20210202000445.711-1-sironi@amazon.de>
+
+From: David Woodhouse <dwmw@amazon.co.uk>
+
+commit c40aaaac1018ff1382f2d35df5129a6bcea3df6b upstream.
+
+Instead of bailing out completely, such a unit can still be used for
+interrupt remapping.
+
+Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
+Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
+Link: https://lore.kernel.org/linux-iommu/549928db2de6532117f36c9c810373c14cf76f51.camel@infradead.org/
+Signed-off-by: Joerg Roedel <jroedel@suse.de>
+[ context change due to moving drivers/iommu/dmar.c to
+ drivers/iommu/intel/dmar.c ]
+Signed-off-by: Filippo Sironi <sironi@amazon.de>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/iommu/dmar.c | 46 +++++++++++++++++++++++++++++++---------------
+ 1 file changed, 31 insertions(+), 15 deletions(-)
+
+--- a/drivers/iommu/dmar.c
++++ b/drivers/iommu/dmar.c
+@@ -1026,8 +1026,8 @@ static int alloc_iommu(struct dmar_drhd_
+ {
+ struct intel_iommu *iommu;
+ u32 ver, sts;
+- int agaw = 0;
+- int msagaw = 0;
++ int agaw = -1;
++ int msagaw = -1;
+ int err;
+
+ if (!drhd->reg_base_addr) {
+@@ -1052,17 +1052,28 @@ static int alloc_iommu(struct dmar_drhd_
+ }
+
+ err = -EINVAL;
+- agaw = iommu_calculate_agaw(iommu);
+- if (agaw < 0) {
+- pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
+- iommu->seq_id);
+- goto err_unmap;
+- }
+- msagaw = iommu_calculate_max_sagaw(iommu);
+- if (msagaw < 0) {
+- pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
+- iommu->seq_id);
+- goto err_unmap;
++ if (cap_sagaw(iommu->cap) == 0) {
++ pr_info("%s: No supported address widths. Not attempting DMA translation.\n",
++ iommu->name);
++ drhd->ignored = 1;
++ }
++
++ if (!drhd->ignored) {
++ agaw = iommu_calculate_agaw(iommu);
++ if (agaw < 0) {
++ pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
++ iommu->seq_id);
++ drhd->ignored = 1;
++ }
++ }
++ if (!drhd->ignored) {
++ msagaw = iommu_calculate_max_sagaw(iommu);
++ if (msagaw < 0) {
++ pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
++ iommu->seq_id);
++ drhd->ignored = 1;
++ agaw = -1;
++ }
+ }
+ iommu->agaw = agaw;
+ iommu->msagaw = msagaw;
+@@ -1089,7 +1100,12 @@ static int alloc_iommu(struct dmar_drhd_
+
+ raw_spin_lock_init(&iommu->register_lock);
+
+- if (intel_iommu_enabled) {
++ /*
++ * This is only for hotplug; at boot time intel_iommu_enabled won't
++ * be set yet. When intel_iommu_init() runs, it registers the units
++ * present at boot time, then sets intel_iommu_enabled.
++ */
++ if (intel_iommu_enabled && !drhd->ignored) {
+ err = iommu_device_sysfs_add(&iommu->iommu, NULL,
+ intel_iommu_groups,
+ "%s", iommu->name);
+@@ -1118,7 +1134,7 @@ error:
+
+ static void free_iommu(struct intel_iommu *iommu)
+ {
+- if (intel_iommu_enabled) {
++ if (intel_iommu_enabled && iommu->iommu.ops) {
+ iommu_device_unregister(&iommu->iommu);
+ iommu_device_sysfs_remove(&iommu->iommu);
+ }