From 2dc24427ade30b0afef2d7533e2ea5343448dfc0 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 05:00:02 -0500 Subject: [PATCH 1/3] 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/3] 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/3] 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)