diff --git a/.github/workflows/dt-checks.yml b/.github/workflows/dt-checks.yml index a887f663e6955..1c2d88af7bc3e 100644 --- a/.github/workflows/dt-checks.yml +++ b/.github/workflows/dt-checks.yml @@ -50,7 +50,13 @@ jobs: - name: Install the tools run: | - sudo apt-get update -q + # Avoid the hosted runner's Azure mirror after repeated DNS failures. + # Keep signed Ubuntu archive/security suites and fail on update errors. + if [ -f /etc/apt/apt-mirrors.txt ]; then + printf '%s\n' 'https://archive.ubuntu.com/ubuntu/' 'https://security.ubuntu.com/ubuntu/' | \ + sudo tee /etc/apt/apt-mirrors.txt >/dev/null + fi + sudo apt-get update -q -o APT::Update::Error-Mode=any sudo apt-get install -y -q flex bison bc libyaml-dev clang-$CLANG_VERSION lld-$CLANG_VERSION llvm-$CLANG_VERSION swig device-tree-compiler echo "/usr/lib/llvm-$CLANG_VERSION/bin" >> "$GITHUB_PATH" pip install --quiet dtschema yamllint pylibfdt diff --git a/.github/workflows/host-tests.yml b/.github/workflows/host-tests.yml index fd665f948d991..cc947aaf986ea 100644 --- a/.github/workflows/host-tests.yml +++ b/.github/workflows/host-tests.yml @@ -61,7 +61,13 @@ jobs: command -v fdtget >/dev/null || need="$need device-tree-compiler" dpkg -s "libclang-rt-$CLANG_VERSION-dev" >/dev/null 2>&1 || need="$need libclang-rt-$CLANG_VERSION-dev" if [ -n "$need" ]; then - sudo apt-get update -q + # Avoid the hosted runner's Azure mirror after repeated DNS failures. + # Keep signed Ubuntu archive/security suites and fail on update errors. + if [ -f /etc/apt/apt-mirrors.txt ]; then + printf '%s\n' 'https://archive.ubuntu.com/ubuntu/' 'https://security.ubuntu.com/ubuntu/' | \ + sudo tee /etc/apt/apt-mirrors.txt >/dev/null + fi + sudo apt-get update -q -o APT::Update::Error-Mode=any sudo apt-get install -y -q $need fi # The tests call clang, ld.lld, cc and gcc by name. diff --git a/Documentation/devicetree/bindings/pci/apple,pcie.yaml b/Documentation/devicetree/bindings/pci/apple,pcie.yaml index a9fd4822fd127..39ec5dda86d6e 100644 --- a/Documentation/devicetree/bindings/pci/apple,pcie.yaml +++ b/Documentation/devicetree/bindings/pci/apple,pcie.yaml @@ -99,6 +99,9 @@ properties: - items: - const: apple,t8112-pciec - const: apple,t8103-pciec + - items: + - const: apple,t8122-pciec + - const: apple,t8103-pciec - const: apple,t6000-pciec - const: apple,t6050-pciec - items: @@ -522,6 +525,15 @@ allOf: - const: debug - const: ltssm - enum: [intr2axi, oe-fabric] + - items: + - const: config + - const: rc + - const: port0 + - const: fabric + - const: debug + - const: ltssm + - const: oe-fabric + - const: intr2axi - items: - const: config - const: rc @@ -547,6 +559,27 @@ allOf: minItems: 3 maxItems: 3 allOf: + - if: + properties: + compatible: + items: + - const: apple,t8122-pciec + - const: apple,t8103-pciec + then: + properties: + reg: + minItems: 8 + maxItems: 8 + reg-names: + items: + - const: config + - const: rc + - const: port0 + - const: fabric + - const: debug + - const: ltssm + - const: oe-fabric + - const: intr2axi - if: properties: compatible: diff --git a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml index 01592811223d2..df1dc71379521 100644 --- a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml +++ b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml @@ -52,7 +52,7 @@ properties: PCIe adapter configuration space of the USB4 router. Present where the root complex exposes the adapter separately, so that the tunnel can be configured before a PCIe-C link is brought up. - - description: MxWrap co-processor control registers, on T8132 and T6050 + - description: MxWrap co-processor control registers, on T6030, T8122, T8132 and T6050 - description: PCIe router AP firmware registers, on T6050 reg-names: @@ -108,6 +108,14 @@ properties: apple,pcie-tunnel is present, the domain after them powers its PCIe-C controller and is enabled only after the USB4 PCIe tunnel is active. + post-init-providers: + $ref: /schemas/types.yaml#/definitions/phandle-array + maxItems: 4 + description: + Power-domain suppliers managed explicitly by the router, rather than + required before it can probe, to break their initialization dependency + cycle. On J613 these are the four power domains listed above. + thunderbolt-switch: type: boolean description: diff --git a/arch/arm64/boot/dts/apple/t8122-j613.dts b/arch/arm64/boot/dts/apple/t8122-j613.dts index 1c503729a3101..b35346094bcbc 100644 --- a/arch/arm64/boot/dts/apple/t8122-j613.dts +++ b/arch/arm64/boot/dts/apple/t8122-j613.dts @@ -10,6 +10,7 @@ /dts-v1/; #include "t8122.dtsi" +#include "t8122-usb4.dtsi" #include "t8122-j613-j615.dtsi" #include @@ -17,6 +18,15 @@ compatible = "apple,j613", "apple,t8122", "apple,arm-platform"; model = "Apple MacBook Air (13-inch, M3, 2024)"; + aliases { + usb4-0-pcie = &usb4_0_pcie; + usb4-0-pcie-port = &usb4_0_pcie_port; + usb4-1-pcie = &usb4_1_pcie; + usb4-1-pcie-port = &usb4_1_pcie_port; + usb4-0-pcie-adapter = &usb4_0_acio; + usb4-1-pcie-adapter = &usb4_1_acio; + }; + /* * Fanless: every die sensor throttles both clusters, E weighted 3:1 * and capped at state 3. Trips as on the J700; sustainable-power from @@ -432,3 +442,6 @@ /* Per-device factory data from this Mac's own iBoot system container. */ firmware-name = "apple/mesacal-j613.bin"; }; + +/* Older Stage 2 loaders prune the shared ATC0 suppliers of this unused node. */ +/delete-node/ &usb_otg0; diff --git a/arch/arm64/boot/dts/apple/t8122-usb4.dtsi b/arch/arm64/boot/dts/apple/t8122-usb4.dtsi new file mode 100644 index 0000000000000..ce69d7ec74f9c --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8122-usb4.dtsi @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* J613 ADT PCIe-C windows; DP tunnelling deliberately omitted. */ +/ { + usb4_0_pcie_tunnel: usb4-pcie-tunnel-0 { + #address-cells = <2>; + #size-cells = <2>; + ranges; + nonposted-mmio; + /* Preserve the SoC DMA window after moving the virtual container. */ + dma-ranges = <0 0 0 0 0xffffffff 0xffffc000>; + + usb4_0_pcie_dart: iommu@721008000 { + compatible = "apple,t8122-dart", "apple,t8110-dart"; + reg = <0x7 0x21008000 0x0 0x4000>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* No bootloader fills this one; the tunnel's cold init needs it. */ + apple,tunable = + <0x20c 0x800001b7 0x800001b7>, + <0x220 0xf0f0f 0xf0f0f>, + <0x224 0xffffff 0x80808>, + <0x300 0x1f31 0x1>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x318 0x1f31 0x1>, + <0x320 0x3ffffffc 0x1f00000>, + <0x328 0x3ffffffc 0x1f0bffc>; + }; + + usb4_0_pcie: pcie@730000000 { + compatible = "apple,t8122-pciec", "apple,t8103-pciec"; + device_type = "pci"; + reg = <0x7 0x30000000 0x0 0x10000000>, + <0x7 0x20000000 0x0 0x4000>, + <0x7 0x21000000 0x0 0x8000>, + <0x7 0x20004000 0x0 0x4000>, + <0x7 0x20200000 0x0 0x4000>, + <0x7 0x21010000 0x0 0x4000>, + <0x7 0x20008000 0x0 0x4000>, + <0x7 0x2100c000 0x0 0x4000>; + reg-names = "config", "rc", "port0", "fabric", + "debug", "ltssm", "oe-fabric", "intr2axi"; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&ps_atc0_cio_pcie>; + resets = <&ps_atc0_pcie>; + apple,pciec-kernel-init; + /* + * m1n1 fills the rc, fabric and debug tunables through the + * usb4-0-pcie alias, but not this one. + */ + apple,tunable-oe-fabric = + <0x4 0x70007 0x40004>, + <0x8 0x1 0x1>, + <0xc 0x1 0x1>, + <0x10 0x801f 0x4>, + <0x18 0x801f 0x4>, + <0x20 0x801f 0x4>; + apple,msi-vector-base = <32>; + msi-controller; + msi-parent = <&usb4_0_pcie>; + msi-ranges = <&aic AIC_IRQ 1107 IRQ_TYPE_EDGE_RISING 32>; + iommu-map = <0x0 &usb4_0_pcie_dart 0x0 0x40>; + iommu-map-mask = <0x3f>; + bus-range = <0x0 0x7f>; + #address-cells = <3>; + #size-cells = <2>; + ranges = <0x43000000 0x8 0x0 + 0x8 0x0 0x2 0x0>, + <0x02000000 0x0 0x100000 + 0xa 0x100000 0x0 0x3ff00000>, + <0x42000000 0x0 0x40000000 + 0xa 0x40000000 0x0 0x40000000>; + + usb4_0_pcie_port: pci@0,0 { + device_type = "pci"; + reg = <0x0 0x0 0x0 0x0 0x0>; + external-facing; + /* apple,tunable is filled by the bootloader */ + #address-cells = <3>; + #size-cells = <2>; + ranges; + interrupt-controller; + #interrupt-cells = <1>; + interrupt-map-mask = <0 0 0 7>; + interrupt-map = <0 0 0 1 &usb4_0_pcie_port 0 0 0 0>, + <0 0 0 2 &usb4_0_pcie_port 0 0 0 1>, + <0 0 0 3 &usb4_0_pcie_port 0 0 0 2>, + <0 0 0 4 &usb4_0_pcie_port 0 0 0 3>; + }; + }; + }; +}; + +&usb4_0_acio { + power-domains = <&ps_atc0_cio>, <&ps_atc0_cio_pcie>, + <&ps_atc0_cio_usb>, <&ps_atc0_pcie>; + post-init-providers = <&ps_atc0_cio>, <&ps_atc0_cio_pcie>, + <&ps_atc0_cio_usb>, <&ps_atc0_pcie>; + apple,pcie-tunnel = <&usb4_0_pcie_tunnel>; +}; +/ { + usb4_1_pcie_tunnel: usb4-pcie-tunnel-1 { + #address-cells = <2>; + #size-cells = <2>; + ranges; + nonposted-mmio; + /* Preserve the SoC DMA window after moving the virtual container. */ + dma-ranges = <0 0 0 0 0xffffffff 0xffffc000>; + + usb4_1_pcie_dart: iommu@b21008000 { + compatible = "apple,t8122-dart", "apple,t8110-dart"; + reg = <0xb 0x21008000 0x0 0x4000>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* No bootloader fills this one; the tunnel's cold init needs it. */ + apple,tunable = + <0x20c 0x800001b7 0x800001b7>, + <0x220 0xf0f0f 0xf0f0f>, + <0x224 0xffffff 0x80808>, + <0x300 0x1f31 0x1>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x318 0x1f31 0x1>, + <0x320 0x3ffffffc 0x1f00000>, + <0x328 0x3ffffffc 0x1f0bffc>; + }; + + usb4_1_pcie: pcie@b30000000 { + compatible = "apple,t8122-pciec", "apple,t8103-pciec"; + device_type = "pci"; + reg = <0xb 0x30000000 0x0 0x10000000>, + <0xb 0x20000000 0x0 0x4000>, + <0xb 0x21000000 0x0 0x8000>, + <0xb 0x20004000 0x0 0x4000>, + <0xb 0x20200000 0x0 0x4000>, + <0xb 0x21010000 0x0 0x4000>, + <0xb 0x20008000 0x0 0x4000>, + <0xb 0x2100c000 0x0 0x4000>; + reg-names = "config", "rc", "port0", "fabric", + "debug", "ltssm", "oe-fabric", "intr2axi"; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&ps_atc1_cio_pcie>; + resets = <&ps_atc1_pcie>; + apple,pciec-kernel-init; + /* + * m1n1 fills the rc, fabric and debug tunables through the + * usb4-1-pcie alias, but not this one. + */ + apple,tunable-oe-fabric = + <0x4 0x70007 0x40004>, + <0xc 0x1 0x1>, + <0xc 0x1 0x1>, + <0x10 0x801f 0x4>, + <0x18 0x801f 0x4>, + <0x20 0x801f 0x4>; + apple,msi-vector-base = <64>; + msi-controller; + msi-parent = <&usb4_1_pcie>; + msi-ranges = <&aic AIC_IRQ 1139 IRQ_TYPE_EDGE_RISING 32>; + iommu-map = <0x0 &usb4_1_pcie_dart 0x0 0x40>; + iommu-map-mask = <0x3f>; + bus-range = <0x0 0x7f>; + #address-cells = <3>; + #size-cells = <2>; + ranges = <0x43000000 0xc 0x0 + 0xc 0x0 0x2 0x0>, + <0x02000000 0x0 0x100000 + 0xe 0x100000 0x0 0x3ff00000>, + <0x42000000 0x0 0x40000000 + 0xe 0x40000000 0x0 0x40000000>; + + usb4_1_pcie_port: pci@0,0 { + device_type = "pci"; + reg = <0x0 0x0 0x0 0x0 0x0>; + external-facing; + /* apple,tunable is filled by the bootloader */ + #address-cells = <3>; + #size-cells = <2>; + ranges; + interrupt-controller; + #interrupt-cells = <1>; + interrupt-map-mask = <0 0 0 7>; + interrupt-map = <0 0 0 1 &usb4_1_pcie_port 0 0 0 0>, + <0 0 0 2 &usb4_1_pcie_port 0 0 0 1>, + <0 0 0 3 &usb4_1_pcie_port 0 0 0 2>, + <0 0 0 4 &usb4_1_pcie_port 0 0 0 3>; + }; + }; + }; +}; + +&usb4_1_acio { + power-domains = <&ps_atc1_cio>, <&ps_atc1_cio_pcie>, + <&ps_atc1_cio_usb>, <&ps_atc1_pcie>; + post-init-providers = <&ps_atc1_cio>, <&ps_atc1_cio_pcie>, + <&ps_atc1_cio_usb>, <&ps_atc1_pcie>; + apple,pcie-tunnel = <&usb4_1_pcie_tunnel>; +}; diff --git a/arch/arm64/boot/dts/apple/t8122.dtsi b/arch/arm64/boot/dts/apple/t8122.dtsi index ba3e893a234f5..67b2f620dfc63 100644 --- a/arch/arm64/boot/dts/apple/t8122.dtsi +++ b/arch/arm64/boot/dts/apple/t8122.dtsi @@ -1591,8 +1591,10 @@ usb4_0_acio: cio@701ac0000 { compatible = "apple,t8122-usb4-acio", "apple,t8103-usb4-acio"; reg = <0x7 0x01ac0000 0x0 0xc000>, - <0x7 0x01080000 0x0 0x20000>; - reg-names = "rc", "sram"; + <0x7 0x01080000 0x0 0x20000>, + <0x7 0x01e44000 0x0 0x4000>, + <0x7 0x01100000 0x0 0x4000>; + reg-names = "rc", "sram", "pcie-adapter", "cpu"; mboxes = <&usb4_0_mbox>; resets = <&cio_reset 0>; @@ -1758,8 +1760,10 @@ usb4_1_acio: cio@b01ac0000 { compatible = "apple,t8122-usb4-acio", "apple,t8103-usb4-acio"; reg = <0xb 0x01ac0000 0x0 0xc000>, - <0xb 0x01080000 0x0 0x20000>; - reg-names = "rc", "sram"; + <0xb 0x01080000 0x0 0x20000>, + <0xb 0x01e44000 0x0 0x4000>, + <0xb 0x01100000 0x0 0x4000>; + reg-names = "rc", "sram", "pcie-adapter", "cpu"; mboxes = <&usb4_1_mbox>; resets = <&cio_reset 1>; diff --git a/drivers/pci/controller/pcie-apple-t8122.h b/drivers/pci/controller/pcie-apple-t8122.h new file mode 100644 index 0000000000000..bf0cef030b682 --- /dev/null +++ b/drivers/pci/controller/pcie-apple-t8122.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef PCIE_APPLE_T8122_H +#define PCIE_APPLE_T8122_H + +#define APPLE_T8122_PCIEC0_ECAM 0x730000000ULL +#define APPLE_T8122_PCIEC1_ECAM 0xb30000000ULL +#define APPLE_T8122_PCIEC_AIC_BASE 1075 +#define APPLE_T8122_PCIEC0_MSI_BASE 32 +#define APPLE_T8122_PCIEC1_MSI_BASE 64 + +/* Reject unknown apertures instead of assigning another bridge's slice. */ +static inline int apple_t8122_msi_vector_base(unsigned long long ecam) +{ + switch (ecam) { + case APPLE_T8122_PCIEC0_ECAM: + return APPLE_T8122_PCIEC0_MSI_BASE; + case APPLE_T8122_PCIEC1_ECAM: + return APPLE_T8122_PCIEC1_MSI_BASE; + default: + return -1; + } +} + +#endif diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index a3671bf39d196..7f887a0ab2b03 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -53,6 +53,7 @@ #include "../pci.h" #include "pci-host-common.h" #include "pcie-apple-t8140-bootstrap.h" +#include "pcie-apple-t8122.h" static ATOMIC_NOTIFIER_HEAD(apple_pcie_tunnel_notifiers); @@ -3422,6 +3423,12 @@ static int apple_msi_init(struct apple_pcie *pcie) default: goto invalid_slice; } + } else if (of_machine_is_compatible("apple,t8122")) { + aic_base = APPLE_T8122_PCIEC_AIC_BASE; + ret = apple_t8122_msi_vector_base(ecam.start); + if (ret < 0) + goto invalid_slice; + expected_base = ret; } else if (of_machine_is_compatible("apple,t8132")) { aic_base = 1506; switch (ecam.start) { diff --git a/drivers/pmdomain/apple/pmgr-pwrstate.c b/drivers/pmdomain/apple/pmgr-pwrstate.c index d6ac0c2cebd9f..29df1e15e3f71 100644 --- a/drivers/pmdomain/apple/pmgr-pwrstate.c +++ b/drivers/pmdomain/apple/pmgr-pwrstate.c @@ -1095,6 +1095,31 @@ static int apple_pmgr_ps_probe(struct platform_device *pdev) return ret; } +/* A reset witness, not a request to override another consumer's vote. */ +int apple_pmgr_ps_verify_off(struct device *consumer) +{ + struct generic_pm_domain *genpd; + struct apple_pmgr_ps *ps; + u32 val; + int ret; + + if (!consumer->pm_domain) + return -ENODEV; + genpd = pd_to_genpd(consumer->pm_domain); + if (genpd->power_off != apple_pmgr_ps_power_off) + return -EOPNOTSUPP; + ps = genpd_to_apple_pmgr_ps(genpd); + ret = regmap_read(ps->regmap, ps->offset, &val); + if (ret) + return ret; + if (val & (APPLE_PMGR_PS_LEVELS | APPLE_PMGR_BUSY | APPLE_PMGR_AUTO_ENABLE)) { + dev_err(consumer, "reset boundary not reached: PMGR %#x\n", val); + return -EBUSY; + } + return 0; +} +EXPORT_SYMBOL_GPL(apple_pmgr_ps_verify_off); + static struct platform_driver apple_pmgr_ps_driver; /* Wait for the masked state word to read @want; @cur gets the last value. */ diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index f4ccdfaa7eee0..2d4610974c95d 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -108,9 +108,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -138,6 +140,7 @@ #define APPLE_MXWRAP_CPU_CTRL 0x28 #define APPLE_MXWRAP_CPU_CTRL_RUN BIT(4) #define APPLE_MXWRAP_CPU_STATUS 0x44 +#define APPLE_CIO_T8122_LINK_CHECK_MS 8000 /* DP IN adapter blocks a host router may have (T6050 has four) */ #define APPLE_DPIN_MAX 4 @@ -145,6 +148,7 @@ struct apple_cio_hw { /* DP IN adapter blocks of the router; 0 means the two of t8103 */ unsigned int dpin_count; + bool strict_power_cycle; bool mxwrap; bool lstx_explicit; /* Leave MxWrap RUN alone: the CIO reconfig restarts the CPU. */ @@ -189,6 +193,15 @@ static const struct apple_cio_hw apple_cio_t6030_hw = { .dp = &apple_dpin_m3, }; +/* T8122 shares the T6030 MxWrap/PHY generation; DP is unqualified. */ +static const struct apple_cio_hw apple_cio_t8122_hw = { + .strict_power_cycle = true, + .mxwrap = true, + .lstx_explicit = true, + .strict_fw_ready = true, + .dp = &apple_dpin_disabled, +}; + /* * T8132 has the T6030 ACIO layout and MxWrap CPU block, but the CIO reconfig * already starts the CPU from its preloaded SRAM: RUN is never written. LSTX @@ -480,6 +493,8 @@ struct apple_cio { struct reset_control *reset; struct dev_pm_domain_list *pd_list; + unsigned int pd_held; + bool reset_failed; struct mutex lock; /* serializes cable transitions and ACIO power up/down */ bool removing; /* protected by lock */ @@ -2576,6 +2591,48 @@ static const struct tb_nhi_ring_layout apple_nhi_ring_layout = { .options_stride = APPLE_CIO_NHI_RING_STRIDE, }; +/* + * PD owns session shutdown. Put it after the dynamic NHI, and the NHI after + * the role/PHY suppliers, so device_shutdown visits the coordinator first. + * Stateless links are purged by device_del of this per-attach NHI. + */ +static int apple_nhi_shutdown_order(struct apple_cio *acio, struct device *nhi) +{ + struct device_node *np; + struct spmi_device *pd; + int ret = 0; + + if (!acio->hw->strict_power_cycle) + return 0; + if (!IS_REACHABLE(CONFIG_SPMI)) + return -EOPNOTSUPP; + np = of_get_parent(acio->connector_np); + pd = spmi_find_device_by_of_node(np); + of_node_put(np); + if (!pd) + return -EPROBE_DEFER; + for (unsigned int port = 0; port < 2; port++) { + struct platform_device *supplier; + + np = of_graph_get_remote_node(acio->connector_np, port, -1); + supplier = of_find_device_by_node(np); + of_node_put(np); + if (!supplier) { + ret = -EPROBE_DEFER; + break; + } + if (!device_link_add(nhi, &supplier->dev, DL_FLAG_STATELESS)) + ret = -EINVAL; + put_device(&supplier->dev); + if (ret) + break; + } + if (!ret && !device_link_add(&pd->dev, nhi, DL_FLAG_STATELESS)) + ret = -EINVAL; + put_device(&pd->dev); + return ret; +} + static int apple_nhi_probe(struct platform_device *pdev) { struct apple_cio *acio = dev_get_drvdata(pdev->dev.parent); @@ -2798,6 +2855,11 @@ static int apple_nhi_probe(struct platform_device *pdev) mutex_unlock(&anhi->tb->lock); + ret = apple_nhi_shutdown_order(acio, &pdev->dev); + if (ret) { + mutex_lock(&anhi->tb->lock); + goto err_unlock_tb_domain; + } WRITE_ONCE(acio->nhi_pdev, pdev); acio->nhi_boot_status = 0; complete(&acio->nhi_boot_completion); @@ -3239,6 +3301,37 @@ static void apple_cio_cpu_halt(struct apple_cio *acio) acio->cpu_base + APPLE_MXWRAP_CPU_CTRL); } +/* Release exactly the references we own, children first, then witness PMGR. */ +static void apple_cio_power_down(struct apple_cio *acio) +{ + int i, ret; + + for (i = acio->pd_list->num_pds - 1; i >= 0; i--) { + if (acio->pd_list->pd_links[i]) + device_link_del(acio->pd_list->pd_links[i]); + acio->pd_list->pd_links[i] = NULL; + dev_pm_syscore_device(acio->pd_list->pd_devs[i], false); + } + if (!acio->hw->strict_power_cycle) + return; + while (acio->pd_held) { + struct device *pd = acio->pd_list->pd_devs[--acio->pd_held]; + + ret = pm_runtime_put_sync_suspend(pd); + if (ret < 0) + acio->reset_failed = true; + } + for (i = 0; i < acio->pd_list->num_pds; i++) { + ret = apple_pmgr_ps_verify_off(acio->pd_list->pd_devs[i]); + if (ret) + acio->reset_failed = true; + } + if (acio->reset_failed) + dev_err(acio->dev, "ACIO reset boundary unproven; restart quarantined until reboot\n"); + else + dev_info(acio->dev, "ACIO reset boundary: PMGR domains off\n"); +} + /* TEST (sv6t7): the PS_PROT word of the slice a DART node names as its clock protection. */ static int apple_cio_test_slice(struct device_node *np, unsigned int *slice, u32 *word) { @@ -3309,7 +3402,7 @@ static void apple_cio_test_log_guards(struct apple_cio *acio) */ static void apple_cio_stop(struct apple_cio *acio) { - int ret, i; + int ret; lockdep_assert_held(&acio->lock); if (!acio->current_cable_info) @@ -3334,12 +3427,7 @@ static void apple_cio_stop(struct apple_cio *acio) apple_cio_test_log_guards(acio); /* Finally, remove the links to the PD domains to power everything off */ - for (i = 0; i < acio->pd_list->num_pds; i++) { - if (acio->pd_list->pd_links[i]) - device_link_del(acio->pd_list->pd_links[i]); - acio->pd_list->pd_links[i] = NULL; - dev_pm_syscore_device(acio->pd_list->pd_devs[i], false); - } + apple_cio_power_down(acio); acio->current_cable_info = 0; acio->resume_link_failed = false; @@ -3421,10 +3509,20 @@ static int apple_cio_start(struct apple_cio *acio) dev_info(acio->dev, "PCIe-C reset before ACIO start\n"); } + if (acio->reset_failed) + return -EHOSTDOWN; + /* Create device links to the power domains in order to power them on */ for (i = 0; i < acio->pd_list->num_pds; i++) { + if (acio->hw->strict_power_cycle) { + ret = pm_runtime_resume_and_get(acio->pd_list->pd_devs[i]); + if (ret < 0) + goto remove_links; + acio->pd_held++; + } link = device_link_add(acio->dev, acio->pd_list->pd_devs[i], - DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE); + acio->hw->strict_power_cycle ? DL_FLAG_STATELESS : + DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE); if (!link) { ret = -ENODEV; goto remove_links; @@ -3585,6 +3683,11 @@ static int apple_cio_start(struct apple_cio *acio) acio->current_cable_info = acio->target_cable_info; acio->start_gen++; + if (acio->hw->strict_power_cycle) { + acio->link_lost_gen = acio->start_gen; + mod_delayed_work(system_freezable_wq, &acio->link_lost_work, + msecs_to_jiffies(APPLE_CIO_T8122_LINK_CHECK_MS)); + } return 0; err_depopulate: @@ -3600,12 +3703,7 @@ static int apple_cio_start(struct apple_cio *acio) acio->rtk = NULL; apple_cio_cpu_halt(acio); /* Cut power to reset the entire block */ - for (i = 0; i < acio->pd_list->num_pds; i++) { - if (acio->pd_list->pd_links[i]) - device_link_del(acio->pd_list->pd_links[i]); - acio->pd_list->pd_links[i] = NULL; - dev_pm_syscore_device(acio->pd_list->pd_devs[i], false); - } + apple_cio_power_down(acio); return ret; } @@ -3703,6 +3801,15 @@ static bool apple_cio_has_peer(struct apple_cio *acio, u64 *activity) tb_switch_for_each_port(anhi->tb->root_switch, port) { if (!tb_port_is_null(port)) continue; + if (acio->hw->strict_power_cycle) { + u32 lane; + int err = tb_port_read(port, &lane, TB_CFG_PORT, + port->cap_phy + LANE_ADP_CS_1, 1); + + dev_info(acio->dev, "attach health generation %u lane %u: %d state %#x activity %llu\n", + acio->start_gen, port->port, err, err ? 0 : lane, + *activity); + } if ((port->xdomain && !port->xdomain->is_unplugged) || (port->remote && !port->remote->sw->is_unplugged)) peer = true; @@ -3860,12 +3967,46 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, { struct apple_cio *acio = typec_thunderbolt_switch_get_drvdata(sw); + if (data->state == TYPEC_THUNDERBOLT_SWITCH_SLEEP) { + cancel_delayed_work_sync(&acio->link_lost_work); + cancel_delayed_work_sync(&acio->resume_link_work); + } + guard(mutex)(&acio->lock); /* Removal turns the cable off itself; only refuse to start new work. */ if (acio->removing) return data->state == TYPEC_THUNDERBOLT_SWITCH_OFF ? 0 : -ESHUTDOWN; + if (data->state == TYPEC_THUNDERBOLT_SWITCH_WAKE) { + acio->sleep_phase = APPLE_CIO_SLEEP_OPEN; + acio->sleep_restart_owed = false; + return 0; + } + + if (data->state == TYPEC_THUNDERBOLT_SWITCH_SLEEP) { + struct platform_device *pdev = READ_ONCE(acio->nhi_pdev); + struct apple_nhi *anhi; + int ret; + + if (!acio->hw->strict_power_cycle) + return -EOPNOTSUPP; + if (acio->current_cable_info) { + if (!pdev) + return -EBUSY; + anhi = platform_get_drvdata(pdev); + if (!anhi) + return -EBUSY; + ret = tb_apple_sleep_quiesce(anhi->tb); + if (ret) + return ret; + acio->sleep_restart_owed = true; + apple_cio_stop(acio); + } + acio->sleep_phase = APPLE_CIO_SLEEP_CLOSED; + return acio->reset_failed ? -EHOSTDOWN : 0; + } + dev_dbg(acio->dev, "set cable state: %d\n", data->state); switch (data->state) { @@ -4205,6 +4346,9 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, bool owed, early; int ret; + if (acio->hw->strict_power_cycle) + return NOTIFY_DONE; + /* Teardown off and no restart owed from an earlier sleep: nothing to do. */ if (!tears_down && !READ_ONCE(s2idle_teardown) && !READ_ONCE(acio->sleep_restart_owed)) @@ -4483,6 +4627,10 @@ static int apple_cio_probe(struct platform_device *pdev) acio->dev = &pdev->dev; acio->np = dev->of_node; acio->hw = of_device_get_match_data(dev); + /* Only J613 has qualified the T8122 router policy and resources. */ + if (acio->hw == &apple_cio_t8122_hw && + !of_machine_is_compatible("apple,j613")) + acio->hw = &apple_cio_t8103_hw; if (!acio->hw) return -EINVAL; @@ -4719,6 +4867,24 @@ static int apple_cio_probe(struct platform_device *pdev) return 0; } +static void apple_cio_shutdown(struct platform_device *pdev) +{ + struct apple_cio *acio = platform_get_drvdata(pdev); + + if (!acio->hw->strict_power_cycle) + return; + + scoped_guard(mutex, &acio->lock) + acio->removing = true; + disable_delayed_work_sync(&acio->sleep_restart_work); + disable_delayed_work_sync(&acio->resume_link_work); + disable_delayed_work_sync(&acio->link_lost_work); + disable_delayed_work_sync(&acio->pcie_tunnel_work); + guard(mutex)(&acio->lock); + acio->target_cable_info = 0; + apple_cio_stop(acio); +} + static void apple_cio_remove(struct platform_device *pdev) { struct apple_cio *acio = platform_get_drvdata(pdev); @@ -4748,6 +4914,10 @@ static void apple_cio_remove(struct platform_device *pdev) static const struct of_device_id apple_acio_match[] = { + { + .compatible = "apple,t8122-usb4-acio", + .data = &apple_cio_t8122_hw, + }, { .compatible = "apple,t6040-usb4-acio", .data = &apple_cio_t6040_hw, @@ -4788,6 +4958,7 @@ static struct platform_driver apple_cio_driver = { }, .probe = apple_cio_probe, .remove = apple_cio_remove, + .shutdown = apple_cio_shutdown, }; static struct platform_driver * const apple_cio_drivers[] = { diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 074ce00efdf1c..08b43f66a85f4 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -3825,6 +3826,30 @@ static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); } +/* Called before the freezer. Close discovery and inspect in one transaction. */ +int tb_apple_sleep_quiesce(struct tb *tb) +{ + struct tb_cm *tcm = tb_priv(tb); + struct tb_port *port; + struct tb_tunnel *tunnel; + + if (!tb->root_switch || !tb->cm_ops) + return -EBUSY; + scoped_guard(mutex, &tb->lock) { + tb_switch_for_each_port(tb->root_switch, port) + if (port->remote) + return -EBUSY; + list_for_each_entry(tunnel, &tcm->tunnel_list, list) + if (!tb_tunnel_is_dma(tunnel)) + return -EBUSY; + tcm->hotplug_active = false; + } + /* No topology worker can now publish a router after the inspection. */ + flush_workqueue(tb->wq); + return 0; +} +EXPORT_SYMBOL_GPL(tb_apple_sleep_quiesce); + static void tb_stop(struct tb *tb) { struct tb_cm *tcm = tb_priv(tb); diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index ea5dd31562695..2aceee8dc95ab 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -1645,4 +1645,6 @@ static inline int tb_configfs_init(void) { return 0; } static inline void tb_configfs_exit(void) { } #endif +int tb_apple_sleep_quiesce(struct tb *tb); + #endif diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 9298672c02aa0..bd597c2cbfb31 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -2564,10 +2565,83 @@ static int cd321x_tbt_notify(struct notifier_block *nb, return NOTIFY_OK; } +/* ACIO -> USB role -> PHY SAFE, before tasks freeze. */ +static int cd321x_session_stop(struct cd321x *cd, bool sleep) +{ + struct tps6598x *tps = &cd->tps; + struct typec_thunderbolt_switch_data data = { + .state = sleep ? TYPEC_THUNDERBOLT_SWITCH_SLEEP : + TYPEC_THUNDERBOLT_SWITCH_OFF, + }; + struct typec_mux_state safe = { .mode = TYPEC_STATE_SAFE }; + bool active; + int ret; + + cd321x_prepare(tps); + guard(mutex)(&tps->lock); + active = cd->state.mode == TYPEC_MODE_USB4 || + cd->state.alt == cd->port_altmode_tbt; + ret = typec_thunderbolt_switch_set(cd->tbt_switch, &data); + if (ret) + return ret; + cd->session_closed = true; + if (sleep && !active) + return 0; + ret = usb_role_switch_set_role(tps->role_sw, USB_ROLE_NONE); + if (!ret) + ret = typec_mux_set(cd->mux, &safe); + if (ret) + dev_err(tps->dev, "session quiesce failed, retaining PHY ownership: %d\n", ret); + else + dev_info(tps->dev, "USB4 session quiesced before freezer\n"); + return ret; +} + +static void cd321x_session_restore(struct cd321x *cd) +{ + struct typec_thunderbolt_switch_data data = { + .state = TYPEC_THUNDERBOLT_SWITCH_WAKE, + }; + + scoped_guard(mutex, &cd->tps.lock) { + typec_thunderbolt_switch_set(cd->tbt_switch, &data); + if (cd->session_closed) { + atomic_set(&cd->restore_event, 1); + cd->session_closed = false; + } + } + cd321x_complete(&cd->tps); +} + +static int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused) +{ + struct cd321x *cd = container_of(nb, struct cd321x, session_pm); + int ret; + + switch (event) { + case PM_SUSPEND_PREPARE: + ret = cd321x_session_stop(cd, true); + if (ret) + cd321x_session_restore(cd); + return notifier_from_errno(ret); + case PM_POST_SUSPEND: + cd321x_session_restore(cd); + break; + } + return NOTIFY_OK; +} + static void cd321x_ready(struct tps6598x *tps) { struct cd321x *cd321x = container_of(tps, struct cd321x, tps); + if (of_machine_is_compatible("apple,j613")) { + cd321x->session_pm.notifier_call = cd321x_session_pm; + cd321x->session_pm.priority = 100; + if (!register_pm_notifier(&cd321x->session_pm)) + cd321x->session_pm_registered = true; + } + guard(mutex)(&tps->lock); /* Include failures reported while the port was still being initialized. */ if (cd321x_pm_ready(&cd321x->pm)) { @@ -2580,6 +2654,10 @@ static void cd321x_ready(struct tps6598x *tps) VISIBLE_IF_KUNIT void cd321x_stop_work(struct cd321x *cd321x) { + if (cd321x->session_pm_registered) { + unregister_pm_notifier(&cd321x->session_pm); + cd321x->session_pm_registered = false; + } scoped_guard(mutex, &cd321x->tps.lock) cd321x_pm_remove(&cd321x->pm); @@ -3654,6 +3732,18 @@ int tipd_init(struct tps6598x *tps) } EXPORT_SYMBOL_GPL(tipd_init); +void tipd_shutdown(struct tps6598x *tps) +{ + int ret; + + if (!of_machine_is_compatible("apple,j613")) + return; + ret = cd321x_session_stop(container_of(tps, struct cd321x, tps), false); + if (ret) + dev_err(tps->dev, "USB4 shutdown incomplete: %d\n", ret); +} +EXPORT_SYMBOL_GPL(tipd_shutdown); + void tipd_remove(struct tps6598x *tps) { int ret = 0; diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 2eaa698da2e49..64fbc80fb0083 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -1121,6 +1121,11 @@ static const struct dev_pm_ops sn201202x_pm_ops = { SET_RUNTIME_PM_OPS(sn201202x_runtime_suspend, sn201202x_runtime_resume, NULL) }; +static void sn201202x_shutdown(struct spmi_device *sdev) +{ + tipd_shutdown(dev_get_drvdata(&sdev->dev)); +} + static struct spmi_driver sn201202x_driver = { .driver = { .name = "sn201202x", @@ -1134,6 +1139,7 @@ static struct spmi_driver sn201202x_driver = { }, .probe = sn201202x_probe, .remove = sn201202x_remove, + .shutdown = sn201202x_shutdown, }; module_spmi_driver(sn201202x_driver); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index a9bc1d7383008..23ff0cb2a1ab5 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -442,6 +442,9 @@ struct cd321x_status { }; struct cd321x { + struct notifier_block session_pm; + bool session_pm_registered; + bool session_closed; struct tps6598x tps; struct tps6598x_dp_sid_status_reg dp_sid_status; @@ -520,6 +523,7 @@ extern const struct regmap_config tps6598x_regmap_config; int tipd_init(struct tps6598x *tps); void tipd_remove(struct tps6598x *tps); +void tipd_shutdown(struct tps6598x *tps); int tipd_prepare(struct device *dev); void tipd_complete(struct device *dev); int tipd_suspend(struct tps6598x *tps); diff --git a/include/linux/soc/apple/pmgr-pwrstate.h b/include/linux/soc/apple/pmgr-pwrstate.h index c86cba6c242d2..3ed2a7b5b6935 100644 --- a/include/linux/soc/apple/pmgr-pwrstate.h +++ b/include/linux/soc/apple/pmgr-pwrstate.h @@ -8,6 +8,7 @@ struct device; struct device_node; #if IS_ENABLED(CONFIG_APPLE_PMGR_PWRSTATE) +int apple_pmgr_ps_verify_off(struct device *consumer); int apple_pmgr_ps_cycle(struct device_node *np); int apple_pmgr_ps_verify_state(struct device *domain_dev, bool on); /* @@ -22,6 +23,7 @@ int apple_pmgr_ps_consumer_off(struct device *consumer); int apple_pmgr_ps_consumer_on(struct device *consumer); bool apple_pmgr_ps_consumer_is_off(struct device *consumer); #else +static inline int apple_pmgr_ps_verify_off(struct device *consumer) { return -EOPNOTSUPP; } static inline int apple_pmgr_ps_verify_state(struct device *domain_dev, bool on) { return -EOPNOTSUPP; diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h index 9d1df67201239..bf7fe40be1501 100644 --- a/include/linux/usb/typec_mux.h +++ b/include/linux/usb/typec_mux.h @@ -212,6 +212,9 @@ enum typec_thunderbolt_switch_state { TYPEC_THUNDERBOLT_SWITCH_OFF, TYPEC_THUNDERBOLT_SWITCH_TBT, TYPEC_THUNDERBOLT_SWITCH_USB4, + /* Close topology admission, reject native tunnels, stop before freezer. */ + TYPEC_THUNDERBOLT_SWITCH_SLEEP, + TYPEC_THUNDERBOLT_SWITCH_WAKE, }; /** diff --git a/tools/testing/j613-usb4/run.py b/tools/testing/j613-usb4/run.py new file mode 100644 index 0000000000000..8515b5e4a7235 --- /dev/null +++ b/tools/testing/j613-usb4/run.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Compile the production PMGR off witness against a fault-injecting regmap.""" +import os +from pathlib import Path +import re +import subprocess +import tempfile + +root = Path(__file__).resolve().parents[3] +source = (root / 'drivers/pmdomain/apple/pmgr-pwrstate.c').read_text() +start = source.index('int apple_pmgr_ps_verify_off(') +end = source.index('\nEXPORT_SYMBOL', start) +macros = '\n'.join(re.findall(r'^#define APPLE_PMGR_(?:AUTO_ENABLE|BUSY|PS_ACTUAL|PS_TARGET|PS_LEVELS)\b[^\n]*', source, re.M)) +prefix = r''' +#include +#include +#include +#include +typedef uint32_t u32; +#define BIT(n) (1U << (n)) +#define GENMASK(h, l) ((~0U >> (31-(h))) & (~0U << (l))) +struct generic_pm_domain { void *power_off; }; +struct device { void *pm_domain; }; +struct apple_pmgr_ps { void *regmap; unsigned int offset; }; +static struct apple_pmgr_ps state; +static struct generic_pm_domain domain; +static void apple_pmgr_ps_power_off(void) {} +#define pd_to_genpd(p) ((struct generic_pm_domain *)(p)) +#define genpd_to_apple_pmgr_ps(p) (&state) +#define dev_err(...) ((void)0) +static u32 value; +static int error; +static int regmap_read(void *map, unsigned int offset, u32 *out) +{ *out = value; return error; } +''' +cases = r''' +int main(void) +{ + struct device d = {0}; + assert(apple_pmgr_ps_verify_off(&d) == -ENODEV); + d.pm_domain = &domain; + assert(apple_pmgr_ps_verify_off(&d) == -EOPNOTSUPP); + domain.power_off = apple_pmgr_ps_power_off; + error = -EIO; + assert(apple_pmgr_ps_verify_off(&d) == -EIO); + error = 0; + assert(apple_pmgr_ps_verify_off(&d) == 0); + for (unsigned int bit = 0; bit < 32; bit++) { + value = BIT(bit); + int expected = value & (APPLE_PMGR_PS_LEVELS | APPLE_PMGR_BUSY | + APPLE_PMGR_AUTO_ENABLE) ? -EBUSY : 0; + assert(apple_pmgr_ps_verify_off(&d) == expected); + } + return 0; +} +''' +with tempfile.TemporaryDirectory(prefix='j613-usb4-') as tmp: + t = Path(tmp) + (t / 'test.c').write_text(prefix + macros + '\n' + source[start:end] + cases) + subprocess.run([os.environ.get('CC', 'cc'), '-Wall', '-Werror', + str(t / 'test.c'), '-o', str(t / 'test')], check=True) + subprocess.run([str(t / 'test')], check=True) +print('PASS: production PMGR witness rejects missing/foreign domains, read faults, all live state bits') + +# Use the same aperture selector and constants as PCIe-C admission. +msi = r''' +#include +#include "pcie-apple-t8122.h" +int main(void) +{ + assert(apple_t8122_msi_vector_base(APPLE_T8122_PCIEC0_ECAM) == 32); + assert(apple_t8122_msi_vector_base(APPLE_T8122_PCIEC1_ECAM) == 64); + assert(APPLE_T8122_PCIEC_AIC_BASE + APPLE_T8122_PCIEC0_MSI_BASE == 1107); + assert(APPLE_T8122_PCIEC_AIC_BASE + APPLE_T8122_PCIEC1_MSI_BASE == 1139); + assert(apple_t8122_msi_vector_base(0) < 0); + assert(apple_t8122_msi_vector_base(APPLE_T8122_PCIEC0_ECAM + 1) < 0); + assert(apple_t8122_msi_vector_base(APPLE_T8122_PCIEC1_ECAM - 1) < 0); + return 0; +} +''' +with tempfile.TemporaryDirectory(prefix='j613-msi-') as tmp: + t = Path(tmp) + (t / 'test.c').write_text(msi) + subprocess.run([os.environ.get('CC', 'cc'), '-Wall', '-Werror', + '-I', str(root / 'drivers/pci/controller'), + str(t / 'test.c'), '-o', str(t / 'test')], check=True) + subprocess.run([str(t / 'test')], check=True) +print('PASS: shared T8122 PCIe-C aperture/MSI selector and unknown apertures')