From 4ce44532dd932bfd830c23ac635293238aed3e3c Mon Sep 17 00:00:00 2001
From: Weidong Han <weidong.han@intel.com>
Date: Tue, 2 Dec 2008 14:07:20 +0800
Subject: [PATCH] set iommu agaw

iommu agaw may be different across iommus.

Signed-off-by: Weidong Han <weidong.han@intel.com>
---
 drivers/pci/dmar.c            |   14 ++++++++++++++
 include/linux/dma_remapping.h |    2 ++
 include/linux/intel-iommu.h   |    1 +
 3 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/drivers/pci/dmar.c b/drivers/pci/dmar.c
index 691b3ad..ebcc7c2 100644
--- a/drivers/pci/dmar.c
+++ b/drivers/pci/dmar.c
@@ -491,6 +491,8 @@ int alloc_iommu(struct dmar_drhd_unit *drhd)
 	int map_size;
 	u32 ver;
 	static int iommu_allocated = 0;
+	unsigned long sagaw;
+	int agaw;
 
 	iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
 	if (!iommu)
@@ -506,6 +508,18 @@ int alloc_iommu(struct dmar_drhd_unit *drhd)
 	iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
 	iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
 
+	/* set agaw, "SAGAW" may be different across iommus */
+	sagaw = cap_sagaw(iommu->cap);
+	for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
+	     agaw >= 0; agaw--)
+		if (test_bit(agaw, &sagaw))
+			break;
+	if (agaw < 0) {
+		printk(KERN_ERR "IOMMU: unsupported sagaw %lx\n", sagaw);
+		goto error;
+	}
+	iommu->agaw = agaw;
+
 	/* the registers might be more than one page */
 	map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
 		cap_max_fault_reg_offset(iommu->cap));
diff --git a/include/linux/dma_remapping.h b/include/linux/dma_remapping.h
index eeb8243..c9d99c9 100644
--- a/include/linux/dma_remapping.h
+++ b/include/linux/dma_remapping.h
@@ -1,6 +1,8 @@
 #ifndef _DMA_REMAPPING_H
 #define _DMA_REMAPPING_H
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 /*
  * VT-d hardware uses 4KiB page size regardless of host page size.
  */
diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h
index 3d017cf..24a2945 100644
--- a/include/linux/intel-iommu.h
+++ b/include/linux/intel-iommu.h
@@ -299,6 +299,7 @@ struct intel_iommu {
 	struct dmar_domain **domains; /* ptr to domains */
 	spinlock_t	lock; /* protect context, domain ids */
 	struct root_entry *root_entry; /* virtual address */
+	int		agaw; /* agaw of this iommu */
 
 	unsigned int irq;
 	unsigned char name[7];    /* Device Name */
-- 
1.5.1

