From 43555e93e7e1b9d0f04c1477dd38d64bf3273043 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:48 +0000 Subject: [PATCH 1/3] iommu/apple-dart: undo a paging attach that fails part way apple_dart_attach_dev_paging_unguarded() programs each of a device's stream maps in turn. If the second one failed (a TLB invalidate that times out, a refused cold ANE stream), the first kept pointing at the new domain's page tables. Only a managed DART was undone. The IOMMU core does not revert the device whose attach failed, because it expects that device unchanged, and it may free the new domain right after. On a first attach there is no old domain at all. The DART could then walk freed page tables. On failure, put the old domain back on every stream map the attach touched: bypass for the identity domain, the old tables for a paging domain of this driver, and blocked translation otherwise (no old domain, the blocked domain, a DCP domain) or when restoring fails. The error path also took the device's streams back out of the domain's stream maps only when the first DART was managed. For any other DART the streams stayed, so the domain went on flushing them, and freeing it unmapped the firmware root of a locked stream the attach never reached. Take the streams out for every DART, except a locked stream the attach reached: the domain's tables may have been in its firmware root, so apple_dart_domain_free() must still invalidate it before freeing them. A managed DART is still quarantined and its streams removed, as before. Verified with host harnesses that compile the patched functions unchanged against mocks of the hardware helpers, under ASan and UBSan. The undo: 11 cases (first attach, identity, blocked, paging and DCP old domains, a failing restore, locked and managed DARTs, an early failure and a success) leave each stream on the old domain or blocked, never on the failed one, with runtime PM balanced; the unpatched function fails 7 of them. The stream maps: 7 cases (plain, locked reached, locked not reached, locked failing, managed, managed and locked) drop exactly the failed device's streams and keep another device's. The kernel object was not built here and no hardware was used. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/iommu/apple-dart.c | 77 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index bfcdd025bd4c7..3aca2d3c1c763 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -3833,11 +3833,78 @@ static int apple_dart_domain_add_streams(struct apple_dart_domain *domain, true); } +/* + * Undo a paging attach that failed after programming the first @touched + * stream maps. The core leaves a device whose attach failed on @old and + * may free @domain, so none of these streams may keep translating through + * @domain's tables: put @old back where it is this driver's identity or + * paging domain, and block the streams otherwise (a first attach has no + * @old) or if that fails. A managed DART was quarantined by the attach + * itself. Locked streams keep the firmware's roots; see + * apple_dart_attach_drop_streams() for their entries in @domain. + */ +static void apple_dart_attach_paging_undo(struct iommu_domain *domain, + struct apple_dart_master_cfg *cfg, + int touched, struct iommu_domain *old) +{ + struct apple_dart_domain *old_dart = NULL; + int i; + + if (old && old != domain && old->ops == domain->ops) { + old_dart = to_dart_domain(old); + if (!old_dart->finalized || !old_dart->pgtbl_ops || old_dart->dcp) + old_dart = NULL; + } + + for (i = 0; i < touched && i < MAX_DARTS_PER_DEVICE; i++) { + struct apple_dart_stream_map *stream_map = &cfg->stream_maps[i]; + struct apple_dart *dart = stream_map->dart; + + if (!dart || dart->locked || dart->managed_sids) + continue; + if (old && old->type == IOMMU_DOMAIN_IDENTITY && + dart->supports_bypass && !dart->retained_mtp) { + apple_dart_hw_enable_bypass(stream_map); + continue; + } + if (old_dart && !apple_dart_setup_translation(old_dart, stream_map)) + continue; + apple_dart_hw_disable_dma(stream_map); + } +} + +/* + * Take a failed attach's streams back out of @domain's stream maps, so that + * @domain's TLB flushes and apple_dart_domain_free() stop treating them as + * attached. Keep a locked stream the attach reached (the first @touched): + * @domain's tables may have been in its firmware root, so + * apple_dart_domain_free() must still invalidate it before freeing them. A + * locked stream the attach did not reach never used @domain, and freeing + * @domain must not unmap its root. + */ +static void apple_dart_attach_drop_streams(struct apple_dart_domain *domain, + struct apple_dart_master_cfg *cfg, + int touched) +{ + struct apple_dart_stream_map *stream_map; + int i, j; + + for_each_stream_map(i, cfg, stream_map) { + struct apple_dart *dart = stream_map->dart; + + if (dart->locked && !dart->managed_sids && i < touched) + continue; + for (j = 0; j < BITS_TO_LONGS(dart->num_streams); j++) + atomic_long_and(~stream_map->sidmap[j], + &domain->stream_maps[i].sidmap[j]); + } +} + static int apple_dart_attach_dev_paging_unguarded(struct iommu_domain *domain, struct device *dev, struct iommu_domain *old) { - int ret, i; + int ret, i, touched = 0; struct apple_dart_stream_map *stream_map; struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); struct apple_dart_domain *dart_domain = to_dart_domain(domain); @@ -3864,6 +3931,8 @@ static int apple_dart_attach_dev_paging_unguarded(struct iommu_domain *domain, streams_added = true; for_each_stream_map(i, cfg, stream_map) { + /* The stream maps up to this one may now point at @domain. */ + touched = i + 1; if (stream_map->dart->managed_sids) { struct apple_dart *dart = stream_map->dart; unsigned long flags; @@ -3890,8 +3959,10 @@ static int apple_dart_attach_dev_paging_unguarded(struct iommu_domain *domain, } err: - if (ret && streams_added && cfg->stream_maps[0].dart->managed_sids) - apple_dart_mod_streams(dart_domain->stream_maps, cfg->stream_maps, false); + if (ret) + apple_dart_attach_paging_undo(domain, cfg, touched, old); + if (ret && streams_added) + apple_dart_attach_drop_streams(dart_domain, cfg, touched); for_each_stream_map(i, cfg, stream_map) pm_runtime_put(stream_map->dart->dev); return ret; From 0d2018fbbcb24541584da35d7037e2dcf94b44b8 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:48 +0000 Subject: [PATCH 2/3] PCI: apple: keep a function's RID-to-SID slot when it is enabled again pci_reenable_device() calls the host bridge's enable_device() hook again without a disable in between. The PCI core does that at a resume, for drivers that use it, and after a bridge's resources are resized. apple_pcie_enable_device() then allocated a second RID-to-SID slot for the same requester ID, and apple_pcie_disable_device() releases only the first match. Each cycle leaked a slot until the table was full and the next enable failed with -ENOSPC. On a port whose table is indexed by SID, the second enable failed at once with -EBUSY, which fails the resume. Look up the function's own valid entry first and reuse its slot. Managed (J713 radio) admission is unchanged and still refuses a second enable. The slot of another function that shares the SID is not reused. Verified with a host harness that compiles the patched enable and disable hooks unchanged against mocks of the port registers, under ASan and UBSan: 21 enables of one function take one slot and one disable frees it, an indexed port re-enables without -EBUSY, two functions sharing a SID keep one slot each on an allocated table and the second is still refused on an indexed one, and managed admission still returns -EBUSY. The unpatched hooks leak a slot per re-enable and fail at the 17th. The kernel object was not built here and no hardware was used. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/pci/controller/pcie-apple.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index a4cfe96ee37df..2b4f60205e2a5 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -3846,6 +3846,26 @@ static bool apple_pcie_adopted_link_trained_by(struct apple_pcie *pcie, return false; } +/* + * The slot of @port's RID-to-SID table that already maps @rid, or -ENOENT. + * pci_reenable_device() (at a resume, after a bridge's resources are resized) + * enables a function again without disabling it first. Its entry must be + * found, not added again: the disable hook takes back one entry only, and a + * table indexed by SID would refuse the second one with -EBUSY. + */ +static int apple_pcie_rid2sid_find(struct apple_pcie_port *port, u32 rid) +{ + int idx; + + for_each_set_bit(idx, port->sid_map, port->sid_map_sz) { + u32 entry = apple_pcie_port_readl(port, port_rid2sid_offset(port, idx)); + + if ((entry & PORT_RID2SID_VALID) && (entry & 0xffff) == rid) + return idx; + } + return -ENOENT; +} + static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev) { struct apple_pcie *pcie = pci_host_bridge_priv(bridge); @@ -3916,7 +3936,11 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d return -EBUSY; } - if (port->pcie->managed_sids || port->pcie->hw->sid_indexed_rid2sid) { + /* A function enabled again keeps its slot. Managed admission still refuses it. */ + idx = pcie->managed_sids ? -ENOENT : apple_pcie_rid2sid_find(port, rid); + if (idx >= 0) { + dev_dbg(&pdev->dev, "RID%x keeps RID-to-SID slot %d\n", rid, idx); + } else if (port->pcie->managed_sids || port->pcie->hw->sid_indexed_rid2sid) { if (sid >= port->sid_map_sz || (pcie->managed_sids && (!rid || !sid || sid >= 16 || !(pcie->managed_sids & BIT(sid))))) From a448333098a0ef9dad4fe4484736ed6ceaab63f3 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:49 +0000 Subject: [PATCH 3/3] thunderbolt, PCI, iommu: apple: drop TEST labels and J714s diagnostics Several working code paths and log lines were labelled "TEST" from T6050 bring-up: the T6050 path sleep quirk, the 10 ms notification timeout under QUIRK_CIO_MACOS_FRAMES (called a "placeholder"), the PS_PROT readout after a CIO DART wake, and the T6050 PCIe-C sleep order. The labels suggest temporary code. Describe what each one does instead, and print the PS_PROT readout with dev_dbg() so it stays available through dynamic debug. Remove the sv6t9 PCIe-C sleep diagnostics. They ran only on J714s (of_machine_is_compatible("apple,j714s")) and printed, at info level on every sleep, the power state of each function behind the tunnel and the PME_Turn_Off register as macOS reads it. Keep the register read that follows PME_Turn_Off, since it is part of the access order traced from macOS; only the print goes. The J714s/J716c retimer scan skip in retimer.c is left as it is. It is not a diagnostic: it stops a scan that makes the host router firmware time out three times per dock enumeration, so removing it changes behaviour on those machines and needs a hardware run. Not built or run here; the change touches comments, log output, and code that only logs. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/iommu/apple-dart-cio.c | 10 ++--- drivers/pci/controller/pcie-apple-cio.c | 59 ++----------------------- drivers/thunderbolt/path-sleep.c | 7 +-- drivers/thunderbolt/switch.c | 2 +- 4 files changed, 14 insertions(+), 64 deletions(-) diff --git a/drivers/iommu/apple-dart-cio.c b/drivers/iommu/apple-dart-cio.c index 7e25682d7d113..bd90583bc6e0a 100644 --- a/drivers/iommu/apple-dart-cio.c +++ b/drivers/iommu/apple-dart-cio.c @@ -282,7 +282,7 @@ EXPORT_SYMBOL_GPL(apple_dart_cio_off); int apple_dart_cio_on(struct apple_dart_cio *s) { - u32 test_word = 0; + u32 prot_word = 0; int ret; if (!s || !READ_ONCE(s->dart->cio_active)) @@ -342,10 +342,10 @@ int apple_dart_cio_on(struct apple_dart_cio *s) return ret; } s->restored = true; - /* TEST (sv6t7): the guard the wake leaves on, until the DART is removed. */ - regmap_read(s->gapf, APPLE_PS_PROT_SLICE(s->slice), &test_word); - dev_info(s->dart->dev, "TEST sv6t7: CIO DART start: PS_PROT slice %u now %#x\n", - s->slice, test_word); + /* Diagnostic (sv6t7): the guard the wake leaves on, until the DART is removed. */ + regmap_read(s->gapf, APPLE_PS_PROT_SLICE(s->slice), &prot_word); + dev_dbg(s->dart->dev, "CIO DART start: PS_PROT slice %u now %#x\n", + s->slice, prot_word); return 0; } EXPORT_SYMBOL_GPL(apple_dart_cio_on); diff --git a/drivers/pci/controller/pcie-apple-cio.c b/drivers/pci/controller/pcie-apple-cio.c index 717cea2ca891e..386bdd05f0e88 100644 --- a/drivers/pci/controller/pcie-apple-cio.c +++ b/drivers/pci/controller/pcie-apple-cio.c @@ -144,7 +144,7 @@ static int apple_pcie_cio_resume(struct apple_pcie *pcie) } /* - * TEST: T6050 PCIe-C, from a macOS sleep and wake with a dock on this port (run mxt4, the + * T6050 PCIe-C, as traced from a macOS sleep and wake with a dock on this port (run mxt4, the * values as traced). The root port's express capability is at 0x70; the two fabric words and * the Intr2AXI word are written by the sleep with no name known for them. */ @@ -206,52 +206,7 @@ static u32 apple_pcie_cio_t6050_pme_reg(void) } /* - * TEST (sv6t9, J714s): what decides L2 on the tunnel's link. The power state of each - * function below the root port as the port goes down (PME_Turn_Off takes a link to L2 from - * functions in D3hot), and the PME register and link status as macOS reads them: the - * register once at once, then until its send bit clears, then the link once. - */ -struct apple_pcie_cio_test_buf { - char *buf; - size_t size, len; -}; - -static int apple_pcie_cio_test_dstate(struct pci_dev *pdev, void *data) -{ - struct apple_pcie_cio_test_buf *b = data; - - b->len += scnprintf(b->buf + b->len, b->size - b->len, " %s %s", pci_name(pdev), - pci_power_name(pdev->current_state)); - return 0; -} - -static void apple_pcie_cio_test_dstates(struct apple_pcie *pcie) -{ - struct pci_host_bridge *bridge = dev_get_drvdata(pcie->dev); - char text[384]; - struct apple_pcie_cio_test_buf b = { .buf = text, .size = sizeof(text) }; - - if (!of_machine_is_compatible("apple,j714s")) - return; - text[0] = '\0'; - if (bridge && bridge->bus) - pci_walk_bus(bridge->bus, apple_pcie_cio_test_dstate, &b); - dev_info(pcie->dev, "TEST sv6t9: PCIe-C functions below the tunnel as it goes down:%s\n", - b.len ? text : " none"); -} - -static void apple_pcie_cio_test_pme(struct apple_pcie *pcie, u32 reg, u32 first, u32 last, - s64 us, u32 link, int l2) -{ - if (!of_machine_is_compatible("apple,j714s")) - return; - dev_info(pcie->dev, "TEST sv6t9: PCIe-C PME_Turn_Off through +%#x: %#x read at once, %#x after %lld us (send bit %s); LINKSTS %#x, L2 %s (macOS mxt4, mxt5, mxt6b, five sleeps: +0x8c, 0x1 at once, 0x10 after 1.26 ms, LINKSTS 0x99000241 with L2; sv6t5, sv6t7, sv6t8: +0x88, clear in 2 to 4 us, LINKSTS 0x99000001, no L2)\n", - reg, first, last, us, last & PORT_PME_TURN_OFF_SEND ? "still set" : "clear", link, - !l2 ? "reported" : l2 == -ENODATA ? "not looked for" : "not reported"); -} - -/* - * TEST: T6050. macOS takes this port DOWN for a sleep and waits for no link (run mxt4, 6.6 ms + * T6050: macOS takes this port DOWN for a sleep and waits for no link (run mxt4, 6.6 ms * before the ATC2_PCIE power word goes off, the router still awake): the slot's events off; * the port's error interrupts masked and acknowledged; the root port's command and bus * numbers 0; the clock gate off and PME_Turn_Off sent (the send bit clears, the link reads @@ -273,7 +228,6 @@ static int apple_pcie_cio_suspend_t6050(struct apple_pcie *pcie) const char *step; bool idle; u32 stat, link, fabric[2] = {}, i2a = 0, pme_reg = apple_pcie_cio_t6050_pme_reg(); - u32 pme_first = 0, pme_last = 0; /* TEST */ int i, ret, recover, perst, ready, l2 = -ENODATA; s64 pme_us = 0; @@ -286,7 +240,6 @@ static int apple_pcie_cio_suspend_t6050(struct apple_pcie *pcie) return ret; } dev_info(pcie->dev, "CIO PCIe-C stop (T6050 order)\n"); - apple_pcie_cio_test_dstates(pcie); apple_pcie_port_disable_irq(port); port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); for_each_set_bit(i, port->sid_map, port->sid_map_sz) @@ -302,13 +255,12 @@ static int apple_pcie_cio_suspend_t6050(struct apple_pcie *pcie) apple_pcie_port_rmw_set(port, PORT_APPCLK_CGDIS, PORT_APPCLK); sent = ktime_get(); apple_pcie_port_writel(port, PORT_PME_TURN_OFF_SEND | PORT_PME_TURN_OFF_BIT4, pme_reg); - /* TEST: macOS reads the register once at once; printed when the port is down. */ - pme_first = apple_pcie_port_readl(port, pme_reg); + /* macOS reads the register once at once: keep its access order. */ + apple_pcie_port_readl(port, pme_reg); ret = read_poll_timeout(apple_pcie_port_readl, stat, !(stat & PORT_PME_TURN_OFF_SEND), 50, PCIEC_T6050_PME_US, false, port, pme_reg); pme_us = ktime_us_delta(ktime_get(), sent); - pme_last = stat; if (ret) goto unwind; /* macOS reads the link once here and goes on: L2 is reported, not required. */ @@ -348,7 +300,6 @@ static int apple_pcie_cio_suspend_t6050(struct apple_pcie *pcie) ktime_us_delta(ktime_get(), start), pme_us, link, l2 ? "not reported" : "reported", fabric[0], fabric[1], i2a, perst ? "not read back" : "read back", ready ? "still ready" : "not ready"); - apple_pcie_cio_test_pme(pcie, pme_reg, pme_first, pme_last, pme_us, link, l2); return 0; unwind: /* @@ -356,8 +307,6 @@ static int apple_pcie_cio_suspend_t6050(struct apple_pcie *pcie) * PME_Turn_Off, or whose state machine was stopped, trains again only behind a * reset: the port goes down the rest of the way and comes back as after a sleep. */ - apple_pcie_cio_test_pme(pcie, pme_reg, pme_first, pme_last, pme_us, - apple_pcie_port_readl(port, PORT_LINKSTS), l2); dev_err(pcie->dev,"T6050 sleep: PCIe-C step \"%s\" failed (%d) after %lld us, status %#x, link %#x, events %#x: the port is reset and started again from cold, sleep refused\n", step, ret, ktime_us_delta(ktime_get(), start), apple_pcie_port_readl(port, PORT_STATUS), diff --git a/drivers/thunderbolt/path-sleep.c b/drivers/thunderbolt/path-sleep.c index 5d686940c6fee..c3d1c0ee5296a 100644 --- a/drivers/thunderbolt/path-sleep.c +++ b/drivers/thunderbolt/path-sleep.c @@ -31,9 +31,10 @@ int tb_path_sleep(struct tb_path *path) /* Even a partial drain must be replayed on abort. Credits stay allocated. */ path->sleep_valid = true; /* - * TEST: macOS writes no PCIe or USB3 path before a T6050 router's sleep in place - * (run mxt6: only the two display tunnels' paths are cleared); the hops are written - * again after the wake, and after a refused sleep they are found as they were. + * QUIRK_CIO_MACOS_FRAMES: macOS writes no PCIe or USB3 path before a T6050 router's + * sleep in place (run mxt6: only the two display tunnels' paths are cleared); the + * hops are written again after the wake, and after a refused sleep they are found as + * they were. */ if (path->tb->nhi->quirks & QUIRK_CIO_MACOS_FRAMES) return 0; diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c index 99c28d6634a5d..f923a74f73c91 100644 --- a/drivers/thunderbolt/switch.c +++ b/drivers/thunderbolt/switch.c @@ -2640,7 +2640,7 @@ int tb_switch_configure(struct tb_switch *sw) /* Set Notification Timeout to 255 ms for all routers */ sw->config.plug_events_delay = 0xff; - /* TEST: placeholder from run mxt6: macOS enumerates the host and the dock with 10 ms. */ + /* macOS enumerates the host and the dock with a 10 ms timeout (traced in run mxt6). */ if (tb->nhi->quirks & QUIRK_CIO_MACOS_FRAMES) sw->config.plug_events_delay = 0x0a; if (tb_switch_is_usb4(sw)) {