Skip to contentSkip to conversation

iommu/ipmmu-vmsa: IPMMU SYS-DMAC iova mapping workaround

PATCH/RFC Magnus Damm 2 participants 4 messages Sep 20, 2016 – Sep 27, 2016

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

From: Magnus Damm <[email protected]>

Here's some prototype code that works around the lack of software support for mapping I/O devices to the SYS-DMAC hardware via the DMA Engine framework when using IOMMU.

The code itself is one big layering violation that goes through the DT and unconditionally maps I/O devices using DMACs via the IPMMU device instance into iova space with a 1:1 mapping.

This very short term prototype will for instance automatically make the SCIF serial port function with the IPMMU hardware in case the SYS-DMAC is hooked up to the IPMMU device.

Not to be confused with the more long term solution to allow the DMA Engine framework to map I/O device memory dynamically.

Not-Yet-Signed-off-by Magnus Damm <[email protected]>

---

Applies on top of: renesas-drivers-2016-09-13-v4.8-rc6

 drivers/iommu/ipmmu-vmsa.c |   75 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
drivers/iommu/ipmmu-vmsa.c +75 -0
@@ -19,6 +19,7 @@
 #include <linux/iommu.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/of_iommu.h>
 #include <linux/platform_device.h>
 #include <linux/sizes.h>
@@ -625,6 +626,78 @@ static void ipmmu_domain_free(struct iom
 	kfree(domain);
 }
 
+static void ipmmu_workaround_map(struct iommu_domain *io_domain,
+				 struct device *dma_dev, struct resource *res)
+{
+	phys_addr_t phys_addr;
+
+	dev_info(dma_dev, "map %pr\n", res);
+
+	phys_addr = iommu_iova_to_phys(io_domain, res->start);
+	if (phys_addr)
+		return;
+
+	iommu_map(io_domain, res->start, res->start,
+		  ALIGN(resource_size(res), SZ_4K),
+		  IOMMU_READ | IOMMU_WRITE);
+}
+
+static void ipmmu_workaround_dt(struct iommu_domain *io_domain,
+				struct device *dev,
+				void (*match)(struct iommu_domain *io_domain,
+					      struct device *dma_dev,
+					      struct resource *res))
+{
+	struct device_node *np = NULL;
+	struct of_phandle_args dma_spec;
+	struct resource r;
+	int i, cnt;
+	bool found;
+
+	/* Locate I/O devices using the DMAC and map their registers */
+	while ((np = of_find_all_nodes(np))) {
+		if (!of_find_property(np, "dmas", NULL))
+			continue;
+
+		cnt = of_property_count_strings(np, "dma-names");
+		if (cnt < 0)
+			continue;
+
+		found = false;
+		for (i = 0; i < cnt; i++) {
+			if (of_parse_phandle_with_args(np, "dmas",
+						       "#dma-cells", i,
+						       &dma_spec))
+				continue;
+
+			if (dma_spec.np == dev->of_node)
+				found = true;
+
+			of_node_put(dma_spec.np);
+		}
+
+		if (!found)
+			continue;
+
+		i = 0;
+		while (!of_address_to_resource(np, i, &r)) {
+			match(io_domain, dev, &r);
+			i++;
+		}
+	}
+}
+
+static void ipmmu_workaround(struct iommu_domain *io_domain,
+			     struct device *dev)
+{
+	/* only apply workaround for DMA controllers */
+	if (!strstr(dev_name(dev), "dma-controller"))
+		return;
+
+	dev_info(dev, "Adding iommu workaround to map I/O devices for DMACs\n");
+	ipmmu_workaround_dt(io_domain, dev, ipmmu_workaround_map);
+}
+
 static int ipmmu_attach_device(struct iommu_domain *io_domain,
 			       struct device *dev)
 {
@@ -678,6 +751,8 @@ static int ipmmu_attach_device(struct io
 	if (ret < 0)
 		return ret;
 
+	ipmmu_workaround(io_domain, dev);
+
 	for (i = 0; i < archdata->num_utlbs; ++i)
 		ipmmu_utlb_enable(domain, archdata->utlbs[i]);
 
#1 lore raw

Hi Magnus,

Quoted 10 lines · Magnus Damm

From: Magnus Damm <[email protected]>

Here's some prototype code that works around the lack of software support for mapping I/O devices to the SYS-DMAC hardware via the DMA Engine framework when using IOMMU.

The code itself is one big layering violation that goes through the DT and unconditionally maps I/O devices using DMACs via the IPMMU device instance into iova space with a 1:1 mapping.

Quoted 6 lines

This very short term prototype will for instance automatically make the SCIF serial port function with the IPMMU hardware in case the SYS-DMAC is hooked up to the IPMMU device.

Not to be confused with the more long term solution to allow the DMA Engine framework to map I/O device memory dynamically.

Good news, Vinod queued the dma_{map,unmap}_resource API and rcar-dmac usage of it yesterday \o/.

Quoted 107 lines
Not-Yet-Signed-off-by Magnus Damm <[email protected]>

---

Applies on top of: renesas-drivers-2016-09-13-v4.8-rc6

 drivers/iommu/ipmmu-vmsa.c |   75 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
drivers/iommu/ipmmu-vmsa.c +75 -0
@@ -19,6 +19,7 @@
 #include <linux/iommu.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/of_iommu.h>
 #include <linux/platform_device.h>
 #include <linux/sizes.h>
@@ -625,6 +626,78 @@ static void ipmmu_domain_free(struct iom
 	kfree(domain);
 }
 
+static void ipmmu_workaround_map(struct iommu_domain *io_domain,
+				 struct device *dma_dev, struct resource *res)
+{
+	phys_addr_t phys_addr;
+
+	dev_info(dma_dev, "map %pr\n", res);
+
+	phys_addr = iommu_iova_to_phys(io_domain, res->start);
+	if (phys_addr)
+		return;
+
+	iommu_map(io_domain, res->start, res->start,
+		  ALIGN(resource_size(res), SZ_4K),
+		  IOMMU_READ | IOMMU_WRITE);
+}
+
+static void ipmmu_workaround_dt(struct iommu_domain *io_domain,
+				struct device *dev,
+				void (*match)(struct iommu_domain *io_domain,
+					      struct device *dma_dev,
+					      struct resource *res))
+{
+	struct device_node *np = NULL;
+	struct of_phandle_args dma_spec;
+	struct resource r;
+	int i, cnt;
+	bool found;
+
+	/* Locate I/O devices using the DMAC and map their registers */
+	while ((np = of_find_all_nodes(np))) {
+		if (!of_find_property(np, "dmas", NULL))
+			continue;
+
+		cnt = of_property_count_strings(np, "dma-names");
+		if (cnt < 0)
+			continue;
+
+		found = false;
+		for (i = 0; i < cnt; i++) {
+			if (of_parse_phandle_with_args(np, "dmas",
+						       "#dma-cells", i,
+						       &dma_spec))
+				continue;
+
+			if (dma_spec.np == dev->of_node)
+				found = true;
+
+			of_node_put(dma_spec.np);
+		}
+
+		if (!found)
+			continue;
+
+		i = 0;
+		while (!of_address_to_resource(np, i, &r)) {
+			match(io_domain, dev, &r);
+			i++;
+		}
+	}
+}
+
+static void ipmmu_workaround(struct iommu_domain *io_domain,
+			     struct device *dev)
+{
+	/* only apply workaround for DMA controllers */
+	if (!strstr(dev_name(dev), "dma-controller"))
+		return;
+
+	dev_info(dev, "Adding iommu workaround to map I/O devices for DMACs\n");
+	ipmmu_workaround_dt(io_domain, dev, ipmmu_workaround_map);
+}
+
 static int ipmmu_attach_device(struct iommu_domain *io_domain,
 			       struct device *dev)
 {
@@ -678,6 +751,8 @@ static int ipmmu_attach_device(struct io
 	if (ret < 0)
 		return ret;
 
+	ipmmu_workaround(io_domain, dev);
+
 	for (i = 0; i < archdata->num_utlbs; ++i)
 		ipmmu_utlb_enable(domain, archdata->utlbs[i]);
 
Signature · 2 lines
Regards,
Niklas S�derlund
#2 lore raw

Hi Niklas,

Quoted 17 lines · Niklas Söderlund

Hi Magnus,

Quoted 14 lines · Magnus Damm

From: Magnus Damm <[email protected]>

Here's some prototype code that works around the lack of software support for mapping I/O devices to the SYS-DMAC hardware via the DMA Engine framework when using IOMMU.

The code itself is one big layering violation that goes through the DT and unconditionally maps I/O devices using DMACs via the IPMMU device instance into iova space with a 1:1 mapping.

This very short term prototype will for instance automatically make the SCIF serial port function with the IPMMU hardware in case the SYS-DMAC is hooked up to the IPMMU device.

Quoted 5 lines
Quoted 2 lines

Not to be confused with the more long term solution to allow the DMA Engine framework to map I/O device memory dynamically.

Good news, Vinod queued the dma_{map,unmap}_resource API and rcar-dmac usage of it yesterday \o/.

Excellent. Are the DMA Engine slave devices supposed to work out of the box with the IPMMU then, or is some other component missing?

Cheers,

/ magnus

#3 lore raw

Hi Magnus,

Quoted 21 lines · Magnus Damm

Hi Niklas,

Quoted 17 lines · Niklas S�derlund

Hi Magnus,

Quoted 14 lines · Magnus Damm

From: Magnus Damm <[email protected]>

Here's some prototype code that works around the lack of software support for mapping I/O devices to the SYS-DMAC hardware via the DMA Engine framework when using IOMMU.

The code itself is one big layering violation that goes through the DT and unconditionally maps I/O devices using DMACs via the IPMMU device instance into iova space with a 1:1 mapping.

This very short term prototype will for instance automatically make the SCIF serial port function with the IPMMU hardware in case the SYS-DMAC is hooked up to the IPMMU device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Not to be confused with the more long term solution to allow the DMA Engine framework to map I/O device memory dynamically.

Good news, Vinod queued the dma_{map,unmap}_resource API and rcar-dmac usage of it yesterday \o/.

Excellent. Are the DMA Engine slave devices supposed to work out of the box with the IPMMU then, or is some other component missing?

The DT and shmobile_defconfig patches are missing. If you enable CONFIG_IPMMU_VMSA and wire up DT (see bellow for Koelsch example) then all devices I have tried works (MMC, I2C and serial consoles).

arch/arm/boot/dts/r8a7791.dtsi +31 -1
index 8f0086b..d19bffd 100644
@@ -325,6 +325,21 @@
                power-domains = <&sysc R8A7791_PD_ALWAYS_ON>;
                #dma-cells = <1>;
                dma-channels = <15>;
+               iommus = <&ipmmu_ds 0>,
+                        <&ipmmu_ds 1>,
+                        <&ipmmu_ds 2>,
+                        <&ipmmu_ds 3>,
+                        <&ipmmu_ds 4>,
+                        <&ipmmu_ds 5>,
+                        <&ipmmu_ds 6>,
+                        <&ipmmu_ds 7>,
+                        <&ipmmu_ds 8>,
+                        <&ipmmu_ds 9>,
+                        <&ipmmu_ds 10>,
+                        <&ipmmu_ds 11>,
+                        <&ipmmu_ds 12>,
+                        <&ipmmu_ds 13>,
+                        <&ipmmu_ds 14>;
        };
 
        dmac1: dma-controller@e6720000 {
@@ -356,6 +371,21 @@
                power-domains = <&sysc R8A7791_PD_ALWAYS_ON>;
                #dma-cells = <1>;
                dma-channels = <15>;
+               iommus = <&ipmmu_ds 15>,
+                        <&ipmmu_ds 16>,
+                        <&ipmmu_ds 17>,
+                        <&ipmmu_ds 18>,
+                        <&ipmmu_ds 19>,
+                        <&ipmmu_ds 20>,
+                        <&ipmmu_ds 21>,
+                        <&ipmmu_ds 22>,
+                        <&ipmmu_ds 23>,
+                        <&ipmmu_ds 24>,
+                        <&ipmmu_ds 25>,
+                        <&ipmmu_ds 26>,
+                        <&ipmmu_ds 27>,
+                        <&ipmmu_ds 28>,
+                        <&ipmmu_ds 29>;
        };
 
        audma0: dma-controller@ec700000 {
@@ -1693,7 +1723,7 @@
                interrupts = <GIC_SPI 198 IRQ_TYPE_LEVEL_HIGH>,
                             <GIC_SPI 199 IRQ_TYPE_LEVEL_HIGH>;
                #iommu-cells = <1>;
-               status = "disabled";
+               status = "okay";
        };
 
        ipmmu_mp: mmu@ec680000 {

Also please note that I'm having problems with the next branch of
git://git.kernel.org/pub/scm/linux/kernel/git/vkoul/slave-dma.git and the commit 761ed4a (tty: serial_core: convert uart_close to use tty_port_close) together with a systemd init system. I have to revert to commit in order for the system to boot, but I will comment on that in a separate e-mail.

Signature · 2 lines
Regards,
Niklas S�derlund