diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index a36e0bee25671..ad24b561a8d5b 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml @@ -189,32 +189,66 @@ allOf: properties: reg: minItems: 21 - maxItems: 22 + maxItems: 25 reg-names: - minItems: 21 - items: - - const: ps - - const: pwrgate - - const: cpu-start - - const: cllt - - const: cluster - - const: commit - - const: wdt - - const: spmi - - const: spmi-hpm - - const: wake - - const: ps-aon - - const: mcc-a - - const: mcc-b - - const: mcc-c - - const: mcc-d - - const: apcie-axi - - const: apcie-rc - - const: apcie-clkgen - - const: apcie-phy - - const: apcie-phy-ip - - const: apcie-port0 - - const: clk-mux + oneOf: + - description: USB-C port controllers on SPMI + minItems: 21 + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: spmi + - const: spmi-hpm + - const: wake + - const: ps-aon + - const: mcc-a + - const: mcc-b + - const: mcc-c + - const: mcc-d + - const: apcie-axi + - const: apcie-rc + - const: apcie-clkgen + - const: apcie-phy + - const: apcie-phy-ip + - const: apcie-port0 + - const: clk-mux + - description: + USB-C port controllers on I2C, which leave out their SPMI bus. + apcie-portN and apcie-phyN are those of a PCIe port past port + 0, its registers and its PHY lane's. Deep sleep is refused on + a board that enables such a port without them. + minItems: 21 + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: spmi + - const: wake + - const: ps-aon + - const: mcc-a + - const: mcc-b + - const: mcc-c + - const: mcc-d + - const: apcie-axi + - const: apcie-rc + - const: apcie-clkgen + - const: apcie-phy + - const: apcie-phy-ip + - const: apcie-port0 + - const: clk-mux + - const: apcie-port1 + - const: apcie-phy1 + - const: apcie-port2 + - const: apcie-phy2 - if: properties: compatible: diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 7e1ff6401057c..e4416d857cb3a 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -7285,6 +7285,18 @@ int apple_dart_quiesce_commands(struct device *dev) } EXPORT_SYMBOL_GPL(apple_dart_quiesce_commands); +void apple_dart_host_wake_failed(struct device *dev) +{ + struct apple_dart *dart = dev_get_drvdata(dev); + + if (WARN_ON_ONCE(!dart)) + return; + guard(spinlock_irqsave)(&dart->lock); + dart->host_resumes = true; + WRITE_ONCE(dart->commands_gated, true); +} +EXPORT_SYMBOL_GPL(apple_dart_host_wake_failed); + int apple_dart_resume_commands(struct device *dev) { struct apple_dart *dart = dev_get_drvdata(dev); diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 5205407fd02cd..925cfe96be559 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -601,6 +601,7 @@ struct apple_radio_sleep; struct apple_pcie { struct mutex lock; struct device *dev; + bool t8122_native; void __iomem *base; void __iomem *fabric_base; void __iomem *debug_base; @@ -741,6 +742,7 @@ struct apple_pcie_port { bool started; bool needs_stop; bool link_failed; + bool sleep_clock_failed; bool irq_disabled; void __iomem *intr2axi; /* adopt_link: this port's */ u8 adopted_bus; /* adopt_link: the secondary bus */ @@ -1345,7 +1347,7 @@ static void apple_pcie_port_enable_irq(struct apple_pcie_port *port) { unsigned long flags; - if (!port->irq_disabled) + if (!port->irq_disabled || port->sleep_clock_failed) return; raw_spin_lock_irqsave(&port->irq_lock, flags); @@ -4034,6 +4036,8 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d port = apple_pcie_get_port(pdev); if (!port) return 0; + if (port->sleep_clock_failed) + return -ENODEV; dev_dbg(&pdev->dev, "added to bus %s, index %d\n", pci_name(pdev->bus->self), port->idx); @@ -4123,7 +4127,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci int idx; port = apple_pcie_get_port(pdev); - if (!port || port->pcie->hw->adopt_link) + if (!port || port->pcie->hw->adopt_link || port->sleep_clock_failed) return; mutex_lock(&port->pcie->lock); @@ -4161,6 +4165,7 @@ static int apple_pcie_init(struct pci_config_window *cfg) if (WARN_ON(!pcie)) return -ENOENT; pcie->cfg = cfg; + pcie->t8122_native = of_device_is_compatible(dev->of_node, "apple,t8122-pcie"); for_each_available_child_of_node_scoped(dev->of_node, of_port) { ret = apple_pcie_setup_port(pcie, of_port); @@ -4180,6 +4185,23 @@ static int apple_pcie_init(struct pci_config_window *cfg) return 0; } +static bool apple_pcie_t8122_config_blocked(struct apple_pcie *pcie, struct pci_bus *bus, + unsigned int devfn) +{ + unsigned long failed; + + if (!pcie->t8122_native) + return false; + failed = apple_sleep_t8122_failed_ports(); + if (!failed) + return false; + while (bus->parent) { + devfn = bus->self->devfn; + bus = bus->parent; + } + return failed & BIT(PCI_SLOT(devfn)); +} + /* * The T8140 host starts with all downstream accesses blocked. * Only a successful owned bootstrap and typed ECAM checks open functions0/1. @@ -4194,6 +4216,8 @@ static void __iomem *apple_pcie_map_bus(struct pci_bus *bus, if (READ_ONCE(pcie->cio_changed)) return NULL; + if (apple_pcie_t8122_config_blocked(pcie, bus, devfn)) + return NULL; if (!apple_radio_config_allowed(pcie)) return NULL; if (pcie->hw->adopt_link) { @@ -7857,6 +7881,38 @@ static int apple_pcie_resume_early(struct device *dev) return 0; } +static void apple_pcie_disconnect_failed_t8122_ports(struct apple_pcie *pcie) +{ + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct apple_pcie_port *port; + unsigned long failed; + + if (!pcie->t8122_native) + return; + failed = apple_sleep_t8122_failed_ports(); + if (!failed) + return; + list_for_each_entry(port, &pcie->ports, entry) { + struct pci_dev *root; + + if (!(failed & BIT(port->idx))) + continue; + port->sleep_clock_failed = true; + port->hib_link_up = false; + port->hib_cold = false; + apple_pcie_port_disable_irq(port); + if (!bridge->bus) + continue; + root = pci_get_slot(bridge->bus, PCI_DEVFN(port->idx, 0)); + if (!root) + continue; + pci_dev_set_disconnected(root, NULL); + if (root->subordinate) + pci_walk_bus(root->subordinate, pci_dev_set_disconnected, NULL); + pci_dev_put(root); + } +} + static int apple_pcie_resume_noirq(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); @@ -7865,6 +7921,7 @@ static int apple_pcie_resume_noirq(struct device *dev) /* The domain's hold for the sleep ends first, whatever follows. */ apple_pcie_release_domains(pcie); + apple_pcie_disconnect_failed_t8122_ports(pcie); if (pcie->cio_prepared) { ret = apple_pcie_cio_resume(pcie); if (!ret) @@ -9608,6 +9665,8 @@ static int apple_pcie_resume(struct device *dev) return 0; } list_for_each_entry(port, &pcie->ports, entry) { + if (port->sleep_clock_failed) + continue; if (!port->hib_link_up) { apple_pcie_port_writel(port, ~0, PORT_INTMSK); apple_pcie_port_writel(port, ~0, PORT_INTSTAT); @@ -9661,6 +9720,9 @@ static int apple_pcie_prepare(struct device *dev) struct apple_pcie *pcie = apple_pcie_lookup(dev); int ret; + if (pcie && pcie->t8122_native && apple_sleep_t8122_failed_ports()) + return -EIO; + /* * A host-owned PCIe-C DART sleeps only inside the NHI's deep-sleep * transaction, prepared before this, or with a kept s2idle link. diff --git a/drivers/soc/apple/sleep-t8122.h b/drivers/soc/apple/sleep-t8122.h index eaf7bf19b2e01..a129e5cd34428 100644 --- a/drivers/soc/apple/sleep-t8122.h +++ b/drivers/soc/apple/sleep-t8122.h @@ -35,6 +35,18 @@ #define T8122_APCIE_PHY_CTRL_RESET BIT(7) #define T8122_APCIE_PHY_CTRL_UP BIT(9) +/* + * Root ports 0 to 2 can be wired; the sleep node's registers name the ones past port 0 + * ("apcie-portN", "apcie-phyN") that its board may enable. Their PHY lanes' words in the + * shared PHY IP block are lane 0's, this far apart. + */ +#define T8122_APCIE_PORTS 3 +#define T8122_APCIE_PHY_LANE0 0xc000 +#define T8122_APCIE_PHY_ON 0xc0000603 +#define T8122_APCIE_PHY_IP_LANE0 0x9000 +#define T8122_APCIE_PHY_IP_STRIDE 0x8000 +#define T8122_APCIE_PORT_INTR2AXI 0x4020 + #define T8122_PWRGATE_E 0x80 #define T8122_PWRGATE_P 0x88 #define T8122_PWRGATE_DISP_A 0xcac @@ -159,6 +171,37 @@ static const u32 t8122_audio_muxes[] = { 0xd8, 0xdc, 0xe0, 0xe4, }; +/* A port's own setup, as the boot loader left it: kept for the ports past port 0. */ +static const u16 t8122_port_words[] = { + 0x088, 0x130, 0x13c, 0x144, 0x808, 0x834, 0x83c, 0x140, 0x8a4, +}; + +/* The PHY IP block's words: the shared ones, then lane 0's. */ +static const struct { u32 off, val; } t8122_phy_ip_words[] = { + { 0x0028, 0x00800800 }, { 0x006c, 0x0000004c }, { 0x0080, 0x80010001 }, + { 0x0094, 0x00000001 }, { 0x1204, 0x0000004c }, { 0x1000, 0x2a2383c8 }, + { 0x102c, 0x04454447 }, { 0x5080, 0x294d8190 }, { 0x5088, 0x00560800 }, + { 0x508c, 0x294d8190 }, { 0x5094, 0x00060800 }, { 0x50a0, 0x00554800 }, + { 0x50ac, 0x00554800 }, { 0x50b8, 0x00554800 }, { 0x5200, 0x0008155e }, + { 0x5224, 0x00000818 }, { 0x5240, 0x04010f69 }, { 0x52b0, 0x10300811 }, + { 0x52fc, 0x03078078 }, { 0x531c, 0x00000002 }, { 0x608c, 0x65918280 }, + { 0x6224, 0x00000818 }, { 0x6228, 0x00000b18 }, { 0x6230, 0x0001e80f }, + { 0x624c, 0x1f80009d }, { 0xa000, 0x080001aa }, { 0xa008, 0x0000000a }, + { 0xa00c, 0x0000000a }, { 0xa010, 0x0000000a }, { 0xa014, 0x0000000a }, + { 0xa038, 0x00000271 }, { 0xa040, 0x000000c8 }, { 0xa04c, 0x00000bff }, + { 0xa050, 0x00000001 }, { 0xa068, 0x00000c41 }, { 0xa070, 0x55b00820 }, + { 0xa084, 0x3e8f4240 }, { 0xa088, 0x00000fff }, { 0xa10c, 0x000f4240 }, + { 0xa11c, 0x000f30dc }, { 0xa178, 0x00300000 }, { 0xa0c0, 0x14e051f8 }, + { 0xa1a4, 0x00000901 }, { 0xa1b0, 0x00000000 }, { 0xa1b8, 0x00000000 }, + { 0xa1e8, 0x00100000 }, { 0x903c, 0x018c3509 }, { 0x90d8, 0x0102068d }, + { 0x90dc, 0x12004408 }, { 0x91e0, 0x00002008 }, { 0x9288, 0x0801172a }, + { 0x9280, 0xcccccccc }, { 0x9000, 0x1e000000 }, { 0x9280, 0xccc8ccc1 }, + { 0x928c, 0x00000003 }, { 0xb02c, 0x01980062 }, { 0xb098, 0x05028000 }, + { 0xc01c, 0x000006a1 }, { 0xc080, 0x540c6831 }, { 0xc08c, 0x10be0c1f }, + { 0xc0a0, 0x021a1780 }, { 0xc0ec, 0x00000000 }, { 0xd008, 0x0000000c }, + { 0xd014, 0x04840408 }, +}; + static struct { u32 clk_mux_saved[ARRAY_SIZE(t8122_audio_muxes)]; u32 nco_saved[T8122_NCO_CHANS][4]; @@ -174,12 +217,27 @@ static struct { void __iomem *apcie_clkgen; void __iomem *apcie_phy; void __iomem *apcie_phy_ip; - void __iomem *apcie_port0; + struct { + void __iomem *port; + void __iomem *phy; + struct device *dart; + bool saved; + u32 port_saved[ARRAY_SIZE(t8122_port_words)]; + u32 lane_saved[ARRAY_SIZE(t8122_phy_ip_words)]; + } apcie_port[T8122_APCIE_PORTS]; + unsigned long apcie_failed; + u32 apcie_axi_ports; /* The AXI bridge's word with each port's bits */ u32 notify_saved[2]; bool notify_staged; bool matched; } t8122; +unsigned long apple_sleep_t8122_failed_ports(void) +{ + return READ_ONCE(t8122.apcie_failed); +} +EXPORT_SYMBOL_GPL(apple_sleep_t8122_failed_ports); + static __always_inline bool t8122_is_pcore(void) { return !!(read_sysreg(mpidr_el1) & T8122_MPIDR_P_CORE); @@ -656,6 +714,124 @@ static void __iomem *t8122_map(struct apple_sleep *s, const char *name, struct r return base; } +/* The root ports past port 0 the board enables, in the PCIe host's node. */ +static unsigned long t8122_extra_ports(void) +{ + struct device_node *host = of_find_compatible_node(NULL, NULL, "apple,t8122-pcie"); + struct device_node *child; + unsigned long ports = 0; + + if (host && of_device_is_available(host)) { + for_each_available_child_of_node(host, child) { + int devfn = of_pci_get_devfn(child); + + if (devfn >= 0) + ports |= BIT(PCI_SLOT(devfn)); + } + } + of_node_put(host); + + return ports & ~BIT(0); +} + +/* + * An enabled port past port 0 needs its registers here: its DART and its host come back + * before its clocks otherwise. t8122_sleep_begin() refuses a deep sleep without them. + */ +static void t8122_map_ports(struct apple_sleep *s) +{ + unsigned long ports = t8122_extra_ports(); + unsigned int i; + + for_each_set_bit(i, &ports, T8122_APCIE_PORTS) { + void __iomem *port, *phy; + struct resource phy_ip; + char name[16]; + int index; + + snprintf(name, sizeof(name), "apcie-port%u", i); + port = t8122_map(s, name, NULL); + snprintf(name, sizeof(name), "apcie-phy%u", i); + phy = t8122_map(s, name, NULL); + index = of_property_match_string(s->dev->of_node, "reg-names", "apcie-phy-ip"); + if (IS_ERR(port) || IS_ERR(phy) || index < 0 || + of_address_to_resource(s->dev->of_node, index, &phy_ip) || + resource_size(&phy_ip) < T8122_APCIE_PHY_IP_LANE0 + + (i + 1) * T8122_APCIE_PHY_IP_STRIDE) { + dev_warn(s->dev, "PCIe port %u: no registers to bring it back, deep sleep is refused\n", + i); + continue; + } + t8122.apcie_port[i].port = port; + t8122.apcie_port[i].phy = phy; + } +} + +static int t8122_get_port_dart(struct apple_sleep *s, struct device_node *host, + unsigned int i) +{ + struct device_node *port __free(device_node) = + of_pci_find_child_device(host, PCI_DEVFN(i, 0)); + struct of_phandle_args iommu; + struct platform_device *pdev; + u32 bus; + int ret; + + if (t8122.apcie_port[i].dart) + return dev_get_drvdata(t8122.apcie_port[i].dart) ? 0 : -ENODEV; + if (!IS_REACHABLE(CONFIG_APPLE_DART)) + return -EOPNOTSUPP; + if (!port || of_property_read_u32(port, "bus-range", &bus) || bus > U8_MAX) + return -EINVAL; + ret = of_map_iommu_id(host, PCI_DEVID(bus, 0), &iommu); + if (ret) + return ret; + pdev = of_find_device_by_node(iommu.np); + of_node_put(iommu.np); + if (!pdev) + return -ENODEV; + if (!dev_get_drvdata(&pdev->dev)) { + put_device(&pdev->dev); + return -ENODEV; + } + ret = devm_add_action_or_reset(s->dev, apple_sleep_put_device, &pdev->dev); + if (ret) + return ret; + t8122.apcie_port[i].dart = &pdev->dev; + return 0; +} + +static int t8122_sleep_begin(struct apple_sleep *s) +{ + unsigned long ports = t8122_extra_ports(); + struct device_node *host __free(device_node) = NULL; + unsigned int i; + int ret; + + if (apple_sleep_t8122_failed_ports()) + return -EIO; + if (!ports) + return 0; + host = of_find_compatible_node(NULL, NULL, "apple,t8122-pcie"); + for_each_set_bit(i, &ports, BITS_PER_LONG) { + if (i >= T8122_APCIE_PORTS || !t8122.apcie_port[i].port) { + dev_err(s->dev, + "refused: no registers to bring PCIe port %u back after a sleep\n", + i); + return -EOPNOTSUPP; + } + ret = t8122_get_port_dart(s, host, i); + if (ret) { + dev_err(s->dev, + "refused: cannot gate PCIe port %u's DART after failed wake: %d\n", + i, ret); + return ret; + } + } + + return 0; +} + static int t8122_sleep_map_regs(struct apple_sleep *s) { static const struct { @@ -690,9 +866,13 @@ static int t8122_sleep_map_regs(struct apple_sleep *s) return dev_err_probe(s->dev, PTR_ERR(*field), "no %s registers\n", regs[i].name); } - t8122.hpm_notify = t8122_map(s, "spmi-hpm", NULL); - if (IS_ERR(t8122.hpm_notify)) - return dev_err_probe(s->dev, PTR_ERR(t8122.hpm_notify), "no spmi-hpm registers\n"); + /* Only where the USB-C port controllers are on SPMI: on I2C this bus isn't there. */ + if (of_property_match_string(s->dev->of_node, "reg-names", "spmi-hpm") >= 0) { + t8122.hpm_notify = t8122_map(s, "spmi-hpm", NULL); + if (IS_ERR(t8122.hpm_notify)) + return dev_err_probe(s->dev, PTR_ERR(t8122.hpm_notify), + "no spmi-hpm registers\n"); + } s->ps_aon = t8122_map(s, "ps-aon", NULL); if (IS_ERR(s->ps_aon)) { @@ -744,7 +924,7 @@ static int t8122_sleep_map_regs(struct apple_sleep *s) { "apcie-clkgen", &t8122.apcie_clkgen }, { "apcie-phy", &t8122.apcie_phy }, { "apcie-phy-ip", &t8122.apcie_phy_ip }, - { "apcie-port0", &t8122.apcie_port0 }, + { "apcie-port0", &t8122.apcie_port[0].port }, }; for (i = 0; i < ARRAY_SIZE(apcie_regs); i++) { @@ -753,7 +933,9 @@ static int t8122_sleep_map_regs(struct apple_sleep *s) return dev_err_probe(s->dev, PTR_ERR(*apcie_regs[i].slot), "no %s registers\n", apcie_regs[i].name); } + t8122.apcie_port[0].phy = t8122.apcie_phy + T8122_APCIE_PHY_LANE0; } + t8122_map_ports(s); /* Ensure the EL1 deep-WFI stub is clean in DRAM before secondary cores jump to it. */ pcore = t8122_is_pcore(); @@ -873,14 +1055,64 @@ static void t8122_sleep_match_undo(struct apple_sleep *s) } } +/* A port's and its PHY lane's shutdown, PERST# asserted; nothing on a lane that is off. */ +static void t8122_port_down(void __iomem *port, void __iomem *phy) +{ + u32 val; + + writel(readl(port + 0x13c) | 0x100, port + 0x13c); + writel(0, port + APCIE_PORT_PERST); + writel(readl(port + 0x13c) & ~0x100, port + 0x13c); + writel(readl(phy) & ~BIT(10), phy); + writel(readl(phy) & ~BIT(9), phy); + writel(readl(phy) | BIT(4), phy); + writel(readl(phy) & ~BIT(0), phy); + readl_poll_timeout_atomic(phy, val, !(val & BIT(2)), 1, APCIE_POLL_US); + writel(readl(phy) & ~BIT(1), phy); + readl_poll_timeout_atomic(phy, val, !(val & BIT(3)), 1, APCIE_POLL_US); + writel(readl(port + APCIE_PORT_APPCLK) & ~BIT(0), port + APCIE_PORT_APPCLK); +} + +/* Port @i past port 0: its setup and its lane's, as the boot loader left them. */ +static void t8122_port_save(unsigned int i) +{ + void __iomem *lane = t8122.apcie_phy_ip + i * T8122_APCIE_PHY_IP_STRIDE; + unsigned int w; + + for (w = 0; w < ARRAY_SIZE(t8122_port_words); w++) + t8122.apcie_port[i].port_saved[w] = + readl(t8122.apcie_port[i].port + t8122_port_words[w]); + for (w = 0; w < ARRAY_SIZE(t8122_phy_ip_words); w++) + if (t8122_phy_ip_words[w].off >= T8122_APCIE_PHY_IP_LANE0) + t8122.apcie_port[i].lane_saved[w] = + readl(lane + t8122_phy_ip_words[w].off); + t8122.apcie_port[i].saved = true; +} + +static void t8122_port_lane_restore(unsigned int i) +{ + void __iomem *lane = t8122.apcie_phy_ip + i * T8122_APCIE_PHY_IP_STRIDE; + unsigned int w; + + if (!t8122.apcie_port[i].saved) + return; + for (w = 0; w < ARRAY_SIZE(t8122_phy_ip_words); w++) + if (t8122_phy_ip_words[w].off >= T8122_APCIE_PHY_IP_LANE0) + writel(t8122.apcie_port[i].lane_saved[w], + lane + t8122_phy_ip_words[w].off); +} + static void t8122_apcie_down(struct apple_sleep *s) { - void __iomem *axi600, *clk, *ctrl, *run, *phy0, *port0; - u32 val, b_phy0; + void __iomem *axi600, *clk, *ctrl, *run; + unsigned int i; + u32 val, on; int ret; + for (i = 1; i < T8122_APCIE_PORTS; i++) + t8122.apcie_port[i].saved = false; if (!t8122.apcie_axi || !t8122.apcie_clkgen || !t8122.apcie_phy || - !t8122.apcie_phy_ip || !t8122.apcie_port0) + !t8122.apcie_phy_ip || !t8122.apcie_port[0].port) return; ret = sleep_ps_raise_start(s, T8122_PS_APCIE); @@ -897,25 +1129,27 @@ static void t8122_apcie_down(struct apple_sleep *s) clk = t8122.apcie_clkgen + APCIE_CLK_CTRL; ctrl = t8122.apcie_phy + APCIE_PHY_CTRL; run = t8122.apcie_phy + APCIE_PHY_RUN; - phy0 = t8122.apcie_phy + 0xc000; - port0 = t8122.apcie_port0; + + for (i = T8122_APCIE_PORTS - 1; i; i--) { + void __iomem *phy = t8122.apcie_port[i].phy; + + if (!t8122.apcie_port[i].port) + continue; + on = readl(phy); + if (!(on & T8122_APCIE_PHY_ON)) + continue; + t8122.apcie_axi_ports = readl(axi600); + t8122_port_save(i); + writel(on & ~GENMASK(31, 30), phy); + t8122_port_down(t8122.apcie_port[i].port, phy); + } /* Port 0 PHY + PERST shutdown */ - b_phy0 = readl(phy0); - if (b_phy0 & 0xc0000603U) { - writel(b_phy0 & ~0xc0000000U, phy0); + on = readl(t8122.apcie_port[0].phy); + if (on & T8122_APCIE_PHY_ON) { + writel(on & ~GENMASK(31, 30), t8122.apcie_port[0].phy); writel(readl(axi600) & ~BIT(16), axi600); - writel(readl(port0 + 0x13c) | 0x100, port0 + 0x13c); - writel(0, port0 + APCIE_PORT_PERST); - writel(readl(port0 + 0x13c) & ~0x100, port0 + 0x13c); - writel(readl(phy0) & ~BIT(10), phy0); - writel(readl(phy0) & ~BIT(9), phy0); - writel(readl(phy0) | BIT(4), phy0); - writel(readl(phy0) & ~BIT(0), phy0); - readl_poll_timeout_atomic(phy0, val, !(val & BIT(2)), 1, APCIE_POLL_US); - writel(readl(phy0) & ~BIT(1), phy0); - readl_poll_timeout_atomic(phy0, val, !(val & BIT(3)), 1, APCIE_POLL_US); - writel(readl(port0 + APCIE_PORT_APPCLK) & ~BIT(0), port0 + APCIE_PORT_APPCLK); + t8122_port_down(t8122.apcie_port[0].port, t8122.apcie_port[0].phy); writel(readl(axi600) | BIT(16), axi600); } @@ -952,10 +1186,12 @@ static void t8122_sleep_prepare_regs(struct apple_sleep *s) apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, s->soc->pmu_sleep_reg, s->soc->pmu_sleep_val); t8122.notify_saved[0] = readl(s->spmi + SPMI_NOTIFY_ENABLE); - t8122.notify_saved[1] = readl(t8122.hpm_notify + SPMI_NOTIFY_ENABLE); + if (t8122.hpm_notify) + t8122.notify_saved[1] = readl(t8122.hpm_notify + SPMI_NOTIFY_ENABLE); t8122.notify_staged = true; writel(s->soc->spmi_notify_sleep, s->spmi + SPMI_NOTIFY_ENABLE); - writel(s->soc->spmi_notify_sleep, t8122.hpm_notify + SPMI_NOTIFY_ENABLE); + if (t8122.hpm_notify) + writel(s->soc->spmi_notify_sleep, t8122.hpm_notify + SPMI_NOTIFY_ENABLE); } static void t8122_sleep_undo_regs(struct apple_sleep *s) @@ -964,7 +1200,8 @@ static void t8122_sleep_undo_regs(struct apple_sleep *s) if (!t8122.notify_staged) return; writel(t8122.notify_saved[0], s->spmi + SPMI_NOTIFY_ENABLE); - writel(t8122.notify_saved[1], t8122.hpm_notify + SPMI_NOTIFY_ENABLE); + if (t8122.hpm_notify) + writel(t8122.notify_saved[1], t8122.hpm_notify + SPMI_NOTIFY_ENABLE); t8122.notify_staged = false; } @@ -1169,24 +1406,6 @@ static int t8122_raise_apcie(struct apple_sleep *s) { 0x2200, 0x00081f55 }, { 0x2224, 0x00000818 }, { 0x2228, 0x00000b18 }, { 0x224c, 0x1f800096 }, { 0x22e4, 0x01024201 }, { 0x22fc, 0x030b40b4 }, }; - static const struct { u32 off, val; } phy_ip[] = { - { 0x0028, 0x00800800 }, { 0x006c, 0x0000004c }, { 0x0080, 0x80010001 }, { 0x0094, 0x00000001 }, - { 0x1204, 0x0000004c }, { 0x1000, 0x2a2383c8 }, { 0x102c, 0x04454447 }, { 0x5080, 0x294d8190 }, - { 0x5088, 0x00560800 }, { 0x508c, 0x294d8190 }, { 0x5094, 0x00060800 }, { 0x50a0, 0x00554800 }, - { 0x50ac, 0x00554800 }, { 0x50b8, 0x00554800 }, { 0x5200, 0x0008155e }, { 0x5224, 0x00000818 }, - { 0x5240, 0x04010f69 }, { 0x52b0, 0x10300811 }, { 0x52fc, 0x03078078 }, { 0x531c, 0x00000002 }, - { 0x608c, 0x65918280 }, { 0x6224, 0x00000818 }, { 0x6228, 0x00000b18 }, { 0x6230, 0x0001e80f }, - { 0x624c, 0x1f80009d }, { 0xa000, 0x080001aa }, { 0xa008, 0x0000000a }, { 0xa00c, 0x0000000a }, - { 0xa010, 0x0000000a }, { 0xa014, 0x0000000a }, { 0xa038, 0x00000271 }, { 0xa040, 0x000000c8 }, - { 0xa04c, 0x00000bff }, { 0xa050, 0x00000001 }, { 0xa068, 0x00000c41 }, { 0xa070, 0x55b00820 }, - { 0xa084, 0x3e8f4240 }, { 0xa088, 0x00000fff }, { 0xa10c, 0x000f4240 }, { 0xa11c, 0x000f30dc }, - { 0xa178, 0x00300000 }, { 0xa0c0, 0x14e051f8 }, { 0xa1a4, 0x00000901 }, { 0xa1b0, 0x00000000 }, - { 0xa1b8, 0x00000000 }, { 0xa1e8, 0x00100000 }, { 0x903c, 0x018c3509 }, { 0x90d8, 0x0102068d }, - { 0x90dc, 0x12004408 }, { 0x91e0, 0x00002008 }, { 0x9288, 0x0801172a }, { 0x9280, 0xcccccccc }, - { 0x9000, 0x1e000000 }, { 0x9280, 0xccc8ccc1 }, { 0x928c, 0x00000003 }, { 0xb02c, 0x01980062 }, - { 0xb098, 0x05028000 }, { 0xc01c, 0x000006a1 }, { 0xc080, 0x540c6831 }, { 0xc08c, 0x10be0c1f }, - { 0xc0a0, 0x021a1780 }, { 0xc0ec, 0x00000000 }, { 0xd008, 0x0000000c }, { 0xd014, 0x04840408 }, - }; void __iomem *ctrl, *run, *ready, *common; unsigned int i; u32 val; @@ -1206,6 +1425,10 @@ static int t8122_raise_apcie(struct apple_sleep *s) for (i = 0; i < ARRAY_SIZE(axi); i++) writel(axi[i].val, t8122.apcie_axi + axi[i].off); + /* Each port has its own bits here: the board's, where it has more than port 0. */ + for (i = 1; i < T8122_APCIE_PORTS; i++) + if (t8122.apcie_port[i].saved) + writel(t8122.apcie_axi_ports, t8122.apcie_axi + 0x600); writel(0, t8122.apcie_rc + APCIE_RC_ZERO); @@ -1238,8 +1461,10 @@ static int t8122_raise_apcie(struct apple_sleep *s) writel(readl(ctrl) & ~T8122_APCIE_PHY_CTRL_RESET, ctrl); writel(readl(run) | APCIE_PHY_RUN_EN, run); - for (i = 0; i < ARRAY_SIZE(phy_ip); i++) - writel(phy_ip[i].val, t8122.apcie_phy_ip + phy_ip[i].off); + for (i = 0; i < ARRAY_SIZE(t8122_phy_ip_words); i++) + writel(t8122_phy_ip_words[i].val, t8122.apcie_phy_ip + t8122_phy_ip_words[i].off); + for (i = 1; i < T8122_APCIE_PORTS; i++) + t8122_port_lane_restore(i); writel(readl(run) | APCIE_PHY_RUN_EN2, run); ret = readl_poll_timeout_atomic(ready, val, val & BIT(0), 1, APCIE_POLL_US); @@ -1254,6 +1479,18 @@ static int t8122_raise_apcie(struct apple_sleep *s) val & BIT(0), 1, APCIE_POLL_US); } +/* The port's clock, its PERST# released, and its interrupts routed to the host. */ +static void t8122_port_start(void __iomem *port) +{ + u32 val; + + writel(0x00100101, port + APCIE_PORT_APPCLK); + writel(1, port + APCIE_PORT_PERST); + readl_poll_timeout_atomic(port + 0x804, val, val & BIT(0), 1, APCIE_POLL_US); + writel(0x00100001, port + APCIE_PORT_APPCLK); + writel(3, port + T8122_APCIE_PORT_INTR2AXI); +} + static void t8122_port0_wake(struct apple_sleep *s) { static const struct { u32 off, val; } port0_init[] = { @@ -1265,24 +1502,71 @@ static void t8122_port0_wake(struct apple_sleep *s) { 0x144, 0x00253770 }, { 0x21c, 0x00000000 }, { 0x834, 0x00000000 }, { 0x83c, 0x00000000 }, { 0x140, 0x00000011 }, { 0x8a4, 0x00002020 }, }; - void __iomem *port = t8122.apcie_port0; + void __iomem *port = t8122.apcie_port[0].port; unsigned int i; - u32 val; writel(readl(t8122.apcie_axi + 0x600) & ~BIT(16), t8122.apcie_axi + 0x600); for (i = 0; i < ARRAY_SIZE(port0_init); i++) writel(port0_init[i].val, port + port0_init[i].off); + t8122_port_start(port); + + writel(readl(t8122.apcie_axi + 0x600) | BIT(16), t8122.apcie_axi + 0x600); + + sleep_apcie_port_clock(port, t8122.apcie_port[0].phy); +} + +/* + * The ports past port 0: their setup as before the sleep and their clocks, for their DART's + * and host's restore. The host trains the link again, with the interrupts and the SID map. + */ +static int t8122_extra_port_wake(unsigned int i) +{ + void __iomem *port = t8122.apcie_port[i].port; + unsigned int w; + u32 val; + int ret; + + /* Turn APPCLK on without reading the port. Restore it only once its PHY answers. */ writel(0x00100101, port + APCIE_PORT_APPCLK); + ret = sleep_apcie_phy_clock(t8122.apcie_port[i].phy); + if (ret) + return ret; + for (w = 0; w < ARRAY_SIZE(t8122_port_words); w++) + writel(t8122.apcie_port[i].port_saved[w], port + t8122_port_words[w]); + writel(~0U, port + APCIE_PORT_INTMSK); + writel(~0U, port + APCIE_PORT_INTSTAT); writel(1, port + APCIE_PORT_PERST); - readl_poll_timeout_atomic(port + 0x804, val, val & BIT(0), 1, APCIE_POLL_US); + ret = readl_poll_timeout_atomic(port + 0x804, val, val & BIT(0), 1, APCIE_POLL_US); + if (ret) + return ret; writel(0x00100001, port + APCIE_PORT_APPCLK); - writel(3, port + 0x4020); + writel(3, port + T8122_APCIE_PORT_INTR2AXI); + return 0; +} - writel(readl(t8122.apcie_axi + 0x600) | BIT(16), t8122.apcie_axi + 0x600); +static void t8122_extra_ports_wake(struct apple_sleep *s, int rc_ret) +{ + unsigned int i; + int ret; - sleep_apcie_port_clock(port, t8122.apcie_phy + 0xc000); + for (i = 1; i < T8122_APCIE_PORTS; i++) { + if (!t8122.apcie_port[i].port) + continue; + /* The entry didn't take it down (a failed raise, a lane that was off). */ + if (!t8122.apcie_port[i].saved) + ret = -ENODATA; + else + ret = rc_ret ?: t8122_extra_port_wake(i); + if (!ret) + continue; + apple_dart_host_wake_failed(t8122.apcie_port[i].dart); + set_bit(i, &t8122.apcie_failed); + dev_err(s->dev, + "PCIe port %u failed to wake: %d, leaving it and its DART offline\n", + i, ret); + } } static void t8122_sleep_wake(struct apple_sleep *s) @@ -1355,8 +1639,11 @@ static void t8122_sleep_wake(struct apple_sleep *s) } if (t8122.apcie_axi) { - if (!t8122_raise_apcie(s) && t8122.apcie_port0 && t8122.apcie_phy) + int ret = t8122_raise_apcie(s); + + if (!ret && t8122.apcie_port[0].port && t8122.apcie_phy) t8122_port0_wake(s); + t8122_extra_ports_wake(s, ret); sleep_ps_raise_finish(s, T8122_PS_APCIE); sleep_ps_raise_finish(s, T8122_PS_ANS); sleep_ps_raise_finish(s, T8122_PS_APCIE_GP); @@ -1409,6 +1696,7 @@ static const struct apple_sleep_ops t8122_sleep_ops = { .aic_ack = aic_sleep_ack, .aic_restore = aic_sleep_restore, .map_regs = t8122_sleep_map_regs, + .begin = t8122_sleep_begin, .display_off = t8122_sleep_display_off, .prepare_regs = t8122_sleep_prepare_regs, .undo_regs = t8122_sleep_undo_regs, diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 0a5a506d98db5..50c63e00e08c6 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -4522,20 +4522,11 @@ static void sleep_apcie_port_stop_once(struct apple_sleep *s) } } -/* - * A port that no host sets up again after a real sleep (one without the managed radio - * contract) gets its clocks back as the loader set them at boot: the DART in its unit is - * clocked by the port and its resume reads it. PERST# and the link stay as the wake left them. - * It stops before the reset release when a clock doesn't ack: the wake's report names the port. - */ -static int sleep_apcie_port_clock(void __iomem *port, void __iomem *phy) +static int sleep_apcie_phy_clock(void __iomem *phy) { u32 val; int ret; - if (!port || !phy) - return 0; - writel(readl(port + APCIE_PORT_APPCLK) | APCIE_PORT_APPCLK_EN, port + APCIE_PORT_APPCLK); writel(readl(phy) & ~(APCIE_PHY_CTRL_CLK0REQ | APCIE_PHY_CTRL_CLK1REQ), phy); writel(readl(phy) | APCIE_PHY_CTRL_CLK0REQ, phy); ret = readl_poll_timeout_atomic(phy, val, val & APCIE_PHY_CTRL_CLK0ACK, 1, APCIE_POLL_US); @@ -4551,6 +4542,20 @@ static int sleep_apcie_port_clock(void __iomem *port, void __iomem *phy) return 0; } +/* + * A port that no host sets up again after a real sleep (one without the managed radio + * contract) gets its clocks back as the loader set them at boot: the DART in its unit is + * clocked by the port and its resume reads it. PERST# and the link stay as the wake left them. + * It stops before the reset release when a clock doesn't ack: the wake's report names the port. + */ +static int sleep_apcie_port_clock(void __iomem *port, void __iomem *phy) +{ + if (!port || !phy) + return 0; + writel(readl(port + APCIE_PORT_APPCLK) | APCIE_PORT_APPCLK_EN, port + APCIE_PORT_APPCLK); + return sleep_apcie_phy_clock(phy); +} + /* A poll here only paces the steps: the power goes next whatever it reads. */ static void sleep_apcie_phy_off(struct apple_sleep *s) { diff --git a/include/linux/soc/apple/dart.h b/include/linux/soc/apple/dart.h index 04b3429931e0b..f4d217a21e227 100644 --- a/include/linux/soc/apple/dart.h +++ b/include/linux/soc/apple/dart.h @@ -161,6 +161,13 @@ static inline void *apple_dart_dcp_scanout_alloc(struct device *dev, size_t size } #endif +/* The host port failed to wake. Gate commands without accessing the DART or sleeping. */ +#if IS_REACHABLE(CONFIG_APPLE_DART) +void apple_dart_host_wake_failed(struct device *dev); +#else +static inline void apple_dart_host_wake_failed(struct device *dev) {} +#endif + /* * The tunneled PCIe IOMMU shares the port clock. Stop issuing commands * before that clock is gated, and allow them again once it is back. diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index b61473131dcf9..729ffe9581ee9 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -292,6 +292,9 @@ void apple_sleep_apcie_ports_owned(bool owned); /* One APCIe port's host has that port back after a real sleep, so the next commit may stop it. */ void apple_sleep_apcie_port_up(unsigned int idx); +/* Extra T8122 ports whose clocks did not return and must stay disconnected. */ +unsigned long apple_sleep_t8122_failed_ports(void); + /* * T6050: from the radio host's noirq suspend, with its DART saved and gated: the port's link * shutdown. An error means nothing was written and the sleep must be refused. @@ -575,6 +578,11 @@ static inline void apple_sleep_apcie_port_up(unsigned int idx) { } +static inline unsigned long apple_sleep_t8122_failed_ports(void) +{ + return 0; +} + static inline bool apple_sleep_radio_port_ready(void) { return false; diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index d5adc30ef8d86..cc08990c17d18 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -413,7 +413,7 @@ def function(source, name): bool hib_l1ss_closing; struct notifier_block hib_l1ss_pci_nb; struct pci_host_bridge *bridge; - bool power_retained, kernel_init, tunnel_cold_init; + bool power_retained, kernel_init, tunnel_cold_init, t8122_native; bool bus_stopped, resume_failed, link_kept; struct apple_pcie_pd *pds; unsigned int num_pds, pd_held; @@ -428,6 +428,7 @@ def function(source, name): static bool suspend_via_firmware; static bool link_healthy = true; static unsigned int radio_prepare_calls, radio_complete_calls, warn_calls; +static unsigned long t8122_failed_ports; static u32 readl(const void *addr) { u32 v; @@ -461,6 +462,10 @@ def function(source, name): (void)pcie; return link_healthy; } +static unsigned long apple_sleep_t8122_failed_ports(void) +{ + return t8122_failed_ports; +} static int apple_radio_prepare(struct device *dev) { (void)dev; @@ -1019,6 +1024,15 @@ def function(source, name): apple_pcie_release_domains(&pcie); assert(pcie.pd_held == 0 && dev.genpd_nb == NULL && dev.remove_calls == 1); + /* A port lost in a T8122 wake refuses the T8122 host's next sleep, and no other host's. */ + reset_host(&dev, &pcie, &pd, t8132, &cio_dart); + t8122_failed_ports = 2; + assert(apple_pcie_prepare(&dev) == 0 && radio_prepare_calls == 1); + pcie.t8122_native = true; + assert(apple_pcie_prepare(&dev) == -EIO && radio_prepare_calls == 1); + t8122_failed_ports = 0; + assert(apple_pcie_prepare(&dev) == 0 && radio_prepare_calls == 2); + /* Non-retained T8132 cio_dart host (kernel_init) also passes prepare and holds domain. */ reset_host(&dev, &pcie, &pd, t8132, &cio_dart); assert(apple_pcie_can_keep_link(&pcie)); diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index d0aee84f8ffe4..7adbd56a08538 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -119,6 +119,7 @@ def mutations(flag, names, base, expect=KILL): 'skip-hold-raise-ps', 'lower-domain-on-s2idle-while-held', ), out()), + 't8122-apcie-wake-tests.py': [([], PASS)], 'm3-stamp-admission-tests.py': [(out(), PASS), (out() + ['--mutation', 'live-reseed'], KILL)], 'hardware-defaults-tests.py': [([], PASS)], diff --git a/tools/asahi/t8122-apcie-wake-tests.py b/tools/asahi/t8122-apcie-wake-tests.py new file mode 100755 index 0000000000000..d24fee08a3085 --- /dev/null +++ b/tools/asahi/t8122-apcie-wake-tests.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Exercise T8122 extra-port wake with simulated PHY and port timeouts.""" + +from pathlib import Path +import re +import resource +import subprocess +import sys +import tempfile + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from sleep_test_utils import function # noqa: E402 + +ROOT = Path(__file__).resolve().parents[2] +SLEEP = (ROOT / "drivers/soc/apple/sleep.c").read_text() +T8122 = (ROOT / "drivers/soc/apple/sleep-t8122.h").read_text() +DART = (ROOT / "drivers/iommu/apple-dart.c").read_text() +PCIE = (ROOT / "drivers/pci/controller/pcie-apple.c").read_text() + +DEFINES = re.findall( + r"^#define (?:APCIE_PHY_CTRL_CLK\w+|APCIE_PHY_CTRL_RESET|" + r"APCIE_PORT_PHY_EN\d|APCIE_PORT_APPCLK|APCIE_PORT_INTMSK|APCIE_PORT_INTSTAT|" + r"APCIE_PORT_PERST|APCIE_POLL_US|T8122_APCIE_PORTS|T8122_APCIE_PORT_INTR2AXI)\b.*$", + SLEEP + T8122, re.M, +) +PORT_WORDS = re.search(r"^static const u16 t8122_port_words\[\].*?^\};", T8122, re.M | re.S) + +FIXTURE = r""" +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef uint16_t u16; +#define __iomem +#define BIT(n) (1UL << (n)) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define WARN_ON_ONCE(x) (x) +#define guard(type) fake_guard +#define fake_guard(lock) ((void)(lock)) +#define PCI_SLOT(devfn) (((devfn) >> 3) & 31) +#define set_bit(n, p) (*(p) |= BIT(n)) +#define dev_err(dev, ...) ((void)(dev)) + +/* CONSTANTS */ + +struct apple_dart { + bool host_resumes, commands_gated; + int lock; +}; +struct device { struct apple_dart *dart; }; +struct apple_sleep { struct device *dev; }; +struct apple_pcie { bool t8122_native; }; +struct pci_dev { unsigned int devfn; }; +struct pci_bus { struct pci_bus *parent; struct pci_dev *self; }; +struct { + struct { + void *port, *phy; + struct device *dart; + bool saved; + u32 port_saved[ARRAY_SIZE(t8122_port_words)]; + } apcie_port[T8122_APCIE_PORTS]; + unsigned long apcie_failed; +} t8122; + +static u32 port[3][0x4024 / 4], phy[3]; +static unsigned int port_reads[3], port_writes[3]; +static unsigned long missing_phy_ack, missing_ready; +static struct apple_dart darts[3]; +static struct device devices[3]; +static struct apple_sleep sleep_state; + +static u32 readl(void *addr) +{ + for (unsigned int i = 1; i < 3; i++) { + if (addr == &phy[i]) { + u32 value = phy[i]; + if (!(missing_phy_ack & BIT(i))) { + if (value & APCIE_PHY_CTRL_CLK0REQ) + value |= APCIE_PHY_CTRL_CLK0ACK; + if (value & APCIE_PHY_CTRL_CLK1REQ) + value |= APCIE_PHY_CTRL_CLK1ACK; + } + return value; + } + if (addr == &port[i][0x804 / 4]) { + port_reads[i]++; + return !(missing_ready & BIT(i)); + } + } + assert(false && "unexpected register read"); + return 0; +} + +static void writel(u32 value, void *addr) +{ + for (unsigned int i = 1; i < 3; i++) { + if (addr == &phy[i]) { + phy[i] = value; + return; + } + if ((uintptr_t)addr >= (uintptr_t)port[i] && + (uintptr_t)addr < (uintptr_t)(port[i] + ARRAY_SIZE(port[i]))) { + port_writes[i]++; + *(u32 *)addr = value; + return; + } + } + assert(false && "unexpected register write"); +} + +#define readl_poll_timeout_atomic(addr, val, cond, delay, timeout) \ + ((val) = readl(addr), (cond) ? 0 : -ETIMEDOUT) +#define dev_get_drvdata(dev) ((dev)->dart) + +/* ACTUAL_FUNCTIONS */ + +static void reset(void) +{ + memset(&t8122, 0, sizeof(t8122)); + memset(port, 0, sizeof(port)); + memset(phy, 0, sizeof(phy)); + memset(darts, 0, sizeof(darts)); + memset(port_reads, 0, sizeof(port_reads)); + memset(port_writes, 0, sizeof(port_writes)); + missing_phy_ack = missing_ready = 0; + for (unsigned int i = 1; i < 3; i++) { + devices[i].dart = &darts[i]; + t8122.apcie_port[i].port = port[i]; + t8122.apcie_port[i].phy = &phy[i]; + t8122.apcie_port[i].dart = &devices[i]; + t8122.apcie_port[i].saved = true; + t8122.apcie_port[i].port_saved[0] = 0xcafe; + } + t8122.apcie_port[0].saved = true; /* The extra-port path never handles this slot. */ +} + +static void check_phy_failure(void) +{ + struct apple_pcie host = { .t8122_native = true }, other = { 0 }; + struct pci_dev root_port = { .devfn = 1 << 3 }; + struct pci_dev downstream_bridge = { .devfn = 0 }; + struct pci_bus root = { 0 }, bus = { &root, &root_port }; + struct pci_bus nested = { &bus, &downstream_bridge }; + + reset(); + missing_phy_ack = BIT(1); + t8122_extra_ports_wake(&sleep_state, 0); + assert(port_reads[1] == 0 && port_writes[1] == 1); + assert(darts[1].commands_gated && darts[1].host_resumes); + assert(port_reads[2] == 1 && !darts[2].commands_gated); + assert(port[2][t8122_port_words[0] / 4] == 0xcafe); + assert(port[2][T8122_APCIE_PORT_INTR2AXI / 4] == 3); + assert(apple_sleep_t8122_failed_ports() == BIT(1)); + assert(apple_pcie_t8122_config_blocked(&host, &root, 1 << 3)); + assert(apple_pcie_t8122_config_blocked(&host, &nested, 0)); + assert(!apple_pcie_t8122_config_blocked(&host, &root, 0)); + assert(!apple_pcie_t8122_config_blocked(&host, &root, 2 << 3)); + assert(!apple_pcie_t8122_config_blocked(&other, &root, 1 << 3)); +} + +int main(void) +{ + check_phy_failure(); + reset(); + missing_ready = BIT(2); + t8122_extra_ports_wake(&sleep_state, 0); + assert(port_reads[2] == 1 && port[2][T8122_APCIE_PORT_INTR2AXI / 4] == 0); + assert(darts[2].commands_gated && apple_sleep_t8122_failed_ports() == BIT(2)); + assert(!darts[1].commands_gated); + + reset(); + t8122_extra_ports_wake(&sleep_state, -ETIMEDOUT); + assert(!port_writes[1] && !port_writes[2] && !port_reads[1] && !port_reads[2]); + assert(darts[1].commands_gated && darts[2].commands_gated); + assert(apple_sleep_t8122_failed_ports() == (BIT(1) | BIT(2))); + + reset(); + t8122.apcie_port[1].saved = false; + t8122_extra_ports_wake(&sleep_state, 0); + assert(!port_writes[1] && !port_reads[1] && darts[1].commands_gated); + assert(!darts[2].commands_gated && apple_sleep_t8122_failed_ports() == BIT(1)); + + reset(); + t8122.apcie_port[2].port = NULL; /* Not enabled on this board. */ + t8122.apcie_port[2].saved = false; + t8122_extra_ports_wake(&sleep_state, -ETIMEDOUT); + assert(!darts[2].commands_gated && apple_sleep_t8122_failed_ports() == BIT(1)); + puts("T8122 PHY, ready, root-complex timeouts and an unsaved port: PASS"); + return 0; +} +""" + +ACTUAL = ( + function(SLEEP, "sleep_apcie_phy_clock"), + function(T8122, "apple_sleep_t8122_failed_ports"), + function(DART, "apple_dart_host_wake_failed"), + function(T8122, "t8122_extra_port_wake"), + function(T8122, "t8122_extra_ports_wake"), + function(PCIE, "apple_pcie_t8122_config_blocked"), +) + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +constants = "\n".join(DEFINES) + "\n" + PORT_WORDS.group() +with tempfile.TemporaryDirectory(prefix="t8122-apcie-") as tmp: + c_file, test = Path(tmp) / "test.c", Path(tmp) / "test" + c_file.write_text(FIXTURE.replace("/* CONSTANTS */", constants) + .replace("/* ACTUAL_FUNCTIONS */", "\n".join(ACTUAL))) + subprocess.run(["cc", "-std=gnu11", "-Wall", "-Werror", str(c_file), "-o", str(test)], + check=True) + subprocess.run([str(test)], check=True)