From 8869d5e5731885efeb46fb8e59cd670b4ecf6629 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:47 +0000 Subject: [PATCH] PCI: mark devices below a DT external-facing port untrusted and removable The device tree binding says a port marked "external-facing" leads out of the machine, and drivers/pci/of.c sets external_facing on such a port. Since commit 3b96b895127b ("PCI: Detect and trust built-in Thunderbolt chips") the PCI core reads that flag only through arch_pci_dev_is_removable(), which exists for x86 with ACPI. On arm64 it is a stub that returns false, so no device behind an Apple PCIe-C (USB4 tunnel) root port was ever marked untrusted or removable. Every such root port in the Apple device trees has external-facing. Because of that, a device plugged into a Thunderbolt or USB4 port got the same IOMMU treatment as internal hardware: whatever default domain the command line or Kconfig chose (identity with iommu.passthrough=1), lazy TLB flushing, and no bounce buffering of sub-page buffers. On T6050, whose PCIe-C DART is not read as a tunnel DART, the DART driver did not force translation either. Mark a device untrusted and removable when its upstream bridge is external-facing and the flag came from the device tree (the bridge has an of_node). Deeper devices inherit it, as before. The port itself stays internal, as it has been since commit 99b50be9d8ec ("PCI: Treat "external-facing" devices themselves as internal"). ACPI systems are unchanged: their external_facing still goes through arch_pci_dev_is_removable(). With the mark in place the IOMMU core forces a strict DMA domain for these devices, refuses a driver that asks for identity, disables ATS, and dma-iommu bounces buffers that do not fill whole IOVA pages through swiotlb. Verified with a host harness that runs the patched set_pcie_untrusted() and pci_set_removable() on a modelled DT topology (external-facing root port, dock bridge, endpoint): both functions below the port are marked, the port and an internal Wi-Fi function are not, and an ACPI-style external_facing without a DT node is left alone. The unpatched code marks nothing. A script that mirrors the device tree checks found external-facing on every enabled PCIe-C root port in all 138 Apple board trees (80 tunnel references). The kernel was not built here and no hardware was used. Expected cost: strict TLB invalidation, and bounce copies for small, unaligned buffers of tunnelled devices (16 KiB IOVA granule). Dock throughput (network and NVMe) on M1, M2 and M4 Pro, before and after, is needed before this is merged. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/pci/probe.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index d625dd5e778a8..77e7cd6d945f8 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -1733,6 +1733,19 @@ static void set_pcie_cxl(struct pci_dev *dev) } +/* + * The device tree marks a port that leads out of the machine with + * "external-facing", and everything below such a port is external. The + * x86 arch_pci_dev_is_removable() reads only the ACPI property, so check + * the device tree one here. The port itself stays internal. + */ +static bool pci_dev_below_of_external_port(struct pci_dev *dev) +{ + struct pci_dev *parent = pci_upstream_bridge(dev); + + return parent && parent->external_facing && dev_of_node(&parent->dev); +} + static void set_pcie_untrusted(struct pci_dev *dev) { struct pci_dev *parent = pci_upstream_bridge(dev); @@ -1748,7 +1761,8 @@ static void set_pcie_untrusted(struct pci_dev *dev) return; } - if (arch_pci_dev_is_removable(dev)) { + if (arch_pci_dev_is_removable(dev) || + pci_dev_below_of_external_port(dev)) { pci_dbg(dev, "marking as untrusted\n"); dev->untrusted = true; } @@ -1776,7 +1790,8 @@ static void pci_set_removable(struct pci_dev *dev) return; } - if (arch_pci_dev_is_removable(dev)) { + if (arch_pci_dev_is_removable(dev) || + pci_dev_below_of_external_port(dev)) { pci_dbg(dev, "marking as removable\n"); dev_set_removable(&dev->dev, DEVICE_REMOVABLE); }