All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Kettenis <kettenis@openbsd.org>
To: u-boot@lists.denx.de
Cc: sjg@chromium.org, bmeng.cn@gmail.com, marex@denx.de,
	Mark Kettenis <kettenis@openbsd.org>
Subject: [PATCH 4/7] iommu: Implement mapping IOMMUs for PCI devices
Date: Tue, 17 Jan 2023 23:04:01 +0100	[thread overview]
Message-ID: <20230117220404.15477-5-kettenis@openbsd.org> (raw)
In-Reply-To: <20230117220404.15477-1-kettenis@openbsd.org>

Systems such as Apple's M1 and M2 SoCs may have separate IOMMUs
for each PCIe root port.  In this case the right IOMMU for a
PCI device behind a particular root port is described by an
"iommu-map" property in the device tree.  Parse this property
and use it to find the right IOMMU device for PCI devices.

Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
---
 drivers/iommu/iommu-uclass.c | 65 ++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/drivers/iommu/iommu-uclass.c b/drivers/iommu/iommu-uclass.c
index f6b1457736..72f123df55 100644
--- a/drivers/iommu/iommu-uclass.c
+++ b/drivers/iommu/iommu-uclass.c
@@ -8,10 +8,71 @@
 #include <common.h>
 #include <dm.h>
 #include <iommu.h>
+#include <malloc.h>
 #include <phys2bus.h>
 #include <asm/io.h>
 
 #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
+
+#if CONFIG_IS_ENABLED(PCI)
+static int dev_pci_iommu_enable(struct udevice *dev)
+{
+	struct udevice *parent = dev->parent;
+	struct udevice *dev_iommu;
+	u32 *iommu_map;
+	u32 iommu_map_mask, length, phandle, rid, rid_base;
+	int i, count, len, ret;
+
+	while (parent) {
+		len = dev_read_size(parent, "iommu-map");
+		if (len > 0)
+			break;
+		parent = parent->parent;
+	}
+
+	if (len <= 0)
+		return 0;
+
+	iommu_map = malloc(len);
+	if (!iommu_map)
+		return -ENOMEM;
+
+	count = len / sizeof(u32);
+	ret = dev_read_u32_array(parent, "iommu-map", iommu_map, count);
+	if (ret < 0) {
+		free(iommu_map);
+		return 0;
+	}
+
+	iommu_map_mask = dev_read_u32_default(parent, "iommu-map-mask", ~0);
+	rid = (dm_pci_get_bdf(dev) >> 8) & iommu_map_mask;
+
+	/* Loop over entries until mapping is found. */
+	for (i = 0; i < count; i += 4) {
+		rid_base = iommu_map[i];
+		phandle = iommu_map[i + 1];
+		length = iommu_map[i + 3];
+
+		if (rid < rid_base || rid >= rid_base + length)
+			continue;
+
+		ret = uclass_get_device_by_phandle_id(UCLASS_IOMMU, phandle,
+						      &dev_iommu);
+		if (ret) {
+			debug("%s: uclass_get_device_by_ofnode failed: %d\n",
+			      __func__, ret);
+			free(iommu_map);
+			return ret;
+		}
+		dev->iommu = dev_iommu;
+		break;
+	}
+
+	free(iommu_map);
+	return 0;
+}
+#endif
+
 int dev_iommu_enable(struct udevice *dev)
 {
 	struct ofnode_phandle_args args;
@@ -39,6 +100,10 @@ int dev_iommu_enable(struct udevice *dev)
 		dev->iommu = dev_iommu;
 	}
 
+	if (CONFIG_IS_ENABLED(PCI) && count < 0 &&
+	    device_is_on_pci_bus(dev))
+		return dev_pci_iommu_enable(dev);
+
 	return 0;
 }
 #endif
-- 
2.39.0


  parent reply	other threads:[~2023-01-17 22:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 22:03 [PATCH 0/7] Apple PCIe/XHCI support Mark Kettenis
2023-01-17 22:03 ` [PATCH 1/7] iommu: Add DMA mapping operations Mark Kettenis
2023-01-18 19:42   ` Simon Glass
2023-01-21 19:28     ` Mark Kettenis
2023-01-17 22:03 ` [PATCH 2/7] iommu: apple: Implement DMA mapping operations for Apple DART Mark Kettenis
2023-01-17 22:04 ` [PATCH 3/7] usb: xhci: Implement DMA mapping Mark Kettenis
2023-01-17 22:51   ` Marek Vasut
2023-01-17 22:04 ` Mark Kettenis [this message]
2023-01-17 22:04 ` [PATCH 5/7] pci: Add Apple PCIe controller driver Mark Kettenis
2023-01-17 22:04 ` [PATCH 6/7] arm: apple: Enable PCIe USB controller Mark Kettenis
2023-01-17 22:04 ` [PATCH 7/7] usb: xhci: Fix root hub descriptor Mark Kettenis
2023-01-17 22:51   ` Marek Vasut

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=20230117220404.15477-5-kettenis@openbsd.org \
    --to=kettenis@openbsd.org \
    --cc=bmeng.cn@gmail.com \
    --cc=marex@denx.de \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.