Skip to contentSkip to conversation

ARM: IOMMU: tegra: Add iommu_ops for GART/SMMU driver

PATCH In review Hiroshi DOYU 6 participants 25 messages Jan 5, 2012 – Jan 26, 2012

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 cover ARM: IOMMU: tegra: Add iommu_ops for GART/SMMU driver

This patchset adds support for IOMMU API for Tegra20(GART) and Tegra30(SMMU). "struct iommu_ops" are implemented for both H/W IOMMU modules.

Tested with "Ion memory manager" and "DMA mapping API"(*1).

This was originally developed with Tegra specific IOMMU API(tegra_iovmm_*()) in:

git://nv-tegra.nvidia.com/linux-2.6.git.

v2:
https://lkml.org/lkml/2011/12/15/203

v1:
https://lkml.org/lkml/2011/11/17/111

*1: Reworking the DMA mapping code (especially on ARM)

    http://lwn.net/Articles/467509/

Hiroshi DOYU (2):
ARM: IOMMU: Tegra20: Add iommu_ops for GART driver
ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

 arch/arm/mach-tegra/include/mach/smmu.h |   63 ++
 drivers/iommu/Kconfig                   |   22 +
 drivers/iommu/Makefile                  |    2 +
 drivers/iommu/tegra-gart.c              |  436 +++++++++++++
 drivers/iommu/tegra-smmu.c              | 1026 +++++++++++++++++++++++++++++++
 5 files changed, 1549 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/mach-tegra/include/mach/smmu.h
 create mode 100644 drivers/iommu/tegra-gart.c
 create mode 100644 drivers/iommu/tegra-smmu.c
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH v3 1/2 ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Tegra 20 IOMMU H/W, GART (Graphics Address Relocation Table). This patch implements struct iommu_ops for GART for the upper IOMMU API.

This H/W module supports only single virtual address space(domain), and manages a single level 1-to-1 mapping H/W translation page table.

---
 drivers/iommu/Kconfig      |   11 +
 drivers/iommu/Makefile     |    1 +
 drivers/iommu/tegra-gart.c |  436 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 448 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iommu/tegra-gart.c
drivers/iommu/Kconfig +11 -0
index 6bea696..4562698 100644
@@ -142,4 +142,15 @@ config OMAP_IOMMU_DEBUG
 
          Say N unless you know you need this.
 
+config TEGRA_IOMMU_GART
+	bool "Tegra GART IOMMU Support"
+	depends on ARCH_TEGRA_2x_SOC
+	default y
+	select IOMMU_API
+	help
+	  Enables support for remapping discontiguous physical memory
+	  shared with the operating system into contiguous I/O virtual
+	  space through the GART (Graphics Address Relocation Table)
+	  hardware included on Tegra SoCs.
+
 endif # IOMMU_SUPPORT
drivers/iommu/Makefile +1 -0
index 0e36b49..3238a31 100644
@@ -8,3 +8,4 @@ obj-$(CONFIG_IRQ_REMAP) += intr_remapping.o
 obj-$(CONFIG_OMAP_IOMMU) += omap-iommu.o
 obj-$(CONFIG_OMAP_IOVMM) += omap-iovmm.o
 obj-$(CONFIG_OMAP_IOMMU_DEBUG) += omap-iommu-debug.o
+obj-$(CONFIG_TEGRA_IOMMU_GART) += tegra-gart.o
drivers/iommu/tegra-gart.c new file +436 -0

436 lines not on this page · raw message

Signature · 1 line
1.7.5.4
maintainer
#2 lore raw

Re: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Hi,

first, some questions about the GART on your platform.

1) How many GARTs are usually implemented? One GART per device or is there a single GART covering all devices, or a mix of both?
2) Are devices allowed to DMA outside of the remappable range or will this fail?

Besides that I think IOMMU-API is not yet fully ready for GART-like drivers like this one. But I will merge it anyway to get things moving. But please answer or fix my objections first.

drivers/iommu/tegra-gart.c line 169

Quoted 13 lines · Hiroshi DOYU
drivers/iommu/tegra-gart.c new file +13 -0
+static void gart_iommu_domain_destroy(struct iommu_domain *domain)
+{
+	struct gart_device *gart = domain->priv;
+
+	spin_lock(&gart->client_lock);
+	if (!list_empty(&gart->client)) {
+		struct gart_client *c;
+
+		list_for_each_entry(c, &gart->client, list)
+			dev_err(gart->dev,
+				"%s is still attached\n", dev_name(c->dev));
+	}
+	spin_unlock(&gart->client_lock);

gart needs a NULL check. When you create a domain and immediatly destroy it without ever attaching a device this code will dereference a NULL pointer.
As a general improvement I suggest that you introduce a gart_domain structure and store it in domain->priv (assigned in domain_init) instead of using the hardware descriptor.

drivers/iommu/tegra-gart.c line 183

Quoted 11 lines
drivers/iommu/tegra-gart.c new file +11 -0
+static int gart_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
+{
+	struct gart_device *gart;
+	struct gart_client *client, *c;
+	int err = 0;
+
+	gart = dev_get_drvdata(dev->parent);
+	if (!gart)
+		return -EINVAL;
+	domain->priv = gart;

What happens when devices behind different GARTs are assigned to the same domain? domain->priv can only hold a pointer to one hardware GART. This can be solved by a 'struct gart_domain' holding a linked list to all gart_devices in this domain.

Joerg

#3 lore raw

Re: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Hi Joerg,

From: Joerg Roedel <[email protected]>
Subject: Re: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver
Date: Mon, 23 Jan 2012 16:00:48 +0100
Message-ID: <[email protected]>

Quoted 6 lines

Hi,

first, some questions about the GART on your platform.

1) How many GARTs are usually implemented? One GART per device or is there a single GART covering all devices, or a mix of both?

There's a single GART in the system which takes care of all devices.

Quoted 2 lines

2) Are devices allowed to DMA outside of the remappable range or will this fail?

Fail.

Quoted 15 lines

Besides that I think IOMMU-API is not yet fully ready for GART-like drivers like this one. But I will merge it anyway to get things moving. But please answer or fix my objections first.

Quoted 9 lines · Hiroshi DOYU
(quoted diff) +9 -0
+static void gart_iommu_domain_destroy(struct iommu_domain *domain)
+{
+	struct gart_device *gart = domain->priv;
+
+	spin_lock(&gart->client_lock);
+	if (!list_empty(&gart->client)) {
+		struct gart_client *c;
+
+		list_for_each_entry(c, &gart->client, list)
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			dev_err(gart->dev,
+				"%s is still attached\n", dev_name(c->dev));
+	}
+	spin_unlock(&gart->client_lock);

gart needs a NULL check. When you create a domain and immediatly destroy it without ever attaching a device this code will dereference a NULL pointer.

Fixed.

Quoted 4 lines

As a general improvement I suggest that you introduce a gart_domain structure and store it in domain->priv (assigned in domain_init) instead of using the hardware descriptor.

Quoted 16 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+static int gart_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
+{
+	struct gart_device *gart;
+	struct gart_client *client, *c;
+	int err = 0;
+
+	gart = dev_get_drvdata(dev->parent);
+	if (!gart)
+		return -EINVAL;
+	domain->priv = gart;

What happens when devices behind different GARTs are assigned to the same domain? domain->priv can only hold a pointer to one hardware GART. This can be solved by a 'struct gart_domain' holding a linked list to all gart_devices in this domain.

As discussed in SMMU thread, I'll revisit the above multiple iommu device support later.

Attached the update patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-ARM-IOMMU-Tegra20-Add-iommu_ops-for-GART-driver.patch
Type: text/x-patch
Size: 14203 bytes
Desc: 0001-ARM-IOMMU-Tegra20-Add-iommu_ops-for-GART-driver.patch
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20120125/763fd94b/attachment.bin>

#4 lore raw

Re: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Quoted 9 lines · Hiroshi Doyu

From: Hiroshi DOYU <[email protected]>
Date: Wed, 16 Nov 2011 17:36:37 +0200
Subject: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Tegra 20 IOMMU H/W, GART (Graphics Address Relocation Table). This patch implements struct iommu_ops for GART for the upper IOMMU API.

This H/W module supports only single virtual address space(domain), and manages a single level 1-to-1 mapping H/W translation page table.

Thanks. Applied with a few minor changes and fixes. Please see below.

Review

Quoted 4 lines
(quoted diff) +4 -0
+config TEGRA_IOMMU_GART
+	bool "Tegra GART IOMMU Support"
+	depends on ARCH_TEGRA_2x_SOC
+	default y

I removed 'default y'. New drivers shouldn't be selected by default.

Quoted 5 lines
(quoted diff) +5 -0
+static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
+			  phys_addr_t pa, size_t bytes, int prot)
+{
+	struct gart_device *gart = domain->priv;
+	unsigned long flags;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	unsigned long pfn;
+
+	if (!gart_iova_range_valid(gart, iova, bytes))
+		return -EINVAL;
+
+	spin_lock_irqsave(&gart->pte_lock, flags);
+	pfn = __phys_to_pfn(pa);
+	if (!pfn_valid(pfn)) {
+		dev_err(gart->dev, "Invalid page: %08x\n", pa);
+		spin_unlock(&gart->pte_lock);

Changed this to spin_unlock_irqrestore().

Quoted 6 lines
(quoted diff) +6 -0
+		return -EINVAL;
+	}
+	gart_set_pte(gart, iova, GART_PTE(pfn));
+	FLUSH_GART_REGS(gart);
+	spin_unlock_irqrestore(&gart->pte_lock, flags);
+	return 0;

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
+			       size_t bytes)
+{
+	struct gart_device *gart = domain->priv;
+	unsigned long flags;
+
+	if (!gart_iova_range_valid(gart, iova, bytes))
+		return -EINVAL;

Return 0 here instead of -EINVAL. Size_t is unsigned and the unmap path returns 0 on failure.

#5 lore raw

Re: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Quoted 5 lines · On Thu, 26 Jan 2012 12:58:13 +0100 "joro at 8bytes.org" <[email protected]> wrote:
Quoted 4 lines · Hiroshi Doyu

From: Hiroshi DOYU <[email protected]>
Date: Wed, 16 Nov 2011 17:36:37 +0200
Subject: [PATCH 1/2] ARM: IOMMU: Tegra20: Add iommu_ops for GART driver

Quoted 7 lines
Quoted 5 lines

Tegra 20 IOMMU H/W, GART (Graphics Address Relocation Table). This patch implements struct iommu_ops for GART for the upper IOMMU API.

This H/W module supports only single virtual address space(domain), and manages a single level 1-to-1 mapping H/W translation page table.

Thanks. Applied with a few minor changes and fixes. Please see below.

All fixes are valid. Thanks.

Quoted 45 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+config TEGRA_IOMMU_GART
+	bool "Tegra GART IOMMU Support"
+	depends on ARCH_TEGRA_2x_SOC
+	default y

I removed 'default y'. New drivers shouldn't be selected by default.

Quoted 5 lines
(quoted diff) +5 -0
+static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
+			  phys_addr_t pa, size_t bytes, int prot)
+{
+	struct gart_device *gart = domain->priv;
+	unsigned long flags;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	unsigned long pfn;
+
+	if (!gart_iova_range_valid(gart, iova, bytes))
+		return -EINVAL;
+
+	spin_lock_irqsave(&gart->pte_lock, flags);
+	pfn = __phys_to_pfn(pa);
+	if (!pfn_valid(pfn)) {
+		dev_err(gart->dev, "Invalid page: %08x\n", pa);
+		spin_unlock(&gart->pte_lock);

Changed this to spin_unlock_irqrestore().

Quoted 6 lines
(quoted diff) +6 -0
+		return -EINVAL;
+	}
+	gart_set_pte(gart, iova, GART_PTE(pfn));
+	FLUSH_GART_REGS(gart);
+	spin_unlock_irqrestore(&gart->pte_lock, flags);
+	return 0;

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
+			       size_t bytes)
+{
+	struct gart_device *gart = domain->priv;
+	unsigned long flags;
+
+	if (!gart_iova_range_valid(gart, iova, bytes))
+		return -EINVAL;

Return 0 here instead of -EINVAL. Size_t is unsigned and the unmap path returns 0 on failure.

#6 lore raw

PATCH v3 2/2 ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

In reply to Hiroshi DOYU

Tegra 30 IOMMU H/W, SMMU (System Memory Management Unit). This patch implements struct iommu_ops for SMMU for the upper IOMMU API.

This H/W module supports multiple virtual address spaces(domain x4), and manages 2 level H/W translation pagetable.

---
 arch/arm/mach-tegra/include/mach/smmu.h |   63 ++
 drivers/iommu/Kconfig                   |   11 +
 drivers/iommu/Makefile                  |    1 +
 drivers/iommu/tegra-smmu.c              | 1026 +++++++++++++++++++++++++++++++
 4 files changed, 1101 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/mach-tegra/include/mach/smmu.h
 create mode 100644 drivers/iommu/tegra-smmu.c
arch/arm/mach-tegra/include/mach/smmu.h new file +63 -0
new file mode 100644
index 0000000..dad403a
@@ -0,0 +1,63 @@
+/*
+ * IOMMU API for SMMU in Tegra30
+ *
+ * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef	MACH_SMMU_H
+#define	MACH_SMMU_H
+
+enum smmu_hwgrp {
+	HWGRP_AFI,
+	HWGRP_AVPC,
+	HWGRP_DC,
+	HWGRP_DCB,
+	HWGRP_EPP,
+	HWGRP_G2,
+	HWGRP_HC,
+	HWGRP_HDA,
+	HWGRP_ISP,
+	HWGRP_MPE,
+	HWGRP_NV,
+	HWGRP_NV2,
+	HWGRP_PPCS,
+	HWGRP_SATA,
+	HWGRP_VDE,
+	HWGRP_VI,
+
+	HWGRP_COUNT,
+
+	HWGRP_END = ~0,
+};
+
+#define HWG_AFI		(1 << HWGRP_AFI)
+#define HWG_AVPC	(1 << HWGRP_AVPC)
+#define HWG_DC		(1 << HWGRP_DC)
+#define HWG_DCB		(1 << HWGRP_DCB)
+#define HWG_EPP		(1 << HWGRP_EPP)
+#define HWG_G2		(1 << HWGRP_G2)
+#define HWG_HC		(1 << HWGRP_HC)
+#define HWG_HDA		(1 << HWGRP_HDA)
+#define HWG_ISP		(1 << HWGRP_ISP)
+#define HWG_MPE		(1 << HWGRP_MPE)
+#define HWG_NV		(1 << HWGRP_NV)
+#define HWG_NV2		(1 << HWGRP_NV2)
+#define HWG_PPCS	(1 << HWGRP_PPCS)
+#define HWG_SATA	(1 << HWGRP_SATA)
+#define HWG_VDE		(1 << HWGRP_VDE)
+#define HWG_VI		(1 << HWGRP_VI)
+
+#endif	/* MACH_SMMU_H */
drivers/iommu/Kconfig +11 -0
index 4562698..4b3d616 100644
@@ -153,4 +153,15 @@ config TEGRA_IOMMU_GART
 	  space through the GART (Graphics Address Relocation Table)
 	  hardware included on Tegra SoCs.
 
+config TEGRA_IOMMU_SMMU
+	bool "Tegra SMMU IOMMU Support"
+	depends on ARCH_TEGRA_3x_SOC
+	default y
+	select IOMMU_API
+	help
+	  Enables support for remapping discontiguous physical memory
+	  shared with the operating system into contiguous I/O virtual
+	  space through the SMMU (System Memory Management Unit)
+	  hardware included on Tegra SoCs.
+
 endif # IOMMU_SUPPORT
drivers/iommu/Makefile +1 -0
index 3238a31..7ad7a3b 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_OMAP_IOMMU) += omap-iommu.o
 obj-$(CONFIG_OMAP_IOVMM) += omap-iovmm.o
 obj-$(CONFIG_OMAP_IOMMU_DEBUG) += omap-iommu-debug.o
 obj-$(CONFIG_TEGRA_IOMMU_GART) += tegra-gart.o
+obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
drivers/iommu/tegra-smmu.c new file +1026 -0

1,026 lines not on this page · raw message

Signature · 1 line
1.7.5.4
maintainer
#7 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Hi,

please see my comments inline. When you fix these issues I think the driver is ready for merging.

Quoted 6 lines · Hiroshi DOYU
drivers/iommu/tegra-smmu.c new file +6 -0
+static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
+			  phys_addr_t pa, size_t bytes, int prot)
+{
+	struct smmu_as *as = domain->priv;
+	unsigned long pfn = __phys_to_pfn(pa);
+	unsigned long flags;

drivers/iommu/tegra-smmu.c line 644

Quoted 10 lines
drivers/iommu/tegra-smmu.c new file +10 -0
+
+	dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
+
+	if (!pfn_valid(pfn))
+		return -ENOMEM;
+
+	spin_lock_irqsave(&as->lock, flags);
+	__smmu_iommu_map_pfn(as, iova, pfn);
+	spin_unlock_irqrestore(&as->lock, flags);
+	return 0;

Why do you completly ignore the size parameter in this function (and in the unmap part below)?
According to the page-sizes you export to the generic layer size can be 4k or 4M. You need to take care of that in this function.

Quoted 14 lines
drivers/iommu/tegra-smmu.c new file +14 -0
+static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;
+	unsigned long flags;
+
+	spin_lock_irqsave(&as->lock, flags);
+
+	if (as->pdir_page) {
+		spin_lock(&smmu->lock);
+		smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
+		smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
+		FLUSH_SMMU_REGS(smmu);
+		spin_unlock(&smmu->lock);

drivers/iommu/tegra-smmu.c line 757

Quoted 10 lines
drivers/iommu/tegra-smmu.c new file +10 -0
+
+		free_pdir(as);
+	}
+
+	if (!list_empty(&as->client)) {
+		struct smmu_client *c;
+
+		list_for_each_entry(c, &as->client, list)
+			dev_err(smmu->dev,
+				"%s is still attached\n", dev_name(c->dev));

This is not an error. Just detach the devices when they are still attached to the domain.

drivers/iommu/tegra-smmu.c line 770

Quoted 13 lines
drivers/iommu/tegra-smmu.c new file +13 -0
+	}
+
+	spin_unlock_irqrestore(&as->lock, flags);
+
+	domain->priv = NULL;
+	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
+}
+
+static int smmu_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

Thanks,

Joerg

#8 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Hi Joerg,

From: Joerg Roedel <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Mon, 23 Jan 2012 16:43:10 +0100
Message-ID: <[email protected]>

Quoted 19 lines

Hi,

please see my comments inline. When you fix these issues I think the driver is ready for merging.

Quoted 13 lines · Hiroshi DOYU
drivers/iommu/tegra-smmu.c new file +13 -0
+static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
+			  phys_addr_t pa, size_t bytes, int prot)
+{
+	struct smmu_as *as = domain->priv;
+	unsigned long pfn = __phys_to_pfn(pa);
+	unsigned long flags;
+
+	dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
+
+	if (!pfn_valid(pfn))
+		return -ENOMEM;
+
+	spin_lock_irqsave(&as->lock, flags);
Quoted 8 lines

drivers/iommu/tegra-smmu.c line 644

Quoted 3 lines
drivers/iommu/tegra-smmu.c new file +3 -0
+	__smmu_iommu_map_pfn(as, iova, pfn);
+	spin_unlock_irqrestore(&as->lock, flags);
+	return 0;

Why do you completly ignore the size parameter in this function (and in the unmap part below)?
According to the page-sizes you export to the generic layer size can be 4k or 4M. You need to take care of that in this function.

I'll drop 4MB support here once. I'll make another patch for 4MB page support later.

Quoted 19 lines
Quoted 19 lines
drivers/iommu/tegra-smmu.c new file +19 -0
+static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;
+	unsigned long flags;
+
+	spin_lock_irqsave(&as->lock, flags);
+
+	if (as->pdir_page) {
+		spin_lock(&smmu->lock);
+		smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
+		smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
+		FLUSH_SMMU_REGS(smmu);
+		spin_unlock(&smmu->lock);
+
+		free_pdir(as);
+	}
+
+	if (!list_empty(&as->client)) {
Quoted 8 lines

drivers/iommu/tegra-smmu.c line 757

Quoted 5 lines
drivers/iommu/tegra-smmu.c new file +5 -0
+		struct smmu_client *c;
+
+		list_for_each_entry(c, &as->client, list)
+			dev_err(smmu->dev,
+				"%s is still attached\n", dev_name(c->dev));

This is not an error. Just detach the devices when they are still attached to the domain.

Ok

Quoted 10 lines
Quoted 10 lines
drivers/iommu/tegra-smmu.c new file +10 -0
+	}
+
+	spin_unlock_irqrestore(&as->lock, flags);
+
+	domain->priv = NULL;
+	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
+}
+
+static int smmu_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
Quoted 8 lines

drivers/iommu/tegra-smmu.c line 737

Quoted 3 lines
drivers/iommu/tegra-smmu.c new file +3 -0
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

I'm a bit confused with the concept of "domain". I thought that "domain" is equivalent to a "virtual address space". Usually a IOMMU device provides a virtual address space for multiple client devices. IOW, a IOMMU device provides a virtual address space, which can be shared with multiple client devices.

Actually Tegra SMMU case, a single IOMMU device has 4 different virtual address speace("smmu_as"). Each "smmu_as" has its own virtual address space. "smmu_as[i]" has mutiple "smmu_client" devices.

smmu_as[i] == domain[i]

I don't understand why "a domain can contain devices behind different SMMUs" because those client devices belong to different virtual address spaces, and they should belong to different "domains".

Could you please explain a bit more about "domain"?

Quoted 6 lines

Thanks,

Joerg

#9 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Quoted 7 lines · Hiroshi Doyu
Quoted 4 lines

Why do you completly ignore the size parameter in this function (and in the unmap part below)?
According to the page-sizes you export to the generic layer size can be 4k or 4M. You need to take care of that in this function.

I'll drop 4MB support here once. I'll make another patch for 4MB page support later.

Okay, so when you only export 4k everything should be fine.

Quoted 15 lines
Quoted 4 lines

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

I'm a bit confused with the concept of "domain". I thought that "domain" is equivalent to a "virtual address space". Usually a IOMMU device provides a virtual address space for multiple client devices. IOW, a IOMMU device provides a virtual address space, which can be shared with multiple client devices.

Actually Tegra SMMU case, a single IOMMU device has 4 different virtual address speace("smmu_as"). Each "smmu_as" has its own virtual address space. "smmu_as[i]" has mutiple "smmu_client" devices.

Quoted 7 lines

smmu_as[i] == domain[i]

I don't understand why "a domain can contain devices behind different SMMUs" because those client devices belong to different virtual address spaces, and they should belong to different "domains".

Could you please explain a bit more about "domain"?

A domain is, as you said, a virtual address space for IO devices. But the important point is, an arbitrary number of devices can be part of a domain. This also means that the devices can be behind different hardware SMMUs. In this case your driver needs to program the page-table pointer into more than one SMMU to give devices behind different SMMUs the same address space.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#10 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

From: Joerg Roedel <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Tue, 24 Jan 2012 12:04:44 +0100
Message-ID: <[email protected]>

Quoted 21 lines
Quoted 21 lines
Quoted 4 lines

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

I'm a bit confused with the concept of "domain". I thought that "domain" is equivalent to a "virtual address space". Usually a IOMMU device provides a virtual address space for multiple client devices. IOW, a IOMMU device provides a virtual address space, which can be shared with multiple client devices.

Actually Tegra SMMU case, a single IOMMU device has 4 different virtual address speace("smmu_as"). Each "smmu_as" has its own virtual address space. "smmu_as[i]" has mutiple "smmu_client" devices.

smmu_as[i] == domain[i]

I don't understand why "a domain can contain devices behind different SMMUs" because those client devices belong to different virtual address spaces, and they should belong to different "domains".

Quoted 8 lines
Quoted 1 line

Could you please explain a bit more about "domain"?

A domain is, as you said, a virtual address space for IO devices. But the important point is, an arbitrary number of devices can be part of a domain. This also means that the devices can be behind different hardware SMMUs. In this case your driver needs to program the page-table pointer into more than one SMMU to give devices behind different SMMUs the same address space.

Thank you for explaining.

Does the above mean that a buffer can be shared with different devices which belong to different IOMMU devices(virtual address spaces)?

For example, assuming the following:

- We have "struct iommu_domain *domain1".
- "domain1" has iommu device "iommu_dev1" and "iommu_dev2".
- "iommu_dev1" has "client_dev1" and "client_dev2".
- "iommu_dev2" has "client_dev3" and "client_dev4".

"iommu_map(domain1, iova, pa, ...)" will create the following mapping ___at once___:

- (iova)-(pa) mapping in iommu_dev1(iommmu_dev1's virtual address space)
- (iova)-(pa) mapping in iommu_dev2(iommmu_dev2's virtual address space)

Is the above correct?

It seems that the same (iova) is used for different virtual address spaces. What kind of case is this beneficial most in?

#11 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Quoted 19 lines · Hiroshi Doyu
Quoted 6 lines

A domain is, as you said, a virtual address space for IO devices. But the important point is, an arbitrary number of devices can be part of a domain. This also means that the devices can be behind different hardware SMMUs. In this case your driver needs to program the page-table pointer into more than one SMMU to give devices behind different SMMUs the same address space.

Thank you for explaining.

Does the above mean that a buffer can be shared with different devices which belong to different IOMMU devices(virtual address spaces)?

For example, assuming the following:

- We have "struct iommu_domain *domain1".
- "domain1" has iommu device "iommu_dev1" and "iommu_dev2".
- "iommu_dev1" has "client_dev1" and "client_dev2".
- "iommu_dev2" has "client_dev3" and "client_dev4".

Quoted 7 lines

"iommu_map(domain1, iova, pa, ...)" will create the following mapping ___at once___:

- (iova)-(pa) mapping in iommu_dev1(iommmu_dev1's virtual address space)
- (iova)-(pa) mapping in iommu_dev2(iommmu_dev2's virtual address space)

Is the above correct?

Yes, this is correct.

Quoted 2 lines

It seems that the same (iova) is used for different virtual address spaces. What kind of case is this beneficial most in?

It is actually the _same_ virtual address space which is used by iommu_dev1 and iommu_dev2. Think of it like multiple threads of a single process. They also share the address space.
This is a requirement of the iommu-api which is beneficial for virtualization and simplifies the usage of the api in general. The user does not need to care which devices can be assigned to which domain because of underlying hardware constraints. The goal of the iommu-api is to hide such contraints.

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#12 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

From: "[email protected]" <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Tue, 24 Jan 2012 12:57:23 +0100
Message-ID: <[email protected]>

Quoted 28 lines · Hiroshi Doyu
Quoted 19 lines
Quoted 6 lines

A domain is, as you said, a virtual address space for IO devices. But the important point is, an arbitrary number of devices can be part of a domain. This also means that the devices can be behind different hardware SMMUs. In this case your driver needs to program the page-table pointer into more than one SMMU to give devices behind different SMMUs the same address space.

Thank you for explaining.

Does the above mean that a buffer can be shared with different devices which belong to different IOMMU devices(virtual address spaces)?

For example, assuming the following:

- We have "struct iommu_domain *domain1".
- "domain1" has iommu device "iommu_dev1" and "iommu_dev2".
- "iommu_dev1" has "client_dev1" and "client_dev2".
- "iommu_dev2" has "client_dev3" and "client_dev4".

Quoted 7 lines

"iommu_map(domain1, iova, pa, ...)" will create the following mapping ___at once___:

- (iova)-(pa) mapping in iommu_dev1(iommmu_dev1's virtual address space)
- (iova)-(pa) mapping in iommu_dev2(iommmu_dev2's virtual address space)

Is the above correct?

Quoted 8 lines

Yes, this is correct.

Quoted 2 lines

It seems that the same (iova) is used for different virtual address spaces. What kind of case is this beneficial most in?

It is actually the _same_ virtual address space which is used by iommu_dev1 and iommu_dev2. Think of it like multiple threads of a single process. They also share the address space.

I see. The above makes more sense.

Quoted 5 lines

This is a requirement of the iommu-api which is beneficial for virtualization and simplifies the usage of the api in general. The user does not need to care which devices can be assigned to which domain because of underlying hardware constraints. The goal of the iommu-api is to hide such contraints.

Now it's clear to me. I'll send the update patch soon. Thank you for explaining all of this.

Quoted 11 lines

Joerg

Signature · 5 lines
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
#13 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Mon, 23 Jan 2012 16:43:10 +0100
Message-ID: <[email protected]>

Quoted 10 lines
Quoted 10 lines
drivers/iommu/tegra-smmu.c new file +10 -0
+	}
+
+	spin_unlock_irqrestore(&as->lock, flags);
+
+	domain->priv = NULL;
+	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
+}
+
+static int smmu_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
Quoted 8 lines

drivers/iommu/tegra-smmu.c line 737

Quoted 3 lines
drivers/iommu/tegra-smmu.c new file +3 -0
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

Actually I really like the concept of this "domain" now, which hides the H/W hierarchy from users.

But in Tegra SMMU/GART case, there's a single one IOMMU device in the system. Keeping a iommu device list in a domain and iterating iommu device list in each iommu_ops seem to be so nice, but I'm afraid that this may be a bit too much when one already knows that there's only one IOMMU device in the system.

If there's no actual problem for 1-1 mapping between IOMMU H/Ws and domains, I think that it may not so bad to keep the original code(1-1) for GART and SMMU. What do you think?

#14 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Hi,

Quoted 33 lines · Hiroshi Doyu

From: Joerg Roedel <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Mon, 23 Jan 2012 16:43:10 +0100
Message-ID: <[email protected]>

Quoted 10 lines
Quoted 10 lines
drivers/iommu/tegra-smmu.c new file +10 -0
+	}
+
+	spin_unlock_irqrestore(&as->lock, flags);
+
+	domain->priv = NULL;
+	dev_dbg(smmu->dev, "smmu_as@%p\n", as);
+}
+
+static int smmu_iommu_attach_dev(struct iommu_domain *domain,
+				 struct device *dev)
Quoted 8 lines

drivers/iommu/tegra-smmu.c line 737

Quoted 3 lines
drivers/iommu/tegra-smmu.c new file +3 -0
+{
+	struct smmu_as *as = domain->priv;
+	struct smmu_device *smmu = as->smmu;

Hmm, this looks like there is a 1-1 mapping between hardware SMMU devices and domains. This is not consistent with IOMMU-API semantics where a domain can contain devices behind different SMMUs. Please fix that.

Actually I really like the concept of this "domain" now, which hides the H/W hierarchy from users.

But in Tegra SMMU/GART case, there's a single one IOMMU device in the system. Keeping a iommu device list in a domain and iterating iommu device list in each iommu_ops seem to be so nice, but I'm afraid that this may be a bit too much when one already knows that there's only one IOMMU device in the system.

Quoted 3 lines

If there's no actual problem for 1-1 mapping between IOMMU H/Ws and domains, I think that it may not so bad to keep the original code(1-1) for GART and SMMU. What do you think?

I think it boils down to "extensability". If you can truly/fully guarantee that there will *always* be a single IOMMU on all upcoming Tegras, then it's really overkill.

But if there's even a remote possibility of the HW being changed and you end up with more IOMMUs, things start to feel necessary for the sake of making it easy to extend.

my 2 cents, feel free to ignore ;-)

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20120124/7a88febd/attachment.sig>
#15 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Quoted 14 lines · Felipe Balbi
Quoted 9 lines · Hiroshi Doyu

Actually I really like the concept of this "domain" now, which hides the H/W hierarchy from users.

But in Tegra SMMU/GART case, there's a single one IOMMU device in the system. Keeping a iommu device list in a domain and iterating iommu device list in each iommu_ops seem to be so nice, but I'm afraid that this may be a bit too much when one already knows that there's only one IOMMU device in the system.

Quoted 3 lines

If there's no actual problem for 1-1 mapping between IOMMU H/Ws and domains, I think that it may not so bad to keep the original code(1-1) for GART and SMMU. What do you think?

Quoted 7 lines

I think it boils down to "extensability". If you can truly/fully guarantee that there will *always* be a single IOMMU on all upcoming Tegras, then it's really overkill.

But if there's even a remote possibility of the HW being changed and you end up with more IOMMUs, things start to feel necessary for the sake of making it easy to extend.

Right. But I am fine with the logic as-is when there is only one SMMU in the system. But please also change the IOMMU driver so that it really only initializes a single SMMU. When boards pop up with more than one you we notice that assumption in the code again and are reminded to change it.

Joerg

#16 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

From: "[email protected]" <[email protected]>
Subject: Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver
Date: Tue, 24 Jan 2012 15:25:21 +0100
Message-ID: <[email protected]>

Quoted 23 lines · Felipe Balbi
Quoted 14 lines · Hiroshi Doyu
Quoted 9 lines

Actually I really like the concept of this "domain" now, which hides the H/W hierarchy from users.

But in Tegra SMMU/GART case, there's a single one IOMMU device in the system. Keeping a iommu device list in a domain and iterating iommu device list in each iommu_ops seem to be so nice, but I'm afraid that this may be a bit too much when one already knows that there's only one IOMMU device in the system.

Quoted 3 lines

If there's no actual problem for 1-1 mapping between IOMMU H/Ws and domains, I think that it may not so bad to keep the original code(1-1) for GART and SMMU. What do you think?

Quoted 7 lines

I think it boils down to "extensability". If you can truly/fully guarantee that there will *always* be a single IOMMU on all upcoming Tegras, then it's really overkill.

But if there's even a remote possibility of the HW being changed and you end up with more IOMMUs, things start to feel necessary for the sake of making it easy to extend.

Quoted 5 lines

Right. But I am fine with the logic as-is when there is only one SMMU in the system. But please also change the IOMMU driver so that it really only initializes a single SMMU. When boards pop up with more than one you we notice that assumption in the code again and are reminded to change it.

Fixed.

I'll revisit 4MB pagesize support and the above multiple iommu device support in a domain later.

Attached the update patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-ARM-IOMMU-Tegra30-Add-iommu_ops-for-SMMU-driver.patch
Type: text/x-patch
Size: 33838 bytes
Desc: 0002-ARM-IOMMU-Tegra30-Add-iommu_ops-for-SMMU-driver.patch
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20120125/1aba4315/attachment-0001.bin>

#17 lore raw

Re: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Quoted 11 lines · Hiroshi Doyu

From: Hiroshi DOYU <[email protected]>
Date: Thu, 17 Nov 2011 07:31:31 +0200
Subject: [PATCH 2/2] ARM: IOMMU: Tegra30: Add iommu_ops for SMMU driver

Tegra 30 IOMMU H/W, SMMU (System Memory Management Unit). This patch implements struct iommu_ops for SMMU for the upper IOMMU API.

This H/W module supports multiple virtual address spaces(domain x4), and manages 2 level H/W translation pagetable.

Quoted 8 lines
---
 arch/arm/mach-tegra/include/mach/smmu.h |   63 ++
 drivers/iommu/Kconfig                   |   11 +
 drivers/iommu/Makefile                  |    1 +
 drivers/iommu/tegra-smmu.c              | 1034 +++++++++++++++++++++++++++++++
 4 files changed, 1109 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/mach-tegra/include/mach/smmu.h
 create mode 100644 drivers/iommu/tegra-smmu.c

Removed 'default y' and applied to arm/tegra as well. Thanks.

Joerg

#18 lore raw
In reply to Hiroshi DOYU
Quoted 5 lines · On Thu, 5 Jan 2012 08:11:47 +0100 Hiroshi Doyu <[email protected]> wrote:

This patchset adds support for IOMMU API for Tegra20(GART) and Tegra30(SMMU). "struct iommu_ops" are implemented for both H/W IOMMU modules.

Tested with "Ion memory manager" and "DMA mapping API"(*1).

Just for DMA mapping test from MPU side, the following one is used.

Quoted 1 line

From a7a163c29694c056e020ba9200648120498e1764 Mon Sep 17 00:00:00 2001

From: Hiroshi DOYU <[email protected]>
Date: Thu, 15 Dec 2011 13:24:24 +0200
Subject: [PATCH 1/1] [NOT-FOR-MERGE] TEST: Simple dma-iommu-mapping API TEST
module

This is not posted to merge but this is necessary to understand how SMMU/GART works with DMA(-iommu-mapping-)API.

This is a test to verify DMA API(DMA iommu mapping API), where SoC specific iommu_ops is used internally.

This does alloc/(un)map/free, but there's no actual access from device side since it requires device specific communications.

---
 drivers/iommu/dmaapi-test.c |  193 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 193 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iommu/dmaapi-test.c
drivers/iommu/dmaapi-test.c new file +193 -0
new file mode 100644
index 0000000..f386249
@@ -0,0 +1,193 @@
+/*
+ * DMA IOMMU mapping API test module
+ *
+ * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
+ *
+ * Author: Krishna Reddy <[email protected]>,
+ *	Hiroshi DOYU <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define DEBUG
+#define pr_fmt(fmt)	KBUILD_MODNAME ":" fmt
+
+#include <linux/module.h>
+#include <linux/dma-mapping.h>
+#include <linux/kthread.h>
+#include <linux/delay.h>
+#include <linux/highmem.h>
+#include <linux/pfn.h>
+#include <linux/dma-mapping.h>
+#include <linux/platform_device.h>
+
+#include <mach/iomap.h>
+#include <mach/smmu.h>
+
+#include <asm/dma-iommu.h>
+
+#include "devices.h"
+
+/* FIXME: 2x and 3x should be supported at once */
+#ifdef CONFIG_ARCH_TEGRA_2x_SOC
+#define IOVA_START	TEGRA_GART_BASE
+#define IOVA_SIZE	TEGRA_GART_SIZE
+static struct platform_device *tegra_iommu_device = &tegra_gart_device;
+#elif CONFIG_ARCH_TEGRA_3x_SOC
+#define IOVA_START	TEGRA_SMMU_BASE
+#define IOVA_SIZE	TEGRA_SMMU_SIZE
+static struct platform_device *tegra_iommu_device = &tegra_smmu_device;
+#else
+#error Unsupported device
+#endif
+
+#define NUM_TEST	3
+#define MAP_SIZE	(4 * PAGE_SIZE)
+
+struct dmaapi_test_case {
+	char *name;
+	void (*fn)(struct device *);
+};
+
+static void dmaapi_test_map_page(struct device *dev)
+{
+	struct page *page;
+	dma_addr_t dma_addr;
+	void *cpu_addr;
+
+	page = alloc_page(GFP_KERNEL);
+	BUG_ON(!page);
+
+	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
+	BUG_ON(!dma_addr);
+
+	cpu_addr = kmap(page);
+	BUG_ON(!cpu_addr);
+	memset(cpu_addr, 0xa5, PAGE_SIZE);
+	kunmap(cpu_addr);
+
+	pr_debug("pid:%d,%s mapped\t%08x:%08x\n",
+		 current->pid, dev_name(dev), dma_addr, page_to_phys(page));
+
+	dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
+	__free_page(page);
+}
+
+static void dmaapi_test_alloc_coherent(struct device *dev)
+{
+	dma_addr_t da[NUM_TEST];
+	void *va[NUM_TEST];
+	int i;
+
+	for (i = 0; i < NUM_TEST; i++) {
+		void *cpu_addr;
+		dma_addr_t dma_addr;
+
+		cpu_addr = dma_alloc_coherent(dev, MAP_SIZE,
+					      &dma_addr, GFP_KERNEL);
+		BUG_ON(!cpu_addr);
+		memset(cpu_addr, 0xa5, MAP_SIZE);
+
+		pr_debug("pid:%d,%s,[%d] mapped\t%08x:%08x\n",
+			 current->pid, dev_name(dev), i,
+			 dma_addr, virt_to_phys(cpu_addr));
+
+		da[i] = dma_addr;
+		va[i] = cpu_addr;
+	}
+
+	while (--i >= 0) {
+		pr_debug("pid:%d,%s,[%d] unmapping\t%08x:%08x\n",
+			 current->pid, dev_name(dev), i,
+			 da[i], virt_to_phys(va[i]));
+		dma_free_coherent(dev, MAP_SIZE, va[i], da[i]);
+	}
+}
+
+static struct dmaapi_test_case test[] = {
+	{
+		.name = "dmaapi/map page",
+		.fn = dmaapi_test_map_page,
+	},
+	{
+		.name = "dmaapi/alloc coherent",
+		.fn = dmaapi_test_alloc_coherent,
+	},
+};
+
+static u32 dummy_hwgrp_map[] = {
+	HWG_DC | HWG_AFI | HWG_AVPC | HWG_DCB,
+	HWG_EPP | HWG_HC | HWG_G2 | HWG_MPE | HWG_HDA | HWG_ISP,
+	HWG_NV | HWG_PPCS | HWG_SATA | HWG_NV2 | HWG_VI | HWG_VDE,
+};
+
+/* FIXME: Need driver for iommu context? */
+static struct platform_device dmaapi_dummy_device[] = {
+	{ .name = "hwgrp at a", .id = -1, },
+	{ .name = "hwgrp at b", .id = -1, },
+	{ .name = "hwgrp at c", .id = -1, },
+};
+
+static int dmaapi_test_thread(void *data)
+{
+	int i;
+	struct dmaapi_test_case *c = data;
+
+	for (i = 0; true; i++) {
+		struct device *dev;
+		int interval[] = {7, 3, 5,};
+		int n;
+
+		n = i % ARRAY_SIZE(dmaapi_dummy_device);
+		ssleep(interval[n]);
+		dev = &dmaapi_dummy_device[n].dev;
+		c->fn(dev);
+	}
+	return 0;
+}
+
+static int __init dmaapi_test_init(void)
+{
+	int i;
+	struct dma_iommu_mapping *map;
+
+	map = arm_iommu_create_mapping(IOVA_START, IOVA_SIZE, 0);
+	BUG_ON(!map);
+	pr_debug("Allocate IOVA: %08x-%08x\n", map->base, map->base + IOVA_SIZE);
+
+	for (i = 0; i < ARRAY_SIZE(dmaapi_dummy_device); i++) {
+		int err;
+		struct platform_device *pdev = &dmaapi_dummy_device[i];
+
+		pdev->dev.platform_data = (void *)dummy_hwgrp_map[i];
+		pdev->dev.parent = &tegra_iommu_device->dev;
+		err = platform_device_register(pdev);
+		BUG_ON(err);
+
+		err = arm_iommu_attach_device(&pdev->dev, map);
+		BUG_ON(err);
+		pr_debug("IOMMU API: Attached to %s\n", dev_name(&pdev->dev));
+	}
+
+	for (i = 0; i < ARRAY_SIZE(test); i++)
+		kthread_run(dmaapi_test_thread, &test[i], test[i].name);
+
+	return 0;
+}
+module_init(dmaapi_test_init);
+
+MODULE_AUTHOR("Krishna Reddy <[email protected]>");
+MODULE_AUTHOR("Hiroshi DOYU <[email protected]>");
+MODULE_DESCRIPTION("DMA IOMMU mapping API test");
+MODULE_LICENSE("GPL v2");
Signature · 1 line
1.7.5.4
#19 lore raw
Quoted 1 line · Hiroshi Doyu

Just for DMA mapping test from MPU side, the following one is used.

This patch is buggy.

Quoted 6 lines
drivers/iommu/dmaapi-test.c new file +6 -0
+static void dmaapi_test_map_page(struct device *dev)
+{
+	struct page *page;
+	dma_addr_t dma_addr;
+	void *cpu_addr;
+

drivers/iommu/dmaapi-test.c line 78

Quoted 10 lines
drivers/iommu/dmaapi-test.c new file +10 -0
+	page = alloc_page(GFP_KERNEL);
+	BUG_ON(!page);
+
+	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
+	BUG_ON(!dma_addr);
+
+	cpu_addr = kmap(page);
+	BUG_ON(!cpu_addr);
+	memset(cpu_addr, 0xa5, PAGE_SIZE);
+	kunmap(cpu_addr);

The DMA API works like this:

- The CPU owns the page or buffer and can access it.
- You map the page or buffer.
- The device owns the page or buffer; the CPU must explicitly access it.
- You unmap the page or buffer.
- The CPU again owns the page/buffer and can access it.

Please respect the DMA API rules.

So. Once dma_map_page() has returned, you must not kmap() or otherwise access the data contained in that page until after you have unmapped it.

#20 lore raw

Hi Russell,

From: Russell King - ARM Linux <[email protected]>
Subject: Re: [PATCH v3 0/2] ARM: IOMMU: tegra: Add iommu_ops for GART/SMMU driver
Date: Thu, 5 Jan 2012 13:53:26 +0100
Message-ID: <[email protected]>

Quoted 24 lines · Hiroshi Doyu
Quoted 1 line

Just for DMA mapping test from MPU side, the following one is used.

This patch is buggy.

Quoted 6 lines
drivers/iommu/dmaapi-test.c new file +6 -0
+static void dmaapi_test_map_page(struct device *dev)
+{
+	struct page *page;
+	dma_addr_t dma_addr;
+	void *cpu_addr;
+

drivers/iommu/dmaapi-test.c line 78

Quoted 10 lines
drivers/iommu/dmaapi-test.c new file +10 -0
+	page = alloc_page(GFP_KERNEL);
+	BUG_ON(!page);
+
+	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
+	BUG_ON(!dma_addr);
+
+	cpu_addr = kmap(page);
+	BUG_ON(!cpu_addr);
+	memset(cpu_addr, 0xa5, PAGE_SIZE);
+	kunmap(cpu_addr);

The DMA API works like this:

Quoted 7 lines

- The CPU owns the page or buffer and can access it.
- You map the page or buffer.
- The device owns the page or buffer; the CPU must explicitly access it.
- You unmap the page or buffer.
- The CPU again owns the page/buffer and can access it.

Please respect the DMA API rules.

Right, I do.

Quoted 2 lines

So. Once dma_map_page() has returned, you must not kmap() or otherwise access the data contained in that page until after you have unmapped it.

Ok, the above function order should be as below?

  page = alloc_page(GFP_KERNEL);
      cpu_addr = kmap(page);
      memset(cpu_addr, 0xa5, PAGE_SIZE);
  	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
  	    < expect GPU does something >
  	dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
      < CPU can access data here again>
      kunmap(cpu_addr);
  __free_page(page);
#21 lore raw
Quoted 11 lines · Hiroshi Doyu

Ok, the above function order should be as below?

  page = alloc_page(GFP_KERNEL);
      cpu_addr = kmap(page);
      memset(cpu_addr, 0xa5, PAGE_SIZE);
  	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
  	    < expect GPU does something >
  	dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
      < CPU can access data here again>
      kunmap(cpu_addr);
  __free_page(page);

That should work, but it's not particularly nice to wrap kmap around the DMA API.

Bear in mind that alloc_page(GFP_KERNEL) will always give you a lowmem page. So, you might as well do:

	page = alloc_page(GFP_KERNEL);
	cpu_addr = page_address(page);
	memset()
	dma_addr = dma_map_single(dev, cpu_addr, PAGE_SIZE, DMA_TO_DEVICE);
		< GPU access >
	dma_unmap_single(dev, dma_ddr, PAGE_SIZE, DMA_TO_DEVICE);
	< CPU access >
	__free_page(page);

and this doesn't raise any issues with kmap vs DMA API.

#22 lore raw

From: Russell King - ARM Linux <[email protected]>
Subject: Re: [PATCH v3 0/2] ARM: IOMMU: tegra: Add iommu_ops for GART/SMMU driver
Date: Thu, 5 Jan 2012 15:46:31 +0100
Message-ID: <[email protected]>

Quoted 28 lines · Hiroshi Doyu
Quoted 11 lines

Ok, the above function order should be as below?

  page = alloc_page(GFP_KERNEL);
      cpu_addr = kmap(page);
      memset(cpu_addr, 0xa5, PAGE_SIZE);
  	dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
  	    < expect GPU does something >
  	dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
      < CPU can access data here again>
      kunmap(cpu_addr);
  __free_page(page);

That should work, but it's not particularly nice to wrap kmap around the DMA API.

Bear in mind that alloc_page(GFP_KERNEL) will always give you a lowmem page. So, you might as well do:

	page = alloc_page(GFP_KERNEL);
	cpu_addr = page_address(page);
	memset()
	dma_addr = dma_map_single(dev, cpu_addr, PAGE_SIZE, DMA_TO_DEVICE);
		< GPU access >
	dma_unmap_single(dev, dma_ddr, PAGE_SIZE, DMA_TO_DEVICE);
	< CPU access >
	__free_page(page);
Quoted 1 line

and this doesn't raise any issues with kmap vs DMA API.

Attached the update one just in case that someone wants to use it for theirs. -------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-NOT-FOR-MERGE-TEST-Simple-dma-iommu-mapping-API-TEST.patch
Type: text/x-patch
Size: 6451 bytes
Desc: 0001-NOT-FOR-MERGE-TEST-Simple-dma-iommu-mapping-API-TEST.patch
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20120111/f10945ee/attachment.bin>

#23 lore raw
In reply to Russell King - ARM Linux
Quoted 24 lines · Russell King - ARM Linux
Quoted 1 line · Hiroshi Doyu

Just for DMA mapping test from MPU side, the following one is used.

This patch is buggy.

Quoted 6 lines
(quoted diff) +6 -0
+static void dmaapi_test_map_page(struct device *dev)
+{
+ ? ? struct page *page;
+ ? ? dma_addr_t dma_addr;
+ ? ? void *cpu_addr;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+ ? ? page = alloc_page(GFP_KERNEL);
+ ? ? BUG_ON(!page);
+
+ ? ? dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
+ ? ? BUG_ON(!dma_addr);
+
+ ? ? cpu_addr = kmap(page);
+ ? ? BUG_ON(!cpu_addr);
+ ? ? memset(cpu_addr, 0xa5, PAGE_SIZE);
+ ? ? kunmap(cpu_addr);

The DMA API works like this:

Quoted 3 lines

- The CPU owns the page or buffer and can access it.
- You map the page or buffer.
- The device owns the page or buffer; the CPU must explicitly access it.

I don't understand what "CPU must __explicitly__ access it" means.

Do you mean that CPU must access the mapped buffer when
it __explicitly__ knows that the access does not cause any side effect?

Quoted 11 lines

- You unmap the page or buffer.
- The CPU again owns the page/buffer and can access it.

Please respect the DMA API rules.

So. ?Once dma_map_page() has returned, you must not kmap() or otherwise access the data contained in that page until after you have unmapped it.
_______________________________________________
iommu mailing list
iommu at lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#24 lore raw
Quoted 26 lines · KyongHo Cho
Quoted 24 lines · Russell King - ARM Linux
Quoted 1 line · Hiroshi Doyu

Just for DMA mapping test from MPU side, the following one is used.

This patch is buggy.

Quoted 6 lines
(quoted diff) +6 -0
+static void dmaapi_test_map_page(struct device *dev)
+{
+ ? ? struct page *page;
+ ? ? dma_addr_t dma_addr;
+ ? ? void *cpu_addr;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+ ? ? page = alloc_page(GFP_KERNEL);
+ ? ? BUG_ON(!page);
+
+ ? ? dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_TO_DEVICE);
+ ? ? BUG_ON(!dma_addr);
+
+ ? ? cpu_addr = kmap(page);
+ ? ? BUG_ON(!cpu_addr);
+ ? ? memset(cpu_addr, 0xa5, PAGE_SIZE);
+ ? ? kunmap(cpu_addr);

The DMA API works like this:

Quoted 3 lines
Quoted 3 lines

- The CPU owns the page or buffer and can access it.
- You map the page or buffer.
- The device owns the page or buffer; the CPU must explicitly access it.

Sorry, that 'must' should have been a 'must not'.

Quoted 4 lines

I don't understand what "CPU must __explicitly__ access it" means.

Do you mean that CPU must access the mapped buffer when
it __explicitly__ knows that the access does not cause any side effect?

No - I mean that the program must not attempt to read or write the buffer.