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/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; 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/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))))) 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)) {