From 2dc24427ade30b0afef2d7533e2ea5343448dfc0 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 05:00:02 -0500 Subject: [PATCH 1/5] iommu/apple-dart: Let a host give up resuming a DART apple_dart_set_host_resumes() leaves a DART's restore after a deep sleep to the PCIe host that clocks it. A host that claimed one port's DART and then can't claim the next one's has to give the first one back, or nothing restores it: the DART's own resume skips it for good. Add the opposite call. Nothing uses it yet. Signed-off-by: DjDeveloperr --- drivers/iommu/apple-dart.c | 9 +++++++++ include/linux/soc/apple/dart.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 7e1ff6401057c..512b548be0efc 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -8426,6 +8426,15 @@ int apple_dart_set_host_resumes(struct device *dev) } EXPORT_SYMBOL_GPL(apple_dart_set_host_resumes); +void apple_dart_clear_host_resumes(struct device *dev) +{ + struct apple_dart *dart = dev_get_drvdata(dev); + + if (dart) + dart->host_resumes = false; +} +EXPORT_SYMBOL_GPL(apple_dart_clear_host_resumes); + /* The host's port is up again: ungate the DART and restore what it saved. */ int apple_dart_host_resume(struct device *dev) { diff --git a/include/linux/soc/apple/dart.h b/include/linux/soc/apple/dart.h index 04b3429931e0b..5a9f6c8bf89cb 100644 --- a/include/linux/soc/apple/dart.h +++ b/include/linux/soc/apple/dart.h @@ -179,6 +179,7 @@ int apple_dart_fw_remote(struct device *dev, dma_addr_t iova, bool write, phys_a * set the port up and calls apple_dart_host_resume(). */ int apple_dart_set_host_resumes(struct device *dev); +void apple_dart_clear_host_resumes(struct device *dev); int apple_dart_host_resume(struct device *dev); /* The same after a sleep entry that stopped its streams: always the whole reset and restore. */ int apple_dart_host_restart(struct device *dev); @@ -194,6 +195,7 @@ static inline int apple_dart_fw_remote(struct device *dev, dma_addr_t iova, bool return -EOPNOTSUPP; } static inline int apple_dart_set_host_resumes(struct device *dev) { return -EOPNOTSUPP; } +static inline void apple_dart_clear_host_resumes(struct device *dev) {} static inline int apple_dart_host_resume(struct device *dev) { return -EOPNOTSUPP; } static inline int apple_dart_host_restart(struct device *dev) { return -EOPNOTSUPP; } #endif From c69b7079b7d0584b1f96d10ff1f1f4a280121b0d Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 05:00:04 -0500 Subject: [PATCH 2/5] PCI: apple: Share the T8132 port reset values The managed radio's recovery has the T8132 port's reset values inside its own function. A host that sets its ports up itself after a deep sleep writes the same ones: move them out, in the same order and with the same values. No functional change. Signed-off-by: DjDeveloperr --- drivers/pci/controller/pcie-apple-radio.c | 14 +++--------- drivers/pci/controller/pcie-apple.c | 26 +++++++++++++++++++++++ tools/asahi/radio-sleep-power-tests.py | 10 ++++++++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/drivers/pci/controller/pcie-apple-radio.c b/drivers/pci/controller/pcie-apple-radio.c index f735815c0e662..37cbaee8aab7d 100644 --- a/drivers/pci/controller/pcie-apple-radio.c +++ b/drivers/pci/controller/pcie-apple-radio.c @@ -399,22 +399,14 @@ static int apple_radio_seal(struct apple_radio_sleep *s, u64 generation) static int apple_radio_port_restore(struct apple_radio_sleep *s) { - static const struct { - u32 offset, value; - } defaults[] = { - { 0x88, 0x110 }, { 0x100, ~0U }, { 0x148, ~0U }, { 0x210, ~0U }, - { 0x80, 0 }, { 0x84, 0 }, { 0x104, ~0U }, { 0x124, 0x100 }, - { 0x16c, 0 }, { 0x13c, 0x10 }, { 0x800, 0x100100 }, { 0x808, 0x1000ff }, - { 0x82c, 0 }, { 0x130, 0x3000000 }, { 0x140, 0x10 }, { 0x144, 0x253770 }, - { 0x21c, 0 }, { 0x834, 0 }, { 0x83c, 0 }, - }; struct apple_pcie_port *port = s->port; void __iomem *dbi = s->pcie->cfg->win; u32 value; int ret, speed = of_pci_get_max_link_speed(port->np); - for (unsigned int i = 0; i < ARRAY_SIZE(defaults); i++) - apple_pcie_port_writel(port, defaults[i].value, defaults[i].offset); + for (unsigned int i = 0; i < ARRAY_SIZE(apple_pcie_t8132_port_regs); i++) + apple_pcie_port_writel(port, apple_pcie_t8132_port_regs[i].value, + apple_pcie_t8132_port_regs[i].offset); apple_tunable_apply(port->base, s->tables[0]); apple_pcie_port_rmw_set(port, PORT_APPCLK_EN, PORT_APPCLK); writel(readl(port->phy) & ~(PHY_LANE_CFG_REFCLK0REQ | PHY_LANE_CFG_REFCLK1REQ), port->phy); diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 5205407fd02cd..6cafacc4b02cc 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -5299,6 +5299,32 @@ static void apple_pcie_neo_sleep_probe(struct platform_device *pdev, struct appl dev_warn(dev, "no deep sleep: the loader didn't publish what this host needs\n"); } +/* + * T8132 port reset values, in the order m1n1 and macOS write them at bring-up. The managed + * radio (pcie-apple-radio.c) and the hosts that own all of their ports use the same ones. + */ +static const struct apple_pcie_reset_reg apple_pcie_t8132_port_regs[] = { + { 0x088, 0x00000110 }, + { 0x100, 0xffffffff }, + { 0x148, 0xffffffff }, + { 0x210, 0xffffffff }, + { 0x080, 0x00000000 }, + { 0x084, 0x00000000 }, + { 0x104, 0xffffffff }, + { 0x124, 0x00000100 }, + { 0x16c, 0x00000000 }, + { 0x13c, 0x00000010 }, + { 0x800, 0x00100100 }, + { 0x808, 0x001000ff }, + { 0x82c, 0x00000000 }, + { 0x130, 0x03000000 }, + { 0x140, 0x00000010 }, + { 0x144, 0x00253770 }, + { 0x21c, 0x00000000 }, + { 0x834, 0x00000000 }, + { 0x83c, 0x00000000 }, +}; + #include "pcie-apple-radio.c" /* diff --git a/tools/asahi/radio-sleep-power-tests.py b/tools/asahi/radio-sleep-power-tests.py index 5b835d0190496..080b45d1464b5 100644 --- a/tools/asahi/radio-sleep-power-tests.py +++ b/tools/asahi/radio-sleep-power-tests.py @@ -9,6 +9,7 @@ resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) root = Path(__file__).resolve().parents[2] src = (root / "drivers/pci/controller/pcie-apple-radio.c").read_text() +host = (root / "drivers/pci/controller/pcie-apple.c").read_text() def extract(name): @@ -17,7 +18,14 @@ def extract(name): return m.group() -actual = "\n".join( +regs = "\n".join( + re.search(r"^" + head + r".*?^\};", host, re.M | re.S).group() + for head in ( + "struct apple_pcie_reset_reg", + "static const struct apple_pcie_reset_reg apple_pcie_t8132_port_regs", + ) +) +actual = regs + "\n" + "\n".join( extract(n) for n in ( "apple_radio_port_restore", From 40959794ea75b7b3e62fc84954e7237978f6ee37 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 05:00:06 -0500 Subject: [PATCH 3/5] PCI: apple: Set the T8132 ports up again after a deep sleep A T8132 host without the managed radio contract, as the J773g's with its Wi-Fi/Bluetooth and Ethernet ports, has nothing that brings its ports back after a deep sleep. apple-sleep can stop them and knows about a host that owns them, but no host says so. The port DARTs, clocked by their ports, are then read by their own noirq resume without a clock: the J773g takes an SError in the resume of iommu@490000000, 15 ms into the wake, and resets. Take the ports over: save what only their registers hold in the noirq suspend, and in the noirq resume write the reset values, the loader's tunables and link limits, the clocks, the RID-to-SID and MSI routes, then the port's DART and the root port's config space, and train the link, each before anything that depends on it. The link's width and speed limits are the ones the loader left. apple-sleep learns about every port that is back, so that the next sleep stops only those. The host takes the ports over only once every port has what its recovery needs, and gives back what it claimed otherwise. A port that doesn't come back is lost until a restart: its endpoint goes back into reset, its functions are marked disconnected, its config space and interrupt stay closed, and the next sleeps neither save over the last good copy nor touch it. The other ports are not affected. Hosts with apple,managed-sids (J713, J715, J604) return from the new probe first and keep their own recovery. Signed-off-by: DjDeveloperr --- drivers/pci/controller/pcie-apple.c | 455 ++++++++++++++++++- tools/asahi/run-host-tests.py | 1 + tools/asahi/t8132-apcie-sleep-tests.c | 587 +++++++++++++++++++++++++ tools/asahi/t8132-apcie-sleep-tests.py | 57 +++ 4 files changed, 1098 insertions(+), 2 deletions(-) create mode 100644 tools/asahi/t8132-apcie-sleep-tests.c create mode 100755 tools/asahi/t8132-apcie-sleep-tests.py diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 6cafacc4b02cc..14a3621e5ed6f 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -293,6 +293,8 @@ struct hw_info { bool tunneled; bool tunnel_intr2axi; bool t8132_reset; + /* Set up again after a deep sleep, see apple_pcie_t8132_resume_noirq() */ + bool t8132_sleep; bool msimap_by_vector; /* * In suspend-to-idle a tunnel host whose power domain is not always-on @@ -358,6 +360,19 @@ static const struct hw_info t6041_hw = { .s2r_noirq = true, }; +/* T8132: the T602x layout, with ports set up again in the noirq resume after a deep sleep. */ +static const struct hw_info t8132_hw = { + .phy_lane_ctl = 0, + .port_msiaddr = PORT_T602X_MSIADDR, + .port_msiaddr_hi = PORT_T602X_MSIADDR_HI, + .port_refclk = 0, + .port_perst = PORT_T602X_PERST, + .port_rid2sid = PORT_T602X_RID2SID, + .port_msimap = PORT_T602X_MSIMAP, + .max_rid2sid = 512, + .t8132_sleep = true, +}; + /* * T8140 uses the newer port register layout and a SID-indexed RID table. * Keep the PHY and application clock policy established by the bootloader; @@ -686,6 +701,9 @@ struct apple_pcie { bool neo_rc_down; /* Down already when the sleep began: a failed wake left it unprogrammed */ bool neo_rc_was_down; + /* T8132 host without the managed radio: it sets its ports up again after a deep sleep */ + bool t8132_sleep; + bool t8132_sleep_down; const struct hw_info *hw; unsigned long *bitmap; struct list_head ports; @@ -749,6 +767,20 @@ struct apple_pcie_port { bool powered; /* cold train: pwren raised, port not parked yet */ struct device *cold_dart; /* a parked port's DART: gated while the port is parked */ struct pci_saved_state *cold_rp_state; /* a parked port's root port, as it was before the park */ + /* t8132_sleep: what a deep sleep takes from the port, and what sets it up again */ + struct gpio_desc *sleep_perst; + struct gpio_desc *sleep_clkreq; + struct device *sleep_dart; + struct apple_tunable *sleep_tables[4]; + u32 *saved_msimap; + u32 saved_appclk; + u32 saved_msicfg; + u32 saved_msibase; + u32 saved_msiaddr; + u32 saved_msiaddr_hi; + u32 saved_link_mode; + u32 saved_link_width; + u32 saved_lnkcap; }; static bool apple_radio_writer_closed(struct apple_pcie *pcie); @@ -4185,6 +4217,23 @@ static int apple_pcie_init(struct pci_config_window *cfg) * Only a successful owned bootstrap and typed ECAM checks open functions0/1. * Generic config callbacks retain their normal native widths and locking. */ +/* A port that didn't come back from a deep sleep: neither it nor what's behind it answers. */ +static bool apple_pcie_t8132_port_lost(struct apple_pcie *pcie, struct pci_bus *bus, + unsigned int devfn) +{ + struct apple_pcie_port *port; + + for (; !pci_is_root_bus(bus); bus = bus->parent) { + if (!bus->self) + return false; + devfn = bus->self->devfn; + } + list_for_each_entry(port, &pcie->ports, entry) + if (port->idx == PCI_SLOT(devfn)) + return READ_ONCE(port->link_failed); + return false; +} + static void __iomem *apple_pcie_map_bus(struct pci_bus *bus, unsigned int devfn, int where) { @@ -4225,6 +4274,8 @@ static void __iomem *apple_pcie_map_bus(struct pci_bus *bus, } return NULL; } + if (pcie->t8132_sleep && apple_pcie_t8132_port_lost(pcie, bus, devfn)) + return NULL; if (!pcie->hw->root_bus_only) return pci_ecam_map_bus(bus, devfn, where); @@ -5325,6 +5376,401 @@ static const struct apple_pcie_reset_reg apple_pcie_t8132_port_regs[] = { { 0x83c, 0x00000000 }, }; +static void apple_pcie_t8132_put_dart(void *data) +{ + struct apple_pcie_port *port = data; + struct device *dart = port->sleep_dart; + + apple_dart_clear_host_resumes(dart); + device_link_remove(port->pcie->dev, dart); + put_device(dart); + port->sleep_dart = NULL; +} + +/* + * The port's DART sits in the port's unit and is clocked by it: after a deep sleep it reads + * nothing until the port is set up again. It's the host's to resume from then on, after it. + */ +static int apple_pcie_t8132_get_dart(struct apple_pcie_port *port) +{ + struct device *dev = port->pcie->dev; + struct of_phandle_args iommu = {}; + struct platform_device *pdev; + u32 bus; + int ret; + + ret = of_property_read_u32_index(port->np, "bus-range", 0, &bus); + if (ret) + return ret; + ret = of_map_iommu_id(dev->of_node, PCI_DEVID(bus, 0), &iommu); + if (ret || !iommu.np) + return ret ?: -ENODEV; + pdev = of_find_device_by_node(iommu.np); + of_node_put(iommu.np); + if (!pdev) + return -ENODEV; + if (!device_link_add(dev, &pdev->dev, DL_FLAG_STATELESS)) { + ret = -EINVAL; + goto put; + } + ret = apple_dart_set_host_resumes(&pdev->dev); + if (ret) { + device_link_remove(dev, &pdev->dev); + goto put; + } + port->sleep_dart = &pdev->dev; + return devm_add_action_or_reset(dev, apple_pcie_t8132_put_dart, port); + +put: + put_device(&pdev->dev); + return ret; +} + +/* What one port's recovery needs. Nothing outside the port's own data changes yet. */ +static int apple_pcie_t8132_port_prepare(struct platform_device *pdev, + struct apple_pcie_port *port) +{ + static const char *const tables[] = { + "apple,tunable-port", "apple,tunable-rc", + "apple,tunable-rc-gen3", "apple,tunable-rc-gen4", + }; + struct fwnode_handle *fwnode = of_fwnode_handle(port->np); + struct device *dev = &pdev->dev; + struct gpio_desc *gpio; + char name[16]; + + if (!port->phy) + return -ENODEV; + + gpio = port->perst_gpio; + if (!gpio) + gpio = devm_fwnode_gpiod_get(dev, fwnode, "reset", GPIOD_ASIS, "PERST#"); + if (IS_ERR(gpio)) + return PTR_ERR(gpio); + /* The ports come back before the interrupts do. */ + if (gpiod_cansleep(gpio)) + return -EINVAL; + port->sleep_perst = gpio; + + gpio = devm_fwnode_gpiod_get(dev, fwnode, "clkreq", GPIOD_ASIS, "CLKREQ#"); + if (PTR_ERR(gpio) == -ENOENT) + gpio = NULL; + if (IS_ERR(gpio)) + return PTR_ERR(gpio); + if (gpio && gpiod_cansleep(gpio)) + return -EINVAL; + port->sleep_clkreq = gpio; + + port->saved_msimap = devm_kcalloc(dev, port->pcie->nvecs, sizeof(*port->saved_msimap), + GFP_KERNEL); + if (!port->saved_msimap) + return -ENOMEM; + + snprintf(name, sizeof(name), "port%d", port->idx); + for (unsigned int i = 0; i < ARRAY_SIZE(tables); i++) { + struct apple_tunable *table; + struct resource *res; + + /* The loader's, from the ADT's entry for this very port. */ + if (!of_property_present(port->np, tables[i])) + continue; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, i ? "config" : name); + if (!res) + return -ENODEV; + table = devm_apple_tunable_parse(dev, port->np, tables[i], res); + if (IS_ERR(table)) + return PTR_ERR(table); + port->sleep_tables[i] = table; + } + return 0; +} + +static void apple_pcie_t8132_disown(void *data) +{ + struct apple_pcie *pcie = data; + + pcie->t8132_sleep = false; + apple_sleep_apcie_ports_owned(false); +} + +/* + * The host owns its ports across a deep sleep: apple-sleep stops them before it lowers APCIe, + * and they're set up and trained again here. It's all of them or none: a port left out would + * leave its DART to be read by the IOMMU core's resume without a clock, an SError. + */ +static void apple_pcie_t8132_sleep_probe(struct platform_device *pdev, struct apple_pcie *pcie) +{ + struct device *dev = &pdev->dev; + struct apple_pcie_port *port; + void *group; + int ret; + + /* The managed radio has its own recovery, and only that port. */ + if (!pcie->hw->t8132_sleep || pcie->managed_sids || list_empty(&pcie->ports)) + return; + + group = devres_open_group(dev, NULL, GFP_KERNEL); + if (!group) + return; + list_for_each_entry(port, &pcie->ports, entry) { + ret = apple_pcie_t8132_port_prepare(pdev, port); + if (ret) + goto release; + } + /* Claimed only once every port can come back. */ + list_for_each_entry(port, &pcie->ports, entry) { + ret = apple_pcie_t8132_get_dart(port); + if (ret) + goto release; + } + ret = devm_add_action_or_reset(dev, apple_pcie_t8132_disown, pcie); + if (ret) + goto release; + devres_remove_group(dev, group); + pcie->t8132_sleep = true; + apple_sleep_apcie_ports_owned(true); + dev_info(dev, "T8132 multi-port deep sleep recovery registered\n"); + return; + +release: + devres_release_group(dev, group); + dev_warn(dev, "no deep sleep recovery: %pe\n", ERR_PTR(ret)); +} + +/* The root port's config space, reached without the PCI core. */ +static void __iomem *apple_pcie_t8132_dbi(struct apple_pcie_port *port) +{ + return port->pcie->cfg->win + PCIE_ECAM_OFFSET(0, PCI_DEVFN(port->idx, 0), 0); +} + +static int apple_pcie_t8132_suspend_noirq(struct apple_pcie *pcie) +{ + struct apple_pcie_port *port; + int i; + + list_for_each_entry(port, &pcie->ports, entry) { + void __iomem *dbi = apple_pcie_t8132_dbi(port); + + /* Lost in an earlier sleep, its registers with it: what was saved then stays. */ + if (READ_ONCE(port->link_failed)) + continue; + apple_pcie_port_disable_irq(port); + port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); + port->saved_appclk = apple_pcie_port_readl(port, PORT_APPCLK); + port->saved_msicfg = apple_pcie_port_readl(port, PORT_MSICFG); + port->saved_msibase = apple_pcie_port_readl(port, PORT_MSIBASE); + port->saved_msiaddr = apple_pcie_port_readl(port, pcie->hw->port_msiaddr); + port->saved_msiaddr_hi = apple_pcie_port_readl(port, pcie->hw->port_msiaddr_hi); + for (i = 0; i < port->sid_map_sz; i++) + port->saved_rid2sid[i] = + apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); + for (i = 0; i < pcie->nvecs; i++) + port->saved_msimap[i] = + apple_pcie_port_readl(port, pcie->hw->port_msimap + 4 * i); + /* The loader's limits for the link */ + port->saved_link_mode = FIELD_GET(NEO_DBI_PORT_LINK_MODE, + readl(dbi + NEO_DBI_PORT_LINK_CONTROL)); + port->saved_link_width = FIELD_GET(NEO_DBI_LINK_WIDTH, + readl(dbi + NEO_DBI_WIDTH_SPEED_CONTROL)); + port->saved_lnkcap = readl(dbi + NEO_DBI_CAP_LNKCAP); + } + + pcie->t8132_sleep_down = true; + /* As for other hosts: a refused sleep must find the block powered. The commit lowers it. */ + device_set_awake_path(pcie->dev); + return 0; +} + +/* The time the managed radio's link gets (pcie-apple-radio.c) */ +#define T8132_LINK_UP_US 500000 + +/* Reset values, tunables and clocks, until the port's core answers. */ +static int apple_pcie_t8132_port_clock(struct apple_pcie_port *port) +{ + void __iomem *lane = port->phy + PHY_LANE_CFG; + u32 val; + int ret; + + for (unsigned int i = 0; i < ARRAY_SIZE(apple_pcie_t8132_port_regs); i++) + apple_pcie_port_writel(port, apple_pcie_t8132_port_regs[i].value, + apple_pcie_t8132_port_regs[i].offset); + if (port->sleep_tables[0]) + apple_tunable_apply(port->base, port->sleep_tables[0]); + apple_pcie_port_rmw_set(port, PORT_APPCLK_EN, PORT_APPCLK); + + rmw_clear(PHY_LANE_CFG_REFCLK0REQ | PHY_LANE_CFG_REFCLK1REQ, lane); + rmw_set(PHY_LANE_CFG_REFCLK0REQ, lane); + ret = readl_poll_timeout(lane, val, val & PHY_LANE_CFG_REFCLK0ACK, 100, NEO_PHY_POLL_US); + if (ret) + return ret; + rmw_set(PHY_LANE_CFG_REFCLK1REQ, lane); + ret = readl_poll_timeout(lane, val, val & PHY_LANE_CFG_REFCLK1ACK, 100, NEO_PHY_POLL_US); + if (ret) + return ret; + rmw_clear(PHY_LANE_CFG_REFBUF_OFF, lane); + rmw_set(PHY_LANE_CFG_REFCLKEN, lane); + + apple_pcie_port_rmw_set(port, PORT_PERST_OFF, port->pcie->hw->port_perst); + ret = readl_poll_timeout(port->base + PORT_STATUS, val, val & PORT_STATUS_READY, + 100, NEO_RC_POLL_US); + if (ret) + return ret; + /* The clock gating the port had before the sleep. */ + val = apple_pcie_port_readl(port, PORT_APPCLK) & ~PORT_APPCLK_CGDIS; + apple_pcie_port_writel(port, val | (port->saved_appclk & PORT_APPCLK_CGDIS), PORT_APPCLK); + rmw_set(PHY_LANE_CFG_REFCLKCGEN, lane); + return readl_poll_timeout(port->base + PORT_LINKSTS, val, !(val & PORT_LINKSTS_BUSY), + 100, NEO_RC_POLL_US); +} + +/* The root port's tunables and link limits. */ +static void apple_pcie_t8132_port_limits(struct apple_pcie_port *port) +{ + void __iomem *dbi = apple_pcie_t8132_dbi(port); + u32 speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, port->saved_lnkcap); + u32 val; + + rmw_set(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); + for (unsigned int i = 1; i < ARRAY_SIZE(port->sleep_tables); i++) + if (port->sleep_tables[i]) + apple_tunable_apply(dbi, port->sleep_tables[i]); + val = readl(dbi + NEO_DBI_CAP_LNKCAP) & ~(PCI_EXP_LNKCAP_SLS | PCI_EXP_LNKCAP_MLW); + writel(val | (port->saved_lnkcap & (PCI_EXP_LNKCAP_SLS | PCI_EXP_LNKCAP_MLW)), + dbi + NEO_DBI_CAP_LNKCAP); + if (speed) { + val = readl(dbi + NEO_DBI_CAP_LNKCAP2) & ~NEO_DBI_LNKCAP2_SLS; + writel(val | FIELD_PREP(NEO_DBI_LNKCAP2_SLS, GENMASK(speed - 1, 0)), + dbi + NEO_DBI_CAP_LNKCAP2); + writew((readw(dbi + NEO_DBI_CAP_LNKCTL2) & ~PCI_EXP_LNKCTL2_TLS) | speed, + dbi + NEO_DBI_CAP_LNKCTL2); + rmw_set(NEO_DBI_SPEED_CHANGE, dbi + NEO_DBI_WIDTH_SPEED_CONTROL); + } + val = readl(dbi + NEO_DBI_PORT_LINK_CONTROL) & ~NEO_DBI_PORT_LINK_MODE; + writel(val | FIELD_PREP(NEO_DBI_PORT_LINK_MODE, port->saved_link_mode), + dbi + NEO_DBI_PORT_LINK_CONTROL); + val = readl(dbi + NEO_DBI_WIDTH_SPEED_CONTROL) & ~NEO_DBI_LINK_WIDTH; + writel(val | FIELD_PREP(NEO_DBI_LINK_WIDTH, port->saved_link_width), + dbi + NEO_DBI_WIDTH_SPEED_CONTROL); + rmw_clear(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); +} + +static void apple_pcie_t8132_port_routes(struct apple_pcie_port *port) +{ + struct apple_pcie *pcie = port->pcie; + int i; + + apple_pcie_port_writel(port, port->saved_msicfg & ~PORT_MSICFG_EN, PORT_MSICFG); + for (i = 0; i < port->sid_map_sz; i++) + apple_pcie_rid2sid_write(port, i, port->saved_rid2sid[i]); + for (i = 0; i < pcie->nvecs; i++) + apple_pcie_port_writel(port, port->saved_msimap[i], + pcie->hw->port_msimap + 4 * i); + apple_pcie_port_writel(port, port->saved_msibase, PORT_MSIBASE); + apple_pcie_port_writel(port, port->saved_msiaddr, pcie->hw->port_msiaddr); + apple_pcie_port_writel(port, port->saved_msiaddr_hi, pcie->hw->port_msiaddr_hi); + apple_pcie_port_writel(port, port->saved_msicfg, PORT_MSICFG); +} + +static int apple_pcie_t8132_port_restore(struct apple_pcie_port *port) +{ + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port->pcie); + struct pci_dev *rp; + u32 val; + int ret; + + /* These restore the pins' function and direction as well. */ + ret = gpiod_direction_output(port->sleep_perst, 1); + if (!ret && port->sleep_clkreq) + ret = gpiod_direction_input(port->sleep_clkreq); + if (!ret) + ret = apple_pcie_t8132_port_clock(port); + if (ret) + return ret; + apple_pcie_t8132_port_limits(port); + apple_pcie_t8132_port_routes(port); + /* The DART before anything behind the port can reach it. */ + ret = apple_dart_host_resume(port->sleep_dart); + if (ret) + return ret; + rp = pci_get_slot(bridge->bus, PCI_DEVFN(port->idx, 0)); + if (rp) { + pci_restore_state(rp); + pci_dev_put(rp); + } + + /* The reference clock before the endpoints leave reset. */ + msleep(PCIE_T_PVPERL_MS); + ret = gpiod_set_value(port->sleep_perst, 0); + if (ret) + return ret; + /* As for the managed radio: the endpoint is out of reset before its link trains. */ + msleep(PCIE_RESET_CONFIG_WAIT_MS); + apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); + ret = readl_poll_timeout(port->base + PORT_LINKSTS, val, val & PORT_LINKSTS_UP, + 1000, T8132_LINK_UP_US); + if (ret) + return ret; + /* Before configuration requests (PCIe r5.0, 6.6.1) */ + msleep(PCIE_RESET_CONFIG_WAIT_MS); + + apple_pcie_port_writel(port, ~0, PORT_INTSTAT); + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + apple_pcie_port_enable_irq(port); + return 0; +} + +/* + * The endpoint goes back into reset, and nothing reads the port or what's behind it any more: + * their state didn't survive, and a port without its clocks doesn't answer. It stays so, with + * its interrupt off, until a restart. The other ports are not affected. + */ +static void apple_pcie_t8132_port_failed(struct apple_pcie_port *port, int err) +{ + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port->pcie); + struct pci_dev *rp; + + gpiod_direction_output(port->sleep_perst, 1); + apple_pcie_port_rmw_clear(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); + WRITE_ONCE(port->link_failed, true); + rp = pci_get_slot(bridge->bus, PCI_DEVFN(port->idx, 0)); + if (rp) { + if (rp->subordinate) + pci_walk_bus(rp->subordinate, pci_dev_set_disconnected, NULL); + pci_dev_set_disconnected(rp, NULL); + pci_dev_put(rp); + } + dev_err(port->pcie->dev, + "port %d: not back after deep sleep: %d; its functions are out of reach until a restart\n", + port->idx, err); +} + +static int apple_pcie_t8132_resume_noirq(struct apple_pcie *pcie) +{ + struct apple_pcie_port *port; + int ret; + + pcie->t8132_sleep_down = false; + + list_for_each_entry(port, &pcie->ports, entry) { + if (READ_ONCE(port->link_failed)) + continue; + /* A refused sleep took nothing. */ + if (!pm_resume_via_firmware()) { + apple_pcie_port_enable_irq(port); + continue; + } + ret = apple_pcie_t8132_port_restore(port); + if (ret) { + apple_pcie_t8132_port_failed(port, ret); + continue; + } + /* Only a port that's up is apple-sleep's to stop in the next sleep. */ + apple_sleep_apcie_port_up(port->idx); + dev_info(pcie->dev, "port %d: link restored after deep sleep\n", port->idx); + } + return 0; +} + #include "pcie-apple-radio.c" /* @@ -6413,6 +6859,7 @@ static int apple_pcie_probe(struct platform_device *pdev) pci_walk_bus(bridge->bus, apple_pcie_tunnel_keep_d0, NULL); if (pcie->hw->adopt_link) apple_pcie_t6050_sleep_probe(pdev, pcie); + apple_pcie_t8132_sleep_probe(pdev, pcie); apple_radio_register(pdev, pcie); return 0; @@ -7416,6 +7863,8 @@ static int apple_pcie_suspend_noirq(struct device *dev) ret = apple_pcie_t6050_reset_for_sleep(pcie); return ret; } + if (pcie->t8132_sleep && pm_suspend_via_firmware()) + return apple_pcie_t8132_suspend_noirq(pcie); if (!pcie->hw->tunneled) { bool s2r_reinit = apple_pcie_s2r_reinit(pcie) && pm_suspend_via_firmware(); @@ -7922,6 +8371,8 @@ static int apple_pcie_resume_noirq(struct device *dev) return apple_radio_resume_noirq(pcie); if (pcie->neo_sleep_armed) return apple_pcie_neo_resume_noirq(pcie); + if (pcie->t8132_sleep_down) + return apple_pcie_t8132_resume_noirq(pcie); if (pcie->hw->adopt_link) { /* Also after a sleep that was abandoned: the link is down either way */ if (READ_ONCE(pcie->t6050_down)) { @@ -9825,8 +10276,8 @@ static const struct of_device_id apple_pcie_of_match[] = { { .compatible = "apple,t8112-pciec", .data = &t8112_pciec_hw }, { .compatible = "apple,t8103-pciec", .data = &t8103_pciec_hw }, { .compatible = "apple,t6000-pciec", .data = &t8112_pciec_hw }, - /* T8132 currently inherits controller and PHY setup from m1n1. */ - { .compatible = "apple,t8132-pcie", .data = &t602x_hw }, + /* T8132 inherits controller and PHY setup from m1n1, and its ports at boot. */ + { .compatible = "apple,t8132-pcie", .data = &t8132_hw }, { .compatible = "apple,t8152-pcie", .data = &t8152_hw }, { .compatible = "apple,t6041-pcie", .data = &t6041_hw }, { .compatible = "apple,t6020-pcie", .data = &t602x_hw }, diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 0b514c78d4113..8cff13f3a5cae 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -639,6 +639,7 @@ def mutations(flag, names, base, expect=KILL): 't604x-cpu-order-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 't6050-dp-tunnel-dt-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 't6050-combo-join-tests.py': [([], PASS)], + 't8132-apcie-sleep-tests.py': [([], PASS)], 't8132-display-gate-tests.py': [([], PASS)], 't8132-usb4-policy-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 'tb-cio-sleep-tests.py': [(out('--output'), PASS)], diff --git a/tools/asahi/t8132-apcie-sleep-tests.c b/tools/asahi/t8132-apcie-sleep-tests.c new file mode 100644 index 0000000000000..0307f81e34985 --- /dev/null +++ b/tools/asahi/t8132-apcie-sleep-tests.c @@ -0,0 +1,587 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* The T8132 host's own sleep and wake code, on two modelled ports. */ +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef uint16_t u16; +#define __iomem +#define BIT(n) (1U << (n)) +#define GENMASK(h, l) ((~0U << (l)) & (~0U >> (31 - (h)))) +#define FIELD_PREP(m, v) (((u32)(v) << __builtin_ctz(m)) & (m)) +#define FIELD_GET(m, v) (((v) & (m)) >> __builtin_ctz(m)) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define PCI_SLOT(d) ((d) >> 3) +#define PCI_DEVFN(s, f) (((s) << 3) | (f)) +#define PCIE_ECAM_OFFSET(b, d, w) (((b) << 20) | ((d) << 12) | (w)) +#define PCIE_T_PVPERL_MS 100 +#define PCIE_RESET_CONFIG_WAIT_MS 100 +#define PCI_EXP_LNKCAP_SLS 0xf +#define PCI_EXP_LNKCAP_MLW GENMASK(9, 4) +#define PCI_EXP_LNKCTL2_TLS 0xf +#define PORT_LTSSMCTL 0x80 +#define PORT_LTSSMCTL_START BIT(0) +#define PORT_INTSTAT 0x100 +#define PORT_INTMSK 0x104 +#define PORT_MSICFG 0x124 +#define PORT_MSICFG_EN BIT(0) +#define PORT_MSIBASE 0x128 +#define PORT_LINKSTS 0x208 +#define PORT_LINKSTS_UP BIT(0) +#define PORT_LINKSTS_BUSY BIT(2) +#define PORT_APPCLK 0x800 +#define PORT_APPCLK_EN BIT(0) +#define PORT_APPCLK_CGDIS BIT(8) +#define PORT_STATUS 0x804 +#define PORT_STATUS_READY BIT(0) +#define PORT_PERST_OFF BIT(0) +#define PHY_LANE_CFG 0 +#define PHY_LANE_CFG_REFCLK0REQ BIT(0) +#define PHY_LANE_CFG_REFCLK1REQ BIT(1) +#define PHY_LANE_CFG_REFCLK0ACK BIT(2) +#define PHY_LANE_CFG_REFCLK1ACK BIT(3) +#define PHY_LANE_CFG_REFBUF_OFF BIT(4) +#define PHY_LANE_CFG_REFCLKEN (BIT(9) | BIT(10)) +#define PHY_LANE_CFG_REFCLKCGEN (BIT(30) | BIT(31)) +#define NEO_PHY_POLL_US 50000 +#define NEO_RC_POLL_US 250000 +#define NEO_DBI_CAP_LNKCAP 0x7c +#define NEO_DBI_CAP_LNKCAP2 0x9c +#define NEO_DBI_LNKCAP2_SLS GENMASK(6, 1) +#define NEO_DBI_CAP_LNKCTL2 0xa0 +#define NEO_DBI_PORT_LINK_CONTROL 0x710 +#define NEO_DBI_PORT_LINK_MODE GENMASK(21, 16) +#define NEO_DBI_WIDTH_SPEED_CONTROL 0x80c +#define NEO_DBI_LINK_WIDTH GENMASK(12, 8) +#define NEO_DBI_SPEED_CHANGE BIT(17) +#define NEO_DBI_RO_WR 0x8bc +#define NEO_DBI_RO_WR_EN BIT(0) +#define PORTS 2 +#define VECS 4 +#define RIDS 8 +#define RID2SID 0x3000 +#define MSIMAP 0x3800 +#define MSIADDR 0x168 +#define MSIADDR_HI 0x16c + +struct apple_pcie_reset_reg { + u16 offset; + u32 value; +}; + +struct device { + bool awake_path, gated, fails; + int resumed; +}; + +struct gpio_desc { + int port; + bool asserted, stuck; +}; + +struct apple_tunable { + int unused; +}; + +struct pci_bus; +struct pci_dev { + unsigned int devfn; + bool disconnected, restored; + struct pci_bus *subordinate; +}; + +struct pci_bus { + struct pci_bus *parent; + struct pci_dev *self; + int index; +}; + +struct pci_host_bridge { + struct pci_bus *bus; +}; + +struct pci_config_window { + char *win; +}; + +struct hw_info { + u32 port_msiaddr, port_msiaddr_hi, port_msimap, port_perst; +}; + +struct apple_pcie { + struct device *dev; + const struct hw_info *hw; + struct pci_config_window *cfg; + int nvecs; + bool t8132_sleep_down; +}; + +struct apple_pcie_port { + struct apple_pcie *pcie; + char *base, *phy; + int idx, sid_map_sz; + bool link_failed, irq_disabled; + u32 saved_intmask, saved_rid2sid[RIDS]; + struct gpio_desc *sleep_perst, *sleep_clkreq; + struct device *sleep_dart; + struct apple_tunable *sleep_tables[4]; + u32 *saved_msimap; + u32 saved_appclk, saved_msicfg, saved_msibase, saved_msiaddr, saved_msiaddr_hi; + u32 saved_link_mode, saved_link_width, saved_lnkcap; +}; + +static u32 regs[PORTS][0x4000 / 4], phys[PORTS][1]; +static u32 ecam[3 * 0x8000 / 4]; +static unsigned int writes[PORTS], dbi_writes[PORTS], ups[PORTS]; +/* The first poll of this number (from 1) that times out, on this port */ +static int fail_port, fail_poll, polls; +static bool via_firmware; + +static struct device host, darts[PORTS]; +static struct gpio_desc perst[PORTS], clkreq; +static u32 msimaps[PORTS][VECS]; +static struct pci_config_window cfg = { .win = (char *)ecam }; +static const struct hw_info hw = { MSIADDR, MSIADDR_HI, MSIMAP, 0x82c }; +static struct apple_pcie pcie = { .dev = &host, .hw = &hw, .cfg = &cfg, .nvecs = VECS }; +static struct apple_pcie_port ports[PORTS]; +static struct pci_bus root_bus, child_bus[PORTS], far_bus; +static struct pci_dev rps[PORTS], fns[PORTS], bridge_below; +static struct pci_host_bridge host_bridge = { &root_bus }; + +#define list_for_each_entry(p, head, member) \ + for (p = ports; p < ports + PORTS ; p++) +#define pci_host_bridge_from_priv(p) ((void)(p), &host_bridge) +#define dev_err(d, ...) ((void)(d), printf(__VA_ARGS__)) +#define dev_info(d, ...) ((void)(d), printf(__VA_ARGS__)) + +static int port_of(const char *p) +{ + for (int i = 0; i < PORTS; i++) + if (p >= (char *)regs[i] && p < (char *)regs[i] + sizeof(regs[i])) + return i; + return -1; +} + +static int phy_of(const char *p) +{ + for (int i = 0; i < PORTS; i++) + if (p == (char *)phys[i]) + return i; + return -1; +} + +static u32 readl(const void *p) +{ + return *(const u32 *)p; +} + +static u16 readw(const void *p) +{ + return *(const u16 *)p; +} + +static void writel(u32 v, void *p) +{ + int i = port_of(p); + + if (i >= 0) { + u32 off = (char *)p - (char *)regs[i]; + + writes[i]++; + if (off == hw.port_perst && (v & PORT_PERST_OFF)) + regs[i][PORT_STATUS / 4] = PORT_STATUS_READY; + if (off == PORT_LTSSMCTL && (v & PORT_LTSSMCTL_START)) { + /* Nothing behind the port runs before its DART and its routes are back. */ + assert(!darts[i].gated && !perst[i].asserted); + assert(regs[i][RID2SID / 4] == ports[i].saved_rid2sid[0]); + regs[i][PORT_LINKSTS / 4] |= PORT_LINKSTS_UP; + } + if (off == PORT_INTMSK && v != ~0U) + assert(regs[i][PORT_LINKSTS / 4] & PORT_LINKSTS_UP); + } else if (phy_of(p) >= 0) { + v &= ~(PHY_LANE_CFG_REFCLK0ACK | PHY_LANE_CFG_REFCLK1ACK); + v |= (v & (PHY_LANE_CFG_REFCLK0REQ | PHY_LANE_CFG_REFCLK1REQ)) << 2; + writes[phy_of(p)]++; + } else { + size_t off = (char *)p - (char *)ecam; + + assert(off < sizeof(ecam)); + for (i = 0; i < PORTS; i++) + if (off / 0x8000 == (size_t)ports[i].idx) { + /* Only a core that's ready answers. */ + assert(regs[i][PORT_STATUS / 4] & PORT_STATUS_READY); + dbi_writes[i]++; + } + } + *(u32 *)p = v; +} + +static void writew(u16 v, void *p) +{ + *(u16 *)p = v; +} + +static void rmw_set(u32 set, void *p) +{ + writel(readl(p) | set, p); +} + +static void rmw_clear(u32 clr, void *p) +{ + writel(readl(p) & ~clr, p); +} + +static bool poll_fails(const char *p) +{ + int i = port_of(p) >= 0 ? port_of(p) : phy_of(p); + + if (i != fail_port) + return false; + polls++; + return polls == fail_poll; +} + +#define readl_poll_timeout(p, v, cond, delay, us) \ + ({ \ + char *_p = (char *)(p); \ + (void)(delay); \ + (void)(us); \ + (v) = readl(_p); \ + poll_fails(_p) ? -ETIMEDOUT : (cond) ? 0 : -ETIMEDOUT; \ + }) + +static u32 apple_pcie_port_readl(struct apple_pcie_port *port, u32 off) +{ + return readl(port->base + off); +} + +static void apple_pcie_port_writel(struct apple_pcie_port *port, u32 v, u32 off) +{ + writel(v, port->base + off); +} + +static void apple_pcie_port_rmw_set(struct apple_pcie_port *port, u32 set, u32 off) +{ + rmw_set(set, port->base + off); +} + +static void apple_pcie_port_rmw_clear(struct apple_pcie_port *port, u32 clr, u32 off) +{ + rmw_clear(clr, port->base + off); +} + +static u32 port_rid2sid_offset(struct apple_pcie_port *port, int i) +{ + (void)port; + return RID2SID + 4 * i; +} + +static u32 apple_pcie_rid2sid_write(struct apple_pcie_port *port, int i, u32 v) +{ + apple_pcie_port_writel(port, v, port_rid2sid_offset(port, i)); + return v; +} + +static void apple_pcie_port_disable_irq(struct apple_pcie_port *port) +{ + port->irq_disabled = true; +} + +static void apple_pcie_port_enable_irq(struct apple_pcie_port *port) +{ + port->irq_disabled = false; +} + +static int gpiod_direction_output(struct gpio_desc *g, int v) +{ + if (g->stuck) + return -EIO; + g->asserted = v; + if (v) + regs[g->port][PORT_LINKSTS / 4] &= ~PORT_LINKSTS_UP; + return 0; +} + +static int gpiod_set_value(struct gpio_desc *g, int v) +{ + return gpiod_direction_output(g, v); +} + +static int gpiod_direction_input(struct gpio_desc *g) +{ + (void)g; + return 0; +} + +static int apple_dart_host_resume(struct device *dart) +{ + assert(dart->gated); + if (dart->fails) + return -EIO; + dart->gated = false; + dart->resumed++; + return 0; +} + +static void apple_tunable_apply(void *base, struct apple_tunable *t) +{ + (void)base; + (void)t; +} + +static bool pm_resume_via_firmware(void) +{ + return via_firmware; +} + +static void device_set_awake_path(struct device *dev) +{ + dev->awake_path = true; +} + +static void apple_sleep_apcie_port_up(unsigned int idx) +{ + for (int i = 0; i < PORTS; i++) + if (ports[i].idx == (int)idx) + ups[i]++; +} + +static struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn) +{ + assert(bus == &root_bus); + for (int i = 0; i < PORTS; i++) + if (rps[i].devfn == devfn) + return &rps[i]; + return NULL; +} + +static void pci_dev_put(struct pci_dev *dev) +{ + (void)dev; +} + +static void pci_restore_state(struct pci_dev *dev) +{ + dev->restored = true; +} + +static int pci_dev_set_disconnected(struct pci_dev *dev, void *data) +{ + (void)data; + dev->disconnected = true; + return 0; +} + +static void pci_walk_bus(struct pci_bus *bus, int (*fn)(struct pci_dev *, void *), void *data) +{ + fn(&fns[bus->index], data); +} + +static bool pci_is_root_bus(struct pci_bus *bus) +{ + return !bus->parent; +} + +static void msleep(unsigned int ms) +{ + (void)ms; +} + +/* ACTUAL_FUNCTIONS */ + +static u32 *ecam_reg(int i, u32 off) +{ + return &ecam[(ports[i].idx * 0x8000 + off) / 4]; +} + +static void boot(void) +{ + memset(regs, 0, sizeof(regs)); + memset(phys, 0, sizeof(phys)); + memset(ecam, 0, sizeof(ecam)); + memset(ports, 0, sizeof(ports)); + memset(darts, 0, sizeof(darts)); + memset(perst, 0, sizeof(perst)); + memset(rps, 0, sizeof(rps)); + memset(fns, 0, sizeof(fns)); + memset(ups, 0, sizeof(ups)); + fail_port = -1; + for (int i = 0; i < PORTS; i++) { + struct apple_pcie_port *p = &ports[i]; + + perst[i].port = i; + p->pcie = &pcie; + p->base = (char *)regs[i]; + p->phy = (char *)phys[i]; + p->idx = 2 * i; + p->sid_map_sz = RIDS; + p->sleep_perst = &perst[i]; + p->sleep_clkreq = i ? NULL : &clkreq; + p->sleep_dart = &darts[i]; + p->saved_msimap = msimaps[i]; + rps[i].devfn = PCI_DEVFN(p->idx, 0); + child_bus[i] = (struct pci_bus){ &root_bus, &rps[i], i }; + rps[i].subordinate = &child_bus[i]; + regs[i][PORT_INTMSK / 4] = 0xfb512f0 + i; + regs[i][PORT_APPCLK / 4] = PORT_APPCLK_EN | (i ? PORT_APPCLK_CGDIS : 0); + regs[i][PORT_MSICFG / 4] = PORT_MSICFG_EN | 0x50; + regs[i][PORT_MSIBASE / 4] = 0x40 + i; + regs[i][MSIADDR / 4] = 0xfffff000; + regs[i][MSIADDR_HI / 4] = 0x7; + regs[i][PORT_LINKSTS / 4] = PORT_LINKSTS_UP; + regs[i][PORT_STATUS / 4] = PORT_STATUS_READY; + for (int j = 0; j < RIDS; j++) + regs[i][(RID2SID + 4 * j) / 4] = 0x80000000 + 0x100 * i + j; + for (int j = 0; j < VECS; j++) + regs[i][(MSIMAP + 4 * j) / 4] = 0x80000010 + 0x100 * i + j; + *ecam_reg(i, NEO_DBI_CAP_LNKCAP) = 0x10 | (i ? 1 : 2); + *ecam_reg(i, NEO_DBI_PORT_LINK_CONTROL) = FIELD_PREP(NEO_DBI_PORT_LINK_MODE, 1); + *ecam_reg(i, NEO_DBI_WIDTH_SPEED_CONTROL) = FIELD_PREP(NEO_DBI_LINK_WIDTH, 1); + } +} + +/* What the deep sleep takes: every register, the link, and what the DARTs can answer. */ +static void lose_power(void) +{ + memset(regs, 0, sizeof(regs)); + memset(phys, 0, sizeof(phys)); + memset(ecam, 0, sizeof(ecam)); + for (int i = 0; i < PORTS; i++) { + darts[i].gated = true; + rps[i].restored = false; + *ecam_reg(i, NEO_DBI_CAP_LNKCAP) = 0x44; + } + polls = 0; + memset(writes, 0, sizeof(writes)); + memset(dbi_writes, 0, sizeof(dbi_writes)); + via_firmware = true; +} + +static void check_up(int i) +{ + struct apple_pcie_port *p = &ports[i]; + + assert(!p->link_failed && !p->irq_disabled && !perst[i].asserted); + assert(darts[i].resumed && !darts[i].gated && rps[i].restored); + assert(!rps[i].disconnected && !fns[i].disconnected); + assert(regs[i][PORT_LINKSTS / 4] & PORT_LINKSTS_UP); + assert(regs[i][PORT_INTMSK / 4] == 0xfb512f0u + i); + assert(regs[i][PORT_MSICFG / 4] == (PORT_MSICFG_EN | 0x50)); + assert(regs[i][PORT_MSIBASE / 4] == 0x40u + i); + assert(regs[i][MSIADDR / 4] == 0xfffff000 && regs[i][MSIADDR_HI / 4] == 0x7); + for (int j = 0; j < RIDS; j++) + assert(regs[i][(RID2SID + 4 * j) / 4] == 0x80000000u + 0x100 * i + j); + for (int j = 0; j < VECS; j++) + assert(regs[i][(MSIMAP + 4 * j) / 4] == 0x80000010u + 0x100 * i + j); + assert(!!(regs[i][PORT_APPCLK / 4] & PORT_APPCLK_CGDIS) == !!i); + /* The loader's width and speed, and not the reset values 4 and x4 */ + assert(*ecam_reg(i, NEO_DBI_CAP_LNKCAP) == (0x10u | (i ? 1 : 2))); + assert(*ecam_reg(i, NEO_DBI_CAP_LNKCAP2) == (i ? 0x2u : 0x6u)); + assert(FIELD_GET(NEO_DBI_PORT_LINK_MODE, *ecam_reg(i, NEO_DBI_PORT_LINK_CONTROL)) == 1); + assert(FIELD_GET(NEO_DBI_LINK_WIDTH, *ecam_reg(i, NEO_DBI_WIDTH_SPEED_CONTROL)) == 1); + assert(!(*ecam_reg(i, NEO_DBI_RO_WR) & NEO_DBI_RO_WR_EN)); + assert(!apple_pcie_t8132_port_lost(&pcie, &root_bus, rps[i].devfn)); + assert(!apple_pcie_t8132_port_lost(&pcie, &child_bus[i], 0)); +} + +static void check_lost(int i) +{ + assert(ports[i].link_failed && ports[i].irq_disabled); + assert(rps[i].disconnected && fns[i].disconnected); + assert(!(regs[i][PORT_LTSSMCTL / 4] & PORT_LTSSMCTL_START)); + assert(!(regs[i][PORT_LINKSTS / 4] & PORT_LINKSTS_UP)); + assert(apple_pcie_t8132_port_lost(&pcie, &root_bus, rps[i].devfn)); + assert(apple_pcie_t8132_port_lost(&pcie, &child_bus[i], PCI_DEVFN(3, 1))); + bridge_below.devfn = PCI_DEVFN(5, 0); + far_bus = (struct pci_bus){ &child_bus[i], &bridge_below, 0 }; + assert(apple_pcie_t8132_port_lost(&pcie, &far_bus, 0)); +} + +static void sleep_and_wake(void) +{ + assert(!apple_pcie_t8132_suspend_noirq(&pcie)); + assert(pcie.t8132_sleep_down && host.awake_path); + lose_power(); + assert(!apple_pcie_t8132_resume_noirq(&pcie)); + assert(!pcie.t8132_sleep_down); +} + +int main(void) +{ + unsigned int before; + + /* Three good cycles */ + boot(); + for (unsigned int n = 1; n <= 3; n++) { + sleep_and_wake(); + for (int i = 0; i < PORTS; i++) { + check_up(i); + assert(ups[i] == n); + } + } + + /* A refused sleep: nothing was lost and nothing is written */ + boot(); + assert(!apple_pcie_t8132_suspend_noirq(&pcie)); + memset(writes, 0, sizeof(writes)); + via_firmware = false; + assert(!apple_pcie_t8132_resume_noirq(&pcie)); + for (int i = 0; i < PORTS; i++) + assert(!ports[i].irq_disabled && !writes[i] && !ups[i] && !darts[i].resumed); + + /* + * Either port failing at each wait: both clock acks, ready, not busy, link up. The other + * one comes back, and the DART is only restored under a port that's clocked. + */ + for (int bad = 0; bad < PORTS; bad++) + for (int poll = 1; poll <= 5; poll++) { + u32 rid2sid[RIDS], msimap[VECS]; + + boot(); + fail_port = bad; + fail_poll = poll; + sleep_and_wake(); + check_lost(bad); + check_up(!bad); + assert(perst[bad].asserted && !ups[bad] && ups[!bad] == 1); + assert(darts[bad].resumed == (poll == 5)); + assert(!!dbi_writes[bad] == (poll == 5)); + + /* The next sleep keeps the good copy and leaves the lost port alone. */ + memcpy(rid2sid, ports[bad].saved_rid2sid, sizeof(rid2sid)); + memcpy(msimap, ports[bad].saved_msimap, sizeof(msimap)); + before = ports[bad].saved_intmask; + fail_port = -1; + sleep_and_wake(); + check_lost(bad); + check_up(!bad); + assert(!writes[bad] && !dbi_writes[bad] && !ups[bad] && ups[!bad] == 2); + assert(!memcmp(rid2sid, ports[bad].saved_rid2sid, sizeof(rid2sid))); + assert(!memcmp(msimap, ports[bad].saved_msimap, sizeof(msimap))); + assert(before == 0xfb512f0u + bad && before == ports[bad].saved_intmask); + } + + /* A DART that can't be restored: no link is trained over it. */ + boot(); + darts[0].fails = true; + sleep_and_wake(); + check_lost(0); + check_up(1); + assert(perst[0].asserted && darts[0].gated); + + /* PERST# that can't be driven */ + boot(); + perst[1].stuck = true; + sleep_and_wake(); + check_lost(1); + check_up(0); + + puts("PASS: T8132 APCIe deep sleep, failed restores and the sleep after them"); + return 0; +} diff --git a/tools/asahi/t8132-apcie-sleep-tests.py b/tools/asahi/t8132-apcie-sleep-tests.py new file mode 100755 index 0000000000000..ff4cba5cc29dc --- /dev/null +++ b/tools/asahi/t8132-apcie-sleep-tests.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Build and run the T8132 APCIe ports' deep-sleep recovery against a register model.""" +from pathlib import Path +import re +import resource +import subprocess +import sys +import tempfile + +# python3 -I, as run-host-tests.py runs it, leaves this directory off sys.path. +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from sleep_test_utils import function # noqa: E402 + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +root = Path(__file__).resolve().parents[2] +src = (root / "drivers/pci/controller/pcie-apple.c").read_text() + +table = re.search( + r"^static const struct apple_pcie_reset_reg apple_pcie_t8132_port_regs.*?^\};", + src, + re.M | re.S, +) +assert table, "no T8132 port reset table" +define = re.search(r"^#define T8132_LINK_UP_US.*$", src, re.M) +assert define, "no link-up timeout" +actual = [table.group(), define.group()] + [ + function(src, name) + for name in ( + "apple_pcie_t8132_port_lost", + "apple_pcie_t8132_dbi", + "apple_pcie_t8132_suspend_noirq", + "apple_pcie_t8132_port_clock", + "apple_pcie_t8132_port_limits", + "apple_pcie_t8132_port_routes", + "apple_pcie_t8132_port_restore", + "apple_pcie_t8132_port_failed", + "apple_pcie_t8132_resume_noirq", + ) +] + +# The wake's hook: in the host's resume, outside the adopted-link hosts' block. +resume = function(src, "apple_pcie_resume_noirq") +hook = resume.index("\tif (pcie->t8132_sleep_down)\n\t\treturn apple_pcie_t8132_resume_noirq(pcie);") +assert hook < resume.index("\tif (pcie->hw->adopt_link) {"), "resume hook out of reach" +suspend = function(src, "apple_pcie_suspend_noirq") +assert "\n\tif (pcie->t8132_sleep && pm_suspend_via_firmware())\n" in suspend + +fixture = Path(__file__).with_suffix(".c").read_text() +with tempfile.TemporaryDirectory(prefix="t8132-apcie-") as tmp: + c, exe = Path(tmp) / "test.c", Path(tmp) / "test" + c.write_text(fixture.replace("/* ACTUAL_FUNCTIONS */", "\n".join(actual))) + subprocess.run( + ["cc", "-std=gnu11", "-Wall", "-Werror", str(c), "-o", str(exe)], + check=True, + ) + subprocess.run([str(exe)], check=True) From 215ffcc80159fb29eb60c0cc3ee4783a8256d6e5 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 05:46:50 -0500 Subject: [PATCH 4/5] soc: apple: sleep: Refuse a T8132 deep sleep without a PCIe port recovery A T8132 wake leaves the APCIe ports to their host. A board without the managed radio contract, the J773g, has a host that says whether it sets them up again, but nothing asks: when its probe failed, its device tree leaves out what the recovery needs, or no driver is bound, a deep sleep still goes through. The wake then reads the port DARTs without a clock and the machine resets with its unsaved work. Refuse it in .begin, before any device suspends. Suspend-to-idle takes another path and is not affected. Deep sleep shouldn't be the default there either. The host probes after apple-sleep on the J773g (2.95 s and 0.05 s), so start with s2idle as the default and make it deep sleep when the host registers. A default that was chosen with mem_sleep_default=, or that another missing part took away, stays as it is. With the recovery registered, and on every host with apple,managed-sids (J713, J715, J604, J623, J624), deep sleep and its default are as before. The other SoCs don't set the new field. Signed-off-by: DjDeveloperr --- drivers/soc/apple/sleep.c | 50 +++ tools/asahi/run-host-tests.py | 1 + tools/asahi/t8132-sleep-refuse-tests.c | 461 ++++++++++++++++++++++++ tools/asahi/t8132-sleep-refuse-tests.py | 61 ++++ 4 files changed, 573 insertions(+) create mode 100644 tools/asahi/t8132-sleep-refuse-tests.c create mode 100755 tools/asahi/t8132-sleep-refuse-tests.py diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 0a5a506d98db5..0c803ff552690 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -657,6 +657,11 @@ struct apple_sleep_soc { * apple,display-power: without them its firmware keeps running. */ bool display_power_dt; + /* + * The wake leaves the APCIe ports to their host: without the managed radio contract, one + * that has said it sets them up again, see sleep_apcie_ports_orphaned(). + */ + bool apcie_ports_host; /* * apple_sleep.dispext: the external displays (index 1 and up) are lowered only with it on, * and a missing phandle for one is no error. Each one's PS write-disable bit (offset, bit; @@ -1026,6 +1031,8 @@ struct apple_sleep { bool apcie_port_stopped; /* Ports no host sets up again after a real sleep: no managed radio contract */ bool apcie_ports_unowned; + /* s2idle is the default only for want of the ports' host */ + bool apcie_default_held; struct { void __iomem *port; void __iomem *phy; @@ -1429,10 +1436,43 @@ EXPORT_SYMBOL_GPL(apple_sleep_debug_link_cut_peek); /* Kept out of struct apple_sleep so pcie-apple and apple-sleep may probe in either order. */ static bool apcie_ports_host_owned; +static DEFINE_MUTEX(apcie_ports_lock); + +/* + * No host sets the ports up again after a real sleep: its driver isn't there, or the probe + * couldn't get what that takes. The DARTs in the ports' units then have no clock when the + * resume reads them: an SError, and the machine resets. + */ +static bool sleep_apcie_ports_orphaned(const struct apple_sleep *s) +{ + return s->soc->apcie_ports_host && s->apcie_ports_unowned && + !READ_ONCE(apcie_ports_host_owned); +} + +/* Deep sleep is the default only with that host, unless mem_sleep_default= asks for it. */ +static void sleep_apcie_ports_default(struct apple_sleep *s) +{ + bool orphaned = sleep_apcie_ports_orphaned(s); + + lockdep_assert_held(&apcie_ports_lock); + if (orphaned && mem_sleep_current == PM_SUSPEND_MEM && + mem_sleep_default != PM_SUSPEND_MEM) { + mem_sleep_current = PM_SUSPEND_TO_IDLE; + s->apcie_default_held = true; + dev_info(s->dev, "s2idle is the default until the PCIe ports have a host that sets them up again after a deep sleep\n"); + } else if (!orphaned && s->apcie_default_held) { + mem_sleep_current = PM_SUSPEND_MEM; + s->apcie_default_held = false; + dev_info(s->dev, "deep sleep is the default: the PCIe ports have their host\n"); + } +} void apple_sleep_apcie_ports_owned(bool owned) { + guard(mutex)(&apcie_ports_lock); WRITE_ONCE(apcie_ports_host_owned, owned); + if (apple_sleep) + sleep_apcie_ports_default(apple_sleep); } EXPORT_SYMBOL_GPL(apple_sleep_apcie_ports_owned); @@ -7209,6 +7249,12 @@ static int apple_sleep_begin(suspend_state_t state) if (ret) return ret; + /* User space is frozen, so the host can't go from here on, and no device has suspended. */ + if (sleep_apcie_ports_orphaned(apple_sleep)) { + dev_err(apple_sleep->dev, "refused: no host sets the PCIe ports up again after a deep sleep\n"); + return -EBUSY; + } + apple_sleep->dry_run = apple_sleep->untested && !READ_ONCE(sleep_untested_commit); if (apple_sleep->soc->ops->begin) { @@ -8940,6 +8986,9 @@ static int apple_sleep_probe(struct platform_device *pdev) mem_sleep_current = PM_SUSPEND_TO_IDLE; dev_warn(dev, "deep sleep needs the parts named above: s2idle stays the default\n"); } + /* Last: it only ever gives back what it took. */ + scoped_guard(mutex, &apcie_ports_lock) + sleep_apcie_ports_default(s); dev_info(dev, "deep sleep %s: boot object resumes %llu\n", mem_sleep_current == PM_SUSPEND_MEM ? "is the default" : @@ -9306,6 +9355,7 @@ static const struct apple_sleep_soc t8132_sleep_soc = { .ndisp = 3, .lower_unbound_displays = true, .display_power_dt = true, + .apcie_ports_host = true, /* ADT dart-dcp, dart-dcpext0 and dart-dcpext1 clock-protection slices */ .disp_prot_slice = { 42, 5, 30 }, .display_order = { 1, 2, 0 }, diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 8cff13f3a5cae..b5d47c7a8ae93 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -641,6 +641,7 @@ def mutations(flag, names, base, expect=KILL): 't6050-combo-join-tests.py': [([], PASS)], 't8132-apcie-sleep-tests.py': [([], PASS)], 't8132-display-gate-tests.py': [([], PASS)], + 't8132-sleep-refuse-tests.py': [([], PASS)], 't8132-usb4-policy-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 'tb-cio-sleep-tests.py': [(out('--output'), PASS)], 'tb-dock-pm-tests.py': [(out('--output'), PASS)], diff --git a/tools/asahi/t8132-sleep-refuse-tests.c b/tools/asahi/t8132-sleep-refuse-tests.c new file mode 100644 index 0000000000000..a1173018c1926 --- /dev/null +++ b/tools/asahi/t8132-sleep-refuse-tests.c @@ -0,0 +1,461 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* A T8132 deep sleep without a host that sets the PCIe ports up again: refused, not the default. */ +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef int suspend_state_t; +#define PM_SUSPEND_ON 0 +#define PM_SUSPEND_TO_IDLE 1 +#define PM_SUSPEND_MEM 3 +#define PM_SUSPEND_MAX 4 +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define __iomem +#define GFP_KERNEL 0 +#define PORTS 2 + +struct device { + int unused; +}; + +struct platform_device { + struct device dev; +}; + +struct apple_sleep; +struct apple_sleep_ops { + int (*begin)(struct apple_sleep *s); + void (*finish)(struct apple_sleep *s); + void (*pmu_wake)(struct apple_sleep *s); +}; + +struct apple_sleep_soc { + const struct apple_sleep_ops *ops; + bool apcie_ports_host, wdt_wd1, no_wd2; +}; + +struct apple_sleep { + const struct apple_sleep_soc *soc; + struct device *dev; + bool untested, dry_run, apcie_ports_unowned, apcie_default_held; + bool apcie_port_stopped, usb_down_ran, smc_woke_alone, wd1_saved; + u32 wd1_saved_ctl, wd1_saved_bite; + char *wdt; +}; + +struct hw_info { + bool t8132_sleep; +}; + +struct apple_pcie_port { + int unused; +}; + +struct apple_pcie { + const struct hw_info *hw; + bool managed_sids, t8132_sleep; + int ports; +}; + +static suspend_state_t mem_sleep_current, mem_sleep_default; +static struct apple_sleep *apple_sleep; +static bool apcie_ports_host_owned; +static int apcie_ports_lock; +static bool sleep_debug_link_cut, sleep_deep_requested, sleep_untested_commit; +static int sleep_lid_closed, sleep_radio_woke; +static int sleep_cpus_awake; +static const int *cpu_online_mask; +static int apple_sleep_wdt_lock; +static bool apple_sleep_wd2_owned, via_firmware, ans_open; +static int armed, disarmed, pins_saved, ops_begun, ops_ended, errors; +static struct apple_sleep the_sleep; + +#define WDT_WD1_CTL 0 +#define WDT_WD1_COUNT 4 +#define WDT_WD1_BITE 8 +#define WDT_SUSPEND_SECS 120 + +static int locked; + +static void unlock(int *unused) +{ + locked--; +} + +#define guard(type) int held __attribute__((cleanup(unlock), unused)) = locked++; (void) +#define lockdep_assert_held(l) ((void)(l), assert(locked == 1)) +#define spin_lock_irqsave(l, f) ((void)(l), (f) = 0) +#define spin_unlock_irqrestore(l, f) ((void)(l), (void)(f)) +#define cpumask_copy(d, s) ((void)(d), (void)(s)) +#define dev_info(d, ...) ((void)(d), printf(__VA_ARGS__)) +#define dev_warn(d, ...) ((void)(d), printf(__VA_ARGS__)) +#define dev_err(d, ...) ((void)(d), errors++, printf(__VA_ARGS__)) +#define ERR_PTR(e) ((void *)(long)(e)) + +static u32 readl(const void *p) +{ + (void)p; + return 0; +} + +static void writel(u32 v, void *p) +{ + (void)v; + (void)p; +} + +static int apple_sleep_profile_check(struct apple_sleep *s) +{ + return s ? 0 : -ENODEV; +} + +static void apple_sleep_set_wd2(struct apple_sleep *s, int secs) +{ + (void)s; + assert(secs == WDT_SUSPEND_SECS); + armed++; +} + +static void apple_sleep_wdt_end(char *wdt) +{ + (void)wdt; + apple_sleep_wd2_owned = false; + disarmed++; +} + +static void sleep_pins_save(struct apple_sleep *s) +{ + (void)s; + pins_saved++; +} + +static void pm_set_suspend_via_firmware(void) +{ + via_firmware = true; +} + +static bool pm_suspend_via_firmware(void) +{ + return via_firmware; +} + +static void sleep_ans_late_open(void) +{ + ans_open = true; +} + +static void sleep_ans_late_release(bool end) +{ + assert(end); + ans_open = false; +} + +static void sleep_v27_scope_end(void) +{ +} + +/* The host's probe: one port's preparation or DART that fails, and the actions it leaves. */ +static int fail_prepare, fail_dart, fail_group, fail_action; +static int prepared, darts; +static void (*action)(void *data); +static void *action_data; +static struct apple_pcie_port ports[PORTS]; + +#define devres_open_group(d, id, gfp) ((void)(d), (void)(gfp), fail_group ? NULL : (void *)ports) +#define devres_remove_group(d, g) ((void)(d), (void)(g)) +#define list_empty(p) (!*(p)) +#define list_for_each_entry(p, head, member) \ + for (p = ports; p < ports + *(head); p++) + +static void devres_release_group(struct device *d, void *group) +{ + (void)d; + assert(group && !action); + darts = 0; +} + +static int devm_add_action_or_reset(struct device *d, void (*fn)(void *), void *data) +{ + (void)d; + if (fail_action) { + fn(data); + return -ENOMEM; + } + action = fn; + action_data = data; + return 0; +} + +static int apple_pcie_t8132_port_prepare(struct platform_device *pdev, struct apple_pcie_port *port) +{ + (void)pdev; + (void)port; + return ++prepared == fail_prepare ? -ENOENT : 0; +} + +static int apple_pcie_t8132_get_dart(struct apple_pcie_port *port) +{ + (void)port; + return ++darts == fail_dart ? -ENODEV : 0; +} + +void apple_sleep_apcie_ports_owned(bool owned); + +/* ACTUAL_FUNCTIONS */ + +static int t_begin(struct apple_sleep *s) +{ + (void)s; + ops_begun++; + return 0; +} + +static void t_finish(struct apple_sleep *s) +{ + (void)s; + ops_ended++; +} + +static const struct apple_sleep_ops ops = { t_begin, t_finish, NULL }; +/* With every T8132 board's tree, and a SoC whose wake leaves no ports to a host */ +static const struct apple_sleep_soc t8132 = { .ops = &ops, .apcie_ports_host = true }; +static const struct apple_sleep_soc other = { .ops = &ops }; +static const struct hw_info t8132_hw = { .t8132_sleep = true }; +static const struct hw_info other_hw; +static struct device dev; +static struct platform_device pdev; +static struct apple_pcie host; + +static void boot(suspend_state_t by_name) +{ + assert(!locked); + mem_sleep_default = by_name; + mem_sleep_current = PM_SUSPEND_TO_IDLE; + apple_sleep = NULL; + apcie_ports_host_owned = false; + fail_prepare = fail_dart = fail_group = fail_action = prepared = darts = 0; + action = NULL; + via_firmware = ans_open = apple_sleep_wd2_owned = false; + armed = disarmed = pins_saved = ops_begun = ops_ended = errors = 0; + memset(&host, 0, sizeof(host)); + host.hw = &t8132_hw; + host.ports = PORTS; +} + +/* The end of apple_sleep_probe(); @undescribed: the tree leaves something else out. */ +static void sleep_probe(const struct apple_sleep_soc *soc, bool managed, bool undescribed) +{ + memset(&the_sleep, 0, sizeof(the_sleep)); + the_sleep.soc = soc; + the_sleep.dev = &dev; + the_sleep.apcie_ports_unowned = !managed; + apple_sleep = &the_sleep; + /* suspend_set_ops() */ + if (mem_sleep_default >= PM_SUSPEND_MEM) + mem_sleep_current = PM_SUSPEND_MEM; + if (undescribed && mem_sleep_current == PM_SUSPEND_MEM && + mem_sleep_default != PM_SUSPEND_MEM) + mem_sleep_current = PM_SUSPEND_TO_IDLE; + guard(mutex)(&apcie_ports_lock); + sleep_apcie_ports_default(&the_sleep); +} + +static void pcie_probe(void) +{ + apple_pcie_t8132_sleep_probe(&pdev, &host); + assert(!locked); +} + +/* A driver that goes takes its devres with it. */ +static void pcie_remove(void) +{ + assert(action == apple_pcie_t8132_disown); + action(action_data); + action = NULL; +} + +/* What suspend_devices_and_enter() does around a sleep that .begin refuses, or doesn't. */ +static int enter(suspend_state_t state) +{ + int ret; + + via_firmware = false; + ret = apple_sleep_begin(state); + apple_sleep_end(); + return ret; +} + +static void refused(void) +{ + int begun = ops_begun, ended = ops_ended, arms = armed, pins = pins_saved, said = errors; + + assert(enter(PM_SUSPEND_MEM) == -EBUSY); + /* Nothing to undo: no watchdog, no saved pin, no SoC step, no firmware sleep. */ + assert(ops_begun == begun && ops_ended == ended && armed == arms && pins_saved == pins); + assert(!via_firmware && !ans_open && !apple_sleep_wd2_owned); + assert(errors == said + 1); + /* It isn't a one-off, and leaves s2idle as it was. */ + assert(enter(PM_SUSPEND_MEM) == -EBUSY); + said = errors; + assert(!enter(PM_SUSPEND_TO_IDLE)); + assert(errors == said && armed == arms && !via_firmware); +} + +static void admitted(void) +{ + int begun = ops_begun, arms = armed, pins = pins_saved, said = errors; + + assert(!enter(PM_SUSPEND_MEM)); + assert(ops_begun == begun + 1 && armed == arms + 1 && pins_saved == pins + 1); + assert(via_firmware && errors == said && disarmed == armed); + assert(!enter(PM_SUSPEND_TO_IDLE)); +} + +static void test_registered(void) +{ + /* The host first */ + boot(PM_SUSPEND_MAX); + pcie_probe(); + assert(host.t8132_sleep && apcie_ports_host_owned); + sleep_probe(&t8132, false, false); + assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); + admitted(); + + /* The host later, as the J773g boots */ + boot(PM_SUSPEND_MAX); + sleep_probe(&t8132, false, false); + assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + refused(); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); + admitted(); + + /* And gone, and back */ + pcie_remove(); + assert(!host.t8132_sleep && mem_sleep_current == PM_SUSPEND_TO_IDLE); + refused(); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_MEM); + admitted(); +} + +static void one_unregistered(bool host_first) +{ + if (host_first) + pcie_probe(); + sleep_probe(&t8132, false, false); + if (!host_first) + pcie_probe(); + assert(!host.t8132_sleep && !apcie_ports_host_owned && !darts); + assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + refused(); + /* Asked for by hand, as the sysfs file lets anyone */ + mem_sleep_current = PM_SUSPEND_MEM; + refused(); +} + +static void test_unregistered(void) +{ + for (int host_first = 0; host_first < 2; host_first++) { + for (int port = 1; port <= PORTS; port++) { + boot(PM_SUSPEND_MAX); + fail_prepare = port; + one_unregistered(host_first); + boot(PM_SUSPEND_MAX); + fail_dart = port; + one_unregistered(host_first); + } + boot(PM_SUSPEND_MAX); + fail_group = 1; + one_unregistered(host_first); + boot(PM_SUSPEND_MAX); + fail_action = 1; + one_unregistered(host_first); + boot(PM_SUSPEND_MAX); + host.ports = 0; + one_unregistered(host_first); + /* Another host's data, as a tree with a compatible of its own gets */ + boot(PM_SUSPEND_MAX); + host.hw = &other_hw; + one_unregistered(host_first); + } + /* No driver for the host at all */ + boot(PM_SUSPEND_MAX); + sleep_probe(&t8132, false, false); + assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + refused(); +} + +static void test_untouched(void) +{ + /* The managed radio contract (J713, J715, J604): its host refuses by itself. */ + boot(PM_SUSPEND_MAX); + host.managed_sids = true; + sleep_probe(&t8132, true, false); + assert(mem_sleep_current == PM_SUSPEND_MEM); + admitted(); + pcie_probe(); + assert(!host.t8132_sleep && mem_sleep_current == PM_SUSPEND_MEM); + admitted(); + + /* A SoC whose wake has another way back for the ports */ + boot(PM_SUSPEND_MAX); + sleep_probe(&other, false, false); + assert(mem_sleep_current == PM_SUSPEND_MEM); + admitted(); + + /* No sleep driver */ + boot(PM_SUSPEND_MAX); + pcie_probe(); + assert(host.t8132_sleep && mem_sleep_current == PM_SUSPEND_TO_IDLE); +} + +static void test_default(void) +{ + /* mem_sleep_default=deep keeps its default, but not its sleep. */ + boot(PM_SUSPEND_MEM); + sleep_probe(&t8132, false, false); + assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); + refused(); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_MEM); + admitted(); + + /* mem_sleep_default=s2idle: a host that registers doesn't make deep sleep the default. */ + boot(PM_SUSPEND_TO_IDLE); + sleep_probe(&t8132, false, false); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + admitted(); + + /* Nor where the tree leaves out another part a deep sleep needs. */ + boot(PM_SUSPEND_MAX); + sleep_probe(&t8132, false, true); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + + /* Chosen by hand meanwhile: still deep once the host is there. */ + boot(PM_SUSPEND_MAX); + sleep_probe(&t8132, false, false); + mem_sleep_current = PM_SUSPEND_MEM; + refused(); + pcie_probe(); + assert(mem_sleep_current == PM_SUSPEND_MEM); + admitted(); +} + +int main(void) +{ + test_registered(); + test_unregistered(); + test_untouched(); + test_default(); + puts("T8132 deep-sleep refusal: PASS"); + return 0; +} diff --git a/tools/asahi/t8132-sleep-refuse-tests.py b/tools/asahi/t8132-sleep-refuse-tests.py new file mode 100755 index 0000000000000..cd5748366670d --- /dev/null +++ b/tools/asahi/t8132-sleep-refuse-tests.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Build and run the T8132 deep-sleep refusal, with the host's probe that lifts it.""" +from pathlib import Path +import re +import resource +import subprocess +import sys +import tempfile + +# python3 -I, as run-host-tests.py runs it, leaves this directory off sys.path. +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from sleep_test_utils import function # noqa: E402 + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +root = Path(__file__).resolve().parents[2] +sleep = (root / "drivers/soc/apple/sleep.c").read_text() +pcie = (root / "drivers/pci/controller/pcie-apple.c").read_text() + +actual = [ + function(sleep, name) + for name in ( + "sleep_apcie_ports_orphaned", + "sleep_apcie_ports_default", + "apple_sleep_apcie_ports_owned", + "apple_sleep_begin", + "apple_sleep_end", + ) +] + [ + function(pcie, name) + for name in ("apple_pcie_t8132_disown", "apple_pcie_t8132_sleep_probe") +] + +# The default is decided after everything else that takes it away. +probe = function(sleep, "apple_sleep_probe") +decided = probe.index("sleep_apcie_ports_default(s);") +assert probe.rindex("mem_sleep_current = PM_SUSPEND_TO_IDLE;") < decided +assert decided < probe.index("boot object resumes") + +# Only T8132's wake leaves the ports down for a host to find. +socs = re.findall(r"^static const struct apple_sleep_soc (\w+) = \{(.*?)^\};", sleep, re.M | re.S) +assert len(socs) > 1 +assert [name for name, body in socs if ".apcie_ports_host = true," in body] == ["t8132_sleep_soc"] +for path in (root / "drivers/soc/apple").glob("*.h"): + assert ".apcie_ports_host" not in path.read_text(), path.name + +# A host with the managed radio contract has its own recovery, and its own refusal. +assert re.search( + r'apcie_ports_unowned = !of_property_present\(np, "apple,managed-sids"\);', + function(sleep, "apple_sleep_map_apcie"), +) + +fixture = Path(__file__).with_suffix(".c").read_text() +with tempfile.TemporaryDirectory(prefix="t8132-refuse-") as tmp: + c, exe = Path(tmp) / "test.c", Path(tmp) / "test" + c.write_text(fixture.replace("/* ACTUAL_FUNCTIONS */", "\n".join(actual))) + subprocess.run( + ["cc", "-std=gnu11", "-Wall", "-Werror", str(c), "-o", str(exe)], + check=True, + ) + subprocess.run([str(exe)], check=True) From 2977a1a312377c8fd9f5f6a6eba86d2a10a2eb97 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 10:56:00 +0000 Subject: [PATCH 5/5] soc: apple: sleep: Leave the T8132 sleep default to the user When the PCIe host registered after apple-sleep, the driver made deep sleep the default again, also over an s2idle that was written to /sys/power/mem_sleep in the meantime. Drop the change of the default and keep the refusal, which is what protects the machine. systemd-sleep tries "freeze" when "mem" fails, so a Mac without the recovery still suspends to idle there. Signed-off-by: DjDeveloperr --- drivers/soc/apple/sleep.c | 27 ----- tools/asahi/t8132-sleep-refuse-tests.c | 136 +++++++----------------- tools/asahi/t8132-sleep-refuse-tests.py | 8 +- 3 files changed, 41 insertions(+), 130 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 0c803ff552690..0af12d15da320 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1031,8 +1031,6 @@ struct apple_sleep { bool apcie_port_stopped; /* Ports no host sets up again after a real sleep: no managed radio contract */ bool apcie_ports_unowned; - /* s2idle is the default only for want of the ports' host */ - bool apcie_default_held; struct { void __iomem *port; void __iomem *phy; @@ -1436,7 +1434,6 @@ EXPORT_SYMBOL_GPL(apple_sleep_debug_link_cut_peek); /* Kept out of struct apple_sleep so pcie-apple and apple-sleep may probe in either order. */ static bool apcie_ports_host_owned; -static DEFINE_MUTEX(apcie_ports_lock); /* * No host sets the ports up again after a real sleep: its driver isn't there, or the probe @@ -1449,30 +1446,9 @@ static bool sleep_apcie_ports_orphaned(const struct apple_sleep *s) !READ_ONCE(apcie_ports_host_owned); } -/* Deep sleep is the default only with that host, unless mem_sleep_default= asks for it. */ -static void sleep_apcie_ports_default(struct apple_sleep *s) -{ - bool orphaned = sleep_apcie_ports_orphaned(s); - - lockdep_assert_held(&apcie_ports_lock); - if (orphaned && mem_sleep_current == PM_SUSPEND_MEM && - mem_sleep_default != PM_SUSPEND_MEM) { - mem_sleep_current = PM_SUSPEND_TO_IDLE; - s->apcie_default_held = true; - dev_info(s->dev, "s2idle is the default until the PCIe ports have a host that sets them up again after a deep sleep\n"); - } else if (!orphaned && s->apcie_default_held) { - mem_sleep_current = PM_SUSPEND_MEM; - s->apcie_default_held = false; - dev_info(s->dev, "deep sleep is the default: the PCIe ports have their host\n"); - } -} - void apple_sleep_apcie_ports_owned(bool owned) { - guard(mutex)(&apcie_ports_lock); WRITE_ONCE(apcie_ports_host_owned, owned); - if (apple_sleep) - sleep_apcie_ports_default(apple_sleep); } EXPORT_SYMBOL_GPL(apple_sleep_apcie_ports_owned); @@ -8986,9 +8962,6 @@ static int apple_sleep_probe(struct platform_device *pdev) mem_sleep_current = PM_SUSPEND_TO_IDLE; dev_warn(dev, "deep sleep needs the parts named above: s2idle stays the default\n"); } - /* Last: it only ever gives back what it took. */ - scoped_guard(mutex, &apcie_ports_lock) - sleep_apcie_ports_default(s); dev_info(dev, "deep sleep %s: boot object resumes %llu\n", mem_sleep_current == PM_SUSPEND_MEM ? "is the default" : diff --git a/tools/asahi/t8132-sleep-refuse-tests.c b/tools/asahi/t8132-sleep-refuse-tests.c index a1173018c1926..43ab5b07cb905 100644 --- a/tools/asahi/t8132-sleep-refuse-tests.c +++ b/tools/asahi/t8132-sleep-refuse-tests.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-only -/* A T8132 deep sleep without a host that sets the PCIe ports up again: refused, not the default. */ +/* A T8132 deep sleep without a host that sets the PCIe ports up again: refused. */ #include #include #include @@ -10,10 +10,8 @@ typedef uint32_t u32; typedef int suspend_state_t; -#define PM_SUSPEND_ON 0 #define PM_SUSPEND_TO_IDLE 1 #define PM_SUSPEND_MEM 3 -#define PM_SUSPEND_MAX 4 #define READ_ONCE(x) (x) #define WRITE_ONCE(x, v) ((x) = (v)) #define __iomem @@ -43,7 +41,7 @@ struct apple_sleep_soc { struct apple_sleep { const struct apple_sleep_soc *soc; struct device *dev; - bool untested, dry_run, apcie_ports_unowned, apcie_default_held; + bool untested, dry_run, apcie_ports_unowned; bool apcie_port_stopped, usb_down_ran, smc_woke_alone, wd1_saved; u32 wd1_saved_ctl, wd1_saved_bite; char *wdt; @@ -63,10 +61,9 @@ struct apple_pcie { int ports; }; -static suspend_state_t mem_sleep_current, mem_sleep_default; +static suspend_state_t mem_sleep_current; static struct apple_sleep *apple_sleep; static bool apcie_ports_host_owned; -static int apcie_ports_lock; static bool sleep_debug_link_cut, sleep_deep_requested, sleep_untested_commit; static int sleep_lid_closed, sleep_radio_woke; static int sleep_cpus_awake; @@ -81,15 +78,6 @@ static struct apple_sleep the_sleep; #define WDT_WD1_BITE 8 #define WDT_SUSPEND_SECS 120 -static int locked; - -static void unlock(int *unused) -{ - locked--; -} - -#define guard(type) int held __attribute__((cleanup(unlock), unused)) = locked++; (void) -#define lockdep_assert_held(l) ((void)(l), assert(locked == 1)) #define spin_lock_irqsave(l, f) ((void)(l), (f) = 0) #define spin_unlock_irqrestore(l, f) ((void)(l), (void)(f)) #define cpumask_copy(d, s) ((void)(d), (void)(s)) @@ -232,11 +220,9 @@ static struct device dev; static struct platform_device pdev; static struct apple_pcie host; -static void boot(suspend_state_t by_name) +static void boot(void) { - assert(!locked); - mem_sleep_default = by_name; - mem_sleep_current = PM_SUSPEND_TO_IDLE; + mem_sleep_current = PM_SUSPEND_MEM; apple_sleep = NULL; apcie_ports_host_owned = false; fail_prepare = fail_dart = fail_group = fail_action = prepared = darts = 0; @@ -248,28 +234,18 @@ static void boot(suspend_state_t by_name) host.ports = PORTS; } -/* The end of apple_sleep_probe(); @undescribed: the tree leaves something else out. */ -static void sleep_probe(const struct apple_sleep_soc *soc, bool managed, bool undescribed) +static void sleep_probe(const struct apple_sleep_soc *soc, bool managed) { memset(&the_sleep, 0, sizeof(the_sleep)); the_sleep.soc = soc; the_sleep.dev = &dev; the_sleep.apcie_ports_unowned = !managed; apple_sleep = &the_sleep; - /* suspend_set_ops() */ - if (mem_sleep_default >= PM_SUSPEND_MEM) - mem_sleep_current = PM_SUSPEND_MEM; - if (undescribed && mem_sleep_current == PM_SUSPEND_MEM && - mem_sleep_default != PM_SUSPEND_MEM) - mem_sleep_current = PM_SUSPEND_TO_IDLE; - guard(mutex)(&apcie_ports_lock); - sleep_apcie_ports_default(&the_sleep); } static void pcie_probe(void) { apple_pcie_t8132_sleep_probe(&pdev, &host); - assert(!locked); } /* A driver that goes takes its devres with it. */ @@ -320,134 +296,101 @@ static void admitted(void) static void test_registered(void) { /* The host first */ - boot(PM_SUSPEND_MAX); + boot(); pcie_probe(); assert(host.t8132_sleep && apcie_ports_host_owned); - sleep_probe(&t8132, false, false); - assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); + sleep_probe(&t8132, false); admitted(); /* The host later, as the J773g boots */ - boot(PM_SUSPEND_MAX); - sleep_probe(&t8132, false, false); - assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + boot(); + sleep_probe(&t8132, false); refused(); pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); admitted(); /* And gone, and back */ pcie_remove(); - assert(!host.t8132_sleep && mem_sleep_current == PM_SUSPEND_TO_IDLE); + assert(!host.t8132_sleep); refused(); pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_MEM); admitted(); + + /* The choice in /sys/power/mem_sleep stays the user's, whatever the host does. */ + for (int state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state += 2) { + boot(); + sleep_probe(&t8132, false); + mem_sleep_current = state; + pcie_probe(); + assert(mem_sleep_current == state); + pcie_remove(); + assert(mem_sleep_current == state); + } } static void one_unregistered(bool host_first) { if (host_first) pcie_probe(); - sleep_probe(&t8132, false, false); + sleep_probe(&t8132, false); if (!host_first) pcie_probe(); assert(!host.t8132_sleep && !apcie_ports_host_owned && !darts); - assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); - refused(); - /* Asked for by hand, as the sysfs file lets anyone */ - mem_sleep_current = PM_SUSPEND_MEM; refused(); + assert(mem_sleep_current == PM_SUSPEND_MEM); } static void test_unregistered(void) { for (int host_first = 0; host_first < 2; host_first++) { for (int port = 1; port <= PORTS; port++) { - boot(PM_SUSPEND_MAX); + boot(); fail_prepare = port; one_unregistered(host_first); - boot(PM_SUSPEND_MAX); + boot(); fail_dart = port; one_unregistered(host_first); } - boot(PM_SUSPEND_MAX); + boot(); fail_group = 1; one_unregistered(host_first); - boot(PM_SUSPEND_MAX); + boot(); fail_action = 1; one_unregistered(host_first); - boot(PM_SUSPEND_MAX); + boot(); host.ports = 0; one_unregistered(host_first); /* Another host's data, as a tree with a compatible of its own gets */ - boot(PM_SUSPEND_MAX); + boot(); host.hw = &other_hw; one_unregistered(host_first); } /* No driver for the host at all */ - boot(PM_SUSPEND_MAX); - sleep_probe(&t8132, false, false); - assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); + boot(); + sleep_probe(&t8132, false); refused(); } static void test_untouched(void) { /* The managed radio contract (J713, J715, J604): its host refuses by itself. */ - boot(PM_SUSPEND_MAX); + boot(); host.managed_sids = true; - sleep_probe(&t8132, true, false); - assert(mem_sleep_current == PM_SUSPEND_MEM); + sleep_probe(&t8132, true); admitted(); pcie_probe(); - assert(!host.t8132_sleep && mem_sleep_current == PM_SUSPEND_MEM); + assert(!host.t8132_sleep); admitted(); /* A SoC whose wake has another way back for the ports */ - boot(PM_SUSPEND_MAX); - sleep_probe(&other, false, false); - assert(mem_sleep_current == PM_SUSPEND_MEM); + boot(); + sleep_probe(&other, false); admitted(); /* No sleep driver */ - boot(PM_SUSPEND_MAX); - pcie_probe(); - assert(host.t8132_sleep && mem_sleep_current == PM_SUSPEND_TO_IDLE); -} - -static void test_default(void) -{ - /* mem_sleep_default=deep keeps its default, but not its sleep. */ - boot(PM_SUSPEND_MEM); - sleep_probe(&t8132, false, false); - assert(mem_sleep_current == PM_SUSPEND_MEM && !the_sleep.apcie_default_held); - refused(); - pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_MEM); - admitted(); - - /* mem_sleep_default=s2idle: a host that registers doesn't make deep sleep the default. */ - boot(PM_SUSPEND_TO_IDLE); - sleep_probe(&t8132, false, false); - pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); - admitted(); - - /* Nor where the tree leaves out another part a deep sleep needs. */ - boot(PM_SUSPEND_MAX); - sleep_probe(&t8132, false, true); - pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_TO_IDLE); - - /* Chosen by hand meanwhile: still deep once the host is there. */ - boot(PM_SUSPEND_MAX); - sleep_probe(&t8132, false, false); - mem_sleep_current = PM_SUSPEND_MEM; - refused(); + boot(); pcie_probe(); - assert(mem_sleep_current == PM_SUSPEND_MEM); - admitted(); + assert(host.t8132_sleep); } int main(void) @@ -455,7 +398,6 @@ int main(void) test_registered(); test_unregistered(); test_untouched(); - test_default(); puts("T8132 deep-sleep refusal: PASS"); return 0; } diff --git a/tools/asahi/t8132-sleep-refuse-tests.py b/tools/asahi/t8132-sleep-refuse-tests.py index cd5748366670d..d87e3e01fbe4e 100755 --- a/tools/asahi/t8132-sleep-refuse-tests.py +++ b/tools/asahi/t8132-sleep-refuse-tests.py @@ -21,7 +21,6 @@ function(sleep, name) for name in ( "sleep_apcie_ports_orphaned", - "sleep_apcie_ports_default", "apple_sleep_apcie_ports_owned", "apple_sleep_begin", "apple_sleep_end", @@ -31,11 +30,8 @@ for name in ("apple_pcie_t8132_disown", "apple_pcie_t8132_sleep_probe") ] -# The default is decided after everything else that takes it away. -probe = function(sleep, "apple_sleep_probe") -decided = probe.index("sleep_apcie_ports_default(s);") -assert probe.rindex("mem_sleep_current = PM_SUSPEND_TO_IDLE;") < decided -assert decided < probe.index("boot object resumes") +# What the user, or the command line, chose stays chosen. +assert "mem_sleep" not in function(sleep, "apple_sleep_apcie_ports_owned") # Only T8132's wake leaves the ports down for a host to find. socs = re.findall(r"^static const struct apple_sleep_soc (\w+) = \{(.*?)^\};", sleep, re.M | re.S)