public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Joerg Roedel <joerg.roedel@amd.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org,
	Joerg Roedel <joerg.roedel@amd.com>
Subject: [PATCH 27/34] x86/amd-iommu: Use dev->arch->iommu to store iommu related information
Date: Fri, 27 Nov 2009 14:55:38 +0100	[thread overview]
Message-ID: <1259330145-14865-28-git-send-email-joerg.roedel@amd.com> (raw)
In-Reply-To: <1259330145-14865-1-git-send-email-joerg.roedel@amd.com>

This patch changes IOMMU code to use dev->archdata->iommu to
store information about the alias device and the domain the
device is attached to.
This allows the driver to get rid of the amd_iommu_pd_table
in the future.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
---
 arch/x86/include/asm/amd_iommu_types.h |    8 +++
 arch/x86/include/asm/device.h          |    2 +-
 arch/x86/kernel/amd_iommu.c            |  109 +++++++++++++++++++++++++-------
 3 files changed, 95 insertions(+), 24 deletions(-)

diff --git a/arch/x86/include/asm/amd_iommu_types.h b/arch/x86/include/asm/amd_iommu_types.h
index 02b6a0f..9eaa27b 100644
--- a/arch/x86/include/asm/amd_iommu_types.h
+++ b/arch/x86/include/asm/amd_iommu_types.h
@@ -248,6 +248,14 @@ struct protection_domain {
 };
 
 /*
+ * This struct contains device specific data for the IOMMU
+ */
+struct iommu_dev_data {
+	struct device *alias;		  /* The Alias Device */
+	struct protection_domain *domain; /* Domain the device is bound to */
+};
+
+/*
  * For dynamic growth the aperture size is split into ranges of 128MB of
  * DMA address space each. This struct represents one such range.
  */
diff --git a/arch/x86/include/asm/device.h b/arch/x86/include/asm/device.h
index cee34e9..029f230 100644
--- a/arch/x86/include/asm/device.h
+++ b/arch/x86/include/asm/device.h
@@ -8,7 +8,7 @@ struct dev_archdata {
 #ifdef CONFIG_X86_64
 struct dma_map_ops *dma_ops;
 #endif
-#ifdef CONFIG_DMAR
+#if defined(CONFIG_DMAR) || defined(CONFIG_AMD_IOMMU)
 	void *iommu; /* hook for IOMMU specific extension */
 #endif
 };
diff --git a/arch/x86/kernel/amd_iommu.c b/arch/x86/kernel/amd_iommu.c
index ed58a16..3214e88 100644
--- a/arch/x86/kernel/amd_iommu.c
+++ b/arch/x86/kernel/amd_iommu.c
@@ -73,6 +73,11 @@ static inline u16 get_device_id(struct device *dev)
 	return calc_devid(pdev->bus->number, pdev->devfn);
 }
 
+static struct iommu_dev_data *get_dev_data(struct device *dev)
+{
+	return dev->archdata.iommu;
+}
+
 /*
  * In this function the list of preallocated protection domains is traversed to
  * find the domain for a specific device
@@ -128,6 +133,35 @@ static bool check_device(struct device *dev)
 	return true;
 }
 
+static int iommu_init_device(struct device *dev)
+{
+	struct iommu_dev_data *dev_data;
+	struct pci_dev *pdev;
+	u16 devid, alias;
+
+	if (dev->archdata.iommu)
+		return 0;
+
+	dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
+	if (!dev_data)
+		return -ENOMEM;
+
+	devid = get_device_id(dev);
+	alias = amd_iommu_alias_table[devid];
+	pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
+	if (pdev)
+		dev_data->alias = &pdev->dev;
+
+	dev->archdata.iommu = dev_data;
+
+
+	return 0;
+}
+
+static void iommu_uninit_device(struct device *dev)
+{
+	kfree(dev->archdata.iommu);
+}
 #ifdef CONFIG_AMD_IOMMU_STATS
 
 /*
@@ -1346,28 +1380,39 @@ static void clear_dte_entry(u16 devid)
 static int __attach_device(struct device *dev,
 			   struct protection_domain *domain)
 {
-	u16 devid = get_device_id(dev);
-	u16 alias = amd_iommu_alias_table[devid];
+	struct iommu_dev_data *dev_data, *alias_data;
+	u16 devid, alias;
+
+	devid      = get_device_id(dev);
+	alias      = amd_iommu_alias_table[devid];
+	dev_data   = get_dev_data(dev);
+	alias_data = get_dev_data(dev_data->alias);
+	if (!alias_data)
+		return -EINVAL;
 
 	/* lock domain */
 	spin_lock(&domain->lock);
 
 	/* Some sanity checks */
-	if (amd_iommu_pd_table[alias] != NULL &&
-	    amd_iommu_pd_table[alias] != domain)
+	if (alias_data->domain != NULL &&
+	    alias_data->domain != domain)
 		return -EBUSY;
 
-	if (amd_iommu_pd_table[devid] != NULL &&
-	    amd_iommu_pd_table[devid] != domain)
+	if (dev_data->domain != NULL &&
+	    dev_data->domain != domain)
 		return -EBUSY;
 
 	/* Do real assignment */
 	if (alias != devid &&
-	    amd_iommu_pd_table[alias] == NULL)
+	    alias_data->domain == NULL) {
+		alias_data->domain = domain;
 		set_dte_entry(alias, domain);
+	}
 
-	if (amd_iommu_pd_table[devid] == NULL)
+	if (dev_data->domain == NULL) {
+		dev_data->domain = domain;
 		set_dte_entry(devid, domain);
+	}
 
 	/* ready */
 	spin_unlock(&domain->lock);
@@ -1406,10 +1451,12 @@ static void __detach_device(struct device *dev)
 {
 	u16 devid = get_device_id(dev);
 	struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
+	struct iommu_dev_data *dev_data = get_dev_data(dev);
 
 	BUG_ON(!iommu);
 
 	clear_dte_entry(devid);
+	dev_data->domain = NULL;
 
 	/*
 	 * If we run in passthrough mode the device must be assigned to the
@@ -1439,18 +1486,23 @@ static void detach_device(struct device *dev)
 static struct protection_domain *domain_for_device(struct device *dev)
 {
 	struct protection_domain *dom;
+	struct iommu_dev_data *dev_data, *alias_data;
 	unsigned long flags;
 	u16 devid, alias;
 
-	devid = get_device_id(dev);
-	alias = amd_iommu_alias_table[devid];
+	devid      = get_device_id(dev);
+	alias      = amd_iommu_alias_table[devid];
+	dev_data   = get_dev_data(dev);
+	alias_data = get_dev_data(dev_data->alias);
+	if (!alias_data)
+		return NULL;
 
 	read_lock_irqsave(&amd_iommu_devtable_lock, flags);
-	dom = amd_iommu_pd_table[devid];
+	dom = dev_data->domain;
 	if (dom == NULL &&
-	    amd_iommu_pd_table[alias] != NULL) {
-		__attach_device(dev, amd_iommu_pd_table[alias]);
-		dom = amd_iommu_pd_table[devid];
+	    alias_data->domain != NULL) {
+		__attach_device(dev, alias_data->domain);
+		dom = alias_data->domain;
 	}
 
 	read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
@@ -1473,14 +1525,12 @@ static int device_change_notifier(struct notifier_block *nb,
 
 	devid  = get_device_id(dev);
 	iommu  = amd_iommu_rlookup_table[devid];
-	domain = domain_for_device(dev);
-
-	if (domain && !dma_ops_domain(domain))
-		WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
-			  "to a non-dma-ops domain\n", dev_name(dev));
 
 	switch (action) {
 	case BUS_NOTIFY_UNBOUND_DRIVER:
+
+		domain = domain_for_device(dev);
+
 		if (!domain)
 			goto out;
 		if (iommu_pass_through)
@@ -1488,6 +1538,11 @@ static int device_change_notifier(struct notifier_block *nb,
 		detach_device(dev);
 		break;
 	case BUS_NOTIFY_ADD_DEVICE:
+
+		iommu_init_device(dev);
+
+		domain = domain_for_device(dev);
+
 		/* allocate a protection domain if a device is added */
 		dma_domain = find_protection_domain(devid);
 		if (dma_domain)
@@ -1502,6 +1557,10 @@ static int device_change_notifier(struct notifier_block *nb,
 		spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
 
 		break;
+	case BUS_NOTIFY_DEL_DEVICE:
+
+		iommu_uninit_device(dev);
+
 	default:
 		goto out;
 	}
@@ -2079,6 +2138,8 @@ static void prealloc_protection_domains(void)
 		if (!check_device(&dev->dev))
 			continue;
 
+		iommu_init_device(&dev->dev);
+
 		/* Is there already any domain for it? */
 		if (domain_for_device(&dev->dev))
 			continue;
@@ -2270,6 +2331,7 @@ static void amd_iommu_domain_destroy(struct iommu_domain *dom)
 static void amd_iommu_detach_device(struct iommu_domain *dom,
 				    struct device *dev)
 {
+	struct iommu_dev_data *dev_data = dev->archdata.iommu;
 	struct amd_iommu *iommu;
 	u16 devid;
 
@@ -2278,7 +2340,7 @@ static void amd_iommu_detach_device(struct iommu_domain *dom,
 
 	devid = get_device_id(dev);
 
-	if (amd_iommu_pd_table[devid] != NULL)
+	if (dev_data->domain != NULL)
 		detach_device(dev);
 
 	iommu = amd_iommu_rlookup_table[devid];
@@ -2293,7 +2355,7 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
 				   struct device *dev)
 {
 	struct protection_domain *domain = dom->priv;
-	struct protection_domain *old_domain;
+	struct iommu_dev_data *dev_data;
 	struct amd_iommu *iommu;
 	int ret;
 	u16 devid;
@@ -2301,14 +2363,15 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
 	if (!check_device(dev))
 		return -EINVAL;
 
+	dev_data = dev->archdata.iommu;
+
 	devid = get_device_id(dev);
 
 	iommu = amd_iommu_rlookup_table[devid];
 	if (!iommu)
 		return -EINVAL;
 
-	old_domain = amd_iommu_pd_table[devid];
-	if (old_domain)
+	if (dev_data->domain)
 		detach_device(dev);
 
 	ret = attach_device(dev, domain);
-- 
1.6.5.3



  parent reply	other threads:[~2009-11-27 13:56 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-27 13:55 [git pull] IOMMU updates for 2.6.33 Joerg Roedel
2009-11-27 13:55 ` [PATCH 01/34] x86/amd-iommu: Separate internal interface definitions Joerg Roedel
2009-11-27 13:55 ` [PATCH 02/34] x86/amd-iommu: Update copyright headers Joerg Roedel
2009-11-27 13:55 ` [PATCH 03/34] x86/amd-iommu: Add an index field to struct amd_iommu Joerg Roedel
2009-11-27 13:55 ` [PATCH 04/34] x86/amd-iommu: Add per IOMMU reference counting Joerg Roedel
2009-11-27 13:55 ` [PATCH 05/34] x86/amd-iommu: Add function to complete a tlb flush Joerg Roedel
2009-11-27 13:55 ` [PATCH 06/34] x86/amd-iommu: Make iommu_flush_pages aware of multiple IOMMUs Joerg Roedel
2009-11-27 13:55 ` [PATCH 07/34] x86/amd-iommu: Use __iommu_flush_pages for tlb flushes Joerg Roedel
2009-11-27 13:55 ` [PATCH 08/34] x86/amd-iommu: Remove iommu_flush_domain function Joerg Roedel
2009-11-27 13:55 ` [PATCH 09/34] x86/amd-iommu: Implement protection domain list Joerg Roedel
2009-11-27 13:55 ` [PATCH 10/34] x86/amd-iommu: Reimplement amd_iommu_flush_all_domains() Joerg Roedel
2009-11-27 13:55 ` [PATCH 11/34] x86/amd-iommu: Reimplement flush_all_domains_on_iommu() Joerg Roedel
2009-11-27 13:55 ` [PATCH 12/34] x86/amd-iommu: Make np-cache a global flag Joerg Roedel
2009-11-27 13:55 ` [PATCH 13/34] x86/amd-iommu: Use check_device for amd_iommu_dma_supported Joerg Roedel
2009-11-27 13:55 ` [PATCH 14/34] x86/amd-iommu: Use check_device in get_device_resources Joerg Roedel
2009-11-27 13:55 ` [PATCH 15/34] x86/amd-iommu: Remove iommu parameter from dma_ops_domain_(un)map Joerg Roedel
2009-11-27 13:55 ` [PATCH 16/34] x86/amd-iommu: Make alloc_new_range aware of multiple IOMMUs Joerg Roedel
2009-11-27 13:55 ` [PATCH 17/34] x86/amd-iommu: Remove iommu parameter from __(un)map_single Joerg Roedel
2009-11-27 13:55 ` [PATCH 18/34] x86/amd-iommu: Remove iommu specific handling from dma_ops path Joerg Roedel
2009-11-27 13:55 ` [PATCH 19/34] x86/amd-iommu: Let domain_for_device handle aliases Joerg Roedel
2009-11-27 13:55 ` [PATCH 20/34] x86/amd-iommu: Simplify get_device_resources() Joerg Roedel
2009-11-27 13:55 ` [PATCH 21/34] x86/amd-iommu: Move find_protection_domain to helper functions Joerg Roedel
2009-11-27 13:55 ` [PATCH 22/34] x86/amd-iommu: Use get_device_id and check_device where appropriate Joerg Roedel
2009-11-27 13:55 ` [PATCH 23/34] x86/amd-iommu: Remove iommu parameter from dma_ops_domain_alloc Joerg Roedel
2009-11-27 13:55 ` [PATCH 24/34] x86/amd-iommu: Move some pte allocation functions in the right section Joerg Roedel
2009-11-27 13:55 ` [PATCH 25/34] x86/amd-iommu: Rearrange dma_ops related functions Joerg Roedel
2009-11-27 13:55 ` [PATCH 26/34] x86/amd-iommu: Remove support for domain sharing Joerg Roedel
2009-11-27 13:55 ` Joerg Roedel [this message]
2009-11-27 13:55 ` [PATCH 28/34] x86/amd-iommu: Add device bind reference counting Joerg Roedel
2009-11-27 13:55 ` [PATCH 29/34] x86/amd-iommu: Keep devices per domain in a list Joerg Roedel
2009-11-27 13:55 ` [PATCH 30/34] x86/amd-iommu: Cleanup attach/detach_device code Joerg Roedel
2009-11-27 13:55 ` [PATCH 31/34] x86/amd-iommu: Introduce iommu_flush_device() function Joerg Roedel
2009-11-27 13:55 ` [PATCH 32/34] x86/amd-iommu: Cleanup DTE flushing code Joerg Roedel
2009-11-27 13:55 ` [PATCH 33/34] x86/amd-iommu: Move reset_iommu_command_buffer out of locked code Joerg Roedel
2009-11-27 13:55 ` [PATCH 34/34] x86/amd-iommu: Remove amd_iommu_pd_table Joerg Roedel
2009-11-27 19:27 ` [git pull] IOMMU updates for 2.6.33 Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1259330145-14865-28-git-send-email-joerg.roedel@amd.com \
    --to=joerg.roedel@amd.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox