linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lu Baolu <baolu.lu@linux.intel.com>
To: David Woodhouse <dwmw2@infradead.org>, Joerg Roedel <joro@8bytes.org>
Cc: ashok.raj@intel.com, jacob.jun.pan@intel.com,
	kevin.tian@intel.com, jamessewart@arista.com, tmurphy@arista.com,
	dima@arista.com, sai.praneeth.prakhya@intel.com,
	iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org,
	Lu Baolu <baolu.lu@linux.intel.com>
Subject: [PATCH v4 01/15] iommu: Add API to request DMA domain for device
Date: Sat, 25 May 2019 13:41:22 +0800	[thread overview]
Message-ID: <20190525054136.27810-2-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <20190525054136.27810-1-baolu.lu@linux.intel.com>

Normally during iommu probing a device, a default doamin will
be allocated and attached to the device. The domain type of
the default domain is statically defined, which results in a
situation where the allocated default domain isn't suitable
for the device due to some limitations. We already have API
iommu_request_dm_for_dev() to replace a DMA domain with an
identity one. This adds iommu_request_dma_domain_for_dev()
to request a dma domain if an allocated identity domain isn't
suitable for the device in question.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
 drivers/iommu/iommu.c | 36 +++++++++++++++++++++++++-----------
 include/linux/iommu.h |  6 ++++++
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index c09d60401778..03ba8406d1e4 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1910,10 +1910,10 @@ struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
 	return region;
 }
 
-/* Request that a device is direct mapped by the IOMMU */
-int iommu_request_dm_for_dev(struct device *dev)
+static int
+request_default_domain_for_dev(struct device *dev, unsigned long type)
 {
-	struct iommu_domain *dm_domain;
+	struct iommu_domain *domain;
 	struct iommu_group *group;
 	int ret;
 
@@ -1926,8 +1926,7 @@ int iommu_request_dm_for_dev(struct device *dev)
 
 	/* Check if the default domain is already direct mapped */
 	ret = 0;
-	if (group->default_domain &&
-	    group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
+	if (group->default_domain && group->default_domain->type == type)
 		goto out;
 
 	/* Don't change mappings of existing devices */
@@ -1937,23 +1936,26 @@ int iommu_request_dm_for_dev(struct device *dev)
 
 	/* Allocate a direct mapped domain */
 	ret = -ENOMEM;
-	dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
-	if (!dm_domain)
+	domain = __iommu_domain_alloc(dev->bus, type);
+	if (!domain)
 		goto out;
 
 	/* Attach the device to the domain */
-	ret = __iommu_attach_group(dm_domain, group);
+	ret = __iommu_attach_group(domain, group);
 	if (ret) {
-		iommu_domain_free(dm_domain);
+		iommu_domain_free(domain);
 		goto out;
 	}
 
+	iommu_group_create_direct_mappings(group, dev);
+
 	/* Make the direct mapped domain the default for this group */
 	if (group->default_domain)
 		iommu_domain_free(group->default_domain);
-	group->default_domain = dm_domain;
+	group->default_domain = domain;
 
-	dev_info(dev, "Using iommu direct mapping\n");
+	dev_info(dev, "Using iommu %s mapping\n",
+		 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
 
 	ret = 0;
 out:
@@ -1963,6 +1965,18 @@ int iommu_request_dm_for_dev(struct device *dev)
 	return ret;
 }
 
+/* Request that a device is direct mapped by the IOMMU */
+int iommu_request_dm_for_dev(struct device *dev)
+{
+	return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
+}
+
+/* Request that a device can't be direct mapped by the IOMMU */
+int iommu_request_dma_domain_for_dev(struct device *dev)
+{
+	return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
+}
+
 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
 {
 	const struct iommu_ops *ops = NULL;
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 14a521f85f14..593a3411d860 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -368,6 +368,7 @@ extern void iommu_set_fault_handler(struct iommu_domain *domain,
 extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
 extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
 extern int iommu_request_dm_for_dev(struct device *dev);
+extern int iommu_request_dma_domain_for_dev(struct device *dev);
 extern struct iommu_resv_region *
 iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot,
 			enum iommu_resv_type type, gfp_t flags);
@@ -632,6 +633,11 @@ static inline int iommu_request_dm_for_dev(struct device *dev)
 	return -ENODEV;
 }
 
+static inline int iommu_request_dma_domain_for_dev(struct device *dev)
+{
+	return -ENODEV;
+}
+
 static inline int iommu_attach_group(struct iommu_domain *domain,
 				     struct iommu_group *group)
 {
-- 
2.17.1


  reply	other threads:[~2019-05-25  5:48 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-25  5:41 [PATCH v4 00/15] iommu/vt-d: Delegate DMA domain to generic iommu Lu Baolu
2019-05-25  5:41 ` Lu Baolu [this message]
2019-05-25  5:41 ` [PATCH v4 02/15] iommu/vt-d: Implement apply_resv_region iommu ops entry Lu Baolu
2019-05-25  5:41 ` [PATCH v4 03/15] iommu/vt-d: Expose ISA direct mapping region via iommu_get_resv_regions Lu Baolu
2019-05-25  5:41 ` [PATCH v4 04/15] iommu/vt-d: Enable DMA remapping after rmrr mapped Lu Baolu
2019-05-25  5:41 ` [PATCH v4 05/15] iommu/vt-d: Add device_def_domain_type() helper Lu Baolu
2019-05-25  5:41 ` [PATCH v4 06/15] iommu/vt-d: Delegate the identity domain to upper layer Lu Baolu
2019-05-25  5:41 ` [PATCH v4 07/15] iommu/vt-d: Delegate the dma " Lu Baolu
2020-08-21 18:33   ` Chris Wilson
2020-08-24  6:31     ` Lu Baolu
2020-08-24  8:35       ` Chris Wilson
2020-08-25  3:13         ` Lu Baolu
2019-05-25  5:41 ` [PATCH v4 08/15] iommu/vt-d: Identify default domains replaced with private Lu Baolu
2019-05-25  5:41 ` [PATCH v4 09/15] iommu/vt-d: Handle 32bit device with identity default domain Lu Baolu
2019-05-25  5:41 ` [PATCH v4 10/15] iommu/vt-d: Probe DMA-capable ACPI name space devices Lu Baolu
2019-05-29  6:16   ` Christoph Hellwig
2019-06-03  0:35     ` Lu Baolu
2019-05-25  5:41 ` [PATCH v4 11/15] iommu/vt-d: Implement is_attach_deferred iommu ops entry Lu Baolu
2019-05-25  5:41 ` [PATCH v4 12/15] iommu/vt-d: Cleanup get_valid_domain_for_dev() Lu Baolu
2019-07-18  3:12   ` Alex Williamson
2019-07-19  9:04     ` Lu Baolu
2019-07-19 15:23       ` Alex Williamson
2019-08-02  1:30         ` Alex Williamson
2019-08-02  7:17           ` Lu Baolu
2019-08-02 16:54             ` Alex Williamson
2019-08-04  3:16               ` Lu Baolu
2019-08-06  0:06               ` Lu Baolu
2019-05-25  5:41 ` [PATCH v4 13/15] iommu/vt-d: Remove startup parameter from device_def_domain_type() Lu Baolu
2019-05-25  5:41 ` [PATCH v4 14/15] iommu/vt-d: Remove duplicated code for device hotplug Lu Baolu
2019-05-25  5:41 ` [PATCH v4 15/15] iommu/vt-d: Remove static identity map code Lu Baolu
2019-05-27 15:00 ` [PATCH v4 00/15] iommu/vt-d: Delegate DMA domain to generic iommu Joerg Roedel

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=20190525054136.27810-2-baolu.lu@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=ashok.raj@intel.com \
    --cc=dima@arista.com \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=jacob.jun.pan@intel.com \
    --cc=jamessewart@arista.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sai.praneeth.prakhya@intel.com \
    --cc=tmurphy@arista.com \
    /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;
as well as URLs for NNTP newsgroup(s).