From 60d2415a0db63316af1c5a11ac5d5aae58bf30c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Sat, 14 Nov 2026 17:05:06 +0100 Subject: [PATCH 001/269] ci: mask the pattern list of unmatched-property dt-schema messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A "does not match any of the regexes" message lists every pattern the schema allows, vendor-prefixes.yaml included. A change that adds a vendor prefix lengthens that list, so each such warning the base already has is reported once as removed and once as new, and the check fails although no device tree changed. Mask the list the way phandle values already are. Signed-off-by: František Balázsy --- scripts/dtc/apple-dt-check.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/dtc/apple-dt-check.sh b/scripts/dtc/apple-dt-check.sh index 04b634e886427..820d8dc2daf41 100755 --- a/scripts/dtc/apple-dt-check.sh +++ b/scripts/dtc/apple-dt-check.sh @@ -2,7 +2,9 @@ # SPDX-License-Identifier: GPL-2.0-only # apple-dt-check.sh BASE HEAD WORKDIR: run CHECK_DTBS on every Apple device tree at BASE and at HEAD # and fail when HEAD has a warning BASE does not. Also runs dt_binding_check on every binding HEAD -# changes. Phandle values in the messages are masked, so renumbering is not a new warning. +# changes. Phandle values in the messages are masked, so renumbering is not a new warning, and so +# is the pattern list of a "does not match any of the regexes" message, which grows with every +# vendor prefix the bindings add. # Needs dtschema (dt-validate), clang/lld (LLVM=1), flex, bison. Prints a summary. set -u BASE=$1 HEAD=$2 W=$3 @@ -17,7 +19,7 @@ warnings() { # rev tag local rc=$? grep -E '\.dtb: |[Ww]arning|rror' "$W/$2.log" | grep -v '^ ' | sed -E "s#$t/##g; s#$o/##g" | - perl -pe 's{(\[\[[^:]*?\]\])}{(my $x = $1) =~ s/\d+/N/g; $x}ge; s{b\x27(?:\\.|[^\x27])*\x27}{b\x27N\x27}g' | + perl -pe 's{(\[\[[^:]*?\]\])}{(my $x = $1) =~ s/\d+/N/g; $x}ge; s{b\x27(?:\\.|[^\x27])*\x27}{b\x27N\x27}g; s{( not match any of the regexes): .*}{$1: ...}' | sort -u > "$W/$2.warn" echo "$2: make rc=$rc, $(wc -l < "$W/$2.warn") warning lines, $(echo $dtbs | wc -w) DTBs" return $rc From c8666fb370383324c0258316ff081a91f264c76e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:02 +0100 Subject: [PATCH 002/269] dt-bindings: apple: T6040 sleep controller, ProRes, PCIe, ATC PHY, DP audio and USB4 properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Describe what the J616s (M4 Pro, T6040) sleep and restore paths read from the device tree: the T6040 sleep controller node, the T6041 ProRes compatible, the PCIe and ATC PHY properties used after a power loss, the DP audio and USB4 ACIO additions, nonposted-mmio on a PMGR block and the pin controller's apple,resume-restore-all. Signed-off-by: František Balázsy --- .../bindings/arm/apple/apple,pmgr.yaml | 2 + .../bindings/media/apple,prores.yaml | 6 +- .../devicetree/bindings/pci/apple,pcie.yaml | 2 +- .../bindings/phy/apple,t6040-atcphy.yaml | 28 +++++++ .../bindings/pinctrl/apple,pinctrl.yaml | 8 ++ .../bindings/power/apple,t6040-sleep.yaml | 80 +++++++++++++++++++ .../bindings/sound/apple,t6030-dpaudio.yaml | 2 + .../thunderbolt/apple,t8103-usb4-acio.yaml | 6 ++ 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml diff --git a/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml b/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml index 91ae5a3ce1bca..4c736d06122dd 100644 --- a/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml +++ b/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml @@ -53,6 +53,8 @@ properties: reg: maxItems: 1 + nonposted-mmio: true + "#address-cells": const: 1 diff --git a/Documentation/devicetree/bindings/media/apple,prores.yaml b/Documentation/devicetree/bindings/media/apple,prores.yaml index 69be59afd4e70..827863013b202 100644 --- a/Documentation/devicetree/bindings/media/apple,prores.yaml +++ b/Documentation/devicetree/bindings/media/apple,prores.yaml @@ -248,8 +248,12 @@ allOf: maxItems: 1 iommus: maxItems: 1 + resets: + description: + The shared codec reset. It also clears the + translations of the engine's DART, so a board may + leave it out and have a hung engine refused instead. required: - - resets - reg-names - if: properties: diff --git a/Documentation/devicetree/bindings/pci/apple,pcie.yaml b/Documentation/devicetree/bindings/pci/apple,pcie.yaml index 55d40f9a40cc7..dea7da061a73f 100644 --- a/Documentation/devicetree/bindings/pci/apple,pcie.yaml +++ b/Documentation/devicetree/bindings/pci/apple,pcie.yaml @@ -504,7 +504,7 @@ allOf: properties: reg: minItems: 8 - maxItems: 8 + maxItems: 9 interrupts: minItems: 6 maxItems: 6 diff --git a/Documentation/devicetree/bindings/phy/apple,t6040-atcphy.yaml b/Documentation/devicetree/bindings/phy/apple,t6040-atcphy.yaml index 3178de1a8b7e4..68ee0945b02d0 100644 --- a/Documentation/devicetree/bindings/phy/apple,t6040-atcphy.yaml +++ b/Documentation/devicetree/bindings/phy/apple,t6040-atcphy.yaml @@ -172,6 +172,29 @@ properties: $ref: "#/$defs/apple,tunable" description: USB2 host tunables, applied to the bank0 region. + apple,tunable-cio-lane0: + $ref: "#/$defs/apple,tunable" + description: > + Lane 0 receiver defaults for CIO (USB4/Thunderbolt) mode, applied to the + core region when the port enters USB4 or Thunderbolt. CIO mode is offered + only when both lane tables are present. + + apple,tunable-cio-lane1: + $ref: "#/$defs/apple,tunable" + description: Lane 1 receiver defaults for CIO mode, applied to the core region. + + apple,tunable-cio-uc-regs: + $ref: "#/$defs/apple,tunable" + description: Optional PHY microcontroller defaults for CIO mode, core region. + + apple,tunable-cio-lane0-eq: + $ref: "#/$defs/apple,tunable" + description: Optional lane 0 receiver equalization for CIO mode, core region. + + apple,tunable-cio-lane1-eq: + $ref: "#/$defs/apple,tunable" + description: Optional lane 1 receiver equalization for CIO mode, core region. + required: - compatible - reg @@ -199,6 +222,11 @@ allOf: mode-switch: false port: false ports: false + apple,tunable-cio-lane0: false + apple,tunable-cio-lane1: false + apple,tunable-cio-uc-regs: false + apple,tunable-cio-lane0-eq: false + apple,tunable-cio-lane1-eq: false additionalProperties: false diff --git a/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml b/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml index e2bec4d954de9..228151a45575d 100644 --- a/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml +++ b/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml @@ -69,6 +69,14 @@ properties: $ref: /schemas/types.yaml#/definitions/uint32 description: The number of pins in this GPIO controller. + apple,resume-restore-all: + type: boolean + description: + After a sleep that powers the block down, every pin's configuration is + written back, not only the pins a driver has claimed, as the vendor + firmware does for this block. The pins in gpio-reserved-ranges are left + as the wake found them. + gpio-reserved-ranges: true interrupts: diff --git a/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml new file mode 100644 index 0000000000000..29ab8c7267c18 --- /dev/null +++ b/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/apple,t6040-sleep.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T6040 system sleep controller + +maintainers: + - František Balázsy + +description: + The power states the platform checks before it lets the T6040 sleep engine take + the SoC down (the ADT's device-check-ps-b4-sleep table). Each entry names a + PMGR register block, the offset of a display pipe's CPU power state and the + offset of its last sub-block; the states in between are 8 bytes apart. + + The optional register blocks are the SoC words the entry and the wake write. + All of them are shared with the drivers that own them, and are touched only + once nothing blocks the sleep any more. + +properties: + compatible: + const: apple,t6040-sleep + + reg: + minItems: 2 + items: + - description: CPU start/stop block (pmgr + 0x88000) + - description: AON watchdog + - description: control words of the three CPU complexes + - description: SoC power gate, wake and retention voters + - description: clock generator, the clock mux park words + - description: SPMI controller of the PMU (boot stage, notify, hibernate) + - description: second SPMI controller's notify word + - description: MasterMini block with the S2R timer + - description: SOC-East retention and power words + - description: PMGR power words of the west axi2af bridges + - description: AOP global block, S2R control and wake masks + + reg-names: + minItems: 2 + items: + - const: cpu-start + - const: wdt + - const: cluster-ctl + - const: soc-pwrgate + - const: clkgen + - const: spmi0 + - const: spmi-a0 + - const: mastermini + - const: soc-east + - const: soc-ps + - const: aop-global + + apple,check-ps-b4-sleep: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + items: + - description: PMGR syscon + - description: offset of the pipe's CPU power state + - description: offset of its last sub-block's power state + + apple,check-ps-b4-sleep-names: + description: One name per apple,check-ps-b4-sleep entry. + +required: + - compatible + - apple,check-ps-b4-sleep + - apple,check-ps-b4-sleep-names + +additionalProperties: false + +examples: + - | + sleep-controller { + compatible = "apple,t6040-sleep"; + apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>; + apple,check-ps-b4-sleep-names = "disp"; + }; diff --git a/Documentation/devicetree/bindings/sound/apple,t6030-dpaudio.yaml b/Documentation/devicetree/bindings/sound/apple,t6030-dpaudio.yaml index 78dcee199f937..c730e4f46a020 100644 --- a/Documentation/devicetree/bindings/sound/apple,t6030-dpaudio.yaml +++ b/Documentation/devicetree/bindings/sound/apple,t6030-dpaudio.yaml @@ -50,6 +50,8 @@ properties: power-domains: maxItems: 1 + post-init-providers: true + ports: $ref: /schemas/graph.yaml#/properties/ports properties: diff --git a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml index 562fb2f315e5c..b18a475f5a355 100644 --- a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml +++ b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml @@ -129,6 +129,12 @@ properties: phy-names: const: cio + apple,atc-phy: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Type-C PHY of this router's port. On T6040 the router starts only while + that PHY is in CIO mode and tells it when it stops. + apple,pcie-tunnel: $ref: /schemas/types.yaml#/definitions/phandle description: From 12f467f6e307f7ac1792b518f2149378eb5dc3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:05 +0100 Subject: [PATCH 003/269] arm64: dts: apple: t6040: J616s sleep controller, ProRes, JPEG and DP audio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Describe the T6040 sleep controller and enable ProRes, JPEG and DP audio on the J616s. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040-j616s.dts | 108 ++++++++++++++++++++++ arch/arm64/boot/dts/apple/t6040.dtsi | 66 +++++++++++++ 2 files changed, 174 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6040-j616s.dts b/arch/arm64/boot/dts/apple/t6040-j616s.dts index e81fb8b6b5d0d..2a1e83d454c7c 100644 --- a/arch/arm64/boot/dts/apple/t6040-j616s.dts +++ b/arch/arm64/boot/dts/apple/t6040-j616s.dts @@ -71,6 +71,15 @@ adj-height-mm = <216>; }; +/* + * The AOP firmware reports the lid angle to the SMC once a second on its own, + * and every report on this mailbox ends a suspend-to-idle period: keep its + * messages in the FIFO across system sleep. + */ +&aop_mbox { + apple,hold-rx-in-sleep; +}; + &aop_audio { apple,chassis-name = "J616"; }; @@ -388,3 +397,102 @@ &aes { status = "okay"; }; + +/* + * The ProRes encoder: the shared codec power state (below the always-on + * fabric domain), its DART and the PMP soc-device record SCODEC0 (27). + * No reset: the shared codec's reset also clears the DART's translations, + * so the driver refuses a hung engine instead. + */ +&ps_fab1_soc { + status = "okay"; +}; + +&ps_scodec { + status = "okay"; +}; + +&pmp_report { + pmp_report_scodec: report@1b { + compatible = "apple,t6041-pmp-v2-report-entry", + "apple,t6000-pmp-v2-report-entry"; + reg = <0x1b>; + label = "pmp-scodec"; + #power-domain-cells = <0>; + power-domains = <&ps_scodec>; + }; +}; + +&apr0_dart { + status = "okay"; +}; + +&apr0 { + power-domains = <&ps_scodec>, <&pmp_report_scodec>; + power-domain-names = "core", "perf"; + /delete-property/ resets; + status = "okay"; +}; + +/* + * The JPEG engines share the PMP soc-device record JPEG0 (26): hold it as + * their performance request while an engine runs. + */ +&pmp_report { + pmp_report_jpg: report@1a { + compatible = "apple,t6041-pmp-v2-report-entry", + "apple,t6000-pmp-v2-report-entry"; + reg = <0x1a>; + label = "pmp-jpg"; + #power-domain-cells = <0>; + power-domains = <&ps_jpg>; + }; +}; + +&jpeg0 { + power-domains = <&ps_jpg>, <&pmp_report_jpg>; + power-domain-names = "core", "perf"; +}; + +&jpeg1 { + power-domains = <&ps_jpg>, <&pmp_report_jpg>; + power-domain-names = "core", "perf"; +}; + +/* + * DP/HDMI audio: the clients behind dcpext0/dcpext1, their power states and + * the SIO the boot loader publishes. Neither side may hold the other at + * probe: audio needs the SIO only when a stream opens, and the external + * displays must come up whether the audio clients bind or not. Only + * properties are added to the display processors: their status belongs to + * the display gate. + */ +&ps_dpa_p { + status = "okay"; +}; + +&ps_dpa1 { + status = "okay"; +}; + +&ps_dpa2 { + status = "okay"; +}; + +&dpaudio1 { + post-init-providers = <&sio>; + status = "okay"; +}; + +&dpaudio2 { + post-init-providers = <&sio>; + status = "okay"; +}; + +&dcpext0 { + post-init-providers = <&dpaudio1>; +}; + +&dcpext1 { + post-init-providers = <&dpaudio2>; +}; diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index 2cc327c7a2f8f..3d657f7c9a6dd 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -9,6 +9,10 @@ /* Blocks and properties the T6041 tree does not describe yet. */ / { + aliases { + sio = &sio; + }; + opp_p0: opp-table-p0 { compatible = "operating-points-v2"; opp-shared; @@ -241,9 +245,71 @@ pmgr_disp: power-management@502290000 { compatible = "apple,t6041-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd"; reg = <0x5 0x02290000 0x0 0x4000>; + /* DISP_CPU's power word takes non-posted accesses (nGnRnE). */ + nonposted-mmio; #address-cells = <1>; #size-cells = <1>; }; + + sleep-controller { + compatible = "apple,t6040-sleep"; + /* + * The SoC blocks the (currently unreachable) commit and wake + * write, all shared read/write with their owning drivers and + * touched only once no blocker remains: + * cpu-start pmgr reg0 + 0x88000 (misc-cores): CPU start/stop + * wdt the AON watchdog, the sleep's WD2 safety net + * cluster-ctl 0x502820000: the three CPU complexes E/P0/P1 + * soc-pwrgate 0x50282c000: wake + CPM retention voters + * clkgen 0x500000000: the clk-mux park words + * spmi0 0x509014000: the PMU boot-stage + NOTIFY + go-hib + * spmi-a0 0x509188000: the second SPMI NOTIFY + * mastermini 0x508900000: the S2R timer at +0x4000 (the commit) + * soc-east 0x502280000: SOC-East retention + east power words + * soc-ps 0x502800000: the west axi2af power words + * aop-global 0x508004000: the S2R control + wake masks/status + * The handler refuses before touching any of them. + */ + reg = <0x5 0x02888000 0x0 0x20>, + <0x5 0x0836c000 0x0 0x4000>, + <0x5 0x02820000 0x0 0x20>, + <0x5 0x0282c000 0x0 0x1000>, + <0x5 0x00000000 0x0 0x80000>, + <0x5 0x09014000 0x0 0x1000>, + <0x5 0x09188000 0x0 0x1000>, + <0x5 0x08900000 0x0 0x8000>, + <0x5 0x02280000 0x0 0x1000>, + <0x5 0x02800000 0x0 0x1000>, + <0x5 0x08004000 0x0 0x1000>; + reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", + "clkgen", "spmi0", "spmi-a0", "mastermini", + "soc-east", "soc-ps", "aop-global"; + /* + * ADT pmgr device-check-ps-b4-sleep (check-ps-b4-sleep = 0x1f): per + * display pipe its CPU and its last sub-block, 8 bytes apart. + */ + apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>, <&pmgr_aux 0x350 0x388>, + <&pmgr_aux 0x4a0 0x4d8>, <&pmgr_aux 0x4f0 0x528>, + <&pmgr_aux 0x2e8 0x320>; + apple,check-ps-b4-sleep-names = "disp", "dispext0", "dispext1", "dispext2", + "dispext3"; + /* + * ANS's storage PCIe (two ST lanes + shared PHY), for its sleep. The boot + * object (aura) publishes apple,apcie here plus the axi2af/cio3pll/ + * clkgen/phy-block/phy-ip windows and apple,tunable-* arrays onto the + * internal PCIe host (apple,t6020-pcie, identified by its "rc" + * reg-name) from the ADT /arm-io/apcie0 at boot, matching aurora + * #1208's single-lane t8140 shape. That publish is idempotent and + * returns early if apple,apcie is already present, so there must be NO + * static apple,apcie or apcie-storage node here -- a static one blocks + * the loader (seen on the J616s: a static apple,apcie made + * the driver see an empty node and refuse). Until the loader fills it, + * the driver NULL-guards every window and a real sleep with ANS stopped + * refuses (suspend-to-idle stays the default). The ST power words + * (ps_ans, ps_apcie_st0/st1, ps_apcie_sys_st0/st1) live in SOC-East and + * are driven by the controller's soc-east window, not from a node here. + */ + }; }; &pmc { From d7c68acf229ff5d46ea3c79a7104d1bdcebe9f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:12:27 +0100 Subject: [PATCH 004/269] arm64: dts: apple: t6040: give each USB4 router its Type-C PHY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 ACIO driver starts a router only over its port's Type-C PHY in CIO mode and refuses to probe without apple,atc-phy, but the routers' nodes never named their PHY. With the J616s left back port's router enabled, ACIO0 failed to probe ("Missing apple,atc-phy"), and the three of them would on any board that enables them. Name atcphy0..2, the PHY at the same port as the router (ADT acioN atc-phy-parent), and make the property required for the T6040 router. Signed-off-by: František Balázsy --- .../bindings/thunderbolt/apple,t8103-usb4-acio.yaml | 1 + arch/arm64/boot/dts/apple/t6040-usb4.dtsi | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml index b18a475f5a355..c684093b994d9 100644 --- a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml +++ b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml @@ -291,6 +291,7 @@ allOf: - nhi@1f00000 - interrupts - interrupt-names + - apple,atc-phy else: required: - iommu@a80000 diff --git a/arch/arm64/boot/dts/apple/t6040-usb4.dtsi b/arch/arm64/boot/dts/apple/t6040-usb4.dtsi index cd3b2c21aca7a..13b50c18e50c0 100644 --- a/arch/arm64/boot/dts/apple/t6040-usb4.dtsi +++ b/arch/arm64/boot/dts/apple/t6040-usb4.dtsi @@ -36,6 +36,8 @@ pinctrl-names = "lstx"; pinctrl-0 = <&usb4_0_lstx_pins>; lstx-gpios = <&pinctrl_aop 49 GPIO_ACTIVE_HIGH>; + /* The router starts only over its port's Type-C PHY in CIO mode. */ + apple,atc-phy = <&atcphy0>; power-domains = <&ps_atc0_cio>, <&ps_atc0_cio_pcie>, <&ps_atc0_cio_usb>, <&ps_atc0_pcie>; thunderbolt-switch; @@ -133,6 +135,8 @@ pinctrl-names = "lstx"; pinctrl-0 = <&usb4_1_lstx_pins>; lstx-gpios = <&pinctrl_aop 51 GPIO_ACTIVE_HIGH>; + /* The router starts only over its port's Type-C PHY in CIO mode. */ + apple,atc-phy = <&atcphy1>; power-domains = <&ps_atc1_cio>, <&ps_atc1_cio_pcie>, <&ps_atc1_cio_usb>, <&ps_atc1_pcie>; thunderbolt-switch; @@ -230,6 +234,8 @@ pinctrl-names = "lstx"; pinctrl-0 = <&usb4_2_lstx_pins>; lstx-gpios = <&pinctrl_aop 53 GPIO_ACTIVE_HIGH>; + /* The router starts only over its port's Type-C PHY in CIO mode. */ + apple,atc-phy = <&atcphy2>; power-domains = <&ps_atc2_cio>, <&ps_atc2_cio_pcie>, <&ps_atc2_cio_usb>, <&ps_atc2_pcie>; thunderbolt-switch; From ff6a5d2ef2e03ab017a090225d969647b7636ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 22:50:10 +0100 Subject: [PATCH 005/269] arm64: dts: apple: put every AP pin back after a T6040 S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The arch/arm64/boot/dts/apple/t604x.dtsi part of "pinctrl: apple: put every AP pin back after a T6040 S2R", which goes with the device tree changes; the rest of that commit is with the sleep controller changes. The AP GPIO block needs the saved configuration of every pin back on the system wake, except the pins the ADT lists in no-restore-gpio-pins (190-192 on J616s). Linux put back only the pins it set up itself. A sleep that cuts the block's power then left the pins the boot loader set up for others at their reset values, among them the chip select of the internal panel's TCON bus; after the wake the display firmware powered the panel's rails and never saw it come up ("No device added after powering on the rails. HPD=0"). Mark the AP block apple,resume-restore-all with the ADT's no-restore list, snapshot every readable pin from the hardware at suspend_noirq and write back the ones that changed at resume_noirq, logging the pins that were not Linux's. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t604x.dtsi | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t604x.dtsi b/arch/arm64/boot/dts/apple/t604x.dtsi index 3484262fa7a43..a032ea8b3b07f 100644 --- a/arch/arm64/boot/dts/apple/t604x.dtsi +++ b/arch/arm64/boot/dts/apple/t604x.dtsi @@ -1370,6 +1370,10 @@ #gpio-cells = <2>; gpio-ranges = <&pinctrl 0 0 200>; apple,npins = <200>; + /* ADT gpio0: every pin is put back after a sleep, except no-restore-gpio-pins. */ + apple,resume-restore-all; + /* Pins the firmware lists as no-restore */ + gpio-reserved-ranges = <190 3>; interrupt-controller; #interrupt-cells = <2>; From 3e320127b55bdb58dfc0b8f48d3b6b467d8a9f56 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 18:14:11 +1100 Subject: [PATCH 006/269] arm64: dts: apple: name the T8140 driver instead of its pull request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The arch/arm64/boot/dts/apple/t6040.dtsi part of "soc: apple: t6040-sleep: name the T8140 driver instead of its pull request", which goes with the device tree changes; the rest of that commit is with the sleep controller changes. Comments in the T6040 sleep code, the display driver and the T6040 device tree pointed at the T8140 sleep driver by a pull request number. Name the driver (sleep.c) or the T8140 instead, and drop the "currently unreachable" note on the sleep controller node. No functional change. Signed-off-by: Ryan Murray Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040.dtsi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index 3d657f7c9a6dd..d34974207ded0 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -254,7 +254,7 @@ sleep-controller { compatible = "apple,t6040-sleep"; /* - * The SoC blocks the (currently unreachable) commit and wake + * The SoC blocks the commit and wake * write, all shared read/write with their owning drivers and * touched only once no blocker remains: * cpu-start pmgr reg0 + 0x88000 (misc-cores): CPU start/stop @@ -298,8 +298,8 @@ * object (aura) publishes apple,apcie here plus the axi2af/cio3pll/ * clkgen/phy-block/phy-ip windows and apple,tunable-* arrays onto the * internal PCIe host (apple,t6020-pcie, identified by its "rc" - * reg-name) from the ADT /arm-io/apcie0 at boot, matching aurora - * #1208's single-lane t8140 shape. That publish is idempotent and + * reg-name) from the ADT /arm-io/apcie0 at boot, matching the + * single-lane t8140 shape. That publish is idempotent and * returns early if apple,apcie is already present, so there must be NO * static apple,apcie or apcie-storage node here -- a static one blocks * the loader (seen on the J616s: a static apple,apcie made From 152b2c075c7f4bc3ad1efb19343cddefd1c46396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:55:43 +0100 Subject: [PATCH 007/269] arm64: dts: apple: enable the sleep controller per board MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The arch/arm64/boot/dts/apple/t6040-j616s.dts, arch/arm64/boot/dts/apple/t6040.dtsi part of "arm64: dts: apple: t6040: enable the sleep controller per board", which goes with the device tree changes; the rest of that commit is with the sleep controller changes. The T6040 sleep controller's node is in the SoC's file, so every T6040 board has it, while its sleep has been run on the J616s alone. It maps fixed T6040 blocks and installs suspend operations that make deep the default state and then refuse it without t6040_sleep.allow_entry, which is of no use on a board nobody has tested, and distribution kernels build the controller. Disable the node in t6040.dtsi and enable it in t6040-j616s.dts. On the J614s and the J773s the controller no longer binds: suspend-to-idle stays their sleep, and the debugfs preflight file is gone there. The interrupt controller saved its configuration for a sleep on every apple,t6041-aic3 whenever the controller was built. Key that on the enabled node as well, so that the other machines with this AIC keep their suspend and hibernation paths as they were. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040-j616s.dts | 5 +++++ arch/arm64/boot/dts/apple/t6040.dtsi | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t6040-j616s.dts b/arch/arm64/boot/dts/apple/t6040-j616s.dts index 2a1e83d454c7c..4cf87185fe811 100644 --- a/arch/arm64/boot/dts/apple/t6040-j616s.dts +++ b/arch/arm64/boot/dts/apple/t6040-j616s.dts @@ -84,6 +84,11 @@ apple,chassis-name = "J616"; }; +/* Suspend-to-RAM through the boot object has been tested on this board. */ +&t6040_sleep { + status = "okay"; +}; + &sound { compatible = "apple,j616-macaudio", "apple,macaudio"; model = "MacBook Pro J616"; diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index d34974207ded0..d2d5622ff64d0 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -251,7 +251,7 @@ #size-cells = <1>; }; - sleep-controller { + t6040_sleep: sleep-controller { compatible = "apple,t6040-sleep"; /* * The SoC blocks the commit and wake @@ -309,6 +309,8 @@ * (ps_ans, ps_apcie_st0/st1, ps_apcie_sys_st0/st1) live in SOC-East and * are driven by the controller's soc-east window, not from a node here. */ + /* Only on a board whose sleep has been tested. */ + status = "disabled"; }; }; From dd04dd7b1d85a85dc8b837b992ca22fca7398819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:11:07 +0100 Subject: [PATCH 008/269] dt-bindings: power: apple,t6040-sleep: describe the node and add apple,apcie MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The description spoke of what a driver does with the node. Say what the node is: the blocks an operating system at EL2 drives itself to suspend the T6040 to RAM, and the display power states that have to be off before it. The boot loader adds apple,apcie to the node, the PCIe host whose shared registers and tunables the storage controller's link needs after a sleep, as on the T8140. The binding did not allow it. The pipe list also lacked the description a vendor property needs, which the binding check reported. Signed-off-by: František Balázsy --- .../bindings/power/apple,t6040-sleep.yaml | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml index 29ab8c7267c18..820e0b636b1d2 100644 --- a/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml +++ b/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml @@ -9,15 +9,19 @@ title: Apple T6040 system sleep controller maintainers: - František Balázsy -description: - The power states the platform checks before it lets the T6040 sleep engine take - the SoC down (the ADT's device-check-ps-b4-sleep table). Each entry names a - PMGR register block, the offset of a display pipe's CPU power state and the - offset of its last sub-block; the states in between are 8 bytes apart. +description: | + What an operating system that runs at EL2, with no firmware below it to ask, + drives itself to suspend the T6040 to RAM and to bring it back: the CPU + start block, the always-on watchdog, the complex and power-gate words, the + PMU's SPMI controllers and the always-on timer that starts the SoC's sleep + engine. All of these register blocks are shared with the devices that own + them. Without the blocks a suspend can be checked, and not entered. - The optional register blocks are the SoC words the entry and the wake write. - All of them are shared with the drivers that own them, and are touched only - once nothing blocks the sleep any more. + The sleep engine waits for the display pipes, so the node lists the power + states that have to be off before a sleep, as the ADT's + device-check-ps-b4-sleep table does. Each entry names a PMGR register block, + the offset of a display pipe's CPU power state and the offset of its last + sub-block; the states in between are 8 bytes apart. properties: compatible: @@ -55,6 +59,9 @@ properties: apple,check-ps-b4-sleep: $ref: /schemas/types.yaml#/definitions/phandle-array + description: + The display pipes whose power states have to be off before a sleep, one + entry per pipe. items: items: - description: PMGR syscon @@ -64,6 +71,14 @@ properties: apple,check-ps-b4-sleep-names: description: One name per apple,check-ps-b4-sleep entry. + apple,apcie: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The PCIe host whose shared registers and tunables the storage controller's + link depends on. The boot loader adds it, with those registers and + tunables on the host's node. Without it the storage controller can't be + stopped for a sleep. + required: - compatible - apple,check-ps-b4-sleep From 6e41315b7fddc5782a80e453d23ab4f8ab57f74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:11:07 +0100 Subject: [PATCH 009/269] MAINTAINERS: describe the T6040 part as it is MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The MAINTAINERS part of "soc: apple: sleep: describe the T6040 part as it is", which goes with the device tree changes; the rest of that commit is with the sleep controller changes. The file's opening comment still explained why the T6040 controller was a driver of its own next to the generic one, and several comments named functions the generic driver no longer has. Describe the part as it is now, with suspend-to-RAM offered only to a system booted with t6040_sleep.allow_entry=1, name the generic driver's functions, and say in sleep.c that a SoC may bring a part. List the private header next to the driver's sources. Signed-off-by: František Balázsy --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 3f919f3e327f3..5cec0c882a568 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2136,6 +2136,7 @@ F: drivers/rtc/rtc-apple-pmu.c F: drivers/soc/apple/aop.rs F: drivers/soc/apple/mcc.c F: drivers/soc/apple/sleep*.c +F: drivers/soc/apple/sleep-internal.h F: drivers/tty/serial/apple_dockchannel* F: include/linux/soc/apple/pci-apple-piodma.h F: include/linux/soc/apple/sleep.h From 8842a1ff871e85d020756de3886a3c2a1c4b0a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:12:02 +0100 Subject: [PATCH 010/269] dt-bindings: display: Add Apple T6041 display power words MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6041 display coprocessors (apple,t6041-dcp, apple,t6041-dcpext) name an apple,display-power node whose "ps" and "pmc-dcp" windows hold the power-state words their driver lowers after it stops the firmware for a suspend-to-RAM and raises on the wake, and the display PMC word that counts the powered coprocessors. The boot loader publishes it with the compatible apple,t6041-display-power: one node over the east power manager block for the internal coprocessor, one over the west block that both external coprocessors name. Describe it, as apple,t8140-display-power.yaml does for T8140. Signed-off-by: František Balázsy --- .../display/apple,t6041-display-power.yaml | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Documentation/devicetree/bindings/display/apple,t6041-display-power.yaml diff --git a/Documentation/devicetree/bindings/display/apple,t6041-display-power.yaml b/Documentation/devicetree/bindings/display/apple,t6041-display-power.yaml new file mode 100644 index 0000000000000..ab19f769ea9f4 --- /dev/null +++ b/Documentation/devicetree/bindings/display/apple,t6041-display-power.yaml @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/display/apple,t6041-display-power.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T6040 display power words + +maintainers: + - František Balázsy + +description: | + The words a T6040 (M4 Pro) display coprocessor's driver writes to lower + its power domains after it stops the firmware for a suspend-to-RAM and to + raise them again on the wake. A display coprocessor names this node in its + apple,display-power property. The words belong to the power manager and + the display block, so no driver binds it. + + The internal display coprocessor has its power-state words in the SoC's + east power manager block, the external ones in the west block, each at its + own offsets; the display PMC word that counts the powered coprocessors is + shared by all three. The boot loader publishes one node for the internal + coprocessor and one that both external coprocessors name. + +properties: + compatible: + const: apple,t6041-display-power + + reg: + items: + - description: a register window, named by reg-names + - description: a register window, named by reg-names + + reg-names: + description: + pmc-dcp is the display PMC word that counts the powered coprocessors, + ps the power manager block that holds the coprocessors' power-state + words. The two may come in either order. + oneOf: + - items: + - const: pmc-dcp + - const: ps + - items: + - const: ps + - const: pmc-dcp + +required: + - compatible + - reg + - reg-names + +additionalProperties: false + +examples: + - | + soc { + #address-cells = <2>; + #size-cells = <2>; + + display-power@5048a9c14 { + compatible = "apple,t6041-display-power"; + reg = <0x5 0x048a9c14 0x0 0x8>, + <0x5 0x02280000 0x0 0x14000>; + reg-names = "pmc-dcp", "ps"; + }; + + display-power@502800000 { + compatible = "apple,t6041-display-power"; + reg = <0x5 0x02800000 0x0 0x14000>, + <0x5 0x048a9c14 0x0 0x8>; + reg-names = "ps", "pmc-dcp"; + }; + }; From 8f54ea6eb5376be9359f389861dd570aa643b438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:19:27 +0100 Subject: [PATCH 011/269] arm64: dts: apple: t6040: describe what the boot loader adds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment on the sleep controller's storage link described how the boot loader's publish behaves and recalled what a static property had done on a J616s. Say what the tree has to give and what the boot loader adds: apple,apcie and the PCIe host's windows and tunables come from the boot loader, which leaves a node that has apple,apcie alone, so the tree gives neither. Comment only; the DTBs are unchanged. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040.dtsi | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index d2d5622ff64d0..10d88b34510fc 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -294,20 +294,17 @@ apple,check-ps-b4-sleep-names = "disp", "dispext0", "dispext1", "dispext2", "dispext3"; /* - * ANS's storage PCIe (two ST lanes + shared PHY), for its sleep. The boot - * object (aura) publishes apple,apcie here plus the axi2af/cio3pll/ - * clkgen/phy-block/phy-ip windows and apple,tunable-* arrays onto the - * internal PCIe host (apple,t6020-pcie, identified by its "rc" - * reg-name) from the ADT /arm-io/apcie0 at boot, matching the - * single-lane t8140 shape. That publish is idempotent and - * returns early if apple,apcie is already present, so there must be NO - * static apple,apcie or apcie-storage node here -- a static one blocks - * the loader (seen on the J616s: a static apple,apcie made - * the driver see an empty node and refuse). Until the loader fills it, - * the driver NULL-guards every window and a real sleep with ANS stopped - * refuses (suspend-to-idle stays the default). The ST power words - * (ps_ans, ps_apcie_st0/st1, ps_apcie_sys_st0/st1) live in SOC-East and - * are driven by the controller's soc-east window, not from a node here. + * apple,apcie, for the sleep of ANS's storage PCIe link (two ST lanes + * and a shared PHY), comes from the boot loader. From the ADT's + * /arm-io/apcie0 it adds it here and puts the axi2af, cio3pll, clkgen, + * phy-block and phy-ip windows and the apple,tunable-* arrays on the + * internal PCIe host (apple,t6020-pcie, the one with an "rc" + * reg-name), in the single-lane T8140 shape. It leaves a node that + * already has apple,apcie alone, so this tree must give neither + * apple,apcie nor an apcie-storage node. Without it a sleep with ANS + * stopped is refused, and suspend-to-idle stays the default. The ST + * power words (ps_ans, ps_apcie_st0/st1, ps_apcie_sys_st0/st1) are in + * SOC-East and are driven through the soc-east window. */ /* Only on a board whose sleep has been tested. */ status = "disabled"; From 9257f406f053c78a0cbaebda14b89f6f4255bd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:21:48 +0100 Subject: [PATCH 012/269] arm64: dts: apple: t6040: give the sleep controller its unit address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep controller has registers but its node had no unit address, which dtc reports (unit_address_vs_reg). Name it after its first register block, as the T8140 tree does. The boot loader and the kernel find the node by its compatible, not by its path. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040.dtsi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index 10d88b34510fc..c3d887b9cff2f 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -251,7 +251,7 @@ #size-cells = <1>; }; - t6040_sleep: sleep-controller { + t6040_sleep: sleep-controller@502888000 { compatible = "apple,t6040-sleep"; /* * The SoC blocks the commit and wake From 7f78dbc7338d8223f6779375773152860a667afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:25:25 +0100 Subject: [PATCH 013/269] dt-bindings: soc: apple: move the T6040 sleep controller next to T8140 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 sleep controller binding sat under power/, the T8140 one under soc/apple/, for the same kind of block. Move it next to the T8140 one, give apple,check-ps-b4-sleep-names its string-array type, and list the file under the Apple SoC entry in MAINTAINERS. Signed-off-by: František Balázsy --- .../bindings/{power => soc/apple}/apple,t6040-sleep.yaml | 3 ++- MAINTAINERS | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) rename Documentation/devicetree/bindings/{power => soc/apple}/apple,t6040-sleep.yaml (96%) diff --git a/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml similarity index 96% rename from Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml rename to Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml index 820e0b636b1d2..3b4ad81508271 100644 --- a/Documentation/devicetree/bindings/power/apple,t6040-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml @@ -1,7 +1,7 @@ # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) %YAML 1.2 --- -$id: http://devicetree.org/schemas/power/apple,t6040-sleep.yaml# +$id: http://devicetree.org/schemas/soc/apple/apple,t6040-sleep.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# title: Apple T6040 system sleep controller @@ -69,6 +69,7 @@ properties: - description: offset of its last sub-block's power state apple,check-ps-b4-sleep-names: + $ref: /schemas/types.yaml#/definitions/string-array description: One name per apple,check-ps-b4-sleep entry. apple,apcie: diff --git a/MAINTAINERS b/MAINTAINERS index 5cec0c882a568..8ef75ca7eca68 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2110,6 +2110,7 @@ F: Documentation/devicetree/bindings/rtc/apple,j700-pmu-rtc.yaml F: Documentation/devicetree/bindings/serial/apple,dockchannel-uart.yaml F: Documentation/devicetree/bindings/soc/apple/apple,dockchannel.yaml F: Documentation/devicetree/bindings/soc/apple/apple,rtk-helper-asc4.yaml +F: Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t6041-aop.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t8140-mcc.yaml From 05bc00c4c6fa15d97d752ed094758a2727dbaef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:28:06 +0100 Subject: [PATCH 014/269] dt-bindings: chosen: document aurora,s2r-mailbox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The boot object publishes /chosen/aurora,s2r-mailbox, and the arm64 CPU ops, the spin-table hibernation and the Apple sleep driver read it, but no binding described it. Document what its presence means and the layout of the mailbox it points at. Signed-off-by: František Balázsy --- .../bindings/chosen/aurora,s2r-mailbox.yaml | 48 +++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml diff --git a/Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml b/Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml new file mode 100644 index 0000000000000..a48f9bbc5ae3e --- /dev/null +++ b/Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/chosen/aurora,s2r-mailbox.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Return path of a boot object that owns the CPU reset vectors + +maintainers: + - František Balázsy + +description: | + On Apple silicon the boot firmware locks every CPU's reset vector to the + boot object it started. A CPU that comes back from a power-off, and every + CPU after a suspend to RAM, therefore restarts in the boot object, not in the + kernel. A boot object that sends such a CPU back to the kernel says so with + this property: a secondary CPU returns through its spin-table release + address, and the boot CPU of a resume jumps, at EL2 with the MMU off, to the + address the kernel left in the mailbox. Its presence also tells the kernel + that its spin-table CPUs may leave the kernel and come back. + + The mailbox is four 64-bit words in the CPU's byte order: + 0: the magic value 0x3152325341525541 ("AURAS2R1"), written by the boot + object before the kernel starts + 1: the MPIDR of the only CPU that may take the resume address + 2: the resume address, set by the kernel before it suspends and cleared + by the boot object when a CPU takes it + 3: how many times the resume address was taken + +select: + properties: + $nodename: + const: chosen + required: + - $nodename + +properties: + $nodename: + const: chosen + + aurora,s2r-mailbox: + $ref: /schemas/types.yaml#/definitions/uint64 + description: + Physical address of the mailbox, inside the boot object's own memory, + which the kernel does not use. + +additionalProperties: true +... diff --git a/MAINTAINERS b/MAINTAINERS index 8ef75ca7eca68..91be6606d41ff 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2094,6 +2094,7 @@ F: include/linux/mt7932.h F: include/linux/soc/apple/pmp-report.h F: Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-quiesced.yaml F: Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml +F: Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml F: Documentation/devicetree/bindings/display/apple,dcp.yaml F: Documentation/devicetree/bindings/display/apple,display-subsystem.yaml F: Documentation/devicetree/bindings/display/apple,t8140-display-power.yaml From 9953429fc4d203792c3255e5702b017a57456b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:52 +0100 Subject: [PATCH 015/269] dt-bindings: chosen: document the aura hibernation layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The kernel hibernates on Apple silicon only where the boot object says, in /chosen, that it keeps the same memory layout on every boot, and it reads the device tree of the resuming boot at the address the boot object gives there. Neither property had a binding, and their prefix was not a registered one. Register the prefix and describe the two properties. The prefix is the name of the boot loader that defined them, as u-boot's is: its released versions publish the properties under this name, so a kernel that looked for another one would not hibernate with any of them. Signed-off-by: František Balázsy --- .../bindings/chosen/aura,hibernate.yaml | 64 +++++++++++++++++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 1 + 3 files changed, 67 insertions(+) create mode 100644 Documentation/devicetree/bindings/chosen/aura,hibernate.yaml diff --git a/Documentation/devicetree/bindings/chosen/aura,hibernate.yaml b/Documentation/devicetree/bindings/chosen/aura,hibernate.yaml new file mode 100644 index 0000000000000..8c8e9390b1e60 --- /dev/null +++ b/Documentation/devicetree/bindings/chosen/aura,hibernate.yaml @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/chosen/aura,hibernate.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Memory layout a boot object keeps the same on every boot + +maintainers: + - František Balázsy + +description: | + A hibernation image puts every saved page back into the frame it was taken + from, so the boot that resumes an image has to give the kernel the same RAM, + and keep the same ranges from it, as the boot that wrote the image. On Apple + silicon the boot firmware loads the boot object at an address that changes + from boot to boot, and what the boot object keeps alive after the hand-off + moves with it. A boot object that instead keeps everything from the start of + RAM up to a fixed end from the kernel, at the same addresses on every boot, + says so with aura,hibernate-layout. Without it the kernel neither hibernates + nor resumes. + + The boot firmware still places some firmware segments anew on every boot, + inside that range, and a kernel restored from an image holds the device tree + of the boot that wrote the image. The boot object therefore puts the device + tree of every boot at one address inside the range, given in + aura,hibernate-dt. The image does not contain that memory, so the restored + kernel reads there what the resuming boot placed. + + The properties are named after aura, the boot loader that defined them and + whose released versions publish them. + +select: + properties: + $nodename: + const: chosen + required: + - $nodename + +properties: + $nodename: + const: chosen + + aura,hibernate-layout: + $ref: /schemas/types.yaml#/definitions/uint64-array + description: + The range of RAM that is kept from the kernel and laid out the same on + every boot. + items: + - description: Physical address of the start of RAM. + - description: Physical address of the end of the range, exclusive. + + aura,hibernate-dt: + $ref: /schemas/types.yaml#/definitions/uint64 + description: + Physical address, inside the range of aura,hibernate-layout, of the + device tree the kernel is started with. The kernel reads at most 2 MiB + there. + +dependencies: + aura,hibernate-dt: [ 'aura,hibernate-layout' ] + +additionalProperties: true +... diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index 4775b267e1f77..ed0d32cde3614 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -207,6 +207,8 @@ patternProperties: description: Atmel Corporation "^auo,.*": description: AU Optronics Corporation + "^aura,.*": + description: aura boot loader for Apple silicon "^aurora,.*": description: Aurora Silicon "^auvidea,.*": diff --git a/MAINTAINERS b/MAINTAINERS index 91be6606d41ff..b7da6fb00b4d3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2093,6 +2093,7 @@ F: drivers/soc/apple/pmp.rs F: include/linux/mt7932.h F: include/linux/soc/apple/pmp-report.h F: Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-quiesced.yaml +F: Documentation/devicetree/bindings/chosen/aura,hibernate.yaml F: Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml F: Documentation/devicetree/bindings/chosen/aurora,s2r-mailbox.yaml F: Documentation/devicetree/bindings/display/apple,dcp.yaml From c4404fc705d173febc5644de3934ca75d09b5cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:26 +0100 Subject: [PATCH 016/269] dt-bindings: soc: apple: t6040-sleep: require the register blocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep controller cannot enter a suspend or undo one without any of its eleven register blocks, and every T6040 board lists all of them. Make reg and reg-names required with all eleven entries instead of accepting a node with only the first two, and give the example the node it describes. Signed-off-by: František Balázsy --- .../bindings/soc/apple/apple,t6040-sleep.yaml | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml index 3b4ad81508271..443c335cf7df5 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml @@ -15,7 +15,7 @@ description: | start block, the always-on watchdog, the complex and power-gate words, the PMU's SPMI controllers and the always-on timer that starts the SoC's sleep engine. All of these register blocks are shared with the devices that own - them. Without the blocks a suspend can be checked, and not entered. + them, and all of them are needed to enter a suspend and to undo it. The sleep engine waits for the display pipes, so the node lists the power states that have to be off before a sleep, as the ADT's @@ -28,7 +28,6 @@ properties: const: apple,t6040-sleep reg: - minItems: 2 items: - description: CPU start/stop block (pmgr + 0x88000) - description: AON watchdog @@ -43,7 +42,6 @@ properties: - description: AOP global block, S2R control and wake masks reg-names: - minItems: 2 items: - const: cpu-start - const: wdt @@ -82,6 +80,8 @@ properties: required: - compatible + - reg + - reg-names - apple,check-ps-b4-sleep - apple,check-ps-b4-sleep-names @@ -89,8 +89,27 @@ additionalProperties: false examples: - | - sleep-controller { - compatible = "apple,t6040-sleep"; - apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>; - apple,check-ps-b4-sleep-names = "disp"; + soc { + #address-cells = <2>; + #size-cells = <2>; + + sleep-controller@502888000 { + compatible = "apple,t6040-sleep"; + reg = <0x5 0x02888000 0x0 0x20>, + <0x5 0x0836c000 0x0 0x4000>, + <0x5 0x02820000 0x0 0x20>, + <0x5 0x0282c000 0x0 0x1000>, + <0x5 0x00000000 0x0 0x80000>, + <0x5 0x09014000 0x0 0x1000>, + <0x5 0x09188000 0x0 0x1000>, + <0x5 0x08900000 0x0 0x8000>, + <0x5 0x02280000 0x0 0x1000>, + <0x5 0x02800000 0x0 0x1000>, + <0x5 0x08004000 0x0 0x1000>; + reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", + "clkgen", "spmi0", "spmi-a0", "mastermini", + "soc-east", "soc-ps", "aop-global"; + apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>; + apple,check-ps-b4-sleep-names = "disp"; + }; }; From eedb5c236474c689e43d0aeeeb090b01af8d6e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:39 +0100 Subject: [PATCH 017/269] dt-bindings: soc: apple: t6040-sleep: size the power word pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The soc-east and soc-ps windows are the SOC-East and SOC power word pages, which the driver reads and writes as one range each: 0xd000 and 0x9000 bytes. Describe them as the pages they are, so that one mapping of each serves every power word in them. Signed-off-by: František Balázsy --- .../devicetree/bindings/soc/apple/apple,t6040-sleep.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml index 443c335cf7df5..eab4fcf4725fd 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml @@ -37,8 +37,8 @@ properties: - description: SPMI controller of the PMU (boot stage, notify, hibernate) - description: second SPMI controller's notify word - description: MasterMini block with the S2R timer - - description: SOC-East retention and power words - - description: PMGR power words of the west axi2af bridges + - description: SOC-East page with retention, fabric, DCS, SIO and other power words + - description: SOC page with the west axi2af, ATC, fabric and DCS power words - description: AOP global block, S2R control and wake masks reg-names: @@ -103,8 +103,8 @@ examples: <0x5 0x09014000 0x0 0x1000>, <0x5 0x09188000 0x0 0x1000>, <0x5 0x08900000 0x0 0x8000>, - <0x5 0x02280000 0x0 0x1000>, - <0x5 0x02800000 0x0 0x1000>, + <0x5 0x02280000 0x0 0xd000>, + <0x5 0x02800000 0x0 0x9000>, <0x5 0x08004000 0x0 0x1000>; reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", "clkgen", "spmi0", "spmi-a0", "mastermini", From 5983f84a5b57b630f9992d42dc2a31ea5494bbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:39 +0100 Subject: [PATCH 018/269] arm64: dts: apple: t6040: size the sleep controller's power word pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Give the soc-east and soc-ps windows of the sleep controller the extent of the SOC-East (0x502280000) and SOC (0x502800000) power word pages, 0xd000 and 0x9000 bytes, instead of the first 0x1000 of each. The addresses are unchanged. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040.dtsi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index c3d887b9cff2f..6440c686c1af2 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -265,8 +265,8 @@ * spmi0 0x509014000: the PMU boot-stage + NOTIFY + go-hib * spmi-a0 0x509188000: the second SPMI NOTIFY * mastermini 0x508900000: the S2R timer at +0x4000 (the commit) - * soc-east 0x502280000: SOC-East retention + east power words - * soc-ps 0x502800000: the west axi2af power words + * soc-east 0x502280000: SOC-East page, retention + east power words + * soc-ps 0x502800000: SOC page, the west axi2af power words * aop-global 0x508004000: the S2R control + wake masks/status * The handler refuses before touching any of them. */ @@ -278,8 +278,8 @@ <0x5 0x09014000 0x0 0x1000>, <0x5 0x09188000 0x0 0x1000>, <0x5 0x08900000 0x0 0x8000>, - <0x5 0x02280000 0x0 0x1000>, - <0x5 0x02800000 0x0 0x1000>, + <0x5 0x02280000 0x0 0xd000>, + <0x5 0x02800000 0x0 0x9000>, <0x5 0x08004000 0x0 0x1000>; reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", "clkgen", "spmi0", "spmi-a0", "mastermini", From 9caa407d37e1ed08dee6b157aa3d6c7d2275d422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:42:11 +0100 Subject: [PATCH 019/269] dt-bindings: soc: apple: describe the sleep controllers in one schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040, T8132 and T8140 sleep controllers do the same job and were described by two schemas that named the same blocks differently. Describe them in apple,sleep.yaml, with the register names per compatible, and give the blocks the T6040 shares with the T8140 the T8140's names: cluster for the CPU complexes' control words, pwrgate for the power-gate and voter block, spmi for the PMU's SPMI controller and wake for the AOP block with the wake masks. The schema keeps what each SoC requires and refuses: the T6040 needs all eleven of its register blocks, the SOC-East and SOC pages sized as the driver maps them, and the display power-state table; the T8140 needs its 28 blocks, the wake sources and the display coprocessors; the T8132 its 25 blocks, the clock protection page as an optional 26th, and the display coprocessors. The controllers are listed in MAINTAINERS under the one file. The T6040 device tree and driver follow in the next two patches. Signed-off-by: František Balázsy --- .../bindings/soc/apple/apple,sleep.yaml | 262 ++++++++++++++++++ .../bindings/soc/apple/apple,t6040-sleep.yaml | 115 -------- .../bindings/soc/apple/apple,t8140-sleep.yaml | 158 ----------- MAINTAINERS | 3 +- 4 files changed, 263 insertions(+), 275 deletions(-) create mode 100644 Documentation/devicetree/bindings/soc/apple/apple,sleep.yaml delete mode 100644 Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml delete mode 100644 Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml diff --git a/Documentation/devicetree/bindings/soc/apple/apple,sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,sleep.yaml new file mode 100644 index 0000000000000..d084121d3bfb1 --- /dev/null +++ b/Documentation/devicetree/bindings/soc/apple/apple,sleep.yaml @@ -0,0 +1,262 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/soc/apple/apple,sleep.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple SoC system sleep controls + +maintainers: + - Diljit Singh + - František Balázsy + +description: | + The registers a kernel running directly at EL2 on an Apple SoC writes to enter + and return from suspend-to-RAM and to power its secondary CPUs off. They belong + to several hardware blocks, some of which other drivers also use, so this node + overlaps those drivers' register ranges and the driver does not claim them. + Each SoC has its own compatible, as its power domains differ. Blocks that + several SoCs have are named alike. + + On the T6040 the sleep engine waits for the display pipes, so the node lists + the power states that have to be off before a sleep, as the ADT's + device-check-ps-b4-sleep table does. Each entry names a PMGR register block, + the offset of a display pipe's CPU power state and the offset of its last + sub-block; the states in between are 8 bytes apart. All of the T6040's + register blocks are needed to enter a suspend and to undo it. + +properties: + compatible: + enum: + - apple,t6040-sleep + - apple,t8132-sleep + - apple,t8140-sleep + + reg: + minItems: 11 + maxItems: 28 + + reg-names: + minItems: 11 + maxItems: 28 + + apple,wake-enable: + $ref: /schemas/types.yaml#/definitions/uint32-array + description: + The wake source IDs left enabled across suspend-to-RAM. Every other + source is masked. + + apple,apcie: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The PCIe host whose shared registers and tunables the storage controller's + link depends on. They are needed even with that host disabled. On the + T6040 the boot loader adds it, with those registers and tunables on the + host's node; without it the storage controller can't be stopped for a + sleep. + + apple,pinctrl: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The pin controller whose pins a deep sleep resets. Its pins are saved + before a sleep and put back after it, before the storage controller + starts. + + apple,pinctrl-skip-pins: + $ref: /schemas/types.yaml#/definitions/uint32-array + description: + Pins of that controller to leave alone across a sleep, neither saved nor + put back. + + apple,debug-port: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The USB-C port controller that carries the debug link, stopped for a sleep. + + apple,display-coprocessors: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: + The DCP and the external DCPs, which must be off before a commit, in that + order. + + apple,check-ps-b4-sleep: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: + The display pipes whose power states have to be off before a sleep, one + entry per pipe. + items: + items: + - description: PMGR syscon + - description: offset of the pipe's CPU power state + - description: offset of its last sub-block's power state + + apple,check-ps-b4-sleep-names: + $ref: /schemas/types.yaml#/definitions/string-array + description: One name per apple,check-ps-b4-sleep entry. + +required: + - compatible + +allOf: + - if: + properties: + compatible: + const: apple,t6040-sleep + then: + properties: + reg: + items: + - description: CPU start/stop block (pmgr + 0x88000) + - description: AON watchdog + - description: control words of the three CPU complexes + - description: SoC power gate, wake and retention voters + - description: clock generator, the clock mux park words + - description: SPMI controller of the PMU (boot stage, notify, hibernate) + - description: second SPMI controller's notify word + - description: MasterMini block with the S2R timer + - description: SOC-East page with retention, fabric, DCS, SIO and other power words + - description: SOC page with the west axi2af, ATC, fabric and DCS power words + - description: AOP global block, S2R control and wake masks + reg-names: + items: + - const: cpu-start + - const: wdt + - const: cluster + - const: pwrgate + - const: clkgen + - const: spmi + - const: spmi-a0 + - const: mastermini + - const: soc-east + - const: soc-ps + - const: wake + apple,wake-enable: false + apple,pinctrl: false + apple,pinctrl-skip-pins: false + apple,debug-port: false + apple,display-coprocessors: false + required: + - reg + - reg-names + - apple,check-ps-b4-sleep + - apple,check-ps-b4-sleep-names + + - if: + properties: + compatible: + const: apple,t8132-sleep + then: + properties: + reg: + minItems: 25 + maxItems: 26 + reg-names: + minItems: 25 + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: aon-misc + - const: wake + - const: amcc0 + - const: amcc1 + - const: pmc-dcp + - const: dvfm-e + - const: dvfm-p + - const: spmi + - const: pmc-voters + - const: dcs + - const: ps-aon + - const: spmi-a0 + - const: clk-mux + - const: mcc0 + - const: mcc1 + - const: ptd + - const: clk-perf + - const: cluster-dcs + - const: ps-prot + apple,check-ps-b4-sleep: false + apple,check-ps-b4-sleep-names: false + required: + - reg + - reg-names + - apple,display-coprocessors + + - if: + properties: + compatible: + const: apple,t8140-sleep + then: + properties: + reg: + minItems: 28 + reg-names: + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: aon-misc + - const: wake + - const: amcc0 + - const: amcc1 + - const: amcc2 + - const: amcc3 + - const: pmc-dcp + - const: dvfm-e + - const: dvfm-p + - const: spmi + - const: pmc-voters + - const: dcs + - const: ps-aon + - const: spmi-a0 + - const: clk-mux + - const: mcc0 + - const: mcc1 + - const: mcc2 + - const: ptd + - const: clk-perf + - const: cluster-dcs + apple,check-ps-b4-sleep: false + apple,check-ps-b4-sleep-names: false + required: + - reg + - reg-names + - apple,wake-enable + - apple,display-coprocessors + +additionalProperties: false + +examples: + - | + soc { + #address-cells = <2>; + #size-cells = <2>; + + sleep-controller@502888000 { + compatible = "apple,t6040-sleep"; + reg = <0x5 0x02888000 0x0 0x20>, + <0x5 0x0836c000 0x0 0x4000>, + <0x5 0x02820000 0x0 0x20>, + <0x5 0x0282c000 0x0 0x1000>, + <0x5 0x00000000 0x0 0x80000>, + <0x5 0x09014000 0x0 0x1000>, + <0x5 0x09188000 0x0 0x1000>, + <0x5 0x08900000 0x0 0x8000>, + <0x5 0x02280000 0x0 0xd000>, + <0x5 0x02800000 0x0 0x9000>, + <0x5 0x08004000 0x0 0x1000>; + reg-names = "cpu-start", "wdt", "cluster", "pwrgate", + "clkgen", "spmi", "spmi-a0", "mastermini", + "soc-east", "soc-ps", "wake"; + apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>; + apple,check-ps-b4-sleep-names = "disp"; + }; + }; diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml deleted file mode 100644 index eab4fcf4725fd..0000000000000 --- a/Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml +++ /dev/null @@ -1,115 +0,0 @@ -# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) -%YAML 1.2 ---- -$id: http://devicetree.org/schemas/soc/apple/apple,t6040-sleep.yaml# -$schema: http://devicetree.org/meta-schemas/core.yaml# - -title: Apple T6040 system sleep controller - -maintainers: - - František Balázsy - -description: | - What an operating system that runs at EL2, with no firmware below it to ask, - drives itself to suspend the T6040 to RAM and to bring it back: the CPU - start block, the always-on watchdog, the complex and power-gate words, the - PMU's SPMI controllers and the always-on timer that starts the SoC's sleep - engine. All of these register blocks are shared with the devices that own - them, and all of them are needed to enter a suspend and to undo it. - - The sleep engine waits for the display pipes, so the node lists the power - states that have to be off before a sleep, as the ADT's - device-check-ps-b4-sleep table does. Each entry names a PMGR register block, - the offset of a display pipe's CPU power state and the offset of its last - sub-block; the states in between are 8 bytes apart. - -properties: - compatible: - const: apple,t6040-sleep - - reg: - items: - - description: CPU start/stop block (pmgr + 0x88000) - - description: AON watchdog - - description: control words of the three CPU complexes - - description: SoC power gate, wake and retention voters - - description: clock generator, the clock mux park words - - description: SPMI controller of the PMU (boot stage, notify, hibernate) - - description: second SPMI controller's notify word - - description: MasterMini block with the S2R timer - - description: SOC-East page with retention, fabric, DCS, SIO and other power words - - description: SOC page with the west axi2af, ATC, fabric and DCS power words - - description: AOP global block, S2R control and wake masks - - reg-names: - items: - - const: cpu-start - - const: wdt - - const: cluster-ctl - - const: soc-pwrgate - - const: clkgen - - const: spmi0 - - const: spmi-a0 - - const: mastermini - - const: soc-east - - const: soc-ps - - const: aop-global - - apple,check-ps-b4-sleep: - $ref: /schemas/types.yaml#/definitions/phandle-array - description: - The display pipes whose power states have to be off before a sleep, one - entry per pipe. - items: - items: - - description: PMGR syscon - - description: offset of the pipe's CPU power state - - description: offset of its last sub-block's power state - - apple,check-ps-b4-sleep-names: - $ref: /schemas/types.yaml#/definitions/string-array - description: One name per apple,check-ps-b4-sleep entry. - - apple,apcie: - $ref: /schemas/types.yaml#/definitions/phandle - description: - The PCIe host whose shared registers and tunables the storage controller's - link depends on. The boot loader adds it, with those registers and - tunables on the host's node. Without it the storage controller can't be - stopped for a sleep. - -required: - - compatible - - reg - - reg-names - - apple,check-ps-b4-sleep - - apple,check-ps-b4-sleep-names - -additionalProperties: false - -examples: - - | - soc { - #address-cells = <2>; - #size-cells = <2>; - - sleep-controller@502888000 { - compatible = "apple,t6040-sleep"; - reg = <0x5 0x02888000 0x0 0x20>, - <0x5 0x0836c000 0x0 0x4000>, - <0x5 0x02820000 0x0 0x20>, - <0x5 0x0282c000 0x0 0x1000>, - <0x5 0x00000000 0x0 0x80000>, - <0x5 0x09014000 0x0 0x1000>, - <0x5 0x09188000 0x0 0x1000>, - <0x5 0x08900000 0x0 0x8000>, - <0x5 0x02280000 0x0 0xd000>, - <0x5 0x02800000 0x0 0x9000>, - <0x5 0x08004000 0x0 0x1000>; - reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", - "clkgen", "spmi0", "spmi-a0", "mastermini", - "soc-east", "soc-ps", "aop-global"; - apple,check-ps-b4-sleep = <&pmgr_disp 0x0 0x58>; - apple,check-ps-b4-sleep-names = "disp"; - }; - }; diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml deleted file mode 100644 index bdbe2a5ccadaf..0000000000000 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ /dev/null @@ -1,158 +0,0 @@ -# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) -%YAML 1.2 ---- -$id: http://devicetree.org/schemas/soc/apple/apple,t8140-sleep.yaml# -$schema: http://devicetree.org/meta-schemas/core.yaml# - -title: Apple SoC system sleep controls - -maintainers: - - Diljit Singh - -description: | - The registers a kernel running directly at EL2 on an Apple SoC writes to enter - and return from suspend-to-RAM and to power its secondary CPUs off. They belong - to several hardware blocks, some of which other drivers also use, so this node - overlaps those drivers' register ranges and the driver does not claim them. - Each SoC has its own compatible, as its power domains differ. - -properties: - compatible: - enum: - - apple,t8132-sleep - - apple,t8140-sleep - - reg: - minItems: 25 - maxItems: 28 - - reg-names: - minItems: 25 - maxItems: 28 - - apple,wake-enable: - $ref: /schemas/types.yaml#/definitions/uint32-array - description: - The wake source IDs left enabled across suspend-to-RAM. Every other - source is masked. - - apple,apcie: - $ref: /schemas/types.yaml#/definitions/phandle - description: - The PCIe host whose shared registers and tunables the storage controller's - link depends on. They are needed even with that host disabled. - - apple,pinctrl: - $ref: /schemas/types.yaml#/definitions/phandle - description: - The pin controller whose pins a deep sleep resets. Its pins are saved - before a sleep and put back after it, before the storage controller - starts. - - apple,pinctrl-skip-pins: - $ref: /schemas/types.yaml#/definitions/uint32-array - description: - Pins of that controller to leave alone across a sleep, neither saved nor - put back. - - apple,debug-port: - $ref: /schemas/types.yaml#/definitions/phandle - description: - The USB-C port controller that carries the debug link, stopped for a sleep. - - apple,display-coprocessors: - $ref: /schemas/types.yaml#/definitions/phandle-array - description: - The DCP and the external DCPs, which must be off before a commit, in that - order. - -required: - - compatible - - reg - - reg-names - - apple,display-coprocessors - -allOf: - - if: - properties: - compatible: - contains: - const: apple,t8140-sleep - then: - required: - - apple,wake-enable - properties: - reg: - minItems: 28 - maxItems: 28 - reg-names: - items: - - const: ps - - const: pwrgate - - const: cpu-start - - const: cllt - - const: cluster - - const: commit - - const: wdt - - const: aon-misc - - const: wake - - const: amcc0 - - const: amcc1 - - const: amcc2 - - const: amcc3 - - const: pmc-dcp - - const: dvfm-e - - const: dvfm-p - - const: spmi - - const: pmc-voters - - const: dcs - - const: ps-aon - - const: spmi-a0 - - const: clk-mux - - const: mcc0 - - const: mcc1 - - const: mcc2 - - const: ptd - - const: clk-perf - - const: cluster-dcs - - if: - properties: - compatible: - contains: - const: apple,t8132-sleep - then: - properties: - reg: - minItems: 25 - maxItems: 26 - reg-names: - minItems: 25 - items: - - const: ps - - const: pwrgate - - const: cpu-start - - const: cllt - - const: cluster - - const: commit - - const: wdt - - const: aon-misc - - const: wake - - const: amcc0 - - const: amcc1 - - const: pmc-dcp - - const: dvfm-e - - const: dvfm-p - - const: spmi - - const: pmc-voters - - const: dcs - - const: ps-aon - - const: spmi-a0 - - const: clk-mux - - const: mcc0 - - const: mcc1 - - const: ptd - - const: clk-perf - - const: cluster-dcs - - const: ps-prot - -additionalProperties: false diff --git a/MAINTAINERS b/MAINTAINERS index b7da6fb00b4d3..70fe19771d80e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2112,11 +2112,10 @@ F: Documentation/devicetree/bindings/rtc/apple,j700-pmu-rtc.yaml F: Documentation/devicetree/bindings/serial/apple,dockchannel-uart.yaml F: Documentation/devicetree/bindings/soc/apple/apple,dockchannel.yaml F: Documentation/devicetree/bindings/soc/apple/apple,rtk-helper-asc4.yaml -F: Documentation/devicetree/bindings/soc/apple/apple,t6040-sleep.yaml +F: Documentation/devicetree/bindings/soc/apple/apple,sleep.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t6041-aop.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t8140-mcc.yaml -F: Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml F: Documentation/devicetree/bindings/sound/apple,t6041-aop-audio.yaml F: Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml F: Documentation/devicetree/bindings/usb/vialab,vl122.yaml From d05b2d3f1ab7a132d3c0b3c247bc4725d22abd98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:43:26 +0100 Subject: [PATCH 020/269] arm64: dts: apple: t6040: name the sleep controller's blocks as the T8140's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Give the register blocks of the T6040 sleep controller that the T8140's controller has too the names the schema uses for both: cluster, pwrgate, spmi and wake. The addresses, sizes and order are unchanged; the compiled tree differs only in those four names. The driver looks the blocks up by these names and follows in the next patch. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040.dtsi | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6040.dtsi b/arch/arm64/boot/dts/apple/t6040.dtsi index 6440c686c1af2..37ce82fc38171 100644 --- a/arch/arm64/boot/dts/apple/t6040.dtsi +++ b/arch/arm64/boot/dts/apple/t6040.dtsi @@ -259,15 +259,15 @@ * touched only once no blocker remains: * cpu-start pmgr reg0 + 0x88000 (misc-cores): CPU start/stop * wdt the AON watchdog, the sleep's WD2 safety net - * cluster-ctl 0x502820000: the three CPU complexes E/P0/P1 - * soc-pwrgate 0x50282c000: wake + CPM retention voters + * cluster 0x502820000: the three CPU complexes E/P0/P1 + * pwrgate 0x50282c000: wake + CPM retention voters * clkgen 0x500000000: the clk-mux park words - * spmi0 0x509014000: the PMU boot-stage + NOTIFY + go-hib + * spmi 0x509014000: the PMU boot-stage + NOTIFY + go-hib * spmi-a0 0x509188000: the second SPMI NOTIFY * mastermini 0x508900000: the S2R timer at +0x4000 (the commit) * soc-east 0x502280000: SOC-East page, retention + east power words * soc-ps 0x502800000: SOC page, the west axi2af power words - * aop-global 0x508004000: the S2R control + wake masks/status + * wake 0x508004000: the S2R control + wake masks/status * The handler refuses before touching any of them. */ reg = <0x5 0x02888000 0x0 0x20>, @@ -281,9 +281,9 @@ <0x5 0x02280000 0x0 0xd000>, <0x5 0x02800000 0x0 0x9000>, <0x5 0x08004000 0x0 0x1000>; - reg-names = "cpu-start", "wdt", "cluster-ctl", "soc-pwrgate", - "clkgen", "spmi0", "spmi-a0", "mastermini", - "soc-east", "soc-ps", "aop-global"; + reg-names = "cpu-start", "wdt", "cluster", "pwrgate", + "clkgen", "spmi", "spmi-a0", "mastermini", + "soc-east", "soc-ps", "wake"; /* * ADT pmgr device-check-ps-b4-sleep (check-ps-b4-sleep = 0x1f): per * display pipe its CPU and its last sub-block, 8 bytes apart. From fae08585692221ea99cabb1a23c83d743ce28b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:46:35 +0100 Subject: [PATCH 021/269] dt-bindings: thunderbolt: apple: name the router's Type-C PHY in phys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 USB4 router runs only over its port's Type-C PHY and tells the PHY when it starts and stops. The PHY was named in a vendor phandle, which the router resolved to a device node and looked up by hand for each call. Describe it with the standard phys property and the "cio" name the T8132 routers already use for the same PHY, so the PHY framework resolves it and orders the router after the PHY. The T6040 routers require both. Not run on a USB4/Thunderbolt device. Signed-off-by: František Balázsy --- .../bindings/thunderbolt/apple,t8103-usb4-acio.yaml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml index c684093b994d9..b51948af8b09f 100644 --- a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml +++ b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml @@ -124,17 +124,12 @@ properties: description: The Type-C PHY whose lanes carry the router's CIO link. A host that power-cycles the router for a deep sleep takes the PHY through the - sleep with it. + sleep with it. On T6040 the router starts only while that PHY is in + CIO mode and tells it when it stops. phy-names: const: cio - apple,atc-phy: - $ref: /schemas/types.yaml#/definitions/phandle - description: - Type-C PHY of this router's port. On T6040 the router starts only while - that PHY is in CIO mode and tells it when it stops. - apple,pcie-tunnel: $ref: /schemas/types.yaml#/definitions/phandle description: @@ -291,7 +286,8 @@ allOf: - nhi@1f00000 - interrupts - interrupt-names - - apple,atc-phy + - phys + - phy-names else: required: - iommu@a80000 From a37cf5d0e61da8d0b8eb9e186c6b3cc4539f5070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:46:36 +0100 Subject: [PATCH 022/269] arm64: dts: apple: t6040: give the USB4 routers their PHY in phys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Name each router's Type-C PHY with phys, as the binding now describes, instead of the vendor phandle. The routers stay disabled here, so no default board changes. The built J616s device tree passes the router schema. Not run on a USB4/Thunderbolt device. Signed-off-by: František Balázsy --- arch/arm64/boot/dts/apple/t6040-usb4.dtsi | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6040-usb4.dtsi b/arch/arm64/boot/dts/apple/t6040-usb4.dtsi index 13b50c18e50c0..cf75493f031b3 100644 --- a/arch/arm64/boot/dts/apple/t6040-usb4.dtsi +++ b/arch/arm64/boot/dts/apple/t6040-usb4.dtsi @@ -37,7 +37,8 @@ pinctrl-0 = <&usb4_0_lstx_pins>; lstx-gpios = <&pinctrl_aop 49 GPIO_ACTIVE_HIGH>; /* The router starts only over its port's Type-C PHY in CIO mode. */ - apple,atc-phy = <&atcphy0>; + phys = <&atcphy0 PHY_TYPE_USB2>; + phy-names = "cio"; power-domains = <&ps_atc0_cio>, <&ps_atc0_cio_pcie>, <&ps_atc0_cio_usb>, <&ps_atc0_pcie>; thunderbolt-switch; @@ -136,7 +137,8 @@ pinctrl-0 = <&usb4_1_lstx_pins>; lstx-gpios = <&pinctrl_aop 51 GPIO_ACTIVE_HIGH>; /* The router starts only over its port's Type-C PHY in CIO mode. */ - apple,atc-phy = <&atcphy1>; + phys = <&atcphy1 PHY_TYPE_USB2>; + phy-names = "cio"; power-domains = <&ps_atc1_cio>, <&ps_atc1_cio_pcie>, <&ps_atc1_cio_usb>, <&ps_atc1_pcie>; thunderbolt-switch; @@ -235,7 +237,8 @@ pinctrl-0 = <&usb4_2_lstx_pins>; lstx-gpios = <&pinctrl_aop 53 GPIO_ACTIVE_HIGH>; /* The router starts only over its port's Type-C PHY in CIO mode. */ - apple,atc-phy = <&atcphy2>; + phys = <&atcphy2 PHY_TYPE_USB2>; + phy-names = "cio"; power-domains = <&ps_atc2_cio>, <&ps_atc2_cio_pcie>, <&ps_atc2_cio_usb>, <&ps_atc2_pcie>; thunderbolt-switch; From c44d77bad3f206dd3165ab9f8e3f01dcc36e3c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:02 +0100 Subject: [PATCH 023/269] PM: hibernate: tell drivers a restored image from a failed one, and allow a platform-fixed layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drivers that hand a coprocessor over across hibernation need to know, in their restore callbacks, whether the image was really restored or the PM core is unwinding an aborted hibernation. Add pm_hibernate_is_restored() (with a Rust helper) and let a boot chain that keeps the memory layout fixed (aura hib=1) admit hibernation where the platform would otherwise be refused. Signed-off-by: František Balázsy --- .../admin-guide/kernel-parameters.txt | 9 +++ include/linux/suspend.h | 6 ++ kernel/power/hibernate.c | 42 +++++++++++++- kernel/power/power.h | 1 + kernel/power/snapshot.c | 46 +++++++++++++++ kernel/power/swap.c | 58 ++++++++++++++++++- 6 files changed, 159 insertions(+), 3 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 2e6fa8b49752e..9ce5baf6854e7 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -549,6 +549,15 @@ Kernel parameters autoconf= [IPV6] See Documentation/networking/ipv6.rst. + apple_pmuv3_traps= + [ARM64,EARLY] Enable the IMPDEF PMUv3 trapping of KVM + guests (HACR_EL2 bit 56) on Apple cores where it has + not been verified yet, currently the M4 Pro/Max (T6040) + E and P cores. With it, KVM advertises a PMUv3 to its + guests backed by the Apple CPU PMU driver; without it + those guests see no PMU. Default: off on these cores. + Format: , a bare "apple_pmuv3_traps" means on. + apm= [APM] Advanced Power Management See header of arch/x86/kernel/apm_32.c. diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 365437ce2cc02..2ebbb01fa14cc 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -453,6 +453,12 @@ bool pm_hibernation_mode_is_suspend(void); static inline bool pm_hibernation_mode_is_suspend(void) { return false; } #endif +#ifdef CONFIG_HIBERNATION +bool pm_hibernate_is_restored(void); +#else +static inline bool pm_hibernate_is_restored(void) { return false; } +#endif + int arch_resume_nosmt(void); #ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 890bf7b447f89..a8d992bb4a2c7 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -81,6 +82,25 @@ static const struct platform_hibernation_ops *hibernation_ops; static atomic_t hibernate_atomic = ATOMIC_INIT(1); +/* Set when swsusp_arch_suspend() returned in a kernel restored from an image. */ +static bool hibernate_restored; + +/** + * pm_hibernate_is_restored - Whether this kernel was just restored from an image. + * + * Devices get PMSG_RESTORE both in the kernel restored from a hibernation image + * and in the kernel that hibernated when hibernation is aborted before the image + * is created (a device failed to freeze, a wakeup event). Only in the first case + * did the hardware go through a boot. True from the restore until the next + * hibernation starts (hibernation_snapshot()); meant for the restore callbacks + * and the PM_POST_HIBERNATION notifiers. + */ +bool pm_hibernate_is_restored(void) +{ + return READ_ONCE(hibernate_restored); +} +EXPORT_SYMBOL_GPL(pm_hibernate_is_restored); + #ifdef CONFIG_SUSPEND /** * pm_hibernation_mode_is_suspend - Check if hibernation has been set to suspend @@ -123,7 +143,13 @@ bool hibernation_in_progress(void) */ static bool hibernation_platform_blocked(void) { - return of_machine_is_compatible("apple,arm-platform"); + /* + * Except where the boot object gives the OS a fixed memory layout and + * the drivers rebuild their coprocessor state from the cold boot that + * restores the image (aura hib=1, see aura-hibernate.c). + */ + return of_machine_is_compatible("apple,arm-platform") && + !aura_hibernate_layout_fixed(); } static bool hibernation_generic_available(void) @@ -399,6 +425,7 @@ static int create_image(int platform_mode) if (!in_suspend) { events_check_enabled = false; clear_or_poison_free_pages(); + WRITE_ONCE(hibernate_restored, !error); } platform_leave(platform_mode); @@ -438,6 +465,7 @@ int hibernation_snapshot(int platform_mode) int error; pm_suspend_clear_flags(); + WRITE_ONCE(hibernate_restored, false); error = platform_begin(platform_mode); if (error) goto Close; @@ -774,6 +802,18 @@ static int load_image_and_restore(void) } error = swsusp_read(&flags); + if (!error) { + /* + * The image is in memory now. Clear its on-disk signature + * before the device restore, so that a restore which hangs + * (for example a co-processor that does not come back after + * the cold boot) cannot leave a valid image for the next boot + * to resume into the same hang. swsusp_close() releases the + * resume device, so do it while that is still open. + */ + if (swsusp_invalidate_sig()) + pr_warn("could not invalidate the image signature; a failed restore may repeat\n"); + } swsusp_close(); if (!error) error = hibernation_restore(flags & SF_PLATFORM_MODE); diff --git a/kernel/power/power.h b/kernel/power/power.h index 75b63843886ee..6c269ce9410ec 100644 --- a/kernel/power/power.h +++ b/kernel/power/power.h @@ -201,6 +201,7 @@ extern void swsusp_free(void); extern int swsusp_read(unsigned int *flags_p); extern int swsusp_write(unsigned int flags); void swsusp_close(void); +int swsusp_invalidate_sig(void); #ifdef CONFIG_SUSPEND extern int swsusp_unmark(void); #else diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index b209712cb2c3a..6ab21287493b5 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "power.h" @@ -2750,6 +2751,49 @@ static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) return pbe->address; } +#ifdef CONFIG_ARM64 +/* + * Most image pages are loaded straight into their original frames through + * the cacheable linear map (only conflicting pages go through restore_pblist). + * Non-coherent DMA masters (on Apple SoCs: the GPU page table walker, DCP, + * every RTKit co-processor, the NVMe queues) read DRAM, and a later eviction + * of a dirty line would overwrite what a device wrote since. Clean and + * invalidate every image page to the PoC before the jump. + */ +static void swsusp_clean_restored_image(void) +{ + unsigned long pfn, n = 0, skipped = 0; + + memory_bm_position_reset(&orig_bm); + for (pfn = memory_bm_next_pfn(&orig_bm); pfn != BM_END_OF_MAP; + pfn = memory_bm_next_pfn(&orig_bm)) { + unsigned long va; + + if (!pfn_valid(pfn)) + continue; + /* + * Not in the boot kernel's linear map (for example the linear + * alias of the kernel image, which faults when touched): + * nothing was loaded there through the linear map. Such frames + * are restored by the restore_pblist copy, which cleans each + * copied page itself. + */ + if (!kernel_page_present(pfn_to_page(pfn))) { + skipped++; + continue; + } + va = (unsigned long)page_address(pfn_to_page(pfn)); + dcache_clean_inval_poc(va, va + PAGE_SIZE); + if (!(++n & 0xffff)) + cond_resched(); + } + pr_debug("%lu image pages cleaned to PoC, %lu outside the linear map left to the copy\n", + n, skipped); +} +#else +static inline void swsusp_clean_restored_image(void) {} +#endif + /** * snapshot_write_next - Get the address to store the next image page. * @handle: Snapshot handle structure to guide the writing. @@ -2871,6 +2915,8 @@ int snapshot_write_finalize(struct snapshot_handle *handle) error = hibernate_restore_protect_page(handle->buffer); /* Do that only if we have loaded the image entirely */ if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages) { + /* while orig_bm still lists the image: recycling overwrites it */ + swsusp_clean_restored_image(); memory_bm_recycle(&orig_bm); free_highmem_data(); } diff --git a/kernel/power/swap.c b/kernel/power/swap.c index c78f1593600bb..8cc439448e053 100644 --- a/kernel/power/swap.c +++ b/kernel/power/swap.c @@ -483,11 +483,27 @@ static int flush_swap_writer(struct swap_map_handle *handle) static int swap_writer_finish(struct swap_map_handle *handle, unsigned int flags, int error) { + int flush; + if (!error) { pr_info("S"); error = mark_swapfiles(handle, flags); pr_cont("|\n"); flush_swap_writer(handle); + /* + * Make the image durable now: otherwise the device cache is + * flushed only by the device shutdown in the power-off that + * follows, and a platform whose power-off does not get that far + * loses the image. A failed flush leaves no durable image: drop + * the signature durably (unlike swsusp_unmark()) so that no + * later boot resumes from freed swap. + */ + flush = blkdev_issue_flush(file_bdev(hib_resume_bdev_file)); + if (!error && flush) { + if (swsusp_invalidate_sig()) + pr_err("image flush failed and the signature could not be dropped\n"); + error = flush; + } } if (error) @@ -1583,8 +1599,13 @@ int swsusp_check(bool exclusive) if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) { memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); swsusp_header_flags = swsusp_header->flags; - /* Reset swap signature now */ - error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC, + /* + * Reset swap signature now. FUA: a REQ_SYNC write + * can sit in a volatile device cache, and a hard + * power-off of a hung restore then loses it and the + * next boot resumes the same image again. + */ + error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC | REQ_FUA, swsusp_resume_block, swsusp_header); } else { @@ -1625,6 +1646,39 @@ void swsusp_close(void) fput(hib_resume_bdev_file); } +/* + * Clear the on-disk image signature without freeing the swap pages. + * Called on the resume path right after the image is read into memory and + * before the device restore, so a restore that hangs (a co-processor that does + * not come back after a cold boot) cannot leave a valid image that the next + * boot resumes into the same hang. Unlike swsusp_unmark() this keeps the swap + * pages: the resuming kernel is about to jump into the restored image, and + * freeing swap here would desync the restored kernel's swap accounting. + */ +int swsusp_invalidate_sig(void) +{ + int error; + + error = hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header); + if (error) + return error; + /* + * swsusp_check() normally reset the signature already; that write may + * still be in the device's volatile cache, so rewrite the header with + * FUA either way and make the reset durable before the restore. + */ + if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) + memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); + else if (memcmp(swsusp_header->sig, swsusp_header->orig_sig, 10)) + return -ENODEV; + error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC | REQ_FUA, + swsusp_resume_block, swsusp_header); + /* A device that ignores FUA still honours an explicit cache flush. */ + if (!error) + error = blkdev_issue_flush(file_bdev(hib_resume_bdev_file)); + return error; +} + /** * swsusp_unmark - Unmark swsusp signature in the resume device * From 95ba8c3214821549c7b538f965285572a02f3d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:02 +0100 Subject: [PATCH 024/269] arm64: hibernate and spin-table CPUs at EL2 on M4 Pro (T6040) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With Linux at EL2 under aura, secondary CPUs come up from a spin table and the boot CPU has no enable-method. Let the spin-table CPUs be parked and released again across hibernation and suspend-to-RAM (a pen in the kernel image the locked RVBAR relay returns to), restore the EL2 state the hibernation resume needs, and keep the T6040 errata on the restored CPUs. An offlined RVBAR spin-table CPU waits in the kernel's own text, so spin_table_cpus_stuck() refuses kexec and hibernation for it. For a hibernation the pen is what takes those CPUs out of the memory the restore overwrites: a kernel that has it (KEXEC_CORE, for cpu_soft_restart()) is not stuck there, and swsusp_arch_suspend() asks spin_table_hibernate_cpus_stuck() instead. kexec keeps the check. Signed-off-by: František Balázsy --- arch/arm64/include/asm/cpu_ops.h | 19 ++ arch/arm64/kernel/Makefile | 3 + arch/arm64/kernel/cpu_errata.c | 34 +++- arch/arm64/kernel/hibernate-asm.S | 11 +- arch/arm64/kernel/hibernate.c | 149 +++++++++++++++- arch/arm64/kernel/smp.c | 23 ++- arch/arm64/kernel/smp_spin_table.c | 233 ++++++++++++++++++++++++- arch/arm64/kernel/smp_spin_table_pen.S | 34 ++++ 8 files changed, 480 insertions(+), 26 deletions(-) create mode 100644 arch/arm64/kernel/smp_spin_table_pen.S diff --git a/arch/arm64/include/asm/cpu_ops.h b/arch/arm64/include/asm/cpu_ops.h index 4ed50e0dc0b5c..6626229cbec35 100644 --- a/arch/arm64/include/asm/cpu_ops.h +++ b/arch/arm64/include/asm/cpu_ops.h @@ -66,6 +66,12 @@ struct spin_table_power_ops { #ifdef CONFIG_HOTPLUG_CPU int spin_table_set_power_ops(const struct spin_table_power_ops *ops); +#ifdef CONFIG_HIBERNATION +int spin_table_hibernate_pen_begin(phys_addr_t image_ttbr1, int primary); +void spin_table_hibernate_pen_end(bool frozen); +void spin_table_hibernate_restored(void); +bool spin_table_hibernate_cpus_stuck(void); +#endif int spin_table_hold_parked(void); void spin_table_unhold_parked(void); int spin_table_stop_parked(void); @@ -99,6 +105,19 @@ static inline bool spin_table_cpus_stuck(void) } #endif +#if !defined(CONFIG_HOTPLUG_CPU) || !defined(CONFIG_HIBERNATION) +static inline int spin_table_hibernate_pen_begin(phys_addr_t image_ttbr1, int primary) +{ + return 0; +} +static inline void spin_table_hibernate_pen_end(bool frozen) {} +static inline void spin_table_hibernate_restored(void) {} +static inline bool spin_table_hibernate_cpus_stuck(void) +{ + return false; +} +#endif + int __init init_cpu_ops(int cpu); extern const struct cpu_operations *get_cpu_ops(int cpu); diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index cac465111055b..263d682d48620 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -65,6 +65,9 @@ obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o obj-$(CONFIG_PARAVIRT) += paravirt.o obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o +ifdef CONFIG_HOTPLUG_CPU +obj-$(CONFIG_HIBERNATION) += smp_spin_table_pen.o +endif obj-$(CONFIG_ELF_CORE) += elfcore.o obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \ cpu-reset.o diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index fa1a6a83caa97..d5c3fe7533802 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -291,6 +292,29 @@ static const struct midr_range apple_cpus[] = { {}, }; +/* + * Apple cores that aren't in apple_cpus whose IMPDEF PMUv3 traps (HACR_EL2 + * bit 56) are turned on only when asked for on the command line + * (apple_pmuv3_traps), until they have been verified on these cores. + */ +static const struct midr_range apple_m4_cpus[] = { + MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), + MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), + {}, +}; + +static bool apple_pmuv3_traps_optin __ro_after_init; + +static int __init early_apple_pmuv3_traps_cfg(char *arg) +{ + /* A bare "apple_pmuv3_traps" enables the traps. */ + if (!arg || !*arg) + arg = "on"; + + return kstrtobool(arg, &apple_pmuv3_traps_optin); +} +early_param("apple_pmuv3_traps", early_apple_pmuv3_traps_cfg); + static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int scope) { u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); @@ -304,13 +328,13 @@ static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int sco if (pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF) return false; - return is_midr_in_range_list(apple_cpus); + if (is_midr_in_range_list(apple_cpus)) + return true; + + return apple_pmuv3_traps_optin && is_midr_in_range_list(apple_m4_cpus); } -/* - * Apple cores with the same vGIC as M1/M2 that aren't in apple_cpus yet: - * that list also turns on the IMPDEF PMUv3 traps, unverified here. - */ +/* Apple cores with the same vGIC as M1/M2 that aren't in apple_cpus yet. */ static const struct midr_range apple_broken_seis_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S index 2baefe7a82d36..f30269635a4fd 100644 --- a/arch/arm64/kernel/hibernate-asm.S +++ b/arch/arm64/kernel/hibernate-asm.S @@ -67,19 +67,24 @@ SYM_CODE_START(swsusp_arch_suspend_exit) copy_page x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 add x1, x10, #PAGE_SIZE - /* Clean the copied page to PoU - based on caches_clean_inval_pou() */ + /* + * Clean the copied page to PoC, not only PoU: on Apple SoCs the + * coprocessors (GPU UAT walker, DCP, ...) are not cache coherent and + * read the restored pages from DRAM. Clean+invalidate to PoC also + * covers what caches_clean_inval_pou() did for the I-side. + */ raw_dcache_line_size x2, x3 sub x3, x2, #1 bic x4, x10, x3 2: /* clean D line / unified line */ -alternative_insn "dc cvau, x4", "dc civac, x4", ARM64_WORKAROUND_CLEAN_CACHE + dc civac, x4 add x4, x4, x2 cmp x4, x1 b.lo 2b ldr x19, [x19, #HIBERN_PBE_NEXT] cbnz x19, 1b - dsb ish /* wait for PoU cleaning to finish */ + dsb ish /* wait for PoC cleaning to finish */ /* switch to the restored kernels page tables */ break_before_make_ttbr_switch x25, x21, x6, x8 diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 9398c8d0ecb03..048a29d5d54fd 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -12,9 +12,12 @@ #define pr_fmt(x) "hibernate: " x #include #include +#include +#include #include #include #include +#include #include #include @@ -81,6 +84,9 @@ static struct arch_hibernate_hdr { phys_addr_t __hyp_stub_vectors; u64 sleep_cpu_mpidr; + + /* What the boot loader decided for the boot that wrote the image. */ + u64 boot_layout; } resume_hdr; static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i) @@ -89,13 +95,127 @@ static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i) memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version)); } +static u64 hibernate_layout_mix(u64 h, const void *p, size_t len) +{ + const u8 *b = p; + + while (len--) { + h ^= *b++; + h *= 0x100000001b3ULL; /* FNV-1a */ + } + return h; +} + +/* + * The image restores every page to the frame it came from, so the boot that + * resumes it must give the kernel what the boot that wrote it did: the same + * RAM, and on Apple machines at EL2 the same CTXR exec window (secondaries only + * execute inside it) and aura's fixed hibernation layout. A boot loader decides + * them on every boot; the kernel build check above cannot see them. + * + * aura's own image moves with the boot object, and its CPU release addresses + * and S2R mailbox with it, inside the range the layout keeps from Linux. The + * restored kernel uses neither address of the boot that wrote the image: its + * CPUs come back from the boot kernel's pen, a CPU started at its reset vector + * again finds the release target the boot kernel wrote (both kernels run at the + * window's fixed address), and the T6040 sleep driver takes this boot's mailbox + * from aura's tree after the restore. What has to match is the spin table's + * shape (the same CPUs, at the same offsets from the mailbox) and that this + * boot keeps all of it out of the image. + */ +/* + * Mix one memory span less the parts in @ex, ranges that this boot keeps out of + * the image. iBoot's no-map allocations inside aura's fixed layout (the DCP + * oslogs, ANS firmware) move with the boot object and split memblock's regions + * differently on every boot; what lies there never holds an image page. + */ +static u64 hibernate_mix_span(u64 h, u64 b, u64 e, u32 flags, const u64 (*ex)[2], int nex) +{ + u64 size; + + if (b >= e) + return h; + if (nex && e > ex[0][0] && b < ex[0][1]) { + h = hibernate_mix_span(h, b, ex[0][0], flags, ex + 1, nex - 1); + return hibernate_mix_span(h, ex[0][1], e, flags, ex + 1, nex - 1); + } + if (nex) + return hibernate_mix_span(h, b, e, flags, ex + 1, nex - 1); + size = e - b; + h = hibernate_layout_mix(h, &b, sizeof(b)); + h = hibernate_layout_mix(h, &size, sizeof(size)); + return hibernate_layout_mix(h, &flags, sizeof(flags)); +} + +static u64 hibernate_boot_layout(void) +{ + static const char * const chosen_props[] = { + "aura,hibernate-layout", "aura,hibernate-dt", + "aurora,ctxr-exec-window", + }; + u64 h = 0xcbf29ce484222325ULL, mbox = 0, rel, hwid; + u64 layout[2], win[2], ex[2][2]; + struct memblock_region *r; + int nex = 0; + struct device_node *np; + bool have_mbox = false; + const void *v; + u8 kept; + int i, len; + + np = of_find_node_by_path("/chosen"); + for (i = 0; np && i < ARRAY_SIZE(chosen_props); i++) { + v = of_get_property(np, chosen_props[i], &len); + if (!v) + continue; + h = hibernate_layout_mix(h, chosen_props[i], strlen(chosen_props[i])); + h = hibernate_layout_mix(h, v, len); + } + if (np && !of_property_read_u64(np, "aurora,s2r-mailbox", &mbox)) { + have_mbox = true; + kept = aura_hibernate_pfn_nosave(PHYS_PFN(mbox)); + h = hibernate_layout_mix(h, "aurora,s2r-mailbox", strlen("aurora,s2r-mailbox")); + h = hibernate_layout_mix(h, &kept, sizeof(kept)); + } + /* aura's layout less the OS window, only where all of it is kept out of the image */ + if (np && !of_property_read_u64_array(np, "aura,hibernate-layout", layout, 2) && + !of_property_read_u64_array(np, "aurora,ctxr-exec-window", win, 2) && + layout[0] <= win[0] && win[0] + win[1] <= layout[1]) { + ex[nex][0] = layout[0]; + ex[nex][1] = win[0]; + if (aura_hibernate_range_nosave(ex[nex][0], ex[nex][1])) + nex++; + ex[nex][0] = win[0] + win[1]; + ex[nex][1] = layout[1]; + if (aura_hibernate_range_nosave(ex[nex][0], ex[nex][1])) + nex++; + } + of_node_put(np); + + for_each_of_cpu_node(np) { + if (of_property_read_u64(np, "cpu-release-addr", &rel)) + continue; + hwid = of_get_cpu_hwid(np, 0); + kept = aura_hibernate_pfn_nosave(PHYS_PFN(rel)); + if (have_mbox) + rel -= mbox; + h = hibernate_layout_mix(h, &hwid, sizeof(hwid)); + h = hibernate_layout_mix(h, &rel, sizeof(rel)); + h = hibernate_layout_mix(h, &kept, sizeof(kept)); + } + + for_each_mem_region(r) + h = hibernate_mix_span(h, r->base, r->base + r->size, r->flags, ex, nex); + return h; +} + int pfn_is_nosave(unsigned long pfn) { unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin); unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1); return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) || - crash_is_nosave(pfn); + crash_is_nosave(pfn) || aura_hibernate_pfn_nosave(pfn); } void notrace save_processor_state(void) @@ -129,8 +249,9 @@ int arch_hibernation_header_save(void *addr, unsigned int max_size) return -ENODEV; } hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu); - pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu, - hdr->sleep_cpu_mpidr); + hdr->boot_layout = hibernate_boot_layout(); + pr_info("Hibernating on CPU %d [mpidr:0x%llx], boot layout %016llx\n", sleep_cpu, + hdr->sleep_cpu_mpidr, hdr->boot_layout); return 0; } @@ -147,6 +268,12 @@ int arch_hibernation_header_restore(void *addr) pr_crit("Hibernate image not generated by this kernel!\n"); return -EINVAL; } + if (hdr->boot_layout != hibernate_boot_layout()) { + pr_crit("Hibernate image written under another boot layout (%016llx, this boot %016llx): not restored\n", + hdr->boot_layout, hibernate_boot_layout()); + return -EINVAL; + } + pr_info("Hibernate image written under this boot layout (%016llx)\n", hdr->boot_layout); sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr); pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu, @@ -337,7 +464,7 @@ int swsusp_arch_suspend(void) unsigned long flags; struct sleep_stack_data state; - if (cpus_are_stuck_in_kernel() || spin_table_cpus_stuck()) { + if (cpus_are_stuck_in_kernel() || spin_table_hibernate_cpus_stuck()) { pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n"); return -EBUSY; } @@ -386,6 +513,9 @@ int swsusp_arch_suspend(void) sleep_cpu = -EINVAL; __cpu_suspend_exit(); + /* The boot kernel's secondaries are in its pen, not where the image parked them. */ + spin_table_hibernate_restored(); + /* * Just in case the boot kernel did turn the SSBD * mitigation off behind our back, let's set the state @@ -489,10 +619,19 @@ int __nocfi swsusp_arch_resume(void) int hibernate_resume_nonboot_cpu_disable(void) { + int ret; + if (sleep_cpu < 0) { pr_err("Failing to resume from hibernate on an unknown CPU.\n"); return -ENODEV; } - return freeze_secondary_cpus(sleep_cpu); + /* Parking spin-table CPUs wait outside the image while it is restored. */ + ret = spin_table_hibernate_pen_begin(resume_hdr.ttbr1_el1, sleep_cpu); + if (ret) + return ret; + ret = freeze_secondary_cpus(sleep_cpu); + spin_table_hibernate_pen_end(!ret); + + return ret; } diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index f6b5decff53da..02badcc245d05 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -1354,14 +1354,29 @@ bool smp_crash_stop_failed(void) } #endif +/* + * The CPUs that have to leave the kernel are the others. A boot CPU whose node + * carries no enable-method (Apple boot objects describe only the secondaries' + * spin table) has no operations of its own: then every other possible CPU must + * have a way out. + */ static bool have_cpu_die(void) { #ifdef CONFIG_HOTPLUG_CPU - int any_cpu = raw_smp_processor_id(); - const struct cpu_operations *ops = get_cpu_ops(any_cpu); + int this_cpu = raw_smp_processor_id(), cpu; + const struct cpu_operations *ops = get_cpu_ops(this_cpu); - if (ops && ops->cpu_die) - return true; + if (ops) + return ops->cpu_die != NULL; + + for_each_possible_cpu(cpu) { + if (cpu == this_cpu) + continue; + ops = get_cpu_ops(cpu); + if (!ops || !ops->cpu_die) + return false; + } + return num_possible_cpus() > 1; #endif return false; } diff --git a/arch/arm64/kernel/smp_spin_table.c b/arch/arm64/kernel/smp_spin_table.c index 2d7660bd22f80..2326f3f5df57a 100644 --- a/arch/arm64/kernel/smp_spin_table.c +++ b/arch/arm64/kernel/smp_spin_table.c @@ -5,10 +5,12 @@ * Copyright (C) 2013 ARM Ltd. */ +#include #include #include #include #include +#include #include #include @@ -163,6 +165,198 @@ static bool spin_table_stop_released; #define SPIN_TABLE_WAIT_US 100000 +#if defined(CONFIG_HIBERNATION) && defined(CONFIG_KEXEC_CORE) +/* + * Resuming from hibernation, the boot kernel takes its secondaries offline and + * then overwrites every page the image saved, the park and its cpumasks + * included. A CPU parked there would run on in memory that changes under it, + * and its translation tables with it. On that path a CPU leaving the kernel + * waits instead in a pen the image doesn't contain: the code and a slot per + * CPU in .data..nosave, run with the MMU off. arm64 resumes only an image of + * the kernel build that is running; with the boot kernel loaded at the image + * kernel's physical address (checked when the resume starts) the pen is at the + * same place in both, and the restored kernel reads what the boot kernel left + * there. It + * starts each CPU from the pen through secondary_holding_pen, as at boot. + * Nothing is powered off, so this needs no platform power ops. + */ +#define SPIN_TABLE_PEN_MAGIC 0x314e455034535053ULL /* "SPS4PEN1" */ +#define SPIN_TABLE_PEN_HERE 0x45524548ULL /* "HERE", from the pen */ + +struct spin_table_pen_slot { + u64 release; /* where the CPU goes; 0 holds it */ + u64 here; /* the CPU reached the pen */ + u64 mpidr; +} __aligned(ARCH_DMA_MINALIGN); + +static struct { + u64 magic; + u64 self; /* this record's address in the kernel that wrote it */ + unsigned long penned[BITS_TO_LONGS(NR_CPUS)]; + struct spin_table_pen_slot slot[NR_CPUS]; +} spin_table_pen __nosavedata __aligned(ARCH_DMA_MINALIGN); + +extern char spin_table_hibernate_pen[]; +extern const struct cpu_operations smp_rvbar_spin_table_ops; +static struct cpumask spin_table_cpus_penned; +static bool spin_table_pen_requested; + +static void spin_table_pen_clean(void *start, size_t size) +{ + dcache_clean_inval_poc((unsigned long)start, (unsigned long)start + size); +} + +/* + * Resuming from hibernation, before the boot kernel takes its secondaries offline. + * The restored kernel finds the pen only where it is in its own image, which holds + * when the boot kernel is loaded at the image kernel's physical address. arm64 can + * restore from a boot kernel placed elsewhere (@image_ttbr1, the image's + * swapper_pg_dir, is used for the jump), but there the pen's frames are image + * frames: the restore would overwrite them while the penned CPUs run from them with + * the MMU off. Refuse the resume then, as long as a CPU other than @primary (the + * one that stays online) would need the pen. + */ +int spin_table_hibernate_pen_begin(phys_addr_t image_ttbr1, int primary) +{ + int cpu; + + /* No record of an earlier, abandoned resume may reach the restored kernel. */ + spin_table_pen.magic = 0; + spin_table_pen_clean(&spin_table_pen, sizeof(spin_table_pen.magic)); + + if (image_ttbr1 != __pa_symbol(swapper_pg_dir)) { + for_each_online_cpu(cpu) { + if (cpu == primary || get_cpu_ops(cpu) != &smp_rvbar_spin_table_ops) + continue; + pr_err("hibernation: the image's kernel ran at another physical address (swapper_pg_dir %pa, here %pa): spin-table CPUs cannot wait in this kernel's pen, resume refused\n", + &image_ttbr1, &(phys_addr_t){ __pa_symbol(swapper_pg_dir) }); + return -EINVAL; + } + return 0; + } + WRITE_ONCE(spin_table_pen_requested, true); + return 0; +} + +/* + * After they went offline. With all of them frozen, the restored kernel finds the + * record; otherwise the resume is abandoned and they go back online from the pen. + */ +void spin_table_hibernate_pen_end(bool frozen) +{ + unsigned int cpu; + + WRITE_ONCE(spin_table_pen_requested, false); + if (!frozen || cpumask_empty(&spin_table_cpus_penned)) + return; + + bitmap_zero(spin_table_pen.penned, NR_CPUS); + for_each_cpu(cpu, &spin_table_cpus_penned) + __set_bit(cpu, spin_table_pen.penned); + spin_table_pen.self = __pa_symbol(&spin_table_pen); + spin_table_pen.magic = SPIN_TABLE_PEN_MAGIC; + spin_table_pen_clean(&spin_table_pen, offsetof(typeof(spin_table_pen), slot)); + pr_info("hibernation: %u CPUs wait outside the image\n", + cpumask_weight(&spin_table_cpus_penned)); +} + +/* + * In the restored kernel, right after the image took over: the CPUs the image parked + * before its snapshot are in the boot kernel's pen now. + */ +void spin_table_hibernate_restored(void) +{ + unsigned int cpu, found = 0; + + if (spin_table_pen.magic != SPIN_TABLE_PEN_MAGIC) + return; + spin_table_pen.magic = 0; + spin_table_pen_clean(&spin_table_pen, sizeof(spin_table_pen.magic)); + if (spin_table_pen.self != __pa_symbol(&spin_table_pen)) { + pr_err("hibernation: the boot kernel's CPU pen was at %#llx, not here: its CPUs stay offline\n", + spin_table_pen.self); + return; + } + + cpumask_clear(&spin_table_cpus_penned); + for_each_present_cpu(cpu) { + if (!test_bit(cpu, spin_table_pen.penned)) + continue; + if (spin_table_pen.slot[cpu].mpidr != cpu_logical_map(cpu)) { + pr_err("hibernation: CPU%u: the pen holds MPIDR %#llx, not %#llx\n", cpu, + spin_table_pen.slot[cpu].mpidr, cpu_logical_map(cpu)); + continue; + } + cpumask_clear_cpu(cpu, &spin_table_cpus_parked); + cpumask_clear_cpu(cpu, &spin_table_cpus_held); + cpumask_clear_cpu(cpu, &spin_table_cpus_unpark); + cpumask_clear_cpu(cpu, &spin_table_cpus_off); + cpumask_set_cpu(cpu, &spin_table_cpus_penned); + found++; + } + pr_info("hibernation: %u CPUs start from the boot kernel's pen\n", found); +} + +/* Leaves the kernel for the pen; the boot CPU waits for it to arrive (cpu_kill). */ +static void __noreturn smp_rvbar_spin_table_pen(unsigned int cpu, u64 mpidr) +{ + struct spin_table_pen_slot *slot = &spin_table_pen.slot[cpu]; + typeof(cpu_soft_restart) *restart; + + slot->release = 0; + slot->here = 0; + slot->mpidr = mpidr; + spin_table_pen_clean(slot, sizeof(*slot)); + cpumask_set_cpu(cpu, &spin_table_cpus_penned); + + cpu_install_idmap(); + restart = (void *)__pa_symbol(cpu_soft_restart); + restart(0, __pa_symbol(spin_table_hibernate_pen), __pa_symbol(slot), 0, 0); + unreachable(); +} + +static bool spin_table_pen_arrived(unsigned int cpu) +{ + struct spin_table_pen_slot *slot = &spin_table_pen.slot[cpu]; + + /* Written with the MMU off: drop any copy of the line before reading. */ + dcache_inval_poc((unsigned long)slot, (unsigned long)(slot + 1)); + return READ_ONCE(slot->here) == SPIN_TABLE_PEN_HERE; +} + +/* Starts a CPU waiting in the pen; it goes on to secondary_holding_pen. */ +static void spin_table_pen_release(unsigned int cpu) +{ + struct spin_table_pen_slot *slot = &spin_table_pen.slot[cpu]; + + WRITE_ONCE(slot->release, __pa_symbol(secondary_holding_pen)); + spin_table_pen_clean(slot, sizeof(*slot)); +} +#else +static const bool spin_table_pen_requested; +static struct cpumask spin_table_cpus_penned; + +static void smp_rvbar_spin_table_pen(unsigned int cpu, u64 mpidr) +{ +} + +static bool spin_table_pen_arrived(unsigned int cpu) +{ + return false; +} + +static void spin_table_pen_release(unsigned int cpu) +{ +} + +#ifdef CONFIG_HIBERNATION +/* Without cpu_soft_restart() a CPU cannot leave the kernel: nothing to keep. */ +int spin_table_hibernate_pen_begin(phys_addr_t image_ttbr1, int primary) { return 0; } +void spin_table_hibernate_pen_end(bool frozen) {} +void spin_table_hibernate_restored(void) {} +#endif +#endif + int spin_table_set_power_ops(const struct spin_table_power_ops *ops) { return cmpxchg(&spin_table_power, NULL, ops) ? -EBUSY : 0; @@ -208,6 +402,9 @@ int spin_table_hold_parked(void) { int us; + if (!READ_ONCE(spin_table_power)) + return -EOPNOTSUPP; + WRITE_ONCE(spin_table_hold_requested, true); dsb(ish); sev(); @@ -278,7 +475,9 @@ static int smp_rvbar_spin_table_cpu_boot(unsigned int cpu) /* The CPU may run straight into the pen: release it before it starts. */ write_pen_release(mpidr); - if (cpumask_test_and_clear_cpu(cpu, &spin_table_cpus_off)) { + if (cpumask_test_and_clear_cpu(cpu, &spin_table_cpus_penned)) { + spin_table_pen_release(cpu); + } else if (cpumask_test_and_clear_cpu(cpu, &spin_table_cpus_off)) { ret = spin_table_power->cpu_on(mpidr); if (ret) { cpumask_set_cpu(cpu, &spin_table_cpus_off); @@ -301,10 +500,11 @@ static bool smp_rvbar_spin_table_cpu_can_disable(unsigned int cpu) static int smp_rvbar_spin_table_cpu_disable(unsigned int cpu) { - if (!IS_ENABLED(CONFIG_KEXEC_CORE)) - return -EOPNOTSUPP; - - return READ_ONCE(spin_table_power) ? 0 : -EOPNOTSUPP; + /* + * A parked CPU comes back through cpu_soft_restart(). Only stopping it for a + * suspend needs the platform's power ops (spin_table_hold_parked()). + */ + return IS_ENABLED(CONFIG_KEXEC_CORE) ? 0 : -EOPNOTSUPP; } #ifdef CONFIG_KEXEC_CORE @@ -350,11 +550,14 @@ static void smp_rvbar_spin_table_cpu_die(unsigned int cpu) u64 mpidr = cpu_logical_map(cpu); /* The early-boot and crash paths get here without hotplug support. */ - if (!READ_ONCE(spin_table_power) || !IS_ENABLED(CONFIG_KEXEC_CORE)) { + if (!IS_ENABLED(CONFIG_KEXEC_CORE)) { while (1) wfe(); } + if (READ_ONCE(spin_table_pen_requested)) + smp_rvbar_spin_table_pen(cpu, mpidr); + cpumask_set_cpu(cpu, &spin_table_cpus_parked); while (!cpumask_test_cpu(cpu, &spin_table_cpus_unpark)) { @@ -373,12 +576,11 @@ static int smp_rvbar_spin_table_cpu_kill(unsigned int cpu) { int us; - if (!READ_ONCE(spin_table_power)) - return 0; - for (us = 0; us < SPIN_TABLE_WAIT_US; us += 10) { if (cpumask_test_cpu(cpu, &spin_table_cpus_parked)) return 0; + if (cpumask_test_cpu(cpu, &spin_table_cpus_penned) && spin_table_pen_arrived(cpu)) + return 0; udelay(10); } @@ -409,4 +611,17 @@ bool spin_table_cpus_stuck(void) return true; return false; } + +#ifdef CONFIG_HIBERNATION +/* + * For a hibernation that only holds without the pen. With it the kernel that + * resumes an image takes its offlined CPUs out of the memory the restore + * overwrites before it starts (spin_table_hibernate_pen_begin()), and the + * restored kernel starts them from there. + */ +bool spin_table_hibernate_cpus_stuck(void) +{ + return !IS_ENABLED(CONFIG_KEXEC_CORE) && spin_table_cpus_stuck(); +} +#endif #endif /* CONFIG_HOTPLUG_CPU */ diff --git a/arch/arm64/kernel/smp_spin_table_pen.S b/arch/arm64/kernel/smp_spin_table_pen.S new file mode 100644 index 0000000000000..4610bc09ad002 --- /dev/null +++ b/arch/arm64/kernel/smp_spin_table_pen.S @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Where a parking spin-table CPU waits while the kernel resuming from + * hibernation overwrites memory with the image (smp_spin_table.c). + */ + +#include +#include + +/* + * x0: the CPU's struct spin_table_pen_slot: the entry address at offset 0, + * written by the kernel that starts the CPU again, and the arrival word at 8, + * written here. + * + * Entered from cpu_soft_restart() with the MMU and caches off and interrupts + * masked, so nothing here depends on the kernel's page tables or on cache + * maintenance by this CPU. The code and the slots are in .data..nosave: the + * image never contains them, so the restore leaves both as the boot kernel + * wrote them, at the same physical address in the restored kernel. + */ + .pushsection ".data..nosave", "aw" + .balign 64 +SYM_CODE_START(spin_table_hibernate_pen) + movz x1, #0x4548 + movk x1, #0x4552, lsl #16 // "HERE" + str x1, [x0, #8] + dsb sy +1: ldr x1, [x0] + cbnz x1, 2f + wfe + b 1b +2: br x1 +SYM_CODE_END(spin_table_hibernate_pen) + .popsection From 11e47b642eaa2d34c8058a2aa08e284639b39f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:02 +0100 Subject: [PATCH 025/269] soc: apple: hibernation under aura's fixed layout and RTKit coprocessor handoff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aura-hibernate reads the fixed boot layout aura publishes (hib=1) and refuses hibernation without it. RTKit gains a cold-restart/re-adopt path for a coprocessor the restored kernel finds running (or halted) under it, used by the DCP, AOP, SEP, ANE and SMC drivers after S4 and S2R. Signed-off-by: František Balázsy --- drivers/soc/apple/aura-hibernate.c | 461 +++++++++++++++++++++++ drivers/soc/apple/rtkit-helper.c | 243 +++++++++++- drivers/soc/apple/rtkit-hib-handoff.c | 211 +++++++++++ drivers/soc/apple/rtkit.c | 57 +++ include/linux/soc/apple/aura-hibernate.h | 38 ++ include/linux/soc/apple/rtkit-helper.h | 16 + include/linux/soc/apple/rtkit.h | 56 +++ rust/bindings/bindings_helper.h | 1 + rust/kernel/soc/apple/rtkit.rs | 9 + 9 files changed, 1089 insertions(+), 3 deletions(-) create mode 100644 drivers/soc/apple/aura-hibernate.c create mode 100644 drivers/soc/apple/rtkit-hib-handoff.c create mode 100644 include/linux/soc/apple/aura-hibernate.h diff --git a/drivers/soc/apple/aura-hibernate.c b/drivers/soc/apple/aura-hibernate.c new file mode 100644 index 0000000000000..59cfe189a4313 --- /dev/null +++ b/drivers/soc/apple/aura-hibernate.c @@ -0,0 +1,461 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Hibernation on Apple Silicon under an aura boot object. + * + * A hibernation image restores every saved page to the frame it was taken from, and swsusp saves + * reserved pages as long as they are mapped. Under aura the memory below Linux's own allocations + * holds what the boot loader keeps alive across the hand-off: aura itself (resident at EL2), its + * heap with the firmware buffers it handed over, the device tree, the initramfs, and the ranges + * the boot firmware placed for the co-processors. Restoring old contents there would overwrite + * the running boot loader and the live firmware state of the resuming boot. + * + * aura marks a boot whose layout is the same on every boot with /chosen/aura,hibernate-layout + * (start, end of the range it keeps from Linux). On such a boot every /memreserve/ range and every + * non-reusable /reserved-memory range is registered as nosave, except the kernel image itself. + * Without the property, hibernation and resume are refused, since the reserved ranges move. + * + * The boot firmware still places some firmware segments anew on every boot (the AOP __OS_LOG), + * inside the range aura keeps from Linux. A kernel restored from an image knows the device tree of the boot + * it was started on, not of the boot that resumed it. aura therefore hands Linux its device tree + * at a fixed address inside that range (/chosen/aura,hibernate-dt): the image does not save it, + * so after a resume the kernel finds this boot's tree at the address it was started with, and + * aura_hibernate_boot_fw_regions() reads a device's firmware regions from it. + * + * aura itself moves with the boot object, and with it its S2R mailbox: a restored kernel reads the + * mailbox of the boot it runs on from that tree too (aura_hibernate_boot_chosen_u64()). + */ + +#define pr_fmt(fmt) "aura-hibernate: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool aura_layout_fixed; +static u64 aura_layout[2]; +/* Where aura puts the device tree on every boot; 0 when it does not. */ +static phys_addr_t aura_boot_dt; +#define AURA_BOOT_DT_MAX SZ_2M + +/* + * Ranges kept out of the image, in page frames. Filled once at late_initcall, + * read by arm64's pfn_is_nosave() for reserved pages. A static table avoids + * register_nosave_region(), which allocates from memblock and is meant for + * setup_arch() time. + */ +#define AURA_NOSAVE_MAX 128 +static struct { unsigned long start, end; } aura_nosave[AURA_NOSAVE_MAX]; +static unsigned int aura_nosave_count; + +bool aura_hibernate_layout_fixed(void) +{ + return aura_layout_fixed; +} + +bool aura_hibernate_pfn_nosave(unsigned long pfn) +{ + unsigned int i; + + for (i = 0; i < aura_nosave_count; i++) + if (pfn >= aura_nosave[i].start && pfn < aura_nosave[i].end) + return true; + return false; +} + +/* Whether the ranges kept out of the image cover all of [start, end). */ +bool aura_hibernate_range_nosave(u64 start, u64 end) +{ + unsigned long pfn = PFN_DOWN(start), last = PFN_UP(end); + unsigned int i; + bool moved; + + while (pfn < last) { + moved = false; + for (i = 0; i < aura_nosave_count; i++) { + if (pfn >= aura_nosave[i].start && pfn < aura_nosave[i].end) { + pfn = aura_nosave[i].end; + moved = true; + } + } + if (!moved) + return false; + } + return true; +} + +static void __init aura_nosave_add(unsigned long s, unsigned long e) +{ + if (s >= e) + return; + if (aura_nosave_count == AURA_NOSAVE_MAX) { + pr_warn("more than %d reserved ranges; hibernation stays disabled\n", AURA_NOSAVE_MAX); + aura_nosave_count++; + return; + } + if (aura_nosave_count < AURA_NOSAVE_MAX) { + aura_nosave[aura_nosave_count].start = s; + aura_nosave[aura_nosave_count].end = e; + aura_nosave_count++; + } +} + +static void __init aura_nosave_range(u64 start, u64 end, unsigned long kstart, unsigned long kend, + const char *what) +{ + unsigned long s = PFN_DOWN(start), e = PFN_UP(end); + + if (s >= e) + return; + /* The kernel image is Linux's own and is saved; split the range around it. */ + if (s < kend && e > kstart) { + if (s < kstart) + aura_nosave_add(s, kstart); + if (e > kend) + aura_nosave_add(kend, e); + } else { + aura_nosave_add(s, e); + } + pr_debug("nosave %s [%#llx, %#llx)\n", what, start, end); +} + +static bool aura_fw_region_kept(u64 start, u64 size) +{ + u64 end = start + size, a; + + if (end < start) + return false; + /* + * Inside the range aura keeps from Linux below its device tree slot, where the boot firmware places the + * firmware: reserved, and not saved in the image. Not the slot or aura's heap above it. + */ + if (start >= aura_layout[0] && end <= aura_boot_dt) + return true; + /* Otherwise Linux must neither own nor map any of it (no-map or not RAM). */ + for (a = start & PAGE_MASK; a < end; a += PAGE_SIZE) + if (memblock_is_map_memory(a)) + return false; + return true; +} + +/* + * Whether @fdt, mapped from @slot, is a tree aura wrote there: a valid header that fits the slot, and + * a /chosen naming this slot and the fixed layout @layout. + */ +static bool aura_slot_tree_valid(const void *fdt, phys_addr_t slot, const u64 layout[2]) +{ + const fdt64_t *v; + int node, len; + + if (fdt_check_header(fdt) || fdt_totalsize(fdt) > AURA_BOOT_DT_MAX) + return false; + node = fdt_path_offset(fdt, "/chosen"); + if (node < 0) + return false; + v = fdt_getprop(fdt, node, "aura,hibernate-dt", &len); + if (!v || len != sizeof(*v) || fdt64_ld(v) != slot) + return false; + v = fdt_getprop(fdt, node, "aura,hibernate-layout", &len); + return v && len == 2 * sizeof(*v) && fdt64_ld(&v[0]) == layout[0] && + fdt64_ld(&v[1]) == layout[1]; +} + +/* + * Reads the firmware memory-regions of @np (its memory-region phandles, each + * region's reg and the iommu-addresses entry naming @np) from the device tree + * of the running boot, which after a resume from disk is not the one the + * kernel was started with. Returns the number of regions, at most @max, or a + * negative errno: -ENODEV when aura does not hand the tree over at a fixed + * address. Only flat, untranslated layouts are accepted: @np's parent without + * dma-ranges, a /reserved-memory without a non-empty ranges. + */ +int aura_hibernate_boot_fw_regions(struct device_node *np, struct aura_fw_region *out, + unsigned int max) +{ + const fdt32_t *phandles, *p; + unsigned int n = 0; + int node, parent, len, ret, i; + u32 own; + char *path; + void *fdt; + + if (!aura_boot_dt || !np) + return -ENODEV; + fdt = memremap(aura_boot_dt, AURA_BOOT_DT_MAX, MEMREMAP_WB); + if (!fdt) + return -ENOMEM; + ret = -EINVAL; + /* The tree aura handed over on this boot, with the same fixed layout. */ + if (!aura_slot_tree_valid(fdt, aura_boot_dt, aura_layout)) + goto out; + + path = kasprintf(GFP_KERNEL, "%pOF", np); + if (!path) { + ret = -ENOMEM; + goto out; + } + node = fdt_path_offset(fdt, path); + kfree(path); + ret = -ENOENT; + if (node < 0) + goto out; + ret = -EINVAL; + own = fdt_get_phandle(fdt, node); + parent = fdt_parent_offset(fdt, node); + if (!own || parent < 0 || fdt_getprop(fdt, parent, "dma-ranges", NULL)) + goto out; + phandles = fdt_getprop(fdt, node, "memory-region", &len); + if (!phandles || len <= 0 || len % sizeof(*phandles)) + goto out; + + for (i = 0; i < len / (int)sizeof(*phandles); i++) { + int region, rparent, ac, sc, cells, pos, plen; + const fdt32_t *reg, *iommu; + u64 phys, size, iova = 0, length = 0; + bool found = false; + + region = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(phandles[i])); + if (region < 0) + goto out; + rparent = fdt_parent_offset(fdt, region); + if (rparent < 0) + goto out; + p = fdt_getprop(fdt, rparent, "ranges", &plen); + if (p && plen) + goto out; + ac = fdt_address_cells(fdt, rparent); + sc = fdt_size_cells(fdt, rparent); + reg = fdt_getprop(fdt, region, "reg", &plen); + if (ac < 1 || ac > 2 || sc < 1 || sc > 2 || !reg || + plen < (int)((ac + sc) * sizeof(*reg))) + goto out; + phys = fdt32_to_cpu(reg[0]); + if (ac == 2) + phys = phys << 32 | fdt32_to_cpu(reg[1]); + size = fdt32_to_cpu(reg[ac]); + if (sc == 2) + size = size << 32 | fdt32_to_cpu(reg[ac + 1]); + + /* Entries: owner phandle, then an address and a size in the owner's bus cells. */ + iommu = fdt_getprop(fdt, region, "iommu-addresses", &plen); + if (!iommu || plen <= 0 || plen % sizeof(*iommu)) + goto out; + cells = plen / sizeof(*iommu); + for (pos = 0; pos < cells;) { + u32 ph = fdt32_to_cpu(iommu[pos++]); + int owner = fdt_node_offset_by_phandle(fdt, ph), op, oac, osc; + + if (owner < 0) + goto out; + op = fdt_parent_offset(fdt, owner); + if (op < 0) + goto out; + oac = fdt_address_cells(fdt, op); + osc = fdt_size_cells(fdt, op); + if (oac < 1 || oac > 2 || osc < 1 || osc > 2 || oac + osc > cells - pos) + goto out; + if (ph == own) { + if (found) + goto out; + found = true; + iova = fdt32_to_cpu(iommu[pos]); + if (oac == 2) + iova = iova << 32 | fdt32_to_cpu(iommu[pos + 1]); + length = fdt32_to_cpu(iommu[pos + oac]); + if (osc == 2) + length = length << 32 | fdt32_to_cpu(iommu[pos + oac + 1]); + } + pos += oac + osc; + } + if (!found || length != size || !size) + goto out; + if (!aura_fw_region_kept(phys, size)) { + pr_warn("%pOF: this boot's firmware region [%#llx, +%#llx) is memory Linux owns\n", + np, phys, size); + ret = -EBUSY; + goto out; + } + if (n == max) { + ret = -E2BIG; + goto out; + } + out[n].phys = phys; + out[n].iova = iova; + out[n].size = size; + n++; + } + ret = n; +out: + memunmap(fdt); + return ret; +} +EXPORT_SYMBOL_GPL(aura_hibernate_boot_fw_regions); + +/* + * Reads the u64 property @name of /chosen from the device tree of the running boot, which after a + * resume from disk is not the one the kernel was started with. Returns 0, -ENODEV when aura does + * not hand the tree over at a fixed address, -EINVAL when the slot holds no tree for this layout, + * -ENOENT when the tree has no such property. + */ +int aura_hibernate_boot_chosen_u64(const char *name, u64 *val) +{ + const fdt64_t *v; + int node, len, ret; + void *fdt; + + if (!aura_boot_dt) + return -ENODEV; + fdt = memremap(aura_boot_dt, AURA_BOOT_DT_MAX, MEMREMAP_WB); + if (!fdt) + return -ENOMEM; + ret = -EINVAL; + if (!aura_slot_tree_valid(fdt, aura_boot_dt, aura_layout)) + goto out; + ret = -ENOENT; + node = fdt_path_offset(fdt, "/chosen"); + v = node < 0 ? NULL : fdt_getprop(fdt, node, name, &len); + if (!v || len != sizeof(*v)) + goto out; + *val = fdt64_ld(v); + ret = 0; +out: + memunmap(fdt); + return ret; +} +EXPORT_SYMBOL_GPL(aura_hibernate_boot_chosen_u64); + +struct aura_hibernate_reprobe { + struct work_struct work; + struct device *dev; +}; + +static void aura_hibernate_reprobe_work(struct work_struct *work) +{ + struct aura_hibernate_reprobe *rp = container_of(work, struct aura_hibernate_reprobe, work); + int ret; + + ret = device_reprobe(rp->dev); + if (ret) + dev_err(rp->dev, "probe after the resume from disk failed: %d\n", ret); + put_device(rp->dev); + kfree(rp); +} + +/* + * Unbinds @dev and probes it again once the tasks are thawed, for a driver whose co-processor + * the boot that resumed the image left halted: its unbind and its probe know how to drop a + * session that died under it and start the firmware anew, as at boot, and a restore callback + * cannot run either. The work runs on the freezable workqueue, which the restore finds frozen, + * so it starts after every device's restore and its complete step, when probing is allowed + * again. Built in, so that a driver module going away meanwhile leaves nothing behind. + */ +int aura_hibernate_reprobe_after_thaw(struct device *dev) +{ + struct aura_hibernate_reprobe *rp; + + rp = kzalloc_obj(*rp); + if (!rp) + return -ENOMEM; + INIT_WORK(&rp->work, aura_hibernate_reprobe_work); + rp->dev = get_device(dev); + queue_work(system_freezable_wq, &rp->work); + return 0; +} +EXPORT_SYMBOL_GPL(aura_hibernate_reprobe_after_thaw); + +static int __init aura_hibernate_init(void) +{ + unsigned long kstart = PFN_DOWN(__pa_symbol(_text)), kend = PFN_UP(__pa_symbol(_end)); + struct device_node *chosen, *rmem, *child; + unsigned int count = 0; + const void *fdt = initial_boot_params; + u64 layout[2], dt_slot; + int i, n; + + /* Only a boot loader that keeps the layout fixed publishes it. */ + chosen = of_find_node_by_path("/chosen"); + if (!chosen || + of_property_read_u64_array(chosen, "aura,hibernate-layout", layout, 2)) { + of_node_put(chosen); + return 0; + } + /* + * The device tree slot. Trusted where this kernel's tree is, or, when a later stage (U-Boot and + * the EFI stub) handed the kernel its own copy of aura's tree, where the slot still holds aura's + * tree for this layout: those stages do not move the firmware segments, so aura's tree and the + * kernel's copy agree on them. + */ + if (!of_property_read_u64(chosen, "aura,hibernate-dt", &dt_slot)) { + bool in_layout = dt_slot >= layout[0] && dt_slot + AURA_BOOT_DT_MAX <= layout[1]; + void *slot_fdt; + + if (in_layout && dt_slot == __fdt_pointer) { + aura_boot_dt = dt_slot; + } else if (in_layout) { + slot_fdt = memremap(dt_slot, AURA_BOOT_DT_MAX, MEMREMAP_WB); + if (slot_fdt && aura_slot_tree_valid(slot_fdt, dt_slot, layout)) { + aura_boot_dt = dt_slot; + pr_info("device tree handed over at %pa; using aura's tree at %#llx\n", + &__fdt_pointer, dt_slot); + } else { + pr_warn("device tree at %pa, aura's slot %#llx holds no tree for this layout\n", + &__fdt_pointer, dt_slot); + } + if (slot_fdt) + memunmap(slot_fdt); + } else { + pr_warn("device tree slot %#llx outside the fixed layout\n", dt_slot); + } + } + of_node_put(chosen); + + if (fdt) { + n = fdt_num_mem_rsv(fdt); + for (i = 0; i < n; i++) { + u64 addr, size; + + if (fdt_get_mem_rsv(fdt, i, &addr, &size) || !size) + continue; + aura_nosave_range(addr, addr + size, kstart, kend, "memreserve"); + count++; + } + } + + rmem = of_find_node_by_path("/reserved-memory"); + for_each_available_child_of_node(rmem, child) { + struct resource r; + + if (of_property_read_bool(child, "reusable")) + continue; + if (of_address_to_resource(child, 0, &r)) + continue; + aura_nosave_range(r.start, r.end + 1, kstart, kend, "reserved-memory"); + count++; + } + of_node_put(rmem); + + if (aura_nosave_count > AURA_NOSAVE_MAX) { + aura_nosave_count = 0; + return 0; + } + aura_layout[0] = layout[0]; + aura_layout[1] = layout[1]; + aura_layout_fixed = true; + pr_info("fixed boot layout [%#llx, %#llx), %u reserved ranges kept out of the image%s\n", + layout[0], layout[1], count, + aura_boot_dt ? ", device tree at a fixed address" : ""); + return 0; +} +late_initcall(aura_hibernate_init); diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index 8c518ef4349e9..93aa9bb6af850 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -48,6 +48,12 @@ struct apple_rtkit_helper { bool idle_pending; /* Ends a sleep whose .prepare failed, which gets no .complete. */ struct notifier_block pm_nb; + /* Boot kernel of a resume from disk: RX held for the image. */ + bool rx_held; + /* This restore took over the session the boot kernel started. */ + bool readopted; + /* This restore found the co-processor of our own session halted. */ + bool halted_underneath; void __iomem *asc_base; @@ -63,6 +69,22 @@ bool apple_rtkit_helper_preserves_input(struct device *dev) } EXPORT_SYMBOL_GPL(apple_rtkit_helper_preserves_input); +bool apple_rtkit_helper_readopted(struct device *dev) +{ + struct apple_rtkit_helper *helper = dev_get_drvdata(dev); + + return helper && helper->readopted; +} +EXPORT_SYMBOL_GPL(apple_rtkit_helper_readopted); + +bool apple_rtkit_helper_halted_underneath(struct device *dev) +{ + struct apple_rtkit_helper *helper = dev_get_drvdata(dev); + + return helper && helper->halted_underneath; +} +EXPORT_SYMBOL_GPL(apple_rtkit_helper_halted_underneath); + static int apple_rtkit_helper_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) { struct apple_rtkit_helper *helper = cookie; @@ -119,9 +141,12 @@ static const struct apple_rtkit_ops apple_rtkit_helper_ops = { .shmem_destroy = apple_rtkit_helper_shmem_destroy, }; -static void apple_rtkit_helper_release(void *rtk) +static void apple_rtkit_helper_release(void *data) { - apple_rtkit_free_retaining_buffers(rtk); + struct apple_rtkit_helper *helper = data; + + /* The current session object: a hibernation restore may have replaced it. */ + apple_rtkit_free_retaining_buffers(helper->rtk); } /* @@ -347,7 +372,7 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev) "Failed to adopt RTKit\n"); ret = devm_add_action_or_reset(dev, apple_rtkit_helper_release, - helper->rtk); + helper); if (ret) return ret; ret = apple_rtkit_helper_pm_register(helper); @@ -388,6 +413,18 @@ static void apple_rtkit_helper_remove(struct platform_device *pdev) if (helper->adopted || helper->preserved) return; + /* + * After a resume from disk the boot loader left the coprocessor halted + * with fresh firmware loaded, while the restored host session still + * believes it runs: nothing would answer the quiesce. Drop the session + * without a handshake so that the next probe starts the CPU again. + */ + if (!(readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & + APPLE_ASC_CPU_CONTROL_RUN)) { + dev_info(&pdev->dev, "co-processor halted underneath (resume from disk): removed without quiesce\n"); + return; + } + if (apple_rtkit_is_running(helper->rtk)) apple_rtkit_quiesce(helper->rtk); @@ -554,6 +591,201 @@ static int __maybe_unused apple_rtkit_helper_resume(struct device *dev) return 0; } +#ifdef CONFIG_HIBERNATION +/* + * What the boot kernel of a resume from disk leaves for the restored kernel + * about a session it started itself: the shared buffers the firmware holds. + */ +struct apple_rtkit_helper_hib { + u32 count; + u32 reserved; + struct apple_rtkit_buffer_desc buf[APPLE_RTKIT_BUF_COUNT]; +}; +static_assert(sizeof(struct apple_rtkit_helper_hib) <= APPLE_RTKIT_HIB_HANDOFF_MAX); + +static void apple_rtkit_helper_hib_tag(struct device *dev, char *tag, size_t len) +{ + snprintf(tag, len, "rtkh:%s", dev_name(dev)); +} + +static bool apple_rtkit_helper_cpu_running(struct apple_rtkit_helper *helper) +{ + return readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & + APPLE_ASC_CPU_CONTROL_RUN; +} + +/* + * Hibernation of an adopted session. The image is written while the firmware + * keeps running; its buffers live in SRAM or in no-map reserved RAM, so they + * are not part of the image and the host side has nothing to quiesce. The + * restore re-adopts the instance the boot loader started for the resume boot, + * exactly as probe adopted the first one. + * + * A session this driver started itself is left running as well: MTP does not + * come back from QUIESCED (RTKit endpoint discovery fails with -ETIME on the + * next start). When this is the boot kernel of a resume from disk (it started + * MTP so that the disk passphrase can be typed), it hands the running + * firmware to the image: mailbox RX is held, so that what the firmware sends + * now stays queued for the restored kernel, the messages already received + * are handled, and the shared buffers are described for the restored kernel, + * which can only take the session over if its own buffers sit at the same + * IOVAs (the firmware keeps writing to the IOVAs it was given, and after the + * restore the DART translates them through the image's page tables). + */ +static int apple_rtkit_helper_freeze(struct device *dev) +{ + struct apple_rtkit_helper *helper = dev_get_drvdata(dev); + struct apple_rtkit_helper_hib rec = {}; + char tag[32]; + int n, ret; + + helper->readopted = false; + helper->halted_underneath = false; + if (helper->adopted || !apple_rtkit_hib_restoring()) + return 0; + if (!apple_rtkit_helper_cpu_running(helper) || + !apple_rtkit_is_running(helper->rtk)) + return 0; + + apple_rtkit_hold_rx(helper->rtk); + apple_rtkit_flush_rx(helper->rtk); + helper->rx_held = true; + + n = apple_rtkit_describe_buffers(helper->rtk, rec.buf, ARRAY_SIZE(rec.buf)); + if (n < 0) { + dev_warn(dev, "resume from disk: cannot describe the RTKit buffers: %d\n", n); + return 0; + } + rec.count = n; + apple_rtkit_helper_hib_tag(dev, tag, sizeof(tag)); + ret = apple_rtkit_hib_handoff_put(tag, &rec, sizeof(rec)); + if (ret) + dev_warn(dev, "resume from disk: no hand-off record for the image: %d\n", ret); + else + dev_info(dev, "resume from disk: RTKit session left running for the image (%d buffers)\n", + n); + return 0; +} + +static int apple_rtkit_helper_thaw(struct device *dev) +{ + struct apple_rtkit_helper *helper = dev_get_drvdata(dev); + char tag[32]; + + /* The jump into the image failed: this kernel keeps the session. */ + if (helper->rx_held) { + helper->rx_held = false; + apple_rtkit_helper_hib_tag(dev, tag, sizeof(tag)); + apple_rtkit_hib_handoff_drop(tag); + apple_rtkit_resume_rx(helper->rtk); + } + return 0; +} + +/* + * A session this driver started itself, after a resume from disk. Taken over + * in place when the boot kernel left it running and its buffers match ours; + * otherwise left as before (stale until the driver is reloaded). + */ +static int apple_rtkit_helper_restore_own(struct apple_rtkit_helper *helper) +{ + struct device *dev = helper->dev; + struct apple_rtkit_helper_hib rec = {}, own = {}; + char tag[32]; + int i, n, ret; + + apple_rtkit_helper_hib_tag(dev, tag, sizeof(tag)); + ret = apple_rtkit_hib_handoff_take(tag, &rec, sizeof(rec)); + if (ret < 0) { + if (apple_rtkit_helper_cpu_running(helper)) { + dev_warn(dev, "resume from disk: co-processor runs without a hand-off from the boot kernel (%d): not taken over\n", + ret); + } else { + helper->halted_underneath = true; + dev_info(dev, "resume from disk: self-started RTKit session left for a reload\n"); + } + return 0; + } + if (!apple_rtkit_helper_cpu_running(helper)) { + helper->halted_underneath = true; + dev_warn(dev, "resume from disk: hand-off present but the co-processor is halted: not taken over\n"); + return 0; + } + + n = apple_rtkit_describe_buffers(helper->rtk, own.buf, ARRAY_SIZE(own.buf)); + own.count = n < 0 ? 0 : n; + if (n < 0 || rec.count > ARRAY_SIZE(rec.buf) || own.count != rec.count || + memcmp(own.buf, rec.buf, rec.count * sizeof(rec.buf[0]))) { + dev_err(dev, "resume from disk: RTKit buffers of the boot kernel differ from the image's: not taken over\n"); + for (i = 0; i < min_t(u32, rec.count, ARRAY_SIZE(rec.buf)); i++) + dev_err(dev, " boot kind %u iova %#llx size %#llx\n", + rec.buf[i].kind, rec.buf[i].iova, rec.buf[i].size); + for (i = 0; i < own.count; i++) + dev_err(dev, " image kind %u iova %#llx size %#llx\n", + own.buf[i].kind, own.buf[i].iova, own.buf[i].size); + return 0; + } + + ret = apple_rtkit_readopt(helper->rtk); + if (ret) { + dev_err(dev, "resume from disk: RTKit session not taken over: %d\n", ret); + return 0; + } + helper->readopted = true; + dev_info(dev, "resume from disk: took over the RTKit session the boot kernel started (%d buffers at the same IOVAs)\n", + n); + return 0; +} + +static int apple_rtkit_helper_restore(struct device *dev) +{ + struct apple_rtkit_helper *helper = dev_get_drvdata(dev); + struct apple_rtkit_inherited_buffer buffers[APPLE_RTKIT_HELPER_MAX_BUFFERS] = {}; + struct apple_rtkit *old, *rtk; + int count, live; + + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return apple_rtkit_helper_thaw(dev); + + if (!helper->adopted) + return apple_rtkit_helper_restore_own(helper); + + /* + * The restored tree still carries the handoff the image's boot stage + * wrote. The resume boot's loader hands the same firmware over at the + * same SRAM and no-map addresses (the inventory comes from the firmware + * carveouts, not from the kernel allocator), so re-read it and check + * that a CPU is running behind it. + */ + live = apple_rtkit_helper_live(helper); + if (live < 0) + return dev_err_probe(dev, live, "hibernation restore: invalid live RTKit handoff\n"); + if (!live) + return dev_err_probe(dev, -ENODEV, + "hibernation restore: no live RTKit session to re-adopt\n"); + count = apple_rtkit_helper_inherit_buffers(helper, buffers); + if (count < 0) + return dev_err_probe(dev, count, + "hibernation restore: invalid inherited RTKit buffers\n"); + + rtk = apple_rtkit_init_adopted_with_buffers(dev, helper, NULL, 0, + &apple_rtkit_helper_ops, buffers, count); + if (IS_ERR(rtk)) + return dev_err_probe(dev, PTR_ERR(rtk), + "hibernation restore: failed to re-adopt RTKit\n"); + + /* The old object described an instance that no longer exists. */ + old = helper->rtk; + helper->rtk = rtk; + helper->strict_buffers = count > 0 || + of_device_is_compatible(dev->of_node, "apple,t8132-mtp"); + apple_rtkit_free_retaining_buffers(old); + dev_info(dev, "re-adopted the running RTKit session after hibernation (%d retained buffers)\n", + count); + return 0; +} +#else static int __maybe_unused apple_rtkit_helper_freeze(struct device *dev) { struct apple_rtkit_helper *helper = dev_get_drvdata(dev); @@ -561,6 +793,7 @@ static int __maybe_unused apple_rtkit_helper_freeze(struct device *dev) /* A hibernation image cannot recreate the firmware's live DMA ownership. */ return helper->adopted ? -EBUSY : 0; } +#endif static const struct dev_pm_ops apple_rtkit_helper_pm_ops = { .prepare = pm_sleep_ptr(apple_rtkit_helper_prepare), @@ -569,6 +802,10 @@ static const struct dev_pm_ops apple_rtkit_helper_pm_ops = { .resume = pm_sleep_ptr(apple_rtkit_helper_resume), .freeze = pm_sleep_ptr(apple_rtkit_helper_freeze), .poweroff = pm_sleep_ptr(apple_rtkit_helper_freeze), +#ifdef CONFIG_HIBERNATION + .thaw = apple_rtkit_helper_thaw, + .restore = apple_rtkit_helper_restore, +#endif }; static const struct of_device_id apple_rtkit_helper_of_match[] = { diff --git a/drivers/soc/apple/rtkit-hib-handoff.c b/drivers/soc/apple/rtkit-hib-handoff.c new file mode 100644 index 0000000000000..bf5705142b8b1 --- /dev/null +++ b/drivers/soc/apple/rtkit-hib-handoff.c @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple RTKit: hand a running co-processor session from the kernel that loads + * a hibernation image (the boot kernel) to the kernel restored from it. + * + * A co-processor that the boot kernel started keeps running across the jump + * into the image, but everything the boot kernel knew about it is overwritten + * by the image. The few facts the restored kernel needs to take the session + * over (where the firmware's shared buffers are, how far a receive ring got) + * are left in __nosavedata: the image never saves those pages, so after the + * restore they still hold what the boot kernel wrote, provided both kernels + * are the same build loaded at the same physical address. Each record carries + * its own physical address and a checksum, so a record from a kernel loaded + * elsewhere, or no record at all, reads as absent. + * + * The storage must be in the kernel image, so this only works with RTKit + * built in. A taken record is cleared. + */ + +#include +#include +#include +#include +#include +#include + +#if IS_BUILTIN(CONFIG_APPLE_RTKIT) && IS_ENABLED(CONFIG_HIBERNATION) + +#define HANDOFF_MAGIC 0x52544b48 /* "RTKH" */ +#define HANDOFF_VERSION 1 +#define HANDOFF_SLOTS 4 +#define HANDOFF_TAG 32 + +struct apple_rtkit_hib_slot { + u32 magic; + u32 version; + u64 self_phys; + char tag[HANDOFF_TAG]; + u32 len; + u32 sum; + u8 data[APPLE_RTKIT_HIB_HANDOFF_MAX]; +}; + +static struct apple_rtkit_hib_slot handoff[HANDOFF_SLOTS] __nosavedata; +static DEFINE_SPINLOCK(handoff_lock); +static bool restoring; + +/* FNV-1a over everything but the checksum itself. */ +static u32 handoff_sum(const struct apple_rtkit_hib_slot *s) +{ + const u8 *p = (const u8 *)s; + size_t off = offsetof(struct apple_rtkit_hib_slot, sum); + u32 h = 0x811c9dc5; + size_t i; + + for (i = 0; i < sizeof(*s); i++) { + if (i >= off && i < off + sizeof(s->sum)) + continue; + h ^= p[i]; + h *= 0x01000193; + } + return h; +} + +static bool handoff_valid(const struct apple_rtkit_hib_slot *s) +{ + return s->magic == HANDOFF_MAGIC && s->version == HANDOFF_VERSION && + s->self_phys == __pa_symbol(s) && + s->len <= APPLE_RTKIT_HIB_HANDOFF_MAX && + s->tag[HANDOFF_TAG - 1] == '\0' && s->sum == handoff_sum(s); +} + +static struct apple_rtkit_hib_slot *handoff_find(const char *tag) +{ + int i; + + for (i = 0; i < HANDOFF_SLOTS; i++) + if (handoff_valid(&handoff[i]) && !strcmp(handoff[i].tag, tag)) + return &handoff[i]; + return NULL; +} + +bool apple_rtkit_hib_restoring(void) +{ + return READ_ONCE(restoring); +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_restoring); + +int apple_rtkit_hib_handoff_put(const char *tag, const void *data, size_t len) +{ + struct apple_rtkit_hib_slot *s; + unsigned long flags; + int i, ret = 0; + + if (len > APPLE_RTKIT_HIB_HANDOFF_MAX || strlen(tag) >= HANDOFF_TAG) + return -EINVAL; + + spin_lock_irqsave(&handoff_lock, flags); + s = handoff_find(tag); + for (i = 0; !s && i < HANDOFF_SLOTS; i++) + if (!handoff_valid(&handoff[i])) + s = &handoff[i]; + if (s) { + memset(s, 0, sizeof(*s)); + s->magic = HANDOFF_MAGIC; + s->version = HANDOFF_VERSION; + s->self_phys = __pa_symbol(s); + strscpy(s->tag, tag, sizeof(s->tag)); + s->len = len; + memcpy(s->data, data, len); + s->sum = handoff_sum(s); + } else { + ret = -ENOSPC; + } + spin_unlock_irqrestore(&handoff_lock, flags); + return ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_put); + +int apple_rtkit_hib_handoff_take(const char *tag, void *data, size_t len) +{ + struct apple_rtkit_hib_slot *s; + unsigned long flags; + int ret; + + spin_lock_irqsave(&handoff_lock, flags); + s = handoff_find(tag); + if (!s) { + ret = -ENOENT; + } else if (s->len != len) { + ret = -EINVAL; + } else { + memcpy(data, s->data, len); + ret = len; + } + if (s) + memset(s, 0, sizeof(*s)); + spin_unlock_irqrestore(&handoff_lock, flags); + return ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_take); + +void apple_rtkit_hib_handoff_drop(const char *tag) +{ + struct apple_rtkit_hib_slot *s; + unsigned long flags; + + spin_lock_irqsave(&handoff_lock, flags); + s = handoff_find(tag); + if (s) + memset(s, 0, sizeof(*s)); + spin_unlock_irqrestore(&handoff_lock, flags); +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_drop); + +static int apple_rtkit_hib_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + switch (action) { + case PM_RESTORE_PREPARE: + WRITE_ONCE(restoring, true); + break; + case PM_POST_RESTORE: + /* The image was not restored: this kernel goes on. */ + WRITE_ONCE(restoring, false); + break; + } + return NOTIFY_DONE; +} + +static struct notifier_block apple_rtkit_hib_nb = { + .notifier_call = apple_rtkit_hib_notify, +}; + +static int __init apple_rtkit_hib_init(void) +{ + /* + * A kernel restored from an image runs this only on its own first + * boot, and the flag is saved false in the image: in the restored + * kernel it reads false. + */ + return register_pm_notifier(&apple_rtkit_hib_nb); +} +core_initcall(apple_rtkit_hib_init); + +#else + +bool apple_rtkit_hib_restoring(void) +{ + return false; +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_restoring); + +int apple_rtkit_hib_handoff_put(const char *tag, const void *data, size_t len) +{ + return -EOPNOTSUPP; +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_put); + +int apple_rtkit_hib_handoff_take(const char *tag, void *data, size_t len) +{ + return -ENOENT; +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_take); + +void apple_rtkit_hib_handoff_drop(const char *tag) +{ +} +EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_drop); + +#endif diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 8230ebe11d411..6c6d75a9428e7 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -1641,6 +1641,63 @@ bool apple_rtkit_has_endpoint(struct apple_rtkit *rtk, u8 ep) } EXPORT_SYMBOL_GPL(apple_rtkit_has_endpoint); +int apple_rtkit_describe_buffers(struct apple_rtkit *rtk, + struct apple_rtkit_buffer_desc *desc, + unsigned int max) +{ + const struct apple_rtkit_shmem *bufs[APPLE_RTKIT_BUF_COUNT] = { + [APPLE_RTKIT_BUF_IOREPORT] = &rtk->ioreport_buffer, + [APPLE_RTKIT_BUF_COREANALYTICS] = &rtk->coreanalytics_buffer, + [APPLE_RTKIT_BUF_CRASHLOG] = &rtk->crashlog_buffer, + [APPLE_RTKIT_BUF_OSLOG] = &rtk->oslog_buffer, + [APPLE_RTKIT_BUF_KDEBUG0] = &rtk->kdebug_buffer0, + [APPLE_RTKIT_BUF_KDEBUG1] = &rtk->kdebug_buffer1, + [APPLE_RTKIT_BUF_PMP_AUX] = &rtk->pmp_aux_buffer, + [APPLE_RTKIT_BUF_SYSLOG] = &rtk->syslog_buffer, + }; + unsigned int i, n = 0; + + for (i = 0; i < APPLE_RTKIT_BUF_COUNT; i++) { + if (!bufs[i]->size) + continue; + if (n == max) + return -ENOSPC; + desc[n].kind = i; + desc[n].is_mapped = bufs[i]->is_mapped; + desc[n].iova = bufs[i]->iova; + desc[n].size = bufs[i]->size; + n++; + } + return n; +} +EXPORT_SYMBOL_GPL(apple_rtkit_describe_buffers); + +int apple_rtkit_readopt(struct apple_rtkit *rtk) +{ + /* + * Only a session that was running when it was left can be taken over: + * its endpoint map, protocol version, syslog geometry and buffers then + * describe the same firmware build and the same IOVAs as the instance + * that runs now (the caller compared the buffers). + */ + if (READ_ONCE(rtk->shutting_down) || !apple_rtkit_is_running(rtk)) + return -EINVAL; + + apple_mbox_hold_rx(rtk->mbox); + /* Acknowledgments still queued belong to the instance that is gone. */ + cancel_work_sync(&rtk->sync_ack_work); + flush_workqueue(rtk->wq); + kfifo_reset(&rtk->sync_ack_fifo); + + reinit_completion(&rtk->iop_pwr_ack_completion); + reinit_completion(&rtk->ap_pwr_ack_completion); + + /* Messages the other kernel left queued in the mailbox arrive now. */ + apple_mbox_release_rx(rtk->mbox); + return 0; +} +EXPORT_SYMBOL_GPL(apple_rtkit_readopt); + int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) { u64 msg; diff --git a/include/linux/soc/apple/aura-hibernate.h b/include/linux/soc/apple/aura-hibernate.h new file mode 100644 index 0000000000000..7c8b80de307be --- /dev/null +++ b/include/linux/soc/apple/aura-hibernate.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_SOC_APPLE_AURA_HIBERNATE_H +#define __LINUX_SOC_APPLE_AURA_HIBERNATE_H + +#include +#include + +struct device; +struct device_node; + +/* A firmware memory-region of a device as this boot's device tree gives it. */ +struct aura_fw_region { + u64 phys; + u64 iova; + u64 size; +}; + +/* + * Declared unconditionally so that Rust drivers can bind to them; without + * CONFIG_APPLE_AURA_HIBERNATE they must not call them. + */ +int aura_hibernate_boot_fw_regions(struct device_node *np, struct aura_fw_region *out, + unsigned int max); +int aura_hibernate_reprobe_after_thaw(struct device *dev); + +#ifdef CONFIG_APPLE_AURA_HIBERNATE +bool aura_hibernate_layout_fixed(void); +bool aura_hibernate_pfn_nosave(unsigned long pfn); +bool aura_hibernate_range_nosave(u64 start, u64 end); +int aura_hibernate_boot_chosen_u64(const char *name, u64 *val); +#else +static inline bool aura_hibernate_layout_fixed(void) { return false; } +static inline bool aura_hibernate_pfn_nosave(unsigned long pfn) { return false; } +static inline bool aura_hibernate_range_nosave(u64 start, u64 end) { return false; } +static inline int aura_hibernate_boot_chosen_u64(const char *name, u64 *val) { return -ENODEV; } +#endif + +#endif diff --git a/include/linux/soc/apple/rtkit-helper.h b/include/linux/soc/apple/rtkit-helper.h index 190caf432e098..4c712936e6c15 100644 --- a/include/linux/soc/apple/rtkit-helper.h +++ b/include/linux/soc/apple/rtkit-helper.h @@ -9,4 +9,20 @@ struct device; /* The caller holds a device link to the bound helper supplier. */ bool apple_rtkit_helper_preserves_input(struct device *dev); +/* + * After a resume from disk: the helper took over, in its .restore callback, + * the session that the boot kernel started and left running. Valid from the + * helper's .restore until its next .freeze. + */ +bool apple_rtkit_helper_readopted(struct device *dev); + +/* + * After a resume from disk: the session this helper had started itself was + * found with its co-processor halted (the boot loader stopped it and the boot + * kernel did not start it). Nothing answers until the helper is probed again, + * which starts the co-processor. Valid from the helper's .restore until its + * next .freeze. + */ +bool apple_rtkit_helper_halted_underneath(struct device *dev); + #endif diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 244e60bbc6989..60fa2ba2fdeba 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -442,4 +442,60 @@ void apple_rtkit_flush_rx(struct apple_rtkit *rtk); */ bool apple_rtkit_has_endpoint(struct apple_rtkit *rtk, u8 ep); +/* + * A shared buffer of a running session, as the co-processor addresses it. + * @kind is one of APPLE_RTKIT_BUF_*. + */ +#define APPLE_RTKIT_BUF_IOREPORT 0 +#define APPLE_RTKIT_BUF_COREANALYTICS 1 +#define APPLE_RTKIT_BUF_CRASHLOG 2 +#define APPLE_RTKIT_BUF_OSLOG 3 +#define APPLE_RTKIT_BUF_KDEBUG0 4 +#define APPLE_RTKIT_BUF_KDEBUG1 5 +#define APPLE_RTKIT_BUF_PMP_AUX 6 +#define APPLE_RTKIT_BUF_SYSLOG 7 +#define APPLE_RTKIT_BUF_COUNT 8 + +struct apple_rtkit_buffer_desc { + u32 kind; + u32 is_mapped; + u64 iova; + u64 size; +}; + +/* + * Describe the shared buffers the co-processor currently holds, in a fixed + * order. Returns the number of entries written, or -ENOSPC if @max is too + * small. + */ +int apple_rtkit_describe_buffers(struct apple_rtkit *rtk, + struct apple_rtkit_buffer_desc *desc, + unsigned int max); + +/* + * Take over, in place, a firmware instance that another kernel started and + * left running with its shared buffers at the IOVAs this session already + * holds (the caller must have checked that with apple_rtkit_describe_buffers() + * against the other kernel's description). Only host-side transient state is + * reset; no message is sent to the co-processor. + */ +int apple_rtkit_readopt(struct apple_rtkit *rtk); + +/* + * Resume from disk. The kernel that loads a hibernation image (the boot + * kernel) can leave a few bytes for the kernel restored from that image in + * memory that the image does not overwrite. Only available with RTKit + * built in; elsewhere put() fails and take() finds nothing. + * + * apple_rtkit_hib_restoring(): true in the boot kernel between + * PM_RESTORE_PREPARE and the jump into the image (or PM_POST_RESTORE), + * i.e. while its devices are quiesced for the image, not frozen for one. + */ +#define APPLE_RTKIT_HIB_HANDOFF_MAX 256 + +bool apple_rtkit_hib_restoring(void); +int apple_rtkit_hib_handoff_put(const char *tag, const void *data, size_t len); +int apple_rtkit_hib_handoff_take(const char *tag, void *data, size_t len); +void apple_rtkit_hib_handoff_drop(const char *tag); + #endif /* _LINUX_APPLE_RTKIT_H_ */ diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 01e0e6cfbced8..3da0006a9c8ea 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -114,6 +114,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/soc/apple/rtkit.rs b/rust/kernel/soc/apple/rtkit.rs index c34f187f4fb10..e4527ff598126 100644 --- a/rust/kernel/soc/apple/rtkit.rs +++ b/rust/kernel/soc/apple/rtkit.rs @@ -367,6 +367,15 @@ impl RtKit { to_result(unsafe { bindings::apple_rtkit_wake(self.rtk) }) } + /// Re-initialises a stopped session so a fresh HELLO/EPMAP boot can be + /// accepted again (hibernation restore of a coprocessor the boot loader + /// cold-restarted): detaches RX, frees inherited buffers, zeroes the + /// endpoint bitmap, reinitialises the completions and restarts the mailbox. + pub fn reinit(self: Pin<&mut Self>) -> Result { + // SAFETY: `rtk` is valid per the type invariant. + to_result(unsafe { bindings::apple_rtkit_reinit(self.rtk) }) + } + /// Waits for the RTKit coprocessor to finish booting. pub fn boot(self: Pin<&mut Self>) -> Result { // SAFETY: `rtk` is valid per the type invariant. From 240d75324cd47268496b570c841e912023200e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 06:15:23 +0100 Subject: [PATCH 026/269] KVM: arm64: keep the guests' counters when the physical counter starts again in a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A guest's virtual and physical counters are the host's physical counter minus an offset that is fixed when the VM is created. A resume from disk is a cold boot: the physical counter has started again, and with the old offsets the guests' counters read less than before the hibernation. A Linux guest takes a clocksource that went backwards for no time passed. It still gets its timer interrupts, but its clock stands still until the host's counter has caught up with the old value, which takes as long as the host had been up. J616s, a guest with two vCPUs that prints its uptime every second, host up for an hour, hibernation and resume: the guest's CVAL went from 0x2076a3246c to 0xfffffce7b9ce8d2f, the guest took a timer interrupt every half second and printed nothing more. Keep the counter's value in a syscore suspend callback. If it reads less in the resume, move every VM's virtual and physical offset by the difference: the guests' counters go on from the value they had when the system went down, as for a VM that was stopped for that long. The time the machine was off is not shown to the guests. A counter that keeps running through a sleep is left alone, and the guests see that sleep as time that passed, as before. The same guest with this change: "the counter started again in the sleep (446875224457 -> 92491224896): the counters of 1 VM(s) kept", and the guest prints on after the resume with its uptime continuous. A suspend to RAM before and one after that hibernation leave it running too; the counter runs through those and the guest's uptime steps by the 25 s of the sleep. Signed-off-by: František Balázsy --- arch/arm64/kvm/arch_timer.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c index 6ac3321f4c575..0773f1c1209e8 100644 --- a/arch/arm64/kvm/arch_timer.c +++ b/arch/arm64/kvm/arch_timer.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -1436,6 +1437,58 @@ static void kvm_timer_handle_errata(void) } } +/* + * A guest's counters are the physical counter minus an offset that is fixed + * when the VM is made. The physical counter can start again in a sleep: it + * always does across a resume from disk, which is a cold boot. The guest's + * counters then fall back by what the counter lost, and the guest's clock + * stands still until the counter has caught up, which takes as long as the + * host had been up. Move the offsets by the same amount instead: the + * guest's counters go on from the value they had when the system went down, + * as for a VM that was stopped that long. A counter that keeps running + * through the sleep is left alone, and the guest sees the sleep as time that + * passed. + * + * Syscore: one CPU, interrupts off, every task frozen, so no vCPU is loaded + * and nothing changes vm_list. + */ +static u64 kvm_timer_sleep_cnt; + +static int kvm_timer_syscore_suspend(void *data) +{ + kvm_timer_sleep_cnt = kvm_phys_timer_read(); + return 0; +} + +static void kvm_timer_syscore_resume(void *data) +{ + u64 now = kvm_phys_timer_read(); + unsigned int vms = 0; + struct kvm *kvm; + u64 lost; + + if (now >= kvm_timer_sleep_cnt) + return; + lost = kvm_timer_sleep_cnt - now; + list_for_each_entry(kvm, &vm_list, vm_list) { + kvm->arch.timer_data.voffset -= lost; + kvm->arch.timer_data.poffset -= lost; + vms++; + } + if (vms) + kvm_info("the counter started again in the sleep (%llu -> %llu): the counters of %u VM(s) kept\n", + kvm_timer_sleep_cnt, now, vms); +} + +static const struct syscore_ops kvm_timer_syscore_ops = { + .suspend = kvm_timer_syscore_suspend, + .resume = kvm_timer_syscore_resume, +}; + +static struct syscore kvm_timer_syscore = { + .ops = &kvm_timer_syscore_ops, +}; + int __init kvm_timer_hyp_init(bool has_gic) { struct arch_timer_kvm_info *info; @@ -1505,6 +1558,7 @@ int __init kvm_timer_hyp_init(bool has_gic) } kvm_timer_handle_errata(); + register_syscore(&kvm_timer_syscore); return 0; out_free_ptimer_irq: From 9db5869ce5bfd938041d62085ffab22f2ca642a8 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 14:28:32 +1100 Subject: [PATCH 027/269] arm64: share the sleep registry so one kernel builds both controllers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The arch/arm64/configs/asahi.config part of "soc: apple: share the sleep registry so one kernel builds both controllers", which goes with the hibernation core changes; the rest of that commit is with the sleep controller changes. The T8140 sleep driver (sleep.c) and the T6040 one (t6040-sleep.c) each defined the coprocessor and enclave registry that drivers reach through , so the two exported the same nine symbols and APPLE_T6040_SLEEP had to depend on !APPLE_SOC_SLEEP. The universal config sets APPLE_SOC_SLEEP, so it never built the T6040 controller. Move the registry into sleep-registry.c behind a hidden APPLE_SLEEP_REGISTRY that both options select. The table holds six entries and the superset of the two records. Each controller binds to the registry in its probe with two answers of its own: whether this sleep's commit stops the registered coprocessors, and whether an enclave registration is taken. Both keep their earlier rules: T8140 answers from its own state and pm_suspend_via_firmware() and takes an enclave only when its commit stops coprocessors; T6040 answers as before and takes every enclave. With no controller bound the commit stops nothing and an enclave registration is refused. The controllers bind only to their own compatibles (apple,t8140-sleep, apple,t6040-sleep), so at most one is bound and installs suspend_ops. Drop the dependency, give the registry prototypes their own guard in sleep.h so every combination compiles, and enable APPLE_T6040_SLEEP in asahi.config. Signed-off-by: Ryan Murray Signed-off-by: František Balázsy --- arch/arm64/configs/asahi.config | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 70858dca94b8a..c0b5ba114bf13 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -177,3 +177,4 @@ CONFIG_EDAC_APPLE_SOC=m # the ctxr_exec_window= command-line option is given. CONFIG_ARM64_CTXR_EXECMEM=y CONFIG_APPLE_SOC_SLEEP=y +CONFIG_APPLE_T6040_SLEEP=y From a6f9c1df55eb2f544549fbb624ef601aa27f6bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:36 +0100 Subject: [PATCH 028/269] PM: hibernate: mark a restored kernel without testing the error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit create_image() sets hibernate_restored inside if (!in_suspend), which is reached only in a kernel restored from an image: there the arch code has cleared in_suspend and swsusp_arch_suspend() returned 0. So !error is always true at that point; store true. No functional change. Signed-off-by: František Balázsy --- kernel/power/hibernate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index a8d992bb4a2c7..41784a6771e34 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -425,7 +425,7 @@ static int create_image(int platform_mode) if (!in_suspend) { events_check_enabled = false; clear_or_poison_free_pages(); - WRITE_ONCE(hibernate_restored, !error); + WRITE_ONCE(hibernate_restored, true); } platform_leave(platform_mode); From b135dadc34839804b6d3b83e7be1a18552b17359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:37 +0100 Subject: [PATCH 029/269] arm64: hibernate: put the boot layout comment above its function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment that explains what hibernate_boot_layout() covers sat above hibernate_mix_span(), directly on top of that function's own comment. Move it to hibernate_boot_layout(). Comment move only, no functional change. Signed-off-by: František Balázsy --- arch/arm64/kernel/hibernate.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 048a29d5d54fd..e5bdc6a2326c1 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -106,23 +106,6 @@ static u64 hibernate_layout_mix(u64 h, const void *p, size_t len) return h; } -/* - * The image restores every page to the frame it came from, so the boot that - * resumes it must give the kernel what the boot that wrote it did: the same - * RAM, and on Apple machines at EL2 the same CTXR exec window (secondaries only - * execute inside it) and aura's fixed hibernation layout. A boot loader decides - * them on every boot; the kernel build check above cannot see them. - * - * aura's own image moves with the boot object, and its CPU release addresses - * and S2R mailbox with it, inside the range the layout keeps from Linux. The - * restored kernel uses neither address of the boot that wrote the image: its - * CPUs come back from the boot kernel's pen, a CPU started at its reset vector - * again finds the release target the boot kernel wrote (both kernels run at the - * window's fixed address), and the T6040 sleep driver takes this boot's mailbox - * from aura's tree after the restore. What has to match is the spin table's - * shape (the same CPUs, at the same offsets from the mailbox) and that this - * boot keeps all of it out of the image. - */ /* * Mix one memory span less the parts in @ex, ranges that this boot keeps out of * the image. iBoot's no-map allocations inside aura's fixed layout (the DCP @@ -147,6 +130,23 @@ static u64 hibernate_mix_span(u64 h, u64 b, u64 e, u32 flags, const u64 (*ex)[2] return hibernate_layout_mix(h, &flags, sizeof(flags)); } +/* + * The image restores every page to the frame it came from, so the boot that + * resumes it must give the kernel what the boot that wrote it did: the same + * RAM, and on Apple machines at EL2 the same CTXR exec window (secondaries only + * execute inside it) and aura's fixed hibernation layout. A boot loader decides + * them on every boot; the kernel build check above cannot see them. + * + * aura's own image moves with the boot object, and its CPU release addresses + * and S2R mailbox with it, inside the range the layout keeps from Linux. The + * restored kernel uses neither address of the boot that wrote the image: its + * CPUs come back from the boot kernel's pen, a CPU started at its reset vector + * again finds the release target the boot kernel wrote (both kernels run at the + * window's fixed address), and the T6040 sleep driver takes this boot's mailbox + * from aura's tree after the restore. What has to match is the spin table's + * shape (the same CPUs, at the same offsets from the mailbox) and that this + * boot keeps all of it out of the image. + */ static u64 hibernate_boot_layout(void) { static const char * const chosen_props[] = { From 2e3101de479edd0b27437d39423a5f110b6ddcd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:37 +0100 Subject: [PATCH 030/269] arm64: smp_spin_table: say what still needs the platform power ops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment over the parking spin table said that CPU hotplug fails until a platform driver provides the power ops. Since the parked CPU comes back through cpu_soft_restart(), only stopping the parked CPUs for a suspend needs them (spin_table_hold_parked() refuses without); hotplug needs KEXEC_CORE. Say so, and rejoin a line break in the pen comment. Comments only, no functional change. Signed-off-by: František Balázsy --- arch/arm64/kernel/smp_spin_table.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kernel/smp_spin_table.c b/arch/arm64/kernel/smp_spin_table.c index 2326f3f5df57a..e65522250995f 100644 --- a/arch/arm64/kernel/smp_spin_table.c +++ b/arch/arm64/kernel/smp_spin_table.c @@ -146,8 +146,10 @@ const struct cpu_operations smp_spin_table_ops = { * masked. From there it goes back through secondary_holding_pen, or, when the * platform suspends the system, it stops and takes its last WFI after the commit. * It is then started again at its reset vector. Stopping and starting a CPU is - * SoC-specific, so a platform driver provides it. Until one has, CPU hotplug - * fails cleanly, as on plain spin-table. + * SoC-specific, so a platform driver provides it (spin_table_set_power_ops()); + * until one has, a suspend that needs the parked CPUs stopped is refused. + * Parking and leaving the park need only cpu_soft_restart(): without + * KEXEC_CORE, CPU hotplug fails cleanly, as on plain spin-table. * * A CPU left awake at the commit can hang the machine, and the commit can't be * undone. So each parked CPU first answers a hold, while the suspend can still @@ -176,9 +178,8 @@ static bool spin_table_stop_released; * the kernel build that is running; with the boot kernel loaded at the image * kernel's physical address (checked when the resume starts) the pen is at the * same place in both, and the restored kernel reads what the boot kernel left - * there. It - * starts each CPU from the pen through secondary_holding_pen, as at boot. - * Nothing is powered off, so this needs no platform power ops. + * there. It starts each CPU from the pen through secondary_holding_pen, as at + * boot. Nothing is powered off, so this needs no platform power ops. */ #define SPIN_TABLE_PEN_MAGIC 0x314e455034535053ULL /* "SPS4PEN1" */ #define SPIN_TABLE_PEN_HERE 0x45524548ULL /* "HERE", from the pen */ From e9c9a50703cc1e2214a7aa3fef2952ae5c983a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:05:05 +0100 Subject: [PATCH 031/269] soc: apple: rtkit-helper: say why a self-started session keeps running MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment cited the error code a test had seen when MTP was started again after a quiesce. Say what the firmware does instead: it does not answer the endpoint discovery of the next start. Comment only, no functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index 93aa9bb6af850..a51f2aa50de30 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -621,16 +621,17 @@ static bool apple_rtkit_helper_cpu_running(struct apple_rtkit_helper *helper) * restore re-adopts the instance the boot loader started for the resume boot, * exactly as probe adopted the first one. * - * A session this driver started itself is left running as well: MTP does not - * come back from QUIESCED (RTKit endpoint discovery fails with -ETIME on the - * next start). When this is the boot kernel of a resume from disk (it started - * MTP so that the disk passphrase can be typed), it hands the running - * firmware to the image: mailbox RX is held, so that what the firmware sends - * now stays queued for the restored kernel, the messages already received - * are handled, and the shared buffers are described for the restored kernel, - * which can only take the session over if its own buffers sit at the same - * IOVAs (the firmware keeps writing to the IOVAs it was given, and after the - * restore the DART translates them through the image's page tables). + * A session this driver started itself is left running as well: the MTP + * firmware does not come back from QUIESCED (it does not answer the endpoint + * discovery of the next start). When this is the boot kernel of a resume from + * disk (it started MTP so that the disk passphrase can be typed), it hands + * the running firmware to the image: mailbox RX is held, so that what the + * firmware sends now stays queued for the restored kernel, the messages + * already received are handled, and the shared buffers are described for the + * restored kernel, which can only take the session over if its own buffers + * sit at the same IOVAs (the firmware keeps writing to the IOVAs it was + * given, and after the restore the DART translates them through the image's + * page tables). */ static int apple_rtkit_helper_freeze(struct device *dev) { From 6c5180789773565cbd7a4a9ea63628cb441fc663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:49 +0100 Subject: [PATCH 032/269] arm64: hibernate: compute the boot layout once on restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit arch_hibernation_header_restore() computed hibernate_boot_layout() a second time to print it on a mismatch, and on a match printed the value the hibernating kernel had already logged. Compute it once, and drop the line on a match. The comparison and its outcome are unchanged. Signed-off-by: František Balázsy --- arch/arm64/kernel/hibernate.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index e5bdc6a2326c1..1119168a392d8 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -259,6 +259,7 @@ EXPORT_SYMBOL(arch_hibernation_header_save); int arch_hibernation_header_restore(void *addr) { + u64 layout; int ret; struct arch_hibernate_hdr_invariants invariants; struct arch_hibernate_hdr *hdr = addr; @@ -268,12 +269,12 @@ int arch_hibernation_header_restore(void *addr) pr_crit("Hibernate image not generated by this kernel!\n"); return -EINVAL; } - if (hdr->boot_layout != hibernate_boot_layout()) { + layout = hibernate_boot_layout(); + if (hdr->boot_layout != layout) { pr_crit("Hibernate image written under another boot layout (%016llx, this boot %016llx): not restored\n", - hdr->boot_layout, hibernate_boot_layout()); + hdr->boot_layout, layout); return -EINVAL; } - pr_info("Hibernate image written under this boot layout (%016llx)\n", hdr->boot_layout); sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr); pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu, From 5236e296c1bc5885b6c0123307b49dd574fb4150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:50 +0100 Subject: [PATCH 033/269] arm64: smp_spin_table: name the pen's physical address in a local MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit spin_table_hibernate_pen_begin() passed a compound literal to pr_err() to print the physical address of swapper_pg_dir. Keep the address in a local, compare against it and print it from there. No functional change. Signed-off-by: František Balázsy --- arch/arm64/kernel/smp_spin_table.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/smp_spin_table.c b/arch/arm64/kernel/smp_spin_table.c index e65522250995f..9d5f26afe4edd 100644 --- a/arch/arm64/kernel/smp_spin_table.c +++ b/arch/arm64/kernel/smp_spin_table.c @@ -219,18 +219,19 @@ static void spin_table_pen_clean(void *start, size_t size) */ int spin_table_hibernate_pen_begin(phys_addr_t image_ttbr1, int primary) { + phys_addr_t here = __pa_symbol(swapper_pg_dir); int cpu; /* No record of an earlier, abandoned resume may reach the restored kernel. */ spin_table_pen.magic = 0; spin_table_pen_clean(&spin_table_pen, sizeof(spin_table_pen.magic)); - if (image_ttbr1 != __pa_symbol(swapper_pg_dir)) { + if (image_ttbr1 != here) { for_each_online_cpu(cpu) { if (cpu == primary || get_cpu_ops(cpu) != &smp_rvbar_spin_table_ops) continue; pr_err("hibernation: the image's kernel ran at another physical address (swapper_pg_dir %pa, here %pa): spin-table CPUs cannot wait in this kernel's pen, resume refused\n", - &image_ttbr1, &(phys_addr_t){ __pa_symbol(swapper_pg_dir) }); + &image_ttbr1, &here); return -EINVAL; } return 0; From 22ce23a4b033cf244dbfb9de778d297ca12998a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:14:12 +0100 Subject: [PATCH 034/269] soc: apple: rtkit-helper: report restore failures with dev_err() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore callback reported its failures with dev_err_probe(), which belongs to probe and records a deferral reason against a bound device. Print them with dev_err() and return the error as before. The hand-over and the take-over of a session, which happen on every resume from disk that has them, go to debug level. Only log calls change. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 39 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index a51f2aa50de30..a1428534bcabc 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -663,8 +663,8 @@ static int apple_rtkit_helper_freeze(struct device *dev) if (ret) dev_warn(dev, "resume from disk: no hand-off record for the image: %d\n", ret); else - dev_info(dev, "resume from disk: RTKit session left running for the image (%d buffers)\n", - n); + dev_dbg(dev, "resume from disk: RTKit session left running for the image (%d buffers)\n", + n); return 0; } @@ -733,8 +733,8 @@ static int apple_rtkit_helper_restore_own(struct apple_rtkit_helper *helper) return 0; } helper->readopted = true; - dev_info(dev, "resume from disk: took over the RTKit session the boot kernel started (%d buffers at the same IOVAs)\n", - n); + dev_dbg(dev, "resume from disk: took over the RTKit session the boot kernel started (%d buffers at the same IOVAs)\n", + n); return 0; } @@ -760,21 +760,26 @@ static int apple_rtkit_helper_restore(struct device *dev) * that a CPU is running behind it. */ live = apple_rtkit_helper_live(helper); - if (live < 0) - return dev_err_probe(dev, live, "hibernation restore: invalid live RTKit handoff\n"); - if (!live) - return dev_err_probe(dev, -ENODEV, - "hibernation restore: no live RTKit session to re-adopt\n"); + if (live < 0) { + dev_err(dev, "hibernation restore: invalid live RTKit handoff: %d\n", live); + return live; + } + if (!live) { + dev_err(dev, "hibernation restore: no live RTKit session to re-adopt\n"); + return -ENODEV; + } count = apple_rtkit_helper_inherit_buffers(helper, buffers); - if (count < 0) - return dev_err_probe(dev, count, - "hibernation restore: invalid inherited RTKit buffers\n"); + if (count < 0) { + dev_err(dev, "hibernation restore: invalid inherited RTKit buffers: %d\n", count); + return count; + } rtk = apple_rtkit_init_adopted_with_buffers(dev, helper, NULL, 0, &apple_rtkit_helper_ops, buffers, count); - if (IS_ERR(rtk)) - return dev_err_probe(dev, PTR_ERR(rtk), - "hibernation restore: failed to re-adopt RTKit\n"); + if (IS_ERR(rtk)) { + dev_err(dev, "hibernation restore: failed to re-adopt RTKit: %pe\n", rtk); + return PTR_ERR(rtk); + } /* The old object described an instance that no longer exists. */ old = helper->rtk; @@ -782,8 +787,8 @@ static int apple_rtkit_helper_restore(struct device *dev) helper->strict_buffers = count > 0 || of_device_is_compatible(dev->of_node, "apple,t8132-mtp"); apple_rtkit_free_retaining_buffers(old); - dev_info(dev, "re-adopted the running RTKit session after hibernation (%d retained buffers)\n", - count); + dev_dbg(dev, "re-adopted the running RTKit session after hibernation (%d retained buffers)\n", + count); return 0; } #else From 2e64582a09606b7fd89eff55ecb905c7306e4c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:19:11 +0100 Subject: [PATCH 035/269] soc: apple: rtkit-helper: build hibernation callbacks unconditionally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hibernation callbacks sat in an #ifdef CONFIG_HIBERNATION block, with a second freeze callback for the other configuration, and .thaw and .restore were set under the same condition. On arm64 the PM core calls .freeze, .poweroff, .thaw and .restore only with CONFIG_HIBERNATION, so the second freeze callback was never called. Drop it, build the callbacks unconditionally and set them with pm_sleep_ptr(). With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index a1428534bcabc..8f0c72721cb8b 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -591,7 +591,6 @@ static int __maybe_unused apple_rtkit_helper_resume(struct device *dev) return 0; } -#ifdef CONFIG_HIBERNATION /* * What the boot kernel of a resume from disk leaves for the restored kernel * about a session it started itself: the shared buffers the firmware holds. @@ -791,15 +790,6 @@ static int apple_rtkit_helper_restore(struct device *dev) count); return 0; } -#else -static int __maybe_unused apple_rtkit_helper_freeze(struct device *dev) -{ - struct apple_rtkit_helper *helper = dev_get_drvdata(dev); - - /* A hibernation image cannot recreate the firmware's live DMA ownership. */ - return helper->adopted ? -EBUSY : 0; -} -#endif static const struct dev_pm_ops apple_rtkit_helper_pm_ops = { .prepare = pm_sleep_ptr(apple_rtkit_helper_prepare), @@ -808,10 +798,8 @@ static const struct dev_pm_ops apple_rtkit_helper_pm_ops = { .resume = pm_sleep_ptr(apple_rtkit_helper_resume), .freeze = pm_sleep_ptr(apple_rtkit_helper_freeze), .poweroff = pm_sleep_ptr(apple_rtkit_helper_freeze), -#ifdef CONFIG_HIBERNATION - .thaw = apple_rtkit_helper_thaw, - .restore = apple_rtkit_helper_restore, -#endif + .thaw = pm_sleep_ptr(apple_rtkit_helper_thaw), + .restore = pm_sleep_ptr(apple_rtkit_helper_restore), }; static const struct of_device_id apple_rtkit_helper_of_match[] = { From f11a999e367b9fc81a307d13debbe836387b98f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:13:25 +0100 Subject: [PATCH 036/269] soc: apple: rtkit-helper: adopt by one buffer rule at probe and restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An adopted MTP session keeps the buffers it negotiated with the boot stage that started it, so on boards where the firmware cannot give them up a request for a buffer the inventory does not list is refused. Probe decides that for a session with an inventory, for T8132, and for the J614s MTP. The restore after a hibernation adopts the session of the resume boot the same way but decided again with a copy of the rule that lacked the J614s: there a session adopted with an empty inventory refused new buffers after probe and granted them after a resume from disk, replacing storage the firmware still uses. Take the decision in one helper, and in the restore make it before the new session's receive path starts, as probe does. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index 8f0c72721cb8b..bcd666f166da4 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -271,6 +271,21 @@ static int apple_rtkit_helper_inherit_buffers( return count; } +/* + * Whether an adopted session refuses buffers its inventory does not list. With + * an inventory, and on T8132 and the J614s without one, no new allocation is + * safe: the firmware still uses what it negotiated with its first host. + * Producers that predate the inventory (T8140) keep mapping IOVAs inside the + * SRAM region. Probe and the restore after a hibernation adopt by this rule. + */ +static bool apple_rtkit_helper_strict(struct device *dev, int count) +{ + return count > 0 || + of_device_is_compatible(dev->of_node, "apple,t8132-mtp") || + (of_machine_is_compatible("apple,j614s") && + of_device_is_compatible(dev->of_node, "apple,t6041-mtp")); +} + static int apple_rtkit_helper_pm_register(struct apple_rtkit_helper *helper); static int apple_rtkit_helper_probe(struct platform_device *pdev) @@ -355,16 +370,9 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev) } if (live) { - /* - * Set before RX starts: with an inventory, or on T8132, no new - * buffer allocation is safe here. Producers that predate the - * inventory (T8140) keep mapping IOVAs inside the SRAM region. - */ + /* Set before RX starts, which may bring a buffer request. */ helper->adopted = true; - helper->strict_buffers = count > 0 || - of_device_is_compatible(dev->of_node, "apple,t8132-mtp") || - (of_machine_is_compatible("apple,j614s") && - of_device_is_compatible(dev->of_node, "apple,t6041-mtp")); + helper->strict_buffers = apple_rtkit_helper_strict(dev, count); helper->rtk = apple_rtkit_init_adopted_with_buffers( dev, helper, NULL, 0, &apple_rtkit_helper_ops, buffers, count); if (IS_ERR(helper->rtk)) @@ -773,6 +781,8 @@ static int apple_rtkit_helper_restore(struct device *dev) return count; } + /* As at probe, before the new session's RX starts. */ + helper->strict_buffers = apple_rtkit_helper_strict(dev, count); rtk = apple_rtkit_init_adopted_with_buffers(dev, helper, NULL, 0, &apple_rtkit_helper_ops, buffers, count); if (IS_ERR(rtk)) { @@ -783,8 +793,6 @@ static int apple_rtkit_helper_restore(struct device *dev) /* The old object described an instance that no longer exists. */ old = helper->rtk; helper->rtk = rtk; - helper->strict_buffers = count > 0 || - of_device_is_compatible(dev->of_node, "apple,t8132-mtp"); apple_rtkit_free_retaining_buffers(old); dev_dbg(dev, "re-adopted the running RTKit session after hibernation (%d retained buffers)\n", count); From 50547e30af8361ecb4366dfc20e3be8ff43ce11f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:54:00 +0100 Subject: [PATCH 037/269] arm64: errata: drop the apple_pmuv3_traps boot parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The parameter turned on the implementation defined PMUv3 traps for KVM guests on the T6040 cores, which are not in apple_cpus, until the traps are verified there. They have not been, the parameter is off by default, and a boot parameter is no place for a test switch: once the traps are verified, the two MIDRs belong in apple_cpus. Remove the parameter and its documentation. The file is again as it was before the parameter: guests on these cores see no PMU. Signed-off-by: František Balázsy --- .../admin-guide/kernel-parameters.txt | 9 ----- arch/arm64/kernel/cpu_errata.c | 34 +++---------------- 2 files changed, 5 insertions(+), 38 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 9ce5baf6854e7..2e6fa8b49752e 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -549,15 +549,6 @@ Kernel parameters autoconf= [IPV6] See Documentation/networking/ipv6.rst. - apple_pmuv3_traps= - [ARM64,EARLY] Enable the IMPDEF PMUv3 trapping of KVM - guests (HACR_EL2 bit 56) on Apple cores where it has - not been verified yet, currently the M4 Pro/Max (T6040) - E and P cores. With it, KVM advertises a PMUv3 to its - guests backed by the Apple CPU PMU driver; without it - those guests see no PMU. Default: off on these cores. - Format: , a bare "apple_pmuv3_traps" means on. - apm= [APM] Advanced Power Management See header of arch/x86/kernel/apm_32.c. diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index d5c3fe7533802..fa1a6a83caa97 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -292,29 +291,6 @@ static const struct midr_range apple_cpus[] = { {}, }; -/* - * Apple cores that aren't in apple_cpus whose IMPDEF PMUv3 traps (HACR_EL2 - * bit 56) are turned on only when asked for on the command line - * (apple_pmuv3_traps), until they have been verified on these cores. - */ -static const struct midr_range apple_m4_cpus[] = { - MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), - MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), - {}, -}; - -static bool apple_pmuv3_traps_optin __ro_after_init; - -static int __init early_apple_pmuv3_traps_cfg(char *arg) -{ - /* A bare "apple_pmuv3_traps" enables the traps. */ - if (!arg || !*arg) - arg = "on"; - - return kstrtobool(arg, &apple_pmuv3_traps_optin); -} -early_param("apple_pmuv3_traps", early_apple_pmuv3_traps_cfg); - static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int scope) { u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); @@ -328,13 +304,13 @@ static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int sco if (pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF) return false; - if (is_midr_in_range_list(apple_cpus)) - return true; - - return apple_pmuv3_traps_optin && is_midr_in_range_list(apple_m4_cpus); + return is_midr_in_range_list(apple_cpus); } -/* Apple cores with the same vGIC as M1/M2 that aren't in apple_cpus yet. */ +/* + * Apple cores with the same vGIC as M1/M2 that aren't in apple_cpus yet: + * that list also turns on the IMPDEF PMUv3 traps, unverified here. + */ static const struct midr_range apple_broken_seis_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), From 500a84751ba2a4b58bd8fbd61bf0c2d7da7c51a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:53 +0100 Subject: [PATCH 038/269] soc: apple: rtkit-helper: test the coprocessor's run bit in one place MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helper that reads the ASC's CPU run bit came with the hibernation callbacks, while the hand-off check and the removal read the same bit with a copy of the expression. Move the helper up and use it there. The three tests that read the bit with readl() are left as they are. No functional change: every path reads the bit as often and at the same point as before. With five users the compiler no longer inlines the helper. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index bcd666f166da4..52ea34080b7f5 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -141,6 +141,13 @@ static const struct apple_rtkit_ops apple_rtkit_helper_ops = { .shmem_destroy = apple_rtkit_helper_shmem_destroy, }; +/* Whether the coprocessor's CPU runs: halted, nothing answers on its mailbox. */ +static bool apple_rtkit_helper_cpu_running(struct apple_rtkit_helper *helper) +{ + return readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & + APPLE_ASC_CPU_CONTROL_RUN; +} + static void apple_rtkit_helper_release(void *data) { struct apple_rtkit_helper *helper = data; @@ -172,8 +179,7 @@ static int apple_rtkit_helper_live(struct apple_rtkit_helper *helper) return -EINVAL; } - if (!(readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & - APPLE_ASC_CPU_CONTROL_RUN)) { + if (!apple_rtkit_helper_cpu_running(helper)) { dev_warn(helper->dev, "RTKit handoff without a running CPU\n"); return -ENODEV; } @@ -427,8 +433,7 @@ static void apple_rtkit_helper_remove(struct platform_device *pdev) * believes it runs: nothing would answer the quiesce. Drop the session * without a handshake so that the next probe starts the CPU again. */ - if (!(readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & - APPLE_ASC_CPU_CONTROL_RUN)) { + if (!apple_rtkit_helper_cpu_running(helper)) { dev_info(&pdev->dev, "co-processor halted underneath (resume from disk): removed without quiesce\n"); return; } @@ -615,12 +620,6 @@ static void apple_rtkit_helper_hib_tag(struct device *dev, char *tag, size_t len snprintf(tag, len, "rtkh:%s", dev_name(dev)); } -static bool apple_rtkit_helper_cpu_running(struct apple_rtkit_helper *helper) -{ - return readl_relaxed(helper->asc_base + APPLE_ASC_CPU_CONTROL) & - APPLE_ASC_CPU_CONTROL_RUN; -} - /* * Hibernation of an adopted session. The image is written while the firmware * keeps running; its buffers live in SRAM or in no-map reserved RAM, so they From 9fe39708e55d7dbd91145848705cd97c99831f5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 03:40:59 +0100 Subject: [PATCH 039/269] soc: apple: rtkit: add an AP idle notice for use with interrupts off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple_rtkit_ap_power_noirq() polls the mailbox, but it waits with usleep_range() and jiffies and takes the acknowledgment from the receive work. None of that is to be had at a sleep controller's commit, where interrupts are off, one CPU is up and timekeeping is suspended. apple_rtkit_sleep_idle_polled() already sends the AP and IOP idle notices there for a coprocessor that is then stopped: it polls a bounded number of times with udelay() and takes the acknowledgments in the poll. Add apple_rtkit_ap_idle_polled() for a coprocessor that stays up and is only told that the AP goes idle, as the SMC is. It shares the start of the polled idle (what is waiting is taken first, an owed OSLOG counter is echoed) and sends the AP notice alone. Such a coprocessor's client still has traffic on its own endpoints: a reply to a command it sent just before, or an event. While the notice is polled for, a message for an application endpoint therefore goes to recv_message_early as usual, and one that is not taken there is queued for the receive work, which runs again after the wake. Everything else is handled as in the polled idle. Compiled only: its first user is the SMC's sleep commit in the next patch, which has not run on hardware yet. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit.c | 63 +++++++++++++++++++++++++++------ include/linux/soc/apple/rtkit.h | 10 ++++++ 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 6c6d75a9428e7..3ee1117bde3c7 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2571,18 +2571,21 @@ static int apple_rtkit_polled_state(struct apple_rtkit *rtk, u8 type, unsigned i return -ETIMEDOUT; } -int apple_rtkit_sleep_idle_polled(struct apple_rtkit *rtk, void __iomem *cpu_control, - unsigned int *us) +/* The receive path while a notice is polled for, and the usual one after it. */ +static void apple_rtkit_polled_set_rx(struct apple_rtkit *rtk, + void (*rx)(struct apple_mbox *mbox, + struct apple_mbox_msg msg, void *cookie)) { unsigned long flags; - u64 owed; - u32 status; - int ret; - *us = 0; spin_lock_irqsave(&rtk->mbox->rx_lock, flags); - rtk->mbox->rx = apple_rtkit_polled_rx; + rtk->mbox->rx = rx; spin_unlock_irqrestore(&rtk->mbox->rx_lock, flags); +} + +static void apple_rtkit_polled_owed(struct apple_rtkit *rtk) +{ + u64 owed; /* What's already waiting is answered first, or the firmware may not go idle. */ apple_mbox_poll(rtk->mbox); @@ -2594,6 +2597,17 @@ int apple_rtkit_sleep_idle_polled(struct apple_rtkit *rtk, void __iomem *cpu_con if (!apple_rtkit_polled_send(rtk, echo)) dev_err(rtk->dev, "RTKit: oslog echo failed before the idle\n"); } +} + +int apple_rtkit_sleep_idle_polled(struct apple_rtkit *rtk, void __iomem *cpu_control, + unsigned int *us) +{ + u32 status; + int ret; + + *us = 0; + apple_rtkit_polled_set_rx(rtk, apple_rtkit_polled_rx); + apple_rtkit_polled_owed(rtk); ret = apple_rtkit_polled_state(rtk, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE, &rtk->ap_power_state, us); @@ -2606,14 +2620,43 @@ int apple_rtkit_sleep_idle_polled(struct apple_rtkit *rtk, void __iomem *cpu_con APPLE_ASC_CPU_STATUS_DOWN, APPLE_RTKIT_POLLED_STEP_US, 5000); - spin_lock_irqsave(&rtk->mbox->rx_lock, flags); - rtk->mbox->rx = apple_rtkit_rx; - spin_unlock_irqrestore(&rtk->mbox->rx_lock, flags); + apple_rtkit_polled_set_rx(rtk, apple_rtkit_rx); return ret; } EXPORT_SYMBOL_GPL(apple_rtkit_sleep_idle_polled); +/* + * For a co-processor that stays up: its client may still be owed a reply or an event on + * its own endpoints. Those go the usual way, to recv_message_early and, if it leaves + * them, to the receive work, which runs again after the wake. + */ +static void apple_rtkit_ap_polled_rx(struct apple_mbox *mbox, struct apple_mbox_msg msg, + void *cookie) +{ + if (msg.msg1 >= APPLE_RTKIT_APP_ENDPOINT_START) + apple_rtkit_rx(mbox, msg, cookie); + else + apple_rtkit_polled_rx(mbox, msg, cookie); +} + +int apple_rtkit_ap_idle_polled(struct apple_rtkit *rtk, unsigned int *us) +{ + int ret; + + *us = 0; + apple_rtkit_polled_set_rx(rtk, apple_rtkit_ap_polled_rx); + apple_rtkit_polled_owed(rtk); + + ret = apple_rtkit_polled_state(rtk, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE, + &rtk->ap_power_state, us); + + apple_rtkit_polled_set_rx(rtk, apple_rtkit_rx); + + return ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_ap_idle_polled); + void apple_rtkit_mailbox_recv_off(struct apple_rtkit *rtk) { apple_mbox_set_recv_enabled(rtk->mbox, false); diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 60fa2ba2fdeba..242d36662a1d3 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -310,6 +310,16 @@ int apple_rtkit_ap_power(struct apple_rtkit *rtk, bool on); /* The same, for a noirq phase: device interrupts are off, so it polls. */ int apple_rtkit_ap_power_noirq(struct apple_rtkit *rtk, bool on); +/* + * The idle notice with interrupts off and one CPU up, where nothing can sleep and the + * receive work doesn't run: the mailbox is polled a bounded number of times, with no + * clock read, and the acknowledgment is taken in the poll. A message on one of the + * client's own endpoints still goes to recv_message_early; one it leaves waits for the + * receive work. @us is about how long the acknowledgment took. The wake's notice is + * apple_rtkit_ap_power() or apple_rtkit_ap_power_noirq(), as after any other. + */ +int apple_rtkit_ap_idle_polled(struct apple_rtkit *rtk, unsigned int *us); + /* * Stop a co-processor for a system sleep that may remove its power: idle, wait * for the status that shows it has finished, then clear the run bit. On an error From f6e50ad0d1caceb963feb803c8cf0c2957b139a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:38:24 +0100 Subject: [PATCH 040/269] soc: apple: aura-hibernate: probe again through the aura reprobe helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/aura-hibernate.c part of "soc: apple: sep: probe again through the aura reprobe helper", which goes with the hibernation core changes; the rest of that commit is with the coprocessor changes. After a resume from disk on a cold boot the SEP is unbound and probed again once the tasks are thawed, to register with the restarted enclave. The driver carried its own work item, spinlock, pending-device pointer and a module-exit guard to cancel the work. aura_hibernate_reprobe_after_thaw() does the same unbind and probe from the freezable workqueue, which the restore finds frozen, so the probe cannot start before every device's restore has completed. The helper is built in, so a module going away in the meantime leaves no work behind and needs no cancel. The registration marker is still removed before the probe is queued, and only once a cold boot is established, as before. A probe that leaves the device without a driver is not reported by device_reprobe(); the helper now logs that case too, as the SEP's own work item did. Without CONFIG_APPLE_AURA_HIBERNATE the probe is not queued and the driver says so, as the AOP does. Signed-off-by: František Balázsy --- drivers/soc/apple/aura-hibernate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/aura-hibernate.c b/drivers/soc/apple/aura-hibernate.c index 59cfe189a4313..4d73d69fa16a9 100644 --- a/drivers/soc/apple/aura-hibernate.c +++ b/drivers/soc/apple/aura-hibernate.c @@ -347,7 +347,8 @@ static void aura_hibernate_reprobe_work(struct work_struct *work) int ret; ret = device_reprobe(rp->dev); - if (ret) + /* A failed probe is not reported by device_reprobe() itself. */ + if (ret || !READ_ONCE(rp->dev->driver)) dev_err(rp->dev, "probe after the resume from disk failed: %d\n", ret); put_device(rp->dev); kfree(rp); From 48b0af496ac305c19c22380d8e0dd0a49066fcfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:54 +0100 Subject: [PATCH 041/269] soc: apple: rtkit: report an idle that kept the firmware's memory apart from an error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A co-processor may acknowledge the idle request of its IOP side with a power state other than idle (the low byte 1 or 2): it is asleep with its firmware and memory kept, which is what the caller wants after the AP side has idled. apple_rtkit_idle() returned -EINVAL for it, the same value as a bad argument or a stale state, and the display driver had to accept -EINVAL as success. Return APPLE_RTKIT_IDLE_RETAINED (-EALREADY) for exactly that acknowledgment. Timeouts and failures of the AP side are returned as before. Every other caller of apple_rtkit_idle(), apple_rtkit_idle_with() and apple_rtkit_sleep_stop() treats any non-zero value as a failure and only forwards or prints it, so for them only the printed number changes. The display driver now accepts the new value instead of -EINVAL and fails on any other error, -EINVAL from the transport included. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit.c | 22 ++++++++++++++++++---- include/linux/soc/apple/rtkit.h | 8 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 3ee1117bde3c7..1f7b101d5d195 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2203,8 +2203,9 @@ static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk, return 0; } -static int apple_rtkit_set_iop_power_state(struct apple_rtkit *rtk, - unsigned int state) +/* Ask the IOP for a power state and wait for its acknowledgment; the caller judges the state. */ +static int apple_rtkit_send_iop_power_state(struct apple_rtkit *rtk, + unsigned int state) { u64 msg; int ret; @@ -2217,7 +2218,15 @@ static int apple_rtkit_set_iop_power_state(struct apple_rtkit *rtk, if (ret) return ret; - ret = apple_rtkit_wait(rtk, &rtk->iop_pwr_ack_completion); + return apple_rtkit_wait(rtk, &rtk->iop_pwr_ack_completion); +} + +static int apple_rtkit_set_iop_power_state(struct apple_rtkit *rtk, + unsigned int state) +{ + int ret; + + ret = apple_rtkit_send_iop_power_state(rtk, state); if (ret) return ret; @@ -2355,11 +2364,16 @@ int apple_rtkit_idle_with(struct apple_rtkit *rtk, void (*after_ap)(void *data), if (after_ap) after_ap(data); - ret = apple_rtkit_set_iop_power_state(rtk, APPLE_RTKIT_PWR_STATE_IDLE); + ret = apple_rtkit_send_iop_power_state(rtk, APPLE_RTKIT_PWR_STATE_IDLE); if (ret) { dev_err(rtk->dev, "RTKit: IOP idle not acknowledged: %d\n", ret); return ret; } + if (rtk->iop_power_state != APPLE_RTKIT_PWR_STATE_IDLE) { + dev_dbg(rtk->dev, "RTKit: IOP acknowledged %#x instead of idle\n", + rtk->iop_power_state); + return APPLE_RTKIT_IDLE_RETAINED; + } rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_IDLE; rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_IDLE; diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 242d36662a1d3..44514f2c32eaa 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -264,6 +265,13 @@ int apple_rtkit_poweroff(struct apple_rtkit *rtk); */ int apple_rtkit_cold_restart(struct apple_rtkit *rtk, int (*start)(void *), void *ctx); +/* + * Returned by apple_rtkit_idle() and apple_rtkit_idle_with() when the AP side is idle + * and the IOP acknowledged a power state other than idle: it is asleep with its memory + * kept. No other error carries this value. + */ +#define APPLE_RTKIT_IDLE_RETAINED (-EALREADY) + /* * Put the co-processor into idle mode */ From 06cd2c47d6e2c2d0cdf5bf164b5409070986f24c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:51:32 +0100 Subject: [PATCH 042/269] arm64: build every sleep controller and drop two symbols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The arch/arm64/configs/asahi.config part of "soc: apple: sleep: build every sleep controller and drop two symbols", which goes with the hibernation core changes; the rest of that commit is with the sleep controller changes. APPLE_SLEEP_REGISTRY has only one selector, APPLE_SOC_SLEEP, and the registry is linked into the same object as the controller, so the symbol selects nothing a user could choose. APPLE_T6040_SLEEP exists for the dependency of the T6040 sleep on the SMC driver and for MFD_SYSCON, but a board that has no T6040 sleep controller already gets none: the node's status in the device tree is the switch. Link the registry into apple-sleep, build the T6040 sleep wherever the SMC driver is built in, as before, and select MFD_SYSCON in that case. The T6040 documentation moves to APPLE_SOC_SLEEP. A kernel with APPLE_SOC_SLEEP and the SMC driver as a module still builds the T8140 sleep alone; with both built in it now also carries the T6040 sleep. The asahi config fragment no longer sets the removed symbol. Signed-off-by: František Balázsy --- arch/arm64/configs/asahi.config | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index c0b5ba114bf13..70858dca94b8a 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -177,4 +177,3 @@ CONFIG_EDAC_APPLE_SOC=m # the ctxr_exec_window= command-line option is given. CONFIG_ARM64_CTXR_EXECMEM=y CONFIG_APPLE_SOC_SLEEP=y -CONFIG_APPLE_T6040_SLEEP=y From 5b712148622202e55a6a73ebfc78b8a30fe716b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:42:32 +0100 Subject: [PATCH 043/269] PM: hibernate: tell drivers that an image is being loaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A driver that must treat the kernel loading a hibernation image differently from a kernel that freezes for an image of its own has no way to ask: the freeze callbacks run the same in both. The SMC driver and the RTKit hand-off each registered a PM notifier that sets a flag on PM_RESTORE_PREPARE and clears it on PM_POST_RESTORE, and the hand-off copy is read by two more drivers. Keep the flag in the PM core, next to the restored flag, and have the two notifier helpers set it around the PM_RESTORE_PREPARE and PM_POST_RESTORE notifications, whether the load starts from the resume path or from the snapshot device. pm_hibernate_is_loading() reads it. It is true from before the first notifier runs (the copies became true only when their own notifier was reached) until PM_POST_RESTORE has been delivered, and it is cleared again when a notifier refuses the load, where the copies of the notifiers not reached never became true. It is saved in an image as false, like the notifier copies, so the restored kernel reads false. The SMC driver reads the core flag in its freeze callback and drops its notifier, which also removes the registration at probe. The RTKit hand-off keeps apple_rtkit_hib_restoring() for its callers and answers it from the core flag. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-hib-handoff.c | 34 +-------------------------- include/linux/suspend.h | 2 ++ kernel/power/hibernate.c | 33 ++++++++++++++++++++++++++ kernel/power/main.c | 13 +++++++++- kernel/power/power.h | 2 ++ 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/drivers/soc/apple/rtkit-hib-handoff.c b/drivers/soc/apple/rtkit-hib-handoff.c index bf5705142b8b1..0700ab8488284 100644 --- a/drivers/soc/apple/rtkit-hib-handoff.c +++ b/drivers/soc/apple/rtkit-hib-handoff.c @@ -18,7 +18,6 @@ */ #include -#include #include #include #include @@ -43,7 +42,6 @@ struct apple_rtkit_hib_slot { static struct apple_rtkit_hib_slot handoff[HANDOFF_SLOTS] __nosavedata; static DEFINE_SPINLOCK(handoff_lock); -static bool restoring; /* FNV-1a over everything but the checksum itself. */ static u32 handoff_sum(const struct apple_rtkit_hib_slot *s) @@ -82,7 +80,7 @@ static struct apple_rtkit_hib_slot *handoff_find(const char *tag) bool apple_rtkit_hib_restoring(void) { - return READ_ONCE(restoring); + return pm_hibernate_is_loading(); } EXPORT_SYMBOL_GPL(apple_rtkit_hib_restoring); @@ -153,36 +151,6 @@ void apple_rtkit_hib_handoff_drop(const char *tag) } EXPORT_SYMBOL_GPL(apple_rtkit_hib_handoff_drop); -static int apple_rtkit_hib_notify(struct notifier_block *nb, - unsigned long action, void *data) -{ - switch (action) { - case PM_RESTORE_PREPARE: - WRITE_ONCE(restoring, true); - break; - case PM_POST_RESTORE: - /* The image was not restored: this kernel goes on. */ - WRITE_ONCE(restoring, false); - break; - } - return NOTIFY_DONE; -} - -static struct notifier_block apple_rtkit_hib_nb = { - .notifier_call = apple_rtkit_hib_notify, -}; - -static int __init apple_rtkit_hib_init(void) -{ - /* - * A kernel restored from an image runs this only on its own first - * boot, and the flag is saved false in the image: in the restored - * kernel it reads false. - */ - return register_pm_notifier(&apple_rtkit_hib_nb); -} -core_initcall(apple_rtkit_hib_init); - #else bool apple_rtkit_hib_restoring(void) diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 2ebbb01fa14cc..a49c8045af181 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -455,8 +455,10 @@ static inline bool pm_hibernation_mode_is_suspend(void) { return false; } #ifdef CONFIG_HIBERNATION bool pm_hibernate_is_restored(void); +bool pm_hibernate_is_loading(void); #else static inline bool pm_hibernate_is_restored(void) { return false; } +static inline bool pm_hibernate_is_loading(void) { return false; } #endif int arch_resume_nosmt(void); diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 41784a6771e34..a93ee54010450 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -101,6 +101,39 @@ bool pm_hibernate_is_restored(void) } EXPORT_SYMBOL_GPL(pm_hibernate_is_restored); +/* Set while this kernel loads a hibernation image, see pm_hibernate_is_loading(). */ +static bool hibernate_loading; + +/** + * pm_hibernate_is_loading - Whether this kernel is loading a hibernation image. + * + * True from the PM_RESTORE_PREPARE notification, which starts the loading of an + * image (from the resume path or through the snapshot device), until the + * PM_POST_RESTORE notification that ends it when the image was not restored + * (or the notification chain refused it). The devices of the kernel that + * loads an image are quiesced for the jump into the image, not frozen for an + * image of their own; the flag lets drivers tell the two apart in their + * freeze callbacks. The kernel restored from an image reads false: the image + * holds the state of the kernel that created it. + */ +bool pm_hibernate_is_loading(void) +{ + return READ_ONCE(hibernate_loading); +} +EXPORT_SYMBOL_GPL(pm_hibernate_is_loading); + +/** + * hibernate_set_loading - Record that an image is being loaded or no longer is. + * @loading: New value of the flag read by pm_hibernate_is_loading(). + * + * Called from the PM notifier chain helpers around PM_RESTORE_PREPARE and + * PM_POST_RESTORE. + */ +void hibernate_set_loading(bool loading) +{ + WRITE_ONCE(hibernate_loading, loading); +} + #ifdef CONFIG_SUSPEND /** * pm_hibernation_mode_is_suspend - Check if hibernation has been set to suspend diff --git a/kernel/power/main.c b/kernel/power/main.c index 5429e9f19b655..ccced400c15c7 100644 --- a/kernel/power/main.c +++ b/kernel/power/main.c @@ -169,14 +169,25 @@ int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down) { int ret; + if (val_up == PM_RESTORE_PREPARE) + hibernate_set_loading(true); + ret = blocking_notifier_call_chain_robust(&pm_chain_head, val_up, val_down, NULL); + if (ret && val_up == PM_RESTORE_PREPARE) + hibernate_set_loading(false); + return notifier_to_errno(ret); } int pm_notifier_call_chain(unsigned long val) { - return blocking_notifier_call_chain(&pm_chain_head, val, NULL); + int ret = blocking_notifier_call_chain(&pm_chain_head, val, NULL); + + if (val == PM_POST_RESTORE) + hibernate_set_loading(false); + + return ret; } /* If set, devices may be suspended and resumed asynchronously. */ diff --git a/kernel/power/power.h b/kernel/power/power.h index 6c269ce9410ec..e43e77f9a999b 100644 --- a/kernel/power/power.h +++ b/kernel/power/power.h @@ -77,6 +77,7 @@ static inline void enable_restore_image_protection(void) {} #endif /* CONFIG_STRICT_KERNEL_RWX */ extern bool hibernation_in_progress(void); +void hibernate_set_loading(bool loading); #else /* !CONFIG_HIBERNATION */ @@ -84,6 +85,7 @@ static inline void hibernate_reserved_size_init(void) {} static inline void hibernate_image_size_init(void) {} static inline bool hibernation_in_progress(void) { return false; } +static inline void hibernate_set_loading(bool loading) {} #endif /* !CONFIG_HIBERNATION */ #define power_attr(_name) \ From db30c722af442a50b0d71004dce246bbef5f0317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:44:23 +0100 Subject: [PATCH 044/269] PM: hibernate: let the architecture clean the pages of a loaded image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit swsusp cleans every page of a loaded image to the point of coherency before the jump into it, for devices that read memory without snooping the CPU caches, and does so under #ifdef CONFIG_ARM64 in the generic snapshot code, with the justification of one SoC family in its comment. Keep the walk over the image's page frames in snapshot.c and ask the architecture, per frame, through arch_hibernate_clean_image_page(), enabled by ARCH_HIBERNATION_CLEAN_IMAGE. arm64 sets the symbol and provides the function with the body the walk had: a frame that is not in the boot kernel's linear map is left to the restore_pblist copy, any other frame is cleaned and invalidated to the PoC. The frames, their order, the cond_resched() cadence and the clean operations are the same as before; the generated arm64 code matches the removed loop body. Other architectures compile the walk out, as they did. The comment of the copy loop in hibernate-asm.S no longer names a SoC. Signed-off-by: František Balázsy --- arch/arm64/Kconfig | 4 ++++ arch/arm64/kernel/hibernate-asm.S | 8 ++++---- arch/arm64/kernel/hibernate.c | 24 ++++++++++++++++++++++++ include/linux/suspend.h | 17 +++++++++++++++++ kernel/power/snapshot.c | 24 ++++++------------------ 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 81f92a6fb50f7..224bba9710481 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2689,6 +2689,10 @@ config ARCH_HIBERNATION_HEADER def_bool y depends on HIBERNATION +config ARCH_HIBERNATION_CLEAN_IMAGE + def_bool y + depends on HIBERNATION + config ARCH_SUSPEND_POSSIBLE def_bool y diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S index f30269635a4fd..89fc1db49f618 100644 --- a/arch/arm64/kernel/hibernate-asm.S +++ b/arch/arm64/kernel/hibernate-asm.S @@ -68,10 +68,10 @@ SYM_CODE_START(swsusp_arch_suspend_exit) add x1, x10, #PAGE_SIZE /* - * Clean the copied page to PoC, not only PoU: on Apple SoCs the - * coprocessors (GPU UAT walker, DCP, ...) are not cache coherent and - * read the restored pages from DRAM. Clean+invalidate to PoC also - * covers what caches_clean_inval_pou() did for the I-side. + * Clean the copied page to PoC, not only PoU: non-coherent DMA + * masters (the coprocessors of some SoCs) read the restored pages + * from DRAM. Clean+invalidate to PoC also covers what + * caches_clean_inval_pou() did for the I-side. */ raw_dcache_line_size x2, x3 sub x3, x2, #1 diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 1119168a392d8..b30cc81f2736f 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -209,6 +210,29 @@ static u64 hibernate_boot_layout(void) return h; } +/* + * Most image pages are loaded straight into their original frames through the + * cacheable linear map. Non-coherent DMA masters (on Apple SoCs: the GPU page + * table walker, DCP, every RTKit co-processor, the NVMe queues) read DRAM, and + * a later eviction of a dirty line would overwrite what a device wrote since. + * Clean and invalidate the page to the PoC before the jump. + * + * A frame that is not in the boot kernel's linear map (for example the linear + * alias of the kernel image, which faults when touched) had nothing loaded + * through the linear map: such frames are restored by the restore_pblist copy, + * which cleans each copied page itself. + */ +int arch_hibernate_clean_image_page(unsigned long pfn) +{ + unsigned long va; + + if (!kernel_page_present(pfn_to_page(pfn))) + return 0; + va = (unsigned long)page_address(pfn_to_page(pfn)); + dcache_clean_inval_poc(va, va + PAGE_SIZE); + return 1; +} + int pfn_is_nosave(unsigned long pfn) { unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin); diff --git a/include/linux/suspend.h b/include/linux/suspend.h index a49c8045af181..3f2a60fbdc61a 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -431,6 +431,23 @@ int hibernate_resume_nonboot_cpu_disable(void); int arch_hibernation_header_save(void *addr, unsigned int max_size); int arch_hibernation_header_restore(void *addr); +#ifdef CONFIG_ARCH_HIBERNATION_CLEAN_IMAGE +/** + * arch_hibernate_clean_image_page - Make a loaded image page visible to devices. + * @pfn: Valid page frame of the image. + * + * Provided by an architecture that sets ARCH_HIBERNATION_CLEAN_IMAGE. + * Called once for every valid page frame of the image after the whole image + * has been loaded, while the list of those frames is still intact, so that + * the architecture can clean the caches for devices that do not snoop them. + * + * Return: 1 when the page was cleaned, 0 when it was left alone because + * nothing was loaded into it through the linear map (the frame is restored + * from restore_pblist). + */ +int arch_hibernate_clean_image_page(unsigned long pfn); +#endif + #else /* CONFIG_HIBERNATION */ static inline void register_nosave_region(unsigned long b, unsigned long e) {} static inline int swsusp_page_is_forbidden(struct page *p) { return 0; } diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 6ab21287493b5..42d24f43991f8 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -2751,14 +2751,13 @@ static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) return pbe->address; } -#ifdef CONFIG_ARM64 +#ifdef CONFIG_ARCH_HIBERNATION_CLEAN_IMAGE /* * Most image pages are loaded straight into their original frames through * the cacheable linear map (only conflicting pages go through restore_pblist). - * Non-coherent DMA masters (on Apple SoCs: the GPU page table walker, DCP, - * every RTKit co-processor, the NVMe queues) read DRAM, and a later eviction - * of a dirty line would overwrite what a device wrote since. Clean and - * invalidate every image page to the PoC before the jump. + * Where devices read memory without snooping the CPU caches, a later eviction + * of a dirty line would overwrite what a device wrote since: let the + * architecture clean every image page before the jump. */ static void swsusp_clean_restored_image(void) { @@ -2767,27 +2766,16 @@ static void swsusp_clean_restored_image(void) memory_bm_position_reset(&orig_bm); for (pfn = memory_bm_next_pfn(&orig_bm); pfn != BM_END_OF_MAP; pfn = memory_bm_next_pfn(&orig_bm)) { - unsigned long va; - if (!pfn_valid(pfn)) continue; - /* - * Not in the boot kernel's linear map (for example the linear - * alias of the kernel image, which faults when touched): - * nothing was loaded there through the linear map. Such frames - * are restored by the restore_pblist copy, which cleans each - * copied page itself. - */ - if (!kernel_page_present(pfn_to_page(pfn))) { + if (!arch_hibernate_clean_image_page(pfn)) { skipped++; continue; } - va = (unsigned long)page_address(pfn_to_page(pfn)); - dcache_clean_inval_poc(va, va + PAGE_SIZE); if (!(++n & 0xffff)) cond_resched(); } - pr_debug("%lu image pages cleaned to PoC, %lu outside the linear map left to the copy\n", + pr_debug("%lu image pages cleaned, %lu left to the restore_pblist copy\n", n, skipped); } #else From 02db30e349ca44643f9fc98d88ba60b203ba188f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:45:46 +0100 Subject: [PATCH 045/269] PM: hibernate: let the platform refuse hibernation through a hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hibernation_available() asked of_machine_is_compatible("apple,arm-platform") and aura_hibernate_layout_fixed() in the generic hibernation code, and the log line carried the reason of one platform. Add hibernation_set_platform_check(): platform code registers a function that is true while hibernation must stay refused, with the reason that is logged at the end of the boot when it is. hibernation_available() asks it where it asked the two Apple conditions. The aura hibernation code registers aura_hibernate_blocked(), which is the same expression (Apple platform and no fixed layout), evaluated at the same moments, and the same reason text; the explanation of why the machines refuse moves with it. The check is registered from the init of that code on every boot of a kernel that has it, where the Apple condition used to be compiled into the core unconditionally. To keep that fail-closed, APPLE_AURA_HIBERNATE is no longer a prompt: it is always on with ARCH_APPLE and HIBERNATION, as the refusal belongs to it. Nothing registers before that init runs, which precedes the first possible hibernation (the resume check is a later initcall level and user space starts after all of them). Signed-off-by: František Balázsy --- drivers/soc/apple/aura-hibernate.c | 20 +++++++++++++++ include/linux/suspend.h | 1 + kernel/power/hibernate.c | 41 +++++++++++++++++------------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/drivers/soc/apple/aura-hibernate.c b/drivers/soc/apple/aura-hibernate.c index 4d73d69fa16a9..b97181762ab28 100644 --- a/drivers/soc/apple/aura-hibernate.c +++ b/drivers/soc/apple/aura-hibernate.c @@ -65,6 +65,23 @@ bool aura_hibernate_layout_fixed(void) return aura_layout_fixed; } +/* + * Apple silicon Macs run firmware on their coprocessors (storage, secure + * enclave, display, always-on processor, system controller, GPU and more). + * Restoring an image means a cold boot through the platform boot chain, which + * restarts that firmware, but most of the Linux drivers have no + * freeze/poweroff/restore path that rebuilds their mailbox, ring and DMA state + * from a cold start. The restored kernel would keep using stale state, so + * writing and restoring images stays disabled on these machines, except where + * the boot object gives the OS a fixed memory layout and the drivers rebuild + * their coprocessor state from the cold boot that restores the image (aura + * hib=1, see above). + */ +static bool aura_hibernate_blocked(void) +{ + return of_machine_is_compatible("apple,arm-platform") && !aura_layout_fixed; +} + bool aura_hibernate_pfn_nosave(unsigned long pfn) { unsigned int i; @@ -385,6 +402,9 @@ static int __init aura_hibernate_init(void) u64 layout[2], dt_slot; int i, n; + hibernation_set_platform_check(aura_hibernate_blocked, + "coprocessor firmware state is not restored from an image"); + /* Only a boot loader that keeps the layout fixed publishes it. */ chosen = of_find_node_by_path("/chosen"); if (!chosen || diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 3f2a60fbdc61a..3013208a46c30 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -422,6 +422,7 @@ extern void hibernation_set_ops(const struct platform_hibernation_ops *ops); extern int hibernate(void); extern bool system_entering_hibernation(void); extern bool hibernation_available(void); +void hibernation_set_platform_check(bool (*blocked)(void), const char *reason); asmlinkage int swsusp_save(void); extern struct pbe *restore_pblist; int pfn_is_nosave(unsigned long pfn); diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index a93ee54010450..1356560841fd3 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,6 @@ #include #include #include -#include #include #include "power.h" @@ -160,29 +158,36 @@ bool hibernation_in_progress(void) return !atomic_read(&hibernate_atomic); } -/* - * Apple silicon Macs run firmware on their coprocessors (storage, secure - * enclave, display, always-on processor, system controller, GPU and more). - * Restoring an image means a cold boot through the platform boot chain, which - * restarts that firmware, but most of the Linux drivers have no - * freeze/poweroff/restore path that rebuilds their mailbox, ring and DMA state - * from a cold start. The restored kernel would keep using stale state, so - * writing and restoring images stays disabled on these machines. +static bool (*hibernation_platform_blocked_fn)(void); +static const char *hibernation_platform_blocked_reason; + +/** + * hibernation_set_platform_check - Let platform code refuse hibernation. + * @blocked: Tells whether hibernation is refused on this platform at the time. + * @reason: Why it is refused, for the log. + * + * A platform whose drivers cannot yet restore their devices from an image + * registers a check that is true until the platform has what it needs. While + * it is true, writing and restoring images stays disabled + * (hibernation_available() is false) and @reason is logged at the end of the + * boot. Only one check can be registered. * * pm_test dry runs are allowed (see hibernate()): every pm_test level stops * before create_image() takes a snapshot, so no image is written or restored. * They exercise the freezer and the device freeze/thaw callbacks that a real * implementation needs. */ +void hibernation_set_platform_check(bool (*blocked)(void), const char *reason) +{ + if (WARN_ON_ONCE(hibernation_platform_blocked_fn)) + return; + hibernation_platform_blocked_reason = reason; + hibernation_platform_blocked_fn = blocked; +} + static bool hibernation_platform_blocked(void) { - /* - * Except where the boot object gives the OS a fixed memory layout and - * the drivers rebuild their coprocessor state from the cold boot that - * restores the image (aura hib=1, see aura-hibernate.c). - */ - return of_machine_is_compatible("apple,arm-platform") && - !aura_hibernate_layout_fixed(); + return hibernation_platform_blocked_fn && hibernation_platform_blocked_fn(); } static bool hibernation_generic_available(void) @@ -201,7 +206,7 @@ bool hibernation_available(void) static int __init hibernation_platform_report(void) { if (hibernation_platform_blocked()) - pr_info("disabled on this platform: coprocessor firmware state is not restored from an image\n"); + pr_info("disabled on this platform: %s\n", hibernation_platform_blocked_reason); return 0; } late_initcall(hibernation_platform_report); From 71d768d2f32d3b3a509a3a4fc52a160e1fbdbe89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:47:36 +0100 Subject: [PATCH 046/269] arm64: hibernate: take platform facts from a hook, not from aura MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit arm64's hibernation code read aura's /chosen properties, called aura's nosave functions and kept aura's layout rules in the boot layout check that is stored with an image and compared before it is restored. Add struct arm64_hibernate_platform with two hooks, registered once at boot: pfn_nosave(), asked by pfn_is_nosave(), and layout(), which adds the platform's facts to the layout cookie. The part of the check that belongs to every arm64 machine stays: the CPU release addresses (as offsets from a base the platform may name, together with whether the platform keeps the page out of the image) and the memory map less the ranges the platform names. The aura hibernation code registers the hooks and carries the rest: the /chosen properties, the S2R mailbox and the split of aura's layout around the exec window, moved as they were, with the comment that explains them. arm64_hibernate_layout_mix() is the mixing function, made visible for the hook. The nosave ranges stay a table in aura-hibernate.c and are not handed to register_nosave_region(): that function allocates from memblock and runs at setup_arch() time, and the table is filled from a late initcall. The aura functions that nothing outside the file calls any more become static, and aura_hibernate_layout_fixed() is dropped. The cookie is bit for bit the same: the old function and the new pair were compiled against the same stubbed device tree and memblock in 200000 randomised boots (properties present or absent, mailbox, window, nosave tables, CPUs with and without release addresses, memory maps); every cookie is identical, and a change of one mixed value in the new code is caught. The set of pages kept out of the image is the table's, asked at the same place. Signed-off-by: František Balázsy --- arch/arm64/include/asm/suspend.h | 32 +++++++ arch/arm64/kernel/hibernate.c | 114 ++++++++++------------- drivers/soc/apple/aura-hibernate.c | 80 ++++++++++++++-- include/linux/soc/apple/aura-hibernate.h | 6 -- 4 files changed, 154 insertions(+), 78 deletions(-) diff --git a/arch/arm64/include/asm/suspend.h b/arch/arm64/include/asm/suspend.h index e9ce68d50ba45..f453f11af781e 100644 --- a/arch/arm64/include/asm/suspend.h +++ b/arch/arm64/include/asm/suspend.h @@ -48,6 +48,38 @@ int swsusp_arch_resume(void); int arch_hibernation_header_save(void *addr, unsigned int max_size); int arch_hibernation_header_restore(void *addr); +/** + * struct arm64_hibernate_layout - What a platform adds to the boot layout check. + * @ex: Ranges that this boot keeps out of the image and that the layout check + * leaves out of the memory it compares, in ascending order. + * @nex: Number of valid entries in @ex. + * @have_base: Whether CPU release addresses are compared as offsets from @base. + * @base: Base address for the release addresses, if @have_base. + */ +struct arm64_hibernate_layout { + u64 ex[2][2]; + int nex; + bool have_base; + u64 base; +}; + +/** + * struct arm64_hibernate_platform - Platform hooks of arm64 hibernation. + * @pfn_nosave: Whether the platform keeps page frame @pfn out of the image. + * @layout: Mix the platform's boot facts into the layout cookie in @h with + * arm64_hibernate_layout_mix(), fill @lay, and return the new hash. The + * cookie is stored with an image and compared before it is restored: the + * boot that resumes an image must give the kernel the layout of the boot + * that wrote it. + */ +struct arm64_hibernate_platform { + bool (*pfn_nosave)(unsigned long pfn); + u64 (*layout)(u64 h, struct arm64_hibernate_layout *lay); +}; + +void arm64_hibernate_set_platform(const struct arm64_hibernate_platform *platform); +u64 arm64_hibernate_layout_mix(u64 h, const void *p, size_t len); + /* Used to resume on the CPU we hibernated on */ int hibernate_resume_nonboot_cpu_disable(void); diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index b30cc81f2736f..ce251b5b20d0d 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include @@ -96,7 +95,16 @@ static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i) memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version)); } -static u64 hibernate_layout_mix(u64 h, const void *p, size_t len) +/** + * arm64_hibernate_layout_mix - Mix bytes into the boot layout cookie. + * @h: Hash so far. + * @p: Bytes to mix. + * @len: Number of bytes. + * + * FNV-1a, for the callback of a platform to mix its boot facts into the cookie + * in the same way as the rest of the layout. + */ +u64 arm64_hibernate_layout_mix(u64 h, const void *p, size_t len) { const u8 *b = p; @@ -126,87 +134,65 @@ static u64 hibernate_mix_span(u64 h, u64 b, u64 e, u32 flags, const u64 (*ex)[2] if (nex) return hibernate_mix_span(h, b, e, flags, ex + 1, nex - 1); size = e - b; - h = hibernate_layout_mix(h, &b, sizeof(b)); - h = hibernate_layout_mix(h, &size, sizeof(size)); - return hibernate_layout_mix(h, &flags, sizeof(flags)); + h = arm64_hibernate_layout_mix(h, &b, sizeof(b)); + h = arm64_hibernate_layout_mix(h, &size, sizeof(size)); + return arm64_hibernate_layout_mix(h, &flags, sizeof(flags)); +} + +static const struct arm64_hibernate_platform *hibernate_platform __ro_after_init; + +/** + * arm64_hibernate_set_platform - Register the platform's hibernation hooks. + * @platform: Hooks of the platform; they must stay valid. + * + * Only one platform can register, at boot. + */ +void __init arm64_hibernate_set_platform(const struct arm64_hibernate_platform *platform) +{ + if (WARN_ON_ONCE(hibernate_platform)) + return; + hibernate_platform = platform; +} + +static bool hibernate_platform_pfn_nosave(unsigned long pfn) +{ + return hibernate_platform && hibernate_platform->pfn_nosave && + hibernate_platform->pfn_nosave(pfn); } /* * The image restores every page to the frame it came from, so the boot that * resumes it must give the kernel what the boot that wrote it did: the same - * RAM, and on Apple machines at EL2 the same CTXR exec window (secondaries only - * execute inside it) and aura's fixed hibernation layout. A boot loader decides - * them on every boot; the kernel build check above cannot see them. - * - * aura's own image moves with the boot object, and its CPU release addresses - * and S2R mailbox with it, inside the range the layout keeps from Linux. The - * restored kernel uses neither address of the boot that wrote the image: its - * CPUs come back from the boot kernel's pen, a CPU started at its reset vector - * again finds the release target the boot kernel wrote (both kernels run at the - * window's fixed address), and the T6040 sleep driver takes this boot's mailbox - * from aura's tree after the restore. What has to match is the spin table's - * shape (the same CPUs, at the same offsets from the mailbox) and that this - * boot keeps all of it out of the image. + * RAM, the same CPUs started the same way, and whatever fixed layout the + * platform's boot loader gives the kernel, which the platform's hook adds. A + * boot loader decides them on every boot; the kernel build check above cannot + * see them. */ static u64 hibernate_boot_layout(void) { - static const char * const chosen_props[] = { - "aura,hibernate-layout", "aura,hibernate-dt", - "aurora,ctxr-exec-window", - }; - u64 h = 0xcbf29ce484222325ULL, mbox = 0, rel, hwid; - u64 layout[2], win[2], ex[2][2]; + struct arm64_hibernate_layout lay = {}; + u64 h = 0xcbf29ce484222325ULL, rel, hwid; struct memblock_region *r; - int nex = 0; struct device_node *np; - bool have_mbox = false; - const void *v; u8 kept; - int i, len; - np = of_find_node_by_path("/chosen"); - for (i = 0; np && i < ARRAY_SIZE(chosen_props); i++) { - v = of_get_property(np, chosen_props[i], &len); - if (!v) - continue; - h = hibernate_layout_mix(h, chosen_props[i], strlen(chosen_props[i])); - h = hibernate_layout_mix(h, v, len); - } - if (np && !of_property_read_u64(np, "aurora,s2r-mailbox", &mbox)) { - have_mbox = true; - kept = aura_hibernate_pfn_nosave(PHYS_PFN(mbox)); - h = hibernate_layout_mix(h, "aurora,s2r-mailbox", strlen("aurora,s2r-mailbox")); - h = hibernate_layout_mix(h, &kept, sizeof(kept)); - } - /* aura's layout less the OS window, only where all of it is kept out of the image */ - if (np && !of_property_read_u64_array(np, "aura,hibernate-layout", layout, 2) && - !of_property_read_u64_array(np, "aurora,ctxr-exec-window", win, 2) && - layout[0] <= win[0] && win[0] + win[1] <= layout[1]) { - ex[nex][0] = layout[0]; - ex[nex][1] = win[0]; - if (aura_hibernate_range_nosave(ex[nex][0], ex[nex][1])) - nex++; - ex[nex][0] = win[0] + win[1]; - ex[nex][1] = layout[1]; - if (aura_hibernate_range_nosave(ex[nex][0], ex[nex][1])) - nex++; - } - of_node_put(np); + if (hibernate_platform && hibernate_platform->layout) + h = hibernate_platform->layout(h, &lay); for_each_of_cpu_node(np) { if (of_property_read_u64(np, "cpu-release-addr", &rel)) continue; hwid = of_get_cpu_hwid(np, 0); - kept = aura_hibernate_pfn_nosave(PHYS_PFN(rel)); - if (have_mbox) - rel -= mbox; - h = hibernate_layout_mix(h, &hwid, sizeof(hwid)); - h = hibernate_layout_mix(h, &rel, sizeof(rel)); - h = hibernate_layout_mix(h, &kept, sizeof(kept)); + kept = hibernate_platform_pfn_nosave(PHYS_PFN(rel)); + if (lay.have_base) + rel -= lay.base; + h = arm64_hibernate_layout_mix(h, &hwid, sizeof(hwid)); + h = arm64_hibernate_layout_mix(h, &rel, sizeof(rel)); + h = arm64_hibernate_layout_mix(h, &kept, sizeof(kept)); } for_each_mem_region(r) - h = hibernate_mix_span(h, r->base, r->base + r->size, r->flags, ex, nex); + h = hibernate_mix_span(h, r->base, r->base + r->size, r->flags, lay.ex, lay.nex); return h; } @@ -239,7 +225,7 @@ int pfn_is_nosave(unsigned long pfn) unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1); return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) || - crash_is_nosave(pfn) || aura_hibernate_pfn_nosave(pfn); + crash_is_nosave(pfn) || hibernate_platform_pfn_nosave(pfn); } void notrace save_processor_state(void) diff --git a/drivers/soc/apple/aura-hibernate.c b/drivers/soc/apple/aura-hibernate.c index b97181762ab28..8c50b3cd33dba 100644 --- a/drivers/soc/apple/aura-hibernate.c +++ b/drivers/soc/apple/aura-hibernate.c @@ -43,6 +43,7 @@ #include #include #include +#include static bool aura_layout_fixed; static u64 aura_layout[2]; @@ -52,7 +53,7 @@ static phys_addr_t aura_boot_dt; /* * Ranges kept out of the image, in page frames. Filled once at late_initcall, - * read by arm64's pfn_is_nosave() for reserved pages. A static table avoids + * read through the arm64 hibernation platform hooks. A static table avoids * register_nosave_region(), which allocates from memblock and is meant for * setup_arch() time. */ @@ -60,11 +61,6 @@ static phys_addr_t aura_boot_dt; static struct { unsigned long start, end; } aura_nosave[AURA_NOSAVE_MAX]; static unsigned int aura_nosave_count; -bool aura_hibernate_layout_fixed(void) -{ - return aura_layout_fixed; -} - /* * Apple silicon Macs run firmware on their coprocessors (storage, secure * enclave, display, always-on processor, system controller, GPU and more). @@ -82,7 +78,7 @@ static bool aura_hibernate_blocked(void) return of_machine_is_compatible("apple,arm-platform") && !aura_layout_fixed; } -bool aura_hibernate_pfn_nosave(unsigned long pfn) +static bool aura_hibernate_pfn_nosave(unsigned long pfn) { unsigned int i; @@ -93,7 +89,7 @@ bool aura_hibernate_pfn_nosave(unsigned long pfn) } /* Whether the ranges kept out of the image cover all of [start, end). */ -bool aura_hibernate_range_nosave(u64 start, u64 end) +static bool aura_hibernate_range_nosave(u64 start, u64 end) { unsigned long pfn = PFN_DOWN(start), last = PFN_UP(end); unsigned int i; @@ -113,6 +109,73 @@ bool aura_hibernate_range_nosave(u64 start, u64 end) return true; } +/* + * The image restores every page to the frame it came from, so the boot that + * resumes it must give the kernel what the boot that wrote it did: on Apple + * machines at EL2 the same CTXR exec window (secondaries only execute inside + * it) and aura's fixed hibernation layout. They are added to the layout cookie + * of arm64 hibernation. + * + * aura's own image moves with the boot object, and its CPU release addresses + * and S2R mailbox with it, inside the range the layout keeps from Linux. The + * restored kernel uses neither address of the boot that wrote the image: its + * CPUs come back from the boot kernel's pen, a CPU started at its reset vector + * again finds the release target the boot kernel wrote (both kernels run at the + * window's fixed address), and the T6040 sleep driver takes this boot's mailbox + * from aura's tree after the restore. What has to match is the spin table's + * shape (the same CPUs, at the same offsets from the mailbox) and that this + * boot keeps all of it out of the image. + */ +static u64 aura_hibernate_layout(u64 h, struct arm64_hibernate_layout *lay) +{ + static const char * const chosen_props[] = { + "aura,hibernate-layout", "aura,hibernate-dt", + "aurora,ctxr-exec-window", + }; + u64 mbox = 0, layout[2], win[2]; + struct device_node *np; + const void *v; + u8 kept; + int i, len; + + np = of_find_node_by_path("/chosen"); + for (i = 0; np && i < ARRAY_SIZE(chosen_props); i++) { + v = of_get_property(np, chosen_props[i], &len); + if (!v) + continue; + h = arm64_hibernate_layout_mix(h, chosen_props[i], strlen(chosen_props[i])); + h = arm64_hibernate_layout_mix(h, v, len); + } + if (np && !of_property_read_u64(np, "aurora,s2r-mailbox", &mbox)) { + lay->have_base = true; + lay->base = mbox; + kept = aura_hibernate_pfn_nosave(PHYS_PFN(mbox)); + h = arm64_hibernate_layout_mix(h, "aurora,s2r-mailbox", + strlen("aurora,s2r-mailbox")); + h = arm64_hibernate_layout_mix(h, &kept, sizeof(kept)); + } + /* aura's layout less the OS window, only where all of it is kept out of the image */ + if (np && !of_property_read_u64_array(np, "aura,hibernate-layout", layout, 2) && + !of_property_read_u64_array(np, "aurora,ctxr-exec-window", win, 2) && + layout[0] <= win[0] && win[0] + win[1] <= layout[1]) { + lay->ex[lay->nex][0] = layout[0]; + lay->ex[lay->nex][1] = win[0]; + if (aura_hibernate_range_nosave(lay->ex[lay->nex][0], lay->ex[lay->nex][1])) + lay->nex++; + lay->ex[lay->nex][0] = win[0] + win[1]; + lay->ex[lay->nex][1] = layout[1]; + if (aura_hibernate_range_nosave(lay->ex[lay->nex][0], lay->ex[lay->nex][1])) + lay->nex++; + } + of_node_put(np); + return h; +} + +static const struct arm64_hibernate_platform aura_hibernate_platform = { + .pfn_nosave = aura_hibernate_pfn_nosave, + .layout = aura_hibernate_layout, +}; + static void __init aura_nosave_add(unsigned long s, unsigned long e) { if (s >= e) @@ -404,6 +467,7 @@ static int __init aura_hibernate_init(void) hibernation_set_platform_check(aura_hibernate_blocked, "coprocessor firmware state is not restored from an image"); + arm64_hibernate_set_platform(&aura_hibernate_platform); /* Only a boot loader that keeps the layout fixed publishes it. */ chosen = of_find_node_by_path("/chosen"); diff --git a/include/linux/soc/apple/aura-hibernate.h b/include/linux/soc/apple/aura-hibernate.h index 7c8b80de307be..729ced7b3c4cb 100644 --- a/include/linux/soc/apple/aura-hibernate.h +++ b/include/linux/soc/apple/aura-hibernate.h @@ -24,14 +24,8 @@ int aura_hibernate_boot_fw_regions(struct device_node *np, struct aura_fw_region int aura_hibernate_reprobe_after_thaw(struct device *dev); #ifdef CONFIG_APPLE_AURA_HIBERNATE -bool aura_hibernate_layout_fixed(void); -bool aura_hibernate_pfn_nosave(unsigned long pfn); -bool aura_hibernate_range_nosave(u64 start, u64 end); int aura_hibernate_boot_chosen_u64(const char *name, u64 *val); #else -static inline bool aura_hibernate_layout_fixed(void) { return false; } -static inline bool aura_hibernate_pfn_nosave(unsigned long pfn) { return false; } -static inline bool aura_hibernate_range_nosave(u64 start, u64 end) { return false; } static inline int aura_hibernate_boot_chosen_u64(const char *name, u64 *val) { return -ENODEV; } #endif From e8f712670c0430d65ec1dc7d1686d5ea9ef84318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:53:10 +0100 Subject: [PATCH 047/269] PM: hibernate: tell the restored kernel whether another boot loaded it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A driver whose device does not survive a boot cannot tell, in the kernel restored from a hibernation image, whether the machine went through one. pm_hibernate_is_restored() is also true after a test_resume, where the kernel that wrote the image loads it itself and nothing rebooted, and drivers fell back on hardware side channels to tell the two apart. The kernel that loads an image now leaves a record for the restored kernel just before the jump: whether it is the kernel that wrote the image, which is the case only when hibernate() loads the image it has just written for test_resume, or one that was started to resume it. The record lives in the no-save section, which the image neither saves nor overwrites, and carries a magic and its own physical address, so a record from a kernel loaded elsewhere, or none, reads as absent. It is the same requirement that in_suspend and other no-save hand-offs already have: both kernels are the same build at the same physical address. The restored kernel copies it where it sets the restored flag, and pm_hibernate_restored_cold() returns 1 when another boot loaded the image, 0 when this kernel did (or was not restored) and -ENODATA when no record was found. The record is cleared when it is read and when a restore fails, and the value has the lifetime of pm_hibernate_is_restored(). The flag says that the machine booted, not that a given device lost its state: a kexec-style resume would read as another boot with firmware that kept running, and a device that needs more than the boot has to check it itself. Nothing uses it yet. Signed-off-by: František Balázsy --- include/linux/suspend.h | 2 ++ kernel/power/hibernate.c | 63 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 3013208a46c30..f76d9d4a24cdb 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -474,9 +474,11 @@ static inline bool pm_hibernation_mode_is_suspend(void) { return false; } #ifdef CONFIG_HIBERNATION bool pm_hibernate_is_restored(void); bool pm_hibernate_is_loading(void); +int pm_hibernate_restored_cold(void); #else static inline bool pm_hibernate_is_restored(void) { return false; } static inline bool pm_hibernate_is_loading(void) { return false; } +static inline int pm_hibernate_restored_cold(void) { return 0; } #endif int arch_resume_nosmt(void); diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 1356560841fd3..4d3b30055fcb4 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -99,6 +99,53 @@ bool pm_hibernate_is_restored(void) } EXPORT_SYMBOL_GPL(pm_hibernate_is_restored); +/* + * What the kernel that loads an image leaves for the kernel restored from it. + * It lives in the no-save section, which the image neither saves nor + * overwrites, so the restored kernel reads what the loading kernel wrote, as + * long as both are the same build loaded at the same physical address (which + * the image's header requires). The record carries its own physical address + * and a magic: a record from a kernel loaded elsewhere, or none, reads as + * absent. + */ +struct hibernate_load_record { + u32 magic; + u32 other_boot; + u64 self_phys; +}; + +#define HIBERNATE_LOAD_MAGIC 0x484c4452 /* "HLDR" */ + +static struct hibernate_load_record hibernate_load_record __nosavedata; + +/* Set while hibernate() loads the image it has just written (test_resume). */ +static bool hibernate_loading_own_image; + +/* Whether the restore came from another boot, see pm_hibernate_restored_cold(). */ +static int hibernate_restored_cold = -ENODATA; + +/** + * pm_hibernate_restored_cold - Whether this kernel was restored by another boot. + * + * Meaningful once pm_hibernate_is_restored() is true, under the same lifetime. + * The kernel that loads an image tells the kernel restored from it whether it + * is the kernel that wrote the image (a test_resume load: the machine did not + * boot) or one that was started to resume it (the boot loader and the + * firmware have been through a boot). Whether a particular device lost its + * state across that boot is for its driver to find out. + * + * Return: 1 if the image was loaded by another boot, 0 if this kernel loaded + * its own image or was not restored, -ENODATA if the record of the loading + * kernel is missing. + */ +int pm_hibernate_restored_cold(void) +{ + if (!READ_ONCE(hibernate_restored)) + return 0; + return READ_ONCE(hibernate_restored_cold); +} +EXPORT_SYMBOL_GPL(pm_hibernate_restored_cold); + /* Set while this kernel loads a hibernation image, see pm_hibernate_is_loading(). */ static bool hibernate_loading; @@ -463,6 +510,12 @@ static int create_image(int platform_mode) if (!in_suspend) { events_check_enabled = false; clear_or_poison_free_pages(); + if (hibernate_load_record.magic == HIBERNATE_LOAD_MAGIC && + hibernate_load_record.self_phys == __pa_symbol(&hibernate_load_record)) + WRITE_ONCE(hibernate_restored_cold, !!hibernate_load_record.other_boot); + else + WRITE_ONCE(hibernate_restored_cold, -ENODATA); + hibernate_load_record.magic = 0; WRITE_ONCE(hibernate_restored, true); } @@ -504,6 +557,7 @@ int hibernation_snapshot(int platform_mode) pm_suspend_clear_flags(); WRITE_ONCE(hibernate_restored, false); + WRITE_ONCE(hibernate_restored_cold, -ENODATA); error = platform_begin(platform_mode); if (error) goto Close; @@ -617,6 +671,9 @@ static int resume_target_kernel(bool platform_mode) save_processor_state(); error = restore_highmem(); if (!error) { + hibernate_load_record.other_boot = !hibernate_loading_own_image; + hibernate_load_record.self_phys = __pa_symbol(&hibernate_load_record); + hibernate_load_record.magic = HIBERNATE_LOAD_MAGIC; error = swsusp_arch_resume(); /* * The code below is only ever reached in case of a failure. @@ -624,6 +681,7 @@ static int resume_target_kernel(bool platform_mode) * swsusp_arch_suspend() was called. */ BUG_ON(!error); + hibernate_load_record.magic = 0; /* * This call to restore_highmem() reverts the changes made by * the previous one. @@ -995,8 +1053,11 @@ int hibernate(void) if (snapshot_test) { pm_pr_dbg("Checking hibernation image\n"); error = swsusp_check(false); - if (!error) + if (!error) { + hibernate_loading_own_image = true; error = load_image_and_restore(); + hibernate_loading_own_image = false; + } } thaw_processes(); From 93b3ce4086c313a00ad25c002edf8db4656eb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:57:27 +0100 Subject: [PATCH 048/269] PM: hibernate: recover devices when no image was created MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When hibernation is abandoned before the image exists, hibernation_snapshot() and create_image() resume the frozen devices with PMSG_RESTORE, the event of a kernel restored from an image: in_suspend is clear in both cases, and only the creating kernel with an image sets it. The cases are a device that fails to freeze, a wakeup event, the pm_test levels devices, platform and core, and a failure in the noirq phase before in_suspend is set. The hardware did not boot in any of them, but the drivers get their restore callbacks, run them for a state they cannot have, and some have to be told by pm_hibernate_is_restored() that nothing happened. Pick the event in one function: in_suspend set keeps what it was (PMSG_THAW, PMSG_RECOVER after an error); in_suspend clear is PMSG_RESTORE only when pm_hibernate_is_restored() says this kernel was restored, and PMSG_RECOVER otherwise, which is what the PM core sends after every other failed freeze (dpm_prepare() failing here, dpm_suspend_start()/end() through resume_event()). The restored flag is reset at the start of every hibernation_snapshot() and set right where the restored kernel leaves swsusp_arch_suspend(), before either resume call. What changes: only the calls of an abandoned hibernation, from the restore callbacks to the thaw callbacks (pm_op() maps PM_EVENT_RECOVER to the thaw callbacks as PM_EVENT_THAW), and, for a device with DPM_FLAG_MAY_SKIP_RESUME, dev_pm_skip_resume() now follows must_resume as it does for every other failed freeze. pm_hibernate_is_recovering() is now true there as well, as its documentation says. Nothing changes for a created image, a restored kernel or a test_resume, nor after a failed power-off in platform mode (PMSG_RESTORE from hibernation_platform_enter(); only ACPI sets platform hibernation ops). For the Apple drivers the abandoned case was already a thaw: every restore callback that checks the flag returns or forwards to its thaw (macsmc, the RTKit helper, apple-dart, sn201202x, DCP, NVMe, PCIe, cs42l84) or does nothing where there is no thaw (AOP, t6040 USB2 PHY, RTC, GPIO and SPMI early restores); PCIe's restore_noirq and thaw_noirq agree on the ports. So they now get the thaw directly. The checks in those callbacks stay for now. Signed-off-by: František Balázsy --- kernel/power/hibernate.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index 4d3b30055fcb4..f771766758f0f 100644 --- a/kernel/power/hibernate.c +++ b/kernel/power/hibernate.c @@ -86,12 +86,12 @@ static bool hibernate_restored; /** * pm_hibernate_is_restored - Whether this kernel was just restored from an image. * - * Devices get PMSG_RESTORE both in the kernel restored from a hibernation image - * and in the kernel that hibernated when hibernation is aborted before the image - * is created (a device failed to freeze, a wakeup event). Only in the first case - * did the hardware go through a boot. True from the restore until the next - * hibernation starts (hibernation_snapshot()); meant for the restore callbacks - * and the PM_POST_HIBERNATION notifiers. + * Devices get PMSG_RESTORE in the kernel restored from a hibernation image, + * whose hardware went through a boot, and PMSG_RECOVER in the kernel that + * hibernated when hibernation is aborted before the image is created (a device + * failed to freeze, a wakeup event). True from the restore until the next + * hibernation starts (hibernation_snapshot()); meant for code that runs after + * the restore, such as the PM_POST_HIBERNATION notifiers. */ bool pm_hibernate_is_restored(void) { @@ -457,6 +457,25 @@ __weak int arch_resume_nosmt(void) return 0; } +/** + * hibernate_resume_event - Pick the event that resumes the devices frozen for an image. + * @error: Result of the image creation so far. + * + * Called once the image is created, restored or given up on. in_suspend is + * set only in the kernel that created the image, so a clear one means either + * that this kernel was restored from an image (devices get PMSG_RESTORE, + * their hardware went through a boot) or that the hibernation was aborted + * before the image existed (a device failed to freeze, a wakeup event, a + * pm_test level): the devices were only frozen, and the right event is + * PMSG_RECOVER, as after any other failed freeze. + */ +static pm_message_t hibernate_resume_event(int error) +{ + if (in_suspend) + return error ? PMSG_RECOVER : PMSG_THAW; + return pm_hibernate_is_restored() ? PMSG_RESTORE : PMSG_RECOVER; +} + /** * create_image - Create a hibernation image. * @platform_mode: Whether or not to use the platform driver. @@ -538,8 +557,7 @@ static int create_image(int platform_mode) Platform_finish: platform_finish(platform_mode); - dpm_resume_start(in_suspend ? - (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); + dpm_resume_start(hibernate_resume_event(error)); return error; } @@ -605,7 +623,7 @@ int hibernation_snapshot(int platform_mode) if (error || !in_suspend) swsusp_free(); - msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; + msg = hibernate_resume_event(error); dpm_resume(msg); if (error || !in_suspend) From cf056ccfcdc33fa9e69345562428d2aeebb5c594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:58:52 +0100 Subject: [PATCH 049/269] soc: apple: rtkit: drop the restore callbacks' checks for an abandoned hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/rtkit-helper.c part of "soc: apple: drop the restore callbacks' checks for an abandoned hibernation", which goes with the hibernation core changes; the rest of that commit is with the sleep controller changes. An abandoned hibernation resumed its devices with PMSG_RESTORE, so every restore callback that must not act on a state it never lost began with a pm_hibernate_is_restored() check, and most forwarded to their thaw. The PM core now sends PMSG_RECOVER there, which reaches the thaw callbacks, so the check and the forward go from macsmc, the RTKit helper, apple-dart, the sn201202x Type-C controller, the SPMI, GPIO and NVMe restore paths, both PCIe restore callbacks, the DCP and AOP restores, the t6040 USB2 PHY's two and the RTC; cs42l84's restore passes that the codec lost its state. Each callback runs for a restored kernel exactly as before. For an abandoned hibernation the old path was already the thaw or nothing, and the thaw now runs on its own: macsmc, the RTKit helper, apple-dart, sn201202x, NVMe and cs42l84 forwarded to the same function (cs42l84's resume asks pm_resume_via_firmware(), which hibernation_snapshot() has cleared); PCIe's restore_noirq did for a bridge without hibernation tunnelling what its thaw_noirq does, and the same call with it; the AOP, t6040, RTC, GPIO and SPMI restores did nothing and have no thaw. DCP keeps its power-off-failed branch (PMSG_RESTORE after a failed platform power-off, which only a platform with hibernation ops reaches) and loses the abandoned one, whose only other effect was a log line. One path changes: macsmc's thaw, after the SMC was handed over and the image could not be entered, calls apple_smc_restore() to start a new session, and its own check sent that back to the thaw, which did nothing. It now starts the session as the comment says it should. The flag keeps its other users, which are not restore callbacks: the AIC syscore resume, the dockchannel receive interrupt, the SEP's PM notifier. Signed-off-by: František Balázsy --- drivers/soc/apple/rtkit-helper.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index 52ea34080b7f5..8c3d6d2f313d9 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -751,10 +751,6 @@ static int apple_rtkit_helper_restore(struct device *dev) struct apple_rtkit *old, *rtk; int count, live; - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return apple_rtkit_helper_thaw(dev); - if (!helper->adopted) return apple_rtkit_helper_restore_own(helper); From af65ee2070dab551e154b0035772f68fbe2c4a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:02 +0100 Subject: [PATCH 050/269] rust: tell drivers a restored image from a failed one, and allow a platform-fixed layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rust/helpers/suspend.c part of "PM: hibernate: tell drivers a restored image from a failed one, and allow a platform-fixed layout", which goes with the sleep controller changes; the rest of that commit is with the hibernation core changes. Drivers that hand a coprocessor over across hibernation need to know, in their restore callbacks, whether the image was really restored or the PM core is unwinding an aborted hibernation. Add pm_hibernate_is_restored() (with a Rust helper) and let a boot chain that keeps the memory layout fixed (aura hib=1) admit hibernation where the platform would otherwise be refused. Signed-off-by: František Balázsy --- rust/helpers/suspend.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/helpers/suspend.c b/rust/helpers/suspend.c index a616907ecc0b5..5f92364a92a2d 100644 --- a/rust/helpers/suspend.c +++ b/rust/helpers/suspend.c @@ -11,3 +11,9 @@ __rust_helper bool rust_helper_pm_resume_via_firmware(void) { return pm_resume_via_firmware(); } + +/* An inline without CONFIG_HIBERNATION, so Rust reaches it here. */ +__rust_helper bool rust_helper_pm_hibernate_is_restored(void) +{ + return pm_hibernate_is_restored(); +} From 84ed8d7c46351cd08758d62517a6a532bb682e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:03 +0100 Subject: [PATCH 051/269] soc: apple: T6040 suspend-to-RAM controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit t6040-sleep drives a real suspend-to-RAM on the J616s at EL2: the order of the PMGR quiesce and commit (CPU complexes, power gates, axi2af bridges, APCIe, DCS, MasterMini S2R timer) on entry, and of its wake restore (CPU complexes, SOC-East retention, axi2af re-arm, APCIe in gate order, GP PCIe port clocks and PERST, the AOP island) before syscore resume. Deep sleep stays opt-in (allow_entry); the other defaults are the set the hardware runs were made with. Still carries bring-up instrumentation (PMU breadcrumb marks, records and knobs) to be removed before this leaves WIP. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 42 + drivers/soc/apple/Makefile | 5 +- drivers/soc/apple/t6040-sleep.c | 6444 +++++++++++++++++++++++++++++++ include/linux/soc/apple/sleep.h | 61 +- rust/helpers/suspend.c | 7 + 5 files changed, 6553 insertions(+), 6 deletions(-) create mode 100644 drivers/soc/apple/t6040-sleep.c diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 9225b054e7cde..596bb011956dd 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -120,6 +120,34 @@ config APPLE_SOC_SLEEP_KUNIT_TEST If unsure, say N. +config APPLE_T6040_SLEEP + bool "Apple T6040 system sleep (refusal-first)" + depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) + depends on HOTPLUG_CPU && SUSPEND + depends on !APPLE_SOC_SLEEP + select MFD_SYSCON + select APPLE_TUNABLE + help + The T6040 (M4 Pro) suspend-to-RAM controller, still a separate + driver next to APPLE_SOC_SLEEP until the T6040 becomes match data + there. It registers a suspend handler that + enumerates every blocker (the untraced SoC power-down sequence, the + unrun wake path, the live state: EL2, the boot object's return path, + the CPUs that can park, the display power states the platform checks before + it sleeps) and refuses, so a suspend request prints the blockers and + enters nothing. + + It keeps the coprocessor and enclave registry for the T6040, so + the drivers that leave their last stop to the sleep's commit (ANS, + the PMP, the SEP) can register, and offers dry runs: with + t6040_sleep.dry_run=2 every driver's firmware-driven sleep step that + can be taken back runs, the CPUs park and are held, and the sleep is + refused at the commit point and resumed warm. The real commit is + compiled but unreachable while any blocker remains. + + It owns the same registry as APPLE_SOC_SLEEP, so a kernel has one + of the two. + config APPLE_RTKIT tristate "Apple RTKit co-processor IPC protocol" depends on APPLE_MAILBOX @@ -203,6 +231,8 @@ config APPLE_SEP depends on TRUSTED_KEYS depends on CRYPTO depends on MFD_MACSMC || !MFD_MACSMC + # apple_pmp_cold_restarts(): a resume from disk on a cold boot. + depends on APPLE_PMP || !APPLE_PMP select CRYPTO_HMAC select CRYPTO_SHA256 select CRYPTO_AES @@ -320,6 +350,18 @@ config APPLE_CLPC Say 'y' or 'm' here if you have an Apple SoC with a PMC. +config APPLE_AURA_HIBERNATE + bool "Hibernation under an aura boot object" + depends on ARCH_APPLE && HIBERNATION && OF_EARLY_FLATTREE + default y + help + Under aura, the boot loader and the firmware keep memory alive + below Linux's allocations, and on Apple M4 Pro boots the boot + firmware places it at a different address every time. With aura's fixed layout + (/chosen/aura,hibernate-layout) this keeps every reserved range out + of the hibernation image; without it, hibernation and resume are + refused instead of overwriting the running boot loader. + config APPLE_DISPLAY_GATE bool "Apple internal display enable gate" depends on ARCH_APPLE && OF_DYNAMIC && OF_OVERLAY diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 5065bb7a1324a..706a9c67a0d84 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -15,9 +15,10 @@ obj-$(CONFIG_APPLE_CLUSTER_PWRGATE) += apple-cluster-pwrgate.o obj-$(CONFIG_APPLE_SOC_SLEEP) += apple-sleep.o apple-sleep-y = sleep.o +obj-$(CONFIG_APPLE_T6040_SLEEP) += t6040-sleep.o obj-$(CONFIG_APPLE_RTKIT) += apple-rtkit.o -apple-rtkit-y = rtkit.o rtkit-crashlog.o +apple-rtkit-y = rtkit.o rtkit-crashlog.o rtkit-hib-handoff.o obj-$(CONFIG_APPLE_RTKIT_HELPER) += apple-rtkit-helper.o apple-rtkit-helper-y = rtkit-helper.o @@ -42,6 +43,8 @@ DTC_FLAGS_t6030-gate-test := -Wno-interrupts_property -Wno-chosen_node_is_root \ DTC_FLAGS_t8122-gate-test := -Wno-interrupts_property -Wno-chosen_node_is_root DTC_FLAGS_t6041-gate-test := -Wno-unit_address_vs_reg -Wno-avoid_unnecessary_addr_size +obj-$(CONFIG_APPLE_AURA_HIBERNATE) += aura-hibernate.o + obj-$(CONFIG_APPLE_TUNABLE) += apple-tunable.o apple-tunable-y = tunable.o diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c new file mode 100644 index 0000000000000..84dbefc31245a --- /dev/null +++ b/drivers/soc/apple/t6040-sleep.c @@ -0,0 +1,6444 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple T6040 (M4 Pro) system sleep: refusal-first controller. + * + * This is the T6040 port of aurora-silicon/linux #1208 (T8140 deep sleep at + * EL2). It does not take the SoC down. #1208's T8140 driver follows the "last + * 14 ms" power-down micro-sequence measured on a J700; that + * measurement has not been made for the T6040, so the exact commit sequence (DCS + * ladder, MCC lanes, PMC voters, CLLT, DVFM, the the platform on/off power-word lists, + * the wake order) is UNKNOWN here. + * + * What it does: + * + * - It keeps the debugfs preflight of the display power states the platform checks + * before it sleeps (device-check-ps-b4-sleep). + * - It keeps #1208's coprocessor and enclave registry (sleep.h) for the T6040: + * a driver that can only stop its coprocessor once the sleep is certain + * (ANS, the PMP, the SEP's sleep request) registers here, readies it in its + * own suspend and leaves the last stop to the commit, after every refusal. + * A refused sleep then finds it running. + * - Its suspend_ops .begin computes the blocker list. By default a + * `echo mem > /sys/power/state` prints every blocker and returns an error + * before any device suspends. + * - Dry runs (t6040_sleep.dry_run, also writable at runtime): + * 1: the ordinary device suspend and CPU parking, the firmware never told. + * 2: pm_suspend_via_firmware() is set, so every driver's firmware-driven + * sleep step that can be taken back runs (apple_sleep_power_loss() is + * false, so what only a power cycle restores is left out), the CPUs + * park and answer the commit's hold, the commit point's checks run, + * and the sleep is refused there and resumed warm. + * 3: 2, plus the last steps that can still be taken back (the SMC's sleep + * commit, each coprocessor's last check, such as ANS's check of the + * shutdown notification it sent at its suspend). + * The AON watchdog's WD2 is armed from .begin to .end of a dry run 2 or 3 + * (t6040_sleep.wd2_secs, 0 to leave it off). + * - The real commit (t6040_sleep_commit) is compiled but unreachable: it runs + * only when no blocker remains and t6040_sleep.allow_entry=1, and the + * missing-data blockers never clear in this kernel. + * + * Why this file and not a generalisation of t8140-sleep.c: the port keeps + * #1208's T8140 behaviour byte-identical, and the per-SoC match data should be + * built once the T6040 commit and wake have been measured. Until then the + * registry lives here as well, with the same contract, and a kernel builds one + * of the two controllers. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PS_TARGET GENMASK(3, 0) +#define PS_ACTUAL GENMASK(7, 4) +#define PS_FLAGS GENMASK(9, 8) +#define PS_AUTO_ENABLE BIT(28) +#define PS_OFF_TIMEOUT_US 5000 +#define PS_STRIDE 8 +#define PS_MAX_PER_GROUP 16 + +/* + * ANS's storage PCIe block: the TWO ST lanes (apcie_st0/apcie_st1) and their + * shared PHY (T8140 has a single lane). Taken down at the commit + * and raised on wake. The power words sit in SOC-East (t604x-pmgr.dtsi + * offsets, same bank as ps_ans); the control windows (axi2af/cio3pll/clkgen/ + * phy-block/phy-ip/rc) and the PLL/lane tunables are NOT in this SoC's static + * description and are published by the boot object (aura) under the apple,apcie + * node, matching aurora #1208. The block-internal offsets below are T8140's: + * the same Apple PCIe IP, expected identical. + */ +#define PS_ANS 0x370 /* SOC-East: ps_ans */ +#define PS_APCIE_ST0 0x108 /* SOC-East: ps_apcie_st0 */ +#define PS_APCIE_ST1 0x110 /* SOC-East: ps_apcie_st1 */ +#define PS_APCIE_SYS_ST0 0x380 /* SOC-East: ps_apcie_sys_st0 */ +#define PS_APCIE_SYS_ST1 0x388 /* SOC-East: ps_apcie_sys_st1 */ +#define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ +#define PS_BUSY BIT(11) /* polled clear by the A2D engine after each request */ + +#define APCIE_AXI_EN0 0x104 +#define APCIE_AXI_EN1 0x108 +#define APCIE_CLK_CTRL 0x0 +#define APCIE_CLK_BUSY BIT(31) +#define APCIE_CLK_RUN BIT(5) +#define APCIE_CLK_OFF BIT(1) +#define APCIE_CLK_OFF2 BIT(2) +#define APCIE_CLK_START 0x4 +#define APCIE_CLK_START_GO BIT(31) +#define APCIE_PHY_COMMON 0x4000 +#define APCIE_PHY_CTRL 0x8000 +#define APCIE_PHY_CTRL_CLK0REQ BIT(0) +#define APCIE_PHY_CTRL_CLK1REQ BIT(1) +#define APCIE_PHY_CTRL_CLK0ACK BIT(2) +#define APCIE_PHY_CTRL_CLK1ACK BIT(3) +#define APCIE_PHY_CTRL_RESET BIT(4) +#define APCIE_PHY_CTRL_UP BIT(27) +#define APCIE_PHY_RUN 0x8004 +#define APCIE_PHY_RUN_EN BIT(0) +#define APCIE_PHY_RUN_EN2 BIT(4) +#define APCIE_PHY_READY 0x8008 +#define APCIE_PHY_IP_PLL_CTL 0x90 +#define APCIE_PHY_IP_PLL_HOLD BIT(16) +#define APCIE_RC_ZERO 0x04 +#define APCIE_RC_READY_START 0x50 +#define APCIE_RC_READY_CFG 0x54 +#define APCIE_RC_READY_STAT 0x58 +#define APCIE_POLL_US 50000 + +/* + * CPU start block (misc-cores-offset 0x88000, the offset aura uses for + * T6031). cpu-start = pmgr reg0 (0x502800000) + + * 0x88000 = 0x502888000. Register semantics are the same as T8140's. + */ +#define CPU_START_STOP 0x0 +#define CPU_START_SYS 0x4 +#define CPU_START_CLUSTER(n) (0x8 + (n) * 4) + +/* + * cluster-ctl (pmgr reg0 + cluster-ctl-offset 0x20000 = 0x502820000). Measured + * on the J616s: the three CPU complexes are at E +0x0, P0 +0x4, + * P1 +0x8; the platform writes the low nibble 0 to power one down + * and raises it to 0xf to power one up, with +0xc the status/poll word. Three + * complexes on J616s (T8140 had E/P/AVE). + */ +#define CLUSTER_CTL(n) ((n) * 4) +#define CLUSTER_CTL_NR 5 /* E, P0, P1 and two more complexes (+0xc, +0x10) */ +#define CLUSTER_CTL_FIRST_WAIT 3 /* the platform waits past the third: the last two are polled */ +#define CLUSTER_CTL_BUSY BIT(4) +#define CLUSTER_CTL_NIBBLE GENMASK(3, 0) +#define CLUSTER_CTL_ON 0xf +#define CLUSTER_TIMEOUT_US 50000 + +/* + * SOC-PwrGate (pmgr reg0x26 = 0x50282c000). Retention + * bits at +0x0/+0x40 and the three CPM voters at +0x100/+0x108/+0x110, each + * BIT31. The wake restore re-asserts them; no pwrgate write was seen + * in the entry (the voters read back 0x80000000, a no-op), so entry + * leaves them alone. + */ +#define PWRGATE_ON 0x80000000 +#define PWRGATE_WAKE_0 0x000 +#define PWRGATE_WAKE_1 0x040 +#define PWRGATE_CPM_0 0x100 +#define PWRGATE_CPM_1 0x108 +#define PWRGATE_CPM_2 0x110 + +/* + * SOC-East (pmgr reg2 = 0x502280000). The SEP/retention word at +0xc00: on + * wake the platform clears DEV_DISABLE (BIT10) and then polls PARENT_MISSING (BIT11) + * clear before going on. Also holds several + * of the axi2af domains' power words. + */ +#define SOC_EAST_RETENTION 0xc00 +#define SOC_EAST_DEV_DISABLE BIT(10) +#define SOC_EAST_PARENT_MISSING BIT(11) +#define SOC_EAST_TIMEOUT_US 5000 + +/* + * ClkGen SOC (pmgr reg3 = 0x500000000). The clk-mux park words + * (0x500038050/054/060/064) have bits [27:24] cleared at quiesce (observed + * 0xa5100000 / 0xa7100000 -> 0xa0100000), with BIT31 set, each polled until + * BIT30 clears. This is the clock quiesce, NOT measured on the J616s: + * the one measured entry did not cover the clkgen block. Wake restores + * the saved words (the clock restore is otherwise a NOP). + */ +#define CLKGEN_MUX_SET 0x80000000 +#define CLKGEN_MUX_MASK 0xf0ffffff +#define CLKGEN_MUX_WORDS 4 +#define CLKGEN_MUX_PENDING BIT(30) +#define CLK_QUIESCE_TIMEOUT_US 1000 + +/* + * The clock quiesce, whole: two SOC-East + * words lose BIT28 and are polled until [7:4] reads their [3:0], then the four + * clk-mux words, in this order, get [27:24] := 0 with BIT31 and are polled until + * BIT30 clears. Nothing else (no +0x70000 write). + */ +#define SOC_EAST_CLK_A 0x120 +#define SOC_EAST_CLK_B 0x130 +#define SOC_EAST_CLK_AUTO BIT(28) + +/* + * The keep-alive enables: 32 words in PMGR SOC + * from +0x1dc000 and 32 in SOC-East from +0x50000, stride 0x44, BIT0 the keep-alive. + * The quiesce clears them right before the S2R timer (unless the boot-arg + * pmgr-disable-ka); its wake restore sets them again. + */ +#define T6040_KA_SOC_PA 0x5029dc000ULL +#define T6040_KA_EAST_PA 0x5022d0000ULL +#define KA_WORDS 32 +#define KA_STRIDE 0x44 +#define KA_SIZE (KA_WORDS * KA_STRIDE) + +/* + * The SMC's AP watchdog deadline (the watchdog timer's reg[2] + 8): + * The platform writes 0 when it enables the AON watchdog for a reconfig, + * right after arming the AON WD2 (measured on the J616s: 0x508330008 := 0). + */ +#define T6040_SMC_WDT_DEADLINE_PA 0x508330008ULL + +/* + * The AGX residency gate at the top of the PMGR quiesce: unless + * MiniPMGR +0x75010 has both [19:18] set (it read 0 on the J616s), the GPU's AFR power + * word (pmgr_gfx 0x288e80000 + 0x100, ADT power-device AFR) is lowered when its + * target is not 0: [19:16] := 4 and polled, [7:4] polled to 4, then the word := 0 + * and [7:4] polled to 0. + */ +#define T6040_AGX_GATE_PA 0x508375010ULL + +/* + * The hardware steps of the generic PMGR quiesce that run on J616s before + * its T6041 part (measured and the ADT). In order: + * - the ACC quiesce of c = E, P0, P1: APSC off, ACC +0xe20020 BIT23 := 1 (64-bit), + * poll BIT7 clear (CPM gating after it reads back already set: not written); + * - features off: cpu-fixed-freq-pll-relock, BIT42 := 0 in the E and P0 + * ACC words; then the TVMs disp2, afr, dcs (two), gfx (two), disp, soc: control + * BIT0 := 0, status polled until BIT1 clears; + * - the PMC performance state quiesce: the AP voter agent (3) drops its + * DCS floor vote, PMC_DVFM_IF + 3 * 0x4000 [3:0] := 0, pending +4 BIT3 polled + * (the SOC domain has no AP voter map entry here: no write). + * The platform polls the APSC and TVM words without a timeout; here every poll is bounded + * and a timeout sets a bit of the commit's 0x5d mark. + */ +static const u64 t6040_acc_apsc_pa[] = { 0x210e20020ULL, 0x211e20020ULL, 0x212e20020ULL }; +#define ACC_APSC_DISABLE BIT_ULL(23) +#define ACC_APSC_BUSY BIT_ULL(7) +#define ACC_PLL_RELOCK BIT_ULL(42) +#define ACC_PLL_RELOCK_COMPLEXES 2 + +static const struct { + u64 ctl; + u64 status; +} t6040_tvm[] = { + { 0x502920000ULL, 0x502920250ULL }, /* disp2-tvm */ + { 0x288e95000ULL, 0x288e953e0ULL }, /* afr-tvm */ + { 0x502908000ULL, 0x502908250ULL }, /* dcs-tvm */ + { 0x502910000ULL, 0x502910250ULL }, + { 0x288e94000ULL, 0x288e943e0ULL }, /* gfx-tvm */ + { 0x288e94400ULL, 0x288e947e0ULL }, + { 0x502918000ULL, 0x502918250ULL }, /* disp-tvm */ + { 0x502900000ULL, 0x502900250ULL }, /* soc-tvm */ +}; +#define TVM_ENABLE BIT(0) +#define TVM_BUSY BIT(1) +#define T6040_PMC_AP_FLOOR_PA 0x50480c000ULL + +/* + * As the platform does (measured on the J616s where marked), a display pipe's PMC votes go + * before any of its power words: boost 0x5048ac028 BIT(p), DVMR 0x5048a4000 BIT(p) with + * the ack polled at +0x20 BIT0 and 0x5048a9c14 := popcount(DVMR) (measured), BWR 0x5048ac020 + * BIT(p ? p + 2 : 0); the cluster rail 0x5048a9c18 BIT2 (DISP_INT) or BIT3 (DISP_EXT, last + * pipe only) with the ack at 0x5048a9c1c (measured). p: 0 internal, 1 DCPEXT0, 2 DCPEXT1. + */ +#define T6040_PMC_DVMR_PA 0x5048a4000ULL +#define PMC_DVMR_ACK 0x20 +#define T6040_PMC_BWR_PA 0x5048ac020ULL +#define PMC_BOOST 0x8 +#define PMC_DCP_COUNT 0x0 /* 0x5048a9c14 */ +#define PMC_RAIL 0x4 /* 0x5048a9c18 */ +#define PMC_RAIL_ACK 0x8 /* 0x5048a9c1c */ +#define PMC_RAIL_INT BIT(2) +#define PMC_RAIL_EXT BIT(3) +#define PMC_TIMEOUT_US 8000 + +/* + * The DCPEXT power words in SOC (soc_ps), CPU then FE then SYS, the values + * (measured: CPU 0x1f0003ff -> 0x0f0000ff -> 0x0f0003f0, FE 0x3ff -> 0x3f0, SYS -> 0x3f0); the + * SYS word after its axi2af bridge bits and its pwrgate pair (A := 3, B := 0) in the + * SOC pwrgate block's second page. + */ +#define T6040_DISPEXT_PWRGATE_PA 0x50282d000ULL + +/* + * The display coprocessors' ASC CPU_CONTROL (+0x44): DCP, DCPEXT0, DCPEXT1. Upstream + * T8140 (#1208, working S2R) writes it 0 twice right before the display CPU word goes + * down; RUN and bit5 alone, cleared at the device suspend, leave the word refusing writes. + */ +static const u64 t6040_dcp_asc_ctl_pa[3] = { 0x402e00044ULL, 0x3a2e00044ULL, 0x602e00044ULL }; + +/* + * The PMP's PTD (pmp-report 0x5003c0000, T6041 layout as drivers/pmdomain/apple/ + * pmp-report.c): SOC-DEV-PS-REQ read +0x1e00, written +0x10f00, SOC-DEV-PS-ACK +0x1e80, + * 64-bit. DCPEXT0/1 are requests 17/18 (DT report@11/@12, withdrawn by that driver at + * suspend_noirq); the internal display is 16, assumed from the pattern. The platform has + * the PMP release a display's request before its power words change. + */ +#define T6040_PTD_PA 0x5003c0000ULL +#define PTD_REQ_READ 0x1e00 +#define PTD_REQ_WRITE 0x10f00 +#define PTD_ACK 0x1e80 +#define PTD_DISP_INT 16 +static const struct { + u32 sys, fe, cpu, pg_a, pg_b; + int axi2af; +} t6040_dispext[2] = { + { 0x340, 0x348, 0x350, 0x234, 0x258, 5 }, + { 0x490, 0x498, 0x4a0, 0x6b4, 0x6d8, 6 }, +}; +#define PMC_FLOOR_DCS GENMASK(3, 0) +#define PMC_PENDING 0x4 +#define PMC_PENDING_AP BIT(3) +#define QUIESCE_TIMEOUT_US 2000 + +/* The 0x5d mark's timeout bits. */ +#define QTO_APSC(c) BIT(c) +#define QTO_TVM(i) BIT(3 + (i)) +#define QTO_PMC BIT(11) +#define QTO_CLUSTER(i) BIT(12 + (i) - CLUSTER_CTL_FIRST_WAIT) +#define QTO_EAST_CLK(i) BIT(14 + (i)) +#define QTO_MUX(i) BIT(16 + (i)) +#define AGX_GATE_RESIDENT GENMASK(19, 18) +#define T6040_AFR_PS_PA 0x288e80100ULL +#define AFR_LEVEL GENMASK(19, 16) +#define AGX_TIMEOUT_US 10000 + +/* + * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000). Same shape as + * #1208's t8140_pmu_write: the reply vector is masked, the command and its data + * are written to +0x4, the reply polled and acknowledged. Measured on the J616s + *: the boot stage is set to 0x50 by an extended write whose command + * word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 + * on both controllers' +0x40. + */ +#define SPMI_STATUS 0x00 +#define SPMI_STATUS_RX_EMPTY BIT(24) +#define SPMI_CMD 0x04 +#define SPMI_REPLY 0x08 +#define SPMI_NOTIFY_ENABLE 0x40 +#define SPMI_NOTIFY_ACK 0x80 +#define SPMI_NOTIFY_REPLY BIT(0) +#define SPMI_CMD_PMU(reg, op) (((u32)(reg) << 16) | BIT(15) | (0xe << 8) | (op)) +#define SPMI_OP_WRITE4 0x33 +#define SPMI_OP_READ4 0x3b +#define PMU_RTC_COMPARE 0x2008 +#define SPMI_CMD_WRITE (BIT(15) | (0xe << 8) | 0x30) /* low 16 bits 0x8e30 */ +#define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 +#define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ +#define PMU_BOOT_STAGE_SLEEP 0x50 +/* The post-timer PMU "go hib (0x7)" replay framing. */ +#define SPMI_GO_HIB_NOTIFY 0x2000000 + +/* + * MasterMini (NUB_MPMGR_AON, RegMap 4 = pmgr reg[0x37] = 0x508900000). + * The S2R timer at +0x4000 (physical 0x508904000) is the commit: writing 1 arms + * the SoC sleep engine. Measured on the J616s: the platform makes exactly this write. + */ +#define MASTERMINI_S2R_TIMER 0x4000 + +/* + * AOP-global (AP controls, RegMap 0x42, window 0x508000000). Mapped from the + * S2R-control word 0x508004000: +0x0 the S2R control (cleared to 0 on wake), + * +0xc..+0x2c the 9 wake-source masks (armed before + * sleep), +0x30..+0x50 the 9 wake-source status words (read on wake). + */ +#define AOP_S2R_CTRL 0x00 +#define AOP_WAKE_MASK(w) (0x0c + (w) * 4) +#define AOP_WAKE_STATUS(w) (0x30 + (w) * 4) +#define AOP_WAKE_WORDS 9 + +/* axi2af fabric-bridge retention re-arm. */ +#define AXI2AF_BRIDGE_104 0x104 +#define AXI2AF_BRIDGE_108 0x108 + +/* The internal display, as the platform drives the PMGR and the DART. */ +#define T6040_DCP_DART_PA 0x402340000ULL +#define T6040_DISP_PWRGATE_PA 0x5022a1000ULL +#define DISP_PWRGATE_A 0x1c /* := 3 before DISP_SYS goes down, 0 after it is up */ +#define DISP_PWRGATE_B 0x60 /* := 0 before DISP_SYS goes down, 1 after it is up */ +#define PS_DISP_FE 0x368 /* soc_east */ +#define PS_DISP_SYS 0x258 /* soc_east */ +#define AXI2AF_DISP_SYS 4 /* t6040_axi2af[] index */ +#define DISP_PS_TIMEOUT_US 8000 + +/* The GP PCIe (t602x port layout, as T8140's radio port): power words in soc_east. */ +#define PS_APCIE_GP 0x100 +#define PS_APCIE_SYS_GP 0x390 +#define AXI2AF_ANS 9 +#define AXI2AF_APCIE_SYS_GP 14 +#define GP_PORT_L2 0x88 +#define GP_PORT_L2_ENTER 0x11 +#define GP_PORT_L2_BUSY BIT(0) +#define GP_PORT_INTSTAT 0x100 +#define GP_PORT_INTMSK 0x104 +#define GP_PORT_INT_LINK GENMASK(7, 4) +#define GP_PORT_PERST_GUARD 0x13c +#define GP_PORT_LINKSTS 0x208 +#define GP_PORT_LINKSTS_UP BIT(0) +#define GP_PORT_APPCLK 0x800 +#define GP_PORT_APPCLK_EN BIT(0) +#define GP_PORT_APPCLK_CGDIS BIT(8) +#define GP_PORT_PERST 0x82c +#define GP_PHY_LINK BIT(31) +#define GP_PHY_EN0 BIT(9) +#define GP_PHY_EN1 BIT(10) +#define GP_PHY_RESET BIT(4) +#define GP_PHY_CLK0REQ BIT(0) +#define GP_PHY_CLK1REQ BIT(1) +#define GP_PHY_CLK0ACK BIT(2) +#define GP_PIN_DATA BIT(0) +#define GP_STOP_US 5000 +#define T6040_PMC_DCP_PA 0x5048a9c14ULL /* count of DCPs powered (+0), second voter (+4); the platform ends at 0/0 */ +#define T6040_AXI2AF_DOMAINS 15 + +/* + * The AON watchdog's WD2, the sleep safety net. ADT wdt reg[0] = 0x50836c000. + * Offsets and the 24 MHz tick follow the platform (its AON watchdog for a reconfig) + * and the ADT (simple-reconfig-wdog-icc-time = 5). Linux's own + * watchdog driver runs WD1 (+0x10..+0x1c), not this one. + */ +#define WDT_WD0_COUNT 0x00 +#define WDT_WD0_BITE 0x04 +#define WDT_WD0_CTL 0x0c +#define WDT_WD1_COUNT 0x10 +#define WDT_WD1_BITE 0x14 +#define WDT_WD1_CTL 0x1c +#define WDT_CTL_RESET_EN BIT(2) +#define WDT_WD2_COUNT 0x20 +#define WDT_WD2_ALARM 0x28 +#define WDT_WD2_CTL 0x2c +#define WDT_TICKS_PER_S 24000000 +/* At the commit, as the reconfig. */ +#define WDT_SLEEP_SECS 5 +/* From .begin to .end: the device suspend and resume, as #1208 does. */ +#define WDT_SUSPEND_SECS 120 +/* ALARM is 32 bits at 24 MHz. */ +#define WDT_MAX_SECS 178U + +/* ASC CPU control, the same in every Apple coprocessor wrapper. */ +#define ASC_CPU_CONTROL 0x44 +#define ASC_CPU_CONTROL_RUN BIT(4) +#define ASC_CPU_STATUS 0x48 + +/* + * The deep-sleep IMPDEF sysreg. The platform on T6040 prepares a core for + * sleep with the same register and bits as + * T8140. Written by every core right before its last WFI; only a reset clears + * it. + */ +#define SYS_APL_DEEP_SLEEP sys_reg(3, 5, 15, 6, 2) +#define DEEP_SLEEP_ENABLE (BIT_ULL(0) | BIT_ULL(63)) +#define SYS_APL_IPI_SR_EL1 sys_reg(3, 5, 15, 1, 1) +#define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) +#define IPI_SR_PENDING BIT(0) + +/* Must match aura's struct aura_s2r_mailbox (aura #117). */ +#define T6040_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ + +struct t6040_s2r_mailbox { + u64 magic; + u64 boot_mpidr; + u64 resume_pa; + u64 resumes; +}; + +struct t6040_sleep_group { + const char *name; + struct regmap *regmap; + u32 first; /* the pipe's CPU */ + u32 last; /* its last sub-block */ +}; + +/* A device the commit point looks at: bound or not, and its own power word. */ +struct t6040_sleep_watch { + const char *compatible; + const char *name; + struct regmap *ps_regmap; + u32 ps_offset; + bool has_ps; + bool bound; +}; + +enum { + T6040_WATCH_SEP, + T6040_WATCH_ANS, + T6040_WATCH_COUNT, +}; + +/* + * ANS's storage-apcie control windows and PLL/lane tunables, mapped from the + * apple,apcie node the boot object publishes (t6040_sleep_map_apcie). Every + * pointer is NULL until a loader fills the node: @valid is false then, and a + * real sleep with ANS stopped refuses. The two ST lanes share this one PHY. + */ +struct t6040_apcie_st { + void __iomem *axi; /* reg-name "axi2af" (shared) */ + void __iomem *cio3pll; /* reg-name "cio3pll" (shared) */ + void __iomem *clkgen; /* reg-name "clkgen" (shared) */ + void __iomem *phy; /* reg-name "phy-block" (shared PHY) */ + void __iomem *phy_ip; /* reg-name "phy-ip" */ + void __iomem *rc; /* reg-name "rc" */ + struct apple_tunable *axi_tunable; /* apple,tunable-axi2af */ + struct apple_tunable *cio3pll_tunable; /* apple,tunable-cio3pll */ + struct apple_tunable *clkgen_tunable; /* apple,tunable-clkgen */ + struct apple_tunable *phy_tunable; /* apple,tunable-phy */ + struct apple_tunable *phy_ip_pll; /* apple,tunable-phy-ip-pll */ + struct apple_tunable *phy_ip_lanes; /* apple,tunable-phy-ip-auspma */ + bool valid; +}; + +struct t6040_sleep { + struct device *dev; + struct t6040_sleep_group *groups; + unsigned int ngroups; + struct dentry *debugfs; + void __iomem *cpu_start; + void __iomem *wdt; + /* The DT's wdt block is the one t6040_resume_stub hardcodes. */ + bool wdt_stub_ok; + /* Bring-up marks: the PMU RTC comparator saved at .enter, put back (+1 s) before the commit. */ + u32 alarm_saved, alarm_hdr; + bool alarm_ok; + /* The commit's and the wake's SoC blocks (see the #defines). Optional. */ + void __iomem *cluster_ctl; + void __iomem *pwrgate; + u32 commit_on_mask; /* axi2af domains still on at the commit (0: none) */ + void __iomem *dcp_dart; /* dart-dcp, for its transaction idle wait */ + void __iomem *disp_pwrgate; /* internal display retention window 0x5022a1000 */ + bool disp_down; /* t6040_disp_off() lowered the internal display */ + void __iomem *pmc_dcp; /* PMS pmc-dcp voter pair 0x5048a9c14/c18 */ + u32 pmc_dcp_saved[2]; + /* The GP PCIe host's two ports (Wi-Fi/BT, SD) for the commit's port stop (T8140's). */ + void __iomem *gp_port[2]; + void __iomem *gp_phy[2]; + void __iomem *gp_pins; + int gp_perst[2]; + bool gp_perst_low[2]; + void __iomem *clkgen; + void __iomem *spmi0; + void __iomem *spmi_a0; + void __iomem *mastermini; + /* Bring-up A2D watch windows (read-only, MCPS/MPMGR A2D steps, iBoot rcfg seq 8/24). */ + void __iomem *a2d_mini, *a2d_mini4, *a2d_mm8, *a2d_pmc, *a2d_blk; + void __iomem *a2d_mm12; /* MasterMini 0x50890c000 (IB2 engine + handshake +0x14) */ + void __iomem *aop_req; /* AOP island power requests 0x5108b8100 (bit 0 DRAM, 1 AP, 2 CPU) */ + void __iomem *mtp_ps; /* MiniPMGR MTP island PS words 0x50830c000..0x060 (bring-up) */ + u32 mtp_left; /* MTP PS words that did not reach ACTUAL 0 at this commit */ + u32 apcie_diag[16]; /* apcie_seq bit 2: 'APCS' + flags + PHY/PS words, PMU 0xa200 */ + void __iomem *atc_aon; /* 0x508320c00: the 4 ATC port blocks of A2D seq 25 */ + void __iomem *dcs[16]; /* DCS(n,k): 0x220200000 + n * 0x2000000 + k * 0x200000 */ + void __iomem *amcc_sem[4]; /* 0x2201c0000 + n * 0x2000000 (+0x28 MCU handshake) */ + void __iomem *pg_main, *pg_east; /* pwrgate 0x50282c000, SOC-East 0x5022a0000 */ + void __iomem *aop_asc; /* AOP ASC 0x510600000 (CPU_CONTROL +0x44) */ + void __iomem *atc_cfg[4]; /* ATCPHY_CFG(n) 0x383200000 + n * 0x8000000 */ + void __iomem *mini_ps; /* 0x508300000: MiniPMGR PS words */ + void __iomem *aop_ps; /* MiniPMGR AOP + audio island PS words 0x508304000..0x158 */ + void __iomem *snap_soc, *snap_east; /* PS snapshot pages 0x502800000 / 0x502280000 */ + void __iomem *pmc_probe_io[9]; /* bring-up PMC probe words */ + u32 aop_left; /* AOP words left on << 8 | first index left on */ + void __iomem *cpm_cfg[3]; /* cluster + 0xe50000 (iBoot idle power-down, bring-up) */ + void __iomem *cpm_impl[3]; /* cluster + 0xe40000 (cpm-impl-reg) */ + void __iomem *cpu4_impl110; /* boot CPU cpu-impl-reg + 0x110 (0x211050110) */ + u32 cpm_to; /* clusters whose CPM state poll timed out at this commit */ + void __iomem *soc_east; + void __iomem *soc_ps; + void __iomem *aop_global; + void __iomem *axi2af_bridge[T6040_AXI2AF_DOMAINS]; + struct t6040_s2r_mailbox *mbox; + u64 mbox_pa; + bool power_ops; + bool wd2_armed; + bool timer_written; /* bring-up observer: the S2R timer is armed */ + u64 siq_cfg; + u64 cntkctl; + /* Saved across the commit, restored on wake. */ + u32 clkgen_mux_saved[CLKGEN_MUX_WORDS]; + bool clkgen_saved; + void __iomem *ka_soc; + void __iomem *ka_east; + u64 ka_on_before; /* keep-alive bits set before the commit cleared them */ + void __iomem *smc_wdt_deadline; + void __iomem *agx_gate; + void __iomem *ane_sys_ps; /* pmgr +0x3c0 */ + void __iomem *ane_ps; /* pmgr_ane: mpm, cpu, td, base */ + void __iomem *acc_apsc[3]; + void __iomem *tvm_ctl[8]; + void __iomem *tvm_status[8]; + void __iomem *pmc_ap_floor; + void __iomem *pmc_dvmr; + void __iomem *pmc_bwr; + void __iomem *dispext_pwrgate; + void __iomem *dcp_asc_ctl[3]; + void __iomem *ptd; + void __iomem *gapf; + u32 quiesce_to; /* timeout bits of this commit's quiesce polls */ + void __iomem *afr_ps; + u32 afr_saved; + u32 cluster_saved[CLUSTER_CTL_NR]; + u32 cluster_after[CLUSTER_CTL_NR]; + u32 wake_mask_saved[AOP_WAKE_WORDS]; + bool wake_mask_saved_valid; + u32 wake_status[AOP_WAKE_WORDS]; + bool returned; + /* ANS's storage ST lanes: windows + whether a commit stopped ANS. */ + struct t6040_apcie_st apcie_st; + bool pcie_ans_stopped; + int ans_off_ret; + int pcie_ans_on_ret; + u32 apcie_wake_step; + u32 gp_phy_saved[2], gp_appclk_saved[2], gp_perst_saved[2]; + bool gp_saved[2]; + u32 gp_port_res; + u32 aop_ps_saved[44]; + bool aop_ps_saved_valid; + struct t6040_sleep_watch watch[T6040_WATCH_COUNT]; +}; + +/* + * 0: refuse at .begin. 1: device suspend and parking, the firmware never told. + * 2: the firmware-driven device suspend short of what only a power cycle + * restores, parking and the commit's hold, refused at the commit point. 3: 2 and + * the last steps that can still be taken back. Read at each suspend's .begin. + */ +static unsigned int dry_run; +module_param(dry_run, uint, 0644); +MODULE_PARM_DESC(dry_run, + "Dry run level: 1 parking only, 2 firmware-driven device sleep refused at the commit point, 3 also the last steps"); + +static bool allow_entry; +module_param(allow_entry, bool, 0444); +MODULE_PARM_DESC(allow_entry, + "Reach the real commit if no blocker remains (none clears with the current unknowns)"); + +/* + * Explicit, deliberate override of the missing-data blockers (t6040_sleep_unknowns) + * for the hardware S2R bring-up: the entry/wake/apcie/dcp code has run on the J616s + * only with this override set, and a real commit can hang or lose devices on + * resume (the AON WD2 is the net). Writable at runtime so it is armed only as the + * last step after the system is quiesced; refuses still apply to the live blockers + * (display/CPU/mailbox) and to allow_entry. Leave 0 for normal operation. + */ +static bool i_know_untested; +module_param(i_know_untested, bool, 0644); +MODULE_PARM_DESC(i_know_untested, + "DANGER: ignore the missing-data blockers for a first HW S2R bring-up (needs allow_entry too)"); + +/* + * The AON WD2 net armed at the commit. The platform arms 5 s for its reconfig; a longer + * net tells "the SoC never slept" (reset at the net) from "it slept and the wake + * failed" (reset at the wake source) on a first bring-up. Never off: 1 to 178. + */ +static unsigned int sleep_wd_secs = 30; +module_param(sleep_wd_secs, uint, 0644); +MODULE_PARM_DESC(sleep_wd_secs, + "AON WD2 net armed at the S2R commit, in seconds (default 5, clamped to 1..178)"); + +static unsigned int wd2_secs = WDT_SUSPEND_SECS; +module_param(wd2_secs, uint, 0644); +MODULE_PARM_DESC(wd2_secs, + "Arm the AON WD2 for this many seconds from .begin to .end of a dry run 2 or 3 (0: off, at most 178)"); + +/* + * After the S2R timer, the platform issues a PMU "go hib (0x7)" SPMI sequence. + * Whether a Linux-at-EL2 driver must issue it, or the MasterMini timer alone + * arms the engine and the PMU sees it over the hardware SPMI link, is unproven. + * Off by default. The commit is unreachable regardless of this. + */ +static bool pmgr_quiesce = true; +module_param(pmgr_quiesce, bool, 0644); +MODULE_PARM_DESC(pmgr_quiesce, + "Run the steps of the generic PMGR quiesce in the commit: APSC off, PLL relock off, TVMs off, PMC DCS floor"); + +/* + * On the J616s the first access to 0x50480c000 (the AP voter agent's floor) hung the + * bus (mark 0x330, reset by the 30 s WD2). The address is unverified on + * hardware; off until it is. + */ +/* + * On the J616s, with the secondaries still spinning (stopped, coherent) until after + * the timer, the complexes +0xc/+0x10 never dropped BIT4 and the engine stalled. On + * The platform every other core is already off (self-disable + deep WFI) when the master + * turns the complexes off. Release the stopped cores to their last WFI at the top + * of the commit and wait for their core PS words to read off. + */ +static bool release_early = true; +module_param(release_early, bool, 0644); +MODULE_PARM_DESC(release_early, + "Release the stopped CPUs to their last WFI at the commit's start and wait until they are off"); + +static bool pmc_floor; +module_param(pmc_floor, bool, 0644); +MODULE_PARM_DESC(pmc_floor, "With pmgr_quiesce: the PMC AP DCS floor vote (its address hung the bus on the J616s)"); + +static bool tvm_gpu = true; +module_param(tvm_gpu, bool, 0644); +MODULE_PARM_DESC(tvm_gpu, "With pmgr_quiesce: also the GPU PMGR's afr/gfx TVMs (0x288e94000..)"); + +/* + * cluster-ctl DISP_INT (+0xc) / DISP_EXT (+0x10): 0 leaves them as they are, 1 writes the + * nibble 0 and waits for BIT4 (the platform), 2 writes without waiting. Their CPU words can't be + * lowered from here, so the slots never finish under 1. + */ +static unsigned int disp_clusters = 1; +module_param(disp_clusters, uint, 0644); +MODULE_PARM_DESC(disp_clusters, + "DISP_INT/DISP_EXT cluster-ctl slots at the commit: 0 untouched, 1 off + wait, 2 off, no wait"); + +/* + * At runtime the PTD request word reads 0x63c00: EACC0/PACC0/PACC1/GFX (10-13) and the + * two DCPEXTs (17, 18, withdrawn by pmp-report at suspend_noirq). Bits set here are + * withdrawn at the commit right before the S2R timer (no ack wait). Bring-up knob. + */ +static unsigned long ptd_withdraw; +module_param(ptd_withdraw, ulong, 0644); +MODULE_PARM_DESC(ptd_withdraw, "PTD SOC-DEV-PS-REQ bits to withdraw right before the S2R timer"); + +static bool post_timer_mask; +module_param(post_timer_mask, bool, 0644); +MODULE_PARM_DESC(post_timer_mask, "Bring-up: the post-timer counter probe's mark carries which cores and ANE words are on"); + +static bool post_timer_ane; +module_param(post_timer_ane, bool, 0644); +MODULE_PARM_DESC(post_timer_ane, "Bring-up: the post-timer counter probe's mark carries the ANE power words instead"); + +static bool post_timer_counter_probe; +module_param(post_timer_counter_probe, bool, 0644); +MODULE_PARM_DESC(post_timer_counter_probe, + "Bring-up: after the S2R timer, test whether CNTPCT still moves (answer by WD2 timing + mark)"); + +static unsigned int post_timer_wd_ms; +module_param(post_timer_wd_ms, uint, 0644); +MODULE_PARM_DESC(post_timer_wd_ms, + "Bring-up: re-arm the AON WD2 to this many ms (max 178000) right after the S2R timer"); + +static unsigned int post_timer_probe_ms; +module_param(post_timer_probe_ms, uint, 0644); +MODULE_PARM_DESC(post_timer_probe_ms, + "Bring-up: after the S2R timer spin this long (max 4 s), then mark 0x50EEPPQQ = cluster-ctl E/P0/P1"); + +static unsigned int timer_delay_ms; +module_param(timer_delay_ms, uint, 0644); +MODULE_PARM_DESC(timer_delay_ms, + "Bring-up: hold this long (max 10 s) right before the S2R timer, mark 0x3e0 first"); + +static bool agx_gate = true; +module_param(agx_gate, bool, 0644); +MODULE_PARM_DESC(agx_gate, + "Lower the GPU's AFR power word in the commit, as the PMGR quiesce does behind its AGX residency gate"); + +static bool keep_alive_off = true; +module_param(keep_alive_off, bool, 0644); +MODULE_PARM_DESC(keep_alive_off, + "Clear the 64 PMGR keep-alive bits right before the S2R timer, as the PMGR quiesce does"); + +static bool replay_pmu_go_hib; +module_param(replay_pmu_go_hib, bool, 0644); +MODULE_PARM_DESC(replay_pmu_go_hib, + "Replay the PMU \"go hib\" SPMI pairs after the S2R timer (necessity unconfirmed)"); + +static struct t6040_sleep *t6040_sleep; + +static void t6040_iounmap(void *p) +{ + iounmap((void __iomem __force *)p); +} + +/* + * Every window here non-posted (nGnRnE), as the platform and aura map the PMGR, PMC and + * coprocessor blocks. No region request: these overlap other drivers. + */ +static void __iomem *t6040_ioremap_np(struct device *dev, phys_addr_t pa, size_t size) +{ + void __iomem *p = ioremap_np(pa, size); + + if (!p || devm_add_action_or_reset(dev, t6040_iounmap, (void __force *)p)) + return NULL; + return p; +} + +/* The dry run level of the suspend in progress, from .begin to .end; 0 otherwise. */ +static unsigned int t6040_sleep_dry; + +bool apple_sleep_power_loss(void) +{ + return pm_suspend_via_firmware() && !READ_ONCE(t6040_sleep_dry); +} +EXPORT_SYMBOL_GPL(apple_sleep_power_loss); + +bool apple_sleep_last_steps(void) +{ + unsigned int dry = READ_ONCE(t6040_sleep_dry); + + return pm_suspend_via_firmware() && (!dry || dry >= 3); +} +EXPORT_SYMBOL_GPL(apple_sleep_last_steps); + +/* + * The coprocessor and enclave registry, with #1208's contract (sleep.h). A + * registered coprocessor that isn't ready refuses the sleep; a ready one keeps + * running until the commit's last stop, after every refusal, so a refused sleep + * finds it as it was. + */ +#define SLEEP_COPROCS 6 + +static struct t6040_sleep_coproc { + struct device *dev; + void __iomem *asc; + bool pcie_ans; + int (*last_check)(void *data); + int (*last_stop)(void *data); + void *data; + bool ready; + bool stopped; + u32 status; + /* No ASC: its last stop takes it whole, power word @ps included, before the others. */ + bool first; + u32 ps; +} t6040_sleep_coprocs[SLEEP_COPROCS]; +static DEFINE_MUTEX(t6040_sleep_coprocs_lock); + +/* The enclave's sleep request, left to the last refusal. Under t6040_sleep_coprocs_lock. */ +static struct { + struct device *dev; + int (*tell)(void *data); + void *data; +} t6040_sleep_enclave; + +static struct t6040_sleep_coproc *t6040_sleep_coproc_find(struct device *dev) +{ + int i; + + for (i = 0; i < SLEEP_COPROCS; i++) + if (t6040_sleep_coprocs[i].dev == dev) + return &t6040_sleep_coprocs[i]; + + return NULL; +} + +/* Below EL2 a monitor owns the last mile, and this code doesn't run it. */ +int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, + int (*last_stop)(void *data), void *data) +{ + struct t6040_sleep_coproc *c; + + if (!is_kernel_in_hyp_mode()) + return -ENODEV; + + guard(mutex)(&t6040_sleep_coprocs_lock); + if (t6040_sleep_coproc_find(dev)) + return -EEXIST; + c = t6040_sleep_coproc_find(NULL); + if (!c) + return -ENOSPC; + c->asc = asc; + c->pcie_ans = pcie_ans; + c->last_check = NULL; + c->last_stop = last_stop; + c->data = data; + c->ready = false; + c->stopped = false; + c->first = false; + c->ps = 0; + c->dev = dev; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_register); + +int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), + void *data) +{ + struct t6040_sleep_coproc *c; + struct device_node *np; + u32 ps = 0; + int ret; + + if (!last_stop) + return -EINVAL; + np = of_parse_phandle(dev->of_node, "power-domains", 0); + if (np) { + of_property_read_u32(np, "reg", &ps); + of_node_put(np); + } + ret = apple_sleep_coproc_register(dev, NULL, false, last_stop, data); + if (ret) + return ret; + + guard(mutex)(&t6040_sleep_coprocs_lock); + c = t6040_sleep_coproc_find(dev); + if (c) { + c->first = true; + c->ps = ps; + } + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_register_first); + +int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)) +{ + struct t6040_sleep_coproc *c; + + guard(mutex)(&t6040_sleep_coprocs_lock); + c = t6040_sleep_coproc_find(dev); + if (!c) + return -ENOENT; + c->last_check = last_check; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_last_check); + +int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), void *data) +{ + if (!is_kernel_in_hyp_mode()) + return -ENODEV; + + guard(mutex)(&t6040_sleep_coprocs_lock); + if (t6040_sleep_enclave.dev) + return -EBUSY; + t6040_sleep_enclave.tell = tell; + t6040_sleep_enclave.data = data; + t6040_sleep_enclave.dev = dev; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_enclave_register); + +void apple_sleep_enclave_unregister(struct device *dev) +{ + guard(mutex)(&t6040_sleep_coprocs_lock); + if (t6040_sleep_enclave.dev == dev) + memset(&t6040_sleep_enclave, 0, sizeof(t6040_sleep_enclave)); +} +EXPORT_SYMBOL_GPL(apple_sleep_enclave_unregister); + +void apple_sleep_coproc_unregister(struct device *dev) +{ + struct t6040_sleep_coproc *c; + + guard(mutex)(&t6040_sleep_coprocs_lock); + c = t6040_sleep_coproc_find(dev); + if (c) + memset(c, 0, sizeof(*c)); +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_unregister); + +void apple_sleep_coproc_set_ready(struct device *dev, bool ready) +{ + struct t6040_sleep_coproc *c; + + guard(mutex)(&t6040_sleep_coprocs_lock); + c = t6040_sleep_coproc_find(dev); + if (!c) + return; + c->ready = ready; + c->stopped = false; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_ready); + +bool apple_sleep_coproc_stopped(struct device *dev) +{ + struct t6040_sleep_coproc *c; + + guard(mutex)(&t6040_sleep_coprocs_lock); + c = t6040_sleep_coproc_find(dev); + return c && c->stopped; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); + +/* + * What still keeps the commit unreachable. The SoC power-down and the + * wake return are IMPLEMENTED from the entry as measured on the J616s and + * from its wake restore, which was not measured (t6040_sleep_commit, + * t6040_sleep_return), but no step + * of a T6040 Linux sleep has run on hardware, and two subsystems still have no + * way back from a real power loss. These are facts about this kernel, not + * runtime state: they never clear here, so the commit stays unreachable until + * tests on hardware retire them. The enclave's + * sleep request and the AIC save/restore are built. + */ +static const char *const t6040_sleep_unknowns[] = { + "U1/U2: the T6040 SoC power-down is implemented from the held entry trace (clusters off, clk-mux park, PMU boot-stage, MasterMini S2R timer) but has never been run on hardware; the PMU go-hib replay's necessity is unconfirmed", + "U3: the T6040 wake return is implemented after the wake restore (complexes on, wake/CPM pwrgates, SOC-East retention, axi2af bridge re-arm, clkgen restore, S2R-ctrl clear) but has never been run; the aura mailbox hand-back is unproven on T6040", + "U4: ANS's two ST-lane PCIe link + shared PHY lower/raise is implemented (t6040_sleep_lower/raise_apcie_st, a two-lane port of #1208) but needs the apple,apcie control windows + PLL/lane tunables published by the boot object; absent here, so a real sleep with ANS stopped still refuses; the GP host's Wi-Fi/SD are left to cold-recover and no HW S2R has run", + "U5: the DCP cold restart after a real power loss is implemented (dcp_desktop_cold_restart, factored from the S4 hibernation path, wired into dcp_t6041_s2r_resume) but the display-domain lower/raise needs the apple,display-power window (absent here), DCPEXT is deferred to ext_dead, and no HW S2R has run", +}; + +#define T6040_SLEEP_DATA_BLOCKERS ARRAY_SIZE(t6040_sleep_unknowns) + +/* + * As in the power-state check, a sub-block is on while its TARGET is not 0. The + * pipe's CPU counts as on while its ACTUAL is not 0: the engine waits for the + * CPU's actual state. A CPU under automatic power gating (AUTO_ENABLE, target 15) + * that reads ACTUAL 0 is gated off by the PMGR and counts as off; taking it out + * of automatic mode raised an SError on T6040, so it is left so. + */ +static bool ignore_display_cpu = true; +module_param(ignore_display_cpu, bool, 0644); +MODULE_PARM_DESC(ignore_display_cpu, + "Let the commit go on with the display pipes' CPU words on (the SoC power-down drops them)"); + +static bool t6040_sleep_ps_on(u32 val, bool cpu) +{ + if (cpu && READ_ONCE(ignore_display_cpu)) + return false; + if (cpu) + return FIELD_GET(PS_ACTUAL, val) || + (FIELD_GET(PS_TARGET, val) && !(val & PS_AUTO_ENABLE)); + return FIELD_GET(PS_TARGET, val); +} + +static unsigned int t6040_sleep_show_group(struct seq_file *s, const struct t6040_sleep_group *g) +{ + unsigned int on = 0; + u32 off; + + for (off = g->first; off <= g->last; off += PS_STRIDE) { + bool cpu = off == g->first; + u32 val; + int ret; + + ret = regmap_read(g->regmap, off, &val); + if (ret) { + /* Unreadable counts as on: the refusal fails closed. */ + if (s) + seq_printf(s, " %-12s +%#05x read failed (%d)\n", g->name, off, + ret); + on++; + continue; + } + if (t6040_sleep_ps_on(val, cpu)) + on++; + if (s) + seq_printf(s, " %-12s +%#05x %s %#010x target %lu actual %lu%s\n", g->name, + off, cpu ? "cpu " : "child", val, FIELD_GET(PS_TARGET, val), + FIELD_GET(PS_ACTUAL, val), + t6040_sleep_ps_on(val, cpu) ? " ON" : ""); + } + + return on; +} + +/* How many secondaries can leave the kernel (park), and the boot ops' name. */ +static unsigned int t6040_sleep_parkable(const char **name) +{ + unsigned int cpu, parkable = 0; + + for_each_possible_cpu(cpu) { + const struct cpu_operations *ops = get_cpu_ops(cpu); + + if (cpu && ops && ops->cpu_die && ops->cpu_disable) + parkable++; + } + if (name) + *name = get_cpu_ops(0) ? get_cpu_ops(0)->name : "?"; + + return parkable; +} + +/* + * The live blockers, recomputed each time: not at EL2, no boot-object return + * path, a CPU that cannot park, or a display pipe still on. Returns the count, + * and lists each when @s is given. @disp_out, if not NULL, gets the subtotal of + * display-pipe blockers -- these clear during device suspend (apple-dcp's + * .suspend stops the DCP and lowers its domains), so .begin excludes them and + * only .enter, after the device suspend, enforces them. + */ +static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct seq_file *s, + unsigned int *disp_out) +{ + /* Mapped and aura's: after a resume from disk, this boot's (t6040_sleep_restore()). */ + bool mailbox = sleep->mbox; + bool el2 = is_kernel_in_hyp_mode(); + unsigned int i, blockers = 0, disp = 0, parkable; + const char *ops_name; + + parkable = t6040_sleep_parkable(&ops_name); + + if (s) { + seq_printf(s, "kernel at EL2: %s\n", el2 ? "yes" : "no"); + seq_printf(s, "boot object return path (/chosen/aurora,s2r-mailbox): %s\n", + mailbox ? "yes" : "no"); + seq_printf(s, "secondaries that can leave the kernel: %u of %u (%s)\n", parkable, + num_possible_cpus() - 1, ops_name); + } + blockers += !el2 + !mailbox + (parkable != num_possible_cpus() - 1); + + if (s) + seq_puts(s, "power states the platform checks before sleep (device-check-ps-b4-sleep):\n"); + for (i = 0; i < sleep->ngroups; i++) { + unsigned int on = t6040_sleep_show_group(s, &sleep->groups[i]); + + if (s) + seq_printf(s, " %-12s %u on\n", sleep->groups[i].name, on); + disp += !!on; + } + + if (disp_out) + *disp_out = disp; + return blockers + disp; +} + +/* The total: live state plus the data this kernel is missing. Zero only if the port is complete. */ +static unsigned int t6040_sleep_total_blockers(struct t6040_sleep *sleep) +{ + unsigned int data = READ_ONCE(i_know_untested) ? 0 : T6040_SLEEP_DATA_BLOCKERS; + + if (!data && T6040_SLEEP_DATA_BLOCKERS) + pr_warn_once("t6040-sleep: i_know_untested set: IGNORING %zu never-run data blockers (U1-U5); a real commit may hang or lose devices on resume\n", + T6040_SLEEP_DATA_BLOCKERS); + return t6040_sleep_live_blockers(sleep, NULL, NULL) + data; +} + +static void t6040_sleep_show_registry(struct seq_file *s) +{ + int i; + + guard(mutex)(&t6040_sleep_coprocs_lock); + seq_puts(s, "coprocessors registered for the commit's last stop:\n"); + for (i = 0; i < SLEEP_COPROCS; i++) { + struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + + if (!c->dev) + continue; + seq_printf(s, " %s%s%s%s%s\n", dev_name(c->dev), c->pcie_ans ? " (ANS)" : "", + c->first ? " (first)" : "", c->last_check ? ", last check" : "", + c->ready ? ", ready" : ""); + } + seq_printf(s, "enclave sleep request left for the last refusal: %s\n", + t6040_sleep_enclave.dev ? dev_name(t6040_sleep_enclave.dev) : "none"); +} + +static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s); + +static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) +{ + struct t6040_sleep *sleep = s->private; + unsigned int live = t6040_sleep_live_blockers(sleep, s, NULL); + unsigned int i; + + t6040_sleep_show_registry(s); + t6040_axi2af_show(sleep, s); + seq_puts(s, "data this kernel is missing for a T6040 commit:\n"); + for (i = 0; i < T6040_SLEEP_DATA_BLOCKERS; i++) + seq_printf(s, " %s\n", t6040_sleep_unknowns[i]); + + seq_printf(s, + "verdict: %u live blocker(s) + %zu missing-data blocker(s); no entry path in this kernel\n", + live, T6040_SLEEP_DATA_BLOCKERS); + seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", + READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, + sleep->power_ops ? "registered" : "not registered"); + if (sleep->cluster_ctl) { + static const char * const nm[] = { "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT" }; + + for (i = 0; i < CLUSTER_CTL_NR; i++) + seq_printf(s, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], + readl(sleep->cluster_ctl + CLUSTER_CTL(i))); + } + if (sleep->gapf) + seq_printf(s, "gapf: base %#x cfg %#x lock %#x, slices dcp(17) %#x dcpext0(29) %#x dcpext1(48) %#x\n", + readl(sleep->gapf), readl(sleep->gapf + 0x20), readl(sleep->gapf + 0x24), + readl(sleep->gapf + 0x540), readl(sleep->gapf + 0x840), + readl(sleep->gapf + 0xd00)); + if (sleep->ptd) + seq_printf(s, "pmp ptd: SOC-DEV-PS-REQ %#llx, SOC-DEV-PS-ACK %#llx, status %#llx\n", + readq(sleep->ptd + PTD_REQ_READ), readq(sleep->ptd + PTD_ACK), + readq(sleep->ptd + 0x10)); + + return 0; +} +DEFINE_SHOW_ATTRIBUTE(t6040_sleep_preflight); + +/* The display groups' on-state, one line per group, no allocation. For the log at the commit point. */ +static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) +{ + unsigned int i, blockers = 0; + + for (i = 0; i < sleep->ngroups; i++) { + const struct t6040_sleep_group *g = &sleep->groups[i]; + u32 off, on_mask = 0, val; + unsigned int bit = 0; + + for (off = g->first; off <= g->last; off += PS_STRIDE, bit++) { + if (regmap_read(g->regmap, off, &val) || + t6040_sleep_ps_on(val, off == g->first)) { + on_mask |= BIT(bit); + pr_info("t6040-sleep: %s +%#x = %#x\n", g->name, off, val); + } + } + pr_info("t6040-sleep: %-8s %s (on mask %#x, cpu first)\n", g->name, + on_mask ? "ON" : "off", on_mask); + blockers += !!on_mask; + } + + return blockers; +} + +/* Log the full blocker list (interrupts may be off: no allocation, pr_* only). */ +static void t6040_sleep_log_blockers(struct t6040_sleep *sleep) +{ + const char *ops_name; + unsigned int i; + + pr_info("t6040-sleep: EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", + is_kernel_in_hyp_mode() ? "yes" : "no", + of_property_present(of_chosen, "aurora,s2r-mailbox") ? "yes" : "no", + t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); + for (i = 0; i < T6040_SLEEP_DATA_BLOCKERS; i++) + pr_info("t6040-sleep: blocker: %s\n", t6040_sleep_unknowns[i]); +} + +/* + * CPU parking power ops. MPIDR Aff1 is the cluster (E=0, P0=1, + * P1=2), Aff0 the core. The start and stop bit is the core's bit in the ADT + * function-enable_core masks, which is its cpu-id: the E cluster has four + * slots (bits 0-3), each P cluster six (P0 bits 4-9, P1 bits 10-15; one slot + * of each is harvested on J616s). Four-core banks (4 * cluster + core) would + * give P1 core 0 the bit of P0 core 4. + */ +static const u8 t6040_cluster_first_bit[] = { 0, 4, 10 }; +static const u8 t6040_cluster_slots[] = { 4, 6, 6 }; + +static unsigned int t6040_cpu_cluster(u64 mpidr) +{ + return MPIDR_AFFINITY_LEVEL(mpidr, 1); +} + +static unsigned int t6040_cpu_core(u64 mpidr) +{ + return MPIDR_AFFINITY_LEVEL(mpidr, 0); +} + +static u32 t6040_cpu_start_bit(u64 mpidr) +{ + unsigned int cluster = t6040_cpu_cluster(mpidr), core = t6040_cpu_core(mpidr); + + if (WARN_ON_ONCE(cluster >= ARRAY_SIZE(t6040_cluster_first_bit) || + core >= t6040_cluster_slots[cluster])) + return 0; + return BIT(t6040_cluster_first_bit[cluster] + core); +} + +/* + * Each core's CoreSight external debug block (ADT cpuN coresight-reg: 0x210010000 + + * cluster * 0x1000000 + core * 0x100000). The platform has every CPU, boot and + * secondary, unlock it (EDLAR) and write EDPRCR := 0 right before its deep WFI, so that + * the debug logic does not prevent power gating. With + * EDPRCR.CORENPDRQ left set, a core only emulates its power-down. + */ +#define T6040_ED_BASE_PA 0x210010000ULL +#define T6040_ED_CLUSTER_STRIDE 0x1000000ULL +#define T6040_ED_CORE_STRIDE 0x100000ULL +#define ED_EDPRCR 0x310 +#define ED_EDLAR 0xfb0 +#define ED_LOCK_ACCESS_KEY 0xc5acce55 +#define T6040_ED_CLUSTERS 3 +#define T6040_ED_CORES 6 + +static void __iomem *t6040_ed[T6040_ED_CLUSTERS][T6040_ED_CORES]; + +static bool ed_power_release = true; +module_param(ed_power_release, bool, 0644); +MODULE_PARM_DESC(ed_power_release, + "Before each core's deep WFI, unlock its external debug and clear EDPRCR"); + +static void t6040_ed_map(struct device *dev) +{ + unsigned int cpu, n = 0; + + for_each_possible_cpu(cpu) { + u64 mpidr = cpu_logical_map(cpu); + unsigned int c = MPIDR_AFFINITY_LEVEL(mpidr, 1), k = MPIDR_AFFINITY_LEVEL(mpidr, 0); + + if (c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES || t6040_ed[c][k]) + continue; + t6040_ed[c][k] = t6040_ioremap_np(dev, T6040_ED_BASE_PA + c * T6040_ED_CLUSTER_STRIDE + + k * T6040_ED_CORE_STRIDE, 0x1000); + n += !!t6040_ed[c][k]; + } + dev_info(dev, "external debug windows mapped for %u cores\n", n); +} + +static void t6040_ed_release(void) +{ + u64 mpidr = read_cpuid_mpidr(); + unsigned int c = MPIDR_AFFINITY_LEVEL(mpidr, 1), k = MPIDR_AFFINITY_LEVEL(mpidr, 0); + void __iomem *ed; + + if (!READ_ONCE(ed_power_release) || c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES) + return; + ed = t6040_ed[c][k]; + if (!ed) + return; + writel_relaxed(ED_LOCK_ACCESS_KEY, ed + ED_EDLAR); + writel_relaxed(0, ed + ED_EDPRCR); +} + +static void t6040_deep_wfi_arm(void) +{ + /* A timer left running ends the WFI and costs power until reset. */ + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntp_ctl_el0); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntv_ctl_el0); + t6040_ed_release(); + sysreg_clear_set_s(SYS_APL_DEEP_SLEEP, 0, DEEP_SLEEP_ENABLE); +} + +/* A core's last instructions: nothing brings it back but a reset. */ +static void __noreturn t6040_deep_wfi(void) +{ + t6040_deep_wfi_arm(); + + while (true) { + dsb(sy); + isb(); + wfi(); + write_sysreg_s(IPI_SR_PENDING, SYS_APL_IPI_SR_EL1); + } +} + +static void t6040_cpu_stop(u64 mpidr) +{ + u32 bit = t6040_cpu_start_bit(mpidr); + + if (t6040_sleep && t6040_sleep->cpu_start && bit) + writel(bit, t6040_sleep->cpu_start + CPU_START_STOP); + dsb(sy); +} + +/* + * Bring-up observer (on the J616s, after the timer only the master core is on, P1 still 0x10; + * the master never WFIs while it probes). One parked core is kept spinning instead of its + * deep WFI; after the master writes the timer and takes its own deep WFI, it samples the + * complexes and the master's core word and keeps the latest in the PMU comparator: + * 0x3PPLLEEM [27:22] P0, [21:16] P1, [15:10] E cluster-ctl bits [5:0], + * [9:6] the master core's ACTUAL, [5:0] sample number (saturates at 63) + * Its own complex can't drop while it spins: put it in P1 to watch E and P0. + */ +static unsigned long observer_mpidr = ULONG_MAX; +module_param(observer_mpidr, ulong, 0644); +MODULE_PARM_DESC(observer_mpidr, + "Bring-up: MPIDR of a parked core that observes the complexes after the S2R timer (ULONG_MAX: none)"); + +static void t6040_mark_nodelay(struct t6040_sleep *s, u32 m) +{ + void __iomem *b = s->spmi0; + u32 i; + + if (!b) + return; + for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) + readl(b + SPMI_REPLY); + writel(SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), b + SPMI_CMD); + writel(m, b + SPMI_CMD); + for (i = 0; i < 1000000; i++) + if (readl(b + SPMI_NOTIFY_ACK) & SPMI_NOTIFY_REPLY) + break; + writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); + if (!(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) + readl(b + SPMI_REPLY); +} + +static u32 t6040_a2d_state(struct t6040_sleep *s); +static u32 t6040_aop_state(struct t6040_sleep *s); + +static bool observer_aop; +module_param(observer_aop, bool, 0644); +MODULE_PARM_DESC(observer_aop, "Bring-up: the observer core marks the AOP power requests (0x1 tag, t6040_aop_state)"); + +static bool observer_a2d; +module_param(observer_a2d, bool, 0644); +MODULE_PARM_DESC(observer_a2d, + "Bring-up: the observer core marks the A2D state (0x2 tag, t6040_a2d_state) instead of the complexes"); + +static void __noreturn t6040_observe(struct t6040_sleep *s) +{ + u32 n = 0, i, e, p0, p1, mc; + + while (!READ_ONCE(s->timer_written)) + cpu_relax(); + for (;;) { + if (READ_ONCE(observer_aop)) { + t6040_mark_nodelay(s, 0x10000000 | t6040_aop_state(s) | (n & 3)); + n++; + for (i = 0; i < 500000; i++) + (void)readl_relaxed(s->cluster_ctl); + continue; + } + if (READ_ONCE(observer_a2d)) { + t6040_mark_nodelay(s, 0x20000000 | t6040_a2d_state(s) | (n & 3)); + n++; + for (i = 0; i < 500000; i++) + (void)readl_relaxed(s->cluster_ctl); + continue; + } + e = readl(s->cluster_ctl + CLUSTER_CTL(0)) & 0x3f; + p0 = readl(s->cluster_ctl + CLUSTER_CTL(1)) & 0x3f; + p1 = readl(s->cluster_ctl + CLUSTER_CTL(2)) & 0x3f; + mc = s->soc_ps ? FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 8 * 4)) : 0xf; + t6040_mark_nodelay(s, 0x30000000 | p0 << 22 | p1 << 16 | e << 10 | mc << 6 | + min(n, 63U)); + n++; + for (i = 0; i < 500000; i++) + (void)readl_relaxed(s->cluster_ctl); + } +} + +static void t6040_cpu_off(u64 mpidr) +{ + struct t6040_sleep *s = t6040_sleep; + + if (s && s->cluster_ctl && READ_ONCE(observer_mpidr) == (mpidr & MPIDR_HWID_BITMASK)) + t6040_observe(s); + t6040_deep_wfi(); +} + +static int t6040_cpu_on(u64 mpidr) +{ + struct t6040_sleep *s = t6040_sleep; + unsigned int cluster = t6040_cpu_cluster(mpidr); + u32 bit = t6040_cpu_start_bit(mpidr); + + if (!s || !s->cpu_start) + return -ENODEV; + if (!bit) + return -EINVAL; + writel(bit, s->cpu_start + CPU_START_SYS); + writel(BIT(t6040_cpu_core(mpidr)), s->cpu_start + CPU_START_CLUSTER(cluster)); + + return 0; +} + +static const struct spin_table_power_ops t6040_cpu_power_ops = { + .cpu_stop = t6040_cpu_stop, + .cpu_off = t6040_cpu_off, + .cpu_on = t6040_cpu_on, +}; + +/* + * The commit's hold needs them. Registered, they also let kexec take parking + * CPUs as able to leave the kernel, for the rest of this boot. Nothing calls + * @cpu_stop or @cpu_off but a commit, and @cpu_on starts only a CPU a commit + * stopped. + */ +static void t6040_sleep_power_ops(struct t6040_sleep *s) +{ + if (s->power_ops || !s->cpu_start) + return; + if (!spin_table_set_power_ops(&t6040_cpu_power_ops)) { + s->power_ops = true; + dev_info(s->dev, "CPU parking power ops registered\n"); + } +} + +static void t6040_sleep_set_wd2(struct t6040_sleep *s, unsigned int secs) +{ + if (!s->wdt) + return; + writel(0, s->wdt + WDT_WD2_CTL); + writel(0, s->wdt + WDT_WD2_COUNT); + writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD2_ALARM); + writel(1, s->wdt + WDT_WD2_CTL); + s->wd2_armed = true; +} + +static void t6040_sleep_clear_wd2(struct t6040_sleep *s) +{ + if (!s->wdt || !s->wd2_armed) + return; + writel(0, s->wdt + WDT_WD2_CTL); + s->wd2_armed = false; +} + +/* + * The PACC1_ANE complex (cluster-ctl +0x8) holds P1 and the ANE: after the S2R timer + * E reads 0x00 and P1 stays 0x10. The ANE's power words, ACTUAL nibbles: + * sys (pmgr +0x3c0), then mpm, cpu, td, base (pmgr_ane +0x0..+0x18). Returns them + * packed sys<<16 | mpm<<12 | cpu<<8 | td<<4 | base; 0xfffff without windows. + */ +#define T6040_ANE_SYS_PS_PA 0x5022803c0ULL +#define T6040_ANE_PS_PA 0x50228c000ULL + +static u32 t6040_ane_actual(struct t6040_sleep *s) +{ + u32 v, i; + + if (!s->ane_sys_ps || !s->ane_ps) + return 0xfffff; + v = FIELD_GET(GENMASK(7, 4), readl(s->ane_sys_ps)) << 16; + for (i = 0; i < 4; i++) + v |= FIELD_GET(GENMASK(7, 4), readl(s->ane_ps + i * 8)) << (12 - 4 * i); + return v; +} + +/* + * Bring-up: how far the hardware A2D got, from registers its programs write or poll + * (iBoot's reconfig programs, MPMGR seq 8 and MCPS seq 24): + * [27:20] progress code 0x508360114 (MPMGR: 4, 0x10, then 5, 7) + * [19:18] MasterMini+0x8214 bits 25:24 (MPMGR handshake acks) + * [17] PMC request 0x5048aa13c bit 0 (MCPS := 1), [16] ack 0x5048aa140 bit 31 + * [15:14] MiniPMGR 0x508364004 bits 1:0 (MCPS handshake) + * [13] 0x502850020 bit 0 (MCPS writes it after progress == 0x10) + * [12:9] GPIO PS 0x502800450 ACTUAL (MCPS powers it on first) + * [8:5] SIO_CPU PS 0x502280130 ACTUAL, [4] its BIT31 (MCPS := 0x80000000 after + * the SIO poll 0x429e08000 bit 1) + * [3] DPA4 PS 0x502280250 TARGET 0 (last of the group-1 domains MCPS turns off) + * [2] PS 0x502280120 TARGET 0 (turned off right after group 1) + * Bits [1:0] are left for a sample count. Every window is optional (0 bits). + */ +static unsigned int a2d_read_mask = 0x3f; +module_param(a2d_read_mask, uint, 0644); +MODULE_PARM_DESC(a2d_read_mask, + "Bring-up: A2D state windows read (bit 0 MiniPMGR 0x114, 1 MasterMini 0x8214, 2 PMC 0x5048aa13c/140, 3 0x508364004, 4 0x502850020, 5 PS words)"); + +static u32 t6040_a2d_state(struct t6040_sleep *s) +{ + u32 v = 0, w, m = READ_ONCE(a2d_read_mask); + + if (s->a2d_mini && (m & BIT(0))) + v |= (readl(s->a2d_mini + 0x114) & 0xff) << 20; + if (s->a2d_mm8 && (m & BIT(1))) + v |= ((readl(s->a2d_mm8 + 0x214) >> 24) & 3) << 18; + if (s->a2d_pmc && (m & BIT(2))) { + v |= (readl(s->a2d_pmc + 0x13c) & 1) << 17; + v |= (readl(s->a2d_pmc + 0x140) >> 31) << 16; + } + if (s->a2d_mini4 && (m & BIT(3))) + v |= (readl(s->a2d_mini4 + 0x4) & 3) << 14; + if (s->a2d_blk && (m & BIT(4))) + v |= (readl(s->a2d_blk + 0x20) & 1) << 13; + if (s->soc_ps && (m & BIT(5))) + v |= FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 0x450)) << 9; + if (s->soc_east && (m & BIT(5))) { + w = readl(s->soc_east + 0x130); + v |= FIELD_GET(PS_ACTUAL, w) << 5 | (w >> 31) << 4; + v |= !FIELD_GET(PS_TARGET, readl(s->soc_east + 0x250)) << 3; + v |= !FIELD_GET(PS_TARGET, readl(s->soc_east + 0x120)) << 2; + } + return v; +} + +/* + * Bring-up: the AOP's power requests against the MPMGR's first A2D handshake (AOP firmware: + * AOP-local 0x5108b8100 bit 0 DRAM (status MasterMini+0x14 bit 0), bit 1 AP + * and bit 2 AOP CPU (status MiniPMGR 0x508360108)). Mark tag 1: + * [27:20] progress 0x508360114, [19:18] MasterMini+0x8214 acks 25:24, + * [17:15] AOP requests 0x5108b8100 [2:0], [14] MasterMini+0x14 bit 0, + * [13:10] MasterMini+0x8214 bits 31 (REQ), 16, and MasterMini+0xc014 (IB2) bits 31, 24; + * [9:6] MasterMini+0xc014 bit 25, MiniPMGR +0x104 [2:0]; [5:2] MiniPMGR +0x108 [3:0]; + * [1:0] sample count + */ +static u32 t6040_aop_state(struct t6040_sleep *s) +{ + u32 v = 0; + + if (s->a2d_mini) { + v |= (readl(s->a2d_mini + 0x114) & 0xff) << 20; + v |= (readl(s->a2d_mini + 0x104) & 7) << 6; + v |= (readl(s->a2d_mini + 0x108) & 0xf) << 2; + } + if (s->a2d_mm8) { + u32 h1 = readl(s->a2d_mm8 + 0x214); + + v |= ((h1 >> 24) & 3) << 18; + v |= (h1 >> 31) << 13 | ((h1 >> 16) & 1) << 12; + } + if (s->a2d_mm12) { + u32 h2 = readl(s->a2d_mm12 + 0x14); + + v |= (h2 >> 31) << 11 | ((h2 >> 24) & 1) << 10 | ((h2 >> 25) & 1) << 9; + } + if (s->aop_req) + v |= (readl(s->aop_req + 0x100) & 7) << 15; + if (s->mastermini) + v |= (readl(s->mastermini + 0x14) & 1) << 14; + return v; +} + +static void t6040_log_a2d(struct t6040_sleep *s, const char *when) +{ + pr_info("t6040-sleep: %s: A2D state %#010x (0x2 tag in marks), AOP state %#010x (0x1 tag), raw AOP req %#x MiniPMGR+0x100/104/108 %#x/%#x/%#x MasterMini+0x14 %#x +0x8214 %#x +0xc014 %#x\n", + when, t6040_a2d_state(s), t6040_aop_state(s), + s->aop_req ? readl(s->aop_req + 0x100) : 0, + s->a2d_mini ? readl(s->a2d_mini + 0x100) : 0, s->a2d_mini ? readl(s->a2d_mini + 0x104) : 0, + s->a2d_mini ? readl(s->a2d_mini + 0x108) : 0, + s->mastermini ? readl(s->mastermini + 0x14) : 0, + s->a2d_mm8 ? readl(s->a2d_mm8 + 0x214) : 0, s->a2d_mm12 ? readl(s->a2d_mm12 + 0x14) : 0); + /* + * The MiniPMGR handshake words the reconfig programs use (iBoot rcfg: MCPS A2D waits + * on +0x44 bit 2 / bit 3 and +0x40 bit 8, D2A raises +0x40/+0x3c/+0x34, D2S +0x48/+0x4c), + * read only, and the five cluster-ctl slots (EACC, PACC0, PACC1_ANE, DISP_INT, DISP_EXT). + */ + if (s->a2d_mini) { + static const u16 hs[] = { 0x08, 0x1c, 0x24, 0x34, 0x38, 0x3c, 0x40, 0x44, 0x48, 0x4c, 0x5c, 0x114, 0x200 }; + u32 v[ARRAY_SIZE(hs)], c[CLUSTER_CTL_NR] = {}; + int i; + + for (i = 0; i < ARRAY_SIZE(hs); i++) + v[i] = readl(s->a2d_mini + hs[i]); + if (s->cluster_ctl) + for (i = 0; i < CLUSTER_CTL_NR; i++) + c[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); + pr_info("t6040-sleep: %s: MiniPMGR +08 %#x +1c %#x +24 %#x +34 %#x +38 %#x +3c %#x +40 %#x +44 %#x +48 %#x +4c %#x +5c %#x +114 %#x +200 %#x; cluster-ctl %#x %#x %#x %#x %#x\n", + when, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11], v[12], + c[0], c[1], c[2], c[3], c[4]); + } +} + +static void t6040_log_ane(struct t6040_sleep *s, const char *when) +{ + u32 c[3] = {}, i; + + if (s->cluster_ctl) + for (i = 0; i < 3; i++) + c[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); + pr_info("t6040-sleep: %s: ANE ACTUAL sys/mpm/cpu/td/base %05x, cluster-ctl E %#x P0 %#x P1 %#x\n", + when, t6040_ane_actual(s), c[0], c[1], c[2]); +} + +/* Bring-up: the WD2 re-armed for @ms from now, writes only (usable with the counter stopped). */ +static void t6040_wd2_raw(struct t6040_sleep *s, u32 ms) +{ + writel(0, s->wdt + WDT_WD2_CTL); + writel(0, s->wdt + WDT_WD2_COUNT); + writel(ms * (WDT_TICKS_PER_S / 1000), s->wdt + WDT_WD2_ALARM); + writel(1, s->wdt + WDT_WD2_CTL); + dsb(sy); +} + +/* + * In process context, after the device suspend: which watched devices are + * bound. The commit point runs with interrupts off and only reads power words. + */ +static void t6040_sleep_watch_bound(struct t6040_sleep *s) +{ + int i; + + for (i = 0; i < T6040_WATCH_COUNT; i++) { + struct t6040_sleep_watch *w = &s->watch[i]; + struct device_node *np = of_find_compatible_node(NULL, NULL, w->compatible); + struct platform_device *pdev = np ? of_find_device_by_node(np) : NULL; + + w->bound = pdev && device_is_bound(&pdev->dev); + if (pdev) + put_device(&pdev->dev); + of_node_put(np); + } +} + +/* Its power word's actual state is on. Unknown counts as on. */ +static bool t6040_sleep_watch_on(struct t6040_sleep_watch *w, u32 *val) +{ + *val = 0; + if (!w->has_ps || regmap_read(w->ps_regmap, w->ps_offset, val)) + return true; + return FIELD_GET(PS_ACTUAL, *val); +} + +/* + * The checks a real commit makes, read-only: every registered coprocessor + * quiesced by its driver, one CPU up and it the boot object's, the SEP bound + * with its sleep request left here, ANS stopped only by its driver, and the + * display pipes off. Logs each failure and returns the count. + */ +/* Why the last t6040_sleep_refuse() refused, one bit per check (the refusal mark). */ +static u32 t6040_refuse_why; +static u32 t6040_sleep_groups_on(struct t6040_sleep *s); + +static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) +{ + struct t6040_sleep_watch *sep = &s->watch[T6040_WATCH_SEP]; + struct t6040_sleep_watch *ans = &s->watch[T6040_WATCH_ANS]; + unsigned int refused = 0, ready = 0, registered = 0; + u32 why = 0; + bool ans_idled = false; + u32 val; + int i; + + for (i = 0; i < SLEEP_COPROCS; i++) { + struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + + if (!c->dev) + continue; + registered++; + if (c->ready) { + ready++; + ans_idled |= c->pcie_ans; + } else { + /* Nothing may lower a coprocessor's domain under its driver. */ + pr_err("t6040-sleep: refused: %s is running and its driver has not quiesced it\n", + dev_name(c->dev)); + refused++; + why |= BIT(0); + } + } + pr_info("t6040-sleep: %u of %u registered coprocessors ready for the commit\n", ready, + registered); + + if (num_online_cpus() != 1 || !s->mbox || + (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { + pr_err("t6040-sleep: refused: wrong CPU, other CPUs online, or no mailbox\n"); + refused++; + why |= BIT(1); + } + + /* + * Refuse rather than half-sleep if any register block the commit or the + * wake must write is missing from the device tree. The deep WFI would + * never arm, and the wake could not restore the SoC. + */ + if (!s->cluster_ctl || !s->pwrgate || !s->mastermini || !s->spmi0 || + !s->soc_east || !s->aop_global) { + pr_err("t6040-sleep: refused: a commit/wake register base is not mapped (cluster-ctl %d pwrgate %d mastermini %d spmi0 %d soc-east %d aop-global %d)\n", + !!s->cluster_ctl, !!s->pwrgate, !!s->mastermini, !!s->spmi0, + !!s->soc_east, !!s->aop_global); + refused++; + why |= BIT(2); + } + + if (!sep->bound) { + /* Nobody told it of the sleep: a commit with it running never returns. */ + t6040_sleep_watch_on(sep, &val); + pr_err("t6040-sleep: refused: sep isn't bound (state %#x)\n", val); + refused++; + why |= BIT(3); + } else if (!t6040_sleep_enclave.tell) { + pr_err("t6040-sleep: refused: sep's sleep request isn't left for the last refusal\n"); + refused++; + why |= BIT(3); + } + + /* Only ANS's driver can stop it, and it doesn't come back without a real sleep. */ + if (!ans_idled && t6040_sleep_watch_on(ans, &val)) { + pr_err("t6040-sleep: refused: ANS is on (%#x) with no driver to stop it\n", val); + refused++; + why |= BIT(4); + } + + /* + * ANS would be stopped, but its ST lanes have no way back without the + * apple,apcie control windows + PLL/lane tunables. #1208 keeps + * suspend-to-idle the default in this state; here the deep sleep refuses. + */ + if (ans_idled && !s->apcie_st.valid) { + pr_err("t6040-sleep: refused: ANS would stop but the storage-apcie windows/tunables are absent (apple,apcie)\n"); + refused++; + why |= BIT(5); + } + + /* The engine waits for the display CPUs with no timeout. */ + if (t6040_sleep_log_groups(s)) { + refused++; + why |= BIT(6) | t6040_sleep_groups_on(s) << 8; + } + t6040_refuse_why = why; + + return refused; +} + +/* The last refusals, each coprocessor's own: nothing has stopped yet. */ +static int t6040_sleep_coprocs_check(void) +{ + int i, ret; + + for (i = 0; i < SLEEP_COPROCS; i++) { + struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + + if (!c->dev || !c->ready || c->stopped || !c->last_check) + continue; + ret = c->last_check(c->data); + if (ret) { + pr_err("t6040-sleep: refused: %s: %d\n", dev_name(c->dev), ret); + return ret; + } + pr_info("t6040-sleep: last check of %s passed\n", dev_name(c->dev)); + } + + return 0; +} + +/* Last of all the refusals: an enclave that gated comes back only through a real sleep. */ +static int t6040_sleep_enclave_tell(void) +{ + int ret; + + if (!t6040_sleep_enclave.tell) + return 0; + ret = t6040_sleep_enclave.tell(t6040_sleep_enclave.data); + if (ret) + pr_err("t6040-sleep: refused: the enclave didn't take the sleep request: %d\n", + ret); + + return ret; +} + +/* + * Bring-up: the platform idles the PMP (AP and IOP power state 0x201) right before the cores + * sleep (PMP mailbox 0x500e08800) and no stop was seen after that, so the + * coprocessor may have to stay runnable for the engine. Bit 0 keeps the PMP's run bit + * after its idle, bit 1 ANS's. + */ +static unsigned int coproc_keep_run; +module_param(coproc_keep_run, uint, 0644); +MODULE_PARM_DESC(coproc_keep_run, + "Bring-up: keep the run bit after the idle at the commit (bit 0 PMP, bit 1 ANS)"); + +/* + * Bring-up: A2D stalls at MiniPMGR +0x40, the AP partition that does not power down. + * LLB's power-down runs the same A2D to completion after powering the MTP (and AOP+audio) + * islands off; the MTP is the only coprocessor whose DAPF reaches the MiniPMGR request words + * +0x100..+0x10b. Bit 0: power the 13 MTP island PS words (MiniPMGR 0x50830c000..0x060, + * MTP_FABRIC..MTP_DMA) off right before the S2R timer, children first, each polled to + * ACTUAL 0 (mark 0x57 + the words left on). Bit 1: the same at the dry-run commit point, + * logged, then the saved words written back (the MTP firmware does not survive it). + */ +static unsigned int mtp_island_off; +module_param(mtp_island_off, uint, 0644); +MODULE_PARM_DESC(mtp_island_off, + "Bring-up: MTP island PS off as LLB before A2D (bit 0 real commit, bit 1 dry-run point)"); + +#define T6040_MTP_PS_PA 0x50830c000ULL +#define T6040_MTP_PS_WORDS 13 + +/* Children first. Returns the words (bit n = +8 * n) that did not reach ACTUAL 0. */ +static u32 t6040_mtp_island_off(struct t6040_sleep *s, u32 *saved) +{ + u32 left = 0, v; + int n; + + if (!s->mtp_ps) + return GENMASK(T6040_MTP_PS_WORDS - 1, 0); + for (n = T6040_MTP_PS_WORDS - 1; n >= 0; n--) { + void __iomem *r = s->mtp_ps + 8 * n; + + v = readl(r); + if (saved) + saved[n] = v; + /* As LLB: automatic mode off first, then the target. */ + if (v & PS_AUTO_ENABLE) { + v &= ~PS_AUTO_ENABLE; + writel(v, r); + } + if (FIELD_GET(PS_TARGET, v)) + writel(v & ~PS_TARGET, r); + if (readl_poll_timeout_atomic(r, v, !FIELD_GET(PS_ACTUAL, v), 1, 2000)) + left |= BIT(n); + } + return left; +} + +/* + * Bring-up: iBoot's idle power-down (the same A2D as LLB's, + * which completes) configures the CPU clusters' CPM right before the cluster-ctl writes and + * the S2R request: cluster+0xe50000 + * |= 0x1c000000, poll +0x90 [19:16] in {5, 6, 10}, RMW (& 0xffffffef9c7e1fff) | 0x61816000, + * cpm-impl-reg cluster+0xe40000 |= bit 63 | bit 0, clear 0x1c000000 again; then the boot + * CPU's cpu-impl-reg 0x211050110 |= 0x3000000. Linux and aura never write these. + * Bit 0: clusters 0 and 1 as iBoot, bit 1: cluster 2 too, bit 2: the boot CPU's +0x110. + * A CPM state poll that times out leaves that cluster with 0x1c000000 cleared (mark 0x56). + */ +static unsigned int cpm_iboot; +module_param(cpm_iboot, uint, 0644); +MODULE_PARM_DESC(cpm_iboot, + "Bring-up: iBoot's CPM cluster config before the S2R request (bit 0 clusters 0+1, bit 1 cluster 2, bit 2 boot CPU +0x110)"); + +#define T6040_CPM_CFG_PA(c) (0x210e50000ULL + (c) * 0x1000000ULL) +#define T6040_CPM_IMPL_PA(c) (0x210e40000ULL + (c) * 0x1000000ULL) +#define T6040_CPU4_IMPL110_PA 0x211050110ULL +#define CPM_CFG_LOCK 0x1c000000ULL +#define CPM_CFG_STATE 0x90 +#define CPM_CFG_KEEP 0xffffffef9c7e1fffULL +#define CPM_CFG_SET 0x61816000ULL +#define CPM_IMPL_SET 0x8000000000000001ULL + +static bool t6040_cpm_state_ok(u64 v) +{ + unsigned int st = (v >> 16) & 0xf; + + return st <= 10 && (BIT(st) & 0x460); +} + +static u32 t6040_cpm_iboot(struct t6040_sleep *s, unsigned int mask) +{ + u32 to = 0; + u64 v; + int c; + + for (c = 0; c < 3; c++) { + void __iomem *cfg = s->cpm_cfg[c], *impl = s->cpm_impl[c]; + + if (!(c < 2 ? mask & BIT(0) : mask & BIT(1)) || !cfg || !impl) + continue; + writeq(readq(cfg) | CPM_CFG_LOCK, cfg); + if (readq_poll_timeout_atomic(cfg + CPM_CFG_STATE, v, t6040_cpm_state_ok(v), 1, 2000)) { + to |= BIT(c); + writeq(readq(cfg) & ~CPM_CFG_LOCK, cfg); + continue; + } + writeq((readq(cfg) & CPM_CFG_KEEP) | CPM_CFG_SET, cfg); + writeq(readq(impl) | CPM_IMPL_SET, impl); + writeq(readq(cfg) & ~CPM_CFG_LOCK, cfg); + } + if ((mask & BIT(2)) && s->cpu4_impl110) + writeq(readq(s->cpu4_impl110) | 0x3000000ULL, s->cpu4_impl110); + return to; +} + +static void t6040_log_cpm(struct t6040_sleep *s, const char *when) +{ + u64 v[3][3] = {}; + int c; + + for (c = 0; c < 3; c++) { + if (s->cpm_cfg[c]) { + v[c][0] = readq(s->cpm_cfg[c]); + v[c][1] = readq(s->cpm_cfg[c] + CPM_CFG_STATE); + } + if (s->cpm_impl[c]) + v[c][2] = readq(s->cpm_impl[c]); + } + pr_info("t6040-sleep: %s: CPM cfg/state/impl c0 %#llx/%#llx/%#llx c1 %#llx/%#llx/%#llx c2 %#llx/%#llx/%#llx cpu4 +0x110 %#llx\n", + when, v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2], v[2][0], v[2][1], v[2][2], + s->cpu4_impl110 ? readq(s->cpu4_impl110) : 0); +} + +/* + * Bring-up: the AOP + audio island off as iBoot's idle power-down (PS ids + * 0x49..0x31 in this order, LLB's id table gives each one's offset from MiniPMGR 0x508304000; + * 0x56 appears twice there). Bit 0: right before the S2R timer (mark 0x55, words left on + * << 8 | first index left on). Wake through the AOP may not work with it. + */ +static unsigned int aop_island_off; +module_param(aop_island_off, uint, 0644); +MODULE_PARM_DESC(aop_island_off, + "Bring-up: AOP + audio island PS off as iBoot before A2D (bit 0 real commit)"); + +#define T6040_AOP_PS_PA 0x508304000ULL +static const u16 t6040_aop_ps_order[] = { + 0x0b0, 0x128, 0x120, 0x158, 0x150, 0x148, 0x140, 0x138, 0x130, 0x118, 0x0c0, 0x0c8, + 0x0e8, 0x0f0, 0x0f8, 0x100, 0x108, 0x110, 0x118, 0x0e0, 0x0d8, 0x0d0, 0x0b8, 0x090, + 0x020, 0x098, 0x0a0, 0x0a8, 0x028, 0x030, 0x068, 0x070, 0x078, 0x080, 0x038, 0x040, + 0x048, 0x058, 0x060, 0x088, 0x050, 0x010, 0x018, 0x008, 0x000, +}; + + +/* + * Bring-up: which SOC / SOC-East power words are still on right before the S2R timer. + * The reset that follows a stalled A2D loses everything but the PMU, so the bitmap (bit i = + * ACTUAL != 0 of t6040_ps_snap_pa[i], ADT order) goes into PMU SRAM at the unused tail of + * the SOCD container: 0xa2c0 'PSNP' + count, then the bitmap words. m4-s2r-psnap decodes it. + */ +static int t6040_pmu_cmd(struct t6040_sleep *s, u32 cmd, const u32 *tx, u32 *rx, + unsigned int nrx); + +static bool ps_snap = true; +module_param(ps_snap, bool, 0644); +MODULE_PARM_DESC(ps_snap, "Bring-up: store the on-bitmap of the SOC/SOC-East PS words in PMU SRAM 0xa2c0 right before the S2R timer"); + +/* Generated from the live J616s ADT: SOC + SOC-East PS words, display CPU words left out. */ +static const u32 t6040_ps_snap_pa[] = { + 0x2280000, 0x2280100, 0x2280108, 0x2280110, 0x2280118, 0x2280120, + 0x2280128, 0x2280130, 0x2280138, 0x2280140, 0x2280148, 0x2280150, + 0x2280158, 0x2280160, 0x2280168, 0x2280170, 0x2280178, 0x2280180, + 0x2280188, 0x2280190, 0x2280198, 0x22801a0, 0x22801a8, 0x22801b0, + 0x22801b8, 0x22801c0, 0x22801c8, 0x22801d0, 0x22801d8, 0x22801e0, + 0x22801e8, 0x22801f0, 0x22801f8, 0x2280200, 0x2280208, 0x2280210, + 0x2280218, 0x2280220, 0x2280228, 0x2280230, 0x2280238, 0x2280240, + 0x2280248, 0x2280250, 0x2280258, 0x2280260, 0x2280268, 0x2280270, + 0x2280278, 0x2280280, 0x2280288, 0x2280290, 0x2280298, 0x22802e0, + 0x22802e8, 0x22802f0, 0x22802f8, 0x2280300, 0x2280308, 0x2280310, + 0x2280318, 0x2280368, 0x2280370, 0x2280378, 0x2280380, 0x2280388, + 0x2280390, 0x2280398, 0x22803a0, 0x22803c0, 0x2280c00, 0x228c000, + 0x228c008, 0x228c010, 0x228c018, 0x2800000, 0x2800008, 0x2800010, + 0x2800018, 0x2800020, 0x2800028, 0x2800030, 0x2800038, 0x2800040, + 0x2800048, 0x2800050, 0x2800058, 0x2800060, 0x2800068, 0x2800070, + 0x2800078, 0x2800080, 0x2800088, 0x2800090, 0x2800100, 0x2800108, + 0x2800110, 0x2800118, 0x2800120, 0x2800128, 0x2800130, 0x2800138, + 0x2800140, 0x2800148, 0x2800150, 0x2800158, 0x2800160, 0x2800168, + 0x2800170, 0x2800178, 0x2800180, 0x2800188, 0x2800190, 0x2800198, + 0x28001a0, 0x28001a8, 0x28001b0, 0x28001b8, 0x28001c0, 0x28001c8, + 0x28001d0, 0x28001d8, 0x28001e0, 0x28001e8, 0x28001f0, 0x28001f8, + 0x2800200, 0x2800278, 0x2800288, 0x2800290, 0x2800298, 0x28002d0, + 0x2800328, 0x2800330, 0x2800338, 0x2800340, 0x2800348, 0x2800390, + 0x2800398, 0x28003a0, 0x28003a8, 0x28003b0, 0x28003b8, 0x28003c0, + 0x28003c8, 0x28003d0, 0x28003d8, 0x28003e0, 0x28003e8, 0x28003f0, + 0x28003f8, 0x2800400, 0x2800408, 0x2800410, 0x2800418, 0x2800420, + 0x2800428, 0x2800430, 0x2800438, 0x2800440, 0x2800448, 0x2800450, + 0x2800458, 0x2800468, 0x2800470, 0x2800478, 0x2800480, 0x2800488, + 0x2800490, 0x2800498, 0x2808000, 0x2808008, 0x2808010, 0x2808018, + 0x2808020, +}; + +#define T6040_PS_SNAP_WORDS DIV_ROUND_UP(ARRAY_SIZE(t6040_ps_snap_pa), 32) +#define T6040_PS_SNAP_PMU 0xa2c0 +#define T6040_PS_SNAP_MAGIC 0x504e5350 /* "PSNP" in PMU byte order */ + +static void t6040_ps_snap_take(struct t6040_sleep *s, u32 *map) +{ + int i; + + memset(map, 0, T6040_PS_SNAP_WORDS * sizeof(u32)); + for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { + u32 pa = t6040_ps_snap_pa[i]; + void __iomem *b = (pa >> 16) == 0x0280 ? s->snap_soc : s->snap_east; + u32 off = pa - ((pa >> 16) == 0x0280 ? 0x02800000 : 0x02280000); + + if (b && FIELD_GET(PS_ACTUAL, readl(b + off))) + map[i / 32] |= BIT(i % 32); + } +} + +static void t6040_ps_snap_store(struct t6040_sleep *s) +{ + u32 map[T6040_PS_SNAP_WORDS], hdr = T6040_PS_SNAP_MAGIC; + u32 cnt = ARRAY_SIZE(t6040_ps_snap_pa); + int i; + + t6040_ps_snap_take(s, map); + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU, SPMI_OP_WRITE4), &hdr, NULL, 0); + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU + 4, SPMI_OP_WRITE4), &cnt, NULL, 0); + for (i = 0; i < T6040_PS_SNAP_WORDS; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU + 8 + 4 * i, SPMI_OP_WRITE4), + &map[i], NULL, 0); +} + + +/* + * Bring-up: the stalled MCPS sits in its PMC write block (static voters 0x5048ac000.., + * rail words 0x504820000 + rail * 0x4000, agent floors 0x504800000 + agent * 0x4000), and an + * AP access to the agent-3 floor hung the bus on the J616s. Read the words one by one, a mark + * (0x3d0 + index) before each so a hang names the word. Bit 0: at the dry-run commit point + * (WD2 armed, values logged). Bit 1: in the real commit right before the S2R timer, after the + * clock and keep-alive steps; the values go to PMU SRAM 0xa280 ('PMCP' + the 9 words). + */ +static unsigned int pmc_probe; +module_param(pmc_probe, uint, 0644); +MODULE_PARM_DESC(pmc_probe, "Bring-up: read the PMC words the A2D engine writes (bit 0 dry-run point, bit 1 real commit before the timer)"); + +static const u64 t6040_pmc_probe_pa[9] = { + 0x5048aa140ULL, 0x5048ac000ULL, 0x5048ac00cULL, 0x5048ac020ULL, 0x504800000ULL, + 0x50480c000ULL, 0x504820000ULL, 0x504824004ULL, 0x504828004ULL, +}; +#define T6040_PMC_PROBE_PMU 0xa280 +#define T6040_PMC_PROBE_MAGIC 0x50434d50 /* "PMCP" in PMU byte order */ + +static void t6040_mark(struct t6040_sleep *s, u32 n); + +static void t6040_pmc_probe_read(struct t6040_sleep *s, u32 *v) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) { + t6040_mark(s, 0x3d0 + i); + v[i] = s->pmc_probe_io[i] ? readl(s->pmc_probe_io[i]) : 0xdeadbeef; + } +} + +/* + * Bring-up (the PMC stopped answering between the dry-run point and the timer): one read + * of PMC 0x5048aa140 after each .enter / commit step, mark 0x380 + checkpoint before it, so a + * hang names the first step after which the PMC is gone. + */ +static bool pmc_bisect; +module_param(pmc_bisect, bool, 0644); +MODULE_PARM_DESC(pmc_bisect, "Bring-up: read PMC 0x5048aa140 after each sleep step (mark 0x380 + step)"); + +static void t6040_pmc_cp(struct t6040_sleep *s, unsigned int k) +{ + if (!READ_ONCE(pmc_bisect) || !s->pmc_probe_io[0]) + return; + t6040_mark(s, 0x380 + k); + (void)readl(s->pmc_probe_io[0]); +} + +static void t6040_pmc_probe_store(struct t6040_sleep *s, const u32 *v) +{ + u32 m = T6040_PMC_PROBE_MAGIC; + int i; + + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMC_PROBE_PMU, SPMI_OP_WRITE4), &m, NULL, 0); + for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMC_PROBE_PMU + 4 + 4 * i, SPMI_OP_WRITE4), + &v[i], NULL, 0); +} + +/* As LLB per word: automatic mode off, target 0, ACTUAL polled (bounded here). */ +static u32 t6040_aop_island_off(struct t6040_sleep *s) +{ + u32 left = 0, first = 0xff, v; + int i; + + if (!s->aop_ps) + return 0xffff; + for (i = 0; i < ARRAY_SIZE(t6040_aop_ps_order); i++) { + void __iomem *r = s->aop_ps + t6040_aop_ps_order[i]; + + v = readl(r); + if (v & PS_AUTO_ENABLE) { + v &= ~PS_AUTO_ENABLE; + writel(v, r); + } + if (FIELD_GET(PS_TARGET, v)) + writel(v & ~PS_TARGET, r); + if (readl_poll_timeout_atomic(r, v, !FIELD_GET(PS_ACTUAL, v), 1, 2000)) { + left++; + if (first == 0xff) + first = i; + } + } + return left ? (left << 8 | first) : 0; +} + +/* + * On the J616s the device resume reached the AOP (510c10000.aop) and took an SError in its resume, + * whose first act after a firmware-driven sleep is reading its ASC's run bit: the AOP + audio + * island was not powered after the wake. aop_wake bit0 records 'AOPW' at PMU 0xa2c0 right + * after the wake return's APCIe step (w1 = words on before | words on after << 8 | raise + * timeouts << 16, w2..w7 = ACTUAL nibbles of the 44 words 0x000..0x158, 8 per word, before any + * raise); bit1 writes back the words saved in .begin, parents first (iBoot's power-down order + * reversed), waiting for ACTUAL where the saved target was on. The ASC stays halted: the AOP + * driver then takes it over cold (unbind + probe after thaw). + */ +static void t6040_ret_mark(struct t6040_sleep *s, u32 n); + +#define T6040_AOP_PS_WORDS 44 +#define T6040_AOPW_PMU 0xa2c0 +#define T6040_AOPW_MAGIC 0x57504f41 /* "AOPW" in PMU byte order */ + +static unsigned int aop_wake = 15; +module_param(aop_wake, uint, 0644); +MODULE_PARM_DESC(aop_wake, "Bring-up wake: bit0 record the AOP island ('AOPW' at PMU 0xa2c0), bit1 power it back as saved in .begin, bit2 words on before the sleep to manual target 0xf, bit3 read the ASC CPU_CONTROL (marks 0x58/0x59)"); + +static void t6040_aop_ps_save(struct t6040_sleep *s) +{ + int i; + + s->aop_ps_saved_valid = false; + if (!s->aop_ps) + return; + for (i = 0; i < T6040_AOP_PS_WORDS; i++) + s->aop_ps_saved[i] = readl(s->aop_ps + 8 * i); + s->aop_ps_saved_valid = true; +} + +static unsigned int t6040_aop_ps_count_on(struct t6040_sleep *s, u32 *nib) +{ + unsigned int on = 0; + int i; + + for (i = 0; i < T6040_AOP_PS_WORDS; i++) { + u32 a = FIELD_GET(PS_ACTUAL, readl(s->aop_ps + 8 * i)); + + if (a) + on++; + if (nib) + nib[i / 8] |= a << (4 * (i % 8)); + } + return on; +} + +static void t6040_aop_wake(struct t6040_sleep *s) +{ + unsigned int mode = READ_ONCE(aop_wake), before, after, to = 0; + u32 w[10] = { T6040_AOPW_MAGIC }; + int k, i; + + if (!mode || !s->aop_ps) + return; + before = t6040_aop_ps_count_on(s, &w[2]); + w[8] = w[9] = 0; + if ((mode & BIT(1)) && s->aop_ps_saved_valid) { + for (k = ARRAY_SIZE(t6040_aop_ps_order) - 1; k >= 0; k--) { + u16 off = t6040_aop_ps_order[k]; + void __iomem *r = s->aop_ps + off; + u32 v, want = s->aop_ps_saved[off / 8], now = readl(r); + /* + * AOP's resume faulted again after a 1 h 43 min sleep (it had passed + * before): a word in automatic mode raised here gates again before the AOP + * driver reads its ASC. bit2: every word that was on before the sleep goes to + * manual target 0xf (AUTO off) and stays up through the resume. + */ + bool manual = (mode & BIT(2)) && FIELD_GET(PS_ACTUAL, want); + + if (manual) { + if (FIELD_GET(PS_ACTUAL, now) == 0xf && !(now & PS_AUTO_ENABLE)) + continue; + writel((now & ~(PS_AUTO_ENABLE | PS_TARGET)) | PS_TARGET, r); + } else { + if (FIELD_GET(PS_ACTUAL, now) || !FIELD_GET(PS_TARGET, want)) + continue; + writel(want, r); + } + if (off / 8 < 32) + w[8] |= BIT(off / 8); + else + w[9] |= BIT(off / 8 - 32); + if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v), 1, 2000)) + to++; + } + } + after = t6040_aop_ps_count_on(s, NULL); + w[1] = before | after << 8 | min(to, 0xffffU) << 16; + if ((mode & BIT(0)) && s->spmi0) + for (i = 0; i < 10; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 4 * i, SPMI_OP_WRITE4), + &w[i], NULL, 0); + + /* + * With every AOP island word that was on before the sleep at manual on, the + * AOP's resume still faulted on its ASC: bit3 reads the ASC CPU_CONTROL here, marks + * 0x58 before and 0x59 after, so an SError at 0x58 says the ASC is dead right at the + * wake (nothing later gated it). AOPW w8 then becomes CPU_CONTROL, w9 the AOP + * requests 0x5108b8100 [15:0] | MasterMini+0x14 << 16. + */ + if ((mode & BIT(3)) && s->aop_asc && s->spmi0) { + u32 ctl, rq; + + rq = (s->aop_req ? readl(s->aop_req + 0x100) & 0xffff : 0xffff) | + (s->mastermini ? (readl(s->mastermini + 0x14) & 0xffff) << 16 : 0); + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 36, SPMI_OP_WRITE4), &rq, NULL, 0); + t6040_ret_mark(s, 0x58); + ctl = readl(s->aop_asc + ASC_CPU_CONTROL); + dsb(sy); + isb(); + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 32, SPMI_OP_WRITE4), &ctl, NULL, 0); + t6040_ret_mark(s, 0x59); + } +} + +/* As read: parents first, the saved targets back. */ +static void t6040_mtp_island_back(struct t6040_sleep *s, const u32 *saved) +{ + int n; + + for (n = 0; n < T6040_MTP_PS_WORDS; n++) + writel(saved[n], s->mtp_ps + 8 * n); +} + +/* As #1208's, measured on ANS: status and control read, then the run bit cleared twice. */ +static void t6040_sleep_coproc_run_off(struct t6040_sleep_coproc *c) +{ + unsigned int keep = READ_ONCE(coproc_keep_run); + + if (!c->asc) { + c->stopped = true; + return; + } + c->status = readl(c->asc + ASC_CPU_STATUS); + if ((c->pcie_ans && (keep & BIT(1))) || + (!c->pcie_ans && c->last_stop && (keep & BIT(0)))) { + c->stopped = true; + return; + } + writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); + writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); + c->stopped = true; +} + +static u32 t6040_mark_hash(const char *name); +static void t6040_mark_raw(struct t6040_sleep *s, u32 v); +static void t6040_ret_mark(struct t6040_sleep *s, u32 n); + +/* After the last refusal: those registered first stop whole first, then the rest. */ +static void t6040_sleep_coprocs_stop(bool first) +{ + u32 h; + int i; + + for (i = 0; i < SLEEP_COPROCS; i++) { + struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + + if (!c->dev || !c->ready || c->first != first || c->stopped) + continue; + /* 0x5fSFHHHH: S 1 its last stop, 2 its run bit, 3 done; F first; H its name's hash. */ + h = t6040_mark_hash(dev_name(c->dev)) | first << 16; + t6040_mark_raw(t6040_sleep, 0x5f100000 | h); + if (c->last_stop) + c->last_stop(c->data); + t6040_mark_raw(t6040_sleep, 0x5f200000 | h); + t6040_sleep_coproc_run_off(c); + t6040_mark_raw(t6040_sleep, 0x5f300000 | h); + /* ANS stopped: the commit lowers its ST lanes, the wake raises them. */ + if (c->pcie_ans && t6040_sleep) + t6040_sleep->pcie_ans_stopped = true; + } + dsb(sy); +} + +/* + * The 4 clk-mux park words (ClkGen SOC + these offsets), from + * The clock quiesce; the offsets skip 0x58/0x5c. + */ +static const u32 t6040_clkgen_mux[CLKGEN_MUX_WORDS] = { + 0x38060, 0x38064, 0x38054, 0x38050, +}; + +/* + * The 9 AOP-global wake-source masks the platform arms before an S2R + * (the values are decoded from the ADT's ap-wake-sources). + * valid & ~enabled per word. + */ +static const u32 t6040_wake_mask[AOP_WAKE_WORDS] = { + 0xe5ff5fff, 0xff42edff, 0xffffffff, 0x3ffffbdf, 0xf7ffffc0, + 0xffffff7f, 0xffffffff, 0xffffffff, 0x00003fff, +}; + +/* + * The hardware S2R-lo entry counter: the count is PMGR + * regmap 1 + 0x70970 shifted right by 3; the platform reads + * 0x508370970 at quiesce. It tells "the SoC entered S2R" from "the + * engine never finished". Read-only, with the AOP-global S2R control, wake masks and + * wake status, logged at probe so the boot after a failed sleep shows them. + */ +#define T6040_S2R_LO_COUNT_PA 0x508370970ULL + +static void t6040_sleep_log_s2r_state(struct t6040_sleep *s, const char *when) +{ + void __iomem *c = ioremap_np(T6040_S2R_LO_COUNT_PA, 4); + u32 raw = c ? readl(c) : 0, m[AOP_WAKE_WORDS] = {}, st[AOP_WAKE_WORDS] = {}, ctl = 0; + int i; + + if (c) + iounmap(c); + if (s->aop_global) { + ctl = readl(s->aop_global + AOP_S2R_CTRL); + for (i = 0; i < AOP_WAKE_WORDS; i++) { + m[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); + st[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); + } + } + dev_info(s->dev, "S2R state at %s: S2R-lo count %u (raw %#x), AOP-global ctl %#x\n", + when, raw >> 3, raw, ctl); + dev_info(s->dev, "S2R state at %s: wake masks %*ph\n", when, + (int)sizeof(m), m); + dev_info(s->dev, "S2R state at %s: wake status %*ph\n", when, + (int)sizeof(st), st); +} + +/* + * The 15 axi2af fabric-bridge retention domains (the + * table for the axi2af workaround). Each domain's power word is in + * SOC (soc_ps, base 0x502800000) or SOC-East (soc_east, base 0x502280000); its + * fabric bridge is a separate window. On wake every domain that is powered up + * (PS TARGET == 0xf) gets bridge +0x104/+0x108 bit0 set, or the bridge drops + * its traffic (the ANS/Wi-Fi/display-dead signature). + */ +static const struct t6040_axi2af_domain { + const char *name; + bool east; /* power word in soc_east (true) or soc_ps (false) */ + u32 ps_off; + phys_addr_t bridge_pa; +} t6040_axi2af[T6040_AXI2AF_DOMAINS] = { + { "atc0_common", false, 0x390, 0x380000000 }, + { "atc1_common", false, 0x3b8, 0x388000000 }, + { "atc2_common", false, 0x3e0, 0x390000000 }, + { "atc3_common", false, 0x408, 0x398000000 }, + { "disp_sys", true, 0x258, 0x403000000 }, + { "dispext0_sys", false, 0x340, 0x3a3000000 }, + { "dispext1_sys", false, 0x490, 0x603000000 }, + { "dispext2_sys", false, 0x4e0, 0x607000000 }, + { "dispext3_sys", false, 0x2d8, 0x307000000 }, + { "ans", true, 0x370, 0x40b000000 }, + { "avd_sys", false, 0x170, 0x280000000 }, + { "venc0_sys", false, 0x278, 0x282000000 }, + { "venc1_sys", false, 0x2a0, 0x284000000 }, + { "jpg", true, 0x3a0, 0x486000000 }, + { "apcie_sys_gp", true, 0x390, 0x416000000 }, +}; + +/* + * The post-timer PMU "go hib (0x7)" SPMI pairs, as the platform sends them. Opaque + * (the abbey PMU opcode map is not decoded); replayed only if replay_pmu_go_hib + * is set. Each is framed as the platform frames it: notify, command, optional data, poll + * +0x80 bit0, acknowledge. + */ +static const struct { + u32 cmd; + u32 data; + bool has_data; +} t6040_pmu_go_hib_seq[] = { + { 0x20008e38, 0, false }, + { 0x20088e33, 0x1e9238, true }, + { 0x200e8e30, 0x0, true }, + { 0x20008e30, 0x41, true }, +}; + +/* + * As the platform writes the PMU over SPMI (mirror of #1208's t8140_pmu_write): the + * reply vector masked, the command and value written to +0x4, the reply polled + * and acknowledged, so none is left raised. + */ +static void t6040_pmu_write(struct t6040_sleep *s, u16 reg, u8 val) +{ + u32 status, enable; + + if (!s->spmi0) + return; + enable = readl(s->spmi0 + SPMI_NOTIFY_ENABLE); + writel(enable & ~SPMI_NOTIFY_REPLY, s->spmi0 + SPMI_NOTIFY_ENABLE); + writel(((u32)reg << 16) | SPMI_CMD_WRITE, s->spmi0 + SPMI_CMD); + writel(val, s->spmi0 + SPMI_CMD); + if (!readl_poll_timeout_atomic(s->spmi0 + SPMI_STATUS, status, + !(status & SPMI_STATUS_RX_EMPTY), 1, 1000)) + readl(s->spmi0 + SPMI_REPLY); + writel(1, s->spmi0 + SPMI_NOTIFY_ACK); + writel(enable, s->spmi0 + SPMI_NOTIFY_ENABLE); +} + +/* + * A PMU (SID 0xe) command with an extended register over spmi0, as #1314 frames T8132's: + * drain stale replies, mask the reply vector, the command and its data, poll and acknowledge + * the reply, then read @nrx words (a header, then the data). + */ +#define T6040_MARK(n) (0x53340000 | (n)) /* "S4" + step */ + +static int t6040_pmu_cmd(struct t6040_sleep *s, u32 cmd, const u32 *tx, u32 *rx, + unsigned int nrx) +{ + void __iomem *b = s->spmi0; + unsigned int i; + u32 val, enable; + int ret; + + if (!b) + return -ENODEV; + for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) + readl(b + SPMI_REPLY); + enable = readl(b + SPMI_NOTIFY_ENABLE); + writel(enable & ~SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ENABLE); + writel(cmd, b + SPMI_CMD); + if (tx) + writel(*tx, b + SPMI_CMD); + ret = readl_poll_timeout_atomic(b + SPMI_NOTIFY_ACK, val, val & SPMI_NOTIFY_REPLY, 1, + 1000); + writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); + for (i = 0; !ret && i < nrx; i++) { + ret = readl_poll_timeout_atomic(b + SPMI_STATUS, val, + !(val & SPMI_STATUS_RX_EMPTY), 1, 1000); + if (!ret) + rx[i] = readl(b + SPMI_REPLY); + } + /* A write's reply word, if the PMU sent one. */ + if (!nrx && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) + readl(b + SPMI_REPLY); + writel(enable, b + SPMI_NOTIFY_ENABLE); + + return ret; +} + +/* + * Bring-up progress through a reset (#1314's way on T8132): the PMU keeps its RTC comparator + * across one, and userspace reads it back on any kernel (spmi_nvmem0 offset 0x2008). A value + * 0x5334xxxx is the last step reached; the saved alarm + 1 s means the commit itself ran. + */ +static bool marks = true; +module_param(marks, bool, 0644); +MODULE_PARM_DESC(marks, "Write S2R progress marks into the PMU RTC comparator (bring-up)"); + +static void t6040_mark_raw(struct t6040_sleep *s, u32 v); + +/* The furthest driver step this sleep reached, carried in the device marks' byte 2. */ +static u8 t6040_furthest; + +static void t6040_mark(struct t6040_sleep *s, u32 n) +{ + u32 v = T6040_MARK(n); + + if (n < 0x40 && n > READ_ONCE(t6040_furthest)) + WRITE_ONCE(t6040_furthest, n); + + t6040_mark_raw(s, v); +} + +static void t6040_mark_save(struct t6040_sleep *s) +{ + u32 rx[2]; + + s->alarm_ok = false; + WRITE_ONCE(t6040_furthest, 0); + if (!READ_ONCE(marks) || !s->spmi0) + return; + if (t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_READ4), NULL, rx, 2)) { + pr_warn("t6040-sleep: marks: can't read the PMU comparator, no marks\n"); + return; + } + s->alarm_hdr = rx[0]; + s->alarm_saved = rx[1]; + s->alarm_ok = true; +} + +/* + * Per-device marks from the PM core (#1314's hook), between .begin and .end: 0x4P00HHHH, P the + * phase (1 prepare, 2 suspend, 3 late, 4 noirq, 8-10 resume), H the FNV-1a hash of the device's + * name folded to 16 bits. A panic keeps the low 28 bits of the last mark and sets the top nibble + * to its kind: 0xe an SError, 0xd a fatal exception (oops), 0x6 any other panic. A bare hardware + * reset keeps the mark as it was. The driver's own families are chosen so that a panic over them + * never reads as a device mark: 0x5334 steps, 0x5b refusal, 0x5c display word, 0x5e bridge. + */ +static void (*pm_bringup_dev_mark)(struct device *dev, unsigned int phase); +static DEFINE_RAW_SPINLOCK(t6040_mark_lock); +static u32 t6040_last_mark; + +static u32 t6040_mark_hash(const char *name) +{ + u32 h = 0x811c9dc5; + + for (; *name; name++) { + h ^= (u8)*name; + h *= 0x01000193; + } + return (h ^ (h >> 16)) & 0xffff; +} + +static void t6040_mark_raw(struct t6040_sleep *s, u32 v) +{ + unsigned long flags; + + if (!s || !READ_ONCE(marks) || !s->alarm_ok) + return; + raw_spin_lock_irqsave(&t6040_mark_lock, flags); + t6040_last_mark = v; + t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); + raw_spin_unlock_irqrestore(&t6040_mark_lock, flags); +} + +/* + * On the J616s a device's suspend failed before .prepare (furthest step 0x01), the PM core rolled + * back and the resume's SError mark overwrote which one it was. susp_rec keeps the last + * suspend-phase device mark (phases 1-4) at PMU 0xa204 too, under 'SUSF' at 0xa200 written in + * .begin (it takes DCSR's place: use with dcs_rec=0). + */ +#define T6040_SUSF_PMU 0xa200 +#define T6040_SUSF_MAGIC 0x46535553 /* "SUSF" in PMU byte order */ + +static bool susp_rec = true; +module_param(susp_rec, bool, 0644); +MODULE_PARM_DESC(susp_rec, "Bring-up: keep the last suspend-phase device mark at PMU 0xa204 ('SUSF' 0xa200)"); + +static void t6040_dev_mark(struct device *dev, unsigned int phase) +{ + struct t6040_sleep *s = t6040_sleep; + u32 v = 0x40000000 | (phase & 0xf) << 24 | READ_ONCE(t6040_furthest) << 16 | + (dev ? t6040_mark_hash(dev_name(dev)) : 0); + + t6040_mark_raw(s, v); + if (READ_ONCE(susp_rec) && phase >= 1 && phase <= 4 && s && s->spmi0 && s->alarm_ok) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU + 4, SPMI_OP_WRITE4), &v, NULL, 0); +} + +static void t6040_susf_begin(struct t6040_sleep *s) +{ + u32 w[2] = { T6040_SUSF_MAGIC, 0 }; + + if (!READ_ONCE(susp_rec) || !s->spmi0) + return; + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU, SPMI_OP_WRITE4), &w[0], NULL, 0); + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU + 4, SPMI_OP_WRITE4), &w[1], NULL, 0); +} + +void apple_sleep_bringup_mark(u32 v) +{ + if (READ_ONCE(pm_bringup_dev_mark)) + t6040_mark_raw(t6040_sleep, v); +} +EXPORT_SYMBOL_GPL(apple_sleep_bringup_mark); + +static bool serror_esr; +module_param(serror_esr, bool, 0644); +MODULE_PARM_DESC(serror_esr, "An SError panic mark carries ESR[15:0] instead of the mark's low half"); + +static unsigned int panic_wd_secs = 15; +module_param(panic_wd_secs, uint, 0644); +MODULE_PARM_DESC(panic_wd_secs, + "Bring-up: a panic during a sleep cycle arms the WD0 and WD1 resets this many seconds out (0: off)"); + +static bool panic_msg_hash; +module_param(panic_msg_hash, bool, 0644); +MODULE_PARM_DESC(panic_msg_hash, + "Bring-up: a panic mark carries the CPU and a 24-bit hash of the message, not the last mark"); + +static int t6040_panic_mark(struct notifier_block *nb, unsigned long action, void *data) +{ + struct t6040_sleep *s = t6040_sleep; + + if (s && READ_ONCE(pm_bringup_dev_mark) && s->alarm_ok) { + const char *msg = data; + u32 kind = 0x60000000; + u32 v; + + if (msg && strstr(msg, "SError")) + kind = 0xe0000000; + else if (msg && strstr(msg, "Fatal exception")) + kind = 0xd0000000; + v = (READ_ONCE(t6040_last_mark) & 0x0fffffff) | kind; + /* An SError keeps the mark's family and step, and its syndrome's low half. */ + if (kind == 0xe0000000 && READ_ONCE(serror_esr)) + v = (v & 0xffff0000) | (read_sysreg(esr_el1) & 0xffff); + /* + * Bring-up: which CPU and which message instead of the last mark: + * kind | CPU << 24 | FNV-1a of the first 32 message bytes folded to 24 bits. + */ + if (READ_ONCE(panic_msg_hash)) { + u32 h = 0x811c9dc5; + int i; + + for (i = 0; msg && msg[i] && i < 32; i++) + h = (h ^ (u8)msg[i]) * 0x01000193; + v = kind | (raw_smp_processor_id() & 0xf) << 24 | ((h ^ (h >> 24)) & 0xffffff); + } + + t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); + + /* + * After an S2R wake the SMC has had no 'waka' yet and the 'phra' restart can + * hang (on the J616s an SError panic right after the resume left the Mac dead for + * 12 min, f803 0x50). Arm the WD0 and WD1 resets as a backstop behind the + * normal panic restart; WD0 is the block the SMC logger bites. + */ + if (s->wdt && READ_ONCE(panic_wd_secs)) { + u32 t = min(READ_ONCE(panic_wd_secs), WDT_MAX_SECS) * WDT_TICKS_PER_S; + + writel(0, s->wdt + WDT_WD0_CTL); + writel(0, s->wdt + WDT_WD0_COUNT); + writel(t, s->wdt + WDT_WD0_BITE); + writel(WDT_CTL_RESET_EN, s->wdt + WDT_WD0_CTL); + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(t, s->wdt + WDT_WD1_BITE); + writel(WDT_CTL_RESET_EN, s->wdt + WDT_WD1_CTL); + dsb(sy); + } + } + return NOTIFY_DONE; +} + +static struct notifier_block t6040_panic_nb = { + .notifier_call = t6040_panic_mark, +}; + +/* The wake alarm back, 1 s later, so a reset after this point reads as "the commit ran". */ +static void t6040_mark_restore(struct t6040_sleep *s) +{ + u32 v = s->alarm_saved + 1; + + if (s->alarm_ok) + t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); +} + +/* + * The optional "go hib" replay. Necessity from Linux is unconfirmed. Its second + * pair writes the PMU RTC compare (reg 0x2008); the measured value 0x1e9238 was the + * alarm of that moment and lies in the past now, which would fire the alarm at once. The live alarm + * goes there instead (the one t6040_mark_restore puts back), or nothing is replayed. + */ +#define PMU_GO_HIB_COMPARE_CMD 0x20088e33 + +static void t6040_pmu_go_hib(struct t6040_sleep *s) +{ + u32 status, data; + int i; + + if (!s->spmi0 || !s->alarm_ok) + return; + for (i = 0; i < ARRAY_SIZE(t6040_pmu_go_hib_seq); i++) { + data = t6040_pmu_go_hib_seq[i].cmd == PMU_GO_HIB_COMPARE_CMD ? + s->alarm_saved + 1 : t6040_pmu_go_hib_seq[i].data; + readl(s->spmi0 + SPMI_STATUS); + writel(SPMI_GO_HIB_NOTIFY, s->spmi0 + SPMI_NOTIFY_ENABLE); + writel(t6040_pmu_go_hib_seq[i].cmd, s->spmi0 + SPMI_CMD); + if (t6040_pmu_go_hib_seq[i].has_data) + writel(data, s->spmi0 + SPMI_CMD); + readl_poll_timeout_atomic(s->spmi0 + SPMI_NOTIFY_ACK, status, + status & SPMI_NOTIFY_REPLY, 1, 1000); + writel(1, s->spmi0 + SPMI_NOTIFY_ACK); + } +} + +/* + * The five complexes off, as the quiesce loop turns cluster c off and waits if c > 3, + * for c = 1..5 (measured on the J616s): each slot's low nibble -> 0, in order +0x0 .. +0x10, + * and the last two (+0xc, +0x10) polled until BIT4 clears (+0xc read 0x2f -> + * 0x20 -> 0x10 -> 0x0 there). The CPU complexes (E, P0, P1) go down at the WFI. + */ +static void t6040_clusters_off(struct t6040_sleep *s) +{ + u32 val; + int i; + + if (!s->cluster_ctl) + return; + for (i = 0; i < CLUSTER_CTL_NR; i++) { + unsigned int dc = READ_ONCE(disp_clusters); + + val = readl(s->cluster_ctl + CLUSTER_CTL(i)); + s->cluster_saved[i] = val; + s->cluster_after[i] = val; + if (i >= CLUSTER_CTL_FIRST_WAIT && !dc) + continue; + writel(val & ~CLUSTER_CTL_NIBBLE, s->cluster_ctl + CLUSTER_CTL(i)); + if (i >= CLUSTER_CTL_FIRST_WAIT && dc == 1 && + readl_poll_timeout_atomic(s->cluster_ctl + CLUSTER_CTL(i), val, + !(val & CLUSTER_CTL_BUSY), 1, CLUSTER_TIMEOUT_US)) + s->quiesce_to |= QTO_CLUSTER(i); + s->cluster_after[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); + } +} + +static void t6040_east_clk_quiesce(struct t6040_sleep *s, u32 off, int bit) +{ + u32 val = readl(s->soc_east + off), cur; + + writel(val & ~SOC_EAST_CLK_AUTO, s->soc_east + off); + if (readl_poll_timeout_atomic(s->soc_east + off, cur, + FIELD_GET(PS_ACTUAL, cur) == FIELD_GET(PS_TARGET, val), 1, + CLK_QUIESCE_TIMEOUT_US)) + s->quiesce_to |= QTO_EAST_CLK(bit); +} + +/* The clock quiesce, saving the clk-mux words for wake. */ +static void t6040_clkgen_park(struct t6040_sleep *s) +{ + u32 cur; + int i; + + if (s->soc_east) { + t6040_east_clk_quiesce(s, SOC_EAST_CLK_A, 0); + t6040_east_clk_quiesce(s, SOC_EAST_CLK_B, 1); + } + if (!s->clkgen) + return; + for (i = 0; i < CLKGEN_MUX_WORDS; i++) { + void __iomem *reg = s->clkgen + t6040_clkgen_mux[i]; + u32 val = readl(reg); + + s->clkgen_mux_saved[i] = val; + writel((val & CLKGEN_MUX_MASK) | CLKGEN_MUX_SET, reg); + if (readl_poll_timeout_atomic(reg, cur, !(cur & CLKGEN_MUX_PENDING), 1, + CLK_QUIESCE_TIMEOUT_US)) + s->quiesce_to |= QTO_MUX(i); + } + s->clkgen_saved = true; +} + +/* The mirror on wake: the saved clk-mux words back, last parked first. */ +static void t6040_clkgen_restore(struct t6040_sleep *s) +{ + int i; + + if (!s->clkgen || !s->clkgen_saved) + return; + for (i = CLKGEN_MUX_WORDS - 1; i >= 0; i--) + writel(s->clkgen_mux_saved[i], s->clkgen + t6040_clkgen_mux[i]); + s->clkgen_saved = false; +} + +/* + * Wait for every other core's power word (SOC + 8 * its cpu-start bit index, + * ECPU_0.. PCPU1_5 in the ADT) to read ACTUAL 0. Returns how many stayed on. + */ +static unsigned int t6040_secondaries_off(struct t6040_sleep *s) +{ + u32 self = t6040_cpu_start_bit(read_cpuid_mpidr() & MPIDR_HWID_BITMASK); + unsigned int cpu, on = 0; + u32 v; + + if (!s->soc_ps) + return 0; + for_each_present_cpu(cpu) { + u32 bit = t6040_cpu_start_bit(cpu_logical_map(cpu)); + + if (!bit || bit == self) + continue; + if (readl_poll_timeout_atomic(s->soc_ps + 8 * __ffs(bit), v, + !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + on++; + } + return on; +} + +/* The hardware steps of the generic PMGR quiesce, in the order (see t6040_acc_apsc_pa). */ +static void t6040_pmgr_quiesce(struct t6040_sleep *s) +{ + u64 q; + u32 v; + int i; + + for (i = 0; i < ARRAY_SIZE(t6040_acc_apsc_pa); i++) { + void __iomem *acc = s->acc_apsc[i]; + + if (!acc) + continue; + t6040_mark(s, 0x300 | i); + writeq(readq(acc) | ACC_APSC_DISABLE, acc); + if (readq_poll_timeout_atomic(acc, q, !(q & ACC_APSC_BUSY), 1, QUIESCE_TIMEOUT_US)) + s->quiesce_to |= QTO_APSC(i); + } + for (i = 0; i < ACC_PLL_RELOCK_COMPLEXES; i++) + if (s->acc_apsc[i]) { + t6040_mark(s, 0x310 | i); + writeq(readq(s->acc_apsc[i]) & ~ACC_PLL_RELOCK, s->acc_apsc[i]); + } + for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { + if (!s->tvm_ctl[i] || !s->tvm_status[i]) + continue; + if (t6040_tvm[i].ctl >> 32 == 0x2 && !READ_ONCE(tvm_gpu)) + continue; + t6040_mark(s, 0x320 | i); + writel(readl(s->tvm_ctl[i]) & ~TVM_ENABLE, s->tvm_ctl[i]); + if (readl_poll_timeout_atomic(s->tvm_status[i], v, !(v & TVM_BUSY), 1, + QUIESCE_TIMEOUT_US)) + s->quiesce_to |= QTO_TVM(i); + } + if (s->pmc_ap_floor && READ_ONCE(pmc_floor)) { + t6040_mark(s, 0x330); + writel(readl(s->pmc_ap_floor) & ~PMC_FLOOR_DCS, s->pmc_ap_floor); + if (readl_poll_timeout_atomic(s->pmc_ap_floor + PMC_PENDING, v, + !(v & PMC_PENDING_AP), 1, 8000)) + s->quiesce_to |= QTO_PMC; + } +} + +/* The AGX residency gate of the PMGR quiesce. Returns a mark step for how far it got. */ +static u32 t6040_agx_gate(struct t6040_sleep *s) +{ + u32 gate, val, cur; + + s->afr_saved = 0; + if (!s->agx_gate || !s->afr_ps) + return 0; + t6040_mark(s, 0x340); + gate = readl(s->agx_gate); + if (!(~gate & AGX_GATE_RESIDENT)) + return 1; + t6040_mark(s, 0x341); + val = readl(s->afr_ps); + if (!FIELD_GET(PS_TARGET, val)) + return 2; + s->afr_saved = val; + writel((val & ~AFR_LEVEL) | FIELD_PREP(AFR_LEVEL, 4), s->afr_ps); + if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(AFR_LEVEL, cur) == 4, 1, + AGX_TIMEOUT_US)) + return 3; + if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(PS_ACTUAL, cur) == 4, 1, + AGX_TIMEOUT_US)) + return 4; + writel(0, s->afr_ps); + if (readl_poll_timeout_atomic(s->afr_ps, cur, !FIELD_GET(PS_ACTUAL, cur), 1, + AGX_TIMEOUT_US)) + return 5; + return 6; +} + +/* + * Keep-alive: BIT0 of the 64 keep-alive words, PMGR SOC then SOC-East per + * index. Clears them all (on == false) or sets those in @mask; returns the + * mask of the bits that were set (SOC in the low half, SOC-East in the high). + */ +static u64 t6040_keep_alive(struct t6040_sleep *s, bool on, u64 mask) +{ + u64 was = 0; + int i; + + if (!s->ka_soc || !s->ka_east) + return 0; + for (i = 0; i < KA_WORDS; i++) { + void __iomem *a = s->ka_soc + i * KA_STRIDE; + void __iomem *b = s->ka_east + i * KA_STRIDE; + u32 va = readl(a), vb = readl(b); + + was |= (u64)(va & 1) << i | (u64)(vb & 1) << (i + KA_WORDS); + writel((va & ~1U) | (on && (mask & BIT_ULL(i))), a); + writel((vb & ~1U) | (on && (mask & BIT_ULL(i + KA_WORDS))), b); + } + return was; +} + +/* A domain's power word, from whichever window holds it. */ +static void __iomem *t6040_axi2af_ps(struct t6040_sleep *s, const struct t6040_axi2af_domain *d) +{ + void __iomem *base = d->east ? s->soc_east : s->soc_ps; + + return base ? base + d->ps_off : NULL; +} + +/* The preflight's list of the axi2af domains' power words. */ +static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s) +{ + int i; + + seq_puts(s, "axi2af domains (the quiesce gate wants every one off):\n"); + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { + void __iomem *ps = t6040_axi2af_ps(sleep, &t6040_axi2af[i]); + u32 val = ps ? readl(ps) : 0; + + seq_printf(s, " %-13s %#010x%s\n", t6040_axi2af[i].name, val, + ps ? (FIELD_GET(PS_ACTUAL, val) ? " ON" : "") : " (unmapped)"); + } +} + +/* + * Read-only axi2af gate: on entry the platform panics unless every reachable domain's + * power word reads [7:4]==0. We only log the ones still on, as a safety check. + */ +static void t6040_axi2af_readcheck(struct t6040_sleep *s) +{ + int i, on = 0; + + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { + void __iomem *ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); + u32 val; + + if (!ps) + continue; + val = readl(ps); + if (FIELD_GET(PS_ACTUAL, val)) { + pr_info("t6040-sleep: axi2af %s still on (%#x) at commit\n", + t6040_axi2af[i].name, val); + on++; + } + } + if (on) + pr_info("t6040-sleep: %d axi2af domain(s) still powered at commit\n", on); +} + +/* + * The wake-side axi2af re-arm: every retained domain powered back up (PS + * TARGET == 0xf) gets its fabric bridge +0x104/+0x108 bit0 set, or the bridge + * drops its traffic. The highest-risk wake write. + */ +/* + * On the J616s the first real resume took an asynchronous SError in here (mark 0x36 landed, 0x37 + * did not). Mode 1 re-arms every domain with TARGET 0xf, as the wake restore does; mode 2 also + * wants ACTUAL on, since an AUTO word can read TARGET 0xf with the domain gated, and its + * bridge is then off too; 0 skips the re-arm. Each domain is marked before (0x5eII01VV) and + * after (0x5eII09VV) its bridge writes, VV the PS word's low byte (ACTUAL << 4 | TARGET). + */ +static unsigned int axi2af_rearm_mode = 2; +module_param(axi2af_rearm_mode, uint, 0644); +MODULE_PARM_DESC(axi2af_rearm_mode, + "Bring-up wake: axi2af bridge re-arm 0 off, 1 TARGET 0xf (as the platform), 2 TARGET 0xf and ACTUAL on"); + +static void t6040_axi2af_rearm(struct t6040_sleep *s) +{ + unsigned int mode = READ_ONCE(axi2af_rearm_mode); + int i; + + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { + void __iomem *ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); + void __iomem *bridge = s->axi2af_bridge[i]; + u32 val; + + if (!ps || !bridge) + continue; + val = readl(ps); + if (!mode || FIELD_GET(PS_TARGET, val) != 0xf) + continue; + if (mode >= 2 && !FIELD_GET(PS_ACTUAL, val)) { + pr_info("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", + t6040_axi2af[i].name, val); + continue; + } + dsb(sy); + isb(); + t6040_mark_raw(s, 0x5e000100 | i << 16 | (val & 0xff)); + writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); + dsb(sy); + isb(); + t6040_mark_raw(s, 0x5e000900 | i << 16 | (val & 0xff)); + } +} + +/* + * Bring-up: every AP wake source masked, as LLB and iBoot do before their power-down (the + * same A2D, which completes there). Nothing but a cold boot (the power key) ends that sleep. + */ +static bool wake_mask_all; +module_param(wake_mask_all, bool, 0644); +MODULE_PARM_DESC(wake_mask_all, + "Bring-up: mask all 9 AOP-global wake words as LLB (no wake source: power key only)"); + +/* Arm the 9 AOP-global wake masks before the commit, saving the old words. */ +static void t6040_wake_arm(struct t6040_sleep *s) +{ + bool all = READ_ONCE(wake_mask_all); + int i; + + if (!s->aop_global) + return; + for (i = 0; i < AOP_WAKE_WORDS; i++) { + s->wake_mask_saved[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); + writel(all ? 0xffffffff : t6040_wake_mask[i], s->aop_global + AOP_WAKE_MASK(i)); + } + s->wake_mask_saved_valid = true; +} + +/* Put the wake masks back at .end of a returned (or refused) sleep. */ +static void t6040_wake_restore(struct t6040_sleep *s) +{ + int i; + + if (!s->aop_global || !s->wake_mask_saved_valid) + return; + for (i = 0; i < AOP_WAKE_WORDS; i++) + writel(s->wake_mask_saved[i], s->aop_global + AOP_WAKE_MASK(i)); + s->wake_mask_saved_valid = false; +} + +/* + * ANS's storage-apcie power words (the two ST lanes + ANS) live in SOC-East. + * These helpers drive one power word in a given bank, as #1208's t8140_ps_* + * do in its single PMGR bank: lower is AUTO_ENABLE off then target 0; raise is + * the target, then AUTO_ENABLE once ACTUAL reads on. Writing PS_FLAGS clears + * them. Separate from t6040_sleep_ps_on(), which reads a cached value. + */ +static u32 t6040_ps_lower_start(void __iomem *base, u32 offset) +{ + void __iomem *reg = base + offset; + u32 val = readl(reg); + + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + writel(val, reg); + } + + return val; +} + +static void t6040_ps_lower_finish(void __iomem *base, u32 offset, u32 val) +{ + writel((val & ~PS_TARGET) | PS_FLAGS, base + offset); +} + +static void t6040_ps_lower(void __iomem *base, u32 offset) +{ + t6040_ps_lower_finish(base, offset, t6040_ps_lower_start(base, offset)); +} + +static int t6040_ps_raise_start(void __iomem *base, u32 offset) +{ + void __iomem *reg = base + offset; + u32 now; + + writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET, reg); + + return readl_poll_timeout_atomic(reg, now, (now & PS_ACTUAL) == PS_ACTUAL, 1, + PS_OFF_TIMEOUT_US); +} + +static void t6040_ps_raise_finish(void __iomem *base, u32 offset) +{ + void __iomem *reg = base + offset; + + writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET | PS_AUTO_ENABLE, + reg); +} + +static int t6040_ps_raise(void __iomem *base, u32 offset) +{ + int ret = t6040_ps_raise_start(base, offset); + + t6040_ps_raise_finish(base, offset); + + return ret; +} + +static int t6040_ps_wait_off(void __iomem *base, u32 offset) +{ + u32 val; + + return readl_poll_timeout_atomic(base + offset, val, !FIELD_GET(PS_ACTUAL, val), 10, + PS_OFF_TIMEOUT_US); +} + +static bool t6040_ps_actual_on(void __iomem *base, u32 offset) +{ + return FIELD_GET(PS_ACTUAL, readl(base + offset)); +} + +/* Read-modify-write a tunable in table order, writing only where it changes. */ +static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_tunable *t) +{ + size_t i; + + if (!t) + return; + for (i = 0; i < t->sz; i++) { + void __iomem *reg = regs + t->values[i].offset; + u32 old = readl(reg); + u32 val = (old & ~t->values[i].mask) | (t->values[i].value & t->values[i].mask); + + if (val != old) + writel(val, reg); + } +} + +/* The shared PHY quieted (as T8140's t8140_apcie_phy_off): both clocks off, run bits down. */ +static void t6040_apcie_phy_off(struct t6040_apcie_st *a) +{ + void __iomem *ctrl = a->phy + APCIE_PHY_CTRL; + void __iomem *run = a->phy + APCIE_PHY_RUN; + u32 val; + + writel(readl(ctrl) | APCIE_PHY_CTRL_RESET, ctrl); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK1REQ, ctrl); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK0REQ, ctrl); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_UP, ctrl); + writel(readl(run) & ~APCIE_PHY_RUN_EN2, run); + writel(readl(run) & ~APCIE_PHY_RUN_EN, run); +} + +/* The shared clkgen off (as T8140's t8140_apcie_clk_off). */ +static void t6040_apcie_clk_off(struct t6040_apcie_st *a) +{ + void __iomem *clk = a->clkgen + APCIE_CLK_CTRL; + u32 val; + + writel(readl(clk) & ~APCIE_CLK_RUN, clk); + writel(readl(clk) | APCIE_CLK_OFF, clk); + readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); + writel(readl(clk) | APCIE_CLK_OFF2, clk); +} + +/* + * ENTRY: lower ANS's storage link, the two-lane adaptation of #1208's + * t8140_sleep_lower_apcie(). Called from the commit's lower_io only once ANS + * has been stopped by its driver (pcie_ans_stopped). The PHY/clkgen/axi are + * shared across the lanes; only the two sys_st/st domains are per-lane. + * + * Not known (needs the first S2R on hardware): whether T6040 truly shares the PHY across + * st0/st1 (assumed here) or gates a per-lane PHY, and whether a separate + * ps_apcie_phy_sw domain exists (none in the DT, so none is driven). A wrong + * shared/per-lane split only times out the raise's ready poll, which names the + * failing stage; the power goes next either way. + */ +/* + * The A2D engine (iBoot MCPS seq 24) powers this group down itself, in its own order: + * APCIE_PHY_SW (0x398) to clock-gated, polled for PARENT_MISSING (bit 11) clear, then + * APCIE_SYS_ST0/1 off, APCIE_ST0/1 clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP + * clock-gated. APCIE_PHY_SW's parents are APCIE_SYS_ST0/1 (ADT): with them already off + * the engine waits on PARENT_MISSING for good (on the J616s: MCPS stuck at 0x502280398). + * apcie_st_ps=0 leaves the power words to the engine (the PHY, clock and AXI quiesce + * still run); 1 lowers them here as #1208 does on T8140. + */ +static bool apcie_st_ps; +module_param(apcie_st_ps, bool, 0644); +MODULE_PARM_DESC(apcie_st_ps, "Lower ANS's ST lanes and ANS in the commit (0: leave them to the A2D engine, which needs APCIE_PHY_SW's parents on)"); + +/* + * With apcie_st_ps=0 the engine still waits on APCIE_PHY_SW: right before the timer + * APCIE_ST0/1, ANS and APCIE_SYS_ST0 are on but APCIE_SYS_ST1 is off (Linux never uses the + * second lane), and APCIE_PHY_SW's parents are both SYS_ST lanes. apcie_st1_on raises + * APCIE_SYS_ST1 (target 0xf, no automatic mode) before the request; the engine lowers it. + */ +static bool apcie_st1_on = true; +module_param(apcie_st1_on, bool, 0644); +MODULE_PARM_DESC(apcie_st1_on, "Bring-up: power APCIE_SYS_ST1 on before the S2R request (APCIE_PHY_SW's second parent)"); + +/* + * With both SYS_ST lanes on the engine still waits on APCIE_PHY_SW bit 11. The platform never + * leaves that gate to the engine: its PCIe quiesce puts the shared PHY to sleep (the root + * complex disable), gates APCIE_PHY_SW itself while APCIE_GP is still on (the clock quiesce), + * then shuts the PCIe PLLs down and gates the other seven. + * apcie_seq: bit 0 S1 as the platform; bit 1 APCIE_PHY_SW to clock-gated (the engine's 0x0f000244, + * bit 11 polled clear) then S3 as the platform; bit 2 record 'APCS' + flags + PHY/PS words in PMU SRAM + * 0xa200; bit 3 APCIE_GP to clock-gated (the engine's 0x244) instead of off. + * Flags (word 1): 0 S1 ran, 1 PHY_SW gate ran, 2 S3 ran, 3 ANS stopped, 4 windows valid, + * 5 GP clock-gated; timeouts 8 CLK1ACK, 9 CLK0ACK, 16 PHY_SW bit 11, 17 clkgen busy, 18 GP. + */ +static unsigned int apcie_seq = 11; +module_param(apcie_seq, uint, 0644); +MODULE_PARM_DESC(apcie_seq, "Bring-up: storage PCIe quiesce as the platform (bit 0 PHY sleep, 1 APCIE_PHY_SW gate + PLL off, 2 record at PMU 0xa200, 3 APCIE_GP clock-gated)"); + +#define T6040_APCIE_DIAG_PMU 0xa200 +#define T6040_APCIE_DIAG_MAGIC 0x53435041 /* "APCS" in PMU byte order */ + +/* S1, the root complex disable: the shared PHY and PHY-IP to sleep; the platform spins unbounded. */ +static void t6040_apcie_phy_off(struct t6040_sleep *s) +{ + struct t6040_apcie_st *a = &s->apcie_st; + void __iomem *ctrl = a->phy + APCIE_PHY_CTRL; + void __iomem *run = a->phy + APCIE_PHY_RUN; + u32 val; + + s->apcie_diag[2] = readl(ctrl); + writel(readl(ctrl) | APCIE_PHY_CTRL_UP, ctrl); + udelay(10); + writel(readl(a->phy_ip + 0x38) & ~GENMASK(27, 26), a->phy_ip + 0x38); + writel(readl(a->phy_ip + 0x8c) | GENMASK(27, 24), a->phy_ip + 0x8c); + udelay(5); + writel(readl(ctrl) | APCIE_PHY_CTRL_RESET, ctrl); + udelay(1); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK1REQ, ctrl); + if (readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US)) + s->apcie_diag[1] |= BIT(8); + udelay(1); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK0REQ, ctrl); + if (readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US)) + s->apcie_diag[1] |= BIT(9); + udelay(1); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_UP, ctrl); + udelay(10); + writel(readl(run) & ~APCIE_PHY_RUN_EN2, run); + writel(readl(run) & ~APCIE_PHY_RUN_EN, run); + s->apcie_diag[3] = readl(ctrl); + s->apcie_diag[4] = readl(run); + s->apcie_diag[1] |= BIT(0); +} + +/* S2 step 1, gate idx 7 off: APCIE_PHY_SW gated while APCIE_GP is still on. */ +static void t6040_apcie_phy_sw_gate(struct t6040_sleep *s) +{ + void __iomem *reg = s->soc_east + PS_APCIE_PHY_SW; + u32 val; + + s->apcie_diag[5] = readl(reg); + writel(0x0f000244, reg); + if (readl_poll_timeout_atomic(reg, val, !(val & PS_BUSY), 1, PS_OFF_TIMEOUT_US)) + s->apcie_diag[1] |= BIT(16); + s->apcie_diag[6] = readl(reg); + s->apcie_diag[1] |= BIT(1); +} + +/* S3, the PCIe PLL shutdown: PCIECLKGEN +0 as the platform. */ +static void t6040_apcie_pll_off(struct t6040_sleep *s) +{ + void __iomem *clk = s->apcie_st.clkgen + APCIE_CLK_CTRL; + u32 val; + + writel((readl(clk) & ~GENMASK(5, 3)) | BIT(4) | BIT(3), clk); + writel(readl(clk) | BIT(1), clk); + writel(readl(clk) & ~BIT(0), clk); + if (readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US)) + s->apcie_diag[1] |= BIT(17); + writel(readl(clk) | BIT(2), clk); + s->apcie_diag[7] = readl(clk); + s->apcie_diag[1] |= BIT(2); +} + +/* + * With APCIE_PHY_SW gated the MCPS finished A2D phase 1 (seq 24, marks 0x16/0x17) and + * stalls in seq 25 raising ATC0_COMMON (0x30f, ACTUAL f polled): its parent FAB2_SOC is off at + * the timer, as are FAB1/4/7_SOC (genpd powers fab2_soc off with the ATC commons at + * suspend_noirq). On the platform the fabrics stay up in D, and the D2A polls these 13 words for AUTO 1 + * and target f. fab_on raises each one not in that state (target f, ACTUAL f polled, then + * AUTO) right before the timer and records 'FABS' + raised mask + timeout mask + the 13 words + * in PMU SRAM 0xa240; a timeout also marks 0x54d0MMMM. + */ +/* + * All 13 were already AUTO + target f except SBR and FAB_AFR, but FAB1/2/4/7_SOC read + * 0x1000030f: automatic level 0, so they power-gate when idle (ACTUAL 0) and the engine still + * waits for ATC0_COMMON. Bit 1 sets their automatic level to f (as FAB0/FAB3, 0x1f0000ff) and + * polls ACTUAL f; record word 1/2 bits 16+ name those words. + */ +/* + * The level write does not stick (FAB1/2/4 read back 0x1000030f), but the MCPS got + * through ATC0 (ATC0_PHYMXWRAP on) and now waits for ATC1_COMMON. On the platform the ATC commons are + * still on when the engine runs: seq 25 hands each port to its AON side and then powers the + * common off itself, and the powered children hold FAB2_SOC up. Bit 2 raises FAB2_SOC by hand + * (AUTO off, target f), then ATC0..3_COMMON (target f, no automatic mode), then gives FAB2_SOC + * its AUTO back; 'ATCC' + FAB2 before/after + the 4 commons before/after go to PMU 0xa280, + * timeouts to record word 2 bit 13 (FAB2) and bits 28-31 (ATC0-3). + */ +/* + * With ATC1's dwc3 unbound the MCPS stalled at ATC0 again. Over five runs ATC0's + * hand-over passed exactly when FAB7_SOC (child DISPEXT1_SYS; the PHY mux wrapper carries the + * DISPEXT DP outputs into the ATC PHYs) happened to be up (0x1f0003ff) and stalled when it was + * auto-gated (0x1000030f); bits 27:24 read back the automatic state, they are not a setting. + * Bit 3 holds FAB1/2/4/7_SOC on by hand (AUTO off, target f, ACTUAL f polled) through the + * timer; record word 1/2 bits 16+i name them (bit 1 is not used with it). + */ +static unsigned int fab_on = 5; +module_param(fab_on, uint, 0644); +MODULE_PARM_DESC(fab_on, "Bring-up: fabric PS words before the S2R timer (bit 0 AUTO + target f as the D2A expects, bit 1 automatic level f, bit 2 ATC commons on under FAB2; records at PMU 0xa240/0xa280)"); + +#define T6040_ATC_PMU 0xa280 +#define T6040_ATC_MAGIC 0x43435441 /* "ATCC" in PMU byte order */ +#define PS_FAB2_SOC 0x338 + +static const u16 t6040_atc_common[4] = { 0x390, 0x3b8, 0x3e0, 0x408 }; + +static void t6040_atc_commons_on(struct t6040_sleep *s, u32 *timeouts) +{ + void __iomem *b = s->snap_soc; + u32 rec[11] = { T6040_ATC_MAGIC }; + int i; + + if (!b) + return; + rec[1] = readl(b + PS_FAB2_SOC); + for (i = 0; i < 4; i++) + rec[3 + i] = readl(b + t6040_atc_common[i]); + if (t6040_ps_raise_start(b, PS_FAB2_SOC)) + *timeouts |= BIT(13); + for (i = 0; i < 4; i++) + if (t6040_ps_raise_start(b, t6040_atc_common[i])) + *timeouts |= BIT(28 + i); + t6040_ps_raise_finish(b, PS_FAB2_SOC); + rec[2] = readl(b + PS_FAB2_SOC); + for (i = 0; i < 4; i++) + rec[7 + i] = readl(b + t6040_atc_common[i]); + for (i = 0; i < ARRAY_SIZE(rec); i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_ATC_PMU + 4 * i, SPMI_OP_WRITE4), &rec[i], + NULL, 0); +} + +/* + * On the first real S2R entry the wake program (seq 21) powers the ATC ports back up and + * stalls at port 3: ATCPHY_CFG(3) +0x2c := 1, then +0x04 bit 8 (ACIO80PHY_AHB_CLK_ACK) never + * rises. Ports 0..2 pass the same step. The PHY S2R entry, run on every port before + * sleep, sets +0x2c bit 0 (S2R_AON_SYNC_RST_N), +0x08 bit 0 (CFG_S2R_FSM_EN) and +0x28 bit 0 + * (AHB_CLK_REQ_N) and waits for that ack. atc_cfg_dump logs +0x00/04/08/24/ + * 28/2c of the 4 ports now; atc_s2r (port mask) does the three writes + the ack poll right + * before the timer, after the commons are up (fab_on bit 2). The 'ATCF' record at PMU 0xa240 + * (instead of 'FABS') keeps, per port, +0x04 and a word of +0x08/+0x24/+0x28/+0x2c low bytes. + */ +static unsigned int atc_s2r = 8; +module_param(atc_s2r, uint, 0644); +MODULE_PARM_DESC(atc_s2r, "Bring-up: port mask; the platform aciophy_s2r_entry's CFG writes (+0x2c, +0x08, +0x28 |= 1, poll +0x04 bit 8) right before the S2R timer"); +static bool atc_rec = true; +module_param(atc_rec, bool, 0644); +MODULE_PARM_DESC(atc_rec, "Bring-up: record the 4 ATCPHY_CFG blocks at PMU 0xa240 right before the S2R timer (replaces 'FABS')"); + +#define T6040_ATCF_MAGIC 0x46435441 /* "ATCF" in PMU byte order */ + +static u32 t6040_atc_s2r_entry(struct t6040_sleep *s, unsigned int mask) +{ + u32 to = 0, v; + int n; + + for (n = 0; n < 4; n++) { + void __iomem *c = s->atc_cfg[n]; + + if (!(mask & BIT(n)) || !c) + continue; + writel(readl(c + 0x2c) | BIT(0), c + 0x2c); + udelay(1); + writel(readl(c + 0x08) | BIT(0), c + 0x08); + udelay(5); + writel(readl(c + 0x28) | BIT(0), c + 0x28); + if (readl_poll_timeout_atomic(c + 0x04, v, v & BIT(8), 1, 1000)) + to |= BIT(n); + } + return to; +} + +static void t6040_atc_rec_store(struct t6040_sleep *s, u32 to) +{ + u32 w[16] = { T6040_ATCF_MAGIC }; + int n, i; + + for (n = 0; n < 4; n++) { + void __iomem *c = s->atc_cfg[n]; + + if (!c) + continue; + w[1 + 2 * n] = readl(c + 0x04); + w[2 + 2 * n] = (readl(c + 0x08) & 0xff) | (readl(c + 0x24) & 0xff) << 8 | + (readl(c + 0x28) & 0xff) << 16 | (readl(c + 0x2c) & 0xff) << 24; + } + w[9] = to; + for (i = 0; i < 16; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(0xa240 + 4 * i, SPMI_OP_WRITE4), &w[i], NULL, 0); +} + +static int t6040_atc_cfg_dump_set(const char *val, const struct kernel_param *kp) +{ + struct t6040_sleep *s = t6040_sleep; + int n; + + if (!s) + return -ENODEV; + for (n = 0; n < 4; n++) { + void __iomem *c = s->atc_cfg[n]; + + if (c) + dev_info(s->dev, "atc%d cfg: +0x00 %08x +0x04 %08x +0x08 %08x +0x24 %08x +0x28 %08x +0x2c %08x\n", + n, readl(c), readl(c + 0x04), readl(c + 0x08), readl(c + 0x24), + readl(c + 0x28), readl(c + 0x2c)); + } + return 0; +} + +static const struct kernel_param_ops t6040_atc_cfg_dump_ops = { + .set = t6040_atc_cfg_dump_set, +}; +module_param_cb(atc_cfg_dump, &t6040_atc_cfg_dump_ops, NULL, 0200); +MODULE_PARM_DESC(atc_cfg_dump, "Bring-up: write anything to log the 4 ATCPHY_CFG blocks now"); + +static const struct { + bool east; + u16 off; +} t6040_fabrics[13] = { + { false, 0x110 } /* AFI */, { false, 0x178 } /* AFC */, + { false, 0x448 } /* SBR */, { false, 0x100 } /* PMS */, + { false, 0x168 } /* FAB0_SOC */, { false, 0x2d0 } /* FAB1_SOC */, + { false, 0x338 } /* FAB2_SOC */, { false, 0x440 } /* FAB5_SOC */, + { false, 0x480 } /* FAB6_SOC */, { false, 0x488 } /* FAB7_SOC */, + { true, 0x118 } /* FAB3_SOC */, { true, 0x128 } /* FAB4_SOC */, + { false, 0x298 } /* FAB_AFR */, +}; + +#define PS_AUTO_LEVEL GENMASK(27, 24) +#define T6040_FAB_PMU 0xa240 +#define T6040_FAB_MAGIC 0x53424146 /* "FABS" in PMU byte order */ + +static void t6040_fabrics_on(struct t6040_sleep *s) +{ + u32 rec[16] = { T6040_FAB_MAGIC }, atc_to = 0; + int i; + + for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { + void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; + u32 off = t6040_fabrics[i].off, v; + + if (!b) { + rec[3 + i] = 0xdeadbeef; + continue; + } + v = readl(b + off); + if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != 0xf) { + rec[1] |= BIT(i); + if (t6040_ps_raise(b, off)) + rec[2] |= BIT(i); + } + v = readl(b + off); + if ((READ_ONCE(fab_on) & BIT(1)) && FIELD_GET(PS_AUTO_LEVEL, v) != 0xf) { + rec[1] |= BIT(16 + i); + writel((v & ~(PS_AUTO_LEVEL | PS_FLAGS)) | FIELD_PREP(PS_AUTO_LEVEL, 0xf), + b + off); + if (readl_poll_timeout_atomic(b + off, v, FIELD_GET(PS_ACTUAL, v) == 0xf, 1, + PS_OFF_TIMEOUT_US)) + rec[2] |= BIT(16 + i); + } + rec[3 + i] = readl(b + off); + } + if (READ_ONCE(fab_on) & BIT(2)) + t6040_atc_commons_on(s, &rec[2]); + if (READ_ONCE(atc_s2r) & 0xf) + atc_to = t6040_atc_s2r_entry(s, READ_ONCE(atc_s2r) & 0xf); + if (READ_ONCE(fab_on) & BIT(3)) { + for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { + void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; + u32 off = t6040_fabrics[i].off; + + if (!b || (off != 0x2d0 && off != 0x338 && off != 0x488 && + !(t6040_fabrics[i].east && off == 0x128))) + continue; + rec[1] |= BIT(16 + i); + if (t6040_ps_raise_start(b, off)) + rec[2] |= BIT(16 + i); + } + } + for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { + void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; + + rec[3 + i] = b ? readl(b + t6040_fabrics[i].off) : 0xdeadbeef; + } + if (READ_ONCE(atc_rec)) + t6040_atc_rec_store(s, atc_to); + else + for (i = 0; i < ARRAY_SIZE(rec); i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_FAB_PMU + 4 * i, SPMI_OP_WRITE4), + &rec[i], NULL, 0); + if (rec[2]) + t6040_mark_raw(s, 0x54d00000 | (rec[2] & 0xffff)); +} + +/* + * The MCPS stalls in seq 25's ATC block of port 0 or port 1 at random. Each port n has a + * block at 0x508320c00 + 0x80n (seq 23 writes +0x00..+0x14, seq 21 +0x1c/+0x40/+0x60, seq 25 + * writes +0x60 := 2 and polls +0x00 bit 30). aon_rec stores, right before the S2R timer (instead + * of 'APCS'), 'AONB' + port 0/1 {+0x00, +0x1c, +0x40, +0x60} + port 2/3 {+0x00, +0x60} + + * ATC0/1_PHYMXWRAP + ATC1_USB_AON at PMU 0xa200; only the offsets those programs use are + * read. Writing aon_dump logs the same words now. + */ +static bool aon_rec; +module_param(aon_rec, bool, 0644); +MODULE_PARM_DESC(aon_rec, "Bring-up: record the ATC A2D hand-over blocks at PMU 0xa200 right before the S2R timer (replaces 'APCS')"); + +#define T6040_AON_MAGIC 0x424e4f41 /* "AONB" in PMU byte order */ + +static void t6040_aon_take(struct t6040_sleep *s, u32 *w) +{ + static const u16 off[12] = { 0x000, 0x01c, 0x040, 0x060, 0x080, 0x09c, 0x0c0, 0x0e0, + 0x100, 0x160, 0x180, 0x1e0 }; + int i; + + w[0] = T6040_AON_MAGIC; + for (i = 0; i < 12; i++) + w[1 + i] = s->atc_aon ? readl(s->atc_aon + off[i]) : 0xdeadbeef; + w[13] = s->mini_ps ? readl(s->mini_ps + 0x150) : 0xdeadbeef; + w[14] = s->mini_ps ? readl(s->mini_ps + 0x158) : 0xdeadbeef; + w[15] = s->mini_ps ? readl(s->mini_ps + 0x0d0) : 0xdeadbeef; +} + +static void t6040_aon_store(struct t6040_sleep *s) +{ + u32 w[16]; + int i; + + t6040_aon_take(s, w); + for (i = 0; i < 16; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], + NULL, 0); +} + +static int t6040_aon_dump_set(const char *val, const struct kernel_param *kp) +{ + u32 w[16]; + + if (!t6040_sleep) + return -ENODEV; + t6040_aon_take(t6040_sleep, w); + dev_info(t6040_sleep->dev, + "aon: p0 %08x %08x %08x %08x p1 %08x %08x %08x %08x p2 %08x %08x p3 %08x %08x mxwrap0 %08x mxwrap1 %08x usb_aon1 %08x\n", + w[1], w[2], w[3], w[4], w[5], w[6], w[7], w[8], w[9], w[10], w[11], w[12], w[13], + w[14], w[15]); + return 0; +} + +static const struct kernel_param_ops t6040_aon_dump_ops = { + .set = t6040_aon_dump_set, +}; +module_param_cb(aon_dump, &t6040_aon_dump_ops, NULL, 0200); +MODULE_PARM_DESC(aon_dump, "Bring-up: write anything to log the ATC A2D hand-over blocks now"); + +/* + * A2D completes; the engine stalls in D2S (seq 28) polling the AMCC0 DCS pair's power + * gate (0x50282c918 bit 30). On this revision the program never writes the DCS power words: the + * DCS gate themselves after self-refresh, only in dynamic power-gate mode (DCS+0x20 bit 12, bit 8 + * enable). The platform keeps that mode on at runtime and re-applies it after each wake + * (DCS dynamic power-gate mode 1: RMW +0x20 mask 0x1100 := 0x1100 on + * all 16 DCS); Linux never sets it. Only the offsets the platform and the engine use are read. + * dcs_dump logs them now; dcs_pg bit 0 applies the the platform mode now (on write) and again in + * .begin; dcs_rec stores 'DCSR' + the DCS +0x20 low halves + the 4 AMCC handshake words + the + * 8 pwrgate CFG0 top bytes + the DCS +0x500 bits [1:0] at PMU 0xa200 right before the timer. + */ +#define T6040_DCS_MAGIC 0x52534344 /* "DCSR" in PMU byte order */ +static const u16 t6040_pg_main_off[4] = { 0x918, 0x998, 0xa18, 0xa98 }; +static const u16 t6040_pg_east_off[4] = { 0xa00, 0xa80, 0xb00, 0xb80 }; + +static bool dcs_rec; +module_param(dcs_rec, bool, 0644); +MODULE_PARM_DESC(dcs_rec, "Bring-up: record the DCS power-gate state at PMU 0xa200 right before the S2R timer (replaces 'AONB'/'APCS')"); + +/* + * D2S still stalls with dynamic power gating on and the AOP/PMP/MTP ruled out. + * #1208's T8140 commit stops each memory channel right before its gates at + * about -18 ms (t8140_sleep_mcc_stop): at DCS +0x580 ("mcc0..2" = 0x2_2040_0580 + + * ch * 0x20_0000, the DCS slices' spacing) the enables word +0x4 cleared one bit at a time, + * then lanes 0..3 strobed and cleared on +0x8 (mask word +0xc), two lanes per channel per + * pass. The T6040 commit never did it (the measurement behind "the platform writes none" did not + * cover these pages). dcs_mcc_stop bit 0 runs that sequence on all 16 DCS right before the S2R + * timer; 'DCSR' words 10/11 then hold DCS 0.0 +0x584/+0x588 after it. + */ +static unsigned int dcs_mcc_stop; +module_param(dcs_mcc_stop, uint, 0644); +MODULE_PARM_DESC(dcs_mcc_stop, "Bring-up: bit 0 = T8140 mcc_stop (DCS +0x584 enables, +0x588 lanes) on all 16 DCS right before the S2R timer"); + +static void t6040_dcs_mcc_stop(struct t6040_sleep *s) +{ + int i, pass, n; + u32 val; + + for (i = 0; i < 16; i++) { + void __iomem *m = s->dcs[i] ? s->dcs[i] + 0x580 : NULL; + + if (!m) + continue; + val = readl(m + 0x4); + for (n = 0; val && n < 32; n++) { + val &= val - 1; + writel(val, m + 0x4); + val = readl(m + 0x4); + } + } + for (pass = 0; pass < 2; pass++) { + for (i = 0; i < 16; i++) { + void __iomem *m = s->dcs[i] ? s->dcs[i] + 0x580 : NULL; + + if (!m) + continue; + for (n = 2 * pass; n < 2 * pass + 2; n++) { + writel(readl(m + 0xc), m + 0xc); + writel(readl(m + 0x8) | BIT(16 + 4 * n), m + 0x8); + writel(readl(m + 0x8) & ~BIT(4 * n), m + 0x8); + } + } + } +} + +/* + * On this revision the D2S program does not write the DCS power-state words; it expects + * them in automatic mode (bit 28) with PS_MIN 0, so each DCS domain drops to 0 after + * self-refresh and its pwrgate block reports bit 30. The platform sets that at boot + * (0x0f0000ff -> 0x1f0000ff) on all 16 DCS PS words; the Linux + * DT leaves the ps_dcs_* domains disabled, so they stay at target f with AUTO off. dcs_dump + * logs them and the 8 AMCC PS words; dcs_ps_auto bit 0 sets AUTO now (the platform order: PS_MIN + * cleared, target f, ACTUAL f polled, then AUTO) and again in .begin. + */ +static const struct { + bool east; + u16 off; +} t6040_dcs_ps[16] = { + { false, 0x188 }, { false, 0x1a0 }, { false, 0x1b0 }, { false, 0x1c0 }, + { true, 0x2e0 }, { true, 0x2e8 }, { true, 0x2f0 }, { true, 0x2f8 }, + { false, 0x1d0 }, { false, 0x1e0 }, { false, 0x1f0 }, { false, 0x200 }, + { true, 0x300 }, { true, 0x308 }, { true, 0x310 }, { true, 0x318 }, +}; +static const u16 t6040_amcc_ps[8] = { 0x180, 0x198, 0x1a8, 0x1b8, 0x1c8, 0x1d8, 0x1e8, 0x1f8 }; + +static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) +{ + void __iomem *b = t6040_dcs_ps[i].east ? s->snap_east : s->snap_soc; + + return b ? b + t6040_dcs_ps[i].off : NULL; +} + +static unsigned int dcs_ps_auto = 1; + +/* Returns a mask of the DCS whose ACTUAL did not reach f before AUTO was set. */ +static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) +{ + u32 to = 0, v; + int i; + + for (i = 0; i < 16; i++) { + void __iomem *r = t6040_dcs_ps_reg(s, i); + + if (!r) + continue; + v = readl(r); + if ((v & PS_AUTO_ENABLE) && !(v & GENMASK(19, 16))) + continue; + writel((v & ~(GENMASK(19, 16) | PS_FLAGS | PS_TARGET)) | 0xf, r); + if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v) == 0xf, 1, + PS_OFF_TIMEOUT_US)) + to |= BIT(i); + writel(readl(r) | PS_AUTO_ENABLE, r); + } + return to; +} + +static int t6040_dcs_ps_auto_set(const char *val, const struct kernel_param *kp) +{ + int ret = param_set_uint(val, kp); + + if (!ret && (dcs_ps_auto & BIT(0)) && t6040_sleep) + dev_info(t6040_sleep->dev, "DCS PS automatic mode on (the platform boot parity), timeouts %#x\n", + t6040_dcs_ps_auto_on(t6040_sleep)); + return ret; +} + +static const struct kernel_param_ops t6040_dcs_ps_auto_ops = { + .set = t6040_dcs_ps_auto_set, + .get = param_get_uint, +}; +module_param_cb(dcs_ps_auto, &t6040_dcs_ps_auto_ops, &dcs_ps_auto, 0644); +MODULE_PARM_DESC(dcs_ps_auto, "Bring-up: bit 0 = DCS power-state words to automatic mode (PS_MIN 0) now and in .begin, as the platform at boot"); + +static void t6040_dcs_take(struct t6040_sleep *s, u32 *w) +{ + int i; + + memset(w, 0, 16 * sizeof(u32)); + w[0] = T6040_DCS_MAGIC; + for (i = 0; i < 16; i++) { + u32 v = s->dcs[i] ? readl(s->dcs[i] + 0x20) : 0xdead; + u32 b = s->dcs[i] ? readl(s->dcs[i] + 0x500) : 0; + + w[1 + i / 2] |= (v & 0xffff) << (16 * (i % 2)); + w[15] |= (b & 3) << (2 * i); + } + for (i = 0; i < 4; i++) { + if (s->amcc_sem[i]) + w[9] |= (readl(s->amcc_sem[i] + 0x28) & 1) << i; + if (s->pg_main) + w[13] |= (readl(s->pg_main + t6040_pg_main_off[i]) >> 24) << (8 * i); + if (s->pg_east) + w[14] |= (readl(s->pg_east + t6040_pg_east_off[i]) >> 24) << (8 * i); + } + /* The AOP island's DRAM request (0x5108b8100 bit 0) and MasterMini+0x14. */ + if (s->aop_req) + w[9] |= (readl(s->aop_req + 0x100) & 0xff) << 8; + if (s->mastermini) + w[9] |= (readl(s->mastermini + 0x14) & 0xffff) << 16; + /* DCS_00 and DCS_04 PS words, AMCC0 PS word (instead of the AOP ASC). */ + w[10] = t6040_dcs_ps_reg(s, 0) ? readl(t6040_dcs_ps_reg(s, 0)) : 0xdeadbeef; + w[11] = t6040_dcs_ps_reg(s, 4) ? readl(t6040_dcs_ps_reg(s, 4)) : 0xdeadbeef; + w[12] = s->snap_soc ? readl(s->snap_soc + t6040_amcc_ps[0]) : 0xdeadbeef; +} + +/* + * With dynamic power gating on (0x1100 on all 16 DCS) and the DRAM MCU handshake done + * for all 4 AMCC, the DCS still do not gate: something keeps using DRAM. The AOP runs through + * A2D/D2S and has a DRAM request bit (0x5108b8100 bit 0). aop_halt bit 0 clears the AOP ASC RUN + * bit right after the 'DCSR' record, just before the S2R timer (diagnostic: the AOP island stays + * on; a wake then has no AOP). Record word 12 = CPU_CONTROL after the halt. + */ +static unsigned int aop_halt; +module_param(aop_halt, uint, 0644); +MODULE_PARM_DESC(aop_halt, "Bring-up: bit 0 = stop the AOP CPU right before the S2R timer (diagnostic)"); + +static void t6040_dcs_store(struct t6040_sleep *s) +{ + u32 w[16]; + int i; + + t6040_dcs_take(s, w); + if ((READ_ONCE(dcs_mcc_stop) & BIT(0)) && s->dcs[0]) { + t6040_dcs_mcc_stop(s); + w[10] = readl(s->dcs[0] + 0x584); + w[11] = readl(s->dcs[0] + 0x588); + } + if ((READ_ONCE(aop_halt) & BIT(0)) && s->aop_asc) { + writel(readl(s->aop_asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, + s->aop_asc + ASC_CPU_CONTROL); + udelay(10); + w[12] = readl(s->aop_asc + ASC_CPU_CONTROL) | (readl(s->aop_req + 0x100) & 0xff) << 24; + } + for (i = 0; i < 16; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], + NULL, 0); +} + +static int t6040_dcs_dump_set(const char *val, const struct kernel_param *kp) +{ + struct t6040_sleep *s = t6040_sleep; + int i; + + if (!s) + return -ENODEV; + for (i = 0; i < 16; i++) + if (s->dcs[i]) + dev_info(s->dev, "dcs %d.%d: +0x20 %08x +0x500 %08x +0x1d48 %08x +0x580 %08x %08x %08x %08x\n", + i / 4, i % 4, readl(s->dcs[i] + 0x20), readl(s->dcs[i] + 0x500), + readl(s->dcs[i] + 0x1d48), readl(s->dcs[i] + 0x580), + readl(s->dcs[i] + 0x584), readl(s->dcs[i] + 0x588), + readl(s->dcs[i] + 0x58c)); + for (i = 0; i < 4; i++) + dev_info(s->dev, "amcc %d: +0x1c0028 %08x pg main %08x east %08x\n", i, + s->amcc_sem[i] ? readl(s->amcc_sem[i] + 0x28) : 0xdeadbeef, + s->pg_main ? readl(s->pg_main + t6040_pg_main_off[i]) : 0xdeadbeef, + s->pg_east ? readl(s->pg_east + t6040_pg_east_off[i]) : 0xdeadbeef); + for (i = 0; i < 16; i++) + dev_info(s->dev, "dcs ps %d: %08x\n", i, + t6040_dcs_ps_reg(s, i) ? readl(t6040_dcs_ps_reg(s, i)) : 0xdeadbeef); + for (i = 0; i < 8; i++) + dev_info(s->dev, "amcc ps %d: %08x\n", i, + s->snap_soc ? readl(s->snap_soc + t6040_amcc_ps[i]) : 0xdeadbeef); + dev_info(s->dev, "aop: req %08x mastermini+0x14 %08x asc ctl %08x status %08x\n", + s->aop_req ? readl(s->aop_req + 0x100) : 0xdeadbeef, + s->mastermini ? readl(s->mastermini + 0x14) : 0xdeadbeef, + s->aop_asc ? readl(s->aop_asc + ASC_CPU_CONTROL) : 0xdeadbeef, + s->aop_asc ? readl(s->aop_asc + ASC_CPU_STATUS) : 0xdeadbeef); + return 0; +} + +static const struct kernel_param_ops t6040_dcs_dump_ops = { + .set = t6040_dcs_dump_set, +}; +module_param_cb(dcs_dump, &t6040_dcs_dump_ops, NULL, 0200); +MODULE_PARM_DESC(dcs_dump, "Bring-up: write anything to log the DCS power-gate state now"); + +static unsigned int dcs_pg = 1; + +static void t6040_dcs_dyn_pg(struct t6040_sleep *s) +{ + int i; + + for (i = 0; i < 16; i++) { + u32 v; + + if (!s->dcs[i]) + continue; + v = readl(s->dcs[i] + 0x20); + if ((v & 0x1100) != 0x1100) + writel(v | 0x1100, s->dcs[i] + 0x20); + } +} + +static int t6040_dcs_pg_set(const char *val, const struct kernel_param *kp) +{ + int ret = param_set_uint(val, kp); + + if (!ret && (dcs_pg & BIT(0)) && t6040_sleep) { + t6040_dcs_dyn_pg(t6040_sleep); + dev_info(t6040_sleep->dev, "DCS dynamic power-gate mode on (the platform parity)\n"); + } + return ret; +} + +static const struct kernel_param_ops t6040_dcs_pg_ops = { + .set = t6040_dcs_pg_set, + .get = param_get_uint, +}; +module_param_cb(dcs_pg, &t6040_dcs_pg_ops, &dcs_pg, 0644); +MODULE_PARM_DESC(dcs_pg, "Bring-up: bit 0 = DCS dynamic power-gate mode on (+0x20 |= 0x1100 on all 16 DCS) now and in .begin"); + +/* Words 8-15 right before the S2R timer, then the whole record to PMU SRAM 0xa200. */ +static void t6040_apcie_diag_store(struct t6040_sleep *s) +{ + static const u16 w[8] = { PS_APCIE_PHY_SW, PS_APCIE_SYS_ST0, PS_APCIE_SYS_ST1, + PS_APCIE_ST0, PS_APCIE_ST1, PS_ANS, PS_APCIE_SYS_GP, + PS_APCIE_GP }; + int i; + + s->apcie_diag[0] = T6040_APCIE_DIAG_MAGIC; + for (i = 0; i < 8; i++) + s->apcie_diag[8 + i] = s->soc_east ? readl(s->soc_east + w[i]) : 0xdeadbeef; + for (i = 0; i < ARRAY_SIZE(s->apcie_diag); i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), + &s->apcie_diag[i], NULL, 0); +} + +static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) +{ + struct t6040_apcie_st *a = &s->apcie_st; + void __iomem *ps = s->soc_east; + unsigned int mac = READ_ONCE(apcie_seq); + + if (!a->valid || !ps) + return; + + /* The PHY's registers need a lane domain on; quiet it first (shared). */ + if (a->phy && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) { + if (mac & BIT(0)) + t6040_apcie_phy_off(s); + else + t6040_apcie_phy_off(a); + } + if (mac & BIT(1)) + t6040_apcie_phy_sw_gate(s); + if (a->clkgen && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) { + if (mac & BIT(1)) + t6040_apcie_pll_off(s); + else + t6040_apcie_clk_off(a); + } + + /* Per-lane: both sys_st then both st, deepest first. */ + if (READ_ONCE(apcie_st_ps)) { + t6040_ps_lower(ps, PS_APCIE_SYS_ST1); + t6040_ps_lower(ps, PS_APCIE_SYS_ST0); + t6040_ps_lower(ps, PS_APCIE_ST1); + t6040_ps_lower(ps, PS_APCIE_ST0); + } + + /* AXI enables off (shared), then ANS last. */ + if (a->axi) { + writel(readl(a->axi + APCIE_AXI_EN0) & ~BIT(0), a->axi + APCIE_AXI_EN0); + writel(readl(a->axi + APCIE_AXI_EN1) & ~BIT(0), a->axi + APCIE_AXI_EN1); + } + if (READ_ONCE(apcie_st_ps)) + t6040_ps_lower(ps, PS_ANS); + /* The caller polls PS_ANS off, as #1208 does. */ +} + +/* + * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of + * #1208's t8140_sleep_raise_apcie(). Runs atomic on the waking core in + * t6040_sleep_return(), AFTER t6040_axi2af_rearm() (the ANS/apcie_sys fabric + * bridges must be re-armed before link traffic) and BEFORE control returns to + * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. + * The two lanes come up together; the PLL/clkgen/PHY tables run once (shared). + * + * Not known (first S2R on hardware): whether ANS trains on one lane or needs both, and whether + * the two apcie_sys_st domains need their own axi2af bridge re-arm (they are NOT + * in the 15-domain axi2af workaround table, so none is done here). + */ +/* + * On the J616s the resume took an SError between marks 0x37 and 0x38, in here: the AXI enables + * (+0x104/+0x108 of 0x4_1600_0000) are the axi2af bridge of APCIE_SYS_GP, which the commit + * (apcie_seq) left off together with GP, and nothing raised it. The root complex enable: + * gates idx 0..6 = ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1 in that + * order, each to ACTUAL 0xf, setting the axi2af bit0 after ANS and SYS_GP; the PLLs; then gate + * idx 7 = APCIE_PHY_SW (which the commit gated) before the PHY, and "refpll_clk_good" (PHY + * common +0 bit31) before the PHY init. apcie_wake bit0: that domain order and both bridges; + * bit1: APCIE_PHY_SW on and the refpll wait. Marks 0x50..0x55 follow the steps. + */ +static unsigned int apcie_wake = 7; +module_param(apcie_wake, uint, 0644); +MODULE_PARM_DESC(apcie_wake, + "Bring-up wake: bit0 the platform gate order ANS,GP,SYS_GP,ST0,SYS_ST0,ST1,SYS_ST1 + axi2af bits; bit1 PHY_SW on + refpll wait; bit2 SYS_GP without AUTO"); + +static void t6040_bridge_rearm(void __iomem *bridge) +{ + if (!bridge) + return; + writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); +} + +static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) +{ + struct t6040_apcie_st *a = &s->apcie_st; + void __iomem *ps = s->soc_east; + void __iomem *ctrl, *run; + unsigned int wake = READ_ONCE(apcie_wake); + u32 val; + int ret; + + if (!a->valid || !ps) + return 0; + ctrl = a->phy + APCIE_PHY_CTRL; + run = a->phy + APCIE_PHY_RUN; + + if (wake & BIT(0)) { + ret = t6040_ps_raise(ps, PS_ANS); + if (!ret) + t6040_bridge_rearm(s->axi2af_bridge[AXI2AF_ANS]); + ret |= t6040_ps_raise(ps, PS_APCIE_GP); + /* + * The 'APCW' record: after the raise SYS_GP read 0x1000030f (AUTO on, auto state 0, + * ACTUAL 0): AUTO gated it again and the ANS apcie DART (0x4_1000_0000) behind it + * took the SError in its noirq resume. bit2 leaves SYS_GP in manual target 0xf, + * as GP/ST0/ST1 read (0x2ff). + */ + if (!ret) + ret = (wake & BIT(2)) ? t6040_ps_raise_start(ps, PS_APCIE_SYS_GP) : + t6040_ps_raise(ps, PS_APCIE_SYS_GP); + if (!ret) { + writel(readl(a->axi + APCIE_AXI_EN0) | BIT(0), a->axi + APCIE_AXI_EN0); + writel(readl(a->axi + APCIE_AXI_EN1) | BIT(0), a->axi + APCIE_AXI_EN1); + } + ret |= t6040_ps_raise(ps, PS_APCIE_ST0); + ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST0); + ret |= t6040_ps_raise(ps, PS_APCIE_ST1); + ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST1); + t6040_ret_mark(s, 0x50); + s->apcie_wake_step = 1; + if (ret) + return ret; + } else { + /* Domains up: ANS first, then both lanes (st then sys_st), AXI enables. */ + ret = t6040_ps_raise(ps, PS_ANS); + ret |= t6040_ps_raise(ps, PS_APCIE_ST0); + ret |= t6040_ps_raise(ps, PS_APCIE_ST1); + ret |= t6040_ps_raise_start(ps, PS_APCIE_SYS_ST0); + ret |= t6040_ps_raise_start(ps, PS_APCIE_SYS_ST1); + if (!ret) { + writel(readl(a->axi + APCIE_AXI_EN0) | BIT(0), a->axi + APCIE_AXI_EN0); + writel(readl(a->axi + APCIE_AXI_EN1) | BIT(0), a->axi + APCIE_AXI_EN1); + } + t6040_ps_raise_finish(ps, PS_APCIE_SYS_ST0); + t6040_ps_raise_finish(ps, PS_APCIE_SYS_ST1); + if (ret) + return ret; + } + + /* Tunables + clkgen (shared). Both OFF bits clear; only a power-off cleared them. */ + apple_tunable_apply(a->axi, a->axi_tunable); + apple_tunable_apply(a->cio3pll, a->cio3pll_tunable); + apple_tunable_apply(a->clkgen, a->clkgen_tunable); + writel(readl(a->clkgen + APCIE_CLK_START) | APCIE_CLK_START_GO, + a->clkgen + APCIE_CLK_START); + writel(readl(a->clkgen + APCIE_CLK_CTRL) & ~(APCIE_CLK_OFF2 | APCIE_CLK_OFF), + a->clkgen + APCIE_CLK_CTRL); + ret = readl_poll_timeout_atomic(a->clkgen + APCIE_CLK_CTRL, val, + !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); + if (wake) + t6040_ret_mark(s, 0x51); + s->apcie_wake_step = 2; + if (ret) + return ret; + + if (wake & BIT(1)) { + ret = t6040_ps_raise(ps, PS_APCIE_PHY_SW); + s->apcie_wake_step = 3; + if (ret) + return ret; + } + + /* Shared PHY bring-up + PLL/lane tables (as T8140's t8140_apcie_phy_ip_apply). */ + apple_tunable_apply(a->phy, a->phy_tunable); + if (wake & BIT(1)) { + ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_COMMON, val, val & BIT(31), 1, + APCIE_POLL_US); + t6040_ret_mark(s, 0x52); + s->apcie_wake_step = 4; + if (ret) + return ret; + } + writel(readl(ctrl) | APCIE_PHY_CTRL_CLK0REQ, ctrl); + ret = readl_poll_timeout_atomic(ctrl, val, val & APCIE_PHY_CTRL_CLK0ACK, 1, APCIE_POLL_US); + if (ret) + return ret; + writel(readl(ctrl) | APCIE_PHY_CTRL_CLK1REQ, ctrl); + ret = readl_poll_timeout_atomic(ctrl, val, val & APCIE_PHY_CTRL_CLK1ACK, 1, APCIE_POLL_US); + if (ret) + return ret; + writel(readl(ctrl) & ~APCIE_PHY_CTRL_RESET, ctrl); + if (wake) + t6040_ret_mark(s, 0x53); + s->apcie_wake_step = 5; + + writel(readl(run) | APCIE_PHY_RUN_EN, run); + t6040_tunable_apply_changed(a->phy_ip, a->phy_ip_pll); + t6040_tunable_apply_changed(a->phy_ip, a->phy_ip_lanes); + writel(readl(a->phy_ip + APCIE_PHY_IP_PLL_CTL) & ~APCIE_PHY_IP_PLL_HOLD, + a->phy_ip + APCIE_PHY_IP_PLL_CTL); + writel(readl(run) | APCIE_PHY_RUN_EN2, run); + ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_READY, val, val & BIT(0), 1, + APCIE_POLL_US); + if (ret) + return ret; + if (wake) + t6040_ret_mark(s, 0x54); + s->apcie_wake_step = 6; + writel(readl(a->phy + APCIE_PHY_COMMON) | BIT(0), a->phy + APCIE_PHY_COMMON); + writel(readl(ctrl) | APCIE_PHY_CTRL_UP, ctrl); + if (wake) + t6040_ret_mark(s, 0x55); + s->apcie_wake_step = 7; + + /* RC ready handshake (as T8140's). */ + writel(0, a->rc + APCIE_RC_ZERO); + writel(0x140, a->rc + APCIE_RC_READY_CFG); + writel(1, a->rc + APCIE_RC_READY_START); + return readl_poll_timeout_atomic(a->rc + APCIE_RC_READY_STAT, val, val & BIT(0), 1, + APCIE_POLL_US); +} + +/* + * The real commit (t6040_sleep_commit, reached through cpu_suspend). COMPILED + * BUT UNREACHABLE: t6040_sleep_enter calls it only when no blocker remains and + * allow_entry is set, and the missing-data blockers (t6040_sleep_unknowns) + * never clear in this kernel. The SoC power-down here is the T6040 entry as + * measured on the J616s, which is SHORT: unlike #1208's T8140 commit there + * is NO dcs_down / mcc_stop / pwrgate / PTD / clk-mux-per-lane / CLLT / dvfm / + * AMCC / ps_seq list (the platform writes none of them). The + * order is the, as measured: WD0 off, PMU boot-stage 0x50 + * + NOTIFY over spmi0/spmi-a0, the AON 5 s net, the three clusters off, the + * clk-mux park, the axi2af read-check, the AURAS2R1 mailbox, the MasterMini S2R + * timer (0x508904000 := 1), the AIC masked, the parked CPUs released, the + * optional PMU "go hib" replay, and the deep WFI. All of it is measured except the + * clk-mux park (not observed) and the go-hib replay (measured bytes, necessity unproven). + * A hang here ends at WD2. + */ +/* + * The first thing Linux runs after a wake, MMU off and no stack (#1314's T8132 stub). The + * last wake program leaves the AON WD2 running for 5 s, less than the device resume takes: + * restart it for 60 s, in t6040_sleep_set_wd2()'s order, then take cpu_resume. The PA is + * the T6040 AON WDT (0x50836c000), used only when the DT's wdt block is that one. + */ +#define T6040_WDT_PA 0x50836c000ULL +asm( +" .pushsection .text, \"ax\"\n" +" .type t6040_resume_stub, %function\n" +"t6040_resume_stub:\n" +" movz x9, #0xc000\n" +" movk x9, #0x0836, lsl #16\n" +" movk x9, #0x5, lsl #32\n" +" str wzr, [x9, #0x2c]\n" +" str wzr, [x9, #0x20]\n" +" movz w10, #0xa800\n" +" movk w10, #0x55d4, lsl #16\n" +" str w10, [x9, #0x28]\n" +" mov w10, #1\n" +" str w10, [x9, #0x2c]\n" +" b cpu_resume\n" +" .size t6040_resume_stub, . - t6040_resume_stub\n" +" .popsection\n" +); +extern char t6040_resume_stub[]; + +static bool resume_stub = true; +module_param(resume_stub, bool, 0644); +MODULE_PARM_DESC(resume_stub, + "Wake into a stub that restarts the AON WD2 for 60 s before cpu_resume (default on)"); + +static u32 t6040_axi2af_on_mask(struct t6040_sleep *s); + +static bool lower_atc = true; +module_param(lower_atc, bool, 0644); +MODULE_PARM_DESC(lower_atc, "Lower the four USB-C (ATC) complexes in the commit off"); + +/* + * The four ATC complexes' power words in SOC (soc_ps, 0x502800000), deepest first: cio_usb, + * cio_pcie, cio, pcie, common (ADT: ATCn_CIO_USB .. ATCn_COMMON). The platform has them off at its + * quiesce gate; their drivers don't lower atcN_common. + * + * Their common_dp, usb, phymxwrap and usb_aon words are MiniPMGR words (0x508300000 + 0xa0.., + * ADT ATCn_COMMON_DP/USB/PHYMXWRAP/USB_AON), not SOC ones: applied to soc_ps, + * those offsets lower SOC AIC (0x118), PMC (0x120), PMS_FPWM0-4 and + * FAB0_SOC (0x168) right before the S2R request; the dead PMC then blocks the A2D engine's + * PMC writes (an AP read of the PMC hangs from that step on). + */ +static const u16 t6040_atc_words[4][5] = { + { 0x3b0, 0x3a8, 0x398, 0x3a0, 0x390 }, + { 0x3d8, 0x3d0, 0x3c0, 0x3c8, 0x3b8 }, + { 0x400, 0x3f8, 0x3e8, 0x3f0, 0x3e0 }, + { 0x438, 0x430, 0x420, 0x428, 0x408 }, +}; + +/* As the platform lowers a domain (T8140 measured): AUTO_ENABLE off first, then target 0, poll. */ +static int t6040_ps_lower_word(void __iomem *reg) +{ + u32 val = readl(reg); + + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + writel(val, reg); + } + writel((val & ~PS_TARGET) | PS_FLAGS, reg); + return readl_poll_timeout_atomic(reg, val, !FIELD_GET(PS_ACTUAL, val), 1, + PS_OFF_TIMEOUT_US); +} + +/* Marks 0x57PKSOOO: ATC P, word K, S 0 written / 8 off / 4 still on, OOO its offset. */ +static void t6040_sleep_lower_atc(struct t6040_sleep *s) +{ + int p, k; + + if (!s->soc_ps) + return; + for (p = 0; p < ARRAY_SIZE(t6040_atc_words); p++) + for (k = 0; k < ARRAY_SIZE(t6040_atc_words[0]); k++) { + u32 off = t6040_atc_words[p][k]; + void __iomem *reg = s->soc_ps + off; + u32 val = readl(reg); + u32 m = 0x57000000 | p << 20 | k << 16 | off; + + if (!FIELD_GET(PS_TARGET, val) && !FIELD_GET(PS_ACTUAL, val)) + continue; + t6040_mark_raw(s, m); + t6040_mark_raw(s, m | (t6040_ps_lower_word(reg) ? 0x4000 : 0x8000)); + } +} + +static bool lower_apcie_gp = true; +module_param(lower_apcie_gp, bool, 0644); +MODULE_PARM_DESC(lower_apcie_gp, "Stop the GP PCIe ports and lower apcie_sys_gp in the commit (T8140's radio port stop)"); + +/* + * T8140's radio port stop, on the same t602x port block: the link into L2, the endpoint's + * PERST# asserted, the PHY's clocks off. The endpoint keeps its power and comes back by its + * host after the sleep. Marks 0x541P00RR: port P, RR 1 started, 2 done. + */ +static void t6040_gp_port_stop(struct t6040_sleep *s, int p) +{ + void __iomem *port = s->gp_port[p]; + void __iomem *phy = s->gp_phy[p]; + u32 val; + + if (!port || !phy) + return; + s->gp_phy_saved[p] = readl(phy); + s->gp_appclk_saved[p] = readl(port + GP_PORT_APPCLK); + s->gp_perst_saved[p] = readl(port + GP_PORT_PERST); + s->gp_saved[p] = true; + t6040_mark_raw(s, 0x54100001 | p << 16); + writel(readl(port + GP_PORT_INTMSK) | GP_PORT_INT_LINK, port + GP_PORT_INTMSK); + writel(GP_PORT_INT_LINK, port + GP_PORT_INTSTAT); + writel(readl(port + GP_PORT_APPCLK) | GP_PORT_APPCLK_CGDIS, port + GP_PORT_APPCLK); + if (readl(port + GP_PORT_LINKSTS) & GP_PORT_LINKSTS_UP) { + writel(GP_PORT_L2_ENTER, port + GP_PORT_L2); + readl_poll_timeout_atomic(port + GP_PORT_L2, val, !(val & GP_PORT_L2_BUSY), 10, + GP_STOP_US); + } + if (s->gp_pins && s->gp_perst[p] >= 0) { + void __iomem *reg = s->gp_pins + 4 * s->gp_perst[p]; + + val = readl(reg); + writel(s->gp_perst_low[p] ? val & ~GP_PIN_DATA : val | GP_PIN_DATA, reg); + } + writel(readl(phy) & ~GP_PHY_LINK, phy); + udelay(1000); + /* What this bit does isn't known. The platform sets it around PERST# (T8140). */ + writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); + writel(0, port + GP_PORT_PERST); + writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); + writel(readl(phy) & ~GP_PHY_EN1, phy); + writel(readl(phy) & ~GP_PHY_EN0, phy); + writel(readl(phy) | GP_PHY_RESET, phy); + writel(readl(phy) & ~GP_PHY_CLK0REQ, phy); + readl_poll_timeout_atomic(phy, val, !(val & GP_PHY_CLK0ACK), 1, GP_STOP_US); + writel(readl(phy) & ~GP_PHY_CLK1REQ, phy); + writel(readl(port + GP_PORT_APPCLK) & ~GP_PORT_APPCLK_EN, port + GP_PORT_APPCLK); + writel(readl(port + GP_PORT_INTSTAT), port + GP_PORT_INTSTAT); + t6040_mark_raw(s, 0x54100002 | p << 16); +} + +/* The GP host's two ports, while the block's domains are all still on. */ +static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) +{ + int p; + + if (!s->soc_east || !FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP))) + return; + for (p = 0; p < 2; p++) + t6040_gp_port_stop(s, p); +} + +/* + * apcie_sys_gp (AUTO off, axi2af bit0s, target 0), then apcie_gp. After ANS's ST lanes: the + * storage lanes' cio3pll/clkgen/PHY windows sit in apcie_gp, and touching them with it off + * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. Marks 0x543x/0x544x. + */ +static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) +{ + void __iomem *sys, *gp, *bridge = s->axi2af_bridge[AXI2AF_APCIE_SYS_GP]; + u32 val; + int ret; + + if (!s->soc_east) + return; + sys = s->soc_east + PS_APCIE_SYS_GP; + gp = s->soc_east + PS_APCIE_GP; + if (!FIELD_GET(PS_ACTUAL, readl(sys))) + return; + + t6040_mark_raw(s, 0x54300001); + val = readl(sys); + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + writel(val, sys); + } + if (bridge) { + writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); + } + writel((val & ~PS_TARGET) | PS_FLAGS, sys); + ret = readl_poll_timeout_atomic(sys, val, !FIELD_GET(PS_ACTUAL, val), 1, PS_OFF_TIMEOUT_US); + t6040_mark_raw(s, 0x54300002 | (ret ? 0x80 : 0)); + + t6040_mark_raw(s, 0x54400001); + if (READ_ONCE(apcie_seq) & BIT(3)) { + writel(0x244, gp); + ret = readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, + PS_OFF_TIMEOUT_US); + s->apcie_diag[1] |= BIT(5) | (ret ? BIT(18) : 0); + } else { + ret = t6040_ps_lower_word(gp); + } + t6040_mark_raw(s, 0x54400002 | (ret ? 0x80 : 0)); +} + +static int t6040_sleep_commit(unsigned long unused) +{ + struct t6040_sleep *s = t6040_sleep; + + t6040_mark(s, 0x20); + s->siq_cfg = read_sysreg_s(SYS_APL_SIQ_CFG_EL1); + spin_table_stop_parked(); + if (READ_ONCE(release_early)) + spin_table_release_stopped(); + t6040_mark(s, 0x21); + t6040_pmc_cp(s, 4); + if (READ_ONCE(lower_atc)) + t6040_sleep_lower_atc(s); + t6040_pmc_cp(s, 11); + if (READ_ONCE(lower_apcie_gp)) + t6040_sleep_stop_gp_ports(s); + t6040_pmc_cp(s, 12); + + /* + * lower_io: ANS's two ST lanes + shared PHY down, once ANS has been + * stopped by its driver. The platform does this ~40 ms before ANS idle; + * here it follows every refusal, so a refused sleep finds the link up. + * No-op unless the apple,apcie windows are present (apcie_st.valid). + */ + memset(s->apcie_diag, 0, sizeof(s->apcie_diag)); + s->apcie_diag[1] = (s->pcie_ans_stopped ? BIT(3) : 0) | (s->apcie_st.valid ? BIT(4) : 0); + if (READ_ONCE(apcie_st1_on) && s->soc_east && + t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1)) + t6040_mark_raw(s, 0x54c00001); + if (s->pcie_ans_stopped) { + t6040_sleep_lower_apcie_st(s); + t6040_pmc_cp(s, 13); + if (s->soc_east && READ_ONCE(apcie_st_ps)) + s->ans_off_ret = t6040_ps_wait_off(s->soc_east, PS_ANS); + } + t6040_pmc_cp(s, 14); + if (READ_ONCE(lower_apcie_gp)) + t6040_sleep_lower_apcie_gp(s); + + /* + * What the quiesce gate would panic on, after everything here has lowered what it + * lowers: 0x58 then the mask of the axi2af domains still on. The S2R timer then resets + * the SoC without a sleep, so the mark is kept: the wake alarm is not put back. + */ + s->commit_on_mask = t6040_axi2af_on_mask(s); + t6040_pmc_cp(s, 5); + + t6040_mark(s, 0x22); + /* WD0 off, then the AON 5 s reconfig net (as the platform arms it for a reconfig). */ + if (s->wdt) + writel(0, s->wdt + WDT_WD0_CTL); + t6040_sleep_set_wd2(s, clamp(READ_ONCE(sleep_wd_secs), 1U, WDT_MAX_SECS)); + /* And the SMC's AP watchdog deadline off, as the platform right after its WD2 (measured). */ + if (s->smc_wdt_deadline) + writel(0, s->smc_wdt_deadline); + + t6040_mark(s, 0x23); + /* PMU boot stage 0x50 and NOTIFY_ENABLE on both SPMI controllers (measured). */ + t6040_pmu_write(s, PMU_BOOT_STAGE, PMU_BOOT_STAGE_SLEEP); + if (s->spmi0) + writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi0 + SPMI_NOTIFY_ENABLE); + if (s->spmi_a0) + writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi_a0 + SPMI_NOTIFY_ENABLE); + + /* + * The order of the PMGR quiesce (the cluster and axi2af + * parts measured on the J616s): the five complexes off, the axi2af gate read, + * the clock quiesce, the keep-alive bits off, then the S2R timer. The CPM + * pwrgate voters are not written on entry: they read back 0x80000000 + * already; the wake side re-asserts them. + */ + s->quiesce_to = 0; + WRITE_ONCE(s->timer_written, false); + if (READ_ONCE(release_early)) { + t6040_mark(s, 0x2c); + s->quiesce_to |= min(t6040_secondaries_off(s), 15U) << 20; + } + if (READ_ONCE(pmgr_quiesce)) { + t6040_mark(s, 0x2b); + t6040_pmgr_quiesce(s); + } + t6040_pmc_cp(s, 6); + if (READ_ONCE(agx_gate)) + t6040_mark(s, 0x2a0 | t6040_agx_gate(s)); + s->cpm_to = 0; + if (READ_ONCE(cpm_iboot)) { + t6040_mark(s, 0x2e); + s->cpm_to = t6040_cpm_iboot(s, READ_ONCE(cpm_iboot)); + } + t6040_pmc_cp(s, 7); + t6040_mark(s, 0x24); + t6040_clusters_off(s); + t6040_pmc_cp(s, 8); + + /* Read-only: the axi2af panic gate the platform checks before the engine goes. */ + t6040_axi2af_readcheck(s); + t6040_mark(s, 0x350); + t6040_clkgen_park(s); + t6040_mark(s, 0x351); + t6040_pmc_cp(s, 9); + if (READ_ONCE(keep_alive_off)) + s->ka_on_before = t6040_keep_alive(s, false, 0); + t6040_pmc_cp(s, 10); + t6040_mark(s, 0x25); + + /* Arm aura's AURAS2R1 mailbox with this boot's resume entry. */ + if (s->mbox) { + WRITE_ONCE(s->mbox->resume_pa, + READ_ONCE(resume_stub) && s->wdt_stub_ok ? + __pa_symbol(t6040_resume_stub) : __pa_symbol(cpu_resume)); + dcache_clean_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); + dsb(sy); + } + + if (s->ptd && READ_ONCE(ptd_withdraw)) + writeq(readq(s->ptd + PTD_REQ_READ) & ~(u64)READ_ONCE(ptd_withdraw), + s->ptd + PTD_REQ_WRITE); + + /* + * Discriminator: hold everything right before the timer. A reset that lands in the + * hold keeps mark 0x3e0 (its source started before the timer: the SMC/PMU after + * 'slpa', or a net); one that moves later by the hold follows the timer (the engine). + */ + if (READ_ONCE(timer_delay_ms)) { + t6040_mark(s, 0x3e0); + mdelay(min(READ_ONCE(timer_delay_ms), 10000U)); + } + + /* iBoot's order: the AOP + audio island, then the MTP island. */ + s->aop_left = 0; + if (READ_ONCE(aop_island_off) & BIT(0)) + s->aop_left = t6040_aop_island_off(s); + s->mtp_left = 0; + if (READ_ONCE(mtp_island_off) & BIT(0)) + s->mtp_left = t6040_mtp_island_off(s, NULL); + + if (READ_ONCE(fab_on)) + t6040_fabrics_on(s); + if (READ_ONCE(ps_snap)) + t6040_ps_snap_store(s); + if (READ_ONCE(dcs_rec)) + t6040_dcs_store(s); + else if (READ_ONCE(aon_rec)) + t6040_aon_store(s); + else if (READ_ONCE(apcie_seq) & BIT(2)) + t6040_apcie_diag_store(s); + if (READ_ONCE(pmc_probe) & BIT(1)) { + u32 pv[ARRAY_SIZE(t6040_pmc_probe_pa)]; + + t6040_pmc_probe_read(s, pv); + t6040_pmc_probe_store(s, pv); + } + + /* Last before the S2R timer: what is still on, re-read now, kept through the reset. */ + s->commit_on_mask |= t6040_axi2af_on_mask(s); + if (s->aop_left) { + t6040_mark_raw(s, 0x55000000 | s->aop_left); + } else if (s->mtp_left) { + t6040_mark_raw(s, 0x57000000 | s->mtp_left); + } else if (s->cpm_to) { + t6040_mark_raw(s, 0x56000000 | s->cpm_to); + } else if (s->commit_on_mask) { + t6040_mark_raw(s, 0x58000000 | s->commit_on_mask); + } else if (s->quiesce_to & (QTO_CLUSTER(3) | QTO_CLUSTER(4))) { + /* + * DISP_INT (+0xc) / DISP_EXT (+0x10) kept BIT4: 0x51, then +0xc and +0x10 as + * read after the wait, then +0xc's low 6 bits and +0x10's [5:4] from before. + */ + t6040_mark_raw(s, 0x51000000 | (s->cluster_after[3] & 0xff) << 16 | + (s->cluster_after[4] & 0xff) << 8 | + (s->cluster_saved[3] & 0x3f) | + ((s->cluster_saved[4] >> 4) & 3) << 6); + } else if (s->quiesce_to) { + /* The commit ran, but a quiesce poll timed out: which ones, kept. */ + t6040_mark_raw(s, 0x5d000000 | s->quiesce_to); + } else { + /* One SPMI write: the alarm back + 1 s reads as "the commit ran". */ + t6040_mark_restore(s); + } + /* The commit: the MasterMini S2R timer arms the SoC sleep engine. */ + if (s->mastermini) + writel(1, s->mastermini + MASTERMINI_S2R_TIMER); + dsb(sy); + WRITE_ONCE(s->timer_written, true); + dsb(ish); + + /* + * Bring-up: an SPMI mark right after the timer never lands. Re-arm the AON + * WD2 here, no SPMI: a reset at timer + this many ms means this core still runs + * after the timer; one at the usual timer + ~4.5 s means it does not (measured: it runs). + * Long values test whether A2D only needs longer than ~5 s: the reset follows the + * timer by that much although the commit armed WD2 for sleep_wd_secs (ADT + * simple-reconfig-wdog-icc-time = 5). + */ + if (READ_ONCE(post_timer_wd_ms) && s->wdt) { + writel(0, s->wdt + WDT_WD2_CTL); + writel(0, s->wdt + WDT_WD2_COUNT); + writel(min(READ_ONCE(post_timer_wd_ms), WDT_MAX_SECS * 1000U) * (WDT_TICKS_PER_S / 1000), + s->wdt + WDT_WD2_ALARM); + writel(1, s->wdt + WDT_WD2_CTL); + dsb(sy); + } + + /* + * Bring-up (this core does run after the timer: the WD2 above fired). Does + * the system counter stop at the timer (every udelay() would then hang)? Safety WD2 + * 4 s, 1M cluster-ctl reads timed by CNTPCT, an SPMI mark polled by count (no udelay): + * 0x50EEPPQQ, E bit 7 set, bit 6 = counter stopped. Then the WD2 by the answer: + * stopped -> 100 ms, moving -> 2.5 s; a loop that hangs leaves the 4 s. + */ + if (READ_ONCE(post_timer_counter_probe) && s->wdt && s->cluster_ctl) { + u32 i, v[3], st; + u64 c0, c1; + bool stopped; + + t6040_wd2_raw(s, 4000); + isb(); + c0 = read_sysreg(cntpct_el0); + for (i = 0; i < 1000000; i++) + (void)readl_relaxed(s->cluster_ctl); + isb(); + c1 = read_sysreg(cntpct_el0); + stopped = c1 == c0; + for (i = 0; i < 3; i++) + v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; + if (s->spmi0 && READ_ONCE(marks) && s->alarm_ok) { + void __iomem *b = s->spmi0; + u32 m = 0x50000000 | (v[0] | 0x80 | (stopped ? 0x40 : 0)) << 16 | + v[1] << 8 | v[2]; + + /* 0x50EAAAAA: tag E, then the ANE ACTUAL nibbles (t6040_ane_actual). */ + if (READ_ONCE(post_timer_ane)) + m = 0x50e00000 | t6040_ane_actual(s); + /* + * 0x50CMMMMM: tag C, ACTUAL != 0 as bits: 0-3 E cores, 4-8 P0 cores, + * 9-13 P1 cores (cpu-start bits 0-8, 10-14), 14-18 ANE sys/mpm/cpu/td/base. + */ + if (READ_ONCE(post_timer_mask)) { + u32 a = t6040_ane_actual(s), mask = 0, b; + + for (b = 0; b < 15 && s->soc_ps; b++) { + if (b == 9) + continue; + if (FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 8 * b))) + mask |= BIT(b < 9 ? b : b - 1); + } + for (b = 0; b < 5; b++) + if ((a >> (16 - 4 * b)) & 0xf) + mask |= BIT(14 + b); + m = 0x50c00000 | mask; + } + + for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) + readl(b + SPMI_REPLY); + writel(SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), b + SPMI_CMD); + writel(m, b + SPMI_CMD); + for (i = 0; i < 1000000; i++) { + st = readl(b + SPMI_NOTIFY_ACK); + if (st & SPMI_NOTIFY_REPLY) + break; + } + writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); + if (!(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) + readl(b + SPMI_REPLY); + } + t6040_wd2_raw(s, stopped ? 100 : 2500); + } + + /* A raised line would end the WFI the engine waits for. */ + aic_sleep_mask_all(); + spin_table_release_stopped(); + + /* Optional, the bytes, necessity from Linux unproven (off by default). */ + if (replay_pmu_go_hib) + t6040_pmu_go_hib(s); + + /* + * Bring-up probe: the SoC resets ~5 s after the timer (a hold before the timer moves it), + * and a probe that waited with udelay() never marked: nothing that waits on + * time or on a reply may finish after the timer. Staged, no timer-based waits; the + * last mark that lands tells how far it got (the wake alarm is not put back): + * 0x50ffffff A: an SPMI mark right after the timer landed + * 0x50EEPPQQ B: cluster-ctl E/P0/P1 bytes [5:0] read once + * 0x50EEPPQQ C: after N * 1000 cluster-ctl reads: E bit 7 set, E bit 6 = the + * counter (CNTPCT) did not move over the reads + */ + if (READ_ONCE(post_timer_probe_ms) && s->cluster_ctl) { + u32 n = min(READ_ONCE(post_timer_probe_ms), 4000U) * 1000, i, v[3]; + u64 c0, c1; + + t6040_mark_raw(s, 0x50ffffff); + for (i = 0; i < 3; i++) + v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; + t6040_mark_raw(s, 0x50000000 | v[0] << 16 | v[1] << 8 | v[2]); + isb(); + c0 = read_sysreg(cntpct_el0); + for (i = 0; i < n; i++) + (void)readl_relaxed(s->cluster_ctl); + isb(); + c1 = read_sysreg(cntpct_el0); + for (i = 0; i < 3; i++) + v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; + t6040_mark_raw(s, 0x50000000 | (v[0] | 0x80 | (c1 == c0 ? 0x40 : 0)) << 16 | + v[1] << 8 | v[2]); + } + + t6040_deep_wfi(); +} + +/* + * The wake return (t6040_sleep_return), reached on the boot core after aura's + * AURAS2R1 mailbox hands it back to cpu_resume. aura restores NO SoC register; + * every write here is the OS driver's job. It is the + * mirror of the PMGR wake restore: the three CPU complexes on, the two wake + * pwrgates, SOC-East retention off (with the PARENT_MISSING poll), the axi2af + * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the + * AP-global S2R control cleared, and the wake-status words read. It runs on + * the J616s once the data blockers are overridden (i_know_untested). + */ +/* + * Wake-return marks (seen on the J616s: an asynchronous SError on CPU 0 after the wake, last + * mark a driver step): complete every access of the step before the mark, so a pending SError is + * taken before the next mark lands and the comparator names the step that caused it. + */ +/* + * On the J616s a wake reached the device resume and took an SError in the noirq resume of the ANS + * apcie DART (0x4_1000_0000, no power domain in the DT). Record what the ANS/APCIe raise did, + * right after it: 'APCW' at PMU 0xa280 (over the ATCC record; PMVC at 0xa2ac stays), w1 = + * stopped(bit31) | step reached << 16 | ret & 0xffff, w2..w9 = the SOC-East PS words of ANS, + * GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, PHY_SW (PMGR, always readable). + */ +#define T6040_APCW_PMU 0xa280 +#define T6040_APCW_MAGIC 0x57435041 /* "APCW" in PMU byte order */ + +static bool apcie_wake_rec = true; +module_param(apcie_wake_rec, bool, 0644); +MODULE_PARM_DESC(apcie_wake_rec, "Bring-up: record the wake-side ANS/APCIe raise at PMU 0xa280 ('APCW')"); + +/* + * With the apcie fabric up, the noirq resume of 410000000.iommu still took the SError: + * that DART sits in the block of port 0 of the GP PCIe host (pcie@1cb0000000, Wi-Fi/BT; port 1 + * SD behind 411000000.iommu), whose clocks the bootloader set up at boot and nobody after an + * S2R. pcie_port_clk (mask of ports) undoes t6040_gp_port_stop()'s PHY/app clock part right after + * the apcie raise: lane REFCLK0/1 request + ack, REFCLKEN, PORT_APPCLK EN, PORT_STATUS ready. + * The link itself stays down. Result per port in a byte of APCW w10: bit0 refclk acked, + * bit1 port ready, bit4 refclk timeout, bit5 ready timeout, bit7 not mapped. + */ +#define GP_PHY_CLK1ACK BIT(3) +#define GP_PORT_STATUS 0x804 +#define GP_PORT_STATUS_READY BIT(0) + +static unsigned int pcie_port_clk = 3; +module_param(pcie_port_clk, uint, 0644); +MODULE_PARM_DESC(pcie_port_clk, + "Bring-up wake: GP PCIe ports (mask) whose refclk + app clock are brought back after the apcie raise"); + +static void t6040_gp_pcie_clk(struct t6040_sleep *s) +{ + unsigned int mask = READ_ONCE(pcie_port_clk); + u32 res = 0, val; + int i; + + for (i = 0; i < 2; i++) { + void __iomem *phy = s->gp_phy[i], *port = s->gp_port[i]; + u32 r = 0, cfg, app; + + if (!(mask & BIT(i))) + continue; + if (!phy || !port || !s->gp_saved[i]) { + res |= BIT(7) << (8 * i); + continue; + } + cfg = s->gp_phy_saved[i]; + app = s->gp_appclk_saved[i]; + /* t6040_gp_port_stop() backwards: clock requests, reference buffer, enables, link. */ + writel(readl(phy) | GP_PHY_CLK0REQ, phy); + if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK0ACK, 1, 50000)) + r |= BIT(4); + writel(readl(phy) | GP_PHY_CLK1REQ, phy); + if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK1ACK, 1, 50000)) + r |= BIT(4); + if (!(r & BIT(4))) + r |= BIT(0); + if (!(cfg & GP_PHY_RESET)) { + writel(readl(phy) & ~GP_PHY_RESET, phy); + udelay(1); + } + writel(readl(phy) | (cfg & (GP_PHY_EN0 | GP_PHY_EN1)), phy); + writel(readl(phy) | (cfg & GP_PHY_LINK), phy); + writel((readl(port + GP_PORT_APPCLK) & ~(GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)) | + (app & (GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)), port + GP_PORT_APPCLK); + /* + * Clocks back (refclk acked) but no PORT_STATUS ready: the stop also wrote + * the controller's PERST register 0. Put it back as it was (the endpoint's PERST# + * pin stays asserted), with the guard bit the platform sets around it. + */ + writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); + writel(s->gp_perst_saved[i], port + GP_PORT_PERST); + writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); + if (readl_poll_timeout_atomic(port + GP_PORT_STATUS, val, val & GP_PORT_STATUS_READY, + 1, 50000)) + r |= BIT(5); + else + r |= BIT(1); + res |= r << (8 * i); + } + s->gp_port_res = res; +} + +static void t6040_apcie_wake_rec(struct t6040_sleep *s) +{ + static const u16 off[8] = { PS_ANS, PS_APCIE_GP, PS_APCIE_SYS_GP, PS_APCIE_ST0, + PS_APCIE_SYS_ST0, PS_APCIE_ST1, PS_APCIE_SYS_ST1, + PS_APCIE_PHY_SW }; + u32 w[11]; + int i; + + if (!READ_ONCE(apcie_wake_rec) || !s->soc_east || !s->spmi0) + return; + w[10] = s->gp_port_res; + w[0] = T6040_APCW_MAGIC; + w[1] = (s->pcie_ans_stopped ? BIT(31) : 0) | (s->apcie_wake_step & 0xff) << 16 | + ((u32)s->pcie_ans_on_ret & 0xffff); + for (i = 0; i < 8; i++) + w[2 + i] = readl(s->soc_east + off[i]); + for (i = 0; i < 11; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCW_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], + NULL, 0); +} + +static void t6040_ret_mark(struct t6040_sleep *s, u32 n) +{ + dsb(sy); + isb(); + t6040_mark(s, n); +} + +static void t6040_sleep_return(struct t6040_sleep *s) +{ + u32 val; + int i, ret; + + t6040_mark(s, 0x30); + /* The reset left the timer and IRQ routing at their EL2 defaults. */ + write_sysreg(s->cntkctl, cntkctl_el1); + isb(); + write_sysreg_s(s->siq_cfg, SYS_APL_SIQ_CFG_EL1); + t6040_ret_mark(s, 0x31); + + spin_table_stop_finished(); + t6040_ret_mark(s, 0x32); + aic_sleep_restore(); + t6040_ret_mark(s, 0x33); + + /* + * The three CPU complexes back on (E, P0, P1), nibble 0xf, poll +0xc for + * the ready bit. The ACC restore then writes ACC register 0xe440f8 + * := 1 per complex; that per-complex ACC window is not mapped here (TODO) + * and is left to the normal per-CPU bring-up. + */ + if (s->cluster_ctl) + for (i = 0; i < CLUSTER_CTL_NR; i++) { + u32 on = i < CLUSTER_CTL_FIRST_WAIT ? CLUSTER_CTL_ON : + s->cluster_saved[i] & CLUSTER_CTL_NIBBLE; + + val = readl(s->cluster_ctl + CLUSTER_CTL(i)); + writel((val & ~CLUSTER_CTL_NIBBLE) | on, s->cluster_ctl + CLUSTER_CTL(i)); + } + t6040_ret_mark(s, 0x34); + + /* Wake pwrgates and CPM voters: read, set BIT31. */ + if (s->pwrgate) { + writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_0); + writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_1); + } + t6040_ret_mark(s, 0x35); + + /* SOC-East retention off: clear DEV_DISABLE, then poll PARENT_MISSING clear. */ + if (s->soc_east) { + val = readl(s->soc_east + SOC_EAST_RETENTION); + writel(val & ~SOC_EAST_DEV_DISABLE, s->soc_east + SOC_EAST_RETENTION); + ret = readl_poll_timeout_atomic(s->soc_east + SOC_EAST_RETENTION, val, + !(val & SOC_EAST_PARENT_MISSING), 1, + SOC_EAST_TIMEOUT_US); + if (ret) + pr_warn("t6040-sleep: SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); + } + t6040_ret_mark(s, 0x36); + + /* + * The SOC DPE restore (the 14 DPE energy accumulators |= 1) is skipped: + * the DPE RegMap base and its offset list are not resolved here, and it + * is energy accounting only, benign to skip. TODO. + */ + + /* The fabric-bridge retention re-arm (the ANS/Wi-Fi/display-dead guard). */ + t6040_axi2af_rearm(s); + t6040_ret_mark(s, 0x37); + + /* + * Raise ANS's two ST lanes + shared PHY from power-off, AFTER the fabric + * bridges are re-armed and BEFORE control returns to the PM core, so + * nvme's resume_early sets the ANS run bit onto a live link. + * No-op unless the apple,apcie windows are present. + */ + s->apcie_wake_step = 0; + s->pcie_ans_on_ret = 0; + if (s->pcie_ans_stopped) + s->pcie_ans_on_ret = t6040_sleep_raise_apcie_st(s); + s->gp_port_res = 0; + if (READ_ONCE(pcie_port_clk)) { + t6040_gp_pcie_clk(s); + t6040_ret_mark(s, 0x56); + } + t6040_apcie_wake_rec(s); + if (READ_ONCE(aop_wake)) { + t6040_aop_wake(s); + t6040_ret_mark(s, 0x57); + } + t6040_ret_mark(s, 0x38); + + if (s->pwrgate) { + writel((readl(s->pwrgate + PWRGATE_CPM_0) & ~PWRGATE_ON) | PWRGATE_ON, + s->pwrgate + PWRGATE_CPM_0); + writel((readl(s->pwrgate + PWRGATE_CPM_1) & ~PWRGATE_ON) | PWRGATE_ON, + s->pwrgate + PWRGATE_CPM_1); + writel((readl(s->pwrgate + PWRGATE_CPM_2) & ~PWRGATE_ON) | PWRGATE_ON, + s->pwrgate + PWRGATE_CPM_2); + } + t6040_ret_mark(s, 0x39); + + /* Undo the entry clk-mux park; the clock restore is otherwise a NOP. */ + t6040_clkgen_restore(s); + t6040_ret_mark(s, 0x3a); + + /* Software ack of the armed S2R request (the platform has no stop of the S2R timer). */ + if (s->aop_global) + writel(0, s->aop_global + AOP_S2R_CTRL); + t6040_ret_mark(s, 0x3b); + + /* The keep-alive bits the commit cleared, back (as the wake restore enables them). */ + if (s->ka_on_before) + t6040_keep_alive(s, true, s->ka_on_before); + t6040_ret_mark(s, 0x3c); + + /* A longer WD2 over the device resume, until .end. */ + t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); + t6040_ret_mark(s, 0x3f); + + if (s->mbox) { + dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); + pr_info("t6040-sleep: wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); + } + if (s->aop_global) + for (i = 0; i < AOP_WAKE_WORDS; i++) + s->wake_status[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); + s->returned = true; +} + +static int t6040_sleep_valid(suspend_state_t state) +{ + return state == PM_SUSPEND_MEM; +} + +/* + * The refusal-first gate. Default: print the blocker list and return an error + * here, before any device suspends, so nothing is entered. A dry run proceeds + * and refuses at .enter. allow_entry with no blocker: the only path that would + * reach the commit, and it cannot, because the missing-data blockers never + * clear. + */ +/* + * Seq 25 stalls because ATC_n_COMMON does not come back up (port 0 or port 1 at random). + * The platform writes SMC key pmVC (the VDD_CIO rail request, + * its enable mask | 0x100) = 0x101 while any ATC USB_AON is on, also at sleep; Linux never + * does, and the ADT marks the rail power-gated (vdd-cio-rail-pg). A rail the SMC drops during + * the sleep transition would race the engine. pmvc != 0 writes that value in .begin and logs + * the key before and after. + */ +static unsigned int pmvc = 0x101; +module_param(pmvc, uint, 0644); +MODULE_PARM_DESC(pmvc, "Bring-up: value for SMC key pmVC (VDD_CIO request, the platform 0x101) written in .begin; 0 = leave it"); + +/* The .begin log does not reach the journal; keep the result in PMU SRAM 0xa2ac. */ +#define T6040_PMVC_PMU 0xa2ac +#define T6040_PMVC_MAGIC 0x43564d50 /* "PMVC" in PMU byte order */ + +/* The SMC node is apple,t6041-smc + apple,t8103-smc here (macsmc's match list). */ +static const struct of_device_id t6040_smc_match[] = { + { .compatible = "apple,t8103-smc" }, + { .compatible = "apple,smc" }, + {} +}; + +static int t6040_sleep_pmvc_write(struct t6040_sleep *s, u32 v, bool rec) +{ + struct platform_device *pdev; + struct device_node *np; + struct apple_smc *smc; + u32 before = 0, after = 0, w[5]; + int rb = -ENODEV, ret = -ENODEV, ra = -ENODEV, i; + + np = of_find_matching_node(NULL, t6040_smc_match); + pdev = np ? of_find_device_by_node(np) : NULL; + of_node_put(np); + smc = pdev ? platform_get_drvdata(pdev) : NULL; + if (smc) { + rb = apple_smc_read_u32(smc, SMC_KEY(pmVC), &before); + ret = apple_smc_write_u32(smc, SMC_KEY(pmVC), v); + ra = apple_smc_read_u32(smc, SMC_KEY(pmVC), &after); + } + if (pdev) + put_device(&pdev->dev); + dev_info(s->dev, "pmVC: read %d 0x%08x, write 0x%08x %d, read %d 0x%08x\n", + rb, before, v, ret, ra, after); + if (rec) { + w[0] = T6040_PMVC_MAGIC; + w[1] = (u16)rb | (u32)(u16)ret << 16; + w[2] = before; + w[3] = (u16)ra; + w[4] = after; + for (i = 0; i < 5; i++) + t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMVC_PMU + 4 * i, SPMI_OP_WRITE4), + &w[i], NULL, 0); + } + return ret; +} + +static int t6040_pmvc_now_set(const char *val, const struct kernel_param *kp) +{ + u32 v; + int ret = kstrtou32(val, 0, &v); + + if (ret) + return ret; + if (!t6040_sleep) + return -ENODEV; + ret = t6040_sleep_pmvc_write(t6040_sleep, v, false); + return ret < 0 ? ret : 0; +} + +static const struct kernel_param_ops t6040_pmvc_now_ops = { + .set = t6040_pmvc_now_set, +}; +module_param_cb(pmvc_now, &t6040_pmvc_now_ops, NULL, 0200); +MODULE_PARM_DESC(pmvc_now, "Bring-up: write this value to SMC key pmVC now and log the key before/after"); + +static void t6040_sleep_pmvc(struct t6040_sleep *s) +{ + u32 v = READ_ONCE(pmvc); + + if (v) + t6040_sleep_pmvc_write(s, v, true); +} + +static int t6040_sleep_begin(suspend_state_t state) +{ + struct t6040_sleep *s = t6040_sleep; + unsigned int disp = 0; + unsigned int live = t6040_sleep_live_blockers(s, NULL, &disp); + unsigned int data = READ_ONCE(i_know_untested) ? 0 : T6040_SLEEP_DATA_BLOCKERS; + /* The display pipes are lowered during device suspend (apple-dcp .suspend), + * so .begin only gates on the non-display live blockers; .enter re-checks the + * full set at the commit point, by which time the display is off. */ + unsigned int begin_total = (live - disp) + data; + unsigned int dry = READ_ONCE(dry_run), secs; + + WRITE_ONCE(t6040_sleep_dry, 0); + s->returned = false; + s->gp_saved[0] = s->gp_saved[1] = false; + t6040_aop_ps_save(s); + t6040_susf_begin(s); + t6040_sleep_pmvc(s); + if (READ_ONCE(dcs_pg) & BIT(0)) + t6040_dcs_dyn_pg(s); + if (READ_ONCE(dcs_ps_auto) & BIT(0)) + t6040_dcs_ps_auto_on(s); + pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend) + %u missing-data blocker(s)\n", + live, disp, data); + t6040_sleep_log_blockers(s); + + if (!begin_total && allow_entry) { + /* Non-display blockers clear and the override/unknowns allow it: proceed + * to device suspend; .enter holds the real commit until the display is + * also off. The AON WD2 is the net from here. */ + if (disp) + pr_warn("t6040-sleep: entering: %u display pipe(s) still on, relying on device suspend to lower them before the commit\n", + disp); + t6040_sleep_power_ops(s); + t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); + pm_set_suspend_via_firmware(); + t6040_mark_save(s); + t6040_mark(s, 0x01); + if (s->alarm_ok) + WRITE_ONCE(pm_bringup_dev_mark, t6040_dev_mark); + return 0; + } + + if (dry >= 2) { + dry = min(dry, 3U); + t6040_sleep_power_ops(s); + if (!s->power_ops) + pr_warn("t6040-sleep: dry run: no CPU parking power ops, so no hold at the commit point\n"); + WRITE_ONCE(t6040_sleep_dry, dry); + pm_set_suspend_via_firmware(); + secs = min(READ_ONCE(wd2_secs), WDT_MAX_SECS); + if (secs && s->wdt) + t6040_sleep_set_wd2(s, secs); + else + secs = 0; + pr_info("t6040-sleep: dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", + dry, dry >= 3 ? "the stops only a power cycle undoes" : + "the last steps", secs); + return 0; + } + + if (dry) { + pr_info("t6040-sleep: dry run 1: parking CPUs and refusing at the commit point; the firmware is never told\n"); + return 0; + } + + pr_info("t6040-sleep: refusing the suspend; nothing entered\n"); + return -EOPNOTSUPP; +} + +/* After the device suspend, in process context. */ +static int t6040_sleep_prepare(void) +{ + t6040_mark(t6040_sleep, 0x02); + t6040_sleep_watch_bound(t6040_sleep); + + return 0; +} + +/* + * A dry run 2 or 3 at the commit point, interrupts off and one CPU up: every + * check a real commit makes, the parked CPUs held and let go, at level 3 each + * coprocessor's last check, then the refusal. Never reached: the enclave's + * sleep request, each coprocessor's last stop, the wake masks, the SoC + * power-down. + */ +static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) +{ + unsigned int refused; + int ret; + + pr_info("t6040-sleep: dry run %u at the commit point: %u CPU(s) online\n", dry, + num_online_cpus()); + t6040_log_ane(s, "dry-run commit point"); + t6040_log_a2d(s, "dry-run commit point"); + t6040_sleep_log_s2r_state(s, "dry-run commit point"); + t6040_log_cpm(s, "dry-run commit point"); + { + u32 map[T6040_PS_SNAP_WORDS]; + + t6040_ps_snap_take(s, map); + pr_info("t6040-sleep: dry-run commit point: PS snapshot %*ph\n", (int)sizeof(map), map); + if (READ_ONCE(ps_snap)) + t6040_ps_snap_store(s); + } + if (READ_ONCE(pmc_probe) & BIT(0)) { + u32 pv[ARRAY_SIZE(t6040_pmc_probe_pa)]; + + t6040_pmc_probe_read(s, pv); + pr_info("t6040-sleep: dry-run commit point: PMC probe aa140 %#x ac000 %#x ac00c %#x ac020 %#x 800000 %#x 80c000 %#x 820000 %#x 824004 %#x 828004 %#x\n", + pv[0], pv[1], pv[2], pv[3], pv[4], pv[5], pv[6], pv[7], pv[8]); + t6040_pmc_probe_store(s, pv); + } + /* + * A wake source pending and left unmasked by the masks a real commit arms would keep the + * AP partition from powering down (A2D then stalls at MiniPMGR +0x40). Read only. + */ + if (s->aop_global) { + u32 p[AOP_WAKE_WORDS]; + int i; + + for (i = 0; i < AOP_WAKE_WORDS; i++) + p[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)) & ~t6040_wake_mask[i]; + pr_info("t6040-sleep: dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", + (int)sizeof(p), p); + } + if ((READ_ONCE(mtp_island_off) & BIT(1)) && s->mtp_ps) { + u32 saved[T6040_MTP_PS_WORDS], after[T6040_MTP_PS_WORDS], left; + int i; + + left = t6040_mtp_island_off(s, saved); + for (i = 0; i < T6040_MTP_PS_WORDS; i++) + after[i] = readl(s->mtp_ps + 8 * i); + pr_info("t6040-sleep: dry-run MTP island off: left on %#x, PS before %*ph\n", + left, (int)sizeof(saved), saved); + pr_info("t6040-sleep: dry-run MTP island off: PS after %*ph\n", + (int)sizeof(after), after); + t6040_log_a2d(s, "dry-run MTP island off"); + t6040_mtp_island_back(s, saved); + t6040_log_a2d(s, "dry-run MTP island back"); + } + refused = t6040_sleep_refuse(s); + pr_info("t6040-sleep: a real commit would %s here (%u check(s) failed)\n", + refused ? "refuse" : "go on", refused); + + ret = s->power_ops ? spin_table_hold_parked() : -EOPNOTSUPP; + if (ret) + pr_err("t6040-sleep: the parked CPUs didn't answer the hold: %d\n", ret); + else + pr_info("t6040-sleep: %u parked CPU(s) answered the hold\n", + num_present_cpus() - num_online_cpus()); + + if (dry >= 3 && !ret) + pr_info("t6040-sleep: the coprocessors' last checks: %d\n", + t6040_sleep_coprocs_check()); + if (!ret) + spin_table_unhold_parked(); + + pr_info("t6040-sleep: dry run: refused before the enclave's sleep request (%s), the coprocessors' last stops and the SoC power-down\n", + t6040_sleep_enclave.dev ? "left by sep" : "none left"); + + return -EBUSY; +} + +/* + * The display power words the platform has off before its sleep (apple,check-ps-b4-sleep), lowered + * here: after the device suspend, interrupts off, one CPU up, so no driver, interrupt handler + * or DART touches the pipes any more. The DCPs only stop their firmware: lowering the domains + * from their suspend panicked on T6040. Each group from its last sub-block to its pipe CPU, + * each word as the pmgr-pwrstate driver powers a domain off (TARGET 0, the one-shot bits + * clear), polled to ACTUAL 0. Returns the number of words left on. + */ +static bool lower_display = true; +module_param(lower_display, bool, 0644); +MODULE_PARM_DESC(lower_display, "Lower the display power words at the S2R commit point"); + +static bool lower_cpu; +module_param(lower_cpu, bool, 0644); +MODULE_PARM_DESC(lower_cpu, "Also lower each display group's pipe CPU word by hand (raised an SError on T6040)"); + +static bool axi2af_quiesce; +module_param(axi2af_quiesce, bool, 0644); +MODULE_PARM_DESC(axi2af_quiesce, "Clear a display group's axi2af fabric-bridge bit0 before lowering it (off: its SYS domain stays on)"); + +#define PS_DEV_DISABLE BIT(10) +#define PS_BUSY BIT(11) +#define PS_RESET_REQ BIT(12) + +/* + * The display mark: 0x5cGGSOOO, GG the group, S 0 before the write, 8 off, 4 still on; + * for a gated CPU 1 waiting for its gate, 2 gated, 3 not gated in time. + */ +#define T6040_DISP_MARK(g, st, off) (0x5c000000 | ((g) & 0xff) << 16 | (st) << 12 | ((off) & 0xfff)) + +/* The axi2af domain that carries a display group ("disp" -> "disp_sys"), or -1. */ +static int t6040_axi2af_of_group(const char *group) +{ + size_t n = strlen(group); + int i; + + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) + if (!strncmp(t6040_axi2af[i].name, group, n) && + !strcmp(t6040_axi2af[i].name + n, "_sys")) + return i; + return -1; +} + +/* + * The entry side of the axi2af workaround (the quiesce step for it and the PMGR + * power-down hook): before a retained domain goes off, its fabric bridge + * +0x104/+0x108 bit0 is cleared; t6040_axi2af_rearm() sets it again on wake. Only while + * the domain is powered: the bridge sits in it. Mark 0x5eII0000, II the domain. + */ +static void t6040_axi2af_quiesce(struct t6040_sleep *s, int i) +{ + void __iomem *ps, *bridge; + + if (i < 0) + return; + ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); + bridge = s->axi2af_bridge[i]; + if (!ps || !bridge || !FIELD_GET(PS_ACTUAL, readl(ps))) + return; + t6040_mark_raw(s, 0x5e000000 | (i & 0xff) << 16); + writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); + t6040_mark_raw(s, 0x5e008000 | (i & 0xff) << 16); +} + +static bool disp_seq_off = true; + +/* + * Under Linux cluster-ctl DISP_INT went 0x2f -> 0x10 and stuck there; on the platform it reached 0, + * DISP_CPU having gone down at the DCP stop (RUN, bit5, dart idle, then the PS word + * in two steps). Rewriting the word under a running DCP resets the SoC. With + * appledrm.s2r_keep_run=0 this tries the exact sequence on it. + */ +static bool pmc_seq; +module_param(pmc_seq, bool, 0644); +MODULE_PARM_DESC(pmc_seq, + "Release a display pipe's PMC votes as the platform (boost, DVMR + ack, BWR, rail + ack) instead of zeroing c14/c18"); + +static bool dispext_off; +module_param(dispext_off, bool, 0644); +MODULE_PARM_DESC(dispext_off, + "Power the stopped DCPEXT pipes down as the platform (PMC votes, CPU, FE, SYS) so DISP_EXT can go off"); + +static bool pmp_disp_release; +module_param(pmp_disp_release, bool, 0644); +MODULE_PARM_DESC(pmp_disp_release, + "With disp_cpu_seq: withdraw the internal display's PMP request (PTD bit 16) and wait for the ack first"); + +static bool disp_cpu_seq; +module_param(disp_cpu_seq, bool, 0644); +MODULE_PARM_DESC(disp_cpu_seq, + "Lower DISP_CPU in the two steps after the DCP stop (needs appledrm.s2r_keep_run=0)"); + +static bool disp_need_cpu_gated; +module_param(disp_need_cpu_gated, bool, 0644); +MODULE_PARM_DESC(disp_need_cpu_gated, "Leave DISP_FE/SYS on unless the PMGR has gated DISP_CPU"); + +#define DISP_CPU_GATE_US 50000 + +static bool gated_cpu_target_only = true; +module_param(gated_cpu_target_only, bool, 0644); +MODULE_PARM_DESC(gated_cpu_target_only, "A DISP_CPU already gated (ACTUAL 0) only gets TARGET 0, keeping AUTO_ENABLE, and dart-dcp is left unread"); + +static bool pmc_dcp_off = true; +module_param(pmc_dcp_off, bool, 0644); +MODULE_PARM_DESC(pmc_dcp_off, "Drop the PMS DCP voters to 0 before the internal display goes down"); +module_param(disp_seq_off, bool, 0644); +MODULE_PARM_DESC(disp_seq_off, "Lower DISP_CPU, DISP_FE and DISP_SYS at the commit point as the PMGR does"); + +/* + * regmap_read_poll_timeout_atomic() reads ktime_get(), which warns while timekeeping is + * suspended (12 warnings from .enter on the J616s). Counted 1 us steps instead. + */ +static int t6040_regmap_poll_off(struct regmap *map, u32 off, u32 *cur) +{ + unsigned int us; + + for (us = 0; ; us++) { + if (regmap_read(map, off, cur)) + return -EIO; + if (!FIELD_GET(PS_ACTUAL, *cur)) + return 0; + if (us >= PS_OFF_TIMEOUT_US) + return -ETIMEDOUT; + udelay(1); + } +} + +/* A power word behind a regmap (pmgr_disp) or a plain window (soc_east). */ +struct t6040_psw { + struct regmap *map; + void __iomem *io; + u32 off; +}; + +static u32 t6040_psw_read(const struct t6040_psw *w) +{ + u32 v = 0; + + if (w->map) + regmap_read(w->map, w->off, &v); + else + v = readl(w->io + w->off); + return v; +} + +static void t6040_psw_write(const struct t6040_psw *w, u32 v) +{ + if (w->map) + regmap_write(w->map, w->off, v); + else + writel(v, w->io + w->off); +} + +static int t6040_psw_poll_long(const struct t6040_psw *w, u32 mask, u32 want, unsigned int max_us) +{ + unsigned int us; + + for (us = 0; us < max_us; us++) { + if ((t6040_psw_read(w) & mask) == want) + return 0; + udelay(1); + } + return -ETIMEDOUT; +} + +static int t6040_psw_poll(const struct t6040_psw *w, u32 mask, u32 want) +{ + unsigned int us; + + for (us = 0; us < DISP_PS_TIMEOUT_US; us++) { + if ((t6040_psw_read(w) & mask) == want) + return 0; + udelay(1); + } + return -ETIMEDOUT; +} + +/* Marks 0x56SS00RR: SS the word's step, RR what it reached (bit 7: a poll timed out). */ +static void t6040_disp_mark(struct t6040_sleep *s, u32 step, u32 rr) +{ + t6040_mark_raw(s, 0x56000000 | (step & 0xff) << 16 | (rr & 0xff)); +} + +/* + * The power-down of a PS word: with + * AUTO_ENABLE set, take it out of automatic mode with the target kept (v & 0xeffffcff) and + * wait for ACTUAL 15; then @pre (the SYS hooks); then TARGET 0 with bits 9:8 written 1 and + * bits 10-12, 24-31 kept; then poll bit11 clear and ACTUAL 0. + */ +static int t6040_psw_off(struct t6040_sleep *s, const struct t6040_psw *w, u32 step, + void (*pre)(struct t6040_sleep *s)) +{ + u32 v = t6040_psw_read(w); + int ret; + + if (v & PS_AUTO_ENABLE) { + t6040_disp_mark(s, step, 0x01); + t6040_psw_write(w, v & 0xeffffcff); + ret = t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); + t6040_disp_mark(s, step, 0x02 | (ret ? 0x80 : 0)); + v = t6040_psw_read(w); + } + if (pre) + pre(s); + t6040_disp_mark(s, step, 0x03); + t6040_psw_write(w, (v & 0xfffffcf0) | 0x300); + ret = t6040_psw_poll(w, PS_BUSY, 0); + t6040_disp_mark(s, step, 0x04 | (ret ? 0x80 : 0)); + ret = t6040_psw_poll(w, PS_ACTUAL, 0); + t6040_disp_mark(s, step, 0x05 | (ret ? 0x80 : 0)); + return ret; +} + +/* DISP_SYS's hooks before its PS write: the axi2af bridge bit0s, then the retention pair. */ +static void t6040_disp_sys_pre(struct t6040_sleep *s) +{ + void __iomem *bridge = s->axi2af_bridge[AXI2AF_DISP_SYS]; + + if (bridge) { + writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); + } + if (s->disp_pwrgate) { + writel(3, s->disp_pwrgate + DISP_PWRGATE_A); + writel(0, s->disp_pwrgate + DISP_PWRGATE_B); + } + t6040_disp_mark(s, 0x30, 0x06); +} + +/* dart-dcp's outstanding-transaction counters ([20:16]) at 0 before its domain goes. */ +static int t6040_dcp_dart_idle(struct t6040_sleep *s) +{ + static const u16 regs[] = { 0x240, 0x244, 0x250, 0x254, 0x260, 0x270 }; + unsigned int i, us = 0; + + if (!s->dcp_dart) + return 0; + for (i = 0; i < ARRAY_SIZE(regs); i++) + while (readl(s->dcp_dart + regs[i]) & GENMASK(20, 16)) { + if (++us > DISP_PS_TIMEOUT_US) { + t6040_disp_mark(s, 0x08, 0x80 | i); + return -ETIMEDOUT; + } + udelay(1); + } + t6040_disp_mark(s, 0x08, 0x00); + return 0; +} + +/* The internal display's three domains, child first, after its sub-blocks (the order). */ +/* A display pipe's PMC votes, released in the order; @rail the cluster rail bit or 0. */ +static int t6040_pmc_disp_release(struct t6040_sleep *s, unsigned int p, u32 rail) +{ + int ret = 0; + u32 v; + + if (s->pmc_bwr) + writel(readl(s->pmc_bwr + PMC_BOOST) & ~BIT(p), s->pmc_bwr + PMC_BOOST); + if (s->pmc_dvmr) { + writel(readl(s->pmc_dvmr) & ~BIT(p), s->pmc_dvmr); + if (readl_poll_timeout_atomic(s->pmc_dvmr + PMC_DVMR_ACK, v, !(v & BIT(0)), 1, + PMC_TIMEOUT_US)) + ret |= 1; + if (s->pmc_dcp) + writel(hweight32(readl(s->pmc_dvmr)), s->pmc_dcp + PMC_DCP_COUNT); + } + if (s->pmc_bwr) + writel(readl(s->pmc_bwr) & ~BIT(p ? p + 2 : 0), s->pmc_bwr); + if (rail && s->pmc_dcp) { + writel(readl(s->pmc_dcp + PMC_RAIL) & ~rail, s->pmc_dcp + PMC_RAIL); + if (readl_poll_timeout_atomic(s->pmc_dcp + PMC_RAIL_ACK, v, !(v & rail), 1, + PMC_TIMEOUT_US)) + ret |= 2; + } + return ret; +} + +/* As T8140: the coprocessor's CPU_CONTROL to 0, twice, right before its CPU word goes. */ +static void t6040_dcp_cold_stop(struct t6040_sleep *s, unsigned int p) +{ + if (!s->dcp_asc_ctl[p]) + return; + writel(0, s->dcp_asc_ctl[p]); + writel(0, s->dcp_asc_ctl[p]); +} + +/* One power word down in the two steps (any AUTO first). Returns 0x80 on a timeout. */ +static u32 t6040_soc_ps_off(void __iomem *w) +{ + u32 v = readl(w), cur; + u32 to = 0; + + if (!FIELD_GET(PS_ACTUAL, v) && !FIELD_GET(PS_TARGET, v)) + return 0; + if (v & PS_AUTO_ENABLE) { + writel(v & 0xeffffcff, w); + if (readl_poll_timeout_atomic(w, cur, FIELD_GET(PS_ACTUAL, cur) == 0xf, 1, + PMC_TIMEOUT_US)) + to = 0x80; + } + writel((readl(w) & 0xfffffcf0) | 0x300, w); + if (readl_poll_timeout_atomic(w, cur, !(cur & PS_BUSY), 1, PMC_TIMEOUT_US) || + readl_poll_timeout_atomic(w, cur, !FIELD_GET(PS_ACTUAL, cur), 1, PMC_TIMEOUT_US)) + to = 0x80; + return to; +} + +/* + * The stopped DCPEXT pipes down it, DCPEXT1 first. Marks 0x56SS00RR, + * SS 0x80 | pipe << 4 | step (1 PMC, 2 CPU, 3 FE, 4 SYS), RR 0x80 on a timeout. + */ +static void t6040_dispext_off(struct t6040_sleep *s) +{ + int n; + + if (!s->soc_ps) + return; + for (n = 1; n >= 0; n--) { + u32 base = 0x80 | n << 4; + void __iomem *bridge = s->axi2af_bridge[t6040_dispext[n].axi2af]; + + t6040_disp_mark(s, base | 1, 0); + if (READ_ONCE(pmc_seq)) + t6040_disp_mark(s, base | 1, + 0x1 | t6040_pmc_disp_release(s, n + 1, n ? 0 : PMC_RAIL_EXT) << 4); + t6040_disp_mark(s, base | 2, 0); + t6040_dcp_cold_stop(s, n + 1); + t6040_disp_mark(s, base | 2, 1); + t6040_disp_mark(s, base | 2, + 2 | t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].cpu)); + t6040_disp_mark(s, base | 3, + t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].fe)); + if (bridge) { + writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); + } + if (s->dispext_pwrgate) { + writel(3, s->dispext_pwrgate + t6040_dispext[n].pg_a); + writel(0, s->dispext_pwrgate + t6040_dispext[n].pg_b); + } + t6040_disp_mark(s, base | 4, + t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].sys)); + } +} + +static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group *g) +{ + struct t6040_psw cpu = { .map = g->regmap, .off = g->first }; + struct t6040_psw fe = { .io = s->soc_east, .off = PS_DISP_FE }; + struct t6040_psw sys = { .io = s->soc_east, .off = PS_DISP_SYS }; + int ret; + + if (!s->soc_east) + return -ENODEV; + s->disp_down = true; + /* The PMS DCP voters to 0 first, as the the platform entry trace and T8140's driver end. */ + if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { + s->pmc_dcp_saved[0] = readl(s->pmc_dcp); + s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); + if (READ_ONCE(pmc_seq)) { + t6040_disp_mark(s, 0x0a, 0); + t6040_disp_mark(s, 0x0a, t6040_pmc_disp_release(s, 0, PMC_RAIL_INT) << 4 | + (readl(s->pmc_dcp + PMC_RAIL) & 0xf)); + } else { + writel(0, s->pmc_dcp); + writel(0, s->pmc_dcp + 4); + } + t6040_disp_mark(s, 0x09, (s->pmc_dcp_saved[0] & 0xf) | (s->pmc_dcp_saved[1] & 0xf) << 4); + } + /* + * DISP_CPU's word refuses any write from here: rewriting its own value at runtime reset + * the SoC (the platform reaches it through its "DISP-CPU-V" PMGR device). Its CPU is + * left in automatic mode and the PMGR gates it once the firmware idles in WFI with the + * run bit kept: wait for ACTUAL 0. Mark 0x560f00RR first: RR ACTUAL | AUTO_ENABLE << 4 | + * (TARGET != 0) << 5; then 0x561000RR, RR 0x10 gated, 0x90 not gated in time. + */ + { + u32 v = t6040_psw_read(&cpu); + + t6040_disp_mark(s, 0x0f, FIELD_GET(PS_ACTUAL, v) | !!(v & PS_AUTO_ENABLE) << 4 | + !!FIELD_GET(PS_TARGET, v) << 5); + if (FIELD_GET(PS_ACTUAL, v)) + t6040_dcp_dart_idle(s); + if (READ_ONCE(disp_cpu_seq) && FIELD_GET(PS_ACTUAL, v)) { + if (s->ptd && READ_ONCE(pmp_disp_release)) { + u64 req = readq(s->ptd + PTD_REQ_READ), ack; + int to; + + t6040_disp_mark(s, 0x0d, (req >> PTD_DISP_INT) & 1); + writeq(req & ~BIT_ULL(PTD_DISP_INT), s->ptd + PTD_REQ_WRITE); + to = readq_poll_timeout_atomic(s->ptd + PTD_ACK, ack, + !(ack & BIT_ULL(PTD_DISP_INT)), 10, + 50000); + t6040_disp_mark(s, 0x0d, 0x10 | (to ? 0x80 : 0) | + ((ack >> PTD_DISP_INT) & 1)); + } + t6040_disp_mark(s, 0x0b, 0); + t6040_dcp_cold_stop(s, 0); + if (s->disp_pwrgate) { + writel(3, s->disp_pwrgate + DISP_PWRGATE_A); + writel(0, s->disp_pwrgate + DISP_PWRGATE_B); + } + t6040_disp_mark(s, 0x0b, 1); + t6040_psw_off(s, &cpu, 0x0c, NULL); + } + ret = t6040_psw_poll_long(&cpu, PS_ACTUAL, 0, DISP_CPU_GATE_US); + t6040_disp_mark(s, 0x10, ret ? 0x90 : 0x10); + if (ret && READ_ONCE(disp_need_cpu_gated)) + return ret; + ret = 0; + } + if (!ret) + ret = t6040_psw_off(s, &fe, 0x20, NULL); + if (!ret) + ret = t6040_psw_off(s, &sys, 0x30, t6040_disp_sys_pre); + return ret; +} + +/* + * The raise (the mirror): TARGET 15, poll ACTUAL 15, poll bit11; for DISP_SYS the + * retention pair (A := 0, B := 1) and the axi2af bridge bit0s set; then bits 9:8 cleared and + * AUTO_ENABLE back where the platform sets it (DISP_CPU, DISP_SYS). Marks 0x56 step 0x4x/0x5x/0x6x. + */ +static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, u32 step, + bool sys, bool autoen) +{ + u32 v = t6040_psw_read(w); + int ret, ret2; + + t6040_disp_mark(s, step, 0x01); + t6040_psw_write(w, (v & ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS)) | PS_TARGET); + ret = t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); + ret2 = t6040_psw_poll(w, PS_BUSY, 0); + t6040_disp_mark(s, step, 0x02 | (ret || ret2 ? 0x80 : 0)); + if (sys) { + void __iomem *bridge = s->axi2af_bridge[AXI2AF_DISP_SYS]; + + if (s->disp_pwrgate) { + writel(0, s->disp_pwrgate + DISP_PWRGATE_A); + writel(1, s->disp_pwrgate + DISP_PWRGATE_B); + } + if (bridge && !ret) { + writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); + } + t6040_disp_mark(s, step, 0x06); + } + v = t6040_psw_read(w); + t6040_psw_write(w, (v & ~PS_AUTO_ENABLE) | PS_FLAGS | (autoen ? PS_AUTO_ENABLE : 0)); + t6040_disp_mark(s, step, 0x05); + return ret; +} + +/* The internal display back on, parent first, whatever brought .enter back. */ +static void t6040_disp_back(struct t6040_sleep *s) +{ + struct t6040_sleep_group *g = NULL; + unsigned int i; + + if (!s->disp_down) + return; + s->disp_down = false; + for (i = 0; i < s->ngroups; i++) + if (!strcmp(s->groups[i].name, "disp")) + g = &s->groups[i]; + { + struct t6040_psw sys = { .io = s->soc_east, .off = PS_DISP_SYS }; + struct t6040_psw fe = { .io = s->soc_east, .off = PS_DISP_FE }; + + if (t6040_psw_on(s, &sys, 0x40, true, true)) + return; + if (t6040_psw_on(s, &fe, 0x50, false, false)) + return; + /* DISP_CPU's word can't be written from here: automatic mode raises it. */ + (void)g; + } + if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { + writel(s->pmc_dcp_saved[1], s->pmc_dcp + 4); + writel(s->pmc_dcp_saved[0], s->pmc_dcp); + } +} + +static bool disp_probe; +module_param(disp_probe, bool, 0644); +MODULE_PARM_DESC(disp_probe, "Bring-up: at the commit point record the internal display's words and refuse"); + +/* + * The internal display as the DCP's stop left it: bits 0-10 the sub-blocks +0x8..+0x58 with + * TARGET != 0, 11-14 DISP_CPU ACTUAL, 15 its AUTO_ENABLE, 16-19 DISP_FE ACTUAL, 20-23 + * DISP_SYS ACTUAL. + */ +static u32 t6040_disp_probe_word(struct t6040_sleep *s) +{ + u32 w = 0, val, off; + unsigned int i; + + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; + + if (strcmp(g->name, "disp")) + continue; + for (off = g->first + PS_STRIDE; off <= g->last && off - g->first <= 11 * PS_STRIDE; + off += PS_STRIDE) + if (!regmap_read(g->regmap, off, &val) && FIELD_GET(PS_TARGET, val)) + w |= BIT((off - g->first) / PS_STRIDE - 1); + if (!regmap_read(g->regmap, g->first, &val)) + w |= FIELD_GET(PS_ACTUAL, val) << 11 | !!(val & PS_AUTO_ENABLE) << 15; + } + if (s->soc_east) { + w |= FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_DISP_FE)) << 16; + w |= FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_DISP_SYS)) << 20; + } + return w; +} + +static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) +{ + bool quiesce = READ_ONCE(axi2af_quiesce), with_cpu = READ_ONCE(lower_cpu); + unsigned int i, left = 0; + + /* + * The platform takes a DCPEXT pipe down CPU, FE, SYS (child first). The group loop below + * lowers FE and SYS and leaves the CPU, whose word then refuses the write: + * the DCPEXT pipes go first. + */ + if (READ_ONCE(dispext_off)) + t6040_dispext_off(s); + + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; + u32 off = g->last; + + if (quiesce) + t6040_axi2af_quiesce(s, t6040_axi2af_of_group(g->name)); + for (;;) { + bool cpu = off == g->first; + u32 val, cur; + + /* An automatically gated CPU still powered: wait for the PMGR to gate it. */ + if (cpu && !regmap_read(g->regmap, off, &val) && (val & PS_AUTO_ENABLE) && + FIELD_GET(PS_ACTUAL, val)) { + t6040_mark_raw(s, T6040_DISP_MARK(i, 1, off)); + t6040_mark_raw(s, T6040_DISP_MARK(i, + t6040_regmap_poll_off(g->regmap, off, &cur) ? 3 : 2, off)); + } + if ((!cpu || with_cpu) && !regmap_read(g->regmap, off, &val) && + t6040_sleep_ps_on(val, cpu)) { + t6040_mark_raw(s, T6040_DISP_MARK(i, 0, off)); + val &= ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS | PS_DEV_DISABLE | + PS_BUSY | PS_RESET_REQ); + regmap_write(g->regmap, off, val); + if (t6040_regmap_poll_off(g->regmap, off, &cur)) { + t6040_mark_raw(s, T6040_DISP_MARK(i, 4, off)); + pr_warn("t6040-sleep: %s +%#x still on: %#x\n", g->name, + off, cur); + left++; + } else { + t6040_mark_raw(s, T6040_DISP_MARK(i, 8, off)); + } + } + if (off == g->first) + break; + off -= PS_STRIDE; + } + if (!i && !strcmp(g->name, "disp") && READ_ONCE(disp_seq_off) && + t6040_disp_off(s, g)) + left++; + } + return left; +} + +/* Which display groups are on, one bit each (the refusal mark). */ +static u32 t6040_sleep_groups_on(struct t6040_sleep *s) +{ + unsigned int i; + u32 mask = 0; + + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; + u32 off, val; + + for (off = g->first; off <= g->last; off += PS_STRIDE) + if (regmap_read(g->regmap, off, &val) || + t6040_sleep_ps_on(val, off == g->first)) + mask |= BIT(i); + } + return mask; +} + +/* The first display word on, for the refusal mark: index, flag bits, ACTUAL and TARGET. */ +static u32 t6040_sleep_first_on(struct t6040_sleep *s) +{ + unsigned int i; + + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; + u32 off, val; + + for (off = g->first; off <= g->last; off += PS_STRIDE) { + if (regmap_read(g->regmap, off, &val) || + !t6040_sleep_ps_on(val, off == g->first)) + continue; + return (((off - g->first) / PS_STRIDE) & 0xf) << 12 | + !!(val & PS_AUTO_ENABLE) << 8 | !!(val & PS_DEV_DISABLE) << 9 | + !!(val & PS_BUSY) << 10 | !!(val & PS_RESET_REQ) << 11 | + (val & (PS_ACTUAL | PS_TARGET)); + } + } + return 0; +} + +static bool axi2af_gate; +module_param(axi2af_gate, bool, 0644); +MODULE_PARM_DESC(axi2af_gate, "Refuse the commit while any axi2af domain is still powered (the panic gate)"); + +/* Which of the 15 axi2af domains read ACTUAL != 0, one bit each. */ +static u32 t6040_axi2af_on_mask(struct t6040_sleep *s) +{ + u32 mask = 0; + int i; + + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { + void __iomem *ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); + + if (ps && FIELD_GET(PS_ACTUAL, readl(ps))) + mask |= BIT(i); + } + return mask; +} + +/* After the late and noirq device suspend, before the CPUs go offline: a bring-up mark. */ +static int t6040_sleep_prepare_late(void) +{ + t6040_mark(t6040_sleep, 0x03); + return 0; +} + +static int t6040_sleep_enter(suspend_state_t state) +{ + struct t6040_sleep *s = t6040_sleep; + unsigned int dry = READ_ONCE(t6040_sleep_dry); + int ret; + + if (dry) + return t6040_sleep_dry_enter(s, dry); + + t6040_mark(s, 0x05); + t6040_pmc_cp(s, 0); + if (READ_ONCE(disp_probe)) { + /* Bring-up: what the DCP's stop left, then refuse with it kept (0x55). */ + t6040_mark_raw(s, 0x55000000 | t6040_disp_probe_word(s)); + WRITE_ONCE(pm_bringup_dev_mark, NULL); + pr_info("t6040-sleep: display probe at the commit point; refusing\n"); + return -EBUSY; + } + if (READ_ONCE(lower_display)) { + unsigned int left = t6040_sleep_lower_display(s); + + t6040_mark(s, 0x06); + if (left) + pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); + } + /* + * The PMGR quiesce panics unless all 15 axi2af domains read ACTUAL 0, and a commit with + * some on reset the SoC about a second after the S2R timer was armed. Refuse + * with the domains still on: 0x59 then a 15-bit mask, bit i = t6040_axi2af[i]. + */ + t6040_pmc_cp(s, 1); + if (READ_ONCE(axi2af_gate)) { + /* The commit lowers the ATC complexes (bits 0-3) and ANS (bit 9) itself. */ + u32 on = t6040_axi2af_on_mask(s) & + ~((READ_ONCE(lower_atc) ? GENMASK(3, 0) : 0) | BIT(9)); + + if (on) { + t6040_mark_raw(s, 0x59000000 | on); + WRITE_ONCE(pm_bringup_dev_mark, NULL); + pr_info("t6040-sleep: at the commit point: axi2af domains still on %#x; refusing\n", + on); + t6040_disp_back(s); + return -EBUSY; + } + } + if (t6040_sleep_total_blockers(s) || !allow_entry) { + unsigned int on = t6040_sleep_log_groups(s); + + /* + * The reason, kept through what may follow: 0x5bGGWRVV, GG the groups on, then the + * first word on: W its index in its group (0 the CPU), R bit 0 AUTO_ENABLE, bit 1 + * DEV_DISABLE, bit 2 BUSY, bit 3 RESET, VV its ACTUAL and TARGET. + */ + t6040_mark_raw(s, 0x5b000000 | (t6040_sleep_groups_on(s) & 0xff) << 16 | + t6040_sleep_first_on(s)); + WRITE_ONCE(pm_bringup_dev_mark, NULL); + + pr_info("t6040-sleep: at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", + num_online_cpus(), on); + t6040_disp_back(s); + return -EBUSY; + } + + /* Unreachable with the current unknowns. #1208's order. */ + if (t6040_sleep_refuse(s)) { + /* 0x5aGGWWWW: why, as t6040_sleep_refuse() recorded it (GG display groups on). */ + t6040_mark_raw(s, 0x5a000000 | (t6040_refuse_why & 0xffffff)); + WRITE_ONCE(pm_bringup_dev_mark, NULL); + t6040_disp_back(s); + return -EBUSY; + } + t6040_mark(s, 0x10); + ret = spin_table_hold_parked(); + if (ret) { + t6040_mark_restore(s); + t6040_disp_back(s); + return ret; + } + t6040_mark(s, 0x11); + ret = t6040_sleep_coprocs_check(); + if (!ret) + ret = t6040_sleep_enclave_tell(); + if (ret) { + spin_table_unhold_parked(); + t6040_mark_restore(s); + t6040_disp_back(s); + return ret; + } + t6040_mark(s, 0x12); + t6040_pmc_cp(s, 2); + s->pcie_ans_stopped = false; + t6040_sleep_coprocs_stop(true); + t6040_sleep_coprocs_stop(false); + t6040_mark(s, 0x13); + t6040_pmc_cp(s, 3); + t6040_wake_arm(s); + t6040_mark(s, 0x14); + + s->cntkctl = read_sysreg(cntkctl_el1); + pm_set_resume_via_firmware(); + t6040_mark(s, 0x15); + ret = cpu_suspend(0, t6040_sleep_commit); + if (!ret) + t6040_sleep_return(s); + else + t6040_mark_restore(s); + if (s->alarm_ok) + pr_info("t6040-sleep: marks: PMU comparator was %#x (reply %#x), cpu_suspend %d\n", + s->alarm_saved, s->alarm_hdr, ret); + + if (s->returned) + t6040_ret_mark(s, 0x41); + t6040_wake_restore(s); + if (s->returned) + t6040_ret_mark(s, 0x42); + t6040_disp_back(s); + if (s->returned) + t6040_ret_mark(s, 0x43); + return ret; +} + +/* + * After .enter: 0x44 at the end of syscore_resume (IRQs still off, ours is registered last), + * 0x45 in .wake (IRQs on, the secondary CPUs back, before the noirq device resume). + */ +static void t6040_sleep_syscore_resume(void *data) +{ + struct t6040_sleep *s = t6040_sleep; + + if (s && s->returned && READ_ONCE(pm_bringup_dev_mark)) + t6040_ret_mark(s, 0x44); +} + +static const struct syscore_ops t6040_sleep_syscore_ops = { + .resume = t6040_sleep_syscore_resume, +}; + +static struct syscore t6040_sleep_syscore = { + .ops = &t6040_sleep_syscore_ops, +}; + +static void t6040_sleep_wake(void) +{ + struct t6040_sleep *s = t6040_sleep; + + if (s && s->returned && READ_ONCE(pm_bringup_dev_mark)) + t6040_ret_mark(s, 0x45); +} + +static void t6040_sleep_end(void) +{ + struct t6040_sleep *s = t6040_sleep; + unsigned int dry = READ_ONCE(t6040_sleep_dry); + + t6040_sleep_clear_wd2(s); + WRITE_ONCE(pm_bringup_dev_mark, NULL); + /* Any return or refusal after .begin: the wake alarm back where the RTC driver set it. */ + if (s->alarm_ok) { + u32 v = s->alarm_saved; + + t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); + s->alarm_ok = false; + } + if (dry) + pr_info("t6040-sleep: dry run %u over, WD2 off\n", dry); + WRITE_ONCE(t6040_sleep_dry, 0); +} + +static const struct platform_suspend_ops t6040_sleep_ops = { + .valid = t6040_sleep_valid, + .begin = t6040_sleep_begin, + .prepare = t6040_sleep_prepare, + .prepare_late = t6040_sleep_prepare_late, + .enter = t6040_sleep_enter, + .wake = t6040_sleep_wake, + .end = t6040_sleep_end, +}; + +static int t6040_sleep_parse_groups(struct t6040_sleep *sleep) +{ + struct device_node *np = sleep->dev->of_node; + const char *prop = "apple,check-ps-b4-sleep"; + int n, i; + + /* Each group: the PMGR syscon, the CPU's offset and the last sub-block's. */ + n = of_property_count_u32_elems(np, prop); + if (n <= 0 || n % 3) + return dev_err_probe(sleep->dev, -EINVAL, "no valid %s\n", prop); + n /= 3; + if (of_property_count_strings(np, "apple,check-ps-b4-sleep-names") != n) + return dev_err_probe(sleep->dev, -EINVAL, "one name per %s group\n", prop); + + sleep->groups = devm_kcalloc(sleep->dev, n, sizeof(*sleep->groups), GFP_KERNEL); + if (!sleep->groups) + return -ENOMEM; + + for (i = 0; i < n; i++) { + struct t6040_sleep_group *g = &sleep->groups[i]; + struct of_phandle_args args; + int ret; + + ret = of_parse_phandle_with_fixed_args(np, prop, 2, i, &args); + if (ret) + return dev_err_probe(sleep->dev, ret, "%s[%d]\n", prop, i); + g->regmap = syscon_node_to_regmap(args.np); + of_node_put(args.np); + if (IS_ERR(g->regmap)) + return dev_err_probe(sleep->dev, PTR_ERR(g->regmap), "%s[%d] regmap\n", + prop, i); + g->first = args.args[0]; + g->last = args.args[1]; + if (g->first > g->last || (g->last - g->first) % PS_STRIDE || + (g->last - g->first) / PS_STRIDE >= PS_MAX_PER_GROUP) + return dev_err_probe(sleep->dev, -EINVAL, "%s[%d]: bad range %#x..%#x\n", + prop, i, g->first, g->last); + of_property_read_string_index(np, "apple,check-ps-b4-sleep-names", i, &g->name); + } + sleep->ngroups = n; + + return 0; +} + +/* + * A watched device's own power word: its first power domain, a PMGR power-state + * node whose reg is the offset in its parent's syscon. Optional. + */ +static void t6040_sleep_watch_init(struct t6040_sleep *s, unsigned int i, + const char *compatible, const char *name) +{ + struct t6040_sleep_watch *w = &s->watch[i]; + struct device_node *np, *pd, *pmgr; + struct regmap *regmap; + + w->compatible = compatible; + w->name = name; + np = of_find_compatible_node(NULL, NULL, compatible); + if (!np) + return; + pd = of_parse_phandle(np, "power-domains", 0); + of_node_put(np); + if (!pd) + return; + pmgr = of_get_parent(pd); + if (pmgr && !of_property_read_u32(pd, "reg", &w->ps_offset)) { + regmap = syscon_node_to_regmap(pmgr); + if (!IS_ERR(regmap)) { + w->ps_regmap = regmap; + w->has_ps = true; + } + } + of_node_put(pmgr); + of_node_put(pd); + if (!w->has_ps) + dev_warn(s->dev, "%s: no power word to watch\n", name); +} + +/* A named register block, shared with the owning driver: mapped, not claimed. Optional. */ +static void __iomem *t6040_sleep_map(struct device *dev, const char *name) +{ + struct device_node *np = dev->of_node; + struct resource res; + int index; + + index = of_property_match_string(np, "reg-names", name); + if (index < 0 || of_address_to_resource(np, index, &res)) + return NULL; + + return t6040_ioremap_np(dev, res.start, resource_size(&res)); +} + +/* A tunable table on the apple,apcie node, named by its matching reg-name. */ +static struct apple_tunable *t6040_apcie_tunable(struct t6040_sleep *s, struct device_node *np, + const char *name, const char *prop) +{ + int idx = of_property_match_string(np, "reg-names", name); + struct apple_tunable *t; + struct resource res; + + if (idx < 0 || of_address_to_resource(np, idx, &res)) + return NULL; + t = devm_apple_tunable_parse(s->dev, np, prop, &res); + + return IS_ERR(t) ? NULL : t; +} + +/* A control window on the apple,apcie node, with its optional tunable. */ +static void __iomem *t6040_apcie_block(struct t6040_sleep *s, struct device_node *np, + const char *name, const char *prop, + struct apple_tunable **tunable) +{ + int idx = of_property_match_string(np, "reg-names", name); + + if (idx < 0) + return NULL; + if (prop) { + *tunable = t6040_apcie_tunable(s, np, name, prop); + if (!*tunable) + return NULL; + } + + return of_iomap(np, idx); +} + +/* + * ANS's storage-apcie windows + PLL/lane tunables, from the apple,apcie node + * the boot object publishes from the ADT. Our static tree has none + * of these, so until aura fills the node @valid stays false and a real sleep + * with ANS stopped refuses (t6040_sleep_refuse). The reg-names and tunable + * property names match aurora #1208's apple,apcie shape exactly, so the same + * loader fills both SoCs. The GP PCIe host (pcie@1cb0000000, Wi-Fi + SD) is a + * different node and is left to cold-recover. + */ +static void t6040_sleep_map_apcie(struct t6040_sleep *s) +{ + struct t6040_apcie_st *a = &s->apcie_st; + struct device_node *np = of_parse_phandle(s->dev->of_node, "apple,apcie", 0); + + if (!np) + return; + a->axi = t6040_apcie_block(s, np, "axi2af", "apple,tunable-axi2af", &a->axi_tunable); + a->cio3pll = t6040_apcie_block(s, np, "cio3pll", "apple,tunable-cio3pll", + &a->cio3pll_tunable); + a->clkgen = t6040_apcie_block(s, np, "clkgen", "apple,tunable-clkgen", &a->clkgen_tunable); + a->phy = t6040_apcie_block(s, np, "phy-block", "apple,tunable-phy", &a->phy_tunable); + a->rc = t6040_apcie_block(s, np, "rc", NULL, NULL); + a->phy_ip = t6040_apcie_block(s, np, "phy-ip", "apple,tunable-phy-ip-pll", &a->phy_ip_pll); + a->phy_ip_lanes = t6040_apcie_tunable(s, np, "phy-ip", "apple,tunable-phy-ip-auspma"); + of_node_put(np); + + a->valid = a->axi && a->cio3pll && a->clkgen && a->phy && a->rc && a->phy_ip && + a->phy_ip_lanes; + if (!a->valid) + dev_info(s->dev, + "no storage-apcie windows/tunables (apple,apcie): a real sleep with ANS stopped refuses\n"); +} + +/* Maps the mailbox at @pa if it is aura's, or returns NULL. */ +static struct t6040_s2r_mailbox *t6040_sleep_mailbox_at(struct t6040_sleep *s, u64 pa) +{ + struct t6040_s2r_mailbox *mbox; + + mbox = devm_memremap(s->dev, pa, sizeof(*mbox), MEMREMAP_WB); + if (IS_ERR(mbox)) + return NULL; + if (mbox->magic != T6040_S2R_MAGIC) { + dev_warn(s->dev, "s2r-mailbox magic %#llx is not AURAS2R1\n", mbox->magic); + devm_memunmap(s->dev, mbox); + return NULL; + } + return mbox; +} + +/* The aura #117 mailbox, if the boot object published one. Optional (a blocker if absent). */ +static void t6040_sleep_map_mailbox(struct t6040_sleep *s) +{ + struct device_node *chosen = of_find_node_by_path("/chosen"); + u64 pa; + int ret; + + if (!chosen) + return; + ret = of_property_read_u64(chosen, "aurora,s2r-mailbox", &pa); + of_node_put(chosen); + if (ret) + return; + + s->mbox = t6040_sleep_mailbox_at(s, pa); + if (s->mbox) + s->mbox_pa = pa; +} + +#ifdef CONFIG_HIBERNATION +/* + * Restored from a hibernation image, this kernel runs under the aura of the boot that resumed it. + * That aura moved with the boot object, and so did its mailbox: the one mapped at probe was the + * hibernating boot's, and the boot core would come back from a sleep through this boot's. Take this + * boot's address from the device tree aura keeps at its fixed slot. Without it, drop the mailbox, so + * that a suspend refuses for it instead of arming memory that is no longer aura's. + */ +static int t6040_sleep_restore(struct device *dev) +{ + struct t6040_sleep *s = dev_get_drvdata(dev); + struct t6040_s2r_mailbox *mbox = NULL; + u64 pa = 0; + int ret; + + ret = aura_hibernate_boot_chosen_u64("aurora,s2r-mailbox", &pa); + if (!ret && s->mbox && pa == s->mbox_pa) + return 0; + if (!ret) + mbox = t6040_sleep_mailbox_at(s, pa); + if (s->mbox) + devm_memunmap(dev, s->mbox); + if (mbox) + dev_info(dev, "restored: s2r-mailbox %#llx, was %#llx on the boot that hibernated\n", + pa, s->mbox_pa); + else + dev_warn(dev, "restored: this boot's s2r-mailbox is unknown (%d, %#llx): dropped\n", + ret, pa); + s->mbox = mbox; + s->mbox_pa = mbox ? pa : 0; + return 0; +} +#endif + +static const struct dev_pm_ops t6040_sleep_pm_ops = { +#ifdef CONFIG_HIBERNATION + .restore = t6040_sleep_restore, +#endif +}; + +/* + * Bring-up, root only: write one value to the internal display's CPU (pmgr_disp +0) or front + * end (soc_east +0x368) power word at runtime, to tell a write that is refused from a state + * change that is. "cpu [value]" or "fe [value]", no value: the word's own. Marks 0x567S00RR around the write. + */ +static ssize_t t6040_ps_write_dbg(struct file *f, const char __user *ubuf, size_t len, + loff_t *ppos) +{ + struct t6040_sleep *s = file_inode(f)->i_private; + char buf[48], which[8]; + u32 val = 0, before = 0, after = 0; + unsigned int i; + int n; + + if (len >= sizeof(buf)) + return -EINVAL; + if (copy_from_user(buf, ubuf, len)) + return -EFAULT; + buf[len] = 0; + n = sscanf(buf, "%7s %x", which, &val); + if (n < 1) + return -EINVAL; + if (!strcmp(which, "cpu")) { + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; + + if (strcmp(g->name, "disp")) + continue; + regmap_read(g->regmap, g->first, &before); + if (n < 2) + val = before; + if (s->spmi0) + t6040_mark_save(s); + t6040_disp_mark(s, 0x70, 0x01); + regmap_write(g->regmap, g->first, val); + udelay(50); + regmap_read(g->regmap, g->first, &after); + t6040_disp_mark(s, 0x70, 0x02); + } + } else if (!strcmp(which, "fe") && s->soc_east) { + before = readl(s->soc_east + PS_DISP_FE); + if (n < 2) + val = before; + if (s->spmi0) + t6040_mark_save(s); + t6040_disp_mark(s, 0x71, 0x01); + writel(val, s->soc_east + PS_DISP_FE); + udelay(50); + after = readl(s->soc_east + PS_DISP_FE); + t6040_disp_mark(s, 0x71, 0x02); + } else { + return -EINVAL; + } + pr_info("t6040-sleep: ps test %s: %#x written over %#x, now %#x\n", which, val, before, after); + return len; +} + +static const struct file_operations t6040_ps_write_fops = { + .owner = THIS_MODULE, + .write = t6040_ps_write_dbg, + .open = simple_open, + .llseek = noop_llseek, +}; + +/* The GP PCIe host's port and PHY windows and its ports' PERST# pins, for the commit. */ +static void t6040_sleep_map_gp(struct t6040_sleep *s) +{ + static const char *const ports[] = { "port0", "port1" }; + static const char *const phys[] = { "phy0", "phy1" }; + struct device_node *host, *child; + struct device_node *pins = NULL; + struct resource res; + int p, idx; + + s->gp_perst[0] = s->gp_perst[1] = -1; + host = of_find_compatible_node(NULL, NULL, "apple,t6041-pcie"); + if (!host) + return; + if (!of_device_is_available(host)) + goto out; + for (p = 0; p < 2; p++) { + idx = of_property_match_string(host, "reg-names", ports[p]); + if (idx >= 0 && !of_address_to_resource(host, idx, &res)) + s->gp_port[p] = t6040_ioremap_np(s->dev, res.start, resource_size(&res)); + idx = of_property_match_string(host, "reg-names", phys[p]); + if (idx >= 0 && !of_address_to_resource(host, idx, &res)) + s->gp_phy[p] = t6040_ioremap_np(s->dev, res.start, resource_size(&res)); + } + for_each_available_child_of_node(host, child) { + struct of_phandle_args args; + u32 reg[1]; + + if (of_property_read_u32_array(child, "reg", reg, 1)) + continue; + p = (reg[0] >> 11) & 0x1f; /* the device number of pci@N,0 */ + if (p > 1 || of_parse_phandle_with_args(child, "reset-gpios", "#gpio-cells", 0, + &args)) + continue; + if (args.args_count >= 2 && (!pins || pins == args.np)) { + if (!pins) + pins = of_node_get(args.np); + s->gp_perst[p] = args.args[0]; + s->gp_perst_low[p] = args.args[1] & 1; /* GPIO_ACTIVE_LOW */ + } + of_node_put(args.np); + } + if (pins) { + if (!of_address_to_resource(pins, 0, &res)) + s->gp_pins = t6040_ioremap_np(s->dev, res.start, resource_size(&res)); + of_node_put(pins); + } + dev_info(s->dev, "GP PCIe: ports %d/%d phys %d/%d, PERST# pins %d/%d\n", + !!s->gp_port[0], !!s->gp_port[1], !!s->gp_phy[0], !!s->gp_phy[1], + s->gp_perst[0], s->gp_perst[1]); +out: + of_node_put(host); +} + +static int t6040_sleep_probe(struct platform_device *pdev) +{ + struct t6040_sleep *sleep; + int ret, i; + + sleep = devm_kzalloc(&pdev->dev, sizeof(*sleep), GFP_KERNEL); + if (!sleep) + return -ENOMEM; + sleep->dev = &pdev->dev; + + ret = t6040_sleep_parse_groups(sleep); + if (ret) + return ret; + + /* + * Optional: the commit's and the wake's hardware (all NULL on absence; + * every write is guarded and the gate refuses if a required one is + * missing). The axi2af fabric bridges are fixed PAs, not DT. + */ + BUILD_BUG_ON(ARRAY_SIZE(t6040_axi2af) != T6040_AXI2AF_DOMAINS); + BUILD_BUG_ON(ARRAY_SIZE(t6040_tvm) != ARRAY_SIZE(((struct t6040_sleep *)0)->tvm_ctl)); + BUILD_BUG_ON(ARRAY_SIZE(t6040_acc_apsc_pa) != ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); + sleep->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); + sleep->wdt = t6040_sleep_map(&pdev->dev, "wdt"); + { + struct resource wres; + int widx = of_property_match_string(pdev->dev.of_node, "reg-names", "wdt"); + + sleep->wdt_stub_ok = widx >= 0 && + !of_address_to_resource(pdev->dev.of_node, widx, &wres) && + wres.start == T6040_WDT_PA; + dev_info(&pdev->dev, "wake stub (WD2 60 s before cpu_resume): %s\n", + sleep->wdt_stub_ok ? "ready" : "no, wdt block is not the AON WDT"); + } + sleep->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); + sleep->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); + sleep->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); + sleep->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); + if (sleep->spmi0) { + u32 rx[2]; + + if (!t6040_pmu_cmd(sleep, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_READ4), NULL, rx, 2)) + dev_info(&pdev->dev, "PMU comparator at probe: %#x (reply %#x)%s\n", rx[1], rx[0], + (rx[1] & 0xffff0000) == T6040_MARK(0) ? + ": the previous boot's last S2R mark" : ""); + } + sleep->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); + sleep->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); + sleep->a2d_mini = t6040_ioremap_np(&pdev->dev, 0x508360000ULL, 0x1000); + sleep->a2d_mini4 = t6040_ioremap_np(&pdev->dev, 0x508364000ULL, 0x1000); + sleep->a2d_mm8 = t6040_ioremap_np(&pdev->dev, 0x508908000ULL, 0x1000); + sleep->a2d_pmc = t6040_ioremap_np(&pdev->dev, 0x5048aa000ULL, 0x1000); + sleep->a2d_blk = t6040_ioremap_np(&pdev->dev, 0x502850000ULL, 0x1000); + sleep->aop_req = t6040_ioremap_np(&pdev->dev, 0x5108b8000ULL, 0x1000); + sleep->a2d_mm12 = t6040_ioremap_np(&pdev->dev, 0x50890c000ULL, 0x1000); + sleep->mtp_ps = t6040_ioremap_np(&pdev->dev, T6040_MTP_PS_PA, 8 * T6040_MTP_PS_WORDS); + sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); + sleep->snap_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); + sleep->atc_aon = t6040_ioremap_np(&pdev->dev, 0x508320c00ULL, 0x200); + for (i = 0; i < 16; i++) + sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, + 0x2000); + for (i = 0; i < 4; i++) + sleep->amcc_sem[i] = t6040_ioremap_np(&pdev->dev, + 0x2201c0000ULL + i * 0x2000000ULL, 0x1000); + sleep->pg_main = t6040_ioremap_np(&pdev->dev, 0x50282c000ULL, 0x1000); + sleep->aop_asc = t6040_ioremap_np(&pdev->dev, 0x510600000ULL, 0x100); + for (i = 0; i < 4; i++) + sleep->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, + 0x383200000ULL + i * 0x8000000ULL, 0x100); + sleep->pg_east = t6040_ioremap_np(&pdev->dev, 0x5022a0000ULL, 0x1000); + sleep->mini_ps = t6040_ioremap_np(&pdev->dev, 0x508300000ULL, 0x200); + sleep->snap_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); + for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) + sleep->pmc_probe_io[i] = t6040_ioremap_np(&pdev->dev, t6040_pmc_probe_pa[i], 4); + for (i = 0; i < 3; i++) { + sleep->cpm_cfg[i] = t6040_ioremap_np(&pdev->dev, T6040_CPM_CFG_PA(i), CPM_CFG_STATE + 8); + sleep->cpm_impl[i] = t6040_ioremap_np(&pdev->dev, T6040_CPM_IMPL_PA(i), 8); + } + sleep->cpu4_impl110 = t6040_ioremap_np(&pdev->dev, T6040_CPU4_IMPL110_PA, 8); + sleep->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); + sleep->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); + sleep->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); + for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) + sleep->axi2af_bridge[i] = t6040_ioremap_np(&pdev->dev, + t6040_axi2af[i].bridge_pa, 0x200); + t6040_sleep_map_apcie(sleep); + t6040_sleep_map_mailbox(sleep); + t6040_sleep_watch_init(sleep, T6040_WATCH_SEP, "apple,sep", "sep"); + t6040_sleep_watch_init(sleep, T6040_WATCH_ANS, "apple,t6041-nvme-ans2", "ans"); + /* Fixed PAs: dart-dcp's counters and the internal display's retention pair. */ + sleep->dcp_dart = t6040_ioremap_np(&pdev->dev, T6040_DCP_DART_PA, 0x1000); + sleep->disp_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISP_PWRGATE_PA, 0x100); + sleep->pmc_dcp = t6040_ioremap_np(&pdev->dev, T6040_PMC_DCP_PA, 12); + sleep->pmc_dvmr = t6040_ioremap_np(&pdev->dev, T6040_PMC_DVMR_PA, 0x40); + sleep->pmc_bwr = t6040_ioremap_np(&pdev->dev, T6040_PMC_BWR_PA, 0x10); + sleep->dispext_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISPEXT_PWRGATE_PA, 0x1000); + for (i = 0; i < ARRAY_SIZE(t6040_dcp_asc_ctl_pa); i++) + sleep->dcp_asc_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_dcp_asc_ctl_pa[i], 4); + sleep->ptd = t6040_ioremap_np(&pdev->dev, T6040_PTD_PA, 0x11000); + /* The DART clock-protection (GAPF) block, read-only here. */ + sleep->gapf = t6040_ioremap_np(&pdev->dev, 0x500578000ULL, 0x1000); + sleep->ka_soc = t6040_ioremap_np(&pdev->dev, T6040_KA_SOC_PA, KA_SIZE); + sleep->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); + sleep->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); + sleep->agx_gate = t6040_ioremap_np(&pdev->dev, T6040_AGX_GATE_PA, 4); + sleep->ane_sys_ps = t6040_ioremap_np(&pdev->dev, T6040_ANE_SYS_PS_PA, 4); + sleep->ane_ps = t6040_ioremap_np(&pdev->dev, T6040_ANE_PS_PA, 0x20); + for (i = 0; i < ARRAY_SIZE(t6040_acc_apsc_pa); i++) + sleep->acc_apsc[i] = t6040_ioremap_np(&pdev->dev, t6040_acc_apsc_pa[i], 8); + for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { + sleep->tvm_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].ctl, 4); + sleep->tvm_status[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].status, 4); + } + sleep->pmc_ap_floor = t6040_ioremap_np(&pdev->dev, T6040_PMC_AP_FLOOR_PA, 8); + sleep->afr_ps = t6040_ioremap_np(&pdev->dev, T6040_AFR_PS_PA, 4); + if (sleep->ka_soc && sleep->ka_east) { + u32 soc = 0, east = 0; + + for (i = 0; i < KA_WORDS; i++) { + soc |= (readl(sleep->ka_soc + i * KA_STRIDE) & 1) << i; + east |= (readl(sleep->ka_east + i * KA_STRIDE) & 1) << i; + } + dev_info(&pdev->dev, "keep-alive bits at probe: SOC %#010x, SOC-East %#010x\n", + soc, east); + } + t6040_sleep_map_gp(sleep); + t6040_ed_map(&pdev->dev); + + platform_set_drvdata(pdev, sleep); + t6040_sleep = sleep; + + sleep->debugfs = debugfs_create_dir("t6040-sleep", NULL); + debugfs_create_file("preflight", 0400, sleep->debugfs, sleep, &t6040_sleep_preflight_fops); + debugfs_create_file("ps_write", 0200, sleep->debugfs, sleep, &t6040_ps_write_fops); + + /* + * The CPU power ops act only when a commit stops the parked secondaries. + * A real entry needs allow_entry; a dry run registers them at its .begin, + * for the hold. Until then kexec keeps refusing parking CPUs as before. + * Parking itself needs no power ops. + */ + if (allow_entry) + t6040_sleep_power_ops(sleep); + + register_syscore(&t6040_sleep_syscore); + suspend_set_ops(&t6040_sleep_ops); + atomic_notifier_chain_register(&panic_notifier_list, &t6040_panic_nb); + dev_info(&pdev->dev, + "refusal-first: %u display groups, cpu-start %s, wdt %s, commit/wake blocks %s, storage-apcie %s, s2r-mailbox %s, parking ops %s, dry run %u\n", + sleep->ngroups, sleep->cpu_start ? "mapped" : "absent", + sleep->wdt ? "mapped" : "absent", + (sleep->cluster_ctl && sleep->pwrgate && sleep->mastermini && sleep->spmi0 && + sleep->soc_east && sleep->aop_global) ? "mapped" : "incomplete", + sleep->apcie_st.valid ? "mapped" : "absent", + sleep->mbox ? "present" : "absent", + sleep->power_ops ? "registered" : "not registered", READ_ONCE(dry_run)); + t6040_sleep_log_s2r_state(sleep, "probe"); + t6040_log_ane(sleep, "probe"); + t6040_log_a2d(sleep, "probe"); + + return 0; +} + +static void t6040_sleep_remove(struct platform_device *pdev) +{ + struct t6040_sleep *sleep = platform_get_drvdata(pdev); + + unregister_syscore(&t6040_sleep_syscore); + debugfs_remove_recursive(sleep->debugfs); +} + +static const struct of_device_id t6040_sleep_of_match[] = { + { .compatible = "apple,t6040-sleep" }, + {} +}; +MODULE_DEVICE_TABLE(of, t6040_sleep_of_match); + +static struct platform_driver t6040_sleep_driver = { + .driver = { + .name = "t6040-sleep", + .of_match_table = t6040_sleep_of_match, + .pm = pm_sleep_ptr(&t6040_sleep_pm_ops), + /* The suspend ops can't be taken back. */ + .suppress_bind_attrs = true, + }, + .probe = t6040_sleep_probe, + .remove = t6040_sleep_remove, +}; +module_platform_driver(t6040_sleep_driver); + +MODULE_DESCRIPTION("Apple T6040 system sleep (refusal-first controller)"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 07e03087cbc3d..418ef9584f1d5 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -9,6 +9,7 @@ #include #include #include +#include #include struct device; @@ -171,6 +172,22 @@ void apple_sleep_restore_pins(void); */ int apple_sleep_engine_reset(struct device *dev); #else +#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +/* + * The T6040 controller (t6040-sleep.c) keeps the coprocessor and enclave registry above, + * with the same contract. The rest of the T8140 calls have no T6040 counterpart yet. + */ +int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, + int (*last_stop)(void *data), void *data); +int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), + void *data); +void apple_sleep_coproc_unregister(struct device *dev); +int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)); +int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), void *data); +void apple_sleep_enclave_unregister(struct device *dev); +void apple_sleep_coproc_set_ready(struct device *dev, bool ready); +bool apple_sleep_coproc_stopped(struct device *dev); +#else static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, int (*last_stop)(void *data), void *data) @@ -184,11 +201,6 @@ static inline int apple_sleep_coproc_register_first(struct device *dev, return -EOPNOTSUPP; } -static inline bool apple_sleep_request_deep_once(void) -{ - return false; -} - static inline void apple_sleep_coproc_unregister(struct device *dev) { } @@ -217,12 +229,19 @@ static inline bool apple_sleep_coproc_stopped(struct device *dev) { return false; } +#endif static inline bool apple_sleep_commit_stops_coprocs(void) { return false; } +/* The T6040 controller can't send a sleep deep either: false, as with no sleep driver. */ +static inline bool apple_sleep_request_deep_once(void) +{ + return false; +} + static inline void apple_sleep_usb_set_asleep(bool asleep) { } @@ -319,4 +338,36 @@ static inline int apple_sleep_engine_reset(struct device *dev) } #endif +/* + * Whether this firmware-driven suspend may end in a real power loss of the SoC, so that a + * driver may stop what only a power cycle brings back. A T6040 dry run sets + * pm_suspend_via_firmware() to run every sleep step that can still be taken back and then + * refuses before the commit: there this is false, and such a stop has to be left out. + * Everywhere else it is pm_suspend_via_firmware(). + * + * apple_sleep_last_steps(): whether the last steps that can still be taken back, but only + * just (the SMC's sleep commit, each coprocessor's last check), run too. A real sleep and + * the deepest dry run take them. + */ +#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +bool apple_sleep_power_loss(void); +bool apple_sleep_last_steps(void); +/* S2R bring-up: a progress mark that survives a reset (t6040-sleep, between .begin and .end). */ +void apple_sleep_bringup_mark(u32 v); +#else +static inline void apple_sleep_bringup_mark(u32 v) +{ +} + +static inline bool apple_sleep_power_loss(void) +{ + return pm_suspend_via_firmware(); +} + +static inline bool apple_sleep_last_steps(void) +{ + return pm_suspend_via_firmware(); +} +#endif + #endif diff --git a/rust/helpers/suspend.c b/rust/helpers/suspend.c index 5f92364a92a2d..9012a7f730569 100644 --- a/rust/helpers/suspend.c +++ b/rust/helpers/suspend.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include __rust_helper bool rust_helper_pm_suspend_via_firmware(void) @@ -17,3 +18,9 @@ __rust_helper bool rust_helper_pm_hibernate_is_restored(void) { return pm_hibernate_is_restored(); } + +/* An inline on most configurations, so Rust reaches it here (linux/soc/apple/sleep.h). */ +__rust_helper bool rust_helper_apple_sleep_power_loss(void) +{ + return apple_sleep_power_loss(); +} From ddb3ce1286e1663e695ec79f5e736fde1f6fe988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:03 +0100 Subject: [PATCH 052/269] soc: apple, tools: asahi: PMP, AOP, SEP, store and DockChannel across hibernation and S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/pm_shim.c, drivers/soc/apple/shim.h, tools/asahi/sep-suspend-errors-tests.py part of "soc: apple: PMP, AOP, SEP, store and DockChannel across hibernation and S2R", which goes with the sleep controller changes; the rest of that commit is with the coprocessor changes. Restore or re-adopt the platform coprocessors after a power loss: PMP report export, AOP with its firmware-driven sleep (run bit taken over cold when halted), the SEP and its enclave store, DockChannel. Signed-off-by: František Balázsy --- drivers/soc/apple/pm_shim.c | 179 ++++++++++++++++++++++-- drivers/soc/apple/shim.h | 10 +- tools/asahi/sep-suspend-errors-tests.py | 6 +- 3 files changed, 178 insertions(+), 17 deletions(-) diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index c1d415c0bd744..689217e8d94cc 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -1,12 +1,13 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* - * System sleep for the SEP driver: the notifier only forwards "about to sleep" - * and "back from sleep" to Rust, which ends any Touch ID capture before the - * system suspends (see pm.rs), and the enclave's power domains are kept on - * across sleep. The device's suspend and resume callbacks are the firmware - * suspend's: the enclave is told, and gates its own domain, whose state word is - * read here. On T8140 at EL2 the sleep driver sends the request at the - * irreversible commit boundary. Only errors before send may refuse. + * System sleep for the SEP driver: the notifier forwards the sleep events to + * Rust, which ends any Touch ID capture before the system suspends (see + * pm.rs); after a resume from disk on a cold boot it probes the device again; + * and the enclave's power domains are kept on across sleep. The device's + * suspend and resume callbacks are the firmware suspend's: the enclave is + * told, and gates its own domain, whose state word is read here. On T8140 at + * EL2 the sleep driver sends the request at the irreversible commit boundary. + * Only errors before send may refuse. */ #include #include @@ -25,8 +26,11 @@ #include #include #include +#include #include +#include #include +#include #include @@ -42,34 +46,175 @@ module_param(sleep_refuse_before_send, bool, 0644); MODULE_PARM_DESC(sleep_refuse_before_send, "Test only: refuse the next prepared deep sleep before sending SEP SLEEP"); -static void (*sep_pm_event)(bool entering); +static int (*sep_pm_event)(int event); +static struct device *sep_pm_dev; static int (*sep_pm_sleep)(void); static void (*sep_pm_wake)(void); static bool (*sep_pm_told)(void); /* In the device's .suspend, the only caller that can leave the request to the sleep. */ static bool sep_pm_suspending; +/* + * Whether the boot loader restarted the SoC under this hibernation. iBoot + * boots sepOS anew on every boot, and the SEP has no run bit the AP can read, + * so the PMP's restore (priority 10, ahead of this notifier) is asked: it + * counts the resumes from disk that found the PMP halted. The count is taken + * at registration and again at each hibernation's prepare and end, so a + * prepare this notifier missed (registered during it) still compares with + * the last count. Negative: no bound PMP, cold or warm cannot be told. + */ +static int sep_pm_cold_base = -ENODEV; + +/* The marker dt.rs leaves on the SEP node once the registration is sent. */ +#define SEP_REGISTERED_PROP "apple,sep-shmem-registered-iova" + +/* + * A resume from disk on a cold boot: the enclave runs a freshly booted sepOS + * that has never seen this kernel's registration, so the restored session is + * dead. The device is probed again from a work item, as at boot: probe sends + * a new registration (the marker is gone with the session it guarded), opens + * the xART store and loads the host state from disk again (the boot that + * resumed this image may have changed both), and recreates /dev/sep-bio. + */ +static struct device *sep_reprobe_dev; +static DEFINE_SPINLOCK(sep_reprobe_lock); + +static void sep_reprobe_fn(struct work_struct *work) +{ + struct device *dev; + int ret; + + spin_lock(&sep_reprobe_lock); + dev = sep_reprobe_dev; + sep_reprobe_dev = NULL; + spin_unlock(&sep_reprobe_lock); + if (!dev) + return; + + dev_info(dev, "resume from disk: attaching the restarted enclave again\n"); + ret = device_reprobe(dev); + /* A failed probe is not reported by device_reprobe() itself. */ + if (ret || !READ_ONCE(dev->driver)) + dev_err(dev, "resume from disk: probing the SEP again failed (%d); Touch ID stays down until a reboot\n", + ret); + put_device(dev); +} + +static DECLARE_WORK(sep_reprobe_work, sep_reprobe_fn); + +static void sep_pm_schedule_reprobe(struct device *dev) +{ + struct device_node *np = dev->of_node; + struct property *prop; + + /* + * Only here, with the cold boot established: removing the marker on a + * live enclave would let a second registration park it. + */ + prop = np ? of_find_property(np, SEP_REGISTERED_PROP, NULL) : NULL; + if (prop && of_remove_property(np, prop)) { + dev_err(dev, "resume from disk: the registration marker could not be removed; Touch ID stays down until a reboot\n"); + return; + } + + spin_lock(&sep_reprobe_lock); + if (!sep_reprobe_dev) { + sep_reprobe_dev = get_device(dev); + queue_work(system_dfl_wq, &sep_reprobe_work); + } + spin_unlock(&sep_reprobe_lock); +} + +/* The module goes away: no probe may run from the work item after it. */ +void sep_pm_exit(void) +{ + struct device *dev; + + cancel_work_sync(&sep_reprobe_work); + spin_lock(&sep_reprobe_lock); + dev = sep_reprobe_dev; + sep_reprobe_dev = NULL; + spin_unlock(&sep_reprobe_lock); + if (dev) + put_device(dev); +} + static int sep_pm_notify(struct notifier_block *nb, unsigned long action, void *data) { - void (*event)(bool entering) = READ_ONCE(sep_pm_event); + int (*event)(int event) = READ_ONCE(sep_pm_event); + struct device *dev = READ_ONCE(sep_pm_dev); + int base, now; if (!event) return NOTIFY_DONE; switch (action) { case PM_SUSPEND_PREPARE: + sep_diag_sleep(true); + event(SEP_PM_SLEEP); + return NOTIFY_OK; + case PM_POST_SUSPEND: + event(SEP_PM_WAKE); + sep_diag_sleep(false); + return NOTIFY_OK; case PM_HIBERNATION_PREPARE: + sep_diag_sleep(true); + sep_pm_cold_base = apple_pmp_cold_restarts(); + event(SEP_PM_HIBERNATE); + return NOTIFY_OK; case PM_RESTORE_PREPARE: sep_diag_sleep(true); - event(true); + /* + * This kernel is about to load an image. If it registered with the + * enclave, the enclave keeps that session and may DMA into memory + * the image overwrites, and the restored kernel would register a + * second time: refuse the restore (this boot goes on instead). + */ + if (event(SEP_PM_RESTORE) > 0) { + pr_err("apple_sep: this kernel attached the SEP; refusing to restore a hibernation image over it\n"); + return NOTIFY_BAD; + } return NOTIFY_OK; - case PM_POST_SUSPEND: - case PM_POST_HIBERNATION: case PM_POST_RESTORE: - event(false); + event(SEP_PM_HIBERNATE_WARM); sep_diag_sleep(false); return NOTIFY_OK; + case PM_POST_HIBERNATION: + sep_diag_sleep(false); + /* + * Ends an aborted hibernation, the wake of a hybrid sleep and a + * resume from the image alike. Only a resume from the image can + * have gone through a boot, and only the PMP knows whether it did + * (a test resume does not): a hybrid sleep's wake is never taken + * for a cold boot, even when the PMP did not come back from its + * suspend-to-RAM and the PMP's restore finds it halted. + */ + base = sep_pm_cold_base; + now = apple_pmp_cold_restarts(); + sep_pm_cold_base = now; + if (!pm_hibernate_is_restored()) { + event(SEP_PM_HIBERNATE_WARM); + return NOTIFY_OK; + } + if (base < 0 || now < 0) { + /* + * Cold or warm cannot be told. Sending to a freshly booted + * enclave, or registering again with a live one, are both + * worse than losing Touch ID until a reboot. + */ + if (dev) + dev_err(dev, "hibernation: no PMP count, cannot tell a cold boot; the SEP session is dropped until a reboot\n"); + event(SEP_PM_HIBERNATE_COLD); + return NOTIFY_OK; + } + if (now != base) { + if (event(SEP_PM_HIBERNATE_COLD) > 0 && dev) + sep_pm_schedule_reprobe(dev); + return NOTIFY_OK; + } + event(SEP_PM_HIBERNATE_WARM); + return NOTIFY_OK; default: return NOTIFY_DONE; } @@ -77,6 +222,8 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, static struct notifier_block sep_pm_nb = { .notifier_call = sep_pm_notify, + /* Below the PMP's 10, so that its restore has counted a cold boot. */ + .priority = 0, }; /* The enclave's own domain is the first one its node lists. */ @@ -474,21 +621,24 @@ const struct dev_pm_ops sep_pm_ops = { .suspend_noirq = pm_sleep_ptr(sep_pm_suspend_noirq), }; -int sep_pm_register(void *ptr, void (*event)(bool entering), int (*sleep)(void), +int sep_pm_register(void *ptr, int (*event)(int event), int (*sleep)(void), void (*wake)(void), bool (*told)(void)) { int ret; + WRITE_ONCE(sep_pm_dev, (struct device *)ptr); WRITE_ONCE(sep_pm_event, event); WRITE_ONCE(sep_pm_sleep, sleep); WRITE_ONCE(sep_pm_wake, wake); WRITE_ONCE(sep_pm_told, told); + sep_pm_cold_base = apple_pmp_cold_restarts(); ret = register_pm_notifier(&sep_pm_nb); if (ret) { WRITE_ONCE(sep_pm_event, NULL); WRITE_ONCE(sep_pm_sleep, NULL); WRITE_ONCE(sep_pm_wake, NULL); WRITE_ONCE(sep_pm_told, NULL); + WRITE_ONCE(sep_pm_dev, NULL); return ret; } @@ -503,6 +653,7 @@ void sep_pm_unregister(void) WRITE_ONCE(sep_pm_sleep, NULL); WRITE_ONCE(sep_pm_wake, NULL); WRITE_ONCE(sep_pm_told, NULL); + WRITE_ONCE(sep_pm_dev, NULL); } static void sep_pm_release_domains(void *data) diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 938e9b9a529a7..279bb46956603 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -26,9 +26,17 @@ void sep_cancel_delayed_work_sync(void *work); /* -- pm_shim.c ---------------------------------------------------------- */ -int sep_pm_register(void *dev, void (*event)(bool entering), int (*sleep)(void), +/* Sleep events for sep_pm_register()'s callback; mirrored in pm.rs. */ +#define SEP_PM_SLEEP 0 +#define SEP_PM_WAKE 1 +#define SEP_PM_HIBERNATE 2 +#define SEP_PM_HIBERNATE_WARM 3 +#define SEP_PM_HIBERNATE_COLD 4 +#define SEP_PM_RESTORE 5 +int sep_pm_register(void *dev, int (*event)(int event), int (*sleep)(void), void (*wake)(void), bool (*told)(void)); void sep_pm_unregister(void); +void sep_pm_exit(void); int sep_pm_keep_domains(void *dev); int sep_pm_wait_gated(void *dev, unsigned int timeout_ms); int sep_pm_clear_dev_disable(void *dev); diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index 2ae69ec2c32fc..f05054301e4b1 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -446,7 +446,7 @@ def replace_once(source, before, after): #![allow(dead_code, unused_imports, unused_variables)] use std::cell::{Cell, RefCell, RefMut}; use std::ffi::{c_int, c_void}; -use std::sync::atomic::{AtomicU32, Ordering, Ordering::Relaxed}; +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering, Ordering::Relaxed}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct Error(i32); impl Error { fn from_errno(n: i32) -> Self { assert!(n < 0); Self(n) } } type Result = std::result::Result; @@ -529,13 +529,15 @@ def replace_once(source, before, after): struct SepData { dev: kernel::device::Device, phase: AtomicU32, control: Lock, mbox: Lock>, gate: Cell, sleep_rx_held: Flag, endpoint: Cell, control_wq: Wq, + session_gone: AtomicBool, } impl SepData { fn new() -> Self { Self { dev: kernel::device::Device, phase: AtomicU32::new(PHASE_READY), control: Lock(RefCell::new(Control { busy: false, wake_ok: true })), mbox: Lock(RefCell::new(Some(Mbox))), gate: Cell::new(pm::SleepGate::Open), - sleep_rx_held: Flag(Cell::new(false)), endpoint: Cell::new(true), control_wq: Wq } + sleep_rx_held: Flag(Cell::new(false)), endpoint: Cell::new(true), control_wq: Wq, + session_gone: AtomicBool::new(false) } } fn endpoint_present(&self, _: u32) -> bool { self.endpoint.get() } fn sleep_gate(&self) -> pm::SleepGate { self.gate.get() } From 042a370d67991cb4b18328a404c0de22d9e738af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:03 +0100 Subject: [PATCH 053/269] irqchip, pmdomain, spmi, rtc, pinctrl, mfd, cpufreq: T6040 state across S4 and S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AIC3 save/restore, power-state and PMP report domains, the SPMI controller's interrupt masks, the PMU RTC, pinctrl, macsmc and cpufreq across hibernation and suspend-to-RAM on T6040. Signed-off-by: František Balázsy --- drivers/cpufreq/apple-soc-cpufreq.c | 8 + drivers/irqchip/irq-apple-aic.c | 57 ++++++- drivers/mfd/macsmc-test.c | 41 ++--- drivers/mfd/macsmc.c | 209 ++++++++++++++++++++++++- drivers/pinctrl/pinctrl-apple-gpio.c | 86 ++++++++-- drivers/pmdomain/apple/pmgr-pwrstate.c | 54 ++++++- drivers/pmdomain/apple/pmp-report.c | 97 +++++++++++- drivers/power/reset/macsmc-reboot.c | 32 +++- drivers/rtc/rtc-apple-pmu.c | 28 +++- drivers/spmi/spmi-apple-controller.c | 107 ++++++++++++- tools/asahi/pmu-rtc-tests.py | 10 ++ tools/asahi/t6030-pmp-graph-tests.py | 2 + 12 files changed, 679 insertions(+), 52 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 379fa031ce599..c989aa9eb3f97 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -1059,6 +1059,14 @@ static void apple_fast_die_sample(struct apple_fast_die *fd) fd->waiting = false; } else { limit = fd->fallback; + /* + * The PMP stopped after it had run (a resume from disk restarts it): + * the wait, and its warning, start over from now. + */ + if (!fd->waiting && fd->report) { + fd->start = jiffies; + fd->warned = false; + } fd->waiting = true; if (!fd->warned && time_after(jiffies, fd->start + APPLE_FAST_DIE_WARN_S * HZ)) { diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index 57a5f868a3606..6cccf4aaf1159 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -1165,7 +1165,7 @@ static int aic_syscore_suspend(void *data) struct aic_irq_chip *ic = aic_irqc; u32 die, i, off = 0; - if (!pm_suspend_via_firmware()) + if (!aic_sleep.irq_cfg || !pm_suspend_via_firmware()) return 0; aic_sleep.config = aic_ic_read(ic, AIC2_CONFIG); @@ -1205,13 +1205,46 @@ static void aic_resume_boot_cpu(void) } } +/* + * After a resume from disk the controller holds what the boot kernel left: its + * init masked every IRQ and it unmasked only the ones it used itself. The IRQ + * core disables lazily and unmasks only an IRQ it recorded as masked, so on + * its own it never unmasks the restored kernel's other IRQs again. Make every + * hardware mask match the restored descriptor: unmasked unless the core + * recorded it masked (a lazily disabled IRQ was unmasked in hardware when the + * image was taken), and masked when this kernel has no mapping for it. On the + * thaw path the masks already match and this rewrites the same values. + */ +static void aic_resync_masks(struct aic_irq_chip *ic) +{ + unsigned int die, i; + + for (die = 0; die < ic->nr_die; die++) { + for (i = 0; i < ic->nr_irq; i++) { + irq_hw_number_t hwirq = AIC_IRQ_HWIRQ(die, i); + unsigned int virq = irq_find_mapping(ic->hw_domain, hwirq); + struct irq_data *d = virq ? irq_get_irq_data(virq) : NULL; + u32 off = die * ic->info.die_stride; + + if (d && !irqd_irq_masked(d)) + aic_ic_write(ic, ic->info.mask_clr + off + MASK_REG(i), MASK_BIT(i)); + else + aic_ic_write(ic, ic->info.mask_set + off + MASK_REG(i), MASK_BIT(i)); + } + } +} + static void aic_syscore_resume(void *data) { struct aic_irq_chip *ic = aic_irqc; u32 die, i, off = 0; - if (!aic_sleep.saved) + if (!aic_sleep.saved) { + /* No firmware-driven suspend: the only other syscore resume that loses masks. */ + if (IS_ENABLED(CONFIG_HIBERNATION)) + aic_resync_masks(ic); return; + } aic_sleep.saved = false; /* Otherwise the suspend was refused, and the controller stayed powered. */ @@ -1254,19 +1287,29 @@ static struct syscore aic_syscore = { .ops = &aic_syscore_ops, }; -static void __init aic_sleep_init(struct aic_irq_chip *irqc) +static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) { - if (!irqc->info.fw_suspend) + /* + * The T6040's AIC (apple,t6041-aic3, matched through aic3_info) is the + * same AICv3 as the T8140's: the same CONFIG, TIMEOUT and CLUSTER_PRIO + * words and IRQ_CFG array. Its suspend-to-RAM path is the separate + * T6040 sleep controller, so it is admitted here only with that. + */ + if (!irqc->info.fw_suspend && + !(IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) && + of_device_is_compatible(node, "apple,t6041-aic3"))) return; aic_sleep.irq_cfg = kcalloc(irqc->nr_die * irqc->nr_irq, sizeof(u32), GFP_KERNEL); if (WARN_ON(!aic_sleep.irq_cfg)) return; - register_syscore(&aic_syscore); + /* A resume from disk needs the mask resync on every SoC. */ + if (aic_sleep.irq_cfg || IS_ENABLED(CONFIG_HIBERNATION)) + register_syscore(&aic_syscore); } #else -static void __init aic_sleep_init(struct aic_irq_chip *irqc) +static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) { } #endif @@ -1424,7 +1467,7 @@ static int __init aic_of_ic_init(struct device_node *node, struct device_node *p "irqchip/apple-aic/ipi:starting", aic_init_cpu, NULL); - aic_sleep_init(irqc); + aic_sleep_init(irqc, node); if (is_kernel_in_hyp_mode()) { struct irq_fwspec mi = { diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index a2dc3531ac36f..f7bb195e00b9f 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -10,7 +10,7 @@ struct macsmc_test_context { unsigned int calls; bool require_sram_lock; /* What the sleep tests saw, in order: keys read, values written, notices. */ - u32 seen[8]; + u32 seen[12]; unsigned int seen_count; int ap_power_result; u64 record; @@ -26,9 +26,11 @@ static int macsmc_test_command(struct apple_smc *smc, u64 cmd, u64 arg, KUNIT_EXPECT_TRUE(kunit_get_current_test(), mutex_is_locked(&smc->mutex)); ctx->calls++; + /* A written value as wide as it was written: WKTP's is one byte. */ if (ctx->seen_count < ARRAY_SIZE(ctx->seen)) - ctx->seen[ctx->seen_count++] = cmd == SMC_MSG_WRITE_KEY ? - get_unaligned((u32 *)ctx->shmem) : arg; + ctx->seen[ctx->seen_count++] = cmd != SMC_MSG_WRITE_KEY ? arg : + get_unaligned((u32 *)ctx->shmem) & + GENMASK(8 * min_t(u64, size, sizeof(u32)) - 1, 0); if (ret_data && ctx->result >= 0) *ret_data = ctx->data; return ctx->result; @@ -236,15 +238,16 @@ static void macsmc_sleep_order_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); - /* As in the trace, values included. */ - KUNIT_ASSERT_EQ(test, ctx->seen_count, 7U); + /* As in the trace, values included: WKTP's 2 comes before 'slpa'. */ + KUNIT_ASSERT_EQ(test, ctx->seen_count, 8U); KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); - KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_IDLE); - KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_ON); - KUNIT_EXPECT_EQ(test, ctx->seen[4], SMC_KEY(MBSE)); - KUNIT_EXPECT_EQ(test, ctx->seen[5], SMC_KEY(MBSW)); - KUNIT_EXPECT_EQ(test, ctx->seen[6], SMC_KEY(waka)); + KUNIT_EXPECT_EQ(test, ctx->seen[1], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[2], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[4], (u32)MACSMC_TEST_AP_ON); + KUNIT_EXPECT_EQ(test, ctx->seen[5], SMC_KEY(MBSE)); + KUNIT_EXPECT_EQ(test, ctx->seen[6], SMC_KEY(MBSW)); + KUNIT_EXPECT_EQ(test, ctx->seen[7], SMC_KEY(waka)); } static void macsmc_test_outbox_poll(struct apple_smc *smc, const char *after, @@ -265,10 +268,11 @@ static void macsmc_sleep_warn_late_test(struct kunit *test) KUNIT_EXPECT_EQ(test, apple_smc_sleep_enter(&ctx->smc), 0); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); KUNIT_EXPECT_TRUE(test, ctx->smc.sleep_warned); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 3U); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); - KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[1], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[2], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_IDLE); } static void macsmc_sleep_refused_test(struct kunit *test) @@ -282,10 +286,11 @@ static void macsmc_sleep_refused_test(struct kunit *test) /* No answer: refuse, undo the notice and leave commands working. */ KUNIT_EXPECT_EQ(test, apple_smc_sleep_commit(&ctx->smc), -ETIME); KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 3U); - KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[1], (u32)MACSMC_TEST_AP_IDLE); - KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_ON); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); + KUNIT_EXPECT_EQ(test, ctx->seen[0], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_ON); } static void macsmc_notice_test(struct kunit *test) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 04e90a97cc313..59b0793f2118f 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -571,6 +572,10 @@ static const struct apple_smc_wake_reason apple_smc_t8132_wake_reasons[] = { { 40, "acattach" }, { 41, "acdetach" }, { 42, "USB-C_plug" }, { 43, "USB2_plug" }, { 44, "USB2_wake" }, { 45, "aupo" }, { 48, "rtc" }, { } }; +#ifdef CONFIG_HIBERNATION +static struct notifier_block apple_smc_pm_nb; +static void apple_smc_pm_unregister(void *unused); +#endif static int apple_smc_probe(struct platform_device *pdev) { @@ -586,6 +591,16 @@ static int apple_smc_probe(struct platform_device *pdev) smc->wake_reasons = of_device_is_compatible(dev->of_node, "apple,t8132-smc") ? apple_smc_t8132_wake_reasons : apple_smc_t8140_wake_reasons; +#ifdef CONFIG_HIBERNATION + /* First, so that a late probe still sees a resume start. */ + ret = register_pm_notifier(&apple_smc_pm_nb); + if (ret) + return ret; + ret = devm_add_action_or_reset(dev, apple_smc_pm_unregister, NULL); + if (ret) + return ret; +#endif + mutex_init(&smc->mutex); spin_lock_init(&smc->lock); smc->dev = &pdev->dev; @@ -678,6 +693,175 @@ static int apple_smc_probe(struct platform_device *pdev) ARRAY_SIZE(apple_smc_t6050_described_devs)); } +#ifdef CONFIG_HIBERNATION +/* + * The kernel that loads a hibernation image binds this driver like any boot + * and so holds a live RTKit session with the SMC when it jumps into the image. + * The restored kernel then asks the firmware to start again (IOP power INIT) + * and waits for a HELLO that a firmware already in a session never sends. So + * the loading kernel hands the SMC over quiesced, the state the upstream + * teardown leaves it in and from which a wake starts a new session. + */ +static bool apple_smc_loading_image; +static bool apple_smc_handed_over; + +static int apple_smc_pm_notify(struct notifier_block *nb, unsigned long action, void *unused) +{ + if (action == PM_RESTORE_PREPARE) + apple_smc_loading_image = true; + else if (action == PM_POST_RESTORE) + apple_smc_loading_image = false; + return NOTIFY_DONE; +} + +static struct notifier_block apple_smc_pm_nb = { + .notifier_call = apple_smc_pm_notify, +}; + +static int apple_smc_restore(struct device *dev); + +static void apple_smc_pm_unregister(void *unused) +{ + unregister_pm_notifier(&apple_smc_pm_nb); +} + +static int apple_smc_freeze(struct device *dev) +{ + struct apple_smc *smc = dev_get_drvdata(dev); + + /* Only the image-loading kernel, right before it enters the image. */ + if (apple_smc_loading_image && smc && apple_rtkit_is_running(smc->rtk)) { + int ret; + + apple_smc_write_flag(smc, SMC_KEY(NTAP), false); + ret = apple_rtkit_quiesce(smc->rtk); + if (ret) { + /* + * Failing here would abandon the image, whose signature is + * already gone, and leave this kernel without a session: enter + * the image anyway, its restore tries a new session. + */ + dev_err(dev, "could not quiesce the SMC for the image: %d\n", ret); + return 0; + } + apple_smc_handed_over = true; + dev_info(dev, "SMC quiesced for the restored kernel\n"); + return 0; + } + + /* + * Nothing host-side needs to be saved or torn down for hibernation. + * The image is written with the SMC coprocessor left running, and the + * driver keeps no SMC state that has to survive inside the image: every + * live handle (the RTKit session in smc->rtk, the SRAM mapping and the + * shared-memory window) is tied to the firmware instance that the boot + * firmware cold-restarts on resume, so it is rebuilt from scratch in + * apple_smc_restore(). Leaving the firmware running here is also what + * keeps thermal/power management working while the image is written. + * + * .poweroff (invoked after the image has been written, just before the + * machine powers off) shares this callback for the same reasons. + */ + return 0; +} + +static int apple_smc_thaw(struct device *dev) +{ + /* The image could not be entered after the hand-over: start a new session. */ + if (apple_smc_handed_over) { + apple_smc_handed_over = false; + return apple_smc_restore(dev); + } + + /* + * .thaw is reached only when image creation was aborted or has just + * finished and the current, live session continues unchanged. Since + * .freeze tore nothing down, there is nothing to restore. + */ + return 0; +} + +static int apple_smc_restore(struct device *dev) +{ + struct apple_smc *smc = dev_get_drvdata(dev); + __be32 count = 0; + int ret; + + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return apple_smc_thaw(dev); + + /* + * The system has been through a full cold boot to load the hibernation + * image: the boot firmware restarted the SMC co-processor, so the RTKit + * session that our restored state still describes belongs to a firmware + * instance that no longer exists. Every SMC command would otherwise + * poll-time-out. + * + * Our RTKit power-state bookkeeping was restored from the pre-hibernate + * image, which marks the (now dead) coprocessor as running; in that + * state apple_rtkit_wake() refuses to run. Reinitialize the transport + * first to drop the stale session back to the held-in-reset state + * (power state OFF, endpoint map cleared, mailbox RX restarted), which + * matches the post-devm_apple_rtkit_init() state probe starts from, then + * replay probe's bring-up handshake against the freshly booted firmware. + */ + ret = apple_rtkit_reinit(smc->rtk); + if (ret) + return dev_err_probe(dev, ret, "Failed to reinit RTKit on restore"); + + smc->boot_stage = APPLE_SMC_BOOTING; + + ret = apple_rtkit_wake(smc->rtk); + if (ret) + return dev_err_probe(dev, ret, "Failed to wake up SMC on restore"); + + ret = apple_rtkit_start_ep(smc->rtk, SMC_ENDPOINT); + if (ret) + return dev_err_probe(dev, ret, "Failed to start SMC endpoint on restore"); + + reinit_completion(&smc->init_done); + reinit_completion(&smc->cmd_done); + + ret = apple_rtkit_send_message(smc->rtk, SMC_ENDPOINT, + FIELD_PREP(SMC_MSG, SMC_MSG_INITIALIZE), NULL, false); + if (ret) + return dev_err_probe(dev, ret, "Failed to send init message on restore"); + + if (wait_for_completion_timeout(&smc->init_done, msecs_to_jiffies(SMC_TIMEOUT_MS)) == 0) { + dev_err(dev, "Timed out initializing SMC on restore\n"); + return -ETIMEDOUT; + } + + if (smc->boot_stage != APPLE_SMC_INITIALIZED) { + dev_err(dev, "SMC failed to boot on restore, boot stage=%d\n", smc->boot_stage); + return -EIO; + } + + /* + * Re-read the key count: the firmware is a fresh instance and nothing + * guarantees the table is identical to the pre-hibernate one. + */ + ret = apple_smc_read(smc, SMC_KEY(#KEY), &count, sizeof(count)); + if (ret >= 0 && ret != sizeof(count)) { + count = 0; + ret = -EIO; + } + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to get key count on restore"); + smc->key_count = be32_to_cpu(count); + + /* + * Re-enable the firmware-side notification flag. The event_handlers + * notifier chain and the sub-device registrations on it survive in the + * restored image, so only the firmware flag has to be re-armed. + */ + apple_smc_write_flag(smc, SMC_KEY(NTAP), true); + + return 0; +} +#endif /* CONFIG_HIBERNATION */ + static const struct of_device_id apple_smc_of_match[] = { { .compatible = "apple,t8152-smc" }, { .compatible = "apple,t6050-smc" }, @@ -780,10 +964,25 @@ static int apple_smc_notice_idle(struct apple_smc *smc) return ret; } +/* + * WKTP, one byte for its power state 0. + * The platform writes it at the sleep request, before 'slpa' (measured on T6040: the WKTP write, + * then pmVC, then MBSE, then the AP idle notice). 0 leaves it unwritten. + */ +static unsigned int sleep_wktp = 2; +module_param(sleep_wktp, uint, 0644); +MODULE_PARM_DESC(sleep_wktp, "WKTP value written before 'slpa' on a firmware-driven suspend (0: none)"); + static int apple_smc_sleep_commit(struct apple_smc *smc) { + unsigned int wktp = READ_ONCE(sleep_wktp); int ret; + if (wktp) { + ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); + if (ret) + dev_err(smc->dev, "Failed to write WKTP = %u: %d\n", wktp, ret); + } apple_smc_write_mbse(smc, SMC_KEY(slpa)); /* Device interrupts are off here. */ mdelay(SMC_SLEEP_ARM_MS); @@ -981,7 +1180,8 @@ static int apple_smc_suspend_noirq(struct device *dev) struct apple_smc *smc = dev_get_drvdata(dev); int ret; - if (!smc->sleep_warned && !smc->sleep_warn_late) + /* A dry run short of its last steps warns the SMC, and .complete takes that back. */ + if ((!smc->sleep_warned && !smc->sleep_warn_late) || !apple_sleep_last_steps()) return 0; apple_smc_set_polled(smc, true); @@ -1004,9 +1204,16 @@ static int apple_smc_resume_noirq(struct device *dev) return 0; } +/* Suspend-to-RAM commits in the noirq phase; hibernation restarts the session in the normal one. */ static const struct dev_pm_ops apple_smc_pm_ops = { .prepare = pm_sleep_ptr(apple_smc_prepare), .complete = pm_sleep_ptr(apple_smc_complete), +#ifdef CONFIG_HIBERNATION + .freeze = apple_smc_freeze, + .thaw = apple_smc_thaw, + .poweroff = apple_smc_freeze, + .restore = apple_smc_restore, +#endif NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_smc_suspend_noirq, apple_smc_resume_noirq) }; diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 3e8ab5a9b933b..22eb998188c64 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -54,6 +54,8 @@ struct apple_gpio_pinctrl { /* The pins' words across a firmware-driven suspend */ u32 *saved; unsigned long *saved_valid; + /* The regmap caches the block: it holds what this driver set, not the hardware. */ + bool cached; /* Pins this driver has written. Others belong to someone else. */ unsigned long *touched; struct apple_gpio_irq_group irqgrps[]; @@ -83,6 +85,15 @@ struct apple_gpio_pinctrl { #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22) #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5)) +/* + * T6040 bring-up: put the configured pins back after a firmware-driven + * suspend also on a cached block, by reading and writing the hardware past + * the cache. Off restores only the uncached blocks, as before. + */ +static bool resume_cached = true; +module_param(resume_cached, bool, 0644); +MODULE_PARM_DESC(resume_cached, "Put configured pins back after S2R on a cached block too (bring-up)"); + static const struct regmap_config regmap_config = { .reg_bits = 32, .val_bits = 32, @@ -603,14 +614,19 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) return dev_err_probe(&pdev->dev, PTR_ERR(pctl->map), "Failed to create regmap\n"); - /* Only with the regmap reaching the hardware: a cache would hide the loss. */ - if (hw && hw->no_regcache) { - pctl->saved = devm_kcalloc(&pdev->dev, npins, sizeof(*pctl->saved), GFP_KERNEL); - pctl->saved_valid = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); - pctl->touched = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); - if (!pctl->saved || !pctl->saved_valid || !pctl->touched) - return -ENOMEM; - } + /* + * A cache would hide the loss: on a cached block the resume compares and + * writes the hardware past it. + */ + pctl->cached = !(hw && hw->no_regcache); + pctl->saved = devm_kcalloc(&pdev->dev, npins, sizeof(*pctl->saved), GFP_KERNEL); + pctl->saved_valid = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); + if (!pctl->saved || !pctl->saved_valid) + return -ENOMEM; + /* A resume from disk also puts pins back, from the cache where there is one. */ + pctl->touched = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); + if (!pctl->touched) + return -ENOMEM; for (i = 0; i < npins; i++) { pins[i].number = i; @@ -676,6 +692,8 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) if (!pctl->saved || pm_suspend_target_state != PM_SUSPEND_MEM || !pm_suspend_via_firmware()) return 0; + if (pctl->cached && !READ_ONCE(resume_cached)) + return 0; /* * Only the pins this driver configured. The others belong to the firmware or another @@ -697,22 +715,70 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) if (!pctl->saved || !pm_resume_via_firmware()) return 0; + /* The saved words came from the cache; the hardware is what to compare. */ + if (pctl->cached) + regcache_cache_bypass(pctl->map, true); for_each_set_bit(i, pctl->saved_valid, pctl->pinctrl_desc.npins) { if (!regmap_read(pctl->map, REG_GPIO(i), &val) && val == pctl->saved[i]) continue; regmap_write(pctl->map, REG_GPIO(i), pctl->saved[i]); restored++; } - dev_dbg(dev, "resume: %u of %u configured pins put back\n", restored, - bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); + if (pctl->cached) + regcache_cache_bypass(pctl->map, false); + if (restored) + dev_info(dev, "resume: %u of %u configured pins put back\n", restored, + bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); return 0; } +/* + * A resume from disk is a cold boot: the block holds what the boot loader and + * the boot kernel set up, not the restored kernel's pins, while the regmap + * cache still holds the restored kernel's words. An interrupt does not get + * its pin back by itself: the IRQ core disables lazily and unmasks only an + * IRQ it recorded as masked, and regmap_update_bits() skips a write the cache + * already has. So an edge interrupt armed before the hibernation, such as the + * HDMI hotplug detect, stays off for good. Write back the pins this driver set + * up as interrupts, from the cache; the other pins are left to their consumers. + * + * The early restore, as a PM domain does not wrap it (its restore_noirq ends + * in the driver's resume_noirq). An uncached block reads back only what the + * boot left, so this does nothing there. + */ +static int __maybe_unused apple_gpio_pinctrl_restore_early(struct device *dev) +{ + struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); + unsigned int i, mode, val, armed = 0; + + /* Hibernation aborted before the image: the block kept its state. */ + if (!pm_hibernate_is_restored()) + return 0; + /* Uncached: no word of the restored kernel to write back. */ + if (!pctl->cached) + return 0; + + for_each_set_bit(i, pctl->touched, pctl->pinctrl_desc.npins) { + if (regmap_read(pctl->map, REG_GPIO(i), &val)) + continue; + mode = FIELD_GET(REG_GPIOx_MODE, val); + if (mode < REG_GPIOx_IN_IRQ_HI || mode > REG_GPIOx_IN_IRQ_ANY) + continue; + regmap_write(pctl->map, REG_GPIO(i), val); + armed++; + } + if (armed) + dev_info(dev, "resume from disk: %u interrupt pin(s) set up again\n", armed); + + return 0; +} + static const struct dev_pm_ops apple_gpio_pinctrl_pm_ops = { NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_gpio_pinctrl_suspend_noirq, apple_gpio_pinctrl_resume_noirq) + .restore_early = pm_sleep_ptr(apple_gpio_pinctrl_restore_early), }; static struct platform_driver apple_gpio_pinctrl_driver = { diff --git a/drivers/pmdomain/apple/pmgr-pwrstate.c b/drivers/pmdomain/apple/pmgr-pwrstate.c index d6ac0c2cebd9f..d807884d29022 100644 --- a/drivers/pmdomain/apple/pmgr-pwrstate.c +++ b/drivers/pmdomain/apple/pmgr-pwrstate.c @@ -672,6 +672,48 @@ static int apple_pmgr_ps_resume_noirq(struct device *dev) return 0; } +#ifdef CONFIG_HIBERNATION +/* + * After a resume from disk genpd still records the domains as they were when + * the image was taken, but the cold boot behind the resume left the hardware + * in the boot loader's state, and genpd powers on only the domains it recorded + * as off. A domain recorded on but found gated would make its consumers' + * restore touch an unpowered block. Bring each such domain up, parents first. + */ +static void apple_pmgr_ps_restore_hw(struct apple_pmgr_ps *ps, unsigned int depth) +{ + struct gpd_link *link; + u32 reg; + + if (depth > 16) + return; + list_for_each_entry(link, &ps->genpd.child_links, child_node) + if (link->parent->power_on == apple_pmgr_ps_power_on) + apple_pmgr_ps_restore_hw(genpd_to_apple_pmgr_ps(link->parent), depth + 1); + + if (ps->firmware_owned || ps->inherited_on || ps->sleep_driver_owned || + ps->genpd.status != GENPD_STATE_ON) + return; + if (regmap_read(ps->regmap, ps->offset, ®)) + return; + if (FIELD_GET(APPLE_PMGR_PS_ACTUAL, reg) == APPLE_PMGR_PS_ACTIVE) + return; + dev_dbg(ps->dev, "PS %s: on in the image, gated after the cold boot (0x%x): powering on\n", + ps->genpd.name, reg); + apple_pmgr_ps_set(&ps->genpd, APPLE_PMGR_PS_ACTIVE, + !ps->no_auto_enable && !ps->no_auto_pm); +} + +static int apple_pmgr_ps_restore_noirq(struct device *dev) +{ + struct apple_pmgr_ps *ps = dev_get_drvdata(dev); + + if (ps) + apple_pmgr_ps_restore_hw(ps, 0); + return 0; +} +#endif + static int apple_pmgr_reset_assert(struct reset_controller_dev *rcdev, unsigned long id) { struct apple_pmgr_ps *ps = rcdev_to_apple_pmgr_ps(rcdev); @@ -1383,7 +1425,17 @@ MODULE_DEVICE_TABLE(of, apple_pmgr_ps_of_match); static const struct dev_pm_ops apple_pmgr_ps_pm_ops = { LATE_SYSTEM_SLEEP_PM_OPS(apple_pmgr_ps_suspend_late, apple_pmgr_ps_resume_early) - NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_pmgr_ps_suspend_noirq, apple_pmgr_ps_resume_noirq) + .suspend_noirq = pm_sleep_ptr(apple_pmgr_ps_suspend_noirq), + .freeze_noirq = pm_sleep_ptr(apple_pmgr_ps_suspend_noirq), + .poweroff_noirq = pm_sleep_ptr(apple_pmgr_ps_suspend_noirq), + /* Suspend-to-RAM resyncs after the sleep engine; a resume from disk after the cold boot. */ + .resume_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), + .thaw_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), +#ifdef CONFIG_HIBERNATION + .restore_noirq = apple_pmgr_ps_restore_noirq, +#else + .restore_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), +#endif }; static struct platform_driver apple_pmgr_ps_driver = { diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 7ce01a88bb069..055f6e42760a6 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -447,6 +447,10 @@ static void apple_pmp_report_ready_work(struct work_struct *work) unsigned int ms = rep->polls++ < PMP_REPORT_FAST_POLLS ? PMP_REPORT_POLL_MS : PMP_REPORT_POLL_SLOW_MS; + if (rep->polls == PMP_REPORT_FAST_POLLS + 1) + dev_warn(rep->dev, "PMP still stopped, requests %#llx held\n", + rep->shadow); + queue_delayed_work(system_freezable_power_efficient_wq, &rep->ready_work, msecs_to_jiffies(ms)); return; @@ -559,6 +563,90 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) } EXPORT_SYMBOL_GPL(apple_pmp_report_wake); +/* + * Hold requests again for a PMP that stopped behind this driver's back: on + * a resume from hibernation the boot loader has cold-restarted the PMP + * (halted, its report region reset when it starts) while the restored image + * still has requests handed. Requests made from now on stay in @shadow and + * the ready work hands all of them over once the PMP runs, as at probe. + * Returns whether the requests are held now. Called with @rep->lock held. + */ +static bool apple_pmp_report_hold_locked(struct apple_pmp_report *rep) +{ + if (rep->defer) + return true; + if (!rep->offsets->defer_requests || apple_pmp_report_running(rep)) + return false; + rep->defer = true; + rep->polls = 0; + queue_delayed_work(system_freezable_power_efficient_wq, &rep->ready_work, 0); + return true; +} + +static bool apple_pmp_report_handed(struct apple_pmp_report *rep) +{ + unsigned long flags; + bool handed; + u64 want; + + spin_lock_irqsave(&rep->lock, flags); + handed = !rep->defer; + want = rep->shadow; + spin_unlock_irqrestore(&rep->lock, flags); + return handed && (readq(rep->base + rep->offsets->actual) & want) == want; +} + +/** + * apple_pmp_report_wait_handed() - wait until every held request is acknowledged + * @timeout_ms: upper bound of the wait + * + * For a consumer that restarts its own firmware after a resume from + * hibernation and must not run it before the PMP has taken the power state + * requests made for it (the GPU's pmp-gfx request). Sleeps. + * + * Return: 0 once every report runs with its requests acknowledged, or + * -ETIMEDOUT. + */ +int apple_pmp_report_wait_handed(unsigned int timeout_ms) +{ + unsigned long deadline = jiffies + msecs_to_jiffies(timeout_ms); + struct apple_pmp_report *rep; + bool done; + + for (;;) { + done = true; + rcu_read_lock(); + list_for_each_entry_rcu(rep, &apple_pmp_reports, list) + done &= apple_pmp_report_handed(rep); + rcu_read_unlock(); + if (done) + return 0; + if (time_after(jiffies, deadline)) + return -ETIMEDOUT; + msleep(10); + } +} +EXPORT_SYMBOL_GPL(apple_pmp_report_wait_handed); + +static int apple_pmp_report_restore_noirq(struct device *dev) +{ + struct apple_pmp_report *rep = dev_get_drvdata(dev); + unsigned long flags; + bool held; + u64 want; + + if (!rep) + return 0; + spin_lock_irqsave(&rep->lock, flags); + held = !rep->defer && apple_pmp_report_hold_locked(rep); + want = rep->shadow; + spin_unlock_irqrestore(&rep->lock, flags); + if (held) + dev_info(dev, "PMP stopped after restore: requests %#llx held until it runs\n", + want); + return 0; +} + /* The unique, enabled and acknowledged T6030 display request (DISP, reg 7). */ static struct device_node *apple_pmp_report_t6030_entry(struct device_node *report) { @@ -2269,8 +2357,10 @@ static int apple_pmp_report_resume(struct device *dev) return 0; } -static DEFINE_SIMPLE_DEV_PM_OPS(apple_pmp_report_pm_ops, apple_pmp_report_suspend, - apple_pmp_report_resume); +static const struct dev_pm_ops apple_pmp_report_pm_ops = { + SYSTEM_SLEEP_PM_OPS(apple_pmp_report_suspend, apple_pmp_report_resume) + .restore_noirq = apple_pmp_report_restore_noirq, +}; static int apple_pmp_report_probe(struct platform_device *pdev) { @@ -2555,7 +2645,8 @@ static int apple_pmp_report_set_state(struct apple_pmp_report *rep, u32 id, rep->shadow |= bit_val; else rep->shadow &= ~bit_val; - ready = !rep->defer; + /* Never write a request to a stopped PMP (see the hold helper). */ + ready = !apple_pmp_report_hold_locked(rep); if (ready) { val = readq(rep->base + rep->offsets->tgt_read); val &= ~bit_val; diff --git a/drivers/power/reset/macsmc-reboot.c b/drivers/power/reset/macsmc-reboot.c index a2e1422e05bd8..1f307fe63e73f 100644 --- a/drivers/power/reset/macsmc-reboot.c +++ b/drivers/power/reset/macsmc-reboot.c @@ -127,15 +127,33 @@ static int macsmc_prepare_atomic(struct sys_off_data *data) static int macsmc_power_off(struct sys_off_data *data) { struct macsmc_reboot *reboot = data->cb_data; + int ret, i; - dev_info(reboot->dev, "Issuing power off (off1)\n"); + dev_emerg(reboot->dev, "Issuing power off (off1)\n"); - if (apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)) < 0) { - dev_err(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n"); - } else { + /* + * This is the only power-off path when Linux runs at EL2 without a + * monitor, and where a PSCI SYSTEM_OFF follows, a monitor without a + * power-off path resets the machine instead. So a slow or refused + * off1 must not fall through after only 100 ms: retry a refused + * command and give an accepted one time to take effect, and say on + * the console which of the two happened. + */ + for (i = 0; i < 3; i++) { + ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)); + if (ret >= 0) + break; + dev_emerg(reboot->dev, "MBSE = off1 refused (%d), attempt %d\n", ret, i + 1); mdelay(100); - WARN_ONCE(1, "Unable to power off system\n"); } + if (ret < 0) { + dev_emerg(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n"); + return NOTIFY_OK; + } + dev_emerg(reboot->dev, "off1 accepted, waiting for the power to drop\n"); + for (i = 0; i < 50; i++) + mdelay(100); + WARN_ONCE(1, "Unable to power off system: off1 accepted, power still on after 5 s\n"); return NOTIFY_OK; } @@ -175,7 +193,7 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio return NOTIFY_DONE; } - dev_info(reboot->dev, "Preparing for reboot (%p4ch)\n", &val); + dev_emerg(reboot->dev, "Preparing for reboot (%p4ch)\n", &val); /* On the Mac Mini, this will turn off the LED for power off */ if (apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val) < 0) @@ -194,6 +212,8 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio if (reboot->nvm.shutdown_flag && nvmem_cell_set_u8(reboot->nvm.shutdown_flag, shutdown_flag) < 0) dev_err(reboot->dev, "Failed to write shutdown_flag\n"); + dev_emerg(reboot->dev, "PMU flags set (shutdown_flag cell %s)\n", + reboot->nvm.shutdown_flag ? "present" : "absent"); return NOTIFY_OK; } diff --git a/drivers/rtc/rtc-apple-pmu.c b/drivers/rtc/rtc-apple-pmu.c index cd46c9b9a10b1..113e17aac5d42 100644 --- a/drivers/rtc/rtc-apple-pmu.c +++ b/drivers/rtc/rtc-apple-pmu.c @@ -432,17 +432,39 @@ static int apple_pmu_rtc_prepare(struct device *dev) if (rtc->io_failed) return -EBUSY; /* + * Suspend to idle keeps the PMU powered and the alarm wakes the system. * A firmware-driven suspend programs the wake sources itself, and the - * alarm stays armed in the PMU, which stays powered. Only a sleep state - * without either has no way to bring the alarm back. + * alarm stays armed in the PMU, which stays powered. Hibernation (the + * target state stays PM_SUSPEND_ON) is accepted as well: the alarm is + * stopped before the power-off below. Only a sleep state without any of + * these has no way to bring the alarm back. */ - if (pm_suspend_target_state != PM_SUSPEND_TO_IDLE && !pm_suspend_via_firmware()) + if (pm_suspend_target_state != PM_SUSPEND_TO_IDLE && + pm_suspend_target_state != PM_SUSPEND_ON && !pm_suspend_via_firmware()) return -EBUSY; return 0; } +/* + * Whether an armed PMU alarm powers the machine on again after the + * hibernation power-off is not established, so do not leave one armed. + */ +static int apple_pmu_rtc_poweroff(struct device *dev) +{ + struct apple_pmu_rtc *rtc = dev_get_drvdata(dev); + int error; + + mutex_lock(&rtc->transaction); + error = apple_pmu_rtc_stop_alarm(rtc); + mutex_unlock(&rtc->transaction); + if (error) + dev_warn(dev, "could not stop the alarm before power-off: %d\n", error); + return 0; +} + static const struct dev_pm_ops apple_pmu_rtc_pm = { .prepare = apple_pmu_rtc_prepare, + .poweroff = apple_pmu_rtc_poweroff, }; static void apple_pmu_rtc_release(void *context) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index ea0b6fe76b34a..ad0e4e7824bfc 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -117,6 +117,8 @@ struct apple_spmi { bool notify_irq; bool notify_parked; bool prev_fail; + /* The noirq restore rewrote the masks after a resume from disk. */ + bool hib_resynced; }; static void apple_spmi_irq_ack_raw(struct apple_spmi *spmi, u32 irq) @@ -630,9 +632,6 @@ static int apple_spmi_resume_noirq(struct device *dev) return 0; } -static DEFINE_NOIRQ_DEV_PM_OPS(apple_spmi_pm_ops, apple_spmi_suspend_noirq, - apple_spmi_resume_noirq); - static int apple_spmi_probe(struct platform_device *pdev) { struct apple_spmi *spmi; @@ -688,6 +687,108 @@ static int apple_spmi_probe(struct platform_device *pdev) return 0; } +#ifdef CONFIG_HIBERNATION +/* + * After a resume from disk the controller holds the boot kernel's masks and + * pending interrupts, not this kernel's: drop the pending ones and rewrite + * the masks from the cache the mask/unmask paths keep, notification IRQ + * included. + */ +static void apple_spmi_hib_resync_irqs(struct apple_spmi *spmi) +{ + u32 nirq = spmi->hw->num_irqs; + unsigned long flags; + + raw_spin_lock_irqsave(&spmi->irq_mask_lock, flags); + /* + * As at probe, drop what is latched: interrupts the boot kernel left + * pending complete a register selection of a device restored now + * before its own answer arrives. The devices read their state afresh. + */ + for (size_t offset = 0; offset < (nirq + 32) / 8; offset += 4) { + writel(0, spmi->regs + SPMI_IRQ_MASK_BASE + offset); + writel(U32_MAX, spmi->regs + SPMI_IRQ_ACK_BASE + offset); + } + for (size_t offset = 0; offset < (nirq + 32) / 8; offset += 4) { + u32 val = 0; + + if (offset < nirq / 8) + val = bitmap_read(spmi->irq_mask_cache, offset * 8, 32); + else if (spmi->notify_irq && offset == (nirq / 32) * 4) + val = BIT(nirq % 32); + writel(val, spmi->regs + SPMI_IRQ_MASK_BASE + offset); + } + raw_spin_unlock_irqrestore(&spmi->irq_mask_lock, flags); +} + +static int apple_spmi_restore_noirq(struct device *dev) +{ + struct apple_spmi *spmi = dev_get_drvdata(dev); + + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return apple_spmi_resume_noirq(dev); + if (spmi->irq < 0) + return 0; + + apple_spmi_hib_resync_irqs(spmi); + spmi->hib_resynced = true; + + return 0; +} + +/* + * A controller in a PM domain does not get the callback above: genpd's + * restore_noirq ends in the driver's resume_noirq, whatever callback + * genpd_finish_resume() was given (outside a wakeup path), and that has + * nothing to do after a resume from disk. The masks then stay as the boot + * kernel left them: its probe masked everything, and only its own drivers + * unmasked theirs before it loaded the image (on T6040 not the Type-C port + * controllers, whose driver is a module). The IRQ core disables lazily and + * unmasks only an IRQ it recorded as masked, so it does not write them again + * either, and the port controllers on nub_spmi_a0/a1 raise no event after the + * restore. genpd does not wrap the early restore: catch up there, before the + * devices on the bus are restored. + */ +static int apple_spmi_restore_early(struct device *dev) +{ + struct apple_spmi *spmi = dev_get_drvdata(dev); + + if (spmi->hib_resynced) { + spmi->hib_resynced = false; + return 0; + } + if (!pm_hibernate_is_restored() || spmi->irq < 0) + return 0; + + /* Commands can run by now: none may have its reply acknowledged away. */ + scoped_guard(mutex, &spmi->fifo_lock) + apple_spmi_hib_resync_irqs(spmi); + dev_info(dev, "resume from disk: interrupt masks rewritten in the early restore\n"); + + return 0; +} + +#endif + +/* + * Suspend-to-RAM parks the notification queue; a resume from disk rewrites the + * masks, in the early restore where a PM domain kept the noirq one from running. + */ +static const struct dev_pm_ops apple_spmi_pm_ops = { + .suspend_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), + .resume_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), + .freeze_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), + .thaw_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), + .poweroff_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), +#ifdef CONFIG_HIBERNATION + .restore_noirq = apple_spmi_restore_noirq, + .restore_early = apple_spmi_restore_early, +#else + .restore_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), +#endif +}; + static const struct of_device_id apple_spmi_match_table[] = { { .compatible = "apple,t8142-spmi", .data = &apple_spmi_gen4 }, { .compatible = "apple,t8103-spmi", .data = &apple_spmi_gen1 }, diff --git a/tools/asahi/pmu-rtc-tests.py b/tools/asahi/pmu-rtc-tests.py index 062597ca80b56..d1ab41dbeb44d 100644 --- a/tools/asahi/pmu-rtc-tests.py +++ b/tools/asahi/pmu-rtc-tests.py @@ -57,6 +57,8 @@ #define kfree free #define guard(x) (void) #define dev_err(...) ((void)0) +#define dev_warn(...) ((void)0) +#define PM_SUSPEND_ON 0 #define PM_SUSPEND_TO_IDLE 1 #define RTC_IRQF 0x80 #define RTC_AF 0x20 @@ -333,6 +335,14 @@ firmware_suspend = true; assert(!apple_pmu_rtc_prepare(&dev)); firmware_suspend = false; + /* Hibernation is accepted: its power-off stops the alarm, and goes on if that fails. */ + pm_suspend_target_state = PM_SUSPEND_ON; + assert(!apple_pmu_rtc_prepare(&dev)); + assert(!apple_pmu_rtc_set_alarm(&dev, &alarm) && (map.bytes[map.ctrl] & 0x40)); + assert(!apple_pmu_rtc_poweroff(&dev)); + assert(!(map.bytes[map.ctrl] & 0x40) && (map.bytes[map.mask] & 1)); + map.ops = 0; map.fail = 1; + assert(!apple_pmu_rtc_poweroff(&dev)); pm_suspend_target_state = PM_SUSPEND_TO_IDLE; } int main(void) { diff --git a/tools/asahi/t6030-pmp-graph-tests.py b/tools/asahi/t6030-pmp-graph-tests.py index 4c85001acf1a8..2b44c999ab689 100644 --- a/tools/asahi/t6030-pmp-graph-tests.py +++ b/tools/asahi/t6030-pmp-graph-tests.py @@ -49,6 +49,8 @@ def main(): 'static int apple_pmp_report_wait_t6030_14(', 'int apple_pmp_report_wait_supplier_ready('] if 'static bool apple_pmp_report_running(' in source: names.insert(0, 'static bool apple_pmp_report_running(') + if 'static bool apple_pmp_report_hold_locked(' in source: + names.insert(names.index('static int apple_pmp_report_set_state('), 'static bool apple_pmp_report_hold_locked(') if 'static int apple_pmp_report_wait_t6030_bound(' in source: names.insert(names.index('static struct device_node *apple_pmp_report_t6030_entry('), 'static int apple_pmp_report_wait_t6030_bound(') # System sleep (apple,release-for-sleep), where the source has it. From 8656e860e950d34f503e016ea2646495a9aa15c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:04 +0100 Subject: [PATCH 054/269] dmaengine: apple-sio: restart the SIO after hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restart and restore the SIO coprocessor after S4; stay idle across a dry-run suspend. Signed-off-by: František Balázsy --- drivers/dma/apple-sio.c | 110 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 8 deletions(-) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index 330bb7fa6b753..d01e34750556e 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -1838,6 +1838,20 @@ static int sio_send_v9_shmem(struct sio_data *sio, return 0; } +/* Publish the shared memory to the firmware; also used after a cold restart. */ +static int sio_send_shmem_params(struct sio_data *sio) +{ + int err; + + if (sio->protocol == 9) + return sio_send_v9_shmem(sio, sio_set_param); + + err = sio_set_param(sio, 1, sio->shmem_iova >> 12); + if (err) + return err; + return sio_set_param(sio, 2, SIO_SHMEM_SIZE); +} + static int sio_alloc_shmem(struct sio_data *sio) { const struct sio_dma_arrays *arrays = sio->hw->arrays; @@ -1879,13 +1893,8 @@ static int sio_alloc_shmem(struct sio_data *sio) err = devm_add_action_or_reset(sio->dev, sio_free_v9_shmem, sio); if (err) return err; - return sio_send_v9_shmem(sio, sio_set_param); } - - err = sio_set_param(sio, 1, sio->shmem_iova >> 12); - if (err) - return err; - return sio_set_param(sio, 2, SIO_SHMEM_SIZE); + return sio_send_shmem_params(sio); free_shmem: /* No address has been published yet. */ @@ -2143,7 +2152,8 @@ static __maybe_unused int sio_suspend_late(struct device *dev) struct sio_data *sio = dev_get_drvdata(dev); int i, ret; - if (!pm_suspend_via_firmware() || sio->asleep) + /* A dry run keeps the power, and an idled firmware doesn't wake with it kept. */ + if (!apple_sleep_power_loss() || sio->asleep) return 0; for (i = 0; i < sio->nchannels; i++) { @@ -2178,6 +2188,58 @@ static __maybe_unused int sio_suspend_late(struct device *dev) return 0; } +/* + * Bring a freshly loaded, halted SIO up again under the existing host state: + * the firmware-facing part of probe, reusing the shared memory, the DMA + * channels and their clients. + */ +static int sio_restart(struct sio_data *sio) +{ + u32 protocol = sio->protocol; + int err, i; + + /* Nothing can answer the calls that were in flight. */ + for (i = 1; i < SIO_NTAGS; i++) { + if (test_and_clear_bit(i, sio->tags.allocated)) + dev_warn(sio->dev, "dropping stale call tag %d\n", i); + reinit_completion(&sio->tags.completions[i]); + } + + err = apple_rtkit_reinit(sio->rtk); + if (err) + return dev_err_probe(sio->dev, err, "restart: RTKit reinit\n"); + + writel(CPU_CONTROL_RUN, sio->base + REG_CPU_CONTROL); + + err = apple_rtkit_boot(sio->rtk); + if (err) + return dev_err_probe(sio->dev, err, "restart: SIO did not boot\n"); + + err = apple_rtkit_start_ep(sio->rtk, EP_SIO); + if (err) + return dev_err_probe(sio->dev, err, "restart: starting SIO endpoint\n"); + + err = sio_call(sio, FIELD_PREP(SIOMSG_TYPE, MSG_START)); + if (err < 1) + return dev_err_probe(sio->dev, err ? err : -ENXIO, + "restart: starting SIO service\n"); + if (sio->protocol != protocol) { + dev_err(sio->dev, "restart: protocol changed from v%u to v%u\n", + protocol, sio->protocol); + return -EPROTO; + } + + err = sio_send_dt_params(sio); + if (err < 0) + return dev_err_probe(sio->dev, err, "restart: boot-up parameters\n"); + + err = sio_send_shmem_params(sio); + if (err < 0) + return dev_err_probe(sio->dev, err, "restart: shared memory\n"); + + return 0; +} + static __maybe_unused int sio_resume_early(struct device *dev) { struct sio_data *sio = dev_get_drvdata(dev); @@ -2232,8 +2294,40 @@ static __maybe_unused int sio_resume_early(struct device *dev) return 0; } +static __maybe_unused int sio_restore(struct device *dev) +{ + struct sio_data *sio = dev_get_drvdata(dev); + int err; + + /* + * The resume boot loaded the SIO firmware again and left the CPU + * halted (this driver is not part of the resume initramfs), while the + * restored session still believes the old instance runs. Start the + * new instance; DMA clients set their channels up again when they + * reprepare their streams. + */ + if (readl(sio->base + REG_CPU_CONTROL) & CPU_CONTROL_RUN) { + dev_info(dev, "resume from disk: SIO still running, session kept\n"); + } else { + err = sio_restart(sio); + if (!err) + dev_info(dev, "resume from disk: SIO restarted (protocol v%u)\n", + sio->protocol); + else + dev_err(dev, "resume from disk: SIO restart failed: %d\n", err); + } + + return pm_runtime_force_resume(dev); +} + +/* A firmware-driven suspend stops the firmware late; a resume from disk restarts it. */ static const struct dev_pm_ops sio_pm_ops = { - SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) + .suspend = pm_sleep_ptr(pm_runtime_force_suspend), + .resume = pm_sleep_ptr(pm_runtime_force_resume), + .freeze = pm_sleep_ptr(pm_runtime_force_suspend), + .thaw = pm_sleep_ptr(pm_runtime_force_resume), + .poweroff = pm_sleep_ptr(pm_runtime_force_suspend), + .restore = pm_sleep_ptr(sio_restore), LATE_SYSTEM_SLEEP_PM_OPS(sio_suspend_late, sio_resume_early) RUNTIME_PM_OPS(sio_suspend, sio_resume, NULL) }; From 827c54c68528750881fc03c0a725440f18d22419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 17:08:24 +0100 Subject: [PATCH 055/269] soc: apple: sep: leave the sleep request for the T6040 sleep's last step too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The enclave's sleep request goes out at the sleep's last refusal only where the sleep driver can take it, checked as CONFIG_APPLE_SOC_SLEEP. A T6040 kernel builds APPLE_T6040_SLEEP instead, which registers the enclave the same way, so the check leaves the request unstaged there, and the enclave's .suspend refuses every firmware-driven sleep with -EOPNOTSUPP. Take either sleep driver. Signed-off-by: František Balázsy --- drivers/soc/apple/pm_shim.c | 3 ++- tools/asahi/sep-suspend-errors-tests.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 689217e8d94cc..487e4a1ef446d 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -472,7 +472,8 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, int ret; /* Check capability before resources that legacy providers need not have. */ - if (!READ_ONCE(sep_pm_suspending) || !IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || + if (!READ_ONCE(sep_pm_suspending) || + !(IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || IS_ENABLED(CONFIG_APPLE_T6040_SLEEP)) || !is_kernel_in_hyp_mode()) return -EOPNOTSUPP; if (c->registered) diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index f05054301e4b1..b5f139dd13c34 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -52,7 +52,8 @@ def replace_once(source, before, after): 'read-as-deadline': ('sep_pm_wait_gated', 'if (ret)\n\t\treturn ret;\n\n\treturn !!poll_ret;', 'if (ret)\n\t\treturn 1;\n\n\treturn !!poll_ret;'), 'no-capability-check': ('sep_pm_commit_prepare', - ' || !IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) ||\n\t !is_kernel_in_hyp_mode()', ''), + ' ||\n\t !(IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || IS_ENABLED(CONFIG_APPLE_T6040_SLEEP)) ||' + '\n\t !is_kernel_in_hyp_mode()', ''), 'no-transport-check': ('sep_pm_commit_prepare', 'if (!apple_mbox_poll_one_supported(mbox))', 'if (false)'), 'lost-accept': ('sleep_enter', 'pm::SleepGate::Lost => return Err(EIO),', @@ -109,6 +110,7 @@ def replace_once(source, before, after): #define READ_ONCE(x) (x) #define IS_ENABLED(x) (config_enabled) #define CONFIG_APPLE_SOC_SLEEP 1 +#define CONFIG_APPLE_T6040_SLEEP 1 #define __maybe_unused #define __iomem #define unlikely(x) (x) From ae57b7a6ea2b7704639898bf253673f17f29367e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:09:25 +0100 Subject: [PATCH 056/269] soc: apple: t6040-sleep: tell drivers when the commit stops the coprocessors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple_sleep_commit_stops_coprocs() was the stub returning false on a T6040 kernel, although the T6040 commit stops every registered coprocessor after the last refusal. A GPU whose stop could not be left to the commit then stopped its firmware early instead of refusing the sleep, and the PMP's suspend did not refuse a sleep it could not leave to the commit either: a later refusal would have found them stopped. Return true for a real entry, the one whose commit runs the last stops, as the T8140 controller does; a dry run never stops them. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 7 +++++++ include/linux/soc/apple/sleep.h | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 84dbefc31245a..b5af846ace1ef 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -990,6 +990,13 @@ bool apple_sleep_coproc_stopped(struct device *dev) } EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); +/* A real entry: its commit stops the registered coprocessors; a dry run's never does. */ +bool apple_sleep_commit_stops_coprocs(void) +{ + return READ_ONCE(t6040_sleep) && is_kernel_in_hyp_mode() && apple_sleep_power_loss(); +} +EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); + /* * What still keeps the commit unreachable. The SoC power-down and the * wake return are IMPLEMENTED from the entry as measured on the J616s and diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 418ef9584f1d5..ca4a066468a5f 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -187,6 +187,7 @@ int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), vo void apple_sleep_enclave_unregister(struct device *dev); void apple_sleep_coproc_set_ready(struct device *dev, bool ready); bool apple_sleep_coproc_stopped(struct device *dev); +bool apple_sleep_commit_stops_coprocs(void); #else static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, int (*last_stop)(void *data), @@ -229,12 +230,12 @@ static inline bool apple_sleep_coproc_stopped(struct device *dev) { return false; } -#endif static inline bool apple_sleep_commit_stops_coprocs(void) { return false; } +#endif /* The T6040 controller can't send a sleep deep either: false, as with no sleep driver. */ static inline bool apple_sleep_request_deep_once(void) From 55a3c63ed50485c1f5887bbacdf472d75a4c8692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:10:10 +0100 Subject: [PATCH 057/269] soc: apple: t6040-sleep: drop the unused power word of a first-stop coprocessor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple_sleep_coproc_register_first() read the "reg" of the device's first power domain into a field nothing used. For the T6040 GPU that domain is the PMP report entry pmp-gfx, so the value was the entry's index, not a PMGR offset. The coprocessor's own last stop handles its power word. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index b5af846ace1ef..8b576af41f78d 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -834,9 +834,8 @@ static struct t6040_sleep_coproc { bool ready; bool stopped; u32 status; - /* No ASC: its last stop takes it whole, power word @ps included, before the others. */ + /* No ASC: its last stop takes it whole, power word included, before the others. */ bool first; - u32 ps; } t6040_sleep_coprocs[SLEEP_COPROCS]; static DEFINE_MUTEX(t6040_sleep_coprocs_lock); @@ -881,7 +880,6 @@ int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie c->ready = false; c->stopped = false; c->first = false; - c->ps = 0; c->dev = dev; return 0; @@ -892,27 +890,18 @@ int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void void *data) { struct t6040_sleep_coproc *c; - struct device_node *np; - u32 ps = 0; int ret; if (!last_stop) return -EINVAL; - np = of_parse_phandle(dev->of_node, "power-domains", 0); - if (np) { - of_property_read_u32(np, "reg", &ps); - of_node_put(np); - } ret = apple_sleep_coproc_register(dev, NULL, false, last_stop, data); if (ret) return ret; guard(mutex)(&t6040_sleep_coprocs_lock); c = t6040_sleep_coproc_find(dev); - if (c) { + if (c) c->first = true; - c->ps = ps; - } return 0; } From ad93de68bfee3cd6d3bdd531718313fa4e3c836a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 22:50:10 +0100 Subject: [PATCH 058/269] pinctrl: apple: put every AP pin back after a T6040 S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The AP GPIO block needs the saved configuration of every pin back on the system wake, except the pins the ADT lists in no-restore-gpio-pins (190-192 on J616s). Linux put back only the pins it set up itself. A sleep that cuts the block's power then left the pins the boot loader set up for others at their reset values, among them the chip select of the internal panel's TCON bus; after the wake the display firmware powered the panel's rails and never saw it come up ("No device added after powering on the rails. HPD=0"). Mark the AP block apple,resume-restore-all with the ADT's no-restore list, snapshot every readable pin from the hardware at suspend_noirq and write back the ones that changed at resume_noirq, logging the pins that were not Linux's. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 55 +++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 22eb998188c64..68db796670e6b 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -58,6 +58,13 @@ struct apple_gpio_pinctrl { bool cached; /* Pins this driver has written. Others belong to someone else. */ unsigned long *touched; + /* + * The block whose every pin the platform puts back after a sleep that lost its power + * (apple,resume-restore-all), but those in gpio-reserved-ranges. + */ + bool restore_all; + unsigned long *no_restore; + unsigned long *restored; struct apple_gpio_irq_group irqgrps[]; }; @@ -94,6 +101,10 @@ static bool resume_cached = true; module_param(resume_cached, bool, 0644); MODULE_PARM_DESC(resume_cached, "Put configured pins back after S2R on a cached block too (bring-up)"); +static bool resume_all = true; +module_param(resume_all, bool, 0644); +MODULE_PARM_DESC(resume_all, "After S2R put back every pin of a block marked apple,resume-restore-all, as the platform (bring-up)"); + static const struct regmap_config regmap_config = { .reg_bits = 32, .val_bits = 32, @@ -627,6 +638,24 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) pctl->touched = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); if (!pctl->touched) return -ENOMEM; + pctl->restore_all = of_property_read_bool(pdev->dev.of_node, "apple,resume-restore-all"); + if (pctl->restore_all) { + int n = of_property_count_u32_elems(pdev->dev.of_node, "gpio-reserved-ranges"); + u32 start, count; + + pctl->no_restore = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); + pctl->restored = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); + if (!pctl->no_restore || !pctl->restored) + return -ENOMEM; + /* The pins the firmware lists as no-restore, as (first pin, count) pairs. */ + for (i = 0; n > 0 && i + 1 < (unsigned int)n; i += 2) + if (!of_property_read_u32_index(pdev->dev.of_node, "gpio-reserved-ranges", + i, &start) && + !of_property_read_u32_index(pdev->dev.of_node, "gpio-reserved-ranges", + i + 1, &count) && + start < npins && count <= npins - start) + bitmap_set(pctl->no_restore, start, count); + } for (i = 0; i < npins; i++) { pins[i].number = i; @@ -695,11 +724,28 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) if (pctl->cached && !READ_ONCE(resume_cached)) return 0; + bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); + /* + * As the platform on the AP block at the system wake: every pin as the + * hardware has it now, but the ones the device tree names, which may not be written + * back. The pins of the panel's TCON bus, set up by the boot loader, are among them. + */ + if (pctl->restore_all && READ_ONCE(resume_all)) { + if (pctl->cached) + regcache_cache_bypass(pctl->map, true); + for (i = 0; i < pctl->pinctrl_desc.npins; i++) + if (!test_bit(i, pctl->no_restore) && + !regmap_read(pctl->map, REG_GPIO(i), &pctl->saved[i])) + set_bit(i, pctl->saved_valid); + if (pctl->cached) + regcache_cache_bypass(pctl->map, false); + return 0; + } + /* * Only the pins this driver configured. The others belong to the firmware or another * processor, and writing one back after the wake raises an SError. */ - bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); for_each_set_bit(i, pctl->touched, pctl->pinctrl_desc.npins) if (!regmap_read(pctl->map, REG_GPIO(i), &pctl->saved[i])) set_bit(i, pctl->saved_valid); @@ -718,17 +764,24 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) /* The saved words came from the cache; the hardware is what to compare. */ if (pctl->cached) regcache_cache_bypass(pctl->map, true); + if (pctl->restored) + bitmap_zero(pctl->restored, pctl->pinctrl_desc.npins); for_each_set_bit(i, pctl->saved_valid, pctl->pinctrl_desc.npins) { if (!regmap_read(pctl->map, REG_GPIO(i), &val) && val == pctl->saved[i]) continue; regmap_write(pctl->map, REG_GPIO(i), pctl->saved[i]); restored++; + if (pctl->restored && !test_bit(i, pctl->touched)) + set_bit(i, pctl->restored); } if (pctl->cached) regcache_cache_bypass(pctl->map, false); if (restored) dev_info(dev, "resume: %u of %u configured pins put back\n", restored, bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); + if (pctl->restored && !bitmap_empty(pctl->restored, pctl->pinctrl_desc.npins)) + dev_info(dev, "resume: pins not set up by Linux put back: %*pbl\n", + pctl->pinctrl_desc.npins, pctl->restored); bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); return 0; From 23a172f30d9ac450ab4d226955892f0ce214a2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 00:37:10 +0100 Subject: [PATCH 059/269] pinctrl: apple: put the block's global word back and write pins in steps after S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a T6040 S2R no pin of the AP block read its input any more: every data bit read 0. Level-low interrupts stormed (the headphone codec's ran 2.7 million times in a minute), and the display firmware, which reads and drives pins of this block itself, never lit the panel again. The wake needs two things that Linux did not do. The word at 0xc48 has to be back (1 on a running J616s). And a pin is never written in one go: the configuration goes first with the input buffer as it is, off after the power loss, the input enable follows in a write of its own, the lock last. Do both for a block restored as a whole. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 52 ++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 68db796670e6b..0589195dfea3a 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -65,6 +65,9 @@ struct apple_gpio_pinctrl { bool restore_all; unsigned long *no_restore; unsigned long *restored; + /* The block's word at REG_GLOBAL, which the platform keeps across a sleep too. */ + u32 saved_global; + bool saved_global_valid; struct apple_gpio_irq_group irqgrps[]; }; @@ -91,6 +94,12 @@ struct apple_gpio_pinctrl { #define REG_GPIOx_LOCK BIT(21) #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22) #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5)) +/* + * One word past the pins and the interrupt groups. The platform reads it when the + * system quiesces and writes it back on the wake, before it puts the pins back. It is 1 + * on a running J616s; with it lost, no pin reads its input any more. + */ +#define REG_GLOBAL 0xc48 /* * T6040 bring-up: put the configured pins back after a firmware-driven @@ -708,6 +717,32 @@ static const struct of_device_id apple_gpio_pinctrl_of_match[] = { }; MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match); +/* + * A pin's whole word written the way the platform writes it: the + * configuration first with the input buffer as it is (off after a power loss), then the + * input enable in a write of its own, the lock last. A locked pin is left alone. Written + * in one go, with the input enable among the rest, a pin read 0 for good after the wake. + */ +static void apple_gpio_safe_reconfigure(struct apple_gpio_pinctrl *pctl, unsigned int pin, + u32 old, u32 cfg) +{ + u32 val = cfg & ~(REG_GPIOx_LOCK | REG_GPIOx_INPUT_ENABLE); + + if (old & REG_GPIOx_LOCK) + return; + if (!(cfg & REG_GPIOx_INPUT_ENABLE)) { + regmap_write(pctl->map, REG_GPIO(pin), + old & ~(REG_GPIOx_LOCK | REG_GPIOx_INPUT_ENABLE)); + regmap_write(pctl->map, REG_GPIO(pin), val); + } else { + regmap_write(pctl->map, REG_GPIO(pin), (old & REG_GPIOx_INPUT_ENABLE) | val); + if (!(old & REG_GPIOx_INPUT_ENABLE)) + regmap_write(pctl->map, REG_GPIO(pin), cfg & ~REG_GPIOx_LOCK); + } + if (cfg & REG_GPIOx_LOCK) + regmap_write(pctl->map, REG_GPIO(pin), cfg); +} + /* * A firmware-driven suspend powers this block off, and neither the pin core nor * a consumer sets a pin up again. Slots that fault aren't readable in the regmap, @@ -730,7 +765,10 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) * hardware has it now, but the ones the device tree names, which may not be written * back. The pins of the panel's TCON bus, set up by the boot loader, are among them. */ + pctl->saved_global_valid = false; if (pctl->restore_all && READ_ONCE(resume_all)) { + pctl->saved_global = readl(pctl->base + REG_GLOBAL); + pctl->saved_global_valid = true; if (pctl->cached) regcache_cache_bypass(pctl->map, true); for (i = 0; i < pctl->pinctrl_desc.npins; i++) @@ -761,15 +799,25 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) if (!pctl->saved || !pm_resume_via_firmware()) return 0; + if (pctl->saved_global_valid) { + val = readl(pctl->base + REG_GLOBAL); + writel(pctl->saved_global, pctl->base + REG_GLOBAL); + dev_info(dev, "resume: global word %#x, put back to %#x\n", val, + pctl->saved_global); + pctl->saved_global_valid = false; + } + /* The saved words came from the cache; the hardware is what to compare. */ if (pctl->cached) regcache_cache_bypass(pctl->map, true); if (pctl->restored) bitmap_zero(pctl->restored, pctl->pinctrl_desc.npins); for_each_set_bit(i, pctl->saved_valid, pctl->pinctrl_desc.npins) { - if (!regmap_read(pctl->map, REG_GPIO(i), &val) && val == pctl->saved[i]) + if (regmap_read(pctl->map, REG_GPIO(i), &val)) + val = 0; + else if (val == pctl->saved[i]) continue; - regmap_write(pctl->map, REG_GPIO(i), pctl->saved[i]); + apple_gpio_safe_reconfigure(pctl, i, val, pctl->saved[i]); restored++; if (pctl->restored && !test_bit(i, pctl->touched)) set_bit(i, pctl->restored); From 9ccadcb9a91c0609cb6583d761087fe3c1b0af67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 10:29:01 +0100 Subject: [PATCH 060/269] soc: apple: t6040-sleep: raise the SIO peripheral words again that were on at the commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The power loss resets every PMGR power word. The domains a Linux driver owns come back when their devices resume, but what the boot loader left on and no driver claims stayed gated after the wake: on J616s I2C0, SPI0/1/3/4/5, the UARTs and AES. SPI4 is the internal panel's bus. The SoC expects every device back as it was. Keep each power word at the commit and, after the wake returned, raise the SIO peripheral words, I2C0 to UART6, that were fully on and came back with a lower target, in ADT order so that parents go first. The snapshot also holds what the sleep engine and the coprocessors' drivers own (SIO_CPU, PMP, PMS_SRAM, ISP_SYS, the ATC commons the commit hands to the engine): those stay with their owners. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 87 ++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 8b576af41f78d..85103d7b6c89d 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -577,6 +577,8 @@ struct t6040_sleep { void __iomem *mini_ps; /* 0x508300000: MiniPMGR PS words */ void __iomem *aop_ps; /* MiniPMGR AOP + audio island PS words 0x508304000..0x158 */ void __iomem *snap_soc, *snap_east; /* PS snapshot pages 0x502800000 / 0x502280000 */ + u32 *ps_saved; /* each PS word at the commit, see t6040_ps_restore() */ + bool ps_saved_valid; void __iomem *pmc_probe_io[9]; /* bring-up PMC probe words */ u32 aop_left; /* AOP words left on << 8 | first index left on */ void __iomem *cpm_cfg[3]; /* cluster + 0xe50000 (iBoot idle power-down, bring-up) */ @@ -2010,6 +2012,84 @@ static const u32 t6040_ps_snap_pa[] = { }; #define T6040_PS_SNAP_WORDS DIV_ROUND_UP(ARRAY_SIZE(t6040_ps_snap_pa), 32) + +/* + * The power loss resets every power word, and only the domains a Linux driver owns are + * raised again when their devices resume. The PMGR puts each device back as it was; + * here the SIO peripheral words (I2C0 .. UART6 in SOC-East) that were fully on at the commit + * and came back lower are raised again, parents first (ADT order). On J616s those are what + * the boot loader left on and no driver claims: I2C0, SPI0/1/3/4/5 (SPI4 goes to the + * internal panel), the UARTs, AES. Nothing else: the commit's snapshot also has the words + * the sleep engine and the coprocessors' drivers own (SIO_CPU, PMP, PMS_SRAM, ISP_SYS, the + * ATC commons the commit hands to the engine), and those are their owners' to raise. + */ +#define T6040_PS_RESTORE_FIRST 0x2280150 /* I2C0 */ +#define T6040_PS_RESTORE_LAST 0x2280228 /* UART6 */ +static bool ps_restore = true; +module_param(ps_restore, bool, 0644); +MODULE_PARM_DESC(ps_restore, "After a real sleep raise the SOC power words that were on at the commit and came back lower"); + +static void __iomem *t6040_ps_snap_addr(struct t6040_sleep *s, unsigned int i) +{ + u32 pa = t6040_ps_snap_pa[i]; + bool soc = (pa >> 16) == 0x0280; + void __iomem *b = soc ? s->snap_soc : s->snap_east; + + return b ? b + (pa - (soc ? 0x02800000 : 0x02280000)) : NULL; +} + +static void t6040_ps_save(struct t6040_sleep *s) +{ + unsigned int i; + + s->ps_saved_valid = false; + if (!s->ps_saved) + return; + for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { + void __iomem *w = t6040_ps_snap_addr(s, i); + + s->ps_saved[i] = w ? readl(w) : 0; + } + s->ps_saved_valid = true; +} + +static void t6040_ps_restore(struct t6040_sleep *s) +{ + unsigned int i, n, raised = 0, failed = 0; + u32 val, cur = 0; + + if (!s->ps_saved_valid || !READ_ONCE(ps_restore)) + return; + s->ps_saved_valid = false; + for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { + void __iomem *w = t6040_ps_snap_addr(s, i); + u32 saved = s->ps_saved[i]; + + if (!w || t6040_ps_snap_pa[i] < T6040_PS_RESTORE_FIRST || + t6040_ps_snap_pa[i] > T6040_PS_RESTORE_LAST || + FIELD_GET(PS_TARGET, saved) != 0xf || FIELD_GET(PS_ACTUAL, saved) != 0xf) + continue; + val = readl(w); + if (FIELD_GET(PS_TARGET, val) == 0xf) + continue; + writel((val & ~PS_TARGET) | FIELD_PREP(PS_TARGET, 0xf), w); + /* Timekeeping is still suspended here: count the wait, no ktime. */ + for (n = 0; n < 1000; n++) { + cur = readl(w); + if (FIELD_GET(PS_ACTUAL, cur) == 0xf) + break; + udelay(10); + } + if (n == 1000) + failed++; + raised++; + pr_info("t6040-sleep: wake: power word %#x: %#x at the commit, %#x after the wake, now %#x\n", + t6040_ps_snap_pa[i], saved, val, cur); + } + if (raised) + pr_info("t6040-sleep: wake: %u power word(s) raised again, %u not on\n", raised, + failed); +} #define T6040_PS_SNAP_PMU 0xa2c0 #define T6040_PS_SNAP_MAGIC 0x504e5350 /* "PSNP" in PMU byte order */ @@ -4396,6 +4476,7 @@ static int t6040_sleep_commit(unsigned long unused) if (READ_ONCE(fab_on)) t6040_fabrics_on(s); + t6040_ps_save(s); if (READ_ONCE(ps_snap)) t6040_ps_snap_store(s); if (READ_ONCE(dcs_rec)) @@ -5828,8 +5909,10 @@ static int t6040_sleep_enter(suspend_state_t state) if (s->returned) t6040_ret_mark(s, 0x41); t6040_wake_restore(s); - if (s->returned) + if (s->returned) { t6040_ret_mark(s, 0x42); + t6040_ps_restore(s); + } t6040_disp_back(s); if (s->returned) t6040_ret_mark(s, 0x43); @@ -6302,6 +6385,8 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->mtp_ps = t6040_ioremap_np(&pdev->dev, T6040_MTP_PS_PA, 8 * T6040_MTP_PS_WORDS); sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); sleep->snap_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); + sleep->ps_saved = devm_kcalloc(&pdev->dev, ARRAY_SIZE(t6040_ps_snap_pa), + sizeof(*sleep->ps_saved), GFP_KERNEL); sleep->atc_aon = t6040_ioremap_np(&pdev->dev, 0x508320c00ULL, 0x200); for (i = 0; i < 16; i++) sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + From 58a32cf4f3437666cebfbac16257beb0ae444ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 07:49:08 +0100 Subject: [PATCH 061/269] soc: apple: t6040-sleep: set the display pipes' PMC interface enables again after the wake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A display pipe's bandwidth-request and boost interfaces in the PMC (0x5048ac020 / 0x5048ac028, one bit per pipe) belong enabled while the pipe's SYS domain is powered. Under Linux the boot loader's values stand, boost 7 on J616s, until an S2R cuts the power: the words come back 0 and nothing sets them again. Without its boost bit the internal pipe's backlight block sends no zone data to the panel's backlight controller (cleared by hand on a running system, the SPI stream stops at once and returns with the bit), the firmware never reaches the stage that enables the frame sync, and the panel stays dark after the wake. Keep both words at the commit and, once the display's domains are back, set the bits that were lost. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 85103d7b6c89d..23aaefe5aedc5 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -551,6 +551,8 @@ struct t6040_sleep { bool disp_down; /* t6040_disp_off() lowered the internal display */ void __iomem *pmc_dcp; /* PMS pmc-dcp voter pair 0x5048a9c14/c18 */ u32 pmc_dcp_saved[2]; + u32 pmc_if_saved[2]; /* BWR and boost interface enables at the commit */ + bool pmc_if_valid; /* The GP PCIe host's two ports (Wi-Fi/BT, SD) for the commit's port stop (T8140's). */ void __iomem *gp_port[2]; void __iomem *gp_phy[2]; @@ -5261,6 +5263,10 @@ static bool gated_cpu_target_only = true; module_param(gated_cpu_target_only, bool, 0644); MODULE_PARM_DESC(gated_cpu_target_only, "A DISP_CPU already gated (ACTUAL 0) only gets TARGET 0, keeping AUTO_ENABLE, and dart-dcp is left unread"); +static bool pmc_if_restore = true; +module_param(pmc_if_restore, bool, 0644); +MODULE_PARM_DESC(pmc_if_restore, "After a real sleep set the display pipes' PMC BWR and boost interface enables again"); + static bool pmc_dcp_off = true; module_param(pmc_dcp_off, bool, 0644); MODULE_PARM_DESC(pmc_dcp_off, "Drop the PMS DCP voters to 0 before the internal display goes down"); @@ -5514,7 +5520,19 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group if (!s->soc_east) return -ENODEV; s->disp_down = true; - /* The PMS DCP voters to 0 first, as the the platform entry trace and T8140's driver end. */ + /* + * The PMC's bandwidth-request and boost interface enables of the display pipes + * (0x5048ac020 / 0x5048ac028, one bit per pipe; the PMGR sets them when it powers + * a pipe's SYS domain). The power loss clears them and nothing sets them + * again: without its boost bit a pipe's backlight data stream never runs. + */ + s->pmc_if_valid = false; + if (s->pmc_bwr) { + s->pmc_if_saved[0] = readl(s->pmc_bwr); + s->pmc_if_saved[1] = readl(s->pmc_bwr + PMC_BOOST); + s->pmc_if_valid = true; + } + /* The PMS DCP voters to 0 first, as the entry and T8140's driver end. */ if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { s->pmc_dcp_saved[0] = readl(s->pmc_dcp); s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); @@ -5639,6 +5657,18 @@ static void t6040_disp_back(struct t6040_sleep *s) writel(s->pmc_dcp_saved[1], s->pmc_dcp + 4); writel(s->pmc_dcp_saved[0], s->pmc_dcp); } + if (s->pmc_if_valid && READ_ONCE(pmc_if_restore)) { + u32 bwr = readl(s->pmc_bwr), boost = readl(s->pmc_bwr + PMC_BOOST); + + s->pmc_if_valid = false; + if (bwr != s->pmc_if_saved[0]) + writel(bwr | s->pmc_if_saved[0], s->pmc_bwr); + if (boost != s->pmc_if_saved[1]) + writel(boost | s->pmc_if_saved[1], s->pmc_bwr + PMC_BOOST); + if (bwr != s->pmc_if_saved[0] || boost != s->pmc_if_saved[1]) + pr_info("t6040-sleep: wake: PMC display interfaces BWR %#x boost %#x, put back to %#x %#x\n", + bwr, boost, readl(s->pmc_bwr), readl(s->pmc_bwr + PMC_BOOST)); + } } static bool disp_probe; From dcd39daf90dfb77c7b491edba62d0d25e4bb3300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 10:10:15 +0100 Subject: [PATCH 062/269] soc: apple: t6040-sleep: clear ANS's fabric bridge enable before the engine takes ANS down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wake sets bit 0 of ANS's fabric bridge (+0x104/+0x108) once a retained domain is back up. Nothing cleared it on the next entry: it has to be cleared before the domain goes off, and here the A2D engine powers ANS down, not this driver. iBoot leaves the bit clear, so the first sleep of a boot went down with it clear; the second met the bridge armed and stalled in the engine's storage power-down until the AP watchdog reset the SoC. Clear the bit in the commit once ANS is stopped. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 23aaefe5aedc5..0d42d72c84cfe 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -630,6 +630,7 @@ struct t6040_sleep { /* ANS's storage ST lanes: windows + whether a commit stopped ANS. */ struct t6040_apcie_st apcie_st; bool pcie_ans_stopped; + bool ans_bridge_quiesced; /* the commit found ANS's fabric bridge armed */ int ans_off_ret; int pcie_ans_on_ret; u32 apcie_wake_step; @@ -3917,6 +3918,37 @@ static void t6040_apcie_diag_store(struct t6040_sleep *s) &s->apcie_diag[i], NULL, 0); } +/* + * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The platform clears the bit before a + * retained domain goes off and sets it again once it is back up; here the wake sets it + * (t6040_sleep_raise_apcie_st()) and the A2D engine, not this driver, takes ANS down. iBoot + * leaves the bit clear, so a boot's first sleep goes down with it clear; without this the + * next one meets the bridge as the first wake armed it. Only while ANS is on: the bridge sits + * in it. Returns whether the bit was set. + */ +static bool ans_bridge_quiesce = true; +module_param(ans_bridge_quiesce, bool, 0644); +MODULE_PARM_DESC(ans_bridge_quiesce, + "Clear ANS's fabric-bridge bit 0 in the commit, once ANS is stopped, as the platform before the domain goes off"); + +static bool t6040_ans_bridge_quiesce(struct t6040_sleep *s) +{ + void __iomem *bridge = s->axi2af_bridge[AXI2AF_ANS]; + u32 a, b; + + if (!READ_ONCE(ans_bridge_quiesce) || !bridge || !s->soc_east || + FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != 0xf) + return false; + a = readl(bridge + AXI2AF_BRIDGE_104); + b = readl(bridge + AXI2AF_BRIDGE_108); + if (!((a | b) & BIT(0))) + return false; + writel(a & ~BIT(0), bridge + AXI2AF_BRIDGE_104); + writel(b & ~BIT(0), bridge + AXI2AF_BRIDGE_108); + + return true; +} + static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; @@ -3955,6 +3987,7 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) writel(readl(a->axi + APCIE_AXI_EN0) & ~BIT(0), a->axi + APCIE_AXI_EN0); writel(readl(a->axi + APCIE_AXI_EN1) & ~BIT(0), a->axi + APCIE_AXI_EN1); } + s->ans_bridge_quiesced = t6040_ans_bridge_quiesce(s); if (READ_ONCE(apcie_st_ps)) t6040_ps_lower(ps, PS_ANS); /* The caller polls PS_ANS off, as #1208 does. */ @@ -5917,6 +5950,7 @@ static int t6040_sleep_enter(suspend_state_t state) t6040_mark(s, 0x12); t6040_pmc_cp(s, 2); s->pcie_ans_stopped = false; + s->ans_bridge_quiesced = false; t6040_sleep_coprocs_stop(true); t6040_sleep_coprocs_stop(false); t6040_mark(s, 0x13); @@ -5935,6 +5969,8 @@ static int t6040_sleep_enter(suspend_state_t state) if (s->alarm_ok) pr_info("t6040-sleep: marks: PMU comparator was %#x (reply %#x), cpu_suspend %d\n", s->alarm_saved, s->alarm_hdr, ret); + if (s->ans_bridge_quiesced) + pr_info("t6040-sleep: ANS's fabric bridge was armed at the commit: quiesced\n"); if (s->returned) t6040_ret_mark(s, 0x41); From dd044b46f88873924ee82a9fbec0cac3252ad5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 10:29:48 +0100 Subject: [PATCH 063/269] soc: apple: t6040-sleep: leave the bring-up marks off by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The marks are written into the PMU RTC comparator, which is the wake alarm's: the mark the commit left there took the alarm's place, so no S2R ever ended on the RTC, only on a key. They also go to the SPMI controller raw, beside the SPMI driver's own transfers. After an S2R wake a transfer of the PMU RTC's interrupt thread then got no reply ("failed to wait for RX FIFO not empty", "cannot acknowledge RTC interrupt: -5"), and that driver gave its alarm up for the rest of the boot. Default the marks off, and keep the suspend-phase record, which only holds marks, with them. With marks=0 the PMU RTC alarm ended three S2R in a row on a J616s, without the SPMI error. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 0d42d72c84cfe..521e2ef3e30d9 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -2553,10 +2553,15 @@ static int t6040_pmu_cmd(struct t6040_sleep *s, u32 cmd, const u32 *tx, u32 *rx, * Bring-up progress through a reset (#1314's way on T8132): the PMU keeps its RTC comparator * across one, and userspace reads it back on any kernel (spmi_nvmem0 offset 0x2008). A value * 0x5334xxxx is the last step reached; the saved alarm + 1 s means the commit itself ran. + * + * Off by default. The comparator is the wake alarm's: a mark left in it at the commit takes + * the alarm's place, and that sleep ends only on a key. And the marks go to the SPMI + * controller raw, beside the SPMI driver's own transfers: during a resume one of the PMU + * RTC's interrupt thread then got no reply, and that driver gives its alarm up for good. */ -static bool marks = true; +static bool marks; module_param(marks, bool, 0644); -MODULE_PARM_DESC(marks, "Write S2R progress marks into the PMU RTC comparator (bring-up)"); +MODULE_PARM_DESC(marks, "Bring-up: write S2R progress marks into the PMU RTC comparator (takes the wake alarm's place: no RTC wake)"); static void t6040_mark_raw(struct t6040_sleep *s, u32 v); @@ -2653,7 +2658,8 @@ static void t6040_susf_begin(struct t6040_sleep *s) { u32 w[2] = { T6040_SUSF_MAGIC, 0 }; - if (!READ_ONCE(susp_rec) || !s->spmi0) + /* The record holds device marks: nothing to keep, and no raw SPMI, without them. */ + if (!READ_ONCE(marks) || !READ_ONCE(susp_rec) || !s->spmi0) return; t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU, SPMI_OP_WRITE4), &w[0], NULL, 0); t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU + 4, SPMI_OP_WRITE4), &w[1], NULL, 0); From fa450ded41479974811073f98f25348387748615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 10:57:24 +0100 Subject: [PATCH 064/269] soc: apple: t6040-sleep: run the S2R entry on every ATC port that still needs it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engine's own entry stalls on a port whose PHY is active and has not asked for its clock gate: it writes ATCPHY_CFG +0x2c and waits for the AHB clock ack at +0x04, which only follows the three S2R entry writes to the port's CFG block. The commit did those for port 3 alone, the HDMI output's DisplayPort lanes. A display in DP alternate mode puts any other port in the same state: with one on port 0 the entry stalled there (MCPS in seq 25 at 0x3_8320_0004) until the AP watchdog reset the SoC. A parked port and one the PHY driver put to sleep already have +0x28 bit 0 set. Do the three writes on every port of the mask that has not, and make the mask all four ports. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 521e2ef3e30d9..7bbf67e313d31 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -3423,10 +3423,17 @@ static void t6040_atc_commons_on(struct t6040_sleep *s, u32 *timeouts) * 28/2c of the 4 ports now; atc_s2r (port mask) does the three writes + the ack poll right * before the timer, after the commons are up (fab_on bit 2). The 'ATCF' record at PMU 0xa240 * (instead of 'FABS') keeps, per port, +0x04 and a word of +0x08/+0x24/+0x28/+0x2c low bytes. + * + * Port 3 carries the HDMI output's DisplayPort lanes and is always in that state. Any other + * port is too once a display runs on it in DP alternate mode: the engine then stalls on that + * port in the entry itself (seq 25, the same +0x2c write and +0x04 poll; port 0 with a + * display on it). A parked port and one the PHY driver put to sleep (USB3) already have + * +0x28 bit 0 set. So every port in the mask whose +0x28 bit 0 is still clear gets the three + * writes; the default mask is all four. */ -static unsigned int atc_s2r = 8; +static unsigned int atc_s2r = 0xf; module_param(atc_s2r, uint, 0644); -MODULE_PARM_DESC(atc_s2r, "Bring-up: port mask; the platform aciophy_s2r_entry's CFG writes (+0x2c, +0x08, +0x28 |= 1, poll +0x04 bit 8) right before the S2R timer"); +MODULE_PARM_DESC(atc_s2r, "Port mask: the CFG writes of the PHY S2R entry (+0x2c, +0x08, +0x28 |= 1, poll +0x04 bit 8) right before the S2R timer on each port that has not asked for its clock gate yet"); static bool atc_rec = true; module_param(atc_rec, bool, 0644); MODULE_PARM_DESC(atc_rec, "Bring-up: record the 4 ATCPHY_CFG blocks at PMU 0xa240 right before the S2R timer (replaces 'FABS')"); @@ -3441,7 +3448,7 @@ static u32 t6040_atc_s2r_entry(struct t6040_sleep *s, unsigned int mask) for (n = 0; n < 4; n++) { void __iomem *c = s->atc_cfg[n]; - if (!(mask & BIT(n)) || !c) + if (!(mask & BIT(n)) || !c || (readl(c + 0x28) & BIT(0))) continue; writel(readl(c + 0x2c) | BIT(0), c + 0x2c); udelay(1); From 046db4a40e9ce8cfe088e2f34033a445039fdab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 21:27:29 +0100 Subject: [PATCH 065/269] mfd: macsmc: end the SMC's silent running once a wake is a full one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep's commit writes WKTP = 2 before 'slpa', the value for power state 0, and nothing wrote the key again. The SMC expects a second write: 0 once a wake has become a full one. Without that 0 the SMC takes all the time awake for a power nap. On a J616s, after the first suspend to RAM of a boot: - a manual fan speed is refused for the rest of the boot: the SMC never leaves its own fan policy after the unlock key, and the mode request times out after 10 s. Of 30 kernel builds on one day, each of which starts by taking the fans: taken in 11 of the 12 before the boot's first sleep, refused in all 18 after one; - under load the SMC logs "SMC Thermal pressure requested Sleep during power nap" and "systemctl: sleep OS (2)" once a second. Write 0 after 'waka' once the wake is not a dark one, and when a sleep ends whose commit was refused. A dark wake is left as it is: it sleeps again. wake_wktp < 0 writes nothing. One boot of a J616s, a fan speed requested after each step: taken before any sleep; refused after a sleep that wrote no WKTP at all (sleep_wktp=0), so the 2 is not what makes the wake silent; taken after a sleep with the 0 written at its wake; refused again after one without (wake_wktp=-1); taken again after the next with it. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 13 +++++++++++-- drivers/mfd/macsmc.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/mfd/macsmc.h | 2 ++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index f7bb195e00b9f..cd43d92bd86f1 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -238,8 +238,8 @@ static void macsmc_sleep_order_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); - /* As in the trace, values included: WKTP's 2 comes before 'slpa'. */ - KUNIT_ASSERT_EQ(test, ctx->seen_count, 8U); + /* As on the platform, values included: WKTP's 2 comes before 'slpa', its 0 ends the wake. */ + KUNIT_ASSERT_EQ(test, ctx->seen_count, 9U); KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); KUNIT_EXPECT_EQ(test, ctx->seen[1], 2U); KUNIT_EXPECT_EQ(test, ctx->seen[2], SMC_KEY(slpa)); @@ -248,6 +248,8 @@ static void macsmc_sleep_order_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[5], SMC_KEY(MBSE)); KUNIT_EXPECT_EQ(test, ctx->seen[6], SMC_KEY(MBSW)); KUNIT_EXPECT_EQ(test, ctx->seen[7], SMC_KEY(waka)); + KUNIT_EXPECT_EQ(test, ctx->seen[8], 0U); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); } static void macsmc_test_outbox_poll(struct apple_smc *smc, const char *after, @@ -291,6 +293,13 @@ static void macsmc_sleep_refused_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(slpa)); KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_IDLE); KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_ON); + + /* The key the commit wrote is taken back when the sleep ends, once. */ + KUNIT_EXPECT_TRUE(test, ctx->smc.wake_silent); + apple_smc_wake_full(&ctx->smc); + apple_smc_wake_full(&ctx->smc); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 5U); + KUNIT_EXPECT_EQ(test, ctx->seen[4], 0U); } static void macsmc_notice_test(struct kunit *test) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 59b0793f2118f..926643f10fa0f 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -978,6 +978,8 @@ static int apple_smc_sleep_commit(struct apple_smc *smc) unsigned int wktp = READ_ONCE(sleep_wktp); int ret; + /* Whatever is written here, the SMC runs the wake after this silent until it is told. */ + smc->wake_silent = true; if (wktp) { ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); if (ret) @@ -994,6 +996,33 @@ static int apple_smc_sleep_commit(struct apple_smc *smc) return ret; } +/* + * WKTP once a wake is a full one: 0, where a wake that stays dark keeps the key nonzero. + * Without the 0 the SMC takes all the time + * awake for a power nap, whether the sleep wrote its 2 or not: it does not leave its own fan + * policy for a manual fan request (the mode request times out), and under load it logs + * "Thermal pressure requested Sleep during power nap" and asks for a sleep once a second. + * Negative writes nothing. + */ +static int wake_wktp; +module_param(wake_wktp, int, 0644); +MODULE_PARM_DESC(wake_wktp, "WKTP value written once a wake is a full one (negative: none)"); + +static void apple_smc_wake_full(struct apple_smc *smc) +{ + int wktp = READ_ONCE(wake_wktp); + int ret; + + if (!smc->wake_silent) + return; + smc->wake_silent = false; + if (wktp < 0) + return; + ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); + if (ret) + dev_err(smc->dev, "Failed to write WKTP = %d: %d\n", wktp, ret); +} + static int apple_smc_wake_reason_names(const struct apple_smc *smc, u64 record, char *buf, size_t size) { @@ -1101,6 +1130,9 @@ static void apple_smc_sleep_wake(struct apple_smc *smc) apple_smc_lid_wake(smc); if (apple_smc_port_reasons_only(smc, smc->wake_record)) apple_sleep_debug_link_cut_wake(); + /* A dark wake sleeps again, and its commit writes the key anyway. */ + if (!pm_dark_wake()) + apple_smc_wake_full(smc); /* A dark wake isn't one: with wakeup_count in use, it would abort the sleep after it. */ if (smc->wake_record && !pm_dark_wake()) pm_wakeup_event(smc->dev, 0); @@ -1130,6 +1162,8 @@ static void apple_smc_complete(struct device *dev) apple_smc_write_mbse(smc, SMC_KEY(waka)); smc->sleep_warned = false; } + /* A commit the SMC refused, or dark wakes that ended in a refusal. */ + apple_smc_wake_full(smc); } /* diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 530809696b254..deed3e0de243c 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -120,6 +120,8 @@ struct apple_smc { bool sleep_warned; /* To be told at the noirq suspend, after the debug link's cut. */ bool sleep_warn_late; + /* A sleep was committed, and the wake after it has not been declared a full one. */ + bool wake_silent; /* In a noirq phase: replies are polled for. */ bool polled; /* MBSW at the last wake: what the SMC had woken the AP for. */ From 135fefca702588befbc861aa12558b716c609412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 07:23:17 +0100 Subject: [PATCH 066/269] soc: apple: t6040-sleep: drop the list of steps that had not run yet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The controller kept four fixed entries that stood for the parts of a T6040 sleep no hardware had run: the SoC power-down, the wake return, the storage PCIe lower and raise, and the display coprocessors' cold restart. They counted as blockers that never clear, and a real entry needed the i_know_untested parameter to ignore them. Every suspend logged them, although all four run on the J616s. Remove the list and the parameter. The commit is reached when no live blocker remains (EL2, the boot object's return path, the CPUs that can park, the display power states) and t6040_sleep.allow_entry=1 is given. A dry run that is asked for comes first, so t6040_sleep.dry_run still refuses at the commit point on a kernel booted with allow_entry=1; with the list gone nothing else would have held it back. The conditions the entries named that are still real are checked where they apply: a sleep with ANS stopped still refuses without the storage PCIe windows. The file header, the Kconfig help and the comments that called the commit unreachable say what the driver does now. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 14 ++--- drivers/soc/apple/t6040-sleep.c | 107 ++++++++------------------------ 2 files changed, 32 insertions(+), 89 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 596bb011956dd..0cf99945d40da 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -130,20 +130,18 @@ config APPLE_T6040_SLEEP help The T6040 (M4 Pro) suspend-to-RAM controller, still a separate driver next to APPLE_SOC_SLEEP until the T6040 becomes match data - there. It registers a suspend handler that - enumerates every blocker (the untraced SoC power-down sequence, the - unrun wake path, the live state: EL2, the boot object's return path, - the CPUs that can park, the display power states the platform checks before - it sleeps) and refuses, so a suspend request prints the blockers and - enters nothing. + there. It registers a suspend handler that enumerates every + blocker in the live state (EL2, the boot object's return path, the + CPUs that can park, the display power states the platform checks before it + sleeps). Unless t6040_sleep.allow_entry=1 is given and no blocker + remains, a suspend request prints the blockers and enters nothing. It keeps the coprocessor and enclave registry for the T6040, so the drivers that leave their last stop to the sleep's commit (ANS, the PMP, the SEP) can register, and offers dry runs: with t6040_sleep.dry_run=2 every driver's firmware-driven sleep step that can be taken back runs, the CPUs park and are held, and the sleep is - refused at the commit point and resumed warm. The real commit is - compiled but unreachable while any blocker remains. + refused at the commit point and resumed warm. It owns the same registry as APPLE_SOC_SLEEP, so a kernel has one of the two. diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 7bbf67e313d31..fdabdff7d11c6 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -3,11 +3,8 @@ * Apple T6040 (M4 Pro) system sleep: refusal-first controller. * * This is the T6040 port of aurora-silicon/linux #1208 (T8140 deep sleep at - * EL2). It does not take the SoC down. #1208's T8140 driver follows the "last - * 14 ms" power-down micro-sequence measured on a J700; that - * measurement has not been made for the T6040, so the exact commit sequence (DCS - * ladder, MCC lanes, PMC voters, CLLT, DVFM, the the platform on/off power-word lists, - * the wake order) is UNKNOWN here. + * EL2). Its commit is the T6040 entry as measured on the J616s, + * which is much shorter than the T8140's (see t6040_sleep_commit). * * What it does: * @@ -33,15 +30,13 @@ * shutdown notification it sent at its suspend). * The AON watchdog's WD2 is armed from .begin to .end of a dry run 2 or 3 * (t6040_sleep.wd2_secs, 0 to leave it off). - * - The real commit (t6040_sleep_commit) is compiled but unreachable: it runs - * only when no blocker remains and t6040_sleep.allow_entry=1, and the - * missing-data blockers never clear in this kernel. + * - The real commit (t6040_sleep_commit) runs only when no blocker remains, + * t6040_sleep.allow_entry=1 is given and no dry run is asked for. * * Why this file and not a generalisation of t8140-sleep.c: the port keeps - * #1208's T8140 behaviour byte-identical, and the per-SoC match data should be - * built once the T6040 commit and wake have been measured. Until then the - * registry lives here as well, with the same contract, and a kernel builds one - * of the two controllers. + * #1208's T8140 behaviour byte-identical. Until the two share per-SoC match + * data the registry lives here as well, with the same contract, and a kernel + * builds one of the two controllers. */ #include @@ -656,20 +651,7 @@ MODULE_PARM_DESC(dry_run, static bool allow_entry; module_param(allow_entry, bool, 0444); MODULE_PARM_DESC(allow_entry, - "Reach the real commit if no blocker remains (none clears with the current unknowns)"); - -/* - * Explicit, deliberate override of the missing-data blockers (t6040_sleep_unknowns) - * for the hardware S2R bring-up: the entry/wake/apcie/dcp code has run on the J616s - * only with this override set, and a real commit can hang or lose devices on - * resume (the AON WD2 is the net). Writable at runtime so it is armed only as the - * last step after the system is quiesced; refuses still apply to the live blockers - * (display/CPU/mailbox) and to allow_entry. Leave 0 for normal operation. - */ -static bool i_know_untested; -module_param(i_know_untested, bool, 0644); -MODULE_PARM_DESC(i_know_untested, - "DANGER: ignore the missing-data blockers for a first HW S2R bring-up (needs allow_entry too)"); + "Reach the real commit if no blocker remains"); /* * The AON WD2 net armed at the commit. The platform arms 5 s for its reconfig; a longer @@ -690,7 +672,7 @@ MODULE_PARM_DESC(wd2_secs, * After the S2R timer, the platform issues a PMU "go hib (0x7)" SPMI sequence. * Whether a Linux-at-EL2 driver must issue it, or the MasterMini timer alone * arms the engine and the PMU sees it over the hardware SPMI link, is unproven. - * Off by default. The commit is unreachable regardless of this. + * Off by default. */ static bool pmgr_quiesce = true; module_param(pmgr_quiesce, bool, 0644); @@ -991,26 +973,6 @@ bool apple_sleep_commit_stops_coprocs(void) } EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); -/* - * What still keeps the commit unreachable. The SoC power-down and the - * wake return are IMPLEMENTED from the entry as measured on the J616s and - * from its wake restore, which was not measured (t6040_sleep_commit, - * t6040_sleep_return), but no step - * of a T6040 Linux sleep has run on hardware, and two subsystems still have no - * way back from a real power loss. These are facts about this kernel, not - * runtime state: they never clear here, so the commit stays unreachable until - * tests on hardware retire them. The enclave's - * sleep request and the AIC save/restore are built. - */ -static const char *const t6040_sleep_unknowns[] = { - "U1/U2: the T6040 SoC power-down is implemented from the held entry trace (clusters off, clk-mux park, PMU boot-stage, MasterMini S2R timer) but has never been run on hardware; the PMU go-hib replay's necessity is unconfirmed", - "U3: the T6040 wake return is implemented after the wake restore (complexes on, wake/CPM pwrgates, SOC-East retention, axi2af bridge re-arm, clkgen restore, S2R-ctrl clear) but has never been run; the aura mailbox hand-back is unproven on T6040", - "U4: ANS's two ST-lane PCIe link + shared PHY lower/raise is implemented (t6040_sleep_lower/raise_apcie_st, a two-lane port of #1208) but needs the apple,apcie control windows + PLL/lane tunables published by the boot object; absent here, so a real sleep with ANS stopped still refuses; the GP host's Wi-Fi/SD are left to cold-recover and no HW S2R has run", - "U5: the DCP cold restart after a real power loss is implemented (dcp_desktop_cold_restart, factored from the S4 hibernation path, wired into dcp_t6041_s2r_resume) but the display-domain lower/raise needs the apple,display-power window (absent here), DCPEXT is deferred to ext_dead, and no HW S2R has run", -}; - -#define T6040_SLEEP_DATA_BLOCKERS ARRAY_SIZE(t6040_sleep_unknowns) - /* * As in the power-state check, a sub-block is on while its TARGET is not 0. The * pipe's CPU counts as on while its ACTUAL is not 0: the engine waits for the @@ -1124,15 +1086,10 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct return blockers + disp; } -/* The total: live state plus the data this kernel is missing. Zero only if the port is complete. */ +/* The total, at the commit point: everything the live state still holds against the commit. */ static unsigned int t6040_sleep_total_blockers(struct t6040_sleep *sleep) { - unsigned int data = READ_ONCE(i_know_untested) ? 0 : T6040_SLEEP_DATA_BLOCKERS; - - if (!data && T6040_SLEEP_DATA_BLOCKERS) - pr_warn_once("t6040-sleep: i_know_untested set: IGNORING %zu never-run data blockers (U1-U5); a real commit may hang or lose devices on resume\n", - T6040_SLEEP_DATA_BLOCKERS); - return t6040_sleep_live_blockers(sleep, NULL, NULL) + data; + return t6040_sleep_live_blockers(sleep, NULL, NULL); } static void t6040_sleep_show_registry(struct seq_file *s) @@ -1164,13 +1121,7 @@ static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) t6040_sleep_show_registry(s); t6040_axi2af_show(sleep, s); - seq_puts(s, "data this kernel is missing for a T6040 commit:\n"); - for (i = 0; i < T6040_SLEEP_DATA_BLOCKERS; i++) - seq_printf(s, " %s\n", t6040_sleep_unknowns[i]); - - seq_printf(s, - "verdict: %u live blocker(s) + %zu missing-data blocker(s); no entry path in this kernel\n", - live, T6040_SLEEP_DATA_BLOCKERS); + seq_printf(s, "verdict: %u live blocker(s)\n", live); seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, sleep->power_ops ? "registered" : "not registered"); @@ -1224,14 +1175,11 @@ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) static void t6040_sleep_log_blockers(struct t6040_sleep *sleep) { const char *ops_name; - unsigned int i; pr_info("t6040-sleep: EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", is_kernel_in_hyp_mode() ? "yes" : "no", of_property_present(of_chosen, "aurora,s2r-mailbox") ? "yes" : "no", t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); - for (i = 0; i < T6040_SLEEP_DATA_BLOCKERS; i++) - pr_info("t6040-sleep: blocker: %s\n", t6040_sleep_unknowns[i]); } /* @@ -4172,10 +4120,9 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) } /* - * The real commit (t6040_sleep_commit, reached through cpu_suspend). COMPILED - * BUT UNREACHABLE: t6040_sleep_enter calls it only when no blocker remains and - * allow_entry is set, and the missing-data blockers (t6040_sleep_unknowns) - * never clear in this kernel. The SoC power-down here is the T6040 entry as + * The real commit (t6040_sleep_commit, reached through cpu_suspend): + * t6040_sleep_enter calls it only when no blocker remains and allow_entry is + * set. The SoC power-down here is the T6040 entry as * measured on the J616s, which is SHORT: unlike #1208's T8140 commit there * is NO dcs_down / mcc_stop / pwrgate / PTD / clk-mux-per-lane / CLLT / dvfm / * AMCC / ps_seq list (the platform writes none of them). The @@ -4703,8 +4650,7 @@ static int t6040_sleep_commit(unsigned long unused) * mirror of the PMGR wake restore: the three CPU complexes on, the two wake * pwrgates, SOC-East retention off (with the PARENT_MISSING poll), the axi2af * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the - * AP-global S2R control cleared, and the wake-status words read. It runs on - * the J616s once the data blockers are overridden (i_know_untested). + * AP-global S2R control cleared, and the wake-status words read. */ /* * Wake-return marks (seen on the J616s: an asynchronous SError on CPU 0 after the wake, last @@ -4954,9 +4900,8 @@ static int t6040_sleep_valid(suspend_state_t state) /* * The refusal-first gate. Default: print the blocker list and return an error * here, before any device suspends, so nothing is entered. A dry run proceeds - * and refuses at .enter. allow_entry with no blocker: the only path that would - * reach the commit, and it cannot, because the missing-data blockers never - * clear. + * and refuses at .enter. allow_entry with no blocker and no dry run: the + * path that reaches the commit. */ /* * Seq 25 stalls because ATC_n_COMMON does not come back up (port 0 or port 1 at random). @@ -5047,11 +4992,10 @@ static int t6040_sleep_begin(suspend_state_t state) struct t6040_sleep *s = t6040_sleep; unsigned int disp = 0; unsigned int live = t6040_sleep_live_blockers(s, NULL, &disp); - unsigned int data = READ_ONCE(i_know_untested) ? 0 : T6040_SLEEP_DATA_BLOCKERS; /* The display pipes are lowered during device suspend (apple-dcp .suspend), * so .begin only gates on the non-display live blockers; .enter re-checks the * full set at the commit point, by which time the display is off. */ - unsigned int begin_total = (live - disp) + data; + unsigned int begin_total = live - disp; unsigned int dry = READ_ONCE(dry_run), secs; WRITE_ONCE(t6040_sleep_dry, 0); @@ -5064,12 +5008,12 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_dcs_dyn_pg(s); if (READ_ONCE(dcs_ps_auto) & BIT(0)) t6040_dcs_ps_auto_on(s); - pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend) + %u missing-data blocker(s)\n", - live, disp, data); + pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", + live, disp); t6040_sleep_log_blockers(s); - if (!begin_total && allow_entry) { - /* Non-display blockers clear and the override/unknowns allow it: proceed + if (!begin_total && allow_entry && !dry) { + /* The non-display blockers are clear and no dry run is asked for: proceed * to device suspend; .enter holds the real commit until the display is * also off. The AON WD2 is the net from here. */ if (disp) @@ -5917,7 +5861,8 @@ static int t6040_sleep_enter(suspend_state_t state) return -EBUSY; } } - if (t6040_sleep_total_blockers(s) || !allow_entry) { + /* A dry run 1 ends here as well: its .begin told the firmware nothing. */ + if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { unsigned int on = t6040_sleep_log_groups(s); /* @@ -5935,7 +5880,7 @@ static int t6040_sleep_enter(suspend_state_t state) return -EBUSY; } - /* Unreachable with the current unknowns. #1208's order. */ + /* #1208's order. */ if (t6040_sleep_refuse(s)) { /* 0x5aGGWWWW: why, as t6040_sleep_refuse() recorded it (GG display groups on). */ t6040_mark_raw(s, 0x5a000000 | (t6040_refuse_why & 0xffffff)); From 895592934762e76f840c2456e9e3209a778092d1 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 17:52:58 +1100 Subject: [PATCH 067/269] soc: apple: t6040-sleep: leave suspend-to-idle alone With no s2idle_ops registered, the PM core calls suspend_ops->begin() and ->end() for suspend-to-idle as well. t6040_sleep_begin() treated every call as a suspend to RAM and refused it while any blocker was listed, so suspend-to-idle failed with -EOPNOTSUPP once this controller had probed. Return early from .begin for any state but PM_SUSPEND_MEM and let .end undo only what a suspend to RAM's .begin did. Also offer PM_SUSPEND_MEM in .valid only when the system was booted with t6040_sleep.allow_entry=1: without it every suspend to RAM is refused, so /sys/power/mem_sleep keeps s2idle as its default instead of a "deep" that cannot be entered. Signed-off-by: Ryan Murray --- drivers/soc/apple/t6040-sleep.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index fdabdff7d11c6..72f2ac7818397 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -4892,11 +4892,18 @@ static void t6040_sleep_return(struct t6040_sleep *s) s->returned = true; } +/* + * Offer "deep" only when the system was booted with allow_entry: without it + * every suspend to RAM is refused, so mem_sleep keeps s2idle as its default. + */ static int t6040_sleep_valid(suspend_state_t state) { - return state == PM_SUSPEND_MEM; + return state == PM_SUSPEND_MEM && READ_ONCE(allow_entry); } +/* Set by .begin for a suspend to RAM; .end only undoes what .begin did. */ +static bool t6040_sleep_mem_begun; + /* * The refusal-first gate. Default: print the blocker list and return an error * here, before any device suspends, so nothing is entered. A dry run proceeds @@ -4998,6 +5005,15 @@ static int t6040_sleep_begin(suspend_state_t state) unsigned int begin_total = live - disp; unsigned int dry = READ_ONCE(dry_run), secs; + /* + * The PM core calls .begin and .end for suspend-to-idle too when there + * are no s2idle_ops. s2idle needs nothing from this controller. + */ + t6040_sleep_mem_begun = false; + if (state != PM_SUSPEND_MEM) + return 0; + t6040_sleep_mem_begun = true; + WRITE_ONCE(t6040_sleep_dry, 0); s->returned = false; s->gp_saved[0] = s->gp_saved[1] = false; @@ -5976,6 +5992,9 @@ static void t6040_sleep_end(void) struct t6040_sleep *s = t6040_sleep; unsigned int dry = READ_ONCE(t6040_sleep_dry); + if (!t6040_sleep_mem_begun) + return; + t6040_sleep_mem_begun = false; t6040_sleep_clear_wd2(s); WRITE_ONCE(pm_bringup_dev_mark, NULL); /* Any return or refusal after .begin: the wake alarm back where the RTC driver set it. */ From cf294469d804ace1b1bb098340dc8ea90c119fce Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 14:28:32 +1100 Subject: [PATCH 068/269] soc: apple: share the sleep registry so one kernel builds both controllers The T8140 sleep driver (sleep.c) and the T6040 one (t6040-sleep.c) each defined the coprocessor and enclave registry that drivers reach through , so the two exported the same nine symbols and APPLE_T6040_SLEEP had to depend on !APPLE_SOC_SLEEP. The universal config sets APPLE_SOC_SLEEP, so it never built the T6040 controller. Move the registry into sleep-registry.c behind a hidden APPLE_SLEEP_REGISTRY that both options select. The table holds six entries and the superset of the two records. Each controller binds to the registry in its probe with two answers of its own: whether this sleep's commit stops the registered coprocessors, and whether an enclave registration is taken. Both keep their earlier rules: T8140 answers from its own state and pm_suspend_via_firmware() and takes an enclave only when its commit stops coprocessors; T6040 answers as before and takes every enclave. With no controller bound the commit stops nothing and an enclave registration is refused. The controllers bind only to their own compatibles (apple,t8140-sleep, apple,t6040-sleep), so at most one is bound and installs suspend_ops. Drop the dependency, give the registry prototypes their own guard in sleep.h so every combination compiles, and enable APPLE_T6040_SLEEP in asahi.config. Signed-off-by: Ryan Murray --- drivers/soc/apple/Kconfig | 11 +- drivers/soc/apple/Makefile | 1 + drivers/soc/apple/sleep-internal.h | 64 +++++++++ drivers/soc/apple/sleep-registry.c | 213 +++++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 193 +++----------------------- drivers/soc/apple/t6040-sleep.c | 212 +++++----------------------- include/linux/soc/apple/sleep.h | 135 +++++++++--------- 7 files changed, 401 insertions(+), 428 deletions(-) create mode 100644 drivers/soc/apple/sleep-internal.h create mode 100644 drivers/soc/apple/sleep-registry.c diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 0cf99945d40da..f567948388da9 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -92,11 +92,16 @@ config APPLE_CLUSTER_PWRGATE started and PMGR never powered on, such as the boot CPU, only clock-gates in WFI. +config APPLE_SLEEP_REGISTRY + bool + depends on ARM64 + config APPLE_SOC_SLEEP bool "Apple SoC system sleep at EL2" default APPLE_T8140_SLEEP depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) depends on HOTPLUG_CPU && SUSPEND + select APPLE_SLEEP_REGISTRY select APPLE_TUNABLE help Powers the secondary CPUs off and suspends the system to RAM on an @@ -124,7 +129,7 @@ config APPLE_T6040_SLEEP bool "Apple T6040 system sleep (refusal-first)" depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) depends on HOTPLUG_CPU && SUSPEND - depends on !APPLE_SOC_SLEEP + select APPLE_SLEEP_REGISTRY select MFD_SYSCON select APPLE_TUNABLE help @@ -143,8 +148,8 @@ config APPLE_T6040_SLEEP can be taken back runs, the CPUs park and are held, and the sleep is refused at the commit point and resumed warm. - It owns the same registry as APPLE_SOC_SLEEP, so a kernel has one - of the two. + It shares the registry with APPLE_SOC_SLEEP, and both can be built + into one kernel: each binds only to its own SoC. config APPLE_RTKIT tristate "Apple RTKit co-processor IPC protocol" diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 706a9c67a0d84..5f80ad9fcd044 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -13,6 +13,7 @@ obj-$(CONFIG_APPLE_PMGR_MISC) += apple-pmgr-misc.o obj-$(CONFIG_APPLE_CLUSTER_PWRGATE) += apple-cluster-pwrgate.o +obj-$(CONFIG_APPLE_SLEEP_REGISTRY) += sleep-registry.o obj-$(CONFIG_APPLE_SOC_SLEEP) += apple-sleep.o apple-sleep-y = sleep.o obj-$(CONFIG_APPLE_T6040_SLEEP) += t6040-sleep.o diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h new file mode 100644 index 0000000000000..233a421d8d438 --- /dev/null +++ b/drivers/soc/apple/sleep-internal.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * The coprocessor and enclave registry that the Apple SoC sleep controllers + * (sleep.c for T8140, t6040-sleep.c for T6040) share. Drivers register through + * ; the controller bound on this SoC walks the table + * at its commit. + */ +#ifndef _APPLE_SLEEP_INTERNAL_H_ +#define _APPLE_SLEEP_INTERNAL_H_ + +#include +#include + +struct device; + +/* The coprocessors the last mile stops. Each driver idles its own first. */ +#define APPLE_SLEEP_COPROCS 6 + +struct apple_sleep_coproc { + struct device *dev; + void __iomem *asc; + bool pcie_ans; + int (*last_check)(void *data); + int (*last_stop)(void *data); + void *data; + bool ready; + bool stopped; + u32 status; + int stop_ret; + /* No ASC: its last stop takes it whole, power word @ps included, before the others. */ + bool first; + u32 ps; +}; + +/* Irreversible enclave stop; nonzero is legal only if SLEEP was not sent. */ +struct apple_sleep_enclave { + struct device *dev; + int (*stop)(void *data); + void *data; +}; + +extern struct apple_sleep_coproc apple_sleep_coprocs[APPLE_SLEEP_COPROCS]; +extern struct apple_sleep_enclave apple_sleep_enclave; +extern struct mutex apple_sleep_coprocs_lock; + +struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev); + +/* + * The sleep controller bound on this SoC. Each binds only to its own SoC's + * compatible, so at most one is bound. + * + * @commit_stops: this sleep's commit stops the registered coprocessors. + * @enclave_admit: 0 to take an enclave registration, or the error to refuse + * it with. NULL takes every one. + */ +struct apple_sleep_ctl { + bool (*commit_stops)(void); + int (*enclave_admit)(void); +}; + +int apple_sleep_registry_bind(const struct apple_sleep_ctl *ctl); +void apple_sleep_registry_unbind(const struct apple_sleep_ctl *ctl); + +#endif diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c new file mode 100644 index 0000000000000..ee3c5264a8c9c --- /dev/null +++ b/drivers/soc/apple/sleep-registry.c @@ -0,0 +1,213 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple SoC sleep: the coprocessor and enclave registry + * + * A driver whose coprocessor can only be stopped once a sleep is certain + * registers it here, readies it in its own suspend and leaves the last stop to + * the commit of whichever sleep controller is bound on this SoC (sleep.c, + * t6040-sleep.c). The table lives apart from the controllers so that one + * kernel can carry all of them; each binds only to its own SoC. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "sleep-internal.h" + +struct apple_sleep_coproc apple_sleep_coprocs[APPLE_SLEEP_COPROCS]; +struct apple_sleep_enclave apple_sleep_enclave; +/* Guards the table, the enclave slot and the bound controller. */ +DEFINE_MUTEX(apple_sleep_coprocs_lock); + +static const struct apple_sleep_ctl *apple_sleep_ctl; + +int apple_sleep_registry_bind(const struct apple_sleep_ctl *ctl) +{ + guard(mutex)(&apple_sleep_coprocs_lock); + if (apple_sleep_ctl) + return -EBUSY; + WRITE_ONCE(apple_sleep_ctl, ctl); + + return 0; +} + +void apple_sleep_registry_unbind(const struct apple_sleep_ctl *ctl) +{ + guard(mutex)(&apple_sleep_coprocs_lock); + if (apple_sleep_ctl == ctl) + WRITE_ONCE(apple_sleep_ctl, NULL); +} + +struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev) +{ + int i; + + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) + if (apple_sleep_coprocs[i].dev == dev) + return &apple_sleep_coprocs[i]; + + return NULL; +} + +/* Below EL2 a monitor owns the last mile, and this code doesn't run it. */ +int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, + int (*last_stop)(void *data), void *data) +{ + struct apple_sleep_coproc *c; + + if (!is_kernel_in_hyp_mode()) + return -ENODEV; + + guard(mutex)(&apple_sleep_coprocs_lock); + if (apple_sleep_coproc_find(dev)) + return -EEXIST; + c = apple_sleep_coproc_find(NULL); + if (!c) + return -ENOSPC; + c->asc = asc; + c->pcie_ans = pcie_ans; + c->last_check = NULL; + c->last_stop = last_stop; + c->data = data; + c->ready = false; + c->stopped = false; + c->first = false; + c->ps = 0; + c->dev = dev; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_register); + +int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), + void *data) +{ + struct apple_sleep_coproc *c; + struct device_node *np; + u32 ps = 0; + int ret; + + if (!last_stop) + return -EINVAL; + np = of_parse_phandle(dev->of_node, "power-domains", 0); + if (np) { + of_property_read_u32(np, "reg", &ps); + of_node_put(np); + } + ret = apple_sleep_coproc_register(dev, NULL, false, last_stop, data); + if (ret) + return ret; + + guard(mutex)(&apple_sleep_coprocs_lock); + c = apple_sleep_coproc_find(dev); + if (c) { + c->first = true; + c->ps = ps; + } + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_register_first); + +int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)) +{ + struct apple_sleep_coproc *c; + + guard(mutex)(&apple_sleep_coprocs_lock); + c = apple_sleep_coproc_find(dev); + if (!c) + return -ENOENT; + c->last_check = last_check; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_last_check); + +/* With no sleep controller bound, nothing would ever send the request: refuse it. */ +int apple_sleep_enclave_register(struct device *dev, int (*stop)(void *data), void *data) +{ + const struct apple_sleep_ctl *ctl; + int ret; + + if (!is_kernel_in_hyp_mode()) + return -ENODEV; + + if (!stop) + return -EINVAL; + ctl = READ_ONCE(apple_sleep_ctl); + if (!ctl) + return -EOPNOTSUPP; + if (ctl->enclave_admit) { + ret = ctl->enclave_admit(); + if (ret) + return ret; + } + + guard(mutex)(&apple_sleep_coprocs_lock); + if (apple_sleep_enclave.dev) + return -EBUSY; + apple_sleep_enclave.stop = stop; + apple_sleep_enclave.data = data; + apple_sleep_enclave.dev = dev; + + return 0; +} +EXPORT_SYMBOL_GPL(apple_sleep_enclave_register); + +void apple_sleep_enclave_unregister(struct device *dev) +{ + guard(mutex)(&apple_sleep_coprocs_lock); + if (apple_sleep_enclave.dev == dev) + memset(&apple_sleep_enclave, 0, sizeof(apple_sleep_enclave)); +} +EXPORT_SYMBOL_GPL(apple_sleep_enclave_unregister); + +void apple_sleep_coproc_unregister(struct device *dev) +{ + struct apple_sleep_coproc *c; + + guard(mutex)(&apple_sleep_coprocs_lock); + c = apple_sleep_coproc_find(dev); + if (c) + memset(c, 0, sizeof(*c)); +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_unregister); + +void apple_sleep_coproc_set_ready(struct device *dev, bool ready) +{ + struct apple_sleep_coproc *c; + + guard(mutex)(&apple_sleep_coprocs_lock); + c = apple_sleep_coproc_find(dev); + if (!c) + return; + c->ready = ready; + c->stopped = false; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_ready); + +bool apple_sleep_coproc_stopped(struct device *dev) +{ + struct apple_sleep_coproc *c; + + guard(mutex)(&apple_sleep_coprocs_lock); + c = apple_sleep_coproc_find(dev); + return c && c->stopped; +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); + +bool apple_sleep_commit_stops_coprocs(void) +{ + const struct apple_sleep_ctl *ctl = READ_ONCE(apple_sleep_ctl); + + return ctl && ctl->commit_stops(); +} +EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 202c07c098947..6d0c0e6ae9d9b 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -78,6 +78,8 @@ #include #include +#include "sleep-internal.h" + /* Must match aura's struct aura_s2r_mailbox. */ #define AURA_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ @@ -288,32 +290,8 @@ struct aura_s2r_mailbox { #define APCIE_RC_READY_STAT 0x58 #define APCIE_POLL_US 50000 -/* The coprocessors the last mile stops. Each driver idles its own first. */ -#define SLEEP_COPROCS 4 - -static struct apple_sleep_coproc { - struct device *dev; - void __iomem *asc; - bool pcie_ans; - int (*last_check)(void *data); - int (*last_stop)(void *data); - void *data; - bool ready; - bool stopped; - u32 status; - int stop_ret; - /* No ASC: its last stop takes it whole, power word @ps included, before the others. */ - bool first; - u32 ps; -} apple_sleep_coprocs[SLEEP_COPROCS]; -static DEFINE_MUTEX(apple_sleep_coprocs_lock); - -/* Irreversible enclave stop; nonzero is legal only if SLEEP was not sent. */ -static struct { - struct device *dev; - int (*stop)(void *data); - void *data; -} apple_sleep_enclave; +/* The coprocessor and enclave registry is shared with the other SoCs' controllers. */ +#define SLEEP_COPROCS APPLE_SLEEP_COPROCS /* * Written with bits 0 and 63 by every core right before its last WFI, measured @@ -689,17 +667,6 @@ struct apple_sleep { static struct apple_sleep *apple_sleep; -static struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev) -{ - int i; - - for (i = 0; i < SLEEP_COPROCS; i++) - if (apple_sleep_coprocs[i].dev == dev) - return &apple_sleep_coprocs[i]; - - return NULL; -} - /* A registered coprocessor its driver has readied, whose run bit the last step clears. */ static bool apple_sleep_coproc_ready_node(const struct device_node *np) { @@ -897,148 +864,21 @@ void apple_sleep_restore_pins(void) } EXPORT_SYMBOL_GPL(apple_sleep_restore_pins); -/* Below EL2 a monitor owns the last mile, and this code doesn't run it. */ -int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, - int (*last_stop)(void *data), void *data) -{ - struct apple_sleep_coproc *c; - - if (!is_kernel_in_hyp_mode()) - return -ENODEV; - - guard(mutex)(&apple_sleep_coprocs_lock); - if (apple_sleep_coproc_find(dev)) - return -EEXIST; - c = apple_sleep_coproc_find(NULL); - if (!c) - return -ENOSPC; - c->asc = asc; - c->pcie_ans = pcie_ans; - c->last_check = NULL; - c->last_stop = last_stop; - c->data = data; - c->ready = false; - c->stopped = false; - c->first = false; - c->ps = 0; - c->dev = dev; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_register); - -int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), - void *data) -{ - struct apple_sleep_coproc *c; - struct device_node *np; - u32 ps = 0; - int ret; - - if (!last_stop) - return -EINVAL; - np = of_parse_phandle(dev->of_node, "power-domains", 0); - if (np) { - of_property_read_u32(np, "reg", &ps); - of_node_put(np); - } - ret = apple_sleep_coproc_register(dev, NULL, false, last_stop, data); - if (ret) - return ret; - - guard(mutex)(&apple_sleep_coprocs_lock); - c = apple_sleep_coproc_find(dev); - if (c) { - c->first = true; - c->ps = ps; - } - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_register_first); - -int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)) -{ - struct apple_sleep_coproc *c; - - guard(mutex)(&apple_sleep_coprocs_lock); - c = apple_sleep_coproc_find(dev); - if (!c) - return -ENOENT; - c->last_check = last_check; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_last_check); - -int apple_sleep_enclave_register(struct device *dev, int (*stop)(void *data), void *data) +/* The registry's questions, answered for this SoC's sleep. */ +static bool apple_sleep_reg_commit_stops(void) { - if (!is_kernel_in_hyp_mode()) - return -ENODEV; - - if (!stop) - return -EINVAL; - if (!apple_sleep_commit_stops_coprocs()) - return -EOPNOTSUPP; - - guard(mutex)(&apple_sleep_coprocs_lock); - if (apple_sleep_enclave.dev) - return -EBUSY; - apple_sleep_enclave.stop = stop; - apple_sleep_enclave.data = data; - apple_sleep_enclave.dev = dev; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_enclave_register); - -void apple_sleep_enclave_unregister(struct device *dev) -{ - guard(mutex)(&apple_sleep_coprocs_lock); - if (apple_sleep_enclave.dev == dev) - memset(&apple_sleep_enclave, 0, sizeof(apple_sleep_enclave)); -} -EXPORT_SYMBOL_GPL(apple_sleep_enclave_unregister); - -void apple_sleep_coproc_unregister(struct device *dev) -{ - struct apple_sleep_coproc *c; - - guard(mutex)(&apple_sleep_coprocs_lock); - c = apple_sleep_coproc_find(dev); - if (c) - memset(c, 0, sizeof(*c)); -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_unregister); - -void apple_sleep_coproc_set_ready(struct device *dev, bool ready) -{ - struct apple_sleep_coproc *c; - - guard(mutex)(&apple_sleep_coprocs_lock); - c = apple_sleep_coproc_find(dev); - if (!c) - return; - c->ready = ready; - c->stopped = false; + return READ_ONCE(apple_sleep) && pm_suspend_via_firmware(); } -EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_ready); -bool apple_sleep_coproc_stopped(struct device *dev) +static int apple_sleep_reg_enclave_admit(void) { - struct apple_sleep_coproc *c; - - guard(mutex)(&apple_sleep_coprocs_lock); - c = apple_sleep_coproc_find(dev); - return c && c->stopped; + return apple_sleep_reg_commit_stops() ? 0 : -EOPNOTSUPP; } -EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); -bool apple_sleep_commit_stops_coprocs(void) -{ - return READ_ONCE(apple_sleep) && pm_suspend_via_firmware(); -} -EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); +static const struct apple_sleep_ctl apple_sleep_reg_ctl = { + .commit_stops = apple_sleep_reg_commit_stops, + .enclave_admit = apple_sleep_reg_enclave_admit, +}; /* * The last thing that can fail on the way to the commit: no CPU but this one is up and @@ -4146,9 +3986,14 @@ static int apple_sleep_probe(struct platform_device *pdev) platform_set_drvdata(pdev, s); - ret = spin_table_set_power_ops(&sleep_cpu_power_ops); + ret = apple_sleep_registry_bind(&apple_sleep_reg_ctl); if (ret) + return dev_err_probe(dev, ret, "another sleep controller is bound\n"); + ret = spin_table_set_power_ops(&sleep_cpu_power_ops); + if (ret) { + apple_sleep_registry_unbind(&apple_sleep_reg_ctl); return dev_err_probe(dev, ret, "cannot control CPU power\n"); + } /* Last: the exported helpers take it as a probed driver, and a failure frees s. */ apple_sleep = s; diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 72f2ac7818397..b6271b891113b 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -33,10 +33,12 @@ * - The real commit (t6040_sleep_commit) runs only when no blocker remains, * t6040_sleep.allow_entry=1 is given and no dry run is asked for. * - * Why this file and not a generalisation of t8140-sleep.c: the port keeps - * #1208's T8140 behaviour byte-identical. Until the two share per-SoC match - * data the registry lives here as well, with the same contract, and a kernel - * builds one of the two controllers. + * Why this file and not a generalisation of sleep.c: the port keeps + * the T8140 behaviour byte-identical, and the per-SoC match data should be + * built once the T6040 commit and wake are known from a trace. Until then it is + * a controller of its own. The coprocessor and enclave registry is shared + * (sleep-registry.c), so one kernel carries both controllers, and each binds + * only to its own SoC. */ #include @@ -79,6 +81,8 @@ #include #include +#include "sleep-internal.h" + #define PS_TARGET GENMASK(3, 0) #define PS_ACTUAL GENMASK(7, 4) #define PS_FLAGS GENMASK(9, 8) @@ -804,174 +808,22 @@ bool apple_sleep_last_steps(void) EXPORT_SYMBOL_GPL(apple_sleep_last_steps); /* - * The coprocessor and enclave registry, with #1208's contract (sleep.h). A - * registered coprocessor that isn't ready refuses the sleep; a ready one keeps - * running until the commit's last stop, after every refusal, so a refused sleep - * finds it as it was. + * The coprocessor and enclave registry (sleep-registry.c), shared with the T8140 + * controller, same contract (sleep.h). A registered coprocessor that isn't ready + * refuses the sleep; a ready one keeps running until the commit's last stop, + * after every refusal, so a refused sleep finds it as it was. */ -#define SLEEP_COPROCS 6 - -static struct t6040_sleep_coproc { - struct device *dev; - void __iomem *asc; - bool pcie_ans; - int (*last_check)(void *data); - int (*last_stop)(void *data); - void *data; - bool ready; - bool stopped; - u32 status; - /* No ASC: its last stop takes it whole, power word included, before the others. */ - bool first; -} t6040_sleep_coprocs[SLEEP_COPROCS]; -static DEFINE_MUTEX(t6040_sleep_coprocs_lock); - -/* The enclave's sleep request, left to the last refusal. Under t6040_sleep_coprocs_lock. */ -static struct { - struct device *dev; - int (*tell)(void *data); - void *data; -} t6040_sleep_enclave; - -static struct t6040_sleep_coproc *t6040_sleep_coproc_find(struct device *dev) -{ - int i; - - for (i = 0; i < SLEEP_COPROCS; i++) - if (t6040_sleep_coprocs[i].dev == dev) - return &t6040_sleep_coprocs[i]; - - return NULL; -} - -/* Below EL2 a monitor owns the last mile, and this code doesn't run it. */ -int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, - int (*last_stop)(void *data), void *data) -{ - struct t6040_sleep_coproc *c; - - if (!is_kernel_in_hyp_mode()) - return -ENODEV; - - guard(mutex)(&t6040_sleep_coprocs_lock); - if (t6040_sleep_coproc_find(dev)) - return -EEXIST; - c = t6040_sleep_coproc_find(NULL); - if (!c) - return -ENOSPC; - c->asc = asc; - c->pcie_ans = pcie_ans; - c->last_check = NULL; - c->last_stop = last_stop; - c->data = data; - c->ready = false; - c->stopped = false; - c->first = false; - c->dev = dev; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_register); - -int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), - void *data) -{ - struct t6040_sleep_coproc *c; - int ret; - - if (!last_stop) - return -EINVAL; - ret = apple_sleep_coproc_register(dev, NULL, false, last_stop, data); - if (ret) - return ret; - - guard(mutex)(&t6040_sleep_coprocs_lock); - c = t6040_sleep_coproc_find(dev); - if (c) - c->first = true; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_register_first); - -int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)) -{ - struct t6040_sleep_coproc *c; - - guard(mutex)(&t6040_sleep_coprocs_lock); - c = t6040_sleep_coproc_find(dev); - if (!c) - return -ENOENT; - c->last_check = last_check; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_last_check); - -int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), void *data) -{ - if (!is_kernel_in_hyp_mode()) - return -ENODEV; - - guard(mutex)(&t6040_sleep_coprocs_lock); - if (t6040_sleep_enclave.dev) - return -EBUSY; - t6040_sleep_enclave.tell = tell; - t6040_sleep_enclave.data = data; - t6040_sleep_enclave.dev = dev; - - return 0; -} -EXPORT_SYMBOL_GPL(apple_sleep_enclave_register); - -void apple_sleep_enclave_unregister(struct device *dev) -{ - guard(mutex)(&t6040_sleep_coprocs_lock); - if (t6040_sleep_enclave.dev == dev) - memset(&t6040_sleep_enclave, 0, sizeof(t6040_sleep_enclave)); -} -EXPORT_SYMBOL_GPL(apple_sleep_enclave_unregister); - -void apple_sleep_coproc_unregister(struct device *dev) -{ - struct t6040_sleep_coproc *c; - - guard(mutex)(&t6040_sleep_coprocs_lock); - c = t6040_sleep_coproc_find(dev); - if (c) - memset(c, 0, sizeof(*c)); -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_unregister); - -void apple_sleep_coproc_set_ready(struct device *dev, bool ready) -{ - struct t6040_sleep_coproc *c; - - guard(mutex)(&t6040_sleep_coprocs_lock); - c = t6040_sleep_coproc_find(dev); - if (!c) - return; - c->ready = ready; - c->stopped = false; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_ready); - -bool apple_sleep_coproc_stopped(struct device *dev) -{ - struct t6040_sleep_coproc *c; - - guard(mutex)(&t6040_sleep_coprocs_lock); - c = t6040_sleep_coproc_find(dev); - return c && c->stopped; -} -EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); +#define SLEEP_COPROCS APPLE_SLEEP_COPROCS /* A real entry: its commit stops the registered coprocessors; a dry run's never does. */ -bool apple_sleep_commit_stops_coprocs(void) +static bool t6040_sleep_reg_commit_stops(void) { return READ_ONCE(t6040_sleep) && is_kernel_in_hyp_mode() && apple_sleep_power_loss(); } -EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); + +static const struct apple_sleep_ctl t6040_sleep_reg_ctl = { + .commit_stops = t6040_sleep_reg_commit_stops, +}; /* * As in the power-state check, a sub-block is on while its TARGET is not 0. The @@ -1096,10 +948,10 @@ static void t6040_sleep_show_registry(struct seq_file *s) { int i; - guard(mutex)(&t6040_sleep_coprocs_lock); + guard(mutex)(&apple_sleep_coprocs_lock); seq_puts(s, "coprocessors registered for the commit's last stop:\n"); for (i = 0; i < SLEEP_COPROCS; i++) { - struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; if (!c->dev) continue; @@ -1108,7 +960,7 @@ static void t6040_sleep_show_registry(struct seq_file *s) c->ready ? ", ready" : ""); } seq_printf(s, "enclave sleep request left for the last refusal: %s\n", - t6040_sleep_enclave.dev ? dev_name(t6040_sleep_enclave.dev) : "none"); + apple_sleep_enclave.dev ? dev_name(apple_sleep_enclave.dev) : "none"); } static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s); @@ -1649,7 +1501,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) int i; for (i = 0; i < SLEEP_COPROCS; i++) { - struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; if (!c->dev) continue; @@ -1695,7 +1547,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) pr_err("t6040-sleep: refused: sep isn't bound (state %#x)\n", val); refused++; why |= BIT(3); - } else if (!t6040_sleep_enclave.tell) { + } else if (!apple_sleep_enclave.stop) { pr_err("t6040-sleep: refused: sep's sleep request isn't left for the last refusal\n"); refused++; why |= BIT(3); @@ -1735,7 +1587,7 @@ static int t6040_sleep_coprocs_check(void) int i, ret; for (i = 0; i < SLEEP_COPROCS; i++) { - struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; if (!c->dev || !c->ready || c->stopped || !c->last_check) continue; @@ -1755,9 +1607,9 @@ static int t6040_sleep_enclave_tell(void) { int ret; - if (!t6040_sleep_enclave.tell) + if (!apple_sleep_enclave.stop) return 0; - ret = t6040_sleep_enclave.tell(t6040_sleep_enclave.data); + ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); if (ret) pr_err("t6040-sleep: refused: the enclave didn't take the sleep request: %d\n", ret); @@ -2286,8 +2138,8 @@ static void t6040_mtp_island_back(struct t6040_sleep *s, const u32 *saved) writel(saved[n], s->mtp_ps + 8 * n); } -/* As #1208's, measured on ANS: status and control read, then the run bit cleared twice. */ -static void t6040_sleep_coproc_run_off(struct t6040_sleep_coproc *c) +/* As on T8140, measured on ANS: status and control read, then the run bit cleared twice. */ +static void t6040_sleep_coproc_run_off(struct apple_sleep_coproc *c) { unsigned int keep = READ_ONCE(coproc_keep_run); @@ -2317,7 +2169,7 @@ static void t6040_sleep_coprocs_stop(bool first) int i; for (i = 0; i < SLEEP_COPROCS; i++) { - struct t6040_sleep_coproc *c = &t6040_sleep_coprocs[i]; + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; if (!c->dev || !c->ready || c->first != first || c->stopped) continue; @@ -5161,7 +5013,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) spin_table_unhold_parked(); pr_info("t6040-sleep: dry run: refused before the enclave's sleep request (%s), the coprocessors' last stops and the SoC power-down\n", - t6040_sleep_enclave.dev ? "left by sep" : "none left"); + apple_sleep_enclave.dev ? "left by sep" : "none left"); return -EBUSY; } @@ -6502,6 +6354,10 @@ static int t6040_sleep_probe(struct platform_device *pdev) t6040_sleep_map_gp(sleep); t6040_ed_map(&pdev->dev); + ret = apple_sleep_registry_bind(&t6040_sleep_reg_ctl); + if (ret) + return dev_err_probe(&pdev->dev, ret, "another sleep controller is bound\n"); + platform_set_drvdata(pdev, sleep); t6040_sleep = sleep; diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index ca4a066468a5f..30352f1cad0c8 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -15,7 +15,12 @@ struct device; struct device_node; -#if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) +/* + * The coprocessor and enclave registry, shared by the sleep controllers + * (drivers/soc/apple/sleep-registry.c). The controller bound on this SoC + * answers apple_sleep_commit_stops_coprocs() and runs the last stops. + */ +#if IS_ENABLED(CONFIG_APPLE_SLEEP_REGISTRY) /* * A coprocessor that can't be stopped until a sleep is certain, and can't be * restarted without one. While one is registered and not ready, a suspend is @@ -36,12 +41,6 @@ int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void void *data); void apple_sleep_coproc_unregister(struct device *dev); -/* - * During a suspend: a driver left something the DRAM-kept wake would stall on, so this - * sleep takes the deep path. False if no sleep driver can do that. - */ -bool apple_sleep_request_deep_once(void); - /* * For a registered coprocessor: the last of its stop that may still refuse the sleep, run * with interrupts off before any coprocessor's @last_stop. A negative return refuses, and the @@ -70,6 +69,61 @@ bool apple_sleep_coproc_stopped(struct device *dev); * registered refuses the sleep: stopped any earlier, a later refusal would strand it. */ bool apple_sleep_commit_stops_coprocs(void); +#else +static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, + bool pcie_ans, int (*last_stop)(void *data), + void *data) +{ + return -EOPNOTSUPP; +} + +static inline int apple_sleep_coproc_register_first(struct device *dev, + int (*last_stop)(void *data), void *data) +{ + return -EOPNOTSUPP; +} + +static inline void apple_sleep_coproc_unregister(struct device *dev) +{ +} + +static inline int apple_sleep_coproc_set_last_check(struct device *dev, + int (*last_check)(void *data)) +{ + return -EOPNOTSUPP; +} + +static inline int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), + void *data) +{ + return -EOPNOTSUPP; +} + +static inline void apple_sleep_enclave_unregister(struct device *dev) +{ +} + +static inline void apple_sleep_coproc_set_ready(struct device *dev, bool ready) +{ +} + +static inline bool apple_sleep_coproc_stopped(struct device *dev) +{ + return false; +} + +static inline bool apple_sleep_commit_stops_coprocs(void) +{ + return false; +} +#endif + +#if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) +/* + * During a suspend: a driver left something the DRAM-kept wake would stall on, so this + * sleep takes the deep path. False if no sleep driver can do that. + */ +bool apple_sleep_request_deep_once(void); /* * The USB host sleeps in place for this sleep, as macOS's does: its always-on domain stays @@ -172,72 +226,7 @@ void apple_sleep_restore_pins(void); */ int apple_sleep_engine_reset(struct device *dev); #else -#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) -/* - * The T6040 controller (t6040-sleep.c) keeps the coprocessor and enclave registry above, - * with the same contract. The rest of the T8140 calls have no T6040 counterpart yet. - */ -int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, - int (*last_stop)(void *data), void *data); -int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), - void *data); -void apple_sleep_coproc_unregister(struct device *dev); -int apple_sleep_coproc_set_last_check(struct device *dev, int (*last_check)(void *data)); -int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), void *data); -void apple_sleep_enclave_unregister(struct device *dev); -void apple_sleep_coproc_set_ready(struct device *dev, bool ready); -bool apple_sleep_coproc_stopped(struct device *dev); -bool apple_sleep_commit_stops_coprocs(void); -#else -static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, - bool pcie_ans, int (*last_stop)(void *data), - void *data) -{ - return -EOPNOTSUPP; -} - -static inline int apple_sleep_coproc_register_first(struct device *dev, - int (*last_stop)(void *data), void *data) -{ - return -EOPNOTSUPP; -} - -static inline void apple_sleep_coproc_unregister(struct device *dev) -{ -} - -static inline int apple_sleep_coproc_set_last_check(struct device *dev, - int (*last_check)(void *data)) -{ - return -EOPNOTSUPP; -} - -static inline int apple_sleep_enclave_register(struct device *dev, int (*tell)(void *data), - void *data) -{ - return -EOPNOTSUPP; -} - -static inline void apple_sleep_enclave_unregister(struct device *dev) -{ -} - -static inline void apple_sleep_coproc_set_ready(struct device *dev, bool ready) -{ -} - -static inline bool apple_sleep_coproc_stopped(struct device *dev) -{ - return false; -} - -static inline bool apple_sleep_commit_stops_coprocs(void) -{ - return false; -} -#endif - -/* The T6040 controller can't send a sleep deep either: false, as with no sleep driver. */ +/* Without the T8140 controller no sleep goes deep: false, as with no sleep driver. */ static inline bool apple_sleep_request_deep_once(void) { return false; From 428de217169156192cb0fb9c90a2bfde8cad7505 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 15:01:29 +1100 Subject: [PATCH 069/269] soc: apple: t6040-sleep: depend on a built-in SMC driver The controller is built in and writes the SMC's pmVC key through apple_smc_read_u32() and apple_smc_write_u32(). With MFD_MACSMC off or built as a module the kernel fails to link. Require MFD_MACSMC=y. Signed-off-by: Ryan Murray --- drivers/soc/apple/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index f567948388da9..a92a4b2c00235 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -129,6 +129,7 @@ config APPLE_T6040_SLEEP bool "Apple T6040 system sleep (refusal-first)" depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) depends on HOTPLUG_CPU && SUSPEND + depends on MFD_MACSMC=y select APPLE_SLEEP_REGISTRY select MFD_SYSCON select APPLE_TUNABLE From a7ce91c18d243891688b1cd4d8f48f44a979bb07 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 12 Nov 2026 18:14:11 +1100 Subject: [PATCH 070/269] soc: apple: t6040-sleep: name the T8140 driver instead of its pull request Comments in the T6040 sleep code, the display driver and the T6040 device tree pointed at the T8140 sleep driver by a pull request number. Name the driver (sleep.c) or the T8140 instead, and drop the "currently unreachable" note on the sleep controller node. No functional change. Signed-off-by: Ryan Murray --- drivers/soc/apple/t6040-sleep.c | 38 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index b6271b891113b..d8978142035c4 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -2,15 +2,15 @@ /* * Apple T6040 (M4 Pro) system sleep: refusal-first controller. * - * This is the T6040 port of aurora-silicon/linux #1208 (T8140 deep sleep at - * EL2). Its commit is the T6040 entry as measured on the J616s, - * which is much shorter than the T8140's (see t6040_sleep_commit). + * This is the T6040 port of the T8140 deep sleep at EL2 (sleep.c). Its commit + * is the T6040 entry as measured on the J616s, which is much + * shorter than the T8140's (see t6040_sleep_commit). * * What it does: * * - It keeps the debugfs preflight of the display power states the platform checks * before it sleeps (device-check-ps-b4-sleep). - * - It keeps #1208's coprocessor and enclave registry (sleep.h) for the T6040: + * - It uses the coprocessor and enclave registry it shares with T8140 (sleep.h): * a driver that can only stop its coprocessor once the sleep is certain * (ANS, the PMP, the SEP's sleep request) registers here, readies it in its * own suspend and leaves the last stop to the commit, after every refusal. @@ -98,7 +98,7 @@ * offsets, same bank as ps_ans); the control windows (axi2af/cio3pll/clkgen/ * phy-block/phy-ip/rc) and the PLL/lane tunables are NOT in this SoC's static * description and are published by the boot object (aura) under the apple,apcie - * node, matching aurora #1208. The block-internal offsets below are T8140's: + * node, in the same shape as on T8140. The block-internal offsets below are T8140's: * the same Apple PCIe IP, expected identical. */ #define PS_ANS 0x370 /* SOC-East: ps_ans */ @@ -304,7 +304,7 @@ static const struct { /* * The display coprocessors' ASC CPU_CONTROL (+0x44): DCP, DCPEXT0, DCPEXT1. Upstream - * T8140 (#1208, working S2R) writes it 0 twice right before the display CPU word goes + * T8140 (working S2R) writes it 0 twice right before the display CPU word goes * down; RUN and bit5 alone, cleared at the device suspend, leave the word refusing writes. */ static const u64 t6040_dcp_asc_ctl_pa[3] = { 0x402e00044ULL, 0x3a2e00044ULL, 0x602e00044ULL }; @@ -347,7 +347,7 @@ static const struct { /* * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000). Same shape as - * #1208's t8140_pmu_write: the reply vector is masked, the command and its data + * T8140's t8140_pmu_write: the reply vector is masked, the command and its data * are written to +0x4, the reply polled and acknowledged. Measured on the J616s *: the boot stage is set to 0x50 by an extended write whose command * word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 @@ -452,7 +452,7 @@ static const struct { #define WDT_TICKS_PER_S 24000000 /* At the commit, as the reconfig. */ #define WDT_SLEEP_SECS 5 -/* From .begin to .end: the device suspend and resume, as #1208 does. */ +/* From .begin to .end: the device suspend and resume, as on T8140. */ #define WDT_SUSPEND_SECS 120 /* ALARM is 32 bits at 24 MHz. */ #define WDT_MAX_SECS 178U @@ -1562,7 +1562,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) /* * ANS would be stopped, but its ST lanes have no way back without the - * apple,apcie control windows + PLL/lane tunables. #1208 keeps + * apple,apcie control windows + PLL/lane tunables. The T8140 driver keeps * suspend-to-idle the default in this state; here the deep sleep refuses. */ if (ans_idled && !s->apcie_st.valid) { @@ -2287,7 +2287,7 @@ static const struct { }; /* - * As the platform writes the PMU over SPMI (mirror of #1208's t8140_pmu_write): the + * As the platform writes the PMU over SPMI (mirror of T8140's t8140_pmu_write): the * reply vector masked, the command and value written to +0x4, the reply polled * and acknowledged, so none is left raised. */ @@ -2916,7 +2916,7 @@ static void t6040_wake_restore(struct t6040_sleep *s) /* * ANS's storage-apcie power words (the two ST lanes + ANS) live in SOC-East. - * These helpers drive one power word in a given bank, as #1208's t8140_ps_* + * These helpers drive one power word in a given bank, as T8140's t8140_ps_* * do in its single PMGR bank: lower is AUTO_ENABLE off then target 0; raise is * the target, then AUTO_ENABLE once ACTUAL reads on. Writing PS_FLAGS clears * them. Separate from t6040_sleep_ps_on(), which reads a cached value. @@ -3032,7 +3032,7 @@ static void t6040_apcie_clk_off(struct t6040_apcie_st *a) } /* - * ENTRY: lower ANS's storage link, the two-lane adaptation of #1208's + * ENTRY: lower ANS's storage link, the two-lane adaptation of * t8140_sleep_lower_apcie(). Called from the commit's lower_io only once ANS * has been stopped by its driver (pcie_ans_stopped). The PHY/clkgen/axi are * shared across the lanes; only the two sys_st/st domains are per-lane. @@ -3050,7 +3050,7 @@ static void t6040_apcie_clk_off(struct t6040_apcie_st *a) * clock-gated. APCIE_PHY_SW's parents are APCIE_SYS_ST0/1 (ADT): with them already off * the engine waits on PARENT_MISSING for good (on the J616s: MCPS stuck at 0x502280398). * apcie_st_ps=0 leaves the power words to the engine (the PHY, clock and AXI quiesce - * still run); 1 lowers them here as #1208 does on T8140. + * still run); 1 lowers them here as the T8140 driver does. */ static bool apcie_st_ps; module_param(apcie_st_ps, bool, 0644); @@ -3463,7 +3463,7 @@ MODULE_PARM_DESC(dcs_rec, "Bring-up: record the DCS power-gate state at PMU 0xa2 /* * D2S still stalls with dynamic power gating on and the AOP/PMP/MTP ruled out. - * #1208's T8140 commit stops each memory channel right before its gates at + * The T8140 commit stops each memory channel right before its gates at * about -18 ms (t8140_sleep_mcc_stop): at DCS +0x580 ("mcc0..2" = 0x2_2040_0580 + * ch * 0x20_0000, the DCS slices' spacing) the enables word +0x4 cleared one bit at a time, * then lanes 0..3 strobed and cleared on +0x8 (mask word +0xc), two lanes per channel per @@ -3803,12 +3803,12 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) s->ans_bridge_quiesced = t6040_ans_bridge_quiesce(s); if (READ_ONCE(apcie_st_ps)) t6040_ps_lower(ps, PS_ANS); - /* The caller polls PS_ANS off, as #1208 does. */ + /* The caller polls PS_ANS off, as on T8140. */ } /* * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of - * #1208's t8140_sleep_raise_apcie(). Runs atomic on the waking core in + * t8140_sleep_raise_apcie(). Runs atomic on the waking core in * t6040_sleep_return(), AFTER t6040_axi2af_rearm() (the ANS/apcie_sys fabric * bridges must be re-armed before link traffic) and BEFORE control returns to * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. @@ -3975,7 +3975,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) * The real commit (t6040_sleep_commit, reached through cpu_suspend): * t6040_sleep_enter calls it only when no blocker remains and allow_entry is * set. The SoC power-down here is the T6040 entry as - * measured on the J616s, which is SHORT: unlike #1208's T8140 commit there + * measured on the J616s, which is SHORT: unlike the T8140 commit there * is NO dcs_down / mcc_stop / pwrgate / PTD / clk-mux-per-lane / CLLT / dvfm / * AMCC / ps_seq list (the platform writes none of them). The * order is the, as measured: WD0 off, PMU boot-stage 0x50 @@ -5748,7 +5748,7 @@ static int t6040_sleep_enter(suspend_state_t state) return -EBUSY; } - /* #1208's order. */ + /* The T8140 driver's order. */ if (t6040_sleep_refuse(s)) { /* 0x5aGGWWWW: why, as t6040_sleep_refuse() recorded it (GG display groups on). */ t6040_mark_raw(s, 0x5a000000 | (t6040_refuse_why & 0xffffff)); @@ -6001,7 +6001,7 @@ static void __iomem *t6040_apcie_block(struct t6040_sleep *s, struct device_node * the boot object publishes from the ADT. Our static tree has none * of these, so until aura fills the node @valid stays false and a real sleep * with ANS stopped refuses (t6040_sleep_refuse). The reg-names and tunable - * property names match aurora #1208's apple,apcie shape exactly, so the same + * property names match the T8140 apple,apcie shape exactly, so the same * loader fills both SoCs. The GP PCIe host (pcie@1cb0000000, Wi-Fi + SD) is a * different node and is left to cold-recover. */ From ccffb0f67994dcf5fbd1bd0fa66c0c53cb9b371c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 14:34:45 +0100 Subject: [PATCH 071/269] tools: asahi: sep-suspend-errors: take the enclave registration from the registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The enclave registration and the question whether a commit stops the coprocessors live in sleep-registry.c now, and sleep.c answers them for the T8140 through the controller it binds. Read the three functions from the registry, the two answers from sleep.c, and bind them in the fixture the way the T8140 controller's probe does. Signed-off-by: František Balázsy --- tools/asahi/sep-suspend-errors-tests.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index b5f139dd13c34..6ed77fa0dbcfd 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -141,6 +141,14 @@ def replace_once(source, before, after): static struct apple_mbox mbox; static int apple_sleep_coprocs_lock; static void *apple_sleep; +struct apple_sleep_ctl { bool (*commit_stops)(void); int (*enclave_admit)(void); }; +static bool apple_sleep_reg_commit_stops(void); +static int apple_sleep_reg_enclave_admit(void); +/* The T8140 controller's answers, bound to the registry as its probe binds them. */ +static const struct apple_sleep_ctl apple_sleep_reg_ctl = { + apple_sleep_reg_commit_stops, apple_sleep_reg_enclave_admit, +}; +static const struct apple_sleep_ctl *apple_sleep_ctl = &apple_sleep_reg_ctl; static struct { struct device *dev; int (*stop)(void *); void *data; } apple_sleep_enclave; @@ -665,19 +673,21 @@ def sources(source, contracts, mutation): shim = (source / 'drivers/soc/apple/pm_shim.c').read_text() mailbox = (source / 'drivers/soc/apple/mailbox.c').read_text() sleep = (source / 'drivers/soc/apple/sleep.c').read_text() + registry = (source / 'drivers/soc/apple/sleep-registry.c').read_text() iopoll = (contracts / 'include/linux/iopoll.h').read_text() names = { 'shim': ['sep_pm_wait_gated', 'sep_pm_clear_dev_disable', 'sep_pm_dart_restore', 'sep_pm_commit_rx', 'sep_pm_commit_take_rx', 'sep_pm_commit_stop', 'sep_pm_commit_prepare', 'sep_pm_commit_finish', 'sep_pm_suspend_noirq'], 'mailbox': ['apple_mbox_poll_one_supported', 'apple_mbox_poll_one'], - 'sleep': ['apple_sleep_commit_stops_coprocs', 'apple_sleep_enclave_register', - 'apple_sleep_enclave_unregister'], + 'sleep': ['apple_sleep_reg_commit_stops', 'apple_sleep_reg_enclave_admit'], + 'registry': ['apple_sleep_commit_stops_coprocs', 'apple_sleep_enclave_register', + 'apple_sleep_enclave_unregister'], 'pm': ['leave_for_commit', 'commit_finish', 'clear_dev_disable', 'dart_restore'], 'sep': ['sleep_enter', 'sleep_leave_for_commit', 'sleep_hold_rx', 'sleep_release_rx', 'sleep_exit', 'sleep_kick'], } - texts = dict(shim=shim, mailbox=mailbox, sleep=sleep, pm=pm, sep=sep) + texts = dict(shim=shim, mailbox=mailbox, sleep=sleep, registry=registry, pm=pm, sep=sep) bodies = {n: function(texts[k], n) for k, group in names.items() for n in group} if mutation: name, before, after = MUTATIONS[mutation] @@ -691,7 +701,7 @@ def sources(source, contracts, mutation): c += '\n' + shim[start:shim.index('} sep_pm_commit;', start) + len('} sep_pm_commit;')] # Use the actual provider helper, with only removal/capability dependency state # injected by send for the post-send negative-poll scenarios. - c += '\n' + '\n'.join(bodies[n] for group in ['mailbox', 'sleep', 'shim'] for n in names[group]) + c += '\n' + '\n'.join(bodies[n] for group in ['mailbox', 'sleep', 'registry', 'shim'] for n in names[group]) c += '\n' + C_CONTROLS gate_start = pm.index('#[derive(Clone, Copy, Debug, PartialEq, Eq)]') gate_end = pm.index('/// Stages `request`', gate_start) From ef59ae7f97eea3b9915dbfd1ed63e7837ad602f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:22:39 +0100 Subject: [PATCH 072/269] soc: apple: t6040-sleep: remove the experiments that are off by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The controller carried the knobs of every experiment the bring-up of the T6040 sleep went through. These were off by default and no sleep on the J616s runs them: the PMC's DCS floor vote, the PTD request withdrawal, the hold before the S2R timer and the probes after it, the replay of the PMU's sleep sequence, the observer core, keeping a coprocessor's run bit, the MTP and the AOP island power-down, the cluster CPM configuration, the PMC probe reads, masking every wake source, lowering the storage lanes and ANS by hand, the memory channel stop, the AOP halt, the records and logs of the ATC hand-over blocks and the DCS state, the display pipe's CPU word and its PMC votes, the DCPEXT power-down, the display probe and the axi2af refusal gate. gated_cpu_target_only had no reader at all. Remove the parameters and the code only they reached, with its register windows, tables and constants. What the default configuration writes, and in which order, is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 1304 +------------------------------ 1 file changed, 9 insertions(+), 1295 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index d8978142035c4..729eeb3808b53 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -247,9 +247,8 @@ * - features off: cpu-fixed-freq-pll-relock, BIT42 := 0 in the E and P0 * ACC words; then the TVMs disp2, afr, dcs (two), gfx (two), disp, soc: control * BIT0 := 0, status polled until BIT1 clears; - * - the PMC performance state quiesce: the AP voter agent (3) drops its - * DCS floor vote, PMC_DVFM_IF + 3 * 0x4000 [3:0] := 0, pending +4 BIT3 polled - * (the SOC domain has no AP voter map entry here: no write). + * The platform then drops the AP voter's DCS floor vote in the PMC. That step is left out: the + * first access to that word hung the bus on the J616s. * The platform polls the APSC and TVM words without a timeout; here every poll is bounded * and a timeout sets a bit of the commit's 0x5d mark. */ @@ -274,7 +273,6 @@ static const struct { }; #define TVM_ENABLE BIT(0) #define TVM_BUSY BIT(1) -#define T6040_PMC_AP_FLOOR_PA 0x50480c000ULL /* * As the platform does (measured on the J616s where marked), a display pipe's PMC votes go @@ -283,31 +281,8 @@ static const struct { * BIT(p ? p + 2 : 0); the cluster rail 0x5048a9c18 BIT2 (DISP_INT) or BIT3 (DISP_EXT, last * pipe only) with the ack at 0x5048a9c1c (measured). p: 0 internal, 1 DCPEXT0, 2 DCPEXT1. */ -#define T6040_PMC_DVMR_PA 0x5048a4000ULL -#define PMC_DVMR_ACK 0x20 #define T6040_PMC_BWR_PA 0x5048ac020ULL #define PMC_BOOST 0x8 -#define PMC_DCP_COUNT 0x0 /* 0x5048a9c14 */ -#define PMC_RAIL 0x4 /* 0x5048a9c18 */ -#define PMC_RAIL_ACK 0x8 /* 0x5048a9c1c */ -#define PMC_RAIL_INT BIT(2) -#define PMC_RAIL_EXT BIT(3) -#define PMC_TIMEOUT_US 8000 - -/* - * The DCPEXT power words in SOC (soc_ps), CPU then FE then SYS, the values - * (measured: CPU 0x1f0003ff -> 0x0f0000ff -> 0x0f0003f0, FE 0x3ff -> 0x3f0, SYS -> 0x3f0); the - * SYS word after its axi2af bridge bits and its pwrgate pair (A := 3, B := 0) in the - * SOC pwrgate block's second page. - */ -#define T6040_DISPEXT_PWRGATE_PA 0x50282d000ULL - -/* - * The display coprocessors' ASC CPU_CONTROL (+0x44): DCP, DCPEXT0, DCPEXT1. Upstream - * T8140 (working S2R) writes it 0 twice right before the display CPU word goes - * down; RUN and bit5 alone, cleared at the device suspend, leave the word refusing writes. - */ -static const u64 t6040_dcp_asc_ctl_pa[3] = { 0x402e00044ULL, 0x3a2e00044ULL, 0x602e00044ULL }; /* * The PMP's PTD (pmp-report 0x5003c0000, T6041 layout as drivers/pmdomain/apple/ @@ -318,25 +293,12 @@ static const u64 t6040_dcp_asc_ctl_pa[3] = { 0x402e00044ULL, 0x3a2e00044ULL, 0x6 */ #define T6040_PTD_PA 0x5003c0000ULL #define PTD_REQ_READ 0x1e00 -#define PTD_REQ_WRITE 0x10f00 #define PTD_ACK 0x1e80 -#define PTD_DISP_INT 16 -static const struct { - u32 sys, fe, cpu, pg_a, pg_b; - int axi2af; -} t6040_dispext[2] = { - { 0x340, 0x348, 0x350, 0x234, 0x258, 5 }, - { 0x490, 0x498, 0x4a0, 0x6b4, 0x6d8, 6 }, -}; -#define PMC_FLOOR_DCS GENMASK(3, 0) -#define PMC_PENDING 0x4 -#define PMC_PENDING_AP BIT(3) #define QUIESCE_TIMEOUT_US 2000 /* The 0x5d mark's timeout bits. */ #define QTO_APSC(c) BIT(c) #define QTO_TVM(i) BIT(3 + (i)) -#define QTO_PMC BIT(11) #define QTO_CLUSTER(i) BIT(12 + (i) - CLUSTER_CTL_FIRST_WAIT) #define QTO_EAST_CLK(i) BIT(14 + (i)) #define QTO_MUX(i) BIT(16 + (i)) @@ -368,8 +330,6 @@ static const struct { #define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 #define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ #define PMU_BOOT_STAGE_SLEEP 0x50 -/* The post-timer PMU "go hib (0x7)" replay framing. */ -#define SPMI_GO_HIB_NOTIFY 0x2000000 /* * MasterMini (NUB_MPMGR_AON, RegMap 4 = pmgr reg[0x37] = 0x508900000). @@ -566,26 +526,14 @@ struct t6040_sleep { void __iomem *a2d_mini, *a2d_mini4, *a2d_mm8, *a2d_pmc, *a2d_blk; void __iomem *a2d_mm12; /* MasterMini 0x50890c000 (IB2 engine + handshake +0x14) */ void __iomem *aop_req; /* AOP island power requests 0x5108b8100 (bit 0 DRAM, 1 AP, 2 CPU) */ - void __iomem *mtp_ps; /* MiniPMGR MTP island PS words 0x50830c000..0x060 (bring-up) */ - u32 mtp_left; /* MTP PS words that did not reach ACTUAL 0 at this commit */ u32 apcie_diag[16]; /* apcie_seq bit 2: 'APCS' + flags + PHY/PS words, PMU 0xa200 */ - void __iomem *atc_aon; /* 0x508320c00: the 4 ATC port blocks of A2D seq 25 */ void __iomem *dcs[16]; /* DCS(n,k): 0x220200000 + n * 0x2000000 + k * 0x200000 */ - void __iomem *amcc_sem[4]; /* 0x2201c0000 + n * 0x2000000 (+0x28 MCU handshake) */ - void __iomem *pg_main, *pg_east; /* pwrgate 0x50282c000, SOC-East 0x5022a0000 */ void __iomem *aop_asc; /* AOP ASC 0x510600000 (CPU_CONTROL +0x44) */ void __iomem *atc_cfg[4]; /* ATCPHY_CFG(n) 0x383200000 + n * 0x8000000 */ - void __iomem *mini_ps; /* 0x508300000: MiniPMGR PS words */ void __iomem *aop_ps; /* MiniPMGR AOP + audio island PS words 0x508304000..0x158 */ void __iomem *snap_soc, *snap_east; /* PS snapshot pages 0x502800000 / 0x502280000 */ u32 *ps_saved; /* each PS word at the commit, see t6040_ps_restore() */ bool ps_saved_valid; - void __iomem *pmc_probe_io[9]; /* bring-up PMC probe words */ - u32 aop_left; /* AOP words left on << 8 | first index left on */ - void __iomem *cpm_cfg[3]; /* cluster + 0xe50000 (iBoot idle power-down, bring-up) */ - void __iomem *cpm_impl[3]; /* cluster + 0xe40000 (cpm-impl-reg) */ - void __iomem *cpu4_impl110; /* boot CPU cpu-impl-reg + 0x110 (0x211050110) */ - u32 cpm_to; /* clusters whose CPM state poll timed out at this commit */ void __iomem *soc_east; void __iomem *soc_ps; void __iomem *aop_global; @@ -594,7 +542,6 @@ struct t6040_sleep { u64 mbox_pa; bool power_ops; bool wd2_armed; - bool timer_written; /* bring-up observer: the S2R timer is armed */ u64 siq_cfg; u64 cntkctl; /* Saved across the commit, restored on wake. */ @@ -610,11 +557,7 @@ struct t6040_sleep { void __iomem *acc_apsc[3]; void __iomem *tvm_ctl[8]; void __iomem *tvm_status[8]; - void __iomem *pmc_ap_floor; - void __iomem *pmc_dvmr; void __iomem *pmc_bwr; - void __iomem *dispext_pwrgate; - void __iomem *dcp_asc_ctl[3]; void __iomem *ptd; void __iomem *gapf; u32 quiesce_to; /* timeout bits of this commit's quiesce polls */ @@ -630,7 +573,6 @@ struct t6040_sleep { struct t6040_apcie_st apcie_st; bool pcie_ans_stopped; bool ans_bridge_quiesced; /* the commit found ANS's fabric bridge armed */ - int ans_off_ret; int pcie_ans_on_ret; u32 apcie_wake_step; u32 gp_phy_saved[2], gp_appclk_saved[2], gp_perst_saved[2]; @@ -672,22 +614,11 @@ module_param(wd2_secs, uint, 0644); MODULE_PARM_DESC(wd2_secs, "Arm the AON WD2 for this many seconds from .begin to .end of a dry run 2 or 3 (0: off, at most 178)"); -/* - * After the S2R timer, the platform issues a PMU "go hib (0x7)" SPMI sequence. - * Whether a Linux-at-EL2 driver must issue it, or the MasterMini timer alone - * arms the engine and the PMU sees it over the hardware SPMI link, is unproven. - * Off by default. - */ static bool pmgr_quiesce = true; module_param(pmgr_quiesce, bool, 0644); MODULE_PARM_DESC(pmgr_quiesce, "Run the steps of the generic PMGR quiesce in the commit: APSC off, PLL relock off, TVMs off, PMC DCS floor"); -/* - * On the J616s the first access to 0x50480c000 (the AP voter agent's floor) hung the - * bus (mark 0x330, reset by the 30 s WD2). The address is unverified on - * hardware; off until it is. - */ /* * On the J616s, with the secondaries still spinning (stopped, coherent) until after * the timer, the complexes +0xc/+0x10 never dropped BIT4 and the engine stalled. On @@ -700,10 +631,6 @@ module_param(release_early, bool, 0644); MODULE_PARM_DESC(release_early, "Release the stopped CPUs to their last WFI at the commit's start and wait until they are off"); -static bool pmc_floor; -module_param(pmc_floor, bool, 0644); -MODULE_PARM_DESC(pmc_floor, "With pmgr_quiesce: the PMC AP DCS floor vote (its address hung the bus on the J616s)"); - static bool tvm_gpu = true; module_param(tvm_gpu, bool, 0644); MODULE_PARM_DESC(tvm_gpu, "With pmgr_quiesce: also the GPU PMGR's afr/gfx TVMs (0x288e94000..)"); @@ -718,43 +645,6 @@ module_param(disp_clusters, uint, 0644); MODULE_PARM_DESC(disp_clusters, "DISP_INT/DISP_EXT cluster-ctl slots at the commit: 0 untouched, 1 off + wait, 2 off, no wait"); -/* - * At runtime the PTD request word reads 0x63c00: EACC0/PACC0/PACC1/GFX (10-13) and the - * two DCPEXTs (17, 18, withdrawn by pmp-report at suspend_noirq). Bits set here are - * withdrawn at the commit right before the S2R timer (no ack wait). Bring-up knob. - */ -static unsigned long ptd_withdraw; -module_param(ptd_withdraw, ulong, 0644); -MODULE_PARM_DESC(ptd_withdraw, "PTD SOC-DEV-PS-REQ bits to withdraw right before the S2R timer"); - -static bool post_timer_mask; -module_param(post_timer_mask, bool, 0644); -MODULE_PARM_DESC(post_timer_mask, "Bring-up: the post-timer counter probe's mark carries which cores and ANE words are on"); - -static bool post_timer_ane; -module_param(post_timer_ane, bool, 0644); -MODULE_PARM_DESC(post_timer_ane, "Bring-up: the post-timer counter probe's mark carries the ANE power words instead"); - -static bool post_timer_counter_probe; -module_param(post_timer_counter_probe, bool, 0644); -MODULE_PARM_DESC(post_timer_counter_probe, - "Bring-up: after the S2R timer, test whether CNTPCT still moves (answer by WD2 timing + mark)"); - -static unsigned int post_timer_wd_ms; -module_param(post_timer_wd_ms, uint, 0644); -MODULE_PARM_DESC(post_timer_wd_ms, - "Bring-up: re-arm the AON WD2 to this many ms (max 178000) right after the S2R timer"); - -static unsigned int post_timer_probe_ms; -module_param(post_timer_probe_ms, uint, 0644); -MODULE_PARM_DESC(post_timer_probe_ms, - "Bring-up: after the S2R timer spin this long (max 4 s), then mark 0x50EEPPQQ = cluster-ctl E/P0/P1"); - -static unsigned int timer_delay_ms; -module_param(timer_delay_ms, uint, 0644); -MODULE_PARM_DESC(timer_delay_ms, - "Bring-up: hold this long (max 10 s) right before the S2R timer, mark 0x3e0 first"); - static bool agx_gate = true; module_param(agx_gate, bool, 0644); MODULE_PARM_DESC(agx_gate, @@ -765,11 +655,6 @@ module_param(keep_alive_off, bool, 0644); MODULE_PARM_DESC(keep_alive_off, "Clear the 64 PMGR keep-alive bits right before the S2R timer, as the PMGR quiesce does"); -static bool replay_pmu_go_hib; -module_param(replay_pmu_go_hib, bool, 0644); -MODULE_PARM_DESC(replay_pmu_go_hib, - "Replay the PMU \"go hib\" SPMI pairs after the S2R timer (necessity unconfirmed)"); - static struct t6040_sleep *t6040_sleep; static void t6040_iounmap(void *p) @@ -1151,90 +1036,8 @@ static void t6040_cpu_stop(u64 mpidr) dsb(sy); } -/* - * Bring-up observer (on the J616s, after the timer only the master core is on, P1 still 0x10; - * the master never WFIs while it probes). One parked core is kept spinning instead of its - * deep WFI; after the master writes the timer and takes its own deep WFI, it samples the - * complexes and the master's core word and keeps the latest in the PMU comparator: - * 0x3PPLLEEM [27:22] P0, [21:16] P1, [15:10] E cluster-ctl bits [5:0], - * [9:6] the master core's ACTUAL, [5:0] sample number (saturates at 63) - * Its own complex can't drop while it spins: put it in P1 to watch E and P0. - */ -static unsigned long observer_mpidr = ULONG_MAX; -module_param(observer_mpidr, ulong, 0644); -MODULE_PARM_DESC(observer_mpidr, - "Bring-up: MPIDR of a parked core that observes the complexes after the S2R timer (ULONG_MAX: none)"); - -static void t6040_mark_nodelay(struct t6040_sleep *s, u32 m) -{ - void __iomem *b = s->spmi0; - u32 i; - - if (!b) - return; - for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) - readl(b + SPMI_REPLY); - writel(SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), b + SPMI_CMD); - writel(m, b + SPMI_CMD); - for (i = 0; i < 1000000; i++) - if (readl(b + SPMI_NOTIFY_ACK) & SPMI_NOTIFY_REPLY) - break; - writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); - if (!(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) - readl(b + SPMI_REPLY); -} - -static u32 t6040_a2d_state(struct t6040_sleep *s); -static u32 t6040_aop_state(struct t6040_sleep *s); - -static bool observer_aop; -module_param(observer_aop, bool, 0644); -MODULE_PARM_DESC(observer_aop, "Bring-up: the observer core marks the AOP power requests (0x1 tag, t6040_aop_state)"); - -static bool observer_a2d; -module_param(observer_a2d, bool, 0644); -MODULE_PARM_DESC(observer_a2d, - "Bring-up: the observer core marks the A2D state (0x2 tag, t6040_a2d_state) instead of the complexes"); - -static void __noreturn t6040_observe(struct t6040_sleep *s) -{ - u32 n = 0, i, e, p0, p1, mc; - - while (!READ_ONCE(s->timer_written)) - cpu_relax(); - for (;;) { - if (READ_ONCE(observer_aop)) { - t6040_mark_nodelay(s, 0x10000000 | t6040_aop_state(s) | (n & 3)); - n++; - for (i = 0; i < 500000; i++) - (void)readl_relaxed(s->cluster_ctl); - continue; - } - if (READ_ONCE(observer_a2d)) { - t6040_mark_nodelay(s, 0x20000000 | t6040_a2d_state(s) | (n & 3)); - n++; - for (i = 0; i < 500000; i++) - (void)readl_relaxed(s->cluster_ctl); - continue; - } - e = readl(s->cluster_ctl + CLUSTER_CTL(0)) & 0x3f; - p0 = readl(s->cluster_ctl + CLUSTER_CTL(1)) & 0x3f; - p1 = readl(s->cluster_ctl + CLUSTER_CTL(2)) & 0x3f; - mc = s->soc_ps ? FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 8 * 4)) : 0xf; - t6040_mark_nodelay(s, 0x30000000 | p0 << 22 | p1 << 16 | e << 10 | mc << 6 | - min(n, 63U)); - n++; - for (i = 0; i < 500000; i++) - (void)readl_relaxed(s->cluster_ctl); - } -} - static void t6040_cpu_off(u64 mpidr) { - struct t6040_sleep *s = t6040_sleep; - - if (s && s->cluster_ctl && READ_ONCE(observer_mpidr) == (mpidr & MPIDR_HWID_BITMASK)) - t6040_observe(s); t6040_deep_wfi(); } @@ -1441,16 +1244,6 @@ static void t6040_log_ane(struct t6040_sleep *s, const char *when) when, t6040_ane_actual(s), c[0], c[1], c[2]); } -/* Bring-up: the WD2 re-armed for @ms from now, writes only (usable with the counter stopped). */ -static void t6040_wd2_raw(struct t6040_sleep *s, u32 ms) -{ - writel(0, s->wdt + WDT_WD2_CTL); - writel(0, s->wdt + WDT_WD2_COUNT); - writel(ms * (WDT_TICKS_PER_S / 1000), s->wdt + WDT_WD2_ALARM); - writel(1, s->wdt + WDT_WD2_CTL); - dsb(sy); -} - /* * In process context, after the device suspend: which watched devices are * bound. The commit point runs with interrupts off and only reads power words. @@ -1617,147 +1410,6 @@ static int t6040_sleep_enclave_tell(void) return ret; } -/* - * Bring-up: the platform idles the PMP (AP and IOP power state 0x201) right before the cores - * sleep (PMP mailbox 0x500e08800) and no stop was seen after that, so the - * coprocessor may have to stay runnable for the engine. Bit 0 keeps the PMP's run bit - * after its idle, bit 1 ANS's. - */ -static unsigned int coproc_keep_run; -module_param(coproc_keep_run, uint, 0644); -MODULE_PARM_DESC(coproc_keep_run, - "Bring-up: keep the run bit after the idle at the commit (bit 0 PMP, bit 1 ANS)"); - -/* - * Bring-up: A2D stalls at MiniPMGR +0x40, the AP partition that does not power down. - * LLB's power-down runs the same A2D to completion after powering the MTP (and AOP+audio) - * islands off; the MTP is the only coprocessor whose DAPF reaches the MiniPMGR request words - * +0x100..+0x10b. Bit 0: power the 13 MTP island PS words (MiniPMGR 0x50830c000..0x060, - * MTP_FABRIC..MTP_DMA) off right before the S2R timer, children first, each polled to - * ACTUAL 0 (mark 0x57 + the words left on). Bit 1: the same at the dry-run commit point, - * logged, then the saved words written back (the MTP firmware does not survive it). - */ -static unsigned int mtp_island_off; -module_param(mtp_island_off, uint, 0644); -MODULE_PARM_DESC(mtp_island_off, - "Bring-up: MTP island PS off as LLB before A2D (bit 0 real commit, bit 1 dry-run point)"); - -#define T6040_MTP_PS_PA 0x50830c000ULL -#define T6040_MTP_PS_WORDS 13 - -/* Children first. Returns the words (bit n = +8 * n) that did not reach ACTUAL 0. */ -static u32 t6040_mtp_island_off(struct t6040_sleep *s, u32 *saved) -{ - u32 left = 0, v; - int n; - - if (!s->mtp_ps) - return GENMASK(T6040_MTP_PS_WORDS - 1, 0); - for (n = T6040_MTP_PS_WORDS - 1; n >= 0; n--) { - void __iomem *r = s->mtp_ps + 8 * n; - - v = readl(r); - if (saved) - saved[n] = v; - /* As LLB: automatic mode off first, then the target. */ - if (v & PS_AUTO_ENABLE) { - v &= ~PS_AUTO_ENABLE; - writel(v, r); - } - if (FIELD_GET(PS_TARGET, v)) - writel(v & ~PS_TARGET, r); - if (readl_poll_timeout_atomic(r, v, !FIELD_GET(PS_ACTUAL, v), 1, 2000)) - left |= BIT(n); - } - return left; -} - -/* - * Bring-up: iBoot's idle power-down (the same A2D as LLB's, - * which completes) configures the CPU clusters' CPM right before the cluster-ctl writes and - * the S2R request: cluster+0xe50000 - * |= 0x1c000000, poll +0x90 [19:16] in {5, 6, 10}, RMW (& 0xffffffef9c7e1fff) | 0x61816000, - * cpm-impl-reg cluster+0xe40000 |= bit 63 | bit 0, clear 0x1c000000 again; then the boot - * CPU's cpu-impl-reg 0x211050110 |= 0x3000000. Linux and aura never write these. - * Bit 0: clusters 0 and 1 as iBoot, bit 1: cluster 2 too, bit 2: the boot CPU's +0x110. - * A CPM state poll that times out leaves that cluster with 0x1c000000 cleared (mark 0x56). - */ -static unsigned int cpm_iboot; -module_param(cpm_iboot, uint, 0644); -MODULE_PARM_DESC(cpm_iboot, - "Bring-up: iBoot's CPM cluster config before the S2R request (bit 0 clusters 0+1, bit 1 cluster 2, bit 2 boot CPU +0x110)"); - -#define T6040_CPM_CFG_PA(c) (0x210e50000ULL + (c) * 0x1000000ULL) -#define T6040_CPM_IMPL_PA(c) (0x210e40000ULL + (c) * 0x1000000ULL) -#define T6040_CPU4_IMPL110_PA 0x211050110ULL -#define CPM_CFG_LOCK 0x1c000000ULL -#define CPM_CFG_STATE 0x90 -#define CPM_CFG_KEEP 0xffffffef9c7e1fffULL -#define CPM_CFG_SET 0x61816000ULL -#define CPM_IMPL_SET 0x8000000000000001ULL - -static bool t6040_cpm_state_ok(u64 v) -{ - unsigned int st = (v >> 16) & 0xf; - - return st <= 10 && (BIT(st) & 0x460); -} - -static u32 t6040_cpm_iboot(struct t6040_sleep *s, unsigned int mask) -{ - u32 to = 0; - u64 v; - int c; - - for (c = 0; c < 3; c++) { - void __iomem *cfg = s->cpm_cfg[c], *impl = s->cpm_impl[c]; - - if (!(c < 2 ? mask & BIT(0) : mask & BIT(1)) || !cfg || !impl) - continue; - writeq(readq(cfg) | CPM_CFG_LOCK, cfg); - if (readq_poll_timeout_atomic(cfg + CPM_CFG_STATE, v, t6040_cpm_state_ok(v), 1, 2000)) { - to |= BIT(c); - writeq(readq(cfg) & ~CPM_CFG_LOCK, cfg); - continue; - } - writeq((readq(cfg) & CPM_CFG_KEEP) | CPM_CFG_SET, cfg); - writeq(readq(impl) | CPM_IMPL_SET, impl); - writeq(readq(cfg) & ~CPM_CFG_LOCK, cfg); - } - if ((mask & BIT(2)) && s->cpu4_impl110) - writeq(readq(s->cpu4_impl110) | 0x3000000ULL, s->cpu4_impl110); - return to; -} - -static void t6040_log_cpm(struct t6040_sleep *s, const char *when) -{ - u64 v[3][3] = {}; - int c; - - for (c = 0; c < 3; c++) { - if (s->cpm_cfg[c]) { - v[c][0] = readq(s->cpm_cfg[c]); - v[c][1] = readq(s->cpm_cfg[c] + CPM_CFG_STATE); - } - if (s->cpm_impl[c]) - v[c][2] = readq(s->cpm_impl[c]); - } - pr_info("t6040-sleep: %s: CPM cfg/state/impl c0 %#llx/%#llx/%#llx c1 %#llx/%#llx/%#llx c2 %#llx/%#llx/%#llx cpu4 +0x110 %#llx\n", - when, v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2], v[2][0], v[2][1], v[2][2], - s->cpu4_impl110 ? readq(s->cpu4_impl110) : 0); -} - -/* - * Bring-up: the AOP + audio island off as iBoot's idle power-down (PS ids - * 0x49..0x31 in this order, LLB's id table gives each one's offset from MiniPMGR 0x508304000; - * 0x56 appears twice there). Bit 0: right before the S2R timer (mark 0x55, words left on - * << 8 | first index left on). Wake through the AOP may not work with it. - */ -static unsigned int aop_island_off; -module_param(aop_island_off, uint, 0644); -MODULE_PARM_DESC(aop_island_off, - "Bring-up: AOP + audio island PS off as iBoot before A2D (bit 0 real commit)"); - #define T6040_AOP_PS_PA 0x508304000ULL static const u16 t6040_aop_ps_order[] = { 0x0b0, 0x128, 0x120, 0x158, 0x150, 0x148, 0x140, 0x138, 0x130, 0x118, 0x0c0, 0x0c8, @@ -1766,7 +1418,6 @@ static const u16 t6040_aop_ps_order[] = { 0x048, 0x058, 0x060, 0x088, 0x050, 0x010, 0x018, 0x008, 0x000, }; - /* * Bring-up: which SOC / SOC-East power words are still on right before the S2R timer. * The reset that follows a stalled A2D loses everything but the PMU, so the bitmap (bit i = @@ -1925,93 +1576,6 @@ static void t6040_ps_snap_store(struct t6040_sleep *s) &map[i], NULL, 0); } - -/* - * Bring-up: the stalled MCPS sits in its PMC write block (static voters 0x5048ac000.., - * rail words 0x504820000 + rail * 0x4000, agent floors 0x504800000 + agent * 0x4000), and an - * AP access to the agent-3 floor hung the bus on the J616s. Read the words one by one, a mark - * (0x3d0 + index) before each so a hang names the word. Bit 0: at the dry-run commit point - * (WD2 armed, values logged). Bit 1: in the real commit right before the S2R timer, after the - * clock and keep-alive steps; the values go to PMU SRAM 0xa280 ('PMCP' + the 9 words). - */ -static unsigned int pmc_probe; -module_param(pmc_probe, uint, 0644); -MODULE_PARM_DESC(pmc_probe, "Bring-up: read the PMC words the A2D engine writes (bit 0 dry-run point, bit 1 real commit before the timer)"); - -static const u64 t6040_pmc_probe_pa[9] = { - 0x5048aa140ULL, 0x5048ac000ULL, 0x5048ac00cULL, 0x5048ac020ULL, 0x504800000ULL, - 0x50480c000ULL, 0x504820000ULL, 0x504824004ULL, 0x504828004ULL, -}; -#define T6040_PMC_PROBE_PMU 0xa280 -#define T6040_PMC_PROBE_MAGIC 0x50434d50 /* "PMCP" in PMU byte order */ - -static void t6040_mark(struct t6040_sleep *s, u32 n); - -static void t6040_pmc_probe_read(struct t6040_sleep *s, u32 *v) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) { - t6040_mark(s, 0x3d0 + i); - v[i] = s->pmc_probe_io[i] ? readl(s->pmc_probe_io[i]) : 0xdeadbeef; - } -} - -/* - * Bring-up (the PMC stopped answering between the dry-run point and the timer): one read - * of PMC 0x5048aa140 after each .enter / commit step, mark 0x380 + checkpoint before it, so a - * hang names the first step after which the PMC is gone. - */ -static bool pmc_bisect; -module_param(pmc_bisect, bool, 0644); -MODULE_PARM_DESC(pmc_bisect, "Bring-up: read PMC 0x5048aa140 after each sleep step (mark 0x380 + step)"); - -static void t6040_pmc_cp(struct t6040_sleep *s, unsigned int k) -{ - if (!READ_ONCE(pmc_bisect) || !s->pmc_probe_io[0]) - return; - t6040_mark(s, 0x380 + k); - (void)readl(s->pmc_probe_io[0]); -} - -static void t6040_pmc_probe_store(struct t6040_sleep *s, const u32 *v) -{ - u32 m = T6040_PMC_PROBE_MAGIC; - int i; - - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMC_PROBE_PMU, SPMI_OP_WRITE4), &m, NULL, 0); - for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMC_PROBE_PMU + 4 + 4 * i, SPMI_OP_WRITE4), - &v[i], NULL, 0); -} - -/* As LLB per word: automatic mode off, target 0, ACTUAL polled (bounded here). */ -static u32 t6040_aop_island_off(struct t6040_sleep *s) -{ - u32 left = 0, first = 0xff, v; - int i; - - if (!s->aop_ps) - return 0xffff; - for (i = 0; i < ARRAY_SIZE(t6040_aop_ps_order); i++) { - void __iomem *r = s->aop_ps + t6040_aop_ps_order[i]; - - v = readl(r); - if (v & PS_AUTO_ENABLE) { - v &= ~PS_AUTO_ENABLE; - writel(v, r); - } - if (FIELD_GET(PS_TARGET, v)) - writel(v & ~PS_TARGET, r); - if (readl_poll_timeout_atomic(r, v, !FIELD_GET(PS_ACTUAL, v), 1, 2000)) { - left++; - if (first == 0xff) - first = i; - } - } - return left ? (left << 8 | first) : 0; -} - /* * On the J616s the device resume reached the AOP (510c10000.aop) and took an SError in its resume, * whose first act after a firmware-driven sleep is reading its ASC's run bit: the AOP + audio @@ -2129,30 +1693,14 @@ static void t6040_aop_wake(struct t6040_sleep *s) } } -/* As read: parents first, the saved targets back. */ -static void t6040_mtp_island_back(struct t6040_sleep *s, const u32 *saved) -{ - int n; - - for (n = 0; n < T6040_MTP_PS_WORDS; n++) - writel(saved[n], s->mtp_ps + 8 * n); -} - /* As on T8140, measured on ANS: status and control read, then the run bit cleared twice. */ static void t6040_sleep_coproc_run_off(struct apple_sleep_coproc *c) { - unsigned int keep = READ_ONCE(coproc_keep_run); - if (!c->asc) { c->stopped = true; return; } c->status = readl(c->asc + ASC_CPU_STATUS); - if ((c->pcie_ans && (keep & BIT(1))) || - (!c->pcie_ans && c->last_stop && (keep & BIT(0)))) { - c->stopped = true; - return; - } writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); c->stopped = true; @@ -2269,23 +1817,6 @@ static const struct t6040_axi2af_domain { { "apcie_sys_gp", true, 0x390, 0x416000000 }, }; -/* - * The post-timer PMU "go hib (0x7)" SPMI pairs, as the platform sends them. Opaque - * (the abbey PMU opcode map is not decoded); replayed only if replay_pmu_go_hib - * is set. Each is framed as the platform frames it: notify, command, optional data, poll - * +0x80 bit0, acknowledge. - */ -static const struct { - u32 cmd; - u32 data; - bool has_data; -} t6040_pmu_go_hib_seq[] = { - { 0x20008e38, 0, false }, - { 0x20088e33, 0x1e9238, true }, - { 0x200e8e30, 0x0, true }, - { 0x20008e30, 0x41, true }, -}; - /* * As the platform writes the PMU over SPMI (mirror of T8140's t8140_pmu_write): the * reply vector masked, the command and value written to +0x4, the reply polled @@ -2554,35 +2085,6 @@ static void t6040_mark_restore(struct t6040_sleep *s) t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); } -/* - * The optional "go hib" replay. Necessity from Linux is unconfirmed. Its second - * pair writes the PMU RTC compare (reg 0x2008); the measured value 0x1e9238 was the - * alarm of that moment and lies in the past now, which would fire the alarm at once. The live alarm - * goes there instead (the one t6040_mark_restore puts back), or nothing is replayed. - */ -#define PMU_GO_HIB_COMPARE_CMD 0x20088e33 - -static void t6040_pmu_go_hib(struct t6040_sleep *s) -{ - u32 status, data; - int i; - - if (!s->spmi0 || !s->alarm_ok) - return; - for (i = 0; i < ARRAY_SIZE(t6040_pmu_go_hib_seq); i++) { - data = t6040_pmu_go_hib_seq[i].cmd == PMU_GO_HIB_COMPARE_CMD ? - s->alarm_saved + 1 : t6040_pmu_go_hib_seq[i].data; - readl(s->spmi0 + SPMI_STATUS); - writel(SPMI_GO_HIB_NOTIFY, s->spmi0 + SPMI_NOTIFY_ENABLE); - writel(t6040_pmu_go_hib_seq[i].cmd, s->spmi0 + SPMI_CMD); - if (t6040_pmu_go_hib_seq[i].has_data) - writel(data, s->spmi0 + SPMI_CMD); - readl_poll_timeout_atomic(s->spmi0 + SPMI_NOTIFY_ACK, status, - status & SPMI_NOTIFY_REPLY, 1, 1000); - writel(1, s->spmi0 + SPMI_NOTIFY_ACK); - } -} - /* * The five complexes off, as the quiesce loop turns cluster c off and waits if c > 3, * for c = 1..5 (measured on the J616s): each slot's low nibble -> 0, in order +0x0 .. +0x10, @@ -2718,13 +2220,6 @@ static void t6040_pmgr_quiesce(struct t6040_sleep *s) QUIESCE_TIMEOUT_US)) s->quiesce_to |= QTO_TVM(i); } - if (s->pmc_ap_floor && READ_ONCE(pmc_floor)) { - t6040_mark(s, 0x330); - writel(readl(s->pmc_ap_floor) & ~PMC_FLOOR_DCS, s->pmc_ap_floor); - if (readl_poll_timeout_atomic(s->pmc_ap_floor + PMC_PENDING, v, - !(v & PMC_PENDING_AP), 1, 8000)) - s->quiesce_to |= QTO_PMC; - } } /* The AGX residency gate of the PMGR quiesce. Returns a mark step for how far it got. */ @@ -2878,26 +2373,16 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) } } -/* - * Bring-up: every AP wake source masked, as LLB and iBoot do before their power-down (the - * same A2D, which completes there). Nothing but a cold boot (the power key) ends that sleep. - */ -static bool wake_mask_all; -module_param(wake_mask_all, bool, 0644); -MODULE_PARM_DESC(wake_mask_all, - "Bring-up: mask all 9 AOP-global wake words as LLB (no wake source: power key only)"); - /* Arm the 9 AOP-global wake masks before the commit, saving the old words. */ static void t6040_wake_arm(struct t6040_sleep *s) { - bool all = READ_ONCE(wake_mask_all); int i; if (!s->aop_global) return; for (i = 0; i < AOP_WAKE_WORDS; i++) { s->wake_mask_saved[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); - writel(all ? 0xffffffff : t6040_wake_mask[i], s->aop_global + AOP_WAKE_MASK(i)); + writel(t6040_wake_mask[i], s->aop_global + AOP_WAKE_MASK(i)); } s->wake_mask_saved_valid = true; } @@ -2921,28 +2406,6 @@ static void t6040_wake_restore(struct t6040_sleep *s) * the target, then AUTO_ENABLE once ACTUAL reads on. Writing PS_FLAGS clears * them. Separate from t6040_sleep_ps_on(), which reads a cached value. */ -static u32 t6040_ps_lower_start(void __iomem *base, u32 offset) -{ - void __iomem *reg = base + offset; - u32 val = readl(reg); - - if (val & PS_AUTO_ENABLE) { - val &= ~(PS_AUTO_ENABLE | PS_FLAGS); - writel(val, reg); - } - - return val; -} - -static void t6040_ps_lower_finish(void __iomem *base, u32 offset, u32 val) -{ - writel((val & ~PS_TARGET) | PS_FLAGS, base + offset); -} - -static void t6040_ps_lower(void __iomem *base, u32 offset) -{ - t6040_ps_lower_finish(base, offset, t6040_ps_lower_start(base, offset)); -} static int t6040_ps_raise_start(void __iomem *base, u32 offset) { @@ -2972,14 +2435,6 @@ static int t6040_ps_raise(void __iomem *base, u32 offset) return ret; } -static int t6040_ps_wait_off(void __iomem *base, u32 offset) -{ - u32 val; - - return readl_poll_timeout_atomic(base + offset, val, !FIELD_GET(PS_ACTUAL, val), 10, - PS_OFF_TIMEOUT_US); -} - static bool t6040_ps_actual_on(void __iomem *base, u32 offset) { return FIELD_GET(PS_ACTUAL, readl(base + offset)); @@ -3052,9 +2507,6 @@ static void t6040_apcie_clk_off(struct t6040_apcie_st *a) * apcie_st_ps=0 leaves the power words to the engine (the PHY, clock and AXI quiesce * still run); 1 lowers them here as the T8140 driver does. */ -static bool apcie_st_ps; -module_param(apcie_st_ps, bool, 0644); -MODULE_PARM_DESC(apcie_st_ps, "Lower ANS's ST lanes and ANS in the commit (0: leave them to the A2D engine, which needs APCIE_PHY_SW's parents on)"); /* * With apcie_st_ps=0 the engine still waits on APCIE_PHY_SW: right before the timer @@ -3383,130 +2835,6 @@ static void t6040_fabrics_on(struct t6040_sleep *s) t6040_mark_raw(s, 0x54d00000 | (rec[2] & 0xffff)); } -/* - * The MCPS stalls in seq 25's ATC block of port 0 or port 1 at random. Each port n has a - * block at 0x508320c00 + 0x80n (seq 23 writes +0x00..+0x14, seq 21 +0x1c/+0x40/+0x60, seq 25 - * writes +0x60 := 2 and polls +0x00 bit 30). aon_rec stores, right before the S2R timer (instead - * of 'APCS'), 'AONB' + port 0/1 {+0x00, +0x1c, +0x40, +0x60} + port 2/3 {+0x00, +0x60} + - * ATC0/1_PHYMXWRAP + ATC1_USB_AON at PMU 0xa200; only the offsets those programs use are - * read. Writing aon_dump logs the same words now. - */ -static bool aon_rec; -module_param(aon_rec, bool, 0644); -MODULE_PARM_DESC(aon_rec, "Bring-up: record the ATC A2D hand-over blocks at PMU 0xa200 right before the S2R timer (replaces 'APCS')"); - -#define T6040_AON_MAGIC 0x424e4f41 /* "AONB" in PMU byte order */ - -static void t6040_aon_take(struct t6040_sleep *s, u32 *w) -{ - static const u16 off[12] = { 0x000, 0x01c, 0x040, 0x060, 0x080, 0x09c, 0x0c0, 0x0e0, - 0x100, 0x160, 0x180, 0x1e0 }; - int i; - - w[0] = T6040_AON_MAGIC; - for (i = 0; i < 12; i++) - w[1 + i] = s->atc_aon ? readl(s->atc_aon + off[i]) : 0xdeadbeef; - w[13] = s->mini_ps ? readl(s->mini_ps + 0x150) : 0xdeadbeef; - w[14] = s->mini_ps ? readl(s->mini_ps + 0x158) : 0xdeadbeef; - w[15] = s->mini_ps ? readl(s->mini_ps + 0x0d0) : 0xdeadbeef; -} - -static void t6040_aon_store(struct t6040_sleep *s) -{ - u32 w[16]; - int i; - - t6040_aon_take(s, w); - for (i = 0; i < 16; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], - NULL, 0); -} - -static int t6040_aon_dump_set(const char *val, const struct kernel_param *kp) -{ - u32 w[16]; - - if (!t6040_sleep) - return -ENODEV; - t6040_aon_take(t6040_sleep, w); - dev_info(t6040_sleep->dev, - "aon: p0 %08x %08x %08x %08x p1 %08x %08x %08x %08x p2 %08x %08x p3 %08x %08x mxwrap0 %08x mxwrap1 %08x usb_aon1 %08x\n", - w[1], w[2], w[3], w[4], w[5], w[6], w[7], w[8], w[9], w[10], w[11], w[12], w[13], - w[14], w[15]); - return 0; -} - -static const struct kernel_param_ops t6040_aon_dump_ops = { - .set = t6040_aon_dump_set, -}; -module_param_cb(aon_dump, &t6040_aon_dump_ops, NULL, 0200); -MODULE_PARM_DESC(aon_dump, "Bring-up: write anything to log the ATC A2D hand-over blocks now"); - -/* - * A2D completes; the engine stalls in D2S (seq 28) polling the AMCC0 DCS pair's power - * gate (0x50282c918 bit 30). On this revision the program never writes the DCS power words: the - * DCS gate themselves after self-refresh, only in dynamic power-gate mode (DCS+0x20 bit 12, bit 8 - * enable). The platform keeps that mode on at runtime and re-applies it after each wake - * (DCS dynamic power-gate mode 1: RMW +0x20 mask 0x1100 := 0x1100 on - * all 16 DCS); Linux never sets it. Only the offsets the platform and the engine use are read. - * dcs_dump logs them now; dcs_pg bit 0 applies the the platform mode now (on write) and again in - * .begin; dcs_rec stores 'DCSR' + the DCS +0x20 low halves + the 4 AMCC handshake words + the - * 8 pwrgate CFG0 top bytes + the DCS +0x500 bits [1:0] at PMU 0xa200 right before the timer. - */ -#define T6040_DCS_MAGIC 0x52534344 /* "DCSR" in PMU byte order */ -static const u16 t6040_pg_main_off[4] = { 0x918, 0x998, 0xa18, 0xa98 }; -static const u16 t6040_pg_east_off[4] = { 0xa00, 0xa80, 0xb00, 0xb80 }; - -static bool dcs_rec; -module_param(dcs_rec, bool, 0644); -MODULE_PARM_DESC(dcs_rec, "Bring-up: record the DCS power-gate state at PMU 0xa200 right before the S2R timer (replaces 'AONB'/'APCS')"); - -/* - * D2S still stalls with dynamic power gating on and the AOP/PMP/MTP ruled out. - * The T8140 commit stops each memory channel right before its gates at - * about -18 ms (t8140_sleep_mcc_stop): at DCS +0x580 ("mcc0..2" = 0x2_2040_0580 + - * ch * 0x20_0000, the DCS slices' spacing) the enables word +0x4 cleared one bit at a time, - * then lanes 0..3 strobed and cleared on +0x8 (mask word +0xc), two lanes per channel per - * pass. The T6040 commit never did it (the measurement behind "the platform writes none" did not - * cover these pages). dcs_mcc_stop bit 0 runs that sequence on all 16 DCS right before the S2R - * timer; 'DCSR' words 10/11 then hold DCS 0.0 +0x584/+0x588 after it. - */ -static unsigned int dcs_mcc_stop; -module_param(dcs_mcc_stop, uint, 0644); -MODULE_PARM_DESC(dcs_mcc_stop, "Bring-up: bit 0 = T8140 mcc_stop (DCS +0x584 enables, +0x588 lanes) on all 16 DCS right before the S2R timer"); - -static void t6040_dcs_mcc_stop(struct t6040_sleep *s) -{ - int i, pass, n; - u32 val; - - for (i = 0; i < 16; i++) { - void __iomem *m = s->dcs[i] ? s->dcs[i] + 0x580 : NULL; - - if (!m) - continue; - val = readl(m + 0x4); - for (n = 0; val && n < 32; n++) { - val &= val - 1; - writel(val, m + 0x4); - val = readl(m + 0x4); - } - } - for (pass = 0; pass < 2; pass++) { - for (i = 0; i < 16; i++) { - void __iomem *m = s->dcs[i] ? s->dcs[i] + 0x580 : NULL; - - if (!m) - continue; - for (n = 2 * pass; n < 2 * pass + 2; n++) { - writel(readl(m + 0xc), m + 0xc); - writel(readl(m + 0x8) | BIT(16 + 4 * n), m + 0x8); - writel(readl(m + 0x8) & ~BIT(4 * n), m + 0x8); - } - } - } -} - /* * On this revision the D2S program does not write the DCS power-state words; it expects * them in automatic mode (bit 28) with PS_MIN 0, so each DCS domain drops to 0 after @@ -3525,7 +2853,6 @@ static const struct { { false, 0x1d0 }, { false, 0x1e0 }, { false, 0x1f0 }, { false, 0x200 }, { true, 0x300 }, { true, 0x308 }, { true, 0x310 }, { true, 0x318 }, }; -static const u16 t6040_amcc_ps[8] = { 0x180, 0x198, 0x1a8, 0x1b8, 0x1c8, 0x1d8, 0x1e8, 0x1f8 }; static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) { @@ -3576,110 +2903,6 @@ static const struct kernel_param_ops t6040_dcs_ps_auto_ops = { module_param_cb(dcs_ps_auto, &t6040_dcs_ps_auto_ops, &dcs_ps_auto, 0644); MODULE_PARM_DESC(dcs_ps_auto, "Bring-up: bit 0 = DCS power-state words to automatic mode (PS_MIN 0) now and in .begin, as the platform at boot"); -static void t6040_dcs_take(struct t6040_sleep *s, u32 *w) -{ - int i; - - memset(w, 0, 16 * sizeof(u32)); - w[0] = T6040_DCS_MAGIC; - for (i = 0; i < 16; i++) { - u32 v = s->dcs[i] ? readl(s->dcs[i] + 0x20) : 0xdead; - u32 b = s->dcs[i] ? readl(s->dcs[i] + 0x500) : 0; - - w[1 + i / 2] |= (v & 0xffff) << (16 * (i % 2)); - w[15] |= (b & 3) << (2 * i); - } - for (i = 0; i < 4; i++) { - if (s->amcc_sem[i]) - w[9] |= (readl(s->amcc_sem[i] + 0x28) & 1) << i; - if (s->pg_main) - w[13] |= (readl(s->pg_main + t6040_pg_main_off[i]) >> 24) << (8 * i); - if (s->pg_east) - w[14] |= (readl(s->pg_east + t6040_pg_east_off[i]) >> 24) << (8 * i); - } - /* The AOP island's DRAM request (0x5108b8100 bit 0) and MasterMini+0x14. */ - if (s->aop_req) - w[9] |= (readl(s->aop_req + 0x100) & 0xff) << 8; - if (s->mastermini) - w[9] |= (readl(s->mastermini + 0x14) & 0xffff) << 16; - /* DCS_00 and DCS_04 PS words, AMCC0 PS word (instead of the AOP ASC). */ - w[10] = t6040_dcs_ps_reg(s, 0) ? readl(t6040_dcs_ps_reg(s, 0)) : 0xdeadbeef; - w[11] = t6040_dcs_ps_reg(s, 4) ? readl(t6040_dcs_ps_reg(s, 4)) : 0xdeadbeef; - w[12] = s->snap_soc ? readl(s->snap_soc + t6040_amcc_ps[0]) : 0xdeadbeef; -} - -/* - * With dynamic power gating on (0x1100 on all 16 DCS) and the DRAM MCU handshake done - * for all 4 AMCC, the DCS still do not gate: something keeps using DRAM. The AOP runs through - * A2D/D2S and has a DRAM request bit (0x5108b8100 bit 0). aop_halt bit 0 clears the AOP ASC RUN - * bit right after the 'DCSR' record, just before the S2R timer (diagnostic: the AOP island stays - * on; a wake then has no AOP). Record word 12 = CPU_CONTROL after the halt. - */ -static unsigned int aop_halt; -module_param(aop_halt, uint, 0644); -MODULE_PARM_DESC(aop_halt, "Bring-up: bit 0 = stop the AOP CPU right before the S2R timer (diagnostic)"); - -static void t6040_dcs_store(struct t6040_sleep *s) -{ - u32 w[16]; - int i; - - t6040_dcs_take(s, w); - if ((READ_ONCE(dcs_mcc_stop) & BIT(0)) && s->dcs[0]) { - t6040_dcs_mcc_stop(s); - w[10] = readl(s->dcs[0] + 0x584); - w[11] = readl(s->dcs[0] + 0x588); - } - if ((READ_ONCE(aop_halt) & BIT(0)) && s->aop_asc) { - writel(readl(s->aop_asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, - s->aop_asc + ASC_CPU_CONTROL); - udelay(10); - w[12] = readl(s->aop_asc + ASC_CPU_CONTROL) | (readl(s->aop_req + 0x100) & 0xff) << 24; - } - for (i = 0; i < 16; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], - NULL, 0); -} - -static int t6040_dcs_dump_set(const char *val, const struct kernel_param *kp) -{ - struct t6040_sleep *s = t6040_sleep; - int i; - - if (!s) - return -ENODEV; - for (i = 0; i < 16; i++) - if (s->dcs[i]) - dev_info(s->dev, "dcs %d.%d: +0x20 %08x +0x500 %08x +0x1d48 %08x +0x580 %08x %08x %08x %08x\n", - i / 4, i % 4, readl(s->dcs[i] + 0x20), readl(s->dcs[i] + 0x500), - readl(s->dcs[i] + 0x1d48), readl(s->dcs[i] + 0x580), - readl(s->dcs[i] + 0x584), readl(s->dcs[i] + 0x588), - readl(s->dcs[i] + 0x58c)); - for (i = 0; i < 4; i++) - dev_info(s->dev, "amcc %d: +0x1c0028 %08x pg main %08x east %08x\n", i, - s->amcc_sem[i] ? readl(s->amcc_sem[i] + 0x28) : 0xdeadbeef, - s->pg_main ? readl(s->pg_main + t6040_pg_main_off[i]) : 0xdeadbeef, - s->pg_east ? readl(s->pg_east + t6040_pg_east_off[i]) : 0xdeadbeef); - for (i = 0; i < 16; i++) - dev_info(s->dev, "dcs ps %d: %08x\n", i, - t6040_dcs_ps_reg(s, i) ? readl(t6040_dcs_ps_reg(s, i)) : 0xdeadbeef); - for (i = 0; i < 8; i++) - dev_info(s->dev, "amcc ps %d: %08x\n", i, - s->snap_soc ? readl(s->snap_soc + t6040_amcc_ps[i]) : 0xdeadbeef); - dev_info(s->dev, "aop: req %08x mastermini+0x14 %08x asc ctl %08x status %08x\n", - s->aop_req ? readl(s->aop_req + 0x100) : 0xdeadbeef, - s->mastermini ? readl(s->mastermini + 0x14) : 0xdeadbeef, - s->aop_asc ? readl(s->aop_asc + ASC_CPU_CONTROL) : 0xdeadbeef, - s->aop_asc ? readl(s->aop_asc + ASC_CPU_STATUS) : 0xdeadbeef); - return 0; -} - -static const struct kernel_param_ops t6040_dcs_dump_ops = { - .set = t6040_dcs_dump_set, -}; -module_param_cb(dcs_dump, &t6040_dcs_dump_ops, NULL, 0200); -MODULE_PARM_DESC(dcs_dump, "Bring-up: write anything to log the DCS power-gate state now"); - static unsigned int dcs_pg = 1; static void t6040_dcs_dyn_pg(struct t6040_sleep *s) @@ -3787,23 +3010,12 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) t6040_apcie_clk_off(a); } - /* Per-lane: both sys_st then both st, deepest first. */ - if (READ_ONCE(apcie_st_ps)) { - t6040_ps_lower(ps, PS_APCIE_SYS_ST1); - t6040_ps_lower(ps, PS_APCIE_SYS_ST0); - t6040_ps_lower(ps, PS_APCIE_ST1); - t6040_ps_lower(ps, PS_APCIE_ST0); - } - - /* AXI enables off (shared), then ANS last. */ + /* AXI enables off (shared). */ if (a->axi) { writel(readl(a->axi + APCIE_AXI_EN0) & ~BIT(0), a->axi + APCIE_AXI_EN0); writel(readl(a->axi + APCIE_AXI_EN1) & ~BIT(0), a->axi + APCIE_AXI_EN1); } s->ans_bridge_quiesced = t6040_ans_bridge_quiesce(s); - if (READ_ONCE(apcie_st_ps)) - t6040_ps_lower(ps, PS_ANS); - /* The caller polls PS_ANS off, as on T8140. */ } /* @@ -4195,13 +3407,10 @@ static int t6040_sleep_commit(unsigned long unused) if (READ_ONCE(release_early)) spin_table_release_stopped(); t6040_mark(s, 0x21); - t6040_pmc_cp(s, 4); if (READ_ONCE(lower_atc)) t6040_sleep_lower_atc(s); - t6040_pmc_cp(s, 11); if (READ_ONCE(lower_apcie_gp)) t6040_sleep_stop_gp_ports(s); - t6040_pmc_cp(s, 12); /* * lower_io: ANS's two ST lanes + shared PHY down, once ANS has been @@ -4216,11 +3425,7 @@ static int t6040_sleep_commit(unsigned long unused) t6040_mark_raw(s, 0x54c00001); if (s->pcie_ans_stopped) { t6040_sleep_lower_apcie_st(s); - t6040_pmc_cp(s, 13); - if (s->soc_east && READ_ONCE(apcie_st_ps)) - s->ans_off_ret = t6040_ps_wait_off(s->soc_east, PS_ANS); } - t6040_pmc_cp(s, 14); if (READ_ONCE(lower_apcie_gp)) t6040_sleep_lower_apcie_gp(s); @@ -4230,7 +3435,6 @@ static int t6040_sleep_commit(unsigned long unused) * the SoC without a sleep, so the mark is kept: the wake alarm is not put back. */ s->commit_on_mask = t6040_axi2af_on_mask(s); - t6040_pmc_cp(s, 5); t6040_mark(s, 0x22); /* WD0 off, then the AON 5 s reconfig net (as the platform arms it for a reconfig). */ @@ -4257,7 +3461,6 @@ static int t6040_sleep_commit(unsigned long unused) * already; the wake side re-asserts them. */ s->quiesce_to = 0; - WRITE_ONCE(s->timer_written, false); if (READ_ONCE(release_early)) { t6040_mark(s, 0x2c); s->quiesce_to |= min(t6040_secondaries_off(s), 15U) << 20; @@ -4266,28 +3469,18 @@ static int t6040_sleep_commit(unsigned long unused) t6040_mark(s, 0x2b); t6040_pmgr_quiesce(s); } - t6040_pmc_cp(s, 6); if (READ_ONCE(agx_gate)) t6040_mark(s, 0x2a0 | t6040_agx_gate(s)); - s->cpm_to = 0; - if (READ_ONCE(cpm_iboot)) { - t6040_mark(s, 0x2e); - s->cpm_to = t6040_cpm_iboot(s, READ_ONCE(cpm_iboot)); - } - t6040_pmc_cp(s, 7); t6040_mark(s, 0x24); t6040_clusters_off(s); - t6040_pmc_cp(s, 8); /* Read-only: the axi2af panic gate the platform checks before the engine goes. */ t6040_axi2af_readcheck(s); t6040_mark(s, 0x350); t6040_clkgen_park(s); t6040_mark(s, 0x351); - t6040_pmc_cp(s, 9); if (READ_ONCE(keep_alive_off)) s->ka_on_before = t6040_keep_alive(s, false, 0); - t6040_pmc_cp(s, 10); t6040_mark(s, 0x25); /* Arm aura's AURAS2R1 mailbox with this boot's resume entry. */ @@ -4299,55 +3492,17 @@ static int t6040_sleep_commit(unsigned long unused) dsb(sy); } - if (s->ptd && READ_ONCE(ptd_withdraw)) - writeq(readq(s->ptd + PTD_REQ_READ) & ~(u64)READ_ONCE(ptd_withdraw), - s->ptd + PTD_REQ_WRITE); - - /* - * Discriminator: hold everything right before the timer. A reset that lands in the - * hold keeps mark 0x3e0 (its source started before the timer: the SMC/PMU after - * 'slpa', or a net); one that moves later by the hold follows the timer (the engine). - */ - if (READ_ONCE(timer_delay_ms)) { - t6040_mark(s, 0x3e0); - mdelay(min(READ_ONCE(timer_delay_ms), 10000U)); - } - - /* iBoot's order: the AOP + audio island, then the MTP island. */ - s->aop_left = 0; - if (READ_ONCE(aop_island_off) & BIT(0)) - s->aop_left = t6040_aop_island_off(s); - s->mtp_left = 0; - if (READ_ONCE(mtp_island_off) & BIT(0)) - s->mtp_left = t6040_mtp_island_off(s, NULL); - if (READ_ONCE(fab_on)) t6040_fabrics_on(s); t6040_ps_save(s); if (READ_ONCE(ps_snap)) t6040_ps_snap_store(s); - if (READ_ONCE(dcs_rec)) - t6040_dcs_store(s); - else if (READ_ONCE(aon_rec)) - t6040_aon_store(s); - else if (READ_ONCE(apcie_seq) & BIT(2)) + if (READ_ONCE(apcie_seq) & BIT(2)) t6040_apcie_diag_store(s); - if (READ_ONCE(pmc_probe) & BIT(1)) { - u32 pv[ARRAY_SIZE(t6040_pmc_probe_pa)]; - - t6040_pmc_probe_read(s, pv); - t6040_pmc_probe_store(s, pv); - } /* Last before the S2R timer: what is still on, re-read now, kept through the reset. */ s->commit_on_mask |= t6040_axi2af_on_mask(s); - if (s->aop_left) { - t6040_mark_raw(s, 0x55000000 | s->aop_left); - } else if (s->mtp_left) { - t6040_mark_raw(s, 0x57000000 | s->mtp_left); - } else if (s->cpm_to) { - t6040_mark_raw(s, 0x56000000 | s->cpm_to); - } else if (s->commit_on_mask) { + if (s->commit_on_mask) { t6040_mark_raw(s, 0x58000000 | s->commit_on_mask); } else if (s->quiesce_to & (QTO_CLUSTER(3) | QTO_CLUSTER(4))) { /* @@ -4369,129 +3524,11 @@ static int t6040_sleep_commit(unsigned long unused) if (s->mastermini) writel(1, s->mastermini + MASTERMINI_S2R_TIMER); dsb(sy); - WRITE_ONCE(s->timer_written, true); - dsb(ish); - - /* - * Bring-up: an SPMI mark right after the timer never lands. Re-arm the AON - * WD2 here, no SPMI: a reset at timer + this many ms means this core still runs - * after the timer; one at the usual timer + ~4.5 s means it does not (measured: it runs). - * Long values test whether A2D only needs longer than ~5 s: the reset follows the - * timer by that much although the commit armed WD2 for sleep_wd_secs (ADT - * simple-reconfig-wdog-icc-time = 5). - */ - if (READ_ONCE(post_timer_wd_ms) && s->wdt) { - writel(0, s->wdt + WDT_WD2_CTL); - writel(0, s->wdt + WDT_WD2_COUNT); - writel(min(READ_ONCE(post_timer_wd_ms), WDT_MAX_SECS * 1000U) * (WDT_TICKS_PER_S / 1000), - s->wdt + WDT_WD2_ALARM); - writel(1, s->wdt + WDT_WD2_CTL); - dsb(sy); - } - - /* - * Bring-up (this core does run after the timer: the WD2 above fired). Does - * the system counter stop at the timer (every udelay() would then hang)? Safety WD2 - * 4 s, 1M cluster-ctl reads timed by CNTPCT, an SPMI mark polled by count (no udelay): - * 0x50EEPPQQ, E bit 7 set, bit 6 = counter stopped. Then the WD2 by the answer: - * stopped -> 100 ms, moving -> 2.5 s; a loop that hangs leaves the 4 s. - */ - if (READ_ONCE(post_timer_counter_probe) && s->wdt && s->cluster_ctl) { - u32 i, v[3], st; - u64 c0, c1; - bool stopped; - - t6040_wd2_raw(s, 4000); - isb(); - c0 = read_sysreg(cntpct_el0); - for (i = 0; i < 1000000; i++) - (void)readl_relaxed(s->cluster_ctl); - isb(); - c1 = read_sysreg(cntpct_el0); - stopped = c1 == c0; - for (i = 0; i < 3; i++) - v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; - if (s->spmi0 && READ_ONCE(marks) && s->alarm_ok) { - void __iomem *b = s->spmi0; - u32 m = 0x50000000 | (v[0] | 0x80 | (stopped ? 0x40 : 0)) << 16 | - v[1] << 8 | v[2]; - - /* 0x50EAAAAA: tag E, then the ANE ACTUAL nibbles (t6040_ane_actual). */ - if (READ_ONCE(post_timer_ane)) - m = 0x50e00000 | t6040_ane_actual(s); - /* - * 0x50CMMMMM: tag C, ACTUAL != 0 as bits: 0-3 E cores, 4-8 P0 cores, - * 9-13 P1 cores (cpu-start bits 0-8, 10-14), 14-18 ANE sys/mpm/cpu/td/base. - */ - if (READ_ONCE(post_timer_mask)) { - u32 a = t6040_ane_actual(s), mask = 0, b; - - for (b = 0; b < 15 && s->soc_ps; b++) { - if (b == 9) - continue; - if (FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 8 * b))) - mask |= BIT(b < 9 ? b : b - 1); - } - for (b = 0; b < 5; b++) - if ((a >> (16 - 4 * b)) & 0xf) - mask |= BIT(14 + b); - m = 0x50c00000 | mask; - } - - for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) - readl(b + SPMI_REPLY); - writel(SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), b + SPMI_CMD); - writel(m, b + SPMI_CMD); - for (i = 0; i < 1000000; i++) { - st = readl(b + SPMI_NOTIFY_ACK); - if (st & SPMI_NOTIFY_REPLY) - break; - } - writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); - if (!(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) - readl(b + SPMI_REPLY); - } - t6040_wd2_raw(s, stopped ? 100 : 2500); - } /* A raised line would end the WFI the engine waits for. */ aic_sleep_mask_all(); spin_table_release_stopped(); - /* Optional, the bytes, necessity from Linux unproven (off by default). */ - if (replay_pmu_go_hib) - t6040_pmu_go_hib(s); - - /* - * Bring-up probe: the SoC resets ~5 s after the timer (a hold before the timer moves it), - * and a probe that waited with udelay() never marked: nothing that waits on - * time or on a reply may finish after the timer. Staged, no timer-based waits; the - * last mark that lands tells how far it got (the wake alarm is not put back): - * 0x50ffffff A: an SPMI mark right after the timer landed - * 0x50EEPPQQ B: cluster-ctl E/P0/P1 bytes [5:0] read once - * 0x50EEPPQQ C: after N * 1000 cluster-ctl reads: E bit 7 set, E bit 6 = the - * counter (CNTPCT) did not move over the reads - */ - if (READ_ONCE(post_timer_probe_ms) && s->cluster_ctl) { - u32 n = min(READ_ONCE(post_timer_probe_ms), 4000U) * 1000, i, v[3]; - u64 c0, c1; - - t6040_mark_raw(s, 0x50ffffff); - for (i = 0; i < 3; i++) - v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; - t6040_mark_raw(s, 0x50000000 | v[0] << 16 | v[1] << 8 | v[2]); - isb(); - c0 = read_sysreg(cntpct_el0); - for (i = 0; i < n; i++) - (void)readl_relaxed(s->cluster_ctl); - isb(); - c1 = read_sysreg(cntpct_el0); - for (i = 0; i < 3; i++) - v[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)) & 0x3f; - t6040_mark_raw(s, 0x50000000 | (v[0] | 0x80 | (c1 == c0 ? 0x40 : 0)) << 16 | - v[1] << 8 | v[2]); - } - t6040_deep_wfi(); } @@ -4950,7 +3987,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) t6040_log_ane(s, "dry-run commit point"); t6040_log_a2d(s, "dry-run commit point"); t6040_sleep_log_s2r_state(s, "dry-run commit point"); - t6040_log_cpm(s, "dry-run commit point"); { u32 map[T6040_PS_SNAP_WORDS]; @@ -4959,14 +3995,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) if (READ_ONCE(ps_snap)) t6040_ps_snap_store(s); } - if (READ_ONCE(pmc_probe) & BIT(0)) { - u32 pv[ARRAY_SIZE(t6040_pmc_probe_pa)]; - - t6040_pmc_probe_read(s, pv); - pr_info("t6040-sleep: dry-run commit point: PMC probe aa140 %#x ac000 %#x ac00c %#x ac020 %#x 800000 %#x 80c000 %#x 820000 %#x 824004 %#x 828004 %#x\n", - pv[0], pv[1], pv[2], pv[3], pv[4], pv[5], pv[6], pv[7], pv[8]); - t6040_pmc_probe_store(s, pv); - } /* * A wake source pending and left unmasked by the masks a real commit arms would keep the * AP partition from powering down (A2D then stalls at MiniPMGR +0x40). Read only. @@ -4980,21 +4008,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) pr_info("t6040-sleep: dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", (int)sizeof(p), p); } - if ((READ_ONCE(mtp_island_off) & BIT(1)) && s->mtp_ps) { - u32 saved[T6040_MTP_PS_WORDS], after[T6040_MTP_PS_WORDS], left; - int i; - - left = t6040_mtp_island_off(s, saved); - for (i = 0; i < T6040_MTP_PS_WORDS; i++) - after[i] = readl(s->mtp_ps + 8 * i); - pr_info("t6040-sleep: dry-run MTP island off: left on %#x, PS before %*ph\n", - left, (int)sizeof(saved), saved); - pr_info("t6040-sleep: dry-run MTP island off: PS after %*ph\n", - (int)sizeof(after), after); - t6040_log_a2d(s, "dry-run MTP island off"); - t6040_mtp_island_back(s, saved); - t6040_log_a2d(s, "dry-run MTP island back"); - } refused = t6040_sleep_refuse(s); pr_info("t6040-sleep: a real commit would %s here (%u check(s) failed)\n", refused ? "refuse" : "go on", refused); @@ -5030,14 +4043,6 @@ static bool lower_display = true; module_param(lower_display, bool, 0644); MODULE_PARM_DESC(lower_display, "Lower the display power words at the S2R commit point"); -static bool lower_cpu; -module_param(lower_cpu, bool, 0644); -MODULE_PARM_DESC(lower_cpu, "Also lower each display group's pipe CPU word by hand (raised an SError on T6040)"); - -static bool axi2af_quiesce; -module_param(axi2af_quiesce, bool, 0644); -MODULE_PARM_DESC(axi2af_quiesce, "Clear a display group's axi2af fabric-bridge bit0 before lowering it (off: its SYS domain stays on)"); - #define PS_DEV_DISABLE BIT(10) #define PS_BUSY BIT(11) #define PS_RESET_REQ BIT(12) @@ -5048,79 +4053,10 @@ MODULE_PARM_DESC(axi2af_quiesce, "Clear a display group's axi2af fabric-bridge b */ #define T6040_DISP_MARK(g, st, off) (0x5c000000 | ((g) & 0xff) << 16 | (st) << 12 | ((off) & 0xfff)) -/* The axi2af domain that carries a display group ("disp" -> "disp_sys"), or -1. */ -static int t6040_axi2af_of_group(const char *group) -{ - size_t n = strlen(group); - int i; - - for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) - if (!strncmp(t6040_axi2af[i].name, group, n) && - !strcmp(t6040_axi2af[i].name + n, "_sys")) - return i; - return -1; -} - -/* - * The entry side of the axi2af workaround (the quiesce step for it and the PMGR - * power-down hook): before a retained domain goes off, its fabric bridge - * +0x104/+0x108 bit0 is cleared; t6040_axi2af_rearm() sets it again on wake. Only while - * the domain is powered: the bridge sits in it. Mark 0x5eII0000, II the domain. - */ -static void t6040_axi2af_quiesce(struct t6040_sleep *s, int i) -{ - void __iomem *ps, *bridge; - - if (i < 0) - return; - ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); - bridge = s->axi2af_bridge[i]; - if (!ps || !bridge || !FIELD_GET(PS_ACTUAL, readl(ps))) - return; - t6040_mark_raw(s, 0x5e000000 | (i & 0xff) << 16); - writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); - writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); - t6040_mark_raw(s, 0x5e008000 | (i & 0xff) << 16); -} - static bool disp_seq_off = true; -/* - * Under Linux cluster-ctl DISP_INT went 0x2f -> 0x10 and stuck there; on the platform it reached 0, - * DISP_CPU having gone down at the DCP stop (RUN, bit5, dart idle, then the PS word - * in two steps). Rewriting the word under a running DCP resets the SoC. With - * appledrm.s2r_keep_run=0 this tries the exact sequence on it. - */ -static bool pmc_seq; -module_param(pmc_seq, bool, 0644); -MODULE_PARM_DESC(pmc_seq, - "Release a display pipe's PMC votes as the platform (boost, DVMR + ack, BWR, rail + ack) instead of zeroing c14/c18"); - -static bool dispext_off; -module_param(dispext_off, bool, 0644); -MODULE_PARM_DESC(dispext_off, - "Power the stopped DCPEXT pipes down as the platform (PMC votes, CPU, FE, SYS) so DISP_EXT can go off"); - -static bool pmp_disp_release; -module_param(pmp_disp_release, bool, 0644); -MODULE_PARM_DESC(pmp_disp_release, - "With disp_cpu_seq: withdraw the internal display's PMP request (PTD bit 16) and wait for the ack first"); - -static bool disp_cpu_seq; -module_param(disp_cpu_seq, bool, 0644); -MODULE_PARM_DESC(disp_cpu_seq, - "Lower DISP_CPU in the two steps after the DCP stop (needs appledrm.s2r_keep_run=0)"); - -static bool disp_need_cpu_gated; -module_param(disp_need_cpu_gated, bool, 0644); -MODULE_PARM_DESC(disp_need_cpu_gated, "Leave DISP_FE/SYS on unless the PMGR has gated DISP_CPU"); - #define DISP_CPU_GATE_US 50000 -static bool gated_cpu_target_only = true; -module_param(gated_cpu_target_only, bool, 0644); -MODULE_PARM_DESC(gated_cpu_target_only, "A DISP_CPU already gated (ACTUAL 0) only gets TARGET 0, keeping AUTO_ENABLE, and dart-dcp is left unread"); - static bool pmc_if_restore = true; module_param(pmc_if_restore, bool, 0644); MODULE_PARM_DESC(pmc_if_restore, "After a real sleep set the display pipes' PMC BWR and boost interface enables again"); @@ -5273,100 +4209,6 @@ static int t6040_dcp_dart_idle(struct t6040_sleep *s) } /* The internal display's three domains, child first, after its sub-blocks (the order). */ -/* A display pipe's PMC votes, released in the order; @rail the cluster rail bit or 0. */ -static int t6040_pmc_disp_release(struct t6040_sleep *s, unsigned int p, u32 rail) -{ - int ret = 0; - u32 v; - - if (s->pmc_bwr) - writel(readl(s->pmc_bwr + PMC_BOOST) & ~BIT(p), s->pmc_bwr + PMC_BOOST); - if (s->pmc_dvmr) { - writel(readl(s->pmc_dvmr) & ~BIT(p), s->pmc_dvmr); - if (readl_poll_timeout_atomic(s->pmc_dvmr + PMC_DVMR_ACK, v, !(v & BIT(0)), 1, - PMC_TIMEOUT_US)) - ret |= 1; - if (s->pmc_dcp) - writel(hweight32(readl(s->pmc_dvmr)), s->pmc_dcp + PMC_DCP_COUNT); - } - if (s->pmc_bwr) - writel(readl(s->pmc_bwr) & ~BIT(p ? p + 2 : 0), s->pmc_bwr); - if (rail && s->pmc_dcp) { - writel(readl(s->pmc_dcp + PMC_RAIL) & ~rail, s->pmc_dcp + PMC_RAIL); - if (readl_poll_timeout_atomic(s->pmc_dcp + PMC_RAIL_ACK, v, !(v & rail), 1, - PMC_TIMEOUT_US)) - ret |= 2; - } - return ret; -} - -/* As T8140: the coprocessor's CPU_CONTROL to 0, twice, right before its CPU word goes. */ -static void t6040_dcp_cold_stop(struct t6040_sleep *s, unsigned int p) -{ - if (!s->dcp_asc_ctl[p]) - return; - writel(0, s->dcp_asc_ctl[p]); - writel(0, s->dcp_asc_ctl[p]); -} - -/* One power word down in the two steps (any AUTO first). Returns 0x80 on a timeout. */ -static u32 t6040_soc_ps_off(void __iomem *w) -{ - u32 v = readl(w), cur; - u32 to = 0; - - if (!FIELD_GET(PS_ACTUAL, v) && !FIELD_GET(PS_TARGET, v)) - return 0; - if (v & PS_AUTO_ENABLE) { - writel(v & 0xeffffcff, w); - if (readl_poll_timeout_atomic(w, cur, FIELD_GET(PS_ACTUAL, cur) == 0xf, 1, - PMC_TIMEOUT_US)) - to = 0x80; - } - writel((readl(w) & 0xfffffcf0) | 0x300, w); - if (readl_poll_timeout_atomic(w, cur, !(cur & PS_BUSY), 1, PMC_TIMEOUT_US) || - readl_poll_timeout_atomic(w, cur, !FIELD_GET(PS_ACTUAL, cur), 1, PMC_TIMEOUT_US)) - to = 0x80; - return to; -} - -/* - * The stopped DCPEXT pipes down it, DCPEXT1 first. Marks 0x56SS00RR, - * SS 0x80 | pipe << 4 | step (1 PMC, 2 CPU, 3 FE, 4 SYS), RR 0x80 on a timeout. - */ -static void t6040_dispext_off(struct t6040_sleep *s) -{ - int n; - - if (!s->soc_ps) - return; - for (n = 1; n >= 0; n--) { - u32 base = 0x80 | n << 4; - void __iomem *bridge = s->axi2af_bridge[t6040_dispext[n].axi2af]; - - t6040_disp_mark(s, base | 1, 0); - if (READ_ONCE(pmc_seq)) - t6040_disp_mark(s, base | 1, - 0x1 | t6040_pmc_disp_release(s, n + 1, n ? 0 : PMC_RAIL_EXT) << 4); - t6040_disp_mark(s, base | 2, 0); - t6040_dcp_cold_stop(s, n + 1); - t6040_disp_mark(s, base | 2, 1); - t6040_disp_mark(s, base | 2, - 2 | t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].cpu)); - t6040_disp_mark(s, base | 3, - t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].fe)); - if (bridge) { - writel(readl(bridge + AXI2AF_BRIDGE_104) & ~BIT(0), bridge + AXI2AF_BRIDGE_104); - writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); - } - if (s->dispext_pwrgate) { - writel(3, s->dispext_pwrgate + t6040_dispext[n].pg_a); - writel(0, s->dispext_pwrgate + t6040_dispext[n].pg_b); - } - t6040_disp_mark(s, base | 4, - t6040_soc_ps_off(s->soc_ps + t6040_dispext[n].sys)); - } -} static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group *g) { @@ -5394,14 +4236,8 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { s->pmc_dcp_saved[0] = readl(s->pmc_dcp); s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); - if (READ_ONCE(pmc_seq)) { - t6040_disp_mark(s, 0x0a, 0); - t6040_disp_mark(s, 0x0a, t6040_pmc_disp_release(s, 0, PMC_RAIL_INT) << 4 | - (readl(s->pmc_dcp + PMC_RAIL) & 0xf)); - } else { - writel(0, s->pmc_dcp); - writel(0, s->pmc_dcp + 4); - } + writel(0, s->pmc_dcp); + writel(0, s->pmc_dcp + 4); t6040_disp_mark(s, 0x09, (s->pmc_dcp_saved[0] & 0xf) | (s->pmc_dcp_saved[1] & 0xf) << 4); } /* @@ -5418,32 +4254,8 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group !!FIELD_GET(PS_TARGET, v) << 5); if (FIELD_GET(PS_ACTUAL, v)) t6040_dcp_dart_idle(s); - if (READ_ONCE(disp_cpu_seq) && FIELD_GET(PS_ACTUAL, v)) { - if (s->ptd && READ_ONCE(pmp_disp_release)) { - u64 req = readq(s->ptd + PTD_REQ_READ), ack; - int to; - - t6040_disp_mark(s, 0x0d, (req >> PTD_DISP_INT) & 1); - writeq(req & ~BIT_ULL(PTD_DISP_INT), s->ptd + PTD_REQ_WRITE); - to = readq_poll_timeout_atomic(s->ptd + PTD_ACK, ack, - !(ack & BIT_ULL(PTD_DISP_INT)), 10, - 50000); - t6040_disp_mark(s, 0x0d, 0x10 | (to ? 0x80 : 0) | - ((ack >> PTD_DISP_INT) & 1)); - } - t6040_disp_mark(s, 0x0b, 0); - t6040_dcp_cold_stop(s, 0); - if (s->disp_pwrgate) { - writel(3, s->disp_pwrgate + DISP_PWRGATE_A); - writel(0, s->disp_pwrgate + DISP_PWRGATE_B); - } - t6040_disp_mark(s, 0x0b, 1); - t6040_psw_off(s, &cpu, 0x0c, NULL); - } ret = t6040_psw_poll_long(&cpu, PS_ACTUAL, 0, DISP_CPU_GATE_US); t6040_disp_mark(s, 0x10, ret ? 0x90 : 0x10); - if (ret && READ_ONCE(disp_need_cpu_gated)) - return ret; ret = 0; } if (!ret) @@ -5529,58 +4341,14 @@ static void t6040_disp_back(struct t6040_sleep *s) } } -static bool disp_probe; -module_param(disp_probe, bool, 0644); -MODULE_PARM_DESC(disp_probe, "Bring-up: at the commit point record the internal display's words and refuse"); - -/* - * The internal display as the DCP's stop left it: bits 0-10 the sub-blocks +0x8..+0x58 with - * TARGET != 0, 11-14 DISP_CPU ACTUAL, 15 its AUTO_ENABLE, 16-19 DISP_FE ACTUAL, 20-23 - * DISP_SYS ACTUAL. - */ -static u32 t6040_disp_probe_word(struct t6040_sleep *s) -{ - u32 w = 0, val, off; - unsigned int i; - - for (i = 0; i < s->ngroups; i++) { - const struct t6040_sleep_group *g = &s->groups[i]; - - if (strcmp(g->name, "disp")) - continue; - for (off = g->first + PS_STRIDE; off <= g->last && off - g->first <= 11 * PS_STRIDE; - off += PS_STRIDE) - if (!regmap_read(g->regmap, off, &val) && FIELD_GET(PS_TARGET, val)) - w |= BIT((off - g->first) / PS_STRIDE - 1); - if (!regmap_read(g->regmap, g->first, &val)) - w |= FIELD_GET(PS_ACTUAL, val) << 11 | !!(val & PS_AUTO_ENABLE) << 15; - } - if (s->soc_east) { - w |= FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_DISP_FE)) << 16; - w |= FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_DISP_SYS)) << 20; - } - return w; -} - static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) { - bool quiesce = READ_ONCE(axi2af_quiesce), with_cpu = READ_ONCE(lower_cpu); unsigned int i, left = 0; - /* - * The platform takes a DCPEXT pipe down CPU, FE, SYS (child first). The group loop below - * lowers FE and SYS and leaves the CPU, whose word then refuses the write: - * the DCPEXT pipes go first. - */ - if (READ_ONCE(dispext_off)) - t6040_dispext_off(s); - for (i = 0; i < s->ngroups; i++) { const struct t6040_sleep_group *g = &s->groups[i]; u32 off = g->last; - if (quiesce) - t6040_axi2af_quiesce(s, t6040_axi2af_of_group(g->name)); for (;;) { bool cpu = off == g->first; u32 val, cur; @@ -5592,7 +4360,7 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) t6040_mark_raw(s, T6040_DISP_MARK(i, t6040_regmap_poll_off(g->regmap, off, &cur) ? 3 : 2, off)); } - if ((!cpu || with_cpu) && !regmap_read(g->regmap, off, &val) && + if (!cpu && !regmap_read(g->regmap, off, &val) && t6040_sleep_ps_on(val, cpu)) { t6040_mark_raw(s, T6040_DISP_MARK(i, 0, off)); val &= ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS | PS_DEV_DISABLE | @@ -5658,10 +4426,6 @@ static u32 t6040_sleep_first_on(struct t6040_sleep *s) return 0; } -static bool axi2af_gate; -module_param(axi2af_gate, bool, 0644); -MODULE_PARM_DESC(axi2af_gate, "Refuse the commit while any axi2af domain is still powered (the panic gate)"); - /* Which of the 15 axi2af domains read ACTUAL != 0, one bit each. */ static u32 t6040_axi2af_on_mask(struct t6040_sleep *s) { @@ -5694,14 +4458,6 @@ static int t6040_sleep_enter(suspend_state_t state) return t6040_sleep_dry_enter(s, dry); t6040_mark(s, 0x05); - t6040_pmc_cp(s, 0); - if (READ_ONCE(disp_probe)) { - /* Bring-up: what the DCP's stop left, then refuse with it kept (0x55). */ - t6040_mark_raw(s, 0x55000000 | t6040_disp_probe_word(s)); - WRITE_ONCE(pm_bringup_dev_mark, NULL); - pr_info("t6040-sleep: display probe at the commit point; refusing\n"); - return -EBUSY; - } if (READ_ONCE(lower_display)) { unsigned int left = t6040_sleep_lower_display(s); @@ -5709,26 +4465,6 @@ static int t6040_sleep_enter(suspend_state_t state) if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); } - /* - * The PMGR quiesce panics unless all 15 axi2af domains read ACTUAL 0, and a commit with - * some on reset the SoC about a second after the S2R timer was armed. Refuse - * with the domains still on: 0x59 then a 15-bit mask, bit i = t6040_axi2af[i]. - */ - t6040_pmc_cp(s, 1); - if (READ_ONCE(axi2af_gate)) { - /* The commit lowers the ATC complexes (bits 0-3) and ANS (bit 9) itself. */ - u32 on = t6040_axi2af_on_mask(s) & - ~((READ_ONCE(lower_atc) ? GENMASK(3, 0) : 0) | BIT(9)); - - if (on) { - t6040_mark_raw(s, 0x59000000 | on); - WRITE_ONCE(pm_bringup_dev_mark, NULL); - pr_info("t6040-sleep: at the commit point: axi2af domains still on %#x; refusing\n", - on); - t6040_disp_back(s); - return -EBUSY; - } - } /* A dry run 1 ends here as well: its .begin told the firmware nothing. */ if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { unsigned int on = t6040_sleep_log_groups(s); @@ -5774,13 +4510,11 @@ static int t6040_sleep_enter(suspend_state_t state) return ret; } t6040_mark(s, 0x12); - t6040_pmc_cp(s, 2); s->pcie_ans_stopped = false; s->ans_bridge_quiesced = false; t6040_sleep_coprocs_stop(true); t6040_sleep_coprocs_stop(false); t6040_mark(s, 0x13); - t6040_pmc_cp(s, 3); t6040_wake_arm(s); t6040_mark(s, 0x14); @@ -6277,34 +5011,19 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->a2d_blk = t6040_ioremap_np(&pdev->dev, 0x502850000ULL, 0x1000); sleep->aop_req = t6040_ioremap_np(&pdev->dev, 0x5108b8000ULL, 0x1000); sleep->a2d_mm12 = t6040_ioremap_np(&pdev->dev, 0x50890c000ULL, 0x1000); - sleep->mtp_ps = t6040_ioremap_np(&pdev->dev, T6040_MTP_PS_PA, 8 * T6040_MTP_PS_WORDS); sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); sleep->snap_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); sleep->ps_saved = devm_kcalloc(&pdev->dev, ARRAY_SIZE(t6040_ps_snap_pa), sizeof(*sleep->ps_saved), GFP_KERNEL); - sleep->atc_aon = t6040_ioremap_np(&pdev->dev, 0x508320c00ULL, 0x200); for (i = 0; i < 16; i++) sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, 0x2000); - for (i = 0; i < 4; i++) - sleep->amcc_sem[i] = t6040_ioremap_np(&pdev->dev, - 0x2201c0000ULL + i * 0x2000000ULL, 0x1000); - sleep->pg_main = t6040_ioremap_np(&pdev->dev, 0x50282c000ULL, 0x1000); sleep->aop_asc = t6040_ioremap_np(&pdev->dev, 0x510600000ULL, 0x100); for (i = 0; i < 4; i++) sleep->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, 0x383200000ULL + i * 0x8000000ULL, 0x100); - sleep->pg_east = t6040_ioremap_np(&pdev->dev, 0x5022a0000ULL, 0x1000); - sleep->mini_ps = t6040_ioremap_np(&pdev->dev, 0x508300000ULL, 0x200); sleep->snap_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); - for (i = 0; i < ARRAY_SIZE(t6040_pmc_probe_pa); i++) - sleep->pmc_probe_io[i] = t6040_ioremap_np(&pdev->dev, t6040_pmc_probe_pa[i], 4); - for (i = 0; i < 3; i++) { - sleep->cpm_cfg[i] = t6040_ioremap_np(&pdev->dev, T6040_CPM_CFG_PA(i), CPM_CFG_STATE + 8); - sleep->cpm_impl[i] = t6040_ioremap_np(&pdev->dev, T6040_CPM_IMPL_PA(i), 8); - } - sleep->cpu4_impl110 = t6040_ioremap_np(&pdev->dev, T6040_CPU4_IMPL110_PA, 8); sleep->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); sleep->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); sleep->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); @@ -6319,11 +5038,7 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->dcp_dart = t6040_ioremap_np(&pdev->dev, T6040_DCP_DART_PA, 0x1000); sleep->disp_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISP_PWRGATE_PA, 0x100); sleep->pmc_dcp = t6040_ioremap_np(&pdev->dev, T6040_PMC_DCP_PA, 12); - sleep->pmc_dvmr = t6040_ioremap_np(&pdev->dev, T6040_PMC_DVMR_PA, 0x40); sleep->pmc_bwr = t6040_ioremap_np(&pdev->dev, T6040_PMC_BWR_PA, 0x10); - sleep->dispext_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISPEXT_PWRGATE_PA, 0x1000); - for (i = 0; i < ARRAY_SIZE(t6040_dcp_asc_ctl_pa); i++) - sleep->dcp_asc_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_dcp_asc_ctl_pa[i], 4); sleep->ptd = t6040_ioremap_np(&pdev->dev, T6040_PTD_PA, 0x11000); /* The DART clock-protection (GAPF) block, read-only here. */ sleep->gapf = t6040_ioremap_np(&pdev->dev, 0x500578000ULL, 0x1000); @@ -6339,7 +5054,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->tvm_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].ctl, 4); sleep->tvm_status[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].status, 4); } - sleep->pmc_ap_floor = t6040_ioremap_np(&pdev->dev, T6040_PMC_AP_FLOOR_PA, 8); sleep->afr_ps = t6040_ioremap_np(&pdev->dev, T6040_AFR_PS_PA, 4); if (sleep->ka_soc && sleep->ka_east) { u32 soc = 0, east = 0; From 92c2a2aff3924d3fdab6ec37ed25d9f56625381a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:25:34 +0100 Subject: [PATCH 073/269] soc: apple: t6040-sleep: make the default-on steps unconditional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each step the bring-up added to the commit, the wake return and the suspend callbacks got a parameter to switch it off again. All of them are on in the configuration that sleeps and wakes on the J616s: the generic PMGR quiesce with the GPU's TVMs, the early release of the stopped CPUs, the wait on the DISP_INT and DISP_EXT complexes, the AGX residency gate, the keep-alive bits, the external debug release before a core's last WFI, the SIO power words raised again after the wake, the second storage lane's SYS domain, ANS's fabric bridge, the wake stub, the ATC complexes and the GP PCIe ports, the display power words, the PMS DCP voters, the PMC display interfaces, and the DCS words' dynamic power gating and automatic mode in .begin. A display pipe's CPU word never counted as on. Remove the parameters and run the steps always, with the same writes in the same order. The DCS words that do not read on before they are put in automatic mode are now named in a warning; nothing logged them before. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 231 ++++++-------------------------- 1 file changed, 41 insertions(+), 190 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 729eeb3808b53..c7dba75a8174b 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -614,47 +614,6 @@ module_param(wd2_secs, uint, 0644); MODULE_PARM_DESC(wd2_secs, "Arm the AON WD2 for this many seconds from .begin to .end of a dry run 2 or 3 (0: off, at most 178)"); -static bool pmgr_quiesce = true; -module_param(pmgr_quiesce, bool, 0644); -MODULE_PARM_DESC(pmgr_quiesce, - "Run the steps of the generic PMGR quiesce in the commit: APSC off, PLL relock off, TVMs off, PMC DCS floor"); - -/* - * On the J616s, with the secondaries still spinning (stopped, coherent) until after - * the timer, the complexes +0xc/+0x10 never dropped BIT4 and the engine stalled. On - * The platform every other core is already off (self-disable + deep WFI) when the master - * turns the complexes off. Release the stopped cores to their last WFI at the top - * of the commit and wait for their core PS words to read off. - */ -static bool release_early = true; -module_param(release_early, bool, 0644); -MODULE_PARM_DESC(release_early, - "Release the stopped CPUs to their last WFI at the commit's start and wait until they are off"); - -static bool tvm_gpu = true; -module_param(tvm_gpu, bool, 0644); -MODULE_PARM_DESC(tvm_gpu, "With pmgr_quiesce: also the GPU PMGR's afr/gfx TVMs (0x288e94000..)"); - -/* - * cluster-ctl DISP_INT (+0xc) / DISP_EXT (+0x10): 0 leaves them as they are, 1 writes the - * nibble 0 and waits for BIT4 (the platform), 2 writes without waiting. Their CPU words can't be - * lowered from here, so the slots never finish under 1. - */ -static unsigned int disp_clusters = 1; -module_param(disp_clusters, uint, 0644); -MODULE_PARM_DESC(disp_clusters, - "DISP_INT/DISP_EXT cluster-ctl slots at the commit: 0 untouched, 1 off + wait, 2 off, no wait"); - -static bool agx_gate = true; -module_param(agx_gate, bool, 0644); -MODULE_PARM_DESC(agx_gate, - "Lower the GPU's AFR power word in the commit, as the PMGR quiesce does behind its AGX residency gate"); - -static bool keep_alive_off = true; -module_param(keep_alive_off, bool, 0644); -MODULE_PARM_DESC(keep_alive_off, - "Clear the 64 PMGR keep-alive bits right before the S2R timer, as the PMGR quiesce does"); - static struct t6040_sleep *t6040_sleep; static void t6040_iounmap(void *p) @@ -711,25 +670,13 @@ static const struct apple_sleep_ctl t6040_sleep_reg_ctl = { }; /* - * As in the power-state check, a sub-block is on while its TARGET is not 0. The - * pipe's CPU counts as on while its ACTUAL is not 0: the engine waits for the - * CPU's actual state. A CPU under automatic power gating (AUTO_ENABLE, target 15) - * that reads ACTUAL 0 is gated off by the PMGR and counts as off; taking it out - * of automatic mode raised an SError on T6040, so it is left so. + * As in the power-state check, a sub-block is on while its TARGET is not 0. A + * pipe's CPU word never counts: it stays under automatic power gating (taking it out + * of automatic mode raised an SError on T6040) and the SoC power-down drops it. */ -static bool ignore_display_cpu = true; -module_param(ignore_display_cpu, bool, 0644); -MODULE_PARM_DESC(ignore_display_cpu, - "Let the commit go on with the display pipes' CPU words on (the SoC power-down drops them)"); - static bool t6040_sleep_ps_on(u32 val, bool cpu) { - if (cpu && READ_ONCE(ignore_display_cpu)) - return false; - if (cpu) - return FIELD_GET(PS_ACTUAL, val) || - (FIELD_GET(PS_TARGET, val) && !(val & PS_AUTO_ENABLE)); - return FIELD_GET(PS_TARGET, val); + return !cpu && FIELD_GET(PS_TARGET, val); } static unsigned int t6040_sleep_show_group(struct seq_file *s, const struct t6040_sleep_group *g) @@ -968,11 +915,6 @@ static u32 t6040_cpu_start_bit(u64 mpidr) static void __iomem *t6040_ed[T6040_ED_CLUSTERS][T6040_ED_CORES]; -static bool ed_power_release = true; -module_param(ed_power_release, bool, 0644); -MODULE_PARM_DESC(ed_power_release, - "Before each core's deep WFI, unlock its external debug and clear EDPRCR"); - static void t6040_ed_map(struct device *dev) { unsigned int cpu, n = 0; @@ -996,7 +938,7 @@ static void t6040_ed_release(void) unsigned int c = MPIDR_AFFINITY_LEVEL(mpidr, 1), k = MPIDR_AFFINITY_LEVEL(mpidr, 0); void __iomem *ed; - if (!READ_ONCE(ed_power_release) || c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES) + if (c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES) return; ed = t6040_ed[c][k]; if (!ed) @@ -1479,9 +1421,6 @@ static const u32 t6040_ps_snap_pa[] = { */ #define T6040_PS_RESTORE_FIRST 0x2280150 /* I2C0 */ #define T6040_PS_RESTORE_LAST 0x2280228 /* UART6 */ -static bool ps_restore = true; -module_param(ps_restore, bool, 0644); -MODULE_PARM_DESC(ps_restore, "After a real sleep raise the SOC power words that were on at the commit and came back lower"); static void __iomem *t6040_ps_snap_addr(struct t6040_sleep *s, unsigned int i) { @@ -1512,7 +1451,7 @@ static void t6040_ps_restore(struct t6040_sleep *s) unsigned int i, n, raised = 0, failed = 0; u32 val, cur = 0; - if (!s->ps_saved_valid || !READ_ONCE(ps_restore)) + if (!s->ps_saved_valid) return; s->ps_saved_valid = false; for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { @@ -2099,15 +2038,11 @@ static void t6040_clusters_off(struct t6040_sleep *s) if (!s->cluster_ctl) return; for (i = 0; i < CLUSTER_CTL_NR; i++) { - unsigned int dc = READ_ONCE(disp_clusters); - val = readl(s->cluster_ctl + CLUSTER_CTL(i)); s->cluster_saved[i] = val; s->cluster_after[i] = val; - if (i >= CLUSTER_CTL_FIRST_WAIT && !dc) - continue; writel(val & ~CLUSTER_CTL_NIBBLE, s->cluster_ctl + CLUSTER_CTL(i)); - if (i >= CLUSTER_CTL_FIRST_WAIT && dc == 1 && + if (i >= CLUSTER_CTL_FIRST_WAIT && readl_poll_timeout_atomic(s->cluster_ctl + CLUSTER_CTL(i), val, !(val & CLUSTER_CTL_BUSY), 1, CLUSTER_TIMEOUT_US)) s->quiesce_to |= QTO_CLUSTER(i); @@ -2212,8 +2147,6 @@ static void t6040_pmgr_quiesce(struct t6040_sleep *s) for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { if (!s->tvm_ctl[i] || !s->tvm_status[i]) continue; - if (t6040_tvm[i].ctl >> 32 == 0x2 && !READ_ONCE(tvm_gpu)) - continue; t6040_mark(s, 0x320 | i); writel(readl(s->tvm_ctl[i]) & ~TVM_ENABLE, s->tvm_ctl[i]); if (readl_poll_timeout_atomic(s->tvm_status[i], v, !(v & TVM_BUSY), 1, @@ -2514,9 +2447,6 @@ static void t6040_apcie_clk_off(struct t6040_apcie_st *a) * second lane), and APCIE_PHY_SW's parents are both SYS_ST lanes. apcie_st1_on raises * APCIE_SYS_ST1 (target 0xf, no automatic mode) before the request; the engine lowers it. */ -static bool apcie_st1_on = true; -module_param(apcie_st1_on, bool, 0644); -MODULE_PARM_DESC(apcie_st1_on, "Bring-up: power APCIE_SYS_ST1 on before the S2R request (APCIE_PHY_SW's second parent)"); /* * With both SYS_ST lanes on the engine still waits on APCIE_PHY_SW bit 11. The platform never @@ -2861,8 +2791,6 @@ static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) return b ? b + t6040_dcs_ps[i].off : NULL; } -static unsigned int dcs_ps_auto = 1; - /* Returns a mask of the DCS whose ACTUAL did not reach f before AUTO was set. */ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) { @@ -2886,25 +2814,6 @@ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) return to; } -static int t6040_dcs_ps_auto_set(const char *val, const struct kernel_param *kp) -{ - int ret = param_set_uint(val, kp); - - if (!ret && (dcs_ps_auto & BIT(0)) && t6040_sleep) - dev_info(t6040_sleep->dev, "DCS PS automatic mode on (the platform boot parity), timeouts %#x\n", - t6040_dcs_ps_auto_on(t6040_sleep)); - return ret; -} - -static const struct kernel_param_ops t6040_dcs_ps_auto_ops = { - .set = t6040_dcs_ps_auto_set, - .get = param_get_uint, -}; -module_param_cb(dcs_ps_auto, &t6040_dcs_ps_auto_ops, &dcs_ps_auto, 0644); -MODULE_PARM_DESC(dcs_ps_auto, "Bring-up: bit 0 = DCS power-state words to automatic mode (PS_MIN 0) now and in .begin, as the platform at boot"); - -static unsigned int dcs_pg = 1; - static void t6040_dcs_dyn_pg(struct t6040_sleep *s) { int i; @@ -2920,24 +2829,6 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) } } -static int t6040_dcs_pg_set(const char *val, const struct kernel_param *kp) -{ - int ret = param_set_uint(val, kp); - - if (!ret && (dcs_pg & BIT(0)) && t6040_sleep) { - t6040_dcs_dyn_pg(t6040_sleep); - dev_info(t6040_sleep->dev, "DCS dynamic power-gate mode on (the platform parity)\n"); - } - return ret; -} - -static const struct kernel_param_ops t6040_dcs_pg_ops = { - .set = t6040_dcs_pg_set, - .get = param_get_uint, -}; -module_param_cb(dcs_pg, &t6040_dcs_pg_ops, &dcs_pg, 0644); -MODULE_PARM_DESC(dcs_pg, "Bring-up: bit 0 = DCS dynamic power-gate mode on (+0x20 |= 0x1100 on all 16 DCS) now and in .begin"); - /* Words 8-15 right before the S2R timer, then the whole record to PMU SRAM 0xa200. */ static void t6040_apcie_diag_store(struct t6040_sleep *s) { @@ -2962,17 +2853,13 @@ static void t6040_apcie_diag_store(struct t6040_sleep *s) * next one meets the bridge as the first wake armed it. Only while ANS is on: the bridge sits * in it. Returns whether the bit was set. */ -static bool ans_bridge_quiesce = true; -module_param(ans_bridge_quiesce, bool, 0644); -MODULE_PARM_DESC(ans_bridge_quiesce, - "Clear ANS's fabric-bridge bit 0 in the commit, once ANS is stopped, as the platform before the domain goes off"); static bool t6040_ans_bridge_quiesce(struct t6040_sleep *s) { void __iomem *bridge = s->axi2af_bridge[AXI2AF_ANS]; u32 a, b; - if (!READ_ONCE(ans_bridge_quiesce) || !bridge || !s->soc_east || + if (!bridge || !s->soc_east || FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != 0xf) return false; a = readl(bridge + AXI2AF_BRIDGE_104); @@ -3225,17 +3112,8 @@ asm( ); extern char t6040_resume_stub[]; -static bool resume_stub = true; -module_param(resume_stub, bool, 0644); -MODULE_PARM_DESC(resume_stub, - "Wake into a stub that restarts the AON WD2 for 60 s before cpu_resume (default on)"); - static u32 t6040_axi2af_on_mask(struct t6040_sleep *s); -static bool lower_atc = true; -module_param(lower_atc, bool, 0644); -MODULE_PARM_DESC(lower_atc, "Lower the four USB-C (ATC) complexes in the commit off"); - /* * The four ATC complexes' power words in SOC (soc_ps, 0x502800000), deepest first: cio_usb, * cio_pcie, cio, pcie, common (ADT: ATCn_CIO_USB .. ATCn_COMMON). The platform has them off at its @@ -3289,10 +3167,6 @@ static void t6040_sleep_lower_atc(struct t6040_sleep *s) } } -static bool lower_apcie_gp = true; -module_param(lower_apcie_gp, bool, 0644); -MODULE_PARM_DESC(lower_apcie_gp, "Stop the GP PCIe ports and lower apcie_sys_gp in the commit (T8140's radio port stop)"); - /* * T8140's radio port stop, on the same t602x port block: the link into L2, the endpoint's * PERST# asserted, the PHY's clocks off. The endpoint keeps its power and comes back by its @@ -3404,13 +3278,15 @@ static int t6040_sleep_commit(unsigned long unused) t6040_mark(s, 0x20); s->siq_cfg = read_sysreg_s(SYS_APL_SIQ_CFG_EL1); spin_table_stop_parked(); - if (READ_ONCE(release_early)) - spin_table_release_stopped(); + /* + * On the platform every other core is off, in its last WFI, before the boot core turns the + * complexes off. With the stopped cores still spinning the sleep engine stalled on the + * J616s: release them now, and wait for their power words to read off further down. + */ + spin_table_release_stopped(); t6040_mark(s, 0x21); - if (READ_ONCE(lower_atc)) - t6040_sleep_lower_atc(s); - if (READ_ONCE(lower_apcie_gp)) - t6040_sleep_stop_gp_ports(s); + t6040_sleep_lower_atc(s); + t6040_sleep_stop_gp_ports(s); /* * lower_io: ANS's two ST lanes + shared PHY down, once ANS has been @@ -3420,14 +3296,12 @@ static int t6040_sleep_commit(unsigned long unused) */ memset(s->apcie_diag, 0, sizeof(s->apcie_diag)); s->apcie_diag[1] = (s->pcie_ans_stopped ? BIT(3) : 0) | (s->apcie_st.valid ? BIT(4) : 0); - if (READ_ONCE(apcie_st1_on) && s->soc_east && + if (s->soc_east && t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1)) t6040_mark_raw(s, 0x54c00001); - if (s->pcie_ans_stopped) { + if (s->pcie_ans_stopped) t6040_sleep_lower_apcie_st(s); - } - if (READ_ONCE(lower_apcie_gp)) - t6040_sleep_lower_apcie_gp(s); + t6040_sleep_lower_apcie_gp(s); /* * What the quiesce gate would panic on, after everything here has lowered what it @@ -3461,16 +3335,11 @@ static int t6040_sleep_commit(unsigned long unused) * already; the wake side re-asserts them. */ s->quiesce_to = 0; - if (READ_ONCE(release_early)) { - t6040_mark(s, 0x2c); - s->quiesce_to |= min(t6040_secondaries_off(s), 15U) << 20; - } - if (READ_ONCE(pmgr_quiesce)) { - t6040_mark(s, 0x2b); - t6040_pmgr_quiesce(s); - } - if (READ_ONCE(agx_gate)) - t6040_mark(s, 0x2a0 | t6040_agx_gate(s)); + t6040_mark(s, 0x2c); + s->quiesce_to |= min(t6040_secondaries_off(s), 15U) << 20; + t6040_mark(s, 0x2b); + t6040_pmgr_quiesce(s); + t6040_mark(s, 0x2a0 | t6040_agx_gate(s)); t6040_mark(s, 0x24); t6040_clusters_off(s); @@ -3479,14 +3348,13 @@ static int t6040_sleep_commit(unsigned long unused) t6040_mark(s, 0x350); t6040_clkgen_park(s); t6040_mark(s, 0x351); - if (READ_ONCE(keep_alive_off)) - s->ka_on_before = t6040_keep_alive(s, false, 0); + s->ka_on_before = t6040_keep_alive(s, false, 0); t6040_mark(s, 0x25); /* Arm aura's AURAS2R1 mailbox with this boot's resume entry. */ if (s->mbox) { WRITE_ONCE(s->mbox->resume_pa, - READ_ONCE(resume_stub) && s->wdt_stub_ok ? + s->wdt_stub_ok ? __pa_symbol(t6040_resume_stub) : __pa_symbol(cpu_resume)); dcache_clean_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); dsb(sy); @@ -3893,6 +3761,7 @@ static int t6040_sleep_begin(suspend_state_t state) * full set at the commit point, by which time the display is off. */ unsigned int begin_total = live - disp; unsigned int dry = READ_ONCE(dry_run), secs; + u32 dcs_to; /* * The PM core calls .begin and .end for suspend-to-idle too when there @@ -3909,10 +3778,11 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_aop_ps_save(s); t6040_susf_begin(s); t6040_sleep_pmvc(s); - if (READ_ONCE(dcs_pg) & BIT(0)) - t6040_dcs_dyn_pg(s); - if (READ_ONCE(dcs_ps_auto) & BIT(0)) - t6040_dcs_ps_auto_on(s); + t6040_dcs_dyn_pg(s); + dcs_to = t6040_dcs_ps_auto_on(s); + if (dcs_to) + pr_warn("t6040-sleep: DCS power words %#x did not read on before automatic mode\n", + dcs_to); pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", live, disp); t6040_sleep_log_blockers(s); @@ -4039,9 +3909,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) * each word as the pmgr-pwrstate driver powers a domain off (TARGET 0, the one-shot bits * clear), polled to ACTUAL 0. Returns the number of words left on. */ -static bool lower_display = true; -module_param(lower_display, bool, 0644); -MODULE_PARM_DESC(lower_display, "Lower the display power words at the S2R commit point"); #define PS_DEV_DISABLE BIT(10) #define PS_BUSY BIT(11) @@ -4053,20 +3920,8 @@ MODULE_PARM_DESC(lower_display, "Lower the display power words at the S2R commit */ #define T6040_DISP_MARK(g, st, off) (0x5c000000 | ((g) & 0xff) << 16 | (st) << 12 | ((off) & 0xfff)) -static bool disp_seq_off = true; - #define DISP_CPU_GATE_US 50000 -static bool pmc_if_restore = true; -module_param(pmc_if_restore, bool, 0644); -MODULE_PARM_DESC(pmc_if_restore, "After a real sleep set the display pipes' PMC BWR and boost interface enables again"); - -static bool pmc_dcp_off = true; -module_param(pmc_dcp_off, bool, 0644); -MODULE_PARM_DESC(pmc_dcp_off, "Drop the PMS DCP voters to 0 before the internal display goes down"); -module_param(disp_seq_off, bool, 0644); -MODULE_PARM_DESC(disp_seq_off, "Lower DISP_CPU, DISP_FE and DISP_SYS at the commit point as the PMGR does"); - /* * regmap_read_poll_timeout_atomic() reads ktime_get(), which warns while timekeeping is * suspended (12 warnings from .enter on the J616s). Counted 1 us steps instead. @@ -4233,7 +4088,7 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group s->pmc_if_valid = true; } /* The PMS DCP voters to 0 first, as the entry and T8140's driver end. */ - if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { + if (s->pmc_dcp) { s->pmc_dcp_saved[0] = readl(s->pmc_dcp); s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); writel(0, s->pmc_dcp); @@ -4323,11 +4178,11 @@ static void t6040_disp_back(struct t6040_sleep *s) /* DISP_CPU's word can't be written from here: automatic mode raises it. */ (void)g; } - if (s->pmc_dcp && READ_ONCE(pmc_dcp_off)) { + if (s->pmc_dcp) { writel(s->pmc_dcp_saved[1], s->pmc_dcp + 4); writel(s->pmc_dcp_saved[0], s->pmc_dcp); } - if (s->pmc_if_valid && READ_ONCE(pmc_if_restore)) { + if (s->pmc_if_valid) { u32 bwr = readl(s->pmc_bwr), boost = readl(s->pmc_bwr + PMC_BOOST); s->pmc_if_valid = false; @@ -4379,8 +4234,7 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) break; off -= PS_STRIDE; } - if (!i && !strcmp(g->name, "disp") && READ_ONCE(disp_seq_off) && - t6040_disp_off(s, g)) + if (!i && !strcmp(g->name, "disp") && t6040_disp_off(s, g)) left++; } return left; @@ -4451,20 +4305,17 @@ static int t6040_sleep_prepare_late(void) static int t6040_sleep_enter(suspend_state_t state) { struct t6040_sleep *s = t6040_sleep; - unsigned int dry = READ_ONCE(t6040_sleep_dry); + unsigned int dry = READ_ONCE(t6040_sleep_dry), left; int ret; if (dry) return t6040_sleep_dry_enter(s, dry); t6040_mark(s, 0x05); - if (READ_ONCE(lower_display)) { - unsigned int left = t6040_sleep_lower_display(s); - - t6040_mark(s, 0x06); - if (left) - pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); - } + left = t6040_sleep_lower_display(s); + t6040_mark(s, 0x06); + if (left) + pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); /* A dry run 1 ends here as well: its .begin told the firmware nothing. */ if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { unsigned int on = t6040_sleep_log_groups(s); From b936dc04473dad358711c189995754b5489b8422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:26:41 +0100 Subject: [PATCH 074/269] soc: apple: t6040-sleep: fold the bit-mask parameters to the tested values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seven steps were steered by a mask or a mode parameter, each bit one stage of the bring-up. The values the J616s sleeps and wakes with are the defaults: - the axi2af bridges are re-armed where a domain's power word reads target 0xf and is on; - the AOP island's power words are put back at the wake, those that were on before the sleep at a manual target 0xf; - the storage PCIe is quiesced in this order: the shared PHY to sleep, APCIE_PHY_SW clock-gated, the PLL off, and APCIE_GP is left clock-gated instead of off; - the fabric power words are raised to automatic mode with target 0xf, the four ATC commons are raised under FAB2_SOC, and every ATC port that has not asked for its clock gate gets the PHY's sleep entry; - the wake raises the storage PCIe domains in order with both fabric bridges, APCIE_PHY_SW and the reference PLL wait, and leaves APCIE_SYS_GP at a manual target; - both GP PCIe ports get their clocks back at the wake; - SMC key pmVC is written 0x101 in .begin. Remove the parameters, the stages that were off (the T8140 order of the PHY and clock quiesce and of the wake raise, the automatic level write and the hand-held fabrics, the record of the PCIe quiesce) and keep the rest as plain code with the same writes in the same order. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 316 +++++++------------------------- 1 file changed, 68 insertions(+), 248 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index c7dba75a8174b..dd03db3b8aef2 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -526,7 +526,6 @@ struct t6040_sleep { void __iomem *a2d_mini, *a2d_mini4, *a2d_mm8, *a2d_pmc, *a2d_blk; void __iomem *a2d_mm12; /* MasterMini 0x50890c000 (IB2 engine + handshake +0x14) */ void __iomem *aop_req; /* AOP island power requests 0x5108b8100 (bit 0 DRAM, 1 AP, 2 CPU) */ - u32 apcie_diag[16]; /* apcie_seq bit 2: 'APCS' + flags + PHY/PS words, PMU 0xa200 */ void __iomem *dcs[16]; /* DCS(n,k): 0x220200000 + n * 0x2000000 + k * 0x200000 */ void __iomem *aop_asc; /* AOP ASC 0x510600000 (CPU_CONTROL +0x44) */ void __iomem *atc_cfg[4]; /* ATCPHY_CFG(n) 0x383200000 + n * 0x8000000 */ @@ -1531,10 +1530,6 @@ static void t6040_ret_mark(struct t6040_sleep *s, u32 n); #define T6040_AOPW_PMU 0xa2c0 #define T6040_AOPW_MAGIC 0x57504f41 /* "AOPW" in PMU byte order */ -static unsigned int aop_wake = 15; -module_param(aop_wake, uint, 0644); -MODULE_PARM_DESC(aop_wake, "Bring-up wake: bit0 record the AOP island ('AOPW' at PMU 0xa2c0), bit1 power it back as saved in .begin, bit2 words on before the sleep to manual target 0xf, bit3 read the ASC CPU_CONTROL (marks 0x58/0x59)"); - static void t6040_aop_ps_save(struct t6040_sleep *s) { int i; @@ -1565,15 +1560,15 @@ static unsigned int t6040_aop_ps_count_on(struct t6040_sleep *s, u32 *nib) static void t6040_aop_wake(struct t6040_sleep *s) { - unsigned int mode = READ_ONCE(aop_wake), before, after, to = 0; + unsigned int before, after, to = 0; u32 w[10] = { T6040_AOPW_MAGIC }; int k, i; - if (!mode || !s->aop_ps) + if (!s->aop_ps) return; before = t6040_aop_ps_count_on(s, &w[2]); w[8] = w[9] = 0; - if ((mode & BIT(1)) && s->aop_ps_saved_valid) { + if (s->aop_ps_saved_valid) { for (k = ARRAY_SIZE(t6040_aop_ps_order) - 1; k >= 0; k--) { u16 off = t6040_aop_ps_order[k]; void __iomem *r = s->aop_ps + off; @@ -1584,7 +1579,7 @@ static void t6040_aop_wake(struct t6040_sleep *s) * driver reads its ASC. bit2: every word that was on before the sleep goes to * manual target 0xf (AUTO off) and stays up through the resume. */ - bool manual = (mode & BIT(2)) && FIELD_GET(PS_ACTUAL, want); + bool manual = FIELD_GET(PS_ACTUAL, want); if (manual) { if (FIELD_GET(PS_ACTUAL, now) == 0xf && !(now & PS_AUTO_ENABLE)) @@ -1605,7 +1600,7 @@ static void t6040_aop_wake(struct t6040_sleep *s) } after = t6040_aop_ps_count_on(s, NULL); w[1] = before | after << 8 | min(to, 0xffffU) << 16; - if ((mode & BIT(0)) && s->spmi0) + if (s->spmi0) for (i = 0; i < 10; i++) t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], NULL, 0); @@ -1617,7 +1612,7 @@ static void t6040_aop_wake(struct t6040_sleep *s) * wake (nothing later gated it). AOPW w8 then becomes CPU_CONTROL, w9 the AOP * requests 0x5108b8100 [15:0] | MasterMini+0x14 << 16. */ - if ((mode & BIT(3)) && s->aop_asc && s->spmi0) { + if (s->aop_asc && s->spmi0) { u32 ctl, rq; rq = (s->aop_req ? readl(s->aop_req + 0x100) & 0xffff : 0xffff) | @@ -2259,25 +2254,14 @@ static void t6040_axi2af_readcheck(struct t6040_sleep *s) } /* - * The wake-side axi2af re-arm: every retained domain powered back up (PS - * TARGET == 0xf) gets its fabric bridge +0x104/+0x108 bit0 set, or the bridge - * drops its traffic. The highest-risk wake write. + * The wake-side axi2af re-arm: every retained domain powered back up (PS TARGET 0xf and + * ACTUAL on) gets its fabric bridge +0x104/+0x108 bit0 set, or the bridge drops its + * traffic. A word in automatic mode can read TARGET 0xf with its domain gated; the bridge + * is then off too (the first resume on the J616s took an SError in this step), so such a + * domain is left alone. */ -/* - * On the J616s the first real resume took an asynchronous SError in here (mark 0x36 landed, 0x37 - * did not). Mode 1 re-arms every domain with TARGET 0xf, as the wake restore does; mode 2 also - * wants ACTUAL on, since an AUTO word can read TARGET 0xf with the domain gated, and its - * bridge is then off too; 0 skips the re-arm. Each domain is marked before (0x5eII01VV) and - * after (0x5eII09VV) its bridge writes, VV the PS word's low byte (ACTUAL << 4 | TARGET). - */ -static unsigned int axi2af_rearm_mode = 2; -module_param(axi2af_rearm_mode, uint, 0644); -MODULE_PARM_DESC(axi2af_rearm_mode, - "Bring-up wake: axi2af bridge re-arm 0 off, 1 TARGET 0xf (as the platform), 2 TARGET 0xf and ACTUAL on"); - static void t6040_axi2af_rearm(struct t6040_sleep *s) { - unsigned int mode = READ_ONCE(axi2af_rearm_mode); int i; for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { @@ -2288,9 +2272,9 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) if (!ps || !bridge) continue; val = readl(ps); - if (!mode || FIELD_GET(PS_TARGET, val) != 0xf) + if (FIELD_GET(PS_TARGET, val) != 0xf) continue; - if (mode >= 2 && !FIELD_GET(PS_ACTUAL, val)) { + if (!FIELD_GET(PS_ACTUAL, val)) { pr_info("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", t6040_axi2af[i].name, val); continue; @@ -2390,35 +2374,6 @@ static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_t } } -/* The shared PHY quieted (as T8140's t8140_apcie_phy_off): both clocks off, run bits down. */ -static void t6040_apcie_phy_off(struct t6040_apcie_st *a) -{ - void __iomem *ctrl = a->phy + APCIE_PHY_CTRL; - void __iomem *run = a->phy + APCIE_PHY_RUN; - u32 val; - - writel(readl(ctrl) | APCIE_PHY_CTRL_RESET, ctrl); - writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK1REQ, ctrl); - readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US); - writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK0REQ, ctrl); - readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US); - writel(readl(ctrl) & ~APCIE_PHY_CTRL_UP, ctrl); - writel(readl(run) & ~APCIE_PHY_RUN_EN2, run); - writel(readl(run) & ~APCIE_PHY_RUN_EN, run); -} - -/* The shared clkgen off (as T8140's t8140_apcie_clk_off). */ -static void t6040_apcie_clk_off(struct t6040_apcie_st *a) -{ - void __iomem *clk = a->clkgen + APCIE_CLK_CTRL; - u32 val; - - writel(readl(clk) & ~APCIE_CLK_RUN, clk); - writel(readl(clk) | APCIE_CLK_OFF, clk); - readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); - writel(readl(clk) | APCIE_CLK_OFF2, clk); -} - /* * ENTRY: lower ANS's storage link, the two-lane adaptation of * t8140_sleep_lower_apcie(). Called from the commit's lower_io only once ANS @@ -2459,12 +2414,6 @@ static void t6040_apcie_clk_off(struct t6040_apcie_st *a) * Flags (word 1): 0 S1 ran, 1 PHY_SW gate ran, 2 S3 ran, 3 ANS stopped, 4 windows valid, * 5 GP clock-gated; timeouts 8 CLK1ACK, 9 CLK0ACK, 16 PHY_SW bit 11, 17 clkgen busy, 18 GP. */ -static unsigned int apcie_seq = 11; -module_param(apcie_seq, uint, 0644); -MODULE_PARM_DESC(apcie_seq, "Bring-up: storage PCIe quiesce as the platform (bit 0 PHY sleep, 1 APCIE_PHY_SW gate + PLL off, 2 record at PMU 0xa200, 3 APCIE_GP clock-gated)"); - -#define T6040_APCIE_DIAG_PMU 0xa200 -#define T6040_APCIE_DIAG_MAGIC 0x53435041 /* "APCS" in PMU byte order */ /* S1, the root complex disable: the shared PHY and PHY-IP to sleep; the platform spins unbounded. */ static void t6040_apcie_phy_off(struct t6040_sleep *s) @@ -2474,7 +2423,6 @@ static void t6040_apcie_phy_off(struct t6040_sleep *s) void __iomem *run = a->phy + APCIE_PHY_RUN; u32 val; - s->apcie_diag[2] = readl(ctrl); writel(readl(ctrl) | APCIE_PHY_CTRL_UP, ctrl); udelay(10); writel(readl(a->phy_ip + 0x38) & ~GENMASK(27, 26), a->phy_ip + 0x38); @@ -2483,20 +2431,15 @@ static void t6040_apcie_phy_off(struct t6040_sleep *s) writel(readl(ctrl) | APCIE_PHY_CTRL_RESET, ctrl); udelay(1); writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK1REQ, ctrl); - if (readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US)) - s->apcie_diag[1] |= BIT(8); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US); udelay(1); writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK0REQ, ctrl); - if (readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US)) - s->apcie_diag[1] |= BIT(9); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US); udelay(1); writel(readl(ctrl) & ~APCIE_PHY_CTRL_UP, ctrl); udelay(10); writel(readl(run) & ~APCIE_PHY_RUN_EN2, run); writel(readl(run) & ~APCIE_PHY_RUN_EN, run); - s->apcie_diag[3] = readl(ctrl); - s->apcie_diag[4] = readl(run); - s->apcie_diag[1] |= BIT(0); } /* S2 step 1, gate idx 7 off: APCIE_PHY_SW gated while APCIE_GP is still on. */ @@ -2505,12 +2448,8 @@ static void t6040_apcie_phy_sw_gate(struct t6040_sleep *s) void __iomem *reg = s->soc_east + PS_APCIE_PHY_SW; u32 val; - s->apcie_diag[5] = readl(reg); writel(0x0f000244, reg); - if (readl_poll_timeout_atomic(reg, val, !(val & PS_BUSY), 1, PS_OFF_TIMEOUT_US)) - s->apcie_diag[1] |= BIT(16); - s->apcie_diag[6] = readl(reg); - s->apcie_diag[1] |= BIT(1); + readl_poll_timeout_atomic(reg, val, !(val & PS_BUSY), 1, PS_OFF_TIMEOUT_US); } /* S3, the PCIe PLL shutdown: PCIECLKGEN +0 as the platform. */ @@ -2522,11 +2461,8 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) writel((readl(clk) & ~GENMASK(5, 3)) | BIT(4) | BIT(3), clk); writel(readl(clk) | BIT(1), clk); writel(readl(clk) & ~BIT(0), clk); - if (readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US)) - s->apcie_diag[1] |= BIT(17); + readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); writel(readl(clk) | BIT(2), clk); - s->apcie_diag[7] = readl(clk); - s->apcie_diag[1] |= BIT(2); } /* @@ -2561,9 +2497,6 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) * Bit 3 holds FAB1/2/4/7_SOC on by hand (AUTO off, target f, ACTUAL f polled) through the * timer; record word 1/2 bits 16+i name them (bit 1 is not used with it). */ -static unsigned int fab_on = 5; -module_param(fab_on, uint, 0644); -MODULE_PARM_DESC(fab_on, "Bring-up: fabric PS words before the S2R timer (bit 0 AUTO + target f as the D2A expects, bit 1 automatic level f, bit 2 ATC commons on under FAB2; records at PMU 0xa240/0xa280)"); #define T6040_ATC_PMU 0xa280 #define T6040_ATC_MAGIC 0x43435441 /* "ATCC" in PMU byte order */ @@ -2613,16 +2546,13 @@ static void t6040_atc_commons_on(struct t6040_sleep *s, u32 *timeouts) * +0x28 bit 0 set. So every port in the mask whose +0x28 bit 0 is still clear gets the three * writes; the default mask is all four. */ -static unsigned int atc_s2r = 0xf; -module_param(atc_s2r, uint, 0644); -MODULE_PARM_DESC(atc_s2r, "Port mask: the CFG writes of the PHY S2R entry (+0x2c, +0x08, +0x28 |= 1, poll +0x04 bit 8) right before the S2R timer on each port that has not asked for its clock gate yet"); static bool atc_rec = true; module_param(atc_rec, bool, 0644); MODULE_PARM_DESC(atc_rec, "Bring-up: record the 4 ATCPHY_CFG blocks at PMU 0xa240 right before the S2R timer (replaces 'FABS')"); #define T6040_ATCF_MAGIC 0x46435441 /* "ATCF" in PMU byte order */ -static u32 t6040_atc_s2r_entry(struct t6040_sleep *s, unsigned int mask) +static u32 t6040_atc_s2r_entry(struct t6040_sleep *s) { u32 to = 0, v; int n; @@ -2630,7 +2560,7 @@ static u32 t6040_atc_s2r_entry(struct t6040_sleep *s, unsigned int mask) for (n = 0; n < 4; n++) { void __iomem *c = s->atc_cfg[n]; - if (!(mask & BIT(n)) || !c || (readl(c + 0x28) & BIT(0))) + if (!c || (readl(c + 0x28) & BIT(0))) continue; writel(readl(c + 0x2c) | BIT(0), c + 0x2c); udelay(1); @@ -2699,13 +2629,12 @@ static const struct { { false, 0x298 } /* FAB_AFR */, }; -#define PS_AUTO_LEVEL GENMASK(27, 24) #define T6040_FAB_PMU 0xa240 #define T6040_FAB_MAGIC 0x53424146 /* "FABS" in PMU byte order */ static void t6040_fabrics_on(struct t6040_sleep *s) { - u32 rec[16] = { T6040_FAB_MAGIC }, atc_to = 0; + u32 rec[16] = { T6040_FAB_MAGIC }, atc_to; int i; for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { @@ -2722,34 +2651,10 @@ static void t6040_fabrics_on(struct t6040_sleep *s) if (t6040_ps_raise(b, off)) rec[2] |= BIT(i); } - v = readl(b + off); - if ((READ_ONCE(fab_on) & BIT(1)) && FIELD_GET(PS_AUTO_LEVEL, v) != 0xf) { - rec[1] |= BIT(16 + i); - writel((v & ~(PS_AUTO_LEVEL | PS_FLAGS)) | FIELD_PREP(PS_AUTO_LEVEL, 0xf), - b + off); - if (readl_poll_timeout_atomic(b + off, v, FIELD_GET(PS_ACTUAL, v) == 0xf, 1, - PS_OFF_TIMEOUT_US)) - rec[2] |= BIT(16 + i); - } rec[3 + i] = readl(b + off); } - if (READ_ONCE(fab_on) & BIT(2)) - t6040_atc_commons_on(s, &rec[2]); - if (READ_ONCE(atc_s2r) & 0xf) - atc_to = t6040_atc_s2r_entry(s, READ_ONCE(atc_s2r) & 0xf); - if (READ_ONCE(fab_on) & BIT(3)) { - for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { - void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; - u32 off = t6040_fabrics[i].off; - - if (!b || (off != 0x2d0 && off != 0x338 && off != 0x488 && - !(t6040_fabrics[i].east && off == 0x128))) - continue; - rec[1] |= BIT(16 + i); - if (t6040_ps_raise_start(b, off)) - rec[2] |= BIT(16 + i); - } - } + t6040_atc_commons_on(s, &rec[2]); + atc_to = t6040_atc_s2r_entry(s); for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; @@ -2829,22 +2734,6 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) } } -/* Words 8-15 right before the S2R timer, then the whole record to PMU SRAM 0xa200. */ -static void t6040_apcie_diag_store(struct t6040_sleep *s) -{ - static const u16 w[8] = { PS_APCIE_PHY_SW, PS_APCIE_SYS_ST0, PS_APCIE_SYS_ST1, - PS_APCIE_ST0, PS_APCIE_ST1, PS_ANS, PS_APCIE_SYS_GP, - PS_APCIE_GP }; - int i; - - s->apcie_diag[0] = T6040_APCIE_DIAG_MAGIC; - for (i = 0; i < 8; i++) - s->apcie_diag[8 + i] = s->soc_east ? readl(s->soc_east + w[i]) : 0xdeadbeef; - for (i = 0; i < ARRAY_SIZE(s->apcie_diag); i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCIE_DIAG_PMU + 4 * i, SPMI_OP_WRITE4), - &s->apcie_diag[i], NULL, 0); -} - /* * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The platform clears the bit before a * retained domain goes off and sets it again once it is back up; here the wake sets it @@ -2876,26 +2765,16 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; void __iomem *ps = s->soc_east; - unsigned int mac = READ_ONCE(apcie_seq); if (!a->valid || !ps) return; /* The PHY's registers need a lane domain on; quiet it first (shared). */ - if (a->phy && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) { - if (mac & BIT(0)) - t6040_apcie_phy_off(s); - else - t6040_apcie_phy_off(a); - } - if (mac & BIT(1)) - t6040_apcie_phy_sw_gate(s); - if (a->clkgen && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) { - if (mac & BIT(1)) - t6040_apcie_pll_off(s); - else - t6040_apcie_clk_off(a); - } + if (a->phy && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) + t6040_apcie_phy_off(s); + t6040_apcie_phy_sw_gate(s); + if (a->clkgen && t6040_ps_actual_on(ps, PS_APCIE_SYS_ST0)) + t6040_apcie_pll_off(s); /* AXI enables off (shared). */ if (a->axi) { @@ -2927,10 +2806,6 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) * common +0 bit31) before the PHY init. apcie_wake bit0: that domain order and both bridges; * bit1: APCIE_PHY_SW on and the refpll wait. Marks 0x50..0x55 follow the steps. */ -static unsigned int apcie_wake = 7; -module_param(apcie_wake, uint, 0644); -MODULE_PARM_DESC(apcie_wake, - "Bring-up wake: bit0 the platform gate order ANS,GP,SYS_GP,ST0,SYS_ST0,ST1,SYS_ST1 + axi2af bits; bit1 PHY_SW on + refpll wait; bit2 SYS_GP without AUTO"); static void t6040_bridge_rearm(void __iomem *bridge) { @@ -2945,7 +2820,6 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) struct t6040_apcie_st *a = &s->apcie_st; void __iomem *ps = s->soc_east; void __iomem *ctrl, *run; - unsigned int wake = READ_ONCE(apcie_wake); u32 val; int ret; @@ -2954,48 +2828,30 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) ctrl = a->phy + APCIE_PHY_CTRL; run = a->phy + APCIE_PHY_RUN; - if (wake & BIT(0)) { - ret = t6040_ps_raise(ps, PS_ANS); - if (!ret) - t6040_bridge_rearm(s->axi2af_bridge[AXI2AF_ANS]); - ret |= t6040_ps_raise(ps, PS_APCIE_GP); - /* - * The 'APCW' record: after the raise SYS_GP read 0x1000030f (AUTO on, auto state 0, - * ACTUAL 0): AUTO gated it again and the ANS apcie DART (0x4_1000_0000) behind it - * took the SError in its noirq resume. bit2 leaves SYS_GP in manual target 0xf, - * as GP/ST0/ST1 read (0x2ff). - */ - if (!ret) - ret = (wake & BIT(2)) ? t6040_ps_raise_start(ps, PS_APCIE_SYS_GP) : - t6040_ps_raise(ps, PS_APCIE_SYS_GP); - if (!ret) { - writel(readl(a->axi + APCIE_AXI_EN0) | BIT(0), a->axi + APCIE_AXI_EN0); - writel(readl(a->axi + APCIE_AXI_EN1) | BIT(0), a->axi + APCIE_AXI_EN1); - } - ret |= t6040_ps_raise(ps, PS_APCIE_ST0); - ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST0); - ret |= t6040_ps_raise(ps, PS_APCIE_ST1); - ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST1); - t6040_ret_mark(s, 0x50); - s->apcie_wake_step = 1; - if (ret) - return ret; - } else { - /* Domains up: ANS first, then both lanes (st then sys_st), AXI enables. */ - ret = t6040_ps_raise(ps, PS_ANS); - ret |= t6040_ps_raise(ps, PS_APCIE_ST0); - ret |= t6040_ps_raise(ps, PS_APCIE_ST1); - ret |= t6040_ps_raise_start(ps, PS_APCIE_SYS_ST0); - ret |= t6040_ps_raise_start(ps, PS_APCIE_SYS_ST1); + /* + * The order, each domain to ACTUAL 0xf: ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, + * the fabric-bridge bit0s set after ANS and after SYS_GP. SYS_GP stays at a manual + * target 0xf: in automatic mode it gated again on the J616s, and the DART behind it + * then faulted in its resume. + */ + ret = t6040_ps_raise(ps, PS_ANS); + if (!ret) + t6040_bridge_rearm(s->axi2af_bridge[AXI2AF_ANS]); + ret |= t6040_ps_raise(ps, PS_APCIE_GP); + if (!ret) + ret = t6040_ps_raise_start(ps, PS_APCIE_SYS_GP); if (!ret) { writel(readl(a->axi + APCIE_AXI_EN0) | BIT(0), a->axi + APCIE_AXI_EN0); writel(readl(a->axi + APCIE_AXI_EN1) | BIT(0), a->axi + APCIE_AXI_EN1); } - t6040_ps_raise_finish(ps, PS_APCIE_SYS_ST0); - t6040_ps_raise_finish(ps, PS_APCIE_SYS_ST1); + ret |= t6040_ps_raise(ps, PS_APCIE_ST0); + ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST0); + ret |= t6040_ps_raise(ps, PS_APCIE_ST1); + ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST1); + t6040_ret_mark(s, 0x50); + s->apcie_wake_step = 1; if (ret) return ret; - } /* Tunables + clkgen (shared). Both OFF bits clear; only a power-off cleared them. */ apple_tunable_apply(a->axi, a->axi_tunable); @@ -3007,29 +2863,24 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) a->clkgen + APCIE_CLK_CTRL); ret = readl_poll_timeout_atomic(a->clkgen + APCIE_CLK_CTRL, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); - if (wake) - t6040_ret_mark(s, 0x51); + t6040_ret_mark(s, 0x51); s->apcie_wake_step = 2; if (ret) return ret; - if (wake & BIT(1)) { - ret = t6040_ps_raise(ps, PS_APCIE_PHY_SW); - s->apcie_wake_step = 3; - if (ret) - return ret; - } + ret = t6040_ps_raise(ps, PS_APCIE_PHY_SW); + s->apcie_wake_step = 3; + if (ret) + return ret; /* Shared PHY bring-up + PLL/lane tables (as T8140's t8140_apcie_phy_ip_apply). */ apple_tunable_apply(a->phy, a->phy_tunable); - if (wake & BIT(1)) { - ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_COMMON, val, val & BIT(31), 1, - APCIE_POLL_US); - t6040_ret_mark(s, 0x52); - s->apcie_wake_step = 4; - if (ret) - return ret; - } + ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_COMMON, val, val & BIT(31), 1, + APCIE_POLL_US); + t6040_ret_mark(s, 0x52); + s->apcie_wake_step = 4; + if (ret) + return ret; writel(readl(ctrl) | APCIE_PHY_CTRL_CLK0REQ, ctrl); ret = readl_poll_timeout_atomic(ctrl, val, val & APCIE_PHY_CTRL_CLK0ACK, 1, APCIE_POLL_US); if (ret) @@ -3039,8 +2890,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) if (ret) return ret; writel(readl(ctrl) & ~APCIE_PHY_CTRL_RESET, ctrl); - if (wake) - t6040_ret_mark(s, 0x53); + t6040_ret_mark(s, 0x53); s->apcie_wake_step = 5; writel(readl(run) | APCIE_PHY_RUN_EN, run); @@ -3053,13 +2903,11 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) APCIE_POLL_US); if (ret) return ret; - if (wake) - t6040_ret_mark(s, 0x54); + t6040_ret_mark(s, 0x54); s->apcie_wake_step = 6; writel(readl(a->phy + APCIE_PHY_COMMON) | BIT(0), a->phy + APCIE_PHY_COMMON); writel(readl(ctrl) | APCIE_PHY_CTRL_UP, ctrl); - if (wake) - t6040_ret_mark(s, 0x55); + t6040_ret_mark(s, 0x55); s->apcie_wake_step = 7; /* RC ready handshake (as T8140's). */ @@ -3260,14 +3108,8 @@ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) t6040_mark_raw(s, 0x54300002 | (ret ? 0x80 : 0)); t6040_mark_raw(s, 0x54400001); - if (READ_ONCE(apcie_seq) & BIT(3)) { - writel(0x244, gp); - ret = readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, - PS_OFF_TIMEOUT_US); - s->apcie_diag[1] |= BIT(5) | (ret ? BIT(18) : 0); - } else { - ret = t6040_ps_lower_word(gp); - } + writel(0x244, gp); + ret = readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, PS_OFF_TIMEOUT_US); t6040_mark_raw(s, 0x54400002 | (ret ? 0x80 : 0)); } @@ -3294,8 +3136,6 @@ static int t6040_sleep_commit(unsigned long unused) * here it follows every refusal, so a refused sleep finds the link up. * No-op unless the apple,apcie windows are present (apcie_st.valid). */ - memset(s->apcie_diag, 0, sizeof(s->apcie_diag)); - s->apcie_diag[1] = (s->pcie_ans_stopped ? BIT(3) : 0) | (s->apcie_st.valid ? BIT(4) : 0); if (s->soc_east && t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1)) t6040_mark_raw(s, 0x54c00001); @@ -3360,13 +3200,10 @@ static int t6040_sleep_commit(unsigned long unused) dsb(sy); } - if (READ_ONCE(fab_on)) - t6040_fabrics_on(s); + t6040_fabrics_on(s); t6040_ps_save(s); if (READ_ONCE(ps_snap)) t6040_ps_snap_store(s); - if (READ_ONCE(apcie_seq) & BIT(2)) - t6040_apcie_diag_store(s); /* Last before the S2R timer: what is still on, re-read now, kept through the reset. */ s->commit_on_mask |= t6040_axi2af_on_mask(s); @@ -3441,14 +3278,8 @@ MODULE_PARM_DESC(apcie_wake_rec, "Bring-up: record the wake-side ANS/APCIe raise #define GP_PORT_STATUS 0x804 #define GP_PORT_STATUS_READY BIT(0) -static unsigned int pcie_port_clk = 3; -module_param(pcie_port_clk, uint, 0644); -MODULE_PARM_DESC(pcie_port_clk, - "Bring-up wake: GP PCIe ports (mask) whose refclk + app clock are brought back after the apcie raise"); - static void t6040_gp_pcie_clk(struct t6040_sleep *s) { - unsigned int mask = READ_ONCE(pcie_port_clk); u32 res = 0, val; int i; @@ -3456,8 +3287,6 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) void __iomem *phy = s->gp_phy[i], *port = s->gp_port[i]; u32 r = 0, cfg, app; - if (!(mask & BIT(i))) - continue; if (!phy || !port || !s->gp_saved[i]) { res |= BIT(7) << (8 * i); continue; @@ -3600,15 +3429,11 @@ static void t6040_sleep_return(struct t6040_sleep *s) if (s->pcie_ans_stopped) s->pcie_ans_on_ret = t6040_sleep_raise_apcie_st(s); s->gp_port_res = 0; - if (READ_ONCE(pcie_port_clk)) { - t6040_gp_pcie_clk(s); - t6040_ret_mark(s, 0x56); - } + t6040_gp_pcie_clk(s); + t6040_ret_mark(s, 0x56); t6040_apcie_wake_rec(s); - if (READ_ONCE(aop_wake)) { - t6040_aop_wake(s); - t6040_ret_mark(s, 0x57); - } + t6040_aop_wake(s); + t6040_ret_mark(s, 0x57); t6040_ret_mark(s, 0x38); if (s->pwrgate) { @@ -3675,11 +3500,9 @@ static bool t6040_sleep_mem_begun; * the sleep transition would race the engine. pmvc != 0 writes that value in .begin and logs * the key before and after. */ -static unsigned int pmvc = 0x101; -module_param(pmvc, uint, 0644); -MODULE_PARM_DESC(pmvc, "Bring-up: value for SMC key pmVC (VDD_CIO request, the platform 0x101) written in .begin; 0 = leave it"); /* The .begin log does not reach the journal; keep the result in PMU SRAM 0xa2ac. */ +#define T6040_PMVC_REQUEST 0x101 #define T6040_PMVC_PMU 0xa2ac #define T6040_PMVC_MAGIC 0x43564d50 /* "PMVC" in PMU byte order */ @@ -3745,10 +3568,7 @@ MODULE_PARM_DESC(pmvc_now, "Bring-up: write this value to SMC key pmVC now and l static void t6040_sleep_pmvc(struct t6040_sleep *s) { - u32 v = READ_ONCE(pmvc); - - if (v) - t6040_sleep_pmvc_write(s, v, true); + t6040_sleep_pmvc_write(s, T6040_PMVC_REQUEST, true); } static int t6040_sleep_begin(suspend_state_t state) From a341ee67a2cdda0e477f148221dced9213ad56d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:35:15 +0100 Subject: [PATCH 075/269] soc: apple: t6040-sleep: remove the progress marks, records, probes and dumps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bring-up watched a sleep through instrumentation that outlived it: - progress marks written into the PMU's RTC comparator from the driver, from the PM core's per-device hook and from a panic notifier, and the wake alarm saved and put back around them. They were off by default (a mark takes the alarm's place), so the alarm was never touched; - records in the PMU's scratch memory: the map of power words that are on, the ATC PHY blocks, the AOP island at the wake, the storage PCIe raise, the pmVC key. Nothing in the kernel reads them back; - the reads that only fed them: the comparator at probe, the A2D, AOP and ANE state at probe and in a dry run, the axi2af mask and the AOP's ASC control word in the commit and at the wake, and the pmVC key before and after its write; - the parameters that logged the ATC PHY blocks or wrote pmVC on demand, and the debugfs file that wrote a display power word. Remove them, with the parameters, windows, struct members and the syscore and panic hooks that served nothing else. Every poll keeps its place and its bound; its result is dropped where only a mark took it. The barriers that came before each mark of the wake return stay, as t6040_wake_sync(), so that the order and the timing of that path are as tested. apple_sleep_bringup_mark() stays as an empty function for the display and GPU drivers that still call it. What the wake needs is kept as plain code. The SIO power words that were on at the commit are saved in memory and raised again; only that range is read now, not every power word of the SoC. The AOP island's words are put back as before. A failed storage PCIe raise, a GP PCIe clock that does not come back and an AOP power word that stays off were only visible in a record: each is a warning after the wake now, as is a failed pmVC write. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 1858 ++++++------------------------- 1 file changed, 328 insertions(+), 1530 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index dd03db3b8aef2..7b11bc89ab382 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -57,7 +57,6 @@ #include #include #include -#include #include #include #include @@ -67,7 +66,6 @@ #include #include #include -#include #include #include @@ -90,6 +88,8 @@ #define PS_OFF_TIMEOUT_US 5000 #define PS_STRIDE 8 #define PS_MAX_PER_GROUP 16 +#define PS_SIO_FIRST 0x150 /* SOC-East: I2C0, the first SIO peripheral word */ +#define PS_SIO_LAST 0x228 /* SOC-East: UART6, the last */ /* * ANS's storage PCIe block: the TWO ST lanes (apcie_st0/apcie_st1) and their @@ -113,7 +113,6 @@ #define APCIE_AXI_EN1 0x108 #define APCIE_CLK_CTRL 0x0 #define APCIE_CLK_BUSY BIT(31) -#define APCIE_CLK_RUN BIT(5) #define APCIE_CLK_OFF BIT(1) #define APCIE_CLK_OFF2 BIT(2) #define APCIE_CLK_START 0x4 @@ -249,8 +248,7 @@ * BIT0 := 0, status polled until BIT1 clears; * The platform then drops the AP voter's DCS floor vote in the PMC. That step is left out: the * first access to that word hung the bus on the J616s. - * The platform polls the APSC and TVM words without a timeout; here every poll is bounded - * and a timeout sets a bit of the commit's 0x5d mark. + * The platform polls the APSC and TVM words without a timeout; here every poll is bounded. */ static const u64 t6040_acc_apsc_pa[] = { 0x210e20020ULL, 0x211e20020ULL, 0x212e20020ULL }; #define ACC_APSC_DISABLE BIT_ULL(23) @@ -296,12 +294,6 @@ static const struct { #define PTD_ACK 0x1e80 #define QUIESCE_TIMEOUT_US 2000 -/* The 0x5d mark's timeout bits. */ -#define QTO_APSC(c) BIT(c) -#define QTO_TVM(i) BIT(3 + (i)) -#define QTO_CLUSTER(i) BIT(12 + (i) - CLUSTER_CTL_FIRST_WAIT) -#define QTO_EAST_CLK(i) BIT(14 + (i)) -#define QTO_MUX(i) BIT(16 + (i)) #define AGX_GATE_RESIDENT GENMASK(19, 18) #define T6040_AFR_PS_PA 0x288e80100ULL #define AFR_LEVEL GENMASK(19, 16) @@ -322,10 +314,6 @@ static const struct { #define SPMI_NOTIFY_ENABLE 0x40 #define SPMI_NOTIFY_ACK 0x80 #define SPMI_NOTIFY_REPLY BIT(0) -#define SPMI_CMD_PMU(reg, op) (((u32)(reg) << 16) | BIT(15) | (0xe << 8) | (op)) -#define SPMI_OP_WRITE4 0x33 -#define SPMI_OP_READ4 0x3b -#define PMU_RTC_COMPARE 0x2008 #define SPMI_CMD_WRITE (BIT(15) | (0xe << 8) | 0x30) /* low 16 bits 0x8e30 */ #define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 #define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ @@ -399,19 +387,11 @@ static const struct { * and the ADT (simple-reconfig-wdog-icc-time = 5). Linux's own * watchdog driver runs WD1 (+0x10..+0x1c), not this one. */ -#define WDT_WD0_COUNT 0x00 -#define WDT_WD0_BITE 0x04 #define WDT_WD0_CTL 0x0c -#define WDT_WD1_COUNT 0x10 -#define WDT_WD1_BITE 0x14 -#define WDT_WD1_CTL 0x1c -#define WDT_CTL_RESET_EN BIT(2) #define WDT_WD2_COUNT 0x20 #define WDT_WD2_ALARM 0x28 #define WDT_WD2_CTL 0x2c #define WDT_TICKS_PER_S 24000000 -/* At the commit, as the reconfig. */ -#define WDT_SLEEP_SECS 5 /* From .begin to .end: the device suspend and resume, as on T8140. */ #define WDT_SUSPEND_SECS 120 /* ALARM is 32 bits at 24 MHz. */ @@ -498,13 +478,9 @@ struct t6040_sleep { void __iomem *wdt; /* The DT's wdt block is the one t6040_resume_stub hardcodes. */ bool wdt_stub_ok; - /* Bring-up marks: the PMU RTC comparator saved at .enter, put back (+1 s) before the commit. */ - u32 alarm_saved, alarm_hdr; - bool alarm_ok; /* The commit's and the wake's SoC blocks (see the #defines). Optional. */ void __iomem *cluster_ctl; void __iomem *pwrgate; - u32 commit_on_mask; /* axi2af domains still on at the commit (0: none) */ void __iomem *dcp_dart; /* dart-dcp, for its transaction idle wait */ void __iomem *disp_pwrgate; /* internal display retention window 0x5022a1000 */ bool disp_down; /* t6040_disp_off() lowered the internal display */ @@ -522,17 +498,14 @@ struct t6040_sleep { void __iomem *spmi0; void __iomem *spmi_a0; void __iomem *mastermini; - /* Bring-up A2D watch windows (read-only, MCPS/MPMGR A2D steps, iBoot rcfg seq 8/24). */ - void __iomem *a2d_mini, *a2d_mini4, *a2d_mm8, *a2d_pmc, *a2d_blk; - void __iomem *a2d_mm12; /* MasterMini 0x50890c000 (IB2 engine + handshake +0x14) */ - void __iomem *aop_req; /* AOP island power requests 0x5108b8100 (bit 0 DRAM, 1 AP, 2 CPU) */ void __iomem *dcs[16]; /* DCS(n,k): 0x220200000 + n * 0x2000000 + k * 0x200000 */ - void __iomem *aop_asc; /* AOP ASC 0x510600000 (CPU_CONTROL +0x44) */ void __iomem *atc_cfg[4]; /* ATCPHY_CFG(n) 0x383200000 + n * 0x8000000 */ void __iomem *aop_ps; /* MiniPMGR AOP + audio island PS words 0x508304000..0x158 */ - void __iomem *snap_soc, *snap_east; /* PS snapshot pages 0x502800000 / 0x502280000 */ - u32 *ps_saved; /* each PS word at the commit, see t6040_ps_restore() */ - bool ps_saved_valid; + /* The SOC and SOC-East power word pages at their fixed addresses. */ + void __iomem *pmgr_soc; /* 0x502800000: fabrics, ATC commons, DCS */ + void __iomem *pmgr_east; /* 0x502280000: fabrics, DCS, SIO */ + u32 sio_ps_saved[(PS_SIO_LAST - PS_SIO_FIRST) / PS_STRIDE + 1]; + bool sio_ps_saved_valid; void __iomem *soc_east; void __iomem *soc_ps; void __iomem *aop_global; @@ -551,19 +524,14 @@ struct t6040_sleep { u64 ka_on_before; /* keep-alive bits set before the commit cleared them */ void __iomem *smc_wdt_deadline; void __iomem *agx_gate; - void __iomem *ane_sys_ps; /* pmgr +0x3c0 */ - void __iomem *ane_ps; /* pmgr_ane: mpm, cpu, td, base */ void __iomem *acc_apsc[3]; void __iomem *tvm_ctl[8]; void __iomem *tvm_status[8]; void __iomem *pmc_bwr; void __iomem *ptd; void __iomem *gapf; - u32 quiesce_to; /* timeout bits of this commit's quiesce polls */ void __iomem *afr_ps; - u32 afr_saved; u32 cluster_saved[CLUSTER_CTL_NR]; - u32 cluster_after[CLUSTER_CTL_NR]; u32 wake_mask_saved[AOP_WAKE_WORDS]; bool wake_mask_saved_valid; u32 wake_status[AOP_WAKE_WORDS]; @@ -576,7 +544,8 @@ struct t6040_sleep { u32 apcie_wake_step; u32 gp_phy_saved[2], gp_appclk_saved[2], gp_perst_saved[2]; bool gp_saved[2]; - u32 gp_port_res; + unsigned int gp_clk_timeouts; + unsigned int aop_ps_timeouts; u32 aop_ps_saved[44]; bool aop_ps_saved_valid; struct t6040_sleep_watch watch[T6040_WATCH_COUNT]; @@ -1039,152 +1008,6 @@ static void t6040_sleep_clear_wd2(struct t6040_sleep *s) s->wd2_armed = false; } -/* - * The PACC1_ANE complex (cluster-ctl +0x8) holds P1 and the ANE: after the S2R timer - * E reads 0x00 and P1 stays 0x10. The ANE's power words, ACTUAL nibbles: - * sys (pmgr +0x3c0), then mpm, cpu, td, base (pmgr_ane +0x0..+0x18). Returns them - * packed sys<<16 | mpm<<12 | cpu<<8 | td<<4 | base; 0xfffff without windows. - */ -#define T6040_ANE_SYS_PS_PA 0x5022803c0ULL -#define T6040_ANE_PS_PA 0x50228c000ULL - -static u32 t6040_ane_actual(struct t6040_sleep *s) -{ - u32 v, i; - - if (!s->ane_sys_ps || !s->ane_ps) - return 0xfffff; - v = FIELD_GET(GENMASK(7, 4), readl(s->ane_sys_ps)) << 16; - for (i = 0; i < 4; i++) - v |= FIELD_GET(GENMASK(7, 4), readl(s->ane_ps + i * 8)) << (12 - 4 * i); - return v; -} - -/* - * Bring-up: how far the hardware A2D got, from registers its programs write or poll - * (iBoot's reconfig programs, MPMGR seq 8 and MCPS seq 24): - * [27:20] progress code 0x508360114 (MPMGR: 4, 0x10, then 5, 7) - * [19:18] MasterMini+0x8214 bits 25:24 (MPMGR handshake acks) - * [17] PMC request 0x5048aa13c bit 0 (MCPS := 1), [16] ack 0x5048aa140 bit 31 - * [15:14] MiniPMGR 0x508364004 bits 1:0 (MCPS handshake) - * [13] 0x502850020 bit 0 (MCPS writes it after progress == 0x10) - * [12:9] GPIO PS 0x502800450 ACTUAL (MCPS powers it on first) - * [8:5] SIO_CPU PS 0x502280130 ACTUAL, [4] its BIT31 (MCPS := 0x80000000 after - * the SIO poll 0x429e08000 bit 1) - * [3] DPA4 PS 0x502280250 TARGET 0 (last of the group-1 domains MCPS turns off) - * [2] PS 0x502280120 TARGET 0 (turned off right after group 1) - * Bits [1:0] are left for a sample count. Every window is optional (0 bits). - */ -static unsigned int a2d_read_mask = 0x3f; -module_param(a2d_read_mask, uint, 0644); -MODULE_PARM_DESC(a2d_read_mask, - "Bring-up: A2D state windows read (bit 0 MiniPMGR 0x114, 1 MasterMini 0x8214, 2 PMC 0x5048aa13c/140, 3 0x508364004, 4 0x502850020, 5 PS words)"); - -static u32 t6040_a2d_state(struct t6040_sleep *s) -{ - u32 v = 0, w, m = READ_ONCE(a2d_read_mask); - - if (s->a2d_mini && (m & BIT(0))) - v |= (readl(s->a2d_mini + 0x114) & 0xff) << 20; - if (s->a2d_mm8 && (m & BIT(1))) - v |= ((readl(s->a2d_mm8 + 0x214) >> 24) & 3) << 18; - if (s->a2d_pmc && (m & BIT(2))) { - v |= (readl(s->a2d_pmc + 0x13c) & 1) << 17; - v |= (readl(s->a2d_pmc + 0x140) >> 31) << 16; - } - if (s->a2d_mini4 && (m & BIT(3))) - v |= (readl(s->a2d_mini4 + 0x4) & 3) << 14; - if (s->a2d_blk && (m & BIT(4))) - v |= (readl(s->a2d_blk + 0x20) & 1) << 13; - if (s->soc_ps && (m & BIT(5))) - v |= FIELD_GET(PS_ACTUAL, readl(s->soc_ps + 0x450)) << 9; - if (s->soc_east && (m & BIT(5))) { - w = readl(s->soc_east + 0x130); - v |= FIELD_GET(PS_ACTUAL, w) << 5 | (w >> 31) << 4; - v |= !FIELD_GET(PS_TARGET, readl(s->soc_east + 0x250)) << 3; - v |= !FIELD_GET(PS_TARGET, readl(s->soc_east + 0x120)) << 2; - } - return v; -} - -/* - * Bring-up: the AOP's power requests against the MPMGR's first A2D handshake (AOP firmware: - * AOP-local 0x5108b8100 bit 0 DRAM (status MasterMini+0x14 bit 0), bit 1 AP - * and bit 2 AOP CPU (status MiniPMGR 0x508360108)). Mark tag 1: - * [27:20] progress 0x508360114, [19:18] MasterMini+0x8214 acks 25:24, - * [17:15] AOP requests 0x5108b8100 [2:0], [14] MasterMini+0x14 bit 0, - * [13:10] MasterMini+0x8214 bits 31 (REQ), 16, and MasterMini+0xc014 (IB2) bits 31, 24; - * [9:6] MasterMini+0xc014 bit 25, MiniPMGR +0x104 [2:0]; [5:2] MiniPMGR +0x108 [3:0]; - * [1:0] sample count - */ -static u32 t6040_aop_state(struct t6040_sleep *s) -{ - u32 v = 0; - - if (s->a2d_mini) { - v |= (readl(s->a2d_mini + 0x114) & 0xff) << 20; - v |= (readl(s->a2d_mini + 0x104) & 7) << 6; - v |= (readl(s->a2d_mini + 0x108) & 0xf) << 2; - } - if (s->a2d_mm8) { - u32 h1 = readl(s->a2d_mm8 + 0x214); - - v |= ((h1 >> 24) & 3) << 18; - v |= (h1 >> 31) << 13 | ((h1 >> 16) & 1) << 12; - } - if (s->a2d_mm12) { - u32 h2 = readl(s->a2d_mm12 + 0x14); - - v |= (h2 >> 31) << 11 | ((h2 >> 24) & 1) << 10 | ((h2 >> 25) & 1) << 9; - } - if (s->aop_req) - v |= (readl(s->aop_req + 0x100) & 7) << 15; - if (s->mastermini) - v |= (readl(s->mastermini + 0x14) & 1) << 14; - return v; -} - -static void t6040_log_a2d(struct t6040_sleep *s, const char *when) -{ - pr_info("t6040-sleep: %s: A2D state %#010x (0x2 tag in marks), AOP state %#010x (0x1 tag), raw AOP req %#x MiniPMGR+0x100/104/108 %#x/%#x/%#x MasterMini+0x14 %#x +0x8214 %#x +0xc014 %#x\n", - when, t6040_a2d_state(s), t6040_aop_state(s), - s->aop_req ? readl(s->aop_req + 0x100) : 0, - s->a2d_mini ? readl(s->a2d_mini + 0x100) : 0, s->a2d_mini ? readl(s->a2d_mini + 0x104) : 0, - s->a2d_mini ? readl(s->a2d_mini + 0x108) : 0, - s->mastermini ? readl(s->mastermini + 0x14) : 0, - s->a2d_mm8 ? readl(s->a2d_mm8 + 0x214) : 0, s->a2d_mm12 ? readl(s->a2d_mm12 + 0x14) : 0); - /* - * The MiniPMGR handshake words the reconfig programs use (iBoot rcfg: MCPS A2D waits - * on +0x44 bit 2 / bit 3 and +0x40 bit 8, D2A raises +0x40/+0x3c/+0x34, D2S +0x48/+0x4c), - * read only, and the five cluster-ctl slots (EACC, PACC0, PACC1_ANE, DISP_INT, DISP_EXT). - */ - if (s->a2d_mini) { - static const u16 hs[] = { 0x08, 0x1c, 0x24, 0x34, 0x38, 0x3c, 0x40, 0x44, 0x48, 0x4c, 0x5c, 0x114, 0x200 }; - u32 v[ARRAY_SIZE(hs)], c[CLUSTER_CTL_NR] = {}; - int i; - - for (i = 0; i < ARRAY_SIZE(hs); i++) - v[i] = readl(s->a2d_mini + hs[i]); - if (s->cluster_ctl) - for (i = 0; i < CLUSTER_CTL_NR; i++) - c[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); - pr_info("t6040-sleep: %s: MiniPMGR +08 %#x +1c %#x +24 %#x +34 %#x +38 %#x +3c %#x +40 %#x +44 %#x +48 %#x +4c %#x +5c %#x +114 %#x +200 %#x; cluster-ctl %#x %#x %#x %#x %#x\n", - when, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11], v[12], - c[0], c[1], c[2], c[3], c[4]); - } -} - -static void t6040_log_ane(struct t6040_sleep *s, const char *when) -{ - u32 c[3] = {}, i; - - if (s->cluster_ctl) - for (i = 0; i < 3; i++) - c[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); - pr_info("t6040-sleep: %s: ANE ACTUAL sys/mpm/cpu/td/base %05x, cluster-ctl E %#x P0 %#x P1 %#x\n", - when, t6040_ane_actual(s), c[0], c[1], c[2]); -} - /* * In process context, after the device suspend: which watched devices are * bound. The commit point runs with interrupts off and only reads power words. @@ -1220,16 +1043,11 @@ static bool t6040_sleep_watch_on(struct t6040_sleep_watch *w, u32 *val) * with its sleep request left here, ANS stopped only by its driver, and the * display pipes off. Logs each failure and returns the count. */ -/* Why the last t6040_sleep_refuse() refused, one bit per check (the refusal mark). */ -static u32 t6040_refuse_why; -static u32 t6040_sleep_groups_on(struct t6040_sleep *s); - static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) { struct t6040_sleep_watch *sep = &s->watch[T6040_WATCH_SEP]; struct t6040_sleep_watch *ans = &s->watch[T6040_WATCH_ANS]; unsigned int refused = 0, ready = 0, registered = 0; - u32 why = 0; bool ans_idled = false; u32 val; int i; @@ -1248,7 +1066,6 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) pr_err("t6040-sleep: refused: %s is running and its driver has not quiesced it\n", dev_name(c->dev)); refused++; - why |= BIT(0); } } pr_info("t6040-sleep: %u of %u registered coprocessors ready for the commit\n", ready, @@ -1258,7 +1075,6 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { pr_err("t6040-sleep: refused: wrong CPU, other CPUs online, or no mailbox\n"); refused++; - why |= BIT(1); } /* @@ -1272,7 +1088,6 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) !!s->cluster_ctl, !!s->pwrgate, !!s->mastermini, !!s->spmi0, !!s->soc_east, !!s->aop_global); refused++; - why |= BIT(2); } if (!sep->bound) { @@ -1280,18 +1095,15 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) t6040_sleep_watch_on(sep, &val); pr_err("t6040-sleep: refused: sep isn't bound (state %#x)\n", val); refused++; - why |= BIT(3); } else if (!apple_sleep_enclave.stop) { pr_err("t6040-sleep: refused: sep's sleep request isn't left for the last refusal\n"); refused++; - why |= BIT(3); } /* Only ANS's driver can stop it, and it doesn't come back without a real sleep. */ if (!ans_idled && t6040_sleep_watch_on(ans, &val)) { pr_err("t6040-sleep: refused: ANS is on (%#x) with no driver to stop it\n", val); refused++; - why |= BIT(4); } /* @@ -1302,15 +1114,11 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) if (ans_idled && !s->apcie_st.valid) { pr_err("t6040-sleep: refused: ANS would stop but the storage-apcie windows/tunables are absent (apple,apcie)\n"); refused++; - why |= BIT(5); } /* The engine waits for the display CPUs with no timeout. */ - if (t6040_sleep_log_groups(s)) { + if (t6040_sleep_log_groups(s)) refused++; - why |= BIT(6) | t6040_sleep_groups_on(s) << 8; - } - t6040_refuse_why = why; return refused; } @@ -1351,6 +1159,14 @@ static int t6040_sleep_enclave_tell(void) return ret; } +/* + * The AOP and audio island's power words (MiniPMGR 0x508304000 + 0x000..0x158). The island + * is off after a sleep, and the AOP's resume, which reads its ASC first, then faulted on the + * J616s. The words are read in .begin and put back at the wake, parents first: a word that + * was on goes to a manual target 0xf (in automatic mode it gated again before the AOP's + * driver got to its ASC), any other gets its saved value if that asks for power. The table + * is the power-down order, children first. + */ #define T6040_AOP_PS_PA 0x508304000ULL static const u16 t6040_aop_ps_order[] = { 0x0b0, 0x128, 0x120, 0x158, 0x150, 0x148, 0x140, 0x138, 0x130, 0x118, 0x0c0, 0x0c8, @@ -1359,107 +1175,80 @@ static const u16 t6040_aop_ps_order[] = { 0x048, 0x058, 0x060, 0x088, 0x050, 0x010, 0x018, 0x008, 0x000, }; -/* - * Bring-up: which SOC / SOC-East power words are still on right before the S2R timer. - * The reset that follows a stalled A2D loses everything but the PMU, so the bitmap (bit i = - * ACTUAL != 0 of t6040_ps_snap_pa[i], ADT order) goes into PMU SRAM at the unused tail of - * the SOCD container: 0xa2c0 'PSNP' + count, then the bitmap words. m4-s2r-psnap decodes it. - */ -static int t6040_pmu_cmd(struct t6040_sleep *s, u32 cmd, const u32 *tx, u32 *rx, - unsigned int nrx); - -static bool ps_snap = true; -module_param(ps_snap, bool, 0644); -MODULE_PARM_DESC(ps_snap, "Bring-up: store the on-bitmap of the SOC/SOC-East PS words in PMU SRAM 0xa2c0 right before the S2R timer"); - -/* Generated from the live J616s ADT: SOC + SOC-East PS words, display CPU words left out. */ -static const u32 t6040_ps_snap_pa[] = { - 0x2280000, 0x2280100, 0x2280108, 0x2280110, 0x2280118, 0x2280120, - 0x2280128, 0x2280130, 0x2280138, 0x2280140, 0x2280148, 0x2280150, - 0x2280158, 0x2280160, 0x2280168, 0x2280170, 0x2280178, 0x2280180, - 0x2280188, 0x2280190, 0x2280198, 0x22801a0, 0x22801a8, 0x22801b0, - 0x22801b8, 0x22801c0, 0x22801c8, 0x22801d0, 0x22801d8, 0x22801e0, - 0x22801e8, 0x22801f0, 0x22801f8, 0x2280200, 0x2280208, 0x2280210, - 0x2280218, 0x2280220, 0x2280228, 0x2280230, 0x2280238, 0x2280240, - 0x2280248, 0x2280250, 0x2280258, 0x2280260, 0x2280268, 0x2280270, - 0x2280278, 0x2280280, 0x2280288, 0x2280290, 0x2280298, 0x22802e0, - 0x22802e8, 0x22802f0, 0x22802f8, 0x2280300, 0x2280308, 0x2280310, - 0x2280318, 0x2280368, 0x2280370, 0x2280378, 0x2280380, 0x2280388, - 0x2280390, 0x2280398, 0x22803a0, 0x22803c0, 0x2280c00, 0x228c000, - 0x228c008, 0x228c010, 0x228c018, 0x2800000, 0x2800008, 0x2800010, - 0x2800018, 0x2800020, 0x2800028, 0x2800030, 0x2800038, 0x2800040, - 0x2800048, 0x2800050, 0x2800058, 0x2800060, 0x2800068, 0x2800070, - 0x2800078, 0x2800080, 0x2800088, 0x2800090, 0x2800100, 0x2800108, - 0x2800110, 0x2800118, 0x2800120, 0x2800128, 0x2800130, 0x2800138, - 0x2800140, 0x2800148, 0x2800150, 0x2800158, 0x2800160, 0x2800168, - 0x2800170, 0x2800178, 0x2800180, 0x2800188, 0x2800190, 0x2800198, - 0x28001a0, 0x28001a8, 0x28001b0, 0x28001b8, 0x28001c0, 0x28001c8, - 0x28001d0, 0x28001d8, 0x28001e0, 0x28001e8, 0x28001f0, 0x28001f8, - 0x2800200, 0x2800278, 0x2800288, 0x2800290, 0x2800298, 0x28002d0, - 0x2800328, 0x2800330, 0x2800338, 0x2800340, 0x2800348, 0x2800390, - 0x2800398, 0x28003a0, 0x28003a8, 0x28003b0, 0x28003b8, 0x28003c0, - 0x28003c8, 0x28003d0, 0x28003d8, 0x28003e0, 0x28003e8, 0x28003f0, - 0x28003f8, 0x2800400, 0x2800408, 0x2800410, 0x2800418, 0x2800420, - 0x2800428, 0x2800430, 0x2800438, 0x2800440, 0x2800448, 0x2800450, - 0x2800458, 0x2800468, 0x2800470, 0x2800478, 0x2800480, 0x2800488, - 0x2800490, 0x2800498, 0x2808000, 0x2808008, 0x2808010, 0x2808018, - 0x2808020, -}; +#define T6040_AOP_PS_WORDS 44 -#define T6040_PS_SNAP_WORDS DIV_ROUND_UP(ARRAY_SIZE(t6040_ps_snap_pa), 32) +static void t6040_aop_ps_save(struct t6040_sleep *s) +{ + int i; -/* - * The power loss resets every power word, and only the domains a Linux driver owns are - * raised again when their devices resume. The PMGR puts each device back as it was; - * here the SIO peripheral words (I2C0 .. UART6 in SOC-East) that were fully on at the commit - * and came back lower are raised again, parents first (ADT order). On J616s those are what - * the boot loader left on and no driver claims: I2C0, SPI0/1/3/4/5 (SPI4 goes to the - * internal panel), the UARTs, AES. Nothing else: the commit's snapshot also has the words - * the sleep engine and the coprocessors' drivers own (SIO_CPU, PMP, PMS_SRAM, ISP_SYS, the - * ATC commons the commit hands to the engine), and those are their owners' to raise. - */ -#define T6040_PS_RESTORE_FIRST 0x2280150 /* I2C0 */ -#define T6040_PS_RESTORE_LAST 0x2280228 /* UART6 */ + s->aop_ps_saved_valid = false; + if (!s->aop_ps) + return; + for (i = 0; i < T6040_AOP_PS_WORDS; i++) + s->aop_ps_saved[i] = readl(s->aop_ps + 8 * i); + s->aop_ps_saved_valid = true; +} -static void __iomem *t6040_ps_snap_addr(struct t6040_sleep *s, unsigned int i) +static void t6040_aop_ps_restore(struct t6040_sleep *s) { - u32 pa = t6040_ps_snap_pa[i]; - bool soc = (pa >> 16) == 0x0280; - void __iomem *b = soc ? s->snap_soc : s->snap_east; + int k; + + s->aop_ps_timeouts = 0; + if (!s->aop_ps || !s->aop_ps_saved_valid) + return; + for (k = ARRAY_SIZE(t6040_aop_ps_order) - 1; k >= 0; k--) { + u16 off = t6040_aop_ps_order[k]; + void __iomem *r = s->aop_ps + off; + u32 v, want = s->aop_ps_saved[off / 8], now = readl(r); - return b ? b + (pa - (soc ? 0x02800000 : 0x02280000)) : NULL; + if (FIELD_GET(PS_ACTUAL, want)) { + if (FIELD_GET(PS_ACTUAL, now) == 0xf && !(now & PS_AUTO_ENABLE)) + continue; + writel((now & ~(PS_AUTO_ENABLE | PS_TARGET)) | PS_TARGET, r); + } else { + if (FIELD_GET(PS_ACTUAL, now) || !FIELD_GET(PS_TARGET, want)) + continue; + writel(want, r); + } + if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v), 1, 2000)) + s->aop_ps_timeouts++; + } } -static void t6040_ps_save(struct t6040_sleep *s) +/* + * The power loss resets every power word, and only the domains a Linux driver owns are + * raised again when their devices resume. The PMGR puts each device back as it was; + * here the SIO peripheral words (I2C0 .. UART6 in SOC-East) that were fully on at the commit + * and came back lower are raised again, parents first. On J616s those are what the boot + * loader left on and no driver claims: I2C0, SPI0/1/3/4/5 (SPI4 goes to the internal panel), + * the UARTs, AES. Nothing else: every other word is its owner's to raise. + */ +static void t6040_sio_ps_save(struct t6040_sleep *s) { unsigned int i; - s->ps_saved_valid = false; - if (!s->ps_saved) + s->sio_ps_saved_valid = false; + if (!s->pmgr_east) return; - for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { - void __iomem *w = t6040_ps_snap_addr(s, i); - - s->ps_saved[i] = w ? readl(w) : 0; - } - s->ps_saved_valid = true; + for (i = 0; i < ARRAY_SIZE(s->sio_ps_saved); i++) + s->sio_ps_saved[i] = readl(s->pmgr_east + PS_SIO_FIRST + i * PS_STRIDE); + s->sio_ps_saved_valid = true; } -static void t6040_ps_restore(struct t6040_sleep *s) +static void t6040_sio_ps_restore(struct t6040_sleep *s) { unsigned int i, n, raised = 0, failed = 0; u32 val, cur = 0; - if (!s->ps_saved_valid) + if (!s->sio_ps_saved_valid) return; - s->ps_saved_valid = false; - for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { - void __iomem *w = t6040_ps_snap_addr(s, i); - u32 saved = s->ps_saved[i]; - - if (!w || t6040_ps_snap_pa[i] < T6040_PS_RESTORE_FIRST || - t6040_ps_snap_pa[i] > T6040_PS_RESTORE_LAST || - FIELD_GET(PS_TARGET, saved) != 0xf || FIELD_GET(PS_ACTUAL, saved) != 0xf) + s->sio_ps_saved_valid = false; + for (i = 0; i < ARRAY_SIZE(s->sio_ps_saved); i++) { + u32 off = PS_SIO_FIRST + i * PS_STRIDE; + void __iomem *w = s->pmgr_east + off; + u32 saved = s->sio_ps_saved[i]; + + if (FIELD_GET(PS_TARGET, saved) != 0xf || FIELD_GET(PS_ACTUAL, saved) != 0xf) continue; val = readl(w); if (FIELD_GET(PS_TARGET, val) == 0xf) @@ -1475,157 +1264,13 @@ static void t6040_ps_restore(struct t6040_sleep *s) if (n == 1000) failed++; raised++; - pr_info("t6040-sleep: wake: power word %#x: %#x at the commit, %#x after the wake, now %#x\n", - t6040_ps_snap_pa[i], saved, val, cur); + pr_info("t6040-sleep: wake: SOC-East power word +%#x: %#x at the commit, %#x after the wake, now %#x\n", + off, saved, val, cur); } if (raised) pr_info("t6040-sleep: wake: %u power word(s) raised again, %u not on\n", raised, failed); } -#define T6040_PS_SNAP_PMU 0xa2c0 -#define T6040_PS_SNAP_MAGIC 0x504e5350 /* "PSNP" in PMU byte order */ - -static void t6040_ps_snap_take(struct t6040_sleep *s, u32 *map) -{ - int i; - - memset(map, 0, T6040_PS_SNAP_WORDS * sizeof(u32)); - for (i = 0; i < ARRAY_SIZE(t6040_ps_snap_pa); i++) { - u32 pa = t6040_ps_snap_pa[i]; - void __iomem *b = (pa >> 16) == 0x0280 ? s->snap_soc : s->snap_east; - u32 off = pa - ((pa >> 16) == 0x0280 ? 0x02800000 : 0x02280000); - - if (b && FIELD_GET(PS_ACTUAL, readl(b + off))) - map[i / 32] |= BIT(i % 32); - } -} - -static void t6040_ps_snap_store(struct t6040_sleep *s) -{ - u32 map[T6040_PS_SNAP_WORDS], hdr = T6040_PS_SNAP_MAGIC; - u32 cnt = ARRAY_SIZE(t6040_ps_snap_pa); - int i; - - t6040_ps_snap_take(s, map); - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU, SPMI_OP_WRITE4), &hdr, NULL, 0); - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU + 4, SPMI_OP_WRITE4), &cnt, NULL, 0); - for (i = 0; i < T6040_PS_SNAP_WORDS; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PS_SNAP_PMU + 8 + 4 * i, SPMI_OP_WRITE4), - &map[i], NULL, 0); -} - -/* - * On the J616s the device resume reached the AOP (510c10000.aop) and took an SError in its resume, - * whose first act after a firmware-driven sleep is reading its ASC's run bit: the AOP + audio - * island was not powered after the wake. aop_wake bit0 records 'AOPW' at PMU 0xa2c0 right - * after the wake return's APCIe step (w1 = words on before | words on after << 8 | raise - * timeouts << 16, w2..w7 = ACTUAL nibbles of the 44 words 0x000..0x158, 8 per word, before any - * raise); bit1 writes back the words saved in .begin, parents first (iBoot's power-down order - * reversed), waiting for ACTUAL where the saved target was on. The ASC stays halted: the AOP - * driver then takes it over cold (unbind + probe after thaw). - */ -static void t6040_ret_mark(struct t6040_sleep *s, u32 n); - -#define T6040_AOP_PS_WORDS 44 -#define T6040_AOPW_PMU 0xa2c0 -#define T6040_AOPW_MAGIC 0x57504f41 /* "AOPW" in PMU byte order */ - -static void t6040_aop_ps_save(struct t6040_sleep *s) -{ - int i; - - s->aop_ps_saved_valid = false; - if (!s->aop_ps) - return; - for (i = 0; i < T6040_AOP_PS_WORDS; i++) - s->aop_ps_saved[i] = readl(s->aop_ps + 8 * i); - s->aop_ps_saved_valid = true; -} - -static unsigned int t6040_aop_ps_count_on(struct t6040_sleep *s, u32 *nib) -{ - unsigned int on = 0; - int i; - - for (i = 0; i < T6040_AOP_PS_WORDS; i++) { - u32 a = FIELD_GET(PS_ACTUAL, readl(s->aop_ps + 8 * i)); - - if (a) - on++; - if (nib) - nib[i / 8] |= a << (4 * (i % 8)); - } - return on; -} - -static void t6040_aop_wake(struct t6040_sleep *s) -{ - unsigned int before, after, to = 0; - u32 w[10] = { T6040_AOPW_MAGIC }; - int k, i; - - if (!s->aop_ps) - return; - before = t6040_aop_ps_count_on(s, &w[2]); - w[8] = w[9] = 0; - if (s->aop_ps_saved_valid) { - for (k = ARRAY_SIZE(t6040_aop_ps_order) - 1; k >= 0; k--) { - u16 off = t6040_aop_ps_order[k]; - void __iomem *r = s->aop_ps + off; - u32 v, want = s->aop_ps_saved[off / 8], now = readl(r); - /* - * AOP's resume faulted again after a 1 h 43 min sleep (it had passed - * before): a word in automatic mode raised here gates again before the AOP - * driver reads its ASC. bit2: every word that was on before the sleep goes to - * manual target 0xf (AUTO off) and stays up through the resume. - */ - bool manual = FIELD_GET(PS_ACTUAL, want); - - if (manual) { - if (FIELD_GET(PS_ACTUAL, now) == 0xf && !(now & PS_AUTO_ENABLE)) - continue; - writel((now & ~(PS_AUTO_ENABLE | PS_TARGET)) | PS_TARGET, r); - } else { - if (FIELD_GET(PS_ACTUAL, now) || !FIELD_GET(PS_TARGET, want)) - continue; - writel(want, r); - } - if (off / 8 < 32) - w[8] |= BIT(off / 8); - else - w[9] |= BIT(off / 8 - 32); - if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v), 1, 2000)) - to++; - } - } - after = t6040_aop_ps_count_on(s, NULL); - w[1] = before | after << 8 | min(to, 0xffffU) << 16; - if (s->spmi0) - for (i = 0; i < 10; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 4 * i, SPMI_OP_WRITE4), - &w[i], NULL, 0); - - /* - * With every AOP island word that was on before the sleep at manual on, the - * AOP's resume still faulted on its ASC: bit3 reads the ASC CPU_CONTROL here, marks - * 0x58 before and 0x59 after, so an SError at 0x58 says the ASC is dead right at the - * wake (nothing later gated it). AOPW w8 then becomes CPU_CONTROL, w9 the AOP - * requests 0x5108b8100 [15:0] | MasterMini+0x14 << 16. - */ - if (s->aop_asc && s->spmi0) { - u32 ctl, rq; - - rq = (s->aop_req ? readl(s->aop_req + 0x100) & 0xffff : 0xffff) | - (s->mastermini ? (readl(s->mastermini + 0x14) & 0xffff) << 16 : 0); - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 36, SPMI_OP_WRITE4), &rq, NULL, 0); - t6040_ret_mark(s, 0x58); - ctl = readl(s->aop_asc + ASC_CPU_CONTROL); - dsb(sy); - isb(); - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_AOPW_PMU + 32, SPMI_OP_WRITE4), &ctl, NULL, 0); - t6040_ret_mark(s, 0x59); - } -} /* As on T8140, measured on ANS: status and control read, then the run bit cleared twice. */ static void t6040_sleep_coproc_run_off(struct apple_sleep_coproc *c) @@ -1640,14 +1285,9 @@ static void t6040_sleep_coproc_run_off(struct apple_sleep_coproc *c) c->stopped = true; } -static u32 t6040_mark_hash(const char *name); -static void t6040_mark_raw(struct t6040_sleep *s, u32 v); -static void t6040_ret_mark(struct t6040_sleep *s, u32 n); - /* After the last refusal: those registered first stop whole first, then the rest. */ static void t6040_sleep_coprocs_stop(bool first) { - u32 h; int i; for (i = 0; i < SLEEP_COPROCS; i++) { @@ -1655,15 +1295,10 @@ static void t6040_sleep_coprocs_stop(bool first) if (!c->dev || !c->ready || c->first != first || c->stopped) continue; - /* 0x5fSFHHHH: S 1 its last stop, 2 its run bit, 3 done; F first; H its name's hash. */ - h = t6040_mark_hash(dev_name(c->dev)) | first << 16; - t6040_mark_raw(t6040_sleep, 0x5f100000 | h); if (c->last_stop) c->last_stop(c->data); - t6040_mark_raw(t6040_sleep, 0x5f200000 | h); t6040_sleep_coproc_run_off(c); - t6040_mark_raw(t6040_sleep, 0x5f300000 | h); - /* ANS stopped: the commit lowers its ST lanes, the wake raises them. */ + /* ANS stopped: the commit quiesces its storage link, the wake raises it. */ if (c->pcie_ans && t6040_sleep) t6040_sleep->pcie_ans_stopped = true; } @@ -1773,250 +1408,20 @@ static void t6040_pmu_write(struct t6040_sleep *s, u16 reg, u8 val) writel(enable, s->spmi0 + SPMI_NOTIFY_ENABLE); } -/* - * A PMU (SID 0xe) command with an extended register over spmi0, as #1314 frames T8132's: - * drain stale replies, mask the reply vector, the command and its data, poll and acknowledge - * the reply, then read @nrx words (a header, then the data). - */ -#define T6040_MARK(n) (0x53340000 | (n)) /* "S4" + step */ - -static int t6040_pmu_cmd(struct t6040_sleep *s, u32 cmd, const u32 *tx, u32 *rx, - unsigned int nrx) -{ - void __iomem *b = s->spmi0; - unsigned int i; - u32 val, enable; - int ret; - - if (!b) - return -ENODEV; - for (i = 0; i < 16 && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY); i++) - readl(b + SPMI_REPLY); - enable = readl(b + SPMI_NOTIFY_ENABLE); - writel(enable & ~SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ENABLE); - writel(cmd, b + SPMI_CMD); - if (tx) - writel(*tx, b + SPMI_CMD); - ret = readl_poll_timeout_atomic(b + SPMI_NOTIFY_ACK, val, val & SPMI_NOTIFY_REPLY, 1, - 1000); - writel(SPMI_NOTIFY_REPLY, b + SPMI_NOTIFY_ACK); - for (i = 0; !ret && i < nrx; i++) { - ret = readl_poll_timeout_atomic(b + SPMI_STATUS, val, - !(val & SPMI_STATUS_RX_EMPTY), 1, 1000); - if (!ret) - rx[i] = readl(b + SPMI_REPLY); - } - /* A write's reply word, if the PMU sent one. */ - if (!nrx && !(readl(b + SPMI_STATUS) & SPMI_STATUS_RX_EMPTY)) - readl(b + SPMI_REPLY); - writel(enable, b + SPMI_NOTIFY_ENABLE); - - return ret; -} - -/* - * Bring-up progress through a reset (#1314's way on T8132): the PMU keeps its RTC comparator - * across one, and userspace reads it back on any kernel (spmi_nvmem0 offset 0x2008). A value - * 0x5334xxxx is the last step reached; the saved alarm + 1 s means the commit itself ran. - * - * Off by default. The comparator is the wake alarm's: a mark left in it at the commit takes - * the alarm's place, and that sleep ends only on a key. And the marks go to the SPMI - * controller raw, beside the SPMI driver's own transfers: during a resume one of the PMU - * RTC's interrupt thread then got no reply, and that driver gives its alarm up for good. - */ -static bool marks; -module_param(marks, bool, 0644); -MODULE_PARM_DESC(marks, "Bring-up: write S2R progress marks into the PMU RTC comparator (takes the wake alarm's place: no RTC wake)"); - -static void t6040_mark_raw(struct t6040_sleep *s, u32 v); - -/* The furthest driver step this sleep reached, carried in the device marks' byte 2. */ -static u8 t6040_furthest; - -static void t6040_mark(struct t6040_sleep *s, u32 n) -{ - u32 v = T6040_MARK(n); - - if (n < 0x40 && n > READ_ONCE(t6040_furthest)) - WRITE_ONCE(t6040_furthest, n); - - t6040_mark_raw(s, v); -} - -static void t6040_mark_save(struct t6040_sleep *s) -{ - u32 rx[2]; - - s->alarm_ok = false; - WRITE_ONCE(t6040_furthest, 0); - if (!READ_ONCE(marks) || !s->spmi0) - return; - if (t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_READ4), NULL, rx, 2)) { - pr_warn("t6040-sleep: marks: can't read the PMU comparator, no marks\n"); - return; - } - s->alarm_hdr = rx[0]; - s->alarm_saved = rx[1]; - s->alarm_ok = true; -} - -/* - * Per-device marks from the PM core (#1314's hook), between .begin and .end: 0x4P00HHHH, P the - * phase (1 prepare, 2 suspend, 3 late, 4 noirq, 8-10 resume), H the FNV-1a hash of the device's - * name folded to 16 bits. A panic keeps the low 28 bits of the last mark and sets the top nibble - * to its kind: 0xe an SError, 0xd a fatal exception (oops), 0x6 any other panic. A bare hardware - * reset keeps the mark as it was. The driver's own families are chosen so that a panic over them - * never reads as a device mark: 0x5334 steps, 0x5b refusal, 0x5c display word, 0x5e bridge. - */ -static void (*pm_bringup_dev_mark)(struct device *dev, unsigned int phase); -static DEFINE_RAW_SPINLOCK(t6040_mark_lock); -static u32 t6040_last_mark; - -static u32 t6040_mark_hash(const char *name) -{ - u32 h = 0x811c9dc5; - - for (; *name; name++) { - h ^= (u8)*name; - h *= 0x01000193; - } - return (h ^ (h >> 16)) & 0xffff; -} - -static void t6040_mark_raw(struct t6040_sleep *s, u32 v) -{ - unsigned long flags; - - if (!s || !READ_ONCE(marks) || !s->alarm_ok) - return; - raw_spin_lock_irqsave(&t6040_mark_lock, flags); - t6040_last_mark = v; - t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); - raw_spin_unlock_irqrestore(&t6040_mark_lock, flags); -} - -/* - * On the J616s a device's suspend failed before .prepare (furthest step 0x01), the PM core rolled - * back and the resume's SError mark overwrote which one it was. susp_rec keeps the last - * suspend-phase device mark (phases 1-4) at PMU 0xa204 too, under 'SUSF' at 0xa200 written in - * .begin (it takes DCSR's place: use with dcs_rec=0). - */ -#define T6040_SUSF_PMU 0xa200 -#define T6040_SUSF_MAGIC 0x46535553 /* "SUSF" in PMU byte order */ - -static bool susp_rec = true; -module_param(susp_rec, bool, 0644); -MODULE_PARM_DESC(susp_rec, "Bring-up: keep the last suspend-phase device mark at PMU 0xa204 ('SUSF' 0xa200)"); - -static void t6040_dev_mark(struct device *dev, unsigned int phase) -{ - struct t6040_sleep *s = t6040_sleep; - u32 v = 0x40000000 | (phase & 0xf) << 24 | READ_ONCE(t6040_furthest) << 16 | - (dev ? t6040_mark_hash(dev_name(dev)) : 0); - - t6040_mark_raw(s, v); - if (READ_ONCE(susp_rec) && phase >= 1 && phase <= 4 && s && s->spmi0 && s->alarm_ok) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU + 4, SPMI_OP_WRITE4), &v, NULL, 0); -} - -static void t6040_susf_begin(struct t6040_sleep *s) -{ - u32 w[2] = { T6040_SUSF_MAGIC, 0 }; - - /* The record holds device marks: nothing to keep, and no raw SPMI, without them. */ - if (!READ_ONCE(marks) || !READ_ONCE(susp_rec) || !s->spmi0) - return; - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU, SPMI_OP_WRITE4), &w[0], NULL, 0); - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_SUSF_PMU + 4, SPMI_OP_WRITE4), &w[1], NULL, 0); -} - +/* A no-op: this controller keeps no progress marks. */ void apple_sleep_bringup_mark(u32 v) { - if (READ_ONCE(pm_bringup_dev_mark)) - t6040_mark_raw(t6040_sleep, v); } EXPORT_SYMBOL_GPL(apple_sleep_bringup_mark); -static bool serror_esr; -module_param(serror_esr, bool, 0644); -MODULE_PARM_DESC(serror_esr, "An SError panic mark carries ESR[15:0] instead of the mark's low half"); - -static unsigned int panic_wd_secs = 15; -module_param(panic_wd_secs, uint, 0644); -MODULE_PARM_DESC(panic_wd_secs, - "Bring-up: a panic during a sleep cycle arms the WD0 and WD1 resets this many seconds out (0: off)"); - -static bool panic_msg_hash; -module_param(panic_msg_hash, bool, 0644); -MODULE_PARM_DESC(panic_msg_hash, - "Bring-up: a panic mark carries the CPU and a 24-bit hash of the message, not the last mark"); - -static int t6040_panic_mark(struct notifier_block *nb, unsigned long action, void *data) -{ - struct t6040_sleep *s = t6040_sleep; - - if (s && READ_ONCE(pm_bringup_dev_mark) && s->alarm_ok) { - const char *msg = data; - u32 kind = 0x60000000; - u32 v; - - if (msg && strstr(msg, "SError")) - kind = 0xe0000000; - else if (msg && strstr(msg, "Fatal exception")) - kind = 0xd0000000; - v = (READ_ONCE(t6040_last_mark) & 0x0fffffff) | kind; - /* An SError keeps the mark's family and step, and its syndrome's low half. */ - if (kind == 0xe0000000 && READ_ONCE(serror_esr)) - v = (v & 0xffff0000) | (read_sysreg(esr_el1) & 0xffff); - /* - * Bring-up: which CPU and which message instead of the last mark: - * kind | CPU << 24 | FNV-1a of the first 32 message bytes folded to 24 bits. - */ - if (READ_ONCE(panic_msg_hash)) { - u32 h = 0x811c9dc5; - int i; - - for (i = 0; msg && msg[i] && i < 32; i++) - h = (h ^ (u8)msg[i]) * 0x01000193; - v = kind | (raw_smp_processor_id() & 0xf) << 24 | ((h ^ (h >> 24)) & 0xffffff); - } - - t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); - - /* - * After an S2R wake the SMC has had no 'waka' yet and the 'phra' restart can - * hang (on the J616s an SError panic right after the resume left the Mac dead for - * 12 min, f803 0x50). Arm the WD0 and WD1 resets as a backstop behind the - * normal panic restart; WD0 is the block the SMC logger bites. - */ - if (s->wdt && READ_ONCE(panic_wd_secs)) { - u32 t = min(READ_ONCE(panic_wd_secs), WDT_MAX_SECS) * WDT_TICKS_PER_S; - - writel(0, s->wdt + WDT_WD0_CTL); - writel(0, s->wdt + WDT_WD0_COUNT); - writel(t, s->wdt + WDT_WD0_BITE); - writel(WDT_CTL_RESET_EN, s->wdt + WDT_WD0_CTL); - writel(0, s->wdt + WDT_WD1_CTL); - writel(0, s->wdt + WDT_WD1_COUNT); - writel(t, s->wdt + WDT_WD1_BITE); - writel(WDT_CTL_RESET_EN, s->wdt + WDT_WD1_CTL); - dsb(sy); - } - } - return NOTIFY_DONE; -} - -static struct notifier_block t6040_panic_nb = { - .notifier_call = t6040_panic_mark, -}; - -/* The wake alarm back, 1 s later, so a reset after this point reads as "the commit ran". */ -static void t6040_mark_restore(struct t6040_sleep *s) +/* + * Between the steps of the wake return: every access of a step is complete, and a fault it + * raised is taken, before the next step starts. + */ +static void t6040_wake_sync(void) { - u32 v = s->alarm_saved + 1; - - if (s->alarm_ok) - t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); + dsb(sy); + isb(); } /* @@ -2035,25 +1440,21 @@ static void t6040_clusters_off(struct t6040_sleep *s) for (i = 0; i < CLUSTER_CTL_NR; i++) { val = readl(s->cluster_ctl + CLUSTER_CTL(i)); s->cluster_saved[i] = val; - s->cluster_after[i] = val; writel(val & ~CLUSTER_CTL_NIBBLE, s->cluster_ctl + CLUSTER_CTL(i)); - if (i >= CLUSTER_CTL_FIRST_WAIT && - readl_poll_timeout_atomic(s->cluster_ctl + CLUSTER_CTL(i), val, - !(val & CLUSTER_CTL_BUSY), 1, CLUSTER_TIMEOUT_US)) - s->quiesce_to |= QTO_CLUSTER(i); - s->cluster_after[i] = readl(s->cluster_ctl + CLUSTER_CTL(i)); + if (i >= CLUSTER_CTL_FIRST_WAIT) + readl_poll_timeout_atomic(s->cluster_ctl + CLUSTER_CTL(i), val, + !(val & CLUSTER_CTL_BUSY), 1, CLUSTER_TIMEOUT_US); } } -static void t6040_east_clk_quiesce(struct t6040_sleep *s, u32 off, int bit) +static void t6040_east_clk_quiesce(struct t6040_sleep *s, u32 off) { u32 val = readl(s->soc_east + off), cur; writel(val & ~SOC_EAST_CLK_AUTO, s->soc_east + off); - if (readl_poll_timeout_atomic(s->soc_east + off, cur, - FIELD_GET(PS_ACTUAL, cur) == FIELD_GET(PS_TARGET, val), 1, - CLK_QUIESCE_TIMEOUT_US)) - s->quiesce_to |= QTO_EAST_CLK(bit); + readl_poll_timeout_atomic(s->soc_east + off, cur, + FIELD_GET(PS_ACTUAL, cur) == FIELD_GET(PS_TARGET, val), 1, + CLK_QUIESCE_TIMEOUT_US); } /* The clock quiesce, saving the clk-mux words for wake. */ @@ -2063,8 +1464,8 @@ static void t6040_clkgen_park(struct t6040_sleep *s) int i; if (s->soc_east) { - t6040_east_clk_quiesce(s, SOC_EAST_CLK_A, 0); - t6040_east_clk_quiesce(s, SOC_EAST_CLK_B, 1); + t6040_east_clk_quiesce(s, SOC_EAST_CLK_A); + t6040_east_clk_quiesce(s, SOC_EAST_CLK_B); } if (!s->clkgen) return; @@ -2074,9 +1475,8 @@ static void t6040_clkgen_park(struct t6040_sleep *s) s->clkgen_mux_saved[i] = val; writel((val & CLKGEN_MUX_MASK) | CLKGEN_MUX_SET, reg); - if (readl_poll_timeout_atomic(reg, cur, !(cur & CLKGEN_MUX_PENDING), 1, - CLK_QUIESCE_TIMEOUT_US)) - s->quiesce_to |= QTO_MUX(i); + readl_poll_timeout_atomic(reg, cur, !(cur & CLKGEN_MUX_PENDING), 1, + CLK_QUIESCE_TIMEOUT_US); } s->clkgen_saved = true; } @@ -2095,26 +1495,24 @@ static void t6040_clkgen_restore(struct t6040_sleep *s) /* * Wait for every other core's power word (SOC + 8 * its cpu-start bit index, - * ECPU_0.. PCPU1_5 in the ADT) to read ACTUAL 0. Returns how many stayed on. + * ECPU_0.. PCPU1_5 in the ADT) to read ACTUAL 0, each for at most 10 ms. */ -static unsigned int t6040_secondaries_off(struct t6040_sleep *s) +static void t6040_secondaries_off(struct t6040_sleep *s) { u32 self = t6040_cpu_start_bit(read_cpuid_mpidr() & MPIDR_HWID_BITMASK); - unsigned int cpu, on = 0; + unsigned int cpu; u32 v; if (!s->soc_ps) - return 0; + return; for_each_present_cpu(cpu) { u32 bit = t6040_cpu_start_bit(cpu_logical_map(cpu)); if (!bit || bit == self) continue; - if (readl_poll_timeout_atomic(s->soc_ps + 8 * __ffs(bit), v, - !FIELD_GET(PS_ACTUAL, v), 1, 10000)) - on++; + readl_poll_timeout_atomic(s->soc_ps + 8 * __ffs(bit), v, + !FIELD_GET(PS_ACTUAL, v), 1, 10000); } - return on; } /* The hardware steps of the generic PMGR quiesce, in the order (see t6040_acc_apsc_pa). */ @@ -2129,56 +1527,43 @@ static void t6040_pmgr_quiesce(struct t6040_sleep *s) if (!acc) continue; - t6040_mark(s, 0x300 | i); writeq(readq(acc) | ACC_APSC_DISABLE, acc); - if (readq_poll_timeout_atomic(acc, q, !(q & ACC_APSC_BUSY), 1, QUIESCE_TIMEOUT_US)) - s->quiesce_to |= QTO_APSC(i); + readq_poll_timeout_atomic(acc, q, !(q & ACC_APSC_BUSY), 1, QUIESCE_TIMEOUT_US); } for (i = 0; i < ACC_PLL_RELOCK_COMPLEXES; i++) - if (s->acc_apsc[i]) { - t6040_mark(s, 0x310 | i); + if (s->acc_apsc[i]) writeq(readq(s->acc_apsc[i]) & ~ACC_PLL_RELOCK, s->acc_apsc[i]); - } for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { if (!s->tvm_ctl[i] || !s->tvm_status[i]) continue; - t6040_mark(s, 0x320 | i); writel(readl(s->tvm_ctl[i]) & ~TVM_ENABLE, s->tvm_ctl[i]); - if (readl_poll_timeout_atomic(s->tvm_status[i], v, !(v & TVM_BUSY), 1, - QUIESCE_TIMEOUT_US)) - s->quiesce_to |= QTO_TVM(i); + readl_poll_timeout_atomic(s->tvm_status[i], v, !(v & TVM_BUSY), 1, + QUIESCE_TIMEOUT_US); } } -/* The AGX residency gate of the PMGR quiesce. Returns a mark step for how far it got. */ -static u32 t6040_agx_gate(struct t6040_sleep *s) +/* The AGX residency gate of the PMGR quiesce (see T6040_AGX_GATE_PA). */ +static void t6040_agx_gate(struct t6040_sleep *s) { u32 gate, val, cur; - s->afr_saved = 0; if (!s->agx_gate || !s->afr_ps) - return 0; - t6040_mark(s, 0x340); + return; gate = readl(s->agx_gate); if (!(~gate & AGX_GATE_RESIDENT)) - return 1; - t6040_mark(s, 0x341); + return; val = readl(s->afr_ps); if (!FIELD_GET(PS_TARGET, val)) - return 2; - s->afr_saved = val; + return; writel((val & ~AFR_LEVEL) | FIELD_PREP(AFR_LEVEL, 4), s->afr_ps); if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(AFR_LEVEL, cur) == 4, 1, AGX_TIMEOUT_US)) - return 3; + return; if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(PS_ACTUAL, cur) == 4, 1, AGX_TIMEOUT_US)) - return 4; + return; writel(0, s->afr_ps); - if (readl_poll_timeout_atomic(s->afr_ps, cur, !FIELD_GET(PS_ACTUAL, cur), 1, - AGX_TIMEOUT_US)) - return 5; - return 6; + readl_poll_timeout_atomic(s->afr_ps, cur, !FIELD_GET(PS_ACTUAL, cur), 1, AGX_TIMEOUT_US); } /* @@ -2279,14 +1664,10 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) t6040_axi2af[i].name, val); continue; } - dsb(sy); - isb(); - t6040_mark_raw(s, 0x5e000100 | i << 16 | (val & 0xff)); + t6040_wake_sync(); writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); - dsb(sy); - isb(); - t6040_mark_raw(s, 0x5e000900 | i << 16 | (val & 0xff)); + t6040_wake_sync(); } } @@ -2375,47 +1756,19 @@ static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_t } /* - * ENTRY: lower ANS's storage link, the two-lane adaptation of - * t8140_sleep_lower_apcie(). Called from the commit's lower_io only once ANS - * has been stopped by its driver (pcie_ans_stopped). The PHY/clkgen/axi are - * shared across the lanes; only the two sys_st/st domains are per-lane. + * ENTRY: quiesce ANS's storage link, once ANS has been stopped by its driver. The PHY, the + * clock generator and the AXI enables are treated as shared by the two ST lanes. * - * Not known (needs the first S2R on hardware): whether T6040 truly shares the PHY across - * st0/st1 (assumed here) or gates a per-lane PHY, and whether a separate - * ps_apcie_phy_sw domain exists (none in the DT, so none is driven). A wrong - * shared/per-lane split only times out the raise's ready poll, which names the - * failing stage; the power goes next either way. - */ -/* - * The A2D engine (iBoot MCPS seq 24) powers this group down itself, in its own order: - * APCIE_PHY_SW (0x398) to clock-gated, polled for PARENT_MISSING (bit 11) clear, then - * APCIE_SYS_ST0/1 off, APCIE_ST0/1 clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP - * clock-gated. APCIE_PHY_SW's parents are APCIE_SYS_ST0/1 (ADT): with them already off - * the engine waits on PARENT_MISSING for good (on the J616s: MCPS stuck at 0x502280398). - * apcie_st_ps=0 leaves the power words to the engine (the PHY, clock and AXI quiesce - * still run); 1 lowers them here as the T8140 driver does. + * The power words of this group are left to the SoC's sleep engine, which powers it down + * itself and in its own order: APCIE_PHY_SW to clock-gated, APCIE_SYS_ST0/1 off, APCIE_ST0/1 + * clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP clock-gated. It polls APCIE_PHY_SW for + * its parents, which are both SYS_ST lanes: with them lowered here, as the T8140 controller + * does, it waited there for good on the J616s. So the commit raises APCIE_SYS_ST1, which + * Linux never uses, and does only what the platform does before the engine runs: the shared PHY put + * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. */ -/* - * With apcie_st_ps=0 the engine still waits on APCIE_PHY_SW: right before the timer - * APCIE_ST0/1, ANS and APCIE_SYS_ST0 are on but APCIE_SYS_ST1 is off (Linux never uses the - * second lane), and APCIE_PHY_SW's parents are both SYS_ST lanes. apcie_st1_on raises - * APCIE_SYS_ST1 (target 0xf, no automatic mode) before the request; the engine lowers it. - */ - -/* - * With both SYS_ST lanes on the engine still waits on APCIE_PHY_SW bit 11. The platform never - * leaves that gate to the engine: its PCIe quiesce puts the shared PHY to sleep (the root - * complex disable), gates APCIE_PHY_SW itself while APCIE_GP is still on (the clock quiesce), - * then shuts the PCIe PLLs down and gates the other seven. - * apcie_seq: bit 0 S1 as the platform; bit 1 APCIE_PHY_SW to clock-gated (the engine's 0x0f000244, - * bit 11 polled clear) then S3 as the platform; bit 2 record 'APCS' + flags + PHY/PS words in PMU SRAM - * 0xa200; bit 3 APCIE_GP to clock-gated (the engine's 0x244) instead of off. - * Flags (word 1): 0 S1 ran, 1 PHY_SW gate ran, 2 S3 ran, 3 ANS stopped, 4 windows valid, - * 5 GP clock-gated; timeouts 8 CLK1ACK, 9 CLK0ACK, 16 PHY_SW bit 11, 17 clkgen busy, 18 GP. - */ - -/* S1, the root complex disable: the shared PHY and PHY-IP to sleep; the platform spins unbounded. */ +/* The shared PHY and PHY-IP to sleep (the root complex disable; it polls unbounded). */ static void t6040_apcie_phy_off(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; @@ -2442,7 +1795,7 @@ static void t6040_apcie_phy_off(struct t6040_sleep *s) writel(readl(run) & ~APCIE_PHY_RUN_EN, run); } -/* S2 step 1, gate idx 7 off: APCIE_PHY_SW gated while APCIE_GP is still on. */ +/* APCIE_PHY_SW to clock-gated (0x0f000244), its busy bit polled clear. */ static void t6040_apcie_phy_sw_gate(struct t6040_sleep *s) { void __iomem *reg = s->soc_east + PS_APCIE_PHY_SW; @@ -2452,7 +1805,7 @@ static void t6040_apcie_phy_sw_gate(struct t6040_sleep *s) readl_poll_timeout_atomic(reg, val, !(val & PS_BUSY), 1, PS_OFF_TIMEOUT_US); } -/* S3, the PCIe PLL shutdown: PCIECLKGEN +0 as the platform. */ +/* The PCIe PLL shutdown: the clock generator's control word. */ static void t6040_apcie_pll_off(struct t6040_sleep *s) { void __iomem *clk = s->apcie_st.clkgen + APCIE_CLK_CTRL; @@ -2466,95 +1819,40 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) } /* - * With APCIE_PHY_SW gated the MCPS finished A2D phase 1 (seq 24, marks 0x16/0x17) and - * stalls in seq 25 raising ATC0_COMMON (0x30f, ACTUAL f polled): its parent FAB2_SOC is off at - * the timer, as are FAB1/4/7_SOC (genpd powers fab2_soc off with the ATC commons at - * suspend_noirq). On the platform the fabrics stay up in D, and the D2A polls these 13 words for AUTO 1 - * and target f. fab_on raises each one not in that state (target f, ACTUAL f polled, then - * AUTO) right before the timer and records 'FABS' + raised mask + timeout mask + the 13 words - * in PMU SRAM 0xa240; a timeout also marks 0x54d0MMMM. + * The sleep engine hands each ATC port to its always-on side and then powers the port's + * common domain off itself: it needs the four ATC commons on, as they are on the platform at that + * point. With them off it stalled on the J616s raising ATC0_COMMON, whose parent FAB2_SOC + * was off as well. FAB2_SOC is raised by hand, then ATC0..3_COMMON to a manual target 0xf, + * then FAB2_SOC gets its automatic mode back: the powered children hold it up. */ -/* - * All 13 were already AUTO + target f except SBR and FAB_AFR, but FAB1/2/4/7_SOC read - * 0x1000030f: automatic level 0, so they power-gate when idle (ACTUAL 0) and the engine still - * waits for ATC0_COMMON. Bit 1 sets their automatic level to f (as FAB0/FAB3, 0x1f0000ff) and - * polls ACTUAL f; record word 1/2 bits 16+ name those words. - */ -/* - * The level write does not stick (FAB1/2/4 read back 0x1000030f), but the MCPS got - * through ATC0 (ATC0_PHYMXWRAP on) and now waits for ATC1_COMMON. On the platform the ATC commons are - * still on when the engine runs: seq 25 hands each port to its AON side and then powers the - * common off itself, and the powered children hold FAB2_SOC up. Bit 2 raises FAB2_SOC by hand - * (AUTO off, target f), then ATC0..3_COMMON (target f, no automatic mode), then gives FAB2_SOC - * its AUTO back; 'ATCC' + FAB2 before/after + the 4 commons before/after go to PMU 0xa280, - * timeouts to record word 2 bit 13 (FAB2) and bits 28-31 (ATC0-3). - */ -/* - * With ATC1's dwc3 unbound the MCPS stalled at ATC0 again. Over five runs ATC0's - * hand-over passed exactly when FAB7_SOC (child DISPEXT1_SYS; the PHY mux wrapper carries the - * DISPEXT DP outputs into the ATC PHYs) happened to be up (0x1f0003ff) and stalled when it was - * auto-gated (0x1000030f); bits 27:24 read back the automatic state, they are not a setting. - * Bit 3 holds FAB1/2/4/7_SOC on by hand (AUTO off, target f, ACTUAL f polled) through the - * timer; record word 1/2 bits 16+i name them (bit 1 is not used with it). - */ - -#define T6040_ATC_PMU 0xa280 -#define T6040_ATC_MAGIC 0x43435441 /* "ATCC" in PMU byte order */ #define PS_FAB2_SOC 0x338 static const u16 t6040_atc_common[4] = { 0x390, 0x3b8, 0x3e0, 0x408 }; -static void t6040_atc_commons_on(struct t6040_sleep *s, u32 *timeouts) +static void t6040_atc_commons_on(struct t6040_sleep *s) { - void __iomem *b = s->snap_soc; - u32 rec[11] = { T6040_ATC_MAGIC }; + void __iomem *b = s->pmgr_soc; int i; if (!b) return; - rec[1] = readl(b + PS_FAB2_SOC); + t6040_ps_raise_start(b, PS_FAB2_SOC); for (i = 0; i < 4; i++) - rec[3 + i] = readl(b + t6040_atc_common[i]); - if (t6040_ps_raise_start(b, PS_FAB2_SOC)) - *timeouts |= BIT(13); - for (i = 0; i < 4; i++) - if (t6040_ps_raise_start(b, t6040_atc_common[i])) - *timeouts |= BIT(28 + i); + t6040_ps_raise_start(b, t6040_atc_common[i]); t6040_ps_raise_finish(b, PS_FAB2_SOC); - rec[2] = readl(b + PS_FAB2_SOC); - for (i = 0; i < 4; i++) - rec[7 + i] = readl(b + t6040_atc_common[i]); - for (i = 0; i < ARRAY_SIZE(rec); i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_ATC_PMU + 4 * i, SPMI_OP_WRITE4), &rec[i], - NULL, 0); } /* - * On the first real S2R entry the wake program (seq 21) powers the ATC ports back up and - * stalls at port 3: ATCPHY_CFG(3) +0x2c := 1, then +0x04 bit 8 (ACIO80PHY_AHB_CLK_ACK) never - * rises. Ports 0..2 pass the same step. The PHY S2R entry, run on every port before - * sleep, sets +0x2c bit 0 (S2R_AON_SYNC_RST_N), +0x08 bit 0 (CFG_S2R_FSM_EN) and +0x28 bit 0 - * (AHB_CLK_REQ_N) and waits for that ack. atc_cfg_dump logs +0x00/04/08/24/ - * 28/2c of the 4 ports now; atc_s2r (port mask) does the three writes + the ack poll right - * before the timer, after the commons are up (fab_on bit 2). The 'ATCF' record at PMU 0xa240 - * (instead of 'FABS') keeps, per port, +0x04 and a word of +0x08/+0x24/+0x28/+0x2c low bytes. - * - * Port 3 carries the HDMI output's DisplayPort lanes and is always in that state. Any other - * port is too once a display runs on it in DP alternate mode: the engine then stalls on that - * port in the entry itself (seq 25, the same +0x2c write and +0x04 poll; port 0 with a - * display on it). A parked port and one the PHY driver put to sleep (USB3) already have - * +0x28 bit 0 set. So every port in the mask whose +0x28 bit 0 is still clear gets the three - * writes; the default mask is all four. + * The CFG writes of the PHY sleep entry, which it runs on every ATC port before a sleep: + * ATCPHY_CFG +0x2c bit 0 (S2R_AON_SYNC_RST_N), +0x08 bit 0 (CFG_S2R_FSM_EN) and +0x28 bit 0 + * (AHB_CLK_REQ_N) set, then +0x04 bit 8 (the clock's ack) polled. Without them the engine + * stalled on the J616s on a port that carries DisplayPort lanes: port 3 (the HDMI output) + * always, any other port once a display runs on it in DP alternate mode. A parked port, and + * one its PHY driver has put to sleep, already has +0x28 bit 0 set and is left alone. */ -static bool atc_rec = true; -module_param(atc_rec, bool, 0644); -MODULE_PARM_DESC(atc_rec, "Bring-up: record the 4 ATCPHY_CFG blocks at PMU 0xa240 right before the S2R timer (replaces 'FABS')"); - -#define T6040_ATCF_MAGIC 0x46435441 /* "ATCF" in PMU byte order */ - -static u32 t6040_atc_s2r_entry(struct t6040_sleep *s) +static void t6040_atc_s2r_entry(struct t6040_sleep *s) { - u32 to = 0, v; + u32 v; int n; for (n = 0; n < 4; n++) { @@ -2567,55 +1865,18 @@ static u32 t6040_atc_s2r_entry(struct t6040_sleep *s) writel(readl(c + 0x08) | BIT(0), c + 0x08); udelay(5); writel(readl(c + 0x28) | BIT(0), c + 0x28); - if (readl_poll_timeout_atomic(c + 0x04, v, v & BIT(8), 1, 1000)) - to |= BIT(n); - } - return to; -} - -static void t6040_atc_rec_store(struct t6040_sleep *s, u32 to) -{ - u32 w[16] = { T6040_ATCF_MAGIC }; - int n, i; - - for (n = 0; n < 4; n++) { - void __iomem *c = s->atc_cfg[n]; - - if (!c) - continue; - w[1 + 2 * n] = readl(c + 0x04); - w[2 + 2 * n] = (readl(c + 0x08) & 0xff) | (readl(c + 0x24) & 0xff) << 8 | - (readl(c + 0x28) & 0xff) << 16 | (readl(c + 0x2c) & 0xff) << 24; + readl_poll_timeout_atomic(c + 0x04, v, v & BIT(8), 1, 1000); } - w[9] = to; - for (i = 0; i < 16; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(0xa240 + 4 * i, SPMI_OP_WRITE4), &w[i], NULL, 0); } -static int t6040_atc_cfg_dump_set(const char *val, const struct kernel_param *kp) -{ - struct t6040_sleep *s = t6040_sleep; - int n; - - if (!s) - return -ENODEV; - for (n = 0; n < 4; n++) { - void __iomem *c = s->atc_cfg[n]; - - if (c) - dev_info(s->dev, "atc%d cfg: +0x00 %08x +0x04 %08x +0x08 %08x +0x24 %08x +0x28 %08x +0x2c %08x\n", - n, readl(c), readl(c + 0x04), readl(c + 0x08), readl(c + 0x24), - readl(c + 0x28), readl(c + 0x2c)); - } - return 0; -} - -static const struct kernel_param_ops t6040_atc_cfg_dump_ops = { - .set = t6040_atc_cfg_dump_set, -}; -module_param_cb(atc_cfg_dump, &t6040_atc_cfg_dump_ops, NULL, 0200); -MODULE_PARM_DESC(atc_cfg_dump, "Bring-up: write anything to log the 4 ATCPHY_CFG blocks now"); - +/* + * The 13 fabric power words. On the platform the fabrics stay up through the entry, and the wake + * polls these words for automatic mode with target 0xf. Under Linux genpd has powered some + * of them off with their last consumer by now (FAB2_SOC with the ATC commons at + * suspend_noirq). Right before the S2R timer each one not in that state is raised: target + * 0xf, ACTUAL 0xf polled, then automatic mode. The ATC commons and the ports' PHY sleep + * entry follow, in this order. + */ static const struct { bool east; u16 off; @@ -2629,55 +1890,31 @@ static const struct { { false, 0x298 } /* FAB_AFR */, }; -#define T6040_FAB_PMU 0xa240 -#define T6040_FAB_MAGIC 0x53424146 /* "FABS" in PMU byte order */ - static void t6040_fabrics_on(struct t6040_sleep *s) { - u32 rec[16] = { T6040_FAB_MAGIC }, atc_to; int i; for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { - void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; + void __iomem *b = t6040_fabrics[i].east ? s->pmgr_east : s->pmgr_soc; u32 off = t6040_fabrics[i].off, v; - if (!b) { - rec[3 + i] = 0xdeadbeef; + if (!b) continue; - } v = readl(b + off); - if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != 0xf) { - rec[1] |= BIT(i); - if (t6040_ps_raise(b, off)) - rec[2] |= BIT(i); - } - rec[3 + i] = readl(b + off); + if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != 0xf) + t6040_ps_raise(b, off); } - t6040_atc_commons_on(s, &rec[2]); - atc_to = t6040_atc_s2r_entry(s); - for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { - void __iomem *b = t6040_fabrics[i].east ? s->snap_east : s->snap_soc; - - rec[3 + i] = b ? readl(b + t6040_fabrics[i].off) : 0xdeadbeef; - } - if (READ_ONCE(atc_rec)) - t6040_atc_rec_store(s, atc_to); - else - for (i = 0; i < ARRAY_SIZE(rec); i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_FAB_PMU + 4 * i, SPMI_OP_WRITE4), - &rec[i], NULL, 0); - if (rec[2]) - t6040_mark_raw(s, 0x54d00000 | (rec[2] & 0xffff)); + t6040_atc_commons_on(s); + t6040_atc_s2r_entry(s); } /* - * On this revision the D2S program does not write the DCS power-state words; it expects - * them in automatic mode (bit 28) with PS_MIN 0, so each DCS domain drops to 0 after - * self-refresh and its pwrgate block reports bit 30. The platform sets that at boot - * (0x0f0000ff -> 0x1f0000ff) on all 16 DCS PS words; the Linux - * DT leaves the ps_dcs_* domains disabled, so they stay at target f with AUTO off. dcs_dump - * logs them and the 8 AMCC PS words; dcs_ps_auto bit 0 sets AUTO now (the platform order: PS_MIN - * cleared, target f, ACTUAL f polled, then AUTO) and again in .begin. + * The memory channels' power gating. The sleep engine does not write the DCS power words: it + * expects each DCS to gate itself after self-refresh. That takes the DCS's dynamic power-gate + * mode (DCS +0x20 bits 12 and 8) and its power word in automatic mode with no minimum state. + * The platform sets both at boot and again after each wake. Nothing does under Linux (the DT leaves + * the ps_dcs_* domains disabled: target 0xf, no automatic mode), and the engine then waited + * for the first DCS pair's power gate for good on the J616s. Both are applied in each .begin. */ static const struct { bool east; @@ -2691,12 +1928,15 @@ static const struct { static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) { - void __iomem *b = t6040_dcs_ps[i].east ? s->snap_east : s->snap_soc; + void __iomem *b = t6040_dcs_ps[i].east ? s->pmgr_east : s->pmgr_soc; return b ? b + t6040_dcs_ps[i].off : NULL; } -/* Returns a mask of the DCS whose ACTUAL did not reach f before AUTO was set. */ +/* + * The order: the minimum state cleared, target 0xf, ACTUAL 0xf polled, then automatic + * mode. Returns a mask of the DCS whose ACTUAL did not reach 0xf. + */ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) { u32 to = 0, v; @@ -2742,7 +1982,6 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) * next one meets the bridge as the first wake armed it. Only while ANS is on: the bridge sits * in it. Returns whether the bit was set. */ - static bool t6040_ans_bridge_quiesce(struct t6040_sleep *s) { void __iomem *bridge = s->axi2af_bridge[AXI2AF_ANS]; @@ -2784,29 +2023,6 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) s->ans_bridge_quiesced = t6040_ans_bridge_quiesce(s); } -/* - * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of - * t8140_sleep_raise_apcie(). Runs atomic on the waking core in - * t6040_sleep_return(), AFTER t6040_axi2af_rearm() (the ANS/apcie_sys fabric - * bridges must be re-armed before link traffic) and BEFORE control returns to - * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. - * The two lanes come up together; the PLL/clkgen/PHY tables run once (shared). - * - * Not known (first S2R on hardware): whether ANS trains on one lane or needs both, and whether - * the two apcie_sys_st domains need their own axi2af bridge re-arm (they are NOT - * in the 15-domain axi2af workaround table, so none is done here). - */ -/* - * On the J616s the resume took an SError between marks 0x37 and 0x38, in here: the AXI enables - * (+0x104/+0x108 of 0x4_1600_0000) are the axi2af bridge of APCIE_SYS_GP, which the commit - * (apcie_seq) left off together with GP, and nothing raised it. The root complex enable: - * gates idx 0..6 = ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1 in that - * order, each to ACTUAL 0xf, setting the axi2af bit0 after ANS and SYS_GP; the PLLs; then gate - * idx 7 = APCIE_PHY_SW (which the commit gated) before the PHY, and "refpll_clk_good" (PHY - * common +0 bit31) before the PHY init. apcie_wake bit0: that domain order and both bridges; - * bit1: APCIE_PHY_SW on and the refpll wait. Marks 0x50..0x55 follow the steps. - */ - static void t6040_bridge_rearm(void __iomem *bridge) { if (!bridge) @@ -2815,6 +2031,22 @@ static void t6040_bridge_rearm(void __iomem *bridge) writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); } +/* + * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of + * #1208's t8140_sleep_raise_apcie(). Runs atomic on the waking core in + * t6040_sleep_return(), AFTER t6040_axi2af_rearm() and BEFORE control returns to + * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. + * Both lanes come up; the PLL/clkgen/PHY tables run once (shared). The two + * apcie_sys_st domains are not in the 15-domain axi2af table, so their bridges + * get no re-arm. + * + * The AXI enables (+0x104/+0x108 of 0x4_1600_0000) are the fabric bridge of + * APCIE_SYS_GP, which the commit lowered together with APCIE_GP: both are raised + * here before the enables are written. APCIE_PHY_SW, which the commit gated, + * comes back before the PHY, and the PHY's reference PLL (common +0 bit 31) is + * waited for before the PHY's clocks are requested. @apcie_wake_step tells a + * failed raise's stage. + */ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; @@ -2848,7 +2080,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST0); ret |= t6040_ps_raise(ps, PS_APCIE_ST1); ret |= t6040_ps_raise(ps, PS_APCIE_SYS_ST1); - t6040_ret_mark(s, 0x50); + t6040_wake_sync(); s->apcie_wake_step = 1; if (ret) return ret; @@ -2863,7 +2095,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) a->clkgen + APCIE_CLK_CTRL); ret = readl_poll_timeout_atomic(a->clkgen + APCIE_CLK_CTRL, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); - t6040_ret_mark(s, 0x51); + t6040_wake_sync(); s->apcie_wake_step = 2; if (ret) return ret; @@ -2877,7 +2109,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) apple_tunable_apply(a->phy, a->phy_tunable); ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_COMMON, val, val & BIT(31), 1, APCIE_POLL_US); - t6040_ret_mark(s, 0x52); + t6040_wake_sync(); s->apcie_wake_step = 4; if (ret) return ret; @@ -2890,7 +2122,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) if (ret) return ret; writel(readl(ctrl) & ~APCIE_PHY_CTRL_RESET, ctrl); - t6040_ret_mark(s, 0x53); + t6040_wake_sync(); s->apcie_wake_step = 5; writel(readl(run) | APCIE_PHY_RUN_EN, run); @@ -2903,11 +2135,11 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) APCIE_POLL_US); if (ret) return ret; - t6040_ret_mark(s, 0x54); + t6040_wake_sync(); s->apcie_wake_step = 6; writel(readl(a->phy + APCIE_PHY_COMMON) | BIT(0), a->phy + APCIE_PHY_COMMON); writel(readl(ctrl) | APCIE_PHY_CTRL_UP, ctrl); - t6040_ret_mark(s, 0x55); + t6040_wake_sync(); s->apcie_wake_step = 7; /* RC ready handshake (as T8140's). */ @@ -2918,21 +2150,6 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) APCIE_POLL_US); } -/* - * The real commit (t6040_sleep_commit, reached through cpu_suspend): - * t6040_sleep_enter calls it only when no blocker remains and allow_entry is - * set. The SoC power-down here is the T6040 entry as - * measured on the J616s, which is SHORT: unlike the T8140 commit there - * is NO dcs_down / mcc_stop / pwrgate / PTD / clk-mux-per-lane / CLLT / dvfm / - * AMCC / ps_seq list (the platform writes none of them). The - * order is the, as measured: WD0 off, PMU boot-stage 0x50 - * + NOTIFY over spmi0/spmi-a0, the AON 5 s net, the three clusters off, the - * clk-mux park, the axi2af read-check, the AURAS2R1 mailbox, the MasterMini S2R - * timer (0x508904000 := 1), the AIC masked, the parked CPUs released, the - * optional PMU "go hib" replay, and the deep WFI. All of it is measured except the - * clk-mux park (not observed) and the go-hib replay (measured bytes, necessity unproven). - * A hang here ends at WD2. - */ /* * The first thing Linux runs after a wake, MMU off and no stack (#1314's T8132 stub). The * last wake program leaves the AON WD2 running for 5 s, less than the device resume takes: @@ -2960,8 +2177,6 @@ asm( ); extern char t6040_resume_stub[]; -static u32 t6040_axi2af_on_mask(struct t6040_sleep *s); - /* * The four ATC complexes' power words in SOC (soc_ps, 0x502800000), deepest first: cio_usb, * cio_pcie, cio, pcie, common (ADT: ATCn_CIO_USB .. ATCn_COMMON). The platform has them off at its @@ -2994,7 +2209,6 @@ static int t6040_ps_lower_word(void __iomem *reg) PS_OFF_TIMEOUT_US); } -/* Marks 0x57PKSOOO: ATC P, word K, S 0 written / 8 off / 4 still on, OOO its offset. */ static void t6040_sleep_lower_atc(struct t6040_sleep *s) { int p, k; @@ -3003,22 +2217,19 @@ static void t6040_sleep_lower_atc(struct t6040_sleep *s) return; for (p = 0; p < ARRAY_SIZE(t6040_atc_words); p++) for (k = 0; k < ARRAY_SIZE(t6040_atc_words[0]); k++) { - u32 off = t6040_atc_words[p][k]; - void __iomem *reg = s->soc_ps + off; + void __iomem *reg = s->soc_ps + t6040_atc_words[p][k]; u32 val = readl(reg); - u32 m = 0x57000000 | p << 20 | k << 16 | off; if (!FIELD_GET(PS_TARGET, val) && !FIELD_GET(PS_ACTUAL, val)) continue; - t6040_mark_raw(s, m); - t6040_mark_raw(s, m | (t6040_ps_lower_word(reg) ? 0x4000 : 0x8000)); + t6040_ps_lower_word(reg); } } /* * T8140's radio port stop, on the same t602x port block: the link into L2, the endpoint's * PERST# asserted, the PHY's clocks off. The endpoint keeps its power and comes back by its - * host after the sleep. Marks 0x541P00RR: port P, RR 1 started, 2 done. + * host after the sleep. */ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) { @@ -3032,7 +2243,6 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) s->gp_appclk_saved[p] = readl(port + GP_PORT_APPCLK); s->gp_perst_saved[p] = readl(port + GP_PORT_PERST); s->gp_saved[p] = true; - t6040_mark_raw(s, 0x54100001 | p << 16); writel(readl(port + GP_PORT_INTMSK) | GP_PORT_INT_LINK, port + GP_PORT_INTMSK); writel(GP_PORT_INT_LINK, port + GP_PORT_INTSTAT); writel(readl(port + GP_PORT_APPCLK) | GP_PORT_APPCLK_CGDIS, port + GP_PORT_APPCLK); @@ -3061,7 +2271,6 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) writel(readl(phy) & ~GP_PHY_CLK1REQ, phy); writel(readl(port + GP_PORT_APPCLK) & ~GP_PORT_APPCLK_EN, port + GP_PORT_APPCLK); writel(readl(port + GP_PORT_INTSTAT), port + GP_PORT_INTSTAT); - t6040_mark_raw(s, 0x54100002 | p << 16); } /* The GP host's two ports, while the block's domains are all still on. */ @@ -3078,13 +2287,13 @@ static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) /* * apcie_sys_gp (AUTO off, axi2af bit0s, target 0), then apcie_gp. After ANS's ST lanes: the * storage lanes' cio3pll/clkgen/PHY windows sit in apcie_gp, and touching them with it off - * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. Marks 0x543x/0x544x. + * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. APCIE_GP goes to clock-gated + * (0x244), the state the sleep engine itself asks for, not to off. */ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) { void __iomem *sys, *gp, *bridge = s->axi2af_bridge[AXI2AF_APCIE_SYS_GP]; u32 val; - int ret; if (!s->soc_east) return; @@ -3093,7 +2302,6 @@ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) if (!FIELD_GET(PS_ACTUAL, readl(sys))) return; - t6040_mark_raw(s, 0x54300001); val = readl(sys); if (val & PS_AUTO_ENABLE) { val &= ~(PS_AUTO_ENABLE | PS_FLAGS); @@ -3104,20 +2312,28 @@ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) writel(readl(bridge + AXI2AF_BRIDGE_108) & ~BIT(0), bridge + AXI2AF_BRIDGE_108); } writel((val & ~PS_TARGET) | PS_FLAGS, sys); - ret = readl_poll_timeout_atomic(sys, val, !FIELD_GET(PS_ACTUAL, val), 1, PS_OFF_TIMEOUT_US); - t6040_mark_raw(s, 0x54300002 | (ret ? 0x80 : 0)); + readl_poll_timeout_atomic(sys, val, !FIELD_GET(PS_ACTUAL, val), 1, PS_OFF_TIMEOUT_US); - t6040_mark_raw(s, 0x54400001); writel(0x244, gp); - ret = readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, PS_OFF_TIMEOUT_US); - t6040_mark_raw(s, 0x54400002 | (ret ? 0x80 : 0)); + readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, PS_OFF_TIMEOUT_US); } +/* + * The real commit (t6040_sleep_commit, reached through cpu_suspend): t6040_sleep_enter + * calls it only when no blocker remains and allow_entry is set. In order: the stopped CPUs + * released to their last WFI, the ATC complexes and the GP PCIe ports lowered, ANS's storage + * link quiesced, WD0 off and the AON WD2 net armed, the PMU's boot stage 0x50 and + * NOTIFY_ENABLE over spmi0/spmi-a0, the steps of the PMGR quiesce (APSC, PLL relock, + * TVMs, the AGX gate, the five complexes, the axi2af read-check, the clock quiesce, the + * keep-alive bits), aura's mailbox armed with the resume entry, the fabrics and the ATC + * ports readied for the sleep engine, the MasterMini S2R timer (0x508904000 := 1), the AIC + * masked and the deep WFI. The watchdog, PMU, cluster, axi2af and timer writes are as + * measured on the J616s. A hang here ends at WD2. + */ static int t6040_sleep_commit(unsigned long unused) { struct t6040_sleep *s = t6040_sleep; - t6040_mark(s, 0x20); s->siq_cfg = read_sysreg_s(SYS_APL_SIQ_CFG_EL1); spin_table_stop_parked(); /* @@ -3126,32 +2342,21 @@ static int t6040_sleep_commit(unsigned long unused) * J616s: release them now, and wait for their power words to read off further down. */ spin_table_release_stopped(); - t6040_mark(s, 0x21); t6040_sleep_lower_atc(s); t6040_sleep_stop_gp_ports(s); /* - * lower_io: ANS's two ST lanes + shared PHY down, once ANS has been - * stopped by its driver. The platform does this ~40 ms before ANS idle; - * here it follows every refusal, so a refused sleep finds the link up. - * No-op unless the apple,apcie windows are present (apcie_st.valid). + * ANS's storage link, once ANS has been stopped by its driver (see + * t6040_apcie_phy_off). It follows every refusal, so a refused sleep finds + * the link up. No-op unless the apple,apcie windows are present (apcie_st.valid). */ - if (s->soc_east && - t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1)) - t6040_mark_raw(s, 0x54c00001); + if (s->soc_east) + t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1); if (s->pcie_ans_stopped) t6040_sleep_lower_apcie_st(s); t6040_sleep_lower_apcie_gp(s); - /* - * What the quiesce gate would panic on, after everything here has lowered what it - * lowers: 0x58 then the mask of the axi2af domains still on. The S2R timer then resets - * the SoC without a sleep, so the mark is kept: the wake alarm is not put back. - */ - s->commit_on_mask = t6040_axi2af_on_mask(s); - - t6040_mark(s, 0x22); - /* WD0 off, then the AON 5 s reconfig net (as the platform arms it for a reconfig). */ + /* WD0 off, then the AON WD2 net (the platform arms 5 s for its reconfig). */ if (s->wdt) writel(0, s->wdt + WDT_WD0_CTL); t6040_sleep_set_wd2(s, clamp(READ_ONCE(sleep_wd_secs), 1U, WDT_MAX_SECS)); @@ -3159,7 +2364,6 @@ static int t6040_sleep_commit(unsigned long unused) if (s->smc_wdt_deadline) writel(0, s->smc_wdt_deadline); - t6040_mark(s, 0x23); /* PMU boot stage 0x50 and NOTIFY_ENABLE on both SPMI controllers (measured). */ t6040_pmu_write(s, PMU_BOOT_STAGE, PMU_BOOT_STAGE_SLEEP); if (s->spmi0) @@ -3174,22 +2378,15 @@ static int t6040_sleep_commit(unsigned long unused) * pwrgate voters are not written on entry: they read back 0x80000000 * already; the wake side re-asserts them. */ - s->quiesce_to = 0; - t6040_mark(s, 0x2c); - s->quiesce_to |= min(t6040_secondaries_off(s), 15U) << 20; - t6040_mark(s, 0x2b); + t6040_secondaries_off(s); t6040_pmgr_quiesce(s); - t6040_mark(s, 0x2a0 | t6040_agx_gate(s)); - t6040_mark(s, 0x24); + t6040_agx_gate(s); t6040_clusters_off(s); /* Read-only: the axi2af panic gate the platform checks before the engine goes. */ t6040_axi2af_readcheck(s); - t6040_mark(s, 0x350); t6040_clkgen_park(s); - t6040_mark(s, 0x351); s->ka_on_before = t6040_keep_alive(s, false, 0); - t6040_mark(s, 0x25); /* Arm aura's AURAS2R1 mailbox with this boot's resume entry. */ if (s->mbox) { @@ -3201,30 +2398,8 @@ static int t6040_sleep_commit(unsigned long unused) } t6040_fabrics_on(s); - t6040_ps_save(s); - if (READ_ONCE(ps_snap)) - t6040_ps_snap_store(s); - - /* Last before the S2R timer: what is still on, re-read now, kept through the reset. */ - s->commit_on_mask |= t6040_axi2af_on_mask(s); - if (s->commit_on_mask) { - t6040_mark_raw(s, 0x58000000 | s->commit_on_mask); - } else if (s->quiesce_to & (QTO_CLUSTER(3) | QTO_CLUSTER(4))) { - /* - * DISP_INT (+0xc) / DISP_EXT (+0x10) kept BIT4: 0x51, then +0xc and +0x10 as - * read after the wait, then +0xc's low 6 bits and +0x10's [5:4] from before. - */ - t6040_mark_raw(s, 0x51000000 | (s->cluster_after[3] & 0xff) << 16 | - (s->cluster_after[4] & 0xff) << 8 | - (s->cluster_saved[3] & 0x3f) | - ((s->cluster_saved[4] >> 4) & 3) << 6); - } else if (s->quiesce_to) { - /* The commit ran, but a quiesce poll timed out: which ones, kept. */ - t6040_mark_raw(s, 0x5d000000 | s->quiesce_to); - } else { - /* One SPMI write: the alarm back + 1 s reads as "the commit ran". */ - t6040_mark_restore(s); - } + t6040_sio_ps_save(s); + /* The commit: the MasterMini S2R timer arms the SoC sleep engine. */ if (s->mastermini) writel(1, s->mastermini + MASTERMINI_S2R_TIMER); @@ -3238,41 +2413,12 @@ static int t6040_sleep_commit(unsigned long unused) } /* - * The wake return (t6040_sleep_return), reached on the boot core after aura's - * AURAS2R1 mailbox hands it back to cpu_resume. aura restores NO SoC register; - * every write here is the OS driver's job. It is the - * mirror of the PMGR wake restore: the three CPU complexes on, the two wake - * pwrgates, SOC-East retention off (with the PARENT_MISSING poll), the axi2af - * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the - * AP-global S2R control cleared, and the wake-status words read. - */ -/* - * Wake-return marks (seen on the J616s: an asynchronous SError on CPU 0 after the wake, last - * mark a driver step): complete every access of the step before the mark, so a pending SError is - * taken before the next mark lands and the comparator names the step that caused it. - */ -/* - * On the J616s a wake reached the device resume and took an SError in the noirq resume of the ANS - * apcie DART (0x4_1000_0000, no power domain in the DT). Record what the ANS/APCIe raise did, - * right after it: 'APCW' at PMU 0xa280 (over the ATCC record; PMVC at 0xa2ac stays), w1 = - * stopped(bit31) | step reached << 16 | ret & 0xffff, w2..w9 = the SOC-East PS words of ANS, - * GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, PHY_SW (PMGR, always readable). - */ -#define T6040_APCW_PMU 0xa280 -#define T6040_APCW_MAGIC 0x57435041 /* "APCW" in PMU byte order */ - -static bool apcie_wake_rec = true; -module_param(apcie_wake_rec, bool, 0644); -MODULE_PARM_DESC(apcie_wake_rec, "Bring-up: record the wake-side ANS/APCIe raise at PMU 0xa280 ('APCW')"); - -/* - * With the apcie fabric up, the noirq resume of 410000000.iommu still took the SError: - * that DART sits in the block of port 0 of the GP PCIe host (pcie@1cb0000000, Wi-Fi/BT; port 1 - * SD behind 411000000.iommu), whose clocks the bootloader set up at boot and nobody after an - * S2R. pcie_port_clk (mask of ports) undoes t6040_gp_port_stop()'s PHY/app clock part right after - * the apcie raise: lane REFCLK0/1 request + ack, REFCLKEN, PORT_APPCLK EN, PORT_STATUS ready. - * The link itself stays down. Result per port in a byte of APCW w10: bit0 refclk acked, - * bit1 port ready, bit4 refclk timeout, bit5 ready timeout, bit7 not mapped. + * The GP PCIe host's two ports (Wi-Fi/BT, SD): the boot loader set their clocks up, and + * nobody does after a sleep. The DART in port 0's block then faulted in its noirq resume on + * the J616s. t6040_gp_port_stop() backwards, short of the link: the lane reference clocks + * requested and acknowledged, the PHY's enables and the port's application clock as saved, + * the controller's PERST register as saved, then the port's ready bit polled. The link + * itself stays down for the host driver. */ #define GP_PHY_CLK1ACK BIT(3) #define GP_PORT_STATUS 0x804 @@ -3280,28 +2426,24 @@ MODULE_PARM_DESC(apcie_wake_rec, "Bring-up: record the wake-side ANS/APCIe raise static void t6040_gp_pcie_clk(struct t6040_sleep *s) { - u32 res = 0, val; + unsigned int timeouts = 0; + u32 val; int i; for (i = 0; i < 2; i++) { void __iomem *phy = s->gp_phy[i], *port = s->gp_port[i]; - u32 r = 0, cfg, app; + u32 cfg, app; - if (!phy || !port || !s->gp_saved[i]) { - res |= BIT(7) << (8 * i); + if (!phy || !port || !s->gp_saved[i]) continue; - } cfg = s->gp_phy_saved[i]; app = s->gp_appclk_saved[i]; - /* t6040_gp_port_stop() backwards: clock requests, reference buffer, enables, link. */ writel(readl(phy) | GP_PHY_CLK0REQ, phy); if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK0ACK, 1, 50000)) - r |= BIT(4); + timeouts++; writel(readl(phy) | GP_PHY_CLK1REQ, phy); if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK1ACK, 1, 50000)) - r |= BIT(4); - if (!(r & BIT(4))) - r |= BIT(0); + timeouts++; if (!(cfg & GP_PHY_RESET)) { writel(readl(phy) & ~GP_PHY_RESET, phy); udelay(1); @@ -3311,67 +2453,46 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) writel((readl(port + GP_PORT_APPCLK) & ~(GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)) | (app & (GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)), port + GP_PORT_APPCLK); /* - * Clocks back (refclk acked) but no PORT_STATUS ready: the stop also wrote - * the controller's PERST register 0. Put it back as it was (the endpoint's PERST# - * pin stays asserted), with the guard bit the platform sets around it. + * The stop also wrote the controller's PERST register 0, and without it the + * port does not report ready. Put it back as it was (the endpoint's PERST# pin + * stays asserted), with the guard bit the platform sets around it. */ writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); writel(s->gp_perst_saved[i], port + GP_PORT_PERST); writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); if (readl_poll_timeout_atomic(port + GP_PORT_STATUS, val, val & GP_PORT_STATUS_READY, 1, 50000)) - r |= BIT(5); - else - r |= BIT(1); - res |= r << (8 * i); + timeouts++; } - s->gp_port_res = res; -} - -static void t6040_apcie_wake_rec(struct t6040_sleep *s) -{ - static const u16 off[8] = { PS_ANS, PS_APCIE_GP, PS_APCIE_SYS_GP, PS_APCIE_ST0, - PS_APCIE_SYS_ST0, PS_APCIE_ST1, PS_APCIE_SYS_ST1, - PS_APCIE_PHY_SW }; - u32 w[11]; - int i; - - if (!READ_ONCE(apcie_wake_rec) || !s->soc_east || !s->spmi0) - return; - w[10] = s->gp_port_res; - w[0] = T6040_APCW_MAGIC; - w[1] = (s->pcie_ans_stopped ? BIT(31) : 0) | (s->apcie_wake_step & 0xff) << 16 | - ((u32)s->pcie_ans_on_ret & 0xffff); - for (i = 0; i < 8; i++) - w[2 + i] = readl(s->soc_east + off[i]); - for (i = 0; i < 11; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_APCW_PMU + 4 * i, SPMI_OP_WRITE4), &w[i], - NULL, 0); -} - -static void t6040_ret_mark(struct t6040_sleep *s, u32 n) -{ - dsb(sy); - isb(); - t6040_mark(s, n); + s->gp_clk_timeouts = timeouts; } +/* + * The wake return (t6040_sleep_return), reached on the boot core after aura's + * AURAS2R1 mailbox hands it back to cpu_resume. aura restores NO SoC register; + * every write here is the OS driver's job. It is the + * mirror of the PMGR wake restore: the three CPU complexes on, the two wake + * pwrgates, SOC-East retention off (with the PARENT_MISSING poll), the axi2af + * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the + * AP-global S2R control cleared, and the wake-status words read. Between them, + * what a Linux sleep needs on top: ANS's storage link, the GP PCIe ports' clocks + * and the AOP island's power words. + */ static void t6040_sleep_return(struct t6040_sleep *s) { u32 val; int i, ret; - t6040_mark(s, 0x30); /* The reset left the timer and IRQ routing at their EL2 defaults. */ write_sysreg(s->cntkctl, cntkctl_el1); isb(); write_sysreg_s(s->siq_cfg, SYS_APL_SIQ_CFG_EL1); - t6040_ret_mark(s, 0x31); + t6040_wake_sync(); spin_table_stop_finished(); - t6040_ret_mark(s, 0x32); + t6040_wake_sync(); aic_sleep_restore(); - t6040_ret_mark(s, 0x33); + t6040_wake_sync(); /* * The three CPU complexes back on (E, P0, P1), nibble 0xf, poll +0xc for @@ -3387,14 +2508,14 @@ static void t6040_sleep_return(struct t6040_sleep *s) val = readl(s->cluster_ctl + CLUSTER_CTL(i)); writel((val & ~CLUSTER_CTL_NIBBLE) | on, s->cluster_ctl + CLUSTER_CTL(i)); } - t6040_ret_mark(s, 0x34); + t6040_wake_sync(); /* Wake pwrgates and CPM voters: read, set BIT31. */ if (s->pwrgate) { writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_0); writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_1); } - t6040_ret_mark(s, 0x35); + t6040_wake_sync(); /* SOC-East retention off: clear DEV_DISABLE, then poll PARENT_MISSING clear. */ if (s->soc_east) { @@ -3406,7 +2527,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) if (ret) pr_warn("t6040-sleep: SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); } - t6040_ret_mark(s, 0x36); + t6040_wake_sync(); /* * The SOC DPE restore (the 14 DPE energy accumulators |= 1) is skipped: @@ -3416,7 +2537,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) /* The fabric-bridge retention re-arm (the ANS/Wi-Fi/display-dead guard). */ t6040_axi2af_rearm(s); - t6040_ret_mark(s, 0x37); + t6040_wake_sync(); /* * Raise ANS's two ST lanes + shared PHY from power-off, AFTER the fabric @@ -3428,13 +2549,10 @@ static void t6040_sleep_return(struct t6040_sleep *s) s->pcie_ans_on_ret = 0; if (s->pcie_ans_stopped) s->pcie_ans_on_ret = t6040_sleep_raise_apcie_st(s); - s->gp_port_res = 0; t6040_gp_pcie_clk(s); - t6040_ret_mark(s, 0x56); - t6040_apcie_wake_rec(s); - t6040_aop_wake(s); - t6040_ret_mark(s, 0x57); - t6040_ret_mark(s, 0x38); + t6040_wake_sync(); + t6040_aop_ps_restore(s); + t6040_wake_sync(); if (s->pwrgate) { writel((readl(s->pwrgate + PWRGATE_CPM_0) & ~PWRGATE_ON) | PWRGATE_ON, @@ -3444,25 +2562,25 @@ static void t6040_sleep_return(struct t6040_sleep *s) writel((readl(s->pwrgate + PWRGATE_CPM_2) & ~PWRGATE_ON) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_2); } - t6040_ret_mark(s, 0x39); + t6040_wake_sync(); /* Undo the entry clk-mux park; the clock restore is otherwise a NOP. */ t6040_clkgen_restore(s); - t6040_ret_mark(s, 0x3a); + t6040_wake_sync(); /* Software ack of the armed S2R request (the platform has no stop of the S2R timer). */ if (s->aop_global) writel(0, s->aop_global + AOP_S2R_CTRL); - t6040_ret_mark(s, 0x3b); + t6040_wake_sync(); /* The keep-alive bits the commit cleared, back (as the wake restore enables them). */ if (s->ka_on_before) t6040_keep_alive(s, true, s->ka_on_before); - t6040_ret_mark(s, 0x3c); + t6040_wake_sync(); /* A longer WD2 over the device resume, until .end. */ t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); - t6040_ret_mark(s, 0x3f); + t6040_wake_sync(); if (s->mbox) { dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); @@ -3487,24 +2605,13 @@ static int t6040_sleep_valid(suspend_state_t state) static bool t6040_sleep_mem_begun; /* - * The refusal-first gate. Default: print the blocker list and return an error - * here, before any device suspends, so nothing is entered. A dry run proceeds - * and refuses at .enter. allow_entry with no blocker and no dry run: the - * path that reaches the commit. + * SMC key pmVC, the request for the VDD_CIO rail: the platform writes its enable mask | 0x100 + * (0x101 on the J616s) while an ATC port's always-on side is powered, also at sleep. Linux + * never asks for the rail, which is power-gated otherwise (ADT vdd-cio-rail-pg), and a rail + * the SMC drops during the sleep transition races the sleep engine's hand-over of the ATC + * ports. Written in each .begin. */ -/* - * Seq 25 stalls because ATC_n_COMMON does not come back up (port 0 or port 1 at random). - * The platform writes SMC key pmVC (the VDD_CIO rail request, - * its enable mask | 0x100) = 0x101 while any ATC USB_AON is on, also at sleep; Linux never - * does, and the ADT marks the rail power-gated (vdd-cio-rail-pg). A rail the SMC drops during - * the sleep transition would race the engine. pmvc != 0 writes that value in .begin and logs - * the key before and after. - */ - -/* The .begin log does not reach the journal; keep the result in PMU SRAM 0xa2ac. */ #define T6040_PMVC_REQUEST 0x101 -#define T6040_PMVC_PMU 0xa2ac -#define T6040_PMVC_MAGIC 0x43564d50 /* "PMVC" in PMU byte order */ /* The SMC node is apple,t6041-smc + apple,t8103-smc here (macsmc's match list). */ static const struct of_device_id t6040_smc_match[] = { @@ -3513,64 +2620,31 @@ static const struct of_device_id t6040_smc_match[] = { {} }; -static int t6040_sleep_pmvc_write(struct t6040_sleep *s, u32 v, bool rec) +static void t6040_sleep_pmvc(struct t6040_sleep *s) { struct platform_device *pdev; struct device_node *np; struct apple_smc *smc; - u32 before = 0, after = 0, w[5]; - int rb = -ENODEV, ret = -ENODEV, ra = -ENODEV, i; + int ret = -ENODEV; np = of_find_matching_node(NULL, t6040_smc_match); pdev = np ? of_find_device_by_node(np) : NULL; of_node_put(np); smc = pdev ? platform_get_drvdata(pdev) : NULL; - if (smc) { - rb = apple_smc_read_u32(smc, SMC_KEY(pmVC), &before); - ret = apple_smc_write_u32(smc, SMC_KEY(pmVC), v); - ra = apple_smc_read_u32(smc, SMC_KEY(pmVC), &after); - } + if (smc) + ret = apple_smc_write_u32(smc, SMC_KEY(pmVC), T6040_PMVC_REQUEST); if (pdev) put_device(&pdev->dev); - dev_info(s->dev, "pmVC: read %d 0x%08x, write 0x%08x %d, read %d 0x%08x\n", - rb, before, v, ret, ra, after); - if (rec) { - w[0] = T6040_PMVC_MAGIC; - w[1] = (u16)rb | (u32)(u16)ret << 16; - w[2] = before; - w[3] = (u16)ra; - w[4] = after; - for (i = 0; i < 5; i++) - t6040_pmu_cmd(s, SPMI_CMD_PMU(T6040_PMVC_PMU + 4 * i, SPMI_OP_WRITE4), - &w[i], NULL, 0); - } - return ret; -} - -static int t6040_pmvc_now_set(const char *val, const struct kernel_param *kp) -{ - u32 v; - int ret = kstrtou32(val, 0, &v); - - if (ret) - return ret; - if (!t6040_sleep) - return -ENODEV; - ret = t6040_sleep_pmvc_write(t6040_sleep, v, false); - return ret < 0 ? ret : 0; -} - -static const struct kernel_param_ops t6040_pmvc_now_ops = { - .set = t6040_pmvc_now_set, -}; -module_param_cb(pmvc_now, &t6040_pmvc_now_ops, NULL, 0200); -MODULE_PARM_DESC(pmvc_now, "Bring-up: write this value to SMC key pmVC now and log the key before/after"); - -static void t6040_sleep_pmvc(struct t6040_sleep *s) -{ - t6040_sleep_pmvc_write(s, T6040_PMVC_REQUEST, true); + if (ret < 0) + dev_warn(s->dev, "pmVC: write %#x failed: %d\n", T6040_PMVC_REQUEST, ret); } +/* + * The refusal-first gate. Default: print the blocker list and return an error + * here, before any device suspends, so nothing is entered. A dry run proceeds + * and refuses at .enter. allow_entry with no blocker and no dry run: the + * path that reaches the commit. + */ static int t6040_sleep_begin(suspend_state_t state) { struct t6040_sleep *s = t6040_sleep; @@ -3596,7 +2670,6 @@ static int t6040_sleep_begin(suspend_state_t state) s->returned = false; s->gp_saved[0] = s->gp_saved[1] = false; t6040_aop_ps_save(s); - t6040_susf_begin(s); t6040_sleep_pmvc(s); t6040_dcs_dyn_pg(s); dcs_to = t6040_dcs_ps_auto_on(s); @@ -3617,10 +2690,6 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_sleep_power_ops(s); t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); pm_set_suspend_via_firmware(); - t6040_mark_save(s); - t6040_mark(s, 0x01); - if (s->alarm_ok) - WRITE_ONCE(pm_bringup_dev_mark, t6040_dev_mark); return 0; } @@ -3654,7 +2723,6 @@ static int t6040_sleep_begin(suspend_state_t state) /* After the device suspend, in process context. */ static int t6040_sleep_prepare(void) { - t6040_mark(t6040_sleep, 0x02); t6040_sleep_watch_bound(t6040_sleep); return 0; @@ -3674,17 +2742,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) pr_info("t6040-sleep: dry run %u at the commit point: %u CPU(s) online\n", dry, num_online_cpus()); - t6040_log_ane(s, "dry-run commit point"); - t6040_log_a2d(s, "dry-run commit point"); t6040_sleep_log_s2r_state(s, "dry-run commit point"); - { - u32 map[T6040_PS_SNAP_WORDS]; - - t6040_ps_snap_take(s, map); - pr_info("t6040-sleep: dry-run commit point: PS snapshot %*ph\n", (int)sizeof(map), map); - if (READ_ONCE(ps_snap)) - t6040_ps_snap_store(s); - } /* * A wake source pending and left unmasked by the masks a real commit arms would keep the * AP partition from powering down (A2D then stalls at MiniPMGR +0x40). Read only. @@ -3734,12 +2792,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) #define PS_BUSY BIT(11) #define PS_RESET_REQ BIT(12) -/* - * The display mark: 0x5cGGSOOO, GG the group, S 0 before the write, 8 off, 4 still on; - * for a gated CPU 1 waiting for its gate, 2 gated, 3 not gated in time. - */ -#define T6040_DISP_MARK(g, st, off) (0x5c000000 | ((g) & 0xff) << 16 | (st) << 12 | ((off) & 0xfff)) - #define DISP_CPU_GATE_US 50000 /* @@ -3811,40 +2863,27 @@ static int t6040_psw_poll(const struct t6040_psw *w, u32 mask, u32 want) return -ETIMEDOUT; } -/* Marks 0x56SS00RR: SS the word's step, RR what it reached (bit 7: a poll timed out). */ -static void t6040_disp_mark(struct t6040_sleep *s, u32 step, u32 rr) -{ - t6040_mark_raw(s, 0x56000000 | (step & 0xff) << 16 | (rr & 0xff)); -} - /* * The power-down of a PS word: with * AUTO_ENABLE set, take it out of automatic mode with the target kept (v & 0xeffffcff) and * wait for ACTUAL 15; then @pre (the SYS hooks); then TARGET 0 with bits 9:8 written 1 and * bits 10-12, 24-31 kept; then poll bit11 clear and ACTUAL 0. */ -static int t6040_psw_off(struct t6040_sleep *s, const struct t6040_psw *w, u32 step, +static int t6040_psw_off(struct t6040_sleep *s, const struct t6040_psw *w, void (*pre)(struct t6040_sleep *s)) { u32 v = t6040_psw_read(w); - int ret; if (v & PS_AUTO_ENABLE) { - t6040_disp_mark(s, step, 0x01); t6040_psw_write(w, v & 0xeffffcff); - ret = t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); - t6040_disp_mark(s, step, 0x02 | (ret ? 0x80 : 0)); + t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); v = t6040_psw_read(w); } if (pre) pre(s); - t6040_disp_mark(s, step, 0x03); t6040_psw_write(w, (v & 0xfffffcf0) | 0x300); - ret = t6040_psw_poll(w, PS_BUSY, 0); - t6040_disp_mark(s, step, 0x04 | (ret ? 0x80 : 0)); - ret = t6040_psw_poll(w, PS_ACTUAL, 0); - t6040_disp_mark(s, step, 0x05 | (ret ? 0x80 : 0)); - return ret; + t6040_psw_poll(w, PS_BUSY, 0); + return t6040_psw_poll(w, PS_ACTUAL, 0); } /* DISP_SYS's hooks before its PS write: the axi2af bridge bit0s, then the retention pair. */ @@ -3860,7 +2899,6 @@ static void t6040_disp_sys_pre(struct t6040_sleep *s) writel(3, s->disp_pwrgate + DISP_PWRGATE_A); writel(0, s->disp_pwrgate + DISP_PWRGATE_B); } - t6040_disp_mark(s, 0x30, 0x06); } /* dart-dcp's outstanding-transaction counters ([20:16]) at 0 before its domain goes. */ @@ -3873,13 +2911,10 @@ static int t6040_dcp_dart_idle(struct t6040_sleep *s) return 0; for (i = 0; i < ARRAY_SIZE(regs); i++) while (readl(s->dcp_dart + regs[i]) & GENMASK(20, 16)) { - if (++us > DISP_PS_TIMEOUT_US) { - t6040_disp_mark(s, 0x08, 0x80 | i); + if (++us > DISP_PS_TIMEOUT_US) return -ETIMEDOUT; - } udelay(1); } - t6040_disp_mark(s, 0x08, 0x00); return 0; } @@ -3913,49 +2948,37 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); writel(0, s->pmc_dcp); writel(0, s->pmc_dcp + 4); - t6040_disp_mark(s, 0x09, (s->pmc_dcp_saved[0] & 0xf) | (s->pmc_dcp_saved[1] & 0xf) << 4); } /* * DISP_CPU's word refuses any write from here: rewriting its own value at runtime reset - * the SoC (the platform reaches it through its "DISP-CPU-V" PMGR device). Its CPU is - * left in automatic mode and the PMGR gates it once the firmware idles in WFI with the - * run bit kept: wait for ACTUAL 0. Mark 0x560f00RR first: RR ACTUAL | AUTO_ENABLE << 4 | - * (TARGET != 0) << 5; then 0x561000RR, RR 0x10 gated, 0x90 not gated in time. + * the SoC. Its CPU is left in automatic mode and the PMGR gates it once the firmware + * idles in WFI with the run bit kept: wait for ACTUAL 0, after dart-dcp's transactions + * have drained, and go on either way. */ - { - u32 v = t6040_psw_read(&cpu); - - t6040_disp_mark(s, 0x0f, FIELD_GET(PS_ACTUAL, v) | !!(v & PS_AUTO_ENABLE) << 4 | - !!FIELD_GET(PS_TARGET, v) << 5); - if (FIELD_GET(PS_ACTUAL, v)) - t6040_dcp_dart_idle(s); - ret = t6040_psw_poll_long(&cpu, PS_ACTUAL, 0, DISP_CPU_GATE_US); - t6040_disp_mark(s, 0x10, ret ? 0x90 : 0x10); - ret = 0; - } - if (!ret) - ret = t6040_psw_off(s, &fe, 0x20, NULL); + if (FIELD_GET(PS_ACTUAL, t6040_psw_read(&cpu))) + t6040_dcp_dart_idle(s); + t6040_psw_poll_long(&cpu, PS_ACTUAL, 0, DISP_CPU_GATE_US); + + ret = t6040_psw_off(s, &fe, NULL); if (!ret) - ret = t6040_psw_off(s, &sys, 0x30, t6040_disp_sys_pre); + ret = t6040_psw_off(s, &sys, t6040_disp_sys_pre); return ret; } /* * The raise (the mirror): TARGET 15, poll ACTUAL 15, poll bit11; for DISP_SYS the * retention pair (A := 0, B := 1) and the axi2af bridge bit0s set; then bits 9:8 cleared and - * AUTO_ENABLE back where the platform sets it (DISP_CPU, DISP_SYS). Marks 0x56 step 0x4x/0x5x/0x6x. + * AUTO_ENABLE back where the platform sets it (DISP_CPU, DISP_SYS). */ -static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, u32 step, - bool sys, bool autoen) +static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, bool sys, + bool autoen) { u32 v = t6040_psw_read(w); - int ret, ret2; + int ret; - t6040_disp_mark(s, step, 0x01); t6040_psw_write(w, (v & ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS)) | PS_TARGET); ret = t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); - ret2 = t6040_psw_poll(w, PS_BUSY, 0); - t6040_disp_mark(s, step, 0x02 | (ret || ret2 ? 0x80 : 0)); + t6040_psw_poll(w, PS_BUSY, 0); if (sys) { void __iomem *bridge = s->axi2af_bridge[AXI2AF_DISP_SYS]; @@ -3967,37 +2990,26 @@ static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, u32 st writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); } - t6040_disp_mark(s, step, 0x06); } v = t6040_psw_read(w); t6040_psw_write(w, (v & ~PS_AUTO_ENABLE) | PS_FLAGS | (autoen ? PS_AUTO_ENABLE : 0)); - t6040_disp_mark(s, step, 0x05); return ret; } /* The internal display back on, parent first, whatever brought .enter back. */ static void t6040_disp_back(struct t6040_sleep *s) { - struct t6040_sleep_group *g = NULL; - unsigned int i; + struct t6040_psw sys = { .io = s->soc_east, .off = PS_DISP_SYS }; + struct t6040_psw fe = { .io = s->soc_east, .off = PS_DISP_FE }; if (!s->disp_down) return; s->disp_down = false; - for (i = 0; i < s->ngroups; i++) - if (!strcmp(s->groups[i].name, "disp")) - g = &s->groups[i]; - { - struct t6040_psw sys = { .io = s->soc_east, .off = PS_DISP_SYS }; - struct t6040_psw fe = { .io = s->soc_east, .off = PS_DISP_FE }; - - if (t6040_psw_on(s, &sys, 0x40, true, true)) - return; - if (t6040_psw_on(s, &fe, 0x50, false, false)) - return; - /* DISP_CPU's word can't be written from here: automatic mode raises it. */ - (void)g; - } + /* DISP_CPU's word can't be written from here: automatic mode raises it. */ + if (t6040_psw_on(s, &sys, true, true)) + return; + if (t6040_psw_on(s, &fe, false, false)) + return; if (s->pmc_dcp) { writel(s->pmc_dcp_saved[1], s->pmc_dcp + 4); writel(s->pmc_dcp_saved[0], s->pmc_dcp); @@ -4030,24 +3042,17 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) /* An automatically gated CPU still powered: wait for the PMGR to gate it. */ if (cpu && !regmap_read(g->regmap, off, &val) && (val & PS_AUTO_ENABLE) && - FIELD_GET(PS_ACTUAL, val)) { - t6040_mark_raw(s, T6040_DISP_MARK(i, 1, off)); - t6040_mark_raw(s, T6040_DISP_MARK(i, - t6040_regmap_poll_off(g->regmap, off, &cur) ? 3 : 2, off)); - } + FIELD_GET(PS_ACTUAL, val)) + t6040_regmap_poll_off(g->regmap, off, &cur); if (!cpu && !regmap_read(g->regmap, off, &val) && t6040_sleep_ps_on(val, cpu)) { - t6040_mark_raw(s, T6040_DISP_MARK(i, 0, off)); val &= ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS | PS_DEV_DISABLE | PS_BUSY | PS_RESET_REQ); regmap_write(g->regmap, off, val); if (t6040_regmap_poll_off(g->regmap, off, &cur)) { - t6040_mark_raw(s, T6040_DISP_MARK(i, 4, off)); pr_warn("t6040-sleep: %s +%#x still on: %#x\n", g->name, off, cur); left++; - } else { - t6040_mark_raw(s, T6040_DISP_MARK(i, 8, off)); } } if (off == g->first) @@ -4060,68 +3065,6 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) return left; } -/* Which display groups are on, one bit each (the refusal mark). */ -static u32 t6040_sleep_groups_on(struct t6040_sleep *s) -{ - unsigned int i; - u32 mask = 0; - - for (i = 0; i < s->ngroups; i++) { - const struct t6040_sleep_group *g = &s->groups[i]; - u32 off, val; - - for (off = g->first; off <= g->last; off += PS_STRIDE) - if (regmap_read(g->regmap, off, &val) || - t6040_sleep_ps_on(val, off == g->first)) - mask |= BIT(i); - } - return mask; -} - -/* The first display word on, for the refusal mark: index, flag bits, ACTUAL and TARGET. */ -static u32 t6040_sleep_first_on(struct t6040_sleep *s) -{ - unsigned int i; - - for (i = 0; i < s->ngroups; i++) { - const struct t6040_sleep_group *g = &s->groups[i]; - u32 off, val; - - for (off = g->first; off <= g->last; off += PS_STRIDE) { - if (regmap_read(g->regmap, off, &val) || - !t6040_sleep_ps_on(val, off == g->first)) - continue; - return (((off - g->first) / PS_STRIDE) & 0xf) << 12 | - !!(val & PS_AUTO_ENABLE) << 8 | !!(val & PS_DEV_DISABLE) << 9 | - !!(val & PS_BUSY) << 10 | !!(val & PS_RESET_REQ) << 11 | - (val & (PS_ACTUAL | PS_TARGET)); - } - } - return 0; -} - -/* Which of the 15 axi2af domains read ACTUAL != 0, one bit each. */ -static u32 t6040_axi2af_on_mask(struct t6040_sleep *s) -{ - u32 mask = 0; - int i; - - for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { - void __iomem *ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); - - if (ps && FIELD_GET(PS_ACTUAL, readl(ps))) - mask |= BIT(i); - } - return mask; -} - -/* After the late and noirq device suspend, before the CPUs go offline: a bring-up mark. */ -static int t6040_sleep_prepare_late(void) -{ - t6040_mark(t6040_sleep, 0x03); - return 0; -} - static int t6040_sleep_enter(suspend_state_t state) { struct t6040_sleep *s = t6040_sleep; @@ -4131,24 +3074,13 @@ static int t6040_sleep_enter(suspend_state_t state) if (dry) return t6040_sleep_dry_enter(s, dry); - t6040_mark(s, 0x05); left = t6040_sleep_lower_display(s); - t6040_mark(s, 0x06); if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); /* A dry run 1 ends here as well: its .begin told the firmware nothing. */ if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { unsigned int on = t6040_sleep_log_groups(s); - /* - * The reason, kept through what may follow: 0x5bGGWRVV, GG the groups on, then the - * first word on: W its index in its group (0 the CPU), R bit 0 AUTO_ENABLE, bit 1 - * DEV_DISABLE, bit 2 BUSY, bit 3 RESET, VV its ACTUAL and TARGET. - */ - t6040_mark_raw(s, 0x5b000000 | (t6040_sleep_groups_on(s) & 0xff) << 16 | - t6040_sleep_first_on(s)); - WRITE_ONCE(pm_bringup_dev_mark, NULL); - pr_info("t6040-sleep: at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", num_online_cpus(), on); t6040_disp_back(s); @@ -4157,93 +3089,59 @@ static int t6040_sleep_enter(suspend_state_t state) /* The T8140 driver's order. */ if (t6040_sleep_refuse(s)) { - /* 0x5aGGWWWW: why, as t6040_sleep_refuse() recorded it (GG display groups on). */ - t6040_mark_raw(s, 0x5a000000 | (t6040_refuse_why & 0xffffff)); - WRITE_ONCE(pm_bringup_dev_mark, NULL); t6040_disp_back(s); return -EBUSY; } - t6040_mark(s, 0x10); ret = spin_table_hold_parked(); if (ret) { - t6040_mark_restore(s); t6040_disp_back(s); return ret; } - t6040_mark(s, 0x11); ret = t6040_sleep_coprocs_check(); if (!ret) ret = t6040_sleep_enclave_tell(); if (ret) { spin_table_unhold_parked(); - t6040_mark_restore(s); t6040_disp_back(s); return ret; } - t6040_mark(s, 0x12); s->pcie_ans_stopped = false; s->ans_bridge_quiesced = false; t6040_sleep_coprocs_stop(true); t6040_sleep_coprocs_stop(false); - t6040_mark(s, 0x13); t6040_wake_arm(s); - t6040_mark(s, 0x14); s->cntkctl = read_sysreg(cntkctl_el1); pm_set_resume_via_firmware(); - t6040_mark(s, 0x15); ret = cpu_suspend(0, t6040_sleep_commit); if (!ret) t6040_sleep_return(s); - else - t6040_mark_restore(s); - if (s->alarm_ok) - pr_info("t6040-sleep: marks: PMU comparator was %#x (reply %#x), cpu_suspend %d\n", - s->alarm_saved, s->alarm_hdr, ret); if (s->ans_bridge_quiesced) pr_info("t6040-sleep: ANS's fabric bridge was armed at the commit: quiesced\n"); if (s->returned) - t6040_ret_mark(s, 0x41); + t6040_wake_sync(); t6040_wake_restore(s); if (s->returned) { - t6040_ret_mark(s, 0x42); - t6040_ps_restore(s); + t6040_wake_sync(); + t6040_sio_ps_restore(s); } t6040_disp_back(s); - if (s->returned) - t6040_ret_mark(s, 0x43); + if (s->returned) { + t6040_wake_sync(); + if (s->pcie_ans_on_ret) + pr_warn("t6040-sleep: wake: the storage PCIe raise failed after step %u: %d\n", + s->apcie_wake_step, s->pcie_ans_on_ret); + if (s->gp_clk_timeouts) + pr_warn("t6040-sleep: wake: %u GP PCIe clock or ready poll(s) timed out\n", + s->gp_clk_timeouts); + if (s->aop_ps_timeouts) + pr_warn("t6040-sleep: wake: %u AOP island power word(s) did not come on\n", + s->aop_ps_timeouts); + } return ret; } -/* - * After .enter: 0x44 at the end of syscore_resume (IRQs still off, ours is registered last), - * 0x45 in .wake (IRQs on, the secondary CPUs back, before the noirq device resume). - */ -static void t6040_sleep_syscore_resume(void *data) -{ - struct t6040_sleep *s = t6040_sleep; - - if (s && s->returned && READ_ONCE(pm_bringup_dev_mark)) - t6040_ret_mark(s, 0x44); -} - -static const struct syscore_ops t6040_sleep_syscore_ops = { - .resume = t6040_sleep_syscore_resume, -}; - -static struct syscore t6040_sleep_syscore = { - .ops = &t6040_sleep_syscore_ops, -}; - -static void t6040_sleep_wake(void) -{ - struct t6040_sleep *s = t6040_sleep; - - if (s && s->returned && READ_ONCE(pm_bringup_dev_mark)) - t6040_ret_mark(s, 0x45); -} - static void t6040_sleep_end(void) { struct t6040_sleep *s = t6040_sleep; @@ -4253,14 +3151,6 @@ static void t6040_sleep_end(void) return; t6040_sleep_mem_begun = false; t6040_sleep_clear_wd2(s); - WRITE_ONCE(pm_bringup_dev_mark, NULL); - /* Any return or refusal after .begin: the wake alarm back where the RTC driver set it. */ - if (s->alarm_ok) { - u32 v = s->alarm_saved; - - t6040_pmu_cmd(s, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_WRITE4), &v, NULL, 0); - s->alarm_ok = false; - } if (dry) pr_info("t6040-sleep: dry run %u over, WD2 off\n", dry); WRITE_ONCE(t6040_sleep_dry, 0); @@ -4270,9 +3160,7 @@ static const struct platform_suspend_ops t6040_sleep_ops = { .valid = t6040_sleep_valid, .begin = t6040_sleep_begin, .prepare = t6040_sleep_prepare, - .prepare_late = t6040_sleep_prepare_late, .enter = t6040_sleep_enter, - .wake = t6040_sleep_wake, .end = t6040_sleep_end, }; @@ -4509,70 +3397,6 @@ static const struct dev_pm_ops t6040_sleep_pm_ops = { #endif }; -/* - * Bring-up, root only: write one value to the internal display's CPU (pmgr_disp +0) or front - * end (soc_east +0x368) power word at runtime, to tell a write that is refused from a state - * change that is. "cpu [value]" or "fe [value]", no value: the word's own. Marks 0x567S00RR around the write. - */ -static ssize_t t6040_ps_write_dbg(struct file *f, const char __user *ubuf, size_t len, - loff_t *ppos) -{ - struct t6040_sleep *s = file_inode(f)->i_private; - char buf[48], which[8]; - u32 val = 0, before = 0, after = 0; - unsigned int i; - int n; - - if (len >= sizeof(buf)) - return -EINVAL; - if (copy_from_user(buf, ubuf, len)) - return -EFAULT; - buf[len] = 0; - n = sscanf(buf, "%7s %x", which, &val); - if (n < 1) - return -EINVAL; - if (!strcmp(which, "cpu")) { - for (i = 0; i < s->ngroups; i++) { - const struct t6040_sleep_group *g = &s->groups[i]; - - if (strcmp(g->name, "disp")) - continue; - regmap_read(g->regmap, g->first, &before); - if (n < 2) - val = before; - if (s->spmi0) - t6040_mark_save(s); - t6040_disp_mark(s, 0x70, 0x01); - regmap_write(g->regmap, g->first, val); - udelay(50); - regmap_read(g->regmap, g->first, &after); - t6040_disp_mark(s, 0x70, 0x02); - } - } else if (!strcmp(which, "fe") && s->soc_east) { - before = readl(s->soc_east + PS_DISP_FE); - if (n < 2) - val = before; - if (s->spmi0) - t6040_mark_save(s); - t6040_disp_mark(s, 0x71, 0x01); - writel(val, s->soc_east + PS_DISP_FE); - udelay(50); - after = readl(s->soc_east + PS_DISP_FE); - t6040_disp_mark(s, 0x71, 0x02); - } else { - return -EINVAL; - } - pr_info("t6040-sleep: ps test %s: %#x written over %#x, now %#x\n", which, val, before, after); - return len; -} - -static const struct file_operations t6040_ps_write_fops = { - .owner = THIS_MODULE, - .write = t6040_ps_write_dbg, - .open = simple_open, - .llseek = noop_llseek, -}; - /* The GP PCIe host's port and PHY windows and its ports' PERST# pins, for the commit. */ static void t6040_sleep_map_gp(struct t6040_sleep *s) { @@ -4665,36 +3489,18 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); sleep->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); sleep->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); - if (sleep->spmi0) { - u32 rx[2]; - - if (!t6040_pmu_cmd(sleep, SPMI_CMD_PMU(PMU_RTC_COMPARE, SPMI_OP_READ4), NULL, rx, 2)) - dev_info(&pdev->dev, "PMU comparator at probe: %#x (reply %#x)%s\n", rx[1], rx[0], - (rx[1] & 0xffff0000) == T6040_MARK(0) ? - ": the previous boot's last S2R mark" : ""); - } sleep->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); sleep->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); - sleep->a2d_mini = t6040_ioremap_np(&pdev->dev, 0x508360000ULL, 0x1000); - sleep->a2d_mini4 = t6040_ioremap_np(&pdev->dev, 0x508364000ULL, 0x1000); - sleep->a2d_mm8 = t6040_ioremap_np(&pdev->dev, 0x508908000ULL, 0x1000); - sleep->a2d_pmc = t6040_ioremap_np(&pdev->dev, 0x5048aa000ULL, 0x1000); - sleep->a2d_blk = t6040_ioremap_np(&pdev->dev, 0x502850000ULL, 0x1000); - sleep->aop_req = t6040_ioremap_np(&pdev->dev, 0x5108b8000ULL, 0x1000); - sleep->a2d_mm12 = t6040_ioremap_np(&pdev->dev, 0x50890c000ULL, 0x1000); sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); - sleep->snap_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); - sleep->ps_saved = devm_kcalloc(&pdev->dev, ARRAY_SIZE(t6040_ps_snap_pa), - sizeof(*sleep->ps_saved), GFP_KERNEL); + sleep->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); for (i = 0; i < 16; i++) sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, 0x2000); - sleep->aop_asc = t6040_ioremap_np(&pdev->dev, 0x510600000ULL, 0x100); for (i = 0; i < 4; i++) sleep->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, 0x383200000ULL + i * 0x8000000ULL, 0x100); - sleep->snap_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); + sleep->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); sleep->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); sleep->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); sleep->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); @@ -4717,8 +3523,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); sleep->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); sleep->agx_gate = t6040_ioremap_np(&pdev->dev, T6040_AGX_GATE_PA, 4); - sleep->ane_sys_ps = t6040_ioremap_np(&pdev->dev, T6040_ANE_SYS_PS_PA, 4); - sleep->ane_ps = t6040_ioremap_np(&pdev->dev, T6040_ANE_PS_PA, 0x20); for (i = 0; i < ARRAY_SIZE(t6040_acc_apsc_pa); i++) sleep->acc_apsc[i] = t6040_ioremap_np(&pdev->dev, t6040_acc_apsc_pa[i], 8); for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { @@ -4748,7 +3552,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->debugfs = debugfs_create_dir("t6040-sleep", NULL); debugfs_create_file("preflight", 0400, sleep->debugfs, sleep, &t6040_sleep_preflight_fops); - debugfs_create_file("ps_write", 0200, sleep->debugfs, sleep, &t6040_ps_write_fops); /* * The CPU power ops act only when a commit stops the parked secondaries. @@ -4759,9 +3562,7 @@ static int t6040_sleep_probe(struct platform_device *pdev) if (allow_entry) t6040_sleep_power_ops(sleep); - register_syscore(&t6040_sleep_syscore); suspend_set_ops(&t6040_sleep_ops); - atomic_notifier_chain_register(&panic_notifier_list, &t6040_panic_nb); dev_info(&pdev->dev, "refusal-first: %u display groups, cpu-start %s, wdt %s, commit/wake blocks %s, storage-apcie %s, s2r-mailbox %s, parking ops %s, dry run %u\n", sleep->ngroups, sleep->cpu_start ? "mapped" : "absent", @@ -4772,8 +3573,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->mbox ? "present" : "absent", sleep->power_ops ? "registered" : "not registered", READ_ONCE(dry_run)); t6040_sleep_log_s2r_state(sleep, "probe"); - t6040_log_ane(sleep, "probe"); - t6040_log_a2d(sleep, "probe"); return 0; } @@ -4782,7 +3581,6 @@ static void t6040_sleep_remove(struct platform_device *pdev) { struct t6040_sleep *sleep = platform_get_drvdata(pdev); - unregister_syscore(&t6040_sleep_syscore); debugfs_remove_recursive(sleep->debugfs); } From f78c770268cf5957d2ae22c4c1bcd4036fd87b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:36:15 +0100 Subject: [PATCH 076/269] soc: apple: t6040-sleep: keep a successful sleep and wake quiet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A sleep and wake of the J616s logged some sixty lines from this driver at info level: the blocker list of a suspend that was not refused, the state of every display group, the coprocessor count, each power word raised again after the wake, the axi2af domains that were on or gated, the PMC interfaces put back, the mailbox's resume count, and at probe the S2R state, the keep-alive bits, the GP PCIe windows and the wake stub. Print at info, warn or error only what a user has to see: why a suspend is refused, what a dry run finds, and what went wrong (a display power word that stays on, an SIO power word that does not come back, a wdt block the wake stub cannot restart). The rest goes to debug level. The keep-alive bits are no longer read at probe: they were read for the log alone. The line of a display group that is off and the blocker list of a suspend that goes ahead are dropped or moved to debug as well; a refusal still prints both. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 93 +++++++++++++++------------------ 1 file changed, 42 insertions(+), 51 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 7b11bc89ab382..5fe699c4b72eb 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -815,8 +815,9 @@ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) pr_info("t6040-sleep: %s +%#x = %#x\n", g->name, off, val); } } - pr_info("t6040-sleep: %-8s %s (on mask %#x, cpu first)\n", g->name, - on_mask ? "ON" : "off", on_mask); + if (on_mask) + pr_info("t6040-sleep: %-8s ON (on mask %#x, cpu first)\n", g->name, + on_mask); blockers += !!on_mask; } @@ -897,7 +898,7 @@ static void t6040_ed_map(struct device *dev) k * T6040_ED_CORE_STRIDE, 0x1000); n += !!t6040_ed[c][k]; } - dev_info(dev, "external debug windows mapped for %u cores\n", n); + dev_dbg(dev, "external debug windows mapped for %u cores\n", n); } static void t6040_ed_release(void) @@ -985,7 +986,7 @@ static void t6040_sleep_power_ops(struct t6040_sleep *s) return; if (!spin_table_set_power_ops(&t6040_cpu_power_ops)) { s->power_ops = true; - dev_info(s->dev, "CPU parking power ops registered\n"); + dev_dbg(s->dev, "CPU parking power ops registered\n"); } } @@ -1068,8 +1069,8 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) refused++; } } - pr_info("t6040-sleep: %u of %u registered coprocessors ready for the commit\n", ready, - registered); + pr_debug("t6040-sleep: %u of %u registered coprocessors ready for the commit\n", ready, + registered); if (num_online_cpus() != 1 || !s->mbox || (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { @@ -1138,7 +1139,7 @@ static int t6040_sleep_coprocs_check(void) pr_err("t6040-sleep: refused: %s: %d\n", dev_name(c->dev), ret); return ret; } - pr_info("t6040-sleep: last check of %s passed\n", dev_name(c->dev)); + pr_debug("t6040-sleep: last check of %s passed\n", dev_name(c->dev)); } return 0; @@ -1264,12 +1265,12 @@ static void t6040_sio_ps_restore(struct t6040_sleep *s) if (n == 1000) failed++; raised++; - pr_info("t6040-sleep: wake: SOC-East power word +%#x: %#x at the commit, %#x after the wake, now %#x\n", - off, saved, val, cur); + pr_debug("t6040-sleep: wake: SOC-East power word +%#x: %#x at the commit, %#x after the wake, now %#x\n", + off, saved, val, cur); } - if (raised) - pr_info("t6040-sleep: wake: %u power word(s) raised again, %u not on\n", raised, - failed); + if (failed) + pr_warn("t6040-sleep: wake: %u of %u SIO power word(s) raised again did not come on\n", + failed, raised); } /* As on T8140, measured on ANS: status and control read, then the run bit cleared twice. */ @@ -1328,7 +1329,7 @@ static const u32 t6040_wake_mask[AOP_WAKE_WORDS] = { * regmap 1 + 0x70970 shifted right by 3; the platform reads * 0x508370970 at quiesce. It tells "the SoC entered S2R" from "the * engine never finished". Read-only, with the AOP-global S2R control, wake masks and - * wake status, logged at probe so the boot after a failed sleep shows them. + * wake status, at debug level: at probe, for the boot after a failed sleep, and in a dry run. */ #define T6040_S2R_LO_COUNT_PA 0x508370970ULL @@ -1347,12 +1348,12 @@ static void t6040_sleep_log_s2r_state(struct t6040_sleep *s, const char *when) st[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); } } - dev_info(s->dev, "S2R state at %s: S2R-lo count %u (raw %#x), AOP-global ctl %#x\n", - when, raw >> 3, raw, ctl); - dev_info(s->dev, "S2R state at %s: wake masks %*ph\n", when, - (int)sizeof(m), m); - dev_info(s->dev, "S2R state at %s: wake status %*ph\n", when, - (int)sizeof(st), st); + dev_dbg(s->dev, "S2R state at %s: S2R-lo count %u (raw %#x), AOP-global ctl %#x\n", + when, raw >> 3, raw, ctl); + dev_dbg(s->dev, "S2R state at %s: wake masks %*ph\n", when, + (int)sizeof(m), m); + dev_dbg(s->dev, "S2R state at %s: wake status %*ph\n", when, + (int)sizeof(st), st); } /* @@ -1615,7 +1616,7 @@ static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s) /* * Read-only axi2af gate: on entry the platform panics unless every reachable domain's - * power word reads [7:4]==0. We only log the ones still on, as a safety check. + * power word reads [7:4]==0. Here the ones still on are only named, at debug level. */ static void t6040_axi2af_readcheck(struct t6040_sleep *s) { @@ -1629,13 +1630,13 @@ static void t6040_axi2af_readcheck(struct t6040_sleep *s) continue; val = readl(ps); if (FIELD_GET(PS_ACTUAL, val)) { - pr_info("t6040-sleep: axi2af %s still on (%#x) at commit\n", - t6040_axi2af[i].name, val); + pr_debug("t6040-sleep: axi2af %s still on (%#x) at commit\n", + t6040_axi2af[i].name, val); on++; } } if (on) - pr_info("t6040-sleep: %d axi2af domain(s) still powered at commit\n", on); + pr_debug("t6040-sleep: %d axi2af domain(s) still powered at commit\n", on); } /* @@ -1660,8 +1661,8 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) if (FIELD_GET(PS_TARGET, val) != 0xf) continue; if (!FIELD_GET(PS_ACTUAL, val)) { - pr_info("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", - t6040_axi2af[i].name, val); + pr_debug("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", + t6040_axi2af[i].name, val); continue; } t6040_wake_sync(); @@ -2584,7 +2585,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) if (s->mbox) { dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); - pr_info("t6040-sleep: wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); + pr_debug("t6040-sleep: wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); } if (s->aop_global) for (i = 0; i < AOP_WAKE_WORDS; i++) @@ -2676,23 +2677,22 @@ static int t6040_sleep_begin(suspend_state_t state) if (dcs_to) pr_warn("t6040-sleep: DCS power words %#x did not read on before automatic mode\n", dcs_to); - pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", - live, disp); - t6040_sleep_log_blockers(s); - if (!begin_total && allow_entry && !dry) { /* The non-display blockers are clear and no dry run is asked for: proceed * to device suspend; .enter holds the real commit until the display is * also off. The AON WD2 is the net from here. */ - if (disp) - pr_warn("t6040-sleep: entering: %u display pipe(s) still on, relying on device suspend to lower them before the commit\n", - disp); + pr_debug("t6040-sleep: entering: %u display pipe(s) on, to be lowered before the commit\n", + disp); t6040_sleep_power_ops(s); t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); pm_set_suspend_via_firmware(); return 0; } + pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", + live, disp); + t6040_sleep_log_blockers(s); + if (dry >= 2) { dry = min(dry, 3U); t6040_sleep_power_ops(s); @@ -3023,8 +3023,8 @@ static void t6040_disp_back(struct t6040_sleep *s) if (boost != s->pmc_if_saved[1]) writel(boost | s->pmc_if_saved[1], s->pmc_bwr + PMC_BOOST); if (bwr != s->pmc_if_saved[0] || boost != s->pmc_if_saved[1]) - pr_info("t6040-sleep: wake: PMC display interfaces BWR %#x boost %#x, put back to %#x %#x\n", - bwr, boost, readl(s->pmc_bwr), readl(s->pmc_bwr + PMC_BOOST)); + pr_debug("t6040-sleep: wake: PMC display interfaces BWR %#x boost %#x, put back to %#x %#x\n", + bwr, boost, readl(s->pmc_bwr), readl(s->pmc_bwr + PMC_BOOST)); } } @@ -3117,7 +3117,7 @@ static int t6040_sleep_enter(suspend_state_t state) if (!ret) t6040_sleep_return(s); if (s->ans_bridge_quiesced) - pr_info("t6040-sleep: ANS's fabric bridge was armed at the commit: quiesced\n"); + pr_debug("t6040-sleep: ANS's fabric bridge was armed at the commit: quiesced\n"); if (s->returned) t6040_wake_sync(); @@ -3444,9 +3444,9 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) s->gp_pins = t6040_ioremap_np(s->dev, res.start, resource_size(&res)); of_node_put(pins); } - dev_info(s->dev, "GP PCIe: ports %d/%d phys %d/%d, PERST# pins %d/%d\n", - !!s->gp_port[0], !!s->gp_port[1], !!s->gp_phy[0], !!s->gp_phy[1], - s->gp_perst[0], s->gp_perst[1]); + dev_dbg(s->dev, "GP PCIe: ports %d/%d phys %d/%d, PERST# pins %d/%d\n", + !!s->gp_port[0], !!s->gp_port[1], !!s->gp_phy[0], !!s->gp_phy[1], + s->gp_perst[0], s->gp_perst[1]); out: of_node_put(host); } @@ -3482,8 +3482,9 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->wdt_stub_ok = widx >= 0 && !of_address_to_resource(pdev->dev.of_node, widx, &wres) && wres.start == T6040_WDT_PA; - dev_info(&pdev->dev, "wake stub (WD2 60 s before cpu_resume): %s\n", - sleep->wdt_stub_ok ? "ready" : "no, wdt block is not the AON WDT"); + if (!sleep->wdt_stub_ok) + dev_warn(&pdev->dev, + "the wdt block is not the AON watchdog: no WD2 restart at the wake\n"); } sleep->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); sleep->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); @@ -3530,16 +3531,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->tvm_status[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].status, 4); } sleep->afr_ps = t6040_ioremap_np(&pdev->dev, T6040_AFR_PS_PA, 4); - if (sleep->ka_soc && sleep->ka_east) { - u32 soc = 0, east = 0; - - for (i = 0; i < KA_WORDS; i++) { - soc |= (readl(sleep->ka_soc + i * KA_STRIDE) & 1) << i; - east |= (readl(sleep->ka_east + i * KA_STRIDE) & 1) << i; - } - dev_info(&pdev->dev, "keep-alive bits at probe: SOC %#010x, SOC-East %#010x\n", - soc, east); - } t6040_sleep_map_gp(sleep); t6040_ed_map(&pdev->dev); From 9040c20db0bd8ec8fb52c61aa812028e411d171d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:37:26 +0100 Subject: [PATCH 077/269] soc: apple: t6040-sleep: describe the driver as it is MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the parameters and the instrumentation gone, several comments still described them: the PMC votes and the PTD request of the display pipes, which the driver only reads or restores, the power word helpers of the T8140 order, and stages named after the bring-up steps that found them. Say what the code does: the file header names the commit and the wake return and the four parameters that are left, sleep_wd_secs gives its real default of 30 s, the comments on the kept steps name the hardware and what was measured on the J616s, and the comment on the display power words stands at its function again. A second definition of PS_BUSY goes. The Kconfig help says what a sleep with allow_entry does. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 3 ++ drivers/soc/apple/t6040-sleep.c | 74 +++++++++++++++------------------ 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index a92a4b2c00235..2c5f58eb4359d 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -141,6 +141,9 @@ config APPLE_T6040_SLEEP CPUs that can park, the display power states the platform checks before it sleeps). Unless t6040_sleep.allow_entry=1 is given and no blocker remains, a suspend request prints the blockers and enters nothing. + With it the SoC is powered down to suspend-to-RAM and comes back + through the boot object; the AON watchdog is the net over the + whole cycle (t6040_sleep.sleep_wd_secs at the power-down itself). It keeps the coprocessor and enclave registry for the T6040, so the drivers that leave their last stop to the sleep's commit (ANS, diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 5fe699c4b72eb..fce51e7a93a0c 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -3,8 +3,10 @@ * Apple T6040 (M4 Pro) system sleep: refusal-first controller. * * This is the T6040 port of the T8140 deep sleep at EL2 (sleep.c). Its commit - * is the T6040 entry as measured on the J616s, which is much - * shorter than the T8140's (see t6040_sleep_commit). + * EL2). Its commit follows the T6040 entry as measured on the J616s, + * with what the SoC's sleep engine needs from an OS that is not the platform (see + * t6040_sleep_commit); its wake return mirrors the PMGR wake restore (see + * t6040_sleep_return). * * What it does: * @@ -31,7 +33,9 @@ * The AON watchdog's WD2 is armed from .begin to .end of a dry run 2 or 3 * (t6040_sleep.wd2_secs, 0 to leave it off). * - The real commit (t6040_sleep_commit) runs only when no blocker remains, - * t6040_sleep.allow_entry=1 is given and no dry run is asked for. + * t6040_sleep.allow_entry=1 is given and no dry run is asked for. The AON + * watchdog's WD2 is its net (t6040_sleep.sleep_wd_secs), and over the device + * suspend and resume around it. * * Why this file and not a generalisation of sleep.c: the port keeps * the T8140 behaviour byte-identical, and the per-SoC match data should be @@ -107,7 +111,7 @@ #define PS_APCIE_SYS_ST0 0x380 /* SOC-East: ps_apcie_sys_st0 */ #define PS_APCIE_SYS_ST1 0x388 /* SOC-East: ps_apcie_sys_st1 */ #define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ -#define PS_BUSY BIT(11) /* polled clear by the A2D engine after each request */ +#define PS_BUSY BIT(11) /* a request is in progress */ #define APCIE_AXI_EN0 0x104 #define APCIE_AXI_EN1 0x108 @@ -245,7 +249,7 @@ * poll BIT7 clear (CPM gating after it reads back already set: not written); * - features off: cpu-fixed-freq-pll-relock, BIT42 := 0 in the E and P0 * ACC words; then the TVMs disp2, afr, dcs (two), gfx (two), disp, soc: control - * BIT0 := 0, status polled until BIT1 clears; + * BIT0 := 0, status polled until BIT1 clears. * The platform then drops the AP voter's DCS floor vote in the PMC. That step is left out: the * first access to that word hung the bus on the J616s. * The platform polls the APSC and TVM words without a timeout; here every poll is bounded. @@ -273,21 +277,16 @@ static const struct { #define TVM_BUSY BIT(1) /* - * As the platform does (measured on the J616s where marked), a display pipe's PMC votes go - * before any of its power words: boost 0x5048ac028 BIT(p), DVMR 0x5048a4000 BIT(p) with - * the ack polled at +0x20 BIT0 and 0x5048a9c14 := popcount(DVMR) (measured), BWR 0x5048ac020 - * BIT(p ? p + 2 : 0); the cluster rail 0x5048a9c18 BIT2 (DISP_INT) or BIT3 (DISP_EXT, last - * pipe only) with the ack at 0x5048a9c1c (measured). p: 0 internal, 1 DCPEXT0, 2 DCPEXT1. + * The PMC's bandwidth-request and boost interface enables of the display pipes + * (0x5048ac020 and +0x8), one bit per pipe. */ #define T6040_PMC_BWR_PA 0x5048ac020ULL #define PMC_BOOST 0x8 /* * The PMP's PTD (pmp-report 0x5003c0000, T6041 layout as drivers/pmdomain/apple/ - * pmp-report.c): SOC-DEV-PS-REQ read +0x1e00, written +0x10f00, SOC-DEV-PS-ACK +0x1e80, - * 64-bit. DCPEXT0/1 are requests 17/18 (DT report@11/@12, withdrawn by that driver at - * suspend_noirq); the internal display is 16, assumed from the pattern. The platform has - * the PMP release a display's request before its power words change. + * pmp-report.c): SOC-DEV-PS-REQ read at +0x1e00, SOC-DEV-PS-ACK at +0x1e80, 64-bit. + * Read only, for the preflight. */ #define T6040_PTD_PA 0x5003c0000ULL #define PTD_REQ_READ 0x1e00 @@ -570,12 +569,12 @@ MODULE_PARM_DESC(allow_entry, /* * The AON WD2 net armed at the commit. The platform arms 5 s for its reconfig; a longer * net tells "the SoC never slept" (reset at the net) from "it slept and the wake - * failed" (reset at the wake source) on a first bring-up. Never off: 1 to 178. + * failed" (reset at the wake source). Never off: 1 to 178. */ static unsigned int sleep_wd_secs = 30; module_param(sleep_wd_secs, uint, 0644); MODULE_PARM_DESC(sleep_wd_secs, - "AON WD2 net armed at the S2R commit, in seconds (default 5, clamped to 1..178)"); + "AON WD2 net armed at the S2R commit, in seconds (default 30, clamped to 1..178)"); static unsigned int wd2_secs = WDT_SUSPEND_SECS; module_param(wd2_secs, uint, 0644); @@ -1699,13 +1698,10 @@ static void t6040_wake_restore(struct t6040_sleep *s) } /* - * ANS's storage-apcie power words (the two ST lanes + ANS) live in SOC-East. - * These helpers drive one power word in a given bank, as T8140's t8140_ps_* - * do in its single PMGR bank: lower is AUTO_ENABLE off then target 0; raise is - * the target, then AUTO_ENABLE once ACTUAL reads on. Writing PS_FLAGS clears - * them. Separate from t6040_sleep_ps_on(), which reads a cached value. + * One power word raised, as T8140's t8140_ps_raise() does: target 0xf with automatic + * mode off and the flag bits cleared, ACTUAL polled, then AUTO_ENABLE. The two halves + * are separate for a word that has to stay at a manual target for a while or for good. */ - static int t6040_ps_raise_start(void __iomem *base, u32 offset) { void __iomem *reg = base + offset; @@ -1978,10 +1974,10 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) /* * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The platform clears the bit before a * retained domain goes off and sets it again once it is back up; here the wake sets it - * (t6040_sleep_raise_apcie_st()) and the A2D engine, not this driver, takes ANS down. iBoot - * leaves the bit clear, so a boot's first sleep goes down with it clear; without this the - * next one meets the bridge as the first wake armed it. Only while ANS is on: the bridge sits - * in it. Returns whether the bit was set. + * (t6040_sleep_raise_apcie_st()) and the SoC's sleep engine, not this driver, takes ANS down. + * The boot loader leaves the bit clear, so a boot's first sleep goes down with it clear; + * without this the next one meets the bridge as the first wake armed it. Only while ANS is + * on: the bridge sits in it. Returns whether the bit was set. */ static bool t6040_ans_bridge_quiesce(struct t6040_sleep *s) { @@ -2186,7 +2182,7 @@ extern char t6040_resume_stub[]; * Their common_dp, usb, phymxwrap and usb_aon words are MiniPMGR words (0x508300000 + 0xa0.., * ADT ATCn_COMMON_DP/USB/PHYMXWRAP/USB_AON), not SOC ones: applied to soc_ps, * those offsets lower SOC AIC (0x118), PMC (0x120), PMS_FPWM0-4 and - * FAB0_SOC (0x168) right before the S2R request; the dead PMC then blocks the A2D engine's + * FAB0_SOC (0x168) right before the S2R request; the dead PMC then blocks the sleep engine's * PMC writes (an AP read of the PMC hangs from that step on). */ static const u16 t6040_atc_words[4][5] = { @@ -2745,7 +2741,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) t6040_sleep_log_s2r_state(s, "dry-run commit point"); /* * A wake source pending and left unmasked by the masks a real commit arms would keep the - * AP partition from powering down (A2D then stalls at MiniPMGR +0x40). Read only. + * AP partition from powering down, and the sleep engine then stalls. Read only. */ if (s->aop_global) { u32 p[AOP_WAKE_WORDS]; @@ -2779,17 +2775,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) return -EBUSY; } -/* - * The display power words the platform has off before its sleep (apple,check-ps-b4-sleep), lowered - * here: after the device suspend, interrupts off, one CPU up, so no driver, interrupt handler - * or DART touches the pipes any more. The DCPs only stop their firmware: lowering the domains - * from their suspend panicked on T6040. Each group from its last sub-block to its pipe CPU, - * each word as the pmgr-pwrstate driver powers a domain off (TARGET 0, the one-shot bits - * clear), polled to ACTUAL 0. Returns the number of words left on. - */ - #define PS_DEV_DISABLE BIT(10) -#define PS_BUSY BIT(11) #define PS_RESET_REQ BIT(12) #define DISP_CPU_GATE_US 50000 @@ -2918,8 +2904,7 @@ static int t6040_dcp_dart_idle(struct t6040_sleep *s) return 0; } -/* The internal display's three domains, child first, after its sub-blocks (the order). */ - +/* The internal display's FE and SYS domains, child first, after its sub-blocks (the order). */ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group *g) { struct t6040_psw cpu = { .map = g->regmap, .off = g->first }; @@ -3028,6 +3013,15 @@ static void t6040_disp_back(struct t6040_sleep *s) } } +/* + * The display power words the platform has off before its sleep (apple,check-ps-b4-sleep), lowered + * here: after the device suspend, interrupts off, one CPU up, so no driver, interrupt handler + * or DART touches the pipes any more. The DCPs only stop their firmware: lowering the domains + * from their suspend panicked on T6040. Each group from its last sub-block up to, and not + * including, its pipe CPU, which the PMGR gates by itself: each word as the pmgr-pwrstate + * driver powers a domain off (TARGET 0, the one-shot bits clear), polled to ACTUAL 0. The + * internal display's FE and SYS domains follow. Returns the number of words left on. + */ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) { unsigned int i, left = 0; From 5b25b86cd38eeb8af5411cc1be93da301d982dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:23:15 +0100 Subject: [PATCH 078/269] pinctrl: apple: put the pins back after S2R without switches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two module parameters guarded what the driver puts back after a firmware-driven suspend: resume_cached let a block with a register cache take part, and resume_all let a block marked apple,resume-restore-all get every pin and its global word back. Both were on, and the J616s wakes with its keyboard, trackpad and panel only that way. The driver now always does both. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 0589195dfea3a..a9d1c05ac4835 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -101,19 +101,6 @@ struct apple_gpio_pinctrl { */ #define REG_GLOBAL 0xc48 -/* - * T6040 bring-up: put the configured pins back after a firmware-driven - * suspend also on a cached block, by reading and writing the hardware past - * the cache. Off restores only the uncached blocks, as before. - */ -static bool resume_cached = true; -module_param(resume_cached, bool, 0644); -MODULE_PARM_DESC(resume_cached, "Put configured pins back after S2R on a cached block too (bring-up)"); - -static bool resume_all = true; -module_param(resume_all, bool, 0644); -MODULE_PARM_DESC(resume_all, "After S2R put back every pin of a block marked apple,resume-restore-all, as the platform (bring-up)"); - static const struct regmap_config regmap_config = { .reg_bits = 32, .val_bits = 32, @@ -756,8 +743,6 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) if (!pctl->saved || pm_suspend_target_state != PM_SUSPEND_MEM || !pm_suspend_via_firmware()) return 0; - if (pctl->cached && !READ_ONCE(resume_cached)) - return 0; bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); /* @@ -766,7 +751,7 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) * back. The pins of the panel's TCON bus, set up by the boot loader, are among them. */ pctl->saved_global_valid = false; - if (pctl->restore_all && READ_ONCE(resume_all)) { + if (pctl->restore_all) { pctl->saved_global = readl(pctl->base + REG_GLOBAL); pctl->saved_global_valid = true; if (pctl->cached) From 8d8b47f257ba917c4da81850b515abf781a8240c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:40:03 +0100 Subject: [PATCH 079/269] soc: apple: sleep: drop the progress mark hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nothing calls apple_sleep_bringup_mark() any more and the T6040 controller keeps no marks: remove its empty definition and the declaration. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 6 ------ include/linux/soc/apple/sleep.h | 6 ------ 2 files changed, 12 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index fce51e7a93a0c..af4212e32a8ae 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -1408,12 +1408,6 @@ static void t6040_pmu_write(struct t6040_sleep *s, u16 reg, u8 val) writel(enable, s->spmi0 + SPMI_NOTIFY_ENABLE); } -/* A no-op: this controller keeps no progress marks. */ -void apple_sleep_bringup_mark(u32 v) -{ -} -EXPORT_SYMBOL_GPL(apple_sleep_bringup_mark); - /* * Between the steps of the wake return: every access of a step is complete, and a fault it * raised is taken, before the next step starts. diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 30352f1cad0c8..cc89213189b9c 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -342,13 +342,7 @@ static inline int apple_sleep_engine_reset(struct device *dev) #if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) bool apple_sleep_power_loss(void); bool apple_sleep_last_steps(void); -/* S2R bring-up: a progress mark that survives a reset (t6040-sleep, between .begin and .end). */ -void apple_sleep_bringup_mark(u32 v); #else -static inline void apple_sleep_bringup_mark(u32 v) -{ -} - static inline bool apple_sleep_power_loss(void) { return pm_suspend_via_firmware(); From 84b6ebdd585124ea885ba46f54a133e6502d7128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 15:22:07 +0100 Subject: [PATCH 080/269] soc: apple: t6040-sleep: drop the dry run that never reached the commit point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dry run 1 suspended the devices without marking the suspend as a firmware-driven one. The PMU RTC refuses such a deep sleep in its .prepare, as it has no way to bring an alarm back from it, so the run ended there with -EBUSY and never parked a CPU. The other two levels cover everything it was meant to show: both park the CPUs and hold them at the commit point. Remove it and number the remaining levels 1 and 2. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 2 +- drivers/soc/apple/t6040-sleep.c | 36 +++++++++++++-------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 2c5f58eb4359d..5755a83d02fed 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -148,7 +148,7 @@ config APPLE_T6040_SLEEP It keeps the coprocessor and enclave registry for the T6040, so the drivers that leave their last stop to the sleep's commit (ANS, the PMP, the SEP) can register, and offers dry runs: with - t6040_sleep.dry_run=2 every driver's firmware-driven sleep step that + t6040_sleep.dry_run=1 every driver's firmware-driven sleep step that can be taken back runs, the CPUs park and are held, and the sleep is refused at the commit point and resumed warm. diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index af4212e32a8ae..56e21d3fe831e 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -21,16 +21,15 @@ * `echo mem > /sys/power/state` prints every blocker and returns an error * before any device suspends. * - Dry runs (t6040_sleep.dry_run, also writable at runtime): - * 1: the ordinary device suspend and CPU parking, the firmware never told. - * 2: pm_suspend_via_firmware() is set, so every driver's firmware-driven + * 1: pm_suspend_via_firmware() is set, so every driver's firmware-driven * sleep step that can be taken back runs (apple_sleep_power_loss() is * false, so what only a power cycle restores is left out), the CPUs * park and answer the commit's hold, the commit point's checks run, * and the sleep is refused there and resumed warm. - * 3: 2, plus the last steps that can still be taken back (the SMC's sleep + * 2: 1, plus the last steps that can still be taken back (the SMC's sleep * commit, each coprocessor's last check, such as ANS's check of the * shutdown notification it sent at its suspend). - * The AON watchdog's WD2 is armed from .begin to .end of a dry run 2 or 3 + * The AON watchdog's WD2 is armed from .begin to .end of a dry run * (t6040_sleep.wd2_secs, 0 to leave it off). * - The real commit (t6040_sleep_commit) runs only when no blocker remains, * t6040_sleep.allow_entry=1 is given and no dry run is asked for. The AON @@ -551,15 +550,14 @@ struct t6040_sleep { }; /* - * 0: refuse at .begin. 1: device suspend and parking, the firmware never told. - * 2: the firmware-driven device suspend short of what only a power cycle - * restores, parking and the commit's hold, refused at the commit point. 3: 2 and + * 0: no dry run. 1: the firmware-driven device suspend short of what only a power + * cycle restores, parking and the commit's hold, refused at the commit point. 2: 1 and * the last steps that can still be taken back. Read at each suspend's .begin. */ static unsigned int dry_run; module_param(dry_run, uint, 0644); MODULE_PARM_DESC(dry_run, - "Dry run level: 1 parking only, 2 firmware-driven device sleep refused at the commit point, 3 also the last steps"); + "Dry run level: 1 firmware-driven device sleep refused at the commit point, 2 also the last steps"); static bool allow_entry; module_param(allow_entry, bool, 0444); @@ -579,7 +577,7 @@ MODULE_PARM_DESC(sleep_wd_secs, static unsigned int wd2_secs = WDT_SUSPEND_SECS; module_param(wd2_secs, uint, 0644); MODULE_PARM_DESC(wd2_secs, - "Arm the AON WD2 for this many seconds from .begin to .end of a dry run 2 or 3 (0: off, at most 178)"); + "Arm the AON WD2 for this many seconds from .begin to .end of a dry run (0: off, at most 178)"); static struct t6040_sleep *t6040_sleep; @@ -614,7 +612,7 @@ bool apple_sleep_last_steps(void) { unsigned int dry = READ_ONCE(t6040_sleep_dry); - return pm_suspend_via_firmware() && (!dry || dry >= 3); + return pm_suspend_via_firmware() && (!dry || dry >= 2); } EXPORT_SYMBOL_GPL(apple_sleep_last_steps); @@ -2683,8 +2681,8 @@ static int t6040_sleep_begin(suspend_state_t state) live, disp); t6040_sleep_log_blockers(s); - if (dry >= 2) { - dry = min(dry, 3U); + if (dry) { + dry = min(dry, 2U); t6040_sleep_power_ops(s); if (!s->power_ops) pr_warn("t6040-sleep: dry run: no CPU parking power ops, so no hold at the commit point\n"); @@ -2696,16 +2694,11 @@ static int t6040_sleep_begin(suspend_state_t state) else secs = 0; pr_info("t6040-sleep: dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", - dry, dry >= 3 ? "the stops only a power cycle undoes" : + dry, dry >= 2 ? "the stops only a power cycle undoes" : "the last steps", secs); return 0; } - if (dry) { - pr_info("t6040-sleep: dry run 1: parking CPUs and refusing at the commit point; the firmware is never told\n"); - return 0; - } - pr_info("t6040-sleep: refusing the suspend; nothing entered\n"); return -EOPNOTSUPP; } @@ -2719,8 +2712,8 @@ static int t6040_sleep_prepare(void) } /* - * A dry run 2 or 3 at the commit point, interrupts off and one CPU up: every - * check a real commit makes, the parked CPUs held and let go, at level 3 each + * A dry run at the commit point, interrupts off and one CPU up: every + * check a real commit makes, the parked CPUs held and let go, at level 2 each * coprocessor's last check, then the refusal. Never reached: the enclave's * sleep request, each coprocessor's last stop, the wake masks, the SoC * power-down. @@ -2757,7 +2750,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) pr_info("t6040-sleep: %u parked CPU(s) answered the hold\n", num_present_cpus() - num_online_cpus()); - if (dry >= 3 && !ret) + if (dry >= 2 && !ret) pr_info("t6040-sleep: the coprocessors' last checks: %d\n", t6040_sleep_coprocs_check()); if (!ret) @@ -3065,7 +3058,6 @@ static int t6040_sleep_enter(suspend_state_t state) left = t6040_sleep_lower_display(s); if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); - /* A dry run 1 ends here as well: its .begin told the firmware nothing. */ if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { unsigned int on = t6040_sleep_log_groups(s); From 4d8cea75b27464bf6390462f9febff5afe4b3999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 15:22:20 +0100 Subject: [PATCH 081/269] soc: apple: t6040-sleep: two comments that still pointed away from the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One named the T8140 driver's routine by a pull request number, the other said the driver waits for more data on the T6040 commit and wake. Both are known and in this file; say why it is a controller of its own instead. Signed-off-by: František Balázsy --- drivers/soc/apple/t6040-sleep.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/t6040-sleep.c index 56e21d3fe831e..5da1f6e31e8fd 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/t6040-sleep.c @@ -37,9 +37,9 @@ * suspend and resume around it. * * Why this file and not a generalisation of sleep.c: the port keeps - * the T8140 behaviour byte-identical, and the per-SoC match data should be - * built once the T6040 commit and wake are known from a trace. Until then it is - * a controller of its own. The coprocessor and enclave registry is shared + * the T8140 behaviour byte-identical, and the T6040 commit and wake differ + * from it in most steps, so it is a controller of its own. The coprocessor and + * enclave registry is shared * (sleep-registry.c), so one kernel carries both controllers, and each binds * only to its own SoC. */ @@ -2022,7 +2022,7 @@ static void t6040_bridge_rearm(void __iomem *bridge) /* * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of - * #1208's t8140_sleep_raise_apcie(). Runs atomic on the waking core in + * T8140's t8140_sleep_raise_apcie(). Runs atomic on the waking core in * t6040_sleep_return(), AFTER t6040_axi2af_rearm() and BEFORE control returns to * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. * Both lanes come up; the PLL/clkgen/PHY tables run once (shared). The two From e42c8b3b333bb4bc10e0b51eeb62eedded2ecfa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:47:24 +0100 Subject: [PATCH 082/269] soc: apple: sleep: let a SoC bring its own sequences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The driver takes what differs between SoCs from lists: power words, the order that was measured, wake sources. A SoC whose sleep entry and wake return differ in more than that has no place in it. Add one pointer to the match data, a part with its own probe and an optional restore from a hibernation image. Where it is set the driver's probe hands over to it before any of its own conditions, and the driver's new restore callback calls the part's. A part also carries its own two answers for the coprocessor and enclave registry, and the driver binds them around the part's probe, as it binds its own for a SoC that it sleeps from its lists. No SoC sets a part yet, and the T8140 sleep is unchanged: its probe only gains the test, and its restore returns 0. The boot object's mailbox layout moves to the private header that such a part shares. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 23 +++++++++++++++++ drivers/soc/apple/sleep.c | 40 ++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 233a421d8d438..5b76df52e0136 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -12,6 +12,7 @@ #include struct device; +struct platform_device; /* The coprocessors the last mile stops. Each driver idles its own first. */ #define APPLE_SLEEP_COPROCS 6 @@ -61,4 +62,26 @@ struct apple_sleep_ctl { int apple_sleep_registry_bind(const struct apple_sleep_ctl *ctl); void apple_sleep_registry_unbind(const struct apple_sleep_ctl *ctl); +/* Must match aura's struct aura_s2r_mailbox. */ +#define AURA_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ + +struct aura_s2r_mailbox { + u64 magic; /* aura, before the kernel starts */ + u64 boot_mpidr; /* aura: the only core that may take resume_pa */ + u64 resume_pa; /* kernel, armed before a commit; aura clears it */ + u64 resumes; /* aura: resume_pa taken this many times */ +}; + +/* + * A SoC whose sleep doesn't fit the lists of struct apple_sleep_soc brings its own sequences. + * @probe then runs in place of the driver's own, and installs the part's suspend operations. + * @restore, if set, runs when the system comes back from a hibernation image. + * @registry: the part's answers to the registry, which the driver binds around @probe. + */ +struct apple_sleep_part { + int (*probe)(struct platform_device *pdev); + int (*restore)(struct device *dev); + struct apple_sleep_ctl registry; +}; + #endif diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 6d0c0e6ae9d9b..fb535cf84c8f6 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -80,16 +80,6 @@ #include "sleep-internal.h" -/* Must match aura's struct aura_s2r_mailbox. */ -#define AURA_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ - -struct aura_s2r_mailbox { - u64 magic; /* aura, before the kernel starts */ - u64 boot_mpidr; /* aura: the only core that may take resume_pa */ - u64 resume_pa; /* kernel, armed before a commit; aura clears it */ - u64 resumes; /* aura: resume_pa taken this many times */ -}; - /* PS word */ #define PS_AUTO_ENABLE BIT(28) #define PS_UNLATCH BIT(12) @@ -505,6 +495,8 @@ struct apple_sleep_soc { /* The PMC whose cluster vote for the encoders needs the complex up, and its bit */ const char *ave_pmc; u32 ave_pmc_cluster; + /* The SoC's own sequences, in place of the lists above, or NULL */ + const struct apple_sleep_part *part; }; struct apple_sleep { @@ -3930,9 +3922,22 @@ static int apple_sleep_alloc_books(struct apple_sleep *s) static int apple_sleep_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; + const struct apple_sleep_soc *soc = of_device_get_match_data(dev); struct apple_sleep *s; int ret; + /* A SoC with its own sequences also has its own conditions, and its own answers. */ + if (soc && soc->part) { + ret = apple_sleep_registry_bind(&soc->part->registry); + if (ret) + return dev_err_probe(dev, ret, "another sleep controller is bound\n"); + ret = soc->part->probe(pdev); + if (ret) + apple_sleep_registry_unbind(&soc->part->registry); + + return ret; + } + /* Below EL2 a monitor owns all of this. */ if (!is_kernel_in_hyp_mode()) return -ENODEV; @@ -4435,10 +4440,25 @@ static const struct of_device_id apple_sleep_of_match[] = { { } }; +static int apple_sleep_restore(struct device *dev) +{ + const struct apple_sleep_soc *soc = of_device_get_match_data(dev); + + if (soc && soc->part && soc->part->restore) + return soc->part->restore(dev); + + return 0; +} + +static const struct dev_pm_ops apple_sleep_pm_ops = { + .restore = pm_sleep_ptr(apple_sleep_restore), +}; + static struct platform_driver apple_sleep_driver = { .driver = { .name = "apple-sleep", .of_match_table = apple_sleep_of_match, + .pm = pm_sleep_ptr(&apple_sleep_pm_ops), .suppress_bind_attrs = true, }, .probe = apple_sleep_probe, From 005a94f3f82f1988bc67facd784bbff8d47166ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 15:35:21 +0100 Subject: [PATCH 083/269] soc: apple: sleep: open the registry to a SoC's part MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A SoC's part keeps its own state and suspend operations, and the registry of coprocessors and of the enclave has to serve it as it serves the driver's own sequences. The last check, the enclave's stop and the two stop passes take the device to log for and where to note a ready ANS, instead of the driver's state. The check that every registered coprocessor is ready moves out of the refusal into a function of its own, and the registry can list itself for a debugfs file. The T8140 sequences are unchanged: the same calls in the same order with the same log lines. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 20 +++++++ drivers/soc/apple/sleep.c | 90 +++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 5b76df52e0136..1f1ca7e6cbdbd 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -13,6 +13,7 @@ struct device; struct platform_device; +struct seq_file; /* The coprocessors the last mile stops. Each driver idles its own first. */ #define APPLE_SLEEP_COPROCS 6 @@ -84,4 +85,23 @@ struct apple_sleep_part { struct apple_sleep_ctl registry; }; +/* For a debugfs file: who is registered, and how far each is. */ +void apple_sleep_registry_show(struct seq_file *s); + +/* + * The rest is for the commit point, in this order: interrupts off and one CPU up, so nothing + * takes the registry's lock. @dev is the sleep driver's, for the log. + * + * Every registered coprocessor has to be ready: returns how many aren't, each logged, and sets + * @pcie_ans_idled if ANS is among the ready ones. + */ +unsigned int apple_sleep_coprocs_refuse(struct device *dev, bool *pcie_ans_idled); +/* Each ready coprocessor's last check: the first refusal, or 0. */ +int apple_sleep_coprocs_check(struct device *dev); +bool apple_sleep_enclave_registered(void); +/* The enclave's stop, last of all refusals: nonzero only if no request was sent. */ +int apple_sleep_enclave_stop(struct device *dev); +/* The last stops, those registered first with @first and then the rest. */ +void apple_sleep_coprocs_stop(bool first, bool *pcie_ans); + #endif diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index fb535cf84c8f6..df9b1c735f424 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -904,8 +905,56 @@ static bool apple_sleep_coproc_lowers(u32 ps) return false; } +bool apple_sleep_enclave_registered(void) +{ + return apple_sleep_enclave.dev; +} + +void apple_sleep_registry_show(struct seq_file *s) +{ + int i; + + guard(mutex)(&apple_sleep_coprocs_lock); + seq_puts(s, "coprocessors registered for the commit's last stop:\n"); + for (i = 0; i < SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev) + continue; + seq_printf(s, " %s%s%s%s%s\n", dev_name(c->dev), c->pcie_ans ? " (ANS)" : "", + c->first ? " (first)" : "", c->last_check ? ", last check" : "", + c->ready ? ", ready" : ""); + } + seq_printf(s, "enclave sleep request left for the last refusal: %s\n", + apple_sleep_enclave.dev ? dev_name(apple_sleep_enclave.dev) : "none"); +} + +/* A registered coprocessor that isn't ready refuses the sleep. */ +unsigned int apple_sleep_coprocs_refuse(struct device *dev, bool *pcie_ans_idled) +{ + unsigned int refused = 0; + int i; + + for (i = 0; i < SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev) + continue; + if (c->ready) { + *pcie_ans_idled |= c->pcie_ans; + } else { + /* Nothing may lower a coprocessor's domain under its driver. */ + dev_err(dev, "refused: %s is running and its driver has not quiesced it\n", + dev_name(c->dev)); + refused++; + } + } + + return refused; +} + /* The last refusals, each coprocessor's own: nothing has stopped yet. */ -static int apple_sleep_coprocs_check(struct apple_sleep *s) +int apple_sleep_coprocs_check(struct device *dev) { int i, ret; @@ -916,7 +965,7 @@ static int apple_sleep_coprocs_check(struct apple_sleep *s) continue; ret = c->last_check(c->data); if (ret) { - dev_err(s->dev, "refused: %s: %d\n", dev_name(c->dev), ret); + dev_err(dev, "refused: %s: %d\n", dev_name(c->dev), ret); return ret; } } @@ -940,7 +989,7 @@ static int apple_sleep_cpus_hold(struct apple_sleep *s) } /* A nonzero result must mean no SLEEP was sent; post-send errors are fatal. */ -static int apple_sleep_enclave_stop(struct apple_sleep *s) +int apple_sleep_enclave_stop(struct device *dev) { int ret; @@ -948,7 +997,7 @@ static int apple_sleep_enclave_stop(struct apple_sleep *s) return 0; ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); if (ret) - dev_err(s->dev, "refused before SEP SLEEP was sent: %d\n", ret); + dev_err(dev, "refused before SEP SLEEP was sent: %d\n", ret); return ret; } @@ -957,20 +1006,21 @@ static int apple_sleep_enclave_stop(struct apple_sleep *s) * In two passes, after the last refusal: from the first stop only a real sleep restarts the * GPU, so a failure here goes on to the commit. Those registered first stop whole first, * before macOS's off words drop under them: the GPU withdraws its PMP request there, and the - * PMP has to see that before its own stop. The rest after the match. + * PMP has to see that before its own stop. The rest after the match. With ANS among the ready + * ones, @pcie_ans. */ -static void apple_sleep_coprocs_stop(struct apple_sleep *s, bool first) +void apple_sleep_coprocs_stop(bool first, bool *pcie_ans) { int i; if (first) - s->pcie_ans_stopped = false; + *pcie_ans = false; for (i = 0; i < SLEEP_COPROCS; i++) { struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; if (!c->dev || !c->ready || c->first != first) continue; - s->pcie_ans_stopped |= c->pcie_ans; + *pcie_ans |= c->pcie_ans; if (c->stopped) continue; c->stop_ret = c->last_stop ? c->last_stop(c->data) : 0; @@ -1571,20 +1621,8 @@ static int apple_sleep_refuse(struct apple_sleep *s) bool refused = false, idled = false; int i; - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev) - continue; - if (c->ready) { - idled |= c->pcie_ans; - } else { - /* Nothing may lower a coprocessor's domain under its driver. */ - dev_err(s->dev, "refused: %s is running and its driver has not quiesced it\n", - dev_name(c->dev)); - refused = true; - } - } + if (apple_sleep_coprocs_refuse(s->dev, &idled)) + refused = true; if (num_online_cpus() != 1 || (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { @@ -3177,19 +3215,19 @@ static int apple_sleep_enter(suspend_state_t state) if (!ret) ret = sleep_display_off(s); if (!ret) - ret = apple_sleep_coprocs_check(s); + ret = apple_sleep_coprocs_check(s->dev); /* Cover the platform's own preparation, after the PM core's early check. */ if (!ret && pm_wakeup_pending()) ret = -EBUSY; if (!ret) - ret = apple_sleep_enclave_stop(s); + ret = apple_sleep_enclave_stop(s->dev); if (ret) { dev_dbg(s->dev, "refused before any commit coprocessor stop: %d\n", ret); spin_table_unhold_parked(); apple_sleep_stray_restore(s); return ret; } - apple_sleep_coprocs_stop(s, true); + apple_sleep_coprocs_stop(true, &s->pcie_ans_stopped); sleep_wake_arm(s); apple_sleep_match(s); @@ -3197,7 +3235,7 @@ static int apple_sleep_enter(suspend_state_t state) /* Acknowledge a leftover done flag, as the DCS word has none on macOS. */ if (s->soc->ack_cluster_dcs && (readl(s->cluster_dcs) & CLUSTER_UP_DONE)) writel(readl(s->cluster_dcs), s->cluster_dcs); - apple_sleep_coprocs_stop(s, false); + apple_sleep_coprocs_stop(false, &s->pcie_ans_stopped); /* Done by now unless no SMC driver is bound. */ apple_sleep_usb_down(s); s->deep_once = READ_ONCE(sleep_deep_requested); From cd2f1c0f1907eee5eb5834eddc4d95ff7f5cb278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:54:32 +0100 Subject: [PATCH 084/269] soc: apple: sleep: let a part decline the lid and the USB wake ports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SMC's driver tells this driver how the lid reads at each wake, and the USB host's which hub ports may wake the system. The display and the port controllers ask for both, and act on the answers: the internal panel stays dark for a closed lid, a port is armed for wake. A SoC's part whose sleep was never run with either must not start to take them just by being built into this driver. Let it say so at its probe: apple_sleep_lid_closed_at_wake() then returns -ENODATA and apple_sleep_usb_port_can_wake() false, as both do in a kernel without the driver. No part sets it yet, and nothing changes on the T8140. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 7 +++++++ drivers/soc/apple/sleep.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 1f1ca7e6cbdbd..e4e00417ff1a6 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -85,6 +85,13 @@ struct apple_sleep_part { struct apple_sleep_ctl registry; }; +/* + * A part whose sleep takes neither the lid's state at the wake nor the USB wake ports says so + * at its probe. apple_sleep_lid_closed_at_wake() and apple_sleep_usb_port_can_wake() then + * answer as they do with no sleep driver, whatever the SMC's and the USB host's drivers told. + */ +void apple_sleep_set_part_plain_wake(void); + /* For a debugfs file: who is registered, and how far each is. */ void apple_sleep_registry_show(struct seq_file *s); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index df9b1c735f424..588cfcaede20e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -660,6 +660,14 @@ struct apple_sleep { static struct apple_sleep *apple_sleep; +/* The SoC's part takes neither the lid at the wake nor the USB wake ports. */ +static bool apple_sleep_part_plain_wake; + +void apple_sleep_set_part_plain_wake(void) +{ + WRITE_ONCE(apple_sleep_part_plain_wake, true); +} + /* A registered coprocessor its driver has readied, whose run bit the last step clears. */ static bool apple_sleep_coproc_ready_node(const struct device_node *np) { @@ -690,6 +698,9 @@ EXPORT_SYMBOL_GPL(apple_sleep_usb_set_wake_ports); bool apple_sleep_usb_port_can_wake(unsigned int port) { + if (READ_ONCE(apple_sleep_part_plain_wake)) + return false; + return port < BITS_PER_LONG && (READ_ONCE(sleep_usb_wake_ports) & BIT(port)); } EXPORT_SYMBOL_GPL(apple_sleep_usb_port_can_wake); @@ -2001,6 +2012,9 @@ EXPORT_SYMBOL_GPL(apple_sleep_lid_at_wake); int apple_sleep_lid_closed_at_wake(void) { + if (READ_ONCE(apple_sleep_part_plain_wake)) + return -ENODATA; + return READ_ONCE(sleep_lid_closed); } EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_at_wake); From 7985932f9d304f31889270f0eaa9bcbc2fb1980a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:55:43 +0100 Subject: [PATCH 085/269] arm64: dts: apple: t6040: enable the sleep controller per board MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 sleep controller's node is in the SoC's file, so every T6040 board has it, while its sleep has been run on the J616s alone. It maps fixed T6040 blocks and installs suspend operations that make deep the default state and then refuse it without t6040_sleep.allow_entry, which is of no use on a board nobody has tested, and distribution kernels build the controller. Disable the node in t6040.dtsi and enable it in t6040-j616s.dts. On the J614s and the J773s the controller no longer binds: suspend-to-idle stays their sleep, and the debugfs preflight file is gone there. The interrupt controller saved its configuration for a sleep on every apple,t6041-aic3 whenever the controller was built. Key that on the enabled node as well, so that the other machines with this AIC keep their suspend and hibernation paths as they were. Signed-off-by: František Balázsy --- drivers/irqchip/irq-apple-aic.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index 6cccf4aaf1159..27b2f3a8cd7d4 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -1287,17 +1287,32 @@ static struct syscore aic_syscore = { .ops = &aic_syscore_ops, }; +/* + * The T6040's AIC (apple,t6041-aic3, matched through aic3_info) is the same + * AICv3 as the T8140's: the same CONFIG, TIMEOUT and CLUSTER_PRIO words and + * IRQ_CFG array. It is admitted to the sleep save only on a board whose device + * tree enables the T6040 sleep controller, so that every other machine with + * this AIC suspends and hibernates as before. + */ +static bool __init aic_t6040_sleep(struct device_node *node) +{ + struct device_node *np; + bool sleeps; + + if (!IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) || + !of_device_is_compatible(node, "apple,t6041-aic3")) + return false; + + np = of_find_compatible_node(NULL, NULL, "apple,t6040-sleep"); + sleeps = of_device_is_available(np); + of_node_put(np); + + return sleeps; +} + static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) { - /* - * The T6040's AIC (apple,t6041-aic3, matched through aic3_info) is the - * same AICv3 as the T8140's: the same CONFIG, TIMEOUT and CLUSTER_PRIO - * words and IRQ_CFG array. Its suspend-to-RAM path is the separate - * T6040 sleep controller, so it is admitted here only with that. - */ - if (!irqc->info.fw_suspend && - !(IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) && - of_device_is_compatible(node, "apple,t6041-aic3"))) + if (!irqc->info.fw_suspend && !aic_t6040_sleep(node)) return; aic_sleep.irq_cfg = kcalloc(irqc->nr_die * irqc->nr_irq, sizeof(u32), GFP_KERNEL); From c79f1bc0478b037b47e9c2dda4773d14af516534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:01:45 +0100 Subject: [PATCH 086/269] soc: apple: sleep: take the T6040 controller in as a part MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 controller was a driver of its own, t6040-sleep.c, next to the generic sleep driver: two platform drivers, two sets of suspend operations and two bindings to the registry in one kernel, of which one binds. Make it a part of the generic driver. The file becomes sleep-t6040.c in the same object, matched through apple,t6040-sleep with its own probe and restore. It keeps its state, its suspend operations and every sequence as they were, suspend-to-idle passing through untouched and deep offered only with t6040_sleep.allow_entry included. Its platform driver and its own walks of the registry go: it calls the generic driver for the readiness check, the last checks, the enclave's stop, the stop passes and the preflight listing, in the order it had. Its two answers to the registry are the part's, bound by the generic driver, and are what they were: the commit stops the coprocessors only in a real entry at EL2, and every enclave is taken. It tells the generic driver at probe that it takes neither the lid at the wake nor the USB wake ports, which were stubs for it when its sleep was tested. The dry run level moves to the generic driver, which exports apple_sleep_power_loss() and apple_sleep_last_steps() and answers them by the level a part sets: 0 unless a dry run of the T6040 is in progress, so on the T8140 both are pm_suspend_via_firmware() as before. Seen from outside: the parameters keep their t6040_sleep. prefix and the debugfs directory its name. The device's log lines carry the driver name apple-sleep now, and the refusals that come from the registry are the generic driver's lines with that prefix instead of "t6040-sleep:" ones; two debug lines about the registry are gone. The stop passes note ANS as the generic driver's do, for every ready ANS, which differs only for one that is ready and already stopped, and its driver clears that as it readies it. APPLE_T6040_SLEEP stays the switch of the part and now depends on APPLE_SOC_SLEEP; it keeps its dependency on a built-in SMC driver, whose key the part writes. The SEP's capability test and its host test go back to the one symbol, and the match data test knows the second compatible and skips the lists of a SoC that has a part. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 40 +-- drivers/soc/apple/Makefile | 2 +- drivers/soc/apple/pm_shim.c | 3 +- drivers/soc/apple/sleep-internal.h | 18 +- drivers/soc/apple/sleep-registry.c | 2 +- .../apple/{t6040-sleep.c => sleep-t6040.c} | 307 +++++------------- drivers/soc/apple/sleep-test.c | 10 + drivers/soc/apple/sleep.c | 35 ++ include/linux/soc/apple/sleep.h | 4 +- tools/asahi/sep-suspend-errors-tests.py | 4 +- 10 files changed, 161 insertions(+), 264 deletions(-) rename drivers/soc/apple/{t6040-sleep.c => sleep-t6040.c} (92%) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 5755a83d02fed..c51eaf3a8fb18 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -110,8 +110,9 @@ config APPLE_SOC_SLEEP /chosen/aurora,s2r-mailbox. Only SoCs whose sleep has been measured and tested are supported, - each by its match data: today T8140 and T8132. On every other SoC - the driver doesn't bind, and s2idle stays the only system sleep. + each by its match data: today T8140, T8132, and with + APPLE_T6040_SLEEP the T6040. On every other SoC the driver doesn't + bind, and s2idle stays the only system sleep. config APPLE_SOC_SLEEP_KUNIT_TEST bool "KUnit tests for the Apple SoC sleep match data" if !KUNIT_ALL_TESTS @@ -127,33 +128,26 @@ config APPLE_SOC_SLEEP_KUNIT_TEST config APPLE_T6040_SLEEP bool "Apple T6040 system sleep (refusal-first)" - depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) - depends on HOTPLUG_CPU && SUSPEND + depends on APPLE_SOC_SLEEP depends on MFD_MACSMC=y - select APPLE_SLEEP_REGISTRY select MFD_SYSCON - select APPLE_TUNABLE help - The T6040 (M4 Pro) suspend-to-RAM controller, still a separate - driver next to APPLE_SOC_SLEEP until the T6040 becomes match data - there. It registers a suspend handler that enumerates every - blocker in the live state (EL2, the boot object's return path, the - CPUs that can park, the display power states the platform checks before it - sleeps). Unless t6040_sleep.allow_entry=1 is given and no blocker - remains, a suspend request prints the blockers and enters nothing. + The T6040 (M4 Pro) part of the Apple SoC sleep driver, on the + boards whose device tree enables its sleep controller. The T6040 + has its own sequences and refuses first: its suspend handler lists + every blocker in the live state (EL2, the boot object's return + path, the CPUs that can park, the display power states the platform checks + before it sleeps). Unless t6040_sleep.allow_entry=1 is given and no + blocker remains, a request for suspend-to-RAM prints the blockers + and enters nothing, and suspend-to-idle stays the default sleep. With it the SoC is powered down to suspend-to-RAM and comes back through the boot object; the AON watchdog is the net over the - whole cycle (t6040_sleep.sleep_wd_secs at the power-down itself). - - It keeps the coprocessor and enclave registry for the T6040, so - the drivers that leave their last stop to the sleep's commit (ANS, - the PMP, the SEP) can register, and offers dry runs: with - t6040_sleep.dry_run=1 every driver's firmware-driven sleep step that - can be taken back runs, the CPUs park and are held, and the sleep is - refused at the commit point and resumed warm. + whole cycle (t6040_sleep.sleep_wd_secs at the power-down itself, + t6040_sleep.wd2_secs over a dry run). - It shares the registry with APPLE_SOC_SLEEP, and both can be built - into one kernel: each binds only to its own SoC. + With t6040_sleep.dry_run=1 every driver's firmware-driven sleep + step that can be taken back runs, the CPUs park and are held, and + the sleep is refused at the commit point and resumed warm. config APPLE_RTKIT tristate "Apple RTKit co-processor IPC protocol" diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 5f80ad9fcd044..3fd9381fa0a00 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -16,7 +16,7 @@ obj-$(CONFIG_APPLE_CLUSTER_PWRGATE) += apple-cluster-pwrgate.o obj-$(CONFIG_APPLE_SLEEP_REGISTRY) += sleep-registry.o obj-$(CONFIG_APPLE_SOC_SLEEP) += apple-sleep.o apple-sleep-y = sleep.o -obj-$(CONFIG_APPLE_T6040_SLEEP) += t6040-sleep.o +apple-sleep-$(CONFIG_APPLE_T6040_SLEEP) += sleep-t6040.o obj-$(CONFIG_APPLE_RTKIT) += apple-rtkit.o apple-rtkit-y = rtkit.o rtkit-crashlog.o rtkit-hib-handoff.o diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 487e4a1ef446d..689217e8d94cc 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -472,8 +472,7 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, int ret; /* Check capability before resources that legacy providers need not have. */ - if (!READ_ONCE(sep_pm_suspending) || - !(IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || IS_ENABLED(CONFIG_APPLE_T6040_SLEEP)) || + if (!READ_ONCE(sep_pm_suspending) || !IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || !is_kernel_in_hyp_mode()) return -EOPNOTSUPP; if (c->registered) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index e4e00417ff1a6..2ec639e889bee 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -1,9 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ /* - * The coprocessor and enclave registry that the Apple SoC sleep controllers - * (sleep.c for T8140, t6040-sleep.c for T6040) share. Drivers register through - * ; the controller bound on this SoC walks the table - * at its commit. + * The coprocessor and enclave registry, and what the Apple SoC sleep driver + * (sleep.c) shares with a SoC's own part of it (sleep-t6040.c for T6040). + * Drivers register through ; the controller bound on + * this SoC walks the table at its commit. */ #ifndef _APPLE_SLEEP_INTERNAL_H_ #define _APPLE_SLEEP_INTERNAL_H_ @@ -85,6 +85,16 @@ struct apple_sleep_part { struct apple_sleep_ctl registry; }; +/* sleep-t6040.c */ +extern const struct apple_sleep_part t6040_sleep_part; + +/* + * For a part with dry runs, from its .begin to its .end: 1 runs the firmware-driven device + * suspend short of what only a power cycle restores, 2 also the last steps that can still be + * taken back. apple_sleep_power_loss() and apple_sleep_last_steps() answer by it. + */ +void apple_sleep_set_dry(unsigned int level); + /* * A part whose sleep takes neither the lid's state at the wake nor the USB wake ports says so * at its probe. apple_sleep_lid_closed_at_wake() and apple_sleep_usb_port_can_wake() then diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c index ee3c5264a8c9c..2a6ae9da5cc9b 100644 --- a/drivers/soc/apple/sleep-registry.c +++ b/drivers/soc/apple/sleep-registry.c @@ -5,7 +5,7 @@ * A driver whose coprocessor can only be stopped once a sleep is certain * registers it here, readies it in its own suspend and leaves the last stop to * the commit of whichever sleep controller is bound on this SoC (sleep.c, - * t6040-sleep.c). The table lives apart from the controllers so that one + * sleep-t6040.c). The table lives apart from the controllers so that one * kernel can carry all of them; each binds only to its own SoC. */ diff --git a/drivers/soc/apple/t6040-sleep.c b/drivers/soc/apple/sleep-t6040.c similarity index 92% rename from drivers/soc/apple/t6040-sleep.c rename to drivers/soc/apple/sleep-t6040.c index 5da1f6e31e8fd..2713a3e14601c 100644 --- a/drivers/soc/apple/t6040-sleep.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -46,7 +46,6 @@ #include #include -#include #include #include #include @@ -54,8 +53,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -84,6 +82,10 @@ #include "sleep-internal.h" +/* The parameters keep the names they had when this was a driver of its own. */ +#undef MODULE_PARAM_PREFIX +#define MODULE_PARAM_PREFIX "t6040_sleep." + #define PS_TARGET GENMASK(3, 0) #define PS_ACTUAL GENMASK(7, 4) #define PS_FLAGS GENMASK(9, 8) @@ -320,7 +322,7 @@ static const struct { /* * MasterMini (NUB_MPMGR_AON, RegMap 4 = pmgr reg[0x37] = 0x508900000). * The S2R timer at +0x4000 (physical 0x508904000) is the commit: writing 1 arms - * the SoC sleep engine. Measured on the J616s: the platform makes exactly this write. + * the SoC sleep engine. Measured on the J616s: exactly this write. */ #define MASTERMINI_S2R_TIMER 0x4000 @@ -339,7 +341,7 @@ static const struct { #define AXI2AF_BRIDGE_104 0x104 #define AXI2AF_BRIDGE_108 0x108 -/* The internal display, as the platform drives the PMGR and the DART. */ +/* The internal display: its PMGR words and its DART. */ #define T6040_DCP_DART_PA 0x402340000ULL #define T6040_DISP_PWRGATE_PA 0x5022a1000ULL #define DISP_PWRGATE_A 0x1c /* := 3 before DISP_SYS goes down, 0 after it is up */ @@ -471,7 +473,8 @@ struct t6040_sleep { struct device *dev; struct t6040_sleep_group *groups; unsigned int ngroups; - struct dentry *debugfs; + /* The dry run level of the suspend in progress, from .begin to .end; 0 otherwise. */ + unsigned int dry; void __iomem *cpu_start; void __iomem *wdt; /* The DT's wdt block is the one t6040_resume_stub hardcodes. */ @@ -565,7 +568,7 @@ MODULE_PARM_DESC(allow_entry, "Reach the real commit if no blocker remains"); /* - * The AON WD2 net armed at the commit. The platform arms 5 s for its reconfig; a longer + * The AON WD2 net armed at the commit. 5 s covers the reconfig; a longer * net tells "the SoC never slept" (reset at the net) from "it slept and the wake * failed" (reset at the wake source). Never off: 1 to 178. */ @@ -587,8 +590,8 @@ static void t6040_iounmap(void *p) } /* - * Every window here non-posted (nGnRnE), as the platform and aura map the PMGR, PMC and - * coprocessor blocks. No region request: these overlap other drivers. + * Every window here non-posted (nGnRnE), as the PMGR, PMC and coprocessor blocks + * require. No region request: these overlap other drivers. */ static void __iomem *t6040_ioremap_np(struct device *dev, phys_addr_t pa, size_t size) { @@ -599,30 +602,12 @@ static void __iomem *t6040_ioremap_np(struct device *dev, phys_addr_t pa, size_t return p; } -/* The dry run level of the suspend in progress, from .begin to .end; 0 otherwise. */ -static unsigned int t6040_sleep_dry; - -bool apple_sleep_power_loss(void) -{ - return pm_suspend_via_firmware() && !READ_ONCE(t6040_sleep_dry); -} -EXPORT_SYMBOL_GPL(apple_sleep_power_loss); - -bool apple_sleep_last_steps(void) +/* The generic driver answers apple_sleep_power_loss() and apple_sleep_last_steps() by it. */ +static void t6040_sleep_set_dry(struct t6040_sleep *s, unsigned int dry) { - unsigned int dry = READ_ONCE(t6040_sleep_dry); - - return pm_suspend_via_firmware() && (!dry || dry >= 2); + s->dry = dry; + apple_sleep_set_dry(dry); } -EXPORT_SYMBOL_GPL(apple_sleep_last_steps); - -/* - * The coprocessor and enclave registry (sleep-registry.c), shared with the T8140 - * controller, same contract (sleep.h). A registered coprocessor that isn't ready - * refuses the sleep; a ready one keeps running until the commit's last stop, - * after every refusal, so a refused sleep finds it as it was. - */ -#define SLEEP_COPROCS APPLE_SLEEP_COPROCS /* A real entry: its commit stops the registered coprocessors; a dry run's never does. */ static bool t6040_sleep_reg_commit_stops(void) @@ -630,12 +615,8 @@ static bool t6040_sleep_reg_commit_stops(void) return READ_ONCE(t6040_sleep) && is_kernel_in_hyp_mode() && apple_sleep_power_loss(); } -static const struct apple_sleep_ctl t6040_sleep_reg_ctl = { - .commit_stops = t6040_sleep_reg_commit_stops, -}; - /* - * As in the power-state check, a sub-block is on while its TARGET is not 0. A + * In the power-state check, a sub-block is on while its TARGET is not 0. A * pipe's CPU word never counts: it stays under automatic power gating (taking it out * of automatic mode raised an SError on T6040) and the SoC power-down drops it. */ @@ -721,7 +702,7 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct blockers += !el2 + !mailbox + (parkable != num_possible_cpus() - 1); if (s) - seq_puts(s, "power states the platform checks before sleep (device-check-ps-b4-sleep):\n"); + seq_puts(s, "power states checked before sleep (device-check-ps-b4-sleep):\n"); for (i = 0; i < sleep->ngroups; i++) { unsigned int on = t6040_sleep_show_group(s, &sleep->groups[i]); @@ -741,25 +722,6 @@ static unsigned int t6040_sleep_total_blockers(struct t6040_sleep *sleep) return t6040_sleep_live_blockers(sleep, NULL, NULL); } -static void t6040_sleep_show_registry(struct seq_file *s) -{ - int i; - - guard(mutex)(&apple_sleep_coprocs_lock); - seq_puts(s, "coprocessors registered for the commit's last stop:\n"); - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev) - continue; - seq_printf(s, " %s%s%s%s%s\n", dev_name(c->dev), c->pcie_ans ? " (ANS)" : "", - c->first ? " (first)" : "", c->last_check ? ", last check" : "", - c->ready ? ", ready" : ""); - } - seq_printf(s, "enclave sleep request left for the last refusal: %s\n", - apple_sleep_enclave.dev ? dev_name(apple_sleep_enclave.dev) : "none"); -} - static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s); static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) @@ -768,7 +730,7 @@ static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) unsigned int live = t6040_sleep_live_blockers(sleep, s, NULL); unsigned int i; - t6040_sleep_show_registry(s); + apple_sleep_registry_show(s); t6040_axi2af_show(sleep, s); seq_printf(s, "verdict: %u live blocker(s)\n", live); seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", @@ -865,8 +827,8 @@ static u32 t6040_cpu_start_bit(u64 mpidr) /* * Each core's CoreSight external debug block (ADT cpuN coresight-reg: 0x210010000 + - * cluster * 0x1000000 + core * 0x100000). The platform has every CPU, boot and - * secondary, unlock it (EDLAR) and write EDPRCR := 0 right before its deep WFI, so that + * cluster * 0x1000000 + core * 0x100000). Every CPU, boot and secondary, + * unlocks it (EDLAR) and writes EDPRCR := 0 right before its deep WFI, so that * the debug logic does not prevent power gating. With * EDPRCR.CORENPDRQ left set, a core only emulates its power-down. */ @@ -1045,29 +1007,11 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) { struct t6040_sleep_watch *sep = &s->watch[T6040_WATCH_SEP]; struct t6040_sleep_watch *ans = &s->watch[T6040_WATCH_ANS]; - unsigned int refused = 0, ready = 0, registered = 0; bool ans_idled = false; + unsigned int refused; u32 val; - int i; - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev) - continue; - registered++; - if (c->ready) { - ready++; - ans_idled |= c->pcie_ans; - } else { - /* Nothing may lower a coprocessor's domain under its driver. */ - pr_err("t6040-sleep: refused: %s is running and its driver has not quiesced it\n", - dev_name(c->dev)); - refused++; - } - } - pr_debug("t6040-sleep: %u of %u registered coprocessors ready for the commit\n", ready, - registered); + refused = apple_sleep_coprocs_refuse(s->dev, &ans_idled); if (num_online_cpus() != 1 || !s->mbox || (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { @@ -1093,7 +1037,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) t6040_sleep_watch_on(sep, &val); pr_err("t6040-sleep: refused: sep isn't bound (state %#x)\n", val); refused++; - } else if (!apple_sleep_enclave.stop) { + } else if (!apple_sleep_enclave_registered()) { pr_err("t6040-sleep: refused: sep's sleep request isn't left for the last refusal\n"); refused++; } @@ -1121,42 +1065,6 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) return refused; } -/* The last refusals, each coprocessor's own: nothing has stopped yet. */ -static int t6040_sleep_coprocs_check(void) -{ - int i, ret; - - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev || !c->ready || c->stopped || !c->last_check) - continue; - ret = c->last_check(c->data); - if (ret) { - pr_err("t6040-sleep: refused: %s: %d\n", dev_name(c->dev), ret); - return ret; - } - pr_debug("t6040-sleep: last check of %s passed\n", dev_name(c->dev)); - } - - return 0; -} - -/* Last of all the refusals: an enclave that gated comes back only through a real sleep. */ -static int t6040_sleep_enclave_tell(void) -{ - int ret; - - if (!apple_sleep_enclave.stop) - return 0; - ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); - if (ret) - pr_err("t6040-sleep: refused: the enclave didn't take the sleep request: %d\n", - ret); - - return ret; -} - /* * The AOP and audio island's power words (MiniPMGR 0x508304000 + 0x000..0x158). The island * is off after a sleep, and the AOP's resume, which reads its ASC first, then faulted on the @@ -1215,8 +1123,8 @@ static void t6040_aop_ps_restore(struct t6040_sleep *s) /* * The power loss resets every power word, and only the domains a Linux driver owns are - * raised again when their devices resume. The PMGR puts each device back as it was; - * here the SIO peripheral words (I2C0 .. UART6 in SOC-East) that were fully on at the commit + * raised again when their devices resume. Here the SIO peripheral words (I2C0 .. UART6 in + * SOC-East) that were fully on at the commit * and came back lower are raised again, parents first. On J616s those are what the boot * loader left on and no driver claims: I2C0, SPI0/1/3/4/5 (SPI4 goes to the internal panel), * the UARTs, AES. Nothing else: every other word is its owner's to raise. @@ -1270,49 +1178,16 @@ static void t6040_sio_ps_restore(struct t6040_sleep *s) failed, raised); } -/* As on T8140, measured on ANS: status and control read, then the run bit cleared twice. */ -static void t6040_sleep_coproc_run_off(struct apple_sleep_coproc *c) -{ - if (!c->asc) { - c->stopped = true; - return; - } - c->status = readl(c->asc + ASC_CPU_STATUS); - writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); - writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); - c->stopped = true; -} - -/* After the last refusal: those registered first stop whole first, then the rest. */ -static void t6040_sleep_coprocs_stop(bool first) -{ - int i; - - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev || !c->ready || c->first != first || c->stopped) - continue; - if (c->last_stop) - c->last_stop(c->data); - t6040_sleep_coproc_run_off(c); - /* ANS stopped: the commit quiesces its storage link, the wake raises it. */ - if (c->pcie_ans && t6040_sleep) - t6040_sleep->pcie_ans_stopped = true; - } - dsb(sy); -} - /* * The 4 clk-mux park words (ClkGen SOC + these offsets), from - * The clock quiesce; the offsets skip 0x58/0x5c. + * the clock quiesce; the offsets skip 0x58/0x5c. */ static const u32 t6040_clkgen_mux[CLKGEN_MUX_WORDS] = { 0x38060, 0x38064, 0x38054, 0x38050, }; /* - * The 9 AOP-global wake-source masks the platform arms before an S2R + * The 9 AOP-global wake-source masks armed before an S2R * (the values are decoded from the ADT's ap-wake-sources). * valid & ~enabled per word. */ @@ -1507,7 +1382,7 @@ static void t6040_secondaries_off(struct t6040_sleep *s) } } -/* The hardware steps of the generic PMGR quiesce, in the order (see t6040_acc_apsc_pa). */ +/* The hardware steps of the generic PMGR quiesce, in order (see t6040_acc_apsc_pa). */ static void t6040_pmgr_quiesce(struct t6040_sleep *s) { u64 q; @@ -1753,7 +1628,7 @@ static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_t * clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP clock-gated. It polls APCIE_PHY_SW for * its parents, which are both SYS_ST lanes: with them lowered here, as the T8140 controller * does, it waited there for good on the J616s. So the commit raises APCIE_SYS_ST1, which - * Linux never uses, and does only what the platform does before the engine runs: the shared PHY put + * Linux never uses, and does only what the engine needs before it runs: the shared PHY put * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. */ @@ -1809,7 +1684,7 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) /* * The sleep engine hands each ATC port to its always-on side and then powers the port's - * common domain off itself: it needs the four ATC commons on, as they are on the platform at that + * common domain off itself: it needs the four ATC commons on at that * point. With them off it stalled on the J616s raising ATC0_COMMON, whose parent FAB2_SOC * was off as well. FAB2_SOC is raised by hand, then ATC0..3_COMMON to a manual target 0xf, * then FAB2_SOC gets its automatic mode back: the powered children hold it up. @@ -1832,7 +1707,7 @@ static void t6040_atc_commons_on(struct t6040_sleep *s) } /* - * The CFG writes of the PHY sleep entry, which it runs on every ATC port before a sleep: + * The CFG writes of the PHY sleep entry, run on every ATC port before a sleep: * ATCPHY_CFG +0x2c bit 0 (S2R_AON_SYNC_RST_N), +0x08 bit 0 (CFG_S2R_FSM_EN) and +0x28 bit 0 * (AHB_CLK_REQ_N) set, then +0x04 bit 8 (the clock's ack) polled. Without them the engine * stalled on the J616s on a port that carries DisplayPort lanes: port 3 (the HDMI output) @@ -1859,7 +1734,7 @@ static void t6040_atc_s2r_entry(struct t6040_sleep *s) } /* - * The 13 fabric power words. On the platform the fabrics stay up through the entry, and the wake + * The 13 fabric power words. The fabrics are expected up through the entry, and the wake * polls these words for automatic mode with target 0xf. Under Linux genpd has powered some * of them off with their last consumer by now (FAB2_SOC with the ATC commons at * suspend_noirq). Right before the S2R timer each one not in that state is raised: target @@ -1901,7 +1776,7 @@ static void t6040_fabrics_on(struct t6040_sleep *s) * The memory channels' power gating. The sleep engine does not write the DCS power words: it * expects each DCS to gate itself after self-refresh. That takes the DCS's dynamic power-gate * mode (DCS +0x20 bits 12 and 8) and its power word in automatic mode with no minimum state. - * The platform sets both at boot and again after each wake. Nothing does under Linux (the DT leaves + * Both are expected at boot and again after each wake. Nothing sets them under Linux (the DT leaves * the ps_dcs_* domains disabled: target 0xf, no automatic mode), and the engine then waited * for the first DCS pair's power gate for good on the J616s. Both are applied in each .begin. */ @@ -1923,7 +1798,7 @@ static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) } /* - * The order: the minimum state cleared, target 0xf, ACTUAL 0xf polled, then automatic + * In order: the minimum state cleared, target 0xf, ACTUAL 0xf polled, then automatic * mode. Returns a mask of the DCS whose ACTUAL did not reach 0xf. */ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) @@ -1964,8 +1839,8 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) } /* - * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The platform clears the bit before a - * retained domain goes off and sets it again once it is back up; here the wake sets it + * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The bit is cleared before a + * retained domain goes off and set again once it is back up; here the wake sets it * (t6040_sleep_raise_apcie_st()) and the SoC's sleep engine, not this driver, takes ANS down. * The boot loader leaves the bit clear, so a boot's first sleep goes down with it clear; * without this the next one meets the bridge as the first wake armed it. Only while ANS is @@ -2050,7 +1925,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) run = a->phy + APCIE_PHY_RUN; /* - * The order, each domain to ACTUAL 0xf: ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, + * In order, each domain to ACTUAL 0xf: ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, * the fabric-bridge bit0s set after ANS and after SYS_GP. SYS_GP stays at a manual * target 0xf: in automatic mode it gated again on the J616s, and the DART behind it * then faulted in its resume. @@ -2168,8 +2043,8 @@ extern char t6040_resume_stub[]; /* * The four ATC complexes' power words in SOC (soc_ps, 0x502800000), deepest first: cio_usb, - * cio_pcie, cio, pcie, common (ADT: ATCn_CIO_USB .. ATCn_COMMON). The platform has them off at its - * quiesce gate; their drivers don't lower atcN_common. + * cio_pcie, cio, pcie, common (ADT: ATCn_CIO_USB .. ATCn_COMMON). The quiesce gate wants + * them off; their drivers don't lower atcN_common. * * Their common_dp, usb, phymxwrap and usb_aon words are MiniPMGR words (0x508300000 + 0xa0.., * ADT ATCn_COMMON_DP/USB/PHYMXWRAP/USB_AON), not SOC ones: applied to soc_ps, @@ -2184,7 +2059,7 @@ static const u16 t6040_atc_words[4][5] = { { 0x438, 0x430, 0x420, 0x428, 0x408 }, }; -/* As the platform lowers a domain (T8140 measured): AUTO_ENABLE off first, then target 0, poll. */ +/* A domain lowered (T8140 measured): AUTO_ENABLE off first, then target 0, poll. */ static int t6040_ps_lower_word(void __iomem *reg) { u32 val = readl(reg); @@ -2248,7 +2123,7 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) } writel(readl(phy) & ~GP_PHY_LINK, phy); udelay(1000); - /* What this bit does isn't known. The platform sets it around PERST# (T8140). */ + /* What this bit does isn't known. It is set around PERST# (T8140). */ writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); writel(0, port + GP_PORT_PERST); writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); @@ -2345,11 +2220,11 @@ static int t6040_sleep_commit(unsigned long unused) t6040_sleep_lower_apcie_st(s); t6040_sleep_lower_apcie_gp(s); - /* WD0 off, then the AON WD2 net (the platform arms 5 s for its reconfig). */ + /* WD0 off, then the AON WD2 net (5 s covers the reconfig). */ if (s->wdt) writel(0, s->wdt + WDT_WD0_CTL); t6040_sleep_set_wd2(s, clamp(READ_ONCE(sleep_wd_secs), 1U, WDT_MAX_SECS)); - /* And the SMC's AP watchdog deadline off, as the platform right after its WD2 (measured). */ + /* And the SMC's AP watchdog deadline off, right after the WD2 (measured). */ if (s->smc_wdt_deadline) writel(0, s->smc_wdt_deadline); @@ -2372,7 +2247,7 @@ static int t6040_sleep_commit(unsigned long unused) t6040_agx_gate(s); t6040_clusters_off(s); - /* Read-only: the axi2af panic gate the platform checks before the engine goes. */ + /* Read-only: the axi2af panic gate checked before the engine goes. */ t6040_axi2af_readcheck(s); t6040_clkgen_park(s); s->ka_on_before = t6040_keep_alive(s, false, 0); @@ -2444,7 +2319,7 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) /* * The stop also wrote the controller's PERST register 0, and without it the * port does not report ready. Put it back as it was (the endpoint's PERST# pin - * stays asserted), with the guard bit the platform sets around it. + * stays asserted), with the guard bit set around it. */ writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); writel(s->gp_perst_saved[i], port + GP_PORT_PERST); @@ -2557,12 +2432,12 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_clkgen_restore(s); t6040_wake_sync(); - /* Software ack of the armed S2R request (the platform has no stop of the S2R timer). */ + /* Software ack of the armed S2R request (the S2R timer has no stop). */ if (s->aop_global) writel(0, s->aop_global + AOP_S2R_CTRL); t6040_wake_sync(); - /* The keep-alive bits the commit cleared, back (as the wake restore enables them). */ + /* The keep-alive bits the commit cleared, back (the wake restore enables them). */ if (s->ka_on_before) t6040_keep_alive(s, true, s->ka_on_before); t6040_wake_sync(); @@ -2655,7 +2530,7 @@ static int t6040_sleep_begin(suspend_state_t state) return 0; t6040_sleep_mem_begun = true; - WRITE_ONCE(t6040_sleep_dry, 0); + t6040_sleep_set_dry(s, 0); s->returned = false; s->gp_saved[0] = s->gp_saved[1] = false; t6040_aop_ps_save(s); @@ -2686,7 +2561,7 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_sleep_power_ops(s); if (!s->power_ops) pr_warn("t6040-sleep: dry run: no CPU parking power ops, so no hold at the commit point\n"); - WRITE_ONCE(t6040_sleep_dry, dry); + t6040_sleep_set_dry(s, dry); pm_set_suspend_via_firmware(); secs = min(READ_ONCE(wd2_secs), WDT_MAX_SECS); if (secs && s->wdt) @@ -2752,12 +2627,12 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) if (dry >= 2 && !ret) pr_info("t6040-sleep: the coprocessors' last checks: %d\n", - t6040_sleep_coprocs_check()); + apple_sleep_coprocs_check(s->dev)); if (!ret) spin_table_unhold_parked(); pr_info("t6040-sleep: dry run: refused before the enclave's sleep request (%s), the coprocessors' last stops and the SoC power-down\n", - apple_sleep_enclave.dev ? "left by sep" : "none left"); + apple_sleep_enclave_registered() ? "left by sep" : "none left"); return -EBUSY; } @@ -2891,7 +2766,7 @@ static int t6040_dcp_dart_idle(struct t6040_sleep *s) return 0; } -/* The internal display's FE and SYS domains, child first, after its sub-blocks (the order). */ +/* The internal display's FE and SYS domains, child first, after its sub-blocks. */ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group *g) { struct t6040_psw cpu = { .map = g->regmap, .off = g->first }; @@ -2904,8 +2779,8 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group s->disp_down = true; /* * The PMC's bandwidth-request and boost interface enables of the display pipes - * (0x5048ac020 / 0x5048ac028, one bit per pipe; the PMGR sets them when it powers - * a pipe's SYS domain). The power loss clears them and nothing sets them + * (0x5048ac020 / 0x5048ac028, one bit per pipe; they belong set while a pipe's SYS + * domain is powered). The power loss clears them and nothing sets them * again: without its boost bit a pipe's backlight data stream never runs. */ s->pmc_if_valid = false; @@ -2914,7 +2789,7 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group s->pmc_if_saved[1] = readl(s->pmc_bwr + PMC_BOOST); s->pmc_if_valid = true; } - /* The PMS DCP voters to 0 first, as the entry and T8140's driver end. */ + /* The PMS DCP voters to 0 first, as T8140's driver ends. */ if (s->pmc_dcp) { s->pmc_dcp_saved[0] = readl(s->pmc_dcp); s->pmc_dcp_saved[1] = readl(s->pmc_dcp + 4); @@ -2940,7 +2815,7 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group /* * The raise (the mirror): TARGET 15, poll ACTUAL 15, poll bit11; for DISP_SYS the * retention pair (A := 0, B := 1) and the axi2af bridge bit0s set; then bits 9:8 cleared and - * AUTO_ENABLE back where the platform sets it (DISP_CPU, DISP_SYS). + * AUTO_ENABLE back where it belongs (DISP_CPU, DISP_SYS). */ static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, bool sys, bool autoen) @@ -3001,7 +2876,7 @@ static void t6040_disp_back(struct t6040_sleep *s) } /* - * The display power words the platform has off before its sleep (apple,check-ps-b4-sleep), lowered + * The display power words the ADT wants off before a sleep (apple,check-ps-b4-sleep), lowered * here: after the device suspend, interrupts off, one CPU up, so no driver, interrupt handler * or DART touches the pipes any more. The DCPs only stop their firmware: lowering the domains * from their suspend panicked on T6040. Each group from its last sub-block up to, and not @@ -3049,7 +2924,7 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) static int t6040_sleep_enter(suspend_state_t state) { struct t6040_sleep *s = t6040_sleep; - unsigned int dry = READ_ONCE(t6040_sleep_dry), left; + unsigned int dry = s->dry, left; int ret; if (dry) @@ -3077,9 +2952,9 @@ static int t6040_sleep_enter(suspend_state_t state) t6040_disp_back(s); return ret; } - ret = t6040_sleep_coprocs_check(); + ret = apple_sleep_coprocs_check(s->dev); if (!ret) - ret = t6040_sleep_enclave_tell(); + ret = apple_sleep_enclave_stop(s->dev); if (ret) { spin_table_unhold_parked(); t6040_disp_back(s); @@ -3087,8 +2962,8 @@ static int t6040_sleep_enter(suspend_state_t state) } s->pcie_ans_stopped = false; s->ans_bridge_quiesced = false; - t6040_sleep_coprocs_stop(true); - t6040_sleep_coprocs_stop(false); + apple_sleep_coprocs_stop(true, &s->pcie_ans_stopped); + apple_sleep_coprocs_stop(false, &s->pcie_ans_stopped); t6040_wake_arm(s); s->cntkctl = read_sysreg(cntkctl_el1); @@ -3125,7 +3000,7 @@ static int t6040_sleep_enter(suspend_state_t state) static void t6040_sleep_end(void) { struct t6040_sleep *s = t6040_sleep; - unsigned int dry = READ_ONCE(t6040_sleep_dry); + unsigned int dry = s->dry; if (!t6040_sleep_mem_begun) return; @@ -3133,7 +3008,7 @@ static void t6040_sleep_end(void) t6040_sleep_clear_wd2(s); if (dry) pr_info("t6040-sleep: dry run %u over, WD2 off\n", dry); - WRITE_ONCE(t6040_sleep_dry, 0); + t6040_sleep_set_dry(s, 0); } static const struct platform_suspend_ops t6040_sleep_ops = { @@ -3371,12 +3246,6 @@ static int t6040_sleep_restore(struct device *dev) } #endif -static const struct dev_pm_ops t6040_sleep_pm_ops = { -#ifdef CONFIG_HIBERNATION - .restore = t6040_sleep_restore, -#endif -}; - /* The GP PCIe host's port and PHY windows and its ports' PERST# pins, for the commit. */ static void t6040_sleep_map_gp(struct t6040_sleep *s) { @@ -3434,8 +3303,12 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) static int t6040_sleep_probe(struct platform_device *pdev) { struct t6040_sleep *sleep; + struct dentry *dir; int ret, i; + /* This sleep was tested with neither the lid's state at the wake nor USB wake ports. */ + apple_sleep_set_part_plain_wake(); + sleep = devm_kzalloc(&pdev->dev, sizeof(*sleep), GFP_KERNEL); if (!sleep) return -ENOMEM; @@ -3514,15 +3387,11 @@ static int t6040_sleep_probe(struct platform_device *pdev) t6040_sleep_map_gp(sleep); t6040_ed_map(&pdev->dev); - ret = apple_sleep_registry_bind(&t6040_sleep_reg_ctl); - if (ret) - return dev_err_probe(&pdev->dev, ret, "another sleep controller is bound\n"); - platform_set_drvdata(pdev, sleep); t6040_sleep = sleep; - sleep->debugfs = debugfs_create_dir("t6040-sleep", NULL); - debugfs_create_file("preflight", 0400, sleep->debugfs, sleep, &t6040_sleep_preflight_fops); + dir = debugfs_create_dir("t6040-sleep", NULL); + debugfs_create_file("preflight", 0400, dir, sleep, &t6040_sleep_preflight_fops); /* * The CPU power ops act only when a commit stops the parked secondaries. @@ -3548,31 +3417,13 @@ static int t6040_sleep_probe(struct platform_device *pdev) return 0; } -static void t6040_sleep_remove(struct platform_device *pdev) -{ - struct t6040_sleep *sleep = platform_get_drvdata(pdev); - - debugfs_remove_recursive(sleep->debugfs); -} - -static const struct of_device_id t6040_sleep_of_match[] = { - { .compatible = "apple,t6040-sleep" }, - {} -}; -MODULE_DEVICE_TABLE(of, t6040_sleep_of_match); - -static struct platform_driver t6040_sleep_driver = { - .driver = { - .name = "t6040-sleep", - .of_match_table = t6040_sleep_of_match, - .pm = pm_sleep_ptr(&t6040_sleep_pm_ops), - /* The suspend ops can't be taken back. */ - .suppress_bind_attrs = true, - }, +const struct apple_sleep_part t6040_sleep_part = { .probe = t6040_sleep_probe, - .remove = t6040_sleep_remove, +#ifdef CONFIG_HIBERNATION + .restore = t6040_sleep_restore, +#endif + /* No enclave_admit: every enclave is taken, in a dry run as in a real sleep. */ + .registry = { + .commit_stops = t6040_sleep_reg_commit_stops, + }, }; -module_platform_driver(t6040_sleep_driver); - -MODULE_DESCRIPTION("Apple T6040 system sleep (refusal-first controller)"); -MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index d16544746d909..090d665d9d704 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -9,6 +9,9 @@ static const char *const apple_sleep_test_tested[] = { "apple,t8140-sleep", "apple,t8132-sleep", +#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) + "apple,t6040-sleep", +#endif }; static bool apple_sleep_test_has(const struct apple_sleep_words *all, u32 offset) @@ -77,6 +80,9 @@ static void apple_sleep_test_words(struct kunit *test) }; int i; + /* A SoC with its own sequences has no lists. */ + if (soc->part) + continue; apple_sleep_test_full(test, c, "ps_all", all); apple_sleep_test_full(test, c, "ps_aon_all", aon); for (i = 0; i < ARRAY_SIZE(named); i++) @@ -134,6 +140,8 @@ static void apple_sleep_test_books(struct kunit *test) for (id = apple_sleep_of_match; id->compatible[0]; id++) { const struct apple_sleep_soc *soc = id->data; + if (soc->part) + continue; KUNIT_EXPECT_LE(test, soc->nstray, BITS_PER_LONG); KUNIT_EXPECT_LE(test, soc->ngate_coprocs, ARRAY_SIZE(((struct apple_sleep *)0)->gate)); @@ -148,6 +156,8 @@ static void apple_sleep_test_wake_names(struct kunit *test) const struct apple_sleep_soc *soc = id->data; int i, j; + if (soc->part) + continue; KUNIT_EXPECT_NE(test, soc->wake_last_word, 0); for (i = 0; i < soc->nwake_names; i++) { u32 n = soc->wake_names[i].id; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 588cfcaede20e..c7dfb69a644ed 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -868,6 +868,32 @@ void apple_sleep_restore_pins(void) } EXPORT_SYMBOL_GPL(apple_sleep_restore_pins); +/* + * The dry run level of the suspend in progress, for a SoC's part that has dry runs: every + * sleep step that can be taken back runs, and the sleep is refused before the commit. 0 + * otherwise. + */ +static unsigned int apple_sleep_dry; + +void apple_sleep_set_dry(unsigned int level) +{ + WRITE_ONCE(apple_sleep_dry, level); +} + +bool apple_sleep_power_loss(void) +{ + return pm_suspend_via_firmware() && !READ_ONCE(apple_sleep_dry); +} +EXPORT_SYMBOL_GPL(apple_sleep_power_loss); + +bool apple_sleep_last_steps(void) +{ + unsigned int dry = READ_ONCE(apple_sleep_dry); + + return pm_suspend_via_firmware() && (!dry || dry >= 2); +} +EXPORT_SYMBOL_GPL(apple_sleep_last_steps); + /* The registry's questions, answered for this SoC's sleep. */ static bool apple_sleep_reg_commit_stops(void) { @@ -4481,6 +4507,12 @@ static const struct apple_sleep_soc t8132_sleep_soc = { .nwake_names = ARRAY_SIZE(t8132_wake_names), .wake_last_word = GENMASK(20, 0), }; +#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +/* T6040 (MacBook Pro J616s): its own sequences, in sleep-t6040.c. */ +static const struct apple_sleep_soc t6040_sleep_soc = { + .part = &t6040_sleep_part, +}; +#endif /* * Only SoCs whose sleep was measured and tested. On every other the driver doesn't bind, and @@ -4489,6 +4521,9 @@ static const struct apple_sleep_soc t8132_sleep_soc = { static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8140-sleep", .data = &t8140_sleep_soc }, { .compatible = "apple,t8132-sleep", .data = &t8132_sleep_soc }, +#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) + { .compatible = "apple,t6040-sleep", .data = &t6040_sleep_soc }, +#endif { } }; diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index cc89213189b9c..9c7580e9361eb 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -330,7 +330,7 @@ static inline int apple_sleep_engine_reset(struct device *dev) /* * Whether this firmware-driven suspend may end in a real power loss of the SoC, so that a - * driver may stop what only a power cycle brings back. A T6040 dry run sets + * driver may stop what only a power cycle brings back. A dry run, which the T6040 has, sets * pm_suspend_via_firmware() to run every sleep step that can still be taken back and then * refuses before the commit: there this is false, and such a stop has to be left out. * Everywhere else it is pm_suspend_via_firmware(). @@ -339,7 +339,7 @@ static inline int apple_sleep_engine_reset(struct device *dev) * just (the SMC's sleep commit, each coprocessor's last check), run too. A real sleep and * the deepest dry run take them. */ -#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +#if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) bool apple_sleep_power_loss(void); bool apple_sleep_last_steps(void); #else diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index 6ed77fa0dbcfd..113d4f3a0bd6a 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -52,8 +52,7 @@ def replace_once(source, before, after): 'read-as-deadline': ('sep_pm_wait_gated', 'if (ret)\n\t\treturn ret;\n\n\treturn !!poll_ret;', 'if (ret)\n\t\treturn 1;\n\n\treturn !!poll_ret;'), 'no-capability-check': ('sep_pm_commit_prepare', - ' ||\n\t !(IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) || IS_ENABLED(CONFIG_APPLE_T6040_SLEEP)) ||' - '\n\t !is_kernel_in_hyp_mode()', ''), + ' || !IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) ||\n\t !is_kernel_in_hyp_mode()', ''), 'no-transport-check': ('sep_pm_commit_prepare', 'if (!apple_mbox_poll_one_supported(mbox))', 'if (false)'), 'lost-accept': ('sleep_enter', 'pm::SleepGate::Lost => return Err(EIO),', @@ -110,7 +109,6 @@ def replace_once(source, before, after): #define READ_ONCE(x) (x) #define IS_ENABLED(x) (config_enabled) #define CONFIG_APPLE_SOC_SLEEP 1 -#define CONFIG_APPLE_T6040_SLEEP 1 #define __maybe_unused #define __iomem #define unlikely(x) (x) From 52563db349314ab938c1a2835f9c1015d8abff5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:02:28 +0100 Subject: [PATCH 087/269] soc: apple: sleep: t6040: use the shared mailbox layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part had its own copy of the boot object's mailbox layout and magic. Use the one the generic driver has in the private header. The compiled code is the same. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 2713a3e14601c..e4a27a0c3cdac 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -414,16 +414,6 @@ static const struct { #define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) #define IPI_SR_PENDING BIT(0) -/* Must match aura's struct aura_s2r_mailbox (aura #117). */ -#define T6040_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ - -struct t6040_s2r_mailbox { - u64 magic; - u64 boot_mpidr; - u64 resume_pa; - u64 resumes; -}; - struct t6040_sleep_group { const char *name; struct regmap *regmap; @@ -511,7 +501,7 @@ struct t6040_sleep { void __iomem *soc_ps; void __iomem *aop_global; void __iomem *axi2af_bridge[T6040_AXI2AF_DOMAINS]; - struct t6040_s2r_mailbox *mbox; + struct aura_s2r_mailbox *mbox; u64 mbox_pa; bool power_ops; bool wd2_armed; @@ -3178,14 +3168,14 @@ static void t6040_sleep_map_apcie(struct t6040_sleep *s) } /* Maps the mailbox at @pa if it is aura's, or returns NULL. */ -static struct t6040_s2r_mailbox *t6040_sleep_mailbox_at(struct t6040_sleep *s, u64 pa) +static struct aura_s2r_mailbox *t6040_sleep_mailbox_at(struct t6040_sleep *s, u64 pa) { - struct t6040_s2r_mailbox *mbox; + struct aura_s2r_mailbox *mbox; mbox = devm_memremap(s->dev, pa, sizeof(*mbox), MEMREMAP_WB); if (IS_ERR(mbox)) return NULL; - if (mbox->magic != T6040_S2R_MAGIC) { + if (mbox->magic != AURA_S2R_MAGIC) { dev_warn(s->dev, "s2r-mailbox magic %#llx is not AURAS2R1\n", mbox->magic); devm_memunmap(s->dev, mbox); return NULL; @@ -3223,7 +3213,7 @@ static void t6040_sleep_map_mailbox(struct t6040_sleep *s) static int t6040_sleep_restore(struct device *dev) { struct t6040_sleep *s = dev_get_drvdata(dev); - struct t6040_s2r_mailbox *mbox = NULL; + struct aura_s2r_mailbox *mbox = NULL; u64 pa = 0; int ret; From d62a1dc6575e7aab09d9fe0e9dc107c551d40e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:04:39 +0100 Subject: [PATCH 088/269] soc: apple: sleep: share the register definitions that are the same MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part repeated the generic driver's definitions of the power word, the CPU start block, the watchdog, the PMU's SPMI queue, the PCIe block that ANS shares and the sleep system registers, all with the same values. Move the generic driver's blocks of them to the private header as they are and drop the part's copies, with the ASC ones it no longer uses. What the generic driver takes from its per-SoC data stays with the part as its own values: the PMU write command with its USID, the boot stage register and value, the response vector's enable for the sleep. So does the complex timeout. The compiled code of both files is the same. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 118 +++++++++++++++++++++++++++++ drivers/soc/apple/sleep-t6040.c | 104 ++++--------------------- drivers/soc/apple/sleep.c | 111 --------------------------- 3 files changed, 132 insertions(+), 201 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 2ec639e889bee..a659b273ac504 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -8,9 +8,12 @@ #ifndef _APPLE_SLEEP_INTERNAL_H_ #define _APPLE_SLEEP_INTERNAL_H_ +#include #include #include +#include + struct device; struct platform_device; struct seq_file; @@ -73,6 +76,121 @@ struct aura_s2r_mailbox { u64 resumes; /* aura: resume_pa taken this many times */ }; +/* + * Registers that are the same on every SoC this driver knows. + */ + +/* PS word */ +#define PS_AUTO_ENABLE BIT(28) +#define PS_UNLATCH BIT(12) +#define PS_DEV_DISABLE BIT(10) +#define PS_FLAGS GENMASK(9, 8) +#define PS_ACTUAL GENMASK(7, 4) +#define PS_TARGET GENMASK(3, 0) +#define PS_OFF_TIMEOUT_US 5000 +#define PS_BUSY_SETTLE_US 1000 + +/* CPU start block, as aura's smp_start_cpu() uses it. A core's bit is the SoC's. */ +#define CPU_START_STOP 0x0 +#define CPU_START_SYS 0x4 +#define CPU_START_CLUSTER(n) (0x8 + (n) * 4) + +/* Watchdog block, ADT /arm-io/wdt reg[0]. WD2 is the sleep safety net. */ +#define WDT_WD0_COUNT 0x00 +#define WDT_WD0_CTL 0x0c +#define WDT_WD1_COUNT 0x10 +#define WDT_WD2_COUNT 0x20 +#define WDT_WD2_ALARM 0x28 +#define WDT_WD2_CTL 0x2c +#define WDT_TICKS_PER_S 24000000 +/* The sleep's own safety net: ADT /arm-io/wdt simple-reconfig-wdog-icc-time. */ +#define WDT_SLEEP_SECS 5 +/* Device suspend and resume can take a while, and nothing else recovers a hang in them. */ +#define WDT_SUSPEND_SECS 120 + +/* The AP queue of the PMU's SPMI controller, and its response vector's enable and ack */ +#define SPMI_STATUS 0x00 +#define SPMI_STATUS_RX_EMPTY BIT(24) +#define SPMI_CMD 0x04 +#define SPMI_REPLY 0x08 +#define SPMI_NOTIFY_ENABLE 0x40 +#define SPMI_NOTIFY_REPLY BIT(0) +#define SPMI_NOTIFY_ACK 0x80 +#define SPMI_CMD_ACTIVE BIT(15) +#define SPMI_OP_EXT_WRITE(n) ((n) - 1) +#define SPMI_OP_EXT_READ(n) (0x20 | ((n) - 1)) +#define SPMI_OP_ZERO_WRITE 0x80 +#define SPMI_OP_SLEEP 0x11 +#define SPMI_OP_WAKEUP 0x13 +#define SPMI_POLL_US 20000 + +/* A coprocessor's ASC */ +#define ASC_CPU_CONTROL 0x44 +#define ASC_CPU_CONTROL_RUN BIT(4) +#define ASC_CPU_STATUS 0x48 + +/* The part of the PCIe block that ANS shares, whether or not its host is bound. */ +#define APCIE_AXI_EN0 0x104 +#define APCIE_AXI_EN1 0x108 +#define APCIE_CLK_CTRL 0x0 +#define APCIE_CLK_BUSY BIT(31) +#define APCIE_CLK_RUN BIT(5) +#define APCIE_CLK_OFF BIT(1) +#define APCIE_CLK_OFF2 BIT(2) +#define APCIE_CLK_START 0x4 +#define APCIE_CLK_START_GO BIT(31) +#define APCIE_PHY_COMMON 0x4000 +#define APCIE_PHY_CTRL 0x8000 +#define APCIE_PHY_CTRL_CLK0REQ BIT(0) +#define APCIE_PHY_CTRL_CLK1REQ BIT(1) +#define APCIE_PHY_CTRL_CLK0ACK BIT(2) +#define APCIE_PHY_CTRL_CLK1ACK BIT(3) +#define APCIE_PHY_CTRL_RESET BIT(4) +#define APCIE_PHY_CTRL_UP BIT(27) +#define APCIE_PHY_RUN 0x8004 +#define APCIE_PHY_RUN_EN BIT(0) +#define APCIE_PHY_RUN_EN2 BIT(4) +#define APCIE_PHY_READY 0x8008 +#define APCIE_PHY_IP_PLL_CTL 0x90 +#define APCIE_PHY_IP_PLL_HOLD BIT(16) +#define APCIE_PORT_INTSTAT 0x100 +#define APCIE_PORT_INTMSK 0x104 +#define APCIE_PORT_INT_LINK GENMASK(7, 4) +#define APCIE_PORT_L2 0x88 +#define APCIE_PORT_L2_ENTER 0x11 +#define APCIE_PORT_L2_BUSY BIT(0) +#define APCIE_PORT_PERST_GUARD 0x13c +#define APCIE_PORT_LINKSTS 0x208 +#define APCIE_PORT_LINKSTS_UP BIT(0) +#define APCIE_PORT_APPCLK 0x800 +#define APCIE_PORT_APPCLK_EN BIT(0) +#define APCIE_PORT_APPCLK_CGDIS BIT(8) +#define APCIE_PORT_PERST 0x82c +#define APCIE_PORT_PHY_LINK BIT(31) +#define APCIE_PORT_PHY_EN0 BIT(9) +#define APCIE_PORT_PHY_EN1 BIT(10) +#define APCIE_ENGINE_CTRL 0x0 +#define APCIE_ENGINE_IRQ_MASK 0x8 +#define APCIE_ENGINE_STOP BIT(0) +#define APCIE_FABRIC_STOP 0x4 +#define APCIE_PORT_STOP_US 5000 +#define APCIE_RC_ZERO 0x04 +#define APCIE_RC_READY_START 0x50 +#define APCIE_RC_READY_CFG 0x54 +#define APCIE_RC_READY_STAT 0x58 +#define APCIE_POLL_US 50000 + +/* + * Written with bits 0 and 63 by every core right before its last WFI. This is not + * CYC_OVRD, which is undefined on this SoC. + */ +#define SYS_APL_DEEP_SLEEP sys_reg(3, 5, 15, 6, 2) +#define DEEP_SLEEP_ENABLE (BIT_ULL(0) | BIT_ULL(63)) + +#define SYS_APL_IPI_SR_EL1 sys_reg(3, 5, 15, 1, 1) +#define IPI_SR_PENDING BIT(0) +#define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) + /* * A SoC whose sleep doesn't fit the lists of struct apple_sleep_soc brings its own sequences. * @probe then runs in place of the driver's own, and installs the part's suspend operations. diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index e4a27a0c3cdac..c17ba56cd3ba7 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -86,11 +86,6 @@ #undef MODULE_PARAM_PREFIX #define MODULE_PARAM_PREFIX "t6040_sleep." -#define PS_TARGET GENMASK(3, 0) -#define PS_ACTUAL GENMASK(7, 4) -#define PS_FLAGS GENMASK(9, 8) -#define PS_AUTO_ENABLE BIT(28) -#define PS_OFF_TIMEOUT_US 5000 #define PS_STRIDE 8 #define PS_MAX_PER_GROUP 16 #define PS_SIO_FIRST 0x150 /* SOC-East: I2C0, the first SIO peripheral word */ @@ -103,8 +98,8 @@ * offsets, same bank as ps_ans); the control windows (axi2af/cio3pll/clkgen/ * phy-block/phy-ip/rc) and the PLL/lane tunables are NOT in this SoC's static * description and are published by the boot object (aura) under the apple,apcie - * node, in the same shape as on T8140. The block-internal offsets below are T8140's: - * the same Apple PCIe IP, expected identical. + * node, in the same shape as on T8140. The block-internal offsets are the T8140's + * (sleep-internal.h): the same Apple PCIe IP. */ #define PS_ANS 0x370 /* SOC-East: ps_ans */ #define PS_APCIE_ST0 0x108 /* SOC-East: ps_apcie_st0 */ @@ -114,43 +109,6 @@ #define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ #define PS_BUSY BIT(11) /* a request is in progress */ -#define APCIE_AXI_EN0 0x104 -#define APCIE_AXI_EN1 0x108 -#define APCIE_CLK_CTRL 0x0 -#define APCIE_CLK_BUSY BIT(31) -#define APCIE_CLK_OFF BIT(1) -#define APCIE_CLK_OFF2 BIT(2) -#define APCIE_CLK_START 0x4 -#define APCIE_CLK_START_GO BIT(31) -#define APCIE_PHY_COMMON 0x4000 -#define APCIE_PHY_CTRL 0x8000 -#define APCIE_PHY_CTRL_CLK0REQ BIT(0) -#define APCIE_PHY_CTRL_CLK1REQ BIT(1) -#define APCIE_PHY_CTRL_CLK0ACK BIT(2) -#define APCIE_PHY_CTRL_CLK1ACK BIT(3) -#define APCIE_PHY_CTRL_RESET BIT(4) -#define APCIE_PHY_CTRL_UP BIT(27) -#define APCIE_PHY_RUN 0x8004 -#define APCIE_PHY_RUN_EN BIT(0) -#define APCIE_PHY_RUN_EN2 BIT(4) -#define APCIE_PHY_READY 0x8008 -#define APCIE_PHY_IP_PLL_CTL 0x90 -#define APCIE_PHY_IP_PLL_HOLD BIT(16) -#define APCIE_RC_ZERO 0x04 -#define APCIE_RC_READY_START 0x50 -#define APCIE_RC_READY_CFG 0x54 -#define APCIE_RC_READY_STAT 0x58 -#define APCIE_POLL_US 50000 - -/* - * CPU start block (misc-cores-offset 0x88000, the offset aura uses for - * T6031). cpu-start = pmgr reg0 (0x502800000) + - * 0x88000 = 0x502888000. Register semantics are the same as T8140's. - */ -#define CPU_START_STOP 0x0 -#define CPU_START_SYS 0x4 -#define CPU_START_CLUSTER(n) (0x8 + (n) * 4) - /* * cluster-ctl (pmgr reg0 + cluster-ctl-offset 0x20000 = 0x502820000). Measured * on the J616s: the three CPU complexes are at E +0x0, P0 +0x4, @@ -300,24 +258,17 @@ static const struct { #define AGX_TIMEOUT_US 10000 /* - * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000). Same shape as - * T8140's t8140_pmu_write: the reply vector is masked, the command and its data - * are written to +0x4, the reply polled and acknowledged. Measured on the J616s - *: the boot stage is set to 0x50 by an extended write whose command - * word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 - * on both controllers' +0x40. - */ -#define SPMI_STATUS 0x00 -#define SPMI_STATUS_RX_EMPTY BIT(24) -#define SPMI_CMD 0x04 -#define SPMI_REPLY 0x08 -#define SPMI_NOTIFY_ENABLE 0x40 -#define SPMI_NOTIFY_ACK 0x80 -#define SPMI_NOTIFY_REPLY BIT(0) -#define SPMI_CMD_WRITE (BIT(15) | (0xe << 8) | 0x30) /* low 16 bits 0x8e30 */ -#define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 + * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000), written as on the T8140. + * Measured on the J616s: the boot stage is set to 0x50 by an extended write whose + * command word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 on + * both controllers' +0x40. + */ +/* A PMU register write on the sleep's SPMI bus, and the boot stage that announces a sleep */ +#define SPMI_CMD_WRITE (BIT(15) | (0xe << 8) | 0x30) #define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ #define PMU_BOOT_STAGE_SLEEP 0x50 +/* The response vector's enable for the sleep */ +#define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 /* * MasterMini (NUB_MPMGR_AON, RegMap 4 = pmgr reg[0x37] = 0x508900000). @@ -382,38 +333,12 @@ static const struct { #define T6040_AXI2AF_DOMAINS 15 /* - * The AON watchdog's WD2, the sleep safety net. ADT wdt reg[0] = 0x50836c000. - * Offsets and the 24 MHz tick follow the platform (its AON watchdog for a reconfig) - * and the ADT (simple-reconfig-wdog-icc-time = 5). Linux's own - * watchdog driver runs WD1 (+0x10..+0x1c), not this one. + * The AON watchdog's WD2, the sleep safety net: ADT wdt reg[0] = 0x50836c000, with the + * T8140's registers. Linux's own watchdog driver runs WD1 (+0x10..+0x1c), not this one. + * ALARM is 32 bits at 24 MHz. */ -#define WDT_WD0_CTL 0x0c -#define WDT_WD2_COUNT 0x20 -#define WDT_WD2_ALARM 0x28 -#define WDT_WD2_CTL 0x2c -#define WDT_TICKS_PER_S 24000000 -/* From .begin to .end: the device suspend and resume, as on T8140. */ -#define WDT_SUSPEND_SECS 120 -/* ALARM is 32 bits at 24 MHz. */ #define WDT_MAX_SECS 178U -/* ASC CPU control, the same in every Apple coprocessor wrapper. */ -#define ASC_CPU_CONTROL 0x44 -#define ASC_CPU_CONTROL_RUN BIT(4) -#define ASC_CPU_STATUS 0x48 - -/* - * The deep-sleep IMPDEF sysreg. The platform on T6040 prepares a core for - * sleep with the same register and bits as - * T8140. Written by every core right before its last WFI; only a reset clears - * it. - */ -#define SYS_APL_DEEP_SLEEP sys_reg(3, 5, 15, 6, 2) -#define DEEP_SLEEP_ENABLE (BIT_ULL(0) | BIT_ULL(63)) -#define SYS_APL_IPI_SR_EL1 sys_reg(3, 5, 15, 1, 1) -#define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) -#define IPI_SR_PENDING BIT(0) - struct t6040_sleep_group { const char *name; struct regmap *regmap; @@ -2627,7 +2552,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) return -EBUSY; } -#define PS_DEV_DISABLE BIT(10) #define PS_RESET_REQ BIT(12) #define DISP_CPU_GATE_US 50000 diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c7dfb69a644ed..d7632635a323e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -81,24 +81,9 @@ #include "sleep-internal.h" -/* PS word */ -#define PS_AUTO_ENABLE BIT(28) -#define PS_UNLATCH BIT(12) -#define PS_DEV_DISABLE BIT(10) -#define PS_FLAGS GENMASK(9, 8) -#define PS_ACTUAL GENMASK(7, 4) -#define PS_TARGET GENMASK(3, 0) - /* Clock protection, per slice: its control word, and the bit that guards the slice's range. */ #define PS_PROT_SLICE(n) (0x100 + 0x40 * (n)) #define PS_PROT_SLICE_ON BIT(4) -#define PS_OFF_TIMEOUT_US 5000 -#define PS_BUSY_SETTLE_US 1000 - -/* CPU start block, as aura's smp_start_cpu() uses it. A core's bit is the SoC's. */ -#define CPU_START_STOP 0x0 -#define CPU_START_SYS 0x4 -#define CPU_START_CLUSTER(n) (0x8 + (n) * 4) /* Power-gate page: reads back this value already. Purpose unknown. */ #define PWRGATE_ON 0x80000000 @@ -114,41 +99,12 @@ #define CLUSTER_TIMEOUT_US 5000 -/* Watchdog block, ADT /arm-io/wdt reg[0]. WD2 is the sleep safety net. */ -#define WDT_WD0_COUNT 0x00 -#define WDT_WD0_CTL 0x0c -#define WDT_WD1_COUNT 0x10 -#define WDT_WD2_COUNT 0x20 -#define WDT_WD2_ALARM 0x28 -#define WDT_WD2_CTL 0x2c -#define WDT_TICKS_PER_S 24000000 -/* The sleep's own safety net: ADT /arm-io/wdt simple-reconfig-wdog-icc-time. */ -#define WDT_SLEEP_SECS 5 -/* Device suspend and resume can take a while, and nothing else recovers a hang in them. */ -#define WDT_SUSPEND_SECS 120 - #define AON_AP_DEADLINE 0x08 /* Wake sources: a mask, so 0 enables. Status words are sticky. Where and how many: the SoC. */ #define WAKE_WORDS 9 #define WAKE_SOURCES_MAX 32 -/* The AP queue of the PMU's SPMI controller, and its response vector's enable and ack */ -#define SPMI_STATUS 0x00 -#define SPMI_STATUS_RX_EMPTY BIT(24) -#define SPMI_CMD 0x04 -#define SPMI_REPLY 0x08 -#define SPMI_NOTIFY_ENABLE 0x40 -#define SPMI_NOTIFY_REPLY BIT(0) -#define SPMI_NOTIFY_ACK 0x80 -#define SPMI_CMD_ACTIVE BIT(15) -#define SPMI_OP_EXT_WRITE(n) ((n) - 1) -#define SPMI_OP_EXT_READ(n) (0x20 | ((n) - 1)) -#define SPMI_OP_ZERO_WRITE 0x80 -#define SPMI_OP_SLEEP 0x11 -#define SPMI_OP_WAKEUP 0x13 -#define SPMI_POLL_US 20000 - /* A port controller on SPMI: registers go through a page select, then a data window. */ #define HPM_REG_SELECT 0x00 #define HPM_REG_DATA 0x20 @@ -225,76 +181,9 @@ #define AMCC_WORD0_DSID_AGE_OUT BIT(25) #define AMCC_WORD1_UNEXP_RT_HIT BIT(8) -/* A coprocessor's ASC */ -#define ASC_CPU_CONTROL 0x44 -#define ASC_CPU_CONTROL_RUN BIT(4) -#define ASC_CPU_STATUS 0x48 - -/* The part of the PCIe block that ANS shares, whether or not its host is bound. */ -#define APCIE_AXI_EN0 0x104 -#define APCIE_AXI_EN1 0x108 -#define APCIE_CLK_CTRL 0x0 -#define APCIE_CLK_BUSY BIT(31) -#define APCIE_CLK_RUN BIT(5) -#define APCIE_CLK_OFF BIT(1) -#define APCIE_CLK_OFF2 BIT(2) -#define APCIE_CLK_START 0x4 -#define APCIE_CLK_START_GO BIT(31) -#define APCIE_PHY_COMMON 0x4000 -#define APCIE_PHY_CTRL 0x8000 -#define APCIE_PHY_CTRL_CLK0REQ BIT(0) -#define APCIE_PHY_CTRL_CLK1REQ BIT(1) -#define APCIE_PHY_CTRL_CLK0ACK BIT(2) -#define APCIE_PHY_CTRL_CLK1ACK BIT(3) -#define APCIE_PHY_CTRL_RESET BIT(4) -#define APCIE_PHY_CTRL_UP BIT(27) -#define APCIE_PHY_RUN 0x8004 -#define APCIE_PHY_RUN_EN BIT(0) -#define APCIE_PHY_RUN_EN2 BIT(4) -#define APCIE_PHY_READY 0x8008 -#define APCIE_PHY_IP_PLL_CTL 0x90 -#define APCIE_PHY_IP_PLL_HOLD BIT(16) -#define APCIE_PORT_INTSTAT 0x100 -#define APCIE_PORT_INTMSK 0x104 -#define APCIE_PORT_INT_LINK GENMASK(7, 4) -#define APCIE_PORT_L2 0x88 -#define APCIE_PORT_L2_ENTER 0x11 -#define APCIE_PORT_L2_BUSY BIT(0) -#define APCIE_PORT_PERST_GUARD 0x13c -#define APCIE_PORT_LINKSTS 0x208 -#define APCIE_PORT_LINKSTS_UP BIT(0) -#define APCIE_PORT_APPCLK 0x800 -#define APCIE_PORT_APPCLK_EN BIT(0) -#define APCIE_PORT_APPCLK_CGDIS BIT(8) -#define APCIE_PORT_PERST 0x82c -#define APCIE_PORT_PHY_LINK BIT(31) -#define APCIE_PORT_PHY_EN0 BIT(9) -#define APCIE_PORT_PHY_EN1 BIT(10) -#define APCIE_ENGINE_CTRL 0x0 -#define APCIE_ENGINE_IRQ_MASK 0x8 -#define APCIE_ENGINE_STOP BIT(0) -#define APCIE_FABRIC_STOP 0x4 -#define APCIE_PORT_STOP_US 5000 -#define APCIE_RC_ZERO 0x04 -#define APCIE_RC_READY_START 0x50 -#define APCIE_RC_READY_CFG 0x54 -#define APCIE_RC_READY_STAT 0x58 -#define APCIE_POLL_US 50000 - /* The coprocessor and enclave registry is shared with the other SoCs' controllers. */ #define SLEEP_COPROCS APPLE_SLEEP_COPROCS -/* - * Written with bits 0 and 63 by every core right before its last WFI, measured - * from macOS. This is not CYC_OVRD, which is undefined on this SoC. - */ -#define SYS_APL_DEEP_SLEEP sys_reg(3, 5, 15, 6, 2) -#define DEEP_SLEEP_ENABLE (BIT_ULL(0) | BIT_ULL(63)) - -#define SYS_APL_IPI_SR_EL1 sys_reg(3, 5, 15, 1, 1) -#define IPI_SR_PENDING BIT(0) -#define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) - #define SLEEP_DISPLAYS_MAX 3 /* CPU clusters: 0 is E, 1 is P, for the cpm-dvfm windows */ From a7a6b5f7480c3ed1a4c6ee98b54848763a365251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:06:26 +0100 Subject: [PATCH 089/269] soc: apple: sleep: write the PMU from one function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part had a copy of the generic driver's PMU register write over the SPMI controller's AP queue, with a test for the mapping in front. Move the write to the private header, on the controller's base and with the PMU's USID as an argument, and call it from both. The device accesses, their order and the reply poll are the same in both. The compiler now keeps the controller's base in a register where it used to load it again from the driver's state after each access, so the two commit functions are a few loads shorter. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 22 ++++++++++++++++++++++ drivers/soc/apple/sleep-t6040.c | 25 +++++-------------------- drivers/soc/apple/sleep.c | 17 +---------------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index a659b273ac504..eb2ada3128cd1 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -9,6 +9,8 @@ #define _APPLE_SLEEP_INTERNAL_H_ #include +#include +#include #include #include @@ -191,6 +193,26 @@ struct aura_s2r_mailbox { #define IPI_SR_PENDING BIT(0) #define SYS_APL_SIQ_CFG_EL1 sys_reg(3, 4, 15, 10, 4) +/* A one-byte register write on the sleep's SPMI bus, to the PMU's USID */ +#define SPMI_CMD_WRITE (BIT(15) | 0x30) +#define SPMI_CMD_USID GENMASK(11, 8) + +/* A PMU write: the reply's vector masked, then acknowledged, so none is left raised. */ +static inline void apple_sleep_pmu_write(void __iomem *spmi, u8 usid, u16 reg, u8 val) +{ + u32 status, enable; + + enable = readl(spmi + SPMI_NOTIFY_ENABLE); + writel(enable & ~SPMI_NOTIFY_REPLY, spmi + SPMI_NOTIFY_ENABLE); + writel(((u32)reg << 16) | SPMI_CMD_WRITE | FIELD_PREP(SPMI_CMD_USID, usid), spmi + SPMI_CMD); + writel(val, spmi + SPMI_CMD); + if (!readl_poll_timeout_atomic(spmi + SPMI_STATUS, status, + !(status & SPMI_STATUS_RX_EMPTY), 1, 1000)) + readl(spmi + SPMI_REPLY); + writel(1, spmi + SPMI_NOTIFY_ACK); + writel(enable, spmi + SPMI_NOTIFY_ENABLE); +} + /* * A SoC whose sleep doesn't fit the lists of struct apple_sleep_soc brings its own sequences. * @probe then runs in place of the driver's own, and installs the part's suspend operations. diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index c17ba56cd3ba7..482922a7eba19 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -263,8 +263,8 @@ static const struct { * command word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 on * both controllers' +0x40. */ -/* A PMU register write on the sleep's SPMI bus, and the boot stage that announces a sleep */ -#define SPMI_CMD_WRITE (BIT(15) | (0xe << 8) | 0x30) +/* The PMU's USID on the sleep's SPMI bus, and the boot stage that announces a sleep */ +#define PMU_USID 0xe #define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ #define PMU_BOOT_STAGE_SLEEP 0x50 /* The response vector's enable for the sleep */ @@ -1174,26 +1174,11 @@ static const struct t6040_axi2af_domain { { "apcie_sys_gp", true, 0x390, 0x416000000 }, }; -/* - * As the platform writes the PMU over SPMI (mirror of T8140's t8140_pmu_write): the - * reply vector masked, the command and value written to +0x4, the reply polled - * and acknowledged, so none is left raised. - */ +/* Only with the PMU's bus mapped. */ static void t6040_pmu_write(struct t6040_sleep *s, u16 reg, u8 val) { - u32 status, enable; - - if (!s->spmi0) - return; - enable = readl(s->spmi0 + SPMI_NOTIFY_ENABLE); - writel(enable & ~SPMI_NOTIFY_REPLY, s->spmi0 + SPMI_NOTIFY_ENABLE); - writel(((u32)reg << 16) | SPMI_CMD_WRITE, s->spmi0 + SPMI_CMD); - writel(val, s->spmi0 + SPMI_CMD); - if (!readl_poll_timeout_atomic(s->spmi0 + SPMI_STATUS, status, - !(status & SPMI_STATUS_RX_EMPTY), 1, 1000)) - readl(s->spmi0 + SPMI_REPLY); - writel(1, s->spmi0 + SPMI_NOTIFY_ACK); - writel(enable, s->spmi0 + SPMI_NOTIFY_ENABLE); + if (s->spmi0) + apple_sleep_pmu_write(s->spmi0, PMU_USID, reg, val); } /* diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index d7632635a323e..d4cd82656dd40 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -125,9 +125,6 @@ #define HPM_CONTRACT_MS 1500 #define HPM_RESET_TRIES 3 -/* A one-byte register write on the sleep's SPMI bus, to the PMU's USID */ -#define SPMI_CMD_WRITE (BIT(15) | 0x30) -#define SPMI_CMD_USID GENMASK(11, 8) /* T8132's power words where they aren't T8140's */ #define T8132_PS_ATC0_COMMON 0x378 @@ -2400,21 +2397,9 @@ static void sleep_ps_rewake_report(struct apple_sleep *s) #define T8132_PS_AON_ON_STATE 4 -/* As macOS writes the PMU: the reply's vector masked, then acknowledged, so none is left raised. */ static void sleep_pmu_write(struct apple_sleep *s, u16 reg, u8 val) { - u32 status, enable; - - enable = readl(s->spmi + SPMI_NOTIFY_ENABLE); - writel(enable & ~SPMI_NOTIFY_REPLY, s->spmi + SPMI_NOTIFY_ENABLE); - writel(((u32)reg << 16) | SPMI_CMD_WRITE | - FIELD_PREP(SPMI_CMD_USID, s->soc->pmu_usid), s->spmi + SPMI_CMD); - writel(val, s->spmi + SPMI_CMD); - if (!readl_poll_timeout_atomic(s->spmi + SPMI_STATUS, status, - !(status & SPMI_STATUS_RX_EMPTY), 1, 1000)) - readl(s->spmi + SPMI_REPLY); - writel(1, s->spmi + SPMI_NOTIFY_ACK); - writel(enable, s->spmi + SPMI_NOTIFY_ENABLE); + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, reg, val); } /* From 9ea9b5aecb5ef4744ba6ec44d8fe0e3d4e172a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:07:40 +0100 Subject: [PATCH 090/269] soc: apple: sleep: map the storage PCIe windows with one pair of helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part had copies of the two helpers that take a window and its tunable table from the node a loader fills for the storage PCIe block. Let the generic driver's pair take the device instead of its own state, declare it in the private header and use it from the part. Both run at probe only. The windows, the tunable properties and the order they are taken in are the same for the T8140 and for the T6040. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 13 +++++++ drivers/soc/apple/sleep-t6040.c | 54 +++++++----------------------- drivers/soc/apple/sleep.c | 37 ++++++++++---------- 3 files changed, 44 insertions(+), 60 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index eb2ada3128cd1..fe4e7d40b9e18 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -16,7 +16,9 @@ #include +struct apple_tunable; struct device; +struct device_node; struct platform_device; struct seq_file; @@ -235,6 +237,17 @@ extern const struct apple_sleep_part t6040_sleep_part; */ void apple_sleep_set_dry(unsigned int level); +/* + * The storage PCIe block's windows and tunables are on a node a loader fills. A tunable table + * @prop for the window @name, or NULL; and the window itself mapped, with its tunable if @prop + * names one, or NULL where either is missing. + */ +struct apple_tunable *apple_sleep_apcie_tunable(struct device *dev, struct device_node *np, + const char *name, const char *prop); +void __iomem *apple_sleep_apcie_block(struct device *dev, struct device_node *np, + const char *name, const char *prop, + struct apple_tunable **tunable); + /* * A part whose sleep takes neither the lid's state at the wake nor the USB wake ports says so * at its probe. apple_sleep_lid_closed_at_wake() and apple_sleep_usb_port_can_wake() then diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 482922a7eba19..9c1678c6579e9 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -3010,39 +3010,6 @@ static void __iomem *t6040_sleep_map(struct device *dev, const char *name) return t6040_ioremap_np(dev, res.start, resource_size(&res)); } -/* A tunable table on the apple,apcie node, named by its matching reg-name. */ -static struct apple_tunable *t6040_apcie_tunable(struct t6040_sleep *s, struct device_node *np, - const char *name, const char *prop) -{ - int idx = of_property_match_string(np, "reg-names", name); - struct apple_tunable *t; - struct resource res; - - if (idx < 0 || of_address_to_resource(np, idx, &res)) - return NULL; - t = devm_apple_tunable_parse(s->dev, np, prop, &res); - - return IS_ERR(t) ? NULL : t; -} - -/* A control window on the apple,apcie node, with its optional tunable. */ -static void __iomem *t6040_apcie_block(struct t6040_sleep *s, struct device_node *np, - const char *name, const char *prop, - struct apple_tunable **tunable) -{ - int idx = of_property_match_string(np, "reg-names", name); - - if (idx < 0) - return NULL; - if (prop) { - *tunable = t6040_apcie_tunable(s, np, name, prop); - if (!*tunable) - return NULL; - } - - return of_iomap(np, idx); -} - /* * ANS's storage-apcie windows + PLL/lane tunables, from the apple,apcie node * the boot object publishes from the ADT. Our static tree has none @@ -3059,14 +3026,19 @@ static void t6040_sleep_map_apcie(struct t6040_sleep *s) if (!np) return; - a->axi = t6040_apcie_block(s, np, "axi2af", "apple,tunable-axi2af", &a->axi_tunable); - a->cio3pll = t6040_apcie_block(s, np, "cio3pll", "apple,tunable-cio3pll", - &a->cio3pll_tunable); - a->clkgen = t6040_apcie_block(s, np, "clkgen", "apple,tunable-clkgen", &a->clkgen_tunable); - a->phy = t6040_apcie_block(s, np, "phy-block", "apple,tunable-phy", &a->phy_tunable); - a->rc = t6040_apcie_block(s, np, "rc", NULL, NULL); - a->phy_ip = t6040_apcie_block(s, np, "phy-ip", "apple,tunable-phy-ip-pll", &a->phy_ip_pll); - a->phy_ip_lanes = t6040_apcie_tunable(s, np, "phy-ip", "apple,tunable-phy-ip-auspma"); + a->axi = apple_sleep_apcie_block(s->dev, np, "axi2af", "apple,tunable-axi2af", + &a->axi_tunable); + a->cio3pll = apple_sleep_apcie_block(s->dev, np, "cio3pll", "apple,tunable-cio3pll", + &a->cio3pll_tunable); + a->clkgen = apple_sleep_apcie_block(s->dev, np, "clkgen", "apple,tunable-clkgen", + &a->clkgen_tunable); + a->phy = apple_sleep_apcie_block(s->dev, np, "phy-block", "apple,tunable-phy", + &a->phy_tunable); + a->rc = apple_sleep_apcie_block(s->dev, np, "rc", NULL, NULL); + a->phy_ip = apple_sleep_apcie_block(s->dev, np, "phy-ip", "apple,tunable-phy-ip-pll", + &a->phy_ip_pll); + a->phy_ip_lanes = apple_sleep_apcie_tunable(s->dev, np, "phy-ip", + "apple,tunable-phy-ip-auspma"); of_node_put(np); a->valid = a->axi && a->cio3pll && a->clkgen && a->phy && a->rc && a->phy_ip && diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index d4cd82656dd40..c6b00bc27f1b6 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -3688,9 +3688,8 @@ static int apple_sleep_map_mailbox(struct apple_sleep *s) return 0; } -static struct apple_tunable *apple_sleep_apcie_tunable(struct apple_sleep *s, - struct device_node *np, - const char *name, const char *prop) +struct apple_tunable *apple_sleep_apcie_tunable(struct device *dev, struct device_node *np, + const char *name, const char *prop) { int idx = of_property_match_string(np, "reg-names", name); struct apple_tunable *t; @@ -3698,21 +3697,21 @@ static struct apple_tunable *apple_sleep_apcie_tunable(struct apple_sleep *s, if (idx < 0 || of_address_to_resource(np, idx, &res)) return NULL; - t = devm_apple_tunable_parse(s->dev, np, prop, &res); + t = devm_apple_tunable_parse(dev, np, prop, &res); return IS_ERR(t) ? NULL : t; } -static void __iomem *apple_sleep_apcie_block(struct apple_sleep *s, struct device_node *np, - const char *name, const char *prop, - struct apple_tunable **tunable) +void __iomem *apple_sleep_apcie_block(struct device *dev, struct device_node *np, + const char *name, const char *prop, + struct apple_tunable **tunable) { int idx = of_property_match_string(np, "reg-names", name); if (idx < 0) return NULL; if (prop) { - *tunable = apple_sleep_apcie_tunable(s, np, name, prop); + *tunable = apple_sleep_apcie_tunable(dev, np, name, prop); if (!*tunable) return NULL; } @@ -3731,18 +3730,18 @@ static void apple_sleep_map_apcie(struct apple_sleep *s) if (!np) return; - s->apcie_axi = apple_sleep_apcie_block(s, np, "axi2af", "apple,tunable-axi2af", + s->apcie_axi = apple_sleep_apcie_block(s->dev, np, "axi2af", "apple,tunable-axi2af", &s->apcie_axi_tunable); - s->apcie_cio3pll = apple_sleep_apcie_block(s, np, "cio3pll", "apple,tunable-cio3pll", + s->apcie_cio3pll = apple_sleep_apcie_block(s->dev, np, "cio3pll", "apple,tunable-cio3pll", &s->apcie_cio3pll_tunable); - s->apcie_clkgen = apple_sleep_apcie_block(s, np, "clkgen", "apple,tunable-clkgen", + s->apcie_clkgen = apple_sleep_apcie_block(s->dev, np, "clkgen", "apple,tunable-clkgen", &s->apcie_clkgen_tunable); - s->apcie_phy = apple_sleep_apcie_block(s, np, "phy-block", "apple,tunable-phy", + s->apcie_phy = apple_sleep_apcie_block(s->dev, np, "phy-block", "apple,tunable-phy", &s->apcie_phy_tunable); - s->apcie_rc = apple_sleep_apcie_block(s, np, "rc", NULL, NULL); - s->apcie_phy_ip = apple_sleep_apcie_block(s, np, "phy-ip", "apple,tunable-phy-ip-pll", + s->apcie_rc = apple_sleep_apcie_block(s->dev, np, "rc", NULL, NULL); + s->apcie_phy_ip = apple_sleep_apcie_block(s->dev, np, "phy-ip", "apple,tunable-phy-ip-pll", &s->apcie_phy_ip_pll); - s->apcie_phy_ip_lanes = apple_sleep_apcie_tunable(s, np, "phy-ip", + s->apcie_phy_ip_lanes = apple_sleep_apcie_tunable(s->dev, np, "phy-ip", "apple,tunable-phy-ip-auspma"); if (!s->apcie_axi || !s->apcie_cio3pll || !s->apcie_clkgen || !s->apcie_phy || !s->apcie_rc || !s->apcie_phy_ip || !s->apcie_phy_ip_lanes) { @@ -3754,12 +3753,12 @@ static void apple_sleep_map_apcie(struct apple_sleep *s) s->apcie_axi = NULL; } - s->apcie_port = apple_sleep_apcie_block(s, np, "port0", NULL, NULL); - s->apcie_port_phy = apple_sleep_apcie_block(s, np, "phy0", NULL, NULL); + s->apcie_port = apple_sleep_apcie_block(s->dev, np, "port0", NULL, NULL); + s->apcie_port_phy = apple_sleep_apcie_block(s->dev, np, "phy0", NULL, NULL); engine = of_parse_phandle(np, "apple,piodma", 0); if (engine) { - s->apcie_engine = apple_sleep_apcie_block(s, engine, "engine", NULL, NULL); - s->apcie_fabric = apple_sleep_apcie_block(s, engine, "fabric", NULL, NULL); + s->apcie_engine = apple_sleep_apcie_block(s->dev, engine, "engine", NULL, NULL); + s->apcie_fabric = apple_sleep_apcie_block(s->dev, engine, "fabric", NULL, NULL); of_node_put(engine); } of_node_put(np); From 8ba3163a0b7054d7c14adc5f7be92e502fa5e3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 09:11:07 +0100 Subject: [PATCH 091/269] soc: apple: sleep: describe the T6040 part as it is MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The file's opening comment still explained why the T6040 controller was a driver of its own next to the generic one, and several comments named functions the generic driver no longer has. Describe the part as it is now, with suspend-to-RAM offered only to a system booted with t6040_sleep.allow_entry=1, name the generic driver's functions, and say in sleep.c that a SoC may bring a part. List the private header next to the driver's sources. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 62 ++++++++++++++++----------------- drivers/soc/apple/sleep.c | 4 +++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 9c1678c6579e9..1f330524c6d2e 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -1,25 +1,28 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* - * Apple T6040 (M4 Pro) system sleep: refusal-first controller. + * Apple SoC system sleep: the T6040 (M4 Pro) part * - * This is the T6040 port of the T8140 deep sleep at EL2 (sleep.c). Its commit - * EL2). Its commit follows the T6040 entry as measured on the J616s, - * with what the SoC's sleep engine needs from an OS that is not the platform (see - * t6040_sleep_commit); its wake return mirrors the PMGR wake restore (see - * t6040_sleep_return). + * The T6040 sleeps through the same boot object and mailbox as the SoCs sleep.c takes from + * its lists, and nearly every step in between differs: three CPU complexes, five display + * pipes whose power states have to read off first, two storage PCIe lanes, the wake block + * and the order of the commit. So this file is a part of that driver (sleep-internal.h) with + * its own probe, state and suspend operations. The commit follows the T6040 entry as + * measured on the J616s, with what the SoC's sleep engine needs from the OS (see + * t6040_sleep_commit); the wake return mirrors the PMGR wake restore + * (see t6040_sleep_return). The registry of coprocessors and of the enclave + * (linux/soc/apple/sleep.h), the dry run level and the mailbox layout are the generic + * driver's. * - * What it does: + * It refuses first: * - * - It keeps the debugfs preflight of the display power states the platform checks - * before it sleeps (device-check-ps-b4-sleep). - * - It uses the coprocessor and enclave registry it shares with T8140 (sleep.h): - * a driver that can only stop its coprocessor once the sleep is certain - * (ANS, the PMP, the SEP's sleep request) registers here, readies it in its - * own suspend and leaves the last stop to the commit, after every refusal. - * A refused sleep then finds it running. - * - Its suspend_ops .begin computes the blocker list. By default a - * `echo mem > /sys/power/state` prints every blocker and returns an error - * before any device suspends. + * - It binds below EL2 and without the boot object's mailbox as well, and shows both as + * blockers in debugfs t6040-sleep/preflight, next to the CPUs that can park and the + * display power states the ADT lists to check before a sleep (device-check-ps-b4-sleep). + * - It offers suspend-to-RAM only to a system booted with t6040_sleep.allow_entry=1. + * Without it suspend-to-idle stays the default sleep, and it passes through here + * untouched either way. + * - Its suspend_ops .begin computes the blocker list of a suspend-to-RAM. With a blocker + * left it prints every one and returns an error before any device suspends. * - Dry runs (t6040_sleep.dry_run, also writable at runtime): * 1: pm_suspend_via_firmware() is set, so every driver's firmware-driven * sleep step that can be taken back runs (apple_sleep_power_loss() is @@ -36,12 +39,8 @@ * watchdog's WD2 is its net (t6040_sleep.sleep_wd_secs), and over the device * suspend and resume around it. * - * Why this file and not a generalisation of sleep.c: the port keeps - * the T8140 behaviour byte-identical, and the T6040 commit and wake differ - * from it in most steps, so it is a controller of its own. The coprocessor and - * enclave registry is shared - * (sleep-registry.c), so one kernel carries both controllers, and each binds - * only to its own SoC. + * The lid's state at the wake and the USB wake ports, which the generic driver keeps for the + * display and the port controllers, are declined: this sleep was tested without them. */ #include @@ -98,7 +97,7 @@ * offsets, same bank as ps_ans); the control windows (axi2af/cio3pll/clkgen/ * phy-block/phy-ip/rc) and the PLL/lane tunables are NOT in this SoC's static * description and are published by the boot object (aura) under the apple,apcie - * node, in the same shape as on T8140. The block-internal offsets are the T8140's + * node, as on the T8140. The block-internal offsets are the T8140's * (sleep-internal.h): the same Apple PCIe IP. */ #define PS_ANS 0x370 /* SOC-East: ps_ans */ @@ -965,8 +964,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) /* * ANS would be stopped, but its ST lanes have no way back without the - * apple,apcie control windows + PLL/lane tunables. The T8140 driver keeps - * suspend-to-idle the default in this state; here the deep sleep refuses. + * apple,apcie control windows + PLL/lane tunables: refused. */ if (ans_idled && !s->apcie_st.valid) { pr_err("t6040-sleep: refused: ANS would stop but the storage-apcie windows/tunables are absent (apple,apcie)\n"); @@ -1465,7 +1463,7 @@ static void t6040_wake_restore(struct t6040_sleep *s) } /* - * One power word raised, as T8140's t8140_ps_raise() does: target 0xf with automatic + * One power word raised, as sleep.c's sleep_ps_raise() does: target 0xf with automatic * mode off and the flag bits cleared, ACTUAL polled, then AUTO_ENABLE. The two halves * are separate for a word that has to stay at a manual target for a while or for good. */ @@ -1797,7 +1795,7 @@ static void t6040_bridge_rearm(void __iomem *bridge) /* * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of - * T8140's t8140_sleep_raise_apcie(). Runs atomic on the waking core in + * sleep.c's apple_sleep_raise_apcie(). Runs atomic on the waking core in * t6040_sleep_return(), AFTER t6040_axi2af_rearm() and BEFORE control returns to * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. * Both lanes come up; the PLL/clkgen/PHY tables run once (shared). The two @@ -1869,7 +1867,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) if (ret) return ret; - /* Shared PHY bring-up + PLL/lane tables (as T8140's t8140_apcie_phy_ip_apply). */ + /* Shared PHY bring-up + PLL/lane tables (as sleep.c's sleep_apcie_phy_ip_apply()). */ apple_tunable_apply(a->phy, a->phy_tunable); ret = readl_poll_timeout_atomic(a->phy + APCIE_PHY_COMMON, val, val & BIT(31), 1, APCIE_POLL_US); @@ -2841,7 +2839,7 @@ static int t6040_sleep_enter(suspend_state_t state) return -EBUSY; } - /* The T8140 driver's order. */ + /* The refusals, the hold and the stops in the generic driver's order. */ if (t6040_sleep_refuse(s)) { t6040_disp_back(s); return -EBUSY; @@ -3015,7 +3013,7 @@ static void __iomem *t6040_sleep_map(struct device *dev, const char *name) * the boot object publishes from the ADT. Our static tree has none * of these, so until aura fills the node @valid stays false and a real sleep * with ANS stopped refuses (t6040_sleep_refuse). The reg-names and tunable - * property names match the T8140 apple,apcie shape exactly, so the same + * property names match the T8140's apple,apcie shape exactly, so the same * loader fills both SoCs. The GP PCIe host (pcie@1cb0000000, Wi-Fi + SD) is a * different node and is left to cold-recover. */ @@ -3064,7 +3062,7 @@ static struct aura_s2r_mailbox *t6040_sleep_mailbox_at(struct t6040_sleep *s, u6 return mbox; } -/* The aura #117 mailbox, if the boot object published one. Optional (a blocker if absent). */ +/* The boot object's mailbox, if it published one. Optional (a blocker if absent). */ static void t6040_sleep_map_mailbox(struct t6040_sleep *s) { struct device_node *chosen = of_find_node_by_path("/chosen"); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c6b00bc27f1b6..a96e10bd032d7 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -30,6 +30,10 @@ * T8132 takes the same path with its own tables and operations, t8132_sleep_ops, * measured the same way from a macOS sleep under aura's hypervisor. Its blocks are * T8140's at the same offsets from a SoC base 0x80000000 higher unless noted. + * + * A SoC whose sequences differ in more than its lists brings them as a part + * (sleep-internal.h), and shares the registry of coprocessors with the rest: + * today T6040, in sleep-t6040.c. */ #include From 0d10d57f36379ff193eb496f108bdfb157a86ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 10:29:46 +0100 Subject: [PATCH 092/269] soc: apple: say what the wake leaves undone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/sleep-t6040.c part of "soc: apple: sleep: t6040, drm: apple: say what the wake leaves undone", which goes with the sleep controller changes; the rest of that commit is with the display changes. Four comments still read as open work: a register of each CPU complex that could be written at the wake, the energy accumulators that could be set again, and the conditional retention of the display domains. The J616s wakes with all 14 CPUs and its displays without them. Say what could be done there, that this driver does not, and what was measured. The comment over the display power words also described a device tree without the apple,display-power node as the present state; the boot object publishes it. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 1f330524c6d2e..0ffa4cf533466 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -2258,9 +2258,10 @@ static void t6040_sleep_return(struct t6040_sleep *s) /* * The three CPU complexes back on (E, P0, P1), nibble 0xf, poll +0xc for - * the ready bit. The ACC restore then writes ACC register 0xe440f8 - * := 1 per complex; that per-complex ACC window is not mapped here (TODO) - * and is left to the normal per-CPU bring-up. + * the ready bit. The platform then also writes 1 to register 0xe440f8 of each + * complex. That is not done here: the secondaries come up through the + * normal per-CPU start afterwards, and all 14 CPUs of the J616s are + * online after a wake without it. */ if (s->cluster_ctl) for (i = 0; i < CLUSTER_CTL_NR; i++) { @@ -2292,9 +2293,9 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_wake_sync(); /* - * The SOC DPE restore (the 14 DPE energy accumulators |= 1) is skipped: - * the DPE RegMap base and its offset list are not resolved here, and it - * is energy accounting only, benign to skip. TODO. + * Bit 0 of the SoC's 14 energy accumulators could be set again here. They + * only account energy and nothing in this kernel reads them, so they are + * left as the power loss left them. */ /* The fabric-bridge retention re-arm (the ANS/Wi-Fi/display-dead guard). */ From fc292b2434bef2711b0ca8337ca7d1164047e324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 10:31:28 +0100 Subject: [PATCH 093/269] soc: apple: sleep: one copy of the power word raise, the tunable write and the WD2 arm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part still had its own copies of three helpers the T8140 sequences have as well: raising a power word (the target, then AUTO_ENABLE once it reads on), writing a tunable table only where a word changes, and arming WD2. Each pair was the same statements on a different base pointer. Keep one inline copy of each in sleep-internal.h and call it from both. No access, value or order changes on either SoC; the compiler inlines them differently into their callers than before. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 47 +++++++++++++++++++++++++++++- drivers/soc/apple/sleep-t6040.c | 32 ++++---------------- drivers/soc/apple/sleep.c | 41 ++++---------------------- 3 files changed, 56 insertions(+), 64 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index fe4e7d40b9e18..f33e0c67bac59 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -12,11 +12,11 @@ #include #include #include +#include #include #include -struct apple_tunable; struct device; struct device_node; struct platform_device; @@ -215,6 +215,51 @@ static inline void apple_sleep_pmu_write(void __iomem *spmi, u8 usid, u16 reg, u writel(enable, spmi + SPMI_NOTIFY_ENABLE); } +/* + * A domain is raised with the target, then AUTO_ENABLE once it reads on. Measured + * byte for byte on ANS and PCIe, for instance 0x0f002300, 0x0f00200f, 0x1f00200f. + */ +static inline int apple_sleep_ps_raise_start(void __iomem *reg) +{ + u32 now; + + writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET, reg); + + return readl_poll_timeout_atomic(reg, now, (now & PS_ACTUAL) == PS_ACTUAL, 1, + PS_OFF_TIMEOUT_US); +} + +static inline void apple_sleep_ps_raise_finish(void __iomem *reg) +{ + writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET | PS_AUTO_ENABLE, + reg); +} + +/* Read-modify-write in table order, with no write where nothing changes. */ +static inline void apple_sleep_tunable_apply_changed(void __iomem *regs, + const struct apple_tunable *t) +{ + size_t i; + + for (i = 0; i < t->sz; i++) { + void __iomem *reg = regs + t->values[i].offset; + u32 old = readl(reg); + u32 val = (old & ~t->values[i].mask) | (t->values[i].value & t->values[i].mask); + + if (val != old) + writel(val, reg); + } +} + +/* WD2 of the AON watchdog, the sleep's safety net, armed for @secs from now. */ +static inline void apple_sleep_wd2_arm(void __iomem *wdt, unsigned int secs) +{ + writel(0, wdt + WDT_WD2_CTL); + writel(0, wdt + WDT_WD2_COUNT); + writel(secs * WDT_TICKS_PER_S, wdt + WDT_WD2_ALARM); + writel(1, wdt + WDT_WD2_CTL); +} + /* * A SoC whose sleep doesn't fit the lists of struct apple_sleep_soc brings its own sequences. * @probe then runs in place of the driver's own, and installs the part's suspend operations. diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 0ffa4cf533466..1ccc0960b0816 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -867,10 +867,7 @@ static void t6040_sleep_set_wd2(struct t6040_sleep *s, unsigned int secs) { if (!s->wdt) return; - writel(0, s->wdt + WDT_WD2_CTL); - writel(0, s->wdt + WDT_WD2_COUNT); - writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD2_ALARM); - writel(1, s->wdt + WDT_WD2_CTL); + apple_sleep_wd2_arm(s->wdt, secs); s->wd2_armed = true; } @@ -1469,21 +1466,12 @@ static void t6040_wake_restore(struct t6040_sleep *s) */ static int t6040_ps_raise_start(void __iomem *base, u32 offset) { - void __iomem *reg = base + offset; - u32 now; - - writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET, reg); - - return readl_poll_timeout_atomic(reg, now, (now & PS_ACTUAL) == PS_ACTUAL, 1, - PS_OFF_TIMEOUT_US); + return apple_sleep_ps_raise_start(base + offset); } static void t6040_ps_raise_finish(void __iomem *base, u32 offset) { - void __iomem *reg = base + offset; - - writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET | PS_AUTO_ENABLE, - reg); + apple_sleep_ps_raise_finish(base + offset); } static int t6040_ps_raise(void __iomem *base, u32 offset) @@ -1503,18 +1491,8 @@ static bool t6040_ps_actual_on(void __iomem *base, u32 offset) /* Read-modify-write a tunable in table order, writing only where it changes. */ static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_tunable *t) { - size_t i; - - if (!t) - return; - for (i = 0; i < t->sz; i++) { - void __iomem *reg = regs + t->values[i].offset; - u32 old = readl(reg); - u32 val = (old & ~t->values[i].mask) | (t->values[i].value & t->values[i].mask); - - if (val != old) - writel(val, reg); - } + if (t) + apple_sleep_tunable_apply_changed(regs, t); } /* diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index a96e10bd032d7..dd673f0390d1b 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1055,27 +1055,14 @@ static void sleep_ps_lower(struct apple_sleep *s, u32 offset) sleep_ps_lower_finish(s, offset, sleep_ps_lower_start(s, offset)); } -/* - * As macOS raises a domain: the target, then AUTO_ENABLE once it reads on. Measured - * byte for byte on ANS and PCIe, for instance 0x0f002300, 0x0f00200f, 0x1f00200f. - */ static int sleep_ps_raise_start(struct apple_sleep *s, u32 offset) { - void __iomem *reg = s->ps + offset; - u32 now; - - writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET, reg); - - return readl_poll_timeout_atomic(reg, now, (now & PS_ACTUAL) == PS_ACTUAL, 1, - PS_OFF_TIMEOUT_US); + return apple_sleep_ps_raise_start(s->ps + offset); } static void sleep_ps_raise_finish(struct apple_sleep *s, u32 offset) { - void __iomem *reg = s->ps + offset; - - writel((readl(reg) & ~(PS_AUTO_ENABLE | GENMASK(11, 0))) | PS_TARGET | PS_AUTO_ENABLE, - reg); + apple_sleep_ps_raise_finish(s->ps + offset); } static int sleep_ps_raise(struct apple_sleep *s, u32 offset) @@ -2097,21 +2084,6 @@ static void apple_sleep_lower_apcie(struct apple_sleep *s) sleep_ps_lower(s, d->ans); } -/* Read-modify-write in table order, with no write where nothing changes. */ -static void sleep_tunable_apply_changed(void __iomem *regs, const struct apple_tunable *t) -{ - size_t i; - - for (i = 0; i < t->sz; i++) { - void __iomem *reg = regs + t->values[i].offset; - u32 old = readl(reg); - u32 val = (old & ~t->values[i].mask) | (t->values[i].value & t->values[i].mask); - - if (val != old) - writel(val, reg); - } -} - /* * The power loss takes the PHY's PLL and lane settings with it, and ANS's link can't * train without them. Applied as macOS does: the PLL table, then the lanes, then the @@ -2121,8 +2093,8 @@ static void sleep_apcie_phy_ip_apply(struct apple_sleep *s) { void __iomem *ctl = s->apcie_phy_ip + APCIE_PHY_IP_PLL_CTL; - sleep_tunable_apply_changed(s->apcie_phy_ip, s->apcie_phy_ip_pll); - sleep_tunable_apply_changed(s->apcie_phy_ip, s->apcie_phy_ip_lanes); + apple_sleep_tunable_apply_changed(s->apcie_phy_ip, s->apcie_phy_ip_pll); + apple_sleep_tunable_apply_changed(s->apcie_phy_ip, s->apcie_phy_ip_lanes); writel(readl(ctl) & ~APCIE_PHY_IP_PLL_HOLD, ctl); } @@ -2222,10 +2194,7 @@ static void apple_sleep_lower_io(struct apple_sleep *s) static void apple_sleep_set_wd2(struct apple_sleep *s, unsigned int secs) { - writel(0, s->wdt + WDT_WD2_CTL); - writel(0, s->wdt + WDT_WD2_COUNT); - writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD2_ALARM); - writel(1, s->wdt + WDT_WD2_CTL); + apple_sleep_wd2_arm(s->wdt, secs); } #define PS_PARENT_MISSING BIT(11) From d1501d0c2948b682234b4c4c9a19bec5b314ab0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 17:08:09 +0100 Subject: [PATCH 094/269] soc: apple: sleep: t6040: name the T8132 stub instead of its pull request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A comment on the wake stub still pointed at the T8132 one by a pull request number. Name the stub. No functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 1ccc0960b0816..849460c6a22e7 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -1891,7 +1891,7 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) } /* - * The first thing Linux runs after a wake, MMU off and no stack (#1314's T8132 stub). The + * The first thing Linux runs after a wake, MMU off and no stack (as the T8132 stub does). The * last wake program leaves the AON WD2 running for 5 s, less than the device resume takes: * restart it for 60 s, in t6040_sleep_set_wd2()'s order, then take cpu_resume. The PA is * the T6040 AON WDT (0x50836c000), used only when the DT's wdt block is that one. From 3ba68676e70f6e5bc4f7f465e5612cb8024886be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:02:21 +0100 Subject: [PATCH 095/269] soc: apple: sleep: t6040: map the S2R-lo counter once, at probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The S2R state printed at debug level reads the hardware S2R-lo entry counter in MiniPMGR. It was mapped and unmapped around each read, and one caller is the dry run at the commit point, which runs with interrupts off and one CPU up: ioremap() and iounmap() may sleep there. Map the counter once at probe with the other fixed windows and read it through that mapping. The registers read and their order are the same; the dry run no longer maps or unmaps anything. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 849460c6a22e7..c83d5320e6c7e 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -439,6 +439,7 @@ struct t6040_sleep { u64 ka_on_before; /* keep-alive bits set before the commit cleared them */ void __iomem *smc_wdt_deadline; void __iomem *agx_gate; + void __iomem *s2r_lo_count; void __iomem *acc_apsc[3]; void __iomem *tvm_ctl[8]; void __iomem *tvm_status[8]; @@ -1112,17 +1113,16 @@ static const u32 t6040_wake_mask[AOP_WAKE_WORDS] = { * 0x508370970 at quiesce. It tells "the SoC entered S2R" from "the * engine never finished". Read-only, with the AOP-global S2R control, wake masks and * wake status, at debug level: at probe, for the boot after a failed sleep, and in a dry run. + * Mapped once at probe: the dry run reads it with interrupts off. */ #define T6040_S2R_LO_COUNT_PA 0x508370970ULL static void t6040_sleep_log_s2r_state(struct t6040_sleep *s, const char *when) { - void __iomem *c = ioremap_np(T6040_S2R_LO_COUNT_PA, 4); - u32 raw = c ? readl(c) : 0, m[AOP_WAKE_WORDS] = {}, st[AOP_WAKE_WORDS] = {}, ctl = 0; + u32 raw = s->s2r_lo_count ? readl(s->s2r_lo_count) : 0; + u32 m[AOP_WAKE_WORDS] = {}, st[AOP_WAKE_WORDS] = {}, ctl = 0; int i; - if (c) - iounmap(c); if (s->aop_global) { ctl = readl(s->aop_global + AOP_S2R_CTRL); for (i = 0; i < AOP_WAKE_WORDS; i++) { @@ -3225,6 +3225,7 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); sleep->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); sleep->agx_gate = t6040_ioremap_np(&pdev->dev, T6040_AGX_GATE_PA, 4); + sleep->s2r_lo_count = t6040_ioremap_np(&pdev->dev, T6040_S2R_LO_COUNT_PA, 4); for (i = 0; i < ARRAY_SIZE(t6040_acc_apsc_pa); i++) sleep->acc_apsc[i] = t6040_ioremap_np(&pdev->dev, t6040_acc_apsc_pa[i], 8); for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { From 926fd87394560d45bfac8330ff1fc760be069e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:13:18 +0100 Subject: [PATCH 096/269] soc: apple: sleep: t6040: log the mailbox the blocker count uses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refusal log of .begin said whether the device tree has an s2r-mailbox property, while the blocker count asks whether a mailbox with the boot object's magic is mapped. After a bad magic, or a mailbox dropped at a hibernation restore, the log read "s2r-mailbox yes" next to the blocker it counted. Log the mapped mailbox. Its comment spoke of interrupts being off; its only caller is .begin. When the first regmap read of a display CPU word fails, the warning printed the polled value uninitialised: start it at 0. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index c83d5320e6c7e..91fe8529c85e4 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -698,14 +698,13 @@ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) return blockers; } -/* Log the full blocker list (interrupts may be off: no allocation, pr_* only). */ +/* Log the blockers outside the display, for a refused or dry .begin. */ static void t6040_sleep_log_blockers(struct t6040_sleep *sleep) { const char *ops_name; pr_info("t6040-sleep: EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", - is_kernel_in_hyp_mode() ? "yes" : "no", - of_property_present(of_chosen, "aurora,s2r-mailbox") ? "yes" : "no", + is_kernel_in_hyp_mode() ? "yes" : "no", sleep->mbox ? "yes" : "no", t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); } @@ -2770,7 +2769,7 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) for (;;) { bool cpu = off == g->first; - u32 val, cur; + u32 val, cur = 0; /* An automatically gated CPU still powered: wait for the PMGR to gate it. */ if (cpu && !regmap_read(g->regmap, off, &val) && (val & PS_AUTO_ENABLE) && From 1a08782450c7061ce3c8a3aca40ac171938fad3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:19:22 +0100 Subject: [PATCH 097/269] soc: apple: sleep: t6040: drop the tests .valid has already made MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PM core reaches .begin and .enter of a suspend to RAM only after .valid accepted the state (sleep_state_supported(), on every path into suspend_devices_and_enter()), and .valid accepts it only with allow_entry, a read-only parameter. .begin still tested allow_entry, and .enter tested it again together with apple_sleep_power_loss(), which is true on the one path that reaches it: dry run level 0 after .begin set pm_suspend_via_firmware(). Drop the three tests. Say in the comments that a dry run, a suspend to RAM as well, needs allow_entry too, so the CPU power ops registered at probe cover it; .begin only tries again if that registration failed. No register access changes. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 91fe8529c85e4..72491a31a0f6e 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -2380,10 +2380,10 @@ static void t6040_sleep_pmvc(struct t6040_sleep *s) } /* - * The refusal-first gate. Default: print the blocker list and return an error - * here, before any device suspends, so nothing is entered. A dry run proceeds - * and refuses at .enter. allow_entry with no blocker and no dry run: the - * path that reaches the commit. + * The refusal-first gate of a suspend to RAM, which t6040_sleep_valid() offers only with + * allow_entry. With a blocker left, print the blocker list and return an error here, before + * any device suspends, so nothing is entered. A dry run proceeds and refuses at .enter. No + * blocker and no dry run: the path that reaches the commit. */ static int t6040_sleep_begin(suspend_state_t state) { @@ -2416,7 +2416,7 @@ static int t6040_sleep_begin(suspend_state_t state) if (dcs_to) pr_warn("t6040-sleep: DCS power words %#x did not read on before automatic mode\n", dcs_to); - if (!begin_total && allow_entry && !dry) { + if (!begin_total && !dry) { /* The non-display blockers are clear and no dry run is asked for: proceed * to device suspend; .enter holds the real commit until the display is * also off. The AON WD2 is the net from here. */ @@ -2808,7 +2808,7 @@ static int t6040_sleep_enter(suspend_state_t state) left = t6040_sleep_lower_display(s); if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); - if (t6040_sleep_total_blockers(s) || !allow_entry || !apple_sleep_power_loss()) { + if (t6040_sleep_total_blockers(s)) { unsigned int on = t6040_sleep_log_groups(s); pr_info("t6040-sleep: at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", @@ -3242,10 +3242,10 @@ static int t6040_sleep_probe(struct platform_device *pdev) debugfs_create_file("preflight", 0400, dir, sleep, &t6040_sleep_preflight_fops); /* - * The CPU power ops act only when a commit stops the parked secondaries. - * A real entry needs allow_entry; a dry run registers them at its .begin, - * for the hold. Until then kexec keeps refusing parking CPUs as before. - * Parking itself needs no power ops. + * The CPU power ops act only when a commit stops the parked secondaries, and a dry run + * needs them for the hold. Both are suspends to RAM, offered only with allow_entry, so + * only then are they registered (.begin tries again if this failed). Without it kexec + * keeps refusing parking CPUs as before. Parking itself needs no power ops. */ if (allow_entry) t6040_sleep_power_ops(sleep); From 24932077ee62a610822af0f573e4a5e5f6cca592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:33:56 +0100 Subject: [PATCH 098/269] soc: apple: sleep: t6040: drop state nobody reads and wrappers that add nothing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - struct t6040_sleep_watch.name was written at probe and never read. - ans_bridge_quiesced kept whether the commit found ANS's fabric bridge armed, only for one debug line after the wake. The bridge is still quiesced as before; t6040_ans_bridge_quiesce() returns nothing. - t6040_sleep_total_blockers() was a one-line wrapper with one caller. - t6040_tunable_apply_changed() tested for a NULL table that cannot be NULL there: the storage PCIe raise runs only with apcie_st.valid, which requires both PHY-IP tables. - The BUILD_BUG_ON() on the size of t6040_axi2af[] compared the array with the size it was declared with. Size the array by its initialiser, so that the check catches a missing or extra domain. No register access changes. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 39 ++++++++------------------------- 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 72491a31a0f6e..667abd63981b9 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -348,7 +348,6 @@ struct t6040_sleep_group { /* A device the commit point looks at: bound or not, and its own power word. */ struct t6040_sleep_watch { const char *compatible; - const char *name; struct regmap *ps_regmap; u32 ps_offset; bool has_ps; @@ -455,7 +454,6 @@ struct t6040_sleep { /* ANS's storage ST lanes: windows + whether a commit stopped ANS. */ struct t6040_apcie_st apcie_st; bool pcie_ans_stopped; - bool ans_bridge_quiesced; /* the commit found ANS's fabric bridge armed */ int pcie_ans_on_ret; u32 apcie_wake_step; u32 gp_phy_saved[2], gp_appclk_saved[2], gp_perst_saved[2]; @@ -631,12 +629,6 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct return blockers + disp; } -/* The total, at the commit point: everything the live state still holds against the commit. */ -static unsigned int t6040_sleep_total_blockers(struct t6040_sleep *sleep) -{ - return t6040_sleep_live_blockers(sleep, NULL, NULL); -} - static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s); static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) @@ -1150,7 +1142,7 @@ static const struct t6040_axi2af_domain { bool east; /* power word in soc_east (true) or soc_ps (false) */ u32 ps_off; phys_addr_t bridge_pa; -} t6040_axi2af[T6040_AXI2AF_DOMAINS] = { +} t6040_axi2af[] = { { "atc0_common", false, 0x390, 0x380000000 }, { "atc1_common", false, 0x3b8, 0x388000000 }, { "atc2_common", false, 0x3e0, 0x390000000 }, @@ -1487,13 +1479,6 @@ static bool t6040_ps_actual_on(void __iomem *base, u32 offset) return FIELD_GET(PS_ACTUAL, readl(base + offset)); } -/* Read-modify-write a tunable in table order, writing only where it changes. */ -static void t6040_tunable_apply_changed(void __iomem *regs, const struct apple_tunable *t) -{ - if (t) - apple_sleep_tunable_apply_changed(regs, t); -} - /* * ENTRY: quiesce ANS's storage link, once ANS has been stopped by its driver. The PHY, the * clock generator and the AXI enables are treated as shared by the two ST lanes. @@ -1719,24 +1704,22 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) * (t6040_sleep_raise_apcie_st()) and the SoC's sleep engine, not this driver, takes ANS down. * The boot loader leaves the bit clear, so a boot's first sleep goes down with it clear; * without this the next one meets the bridge as the first wake armed it. Only while ANS is - * on: the bridge sits in it. Returns whether the bit was set. + * on: the bridge sits in it. */ -static bool t6040_ans_bridge_quiesce(struct t6040_sleep *s) +static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) { void __iomem *bridge = s->axi2af_bridge[AXI2AF_ANS]; u32 a, b; if (!bridge || !s->soc_east || FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != 0xf) - return false; + return; a = readl(bridge + AXI2AF_BRIDGE_104); b = readl(bridge + AXI2AF_BRIDGE_108); if (!((a | b) & BIT(0))) - return false; + return; writel(a & ~BIT(0), bridge + AXI2AF_BRIDGE_104); writel(b & ~BIT(0), bridge + AXI2AF_BRIDGE_108); - - return true; } static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) @@ -1759,7 +1742,7 @@ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) writel(readl(a->axi + APCIE_AXI_EN0) & ~BIT(0), a->axi + APCIE_AXI_EN0); writel(readl(a->axi + APCIE_AXI_EN1) & ~BIT(0), a->axi + APCIE_AXI_EN1); } - s->ans_bridge_quiesced = t6040_ans_bridge_quiesce(s); + t6040_ans_bridge_quiesce(s); } static void t6040_bridge_rearm(void __iomem *bridge) @@ -1865,8 +1848,8 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) s->apcie_wake_step = 5; writel(readl(run) | APCIE_PHY_RUN_EN, run); - t6040_tunable_apply_changed(a->phy_ip, a->phy_ip_pll); - t6040_tunable_apply_changed(a->phy_ip, a->phy_ip_lanes); + apple_sleep_tunable_apply_changed(a->phy_ip, a->phy_ip_pll); + apple_sleep_tunable_apply_changed(a->phy_ip, a->phy_ip_lanes); writel(readl(a->phy_ip + APCIE_PHY_IP_PLL_CTL) & ~APCIE_PHY_IP_PLL_HOLD, a->phy_ip + APCIE_PHY_IP_PLL_CTL); writel(readl(run) | APCIE_PHY_RUN_EN2, run); @@ -2808,7 +2791,7 @@ static int t6040_sleep_enter(suspend_state_t state) left = t6040_sleep_lower_display(s); if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); - if (t6040_sleep_total_blockers(s)) { + if (t6040_sleep_live_blockers(s, NULL, NULL)) { unsigned int on = t6040_sleep_log_groups(s); pr_info("t6040-sleep: at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", @@ -2836,7 +2819,6 @@ static int t6040_sleep_enter(suspend_state_t state) return ret; } s->pcie_ans_stopped = false; - s->ans_bridge_quiesced = false; apple_sleep_coprocs_stop(true, &s->pcie_ans_stopped); apple_sleep_coprocs_stop(false, &s->pcie_ans_stopped); t6040_wake_arm(s); @@ -2846,8 +2828,6 @@ static int t6040_sleep_enter(suspend_state_t state) ret = cpu_suspend(0, t6040_sleep_commit); if (!ret) t6040_sleep_return(s); - if (s->ans_bridge_quiesced) - pr_debug("t6040-sleep: ANS's fabric bridge was armed at the commit: quiesced\n"); if (s->returned) t6040_wake_sync(); @@ -2950,7 +2930,6 @@ static void t6040_sleep_watch_init(struct t6040_sleep *s, unsigned int i, struct regmap *regmap; w->compatible = compatible; - w->name = name; np = of_find_compatible_node(NULL, NULL, compatible); if (!np) return; From 109867e7b3adcdb35e752a1cec9748248c269f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:02 +0100 Subject: [PATCH 099/269] soc: apple: sleep: t6040: drop the PTD and GAPF windows from the preflight MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preflight mapped the PMP's PTD block (68 KiB) and the DART's GAPF block at probe only to print six of their registers, under offsets with no names. Both blocks belong to other drivers, which can show their own state. Drop the two windows and their lines; nothing on the sleep path used them. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 667abd63981b9..54eb0bf0e8e9b 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -241,14 +241,6 @@ static const struct { #define T6040_PMC_BWR_PA 0x5048ac020ULL #define PMC_BOOST 0x8 -/* - * The PMP's PTD (pmp-report 0x5003c0000, T6041 layout as drivers/pmdomain/apple/ - * pmp-report.c): SOC-DEV-PS-REQ read at +0x1e00, SOC-DEV-PS-ACK at +0x1e80, 64-bit. - * Read only, for the preflight. - */ -#define T6040_PTD_PA 0x5003c0000ULL -#define PTD_REQ_READ 0x1e00 -#define PTD_ACK 0x1e80 #define QUIESCE_TIMEOUT_US 2000 #define AGX_GATE_RESIDENT GENMASK(19, 18) @@ -443,8 +435,6 @@ struct t6040_sleep { void __iomem *tvm_ctl[8]; void __iomem *tvm_status[8]; void __iomem *pmc_bwr; - void __iomem *ptd; - void __iomem *gapf; void __iomem *afr_ps; u32 cluster_saved[CLUSTER_CTL_NR]; u32 wake_mask_saved[AOP_WAKE_WORDS]; @@ -650,15 +640,6 @@ static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) seq_printf(s, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], readl(sleep->cluster_ctl + CLUSTER_CTL(i))); } - if (sleep->gapf) - seq_printf(s, "gapf: base %#x cfg %#x lock %#x, slices dcp(17) %#x dcpext0(29) %#x dcpext1(48) %#x\n", - readl(sleep->gapf), readl(sleep->gapf + 0x20), readl(sleep->gapf + 0x24), - readl(sleep->gapf + 0x540), readl(sleep->gapf + 0x840), - readl(sleep->gapf + 0xd00)); - if (sleep->ptd) - seq_printf(s, "pmp ptd: SOC-DEV-PS-REQ %#llx, SOC-DEV-PS-ACK %#llx, status %#llx\n", - readq(sleep->ptd + PTD_REQ_READ), readq(sleep->ptd + PTD_ACK), - readq(sleep->ptd + 0x10)); return 0; } @@ -3196,9 +3177,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->disp_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISP_PWRGATE_PA, 0x100); sleep->pmc_dcp = t6040_ioremap_np(&pdev->dev, T6040_PMC_DCP_PA, 12); sleep->pmc_bwr = t6040_ioremap_np(&pdev->dev, T6040_PMC_BWR_PA, 0x10); - sleep->ptd = t6040_ioremap_np(&pdev->dev, T6040_PTD_PA, 0x11000); - /* The DART clock-protection (GAPF) block, read-only here. */ - sleep->gapf = t6040_ioremap_np(&pdev->dev, 0x500578000ULL, 0x1000); sleep->ka_soc = t6040_ioremap_np(&pdev->dev, T6040_KA_SOC_PA, KA_SIZE); sleep->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); sleep->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); From 56ca733c3f1795f5f68f2947389a92a8108b3657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:02 +0100 Subject: [PATCH 100/269] soc: apple: sleep: one name per power word bit and per GP port register MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part defined the GP PCIe port and PHY registers a second time under GP_* names, with the values of the APCIE_PORT_* and APCIE_PHY_CTRL_* definitions it already shares with the T8140 sequences: the same port block. Use the shared names, and add the port status register and its ready bit (+0x804 bit 0, PORT_STATUS in pcie-apple.c) next to them. Bit 11 of a power word had three names (PS_BUSY in both files, PS_PARENT_MISSING in sleep.c, SOC_EAST_PARENT_MISSING here), bit 12 two (PS_UNLATCH, PS_RESET_REQ) and bit 10 two (PS_DEV_DISABLE, SOC_EAST_DEV_DISABLE). Keep one each in sleep-internal.h; bit 11 is PS_BUSY, as the PMGR power state driver calls it. No functional change: both objects are unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 3 + drivers/soc/apple/sleep-t6040.c | 119 +++++++++++------------------ drivers/soc/apple/sleep.c | 4 +- 3 files changed, 50 insertions(+), 76 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index f33e0c67bac59..bb02e071eadf7 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -87,6 +87,7 @@ struct aura_s2r_mailbox { /* PS word */ #define PS_AUTO_ENABLE BIT(28) #define PS_UNLATCH BIT(12) +#define PS_BUSY BIT(11) #define PS_DEV_DISABLE BIT(10) #define PS_FLAGS GENMASK(9, 8) #define PS_ACTUAL GENMASK(7, 4) @@ -169,6 +170,8 @@ struct aura_s2r_mailbox { #define APCIE_PORT_APPCLK 0x800 #define APCIE_PORT_APPCLK_EN BIT(0) #define APCIE_PORT_APPCLK_CGDIS BIT(8) +#define APCIE_PORT_STATUS 0x804 +#define APCIE_PORT_STATUS_READY BIT(0) #define APCIE_PORT_PERST 0x82c #define APCIE_PORT_PHY_LINK BIT(31) #define APCIE_PORT_PHY_EN0 BIT(9) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 54eb0bf0e8e9b..76ed24506d250 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -106,7 +106,6 @@ #define PS_APCIE_SYS_ST0 0x380 /* SOC-East: ps_apcie_sys_st0 */ #define PS_APCIE_SYS_ST1 0x388 /* SOC-East: ps_apcie_sys_st1 */ #define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ -#define PS_BUSY BIT(11) /* a request is in progress */ /* * cluster-ctl (pmgr reg0 + cluster-ctl-offset 0x20000 = 0x502820000). Measured @@ -139,13 +138,11 @@ /* * SOC-East (pmgr reg2 = 0x502280000). The SEP/retention word at +0xc00: on - * wake the platform clears DEV_DISABLE (BIT10) and then polls PARENT_MISSING (BIT11) - * clear before going on. Also holds several + * wake the platform clears PS_DEV_DISABLE and then polls PS_BUSY clear before going + * on. Also holds several * of the axi2af domains' power words. */ #define SOC_EAST_RETENTION 0xc00 -#define SOC_EAST_DEV_DISABLE BIT(10) -#define SOC_EAST_PARENT_MISSING BIT(11) #define SOC_EAST_TIMEOUT_US 5000 /* @@ -298,28 +295,7 @@ static const struct { #define PS_APCIE_SYS_GP 0x390 #define AXI2AF_ANS 9 #define AXI2AF_APCIE_SYS_GP 14 -#define GP_PORT_L2 0x88 -#define GP_PORT_L2_ENTER 0x11 -#define GP_PORT_L2_BUSY BIT(0) -#define GP_PORT_INTSTAT 0x100 -#define GP_PORT_INTMSK 0x104 -#define GP_PORT_INT_LINK GENMASK(7, 4) -#define GP_PORT_PERST_GUARD 0x13c -#define GP_PORT_LINKSTS 0x208 -#define GP_PORT_LINKSTS_UP BIT(0) -#define GP_PORT_APPCLK 0x800 -#define GP_PORT_APPCLK_EN BIT(0) -#define GP_PORT_APPCLK_CGDIS BIT(8) -#define GP_PORT_PERST 0x82c -#define GP_PHY_LINK BIT(31) -#define GP_PHY_EN0 BIT(9) -#define GP_PHY_EN1 BIT(10) -#define GP_PHY_RESET BIT(4) -#define GP_PHY_CLK0REQ BIT(0) -#define GP_PHY_CLK1REQ BIT(1) -#define GP_PHY_CLK0ACK BIT(2) #define GP_PIN_DATA BIT(0) -#define GP_STOP_US 5000 #define T6040_PMC_DCP_PA 0x5048a9c14ULL /* count of DCPs powered (+0), second voter (+4); the platform ends at 0/0 */ #define T6040_AXI2AF_DOMAINS 15 @@ -1943,16 +1919,16 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) if (!port || !phy) return; s->gp_phy_saved[p] = readl(phy); - s->gp_appclk_saved[p] = readl(port + GP_PORT_APPCLK); - s->gp_perst_saved[p] = readl(port + GP_PORT_PERST); + s->gp_appclk_saved[p] = readl(port + APCIE_PORT_APPCLK); + s->gp_perst_saved[p] = readl(port + APCIE_PORT_PERST); s->gp_saved[p] = true; - writel(readl(port + GP_PORT_INTMSK) | GP_PORT_INT_LINK, port + GP_PORT_INTMSK); - writel(GP_PORT_INT_LINK, port + GP_PORT_INTSTAT); - writel(readl(port + GP_PORT_APPCLK) | GP_PORT_APPCLK_CGDIS, port + GP_PORT_APPCLK); - if (readl(port + GP_PORT_LINKSTS) & GP_PORT_LINKSTS_UP) { - writel(GP_PORT_L2_ENTER, port + GP_PORT_L2); - readl_poll_timeout_atomic(port + GP_PORT_L2, val, !(val & GP_PORT_L2_BUSY), 10, - GP_STOP_US); + writel(readl(port + APCIE_PORT_INTMSK) | APCIE_PORT_INT_LINK, port + APCIE_PORT_INTMSK); + writel(APCIE_PORT_INT_LINK, port + APCIE_PORT_INTSTAT); + writel(readl(port + APCIE_PORT_APPCLK) | APCIE_PORT_APPCLK_CGDIS, port + APCIE_PORT_APPCLK); + if (readl(port + APCIE_PORT_LINKSTS) & APCIE_PORT_LINKSTS_UP) { + writel(APCIE_PORT_L2_ENTER, port + APCIE_PORT_L2); + readl_poll_timeout_atomic(port + APCIE_PORT_L2, val, !(val & APCIE_PORT_L2_BUSY), + 10, APCIE_PORT_STOP_US); } if (s->gp_pins && s->gp_perst[p] >= 0) { void __iomem *reg = s->gp_pins + 4 * s->gp_perst[p]; @@ -1960,20 +1936,20 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) val = readl(reg); writel(s->gp_perst_low[p] ? val & ~GP_PIN_DATA : val | GP_PIN_DATA, reg); } - writel(readl(phy) & ~GP_PHY_LINK, phy); + writel(readl(phy) & ~APCIE_PORT_PHY_LINK, phy); udelay(1000); /* What this bit does isn't known. It is set around PERST# (T8140). */ - writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); - writel(0, port + GP_PORT_PERST); - writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); - writel(readl(phy) & ~GP_PHY_EN1, phy); - writel(readl(phy) & ~GP_PHY_EN0, phy); - writel(readl(phy) | GP_PHY_RESET, phy); - writel(readl(phy) & ~GP_PHY_CLK0REQ, phy); - readl_poll_timeout_atomic(phy, val, !(val & GP_PHY_CLK0ACK), 1, GP_STOP_US); - writel(readl(phy) & ~GP_PHY_CLK1REQ, phy); - writel(readl(port + GP_PORT_APPCLK) & ~GP_PORT_APPCLK_EN, port + GP_PORT_APPCLK); - writel(readl(port + GP_PORT_INTSTAT), port + GP_PORT_INTSTAT); + writel(readl(port + APCIE_PORT_PERST_GUARD) | BIT(8), port + APCIE_PORT_PERST_GUARD); + writel(0, port + APCIE_PORT_PERST); + writel(readl(port + APCIE_PORT_PERST_GUARD) & ~BIT(8), port + APCIE_PORT_PERST_GUARD); + writel(readl(phy) & ~APCIE_PORT_PHY_EN1, phy); + writel(readl(phy) & ~APCIE_PORT_PHY_EN0, phy); + writel(readl(phy) | APCIE_PHY_CTRL_RESET, phy); + writel(readl(phy) & ~APCIE_PHY_CTRL_CLK0REQ, phy); + readl_poll_timeout_atomic(phy, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_PORT_STOP_US); + writel(readl(phy) & ~APCIE_PHY_CTRL_CLK1REQ, phy); + writel(readl(port + APCIE_PORT_APPCLK) & ~APCIE_PORT_APPCLK_EN, port + APCIE_PORT_APPCLK); + writel(readl(port + APCIE_PORT_INTSTAT), port + APCIE_PORT_INTSTAT); } /* The GP host's two ports, while the block's domains are all still on. */ @@ -2123,12 +2099,9 @@ static int t6040_sleep_commit(unsigned long unused) * the controller's PERST register as saved, then the port's ready bit polled. The link * itself stays down for the host driver. */ -#define GP_PHY_CLK1ACK BIT(3) -#define GP_PORT_STATUS 0x804 -#define GP_PORT_STATUS_READY BIT(0) - static void t6040_gp_pcie_clk(struct t6040_sleep *s) { + const u32 appclk = APCIE_PORT_APPCLK_EN | APCIE_PORT_APPCLK_CGDIS; unsigned int timeouts = 0; u32 val; int i; @@ -2141,30 +2114,32 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) continue; cfg = s->gp_phy_saved[i]; app = s->gp_appclk_saved[i]; - writel(readl(phy) | GP_PHY_CLK0REQ, phy); - if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK0ACK, 1, 50000)) + writel(readl(phy) | APCIE_PHY_CTRL_CLK0REQ, phy); + if (readl_poll_timeout_atomic(phy, val, val & APCIE_PHY_CTRL_CLK0ACK, 1, 50000)) timeouts++; - writel(readl(phy) | GP_PHY_CLK1REQ, phy); - if (readl_poll_timeout_atomic(phy, val, val & GP_PHY_CLK1ACK, 1, 50000)) + writel(readl(phy) | APCIE_PHY_CTRL_CLK1REQ, phy); + if (readl_poll_timeout_atomic(phy, val, val & APCIE_PHY_CTRL_CLK1ACK, 1, 50000)) timeouts++; - if (!(cfg & GP_PHY_RESET)) { - writel(readl(phy) & ~GP_PHY_RESET, phy); + if (!(cfg & APCIE_PHY_CTRL_RESET)) { + writel(readl(phy) & ~APCIE_PHY_CTRL_RESET, phy); udelay(1); } - writel(readl(phy) | (cfg & (GP_PHY_EN0 | GP_PHY_EN1)), phy); - writel(readl(phy) | (cfg & GP_PHY_LINK), phy); - writel((readl(port + GP_PORT_APPCLK) & ~(GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)) | - (app & (GP_PORT_APPCLK_EN | GP_PORT_APPCLK_CGDIS)), port + GP_PORT_APPCLK); + writel(readl(phy) | (cfg & (APCIE_PORT_PHY_EN0 | APCIE_PORT_PHY_EN1)), phy); + writel(readl(phy) | (cfg & APCIE_PORT_PHY_LINK), phy); + writel((readl(port + APCIE_PORT_APPCLK) & ~appclk) | (app & appclk), + port + APCIE_PORT_APPCLK); /* * The stop also wrote the controller's PERST register 0, and without it the * port does not report ready. Put it back as it was (the endpoint's PERST# pin * stays asserted), with the guard bit set around it. */ - writel(readl(port + GP_PORT_PERST_GUARD) | BIT(8), port + GP_PORT_PERST_GUARD); - writel(s->gp_perst_saved[i], port + GP_PORT_PERST); - writel(readl(port + GP_PORT_PERST_GUARD) & ~BIT(8), port + GP_PORT_PERST_GUARD); - if (readl_poll_timeout_atomic(port + GP_PORT_STATUS, val, val & GP_PORT_STATUS_READY, - 1, 50000)) + writel(readl(port + APCIE_PORT_PERST_GUARD) | BIT(8), + port + APCIE_PORT_PERST_GUARD); + writel(s->gp_perst_saved[i], port + APCIE_PORT_PERST); + writel(readl(port + APCIE_PORT_PERST_GUARD) & ~BIT(8), + port + APCIE_PORT_PERST_GUARD); + if (readl_poll_timeout_atomic(port + APCIE_PORT_STATUS, val, + val & APCIE_PORT_STATUS_READY, 1, 50000)) timeouts++; } s->gp_clk_timeouts = timeouts; @@ -2175,7 +2150,7 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) * AURAS2R1 mailbox hands it back to cpu_resume. aura restores NO SoC register; * every write here is the OS driver's job. It is the * mirror of the PMGR wake restore: the three CPU complexes on, the two wake - * pwrgates, SOC-East retention off (with the PARENT_MISSING poll), the axi2af + * pwrgates, SOC-East retention off (with the busy poll), the axi2af * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the * AP-global S2R control cleared, and the wake-status words read. Between them, * what a Linux sleep needs on top: ANS's storage link, the GP PCIe ports' clocks @@ -2221,12 +2196,12 @@ static void t6040_sleep_return(struct t6040_sleep *s) } t6040_wake_sync(); - /* SOC-East retention off: clear DEV_DISABLE, then poll PARENT_MISSING clear. */ + /* SOC-East retention off: clear DEV_DISABLE, then poll BUSY clear. */ if (s->soc_east) { val = readl(s->soc_east + SOC_EAST_RETENTION); - writel(val & ~SOC_EAST_DEV_DISABLE, s->soc_east + SOC_EAST_RETENTION); + writel(val & ~PS_DEV_DISABLE, s->soc_east + SOC_EAST_RETENTION); ret = readl_poll_timeout_atomic(s->soc_east + SOC_EAST_RETENTION, val, - !(val & SOC_EAST_PARENT_MISSING), 1, + !(val & PS_BUSY), 1, SOC_EAST_TIMEOUT_US); if (ret) pr_warn("t6040-sleep: SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); @@ -2477,8 +2452,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) return -EBUSY; } -#define PS_RESET_REQ BIT(12) - #define DISP_CPU_GATE_US 50000 /* @@ -2742,7 +2715,7 @@ static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) if (!cpu && !regmap_read(g->regmap, off, &val) && t6040_sleep_ps_on(val, cpu)) { val &= ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS | PS_DEV_DISABLE | - PS_BUSY | PS_RESET_REQ); + PS_BUSY | PS_UNLATCH); regmap_write(g->regmap, off, val); if (t6040_regmap_poll_off(g->regmap, off, &cur)) { pr_warn("t6040-sleep: %s +%#x still on: %#x\n", g->name, diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index dd673f0390d1b..ba2f7651280ba 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1181,7 +1181,6 @@ EXPORT_SYMBOL_GPL(apple_sleep_domain_is_down); * target 4 and 0, released with the domain off, then raised again, its DARTs' state kept. The * caller has cleared the run bit, and starts the firmware after. */ -#define PS_BUSY BIT(11) #define PS_RESET BIT(31) #define PS_ENGINE_RESET_4 0x8f000244 #define PS_ENGINE_RESET_0 0x80000000 @@ -2197,7 +2196,6 @@ static void apple_sleep_set_wd2(struct apple_sleep *s, unsigned int secs) apple_sleep_wd2_arm(s->wdt, secs); } -#define PS_PARENT_MISSING BIT(11) #define PS_AUTO_STATE GENMASK(27, 24) #define PS_REWAKE_PASSES 2 @@ -2229,7 +2227,7 @@ static int sleep_ps_find(const u32 *all, int n, u32 offset) */ static int sleep_ps_restore(void __iomem *reg, u32 saved) { - u32 keep = (saved & ~(PS_AUTO_ENABLE | PS_AUTO_STATE | PS_PARENT_MISSING | PS_DEV_DISABLE | + u32 keep = (saved & ~(PS_AUTO_ENABLE | PS_AUTO_STATE | PS_BUSY | PS_DEV_DISABLE | PS_FLAGS | PS_ACTUAL)) | (readl(reg) & PS_AUTO_STATE); u32 now; int ret; From 9f71b06d8bd7e9d76bc6a027bf43f5ad9aa35bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:03 +0100 Subject: [PATCH 101/269] soc: apple: sleep: t6040: name the constants and masks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Give the numbers of the T6040 part their names: the power word's active state 0xf and minimum state field 19:16 (PS_ACTIVE, PS_MIN, as in the PMGR power state driver, now in sleep-internal.h; the AGX gate's "level" field is the same PS_MIN), the clock-gated words the commit writes, the keep-alive bit, the DCS dynamic power gate bits, the ATC PHY sleep entry registers, the PCIe clock generator bits that already had names, dart-dcp's transaction count field, and PS_STRIDE where the stride was spelled 8. Loops over the DCS, ATC and GP port windows are bounded by the arrays they walk. The masks written as hexadecimal literals (0xeffffcff, 0xfffffcf0 | 0x300) and the (x & ~M) | M forms become the expressions they stand for. No functional change: the object is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 2 + drivers/soc/apple/sleep-t6040.c | 129 ++++++++++++++++------------- 2 files changed, 75 insertions(+), 56 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index bb02e071eadf7..ed1787a49efff 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -86,12 +86,14 @@ struct aura_s2r_mailbox { /* PS word */ #define PS_AUTO_ENABLE BIT(28) +#define PS_MIN GENMASK(19, 16) #define PS_UNLATCH BIT(12) #define PS_BUSY BIT(11) #define PS_DEV_DISABLE BIT(10) #define PS_FLAGS GENMASK(9, 8) #define PS_ACTUAL GENMASK(7, 4) #define PS_TARGET GENMASK(3, 0) +#define PS_ACTIVE 0xf #define PS_OFF_TIMEOUT_US 5000 #define PS_BUSY_SETTLE_US 1000 diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 76ed24506d250..0a7df8b302b44 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -106,6 +106,9 @@ #define PS_APCIE_SYS_ST0 0x380 /* SOC-East: ps_apcie_sys_st0 */ #define PS_APCIE_SYS_ST1 0x388 /* SOC-East: ps_apcie_sys_st1 */ #define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ +/* The clock-gated words (TARGET 4) the commit writes, as the sleep engine asks for them. */ +#define PS_APCIE_PHY_SW_CLKGATED 0x0f000244 +#define PS_APCIE_GP_CLKGATED 0x00000244 /* * cluster-ctl (pmgr reg0 + cluster-ctl-offset 0x20000 = 0x502820000). Measured @@ -180,6 +183,7 @@ #define KA_WORDS 32 #define KA_STRIDE 0x44 #define KA_SIZE (KA_WORDS * KA_STRIDE) +#define KA_ENABLE BIT(0) /* * The SMC's AP watchdog deadline (the watchdog timer's reg[2] + 8): @@ -242,7 +246,6 @@ static const struct { #define AGX_GATE_RESIDENT GENMASK(19, 18) #define T6040_AFR_PS_PA 0x288e80100ULL -#define AFR_LEVEL GENMASK(19, 16) #define AGX_TIMEOUT_US 10000 /* @@ -950,7 +953,7 @@ static void t6040_aop_ps_save(struct t6040_sleep *s) if (!s->aop_ps) return; for (i = 0; i < T6040_AOP_PS_WORDS; i++) - s->aop_ps_saved[i] = readl(s->aop_ps + 8 * i); + s->aop_ps_saved[i] = readl(s->aop_ps + PS_STRIDE * i); s->aop_ps_saved_valid = true; } @@ -967,9 +970,9 @@ static void t6040_aop_ps_restore(struct t6040_sleep *s) u32 v, want = s->aop_ps_saved[off / 8], now = readl(r); if (FIELD_GET(PS_ACTUAL, want)) { - if (FIELD_GET(PS_ACTUAL, now) == 0xf && !(now & PS_AUTO_ENABLE)) + if (FIELD_GET(PS_ACTUAL, now) == PS_ACTIVE && !(now & PS_AUTO_ENABLE)) continue; - writel((now & ~(PS_AUTO_ENABLE | PS_TARGET)) | PS_TARGET, r); + writel((now & ~PS_AUTO_ENABLE) | PS_TARGET, r); } else { if (FIELD_GET(PS_ACTUAL, now) || !FIELD_GET(PS_TARGET, want)) continue; @@ -1013,16 +1016,17 @@ static void t6040_sio_ps_restore(struct t6040_sleep *s) void __iomem *w = s->pmgr_east + off; u32 saved = s->sio_ps_saved[i]; - if (FIELD_GET(PS_TARGET, saved) != 0xf || FIELD_GET(PS_ACTUAL, saved) != 0xf) + if (FIELD_GET(PS_TARGET, saved) != PS_ACTIVE || + FIELD_GET(PS_ACTUAL, saved) != PS_ACTIVE) continue; val = readl(w); - if (FIELD_GET(PS_TARGET, val) == 0xf) + if (FIELD_GET(PS_TARGET, val) == PS_ACTIVE) continue; - writel((val & ~PS_TARGET) | FIELD_PREP(PS_TARGET, 0xf), w); + writel(val | PS_TARGET, w); /* Timekeeping is still suspended here: count the wait, no ktime. */ for (n = 0; n < 1000; n++) { cur = readl(w); - if (FIELD_GET(PS_ACTUAL, cur) == 0xf) + if (FIELD_GET(PS_ACTUAL, cur) == PS_ACTIVE) break; udelay(10); } @@ -1220,7 +1224,7 @@ static void t6040_secondaries_off(struct t6040_sleep *s) if (!bit || bit == self) continue; - readl_poll_timeout_atomic(s->soc_ps + 8 * __ffs(bit), v, + readl_poll_timeout_atomic(s->soc_ps + PS_STRIDE * __ffs(bit), v, !FIELD_GET(PS_ACTUAL, v), 1, 10000); } } @@ -1265,8 +1269,8 @@ static void t6040_agx_gate(struct t6040_sleep *s) val = readl(s->afr_ps); if (!FIELD_GET(PS_TARGET, val)) return; - writel((val & ~AFR_LEVEL) | FIELD_PREP(AFR_LEVEL, 4), s->afr_ps); - if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(AFR_LEVEL, cur) == 4, 1, + writel((val & ~PS_MIN) | FIELD_PREP(PS_MIN, 4), s->afr_ps); + if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(PS_MIN, cur) == 4, 1, AGX_TIMEOUT_US)) return; if (readl_poll_timeout_atomic(s->afr_ps, cur, FIELD_GET(PS_ACTUAL, cur) == 4, 1, @@ -1293,9 +1297,9 @@ static u64 t6040_keep_alive(struct t6040_sleep *s, bool on, u64 mask) void __iomem *b = s->ka_east + i * KA_STRIDE; u32 va = readl(a), vb = readl(b); - was |= (u64)(va & 1) << i | (u64)(vb & 1) << (i + KA_WORDS); - writel((va & ~1U) | (on && (mask & BIT_ULL(i))), a); - writel((vb & ~1U) | (on && (mask & BIT_ULL(i + KA_WORDS))), b); + was |= (u64)(va & KA_ENABLE) << i | (u64)(vb & KA_ENABLE) << (i + KA_WORDS); + writel((va & ~KA_ENABLE) | (on && (mask & BIT_ULL(i))), a); + writel((vb & ~KA_ENABLE) | (on && (mask & BIT_ULL(i + KA_WORDS))), b); } return was; } @@ -1367,7 +1371,7 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) if (!ps || !bridge) continue; val = readl(ps); - if (FIELD_GET(PS_TARGET, val) != 0xf) + if (FIELD_GET(PS_TARGET, val) != PS_ACTIVE) continue; if (!FIELD_GET(PS_ACTUAL, val)) { pr_debug("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", @@ -1476,13 +1480,13 @@ static void t6040_apcie_phy_off(struct t6040_sleep *s) writel(readl(run) & ~APCIE_PHY_RUN_EN, run); } -/* APCIE_PHY_SW to clock-gated (0x0f000244), its busy bit polled clear. */ +/* APCIE_PHY_SW to clock-gated, its busy bit polled clear. */ static void t6040_apcie_phy_sw_gate(struct t6040_sleep *s) { void __iomem *reg = s->soc_east + PS_APCIE_PHY_SW; u32 val; - writel(0x0f000244, reg); + writel(PS_APCIE_PHY_SW_CLKGATED, reg); readl_poll_timeout_atomic(reg, val, !(val & PS_BUSY), 1, PS_OFF_TIMEOUT_US); } @@ -1492,11 +1496,11 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) void __iomem *clk = s->apcie_st.clkgen + APCIE_CLK_CTRL; u32 val; - writel((readl(clk) & ~GENMASK(5, 3)) | BIT(4) | BIT(3), clk); - writel(readl(clk) | BIT(1), clk); + writel((readl(clk) & ~(APCIE_CLK_RUN | GENMASK(4, 3))) | GENMASK(4, 3), clk); + writel(readl(clk) | APCIE_CLK_OFF, clk); writel(readl(clk) & ~BIT(0), clk); readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); - writel(readl(clk) | BIT(2), clk); + writel(readl(clk) | APCIE_CLK_OFF2, clk); } /* @@ -1518,7 +1522,7 @@ static void t6040_atc_commons_on(struct t6040_sleep *s) if (!b) return; t6040_ps_raise_start(b, PS_FAB2_SOC); - for (i = 0; i < 4; i++) + for (i = 0; i < ARRAY_SIZE(t6040_atc_common); i++) t6040_ps_raise_start(b, t6040_atc_common[i]); t6040_ps_raise_finish(b, PS_FAB2_SOC); } @@ -1531,22 +1535,32 @@ static void t6040_atc_commons_on(struct t6040_sleep *s) * always, any other port once a display runs on it in DP alternate mode. A parked port, and * one its PHY driver has put to sleep, already has +0x28 bit 0 set and is left alone. */ +#define ATC_CFG_CLK_STATUS 0x04 +#define ATC_CFG_CLK_ACK BIT(8) +#define ATC_CFG_S2R_FSM 0x08 +#define ATC_CFG_S2R_FSM_EN BIT(0) +#define ATC_CFG_AHB_CLK 0x28 +#define ATC_CFG_AHB_CLK_REQ_N BIT(0) +#define ATC_CFG_S2R_AON 0x2c +#define ATC_CFG_S2R_AON_SYNC_RST_N BIT(0) + static void t6040_atc_s2r_entry(struct t6040_sleep *s) { u32 v; int n; - for (n = 0; n < 4; n++) { + for (n = 0; n < ARRAY_SIZE(s->atc_cfg); n++) { void __iomem *c = s->atc_cfg[n]; - if (!c || (readl(c + 0x28) & BIT(0))) + if (!c || (readl(c + ATC_CFG_AHB_CLK) & ATC_CFG_AHB_CLK_REQ_N)) continue; - writel(readl(c + 0x2c) | BIT(0), c + 0x2c); + writel(readl(c + ATC_CFG_S2R_AON) | ATC_CFG_S2R_AON_SYNC_RST_N, + c + ATC_CFG_S2R_AON); udelay(1); - writel(readl(c + 0x08) | BIT(0), c + 0x08); + writel(readl(c + ATC_CFG_S2R_FSM) | ATC_CFG_S2R_FSM_EN, c + ATC_CFG_S2R_FSM); udelay(5); - writel(readl(c + 0x28) | BIT(0), c + 0x28); - readl_poll_timeout_atomic(c + 0x04, v, v & BIT(8), 1, 1000); + writel(readl(c + ATC_CFG_AHB_CLK) | ATC_CFG_AHB_CLK_REQ_N, c + ATC_CFG_AHB_CLK); + readl_poll_timeout_atomic(c + ATC_CFG_CLK_STATUS, v, v & ATC_CFG_CLK_ACK, 1, 1000); } } @@ -1582,7 +1596,7 @@ static void t6040_fabrics_on(struct t6040_sleep *s) if (!b) continue; v = readl(b + off); - if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != 0xf) + if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != PS_ACTIVE) t6040_ps_raise(b, off); } t6040_atc_commons_on(s); @@ -1623,16 +1637,17 @@ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) u32 to = 0, v; int i; - for (i = 0; i < 16; i++) { + for (i = 0; i < ARRAY_SIZE(t6040_dcs_ps); i++) { void __iomem *r = t6040_dcs_ps_reg(s, i); if (!r) continue; v = readl(r); - if ((v & PS_AUTO_ENABLE) && !(v & GENMASK(19, 16))) + if ((v & PS_AUTO_ENABLE) && !(v & PS_MIN)) continue; - writel((v & ~(GENMASK(19, 16) | PS_FLAGS | PS_TARGET)) | 0xf, r); - if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v) == 0xf, 1, + writel((v & ~(PS_MIN | PS_FLAGS | PS_TARGET)) | + FIELD_PREP(PS_TARGET, PS_ACTIVE), r); + if (readl_poll_timeout_atomic(r, v, FIELD_GET(PS_ACTUAL, v) == PS_ACTIVE, 1, PS_OFF_TIMEOUT_US)) to |= BIT(i); writel(readl(r) | PS_AUTO_ENABLE, r); @@ -1640,18 +1655,21 @@ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) return to; } +#define DCS_PG_CTL 0x20 +#define DCS_PG_DYN (BIT(12) | BIT(8)) + static void t6040_dcs_dyn_pg(struct t6040_sleep *s) { int i; - for (i = 0; i < 16; i++) { + for (i = 0; i < ARRAY_SIZE(s->dcs); i++) { u32 v; if (!s->dcs[i]) continue; - v = readl(s->dcs[i] + 0x20); - if ((v & 0x1100) != 0x1100) - writel(v | 0x1100, s->dcs[i] + 0x20); + v = readl(s->dcs[i] + DCS_PG_CTL); + if ((v & DCS_PG_DYN) != DCS_PG_DYN) + writel(v | DCS_PG_DYN, s->dcs[i] + DCS_PG_CTL); } } @@ -1669,7 +1687,7 @@ static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) u32 a, b; if (!bridge || !s->soc_east || - FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != 0xf) + FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != PS_ACTIVE) return; a = readl(bridge + AXI2AF_BRIDGE_104); b = readl(bridge + AXI2AF_BRIDGE_108); @@ -1959,7 +1977,7 @@ static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) if (!s->soc_east || !FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP))) return; - for (p = 0; p < 2; p++) + for (p = 0; p < ARRAY_SIZE(s->gp_port); p++) t6040_gp_port_stop(s, p); } @@ -1967,7 +1985,7 @@ static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) * apcie_sys_gp (AUTO off, axi2af bit0s, target 0), then apcie_gp. After ANS's ST lanes: the * storage lanes' cio3pll/clkgen/PHY windows sit in apcie_gp, and touching them with it off * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. APCIE_GP goes to clock-gated - * (0x244), the state the sleep engine itself asks for, not to off. + * (TARGET 4), the state the sleep engine itself asks for, not to off. */ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) { @@ -1993,7 +2011,7 @@ static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) writel((val & ~PS_TARGET) | PS_FLAGS, sys); readl_poll_timeout_atomic(sys, val, !FIELD_GET(PS_ACTUAL, val), 1, PS_OFF_TIMEOUT_US); - writel(0x244, gp); + writel(PS_APCIE_GP_CLKGATED, gp); readl_poll_timeout_atomic(gp, val, FIELD_GET(PS_ACTUAL, val) == 4, 1, PS_OFF_TIMEOUT_US); } @@ -2106,7 +2124,7 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) u32 val; int i; - for (i = 0; i < 2; i++) { + for (i = 0; i < ARRAY_SIZE(s->gp_port); i++) { void __iomem *phy = s->gp_phy[i], *port = s->gp_port[i]; u32 cfg, app; @@ -2234,12 +2252,9 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_wake_sync(); if (s->pwrgate) { - writel((readl(s->pwrgate + PWRGATE_CPM_0) & ~PWRGATE_ON) | PWRGATE_ON, - s->pwrgate + PWRGATE_CPM_0); - writel((readl(s->pwrgate + PWRGATE_CPM_1) & ~PWRGATE_ON) | PWRGATE_ON, - s->pwrgate + PWRGATE_CPM_1); - writel((readl(s->pwrgate + PWRGATE_CPM_2) & ~PWRGATE_ON) | PWRGATE_ON, - s->pwrgate + PWRGATE_CPM_2); + writel(readl(s->pwrgate + PWRGATE_CPM_0) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_0); + writel(readl(s->pwrgate + PWRGATE_CPM_1) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_1); + writel(readl(s->pwrgate + PWRGATE_CPM_2) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_2); } t6040_wake_sync(); @@ -2525,7 +2540,7 @@ static int t6040_psw_poll(const struct t6040_psw *w, u32 mask, u32 want) /* * The power-down of a PS word: with - * AUTO_ENABLE set, take it out of automatic mode with the target kept (v & 0xeffffcff) and + * AUTO_ENABLE set, take it out of automatic mode with the target kept and the flags cleared, and * wait for ACTUAL 15; then @pre (the SYS hooks); then TARGET 0 with bits 9:8 written 1 and * bits 10-12, 24-31 kept; then poll bit11 clear and ACTUAL 0. */ @@ -2535,13 +2550,13 @@ static int t6040_psw_off(struct t6040_sleep *s, const struct t6040_psw *w, u32 v = t6040_psw_read(w); if (v & PS_AUTO_ENABLE) { - t6040_psw_write(w, v & 0xeffffcff); + t6040_psw_write(w, v & ~(PS_AUTO_ENABLE | PS_FLAGS)); t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); v = t6040_psw_read(w); } if (pre) pre(s); - t6040_psw_write(w, (v & 0xfffffcf0) | 0x300); + t6040_psw_write(w, (v & ~(PS_FLAGS | PS_TARGET)) | PS_FLAGS); t6040_psw_poll(w, PS_BUSY, 0); return t6040_psw_poll(w, PS_ACTUAL, 0); } @@ -2561,7 +2576,9 @@ static void t6040_disp_sys_pre(struct t6040_sleep *s) } } -/* dart-dcp's outstanding-transaction counters ([20:16]) at 0 before its domain goes. */ +/* dart-dcp's outstanding-transaction counters at 0 before its domain goes. */ +#define DCP_DART_TXN_COUNT GENMASK(20, 16) + static int t6040_dcp_dart_idle(struct t6040_sleep *s) { static const u16 regs[] = { 0x240, 0x244, 0x250, 0x254, 0x260, 0x270 }; @@ -2570,7 +2587,7 @@ static int t6040_dcp_dart_idle(struct t6040_sleep *s) if (!s->dcp_dart) return 0; for (i = 0; i < ARRAY_SIZE(regs); i++) - while (readl(s->dcp_dart + regs[i]) & GENMASK(20, 16)) { + while (readl(s->dcp_dart + regs[i]) & DCP_DART_TXN_COUNT) { if (++us > DISP_PS_TIMEOUT_US) return -ETIMEDOUT; udelay(1); @@ -2635,7 +2652,7 @@ static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, bool s u32 v = t6040_psw_read(w); int ret; - t6040_psw_write(w, (v & ~(PS_TARGET | PS_AUTO_ENABLE | PS_FLAGS)) | PS_TARGET); + t6040_psw_write(w, (v & ~(PS_AUTO_ENABLE | PS_FLAGS)) | PS_TARGET); ret = t6040_psw_poll(w, PS_ACTUAL, PS_ACTUAL); t6040_psw_poll(w, PS_BUSY, 0); if (sys) { @@ -3042,7 +3059,7 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) return; if (!of_device_is_available(host)) goto out; - for (p = 0; p < 2; p++) { + for (p = 0; p < ARRAY_SIZE(s->gp_port); p++) { idx = of_property_match_string(host, "reg-names", ports[p]); if (idx >= 0 && !of_address_to_resource(host, idx, &res)) s->gp_port[p] = t6040_ioremap_np(s->dev, res.start, resource_size(&res)); @@ -3127,11 +3144,11 @@ static int t6040_sleep_probe(struct platform_device *pdev) sleep->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); sleep->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); - for (i = 0; i < 16; i++) + for (i = 0; i < ARRAY_SIZE(sleep->dcs); i++) sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, 0x2000); - for (i = 0; i < 4; i++) + for (i = 0; i < ARRAY_SIZE(sleep->atc_cfg); i++) sleep->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, 0x383200000ULL + i * 0x8000000ULL, 0x100); sleep->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); From 55882b95c7694c769fb363c56f50457bdcf48f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:04 +0100 Subject: [PATCH 102/269] soc: apple: sleep: t6040: group the definitions and attach the comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The AGX gate, quiesce timeout, FAB2_SOC, WDT address and display CPU gate definitions sat between unrelated blocks or next to their first use; put each with its block. - The clock quiesce had two block comments and the CPM voters' "not written on entry" note two copies; keep one of each. - The comment on ANS's storage link quiesce floated above another function under an "ENTRY:" label; put it on the function it describes. The wake return repeated the storage link raise's own comment; refer to it instead. - Index the axi2af domain table with an enum and designated initialisers, instead of three bare positions. - The external debug blocks use the CPU's cluster and core helpers and the cluster table's size. - Move the preflight below the function it calls, so that the forward declaration goes. No functional change: the object is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 196 ++++++++++++++++---------------- 1 file changed, 96 insertions(+), 100 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 0a7df8b302b44..dee47a8eda735 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -106,6 +106,7 @@ #define PS_APCIE_SYS_ST0 0x380 /* SOC-East: ps_apcie_sys_st0 */ #define PS_APCIE_SYS_ST1 0x388 /* SOC-East: ps_apcie_sys_st1 */ #define PS_APCIE_PHY_SW 0x398 /* SOC-East: ps_apcie_phy_sw (the shared PHY) */ +#define PS_FAB2_SOC 0x338 /* SOC: the ATC commons' parent */ /* The clock-gated words (TARGET 4) the commit writes, as the sleep engine asks for them. */ #define PS_APCIE_PHY_SW_CLKGATED 0x0f000244 #define PS_APCIE_GP_CLKGATED 0x00000244 @@ -149,29 +150,21 @@ #define SOC_EAST_TIMEOUT_US 5000 /* - * ClkGen SOC (pmgr reg3 = 0x500000000). The clk-mux park words - * (0x500038050/054/060/064) have bits [27:24] cleared at quiesce (observed - * 0xa5100000 / 0xa7100000 -> 0xa0100000), with BIT31 set, each polled until - * BIT30 clears. This is the clock quiesce, NOT measured on the J616s: - * the one measured entry did not cover the clkgen block. Wake restores - * the saved words (the clock restore is otherwise a NOP). + * The clock quiesce, whole and nothing else: two SOC-East words lose BIT28 and are + * polled until [7:4] reads their [3:0]; then the four clk-mux park words of ClkGen SOC (ADT + * pmgr reg[3] = 0x500000000, +0x38050/054/060/064), in t6040_clkgen_mux[] order, get [27:24] + * := 0 with BIT31 set, each polled until BIT30 clears. The wake writes the saved clk-mux + * words back; there is nothing else to restore. Not measured on the J616s. */ +#define SOC_EAST_CLK_A 0x120 +#define SOC_EAST_CLK_B 0x130 +#define SOC_EAST_CLK_AUTO BIT(28) #define CLKGEN_MUX_SET 0x80000000 #define CLKGEN_MUX_MASK 0xf0ffffff #define CLKGEN_MUX_WORDS 4 #define CLKGEN_MUX_PENDING BIT(30) #define CLK_QUIESCE_TIMEOUT_US 1000 -/* - * The clock quiesce, whole: two SOC-East - * words lose BIT28 and are polled until [7:4] reads their [3:0], then the four - * clk-mux words, in this order, get [27:24] := 0 with BIT31 and are polled until - * BIT30 clears. Nothing else (no +0x70000 write). - */ -#define SOC_EAST_CLK_A 0x120 -#define SOC_EAST_CLK_B 0x130 -#define SOC_EAST_CLK_AUTO BIT(28) - /* * The keep-alive enables: 32 words in PMGR SOC * from +0x1dc000 and 32 in SOC-East from +0x50000, stride 0x44, BIT0 the keep-alive. @@ -200,6 +193,9 @@ * and [7:4] polled to 0. */ #define T6040_AGX_GATE_PA 0x508375010ULL +#define AGX_GATE_RESIDENT GENMASK(19, 18) +#define T6040_AFR_PS_PA 0x288e80100ULL +#define AGX_TIMEOUT_US 10000 /* * The hardware steps of the generic PMGR quiesce that run on J616s before @@ -218,6 +214,7 @@ static const u64 t6040_acc_apsc_pa[] = { 0x210e20020ULL, 0x211e20020ULL, 0x212e2 #define ACC_APSC_BUSY BIT_ULL(7) #define ACC_PLL_RELOCK BIT_ULL(42) #define ACC_PLL_RELOCK_COMPLEXES 2 +#define QUIESCE_TIMEOUT_US 2000 static const struct { u64 ctl; @@ -242,12 +239,6 @@ static const struct { #define T6040_PMC_BWR_PA 0x5048ac020ULL #define PMC_BOOST 0x8 -#define QUIESCE_TIMEOUT_US 2000 - -#define AGX_GATE_RESIDENT GENMASK(19, 18) -#define T6040_AFR_PS_PA 0x288e80100ULL -#define AGX_TIMEOUT_US 10000 - /* * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000), written as on the T8140. * Measured on the J616s: the boot stage is set to 0x50 by an extended write whose @@ -290,17 +281,34 @@ static const struct { #define DISP_PWRGATE_B 0x60 /* := 0 before DISP_SYS goes down, 1 after it is up */ #define PS_DISP_FE 0x368 /* soc_east */ #define PS_DISP_SYS 0x258 /* soc_east */ -#define AXI2AF_DISP_SYS 4 /* t6040_axi2af[] index */ #define DISP_PS_TIMEOUT_US 8000 +#define DISP_CPU_GATE_US 50000 /* The GP PCIe (t602x port layout, as T8140's radio port): power words in soc_east. */ #define PS_APCIE_GP 0x100 #define PS_APCIE_SYS_GP 0x390 -#define AXI2AF_ANS 9 -#define AXI2AF_APCIE_SYS_GP 14 #define GP_PIN_DATA BIT(0) #define T6040_PMC_DCP_PA 0x5048a9c14ULL /* count of DCPs powered (+0), second voter (+4); the platform ends at 0/0 */ -#define T6040_AXI2AF_DOMAINS 15 + +/* The axi2af fabric-bridge retention domains, t6040_axi2af[] */ +enum { + AXI2AF_ATC0_COMMON, + AXI2AF_ATC1_COMMON, + AXI2AF_ATC2_COMMON, + AXI2AF_ATC3_COMMON, + AXI2AF_DISP_SYS, + AXI2AF_DISPEXT0_SYS, + AXI2AF_DISPEXT1_SYS, + AXI2AF_DISPEXT2_SYS, + AXI2AF_DISPEXT3_SYS, + AXI2AF_ANS, + AXI2AF_AVD_SYS, + AXI2AF_VENC0_SYS, + AXI2AF_VENC1_SYS, + AXI2AF_JPG, + AXI2AF_APCIE_SYS_GP, + T6040_AXI2AF_DOMAINS +}; /* * The AON watchdog's WD2, the sleep safety net: ADT wdt reg[0] = 0x50836c000, with the @@ -308,6 +316,7 @@ static const struct { * ALARM is 32 bits at 24 MHz. */ #define WDT_MAX_SECS 178U +#define T6040_WDT_PA 0x50836c000ULL /* the address t6040_resume_stub writes */ struct t6040_sleep_group { const char *name; @@ -598,32 +607,6 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct return blockers + disp; } -static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s); - -static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) -{ - struct t6040_sleep *sleep = s->private; - unsigned int live = t6040_sleep_live_blockers(sleep, s, NULL); - unsigned int i; - - apple_sleep_registry_show(s); - t6040_axi2af_show(sleep, s); - seq_printf(s, "verdict: %u live blocker(s)\n", live); - seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", - READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, - sleep->power_ops ? "registered" : "not registered"); - if (sleep->cluster_ctl) { - static const char * const nm[] = { "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT" }; - - for (i = 0; i < CLUSTER_CTL_NR; i++) - seq_printf(s, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], - readl(sleep->cluster_ctl + CLUSTER_CTL(i))); - } - - return 0; -} -DEFINE_SHOW_ATTRIBUTE(t6040_sleep_preflight); - /* The display groups' on-state, one line per group, no allocation. For the log at the commit point. */ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) { @@ -704,8 +687,8 @@ static u32 t6040_cpu_start_bit(u64 mpidr) #define ED_EDPRCR 0x310 #define ED_EDLAR 0xfb0 #define ED_LOCK_ACCESS_KEY 0xc5acce55 -#define T6040_ED_CLUSTERS 3 -#define T6040_ED_CORES 6 +#define T6040_ED_CLUSTERS ARRAY_SIZE(t6040_cluster_slots) +#define T6040_ED_CORES 6 /* the most slots of a cluster */ static void __iomem *t6040_ed[T6040_ED_CLUSTERS][T6040_ED_CORES]; @@ -715,7 +698,7 @@ static void t6040_ed_map(struct device *dev) for_each_possible_cpu(cpu) { u64 mpidr = cpu_logical_map(cpu); - unsigned int c = MPIDR_AFFINITY_LEVEL(mpidr, 1), k = MPIDR_AFFINITY_LEVEL(mpidr, 0); + unsigned int c = t6040_cpu_cluster(mpidr), k = t6040_cpu_core(mpidr); if (c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES || t6040_ed[c][k]) continue; @@ -729,7 +712,7 @@ static void t6040_ed_map(struct device *dev) static void t6040_ed_release(void) { u64 mpidr = read_cpuid_mpidr(); - unsigned int c = MPIDR_AFFINITY_LEVEL(mpidr, 1), k = MPIDR_AFFINITY_LEVEL(mpidr, 0); + unsigned int c = t6040_cpu_cluster(mpidr), k = t6040_cpu_core(mpidr); void __iomem *ed; if (c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES) @@ -1104,21 +1087,21 @@ static const struct t6040_axi2af_domain { u32 ps_off; phys_addr_t bridge_pa; } t6040_axi2af[] = { - { "atc0_common", false, 0x390, 0x380000000 }, - { "atc1_common", false, 0x3b8, 0x388000000 }, - { "atc2_common", false, 0x3e0, 0x390000000 }, - { "atc3_common", false, 0x408, 0x398000000 }, - { "disp_sys", true, 0x258, 0x403000000 }, - { "dispext0_sys", false, 0x340, 0x3a3000000 }, - { "dispext1_sys", false, 0x490, 0x603000000 }, - { "dispext2_sys", false, 0x4e0, 0x607000000 }, - { "dispext3_sys", false, 0x2d8, 0x307000000 }, - { "ans", true, 0x370, 0x40b000000 }, - { "avd_sys", false, 0x170, 0x280000000 }, - { "venc0_sys", false, 0x278, 0x282000000 }, - { "venc1_sys", false, 0x2a0, 0x284000000 }, - { "jpg", true, 0x3a0, 0x486000000 }, - { "apcie_sys_gp", true, 0x390, 0x416000000 }, + [AXI2AF_ATC0_COMMON] = { "atc0_common", false, 0x390, 0x380000000 }, + [AXI2AF_ATC1_COMMON] = { "atc1_common", false, 0x3b8, 0x388000000 }, + [AXI2AF_ATC2_COMMON] = { "atc2_common", false, 0x3e0, 0x390000000 }, + [AXI2AF_ATC3_COMMON] = { "atc3_common", false, 0x408, 0x398000000 }, + [AXI2AF_DISP_SYS] = { "disp_sys", true, 0x258, 0x403000000 }, + [AXI2AF_DISPEXT0_SYS] = { "dispext0_sys", false, 0x340, 0x3a3000000 }, + [AXI2AF_DISPEXT1_SYS] = { "dispext1_sys", false, 0x490, 0x603000000 }, + [AXI2AF_DISPEXT2_SYS] = { "dispext2_sys", false, 0x4e0, 0x607000000 }, + [AXI2AF_DISPEXT3_SYS] = { "dispext3_sys", false, 0x2d8, 0x307000000 }, + [AXI2AF_ANS] = { "ans", true, 0x370, 0x40b000000 }, + [AXI2AF_AVD_SYS] = { "avd_sys", false, 0x170, 0x280000000 }, + [AXI2AF_VENC0_SYS] = { "venc0_sys", false, 0x278, 0x282000000 }, + [AXI2AF_VENC1_SYS] = { "venc1_sys", false, 0x2a0, 0x284000000 }, + [AXI2AF_JPG] = { "jpg", true, 0x3a0, 0x486000000 }, + [AXI2AF_APCIE_SYS_GP] = { "apcie_sys_gp", true, 0x390, 0x416000000 }, }; /* Only with the PMU's bus mapped. */ @@ -1327,6 +1310,32 @@ static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s) } } +static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) +{ + struct t6040_sleep *sleep = s->private; + unsigned int live = t6040_sleep_live_blockers(sleep, s, NULL); + unsigned int i; + + apple_sleep_registry_show(s); + t6040_axi2af_show(sleep, s); + seq_printf(s, "verdict: %u live blocker(s)\n", live); + seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", + READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, + sleep->power_ops ? "registered" : "not registered"); + if (sleep->cluster_ctl) { + static const char * const nm[] = { + "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT", + }; + + for (i = 0; i < CLUSTER_CTL_NR; i++) + seq_printf(s, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], + readl(sleep->cluster_ctl + CLUSTER_CTL(i))); + } + + return 0; +} +DEFINE_SHOW_ATTRIBUTE(t6040_sleep_preflight); + /* * Read-only axi2af gate: on entry the platform panics unless every reachable domain's * power word reads [7:4]==0. Here the ones still on are only named, at debug level. @@ -1440,19 +1449,6 @@ static bool t6040_ps_actual_on(void __iomem *base, u32 offset) return FIELD_GET(PS_ACTUAL, readl(base + offset)); } -/* - * ENTRY: quiesce ANS's storage link, once ANS has been stopped by its driver. The PHY, the - * clock generator and the AXI enables are treated as shared by the two ST lanes. - * - * The power words of this group are left to the SoC's sleep engine, which powers it down - * itself and in its own order: APCIE_PHY_SW to clock-gated, APCIE_SYS_ST0/1 off, APCIE_ST0/1 - * clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP clock-gated. It polls APCIE_PHY_SW for - * its parents, which are both SYS_ST lanes: with them lowered here, as the T8140 controller - * does, it waited there for good on the J616s. So the commit raises APCIE_SYS_ST1, which - * Linux never uses, and does only what the engine needs before it runs: the shared PHY put - * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. - */ - /* The shared PHY and PHY-IP to sleep (the root complex disable; it polls unbounded). */ static void t6040_apcie_phy_off(struct t6040_sleep *s) { @@ -1510,8 +1506,6 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) * was off as well. FAB2_SOC is raised by hand, then ATC0..3_COMMON to a manual target 0xf, * then FAB2_SOC gets its automatic mode back: the powered children hold it up. */ -#define PS_FAB2_SOC 0x338 - static const u16 t6040_atc_common[4] = { 0x390, 0x3b8, 0x3e0, 0x408 }; static void t6040_atc_commons_on(struct t6040_sleep *s) @@ -1697,6 +1691,18 @@ static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) writel(b & ~BIT(0), bridge + AXI2AF_BRIDGE_108); } +/* + * Quiesce ANS's storage link, once ANS has been stopped by its driver. The PHY, the + * clock generator and the AXI enables are treated as shared by the two ST lanes. + * + * The power words of this group are left to the SoC's sleep engine, which powers it down + * itself and in its own order: APCIE_PHY_SW to clock-gated, APCIE_SYS_ST0/1 off, APCIE_ST0/1 + * clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP clock-gated. It polls APCIE_PHY_SW for + * its parents, which are both SYS_ST lanes: with them lowered here, as the T8140 controller + * does, it waited there for good on the J616s. So the commit raises APCIE_SYS_ST1, which + * Linux never uses, and does only what the engine needs before it runs: the shared PHY put + * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. + */ static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; @@ -1729,7 +1735,7 @@ static void t6040_bridge_rearm(void __iomem *bridge) } /* - * RESUME: raise ANS's storage link from power-off, the two-lane adaptation of + * Raise ANS's storage link from power-off, the two-lane adaptation of * sleep.c's apple_sleep_raise_apcie(). Runs atomic on the waking core in * t6040_sleep_return(), AFTER t6040_axi2af_rearm() and BEFORE control returns to * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. @@ -1851,9 +1857,8 @@ static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) * The first thing Linux runs after a wake, MMU off and no stack (as the T8132 stub does). The * last wake program leaves the AON WD2 running for 5 s, less than the device resume takes: * restart it for 60 s, in t6040_sleep_set_wd2()'s order, then take cpu_resume. The PA is - * the T6040 AON WDT (0x50836c000), used only when the DT's wdt block is that one. + * the T6040 AON WDT (T6040_WDT_PA), used only when the DT's wdt block is that one. */ -#define T6040_WDT_PA 0x50836c000ULL asm( " .pushsection .text, \"ax\"\n" " .type t6040_resume_stub, %function\n" @@ -2071,9 +2076,7 @@ static int t6040_sleep_commit(unsigned long unused) /* * The order of the PMGR quiesce (the cluster and axi2af * parts measured on the J616s): the five complexes off, the axi2af gate read, - * the clock quiesce, the keep-alive bits off, then the S2R timer. The CPM - * pwrgate voters are not written on entry: they read back 0x80000000 - * already; the wake side re-asserts them. + * the clock quiesce, the keep-alive bits off, then the S2R timer. */ t6040_secondaries_off(s); t6040_pmgr_quiesce(s); @@ -2236,12 +2239,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_axi2af_rearm(s); t6040_wake_sync(); - /* - * Raise ANS's two ST lanes + shared PHY from power-off, AFTER the fabric - * bridges are re-armed and BEFORE control returns to the PM core, so - * nvme's resume_early sets the ANS run bit onto a live link. - * No-op unless the apple,apcie windows are present. - */ + /* ANS's storage link, after the bridges and before nvme's resume_early (see there). */ s->apcie_wake_step = 0; s->pcie_ans_on_ret = 0; if (s->pcie_ans_stopped) @@ -2467,8 +2465,6 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) return -EBUSY; } -#define DISP_CPU_GATE_US 50000 - /* * regmap_read_poll_timeout_atomic() reads ktime_get(), which warns while timekeeping is * suspended (12 warnings from .enter on the J616s). Counted 1 us steps instead. From 9f3679b6f1d50a10953821bab6a2d2b9102274dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:04 +0100 Subject: [PATCH 103/269] soc: apple: sleep: t6040: call the state s and the seq_file m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The driver's state was "sleep" in some functions and "s" in the others, where a seq_file was "s" too. Call the state "s" and the seq_file "m" throughout. No functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 218 ++++++++++++++++---------------- 1 file changed, 109 insertions(+), 109 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index dee47a8eda735..c6898e93a1fd6 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -516,7 +516,7 @@ static bool t6040_sleep_ps_on(u32 val, bool cpu) return !cpu && FIELD_GET(PS_TARGET, val); } -static unsigned int t6040_sleep_show_group(struct seq_file *s, const struct t6040_sleep_group *g) +static unsigned int t6040_sleep_show_group(struct seq_file *m, const struct t6040_sleep_group *g) { unsigned int on = 0; u32 off; @@ -529,16 +529,16 @@ static unsigned int t6040_sleep_show_group(struct seq_file *s, const struct t604 ret = regmap_read(g->regmap, off, &val); if (ret) { /* Unreadable counts as on: the refusal fails closed. */ - if (s) - seq_printf(s, " %-12s +%#05x read failed (%d)\n", g->name, off, + if (m) + seq_printf(m, " %-12s +%#05x read failed (%d)\n", g->name, off, ret); on++; continue; } if (t6040_sleep_ps_on(val, cpu)) on++; - if (s) - seq_printf(s, " %-12s +%#05x %s %#010x target %lu actual %lu%s\n", g->name, + if (m) + seq_printf(m, " %-12s +%#05x %s %#010x target %lu actual %lu%s\n", g->name, off, cpu ? "cpu " : "child", val, FIELD_GET(PS_TARGET, val), FIELD_GET(PS_ACTUAL, val), t6040_sleep_ps_on(val, cpu) ? " ON" : ""); @@ -572,33 +572,33 @@ static unsigned int t6040_sleep_parkable(const char **name) * .suspend stops the DCP and lowers its domains), so .begin excludes them and * only .enter, after the device suspend, enforces them. */ -static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct seq_file *s, +static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *s, struct seq_file *m, unsigned int *disp_out) { /* Mapped and aura's: after a resume from disk, this boot's (t6040_sleep_restore()). */ - bool mailbox = sleep->mbox; + bool mailbox = s->mbox; bool el2 = is_kernel_in_hyp_mode(); unsigned int i, blockers = 0, disp = 0, parkable; const char *ops_name; parkable = t6040_sleep_parkable(&ops_name); - if (s) { - seq_printf(s, "kernel at EL2: %s\n", el2 ? "yes" : "no"); - seq_printf(s, "boot object return path (/chosen/aurora,s2r-mailbox): %s\n", + if (m) { + seq_printf(m, "kernel at EL2: %s\n", el2 ? "yes" : "no"); + seq_printf(m, "boot object return path (/chosen/aurora,s2r-mailbox): %s\n", mailbox ? "yes" : "no"); - seq_printf(s, "secondaries that can leave the kernel: %u of %u (%s)\n", parkable, + seq_printf(m, "secondaries that can leave the kernel: %u of %u (%s)\n", parkable, num_possible_cpus() - 1, ops_name); } blockers += !el2 + !mailbox + (parkable != num_possible_cpus() - 1); - if (s) - seq_puts(s, "power states checked before sleep (device-check-ps-b4-sleep):\n"); - for (i = 0; i < sleep->ngroups; i++) { - unsigned int on = t6040_sleep_show_group(s, &sleep->groups[i]); + if (m) + seq_puts(m, "power states checked before sleep (device-check-ps-b4-sleep):\n"); + for (i = 0; i < s->ngroups; i++) { + unsigned int on = t6040_sleep_show_group(m, &s->groups[i]); - if (s) - seq_printf(s, " %-12s %u on\n", sleep->groups[i].name, on); + if (m) + seq_printf(m, " %-12s %u on\n", s->groups[i].name, on); disp += !!on; } @@ -608,12 +608,12 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *sleep, struct } /* The display groups' on-state, one line per group, no allocation. For the log at the commit point. */ -static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) +static unsigned int t6040_sleep_log_groups(struct t6040_sleep *s) { unsigned int i, blockers = 0; - for (i = 0; i < sleep->ngroups; i++) { - const struct t6040_sleep_group *g = &sleep->groups[i]; + for (i = 0; i < s->ngroups; i++) { + const struct t6040_sleep_group *g = &s->groups[i]; u32 off, on_mask = 0, val; unsigned int bit = 0; @@ -634,12 +634,12 @@ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *sleep) } /* Log the blockers outside the display, for a refused or dry .begin. */ -static void t6040_sleep_log_blockers(struct t6040_sleep *sleep) +static void t6040_sleep_log_blockers(struct t6040_sleep *s) { const char *ops_name; pr_info("t6040-sleep: EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", - is_kernel_in_hyp_mode() ? "yes" : "no", sleep->mbox ? "yes" : "no", + is_kernel_in_hyp_mode() ? "yes" : "no", s->mbox ? "yes" : "no", t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); } @@ -1296,40 +1296,40 @@ static void __iomem *t6040_axi2af_ps(struct t6040_sleep *s, const struct t6040_a } /* The preflight's list of the axi2af domains' power words. */ -static void t6040_axi2af_show(struct t6040_sleep *sleep, struct seq_file *s) +static void t6040_axi2af_show(struct t6040_sleep *s, struct seq_file *m) { int i; - seq_puts(s, "axi2af domains (the quiesce gate wants every one off):\n"); + seq_puts(m, "axi2af domains (the quiesce gate wants every one off):\n"); for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) { - void __iomem *ps = t6040_axi2af_ps(sleep, &t6040_axi2af[i]); + void __iomem *ps = t6040_axi2af_ps(s, &t6040_axi2af[i]); u32 val = ps ? readl(ps) : 0; - seq_printf(s, " %-13s %#010x%s\n", t6040_axi2af[i].name, val, + seq_printf(m, " %-13s %#010x%s\n", t6040_axi2af[i].name, val, ps ? (FIELD_GET(PS_ACTUAL, val) ? " ON" : "") : " (unmapped)"); } } -static int t6040_sleep_preflight_show(struct seq_file *s, void *unused) +static int t6040_sleep_preflight_show(struct seq_file *m, void *unused) { - struct t6040_sleep *sleep = s->private; - unsigned int live = t6040_sleep_live_blockers(sleep, s, NULL); + struct t6040_sleep *s = m->private; + unsigned int live = t6040_sleep_live_blockers(s, m, NULL); unsigned int i; - apple_sleep_registry_show(s); - t6040_axi2af_show(sleep, s); - seq_printf(s, "verdict: %u live blocker(s)\n", live); - seq_printf(s, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", + apple_sleep_registry_show(m); + t6040_axi2af_show(s, m); + seq_printf(m, "verdict: %u live blocker(s)\n", live); + seq_printf(m, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, - sleep->power_ops ? "registered" : "not registered"); - if (sleep->cluster_ctl) { + s->power_ops ? "registered" : "not registered"); + if (s->cluster_ctl) { static const char * const nm[] = { "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT", }; for (i = 0; i < CLUSTER_CTL_NR; i++) - seq_printf(s, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], - readl(sleep->cluster_ctl + CLUSTER_CTL(i))); + seq_printf(m, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], + readl(s->cluster_ctl + CLUSTER_CTL(i))); } return 0; @@ -2541,7 +2541,7 @@ static int t6040_psw_poll(const struct t6040_psw *w, u32 mask, u32 want) * bits 10-12, 24-31 kept; then poll bit11 clear and ACTUAL 0. */ static int t6040_psw_off(struct t6040_sleep *s, const struct t6040_psw *w, - void (*pre)(struct t6040_sleep *s)) + void (*pre)(struct t6040_sleep *s)) { u32 v = t6040_psw_read(w); @@ -2643,7 +2643,7 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group * AUTO_ENABLE back where it belongs (DISP_CPU, DISP_SYS). */ static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, bool sys, - bool autoen) + bool autoen) { u32 v = t6040_psw_read(w); int ret; @@ -2841,46 +2841,46 @@ static const struct platform_suspend_ops t6040_sleep_ops = { .end = t6040_sleep_end, }; -static int t6040_sleep_parse_groups(struct t6040_sleep *sleep) +static int t6040_sleep_parse_groups(struct t6040_sleep *s) { - struct device_node *np = sleep->dev->of_node; + struct device_node *np = s->dev->of_node; const char *prop = "apple,check-ps-b4-sleep"; int n, i; /* Each group: the PMGR syscon, the CPU's offset and the last sub-block's. */ n = of_property_count_u32_elems(np, prop); if (n <= 0 || n % 3) - return dev_err_probe(sleep->dev, -EINVAL, "no valid %s\n", prop); + return dev_err_probe(s->dev, -EINVAL, "no valid %s\n", prop); n /= 3; if (of_property_count_strings(np, "apple,check-ps-b4-sleep-names") != n) - return dev_err_probe(sleep->dev, -EINVAL, "one name per %s group\n", prop); + return dev_err_probe(s->dev, -EINVAL, "one name per %s group\n", prop); - sleep->groups = devm_kcalloc(sleep->dev, n, sizeof(*sleep->groups), GFP_KERNEL); - if (!sleep->groups) + s->groups = devm_kcalloc(s->dev, n, sizeof(*s->groups), GFP_KERNEL); + if (!s->groups) return -ENOMEM; for (i = 0; i < n; i++) { - struct t6040_sleep_group *g = &sleep->groups[i]; + struct t6040_sleep_group *g = &s->groups[i]; struct of_phandle_args args; int ret; ret = of_parse_phandle_with_fixed_args(np, prop, 2, i, &args); if (ret) - return dev_err_probe(sleep->dev, ret, "%s[%d]\n", prop, i); + return dev_err_probe(s->dev, ret, "%s[%d]\n", prop, i); g->regmap = syscon_node_to_regmap(args.np); of_node_put(args.np); if (IS_ERR(g->regmap)) - return dev_err_probe(sleep->dev, PTR_ERR(g->regmap), "%s[%d] regmap\n", + return dev_err_probe(s->dev, PTR_ERR(g->regmap), "%s[%d] regmap\n", prop, i); g->first = args.args[0]; g->last = args.args[1]; if (g->first > g->last || (g->last - g->first) % PS_STRIDE || (g->last - g->first) / PS_STRIDE >= PS_MAX_PER_GROUP) - return dev_err_probe(sleep->dev, -EINVAL, "%s[%d]: bad range %#x..%#x\n", + return dev_err_probe(s->dev, -EINVAL, "%s[%d]: bad range %#x..%#x\n", prop, i, g->first, g->last); of_property_read_string_index(np, "apple,check-ps-b4-sleep-names", i, &g->name); } - sleep->ngroups = n; + s->ngroups = n; return 0; } @@ -3095,19 +3095,19 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) static int t6040_sleep_probe(struct platform_device *pdev) { - struct t6040_sleep *sleep; + struct t6040_sleep *s; struct dentry *dir; int ret, i; /* This sleep was tested with neither the lid's state at the wake nor USB wake ports. */ apple_sleep_set_part_plain_wake(); - sleep = devm_kzalloc(&pdev->dev, sizeof(*sleep), GFP_KERNEL); - if (!sleep) + s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL); + if (!s) return -ENOMEM; - sleep->dev = &pdev->dev; + s->dev = &pdev->dev; - ret = t6040_sleep_parse_groups(sleep); + ret = t6040_sleep_parse_groups(s); if (ret) return ret; @@ -3119,70 +3119,70 @@ static int t6040_sleep_probe(struct platform_device *pdev) BUILD_BUG_ON(ARRAY_SIZE(t6040_axi2af) != T6040_AXI2AF_DOMAINS); BUILD_BUG_ON(ARRAY_SIZE(t6040_tvm) != ARRAY_SIZE(((struct t6040_sleep *)0)->tvm_ctl)); BUILD_BUG_ON(ARRAY_SIZE(t6040_acc_apsc_pa) != ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); - sleep->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); - sleep->wdt = t6040_sleep_map(&pdev->dev, "wdt"); + s->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); + s->wdt = t6040_sleep_map(&pdev->dev, "wdt"); { struct resource wres; int widx = of_property_match_string(pdev->dev.of_node, "reg-names", "wdt"); - sleep->wdt_stub_ok = widx >= 0 && + s->wdt_stub_ok = widx >= 0 && !of_address_to_resource(pdev->dev.of_node, widx, &wres) && wres.start == T6040_WDT_PA; - if (!sleep->wdt_stub_ok) + if (!s->wdt_stub_ok) dev_warn(&pdev->dev, "the wdt block is not the AON watchdog: no WD2 restart at the wake\n"); } - sleep->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); - sleep->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); - sleep->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); - sleep->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); - sleep->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); - sleep->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); - sleep->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); - sleep->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); - for (i = 0; i < ARRAY_SIZE(sleep->dcs); i++) - sleep->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + - (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, - 0x2000); - for (i = 0; i < ARRAY_SIZE(sleep->atc_cfg); i++) - sleep->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, - 0x383200000ULL + i * 0x8000000ULL, 0x100); - sleep->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); - sleep->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); - sleep->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); - sleep->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); + s->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); + s->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); + s->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); + s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); + s->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); + s->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); + s->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); + s->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); + for (i = 0; i < ARRAY_SIZE(s->dcs); i++) + s->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, + 0x2000); + for (i = 0; i < ARRAY_SIZE(s->atc_cfg); i++) + s->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, + 0x383200000ULL + i * 0x8000000ULL, 0x100); + s->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); + s->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); + s->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); + s->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) - sleep->axi2af_bridge[i] = t6040_ioremap_np(&pdev->dev, + s->axi2af_bridge[i] = t6040_ioremap_np(&pdev->dev, t6040_axi2af[i].bridge_pa, 0x200); - t6040_sleep_map_apcie(sleep); - t6040_sleep_map_mailbox(sleep); - t6040_sleep_watch_init(sleep, T6040_WATCH_SEP, "apple,sep", "sep"); - t6040_sleep_watch_init(sleep, T6040_WATCH_ANS, "apple,t6041-nvme-ans2", "ans"); + t6040_sleep_map_apcie(s); + t6040_sleep_map_mailbox(s); + t6040_sleep_watch_init(s, T6040_WATCH_SEP, "apple,sep", "sep"); + t6040_sleep_watch_init(s, T6040_WATCH_ANS, "apple,t6041-nvme-ans2", "ans"); /* Fixed PAs: dart-dcp's counters and the internal display's retention pair. */ - sleep->dcp_dart = t6040_ioremap_np(&pdev->dev, T6040_DCP_DART_PA, 0x1000); - sleep->disp_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISP_PWRGATE_PA, 0x100); - sleep->pmc_dcp = t6040_ioremap_np(&pdev->dev, T6040_PMC_DCP_PA, 12); - sleep->pmc_bwr = t6040_ioremap_np(&pdev->dev, T6040_PMC_BWR_PA, 0x10); - sleep->ka_soc = t6040_ioremap_np(&pdev->dev, T6040_KA_SOC_PA, KA_SIZE); - sleep->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); - sleep->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); - sleep->agx_gate = t6040_ioremap_np(&pdev->dev, T6040_AGX_GATE_PA, 4); - sleep->s2r_lo_count = t6040_ioremap_np(&pdev->dev, T6040_S2R_LO_COUNT_PA, 4); + s->dcp_dart = t6040_ioremap_np(&pdev->dev, T6040_DCP_DART_PA, 0x1000); + s->disp_pwrgate = t6040_ioremap_np(&pdev->dev, T6040_DISP_PWRGATE_PA, 0x100); + s->pmc_dcp = t6040_ioremap_np(&pdev->dev, T6040_PMC_DCP_PA, 12); + s->pmc_bwr = t6040_ioremap_np(&pdev->dev, T6040_PMC_BWR_PA, 0x10); + s->ka_soc = t6040_ioremap_np(&pdev->dev, T6040_KA_SOC_PA, KA_SIZE); + s->ka_east = t6040_ioremap_np(&pdev->dev, T6040_KA_EAST_PA, KA_SIZE); + s->smc_wdt_deadline = t6040_ioremap_np(&pdev->dev, T6040_SMC_WDT_DEADLINE_PA, 4); + s->agx_gate = t6040_ioremap_np(&pdev->dev, T6040_AGX_GATE_PA, 4); + s->s2r_lo_count = t6040_ioremap_np(&pdev->dev, T6040_S2R_LO_COUNT_PA, 4); for (i = 0; i < ARRAY_SIZE(t6040_acc_apsc_pa); i++) - sleep->acc_apsc[i] = t6040_ioremap_np(&pdev->dev, t6040_acc_apsc_pa[i], 8); + s->acc_apsc[i] = t6040_ioremap_np(&pdev->dev, t6040_acc_apsc_pa[i], 8); for (i = 0; i < ARRAY_SIZE(t6040_tvm); i++) { - sleep->tvm_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].ctl, 4); - sleep->tvm_status[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].status, 4); + s->tvm_ctl[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].ctl, 4); + s->tvm_status[i] = t6040_ioremap_np(&pdev->dev, t6040_tvm[i].status, 4); } - sleep->afr_ps = t6040_ioremap_np(&pdev->dev, T6040_AFR_PS_PA, 4); - t6040_sleep_map_gp(sleep); + s->afr_ps = t6040_ioremap_np(&pdev->dev, T6040_AFR_PS_PA, 4); + t6040_sleep_map_gp(s); t6040_ed_map(&pdev->dev); - platform_set_drvdata(pdev, sleep); - t6040_sleep = sleep; + platform_set_drvdata(pdev, s); + t6040_sleep = s; dir = debugfs_create_dir("t6040-sleep", NULL); - debugfs_create_file("preflight", 0400, dir, sleep, &t6040_sleep_preflight_fops); + debugfs_create_file("preflight", 0400, dir, s, &t6040_sleep_preflight_fops); /* * The CPU power ops act only when a commit stops the parked secondaries, and a dry run @@ -3191,19 +3191,19 @@ static int t6040_sleep_probe(struct platform_device *pdev) * keeps refusing parking CPUs as before. Parking itself needs no power ops. */ if (allow_entry) - t6040_sleep_power_ops(sleep); + t6040_sleep_power_ops(s); suspend_set_ops(&t6040_sleep_ops); dev_info(&pdev->dev, "refusal-first: %u display groups, cpu-start %s, wdt %s, commit/wake blocks %s, storage-apcie %s, s2r-mailbox %s, parking ops %s, dry run %u\n", - sleep->ngroups, sleep->cpu_start ? "mapped" : "absent", - sleep->wdt ? "mapped" : "absent", - (sleep->cluster_ctl && sleep->pwrgate && sleep->mastermini && sleep->spmi0 && - sleep->soc_east && sleep->aop_global) ? "mapped" : "incomplete", - sleep->apcie_st.valid ? "mapped" : "absent", - sleep->mbox ? "present" : "absent", - sleep->power_ops ? "registered" : "not registered", READ_ONCE(dry_run)); - t6040_sleep_log_s2r_state(sleep, "probe"); + s->ngroups, s->cpu_start ? "mapped" : "absent", + s->wdt ? "mapped" : "absent", + (s->cluster_ctl && s->pwrgate && s->mastermini && s->spmi0 && + s->soc_east && s->aop_global) ? "mapped" : "incomplete", + s->apcie_st.valid ? "mapped" : "absent", + s->mbox ? "present" : "absent", + s->power_ops ? "registered" : "not registered", READ_ONCE(dry_run)); + t6040_sleep_log_s2r_state(s, "probe"); return 0; } From b5a4342fa10c7d8735e76e4163da8c318bc99441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:05 +0100 Subject: [PATCH 104/269] soc: apple: sleep: t6040: one prefix rule for the step functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps on one block were named both t6040_sleep__() and t6040__(). Keep t6040_sleep_* for the driver's own frame (suspend operations, probe, mapping, blockers, the commit and the wake return) and name each step on a block t6040__(), as most of them already were. No functional change: the object is unchanged but for the symbol names. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index c6898e93a1fd6..43220b9dfc84d 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -1670,7 +1670,7 @@ static void t6040_dcs_dyn_pg(struct t6040_sleep *s) /* * ANS's own fabric bridge (0x4_0b00_0000 +0x104/+0x108 bit 0). The bit is cleared before a * retained domain goes off and set again once it is back up; here the wake sets it - * (t6040_sleep_raise_apcie_st()) and the SoC's sleep engine, not this driver, takes ANS down. + * (t6040_apcie_st_raise()) and the SoC's sleep engine, not this driver, takes ANS down. * The boot loader leaves the bit clear, so a boot's first sleep goes down with it clear; * without this the next one meets the bridge as the first wake armed it. Only while ANS is * on: the bridge sits in it. @@ -1703,7 +1703,7 @@ static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) * Linux never uses, and does only what the engine needs before it runs: the shared PHY put * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. */ -static void t6040_sleep_lower_apcie_st(struct t6040_sleep *s) +static void t6040_apcie_st_lower(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; void __iomem *ps = s->soc_east; @@ -1750,7 +1750,7 @@ static void t6040_bridge_rearm(void __iomem *bridge) * waited for before the PHY's clocks are requested. @apcie_wake_step tells a * failed raise's stage. */ -static int t6040_sleep_raise_apcie_st(struct t6040_sleep *s) +static int t6040_apcie_st_raise(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; void __iomem *ps = s->soc_east; @@ -1911,7 +1911,7 @@ static int t6040_ps_lower_word(void __iomem *reg) PS_OFF_TIMEOUT_US); } -static void t6040_sleep_lower_atc(struct t6040_sleep *s) +static void t6040_atc_lower(struct t6040_sleep *s) { int p, k; @@ -1976,7 +1976,7 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) } /* The GP host's two ports, while the block's domains are all still on. */ -static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) +static void t6040_gp_ports_stop(struct t6040_sleep *s) { int p; @@ -1992,7 +1992,7 @@ static void t6040_sleep_stop_gp_ports(struct t6040_sleep *s) * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. APCIE_GP goes to clock-gated * (TARGET 4), the state the sleep engine itself asks for, not to off. */ -static void t6040_sleep_lower_apcie_gp(struct t6040_sleep *s) +static void t6040_apcie_gp_lower(struct t6040_sleep *s) { void __iomem *sys, *gp, *bridge = s->axi2af_bridge[AXI2AF_APCIE_SYS_GP]; u32 val; @@ -2044,8 +2044,8 @@ static int t6040_sleep_commit(unsigned long unused) * J616s: release them now, and wait for their power words to read off further down. */ spin_table_release_stopped(); - t6040_sleep_lower_atc(s); - t6040_sleep_stop_gp_ports(s); + t6040_atc_lower(s); + t6040_gp_ports_stop(s); /* * ANS's storage link, once ANS has been stopped by its driver (see @@ -2055,8 +2055,8 @@ static int t6040_sleep_commit(unsigned long unused) if (s->soc_east) t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1); if (s->pcie_ans_stopped) - t6040_sleep_lower_apcie_st(s); - t6040_sleep_lower_apcie_gp(s); + t6040_apcie_st_lower(s); + t6040_apcie_gp_lower(s); /* WD0 off, then the AON WD2 net (5 s covers the reconfig). */ if (s->wdt) @@ -2243,7 +2243,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) s->apcie_wake_step = 0; s->pcie_ans_on_ret = 0; if (s->pcie_ans_stopped) - s->pcie_ans_on_ret = t6040_sleep_raise_apcie_st(s); + s->pcie_ans_on_ret = t6040_apcie_st_raise(s); t6040_gp_pcie_clk(s); t6040_wake_sync(); t6040_aop_ps_restore(s); @@ -2709,7 +2709,7 @@ static void t6040_disp_back(struct t6040_sleep *s) * driver powers a domain off (TARGET 0, the one-shot bits clear), polled to ACTUAL 0. The * internal display's FE and SYS domains follow. Returns the number of words left on. */ -static unsigned int t6040_sleep_lower_display(struct t6040_sleep *s) +static unsigned int t6040_disp_lower(struct t6040_sleep *s) { unsigned int i, left = 0; @@ -2755,7 +2755,7 @@ static int t6040_sleep_enter(suspend_state_t state) if (dry) return t6040_sleep_dry_enter(s, dry); - left = t6040_sleep_lower_display(s); + left = t6040_disp_lower(s); if (left) pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); if (t6040_sleep_live_blockers(s, NULL, NULL)) { From 9994c5d975e89c9910195d0313a13038330ce940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:06 +0100 Subject: [PATCH 105/269] soc: apple: sleep: t6040: print every message through dev_*() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 part printed 37 messages with pr_*() and a hand-written "t6040-sleep: " prefix, next to dev_*() messages that print as "apple-sleep :". One driver had two prefixes. dev_*() allocates nothing either, and the T8140 sequences use it at the same points. Print every message through dev_*() with the driver's device. Only the log call sites change. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 126 ++++++++++++++++---------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 43220b9dfc84d..553d46bda303c 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -621,12 +621,12 @@ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *s) if (regmap_read(g->regmap, off, &val) || t6040_sleep_ps_on(val, off == g->first)) { on_mask |= BIT(bit); - pr_info("t6040-sleep: %s +%#x = %#x\n", g->name, off, val); + dev_info(s->dev, " %s +%#x = %#x\n", g->name, off, val); } } if (on_mask) - pr_info("t6040-sleep: %-8s ON (on mask %#x, cpu first)\n", g->name, - on_mask); + dev_info(s->dev, "%-8s ON (on mask %#x, cpu first)\n", g->name, + on_mask); blockers += !!on_mask; } @@ -638,9 +638,9 @@ static void t6040_sleep_log_blockers(struct t6040_sleep *s) { const char *ops_name; - pr_info("t6040-sleep: EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", - is_kernel_in_hyp_mode() ? "yes" : "no", s->mbox ? "yes" : "no", - t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); + dev_info(s->dev, "EL2 %s, s2r-mailbox %s, parkable %u/%u (%s)\n", + is_kernel_in_hyp_mode() ? "yes" : "no", s->mbox ? "yes" : "no", + t6040_sleep_parkable(&ops_name), num_possible_cpus() - 1, ops_name); } /* @@ -861,7 +861,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) if (num_online_cpus() != 1 || !s->mbox || (read_cpuid_mpidr() & MPIDR_HWID_BITMASK) != s->mbox->boot_mpidr) { - pr_err("t6040-sleep: refused: wrong CPU, other CPUs online, or no mailbox\n"); + dev_err(s->dev, "refused: wrong CPU, other CPUs online, or no mailbox\n"); refused++; } @@ -872,25 +872,25 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) */ if (!s->cluster_ctl || !s->pwrgate || !s->mastermini || !s->spmi0 || !s->soc_east || !s->aop_global) { - pr_err("t6040-sleep: refused: a commit/wake register base is not mapped (cluster-ctl %d pwrgate %d mastermini %d spmi0 %d soc-east %d aop-global %d)\n", - !!s->cluster_ctl, !!s->pwrgate, !!s->mastermini, !!s->spmi0, - !!s->soc_east, !!s->aop_global); + dev_err(s->dev, "refused: a commit/wake register base is not mapped (cluster-ctl %d pwrgate %d mastermini %d spmi0 %d soc-east %d aop-global %d)\n", + !!s->cluster_ctl, !!s->pwrgate, !!s->mastermini, !!s->spmi0, + !!s->soc_east, !!s->aop_global); refused++; } if (!sep->bound) { /* Nobody told it of the sleep: a commit with it running never returns. */ t6040_sleep_watch_on(sep, &val); - pr_err("t6040-sleep: refused: sep isn't bound (state %#x)\n", val); + dev_err(s->dev, "refused: sep isn't bound (state %#x)\n", val); refused++; } else if (!apple_sleep_enclave_registered()) { - pr_err("t6040-sleep: refused: sep's sleep request isn't left for the last refusal\n"); + dev_err(s->dev, "refused: sep's sleep request isn't left for the last refusal\n"); refused++; } /* Only ANS's driver can stop it, and it doesn't come back without a real sleep. */ if (!ans_idled && t6040_sleep_watch_on(ans, &val)) { - pr_err("t6040-sleep: refused: ANS is on (%#x) with no driver to stop it\n", val); + dev_err(s->dev, "refused: ANS is on (%#x) with no driver to stop it\n", val); refused++; } @@ -899,7 +899,7 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) * apple,apcie control windows + PLL/lane tunables: refused. */ if (ans_idled && !s->apcie_st.valid) { - pr_err("t6040-sleep: refused: ANS would stop but the storage-apcie windows/tunables are absent (apple,apcie)\n"); + dev_err(s->dev, "refused: ANS would stop but the storage-apcie windows/tunables are absent (apple,apcie)\n"); refused++; } @@ -1016,12 +1016,12 @@ static void t6040_sio_ps_restore(struct t6040_sleep *s) if (n == 1000) failed++; raised++; - pr_debug("t6040-sleep: wake: SOC-East power word +%#x: %#x at the commit, %#x after the wake, now %#x\n", - off, saved, val, cur); + dev_dbg(s->dev, "wake: SOC-East power word +%#x: %#x at the commit, %#x after the wake, now %#x\n", + off, saved, val, cur); } if (failed) - pr_warn("t6040-sleep: wake: %u of %u SIO power word(s) raised again did not come on\n", - failed, raised); + dev_warn(s->dev, "wake: %u of %u SIO power word(s) raised again did not come on\n", + failed, raised); } /* @@ -1352,13 +1352,13 @@ static void t6040_axi2af_readcheck(struct t6040_sleep *s) continue; val = readl(ps); if (FIELD_GET(PS_ACTUAL, val)) { - pr_debug("t6040-sleep: axi2af %s still on (%#x) at commit\n", - t6040_axi2af[i].name, val); + dev_dbg(s->dev, "axi2af %s still on (%#x) at commit\n", + t6040_axi2af[i].name, val); on++; } } if (on) - pr_debug("t6040-sleep: %d axi2af domain(s) still powered at commit\n", on); + dev_dbg(s->dev, "%d axi2af domain(s) still powered at commit\n", on); } /* @@ -1383,8 +1383,8 @@ static void t6040_axi2af_rearm(struct t6040_sleep *s) if (FIELD_GET(PS_TARGET, val) != PS_ACTIVE) continue; if (!FIELD_GET(PS_ACTUAL, val)) { - pr_debug("t6040-sleep: wake: axi2af %s not re-armed, PS %#x (gated)\n", - t6040_axi2af[i].name, val); + dev_dbg(s->dev, "wake: axi2af %s not re-armed, PS %#x (gated)\n", + t6040_axi2af[i].name, val); continue; } t6040_wake_sync(); @@ -2225,7 +2225,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) !(val & PS_BUSY), 1, SOC_EAST_TIMEOUT_US); if (ret) - pr_warn("t6040-sleep: SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); + dev_warn(s->dev, "SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); } t6040_wake_sync(); @@ -2276,7 +2276,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) if (s->mbox) { dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); - pr_debug("t6040-sleep: wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); + dev_dbg(s->dev, "wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); } if (s->aop_global) for (i = 0; i < AOP_WAKE_WORDS; i++) @@ -2366,29 +2366,29 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_dcs_dyn_pg(s); dcs_to = t6040_dcs_ps_auto_on(s); if (dcs_to) - pr_warn("t6040-sleep: DCS power words %#x did not read on before automatic mode\n", - dcs_to); + dev_warn(s->dev, "DCS power words %#x did not read on before automatic mode\n", + dcs_to); if (!begin_total && !dry) { /* The non-display blockers are clear and no dry run is asked for: proceed * to device suspend; .enter holds the real commit until the display is * also off. The AON WD2 is the net from here. */ - pr_debug("t6040-sleep: entering: %u display pipe(s) on, to be lowered before the commit\n", - disp); + dev_dbg(s->dev, "entering: %u display pipe(s) on, to be lowered before the commit\n", + disp); t6040_sleep_power_ops(s); t6040_sleep_set_wd2(s, WDT_SUSPEND_SECS); pm_set_suspend_via_firmware(); return 0; } - pr_info("t6040-sleep: suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", - live, disp); + dev_info(s->dev, "suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", + live, disp); t6040_sleep_log_blockers(s); if (dry) { dry = min(dry, 2U); t6040_sleep_power_ops(s); if (!s->power_ops) - pr_warn("t6040-sleep: dry run: no CPU parking power ops, so no hold at the commit point\n"); + dev_warn(s->dev, "dry run: no CPU parking power ops, so no hold at the commit point\n"); t6040_sleep_set_dry(s, dry); pm_set_suspend_via_firmware(); secs = min(READ_ONCE(wd2_secs), WDT_MAX_SECS); @@ -2396,13 +2396,13 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_sleep_set_wd2(s, secs); else secs = 0; - pr_info("t6040-sleep: dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", - dry, dry >= 2 ? "the stops only a power cycle undoes" : - "the last steps", secs); + dev_info(s->dev, "dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", + dry, dry >= 2 ? "the stops only a power cycle undoes" : + "the last steps", secs); return 0; } - pr_info("t6040-sleep: refusing the suspend; nothing entered\n"); + dev_info(s->dev, "refusing the suspend; nothing entered\n"); return -EOPNOTSUPP; } @@ -2426,8 +2426,8 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) unsigned int refused; int ret; - pr_info("t6040-sleep: dry run %u at the commit point: %u CPU(s) online\n", dry, - num_online_cpus()); + dev_info(s->dev, "dry run %u at the commit point: %u CPU(s) online\n", dry, + num_online_cpus()); t6040_sleep_log_s2r_state(s, "dry-run commit point"); /* * A wake source pending and left unmasked by the masks a real commit arms would keep the @@ -2439,28 +2439,28 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) for (i = 0; i < AOP_WAKE_WORDS; i++) p[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)) & ~t6040_wake_mask[i]; - pr_info("t6040-sleep: dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", - (int)sizeof(p), p); + dev_info(s->dev, "dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", + (int)sizeof(p), p); } refused = t6040_sleep_refuse(s); - pr_info("t6040-sleep: a real commit would %s here (%u check(s) failed)\n", - refused ? "refuse" : "go on", refused); + dev_info(s->dev, "a real commit would %s here (%u check(s) failed)\n", + refused ? "refuse" : "go on", refused); ret = s->power_ops ? spin_table_hold_parked() : -EOPNOTSUPP; if (ret) - pr_err("t6040-sleep: the parked CPUs didn't answer the hold: %d\n", ret); + dev_err(s->dev, "the parked CPUs didn't answer the hold: %d\n", ret); else - pr_info("t6040-sleep: %u parked CPU(s) answered the hold\n", - num_present_cpus() - num_online_cpus()); + dev_info(s->dev, "%u parked CPU(s) answered the hold\n", + num_present_cpus() - num_online_cpus()); if (dry >= 2 && !ret) - pr_info("t6040-sleep: the coprocessors' last checks: %d\n", - apple_sleep_coprocs_check(s->dev)); + dev_info(s->dev, "the coprocessors' last checks: %d\n", + apple_sleep_coprocs_check(s->dev)); if (!ret) spin_table_unhold_parked(); - pr_info("t6040-sleep: dry run: refused before the enclave's sleep request (%s), the coprocessors' last stops and the SoC power-down\n", - apple_sleep_enclave_registered() ? "left by sep" : "none left"); + dev_info(s->dev, "dry run: refused before the enclave's sleep request (%s), the coprocessors' last stops and the SoC power-down\n", + apple_sleep_enclave_registered() ? "left by sep" : "none left"); return -EBUSY; } @@ -2695,8 +2695,8 @@ static void t6040_disp_back(struct t6040_sleep *s) if (boost != s->pmc_if_saved[1]) writel(boost | s->pmc_if_saved[1], s->pmc_bwr + PMC_BOOST); if (bwr != s->pmc_if_saved[0] || boost != s->pmc_if_saved[1]) - pr_debug("t6040-sleep: wake: PMC display interfaces BWR %#x boost %#x, put back to %#x %#x\n", - bwr, boost, readl(s->pmc_bwr), readl(s->pmc_bwr + PMC_BOOST)); + dev_dbg(s->dev, "wake: PMC display interfaces BWR %#x boost %#x, put back to %#x %#x\n", + bwr, boost, readl(s->pmc_bwr), readl(s->pmc_bwr + PMC_BOOST)); } } @@ -2731,8 +2731,8 @@ static unsigned int t6040_disp_lower(struct t6040_sleep *s) PS_BUSY | PS_UNLATCH); regmap_write(g->regmap, off, val); if (t6040_regmap_poll_off(g->regmap, off, &cur)) { - pr_warn("t6040-sleep: %s +%#x still on: %#x\n", g->name, - off, cur); + dev_warn(s->dev, "%s +%#x still on: %#x\n", g->name, + off, cur); left++; } } @@ -2757,12 +2757,12 @@ static int t6040_sleep_enter(suspend_state_t state) left = t6040_disp_lower(s); if (left) - pr_warn("t6040-sleep: %u display power word(s) did not go off\n", left); + dev_warn(s->dev, "%u display power word(s) did not go off\n", left); if (t6040_sleep_live_blockers(s, NULL, NULL)) { unsigned int on = t6040_sleep_log_groups(s); - pr_info("t6040-sleep: at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", - num_online_cpus(), on); + dev_info(s->dev, "at the commit point: %u CPU(s) online, %u display group(s) on; refusing\n", + num_online_cpus(), on); t6040_disp_back(s); return -EBUSY; } @@ -2807,14 +2807,14 @@ static int t6040_sleep_enter(suspend_state_t state) if (s->returned) { t6040_wake_sync(); if (s->pcie_ans_on_ret) - pr_warn("t6040-sleep: wake: the storage PCIe raise failed after step %u: %d\n", - s->apcie_wake_step, s->pcie_ans_on_ret); + dev_warn(s->dev, "wake: the storage PCIe raise failed after step %u: %d\n", + s->apcie_wake_step, s->pcie_ans_on_ret); if (s->gp_clk_timeouts) - pr_warn("t6040-sleep: wake: %u GP PCIe clock or ready poll(s) timed out\n", - s->gp_clk_timeouts); + dev_warn(s->dev, "wake: %u GP PCIe clock or ready poll(s) timed out\n", + s->gp_clk_timeouts); if (s->aop_ps_timeouts) - pr_warn("t6040-sleep: wake: %u AOP island power word(s) did not come on\n", - s->aop_ps_timeouts); + dev_warn(s->dev, "wake: %u AOP island power word(s) did not come on\n", + s->aop_ps_timeouts); } return ret; } @@ -2829,7 +2829,7 @@ static void t6040_sleep_end(void) t6040_sleep_mem_begun = false; t6040_sleep_clear_wd2(s); if (dry) - pr_info("t6040-sleep: dry run %u over, WD2 off\n", dry); + dev_info(s->dev, "dry run %u over, WD2 off\n", dry); t6040_sleep_set_dry(s, 0); } From 49f64ba456a6848b4876a090e39c36628e82d384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:06 +0100 Subject: [PATCH 106/269] soc: apple: sleep: t6040: correct the comments that disagree with the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - cluster-ctl has five words, not three: the CPU complexes and the internal and external display's complexes, which the commit polls; +0xc is one of them, not a status word. - The wake does not poll the complexes, and puts the display complexes back to the nibble the commit found. - The GP PCIe host is not left to cold-recover: the commit stops its ports and the wake restores their clocks. - The summary of the wake return omitted the stopped CPUs, the AIC, the keep-alive bits and WD2, and spoke of three complexes. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 553d46bda303c..5d64d7382ff6a 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -112,15 +112,14 @@ #define PS_APCIE_GP_CLKGATED 0x00000244 /* - * cluster-ctl (pmgr reg0 + cluster-ctl-offset 0x20000 = 0x502820000). Measured - * on the J616s: the three CPU complexes are at E +0x0, P0 +0x4, - * P1 +0x8; the platform writes the low nibble 0 to power one down - * and raises it to 0xf to power one up, with +0xc the status/poll word. Three - * complexes on J616s (T8140 had E/P/AVE). + * cluster-ctl (ADT pmgr reg[0] + cluster-ctl-offset 0x20000 = 0x502820000), one word per + * complex, measured on the J616s: the CPU complexes E +0x0, P0 +0x4 and P1 +0x8, then the + * internal and the external display's complexes +0xc and +0x10. The low nibble 0 powers a + * complex down and 0xf powers it up; BIT4 reads set until a power-down has finished. */ #define CLUSTER_CTL(n) ((n) * 4) -#define CLUSTER_CTL_NR 5 /* E, P0, P1 and two more complexes (+0xc, +0x10) */ -#define CLUSTER_CTL_FIRST_WAIT 3 /* the platform waits past the third: the last two are polled */ +#define CLUSTER_CTL_NR 5 /* E, P0, P1, internal and external display */ +#define CLUSTER_CTL_FIRST_WAIT 3 /* the two display complexes are polled */ #define CLUSTER_CTL_BUSY BIT(4) #define CLUSTER_CTL_NIBBLE GENMASK(3, 0) #define CLUSTER_CTL_ON 0xf @@ -2167,15 +2166,14 @@ static void t6040_gp_pcie_clk(struct t6040_sleep *s) } /* - * The wake return (t6040_sleep_return), reached on the boot core after aura's - * AURAS2R1 mailbox hands it back to cpu_resume. aura restores NO SoC register; - * every write here is the OS driver's job. It is the - * mirror of the PMGR wake restore: the three CPU complexes on, the two wake - * pwrgates, SOC-East retention off (with the busy poll), the axi2af - * fabric-bridge re-arm, the CPM voters re-asserted, the clk mux restored, the - * AP-global S2R control cleared, and the wake-status words read. Between them, - * what a Linux sleep needs on top: ANS's storage link, the GP PCIe ports' clocks - * and the AOP island's power words. + * The wake return, on the boot core, once the boot object's mailbox has handed it to the + * resume entry. The boot object restores no SoC register: every write here is this driver's. + * In order: the timer and IRQ routing, the stopped CPUs and the AIC, then as the PMGR + * wake restore the complexes back, the two wake pwrgates, SOC-East retention off (with the + * busy poll) and the axi2af fabric-bridge re-arm; what a Linux sleep needs on top, ANS's + * storage link, the GP PCIe ports' clocks and the AOP island's power words; then the CPM + * voters re-asserted, the clk mux restored, the AOP-global S2R control cleared, the + * keep-alive bits back, WD2 armed over the device resume, and the wake-status words read. */ static void t6040_sleep_return(struct t6040_sleep *s) { @@ -2194,9 +2192,9 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_wake_sync(); /* - * The three CPU complexes back on (E, P0, P1), nibble 0xf, poll +0xc for - * the ready bit. The platform then also writes 1 to register 0xe440f8 of each - * complex. That is not done here: the secondaries come up through the + * The complexes back: E, P0 and P1 to 0xf, the two display complexes to the nibble + * the commit found, with no poll. Register 0xe440f8 of each CPU complex could also + * be written 1. That is not done here: the secondaries come up through the * normal per-CPU start afterwards, and all 14 CPUs of the J616s are * online after a wake without it. */ @@ -2938,8 +2936,8 @@ static void __iomem *t6040_sleep_map(struct device *dev, const char *name) * of these, so until aura fills the node @valid stays false and a real sleep * with ANS stopped refuses (t6040_sleep_refuse). The reg-names and tunable * property names match the T8140's apple,apcie shape exactly, so the same - * loader fills both SoCs. The GP PCIe host (pcie@1cb0000000, Wi-Fi + SD) is a - * different node and is left to cold-recover. + * loader fills both SoCs. The GP PCIe host (pcie@1cb0000000, Wi-Fi and SD) is a + * different node, mapped by t6040_sleep_map_gp(). */ static void t6040_sleep_map_apcie(struct t6040_sleep *s) { From 5925ba08d68aec16753bd9ae158542e6f44fc52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:07:43 +0100 Subject: [PATCH 107/269] soc: apple: sleep: t6040: say what the hardware expects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several comments explained a step by how it was found: register map names of another driver, a boot argument, the structure of another operating system's routines, register values read at one point in time, and what went wrong on a board before the step was there. Say instead what the hardware expects and what happens without the step, in the present tense, and drop the capitals used for emphasis. The warning on a busy SOC-East retention word names the bit as the code now does. No functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 138 ++++++++++++++------------------ 1 file changed, 62 insertions(+), 76 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 5d64d7382ff6a..6abb114bd88eb 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -91,11 +91,11 @@ #define PS_SIO_LAST 0x228 /* SOC-East: UART6, the last */ /* - * ANS's storage PCIe block: the TWO ST lanes (apcie_st0/apcie_st1) and their + * ANS's storage PCIe block: the two ST lanes (apcie_st0/apcie_st1) and their * shared PHY (T8140 has a single lane). Taken down at the commit * and raised on wake. The power words sit in SOC-East (t604x-pmgr.dtsi * offsets, same bank as ps_ans); the control windows (axi2af/cio3pll/clkgen/ - * phy-block/phy-ip/rc) and the PLL/lane tunables are NOT in this SoC's static + * phy-block/phy-ip/rc) and the PLL/lane tunables are not in this SoC's static * description and are published by the boot object (aura) under the apple,apcie * node, as on the T8140. The block-internal offsets are the T8140's * (sleep-internal.h): the same Apple PCIe IP. @@ -126,11 +126,9 @@ #define CLUSTER_TIMEOUT_US 50000 /* - * SOC-PwrGate (pmgr reg0x26 = 0x50282c000). Retention - * bits at +0x0/+0x40 and the three CPM voters at +0x100/+0x108/+0x110, each - * BIT31. The wake restore re-asserts them; no pwrgate write was seen - * in the entry (the voters read back 0x80000000, a no-op), so entry - * leaves them alone. + * SOC-PwrGate (ADT pmgr reg[0x26] = 0x50282c000): the retention bits at +0x0/+0x40 and + * the three CPM voters at +0x100/+0x108/+0x110, each BIT31. The wake restore re-asserts + * them. The entry leaves them alone: the voters already read 0x80000000 there. */ #define PWRGATE_ON 0x80000000 #define PWRGATE_WAKE_0 0x000 @@ -140,9 +138,8 @@ #define PWRGATE_CPM_2 0x110 /* - * SOC-East (pmgr reg2 = 0x502280000). The SEP/retention word at +0xc00: on - * wake the platform clears PS_DEV_DISABLE and then polls PS_BUSY clear before going - * on. Also holds several + * SOC-East (ADT pmgr reg[2] = 0x502280000). The SEP/retention word at +0xc00: the wake + * clears PS_DEV_DISABLE and then polls PS_BUSY clear before going on. Also holds several * of the axi2af domains' power words. */ #define SOC_EAST_RETENTION 0xc00 @@ -165,10 +162,9 @@ #define CLK_QUIESCE_TIMEOUT_US 1000 /* - * The keep-alive enables: 32 words in PMGR SOC - * from +0x1dc000 and 32 in SOC-East from +0x50000, stride 0x44, BIT0 the keep-alive. - * The quiesce clears them right before the S2R timer (unless the boot-arg - * pmgr-disable-ka); its wake restore sets them again. + * The keep-alive enables: 32 words in PMGR SOC from +0x1dc000 and 32 in SOC-East from + * +0x50000, stride 0x44, BIT0 the keep-alive. Cleared right before the S2R timer, set + * again at the wake. */ #define T6040_KA_SOC_PA 0x5029dc000ULL #define T6040_KA_EAST_PA 0x5022d0000ULL @@ -178,9 +174,8 @@ #define KA_ENABLE BIT(0) /* - * The SMC's AP watchdog deadline (the watchdog timer's reg[2] + 8): - * The platform writes 0 when it enables the AON watchdog for a reconfig, - * right after arming the AON WD2 (measured on the J616s: 0x508330008 := 0). + * The SMC's AP watchdog deadline (the watchdog timer's reg[2] + 8): written 0 right after + * the AON WD2 is armed for the sleep. */ #define T6040_SMC_WDT_DEADLINE_PA 0x508330008ULL @@ -197,16 +192,14 @@ #define AGX_TIMEOUT_US 10000 /* - * The hardware steps of the generic PMGR quiesce that run on J616s before - * its T6041 part (measured and the ADT). In order: + * Before the SoC-specific steps of the PMGR quiesce, in order: * - the ACC quiesce of c = E, P0, P1: APSC off, ACC +0xe20020 BIT23 := 1 (64-bit), * poll BIT7 clear (CPM gating after it reads back already set: not written); * - features off: cpu-fixed-freq-pll-relock, BIT42 := 0 in the E and P0 * ACC words; then the TVMs disp2, afr, dcs (two), gfx (two), disp, soc: control * BIT0 := 0, status polled until BIT1 clears. - * The platform then drops the AP voter's DCS floor vote in the PMC. That step is left out: the - * first access to that word hung the bus on the J616s. - * The platform polls the APSC and TVM words without a timeout; here every poll is bounded. + * The PMC's DCS floor vote is not touched: an access to that word hangs the bus. Every poll + * here is bounded. */ static const u64 t6040_acc_apsc_pa[] = { 0x210e20020ULL, 0x211e20020ULL, 0x212e20020ULL }; #define ACC_APSC_DISABLE BIT_ULL(23) @@ -240,26 +233,25 @@ static const struct { /* * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000), written as on the T8140. - * Measured on the J616s: the boot stage is set to 0x50 by an extended write whose - * command word is (0xf801 << 16) | 0x8e30 = 0xf8018e30, and NOTIFY_ENABLE is 0x2000001 on - * both controllers' +0x40. + * Measured on the J616s: the boot stage is set to 0x50 by an extended write to + * PMU_BOOT_STAGE, and NOTIFY_ENABLE is 0x2000001 on both controllers' +0x40. */ /* The PMU's USID on the sleep's SPMI bus, and the boot stage that announces a sleep */ #define PMU_USID 0xe -#define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg; cmd 0xf8018e30 */ +#define PMU_BOOT_STAGE 0xf801 /* T6040 slave/reg */ #define PMU_BOOT_STAGE_SLEEP 0x50 /* The response vector's enable for the sleep */ #define SPMI_NOTIFY_ENABLE_SLEEP 0x2000001 /* - * MasterMini (NUB_MPMGR_AON, RegMap 4 = pmgr reg[0x37] = 0x508900000). + * MasterMini, the block of the always-on PMGR (ADT pmgr reg[0x37] = 0x508900000). * The S2R timer at +0x4000 (physical 0x508904000) is the commit: writing 1 arms * the SoC sleep engine. Measured on the J616s: exactly this write. */ #define MASTERMINI_S2R_TIMER 0x4000 /* - * AOP-global (AP controls, RegMap 0x42, window 0x508000000). Mapped from the + * AOP-global (the AP controls, window 0x508000000). Mapped from the * S2R-control word 0x508004000: +0x0 the S2R control (cleared to 0 on wake), * +0xc..+0x2c the 9 wake-source masks (armed before * sleep), +0x30..+0x50 the 9 wake-source status words (read on wake). @@ -508,7 +500,7 @@ static bool t6040_sleep_reg_commit_stops(void) /* * In the power-state check, a sub-block is on while its TARGET is not 0. A * pipe's CPU word never counts: it stays under automatic power gating (taking it out - * of automatic mode raised an SError on T6040) and the SoC power-down drops it. + * of automatic mode raises an SError) and the SoC power-down drops it. */ static bool t6040_sleep_ps_on(u32 val, bool cpu) { @@ -911,10 +903,10 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) /* * The AOP and audio island's power words (MiniPMGR 0x508304000 + 0x000..0x158). The island - * is off after a sleep, and the AOP's resume, which reads its ASC first, then faulted on the - * J616s. The words are read in .begin and put back at the wake, parents first: a word that - * was on goes to a manual target 0xf (in automatic mode it gated again before the AOP's - * driver got to its ASC), any other gets its saved value if that asks for power. The table + * is off after a sleep, and the AOP's resume, which reads its ASC first, faults on it. The + * words are read in .begin and put back at the wake, parents first: a word that was on goes + * to a manual target 0xf (in automatic mode it gates again before the AOP's driver gets to + * its ASC), any other gets its saved value if that asks for power. The table * is the power-down order, children first. */ #define T6040_AOP_PS_PA 0x508304000ULL @@ -1042,12 +1034,11 @@ static const u32 t6040_wake_mask[AOP_WAKE_WORDS] = { }; /* - * The hardware S2R-lo entry counter: the count is PMGR - * regmap 1 + 0x70970 shifted right by 3; the platform reads - * 0x508370970 at quiesce. It tells "the SoC entered S2R" from "the - * engine never finished". Read-only, with the AOP-global S2R control, wake masks and - * wake status, at debug level: at probe, for the boot after a failed sleep, and in a dry run. - * Mapped once at probe: the dry run reads it with interrupts off. + * The hardware S2R-lo entry counter: bits 31:3 of MiniPMGR + 0x70970. It tells "the SoC + * entered S2R" from "the engine never finished". Read-only, with the AOP-global S2R + * control, wake masks and wake status, at debug level: at probe, for the boot after a + * failed sleep, and in a dry run. Mapped once at probe: the dry run reads it with + * interrupts off. */ #define T6040_S2R_LO_COUNT_PA 0x508370970ULL @@ -1073,12 +1064,11 @@ static void t6040_sleep_log_s2r_state(struct t6040_sleep *s, const char *when) } /* - * The 15 axi2af fabric-bridge retention domains (the - * table for the axi2af workaround). Each domain's power word is in + * The 15 domains with an axi2af fabric bridge to re-arm. Each domain's power word is in * SOC (soc_ps, base 0x502800000) or SOC-East (soc_east, base 0x502280000); its * fabric bridge is a separate window. On wake every domain that is powered up * (PS TARGET == 0xf) gets bridge +0x104/+0x108 bit0 set, or the bridge drops - * its traffic (the ANS/Wi-Fi/display-dead signature). + * its traffic. */ static const struct t6040_axi2af_domain { const char *name; @@ -1121,10 +1111,9 @@ static void t6040_wake_sync(void) } /* - * The five complexes off, as the quiesce loop turns cluster c off and waits if c > 3, - * for c = 1..5 (measured on the J616s): each slot's low nibble -> 0, in order +0x0 .. +0x10, - * and the last two (+0xc, +0x10) polled until BIT4 clears (+0xc read 0x2f -> - * 0x20 -> 0x10 -> 0x0 there). The CPU complexes (E, P0, P1) go down at the WFI. + * The five complexes off, measured on the J616s: each slot's low nibble to 0, in order + * +0x0 .. +0x10, and the two display complexes (+0xc, +0x10) polled until BIT4 clears. + * The CPU complexes (E, P0, P1) go down at the WFI. */ static void t6040_clusters_off(struct t6040_sleep *s) { @@ -1336,8 +1325,8 @@ static int t6040_sleep_preflight_show(struct seq_file *m, void *unused) DEFINE_SHOW_ATTRIBUTE(t6040_sleep_preflight); /* - * Read-only axi2af gate: on entry the platform panics unless every reachable domain's - * power word reads [7:4]==0. Here the ones still on are only named, at debug level. + * Read-only axi2af gate: every reachable domain's power word is expected to read + * [7:4] == 0 here. The ones still on are only named, at debug level. */ static void t6040_axi2af_readcheck(struct t6040_sleep *s) { @@ -1364,8 +1353,7 @@ static void t6040_axi2af_readcheck(struct t6040_sleep *s) * The wake-side axi2af re-arm: every retained domain powered back up (PS TARGET 0xf and * ACTUAL on) gets its fabric bridge +0x104/+0x108 bit0 set, or the bridge drops its * traffic. A word in automatic mode can read TARGET 0xf with its domain gated; the bridge - * is then off too (the first resume on the J616s took an SError in this step), so such a - * domain is left alone. + * is then off too, and an access to it raises an SError, so such a domain is left alone. */ static void t6040_axi2af_rearm(struct t6040_sleep *s) { @@ -1448,7 +1436,7 @@ static bool t6040_ps_actual_on(void __iomem *base, u32 offset) return FIELD_GET(PS_ACTUAL, readl(base + offset)); } -/* The shared PHY and PHY-IP to sleep (the root complex disable; it polls unbounded). */ +/* The shared PHY and PHY-IP to sleep: the PCIe root complex disable sequence. */ static void t6040_apcie_phy_off(struct t6040_sleep *s) { struct t6040_apcie_st *a = &s->apcie_st; @@ -1501,8 +1489,8 @@ static void t6040_apcie_pll_off(struct t6040_sleep *s) /* * The sleep engine hands each ATC port to its always-on side and then powers the port's * common domain off itself: it needs the four ATC commons on at that - * point. With them off it stalled on the J616s raising ATC0_COMMON, whose parent FAB2_SOC - * was off as well. FAB2_SOC is raised by hand, then ATC0..3_COMMON to a manual target 0xf, + * point. With them off it waits for good raising ATC0_COMMON, whose parent FAB2_SOC is + * off as well. FAB2_SOC is raised by hand, then ATC0..3_COMMON to a manual target 0xf, * then FAB2_SOC gets its automatic mode back: the powered children hold it up. */ static const u16 t6040_atc_common[4] = { 0x390, 0x3b8, 0x3e0, 0x408 }; @@ -1524,8 +1512,8 @@ static void t6040_atc_commons_on(struct t6040_sleep *s) * The CFG writes of the PHY sleep entry, run on every ATC port before a sleep: * ATCPHY_CFG +0x2c bit 0 (S2R_AON_SYNC_RST_N), +0x08 bit 0 (CFG_S2R_FSM_EN) and +0x28 bit 0 * (AHB_CLK_REQ_N) set, then +0x04 bit 8 (the clock's ack) polled. Without them the engine - * stalled on the J616s on a port that carries DisplayPort lanes: port 3 (the HDMI output) - * always, any other port once a display runs on it in DP alternate mode. A parked port, and + * waits for good on a port that carries DisplayPort lanes: port 3 (the HDMI output) always, + * any other port once a display runs on it in DP alternate mode. A parked port, and * one its PHY driver has put to sleep, already has +0x28 bit 0 set and is left alone. */ #define ATC_CFG_CLK_STATUS 0x04 @@ -1601,8 +1589,8 @@ static void t6040_fabrics_on(struct t6040_sleep *s) * expects each DCS to gate itself after self-refresh. That takes the DCS's dynamic power-gate * mode (DCS +0x20 bits 12 and 8) and its power word in automatic mode with no minimum state. * Both are expected at boot and again after each wake. Nothing sets them under Linux (the DT leaves - * the ps_dcs_* domains disabled: target 0xf, no automatic mode), and the engine then waited - * for the first DCS pair's power gate for good on the J616s. Both are applied in each .begin. + * the ps_dcs_* domains disabled: target 0xf, no automatic mode), and the engine then waits + * for the first DCS pair's power gate for good. Both are applied in each .begin. */ static const struct { bool east; @@ -1698,7 +1686,7 @@ static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) * itself and in its own order: APCIE_PHY_SW to clock-gated, APCIE_SYS_ST0/1 off, APCIE_ST0/1 * clock-gated, ANS off, APCIE_SYS_GP off, APCIE_GP clock-gated. It polls APCIE_PHY_SW for * its parents, which are both SYS_ST lanes: with them lowered here, as the T8140 controller - * does, it waited there for good on the J616s. So the commit raises APCIE_SYS_ST1, which + * does, it waits there for good. So the commit raises APCIE_SYS_ST1, which * Linux never uses, and does only what the engine needs before it runs: the shared PHY put * to sleep, APCIE_PHY_SW clock-gated while APCIE_GP is still on, the PCIe PLL shut down. */ @@ -1736,7 +1724,7 @@ static void t6040_bridge_rearm(void __iomem *bridge) /* * Raise ANS's storage link from power-off, the two-lane adaptation of * sleep.c's apple_sleep_raise_apcie(). Runs atomic on the waking core in - * t6040_sleep_return(), AFTER t6040_axi2af_rearm() and BEFORE control returns to + * t6040_sleep_return(), after t6040_axi2af_rearm() and before control returns to * the PM core, so nvme's resume_early sets the ANS run bit onto a live link. * Both lanes come up; the PLL/clkgen/PHY tables run once (shared). The two * apcie_sys_st domains are not in the 15-domain axi2af table, so their bridges @@ -1765,8 +1753,8 @@ static int t6040_apcie_st_raise(struct t6040_sleep *s) /* * In order, each domain to ACTUAL 0xf: ANS, GP, SYS_GP, ST0, SYS_ST0, ST1, SYS_ST1, * the fabric-bridge bit0s set after ANS and after SYS_GP. SYS_GP stays at a manual - * target 0xf: in automatic mode it gated again on the J616s, and the DART behind it - * then faulted in its resume. + * target 0xf: in automatic mode it gates again, and the DART behind it then faults in + * its resume. */ ret = t6040_ps_raise(ps, PS_ANS); if (!ret) @@ -1884,10 +1872,8 @@ extern char t6040_resume_stub[]; * them off; their drivers don't lower atcN_common. * * Their common_dp, usb, phymxwrap and usb_aon words are MiniPMGR words (0x508300000 + 0xa0.., - * ADT ATCn_COMMON_DP/USB/PHYMXWRAP/USB_AON), not SOC ones: applied to soc_ps, - * those offsets lower SOC AIC (0x118), PMC (0x120), PMS_FPWM0-4 and - * FAB0_SOC (0x168) right before the S2R request; the dead PMC then blocks the sleep engine's - * PMC writes (an AP read of the PMC hangs from that step on). + * ADT ATCn_COMMON_DP/USB/PHYMXWRAP/USB_AON) and not in this table: the same offsets in SOC + * are AIC, PMC, PMS_FPWM0-4 and FAB0_SOC. */ static const u16 t6040_atc_words[4][5] = { { 0x3b0, 0x3a8, 0x398, 0x3a0, 0x390 }, @@ -1987,8 +1973,8 @@ static void t6040_gp_ports_stop(struct t6040_sleep *s) /* * apcie_sys_gp (AUTO off, axi2af bit0s, target 0), then apcie_gp. After ANS's ST lanes: the - * storage lanes' cio3pll/clkgen/PHY windows sit in apcie_gp, and touching them with it off - * hung the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. APCIE_GP goes to clock-gated + * storage lanes' cio3pll/clkgen/PHY windows sit in apcie_gp, and an access to them with it + * off hangs the bus. T8140's order: SYS_ST, ST, SYS_GP, GP. APCIE_GP goes to clock-gated * (TARGET 4), the state the sleep engine itself asks for, not to off. */ static void t6040_apcie_gp_lower(struct t6040_sleep *s) @@ -2038,9 +2024,9 @@ static int t6040_sleep_commit(unsigned long unused) s->siq_cfg = read_sysreg_s(SYS_APL_SIQ_CFG_EL1); spin_table_stop_parked(); /* - * On the platform every other core is off, in its last WFI, before the boot core turns the - * complexes off. With the stopped cores still spinning the sleep engine stalled on the - * J616s: release them now, and wait for their power words to read off further down. + * Every other core has to be off, in its last WFI, before the boot core turns the + * complexes off, or the sleep engine waits for them: release them now, and wait for + * their power words to read off further down. */ spin_table_release_stopped(); t6040_atc_lower(s); @@ -2113,8 +2099,8 @@ static int t6040_sleep_commit(unsigned long unused) /* * The GP PCIe host's two ports (Wi-Fi/BT, SD): the boot loader set their clocks up, and - * nobody does after a sleep. The DART in port 0's block then faulted in its noirq resume on - * the J616s. t6040_gp_port_stop() backwards, short of the link: the lane reference clocks + * nobody does after a sleep. Without them the DART in port 0's block faults in its noirq + * resume. t6040_gp_port_stop() backwards, short of the link: the lane reference clocks * requested and acknowledged, the PHY's enables and the port's application clock as saved, * the controller's PERST register as saved, then the port's ready bit polled. The link * itself stays down for the host driver. @@ -2223,7 +2209,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) !(val & PS_BUSY), 1, SOC_EAST_TIMEOUT_US); if (ret) - dev_warn(s->dev, "SOC-East PARENT_MISSING stuck on wake (%#x)\n", val); + dev_warn(s->dev, "SOC-East retention word still busy on wake (%#x)\n", val); } t6040_wake_sync(); @@ -2233,7 +2219,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) * left as the power loss left them. */ - /* The fabric-bridge retention re-arm (the ANS/Wi-Fi/display-dead guard). */ + /* The fabric-bridge retention re-arm, or the bridges drop their traffic. */ t6040_axi2af_rearm(s); t6040_wake_sync(); @@ -2465,7 +2451,7 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) /* * regmap_read_poll_timeout_atomic() reads ktime_get(), which warns while timekeeping is - * suspended (12 warnings from .enter on the J616s). Counted 1 us steps instead. + * suspended. Counted 1 us steps instead. */ static int t6040_regmap_poll_off(struct regmap *map, u32 off, u32 *cur) { @@ -2702,7 +2688,7 @@ static void t6040_disp_back(struct t6040_sleep *s) * The display power words the ADT wants off before a sleep (apple,check-ps-b4-sleep), lowered * here: after the device suspend, interrupts off, one CPU up, so no driver, interrupt handler * or DART touches the pipes any more. The DCPs only stop their firmware: lowering the domains - * from their suspend panicked on T6040. Each group from its last sub-block up to, and not + * from their suspend makes the SoC panic. Each group from its last sub-block up to, and not * including, its pipe CPU, which the PMGR gates by itself: each word as the pmgr-pwrstate * driver powers a domain off (TARGET 0, the one-shot bits clear), polled to ACTUAL 0. The * internal display's FE and SYS domains follow. Returns the number of words left on. From f96e888cc121f07b3119502b0f6b039167d26e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:07:44 +0100 Subject: [PATCH 108/269] soc: apple: sleep: t6040: follow the coding style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sort the includes, include what is used (linux/cpumask.h, linux/device.h, linux/cacheflush.h) instead of linux/cpu.h, linux/slab.h, linux/smp.h and asm/cacheflush.h, and fix what checkpatch --strict reports: block comment style, lines over 100 columns, a missing blank line, two multiple assignments, an alignment and a line ending in an open parenthesis. Left as they are: udelay() in atomic context, the SMC key name pmVC, and the extern of the wake entry, which goes with the entry itself. No functional change: the object is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 74 ++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 6abb114bd88eb..1baafbe34476a 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -45,39 +45,39 @@ #include #include -#include +#include +#include #include +#include #include #include #include +#include #include #include #include #include #include #include -#include #include #include #include -#include -#include #include #include #include #include +#include #include -#include #include -#include #include +#include #include #include #include #include + #include -#include #include "sleep-internal.h" @@ -221,6 +221,7 @@ static const struct { { 0x502918000ULL, 0x502918250ULL }, /* disp-tvm */ { 0x502900000ULL, 0x502900250ULL }, /* soc-tvm */ }; + #define TVM_ENABLE BIT(0) #define TVM_BUSY BIT(1) @@ -279,7 +280,8 @@ static const struct { #define PS_APCIE_GP 0x100 #define PS_APCIE_SYS_GP 0x390 #define GP_PIN_DATA BIT(0) -#define T6040_PMC_DCP_PA 0x5048a9c14ULL /* count of DCPs powered (+0), second voter (+4); the platform ends at 0/0 */ +/* The PMC's DCP voters: the count of DCPs powered (+0) and a second voter (+4), both 0 at sleep */ +#define T6040_PMC_DCP_PA 0x5048a9c14ULL /* The axi2af fabric-bridge retention domains, t6040_axi2af[] */ enum { @@ -598,7 +600,7 @@ static unsigned int t6040_sleep_live_blockers(struct t6040_sleep *s, struct seq_ return blockers + disp; } -/* The display groups' on-state, one line per group, no allocation. For the log at the commit point. */ +/* The display groups' on-state, one line per group, for the log at the commit point. */ static unsigned int t6040_sleep_log_groups(struct t6040_sleep *s) { unsigned int i, blockers = 0; @@ -693,7 +695,8 @@ static void t6040_ed_map(struct device *dev) if (c >= T6040_ED_CLUSTERS || k >= T6040_ED_CORES || t6040_ed[c][k]) continue; - t6040_ed[c][k] = t6040_ioremap_np(dev, T6040_ED_BASE_PA + c * T6040_ED_CLUSTER_STRIDE + + t6040_ed[c][k] = t6040_ioremap_np(dev, T6040_ED_BASE_PA + + c * T6040_ED_CLUSTER_STRIDE + k * T6040_ED_CORE_STRIDE, 0x1000); n += !!t6040_ed[c][k]; } @@ -1846,8 +1849,7 @@ static int t6040_apcie_st_raise(struct t6040_sleep *s) * restart it for 60 s, in t6040_sleep_set_wd2()'s order, then take cpu_resume. The PA is * the T6040 AON WDT (T6040_WDT_PA), used only when the DT's wdt block is that one. */ -asm( -" .pushsection .text, \"ax\"\n" +asm(" .pushsection .text, \"ax\"\n" " .type t6040_resume_stub, %function\n" "t6040_resume_stub:\n" " movz x9, #0xc000\n" @@ -2196,8 +2198,10 @@ static void t6040_sleep_return(struct t6040_sleep *s) /* Wake pwrgates and CPM voters: read, set BIT31. */ if (s->pwrgate) { - writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_0); - writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_1); + writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, + s->pwrgate + PWRGATE_WAKE_0); + writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, + s->pwrgate + PWRGATE_WAKE_1); } t6040_wake_sync(); @@ -2326,9 +2330,11 @@ static int t6040_sleep_begin(suspend_state_t state) struct t6040_sleep *s = t6040_sleep; unsigned int disp = 0; unsigned int live = t6040_sleep_live_blockers(s, NULL, &disp); - /* The display pipes are lowered during device suspend (apple-dcp .suspend), + /* + * The display pipes are lowered during device suspend (apple-dcp .suspend), * so .begin only gates on the non-display live blockers; .enter re-checks the - * full set at the commit point, by which time the display is off. */ + * full set at the commit point, by which time the display is off. + */ unsigned int begin_total = live - disp; unsigned int dry = READ_ONCE(dry_run), secs; u32 dcs_to; @@ -2344,7 +2350,8 @@ static int t6040_sleep_begin(suspend_state_t state) t6040_sleep_set_dry(s, 0); s->returned = false; - s->gp_saved[0] = s->gp_saved[1] = false; + s->gp_saved[0] = false; + s->gp_saved[1] = false; t6040_aop_ps_save(s); t6040_sleep_pmvc(s); t6040_dcs_dyn_pg(s); @@ -2353,9 +2360,11 @@ static int t6040_sleep_begin(suspend_state_t state) dev_warn(s->dev, "DCS power words %#x did not read on before automatic mode\n", dcs_to); if (!begin_total && !dry) { - /* The non-display blockers are clear and no dry run is asked for: proceed + /* + * The non-display blockers are clear and no dry run is asked for: proceed * to device suspend; .enter holds the real commit until the display is - * also off. The AON WD2 is the net from here. */ + * also off. The AON WD2 is the net from here. + */ dev_dbg(s->dev, "entering: %u display pipe(s) on, to be lowered before the commit\n", disp); t6040_sleep_power_ops(s); @@ -2643,8 +2652,10 @@ static int t6040_psw_on(struct t6040_sleep *s, const struct t6040_psw *w, bool s writel(1, s->disp_pwrgate + DISP_PWRGATE_B); } if (bridge && !ret) { - writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), bridge + AXI2AF_BRIDGE_104); - writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), bridge + AXI2AF_BRIDGE_108); + writel(readl(bridge + AXI2AF_BRIDGE_104) | BIT(0), + bridge + AXI2AF_BRIDGE_104); + writel(readl(bridge + AXI2AF_BRIDGE_108) | BIT(0), + bridge + AXI2AF_BRIDGE_108); } } v = t6040_psw_read(w); @@ -2705,7 +2716,7 @@ static unsigned int t6040_disp_lower(struct t6040_sleep *s) bool cpu = off == g->first; u32 val, cur = 0; - /* An automatically gated CPU still powered: wait for the PMGR to gate it. */ + /* An automatically gated CPU still on: wait for the PMGR to gate it. */ if (cpu && !regmap_read(g->regmap, off, &val) && (val & PS_AUTO_ENABLE) && FIELD_GET(PS_ACTUAL, val)) t6040_regmap_poll_off(g->regmap, off, &cur); @@ -2991,11 +3002,12 @@ static void t6040_sleep_map_mailbox(struct t6040_sleep *s) #ifdef CONFIG_HIBERNATION /* - * Restored from a hibernation image, this kernel runs under the aura of the boot that resumed it. - * That aura moved with the boot object, and so did its mailbox: the one mapped at probe was the - * hibernating boot's, and the boot core would come back from a sleep through this boot's. Take this - * boot's address from the device tree aura keeps at its fixed slot. Without it, drop the mailbox, so - * that a suspend refuses for it instead of arming memory that is no longer aura's. + * Restored from a hibernation image, this kernel runs under the aura of the boot that resumed + * it. That aura moved with the boot object, and so did its mailbox: the one mapped at probe was + * the hibernating boot's, and the boot core would come back from a sleep through this boot's. + * Take this boot's address from the device tree aura keeps at its fixed slot. Without it, drop + * the mailbox, so that a suspend refuses for it instead of arming memory that is no longer + * aura's. */ static int t6040_sleep_restore(struct device *dev) { @@ -3033,7 +3045,8 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) struct resource res; int p, idx; - s->gp_perst[0] = s->gp_perst[1] = -1; + s->gp_perst[0] = -1; + s->gp_perst[1] = -1; host = of_find_compatible_node(NULL, NULL, "apple,t6041-pcie"); if (!host) return; @@ -3055,7 +3068,7 @@ static void t6040_sleep_map_gp(struct t6040_sleep *s) continue; p = (reg[0] >> 11) & 0x1f; /* the device number of pci@N,0 */ if (p > 1 || of_parse_phandle_with_args(child, "reset-gpios", "#gpio-cells", 0, - &args)) + &args)) continue; if (args.args_count >= 2 && (!pins || pins == args.np)) { if (!pins) @@ -3102,7 +3115,8 @@ static int t6040_sleep_probe(struct platform_device *pdev) */ BUILD_BUG_ON(ARRAY_SIZE(t6040_axi2af) != T6040_AXI2AF_DOMAINS); BUILD_BUG_ON(ARRAY_SIZE(t6040_tvm) != ARRAY_SIZE(((struct t6040_sleep *)0)->tvm_ctl)); - BUILD_BUG_ON(ARRAY_SIZE(t6040_acc_apsc_pa) != ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); + BUILD_BUG_ON(ARRAY_SIZE(t6040_acc_apsc_pa) != + ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); s->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); s->wdt = t6040_sleep_map(&pdev->dev, "wdt"); { From 824382d477fce21e8f6ad9e8d0896cfe5851ee47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:07:45 +0100 Subject: [PATCH 109/269] soc: apple: sleep: t6040: keep the .begin state in the driver's state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Whether .begin started a suspend to RAM was a file-scope variable, declared in the middle of the file. Keep it in struct t6040_sleep with the rest of the driver's state. No functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 1baafbe34476a..47194cfbde6fc 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -401,6 +401,8 @@ struct t6040_sleep { u64 mbox_pa; bool power_ops; bool wd2_armed; + /* Set by .begin for a suspend to RAM; .end only undoes what .begin did. */ + bool mem_begun; u64 siq_cfg; u64 cntkctl; /* Saved across the commit, restored on wake. */ @@ -2281,9 +2283,6 @@ static int t6040_sleep_valid(suspend_state_t state) return state == PM_SUSPEND_MEM && READ_ONCE(allow_entry); } -/* Set by .begin for a suspend to RAM; .end only undoes what .begin did. */ -static bool t6040_sleep_mem_begun; - /* * SMC key pmVC, the request for the VDD_CIO rail: the platform writes its enable mask | 0x100 * (0x101 on the J616s) while an ATC port's always-on side is powered, also at sleep. Linux @@ -2343,10 +2342,10 @@ static int t6040_sleep_begin(suspend_state_t state) * The PM core calls .begin and .end for suspend-to-idle too when there * are no s2idle_ops. s2idle needs nothing from this controller. */ - t6040_sleep_mem_begun = false; + s->mem_begun = false; if (state != PM_SUSPEND_MEM) return 0; - t6040_sleep_mem_begun = true; + s->mem_begun = true; t6040_sleep_set_dry(s, 0); s->returned = false; @@ -2819,9 +2818,9 @@ static void t6040_sleep_end(void) struct t6040_sleep *s = t6040_sleep; unsigned int dry = s->dry; - if (!t6040_sleep_mem_begun) + if (!s->mem_begun) return; - t6040_sleep_mem_begun = false; + s->mem_begun = false; t6040_sleep_clear_wd2(s); if (dry) dev_info(s->dev, "dry run %u over, WD2 off\n", dry); From d26899e1462b1c20ae85ec1166feebc39f800d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 18:58:00 +0100 Subject: [PATCH 110/269] mfd: macsmc: say what the SMC needs in the hibernation and WKTP comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comments on the hibernation callbacks retold how the restore came about ("our restored state", "the upstream teardown") instead of what the SMC needs, and the two WKTP comments named power management settings and constants, quoted an SMC log line and cited a measurement. Say what each step is for: the SMC keeps running while the image is written, the boot that loads the image restarts it, and the restore resets the transport and repeats the probe handshake. For WKTP, say which value goes when and what the SMC does without the 0. Comments only, no functional change. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 60 ++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 926643f10fa0f..dc12902e3e148 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -699,8 +699,9 @@ static int apple_smc_probe(struct platform_device *pdev) * and so holds a live RTKit session with the SMC when it jumps into the image. * The restored kernel then asks the firmware to start again (IOP power INIT) * and waits for a HELLO that a firmware already in a session never sends. So - * the loading kernel hands the SMC over quiesced, the state the upstream - * teardown leaves it in and from which a wake starts a new session. + * the loading kernel hands the SMC over quiesced, the state an unbind leaves + * it in (apple_smc_rtkit_shutdown()) and from which a wake starts a new + * session. */ static bool apple_smc_loading_image; static bool apple_smc_handed_over; @@ -750,17 +751,10 @@ static int apple_smc_freeze(struct device *dev) } /* - * Nothing host-side needs to be saved or torn down for hibernation. - * The image is written with the SMC coprocessor left running, and the - * driver keeps no SMC state that has to survive inside the image: every - * live handle (the RTKit session in smc->rtk, the SRAM mapping and the - * shared-memory window) is tied to the firmware instance that the boot - * firmware cold-restarts on resume, so it is rebuilt from scratch in - * apple_smc_restore(). Leaving the firmware running here is also what - * keeps thermal/power management working while the image is written. - * - * .poweroff (invoked after the image has been written, just before the - * machine powers off) shares this callback for the same reasons. + * Hibernating: the SMC keeps running, and with it thermal and power + * management, while the image is written and until the power-off + * (.poweroff shares this). Nothing is saved: the boot that loads the + * image restarts the SMC, and apple_smc_restore() starts a new session. */ return 0; } @@ -773,11 +767,7 @@ static int apple_smc_thaw(struct device *dev) return apple_smc_restore(dev); } - /* - * .thaw is reached only when image creation was aborted or has just - * finished and the current, live session continues unchanged. Since - * .freeze tore nothing down, there is nothing to restore. - */ + /* The image was written or abandoned: .freeze left the session running. */ return 0; } @@ -792,19 +782,12 @@ static int apple_smc_restore(struct device *dev) return apple_smc_thaw(dev); /* - * The system has been through a full cold boot to load the hibernation - * image: the boot firmware restarted the SMC co-processor, so the RTKit - * session that our restored state still describes belongs to a firmware - * instance that no longer exists. Every SMC command would otherwise - * poll-time-out. - * - * Our RTKit power-state bookkeeping was restored from the pre-hibernate - * image, which marks the (now dead) coprocessor as running; in that - * state apple_rtkit_wake() refuses to run. Reinitialize the transport - * first to drop the stale session back to the held-in-reset state - * (power state OFF, endpoint map cleared, mailbox RX restarted), which - * matches the post-devm_apple_rtkit_init() state probe starts from, then - * replay probe's bring-up handshake against the freshly booted firmware. + * The boot that loaded the image restarted the SMC. The RTKit session + * in the image belongs to a firmware instance that no longer runs, and + * its power state still reads running, which apple_rtkit_wake() + * refuses. Reset the transport to the state probe starts from (power + * state off, no endpoints, mailbox receive restarted) and repeat + * probe's handshake with the new instance. */ ret = apple_rtkit_reinit(smc->rtk); if (ret) @@ -965,9 +948,9 @@ static int apple_smc_notice_idle(struct apple_smc *smc) } /* - * WKTP, one byte for its power state 0. - * The platform writes it at the sleep request, before 'slpa' (measured on T6040: the WKTP write, - * then pmVC, then MBSE, then the AP idle notice). 0 leaves it unwritten. + * WKTP, one byte, the value for power state 0. It goes first at the sleep request, + * ahead of the other sleep keys ('slpa' among them) and the AP idle notice, as the platform + * does. 0 leaves it unwritten. */ static unsigned int sleep_wktp = 2; module_param(sleep_wktp, uint, 0644); @@ -997,11 +980,10 @@ static int apple_smc_sleep_commit(struct apple_smc *smc) } /* - * WKTP once a wake is a full one: 0, where a wake that stays dark keeps the key nonzero. - * Without the 0 the SMC takes all the time - * awake for a power nap, whether the sleep wrote its 2 or not: it does not leave its own fan - * policy for a manual fan request (the mode request times out), and under load it logs - * "Thermal pressure requested Sleep during power nap" and asks for a sleep once a second. + * WKTP once a wake is a full one: 0, where a wake that stays dark keeps the key nonzero, + * Without the 0 the SMC treats all the time awake as a power nap, whether + * the sleep wrote its 2 or not: it keeps its own fan policy against a manual fan request + * (the mode request times out), and under thermal load it requests a sleep once a second. * Negative writes nothing. */ static int wake_wktp; From 52e740fac02fd2ee67e7152b5ca7d82a29a0b961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:09:58 +0100 Subject: [PATCH 111/269] mfd: macsmc: drop a duplicate include and a never-NULL drvdata test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit was included twice. apple_smc_freeze() tested the driver data for NULL, but PM callbacks run only on a bound device, whose probe has set it. No functional change. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index dc12902e3e148..77af9f1c655a7 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -731,7 +730,7 @@ static int apple_smc_freeze(struct device *dev) struct apple_smc *smc = dev_get_drvdata(dev); /* Only the image-loading kernel, right before it enters the image. */ - if (apple_smc_loading_image && smc && apple_rtkit_is_running(smc->rtk)) { + if (apple_smc_loading_image && apple_rtkit_is_running(smc->rtk)) { int ret; apple_smc_write_flag(smc, SMC_KEY(NTAP), false); From 2f8d83fe943da2685b77abdcb637f5a90ddde126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:15:16 +0100 Subject: [PATCH 112/269] mfd: macsmc: report restore failures with dev_err() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple_smc_restore() is a PM callback, not a probe: dev_err_probe() there would record a deferral reason against a bound device, and its five messages had no trailing newline. Print them with dev_err() and the error code, and return the error as before. No functional change beyond the log text. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 77af9f1c655a7..7d3d071220273 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -789,26 +789,34 @@ static int apple_smc_restore(struct device *dev) * probe's handshake with the new instance. */ ret = apple_rtkit_reinit(smc->rtk); - if (ret) - return dev_err_probe(dev, ret, "Failed to reinit RTKit on restore"); + if (ret) { + dev_err(dev, "Failed to reinit RTKit on restore: %d\n", ret); + return ret; + } smc->boot_stage = APPLE_SMC_BOOTING; ret = apple_rtkit_wake(smc->rtk); - if (ret) - return dev_err_probe(dev, ret, "Failed to wake up SMC on restore"); + if (ret) { + dev_err(dev, "Failed to wake up SMC on restore: %d\n", ret); + return ret; + } ret = apple_rtkit_start_ep(smc->rtk, SMC_ENDPOINT); - if (ret) - return dev_err_probe(dev, ret, "Failed to start SMC endpoint on restore"); + if (ret) { + dev_err(dev, "Failed to start SMC endpoint on restore: %d\n", ret); + return ret; + } reinit_completion(&smc->init_done); reinit_completion(&smc->cmd_done); ret = apple_rtkit_send_message(smc->rtk, SMC_ENDPOINT, FIELD_PREP(SMC_MSG, SMC_MSG_INITIALIZE), NULL, false); - if (ret) - return dev_err_probe(dev, ret, "Failed to send init message on restore"); + if (ret) { + dev_err(dev, "Failed to send init message on restore: %d\n", ret); + return ret; + } if (wait_for_completion_timeout(&smc->init_done, msecs_to_jiffies(SMC_TIMEOUT_MS)) == 0) { dev_err(dev, "Timed out initializing SMC on restore\n"); @@ -829,8 +837,10 @@ static int apple_smc_restore(struct device *dev) count = 0; ret = -EIO; } - if (ret < 0) - return dev_err_probe(dev, ret, "Failed to get key count on restore"); + if (ret < 0) { + dev_err(dev, "Failed to get key count on restore: %d\n", ret); + return ret; + } smc->key_count = be32_to_cpu(count); /* From 613cb633b1d6dec7d96b19f66cb72b9f27bcd571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:35 +0100 Subject: [PATCH 113/269] mfd: macsmc: build the hibernation callbacks with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hibernation callbacks, the PM notifier and its registration sat in three #ifdef CONFIG_HIBERNATION blocks, with forward declarations only because the notifier was defined after probe. Define the notifier before probe, register it under IS_ENABLED(CONFIG_HIBERNATION), and set the callbacks with pm_sleep_ptr() like the others, so that every configuration compiles the same code. Without CONFIG_HIBERNATION the PM core never calls .freeze, .thaw, .poweroff or .restore, and pm_hibernate_is_restored() is false there. With it the object code is unchanged. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 92 ++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 7d3d071220273..9775486247ca6 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -571,10 +571,36 @@ static const struct apple_smc_wake_reason apple_smc_t8132_wake_reasons[] = { { 40, "acattach" }, { 41, "acdetach" }, { 42, "USB-C_plug" }, { 43, "USB2_plug" }, { 44, "USB2_wake" }, { 45, "aupo" }, { 48, "rtc" }, { } }; -#ifdef CONFIG_HIBERNATION -static struct notifier_block apple_smc_pm_nb; -static void apple_smc_pm_unregister(void *unused); -#endif + +/* + * The kernel that loads a hibernation image binds this driver like any boot + * and so holds a live RTKit session with the SMC when it jumps into the image. + * The restored kernel then asks the firmware to start again (IOP power INIT) + * and waits for a HELLO that a firmware already in a session never sends. So + * the loading kernel hands the SMC over quiesced, the state an unbind leaves + * it in (apple_smc_rtkit_shutdown()) and from which a wake starts a new + * session. + */ +static bool apple_smc_loading_image; +static bool apple_smc_handed_over; + +static int apple_smc_pm_notify(struct notifier_block *nb, unsigned long action, void *unused) +{ + if (action == PM_RESTORE_PREPARE) + apple_smc_loading_image = true; + else if (action == PM_POST_RESTORE) + apple_smc_loading_image = false; + return NOTIFY_DONE; +} + +static struct notifier_block apple_smc_pm_nb = { + .notifier_call = apple_smc_pm_notify, +}; + +static void apple_smc_pm_unregister(void *unused) +{ + unregister_pm_notifier(&apple_smc_pm_nb); +} static int apple_smc_probe(struct platform_device *pdev) { @@ -590,15 +616,15 @@ static int apple_smc_probe(struct platform_device *pdev) smc->wake_reasons = of_device_is_compatible(dev->of_node, "apple,t8132-smc") ? apple_smc_t8132_wake_reasons : apple_smc_t8140_wake_reasons; -#ifdef CONFIG_HIBERNATION /* First, so that a late probe still sees a resume start. */ - ret = register_pm_notifier(&apple_smc_pm_nb); - if (ret) - return ret; - ret = devm_add_action_or_reset(dev, apple_smc_pm_unregister, NULL); - if (ret) - return ret; -#endif + if (IS_ENABLED(CONFIG_HIBERNATION)) { + ret = register_pm_notifier(&apple_smc_pm_nb); + if (ret) + return ret; + ret = devm_add_action_or_reset(dev, apple_smc_pm_unregister, NULL); + if (ret) + return ret; + } mutex_init(&smc->mutex); spin_lock_init(&smc->lock); @@ -692,39 +718,8 @@ static int apple_smc_probe(struct platform_device *pdev) ARRAY_SIZE(apple_smc_t6050_described_devs)); } -#ifdef CONFIG_HIBERNATION -/* - * The kernel that loads a hibernation image binds this driver like any boot - * and so holds a live RTKit session with the SMC when it jumps into the image. - * The restored kernel then asks the firmware to start again (IOP power INIT) - * and waits for a HELLO that a firmware already in a session never sends. So - * the loading kernel hands the SMC over quiesced, the state an unbind leaves - * it in (apple_smc_rtkit_shutdown()) and from which a wake starts a new - * session. - */ -static bool apple_smc_loading_image; -static bool apple_smc_handed_over; - -static int apple_smc_pm_notify(struct notifier_block *nb, unsigned long action, void *unused) -{ - if (action == PM_RESTORE_PREPARE) - apple_smc_loading_image = true; - else if (action == PM_POST_RESTORE) - apple_smc_loading_image = false; - return NOTIFY_DONE; -} - -static struct notifier_block apple_smc_pm_nb = { - .notifier_call = apple_smc_pm_notify, -}; - static int apple_smc_restore(struct device *dev); -static void apple_smc_pm_unregister(void *unused) -{ - unregister_pm_notifier(&apple_smc_pm_nb); -} - static int apple_smc_freeze(struct device *dev) { struct apple_smc *smc = dev_get_drvdata(dev); @@ -852,7 +847,6 @@ static int apple_smc_restore(struct device *dev) return 0; } -#endif /* CONFIG_HIBERNATION */ static const struct of_device_id apple_smc_of_match[] = { { .compatible = "apple,t8152-smc" }, @@ -1233,12 +1227,10 @@ static int apple_smc_resume_noirq(struct device *dev) static const struct dev_pm_ops apple_smc_pm_ops = { .prepare = pm_sleep_ptr(apple_smc_prepare), .complete = pm_sleep_ptr(apple_smc_complete), -#ifdef CONFIG_HIBERNATION - .freeze = apple_smc_freeze, - .thaw = apple_smc_thaw, - .poweroff = apple_smc_freeze, - .restore = apple_smc_restore, -#endif + .freeze = pm_sleep_ptr(apple_smc_freeze), + .thaw = pm_sleep_ptr(apple_smc_thaw), + .poweroff = pm_sleep_ptr(apple_smc_freeze), + .restore = pm_sleep_ptr(apple_smc_restore), NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_smc_suspend_noirq, apple_smc_resume_noirq) }; From 2313aeb62e13f19ff564ce59d801d2eeff36f5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:36 +0100 Subject: [PATCH 114/269] power: reset: macsmc-reboot: log power-off and reboot at usual levels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every power-off and reboot printed its progress at KERN_EMERG, and the reboot notifier added a line about the PMU flag cell. Print the request at info level again, a refused off1 attempt as a warning, the final failure as an error and the accepted off1 at debug level, and drop the PMU flag line. An accepted off1 that does not cut the power still ends in the WARN after 5 s. Only log calls change. Signed-off-by: František Balázsy --- drivers/power/reset/macsmc-reboot.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/power/reset/macsmc-reboot.c b/drivers/power/reset/macsmc-reboot.c index 1f307fe63e73f..de8161a96eabe 100644 --- a/drivers/power/reset/macsmc-reboot.c +++ b/drivers/power/reset/macsmc-reboot.c @@ -129,7 +129,7 @@ static int macsmc_power_off(struct sys_off_data *data) struct macsmc_reboot *reboot = data->cb_data; int ret, i; - dev_emerg(reboot->dev, "Issuing power off (off1)\n"); + dev_info(reboot->dev, "Issuing power off (off1)\n"); /* * This is the only power-off path when Linux runs at EL2 without a @@ -143,14 +143,14 @@ static int macsmc_power_off(struct sys_off_data *data) ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)); if (ret >= 0) break; - dev_emerg(reboot->dev, "MBSE = off1 refused (%d), attempt %d\n", ret, i + 1); + dev_warn(reboot->dev, "MBSE = off1 refused (%d), attempt %d\n", ret, i + 1); mdelay(100); } if (ret < 0) { - dev_emerg(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n"); + dev_err(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n"); return NOTIFY_OK; } - dev_emerg(reboot->dev, "off1 accepted, waiting for the power to drop\n"); + dev_dbg(reboot->dev, "off1 accepted, waiting for the power to drop\n"); for (i = 0; i < 50; i++) mdelay(100); WARN_ONCE(1, "Unable to power off system: off1 accepted, power still on after 5 s\n"); @@ -193,7 +193,7 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio return NOTIFY_DONE; } - dev_emerg(reboot->dev, "Preparing for reboot (%p4ch)\n", &val); + dev_info(reboot->dev, "Preparing for reboot (%p4ch)\n", &val); /* On the Mac Mini, this will turn off the LED for power off */ if (apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val) < 0) @@ -212,8 +212,6 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio if (reboot->nvm.shutdown_flag && nvmem_cell_set_u8(reboot->nvm.shutdown_flag, shutdown_flag) < 0) dev_err(reboot->dev, "Failed to write shutdown_flag\n"); - dev_emerg(reboot->dev, "PMU flags set (shutdown_flag cell %s)\n", - reboot->nvm.shutdown_flag ? "present" : "absent"); return NOTIFY_OK; } From 70b3c01f239f80891e15a2aef088721ea1acd67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:05:03 +0100 Subject: [PATCH 115/269] irqchip/apple-aic: drop two dead conditions around the sleep syscore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aic_sleep_init() returns before register_syscore() when the IRQ_CFG buffer could not be allocated, so the test of that buffer guarding the registration was always true, and the same test in aic_syscore_suspend() never so. The comment on the registration also claimed a resume from disk resyncs the masks on every SoC, while the syscore is registered only where the sleep save is set up. Drop both tests and the comment. The registration and the save are unchanged. Signed-off-by: František Balázsy --- drivers/irqchip/irq-apple-aic.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index 27b2f3a8cd7d4..0ef641dfa283b 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -1165,7 +1165,7 @@ static int aic_syscore_suspend(void *data) struct aic_irq_chip *ic = aic_irqc; u32 die, i, off = 0; - if (!aic_sleep.irq_cfg || !pm_suspend_via_firmware()) + if (!pm_suspend_via_firmware()) return 0; aic_sleep.config = aic_ic_read(ic, AIC2_CONFIG); @@ -1319,9 +1319,7 @@ static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node if (WARN_ON(!aic_sleep.irq_cfg)) return; - /* A resume from disk needs the mask resync on every SoC. */ - if (aic_sleep.irq_cfg || IS_ENABLED(CONFIG_HIBERNATION)) - register_syscore(&aic_syscore); + register_syscore(&aic_syscore); } #else static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) From 40b36958120cbda0a96eec58261cecd04116831a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:05:03 +0100 Subject: [PATCH 116/269] pinctrl: apple: describe the resume order by what the pins need MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five comments told the wake's writes step by step. Say what the block needs instead: the global word has to be back before the pins, a pin takes its word in three writes after a power loss, and a block that loses every pin is saved whole except the reserved pins. Comments only, no functional change. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index a9d1c05ac4835..d10b3c2a1a6bc 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -59,13 +59,13 @@ struct apple_gpio_pinctrl { /* Pins this driver has written. Others belong to someone else. */ unsigned long *touched; /* - * The block whose every pin the platform puts back after a sleep that lost its power - * (apple,resume-restore-all), but those in gpio-reserved-ranges. + * A block whose every pin has to be put back after a sleep that lost its + * power (apple,resume-restore-all), but those in gpio-reserved-ranges. */ bool restore_all; unsigned long *no_restore; unsigned long *restored; - /* The block's word at REG_GLOBAL, which the platform keeps across a sleep too. */ + /* The block's word at REG_GLOBAL, kept across a sleep as well. */ u32 saved_global; bool saved_global_valid; struct apple_gpio_irq_group irqgrps[]; @@ -95,9 +95,9 @@ struct apple_gpio_pinctrl { #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22) #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5)) /* - * One word past the pins and the interrupt groups. The platform reads it when the - * system quiesces and writes it back on the wake, before it puts the pins back. It is 1 - * on a running J616s; with it lost, no pin reads its input any more. + * One word past the pins and the interrupt groups. The block loses it with its + * power, and it has to be back before the pins are: with it lost, no pin reads + * its input any more. It is 1 on a running J616s. */ #define REG_GLOBAL 0xc48 @@ -705,10 +705,10 @@ static const struct of_device_id apple_gpio_pinctrl_of_match[] = { MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match); /* - * A pin's whole word written the way the platform writes it: the + * A pin's whole word, in the order the pin takes it after a power loss: the * configuration first with the input buffer as it is (off after a power loss), then the * input enable in a write of its own, the lock last. A locked pin is left alone. Written - * in one go, with the input enable among the rest, a pin read 0 for good after the wake. + * in one go, with the input enable among the rest, the pin reads 0 for good after the wake. */ static void apple_gpio_safe_reconfigure(struct apple_gpio_pinctrl *pctl, unsigned int pin, u32 old, u32 cfg) @@ -746,9 +746,9 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); /* - * As the platform on the AP block at the system wake: every pin as the - * hardware has it now, but the ones the device tree names, which may not be written - * back. The pins of the panel's TCON bus, set up by the boot loader, are among them. + * A block that loses every pin: every pin as the hardware has it now, but the ones + * the device tree lists in gpio-reserved-ranges, which may not be written back. The + * pins of the panel's TCON bus, set up by the boot loader, are among them. */ pctl->saved_global_valid = false; if (pctl->restore_all) { From 9719ac0c328563ba49e4a19853727d4cee171531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:05:04 +0100 Subject: [PATCH 117/269] spmi: apple: say why the masks are rewritten in the early restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment over apple_spmi_restore_early() walked through how the missing interrupts were found. Say what happens instead: genpd keeps the noirq restore from running, the IRQ core does not rewrite the masks, so a device whose driver the boot kernel did not load stays masked unless the early restore rewrites them. Comment only, no functional change. Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index ad0e4e7824bfc..1dc4bca2c5124 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -739,16 +739,14 @@ static int apple_spmi_restore_noirq(struct device *dev) /* * A controller in a PM domain does not get the callback above: genpd's - * restore_noirq ends in the driver's resume_noirq, whatever callback - * genpd_finish_resume() was given (outside a wakeup path), and that has - * nothing to do after a resume from disk. The masks then stay as the boot - * kernel left them: its probe masked everything, and only its own drivers - * unmasked theirs before it loaded the image (on T6040 not the Type-C port - * controllers, whose driver is a module). The IRQ core disables lazily and - * unmasks only an IRQ it recorded as masked, so it does not write them again - * either, and the port controllers on nub_spmi_a0/a1 raise no event after the - * restore. genpd does not wrap the early restore: catch up there, before the - * devices on the bus are restored. + * restore_noirq calls the driver's resume_noirq instead (outside a wakeup + * path). The masks then stay as the boot kernel left them, everything masked + * but the interrupts of the drivers it loaded, and the IRQ core does not write + * them again (it disables lazily and unmasks only an IRQ it recorded as + * masked). A device whose driver the boot kernel did not load, such as a + * Type-C port controller on nub_spmi_a0/a1, would raise no event after the + * restore. genpd does not wrap the early restore, so the masks are rewritten + * there, before the devices on the bus are restored. */ static int apple_spmi_restore_early(struct device *dev) { From e57c498467a06533fa7ada9b0c1c4c7e92c72481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:50 +0100 Subject: [PATCH 118/269] pmdomain: apple: pmgr-pwrstate: set restore_noirq with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore callback sat in an #ifdef CONFIG_HIBERNATION block, with an #else that pointed .restore_noirq at the resume callback. Without CONFIG_HIBERNATION the PM core never calls .restore_noirq, so the #else was dead. Build the callback unconditionally and set it with pm_sleep_ptr() like the others. With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/pmdomain/apple/pmgr-pwrstate.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/pmdomain/apple/pmgr-pwrstate.c b/drivers/pmdomain/apple/pmgr-pwrstate.c index d807884d29022..ee7cf19d5816a 100644 --- a/drivers/pmdomain/apple/pmgr-pwrstate.c +++ b/drivers/pmdomain/apple/pmgr-pwrstate.c @@ -672,7 +672,6 @@ static int apple_pmgr_ps_resume_noirq(struct device *dev) return 0; } -#ifdef CONFIG_HIBERNATION /* * After a resume from disk genpd still records the domains as they were when * the image was taken, but the cold boot behind the resume left the hardware @@ -712,7 +711,6 @@ static int apple_pmgr_ps_restore_noirq(struct device *dev) apple_pmgr_ps_restore_hw(ps, 0); return 0; } -#endif static int apple_pmgr_reset_assert(struct reset_controller_dev *rcdev, unsigned long id) { @@ -1431,11 +1429,7 @@ static const struct dev_pm_ops apple_pmgr_ps_pm_ops = { /* Suspend-to-RAM resyncs after the sleep engine; a resume from disk after the cold boot. */ .resume_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), .thaw_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), -#ifdef CONFIG_HIBERNATION - .restore_noirq = apple_pmgr_ps_restore_noirq, -#else - .restore_noirq = pm_sleep_ptr(apple_pmgr_ps_resume_noirq), -#endif + .restore_noirq = pm_sleep_ptr(apple_pmgr_ps_restore_noirq), }; static struct platform_driver apple_pmgr_ps_driver = { From aa404cd17134386ee23e165b3a98f6281c9eefaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:52 +0100 Subject: [PATCH 119/269] dmaengine: apple-sio: report restart failures with dev_err() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sio_restart() runs from the restore callback, not from probe: dev_err_probe() there would record a deferral reason against a bound device. Print the six failures with dev_err() and return the error as before. Report the restart and a firmware that kept running, which happen on every resume from disk, at debug level. Only log calls change. Signed-off-by: František Balázsy --- drivers/dma/apple-sio.c | 44 ++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index d01e34750556e..bf57fe0f3ef34 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -2206,23 +2206,31 @@ static int sio_restart(struct sio_data *sio) } err = apple_rtkit_reinit(sio->rtk); - if (err) - return dev_err_probe(sio->dev, err, "restart: RTKit reinit\n"); + if (err) { + dev_err(sio->dev, "restart: RTKit reinit: %d\n", err); + return err; + } writel(CPU_CONTROL_RUN, sio->base + REG_CPU_CONTROL); err = apple_rtkit_boot(sio->rtk); - if (err) - return dev_err_probe(sio->dev, err, "restart: SIO did not boot\n"); + if (err) { + dev_err(sio->dev, "restart: SIO did not boot: %d\n", err); + return err; + } err = apple_rtkit_start_ep(sio->rtk, EP_SIO); - if (err) - return dev_err_probe(sio->dev, err, "restart: starting SIO endpoint\n"); + if (err) { + dev_err(sio->dev, "restart: starting SIO endpoint: %d\n", err); + return err; + } err = sio_call(sio, FIELD_PREP(SIOMSG_TYPE, MSG_START)); - if (err < 1) - return dev_err_probe(sio->dev, err ? err : -ENXIO, - "restart: starting SIO service\n"); + if (err < 1) { + err = err ? err : -ENXIO; + dev_err(sio->dev, "restart: starting SIO service: %d\n", err); + return err; + } if (sio->protocol != protocol) { dev_err(sio->dev, "restart: protocol changed from v%u to v%u\n", protocol, sio->protocol); @@ -2230,12 +2238,16 @@ static int sio_restart(struct sio_data *sio) } err = sio_send_dt_params(sio); - if (err < 0) - return dev_err_probe(sio->dev, err, "restart: boot-up parameters\n"); + if (err < 0) { + dev_err(sio->dev, "restart: boot-up parameters: %d\n", err); + return err; + } err = sio_send_shmem_params(sio); - if (err < 0) - return dev_err_probe(sio->dev, err, "restart: shared memory\n"); + if (err < 0) { + dev_err(sio->dev, "restart: shared memory: %d\n", err); + return err; + } return 0; } @@ -2307,12 +2319,12 @@ static __maybe_unused int sio_restore(struct device *dev) * reprepare their streams. */ if (readl(sio->base + REG_CPU_CONTROL) & CPU_CONTROL_RUN) { - dev_info(dev, "resume from disk: SIO still running, session kept\n"); + dev_dbg(dev, "resume from disk: SIO still running, session kept\n"); } else { err = sio_restart(sio); if (!err) - dev_info(dev, "resume from disk: SIO restarted (protocol v%u)\n", - sio->protocol); + dev_dbg(dev, "resume from disk: SIO restarted (protocol v%u)\n", + sio->protocol); else dev_err(dev, "resume from disk: SIO restart failed: %d\n", err); } From c17e76fc67bbf904a06b0caec6d43973dbe687d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:14:10 +0100 Subject: [PATCH 120/269] pinctrl: apple: report the per-sleep pin restore at debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every resume from a sleep that powered the block down printed up to three info lines per block (the global word, the pins put back, the pins Linux had not set up), and every resume from disk one more per block. That is per-cycle detail, and the resume of a configured pin count was debug level before. Print all four at debug level. Only log calls change. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index d10b3c2a1a6bc..8282cb9c52778 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -787,8 +787,8 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) if (pctl->saved_global_valid) { val = readl(pctl->base + REG_GLOBAL); writel(pctl->saved_global, pctl->base + REG_GLOBAL); - dev_info(dev, "resume: global word %#x, put back to %#x\n", val, - pctl->saved_global); + dev_dbg(dev, "resume: global word %#x, put back to %#x\n", val, + pctl->saved_global); pctl->saved_global_valid = false; } @@ -810,11 +810,11 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) if (pctl->cached) regcache_cache_bypass(pctl->map, false); if (restored) - dev_info(dev, "resume: %u of %u configured pins put back\n", restored, - bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); + dev_dbg(dev, "resume: %u of %u configured pins put back\n", restored, + bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); if (pctl->restored && !bitmap_empty(pctl->restored, pctl->pinctrl_desc.npins)) - dev_info(dev, "resume: pins not set up by Linux put back: %*pbl\n", - pctl->pinctrl_desc.npins, pctl->restored); + dev_dbg(dev, "resume: pins not set up by Linux put back: %*pbl\n", + pctl->pinctrl_desc.npins, pctl->restored); bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); return 0; @@ -856,7 +856,7 @@ static int __maybe_unused apple_gpio_pinctrl_restore_early(struct device *dev) armed++; } if (armed) - dev_info(dev, "resume from disk: %u interrupt pin(s) set up again\n", armed); + dev_dbg(dev, "resume from disk: %u interrupt pin(s) set up again\n", armed); return 0; } From 652cfe2a254bc73c3b48f73e121395630be27ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:14:11 +0100 Subject: [PATCH 121/269] spmi: apple: report the early mask rewrite at debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The early restore printed an info line on every resume from disk where it rewrote the masks. Print it at debug level. Only a log call changes. Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index 1dc4bca2c5124..08f26adc55cc0 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -762,7 +762,7 @@ static int apple_spmi_restore_early(struct device *dev) /* Commands can run by now: none may have its reply acknowledged away. */ scoped_guard(mutex, &spmi->fifo_lock) apple_spmi_hib_resync_irqs(spmi); - dev_info(dev, "resume from disk: interrupt masks rewritten in the early restore\n"); + dev_dbg(dev, "resume from disk: interrupt masks rewritten in the early restore\n"); return 0; } From c9afea3b3eb6ce9ab7fe83a2438f238bd9600b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:19:09 +0100 Subject: [PATCH 122/269] pinctrl: apple: drop the restored-pin report and never-true tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bitmap of restored pins that Linux had not set up existed only for a debug message; a pin of a restore-all block is put back whether Linux set it up or not. The buffer of saved words is allocated for every block since probe fails without it, so the suspend and resume tests for it were never true. pm_sleep_ptr() keeps the early restore referenced in every configuration, so it needs no __maybe_unused. Drop all three. What is saved, written back and when is unchanged. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 8282cb9c52778..e027956d4659a 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -64,7 +64,6 @@ struct apple_gpio_pinctrl { */ bool restore_all; unsigned long *no_restore; - unsigned long *restored; /* The block's word at REG_GLOBAL, kept across a sleep as well. */ u32 saved_global; bool saved_global_valid; @@ -640,8 +639,7 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) u32 start, count; pctl->no_restore = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); - pctl->restored = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); - if (!pctl->no_restore || !pctl->restored) + if (!pctl->no_restore) return -ENOMEM; /* The pins the firmware lists as no-restore, as (first pin, count) pairs. */ for (i = 0; n > 0 && i + 1 < (unsigned int)n; i += 2) @@ -740,8 +738,7 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i; - if (!pctl->saved || pm_suspend_target_state != PM_SUSPEND_MEM || - !pm_suspend_via_firmware()) + if (pm_suspend_target_state != PM_SUSPEND_MEM || !pm_suspend_via_firmware()) return 0; bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); @@ -781,7 +778,7 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i, val, restored = 0; - if (!pctl->saved || !pm_resume_via_firmware()) + if (!pm_resume_via_firmware()) return 0; if (pctl->saved_global_valid) { @@ -795,8 +792,6 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) /* The saved words came from the cache; the hardware is what to compare. */ if (pctl->cached) regcache_cache_bypass(pctl->map, true); - if (pctl->restored) - bitmap_zero(pctl->restored, pctl->pinctrl_desc.npins); for_each_set_bit(i, pctl->saved_valid, pctl->pinctrl_desc.npins) { if (regmap_read(pctl->map, REG_GPIO(i), &val)) val = 0; @@ -804,17 +799,12 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) continue; apple_gpio_safe_reconfigure(pctl, i, val, pctl->saved[i]); restored++; - if (pctl->restored && !test_bit(i, pctl->touched)) - set_bit(i, pctl->restored); } if (pctl->cached) regcache_cache_bypass(pctl->map, false); if (restored) dev_dbg(dev, "resume: %u of %u configured pins put back\n", restored, bitmap_weight(pctl->saved_valid, pctl->pinctrl_desc.npins)); - if (pctl->restored && !bitmap_empty(pctl->restored, pctl->pinctrl_desc.npins)) - dev_dbg(dev, "resume: pins not set up by Linux put back: %*pbl\n", - pctl->pinctrl_desc.npins, pctl->restored); bitmap_zero(pctl->saved_valid, pctl->pinctrl_desc.npins); return 0; @@ -834,7 +824,7 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) * in the driver's resume_noirq). An uncached block reads back only what the * boot left, so this does nothing there. */ -static int __maybe_unused apple_gpio_pinctrl_restore_early(struct device *dev) +static int apple_gpio_pinctrl_restore_early(struct device *dev) { struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i, mode, val, armed = 0; From 4368700c7d187c54ca8eddf50e7950795dc70576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:19:10 +0100 Subject: [PATCH 123/269] spmi: apple: set the restore callbacks with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two restore callbacks sat in an #ifdef CONFIG_HIBERNATION block, with an #else in the dev_pm_ops that pointed .restore_noirq at the resume callback. Without CONFIG_HIBERNATION the PM core never calls the restore callbacks, and pm_hibernate_is_restored() is false there, so the restore callback falls back to the resume one by itself. Build both unconditionally and set them with pm_sleep_ptr(). With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index 08f26adc55cc0..7d73d0cf1d60e 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -687,7 +687,6 @@ static int apple_spmi_probe(struct platform_device *pdev) return 0; } -#ifdef CONFIG_HIBERNATION /* * After a resume from disk the controller holds the boot kernel's masks and * pending interrupts, not this kernel's: drop the pending ones and rewrite @@ -767,8 +766,6 @@ static int apple_spmi_restore_early(struct device *dev) return 0; } -#endif - /* * Suspend-to-RAM parks the notification queue; a resume from disk rewrites the * masks, in the early restore where a PM domain kept the noirq one from running. @@ -779,12 +776,8 @@ static const struct dev_pm_ops apple_spmi_pm_ops = { .freeze_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), .thaw_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), .poweroff_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), -#ifdef CONFIG_HIBERNATION - .restore_noirq = apple_spmi_restore_noirq, - .restore_early = apple_spmi_restore_early, -#else - .restore_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), -#endif + .restore_noirq = pm_sleep_ptr(apple_spmi_restore_noirq), + .restore_early = pm_sleep_ptr(apple_spmi_restore_early), }; static const struct of_device_id apple_spmi_match_table[] = { From 6c5a94a81d6478a71716bd85cc4426b60f3a82d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:21:47 +0100 Subject: [PATCH 124/269] soc: apple: Kconfig: tidy the T6040 sleep and aura hibernation texts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 sleep prompt carried a "(refusal-first)" tag that the help text explains anyway, and its help named a check before the sleep instead of what has to be off. The aura hibernation help had a line past 80 columns. Drop the tag, say which display power states have to be off, and rewrap both paragraphs. Text only; symbols and dependencies are unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index c51eaf3a8fb18..22b579e8f21b1 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -127,7 +127,7 @@ config APPLE_SOC_SLEEP_KUNIT_TEST If unsure, say N. config APPLE_T6040_SLEEP - bool "Apple T6040 system sleep (refusal-first)" + bool "Apple T6040 system sleep" depends on APPLE_SOC_SLEEP depends on MFD_MACSMC=y select MFD_SYSCON @@ -136,12 +136,12 @@ config APPLE_T6040_SLEEP boards whose device tree enables its sleep controller. The T6040 has its own sequences and refuses first: its suspend handler lists every blocker in the live state (EL2, the boot object's return - path, the CPUs that can park, the display power states the platform checks - before it sleeps). Unless t6040_sleep.allow_entry=1 is given and no - blocker remains, a request for suspend-to-RAM prints the blockers - and enters nothing, and suspend-to-idle stays the default sleep. - With it the SoC is powered down to suspend-to-RAM and comes back - through the boot object; the AON watchdog is the net over the + path, the CPUs that can park, the display power states that have to + be off before a sleep). Unless t6040_sleep.allow_entry=1 is given + and no blocker remains, a request for suspend-to-RAM prints the + blockers and enters nothing, and suspend-to-idle stays the default + sleep. With it the SoC is powered down to suspend-to-RAM and comes + back through the boot object; the AON watchdog is the net over the whole cycle (t6040_sleep.sleep_wd_secs at the power-down itself, t6040_sleep.wd2_secs over a dry run). @@ -358,10 +358,11 @@ config APPLE_AURA_HIBERNATE help Under aura, the boot loader and the firmware keep memory alive below Linux's allocations, and on Apple M4 Pro boots the boot - firmware places it at a different address every time. With aura's fixed layout - (/chosen/aura,hibernate-layout) this keeps every reserved range out - of the hibernation image; without it, hibernation and resume are - refused instead of overwriting the running boot loader. + firmware places it at a different address every time. With + aura's fixed layout (/chosen/aura,hibernate-layout) this keeps + every reserved range out of the hibernation image; without it, + hibernation and resume are refused instead of overwriting the + running boot loader. config APPLE_DISPLAY_GATE bool "Apple internal display enable gate" From adb570c940a36b6bbb7bd94e137a433112690298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:06:48 +0100 Subject: [PATCH 125/269] soc: apple: sep: hold no sleep for a restore the notifier refuses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A kernel that attached the enclave refuses to load a hibernation image over it: its PM_RESTORE_PREPARE notifier returns NOTIFY_BAD. The PM core then sends PM_POST_RESTORE to the notifiers that ran before the one that refused, not to that one. The notifier had already marked a sleep as held before it asked the driver, and nothing took the mark back: diag/sleep read "held" for the rest of that boot, or until a later sleep ended, although the boot went on with the enclave in use. Mark the sleep only once the restore is accepted. The host test runs the notifier now: a refused restore leaves nothing held, an accepted one is released by PM_POST_RESTORE, and a mutation that marks the sleep before the refusal is caught. Signed-off-by: František Balázsy --- drivers/soc/apple/pm_shim.c | 3 +- tools/asahi/sep-suspend-errors-tests.py | 54 +++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 689217e8d94cc..1536fe67b00bf 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -164,7 +164,6 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, event(SEP_PM_HIBERNATE); return NOTIFY_OK; case PM_RESTORE_PREPARE: - sep_diag_sleep(true); /* * This kernel is about to load an image. If it registered with the * enclave, the enclave keeps that session and may DMA into memory @@ -175,6 +174,8 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, pr_err("apple_sep: this kernel attached the SEP; refusing to restore a hibernation image over it\n"); return NOTIFY_BAD; } + /* Not before the refusal: no PM_POST_RESTORE comes to the one who refused. */ + sep_diag_sleep(true); return NOTIFY_OK; case PM_POST_RESTORE: event(SEP_PM_HIBERNATE_WARM); diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index 113d4f3a0bd6a..10f2a4b904b2b 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -2,8 +2,8 @@ # SPDX-License-Identifier: GPL-2.0-only OR MIT """Compile actual SEP commit/error paths with host-only dependency stubs. -Extract current C stop/finish/prepare/noirq, provider poll-one and registration, -Rust wrappers/enter/leave/exit/kick, and the kernel polling macros verbatim. +Extract current C stop/finish/prepare/noirq, the PM notifier, provider poll-one and +registration, Rust wrappers/enter/leave/exit/kick, and the kernel polling macros verbatim. C fatal paths are observed wholly within C, never by unwinding a Rust frame. Virtual time/MMIO only test AP control decisions, not firmware timing or safety. No target access or kernel build occurs. @@ -88,6 +88,8 @@ def replace_once(source, before, after): 'msg = apple_mbox_read_msg(mbox);\n\t\tmbox->rx(mbox, msg, mbox->cookie);\n\t\tret = 2;'), 'post-send-wake-refusal': ('sep_pm_commit_stop', 'c->sent = true;', 'c->sent = true;\n\tif (pm_wakeup_pending()) return -EBUSY;'), + 'held-before-refusal': ('sep_pm_notify', 'if (event(SEP_PM_RESTORE) > 0) {', + 'sep_diag_sleep(true);\n\t\tif (event(SEP_PM_RESTORE) > 0) {'), } C_PREFIX = r''' @@ -162,6 +164,22 @@ def replace_once(source, before, after): static unsigned queued, consumed; static ktime_t now; static bool (*sep_pm_told)(void); +/* The PM notifier: its events go to the driver, and the diag state shows a sleep held. */ +struct notifier_block { int unused; }; +enum { NOTIFY_DONE, NOTIFY_OK, NOTIFY_BAD = 0x8002 }; +enum { PM_HIBERNATION_PREPARE = 1, PM_POST_HIBERNATION, PM_SUSPEND_PREPARE, PM_POST_SUSPEND, + PM_RESTORE_PREPARE, PM_POST_RESTORE }; +#define pr_err(...) ((void)0) +static int (*sep_pm_event)(int event); +static struct device *sep_pm_dev; +static int sep_pm_cold_base, cold_restarts, held, sleeps_ended, reprobes; +static bool restored; +static void sep_diag_sleep(bool entering) { + if (entering) held = 1; else if (held) { held = 0; ++sleeps_ended; } +} +static int apple_pmp_cold_restarts(void) { return cold_restarts; } +static bool pm_hibernate_is_restored(void) { return restored; } +static void sep_pm_schedule_reprobe(struct device *d) { ++reprobes; } static bool yes(void) { return true; } static bool no(void) { return false; } static bool pm_suspend_via_firmware(void) { return firmware; } @@ -268,6 +286,8 @@ def replace_once(source, before, after): resource_bytes = 4; close_gate = 1; early_gate = 0; read_error_after = 1; queued = consumed = 0; message_in_lock = matched_read = false; now = 0; fatal_armed = false; fatal_message[0] = 0; sep_pm_told = yes; + sep_pm_event = NULL; sep_pm_dev = &device; sep_pm_cold_base = cold_restarts = 0; + held = sleeps_ended = reprobes = 0; restored = false; } void parameter(int which, int value) { switch (which) { @@ -443,8 +463,31 @@ def replace_once(source, before, after): fixture(); sep_pm_told = NULL; assert(noirq() == 0 && !lookups); fixture(); sep_pm_told = no; assert(noirq() == 0 && !lookups); } +static int attached, last_event; +static int driver_event(int event) { + last_event = event; return event == SEP_PM_RESTORE ? attached : 0; +} +static void test_notifier(void) { + struct notifier_block nb; + // No driver registered: the chain goes on untouched. + fixture(); assert(sep_pm_notify(&nb, PM_RESTORE_PREPARE, NULL) == NOTIFY_DONE && !held); + // A kernel that attached the enclave refuses to load an image. The PM core sends no + // PM_POST_RESTORE to the notifier that refused, so nothing may be left held. + fixture(); sep_pm_event = driver_event; attached = 1; + assert(sep_pm_notify(&nb, PM_RESTORE_PREPARE, NULL) == NOTIFY_BAD); + assert(last_event == SEP_PM_RESTORE && !held && !sleeps_ended); + // One that did not goes on, and the end of the restore ends the hold. + attached = 0; + assert(sep_pm_notify(&nb, PM_RESTORE_PREPARE, NULL) == NOTIFY_OK && held); + assert(sep_pm_notify(&nb, PM_POST_RESTORE, NULL) == NOTIFY_OK); + assert(last_event == SEP_PM_HIBERNATE_WARM && !held && sleeps_ended == 1); + // A suspend holds and releases the same way. + assert(sep_pm_notify(&nb, PM_SUSPEND_PREPARE, NULL) == NOTIFY_OK && held); + assert(sep_pm_notify(&nb, PM_POST_SUSPEND, NULL) == NOTIFY_OK && !held && sleeps_ended == 2); +} int main(void) { test_prepare_and_cleanup(); test_unsent(); test_provider(); test_sent(); test_poll_contract(); + test_notifier(); puts("SEP C commit controls passed"); return 0; } #endif @@ -669,6 +712,9 @@ def sources(source, contracts, mutation): pm = (source / 'drivers/soc/apple/pm.rs').read_text() sep = (source / 'drivers/soc/apple/sep.rs').read_text() shim = (source / 'drivers/soc/apple/pm_shim.c').read_text() + events = re.findall(r'^#define SEP_PM_[A-Z_]+\s+\d+$', + (source / 'drivers/soc/apple/shim.h').read_text(), re.M) + assert len(events) == 6, events mailbox = (source / 'drivers/soc/apple/mailbox.c').read_text() sleep = (source / 'drivers/soc/apple/sleep.c').read_text() registry = (source / 'drivers/soc/apple/sleep-registry.c').read_text() @@ -676,7 +722,8 @@ def sources(source, contracts, mutation): names = { 'shim': ['sep_pm_wait_gated', 'sep_pm_clear_dev_disable', 'sep_pm_dart_restore', 'sep_pm_commit_rx', 'sep_pm_commit_take_rx', 'sep_pm_commit_stop', - 'sep_pm_commit_prepare', 'sep_pm_commit_finish', 'sep_pm_suspend_noirq'], + 'sep_pm_commit_prepare', 'sep_pm_commit_finish', 'sep_pm_suspend_noirq', + 'sep_pm_notify'], 'mailbox': ['apple_mbox_poll_one_supported', 'apple_mbox_poll_one'], 'sleep': ['apple_sleep_reg_commit_stops', 'apple_sleep_reg_enclave_admit'], 'registry': ['apple_sleep_commit_stops_coprocs', 'apple_sleep_enclave_register', @@ -695,6 +742,7 @@ def sources(source, contracts, mutation): c = C_PREFIX + '\n' + '\n'.join(macro(iopoll, n) for n in macros) c += '\n' + '\n'.join(re.search(r'^#define ' + n + r'\s+.*$', shim, re.M).group() for n in ['SEP_PM_POLL_US', 'SEP_PM_TIMEOUT_US']) + c += '\n' + '\n'.join(events) start = shim.index('static struct sep_pm_commit {') c += '\n' + shim[start:shim.index('} sep_pm_commit;', start) + len('} sep_pm_commit;')] # Use the actual provider helper, with only removal/capability dependency state From 3678af9b8c60e63cde54d9c274e61ede15d6946e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:23:24 +0100 Subject: [PATCH 126/269] rtc: apple-pmu: have the alarm set again after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A resume from disk goes through a boot, and the PMU's alarm does not come back as the image left it. The kernel that loads the image stops an alarm that is no longer ahead when it binds this driver, and the boot firmware leaves the comparator close to the end of its range when it has no wake of its own. An alarm whose time passed with the machine off had its event acknowledged there. The restored kernel's RTC core still counted the alarm as set: its time stayed in wakealarm, a new alarm was refused as busy, and an alarm still ahead never fired. After a real restore, have the RTC core go through its timers: an alarm that is due is reported, a later one is written to the PMU again, and with none the PMU's alarm is stopped. The power-off callback does not cover this. Only a hibernation that powers off through platform hibernation ops calls it, and no Apple machine has those: the power-off or reboot after the image leaves the alarm armed. Say so where the callback and the prepare comment claimed otherwise. The host test covers the restore, with two mutations. Signed-off-by: František Balázsy --- drivers/rtc/rtc-apple-pmu.c | 25 +++++++++++++++++++++++-- tools/asahi/pmu-rtc-tests.py | 19 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/drivers/rtc/rtc-apple-pmu.c b/drivers/rtc/rtc-apple-pmu.c index 113e17aac5d42..e4bf60b1b1b32 100644 --- a/drivers/rtc/rtc-apple-pmu.c +++ b/drivers/rtc/rtc-apple-pmu.c @@ -435,8 +435,8 @@ static int apple_pmu_rtc_prepare(struct device *dev) * Suspend to idle keeps the PMU powered and the alarm wakes the system. * A firmware-driven suspend programs the wake sources itself, and the * alarm stays armed in the PMU, which stays powered. Hibernation (the - * target state stays PM_SUSPEND_ON) is accepted as well: the alarm is - * stopped before the power-off below. Only a sleep state without any of + * target state stays PM_SUSPEND_ON) is accepted as well: the restore + * below has the alarm set again. Only a sleep state without any of * these has no way to bring the alarm back. */ if (pm_suspend_target_state != PM_SUSPEND_TO_IDLE && @@ -446,6 +446,8 @@ static int apple_pmu_rtc_prepare(struct device *dev) } /* + * Only a hibernation that powers off through platform hibernation ops gets + * here; a plain power-off or reboot after the image leaves the alarm as it is. * Whether an armed PMU alarm powers the machine on again after the * hibernation power-off is not established, so do not leave one armed. */ @@ -462,9 +464,28 @@ static int apple_pmu_rtc_poweroff(struct device *dev) return 0; } +/* + * A resume from disk went through a boot. The firmware and the kernel that + * loaded the image had the PMU in between: either may have stopped the alarm + * or set the comparator anew, and the alarm's time may have passed with the + * machine off, its event acknowledged by one of them. The RTC core still + * counts the alarm as set. Have it go through its timers: an alarm that is due + * is reported, a later one is written to the PMU again, and with none set the + * PMU's alarm is stopped. Not after a hibernation given up before its image. + */ +static int apple_pmu_rtc_restore(struct device *dev) +{ + struct apple_pmu_rtc *rtc = dev_get_drvdata(dev); + + if (pm_hibernate_is_restored()) + rtc_update_irq(rtc->clock, 1, RTC_IRQF | RTC_AF); + return 0; +} + static const struct dev_pm_ops apple_pmu_rtc_pm = { .prepare = apple_pmu_rtc_prepare, .poweroff = apple_pmu_rtc_poweroff, + .restore = apple_pmu_rtc_restore, }; static void apple_pmu_rtc_release(void *context) diff --git a/tools/asahi/pmu-rtc-tests.py b/tools/asahi/pmu-rtc-tests.py index d1ab41dbeb44d..c2037693c3d05 100644 --- a/tools/asahi/pmu-rtc-tests.py +++ b/tools/asahi/pmu-rtc-tests.py @@ -28,6 +28,10 @@ 'baku-limit': ('.max_delay = U32_MAX,', '.max_delay = APPLE_PMU_RTC_MAX_DELAY,'), 'set-no-round': ('(counter + BIT(APPLE_PMU_RTC_HZ_SHIFT - 1))', '(counter)'), 'set-negative': ('if (offset < 0 || offset', 'if (offset'), + 'restore-unrestored': ('if (pm_hibernate_is_restored())', + 'if (pm_hibernate_is_restored() || true)'), + 'restore-nothing': ('if (pm_hibernate_is_restored())', + 'if (pm_hibernate_is_restored() && false)'), } PRELUDE = r''' @@ -77,8 +81,9 @@ int ops, fail; }; static int disabled, pm_suspend_target_state = PM_SUSPEND_TO_IDLE; -static bool firmware_suspend; +static bool firmware_suspend, restored; static bool pm_suspend_via_firmware(void) { return firmware_suspend; } +static bool pm_hibernate_is_restored(void) { return restored; } static inline __attribute__((unused)) s64 sign_extend64(u64 v, int bit) { return (s64)(v << (63 - bit)) >> (63 - bit); } static u64 get_unaligned_le64(const void *p) { u64 v; memcpy(&v, p, 8); return v; } static void mutex_lock(struct mutex *m) { (void)m; } @@ -335,7 +340,7 @@ firmware_suspend = true; assert(!apple_pmu_rtc_prepare(&dev)); firmware_suspend = false; - /* Hibernation is accepted: its power-off stops the alarm, and goes on if that fails. */ + /* Hibernation is accepted. A platform power-off stops the alarm, and goes on if that fails. */ pm_suspend_target_state = PM_SUSPEND_ON; assert(!apple_pmu_rtc_prepare(&dev)); assert(!apple_pmu_rtc_set_alarm(&dev, &alarm) && (map.bytes[map.ctrl] & 0x40)); @@ -344,6 +349,16 @@ map.ops = 0; map.fail = 1; assert(!apple_pmu_rtc_poweroff(&dev)); pm_suspend_target_state = PM_SUSPEND_TO_IDLE; + /* + * After a resume from disk the RTC core goes through its alarm again, without + * a PMU access of the driver's own; a hibernation given up before its image + * leaves the alarm alone. + */ + reset(v); + assert(!apple_pmu_rtc_restore(&dev) && clock_model.events == 0 && map.ops == 0); + restored = true; + assert(!apple_pmu_rtc_restore(&dev) && clock_model.events == 1 && map.ops == 0); + restored = false; } int main(void) { run_variant(BAKU); run_variant(ABBEY); run_variant(STOWE); run_variant(MAVERICK); From 9eabdf5241fb4bcc9f2a51777a16feb9fc9ac002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:34:46 +0100 Subject: [PATCH 127/269] pinctrl: apple: put back every pin the driver set after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a resume from disk the block holds what the boot loader and the kernel that loaded the image set up, while the register cache of the restored kernel still holds the words of the image. The early restore wrote back only the pins set up as interrupts and left the others to their consumers. A consumer does not reach the pin either: its write goes through regmap_update_bits(), which compares with the cache and skips a word the cache already has. An output set again to the level it had before the hibernation, and a pin function selected once at probe, stayed as the boot left them. Compare every pin this driver has written with the block, and write the cached word back where they differ. The write is the one the resume from a suspend that cut the block's power uses: the configuration first, the input enable in a write of its own, the lock last, and a locked pin left alone. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index e027956d4659a..75fa4aba27017 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -813,12 +813,14 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) /* * A resume from disk is a cold boot: the block holds what the boot loader and * the boot kernel set up, not the restored kernel's pins, while the regmap - * cache still holds the restored kernel's words. An interrupt does not get - * its pin back by itself: the IRQ core disables lazily and unmasks only an - * IRQ it recorded as masked, and regmap_update_bits() skips a write the cache - * already has. So an edge interrupt armed before the hibernation, such as the - * HDMI hotplug detect, stays off for good. Write back the pins this driver set - * up as interrupts, from the cache; the other pins are left to their consumers. + * cache still holds the restored kernel's words. Neither an interrupt nor a + * consumer gets its pin back by itself: the IRQ core disables lazily and + * unmasks only an IRQ it recorded as masked, and regmap_update_bits() skips a + * write the cache already has. So an edge interrupt armed before the + * hibernation, such as the HDMI hotplug detect, stays off for good, and an + * output that its consumer sets to the level it had before is never driven. + * Write back, from the cache, every pin this driver set up that the block + * holds differently, in the order a pin takes its word. * * The early restore, as a PM domain does not wrap it (its restore_noirq ends * in the driver's resume_noirq). An uncached block reads back only what the @@ -827,7 +829,7 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) static int apple_gpio_pinctrl_restore_early(struct device *dev) { struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); - unsigned int i, mode, val, armed = 0; + unsigned int i, cfg, val, restored = 0; /* Hibernation aborted before the image: the block kept its state. */ if (!pm_hibernate_is_restored()) @@ -837,16 +839,18 @@ static int apple_gpio_pinctrl_restore_early(struct device *dev) return 0; for_each_set_bit(i, pctl->touched, pctl->pinctrl_desc.npins) { - if (regmap_read(pctl->map, REG_GPIO(i), &val)) + if (regmap_read(pctl->map, REG_GPIO(i), &cfg)) continue; - mode = FIELD_GET(REG_GPIOx_MODE, val); - if (mode < REG_GPIOx_IN_IRQ_HI || mode > REG_GPIOx_IN_IRQ_ANY) + /* Past the cache, which every write below leaves at @cfg again. */ + val = readl_relaxed(pctl->base + REG_GPIO(i)); + if (val == cfg) continue; - regmap_write(pctl->map, REG_GPIO(i), val); - armed++; + apple_gpio_safe_reconfigure(pctl, i, val, cfg); + restored++; } - if (armed) - dev_dbg(dev, "resume from disk: %u interrupt pin(s) set up again\n", armed); + if (restored) + dev_dbg(dev, "resume from disk: %u of %u configured pins put back\n", restored, + bitmap_weight(pctl->touched, pctl->pinctrl_desc.npins)); return 0; } From 993255e06cc500fff55a8882737ebffc2a2240fd Mon Sep 17 00:00:00 2001 From: AxxzyWasTaken Date: Thu, 12 Nov 2026 08:50:30 +0000 Subject: [PATCH 128/269] spmi: apple: drop replies left by firmware at a firmware-driven resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While Linux is away the queue keeps being used: aura's wake path and the platform's sleep code write the PMU through it. Every command the driver sent was answered before the suspend, so a reply still waiting at the resume is someone else's, and the next command takes it as its own. On the M4 Max that was the RTC's acknowledge of the alarm that woke the machine ("unexpected SPMI response 0x8b30"), after which the RTC refused every later suspend. Read such replies out at resume_noirq after a firmware-driven suspend. Signed-off-by: AxxzyWasTaken Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index 7d73d0cf1d60e..1165906264738 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -616,6 +616,23 @@ static int apple_spmi_resume_noirq(struct device *dev) return 0; guard(mutex)(&spmi->fifo_lock); + /* + * Firmware and the platform's sleep code use this queue while Linux is + * away. Every command of ours was answered before the suspend, so any + * reply waiting now is someone else's, and would be read as the reply + * to our next command. + */ + if (pm_resume_via_firmware()) { + unsigned int stale = 0; + + while (!(readl(spmi->regs + spmi->hw->status) & spmi->hw->rx_empty) && + stale < 64) { + readl(spmi->regs + spmi->hw->reply); + stale++; + } + if (stale) + dev_info(dev, "dropped %u stale reply words after the wake\n", stale); + } raw_spin_lock_irqsave(&spmi->irq_mask_lock, flags); /* Whether the enables survive a real sleep has not been measured. */ if (pm_resume_via_firmware()) { From 1d474d828bc88b03a57788f5c38de1be8c0feb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:05 +0100 Subject: [PATCH 129/269] spmi: apple: rewrite the masks once after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a resume from disk the controller holds the masks and the pending interrupts of the kernel that loaded the image. The driver rewrote them in the noirq restore and, for a controller in a PM domain, in the early restore, with a flag to keep the two apart: for a device in a domain genpd calls the driver's noirq resume, not its noirq restore. The early restore runs for every controller, in a domain or not, and still before any device on the bus is restored. There the rewrite can also take the command lock, so that no reply is acknowledged away under a command. Do the rewrite there only: the noirq restore becomes the noirq resume, as genpd already made it for the controllers in a domain, and the flag goes. Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 44 +++++++--------------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index 1165906264738..bac65a59e5ea4 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -117,8 +117,6 @@ struct apple_spmi { bool notify_irq; bool notify_parked; bool prev_fail; - /* The noirq restore rewrote the masks after a resume from disk. */ - bool hib_resynced; }; static void apple_spmi_irq_ack_raw(struct apple_spmi *spmi, u32 irq) @@ -737,41 +735,24 @@ static void apple_spmi_hib_resync_irqs(struct apple_spmi *spmi) raw_spin_unlock_irqrestore(&spmi->irq_mask_lock, flags); } -static int apple_spmi_restore_noirq(struct device *dev) -{ - struct apple_spmi *spmi = dev_get_drvdata(dev); - - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return apple_spmi_resume_noirq(dev); - if (spmi->irq < 0) - return 0; - - apple_spmi_hib_resync_irqs(spmi); - spmi->hib_resynced = true; - - return 0; -} - /* - * A controller in a PM domain does not get the callback above: genpd's - * restore_noirq calls the driver's resume_noirq instead (outside a wakeup - * path). The masks then stay as the boot kernel left them, everything masked + * Left alone, the masks stay as the boot kernel left them, everything masked * but the interrupts of the drivers it loaded, and the IRQ core does not write * them again (it disables lazily and unmasks only an IRQ it recorded as * masked). A device whose driver the boot kernel did not load, such as a * Type-C port controller on nub_spmi_a0/a1, would raise no event after the - * restore. genpd does not wrap the early restore, so the masks are rewritten - * there, before the devices on the bus are restored. + * restore. + * + * In the early restore, for every controller alike: one in a PM domain gets no + * restore_noirq of its own, as genpd calls the driver's resume_noirq there + * (outside a wakeup path), and genpd does not wrap the early restore. It still + * runs before the devices on the bus are restored. */ static int apple_spmi_restore_early(struct device *dev) { struct apple_spmi *spmi = dev_get_drvdata(dev); - if (spmi->hib_resynced) { - spmi->hib_resynced = false; - return 0; - } + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ if (!pm_hibernate_is_restored() || spmi->irq < 0) return 0; @@ -785,15 +766,10 @@ static int apple_spmi_restore_early(struct device *dev) /* * Suspend-to-RAM parks the notification queue; a resume from disk rewrites the - * masks, in the early restore where a PM domain kept the noirq one from running. + * masks in the early restore. */ static const struct dev_pm_ops apple_spmi_pm_ops = { - .suspend_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), - .resume_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), - .freeze_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), - .thaw_noirq = pm_sleep_ptr(apple_spmi_resume_noirq), - .poweroff_noirq = pm_sleep_ptr(apple_spmi_suspend_noirq), - .restore_noirq = pm_sleep_ptr(apple_spmi_restore_noirq), + NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_spmi_suspend_noirq, apple_spmi_resume_noirq) .restore_early = pm_sleep_ptr(apple_spmi_restore_early), }; From ecea7d36cc6ddbcb364cea8324c798e19dec7710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:30 +0100 Subject: [PATCH 130/269] mfd: macsmc: write WKTP only to an SMC known to take it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep commit writes the WKTP key ahead of 'slpa', and the wake writes it again once it is a full one. That is what the T6041's SMC needs: without the second write it goes on treating the time awake as a dark wake, keeps its own fan policy against a manual request and asks for a sleep under thermal load. The writes sat in the commit path every SoC shares, so the T8140's sleep sequence, which was established without the key, gained two commands that have not been run there. Key the writes on match data, set for apple,t6041-smc. Every other SMC gets the sequence it had. The KUnit cases run the sleep order and the refused commit for both kinds of SMC. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 71 ++++++++++++++++++++++++++++---------- drivers/mfd/macsmc.c | 23 ++++++++++-- include/linux/mfd/macsmc.h | 2 ++ 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index cd43d92bd86f1..bcca52f35b2a7 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -224,12 +224,15 @@ static int macsmc_test_ap_power(struct apple_smc *smc, bool on) return ctx->ap_power_result; } -static void macsmc_sleep_order_test(struct kunit *test) +/* A sleep and its wake, for an SMC that takes WKTP and for one that does not. */ +static void macsmc_sleep_order(struct kunit *test, bool wktp) { struct macsmc_test_context *ctx = test->priv; + unsigned int n = 0; kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); ctx->result = 8; + ctx->smc.wktp = wktp; apple_smc_sleep_warn(&ctx->smc); KUNIT_EXPECT_EQ(test, apple_smc_sleep_commit(&ctx->smc), 0); @@ -239,19 +242,31 @@ static void macsmc_sleep_order_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); /* As on the platform, values included: WKTP's 2 comes before 'slpa', its 0 ends the wake. */ - KUNIT_ASSERT_EQ(test, ctx->seen_count, 9U); - KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); - KUNIT_EXPECT_EQ(test, ctx->seen[1], 2U); - KUNIT_EXPECT_EQ(test, ctx->seen[2], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_IDLE); - KUNIT_EXPECT_EQ(test, ctx->seen[4], (u32)MACSMC_TEST_AP_ON); - KUNIT_EXPECT_EQ(test, ctx->seen[5], SMC_KEY(MBSE)); - KUNIT_EXPECT_EQ(test, ctx->seen[6], SMC_KEY(MBSW)); - KUNIT_EXPECT_EQ(test, ctx->seen[7], SMC_KEY(waka)); - KUNIT_EXPECT_EQ(test, ctx->seen[8], 0U); + KUNIT_ASSERT_EQ(test, ctx->seen_count, wktp ? 9U : 7U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); + if (wktp) + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_ON); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSE)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSW)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(waka)); + if (wktp) + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); } +static void macsmc_sleep_order_test(struct kunit *test) +{ + macsmc_sleep_order(test, false); +} + +static void macsmc_sleep_order_wktp_test(struct kunit *test) +{ + macsmc_sleep_order(test, true); +} + static void macsmc_test_outbox_poll(struct apple_smc *smc, const char *after, unsigned int min_ms, unsigned int quiet_ms) { @@ -264,6 +279,7 @@ static void macsmc_sleep_warn_late_test(struct kunit *test) kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); kunit_activate_static_stub(test, apple_smc_outbox_poll, macsmc_test_outbox_poll); ctx->result = 8; + ctx->smc.wktp = true; /* A sleep that cuts the debug link: slpw comes at the noirq suspend, before slpa. */ ctx->smc.sleep_warn_late = true; @@ -277,29 +293,44 @@ static void macsmc_sleep_warn_late_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_IDLE); } -static void macsmc_sleep_refused_test(struct kunit *test) +static void macsmc_sleep_refused(struct kunit *test, bool wktp) { struct macsmc_test_context *ctx = test->priv; + unsigned int n = 0; kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); ctx->ap_power_result = -ETIME; ctx->result = 4; + ctx->smc.wktp = wktp; /* No answer: refuse, undo the notice and leave commands working. */ KUNIT_EXPECT_EQ(test, apple_smc_sleep_commit(&ctx->smc), -ETIME); KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); - KUNIT_EXPECT_EQ(test, ctx->seen[0], 2U); - KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[2], (u32)MACSMC_TEST_AP_IDLE); - KUNIT_EXPECT_EQ(test, ctx->seen[3], (u32)MACSMC_TEST_AP_ON); + KUNIT_ASSERT_EQ(test, ctx->seen_count, wktp ? 4U : 3U); + if (wktp) + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_ON); /* The key the commit wrote is taken back when the sleep ends, once. */ KUNIT_EXPECT_TRUE(test, ctx->smc.wake_silent); apple_smc_wake_full(&ctx->smc); apple_smc_wake_full(&ctx->smc); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 5U); - KUNIT_EXPECT_EQ(test, ctx->seen[4], 0U); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + KUNIT_ASSERT_EQ(test, ctx->seen_count, wktp ? 5U : 3U); + if (wktp) + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); +} + +static void macsmc_sleep_refused_test(struct kunit *test) +{ + macsmc_sleep_refused(test, false); +} + +static void macsmc_sleep_refused_wktp_test(struct kunit *test) +{ + macsmc_sleep_refused(test, true); } static void macsmc_notice_test(struct kunit *test) @@ -410,8 +441,10 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_sram_value_test), KUNIT_CASE(macsmc_key_info_test), KUNIT_CASE(macsmc_sleep_order_test), + KUNIT_CASE(macsmc_sleep_order_wktp_test), KUNIT_CASE(macsmc_sleep_warn_late_test), KUNIT_CASE(macsmc_sleep_refused_test), + KUNIT_CASE(macsmc_sleep_refused_wktp_test), KUNIT_CASE(macsmc_notice_test), KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 9775486247ca6..a304aa2f73a34 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -89,6 +90,19 @@ static const struct mfd_cell apple_smc_t6050_described_devs[] = { MFD_CELL_OF("macsmc-rtc", NULL, NULL, 0, 0, "apple,smc-rtc"), }; +/* What this driver has to know of one SoC's SMC that the others do not share. */ +struct apple_smc_hw { + /* + * The SMC takes the WKTP key at the sleep request and once a wake is a + * full one: see apple_smc_sleep_commit() and apple_smc_wake_full(). + */ + bool wktp; +}; + +static const struct apple_smc_hw apple_smc_t6041_hw = { + .wktp = true, +}; + static int apple_smc_add_described_devices(struct apple_smc *smc, const struct mfd_cell *cells, size_t count) { @@ -604,6 +618,7 @@ static void apple_smc_pm_unregister(void *unused) static int apple_smc_probe(struct platform_device *pdev) { + const struct apple_smc_hw *hw = device_get_match_data(&pdev->dev); struct device *dev = &pdev->dev; struct apple_smc *smc; __be32 count = 0; @@ -615,6 +630,7 @@ static int apple_smc_probe(struct platform_device *pdev) return -ENOMEM; smc->wake_reasons = of_device_is_compatible(dev->of_node, "apple,t8132-smc") ? apple_smc_t8132_wake_reasons : apple_smc_t8140_wake_reasons; + smc->wktp = hw && hw->wktp; /* First, so that a late probe still sees a resume start. */ if (IS_ENABLED(CONFIG_HIBERNATION)) { @@ -851,6 +867,7 @@ static int apple_smc_restore(struct device *dev) static const struct of_device_id apple_smc_of_match[] = { { .compatible = "apple,t8152-smc" }, { .compatible = "apple,t6050-smc" }, + { .compatible = "apple,t6041-smc", .data = &apple_smc_t6041_hw }, { .compatible = "apple,t8103-smc" }, { .compatible = "apple,smc" }, {}, @@ -953,7 +970,7 @@ static int apple_smc_notice_idle(struct apple_smc *smc) /* * WKTP, one byte, the value for power state 0. It goes first at the sleep request, * ahead of the other sleep keys ('slpa' among them) and the AP idle notice, as the platform - * does. 0 leaves it unwritten. + * does. 0 leaves it unwritten. Only for an SMC known to take it: T6041's. */ static unsigned int sleep_wktp = 2; module_param(sleep_wktp, uint, 0644); @@ -966,7 +983,7 @@ static int apple_smc_sleep_commit(struct apple_smc *smc) /* Whatever is written here, the SMC runs the wake after this silent until it is told. */ smc->wake_silent = true; - if (wktp) { + if (smc->wktp && wktp) { ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); if (ret) dev_err(smc->dev, "Failed to write WKTP = %u: %d\n", wktp, ret); @@ -1001,7 +1018,7 @@ static void apple_smc_wake_full(struct apple_smc *smc) if (!smc->wake_silent) return; smc->wake_silent = false; - if (wktp < 0) + if (!smc->wktp || wktp < 0) return; ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); if (ret) diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index deed3e0de243c..9983f1e099759 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -122,6 +122,8 @@ struct apple_smc { bool sleep_warn_late; /* A sleep was committed, and the wake after it has not been declared a full one. */ bool wake_silent; + /* This SoC's SMC is told of a sleep and of a full wake through WKTP as well. */ + bool wktp; /* In a noirq phase: replies are polled for. */ bool polled; /* MBSW at the last wake: what the SMC had woken the AP for. */ From ec3e925c92107d41d3a50800ffb3c77749e670ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:36 +0100 Subject: [PATCH 131/269] pinctrl: apple: write a pin back in steps only on T6041 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a suspend that cut a block's power a T6041 pin takes its word in steps: the configuration, the input enable in a write of its own, the lock last. Written whole, the pin reads 0 for good. The helper that does this replaced the single write for every block, the uncached T8140 blocks included: the single write is what has been run there, and the helper also leaves alone a pin that reads locked. Select the stepwise write through match data, set for apple,t6041-pinctrl. Every other block gets its word whole again. Signed-off-by: František Balázsy --- drivers/pinctrl/pinctrl-apple-gpio.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 75fa4aba27017..4379d123af0b3 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -56,6 +56,8 @@ struct apple_gpio_pinctrl { unsigned long *saved_valid; /* The regmap caches the block: it holds what this driver set, not the hardware. */ bool cached; + /* A pin is written back in steps: see apple_gpio_safe_reconfigure(). */ + bool staged; /* Pins this driver has written. Others belong to someone else. */ unsigned long *touched; /* @@ -420,6 +422,11 @@ struct apple_gpio_hw { * only within the valid GPIO registers minus gpio-reserved-ranges. */ bool no_regcache; + /* + * A pin written back after a sleep or a resume from disk takes its word in + * steps (apple_gpio_safe_reconfigure()). Elsewhere the word goes back whole. + */ + bool staged_restore; }; /* T8140 (A18 Pro) and T8132 (M4) both have faulting slots in this block. */ @@ -427,6 +434,11 @@ static const struct apple_gpio_hw apple_gpio_uncached_hw = { .no_regcache = true, }; +/* T6040 and T6041 (M4 Pro and Max) */ +static const struct apple_gpio_hw apple_gpio_t6041_hw = { + .staged_restore = true, +}; + static int apple_gpio_regmap_access(struct device *dev, unsigned int npins, struct regmap_config *config) { @@ -625,6 +637,7 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) * writes the hardware past it. */ pctl->cached = !(hw && hw->no_regcache); + pctl->staged = hw && hw->staged_restore; pctl->saved = devm_kcalloc(&pdev->dev, npins, sizeof(*pctl->saved), GFP_KERNEL); pctl->saved_valid = devm_bitmap_zalloc(&pdev->dev, npins, GFP_KERNEL); if (!pctl->saved || !pctl->saved_valid) @@ -696,6 +709,7 @@ static const struct of_device_id apple_gpio_pinctrl_of_match[] = { { .compatible = "apple,t8140-pinctrl", .data = &apple_gpio_uncached_hw }, { .compatible = "apple,t8132-pinctrl", .data = &apple_gpio_uncached_hw }, { .compatible = "apple,t6050-pinctrl", .data = &apple_gpio_uncached_hw }, + { .compatible = "apple,t6041-pinctrl", .data = &apple_gpio_t6041_hw }, { .compatible = "apple,t8103-pinctrl", }, { .compatible = "apple,pinctrl", }, { } @@ -707,12 +721,17 @@ MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match); * configuration first with the input buffer as it is (off after a power loss), then the * input enable in a write of its own, the lock last. A locked pin is left alone. Written * in one go, with the input enable among the rest, the pin reads 0 for good after the wake. + * That is established for the T6041 blocks; any other block gets its word whole, as before. */ static void apple_gpio_safe_reconfigure(struct apple_gpio_pinctrl *pctl, unsigned int pin, u32 old, u32 cfg) { u32 val = cfg & ~(REG_GPIOx_LOCK | REG_GPIOx_INPUT_ENABLE); + if (!pctl->staged) { + regmap_write(pctl->map, REG_GPIO(pin), cfg); + return; + } if (old & REG_GPIOx_LOCK) return; if (!(cfg & REG_GPIOx_INPUT_ENABLE)) { From 0105206543fe5dafc9eae441c2da6d7844ced69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:55 +0100 Subject: [PATCH 132/269] mfd: macsmc: make the WKTP values constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two module parameters chose what the sleep commit and the full wake write to WKTP while the values were being established. They are: 2 at the sleep request, 0 once the wake is a full one. Nothing sets the parameters to anything else, and a value written from user space into the sleep sequence is no interface to keep. Remove sleep_wktp and wake_wktp and write the two values. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index a304aa2f73a34..9f32ac8782bea 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -969,24 +969,23 @@ static int apple_smc_notice_idle(struct apple_smc *smc) /* * WKTP, one byte, the value for power state 0. It goes first at the sleep request, - * ahead of the other sleep keys ('slpa' among them) and the AP idle notice, as the platform - * does. 0 leaves it unwritten. Only for an SMC known to take it: T6041's. + * ahead of the other sleep keys ('slpa' among them) and the AP idle notice. Only for + * an SMC known to take it: T6041's. */ -static unsigned int sleep_wktp = 2; -module_param(sleep_wktp, uint, 0644); -MODULE_PARM_DESC(sleep_wktp, "WKTP value written before 'slpa' on a firmware-driven suspend (0: none)"); +#define SMC_WKTP_SLEEP 2 +/* And once a wake is a full one: see apple_smc_wake_full(). */ +#define SMC_WKTP_AWAKE 0 static int apple_smc_sleep_commit(struct apple_smc *smc) { - unsigned int wktp = READ_ONCE(sleep_wktp); int ret; /* Whatever is written here, the SMC runs the wake after this silent until it is told. */ smc->wake_silent = true; - if (smc->wktp && wktp) { - ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); + if (smc->wktp) { + ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), SMC_WKTP_SLEEP); if (ret) - dev_err(smc->dev, "Failed to write WKTP = %u: %d\n", wktp, ret); + dev_err(smc->dev, "Failed to write WKTP = %u: %d\n", SMC_WKTP_SLEEP, ret); } apple_smc_write_mbse(smc, SMC_KEY(slpa)); /* Device interrupts are off here. */ @@ -1004,25 +1003,19 @@ static int apple_smc_sleep_commit(struct apple_smc *smc) * Without the 0 the SMC treats all the time awake as a power nap, whether * the sleep wrote its 2 or not: it keeps its own fan policy against a manual fan request * (the mode request times out), and under thermal load it requests a sleep once a second. - * Negative writes nothing. */ -static int wake_wktp; -module_param(wake_wktp, int, 0644); -MODULE_PARM_DESC(wake_wktp, "WKTP value written once a wake is a full one (negative: none)"); - static void apple_smc_wake_full(struct apple_smc *smc) { - int wktp = READ_ONCE(wake_wktp); int ret; if (!smc->wake_silent) return; smc->wake_silent = false; - if (!smc->wktp || wktp < 0) + if (!smc->wktp) return; - ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), wktp); + ret = apple_smc_write_u8(smc, SMC_KEY(WKTP), SMC_WKTP_AWAKE); if (ret) - dev_err(smc->dev, "Failed to write WKTP = %d: %d\n", wktp, ret); + dev_err(smc->dev, "Failed to write WKTP = %u: %d\n", SMC_WKTP_AWAKE, ret); } static int apple_smc_wake_reason_names(const struct apple_smc *smc, u64 record, char *buf, From 34c02e1a399dc2dc5ff45694cc0226b2fd287f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:49 +0100 Subject: [PATCH 133/269] irqchip/apple-aic: mark the T6041 AIC as lost in a firmware suspend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6041's AIC is the AICv3 of the T8140 and loses the same state when a firmware-driven suspend cuts its power. It was admitted to the save and restore by a helper that looked for the T6040 sleep controller in the device tree and for its driver in the configuration, next to the fw_suspend flag the T8140 entry carries for the same purpose. Give apple,t6041-aic3 an entry of its own with fw_suspend set and drop the helper. The save runs only under a firmware-driven suspend, so a board without the sleep controller does nothing more than keep an unused array. A kernel built without the T6040 sleep driver gains something: the syscore, and with it the mask resync after a resume from disk, used to be registered only with that driver configured, so such a kernel came back from hibernation with the masks of the kernel that loaded the image. Signed-off-by: František Balázsy --- drivers/irqchip/irq-apple-aic.c | 47 ++++++++++++++------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index 0ef641dfa283b..0feac86155628 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -349,6 +349,18 @@ static const struct aic_info aic3_t8132_info __initconst = { .fw_suspend = true, }; +/* + * The same AICv3 as T8140's across a firmware-driven suspend: the CONFIG, + * TIMEOUT and CLUSTER_PRIO words and the IRQ_CFG array are lost with its power. + */ +static const struct aic_info aic3_t6041_info __initconst = { + .version = 3, + .irq_cfg = AIC3_IRQ_CFG, + .fast_ipi = true, + .local_fast_ipi = true, + .fw_suspend = true, +}; + static const struct aic_info aic3_t8142_info __initconst = { .version = 3, .irq_cfg = AIC3_IRQ_CFG, @@ -366,6 +378,10 @@ static const struct of_device_id aic_info_match[] = { .compatible = "apple,t8140-aic3", .data = &aic3_t8140_info, }, + { + .compatible = "apple,t6041-aic3", + .data = &aic3_t6041_info, + }, { .compatible = "apple,t8142-aic3", .data = &aic3_t8142_info, @@ -1287,32 +1303,9 @@ static struct syscore aic_syscore = { .ops = &aic_syscore_ops, }; -/* - * The T6040's AIC (apple,t6041-aic3, matched through aic3_info) is the same - * AICv3 as the T8140's: the same CONFIG, TIMEOUT and CLUSTER_PRIO words and - * IRQ_CFG array. It is admitted to the sleep save only on a board whose device - * tree enables the T6040 sleep controller, so that every other machine with - * this AIC suspends and hibernates as before. - */ -static bool __init aic_t6040_sleep(struct device_node *node) -{ - struct device_node *np; - bool sleeps; - - if (!IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) || - !of_device_is_compatible(node, "apple,t6041-aic3")) - return false; - - np = of_find_compatible_node(NULL, NULL, "apple,t6040-sleep"); - sleeps = of_device_is_available(np); - of_node_put(np); - - return sleeps; -} - -static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) +static void __init aic_sleep_init(struct aic_irq_chip *irqc) { - if (!irqc->info.fw_suspend && !aic_t6040_sleep(node)) + if (!irqc->info.fw_suspend) return; aic_sleep.irq_cfg = kcalloc(irqc->nr_die * irqc->nr_irq, sizeof(u32), GFP_KERNEL); @@ -1322,7 +1315,7 @@ static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node register_syscore(&aic_syscore); } #else -static void __init aic_sleep_init(struct aic_irq_chip *irqc, struct device_node *node) +static void __init aic_sleep_init(struct aic_irq_chip *irqc) { } #endif @@ -1480,7 +1473,7 @@ static int __init aic_of_ic_init(struct device_node *node, struct device_node *p "irqchip/apple-aic/ipi:starting", aic_init_cpu, NULL); - aic_sleep_init(irqc, node); + aic_sleep_init(irqc); if (is_kernel_in_hyp_mode()) { struct irq_fwspec mi = { From 03837bb493eb9d5d2172cb23a9a60b46107f47f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:50 +0100 Subject: [PATCH 134/269] irqchip/apple-aic: resync the masks only after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The masks are rewritten from the IRQ core's books on every syscore resume that did not follow a firmware-driven suspend, on the grounds that a resume from disk is the only other one that loses them. The resume that follows the snapshot of a hibernation, in the kernel that goes on to write the image, got the same rewrite with the values already there. Ask the PM core whether this kernel was restored from an image, and rewrite the masks only then. Signed-off-by: František Balázsy --- drivers/irqchip/irq-apple-aic.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index 0feac86155628..a04421a44dbe0 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -1228,8 +1228,7 @@ static void aic_resume_boot_cpu(void) * its own it never unmasks the restored kernel's other IRQs again. Make every * hardware mask match the restored descriptor: unmasked unless the core * recorded it masked (a lazily disabled IRQ was unmasked in hardware when the - * image was taken), and masked when this kernel has no mapping for it. On the - * thaw path the masks already match and this rewrites the same values. + * image was taken), and masked when this kernel has no mapping for it. */ static void aic_resync_masks(struct aic_irq_chip *ic) { @@ -1256,8 +1255,8 @@ static void aic_syscore_resume(void *data) u32 die, i, off = 0; if (!aic_sleep.saved) { - /* No firmware-driven suspend: the only other syscore resume that loses masks. */ - if (IS_ENABLED(CONFIG_HIBERNATION)) + /* No firmware-driven suspend: only a resume from disk has lost the masks. */ + if (pm_hibernate_is_restored()) aic_resync_masks(ic); return; } From 3f9a32b5198103202ac0d909f08aa92d51ee5d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:51 +0100 Subject: [PATCH 135/269] irqchip/apple-aic: resync the masks after a resume from disk on any SoC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A kernel restored from a hibernation image finds in the AIC the masks of the kernel that loaded the image: that one masked every IRQ at its init and unmasked what it used itself. This does not depend on the SoC or on the AIC's version, but the syscore that puts the masks back was registered only for the SoCs with a firmware-driven suspend. Register it whenever hibernation is built in. The save and restore around a firmware-driven suspend stay with the SoCs that have one: the array for it is allocated only there, and the suspend callback returns at once anywhere else. On a machine that never resumes from disk nothing changes: the resume callback writes only in a restored kernel. Signed-off-by: František Balázsy --- drivers/irqchip/irq-apple-aic.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index a04421a44dbe0..a94029884eddb 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -1181,7 +1181,8 @@ static int aic_syscore_suspend(void *data) struct aic_irq_chip *ic = aic_irqc; u32 die, i, off = 0; - if (!pm_suspend_via_firmware()) + /* Registered for the resume from disk alone where the SoC has no such suspend. */ + if (!ic->info.fw_suspend || !pm_suspend_via_firmware()) return 0; aic_sleep.config = aic_ic_read(ic, AIC2_CONFIG); @@ -1302,14 +1303,21 @@ static struct syscore aic_syscore = { .ops = &aic_syscore_ops, }; +/* + * The save across a firmware-driven suspend is for the SoCs that have one. The + * mask resync after a resume from disk is for every SoC: whatever the AIC, the + * kernel that loaded the image left its own masks in it. + */ static void __init aic_sleep_init(struct aic_irq_chip *irqc) { - if (!irqc->info.fw_suspend) + if (!irqc->info.fw_suspend && !IS_ENABLED(CONFIG_HIBERNATION)) return; - aic_sleep.irq_cfg = kcalloc(irqc->nr_die * irqc->nr_irq, sizeof(u32), GFP_KERNEL); - if (WARN_ON(!aic_sleep.irq_cfg)) - return; + if (irqc->info.fw_suspend) { + aic_sleep.irq_cfg = kcalloc(irqc->nr_die * irqc->nr_irq, sizeof(u32), GFP_KERNEL); + if (WARN_ON(!aic_sleep.irq_cfg)) + return; + } register_syscore(&aic_syscore); } From 66a2c2d2d6bde0d990fd55423e76d91df8cd68f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:54 +0100 Subject: [PATCH 136/269] mfd: macsmc: share the SMC handshake between probe and restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a resume from disk the driver repeats what probe does with a freshly woken SMC: start the endpoint, send the initialization, wait for it, check the boot stage and read the key count. The restore had its own copy of the five steps. Put them in one function. Probe now initializes the completions, the driver data and the notifier head before the handshake instead of in the middle of it, and the restore resets the completions before the endpoint starts instead of after; none of that reaches the SMC, which sees the same messages in the same order. The messages of a failed handshake are the same at probe and at restore now, each with its error code. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 118 +++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 66 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 9f32ac8782bea..53c9b88a4a284 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -616,12 +616,58 @@ static void apple_smc_pm_unregister(void *unused) unregister_pm_notifier(&apple_smc_pm_nb); } +/* + * The handshake with an SMC that was just woken, at probe and after a resume + * from disk alike: its endpoint, the initialization and the key count. The + * firmware is a new instance each time, and nothing says that its key table is + * the one of the instance before. + */ +static int apple_smc_handshake(struct apple_smc *smc) +{ + struct device *dev = smc->dev; + __be32 count = 0; + int ret; + + ret = apple_rtkit_start_ep(smc->rtk, SMC_ENDPOINT); + if (ret) { + dev_err(dev, "Failed to start SMC endpoint: %d\n", ret); + return ret; + } + + ret = apple_rtkit_send_message(smc->rtk, SMC_ENDPOINT, + FIELD_PREP(SMC_MSG, SMC_MSG_INITIALIZE), NULL, false); + if (ret) { + dev_err(dev, "Failed to send init message: %d\n", ret); + return ret; + } + + if (wait_for_completion_timeout(&smc->init_done, msecs_to_jiffies(SMC_TIMEOUT_MS)) == 0) { + dev_err(dev, "Timed out initializing SMC\n"); + return -ETIMEDOUT; + } + + if (smc->boot_stage != APPLE_SMC_INITIALIZED) { + dev_err(dev, "SMC failed to boot successfully, boot stage=%d\n", smc->boot_stage); + return -EIO; + } + + ret = apple_smc_read(smc, SMC_KEY(#KEY), &count, sizeof(count)); + if (ret >= 0 && ret != sizeof(count)) + ret = -EIO; + if (ret < 0) { + dev_err(dev, "Failed to get key count: %d\n", ret); + return ret; + } + smc->key_count = be32_to_cpu(count); + + return 0; +} + static int apple_smc_probe(struct platform_device *pdev) { const struct apple_smc_hw *hw = device_get_match_data(&pdev->dev); struct device *dev = &pdev->dev; struct apple_smc *smc; - __be32 count = 0; bool t6050; int ret; @@ -662,39 +708,14 @@ static int apple_smc_probe(struct platform_device *pdev) if (ret) return ret; - ret = apple_rtkit_start_ep(smc->rtk, SMC_ENDPOINT); - if (ret) - return dev_err_probe(dev, ret, "Failed to start SMC endpoint"); - init_completion(&smc->init_done); init_completion(&smc->cmd_done); - - ret = apple_rtkit_send_message(smc->rtk, SMC_ENDPOINT, - FIELD_PREP(SMC_MSG, SMC_MSG_INITIALIZE), NULL, false); - if (ret) - return dev_err_probe(dev, ret, "Failed to send init message"); - - if (wait_for_completion_timeout(&smc->init_done, msecs_to_jiffies(SMC_TIMEOUT_MS)) == 0) { - dev_err(dev, "Timed out initializing SMC"); - return -ETIMEDOUT; - } - - if (smc->boot_stage != APPLE_SMC_INITIALIZED) { - dev_err(dev, "SMC failed to boot successfully, boot stage=%d\n", smc->boot_stage); - return -EIO; - } - dev_set_drvdata(&pdev->dev, smc); BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers); - ret = apple_smc_read(smc, SMC_KEY(#KEY), &count, sizeof(count)); - if (ret >= 0 && ret != sizeof(count)) { - count = 0; - ret = -EIO; - } - if (ret < 0) - return dev_err_probe(smc->dev, ret, "Failed to get key count"); - smc->key_count = be32_to_cpu(count); + ret = apple_smc_handshake(smc); + if (ret) + return ret; /* Notifications, GPIO and power management are not qualified on T8152. */ if (of_device_is_compatible(dev->of_node, "apple,t8152-smc")) @@ -784,7 +805,6 @@ static int apple_smc_thaw(struct device *dev) static int apple_smc_restore(struct device *dev) { struct apple_smc *smc = dev_get_drvdata(dev); - __be32 count = 0; int ret; /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ @@ -813,46 +833,12 @@ static int apple_smc_restore(struct device *dev) return ret; } - ret = apple_rtkit_start_ep(smc->rtk, SMC_ENDPOINT); - if (ret) { - dev_err(dev, "Failed to start SMC endpoint on restore: %d\n", ret); - return ret; - } - reinit_completion(&smc->init_done); reinit_completion(&smc->cmd_done); - ret = apple_rtkit_send_message(smc->rtk, SMC_ENDPOINT, - FIELD_PREP(SMC_MSG, SMC_MSG_INITIALIZE), NULL, false); - if (ret) { - dev_err(dev, "Failed to send init message on restore: %d\n", ret); - return ret; - } - - if (wait_for_completion_timeout(&smc->init_done, msecs_to_jiffies(SMC_TIMEOUT_MS)) == 0) { - dev_err(dev, "Timed out initializing SMC on restore\n"); - return -ETIMEDOUT; - } - - if (smc->boot_stage != APPLE_SMC_INITIALIZED) { - dev_err(dev, "SMC failed to boot on restore, boot stage=%d\n", smc->boot_stage); - return -EIO; - } - - /* - * Re-read the key count: the firmware is a fresh instance and nothing - * guarantees the table is identical to the pre-hibernate one. - */ - ret = apple_smc_read(smc, SMC_KEY(#KEY), &count, sizeof(count)); - if (ret >= 0 && ret != sizeof(count)) { - count = 0; - ret = -EIO; - } - if (ret < 0) { - dev_err(dev, "Failed to get key count on restore: %d\n", ret); + ret = apple_smc_handshake(smc); + if (ret) return ret; - } - smc->key_count = be32_to_cpu(count); /* * Re-enable the firmware-side notification flag. The event_handlers From 6dfa5af6241d922096a210bf39ef57a37e04fa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:55 +0100 Subject: [PATCH 137/269] spmi: apple: share the interrupt reset between probe and restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Probe masks every interrupt and drops what is latched before it sets the controller up, and the restore after a hibernation starts with the same loop. Give the loop a function. No functional change: the same writes in the same order. Signed-off-by: František Balázsy --- drivers/spmi/spmi-apple-controller.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index bac65a59e5ea4..dee6e68380438 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -532,6 +532,15 @@ static void apple_spmi_irq_handler(struct irq_desc *desc) chained_irq_exit(chip, desc); } +/* Every interrupt masked, the notification included, and what is latched dropped. */ +static void apple_spmi_irq_reset(struct apple_spmi *spmi) +{ + for (size_t offset = 0; offset < (spmi->hw->num_irqs + 32) / 8; offset += 4) { + writel(0, spmi->regs + SPMI_IRQ_MASK_BASE + offset); + writel(U32_MAX, spmi->regs + SPMI_IRQ_ACK_BASE + offset); + } +} + static void apple_spmi_teardown_irq(void *data) { struct apple_spmi *spmi = data; @@ -556,10 +565,7 @@ static int apple_spmi_init_irq(struct platform_device *pdev, raw_spin_lock_init(&spmi->irq_mask_lock); - for (size_t offset = 0; offset < (spmi->hw->num_irqs + 32) / 8; offset += 4) { - writel(0, spmi->regs + SPMI_IRQ_MASK_BASE + offset); - writel(U32_MAX, spmi->regs + SPMI_IRQ_ACK_BASE + offset); - } + apple_spmi_irq_reset(spmi); spmi->irqd = devm_irq_domain_instantiate(&pdev->dev, &info); if (IS_ERR(spmi->irqd)) @@ -719,10 +725,7 @@ static void apple_spmi_hib_resync_irqs(struct apple_spmi *spmi) * pending complete a register selection of a device restored now * before its own answer arrives. The devices read their state afresh. */ - for (size_t offset = 0; offset < (nirq + 32) / 8; offset += 4) { - writel(0, spmi->regs + SPMI_IRQ_MASK_BASE + offset); - writel(U32_MAX, spmi->regs + SPMI_IRQ_ACK_BASE + offset); - } + apple_spmi_irq_reset(spmi); for (size_t offset = 0; offset < (nirq + 32) / 8; offset += 4) { u32 val = 0; From 58fe93662d64515f5d07b0e97da358f0b7b7f632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:49:07 +0100 Subject: [PATCH 138/269] mfd: macsmc: expect the lid read in the sleep order test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wake reads the lid's state for the display after it has told the SMC that the wake was taken, at every wake. The sleep order test still counted the commands of a wake without that read and has failed on its count since. Expect the read where it comes: after 'waka', before the key that ends the wake. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index bcca52f35b2a7..11318fb21768d 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -241,8 +241,11 @@ static void macsmc_sleep_order(struct kunit *test, bool wktp) KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); - /* As on the platform, values included: WKTP's 2 comes before 'slpa', its 0 ends the wake. */ - KUNIT_ASSERT_EQ(test, ctx->seen_count, wktp ? 9U : 7U); + /* + * Values included: WKTP's 2 comes before 'slpa', its 0 ends the wake. + * The lid is read at every wake, for the display. + */ + KUNIT_ASSERT_EQ(test, ctx->seen_count, wktp ? 10U : 8U); KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); if (wktp) KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); @@ -252,6 +255,7 @@ static void macsmc_sleep_order(struct kunit *test, bool wktp) KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSE)); KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSW)); KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(waka)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MSLD)); if (wktp) KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); From 316b80078fa9ec10443950c858c135eae8786f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:49:08 +0100 Subject: [PATCH 139/269] mfd: macsmc: leave the SMC's sleep commit out of a dry run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A T6040 dry run of level 2 took the sleep up to the commit point with the steps counted as "can still be taken back", the SMC's sleep commit among them: the WKTP key, MBSE = slpa and the AP idle notice. It then refused, and the resume sent the SMC what a wake sends. That does not bring the SMC back. On a J616s every sensor the SMC reports stood still after such a run, the CPU die temperature at one value under load, until the next suspend that really lost power had woken; then the reports were live at once. A suspend to idle did not change it either way. So the commit belongs with the stops that only a power cycle undoes, which a dry run leaves out. Send it only when the suspend goes on to the power loss. A dry run of either level then warns the SMC and takes the warning back, as level 1 always did. apple_sleep_last_steps() had this one user and goes. A real sleep sends what it sent, in the same place. What a real sleep can still meet after the commit, a refusal at the commit point or a wake event before it, is not changed by this. The KUnit case runs a sleep that ends short of the power loss. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 31 +++++++++++++++++++ drivers/mfd/macsmc.c | 55 ++++++++++++++++++++++++--------- drivers/soc/apple/sleep-t6040.c | 10 +++--- drivers/soc/apple/sleep.c | 8 ----- include/linux/soc/apple/sleep.h | 14 ++------- 5 files changed, 81 insertions(+), 37 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index 11318fb21768d..ad67df925b2f5 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -337,6 +337,36 @@ static void macsmc_sleep_refused_wktp_test(struct kunit *test) macsmc_sleep_refused(test, true); } +static bool macsmc_test_no_commit(void) +{ + return false; +} + +static void macsmc_sleep_dry_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + + kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); + kunit_activate_static_stub(test, apple_smc_sleep_commits, macsmc_test_no_commit); + ctx->result = 8; + ctx->smc.wktp = true; + + /* + * A sleep that ends short of the power loss, as a dry run does: the SMC is + * warned and told that the sleep is off, and gets nothing of the commit. + */ + apple_smc_sleep_warn(&ctx->smc); + KUNIT_EXPECT_EQ(test, apple_smc_sleep_noirq(&ctx->smc), 0); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + apple_smc_wake_noirq(&ctx->smc); + apple_smc_sleep_end(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); + KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(waka)); +} + static void macsmc_notice_test(struct kunit *test) { struct macsmc_test_context *ctx = test->priv; @@ -449,6 +479,7 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_sleep_warn_late_test), KUNIT_CASE(macsmc_sleep_refused_test), KUNIT_CASE(macsmc_sleep_refused_wktp_test), + KUNIT_CASE(macsmc_sleep_dry_test), KUNIT_CASE(macsmc_notice_test), KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 53c9b88a4a284..a6503bf2fea2c 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -1134,10 +1134,8 @@ static int apple_smc_prepare(struct device *dev) } /* Warned, and a refusal came before the notice: tell it the sleep is off. */ -static void apple_smc_complete(struct device *dev) +static void apple_smc_sleep_end(struct apple_smc *smc) { - struct apple_smc *smc = dev_get_drvdata(dev); - smc->sleep_warn_late = false; if (smc->sleep_warned) { apple_smc_write_mbse(smc, SMC_KEY(waka)); @@ -1147,6 +1145,11 @@ static void apple_smc_complete(struct device *dev) apple_smc_wake_full(smc); } +static void apple_smc_complete(struct device *dev) +{ + apple_smc_sleep_end(dev_get_drvdata(dev)); +} + /* * For at least @min_ms, then until the outbox is quiet for @quiet_ms. The events go to their * handlers as usual, through the RTKit worker. @@ -1190,13 +1193,28 @@ static int apple_smc_sleep_enter(struct apple_smc *smc) return apple_smc_sleep_commit(smc); } -static int apple_smc_suspend_noirq(struct device *dev) +/* + * Whether this suspend goes on to the SoC's power loss. Only then is the SMC given its sleep + * commit, which nothing sent to it takes back: a T6041 SMC that took the commit and then saw + * no power loss kept every sensor report at its last value until a later sleep had completed, + * although it got the notices of a wake as after any sleep. So the commit is among the stops + * that only a power cycle undoes, and a dry run, which ends short of the power loss by + * design, leaves it out. + */ +static bool apple_smc_sleep_commits(void) +{ + if (IS_ENABLED(CONFIG_MFD_MACSMC_KUNIT_TEST)) + KUNIT_STATIC_STUB_REDIRECT(apple_smc_sleep_commits); + + return apple_sleep_power_loss(); +} + +static int apple_smc_sleep_noirq(struct apple_smc *smc) { - struct apple_smc *smc = dev_get_drvdata(dev); int ret; - /* A dry run short of its last steps warns the SMC, and .complete takes that back. */ - if ((!smc->sleep_warned && !smc->sleep_warn_late) || !apple_sleep_last_steps()) + /* A sleep that stops short of the commit warns the SMC, and .complete takes that back. */ + if ((!smc->sleep_warned && !smc->sleep_warn_late) || !apple_smc_sleep_commits()) return 0; apple_smc_set_polled(smc, true); @@ -1206,15 +1224,24 @@ static int apple_smc_suspend_noirq(struct device *dev) return ret; } -static int apple_smc_resume_noirq(struct device *dev) +static int apple_smc_suspend_noirq(struct device *dev) { - struct apple_smc *smc = dev_get_drvdata(dev); + return apple_smc_sleep_noirq(dev_get_drvdata(dev)); +} - if (smc->ap_notified) { - apple_smc_set_polled(smc, true); - apple_smc_sleep_wake(smc); - apple_smc_set_polled(smc, false); - } +static void apple_smc_wake_noirq(struct apple_smc *smc) +{ + if (!smc->ap_notified) + return; + + apple_smc_set_polled(smc, true); + apple_smc_sleep_wake(smc); + apple_smc_set_polled(smc, false); +} + +static int apple_smc_resume_noirq(struct device *dev) +{ + apple_smc_wake_noirq(dev_get_drvdata(dev)); return 0; } diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 47194cfbde6fc..ca5681dc8fc0b 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -29,9 +29,11 @@ * false, so what only a power cycle restores is left out), the CPUs * park and answer the commit's hold, the commit point's checks run, * and the sleep is refused there and resumed warm. - * 2: 1, plus the last steps that can still be taken back (the SMC's sleep - * commit, each coprocessor's last check, such as ANS's check of the - * shutdown notification it sent at its suspend). + * 2: 1, plus the last steps that can still be taken back (each + * coprocessor's last check, such as ANS's check of the shutdown + * notification it sent at its suspend). The SMC's sleep commit is not + * one of them: the SMC leaves the state it puts it in only through a + * power loss of the SoC, so no dry run sends it. * The AON watchdog's WD2 is armed from .begin to .end of a dry run * (t6040_sleep.wd2_secs, 0 to leave it off). * - The real commit (t6040_sleep_commit) runs only when no blocker remains, @@ -488,7 +490,7 @@ static void __iomem *t6040_ioremap_np(struct device *dev, phys_addr_t pa, size_t return p; } -/* The generic driver answers apple_sleep_power_loss() and apple_sleep_last_steps() by it. */ +/* The generic driver answers apple_sleep_power_loss() by it. */ static void t6040_sleep_set_dry(struct t6040_sleep *s, unsigned int dry) { s->dry = dry; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index ba2f7651280ba..72609b4dab10d 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -776,14 +776,6 @@ bool apple_sleep_power_loss(void) } EXPORT_SYMBOL_GPL(apple_sleep_power_loss); -bool apple_sleep_last_steps(void) -{ - unsigned int dry = READ_ONCE(apple_sleep_dry); - - return pm_suspend_via_firmware() && (!dry || dry >= 2); -} -EXPORT_SYMBOL_GPL(apple_sleep_last_steps); - /* The registry's questions, answered for this SoC's sleep. */ static bool apple_sleep_reg_commit_stops(void) { diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 9c7580e9361eb..4daf4105808a8 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -332,26 +332,18 @@ static inline int apple_sleep_engine_reset(struct device *dev) * Whether this firmware-driven suspend may end in a real power loss of the SoC, so that a * driver may stop what only a power cycle brings back. A dry run, which the T6040 has, sets * pm_suspend_via_firmware() to run every sleep step that can still be taken back and then - * refuses before the commit: there this is false, and such a stop has to be left out. + * refuses before the commit: there this is false, and such a stop has to be left out. The + * SMC's sleep commit is one of them: an SMC that took it stays in its sleep state until the + * SoC has lost its power and come back. * Everywhere else it is pm_suspend_via_firmware(). - * - * apple_sleep_last_steps(): whether the last steps that can still be taken back, but only - * just (the SMC's sleep commit, each coprocessor's last check), run too. A real sleep and - * the deepest dry run take them. */ #if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) bool apple_sleep_power_loss(void); -bool apple_sleep_last_steps(void); #else static inline bool apple_sleep_power_loss(void) { return pm_suspend_via_firmware(); } - -static inline bool apple_sleep_last_steps(void) -{ - return pm_suspend_via_firmware(); -} #endif #endif From f487ada45a7cc70c0f0b932d7d32bc4c46fae615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:49:09 +0100 Subject: [PATCH 140/269] mfd: macsmc: say when a sleep is refused after the SMC's commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SMC gets its sleep commit in the noirq suspend, as the last of the coprocessors and ahead of the CPUs' parking. What can still refuse the sleep comes after that: a later device, a wake event, the checks at the commit point. The resume then sends the notices of a wake, but an SMC that took the commit and saw no power loss stays in its sleep state until a sleep has completed: on a J616s its sensor reports stand still for that long. Nothing known to the driver takes the commit back, and sending it later would change the order a sleep that works depends on. Until one of the two is settled, say so in the log when it happens, so that reports which no longer move have their cause next to them. The KUnit case runs the commit, the refusal and the end of the sleep, and expects what a wake sends, once. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 43 +++++++++++++++++++++++++++++++++++++++ drivers/mfd/macsmc.c | 9 ++++++++ 2 files changed, 52 insertions(+) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index ad67df925b2f5..afcec94ba4d81 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -367,6 +367,48 @@ static void macsmc_sleep_dry_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(waka)); } +static bool macsmc_test_commit(void) +{ + return true; +} + +static void macsmc_sleep_refused_late_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + unsigned int n = 0; + + kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); + kunit_activate_static_stub(test, apple_smc_outbox_poll, macsmc_test_outbox_poll); + kunit_activate_static_stub(test, apple_smc_sleep_commits, macsmc_test_commit); + ctx->result = 8; + ctx->smc.wktp = true; + + /* + * A real sleep that something refuses after the commit: no power loss, and + * the SMC gets what a wake sends, in a wake's order and once. + */ + apple_smc_sleep_warn(&ctx->smc); + KUNIT_EXPECT_EQ(test, apple_smc_sleep_noirq(&ctx->smc), 0); + KUNIT_EXPECT_TRUE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_TRUE(test, ctx->smc.wake_silent); + apple_smc_wake_noirq(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + apple_smc_sleep_end(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 10U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_ON); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSE)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSW)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(waka)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MSLD)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); +} + static void macsmc_notice_test(struct kunit *test) { struct macsmc_test_context *ctx = test->priv; @@ -480,6 +522,7 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_sleep_refused_test), KUNIT_CASE(macsmc_sleep_refused_wktp_test), KUNIT_CASE(macsmc_sleep_dry_test), + KUNIT_CASE(macsmc_sleep_refused_late_test), KUNIT_CASE(macsmc_notice_test), KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index a6503bf2fea2c..294caebad7a3d 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -1234,6 +1234,15 @@ static void apple_smc_wake_noirq(struct apple_smc *smc) if (!smc->ap_notified) return; + /* + * Committed, and no power loss followed: a check that comes after this device's + * suspend refused the sleep, or a wake event got in first. The SMC gets the notices + * of a wake all the same, which is all there is to send, but it leaves its sleep + * state only through a power loss: see apple_smc_sleep_commits(). + */ + if (!pm_resume_via_firmware()) + dev_warn(smc->dev, "sleep refused after the SMC's commit: its sensor reports may stand still until a sleep completes\n"); + apple_smc_set_polled(smc, true); apple_smc_sleep_wake(smc); apple_smc_set_polled(smc, false); From 1995d17b9bd53425c89e6957c5de6ea209e0571c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 02:45:16 +0100 Subject: [PATCH 141/269] mfd: macsmc: warn the SMC of a sleep only where it is committed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The driver wrote MBSE = slpw at .prepare and, when the suspend was called off before the commit, MBSE = waka at .complete. The SMC does not take the warning back. On a J616s, after a suspend that stopped before the commit (the SMC got slpw and then waka, about a second apart), the SMC no longer ran the fans: both stood at 0 rpm with the CPU die at 100 to 113 C under load, and a fan request was refused, until the reboot. Before that suspend, in the same boot, the SMC had started the fans by itself and had taken a request. The sensor reports stayed live, so this is not the standstill a refused commit leaves. A device that refuses and a wake event during the device suspend are ordinary ends of a suspend, and each left the machine without its fan policy. Send the warning in the noirq suspend, right before the commit and only when the suspend goes on to the power loss, which is the path a sleep that cuts the debug link already takes: slpw, 200 ms of polling, then the WKTP key, slpa and the AP idle notice. A suspend that ends before that sends the SMC nothing. What can refuse after the commit still leaves the SMC in its sleep state; the message for that case stays. The KUnit case for a suspend that stops short of the power loss now expects no command at all. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 13 ++++++------- drivers/mfd/macsmc.c | 28 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index afcec94ba4d81..1af284051d6fe 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -285,7 +285,7 @@ static void macsmc_sleep_warn_late_test(struct kunit *test) ctx->result = 8; ctx->smc.wktp = true; - /* A sleep that cuts the debug link: slpw comes at the noirq suspend, before slpa. */ + /* slpw comes at the noirq suspend, right before the commit. */ ctx->smc.sleep_warn_late = true; KUNIT_EXPECT_EQ(test, apple_smc_sleep_enter(&ctx->smc), 0); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); @@ -352,19 +352,18 @@ static void macsmc_sleep_dry_test(struct kunit *test) ctx->smc.wktp = true; /* - * A sleep that ends short of the power loss, as a dry run does: the SMC is - * warned and told that the sleep is off, and gets nothing of the commit. + * A sleep that ends short of the power loss, as a dry run does: the SMC + * gets neither the warning nor anything of the commit. */ - apple_smc_sleep_warn(&ctx->smc); + ctx->smc.sleep_warn_late = true; KUNIT_EXPECT_EQ(test, apple_smc_sleep_noirq(&ctx->smc), 0); KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); apple_smc_wake_noirq(&ctx->smc); apple_smc_sleep_end(&ctx->smc); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 2U); - KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); - KUNIT_EXPECT_EQ(test, ctx->seen[1], SMC_KEY(waka)); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); } static bool macsmc_test_commit(void) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 294caebad7a3d..bbc9457f45a59 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -863,8 +863,8 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match); /* * The SMC keeps running through a firmware-driven suspend. It is told first and * stops last, and on a wake it hears first. In order, measured on T8140 and - * unverified on other SoCs: MBSE = slpw before any other coprocessor is told; - * MBSE = slpa and the AP power notice after all the others; on a wake the notice, + * unverified on other SoCs: MBSE = slpw, MBSE = slpa and the AP power notice after + * all the other coprocessors (slpw goes ahead of them); on a wake the notice, * MBSE and MBSW read, MBSE = waka. The noirq phases give that order whatever the * device order is, so these commands poll. What the values mean is inferred * from their letters. @@ -875,15 +875,15 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match); * The debug link's cut, on a debug cable only, makes the SMC see a cable pulled and plugged * back. With the cut after slpw, the SMC woke the AP right after the commit, for USB-C_plug * or acdetach, in every such sleep (J700, 223 of 223), even with its outbox quiet for 500 ms - * before the notice. A sleep that cuts the link warns the SMC only after the cut. + * before the notice. So the warning comes after the cut. */ /* The gaps macOS leaves before the AP idle notice and after it. */ #define SMC_SLEEP_ARM_MS 6 #define SMC_SLEEP_IDLE_MS 30 /* - * Between a late warning (a sleep that cut the debug link) and slpa. macOS's shortest gap - * was 770 ms; the warning at .prepare leaves 150-500 ms, and those sleeps run to the alarm. + * Between the warning and slpa. Sleeps with 150-500 ms between them ran to the alarm; + * 770 ms is known to work. */ #define SMC_SLEEP_WARN_MS 200 @@ -1125,10 +1125,8 @@ static int apple_smc_prepare(struct device *dev) if (!pm_suspend_via_firmware()) return 0; - if (apple_sleep_debug_link_up()) - smc->sleep_warn_late = true; - else - apple_smc_sleep_warn(smc); + /* Warned with the commit, not here: see apple_smc_sleep_enter(). */ + smc->sleep_warn_late = true; return 0; } @@ -1179,7 +1177,15 @@ static void apple_smc_outbox_poll(struct apple_smc *smc, const char *after, last, ms >= SMC_OUTBOX_SETTLE_MS ? ", still busy" : ""); } -/* The noirq suspend's part, polled: the cut, a late warning, the commit's notices. */ +/* + * The noirq suspend's part, polled: the cut, the warning, the commit's notices. + * + * The warning is not sent earlier, although the SMC could take it then. A T6041 SMC that + * was warned and then told that the sleep was off, with no commit between, stopped running + * the fans (0 rpm at a die temperature of 110 C) and refused a fan request, until a later + * sleep had completed: nothing sent to it takes the warning back. So it goes where the + * commit goes, after every device has had its chance to refuse. + */ static int apple_smc_sleep_enter(struct apple_smc *smc) { if (apple_sleep_debug_link_cut()) @@ -1213,7 +1219,7 @@ static int apple_smc_sleep_noirq(struct apple_smc *smc) { int ret; - /* A sleep that stops short of the commit warns the SMC, and .complete takes that back. */ + /* A sleep that stops short of the commit sends the SMC nothing. */ if ((!smc->sleep_warned && !smc->sleep_warn_late) || !apple_smc_sleep_commits()) return 0; From 56cf9255a1d3106d82ed52e6a3de4a95d441645b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 03:41:00 +0100 Subject: [PATCH 142/269] mfd: macsmc: send the sleep commit from the sleep controller's last stop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A T6041 SMC takes neither MBSE = slpw nor the commit after it back, and only a completed sleep or a reboot brings it out again. The driver sends both in its noirq suspend. A sleep can still be refused after that: a wake event that the PM core finds pending after the noirq phase, a CPU that does not park, the sleep controller's checks at its commit, the enclave before it sends its sleep request. The SMC then has slpw, WKTP, slpa and the AP idle notice and no power loss, and its driver can only say so ("sleep refused after the SMC's commit"). The T6040 sleep controller stops coprocessors at its commit: with interrupts off, one CPU up and every refusal behind it, the enclave's sleep request being the last of them. Register the SMC there, among the stops that run first, and send the whole sequence from that stop: slpw, 200 ms in which the mailbox is polled, WKTP = 2, slpa, 6 ms, the AP idle notice and 30 ms, as the noirq suspend sends them. The noirq suspend then sends nothing, so a sleep refused anywhere before the last stops leaves the SMC as it was, and so does a dry run, which never reaches them. The wake is unchanged: it follows ap_notified, which the last stop sets. Against the noirq suspend the sequence now comes after the enclave's sleep request instead of before it, because that request can still be refused. It stays ahead of the other coprocessors' stops: those registered as first run in the order of the registry's table, where the SMC takes its place at probe and the only other one, the GPU, takes a free place for each sleep. That place is a later one unless a driver that registered before the SMC has been unbound since. Nothing can sleep at the last stop and the RTKit worker does not run, so the keys go through apple_smc_write_atomic() and the notice through apple_rtkit_ap_idle_polled(). Atomic mode was only ever entered for a shutdown; the last stop enters it and leaves it again, giving up an answer that is still missing, so that the wake's commands are the usual ones. apple_smc_write_atomic() timed its poll with ktime_get(), which warns while timekeeping is suspended, as it is here: it reads the fast monotonic clock instead. An event the SMC sends during the sequence is taken from its outbox and reaches its handler after the wake. A last stop cannot refuse, and the sleep goes on whatever it returns. If the SMC fails a step, the step is logged and the rest is sent all the same: an SMC that took the warning leaves its sleep state only through the rest of the sequence and the power loss. The wake then sends its part as after any sleep. With an SMC that answers nothing, each write waits out its 5 s timeout with interrupts off. Only the T6041 SMC registers. Every other SoC, a kernel that does not run the last stops (below EL2, or without the sleep controller) and a full registry keep the noirq suspend exactly as it is; T8140's order, with the debug link's cut before the warning, was measured there. The cut and its outbox poll stay in the noirq suspend for both paths: the cut sleeps, and it has to precede the warning. The T6040 controller has no cut, so that call does nothing there. The existing KUnit cases describe the noirq path and pass unchanged. New cases cover the last stop: nothing is sent in the noirq suspend and the whole sequence comes from the stop, once; a refusal before the stop sends nothing at the suspend and nothing at the resume; the wake's sequence follows a stop, also one in which every step failed. J616s, one boot: a dry run and a sleep refused before the enclave's request leave the SMC without a single message, with its sensor reports live and the fans under its control; four sleeps, one of them right after the refusal and one after a hibernation, go through the last stop (no message from the SMC in the 200 ms) and wake with the usual record. A wake event during the entry was not tried. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 145 +++++++++++++++++++++++++++++- drivers/mfd/macsmc.c | 178 ++++++++++++++++++++++++++++++++++--- include/linux/mfd/macsmc.h | 2 + 3 files changed, 312 insertions(+), 13 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index 1af284051d6fe..1a97d362bee7c 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -13,6 +13,7 @@ struct macsmc_test_context { u32 seen[12]; unsigned int seen_count; int ap_power_result; + int atomic_result; u64 record; }; @@ -285,7 +286,7 @@ static void macsmc_sleep_warn_late_test(struct kunit *test) ctx->result = 8; ctx->smc.wktp = true; - /* slpw comes at the noirq suspend, right before the commit. */ + /* Without last stops, slpw comes at the noirq suspend, right before the commit. */ ctx->smc.sleep_warn_late = true; KUNIT_EXPECT_EQ(test, apple_smc_sleep_enter(&ctx->smc), 0); KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); @@ -383,8 +384,9 @@ static void macsmc_sleep_refused_late_test(struct kunit *test) ctx->smc.wktp = true; /* - * A real sleep that something refuses after the commit: no power loss, and - * the SMC gets what a wake sends, in a wake's order and once. + * A real sleep, committed in the noirq suspend, that something refuses after the + * commit: no power loss, and the SMC gets what a wake sends, in a wake's order and + * once. */ apple_smc_sleep_warn(&ctx->smc); KUNIT_EXPECT_EQ(test, apple_smc_sleep_noirq(&ctx->smc), 0); @@ -408,6 +410,140 @@ static void macsmc_sleep_refused_late_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); } +/* The atomic writes of the sleep controller's last stop: no mutex, and only in atomic mode. */ +static int macsmc_test_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, + size_t size) +{ + struct macsmc_test_context *ctx = container_of(smc, struct macsmc_test_context, smc); + + KUNIT_EXPECT_TRUE(kunit_get_current_test(), smc->atomic_mode); + KUNIT_EXPECT_FALSE(kunit_get_current_test(), mutex_is_locked(&smc->mutex)); + if (ctx->seen_count < ARRAY_SIZE(ctx->seen)) + ctx->seen[ctx->seen_count++] = size == sizeof(u8) ? *(const u8 *)buf : + get_unaligned((const u32 *)buf); + return ctx->atomic_result; +} + +static bool macsmc_test_at_stop(struct apple_smc *smc) +{ + return true; +} + +/* A sleep on a controller that runs last stops, up to where the noirq suspend has passed. */ +static void macsmc_last_stop_begin(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + + kunit_activate_static_stub(test, apple_smc_ap_power, macsmc_test_ap_power); + kunit_activate_static_stub(test, apple_smc_outbox_poll, macsmc_test_outbox_poll); + kunit_activate_static_stub(test, apple_smc_sleep_commits, macsmc_test_commit); + kunit_activate_static_stub(test, apple_smc_commits_at_stop, macsmc_test_at_stop); + kunit_activate_static_stub(test, apple_smc_write_atomic, macsmc_test_write_atomic); + ctx->result = 8; + ctx->smc.wktp = true; + + /* The noirq suspend sends nothing and leaves the warning for the last stop. */ + ctx->smc.sleep_warn_late = true; + KUNIT_EXPECT_EQ(test, apple_smc_sleep_noirq(&ctx->smc), 0); + KUNIT_EXPECT_TRUE(test, ctx->smc.sleep_warn_late); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); +} + +/* The wake after a last stop: a wake's commands in a wake's order, with the mutex again. */ +static void macsmc_last_stop_wake(struct kunit *test, unsigned int n) +{ + struct macsmc_test_context *ctx = test->priv; + + KUNIT_EXPECT_TRUE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_TRUE(test, ctx->smc.wake_silent); + KUNIT_EXPECT_FALSE(test, ctx->smc.atomic_mode); + KUNIT_EXPECT_FALSE(test, ctx->smc.atomic_pending); + apple_smc_wake_noirq(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + apple_smc_sleep_end(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + KUNIT_ASSERT_EQ(test, ctx->seen_count, n + 6); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_ON); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSE)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MBSW)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(waka)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(MSLD)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 0U); +} + +static void macsmc_last_stop_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + unsigned int n = 0; + + macsmc_last_stop_begin(test); + + /* The whole sequence comes from the last stop, in the noirq suspend's order, once. */ + KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), 0); + KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), 0); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); + KUNIT_EXPECT_TRUE(test, ctx->smc.sleep_warned); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + + macsmc_last_stop_wake(test, n); +} + +static void macsmc_last_stop_refused_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + + macsmc_last_stop_begin(test); + + /* + * Refused after the noirq suspend and before the last stops, as by a wake event or + * the enclave: the SMC was sent nothing, and the resume sends it nothing. + */ + apple_smc_wake_noirq(&ctx->smc); + apple_smc_sleep_end(&ctx->smc); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warn_late); + KUNIT_EXPECT_FALSE(test, ctx->smc.sleep_warned); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_FALSE(test, ctx->smc.wake_silent); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); + + /* A last stop with nothing left for it sends nothing either. */ + KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), 0); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); +} + +static void macsmc_last_stop_failed_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + unsigned int n = 0; + + macsmc_last_stop_begin(test); + + /* + * An SMC that answers nothing: a last stop cannot refuse, so every step is still + * sent, the first failure is returned, and the wake sends its part. + */ + ctx->atomic_result = -ETIMEDOUT; + ctx->ap_power_result = -ETIME; + ctx->smc.atomic_pending = true; + KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), -ETIMEDOUT); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); + KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + + ctx->ap_power_result = 0; + macsmc_last_stop_wake(test, n); +} + static void macsmc_notice_test(struct kunit *test) { struct macsmc_test_context *ctx = test->priv; @@ -522,6 +658,9 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_sleep_refused_wktp_test), KUNIT_CASE(macsmc_sleep_dry_test), KUNIT_CASE(macsmc_sleep_refused_late_test), + KUNIT_CASE(macsmc_last_stop_test), + KUNIT_CASE(macsmc_last_stop_refused_test), + KUNIT_CASE(macsmc_last_stop_failed_test), KUNIT_CASE(macsmc_notice_test), KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index bbc9457f45a59..4d9de6f84117a 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -97,10 +98,17 @@ struct apple_smc_hw { * full one: see apple_smc_sleep_commit() and apple_smc_wake_full(). */ bool wktp; + /* + * This SoC's sleep controller stops its coprocessors at the commit, behind + * every refusal, and the SMC's sleep commit goes with them: see + * apple_smc_last_stop(). + */ + bool commit_at_stop; }; static const struct apple_smc_hw apple_smc_t6041_hw = { .wktp = true, + .commit_at_stop = true, }; static int apple_smc_add_described_devices(struct apple_smc *smc, const struct mfd_cell *cells, @@ -370,11 +378,13 @@ EXPORT_SYMBOL(apple_smc_enter_atomic); int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, size_t size) { + if (IS_ENABLED(CONFIG_MFD_MACSMC_KUNIT_TEST)) + KUNIT_STATIC_STUB_REDIRECT(apple_smc_write_atomic, smc, key, buf, size); + guard(spinlock_irqsave)(&smc->lock); u8 result; int ret, poll_ret; - u64 msg; - ktime_t t0; + u64 msg, t0; s64 us; if (size > SMC_MAX_SIZE || size == 0) @@ -418,12 +428,13 @@ int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, return ret; } - t0 = ktime_get(); + /* A sleep's last stop writes with timekeeping suspended, which ktime_get() warns of. */ + t0 = ktime_get_mono_fast_ns(); ret = read_poll_timeout_atomic(apple_rtkit_poll, poll_ret, poll_ret < 0 || !smc->atomic_pending, 100, SMC_ATOMIC_TIMEOUT_MS * USEC_PER_MSEC, false, smc->rtk); - us = ktime_us_delta(ktime_get(), t0); + us = div_u64(ktime_get_mono_fast_ns() - t0, NSEC_PER_USEC); if (us > 100 * USEC_PER_MSEC || ret < 0 || smc->atomic_pending) dev_warn(smc->dev, "atomic: key %p4ch reply after %lld us (ret %d)\n", &key, us, ret); @@ -663,6 +674,15 @@ static int apple_smc_handshake(struct apple_smc *smc) return 0; } +static int apple_smc_last_stop(void *data); + +static void apple_smc_last_stop_unregister(void *data) +{ + struct apple_smc *smc = data; + + apple_sleep_coproc_unregister(smc->dev); +} + static int apple_smc_probe(struct platform_device *pdev) { const struct apple_smc_hw *hw = device_get_match_data(&pdev->dev); @@ -728,6 +748,18 @@ static int apple_smc_probe(struct platform_device *pdev) if (ret) return ret; + /* + * Without the registration (no sleep controller with last stops for this kernel, + * or no room in its table) the noirq suspend commits, as on every other SoC. + */ + if (hw && hw->commit_at_stop && + !apple_sleep_coproc_register_first(dev, apple_smc_last_stop, smc)) { + smc->commit_at_stop = true; + ret = devm_add_action_or_reset(dev, apple_smc_last_stop_unregister, smc); + if (ret) + return ret; + } + /* Enable notifications */ apple_smc_write_flag(smc, SMC_KEY(NTAP), true); ret = devm_add_action_or_reset(dev, apple_smc_disable_notifications, smc); @@ -876,6 +908,13 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match); * back. With the cut after slpw, the SMC woke the AP right after the commit, for USB-C_plug * or acdetach, in every such sleep (J700, 223 of 223), even with its outbox quiet for 500 ms * before the notice. So the warning comes after the cut. + * + * A T6041 SMC takes neither the warning nor the commit back: see apple_smc_sleep_enter() and + * apple_smc_sleep_commits(). After the noirq suspend a sleep can still be refused: by a wake + * event, by a CPU that does not park, by the sleep controller's checks at its commit, by the + * enclave before its sleep request. So where the controller stops coprocessors at its commit, + * with interrupts off, one CPU up and every refusal behind it, the whole sequence is sent + * from there, ahead of the other coprocessors' stops: apple_smc_last_stop(). */ /* The gaps macOS leaves before the AP idle notice and after it. */ @@ -893,9 +932,15 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match); static int apple_smc_ap_power(struct apple_smc *smc, bool on) { + unsigned int us; + if (IS_ENABLED(CONFIG_MFD_MACSMC_KUNIT_TEST)) KUNIT_STATIC_STUB_REDIRECT(apple_smc_ap_power, smc, on); + /* With interrupts off there is only the idle notice: see apple_smc_last_stop(). */ + if (smc->atomic_mode) + return on ? -EINVAL : apple_rtkit_ap_idle_polled(smc->rtk, &us); + return smc->polled ? apple_rtkit_ap_power_noirq(smc->rtk, on) : apple_rtkit_ap_power(smc->rtk, on); } @@ -1134,6 +1179,8 @@ static int apple_smc_prepare(struct device *dev) /* Warned, and a refusal came before the notice: tell it the sleep is off. */ static void apple_smc_sleep_end(struct apple_smc *smc) { + if (smc->commit_at_stop) + apple_sleep_coproc_set_ready(smc->dev, false); smc->sleep_warn_late = false; if (smc->sleep_warned) { apple_smc_write_mbse(smc, SMC_KEY(waka)); @@ -1150,7 +1197,8 @@ static void apple_smc_complete(struct device *dev) /* * For at least @min_ms, then until the outbox is quiet for @quiet_ms. The events go to their - * handlers as usual, through the RTKit worker. + * handlers as usual, through the RTKit worker. With interrupts off, in atomic mode, that + * worker runs them once the sleep is over. */ static void apple_smc_outbox_poll(struct apple_smc *smc, const char *after, unsigned int min_ms, unsigned int quiet_ms) @@ -1170,7 +1218,10 @@ static void apple_smc_outbox_poll(struct apple_smc *smc, const char *after, } else { quiet++; } - usleep_range(1000, 1200); + if (smc->atomic_mode) + mdelay(1); + else + usleep_range(1000, 1200); } /* A debug cable only. */ dev_info(smc->dev, "%s: %u messages in %u ms, the last at %u ms%s\n", after, count, ms, @@ -1178,7 +1229,100 @@ static void apple_smc_outbox_poll(struct apple_smc *smc, const char *after, } /* - * The noirq suspend's part, polled: the cut, the warning, the commit's notices. + * Whether the sleep controller's last stops send this sleep's commit: the SMC is registered + * for one, and the controller runs them for this sleep. + */ +static bool apple_smc_commits_at_stop(struct apple_smc *smc) +{ + if (IS_ENABLED(CONFIG_MFD_MACSMC_KUNIT_TEST)) + KUNIT_STATIC_STUB_REDIRECT(apple_smc_commits_at_stop, smc); + + return smc->commit_at_stop && apple_sleep_commit_stops_coprocs(); +} + +static int apple_smc_stop_write(struct apple_smc *smc, smc_key key, const void *buf, size_t size) +{ + int ret = apple_smc_write_atomic(smc, key, buf, size); + + if (ret < 0) + dev_err(smc->dev, "Failed to write %p4ch at the last stop: %d\n", &key, ret); + + return ret < 0 ? ret : 0; +} + +/* + * The warning and the commit as one of the sleep controller's last stops: interrupts are + * off, one CPU is up, the enclave has its sleep request and nothing can refuse any more. So + * an SMC that got any of this also gets the power loss that brings it out of its sleep + * state. The order and the gaps are the noirq suspend's: apple_smc_sleep_enter() and + * apple_smc_sleep_commit(). + * + * Nothing sleeps here and the RTKit worker does not run, so the writes are the atomic ones + * and the idle notice's answer is polled for. Every other user of the SMC is suspended, and + * the mutex is not taken. An event the SMC sends meanwhile is taken from its outbox and + * reaches its handler after the wake. + * + * A last stop cannot refuse, and the sleep goes on whatever the SMC answers. So does this: + * a step that fails is logged and the rest is sent all the same, since an SMC that took + * the warning leaves its sleep state only through the rest of the sequence and the power + * loss. Each write waits up to SMC_ATOMIC_TIMEOUT_MS for its answer and as long again + * for an answer still missing from the write before. The wake sends its part, as after + * any sleep, once this has started. Returns the first failure. + */ +static int apple_smc_last_stop(void *data) +{ + const u32 slpw = SMC_KEY(slpw), slpa = SMC_KEY(slpa); + const u8 wktp = SMC_WKTP_SLEEP; + struct apple_smc *smc = data; + int ret, err; + + /* Not left for here: the noirq suspend sent it, or this suspend is not a sleep. */ + if (!smc->sleep_warn_late) + return 0; + smc->sleep_warn_late = false; + + scoped_guard(spinlock_irqsave, &smc->lock) + smc->atomic_mode = true; + + err = apple_smc_stop_write(smc, SMC_KEY(MBSE), &slpw, sizeof(slpw)); + smc->sleep_warned = true; + apple_smc_outbox_poll(smc, "sleep warning at the last stop", SMC_SLEEP_WARN_MS, 0); + + /* As in apple_smc_sleep_commit(). */ + smc->wake_silent = true; + if (smc->wktp) { + ret = apple_smc_stop_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); + err = err ?: ret; + } + ret = apple_smc_stop_write(smc, SMC_KEY(MBSE), &slpa, sizeof(slpa)); + err = err ?: ret; + mdelay(SMC_SLEEP_ARM_MS); + + /* Not taken back if the notice fails: the sleep goes on, and its wake sends the other. */ + smc->ap_notified = true; + ret = apple_smc_ap_power(smc, false); + if (ret) + dev_err(smc->dev, "SMC did not take the sleep notice at the last stop: %d\n", ret); + else + mdelay(SMC_SLEEP_IDLE_MS); + err = err ?: ret; + + /* + * The wake's commands are the usual ones. An answer that never came is given up: if + * it comes after all, it fails the sequence check of the command it lands on. + */ + scoped_guard(spinlock_irqsave, &smc->lock) { + smc->atomic_pending = false; + smc->atomic_mode = false; + } + + return err; +} + +/* + * The noirq suspend's part, polled: the cut, the warning, the commit's notices. Where the + * sleep controller's last stops send the warning and the commit, only the cut is left here: + * it sleeps, and it has to come before the warning. * * The warning is not sent earlier, although the SMC could take it then. A T6041 SMC that * was warned and then told that the sleep was off, with no commit between, stopped running @@ -1190,6 +1334,8 @@ static int apple_smc_sleep_enter(struct apple_smc *smc) { if (apple_sleep_debug_link_cut()) apple_smc_outbox_poll(smc, "debug link cut", 0, SMC_OUTBOX_QUIET_MS); + if (apple_smc_commits_at_stop(smc)) + return 0; if (smc->sleep_warn_late) { smc->sleep_warn_late = false; apple_smc_sleep_warn(smc); @@ -1219,8 +1365,16 @@ static int apple_smc_sleep_noirq(struct apple_smc *smc) { int ret; + if (!smc->sleep_warned && !smc->sleep_warn_late) + return 0; + /* + * Nothing of the SMC's is in the way of the sleep controller's stops, in a dry run's + * checks as in a sleep. Its last stop runs only where a sleep is committed. + */ + if (smc->commit_at_stop) + apple_sleep_coproc_set_ready(smc->dev, true); /* A sleep that stops short of the commit sends the SMC nothing. */ - if ((!smc->sleep_warned && !smc->sleep_warn_late) || !apple_smc_sleep_commits()) + if (!apple_smc_sleep_commits()) return 0; apple_smc_set_polled(smc, true); @@ -1244,7 +1398,8 @@ static void apple_smc_wake_noirq(struct apple_smc *smc) * Committed, and no power loss followed: a check that comes after this device's * suspend refused the sleep, or a wake event got in first. The SMC gets the notices * of a wake all the same, which is all there is to send, but it leaves its sleep - * state only through a power loss: see apple_smc_sleep_commits(). + * state only through a power loss: see apple_smc_sleep_commits(). A commit sent from + * the sleep controller's last stop has every refusal behind it. */ if (!pm_resume_via_firmware()) dev_warn(smc->dev, "sleep refused after the SMC's commit: its sensor reports may stand still until a sleep completes\n"); @@ -1261,7 +1416,10 @@ static int apple_smc_resume_noirq(struct device *dev) return 0; } -/* Suspend-to-RAM commits in the noirq phase; hibernation restarts the session in the normal one. */ +/* + * Suspend-to-RAM commits in the noirq phase or from the sleep controller's last stop; + * hibernation restarts the session in the normal one. + */ static const struct dev_pm_ops apple_smc_pm_ops = { .prepare = pm_sleep_ptr(apple_smc_prepare), .complete = pm_sleep_ptr(apple_smc_complete), diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 9983f1e099759..b404d39429ec4 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -126,6 +126,8 @@ struct apple_smc { bool wktp; /* In a noirq phase: replies are polled for. */ bool polled; + /* Registered with the sleep controller, whose last stops can send the sleep commit. */ + bool commit_at_stop; /* MBSW at the last wake: what the SMC had woken the AP for. */ u64 wake_record; /* MBSW's bits as this SoC numbers them, from the probe. */ From 70e4cd36bfbc8cad0c02b614fef70e8da6ea9f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 13:33:51 +0100 Subject: [PATCH 143/269] dmaengine: apple-sio: stop the T6041 firmware at the sleep's commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6041 driver idled the SIO firmware in the late phase of a suspend to RAM. An idled firmware does not come back with its power kept, so a sleep refused after that phase left it stopped until the next sleep that went through: on a J616s a refusal of the enclave's sleep request logged "sleep refused: firmware not back after a reset (-19), stopped until a real sleep", and the DMA service and the AES engine behind it were gone. Register the stop for the sleep's last step, as the T8140 does: the AP and IOP power idle with interrupts off, after the last refusal. A sleep refused before that finds the firmware running. A dry run of the T6040 sleep stops no coprocessor, and counted a registered one that was not ready as a check that a real commit would fail. Report the firmware ready in a dry run as well. On a J616s: after a sleep refused at the enclave's request and after a dry run the engine still encrypts and decrypts 16 MiB in each of CBC, CTR and ECB equal to the software implementation, the dry run reports no failed check, and suspends to RAM and to idle after them complete with the firmware back. Signed-off-by: František Balázsy --- drivers/dma/apple-sio.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index bf57fe0f3ef34..a27b50591a610 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -645,6 +645,7 @@ static const struct sio_hw_data sio_hw_t6030 = { * the 5 of the host's mailbox call timeout is not part of the record. */ static const struct sio_hw_data sio_hw_t6041 = { + .stop_at_commit = true, .protocol = 10, .fifo_threshold = 0x600, .cfg_word4 = 0x800, @@ -2150,10 +2151,15 @@ static bool sio_chan_busy(struct sio_chan *siochan) static __maybe_unused int sio_suspend_late(struct device *dev) { struct sio_data *sio = dev_get_drvdata(dev); + bool dry; int i, ret; - /* A dry run keeps the power, and an idled firmware doesn't wake with it kept. */ - if (!apple_sleep_power_loss() || sio->asleep) + /* + * A dry run keeps the power, and an idled firmware doesn't wake with it kept. One whose + * stop is the commit's is ready there as for a real sleep: a dry run stops nothing. + */ + dry = !apple_sleep_power_loss() && sio->commit_registered && pm_suspend_via_firmware(); + if ((!apple_sleep_power_loss() && !dry) || sio->asleep) return 0; for (i = 0; i < sio->nchannels; i++) { @@ -2163,7 +2169,7 @@ static __maybe_unused int sio_suspend_late(struct device *dev) } } - if (sio->commit_registered && apple_sleep_commit_stops_coprocs()) { + if (sio->commit_registered && (dry || apple_sleep_commit_stops_coprocs())) { apple_sleep_coproc_set_ready(dev, true); sio->commit_used = true; sio->asleep = true; From 3f8f167f0fd6fdf3a1acf156bbbd0bb0c2ab73ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:35:35 +0100 Subject: [PATCH 144/269] mfd: macsmc: leave out the rest of the last stop for an SMC that does not answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep controller's last stop sends the SMC four commands with interrupts off: the sleep warning, WKTP, the sleep arm and the AP idle notice. A command waits up to 5 s for its answer, and the next one waits as long again for an answer still missing from the one before. An SMC that does not answer at all therefore held the CPU for up to 15 s with interrupts off before the sleep went on. Stop after the first command that gets no answer (timeout, or the answer to the one before still missing) and return the failure. The sleep goes on as before, and no idle notice is owed to the wake. An SMC that does answer gets the whole sequence unchanged. The KUnit case for an SMC that answers nothing now expects the warning only; a second case covers the answer that is still missing. Signed-off-by: František Balázsy --- drivers/mfd/macsmc-test.c | 33 ++++++++++++++++++++++----------- drivers/mfd/macsmc.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index 1a97d362bee7c..29920e5c482c8 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -522,26 +522,36 @@ static void macsmc_last_stop_refused_test(struct kunit *test) static void macsmc_last_stop_failed_test(struct kunit *test) { struct macsmc_test_context *ctx = test->priv; - unsigned int n = 0; macsmc_last_stop_begin(test); /* - * An SMC that answers nothing: a last stop cannot refuse, so every step is still - * sent, the first failure is returned, and the wake sends its part. + * An SMC that answers nothing: a last stop cannot refuse, but each further step would + * wait out its timeout again with interrupts off. Only the warning is tried, nothing else + * is sent, no idle notice is owed to the wake, and the first failure is returned. */ ctx->atomic_result = -ETIMEDOUT; - ctx->ap_power_result = -ETIME; ctx->smc.atomic_pending = true; KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), -ETIMEDOUT); - KUNIT_ASSERT_EQ(test, ctx->seen_count, 4U); - KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpw)); - KUNIT_EXPECT_EQ(test, ctx->seen[n++], 2U); - KUNIT_EXPECT_EQ(test, ctx->seen[n++], SMC_KEY(slpa)); - KUNIT_EXPECT_EQ(test, ctx->seen[n++], (u32)MACSMC_TEST_AP_IDLE); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 1U); + KUNIT_EXPECT_EQ(test, ctx->seen[0], SMC_KEY(slpw)); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); + KUNIT_EXPECT_FALSE(test, ctx->smc.atomic_mode); + KUNIT_EXPECT_FALSE(test, ctx->smc.atomic_pending); + KUNIT_EXPECT_TRUE(test, ctx->smc.sleep_warned); +} - ctx->ap_power_result = 0; - macsmc_last_stop_wake(test, n); +/* The same when the answer to an earlier command is still missing (-EBUSY). */ +static void macsmc_last_stop_late_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + + macsmc_last_stop_begin(test); + + ctx->atomic_result = -EBUSY; + KUNIT_EXPECT_EQ(test, apple_smc_last_stop(&ctx->smc), -EBUSY); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 1U); + KUNIT_EXPECT_FALSE(test, ctx->smc.ap_notified); } static void macsmc_notice_test(struct kunit *test) @@ -661,6 +671,7 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_last_stop_test), KUNIT_CASE(macsmc_last_stop_refused_test), KUNIT_CASE(macsmc_last_stop_failed_test), + KUNIT_CASE(macsmc_last_stop_late_test), KUNIT_CASE(macsmc_notice_test), KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 4d9de6f84117a..fccd81ea517de 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -753,7 +753,8 @@ static int apple_smc_probe(struct platform_device *pdev) * or no room in its table) the noirq suspend commits, as on every other SoC. */ if (hw && hw->commit_at_stop && - !apple_sleep_coproc_register_first(dev, apple_smc_last_stop, smc)) { + !apple_sleep_coproc_register_first(dev, APPLE_SLEEP_FIRST_SMC, apple_smc_last_stop, + smc)) { smc->commit_at_stop = true; ret = devm_add_action_or_reset(dev, apple_smc_last_stop_unregister, smc); if (ret) @@ -1265,10 +1266,17 @@ static int apple_smc_stop_write(struct apple_smc *smc, smc_key key, const void * * A last stop cannot refuse, and the sleep goes on whatever the SMC answers. So does this: * a step that fails is logged and the rest is sent all the same, since an SMC that took * the warning leaves its sleep state only through the rest of the sequence and the power - * loss. Each write waits up to SMC_ATOMIC_TIMEOUT_MS for its answer and as long again - * for an answer still missing from the write before. The wake sends its part, as after - * any sleep, once this has started. Returns the first failure. + * loss. The one exception is an SMC that does not answer at all: a write waits up to + * SMC_ATOMIC_TIMEOUT_MS for its answer, so after the first timeout the rest is left out + * instead of waiting that long again for each step. The wake sends its part, as after any + * sleep, once this has started. Returns the first failure. */ +static bool apple_smc_stop_dead(int ret) +{ + /* No answer within SMC_ATOMIC_TIMEOUT_MS, or the one before is still missing. */ + return ret == -ETIMEDOUT || ret == -EBUSY; +} + static int apple_smc_last_stop(void *data) { const u32 slpw = SMC_KEY(slpw), slpa = SMC_KEY(slpa); @@ -1286,6 +1294,8 @@ static int apple_smc_last_stop(void *data) err = apple_smc_stop_write(smc, SMC_KEY(MBSE), &slpw, sizeof(slpw)); smc->sleep_warned = true; + if (apple_smc_stop_dead(err)) + goto dead; apple_smc_outbox_poll(smc, "sleep warning at the last stop", SMC_SLEEP_WARN_MS, 0); /* As in apple_smc_sleep_commit(). */ @@ -1293,9 +1303,13 @@ static int apple_smc_last_stop(void *data) if (smc->wktp) { ret = apple_smc_stop_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); err = err ?: ret; + if (apple_smc_stop_dead(ret)) + goto dead; } ret = apple_smc_stop_write(smc, SMC_KEY(MBSE), &slpa, sizeof(slpa)); err = err ?: ret; + if (apple_smc_stop_dead(ret)) + goto dead; mdelay(SMC_SLEEP_ARM_MS); /* Not taken back if the notice fails: the sleep goes on, and its wake sends the other. */ @@ -1306,7 +1320,17 @@ static int apple_smc_last_stop(void *data) else mdelay(SMC_SLEEP_IDLE_MS); err = err ?: ret; + goto out; +dead: + /* + * An SMC that answers nothing for SMC_ATOMIC_TIMEOUT_MS takes nothing more: each further + * write would wait that long again, and again for the answer still missing, all with + * interrupts off. The sleep goes on without the rest. + */ + dev_err(smc->dev, "SMC does not answer at the last stop, the rest is left out\n"); + err = err ?: -ETIMEDOUT; +out: /* * The wake's commands are the usual ones. An answer that never came is given up: if * it comes after all, it fails the sequence check of the command it lands on. From 51eafe8ecbbbf231ad90d8e095c45cc7228d0d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:35:35 +0100 Subject: [PATCH 145/269] soc: apple: sleep: order the first stops explicitly, drop an unused export MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The coprocessors registered first (the SMC and the GPU) ran their last stops in the order of their slots in the registry table, which follows the order of their probes. The SMC's warning and commit have to come before the GPU's stop, so name the order: apple_sleep_coproc_register_first() takes an APPLE_SLEEP_FIRST_* value and the first pass runs the stops by it. The order on the J616s is the one the table gave so far. The G16 GPU of the T8132 registers in the GPU's place. apple_sleep_debug_link_up() has no caller since the debug link is cut and read back inside the sleep controller; remove it with its stub. Signed-off-by: František Balázsy --- drivers/gpu/drm/asahi/g17/sleep.c | 4 ++-- drivers/soc/apple/sleep-internal.h | 1 + drivers/soc/apple/sleep-registry.c | 8 +++++--- drivers/soc/apple/sleep.c | 32 ++++++++++++++---------------- include/linux/soc/apple/sleep.h | 19 +++++++++--------- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/sleep.c b/drivers/gpu/drm/asahi/g17/sleep.c index 2aebc75d2188f..e3e0d9c3c654f 100644 --- a/drivers/gpu/drm/asahi/g17/sleep.c +++ b/drivers/gpu/drm/asahi/g17/sleep.c @@ -322,7 +322,7 @@ struct asahi_g17_commit *asahi_g17_commit_prepare(struct device *dev, if (c->request) apple_pmp_report_entry_commit_owned(c->request, true); - ret = apple_sleep_coproc_register_first(dev, asahi_g17_commit_stop, c); + ret = apple_sleep_coproc_register_first(dev, APPLE_SLEEP_FIRST_GPU, asahi_g17_commit_stop, c); if (ret) goto err; apple_sleep_coproc_set_ready(dev, true); @@ -369,7 +369,7 @@ struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct app if (!c->cpu[0] || !c->ps) goto err; - ret = apple_sleep_coproc_register_first(dev, asahi_g17_commit_stop, c); + ret = apple_sleep_coproc_register_first(dev, APPLE_SLEEP_FIRST_GPU, asahi_g17_commit_stop, c); if (ret) goto err; apple_sleep_coproc_set_ready(dev, true); diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index ed1787a49efff..39a9b8c17cd2e 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -38,6 +38,7 @@ struct apple_sleep_coproc { int stop_ret; /* No ASC: its last stop takes it whole, power word @ps included, before the others. */ bool first; + unsigned int order; /* among those registered first, lowest runs first */ u32 ps; }; diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c index 2a6ae9da5cc9b..0cdcf7a2f5ee8 100644 --- a/drivers/soc/apple/sleep-registry.c +++ b/drivers/soc/apple/sleep-registry.c @@ -80,6 +80,7 @@ int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie c->ready = false; c->stopped = false; c->first = false; + c->order = 0; c->ps = 0; c->dev = dev; @@ -87,15 +88,15 @@ int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie } EXPORT_SYMBOL_GPL(apple_sleep_coproc_register); -int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), - void *data) +int apple_sleep_coproc_register_first(struct device *dev, unsigned int order, + int (*last_stop)(void *data), void *data) { struct apple_sleep_coproc *c; struct device_node *np; u32 ps = 0; int ret; - if (!last_stop) + if (!last_stop || order >= APPLE_SLEEP_FIRST_ORDERS) return -EINVAL; np = of_parse_phandle(dev->of_node, "power-domains", 0); if (np) { @@ -110,6 +111,7 @@ int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void c = apple_sleep_coproc_find(dev); if (c) { c->first = true; + c->order = order; c->ps = ps; } diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 72609b4dab10d..7cd0cb0def532 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -930,20 +930,26 @@ int apple_sleep_enclave_stop(struct device *dev) */ void apple_sleep_coprocs_stop(bool first, bool *pcie_ans) { + unsigned int order; int i; if (first) *pcie_ans = false; - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + /* Those registered first run by their order, the others in one pass over the table. */ + for (order = 0; order < (first ? APPLE_SLEEP_FIRST_ORDERS : 1); order++) { + for (i = 0; i < SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - if (!c->dev || !c->ready || c->first != first) - continue; - *pcie_ans |= c->pcie_ans; - if (c->stopped) - continue; - c->stop_ret = c->last_stop ? c->last_stop(c->data) : 0; - apple_sleep_coproc_run_off(c); + if (!c->dev || !c->ready || c->first != first) + continue; + if (first && c->order != order) + continue; + *pcie_ans |= c->pcie_ans; + if (c->stopped) + continue; + c->stop_ret = c->last_stop ? c->last_stop(c->data) : 0; + apple_sleep_coproc_run_off(c); + } } dsb(sy); } @@ -1872,14 +1878,6 @@ bool apple_sleep_debug_link_cut(void) } EXPORT_SYMBOL_GPL(apple_sleep_debug_link_cut); -bool apple_sleep_debug_link_up(void) -{ - struct apple_sleep *s = apple_sleep; - - return s && !s->usb_down_ran && sleep_debug_link_up(s); -} -EXPORT_SYMBOL_GPL(apple_sleep_debug_link_up); - /* * macOS takes the wake for a lid closing during the sleep as a dark one: every driver wakes, * the display stays off, and the root domain sleeps again as soon as it sees the lid closed. diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 4daf4105808a8..48ff16702b52e 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -20,6 +20,11 @@ struct device_node; * (drivers/soc/apple/sleep-registry.c). The controller bound on this SoC * answers apple_sleep_commit_stops_coprocs() and runs the last stops. */ +/* Order of the coprocessors registered first (apple_sleep_coproc_register_first()). */ +#define APPLE_SLEEP_FIRST_SMC 0 /* its warning and commit come before the GPU's stop */ +#define APPLE_SLEEP_FIRST_GPU 1 +#define APPLE_SLEEP_FIRST_ORDERS 2 + #if IS_ENABLED(CONFIG_APPLE_SLEEP_REGISTRY) /* * A coprocessor that can't be stopped until a sleep is certain, and can't be @@ -36,9 +41,11 @@ int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie /* * The same with no ASC to clear: @last_stop clears its own run bits and lowers its own power * domain, and runs before every other coprocessor's. The refusal checks leave that domain on. + * Those registered first run in the order of @order, an APPLE_SLEEP_FIRST_* value, not of + * their place in the table. */ -int apple_sleep_coproc_register_first(struct device *dev, int (*last_stop)(void *data), - void *data); +int apple_sleep_coproc_register_first(struct device *dev, unsigned int order, + int (*last_stop)(void *data), void *data); void apple_sleep_coproc_unregister(struct device *dev); /* @@ -77,7 +84,7 @@ static inline int apple_sleep_coproc_register(struct device *dev, void __iomem * return -EOPNOTSUPP; } -static inline int apple_sleep_coproc_register_first(struct device *dev, +static inline int apple_sleep_coproc_register_first(struct device *dev, unsigned int order, int (*last_stop)(void *data), void *data) { return -EOPNOTSUPP; @@ -172,7 +179,6 @@ bool apple_sleep_debug_link_cut_peek(struct device *dev); bool apple_sleep_debug_link_cut(void); /* This sleep will end the debug link: for the SMC's driver, which warns the SMC after it. */ -bool apple_sleep_debug_link_up(void); /* * From the SMC's driver, in its noirq resume: the SMC woke the AP for the lid alone, and the @@ -279,11 +285,6 @@ static inline bool apple_sleep_debug_link_cut(void) return false; } -static inline bool apple_sleep_debug_link_up(void) -{ - return false; -} - static inline void apple_sleep_lid_closed_wake(void) { } From ae28dfc856dddd84b4c447793dbaefeef935490a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:38:24 +0100 Subject: [PATCH 146/269] soc: apple: sep: probe again through the aura reprobe helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a resume from disk on a cold boot the SEP is unbound and probed again once the tasks are thawed, to register with the restarted enclave. The driver carried its own work item, spinlock, pending-device pointer and a module-exit guard to cancel the work. aura_hibernate_reprobe_after_thaw() does the same unbind and probe from the freezable workqueue, which the restore finds frozen, so the probe cannot start before every device's restore has completed. The helper is built in, so a module going away in the meantime leaves no work behind and needs no cancel. The registration marker is still removed before the probe is queued, and only once a cold boot is established, as before. A probe that leaves the device without a driver is not reported by device_reprobe(); the helper now logs that case too, as the SEP's own work item did. Without CONFIG_APPLE_AURA_HIBERNATE the probe is not queued and the driver says so, as the AOP does. Signed-off-by: František Balázsy --- drivers/soc/apple/pm_shim.c | 64 +++++++++---------------------------- drivers/soc/apple/shim.h | 1 - 2 files changed, 15 insertions(+), 50 deletions(-) diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 1536fe67b00bf..a0ef50450d6c1 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -24,13 +24,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include @@ -71,41 +71,17 @@ static int sep_pm_cold_base = -ENODEV; /* * A resume from disk on a cold boot: the enclave runs a freshly booted sepOS * that has never seen this kernel's registration, so the restored session is - * dead. The device is probed again from a work item, as at boot: probe sends - * a new registration (the marker is gone with the session it guarded), opens - * the xART store and loads the host state from disk again (the boot that - * resumed this image may have changed both), and recreates /dev/sep-bio. + * dead. The device is probed again once the tasks are thawed, as at boot: + * probe sends a new registration (the marker is gone with the session it + * guarded), opens the xART store and loads the host state from disk again (the + * boot that resumed this image may have changed both), and recreates + * /dev/sep-bio. */ -static struct device *sep_reprobe_dev; -static DEFINE_SPINLOCK(sep_reprobe_lock); - -static void sep_reprobe_fn(struct work_struct *work) -{ - struct device *dev; - int ret; - - spin_lock(&sep_reprobe_lock); - dev = sep_reprobe_dev; - sep_reprobe_dev = NULL; - spin_unlock(&sep_reprobe_lock); - if (!dev) - return; - - dev_info(dev, "resume from disk: attaching the restarted enclave again\n"); - ret = device_reprobe(dev); - /* A failed probe is not reported by device_reprobe() itself. */ - if (ret || !READ_ONCE(dev->driver)) - dev_err(dev, "resume from disk: probing the SEP again failed (%d); Touch ID stays down until a reboot\n", - ret); - put_device(dev); -} - -static DECLARE_WORK(sep_reprobe_work, sep_reprobe_fn); - static void sep_pm_schedule_reprobe(struct device *dev) { struct device_node *np = dev->of_node; struct property *prop; + int ret; /* * Only here, with the cold boot established: removing the marker on a @@ -117,26 +93,16 @@ static void sep_pm_schedule_reprobe(struct device *dev) return; } - spin_lock(&sep_reprobe_lock); - if (!sep_reprobe_dev) { - sep_reprobe_dev = get_device(dev); - queue_work(system_dfl_wq, &sep_reprobe_work); + if (!IS_ENABLED(CONFIG_APPLE_AURA_HIBERNATE)) { + dev_err(dev, "resume from disk: no aura hibernation support to probe the SEP again; Touch ID stays down until a reboot\n"); + return; } - spin_unlock(&sep_reprobe_lock); -} - -/* The module goes away: no probe may run from the work item after it. */ -void sep_pm_exit(void) -{ - struct device *dev; - cancel_work_sync(&sep_reprobe_work); - spin_lock(&sep_reprobe_lock); - dev = sep_reprobe_dev; - sep_reprobe_dev = NULL; - spin_unlock(&sep_reprobe_lock); - if (dev) - put_device(dev); + dev_info(dev, "resume from disk: attaching the restarted enclave again\n"); + ret = aura_hibernate_reprobe_after_thaw(dev); + if (ret) + dev_err(dev, "resume from disk: probing the SEP again could not be queued (%d); Touch ID stays down until a reboot\n", + ret); } static int sep_pm_notify(struct notifier_block *nb, unsigned long action, diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 279bb46956603..f32945e01f5df 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -36,7 +36,6 @@ void sep_cancel_delayed_work_sync(void *work); int sep_pm_register(void *dev, int (*event)(int event), int (*sleep)(void), void (*wake)(void), bool (*told)(void)); void sep_pm_unregister(void); -void sep_pm_exit(void); int sep_pm_keep_domains(void *dev); int sep_pm_wait_gated(void *dev, unsigned int timeout_ms); int sep_pm_clear_dev_disable(void *dev); From db76911f66f14f9c5f17f5f32fbc135207c24b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:53:09 +0100 Subject: [PATCH 147/269] soc: apple: sleep-t6040: decide the refusal before the setup writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A suspend to RAM that is refused for a live blocker returned only after .begin had saved the AOP island power words, written the SMC rail request, set the DCS blocks to dynamic power gating and raised their power words to automatic mode. None of that is undone when the suspend is refused, so a refused "mem" changed the hardware it was refusing to suspend. Take the refusal decision first: with a non-display blocker left and no dry run, log the blockers and return -EOPNOTSUPP before any of those writes. A suspend that proceeds and a dry run make the same writes in the same order as before, and the commit point is untouched. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 49 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index ca5681dc8fc0b..bec2f3dbc3e8f 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -2353,6 +2353,19 @@ static int t6040_sleep_begin(suspend_state_t state) s->returned = false; s->gp_saved[0] = false; s->gp_saved[1] = false; + /* + * Decide before anything is written: a refused suspend leaves the AOP islands, the SMC + * rail request and the DCS power words as they were. Only a dry run or a proceeding + * suspend, which both go through device suspend and .enter, needs them set up. + */ + if (begin_total && !dry) { + dev_info(s->dev, "suspend requested: %u live blocker(s) (%u display, cleared during device suspend)\n", + live, disp); + t6040_sleep_log_blockers(s); + dev_info(s->dev, "refusing the suspend; nothing entered\n"); + return -EOPNOTSUPP; + } + t6040_aop_ps_save(s); t6040_sleep_pmvc(s); t6040_dcs_dyn_pg(s); @@ -2378,26 +2391,22 @@ static int t6040_sleep_begin(suspend_state_t state) live, disp); t6040_sleep_log_blockers(s); - if (dry) { - dry = min(dry, 2U); - t6040_sleep_power_ops(s); - if (!s->power_ops) - dev_warn(s->dev, "dry run: no CPU parking power ops, so no hold at the commit point\n"); - t6040_sleep_set_dry(s, dry); - pm_set_suspend_via_firmware(); - secs = min(READ_ONCE(wd2_secs), WDT_MAX_SECS); - if (secs && s->wdt) - t6040_sleep_set_wd2(s, secs); - else - secs = 0; - dev_info(s->dev, "dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", - dry, dry >= 2 ? "the stops only a power cycle undoes" : - "the last steps", secs); - return 0; - } - - dev_info(s->dev, "refusing the suspend; nothing entered\n"); - return -EOPNOTSUPP; + /* A dry run: the refusal case returned above. */ + dry = min(dry, 2U); + t6040_sleep_power_ops(s); + if (!s->power_ops) + dev_warn(s->dev, "dry run: no CPU parking power ops, so no hold at the commit point\n"); + t6040_sleep_set_dry(s, dry); + pm_set_suspend_via_firmware(); + secs = min(READ_ONCE(wd2_secs), WDT_MAX_SECS); + if (secs && s->wdt) + t6040_sleep_set_wd2(s, secs); + else + secs = 0; + dev_info(s->dev, "dry run %u: firmware-driven device sleep short of %s, refused at the commit point, WD2 %u s (0: off)\n", + dry, dry >= 2 ? "the stops only a power cycle undoes" : + "the last steps", secs); + return 0; } /* After the device suspend, in process context. */ From 9e875102f0b63fa6e032846a96a80b23ec633c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:26 +0100 Subject: [PATCH 148/269] soc: apple: sleep-t6040: require the register blocks at probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sleep node of every T6040 board (J614s, J616s, J773s, through t6040.dtsi) lists all eleven register blocks, and a suspend cannot be entered or undone without any of them: the commit would not arm the deep wait for interrupt and the wake could not restore the SoC. The driver nevertheless took each block as optional, guarded every access with a NULL test, and only refused the suspend at the commit point for six of them. Map the blocks first in probe and fail the probe when one is missing, so a node without them never offers a suspend, and drop the NULL tests and the now unreachable refusal. The accesses, their order and the commit point are unchanged. The binding already requires all eleven. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 202 ++++++++++++-------------------- 1 file changed, 74 insertions(+), 128 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index bec2f3dbc3e8f..99e59172754ff 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -764,7 +764,7 @@ static int t6040_cpu_on(u64 mpidr) unsigned int cluster = t6040_cpu_cluster(mpidr); u32 bit = t6040_cpu_start_bit(mpidr); - if (!s || !s->cpu_start) + if (!s) return -ENODEV; if (!bit) return -EINVAL; @@ -788,7 +788,7 @@ static const struct spin_table_power_ops t6040_cpu_power_ops = { */ static void t6040_sleep_power_ops(struct t6040_sleep *s) { - if (s->power_ops || !s->cpu_start) + if (s->power_ops) return; if (!spin_table_set_power_ops(&t6040_cpu_power_ops)) { s->power_ops = true; @@ -798,15 +798,13 @@ static void t6040_sleep_power_ops(struct t6040_sleep *s) static void t6040_sleep_set_wd2(struct t6040_sleep *s, unsigned int secs) { - if (!s->wdt) - return; apple_sleep_wd2_arm(s->wdt, secs); s->wd2_armed = true; } static void t6040_sleep_clear_wd2(struct t6040_sleep *s) { - if (!s->wdt || !s->wd2_armed) + if (!s->wd2_armed) return; writel(0, s->wdt + WDT_WD2_CTL); s->wd2_armed = false; @@ -863,19 +861,6 @@ static unsigned int t6040_sleep_refuse(struct t6040_sleep *s) refused++; } - /* - * Refuse rather than half-sleep if any register block the commit or the - * wake must write is missing from the device tree. The deep WFI would - * never arm, and the wake could not restore the SoC. - */ - if (!s->cluster_ctl || !s->pwrgate || !s->mastermini || !s->spmi0 || - !s->soc_east || !s->aop_global) { - dev_err(s->dev, "refused: a commit/wake register base is not mapped (cluster-ctl %d pwrgate %d mastermini %d spmi0 %d soc-east %d aop-global %d)\n", - !!s->cluster_ctl, !!s->pwrgate, !!s->mastermini, !!s->spmi0, - !!s->soc_east, !!s->aop_global); - refused++; - } - if (!sep->bound) { /* Nobody told it of the sleep: a commit with it running never returns. */ t6040_sleep_watch_on(sep, &val); @@ -1055,12 +1040,10 @@ static void t6040_sleep_log_s2r_state(struct t6040_sleep *s, const char *when) u32 m[AOP_WAKE_WORDS] = {}, st[AOP_WAKE_WORDS] = {}, ctl = 0; int i; - if (s->aop_global) { - ctl = readl(s->aop_global + AOP_S2R_CTRL); - for (i = 0; i < AOP_WAKE_WORDS; i++) { - m[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); - st[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); - } + ctl = readl(s->aop_global + AOP_S2R_CTRL); + for (i = 0; i < AOP_WAKE_WORDS; i++) { + m[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); + st[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); } dev_dbg(s->dev, "S2R state at %s: S2R-lo count %u (raw %#x), AOP-global ctl %#x\n", when, raw >> 3, raw, ctl); @@ -1100,11 +1083,9 @@ static const struct t6040_axi2af_domain { [AXI2AF_APCIE_SYS_GP] = { "apcie_sys_gp", true, 0x390, 0x416000000 }, }; -/* Only with the PMU's bus mapped. */ static void t6040_pmu_write(struct t6040_sleep *s, u16 reg, u8 val) { - if (s->spmi0) - apple_sleep_pmu_write(s->spmi0, PMU_USID, reg, val); + apple_sleep_pmu_write(s->spmi0, PMU_USID, reg, val); } /* @@ -1127,8 +1108,6 @@ static void t6040_clusters_off(struct t6040_sleep *s) u32 val; int i; - if (!s->cluster_ctl) - return; for (i = 0; i < CLUSTER_CTL_NR; i++) { val = readl(s->cluster_ctl + CLUSTER_CTL(i)); s->cluster_saved[i] = val; @@ -1155,12 +1134,8 @@ static void t6040_clkgen_park(struct t6040_sleep *s) u32 cur; int i; - if (s->soc_east) { - t6040_east_clk_quiesce(s, SOC_EAST_CLK_A); - t6040_east_clk_quiesce(s, SOC_EAST_CLK_B); - } - if (!s->clkgen) - return; + t6040_east_clk_quiesce(s, SOC_EAST_CLK_A); + t6040_east_clk_quiesce(s, SOC_EAST_CLK_B); for (i = 0; i < CLKGEN_MUX_WORDS; i++) { void __iomem *reg = s->clkgen + t6040_clkgen_mux[i]; u32 val = readl(reg); @@ -1178,7 +1153,7 @@ static void t6040_clkgen_restore(struct t6040_sleep *s) { int i; - if (!s->clkgen || !s->clkgen_saved) + if (!s->clkgen_saved) return; for (i = CLKGEN_MUX_WORDS - 1; i >= 0; i--) writel(s->clkgen_mux_saved[i], s->clkgen + t6040_clkgen_mux[i]); @@ -1195,8 +1170,6 @@ static void t6040_secondaries_off(struct t6040_sleep *s) unsigned int cpu; u32 v; - if (!s->soc_ps) - return; for_each_present_cpu(cpu) { u32 bit = t6040_cpu_start_bit(cpu_logical_map(cpu)); @@ -1309,6 +1282,9 @@ static int t6040_sleep_preflight_show(struct seq_file *m, void *unused) { struct t6040_sleep *s = m->private; unsigned int live = t6040_sleep_live_blockers(s, m, NULL); + static const char * const nm[] = { + "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT", + }; unsigned int i; apple_sleep_registry_show(m); @@ -1317,15 +1293,9 @@ static int t6040_sleep_preflight_show(struct seq_file *m, void *unused) seq_printf(m, "dry_run %u, wd2_secs %u, allow_entry %d, parking ops %s\n", READ_ONCE(dry_run), READ_ONCE(wd2_secs), allow_entry, s->power_ops ? "registered" : "not registered"); - if (s->cluster_ctl) { - static const char * const nm[] = { - "EACC", "PACC0", "PACC1_ANE", "DISP_INT", "DISP_EXT", - }; - - for (i = 0; i < CLUSTER_CTL_NR; i++) - seq_printf(m, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], - readl(s->cluster_ctl + CLUSTER_CTL(i))); - } + for (i = 0; i < CLUSTER_CTL_NR; i++) + seq_printf(m, "cluster-ctl +%#x %-9s %#x\n", CLUSTER_CTL(i), nm[i], + readl(s->cluster_ctl + CLUSTER_CTL(i))); return 0; } @@ -1393,8 +1363,6 @@ static void t6040_wake_arm(struct t6040_sleep *s) { int i; - if (!s->aop_global) - return; for (i = 0; i < AOP_WAKE_WORDS; i++) { s->wake_mask_saved[i] = readl(s->aop_global + AOP_WAKE_MASK(i)); writel(t6040_wake_mask[i], s->aop_global + AOP_WAKE_MASK(i)); @@ -1407,7 +1375,7 @@ static void t6040_wake_restore(struct t6040_sleep *s) { int i; - if (!s->aop_global || !s->wake_mask_saved_valid) + if (!s->wake_mask_saved_valid) return; for (i = 0; i < AOP_WAKE_WORDS; i++) writel(s->wake_mask_saved[i], s->aop_global + AOP_WAKE_MASK(i)); @@ -1674,7 +1642,7 @@ static void t6040_ans_bridge_quiesce(struct t6040_sleep *s) void __iomem *bridge = s->axi2af_bridge[AXI2AF_ANS]; u32 a, b; - if (!bridge || !s->soc_east || + if (!bridge || FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_ANS)) != PS_ACTIVE) return; a = readl(bridge + AXI2AF_BRIDGE_104); @@ -1906,8 +1874,6 @@ static void t6040_atc_lower(struct t6040_sleep *s) { int p, k; - if (!s->soc_ps) - return; for (p = 0; p < ARRAY_SIZE(t6040_atc_words); p++) for (k = 0; k < ARRAY_SIZE(t6040_atc_words[0]); k++) { void __iomem *reg = s->soc_ps + t6040_atc_words[p][k]; @@ -1971,7 +1937,7 @@ static void t6040_gp_ports_stop(struct t6040_sleep *s) { int p; - if (!s->soc_east || !FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP))) + if (!FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP))) return; for (p = 0; p < ARRAY_SIZE(s->gp_port); p++) t6040_gp_port_stop(s, p); @@ -1988,8 +1954,6 @@ static void t6040_apcie_gp_lower(struct t6040_sleep *s) void __iomem *sys, *gp, *bridge = s->axi2af_bridge[AXI2AF_APCIE_SYS_GP]; u32 val; - if (!s->soc_east) - return; sys = s->soc_east + PS_APCIE_SYS_GP; gp = s->soc_east + PS_APCIE_GP; if (!FIELD_GET(PS_ACTUAL, readl(sys))) @@ -2041,17 +2005,15 @@ static int t6040_sleep_commit(unsigned long unused) /* * ANS's storage link, once ANS has been stopped by its driver (see * t6040_apcie_phy_off). It follows every refusal, so a refused sleep finds - * the link up. No-op unless the apple,apcie windows are present (apcie_st.valid). + * the link up. */ - if (s->soc_east) - t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1); + t6040_ps_raise_start(s->soc_east, PS_APCIE_SYS_ST1); if (s->pcie_ans_stopped) t6040_apcie_st_lower(s); t6040_apcie_gp_lower(s); /* WD0 off, then the AON WD2 net (5 s covers the reconfig). */ - if (s->wdt) - writel(0, s->wdt + WDT_WD0_CTL); + writel(0, s->wdt + WDT_WD0_CTL); t6040_sleep_set_wd2(s, clamp(READ_ONCE(sleep_wd_secs), 1U, WDT_MAX_SECS)); /* And the SMC's AP watchdog deadline off, right after the WD2 (measured). */ if (s->smc_wdt_deadline) @@ -2059,10 +2021,8 @@ static int t6040_sleep_commit(unsigned long unused) /* PMU boot stage 0x50 and NOTIFY_ENABLE on both SPMI controllers (measured). */ t6040_pmu_write(s, PMU_BOOT_STAGE, PMU_BOOT_STAGE_SLEEP); - if (s->spmi0) - writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi0 + SPMI_NOTIFY_ENABLE); - if (s->spmi_a0) - writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi_a0 + SPMI_NOTIFY_ENABLE); + writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi0 + SPMI_NOTIFY_ENABLE); + writel(SPMI_NOTIFY_ENABLE_SLEEP, s->spmi_a0 + SPMI_NOTIFY_ENABLE); /* * The order of the PMGR quiesce (the cluster and axi2af @@ -2092,8 +2052,7 @@ static int t6040_sleep_commit(unsigned long unused) t6040_sio_ps_save(s); /* The commit: the MasterMini S2R timer arms the SoC sleep engine. */ - if (s->mastermini) - writel(1, s->mastermini + MASTERMINI_S2R_TIMER); + writel(1, s->mastermini + MASTERMINI_S2R_TIMER); dsb(sy); /* A raised line would end the WFI the engine waits for. */ @@ -2190,35 +2149,27 @@ static void t6040_sleep_return(struct t6040_sleep *s) * normal per-CPU start afterwards, and all 14 CPUs of the J616s are * online after a wake without it. */ - if (s->cluster_ctl) - for (i = 0; i < CLUSTER_CTL_NR; i++) { - u32 on = i < CLUSTER_CTL_FIRST_WAIT ? CLUSTER_CTL_ON : - s->cluster_saved[i] & CLUSTER_CTL_NIBBLE; + for (i = 0; i < CLUSTER_CTL_NR; i++) { + u32 on = i < CLUSTER_CTL_FIRST_WAIT ? CLUSTER_CTL_ON : + s->cluster_saved[i] & CLUSTER_CTL_NIBBLE; - val = readl(s->cluster_ctl + CLUSTER_CTL(i)); - writel((val & ~CLUSTER_CTL_NIBBLE) | on, s->cluster_ctl + CLUSTER_CTL(i)); - } + val = readl(s->cluster_ctl + CLUSTER_CTL(i)); + writel((val & ~CLUSTER_CTL_NIBBLE) | on, s->cluster_ctl + CLUSTER_CTL(i)); + } t6040_wake_sync(); /* Wake pwrgates and CPM voters: read, set BIT31. */ - if (s->pwrgate) { - writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, - s->pwrgate + PWRGATE_WAKE_0); - writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, - s->pwrgate + PWRGATE_WAKE_1); - } + writel(readl(s->pwrgate + PWRGATE_WAKE_0) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_0); + writel(readl(s->pwrgate + PWRGATE_WAKE_1) | PWRGATE_ON, s->pwrgate + PWRGATE_WAKE_1); t6040_wake_sync(); /* SOC-East retention off: clear DEV_DISABLE, then poll BUSY clear. */ - if (s->soc_east) { - val = readl(s->soc_east + SOC_EAST_RETENTION); - writel(val & ~PS_DEV_DISABLE, s->soc_east + SOC_EAST_RETENTION); - ret = readl_poll_timeout_atomic(s->soc_east + SOC_EAST_RETENTION, val, - !(val & PS_BUSY), 1, - SOC_EAST_TIMEOUT_US); - if (ret) - dev_warn(s->dev, "SOC-East retention word still busy on wake (%#x)\n", val); - } + val = readl(s->soc_east + SOC_EAST_RETENTION); + writel(val & ~PS_DEV_DISABLE, s->soc_east + SOC_EAST_RETENTION); + ret = readl_poll_timeout_atomic(s->soc_east + SOC_EAST_RETENTION, val, + !(val & PS_BUSY), 1, SOC_EAST_TIMEOUT_US); + if (ret) + dev_warn(s->dev, "SOC-East retention word still busy on wake (%#x)\n", val); t6040_wake_sync(); /* @@ -2241,11 +2192,9 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_aop_ps_restore(s); t6040_wake_sync(); - if (s->pwrgate) { - writel(readl(s->pwrgate + PWRGATE_CPM_0) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_0); - writel(readl(s->pwrgate + PWRGATE_CPM_1) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_1); - writel(readl(s->pwrgate + PWRGATE_CPM_2) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_2); - } + writel(readl(s->pwrgate + PWRGATE_CPM_0) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_0); + writel(readl(s->pwrgate + PWRGATE_CPM_1) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_1); + writel(readl(s->pwrgate + PWRGATE_CPM_2) | PWRGATE_ON, s->pwrgate + PWRGATE_CPM_2); t6040_wake_sync(); /* Undo the entry clk-mux park; the clock restore is otherwise a NOP. */ @@ -2253,8 +2202,7 @@ static void t6040_sleep_return(struct t6040_sleep *s) t6040_wake_sync(); /* Software ack of the armed S2R request (the S2R timer has no stop). */ - if (s->aop_global) - writel(0, s->aop_global + AOP_S2R_CTRL); + writel(0, s->aop_global + AOP_S2R_CTRL); t6040_wake_sync(); /* The keep-alive bits the commit cleared, back (the wake restore enables them). */ @@ -2270,9 +2218,8 @@ static void t6040_sleep_return(struct t6040_sleep *s) dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); dev_dbg(s->dev, "wake: resumes now %llu\n", READ_ONCE(s->mbox->resumes)); } - if (s->aop_global) - for (i = 0; i < AOP_WAKE_WORDS; i++) - s->wake_status[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); + for (i = 0; i < AOP_WAKE_WORDS; i++) + s->wake_status[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)); s->returned = true; } @@ -2426,8 +2373,9 @@ static int t6040_sleep_prepare(void) */ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) { + u32 p[AOP_WAKE_WORDS]; unsigned int refused; - int ret; + int ret, i; dev_info(s->dev, "dry run %u at the commit point: %u CPU(s) online\n", dry, num_online_cpus()); @@ -2436,15 +2384,10 @@ static int t6040_sleep_dry_enter(struct t6040_sleep *s, unsigned int dry) * A wake source pending and left unmasked by the masks a real commit arms would keep the * AP partition from powering down, and the sleep engine then stalls. Read only. */ - if (s->aop_global) { - u32 p[AOP_WAKE_WORDS]; - int i; - - for (i = 0; i < AOP_WAKE_WORDS; i++) - p[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)) & ~t6040_wake_mask[i]; - dev_info(s->dev, "dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", - (int)sizeof(p), p); - } + for (i = 0; i < AOP_WAKE_WORDS; i++) + p[i] = readl(s->aop_global + AOP_WAKE_STATUS(i)) & ~t6040_wake_mask[i]; + dev_info(s->dev, "dry-run commit point: wake status left unmasked by the commit's masks %*ph\n", + (int)sizeof(p), p); refused = t6040_sleep_refuse(s); dev_info(s->dev, "a real commit would %s here (%u check(s) failed)\n", refused ? "refuse" : "go on", refused); @@ -2602,8 +2545,6 @@ static int t6040_disp_off(struct t6040_sleep *s, const struct t6040_sleep_group struct t6040_psw sys = { .io = s->soc_east, .off = PS_DISP_SYS }; int ret; - if (!s->soc_east) - return -ENODEV; s->disp_down = true; /* * The PMC's bandwidth-request and boost interface enables of the display pipes @@ -3119,9 +3060,9 @@ static int t6040_sleep_probe(struct platform_device *pdev) return ret; /* - * Optional: the commit's and the wake's hardware (all NULL on absence; - * every write is guarded and the gate refuses if a required one is - * missing). The axi2af fabric bridges are fixed PAs, not DT. + * The commit's and the wake's register blocks are mandatory: without them a suspend + * could not be entered or undone, and the node always lists them. The axi2af fabric + * bridges are fixed PAs, not DT. */ BUILD_BUG_ON(ARRAY_SIZE(t6040_axi2af) != T6040_AXI2AF_DOMAINS); BUILD_BUG_ON(ARRAY_SIZE(t6040_tvm) != ARRAY_SIZE(((struct t6040_sleep *)0)->tvm_ctl)); @@ -3129,6 +3070,23 @@ static int t6040_sleep_probe(struct platform_device *pdev) ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); s->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); s->wdt = t6040_sleep_map(&pdev->dev, "wdt"); + s->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); + s->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); + s->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); + s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); + s->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); + s->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); + s->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); + s->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); + s->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); + if (!s->cpu_start || !s->wdt || !s->cluster_ctl || !s->pwrgate || !s->clkgen || + !s->spmi0 || !s->spmi_a0 || !s->mastermini || !s->soc_east || !s->soc_ps || + !s->aop_global) + return dev_err_probe(&pdev->dev, -ENODEV, + "a register block is missing or not mapped (cpu-start %d wdt %d cluster-ctl %d soc-pwrgate %d clkgen %d spmi0 %d spmi-a0 %d mastermini %d soc-east %d soc-ps %d aop-global %d)\n", + !!s->cpu_start, !!s->wdt, !!s->cluster_ctl, !!s->pwrgate, + !!s->clkgen, !!s->spmi0, !!s->spmi_a0, !!s->mastermini, + !!s->soc_east, !!s->soc_ps, !!s->aop_global); { struct resource wres; int widx = of_property_match_string(pdev->dev.of_node, "reg-names", "wdt"); @@ -3140,12 +3098,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) dev_warn(&pdev->dev, "the wdt block is not the AON watchdog: no WD2 restart at the wake\n"); } - s->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); - s->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); - s->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); - s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); - s->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); - s->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); s->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); s->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); for (i = 0; i < ARRAY_SIZE(s->dcs); i++) @@ -3156,9 +3108,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) s->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, 0x383200000ULL + i * 0x8000000ULL, 0x100); s->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); - s->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); - s->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); - s->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) s->axi2af_bridge[i] = t6040_ioremap_np(&pdev->dev, t6040_axi2af[i].bridge_pa, 0x200); @@ -3203,11 +3152,8 @@ static int t6040_sleep_probe(struct platform_device *pdev) suspend_set_ops(&t6040_sleep_ops); dev_info(&pdev->dev, - "refusal-first: %u display groups, cpu-start %s, wdt %s, commit/wake blocks %s, storage-apcie %s, s2r-mailbox %s, parking ops %s, dry run %u\n", - s->ngroups, s->cpu_start ? "mapped" : "absent", - s->wdt ? "mapped" : "absent", - (s->cluster_ctl && s->pwrgate && s->mastermini && s->spmi0 && - s->soc_east && s->aop_global) ? "mapped" : "incomplete", + "refusal-first: %u display groups, storage-apcie %s, s2r-mailbox %s, parking ops %s, dry run %u\n", + s->ngroups, s->apcie_st.valid ? "mapped" : "absent", s->mbox ? "present" : "absent", s->power_ops ? "registered" : "not registered", READ_ONCE(dry_run)); From 38ceef94e16bb9225c716a45d37efde254b6476d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:52:40 +0100 Subject: [PATCH 149/269] soc: apple: sleep-t6040: map each power word page once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The driver mapped the SOC-East page (0x502280000) twice, as soc-east for its first 0x1000 bytes and at a fixed address for 0xd000 bytes, and the SOC page (0x502800000) twice, as soc-ps and for 0x9000 bytes. Both pairs are the same non-posted window. Take the extent from the node now that it describes the whole page, and use soc-east and soc-ps for every power word. A node whose window is smaller than the pages the driver accesses fails the probe instead of leaving later accesses outside the mapping. Only base pointers change; the accesses and their order are the same. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 51 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 99e59172754ff..fe19cea4603ee 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -90,6 +90,9 @@ #define PS_STRIDE 8 #define PS_MAX_PER_GROUP 16 #define PS_SIO_FIRST 0x150 /* SOC-East: I2C0, the first SIO peripheral word */ +/* The extent of the two power word pages the driver accesses (the DT window sizes). */ +#define SOC_EAST_SIZE 0xd000 +#define SOC_PS_SIZE 0x9000 #define PS_SIO_LAST 0x228 /* SOC-East: UART6, the last */ /* @@ -390,13 +393,11 @@ struct t6040_sleep { void __iomem *dcs[16]; /* DCS(n,k): 0x220200000 + n * 0x2000000 + k * 0x200000 */ void __iomem *atc_cfg[4]; /* ATCPHY_CFG(n) 0x383200000 + n * 0x8000000 */ void __iomem *aop_ps; /* MiniPMGR AOP + audio island PS words 0x508304000..0x158 */ - /* The SOC and SOC-East power word pages at their fixed addresses. */ - void __iomem *pmgr_soc; /* 0x502800000: fabrics, ATC commons, DCS */ - void __iomem *pmgr_east; /* 0x502280000: fabrics, DCS, SIO */ u32 sio_ps_saved[(PS_SIO_LAST - PS_SIO_FIRST) / PS_STRIDE + 1]; bool sio_ps_saved_valid; - void __iomem *soc_east; - void __iomem *soc_ps; + /* The SOC-East and SOC power word pages: fabrics, ATC commons, DCS, SIO. */ + void __iomem *soc_east; /* 0x502280000 */ + void __iomem *soc_ps; /* 0x502800000 */ void __iomem *aop_global; void __iomem *axi2af_bridge[T6040_AXI2AF_DOMAINS]; struct aura_s2r_mailbox *mbox; @@ -962,10 +963,8 @@ static void t6040_sio_ps_save(struct t6040_sleep *s) unsigned int i; s->sio_ps_saved_valid = false; - if (!s->pmgr_east) - return; for (i = 0; i < ARRAY_SIZE(s->sio_ps_saved); i++) - s->sio_ps_saved[i] = readl(s->pmgr_east + PS_SIO_FIRST + i * PS_STRIDE); + s->sio_ps_saved[i] = readl(s->soc_east + PS_SIO_FIRST + i * PS_STRIDE); s->sio_ps_saved_valid = true; } @@ -979,7 +978,7 @@ static void t6040_sio_ps_restore(struct t6040_sleep *s) s->sio_ps_saved_valid = false; for (i = 0; i < ARRAY_SIZE(s->sio_ps_saved); i++) { u32 off = PS_SIO_FIRST + i * PS_STRIDE; - void __iomem *w = s->pmgr_east + off; + void __iomem *w = s->soc_east + off; u32 saved = s->sio_ps_saved[i]; if (FIELD_GET(PS_TARGET, saved) != PS_ACTIVE || @@ -1472,11 +1471,9 @@ static const u16 t6040_atc_common[4] = { 0x390, 0x3b8, 0x3e0, 0x408 }; static void t6040_atc_commons_on(struct t6040_sleep *s) { - void __iomem *b = s->pmgr_soc; + void __iomem *b = s->soc_ps; int i; - if (!b) - return; t6040_ps_raise_start(b, PS_FAB2_SOC); for (i = 0; i < ARRAY_SIZE(t6040_atc_common); i++) t6040_ps_raise_start(b, t6040_atc_common[i]); @@ -1546,11 +1543,9 @@ static void t6040_fabrics_on(struct t6040_sleep *s) int i; for (i = 0; i < ARRAY_SIZE(t6040_fabrics); i++) { - void __iomem *b = t6040_fabrics[i].east ? s->pmgr_east : s->pmgr_soc; + void __iomem *b = t6040_fabrics[i].east ? s->soc_east : s->soc_ps; u32 off = t6040_fabrics[i].off, v; - if (!b) - continue; v = readl(b + off); if (!(v & PS_AUTO_ENABLE) || FIELD_GET(PS_TARGET, v) != PS_ACTIVE) t6040_ps_raise(b, off); @@ -1579,9 +1574,9 @@ static const struct { static void __iomem *t6040_dcs_ps_reg(struct t6040_sleep *s, int i) { - void __iomem *b = t6040_dcs_ps[i].east ? s->pmgr_east : s->pmgr_soc; + void __iomem *b = t6040_dcs_ps[i].east ? s->soc_east : s->soc_ps; - return b ? b + t6040_dcs_ps[i].off : NULL; + return b + t6040_dcs_ps[i].off; } /* @@ -1596,8 +1591,6 @@ static u32 t6040_dcs_ps_auto_on(struct t6040_sleep *s) for (i = 0; i < ARRAY_SIZE(t6040_dcs_ps); i++) { void __iomem *r = t6040_dcs_ps_reg(s, i); - if (!r) - continue; v = readl(r); if ((v & PS_AUTO_ENABLE) && !(v & PS_MIN)) continue; @@ -2864,20 +2857,28 @@ static void t6040_sleep_watch_init(struct t6040_sleep *s, unsigned int i, dev_warn(s->dev, "%s: no power word to watch\n", name); } -/* A named register block, shared with the owning driver: mapped, not claimed. Optional. */ -static void __iomem *t6040_sleep_map(struct device *dev, const char *name) +/* + * A named register block, shared with the owning driver: mapped, not claimed. NULL when it is + * missing from the node or smaller than @min, the extent the driver accesses. + */ +static void __iomem *t6040_sleep_map_min(struct device *dev, const char *name, size_t min) { struct device_node *np = dev->of_node; struct resource res; int index; index = of_property_match_string(np, "reg-names", name); - if (index < 0 || of_address_to_resource(np, index, &res)) + if (index < 0 || of_address_to_resource(np, index, &res) || resource_size(&res) < min) return NULL; return t6040_ioremap_np(dev, res.start, resource_size(&res)); } +static void __iomem *t6040_sleep_map(struct device *dev, const char *name) +{ + return t6040_sleep_map_min(dev, name, 0); +} + /* * ANS's storage-apcie windows + PLL/lane tunables, from the apple,apcie node * the boot object publishes from the ADT. Our static tree has none @@ -3076,8 +3077,8 @@ static int t6040_sleep_probe(struct platform_device *pdev) s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); s->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); s->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); - s->soc_east = t6040_sleep_map(&pdev->dev, "soc-east"); - s->soc_ps = t6040_sleep_map(&pdev->dev, "soc-ps"); + s->soc_east = t6040_sleep_map_min(&pdev->dev, "soc-east", SOC_EAST_SIZE); + s->soc_ps = t6040_sleep_map_min(&pdev->dev, "soc-ps", SOC_PS_SIZE); s->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); if (!s->cpu_start || !s->wdt || !s->cluster_ctl || !s->pwrgate || !s->clkgen || !s->spmi0 || !s->spmi_a0 || !s->mastermini || !s->soc_east || !s->soc_ps || @@ -3099,7 +3100,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) "the wdt block is not the AON watchdog: no WD2 restart at the wake\n"); } s->aop_ps = t6040_ioremap_np(&pdev->dev, T6040_AOP_PS_PA, 0x160); - s->pmgr_soc = t6040_ioremap_np(&pdev->dev, 0x502800000ULL, 0x9000); for (i = 0; i < ARRAY_SIZE(s->dcs); i++) s->dcs[i] = t6040_ioremap_np(&pdev->dev, 0x220200000ULL + (i / 4) * 0x2000000ULL + (i % 4) * 0x200000ULL, @@ -3107,7 +3107,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) for (i = 0; i < ARRAY_SIZE(s->atc_cfg); i++) s->atc_cfg[i] = t6040_ioremap_np(&pdev->dev, 0x383200000ULL + i * 0x8000000ULL, 0x100); - s->pmgr_east = t6040_ioremap_np(&pdev->dev, 0x502280000ULL, 0xd000); for (i = 0; i < T6040_AXI2AF_DOMAINS; i++) s->axi2af_bridge[i] = t6040_ioremap_np(&pdev->dev, t6040_axi2af[i].bridge_pa, 0x200); From 7e81a56ba6c3d39286d7dd6dbcdd9605e751220d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:45:58 +0100 Subject: [PATCH 150/269] soc: apple: sleep: keep the registry's walkers with the registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The coprocessor and enclave table lives in sleep-registry.c, but the functions that walk it at the commit point sat in sleep.c, the controller of one SoC. Every controller that is bound on a SoC uses them, so they belong with the table. Move the readiness refusal, the last checks, the stop passes, the enclave's stop and the debugfs listing, together with the helpers they alone use, and declare the one that sleep.c still calls. The code is unchanged. sleep.c's enter function now calls the check for a power word that a coprocessor's last stop lowers where it had it inlined. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 2 + drivers/soc/apple/sleep-registry.c | 149 +++++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 147 ---------------------------- 3 files changed, 151 insertions(+), 147 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 39a9b8c17cd2e..4775539701a4b 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -320,6 +320,8 @@ unsigned int apple_sleep_coprocs_refuse(struct device *dev, bool *pcie_ans_idled /* Each ready coprocessor's last check: the first refusal, or 0. */ int apple_sleep_coprocs_check(struct device *dev); bool apple_sleep_enclave_registered(void); +/* A power word that a ready coprocessor's last stop lowers: on until the commit. */ +bool apple_sleep_coproc_lowers(u32 ps); /* The enclave's stop, last of all refusals: nonzero only if no request was sent. */ int apple_sleep_enclave_stop(struct device *dev); /* The last stops, those registered first with @first and then the rest. */ diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c index 0cdcf7a2f5ee8..a24dc6d3fb6dd 100644 --- a/drivers/soc/apple/sleep-registry.c +++ b/drivers/soc/apple/sleep-registry.c @@ -15,9 +15,11 @@ #include #include #include +#include #include #include +#include #include #include "sleep-internal.h" @@ -213,3 +215,150 @@ bool apple_sleep_commit_stops_coprocs(void) return ctl && ctl->commit_stops(); } EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); + +/* + * The last thing that can fail on the way to the commit: no CPU but this one is up and + * interrupts are off, so no lock can be held. A coprocessor stopped here comes back only + * through a real sleep, so every refusal has to be behind this, and nothing after it may + * go back. In this order, on ANS itself: whatever its driver does last, + * status and control read, then 0 written twice. + */ +static void apple_sleep_coproc_run_off(struct apple_sleep_coproc *c) +{ + /* Its own last stop cleared its run bits. */ + if (!c->asc) { + c->stopped = true; + return; + } + c->status = readl(c->asc + ASC_CPU_STATUS); + writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); + writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); + c->stopped = true; +} + +/* A power word that a ready coprocessor's last stop lowers: on until the commit. */ +bool apple_sleep_coproc_lowers(u32 ps) +{ + int i; + + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) + if (apple_sleep_coprocs[i].dev && apple_sleep_coprocs[i].ready && + apple_sleep_coprocs[i].first && apple_sleep_coprocs[i].ps == ps) + return true; + return false; +} + +bool apple_sleep_enclave_registered(void) +{ + return apple_sleep_enclave.dev; +} + +void apple_sleep_registry_show(struct seq_file *s) +{ + int i; + + guard(mutex)(&apple_sleep_coprocs_lock); + seq_puts(s, "coprocessors registered for the commit's last stop:\n"); + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev) + continue; + seq_printf(s, " %s%s%s%s%s\n", dev_name(c->dev), c->pcie_ans ? " (ANS)" : "", + c->first ? " (first)" : "", c->last_check ? ", last check" : "", + c->ready ? ", ready" : ""); + } + seq_printf(s, "enclave sleep request left for the last refusal: %s\n", + apple_sleep_enclave.dev ? dev_name(apple_sleep_enclave.dev) : "none"); +} + +/* A registered coprocessor that isn't ready refuses the sleep. */ +unsigned int apple_sleep_coprocs_refuse(struct device *dev, bool *pcie_ans_idled) +{ + unsigned int refused = 0; + int i; + + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev) + continue; + if (c->ready) { + *pcie_ans_idled |= c->pcie_ans; + } else { + /* Nothing may lower a coprocessor's domain under its driver. */ + dev_err(dev, "refused: %s is running and its driver has not quiesced it\n", + dev_name(c->dev)); + refused++; + } + } + + return refused; +} + +/* The last refusals, each coprocessor's own: nothing has stopped yet. */ +int apple_sleep_coprocs_check(struct device *dev) +{ + int i, ret; + + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev || !c->ready || c->stopped || !c->last_check) + continue; + ret = c->last_check(c->data); + if (ret) { + dev_err(dev, "refused: %s: %d\n", dev_name(c->dev), ret); + return ret; + } + } + + return 0; +} + +/* A nonzero result must mean no SLEEP was sent; post-send errors are fatal. */ +int apple_sleep_enclave_stop(struct device *dev) +{ + int ret; + + if (!apple_sleep_enclave.stop) + return 0; + ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); + if (ret) + dev_err(dev, "refused before SEP SLEEP was sent: %d\n", ret); + + return ret; +} + +/* + * In two passes, after the last refusal: from the first stop only a real sleep restarts the + * GPU, so a failure here goes on to the commit. Those registered first stop whole first, + * before the off words drop under them: the GPU withdraws its PMP request there, and the + * PMP has to see that before its own stop. The rest after the match. With ANS among the ready + * ones, @pcie_ans. + */ +void apple_sleep_coprocs_stop(bool first, bool *pcie_ans) +{ + unsigned int order; + int i; + + if (first) + *pcie_ans = false; + /* Those registered first run by their order, the others in one pass over the table. */ + for (order = 0; order < (first ? APPLE_SLEEP_FIRST_ORDERS : 1); order++) { + for (i = 0; i < APPLE_SLEEP_COPROCS; i++) { + struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; + + if (!c->dev || !c->ready || c->first != first) + continue; + if (first && c->order != order) + continue; + *pcie_ans |= c->pcie_ans; + if (c->stopped) + continue; + c->stop_ret = c->last_stop ? c->last_stop(c->data) : 0; + apple_sleep_coproc_run_off(c); + } + } + dsb(sy); +} diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 7cd0cb0def532..fbc82d638450a 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -792,106 +792,6 @@ static const struct apple_sleep_ctl apple_sleep_reg_ctl = { .enclave_admit = apple_sleep_reg_enclave_admit, }; -/* - * The last thing that can fail on the way to the commit: no CPU but this one is up and - * interrupts are off, so no lock can be held. A coprocessor stopped here comes back only - * through a real sleep, so every refusal has to be behind this, and nothing after it may - * go back. In the order measured on macOS, on ANS itself: whatever its driver does last, - * status and control read, then 0 written twice. - */ -static void apple_sleep_coproc_run_off(struct apple_sleep_coproc *c) -{ - /* Its own last stop cleared its run bits. */ - if (!c->asc) { - c->stopped = true; - return; - } - c->status = readl(c->asc + ASC_CPU_STATUS); - writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); - writel(readl(c->asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, c->asc + ASC_CPU_CONTROL); - c->stopped = true; -} - -/* A power word that a ready coprocessor's last stop lowers: on until the commit. */ -static bool apple_sleep_coproc_lowers(u32 ps) -{ - int i; - - for (i = 0; i < SLEEP_COPROCS; i++) - if (apple_sleep_coprocs[i].dev && apple_sleep_coprocs[i].ready && - apple_sleep_coprocs[i].first && apple_sleep_coprocs[i].ps == ps) - return true; - return false; -} - -bool apple_sleep_enclave_registered(void) -{ - return apple_sleep_enclave.dev; -} - -void apple_sleep_registry_show(struct seq_file *s) -{ - int i; - - guard(mutex)(&apple_sleep_coprocs_lock); - seq_puts(s, "coprocessors registered for the commit's last stop:\n"); - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev) - continue; - seq_printf(s, " %s%s%s%s%s\n", dev_name(c->dev), c->pcie_ans ? " (ANS)" : "", - c->first ? " (first)" : "", c->last_check ? ", last check" : "", - c->ready ? ", ready" : ""); - } - seq_printf(s, "enclave sleep request left for the last refusal: %s\n", - apple_sleep_enclave.dev ? dev_name(apple_sleep_enclave.dev) : "none"); -} - -/* A registered coprocessor that isn't ready refuses the sleep. */ -unsigned int apple_sleep_coprocs_refuse(struct device *dev, bool *pcie_ans_idled) -{ - unsigned int refused = 0; - int i; - - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev) - continue; - if (c->ready) { - *pcie_ans_idled |= c->pcie_ans; - } else { - /* Nothing may lower a coprocessor's domain under its driver. */ - dev_err(dev, "refused: %s is running and its driver has not quiesced it\n", - dev_name(c->dev)); - refused++; - } - } - - return refused; -} - -/* The last refusals, each coprocessor's own: nothing has stopped yet. */ -int apple_sleep_coprocs_check(struct device *dev) -{ - int i, ret; - - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev || !c->ready || c->stopped || !c->last_check) - continue; - ret = c->last_check(c->data); - if (ret) { - dev_err(dev, "refused: %s: %d\n", dev_name(c->dev), ret); - return ret; - } - } - - return 0; -} - /* * The other cores stop at the commit, and one left awake there keeps the engine waiting * until WD2 resets the SoC. Each answers here, before the display goes off, and waits for the @@ -907,53 +807,6 @@ static int apple_sleep_cpus_hold(struct apple_sleep *s) return ret; } -/* A nonzero result must mean no SLEEP was sent; post-send errors are fatal. */ -int apple_sleep_enclave_stop(struct device *dev) -{ - int ret; - - if (!apple_sleep_enclave.stop) - return 0; - ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); - if (ret) - dev_err(dev, "refused before SEP SLEEP was sent: %d\n", ret); - - return ret; -} - -/* - * In two passes, after the last refusal: from the first stop only a real sleep restarts the - * GPU, so a failure here goes on to the commit. Those registered first stop whole first, - * before macOS's off words drop under them: the GPU withdraws its PMP request there, and the - * PMP has to see that before its own stop. The rest after the match. With ANS among the ready - * ones, @pcie_ans. - */ -void apple_sleep_coprocs_stop(bool first, bool *pcie_ans) -{ - unsigned int order; - int i; - - if (first) - *pcie_ans = false; - /* Those registered first run by their order, the others in one pass over the table. */ - for (order = 0; order < (first ? APPLE_SLEEP_FIRST_ORDERS : 1); order++) { - for (i = 0; i < SLEEP_COPROCS; i++) { - struct apple_sleep_coproc *c = &apple_sleep_coprocs[i]; - - if (!c->dev || !c->ready || c->first != first) - continue; - if (first && c->order != order) - continue; - *pcie_ans |= c->pcie_ans; - if (c->stopped) - continue; - c->stop_ret = c->last_stop ? c->last_stop(c->data) : 0; - apple_sleep_coproc_run_off(c); - } - } - dsb(sy); -} - /* MPIDR's cluster is affinity 2, its core affinity 0, as 0.0.n and 1.1.n on T8140. */ static unsigned int sleep_cpu_cluster(u64 mpidr) { From 5066af22cd1cc17589ae766b989157cafeff881e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:48:19 +0100 Subject: [PATCH 151/269] soc: apple: sleep: keep the registry's answers in the registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each sleep controller handed the registry a table of two callbacks, whether its commit stops the registered coprocessors and whether an enclave may register. Both answers follow from three facts the registry can hold itself: a controller is bound, its probe is complete, and a suspend goes through firmware without being a dry run. The only difference between the SoCs is that T6040 takes every enclave registration, a dry run included, and T8140 only while such a commit is the case. Have the controller bind with a flag for that, tell the registry when its probe is complete, and drop struct apple_sleep_ctl and the callbacks. T8140 needs no dry-run test because its dry-run level is never raised, and it binds only at EL2. The answers are the same at every point of the probe and of a suspend. The SEP host test now extracts apple_sleep_power_loss() instead of the two callbacks and models the registry's state; all of its mutations still fail. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 23 +++++++------- drivers/soc/apple/sleep-registry.c | 42 ++++++++++++++----------- drivers/soc/apple/sleep-t6040.c | 13 ++------ drivers/soc/apple/sleep.c | 25 +++------------ tools/asahi/sep-suspend-errors-tests.py | 21 ++++++------- 5 files changed, 51 insertions(+), 73 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 4775539701a4b..f960f482619b7 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -57,19 +57,18 @@ struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev); /* * The sleep controller bound on this SoC. Each binds only to its own SoC's - * compatible, so at most one is bound. + * compatible, so at most one is bound. Bound, it tells the registry when its + * probe is complete: from then on a suspend through firmware that is not a dry + * run is one whose commit stops the registered coprocessors. * - * @commit_stops: this sleep's commit stops the registered coprocessors. - * @enclave_admit: 0 to take an enclave registration, or the error to refuse - * it with. NULL takes every one. + * APPLE_SLEEP_REG_ENCLAVE_ANY: take every enclave registration. Without it one + * is taken only while such a commit is the case. */ -struct apple_sleep_ctl { - bool (*commit_stops)(void); - int (*enclave_admit)(void); -}; +#define APPLE_SLEEP_REG_ENCLAVE_ANY BIT(0) -int apple_sleep_registry_bind(const struct apple_sleep_ctl *ctl); -void apple_sleep_registry_unbind(const struct apple_sleep_ctl *ctl); +int apple_sleep_registry_bind(unsigned int flags); +void apple_sleep_registry_unbind(void); +void apple_sleep_registry_ready(void); /* Must match aura's struct aura_s2r_mailbox. */ #define AURA_S2R_MAGIC 0x3152325341525541ULL /* "AURAS2R1" */ @@ -270,12 +269,12 @@ static inline void apple_sleep_wd2_arm(void __iomem *wdt, unsigned int secs) * A SoC whose sleep doesn't fit the lists of struct apple_sleep_soc brings its own sequences. * @probe then runs in place of the driver's own, and installs the part's suspend operations. * @restore, if set, runs when the system comes back from a hibernation image. - * @registry: the part's answers to the registry, which the driver binds around @probe. + * @registry_flags: how the part binds to the registry, which the driver does around @probe. */ struct apple_sleep_part { int (*probe)(struct platform_device *pdev); int (*restore)(struct device *dev); - struct apple_sleep_ctl registry; + unsigned int registry_flags; }; /* sleep-t6040.c */ diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c index a24dc6d3fb6dd..0e7db409a1d47 100644 --- a/drivers/soc/apple/sleep-registry.c +++ b/drivers/soc/apple/sleep-registry.c @@ -29,23 +29,32 @@ struct apple_sleep_enclave apple_sleep_enclave; /* Guards the table, the enclave slot and the bound controller. */ DEFINE_MUTEX(apple_sleep_coprocs_lock); -static const struct apple_sleep_ctl *apple_sleep_ctl; +#define APPLE_SLEEP_REG_BOUND BIT(8) +#define APPLE_SLEEP_REG_READY BIT(9) -int apple_sleep_registry_bind(const struct apple_sleep_ctl *ctl) +/* The bound controller's flags and state. */ +static unsigned int apple_sleep_reg; + +int apple_sleep_registry_bind(unsigned int flags) { guard(mutex)(&apple_sleep_coprocs_lock); - if (apple_sleep_ctl) + if (apple_sleep_reg & APPLE_SLEEP_REG_BOUND) return -EBUSY; - WRITE_ONCE(apple_sleep_ctl, ctl); + WRITE_ONCE(apple_sleep_reg, flags | APPLE_SLEEP_REG_BOUND); return 0; } -void apple_sleep_registry_unbind(const struct apple_sleep_ctl *ctl) +void apple_sleep_registry_unbind(void) +{ + guard(mutex)(&apple_sleep_coprocs_lock); + WRITE_ONCE(apple_sleep_reg, 0); +} + +void apple_sleep_registry_ready(void) { guard(mutex)(&apple_sleep_coprocs_lock); - if (apple_sleep_ctl == ctl) - WRITE_ONCE(apple_sleep_ctl, NULL); + WRITE_ONCE(apple_sleep_reg, apple_sleep_reg | APPLE_SLEEP_REG_READY); } struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev) @@ -138,22 +147,18 @@ EXPORT_SYMBOL_GPL(apple_sleep_coproc_set_last_check); /* With no sleep controller bound, nothing would ever send the request: refuse it. */ int apple_sleep_enclave_register(struct device *dev, int (*stop)(void *data), void *data) { - const struct apple_sleep_ctl *ctl; - int ret; + unsigned int reg; if (!is_kernel_in_hyp_mode()) return -ENODEV; if (!stop) return -EINVAL; - ctl = READ_ONCE(apple_sleep_ctl); - if (!ctl) + reg = READ_ONCE(apple_sleep_reg); + if (!(reg & APPLE_SLEEP_REG_BOUND)) + return -EOPNOTSUPP; + if (!(reg & APPLE_SLEEP_REG_ENCLAVE_ANY) && !apple_sleep_commit_stops_coprocs()) return -EOPNOTSUPP; - if (ctl->enclave_admit) { - ret = ctl->enclave_admit(); - if (ret) - return ret; - } guard(mutex)(&apple_sleep_coprocs_lock); if (apple_sleep_enclave.dev) @@ -210,9 +215,8 @@ EXPORT_SYMBOL_GPL(apple_sleep_coproc_stopped); bool apple_sleep_commit_stops_coprocs(void) { - const struct apple_sleep_ctl *ctl = READ_ONCE(apple_sleep_ctl); - - return ctl && ctl->commit_stops(); + return (READ_ONCE(apple_sleep_reg) & APPLE_SLEEP_REG_READY) && is_kernel_in_hyp_mode() && + apple_sleep_power_loss(); } EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index fe19cea4603ee..27a2c85fff82b 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -498,12 +498,6 @@ static void t6040_sleep_set_dry(struct t6040_sleep *s, unsigned int dry) apple_sleep_set_dry(dry); } -/* A real entry: its commit stops the registered coprocessors; a dry run's never does. */ -static bool t6040_sleep_reg_commit_stops(void) -{ - return READ_ONCE(t6040_sleep) && is_kernel_in_hyp_mode() && apple_sleep_power_loss(); -} - /* * In the power-state check, a sub-block is on while its TARGET is not 0. A * pipe's CPU word never counts: it stays under automatic power gating (taking it out @@ -3136,6 +3130,7 @@ static int t6040_sleep_probe(struct platform_device *pdev) platform_set_drvdata(pdev, s); t6040_sleep = s; + apple_sleep_registry_ready(); dir = debugfs_create_dir("t6040-sleep", NULL); debugfs_create_file("preflight", 0400, dir, s, &t6040_sleep_preflight_fops); @@ -3166,8 +3161,6 @@ const struct apple_sleep_part t6040_sleep_part = { #ifdef CONFIG_HIBERNATION .restore = t6040_sleep_restore, #endif - /* No enclave_admit: every enclave is taken, in a dry run as in a real sleep. */ - .registry = { - .commit_stops = t6040_sleep_reg_commit_stops, - }, + /* Every enclave is taken, in a dry run as in a real sleep. */ + .registry_flags = APPLE_SLEEP_REG_ENCLAVE_ANY, }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index fbc82d638450a..25fc71e58c38e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -776,22 +776,6 @@ bool apple_sleep_power_loss(void) } EXPORT_SYMBOL_GPL(apple_sleep_power_loss); -/* The registry's questions, answered for this SoC's sleep. */ -static bool apple_sleep_reg_commit_stops(void) -{ - return READ_ONCE(apple_sleep) && pm_suspend_via_firmware(); -} - -static int apple_sleep_reg_enclave_admit(void) -{ - return apple_sleep_reg_commit_stops() ? 0 : -EOPNOTSUPP; -} - -static const struct apple_sleep_ctl apple_sleep_reg_ctl = { - .commit_stops = apple_sleep_reg_commit_stops, - .enclave_admit = apple_sleep_reg_enclave_admit, -}; - /* * The other cores stop at the commit, and one left awake there keeps the engine waiting * until WD2 resets the SoC. Each answers here, before the display goes off, and waits for the @@ -3693,12 +3677,12 @@ static int apple_sleep_probe(struct platform_device *pdev) /* A SoC with its own sequences also has its own conditions, and its own answers. */ if (soc && soc->part) { - ret = apple_sleep_registry_bind(&soc->part->registry); + ret = apple_sleep_registry_bind(soc->part->registry_flags); if (ret) return dev_err_probe(dev, ret, "another sleep controller is bound\n"); ret = soc->part->probe(pdev); if (ret) - apple_sleep_registry_unbind(&soc->part->registry); + apple_sleep_registry_unbind(); return ret; } @@ -3756,17 +3740,18 @@ static int apple_sleep_probe(struct platform_device *pdev) platform_set_drvdata(pdev, s); - ret = apple_sleep_registry_bind(&apple_sleep_reg_ctl); + ret = apple_sleep_registry_bind(0); if (ret) return dev_err_probe(dev, ret, "another sleep controller is bound\n"); ret = spin_table_set_power_ops(&sleep_cpu_power_ops); if (ret) { - apple_sleep_registry_unbind(&apple_sleep_reg_ctl); + apple_sleep_registry_unbind(); return dev_err_probe(dev, ret, "cannot control CPU power\n"); } /* Last: the exported helpers take it as a probed driver, and a failure frees s. */ apple_sleep = s; + apple_sleep_registry_ready(); /* Deep becomes the default sleep state, unless mem_sleep_default= chose another. */ suspend_set_ops(&apple_sleep_ops); diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index 10f2a4b904b2b..6837789b5fe5f 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -140,15 +140,12 @@ def replace_once(source, before, after): static struct apple_mbox_hw hw; static struct apple_mbox mbox; static int apple_sleep_coprocs_lock; -static void *apple_sleep; -struct apple_sleep_ctl { bool (*commit_stops)(void); int (*enclave_admit)(void); }; -static bool apple_sleep_reg_commit_stops(void); -static int apple_sleep_reg_enclave_admit(void); -/* The T8140 controller's answers, bound to the registry as its probe binds them. */ -static const struct apple_sleep_ctl apple_sleep_reg_ctl = { - apple_sleep_reg_commit_stops, apple_sleep_reg_enclave_admit, -}; -static const struct apple_sleep_ctl *apple_sleep_ctl = &apple_sleep_reg_ctl; +#define APPLE_SLEEP_REG_ENCLAVE_ANY BIT(0) +#define APPLE_SLEEP_REG_BOUND BIT(8) +#define APPLE_SLEEP_REG_READY BIT(9) +/* The T8140 controller as its probe leaves the registry: bound, then ready. */ +static unsigned int apple_sleep_reg = APPLE_SLEEP_REG_BOUND | APPLE_SLEEP_REG_READY; +static unsigned int apple_sleep_dry; static struct { struct device *dev; int (*stop)(void *); void *data; } apple_sleep_enclave; @@ -277,7 +274,7 @@ def replace_once(source, before, after): memset(&hw, 0, sizeof(hw)); mbox = (struct apple_mbox){ .regs = &hw, .hw = &hw, .rx = foreign_rx, .cookie = &device }; config_enabled = hyp = sep_pm_suspending = dart_present = dart_ready = map_ok = true; - firmware = tx_ready = true; apple_sleep = &device; + firmware = tx_ready = true; apple_sleep_reg = APPLE_SLEEP_REG_BOUND | APPLE_SLEEP_REG_READY; real_wake = pending_wake = late_wake = sleep_refuse_before_send = false; domain_error = property_error = address_error = read_error = gate_after = reads = 0; put_refs = maps = unmaps = lookups = prepares = restores = sends = closes = 0; @@ -294,7 +291,7 @@ def replace_once(source, before, after): case 0: config_enabled = value; break; case 1: hyp = value; break; case 2: sep_pm_suspending = value; break; case 3: dart_present = value; break; case 4: map_ok = value; break; case 5: domain_error = value; break; - case 6: read_error = value; break; case 7: apple_sleep = value ? &device : NULL; break; + case 6: read_error = value; break; case 7: apple_sleep_reg = APPLE_SLEEP_REG_BOUND | (value ? APPLE_SLEEP_REG_READY : 0); break; case 8: gate_after = value; break; case 9: sep_pm_commit.registered = value; break; case 10: read_error_after = value; break; case 11: dart_ready = value; break; case 12: real_wake = value; break; case 13: hw.is_ascwrap_v8 = value; break; @@ -725,7 +722,7 @@ def sources(source, contracts, mutation): 'sep_pm_commit_prepare', 'sep_pm_commit_finish', 'sep_pm_suspend_noirq', 'sep_pm_notify'], 'mailbox': ['apple_mbox_poll_one_supported', 'apple_mbox_poll_one'], - 'sleep': ['apple_sleep_reg_commit_stops', 'apple_sleep_reg_enclave_admit'], + 'sleep': ['apple_sleep_power_loss'], 'registry': ['apple_sleep_commit_stops_coprocs', 'apple_sleep_enclave_register', 'apple_sleep_enclave_unregister'], 'pm': ['leave_for_commit', 'commit_finish', 'clear_dev_disable', 'dart_restore'], From f750948699a3dedc87cdcf4e2b6556c18699d74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:49:25 +0100 Subject: [PATCH 152/269] soc: apple: sleep: keep the wake information with the registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The lid's state and the radio's wake answer, the USB wake ports and the sleep's decision to take none of them were kept in sleep.c, the controller of one SoC, and a part that declined them called a setter at the start of its probe. They are answers any controller gives, so move them next to the registry, and have a controller say what it takes when it binds, as it already does for enclaves. A controller bound without the lid or the USB wake ports gets the answers of a kernel with no sleep driver, and so does a kernel with no controller bound. apple_sleep_set_part_plain_wake() goes, and with it the state that was set before the probe could fail. A T6040 probe that fails after that point used to leave the lid and USB wake ports declined with no controller bound; they now answer as they do with none, which is what their description says. Nothing else changes: T8140 binds with both capabilities, T6040 with neither, and the answers while bound are the same. The T8140 sleep's start forgets the two wake answers through one call. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-internal.h | 14 +++--- drivers/soc/apple/sleep-registry.c | 68 ++++++++++++++++++++++++++++++ drivers/soc/apple/sleep-t6040.c | 8 ++-- drivers/soc/apple/sleep.c | 64 ++-------------------------- 4 files changed, 83 insertions(+), 71 deletions(-) diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index f960f482619b7..35afc3e81d8f5 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -63,8 +63,14 @@ struct apple_sleep_coproc *apple_sleep_coproc_find(struct device *dev); * * APPLE_SLEEP_REG_ENCLAVE_ANY: take every enclave registration. Without it one * is taken only while such a commit is the case. + * APPLE_SLEEP_REG_LID_AT_WAKE, APPLE_SLEEP_REG_USB_WAKE_PORTS: the sleep takes the lid's state + * at the wake, and the USB wake ports. Without them + * apple_sleep_lid_closed_at_wake() and apple_sleep_usb_port_can_wake() answer as they do + * with no sleep driver, whatever the SMC's and the USB host's drivers told. */ #define APPLE_SLEEP_REG_ENCLAVE_ANY BIT(0) +#define APPLE_SLEEP_REG_LID_AT_WAKE BIT(1) +#define APPLE_SLEEP_REG_USB_WAKE_PORTS BIT(2) int apple_sleep_registry_bind(unsigned int flags); void apple_sleep_registry_unbind(void); @@ -298,12 +304,8 @@ void __iomem *apple_sleep_apcie_block(struct device *dev, struct device_node *np const char *name, const char *prop, struct apple_tunable **tunable); -/* - * A part whose sleep takes neither the lid's state at the wake nor the USB wake ports says so - * at its probe. apple_sleep_lid_closed_at_wake() and apple_sleep_usb_port_can_wake() then - * answer as they do with no sleep driver, whatever the SMC's and the USB host's drivers told. - */ -void apple_sleep_set_part_plain_wake(void); +/* Each sleep begins with no answer about the wake before it. */ +void apple_sleep_wake_info_forget(void); /* For a debugfs file: who is registered, and how far each is. */ void apple_sleep_registry_show(struct seq_file *s); diff --git a/drivers/soc/apple/sleep-registry.c b/drivers/soc/apple/sleep-registry.c index 0e7db409a1d47..5903c0fbef337 100644 --- a/drivers/soc/apple/sleep-registry.c +++ b/drivers/soc/apple/sleep-registry.c @@ -366,3 +366,71 @@ void apple_sleep_coprocs_stop(bool first, bool *pcie_ans) } dsb(sy); } + +/* + * A controller that was tested with neither the lid's state at the wake nor the USB wake ports + * binds without the capability: the queries then answer as they do with no sleep driver. + */ +static bool apple_sleep_declines(unsigned int cap) +{ + unsigned int reg = READ_ONCE(apple_sleep_reg); + + return (reg & APPLE_SLEEP_REG_BOUND) && !(reg & cap); +} + +static unsigned long sleep_usb_wake_ports; + +void apple_sleep_usb_set_wake_ports(unsigned long ports) +{ + WRITE_ONCE(sleep_usb_wake_ports, ports); +} +EXPORT_SYMBOL_GPL(apple_sleep_usb_set_wake_ports); + +bool apple_sleep_usb_port_can_wake(unsigned int port) +{ + if (apple_sleep_declines(APPLE_SLEEP_REG_USB_WAKE_PORTS)) + return false; + + return port < BITS_PER_LONG && (READ_ONCE(sleep_usb_wake_ports) & BIT(port)); +} +EXPORT_SYMBOL_GPL(apple_sleep_usb_port_can_wake); + +/* Read in the SMC's noirq resume. Forgotten as each sleep begins. */ +static int sleep_lid_closed = -ENODATA; + +void apple_sleep_lid_at_wake(bool closed) +{ + WRITE_ONCE(sleep_lid_closed, closed); +} +EXPORT_SYMBOL_GPL(apple_sleep_lid_at_wake); + +int apple_sleep_lid_closed_at_wake(void) +{ + if (apple_sleep_declines(APPLE_SLEEP_REG_LID_AT_WAKE)) + return -ENODATA; + + return READ_ONCE(sleep_lid_closed); +} +EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_at_wake); + +/* Set in the SMC's noirq resume. Forgotten as each sleep begins. */ +static int sleep_radio_woke = -ENODATA; + +void apple_sleep_radio_at_wake(bool woke) +{ + WRITE_ONCE(sleep_radio_woke, woke); +} +EXPORT_SYMBOL_GPL(apple_sleep_radio_at_wake); + +int apple_sleep_radio_woke(void) +{ + return READ_ONCE(sleep_radio_woke); +} +EXPORT_SYMBOL_GPL(apple_sleep_radio_woke); + +/* Each sleep begins with no answer about the wake before it. */ +void apple_sleep_wake_info_forget(void) +{ + WRITE_ONCE(sleep_lid_closed, -ENODATA); + WRITE_ONCE(sleep_radio_woke, -ENODATA); +} diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 27a2c85fff82b..6074a77fec6aa 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -3042,9 +3042,6 @@ static int t6040_sleep_probe(struct platform_device *pdev) struct dentry *dir; int ret, i; - /* This sleep was tested with neither the lid's state at the wake nor USB wake ports. */ - apple_sleep_set_part_plain_wake(); - s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL); if (!s) return -ENOMEM; @@ -3161,6 +3158,9 @@ const struct apple_sleep_part t6040_sleep_part = { #ifdef CONFIG_HIBERNATION .restore = t6040_sleep_restore, #endif - /* Every enclave is taken, in a dry run as in a real sleep. */ + /* + * Every enclave is taken, in a dry run as in a real sleep. This sleep was tested with + * neither the lid's state at the wake nor USB wake ports. + */ .registry_flags = APPLE_SLEEP_REG_ENCLAVE_ANY, }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 25fc71e58c38e..a7f25c47e5bdc 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -550,14 +550,6 @@ struct apple_sleep { static struct apple_sleep *apple_sleep; -/* The SoC's part takes neither the lid at the wake nor the USB wake ports. */ -static bool apple_sleep_part_plain_wake; - -void apple_sleep_set_part_plain_wake(void) -{ - WRITE_ONCE(apple_sleep_part_plain_wake, true); -} - /* A registered coprocessor its driver has readied, whose run bit the last step clears. */ static bool apple_sleep_coproc_ready_node(const struct device_node *np) { @@ -578,23 +570,6 @@ void apple_sleep_usb_set_asleep(bool asleep) } EXPORT_SYMBOL_GPL(apple_sleep_usb_set_asleep); -static unsigned long sleep_usb_wake_ports; - -void apple_sleep_usb_set_wake_ports(unsigned long ports) -{ - WRITE_ONCE(sleep_usb_wake_ports, ports); -} -EXPORT_SYMBOL_GPL(apple_sleep_usb_set_wake_ports); - -bool apple_sleep_usb_port_can_wake(unsigned int port) -{ - if (READ_ONCE(apple_sleep_part_plain_wake)) - return false; - - return port < BITS_PER_LONG && (READ_ONCE(sleep_usb_wake_ports) & BIT(port)); -} -EXPORT_SYMBOL_GPL(apple_sleep_usb_port_can_wake); - /* The USB host's vote, kept until the word is mapped. Only bit 0 is touched, as on macOS. */ static DEFINE_SPINLOCK(sleep_slp_ddr_lock); static bool sleep_slp_ddr_usb; @@ -1730,39 +1705,6 @@ void apple_sleep_lid_closed_wake(void) } EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_wake); -/* Read in the SMC's noirq resume. Forgotten as each sleep begins. */ -static int sleep_lid_closed = -ENODATA; - -void apple_sleep_lid_at_wake(bool closed) -{ - WRITE_ONCE(sleep_lid_closed, closed); -} -EXPORT_SYMBOL_GPL(apple_sleep_lid_at_wake); - -int apple_sleep_lid_closed_at_wake(void) -{ - if (READ_ONCE(apple_sleep_part_plain_wake)) - return -ENODATA; - - return READ_ONCE(sleep_lid_closed); -} -EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_at_wake); - -/* Set in the SMC's noirq resume. Forgotten as each sleep begins. */ -static int sleep_radio_woke = -ENODATA; - -void apple_sleep_radio_at_wake(bool woke) -{ - WRITE_ONCE(sleep_radio_woke, woke); -} -EXPORT_SYMBOL_GPL(apple_sleep_radio_at_wake); - -int apple_sleep_radio_woke(void) -{ - return READ_ONCE(sleep_radio_woke); -} -EXPORT_SYMBOL_GPL(apple_sleep_radio_woke); - /* * The SMC woke the AP for the port alone, and this sleep cut the debug link: the cut's own * events. Sleep again. The link stays down until tipd rearms it at a full wake, so the next @@ -2975,8 +2917,7 @@ static int apple_sleep_begin(suspend_state_t state) { /* The last sleep's cut is its port controller's to take, by its .complete. */ WRITE_ONCE(sleep_debug_link_cut, false); - WRITE_ONCE(sleep_lid_closed, -ENODATA); - WRITE_ONCE(sleep_radio_woke, -ENODATA); + apple_sleep_wake_info_forget(); /* The PM core also calls .begin for s2idle when no s2idle ops are installed. */ if (state != PM_SUSPEND_MEM) return 0; @@ -3740,7 +3681,8 @@ static int apple_sleep_probe(struct platform_device *pdev) platform_set_drvdata(pdev, s); - ret = apple_sleep_registry_bind(0); + ret = apple_sleep_registry_bind(APPLE_SLEEP_REG_LID_AT_WAKE | + APPLE_SLEEP_REG_USB_WAKE_PORTS); if (ret) return dev_err_probe(dev, ret, "another sleep controller is bound\n"); ret = spin_table_set_power_ops(&sleep_cpu_power_ops); From c60ff39257242348c6e72e2317016bf79ca5d26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:51:32 +0100 Subject: [PATCH 153/269] soc: apple: sleep: build every sleep controller and drop two symbols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit APPLE_SLEEP_REGISTRY has only one selector, APPLE_SOC_SLEEP, and the registry is linked into the same object as the controller, so the symbol selects nothing a user could choose. APPLE_T6040_SLEEP exists for the dependency of the T6040 sleep on the SMC driver and for MFD_SYSCON, but a board that has no T6040 sleep controller already gets none: the node's status in the device tree is the switch. Link the registry into apple-sleep, build the T6040 sleep wherever the SMC driver is built in, as before, and select MFD_SYSCON in that case. The T6040 documentation moves to APPLE_SOC_SLEEP. A kernel with APPLE_SOC_SLEEP and the SMC driver as a module still builds the T8140 sleep alone; with both built in it now also carries the T6040 sleep. The asahi config fragment no longer sets the removed symbol. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 51 ++++++++++++------------------ drivers/soc/apple/Makefile | 8 +++-- drivers/soc/apple/sleep-internal.h | 3 +- drivers/soc/apple/sleep-test.c | 2 +- drivers/soc/apple/sleep.c | 4 +-- include/linux/soc/apple/sleep.h | 2 +- 6 files changed, 31 insertions(+), 39 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 22b579e8f21b1..2f3ddf261acd7 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -92,17 +92,13 @@ config APPLE_CLUSTER_PWRGATE started and PMGR never powered on, such as the boot CPU, only clock-gates in WFI. -config APPLE_SLEEP_REGISTRY - bool - depends on ARM64 - config APPLE_SOC_SLEEP bool "Apple SoC system sleep at EL2" default APPLE_T8140_SLEEP depends on ARM64 && (ARCH_APPLE || COMPILE_TEST) depends on HOTPLUG_CPU && SUSPEND - select APPLE_SLEEP_REGISTRY select APPLE_TUNABLE + select MFD_SYSCON if MFD_MACSMC=y help Powers the secondary CPUs off and suspends the system to RAM on an Apple SoC whose kernel runs directly at EL2, with no monitor to ask. @@ -110,9 +106,25 @@ config APPLE_SOC_SLEEP /chosen/aurora,s2r-mailbox. Only SoCs whose sleep has been measured and tested are supported, - each by its match data: today T8140, T8132, and with - APPLE_T6040_SLEEP the T6040. On every other SoC the driver doesn't - bind, and s2idle stays the only system sleep. + each by its match data: today T8140, T8132, and with MFD_MACSMC + built in the T6040. On every other SoC the driver doesn't bind, and + s2idle stays the only system sleep. + + The T6040 (M4 Pro) part has its own sequences and refuses first, on + the boards whose device tree enables its sleep controller: its + suspend handler lists every blocker in the live state (EL2, the boot + object's return path, the CPUs that can park, the display power + states that have to be off before a sleep). Unless + t6040_sleep.allow_entry=1 is given and no blocker remains, a request + for suspend-to-RAM prints the blockers and enters nothing, and + suspend-to-idle stays the default sleep. With it the SoC is powered + down to suspend-to-RAM and comes back through the boot object; the + AON watchdog is the net over the whole cycle + (t6040_sleep.sleep_wd_secs at the power-down itself, + t6040_sleep.wd2_secs over a dry run). With t6040_sleep.dry_run=1 + every driver's firmware-driven sleep step that can be taken back + runs, the CPUs park and are held, and the sleep is refused at the + commit point and resumed warm. config APPLE_SOC_SLEEP_KUNIT_TEST bool "KUnit tests for the Apple SoC sleep match data" if !KUNIT_ALL_TESTS @@ -126,29 +138,6 @@ config APPLE_SOC_SLEEP_KUNIT_TEST If unsure, say N. -config APPLE_T6040_SLEEP - bool "Apple T6040 system sleep" - depends on APPLE_SOC_SLEEP - depends on MFD_MACSMC=y - select MFD_SYSCON - help - The T6040 (M4 Pro) part of the Apple SoC sleep driver, on the - boards whose device tree enables its sleep controller. The T6040 - has its own sequences and refuses first: its suspend handler lists - every blocker in the live state (EL2, the boot object's return - path, the CPUs that can park, the display power states that have to - be off before a sleep). Unless t6040_sleep.allow_entry=1 is given - and no blocker remains, a request for suspend-to-RAM prints the - blockers and enters nothing, and suspend-to-idle stays the default - sleep. With it the SoC is powered down to suspend-to-RAM and comes - back through the boot object; the AON watchdog is the net over the - whole cycle (t6040_sleep.sleep_wd_secs at the power-down itself, - t6040_sleep.wd2_secs over a dry run). - - With t6040_sleep.dry_run=1 every driver's firmware-driven sleep - step that can be taken back runs, the CPUs park and are held, and - the sleep is refused at the commit point and resumed warm. - config APPLE_RTKIT tristate "Apple RTKit co-processor IPC protocol" depends on APPLE_MAILBOX diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 3fd9381fa0a00..6f3f35604eb95 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -13,10 +13,12 @@ obj-$(CONFIG_APPLE_PMGR_MISC) += apple-pmgr-misc.o obj-$(CONFIG_APPLE_CLUSTER_PWRGATE) += apple-cluster-pwrgate.o -obj-$(CONFIG_APPLE_SLEEP_REGISTRY) += sleep-registry.o obj-$(CONFIG_APPLE_SOC_SLEEP) += apple-sleep.o -apple-sleep-y = sleep.o -apple-sleep-$(CONFIG_APPLE_T6040_SLEEP) += sleep-t6040.o +apple-sleep-y = sleep.o sleep-registry.o +# The T6040 sleep calls the SMC driver directly +ifeq ($(CONFIG_MFD_MACSMC),y) +apple-sleep-y += sleep-t6040.o +endif obj-$(CONFIG_APPLE_RTKIT) += apple-rtkit.o apple-rtkit-y = rtkit.o rtkit-crashlog.o rtkit-hib-handoff.o diff --git a/drivers/soc/apple/sleep-internal.h b/drivers/soc/apple/sleep-internal.h index 35afc3e81d8f5..c5286dbcb088e 100644 --- a/drivers/soc/apple/sleep-internal.h +++ b/drivers/soc/apple/sleep-internal.h @@ -283,7 +283,8 @@ struct apple_sleep_part { unsigned int registry_flags; }; -/* sleep-t6040.c */ +/* sleep-t6040.c, built where the SMC driver is built in */ +#define APPLE_SLEEP_T6040 IS_BUILTIN(CONFIG_MFD_MACSMC) extern const struct apple_sleep_part t6040_sleep_part; /* diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index 090d665d9d704..867c0e681009b 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -9,7 +9,7 @@ static const char *const apple_sleep_test_tested[] = { "apple,t8140-sleep", "apple,t8132-sleep", -#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +#if APPLE_SLEEP_T6040 "apple,t6040-sleep", #endif }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index a7f25c47e5bdc..15b844d046b42 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -4121,7 +4121,7 @@ static const struct apple_sleep_soc t8132_sleep_soc = { .nwake_names = ARRAY_SIZE(t8132_wake_names), .wake_last_word = GENMASK(20, 0), }; -#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +#if APPLE_SLEEP_T6040 /* T6040 (MacBook Pro J616s): its own sequences, in sleep-t6040.c. */ static const struct apple_sleep_soc t6040_sleep_soc = { .part = &t6040_sleep_part, @@ -4135,7 +4135,7 @@ static const struct apple_sleep_soc t6040_sleep_soc = { static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8140-sleep", .data = &t8140_sleep_soc }, { .compatible = "apple,t8132-sleep", .data = &t8132_sleep_soc }, -#if IS_ENABLED(CONFIG_APPLE_T6040_SLEEP) +#if APPLE_SLEEP_T6040 { .compatible = "apple,t6040-sleep", .data = &t6040_sleep_soc }, #endif { } diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 48ff16702b52e..ddd15c742e2de 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -25,7 +25,7 @@ struct device_node; #define APPLE_SLEEP_FIRST_GPU 1 #define APPLE_SLEEP_FIRST_ORDERS 2 -#if IS_ENABLED(CONFIG_APPLE_SLEEP_REGISTRY) +#if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) /* * A coprocessor that can't be stopped until a sleep is certain, and can't be * restarted without one. While one is registered and not ready, a suspend is From 9af13df95d598faa0f15e149ea1ad72cb7bf4717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:52:22 +0100 Subject: [PATCH 154/269] soc: apple: sleep: leave the SoC with a part of its own out of the layout and ops tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 sleep controller is always in the match table now, and it keeps its sequences in a part of its own: its SoC data carries no cluster, MCC or wake word counts and no operations table. The books and wake-name tests already pass over such an entry; the layout and operations tests did not and failed on it. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-test.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index 867c0e681009b..267f11297864b 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -120,6 +120,9 @@ static void apple_sleep_test_ops(struct kunit *test) const struct apple_sleep_soc *soc = id->data; const struct apple_sleep_ops *ops = soc->ops; + /* A SoC with a part of its own (T6040) brings its sequences there. */ + if (soc->part) + continue; KUNIT_ASSERT_NOT_NULL_MSG(test, ops, "%s has no operations", id->compatible); KUNIT_EXPECT_NOT_NULL(test, ops->deep_wfi_arm); KUNIT_EXPECT_NOT_NULL(test, ops->lower_io); @@ -181,6 +184,8 @@ static void apple_sleep_test_layout(struct kunit *test) for (id = apple_sleep_of_match; id->compatible[0]; id++) { const struct apple_sleep_soc *soc = id->data; + if (soc->part) + continue; KUNIT_EXPECT_GE(test, soc->ncluster_cpu, 1); KUNIT_EXPECT_LE(test, soc->ncluster_cpu, SLEEP_CPU_CLUSTERS_MAX); KUNIT_EXPECT_GT(test, soc->cpu_start_stride, 0); From 2ff613cfa29df0c99002fde76ec24c474d6b67c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:44:04 +0100 Subject: [PATCH 155/269] soc: apple: sleep-t6040: look the register blocks up by the shared names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The device tree names the T6040 sleep controller's cluster, power-gate, SPMI and wake blocks as the schema names them for the T8140's: cluster, pwrgate, spmi and wake. Map them under those names, and name them so in the probe message and the comments. The blocks, their order and every access to them are unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 6074a77fec6aa..f99597375884c 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -238,7 +238,7 @@ static const struct { #define PMC_BOOST 0x8 /* - * The PMU over SPMI (spmi0 = 0x509014000, spmi-a0 = 0x509188000), written as on the T8140. + * The PMU over SPMI (spmi = 0x509014000, spmi-a0 = 0x509188000), written as on the T8140. * Measured on the J616s: the boot stage is set to 0x50 by an extended write to * PMU_BOOT_STAGE, and NOTIFY_ENABLE is 0x2000001 on both controllers' +0x40. */ @@ -1967,7 +1967,7 @@ static void t6040_apcie_gp_lower(struct t6040_sleep *s) * calls it only when no blocker remains and allow_entry is set. In order: the stopped CPUs * released to their last WFI, the ATC complexes and the GP PCIe ports lowered, ANS's storage * link quiesced, WD0 off and the AON WD2 net armed, the PMU's boot stage 0x50 and - * NOTIFY_ENABLE over spmi0/spmi-a0, the steps of the PMGR quiesce (APSC, PLL relock, + * NOTIFY_ENABLE over spmi/spmi-a0, the steps of the PMGR quiesce (APSC, PLL relock, * TVMs, the AGX gate, the five complexes, the axi2af read-check, the clock quiesce, the * keep-alive bits), aura's mailbox armed with the resume entry, the fabrics and the ATC * ports readied for the sleep engine, the MasterMini S2R timer (0x508904000 := 1), the AIC @@ -3062,20 +3062,20 @@ static int t6040_sleep_probe(struct platform_device *pdev) ARRAY_SIZE(((struct t6040_sleep *)0)->acc_apsc)); s->cpu_start = t6040_sleep_map(&pdev->dev, "cpu-start"); s->wdt = t6040_sleep_map(&pdev->dev, "wdt"); - s->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster-ctl"); - s->pwrgate = t6040_sleep_map(&pdev->dev, "soc-pwrgate"); + s->cluster_ctl = t6040_sleep_map(&pdev->dev, "cluster"); + s->pwrgate = t6040_sleep_map(&pdev->dev, "pwrgate"); s->clkgen = t6040_sleep_map(&pdev->dev, "clkgen"); - s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi0"); + s->spmi0 = t6040_sleep_map(&pdev->dev, "spmi"); s->spmi_a0 = t6040_sleep_map(&pdev->dev, "spmi-a0"); s->mastermini = t6040_sleep_map(&pdev->dev, "mastermini"); s->soc_east = t6040_sleep_map_min(&pdev->dev, "soc-east", SOC_EAST_SIZE); s->soc_ps = t6040_sleep_map_min(&pdev->dev, "soc-ps", SOC_PS_SIZE); - s->aop_global = t6040_sleep_map(&pdev->dev, "aop-global"); + s->aop_global = t6040_sleep_map(&pdev->dev, "wake"); if (!s->cpu_start || !s->wdt || !s->cluster_ctl || !s->pwrgate || !s->clkgen || !s->spmi0 || !s->spmi_a0 || !s->mastermini || !s->soc_east || !s->soc_ps || !s->aop_global) return dev_err_probe(&pdev->dev, -ENODEV, - "a register block is missing or not mapped (cpu-start %d wdt %d cluster-ctl %d soc-pwrgate %d clkgen %d spmi0 %d spmi-a0 %d mastermini %d soc-east %d soc-ps %d aop-global %d)\n", + "a register block is missing or not mapped (cpu-start %d wdt %d cluster %d pwrgate %d clkgen %d spmi %d spmi-a0 %d mastermini %d soc-east %d soc-ps %d wake %d)\n", !!s->cpu_start, !!s->wdt, !!s->cluster_ctl, !!s->pwrgate, !!s->clkgen, !!s->spmi0, !!s->spmi_a0, !!s->mastermini, !!s->soc_east, !!s->soc_ps, !!s->aop_global); From 11aa7ccc67f6847c5581ee150e180b0ccaf3f2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:44:35 +0100 Subject: [PATCH 156/269] soc: apple: sleep-t6040: share the test for the GP host's system domain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The GP PCIe host's ports are stopped, and its power states lowered, only while its system domain is still on, and both places read the domain's power word and test the same field on their own. Put the test in one helper so the two cannot drift apart; each user still reads the word where it did before, and the lowering stays in its one function. The object code is identical to the one before. Signed-off-by: František Balázsy --- drivers/soc/apple/sleep-t6040.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index f99597375884c..27db0bc86b166 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -1919,12 +1919,18 @@ static void t6040_gp_port_stop(struct t6040_sleep *s, int p) writel(readl(port + APCIE_PORT_INTSTAT), port + APCIE_PORT_INTSTAT); } +/* Whether the GP host's system domain is still on; if not, there is nothing left to stop. */ +static __always_inline bool t6040_apcie_sys_gp_on(struct t6040_sleep *s) +{ + return FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP)); +} + /* The GP host's two ports, while the block's domains are all still on. */ static void t6040_gp_ports_stop(struct t6040_sleep *s) { int p; - if (!FIELD_GET(PS_ACTUAL, readl(s->soc_east + PS_APCIE_SYS_GP))) + if (!t6040_apcie_sys_gp_on(s)) return; for (p = 0; p < ARRAY_SIZE(s->gp_port); p++) t6040_gp_port_stop(s, p); @@ -1943,7 +1949,7 @@ static void t6040_apcie_gp_lower(struct t6040_sleep *s) sys = s->soc_east + PS_APCIE_SYS_GP; gp = s->soc_east + PS_APCIE_GP; - if (!FIELD_GET(PS_ACTUAL, readl(sys))) + if (!t6040_apcie_sys_gp_on(s)) return; val = readl(sys); From 8c022daacc0ea1608f6f38bc40dbee8cf8b4b7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:42:32 +0100 Subject: [PATCH 157/269] mfd: macsmc: tell drivers that an image is being loaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/mfd/macsmc.c part of "PM: hibernate: tell drivers that an image is being loaded", which goes with the sleep controller changes; the rest of that commit is with the hibernation core changes. A driver that must treat the kernel loading a hibernation image differently from a kernel that freezes for an image of its own has no way to ask: the freeze callbacks run the same in both. The SMC driver and the RTKit hand-off each registered a PM notifier that sets a flag on PM_RESTORE_PREPARE and clears it on PM_POST_RESTORE, and the hand-off copy is read by two more drivers. Keep the flag in the PM core, next to the restored flag, and have the two notifier helpers set it around the PM_RESTORE_PREPARE and PM_POST_RESTORE notifications, whether the load starts from the resume path or from the snapshot device. pm_hibernate_is_loading() reads it. It is true from before the first notifier runs (the copies became true only when their own notifier was reached) until PM_POST_RESTORE has been delivered, and it is cleared again when a notifier refuses the load, where the copies of the notifiers not reached never became true. It is saved in an image as false, like the notifier copies, so the restored kernel reads false. The SMC driver reads the core flag in its freeze callback and drops its notifier, which also removes the registration at probe. The RTKit hand-off keeps apple_rtkit_hib_restoring() for its callers and answers it from the core flag. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index fccd81ea517de..a4a0bfa0ed0db 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -606,27 +606,8 @@ static const struct apple_smc_wake_reason apple_smc_t8132_wake_reasons[] = { * it in (apple_smc_rtkit_shutdown()) and from which a wake starts a new * session. */ -static bool apple_smc_loading_image; static bool apple_smc_handed_over; -static int apple_smc_pm_notify(struct notifier_block *nb, unsigned long action, void *unused) -{ - if (action == PM_RESTORE_PREPARE) - apple_smc_loading_image = true; - else if (action == PM_POST_RESTORE) - apple_smc_loading_image = false; - return NOTIFY_DONE; -} - -static struct notifier_block apple_smc_pm_nb = { - .notifier_call = apple_smc_pm_notify, -}; - -static void apple_smc_pm_unregister(void *unused) -{ - unregister_pm_notifier(&apple_smc_pm_nb); -} - /* * The handshake with an SMC that was just woken, at probe and after a resume * from disk alike: its endpoint, the initialization and the key count. The @@ -698,16 +679,6 @@ static int apple_smc_probe(struct platform_device *pdev) apple_smc_t8132_wake_reasons : apple_smc_t8140_wake_reasons; smc->wktp = hw && hw->wktp; - /* First, so that a late probe still sees a resume start. */ - if (IS_ENABLED(CONFIG_HIBERNATION)) { - ret = register_pm_notifier(&apple_smc_pm_nb); - if (ret) - return ret; - ret = devm_add_action_or_reset(dev, apple_smc_pm_unregister, NULL); - if (ret) - return ret; - } - mutex_init(&smc->mutex); spin_lock_init(&smc->lock); smc->dev = &pdev->dev; @@ -795,7 +766,7 @@ static int apple_smc_freeze(struct device *dev) struct apple_smc *smc = dev_get_drvdata(dev); /* Only the image-loading kernel, right before it enters the image. */ - if (apple_smc_loading_image && apple_rtkit_is_running(smc->rtk)) { + if (pm_hibernate_is_loading() && apple_rtkit_is_running(smc->rtk)) { int ret; apple_smc_write_flag(smc, SMC_KEY(NTAP), false); From 56f39932f78b725828ac1d3bbce3d076239cf9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:45:46 +0100 Subject: [PATCH 158/269] soc: apple: let the platform refuse hibernation through a hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/Kconfig part of "PM: hibernate: let the platform refuse hibernation through a hook", which goes with the sleep controller changes; the rest of that commit is with the hibernation core changes. hibernation_available() asked of_machine_is_compatible("apple,arm-platform") and aura_hibernate_layout_fixed() in the generic hibernation code, and the log line carried the reason of one platform. Add hibernation_set_platform_check(): platform code registers a function that is true while hibernation must stay refused, with the reason that is logged at the end of the boot when it is. hibernation_available() asks it where it asked the two Apple conditions. The aura hibernation code registers aura_hibernate_blocked(), which is the same expression (Apple platform and no fixed layout), evaluated at the same moments, and the same reason text; the explanation of why the machines refuse moves with it. The check is registered from the init of that code on every boot of a kernel that has it, where the Apple condition used to be compiled into the core unconditionally. To keep that fail-closed, APPLE_AURA_HIBERNATE is no longer a prompt: it is always on with ARCH_APPLE and HIBERNATION, as the refusal belongs to it. Nothing registers before that init runs, which precedes the first possible hibernation (the resume check is a later initcall level and user space starts after all of them). Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index 2f3ddf261acd7..bdeea328ab47d 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -341,9 +341,8 @@ config APPLE_CLPC Say 'y' or 'm' here if you have an Apple SoC with a PMC. config APPLE_AURA_HIBERNATE - bool "Hibernation under an aura boot object" + def_bool y depends on ARCH_APPLE && HIBERNATION && OF_EARLY_FLATTREE - default y help Under aura, the boot loader and the firmware keep memory alive below Linux's allocations, and on Apple M4 Pro boots the boot @@ -351,7 +350,8 @@ config APPLE_AURA_HIBERNATE aura's fixed layout (/chosen/aura,hibernate-layout) this keeps every reserved range out of the hibernation image; without it, hibernation and resume are refused instead of overwriting the - running boot loader. + running boot loader. The refusal is part of the hibernation + support of these machines, so the option cannot be turned off. config APPLE_DISPLAY_GATE bool "Apple internal display enable gate" From beeff17bdd57128fa96bea60df883fc869bb37b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:53:18 +0100 Subject: [PATCH 159/269] soc: apple: sep: learn a cold resume from the PM core, not from the PMP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iBoot boots sepOS anew on every boot and the SEP has no run bit the AP can read, so its resume from disk asked the PMP: the PMP's restore counted the resumes that found the PMP halted, and the SEP's notifier, one priority below the PMP's, compared the count taken at registration and at each hibernation's prepare and end. That tied the SEP to the PMP driver (a Kconfig dependency, a count, an export and a call from the PMP's restore) and to the order of two notifiers. pm_hibernate_restored_cold() answers the same question in the one place that knows it: whether another boot loaded the image. The notifier asks it where it compared the counts. Not restored (aborted hibernation, wake of a hybrid sleep): warm, as before. Restored and loaded by another boot: the cold path, the session is dropped and the device probed again. Restored by the kernel that wrote the image (test_resume): warm; the PMP found itself running there too. No record: the session is dropped without a probe, which is what a missing PMP count did. On the J616s the boot that loads an image always restarts the PMP, so the two signals agree on a resume, a test resume and an aborted hibernation. They differ where a boot that loaded an image did not restart the firmware (a kexec-style resume), and for a machine without a bound PMP, which can now tell. The PMP's own restore is unchanged: it still reads the ASC run bit and only stops reporting a count. The host test now drives the end of a hibernation through the notifier for an aborted hibernation, a resume after another boot, a test resume and a missing record, with three mutations that each fail it. Signed-off-by: František Balázsy --- drivers/soc/apple/Kconfig | 2 -- drivers/soc/apple/pm_shim.c | 39 +++++++------------------ tools/asahi/sep-suspend-errors-tests.py | 31 +++++++++++++++++--- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index bdeea328ab47d..ef61ebcc97f61 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -221,8 +221,6 @@ config APPLE_SEP depends on TRUSTED_KEYS depends on CRYPTO depends on MFD_MACSMC || !MFD_MACSMC - # apple_pmp_cold_restarts(): a resume from disk on a cold boot. - depends on APPLE_PMP || !APPLE_PMP select CRYPTO_HMAC select CRYPTO_SHA256 select CRYPTO_AES diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index a0ef50450d6c1..23c371ec7a9d4 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -54,17 +53,6 @@ static bool (*sep_pm_told)(void); /* In the device's .suspend, the only caller that can leave the request to the sleep. */ static bool sep_pm_suspending; -/* - * Whether the boot loader restarted the SoC under this hibernation. iBoot - * boots sepOS anew on every boot, and the SEP has no run bit the AP can read, - * so the PMP's restore (priority 10, ahead of this notifier) is asked: it - * counts the resumes from disk that found the PMP halted. The count is taken - * at registration and again at each hibernation's prepare and end, so a - * prepare this notifier missed (registered during it) still compares with - * the last count. Negative: no bound PMP, cold or warm cannot be told. - */ -static int sep_pm_cold_base = -ENODEV; - /* The marker dt.rs leaves on the SEP node once the registration is sent. */ #define SEP_REGISTERED_PROP "apple,sep-shmem-registered-iova" @@ -110,7 +98,7 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, { int (*event)(int event) = READ_ONCE(sep_pm_event); struct device *dev = READ_ONCE(sep_pm_dev); - int base, now; + int cold; if (!event) return NOTIFY_DONE; @@ -126,7 +114,6 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, return NOTIFY_OK; case PM_HIBERNATION_PREPARE: sep_diag_sleep(true); - sep_pm_cold_base = apple_pmp_cold_restarts(); event(SEP_PM_HIBERNATE); return NOTIFY_OK; case PM_RESTORE_PREPARE: @@ -151,31 +138,30 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, sep_diag_sleep(false); /* * Ends an aborted hibernation, the wake of a hybrid sleep and a - * resume from the image alike. Only a resume from the image can - * have gone through a boot, and only the PMP knows whether it did - * (a test resume does not): a hybrid sleep's wake is never taken - * for a cold boot, even when the PMP did not come back from its - * suspend-to-RAM and the PMP's restore finds it halted. + * resume from the image alike. iBoot boots sepOS anew on every + * boot and the SEP has no run bit the AP can read, so only a + * resume from the image that another boot loaded can have gone + * through a boot; the PM core knows whether it did (a test + * resume does not). A hybrid sleep's wake is never taken for a + * cold boot. */ - base = sep_pm_cold_base; - now = apple_pmp_cold_restarts(); - sep_pm_cold_base = now; if (!pm_hibernate_is_restored()) { event(SEP_PM_HIBERNATE_WARM); return NOTIFY_OK; } - if (base < 0 || now < 0) { + cold = pm_hibernate_restored_cold(); + if (cold < 0) { /* * Cold or warm cannot be told. Sending to a freshly booted * enclave, or registering again with a live one, are both * worse than losing Touch ID until a reboot. */ if (dev) - dev_err(dev, "hibernation: no PMP count, cannot tell a cold boot; the SEP session is dropped until a reboot\n"); + dev_err(dev, "hibernation: no record of the boot that loaded the image, cannot tell a cold boot; the SEP session is dropped until a reboot\n"); event(SEP_PM_HIBERNATE_COLD); return NOTIFY_OK; } - if (now != base) { + if (cold) { if (event(SEP_PM_HIBERNATE_COLD) > 0 && dev) sep_pm_schedule_reprobe(dev); return NOTIFY_OK; @@ -189,8 +175,6 @@ static int sep_pm_notify(struct notifier_block *nb, unsigned long action, static struct notifier_block sep_pm_nb = { .notifier_call = sep_pm_notify, - /* Below the PMP's 10, so that its restore has counted a cold boot. */ - .priority = 0, }; /* The enclave's own domain is the first one its node lists. */ @@ -598,7 +582,6 @@ int sep_pm_register(void *ptr, int (*event)(int event), int (*sleep)(void), WRITE_ONCE(sep_pm_sleep, sleep); WRITE_ONCE(sep_pm_wake, wake); WRITE_ONCE(sep_pm_told, told); - sep_pm_cold_base = apple_pmp_cold_restarts(); ret = register_pm_notifier(&sep_pm_nb); if (ret) { WRITE_ONCE(sep_pm_event, NULL); diff --git a/tools/asahi/sep-suspend-errors-tests.py b/tools/asahi/sep-suspend-errors-tests.py index 6837789b5fe5f..23dc813080519 100644 --- a/tools/asahi/sep-suspend-errors-tests.py +++ b/tools/asahi/sep-suspend-errors-tests.py @@ -90,6 +90,10 @@ def replace_once(source, before, after): 'c->sent = true;\n\tif (pm_wakeup_pending()) return -EBUSY;'), 'held-before-refusal': ('sep_pm_notify', 'if (event(SEP_PM_RESTORE) > 0) {', 'sep_diag_sleep(true);\n\t\tif (event(SEP_PM_RESTORE) > 0) {'), + 'cold-as-warm': ('sep_pm_notify', 'if (cold) {', 'if (false) {'), + 'unrecorded-as-warm': ('sep_pm_notify', 'if (cold < 0) {', 'if (false) {'), + 'unrecorded-as-cold': ('sep_pm_notify', 'cold = pm_hibernate_restored_cold();', + 'cold = pm_hibernate_restored_cold() != 0;'), } C_PREFIX = r''' @@ -169,12 +173,12 @@ def replace_once(source, before, after): #define pr_err(...) ((void)0) static int (*sep_pm_event)(int event); static struct device *sep_pm_dev; -static int sep_pm_cold_base, cold_restarts, held, sleeps_ended, reprobes; +static int restored_cold, held, sleeps_ended, reprobes; static bool restored; static void sep_diag_sleep(bool entering) { if (entering) held = 1; else if (held) { held = 0; ++sleeps_ended; } } -static int apple_pmp_cold_restarts(void) { return cold_restarts; } +static int pm_hibernate_restored_cold(void) { return restored_cold; } static bool pm_hibernate_is_restored(void) { return restored; } static void sep_pm_schedule_reprobe(struct device *d) { ++reprobes; } static bool yes(void) { return true; } @@ -283,7 +287,7 @@ def replace_once(source, before, after): resource_bytes = 4; close_gate = 1; early_gate = 0; read_error_after = 1; queued = consumed = 0; message_in_lock = matched_read = false; now = 0; fatal_armed = false; fatal_message[0] = 0; sep_pm_told = yes; - sep_pm_event = NULL; sep_pm_dev = &device; sep_pm_cold_base = cold_restarts = 0; + sep_pm_event = NULL; sep_pm_dev = &device; restored_cold = 0; held = sleeps_ended = reprobes = 0; restored = false; } void parameter(int which, int value) { @@ -462,7 +466,9 @@ def replace_once(source, before, after): } static int attached, last_event; static int driver_event(int event) { - last_event = event; return event == SEP_PM_RESTORE ? attached : 0; + last_event = event; + // The driver reports a live session to drop for a cold boot, like the real one. + return event == SEP_PM_RESTORE ? attached : event == SEP_PM_HIBERNATE_COLD; } static void test_notifier(void) { struct notifier_block nb; @@ -478,6 +484,23 @@ def replace_once(source, before, after): assert(sep_pm_notify(&nb, PM_RESTORE_PREPARE, NULL) == NOTIFY_OK && held); assert(sep_pm_notify(&nb, PM_POST_RESTORE, NULL) == NOTIFY_OK); assert(last_event == SEP_PM_HIBERNATE_WARM && !held && sleeps_ended == 1); + // The end of a hibernation: an aborted one or a hybrid sleep's wake was never restored. + fixture(); sep_pm_event = driver_event; restored_cold = 1; + assert(sep_pm_notify(&nb, PM_HIBERNATION_PREPARE, NULL) == NOTIFY_OK && held); + assert(sep_pm_notify(&nb, PM_POST_HIBERNATION, NULL) == NOTIFY_OK); + assert(last_event == SEP_PM_HIBERNATE_WARM && !held && sleeps_ended == 1 && !reprobes); + // Restored after a boot that loaded the image: the enclave restarted, the device is probed again. + restored = true; restored_cold = 1; + assert(sep_pm_notify(&nb, PM_POST_HIBERNATION, NULL) == NOTIFY_OK); + assert(last_event == SEP_PM_HIBERNATE_COLD && reprobes == 1); + // Restored by the kernel that wrote the image (test_resume): the enclave never restarted. + restored_cold = 0; reprobes = 0; + assert(sep_pm_notify(&nb, PM_POST_HIBERNATION, NULL) == NOTIFY_OK); + assert(last_event == SEP_PM_HIBERNATE_WARM && !reprobes); + // No record of the loading kernel: the session is dropped, no probe is queued. + restored_cold = -ENODATA; + assert(sep_pm_notify(&nb, PM_POST_HIBERNATION, NULL) == NOTIFY_OK); + assert(last_event == SEP_PM_HIBERNATE_COLD && !reprobes); // A suspend holds and releases the same way. assert(sep_pm_notify(&nb, PM_SUSPEND_PREPARE, NULL) == NOTIFY_OK && held); assert(sep_pm_notify(&nb, PM_POST_SUSPEND, NULL) == NOTIFY_OK && !held && sleeps_ended == 2); From cd453531984618960fc42324d3fabbe05000776b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:58:52 +0100 Subject: [PATCH 160/269] soc: apple: drop the restore callbacks' checks for an abandoned hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An abandoned hibernation resumed its devices with PMSG_RESTORE, so every restore callback that must not act on a state it never lost began with a pm_hibernate_is_restored() check, and most forwarded to their thaw. The PM core now sends PMSG_RECOVER there, which reaches the thaw callbacks, so the check and the forward go from macsmc, the RTKit helper, apple-dart, the sn201202x Type-C controller, the SPMI, GPIO and NVMe restore paths, both PCIe restore callbacks, the DCP and AOP restores, the t6040 USB2 PHY's two and the RTC; cs42l84's restore passes that the codec lost its state. Each callback runs for a restored kernel exactly as before. For an abandoned hibernation the old path was already the thaw or nothing, and the thaw now runs on its own: macsmc, the RTKit helper, apple-dart, sn201202x, NVMe and cs42l84 forwarded to the same function (cs42l84's resume asks pm_resume_via_firmware(), which hibernation_snapshot() has cleared); PCIe's restore_noirq did for a bridge without hibernation tunnelling what its thaw_noirq does, and the same call with it; the AOP, t6040, RTC, GPIO and SPMI restores did nothing and have no thaw. DCP keeps its power-off-failed branch (PMSG_RESTORE after a failed platform power-off, which only a platform with hibernation ops reaches) and loses the abandoned one, whose only other effect was a log line. One path changes: macsmc's thaw, after the SMC was handed over and the image could not be entered, calls apple_smc_restore() to start a new session, and its own check sent that back to the thaw, which did nothing. It now starts the session as the comment says it should. The flag keeps its other users, which are not restore callbacks: the AIC syscore resume, the dockchannel receive interrupt, the SEP's PM notifier. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 4 ---- drivers/pinctrl/pinctrl-apple-gpio.c | 3 --- drivers/rtc/rtc-apple-pmu.c | 5 ++--- drivers/spmi/spmi-apple-controller.c | 3 +-- tools/asahi/pmu-rtc-tests.py | 18 ++++-------------- 5 files changed, 7 insertions(+), 26 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index a4a0bfa0ed0db..ac63a22d107f8 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -811,10 +811,6 @@ static int apple_smc_restore(struct device *dev) struct apple_smc *smc = dev_get_drvdata(dev); int ret; - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return apple_smc_thaw(dev); - /* * The boot that loaded the image restarted the SMC. The RTKit session * in the image belongs to a firmware instance that no longer runs, and diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 4379d123af0b3..3b686a0218940 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -850,9 +850,6 @@ static int apple_gpio_pinctrl_restore_early(struct device *dev) struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i, cfg, val, restored = 0; - /* Hibernation aborted before the image: the block kept its state. */ - if (!pm_hibernate_is_restored()) - return 0; /* Uncached: no word of the restored kernel to write back. */ if (!pctl->cached) return 0; diff --git a/drivers/rtc/rtc-apple-pmu.c b/drivers/rtc/rtc-apple-pmu.c index e4bf60b1b1b32..2e07c1a393371 100644 --- a/drivers/rtc/rtc-apple-pmu.c +++ b/drivers/rtc/rtc-apple-pmu.c @@ -471,14 +471,13 @@ static int apple_pmu_rtc_poweroff(struct device *dev) * machine off, its event acknowledged by one of them. The RTC core still * counts the alarm as set. Have it go through its timers: an alarm that is due * is reported, a later one is written to the PMU again, and with none set the - * PMU's alarm is stopped. Not after a hibernation given up before its image. + * PMU's alarm is stopped. */ static int apple_pmu_rtc_restore(struct device *dev) { struct apple_pmu_rtc *rtc = dev_get_drvdata(dev); - if (pm_hibernate_is_restored()) - rtc_update_irq(rtc->clock, 1, RTC_IRQF | RTC_AF); + rtc_update_irq(rtc->clock, 1, RTC_IRQF | RTC_AF); return 0; } diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c index dee6e68380438..9042ebcba7a8f 100644 --- a/drivers/spmi/spmi-apple-controller.c +++ b/drivers/spmi/spmi-apple-controller.c @@ -755,8 +755,7 @@ static int apple_spmi_restore_early(struct device *dev) { struct apple_spmi *spmi = dev_get_drvdata(dev); - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored() || spmi->irq < 0) + if (spmi->irq < 0) return 0; /* Commands can run by now: none may have its reply acknowledged away. */ diff --git a/tools/asahi/pmu-rtc-tests.py b/tools/asahi/pmu-rtc-tests.py index c2037693c3d05..02b271bc28e12 100644 --- a/tools/asahi/pmu-rtc-tests.py +++ b/tools/asahi/pmu-rtc-tests.py @@ -28,10 +28,8 @@ 'baku-limit': ('.max_delay = U32_MAX,', '.max_delay = APPLE_PMU_RTC_MAX_DELAY,'), 'set-no-round': ('(counter + BIT(APPLE_PMU_RTC_HZ_SHIFT - 1))', '(counter)'), 'set-negative': ('if (offset < 0 || offset', 'if (offset'), - 'restore-unrestored': ('if (pm_hibernate_is_restored())', - 'if (pm_hibernate_is_restored() || true)'), - 'restore-nothing': ('if (pm_hibernate_is_restored())', - 'if (pm_hibernate_is_restored() && false)'), + 'restore-nothing': ('rtc_update_irq(rtc->clock, 1, RTC_IRQF | RTC_AF);', + 'if (false) rtc_update_irq(rtc->clock, 1, RTC_IRQF | RTC_AF);'), } PRELUDE = r''' @@ -81,9 +79,8 @@ int ops, fail; }; static int disabled, pm_suspend_target_state = PM_SUSPEND_TO_IDLE; -static bool firmware_suspend, restored; +static bool firmware_suspend; static bool pm_suspend_via_firmware(void) { return firmware_suspend; } -static bool pm_hibernate_is_restored(void) { return restored; } static inline __attribute__((unused)) s64 sign_extend64(u64 v, int bit) { return (s64)(v << (63 - bit)) >> (63 - bit); } static u64 get_unaligned_le64(const void *p) { u64 v; memcpy(&v, p, 8); return v; } static void mutex_lock(struct mutex *m) { (void)m; } @@ -349,16 +346,9 @@ map.ops = 0; map.fail = 1; assert(!apple_pmu_rtc_poweroff(&dev)); pm_suspend_target_state = PM_SUSPEND_TO_IDLE; - /* - * After a resume from disk the RTC core goes through its alarm again, without - * a PMU access of the driver's own; a hibernation given up before its image - * leaves the alarm alone. - */ + /* After a resume from disk the RTC core goes through its alarm again, without a PMU access of the driver's own. */ reset(v); - assert(!apple_pmu_rtc_restore(&dev) && clock_model.events == 0 && map.ops == 0); - restored = true; assert(!apple_pmu_rtc_restore(&dev) && clock_model.events == 1 && map.ops == 0); - restored = false; } int main(void) { run_variant(BAKU); run_variant(ABBEY); run_variant(STOWE); run_variant(MAVERICK); From db9d7425e8fdce90ee7e42848203ee2216fd2185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 18:38:28 +0100 Subject: [PATCH 161/269] mfd: macsmc: own the pmVC request for the VDD_CIO rail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 sleep controller wrote the SMC key pmVC itself, with the key, the value 0x101 (the J616s enable mask | 0x100) and the failure text all living in sleep-t6040.c. The SMC key space belongs to the SMC driver, so move that knowledge here: apple_smc_request_pmvc() performs the same u32 write of 0x101 to pmVC and warns with the same text when it fails. include/linux/mfd/macsmc.h declares it, with a -ENODEV stub when the SMC driver is not reachable. The moment of the write is unchanged. It is deliberately not made a suspend callback of the SMC driver: that would run relative to the USB-C controller's prepare step, which hands the port controller to the S3 state, in a device-list order rather than the order the sleep controller has today. sleep-t6040 still looks the SMC up by its compatible as before and calls the helper from the same point of its .begin, after the AOP island save and before the DCS power setup, with the same warning when no SMC is there (now logged without the value). The write reaches the SMC with the same key, size and value. Hardware test: one suspend to RAM with a USB-C device attached and powered; the log shows no "pmVC:" warning, the ATC port hand-over completes, and the device is still present after the wake. Signed-off-by: František Balázsy --- drivers/mfd/macsmc.c | 30 ++++++++++++++++++++++++++++++ drivers/soc/apple/sleep-t6040.c | 14 +++++--------- include/linux/mfd/macsmc.h | 12 ++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index ac63a22d107f8..c319d92ac8e1a 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -936,6 +936,36 @@ static void apple_smc_write_mbse(struct apple_smc *smc, smc_key value) dev_err(smc->dev, "Failed to issue MBSE = %p4ch: %d\n", &value, ret); } +/* + * SMC key pmVC, the request for the VDD_CIO rail: the enable mask | 0x100 + * (0x101 on the J616s), written while an ATC port's always-on side is powered, also at sleep. Linux + * never asks for the rail, which is power-gated otherwise (ADT vdd-cio-rail-pg), and a rail + * the SMC drops during the sleep transition races the sleep engine's hand-over of the ATC + * ports. + */ +#define SMC_PMVC_REQUEST 0x101 + +/** + * apple_smc_request_pmvc() - ask the SMC to keep the VDD_CIO rail up + * @smc: the SMC + * + * Writes pmVC at the moment the caller picks; it does not hook the SMC's own suspend + * callbacks, so a sleep controller that has to issue it ahead of the device suspend + * phase keeps that order. + * + * Return: 0 on success, a negative errno otherwise. + */ +int apple_smc_request_pmvc(struct apple_smc *smc) +{ + int ret; + + ret = apple_smc_write_u32(smc, __SMC_KEY('p', 'm', 'V', 'C'), SMC_PMVC_REQUEST); + if (ret < 0) + dev_warn(smc->dev, "pmVC: write %#x failed: %d\n", SMC_PMVC_REQUEST, ret); + return ret; +} +EXPORT_SYMBOL(apple_smc_request_pmvc); + static void apple_smc_sleep_warn(struct apple_smc *smc) { apple_smc_write_mbse(smc, SMC_KEY(slpw)); diff --git a/drivers/soc/apple/sleep-t6040.c b/drivers/soc/apple/sleep-t6040.c index 27db0bc86b166..0efef072c7362 100644 --- a/drivers/soc/apple/sleep-t6040.c +++ b/drivers/soc/apple/sleep-t6040.c @@ -2226,13 +2226,9 @@ static int t6040_sleep_valid(suspend_state_t state) } /* - * SMC key pmVC, the request for the VDD_CIO rail: the platform writes its enable mask | 0x100 - * (0x101 on the J616s) while an ATC port's always-on side is powered, also at sleep. Linux - * never asks for the rail, which is power-gated otherwise (ADT vdd-cio-rail-pg), and a rail - * the SMC drops during the sleep transition races the sleep engine's hand-over of the ATC - * ports. Written in each .begin. + * The VDD_CIO rail request (SMC key pmVC) is written in each .begin; its key and value + * belong to the SMC driver (apple_smc_request_pmvc()). */ -#define T6040_PMVC_REQUEST 0x101 /* The SMC node is apple,t6041-smc + apple,t8103-smc here (macsmc's match list). */ static const struct of_device_id t6040_smc_match[] = { @@ -2253,11 +2249,11 @@ static void t6040_sleep_pmvc(struct t6040_sleep *s) of_node_put(np); smc = pdev ? platform_get_drvdata(pdev) : NULL; if (smc) - ret = apple_smc_write_u32(smc, SMC_KEY(pmVC), T6040_PMVC_REQUEST); + ret = apple_smc_request_pmvc(smc); if (pdev) put_device(&pdev->dev); - if (ret < 0) - dev_warn(s->dev, "pmVC: write %#x failed: %d\n", T6040_PMVC_REQUEST, ret); + if (!smc) + dev_warn(s->dev, "pmVC: no SMC to write to: %d\n", ret); } /* diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index b404d39429ec4..da51c9be40efb 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -305,6 +305,18 @@ static inline int macsmc_hwmon_fans_request(struct device *dev, unsigned int per } #endif +/* + * Request the VDD_CIO rail (SMC key pmVC) for a system sleep: see drivers/mfd/macsmc.c. + */ +#if IS_REACHABLE(CONFIG_MFD_MACSMC) +int apple_smc_request_pmvc(struct apple_smc *smc); +#else +static inline int apple_smc_request_pmvc(struct apple_smc *smc) +{ + return -ENODEV; +} +#endif + static inline int apple_smc_read_flag(struct apple_smc *smc, smc_key key, bool *flag) { u8 val; From 7fbe928397820257f456e9f024691e70c988bf16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Sat, 14 Nov 2026 01:16:20 +0100 Subject: [PATCH 162/269] pmdomain: apple: pmc: put the voter enables back after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PMC's early resume writes the agent enables and the floors back from the driver's books, but only when pm_resume_via_firmware() is set, which the S2R path does. The same callback is the restore_early callback, and a kernel restored from a hibernation image never sets it, so the restore skipped it. That kernel runs after a boot loader that started from a reset PMC, which leaves every agent disabled on every rail: the floors are still written, but none is honoured. On the J616s (T6040) after a resume from disk the PMC reads 0 in every rail enable word and "honoured" 0 for the PMP's and the performance controller's DCS 5 / fabric 2 floors, so the memory clock stays at its lowest state until the next boot. Memory-bound GPU work is the visible part: token generation of a 4B language model ran at 36 instead of 55 tokens per second, while compute-bound prefill was unchanged. Split the restore out of the resume callback and run it from a restore_early callback of its own without the check. The S2R resume path is unchanged. A KUnit case clears the enable words after an agent enable and checks that the restore writes them back. Hardware test on the J616s, one resume from disk from the desktop: the DCS and SOC enable words read 0xb, both floors are honoured, a CPU memory copy runs at 123 GB/s (31 GB/s without the fix) and the same model decodes at 58.6 tokens per second. Signed-off-by: František Balázsy --- drivers/pmdomain/apple/pmc-test.c | 18 +++++++++++++ drivers/pmdomain/apple/pmc.c | 43 ++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/drivers/pmdomain/apple/pmc-test.c b/drivers/pmdomain/apple/pmc-test.c index 4e7f19d47317a..5e0446c8fe21b 100644 --- a/drivers/pmdomain/apple/pmc-test.c +++ b/drivers/pmdomain/apple/pmc-test.c @@ -76,6 +76,23 @@ static void apple_pmc_test_agent_rails(struct kunit *test) rail < 2 ? BIT(1) : 0); } +/* After a resume from disk the enables are written again from the books. */ +static void apple_pmc_test_restore_after_reset(struct kunit *test) +{ + struct apple_pmc *pmc = apple_pmc_test_alloc(test); + unsigned int rail; + + pmc->hw = &apple_pmc_hw_t6040; + KUNIT_ASSERT_EQ(test, apple_pmc_agent_enable(pmc, 1, true), 0); + for (rail = 0; rail < pmc->hw->rails; rail++) + writel(0, pmc->rails + pmc->hw->rail_enable + 4 * rail); + + KUNIT_ASSERT_EQ(test, apple_pmc_restore_state(pmc), 0); + for (rail = 0; rail < pmc->hw->rails; rail++) + KUNIT_EXPECT_EQ(test, apple_pmc_enable_read(pmc, rail), + rail < 2 ? BIT(1) : 0); +} + /* Lifting a DCS limit gives the AP's agent back its own vote, not 0. */ static void apple_pmc_test_dcs_limit_restores_ap_vote(struct kunit *test) { @@ -550,6 +567,7 @@ static struct kunit_case apple_pmc_test_cases[] = { KUNIT_CASE(apple_pmc_test_pending_floor), KUNIT_CASE(apple_pmc_test_agent_enable), KUNIT_CASE(apple_pmc_test_agent_rails), + KUNIT_CASE(apple_pmc_test_restore_after_reset), KUNIT_CASE(apple_pmc_test_dcs_limit_restores_ap_vote), KUNIT_CASE(apple_pmc_test_floor_aggregate), KUNIT_CASE(apple_pmc_test_floor_pending_timeout), diff --git a/drivers/pmdomain/apple/pmc.c b/drivers/pmdomain/apple/pmc.c index 586e3f675db9e..2341ff1d0c9fa 100644 --- a/drivers/pmdomain/apple/pmc.c +++ b/drivers/pmdomain/apple/pmc.c @@ -1534,16 +1534,12 @@ static const struct apple_pmc_hw apple_pmc_hw_t6050 = { * A system sleep that reached the hardware may have taken the enables and floors * with it. Put back what the driver's books say: these are no-ops if they stayed. */ -static int apple_pmc_resume_early(struct device *dev) +static int apple_pmc_restore_state(struct apple_pmc *pmc) { - struct apple_pmc *pmc = dev_get_drvdata(dev); unsigned long flags; unsigned int rail; int ret = 0, err; - if (!pm_resume_via_firmware() || !pmc) - return 0; - raw_spin_lock_irqsave(&pmc->lock, flags); for (rail = 0; rail < pmc->hw->rails; rail++) { err = apple_pmc_enable_write(pmc, rail); @@ -1554,14 +1550,49 @@ static int apple_pmc_resume_early(struct device *dev) } } raw_spin_unlock_irqrestore(&pmc->lock, flags); + + return ret; +} + +static int apple_pmc_resume_early(struct device *dev) +{ + struct apple_pmc *pmc = dev_get_drvdata(dev); + int ret; + + if (!pm_resume_via_firmware() || !pmc) + return 0; + + ret = apple_pmc_restore_state(pmc); if (ret) dev_err(dev, "state not restored after sleep: %d\n", ret); return 0; } +/* + * The kernel restored from a hibernation image runs after a boot loader that + * started from a reset PMC: every agent is off on every rail again, so the + * floors of the agents this driver enabled (the CPU performance controller's + * DCS and fabric votes among them) are no longer honoured. Restore always. + */ +static int apple_pmc_restore_early(struct device *dev) +{ + struct apple_pmc *pmc = dev_get_drvdata(dev); + int ret; + + if (!pmc) + return 0; + + ret = apple_pmc_restore_state(pmc); + if (ret) + dev_err(dev, "state not restored after resume from disk: %d\n", ret); + + return 0; +} + static const struct dev_pm_ops apple_pmc_pm_ops = { - LATE_SYSTEM_SLEEP_PM_OPS(NULL, apple_pmc_resume_early) + .resume_early = pm_sleep_ptr(apple_pmc_resume_early), + .restore_early = pm_sleep_ptr(apple_pmc_restore_early), }; static const struct of_device_id apple_pmc_of_match[] = { From 852b40315e0da10c958d9b8f9c99dd5b592bcc48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:03 +0100 Subject: [PATCH 163/269] soc: apple: PMP, AOP, SEP, store and DockChannel across hibernation and S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restore or re-adopt the platform coprocessors after a power loss: PMP report export, AOP with its firmware-driven sleep (run bit taken over cold when halted), the SEP and its enclave store, DockChannel. Signed-off-by: František Balázsy --- drivers/soc/apple/aop.rs | 583 ++++++++++++++++++++++++--- drivers/soc/apple/dockchannel.c | 179 +++++++- drivers/soc/apple/pm.rs | 198 ++++++++- drivers/soc/apple/pmp-export.c | 92 ++++- drivers/soc/apple/pmp.rs | 115 +++++- drivers/soc/apple/sep.rs | 116 +++++- include/linux/soc/apple/pmp-report.h | 6 + include/linux/soc/apple/pmp.h | 15 + tools/asahi/display26-tests.py | 28 +- 9 files changed, 1216 insertions(+), 116 deletions(-) diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 95c9c8bcdea70..bf47a755d2ef0 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -11,7 +11,8 @@ use core::{ marker::Unpin, mem, ptr::{self, NonNull}, - slice, // + slice, + sync::atomic::{AtomicI32, AtomicU64, Ordering}, // }; use kernel::{ @@ -1242,6 +1243,33 @@ const RETIRED_DRIVER_OVERRIDE: &CStr = c_str!("apple-aop-retired"); /// before a reboot. static RETIRED: Atomic = Atomic::new(false); +/// Set by unbind when the ASC CPU run bit reads 0: the co-processor was +/// halted underneath this instance, as the boot loader leaves it after a +/// resume from disk (a cold boot; the restored kernel still holds the session +/// of the instance that probed before the image). A halted CPU neither +/// answers nor DMAs, so the teardown sends nothing and frees everything, and +/// the next probe starts the firmware the boot loader staged, as at boot. +static HALTED_AT_UNBIND: Atomic = Atomic::new(false); + +/// Set by unbind with [`HALTED_AT_UNBIND`], cleared by the next successful +/// probe: the next probe starts firmware staged on a boot that is not the one +/// this kernel was started on, so its DRAM segments are taken from that boot +/// (see [`AopData::remap_fw_regions_cold`]). +static COLD_REPROBE: Atomic = Atomic::new(false); + +/// Set with [`COLD_REPROBE`] when [`RETIRED`] was already set at that halted +/// unbind: the failed shutdown predates the restaging, so the cold probe may +/// bring the co-processor up again. +static RETIRED_BEFORE_HALT: Atomic = Atomic::new(false); + +/// The firmware regions mapped in the default domain now (phys, IOVA, +/// size): -1 until the first probe takes them from the DT; a cold probe +/// replaces them with the resuming boot's. +const FW_SET_MAX: usize = 8; +static FW_CUR_LEN: AtomicI32 = AtomicI32::new(-1); +static FW_CUR: [[AtomicU64; 3]; FW_SET_MAX] = + [const { [const { AtomicU64::new(0) }; 3] }; FW_SET_MAX]; + impl ChildDevice { /// Keeps the child from binding to any driver again; it stays registered. fn retire(&self) { @@ -1976,26 +2004,9 @@ impl AopData { Ok((phys, iova, size)) } - /// Checks the firmware segments the AOP runs from DRAM (T6040). The - /// bootloader reserves them and lists them in `memory-region`, and each - /// one's `iommu-addresses` gives the IOVA the firmware uses for it. The - /// IOMMU core maps them in the default domain when the device is added - /// (one to one when the IOVA is the physical address, translated - /// otherwise), so they are only checked here. Every page has to be mapped - /// where the DT says, the external code (`__ETEXT`) at `ec0p` and any - /// other segment (`__OS_LOG`) at its physical address. - fn check_fw_regions(dev: &device::Device, ec0p: u64) -> Result<()> { - // SAFETY: `dev` is a live device being probed. - let np = unsafe { (*dev.as_raw()).of_node }; - if np.is_null() { - return Err(ENODEV); - } - // SAFETY: `np` is the device's node, valid while it is bound. - let count = unsafe { bindings::of_reserved_mem_region_count(np) }; - if count <= 0 { - dev_err!(dev, "No firmware memory-region from the bootloader"); - return Err(ENOENT); - } + /// The device's translated IOMMU domain, which the firmware segments are + /// mapped in. + fn fw_domain(dev: &device::Device) -> Result<*mut bindings::iommu_domain> { // SAFETY: `dev` is a live device being probed. let domain = unsafe { bindings::iommu_get_domain_for_dev(dev.as_raw()) }; let ty = if domain.is_null() { @@ -2008,59 +2019,331 @@ impl AopData { dev_err!(dev, "No translated IOMMU domain for the firmware"); return Err(ENODEV); } - let mut etext = false; - for idx in 0..count { - let (phys, iova, size) = Self::fw_region(np, idx).inspect_err(|e| { - dev_err!(dev, "Invalid firmware memory-region {} ({:?})", idx, e); - })?; - if phys % FW_REGION_PAGE != 0 - || size % FW_REGION_PAGE != 0 - || size > FW_REGION_MAX_SIZE - || iova % FW_REGION_PAGE != 0 - || iova.checked_add(size).is_none() - || phys.checked_add(size).is_none() - { - dev_err!(dev, "Invalid firmware memory-region {}", idx); + Ok(domain) + } + + /// The device's node and its number of firmware memory-regions. + fn fw_node(dev: &device::Device) -> Result<(*mut bindings::device_node, c_int)> { + // SAFETY: `dev` is a live device being probed. + let np = unsafe { (*dev.as_raw()).of_node }; + if np.is_null() { + return Err(ENODEV); + } + // SAFETY: `np` is the device's node, valid while it is bound. + let count = unsafe { bindings::of_reserved_mem_region_count(np) }; + if count <= 0 { + dev_err!(dev, "No firmware memory-region from the bootloader"); + return Err(ENOENT); + } + Ok((np, count)) + } + + /// The layout rules of one firmware memory-region: 16K pages, at most + /// [`FW_REGION_MAX_SIZE`], the IOVA either `ec0p` (at most one region, + /// counted in `etext`) or the physical address. + fn check_fw_region_layout( + dev: &device::Device, + ec0p: u64, + idx: c_int, + (phys, iova, size): (u64, u64, u64), + etext: &mut bool, + ) -> Result<()> { + if phys % FW_REGION_PAGE != 0 + || size % FW_REGION_PAGE != 0 + || size > FW_REGION_MAX_SIZE + || iova % FW_REGION_PAGE != 0 + || iova.checked_add(size).is_none() + || phys.checked_add(size).is_none() + { + dev_err!(dev, "Invalid firmware memory-region {}", idx); + return Err(EINVAL); + } + if iova == ec0p { + if *etext { + dev_err!(dev, "Two firmware memory-regions at EC0p {:#x}", ec0p); return Err(EINVAL); } - if iova == ec0p { - if etext { - dev_err!(dev, "Two firmware memory-regions at EC0p {:#x}", ec0p); - return Err(EINVAL); - } - etext = true; - } else if iova != phys { + *etext = true; + } else if iova != phys { + dev_err!( + dev, + "Firmware memory-region {} at IOVA {:#x}, neither EC0p {:#x} nor its physical address", + idx, + iova, + ec0p + ); + return Err(EINVAL); + } + Ok(()) + } + + /// Every page of a firmware memory-region has to be mapped at its IOVA. + fn check_fw_region_mapped( + dev: &device::Device, + domain: *mut bindings::iommu_domain, + idx: c_int, + (phys, iova, size): (u64, u64, u64), + ) -> Result<()> { + let mut off = 0; + while off < size { + // SAFETY: `domain` is the device's domain, live while it is + // bound. + let back = unsafe { bindings::iommu_iova_to_phys(domain, iova + off) }; + if back != phys + off { dev_err!( dev, - "Firmware memory-region {} at IOVA {:#x}, neither EC0p {:#x} nor its physical address", + "Firmware memory-region {} is not mapped at IOVA {:#x} (region phys {:#x} iova {:#x} size {:#x}, domain gives {:#x})", idx, + iova + off, + phys, iova, - ec0p + size, + back ); - return Err(EINVAL); + return Err(EIO); + } + off += FW_REGION_PAGE; + } + Ok(()) + } + + /// The firmware regions the default domain maps now: the DT's until a + /// resume from disk replaced them with those of the boot that resumed. + fn fw_set_load(dev: &device::Device) -> Result> { + let mut set = KVec::new(); + let len = FW_CUR_LEN.load(Ordering::Acquire); + if len >= 0 { + for r in FW_CUR.iter().take(len as usize) { + set.push( + ( + r[0].load(Ordering::Relaxed), + r[1].load(Ordering::Relaxed), + r[2].load(Ordering::Relaxed), + ), + GFP_KERNEL, + )?; + } + return Ok(set); + } + let (np, count) = Self::fw_node(dev)?; + for idx in 0..count { + let region = Self::fw_region(np, idx).inspect_err(|e| { + dev_err!(dev, "Invalid firmware memory-region {} ({:?})", idx, e); + })?; + set.push(region, GFP_KERNEL)?; + } + Ok(set) + } + + fn fw_set_store(set: &[(u64, u64, u64)]) -> Result<()> { + if set.len() > FW_SET_MAX { + return Err(E2BIG); + } + for (slot, r) in FW_CUR.iter().zip(set.iter()) { + slot[0].store(r.0, Ordering::Relaxed); + slot[1].store(r.1, Ordering::Relaxed); + slot[2].store(r.2, Ordering::Relaxed); + } + FW_CUR_LEN.store(set.len() as i32, Ordering::Release); + Ok(()) + } + + /// The layout rules and the mappings of a whole set: every region valid, + /// exactly one at `ec0p`, every page mapped. + fn check_fw_set( + dev: &device::Device, + domain: *mut bindings::iommu_domain, + ec0p: u64, + set: &[(u64, u64, u64)], + ) -> Result<()> { + let mut etext = false; + for (idx, r) in set.iter().enumerate() { + Self::check_fw_region_layout(dev, ec0p, idx as c_int, *r, &mut etext)?; + Self::check_fw_region_mapped(dev, domain, idx as c_int, *r)?; + } + if !etext { + dev_err!(dev, "No firmware memory-region at EC0p {:#x}", ec0p); + return Err(ENOENT); + } + Ok(()) + } + + /// Checks the firmware segments the AOP runs from DRAM (T6040). The + /// bootloader reserves them and lists them in `memory-region`, and each + /// one's `iommu-addresses` gives the IOVA the firmware uses for it. The + /// IOMMU core maps them in the default domain when the device is added + /// (one to one when the IOVA is the physical address, translated + /// otherwise), so they are only checked here. Every page has to be mapped + /// where the DT says, the external code (`__ETEXT`) at `ec0p` and any + /// other segment (`__OS_LOG`) at its physical address. After a resume + /// from disk the set checked is the one [`Self::remap_fw_regions_cold`] + /// mapped. + fn check_fw_regions(dev: &device::Device, ec0p: u64) -> Result<()> { + let set = Self::fw_set_load(dev)?; + let domain = Self::fw_domain(dev)?; + Self::check_fw_set(dev, domain, ec0p, &set)?; + Self::fw_set_store(&set) + } + + /// The firmware segments after a resume from disk (T6040 under aura). + /// + /// The resume is a cold boot: the boot firmware staged the AOP firmware anew and + /// placed some of its DRAM segments elsewhere than on the boot the + /// current mappings come from (the `__OS_LOG` moves on every boot, + /// `__ETEXT` has stayed in place so far). This boot's regions come from + /// the device tree aura handed over this time, at its fixed address; the + /// previous set's mappings that this boot does not have are removed, this + /// boot's are mapped (IOVA as the device tree says), checked as at boot, + /// and become the current set. Nothing is copied: the old physical ranges + /// may hold other firmware on this boot. Without aura's device tree slot + /// the current set is only checked, as before. + #[cfg(CONFIG_APPLE_AURA_HIBERNATE)] + fn remap_fw_regions_cold(dev: &device::Device, ec0p: u64) -> Result<()> { + let (np, _) = Self::fw_node(dev)?; + let prev = Self::fw_set_load(dev)?; + let domain = Self::fw_domain(dev)?; + let mut raw = [bindings::aura_fw_region { + phys: 0, + iova: 0, + size: 0, + }; FW_SET_MAX]; + // SAFETY: `np` is the device's node and `raw` has room for + // `FW_SET_MAX` entries. + let n = unsafe { + bindings::aura_hibernate_boot_fw_regions(np, raw.as_mut_ptr(), FW_SET_MAX as u32) + }; + if n == -(bindings::ENODEV as c_int) { + dev_warn!( + dev, + "resume from disk: no device tree of this boot from aura; checking the current firmware regions" + ); + return Self::check_fw_regions(dev, ec0p); + } + if n < 0 { + dev_err!( + dev, + "This boot's firmware memory-regions are not available ({}); the firmware cannot be mapped", + n + ); + return Err(Error::from_errno(n)); + } + let boot: KVec<(u64, u64, u64)> = { + let mut v = KVec::new(); + for r in &raw[..n as usize] { + v.push((r.phys, r.iova, r.size), GFP_KERNEL)?; + } + v + }; + let mut etext = false; + for (idx, r) in boot.iter().enumerate() { + Self::check_fw_region_layout(dev, ec0p, idx as c_int, *r, &mut etext)?; + } + if !etext { + dev_err!(dev, "No firmware memory-region at EC0p {:#x} on this boot", ec0p); + return Err(ENOENT); + } + // Mapped like the IOMMU core maps a direct region of this device. + // SAFETY: `np` is a valid node. + let coherent = unsafe { bindings::of_dma_is_coherent(np) }; + let mut prot = (bindings::IOMMU_READ | bindings::IOMMU_WRITE) as c_int; + if coherent { + prot |= bindings::IOMMU_CACHE as c_int; + } + + // The previous set's regions that this boot moved: unmap only pages + // that still map the old physical range, never anything else at that + // IOVA. Repeating it after a partial failure is harmless. + for (idx, &(phys, iova, size)) in prev.iter().enumerate() { + if boot.iter().any(|r| *r == (phys, iova, size)) { + continue; } let mut off = 0; + let mut unmapped = 0; while off < size { // SAFETY: `domain` is the device's domain, live while it is // bound. let back = unsafe { bindings::iommu_iova_to_phys(domain, iova + off) }; - if back != phys + off { - dev_err!( - dev, - "Firmware memory-region {} is not mapped at IOVA {:#x}", - idx, - iova + off - ); - return Err(EIO); + if back == phys + off { + // SAFETY: As above; the page was mapped for this region. + let done = unsafe { + bindings::iommu_unmap(domain, (iova + off) as usize, FW_REGION_PAGE as usize) + }; + if done != FW_REGION_PAGE as usize { + dev_err!(dev, "Could not unmap IOVA {:#x} of the old boot", iova + off); + return Err(EIO); + } + unmapped += 1; } off += FW_REGION_PAGE; } + dev_info!( + dev, + "resume from disk: old firmware region {} [{:#x}, +{:#x}) at IOVA {:#x} dropped ({} pages unmapped)", + idx, + phys, + size, + iova, + unmapped + ); } - if !etext { - dev_err!(dev, "No firmware memory-region at EC0p {:#x}", ec0p); - return Err(ENOENT); + + // This boot's regions: map what is missing, refuse what is taken. + for (idx, &(phys, iova, size)) in boot.iter().enumerate() { + let mut off = 0; + let mut mapped = 0; + while off < size { + // SAFETY: `domain` is the device's domain, live while it is + // bound. + let back = unsafe { bindings::iommu_iova_to_phys(domain, iova + off) }; + if back != phys + off { + if back != 0 { + dev_err!( + dev, + "IOVA {:#x} for this boot's firmware region {} is in use ({:#x})", + iova + off, + idx, + back + ); + return Err(EBUSY); + } + // SAFETY: As above; the IOVA page is free and the physical + // page is this boot's firmware segment, which Linux does + // not own (checked by the helper). + let err = unsafe { + bindings::iommu_map( + domain, + (iova + off) as usize, + phys + off, + FW_REGION_PAGE as usize, + prot, + bindings::GFP_KERNEL, + ) + }; + if err != 0 { + dev_err!(dev, "Could not map IOVA {:#x} ({})", iova + off, err); + return Err(Error::from_errno(err)); + } + mapped += 1; + } + off += FW_REGION_PAGE; + } + dev_info!( + dev, + "resume from disk: firmware region {} [{:#x}, +{:#x}) at IOVA {:#x} ({} pages mapped)", + idx, + phys, + size, + iova, + mapped + ); } - Ok(()) + Self::check_fw_set(dev, domain, ec0p, &boot)?; + Self::fw_set_store(&boot) + } + + #[cfg(not(CONFIG_APPLE_AURA_HIBERNATE))] + fn remap_fw_regions_cold(dev: &device::Device, ec0p: u64) -> Result<()> { + Self::check_fw_regions(dev, ec0p) } /// Leaks every allocation the firmware may still access. Called when the @@ -2287,6 +2570,16 @@ impl AOP for AopData { // No registration is queued after this point, so once the queue is // empty the list of children is complete. self.registration_queue.drain(); + let halted = HALTED_AT_UNBIND.xchg(false, Acquire) && self.cpu_started.load(Acquire); + if halted { + // Nobody answers: the children's unbind calls fail at once + // instead of each waiting for a reply. + self.transport_closing.store(true, Release); + dev_info!( + self.dev, + "co-processor halted underneath (resume from disk): releasing the old session without a shutdown" + ); + } // Unbind the children while the transport still works, so that their // unbind can talk to their services. Then drop any listener a child // left behind: no report may reach a driver that is going away. @@ -2297,8 +2590,10 @@ impl AOP for AopData { self.hid_listeners.lock().clear(); self.report_listeners.lock().clear(); self.transport_closing.store(true, Release); - if let Err(e) = self.stop() { - dev_err!(self.dev, "Failed to stop AOP {:?}", e); + if !halted { + if let Err(e) = self.stop() { + dev_err!(self.dev, "Failed to stop AOP {:?}", e); + } } // Take the handle out of the shared state before dropping it: the // drop waits for the RTKit receive worker, which takes the same lock. @@ -2306,7 +2601,7 @@ impl AOP for AopData { let rtkit = self.rtkit.lock().take(); let mut quiesced = true; if let Some(mut rtkit) = rtkit { - if self.quiesce_on_unbind && self.cpu_started.load(Acquire) { + if self.quiesce_on_unbind && self.cpu_started.load(Acquire) && !halted { // The co-processor DMAs into the shared buffers until it has // acknowledged the shutdown. If it does not, nothing it may // still write to can be freed. @@ -3069,13 +3364,127 @@ unsafe extern "C" fn aop_suspend_late(dev: *mut bindings::device) -> c_int { /// Called by the PM core with a bound AOP device. unsafe extern "C" fn aop_resume(dev: *mut bindings::device) -> c_int { // SAFETY: by this function's contract. - unsafe { aop_from_dev(dev) }.sleep_undo(); + let data = unsafe { aop_from_dev(dev) }; + // A sleep that powered the SoC down leaves the co-processor's run bit clear: its session is + // gone and nothing restarted it, so the warm wake's stop/start notices would each time out on + // a dead firmware (bounded by the RTKit handshake timeout, but seconds of stall) and the AOP + // would stay down until a reboot. Take it over the cold way instead, exactly as a resume from + // disk does. A co-processor that kept running (the measured T8140 behaviour, unverified on + // T6040) has its run bit set, so this is a no-op there and the warm wake runs unchanged. + // SAFETY: only reads a flag. + if unsafe { bindings::pm_resume_via_firmware() } { + // SAFETY: by this function's contract. + if unsafe { aop_cold_lost(dev, data) } { + return 0; + } + } + data.sleep_undo(); + 0 +} + +/// A co-processor whose run bit is clear lost its power for this sleep. Close the transport so no +/// client waits on the dead firmware and queue the unbind and probe that start it again once tasks +/// thaw, as [`aop_restore`] does after a resume from disk. Returns whether it took the device over; +/// a co-processor still running is left to the warm wake. +/// +/// # Safety +/// +/// Called by a PM core resume callback with a bound AOP device; `data` is its drvdata. +unsafe fn aop_cold_lost(dev: *mut bindings::device, data: &AopData) -> bool { + if !data.cpu_started.load(Acquire) || data.removing.load(Acquire) { + return false; + } + // SAFETY: the PM core runs this callback on a bound device, under its device lock. + let bound = unsafe { device::Device::::from_raw(dev) }; + let Ok(pdev) = <&platform::Device>::try_from(bound) else { + return false; + }; + if !asc_cpu_halted(pdev) { + return false; + } + data.transport_closing.store(true, Release); + match restart_after_thaw(dev) { + Ok(()) => dev_info!( + data.dev, + "co-processor halted (power lost for the sleep), restarting it once tasks are thawed\n" + ), + Err(e) => dev_warn!( + data.dev, + "co-processor halted (power lost for the sleep), calls refused; restart not queued ({:?})\n", + e + ), + } + true +} + +/// Queues the unbind and probe that start a halted co-processor again. +#[cfg(CONFIG_APPLE_AURA_HIBERNATE)] +fn restart_after_thaw(dev: *mut bindings::device) -> Result<()> { + // SAFETY: `dev` is a live device; the helper holds a reference until its work ran. + to_result(unsafe { bindings::aura_hibernate_reprobe_after_thaw(dev) }) +} + +/// Without aura's hibernation glue nothing restarts it here: user space re-probes it. +#[cfg(not(CONFIG_APPLE_AURA_HIBERNATE))] +fn restart_after_thaw(_dev: *mut bindings::device) -> Result<()> { + Err(EOPNOTSUPP) +} + +/// A resume from disk is a cold boot: the boot loader staged the co-processor anew and left it +/// halted, and the boot kernel did not start it. The session this kernel holds is gone, but +/// nothing tells its clients: every call they make waits for a reply that never comes, and the +/// mailbox keeps the doorbells. The firmware the next probe starts then takes them as its own +/// first messages (on J616s two such calls from the motion sensors' PM notifier, and the +/// re-probed firmware crashed at a NULL pointer while its endpoints came up). So the transport +/// closes here, before any client runs, and the device is probed again once the tasks are +/// thawed: the unbind finds the run bit clear and drops the old session without a shutdown, +/// the probe starts the firmware as at boot. An aborted hibernation and a co-processor that +/// kept running are left as they are. +/// +/// # Safety +/// +/// Called by the PM core with a bound AOP device. +unsafe extern "C" fn aop_restore(dev: *mut bindings::device) -> c_int { + // SAFETY: only reads a flag. + if !unsafe { bindings::pm_hibernate_is_restored() } { + return 0; + } + // SAFETY: by this function's contract. + let data = unsafe { aop_from_dev(dev) }; + if !data.cpu_started.load(Acquire) || data.removing.load(Acquire) { + return 0; + } + // SAFETY: the PM core runs this callback on a bound device, under its device lock. + let bound = unsafe { device::Device::::from_raw(dev) }; + let Ok(pdev) = <&platform::Device>::try_from(bound) else { + return 0; + }; + if !asc_cpu_halted(pdev) { + dev_info!( + data.dev, + "resume from disk: the co-processor kept running, its session is left as is\n" + ); + return 0; + } + data.transport_closing.store(true, Release); + match restart_after_thaw(dev) { + Ok(()) => dev_info!( + data.dev, + "resume from disk: co-processor halted (cold boot), restarting it once tasks are thawed\n" + ), + Err(e) => dev_warn!( + data.dev, + "resume from disk: co-processor halted (cold boot), calls refused; restart not queued ({:?})\n", + e + ), + } 0 } static AOP_PM_OPS: bindings::dev_pm_ops = bindings::dev_pm_ops { suspend_late: Some(aop_suspend_late), resume: Some(aop_resume), + restore: Some(aop_restore), // SAFETY: all-zero is a valid `dev_pm_ops`: no callbacks. ..unsafe { core::mem::zeroed() } }; @@ -3112,7 +3521,15 @@ impl platform::Driver for AopDriver { info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, Error> + 'bound { let cfg = info.ok_or(ENODEV)?; - if RETIRED.load(Acquire) { + let cold = COLD_REPROBE.load(Acquire); + if cold && RETIRED.load(Acquire) && RETIRED_BEFORE_HALT.load(Acquire) { + // The boot loader halted and restaged the co-processor since the + // shutdown failed: nothing runs on the retained buffers anymore. + dev_warn!( + pdev.as_ref(), + "an earlier instance could not be shut down, but the co-processor was restaged since (resume from disk)" + ); + } else if RETIRED.load(Acquire) { dev_err!( pdev.as_ref(), "an earlier instance could not be shut down; reboot before probing again" @@ -3128,7 +3545,19 @@ impl platform::Driver for AopDriver { let asc_mmio = asc_mmio.as_view().relaxed(); if cfg.fw_regions { // Before anything is written to the AOP: no mapping, no start. - AopData::check_fw_regions(pdev.as_ref(), cfg.ec0p)?; + if cold { + // Remapping is only safe while the firmware does not run. + if asc_mmio.read(ASC_CPU_CONTROL).cpu_run() != 0 { + dev_err!( + pdev.as_ref(), + "resume from disk: the co-processor runs, its firmware regions are left alone" + ); + return Err(EBUSY); + } + AopData::remap_fw_regions_cold(pdev.as_ref(), cfg.ec0p)?; + } else { + AopData::check_fw_regions(pdev.as_ref(), cfg.ec0p)?; + } } let data = AopData::new(pdev, cfg)?; // Whatever fails below leaves the AOP running and its children @@ -3161,6 +3590,14 @@ impl platform::Driver for AopDriver { } asc_mmio.update(ASC_CPU_CONTROL, |r| r.with_const_cpu_run::<1>()); data.cpu_started.store(true, Release); + if cold { + // The firmware runs from here on: a later unbind sees it running + // and the next probe is an ordinary one, whatever happens below. + COLD_REPROBE.store(false, Release); + if RETIRED_BEFORE_HALT.xchg(false, Acquire) { + RETIRED.store(false, Release); + } + } let started = if cfg.setup_port { // The RTKit handshake up to AP power on comes first, then the // setup port's own power state and endpoint buffers, and only @@ -3198,13 +3635,33 @@ impl platform::Driver for AopDriver { Ok(Self(data)) } - fn unbind(_dev: &platform::Device>, this: Pin<&Self>) { + fn unbind(dev: &platform::Device>, this: Pin<&Self>) { + let halted = asc_cpu_halted(dev); + HALTED_AT_UNBIND.store(halted, Release); + if halted { + RETIRED_BEFORE_HALT.store(RETIRED.load(Acquire), Release); + COLD_REPROBE.store(true, Release); + } // The device is still bound here, which the DMA allocations the // teardown frees require. Drop only repeats it as a no-op. this.0.remove(); } } +/// Reads the ASC CPU run bit. Probe sets it; only the boot loader clears it +/// (the co-processor is staged halted at every boot, including the one a +/// resume from disk starts with). Any failure to read it counts as running, +/// which keeps the full shutdown. +fn asc_cpu_halted(pdev: &platform::Device) -> bool { + let Some(req) = pdev.io_request_by_index(1) else { + return false; + }; + let Ok(asc) = req.iomap_sized::() else { + return false; + }; + asc.as_view().relaxed().read(ASC_CPU_CONTROL).cpu_run() == 0 +} + impl Drop for AopDriver { fn drop(&mut self) { self.0.remove(); diff --git a/drivers/soc/apple/dockchannel.c b/drivers/soc/apple/dockchannel.c index 77dc01f8b4359..f1b0ea7d36317 100644 --- a/drivers/soc/apple/dockchannel.c +++ b/drivers/soc/apple/dockchannel.c @@ -43,6 +43,13 @@ #define DATA_RX32 0x28 #define DATA_RX_COUNT 0x2c +/* Bits of dockchannel::armed: the interrupt is enabled and has not fired since. */ +#define DOCKCHANNEL_TX_ARMED 0 +#define DOCKCHANNEL_RX_ARMED 1 + +/* Receive interrupts below the threshold in a row before they are handled anyway. */ +#define DOCKCHANNEL_RX_SPURIOUS_MAX 16 + struct dockchannel { struct device *dev; int tx_irq; @@ -56,6 +63,20 @@ struct dockchannel { struct completion tx_comp; struct completion rx_comp; + /* + * Each interrupt is enabled by whoever arms it and disabled exactly once + * afterwards, by its hard handler or by whoever gives up waiting, so the + * enable depth stays balanced. + */ + unsigned long armed; + /* The receive threshold last programmed. */ + u32 rx_thresh; + unsigned int rx_spurious; + /* Receive was stopped by dockchannel_await(..., 0) from outside the callback. */ + bool rx_stopped; + /* The receive interrupt thread, which runs the client callback. */ + struct task_struct *rx_thread; + void *cookie; void (*data_available)(void *cookie, size_t avail); }; @@ -70,23 +91,74 @@ struct dockchannel_common { /* Dockchannel FIFO functions */ +static void dockchannel_arm(struct dockchannel *dockchannel, int bit, int irq) +{ + if (!test_and_set_bit(bit, &dockchannel->armed)) + enable_irq(irq); +} + +/* + * Never disable_irq() here: a client's receive callback runs in the receive + * interrupt thread and waits for data from it, and disable_irq() would wait + * for that thread to finish, i.e. for itself. + */ +static void dockchannel_disarm(struct dockchannel *dockchannel, int bit, int irq) +{ + if (test_and_clear_bit(bit, &dockchannel->armed)) + disable_irq_nosync(irq); +} + static irqreturn_t dockchannel_tx_irq(int irq, void *data) { struct dockchannel *dockchannel = data; + /* Not armed: a sender giving up disables it. */ + if (!test_and_clear_bit(DOCKCHANNEL_TX_ARMED, &dockchannel->armed)) + return IRQ_HANDLED; disable_irq_nosync(irq); complete(&dockchannel->tx_comp); return IRQ_HANDLED; } +/* + * An interrupt with less data than the threshold that was programmed means + * that the FIFO does not hold that threshold: after a resume from disk the + * configuration registers hold what the boot loader or the boot kernel left, + * while the restored interrupt state says that receive is armed. Program it + * again and keep waiting, instead of waking a client to read data that is + * not there. + */ +static bool dockchannel_rx_spurious(struct dockchannel *dockchannel) +{ + u32 thresh = READ_ONCE(dockchannel->rx_thresh); + + if (readl_relaxed(dockchannel->data_base + DATA_RX_COUNT) >= thresh) + goto real; + writel_relaxed(thresh, dockchannel->config_base + CONFIG_RX_THRESH); + if (readl_relaxed(dockchannel->data_base + DATA_RX_COUNT) >= thresh) + goto real; + if (++dockchannel->rx_spurious <= DOCKCHANNEL_RX_SPURIOUS_MAX) + return true; + dev_warn_once(dockchannel->dev, + "receive interrupt keeps firing below its threshold (%u)\n", thresh); +real: + dockchannel->rx_spurious = 0; + return false; +} + static irqreturn_t dockchannel_rx_irq(int irq, void *data) { struct dockchannel *dockchannel = data; + if (dockchannel_rx_spurious(dockchannel)) + return IRQ_HANDLED; + /* Not armed: whoever is disarming it disables it. */ + if (!test_and_clear_bit(DOCKCHANNEL_RX_ARMED, &dockchannel->armed)) + return IRQ_HANDLED; disable_irq_nosync(irq); - if (dockchannel->awaiting) { + if (READ_ONCE(dockchannel->awaiting)) { return IRQ_WAKE_THREAD; } else { complete(&dockchannel->rx_comp); @@ -97,9 +169,15 @@ static irqreturn_t dockchannel_rx_irq(int irq, void *data) static irqreturn_t dockchannel_rx_irq_thread(int irq, void *data) { struct dockchannel *dockchannel = data; - size_t avail = readl_relaxed(dockchannel->data_base + DATA_RX_COUNT); + size_t avail; + + WRITE_ONCE(dockchannel->rx_thread, current); + /* Woken just before a stop: the client asked for nothing more. */ + if (READ_ONCE(dockchannel->rx_stopped) || !dockchannel->data_available) + return IRQ_HANDLED; - dockchannel->awaiting = false; + avail = readl_relaxed(dockchannel->data_base + DATA_RX_COUNT); + WRITE_ONCE(dockchannel->awaiting, false); dockchannel->data_available(dockchannel->cookie, avail); return IRQ_HANDLED; @@ -119,11 +197,12 @@ int dockchannel_send(struct dockchannel *dockchannel, const void *buf, size_t co writel_relaxed(threshold, dockchannel->config_base + CONFIG_TX_THRESH); reinit_completion(&dockchannel->tx_comp); - enable_irq(dockchannel->tx_irq); + dockchannel_arm(dockchannel, DOCKCHANNEL_TX_ARMED, dockchannel->tx_irq); if (!wait_for_completion_timeout(&dockchannel->tx_comp, msecs_to_jiffies(DOCKCHANNEL_TX_TIMEOUT_MS))) { - disable_irq(dockchannel->tx_irq); + dockchannel_disarm(dockchannel, DOCKCHANNEL_TX_ARMED, + dockchannel->tx_irq); return -ETIMEDOUT; } @@ -147,6 +226,11 @@ int dockchannel_send(struct dockchannel *dockchannel, const void *buf, size_t co } EXPORT_SYMBOL(dockchannel_send); +/* + * Called by the client's receive callback, in the receive interrupt thread: + * waiting for the rest of a frame here re-arms the receive interrupt, whose + * hard handler then completes the wait instead of waking the thread. + */ int dockchannel_recv(struct dockchannel *dockchannel, void *buf, size_t count) { size_t left = count; @@ -159,13 +243,15 @@ int dockchannel_recv(struct dockchannel *dockchannel, void *buf, size_t count) if (avail == 0) { size_t threshold = min((size_t)(dockchannel->fifo_size / 2), left); + WRITE_ONCE(dockchannel->rx_thresh, threshold); writel_relaxed(threshold, dockchannel->config_base + CONFIG_RX_THRESH); reinit_completion(&dockchannel->rx_comp); - enable_irq(dockchannel->rx_irq); + dockchannel_arm(dockchannel, DOCKCHANNEL_RX_ARMED, dockchannel->rx_irq); if (!wait_for_completion_timeout(&dockchannel->rx_comp, msecs_to_jiffies(DOCKCHANNEL_RX_TIMEOUT_MS))) { - disable_irq(dockchannel->rx_irq); + dockchannel_disarm(dockchannel, DOCKCHANNEL_RX_ARMED, + dockchannel->rx_irq); return -ETIMEDOUT; } @@ -189,6 +275,24 @@ int dockchannel_recv(struct dockchannel *dockchannel, void *buf, size_t count) } EXPORT_SYMBOL(dockchannel_recv); +/* + * Stop receiving. From outside the callback this also waits for a callback + * that is running to return, and undoes the re-arm it may have done meanwhile; + * the thread does not call the client again until receive is restarted. + */ +static void dockchannel_rx_stop(struct dockchannel *dockchannel) +{ + WRITE_ONCE(dockchannel->rx_stopped, true); + WRITE_ONCE(dockchannel->awaiting, false); + dockchannel_disarm(dockchannel, DOCKCHANNEL_RX_ARMED, dockchannel->rx_irq); + if (current == READ_ONCE(dockchannel->rx_thread)) + return; + + synchronize_irq(dockchannel->rx_irq); + WRITE_ONCE(dockchannel->awaiting, false); + dockchannel_disarm(dockchannel, DOCKCHANNEL_RX_ARMED, dockchannel->rx_irq); +} + int dockchannel_await(struct dockchannel *dockchannel, void (*callback)(void *cookie, size_t avail), void *cookie, size_t count) @@ -196,16 +300,24 @@ int dockchannel_await(struct dockchannel *dockchannel, size_t threshold = min((size_t)dockchannel->fifo_size, count); if (!count) { - dockchannel->awaiting = false; - disable_irq(dockchannel->rx_irq); + dockchannel_rx_stop(dockchannel); return 0; } + if (current == READ_ONCE(dockchannel->rx_thread)) { + /* The callback re-arms itself; a stop that came meanwhile wins. */ + if (READ_ONCE(dockchannel->rx_stopped)) + return 0; + } else { + WRITE_ONCE(dockchannel->rx_stopped, false); + } + dockchannel->data_available = callback; dockchannel->cookie = cookie; - dockchannel->awaiting = true; + WRITE_ONCE(dockchannel->awaiting, true); + WRITE_ONCE(dockchannel->rx_thresh, threshold); writel_relaxed(threshold, dockchannel->config_base + CONFIG_RX_THRESH); - enable_irq(dockchannel->rx_irq); + dockchannel_arm(dockchannel, DOCKCHANNEL_RX_ARMED, dockchannel->rx_irq); return threshold; } @@ -337,14 +449,23 @@ static void dockchannel_irq_unmask(struct irq_data *data) * The controller hangs off a single chained AIC interrupt, which the IRQ core * never disables for system sleep and which keeps being delivered in * suspend-to-idle. A FIFO interrupt left unmasked can therefore end a - * suspend-to-idle without configuring anything, here or in the AIC. + * suspend-to-idle without configuring anything here. A suspend-to-RAM masks + * every AIC interrupt that is not a wakeup source, so arming a FIFO interrupt + * arms the chained parent too; the IRQ core counts the arms. */ +static int dockchannel_irq_set_wake(struct irq_data *data, unsigned int on) +{ + struct dockchannel_common *dcc = irq_data_get_irq_chip_data(data); + + return irq_set_irq_wake(dcc->irq, on); +} + static const struct irq_chip dockchannel_irqchip = { .name = "dockchannel-irqc", .irq_ack = dockchannel_irq_ack, .irq_mask = dockchannel_irq_mask, .irq_unmask = dockchannel_irq_unmask, - .flags = IRQCHIP_SKIP_SET_WAKE, + .irq_set_wake = dockchannel_irq_set_wake, }; static int dockchannel_irq_domain_map(struct irq_domain *d, unsigned int virq, @@ -415,6 +536,35 @@ static void dockchannel_remove(struct platform_device *pdev) writel_relaxed(~0, dcc->irq_base + IRQ_FLAG); } +#ifdef CONFIG_HIBERNATION +/* + * After a resume from disk the interrupt mask register holds what the boot + * kernel left in it (nothing, if it never loaded this driver), while the + * restored interrupt descriptors still describe the image's state. Write the + * image's state back before the channel clients restore; an unmasked receive + * interrupt that the hardware has masked would never fire. + */ +static int dockchannel_restore_noirq(struct device *dev) +{ + struct dockchannel_common *dcc = dev_get_drvdata(dev); + u32 mask = 0; + int hwirq; + + for (hwirq = 0; hwirq < DOCKCHANNEL_MAX_IRQ; hwirq++) { + unsigned int virq = irq_find_mapping(dcc->domain, hwirq); + + if (virq && !irqd_irq_masked(irq_get_irq_data(virq))) + mask |= BIT(hwirq); + } + writel_relaxed(mask, dcc->irq_base + IRQ_MASK); + return 0; +} + +static const struct dev_pm_ops dockchannel_pm_ops = { + .restore_noirq = dockchannel_restore_noirq, +}; +#endif + static const struct of_device_id dockchannel_of_match[] = { { .compatible = "apple,dockchannel" }, {}, @@ -425,6 +575,9 @@ static struct platform_driver dockchannel_driver = { .driver = { .name = "dockchannel", .of_match_table = dockchannel_of_match, +#ifdef CONFIG_HIBERNATION + .pm = &dockchannel_pm_ops, +#endif }, .probe = dockchannel_probe, .remove = dockchannel_remove, diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index e18e84bb1986a..353d1dd991a47 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -6,6 +6,16 @@ //! capture in flight did not wake. Before the system suspends, end any capture //! in progress and hold new ones until it has resumed. The hwrng fill thread //! is not frozen either, so entropy requests are held over the same window. +//! +//! A hibernation also holds every request Linux starts, and the enclave's +//! xART writes are refused, from its prepare until the kernel knows how it +//! ended. The image must not miss host or gigalocker state written after the +//! snapshot, and after a resume from disk on a cold boot the enclave is a +//! freshly booted sepOS that must not see a message of the old session before +//! the new registration. The PM core tells a resume from the image from an +//! aborted hibernation or the wake of a hybrid sleep, and the PMP, which the +//! boot loader halts on a cold boot, whether that resume went through one +//! (`pm_shim.c`); the device is then probed again. //! The notifier itself is in `pm_shim.c`; the capture and entropy handling is //! on [`SepData`]. //! @@ -29,7 +39,7 @@ static SEP: AtomicPtr = AtomicPtr::new(core::ptr::null_mut()); extern "C" { fn sep_pm_register( dev: *mut c_void, - event: unsafe extern "C" fn(bool), + event: unsafe extern "C" fn(c_int) -> c_int, sleep: unsafe extern "C" fn() -> c_int, wake: unsafe extern "C" fn(), told: unsafe extern "C" fn() -> bool, @@ -47,9 +57,25 @@ extern "C" { mask: u64, ) -> c_int; fn sep_pm_commit_finish(dev: *mut c_void) -> c_int; + fn sep_pm_exit(); pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; } +// Sleep events from `pm_shim.c`; must match `SEP_PM_*` in `shim.h`. +const EVENT_SLEEP: c_int = 0; +const EVENT_WAKE: c_int = 1; +const EVENT_HIBERNATE: c_int = 2; +const EVENT_HIBERNATE_WARM: c_int = 3; +const EVENT_HIBERNATE_COLD: c_int = 4; +const EVENT_RESTORE: c_int = 5; + +/// Cancels a probe queued after a resume from disk. Called once the module +/// goes away, before the driver is unregistered. +pub(crate) fn exit() { + // SAFETY: no preconditions; waits for a running reprobe to finish. + unsafe { sep_pm_exit() }; +} + /// Who may use the mailbox around a firmware-driven suspend. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum SleepGate { @@ -164,6 +190,14 @@ trait SleepHolds { fn hold_captures(&self); fn release_captures(&self); fn release_entropy(&self); + /// Holds every request Linux starts and refuses the enclave's xART + /// writes, or lifts that, around a hibernation. + fn hold_session(&self, held: bool); + /// The enclave was booted anew under a hibernation image: the session + /// is gone. Returns whether the device should be probed again. + fn session_lost(&self) -> bool; + /// Whether this kernel registered with the enclave. + fn attached(&self) -> bool; } impl SleepHolds for SepData { @@ -179,32 +213,70 @@ impl SleepHolds for SepData { fn release_entropy(&self) { self.rng_sleep_finished(); } + fn hold_session(&self, held: bool) { + self.hibernation_hold(held); + } + fn session_lost(&self) -> bool { + self.lose_session() + } + fn attached(&self) -> bool { + SepData::attached(self) + } } -/// `entering`: the system is about to sleep; otherwise it has resumed, or -/// the suspend was aborted. -fn sleep_event(sep: &impl SleepHolds, entering: bool) { - if entering { - sep.hold_entropy(); - sep.hold_captures(); - } else { - sep.release_captures(); - sep.release_entropy(); +/// Takes or releases the holds for one sleep event. Returns 1 when the +/// device must be probed again, or for `EVENT_RESTORE` when the restore must +/// be refused; 0 otherwise. +fn sleep_event(sep: &impl SleepHolds, event: c_int) -> c_int { + match event { + EVENT_SLEEP => { + sep.hold_entropy(); + sep.hold_captures(); + } + EVENT_HIBERNATE => { + sep.hold_entropy(); + sep.hold_captures(); + sep.hold_session(true); + } + EVENT_WAKE => { + sep.release_captures(); + sep.release_entropy(); + } + // This kernel is about to load a hibernation image: refused (1) if + // it registered with the enclave, see `pm_shim.c`. + EVENT_RESTORE => { + if sep.attached() { + return 1; + } + sep.hold_entropy(); + sep.hold_captures(); + sep.hold_session(true); + } + EVENT_HIBERNATE_WARM => { + sep.hold_session(false); + sep.release_captures(); + sep.release_entropy(); + } + // The holds stay: the removal ahead of the new probe releases them + // into a session that refuses everything. + EVENT_HIBERNATE_COLD => return sep.session_lost() as c_int, + _ => {} } + 0 } /// # Safety /// Called by `pm_shim.c` from the PM notifier chain, in process context. -unsafe extern "C" fn pm_event(entering: bool) { +unsafe extern "C" fn pm_event(event: c_int) -> c_int { let ptr = SEP.load(Ordering::Acquire); if ptr.is_null() { - return; + return 0; } // SAFETY: produced by `into_foreign` in `register`, and reclaimed by // `unregister` only after `sep_pm_unregister` has waited out any running // notifier call, so it is live for this borrow. let sep = unsafe { as ForeignOwnable>::borrow(ptr) }; - sleep_event(&*sep, entering); + sleep_event(&*sep, event) } /// Runs `f` on the registered instance. Only for callbacks that @@ -270,6 +342,8 @@ pub(crate) fn register(sep: Arc) -> Result<()> { // SAFETY: the shim stores the callback and registers a notifier block // that lives for the module's lifetime. + // `dev` is the bound SEP device: the shim drops it in `sep_pm_unregister` + // and takes its own reference for a reprobe. let rc = unsafe { sep_pm_register(dev, pm_event, pm_sleep, pm_wake, pm_told) }; if rc != 0 { let raw = SEP.swap(core::ptr::null_mut(), Ordering::AcqRel); @@ -308,10 +382,15 @@ mod tests { const HOLD_CAPTURES: u8 = 2; const RELEASE_CAPTURES: u8 = 3; const RELEASE_ENTROPY: u8 = 4; + const HOLD_SESSION: u8 = 5; + const RELEASE_SESSION: u8 = 6; + const LOSE_SESSION: u8 = 7; struct Calls { made: RefCell<[u8; 8]>, count: Cell, + lose: Cell, + attached: Cell, } impl Calls { @@ -319,6 +398,8 @@ mod tests { Calls { made: RefCell::new([0; 8]), count: Cell::new(0), + lose: Cell::new(true), + attached: Cell::new(false), } } fn call(&self, which: u8) { @@ -344,6 +425,16 @@ mod tests { fn release_entropy(&self) { self.call(RELEASE_ENTROPY); } + fn hold_session(&self, held: bool) { + self.call(if held { HOLD_SESSION } else { RELEASE_SESSION }); + } + fn session_lost(&self) -> bool { + self.call(LOSE_SESSION); + self.lose.get() + } + fn attached(&self) -> bool { + self.attached.get() + } } #[test] @@ -376,7 +467,7 @@ mod tests { #[test] fn going_to_sleep_holds_entropy_then_captures() { let calls = Calls::new(); - sleep_event(&calls, true); + assert_eq!(sleep_event(&calls, EVENT_SLEEP), 0); assert_eq!( calls.made(), [HOLD_ENTROPY, HOLD_CAPTURES, 0, 0, 0, 0, 0, 0] @@ -386,9 +477,9 @@ mod tests { #[test] fn sleep_holds_stay_until_the_post_event() { let calls = Calls::new(); - sleep_event(&calls, true); + assert_eq!(sleep_event(&calls, EVENT_SLEEP), 0); assert_eq!(calls.count.get(), 2); - sleep_event(&calls, false); + assert_eq!(sleep_event(&calls, EVENT_WAKE), 0); assert_eq!( calls.made(), [HOLD_ENTROPY, HOLD_CAPTURES, RELEASE_CAPTURES, RELEASE_ENTROPY, 0, 0, 0, 0] @@ -398,10 +489,83 @@ mod tests { #[test] fn waking_releases_captures_then_entropy() { let calls = Calls::new(); - sleep_event(&calls, false); + assert_eq!(sleep_event(&calls, EVENT_WAKE), 0); assert_eq!( calls.made(), [RELEASE_CAPTURES, RELEASE_ENTROPY, 0, 0, 0, 0, 0, 0] ); } + + #[test] + fn a_hibernation_holds_the_session_last() { + let calls = Calls::new(); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE), 0); + assert_eq!( + calls.made(), + [HOLD_ENTROPY, HOLD_CAPTURES, HOLD_SESSION, 0, 0, 0, 0, 0] + ); + } + + #[test] + fn a_warm_end_of_a_hibernation_lifts_the_session_hold_first() { + let calls = Calls::new(); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE), 0); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE_WARM), 0); + assert_eq!( + calls.made(), + [ + HOLD_ENTROPY, + HOLD_CAPTURES, + HOLD_SESSION, + RELEASE_SESSION, + RELEASE_CAPTURES, + RELEASE_ENTROPY, + 0, + 0 + ] + ); + } + + #[test] + fn a_cold_resume_keeps_the_holds_and_asks_for_a_probe() { + let calls = Calls::new(); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE), 0); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE_COLD), 1); + assert_eq!( + calls.made(), + [HOLD_ENTROPY, HOLD_CAPTURES, HOLD_SESSION, LOSE_SESSION, 0, 0, 0, 0] + ); + } + + #[test] + fn a_second_cold_resume_asks_for_no_second_probe() { + let calls = Calls::new(); + calls.lose.set(false); + assert_eq!(sleep_event(&calls, EVENT_HIBERNATE_COLD), 0); + } + + #[test] + fn a_restore_over_an_attached_enclave_is_refused_without_holds() { + let calls = Calls::new(); + calls.attached.set(true); + assert_eq!(sleep_event(&calls, EVENT_RESTORE), 1); + assert_eq!(calls.count.get(), 0); + } + + #[test] + fn a_restore_without_a_session_holds_like_a_hibernation() { + let calls = Calls::new(); + assert_eq!(sleep_event(&calls, EVENT_RESTORE), 0); + assert_eq!( + calls.made(), + [HOLD_ENTROPY, HOLD_CAPTURES, HOLD_SESSION, 0, 0, 0, 0, 0] + ); + } + + #[test] + fn an_unknown_event_changes_nothing() { + let calls = Calls::new(); + assert_eq!(sleep_event(&calls, 99), 0); + assert_eq!(calls.count.get(), 0); + } } diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index 25629d0581706..3cbc26a34bde3 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -1,8 +1,11 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT +#include #include #include #include +#include #include +#include #include #include #include @@ -20,27 +23,91 @@ static struct device *pmp_device; /* The firmware is stopped for a system sleep. */ static bool pmp_asleep; -int apple_pmp_register(struct device *dev, const void *data) +/* Resumes from disk whose restore found the PMP halted, see below. */ +static atomic_t pmp_cold_restarts = ATOMIC_INIT(0); + +void apple_pmp_note_cold_restart(void) +{ + atomic_inc(&pmp_cold_restarts); +} + +/* + * A co-processor that iBoot restarts on a cold boot but that has no run bit + * the AP can read (the SEP) learns from this that the resume from disk it is + * being notified of was a cold boot: compare the count across the + * hibernation, from a PM notifier of lower priority than the PMP's. + */ +int apple_pmp_cold_restarts(void) { - int ret = 0; + /* No bound PMP: no restore runs, so a count would mean nothing. */ + if (!READ_ONCE(pmp_data)) + return -ENODEV; + return atomic_read(&pmp_cold_restarts) & INT_MAX; +} +EXPORT_SYMBOL_GPL(apple_pmp_cold_restarts); +/* + * After a hibernation restore the boot loader has cold-restarted the PMP + * (image resident in PMS_SRAM, ASC CPU halted) and nothing replays the + * probe-time bring-up, so the firmware stays down (the display gate and + * cpufreq both wait on a PMP that never reports running). The module cannot be + * reloaded (the power-vote bridge is retained until reboot), so rebuild the + * firmware session in place. + * + * The restored kernel resumes inside hibernate() with in_suspend == 0 and + * sends PM_POST_HIBERNATION, not PM_POST_RESTORE (that one only reaches the + * boot kernel when it fails to restore). PM_POST_HIBERNATION also ends an + * aborted hibernation, where the firmware never stopped: restore() reads the + * ASC CPU run bit and leaves a running firmware alone. + */ +static int apple_pmp_pm_notify(struct notifier_block *nb, unsigned long action, + void *unused) +{ + if (action != PM_POST_HIBERNATION && action != PM_POST_RESTORE) + return NOTIFY_DONE; + /* Serialize against unregister in Drop, as the power-command path does. */ guard(mutex)(&pmp_lock); if (pmp_data) - ret = -EBUSY; - else { - pmp_data = data; + apple_pmp_restore(pmp_data); + return NOTIFY_DONE; +} + +static struct notifier_block apple_pmp_pm_nb = { + .notifier_call = apple_pmp_pm_notify, + /* Ahead of every consumer's restore at priority 0 or below (ANE, DCP, GPU). */ + .priority = 10, +}; + +int apple_pmp_register(struct device *dev, const void *data) +{ + scoped_guard(mutex, &pmp_lock) { + if (pmp_data) + return -EBUSY; + WRITE_ONCE(pmp_data, data); pmp_device = dev; } - return ret; + /* + * Outside pmp_lock: the notifier chain's lock is taken around the + * callback, which takes pmp_lock, so nesting them here would invert the + * order. The callback re-checks pmp_data under the lock. + */ + register_pm_notifier(&apple_pmp_pm_nb); + return 0; } void apple_pmp_unregister(const void *data) { - guard(mutex)(&pmp_lock); - if (pmp_data == data) { - pmp_data = NULL; - pmp_device = NULL; + bool was_registered = false; + + scoped_guard(mutex, &pmp_lock) { + if (pmp_data == data) { + WRITE_ONCE(pmp_data, NULL); + pmp_device = NULL; + was_registered = true; + } } + if (was_registered) + unregister_pm_notifier(&apple_pmp_pm_nb); } int apple_pmp_link_device(struct device *consumer) @@ -211,6 +278,11 @@ static int apple_pmp_suspend_late(struct device *dev) dev_err(dev, "no stop at the sleep's commit: refusing the sleep\n"); return -EBUSY; } + /* An idled PMP comes back only through a real sleep, which a dry run never reaches. */ + if (!at_commit && !apple_sleep_power_loss()) { + dev_warn(dev, "dry run: not idled, the sleep controller can't take it at the commit\n"); + return 0; + } apple_pmp_report_sleep(dev, at_commit); ret = at_commit ? apple_pmp_commit_prepare(dev) : apple_pmp_sleep(pmp_data); if (ret) { diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 337be4192474e..de76f122ef927 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -181,10 +181,14 @@ struct PmpData { display_error: AtomicI32, /// See [`PmpHwConfig::sram_backup`]. sram_backup: Option<(usize, usize)>, + /// The SoC configuration, kept so the hibernation restore can re-run the + /// probe-time bring-up (the boot loader cold-restarts the PMP on an S4 + /// resume and nothing else replays it). + cfg: &'static PmpHwConfig, } impl PmpData { - fn new(dev: &platform::Device>, cfg: &PmpHwConfig) -> Result> { + fn new(dev: &platform::Device>, cfg: &'static PmpHwConfig) -> Result> { let pmp_req = dev.io_request_by_name(c"pmp").ok_or(EINVAL)?; let pmp_mmio = pmp_req.iomap_sized::()?.into_devres()?; let asc_req = dev.io_request_by_name(c"asc").ok_or(EINVAL)?; @@ -202,11 +206,98 @@ impl PmpData { display: AtomicPtr::new(core::ptr::null_mut()), display_error: AtomicI32::new(0), sram_backup: cfg.sram_backup, + cfg, } ), GFP_KERNEL, ) } + + /// Build the SRAM boot-argument patch list from the device-tree node, as + /// `probe()` does. Re-derivable on a hibernation restore (the DT is static). + fn build_patches(&self) -> Result> { + let node = self.dev.fwnode().ok_or(EIO)?; + let dvid = node + .property_read(c"apple,dram-vendor-id") + .required_by(&self.dev)?; + let bdid = node.property_read(c"apple,board-id").required_by(&self.dev)?; + let mut patches = KVec::with_capacity(9, GFP_KERNEL)?; + patches.push((from_fourcc(b"BDID"), bdid), GFP_KERNEL)?; + patches.push((from_fourcc(b"DVID"), dvid), GFP_KERNEL)?; + for (tag, prop) in OPTIONAL_BOOTARGS.iter() { + if let Some(v) = node.property_read::(prop).optional() { + patches.push((from_fourcc(tag), v), GFP_KERNEL)?; + } + } + if let Some(v) = node.property_read::(c"apple,pmc-pmgr").optional() { + patches.push((from_fourcc(b"PMCV"), v & 1), GFP_KERNEL)?; + patches.push((from_fourcc(b"PMCB"), (v >> 3) & 1), GFP_KERNEL)?; + } + Ok(patches) + } + + /// Re-run the PMP bring-up after a hibernation restore. The boot loader + /// cold-restarts the PMP (image resident in PMS_SRAM, ASC CPU halted), and + /// nothing replays the probe-time sequence, so the firmware stays down: + /// "no PMP within 20000ms" / cpufreq "PMP not running after 30s". The + /// retained `PmpData` Arc, both `DevresIoMem` mappings and the C power-vote + /// bridge all stay; only the volatile session and firmware are rebuilt. + /// The decision between a halted (reinit + boot) and a live (adopt) firmware + /// follows the ASC CPU run bit, as the NVMe ANS restore does. + fn restore(&self) -> Result<()> { + // Reached from a PM notifier, not the bound probe context, so access the + // retained mappings revocably (the device is still bound during resume). + // try_access() holds an RCU read-side lock while its guard lives: keep each + // guard to the MMIO accesses, never across a mutex, an allocation or the + // sleeping RTKit handshake. + let cpu_running = { + let asc = self.asc_mmio.try_access().ok_or(ENODEV)?; + asc.as_view().relaxed().read(ASC_CPU_CONTROL).cpu_run() != 0 + }; + dev_info!(self.dev, "PMP restore: ASC CPU_RUN={}\n", cpu_running as u32); + { + let mut guard = self.rtkit.lock(); + let rtk = guard.as_mut().as_pin_mut().ok_or(ENODEV)?; + if rtk.is_crashed() { + dev_err!(self.dev, "PMP restore: session crashed, unrecoverable\n"); + return Err(EIO); + } + if cpu_running { + // The firmware never stopped (an aborted hibernation, or a + // restore failure in the boot kernel): nothing to rebuild. + dev_info!(self.dev, "PMP restore: firmware still running, left as is\n"); + return Ok(()); + } + // Running before the image and halted now: the boot loader + // restarted the SoC under it, a cold boot, which co-processors + // without a run bit the AP can read (the SEP) have to know. + // SAFETY: no arguments; the bridge only counts. + unsafe { bindings::apple_pmp_note_cold_restart() }; + // Halted firmware: detach the stale session so a fresh HELLO/EPMAP + // boot is accepted, as the NVMe ANS restore reinits its session. + rtk.reinit()?; + } + // The runtime handshake on the PMP endpoint fails against the state + // left from before the hibernation, so clear it first. + *self.state.lock() = PmpState::new()?; + *self.power_command.lock() = Command::Idle; + // Re-patch the SRAM boot args (the cold-restarted firmware re-reads them). + let patches = self.build_patches()?; + { + let pmp = self.pmp_mmio.try_access().ok_or(ENODEV)?; + self.patch_bootargs(&pmp.as_view(), &patches, self.cfg.check_bootargs_uuid)?; + } + // Release the ASC CPU and drive the wake/boot handshake, as probe does. + { + let asc = self.asc_mmio.try_access().ok_or(ENODEV)?; + asc.as_view() + .relaxed() + .update(ASC_CPU_CONTROL, |r| r.with_const_cpu_run::<1>()); + } + self.start()?; + dev_info!(self.dev, "PMP restore: firmware and endpoint back\n"); + Ok(()) + } fn start(&self) -> Result<()> { let mut guard = self.rtkit.lock(); let mut rtk = guard.as_mut().as_pin_mut().unwrap(); @@ -782,6 +873,28 @@ pub unsafe extern "C" fn apple_pmp_wake(data: *const core::ffi::c_void) -> i32 { } } +/// Rebuild the PMP firmware session after a resume from disk (PM_POST_HIBERNATION). +/// The C power-vote bridge calls this with the retained `PmpData` pointer; the +/// bridge, the Arc and the MMIO mappings all stay — only the firmware session is +/// rebuilt. Returns 0 on success or a negative errno. A failure is logged and the +/// PMP stays down (cpufreq keeps its fallback clamp) rather than aborting resume. +/// +/// # Safety +/// `data` must be the live `PmpData` pointer the bridge was registered with, and +/// the caller must serialize against unregister in `Drop`. +#[export] +pub unsafe extern "C" fn apple_pmp_restore(data: *const core::ffi::c_void) -> i32 { + // SAFETY: the C bridge holds the retained foreign reference until reboot. + let data = unsafe { &*data.cast::() }; + match data.restore() { + Ok(()) => 0, + Err(e) => { + dev_err!(data.dev, "PMP restore failed: {:?}\n", e); + e.to_errno() + } + } +} + struct NoBuffer; impl rtkit::Buffer for NoBuffer { fn iova(&self) -> Result { diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 02fcc78b25564..ec6c1bc044fb3 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -971,6 +971,18 @@ struct SepData { shutting_down: Atomic, capture_epoch: Atomic, + // A hibernation is under way (prepare until the kernel knows how it + // ended): requests Linux starts get EAGAIN and the enclave's xART writes + // are refused, so that neither the host state nor the gigalocker changes + // behind the image. Separate from `sleep_gate`, which only a + // firmware-driven suspend moves. See pm.rs. + session_held: Atomic, + // The enclave was booted anew under a hibernation image (a resume from + // disk on a cold boot): nothing of this session may reach it any more. + // Set once; the device is then probed again. + session_gone: Atomic, + xart_held_logged: Atomic, + // The system is going to sleep: no capture may start. Changed under the // bio session lock; `sleep_over` is signalled when it clears. suspending: Atomic, @@ -1299,6 +1311,9 @@ impl SepData { registered: Atomic::new(false), shutting_down: Atomic::new(false), capture_epoch: Atomic::new(0), + session_held: Atomic::new(false), + session_gone: Atomic::new(false), + xart_held_logged: Atomic::new(false), suspending: Atomic::new(false), sleep_over <- new_condvar!("SepData::sleep_over"), captures_running: Atomic::new(0), @@ -1501,24 +1516,29 @@ impl SepData { } } + /// Sends a request Linux starts. Refused while a hibernation holds the + /// session (EAGAIN) and once the enclave has been booted anew under it + /// (ENODEV). fn send(&self, msg: Message) -> Result<()> { - if self.shutting_down.load(Relaxed) { + if self.shutting_down.load(Relaxed) || self.session_gone.load(Relaxed) { return Err(ENODEV); } // Checked with the mailbox lock held: a sender can wait for it for longer - // than the system takes to tell the enclave it's going to sleep. + // than the system takes to tell the enclave it's going to sleep. The + // hibernation hold is taken under it too. let mbox = self.mbox.lock(); let mbox = mbox.as_ref().ok_or(ENODEV)?; - if !self.sleep_gate().may_request() { + if !self.sleep_gate().may_request() || self.session_held.load(Relaxed) { return Err(EAGAIN); } Self::ring(mbox, msg) } /// As [`Self::send`], for an answer to something the enclave asked: it - /// waits for these before it can answer the sleep request. + /// waits for these before it can answer the sleep request. A hibernation + /// hold does not apply to them, but a lost session does. fn send_reply(&self, msg: Message) -> Result<()> { - if self.shutting_down.load(Relaxed) { + if self.shutting_down.load(Relaxed) || self.session_gone.load(Relaxed) { return Err(ENODEV); } let mbox = self.mbox.lock(); @@ -1529,6 +1549,45 @@ impl SepData { Self::ring(mbox, msg) } + /// A hibernation starts (`held`) or ended without a cold boot. The hold + /// is taken after the capture and entropy holds and lifted before them. + pub(crate) fn hibernation_hold(&self, held: bool) { + { + let _mbox = self.mbox.lock(); + self.session_held.store(held, Relaxed); + } + if held { + dev_info_ratelimited!(self.dev, "hibernation: holding SEP requests and xART writes\n"); + } else { + self.xart_held_logged.store(false, Relaxed); + dev_info_ratelimited!( + self.dev, + "hibernation: ended without a cold boot, SEP session kept\n" + ); + } + } + + /// Whether this kernel registered with the enclave (a kernel about to + /// load a hibernation image must not have). + pub(crate) fn attached(&self) -> bool { + self.registered.load(Relaxed) && !self.session_gone.load(Relaxed) + } + + /// The boot loader booted the enclave anew under the hibernation image. + /// Everything this session holds there is gone: refuse every message, so + /// that the fresh sepOS sees nothing before the new registration, and let + /// `pm_shim.c` probe the device again. Returns whether to probe again. + pub(crate) fn lose_session(&self) -> bool { + if self.session_gone.xchg(true, Relaxed) { + return false; + } + dev_info_ratelimited!( + self.dev, + "resume from disk on a cold boot: the boot loader restarted the enclave; dropping this session and probing again\n" + ); + true + } + pub(crate) fn sleep_gate(&self) -> pm::SleepGate { pm::SleepGate::from_u32(self.sleep_gate.load(Acquire)) } @@ -1548,6 +1607,15 @@ impl SepData { pm::SleepGate::Open => {} _ => return Err(EBUSY), } + // Booted anew under a hibernation image: nothing of the old session may + // reach it, the sleep request included (see `lose_session`). + if self.session_gone.load(Relaxed) { + dev_warn_ratelimited!( + self.dev, + "the enclave was restarted under a resume from disk, so it isn't told of this sleep\n" + ); + return Ok(()); + } // Do not commit a running enclave whose sleep protocol is not ready. if self.phase.load(Relaxed) != PHASE_READY || !self.endpoint_present(proto::EP_CONTROL) { dev_warn_ratelimited!( @@ -1875,12 +1943,16 @@ impl SepData { /// way to sleep waits for it. It must not then write its request into /// the suspend. Lock order: mailbox, then control. fn send_entropy_request(&self, msg: Message) -> Result<()> { - if self.shutting_down.load(Relaxed) { + if self.shutting_down.load(Relaxed) || self.session_gone.load(Relaxed) { return Err(ENODEV); } let mbox = self.mbox.lock(); let mbox = mbox.as_ref().ok_or(ENODEV)?; - if !self.sleep_gate().may_request() || !self.control.lock().entropy_may_send() { + // Under the mailbox lock, as in `send`. + if !self.sleep_gate().may_request() + || self.session_held.load(Relaxed) + || !self.control.lock().entropy_may_send() + { return Err(EAGAIN); } Self::ring(mbox, msg) @@ -2158,6 +2230,21 @@ impl SepData { } fn service_xarm(&self, req: &crate::xarm::XarmRequest) { + // Behind a hibernation image nothing may change: the restored kernel + // would hold a stale view of the gigalocker and of the host files. + // Refused like a read-only gigalocker; reads are still served. + if xarm::is_write(req.opcode) && self.session_held.load(Relaxed) { + if !self.xart_held_logged.xchg(true, Relaxed) { + dev_warn_ratelimited!( + self.dev, + "xART: refused the enclave's write (op 0x{:02x}) during a hibernation\n", + req.opcode + ); + } + self.fail_xarm(req.tag); + return; + } + let want = req.length as usize; if want > OOL_SIZE_XARM { self.fail_xarm(req.tag); @@ -3639,6 +3726,15 @@ impl Drop for SepDriver { } } +/// Dropped after the driver registration (fields drop in order): a probe +/// queued after a resume from disk must not run once the module goes away. +struct ReprobeGuard; + +impl Drop for ReprobeGuard { + fn drop(&mut self) { + pm::exit(); + } +} #[pin_data] struct SepModule { @@ -3647,6 +3743,10 @@ struct SepModule { #[pin] _driver: driver::Registration>, + + // After the driver: once it is unregistered no notifier can queue a + // reprobe, and the one queued before is cancelled here. + _reprobe: ReprobeGuard, } impl kernel::InPlaceModule for SepModule { @@ -3675,6 +3775,8 @@ impl kernel::InPlaceModule for SepModule { ::NAME, module, ), + + _reprobe: ReprobeGuard, }) } } diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 9cc33b48e334a..c42c68da4d6f5 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -21,6 +21,7 @@ int apple_pmp_report_ane_perf(const struct device_node *np, struct device *owner, unsigned int level, bool *held); int apple_pmp_report_wait_supplier_ready(struct device *supplier, unsigned int timeout_ms); int apple_pmp_report_wait_running(struct device_node *report_np, unsigned int timeout_ms); +int apple_pmp_report_wait_handed(unsigned int timeout_ms); /* Caller holds the PMP owner's vote lock and keeps the supplier bound. */ int apple_pmp_report_check_t6030_gfx(struct device *supplier); int apple_pmp_report_set_t6030_gfx(struct device *supplier, bool enable); @@ -113,6 +114,11 @@ static inline int apple_pmp_report_wait_running(struct device_node *report_np, return -ENODEV; } +static inline int apple_pmp_report_wait_handed(unsigned int timeout_ms) +{ + return 0; +} + static inline int apple_pmp_report_check_t6030_gfx(struct device *supplier) { return -ENODEV; diff --git a/include/linux/soc/apple/pmp.h b/include/linux/soc/apple/pmp.h index 8663ca195febe..80617a83cbc21 100644 --- a/include/linux/soc/apple/pmp.h +++ b/include/linux/soc/apple/pmp.h @@ -11,6 +11,13 @@ int apple_pmp_link_device(struct device *consumer); int apple_pmp_set_device_power(u8 command, u16 device_id, u32 enabled); /* After the acknowledged AGX vote and successful GPU firmware startup. */ int apple_pmp_t6030_gpu_ready(void); +/* + * How many resumes from disk the PMP found halted by the boot loader, i.e. + * cold boots of the SoC under a hibernation image, or -ENODEV without a + * bound PMP. Counted in the PMP's own PM_POST_HIBERNATION notifier + * (priority 10), so a lower-priority notifier sees this resume counted. + */ +int apple_pmp_cold_restarts(void); #else static inline int apple_pmp_link_device(struct device *consumer) { @@ -24,6 +31,10 @@ static inline int apple_pmp_t6030_gpu_ready(void) { return -ENODEV; } +static inline int apple_pmp_cold_restarts(void) +{ + return -ENODEV; +} #endif /* Registration is private to the PMP Rust driver and its C bridge. */ bool apple_pmp_keep_bound(void); @@ -49,4 +60,8 @@ struct apple_pmp_commit_stop { }; int apple_pmp_sleep_prepare(const void *data, struct apple_pmp_commit_stop *stop); +/* Rebuild the firmware session after a resume from disk (PM_POST_HIBERNATION). */ +int apple_pmp_restore(const void *data); +/* Called by the restore when it finds the PMP halted (a cold boot). */ +void apple_pmp_note_cold_restart(void); #endif diff --git a/tools/asahi/display26-tests.py b/tools/asahi/display26-tests.py index f0c993fe03624..8c513c4a8f544 100644 --- a/tools/asahi/display26-tests.py +++ b/tools/asahi/display26-tests.py @@ -87,7 +87,7 @@ def main(): '-I'+str(tmp), '-I'+str(ROOT), str(tmp/'profile.c'), '-o', str(tmp/'profile')) run(str(tmp/'profile')) # Platform functions are mocked; the bridge's actual decisions and cleanup execute. - for name in ('device', 'export', 'mutex', 'of'): + for name in ('atomic', 'device', 'export', 'limits', 'mutex', 'notifier', 'of'): write(tmp, 'linux/'+name+'.h', '#include "mock.h"\n') write(tmp, 'linux/errno.h', '#define EPROBE_DEFER 517\n#define ENODEV 19\n#define EINVAL 22\n#define EBUSY 16\n#define EIO 5\n#define ETIMEDOUT 110\n#define EAGAIN 11\n') write(tmp, 'linux/kconfig.h', '#define IS_REACHABLE(x) 1\n#define IS_ENABLED(x) 1\n') @@ -95,9 +95,12 @@ def main(): write(tmp, 'linux/pm.h', '#include "mock.h"\n' 'struct dev_pm_ops { int (*suspend_late)(struct device *); int (*resume_early)(struct device *); };\n' '#define LATE_SYSTEM_SLEEP_PM_OPS(s, r) .suspend_late = (s), .resume_early = (r),\n') - write(tmp, 'linux/suspend.h', '#include \n' + write(tmp, 'linux/suspend.h', '#include "mock.h"\n' 'static inline bool pm_suspend_via_firmware(void) { return false; }\n' - 'static inline bool pm_resume_via_firmware(void) { return false; }\n') + 'static inline bool pm_resume_via_firmware(void) { return false; }\n' + '#define PM_POST_HIBERNATION 0x0002\n#define PM_POST_RESTORE 0x0006\n' + 'int register_pm_notifier(struct notifier_block *nb);\n' + 'int unregister_pm_notifier(struct notifier_block *nb);\n') # The bridge's sleep-commit stop: MMIO, RTKit and the sleep driver, reached only in a suspend. write(tmp, 'linux/io.h', '#include "mock.h"\n' 'static inline u32 readl_relaxed(const volatile void __iomem *a) { assert(!"no system sleep here"); return 0; }\n' @@ -114,12 +117,14 @@ def main(): '\t\t\t\t\t int (*f)(void *), void *x) { assert(!"no system sleep here"); return -EIO; }\n' 'static inline void apple_sleep_coproc_set_ready(struct device *d, bool r) { assert(!"no system sleep here"); }\n' 'static inline bool apple_sleep_coproc_stopped(struct device *d) { assert(!"no system sleep here"); return false; }\n' - 'static inline bool apple_sleep_commit_stops_coprocs(void) { assert(!"no system sleep here"); return false; }\n') + 'static inline bool apple_sleep_commit_stops_coprocs(void) { assert(!"no system sleep here"); return false; }\n' + 'static inline bool apple_sleep_power_loss(void) { assert(!"no system sleep here"); return false; }\n') write(tmp, 'linux/soc/apple/pmp-display.h', '#include \nbool apple_pmp_display_retained_board(void);\n') write(tmp, 'mock.h', '''#pragma once #include #include #include +#include #include #define dev_err(...) ((void)0) #define dev_warn(...) ((void)0) @@ -142,6 +147,14 @@ def main(): #define guard(x) struct mutex_guard JOIN(g,__LINE__) __attribute__((cleanup(leave))) = enter #define scoped_guard(x,m) for(bool once=true;once;) for(struct mutex_guard g __attribute__((cleanup(leave)))=enter(m);once;once=false) #define EXPORT_SYMBOL_GPL(x) +typedef struct { int counter; } atomic_t; +#define ATOMIC_INIT(i) { (i) } +static inline void atomic_inc(atomic_t *v){v->counter++;} +static inline int atomic_read(const atomic_t *v){return v->counter;} +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x,v) ((x)=(v)) +struct notifier_block { int (*notifier_call)(struct notifier_block*,unsigned long,void*); int priority; }; +#define NOTIFY_DONE 0 #define DL_FLAG_AUTOREMOVE_CONSUMER 1 static struct device_node *phandle; static bool link_fail; static struct device_node *of_parse_phandle(struct device_node*n,const char*k,int index){return phandle;} @@ -168,6 +181,11 @@ def main(): int apple_pmp_sleep(const void*data){assert(!"no system sleep here");return -EIO;} int apple_pmp_sleep_prepare(const void*data,struct apple_pmp_commit_stop*stop){assert(!"no system sleep here");return -EIO;} int apple_pmp_wake(const void*data){assert(!"no system sleep here");return -EIO;} +int apple_pmp_restore(const void*data){assert(!"no hibernation here");return -EIO;} +/* The PM notifier chain: one block, registered while the PMP is. */ +static struct notifier_block *pm_notifier; +int register_pm_notifier(struct notifier_block*nb){assert(!pm_notifier);pm_notifier=nb;return 0;} +int unregister_pm_notifier(struct notifier_block*nb){assert(pm_notifier==nb);pm_notifier=NULL;return 0;} int main(void){ struct device_node n={1},wrong={2}; struct device supplier={&n,true,0},consumer={NULL,false,0}; int data; assert(apple_pmp_set_device_power(0xf,8,1)==-ENODEV); @@ -190,7 +208,7 @@ def main(): assert(apple_pmp_set_device_power(0xf,5,1)==0);assert(calls==3); assert(apple_pmp_set_device_power(0xe,5,0)==0);assert(calls==4); apple_pmp_unregister(&wrong);assert(pmp_data==&data); - apple_pmp_unregister(&data);assert(!pmp_data && !pmp_device); + apple_pmp_unregister(&data);assert(!pmp_data && !pmp_device);assert(!pm_notifier); assert(apple_pmp_set_device_power(0xf,8,1)==-ENODEV); puts("production PMP bridge supplier/ready/ack controls: PASS"); } From 329b95c47acb55f95acba7b0d161a40f03037787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:04 +0100 Subject: [PATCH 164/269] HID: dockchannel, iio: aop_als: J616s keyboard, trackpad and light sensor across sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Re-open the DockChannel HID transport and the AOP light sensor after S4 and S2R. Signed-off-by: František Balázsy --- drivers/hid/dockchannel-hid/dockchannel-hid.c | 409 ++++++++++++++++-- drivers/iio/common/aop_sensors/aop_als.rs | 164 ++++++- 2 files changed, 531 insertions(+), 42 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 89d24f77081b0..292aeb8d78c1b 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -208,6 +209,11 @@ struct dockchannel_hid { bool suspended; /* The firmware was told of a sleep and has not been told of the wake */ bool host_asleep; + /* Boot kernel of a resume from disk: input handed to the image. */ + bool hib_quiesced; + u32 hib_off_mask; + /* After taking the ring over: one resync to the producer allowed. */ + bool ring_resync; /* Held from probe on when the multi-touch interface uses power method 2 */ struct gpio_desc *afe_reset; @@ -743,6 +749,50 @@ static int dchid_request_gpio(struct dchid_iface *iface) return 0; } +/* Upload the interface firmware, then reset the interface to run it. */ +static int dchid_load_firmware(struct dchid_iface *iface, void *fw, size_t size) +{ + int ret; + + /* Send firmware to the device */ + dev_dbg(iface->dchid->dev, "Sending firmware for %s\n", iface->name); + /* Retained MTP must see AFE reset throughout firmware upload. */ + if (iface->dchid->inherited_mtp && + iface->power_method == DCHID_POWER_METHOD_2) { + if (iface->firmware_managed_reset) + return -EINVAL; + ret = gpiod_set_value_cansleep(iface->dchid->afe_reset, 1); + if (ret < 0) + return ret; + fsleep(50 * USEC_PER_MSEC); + } + ret = dchid_send_firmware(iface, fw, size); + if (ret < 0) { + dev_err(iface->dchid->dev, "Failed to send %s firmware\n", iface->name); + return ret; + } + + /* After loading firmware, multi-touch needs a reset */ + dev_dbg(iface->dchid->dev, "Resetting %s\n", iface->name); + if (iface->power_method == DCHID_POWER_METHOD_2) { + /* The retained session is already off while reset is asserted. */ + if (!iface->dchid->inherited_mtp) { + ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_OFF); + if (ret < 0) + return ret; + } + ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_ON); + if (ret < 0) + return ret; + } else { + ret = dchid_reset_interface(iface, DCHID_POWER_STATE_OFF); + /* After a failed AFE power-off the trackpad stays off: no retry. */ + if (ret >= 0 || !iface->dchid->afe_smc || iface->index != 1) + dchid_reset_interface(iface, DCHID_POWER_STATE_ON); + } + return 0; +} + static int dchid_start_interface(struct dchid_iface *iface) { void *fw; @@ -775,45 +825,9 @@ static int dchid_start_interface(struct dchid_iface *iface) /* Only multi-touch has firmware */ if (fw && size) { - - /* Send firmware to the device */ - dev_dbg(iface->dchid->dev, "Sending firmware for %s\n", iface->name); - /* Retained MTP must see AFE reset throughout firmware upload. */ - if (iface->dchid->inherited_mtp && - iface->power_method == DCHID_POWER_METHOD_2) { - if (iface->firmware_managed_reset) { - ret = -EINVAL; - goto err; - } - ret = gpiod_set_value_cansleep(iface->dchid->afe_reset, 1); - if (ret < 0) - goto err; - fsleep(50 * USEC_PER_MSEC); - } - ret = dchid_send_firmware(iface, fw, size); - if (ret < 0) { - dev_err(iface->dchid->dev, "Failed to send %s firmware\n", iface->name); + ret = dchid_load_firmware(iface, fw, size); + if (ret < 0) goto err; - } - - /* After loading firmware, multi-touch needs a reset */ - dev_dbg(iface->dchid->dev, "Resetting %s\n", iface->name); - if (iface->power_method == DCHID_POWER_METHOD_2) { - /* The retained session is already off while reset is asserted. */ - if (!iface->dchid->inherited_mtp) { - ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_OFF); - if (ret < 0) - goto err; - } - ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_ON); - if (ret < 0) - goto err; - } else { - ret = dchid_reset_interface(iface, DCHID_POWER_STATE_OFF); - /* After a failed AFE power-off the trackpad stays off: no retry. */ - if (ret >= 0 || !iface->dchid->afe_smc || iface->index != 1) - dchid_reset_interface(iface, DCHID_POWER_STATE_ON); - } } return 0; @@ -1531,11 +1545,23 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid) dchid_ring_copy(dchid, dchid->pkt_buf, consumer, sizeof(struct dchid_hdr)); size = dchid_frame_size((void *)dchid->pkt_buf); + if (size < 0 && dchid->ring_resync) + goto resync; if (size < 0) return size; if (available < size) return 0; dchid_ring_copy(dchid, dchid->pkt_buf, consumer, size); + if (dchid->ring_resync) { + u32 checksum; + + if (!dchid_verify_checksum((void *)dchid->pkt_buf, + dchid->pkt_buf + sizeof(struct dchid_hdr), + &checksum)) + goto resync; + /* The first good frame shows the ring is in step. */ + dchid->ring_resync = false; + } dchid_dispatch(dchid, (void *)dchid->pkt_buf, dchid->pkt_buf + sizeof(struct dchid_hdr), false); consumer += size; @@ -1548,6 +1574,21 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid) cond_resched(); } return 0; + +resync: + /* + * Only right after a resume from disk took the ring over: frames the + * firmware wrote while the image was being restored are lost. Drop + * what is between the consumer and the producer once. + */ + dchid->ring_resync = false; + dev_warn(dchid->dev, "receive ring out of step after resume from disk: skipping %#x..%#x\n", + consumer, producer); + dchid->ring_read = producer; + /* As in the loop: frame reads complete before the producer may reuse them. */ + mb(); + WRITE_ONCE(indices[1], producer); + return 0; } static void dchid_handle_packet(void *cookie, size_t avail) @@ -1559,9 +1600,12 @@ static void dchid_handle_packet(void *cookie, size_t avail) if (READ_ONCE(dchid->stopping)) return; + /* Woken with less than a header in the FIFO: nothing to read yet. */ + if (avail < sizeof(hdr)) + goto done; if (dockchannel_recv(dchid->dc, &hdr, sizeof(hdr)) != sizeof(hdr)) { dev_err(dchid->dev, "Read failed (header)\n"); - return; + goto done; } if (hdr.hdr_len != sizeof(hdr)) { dev_err(dchid->dev, "Bad header length %d\n", hdr.hdr_len); @@ -1913,8 +1957,295 @@ static int dockchannel_hid_resume(struct device *dev) return ret; } +#ifdef CONFIG_HIBERNATION +/* + * Resume from disk with the MTP started by the boot kernel (for instance to + * type a disk passphrase in its initramfs). The boot kernel quiesces its + * devices before it jumps into the image; the MTP firmware is not stopped + * (it does not come back from RTKit QUIESCED), so the restored kernel takes + * over the running instance, as the RTKit helper does for the RTKit session: + * + * boot kernel (.freeze while apple_rtkit_hib_restoring()): + * the input interfaces (keyboard; multi-touch once its firmware runs) are + * powered off with the interface power-state commands, so that the + * firmware sends (and DMAs) as little as possible while + * the image is restored; then receive stops, the receive ring is drained + * and its position is left for the image with the ring's IOVA. + * + * restored kernel (.restore), only if the helper took the RTKit session + * over and the ring sits at the same IOVA in both kernels: + * the ring indices are set to the position the boot kernel stopped at + * (or, if the boot kernel never registered the ring, the ring is + * registered again from its start), receive restarts, and the interfaces + * the boot kernel powered off are powered on again. A multi-touch + * interface the image had started but the boot kernel had not gets its + * firmware as on a first open. + * + * Otherwise .restore keeps the plain behaviour: a co-processor the boot + * loader left halted stops the transport until userspace reloads the + * drivers, one that runs but cannot be adopted is left as it is. + * Only the ring transport (T6041 / J616s) is handled. + */ +struct dchid_hib { + u32 version; + u32 ring_size; + u64 ring_dma; + u32 ring_pos; + u32 off_mask; + u32 started_mask; + u32 flags; +}; + +#define DCHID_HIB_VERSION 2 +/* The boot kernel gave its firmware the receive ring. */ +#define DCHID_HIB_RING_REGISTERED BIT(0) + +static void dchid_hib_tag(struct dockchannel_hid *dchid, char *tag, size_t len) +{ + snprintf(tag, len, "dchid:%s", dev_name(dchid->dev)); +} + +static bool dchid_hib_input(struct dchid_iface *iface) +{ + if (!strcmp(iface->name, "keyboard")) + return true; + /* Without its firmware the multi-touch interface was never powered. */ + return !strcmp(iface->name, "multi-touch") && iface->starting; +} + +/* The power sequences of dchid_load_firmware(), one direction at a time. */ +static int dchid_hib_set_power(struct dchid_iface *iface, u8 state) +{ + if (iface->power_method == DCHID_POWER_METHOD_2) + return dchid_pm2_set_power(iface, state); + return dchid_reset_interface(iface, state); +} + +static void dchid_hib_quiesce(struct dockchannel_hid *dchid) +{ + struct dchid_hib rec = { + .version = DCHID_HIB_VERSION, + .ring_size = DCHID_RING_SIZE, + .ring_dma = dchid->ring_dma, + }; + u32 *indices = dchid->ring; + char tag[32]; + int i, ret; + + for (i = 0; i < MAX_INTERFACES; i++) { + struct dchid_iface *iface = dchid->ifaces[i]; + + if (!iface || !iface->hid) + continue; + if (iface->starting) + rec.started_mask |= BIT(i); + if (!dchid_hib_input(iface)) + continue; + ret = dchid_hib_set_power(iface, DCHID_POWER_STATE_OFF); + if (ret < 0) { + dev_warn(dchid->dev, "resume from disk: %s not powered off: %d\n", + iface->name, ret); + continue; + } + rec.off_mask |= BIT(i); + } + + if (READ_ONCE(dchid->ring_registered) && !dchid->ring_error) + rec.flags |= DCHID_HIB_RING_REGISTERED; + + /* The power-off acknowledgments came through the ring: stop now. */ + dockchannel_await(dchid->dc, NULL, NULL, 0); + ret = dchid_drain_ring(dchid); + if (ret < 0) + dev_warn(dchid->dev, "resume from disk: final ring drain failed: %d\n", ret); + rec.ring_pos = dchid->ring_read; + if (READ_ONCE(indices[0]) != rec.ring_pos) + dev_warn(dchid->dev, "resume from disk: ring not empty at hand-off (%#x, producer %#x)\n", + rec.ring_pos, READ_ONCE(indices[0])); + + dchid->hib_quiesced = true; + dchid->hib_off_mask = rec.off_mask; + dchid_hib_tag(dchid, tag, sizeof(tag)); + ret = apple_rtkit_hib_handoff_put(tag, &rec, sizeof(rec)); + if (ret) + dev_warn(dchid->dev, "resume from disk: no hand-off record for the image: %d\n", + ret); + else + dev_info(dchid->dev, "resume from disk: input left for the image (interfaces off %#x, ring at %#x)\n", + rec.off_mask, rec.ring_pos); +} + +static int dockchannel_hid_freeze(struct device *dev) +{ + struct dockchannel_hid *dchid = dev_get_drvdata(dev); + int ret; + + ret = dockchannel_hid_suspend(dev); + if (ret) + return ret; + if (dchid->use_ring && apple_rtkit_hib_restoring()) + dchid_hib_quiesce(dchid); + return 0; +} + +static void dchid_hib_power_on(struct dockchannel_hid *dchid, u32 mask) +{ + int i, ret; + + for (i = 0; i < MAX_INTERFACES; i++) { + struct dchid_iface *iface = dchid->ifaces[i]; + + if (!(mask & BIT(i)) || !iface) + continue; + ret = dchid_hib_set_power(iface, DCHID_POWER_STATE_ON); + if (ret < 0) + dev_err(dchid->dev, "resume from disk: %s not powered on: %d\n", + iface->name, ret); + } +} + +static int dockchannel_hid_thaw(struct device *dev) +{ + struct dockchannel_hid *dchid = dev_get_drvdata(dev); + char tag[32]; + + /* The jump into the image failed: this kernel keeps the input. */ + if (dchid->hib_quiesced) { + dchid->hib_quiesced = false; + dchid_hib_tag(dchid, tag, sizeof(tag)); + apple_rtkit_hib_handoff_drop(tag); + dockchannel_await(dchid->dc, dchid_handle_packet, dchid, + sizeof(struct dchid_hdr)); + dchid_hib_power_on(dchid, dchid->hib_off_mask); + } + return dockchannel_hid_resume(dev); +} + +static void dchid_hib_take_over(struct dockchannel_hid *dchid, const struct dchid_hib *rec) +{ + const u32 capacity = DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE; + u32 *indices = dchid->ring; + bool reregister; + int i, ret; + + if (rec->version != DCHID_HIB_VERSION || rec->ring_size != DCHID_RING_SIZE || + rec->ring_dma != dchid->ring_dma || rec->ring_pos >= capacity || + !IS_ALIGNED(rec->ring_pos, 4)) { + dev_err(dchid->dev, "resume from disk: boot kernel ring %#llx+%#x@%#x does not match the image's %pad: input not taken over\n", + rec->ring_dma, rec->ring_size, rec->ring_pos, &dchid->ring_dma); + return; + } + + /* + * A boot kernel that started no interface (nothing opened the trackpad) + * never gave its firmware the receive ring, which this kernel had + * registered with its own: register it again, from the start, as a + * first open does. Otherwise the firmware goes on at the position the + * boot kernel drained to; the ring memory and indices it wrote were in + * the boot kernel's RAM. + */ + reregister = !(rec->flags & DCHID_HIB_RING_REGISTERED) && + READ_ONCE(dchid->ring_registered); + dockchannel_await(dchid->dc, NULL, NULL, 0); + mutex_lock(&dchid->ring_mutex); + if (reregister) { + dchid->ring_read = 0; + WRITE_ONCE(indices[0], 0); + WRITE_ONCE(indices[1], 0); + dchid->ring_resync = false; + dchid->ring_error = 0; + WRITE_ONCE(dchid->ring_registered, false); + } else { + dchid->ring_read = rec->ring_pos; + WRITE_ONCE(indices[0], rec->ring_pos); + WRITE_ONCE(indices[1], rec->ring_pos); + dchid->ring_resync = true; + } + mutex_unlock(&dchid->ring_mutex); + /* Indices visible to the firmware before it is told anything. */ + wmb(); + dockchannel_await(dchid->dc, dchid_handle_packet, dchid, + sizeof(struct dchid_hdr)); + + if (reregister) { + ret = dchid_register_ring(dchid); + if (ret < 0) + dev_err(dchid->dev, "resume from disk: receive ring not registered with the boot kernel's firmware: %d\n", + ret); + else + dev_info(dchid->dev, "resume from disk: receive ring registered with the boot kernel's firmware\n"); + } + + dchid_hib_power_on(dchid, rec->off_mask); + + /* Started here but not in the boot kernel: firmware as on a first open. */ + for (i = 0; i < MAX_INTERFACES; i++) { + struct dchid_iface *iface = dchid->ifaces[i]; + void *fw; + size_t size; + + if (!iface || !iface->starting || (rec->started_mask & BIT(i))) + continue; + ret = dchid_get_firmware(iface, &fw, &size); + if (!ret && fw && size) + ret = dchid_load_firmware(iface, fw, size); + if (ret < 0) + dev_err(dchid->dev, "resume from disk: %s not restarted: %d\n", + iface->name, ret); + } + + dev_info(dchid->dev, "resume from disk: took over the input the boot kernel started (interfaces on %#x, ring at %#x)\n", + rec->off_mask, rec->ring_pos); +} + +static int dockchannel_hid_restore(struct device *dev) +{ + struct dockchannel_hid *dchid = dev_get_drvdata(dev); + struct dchid_hib rec; + char tag[32]; + int ret; + + dchid->hib_quiesced = false; + dchid_hib_tag(dchid, tag, sizeof(tag)); + ret = apple_rtkit_hib_handoff_take(tag, &rec, sizeof(rec)); + if (ret == sizeof(rec)) { + if (!dchid->use_ring) + dev_warn(dev, "resume from disk: hand-off without a receive ring: input not taken over\n"); + else if (!apple_rtkit_helper_readopted(dchid->helper_link->supplier)) + dev_warn(dev, "resume from disk: the RTKit helper did not take the session over: input not taken over\n"); + else + dchid_hib_take_over(dchid, &rec); + } else if (ret != -ENOENT) { + dev_warn(dev, "resume from disk: invalid hand-off record: %d\n", ret); + } + + /* + * The boot loader halted the co-processor and the boot kernel did not + * start it: the firmware this kernel talked to is gone, and the FIFO + * never answers again. Stop the transport, so that receive does not + * wait for frames and the HID core's requests fail at once, until the + * drivers are probed again (which starts the co-processor). + */ + if (apple_rtkit_helper_halted_underneath(dchid->helper_link->supplier)) { + WRITE_ONCE(dchid->stopping, true); + dockchannel_await(dchid->dc, NULL, NULL, 0); + dev_info(dev, "resume from disk: co-processor halted underneath: input stopped until the drivers are reloaded\n"); + } + return dockchannel_hid_resume(dev); +} + +static const struct dev_pm_ops dockchannel_hid_pm_ops = { + .suspend = dockchannel_hid_suspend, + .resume = dockchannel_hid_resume, + .freeze = dockchannel_hid_freeze, + .thaw = dockchannel_hid_thaw, + .poweroff = dockchannel_hid_suspend, + .restore = dockchannel_hid_restore, +}; +#else static DEFINE_SIMPLE_DEV_PM_OPS(dockchannel_hid_pm_ops, dockchannel_hid_suspend, dockchannel_hid_resume); +#endif static const struct of_device_id dockchannel_hid_of_match[] = { { .compatible = "apple,dockchannel-hid" }, diff --git a/drivers/iio/common/aop_sensors/aop_als.rs b/drivers/iio/common/aop_sensors/aop_als.rs index e64f7ff26a6e0..87290b2e175f3 100644 --- a/drivers/iio/common/aop_sensors/aop_als.rs +++ b/drivers/iio/common/aop_sensors/aop_als.rs @@ -4,15 +4,25 @@ //! //! Copyright (C) The Asahi Linux Contributors +use core::{ + ffi::{c_int, c_void}, + ptr, +}; + use kernel::{ bindings, c_str, - device::Core, + device::{self, Core}, firmware::Firmware, iio::common::aop_sensors::{AopSensorData, IIORegistration, MessageProcessor, MICRO}, module_platform_driver, of, platform, prelude::*, soc::apple::aop::{EPICService, FakehidRegistration, AOP}, - sync::Arc, + sync::{ + aref::ARef, + atomic::{Atomic, Relaxed}, + Arc, + }, + types::Opaque, }; const EPIC_SUBTYPE_GET_AOP_PROPERTY: u16 = 0xa; @@ -146,6 +156,149 @@ fn set_als_property(aop: &dyn AOP, svc: &EPICService, tag: u32, data: &[u8]) -> aop.epic_call(svc, EPIC_SUBTYPE_SET_ALS_PROPERTY, &buf) } +/// Stops the reports around system sleep and starts them again after. +/// +/// The AOP mailbox interrupt stays enabled in suspend-to-idle, so every +/// report ends the CPUs' idle period: at the 200 ms interval the sensor +/// woke the system five times a second. A zero interval stops the reports; +/// The platform writes one to its AOP sensors when the system sleeps and the +/// interval they had when it wakes. +/// +/// Only suspend is handled. A hibernation powers the system off, and after +/// a resume from disk the AOP core may have restarted the co-processor and +/// probed this driver again, which then starts the reports itself. +#[repr(C)] +struct PmHook { + /// First, so that the callback finds the hook from it. + nb: Opaque, + dev: ARef, + aop: Arc, + service: EPICService, + registered: Atomic, +} + +/// The PM notifier callback. +/// +/// # Safety +/// +/// `nb` is the notifier block of a registered [`PmHook`]. +unsafe extern "C" fn pm_event( + nb: *mut bindings::notifier_block, + action: usize, + _data: *mut c_void, +) -> c_int { + // SAFETY: `nb` is the first field of a registered `PmHook`. + let hook = unsafe { &*nb.cast::() }; + let interval = match action as u32 { + bindings::PM_SUSPEND_PREPARE => 0, + bindings::PM_POST_SUSPEND => ALS_INTERVAL_US, + _ => return bindings::NOTIFY_DONE as c_int, + }; + // As at probe, only the transport can fail; the sensor's status word is + // not an error (see the property comments above). + match set_als_property( + hook.aop.as_ref(), + &hook.service, + ALS_PROP_INTERVAL, + &interval.to_le_bytes(), + ) { + Ok(status) => dev_dbg!( + hook.dev, + "interval = {} for sleep: status {:#x}\n", + interval, + status + ), + Err(e) => dev_warn!(hook.dev, "reports not switched for sleep: {:?}\n", e), + } + bindings::NOTIFY_OK as c_int +} + +/// # Safety +/// +/// `nb` stays valid and pinned until [`pm_unregister`] is called on it. +#[cfg(CONFIG_PM_SLEEP)] +unsafe fn pm_register(nb: *mut bindings::notifier_block) -> c_int { + // SAFETY: By the caller's contract. + unsafe { bindings::register_pm_notifier(nb) } +} + +/// # Safety +/// +/// `nb` was registered by [`pm_register`]. +#[cfg(CONFIG_PM_SLEEP)] +unsafe fn pm_unregister(nb: *mut bindings::notifier_block) { + // SAFETY: By the caller's contract. + unsafe { bindings::unregister_pm_notifier(nb) }; +} + +/// Without system sleep there is nothing to switch. +/// +/// # Safety +/// +/// None; the signature matches the one with system sleep. +#[cfg(not(CONFIG_PM_SLEEP))] +unsafe fn pm_register(_nb: *mut bindings::notifier_block) -> c_int { + 0 +} + +/// # Safety +/// +/// None; the signature matches the one with system sleep. +#[cfg(not(CONFIG_PM_SLEEP))] +unsafe fn pm_unregister(_nb: *mut bindings::notifier_block) {} + +// SAFETY: The notifier block is only touched by the PM core, which +// serializes registration, removal and the callbacks itself. +unsafe impl Send for PmHook {} +// SAFETY: See above; the other fields are `Sync`. +unsafe impl Sync for PmHook {} + +impl PmHook { + fn new( + dev: &device::Device, + aop: Arc, + service: EPICService, + ) -> Result>> { + let hook = KBox::pin( + Self { + nb: Opaque::new(bindings::notifier_block { + notifier_call: Some(pm_event), + next: ptr::null_mut(), + priority: 0, + }), + dev: dev.into(), + aop, + service, + registered: Atomic::new(false), + }, + GFP_KERNEL, + )?; + // SAFETY: The block is pinned inside the hook, which unregisters it + // before it goes. + let ret = unsafe { pm_register(hook.nb.get()) }; + if ret < 0 { + return Err(Error::from_errno(ret)); + } + hook.registered.store(true, Relaxed); + Ok(hook) + } + + /// Returns once no callback is running any more; repeated calls do + /// nothing. + fn unregister(&self) { + if self.registered.xchg(false, Relaxed) { + // SAFETY: `nb` was registered by `new()`. + unsafe { pm_unregister(self.nb.get()) }; + } + } +} + +impl Drop for PmHook { + fn drop(&mut self) { + self.unregister(); + } +} + /// Converts the bits of an IEEE 754 single to microlux, rounding down. /// Negative values clamp to zero; NaN and the infinities carry no reading. /// Values beyond `MAX_MICRO` saturate when they are stored. @@ -206,7 +359,9 @@ enum AlsIio { } struct IIOAopAlsDriver { - /// Declared first: the subscription ends before the IIO device goes. + /// Fields are declared in teardown order: sleep handling first, then the + /// subscription, which ends before the IIO device goes. + pm: Pin>, listener: FakehidRegistration, _iio: AlsIio, } @@ -285,13 +440,16 @@ impl platform::Driver for IIOAopAlsDriver { }, ) }; + let pm = PmHook::new(dev, adata, service)?; Ok(IIOAopAlsDriver { + pm, listener, _iio: iio, }) } fn unbind(_dev: &platform::Device>, this: Pin<&Self>) { + this.pm.unregister(); this.listener.unregister(); } } From 3589dbdb9636a864c1355efed90f6c7eef1b872c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:27:26 +0100 Subject: [PATCH 165/269] soc: apple: pmp: keep the T6041 PMP's data SRAM across S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a real T6040 S2R the PMP never answered its restart (endpoint discovery -62), and the GPU, whose wake asks the PMP, was lost with it: T6041 kept no SRAM across the sleep, its sram_backup was None. The ADT's segment-ranges place the PMP firmware in its SRAM region: __TEXT at 0x500700000 (0x61000 bytes), __DATA at 0x500761000 (0x7d000 bytes). Keep __DATA, the segment T8140's kept range covers, through the commit's existing copy and the wake's write-back. To tell whether the text, which nothing keeps, survives the sleep, the commit reads one text word per 4 KiB and the wake compares them before the restart (T6040 only, read only, inside __TEXT). Signed-off-by: František Balázsy --- drivers/soc/apple/pmp-export.c | 26 ++++++++++++++++++++++++++ drivers/soc/apple/pmp.rs | 8 +++++++- include/linux/soc/apple/pmp.h | 2 ++ tools/asahi/display26-tests.py | 3 ++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index 3cbc26a34bde3..f7532b89b4dad 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -200,6 +200,16 @@ EXPORT_SYMBOL_GPL(apple_pmp_t6030_gpu_ready); static struct apple_pmp_commit_stop pmp_commit; static bool pmp_commit_registered, pmp_commit_used; +/* + * T6040 bring-up: whether the firmware's text, which the sleep doesn't keep, + * survived it. One word per 4 KiB of the text, read at the commit and again + * before the restart; read only, inside the ADT's __TEXT segment. + */ +#define PMP_TEXT_STEP 0x1000 +#define PMP_TEXT_SAMPLES 128 +static u32 pmp_text_sample[PMP_TEXT_SAMPLES]; +static unsigned int pmp_text_samples; + /* Interrupts off, one CPU: what apple_pmp_sleep() does, through pointers taken before. */ static int apple_pmp_commit_stop_fn(void *unused) { @@ -212,6 +222,11 @@ static int apple_pmp_commit_stop_fn(void *unused) apple_rtkit_mailbox_recv_off(c->rtk); for (i = 0; c->sram && i < c->count; i++) c->words[i] = readl_relaxed(c->sram + 4 * i); + pmp_text_samples = 0; + if (c->sram && of_machine_is_compatible("apple,t6040")) + for (i = 0; i < c->offset / PMP_TEXT_STEP && i < PMP_TEXT_SAMPLES; i++) + pmp_text_sample[pmp_text_samples++] = + readl_relaxed(c->sram - c->offset + i * PMP_TEXT_STEP); /* Told to idle, it can't carry on whatever it answered: the sleep goes on. */ return ret ? 1 : 0; } @@ -320,6 +335,17 @@ static int apple_pmp_resume_early(struct device *dev) return 0; } pmp_asleep = false; + if (pmp_text_samples && pmp_commit.sram) { + unsigned int i, changed = 0; + + for (i = 0; i < pmp_text_samples; i++) + if (readl_relaxed(pmp_commit.sram - pmp_commit.offset + i * PMP_TEXT_STEP) != + pmp_text_sample[i]) + changed++; + dev_info(dev, "firmware text across the sleep: %u of %u sampled words changed\n", + changed, pmp_text_samples); + pmp_text_samples = 0; + } ret = apple_pmp_wake(pmp_data); if (ret) dev_err(dev, "firmware did not come back from sleep: %d\n", ret); diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index de76f122ef927..7ca819d0b03f1 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -610,6 +610,7 @@ impl PmpData { stop.sram = core::ptr::null_mut(); stop.words = core::ptr::null_mut(); stop.count = 0; + stop.offset = 0; if let Some(copy) = copy.as_mut() { stop.sram = MmioBackend::as_ptr(pmp_mmio) .cast::() @@ -617,6 +618,7 @@ impl PmpData { .cast(); stop.words = copy.words.as_mut_ptr(); stop.count = copy.words.len(); + stop.offset = copy.offset; } self.state.lock().sram = copy; Ok(()) @@ -977,11 +979,15 @@ const HW_CFG_T8140: PmpHwConfig = PmpHwConfig { /// T6041: the DART window starts at 1 TiB; the boot argument header is /// version 5. The firmware advertises endpoint 7 and does not acknowledge /// the IOP power state (-ETIME) until it is started and has its buffer. +/// SRAM kept across a system sleep (bring-up): the firmware's __DATA, as the +/// ADT's segment-ranges place it (0x500761000, 0x7d000 bytes, after __TEXT at +/// 0x500700000, 0x61000 bytes), the segment T8140's range covers too. +/// Without it the PMP never answered after a real S2R (endpoint discovery -62). const HW_CFG_T6041: PmpHwConfig = PmpHwConfig { dma_mask: Some(DmaMask::new::<42>()), check_bootargs_uuid: true, coreanalytics: Ep7::Always, - sram_backup: None, + sram_backup: Some((0x61000, 0x7d000)), }; /// T8142: the DART window starts at 1 TiB as on T8140. Its registry-v2 diff --git a/include/linux/soc/apple/pmp.h b/include/linux/soc/apple/pmp.h index 80617a83cbc21..48d3a5640786a 100644 --- a/include/linux/soc/apple/pmp.h +++ b/include/linux/soc/apple/pmp.h @@ -57,6 +57,8 @@ struct apple_pmp_commit_stop { void __iomem *sram; u32 *words; size_t count; + /* Bytes from the SRAM's start to @sram: the firmware's text before its kept data. */ + size_t offset; }; int apple_pmp_sleep_prepare(const void *data, struct apple_pmp_commit_stop *stop); diff --git a/tools/asahi/display26-tests.py b/tools/asahi/display26-tests.py index 8c513c4a8f544..e97d822de2f77 100644 --- a/tools/asahi/display26-tests.py +++ b/tools/asahi/display26-tests.py @@ -130,7 +130,8 @@ def main(): #define dev_warn(...) ((void)0) #define dev_dbg(...) ((void)0) #define dev_err_probe(d, e, ...) (e) -#define dev_info(...) ((void)0) +/* Takes its arguments, so that a value that is only logged counts as used. */ +static inline void dev_info(const void *dev, const char *fmt, ...) {} #include #define __iomem #include From 6ecabf6ac45f401fac6a10277ad6d2e3848ad31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:15:38 +0100 Subject: [PATCH 166/269] ASoC: cs42l84: keep the jack interrupt quiet across system sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The codec had no PM callbacks. Its level-low interrupt thread kept running over the sleep and read the codec over I2C while the bus and the codec went down, and after a T6040 S2R, which cuts the codec's power, the codec came back with its reset defaults: no plug detection set up, and its interrupt thread was found stuck after the wake. Disable the interrupt and the remote-button polling from the suspend to the resume. After a sleep that cut the power, reset the codec as probe does, write its registers back from the cache (LATCH_TO_VP first, as the plug detection set-up does) and report a jack that was seen before as gone: one still plugged in raises the plug interrupt again. After a sleep that kept it powered, poll a headset's buttons again. Signed-off-by: František Balázsy --- sound/soc/codecs/cs42l84.c | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/sound/soc/codecs/cs42l84.c b/sound/soc/codecs/cs42l84.c index 07a47433ef4d9..719ba89ea9045 100644 --- a/sound/soc/codecs/cs42l84.c +++ b/sound/soc/codecs/cs42l84.c @@ -23,7 +23,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -1256,6 +1258,77 @@ static int cs42l84_i2c_probe(struct i2c_client *i2c_client) return ret; } +/* + * System sleep. The interrupt is level low and its thread reads the codec over + * I2C, so it is off from the suspend until the codec is set up again. A real + * S2R cuts the codec's power: it comes back with its reset defaults, so its + * registers are written again from the cache, and a jack that was seen before + * is reported gone; one still plugged in raises the plug interrupt again. + */ +static int cs42l84_suspend(struct device *dev) +{ + struct cs42l84_private *cs42l84 = dev_get_drvdata(dev); + struct i2c_client *i2c_client = to_i2c_client(dev); + + if (i2c_client->irq) + disable_irq(i2c_client->irq); + scoped_guard(mutex, &cs42l84->irq_lock) + cs42l84_remote_stop(cs42l84); + cancel_delayed_work_sync(&cs42l84->remote_poll_work); + regcache_cache_only(cs42l84->regmap, true); + + return 0; +} + +static int cs42l84_resume(struct device *dev) +{ + struct cs42l84_private *cs42l84 = dev_get_drvdata(dev); + struct i2c_client *i2c_client = to_i2c_client(dev); + int ret = 0; + + regcache_cache_only(cs42l84->regmap, false); + if (pm_resume_via_firmware()) { + if (cs42l84->reset_gpio) { + gpiod_set_value_cansleep(cs42l84->reset_gpio, 0); + usleep_range(1000, 2000); + gpiod_set_value_cansleep(cs42l84->reset_gpio, 1); + } + usleep_range(CS42L84_BOOT_TIME_US, CS42L84_BOOT_TIME_US * 2); + + scoped_guard(mutex, &cs42l84->irq_lock) { + regcache_mark_dirty(cs42l84->regmap); + /* LATCH_TO_VP first, as the plug detection set-up writes it. */ + ret = regcache_sync_region(cs42l84->regmap, CS42L84_MIC_DET_CTL4, + CS42L84_MIC_DET_CTL4); + if (!ret) + ret = regcache_sync(cs42l84->regmap); + if (ret) + dev_err(dev, "registers not restored after the sleep: %d\n", ret); + + if (cs42l84->tip_state != CS42L84_UNPLUG) { + cs42l84->tip_state = CS42L84_UNPLUG; + cs42l84->ring_state = CS42L84_UNPLUG; + cs42l84->hs_type = 0; + cs42l84_revert_hs(cs42l84); + if (cs42l84->jack) + snd_soc_jack_report(cs42l84->jack, 0, SND_JACK_HEADSET); + cs42l84_set_interrupt_masks(cs42l84, + CS42L84_RS_PLUG | CS42L84_RS_UNPLUG); + } + } + } else if (cs42l84->hs_type == SND_JACK_HEADSET) { + /* The headset is still there: poll its buttons again. */ + scoped_guard(mutex, &cs42l84->irq_lock) + cs42l84_remote_start(cs42l84); + } + if (i2c_client->irq) + enable_irq(i2c_client->irq); + + return ret; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(cs42l84_pm_ops, cs42l84_suspend, cs42l84_resume); + static void cs42l84_i2c_remove(struct i2c_client *i2c_client) { struct cs42l84_private *cs42l84 = i2c_get_clientdata(i2c_client); @@ -1284,6 +1357,7 @@ static struct i2c_driver cs42l84_i2c_driver = { .driver = { .name = "cs42l84", .of_match_table = cs42l84_of_match, + .pm = pm_sleep_ptr(&cs42l84_pm_ops), }, .id_table = cs42l84_id, .probe = cs42l84_i2c_probe, From 9670dfd2aa01a0e693223478592d32c3ba6d7d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:16:56 +0100 Subject: [PATCH 167/269] soc: apple: pmp: keep the T6040 PMP bound as the T8140 one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PMP registers its stop with the sleep controller once and never undoes it. On T8140 that is why its driver cannot be unbound: an unbound PMP still runs, its stale registration is never readied again and refuses every sleep until reboot. The T6040 sleep controller keeps the same registration, so the same holds there. Signed-off-by: František Balázsy --- drivers/soc/apple/pmp-export.c | 6 ++++-- drivers/soc/apple/pmp.rs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index f7532b89b4dad..b896d8aa21211 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -234,11 +234,13 @@ static int apple_pmp_commit_stop_fn(void *unused) /* * Retained J713 power votes have no teardown. On T8140 an unbind leaves the firmware running, * and a commit with it running reset the SoC: its sleep registration is what refuses - * then, every sleep until reboot, since a rebind can't register again. + * then, every sleep until reboot, since a rebind can't register again. The T6040 sleep + * controller keeps the same registration, never undone. */ bool apple_pmp_keep_bound(void) { - return apple_pmp_display_retained_board() || of_machine_is_compatible("apple,t8140"); + return apple_pmp_display_retained_board() || of_machine_is_compatible("apple,t8140") || + of_machine_is_compatible("apple,t6040"); } /* Whether the sleep's last step takes the PMP: registered with the sleep driver, once. */ diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 7ca819d0b03f1..49da36c56250f 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -1043,7 +1043,8 @@ impl platform::Driver for PmpDriver { type Data<'bound> = PmpDriver; fn suppress_bind_attrs() -> bool { - // Retained J713 power votes have no teardown path, and an unbound T8140 PMP still runs. + // Retained J713 power votes have no teardown path, and an unbound T8140 or T6040 PMP + // still runs. // SAFETY: read-only machine identity checks; other machines remain unchanged. unsafe { bindings::apple_pmp_keep_bound() } } From d9e9867935047be1d150696037e7d8cdaa8c8efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:28:46 +0100 Subject: [PATCH 168/269] soc: apple: pmp: drop the sampling of the firmware's text across a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On T6040 the stop at the sleep's commit read one word per 4 KiB of the PMP firmware's text, the resume read them again and logged how many had changed. That answered whether the sleep keeps the text; nothing acted on the result. Drop the sampling, its log line and the offset that only it took from the commit stop's description. The SRAM range kept across the sleep is saved and restored as before. Signed-off-by: František Balázsy --- drivers/soc/apple/pmp-export.c | 26 -------------------------- drivers/soc/apple/pmp.rs | 4 +--- include/linux/soc/apple/pmp.h | 2 -- 3 files changed, 1 insertion(+), 31 deletions(-) diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index b896d8aa21211..6359ee741f2e4 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -200,16 +200,6 @@ EXPORT_SYMBOL_GPL(apple_pmp_t6030_gpu_ready); static struct apple_pmp_commit_stop pmp_commit; static bool pmp_commit_registered, pmp_commit_used; -/* - * T6040 bring-up: whether the firmware's text, which the sleep doesn't keep, - * survived it. One word per 4 KiB of the text, read at the commit and again - * before the restart; read only, inside the ADT's __TEXT segment. - */ -#define PMP_TEXT_STEP 0x1000 -#define PMP_TEXT_SAMPLES 128 -static u32 pmp_text_sample[PMP_TEXT_SAMPLES]; -static unsigned int pmp_text_samples; - /* Interrupts off, one CPU: what apple_pmp_sleep() does, through pointers taken before. */ static int apple_pmp_commit_stop_fn(void *unused) { @@ -222,11 +212,6 @@ static int apple_pmp_commit_stop_fn(void *unused) apple_rtkit_mailbox_recv_off(c->rtk); for (i = 0; c->sram && i < c->count; i++) c->words[i] = readl_relaxed(c->sram + 4 * i); - pmp_text_samples = 0; - if (c->sram && of_machine_is_compatible("apple,t6040")) - for (i = 0; i < c->offset / PMP_TEXT_STEP && i < PMP_TEXT_SAMPLES; i++) - pmp_text_sample[pmp_text_samples++] = - readl_relaxed(c->sram - c->offset + i * PMP_TEXT_STEP); /* Told to idle, it can't carry on whatever it answered: the sleep goes on. */ return ret ? 1 : 0; } @@ -337,17 +322,6 @@ static int apple_pmp_resume_early(struct device *dev) return 0; } pmp_asleep = false; - if (pmp_text_samples && pmp_commit.sram) { - unsigned int i, changed = 0; - - for (i = 0; i < pmp_text_samples; i++) - if (readl_relaxed(pmp_commit.sram - pmp_commit.offset + i * PMP_TEXT_STEP) != - pmp_text_sample[i]) - changed++; - dev_info(dev, "firmware text across the sleep: %u of %u sampled words changed\n", - changed, pmp_text_samples); - pmp_text_samples = 0; - } ret = apple_pmp_wake(pmp_data); if (ret) dev_err(dev, "firmware did not come back from sleep: %d\n", ret); diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 49da36c56250f..0c07019c08d3c 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -610,7 +610,6 @@ impl PmpData { stop.sram = core::ptr::null_mut(); stop.words = core::ptr::null_mut(); stop.count = 0; - stop.offset = 0; if let Some(copy) = copy.as_mut() { stop.sram = MmioBackend::as_ptr(pmp_mmio) .cast::() @@ -618,7 +617,6 @@ impl PmpData { .cast(); stop.words = copy.words.as_mut_ptr(); stop.count = copy.words.len(); - stop.offset = copy.offset; } self.state.lock().sram = copy; Ok(()) @@ -979,7 +977,7 @@ const HW_CFG_T8140: PmpHwConfig = PmpHwConfig { /// T6041: the DART window starts at 1 TiB; the boot argument header is /// version 5. The firmware advertises endpoint 7 and does not acknowledge /// the IOP power state (-ETIME) until it is started and has its buffer. -/// SRAM kept across a system sleep (bring-up): the firmware's __DATA, as the +/// SRAM kept across a system sleep: the firmware's __DATA, as the /// ADT's segment-ranges place it (0x500761000, 0x7d000 bytes, after __TEXT at /// 0x500700000, 0x61000 bytes), the segment T8140's range covers too. /// Without it the PMP never answered after a real S2R (endpoint discovery -62). diff --git a/include/linux/soc/apple/pmp.h b/include/linux/soc/apple/pmp.h index 48d3a5640786a..80617a83cbc21 100644 --- a/include/linux/soc/apple/pmp.h +++ b/include/linux/soc/apple/pmp.h @@ -57,8 +57,6 @@ struct apple_pmp_commit_stop { void __iomem *sram; u32 *words; size_t count; - /* Bytes from the SRAM's start to @sram: the firmware's text before its kept data. */ - size_t offset; }; int apple_pmp_sleep_prepare(const void *data, struct apple_pmp_commit_stop *stop); From 2baa22a3cd8f3387414605bc5a33b129a25e3402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:38 +0100 Subject: [PATCH 169/269] soc: apple: aop: describe the cold restart without test notes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two comments on the cold restart cited a test: an unverified measurement on T8140 and a firmware crash seen on J616s. Say instead what the hardware does: a co-processor that keeps running through the sleep has its run bit set, and a firmware started under doorbells left in the mailbox takes them as its own first messages, which it does not survive while its endpoints come up. Comments only, no functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/aop.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index bf47a755d2ef0..4473332a77cd4 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -3369,8 +3369,8 @@ unsafe extern "C" fn aop_resume(dev: *mut bindings::device) -> c_int { // gone and nothing restarted it, so the warm wake's stop/start notices would each time out on // a dead firmware (bounded by the RTKit handshake timeout, but seconds of stall) and the AOP // would stay down until a reboot. Take it over the cold way instead, exactly as a resume from - // disk does. A co-processor that kept running (the measured T8140 behaviour, unverified on - // T6040) has its run bit set, so this is a no-op there and the warm wake runs unchanged. + // disk does. A co-processor that kept running through the sleep, as on T8140, has its run + // bit set, so this is a no-op there and the warm wake runs unchanged. // SAFETY: only reads a flag. if unsafe { bindings::pm_resume_via_firmware() } { // SAFETY: by this function's contract. @@ -3433,9 +3433,8 @@ fn restart_after_thaw(_dev: *mut bindings::device) -> Result<()> { /// A resume from disk is a cold boot: the boot loader staged the co-processor anew and left it /// halted, and the boot kernel did not start it. The session this kernel holds is gone, but /// nothing tells its clients: every call they make waits for a reply that never comes, and the -/// mailbox keeps the doorbells. The firmware the next probe starts then takes them as its own -/// first messages (on J616s two such calls from the motion sensors' PM notifier, and the -/// re-probed firmware crashed at a NULL pointer while its endpoints came up). So the transport +/// mailbox keeps the doorbells. The firmware the next probe starts would take them as its own +/// first messages while its endpoints come up, which it does not survive. So the transport /// closes here, before any client runs, and the device is probed again once the tasks are /// thawed: the unbind finds the run bit clear and drops the old session without a shutdown, /// the probe starts the firmware as at boot. An aborted hibernation and a co-processor that From 228804ef8ec61ffa6d10873f858d9b5828a559a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:39 +0100 Subject: [PATCH 170/269] soc: apple: pmp: describe the restore and the T6041 SRAM without logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore comment quoted two log lines and the T6041 SRAM comment an error code from a test. Say what happens instead: without the restore the firmware stays down and its clients wait for it, and without the SRAM kept the PMP does not answer its endpoint discovery after a sleep that powered the SoC down. Comments only, no functional change. Signed-off-by: František Balázsy --- drivers/soc/apple/pmp.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 0c07019c08d3c..72bd690712f00 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -238,8 +238,8 @@ impl PmpData { /// Re-run the PMP bring-up after a hibernation restore. The boot loader /// cold-restarts the PMP (image resident in PMS_SRAM, ASC CPU halted), and - /// nothing replays the probe-time sequence, so the firmware stays down: - /// "no PMP within 20000ms" / cpufreq "PMP not running after 30s". The + /// nothing replays the probe-time sequence, so the firmware stays down and + /// its clients, cpufreq among them, wait for it in vain. The /// retained `PmpData` Arc, both `DevresIoMem` mappings and the C power-vote /// bridge all stay; only the volatile session and firmware are rebuilt. /// The decision between a halted (reinit + boot) and a live (adopt) firmware @@ -980,7 +980,8 @@ const HW_CFG_T8140: PmpHwConfig = PmpHwConfig { /// SRAM kept across a system sleep: the firmware's __DATA, as the /// ADT's segment-ranges place it (0x500761000, 0x7d000 bytes, after __TEXT at /// 0x500700000, 0x61000 bytes), the segment T8140's range covers too. -/// Without it the PMP never answered after a real S2R (endpoint discovery -62). +/// Without it the PMP does not answer its endpoint discovery after a sleep +/// that powered the SoC down. const HW_CFG_T6041: PmpHwConfig = PmpHwConfig { dma_mask: Some(DmaMask::new::<42>()), check_bootargs_uuid: true, From 7f2b22a52819b4b2bdbd7c90bee796c556a55c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 19:51:39 +0100 Subject: [PATCH 171/269] iio: aop_als: describe the sleep handling by what the sensor needs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment told the writes step by step. Say what the sensor takes instead: a zero interval when the system sleeps, the previous interval when it wakes. Comment only, no functional change. Signed-off-by: František Balázsy --- drivers/iio/common/aop_sensors/aop_als.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/common/aop_sensors/aop_als.rs b/drivers/iio/common/aop_sensors/aop_als.rs index 87290b2e175f3..62eac075fa5ca 100644 --- a/drivers/iio/common/aop_sensors/aop_als.rs +++ b/drivers/iio/common/aop_sensors/aop_als.rs @@ -160,9 +160,9 @@ fn set_als_property(aop: &dyn AOP, svc: &EPICService, tag: u32, data: &[u8]) -> /// /// The AOP mailbox interrupt stays enabled in suspend-to-idle, so every /// report ends the CPUs' idle period: at the 200 ms interval the sensor -/// woke the system five times a second. A zero interval stops the reports; -/// The platform writes one to its AOP sensors when the system sleeps and the -/// interval they had when it wakes. +/// woke the system five times a second. A zero interval stops the reports: +/// it is written when the system sleeps and the previous interval when it +/// wakes. /// /// Only suspend is handled. A hibernation powers the system off, and after /// a resume from disk the AOP core may have restarted the co-processor and From c88809a567d4db1a5bc52670dc9245637518a66c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:51 +0100 Subject: [PATCH 172/269] soc: apple: dockchannel: set the restore callback with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore callback, its dev_pm_ops and the driver's .pm pointer were in #ifdef CONFIG_HIBERNATION blocks. Build them unconditionally and use pm_sleep_ptr(), the usual idiom; without CONFIG_HIBERNATION the PM core never calls .restore_noirq. With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/soc/apple/dockchannel.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/soc/apple/dockchannel.c b/drivers/soc/apple/dockchannel.c index f1b0ea7d36317..00faf93b6c5cf 100644 --- a/drivers/soc/apple/dockchannel.c +++ b/drivers/soc/apple/dockchannel.c @@ -536,7 +536,6 @@ static void dockchannel_remove(struct platform_device *pdev) writel_relaxed(~0, dcc->irq_base + IRQ_FLAG); } -#ifdef CONFIG_HIBERNATION /* * After a resume from disk the interrupt mask register holds what the boot * kernel left in it (nothing, if it never loaded this driver), while the @@ -561,9 +560,8 @@ static int dockchannel_restore_noirq(struct device *dev) } static const struct dev_pm_ops dockchannel_pm_ops = { - .restore_noirq = dockchannel_restore_noirq, + .restore_noirq = pm_sleep_ptr(dockchannel_restore_noirq), }; -#endif static const struct of_device_id dockchannel_of_match[] = { { .compatible = "apple,dockchannel" }, @@ -575,9 +573,7 @@ static struct platform_driver dockchannel_driver = { .driver = { .name = "dockchannel", .of_match_table = dockchannel_of_match, -#ifdef CONFIG_HIBERNATION - .pm = &dockchannel_pm_ops, -#endif + .pm = pm_sleep_ptr(&dockchannel_pm_ops), }, .probe = dockchannel_probe, .remove = dockchannel_remove, From bfb8238020780cb292a57beb196ad0e760670482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:11:52 +0100 Subject: [PATCH 173/269] HID: dockchannel: build the hibernation callbacks with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hibernation callbacks sat in an #ifdef CONFIG_HIBERNATION block, and the #else defined simple PM ops instead. Without CONFIG_HIBERNATION the PM core never calls .freeze, .thaw or .restore, so one dev_pm_ops with pm_sleep_ptr() serves both configurations; the #else goes. With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/hid/dockchannel-hid/dockchannel-hid.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 292aeb8d78c1b..a45c2af4933bb 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -1957,7 +1957,6 @@ static int dockchannel_hid_resume(struct device *dev) return ret; } -#ifdef CONFIG_HIBERNATION /* * Resume from disk with the MTP started by the boot kernel (for instance to * type a disk passphrase in its initramfs). The boot kernel quiesces its @@ -2235,17 +2234,13 @@ static int dockchannel_hid_restore(struct device *dev) } static const struct dev_pm_ops dockchannel_hid_pm_ops = { - .suspend = dockchannel_hid_suspend, - .resume = dockchannel_hid_resume, - .freeze = dockchannel_hid_freeze, - .thaw = dockchannel_hid_thaw, - .poweroff = dockchannel_hid_suspend, - .restore = dockchannel_hid_restore, + .suspend = pm_sleep_ptr(dockchannel_hid_suspend), + .resume = pm_sleep_ptr(dockchannel_hid_resume), + .freeze = pm_sleep_ptr(dockchannel_hid_freeze), + .thaw = pm_sleep_ptr(dockchannel_hid_thaw), + .poweroff = pm_sleep_ptr(dockchannel_hid_suspend), + .restore = pm_sleep_ptr(dockchannel_hid_restore), }; -#else -static DEFINE_SIMPLE_DEV_PM_OPS(dockchannel_hid_pm_ops, dockchannel_hid_suspend, - dockchannel_hid_resume); -#endif static const struct of_device_id dockchannel_hid_of_match[] = { { .compatible = "apple,dockchannel-hid" }, From e491222d8a4b145db35f4171e01866ff3da3f7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:18:44 +0100 Subject: [PATCH 174/269] ASoC: cs42l84: set the codec up again after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A resume from disk goes through a boot. The codec then holds its reset defaults, or what the kernel that loaded the image wrote, while the restored kernel's register cache holds the state of the image. The restore callback was the resume one, which writes the registers back only after a firmware-driven suspend: after a hibernation the codec kept the wrong registers, the plug detection and the interrupt masks among them, and a jack plugged before the hibernation stayed reported although nothing drove it any more. Give the restore its own callback: after a real restore it resets the codec, writes the cache back and reports a jack gone, as the resume does after a suspend that cut the power. A hibernation given up before its image goes on as before. Signed-off-by: František Balázsy --- sound/soc/codecs/cs42l84.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/sound/soc/codecs/cs42l84.c b/sound/soc/codecs/cs42l84.c index 719ba89ea9045..81e4cab03a8fc 100644 --- a/sound/soc/codecs/cs42l84.c +++ b/sound/soc/codecs/cs42l84.c @@ -1261,9 +1261,11 @@ static int cs42l84_i2c_probe(struct i2c_client *i2c_client) /* * System sleep. The interrupt is level low and its thread reads the codec over * I2C, so it is off from the suspend until the codec is set up again. A real - * S2R cuts the codec's power: it comes back with its reset defaults, so its - * registers are written again from the cache, and a jack that was seen before - * is reported gone; one still plugged in raises the plug interrupt again. + * S2R cuts the codec's power, and a resume from disk went through a boot: the + * codec comes back with its reset defaults, or with what the kernel that + * loaded the image set up. Its registers are then written again from the + * cache, and a jack that was seen before is reported gone; one still plugged + * in raises the plug interrupt again. */ static int cs42l84_suspend(struct device *dev) { @@ -1280,14 +1282,14 @@ static int cs42l84_suspend(struct device *dev) return 0; } -static int cs42l84_resume(struct device *dev) +static int cs42l84_wake(struct device *dev, bool lost) { struct cs42l84_private *cs42l84 = dev_get_drvdata(dev); struct i2c_client *i2c_client = to_i2c_client(dev); int ret = 0; regcache_cache_only(cs42l84->regmap, false); - if (pm_resume_via_firmware()) { + if (lost) { if (cs42l84->reset_gpio) { gpiod_set_value_cansleep(cs42l84->reset_gpio, 0); usleep_range(1000, 2000); @@ -1327,7 +1329,25 @@ static int cs42l84_resume(struct device *dev) return ret; } -static DEFINE_SIMPLE_DEV_PM_OPS(cs42l84_pm_ops, cs42l84_suspend, cs42l84_resume); +static int cs42l84_resume(struct device *dev) +{ + return cs42l84_wake(dev, pm_resume_via_firmware()); +} + +/* Also after a hibernation that was given up before its image: nothing lost then. */ +static int cs42l84_restore(struct device *dev) +{ + return cs42l84_wake(dev, pm_hibernate_is_restored()); +} + +static const struct dev_pm_ops cs42l84_pm_ops = { + .suspend = pm_sleep_ptr(cs42l84_suspend), + .resume = pm_sleep_ptr(cs42l84_resume), + .freeze = pm_sleep_ptr(cs42l84_suspend), + .thaw = pm_sleep_ptr(cs42l84_resume), + .poweroff = pm_sleep_ptr(cs42l84_suspend), + .restore = pm_sleep_ptr(cs42l84_restore), +}; static void cs42l84_i2c_remove(struct i2c_client *i2c_client) { From bcff0686967466b6fe163b37ccf800d4368da739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:41 +0100 Subject: [PATCH 175/269] soc: apple: dockchannel: filter short receive IRQs only after a restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The receive interrupt handler first reads the FIFO count and, below the programmed threshold, writes the threshold again and treats the interrupt as spurious. That repairs one situation: after a resume from disk the threshold register holds what the boot left, while the restored interrupt state says that receive is armed. But it ran on every receive interrupt of every SoC, with a register read of its own, and it held back an interrupt that came early for any other reason, which the clients have always handled by reading what is there. Run it only in a kernel that was restored from an image. Elsewhere the handler takes every receive interrupt as it did. Signed-off-by: František Balázsy --- drivers/soc/apple/dockchannel.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/dockchannel.c b/drivers/soc/apple/dockchannel.c index 00faf93b6c5cf..812dfc96ac0a8 100644 --- a/drivers/soc/apple/dockchannel.c +++ b/drivers/soc/apple/dockchannel.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -127,12 +128,15 @@ static irqreturn_t dockchannel_tx_irq(int irq, void *data) * configuration registers hold what the boot loader or the boot kernel left, * while the restored interrupt state says that receive is armed. Program it * again and keep waiting, instead of waking a client to read data that is - * not there. + * not there. Only a kernel restored from an image is in that position: any + * other takes every receive interrupt as it comes. */ static bool dockchannel_rx_spurious(struct dockchannel *dockchannel) { u32 thresh = READ_ONCE(dockchannel->rx_thresh); + if (!pm_hibernate_is_restored()) + return false; if (readl_relaxed(dockchannel->data_base + DATA_RX_COUNT) >= thresh) goto real; writel_relaxed(thresh, dockchannel->config_base + CONFIG_RX_THRESH); From b25063a60fb9e17ccf5b8f55e532cf2b28f31316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:53:47 +0100 Subject: [PATCH 176/269] soc: apple: dockchannel: skip set_wake again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The FIFO interrupt chip passed a wake request on to its parent interrupt at the AIC, on the grounds that a suspend to RAM masks every AIC interrupt that is not a wakeup source. Nothing does: the AIC chips skip set_wake themselves, and a firmware-driven suspend masks every AIC interrupt and brings the masks back from the IRQ core's books. No client arms a FIFO interrupt for wake either: data that has to wake the system is reported with pm_wakeup_hard_event(). Put IRQCHIP_SKIP_SET_WAKE and its comment back. No functional change: nothing called the callback. Signed-off-by: František Balázsy --- drivers/soc/apple/dockchannel.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/drivers/soc/apple/dockchannel.c b/drivers/soc/apple/dockchannel.c index 812dfc96ac0a8..291c5cc3ce319 100644 --- a/drivers/soc/apple/dockchannel.c +++ b/drivers/soc/apple/dockchannel.c @@ -453,23 +453,14 @@ static void dockchannel_irq_unmask(struct irq_data *data) * The controller hangs off a single chained AIC interrupt, which the IRQ core * never disables for system sleep and which keeps being delivered in * suspend-to-idle. A FIFO interrupt left unmasked can therefore end a - * suspend-to-idle without configuring anything here. A suspend-to-RAM masks - * every AIC interrupt that is not a wakeup source, so arming a FIFO interrupt - * arms the chained parent too; the IRQ core counts the arms. + * suspend-to-idle without configuring anything, here or in the AIC. */ -static int dockchannel_irq_set_wake(struct irq_data *data, unsigned int on) -{ - struct dockchannel_common *dcc = irq_data_get_irq_chip_data(data); - - return irq_set_irq_wake(dcc->irq, on); -} - static const struct irq_chip dockchannel_irqchip = { .name = "dockchannel-irqc", .irq_ack = dockchannel_irq_ack, .irq_mask = dockchannel_irq_mask, .irq_unmask = dockchannel_irq_unmask, - .irq_set_wake = dockchannel_irq_set_wake, + .flags = IRQCHIP_SKIP_SET_WAKE, }; static int dockchannel_irq_domain_map(struct irq_domain *d, unsigned int virq, From 681bd79c89733007a36cb6fede2995de6dec5c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:38:24 +0100 Subject: [PATCH 177/269] soc: apple: probe again through the aura reprobe helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/pm.rs, drivers/soc/apple/sep.rs part of "soc: apple: sep: probe again through the aura reprobe helper", which goes with the coprocessor changes; the rest of that commit is with the sleep controller changes. After a resume from disk on a cold boot the SEP is unbound and probed again once the tasks are thawed, to register with the restarted enclave. The driver carried its own work item, spinlock, pending-device pointer and a module-exit guard to cancel the work. aura_hibernate_reprobe_after_thaw() does the same unbind and probe from the freezable workqueue, which the restore finds frozen, so the probe cannot start before every device's restore has completed. The helper is built in, so a module going away in the meantime leaves no work behind and needs no cancel. The registration marker is still removed before the probe is queued, and only once a cold boot is established, as before. A probe that leaves the device without a driver is not reported by device_reprobe(); the helper now logs that case too, as the SEP's own work item did. Without CONFIG_APPLE_AURA_HIBERNATE the probe is not queued and the driver says so, as the AOP does. Signed-off-by: František Balázsy --- drivers/soc/apple/pm.rs | 11 +---------- drivers/soc/apple/sep.rs | 16 ---------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index 353d1dd991a47..86979c30d49b1 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -57,7 +57,6 @@ extern "C" { mask: u64, ) -> c_int; fn sep_pm_commit_finish(dev: *mut c_void) -> c_int; - fn sep_pm_exit(); pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; } @@ -69,13 +68,6 @@ const EVENT_HIBERNATE_WARM: c_int = 3; const EVENT_HIBERNATE_COLD: c_int = 4; const EVENT_RESTORE: c_int = 5; -/// Cancels a probe queued after a resume from disk. Called once the module -/// goes away, before the driver is unregistered. -pub(crate) fn exit() { - // SAFETY: no preconditions; waits for a running reprobe to finish. - unsafe { sep_pm_exit() }; -} - /// Who may use the mailbox around a firmware-driven suspend. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum SleepGate { @@ -342,8 +334,7 @@ pub(crate) fn register(sep: Arc) -> Result<()> { // SAFETY: the shim stores the callback and registers a notifier block // that lives for the module's lifetime. - // `dev` is the bound SEP device: the shim drops it in `sep_pm_unregister` - // and takes its own reference for a reprobe. + // `dev` is the bound SEP device: the shim drops it in `sep_pm_unregister`. let rc = unsafe { sep_pm_register(dev, pm_event, pm_sleep, pm_wake, pm_told) }; if rc != 0 { let raw = SEP.swap(core::ptr::null_mut(), Ordering::AcqRel); diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index ec6c1bc044fb3..4c12380cdcec4 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -3726,16 +3726,6 @@ impl Drop for SepDriver { } } -/// Dropped after the driver registration (fields drop in order): a probe -/// queued after a resume from disk must not run once the module goes away. -struct ReprobeGuard; - -impl Drop for ReprobeGuard { - fn drop(&mut self) { - pm::exit(); - } -} - #[pin_data] struct SepModule { // field-init order enables the DT nodes before the driver registers; a failure here aborts module init @@ -3743,10 +3733,6 @@ struct SepModule { #[pin] _driver: driver::Registration>, - - // After the driver: once it is unregistered no notifier can queue a - // reprobe, and the one queued before is cancelled here. - _reprobe: ReprobeGuard, } impl kernel::InPlaceModule for SepModule { @@ -3775,8 +3761,6 @@ impl kernel::InPlaceModule for SepModule { ::NAME, module, ), - - _reprobe: ReprobeGuard, }) } } From ee74b39d128272f426ef8a4633aa622bc4978d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:53:18 +0100 Subject: [PATCH 178/269] soc: apple: learn a cold resume from the PM core, not from the PMP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/pmp-export.c, drivers/soc/apple/pmp.rs, include/linux/soc/apple/pmp.h part of "soc: apple: sep: learn a cold resume from the PM core, not from the PMP", which goes with the coprocessor changes; the rest of that commit is with the sleep controller changes. iBoot boots sepOS anew on every boot and the SEP has no run bit the AP can read, so its resume from disk asked the PMP: the PMP's restore counted the resumes that found the PMP halted, and the SEP's notifier, one priority below the PMP's, compared the count taken at registration and at each hibernation's prepare and end. That tied the SEP to the PMP driver (a Kconfig dependency, a count, an export and a call from the PMP's restore) and to the order of two notifiers. pm_hibernate_restored_cold() answers the same question in the one place that knows it: whether another boot loaded the image. The notifier asks it where it compared the counts. Not restored (aborted hibernation, wake of a hybrid sleep): warm, as before. Restored and loaded by another boot: the cold path, the session is dropped and the device probed again. Restored by the kernel that wrote the image (test_resume): warm; the PMP found itself running there too. No record: the session is dropped without a probe, which is what a missing PMP count did. On the J616s the boot that loads an image always restarts the PMP, so the two signals agree on a resume, a test resume and an aborted hibernation. They differ where a boot that loaded an image did not restart the firmware (a kexec-style resume), and for a machine without a bound PMP, which can now tell. The PMP's own restore is unchanged: it still reads the ASC run bit and only stops reporting a count. The host test now drives the end of a hibernation through the notifier for an aborted hibernation, a resume after another boot, a test resume and a missing record, with three mutations that each fail it. Signed-off-by: František Balázsy --- drivers/soc/apple/pmp-export.c | 25 ------------------------- drivers/soc/apple/pmp.rs | 5 ----- include/linux/soc/apple/pmp.h | 13 ------------- 3 files changed, 43 deletions(-) diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index 6359ee741f2e4..b34dfe599b6d1 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -1,9 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT -#include #include #include #include -#include #include #include #include @@ -23,29 +21,6 @@ static struct device *pmp_device; /* The firmware is stopped for a system sleep. */ static bool pmp_asleep; -/* Resumes from disk whose restore found the PMP halted, see below. */ -static atomic_t pmp_cold_restarts = ATOMIC_INIT(0); - -void apple_pmp_note_cold_restart(void) -{ - atomic_inc(&pmp_cold_restarts); -} - -/* - * A co-processor that iBoot restarts on a cold boot but that has no run bit - * the AP can read (the SEP) learns from this that the resume from disk it is - * being notified of was a cold boot: compare the count across the - * hibernation, from a PM notifier of lower priority than the PMP's. - */ -int apple_pmp_cold_restarts(void) -{ - /* No bound PMP: no restore runs, so a count would mean nothing. */ - if (!READ_ONCE(pmp_data)) - return -ENODEV; - return atomic_read(&pmp_cold_restarts) & INT_MAX; -} -EXPORT_SYMBOL_GPL(apple_pmp_cold_restarts); - /* * After a hibernation restore the boot loader has cold-restarted the PMP * (image resident in PMS_SRAM, ASC CPU halted) and nothing replays the diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 72bd690712f00..6de39b8b76c55 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -268,11 +268,6 @@ impl PmpData { dev_info!(self.dev, "PMP restore: firmware still running, left as is\n"); return Ok(()); } - // Running before the image and halted now: the boot loader - // restarted the SoC under it, a cold boot, which co-processors - // without a run bit the AP can read (the SEP) have to know. - // SAFETY: no arguments; the bridge only counts. - unsafe { bindings::apple_pmp_note_cold_restart() }; // Halted firmware: detach the stale session so a fresh HELLO/EPMAP // boot is accepted, as the NVMe ANS restore reinits its session. rtk.reinit()?; diff --git a/include/linux/soc/apple/pmp.h b/include/linux/soc/apple/pmp.h index 80617a83cbc21..59e9c05ff1e68 100644 --- a/include/linux/soc/apple/pmp.h +++ b/include/linux/soc/apple/pmp.h @@ -11,13 +11,6 @@ int apple_pmp_link_device(struct device *consumer); int apple_pmp_set_device_power(u8 command, u16 device_id, u32 enabled); /* After the acknowledged AGX vote and successful GPU firmware startup. */ int apple_pmp_t6030_gpu_ready(void); -/* - * How many resumes from disk the PMP found halted by the boot loader, i.e. - * cold boots of the SoC under a hibernation image, or -ENODEV without a - * bound PMP. Counted in the PMP's own PM_POST_HIBERNATION notifier - * (priority 10), so a lower-priority notifier sees this resume counted. - */ -int apple_pmp_cold_restarts(void); #else static inline int apple_pmp_link_device(struct device *consumer) { @@ -31,10 +24,6 @@ static inline int apple_pmp_t6030_gpu_ready(void) { return -ENODEV; } -static inline int apple_pmp_cold_restarts(void) -{ - return -ENODEV; -} #endif /* Registration is private to the PMP Rust driver and its C bridge. */ bool apple_pmp_keep_bound(void); @@ -62,6 +51,4 @@ struct apple_pmp_commit_stop { int apple_pmp_sleep_prepare(const void *data, struct apple_pmp_commit_stop *stop); /* Rebuild the firmware session after a resume from disk (PM_POST_HIBERNATION). */ int apple_pmp_restore(const void *data); -/* Called by the restore when it finds the PMP halted (a cold boot). */ -void apple_pmp_note_cold_restart(void); #endif From 3f5a6bf829132e85e065a55444788c0fd86662ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:58:52 +0100 Subject: [PATCH 179/269] soc: apple, ASoC: cs42l84: drop the restore callbacks' checks for an abandoned hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/soc/apple/aop.rs, sound/soc/codecs/cs42l84.c part of "soc: apple: drop the restore callbacks' checks for an abandoned hibernation", which goes with the coprocessor changes; the rest of that commit is with the sleep controller changes. An abandoned hibernation resumed its devices with PMSG_RESTORE, so every restore callback that must not act on a state it never lost began with a pm_hibernate_is_restored() check, and most forwarded to their thaw. The PM core now sends PMSG_RECOVER there, which reaches the thaw callbacks, so the check and the forward go from macsmc, the RTKit helper, apple-dart, the sn201202x Type-C controller, the SPMI, GPIO and NVMe restore paths, both PCIe restore callbacks, the DCP and AOP restores, the t6040 USB2 PHY's two and the RTC; cs42l84's restore passes that the codec lost its state. Each callback runs for a restored kernel exactly as before. For an abandoned hibernation the old path was already the thaw or nothing, and the thaw now runs on its own: macsmc, the RTKit helper, apple-dart, sn201202x, NVMe and cs42l84 forwarded to the same function (cs42l84's resume asks pm_resume_via_firmware(), which hibernation_snapshot() has cleared); PCIe's restore_noirq did for a bridge without hibernation tunnelling what its thaw_noirq does, and the same call with it; the AOP, t6040, RTC, GPIO and SPMI restores did nothing and have no thaw. DCP keeps its power-off-failed branch (PMSG_RESTORE after a failed platform power-off, which only a platform with hibernation ops reaches) and loses the abandoned one, whose only other effect was a log line. One path changes: macsmc's thaw, after the SMC was handed over and the image could not be entered, calls apple_smc_restore() to start a new session, and its own check sent that back to the thaw, which did nothing. It now starts the session as the comment says it should. The flag keeps its other users, which are not restore callbacks: the AIC syscore resume, the dockchannel receive interrupt, the SEP's PM notifier. Signed-off-by: František Balázsy --- drivers/soc/apple/aop.rs | 4 ---- sound/soc/codecs/cs42l84.c | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 4473332a77cd4..c66664e279c95 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -3444,10 +3444,6 @@ fn restart_after_thaw(_dev: *mut bindings::device) -> Result<()> { /// /// Called by the PM core with a bound AOP device. unsafe extern "C" fn aop_restore(dev: *mut bindings::device) -> c_int { - // SAFETY: only reads a flag. - if !unsafe { bindings::pm_hibernate_is_restored() } { - return 0; - } // SAFETY: by this function's contract. let data = unsafe { aop_from_dev(dev) }; if !data.cpu_started.load(Acquire) || data.removing.load(Acquire) { diff --git a/sound/soc/codecs/cs42l84.c b/sound/soc/codecs/cs42l84.c index 81e4cab03a8fc..37a3adae11431 100644 --- a/sound/soc/codecs/cs42l84.c +++ b/sound/soc/codecs/cs42l84.c @@ -1334,10 +1334,9 @@ static int cs42l84_resume(struct device *dev) return cs42l84_wake(dev, pm_resume_via_firmware()); } -/* Also after a hibernation that was given up before its image: nothing lost then. */ static int cs42l84_restore(struct device *dev) { - return cs42l84_wake(dev, pm_hibernate_is_restored()); + return cs42l84_wake(dev, true); } static const struct dev_pm_ops cs42l84_pm_ops = { From 282f733ee03b3baeb77ff826af8627f4dc406789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:03 +0100 Subject: [PATCH 180/269] iommu, nvme, PCI, brcmfmac: Apple T6040 storage and radio after a power loss MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple-dart re-imports locked firmware translation tables, apple NVMe and the PCIe host restart the ANS and the GP ports after S4/S2R, brcmfmac recovers from a link that has to retrain. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 169 ++++++++- .../broadcom/brcm80211/brcmfmac/pcie.c | 14 +- drivers/nvme/host/apple.c | 154 +++++++- drivers/pci/controller/pcie-apple.c | 359 +++++++++++++++++- tools/asahi/apple-dart-fw/fixture.c | 2 + 5 files changed, 673 insertions(+), 25 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index a8b91c7e63316..5f932df4934fb 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -534,6 +534,14 @@ struct apple_dart { * The first error of each stream is logged. */ int locked_retire_err[DART_MAX_STREAMS]; + /* Physical address of each mapped locked root, as read from its TTBR. */ + phys_addr_t locked_phys[DART_MAX_STREAMS][DART_MAX_TTBR]; + /* + * Page-table configuration last published into each locked stream's + * roots, for the re-import after a resume from disk (the boot loader + * installs fresh firmware roots). Cleared when the stream's roots retire. + */ + struct io_pgtable_cfg *locked_cfg[DART_MAX_STREAMS]; }; #define APPLE_DART_WRITER_COUNTED 1 @@ -1463,6 +1471,7 @@ apple_dart_hw_map_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx) dart->locked_fw[sid][idx] = fw; if (!idx) dart->locked_fw_tree[sid] = tree; + dart->locked_phys[sid][idx] = phys; spin_unlock_irqrestore(&dart->lock, flags); } @@ -1513,6 +1522,8 @@ apple_dart_hw_unmap_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx tree = dart->locked_fw_tree[sid]; dart->locked_fw_tree[sid] = NULL; } + dart->locked_phys[sid][idx] = 0; + dart->locked_cfg[sid] = NULL; spin_unlock_irqrestore(&dart->lock, flags); /* Once per stream: the error is never cleared. */ if (first) @@ -1612,6 +1623,8 @@ apple_dart_hw_sync_locked(struct io_pgtable_cfg *cfg, if (ret) goto out; } + if (dart->locked_ttbr[sid][0]) + dart->locked_cfg[sid] = cfg; } out: spin_unlock_irqrestore(&dart->lock, flags); @@ -5365,8 +5378,13 @@ static int apple_dart_probe(struct platform_device *pdev) dart->oas = DART_PCIEC_ADDR_WIDTH; dart->num_streams = DART_PCIEC_STREAMS; dart->four_level = true; - /* T8132 PCIe-C DARTs keep per-stream exception records. */ - if (of_device_is_compatible(dev->of_node, "apple,t8132-dart")) + /* + * T8132 PCIe-C DARTs keep per-stream exception records. + * T6040/T6041 carry the same PCIe-C block, and their DARTs + * report version 0x0202 (PARAMS3) wherever they can be read. + */ + if (of_device_is_compatible(dev->of_node, "apple,t8132-dart") || + of_device_is_compatible(dev->of_node, "apple,t6041-dart")) dart->version = 0x0202; } else { dart->pgsize = 1 << DART_PCIEC_PAGE_SHIFT; @@ -6486,6 +6504,14 @@ static __maybe_unused int apple_dart_resume(struct device *dev) return ret; } + /* + * A T6040/T6041 PCIe-C DART that lost its state lost its tunables with it: + * apply them again before the reset, in the order of probe. + */ + if (dart->tunneled && dart->tunables && + of_device_is_compatible(dev->of_node, "apple,t6041-dart")) + apple_dart_apply_tunables(dart, dart->tunables); + ret = apple_dart_hw_reset(dart); /* @@ -6676,8 +6702,145 @@ static __maybe_unused int apple_dart_sys_resume_noirq(struct device *dev) return pm_suspend_via_firmware() ? apple_dart_resume_noirq(dev) : 0; } +#ifdef CONFIG_HIBERNATION +/* + * Resume from disk is a cold boot: the boot loader locked every locked DART + * again with fresh firmware-owned roots, while the restored kernel still + * describes the roots of the image (their mapping, their firmware snapshot + * and the slots it had published). Every publication then fails (-EBUSY), + * and with it every DMA mapping through the DART (observed for the PMP and + * every DCP: "failed buffer request ... (-12)"). Re-import the live roots + * the way attach maps them, then publish the stream's page tables into them + * again, as attach does. Firmware-owned memory is never written: the old root's + * published slots are forgotten, not retired. + */ +static int apple_dart_reimport_locked(struct apple_dart *dart) +{ + DECLARE_BITMAP(redo, DART_MAX_STREAMS) = {}; + unsigned int sid, idx; + int ret = 0, done = 0; + + /* + * Two passes, as attach does: every stream's live roots are re-read and + * snapshotted before any of them is republished, so that a root shared + * by several streams is snapshotted before Linux slots enter it. + */ + for (sid = 0; sid < dart->num_streams; sid++) { + bool any = false; + + for (idx = 0; idx < dart->hw->ttbr_count; idx++) { + u64 *live = dart->locked_ttbr[sid][idx]; + u64 *owned = dart->locked_owned[sid][idx]; + struct apple_dart_fw_root *fw, *old_fw; + unsigned long flags; + phys_addr_t phys; + u64 *l1 = live; + u32 ttbr; + + if (!live || !owned) + continue; + ttbr = apple_dart_readl(dart, DART_TTBR(dart, sid, idx)); + if (!(ttbr & dart->hw->ttbr_valid)) { + dev_err(dart->dev, "hibernation restore: stream %u ttbr[%u] not valid\n", + sid, idx); + ret = ret ?: -EIO; + continue; + } + ttbr &= ~dart->hw->ttbr_valid; + if (dart->hw->ttbr_addr_field_shift) + ttbr >>= dart->hw->ttbr_addr_field_shift; + phys = ((phys_addr_t)ttbr) << dart->hw->ttbr_shift; + if (dart->fw_handoff && !apple_dart_fw_table_ram(dart, phys)) { + ret = ret ?: -EINVAL; + continue; + } + if (phys != dart->locked_phys[sid][idx]) { + l1 = devm_memremap(dart->dev, phys, dart->pgsize, MEMREMAP_WB); + if (IS_ERR(l1)) { + ret = ret ?: PTR_ERR(l1); + continue; + } + } + fw = apple_dart_snapshot_fw_root(dart, sid, l1); + if (IS_ERR(fw)) { + if (l1 != live) + devm_memunmap(dart->dev, l1); + ret = ret ?: PTR_ERR(fw); + continue; + } + spin_lock_irqsave(&dart->lock, flags); + old_fw = dart->locked_fw[sid][idx]; + memset(owned, 0, dart->pgsize); + dart->locked_ttbr[sid][idx] = l1; + dart->locked_phys[sid][idx] = phys; + dart->locked_fw[sid][idx] = fw; + spin_unlock_irqrestore(&dart->lock, flags); + apple_dart_free_fw_root(dart, old_fw); + if (l1 != live) + devm_memunmap(dart->dev, live); + any = true; + } + if (any) + __set_bit(sid, redo); + } + + for_each_set_bit(sid, redo, dart->num_streams) { + struct apple_dart_stream_map stream = { .dart = dart }; + struct io_pgtable_cfg *cfg = READ_ONCE(dart->locked_cfg[sid]); + int err; + + if (!cfg) + continue; + __set_bit(sid, stream.sidmap); + err = apple_dart_hw_sync_locked(cfg, &stream, false); + if (!err) + err = dart->hw->invalidate_tlb(&stream); + if (err) { + dev_err(dart->dev, "hibernation restore: stream %u not republished: %d\n", + sid, err); + ret = ret ?: err; + continue; + } + done++; + } + dev_info(dart->dev, "hibernation restore: %d locked streams re-imported from the boot loader's roots (%d)\n", + done, ret); + return ret; +} + +static int apple_dart_restore(struct device *dev) +{ + struct apple_dart *dart = dev_get_drvdata(dev); + int ret; + + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return apple_dart_system_resume(dev); + ret = pm_runtime_force_resume(dev); + if (ret || !dart->locked) + return ret; + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) + return ret; + /* Logged per stream; a failed stream must not fail the whole restore. */ + apple_dart_reimport_locked(dart); + pm_runtime_put(dev); + return 0; +} +#endif + +/* A resume from disk also re-imports the roots the boot loader locked again. */ static const struct dev_pm_ops apple_dart_pm_ops = { - SYSTEM_SLEEP_PM_OPS(apple_dart_system_suspend, apple_dart_system_resume) + .suspend = pm_sleep_ptr(apple_dart_system_suspend), + .resume = pm_sleep_ptr(apple_dart_system_resume), + .freeze = pm_sleep_ptr(apple_dart_system_suspend), + .thaw = pm_sleep_ptr(apple_dart_system_resume), + .poweroff = pm_sleep_ptr(apple_dart_system_suspend), +#ifdef CONFIG_HIBERNATION + .restore = apple_dart_restore, +#else + .restore = pm_sleep_ptr(apple_dart_system_resume), +#endif NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_dart_sys_suspend_noirq, apple_dart_sys_resume_noirq) RUNTIME_PM_OPS(apple_dart_suspend, apple_dart_resume, NULL) }; diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 2bfee245f4331..892b787924847 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -3484,6 +3484,7 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) struct brcmf_pciedev_info *devinfo; struct brcmf_bus *bus; struct pci_dev *pdev; + u32 intmask; int err; err = apple_radio_sleep_endpoint_resume(dev); @@ -3509,8 +3510,19 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) return 0; } + /* + * ~0 is unbacked MMIO (link still down after the Apple RC power + * cycle), not a live firmware. The hot-resume path would issue more + * mailbox MMIO and wedge. Fall through to remove+probe. + */ + intmask = brcmf_pcie_read_pcie32(devinfo, devinfo->reginfo->intmask); + if (intmask == ~0u) { + brcmf_err(bus, "INTMASK read ~0 on resume; reloading firmware\n"); + goto cleanup; + } + /* Check if device is still up and running, if so we are ready */ - if (brcmf_pcie_read_pcie32(devinfo, devinfo->reginfo->intmask) != 0) { + if (intmask != 0) { brcmf_dbg(PCIE, "Try to wakeup device....\n"); /* Set the device up, so we can write the MB data message in ring mode */ devinfo->state = BRCMFMAC_PCIE_STATE_UP; diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 88293eaf15367..c5f2935f4c9eb 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -217,6 +217,12 @@ struct apple_nvme_hw { bool cold_reset; /* Firmware-driven suspend has been traced on this controller. */ bool sleep_traced; + /* + * Not measured, but the sleep controller may take it the same way: its flush, + * quiesce and shutdown notification, then the polled idle at the commit. The + * values below come from this SoC's own sources, as each says. + */ + bool sleep_untraced; /* The last endpoint map reply from that trace */ u32 sleep_epmap_last_mask; /* Higher namespaces are the firmware's, and a Flush to one faults the controller. */ @@ -2399,6 +2405,9 @@ static void apple_nvme_free_rtkit(void *data) apple_nvme_set_reset_ready(anv, false); guard(mutex)(&anv->disable_lock); + /* A failed hibernation restore leaves no session behind. */ + if (!anv->rtk) + return; if (anv->hw->needs_ioq_registers) apple_rtkit_free_retaining_buffers(anv->rtk); else @@ -2742,7 +2751,7 @@ static int apple_nvme_probe(struct platform_device *pdev) INIT_DELAYED_WORK(&anv->flush_dwork, apple_nvme_flush_work); - if (anv->hw->sleep_traced) { + if (anv->hw->sleep_traced || anv->hw->sleep_untraced) { anv->sleep_registered = !apple_sleep_coproc_register(anv->dev, anv->mmio_coproc, true, apple_nvme_sleep_last_stop, anv); @@ -3107,8 +3116,123 @@ static int apple_nvme_suspend(struct device *dev) return ret; } +/* + * Hibernation restore on post-M4. The machine cold-booted since .freeze: + * the boot firmware restarted ANS, the boot stages kept it running, and the + * resume kernel adopted that session to read the image (its own queues, and + * its .freeze cleared CC.EN before jumping here). anv->rtk therefore + * describes a firmware instance that no longer exists, and the live session + * on the mailbox has never been adopted by this kernel. Unlike + * apple_nvme_can_adopt_rtkit(), accept a disabled controller: it was just + * disabled below, and RUN + BOOT_STATUS_OK + !CFS are what survive. + */ +static bool apple_nvme_can_adopt_restored_rtkit(struct apple_nvme *anv) +{ + u32 csts; + + if (!(readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL) & + APPLE_ANS_COPROC_CPU_CONTROL_RUN) || + readl(anv->mmio_nvme + APPLE_ANS_BOOT_STATUS) != APPLE_ANS_BOOT_STATUS_OK) + return false; + csts = readl(anv->mmio_nvme + NVME_REG_CSTS); + if (csts & NVME_CSTS_CFS) + return false; + return !(readl(anv->mmio_nvme + NVME_REG_CC) & NVME_CC_ENABLE) && + !(csts & NVME_CSTS_RDY); +} + +static int apple_nvme_restore_post_m4(struct apple_nvme *anv) +{ + struct apple_rtkit *rtk; + int ret; + + guard(mutex)(&anv->disable_lock); + if (READ_ONCE(anv->quarantined)) + return -EIO; + dev_dbg(anv->dev, + "hibernation restore: CPU_CONTROL=%#x BOOT_STATUS=%#x CC=%#x CSTS=%#x\n", + readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL), + readl(anv->mmio_nvme + APPLE_ANS_BOOT_STATUS), + readl(anv->mmio_nvme + NVME_REG_CC), + readl(anv->mmio_nvme + NVME_REG_CSTS)); + + /* + * Stop the controller first: until CC.EN is clear the firmware may + * still fetch from the resume kernel's queues, whose memory is now + * this kernel's. This is the owned-session reset path. + */ + ret = apple_nvme_disable_locked(anv, false); + if (ret) + return ret; + + /* + * Our firmware instance ended with the power-off. No shutdown + * handshake: the new instance would receive it. Shared buffers are + * retained as in apple_nvme_free_rtkit(). + */ + apple_nvme_set_reset_ready(anv, false); + apple_rtkit_free_retaining_buffers(anv->rtk); + anv->rtk = NULL; + anv->owns_rtkit = false; + + if (!apple_nvme_can_adopt_restored_rtkit(anv)) { + dev_err(anv->dev, "hibernation restore: no adoptable ANS session\n"); + apple_nvme_quarantine(anv, -EIO); + return -EIO; + } + + /* Publish admission before the initializer enables mailbox RX. */ + anv->inherited_rtkit = true; + rtk = apple_rtkit_init_adopted(anv->dev, anv, NULL, 0, + &apple_nvme_rtkit_ops); + if (IS_ERR(rtk)) { + ret = PTR_ERR(rtk); + dev_err(anv->dev, "hibernation restore: RTKit init failed: %d\n", ret); + apple_nvme_quarantine(anv, ret); + return ret; + } + anv->rtk = rtk; + ret = apple_rtkit_adopt_running(rtk); + if (ret) { + dev_err(anv->dev, "hibernation restore: RTKit adoption failed: %d\n", ret); + apple_nvme_quarantine(anv, ret); + return ret; + } + /* Owned: reset_work takes the live-session path, as after s2idle. */ + anv->owns_rtkit = true; + apple_nvme_set_reset_ready(anv, true); + dev_info(anv->dev, + "hibernation restore: adopted the boot loader's ANS session\n"); + return 0; +} + +static int apple_nvme_restore(struct device *dev) +{ + struct apple_nvme *anv = dev_get_drvdata(dev); + int ret; + + /* Hibernation aborted before the image: the same ANS session runs on. */ + if (IS_ENABLED(CONFIG_HIBERNATION) && anv->hw->needs_ioq_registers && + pm_hibernate_is_restored()) { + ret = apple_nvme_restore_post_m4(anv); + if (ret) + return ret; + } + return apple_nvme_resume(dev); +} + +/* + * freeze and poweroff disable the controller and keep the session, like + * suspend; thaw (image not written) is a resume. Only restore differs. The + * early resume restarts ANS only after a firmware-driven suspend idled it. + */ static const struct dev_pm_ops apple_nvme_pm_ops = { - SYSTEM_SLEEP_PM_OPS(apple_nvme_suspend, apple_nvme_resume) + .suspend = pm_sleep_ptr(apple_nvme_suspend), + .resume = pm_sleep_ptr(apple_nvme_resume), + .freeze = pm_sleep_ptr(apple_nvme_suspend), + .thaw = pm_sleep_ptr(apple_nvme_resume), + .poweroff = pm_sleep_ptr(apple_nvme_suspend), + .restore = pm_sleep_ptr(apple_nvme_restore), LATE_SYSTEM_SLEEP_PM_OPS(NULL, apple_nvme_resume_early) }; @@ -3183,6 +3307,32 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { .needs_ioq_registers = true, .lsq_in_nvmmu = true, .max_queue_depth = 64, + /* + * Registered with the T6040 sleep controller. The flush, quiesce, CC.SHN + * shutdown notification and the resume path (SART replay, warm RTKit + * restart, controller reset + namespace re-attach) are all generic and + * already wired through this sleep_untraced state; the controller stops + * ANS at the commit and raises its two ST lanes on wake + * (t6040_sleep_lower/raise_apcie_st, U4). What flips a real power-loss + * sleep from refused to allowed is NOT a flag here but the controller's + * apple,apcie validity: with the ST-lane windows + PLL/lane tunables + * absent it refuses (apple_sleep_power_loss() stays unreachable), so a dry + * run is all that runs -- the flush, the quiesce and the shutdown + * notification, then the controller reset as after any refusal. + * + * CC.SHN: the platform 26.7.1 on this SoC, at power state 0, + * shuts NVMe down in its suspend variant: + * the shutdown notification writes CC |= 0xc000 and + * polls CSTS.SHST for complete. Flush: namespace 1 only, the OS's own; the + * others belong to the firmware, as on T8140. + * + * TODO(HW): sleep_epmap_last_mask (the warm-restart last-epmap echo) is + * unmeasured on T6040 ANS, so it is left at the rtkit default + * (echo all); the first wake's warm_restart / AURA_ANS_WATCH log sets it. + */ + .sleep_untraced = true, + .sleep_flush_max_nsid = 1, + .sleep_shn = NVME_CC_SHN_MASK, .quirks = NVME_QUIRK_NO_APST, }; diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index b6c3b98af2c9e..2b58a5d597db2 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -275,6 +275,12 @@ struct hw_info { bool pipe_rst; bool no_port_reset; bool mps_timers; + /* Cold init waits for port RUN before the root-port config tunables. */ + bool tunnel_ready_before_rc; + /* The router acknowledges a tunneled reset by setting ACK_PEND. */ + bool tunnel_ack_on_set; + /* Stop the tunnel for hibernation and start it on thaw and restore. */ + bool tunnel_hibernate; }; static const struct hw_info t8103_hw = { @@ -405,6 +411,17 @@ static const struct hw_info t8132_pciec_hw = { .s2idle_hold_domain = true, }; +/* + * T6040/T6041 (M4 Pro/Max) PCIe-C is the T8132 block: the platform drives it as + * the T8132 one and only sets its own + * register indices, so the port layout, RID2SID count and tunnel reset + * registers are those of T8132. Two steps follow the platform rather than the T8132 + * Linux sequence: the port must report RUN before the root-port config + * tunables are written, and the tunneled reset acknowledgment is the + * ACK_PEND bit being set. Like the platform, which disables the port and its + * clocks before sleep and runs the full enable sequence on wake, + * hibernation stops the port and cold-starts it again. + */ static const struct hw_info t6040_pciec_hw = { .port_msiaddr = PORT_MSIADDR, .port_msiaddr_hi = PORT_MSIADDR + 4, @@ -418,6 +435,9 @@ static const struct hw_info t6040_pciec_hw = { .t8132_reset = true, .msimap_by_vector = true, .s2idle_hold_domain = true, + .tunnel_ready_before_rc = true, + .tunnel_ack_on_set = true, + .tunnel_hibernate = true, }; /* @@ -552,6 +572,8 @@ struct apple_pcie { bool neo_rc_down; /* Down already when the sleep began: a failed wake left it unprogrammed */ bool neo_rc_was_down; + /* Functions' saved state, kept from a restore's noirq phase to its link */ + struct list_head hib_fns; const struct hw_info *hw; unsigned long *bitmap; struct list_head ports; @@ -592,6 +614,17 @@ struct apple_pcie_port { unsigned long adopted_slots; /* adopt_link: RID2SID slots routed (slot == SID) */ struct gpio_desc *pwren; /* adopt_link: endpoint power, if Linux trained */ bool powered; /* cold train: pwren raised, port not parked yet */ + /* Conventional port: link up when the image was taken */ + bool hib_link_up; + /* ... and found down by the restore: the boot that resumed did not set it up */ + bool hib_cold; +}; + +/* A function's configuration, for its link back after a resume from disk. */ +struct apple_pcie_hib_fn { + struct list_head entry; + struct pci_dev *pdev; + struct pci_saved_state *state; }; static bool apple_radio_writer_closed(struct apple_pcie *pcie); @@ -1328,9 +1361,9 @@ static int apple_pcie_tunnel_cold_init(struct apple_pcie_port *port) /* * Without an m1n1 handoff, T600x/T602x ports must report RUN before * the root complex config space is written: writing the RC tunables - * first hard-resets a J416c. + * first hard-resets a J416c. The platform waits for RUN there on T6040 too. */ - if (!pcie->kernel_init) { + if (!pcie->kernel_init || pcie->hw->tunnel_ready_before_rc) { ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, stat & PORT_STATUS_READY, 10, 250000, false, port, @@ -1339,9 +1372,9 @@ static int apple_pcie_tunnel_cold_init(struct apple_pcie_port *port) return dev_err_probe(pcie->dev, ret, "port %pOF cold init: RUN not set (status %#x)\n", port->np, stat); - } else { - apple_pcie_port_rmw_clear(port, PORT_APPCLK_CGDIS, PORT_APPCLK); } + if (pcie->kernel_init) + apple_pcie_port_rmw_clear(port, PORT_APPCLK_CGDIS, PORT_APPCLK); dev_info(pcie->dev, "port %pOF cold init: %sroot tunables\n", port->np, pcie->oe_fabric_base ? "oe-fabric and " : ""); @@ -1704,9 +1737,12 @@ static int apple_pcie_tunnel_stop(struct apple_pcie_port *port) err = ret; apple_pcie_port_rmw_set(port, PORT_TUNCTRL_PERST_ACK_REQ, apple_pcie_port_tunctrl(port)); - /* M1 reports an outstanding acknowledgment until ACK_PEND clears. */ + /* + * M1 reports an outstanding acknowledgment until ACK_PEND clears. + * T6040 reports the acknowledgment by setting it, as the platform waits. + */ ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, - pcie->kernel_init ? + pcie->kernel_init && !pcie->hw->tunnel_ack_on_set ? !(stat & PORT_TUNSTAT_PERST_ACK_PEND) : stat & PORT_TUNSTAT_PERST_ACK_PEND, 1000, 1000000, false, port, apple_pcie_port_tunstat(port)); @@ -2913,6 +2949,22 @@ static int apple_pcie_port_reset_endpoint(struct apple_pcie *pcie, stat & PORT_STATUS_READY, 100, 250000); } +/* Let the port gate its clocks again once the link is set up. */ +static void apple_pcie_port_gate_clocks(struct apple_pcie *pcie, + struct apple_pcie_port *port) +{ + if (pcie->hw->retain_clocks) + return; + if (pcie->hw->port_refclk) + rmw_clear(PORT_REFCLK_CGDIS, port->base + pcie->hw->port_refclk); + else if (port->phy) + rmw_set(PHY_LANE_CFG_REFCLKCGEN, port->phy + PHY_LANE_CFG); + + /* PCIe-C APPCLK state is part of the m1n1 handoff. */ + if (!pcie->hw->tunneled) + rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK); +} + static int apple_pcie_setup_port(struct apple_pcie *pcie, struct device_node *np) { @@ -3078,18 +3130,7 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, } } - if (!pcie->hw->retain_clocks) { - if (pcie->hw->port_refclk) - rmw_clear(PORT_REFCLK_CGDIS, - port->base + pcie->hw->port_refclk); - else if (port->phy) - rmw_set(PHY_LANE_CFG_REFCLKCGEN, - port->phy + PHY_LANE_CFG); - - /* PCIe-C APPCLK state is part of the m1n1 handoff. */ - if (!pcie->hw->tunneled) - rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK); - } + apple_pcie_port_gate_clocks(pcie, port); ret = apple_pcie_port_setup_irq(port); if (ret) @@ -3314,7 +3355,26 @@ static int apple_msi_init(struct apple_pcie *pcie) if (of_address_to_resource(to_of_node(fwnode), 0, &ecam)) goto invalid_slice; /* These ECAM apertures identify the ADT bridges of each SoC. */ - if (of_machine_is_compatible("apple,t6030")) { + if (pcie->hw == &t6040_pciec_hw) { + /* + * ADT msi-vector-offset 0x817. The internal port owns + * vectors 0..31, bridge pcicN 0x20 * (N + 1). + */ + aic_base = 2071; + switch (ecam.start) { + case 0x1c50000000ULL: + expected_base = 0x20; + break; + case 0x1c60000000ULL: + expected_base = 0x40; + break; + case 0x1c70000000ULL: + expected_base = 0x60; + break; + default: + goto invalid_slice; + } + } else if (of_machine_is_compatible("apple,t6030")) { aic_base = 1342; switch (ecam.start) { case 0x730000000ULL: @@ -4988,6 +5048,7 @@ static int apple_pcie_probe(struct platform_device *pdev) mutex_init(&pcie->lock); INIT_LIST_HEAD(&pcie->ports); + INIT_LIST_HEAD(&pcie->hib_fns); /* Resolve PM dependencies before publishing the PCI hierarchy. */ if (pcie->hw->tunneled) { @@ -6468,12 +6529,272 @@ static int apple_pcie_prepare(struct device *dev) return apple_radio_prepare(dev); } +/* + * A conventional host (the internal ports with their radio and SD reader) keeps running + * through freeze and thaw. Only a kernel restored from an image finds it changed: the boot + * that resumed it went through the boot loader, and when its kernel did not set the ports up + * (a boot kernel without the GPIO drivers that probing the host takes), the endpoints sit + * unpowered or in reset behind a link that is down. The restore then does what probe did. + */ +static bool apple_pcie_hib_conventional(struct apple_pcie *pcie) +{ + return !pcie->hw->tunneled && !pcie->managed_sids && !pcie->piodma_supplier && + !pcie->hw->root_bus_only; +} + +/* What only the port registers hold, for a restore that finds them reset. */ +static void apple_pcie_hib_save(struct apple_pcie *pcie) +{ + struct apple_pcie_port *port; + int i; + + mutex_lock(&pcie->lock); + list_for_each_entry(port, &pcie->ports, entry) { + port->hib_link_up = apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP; + port->hib_cold = false; + port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); + for_each_set_bit(i, port->sid_map, port->sid_map_sz) + port->saved_rid2sid[i] = + apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); + } + mutex_unlock(&pcie->lock); +} + +static void apple_pcie_hib_free_fns(struct apple_pcie *pcie) +{ + struct apple_pcie_hib_fn *fn, *tmp; + + list_for_each_entry_safe(fn, tmp, &pcie->hib_fns, entry) { + list_del(&fn->entry); + kfree(fn->state); + pci_dev_put(fn->pdev); + kfree(fn); + } +} + +/* + * The PCI core's noirq restore spends the functions' saved state on a link that is down + * ("device inaccessible"). A copy taken first goes back once the link is: the core's normal + * restore then restores it (pci_pm_restore() restores a function whose state is saved). + */ +static int apple_pcie_hib_keep_fn(struct pci_dev *pdev, void *data) +{ + struct apple_pcie *pcie = data; + struct apple_pcie_port *port; + struct apple_pcie_hib_fn *fn; + + if (pci_is_root_bus(pdev->bus) || !pdev->state_saved) + return 0; + port = apple_pcie_get_port(pdev); + if (!port || !port->hib_cold) + return 0; + fn = kzalloc_obj(*fn); + if (!fn) + return 0; + fn->state = pci_store_saved_state(pdev); + if (!fn->state) { + kfree(fn); + return 0; + } + fn->pdev = pci_dev_get(pdev); + list_add_tail(&fn->entry, &pcie->hib_fns); + return 0; +} + +/* + * Before the PCI core restores the functions. Reading the link state is what probe does on + * every cold boot; nothing is written to a port in this phase. Its PWREN is an SMC GPIO, and + * the SMC session is back only in the normal phase. + */ +static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie) +{ + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct apple_pcie_port *port; + bool cold = false; + + apple_pcie_hib_free_fns(pcie); + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->hib_link_up) + continue; + if (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP) + continue; + port->hib_cold = true; + cold = true; + dev_info(pcie->dev, "resume from disk: %pOF lost its link, setting it up again\n", + port->np); + } + if (cold && bridge->bus) + pci_walk_bus(bridge->bus, apple_pcie_hib_keep_fn, pcie); +} + +/* Bounded like probe's wait, polled: the link interrupts stay masked meanwhile. */ +static int apple_pcie_hib_train(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + int attempt, attempts = port->perst ? 1 + LINK_RESET_RETRIES : 1; + u32 stat; + int ret = -ETIMEDOUT; + + for (attempt = 0; attempt < attempts; attempt++) { + if (attempt) { + dev_info(pcie->dev, "%pOF link down after %dms (LTSSM %#x), resetting the endpoint\n", + port->np, link_up_timeout, apple_pcie_port_ltssm(pcie, port)); + ret = apple_pcie_port_reset_endpoint(pcie, port); + if (ret) + return ret; + } + apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); + ret = readl_relaxed_poll_timeout(port->base + PORT_LINKSTS, stat, + stat & PORT_LINKSTS_UP, 100, + link_up_timeout * 1000); + if (!ret) + break; + } + return ret; +} + +/* This kernel's RID-to-SID table, every other entry cleared as probe clears them. */ +static void apple_pcie_hib_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + int i; + + mutex_lock(&pcie->lock); + for (i = 0; i < port->sid_map_sz; i++) + apple_pcie_rid2sid_write(port, i, test_bit(i, port->sid_map) ? + port->saved_rid2sid[i] : 0); + mutex_unlock(&pcie->lock); +} + +/* Probe's bring-up of a conventional port, with the state this kernel had programmed. */ +static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + int ret; + + /* A port with auxiliary resets does not keep its GPIOs for a second bring-up. */ + if (!port->perst_gpio) + return -EOPNOTSUPP; + ret = apple_pcie_setup_link(pcie, port, port->np); + if (ret) + return ret; + apple_pcie_port_gate_clocks(pcie, port); + /* All port interrupts masked and cleared, MSI routing as at probe. */ + apple_pcie_tunnel_restore_irq_hw(port); + apple_pcie_hib_rid2sid(pcie, port); + ret = apple_pcie_hib_train(pcie, port); + if (ret) + return ret; + apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + /* 100 ms after the link before configuration requests (PCIe r5.0, 6.6.1). */ + msleep(100); + return 0; +} + +/* + * The normal phase, after the SMC's own restore (the PWREN GPIOs are its) and before the PCI + * core's normal restore of the functions, which waits for this host as their ancestor. + */ +static int apple_pcie_restore(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + struct apple_pcie_port *port; + struct apple_pcie_hib_fn *fn; + unsigned int back = 0; + int ret; + + if (!apple_pcie_hib_conventional(pcie) || !pm_hibernate_is_restored()) + return 0; + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->hib_link_up) + continue; + if (!port->hib_cold) { + /* + * Up: set up by the boot that resumed, or never reset. The functions + * this kernel enabled keep their mappings only if the table is its own. + */ + apple_pcie_hib_rid2sid(pcie, port); + continue; + } + ret = apple_pcie_hib_port_up(pcie, port); + if (ret) { + dev_err(dev, "resume from disk: %pOF link not back (%d), its functions stay unreachable\n", + port->np, ret); + port->hib_cold = false; + continue; + } + dev_info(dev, "resume from disk: %pOF link back, PWREN and PERST# cycled as at probe\n", + port->np); + back++; + } + list_for_each_entry(fn, &pcie->hib_fns, entry) { + port = apple_pcie_get_port(fn->pdev); + if (!port || !port->hib_cold) + continue; + if (pci_load_saved_state(fn->pdev, fn->state)) + pci_warn(fn->pdev, "resume from disk: saved configuration not taken back\n"); + } + apple_pcie_hib_free_fns(pcie); + list_for_each_entry(port, &pcie->ports, entry) + port->hib_cold = false; + if (back) + dev_dbg(dev, "resume from disk: %u port(s) set up again\n", back); + return 0; +} + +/* + * Hibernation, for the controllers that opt in: freeze and poweroff stop the + * tunnel as a system sleep that keeps nothing does, thaw and restore start it + * again. After a restore the controller comes back from a power cycle, so the + * start replays the whole cold sequence. A conventional host only saves what a + * restore over a cold boot needs, see apple_pcie_hib_conventional(). + */ +static int apple_pcie_hibernate_noirq(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (!pcie->hw->tunnel_hibernate) { + if (apple_pcie_hib_conventional(pcie)) + apple_pcie_hib_save(pcie); + return 0; + } + pcie->reset_on_resume = false; + pcie->link_kept = false; + apple_pcie_stop_for_sleep(dev); + return 0; +} + +static int apple_pcie_wake_noirq(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (!pcie->hw->tunnel_hibernate) + return 0; + return apple_pcie_resume_noirq(dev); +} + +/* Thaw and an aborted hibernation keep every port as it was: only a restored image looks. */ +static int apple_pcie_restore_noirq(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (!pcie->hw->tunnel_hibernate) { + if (apple_pcie_hib_conventional(pcie) && pm_hibernate_is_restored()) + apple_pcie_hib_restore_noirq(pcie); + return 0; + } + return apple_pcie_resume_noirq(dev); +} + static const struct dev_pm_ops apple_pcie_pm_ops = { .prepare = apple_pcie_prepare, .complete = apple_radio_complete, .suspend_noirq = apple_pcie_suspend_noirq, .resume_noirq = apple_pcie_resume_noirq, .resume_early = apple_pcie_resume_early, + .freeze_noirq = apple_pcie_hibernate_noirq, + .thaw_noirq = apple_pcie_wake_noirq, + .poweroff_noirq = apple_pcie_hibernate_noirq, + .restore_noirq = apple_pcie_restore_noirq, + .restore = apple_pcie_restore, }; static const struct of_device_id apple_pcie_of_match[] = { diff --git a/tools/asahi/apple-dart-fw/fixture.c b/tools/asahi/apple-dart-fw/fixture.c index 4ecb32bb7e512..0b0ed7e82127e 100644 --- a/tools/asahi/apple-dart-fw/fixture.c +++ b/tools/asahi/apple-dart-fw/fixture.c @@ -68,6 +68,8 @@ struct apple_dart { /* A J713 sleep's, never set here. */ void *sleep_record; bool cio_active; + phys_addr_t locked_phys[2][4]; + struct io_pgtable_cfg *locked_cfg[2]; }; struct apple_dart_stream_map {struct apple_dart*dart;unsigned long sidmap[1];}; struct apple_dart_atomic_stream_map {struct apple_dart*dart;unsigned long sidmap[1];}; From 3b5ba291fc0fa4a54183856053d9901b2054b8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:33:41 +0100 Subject: [PATCH 181/269] PCI: apple: set a conventional host's ports up again after a real S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A real T6040 S2R takes the GP host's power: after the wake the Wi-Fi and SD reader functions sit behind links that are down (brcmfmac -19, the SD reader "Unable to change power state ... device inaccessible"), and only the Neo host had a sleep path. The resume from disk already handles a conventional host that comes back from a power cycle: save the port state, find the links that were lost, keep the functions' saved configuration from the noirq restore, then in the normal phase cycle PWREN and PERST# and train as at probe. Run the same on a real S2R: the save at suspend_noirq, the link check at resume_noirq and the bring-up from a new .resume. s2r_conventional=0 leaves the host as before. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 70 ++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 2b58a5d597db2..a614df81ddc42 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -81,6 +81,14 @@ static int link_up_timeout = 500; module_param(link_up_timeout, int, 0644); MODULE_PARM_DESC(link_up_timeout, "PCIe link training timeout in milliseconds"); +/* + * T6040 S2R (bring-up): a real sleep takes a conventional host's power as a + * resume from disk does, so its ports come back the same way. + */ +static bool s2r_conventional = true; +module_param(s2r_conventional, bool, 0644); +MODULE_PARM_DESC(s2r_conventional, "Set a conventional host's ports up again after a real S2R, as after a resume from disk"); + /* T8103 (original M1) and related SoCs */ #define CORE_RC_PHYIF_CTL 0x00024 #define CORE_RC_PHYIF_CTL_RUN BIT(0) @@ -5846,6 +5854,9 @@ static void apple_pcie_stop_for_sleep(struct device *dev) } static void apple_pcie_neo_save_functions(struct apple_pcie *pcie); +static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie); +static void apple_pcie_hib_save(struct apple_pcie *pcie); +static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what); /* * The J716c radio drivers save PCI state without entering D3. The PCI core @@ -5953,6 +5964,8 @@ static int apple_pcie_suspend_noirq(struct device *dev) if (!pcie->hw->tunneled) { if (pm_suspend_no_platform() && pcie->num_pds) return apple_pcie_hold_domains(pcie); + if (apple_pcie_s2r_conventional(pcie) && pm_suspend_via_firmware()) + apple_pcie_hib_save(pcie); return 0; } pcie->reset_on_resume = false; @@ -6443,8 +6456,11 @@ static int apple_pcie_resume_noirq(struct device *dev) } return ret; } - if (!pcie->hw->tunneled) + if (!pcie->hw->tunneled) { + if (apple_pcie_s2r_conventional(pcie) && pm_resume_via_firmware()) + apple_pcie_hib_restore_noirq(pcie, "S2R"); return 0; + } if (pcie->resume_failed) return -EIO; /* @@ -6542,6 +6558,11 @@ static bool apple_pcie_hib_conventional(struct apple_pcie *pcie) !pcie->hw->root_bus_only; } +static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie) +{ + return READ_ONCE(s2r_conventional) && apple_pcie_hib_conventional(pcie); +} + /* What only the port registers hold, for a restore that finds them reset. */ static void apple_pcie_hib_save(struct apple_pcie *pcie) { @@ -6606,7 +6627,7 @@ static int apple_pcie_hib_keep_fn(struct pci_dev *pdev, void *data) * every cold boot; nothing is written to a port in this phase. Its PWREN is an SMC GPIO, and * the SMC session is back only in the normal phase. */ -static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie) +static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what) { struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); struct apple_pcie_port *port; @@ -6620,8 +6641,8 @@ static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie) continue; port->hib_cold = true; cold = true; - dev_info(pcie->dev, "resume from disk: %pOF lost its link, setting it up again\n", - port->np); + dev_info(pcie->dev, "%s: %pOF lost its link, setting it up again\n", + what, port->np); } if (cold && bridge->bus) pci_walk_bus(bridge->bus, apple_pcie_hib_keep_fn, pcie); @@ -6693,16 +6714,14 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por * The normal phase, after the SMC's own restore (the PWREN GPIOs are its) and before the PCI * core's normal restore of the functions, which waits for this host as their ancestor. */ -static int apple_pcie_restore(struct device *dev) +static void apple_pcie_conventional_up(struct apple_pcie *pcie, const char *what) { - struct apple_pcie *pcie = apple_pcie_lookup(dev); + struct device *dev = pcie->dev; struct apple_pcie_port *port; struct apple_pcie_hib_fn *fn; unsigned int back = 0; int ret; - if (!apple_pcie_hib_conventional(pcie) || !pm_hibernate_is_restored()) - return 0; list_for_each_entry(port, &pcie->ports, entry) { if (!port->hib_link_up) continue; @@ -6716,13 +6735,13 @@ static int apple_pcie_restore(struct device *dev) } ret = apple_pcie_hib_port_up(pcie, port); if (ret) { - dev_err(dev, "resume from disk: %pOF link not back (%d), its functions stay unreachable\n", - port->np, ret); + dev_err(dev, "%s: %pOF link not back (%d), its functions stay unreachable\n", + what, port->np, ret); port->hib_cold = false; continue; } - dev_info(dev, "resume from disk: %pOF link back, PWREN and PERST# cycled as at probe\n", - port->np); + dev_info(dev, "%s: %pOF link back, PWREN and PERST# cycled as at probe\n", + what, port->np); back++; } list_for_each_entry(fn, &pcie->hib_fns, entry) { @@ -6730,13 +6749,33 @@ static int apple_pcie_restore(struct device *dev) if (!port || !port->hib_cold) continue; if (pci_load_saved_state(fn->pdev, fn->state)) - pci_warn(fn->pdev, "resume from disk: saved configuration not taken back\n"); + pci_warn(fn->pdev, "%s: saved configuration not taken back\n", what); } apple_pcie_hib_free_fns(pcie); list_for_each_entry(port, &pcie->ports, entry) port->hib_cold = false; if (back) - dev_dbg(dev, "resume from disk: %u port(s) set up again\n", back); + dev_dbg(dev, "%s: %u port(s) set up again\n", what, back); +} + +static int apple_pcie_restore(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (!apple_pcie_hib_conventional(pcie) || !pm_hibernate_is_restored()) + return 0; + apple_pcie_conventional_up(pcie, "resume from disk"); + return 0; +} + +/* The normal phase of a real S2R's resume, ordered as the restore above. */ +static int apple_pcie_resume(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (!apple_pcie_s2r_conventional(pcie) || !pm_resume_via_firmware()) + return 0; + apple_pcie_conventional_up(pcie, "S2R"); return 0; } @@ -6778,7 +6817,7 @@ static int apple_pcie_restore_noirq(struct device *dev) if (!pcie->hw->tunnel_hibernate) { if (apple_pcie_hib_conventional(pcie) && pm_hibernate_is_restored()) - apple_pcie_hib_restore_noirq(pcie); + apple_pcie_hib_restore_noirq(pcie, "resume from disk"); return 0; } return apple_pcie_resume_noirq(dev); @@ -6790,6 +6829,7 @@ static const struct dev_pm_ops apple_pcie_pm_ops = { .suspend_noirq = apple_pcie_suspend_noirq, .resume_noirq = apple_pcie_resume_noirq, .resume_early = apple_pcie_resume_early, + .resume = apple_pcie_resume, .freeze_noirq = apple_pcie_hibernate_noirq, .thaw_noirq = apple_pcie_wake_noirq, .poweroff_noirq = apple_pcie_hibernate_noirq, From 3f16a938a0d06e0dd99248a7c7112d9dbd0ee2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 18:16:18 +0100 Subject: [PATCH 182/269] nvme: apple: describe the T6040 sleep as it runs now MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 entry still said a power-loss sleep is refused and only a dry run reaches it, and left the warm restart's endpoint map echo as an unmeasured TODO. The sleep controller now cuts the power, and ANS comes back with RTKit's default echo of the whole map: say so. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index c5f2935f4c9eb..b7a6df65671cc 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -3313,12 +3313,11 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { * restart, controller reset + namespace re-attach) are all generic and * already wired through this sleep_untraced state; the controller stops * ANS at the commit and raises its two ST lanes on wake - * (t6040_sleep_lower/raise_apcie_st, U4). What flips a real power-loss - * sleep from refused to allowed is NOT a flag here but the controller's - * apple,apcie validity: with the ST-lane windows + PLL/lane tunables - * absent it refuses (apple_sleep_power_loss() stays unreachable), so a dry - * run is all that runs -- the flush, the quiesce and the shutdown - * notification, then the controller reset as after any refusal. + * (t6040_sleep_lower/raise_apcie_st). Whether a sleep cuts the power + * is the controller's decision (apple_sleep_power_loss()); a dry run (2 + * or 3) flushes, quiesces and sends the shutdown notification at the + * device suspend, as a sleep does, and resets the controller as after + * any refusal. * * CC.SHN: the platform 26.7.1 on this SoC, at power state 0, * shuts NVMe down in its suspend variant: @@ -3326,9 +3325,9 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { * polls CSTS.SHST for complete. Flush: namespace 1 only, the OS's own; the * others belong to the firmware, as on T8140. * - * TODO(HW): sleep_epmap_last_mask (the warm-restart last-epmap echo) is - * unmeasured on T6040 ANS, so it is left at the rtkit default - * (echo all); the first wake's warm_restart / AURA_ANS_WATCH log sets it. + * sleep_epmap_last_mask stays 0, RTKit's default of echoing the whole + * endpoint map on the warm restart: ANS came back with it in the J616s + * S2R tests that cut the power. */ .sleep_untraced = true, .sleep_flush_max_nsid = 1, From 564e4305a9f4fb5e049524f9b767f3607e0bdfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 02:18:15 +0100 Subject: [PATCH 183/269] Bluetooth: hci_bcm4377: restart the transport of a chip a sleep left without firmware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a J616s the BCM4388 comes back from a suspend to RAM without its firmware: the function's private configuration windows read their reset values (window 1 0x19328000 where the driver had set 0x18002000) and, once the windows are written again, the boot stage reads 0. The resume wrote UNQUIESCE through the wrong window and sent its HCI commands to a controller that could not answer. Each timed out after 2 s ("hci0: command 0x0c01 tx timeout"), the driver was rebound, and the resume of devices took 2 to 4 s longer. Read window 1 at resume. If it was reset, write the windows again and read the boot stage and the RTI status. Without a running firmware nothing is sent: the send callback fails, which ends a pending command at once, the transport is marked for its hardware reset, and the recovery that a command timeout starts on the J493 (close and open of the transport, function reset, firmware boot, calibration, address) is queued from the resume itself, without the timeout. The J616s joins that recovery; hci0 stays registered. A function that a resume found cold stays cold until its reset has booted it, also when a later resume finds the windows as the first one wrote them (a suspend that fenced the queued recovery and did not reset the function). A cold resume queues a setup retry the suspend held back, as a warm one does: the retry's open resets the function. tools/asahi: bcm4377-resume-tests models the configuration windows, the two status words and the send callback and gains the cold cases with their mutations; bcm4377-id-tests admits the BCM4388 on a J616s next to the BCM4378 on a J493. J616s, suspend to RAM: the driver's resume takes 0.1 to 0.5 ms (2023 and 4022 ms before) and "hardware reset recovery completed successfully" follows 0.42 to 0.47 s after the end of the resume. Six suspends, two of them after a hibernation, the last three with the function's WAKE# interrupt armed (wake-gpios), where the suspend writes no QUIESCE: controller powered after each, the same hci0. Signed-off-by: František Balázsy --- drivers/bluetooth/hci_bcm4377.c | 76 +++++- tools/asahi/bcm4377-id-tests.py | 18 +- tools/asahi/bcm4377-resume-tests.py | 33 ++- tools/asahi/bcm4377-resume/controls.c | 330 +++++++++++++++++++++++++- tools/asahi/bcm4377-resume/fixture.h | 143 ++++++++++- 5 files changed, 572 insertions(+), 28 deletions(-) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index 02cc8fccbc27c..f4eba1ae1e3ea 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -619,6 +619,8 @@ struct bcm4377_data { bool resume_powered; bool resume_guarded; bool resume_recovering; + /* Back from a system sleep without its firmware: nothing is sent until the reset. */ + bool resume_cold; struct work_struct resume_work; bdaddr_t bdaddr; @@ -1477,6 +1479,7 @@ static int bcm4377_pci_reset(struct bcm4377_data *bcm4377) } bcm4377->needs_reset = false; + WRITE_ONCE(bcm4377->resume_cold, false); dev_info(&bcm4377->pdev->dev, "hardware reset recovery completed successfully\n"); return 0; @@ -1594,8 +1597,8 @@ static void bcm4377_setup_retry_work(struct work_struct *work) static bool bcm4377_resume_recovery_supported(struct bcm4377_data *bcm4377) { - return bcm4377->hw->id == 0x4378 && - of_machine_is_compatible("apple,j493"); + return (bcm4377->hw->id == 0x4378 && of_machine_is_compatible("apple,j493")) || + (bcm4377->hw->id == 0x4388 && of_machine_is_compatible("apple,j616s")); } static bool bcm4377_resume_recovery_allowed(struct hci_dev *hdev) @@ -1642,8 +1645,12 @@ static int bcm4377_resume_recovery_sync(struct hci_dev *hdev, void *data) if (!allowed) return 0; - dev_warn(&bcm4377->pdev->dev, - "recovering transport once after resume command timeout\n"); + if (READ_ONCE(bcm4377->resume_cold)) + dev_info(&bcm4377->pdev->dev, + "restarting the transport: the sleep left no firmware running\n"); + else + dev_warn(&bcm4377->pdev->dev, + "recovering transport once after resume command timeout\n"); ret = hci_reset_dev_sync(hdev); spin_lock_irqsave(&bcm4377->resume_lock, flags); bcm4377->resume_recovering = false; @@ -1991,6 +1998,12 @@ static int bcm4377_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) struct bcm4377_transfer_ring *ring; int ret; + /* No firmware behind the rings: fail at once, a command must not wait out its timeout. */ + if (READ_ONCE(bcm4377->resume_cold)) { + hdev->stat.err_tx++; + return -ENODEV; + } + switch (hci_skb_pkt_type(skb)) { case HCI_COMMAND_PKT: hdev->stat.cmd_tx++; @@ -3178,11 +3191,35 @@ static int bcm4377_suspend(struct device *dev) return ret; } +/* + * A system sleep that takes the chip's power or resets it leaves the function's private + * windows at their reset values and no firmware: a J616s after a suspend to RAM reads + * window 1 as 0x19328000 and, once the windows are back, boot stage 0. Put the windows + * back and say whether a firmware still runs behind them. + */ +static bool bcm4377_resume_cold(struct bcm4377_data *bcm4377) +{ + u32 win = bcm4377->hw->bar0_window1, bootstage, rti_status; + + pci_read_config_dword(bcm4377->pdev, BCM4377_PCIECFG_BAR0_WINDOW1, &win); + if (win == bcm4377->hw->bar0_window1) + return false; + if (bcm4377_init_cfg(bcm4377)) + return true; + bootstage = ioread32(bcm4377->bar2 + bcm4377->hw->bar2_offset + BCM4377_BAR2_BOOTSTAGE); + rti_status = ioread32(bcm4377->bar2 + bcm4377->hw->bar2_offset + BCM4377_BAR2_RTI_STATUS); + dev_info(&bcm4377->pdev->dev, + "the sleep reset the function (window %#x): boot stage %u, RTI status %u\n", + win, bootstage, rti_status); + return bootstage != 2 || rti_status != 2; +} + static int bcm4377_resume(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); struct bcm4377_data *bcm4377 = pci_get_drvdata(pdev); unsigned long flags; + bool cold; int ret; ret = apple_radio_sleep_endpoint_resume(dev); @@ -3194,6 +3231,16 @@ static int bcm4377_resume(struct device *dev) bcm4377->wake_armed = false; } + /* A function an earlier sleep left cold stays cold until a reset boots it. */ + cold = bcm4377_resume_recovery_supported(bcm4377) && + (bcm4377_resume_cold(bcm4377) || READ_ONCE(bcm4377->resume_cold)); + if (cold) { + /* No firmware to wake: the next open resets the function and boots it. */ + bcm4377->quiesced = false; + bcm4377->needs_reset = true; + WRITE_ONCE(bcm4377->resume_cold, true); + } + if (bcm4377->quiesced) { iowrite32(BCM4377_BAR0_SLEEP_CONTROL_UNQUIESCE, bcm4377->bar0 + BCM4377_BAR0_SLEEP_CONTROL); @@ -3213,11 +3260,24 @@ static int bcm4377_resume(struct device *dev) ret = hci_resume_dev(bcm4377->hdev); apple_radio_sleep_endpoint_resumed(dev, ret); - if (!apple_radio_sleep_owned(dev)) { - WRITE_ONCE(bcm4377->setup_retry_blocked, false); - if (!ret) - bcm4377_queue_setup_retry(bcm4377); + if (apple_radio_sleep_owned(dev)) + return ret; + WRITE_ONCE(bcm4377->setup_retry_blocked, false); + if (cold) { + /* What a command timeout would start, without the timeout. */ + spin_lock_irqsave(&bcm4377->resume_lock, flags); + if (bcm4377->resume_armed) { + bcm4377->resume_armed = false; + bcm4377->resume_pending = bcm4377->resume_epoch; + queue_work(system_freezable_wq, &bcm4377->resume_work); + } + spin_unlock_irqrestore(&bcm4377->resume_lock, flags); + /* A setup retry the suspend held back needs no firmware: its open resets. */ + bcm4377_queue_setup_retry(bcm4377); + return 0; } + if (!ret) + bcm4377_queue_setup_retry(bcm4377); return ret; } diff --git a/tools/asahi/bcm4377-id-tests.py b/tools/asahi/bcm4377-id-tests.py index 167139cfffbc4..e211281d8e467 100755 --- a/tools/asahi/bcm4377-id-tests.py +++ b/tools/asahi/bcm4377-id-tests.py @@ -40,7 +40,7 @@ def take(source, pattern): struct hci_dev { void (*reset)(struct hci_dev *); }; static void bcm4377_hci_timeout(struct hci_dev *hdev) { (void)hdev; } static const char *board; -static bool of_machine_is_compatible(const char *compat) { assert(!strcmp(compat,"apple,j493"));return !strcmp(board,compat); } +static bool of_machine_is_compatible(const char *compat) { assert(!strcmp(compat,"apple,j493")||!strcmp(compat,"apple,j616s"));return !strcmp(board,compat); } ''' SUFFIX = r''' @@ -51,16 +51,19 @@ def take(source, pattern): } int main(void) { unsigned cases=0; - const char *boards[]={"apple,j493","apple,j273","apple,j293","apple,j313","apple,j274","apple,j375","apple,j473","apple,j474","apple,j414s","apple,j416c","apple,j613","apple,j615","apple,j514s","apple,j700"}; + const char *boards[]={"apple,j493","apple,j273","apple,j293","apple,j313","apple,j274","apple,j375","apple,j473","apple,j474","apple,j414s","apple,j416c","apple,j613","apple,j615","apple,j514s","apple,j616s","apple,j700"}; assert(bcm4377_hw_variants[BCM4378].id==0x4378); assert(bcm4377_hw_variants[BCM4378].id!=4378); + assert(bcm4377_hw_variants[BCM4388].id==0x4388); + assert(bcm4377_hw_variants[BCM4388].id!=4388); for(unsigned chip=0;chip<4;chip++) { const struct pci_device_id *id=&bcm4377_devid_table[chip]; assert(id->vendor==PCI_VENDOR_ID_BROADCOM); assert(id->driver_data==chip); for(unsigned b=0;bdevice==BCM4378_DEVICE_ID && !strcmp(board,"apple,j493"); + bool expected=(id->device==BCM4378_DEVICE_ID && !strcmp(board,"apple,j493")) || + (id->device==BCM4388_DEVICE_ID && !strcmp(board,"apple,j616s")); assert(probe_registration(&data,id,&hdev)==expected); assert(data.hw==&bcm4377_hw_variants[id->driver_data]); assert(bcm4377_resume_recovery_supported(&data)==expected); @@ -71,6 +74,9 @@ def take(source, pattern): board="apple,j493"; struct bcm4377_hw wrong=bcm4377_hw_variants[BCM4378];wrong.id=4378; struct bcm4377_data decimal={.hw=&wrong};assert(!bcm4377_resume_recovery_supported(&decimal));cases++; + board="apple,j616s"; + struct bcm4377_hw wrong88=bcm4377_hw_variants[BCM4388];wrong88.id=4388; + struct bcm4377_data decimal88={.hw=&wrong88};assert(!bcm4377_resume_recovery_supported(&decimal88));cases++; printf("%u table-backed identity and callback cases PASS\n",cases);return 0; } ''' @@ -125,6 +131,12 @@ def main(): gates['decimal-id'] = pieces['gate'].replace('0x4378', '4378') gates['other-chip'] = pieces['gate'].replace('0x4378', '0x4387') gates['board-bypass'] = pieces['gate'].replace('of_machine_is_compatible("apple,j493")', 'true') + # The J616s clause gets the same three, and the two clauses must not trade boards. + assert pieces['gate'].count('0x4388') == 1 + gates['j616s-decimal-id'] = pieces['gate'].replace('0x4388', '4388') + gates['j616s-other-chip'] = pieces['gate'].replace('0x4388', '0x4387') + gates['j616s-board-bypass'] = pieces['gate'].replace('of_machine_is_compatible("apple,j616s")', 'true') + gates['crossed-boards'] = pieces['gate'].replace('"apple,j493"', '@').replace('"apple,j616s"', '"apple,j493"').replace('@', '"apple,j616s"') results = {} def no_core(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) diff --git a/tools/asahi/bcm4377-resume-tests.py b/tools/asahi/bcm4377-resume-tests.py index f8dfa488d5079..07e68dc7a4a58 100755 --- a/tools/asahi/bcm4377-resume-tests.py +++ b/tools/asahi/bcm4377-resume-tests.py @@ -3,7 +3,7 @@ """Compile retained Bluetooth resume callbacks against controlled core/PCI services.""" import argparse, hashlib, json, os, re, resource, subprocess from pathlib import Path -p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--old-ref');p.add_argument('--mutation',choices=['epoch','close','producer-fence','active-fence','board']);a=p.parse_args() +p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--old-ref');p.add_argument('--mutation',choices=['epoch','close','producer-fence','active-fence','board','board-j616s','cold-gate','cold-windows','cold-running-fw','cold-rti','cold-unquiesce','cold-reset','cold-send','cold-queue','cold-unarmed','cold-rearm','cold-error','cold-sticky','cold-setup-retry']);a=p.parse_args() r=Path(__file__).resolve().parents[2];o=Path(a.out);o.mkdir(parents=True,exist_ok=False);(o/'tmp').mkdir() def extract(s,n): m=re.search(r'^(?:static )?(?:int|void|bool) '+n+r'\([^;]*?\)\n\{',s,re.M|re.S) @@ -12,7 +12,7 @@ def extract(s,n): while depth:depth+=(s[b]=='{')-(s[b]=='}');b+=1 return s[m.start():b]+'\n' s=(r/'drivers/bluetooth/hci_bcm4377.c').read_text();core=(r/'net/bluetooth/hci_sync.c').read_text();core_hci=(r/'net/bluetooth/hci_core.c').read_text() -names=['bcm4377_setup_retry_allowed','bcm4377_queue_setup_retry','bcm4377_setup_retry_work','bcm4377_resume_recovery_supported','bcm4377_resume_recovery_allowed','bcm4377_resume_recovery_invalidate','bcm4377_resume_recovery_sync','bcm4377_resume_recovery_destroy','bcm4377_resume_recovery_work','bcm4377_hci_timeout','bcm4377_resume_recovery_stop','bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_setup_once','bcm4377_hci_setup','bcm4377_hci_post_init','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume'] +names=['bcm4377_setup_retry_allowed', 'bcm4377_queue_setup_retry', 'bcm4377_setup_retry_work', 'bcm4377_resume_recovery_supported', 'bcm4377_resume_recovery_allowed', 'bcm4377_resume_recovery_invalidate', 'bcm4377_resume_recovery_sync', 'bcm4377_resume_recovery_destroy', 'bcm4377_resume_recovery_work', 'bcm4377_hci_timeout', 'bcm4377_resume_recovery_stop', 'bcm4377_hci_open', 'bcm4377_hci_close', 'bcm4377_hci_setup_once', 'bcm4377_hci_setup', 'bcm4377_hci_post_init', 'bcm4377_hci_send_frame', 'bcm4377_init_cfg', 'bcm4377_hci_unregister_dev', 'bcm4377_suspend', 'bcm4377_resume_cold', 'bcm4377_resume', 'bcm4377_sleep_finish'] bodies={n:extract(s,n) for n in names};shared=[] if a.old_ref: old=subprocess.check_output(['git','show',a.old_ref+':drivers/bluetooth/hci_bcm4377.c'],cwd=r,text=True) @@ -24,9 +24,24 @@ def extract(s,n): 'producer-fence':('bcm4377_resume_recovery_stop','cancel_work_sync(&bcm4377->resume_work);','',21), 'active-fence':('bcm4377_resume_recovery_stop','flush_work(&bcm4377->hdev->cmd_sync_work);','',40), 'board':('bcm4377_resume_recovery_supported','of_machine_is_compatible("apple,j493")','true',13), + 'board-j616s':('bcm4377_resume_recovery_supported','of_machine_is_compatible("apple,j616s")','true',16), + # A resume that finds the function reset by the sleep and without firmware. + 'cold-gate':('bcm4377_resume','bcm4377_resume_recovery_supported(bcm4377) &&\n\t (bcm4377_resume_cold','(bcm4377_resume_cold',54), + 'cold-windows':('bcm4377_resume_cold','if (bcm4377_init_cfg(bcm4377))\n\t\treturn true;','',46), + 'cold-running-fw':('bcm4377_resume_cold','return bootstage != 2 || rti_status != 2;','return true;',50), + 'cold-rti':('bcm4377_resume_cold','bootstage != 2 || rti_status != 2','bootstage != 2 && rti_status != 2',52), + 'cold-unquiesce':('bcm4377_resume','\t\tbcm4377->quiesced = false;\n\t\tbcm4377->needs_reset = true;','\t\tbcm4377->needs_reset = true;',46), + 'cold-reset':('bcm4377_resume','\t\tbcm4377->needs_reset = true;\n','',48), + 'cold-send':('bcm4377_hci_send_frame','READ_ONCE(bcm4377->resume_cold)','false',58), + 'cold-queue':('bcm4377_resume','queue_work(system_freezable_wq, &bcm4377->resume_work);','',46), + 'cold-unarmed':('bcm4377_resume','if (bcm4377->resume_armed) {','if (true) {',48), + 'cold-rearm':('bcm4377_resume','\t\t\tbcm4377->resume_armed = false;\n','',46), + 'cold-error':('bcm4377_resume','\t\treturn 0;\n\t}\n\tif (!ret)','\t}\n\tif (!ret)',47), + 'cold-sticky':('bcm4377_resume',' || READ_ONCE(bcm4377->resume_cold)','',61), + 'cold-setup-retry':('bcm4377_resume','\t\tbcm4377_queue_setup_retry(bcm4377);\n\t\treturn 0;','\t\treturn 0;',62), } if a.mutation: - n,old,new,case=mutations[a.mutation];assert old in bodies[n];bodies[n]=bodies[n].replace(old,new,1) + n,old,new,case=mutations[a.mutation];assert bodies[n].count(old)==1,(n,old);bodies[n]=bodies[n].replace(old,new,1) core_bodies={n:extract(core_hci,n) for n in ['hci_cmd_timeout','hci_cancel_cmd_sync','hci_suspend_dev','hci_resume_dev']} # The fixture models UP as a mask; the core uses a bit index. core_bodies={n:v.replace('test_bit(HCI_UP,', 'test_bit(HCI_UP_INDEX,') for n,v in core_bodies.items()} @@ -35,9 +50,13 @@ def extract(s,n): hw_enum=re.search(r'^enum bcm4377_chip \{.*?^\};',s,re.M|re.S).group() hw_table=re.search(r'^static const struct bcm4377_hw bcm4377_hw_variants\[\] = \{.*?^\};',s,re.M|re.S).group() fixture=re.sub(r'^struct bcm4377_hw \{.*?\};', 'typedef uint32_t u32;\n'+hw_struct,fixture,count=1,flags=re.M) -# The variant table names register offsets by macro; take their definitions from the driver. -hw_macros='\n'+''.join(m.group()+'\n' for name in sorted(set(re.findall(r'\b[A-Z][A-Z0-9_]+\b',hw_table))) for m in [re.search(r'^#define '+name+r'\b.*$',s,re.M)] if m) +# The variant table, the window setup and the cold-resume probe name registers by macro; take their definitions from the driver. +hw_macros='\n'+''.join(m.group()+'\n' for name in sorted(set(re.findall(r'\b[A-Z][A-Z0-9_]+\b',hw_table+extract(s,'bcm4377_init_cfg')+extract(s,'bcm4377_resume_cold')))) for m in [re.search(r'^#define '+name+r'\b.*$',s,re.M)] if m) assert 'BAR2_CONTEXT_ADDR_HI' in hw_macros,hw_macros +assert all(name in hw_macros for name in ('PCIECFG_BAR0_WINDOW1','PCIECFG_BAR2_WINDOW_DEFAULT','BAR2_BOOTSTAGE','BAR2_RTI_STATUS')),hw_macros +# The function reset is a fixture that ends the cold state on success; hold the driver to the same single place. +reset=extract(s,'bcm4377_pci_reset') +assert s.count('WRITE_ONCE(bcm4377->resume_cold, false)')==1 and re.search(r'^\tbcm4377->needs_reset = false;\n\tWRITE_ONCE\(bcm4377->resume_cold, false\);$',reset,re.M),'bcm4377_pci_reset' # bcm4377_suspend() undoes a failed wake arming through bcm4377_resume(), declared ahead of it. resume_decl=re.search(r'^static int bcm4377_resume\(struct device \*dev\);$',s,re.M).group()+'\n' fixture=fixture.replace('struct bcm4377_hw *hw;', 'const struct bcm4377_hw *hw;') @@ -56,10 +75,10 @@ def extract(s,n): (o/'compile.log').write_text(c.stdout+c.stderr);assert not c.returncode,c.stderr def no_core():resource.setrlimit(resource.RLIMIT_CORE,(0,0)) results=[] -for n in ([mutations[a.mutation][3]] if a.mutation else [0,1,2,28] if a.old_ref else range(46)): +for n in ([mutations[a.mutation][3]] if a.mutation else [0,1,2,28] if a.old_ref else range(63)): c=subprocess.run([str(o/'controls'),str(n)],capture_output=True,text=True,preexec_fn=no_core,env=dict(env,ASAN_OPTIONS='detect_leaks=1')) results.append({'case':n,'exit':c.returncode,'stdout':c.stdout,'stderr':c.stderr}) expected=-6 if a.old_ref or a.mutation else 0 -receipt={'source_sha256':hashlib.sha256(s.encode()).hexdigest(),'core_sha256':hashlib.sha256(core.encode()).hexdigest(),'core_hci_sha256':hashlib.sha256(core_hci.encode()).hexdigest(),'core_bodies':{n:hashlib.sha256(v.encode()).hexdigest() for n,v in core_bodies.items()},'actual_hw_struct_sha256':hashlib.sha256(hw_struct.encode()).hexdigest(),'actual_hw_table_sha256':hashlib.sha256(hw_table.encode()).hexdigest(),'table_callback_fixture':'Production variant table compiled unchanged; only calibration/PTB callback names map to controlled send boundaries.', 'extracted_bodies':{n:hashlib.sha256(v.encode()).hexdigest() for n,v in bodies.items()},'shared_new_prerequisites_for_old':shared,'old_ref':a.old_ref,'mutation':a.mutation,'core_fixture_adapter':'Only HCI_UP test_bit index is translated from fixture mask32 to index5; production conditions/order unchanged.', 'source_semantics':'Actual driver callbacks and synchronous close/open helper; core request serialization, PCI/firmware and HCI services are controlled fixtures, not whole-kernel or radio execution.','translation_sha256':hashlib.sha256(code.encode()).hexdigest(),'controls':results,'expected_exit':expected} +receipt={'source_sha256':hashlib.sha256(s.encode()).hexdigest(),'core_sha256':hashlib.sha256(core.encode()).hexdigest(),'core_hci_sha256':hashlib.sha256(core_hci.encode()).hexdigest(),'core_bodies':{n:hashlib.sha256(v.encode()).hexdigest() for n,v in core_bodies.items()},'actual_hw_struct_sha256':hashlib.sha256(hw_struct.encode()).hexdigest(),'actual_hw_table_sha256':hashlib.sha256(hw_table.encode()).hexdigest(),'table_callback_fixture':'Production variant table compiled unchanged; only calibration/PTB callback names map to controlled send boundaries.', 'extracted_bodies':{n:hashlib.sha256(v.encode()).hexdigest() for n,v in bodies.items()},'shared_new_prerequisites_for_old':shared,'old_ref':a.old_ref,'mutation':a.mutation,'core_fixture_adapter':'Only HCI_UP test_bit index is translated from fixture mask32 to index5; production conditions/order unchanged.', 'source_semantics':'Actual driver callbacks, config-window setup, cold-resume probe, send callback and synchronous close/open helper; core request serialization, PCI config space and BAR registers, the function reset with its firmware boot, and HCI services are controlled fixtures, not whole-kernel or radio execution.','reset_fixture':'bcm4377_pci_reset() is a controlled boundary; the driver source is held to clearing resume_cold only at the end of its success path.','translation_sha256':hashlib.sha256(code.encode()).hexdigest(),'controls':results,'expected_exit':expected} (o/'receipt.json').write_text(json.dumps(receipt,indent=2)+'\n');assert all(x['exit']==expected for x in results),results print(f'{len(results)} production controls passed') diff --git a/tools/asahi/bcm4377-resume/controls.c b/tools/asahi/bcm4377-resume/controls.c index ff558fd981ee8..de4866a7cd690 100644 --- a/tools/asahi/bcm4377-resume/controls.c +++ b/tools/asahi/bcm4377-resume/controls.c @@ -1,6 +1,90 @@ // SPDX-License-Identifier: GPL-2.0-only static bool timeout_during_resume; -static int hci_resume_sync(struct hci_dev *h) { if(timeout_during_resume)hci_cmd_timeout(&h->cmd_timer.work);return resume_error; } +static int resume_frames; +static char bars[2]; + +static int send(struct hci_dev *h, int type) +{ + unsigned char data[4] = {}; + struct sk_buff skb = { .pkt_type = type, .data = data, .len = sizeof(data) }; + + return bcm4377_hci_send_frame(h, &skb); +} + +/* The core sends its resume commands through the driver and carries on after one that fails. */ +static int hci_resume_sync(struct hci_dev *h) +{ + int i; + + if (timeout_during_resume) + hci_cmd_timeout(&h->cmd_timer.work); + for (i = 0; i < resume_frames; i++) + send(h, HCI_COMMAND_PKT); + return resume_error; +} + +/* + * The sleep control register answers through BAR0. The firmware's status words are only + * there through the windows the driver sets, never through the ones a sleep reset. + */ +static int ioread32(void *addr) +{ + struct bcm4377_data *b = active; + char *vars = b->bar2 + b->hw->bar2_offset; + + if (addr == b->bar0 + BCM4377_BAR0_SLEEP_CONTROL) + return 0; + assert(!memcmp(cfg, cfg_probe, sizeof(cfg))); + if (addr == vars + BCM4377_BAR2_BOOTSTAGE) + return fw_bootstage; + assert(addr == vars + BCM4377_BAR2_RTI_STATUS); + return fw_rti_status; +} + +/* What probe leaves in the function's private windows. */ +static void probe_cfg(struct bcm4377_data *b) +{ + memset(cfg, 0, sizeof(cfg)); + assert(!bcm4377_init_cfg(b)); + memcpy(cfg_probe, cfg, sizeof(cfg)); + cfg_reads = 0; + cfg_writes = 0; +} + +/* A sleep that takes the function's power: a J616s reads window 1 as 0x19328000 after it. */ +static void power_loss(void) +{ + memset(cfg, 0, sizeof(cfg)); + cfg[BCM4377_PCIECFG_BAR0_WINDOW1 / 4] = 0x19328000; + fw_bootstage = 0; + fw_rti_status = 0; +} + +static int cold_cycle(struct device *dev) +{ + assert(!bcm4377_suspend(dev)); + power_loss(); + return bcm4377_resume(dev); +} + +static void j616s(struct bcm4377_hw *hw, struct bcm4377_data *b) +{ + board_j493 = false; + board_j616s = true; + *hw = bcm4377_hw_variants[BCM4388]; + probe_cfg(b); +} + +static int power_on(struct hci_dev *h) +{ + int ret; + + req_locked = true; + ret = hci_dev_open_sync(h); + req_locked = false; + return ret; +} + static int hci_dev_close_sync(struct hci_dev *h) {assert(req_locked);h->flags&=~HCI_UP;return bcm4377_hci_close(h);} static int hci_dev_open_sync(struct hci_dev *h) {assert(req_locked);int e=bcm4377_hci_open(h);if(!e)e=bcm4377_hci_post_init(h);if(!e)h->flags|=HCI_UP;return e;} static void deliver_work(struct bcm4377_data *b) {if(b->resume_work.queued){b->resume_work.queued=0;bcm4377_resume_recovery_work(&b->resume_work);}} @@ -13,6 +97,9 @@ int main(int argc,char **argv) { struct bcm4377_hw hw=bcm4377_hw_variants[BCM4378];struct pci_dev pci={}; struct bcm4377_data b={.hw=&hw,.pdev=&pci};struct hci_dev h={.data=&b,.flags=HCI_UP|HCI_RUNNING};b.hdev=&h;active=&b;pci.data=&b;struct device dev={.pci=&pci};pthread_mutex_init(&b.resume_lock,NULL);if(bcm4377_resume_recovery_supported(&b))h.reset=bcm4377_hci_timeout; bdaddr_t own={{2,3,4,5,6,7}},setup={{8,9,10,11,12,13}},pub={{14,15,16,17,18,19}};b.bdaddr=own;h.setup_addr=setup;h.public_addr=pub; + b.bar0 = bars; + b.bar2 = bars + 1; + probe_cfg(&b); switch(atoi(argv[1])) { case 0: resume_cycle(&dev);recover(&b);assert(reset_count==1&&cal_count==1&&ptb_count==1&&addr_count==1&&!bacmp(&h.bdaddr,&own)&&(h.flags&HCI_UP)&&!b.cal_needed);break; case 1: timeout_during_resume=true;resume_cycle(&dev);deliver_work(&b);run_request(&h);assert(reset_count==1);break; @@ -108,6 +195,247 @@ int main(int argc,char **argv) { recover(&b); assert(rebind_count == 1); break; + /* + * J616s: the BCM4388 is back from the sleep with its windows reset and no firmware. + * Nothing is sent to it, and the resume itself queues the recovery, once. + */ + case 46: + j616s(&hw, &b); + resume_frames = 2; + assert(!cold_cycle(&dev)); + assert(!unquiesces && !b.quiesced); + assert(b.needs_reset && b.resume_cold && !memcmp(cfg, cfg_probe, sizeof(cfg))); + assert(h.stat.err_tx == 2 && !h.stat.cmd_tx && !enqueued && !skbs_freed); + assert(b.resume_work.queues == 1 && !reset_count && !request.fn); + hci_cmd_timeout(&h.cmd_timer.work); + assert(b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && cal_count == 1 && ptb_count == 1 && addr_count == 1); + assert(!bacmp(&h.bdaddr, &own) && (h.flags & HCI_UP) && !b.cal_needed); + assert(!b.needs_reset && !b.resume_cold); + assert(!send(&h, HCI_COMMAND_PKT) && enqueued == 1 && skbs_freed == 1); + recover(&b); + assert(reset_count == 1); + break; + /* A cold resume succeeds whatever the core's resume returns; a warm one reports it. */ + case 47: + j616s(&hw, &b); + resume_error = -EIO; + assert(!cold_cycle(&dev)); + assert(b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && (h.flags & HCI_UP)); + assert(!bcm4377_suspend(&dev)); + assert(bcm4377_resume(&dev) == -EIO); + assert(unquiesces == 1 && !b.resume_cold && b.resume_work.queues == 1); + break; + /* A controller that was not powered gets no recovery: its next open resets the function. */ + case 48: + j616s(&hw, &b); + h.flags = 0; + assert(!cold_cycle(&dev)); + assert(b.needs_reset && b.resume_cold); + assert(!unquiesces && !b.resume_work.queues); + recover(&b); + assert(!reset_count && send(&h, HCI_COMMAND_PKT) == -ENODEV); + assert(!power_on(&h)); + assert(reset_count == 1 && cal_count == 1 && addr_count == 1 && (h.flags & HCI_UP)); + assert(!b.resume_cold && !send(&h, HCI_COMMAND_PKT) && enqueued == 1); + break; + /* A command timeout inside the resume takes the recovery; the resume queues no second. */ + case 49: + j616s(&hw, &b); + timeout_during_resume = true; + assert(!cold_cycle(&dev)); + assert(b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !b.resume_cold); + recover(&b); + assert(reset_count == 1); + break; + /* Windows the sleep reset over a firmware that still runs: put back, then as before. */ + case 50: + j616s(&hw, &b); + resume_frames = 2; + assert(!bcm4377_suspend(&dev)); + power_loss(); + fw_bootstage = 2; + fw_rti_status = 2; + assert(!bcm4377_resume(&dev)); + assert(unquiesces == 1 && !misdirected && !b.needs_reset && !b.resume_cold); + assert(!b.resume_work.queues && enqueued == 2 && !h.stat.err_tx); + recover(&b); + assert(reset_count == 1); + break; + /* Windows the sleep left alone: nothing is written, resume and recovery as before. */ + case 51: + j616s(&hw, &b); + resume_frames = 2; + resume_cycle(&dev); + assert(!cfg_writes && unquiesces == 1 && !b.needs_reset && !b.resume_cold); + assert(!b.resume_work.queues && enqueued == 2 && !h.stat.err_tx); + recover(&b); + assert(reset_count == 1 && cal_count == 1 && addr_count == 1); + break; + /* Either status word alone says no firmware runs, and each such sleep is recovered. */ + case 52: + j616s(&hw, &b); + assert(!bcm4377_suspend(&dev)); + power_loss(); + fw_bootstage = 2; + assert(!bcm4377_resume(&dev)); + assert(b.resume_cold && !unquiesces); + deliver_work(&b); + run_request(&h); + assert(!bcm4377_suspend(&dev)); + power_loss(); + fw_rti_status = 2; + assert(!bcm4377_resume(&dev)); + assert(b.resume_cold && !unquiesces); + deliver_work(&b); + run_request(&h); + assert(reset_count == 2 && cal_count == 2 && addr_count == 2); + assert(b.resume_work.queues == 2 && !b.resume_cold); + break; + /* The J493 pair takes the same path. */ + case 53: + assert(!cold_cycle(&dev)); + assert(!unquiesces && b.resume_cold && b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && cal_count == 1 && ptb_count == 1 && addr_count == 1); + assert((h.flags & HCI_UP) && !b.resume_cold); + break; + /* Any other chip and board pair is not looked at: its resume is the one before. */ + case 54: + hw = bcm4377_hw_variants[BCM4388]; + probe_cfg(&b); + assert(!cold_cycle(&dev)); + assert(!cfg_reads && !cfg_writes && unquiesces == 1); + assert(!b.needs_reset && !b.resume_cold && !send(&h, HCI_COMMAND_PKT)); + board_j493 = false; + board_j616s = true; + hw = bcm4377_hw_variants[BCM4378]; + probe_cfg(&b); + assert(!cold_cycle(&dev)); + assert(!cfg_reads && !cfg_writes && unquiesces == 2); + assert(!b.needs_reset && !b.resume_cold && !send(&h, HCI_COMMAND_PKT)); + recover(&b); + assert(!reset_count && !b.resume_work.queues); + break; + /* Windows that cannot be written back: no status word is read, the function is reset. */ + case 55: + j616s(&hw, &b); + assert(!bcm4377_suspend(&dev)); + power_loss(); + cfg_error = -EIO; + assert(!bcm4377_resume(&dev)); + cfg_error = 0; + assert(!unquiesces && b.needs_reset && b.resume_cold && b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !b.resume_cold); + break; + /* The next suspend fences the recovery a cold resume queued, as work and as request. */ + case 56: + j616s(&hw, &b); + assert(!cold_cycle(&dev)); + assert(b.resume_work.queued); + assert(!cold_cycle(&dev)); + assert(!request.fn && b.resume_work.queues == 2); + deliver_work(&b); + assert(request.fn); + assert(!cold_cycle(&dev)); + assert(!request.fn && b.resume_work.queues == 3 && quiesces == 3 && !unquiesces); + assert(!reset_count && b.resume_cold); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + break; + /* A power-off before the queued recovery runs invalidates it; the next open resets. */ + case 57: + j616s(&hw, &b); + assert(!cold_cycle(&dev)); + deliver_work(&b); + assert(request.fn); + h.flags &= ~HCI_UP; + bcm4377_hci_close(&h); + run_request(&h); + assert(!reset_count && b.resume_cold && b.needs_reset); + assert(!power_on(&h)); + assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + break; + /* The send callback: each frame reaches its ring, and none does while cold. */ + case 58: + j616s(&hw, &b); + assert(!send(&h, HCI_COMMAND_PKT) && enqueued_ring == &b.hci_h2d_ring); + assert(!send(&h, HCI_ACLDATA_PKT) && enqueued_ring == &b.acl_h2d_ring); + assert(!send(&h, HCI_SCODATA_PKT) && enqueued_ring == &b.sco_h2d_ring); + assert(enqueued == 3 && skbs_freed == 3 && h.stat.byte_tx == 12 && !h.stat.err_tx); + assert(h.stat.cmd_tx == 1 && h.stat.acl_tx == 1 && h.stat.sco_tx == 1); + assert(!cold_cycle(&dev)); + assert(send(&h, HCI_COMMAND_PKT) == -ENODEV); + assert(send(&h, HCI_ACLDATA_PKT) == -ENODEV); + assert(send(&h, HCI_SCODATA_PKT) == -ENODEV); + /* The core frees a frame the driver refused. */ + assert(enqueued == 3 && skbs_freed == 3 && h.stat.byte_tx == 12); + assert(h.stat.err_tx == 3 && h.stat.cmd_tx == 1 && h.stat.acl_tx == 1); + assert(h.stat.sco_tx == 1); + deliver_work(&b); + run_request(&h); + assert(!send(&h, HCI_COMMAND_PKT) && enqueued == 4 && skbs_freed == 4); + break; + /* A reset that fails leaves the function cold; the one that succeeds ends it. */ + case 59: + j616s(&hw, &b); + assert(!cold_cycle(&dev)); + reset_error = -EIO; + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !(h.flags & HCI_UP) && b.needs_reset && b.resume_cold); + reset_error = 0; + assert(!power_on(&h)); + assert(reset_count == 2 && !b.resume_cold && (h.flags & HCI_UP)); + assert(!send(&h, HCI_COMMAND_PKT)); + break; + /* Armed for wake: no QUIESCE went out, and the cold resume is the same. */ + case 60: + j616s(&hw, &b); + fixture_wakeup = true; + assert(!cold_cycle(&dev)); + assert(!quiesces && !unquiesces && !b.wake_armed); + assert(b.resume_cold && b.resume_work.queues == 1); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !b.resume_cold); + break; + /* A sleep that leaves the windows alone does not end the cold state an earlier one left. */ + case 61: + j616s(&hw, &b); + assert(!cold_cycle(&dev)); + assert(b.resume_work.queued); + resume_cycle(&dev); + assert(b.resume_cold && b.needs_reset && !unquiesces); + assert(b.resume_work.queues == 2); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + break; + /* A setup retry the suspend held back is queued again by a cold resume too. */ + case 62: + j616s(&hw, &b); + h.flags = HCI_SETUP | HCI_AUTO_OFF; + cal_failures = 1; + assert(bcm4377_hci_setup(&h) == -ETIMEDOUT); + bcm4377_hci_close(&h); + assert(b.setup_retry_pending && b.setup_retry_work.work.queued); + assert(!cold_cycle(&dev)); + assert(b.resume_cold && !b.resume_work.queues); + assert(b.setup_retry_pending && b.setup_retry_work.work.queued); + break; default:abort(); } if (atoi(argv[1]) == 6 || atoi(argv[1]) == 44 || atoi(argv[1]) == 45) diff --git a/tools/asahi/bcm4377-resume/fixture.h b/tools/asahi/bcm4377-resume/fixture.h index 5eece3476e65f..c0b3f095e9acc 100644 --- a/tools/asahi/bcm4377-resume/fixture.h +++ b/tools/asahi/bcm4377-resume/fixture.h @@ -34,9 +34,15 @@ #define BCM4377_BAR0_SLEEP_CONTROL_QUIESCE 1 #define BCM4377_BAR0_SLEEP_CONTROL_UNQUIESCE 0 #define system_freezable_wq 0 +#define BIT(n) (1U << (n)) +#define HCI_COMMAND_PKT 1 +#define HCI_ACLDATA_PKT 2 +#define HCI_SCODATA_PKT 3 #define container_of(p,t,m) ((t*)((char*)(p)-offsetof(t,m))) #define to_delayed_work(p) container_of(p,struct delayed_work,work) -struct work_struct { int queued; }; +struct work_struct { + int queued, queues; +}; struct delayed_work { struct work_struct work; }; typedef struct { unsigned char b[6]; } bdaddr_t; static const bdaddr_t zero={}; @@ -45,8 +51,29 @@ static void bacpy(bdaddr_t *a,const bdaddr_t *b) { *a=*b; } static int bacmp(const bdaddr_t *a,const bdaddr_t *b) { return memcmp(a,b,6); } struct bcm4377_data; struct firmware { const unsigned char *data; int size; }; -struct hci_dev { struct bcm4377_data *data; unsigned int flags; void *req_workqueue,*workqueue; struct work_struct power_on,cmd_sync_work,cmd_work; struct delayed_work cmd_timer,ncmd_timer; int req_lock,cmd_cnt,suspend_state,wake_reason,wake_addr_type; bdaddr_t wake_addr; int *req_skb; void (*reset)(struct hci_dev*); bdaddr_t setup_addr,public_addr,bdaddr; }; +struct hci_dev { + struct bcm4377_data *data; + unsigned int flags; + void *req_workqueue, *workqueue; + struct work_struct power_on, cmd_sync_work, cmd_work; + struct delayed_work cmd_timer, ncmd_timer; + int req_lock, cmd_cnt, suspend_state, wake_reason, wake_addr_type; + bdaddr_t wake_addr; + int *req_skb; + void (*reset)(struct hci_dev *hdev); + bdaddr_t setup_addr, public_addr, bdaddr; + struct { + int err_tx, cmd_tx, acl_tx, sco_tx, byte_tx; + } stat; +}; struct ring { bool enabled; }; +/* bcm4377_hci_send_frame() names the transfer ring type; the fixture has one ring type. */ +#define bcm4377_transfer_ring ring +struct sk_buff { + int pkt_type; + unsigned char *data; + unsigned int len; +}; struct pci_dev { int irq, dev; void *data; @@ -59,11 +86,11 @@ struct bcm4377_data { bool setup_retry_used, setup_retry_pending, setup_retry_blocked; struct delayed_work setup_retry_work; bdaddr_t bdaddr; - char *bar0; + char *bar0, *bar2; pthread_mutex_t resume_lock; uint64_t resume_epoch, resume_pending; bool resume_armed, resume_blocked, resume_powered; - bool resume_guarded, resume_recovering; + bool resume_guarded, resume_recovering, resume_cold; struct work_struct resume_work; struct ring acl_d2h_ring,acl_h2d_ring,sco_d2h_ring,sco_h2d_ring,hci_d2h_ring,hci_h2d_ring,sco_event_ring,sco_ack_ring,hci_acl_event_ring,hci_acl_ack_ring; }; @@ -91,10 +118,42 @@ static int bcm4377_check_bdaddr(struct bcm4377_data *b) { return 0; } static int bcm4377_destroy_transfer_ring(struct bcm4377_data *b,struct ring *r) { r->enabled=false;return close_error; } static int bcm4377_destroy_completion_ring(struct bcm4377_data *b,struct ring *r) { r->enabled=false;return close_error; } static int bcm4377_hci_set_bdaddr(struct hci_dev *h,const bdaddr_t *a) { addr_count++;if(addr_error)return addr_error;h->bdaddr=*a;return 0; } -static int bcm4377_pci_reset(struct bcm4377_data *b) { reset_count++;if(active_callback_pause){pthread_barrier_wait(&active_started);pthread_barrier_wait(&active_release);}if(reset_error)return reset_error;b->needs_reset=false;return 0; } + +/* What the function shows the driver: its private config windows and the firmware's status. */ +static uint32_t cfg[64], cfg_probe[64]; +static int cfg_reads, cfg_writes, cfg_error, fw_bootstage = 2, fw_rti_status = 2; + +/* + * Controlled boundary for the function reset, the window setup, the firmware boot and the + * RTI handshake. Like the driver's, a reset that succeeds ends the cold state. + */ +static int bcm4377_pci_reset(struct bcm4377_data *b) +{ + reset_count++; + if (active_callback_pause) { + pthread_barrier_wait(&active_started); + pthread_barrier_wait(&active_release); + } + if (reset_error) + return reset_error; + memcpy(cfg, cfg_probe, sizeof(cfg)); + fw_bootstage = 2; + fw_rti_status = 2; + b->needs_reset = false; + b->resume_cold = false; + return 0; +} + static int bcm4377_hci_open_rings(struct bcm4377_data *b) { open_count++;return rings_error; } static int mod_delayed_work(int wq,struct delayed_work *w,int delay) { w->work.queued=1;return 1; } -static int queue_work(void *wq,struct work_struct *w) { w->queued=1;queued_count++;return 1; } + +static int queue_work(void *wq, struct work_struct *w) +{ + w->queued = 1; + w->queues++; + queued_count++; + return 1; +} typedef uint64_t u64; #define spin_lock_irqsave(l,f) do { (f)=0;pthread_mutex_lock(l); } while(0) @@ -107,7 +166,16 @@ static pthread_t producer_thread; static void hci_cmd_timeout(struct work_struct *work); static struct bcm4377_data *active; static bool hdev_is_powered(struct hci_dev *h) {return (h->flags&HCI_UP) && !(h->flags&HCI_AUTO_OFF);} -static bool of_machine_is_compatible(const char *s) {assert(!strcmp(s,"apple,j493"));return board_j493;} +static bool board_j616s; + +static bool of_machine_is_compatible(const char *s) +{ + if (!strcmp(s, "apple,j616s")) + return board_j616s; + assert(!strcmp(s, "apple,j493")); + return board_j493; +} + static void *kmalloc(size_t n,int flags) {return allocation_error?NULL:malloc(n);} static void kfree(void *p) {free(p);} #define bt_dev_warn(...) ((void)0) @@ -122,7 +190,23 @@ static void cancel_delayed_work_sync(struct delayed_work *w) {if(w==&active->hde #define disable_delayed_work_sync cancel_delayed_work_sync static struct pci_dev *to_pci_dev(struct device *d) {return d->pci;} static void *pci_get_drvdata(struct pci_dev *p) {return p->data;} -static void iowrite32(int val,void *addr) {if(val==1){assert(!request.fn && !active->resume_work.queued && !active_running);quiesces++;}} + +/* An UNQUIESCE through windows a sleep reset does not reach the sleep control register. */ +static int unquiesces, misdirected; + +static void iowrite32(int val, void *addr) +{ + if (val == BCM4377_BAR0_SLEEP_CONTROL_QUIESCE) { + assert(!request.fn && !active->resume_work.queued && !active_running); + quiesces++; + return; + } + assert(val == BCM4377_BAR0_SLEEP_CONTROL_UNQUIESCE); + unquiesces++; + if (memcmp(cfg, cfg_probe, sizeof(cfg))) + misdirected++; +} + static bool fixture_wakeup; static bool device_may_wakeup(struct device *d) { return fixture_wakeup; } /* Not a J713 sleep: the radio sleep hooks do nothing, as on every other board. */ @@ -138,7 +222,48 @@ static void apple_radio_sleep_endpoint_resumed(struct device *d, int result) {} static void pci_save_state(struct pci_dev *p) {} static int enable_irq_wake(int irq) { return 0; } static void disable_irq_wake(int irq) {} -static int ioread32(void *addr) { return 0; } +static int ioread32(void *addr); + +static int pci_read_config_dword(struct pci_dev *p, int where, uint32_t *val) +{ + assert(p == active->pdev && !(where & 3) && where < 256); + cfg_reads++; + *val = cfg[where / 4]; + return 0; +} + +static int pci_write_config_dword(struct pci_dev *p, int where, uint32_t val) +{ + assert(p == active->pdev && !(where & 3) && where < 256); + if (cfg_error) + return cfg_error; + cfg_writes++; + cfg[where / 4] = val; + return 0; +} + +static int enqueued, skbs_freed; +static struct ring *enqueued_ring; + +static int hci_skb_pkt_type(struct sk_buff *skb) +{ + return skb->pkt_type; +} + +static int bcm4377_enqueue(struct bcm4377_data *b, struct ring *r, void *data, size_t len, + bool wait) +{ + assert(!wait); + enqueued++; + enqueued_ring = r; + return 0; +} + +static void kfree_skb(struct sk_buff *skb) +{ + skbs_freed++; +} + static int hci_suspend_dev(struct hci_dev *h); static int hci_resume_dev(struct hci_dev *h); static void hci_unregister_dev(struct hci_dev *h) {assert(!request.fn&&!active->resume_work.queued&&!active_running);h->flags|=HCI_UNREGISTER;unregistered++;} From d06908716525e509204ef0acfbea88c423bfe000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 02:18:15 +0100 Subject: [PATCH 184/269] PCI: apple: bring a conventional host's links up in the noirq phase after a real S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A suspend to RAM takes the power of the port blocks of the J616s's internal PCIe host. The sleep's wake side clocks the ports again; the links stay down, the pin restore releases PERST#, and the ports and their root ports hold reset values. The host set the links up again in its normal resume, one phase after the PCI core needs them: brcmfmac 0000:01:00.0: Unable to change power state from D3hot to D0, device inaccessible hci_bcm4377 0000:01:00.1: Unable to change power state from D3hot to D0, device inaccessible pci 0000:02:00.0: Unable to change power state from unknown to D0, device inaccessible and it put back nothing of what the boot loader had set. After the wake the root ports advertised 16GT/s x4 where they had 5GT/s x1, link mode x4, and the port tunable, the root port tunables and port word 0x130 were gone. The link still trained at 5GT/s x1 only because the PCI core restores Link Control 2. Save the port word and the root port's link words at suspend, parse the tunables the loader publishes for a port (apple,tunable-port, -rc, -rc-gen3, -rc-gen4), and after a real S2R do in resume_noirq what the loader and probe did between them: PERST# asserted again, the reference clock, the port's settings, PERST# released, the root port's settings under the DBI write enable with a directed speed change, the link trained. The endpoint's power line is the SMC's and is not touched; it reads on after the wake. A port that does not come up there is left to the normal phase as before, and so is a resume from disk, where the loader has just set the ports up and the power line may be off. s2r_noirq_up=0 keeps the old order. J616s, six suspends, the last three with the WAKE# interrupts of the two radio functions armed: both links back in the noirq phase with the words of a fresh boot (LnkCap 5GT/s x1, every tunable as published), none of the three messages. "noirq resume of devices" takes 0.42 s for the two ports; the host's normal resume, 0.65 s before, has nothing left to do. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 156 +++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index a614df81ddc42..aeada2631f86d 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -87,6 +87,9 @@ MODULE_PARM_DESC(link_up_timeout, "PCIe link training timeout in milliseconds"); */ static bool s2r_conventional = true; module_param(s2r_conventional, bool, 0644); +static bool s2r_noirq_up = true; +module_param(s2r_noirq_up, bool, 0644); +MODULE_PARM_DESC(s2r_noirq_up, "After a real S2R bring a conventional host's links up in the noirq phase, with what the boot loader had set on the ports put back"); MODULE_PARM_DESC(s2r_conventional, "Set a conventional host's ports up again after a real S2R, as after a resume from disk"); /* T8103 (original M1) and related SoCs */ @@ -622,6 +625,15 @@ struct apple_pcie_port { unsigned long adopted_slots; /* adopt_link: RID2SID slots routed (slot == SID) */ struct gpio_desc *pwren; /* adopt_link: endpoint power, if Linux trained */ bool powered; /* cold train: pwren raised, port not parked yet */ + /* + * Conventional port: what the boot loader set on the port and its root port, which a + * sleep that powers the block down loses. + */ + struct apple_tunable *s2r_port_tunable; + struct apple_tunable *s2r_rc_tunable[3]; + u32 s2r_port_130; + u32 s2r_dbi[5]; + bool s2r_saved; /* Conventional port: link up when the image was taken */ bool hib_link_up; /* ... and found down by the restore: the boot that resumed did not set it up */ @@ -2973,6 +2985,28 @@ static void apple_pcie_port_gate_clocks(struct apple_pcie *pcie, rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK); } +/* + * The tunables the loader applied to a conventional port and its root port at boot, where it + * publishes them: written again after a sleep that reset the block. Optional. + */ +static void apple_pcie_port_s2r_tunables(struct apple_pcie *pcie, struct apple_pcie_port *port, + struct resource *port_res) +{ + static const char * const rc_names[] = { + "apple,tunable-rc", "apple,tunable-rc-gen3", "apple,tunable-rc-gen4", + }; + struct resource rc = DEFINE_RES_MEM(0, SZ_4K); + struct apple_tunable *t; + int i; + + t = devm_apple_tunable_parse(pcie->dev, port->np, "apple,tunable-port", port_res); + port->s2r_port_tunable = IS_ERR(t) ? NULL : t; + for (i = 0; i < ARRAY_SIZE(rc_names); i++) { + t = devm_apple_tunable_parse(pcie->dev, port->np, rc_names[i], &rc); + port->s2r_rc_tunable[i] = IS_ERR(t) ? NULL : t; + } +} + static int apple_pcie_setup_port(struct apple_pcie *pcie, struct device_node *np) { @@ -3036,6 +3070,8 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, if (ret) goto err_teardown; } + if (!pcie->hw->tunneled && !pcie->hw->adopt_link && !pcie->managed_sids) + apple_pcie_port_s2r_tunables(pcie, port, res); if (pcie->hw->tunneled) { port->tunable = devm_apple_tunable_parse(pcie->dev, np, "apple,tunable", res); @@ -5857,6 +5893,7 @@ static void apple_pcie_neo_save_functions(struct apple_pcie *pcie); static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie); static void apple_pcie_hib_save(struct apple_pcie *pcie); static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what); +static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie); /* * The J716c radio drivers save PCI state without entering D3. The PCI core @@ -6457,8 +6494,11 @@ static int apple_pcie_resume_noirq(struct device *dev) return ret; } if (!pcie->hw->tunneled) { - if (apple_pcie_s2r_conventional(pcie) && pm_resume_via_firmware()) + if (apple_pcie_s2r_conventional(pcie) && pm_resume_via_firmware()) { + if (READ_ONCE(s2r_noirq_up)) + apple_pcie_s2r_up_noirq(pcie); apple_pcie_hib_restore_noirq(pcie, "S2R"); + } return 0; } if (pcie->resume_failed) @@ -6563,6 +6603,119 @@ static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie) return READ_ONCE(s2r_conventional) && apple_pcie_hib_conventional(pcie); } +/* + * Root port words the loader limits at boot (one lane at the endpoint's speed) and a reset + * puts back to the core's own: link capabilities, the target speed, link mode and lane count. + */ +static const u16 apple_pcie_s2r_dbi[] = { + NEO_DBI_CAP_LNKCAP, NEO_DBI_CAP_LNKCAP2, NEO_DBI_CAP_LNKCTL2, + NEO_DBI_PORT_LINK_CONTROL, NEO_DBI_WIDTH_SPEED_CONTROL, +}; + +static int apple_pcie_hib_train(struct apple_pcie *pcie, struct apple_pcie_port *port); +static void apple_pcie_hib_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port); + +static void apple_pcie_s2r_save_port(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + int i; + + BUILD_BUG_ON(ARRAY_SIZE(apple_pcie_s2r_dbi) != ARRAY_SIZE(port->s2r_dbi)); + /* The loader's port setup writes 0x130; the port tunable has its own word. */ + port->s2r_port_130 = apple_pcie_port_readl(port, 0x130); + for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) + port->s2r_dbi[i] = readl_relaxed(dbi + apple_pcie_s2r_dbi[i]); + port->s2r_saved = true; +} + +/* The root port part goes in once the port runs again, before the link starts. */ +static void apple_pcie_s2r_restore_rc(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + int i; + + rmw_set(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); + for (i = 0; i < ARRAY_SIZE(port->s2r_rc_tunable); i++) + apple_tunable_apply(dbi, port->s2r_rc_tunable[i]); + for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) { + /* Link Control 2 shares its word with a status register. */ + if (apple_pcie_s2r_dbi[i] == NEO_DBI_CAP_LNKCTL2) + writew_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); + else + writel_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); + } + /* The link goes to the target speed once it is up, as after the loader's setup. */ + rmw_set(NEO_DBI_SPEED_CHANGE, dbi + NEO_DBI_WIDTH_SPEED_CONTROL); + rmw_clear(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); +} + +/* + * A real S2R took the port block's power: the sleep's wake side clocked the port again and + * the pin restore released PERST#, with the link down and every word of the port and of its + * root port at its reset value. Do what the loader and probe did between them, here, so that + * the PCI core's noirq pass finds the functions: PERST# asserted again, the port's and the + * root port's settings back, PERST# released, the link trained. The endpoint's power line + * is not touched: it is the SMC's, whose session is back only in the normal phase, and it + * reads on after the wake. + */ +static int apple_pcie_s2r_port_up_noirq(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + u32 stat; + int ret; + + rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); + gpiod_set_value(port->perst_gpio, 1); + if (!pcie->hw->retain_clocks) { + ret = apple_pcie_setup_refclk(pcie, port); + if (ret < 0) + return ret; + } + apple_pcie_port_writel(port, port->s2r_port_130, 0x130); + apple_tunable_apply(port->base, port->s2r_port_tunable); + /* Tperst-clk (PCIe CEM r5.0, 2.9.2) */ + usleep_range(100, 200); + rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); + gpiod_set_value(port->perst_gpio, 0); + /* 100 ms after PERST# (PCIe r5.0, 6.6.1) */ + msleep(100); + ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, + stat & PORT_STATUS_READY, 100, 250000); + if (ret) + return ret; + apple_pcie_s2r_restore_rc(pcie, port); + apple_pcie_port_gate_clocks(pcie, port); + apple_pcie_tunnel_restore_irq_hw(port); + apple_pcie_hib_rid2sid(pcie, port); + ret = apple_pcie_hib_train(pcie, port); + if (ret) + return ret; + apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + msleep(100); + return 0; +} + +/* A port that does not come up here is left to the normal phase, as before. */ +static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) +{ + struct apple_pcie_port *port; + int ret; + + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->hib_link_up || !port->s2r_saved || !port->perst_gpio || + gpiod_cansleep(port->perst_gpio) || + (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP)) + continue; + ret = apple_pcie_s2r_port_up_noirq(pcie, port); + if (ret) + dev_warn(pcie->dev, "S2R: %pOF link not back in the noirq phase (%d, LTSSM %#x)\n", + port->np, ret, apple_pcie_port_ltssm(pcie, port)); + else + dev_info(pcie->dev, "S2R: %pOF link back with the loader's port settings\n", + port->np); + } +} + /* What only the port registers hold, for a restore that finds them reset. */ static void apple_pcie_hib_save(struct apple_pcie *pcie) { @@ -6571,6 +6724,7 @@ static void apple_pcie_hib_save(struct apple_pcie *pcie) mutex_lock(&pcie->lock); list_for_each_entry(port, &pcie->ports, entry) { + apple_pcie_s2r_save_port(pcie, port); port->hib_link_up = apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP; port->hib_cold = false; port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); From 59788bdc2698e82602ec3a95b90fc6edbb3c0f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:22:53 +0100 Subject: [PATCH 185/269] PCI: apple: set a conventional host up after a real S2R without switches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two module parameters guarded what a conventional host does after a real suspend-to-RAM: s2r_conventional for setting its ports up again as after a resume from disk, and s2r_noirq_up for bringing its links up in the noirq phase. Both were on, and the radio behind the host comes back only with both. The host now always does both, and the predicate that only added the parameter to apple_pcie_hib_conventional() goes. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index aeada2631f86d..9bb97876b8788 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -81,17 +81,6 @@ static int link_up_timeout = 500; module_param(link_up_timeout, int, 0644); MODULE_PARM_DESC(link_up_timeout, "PCIe link training timeout in milliseconds"); -/* - * T6040 S2R (bring-up): a real sleep takes a conventional host's power as a - * resume from disk does, so its ports come back the same way. - */ -static bool s2r_conventional = true; -module_param(s2r_conventional, bool, 0644); -static bool s2r_noirq_up = true; -module_param(s2r_noirq_up, bool, 0644); -MODULE_PARM_DESC(s2r_noirq_up, "After a real S2R bring a conventional host's links up in the noirq phase, with what the boot loader had set on the ports put back"); -MODULE_PARM_DESC(s2r_conventional, "Set a conventional host's ports up again after a real S2R, as after a resume from disk"); - /* T8103 (original M1) and related SoCs */ #define CORE_RC_PHYIF_CTL 0x00024 #define CORE_RC_PHYIF_CTL_RUN BIT(0) @@ -5890,7 +5879,7 @@ static void apple_pcie_stop_for_sleep(struct device *dev) } static void apple_pcie_neo_save_functions(struct apple_pcie *pcie); -static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie); +static bool apple_pcie_hib_conventional(struct apple_pcie *pcie); static void apple_pcie_hib_save(struct apple_pcie *pcie); static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what); static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie); @@ -6001,7 +5990,7 @@ static int apple_pcie_suspend_noirq(struct device *dev) if (!pcie->hw->tunneled) { if (pm_suspend_no_platform() && pcie->num_pds) return apple_pcie_hold_domains(pcie); - if (apple_pcie_s2r_conventional(pcie) && pm_suspend_via_firmware()) + if (apple_pcie_hib_conventional(pcie) && pm_suspend_via_firmware()) apple_pcie_hib_save(pcie); return 0; } @@ -6494,9 +6483,8 @@ static int apple_pcie_resume_noirq(struct device *dev) return ret; } if (!pcie->hw->tunneled) { - if (apple_pcie_s2r_conventional(pcie) && pm_resume_via_firmware()) { - if (READ_ONCE(s2r_noirq_up)) - apple_pcie_s2r_up_noirq(pcie); + if (apple_pcie_hib_conventional(pcie) && pm_resume_via_firmware()) { + apple_pcie_s2r_up_noirq(pcie); apple_pcie_hib_restore_noirq(pcie, "S2R"); } return 0; @@ -6591,6 +6579,8 @@ static int apple_pcie_prepare(struct device *dev) * that resumed it went through the boot loader, and when its kernel did not set the ports up * (a boot kernel without the GPIO drivers that probing the host takes), the endpoints sit * unpowered or in reset behind a link that is down. The restore then does what probe did. + * A real suspend-to-RAM takes the host's power as a resume from disk does, so its ports + * come back the same way. */ static bool apple_pcie_hib_conventional(struct apple_pcie *pcie) { @@ -6598,11 +6588,6 @@ static bool apple_pcie_hib_conventional(struct apple_pcie *pcie) !pcie->hw->root_bus_only; } -static bool apple_pcie_s2r_conventional(struct apple_pcie *pcie) -{ - return READ_ONCE(s2r_conventional) && apple_pcie_hib_conventional(pcie); -} - /* * Root port words the loader limits at boot (one lane at the endpoint's speed) and a reset * puts back to the core's own: link capabilities, the target speed, link mode and lane count. @@ -6927,7 +6912,7 @@ static int apple_pcie_resume(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (!apple_pcie_s2r_conventional(pcie) || !pm_resume_via_firmware()) + if (!apple_pcie_hib_conventional(pcie) || !pm_resume_via_firmware()) return 0; apple_pcie_conventional_up(pcie, "S2R"); return 0; From f66b5c377158ce623393f942c32d00164430344d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:01:05 +0100 Subject: [PATCH 186/269] nvme-apple: one prefix rule for the step functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/nvme/host/apple.c part of "soc: apple: sleep: t6040: one prefix rule for the step functions", which goes with the PCIe, storage and radio changes; the rest of that commit is with the sleep controller changes. Steps on one block were named both t6040_sleep__() and t6040__(). Keep t6040_sleep_* for the driver's own frame (suspend operations, probe, mapping, blockers, the commit and the wake return) and name each step on a block t6040__(), as most of them already were. No functional change: the object is unchanged but for the symbol names. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index b7a6df65671cc..8e3b76f7ff693 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -3313,7 +3313,7 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { * restart, controller reset + namespace re-attach) are all generic and * already wired through this sleep_untraced state; the controller stops * ANS at the commit and raises its two ST lanes on wake - * (t6040_sleep_lower/raise_apcie_st). Whether a sleep cuts the power + * (t6040_apcie_st_lower/raise). Whether a sleep cuts the power * is the controller's decision (apple_sleep_power_loss()); a dry run (2 * or 3) flushes, quiesces and sends the shutdown notification at the * device suspend, as a sleep does, and resets the controller as after From 1236dc1b45c022ca403358ad467fb90ca41fc7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:05:04 +0100 Subject: [PATCH 187/269] iommu/apple-dart: drop a quoted log from the restore comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment over apple_dart_reimport_locked() quoted a client's error message and named where it was seen. The failure it describes, every DMA mapping through the DART, says enough. Comment only, no functional change. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 5f932df4934fb..972d200b7ffd4 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -6708,8 +6708,7 @@ static __maybe_unused int apple_dart_sys_resume_noirq(struct device *dev) * again with fresh firmware-owned roots, while the restored kernel still * describes the roots of the image (their mapping, their firmware snapshot * and the slots it had published). Every publication then fails (-EBUSY), - * and with it every DMA mapping through the DART (observed for the PMP and - * every DCP: "failed buffer request ... (-12)"). Re-import the live roots + * and with it every DMA mapping through the DART. Re-import the live roots * the way attach maps them, then publish the stream's page tables into them * again, as attach does. Firmware-owned memory is never written: the old root's * published slots are forgotten, not retired. From 99c8a1dec3830a2cfa109bbde99a86a25c56b8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:14:12 +0100 Subject: [PATCH 188/269] iommu/apple-dart: report the locked stream re-import at debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every resume from disk printed an info line per locked DART, ending in an unexplained number. Print it at debug level and say that the number is the first error. Only a log call changes. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 972d200b7ffd4..295b775388ec4 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -6802,8 +6802,8 @@ static int apple_dart_reimport_locked(struct apple_dart *dart) } done++; } - dev_info(dart->dev, "hibernation restore: %d locked streams re-imported from the boot loader's roots (%d)\n", - done, ret); + dev_dbg(dart->dev, "hibernation restore: %d locked streams re-imported from the boot loader's roots, first error %d\n", + done, ret); return ret; } From d7b3b366f8403ce9dea1fe28738d2d7f6352d30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:19:10 +0100 Subject: [PATCH 189/269] iommu/apple-dart: set the restore callback with pm_sleep_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore callback and the re-import sat in an #ifdef CONFIG_HIBERNATION block, with an #else in the dev_pm_ops that pointed .restore at the resume callback. Without CONFIG_HIBERNATION the PM core never calls .restore, and pm_hibernate_is_restored() is false there, so the restore callback falls back to the resume one by itself. Build it unconditionally and set it with pm_sleep_ptr(). With CONFIG_HIBERNATION the object code is unchanged. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 295b775388ec4..2e17363d032a0 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -6702,7 +6702,6 @@ static __maybe_unused int apple_dart_sys_resume_noirq(struct device *dev) return pm_suspend_via_firmware() ? apple_dart_resume_noirq(dev) : 0; } -#ifdef CONFIG_HIBERNATION /* * Resume from disk is a cold boot: the boot loader locked every locked DART * again with fresh firmware-owned roots, while the restored kernel still @@ -6826,7 +6825,6 @@ static int apple_dart_restore(struct device *dev) pm_runtime_put(dev); return 0; } -#endif /* A resume from disk also re-imports the roots the boot loader locked again. */ static const struct dev_pm_ops apple_dart_pm_ops = { @@ -6835,11 +6833,7 @@ static const struct dev_pm_ops apple_dart_pm_ops = { .freeze = pm_sleep_ptr(apple_dart_system_suspend), .thaw = pm_sleep_ptr(apple_dart_system_resume), .poweroff = pm_sleep_ptr(apple_dart_system_suspend), -#ifdef CONFIG_HIBERNATION - .restore = apple_dart_restore, -#else - .restore = pm_sleep_ptr(apple_dart_system_resume), -#endif + .restore = pm_sleep_ptr(apple_dart_restore), NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_dart_sys_suspend_noirq, apple_dart_sys_resume_noirq) RUNTIME_PM_OPS(apple_dart_suspend, apple_dart_resume, NULL) }; From cf6ed825be8c42e73a4222fc62e584b04f14236c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:28:54 +0100 Subject: [PATCH 190/269] Bluetooth: hci_bcm4377: resume the core after a cold function's reset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A BCM4388 that a suspend to RAM left without its firmware cannot answer a command until the function was reset and the firmware booted. The resume still called hci_resume_dev() on it: the core sent the commands of its resume, the send callback refused each one, and every wake logged them at error level before the transport was restarted: Bluetooth: hci0: sending frame failed (-19) Bluetooth: hci0: Opcode 0x0c01 failed: -19 Bluetooth: hci0: sending frame failed (-19) Bluetooth: hci0: Opcode 0x0c1a failed: -19 hci_bcm4377 0000:01:00.1: transport needs hardware reset on next open A controller with a scan filter, advertising or a discovery to restore gets more of these. The restart is a close and an open of the transport, and the open programs all of that again, so nothing was lost; but the commands were sent to a function that was known not to answer, and a wake that worked as designed read like a failure. The core's resume cannot simply be left out. It is what clears the core's suspended state, which a close and an open do not touch: without it the controller comes back with page scan, advertising and discovery paused for good. So the resume of a cold function now only queues the reset and leaves the core suspended. The open that booted the firmware queues the core's resume, which then finds a controller that answers: the event mask and the scan state are written once, to a running firmware. This holds for any open: after a reset that failed, or a power-off between the wake and the queued reset, the core resumes behind the next open that succeeds. The next suspend fences that work like the recovery itself. A cold function without a queued reset (a controller that was not powered) is resumed at once, as before; the core does nothing for it. The close of that restart no longer warns that the transport needs a hardware reset: the driver has just said why it restarts it. The core's own PM notifier calls hci_resume_dev() as well, once tasks are thawed. The queued reset is thawed with them and normally holds the request lock by then: the notifier waits for the open or finds the controller down, and either way the core resumes only once the firmware answers. Should the notifier run first, its commands fail as before. tools/asahi: the fixture models the core's suspended mark, the cold cases hold that no frame reaches a cold function, and new cases and mutations cover the resume behind the open, the failed reset, the power-off in between and both fences. Signed-off-by: František Balázsy --- drivers/bluetooth/hci_bcm4377.c | 74 +++++++++- tools/asahi/bcm4377-resume-tests.py | 16 ++- tools/asahi/bcm4377-resume/controls.c | 199 ++++++++++++++++++++------ tools/asahi/bcm4377-resume/fixture.h | 28 +++- 4 files changed, 261 insertions(+), 56 deletions(-) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index f4eba1ae1e3ea..b0b662daee3eb 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -621,7 +621,10 @@ struct bcm4377_data { bool resume_recovering; /* Back from a system sleep without its firmware: nothing is sent until the reset. */ bool resume_cold; + /* The HCI core is still suspended: it resumes after the open that boots the firmware. */ + bool hci_resume_owed; struct work_struct resume_work; + struct work_struct hci_resume_work; bdaddr_t bdaddr; wait_queue_head_t event; @@ -1695,6 +1698,45 @@ static void bcm4377_resume_recovery_work(struct work_struct *work) bcm4377_resume_recovery_destroy(bcm4377->hdev, epoch, ret); } +/* Resume the HCI core. It stays suspended, and owed, while the controller is not powered. */ +static int bcm4377_hci_resume(struct bcm4377_data *bcm4377) +{ + struct hci_dev *hdev = bcm4377->hdev; + int ret = hci_resume_dev(hdev); + + if (!READ_ONCE(hdev->suspended)) + WRITE_ONCE(bcm4377->hci_resume_owed, false); + return ret; +} + +/* + * A function that came back from a sleep without its firmware answers none of the commands + * the core's resume sends. The core stays suspended until an open has reset the function + * and booted the firmware, and resumes here, after that open. + */ +static void bcm4377_hci_resume_work(struct work_struct *work) +{ + struct bcm4377_data *bcm4377 = container_of(work, struct bcm4377_data, + hci_resume_work); + struct hci_dev *hdev = bcm4377->hdev; + + /* The open that queued this holds the request lock until the controller is up. */ + hci_req_sync_lock(hdev); + hci_req_sync_unlock(hdev); + if (READ_ONCE(bcm4377->hci_resume_owed)) + bcm4377_hci_resume(bcm4377); +} + +static void bcm4377_queue_hci_resume(struct bcm4377_data *bcm4377) +{ + unsigned long flags; + + spin_lock_irqsave(&bcm4377->resume_lock, flags); + if (READ_ONCE(bcm4377->hci_resume_owed) && !bcm4377->resume_blocked) + queue_work(system_freezable_wq, &bcm4377->hci_resume_work); + spin_unlock_irqrestore(&bcm4377->resume_lock, flags); +} + static void bcm4377_hci_reset(struct hci_dev *hdev); static void bcm4377_hci_timeout(struct hci_dev *hdev) @@ -1742,6 +1784,8 @@ static void bcm4377_resume_recovery_stop(struct bcm4377_data *bcm4377) NULL, NULL); /* A callback already removed from the queue must retire before PCI quiesces. */ flush_work(&bcm4377->hdev->cmd_sync_work); + /* An open that ended ahead of this fence may have queued the core's resume. */ + cancel_work_sync(&bcm4377->hci_resume_work); } static int bcm4377_hci_open(struct hci_dev *hdev) @@ -1820,8 +1864,10 @@ static int bcm4377_hci_close(struct hci_dev *hdev) } if (bcm4377->needs_reset) { - dev_warn(&bcm4377->pdev->dev, - "transport needs hardware reset on next open\n"); + /* Expected of a function a sleep left cold; its restart has said so. */ + if (!READ_ONCE(bcm4377->resume_cold)) + dev_warn(&bcm4377->pdev->dev, + "transport needs hardware reset on next open\n"); bcm4377->acl_d2h_ring.enabled = false; bcm4377->acl_h2d_ring.enabled = false; bcm4377->sco_d2h_ring.enabled = false; @@ -1955,7 +2001,7 @@ static int bcm4377_hci_post_init(struct hci_dev *hdev) int ret; if (!bcm4377->cal_needed) - return 0; + goto out; if (!bcm4377->cal_loaded) { dev_info(&bcm4377->pdev->dev, @@ -1989,6 +2035,9 @@ static int bcm4377_hci_post_init(struct hci_dev *hdev) } bcm4377->cal_needed = false; +out: + /* The firmware answers: a core that a cold resume left suspended can resume. */ + bcm4377_queue_hci_resume(bcm4377); return 0; } @@ -2977,6 +3026,7 @@ static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id) INIT_DELAYED_WORK(&bcm4377->setup_retry_work, bcm4377_setup_retry_work); spin_lock_init(&bcm4377->resume_lock); INIT_WORK(&bcm4377->resume_work, bcm4377_resume_recovery_work); + INIT_WORK(&bcm4377->hci_resume_work, bcm4377_hci_resume_work); ret = bcm4377_prepare_rings(bcm4377); if (ret) @@ -3258,24 +3308,34 @@ static int bcm4377_resume(struct device *dev) } bcm4377->resume_powered = false; - ret = hci_resume_dev(bcm4377->hdev); - apple_radio_sleep_endpoint_resumed(dev, ret); - if (apple_radio_sleep_owned(dev)) + /* The radio-sleep owner's endpoint resumes as it suspended: whole, and reports it. */ + if (apple_radio_sleep_owned(dev)) { + ret = bcm4377_hci_resume(bcm4377); + apple_radio_sleep_endpoint_resumed(dev, ret); return ret; - WRITE_ONCE(bcm4377->setup_retry_blocked, false); + } if (cold) { /* What a command timeout would start, without the timeout. */ spin_lock_irqsave(&bcm4377->resume_lock, flags); if (bcm4377->resume_armed) { bcm4377->resume_armed = false; bcm4377->resume_pending = bcm4377->resume_epoch; + /* Nothing answers before that reset: the core resumes after the open. */ + WRITE_ONCE(bcm4377->hci_resume_owed, true); queue_work(system_freezable_wq, &bcm4377->resume_work); } spin_unlock_irqrestore(&bcm4377->resume_lock, flags); + /* No reset queued: the controller is down or the core's resume fails at once. */ + if (!READ_ONCE(bcm4377->hci_resume_owed)) + bcm4377_hci_resume(bcm4377); + WRITE_ONCE(bcm4377->setup_retry_blocked, false); /* A setup retry the suspend held back needs no firmware: its open resets. */ bcm4377_queue_setup_retry(bcm4377); return 0; } + + ret = bcm4377_hci_resume(bcm4377); + WRITE_ONCE(bcm4377->setup_retry_blocked, false); if (!ret) bcm4377_queue_setup_retry(bcm4377); return ret; diff --git a/tools/asahi/bcm4377-resume-tests.py b/tools/asahi/bcm4377-resume-tests.py index 07e68dc7a4a58..c7f1c6cd104a2 100755 --- a/tools/asahi/bcm4377-resume-tests.py +++ b/tools/asahi/bcm4377-resume-tests.py @@ -3,7 +3,7 @@ """Compile retained Bluetooth resume callbacks against controlled core/PCI services.""" import argparse, hashlib, json, os, re, resource, subprocess from pathlib import Path -p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--old-ref');p.add_argument('--mutation',choices=['epoch','close','producer-fence','active-fence','board','board-j616s','cold-gate','cold-windows','cold-running-fw','cold-rti','cold-unquiesce','cold-reset','cold-send','cold-queue','cold-unarmed','cold-rearm','cold-error','cold-sticky','cold-setup-retry']);a=p.parse_args() +p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--old-ref');p.add_argument('--mutation',choices=['epoch','close','producer-fence','active-fence','board','board-j616s','cold-gate','cold-windows','cold-running-fw','cold-rti','cold-unquiesce','cold-reset','cold-send','cold-queue','cold-unarmed','cold-rearm','cold-error','cold-sticky','cold-setup-retry','cold-defer','cold-open-queue','cold-owed-kept','cold-resume-fence','cold-queue-fence']);a=p.parse_args() r=Path(__file__).resolve().parents[2];o=Path(a.out);o.mkdir(parents=True,exist_ok=False);(o/'tmp').mkdir() def extract(s,n): m=re.search(r'^(?:static )?(?:int|void|bool) '+n+r'\([^;]*?\)\n\{',s,re.M|re.S) @@ -12,11 +12,11 @@ def extract(s,n): while depth:depth+=(s[b]=='{')-(s[b]=='}');b+=1 return s[m.start():b]+'\n' s=(r/'drivers/bluetooth/hci_bcm4377.c').read_text();core=(r/'net/bluetooth/hci_sync.c').read_text();core_hci=(r/'net/bluetooth/hci_core.c').read_text() -names=['bcm4377_setup_retry_allowed', 'bcm4377_queue_setup_retry', 'bcm4377_setup_retry_work', 'bcm4377_resume_recovery_supported', 'bcm4377_resume_recovery_allowed', 'bcm4377_resume_recovery_invalidate', 'bcm4377_resume_recovery_sync', 'bcm4377_resume_recovery_destroy', 'bcm4377_resume_recovery_work', 'bcm4377_hci_timeout', 'bcm4377_resume_recovery_stop', 'bcm4377_hci_open', 'bcm4377_hci_close', 'bcm4377_hci_setup_once', 'bcm4377_hci_setup', 'bcm4377_hci_post_init', 'bcm4377_hci_send_frame', 'bcm4377_init_cfg', 'bcm4377_hci_unregister_dev', 'bcm4377_suspend', 'bcm4377_resume_cold', 'bcm4377_resume', 'bcm4377_sleep_finish'] +names=['bcm4377_setup_retry_allowed','bcm4377_queue_setup_retry','bcm4377_setup_retry_work','bcm4377_resume_recovery_supported','bcm4377_resume_recovery_allowed','bcm4377_resume_recovery_invalidate','bcm4377_resume_recovery_sync','bcm4377_resume_recovery_destroy','bcm4377_resume_recovery_work','bcm4377_hci_resume','bcm4377_hci_resume_work','bcm4377_queue_hci_resume','bcm4377_hci_timeout','bcm4377_resume_recovery_stop','bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_setup_once','bcm4377_hci_setup','bcm4377_hci_post_init','bcm4377_hci_send_frame','bcm4377_init_cfg','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume_cold','bcm4377_resume'] bodies={n:extract(s,n) for n in names};shared=[] if a.old_ref: old=subprocess.check_output(['git','show',a.old_ref+':drivers/bluetooth/hci_bcm4377.c'],cwd=r,text=True) - for n in ['bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_unregister_dev','bcm4377_suspend','bcm4377_resume']:bodies[n]=extract(old,n) + for n in ['bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume']:bodies[n]=extract(old,n) shared=[n for n in names if 'resume_recovery' in n or n=='bcm4377_hci_timeout'] mutations={ 'epoch':('bcm4377_resume_recovery_sync','epoch == bcm4377->resume_epoch && ','' ,33), @@ -36,9 +36,15 @@ def extract(s,n): 'cold-queue':('bcm4377_resume','queue_work(system_freezable_wq, &bcm4377->resume_work);','',46), 'cold-unarmed':('bcm4377_resume','if (bcm4377->resume_armed) {','if (true) {',48), 'cold-rearm':('bcm4377_resume','\t\t\tbcm4377->resume_armed = false;\n','',46), - 'cold-error':('bcm4377_resume','\t\treturn 0;\n\t}\n\tif (!ret)','\t}\n\tif (!ret)',47), + 'cold-error':('bcm4377_resume','\t\treturn 0;\n\t}\n\n\tret = bcm4377_hci_resume(bcm4377);','\t}\n\n\tret = bcm4377_hci_resume(bcm4377);',47), 'cold-sticky':('bcm4377_resume',' || READ_ONCE(bcm4377->resume_cold)','',61), 'cold-setup-retry':('bcm4377_resume','\t\tbcm4377_queue_setup_retry(bcm4377);\n\t\treturn 0;','\t\treturn 0;',62), + # The core's resume waits for the open that boots the firmware, and is fenced like the recovery. + 'cold-defer':('bcm4377_resume','\t\tif (!READ_ONCE(bcm4377->hci_resume_owed))\n\t\t\tbcm4377_hci_resume(bcm4377);','\t\tbcm4377_hci_resume(bcm4377);',46), + 'cold-open-queue':('bcm4377_hci_post_init','\tbcm4377_queue_hci_resume(bcm4377);\n','',46), + 'cold-owed-kept':('bcm4377_hci_resume','\tif (!READ_ONCE(hdev->suspended))\n\t\tWRITE_ONCE','\tWRITE_ONCE',63), + 'cold-resume-fence':('bcm4377_resume_recovery_stop','\tcancel_work_sync(&bcm4377->hci_resume_work);\n','',64), + 'cold-queue-fence':('bcm4377_queue_hci_resume',' && !bcm4377->resume_blocked','',66), } if a.mutation: n,old,new,case=mutations[a.mutation];assert bodies[n].count(old)==1,(n,old);bodies[n]=bodies[n].replace(old,new,1) @@ -75,7 +81,7 @@ def extract(s,n): (o/'compile.log').write_text(c.stdout+c.stderr);assert not c.returncode,c.stderr def no_core():resource.setrlimit(resource.RLIMIT_CORE,(0,0)) results=[] -for n in ([mutations[a.mutation][3]] if a.mutation else [0,1,2,28] if a.old_ref else range(63)): +for n in ([mutations[a.mutation][3]] if a.mutation else [0,1,2,28] if a.old_ref else range(67)): c=subprocess.run([str(o/'controls'),str(n)],capture_output=True,text=True,preexec_fn=no_core,env=dict(env,ASAN_OPTIONS='detect_leaks=1')) results.append({'case':n,'exit':c.returncode,'stdout':c.stdout,'stderr':c.stderr}) expected=-6 if a.old_ref or a.mutation else 0 diff --git a/tools/asahi/bcm4377-resume/controls.c b/tools/asahi/bcm4377-resume/controls.c index de4866a7cd690..2cd4318ece449 100644 --- a/tools/asahi/bcm4377-resume/controls.c +++ b/tools/asahi/bcm4377-resume/controls.c @@ -11,11 +11,17 @@ static int send(struct hci_dev *h, int type) return bcm4377_hci_send_frame(h, &skb); } -/* The core sends its resume commands through the driver and carries on after one that fails. */ +/* + * The core resumes a controller it marked suspended, once: it sends its resume commands + * through the driver and carries on after one that fails. + */ static int hci_resume_sync(struct hci_dev *h) { int i; + if (!h->suspended) + return 0; + h->suspended = false; if (timeout_during_resume) hci_cmd_timeout(&h->cmd_timer.work); for (i = 0; i < resume_frames; i++) @@ -88,6 +94,23 @@ static int power_on(struct hci_dev *h) static int hci_dev_close_sync(struct hci_dev *h) {assert(req_locked);h->flags&=~HCI_UP;return bcm4377_hci_close(h);} static int hci_dev_open_sync(struct hci_dev *h) {assert(req_locked);int e=bcm4377_hci_open(h);if(!e)e=bcm4377_hci_post_init(h);if(!e)h->flags|=HCI_UP;return e;} static void deliver_work(struct bcm4377_data *b) {if(b->resume_work.queued){b->resume_work.queued=0;bcm4377_resume_recovery_work(&b->resume_work);}} + +/* The work that resumes the core after the open that booted the firmware. */ +static void deliver_hci_resume(struct bcm4377_data *b) +{ + if (b->hci_resume_work.queued) { + b->hci_resume_work.queued = 0; + bcm4377_hci_resume_work(&b->hci_resume_work); + } +} + +/* The whole recovery a cold resume queued: reset, open, then the core's resume. */ +static void cold_recover(struct bcm4377_data *b) +{ + deliver_work(b); + run_request(b->hdev); + deliver_hci_resume(b); +} static void resume_cycle(struct device *dev) {assert(!bcm4377_suspend(dev));assert(!bcm4377_resume(dev));} static void recover(struct bcm4377_data *b) {hci_cmd_timeout(&b->hdev->cmd_timer.work);deliver_work(b);run_request(b->hdev);} static void *active_callback(void *data) {run_request(data);return NULL;} @@ -197,7 +220,8 @@ int main(int argc,char **argv) { break; /* * J616s: the BCM4388 is back from the sleep with its windows reset and no firmware. - * Nothing is sent to it, and the resume itself queues the recovery, once. + * Nothing is sent to it: the resume queues the recovery, once, and leaves the core + * suspended. The core resumes after the open that booted the firmware. */ case 46: j616s(&hw, &b); @@ -205,7 +229,8 @@ int main(int argc,char **argv) { assert(!cold_cycle(&dev)); assert(!unquiesces && !b.quiesced); assert(b.needs_reset && b.resume_cold && !memcmp(cfg, cfg_probe, sizeof(cfg))); - assert(h.stat.err_tx == 2 && !h.stat.cmd_tx && !enqueued && !skbs_freed); + assert(!h.stat.err_tx && !h.stat.cmd_tx && !enqueued && !skbs_freed); + assert(h.suspended && b.hci_resume_owed && !b.hci_resume_work.queues); assert(b.resume_work.queues == 1 && !reset_count && !request.fn); hci_cmd_timeout(&h.cmd_timer.work); assert(b.resume_work.queues == 1); @@ -214,9 +239,14 @@ int main(int argc,char **argv) { assert(reset_count == 1 && cal_count == 1 && ptb_count == 1 && addr_count == 1); assert(!bacmp(&h.bdaddr, &own) && (h.flags & HCI_UP) && !b.cal_needed); assert(!b.needs_reset && !b.resume_cold); - assert(!send(&h, HCI_COMMAND_PKT) && enqueued == 1 && skbs_freed == 1); + assert(h.suspended && b.hci_resume_owed && !enqueued); + assert(b.hci_resume_work.queues == 1); + deliver_hci_resume(&b); + assert(!h.suspended && !b.hci_resume_owed); + assert(enqueued == 2 && skbs_freed == 2 && !h.stat.err_tx); + assert(!send(&h, HCI_COMMAND_PKT) && enqueued == 3 && skbs_freed == 3); recover(&b); - assert(reset_count == 1); + assert(reset_count == 1 && b.hci_resume_work.queues == 1); break; /* A cold resume succeeds whatever the core's resume returns; a warm one reports it. */ case 47: @@ -224,35 +254,39 @@ int main(int argc,char **argv) { resume_error = -EIO; assert(!cold_cycle(&dev)); assert(b.resume_work.queues == 1); - deliver_work(&b); - run_request(&h); - assert(reset_count == 1 && (h.flags & HCI_UP)); + cold_recover(&b); + assert(reset_count == 1 && (h.flags & HCI_UP) && !h.suspended); assert(!bcm4377_suspend(&dev)); assert(bcm4377_resume(&dev) == -EIO); assert(unquiesces == 1 && !b.resume_cold && b.resume_work.queues == 1); break; - /* A controller that was not powered gets no recovery: its next open resets the function. */ + /* + * A controller that was not powered gets no recovery and was never suspended: its + * next open resets the function, and the core has nothing to resume after it. + */ case 48: j616s(&hw, &b); h.flags = 0; + resume_frames = 2; assert(!cold_cycle(&dev)); assert(b.needs_reset && b.resume_cold); - assert(!unquiesces && !b.resume_work.queues); + assert(!unquiesces && !b.resume_work.queues && !b.hci_resume_owed); recover(&b); assert(!reset_count && send(&h, HCI_COMMAND_PKT) == -ENODEV); assert(!power_on(&h)); assert(reset_count == 1 && cal_count == 1 && addr_count == 1 && (h.flags & HCI_UP)); + assert(!b.hci_resume_work.queues); assert(!b.resume_cold && !send(&h, HCI_COMMAND_PKT) && enqueued == 1); break; - /* A command timeout inside the resume takes the recovery; the resume queues no second. */ + /* A command timeout in the core's resume after the reset starts no second recovery. */ case 49: j616s(&hw, &b); timeout_during_resume = true; assert(!cold_cycle(&dev)); assert(b.resume_work.queues == 1); - deliver_work(&b); - run_request(&h); - assert(reset_count == 1 && !b.resume_cold); + cold_recover(&b); + assert(reset_count == 1 && !b.resume_cold && !h.suspended); + assert(b.resume_work.queues == 1); recover(&b); assert(reset_count == 1); break; @@ -267,8 +301,9 @@ int main(int argc,char **argv) { assert(!bcm4377_resume(&dev)); assert(unquiesces == 1 && !misdirected && !b.needs_reset && !b.resume_cold); assert(!b.resume_work.queues && enqueued == 2 && !h.stat.err_tx); + assert(!h.suspended && !b.hci_resume_owed); recover(&b); - assert(reset_count == 1); + assert(reset_count == 1 && !b.hci_resume_work.queues); break; /* Windows the sleep left alone: nothing is written, resume and recovery as before. */ case 51: @@ -288,26 +323,27 @@ int main(int argc,char **argv) { fw_bootstage = 2; assert(!bcm4377_resume(&dev)); assert(b.resume_cold && !unquiesces); - deliver_work(&b); - run_request(&h); + cold_recover(&b); assert(!bcm4377_suspend(&dev)); power_loss(); fw_rti_status = 2; assert(!bcm4377_resume(&dev)); assert(b.resume_cold && !unquiesces); - deliver_work(&b); - run_request(&h); + cold_recover(&b); assert(reset_count == 2 && cal_count == 2 && addr_count == 2); assert(b.resume_work.queues == 2 && !b.resume_cold); + assert(b.hci_resume_work.queues == 2 && !h.suspended); break; /* The J493 pair takes the same path. */ case 53: + resume_frames = 2; assert(!cold_cycle(&dev)); assert(!unquiesces && b.resume_cold && b.resume_work.queues == 1); - deliver_work(&b); - run_request(&h); + assert(!h.stat.err_tx && !enqueued); + cold_recover(&b); assert(reset_count == 1 && cal_count == 1 && ptb_count == 1 && addr_count == 1); assert((h.flags & HCI_UP) && !b.resume_cold); + assert(enqueued == 2 && !h.suspended); break; /* Any other chip and board pair is not looked at: its resume is the one before. */ case 54: @@ -324,7 +360,7 @@ int main(int argc,char **argv) { assert(!cfg_reads && !cfg_writes && unquiesces == 2); assert(!b.needs_reset && !b.resume_cold && !send(&h, HCI_COMMAND_PKT)); recover(&b); - assert(!reset_count && !b.resume_work.queues); + assert(!reset_count && !b.resume_work.queues && !b.hci_resume_work.queues); break; /* Windows that cannot be written back: no status word is read, the function is reset. */ case 55: @@ -335,9 +371,8 @@ int main(int argc,char **argv) { assert(!bcm4377_resume(&dev)); cfg_error = 0; assert(!unquiesces && b.needs_reset && b.resume_cold && b.resume_work.queues == 1); - deliver_work(&b); - run_request(&h); - assert(reset_count == 1 && !b.resume_cold); + cold_recover(&b); + assert(reset_count == 1 && !b.resume_cold && !h.suspended); break; /* The next suspend fences the recovery a cold resume queued, as work and as request. */ case 56: @@ -350,14 +385,18 @@ int main(int argc,char **argv) { assert(request.fn); assert(!cold_cycle(&dev)); assert(!request.fn && b.resume_work.queues == 3 && quiesces == 3 && !unquiesces); - assert(!reset_count && b.resume_cold); - deliver_work(&b); - run_request(&h); + assert(!reset_count && b.resume_cold && b.hci_resume_owed && h.suspended); + cold_recover(&b); assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + assert(b.hci_resume_work.queues == 1 && !h.suspended && !b.hci_resume_owed); break; - /* A power-off before the queued recovery runs invalidates it; the next open resets. */ + /* + * A power-off before the queued recovery runs invalidates it; the next open resets, + * and the core resumes after that open. + */ case 57: j616s(&hw, &b); + resume_frames = 2; assert(!cold_cycle(&dev)); deliver_work(&b); assert(request.fn); @@ -365,8 +404,12 @@ int main(int argc,char **argv) { bcm4377_hci_close(&h); run_request(&h); assert(!reset_count && b.resume_cold && b.needs_reset); + assert(b.hci_resume_owed && h.suspended && !b.hci_resume_work.queues); assert(!power_on(&h)); assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + assert(b.hci_resume_work.queues == 1 && !enqueued); + deliver_hci_resume(&b); + assert(enqueued == 2 && !h.stat.err_tx && !h.suspended && !b.hci_resume_owed); break; /* The send callback: each frame reaches its ring, and none does while cold. */ case 58: @@ -384,21 +427,27 @@ int main(int argc,char **argv) { assert(enqueued == 3 && skbs_freed == 3 && h.stat.byte_tx == 12); assert(h.stat.err_tx == 3 && h.stat.cmd_tx == 1 && h.stat.acl_tx == 1); assert(h.stat.sco_tx == 1); - deliver_work(&b); - run_request(&h); + cold_recover(&b); assert(!send(&h, HCI_COMMAND_PKT) && enqueued == 4 && skbs_freed == 4); break; - /* A reset that fails leaves the function cold; the one that succeeds ends it. */ + /* + * A reset that fails leaves the function cold, the controller down and the core + * suspended; the open that succeeds ends all three. + */ case 59: j616s(&hw, &b); + resume_frames = 2; assert(!cold_cycle(&dev)); reset_error = -EIO; - deliver_work(&b); - run_request(&h); + cold_recover(&b); assert(reset_count == 1 && !(h.flags & HCI_UP) && b.needs_reset && b.resume_cold); + assert(b.hci_resume_owed && h.suspended && !b.hci_resume_work.queues); reset_error = 0; assert(!power_on(&h)); assert(reset_count == 2 && !b.resume_cold && (h.flags & HCI_UP)); + assert(b.hci_resume_work.queues == 1 && !enqueued && !h.stat.err_tx); + deliver_hci_resume(&b); + assert(enqueued == 2 && !h.stat.err_tx && !h.suspended && !b.hci_resume_owed); assert(!send(&h, HCI_COMMAND_PKT)); break; /* Armed for wake: no QUIESCE went out, and the cold resume is the same. */ @@ -408,9 +457,8 @@ int main(int argc,char **argv) { assert(!cold_cycle(&dev)); assert(!quiesces && !unquiesces && !b.wake_armed); assert(b.resume_cold && b.resume_work.queues == 1); - deliver_work(&b); - run_request(&h); - assert(reset_count == 1 && !b.resume_cold); + cold_recover(&b); + assert(reset_count == 1 && !b.resume_cold && !h.suspended); break; /* A sleep that leaves the windows alone does not end the cold state an earlier one left. */ case 61: @@ -420,9 +468,9 @@ int main(int argc,char **argv) { resume_cycle(&dev); assert(b.resume_cold && b.needs_reset && !unquiesces); assert(b.resume_work.queues == 2); - deliver_work(&b); - run_request(&h); + cold_recover(&b); assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + assert(!h.suspended && !b.hci_resume_owed); break; /* A setup retry the suspend held back is queued again by a cold resume too. */ case 62: @@ -433,14 +481,85 @@ int main(int argc,char **argv) { bcm4377_hci_close(&h); assert(b.setup_retry_pending && b.setup_retry_work.work.queued); assert(!cold_cycle(&dev)); - assert(b.resume_cold && !b.resume_work.queues); + assert(b.resume_cold && !b.resume_work.queues && !b.hci_resume_owed); assert(b.setup_retry_pending && b.setup_retry_work.work.queued); break; + /* + * A controller powered off between the reset and the core's resume keeps its turn: + * the core resumes after the next open, which needs no reset. + */ + case 63: + j616s(&hw, &b); + resume_frames = 2; + assert(!cold_cycle(&dev)); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && b.hci_resume_work.queued); + h.flags &= ~HCI_UP; + bcm4377_hci_close(&h); + deliver_hci_resume(&b); + assert(h.suspended && b.hci_resume_owed && !enqueued && !h.stat.err_tx); + assert(!power_on(&h)); + assert(reset_count == 1 && b.hci_resume_work.queues == 2); + deliver_hci_resume(&b); + assert(enqueued == 2 && !h.stat.err_tx && !h.suspended && !b.hci_resume_owed); + assert(!power_on(&h)); + assert(b.hci_resume_work.queues == 2); + break; + /* + * The next suspend fences a queued resume of the core, so it cannot run against the + * function the next sleep leaves cold: it follows that sleep's reset. + */ + case 64: + j616s(&hw, &b); + resume_frames = 2; + assert(!cold_cycle(&dev)); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && b.hci_resume_work.queued); + assert(!cold_cycle(&dev)); + assert(!b.hci_resume_work.queued && b.hci_resume_owed && h.suspended); + assert(!enqueued && !h.stat.err_tx); + cold_recover(&b); + assert(reset_count == 2 && enqueued == 2 && !h.stat.err_tx); + assert(!h.suspended && !b.hci_resume_owed); + break; + /* After the reset, a sleep that keeps the firmware resumes the core itself. */ + case 65: + j616s(&hw, &b); + resume_frames = 2; + assert(!cold_cycle(&dev)); + deliver_work(&b); + run_request(&h); + assert(reset_count == 1 && b.hci_resume_work.queued); + resume_cycle(&dev); + assert(unquiesces == 1 && !b.hci_resume_work.queued); + assert(enqueued == 2 && !h.stat.err_tx && !h.suspended && !b.hci_resume_owed); + break; + /* An open that ends behind the suspend fence queues nothing; the resume does it. */ + case 66: + j616s(&hw, &b); + resume_frames = 2; + assert(!cold_cycle(&dev)); + deliver_work(&b); + h.flags &= ~HCI_UP; + bcm4377_hci_close(&h); + run_request(&h); + assert(!bcm4377_suspend(&dev)); + assert(!power_on(&h)); + assert(reset_count == 1 && !b.resume_cold && (h.flags & HCI_UP)); + assert(b.hci_resume_owed && !b.hci_resume_work.queues && !enqueued); + assert(!bcm4377_resume(&dev)); + assert(unquiesces == 1 && enqueued == 2 && !h.stat.err_tx); + assert(!h.suspended && !b.hci_resume_owed && !b.hci_resume_work.queues); + break; default:abort(); } if (atoi(argv[1]) == 6 || atoi(argv[1]) == 44 || atoi(argv[1]) == 45) assert(rebind_count == 1); else assert(!rebind_count); + /* Every queued resume of the core was delivered or fenced. */ + assert(!b.hci_resume_work.queued); assert(!request.fn);printf("case %s passed\n",argv[1]);return 0; } diff --git a/tools/asahi/bcm4377-resume/fixture.h b/tools/asahi/bcm4377-resume/fixture.h index c0b3f095e9acc..b61f744965e60 100644 --- a/tools/asahi/bcm4377-resume/fixture.h +++ b/tools/asahi/bcm4377-resume/fixture.h @@ -58,6 +58,8 @@ struct hci_dev { struct work_struct power_on, cmd_sync_work, cmd_work; struct delayed_work cmd_timer, ncmd_timer; int req_lock, cmd_cnt, suspend_state, wake_reason, wake_addr_type; + /* The core's own mark: set by its suspend, cleared by its resume when powered. */ + bool suspended; bdaddr_t wake_addr; int *req_skb; void (*reset)(struct hci_dev *hdev); @@ -90,8 +92,8 @@ struct bcm4377_data { pthread_mutex_t resume_lock; uint64_t resume_epoch, resume_pending; bool resume_armed, resume_blocked, resume_powered; - bool resume_guarded, resume_recovering, resume_cold; - struct work_struct resume_work; + bool resume_guarded, resume_recovering, resume_cold, hci_resume_owed; + struct work_struct resume_work, hci_resume_work; struct ring acl_d2h_ring,acl_h2d_ring,sco_d2h_ring,sco_h2d_ring,hci_d2h_ring,hci_h2d_ring,sco_event_ring,sco_ack_ring,hci_acl_event_ring,hci_acl_ack_ring; }; static int cal_count,ptb_count,reset_count,open_count,queued_count,addr_count,cal_failures,ptb_error,addr_error,reset_error,rings_error,close_error; @@ -198,6 +200,7 @@ static void iowrite32(int val, void *addr) { if (val == BCM4377_BAR0_SLEEP_CONTROL_QUIESCE) { assert(!request.fn && !active->resume_work.queued && !active_running); + assert(!active->hci_resume_work.queued); quiesces++; return; } @@ -266,7 +269,15 @@ static void kfree_skb(struct sk_buff *skb) static int hci_suspend_dev(struct hci_dev *h); static int hci_resume_dev(struct hci_dev *h); -static void hci_unregister_dev(struct hci_dev *h) {assert(!request.fn&&!active->resume_work.queued&&!active_running);h->flags|=HCI_UNREGISTER;unregistered++;} + +static void hci_unregister_dev(struct hci_dev *h) +{ + assert(!request.fn && !active->resume_work.queued && !active_running); + assert(!active->hci_resume_work.queued); + h->flags |= HCI_UNREGISTER; + unregistered++; +} + static int hci_dev_close_sync(struct hci_dev *h); static int hci_dev_open_sync(struct hci_dev *h); int hci_reset_dev_sync(struct hci_dev *h); @@ -278,7 +289,16 @@ static int hci_skb_opcode(int *p) {return *p;} static bool mgmt_powering_down(struct hci_dev *h) {return h->flags&HCI_POWERING_DOWN;} static void hci_req_sync_lock(struct hci_dev *h) {assert(!req_locked);req_locked=true;} static void hci_req_sync_unlock(struct hci_dev *h) {assert(req_locked);req_locked=false;} -static int hci_suspend_sync(struct hci_dev *h) {return suspend_error;} + +/* The core marks the controller suspended unless its suspend fails. */ +static int hci_suspend_sync(struct hci_dev *h) +{ + if (suspend_error) + return suspend_error; + h->suspended = true; + return 0; +} + static int hci_resume_sync(struct hci_dev *h); static void hci_clear_wake_reason(struct hci_dev *h) {} static void mgmt_suspending(struct hci_dev *h,int state) {} From 75f367a39f98dac945f57c9a973e98e98bb6992b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:28:58 +0100 Subject: [PATCH 191/269] nvme-apple: configure the RTKit object of a restore as probe does MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Probe tells its RTKit object two things about the controller: the longer handshake timeout of the T6050 ANS and, for a controller the sleep controller stops, the last endpoint map reply of a warm restart. A resume from hibernation on a controller with separate I/O queue registers frees that object and creates one for the session the boot loader started, and the new object got neither. T6040 needs neither (its mask is the default and it is no T6050), so nothing changes there; on T8140 the next warm restart after a restore would have answered the endpoint map with the default, and on T6050 the adoption would have run with the short timeout. Put both into apple_nvme_rtkit_configure() and call it for every object, before it is used. Both calls only store a value in the object; no controller or coprocessor register access moves. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 8e3b76f7ff693..2a2c6508a4180 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -2437,6 +2437,24 @@ static void apple_nvme_flush_work(struct work_struct *work) } } +/* + * What a new RTKit object is told about this controller: at probe, and when a + * hibernation restore replaces the object with one for the session the boot + * loader started. + */ +static void apple_nvme_rtkit_configure(struct apple_nvme *anv) +{ + /* + * The T6050 ANS firmware handed over by iBoot can take about 3 s to + * answer the power-state and endpoint-map handshake. + */ + if (of_device_is_compatible(anv->dev->of_node, "apple,t6050-nvme-ans2")) + apple_rtkit_set_handshake_timeout(anv->rtk, + APPLE_NVME_T6050_HANDSHAKE_TIMEOUT_MS); + if (anv->hw->sleep_traced || anv->hw->sleep_untraced) + apple_rtkit_set_warm_epmap_mask(anv->rtk, anv->hw->sleep_epmap_last_mask); +} + static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -2636,13 +2654,7 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) goto out_detach_genpd; } - /* - * The T6050 ANS firmware handed over by iBoot can take about 3 s to - * answer the power-state and endpoint-map handshake. - */ - if (of_device_is_compatible(dev->of_node, "apple,t6050-nvme-ans2")) - apple_rtkit_set_handshake_timeout(anv->rtk, - APPLE_NVME_T6050_HANDSHAKE_TIMEOUT_MS); + apple_nvme_rtkit_configure(anv); ret = devm_add_action_or_reset(dev, apple_nvme_free_rtkit, anv); if (ret) @@ -2761,7 +2773,6 @@ static int apple_nvme_probe(struct platform_device *pdev) apple_sleep_coproc_unregister(anv->dev); anv->sleep_registered = false; } - apple_rtkit_set_warm_epmap_mask(anv->rtk, anv->hw->sleep_epmap_last_mask); } apple_nvme_set_reset_ready(anv, true); @@ -3192,6 +3203,7 @@ static int apple_nvme_restore_post_m4(struct apple_nvme *anv) return ret; } anv->rtk = rtk; + apple_nvme_rtkit_configure(anv); ret = apple_rtkit_adopt_running(rtk); if (ret) { dev_err(anv->dev, "hibernation restore: RTKit adoption failed: %d\n", ret); From 9e51c28de5d46e36a751590d565596210c2e2e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:28:58 +0100 Subject: [PATCH 192/269] nvme-apple: one flag for a controller the sleep controller stops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit T8140 registers its ANS with the sleep controller through sleep_traced. T6040 got a second flag for the same registration, sleep_untraced, with a comment that said the sequence was not measured there; the T6040 table says a few lines further down that the ANS came back from the suspends to RAM that cut the power. The two flags were tested together in the one place that reads them. Keep one flag and name it for what it does, sleep_coproc: the sleep controller takes this ANS through a firmware-driven suspend. T8132, T8140 and T6040 set it. The probe condition is the same for every table, so no register access changes. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 2a2c6508a4180..107b88eda2ab6 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -215,15 +215,13 @@ struct apple_nvme_hw { bool needs_ioq_registers; /* T8132 needs a PMGR reset when starting a stopped coprocessor. */ bool cold_reset; - /* Firmware-driven suspend has been traced on this controller. */ - bool sleep_traced; /* - * Not measured, but the sleep controller may take it the same way: its flush, - * quiesce and shutdown notification, then the polled idle at the commit. The - * values below come from this SoC's own sources, as each says. + * The sleep controller takes this ANS through a firmware-driven suspend: + * flush, quiesce and shutdown notification at the device suspend, the + * polled idle at the commit and a warm restart after the wake. */ - bool sleep_untraced; - /* The last endpoint map reply from that trace */ + bool sleep_coproc; + /* The last endpoint map reply of that warm restart; 0: the whole map again. */ u32 sleep_epmap_last_mask; /* Higher namespaces are the firmware's, and a Flush to one faults the controller. */ u32 sleep_flush_max_nsid; @@ -2451,7 +2449,7 @@ static void apple_nvme_rtkit_configure(struct apple_nvme *anv) if (of_device_is_compatible(anv->dev->of_node, "apple,t6050-nvme-ans2")) apple_rtkit_set_handshake_timeout(anv->rtk, APPLE_NVME_T6050_HANDSHAKE_TIMEOUT_MS); - if (anv->hw->sleep_traced || anv->hw->sleep_untraced) + if (anv->hw->sleep_coproc) apple_rtkit_set_warm_epmap_mask(anv->rtk, anv->hw->sleep_epmap_last_mask); } @@ -2763,7 +2761,7 @@ static int apple_nvme_probe(struct platform_device *pdev) INIT_DELAYED_WORK(&anv->flush_dwork, apple_nvme_flush_work); - if (anv->hw->sleep_traced || anv->hw->sleep_untraced) { + if (anv->hw->sleep_coproc) { anv->sleep_registered = !apple_sleep_coproc_register(anv->dev, anv->mmio_coproc, true, apple_nvme_sleep_last_stop, anv); @@ -3271,7 +3269,7 @@ static const struct apple_nvme_hw apple_nvme_t8132_hw = { .cold_reset = true, .max_queue_depth = 64, /* T8140's values: T8132's sleep path hasn't been traced. */ - .sleep_traced = true, + .sleep_coproc = true, .sleep_epmap_last_mask = BIT(0), .sleep_flush_max_nsid = 1, .sleep_shn = NVME_CC_SHN_MASK, @@ -3284,7 +3282,7 @@ static const struct apple_nvme_hw apple_nvme_t8140_hw = { .has_separate_nvmmu = true, .needs_ioq_registers = true, .max_queue_depth = 64, - .sleep_traced = true, + .sleep_coproc = true, .sleep_epmap_last_mask = BIT(0), .sleep_flush_max_nsid = 1, .sleep_shn = NVME_CC_SHN_MASK, @@ -3323,7 +3321,7 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { * Registered with the T6040 sleep controller. The flush, quiesce, CC.SHN * shutdown notification and the resume path (SART replay, warm RTKit * restart, controller reset + namespace re-attach) are all generic and - * already wired through this sleep_untraced state; the controller stops + * already wired through sleep_coproc; the controller stops * ANS at the commit and raises its two ST lanes on wake * (t6040_apcie_st_lower/raise). Whether a sleep cuts the power * is the controller's decision (apple_sleep_power_loss()); a dry run (2 @@ -3341,7 +3339,7 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { * endpoint map on the warm restart: ANS came back with it in the J616s * S2R tests that cut the power. */ - .sleep_untraced = true, + .sleep_coproc = true, .sleep_flush_max_nsid = 1, .sleep_shn = NVME_CC_SHN_MASK, .quirks = NVME_QUIRK_NO_APST, From 1d123f051755574031557f7e2186185fba333194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:28:59 +0100 Subject: [PATCH 193/269] nvme-apple: say what the T6040 ANS does around a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment of the T6040 table named functions of the sleep controller, its test modes and the runs the values were confirmed in. Describe the contract with the firmware instead: what the sleep controller does to this ANS and when, how the firmware takes the shutdown notification, which namespaces are its own, and what it expects of the warm restart. pm_hibernate_is_restored() is false without CONFIG_HIBERNATION, so the IS_ENABLED() test in front of it goes. No change to the object code. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 107b88eda2ab6..a186a9be6bce9 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -3222,8 +3222,7 @@ static int apple_nvme_restore(struct device *dev) int ret; /* Hibernation aborted before the image: the same ANS session runs on. */ - if (IS_ENABLED(CONFIG_HIBERNATION) && anv->hw->needs_ioq_registers && - pm_hibernate_is_restored()) { + if (anv->hw->needs_ioq_registers && pm_hibernate_is_restored()) { ret = apple_nvme_restore_post_m4(anv); if (ret) return ret; @@ -3318,26 +3317,21 @@ static const struct apple_nvme_hw apple_nvme_t6040_hw = { .lsq_in_nvmmu = true, .max_queue_depth = 64, /* - * Registered with the T6040 sleep controller. The flush, quiesce, CC.SHN - * shutdown notification and the resume path (SART replay, warm RTKit - * restart, controller reset + namespace re-attach) are all generic and - * already wired through sleep_coproc; the controller stops - * ANS at the commit and raises its two ST lanes on wake - * (t6040_apcie_st_lower/raise). Whether a sleep cuts the power - * is the controller's decision (apple_sleep_power_loss()); a dry run (2 - * or 3) flushes, quiesces and sends the shutdown notification at the - * device suspend, as a sleep does, and resets the controller as after - * any refusal. + * The sleep controller takes this ANS through a firmware-driven suspend + * as on T8140: flush, quiesce and the shutdown notification at the + * device suspend, the firmware idled at the commit, its storage link + * quiesced behind it and brought up again on wake, then the SART replay, + * the warm RTKit restart and a controller reset. A sleep taken back + * before the commit has flushed, quiesced and notified too, and resets + * the controller like any refused sleep. * - * CC.SHN: the platform 26.7.1 on this SoC, at power state 0, - * shuts NVMe down in its suspend variant: - * the shutdown notification writes CC |= 0xc000 and - * polls CSTS.SHST for complete. Flush: namespace 1 only, the OS's own; the - * others belong to the firmware, as on T8140. + * The firmware takes the shutdown notification with both CC.SHN bits set + * and answers with CSTS.SHST complete. It owns every namespace above 1, + * as on T8140, so only namespace 1 is flushed. * - * sleep_epmap_last_mask stays 0, RTKit's default of echoing the whole - * endpoint map on the warm restart: ANS came back with it in the J616s - * S2R tests that cut the power. + * After a sleep that cut the power it takes the whole endpoint map + * echoed as the last reply of the warm restart, RTKit's default: + * sleep_epmap_last_mask stays 0. */ .sleep_coproc = true, .sleep_flush_max_nsid = 1, From 5bccc0db62f3c719b5ad683947cac195fdcafc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:29:00 +0100 Subject: [PATCH 194/269] Bluetooth: hci_bcm4377: a table for the boards restarted in place MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The boards whose transport is restarted in place after a resume were one expression that grew by an OR with the second board, with the chip IDs repeated in it. Make it a table of chip and machine pairs that the admission function walks; the next board is a row. The probe of a function the sleep reset compared the boot stage and the RTI status with a bare 2. Name the two values a running firmware shows. No change in behaviour: the same two pairs are admitted and the same values are compared. tools/asahi: the identity test takes the table with the function and gets a row on the other row's board as mutants, and the resume test compiles the table ahead of the functions. Signed-off-by: František Balázsy --- drivers/bluetooth/hci_bcm4377.c | 29 ++++++++++++++++++++++++++--- tools/asahi/bcm4377-id-tests.py | 12 +++++++++--- tools/asahi/bcm4377-resume-tests.py | 15 +++++++++------ 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index b0b662daee3eb..127c3d9273871 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -85,6 +85,8 @@ enum bcm4377_chip { #define BCM4377_BAR0_HOST_WINDOW_SIZE 0x598 #define BCM4377_BAR2_BOOTSTAGE 0x200454 +/* The firmware has booted. */ +#define BCM4377_BOOTSTAGE_RUNNING 2 #define BCM4377_BAR2_FW_LO 0x200478 #define BCM4377_BAR2_FW_HI 0x20047c @@ -96,6 +98,8 @@ enum bcm4377_chip { #define BCM4388_BAR2_CONTEXT_ADDR_HI 0x200490 #define BCM4377_BAR2_RTI_STATUS 0x20045c +/* The firmware has taken the context and serves the rings. */ +#define BCM4377_RTI_STATUS_RUNNING 2 #define BCM4377_BAR2_RTI_WINDOW_LO 0x200494 #define BCM4377_BAR2_RTI_WINDOW_HI 0x200498 #define BCM4377_BAR2_RTI_WINDOW_SIZE 0x20049c @@ -1598,10 +1602,28 @@ static void bcm4377_setup_retry_work(struct work_struct *work) queue_work(hdev->req_workqueue, &hdev->power_on); } +/* + * Boards whose transport is restarted in place, by a close and an open that resets the + * function: when a command gets no answer after a resume, and when a sleep left the + * function without its firmware. + */ +static const struct bcm4377_resume_board { + unsigned int id; + const char *machine; +} bcm4377_resume_boards[] = { + { .id = 0x4378, .machine = "apple,j493" }, + { .id = 0x4388, .machine = "apple,j616s" }, + { } +}; + static bool bcm4377_resume_recovery_supported(struct bcm4377_data *bcm4377) { - return (bcm4377->hw->id == 0x4378 && of_machine_is_compatible("apple,j493")) || - (bcm4377->hw->id == 0x4388 && of_machine_is_compatible("apple,j616s")); + const struct bcm4377_resume_board *board; + + for (board = bcm4377_resume_boards; board->machine; board++) + if (bcm4377->hw->id == board->id && of_machine_is_compatible(board->machine)) + return true; + return false; } static bool bcm4377_resume_recovery_allowed(struct hci_dev *hdev) @@ -3261,7 +3283,8 @@ static bool bcm4377_resume_cold(struct bcm4377_data *bcm4377) dev_info(&bcm4377->pdev->dev, "the sleep reset the function (window %#x): boot stage %u, RTI status %u\n", win, bootstage, rti_status); - return bootstage != 2 || rti_status != 2; + return bootstage != BCM4377_BOOTSTAGE_RUNNING || + rti_status != BCM4377_RTI_STATUS_RUNNING; } static int bcm4377_resume(struct device *dev) diff --git a/tools/asahi/bcm4377-id-tests.py b/tools/asahi/bcm4377-id-tests.py index e211281d8e467..5e68a82b42890 100755 --- a/tools/asahi/bcm4377-id-tests.py +++ b/tools/asahi/bcm4377-id-tests.py @@ -103,6 +103,8 @@ def main(): } pieces = {name: take(current, pattern) for name, pattern in patterns.items()} old_pieces = {name: take(old, pattern) for name, pattern in patterns.items()} + # The admission function walks a table of chip and board pairs; the old one named its pair. + pieces['gate'] = take(current, r'^static const struct bcm4377_resume_board \{.*?^\};') + '\n\n' + pieces['gate'] # The old gate is compiled against the current tables. Later commits added # hardware fields and variant settings, so the struct and table may differ, # but the old gate must still see the same chip IDs, PCI table and probe code. @@ -130,12 +132,16 @@ def main(): assert pieces['gate'].count('0x4378') == 1 gates['decimal-id'] = pieces['gate'].replace('0x4378', '4378') gates['other-chip'] = pieces['gate'].replace('0x4378', '0x4387') - gates['board-bypass'] = pieces['gate'].replace('of_machine_is_compatible("apple,j493")', 'true') - # The J616s clause gets the same three, and the two clauses must not trade boards. + # No row may match on any board, nor on the other row's. + assert pieces['gate'].count('of_machine_is_compatible(board->machine)') == 1 + gates['board-bypass'] = pieces['gate'].replace('of_machine_is_compatible(board->machine)', 'true') + assert pieces['gate'].count('"apple,j493"') == 1 and pieces['gate'].count('"apple,j616s"') == 1 + gates['j493-row-on-j616s'] = pieces['gate'].replace('"apple,j493"', '"apple,j616s"') + # The J616s row gets the same three, and the two rows must not trade boards. assert pieces['gate'].count('0x4388') == 1 gates['j616s-decimal-id'] = pieces['gate'].replace('0x4388', '4388') gates['j616s-other-chip'] = pieces['gate'].replace('0x4388', '0x4387') - gates['j616s-board-bypass'] = pieces['gate'].replace('of_machine_is_compatible("apple,j616s")', 'true') + gates['j616s-row-on-j493'] = pieces['gate'].replace('"apple,j616s"', '"apple,j493"') gates['crossed-boards'] = pieces['gate'].replace('"apple,j493"', '@').replace('"apple,j616s"', '"apple,j493"').replace('@', '"apple,j616s"') results = {} def no_core(): diff --git a/tools/asahi/bcm4377-resume-tests.py b/tools/asahi/bcm4377-resume-tests.py index c7f1c6cd104a2..33053098c0414 100755 --- a/tools/asahi/bcm4377-resume-tests.py +++ b/tools/asahi/bcm4377-resume-tests.py @@ -13,23 +13,26 @@ def extract(s,n): return s[m.start():b]+'\n' s=(r/'drivers/bluetooth/hci_bcm4377.c').read_text();core=(r/'net/bluetooth/hci_sync.c').read_text();core_hci=(r/'net/bluetooth/hci_core.c').read_text() names=['bcm4377_setup_retry_allowed','bcm4377_queue_setup_retry','bcm4377_setup_retry_work','bcm4377_resume_recovery_supported','bcm4377_resume_recovery_allowed','bcm4377_resume_recovery_invalidate','bcm4377_resume_recovery_sync','bcm4377_resume_recovery_destroy','bcm4377_resume_recovery_work','bcm4377_hci_resume','bcm4377_hci_resume_work','bcm4377_queue_hci_resume','bcm4377_hci_timeout','bcm4377_resume_recovery_stop','bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_setup_once','bcm4377_hci_setup','bcm4377_hci_post_init','bcm4377_hci_send_frame','bcm4377_init_cfg','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume_cold','bcm4377_resume'] -bodies={n:extract(s,n) for n in names};shared=[] +# The board table goes ahead of the functions that walk it. +boards=re.search(r'^static const struct bcm4377_resume_board \{.*?^\};',s,re.M|re.S).group()+'\n' +bodies={'@boards':boards,**{n:extract(s,n) for n in names}};shared=[] if a.old_ref: old=subprocess.check_output(['git','show',a.old_ref+':drivers/bluetooth/hci_bcm4377.c'],cwd=r,text=True) - for n in ['bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume']:bodies[n]=extract(old,n) + for n in ['bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_unregister_dev','bcm4377_suspend','bcm4377_resume']:bodies[n]=extract(old,n) shared=[n for n in names if 'resume_recovery' in n or n=='bcm4377_hci_timeout'] mutations={ 'epoch':('bcm4377_resume_recovery_sync','epoch == bcm4377->resume_epoch && ','' ,33), 'close':('bcm4377_hci_close','bcm4377_resume_recovery_invalidate(bcm4377);','',18), 'producer-fence':('bcm4377_resume_recovery_stop','cancel_work_sync(&bcm4377->resume_work);','',21), 'active-fence':('bcm4377_resume_recovery_stop','flush_work(&bcm4377->hdev->cmd_sync_work);','',40), - 'board':('bcm4377_resume_recovery_supported','of_machine_is_compatible("apple,j493")','true',13), - 'board-j616s':('bcm4377_resume_recovery_supported','of_machine_is_compatible("apple,j616s")','true',16), + 'board':('bcm4377_resume_recovery_supported','of_machine_is_compatible(board->machine)','true',13), + # The BCM4388 row on the J493's board. + 'board-j616s':('@boards','"apple,j616s"','"apple,j493"',16), # A resume that finds the function reset by the sleep and without firmware. 'cold-gate':('bcm4377_resume','bcm4377_resume_recovery_supported(bcm4377) &&\n\t (bcm4377_resume_cold','(bcm4377_resume_cold',54), 'cold-windows':('bcm4377_resume_cold','if (bcm4377_init_cfg(bcm4377))\n\t\treturn true;','',46), - 'cold-running-fw':('bcm4377_resume_cold','return bootstage != 2 || rti_status != 2;','return true;',50), - 'cold-rti':('bcm4377_resume_cold','bootstage != 2 || rti_status != 2','bootstage != 2 && rti_status != 2',52), + 'cold-running-fw':('bcm4377_resume_cold','return bootstage != BCM4377_BOOTSTAGE_RUNNING ||\n\t rti_status != BCM4377_RTI_STATUS_RUNNING;','return true;',50), + 'cold-rti':('bcm4377_resume_cold','BCM4377_BOOTSTAGE_RUNNING ||','BCM4377_BOOTSTAGE_RUNNING &&',52), 'cold-unquiesce':('bcm4377_resume','\t\tbcm4377->quiesced = false;\n\t\tbcm4377->needs_reset = true;','\t\tbcm4377->needs_reset = true;',46), 'cold-reset':('bcm4377_resume','\t\tbcm4377->needs_reset = true;\n','',48), 'cold-send':('bcm4377_hci_send_frame','READ_ONCE(bcm4377->resume_cold)','false',58), From 19b11ddefe1cb2c38887c783e87726814061ad6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:29:01 +0100 Subject: [PATCH 195/269] Bluetooth: hci_bcm4377: keep the core's PM notifier off a cold function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The HCI core suspends and resumes every controller from a PM notifier of its own, before tasks are frozen and after they are thawed. The system sleep callbacks of this driver call hci_suspend_dev() and hci_resume_dev() too, so each ran twice per sleep, the second time without commands but with its management events sent again. Where a suspend to RAM takes the firmware, the notifier's resume races with the driver: the driver leaves the core suspended until the queued reset has booted the firmware, and the notifier resumes it as soon as tasks are thawed. The reset is queued on a freezable workqueue and normally has the request lock by then, so the notifier waits for it or finds the controller down and does nothing. If the notifier ever gets there first, it sends the core's resume commands to a function without firmware, which fails each of them at error level. Give the board table a property for such a board and set HCI_QUIRK_NO_SUSPEND_NOTIFIER for it, as the MT7932 driver does for the same reason: the driver's callbacks are then the only ones. The core is suspended from the device suspend callback, after the freeze, and resumed from the device resume callback or, after a cold wake, behind the reset. Userspace sees one suspend and one resume event per sleep. The BCM4378 on a J493 keeps the notifier. tools/asahi: the identity test holds the quirk to the BCM4388 on a J616s, with the property moved and removed as mutants. Signed-off-by: František Balázsy --- drivers/bluetooth/hci_bcm4377.c | 32 +++++++++++++++++++++++++---- tools/asahi/bcm4377-id-tests.py | 24 +++++++++++++++++++--- tools/asahi/bcm4377-resume-tests.py | 6 +++--- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index 127c3d9273871..a603efc4ef146 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -1610,20 +1610,38 @@ static void bcm4377_setup_retry_work(struct work_struct *work) static const struct bcm4377_resume_board { unsigned int id; const char *machine; + /* + * A suspend to RAM takes the firmware. The core then resumes after the reset, from + * this driver, which makes its system sleep callbacks the only ones that suspend + * and resume the core. + */ + bool cold_sleep; } bcm4377_resume_boards[] = { { .id = 0x4378, .machine = "apple,j493" }, - { .id = 0x4388, .machine = "apple,j616s" }, + { .id = 0x4388, .machine = "apple,j616s", .cold_sleep = true }, { } }; -static bool bcm4377_resume_recovery_supported(struct bcm4377_data *bcm4377) +static const struct bcm4377_resume_board *bcm4377_resume_board(struct bcm4377_data *bcm4377) { const struct bcm4377_resume_board *board; for (board = bcm4377_resume_boards; board->machine; board++) if (bcm4377->hw->id == board->id && of_machine_is_compatible(board->machine)) - return true; - return false; + return board; + return NULL; +} + +static bool bcm4377_resume_recovery_supported(struct bcm4377_data *bcm4377) +{ + return bcm4377_resume_board(bcm4377); +} + +static bool bcm4377_resume_cold_sleep(struct bcm4377_data *bcm4377) +{ + const struct bcm4377_resume_board *board = bcm4377_resume_board(bcm4377); + + return board && board->cold_sleep; } static bool bcm4377_resume_recovery_allowed(struct hci_dev *hdev) @@ -3154,6 +3172,12 @@ static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id) hdev->post_init = bcm4377_hci_post_init; if (bcm4377_resume_recovery_supported(bcm4377)) hdev->reset = bcm4377_hci_timeout; + /* + * The core's notifier would resume the controller as soon as tasks are thawed, when + * the function a sleep left cold is not reset yet. bcm4377_resume() does it instead. + */ + if (bcm4377_resume_cold_sleep(bcm4377)) + hci_set_quirk(hdev, HCI_QUIRK_NO_SUSPEND_NOTIFIER); if (bcm4377->hw->broken_mws_transport_config) hci_set_quirk(hdev, HCI_QUIRK_BROKEN_MWS_TRANSPORT_CONFIG); diff --git a/tools/asahi/bcm4377-id-tests.py b/tools/asahi/bcm4377-id-tests.py index 5e68a82b42890..d1abc3413c749 100755 --- a/tools/asahi/bcm4377-id-tests.py +++ b/tools/asahi/bcm4377-id-tests.py @@ -37,7 +37,9 @@ def take(source, pattern): static int bcm4388_send_calibration(struct bcm4377_data *b) { (void)b;return 0; } struct pci_device_id { unsigned int vendor,device,subvendor,subdevice,class,class_mask;unsigned long driver_data; }; struct bcm4377_data { const struct bcm4377_hw *hw; }; -struct hci_dev { void (*reset)(struct hci_dev *); }; +struct hci_dev { void (*reset)(struct hci_dev *); bool no_suspend_notifier; }; +#define HCI_QUIRK_NO_SUSPEND_NOTIFIER 1 +#define hci_set_quirk(hdev, nr) ((hdev)->no_suspend_notifier = (nr) == HCI_QUIRK_NO_SUSPEND_NOTIFIER) static void bcm4377_hci_timeout(struct hci_dev *hdev) { (void)hdev; } static const char *board; static bool of_machine_is_compatible(const char *compat) { assert(!strcmp(compat,"apple,j493")||!strcmp(compat,"apple,j616s"));return !strcmp(board,compat); } @@ -47,6 +49,7 @@ def take(source, pattern): static bool probe_registration(struct bcm4377_data *bcm4377,const struct pci_device_id *id,struct hci_dev *hdev) { @SELECTION@ @REGISTRATION@ +@NOTIFIER@ return hdev->reset==bcm4377_hci_timeout; } int main(void) { @@ -68,6 +71,8 @@ def take(source, pattern): assert(data.hw==&bcm4377_hw_variants[id->driver_data]); assert(bcm4377_resume_recovery_supported(&data)==expected); if(expected)assert(hdev.reset==bcm4377_hci_timeout);else assert(!hdev.reset); + /* Only the pair whose suspend to RAM takes the firmware suspends and resumes HCI alone. */ + assert(hdev.no_suspend_notifier==(id->device==BCM4388_DEVICE_ID && !strcmp(board,"apple,j616s"))); cases++; } } @@ -101,10 +106,15 @@ def main(): 'registration': r'^\tif \(bcm4377_resume_recovery_supported\(bcm4377\)\)\n\t\thdev->reset = bcm4377_hci_timeout;', 'gate': r'^static bool bcm4377_resume_recovery_supported\([^;]*?\)\n\{.*?^\}', } + # What the current probe adds for a pair whose sleep takes the firmware; the old one has none. + notifier = take(current, r'^\tif \(bcm4377_resume_cold_sleep\(bcm4377\)\)\n\t\thci_set_quirk\(hdev, HCI_QUIRK_NO_SUSPEND_NOTIFIER\);') pieces = {name: take(current, pattern) for name, pattern in patterns.items()} old_pieces = {name: take(old, pattern) for name, pattern in patterns.items()} # The admission function walks a table of chip and board pairs; the old one named its pair. - pieces['gate'] = take(current, r'^static const struct bcm4377_resume_board \{.*?^\};') + '\n\n' + pieces['gate'] + pieces['gate'] = '\n\n'.join([take(current, r'^static const struct bcm4377_resume_board \{.*?^\};'), + take(current, r'^static const struct bcm4377_resume_board \*bcm4377_resume_board\([^;]*?\)\n\{.*?^\}'), + pieces['gate'], + take(current, r'^static bool bcm4377_resume_cold_sleep\([^;]*?\)\n\{.*?^\}')]) # The old gate is compiled against the current tables. Later commits added # hardware fields and variant settings, so the struct and table may differ, # but the old gate must still see the same chip IDs, PCI table and probe code. @@ -128,6 +138,9 @@ def main(): assert 'BAR2_CONTEXT_ADDR_HI' in hw_defs, hw_defs source_prefix = PREFIX + defs + '\n' + hw_defs + '\n' + pci_defs + '\n' + any_id + '\n' + '\n'.join(pieces[n] for n in ('enum', 'hw_struct', 'variants', 'pci_macro_table')) suffix = SUFFIX.replace('@SELECTION@', pieces['selection']).replace('@REGISTRATION@', pieces['registration']) + # The published original has no such pair: its probe is compiled without the statement. + old_suffix = suffix.replace('@NOTIFIER@', '') + suffix = suffix.replace('@NOTIFIER@', notifier) gates = {'positive': pieces['gate'], 'published-original': old_pieces['gate']} assert pieces['gate'].count('0x4378') == 1 gates['decimal-id'] = pieces['gate'].replace('0x4378', '4378') @@ -143,12 +156,17 @@ def main(): gates['j616s-other-chip'] = pieces['gate'].replace('0x4388', '0x4387') gates['j616s-row-on-j493'] = pieces['gate'].replace('"apple,j616s"', '"apple,j493"') gates['crossed-boards'] = pieces['gate'].replace('"apple,j493"', '@').replace('"apple,j616s"', '"apple,j493"').replace('@', '"apple,j616s"') + # The row whose sleep takes the firmware is the J616s one, and only that one. + assert pieces['gate'].count(', .cold_sleep = true') == 1 + gates['cold-sleep-nowhere'] = pieces['gate'].replace(', .cold_sleep = true', '') + gates['cold-sleep-on-j493'] = pieces['gate'].replace(', .cold_sleep = true', '').replace('"apple,j493" }', '"apple,j493", .cold_sleep = true }') + assert gates['cold-sleep-on-j493'].count('.cold_sleep = true') == 1 results = {} def no_core(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) for name, gate in gates.items(): source = args.out / f'{name}.c' - source.write_text(source_prefix + gate + suffix) + source.write_text(source_prefix + gate + (old_suffix if name == 'published-original' else suffix)) binary = source.with_suffix('') subprocess.run([args.cc, '-std=gnu11', '-g', '-O1', '-fsanitize=address,undefined', '-fno-omit-frame-pointer', str(source), '-o', str(binary)], check=True) run = subprocess.run([str(binary)], capture_output=True, text=True, preexec_fn=no_core) diff --git a/tools/asahi/bcm4377-resume-tests.py b/tools/asahi/bcm4377-resume-tests.py index 33053098c0414..ee7ab21c04644 100755 --- a/tools/asahi/bcm4377-resume-tests.py +++ b/tools/asahi/bcm4377-resume-tests.py @@ -6,13 +6,13 @@ p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--old-ref');p.add_argument('--mutation',choices=['epoch','close','producer-fence','active-fence','board','board-j616s','cold-gate','cold-windows','cold-running-fw','cold-rti','cold-unquiesce','cold-reset','cold-send','cold-queue','cold-unarmed','cold-rearm','cold-error','cold-sticky','cold-setup-retry','cold-defer','cold-open-queue','cold-owed-kept','cold-resume-fence','cold-queue-fence']);a=p.parse_args() r=Path(__file__).resolve().parents[2];o=Path(a.out);o.mkdir(parents=True,exist_ok=False);(o/'tmp').mkdir() def extract(s,n): - m=re.search(r'^(?:static )?(?:int|void|bool) '+n+r'\([^;]*?\)\n\{',s,re.M|re.S) + m=re.search(r'^(?:static )?(?:(?:int|void|bool) |const struct \w+ \*)'+n+r'\([^;]*?\)\n\{',s,re.M|re.S) if not m:raise ValueError(n) b=s.index('{',m.start())+1;depth=1 while depth:depth+=(s[b]=='{')-(s[b]=='}');b+=1 return s[m.start():b]+'\n' s=(r/'drivers/bluetooth/hci_bcm4377.c').read_text();core=(r/'net/bluetooth/hci_sync.c').read_text();core_hci=(r/'net/bluetooth/hci_core.c').read_text() -names=['bcm4377_setup_retry_allowed','bcm4377_queue_setup_retry','bcm4377_setup_retry_work','bcm4377_resume_recovery_supported','bcm4377_resume_recovery_allowed','bcm4377_resume_recovery_invalidate','bcm4377_resume_recovery_sync','bcm4377_resume_recovery_destroy','bcm4377_resume_recovery_work','bcm4377_hci_resume','bcm4377_hci_resume_work','bcm4377_queue_hci_resume','bcm4377_hci_timeout','bcm4377_resume_recovery_stop','bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_setup_once','bcm4377_hci_setup','bcm4377_hci_post_init','bcm4377_hci_send_frame','bcm4377_init_cfg','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume_cold','bcm4377_resume'] +names=['bcm4377_setup_retry_allowed','bcm4377_queue_setup_retry','bcm4377_setup_retry_work','bcm4377_resume_board','bcm4377_resume_recovery_supported','bcm4377_resume_recovery_allowed','bcm4377_resume_recovery_invalidate','bcm4377_resume_recovery_sync','bcm4377_resume_recovery_destroy','bcm4377_resume_recovery_work','bcm4377_hci_resume','bcm4377_hci_resume_work','bcm4377_queue_hci_resume','bcm4377_hci_timeout','bcm4377_resume_recovery_stop','bcm4377_hci_open','bcm4377_hci_close','bcm4377_hci_setup_once','bcm4377_hci_setup','bcm4377_hci_post_init','bcm4377_hci_send_frame','bcm4377_init_cfg','bcm4377_hci_unregister_dev','bcm4377_sleep_finish','bcm4377_suspend','bcm4377_resume_cold','bcm4377_resume'] # The board table goes ahead of the functions that walk it. boards=re.search(r'^static const struct bcm4377_resume_board \{.*?^\};',s,re.M|re.S).group()+'\n' bodies={'@boards':boards,**{n:extract(s,n) for n in names}};shared=[] @@ -25,7 +25,7 @@ def extract(s,n): 'close':('bcm4377_hci_close','bcm4377_resume_recovery_invalidate(bcm4377);','',18), 'producer-fence':('bcm4377_resume_recovery_stop','cancel_work_sync(&bcm4377->resume_work);','',21), 'active-fence':('bcm4377_resume_recovery_stop','flush_work(&bcm4377->hdev->cmd_sync_work);','',40), - 'board':('bcm4377_resume_recovery_supported','of_machine_is_compatible(board->machine)','true',13), + 'board':('bcm4377_resume_board','of_machine_is_compatible(board->machine)','true',13), # The BCM4388 row on the J493's board. 'board-j616s':('@boards','"apple,j616s"','"apple,j493"',16), # A resume that finds the function reset by the sleep and without firmware. From 0c9fefec47016d8ea9d0f942ed925cae9e4b7b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:29:02 +0100 Subject: [PATCH 196/269] Bluetooth: hci_bcm4377: report a function the sleep reset at debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a board whose suspend to RAM takes the firmware, every wake printed the window and the two status words it found. That is the expected state there, and the restart that follows says why it runs; keep the values for debugging. The comment names the chip the values were read on instead of the machine. Signed-off-by: František Balázsy --- drivers/bluetooth/hci_bcm4377.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index a603efc4ef146..7c545e355df5d 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -3289,9 +3289,9 @@ static int bcm4377_suspend(struct device *dev) /* * A system sleep that takes the chip's power or resets it leaves the function's private - * windows at their reset values and no firmware: a J616s after a suspend to RAM reads - * window 1 as 0x19328000 and, once the windows are back, boot stage 0. Put the windows - * back and say whether a firmware still runs behind them. + * windows at their reset values and no firmware: a BCM4388 then reads window 1 as + * 0x19328000 and, once the windows are back, boot stage 0. Put the windows back and say + * whether a firmware still runs behind them. */ static bool bcm4377_resume_cold(struct bcm4377_data *bcm4377) { @@ -3304,9 +3304,9 @@ static bool bcm4377_resume_cold(struct bcm4377_data *bcm4377) return true; bootstage = ioread32(bcm4377->bar2 + bcm4377->hw->bar2_offset + BCM4377_BAR2_BOOTSTAGE); rti_status = ioread32(bcm4377->bar2 + bcm4377->hw->bar2_offset + BCM4377_BAR2_RTI_STATUS); - dev_info(&bcm4377->pdev->dev, - "the sleep reset the function (window %#x): boot stage %u, RTI status %u\n", - win, bootstage, rti_status); + dev_dbg(&bcm4377->pdev->dev, + "the sleep reset the function (window %#x): boot stage %u, RTI status %u\n", + win, bootstage, rti_status); return bootstage != BCM4377_BOOTSTAGE_RUNNING || rti_status != BCM4377_RTI_STATUS_RUNNING; } From 15ef9b4f0a74388feaf1579c30d3331eb867f5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:01:56 +0100 Subject: [PATCH 197/269] iommu/apple-dart: decide at probe which DART gets its tunables again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A T6040 or T6041 PCIe-C DART that lost its state lost its probe tunables with it, and the resume applies them again before the reset. It found those DARTs by testing the compatible on every resume. Like the other per-SoC properties of a DART, record it in a flag at probe, where the tunables are applied the first time. No functional change: the flag is set exactly where the three conditions of the resume held. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 2e17363d032a0..15b6a97fa1efc 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -414,6 +414,8 @@ struct apple_dart { u32 restore_on_power_up : 1; /* Powered down by its DCP across a real sleep: see apple_dart_host_power_on(). */ u32 host_off_slept : 1; + /* A PCIe-C DART that loses its probe tunables with its state (T6040, T6041). */ + u32 reapply_tunables : 1; bool commands_gated; /* Resume restored the translations, but the last steps of its reset failed. */ bool reset_incomplete; @@ -5359,6 +5361,8 @@ static int apple_dart_probe(struct platform_device *pdev) dev_dbg(dev, "applying %zu PCIe-C DART tunables\n", dart->tunables->sz); apple_dart_apply_tunables(dart, dart->tunables); + dart->reapply_tunables = of_device_is_compatible(dev->of_node, + "apple,t6041-dart"); } if (tunneled) { @@ -6508,8 +6512,7 @@ static __maybe_unused int apple_dart_resume(struct device *dev) * A T6040/T6041 PCIe-C DART that lost its state lost its tunables with it: * apply them again before the reset, in the order of probe. */ - if (dart->tunneled && dart->tunables && - of_device_is_compatible(dev->of_node, "apple,t6041-dart")) + if (dart->reapply_tunables) apple_dart_apply_tunables(dart, dart->tunables); ret = apple_dart_hw_reset(dart); From 1fad76024a52f986d98bd59071e917826dccffc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 03:41:44 +0100 Subject: [PATCH 198/269] PCI: apple: let the ports share the waits of a wake from S2R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a suspend-to-RAM the ports of the T6040 controller come back one after the other in the noirq resume, and each takes two waits of 100 ms: after the release of PERST# and after its link has trained. With two ports (Wi-Fi/Bluetooth and the card reader on a J616s) that is 414 ms of the 423 ms the whole noirq phase takes. Bring every port up to the release of PERST# first, wait once, then restore and train each, and wait once more. Each port still gets at least the same two waits; the phase loses about 200 ms. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 59 +++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 9bb97876b8788..11ae93cc9fee3 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -6642,10 +6642,13 @@ static void apple_pcie_s2r_restore_rc(struct apple_pcie *pcie, struct apple_pcie * root port's settings back, PERST# released, the link trained. The endpoint's power line * is not touched: it is the SMC's, whose session is back only in the normal phase, and it * reads on after the wake. + * + * In two steps, so that the ports share the two waits instead of each taking its own: up to + * the release of PERST#, and from the port's ready bit on. */ -static int apple_pcie_s2r_port_up_noirq(struct apple_pcie *pcie, struct apple_pcie_port *port) +static int apple_pcie_s2r_port_release_noirq(struct apple_pcie *pcie, + struct apple_pcie_port *port) { - u32 stat; int ret; rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); @@ -6661,8 +6664,14 @@ static int apple_pcie_s2r_port_up_noirq(struct apple_pcie *pcie, struct apple_pc usleep_range(100, 200); rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); gpiod_set_value(port->perst_gpio, 0); - /* 100 ms after PERST# (PCIe r5.0, 6.6.1) */ - msleep(100); + return 0; +} + +static int apple_pcie_s2r_port_train_noirq(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + u32 stat; + int ret; + ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, 100, 250000); if (ret) @@ -6676,31 +6685,59 @@ static int apple_pcie_s2r_port_up_noirq(struct apple_pcie *pcie, struct apple_pc return ret; apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); - msleep(100); return 0; } +static void apple_pcie_s2r_port_failed(struct apple_pcie *pcie, struct apple_pcie_port *port, + int ret) +{ + dev_warn(pcie->dev, "S2R: %pOF link not back in the noirq phase (%d, LTSSM %#x)\n", + port->np, ret, apple_pcie_port_ltssm(pcie, port)); +} + /* A port that does not come up here is left to the normal phase, as before. */ static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) { struct apple_pcie_port *port; + unsigned long released = 0, trained = 0; int ret; list_for_each_entry(port, &pcie->ports, entry) { if (!port->hib_link_up || !port->s2r_saved || !port->perst_gpio || - gpiod_cansleep(port->perst_gpio) || + gpiod_cansleep(port->perst_gpio) || port->idx >= BITS_PER_LONG || (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP)) continue; - ret = apple_pcie_s2r_port_up_noirq(pcie, port); + ret = apple_pcie_s2r_port_release_noirq(pcie, port); if (ret) - dev_warn(pcie->dev, "S2R: %pOF link not back in the noirq phase (%d, LTSSM %#x)\n", - port->np, ret, apple_pcie_port_ltssm(pcie, port)); + apple_pcie_s2r_port_failed(pcie, port, ret); else - dev_info(pcie->dev, "S2R: %pOF link back with the loader's port settings\n", - port->np); + released |= BIT(port->idx); + } + if (!released) + return; + /* 100 ms after PERST# (PCIe r5.0, 6.6.1), once for all of them. */ + msleep(100); + list_for_each_entry(port, &pcie->ports, entry) { + if (port->idx >= BITS_PER_LONG || !(released & BIT(port->idx))) + continue; + ret = apple_pcie_s2r_port_train_noirq(pcie, port); + if (ret) + apple_pcie_s2r_port_failed(pcie, port, ret); + else + trained |= BIT(port->idx); + } + if (!trained) + return; + msleep(100); + list_for_each_entry(port, &pcie->ports, entry) { + if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) + continue; + dev_info(pcie->dev, "S2R: %pOF link back with the loader's port settings\n", + port->np); } } + /* What only the port registers hold, for a restore that finds them reset. */ static void apple_pcie_hib_save(struct apple_pcie *pcie) { From dc0b939650acd2d1f3c9f7ab36d0b031f55f3c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 03:41:54 +0100 Subject: [PATCH 199/269] PCI: apple: wait after the training only for a link above 5 GT/s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The second 100 ms of a port's wake from suspend-to-RAM follows the link training. PCIe r5.0, 6.6.1 asks for it on a link that runs above 5 GT/s; up to 5 GT/s the 100 ms since PERST# are the whole requirement, and the ports have waited those. Both ports of a J616s have a target speed of 5 GT/s. Take the wait only when a trained port's target speed is above 5 GT/s. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 11ae93cc9fee3..744ab60ad43a8 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -6700,6 +6700,7 @@ static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) { struct apple_pcie_port *port; unsigned long released = 0, trained = 0; + bool fast = false; int ret; list_for_each_entry(port, &pcie->ports, entry) { @@ -6726,9 +6727,23 @@ static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) else trained |= BIT(port->idx); } - if (!trained) - return; - msleep(100); + /* + * A link above 5 GT/s takes no config request before 100 ms after its training + * (PCIe r5.0, 6.6.1). Up to 5 GT/s the time since PERST# is what counts, and that + * has passed. + */ + list_for_each_entry(port, &pcie->ports, entry) { + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + + if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) + continue; + /* The target, not the current speed: the change of speed may still run. */ + if ((readw_relaxed(dbi + NEO_DBI_CAP_LNKCTL2) & PCI_EXP_LNKCTL2_TLS) > + PCI_EXP_LNKCTL2_TLS_5_0GT) + fast = true; + } + if (fast) + msleep(100); list_for_each_entry(port, &pcie->ports, entry) { if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) continue; From b634b7b813e47071cf8b221ad70f12f1cb0b2e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 07:00:25 +0100 Subject: [PATCH 200/269] wifi: brcmfmac: pcie: restart a function back from a power loss behind the resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A function that lost its power in a firmware suspend is found cold at the resume (interrupt mask 0), and the resume callback removes and probes it. On a J616s that takes 619 ms of the callback, about 550 ms of it for taking the interfaces and the wiphy down. Every device that follows in the resume order waits for it, and without USB devices it is what the whole resume phase ends with. Queue the remove and probe instead and return, on a freezable workqueue, so that the work starts once the resume of every device is over: run during it, the removal of the wiphy only moved the wait to the wiphy's own resume (522 ms there). The work takes the device lock, which an unbind holds too, and does nothing for a function that has no driver data any more. A PM notifier flushes the queue before the next suspend or hibernation, so that one never meets a function in the middle of its restart. User space sees what it saw before, the wiphy going away and a new one coming, a little later: after it is thawed. Hibernation and a suspend to idle keep the restart in the callback. J616s, suspend-to-RAM with the function associated, no USB hub attached: the callback returns after 24 us, the resume of the devices takes 453 ms, against 542 with the restart running during it, and the interface associates again. With the restart in the callback the callback alone took 619 ms. The host test of the PM callbacks gets the two new calls and a case for the queued restart and for the one that cannot be queued. Signed-off-by: František Balázsy --- .../broadcom/brcm80211/brcmfmac/pcie.c | 106 +++++++++++++++++- tools/asahi/brcmf-suspend-tests.py | 9 ++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 892b787924847..55454bb617547 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -10,11 +10,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -3479,6 +3481,69 @@ static int brcmf_pcie_pm_enter_D3(struct device *dev) } +static struct pci_driver brcmf_pciedrvr; +static struct workqueue_struct *brcmf_pcie_restart_wq; + +struct brcmf_pcie_restart { + struct work_struct work; + struct pci_dev *pdev; +}; + +static void brcmf_pcie_restart_work(struct work_struct *work) +{ + struct brcmf_pcie_restart *restart = + container_of(work, struct brcmf_pcie_restart, work); + struct pci_dev *pdev = restart->pdev; + int err; + + /* The resume callback that queued this still holds the lock; so does an unbind. */ + device_lock(&pdev->dev); + if (pdev->dev.driver == &brcmf_pciedrvr.driver && dev_get_drvdata(&pdev->dev)) { + brcmf_pcie_remove(pdev); + err = brcmf_pcie_probe(pdev, NULL); + if (err) + __brcmf_err(NULL, __func__, "probe after resume failed, err=%d\n", + err); + } + device_unlock(&pdev->dev); + pci_dev_put(pdev); + kfree(restart); +} + +/* Whether the remove and probe of a function back from a power loss were queued. */ +static bool brcmf_pcie_restart_queue(struct pci_dev *pdev) +{ + struct brcmf_pcie_restart *restart; + + if (!brcmf_pcie_restart_wq) + return false; + restart = kzalloc(sizeof(*restart), GFP_KERNEL); + if (!restart) + return false; + INIT_WORK(&restart->work, brcmf_pcie_restart_work); + restart->pdev = pci_dev_get(pdev); + queue_work(brcmf_pcie_restart_wq, &restart->work); + return true; +} + +/* The next sleep finds the function probed again, not in the middle of it. */ +static int brcmf_pcie_restart_pm(struct notifier_block *nb, unsigned long action, void *data) +{ + switch (action) { + case PM_SUSPEND_PREPARE: + case PM_HIBERNATION_PREPARE: + case PM_RESTORE_PREPARE: + if (brcmf_pcie_restart_wq) + flush_workqueue(brcmf_pcie_restart_wq); + break; + } + return NOTIFY_DONE; +} + +static struct notifier_block brcmf_pcie_restart_nb = { + .notifier_call = brcmf_pcie_restart_pm, +}; + static int brcmf_pcie_pm_leave_D3(struct device *dev) { struct brcmf_pciedev_info *devinfo; @@ -3549,6 +3614,13 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) brcmf_chip_detach(devinfo->ci); devinfo->ci = NULL; pdev = devinfo->pdev; + /* + * A function that lost its power in a firmware suspend starts over. Taking the + * interfaces and the wiphy down takes about 550 ms, and everything that follows + * this device in the resume waits for it, so do it right behind the resume. + */ + if (pm_resume_via_firmware() && brcmf_pcie_restart_queue(pdev)) + return 0; brcmf_pcie_remove(pdev); err = brcmf_pcie_probe(pdev, NULL); @@ -3639,13 +3711,45 @@ static struct pci_driver brcmf_pciedrvr = { int brcmf_pcie_register(void) { + int ret; + brcmf_dbg(PCIE, "Enter\n"); - return pci_register_driver(&brcmf_pciedrvr); +#ifdef CONFIG_PM + /* + * Without it the restart stays in the resume callback. Freezable: the work then + * starts once the resume of every device is over. Run during it, the removal of + * the wiphy only moves the wait to the wiphy's own resume. + */ + brcmf_pcie_restart_wq = alloc_ordered_workqueue("brcmf_pcie_restart", WQ_FREEZABLE); + if (brcmf_pcie_restart_wq) + register_pm_notifier(&brcmf_pcie_restart_nb); +#endif + ret = pci_register_driver(&brcmf_pciedrvr); +#ifdef CONFIG_PM + if (ret && brcmf_pcie_restart_wq) { + unregister_pm_notifier(&brcmf_pcie_restart_nb); + destroy_workqueue(brcmf_pcie_restart_wq); + brcmf_pcie_restart_wq = NULL; + } +#endif + return ret; } void brcmf_pcie_exit(void) { brcmf_dbg(PCIE, "Enter\n"); +#ifdef CONFIG_PM + if (brcmf_pcie_restart_wq) { + unregister_pm_notifier(&brcmf_pcie_restart_nb); + flush_workqueue(brcmf_pcie_restart_wq); + } +#endif pci_unregister_driver(&brcmf_pciedrvr); +#ifdef CONFIG_PM + if (brcmf_pcie_restart_wq) { + destroy_workqueue(brcmf_pcie_restart_wq); + brcmf_pcie_restart_wq = NULL; + } +#endif } diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index 41957747d20ca..efef34ea7de04 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -180,6 +180,9 @@ def function(source, name): static void brcmf_pcie_sleep_finish(struct device *d) {(void)d;abort();} static void brcmf_bus_cancel_reset_work(struct brcmf_bus *b) {(void)b;abort();} static int brcmf_pcie_pm_leave_D3(struct device *dev); +static bool via_firmware,queue_fails;static unsigned int queued; +static bool pm_resume_via_firmware(void) {return via_firmware;} +static bool brcmf_pcie_restart_queue(struct pci_dev *p) {(void)p;if(queue_fails)return false;queued++;return true;} ''' CALLBACK_STUBS = r''' @@ -359,6 +362,12 @@ def function(source, name): probe_error=fail?-EIO:0;assert(brcmf_pcie_pm_leave_D3(&device)==probe_error); assert(removes==1 && probes==1 && ndev[0].stopped && msgbuf.tx_suspended);cases++; } + /* After a firmware suspend the cold function's restart is queued, or done here if it can't be. */ + for(int nomem=0;nomem<2;nomem++) { + reset();assert(!brcmf_pcie_pm_enter_D3(&device));retain=false; + via_firmware=true;queue_fails=nomem;queued=0;assert(!brcmf_pcie_pm_leave_D3(&device)); + assert(queued==!nomem && removes==nomem && probes==nomem);via_firmware=false;queue_fails=false;cases++; + } printf("%u regulatory, TX queue, worker retention and PCIe PM controls passed\n",cases); return 0; } From 80cfcb36f0659ec870c6c81ae652679763e873e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:37:29 +0100 Subject: [PATCH 201/269] PCI: apple: drop the unreachable T6040 MSI slice branch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The MSI slice table is reached for the t8122, t8132 and t6040 PCIe-C controllers. The T6040 bridges are described only by "apple,t6040-pciec", which selects t6040_pciec_hw, and that case is handled first. The later of_machine_is_compatible("apple,t6040") arm would need a T6040 machine whose controller node binds to the t8122 or t8132 data instead, which no devicetree has. It repeated the vector base and ECAM table of the first arm. Remove it. The T6040 slice layout is unchanged. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 744ab60ad43a8..294d4d49e2b08 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -3422,21 +3422,6 @@ static int apple_msi_init(struct apple_pcie *pcie) default: goto invalid_slice; } - } else if (of_machine_is_compatible("apple,t6040")) { - aic_base = 2071; - switch (ecam.start) { - case 0x1c50000000ULL: - expected_base = 0x20; - break; - case 0x1c60000000ULL: - expected_base = 0x40; - break; - case 0x1c70000000ULL: - expected_base = 0x60; - break; - default: - goto invalid_slice; - } } else if (of_machine_is_compatible("apple,t8132")) { aic_base = 1506; switch (ecam.start) { From f1eaa5bedd9fa6f25819b6194add7b6063b25cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:38:18 +0100 Subject: [PATCH 202/269] PCI: apple: fail the probe on a malformed port tunable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The S2R port and root-complex tunables are optional, so a missing property (-ENOENT) means nothing to restore after a sleep. Any other parse error (-EINVAL for a malformed property, -ENOMEM) was dropped the same way, and the port then came back from a sleep without the calibration the firmware had published. Only treat -ENOENT as "no tunable"; return the other errors from the port setup so the probe fails with the property named. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 294d4d49e2b08..fd1f65cfe88f0 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -2976,10 +2976,11 @@ static void apple_pcie_port_gate_clocks(struct apple_pcie *pcie, /* * The tunables the loader applied to a conventional port and its root port at boot, where it - * publishes them: written again after a sleep that reset the block. Optional. + * publishes them: written again after a sleep that reset the block. A missing property is + * fine, a malformed one fails the probe. */ -static void apple_pcie_port_s2r_tunables(struct apple_pcie *pcie, struct apple_pcie_port *port, - struct resource *port_res) +static int apple_pcie_port_s2r_tunables(struct apple_pcie *pcie, struct apple_pcie_port *port, + struct resource *port_res) { static const char * const rc_names[] = { "apple,tunable-rc", "apple,tunable-rc-gen3", "apple,tunable-rc-gen4", @@ -2989,11 +2990,19 @@ static void apple_pcie_port_s2r_tunables(struct apple_pcie *pcie, struct apple_p int i; t = devm_apple_tunable_parse(pcie->dev, port->np, "apple,tunable-port", port_res); + if (IS_ERR(t) && PTR_ERR(t) != -ENOENT) + return dev_err_probe(pcie->dev, PTR_ERR(t), "port %pOF: apple,tunable-port\n", + port->np); port->s2r_port_tunable = IS_ERR(t) ? NULL : t; for (i = 0; i < ARRAY_SIZE(rc_names); i++) { t = devm_apple_tunable_parse(pcie->dev, port->np, rc_names[i], &rc); + if (IS_ERR(t) && PTR_ERR(t) != -ENOENT) + return dev_err_probe(pcie->dev, PTR_ERR(t), "port %pOF: %s\n", + port->np, rc_names[i]); port->s2r_rc_tunable[i] = IS_ERR(t) ? NULL : t; } + + return 0; } static int apple_pcie_setup_port(struct apple_pcie *pcie, @@ -3059,8 +3068,11 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, if (ret) goto err_teardown; } - if (!pcie->hw->tunneled && !pcie->hw->adopt_link && !pcie->managed_sids) - apple_pcie_port_s2r_tunables(pcie, port, res); + if (!pcie->hw->tunneled && !pcie->hw->adopt_link && !pcie->managed_sids) { + ret = apple_pcie_port_s2r_tunables(pcie, port, res); + if (ret) + goto err_teardown; + } if (pcie->hw->tunneled) { port->tunable = devm_apple_tunable_parse(pcie->dev, np, "apple,tunable", res); From 774f371ac9883437012c2c8f06cb6e2f3ae72d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:46:49 +0100 Subject: [PATCH 203/269] PCI, nvme: apple: word the comments as hardware contracts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several comments in the PCIe host and the NVMe driver explain a register, a bring-up order or a timeout by what another operating system does. State what the block needs or does instead: the bits and their order, the wait for RUN, the shutdown notification timeout, the time ANS firmware takes to report on. The shutdown notification timeout macro loses the name of the other system. Comment and macro name only; the object code of both files is unchanged. Signed-off-by: František Balázsy --- drivers/nvme/host/apple.c | 28 +++++++------- drivers/pci/controller/pcie-apple.c | 57 ++++++++++++++--------------- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index a186a9be6bce9..4012d039a3939 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -225,7 +225,7 @@ struct apple_nvme_hw { u32 sleep_epmap_last_mask; /* Higher namespaces are the firmware's, and a Flush to one faults the controller. */ u32 sleep_flush_max_nsid; - /* CC.SHN for a sleep, as read back from the OS the sleep was traced on. */ + /* CC.SHN the controller wants for a sleep. */ u32 sleep_shn; /* The ANS is never reset through PMGR here, so no reset is named. */ bool reset_optional; @@ -2545,7 +2545,7 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) /* * Newer ANS firmware moves the linear submission queue registers into * the NVMMU block and ends the "nvme" block before them (T6040, and - * T8142 booting the macOS 27.0 stub: 0x10000, ADT ans reg[9]). A + * T8142 with the 27.0 firmware stub: window of 0x10000). A * window that does not reach the doorbells means that layout. */ anv->lsq_in_nvmmu = anv->hw->lsq_in_nvmmu || @@ -2858,20 +2858,20 @@ static void apple_nvme_shutdown(struct platform_device *pdev) } } -/* macOS sees the firmware report on 68 ms after HELLO. Each try waits RTKit's timeout again. */ +/* The firmware reports on about 68 ms after HELLO. Each try waits RTKit's timeout again. */ #define APPLE_NVME_SLEEP_ON_TRIES 15 /* * After a real sleep ANS has lost power, and its resident firmware restarts once the run bit - * is set. Measured on macOS under aura, the same in five runs: HELLO first and unprompted, + * is set. Measured under aura, the same in five runs: HELLO first and unprompted, * then the endpoint map, the last part answered with endpoint 0x20 only. No endpoint is * started and no buffer asked for. About 68 ms later the firmware reports on, and the AP state * follows, with the controller's registers reading 0. A refused suspend never told the * firmware anything, and takes a reset. * - * The run bit goes in the early phase, so HELLO comes within milliseconds of the wake as on - * macOS. The wait for on doesn't: macOS rings the SEP a few ms after ANS's HELLO, before ANS - * reports on, so the reset work waits, after the SEP's own resume has had its turn. + * The run bit goes in the early phase, so HELLO comes within milliseconds of the wake. The + * wait for on doesn't: the SEP is rung a few ms after ANS's HELLO, before ANS reports on, + * so the reset work waits, after the SEP's own resume has had its turn. */ static int apple_nvme_resume_early(struct device *dev) { @@ -2887,7 +2887,7 @@ static int apple_nvme_resume_early(struct device *dev) apple_sleep_coproc_set_ready(dev, false); /* Its link doesn't come up with the pins as the sleep left them. */ apple_sleep_restore_pins(); - /* macOS writes the SART's entries back again right before the run bit. */ + /* The SART's entries go back in again right before the run bit. */ if (anv->sart) apple_sart_replay(anv->sart); @@ -3024,12 +3024,12 @@ static bool apple_nvme_sleep_wanted(struct apple_nvme *anv) } /* - * As macOS sends it, from its power-state change during the device suspend: the shutdown - * notification, waited for with interrupts on for as long as macOS allows (35 s). The - * controller can take seconds to save its context (1.7 s in one macOS sleep). A sleep - * refused later finds it shut down, and resume's controller reset takes it back. + * Sent from the device suspend: the shutdown notification, waited for with interrupts on + * for up to 35 s. The controller can take seconds to save its context (1.7 s in one + * sleep). A sleep refused later finds it shut down, and resume's controller reset takes it + * back. */ -#define APPLE_ANS_SLEEP_SHN_MACOS_TIMEOUT_US (35 * USEC_PER_SEC) +#define APPLE_ANS_SLEEP_SHN_TIMEOUT_US (35 * USEC_PER_SEC) static int apple_nvme_sleep_send_shn(struct apple_nvme *anv) { @@ -3043,7 +3043,7 @@ static int apple_nvme_sleep_send_shn(struct apple_nvme *anv) writel(anv->ctrl.ctrl_config, anv->mmio_nvme + NVME_REG_CC); ret = readl_poll_timeout(anv->mmio_nvme + NVME_REG_CSTS, csts, (csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT, - 1000, APPLE_ANS_SLEEP_SHN_MACOS_TIMEOUT_US); + 1000, APPLE_ANS_SLEEP_SHN_TIMEOUT_US); us = ktime_us_delta(ktime_get(), start); if (ret) dev_err(anv->dev, "shutdown notification failed after %lld ms: CSTS %#x\n", diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index fd1f65cfe88f0..a8f097c3399c8 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -103,16 +103,16 @@ MODULE_PARM_DESC(link_up_timeout, "PCIe link training timeout in milliseconds"); #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)) -/* Bit 31 of the pair: the only one a trace of macOS shows cleared, after PERST# is asserted */ +/* Bit 31 of the pair: the only one that is cleared once PERST# is asserted */ #define PHY_LANE_CFG_REFCLKCGEN_B31 BIT(31) #define PHY_LANE_CTL 0x00004 #define PHY_LANE_CTL_CFGACC BIT(15) #define PORT_LTSSMCTL 0x00080 #define PORT_LTSSMCTL_START BIT(0) -/* T6050: a trace of macOS shows 0x11 written here to send PME_Turn_Off */ +/* T6050: writing 0x11 here sends PME_Turn_Off */ #define PORT_PME_TURN_OFF 0x00088 -/* Reads back 0 within a few reads of the write (the trace reads 0x1, then 0x10) */ +/* Reads back 0 within a few reads of the write (first 0x1, then 0x10) */ #define PORT_PME_TURN_OFF_SEND BIT(0) /* Written with bit 0 and still set afterwards; meaning not known */ #define PORT_PME_TURN_OFF_BIT4 BIT(4) @@ -144,7 +144,7 @@ MODULE_PARM_DESC(link_up_timeout, "PCIe link training timeout in milliseconds"); #define PORT_LINKSTS 0x00208 #define PORT_LINKSTS_UP BIT(0) #define PORT_LINKSTS_BUSY BIT(2) -/* In traces of macOS: 0x9b00134d for a port that reached L2, 0x9b001001 for one that did not */ +/* Status word: 0x9b00134d for a port that reached L2, 0x9b001001 for one that did not */ #define PORT_LINKSTS_L2 BIT(6) #define PORT_LINKCMDSTS 0x00210 #define PORT_OUTS_NPREQS 0x00284 @@ -412,14 +412,13 @@ static const struct hw_info t8132_pciec_hw = { }; /* - * T6040/T6041 (M4 Pro/Max) PCIe-C is the T8132 block: the platform drives it as - * the T8132 one and only sets its own + * T6040/T6041 (M4 Pro/Max) PCIe-C is the T8132 block with only its own * register indices, so the port layout, RID2SID count and tunnel reset - * registers are those of T8132. Two steps follow the platform rather than the T8132 - * Linux sequence: the port must report RUN before the root-port config + * registers are those of T8132. Two steps differ from the T8132 Linux + * sequence: the port must report RUN before the root-port config * tunables are written, and the tunneled reset acknowledgment is the - * ACK_PEND bit being set. Like the platform, which disables the port and its - * clocks before sleep and runs the full enable sequence on wake, + * ACK_PEND bit being set. The block needs its port and its clocks + * disabled before sleep and the full enable sequence on wake, so * hibernation stops the port and cold-starts it again. */ static const struct hw_info t6040_pciec_hw = { @@ -1370,7 +1369,7 @@ static int apple_pcie_tunnel_cold_init(struct apple_pcie_port *port) /* * Without an m1n1 handoff, T600x/T602x ports must report RUN before * the root complex config space is written: writing the RC tunables - * first hard-resets a J416c. The platform waits for RUN there on T6040 too. + * first hard-resets a J416c. T6040 needs the wait for RUN there too. */ if (!pcie->kernel_init || pcie->hw->tunnel_ready_before_rc) { ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, @@ -1748,7 +1747,7 @@ static int apple_pcie_tunnel_stop(struct apple_pcie_port *port) apple_pcie_port_rmw_set(port, PORT_TUNCTRL_PERST_ACK_REQ, apple_pcie_port_tunctrl(port)); /* * M1 reports an outstanding acknowledgment until ACK_PEND clears. - * T6040 reports the acknowledgment by setting it, as the platform waits. + * T6040 reports the acknowledgment by setting it. */ ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, pcie->kernel_init && !pcie->hw->tunnel_ack_on_set ? @@ -2400,29 +2399,27 @@ static int apple_pcie_t6050_port_train(struct apple_pcie_port *port) #define T6050_PHY_CTRL_LANES GENMASK(9, 8) #define T6050_PHY_CFG 0x4 /* - * Set before the PHY-IP tunables. A trace of macOS bringing the port up shows no - * access to this bit; meaning not known. + * Set before the PHY-IP tunables; meaning not known. */ #define T6050_PHY_CFG_BIT0 BIT(0) /* - * Set after the PHY-IP tunables. A trace of macOS shows it set too, after which - * T6050_PHY_STAT_BIT0 rises (the trace reads 0x10 seven times, then 0x11); meaning - * not known. + * Set after the PHY-IP tunables, after which T6050_PHY_STAT_BIT0 rises (the status + * reads 0x10 several times, then 0x11); meaning not known. */ #define T6050_PHY_CFG_BIT4 BIT(4) #define T6050_PHY_STAT 0x8 -/* Rises once T6050_PHY_CFG_BIT4 is set; polled by macOS and by m1n1; meaning not known */ +/* Rises once T6050_PHY_CFG_BIT4 is set; m1n1 polls it too; meaning not known */ #define T6050_PHY_STAT_BIT0 BIT(0) /* - * Set and held once the shared PHY is started in macOS's post-iBoot order (aura's + * Set and held once the shared PHY is started after iBoot (aura's * T6050 bring-up sets T6050_PHY_CFG_BIT4 and waits for it); meaning not known */ #define T6050_PHY_STAT_BIT4 BIT(4) #define T6050_PHYCMN_CLK 0x0 #define T6050_PHYCMN_CLK_MODE GENMASK(1, 0) /* - * Written before CORE_RC_CTL_RUN: a trace of macOS shows 0x140 written here, and m1n1 - * writes the same. Neither bit's meaning is known. + * Written before CORE_RC_CTL_RUN with 0x140, as m1n1 does. Neither bit's meaning is + * known. */ #define CORE_RC_T6050_PRE_RUN 0x54 #define CORE_RC_T6050_PRE_RUN_BIT6 BIT(6) @@ -2509,7 +2506,7 @@ static int apple_pcie_t6050_shared_phy_on(struct apple_pcie *pcie) T6050_PHY_CTRL_RESET_T8103)) == up) return 0; /* - * A boot loader that started the PHY in macOS's order (it trained another port) + * A boot loader that started the PHY already (it trained another port) * leaves status bit 4 set and the RC ready: the PHY runs, so it is not restarted * under that port's link. */ @@ -2649,7 +2646,7 @@ static int apple_pcie_t6050_cold_train(struct apple_pcie_port *port) /* * PME_Turn_Off, then wait for L2: the clock gate disabled, the message sent, and the * link reported in L2. Both waits are made even when the first fails (the N1 may - * clear the send bit and never reach L2, as under macOS); the first error is returned. + * clear the send bit and never reach L2); the first error is returned. */ static int apple_pcie_t6050_port_l2(struct apple_pcie_port *port) { @@ -4608,7 +4605,7 @@ static int apple_pcie_neo_phy_clocks(void __iomem *phy) } /* - * The part of the block that ANS shares, from power-off in macOS's order, measured on + * The part of the block that ANS shares, from power-off in the order it needs, measured on * one sleep. The sleep driver does it at the wake when ANS is bound, and it can't be * redone under a running PHY. */ @@ -4689,7 +4686,7 @@ static int apple_pcie_neo_cold_init(struct apple_pcie *pcie, struct apple_pcie_p apple_tunable_apply(port->base, cold->port_tunable); rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); - /* PERST# off before the PHY's clocks, as macOS has it coming from power-off. */ + /* PERST# off before the PHY's clocks, the order the block needs coming from power-off. */ rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, 100, NEO_RC_POLL_US); @@ -6059,7 +6056,7 @@ static int apple_pcie_neo_link_up(struct apple_pcie *pcie, struct apple_pcie_por if (ret) return ret; released = ktime_get(); - /* macOS starts the training right after PERST#. */ + /* The training starts right after PERST#. */ ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, 100, 250000); if (ret) @@ -6236,8 +6233,8 @@ static void apple_pcie_neo_radio_work(struct work_struct *work) } /* - * The rail is the one thing that brings back a chip whose link won't train: macOS's last resort - * for a dead chip too. A cold chip then has its firmware loaded by the probe that follows. + * The rail is the one thing that brings back a chip whose link won't train, a dead chip + * too. A cold chip then has its firmware loaded by the probe that follows. */ static int apple_pcie_neo_rail_cycle(struct apple_pcie *pcie, struct apple_pcie_port *port, struct pci_dev *root) @@ -6359,8 +6356,8 @@ static void apple_pcie_neo_recover_work(struct work_struct *work) } /* - * Before the PCI core restores the functions, which need the link. macOS's host bridge - * holds them back the same way until its radio port is enabled. + * Before the PCI core restores the functions, which need the link. They are held back + * until the radio port is enabled. */ static int apple_pcie_neo_resume_noirq(struct apple_pcie *pcie) { From e8660e1ce2a020c06e6e639636aa10d287ccfa71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:47:49 +0100 Subject: [PATCH 204/269] PCI: apple: name the restore helpers after what they do MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helpers that save the port state for a sleep and bring a conventional host back after one are named after hibernation, although a real suspend-to-RAM uses them the same way. Call them restore_*, and define the S2R and restore helpers before their users so that the forward declarations go. No functional change: every function is identical apart from its symbol name. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 1664 +++++++++++++-------------- 1 file changed, 828 insertions(+), 836 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index a8f097c3399c8..3b0d65ab229a7 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -572,7 +572,7 @@ struct apple_pcie { /* Down already when the sleep began: a failed wake left it unprogrammed */ bool neo_rc_was_down; /* Functions' saved state, kept from a restore's noirq phase to its link */ - struct list_head hib_fns; + struct list_head restore_fns; const struct hw_info *hw; unsigned long *bitmap; struct list_head ports; @@ -623,13 +623,13 @@ struct apple_pcie_port { u32 s2r_dbi[5]; bool s2r_saved; /* Conventional port: link up when the image was taken */ - bool hib_link_up; + bool restore_link_up; /* ... and found down by the restore: the boot that resumed did not set it up */ - bool hib_cold; + bool restore_cold; }; /* A function's configuration, for its link back after a resume from disk. */ -struct apple_pcie_hib_fn { +struct apple_pcie_restore_fn { struct list_head entry; struct pci_dev *pdev; struct pci_saved_state *state; @@ -5075,7 +5075,7 @@ static int apple_pcie_probe(struct platform_device *pdev) mutex_init(&pcie->lock); INIT_LIST_HEAD(&pcie->ports); - INIT_LIST_HEAD(&pcie->hib_fns); + INIT_LIST_HEAD(&pcie->restore_fns); /* Resolve PM dependencies before publishing the PCI hierarchy. */ if (pcie->hw->tunneled) { @@ -5873,1005 +5873,979 @@ static void apple_pcie_stop_for_sleep(struct device *dev) } static void apple_pcie_neo_save_functions(struct apple_pcie *pcie); -static bool apple_pcie_hib_conventional(struct apple_pcie *pcie); -static void apple_pcie_hib_save(struct apple_pcie *pcie); -static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what); -static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie); /* - * The J716c radio drivers save PCI state without entering D3. The PCI core - * keeps the root port in D0 as well. Its host and DART have no switchable - * power domains, and the DART saves its live translations without stopping - * them. For s2idle, retain that running hierarchy instead of resetting it. - * - * Check hardware PMCSR rather than current_state: the PCI core may mark a - * driverless root port PCI_UNKNOWN even though its hardware remains in D0. + * A conventional host (the internal ports with their radio and SD reader) keeps running + * through freeze and thaw. Only a kernel restored from an image finds it changed: the boot + * that resumed it went through the boot loader, and when its kernel did not set the ports up + * (a boot kernel without the GPIO drivers that probing the host takes), the endpoints sit + * unpowered or in reset behind a link that is down. The restore then does what probe did. + * A real suspend-to-RAM takes the host's power as a resume from disk does, so its ports + * come back the same way. */ -static int apple_pcie_check_d0(struct pci_dev *pdev, void *data) +static bool apple_pcie_conventional_host(struct apple_pcie *pcie) { - int *error = data; - u16 value; - - if (pci_read_config_word(pdev, PCI_VENDOR_ID, &value) || - value != pdev->vendor) { - *error = -ENODEV; - } else if (pdev->pm_cap && - (pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &value) || - (value & PCI_PM_CTRL_STATE_MASK) != PCI_D0)) { - *error = -EBUSY; - } - if (*error) - pci_err(pdev, "adopted link requires accessible D0 devices for s2idle\n"); - return *error; + return !pcie->hw->tunneled && !pcie->managed_sids && !pcie->piodma_supplier && + !pcie->hw->root_bus_only; } -static int apple_pcie_check_adopted_s2idle(struct apple_pcie *pcie) -{ - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct apple_pcie_port *port; - int error = 0; - - /* Other adopted platforms and deeper sleep remain unqualified. */ - if (!(of_machine_is_compatible("apple,j716c") || - of_machine_is_compatible("apple,k114c")) || !pm_suspend_no_platform()) - return -EBUSY; +/* + * Root port words the loader limits at boot (one lane at the endpoint's speed) and a reset + * puts back to the core's own: link capabilities, the target speed, link mode and lane count. + */ +static const u16 apple_pcie_s2r_dbi[] = { + NEO_DBI_CAP_LNKCAP, NEO_DBI_CAP_LNKCAP2, NEO_DBI_CAP_LNKCTL2, + NEO_DBI_PORT_LINK_CONTROL, NEO_DBI_WIDTH_SPEED_CONTROL, +}; - if (!(readl(pcie->base + CORE_RC_CTL) & CORE_RC_CTL_RUN) || - !(readl(pcie->base + CORE_RC_STAT) & CORE_RC_STAT_READY)) - return -ENOLINK; +/* Bounded like probe's wait, polled: the link interrupts stay masked meanwhile. */ +static int apple_pcie_restore_train(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + int attempt, attempts = port->perst ? 1 + LINK_RESET_RETRIES : 1; + u32 stat; + int ret = -ETIMEDOUT; - list_for_each_entry(port, &pcie->ports, entry) { - if (READ_ONCE(port->link_failed) || - !(apple_pcie_port_readl(port, PORT_STATUS) & PORT_STATUS_READY) || - (apple_pcie_port_readl(port, PORT_LINKSTS) & - (PORT_LINKSTS_UP | PORT_LINKSTS_BUSY)) != PORT_LINKSTS_UP || - (apple_pcie_port_readl(port, PORT_INTSTAT) & BIT(PORT_INT_LINK_DOWN))) - return -ENOLINK; + for (attempt = 0; attempt < attempts; attempt++) { + if (attempt) { + dev_info(pcie->dev, "%pOF link down after %dms (LTSSM %#x), resetting the endpoint\n", + port->np, link_up_timeout, apple_pcie_port_ltssm(pcie, port)); + ret = apple_pcie_port_reset_endpoint(pcie, port); + if (ret) + return ret; + } + apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); + ret = readl_relaxed_poll_timeout(port->base + PORT_LINKSTS, stat, + stat & PORT_LINKSTS_UP, 100, + link_up_timeout * 1000); + if (!ret) + break; } - pci_walk_bus(bridge->bus, apple_pcie_check_d0, &error); - return error; + return ret; } -static int apple_pcie_suspend_noirq(struct device *dev) +/* This kernel's RID-to-SID table, every other entry cleared as probe clears them. */ +static void apple_pcie_restore_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port) { - struct apple_pcie *pcie = apple_pcie_lookup(dev); + int i; - if (pcie->cio_prepared) - return apple_pcie_cio_suspend(pcie); - /* The reversed DART link is safe only with retained clocks or native PM. */ - if (pcie->cio_dart && (!pm_suspend_no_platform() || !s2idle_keep_link)) - return -EBUSY; - if (pcie->neo_sleep_armed) { - struct apple_pcie_port *port; + mutex_lock(&pcie->lock); + for (i = 0; i < port->sid_map_sz; i++) + apple_pcie_rid2sid_write(port, i, test_bit(i, port->sid_map) ? + port->saved_rid2sid[i] : 0); + mutex_unlock(&pcie->lock); +} - pcie->neo_rc_was_down = READ_ONCE(pcie->neo_rc_down); - /* A port that didn't come back from the last sleep keeps what it saved then. */ - list_for_each_entry(port, &pcie->ports, entry) { - int i; +static void apple_pcie_s2r_save_port(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + int i; - apple_pcie_port_disable_irq(port); - if (pcie->neo_rc_was_down) - continue; - port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); - for_each_set_bit(i, port->sid_map, port->sid_map_sz) - port->saved_rid2sid[i] = - apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); - } - /* The functions too: the core read them through closed config space. */ - if (!pcie->neo_rc_was_down) - apple_pcie_neo_save_functions(pcie); - WRITE_ONCE(pcie->neo_rc_down, true); - /* - * Genpd would gate the host here, and an abandoned sleep then finds - * it unprogrammed with nothing to bring it up. The commit lowers it. - */ - device_set_awake_path(dev); - return 0; - } + BUILD_BUG_ON(ARRAY_SIZE(apple_pcie_s2r_dbi) != ARRAY_SIZE(port->s2r_dbi)); + /* The loader's port setup writes 0x130; the port tunable has its own word. */ + port->s2r_port_130 = apple_pcie_port_readl(port, 0x130); + for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) + port->s2r_dbi[i] = readl_relaxed(dbi + apple_pcie_s2r_dbi[i]); + port->s2r_saved = true; +} - /* - * The managed J713 radio has a native port/DART recovery transaction. - * Suspend-to-idle leaves its link in place (no power domain). - */ - if (pcie->managed_sids && pm_suspend_target_state != PM_SUSPEND_TO_IDLE) - return apple_radio_suspend_noirq(pcie); - if (!pcie->managed_sids && pcie->hw->adopt_link) { - int ret = apple_pcie_check_adopted_s2idle(pcie); +/* The root port part goes in once the port runs again, before the link starts. */ +static void apple_pcie_s2r_restore_rc(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + int i; - pcie->link_kept = !ret; - return ret; - } - if (!pcie->hw->tunneled) { - if (pm_suspend_no_platform() && pcie->num_pds) - return apple_pcie_hold_domains(pcie); - if (apple_pcie_hib_conventional(pcie) && pm_suspend_via_firmware()) - apple_pcie_hib_save(pcie); - return 0; - } - pcie->reset_on_resume = false; - pcie->link_kept = apple_pcie_keep_link(pcie); - if (!pcie->link_kept) { - if (pcie->cio_dart) - return -EBUSY; - apple_pcie_stop_for_sleep(dev); + rmw_set(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); + for (i = 0; i < ARRAY_SIZE(port->s2r_rc_tunable); i++) + apple_tunable_apply(dbi, port->s2r_rc_tunable[i]); + for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) { + /* Link Control 2 shares its word with a status register. */ + if (apple_pcie_s2r_dbi[i] == NEO_DBI_CAP_LNKCTL2) + writew_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); + else + writel_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); } - return 0; + /* The link goes to the target speed once it is up, as after the loader's setup. */ + rmw_set(NEO_DBI_SPEED_CHANGE, dbi + NEO_DBI_WIDTH_SPEED_CONTROL); + rmw_clear(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); } -static int apple_pcie_reset_for_resume(struct apple_pcie *pcie) +/* + * A real S2R took the port block's power: the sleep's wake side clocked the port again and + * the pin restore released PERST#, with the link down and every word of the port and of its + * root port at its reset value. Do what the loader and probe did between them, here, so that + * the PCI core's noirq pass finds the functions: PERST# asserted again, the port's and the + * root port's settings back, PERST# released, the link trained. The endpoint's power line + * is not touched: it is the SMC's, whose session is back only in the normal phase, and it + * reads on after the wake. + * + * In two steps, so that the ports share the two waits instead of each taking its own: up to + * the release of PERST#, and from the port's ready bit on. + */ +static int apple_pcie_s2r_port_release_noirq(struct apple_pcie *pcie, + struct apple_pcie_port *port) { - struct apple_pcie_port *port; - int i, ret; - - /* - * Keep the hierarchy bound and suspended. Reset only after its I/O - * drained, then restore the port and IOMMU before releasing tunneled - * reset. Endpoints must never run with missing address translations. - */ - ret = reset_control_reset(pcie->reset); - if (ret) - return ret; - - list_for_each_entry(port, &pcie->ports, entry) { - u32 stat; + int ret; - /* A failed cold setup still owns partially enabled hardware. */ - port->needs_stop = true; - apple_pcie_port_writel(port, PORT_TUNCTRL_PERST_ON, apple_pcie_port_tunctrl(port)); - ret = apple_pcie_tunnel_cold_init(port); - if (ret) - return ret; - ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, - stat & PORT_TUNSTAT_PERST_ON, - 1000, 100000, false, port, - apple_pcie_port_tunstat(port)); - if (ret) + rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); + gpiod_set_value(port->perst_gpio, 1); + if (!pcie->hw->retain_clocks) { + ret = apple_pcie_setup_refclk(pcie, port); + if (ret < 0) return ret; - apple_pcie_tunnel_restore_irq_hw(port); - for_each_set_bit(i, port->sid_map, port->sid_map_sz) - apple_pcie_rid2sid_write(port, i, port->saved_rid2sid[i]); } - ret = apple_pcie_walk_tunnel_darts(pcie, apple_dart_restore_tunnel_state, true); - if (ret) - return ret; - - dev_info(pcie->dev, "PCIe-C reset and DART state restored before link resume\n"); + apple_pcie_port_writel(port, port->s2r_port_130, 0x130); + apple_tunable_apply(port->base, port->s2r_port_tunable); + /* Tperst-clk (PCIe CEM r5.0, 2.9.2) */ + usleep_range(100, 200); + rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); + gpiod_set_value(port->perst_gpio, 0); return 0; } -/* - * The radio kept its power and firmware, in reset since the commit. Its rail isn't touched: - * cycling it, or the port's own reset, is what a cold start needs. PERST# released, the link - * trains. Device interrupts are off in this phase, so this polls for it. - */ -static int apple_pcie_neo_link_up(struct apple_pcie *pcie, struct apple_pcie_port *port) +static int apple_pcie_s2r_port_train_noirq(struct apple_pcie *pcie, struct apple_pcie_port *port) { - ktime_t released; - s64 left_us; u32 stat; int ret; - usleep_range(1000, 2000); - ret = gpiod_set_value_cansleep(port->perst_gpio, 0); + ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, + stat & PORT_STATUS_READY, 100, 250000); if (ret) return ret; - released = ktime_get(); - /* The training starts right after PERST#. */ - ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, - 100, 250000); - if (ret) - return ret; - apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); - ret = readl_relaxed_poll_timeout(port->base + PORT_LINKSTS, stat, stat & PORT_LINKSTS_UP, - 100, link_up_timeout * 1000); + apple_pcie_s2r_restore_rc(pcie, port); + apple_pcie_port_gate_clocks(pcie, port); + apple_pcie_tunnel_restore_irq_hw(port); + apple_pcie_restore_rid2sid(pcie, port); + ret = apple_pcie_restore_train(pcie, port); if (ret) return ret; apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); - /* No config request before 100 ms after PERST#, on a 5 GT/s port (PCIe r5.0, 6.6.1). */ - left_us = 100 * USEC_PER_MSEC - ktime_us_delta(ktime_get(), released); - if (left_us > 0) - fsleep(left_us); - + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); return 0; } -/* - * The PCI core saves the functions again at every sleep, and with the radio down it reads them - * through closed config space. Keep the last state they saved while they answered. - */ -static void apple_pcie_neo_save_functions(struct apple_pcie *pcie) +static void apple_pcie_s2r_port_failed(struct apple_pcie *pcie, struct apple_pcie_port *port, + int ret) { - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct pci_dev *root, *fn; - int i; + dev_warn(pcie->dev, "S2R: %pOF link not back in the noirq phase (%d, LTSSM %#x)\n", + port->np, ret, apple_pcie_port_ltssm(pcie, port)); +} - /* Pairs with the release once the functions' configuration is loaded. */ - if (!smp_load_acquire(&pcie->neo_config_ready)) - return; - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - if (!root) +/* A port that does not come up here is left to the normal phase, as before. */ +static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) +{ + struct apple_pcie_port *port; + unsigned long released = 0, trained = 0; + bool fast = false; + int ret; + + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->restore_link_up || !port->s2r_saved || !port->perst_gpio || + gpiod_cansleep(port->perst_gpio) || port->idx >= BITS_PER_LONG || + (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP)) + continue; + ret = apple_pcie_s2r_port_release_noirq(pcie, port); + if (ret) + apple_pcie_s2r_port_failed(pcie, port, ret); + else + released |= BIT(port->idx); + } + if (!released) return; - for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) + /* 100 ms after PERST# (PCIe r5.0, 6.6.1), once for all of them. */ + msleep(100); + list_for_each_entry(port, &pcie->ports, entry) { + if (port->idx >= BITS_PER_LONG || !(released & BIT(port->idx))) continue; - kfree(pcie->neo_fn_state[i]); - pcie->neo_fn_state[i] = pci_store_saved_state(fn); - pcie->neo_fn_pm_cap[i] = fn->pm_cap; - pci_dev_put(fn); + ret = apple_pcie_s2r_port_train_noirq(pcie, port); + if (ret) + apple_pcie_s2r_port_failed(pcie, port, ret); + else + trained |= BIT(port->idx); } - pci_dev_put(root); -} - -/* With the link back: what the core restores next is that state. */ -static void apple_pcie_neo_load_functions(struct apple_pcie *pcie, struct pci_dev *root) -{ - struct pci_dev *fn; - int i; + /* + * A link above 5 GT/s takes no config request before 100 ms after its training + * (PCIe r5.0, 6.6.1). Up to 5 GT/s the time since PERST# is what counts, and that + * has passed. + */ + list_for_each_entry(port, &pcie->ports, entry) { + void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); - for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) + if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) continue; - if (pcie->neo_fn_state[i]) - pci_load_saved_state(fn, pcie->neo_fn_state[i]); - pci_dev_put(fn); + /* The target, not the current speed: the change of speed may still run. */ + if ((readw_relaxed(dbi + NEO_DBI_CAP_LNKCTL2) & PCI_EXP_LNKCTL2_TLS) > + PCI_EXP_LNKCTL2_TLS_5_0GT) + fast = true; + } + if (fast) + msleep(100); + list_for_each_entry(port, &pcie->ports, entry) { + if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) + continue; + dev_info(pcie->dev, "S2R: %pOF link back with the loader's port settings\n", + port->np); } } -/* - * Closed config space keeps the PCI core's restore out of the functions, except for one thing: - * it writes an MSI-X table in their BAR, with no check of its own. With the functions out of - * reach, set that aside so that it has nothing to write; their reach back puts it in place. - */ -static void apple_pcie_neo_msix_gate(struct apple_pcie *pcie, bool reachable) +/* What only the port registers hold, for a restore that finds them reset. */ +static void apple_pcie_restore_save(struct apple_pcie *pcie) { - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct pci_dev *root, *fn; + struct apple_pcie_port *port; int i; - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - if (!root) - return; - for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_msix); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) - continue; - if (reachable && pcie->neo_fn_msix[i]) { - fn->msix_enabled = 1; - pcie->neo_fn_msix[i] = false; - } else if (!reachable && fn->msix_enabled) { - fn->msix_enabled = 0; - pcie->neo_fn_msix[i] = true; - } - pci_dev_put(fn); + mutex_lock(&pcie->lock); + list_for_each_entry(port, &pcie->ports, entry) { + apple_pcie_s2r_save_port(pcie, port); + port->restore_link_up = apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP; + port->restore_cold = false; + port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); + for_each_set_bit(i, port->sid_map, port->sid_map_sz) + port->saved_rid2sid[i] = + apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); } - pci_dev_put(root); + mutex_unlock(&pcie->lock); } -/* On the link: the one engine read that native ECAM needs, then the functions' config. */ -static int apple_pcie_neo_bootstrap_up(struct apple_pcie *pcie, struct pci_dev *root) +static void apple_pcie_restore_free_fns(struct apple_pcie *pcie) { - int ret; + struct apple_pcie_restore_fn *fn, *tmp; - ret = apple_piodma_bootstrap_power_lost(pcie->piodma_supplier); - if (!ret) - ret = apple_piodma_bootstrap_prime(pcie->piodma_supplier, root); - if (!ret) { - apple_piodma_bootstrap_admitted(pcie->piodma_supplier); - apple_pcie_neo_load_functions(pcie, root); - /* The functions' configuration before the flag that says it's there. */ - smp_store_release(&pcie->neo_config_ready, true); - apple_pcie_neo_msix_gate(pcie, true); + list_for_each_entry_safe(fn, tmp, &pcie->restore_fns, entry) { + list_del(&fn->entry); + kfree(fn->state); + pci_dev_put(fn->pdev); + kfree(fn); } - return ret; } /* - * The link back, and the one engine read that native ECAM needs on it. The root port's - * bus number is restored first, as that read crosses it. Not a firmware load. + * The PCI core's noirq restore spends the functions' saved state on a link that is down + * ("device inaccessible"). A copy taken first goes back once the link is: the core's normal + * restore then restores it (pci_pm_restore() restores a function whose state is saved). */ -static int apple_pcie_neo_radio_up(struct apple_pcie *pcie, struct apple_pcie_port *port) +static int apple_pcie_restore_keep_fn(struct pci_dev *pdev, void *data) { - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct pci_dev *root; - int ret; - - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - if (!root) - return -ENODEV; - pci_restore_state(root); - ret = apple_pcie_neo_link_up(pcie, port); - if (!ret) - ret = apple_pcie_neo_bootstrap_up(pcie, root); - pci_dev_put(root); + struct apple_pcie *pcie = data; + struct apple_pcie_port *port; + struct apple_pcie_restore_fn *fn; - return ret; + if (pci_is_root_bus(pdev->bus) || !pdev->state_saved) + return 0; + port = apple_pcie_get_port(pdev); + if (!port || !port->restore_cold) + return 0; + fn = kzalloc_obj(*fn); + if (!fn) + return 0; + fn->state = pci_store_saved_state(pdev); + if (!fn->state) { + kfree(fn); + return 0; + } + fn->pdev = pci_dev_get(pdev); + list_add_tail(&fn->entry, &pcie->restore_fns); + return 0; } /* - * After a sleep: the link, then the functions' config once the PCI core's resume_noirq is - * past them, which found nothing to restore. Their resume, and anything else below the host, - * waits for this through the bootstrap supplier's resume. + * Before the PCI core restores the functions. Reading the link state is what probe does on + * every cold boot; nothing is written to a port in this phase. Its PWREN is an SMC GPIO, and + * the SMC session is back only in the normal phase. */ -static void apple_pcie_neo_radio_work(struct work_struct *work) +static void apple_pcie_restore_check_noirq(struct apple_pcie *pcie, const char *what) { - struct apple_pcie *pcie = container_of(work, struct apple_pcie, neo_radio_work); struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); struct apple_pcie_port *port; - struct pci_dev *root, *fn; - int i, ret; + bool cold = false; - port = list_first_entry(&pcie->ports, struct apple_pcie_port, entry); - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - ret = root ? apple_pcie_neo_link_up(pcie, port) : -ENODEV; - if (!ret && !wait_for_completion_timeout(&pcie->neo_noirq_done, HZ)) - ret = -ETIMEDOUT; - if (!ret) - ret = apple_pcie_neo_bootstrap_up(pcie, root); - WRITE_ONCE(pcie->neo_radio_waking, false); - for (i = 0; !ret && root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) + apple_pcie_restore_free_fns(pcie); + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->restore_link_up) continue; - pci_restore_state(fn); - pci_dev_put(fn); - } - if (ret) { - /* The functions' resume then finds nothing, and their drivers say so. */ - WRITE_ONCE(pcie->neo_config_ready, false); - apple_pcie_neo_msix_gate(pcie, false); - dev_err(pcie->dev, "radios did not come back after sleep: %d\n", ret); - apple_pcie_neo_recover(pcie); - } else { - dev_dbg(pcie->dev, "radio link back after sleep, rail and firmware kept\n"); + if (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP) + continue; + port->restore_cold = true; + cold = true; + dev_info(pcie->dev, "%s: %pOF lost its link, setting it up again\n", + what, port->np); } - list_for_each_entry(port, &pcie->ports, entry) - apple_pcie_port_enable_irq(port); - pci_dev_put(root); - apple_piodma_bootstrap_radio_done(pcie->piodma_supplier); + if (cold && bridge->bus) + pci_walk_bus(bridge->bus, apple_pcie_restore_keep_fn, pcie); } /* - * The rail is the one thing that brings back a chip whose link won't train, a dead chip - * too. A cold chip then has its firmware loaded by the probe that follows. + * The J716c radio drivers save PCI state without entering D3. The PCI core + * keeps the root port in D0 as well. Its host and DART have no switchable + * power domains, and the DART saves its live translations without stopping + * them. For s2idle, retain that running hierarchy instead of resetting it. + * + * Check hardware PMCSR rather than current_state: the PCI core may mark a + * driverless root port PCI_UNKNOWN even though its hardware remains in D0. */ -static int apple_pcie_neo_rail_cycle(struct apple_pcie *pcie, struct apple_pcie_port *port, - struct pci_dev *root) +static int apple_pcie_check_d0(struct pci_dev *pdev, void *data) { - struct pci_dev *fn; - int i, ret; + int *error = data; + u16 value; - ret = gpiod_set_value_cansleep(port->perst_gpio, 1); - if (!ret) - ret = gpiod_set_value_cansleep(port->pwren_gpio, 0); - if (ret) - return ret; - msleep(pcie->hw->pwren_off_ms); - ret = gpiod_set_value_cansleep(port->pwren_gpio, 1); - if (ret) - return ret; - msleep(pcie->hw->pwren_on_ms); - ret = apple_pcie_neo_radio_up(pcie, port); - if (ret) - return ret; - /* The PCI core's restore found them gone. Wi-Fi's stop needs its BAR back. */ - for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) - continue; - pci_set_power_state(fn, PCI_D0); - pci_restore_state(fn); - pci_dev_put(fn); - } - return 0; -} - -/* The way a hotplug would. Bluetooth first: Wi-Fi's reset needs it gone. */ -static int apple_pcie_neo_stop_functions(struct apple_pcie *pcie, struct pci_dev *root) -{ - struct pci_dev *fn; - int ret = 0; - - /* Stopping Wi-Fi writes its BAR, which needs the chip to answer. */ - fn = root->subordinate ? pci_get_slot(root->subordinate, PCI_DEVFN(0, 0)) : NULL; - if (fn && !pci_device_is_present(fn)) - ret = -ENODEV; - pci_dev_put(fn); - if (ret) - return ret; - - pci_lock_rescan_remove(); - if (root->subordinate) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, 1)); - if (fn) { - pci_stop_and_remove_bus_device(fn); - pci_dev_put(fn); - } - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, 0)); - if (fn) { - ret = apple_piodma_bootstrap_radio_stop(pcie->piodma_supplier, fn); - if (!ret) - pci_stop_and_remove_bus_device(fn); - pci_dev_put(fn); - } - } - pci_unlock_rescan_remove(); - - return ret; -} - -static void apple_pcie_neo_scan_functions(struct pci_dev *root) -{ - pci_lock_rescan_remove(); - if (root->subordinate) { - pci_scan_child_bus(root->subordinate); - pci_assign_unassigned_bridge_resources(root); - pci_bus_add_devices(root->subordinate); + if (pci_read_config_word(pdev, PCI_VENDOR_ID, &value) || + value != pdev->vendor) { + *error = -ENODEV; + } else if (pdev->pm_cap && + (pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &value) || + (value & PCI_PM_CTRL_STATE_MASK) != PCI_D0)) { + *error = -EBUSY; } - pci_unlock_rescan_remove(); + if (*error) + pci_err(pdev, "adopted link requires accessible D0 devices for s2idle\n"); + return *error; } -/* - * Keeping the firmware across a sleep is what's wanted, and this is for when it didn't work: the - * radio lost power, its rings or its IPC. Removing the functions resets the chip, and their - * probes load the firmware again from ROM, as at boot. The link only needs its rail cycled if - * it didn't come back. Otherwise the next sleep tries the whole wake again. - */ -static void apple_pcie_neo_recover_work(struct work_struct *work) +static int apple_pcie_check_adopted_s2idle(struct apple_pcie *pcie) { - struct apple_pcie *pcie = container_of(work, struct apple_pcie, neo_recover_work); struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); struct apple_pcie_port *port; - struct pci_dev *root; - int ret = 0; - - /* A wake whose radio work outlived the resume finishes it first. */ - flush_work(&pcie->neo_radio_work); - WRITE_ONCE(pcie->neo_recover, false); - port = list_first_entry(&pcie->ports, struct apple_pcie_port, entry); - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - if (!root) - return; - if (READ_ONCE(pcie->neo_rc_down)) { - ret = -EIO; - goto out; - } - /* Pairs with the release once the functions' configuration is loaded. */ - if (!smp_load_acquire(&pcie->neo_config_ready)) { - dev_warn(pcie->dev, "radio link down after sleep: cycling its rail\n"); - ret = apple_pcie_neo_rail_cycle(pcie, port, root); - if (ret) - goto out; - } - ret = apple_pcie_neo_stop_functions(pcie, root); - if (ret != -ENODEV) - apple_pcie_neo_scan_functions(root); -out: - if (ret) - dev_err(pcie->dev, "radios stay down until the next sleep: %d\n", ret); - else - dev_info(pcie->dev, "radios restarted after sleep, firmware loaded again\n"); - pci_dev_put(root); -} + int error = 0; -/* - * Before the PCI core restores the functions, which need the link. They are held back - * until the radio port is enabled. - */ -static int apple_pcie_neo_resume_noirq(struct apple_pcie *pcie) -{ - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct apple_pcie_port *port; - struct pci_dev *root, *fn; - int i, ret = 0; + /* Other adopted platforms and deeper sleep remain unqualified. */ + if (!(of_machine_is_compatible("apple,j716c") || + of_machine_is_compatible("apple,k114c")) || !pm_suspend_no_platform()) + return -EBUSY; - /* Refused: as before the sleep. A host a failed wake left unprogrammed stays down. */ - if (!pm_resume_via_firmware()) { - if (pcie->neo_rc_was_down) - return 0; - list_for_each_entry(port, &pcie->ports, entry) - apple_pcie_port_enable_irq(port); - WRITE_ONCE(pcie->neo_rc_down, false); - return 0; - } + if (!(readl(pcie->base + CORE_RC_CTL) & CORE_RC_CTL_RUN) || + !(readl(pcie->base + CORE_RC_STAT) & CORE_RC_STAT_READY)) + return -ENOLINK; list_for_each_entry(port, &pcie->ports, entry) { - /* The pin controller just put it back as it was: released, with no clock yet. */ - ret = gpiod_set_value_cansleep(port->perst_gpio, 1); - if (!ret) - ret = apple_pcie_neo_cold_init(pcie, port); - if (ret) { - dev_err(pcie->dev, "host did not come back after sleep: %d\n", ret); - WRITE_ONCE(pcie->neo_config_ready, false); - apple_pcie_neo_msix_gate(pcie, false); - return ret; - } - apple_pcie_tunnel_restore_irq_hw(port); - apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); - for_each_set_bit(i, port->sid_map, port->sid_map_sz) - apple_pcie_rid2sid_write(port, i, port->saved_rid2sid[i]); - } - ret = apple_dart_host_resume(pcie->neo_cold->dart); - if (ret) { - WRITE_ONCE(pcie->neo_config_ready, false); - apple_pcie_neo_msix_gate(pcie, false); - return ret; - } - WRITE_ONCE(pcie->neo_rc_down, false); - apple_sleep_radio_port_up(); - - /* - * The link takes 100 ms, which nothing else here waits for: the work below brings it - * back. The root port's bus number now, before the PCI core restores the root port. The - * functions' config waits for their link, so the PCI core has nothing to restore there. - */ - root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); - if (!root) - return -ENODEV; - pci_restore_state(root); - for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { - fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); - if (!fn) - continue; - fn->state_saved = false; - pci_dev_put(fn); + if (READ_ONCE(port->link_failed) || + !(apple_pcie_port_readl(port, PORT_STATUS) & PORT_STATUS_READY) || + (apple_pcie_port_readl(port, PORT_LINKSTS) & + (PORT_LINKSTS_UP | PORT_LINKSTS_BUSY)) != PORT_LINKSTS_UP || + (apple_pcie_port_readl(port, PORT_INTSTAT) & BIT(PORT_INT_LINK_DOWN))) + return -ENOLINK; } - pci_dev_put(root); - WRITE_ONCE(pcie->neo_radio_waking, true); - reinit_completion(&pcie->neo_noirq_done); - apple_piodma_bootstrap_radio_pending(pcie->piodma_supplier); - queue_work(system_highpri_wq, &pcie->neo_radio_work); - - return 0; + pci_walk_bus(bridge->bus, apple_pcie_check_d0, &error); + return error; } -/* The PCI core's resume_noirq is done everywhere, the radio functions' included. */ -static int apple_pcie_resume_early(struct device *dev) +static int apple_pcie_suspend_noirq(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (pcie && pcie->neo_cold) - complete_all(&pcie->neo_noirq_done); - return 0; -} - -static int apple_pcie_resume_noirq(struct device *dev) -{ - struct apple_pcie *pcie = apple_pcie_lookup(dev); - struct apple_pcie_port *port; - int ret; + if (pcie->cio_prepared) + return apple_pcie_cio_suspend(pcie); + /* The reversed DART link is safe only with retained clocks or native PM. */ + if (pcie->cio_dart && (!pm_suspend_no_platform() || !s2idle_keep_link)) + return -EBUSY; + if (pcie->neo_sleep_armed) { + struct apple_pcie_port *port; - /* The domain's hold for the sleep ends first, whatever follows. */ - apple_pcie_release_domains(pcie); - if (pcie->cio_prepared) { - ret = apple_pcie_cio_resume(pcie); - if (!ret) - return 0; - /* - * A dock that left or lost power during the sleep. End like a - * surprise removal: the port stays stopped and config space - * closed, and the hierarchy is removed with the tunnel after - * resume. The NHI still ends the DART transaction. - */ - dev_err(dev, "PCIe-C not back after deep sleep: %d\n", ret); - pcie->resume_failed = true; - apple_pcie_tunnel_hierarchy_gone(dev); - return ret; - } - if (pcie->managed_sids) - return apple_radio_resume_noirq(pcie); - if (pcie->neo_sleep_armed) - return apple_pcie_neo_resume_noirq(pcie); - if (pcie->hw->adopt_link) { - if (!pcie->link_kept) - return 0; - pcie->link_kept = false; - ret = apple_pcie_check_adopted_s2idle(pcie); - if (ret) { - /* Block downstream config access if the retained link was lost. */ - list_for_each_entry(port, &pcie->ports, entry) - WRITE_ONCE(port->link_failed, true); - dev_err(dev, "adopted link lost during suspend-to-idle: %d\n", ret); - } - return ret; - } - if (!pcie->hw->tunneled) { - if (apple_pcie_hib_conventional(pcie) && pm_resume_via_firmware()) { - apple_pcie_s2r_up_noirq(pcie); - apple_pcie_hib_restore_noirq(pcie, "S2R"); - } - return 0; - } - if (pcie->resume_failed) - return -EIO; - /* - * A host quiesced by a tunnel deactivation has no hierarchy and its - * ports are stopped. Leave them stopped: restarting them without a - * tunnel fails and marks the host dead, while - * apple_pcie_tunnel_restore() restarts them on reactivation. - */ - if (pcie->bus_stopped) - return 0; - if (pcie->link_kept) { - pcie->link_kept = false; - if (apple_pcie_tunnel_link_healthy(pcie)) - return 0; - dev_warn(dev, "PCIe-C link lost during suspend-to-idle\n"); - /* - * A cold-initialized T600x/T602x port does not train again after - * an in-place restart. Fail like a surprise unplug instead: ACIO - * revalidates the connection after resume and replaces this host - * with a freshly cold-initialized one. - */ - if (pcie->tunnel_cold_init) { - ret = -ENOLINK; - goto failed; - } - /* Lost while asleep: take the same path as a stopped tunnel. */ - apple_pcie_stop_for_sleep(dev); - } - if (pcie->reset_on_resume) { - pcie->reset_on_resume = false; - ret = apple_pcie_reset_for_resume(pcie); - if (ret) { - dev_err(dev, "PCIe-C cold resume failed: %d\n", ret); - goto failed; + pcie->neo_rc_was_down = READ_ONCE(pcie->neo_rc_down); + /* A port that didn't come back from the last sleep keeps what it saved then. */ + list_for_each_entry(port, &pcie->ports, entry) { + int i; + + apple_pcie_port_disable_irq(port); + if (pcie->neo_rc_was_down) + continue; + port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); + for_each_set_bit(i, port->sid_map, port->sid_map_sz) + port->saved_rid2sid[i] = + apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); } + /* The functions too: the core read them through closed config space. */ + if (!pcie->neo_rc_was_down) + apple_pcie_neo_save_functions(pcie); + WRITE_ONCE(pcie->neo_rc_down, true); + /* + * Genpd would gate the host here, and an abandoned sleep then finds + * it unprogrammed with nothing to bring it up. The commit lowers it. + */ + device_set_awake_path(dev); + return 0; } - list_for_each_entry(port, &pcie->ports, entry) { - ret = apple_pcie_tunnel_start(port); - if (ret) - goto failed; - } - list_for_each_entry(port, &pcie->ports, entry) - apple_pcie_port_enable_irq(port); - - return 0; -failed: - /* - * Ports the sleep path stopped are still gated and masked; nothing has - * reopened them. A retained link that failed was never gated: leave it - * serviceable, as after a surprise unplug, until teardown masks it. - */ - /* Device PM keeps resuming dependents even after this callback fails. */ - pcie->resume_failed = true; - if (!pcie->bus_stopped) - apple_pcie_tunnel_hierarchy_gone(dev); - apple_pcie_walk_tunnel_darts(pcie, apple_dart_quiesce_commands, false); /* - * Leave the port clocked with its link down, the state a surprise - * unplug leaves behind, in which stray accesses complete with an - * error. Quiesce removes the hierarchy and then stops the port when - * ACIO is torn down after resume. + * The managed J713 radio has a native port/DART recovery transaction. + * Suspend-to-idle leaves its link in place (no power domain). */ - return ret; + if (pcie->managed_sids && pm_suspend_target_state != PM_SUSPEND_TO_IDLE) + return apple_radio_suspend_noirq(pcie); + if (!pcie->managed_sids && pcie->hw->adopt_link) { + int ret = apple_pcie_check_adopted_s2idle(pcie); + + pcie->link_kept = !ret; + return ret; + } + if (!pcie->hw->tunneled) { + if (pm_suspend_no_platform() && pcie->num_pds) + return apple_pcie_hold_domains(pcie); + if (apple_pcie_conventional_host(pcie) && pm_suspend_via_firmware()) + apple_pcie_restore_save(pcie); + return 0; + } + pcie->reset_on_resume = false; + pcie->link_kept = apple_pcie_keep_link(pcie); + if (!pcie->link_kept) { + if (pcie->cio_dart) + return -EBUSY; + apple_pcie_stop_for_sleep(dev); + } + return 0; } -static int apple_pcie_prepare(struct device *dev) +static int apple_pcie_reset_for_resume(struct apple_pcie *pcie) { - struct apple_pcie *pcie = apple_pcie_lookup(dev); + struct apple_pcie_port *port; + int i, ret; /* - * A host-owned PCIe-C DART sleeps only inside the NHI's deep-sleep - * transaction, prepared before this, or with a kept s2idle link. - * Refuse anything else now, before other devices suspend. + * Keep the hierarchy bound and suspended. Reset only after its I/O + * drained, then restore the port and IOMMU before releasing tunneled + * reset. Endpoints must never run with missing address translations. */ - if (pcie && pcie->cio_dart && !pcie->cio_prepared && - pm_suspend_target_state != PM_SUSPEND_ON && - (!pm_suspend_no_platform() || !s2idle_keep_link)) { - dev_warn(dev, "no CIO sleep transaction from the NHI; refusing sleep\n"); - return -EBUSY; + ret = reset_control_reset(pcie->reset); + if (ret) + return ret; + + list_for_each_entry(port, &pcie->ports, entry) { + u32 stat; + + /* A failed cold setup still owns partially enabled hardware. */ + port->needs_stop = true; + apple_pcie_port_writel(port, PORT_TUNCTRL_PERST_ON, apple_pcie_port_tunctrl(port)); + ret = apple_pcie_tunnel_cold_init(port); + if (ret) + return ret; + ret = read_poll_timeout_atomic(apple_pcie_port_readl, stat, + stat & PORT_TUNSTAT_PERST_ON, + 1000, 100000, false, port, + apple_pcie_port_tunstat(port)); + if (ret) + return ret; + apple_pcie_tunnel_restore_irq_hw(port); + for_each_set_bit(i, port->sid_map, port->sid_map_sz) + apple_pcie_rid2sid_write(port, i, port->saved_rid2sid[i]); } - return apple_radio_prepare(dev); + ret = apple_pcie_walk_tunnel_darts(pcie, apple_dart_restore_tunnel_state, true); + if (ret) + return ret; + + dev_info(pcie->dev, "PCIe-C reset and DART state restored before link resume\n"); + return 0; } /* - * A conventional host (the internal ports with their radio and SD reader) keeps running - * through freeze and thaw. Only a kernel restored from an image finds it changed: the boot - * that resumed it went through the boot loader, and when its kernel did not set the ports up - * (a boot kernel without the GPIO drivers that probing the host takes), the endpoints sit - * unpowered or in reset behind a link that is down. The restore then does what probe did. - * A real suspend-to-RAM takes the host's power as a resume from disk does, so its ports - * come back the same way. + * The radio kept its power and firmware, in reset since the commit. Its rail isn't touched: + * cycling it, or the port's own reset, is what a cold start needs. PERST# released, the link + * trains. Device interrupts are off in this phase, so this polls for it. */ -static bool apple_pcie_hib_conventional(struct apple_pcie *pcie) +static int apple_pcie_neo_link_up(struct apple_pcie *pcie, struct apple_pcie_port *port) { - return !pcie->hw->tunneled && !pcie->managed_sids && !pcie->piodma_supplier && - !pcie->hw->root_bus_only; + ktime_t released; + s64 left_us; + u32 stat; + int ret; + + usleep_range(1000, 2000); + ret = gpiod_set_value_cansleep(port->perst_gpio, 0); + if (ret) + return ret; + released = ktime_get(); + /* The training starts right after PERST#. */ + ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, + 100, 250000); + if (ret) + return ret; + apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); + ret = readl_relaxed_poll_timeout(port->base + PORT_LINKSTS, stat, stat & PORT_LINKSTS_UP, + 100, link_up_timeout * 1000); + if (ret) + return ret; + apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); + /* No config request before 100 ms after PERST#, on a 5 GT/s port (PCIe r5.0, 6.6.1). */ + left_us = 100 * USEC_PER_MSEC - ktime_us_delta(ktime_get(), released); + if (left_us > 0) + fsleep(left_us); + + return 0; } /* - * Root port words the loader limits at boot (one lane at the endpoint's speed) and a reset - * puts back to the core's own: link capabilities, the target speed, link mode and lane count. + * The PCI core saves the functions again at every sleep, and with the radio down it reads them + * through closed config space. Keep the last state they saved while they answered. */ -static const u16 apple_pcie_s2r_dbi[] = { - NEO_DBI_CAP_LNKCAP, NEO_DBI_CAP_LNKCAP2, NEO_DBI_CAP_LNKCTL2, - NEO_DBI_PORT_LINK_CONTROL, NEO_DBI_WIDTH_SPEED_CONTROL, -}; +static void apple_pcie_neo_save_functions(struct apple_pcie *pcie) +{ + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct pci_dev *root, *fn; + int i; -static int apple_pcie_hib_train(struct apple_pcie *pcie, struct apple_pcie_port *port); -static void apple_pcie_hib_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port); + /* Pairs with the release once the functions' configuration is loaded. */ + if (!smp_load_acquire(&pcie->neo_config_ready)) + return; + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return; + for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + kfree(pcie->neo_fn_state[i]); + pcie->neo_fn_state[i] = pci_store_saved_state(fn); + pcie->neo_fn_pm_cap[i] = fn->pm_cap; + pci_dev_put(fn); + } + pci_dev_put(root); +} -static void apple_pcie_s2r_save_port(struct apple_pcie *pcie, struct apple_pcie_port *port) +/* With the link back: what the core restores next is that state. */ +static void apple_pcie_neo_load_functions(struct apple_pcie *pcie, struct pci_dev *root) { - void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + struct pci_dev *fn; int i; - BUILD_BUG_ON(ARRAY_SIZE(apple_pcie_s2r_dbi) != ARRAY_SIZE(port->s2r_dbi)); - /* The loader's port setup writes 0x130; the port tunable has its own word. */ - port->s2r_port_130 = apple_pcie_port_readl(port, 0x130); - for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) - port->s2r_dbi[i] = readl_relaxed(dbi + apple_pcie_s2r_dbi[i]); - port->s2r_saved = true; + for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + if (pcie->neo_fn_state[i]) + pci_load_saved_state(fn, pcie->neo_fn_state[i]); + pci_dev_put(fn); + } } -/* The root port part goes in once the port runs again, before the link starts. */ -static void apple_pcie_s2r_restore_rc(struct apple_pcie *pcie, struct apple_pcie_port *port) +/* + * Closed config space keeps the PCI core's restore out of the functions, except for one thing: + * it writes an MSI-X table in their BAR, with no check of its own. With the functions out of + * reach, set that aside so that it has nothing to write; their reach back puts it in place. + */ +static void apple_pcie_neo_msix_gate(struct apple_pcie *pcie, bool reachable) { - void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct pci_dev *root, *fn; int i; - rmw_set(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); - for (i = 0; i < ARRAY_SIZE(port->s2r_rc_tunable); i++) - apple_tunable_apply(dbi, port->s2r_rc_tunable[i]); - for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) { - /* Link Control 2 shares its word with a status register. */ - if (apple_pcie_s2r_dbi[i] == NEO_DBI_CAP_LNKCTL2) - writew_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); - else - writel_relaxed(port->s2r_dbi[i], dbi + apple_pcie_s2r_dbi[i]); + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return; + for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_msix); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + if (reachable && pcie->neo_fn_msix[i]) { + fn->msix_enabled = 1; + pcie->neo_fn_msix[i] = false; + } else if (!reachable && fn->msix_enabled) { + fn->msix_enabled = 0; + pcie->neo_fn_msix[i] = true; + } + pci_dev_put(fn); } - /* The link goes to the target speed once it is up, as after the loader's setup. */ - rmw_set(NEO_DBI_SPEED_CHANGE, dbi + NEO_DBI_WIDTH_SPEED_CONTROL); - rmw_clear(NEO_DBI_RO_WR_EN, dbi + NEO_DBI_RO_WR); + pci_dev_put(root); +} + +/* On the link: the one engine read that native ECAM needs, then the functions' config. */ +static int apple_pcie_neo_bootstrap_up(struct apple_pcie *pcie, struct pci_dev *root) +{ + int ret; + + ret = apple_piodma_bootstrap_power_lost(pcie->piodma_supplier); + if (!ret) + ret = apple_piodma_bootstrap_prime(pcie->piodma_supplier, root); + if (!ret) { + apple_piodma_bootstrap_admitted(pcie->piodma_supplier); + apple_pcie_neo_load_functions(pcie, root); + /* The functions' configuration before the flag that says it's there. */ + smp_store_release(&pcie->neo_config_ready, true); + apple_pcie_neo_msix_gate(pcie, true); + } + return ret; } /* - * A real S2R took the port block's power: the sleep's wake side clocked the port again and - * the pin restore released PERST#, with the link down and every word of the port and of its - * root port at its reset value. Do what the loader and probe did between them, here, so that - * the PCI core's noirq pass finds the functions: PERST# asserted again, the port's and the - * root port's settings back, PERST# released, the link trained. The endpoint's power line - * is not touched: it is the SMC's, whose session is back only in the normal phase, and it - * reads on after the wake. - * - * In two steps, so that the ports share the two waits instead of each taking its own: up to - * the release of PERST#, and from the port's ready bit on. + * The link back, and the one engine read that native ECAM needs on it. The root port's + * bus number is restored first, as that read crosses it. Not a firmware load. */ -static int apple_pcie_s2r_port_release_noirq(struct apple_pcie *pcie, - struct apple_pcie_port *port) +static int apple_pcie_neo_radio_up(struct apple_pcie *pcie, struct apple_pcie_port *port) { + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct pci_dev *root; int ret; - rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); - gpiod_set_value(port->perst_gpio, 1); - if (!pcie->hw->retain_clocks) { - ret = apple_pcie_setup_refclk(pcie, port); - if (ret < 0) - return ret; + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return -ENODEV; + pci_restore_state(root); + ret = apple_pcie_neo_link_up(pcie, port); + if (!ret) + ret = apple_pcie_neo_bootstrap_up(pcie, root); + pci_dev_put(root); + + return ret; +} + +/* + * After a sleep: the link, then the functions' config once the PCI core's resume_noirq is + * past them, which found nothing to restore. Their resume, and anything else below the host, + * waits for this through the bootstrap supplier's resume. + */ +static void apple_pcie_neo_radio_work(struct work_struct *work) +{ + struct apple_pcie *pcie = container_of(work, struct apple_pcie, neo_radio_work); + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct apple_pcie_port *port; + struct pci_dev *root, *fn; + int i, ret; + + port = list_first_entry(&pcie->ports, struct apple_pcie_port, entry); + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + ret = root ? apple_pcie_neo_link_up(pcie, port) : -ENODEV; + if (!ret && !wait_for_completion_timeout(&pcie->neo_noirq_done, HZ)) + ret = -ETIMEDOUT; + if (!ret) + ret = apple_pcie_neo_bootstrap_up(pcie, root); + WRITE_ONCE(pcie->neo_radio_waking, false); + for (i = 0; !ret && root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + pci_restore_state(fn); + pci_dev_put(fn); } - apple_pcie_port_writel(port, port->s2r_port_130, 0x130); - apple_tunable_apply(port->base, port->s2r_port_tunable); - /* Tperst-clk (PCIe CEM r5.0, 2.9.2) */ - usleep_range(100, 200); - rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); - gpiod_set_value(port->perst_gpio, 0); - return 0; + if (ret) { + /* The functions' resume then finds nothing, and their drivers say so. */ + WRITE_ONCE(pcie->neo_config_ready, false); + apple_pcie_neo_msix_gate(pcie, false); + dev_err(pcie->dev, "radios did not come back after sleep: %d\n", ret); + apple_pcie_neo_recover(pcie); + } else { + dev_dbg(pcie->dev, "radio link back after sleep, rail and firmware kept\n"); + } + list_for_each_entry(port, &pcie->ports, entry) + apple_pcie_port_enable_irq(port); + pci_dev_put(root); + apple_piodma_bootstrap_radio_done(pcie->piodma_supplier); } -static int apple_pcie_s2r_port_train_noirq(struct apple_pcie *pcie, struct apple_pcie_port *port) +/* + * The rail is the one thing that brings back a chip whose link won't train, a dead chip + * too. A cold chip then has its firmware loaded by the probe that follows. + */ +static int apple_pcie_neo_rail_cycle(struct apple_pcie *pcie, struct apple_pcie_port *port, + struct pci_dev *root) { - u32 stat; - int ret; + struct pci_dev *fn; + int i, ret; - ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, - stat & PORT_STATUS_READY, 100, 250000); + ret = gpiod_set_value_cansleep(port->perst_gpio, 1); + if (!ret) + ret = gpiod_set_value_cansleep(port->pwren_gpio, 0); if (ret) return ret; - apple_pcie_s2r_restore_rc(pcie, port); - apple_pcie_port_gate_clocks(pcie, port); - apple_pcie_tunnel_restore_irq_hw(port); - apple_pcie_hib_rid2sid(pcie, port); - ret = apple_pcie_hib_train(pcie, port); + msleep(pcie->hw->pwren_off_ms); + ret = gpiod_set_value_cansleep(port->pwren_gpio, 1); if (ret) return ret; - apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); - apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + msleep(pcie->hw->pwren_on_ms); + ret = apple_pcie_neo_radio_up(pcie, port); + if (ret) + return ret; + /* The PCI core's restore found them gone. Wi-Fi's stop needs its BAR back. */ + for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + pci_set_power_state(fn, PCI_D0); + pci_restore_state(fn); + pci_dev_put(fn); + } return 0; } -static void apple_pcie_s2r_port_failed(struct apple_pcie *pcie, struct apple_pcie_port *port, - int ret) +/* The way a hotplug would. Bluetooth first: Wi-Fi's reset needs it gone. */ +static int apple_pcie_neo_stop_functions(struct apple_pcie *pcie, struct pci_dev *root) { - dev_warn(pcie->dev, "S2R: %pOF link not back in the noirq phase (%d, LTSSM %#x)\n", - port->np, ret, apple_pcie_port_ltssm(pcie, port)); -} + struct pci_dev *fn; + int ret = 0; -/* A port that does not come up here is left to the normal phase, as before. */ -static void apple_pcie_s2r_up_noirq(struct apple_pcie *pcie) -{ - struct apple_pcie_port *port; - unsigned long released = 0, trained = 0; - bool fast = false; - int ret; + /* Stopping Wi-Fi writes its BAR, which needs the chip to answer. */ + fn = root->subordinate ? pci_get_slot(root->subordinate, PCI_DEVFN(0, 0)) : NULL; + if (fn && !pci_device_is_present(fn)) + ret = -ENODEV; + pci_dev_put(fn); + if (ret) + return ret; - list_for_each_entry(port, &pcie->ports, entry) { - if (!port->hib_link_up || !port->s2r_saved || !port->perst_gpio || - gpiod_cansleep(port->perst_gpio) || port->idx >= BITS_PER_LONG || - (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP)) - continue; - ret = apple_pcie_s2r_port_release_noirq(pcie, port); - if (ret) - apple_pcie_s2r_port_failed(pcie, port, ret); - else - released |= BIT(port->idx); - } - if (!released) - return; - /* 100 ms after PERST# (PCIe r5.0, 6.6.1), once for all of them. */ - msleep(100); - list_for_each_entry(port, &pcie->ports, entry) { - if (port->idx >= BITS_PER_LONG || !(released & BIT(port->idx))) - continue; - ret = apple_pcie_s2r_port_train_noirq(pcie, port); - if (ret) - apple_pcie_s2r_port_failed(pcie, port, ret); - else - trained |= BIT(port->idx); + pci_lock_rescan_remove(); + if (root->subordinate) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, 1)); + if (fn) { + pci_stop_and_remove_bus_device(fn); + pci_dev_put(fn); + } + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, 0)); + if (fn) { + ret = apple_piodma_bootstrap_radio_stop(pcie->piodma_supplier, fn); + if (!ret) + pci_stop_and_remove_bus_device(fn); + pci_dev_put(fn); + } } - /* - * A link above 5 GT/s takes no config request before 100 ms after its training - * (PCIe r5.0, 6.6.1). Up to 5 GT/s the time since PERST# is what counts, and that - * has passed. - */ - list_for_each_entry(port, &pcie->ports, entry) { - void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); + pci_unlock_rescan_remove(); - if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) - continue; - /* The target, not the current speed: the change of speed may still run. */ - if ((readw_relaxed(dbi + NEO_DBI_CAP_LNKCTL2) & PCI_EXP_LNKCTL2_TLS) > - PCI_EXP_LNKCTL2_TLS_5_0GT) - fast = true; - } - if (fast) - msleep(100); - list_for_each_entry(port, &pcie->ports, entry) { - if (port->idx >= BITS_PER_LONG || !(trained & BIT(port->idx))) - continue; - dev_info(pcie->dev, "S2R: %pOF link back with the loader's port settings\n", - port->np); - } + return ret; } - -/* What only the port registers hold, for a restore that finds them reset. */ -static void apple_pcie_hib_save(struct apple_pcie *pcie) +static void apple_pcie_neo_scan_functions(struct pci_dev *root) { - struct apple_pcie_port *port; - int i; - - mutex_lock(&pcie->lock); - list_for_each_entry(port, &pcie->ports, entry) { - apple_pcie_s2r_save_port(pcie, port); - port->hib_link_up = apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP; - port->hib_cold = false; - port->saved_intmask = apple_pcie_port_readl(port, PORT_INTMSK); - for_each_set_bit(i, port->sid_map, port->sid_map_sz) - port->saved_rid2sid[i] = - apple_pcie_port_readl(port, port_rid2sid_offset(port, i)); + pci_lock_rescan_remove(); + if (root->subordinate) { + pci_scan_child_bus(root->subordinate); + pci_assign_unassigned_bridge_resources(root); + pci_bus_add_devices(root->subordinate); } - mutex_unlock(&pcie->lock); + pci_unlock_rescan_remove(); } -static void apple_pcie_hib_free_fns(struct apple_pcie *pcie) +/* + * Keeping the firmware across a sleep is what's wanted, and this is for when it didn't work: the + * radio lost power, its rings or its IPC. Removing the functions resets the chip, and their + * probes load the firmware again from ROM, as at boot. The link only needs its rail cycled if + * it didn't come back. Otherwise the next sleep tries the whole wake again. + */ +static void apple_pcie_neo_recover_work(struct work_struct *work) { - struct apple_pcie_hib_fn *fn, *tmp; + struct apple_pcie *pcie = container_of(work, struct apple_pcie, neo_recover_work); + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); + struct apple_pcie_port *port; + struct pci_dev *root; + int ret = 0; - list_for_each_entry_safe(fn, tmp, &pcie->hib_fns, entry) { - list_del(&fn->entry); - kfree(fn->state); - pci_dev_put(fn->pdev); - kfree(fn); + /* A wake whose radio work outlived the resume finishes it first. */ + flush_work(&pcie->neo_radio_work); + WRITE_ONCE(pcie->neo_recover, false); + port = list_first_entry(&pcie->ports, struct apple_pcie_port, entry); + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return; + if (READ_ONCE(pcie->neo_rc_down)) { + ret = -EIO; + goto out; + } + /* Pairs with the release once the functions' configuration is loaded. */ + if (!smp_load_acquire(&pcie->neo_config_ready)) { + dev_warn(pcie->dev, "radio link down after sleep: cycling its rail\n"); + ret = apple_pcie_neo_rail_cycle(pcie, port, root); + if (ret) + goto out; } + ret = apple_pcie_neo_stop_functions(pcie, root); + if (ret != -ENODEV) + apple_pcie_neo_scan_functions(root); +out: + if (ret) + dev_err(pcie->dev, "radios stay down until the next sleep: %d\n", ret); + else + dev_info(pcie->dev, "radios restarted after sleep, firmware loaded again\n"); + pci_dev_put(root); } /* - * The PCI core's noirq restore spends the functions' saved state on a link that is down - * ("device inaccessible"). A copy taken first goes back once the link is: the core's normal - * restore then restores it (pci_pm_restore() restores a function whose state is saved). + * Before the PCI core restores the functions, which need the link. They are held back + * until the radio port is enabled. */ -static int apple_pcie_hib_keep_fn(struct pci_dev *pdev, void *data) +static int apple_pcie_neo_resume_noirq(struct apple_pcie *pcie) { - struct apple_pcie *pcie = data; + struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); struct apple_pcie_port *port; - struct apple_pcie_hib_fn *fn; + struct pci_dev *root, *fn; + int i, ret = 0; - if (pci_is_root_bus(pdev->bus) || !pdev->state_saved) - return 0; - port = apple_pcie_get_port(pdev); - if (!port || !port->hib_cold) - return 0; - fn = kzalloc_obj(*fn); - if (!fn) - return 0; - fn->state = pci_store_saved_state(pdev); - if (!fn->state) { - kfree(fn); + /* Refused: as before the sleep. A host a failed wake left unprogrammed stays down. */ + if (!pm_resume_via_firmware()) { + if (pcie->neo_rc_was_down) + return 0; + list_for_each_entry(port, &pcie->ports, entry) + apple_pcie_port_enable_irq(port); + WRITE_ONCE(pcie->neo_rc_down, false); return 0; } - fn->pdev = pci_dev_get(pdev); - list_add_tail(&fn->entry, &pcie->hib_fns); + + list_for_each_entry(port, &pcie->ports, entry) { + /* The pin controller just put it back as it was: released, with no clock yet. */ + ret = gpiod_set_value_cansleep(port->perst_gpio, 1); + if (!ret) + ret = apple_pcie_neo_cold_init(pcie, port); + if (ret) { + dev_err(pcie->dev, "host did not come back after sleep: %d\n", ret); + WRITE_ONCE(pcie->neo_config_ready, false); + apple_pcie_neo_msix_gate(pcie, false); + return ret; + } + apple_pcie_tunnel_restore_irq_hw(port); + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + for_each_set_bit(i, port->sid_map, port->sid_map_sz) + apple_pcie_rid2sid_write(port, i, port->saved_rid2sid[i]); + } + ret = apple_dart_host_resume(pcie->neo_cold->dart); + if (ret) { + WRITE_ONCE(pcie->neo_config_ready, false); + apple_pcie_neo_msix_gate(pcie, false); + return ret; + } + WRITE_ONCE(pcie->neo_rc_down, false); + apple_sleep_radio_port_up(); + + /* + * The link takes 100 ms, which nothing else here waits for: the work below brings it + * back. The root port's bus number now, before the PCI core restores the root port. The + * functions' config waits for their link, so the PCI core has nothing to restore there. + */ + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return -ENODEV; + pci_restore_state(root); + for (i = 0; root->subordinate && i < ARRAY_SIZE(pcie->neo_fn_state); i++) { + fn = pci_get_slot(root->subordinate, PCI_DEVFN(0, i)); + if (!fn) + continue; + fn->state_saved = false; + pci_dev_put(fn); + } + pci_dev_put(root); + WRITE_ONCE(pcie->neo_radio_waking, true); + reinit_completion(&pcie->neo_noirq_done); + apple_piodma_bootstrap_radio_pending(pcie->piodma_supplier); + queue_work(system_highpri_wq, &pcie->neo_radio_work); + return 0; } -/* - * Before the PCI core restores the functions. Reading the link state is what probe does on - * every cold boot; nothing is written to a port in this phase. Its PWREN is an SMC GPIO, and - * the SMC session is back only in the normal phase. - */ -static void apple_pcie_hib_restore_noirq(struct apple_pcie *pcie, const char *what) +/* The PCI core's resume_noirq is done everywhere, the radio functions' included. */ +static int apple_pcie_resume_early(struct device *dev) { - struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); - struct apple_pcie_port *port; - bool cold = false; + struct apple_pcie *pcie = apple_pcie_lookup(dev); - apple_pcie_hib_free_fns(pcie); - list_for_each_entry(port, &pcie->ports, entry) { - if (!port->hib_link_up) - continue; - if (apple_pcie_port_readl(port, PORT_LINKSTS) & PORT_LINKSTS_UP) - continue; - port->hib_cold = true; - cold = true; - dev_info(pcie->dev, "%s: %pOF lost its link, setting it up again\n", - what, port->np); - } - if (cold && bridge->bus) - pci_walk_bus(bridge->bus, apple_pcie_hib_keep_fn, pcie); + if (pcie && pcie->neo_cold) + complete_all(&pcie->neo_noirq_done); + return 0; } -/* Bounded like probe's wait, polled: the link interrupts stay masked meanwhile. */ -static int apple_pcie_hib_train(struct apple_pcie *pcie, struct apple_pcie_port *port) +static int apple_pcie_resume_noirq(struct device *dev) { - int attempt, attempts = port->perst ? 1 + LINK_RESET_RETRIES : 1; - u32 stat; - int ret = -ETIMEDOUT; + struct apple_pcie *pcie = apple_pcie_lookup(dev); + struct apple_pcie_port *port; + int ret; - for (attempt = 0; attempt < attempts; attempt++) { - if (attempt) { - dev_info(pcie->dev, "%pOF link down after %dms (LTSSM %#x), resetting the endpoint\n", - port->np, link_up_timeout, apple_pcie_port_ltssm(pcie, port)); - ret = apple_pcie_port_reset_endpoint(pcie, port); - if (ret) - return ret; - } - apple_pcie_port_writel(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); - ret = readl_relaxed_poll_timeout(port->base + PORT_LINKSTS, stat, - stat & PORT_LINKSTS_UP, 100, - link_up_timeout * 1000); + /* The domain's hold for the sleep ends first, whatever follows. */ + apple_pcie_release_domains(pcie); + if (pcie->cio_prepared) { + ret = apple_pcie_cio_resume(pcie); if (!ret) - break; + return 0; + /* + * A dock that left or lost power during the sleep. End like a + * surprise removal: the port stays stopped and config space + * closed, and the hierarchy is removed with the tunnel after + * resume. The NHI still ends the DART transaction. + */ + dev_err(dev, "PCIe-C not back after deep sleep: %d\n", ret); + pcie->resume_failed = true; + apple_pcie_tunnel_hierarchy_gone(dev); + return ret; } - return ret; -} + if (pcie->managed_sids) + return apple_radio_resume_noirq(pcie); + if (pcie->neo_sleep_armed) + return apple_pcie_neo_resume_noirq(pcie); + if (pcie->hw->adopt_link) { + if (!pcie->link_kept) + return 0; + pcie->link_kept = false; + ret = apple_pcie_check_adopted_s2idle(pcie); + if (ret) { + /* Block downstream config access if the retained link was lost. */ + list_for_each_entry(port, &pcie->ports, entry) + WRITE_ONCE(port->link_failed, true); + dev_err(dev, "adopted link lost during suspend-to-idle: %d\n", ret); + } + return ret; + } + if (!pcie->hw->tunneled) { + if (apple_pcie_conventional_host(pcie) && pm_resume_via_firmware()) { + apple_pcie_s2r_up_noirq(pcie); + apple_pcie_restore_check_noirq(pcie, "S2R"); + } + return 0; + } + if (pcie->resume_failed) + return -EIO; + /* + * A host quiesced by a tunnel deactivation has no hierarchy and its + * ports are stopped. Leave them stopped: restarting them without a + * tunnel fails and marks the host dead, while + * apple_pcie_tunnel_restore() restarts them on reactivation. + */ + if (pcie->bus_stopped) + return 0; + if (pcie->link_kept) { + pcie->link_kept = false; + if (apple_pcie_tunnel_link_healthy(pcie)) + return 0; + dev_warn(dev, "PCIe-C link lost during suspend-to-idle\n"); + /* + * A cold-initialized T600x/T602x port does not train again after + * an in-place restart. Fail like a surprise unplug instead: ACIO + * revalidates the connection after resume and replaces this host + * with a freshly cold-initialized one. + */ + if (pcie->tunnel_cold_init) { + ret = -ENOLINK; + goto failed; + } + /* Lost while asleep: take the same path as a stopped tunnel. */ + apple_pcie_stop_for_sleep(dev); + } + if (pcie->reset_on_resume) { + pcie->reset_on_resume = false; + ret = apple_pcie_reset_for_resume(pcie); + if (ret) { + dev_err(dev, "PCIe-C cold resume failed: %d\n", ret); + goto failed; + } + } + list_for_each_entry(port, &pcie->ports, entry) { + ret = apple_pcie_tunnel_start(port); + if (ret) + goto failed; + } + list_for_each_entry(port, &pcie->ports, entry) + apple_pcie_port_enable_irq(port); -/* This kernel's RID-to-SID table, every other entry cleared as probe clears them. */ -static void apple_pcie_hib_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port) -{ - int i; + return 0; - mutex_lock(&pcie->lock); - for (i = 0; i < port->sid_map_sz; i++) - apple_pcie_rid2sid_write(port, i, test_bit(i, port->sid_map) ? - port->saved_rid2sid[i] : 0); - mutex_unlock(&pcie->lock); +failed: + /* + * Ports the sleep path stopped are still gated and masked; nothing has + * reopened them. A retained link that failed was never gated: leave it + * serviceable, as after a surprise unplug, until teardown masks it. + */ + /* Device PM keeps resuming dependents even after this callback fails. */ + pcie->resume_failed = true; + if (!pcie->bus_stopped) + apple_pcie_tunnel_hierarchy_gone(dev); + apple_pcie_walk_tunnel_darts(pcie, apple_dart_quiesce_commands, false); + /* + * Leave the port clocked with its link down, the state a surprise + * unplug leaves behind, in which stray accesses complete with an + * error. Quiesce removes the hierarchy and then stops the port when + * ACIO is torn down after resume. + */ + return ret; } /* Probe's bring-up of a conventional port, with the state this kernel had programmed. */ -static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_port *port) +static int apple_pcie_restore_port_up(struct apple_pcie *pcie, struct apple_pcie_port *port) { int ret; @@ -6884,8 +6858,8 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por apple_pcie_port_gate_clocks(pcie, port); /* All port interrupts masked and cleared, MSI routing as at probe. */ apple_pcie_tunnel_restore_irq_hw(port); - apple_pcie_hib_rid2sid(pcie, port); - ret = apple_pcie_hib_train(pcie, port); + apple_pcie_restore_rid2sid(pcie, port); + ret = apple_pcie_restore_train(pcie, port); if (ret) return ret; apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); @@ -6895,6 +6869,24 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por return 0; } +static int apple_pcie_prepare(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + /* + * A host-owned PCIe-C DART sleeps only inside the NHI's deep-sleep + * transaction, prepared before this, or with a kept s2idle link. + * Refuse anything else now, before other devices suspend. + */ + if (pcie && pcie->cio_dart && !pcie->cio_prepared && + pm_suspend_target_state != PM_SUSPEND_ON && + (!pm_suspend_no_platform() || !s2idle_keep_link)) { + dev_warn(dev, "no CIO sleep transaction from the NHI; refusing sleep\n"); + return -EBUSY; + } + return apple_radio_prepare(dev); +} + /* * The normal phase, after the SMC's own restore (the PWREN GPIOs are its) and before the PCI * core's normal restore of the functions, which waits for this host as their ancestor. @@ -6903,42 +6895,42 @@ static void apple_pcie_conventional_up(struct apple_pcie *pcie, const char *what { struct device *dev = pcie->dev; struct apple_pcie_port *port; - struct apple_pcie_hib_fn *fn; + struct apple_pcie_restore_fn *fn; unsigned int back = 0; int ret; list_for_each_entry(port, &pcie->ports, entry) { - if (!port->hib_link_up) + if (!port->restore_link_up) continue; - if (!port->hib_cold) { + if (!port->restore_cold) { /* * Up: set up by the boot that resumed, or never reset. The functions * this kernel enabled keep their mappings only if the table is its own. */ - apple_pcie_hib_rid2sid(pcie, port); + apple_pcie_restore_rid2sid(pcie, port); continue; } - ret = apple_pcie_hib_port_up(pcie, port); + ret = apple_pcie_restore_port_up(pcie, port); if (ret) { dev_err(dev, "%s: %pOF link not back (%d), its functions stay unreachable\n", what, port->np, ret); - port->hib_cold = false; + port->restore_cold = false; continue; } dev_info(dev, "%s: %pOF link back, PWREN and PERST# cycled as at probe\n", what, port->np); back++; } - list_for_each_entry(fn, &pcie->hib_fns, entry) { + list_for_each_entry(fn, &pcie->restore_fns, entry) { port = apple_pcie_get_port(fn->pdev); - if (!port || !port->hib_cold) + if (!port || !port->restore_cold) continue; if (pci_load_saved_state(fn->pdev, fn->state)) pci_warn(fn->pdev, "%s: saved configuration not taken back\n", what); } - apple_pcie_hib_free_fns(pcie); + apple_pcie_restore_free_fns(pcie); list_for_each_entry(port, &pcie->ports, entry) - port->hib_cold = false; + port->restore_cold = false; if (back) dev_dbg(dev, "%s: %u port(s) set up again\n", what, back); } @@ -6947,7 +6939,7 @@ static int apple_pcie_restore(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (!apple_pcie_hib_conventional(pcie) || !pm_hibernate_is_restored()) + if (!apple_pcie_conventional_host(pcie) || !pm_hibernate_is_restored()) return 0; apple_pcie_conventional_up(pcie, "resume from disk"); return 0; @@ -6958,7 +6950,7 @@ static int apple_pcie_resume(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (!apple_pcie_hib_conventional(pcie) || !pm_resume_via_firmware()) + if (!apple_pcie_conventional_host(pcie) || !pm_resume_via_firmware()) return 0; apple_pcie_conventional_up(pcie, "S2R"); return 0; @@ -6969,15 +6961,15 @@ static int apple_pcie_resume(struct device *dev) * tunnel as a system sleep that keeps nothing does, thaw and restore start it * again. After a restore the controller comes back from a power cycle, so the * start replays the whole cold sequence. A conventional host only saves what a - * restore over a cold boot needs, see apple_pcie_hib_conventional(). + * restore over a cold boot needs, see apple_pcie_conventional_host(). */ static int apple_pcie_hibernate_noirq(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); if (!pcie->hw->tunnel_hibernate) { - if (apple_pcie_hib_conventional(pcie)) - apple_pcie_hib_save(pcie); + if (apple_pcie_conventional_host(pcie)) + apple_pcie_restore_save(pcie); return 0; } pcie->reset_on_resume = false; @@ -7001,8 +6993,8 @@ static int apple_pcie_restore_noirq(struct device *dev) struct apple_pcie *pcie = apple_pcie_lookup(dev); if (!pcie->hw->tunnel_hibernate) { - if (apple_pcie_hib_conventional(pcie) && pm_hibernate_is_restored()) - apple_pcie_hib_restore_noirq(pcie, "resume from disk"); + if (apple_pcie_conventional_host(pcie) && pm_hibernate_is_restored()) + apple_pcie_restore_check_noirq(pcie, "resume from disk"); return 0; } return apple_pcie_resume_noirq(dev); From 5459bcaf967d935915ebf53c25e8168c001abaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:48:32 +0100 Subject: [PATCH 205/269] PCI: apple: select the conventional restore by match data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The restore of an internal host (radio and SD reader behind a down link after a restored image or a real suspend-to-RAM) was selected by exclusion: not tunneled, no managed SIDs, no PIODMA supplier, not root bus only. A supplier is only taken on a root bus only host, so that term was implied. Set one hw_info flag on the four conventional controllers instead (T8103, T602x, T8152 and T6050 adopt); only the managed SID list, which comes from the board and not from the controller, stays a run-time test. The two bring-ups that bring a restored port back end in the same five steps: gate the clocks, restore the interrupt routing, program the RID-to-SID table, train the link, unmask what the kernel had. Make them one helper instead of two copies. The same set of controllers selects the restore as before. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 51 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 3b0d65ab229a7..1130046fcd921 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -281,6 +281,12 @@ struct hw_info { bool tunnel_ack_on_set; /* Stop the tunnel for hibernation and start it on thaw and restore. */ bool tunnel_hibernate; + /* + * Internal ports with their endpoints, which a restored image or a real S2R finds + * reset behind a down link: probe's bring-up runs again, see + * apple_pcie_conventional_host(). + */ + bool conventional_restore; }; static const struct hw_info t8103_hw = { @@ -292,6 +298,7 @@ static const struct hw_info t8103_hw = { .port_rid2sid = PORT_RID2SID, .port_msimap = 0, .max_rid2sid = 64, + .conventional_restore = true, }; static const struct hw_info t602x_hw = { @@ -304,6 +311,7 @@ static const struct hw_info t602x_hw = { .port_msimap = PORT_T602X_MSIMAP, /* 16 on t602x, guess for autodetect on future HW */ .max_rid2sid = 512, + .conventional_restore = true, }; /* @@ -336,6 +344,7 @@ static const struct hw_info t8152_hw = { .max_rid2sid = 512, .refbuf_off = true, .pipe_rst = true, + .conventional_restore = true, }; /* @@ -351,6 +360,7 @@ static const struct hw_info t6050_hw = { .max_rid2sid = 64, .retain_clocks = true, .adopt_link = true, + .conventional_restore = true, }; #define T6050_ROUTE(rid, sid) (PORT_RID2SID_VALID | \ @@ -5885,8 +5895,7 @@ static void apple_pcie_neo_save_functions(struct apple_pcie *pcie); */ static bool apple_pcie_conventional_host(struct apple_pcie *pcie) { - return !pcie->hw->tunneled && !pcie->managed_sids && !pcie->piodma_supplier && - !pcie->hw->root_bus_only; + return pcie->hw->conventional_restore && !pcie->managed_sids; } /* @@ -5935,6 +5944,26 @@ static void apple_pcie_restore_rid2sid(struct apple_pcie *pcie, struct apple_pci mutex_unlock(&pcie->lock); } +/* + * What both ways back have in common once the port runs with its root port set: clocks gated + * as at probe, all port interrupts masked and cleared with the MSI routing as at probe, the + * RID-to-SID table, the link trained, and the interrupt mask the kernel had. + */ +static int apple_pcie_restore_port_finish(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + int ret; + + apple_pcie_port_gate_clocks(pcie, port); + apple_pcie_tunnel_restore_irq_hw(port); + apple_pcie_restore_rid2sid(pcie, port); + ret = apple_pcie_restore_train(pcie, port); + if (ret) + return ret; + apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); + apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); + return 0; +} + static void apple_pcie_s2r_save_port(struct apple_pcie *pcie, struct apple_pcie_port *port) { void __iomem *dbi = pcie->cfg->win + ((resource_size_t)port->idx << 15); @@ -6012,15 +6041,7 @@ static int apple_pcie_s2r_port_train_noirq(struct apple_pcie *pcie, struct apple if (ret) return ret; apple_pcie_s2r_restore_rc(pcie, port); - apple_pcie_port_gate_clocks(pcie, port); - apple_pcie_tunnel_restore_irq_hw(port); - apple_pcie_restore_rid2sid(pcie, port); - ret = apple_pcie_restore_train(pcie, port); - if (ret) - return ret; - apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); - apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); - return 0; + return apple_pcie_restore_port_finish(pcie, port); } static void apple_pcie_s2r_port_failed(struct apple_pcie *pcie, struct apple_pcie_port *port, @@ -6855,15 +6876,9 @@ static int apple_pcie_restore_port_up(struct apple_pcie *pcie, struct apple_pcie ret = apple_pcie_setup_link(pcie, port, port->np); if (ret) return ret; - apple_pcie_port_gate_clocks(pcie, port); - /* All port interrupts masked and cleared, MSI routing as at probe. */ - apple_pcie_tunnel_restore_irq_hw(port); - apple_pcie_restore_rid2sid(pcie, port); - ret = apple_pcie_restore_train(pcie, port); + ret = apple_pcie_restore_port_finish(pcie, port); if (ret) return ret; - apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); - apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); /* 100 ms after the link before configuration requests (PCIe r5.0, 6.6.1). */ msleep(100); return 0; From 16fa403b0bc42e8600e9e45199cc0886c89849bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:48:53 +0100 Subject: [PATCH 206/269] PCI: apple: check the RID-to-SID table after a restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A restore writes the whole RID-to-SID table back, every other entry cleared, on a block that has just come back from a power cycle or a reset. The write already reads each word back for completion; compare it with what was written and warn once per port, with the first word that differs, when the table did not take. The restore carries on either way: a table that did not stick shows up as DART faults of the affected function, and the warning names the cause. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 1130046fcd921..f411ca0c3608b 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -5935,13 +5935,26 @@ static int apple_pcie_restore_train(struct apple_pcie *pcie, struct apple_pcie_p /* This kernel's RID-to-SID table, every other entry cleared as probe clears them. */ static void apple_pcie_restore_rid2sid(struct apple_pcie *pcie, struct apple_pcie_port *port) { - int i; + u32 val, got, want = 0, bad = 0, first = 0; + int i, first_idx = -1; mutex_lock(&pcie->lock); - for (i = 0; i < port->sid_map_sz; i++) - apple_pcie_rid2sid_write(port, i, test_bit(i, port->sid_map) ? - port->saved_rid2sid[i] : 0); + for (i = 0; i < port->sid_map_sz; i++) { + val = test_bit(i, port->sid_map) ? port->saved_rid2sid[i] : 0; + got = apple_pcie_rid2sid_write(port, i, val); + if (got == val) + continue; + if (!bad++) { + first_idx = i; + want = val; + first = got; + } + } mutex_unlock(&pcie->lock); + if (bad) + dev_warn(pcie->dev, + "%pOF: %u RID-to-SID word(s) did not read back as written after the restore (first: entry %d wrote %#x, read %#x)\n", + port->np, bad, first_idx, want, first); } /* From e66bd72adb71a4cf1e5e35e3d7c65ded93162218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:58:52 +0100 Subject: [PATCH 207/269] iommu/apple-dart, nvme-apple, PCI: apple: drop the restore callbacks' checks for an abandoned hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/iommu/apple-dart.c, drivers/nvme/host/apple.c, drivers/pci/controller/pcie-apple.c part of "soc: apple: drop the restore callbacks' checks for an abandoned hibernation", which goes with the PCIe, storage and radio changes; the rest of that commit is with the sleep controller changes. An abandoned hibernation resumed its devices with PMSG_RESTORE, so every restore callback that must not act on a state it never lost began with a pm_hibernate_is_restored() check, and most forwarded to their thaw. The PM core now sends PMSG_RECOVER there, which reaches the thaw callbacks, so the check and the forward go from macsmc, the RTKit helper, apple-dart, the sn201202x Type-C controller, the SPMI, GPIO and NVMe restore paths, both PCIe restore callbacks, the DCP and AOP restores, the t6040 USB2 PHY's two and the RTC; cs42l84's restore passes that the codec lost its state. Each callback runs for a restored kernel exactly as before. For an abandoned hibernation the old path was already the thaw or nothing, and the thaw now runs on its own: macsmc, the RTKit helper, apple-dart, sn201202x, NVMe and cs42l84 forwarded to the same function (cs42l84's resume asks pm_resume_via_firmware(), which hibernation_snapshot() has cleared); PCIe's restore_noirq did for a bridge without hibernation tunnelling what its thaw_noirq does, and the same call with it; the AOP, t6040, RTC, GPIO and SPMI restores did nothing and have no thaw. DCP keeps its power-off-failed branch (PMSG_RESTORE after a failed platform power-off, which only a platform with hibernation ops reaches) and loses the abandoned one, whose only other effect was a log line. One path changes: macsmc's thaw, after the SMC was handed over and the image could not be entered, calls apple_smc_restore() to start a new session, and its own check sent that back to the thaw, which did nothing. It now starts the session as the comment says it should. The flag keeps its other users, which are not restore callbacks: the AIC syscore resume, the dockchannel receive interrupt, the SEP's PM notifier. Signed-off-by: František Balázsy --- drivers/iommu/apple-dart.c | 3 --- drivers/nvme/host/apple.c | 3 +-- drivers/pci/controller/pcie-apple.c | 6 +++--- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 15b6a97fa1efc..b046fc8aa8388 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -6814,9 +6814,6 @@ static int apple_dart_restore(struct device *dev) struct apple_dart *dart = dev_get_drvdata(dev); int ret; - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return apple_dart_system_resume(dev); ret = pm_runtime_force_resume(dev); if (ret || !dart->locked) return ret; diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 4012d039a3939..0b1182064f97c 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -3221,8 +3221,7 @@ static int apple_nvme_restore(struct device *dev) struct apple_nvme *anv = dev_get_drvdata(dev); int ret; - /* Hibernation aborted before the image: the same ANS session runs on. */ - if (anv->hw->needs_ioq_registers && pm_hibernate_is_restored()) { + if (anv->hw->needs_ioq_registers) { ret = apple_nvme_restore_post_m4(anv); if (ret) return ret; diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index f411ca0c3608b..4e40434a2b4b2 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -6967,7 +6967,7 @@ static int apple_pcie_restore(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (!apple_pcie_conventional_host(pcie) || !pm_hibernate_is_restored()) + if (!apple_pcie_conventional_host(pcie)) return 0; apple_pcie_conventional_up(pcie, "resume from disk"); return 0; @@ -7015,13 +7015,13 @@ static int apple_pcie_wake_noirq(struct device *dev) return apple_pcie_resume_noirq(dev); } -/* Thaw and an aborted hibernation keep every port as it was: only a restored image looks. */ +/* Thaw keeps every port as it was: only a restored image looks. */ static int apple_pcie_restore_noirq(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); if (!pcie->hw->tunnel_hibernate) { - if (apple_pcie_conventional_host(pcie) && pm_hibernate_is_restored()) + if (apple_pcie_conventional_host(pcie)) apple_pcie_restore_check_noirq(pcie, "resume from disk"); return 0; } From e408512028052ae070852616a47f75e22fe97782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 19:37:57 +0100 Subject: [PATCH 208/269] PCI: apple: say why the sleep reads the root port words each time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The words a sleep that powers the port block down has to put back (link capabilities, Link Control 2, the link mode and width, port word 0x130) are read at every sleep. They cannot be read once at probe: a cold initialization of the port writes them again, the PCI core changes the target speed through the root port's config space, and a tunnel's setup writes 0x130, so a copy from probe can differ from the state the restore has to reproduce. Say so where they are read. No functional change. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 4e40434a2b4b2..2c03507f6bb6b 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -5983,7 +5983,13 @@ static void apple_pcie_s2r_save_port(struct apple_pcie *pcie, struct apple_pcie_ int i; BUILD_BUG_ON(ARRAY_SIZE(apple_pcie_s2r_dbi) != ARRAY_SIZE(port->s2r_dbi)); - /* The loader's port setup writes 0x130; the port tunable has its own word. */ + /* + * Read at every sleep, not once at probe: after the loader, these words are written again + * by the cold initialization of a port (link capabilities, mode, width, target speed), + * by the PCI core through the root port's config space (Link Control 2) and by a tunnel's + * setup (0x130), so the restore has to reproduce the values of the moment. + * The loader's port setup writes 0x130; the port tunable has its own word. + */ port->s2r_port_130 = apple_pcie_port_readl(port, 0x130); for (i = 0; i < ARRAY_SIZE(apple_pcie_s2r_dbi); i++) port->s2r_dbi[i] = readl_relaxed(dbi + apple_pcie_s2r_dbi[i]); From 6f80d31e4a1ba548beba268b31bc46bbb098a9b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 19:49:42 +0100 Subject: [PATCH 209/269] PCI: apple: bring the conventional ports up side by side after a resume from disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A resume from disk finds the conventional host's ports reset by the boot that loaded the image, and sets each one up again as probe did: PWREN, 100 ms before PERST# is released, 100 ms before the link, the link trained, 100 ms before the first configuration request. On the J616s that is 0.31 s per port, done one port after the other (0.62 s of the restore for the radio and the SD reader). The bring-up of a port touches only that port's registers and PHY, its own PERST# and PWREN GPIOs and its slots of the RID-to-SID table, which are written under the host's lock. Run the ports' bring-ups as async work and wait for all of them before the saved configurations of the functions are taken back, so that their waits overlap. Failures are reported per port as before. Hardware test: resume from disk; both ports report "link back, PWREN and PERST# cycled as at probe", Wi-Fi, Bluetooth and the SD reader work after the resume, and the device restore takes about 0.3 s less. Signed-off-by: František Balázsy --- drivers/pci/controller/pcie-apple.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 2c03507f6bb6b..4bb33b5623212 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -18,6 +18,7 @@ * Author: Marc Zyngier */ +#include #include #include #include @@ -636,6 +637,8 @@ struct apple_pcie_port { bool restore_link_up; /* ... and found down by the restore: the boot that resumed did not set it up */ bool restore_cold; + /* The result of setting it up again (apple_pcie_conventional_up()) */ + int restore_ret; }; /* A function's configuration, for its link back after a resume from disk. */ @@ -6921,12 +6924,25 @@ static int apple_pcie_prepare(struct device *dev) return apple_radio_prepare(dev); } +static void apple_pcie_restore_port_up_async(void *data, async_cookie_t cookie) +{ + struct apple_pcie_port *port = data; + + port->restore_ret = apple_pcie_restore_port_up(port->pcie, port); +} + /* * The normal phase, after the SMC's own restore (the PWREN GPIOs are its) and before the PCI * core's normal restore of the functions, which waits for this host as their ancestor. + * + * Each port's bring-up is mostly the waits of the PCIe timing (power to PERST#, PERST# to + * the link, the link to the first configuration request) and touches only that port's own + * registers, GPIOs and RID-to-SID table (under the host's lock): the ports are brought up + * side by side and their waits overlap. */ static void apple_pcie_conventional_up(struct apple_pcie *pcie, const char *what) { + ASYNC_DOMAIN_EXCLUSIVE(ports_up); struct device *dev = pcie->dev; struct apple_pcie_port *port; struct apple_pcie_restore_fn *fn; @@ -6944,7 +6960,14 @@ static void apple_pcie_conventional_up(struct apple_pcie *pcie, const char *what apple_pcie_restore_rid2sid(pcie, port); continue; } - ret = apple_pcie_restore_port_up(pcie, port); + async_schedule_domain(apple_pcie_restore_port_up_async, port, &ports_up); + } + async_synchronize_full_domain(&ports_up); + + list_for_each_entry(port, &pcie->ports, entry) { + if (!port->restore_link_up || !port->restore_cold) + continue; + ret = port->restore_ret; if (ret) { dev_err(dev, "%s: %pOF link not back (%d), its functions stay unreachable\n", what, port->np, ret); From f0617f963db37f90f932b8856e5c3fbb58c1ae40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 19:51:03 +0100 Subject: [PATCH 210/269] wifi: brcmfmac: pcie: restart behind the restore after a resume from disk too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A function that comes back from a firmware suspend without its firmware is removed and probed again from a work item, so that the resume of the devices behind it does not wait for that. The restore of a hibernation image finds the function in the same state (the boot that loaded the image reset it) but still removes and probes it in the restore callback: 0.61 s of the device restore on a J616s. Queue the restart for the restore of a loaded image as well. The PM notifier that flushes the queue before the next suspend, hibernation or restore already covers it. Hardware test: resume from disk; the device restore no longer shows brcmfmac in its slowest callbacks, Wi-Fi reconnects on its own after the resume, and a suspend to RAM right after it finds the function probed. tools/asahi: the suspend test models the restore of an image as well, and its queued restart. Signed-off-by: František Balázsy --- .../wireless/broadcom/brcm80211/brcmfmac/pcie.c | 10 ++++++---- tools/asahi/brcmf-suspend-tests.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 55454bb617547..b57310bc2008b 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -3615,11 +3615,13 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) devinfo->ci = NULL; pdev = devinfo->pdev; /* - * A function that lost its power in a firmware suspend starts over. Taking the - * interfaces and the wiphy down takes about 550 ms, and everything that follows - * this device in the resume waits for it, so do it right behind the resume. + * A function that lost its power in a firmware suspend, or in the boot that loaded a + * hibernation image, starts over. Taking the interfaces and the wiphy down and the + * firmware download take about 600 ms, and everything that follows this device in + * the resume waits for it, so do it right behind the resume. */ - if (pm_resume_via_firmware() && brcmf_pcie_restart_queue(pdev)) + if ((pm_resume_via_firmware() || pm_hibernate_is_restored()) && + brcmf_pcie_restart_queue(pdev)) return 0; brcmf_pcie_remove(pdev); diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index efef34ea7de04..a039c8ce27f27 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -180,8 +180,9 @@ def function(source, name): static void brcmf_pcie_sleep_finish(struct device *d) {(void)d;abort();} static void brcmf_bus_cancel_reset_work(struct brcmf_bus *b) {(void)b;abort();} static int brcmf_pcie_pm_leave_D3(struct device *dev); -static bool via_firmware,queue_fails;static unsigned int queued; +static bool via_firmware,image_restored,queue_fails;static unsigned int queued; static bool pm_resume_via_firmware(void) {return via_firmware;} +static bool pm_hibernate_is_restored(void) {return image_restored;} static bool brcmf_pcie_restart_queue(struct pci_dev *p) {(void)p;if(queue_fails)return false;queued++;return true;} ''' @@ -362,11 +363,15 @@ def function(source, name): probe_error=fail?-EIO:0;assert(brcmf_pcie_pm_leave_D3(&device)==probe_error); assert(removes==1 && probes==1 && ndev[0].stopped && msgbuf.tx_suspended);cases++; } - /* After a firmware suspend the cold function's restart is queued, or done here if it can't be. */ - for(int nomem=0;nomem<2;nomem++) { + /* + * After a firmware suspend or the restore of a hibernation image the cold function's restart + * is queued, or done here if it can't be. + */ + for(int image=0;image<2;image++)for(int nomem=0;nomem<2;nomem++) { reset();assert(!brcmf_pcie_pm_enter_D3(&device));retain=false; - via_firmware=true;queue_fails=nomem;queued=0;assert(!brcmf_pcie_pm_leave_D3(&device)); - assert(queued==!nomem && removes==nomem && probes==nomem);via_firmware=false;queue_fails=false;cases++; + via_firmware=!image;image_restored=image;queue_fails=nomem;queued=0;assert(!brcmf_pcie_pm_leave_D3(&device)); + assert(queued==!nomem && removes==nomem && probes==nomem); + via_firmware=image_restored=false;queue_fails=false;cases++; } printf("%u regulatory, TX queue, worker retention and PCIe PM controls passed\n",cases); return 0; From 151cfbb11ebd1c0d79a9f2d38c2f4048a86c8021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 19:53:37 +0100 Subject: [PATCH 211/269] wifi: brcmfmac: do not sleep 500 ms per interface in the suspend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A suspend without wake-on-WLAN disassociates every ready interface and then sleeps 500 ms per interface, "so that wpa_supplicant receives the events the DISASSOC generates". That sleep is paid on every suspend and hibernation (0.58 s of wiphy_suspend on a J616s with one interface). It delivers nothing: brcmf_link_down() already reports the disconnect to cfg80211, and so to wpa_supplicant, before it returns. Of the events the firmware may send for the DISASSOC, a DISASSOC completion has no handler, so it is not enabled in the firmware's event mask and is dropped on arrival, and a link down or deauthentication finds the interface no longer connected and is ignored. Traced on a BCM4387 (PCIe) across a suspend-to-idle: no firmware event reaches the host in the 500 ms after the DISASSOC. Drop the sleep. Hardware test: suspend-to-idle and suspend-to-RAM with Wi-Fi associated; wiphy_suspend takes a few milliseconds instead of 0.58 s, NetworkManager shows the disconnect before the sleep and Wi-Fi reassociates after the wake. Signed-off-by: František Balázsy --- drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c index b61a51dbbce1a..1311738f3ae6a 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c @@ -5274,11 +5274,6 @@ static s32 brcmf_cfg80211_suspend(struct wiphy *wiphy, * in suspended state */ brcmf_link_down(vif, WLAN_REASON_UNSPECIFIED, true); - /* Make sure WPA_Supplicant receives all the event - * generated due to DISASSOC call to the fw to keep - * the state fw and WPA_Supplicant state consistent - */ - brcmf_delay(500); } /* Configure MPC */ brcmf_set_mpc(ifp, 1); From 4dcdbe85aec439af6850e27ac34e44374a5e3b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Tue, 10 Nov 2026 16:34:04 +0100 Subject: [PATCH 212/269] thunderbolt, phy: apple, usb: typec: J616s USB4 and Type-C across sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit T6040 ACIO host router and CIO PHY stop/restart for sleep, ATC USB2/USB3 PHY restore, and the tipd SPMI controllers after a power loss. Signed-off-by: František Balázsy --- .../ABI/testing/sysfs-driver-sn201202x | 23 + drivers/phy/apple/t6040-usb2.c | 1370 +++++++++++++---- drivers/thunderbolt/apple-dpin-test.c | 3 + drivers/thunderbolt/apple.c | 283 +++- drivers/thunderbolt/nhi.h | 4 + drivers/thunderbolt/tb.c | 17 + drivers/usb/typec/tipd/core.c | 80 + drivers/usb/typec/tipd/spmi.c | 163 +- drivers/usb/typec/tipd/tps6598x.h | 8 + include/linux/phy/apple-t6040-cio.h | 67 + tools/asahi/tb-dp-repair-tests.py | 2 + .../selftests/drivers/gpu/apple-dcp/pm.py | 3 + 12 files changed, 1706 insertions(+), 317 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-sn201202x create mode 100644 include/linux/phy/apple-t6040-cio.h diff --git a/Documentation/ABI/testing/sysfs-driver-sn201202x b/Documentation/ABI/testing/sysfs-driver-sn201202x new file mode 100644 index 0000000000000..967f4abb0596f --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-sn201202x @@ -0,0 +1,23 @@ +What: /sys/bus/spmi/devices//hib_resume +Date: November 2026 +Contact: František Balázsy +Description: + (RW) State a resume from hibernation left an Apple SN201202x + USB Type-C port controller in. Reads one of: + + none + No resume from hibernation is pending for this port. + roles-pending + The controller was restored in place after the cold + boot behind the resume; the port is revalidated once + the USB controller that provides its role switch has + been bound again. + failed + The in-place restore failed; the controller has to be + probed again (unbind and bind the device). + + Writing "roles" while the state is "roles-pending" looks the + USB role switch up again and revalidates the port, without + taking the port, or a display on it, down. Other values fail + with -EINVAL, a write in another state with -EALREADY, and + -EAGAIN means the USB role switch is not registered yet. diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 50d82116aac2d..df861ecf317f4 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -48,11 +49,12 @@ #include #include #include +#include +#include #include #include #include #include -#include #define T6040_USB2_USBCTL 0x00 #define T6040_USB2_USBCTL_MODE GENMASK(2, 0) @@ -124,10 +126,8 @@ #define T6040_CORE_CMN_OVRD_SLEEP_B_BIG BIT(0) #define T6040_CORE_CMN_OVRD_SLEEP_B_SML BIT(2) #define T6040_CORE_CMN_OVRD_CLAMP BIT(4) -#define T6040_CORE_LANE_RX 0x10 /* CFG1_TOP */ +#define T6040_CORE_CFG1_TOP 0x10 /* ACIOPHY_TOP_BLK_CFG1_TOP: per-lane RX power */ #define T6040_CORE_SLP_CTRL 0x120 /* CFG_SLP_CTRL */ -#define T6040_CORE_LN0_RX_TOP_CIO 0x935c -#define T6040_CORE_LN1_RX_TOP_CIO 0x1935c #define T6040_CORE_CMN_BIAS 0xa00 /* AUS40CMN_SHM CMN_BIAS_REG */ #define T6040_CORE_CMN_BIAS_BGBIAS_EN_OV BIT(1) #define T6040_CORE_UC_CTRL 0x260 /* ACIOPHY_TOP_BLK_UC_CTRL_REG */ @@ -292,7 +292,6 @@ struct t6040_usb2_phy { bool usb3_pipe; /* dwc3 asked for the PIPE switch this power cycle */ bool usb3_lent; /* DP mode took the lanes of this Type-C port */ bool pipe_switched; /* the controller's PIPE runs on this PHY */ - bool cio_up; /* the lanes run USB4/Thunderbolt */ /* DisplayPort; dp_ok is false when the DT lacks a DP route */ struct phy *dp; @@ -305,6 +304,17 @@ struct t6040_usb2_phy { bool dp_mode_flip; bool dp_flip; /* plug orientation reversed for the DP lanes */ int dp_rate; /* link rate index of the running link sequence, -1 = off */ + + /* CIO (USB4/Thunderbolt); cio_ok is false unless the DT has the CIO tables */ + struct apple_tunable *cio_tunables[5]; + bool cio_ok; + bool cio_up; /* PHY in mode 2, holds a runtime PM reference */ + bool pipe_tunneled; /* the controller's PIPE runs on the USB4 router */ + u8 tun_rate[2]; /* DP tunnel link rate per DP IN adapter, 0 = off */ + bool tun_pll; /* AUSPLL programmed for DP tunnels */ + bool router_running; /* the USB4 router runs on this port's CIO link */ + bool usb3_tunnel_req; /* the router carries USB3 for the controller */ + bool cio_flip; }; static inline void t6040_usb2_mask32(void __iomem *reg, u32 mask, u32 set) @@ -677,19 +687,16 @@ static int t6040_usb3_cpu_start(struct t6040_usb2_phy *tphy) } /* - * C: runFirmware. macOS starts the coprocessor on every attach and stops it on - * every detach; each start makes the firmware tune the PHY. Then release the - * PHY reset (D). + * Part C of the PHY init from the delay after the lane swap on, for the modes + * that run the PHY firmware (USB3 and CIO): the firmware start, the tune-done + * poll and the reset release (D). @what and @lane only label the log. */ -static int t6040_usb3_run_firmware(struct t6040_usb2_phy *tphy, bool usb, bool swap) +static int t6040_phy_run_firmware(struct t6040_usb2_phy *tphy, const char *what, int lane) { void __iomem *cpu = t6040_cpu(tphy); u32 uc_before; int ret; - if (usb) - t6040_usb2_mask32(t6040_cfg(tphy) + T6040_CFG_SPARE0, T6040_CFG_SPARE0_USB_LN_SWAP, - swap ? T6040_CFG_SPARE0_USB_LN_SWAP : 0); fsleep(20); t6040_usb2_set32(cpu + T6040_CPU_IDLE_CTRL, T6040_CPU_IDLE_CTRL_EN); fsleep(10); @@ -698,27 +705,39 @@ static int t6040_usb3_run_firmware(struct t6040_usb2_phy *tphy, bool usb, bool s if (!tphy->rtk || !apple_rtkit_is_running(tphy->rtk)) { ret = t6040_usb3_cpu_start(tphy); if (ret) { - dev_err(tphy->dev, "USB3: the PHY coprocessor was not started: %d (run %#x, idle %#x, status %#x)\n", - ret, readl(cpu + T6040_CPU_RUN), + dev_err(tphy->dev, "%s: the PHY coprocessor was not started: %d (run %#x, idle %#x, status %#x)\n", + what, ret, readl(cpu + T6040_CPU_RUN), readl(cpu + T6040_CPU_IDLE_CTRL), readl(cpu + T6040_CPU_STATUS)); return ret; } } ret = t6040_usb3_wait_tuned(tphy); if (ret) { - dev_err(tphy->dev, "USB3: the PHY coprocessor did not tune the PHY (uc %#x -> %#x, idle %#x, status %#x)\n", - uc_before, readl(t6040_core(tphy) + T6040_CORE_UC_CTRL), + dev_err(tphy->dev, "%s: the PHY coprocessor did not tune the PHY (uc %#x -> %#x, idle %#x, status %#x)\n", + what, uc_before, readl(t6040_core(tphy) + T6040_CORE_UC_CTRL), readl(cpu + T6040_CPU_IDLE_CTRL), readl(cpu + T6040_CPU_STATUS)); return ret; } - dev_info(tphy->dev, "USB3 PHY tuned: lane %d, coprocessor started, uc %#x -> %#x\n", swap, - uc_before, readl(t6040_core(tphy) + T6040_CORE_UC_CTRL)); + dev_info(tphy->dev, "%s PHY tuned: lane %d, coprocessor started, uc %#x -> %#x\n", what, + lane, uc_before, readl(t6040_core(tphy) + T6040_CORE_UC_CTRL)); t6040_usb2_set32(t6040_cfg(tphy) + T6040_CFG_CTRL, T6040_CFG_CTRL_PHY_RESET_N); fsleep(100); return 0; } +/* + * C: the firmware start. The coprocessor is started on every attach and stopped on + * every detach; each start makes the firmware tune the PHY. Then release the + * PHY reset (D). + */ +static int t6040_usb3_run_firmware(struct t6040_usb2_phy *tphy, bool swap) +{ + t6040_usb2_mask32(t6040_cfg(tphy) + T6040_CFG_SPARE0, T6040_CFG_SPARE0_USB_LN_SWAP, + swap ? T6040_CFG_SPARE0_USB_LN_SWAP : 0); + return t6040_phy_run_firmware(tphy, "USB3", swap); +} + /* * Detach (RTKit power state 0): Power Down, RTKit AP then IOP to idle, * wait up to 10 ms for the CPU to idle, STOP. The RTKit transport stays stopped @@ -915,15 +934,15 @@ static void t6040_usb3_bist_off(struct t6040_usb2_phy *tphy) } /* - * aciophy_phy_shutdown_partA: the PHY's USB link side down (the same BIST - * handshake as the PIPE switch), both TX lanes, then the common block. + * Part A of the PHY shutdown for every firmware mode: both TX lanes + * powered down, then the common block shut down. */ -/* aciophy_tx_powerdown(0), (1), then aciophy_cmn_shutdown: USB and CIO alike. */ -static void t6040_phy_tx_cmn_down(struct t6040_usb2_phy *tphy) +static void t6040_phy_tx_cmn_shutdown(struct t6040_usb2_phy *tphy) { void __iomem *core = t6040_core(tphy); unsigned int lane; + /* The TX power-down of lane 0, then of lane 1 */ for (lane = 0; lane < 2; lane++) { t6040_usb2_set32(core + T6040_CORE_SLP_CTRL, 0x100 << lane); udelay(1); @@ -944,6 +963,10 @@ static void t6040_phy_tx_cmn_down(struct t6040_usb2_phy *tphy) udelay(1); } +/* + * Part A of the PHY shutdown: the PHY's USB link side down (the same BIST + * handshake as the PIPE switch), both TX lanes, then the common block. + */ static void t6040_usb3_shutdown_lanes(struct t6040_usb2_phy *tphy) { void __iomem *bist = t6040_bist(tphy); @@ -959,8 +982,8 @@ static void t6040_usb3_shutdown_lanes(struct t6040_usb2_phy *tphy) t6040_usb3_bist_wait(tphy, T6040_BIST_STATUS_PHY_2, true, 1000); t6040_usb2_clear32(bist + T6040_BIST_CFG0, BIT(0)); - t6040_phy_tx_cmn_down(tphy); - fsleep(200); + t6040_phy_tx_cmn_shutdown(tphy); + fsleep(200); /* the PHY shutdown waits 200 us after part A */ } static int t6040_usb3_pipe_lock(struct t6040_usb2_phy *tphy, unsigned int ms) @@ -1071,11 +1094,17 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) lockdep_assert_held(&tphy->lock); if (!tphy->usb3_ok || tphy->usb3_up) return; - /* DP and CIO own all four lanes; USB3 comes back in t6040_usb3_reclaim_locked(). */ - if (tphy->dp_up || tphy->cio_up) { + /* DP mode owns all four lanes; USB3 comes back in t6040_usb3_reclaim_locked(). */ + if (tphy->dp_up) { dev_dbg(tphy->dev, "USB3: lanes held by DP, USB2 only for now\n"); return; } + /* CIO owns both lanes; USB3 comes back in t6040_usb3_reclaim_locked() after it. */ + if (tphy->cio_up) { + dev_dbg(tphy->dev, "USB3: lanes held by CIO, USB2 only for now\n"); + tphy->usb3_lent = true; + return; + } switch (tphy->orientation) { case TYPEC_ORIENTATION_NORMAL: @@ -1105,7 +1134,7 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) goto sleep; /* the coprocessor block may be unclocked: never touch it */ t6040_usb3_tunables(tphy); t6040_usb3_lanes(tphy, swap); - ret = t6040_usb3_run_firmware(tphy, true, swap); + ret = t6040_usb3_run_firmware(tphy, swap); if (ret) goto retry_or_fail; @@ -1131,6 +1160,8 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) dev_warn(tphy->dev, "USB3 PHY bring-up failed (%d), USB2 only\n", ret); } +static void t6040_cio_pipe_update_locked(struct t6040_usb2_phy *tphy); + static int t6040_usb3_power_on(struct phy *phy) { struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); @@ -1148,6 +1179,11 @@ static void t6040_usb3_down_locked(struct t6040_usb2_phy *tphy) t6040_usb3_pipe_park(tphy); tphy->pipe_switched = false; } + /* The USB3 mode 0 from 3: the park from the USB4 router uses the same writes. */ + if (tphy->pipe_tunneled) { + t6040_usb3_pipe_park(tphy); + tphy->pipe_tunneled = false; + } /* DWC3 can drop its role before the DP fabric has released pin D. */ if (tphy->dp_up && tphy->dp_mode_two_lanes) return; @@ -1190,6 +1226,10 @@ static int t6040_usb3_set_mode(struct phy *phy, enum phy_mode mode, int submode) guard(mutex)(&tphy->lock); tphy->usb3_pipe = true; + if (tphy->cio_up) { + t6040_cio_pipe_update_locked(tphy); + return 0; + } if (!tphy->usb3_up || tphy->pipe_switched) return 0; t6040_usb3_pipe_switch(tphy); @@ -1615,176 +1655,6 @@ static void t6040_usb3_reclaim_locked(struct t6040_usb2_phy *tphy) dev_info(tphy->dev, "USB3 %s after DP\n", tphy->usb3_up ? "back" : "not restored"); } -/* ---- CIO: USB4 and Thunderbolt on both lanes ---- */ - -/* The ADT tables of the common set, then the CIO ones; no DPTX writes. */ -static void t6040_cio_tunables(struct t6040_usb2_phy *tphy) -{ - void __iomem *core = t6040_core(tphy); - size_t i; - - for (i = 0; i < T6040_USB3_NR_TUNABLES; i++) - apple_tunable_apply(tphy->regs[t6040_usb3_tunable_list[i].region], - tphy->tunables[i]); - /* tunable_LN0/1_RX_TOP_CIO_DFLT; UC_REGS_CIO and RX_EQ_CIO are empty. */ - t6040_usb2_clear32(core + T6040_CORE_LN0_RX_TOP_CIO, 0xe00); - t6040_usb2_clear32(core + T6040_CORE_LN1_RX_TOP_CIO, 0xe00); -} - -/* aciophy_xbar_config, mode 2: orientation is one crossbar bit. */ -static void t6040_cio_xbar(struct t6040_usb2_phy *tphy, bool flip) -{ - void __iomem *core = t6040_core(tphy); - - t6040_usb2_clear32(core + T6040_CORE_XBAR, 0xfc); - t6040_usb2_mask32(core + T6040_CORE_XBAR_LANES, 0x1f, flip ? 1 : 0); - t6040_usb2_mask32(core + T6040_CORE_XBAR, 0x3, 0x2); -} - -/* aciophy_slpctrl_init, mode 2: both lanes, whatever the orientation. */ -static void t6040_cio_lanes_on(struct t6040_usb2_phy *tphy) -{ - static const struct { u32 off, bit; bool set; } steps[] = { - { T6040_CORE_LANE_RX, 0x10, true }, { T6040_CORE_LANE_RX, 0x40, true }, - { T6040_CORE_LANE_RX, 0x1, true }, { T6040_CORE_LANE_RX, 0x4, true }, - { T6040_CORE_CMN_OVRD, 0x400, true }, { T6040_CORE_CMN_OVRD, 0x1000, true }, - { T6040_CORE_CMN_OVRD, 0x40, true }, { T6040_CORE_CMN_OVRD, 0x100, true }, - { T6040_CORE_CMN_OVRD, 0x4000, false }, { T6040_CORE_CMN_OVRD, 0x10000, true }, - { T6040_CORE_SLP_CTRL, 0x40, false }, { T6040_CORE_SLP_CTRL, 0x4, false }, - { T6040_CORE_SLP_CTRL, 0x400, false }, - }; - void __iomem *core = t6040_core(tphy); - unsigned int lane; - size_t i; - - t6040_usb2_clear32(core + T6040_CORE_CMN_OVRD, 0x2a); - udelay(1); - for (lane = 0; lane < 2; lane++) { - for (i = 0; i < ARRAY_SIZE(steps); i++) { - if (steps[i].set) - t6040_usb2_set32(core + steps[i].off, steps[i].bit << lane); - else - t6040_usb2_clear32(core + steps[i].off, steps[i].bit << lane); - udelay(1); - } - } -} - -/* aciophy_rx_powerdown(0), (1): CIO powers the receivers down, USB does not. */ -static void t6040_cio_rx_down(struct t6040_usb2_phy *tphy) -{ - void __iomem *core = t6040_core(tphy); - unsigned int lane; - - for (lane = 0; lane < 2; lane++) { - t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x4000 << lane); - udelay(1); - t6040_usb2_clear32(core + T6040_CORE_CMN_OVRD, 0x40 << lane); - udelay(1); - t6040_usb2_clear32(core + T6040_CORE_CMN_OVRD, 0x400 << lane); - udelay(1); - t6040_usb2_clear32(core + T6040_CORE_LANE_RX, 0x1 << lane); - udelay(1); - t6040_usb2_clear32(core + T6040_CORE_LANE_RX, 0x10 << lane); - udelay(1); - } -} - -/* The Type-C state that needs CIO: USB4 entered, or the Thunderbolt alt mode. */ -static bool t6040_cio_state(const struct typec_mux_state *state) -{ - if (state->alt) - return state->alt->svid == USB_TYPEC_TBT_SID; - if (state->mode != TYPEC_MODE_USB4 || !state->data) - return false; - return FIELD_GET(EUDO_USB_MODE_MASK, - ((const struct enter_usb_data *)state->data)->eudo) == EUDO_USB_MODE_USB4; -} - -static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) -{ - bool cycle; - bool flip; - int ret; - - lockdep_assert_held(&tphy->lock); - if (!tphy->usb3_ok || tphy->dp_up) - return -EBUSY; - switch (tphy->orientation) { - case TYPEC_ORIENTATION_NORMAL: - flip = false; - break; - case TYPEC_ORIENTATION_REVERSE: - flip = true; - break; - default: - dev_warn(tphy->dev, "CIO: no plug orientation\n"); - return -EINVAL; - } - if (tphy->usb3_up || tphy->pipe_switched) - t6040_usb3_down_locked(tphy); - tphy->usb3_lent = tphy->usb3_on; - - ret = pm_runtime_resume_and_get(tphy->dev); - if (ret) - return ret; - - cycle = t6040_recovery_start(&tphy->recovery); -retry: - if (cycle) { - ret = t6040_usb3_hard_recover(tphy); - if (ret) - goto put; - } - - ret = t6040_usb3_wake(tphy); - if (ret) - goto sleep; /* the coprocessor block may be unclocked: never touch it */ - t6040_cio_tunables(tphy); - t6040_cio_xbar(tphy, flip); - t6040_cio_lanes_on(tphy); - ret = t6040_usb3_run_firmware(tphy, false, flip); - if (ret) { - t6040_phy_tx_cmn_down(tphy); - t6040_cio_rx_down(tphy); - fsleep(200); - t6040_usb3_firmware_power_down(tphy); - t6040_usb3_sleep(tphy); - if (t6040_recovery_retry(&tphy->recovery)) { - cycle = true; - goto retry; - } - goto put; - } - t6040_recovery_complete(&tphy->recovery); - tphy->cio_up = true; - dev_info(tphy->dev, "CIO PHY up, %s orientation\n", flip ? "reverse" : "normal"); - return 0; - -sleep: - t6040_usb3_sleep(tphy); -put: - pm_runtime_put(tphy->dev); - dev_warn(tphy->dev, "CIO PHY bring-up failed: %d\n", ret); - return ret; -} - -static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) -{ - lockdep_assert_held(&tphy->lock); - if (!tphy->cio_up) - return; - t6040_phy_tx_cmn_down(tphy); - t6040_cio_rx_down(tphy); - fsleep(200); - t6040_usb3_firmware_power_down(tphy); - t6040_usb3_sleep(tphy); - tphy->cio_up = false; - pm_runtime_put(tphy->dev); - dev_info(tphy->dev, "CIO PHY asleep\n"); - t6040_usb3_reclaim_locked(tphy); -} - /* ---- DP phy_ops ---- */ static int t6040_dp_power_on(struct phy *phy) @@ -1795,6 +1665,7 @@ static int t6040_dp_power_on(struct phy *phy) guard(mutex)(&tphy->lock); if (!tphy->dp_ok || tphy->dp_up) return 0; + /* The port carries USB4/Thunderbolt: DP reaches it only as a tunnel. */ if (tphy->cio_up) return -EBUSY; /* Pin D shares the running PHY with USB3. Keep its PIPE and firmware. */ @@ -1853,7 +1724,7 @@ static int t6040_dp_power_off(struct phy *phy) * firmware for the first time over the DP state crashes it. */ if (tphy->dp_up && !tphy->dp_mode_two_lanes && !t6040_dp_only(tphy)) { - t6040_phy_tx_cmn_down(tphy); + t6040_phy_tx_cmn_shutdown(tphy); t6040_usb3_sleep(tphy); dev_info(tphy->dev, "DP PHY down, PHY asleep\n"); } @@ -1949,124 +1820,781 @@ static const struct phy_ops t6040_dp_phy_ops = { .set_mode = t6040_dp_set_mode, }; -/* ---- Type-C mode switch: the Type-C controller asks for DP on a USB-C port ---- */ - -static int t6040_dp_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state) -{ - struct t6040_usb2_phy *tphy = typec_mux_get_drvdata(mux); - bool dp = state->alt && state->alt->svid == USB_TYPEC_DP_SID; - bool alt = false; +/* ---- DP tunnels: the pixel clocks of the router's DP IN adapters (PHY in CIO) ---- + * + * The DP 1.4 tunnel's init, update and shutdown and the DP clock + * configuration, as the platform sequences them: one fixed + * AUSPLL frequency feeds the DPTX pixel clocks, whose select field divides it + * per link rate. DP IN adapter 0 is taken to use PCLK1 and clock switch 1, + * adapter 1 PCLK2 and switch 2 (the PHY does not choose; the platform gets the index + * from the display controller). Driven only by appledrm through + * apple_atc_dp_tunnel_rate(), and only while the PHY is in CIO. + */ +#define T6040_CORE_RCAL1 0x804 /* AUS40CMN_DIG RCAL1 */ +#define T6040_CORE_RCAL1_DONE BIT(0) +#define T6040_CORE_AUSPLL_CMD 0x3000 /* AUSPLL_TOP_APB_CMD_CMDOVERRIDE */ +#define T6040_CORE_AUSPLL_CMD_REQ BIT(0) +#define T6040_CORE_AUSPLL_CMD_ACK BIT(1) +#define T6040_CORE_AUSPLL_FREQDESC_A 0x3080 +#define T6040_CORE_AUSPLL_FREQDESC_B 0x3084 +#define T6040_CORE_AUSPLL_FREQDESC_C 0x3088 +#define T6040_CORE_AUSPLL_CLKOUT_MASTER 0x3200 +#define T6040_CORE_AUSPLL_CLKOUT_DIV 0x3208 +#define T6040_CORE_AUSPLL_BGR 0x3218 +#define T6040_CORE_AUSPLL_FREQINIT_1 0x3234 +#define T6040_CORE_TX_DP_CTRL0 0x7000 +#define T6040_CORE_TX_DP_CTRL0_ENABLES GENMASK(18, 16) +#define T6040_CORE_DP_PCLK_STATUS 0x7034 +#define T6040_CORE_DP_PCLK_STATUS_LOCK BIT(3) /* AUSPLL_LOCK */ + +static const struct t6040_tun_pclk { + u8 sel_shift; /* TX_DP_CTRL0 select field */ + u32 en; /* TX_DP_CTRL0 enable */ + u32 sw_mask, sw_gate, sw_on; /* clock switch: cleared, set, status when on */ + u32 req[2], req_stat[2], sw_off; /* PMGR requests, their status, cleared at off */ +} t6040_tun_pclk[2] = { + { 4, BIT(16), 0x473, 0x10, BIT(0), { 0x20, 0x40 }, { BIT(1), BIT(2) }, 0x403 }, + { 8, BIT(17), 0xb8c, 0x80, BIT(3), { 0x100, 0x200 }, { BIT(4), BIT(5) }, 0x80c }, +}; - guard(mutex)(&tphy->lock); - if (t6040_cio_state(state)) { - if (tphy->cio_up) - return 0; - return t6040_cio_up_locked(tphy); - } - t6040_cio_down_locked(tphy); - tphy->dp_flip = (tphy->orientation == TYPEC_ORIENTATION_REVERSE); - /* - * Latch the DP alt mode state for the DCP's Type-C route. Pin - * assignments C and E put DP on all four lanes. D leaves the USB3 - * pair with its running coprocessor and gives DP the other pair. - * The DCP brings the DP lanes up through phy_power_on()/phy_configure(). - */ - if (dp) { - switch (state->mode) { - case TYPEC_DP_STATE_C: - case TYPEC_DP_STATE_E: - alt = true; - break; - case TYPEC_DP_STATE_D: - alt = true; - break; - default: - break; - } +/* The pixel clock select: RBR, HBR, HBR2, HBR3; UHBR is not tunnelled on this path. */ +static int t6040_tun_select(u8 rate) +{ + switch (rate) { + case 0x06: + return 4; + case 0x0a: + return 3; + case 0x14: + return 1; + case 0x1e: + return 0; + default: + return -EINVAL; } - tphy->dp_alt = alt; - tphy->dp_two_lanes = alt && state->mode == TYPEC_DP_STATE_D; - dev_dbg(tphy->dev, "Type-C mux: %s state %lu -> DP alt mode %d, lane map %s\n", - dp ? "DP" : "non-DP", state->mode, alt, tphy->dp_flip ? "reversed" : "normal"); - if (alt && tphy->dp_up && !tphy->dp_mode_two_lanes && !tphy->dp_two_lanes) - t6040_dp_xbar_config(tphy, tphy->dp_flip); - t6040_usb3_reclaim_locked(tphy); - return 0; } -static void t6040_dp_mux_unregister(void *mux) +static u32 t6040_tun_enables(struct t6040_usb2_phy *tphy) { - typec_mux_unregister(mux); + return (tphy->tun_rate[0] ? t6040_tun_pclk[0].en : 0) | + (tphy->tun_rate[1] ? t6040_tun_pclk[1].en : 0); } -/* - * Everything the DP route needs from the device tree; a node without it gets - * no DP phy. A USB-C PHY offers DP through a Type-C mode switch; a fixed DP - * route (an HDMI converter's PHY) is driven straight by the DCP with no mux - * and uses the normal lane map. - */ -static int t6040_dp_probe(struct t6040_usb2_phy *tphy) +/* AUSPLL_TOP command override: request @req with the toggle protocol, then wait for @ack. */ +static int t6040_tun_pll_cmd(struct t6040_usb2_phy *tphy, u32 val, bool ack_set) { - struct device *dev = tphy->dev; - unsigned int i; - - tphy->dp_rate = -1; + void __iomem *cmd = t6040_core(tphy) + T6040_CORE_AUSPLL_CMD; + u32 v; - /* - * DP mode needs the "core" and "cfg" windows. A USB-C PHY has them - * alongside its USB regions; a fixed DP PHY has them without any - * pipehandler or USB3 coprocessor. A node with neither is USB2 only. - */ - if (!tphy->regs[T6040_USB3_CORE] || !tphy->regs[T6040_USB3_CFG]) - return 0; + writel((readl(cmd) & 0xe0000006) | val, cmd); + return readl_poll_timeout(cmd, v, !!(v & T6040_CORE_AUSPLL_CMD_ACK) == ack_set, + 10, 500 * USEC_PER_MSEC); +} - tphy->dp = devm_phy_create(dev, NULL, &t6040_dp_phy_ops); - if (IS_ERR(tphy->dp)) - return PTR_ERR(tphy->dp); - phy_set_drvdata(tphy->dp, tphy); +/* The DP 1.4 tunnel shutdown: pixel clocks off, AUSPLL released. */ +static void t6040_tun_pll_stop(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + bool ack = readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_ACK; + u32 v; - /* DP-only node: the generic apply_tunables tables (absent = not applied). */ - if (t6040_dp_only(tphy)) { - for (i = 0; i < T6040_DP_NR_GENERIC_TUNABLES; i++) { - const char *prop = t6040_usb3_tunable_list[i].prop; + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, T6040_CORE_TX_DP_CTRL0_ENABLES); + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, 0xc); + if (t6040_tun_pll_cmd(tphy, (ack ? 0 : T6040_CORE_AUSPLL_CMD_REQ) | 0x10000000, !ack) || + t6040_tun_pll_cmd(tphy, (ack ? T6040_CORE_AUSPLL_CMD_REQ : 0) | 0x10000018, ack) || + readl_poll_timeout(core + T6040_CORE_DP_PCLK_STATUS, v, + !(v & T6040_CORE_DP_PCLK_STATUS_LOCK), 10, 500 * USEC_PER_MSEC)) + dev_warn(tphy->dev, "DP tunnel: AUSPLL release timed out (cmd %#x, pclk status %#x)\n", + readl(core + T6040_CORE_AUSPLL_CMD), + readl(core + T6040_CORE_DP_PCLK_STATUS)); + t6040_usb2_clear32(core + T6040_CORE_AUSPLL_CLKOUT_MASTER, 0x8); + tphy->tun_pll = false; +} - if (!of_property_present(dev->of_node, prop) || - !tphy->res[t6040_usb3_tunable_list[i].region]) { - dev_warn(dev, "DP: no %s\n", prop); - tphy->tunables[i] = NULL; - continue; - } - struct resource *res = tphy->res[t6040_usb3_tunable_list[i].region]; +/* The common block's init, then the fixed AUSPLL program of the DP 1.4 tunnel init. */ +static int t6040_tun_pll_start(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + bool ack; + u32 v; - tphy->tunables[i] = devm_apple_tunable_parse(dev, dev->of_node, prop, res); - if (IS_ERR(tphy->tunables[i])) { - dev_warn(dev, "DP: %s: %pe\n", prop, tphy->tunables[i]); - tphy->tunables[i] = NULL; - } - } + if ((readl(core + T6040_CORE_AUSPLL_CLKOUT_MASTER) & 0x808) || + (readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_REQ)) { + dev_warn(tphy->dev, "DP tunnel: AUSPLL already in use (master %#x, cmd %#x)\n", + readl(core + T6040_CORE_AUSPLL_CLKOUT_MASTER), + readl(core + T6040_CORE_AUSPLL_CMD)); + return -EBUSY; } + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, T6040_CORE_TX_DP_CTRL0_ENABLES); - if (of_property_present(dev->of_node, "mode-switch")) { - struct typec_mux_desc mux_desc = { - .drvdata = tphy, - .fwnode = dev_fwnode(dev), - .set = t6040_dp_mux_set, - }; - - tphy->mux = typec_mux_register(dev, &mux_desc); - if (IS_ERR(tphy->mux)) - return PTR_ERR(tphy->mux); - if (devm_add_action_or_reset(dev, t6040_dp_mux_unregister, tphy->mux)) - return -ENOMEM; + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x4); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x8); + udelay(1); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x1); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x2); + udelay(1); + t6040_usb2_clear32(core + T6040_CORE_CMN_OVRD, T6040_CORE_CMN_OVRD_CLAMP); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x20); + udelay(1); + t6040_usb2_set32(core + T6040_CORE_CMN_BIAS, T6040_CORE_CMN_BIAS_BGBIAS_EN_OV); + udelay(15); + if (readl_poll_timeout(core + T6040_CORE_RCAL1, v, v & T6040_CORE_RCAL1_DONE, 1, 100)) + dev_dbg(tphy->dev, "DP tunnel: RCAL not done (%#x)\n", v); + + t6040_usb2_clear32(core + T6040_CORE_AUSPLL_FREQINIT_1, 0x3); + writel(0x1e0e021c, core + T6040_CORE_AUSPLL_FREQDESC_A); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_FREQDESC_B, 0x0fffffff, 0); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_FREQDESC_C, 0x007fffff, 0x644a00); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_CLKOUT_DIV, 0x001f0000, 0x10000); + t6040_usb2_set32(core + T6040_CORE_AUSPLL_BGR, 0x1); + t6040_usb2_set32(core + T6040_CORE_AUSPLL_CLKOUT_MASTER, 0x808); + tphy->tun_pll = true; + + ack = readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_ACK; + if (t6040_tun_pll_cmd(tphy, (ack ? 0 : T6040_CORE_AUSPLL_CMD_REQ) | 0x10000000, true) || + readl_poll_timeout(core + T6040_CORE_DP_PCLK_STATUS, v, + !!(v & T6040_CORE_DP_PCLK_STATUS_LOCK) == !ack, + 10, 500 * USEC_PER_MSEC) || + t6040_tun_pll_cmd(tphy, (ack ? T6040_CORE_AUSPLL_CMD_REQ : 0) | 0x10010000, ack)) { + dev_err(tphy->dev, "DP tunnel: AUSPLL did not lock (cmd %#x, pclk status %#x)\n", + readl(core + T6040_CORE_AUSPLL_CMD), + readl(core + T6040_CORE_DP_PCLK_STATUS)); + t6040_tun_pll_stop(tphy); + return -ETIMEDOUT; } - - tphy->dp_ok = true; + t6040_usb2_set32(core + T6040_CORE_TX_DP_CTRL0, 0xc); return 0; } -/* ---- the coprocessor's RTKit ---- */ +/* The DP 1.4 tunnel update for one pclk: select with its enable off, then the enables. */ +static void t6040_tun_select_write(struct t6040_usb2_phy *tphy, unsigned int dpin, int sel) +{ + const struct t6040_tun_pclk *p = &t6040_tun_pclk[dpin]; + void __iomem *ctrl0 = t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0; + u32 v = readl(ctrl0); -/* The coprocessor places its buffers in its own __DATA SRAM. */ + if (!!(v & p->en) != !!tphy->tun_rate[dpin] || + ((v >> p->sel_shift) & 0xf) != sel) { + v = (v & ~(p->en | (0xf << p->sel_shift))) | (sel << p->sel_shift); + writel(v, ctrl0); + } + writel((v & ~T6040_CORE_TX_DP_CTRL0_ENABLES) | t6040_tun_enables(tphy), ctrl0); +} + +/* The DP clock configuration for one DP 1.x switch: pixel-clock gate on, or off. */ +static int t6040_tun_switch(struct t6040_usb2_phy *tphy, unsigned int dpin, bool on) +{ + const struct t6040_tun_pclk *p = &t6040_tun_pclk[dpin]; + void __iomem *cfg = t6040_cfg(tphy); + unsigned int i; + u32 s; + + if (on) { + t6040_usb2_mask32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_mask, p->sw_gate); + return readl_poll_timeout(cfg + T6040_CFG_DP_CLKSWITCH_STAT, s, s & p->sw_on, + 10, 5 * USEC_PER_MSEC); + } + t6040_usb2_set32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_gate); + for (i = 0; i < 2; i++) { + if (!(readl(cfg + T6040_CFG_DP_CLKSWITCH_CTRL) & p->req[i])) + continue; + t6040_usb2_clear32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->req[i]); + if (readl_poll_timeout(cfg + T6040_CFG_DP_CLKSWITCH_STAT, s, !(s & p->req_stat[i]), + 10, 5 * USEC_PER_MSEC)) + dev_dbg(tphy->dev, "DP tunnel %u: request %#x not released (%#x)\n", + dpin, p->req[i], s); + } + t6040_usb2_clear32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_off); + return 0; +} + +/* All tunnel clocks down, as the lane configuration does before the ACIO shutdown. */ +static void t6040_tun_stop_all_locked(struct t6040_usb2_phy *tphy) +{ + unsigned int dpin; + + lockdep_assert_held(&tphy->lock); + if (!tphy->tun_pll) + return; + for (dpin = 0; dpin < 2; dpin++) { + if (!tphy->tun_rate[dpin]) + continue; + tphy->tun_rate[dpin] = 0; + t6040_tun_switch(tphy, dpin, false); + } + t6040_tun_pll_stop(tphy); + dev_info(tphy->dev, "DP tunnel clocks off\n"); +} + +static int t6040_tun_rate_locked(struct t6040_usb2_phy *tphy, unsigned int dpin, u8 rate) +{ + int sel, ret; + u8 old = tphy->tun_rate[dpin]; + + lockdep_assert_held(&tphy->lock); + if (!rate) { + if (!old) + return 0; + tphy->tun_rate[dpin] = 0; + t6040_tun_switch(tphy, dpin, false); + t6040_usb2_clear32(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0, + t6040_tun_pclk[dpin].en); + if (!tphy->tun_rate[!dpin]) + t6040_tun_pll_stop(tphy); + dev_dbg(tphy->dev, "DP tunnel %u off\n", dpin); + return 0; + } + sel = t6040_tun_select(rate); + if (sel < 0) + return sel; + if (!tphy->cio_up) + return -EBUSY; + if (old == rate) + return 0; + if (!tphy->tun_pll) { + ret = t6040_tun_pll_start(tphy); + if (ret) + return ret; + } + tphy->tun_rate[dpin] = rate; + t6040_tun_select_write(tphy, dpin, sel); + ret = t6040_tun_switch(tphy, dpin, true); + if (ret) { + dev_err(tphy->dev, "DP tunnel %u: clock switch timed out (ctrl %#x status %#x)\n", + dpin, readl(t6040_cfg(tphy) + T6040_CFG_DP_CLKSWITCH_CTRL), + readl(t6040_cfg(tphy) + T6040_CFG_DP_CLKSWITCH_STAT)); + t6040_tun_rate_locked(tphy, dpin, 0); + return ret; + } + dev_info(tphy->dev, "DP tunnel %u: rate %#x, select %d (ctrl0 %#x, pclk status %#x)\n", + dpin, rate, sel, readl(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0), + readl(t6040_core(tphy) + T6040_CORE_DP_PCLK_STATUS)); + return 0; +} + +/* + * The dp-tunnel.h provider for appledrm, which looks it up with symbol_get(). + * phy-apple-atc exports the same names for the older SoCs; a kernel that + * builds both gets them from there (and T6040 tunnels then need appledrm to + * pick a T6040 provider instead). + */ +#if !IS_ENABLED(CONFIG_PHY_APPLE_ATC) +int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) +{ + struct t6040_usb2_phy *tphy; + + if (!phy || phy->ops != &t6040_dp_phy_ops || dpin > 1) + return -EINVAL; + tphy = phy_get_drvdata(phy); + guard(mutex)(&tphy->lock); + return t6040_tun_rate_locked(tphy, dpin, rate); +} +EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_rate); + +/* The rate call does all of it, as on every SoC but t600x. */ +int apple_atc_dp_tunnel_open(struct phy *phy) +{ + if (!phy || phy->ops != &t6040_dp_phy_ops) + return -EINVAL; + return -EOPNOTSUPP; +} +EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_open); +#endif + +/* ---- CIO: USB4/Thunderbolt (PHY mode 2) ---- + * + * The PHY init for mode 2 with the orientation: the power-up reset (shared + * with USB3), the generic ADT tables, the CIO core tunables (the same + * three core tables as USB3) and the CIO-only tunables (the CIO lane + * tables, applied in every CIO session: nothing on the PHY side picks 40G or + * 80G), the mode-2 crossbar, the mode-2 sleep-control init (common and both + * lanes, receivers powered by override), then part C: the PHY coprocessor + * firmware tunes the PHY as for USB3, without the USB lane-swap bit. The + * firmware's only message, Power Down, is sent at exit as for + * USB3. The Type-C controller sets the mux to USB4/TBT after the orientation + * and before it hands the cable to the host router (drivers/thunderbolt/apple.c), + * and back to SAFE after the router has stopped; the PHY holds a runtime PM + * reference in between. CIO is offered only with the CIO tables in the DT; + * without them every mux state takes the path it took before. + * + * USB3 runs tunnelled through the router: the controller's PIPE goes to the + * USB4 backend (the USB3 mode 3, see t6040_cio_pipe_tunnel) + * while the PHY is in CIO, only with the cio_usb3_tunnel parameter set. + */ + +static bool cio_usb3_tunnel; +module_param(cio_usb3_tunnel, bool, 0644); +MODULE_PARM_DESC(cio_usb3_tunnel, + "Route the USB3 PIPE to a running USB4 router without its request (testing)"); + +/* The ADT tables of the CIO-only tunables, in order; lane0/1 are required. */ +static const char * const t6040_cio_tunable_props[] = { + "apple,tunable-cio-uc-regs", /* tunable_UC_REGS_CIO_DFLT, core + 0x400 */ + "apple,tunable-cio-lane0", /* tunable_LN0_RX_TOP_CIO_DFLT, core + 0x9000 */ + "apple,tunable-cio-lane1", /* tunable_LN1_RX_TOP_CIO_DFLT, core + 0x19000 */ + "apple,tunable-cio-lane0-eq", /* tunable_LN0_RX_EQ_CIO_DFLT, core + 0xb000 */ + "apple,tunable-cio-lane1-eq", /* tunable_LN1_RX_EQ_CIO_DFLT, core + 0x1b000 */ +}; + +static_assert(ARRAY_SIZE(t6040_cio_tunable_props) == + ARRAY_SIZE(((struct t6040_usb2_phy *)NULL)->cio_tunables)); + +/* The PIPE backend of USB3 tunnelled through the USB4 router */ +#define T6040_PIPEHANDLER_MUX_CLK_USB4 2 +#define T6040_PIPEHANDLER_MUX_DATA_USB4 1 + +/* The tunables of mode 2: generic tables, the CIO core tunables, the CIO-only tunables. */ +static void t6040_cio_tunables(struct t6040_usb2_phy *tphy) +{ + size_t i; + + for (i = 0; i < T6040_USB3_NR_TUNABLES; i++) + apple_tunable_apply(tphy->regs[t6040_usb3_tunable_list[i].region], + tphy->tunables[i]); + for (i = 0; i < ARRAY_SIZE(tphy->cio_tunables); i++) + apple_tunable_apply(t6040_core(tphy), tphy->cio_tunables[i]); +} + +/* The mode-2 crossbar: lane fields of core+0x6c cleared, core+0x78[4:0] = flip, then [1:0] = 2. */ +static void t6040_cio_xbar_config(struct t6040_usb2_phy *tphy, bool flip) +{ + void __iomem *core = t6040_core(tphy); + + t6040_usb2_mask32(core + T6040_CORE_XBAR, ~0xffffff03u, 0); + t6040_usb2_mask32(core + T6040_CORE_XBAR_LANES, 0x1f, flip ? 1 : 0); + t6040_usb2_mask32(core + T6040_CORE_XBAR, 0x3, 0x2); +} + +struct t6040_cio_step { + u16 off; + u32 bit; /* lane 0; lane 1 is the next bit up */ + bool set; +}; + +static void t6040_cio_steps(struct t6040_usb2_phy *tphy, const struct t6040_cio_step *step, + size_t n, unsigned int lane) +{ + size_t i; + + for (i = 0; i < n; i++) { + t6040_usb2_mask32(t6040_core(tphy) + step[i].off, step[i].bit << lane, + step[i].set ? step[i].bit << lane : 0); + udelay(1); + } +} + +/* RX power-up, per lane: the lane's receiver powered by override (CFG1_TOP, CFG0_TOP). */ +static const struct t6040_cio_step t6040_cio_pwr_up_rxa[] = { + { T6040_CORE_CFG1_TOP, 0x10, true }, { T6040_CORE_CFG1_TOP, 0x40, true }, + { T6040_CORE_CFG1_TOP, 0x1, true }, { T6040_CORE_CFG1_TOP, 0x4, true }, + { T6040_CORE_CMN_OVRD, 0x400, true }, { T6040_CORE_CMN_OVRD, 0x1000, true }, + { T6040_CORE_CMN_OVRD, 0x40, true }, { T6040_CORE_CMN_OVRD, 0x100, true }, + { T6040_CORE_CMN_OVRD, 0x4000, false }, { T6040_CORE_CMN_OVRD, 0x10000, true }, +}; + +/* TX override removal, per lane: the transmitter's overrides released (CFG_SLP_CTRL). */ +static const struct t6040_cio_step t6040_cio_tx_rm_ovrd[] = { + { T6040_CORE_SLP_CTRL, 0x40, false }, { T6040_CORE_SLP_CTRL, 0x4, false }, + { T6040_CORE_SLP_CTRL, 0x400, false }, +}; + +/* RX power-down, per lane */ +static const struct t6040_cio_step t6040_cio_rx_powerdown[] = { + { T6040_CORE_CMN_OVRD, 0x4000, true }, { T6040_CORE_CMN_OVRD, 0x40, false }, + { T6040_CORE_CMN_OVRD, 0x400, false }, { T6040_CORE_CFG1_TOP, 0x1, false }, + { T6040_CORE_CFG1_TOP, 0x10, false }, +}; + +/* + * The mode-2 sleep-control init: the common block's overrides removed, then + * the RX power-up and the TX override removal per lane. + */ +static void t6040_cio_slpctrl(struct t6040_usb2_phy *tphy) +{ + unsigned int lane; + + t6040_usb2_clear32(t6040_core(tphy) + T6040_CORE_CMN_OVRD, 0x2a); + udelay(1); + for (lane = 0; lane < 2; lane++) { + t6040_cio_steps(tphy, t6040_cio_pwr_up_rxa, ARRAY_SIZE(t6040_cio_pwr_up_rxa), lane); + t6040_cio_steps(tphy, t6040_cio_tx_rm_ovrd, ARRAY_SIZE(t6040_cio_tx_rm_ovrd), lane); + } +} + +/* + * The USB3 mode 3 from 0: the PIPE from the dummy backend to the USB4 + * router, with the lock handshake only while the controller is out of reset. + * No non-selected override and no BIST bracket, unlike the PHY's own USB3. + */ +static void t6040_cio_pipe_tunnel(struct t6040_usb2_phy *tphy) +{ + void __iomem *ph = tphy->pipehandler; + bool running = readl(ph + T6040_PIPEHANDLER_AON_GEN) & + T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N; + + if (readl(ph + T6040_PIPEHANDLER_LOCK_ACK) & T6040_PIPEHANDLER_LOCK_EN) + dev_dbg(tphy->dev, "CIO: PIPE lock already acknowledged\n"); + t6040_usb2_clear32(ph + T6040_PIPEHANDLER_OVERRIDE_VALUES, 0xe); + t6040_usb2_set32(ph + T6040_PIPEHANDLER_OVERRIDE, T6040_PIPEHANDLER_OVERRIDE_EN); + if (running && t6040_usb3_pipe_lock(tphy, 6)) + dev_dbg(tphy->dev, "CIO: PIPE lock not acknowledged, switching anyway\n"); + t6040_usb3_pipe_mux(tphy, T6040_PIPEHANDLER_MUX_CLK_USB4, + T6040_PIPEHANDLER_MUX_DATA_USB4); + t6040_usb2_clear32(ph + T6040_PIPEHANDLER_OVERRIDE, T6040_PIPEHANDLER_OVERRIDE_EN); + t6040_usb2_clear32(ph + T6040_PIPEHANDLER_OVERRIDE, T6040_PIPEHANDLER_OVERRIDE_EN2); + if (running) + t6040_usb3_pipe_unlock(tphy); + tphy->pipe_tunneled = true; + dev_info(tphy->dev, "USB3 PIPE on the USB4 router\n"); +} + +/* + * The PIPE is on the router while the router runs and carries USB3 for the + * controller (the platform asks for USB3 mode 3 only for a tunnelled USB3 + * transport, once it uses the tunnel), dwc3 holds the USB3 phy with its PIPE + * and the controller is out of reset. Otherwise it goes back to the dummy. + */ +static void t6040_cio_pipe_update_locked(struct t6040_usb2_phy *tphy) +{ + bool want; + + lockdep_assert_held(&tphy->lock); + want = tphy->cio_up && tphy->router_running && + (tphy->usb3_tunnel_req || cio_usb3_tunnel) && + tphy->usb3_on && tphy->usb3_pipe && tphy->dwc3_running && !tphy->pipe_switched; + if (want && !tphy->pipe_tunneled) { + t6040_cio_pipe_tunnel(tphy); + } else if (!want && tphy->pipe_tunneled) { + t6040_usb3_pipe_park(tphy); + tphy->pipe_tunneled = false; + dev_info(tphy->dev, "USB3 PIPE back on the dummy PHY\n"); + } +} + +/* The PHY shutdown of mode 2: part A, the coprocessor down, part B. */ +static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) +{ + u32 uc; + unsigned int lane; + + lockdep_assert_held(&tphy->lock); + if (!tphy->cio_up) + return; + t6040_tun_stop_all_locked(tphy); + /* The USB3 mode 0 from 3: the same writes as the park from 2 */ + if (tphy->pipe_tunneled) { + t6040_usb3_pipe_park(tphy); + tphy->pipe_tunneled = false; + } + uc = readl(t6040_core(tphy) + T6040_CORE_UC_CTRL); + t6040_phy_tx_cmn_shutdown(tphy); + for (lane = 0; lane < 2; lane++) + t6040_cio_steps(tphy, t6040_cio_rx_powerdown, ARRAY_SIZE(t6040_cio_rx_powerdown), + lane); + fsleep(200); + t6040_usb3_firmware_power_down(tphy); + t6040_usb3_sleep(tphy); + dev_info(tphy->dev, "CIO PHY asleep: uc %#x -> %#x, cfg %#x/%#x\n", uc, + readl(t6040_core(tphy) + T6040_CORE_UC_CTRL), + readl(t6040_cfg(tphy) + T6040_CFG_CTRL), + readl(t6040_cfg(tphy) + T6040_CFG_STATUS)); + tphy->cio_up = false; + pm_runtime_put(tphy->dev); +} + +/* + * Bring the PHY into CIO for the plug orientation. A failure is returned to + * the Type-C controller, which then does not hand the cable to the router. + */ +static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + bool flip; + int ret; + + lockdep_assert_held(&tphy->lock); + if (tphy->cio_up) + return 0; + + switch (tphy->orientation) { + case TYPEC_ORIENTATION_NORMAL: + flip = false; + break; + case TYPEC_ORIENTATION_REVERSE: + flip = true; + break; + default: + dev_warn(tphy->dev, "CIO: no plug orientation\n"); + return -ENODEV; + } + if (tphy->dp_up) { + dev_warn(tphy->dev, "CIO: lanes held by DP\n"); + return -EBUSY; + } + /* USB3 hands its lanes over and comes back once the port leaves CIO. */ + if (tphy->usb3_up || tphy->pipe_switched) { + t6040_usb3_down_locked(tphy); + tphy->usb3_lent = true; + } + + ret = pm_runtime_resume_and_get(tphy->dev); + if (ret) { + t6040_usb3_reclaim_locked(tphy); + return ret; + } + ret = t6040_usb3_wake(tphy); + if (ret) + goto sleep; /* the coprocessor block may be unclocked: never touch it */ + t6040_cio_tunables(tphy); + t6040_cio_xbar_config(tphy, flip); + t6040_cio_slpctrl(tphy); + ret = t6040_phy_run_firmware(tphy, "CIO", flip); + if (ret) + goto firmware; + + tphy->cio_up = true; + tphy->cio_flip = flip; + dev_info(tphy->dev, "CIO PHY up (mode 2), %s orientation: xbar %#x/%#x cfg %#x/%#x\n", + flip ? "reverse" : "normal", readl(core + T6040_CORE_XBAR), + readl(core + T6040_CORE_XBAR_LANES), readl(t6040_cfg(tphy) + T6040_CFG_CTRL), + readl(t6040_cfg(tphy) + T6040_CFG_STATUS)); + t6040_cio_pipe_update_locked(tphy); + return 0; + +firmware: + t6040_usb3_firmware_power_down(tphy); +sleep: + t6040_usb3_sleep(tphy); + pm_runtime_put(tphy->dev); + dev_warn(tphy->dev, "CIO PHY bring-up failed: %d\n", ret); + /* The port stays out of CIO: give the lanes back to USB3. */ + t6040_usb3_reclaim_locked(tphy); + return ret; +} + +/* USB4 entered with Enter_USB in USB4 mode, or Thunderbolt alt mode. */ +static bool cio_without_router; +module_param(cio_without_router, bool, 0644); +MODULE_PARM_DESC(cio_without_router, + "bring-up test: enter CIO on a USB4/Thunderbolt mux event even while no USB4 router node for this port is enabled (default N)"); + +/* + * A USB4/Thunderbolt mux event only takes the port into CIO while the router + * that runs over this PHY (a node whose apple,atc-phy points here) is enabled + * in the live device tree. The default device trees keep the routers + * disabled, so the port stays on its USB2/USB3/DP paths until a router is + * enabled (the USB4 bring-up tree, or at runtime). + */ +static bool t6040_cio_router_enabled(struct t6040_usb2_phy *tphy) +{ + struct device_node *np; + + if (READ_ONCE(cio_without_router)) + return true; + for_each_compatible_node(np, NULL, "apple,t6040-usb4-acio") { + struct device_node *phy = of_parse_phandle(np, "apple,atc-phy", 0); + bool match = phy == tphy->dev->of_node && of_device_is_available(np); + + of_node_put(phy); + if (match) { + of_node_put(np); + return true; + } + } + return false; +} + +static bool t6040_cio_requested(const struct typec_mux_state *state) +{ + const struct enter_usb_data *data = state->data; + + if (state->alt) + return state->alt->svid == USB_TYPEC_TBT_SID; + return state->mode == TYPEC_MODE_USB4 && data && + FIELD_GET(EUDO_USB_MODE_MASK, data->eudo) == EUDO_USB_MODE_USB4; +} + +static void t6040_cio_remove(void *data) +{ + struct t6040_usb2_phy *tphy = data; + + guard(mutex)(&tphy->lock); + if (tphy->router_running) + dev_warn(tphy->dev, "CIO: unbound under a running USB4 router\n"); + tphy->router_running = false; + t6040_cio_down_locked(tphy); +} + +/* CIO needs the USB3 pieces and the CIO lane tables; anything less leaves it off. */ +static int t6040_cio_probe(struct t6040_usb2_phy *tphy) +{ + struct device *dev = tphy->dev; + size_t i; + + if (!tphy->usb3_ok || + (!of_property_present(dev->of_node, t6040_cio_tunable_props[1]) && + !of_property_present(dev->of_node, t6040_cio_tunable_props[2]))) + return 0; + + for (i = 0; i < ARRAY_SIZE(t6040_cio_tunable_props); i++) { + const char *prop = t6040_cio_tunable_props[i]; + struct apple_tunable *t; + + if (!of_property_present(dev->of_node, prop)) { + if (i == 1 || i == 2) { + dev_warn(dev, "CIO: no %s, no USB4/Thunderbolt\n", prop); + return 0; + } + continue; + } + t = devm_apple_tunable_parse(dev, dev->of_node, prop, tphy->res[T6040_USB3_CORE]); + if (IS_ERR(t)) { + dev_warn(dev, "CIO: %s: %pe, no USB4/Thunderbolt\n", prop, t); + return 0; + } + tphy->cio_tunables[i] = t; + } + + /* Registered before the mode switch, so it runs after the switch is gone. */ + if (devm_add_action_or_reset(dev, t6040_cio_remove, tphy)) + return -ENOMEM; + tphy->cio_ok = true; + dev_dbg(dev, "CIO (USB4/Thunderbolt) mode available\n"); + return 0; +} + +/* ---- Type-C mode switch: the Type-C controller asks for DP on a USB-C port ---- */ + +static int t6040_dp_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state) +{ + struct t6040_usb2_phy *tphy = typec_mux_get_drvdata(mux); + bool dp = state->alt && state->alt->svid == USB_TYPEC_DP_SID; + bool alt = false; + + guard(mutex)(&tphy->lock); + /* USB4/TBT, only with the CIO tables; leaving it first takes the PHY out of CIO. */ + if (tphy->cio_ok && (tphy->cio_up || t6040_cio_router_enabled(tphy))) { + bool cio = t6040_cio_requested(state); + + if (tphy->cio_up && !cio) { + /* The router stops before the mux leaves USB4/TBT (apple.c). */ + if (tphy->router_running) { + dev_err(tphy->dev, "CIO: the USB4 router still runs, not leaving CIO\n"); + return -EBUSY; + } + t6040_cio_down_locked(tphy); + } + if (cio) { + tphy->dp_alt = false; + tphy->dp_two_lanes = false; + dev_dbg(tphy->dev, "Type-C mux: %s -> CIO\n", + state->alt ? "Thunderbolt" : "USB4"); + return t6040_cio_up_locked(tphy); + } + } + tphy->dp_flip = (tphy->orientation == TYPEC_ORIENTATION_REVERSE); + /* + * Latch the DP alt mode state for the DCP's Type-C route. Pin + * assignments C and E put DP on all four lanes. D leaves the USB3 + * pair with its running coprocessor and gives DP the other pair. + * The DCP brings the DP lanes up through phy_power_on()/phy_configure(). + */ + if (dp) { + switch (state->mode) { + case TYPEC_DP_STATE_C: + case TYPEC_DP_STATE_E: + alt = true; + break; + case TYPEC_DP_STATE_D: + alt = true; + break; + default: + break; + } + } + tphy->dp_alt = alt; + tphy->dp_two_lanes = alt && state->mode == TYPEC_DP_STATE_D; + dev_dbg(tphy->dev, "Type-C mux: %s state %lu -> DP alt mode %d, lane map %s\n", + dp ? "DP" : "non-DP", state->mode, alt, tphy->dp_flip ? "reversed" : "normal"); + if (alt && tphy->dp_up && !tphy->dp_mode_two_lanes && !tphy->dp_two_lanes) + t6040_dp_xbar_config(tphy, tphy->dp_flip); + t6040_usb3_reclaim_locked(tphy); + return 0; +} + +static void t6040_dp_mux_unregister(void *mux) +{ + typec_mux_unregister(mux); +} + +/* + * Everything the DP route needs from the device tree; a node without it gets + * no DP phy. A USB-C PHY offers DP through a Type-C mode switch; a fixed DP + * route (an HDMI converter's PHY) is driven straight by the DCP with no mux + * and uses the normal lane map. + */ +static int t6040_dp_probe(struct t6040_usb2_phy *tphy) +{ + struct device *dev = tphy->dev; + unsigned int i; + + tphy->dp_rate = -1; + + /* + * DP mode needs the "core" and "cfg" windows. A USB-C PHY has them + * alongside its USB regions; a fixed DP PHY has them without any + * pipehandler or USB3 coprocessor. A node with neither is USB2 only. + */ + if (!tphy->regs[T6040_USB3_CORE] || !tphy->regs[T6040_USB3_CFG]) + return 0; + + tphy->dp = devm_phy_create(dev, NULL, &t6040_dp_phy_ops); + if (IS_ERR(tphy->dp)) + return PTR_ERR(tphy->dp); + phy_set_drvdata(tphy->dp, tphy); + + /* DP-only node: the generic apply_tunables tables (absent = not applied). */ + if (t6040_dp_only(tphy)) { + for (i = 0; i < T6040_DP_NR_GENERIC_TUNABLES; i++) { + const char *prop = t6040_usb3_tunable_list[i].prop; + + if (!of_property_present(dev->of_node, prop) || + !tphy->res[t6040_usb3_tunable_list[i].region]) { + dev_warn(dev, "DP: no %s\n", prop); + tphy->tunables[i] = NULL; + continue; + } + struct resource *res = tphy->res[t6040_usb3_tunable_list[i].region]; + + tphy->tunables[i] = devm_apple_tunable_parse(dev, dev->of_node, prop, res); + if (IS_ERR(tphy->tunables[i])) { + dev_warn(dev, "DP: %s: %pe\n", prop, tphy->tunables[i]); + tphy->tunables[i] = NULL; + } + } + } + + if (of_property_present(dev->of_node, "mode-switch")) { + struct typec_mux_desc mux_desc = { + .drvdata = tphy, + .fwnode = dev_fwnode(dev), + .set = t6040_dp_mux_set, + }; + + tphy->mux = typec_mux_register(dev, &mux_desc); + if (IS_ERR(tphy->mux)) + return PTR_ERR(tphy->mux); + if (devm_add_action_or_reset(dev, t6040_dp_mux_unregister, tphy->mux)) + return -ENOMEM; + } + + tphy->dp_ok = true; + return 0; +} + +/* ---- the coprocessor's RTKit ---- */ + +/* The coprocessor places its buffers in its own __DATA SRAM. */ static int t6040_cpu_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) { struct t6040_usb2_phy *tphy = cookie; @@ -2486,6 +3014,10 @@ static int t6040_usb2_probe(struct platform_device *pdev) if (ret) return ret; + ret = t6040_cio_probe(tphy); + if (ret) + return ret; + ret = t6040_dp_probe(tphy); if (ret) return ret; @@ -2494,6 +3026,159 @@ static int t6040_usb2_probe(struct platform_device *pdev) return PTR_ERR_OR_ZERO(provider); } +#ifdef CONFIG_HIBERNATION +static int t6040_phy_count(struct phy *phy) +{ + return IS_ERR_OR_NULL(phy) ? 0 : phy->power_count; +} + +/* + * Resume from disk is a cold boot: the PHY, its coprocessor and the + * pipehandler come back reset, while this driver's state comes from the + * image. Before the image its consumers gave it back: the sleep hook + * unbinds dwc3 (USB2/USB3 phys powered off, controller reset asserted), + * the DCP powers the DP phy off at hibernation prepare. What is left set + * here describes hardware that no longer is: clear it to the state after + * probe, so that the next power-on programs the PHY afresh (with pin D, + * t6040_dp_power_on() otherwise takes the USB3 PHY for running). What + * consumers hold (powered, usb3_on, usb3_pipe, dwc3_running and the PHY + * core counts, with their runtime PM reference) is theirs to release and + * is kept, so that every count stays balanced; a phy still held is only + * reported. The port's Type-C state (orientation, DP alt mode, pin + * assignment) describes the partner, not the PHY, and stays for the next + * power-on. Early, before any .restore: the Type-C controller's may set + * the mux again. Nothing touches the hardware here. CIO is torn down + * before the image (.freeze), so the image never holds a CIO session. + */ +static int t6040_usb2_restore_early(struct device *dev) +{ + struct t6040_usb2_phy *tphy = dev_get_drvdata(dev); + + if (!tphy) + return 0; + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return 0; + guard(mutex)(&tphy->lock); + if (tphy->powered || tphy->usb3_on || tphy->dwc3_running || + t6040_phy_count(tphy->dp)) + dev_warn(dev, + "restore: still held across the image (usb2 %d, usb3 %d, dwc3 %d, dp %d/%d): its next power-on finds the PHY cold\n", + tphy->powered, tphy->usb3_on, tphy->dwc3_running, + t6040_phy_count(tphy->dp), tphy->dp_up); + /* .freeze refuses an image with CIO up; never trust one that has it. */ + if (tphy->cio_up) { + dev_warn(dev, "restore: CIO was up in the image, dropped\n"); + tphy->cio_up = false; + tphy->router_running = false; + tphy->usb3_tunnel_req = false; + tphy->pipe_tunneled = false; + tphy->tun_pll = false; + tphy->tun_rate[0] = tphy->tun_rate[1] = 0; + pm_runtime_put_noidle(dev); + } + dev_info(dev, "restore: PHY state reset (usb3 up %d, pipe %d, lent %d, dp up %d, 2-lane %d)\n", + tphy->usb3_up, tphy->pipe_switched, tphy->usb3_lent, tphy->dp_up, + tphy->dp_mode_two_lanes); + tphy->usb3_up = false; + tphy->pipe_switched = false; + tphy->usb3_lent = false; + tphy->dp_up = false; + tphy->dp_mode_two_lanes = false; + tphy->dp_mode_flip = false; + tphy->dp_rate = -1; + tphy->shutdown_failed = false; + return 0; +} + +/* + * The coprocessor's RTKit session is reset with it. Its host side is + * reset at power-down already (t6040_usb3_firmware_power_down()); one + * still running here (USB3 held across the image) is reset now, in the + * normal phase, after the DARTs its buffers live behind are restored. + */ +static int t6040_usb2_restore(struct device *dev) +{ + struct t6040_usb2_phy *tphy = dev_get_drvdata(dev); + int ret; + + if (!tphy || IS_ERR_OR_NULL(tphy->rtk)) + return 0; + if (!pm_hibernate_is_restored()) + return 0; + guard(mutex)(&tphy->lock); + if (!apple_rtkit_is_running(tphy->rtk)) + return 0; + ret = apple_rtkit_reinit(tphy->rtk); + dev_info(dev, "restore: USB3 coprocessor RTKit reset: %d\n", ret); + return 0; +} + +#endif + +#ifdef CONFIG_PM_SLEEP +/* + * System sleep with the port in USB4/Thunderbolt: s2idle, a firmware-driven + * suspend (pm_suspend_via_firmware(), which brings the PHY back like a cold + * start) and the three phases of hibernation that leave the running system + * (freeze before the image, poweroff after it). CIO is not kept across a + * sleep: the ACIO sleep is a full PHY shutdown, and + * the ACIO wake a full PHY init for mode 2; the firmware-assisted S2R state + * save is for the USB modes only. Here CIO is torn down the same way in each + * of them, after the router has stopped (apple_t6040_cio_router_stopped(), + * from its sleep notifier before any device callback); its DP tunnels and the + * tunnelled PIPE go with it. Nothing is kept for resume: the Type-C + * controller reads the port afresh after every sleep and sets the mux again, + * which brings CIO back before it starts the router. A router still marked + * running breaks that contract and fails the transition. Without a CIO + * session none of this touches the hardware. + */ +static int t6040_usb2_sleep(struct device *dev, const char *what) +{ + struct t6040_usb2_phy *tphy = dev_get_drvdata(dev); + + if (!tphy) + return 0; + guard(mutex)(&tphy->lock); + if (!tphy->cio_up) + return 0; + if (tphy->router_running) { + dev_err(dev, "%s: the USB4 router still runs on this port; it must stop before the PHY\n", + what); + return -EBUSY; + } + t6040_cio_down_locked(tphy); + dev_info(dev, "%s: CIO down for system sleep; the Type-C controller brings it back\n", + what); + return 0; +} + +static int t6040_usb2_suspend(struct device *dev) +{ + return t6040_usb2_sleep(dev, "suspend"); +} + +static int t6040_usb2_freeze(struct device *dev) +{ + return t6040_usb2_sleep(dev, "freeze"); +} + +static int t6040_usb2_poweroff(struct device *dev) +{ + return t6040_usb2_sleep(dev, "poweroff"); +} + +static const struct dev_pm_ops t6040_usb2_pm_ops = { + .suspend = t6040_usb2_suspend, + .freeze = t6040_usb2_freeze, + .poweroff = t6040_usb2_poweroff, +#ifdef CONFIG_HIBERNATION + .restore_early = t6040_usb2_restore_early, + .restore = t6040_usb2_restore, +#endif +}; +#endif + static const struct of_device_id t6040_usb2_of_match[] = { { .compatible = "apple,t6040-atcphy" }, {} @@ -2505,8 +3190,101 @@ static struct platform_driver t6040_usb2_driver = { .driver = { .name = "phy-apple-t6040-usb2", .of_match_table = t6040_usb2_of_match, +#ifdef CONFIG_PM_SLEEP + .pm = &t6040_usb2_pm_ops, +#endif }, }; + +/* ---- the USB4 router's view of the PHY (linux/phy/apple-t6040-cio.h) ---- */ + +/* + * Run @fn on the bound PHY of @phy_np with its lock held. The device lock + * keeps the driver bound for the call. + */ +static int t6040_cio_call(struct device_node *phy_np, + int (*fn)(struct t6040_usb2_phy *tphy, unsigned long arg), + unsigned long arg) +{ + struct platform_device *pdev; + struct t6040_usb2_phy *tphy; + int ret; + + if (!phy_np || !of_device_is_compatible(phy_np, "apple,t6040-atcphy")) + return -ENODEV; + pdev = of_find_device_by_node(phy_np); + if (!pdev) + return -EPROBE_DEFER; + device_lock(&pdev->dev); + tphy = platform_get_drvdata(pdev); + if (pdev->dev.driver != &t6040_usb2_driver.driver || !tphy) { + ret = -EPROBE_DEFER; + } else { + guard(mutex)(&tphy->lock); + ret = fn(tphy, arg); + } + device_unlock(&pdev->dev); + put_device(&pdev->dev); + return ret; +} + +static int t6040_cio_state_fn(struct t6040_usb2_phy *tphy, unsigned long arg) +{ + return tphy->cio_up ? 1 : 0; +} + +static int t6040_cio_router_fn(struct t6040_usb2_phy *tphy, unsigned long running) +{ + if (running) { + if (!tphy->cio_up) + return -EAGAIN; + tphy->router_running = true; + dev_dbg(tphy->dev, "CIO: router started\n"); + t6040_cio_pipe_update_locked(tphy); + return 0; + } + if (!tphy->router_running) + return 0; + tphy->router_running = false; + tphy->usb3_tunnel_req = false; + t6040_cio_pipe_update_locked(tphy); + dev_dbg(tphy->dev, "CIO: router stopped\n"); + return 0; +} + +static int t6040_cio_usb3_fn(struct t6040_usb2_phy *tphy, unsigned long on) +{ + if (on && !tphy->router_running) + return -EAGAIN; + tphy->usb3_tunnel_req = on; + t6040_cio_pipe_update_locked(tphy); + return 0; +} + +int apple_t6040_cio_state(struct device_node *phy_np) +{ + return t6040_cio_call(phy_np, t6040_cio_state_fn, 0); +} +EXPORT_SYMBOL_GPL(apple_t6040_cio_state); + +int apple_t6040_cio_router_started(struct device_node *phy_np) +{ + return t6040_cio_call(phy_np, t6040_cio_router_fn, 1); +} +EXPORT_SYMBOL_GPL(apple_t6040_cio_router_started); + +void apple_t6040_cio_router_stopped(struct device_node *phy_np) +{ + t6040_cio_call(phy_np, t6040_cio_router_fn, 0); +} +EXPORT_SYMBOL_GPL(apple_t6040_cio_router_stopped); + +int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on) +{ + return t6040_cio_call(phy_np, t6040_cio_usb3_fn, on); +} +EXPORT_SYMBOL_GPL(apple_t6040_cio_usb3_tunnel); + module_platform_driver(t6040_usb2_driver); MODULE_AUTHOR("The Asahi Linux Contributors"); diff --git a/drivers/thunderbolt/apple-dpin-test.c b/drivers/thunderbolt/apple-dpin-test.c index dd532a2a051b5..119c2d4466970 100644 --- a/drivers/thunderbolt/apple-dpin-test.c +++ b/drivers/thunderbolt/apple-dpin-test.c @@ -362,6 +362,9 @@ static void apple_dpin_qualification_profiles(struct kunit *test) { "apple,t8132", &apple_dpin_m4, false, &apple_dpin_m4 }, { "apple,t6050", &apple_dpin_m5, true, &apple_dpin_m5 }, { "apple,t6050", &apple_dpin_m5, false, &apple_dpin_m5 }, + /* T6040/T6041 qualify; the router keeps them off without t6040_dp_tunnels. */ + { "apple,t6040", &apple_dpin_m3, true, &apple_dpin_m3 }, + { "apple,t6041", &apple_dpin_m3, true, &apple_dpin_m3 }, { "apple,unknown", &apple_dpin_m1, true, &apple_dpin_disabled }, }; unsigned int i; diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 3aa62de12e91b..c4f821e1d78ec 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -95,6 +95,7 @@ #include #include #include +#include #include #include #include @@ -142,6 +143,11 @@ struct apple_cio_hw { /* DP IN adapter blocks of the router; 0 means the two of t8103 */ unsigned int dpin_count; + /* + * Type 7 (acio,v2) routers: firmware ready at state BIT(24), PDF + * masks in the receive ring registers, loader tuning required. + */ + bool type7; bool mxwrap; bool lstx_explicit; /* Leave MxWrap RUN alone: the CIO reconfig restarts the CPU. */ @@ -153,8 +159,6 @@ struct apple_cio_hw { bool no_host_clx; /* Keep the domain's links symmetric. */ bool no_asym; - /* Type 7 router: Enter_USB cable words, bootloader RC tuning, Type 7 NHI. */ - bool type7; /* * Release the CPU with a plain store of the RUN value rather than a * read-modify-write, and halt it again when the router stops. @@ -168,6 +172,15 @@ struct apple_cio_hw { bool gen4_link_recovery; /* Leave the host router's TMU to its firmware; host TMU constants. */ bool host_tmu; + /* + * The router runs only over its Type-C PHY in CIO mode ("apple,atc-phy") + * and tells the PHY when it starts and stops. + */ + bool cio_phy; + /* Power the router off for every system sleep, hibernation included. */ + bool sleep_stop; + /* Let a Thunderbolt 3 (not USB4) link settle after RTKit boot. */ + bool tbt_settle; const struct apple_dpin_policy *dp; }; @@ -234,17 +247,44 @@ static const struct apple_cio_hw apple_cio_t6050_hw = { .dp = &apple_dpin_m5, }; +/* + * T6040/T6041 (M4 Pro/Max) routers are Type 7 as on T8142: the platform drives their + * acio,v2 nodes with the Type 7 HAL, and the ADT has no per-direction NHI + * fabric tables. iBoot loads the CIO firmware into the router's SRAM; the platform + * never loads it and never writes RUN: starting the IOP enables acio-cpuN's + * clock gate, the PMGR virtual device CIOn-RECONFIG-V, i.e. the CIO + * reconfiguration this driver requests through its reset. After RTKit boot + * The platform waits 400 ms for Thunderbolt 3 (not USB4) links, hands LSTX to the + * router, then polls the firmware state for 1 (assert: bit 31 + code). The platform + * powers the router off for every system sleep, with or without a cable, and + * starts it cold on wake; so does this driver. The host links stay in CL0 + * until CL states are qualified (the platform allows them). Not hardware-tested. + */ static const struct apple_cio_hw apple_cio_t6040_hw = { - .mxwrap = true, .type7 = true, + .mxwrap = true, .lstx_explicit = true, .skip_run = true, .lstx_after_boot = true, .strict_fw_ready = true, .no_host_clx = true, - .dp = &apple_dpin_disabled, + .cio_phy = true, + .sleep_stop = true, + .tbt_settle = true, + /* + * The DP IN adapter handshake is the t8103 one, as on M3 (the platform + * has one handshake for every SoC). Off unless + * t6040_dp_tunnels is set: the DP IN adapter to dpin0/1 mapping is + * not decoded. + */ + .dp = &apple_dpin_m3, }; +/* The platform sleeps 400 ms on the embedded CPU's enable if CIO generation < 3. */ +#define APPLE_CIO_TBT_SETTLE_MS 400 +/* Upper bound for a deferred sleep stop before the suspend is aborted. */ +#define APPLE_CIO_SLEEP_STOP_TIMEOUT_MS 60000 + #define APPLE_CIO_NHI_HOP_COUNT 0x0 #define APPLE_CIO_NHI_HOP_COUNT_MASK GENMASK(9, 0) @@ -355,6 +395,11 @@ struct apple_dpin_binding; * @nhi_boot_completion: Signals completion of the NHI bringup * @nhi_boot_status: Status returned by the NHI bringup * @tbt_switch: USB4/Thunderbolt switch triggered by the Type-C PD driver + * @sleep_hibernating: Hibernation is under way; thaw does not reopen the port + * @sleep_stop_work: Stops a port with a PCIe-C host for system sleep + * @sleep_stop_done: Signals that @sleep_stop_work has finished + * @sleep_stop_queued: @sleep_stop_work was queued for this sleep + * @phy_np: Type-C PHY the router runs over (hw->cio_phy) */ struct apple_cio { struct device *dev; @@ -418,6 +463,14 @@ struct apple_cio { } sleep_phase; /* protected by lock */ bool sleep_restart_owed; /* protected by lock */ bool sleep_aborted_early; /* protected by lock: devices never suspended */ + /* hw->sleep_stop: hibernation is under way, thaw does not reopen */ + bool sleep_hibernating; /* protected by lock */ + /* hw->sleep_stop: the stop of a port with a PCIe-C host runs here */ + struct work_struct sleep_stop_work; + struct completion sleep_stop_done; + bool sleep_stop_queued; /* PM notifier and prepare only */ + /* hw->cio_phy: the Type-C PHY this router runs over */ + struct device_node *phy_np; u32 current_cable_info; u32 target_cable_info; @@ -505,6 +558,11 @@ static bool dp_display = true; module_param(dp_display, bool, 0444); MODULE_PARM_DESC(dp_display, "Drive displays behind qualified Thunderbolt DP tunnels (default: true)"); +static bool t6040_dp_tunnels; +module_param(t6040_dp_tunnels, bool, 0444); +MODULE_PARM_DESC(t6040_dp_tunnels, + "T6040/T6041: tunnel DisplayPort from the routers' DP IN adapters (bring-up, default off)"); + /* * The M1 Pro/Max ATC is the t8103 generation: same DP IN adapter registers, * same crossbar and the same tunnel pixel clock sequence. @@ -521,6 +579,9 @@ static const struct of_device_id apple_dpin_qualified_soc[] = { { .compatible = "apple,t8132" }, /* T6050: t8103 DP IN handshake, M3 flow, four DP IN adapters */ { .compatible = "apple,t6050" }, + /* M4 Pro/Max: only with t6040_dp_tunnels, see apple_cio_t6040_hw. */ + { .compatible = "apple,t6040" }, + { .compatible = "apple,t6041" }, {}, }; @@ -2394,6 +2455,18 @@ static const struct tb_nhi_ops apple_nhi_ops = { .pci_tunnel_deactivate = apple_nhi_pci_tunnel_deactivate, }; +/* + * hw->cio_phy: the port's USB controller reaches a dock's USB3 through the + * router's host USB3 adapter, so the PHY points its PIPE at the router while + * such a tunnel is up (the USB3 mode 3) and back before the tunnel goes. + */ +static int apple_nhi_usb3_tunnel_changed(struct tb_nhi *nhi, bool on) +{ + struct apple_cio *acio = nhi_to_anhi(nhi)->acio; + + return apple_t6040_cio_usb3_tunnel(acio->phy_np, on); +} + static const struct tb_nhi_ring_layout apple_nhi_ring_layout = { .tx_desc_base = APPLE_CIO_NHI_TXRING_DESC_BASE, .rx_desc_base = APPLE_CIO_NHI_RXRING_DESC_BASE, @@ -2517,6 +2590,8 @@ static int apple_nhi_probe(struct platform_device *pdev) ret = apple_nhi_cio_bind(anhi); if (ret) goto err; + if (acio->hw->cio_phy) + anhi->ops.usb3_tunnel_changed = apple_nhi_usb3_tunnel_changed; anhi->nhi.ops = &anhi->ops; anhi->nhi.ring_layout = &apple_nhi_ring_layout; anhi->nhi.iobase = anhi->nhi_base; @@ -3068,6 +3143,12 @@ static void apple_cio_stop(struct apple_cio *acio) lockdep_assert_held(&acio->lock); if (!acio->current_cable_info) return; + /* + * Domain removal frees the tunnels without deactivating them one by + * one: give the USB controller its PIPE back before the router goes. + */ + if (acio->hw->cio_phy) + apple_t6040_cio_usb3_tunnel(acio->phy_np, false); apple_cio_remove_children(acio); /* Try to shut down and power off the co-processor gracefully */ @@ -3079,11 +3160,18 @@ static void apple_cio_stop(struct apple_cio *acio) acio->rtk = NULL; apple_cio_cpu_halt(acio); - /* High only if the router left it driven high with the cable still in */ - if (acio->hw->lstx_after_boot) - apple_cio_lstx_hold(acio, acio->target_cable_info && - (readl(acio->rc_base + APPLE_CIO_M3_CTRL) & - APPLE_CIO_LSTX_CTRL_LEVEL)); + /* + * High only if the router left it high with the cable still in. Type 7 + * follows the platform (its disable of the embedded CPU): anything but + * "driven low" (2) counts as high, including a pin the firmware released. + */ + if (acio->hw->lstx_after_boot) { + u32 lstx = readl(acio->rc_base + APPLE_CIO_M3_CTRL); + bool high = acio->type7 ? lstx != APPLE_CIO_LSTX_CTRL_ENABLE : + lstx & APPLE_CIO_LSTX_CTRL_LEVEL; + + apple_cio_lstx_hold(acio, acio->target_cable_info && high); + } /* Finally, remove the links to the PD domains to power everything off */ for (i = 0; i < acio->pd_list->num_pds; i++) { @@ -3096,6 +3184,9 @@ static void apple_cio_stop(struct apple_cio *acio) acio->current_cable_info = 0; acio->resume_link_failed = false; acio->link_lost_restart = false; + /* ACIO is off: the PHY may leave CIO now (unplug, removal, sleep). */ + if (acio->hw->cio_phy) + apple_t6040_cio_router_stopped(acio->phy_np); } /* @@ -3133,6 +3224,8 @@ static bool apple_cio_recovering(struct apple_cio *acio) return acio->resume_recovery_active; } +static int apple_cio_power_up(struct apple_cio *acio); + /** * apple_cio_start - Power up and start the ACIO complex * @acio: ACIO complex to start @@ -3145,6 +3238,36 @@ static bool apple_cio_recovering(struct apple_cio *acio) * Return: 0 on success or a negative error code on failure. */ static int apple_cio_start(struct apple_cio *acio) +{ + int ret; + + lockdep_assert_held(&acio->lock); + if (!acio->hw->cio_phy) + return apple_cio_power_up(acio); + + /* + * The PHY must be in CIO, tuned by its firmware, before ACIO powers up + * (the platform opens the PHY in mode 2 before the embedded CPU's enable). The + * Type-C controller sets the mux first and retries on its next event. + */ + ret = apple_t6040_cio_state(acio->phy_np); + if (ret != 1) { + ret = ret < 0 ? ret : -EAGAIN; + dev_err(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); + return ret; + } + ret = apple_t6040_cio_router_started(acio->phy_np); + if (ret) { + dev_err(acio->dev, "Type-C PHY refused the router: %d\n", ret); + return ret; + } + ret = apple_cio_power_up(acio); + if (ret) + apple_t6040_cio_router_stopped(acio->phy_np); + return ret; +} + +static int apple_cio_power_up(struct apple_cio *acio) { struct device_link *link; int i, ret, attempts; @@ -3268,6 +3391,11 @@ static int apple_cio_start(struct apple_cio *acio) goto err_free_rtkit; } + /* The platform gives a Thunderbolt 3 or legacy link 400 ms here, USB4 none. */ + if (acio->hw->tbt_settle && + (acio->target_cable_info & TB_VSE_CAP_APPLE_CABLE_INFO_TBT2_3)) + msleep(APPLE_CIO_TBT_SETTLE_MS); + /* * The router takes LSTX over at the level it last drove, low after a * cold start, and then gets its pin back from the AOP GPIO controller. @@ -3549,6 +3677,18 @@ static int apple_cio_prepare(struct device *dev) { struct apple_cio *acio = dev_get_drvdata(dev); + /* + * A sleep stop deferred from the PM notifier must be over before any + * device suspends: the PHY refuses to sleep under a running router. + * Notifiers are done and the PM core holds no lock the stop needs. + */ + if (READ_ONCE(acio->sleep_stop_queued) && + !wait_for_completion_timeout(&acio->sleep_stop_done, + msecs_to_jiffies(APPLE_CIO_SLEEP_STOP_TIMEOUT_MS))) { + dev_err(dev, "router did not stop for system sleep, aborting it\n"); + return -EBUSY; + } + /* Drain before taking lock; the worker acquires it to check the link. */ cancel_delayed_work_sync(&acio->resume_link_work); cancel_delayed_work_sync(&acio->link_lost_work); @@ -3566,7 +3706,8 @@ static void apple_cio_complete(struct device *dev) struct apple_cio *acio = dev_get_drvdata(dev); guard(mutex)(&acio->lock); - if (acio->sleep_phase == APPLE_CIO_SLEEP_CLOSED) + /* Thaw before the image is written is not a resume: stay closed. */ + if (acio->sleep_phase == APPLE_CIO_SLEEP_CLOSED && !acio->sleep_hibernating) acio->sleep_phase = APPLE_CIO_SLEEP_RESUMED; if (!READ_ONCE(acio->resume_link_pending)) return; @@ -3780,34 +3921,134 @@ static void apple_cio_sleep_restart_work(struct work_struct *work) typec_thunderbolt_switch_notify_restore(acio->tbt_switch); } +/* + * hw->sleep_stop: the router goes down for every system sleep + * (its sleep sets hardware state 1 with a cable, hardware state 0 without), + * whether the sleep is s2idle, a firmware-driven suspend or hibernation. + * Nothing of the router survives that is used again: the next start is a cold + * one, CIO reconfiguration (which restarts the firmware iBoot left in the + * router's SRAM) and RTKit boot, after the Type-C controller has reapplied the + * cable. + * Until the sleep is over, cable changes are only recorded, and a restart is + * owed even without a cable so that a cable plugged in during sleep is read. + * Called with acio->lock held. + */ +static void apple_cio_sleep_stop_locked(struct apple_cio *acio) +{ + lockdep_assert_held(&acio->lock); + + acio->sleep_phase = APPLE_CIO_SLEEP_CLOSED; + WRITE_ONCE(acio->sleep_restart_owed, true); + if (acio->removing || !acio->current_cable_info) + return; + + /* Close PCIe admission; the next NHI probe reopens it. */ + scoped_guard(mutex, &acio->pcie_tunnel_lock) + acio->pcie_tunnel_stopping = true; + dev_info(acio->dev, "powering off router for system sleep (cable info 0x%x)\n", + acio->current_cable_info); + apple_cio_stop(acio); +} + +static void apple_cio_sleep_stop_work(struct work_struct *work) +{ + struct apple_cio *acio = container_of(work, struct apple_cio, sleep_stop_work); + + scoped_guard(mutex, &acio->lock) + apple_cio_sleep_stop_locked(acio); + complete_all(&acio->sleep_stop_done); +} + +static void apple_cio_sleep_stop(struct apple_cio *acio, bool hibernate) +{ + bool defer; + + cancel_delayed_work_sync(&acio->resume_link_work); + + scoped_guard(mutex, &acio->lock) { + acio->sleep_hibernating = hibernate; + acio->sleep_aborted_early = false; + /* + * PCIe endpoints may unregister PM notifiers on removal, which + * would deadlock inside this notifier chain. A port with a PCIe-C + * host stops from a worker instead, which this device's .prepare + * waits for before any device suspends. + */ + scoped_guard(mutex, &acio->pcie_tunnel_lock) + defer = acio->current_cable_info && acio->pcie_tunnel_populated; + if (!defer) { + apple_cio_sleep_stop_locked(acio); + return; + } + } + + reinit_completion(&acio->sleep_stop_done); + WRITE_ONCE(acio->sleep_stop_queued, true); + queue_work(system_dfl_wq, &acio->sleep_stop_work); +} + static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, void *unused) { struct apple_cio *acio = container_of(nb, struct apple_cio, pm_notifier); + bool stop = acio->hw->sleep_stop; bool owed, early; /* Teardown off and no restart owed from an earlier sleep: nothing to do. */ - if (!READ_ONCE(s2idle_teardown) && !READ_ONCE(acio->sleep_restart_owed)) + if (!stop && !READ_ONCE(s2idle_teardown) && !READ_ONCE(acio->sleep_restart_owed)) return NOTIFY_DONE; switch (event) { + case PM_HIBERNATION_PREPARE: + case PM_RESTORE_PREPARE: + if (!stop) + break; + fallthrough; case PM_SUSPEND_PREPARE: /* Nothing from an earlier sleep may restart the port during this one. */ cancel_delayed_work_sync(&acio->sleep_restart_work); - apple_cio_sleep_close_owed(acio); /* Before tasks freeze: tearing down the NHI needs unfrozen work. */ + if (stop) { + apple_cio_sleep_stop(acio, event != PM_SUSPEND_PREPARE); + break; + } + apple_cio_sleep_close_owed(acio); if (READ_ONCE(s2idle_teardown)) apple_cio_sleep_teardown(acio); break; + case PM_POST_HIBERNATION: + case PM_POST_RESTORE: + if (!stop) + break; + fallthrough; case PM_POST_SUSPEND: + /* A deferred stop is over once .prepare has run; not if it never did. */ + if (READ_ONCE(acio->sleep_stop_queued)) { + /* As .prepare: a stop that never ends must not hang PM forever. */ + if (!wait_for_completion_timeout(&acio->sleep_stop_done, + msecs_to_jiffies(APPLE_CIO_SLEEP_STOP_TIMEOUT_MS))) { + dev_err(acio->dev, + "router stop for system sleep did not finish, not restarting the port\n"); + break; + } + WRITE_ONCE(acio->sleep_stop_queued, false); + } scoped_guard(mutex, &acio->lock) { - /* Aborted before device suspend: complete never ran. */ + /* + * Aborted before device suspend: complete never ran. After + * hibernation complete did not reopen either, and the cable + * state the Type-C controller reapplied on restore was only + * recorded: ask for it again now. + */ if (acio->sleep_phase == APPLE_CIO_SLEEP_CLOSED) { acio->sleep_phase = APPLE_CIO_SLEEP_RESUMED; acio->sleep_aborted_early = true; } + acio->sleep_hibernating = false; owed = acio->sleep_restart_owed; - early = acio->sleep_aborted_early; + /* A cable recorded while closed has nobody else to start it. */ + early = acio->sleep_aborted_early || + (stop && acio->target_cable_info && !acio->current_cable_info); } /* Start from a worker: probing children inside this chain can deadlock. */ if (owed) @@ -3853,6 +4094,8 @@ static int apple_cio_probe(struct platform_device *pdev) INIT_DELAYED_WORK(&acio->resume_link_work, apple_cio_resume_link_work); INIT_DELAYED_WORK(&acio->link_lost_work, apple_cio_link_lost_work); INIT_DELAYED_WORK(&acio->sleep_restart_work, apple_cio_sleep_restart_work); + INIT_WORK(&acio->sleep_stop_work, apple_cio_sleep_stop_work); + init_completion(&acio->sleep_stop_done); init_completion(&acio->nhi_boot_completion); acio->dev = &pdev->dev; acio->np = dev->of_node; @@ -3860,6 +4103,15 @@ static int apple_cio_probe(struct platform_device *pdev) if (!acio->hw) return -EINVAL; + if (acio->hw->cio_phy) { + acio->phy_np = of_parse_phandle(acio->np, "apple,atc-phy", 0); + if (!acio->phy_np) + return dev_err_probe(dev, -EINVAL, "Missing apple,atc-phy\n"); + ret = devm_add_action_or_reset(dev, apple_cio_of_node_put, acio->phy_np); + if (ret) + return ret; + } + acio->type7 = acio->hw->type7; if (acio->type7 && of_property_count_u32_elems(acio->np, "apple,tunable-rc") <= 0) @@ -4017,6 +4269,8 @@ static int apple_cio_probe(struct platform_device *pdev) acio->dp_dual_stream = apple_dp_tunnel_dual_stream(acio->connector_np); /* Register layout, root qualification and connector wiring are separate. */ acio->dp = apple_dpin_policy_select(acio->hw->dp, of_root, acio->dp_dual_stream); + if (acio->hw->cio_phy && !t6040_dp_tunnels) + acio->dp = &apple_dpin_disabled; if (acio->connector_np) { ret = devm_add_action_or_reset(dev, apple_cio_of_node_put, acio->connector_np); @@ -4100,6 +4354,7 @@ static void apple_cio_remove(struct platform_device *pdev) apple_pcie_tunnel_unregister_notifier(&acio->pcie_notifier); unregister_pm_notifier(&acio->pm_notifier); disable_delayed_work_sync(&acio->sleep_restart_work); + disable_work_sync(&acio->sleep_stop_work); disable_delayed_work_sync(&acio->resume_link_work); disable_delayed_work_sync(&acio->link_lost_work); diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h index 6a7461ed5602e..4198cf596ef26 100644 --- a/drivers/thunderbolt/nhi.h +++ b/drivers/thunderbolt/nhi.h @@ -110,6 +110,9 @@ struct tb_nhi_ring_layout { * connected to a lane adapter of the host router is unplugged. * Lets the NHI recover a port whose link went down while the * cable stays attached. + * @usb3_tunnel_changed: A USB3 tunnel from a host router USB3 adapter is + * now active (@on, after activation) or about to be + * deactivated (!@on). Called with the domain lock held. * @init_interrupts: NHI specific interrupt initialization hook * @router_sleep: Enter host-router sleep with the control channel running * @router_wake: Start the host router's links again after its controller restarted @@ -141,6 +144,7 @@ struct tb_nhi_ops { bool (*add_links)(struct tb_nhi *nhi); bool (*is_present)(struct tb_nhi *nhi); void (*link_lost)(struct tb_nhi *nhi); + int (*usb3_tunnel_changed)(struct tb_nhi *nhi, bool on); void (*dp_tunnel_changed)(struct tb_nhi *nhi, u8 in_port, u64 generation, bool active); bool (*dp_tunnel_awaits_display)(struct tb_nhi *nhi, struct tb_port *in); int (*init_interrupts)(struct tb_nhi *nhi); diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 25986e65f3fda..0e9df279fdc2f 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -948,6 +948,21 @@ static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port, tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down); } +/* Tell the host interface when a USB3 tunnel from the host router changes. */ +static void tb_usb3_host_tunnel_changed(struct tb_tunnel *tunnel, bool on) +{ + const struct tb_nhi_ops *ops = tunnel->tb->nhi->ops; + int ret; + + if (!tb_tunnel_is_usb3(tunnel) || tb_route(tunnel->src_port->sw) || + !ops || !ops->usb3_tunnel_changed) + return; + ret = ops->usb3_tunnel_changed(tunnel->tb->nhi, on); + if (ret) + tb_tunnel_warn(tunnel, "host USB3 %s failed: %d\n", + on ? "attach" : "detach", ret); +} + static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) { struct tb_switch *parent = tb_switch_parent(sw); @@ -1026,6 +1041,7 @@ static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) } list_add_tail(&tunnel->list, &tcm->tunnel_list); + tb_usb3_host_tunnel_changed(tunnel, true); if (tb_route(parent)) tb_reclaim_usb3_bandwidth(tb, down, up); @@ -1852,6 +1868,7 @@ static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel) return; tb_dp_tunnel_notify(tunnel, false); + tb_usb3_host_tunnel_changed(tunnel, false); tb_tunnel_deactivate(tunnel); list_del(&tunnel->list); diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 538959833e4ee..61525a79bf617 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -2453,6 +2453,29 @@ VISIBLE_IF_KUNIT bool cd321x_tbt_switch_keep_waiting(struct device *dev) } EXPORT_SYMBOL_IF_KUNIT(cd321x_tbt_switch_keep_waiting); +/* + * The connection lookup skips a router whose node is disabled. A router that is + * described but disabled in the device tree can still be enabled at runtime (an + * overlay during bring-up), so the port watches for it as for a late router. + */ +static bool cd321x_tbt_router_described(struct fwnode_handle *fwnode) +{ + struct fwnode_handle *ep, *node; + bool found = false; + + fwnode_graph_for_each_endpoint(fwnode, ep) { + node = fwnode_graph_get_remote_port_parent(ep); + found = fwnode_property_present(node, "thunderbolt-switch"); + fwnode_handle_put(node); + if (found) { + fwnode_handle_put(ep); + break; + } + } + + return found; +} + /* * The USB4/Thunderbolt router is only needed once a USB4 or Thunderbolt link * comes up. If its driver is not built or has not bound yet, do not leave power @@ -2468,6 +2491,11 @@ cd321x_tbt_switch_get(struct device *dev, struct fwnode_handle *fwnode, *late = false; sw = fwnode_typec_thunderbolt_switch_get(fwnode); + if (!sw && cd321x_tbt_router_described(fwnode)) { + dev_dbg(dev, "USB4/Thunderbolt router disabled, watching for it\n"); + *late = true; + return NULL; + } if (sw != ERR_PTR(-EPROBE_DEFER) || cd321x_tbt_switch_keep_waiting(dev)) return sw; @@ -3665,6 +3693,58 @@ int tipd_resume(struct tps6598x *tps) } EXPORT_SYMBOL_GPL(tipd_resume); +/* + * Bring a controller that went through a cold boot (a resume from disk) back + * to the state its probe leaves it in, without registering anything again: + * system power state S0, application firmware running (patch loaded if it + * waits for one) and the interrupt mask the driver uses. The port state is + * read afresh by the revalidation that follows. + */ +int tipd_reinit_after_cold_boot(struct tps6598x *tps) +{ + int ret; + + if (tps->data->switch_power_state) { + ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); + if (ret) + return ret; + } + ret = tps6598x_check_mode(tps); + if (ret < 0) + return ret; + if (ret == TPS_MODE_PTCH) { + ret = tps->data->init(tps); + if (ret) + return ret; + } + return tps6598x_write64(tps, TPS_REG_INT_MASK1, cd321x_irq_mask(tps)); +} +EXPORT_SYMBOL_GPL(tipd_reinit_after_cold_boot); + +/* + * Look the connector's USB role switch up again. After a resume from disk the + * USB controller that provides it was bound again, which registered a new + * switch; the old one is unregistered and only refuses requests. + */ +int tipd_refresh_role_switch(struct tps6598x *tps) +{ + struct fwnode_handle *fwnode; + struct usb_role_switch *sw, *old; + + fwnode = device_get_named_child_node(tps->dev, "connector"); + sw = fwnode_usb_role_switch_get(fwnode); + fwnode_handle_put(fwnode); + if (IS_ERR(sw)) + return PTR_ERR(sw); + scoped_guard(mutex, &tps->lock) { + old = tps->role_sw; + tps->role_sw = sw; + } + usb_role_switch_put(old); + return 0; +} +EXPORT_SYMBOL_GPL(tipd_refresh_role_switch); + /* Freezable cable work has drained before device PM calls prepare. */ int tipd_prepare(struct device *dev) { diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index ddd28b8d94e46..229809c59ee3e 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -51,7 +51,7 @@ static int regmap_sn201202x_select_reg(struct spmi_device *sdev, u8 reg) if (err) return err; - if (sn->polled || sn->select_polled) { + if (sn->polled || sn->select_polled || READ_ONCE(sn->cold_polled)) { int poll_err; if (sn->atomic) @@ -66,8 +66,13 @@ static int regmap_sn201202x_select_reg(struct spmi_device *sdev, u8 reg) return err ?: poll_err; } - if (!wait_for_completion_timeout(&sn->select_completion, msecs_to_jiffies(100))) - return -ETIMEDOUT; + if (!wait_for_completion_timeout(&sn->select_completion, msecs_to_jiffies(100))) { + /* The selection may have completed without its interrupt. */ + err = spmi_register_read(sdev, 0, &val); + if (!err && val == reg) + return 0; + return err ?: -ETIMEDOUT; + } while (--attempts) { err = spmi_register_read(sdev, 0, &val); @@ -555,7 +560,7 @@ static int sn201202x_wake_poll(struct sn201202x *sn, bool poll) static int sn201202x_wake(struct sn201202x *sn) { - return sn201202x_wake_poll(sn, sn->polled); + return sn201202x_wake_poll(sn, sn->polled || READ_ONCE(sn->cold_polled)); } VISIBLE_IF_KUNIT int __maybe_unused sn201202x_runtime_suspend(struct device *dev) @@ -581,7 +586,7 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_runtime_suspend(struct device *dev reinit_completion(&sn->sleep_completion); if (spmi_command_sleep(sn->sdev)) goto retry; - if (!sn->polled && + if (!sn->polled && !READ_ONCE(sn->cold_polled) && !wait_for_completion_timeout(&sn->sleep_completion, msecs_to_jiffies(100))) { /* * It accepted SLEEP, so it may be asleep without having @@ -836,6 +841,9 @@ static void sn201202x_remove(struct spmi_device *device) struct tps6598x *tps = spmi_device_get_drvdata(device); struct sn201202x *sn = tps_to_sn(tps); + /* The hold freeze took on one the restore left alone */ + if (sn->cold_restored) + pm_runtime_put_noidle(tps->dev); sn201202x_idle_sleep_stop(tps); tipd_remove(tps); spmi_command_sleep(sn->sdev); @@ -847,6 +855,10 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_resume(struct device *dev) struct sn201202x *sn = tps_to_sn(tps); int err; + /* Its suspend left it alone too: interrupt masked, still held awake */ + if (sn->cold_restored) + return 0; + if (READ_ONCE(sn->sleep_watch)) { /* Waits for a running sleep_event() */ disable_irq(tps->irq); @@ -880,6 +892,10 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) bool watch; int err, ret; + /* A controller a resume from disk left dead waits for its new probe. */ + if (sn->cold_restored) + return 0; + /* * A sleeping controller cannot be read, so its events must not be * handled until it is awake again. An interrupt arriving meanwhile is @@ -920,7 +936,7 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) err = spmi_command_sleep(sn->sdev); if (err) goto out_resume; - if (!sn->polled && + if (!sn->polled && !READ_ONCE(sn->cold_polled) && !wait_for_completion_timeout(&sn->sleep_completion, msecs_to_jiffies(100))) { err = -ETIMEDOUT; goto out_wake; @@ -946,12 +962,86 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) } EXPORT_SYMBOL_IF_KUNIT(sn201202x_suspend); +/* + * A resume from hibernation is a cold boot: the boot loader and the kernel + * that loaded the image have taken the controller through their own bring-up, + * so the state saved in the image (its sleep, the latched events) no longer + * exists and waking it the suspend way times out. Leave it alone with its + * interrupt still masked; the controller is probed again from scratch after + * the restore (userspace unbinds and binds it, e.g. from a sleep hook). + */ +static int __maybe_unused sn201202x_restore(struct device *dev) +{ + struct tps6598x *tps = dev_get_drvdata(dev); + struct sn201202x *sn = tps_to_sn(tps); + int err; + const char *step; + + /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ + if (!pm_hibernate_is_restored()) + return sn201202x_resume(dev); + /* + * An earlier restore left it for a new probe: freeze took no hold + * and left its interrupt alone, so there is nothing to give back. + */ + if (sn->cold_restored) + return 0; + + /* + * The cold boot left the controller awake, so a wakeup command raises + * no wake interrupt, and its register selections do not complete by + * interrupt reliably: talk to it polled, as to a controller without + * interrupts (selections, wake and sleep), until it is probed again. + */ + WRITE_ONCE(sn->cold_polled, true); + step = "wake"; + err = sn201202x_wake(sn); + if (err == -ETIMEDOUT && !sn->polled) { + step = "awake check"; + err = sn201202x_wake_poll(sn, true); + } + if (!err) { + step = "set-up"; + err = tipd_reinit_after_cold_boot(tps); + } + if (err) { + /* + * Interrupt still masked and held awake from freeze: nothing + * reads a dead chip, and it does not sleep while idle. + */ + sn->cold_restored = true; + dev_warn(dev, "resume from disk: restore failed at %s (%d), controller left for a new probe\n", + step, err); + return 0; + } + /* The cold boot reset the controller, which also removed its mitigation. */ + scoped_guard(mutex, &tps->lock) + sn201202x_ldcm_invalidate(tps); + if (tps->irq) + enable_irq(tps->irq); + /* Give back the hold freeze took, as resume does */ + pm_runtime_put_autosuspend(dev); + sn->roles_pending = true; + dev_info(dev, "resume from disk: controller restored, port revalidation waits for the USB role switch\n"); + return 0; +} + +/* + * A port still waiting for its role switch at the next system sleep gives up + * the wait: that sleep's resume revalidates it with whatever switch is there (a + * missing one only refuses). A controller the restore from disk left for a new + * probe is not asked for its connector state. + */ static int sn201202x_prepare(struct device *dev) { struct tps6598x *tps = dev_get_drvdata(dev); struct sn201202x *sn = tps_to_sn(tps); int ret; + sn->roles_pending = false; + if (sn->cold_restored) + return tipd_prepare(dev); + cancel_delayed_work_sync(&sn->ldcm_work); mutex_lock(&tps->lock); ret = sn201202x_ldcm_update(tps); @@ -974,19 +1064,77 @@ static int sn201202x_prepare(struct device *dev) static void sn201202x_complete(struct device *dev) { struct tps6598x *tps = dev_get_drvdata(dev); + struct sn201202x *sn = tps_to_sn(tps); + /* A controller the restore from disk left alone is probed again instead. */ + if (sn->cold_restored) + return; /* Refresh the detector snapshot before cable revalidation is thawed. */ + scoped_guard(mutex, &tps->lock) + sn201202x_ldcm_refresh(tps); + /* A port whose USB role switch is not back yet: hib_resume below starts it. */ + if (!sn->roles_pending) + tipd_complete(dev); +} + +/* + * hib_resume: the state a resume from disk left the controller in: "none", + * "roles-pending" (restored, waiting for the USB controller to be bound + * again) or "failed" (to be probed again). Writing "roles" once the USB + * controller is bound looks its role switch up again and revalidates the + * port, without taking the port (and a display on it) down. + */ +static ssize_t hib_resume_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct tps6598x *tps = dev_get_drvdata(dev); + struct sn201202x *sn = tps_to_sn(tps); + + return sysfs_emit(buf, "%s\n", sn->cold_restored ? "failed" : + sn->roles_pending ? "roles-pending" : "none"); +} + +static ssize_t hib_resume_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct tps6598x *tps = dev_get_drvdata(dev); + struct sn201202x *sn = tps_to_sn(tps); + int ret; + + if (!sysfs_streq(buf, "roles")) + return -EINVAL; + if (!sn->roles_pending) + return -EALREADY; + ret = tipd_refresh_role_switch(tps); + if (ret) + return ret == -EPROBE_DEFER ? -EAGAIN : ret; + sn->roles_pending = false; scoped_guard(mutex, &tps->lock) sn201202x_ldcm_refresh(tps); tipd_complete(dev); + dev_info(dev, "resume from disk: USB role switch looked up again, port revalidates\n"); + return count; } +static DEVICE_ATTR_RW(hib_resume); + +static struct attribute *sn201202x_attrs[] = { + &dev_attr_hib_resume.attr, + NULL +}; +ATTRIBUTE_GROUPS(sn201202x); MODULE_DEVICE_TABLE(of, sn201202x_of_match); static const struct dev_pm_ops sn201202x_pm_ops = { .prepare = pm_sleep_ptr(sn201202x_prepare), .complete = pm_sleep_ptr(sn201202x_complete), - SET_SYSTEM_SLEEP_PM_OPS(sn201202x_suspend, sn201202x_resume) +#ifdef CONFIG_PM_SLEEP + .suspend = sn201202x_suspend, + .resume = sn201202x_resume, + .freeze = sn201202x_suspend, + .thaw = sn201202x_resume, + .poweroff = sn201202x_suspend, + .restore = sn201202x_restore, +#endif SET_RUNTIME_PM_OPS(sn201202x_runtime_suspend, sn201202x_runtime_resume, NULL) }; @@ -1000,6 +1148,7 @@ static struct spmi_driver sn201202x_driver = { * port; let the ports of one machine probe at the same time. */ .probe_type = PROBE_PREFER_ASYNCHRONOUS, + .dev_groups = sn201202x_groups, }, .probe = sn201202x_probe, .remove = sn201202x_remove, diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 68d246b7397e1..2abaa1a6acf7b 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -508,6 +508,12 @@ struct sn201202x { bool ldcm_mitigated; /* ldcm_mitigated matches the controller (unknown after a reset) */ bool ldcm_mitigation_known; + /* Restored from disk: left untouched until it is probed again. */ + bool cold_restored; + /* Restored from disk: revalidation waits for the new USB role switch. */ + bool roles_pending; + /* Restored from disk: the controller is driven polled until a new probe. */ + bool cold_polled; }; extern const struct tipd_data *tipd_hw_variants[]; @@ -519,6 +525,8 @@ int tipd_prepare(struct device *dev); void tipd_complete(struct device *dev); int tipd_suspend(struct tps6598x *tps); int tipd_resume(struct tps6598x *tps); +int tipd_refresh_role_switch(struct tps6598x *tps); +int tipd_reinit_after_cold_boot(struct tps6598x *tps); /* Internal to tps6598x-core */ int tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len); diff --git a/include/linux/phy/apple-t6040-cio.h b/include/linux/phy/apple-t6040-cio.h new file mode 100644 index 0000000000000..14a8132dbeac9 --- /dev/null +++ b/include/linux/phy/apple-t6040-cio.h @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * Apple T6040 Type-C PHY: the USB4 host router's view of the PHY's CIO + * (USB4/Thunderbolt) mode. The Type-C controller puts the PHY into CIO + * through the mux before it hands the cable to the router, and takes it out + * after the router has stopped. The router uses these calls to check that + * order and to tell the PHY whether it runs, which the PHY needs before it + * takes CIO down for system sleep. @phy_np is the PHY node the router's + * "apple,atc-phy" property points to. + */ +#ifndef __LINUX_PHY_APPLE_T6040_CIO_H +#define __LINUX_PHY_APPLE_T6040_CIO_H + +#include +#include +#include + +struct device_node; + +#if IS_REACHABLE(CONFIG_PHY_APPLE_T6040_USB2) +/* + * 1: CIO up and tuned by the PHY firmware; 0: not in CIO; -EPROBE_DEFER: + * the PHY is not bound yet; -ENODEV: @phy_np is not a T6040 Type-C PHY. + */ +int apple_t6040_cio_state(struct device_node *phy_np); + +/* + * Before the router starts: marks it running on the PHY's CIO link. + * -EAGAIN unless CIO is up; -EPROBE_DEFER / -ENODEV as above. + */ +int apple_t6040_cio_router_started(struct device_node *phy_np); + +/* + * After every router stop, once ACIO is powered down (unplug, error, and the + * stop for system sleep, which must come before any device's .suspend, + * .freeze or .poweroff). Undoes apple_t6040_cio_router_started(). + */ +void apple_t6040_cio_router_stopped(struct device_node *phy_np); + +/* + * The router carries (@on) or no longer carries USB3 for the port's USB + * controller: the controller's PIPE follows (the USB3 mode 3). -EAGAIN + * when @on and the router is not marked running. + */ +int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on); +#else +static inline int apple_t6040_cio_state(struct device_node *phy_np) +{ + return -ENODEV; +} + +static inline int apple_t6040_cio_router_started(struct device_node *phy_np) +{ + return -ENODEV; +} + +static inline void apple_t6040_cio_router_stopped(struct device_node *phy_np) +{ +} + +static inline int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on) +{ + return -ENODEV; +} +#endif + +#endif diff --git a/tools/asahi/tb-dp-repair-tests.py b/tools/asahi/tb-dp-repair-tests.py index 16932f99efa51..9e6651a046691 100644 --- a/tools/asahi/tb-dp-repair-tests.py +++ b/tools/asahi/tb-dp-repair-tests.py @@ -91,6 +91,8 @@ def function(text, name): static void list_del_init(struct list_head*l){} static bool tb_port_is_dpin(struct tb_port*p){return p->is_dpin;} static void tb_dp_tunnel_notify(struct tb_tunnel*t,bool active){} +/* Only a USB3 tunnel from the host router is reported; these cases retire DP tunnels. */ +static void tb_usb3_host_tunnel_changed(struct tb_tunnel*t,bool on){CHECK(t->type==TB_TUNNEL_DP && !on);} static void tb_tunnel_deactivate(struct tb_tunnel*t){} static void tb_detach_bandwidth_group(struct tb_port*p){} static void tb_switch_dealloc_dp_resource(struct tb_switch*s,struct tb_port*p){} diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py index 4dcdc35ed409b..52037e24b808a 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py @@ -481,6 +481,7 @@ def function(text, name): #define IS_ERR(p) ((intptr_t)(p)<0) #define ERR_PTR(e) ((void *)(intptr_t)(e)) #define dev_info(...) ((void)0) +#define dev_dbg(...) ((void)0) #define PTR_ERR(p) ((int)(intptr_t)(p)) #define IS_ERR_OR_NULL(p) (!(p)||IS_ERR(p)) #define container_of(p,t,m) ((t *)((char *)(p)-offsetof(t,m))) @@ -533,6 +534,8 @@ def function(text, name): static void *fwnode_typec_thunderbolt_switch_get(struct fwnode_handle *f) { (void)f;if(switch_error)return (void *)(intptr_t)switch_error;switches++;return &switches; } +/* The lookup above finds a router or fails, so none is taken for a disabled one. */ +static bool cd321x_tbt_router_described(struct fwnode_handle *f) { (void)f;assert(!"the router lookup found none");return false; } static int cd321x_mux_set(struct cd321x *c,bool *held) { assert(c->state.mode==TYPEC_STATE_SAFE); *held=mux_error==-ETIMEDOUT&&mux_transient;return mux_error; From e88c115cafe9ffd20f2eba1c498494396314bcf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 12:04:01 +0100 Subject: [PATCH 213/269] phy: apple: t6040: write the lane tunables again after each DP link power-up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A lane's registers are back at their reset values every time the link sequence has powered the lane down. The ADT's lane0/lane1 tables and the DPTX lane tunables were written once, when the PHY entered DP mode, so only the first link at the first rate ran with them: after a rate change, a failed training or one DPMS cycle they were gone for every link that followed. Measured on J616s, port 0 with a DP display: LANE0 RX_CFG +0x90/0x94/0xa8 and TX +0xcc read 0x023a1781/0x00081f81/0x00200001/0x023a1780 with the first link, and 0x02021781/0x00001f81/0x00000001/0x02021780 after DPMS off and on. Write them again once the link sequence has the lanes up. Pin D keeps its lanes up under USB3's firmware and is left alone. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 57 ++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index df861ecf317f4..811a27bec6f2e 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -1277,23 +1277,28 @@ static void t6040_usb3_switch_unregister(void *sw) */ /* The fixed DPTX tunable writes (apply_dptx_tunables, shared with USB3). */ +static const struct { u32 off, clear, set; } t6040_dptx_tunables[] = { + { 0x00800, 0x000003f0, 0x000001c0 }, /* AUS40CMN_DIG RCAL0 */ + { 0x00818, 0x0000f000, 0x00003000 }, /* AUS40CMN_DIG RCAL5 */ + { 0x03888, 0x000000ff, 0x00000027 }, /* CIO4PLL FREQDESCRIPTOR_0_C */ + { 0x28004, 0x00000038, 0x00000008 }, /* AUX_LDO_CTRL */ + { 0x03adc, 0x00003ff0, 0x00001760 }, /* CIO4PLL CFG_FRACN_FUNC */ + /* The lane entries, T6040_DPTX_LANE_TUNABLES of them, come last. */ + { 0x0e0cc, 0x01f80000, 0x00380000 }, /* LANE0 TX_EQ_CTRL_REG11 */ + { 0x1e0cc, 0x01f80000, 0x00380000 }, /* LANE1 TX_EQ_CTRL_REG11 */ + { 0x0d090, 0x01f80000, 0x00380000 }, /* LANE0 RX_CFG TX_EQ_CTRL_REG11 */ + { 0x1d090, 0x01f80000, 0x00380000 }, /* LANE1 RX_CFG TX_EQ_CTRL_REG11 */ +}; + +#define T6040_DPTX_LANE_TUNABLES 4 + static void t6040_dp_fixed_tunables(void __iomem *core) { - static const struct { u32 off, clear, set; } dptx[] = { - { 0x00800, 0x000003f0, 0x000001c0 }, /* AUS40CMN_DIG RCAL0 */ - { 0x00818, 0x0000f000, 0x00003000 }, /* AUS40CMN_DIG RCAL5 */ - { 0x03888, 0x000000ff, 0x00000027 }, /* CIO4PLL FREQDESCRIPTOR_0_C */ - { 0x28004, 0x00000038, 0x00000008 }, /* AUX_LDO_CTRL */ - { 0x03adc, 0x00003ff0, 0x00001760 }, /* CIO4PLL CFG_FRACN_FUNC */ - { 0x0e0cc, 0x01f80000, 0x00380000 }, /* LANE0 TX_EQ_CTRL_REG11 */ - { 0x1e0cc, 0x01f80000, 0x00380000 }, /* LANE1 TX_EQ_CTRL_REG11 */ - { 0x0d090, 0x01f80000, 0x00380000 }, /* LANE0 RX_CFG TX_EQ_CTRL_REG11 */ - { 0x1d090, 0x01f80000, 0x00380000 }, /* LANE1 RX_CFG TX_EQ_CTRL_REG11 */ - }; size_t i; - for (i = 0; i < ARRAY_SIZE(dptx); i++) - t6040_usb2_mask32(core + dptx[i].off, dptx[i].clear, dptx[i].set); + for (i = 0; i < ARRAY_SIZE(t6040_dptx_tunables); i++) + t6040_usb2_mask32(core + t6040_dptx_tunables[i].off, t6040_dptx_tunables[i].clear, + t6040_dptx_tunables[i].set); } /* aciophy_xbar_config for PHY mode 3 (DP on all four lanes). */ @@ -1562,6 +1567,29 @@ static int t6040_dp_seq_step(struct t6040_usb2_phy *tphy, const struct atc_op *s return ret; } +/* The ADT's lane0 and lane1 tables in t6040_usb3_tunable_list. */ +#define T6040_USB3_TUNABLE_LANE0 4 +static_assert(T6040_USB3_TUNABLE_LANE0 + 2 == T6040_DP_NR_GENERIC_TUNABLES); + +/* + * A lane's registers are back at their reset values each time the lane has been powered + * down: after one link at a rate, one DPMS cycle or one failed training, the lane tables + * of the ADT and the DPTX lane tunables were gone for every link that followed. Write them + * again once the link sequence has the lanes up. + */ +static void t6040_dp_lane_tunables(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + size_t i; + + for (i = T6040_USB3_TUNABLE_LANE0; i < T6040_USB3_TUNABLE_LANE0 + 2; i++) + apple_tunable_apply(core, tphy->tunables[i]); + for (i = ARRAY_SIZE(t6040_dptx_tunables) - T6040_DPTX_LANE_TUNABLES; + i < ARRAY_SIZE(t6040_dptx_tunables); i++) + t6040_usb2_mask32(core + t6040_dptx_tunables[i].off, t6040_dptx_tunables[i].clear, + t6040_dptx_tunables[i].set); +} + /* Take the link down if it is up, then bring it up at rate index @idx (-1: off). */ static int t6040_dp_set_rate(struct t6040_usb2_phy *tphy, int idx) { @@ -1605,6 +1633,9 @@ static int t6040_dp_set_rate(struct t6040_usb2_phy *tphy, int idx) if (ret) return ret; } + /* Pin D leaves the lanes to USB3's firmware, which keeps them up. */ + if (!tphy->dp_mode_two_lanes) + t6040_dp_lane_tunables(tphy); ret = t6040_dp_configure_clks(tphy); if (ret) return ret; From 3a6a1db48cf589f11c4f6fcf82c15041e9cd467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 12:30:40 +0100 Subject: [PATCH 214/269] phy: apple: t6040: shut the PHY down when four-lane DP powers off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Powering the DP phy off after DP on all four lanes puts only a Type-C port's PHY to sleep, with no step for a link that is still up: the fixed DP PHY of the HDMI converter stays as it ran. A PHY left that way is what an S2R found active, and the sleep engine stalled on it unless the sleep controller wrote the CFG block behind the driver's back; the next power-on programmed a PHY that had not been through its reset, with CFG_S2R_FSM_EN still set after a wake. Closing a DP client takes the DP transmitter and AUX down, then both TX lanes and the common block of the PHY, and after 200 us puts the CFG block back to its boot state (clock request released, PHY and APB reset, clamp, both sleep requests, S2R FSM enable cleared). This mode runs no coprocessor, so there is nothing else to stop. Do the same on every port that ran four lanes; pin D, which shares the running USB3 PHY, is left as it is. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 811a27bec6f2e..3407978763ed1 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -1736,6 +1736,32 @@ static int t6040_dp_power_on(struct phy *phy) return 0; } +/* + * The PHY's close for DP on all four lanes (with a DP client: the DP + * transmitter's shutdown, the AUX shutdown, the PHY shutdown of mode 3): a link still up goes + * down, then both TX lanes and the common block (part A), and after its 200 us the CFG + * block returns to its boot state (part B; no coprocessor to stop in this mode). The AUX + * block has no step of its own here: it goes into reset and behind the clamp with the PHY. + * + * Without this the fixed DP PHY of the HDMI converter stays as it ran once it is powered + * off; a Type-C port's was put to sleep, with no step for a link still up. The sleep engine + * of an S2R stalled on a port left as it ran, and the next power-on programmed a PHY that + * had not been through its reset. + */ +static void t6040_dp_shutdown(struct t6040_usb2_phy *tphy) +{ + int ret = t6040_dp_set_rate(tphy, -1); + + if (ret) + dev_warn(tphy->dev, "DP link not taken down before the PHY shutdown: %d\n", ret); + tphy->dp_rate = -1; + t6040_phy_tx_cmn_shutdown(tphy); + fsleep(200); + t6040_usb3_sleep(tphy); + dev_dbg(tphy->dev, "DP PHY shut down: cfg %#x/%#x\n", + readl(t6040_cfg(tphy) + T6040_CFG_CTRL), readl(t6040_cfg(tphy) + T6040_CFG_STATUS)); +} + static int t6040_dp_power_off(struct phy *phy) { struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); @@ -1754,9 +1780,8 @@ static int t6040_dp_power_off(struct phy *phy) * sleeping PHY, as on a fresh attach. Booting the coprocessor's * firmware for the first time over the DP state crashes it. */ - if (tphy->dp_up && !tphy->dp_mode_two_lanes && !t6040_dp_only(tphy)) { - t6040_phy_tx_cmn_shutdown(tphy); - t6040_usb3_sleep(tphy); + if (tphy->dp_up && !tphy->dp_mode_two_lanes) { + t6040_dp_shutdown(tphy); dev_info(tphy->dev, "DP PHY down, PHY asleep\n"); } tphy->dp_up = false; From 67ddba59c5c2b25c78e36dd3eace9d872065c005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 14:08:17 +0100 Subject: [PATCH 215/269] phy: apple: t6040: split the USB2 sequences and the controller's reset from their operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The USB2 host sequence, the USB2 power-down and the two halves of the controller's reset sit inside the phy and reset operations, behind their checks of what the consumer holds. A sleep that keeps the host in place has to run the same register sequences while the consumers keep their references. Move each into a helper that is called with the PHY's lock held. The operations keep their checks and call the helpers: the registers are written and read back in the same order as before. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 111 +++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 3407978763ed1..28d0b4395e6c0 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -370,19 +370,12 @@ static u32 t6040_usb2_sig_want(struct t6040_usb2_phy *tphy) return want & T6040_USB2_SIG_INIT; } -static int t6040_usb2_power_on(struct phy *phy) +/* The USB2 host sequence itself. A field that did not take leaves the PHY failed. */ +static int t6040_usb2_on_locked(struct t6040_usb2_phy *tphy) { - struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); u32 status, ctl, sig, tune, mode, sig_want; - guard(mutex)(&tphy->lock); - if (tphy->shutdown_failed) - return -EIO; - - if (tphy->mode != PHY_MODE_USB_HOST) - return -EINVAL; - if (tphy->powered) - return 0; + lockdep_assert_held(&tphy->lock); /* * eUSB2 PHY init, in this exact order. @@ -459,7 +452,6 @@ static int t6040_usb2_power_on(struct phy *phy) !(status & T6040_USB2_EVT_CTL_EN)) goto failed; - tphy->powered = true; dev_info(tphy->dev, "USB2 host sequence complete\n"); return 0; failed: @@ -467,21 +459,37 @@ static int t6040_usb2_power_on(struct phy *phy) return -EIO; } +static int t6040_usb2_power_on(struct phy *phy) +{ + struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); + int ret; + + guard(mutex)(&tphy->lock); + if (tphy->shutdown_failed) + return -EIO; + + if (tphy->mode != PHY_MODE_USB_HOST) + return -EINVAL; + if (tphy->powered) + return 0; + + ret = t6040_usb2_on_locked(tphy); + if (ret) + return ret; + tphy->powered = true; + return 0; +} + /* * Power the PHY down: port reset (held 5 ms), USBCTL mode 4, SIDDQ, PHY * reset, then gate the APB and reference clocks. The host controller must be * halted first; this is not a substitute for an xHCI halt. */ -static int t6040_usb2_power_off(struct phy *phy) +static int t6040_usb2_off_locked(struct t6040_usb2_phy *tphy) { - struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); u32 ctl, mode, tune; - guard(mutex)(&tphy->lock); - if (tphy->shutdown_failed) - return -EIO; - if (!tphy->powered) - return 0; + lockdep_assert_held(&tphy->lock); tphy->shutdown_failed = true; if (!(readl(tphy->usb2 + T6040_USB2_CTL) & T6040_USB2_CTL_PORT_RESET)) { @@ -521,6 +529,23 @@ static int t6040_usb2_power_off(struct phy *phy) T6040_USB2_MISCTUNE_REFCLK_GATE_OFF)) return -EIO; tphy->shutdown_failed = false; + return 0; +} + +static int t6040_usb2_power_off(struct phy *phy) +{ + struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); + int ret; + + guard(mutex)(&tphy->lock); + if (tphy->shutdown_failed) + return -EIO; + if (!tphy->powered) + return 0; + + ret = t6040_usb2_off_locked(tphy); + if (ret) + return ret; tphy->powered = false; return 0; } @@ -2888,6 +2913,36 @@ static int t6040_usb3_probe(struct t6040_usb2_phy *tphy, struct platform_device return 0; } +/* The controller's reset, see t6040_usb2_reset_assert(). */ +static void t6040_dwc3_reset_hold_locked(struct t6040_usb2_phy *tphy) +{ + lockdep_assert_held(&tphy->lock); + t6040_usb2_clear32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, + T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N); + t6040_usb2_set32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, + T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); +} + +/* -EIO if the register does not read back released: the controller may not be there. */ +static int t6040_dwc3_reset_release_locked(struct t6040_usb2_phy *tphy) +{ + u32 value; + + lockdep_assert_held(&tphy->lock); + t6040_usb2_clear32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, + T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); + t6040_usb2_set32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, + T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N); + + value = readl(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN); + if (value == U32_MAX || + (value & (T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N | + T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN)) != + T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N) + return -EIO; + return 0; +} + /* * dwc3-apple asserts this at probe, deasserts it before dwc3_core_init() * (which then powers this PHY on), and asserts it again on stop. As in @@ -2907,10 +2962,7 @@ static int t6040_usb2_reset_assert(struct reset_controller_dev *rcdev, return ret; guard(mutex)(&tphy->lock); - t6040_usb2_clear32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, - T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N); - t6040_usb2_set32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, - T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); + t6040_dwc3_reset_hold_locked(tphy); if (tphy->dwc3_running) { tphy->dwc3_running = false; pm_runtime_put(tphy->dev); @@ -2923,7 +2975,6 @@ static int t6040_usb2_reset_deassert(struct reset_controller_dev *rcdev, { struct t6040_usb2_phy *tphy = container_of(rcdev, struct t6040_usb2_phy, rcdev); - u32 value; int ret; PM_RUNTIME_ACQUIRE(tphy->dev, pm); @@ -2934,17 +2985,9 @@ static int t6040_usb2_reset_deassert(struct reset_controller_dev *rcdev, guard(mutex)(&tphy->lock); if (tphy->shutdown_failed) return -EIO; - t6040_usb2_clear32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, - T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); - t6040_usb2_set32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN, - T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N); - - value = readl(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN); - if (value == U32_MAX || - (value & (T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N | - T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN)) != - T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N) - return -EIO; + ret = t6040_dwc3_reset_release_locked(tphy); + if (ret) + return ret; if (!tphy->dwc3_running) { pm_runtime_get_noresume(tphy->dev); tphy->dwc3_running = true; From 1c29370a8ea75ed9c1704d704de9d3c7c52d2e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 14:15:19 +0100 Subject: [PATCH 216/269] phy: apple: t6040: take a Type-C port's USB host through a deep sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every USB device leaves the bus at a T6040 S2R and comes back as a new one: dwc3-apple stops the host before a deep sleep and starts it again after, because only the T8140 Type-C PHY (atc.c) can keep a host in place. A mounted USB disk loses its block device that way. The T6040 PHY has no hibernation to take the controller through, and its USB3 bring-up boots the PHY's coprocessor, which can't be done with interrupts off. What it can do is the stop and the start of a cable session below an xHCI device that stays: USB3 down with its PIPE parked, USB2 off and the controller in reset once xHCI has suspended; the reset released before anything touches the core after the sleep; USB2 and USB3 back once the core is set up again. xHCI then starts over and the USB core resets the devices it finds again. Offer these steps to the controller's glue next to atc.c's calls (linux/phy/apple-atc.h), with apple_typec_phy_sleep_*() in front of both drivers: each one answers for its own PHYs and doesn't know the other's. atc.c is not changed. The new result APPLE_ATCPHY_SLEEP_RESET tells the glue that the core went through its reset and that the PHY comes back after the core, with apple_typec_phy_sleep_power_up(). The port has to do nothing but USB. DisplayPort on the lanes, with any pin assignment, and USB4/Thunderbolt have clients of their own: their ports keep stopping the host. The register sequences are those of power_off, power_on and the reset, in the order dwc3-apple's stop and start run them, so the CFG block is parked at the sleep's commit as after any detach, and they are the same whether or not the SoC then loses power. The PHY's firmware can also save and restore its state around the sleep; that is not used here. A coprocessor that needs the power cycle of its domains doesn't get it under a system resume, where they stay on: the wake fails then, and the host restarts once the system has resumed. A PHY that didn't come back from such a sleep stops the host for the later ones. The KUnit tests cover which port states sleep this way. On a J616s (MacBook Pro, M4 Pro) a dock with its hub, Ethernet adapter and card reader on one port and a USB3 disk on another went through repeated suspend-to-RAM cycles: every device was reset-resumed and kept its device number, the Ethernet interface its address and the disk its block device. Signed-off-by: František Balázsy --- drivers/phy/apple/Kconfig | 7 +- drivers/phy/apple/t6040-recovery-test.c | 118 ++++++++++- drivers/phy/apple/t6040-usb2.c | 269 +++++++++++++++++++++++- include/linux/phy/apple-atc.h | 139 ++++++++++++ 4 files changed, 527 insertions(+), 6 deletions(-) diff --git a/drivers/phy/apple/Kconfig b/drivers/phy/apple/Kconfig index 381c830e63978..196ded718c165 100644 --- a/drivers/phy/apple/Kconfig +++ b/drivers/phy/apple/Kconfig @@ -60,9 +60,10 @@ config PHY_APPLE_T6040_KUNIT_TEST default KUNIT_ALL_TESTS help Test the bounded hard-recovery state machine for a missed Power Down - acknowledgment, a failed firmware tune and retry exhaustion. The tests - access no hardware and run when the PHY driver is loaded. They are only - useful for kernel developers. + acknowledgment, a failed firmware tune and retry exhaustion, and which + port states the PHY takes through a deep sleep with the USB host kept. + The tests access no hardware and run when the PHY driver is loaded. + They are only useful for kernel developers. config PHY_APPLE_DPTX tristate "Apple DPTX PHY" diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 552afba86f117..197338fdb007e 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -1,5 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause -/* Included by t6040-usb2.c to test its recovery state machine. */ +/* + * Included by t6040-usb2.c to test its recovery state machine and which ports + * it takes through a sleep with the host kept. + */ #include @@ -47,4 +50,115 @@ static struct kunit_suite t6040_recovery_test_suite = { .test_cases = t6040_recovery_test_cases, }; -kunit_test_suite(t6040_recovery_test_suite); +/* A Type-C port's PHY with a USB2 host running on it, as dwc3-apple's start leaves it. */ +static struct t6040_usb2_phy *t6040_sleep_test_phy(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = kunit_kzalloc(test, sizeof(*tphy), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, tphy); + /* Never dereferenced: only that the PHY has a pipehandler counts. */ + tphy->pipehandler = (void __iomem *)tphy; + tphy->mode = PHY_MODE_USB_HOST; + tphy->powered = true; + tphy->dwc3_running = true; + return tphy; +} + +/* USB2 alone, and USB3 with the PIPE on the PHY, are the two states that sleep in place. */ +static void t6040_sleep_usb_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + tphy->usb3_on = true; + tphy->usb3_pipe = true; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + tphy->usb3_up = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->pipe_switched = true; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); +} + +/* No host, no controller out of reset, or the fixed DisplayPort PHY: nothing to keep. */ +static void t6040_sleep_no_host_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + tphy->powered = false; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->powered = true; + tphy->dwc3_running = false; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->dwc3_running = true; + tphy->pipehandler = NULL; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); +} + +/* DisplayPort on the lanes, with any pin assignment, and USB4/Thunderbolt stop the host. */ +static void t6040_sleep_other_modes_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + tphy->dp_alt = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->dp_two_lanes = true; + tphy->usb3_up = true; + tphy->pipe_switched = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->dp_alt = false; + tphy->dp_two_lanes = false; + tphy->dp_up = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->dp_up = false; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + + tphy->usb3_up = false; + tphy->pipe_switched = false; + tphy->usb3_lent = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->cio_up = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->usb3_lent = false; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->cio_up = false; + tphy->pipe_tunneled = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->pipe_tunneled = false; + tphy->router_running = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); +} + +/* A PHY that failed, is down for a sleep already, or didn't come back from one. */ +static void t6040_sleep_failed_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + tphy->shutdown_failed = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->shutdown_failed = false; + tphy->sleep_no_keep = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->sleep_no_keep = false; + tphy->sleep_down = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->sleep_down = false; + tphy->sleep_reset = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + tphy->sleep_reset = false; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); +} + +static struct kunit_case t6040_sleep_test_cases[] = { + KUNIT_CASE(t6040_sleep_usb_test), + KUNIT_CASE(t6040_sleep_no_host_test), + KUNIT_CASE(t6040_sleep_other_modes_test), + KUNIT_CASE(t6040_sleep_failed_test), + {} +}; + +static struct kunit_suite t6040_sleep_test_suite = { + .name = "phy-apple-t6040-sleep", + .test_cases = t6040_sleep_test_cases, +}; + +kunit_test_suites(&t6040_recovery_test_suite, &t6040_sleep_test_suite); diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 28d0b4395e6c0..3259d8e50166e 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -315,6 +316,15 @@ struct t6040_usb2_phy { bool router_running; /* the USB4 router runs on this port's CIO link */ bool usb3_tunnel_req; /* the router carries USB3 for the controller */ bool cio_flip; + + /* A sleep with the host kept, see apple_t6040_atcphy_sleep_prepare() */ + bool sleep_armed; /* taken on by the prepare, until the finish */ + bool sleep_down; /* USB2 off and USB3 asleep for it, behind their consumers */ + bool sleep_reset; /* the controller held in reset for it, behind its consumer */ + bool sleep_usb3; /* USB3 ran when it went down, and comes back with USB2 */ + bool sleep_waking; /* in that wake: no power cycle of the PHY's domains */ + bool sleep_no_keep; /* a wake of its own failed: the host stops for later sleeps */ + int sleep_ret; /* what the wake has come to, for the controller's glue */ }; static inline void t6040_usb2_mask32(void __iomem *reg, u32 mask, u32 set) @@ -543,10 +553,12 @@ static int t6040_usb2_power_off(struct phy *phy) if (!tphy->powered) return 0; + /* Also the stop of a host whose PHY a sleep left down: the sequence only repeats. */ ret = t6040_usb2_off_locked(tphy); if (ret) return ret; tphy->powered = false; + tphy->sleep_down = false; return 0; } @@ -1119,6 +1131,11 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) lockdep_assert_held(&tphy->lock); if (!tphy->usb3_ok || tphy->usb3_up) return; + /* Down for a sleep with the host kept: USB3 comes back with the wake, after USB2. */ + if (tphy->sleep_down) { + dev_dbg(tphy->dev, "USB3: the PHY is down for a sleep, not started\n"); + return; + } /* DP mode owns all four lanes; USB3 comes back in t6040_usb3_reclaim_locked(). */ if (tphy->dp_up) { dev_dbg(tphy->dev, "USB3: lanes held by DP, USB2 only for now\n"); @@ -1172,7 +1189,11 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) t6040_usb3_shutdown_lanes(tphy); t6040_usb3_firmware_power_down(tphy); t6040_usb3_sleep(tphy); - if (t6040_recovery_retry(&tphy->recovery)) { + /* + * The retry power-cycles the PHY's domains, which don't go off under a system + * resume: a wake with the host kept leaves it to the host's restart after it. + */ + if (t6040_recovery_retry(&tphy->recovery) && !tphy->sleep_waking) { cycle = true; goto retry; } @@ -2963,6 +2984,8 @@ static int t6040_usb2_reset_assert(struct reset_controller_dev *rcdev, guard(mutex)(&tphy->lock); t6040_dwc3_reset_hold_locked(tphy); + /* From here the reset is its consumer's again, whatever a sleep did with it. */ + tphy->sleep_reset = false; if (tphy->dwc3_running) { tphy->dwc3_running = false; pm_runtime_put(tphy->dev); @@ -2988,6 +3011,7 @@ static int t6040_usb2_reset_deassert(struct reset_controller_dev *rcdev, ret = t6040_dwc3_reset_release_locked(tphy); if (ret) return ret; + tphy->sleep_reset = false; if (!tphy->dwc3_running) { pm_runtime_get_noresume(tphy->dev); tphy->dwc3_running = true; @@ -3125,6 +3149,249 @@ static int t6040_usb2_probe(struct platform_device *pdev) return PTR_ERR_OR_ZERO(provider); } +/* ---- a sleep with the host kept (linux/phy/apple-atc.h) ---- + * + * A deep sleep powers the SoC down, and with it this PHY, the controller and + * its xHC. dwc3-apple used to stop the host for it, which removes every USB + * device, and start it again afterwards. For a port that only does USB it + * keeps the xHCI device and its USB devices instead, and the PHY goes through + * the same stop and start below them: down once xHCI has suspended + * (apple_t6040_atcphy_sleep_enter()), the controller out of reset before its + * core is touched again (apple_t6040_atcphy_sleep_result()), USB2 and USB3 back + * once the core is set up (apple_t6040_atcphy_sleep_power_up()). xHCI then + * starts over and the USB core resets the devices it finds again. + * + * The register sequences are those of power_off, power_on and the reset, in + * the order dwc3-apple's stop and start run them, so the CFG block is parked + * at the sleep's commit as after any detach. The platform has the PHY's firmware + * save its state instead and restore it at the wake; + * the devices stay without that, and it isn't + * done here. The steps are the same whether or not the SoC then loses power + * (a refused sleep, a dry run), and leave nothing the wake doesn't set up + * again. + * + * The phys' power counts and the reset's runtime PM reference are their + * consumers' and stay as they are; sleep_down and sleep_reset say what the + * hardware is behind them. A host that stops after all goes through the phys' + * and the reset's own operations, which end that. + */ + +static struct t6040_usb2_phy *t6040_from_usb2_phy(struct phy *phy) +{ + if (!phy || phy->ops != &t6040_usb2_phy_ops) + return NULL; + return phy_get_drvdata(phy); +} + +/* + * A USB2 host that runs, alone or with USB3 on this PHY and the PIPE on it. + * DisplayPort on the lanes, with any pin assignment, and USB4/Thunderbolt have + * clients of their own, which come back in their own order: such a port's + * host stops for the sleep. + */ +static bool t6040_can_sleep_in_place(const struct t6040_usb2_phy *tphy) +{ + return tphy->pipehandler && tphy->powered && tphy->dwc3_running && + tphy->mode == PHY_MODE_USB_HOST && !tphy->shutdown_failed && + !tphy->sleep_no_keep && !tphy->sleep_down && !tphy->sleep_reset && + !tphy->cio_up && !tphy->pipe_tunneled && !tphy->router_running && + !tphy->dp_up && !tphy->dp_alt && !tphy->usb3_lent && + tphy->usb3_up == tphy->pipe_switched; +} + +int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + + if (!tphy) + return -EOPNOTSUPP; + + guard(mutex)(&tphy->lock); + if (!t6040_can_sleep_in_place(tphy)) { + dev_info(tphy->dev, + "the host stops for the sleep: USB2 %d, USB3 %d, PIPE %d, DP %d/%d, CIO %d, lent %d, failed %d/%d\n", + tphy->powered, tphy->usb3_up, tphy->pipe_switched, tphy->dp_alt, + tphy->dp_up, tphy->cio_up, tphy->usb3_lent, tphy->shutdown_failed, + tphy->sleep_no_keep); + return -EBUSY; + } + tphy->sleep_armed = true; + tphy->sleep_ret = 0; + return 0; +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_prepare); + +int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + int ret; + + if (!tphy) + return -EOPNOTSUPP; + + PM_RUNTIME_ACQUIRE(tphy->dev, pm); + ret = PM_RUNTIME_ACQUIRE_ERR(&pm); + if (ret) + return ret; + + guard(mutex)(&tphy->lock); + if (!tphy->sleep_armed) + return -EINVAL; + if (tphy->sleep_down) + return 0; + /* The mux moved since the prepare: that mode's clients don't come back this way. */ + if (!t6040_can_sleep_in_place(tphy)) { + dev_err(tphy->dev, + "USB changed for the sleep (USB3 %d, PIPE %d, DP %d/%d, CIO %d): refusing it\n", + tphy->usb3_up, tphy->pipe_switched, tphy->dp_alt, tphy->dp_up, + tphy->cio_up); + tphy->sleep_ret = -EBUSY; + return -EBUSY; + } + + /* As dwc3-apple's stop: USB3 with its PIPE, USB2, then the controller's reset. */ + tphy->sleep_usb3 = tphy->usb3_up; + t6040_usb3_down_locked(tphy); + ret = t6040_usb2_off_locked(tphy); + if (ret) + dev_err(tphy->dev, "USB2 did not power down for the sleep: %d\n", ret); + t6040_dwc3_reset_hold_locked(tphy); + tphy->sleep_down = true; + tphy->sleep_reset = true; + dev_info(tphy->dev, "PHY down for the sleep with the host kept, USB3 %s\n", + tphy->sleep_usb3 ? "was up" : "was off"); + return 0; +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_enter); + +/* -ENODEV while the reset does not read back released: the core must not be touched. */ +static int t6040_sleep_release_locked(struct t6040_usb2_phy *tphy) +{ + lockdep_assert_held(&tphy->lock); + if (t6040_dwc3_reset_release_locked(tphy)) { + dev_err(tphy->dev, "the controller's reset did not release after the sleep: %#x\n", + readl(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN)); + tphy->sleep_ret = -ENODEV; + return -ENODEV; + } + tphy->sleep_reset = false; + tphy->sleep_ret = APPLE_ATCPHY_SLEEP_RESET; + return APPLE_ATCPHY_SLEEP_RESET; +} + +int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + int ret; + + if (!tphy) + return -EOPNOTSUPP; + + PM_RUNTIME_ACQUIRE(tphy->dev, pm); + ret = PM_RUNTIME_ACQUIRE_ERR(&pm); + + guard(mutex)(&tphy->lock); + if (!tphy->sleep_armed) + return -ENODATA; + /* The first call after the sleep: without the PHY's power the reset can't release. */ + if (tphy->sleep_reset && ret) + tphy->sleep_ret = -ENODEV; + else if (tphy->sleep_reset) + t6040_sleep_release_locked(tphy); + return tphy->sleep_ret; +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_result); + +int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + int ret; + + if (!tphy) + return -EOPNOTSUPP; + + PM_RUNTIME_ACQUIRE(tphy->dev, pm); + ret = PM_RUNTIME_ACQUIRE_ERR(&pm); + if (ret) + return ret; + + guard(mutex)(&tphy->lock); + /* Only for a controller this sleep's result has taken out of reset. */ + if (!tphy->sleep_armed || tphy->sleep_reset) + return -EINVAL; + if (!tphy->sleep_down) + return tphy->sleep_ret < 0 ? tphy->sleep_ret : 0; + + /* As dwc3-apple's start, in the core's init: USB2, then USB3 if it ran. */ + ret = tphy->shutdown_failed ? -EIO : t6040_usb2_on_locked(tphy); + if (ret) + goto failed; + tphy->sleep_down = false; + if (tphy->sleep_usb3) { + /* A coprocessor that didn't stop cleanly needs its domains' power cycle first. */ + if (!tphy->recovery.cycle_pending) { + tphy->sleep_waking = true; + t6040_usb3_up_locked(tphy); + tphy->sleep_waking = false; + } + if (!tphy->usb3_up) { + ret = -EIO; + goto failed; + } + } + dev_info(tphy->dev, "PHY back after the sleep with the host kept, USB3 %s\n", + tphy->usb3_up ? "up" : "off"); + return 0; + +failed: + dev_err(tphy->dev, "PHY not back after the sleep (%d, USB2 %s): the host has to restart\n", + ret, tphy->sleep_down ? "off" : "on"); + tphy->sleep_ret = ret; + return ret; +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_power_up); + +int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + + if (!tphy) + return -EOPNOTSUPP; + + PM_RUNTIME_ACQUIRE(tphy->dev, pm); + if (PM_RUNTIME_ACQUIRE_ERR(&pm)) + return -ENODEV; + + guard(mutex)(&tphy->lock); + /* Not held in reset by a sleep: there is nothing of this PHY's to put right. */ + if (!tphy->sleep_reset) + return -ENODEV; + return t6040_sleep_release_locked(tphy); +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_recover); + +void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) +{ + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + + if (!tphy) + return; + + guard(mutex)(&tphy->lock); + if (!tphy->sleep_armed) + return; + /* A sleep refused for a USB change doesn't count, nor a restart for another reason. */ + if (!kept && tphy->sleep_ret < 0 && tphy->sleep_ret != -EBUSY) { + tphy->sleep_no_keep = true; + dev_warn(tphy->dev, + "PHY did not come back from a sleep with the host kept (%d): the host stops for sleeps from now on\n", + tphy->sleep_ret); + } + tphy->sleep_armed = false; + tphy->sleep_ret = 0; +} +EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_finish); + #ifdef CONFIG_HIBERNATION static int t6040_phy_count(struct phy *phy) { diff --git a/include/linux/phy/apple-atc.h b/include/linux/phy/apple-atc.h index c81d09de1ce5c..27960bbe3f0f3 100644 --- a/include/linux/phy/apple-atc.h +++ b/include/linux/phy/apple-atc.h @@ -14,6 +14,14 @@ struct phy; /* In D0 after the sleep, but its always-on side lost power, so xHCI's saved state is gone. */ #define APPLE_ATCPHY_SLEEP_POWER_LOST 1 +/* + * The PHY held the controller in reset through the sleep, with its own USB2 and USB3 sides + * down, and has released the reset: the core's registers are gone whether or not the SoC + * lost power, and so is xHCI's saved state. The PHY's own sides come back with + * apple_typec_phy_sleep_power_up(), once the core is set up again. + */ +#define APPLE_ATCPHY_SLEEP_RESET 2 + #if IS_REACHABLE(CONFIG_PHY_APPLE_ATC) /* * For a firmware-driven system sleep, before any device suspends and without the @@ -103,4 +111,135 @@ static inline int apple_atcphy_cio_finish(struct phy *phy) #endif +#if IS_REACHABLE(CONFIG_PHY_APPLE_T6040_USB2) +/* + * The T6040 Type-C PHY's side of the same sleep, for a Type-C port in host mode with nothing + * but USB on its lanes. It has no hibernation to take the controller through: it takes its + * USB3 and USB2 sides down and the controller into reset, as a stop of the host does, and + * brings them back the way a start does. That boots the PHY's coprocessor, so none of it + * runs with interrupts off: the controller's glue calls each step from process context. + * Each returns -EOPNOTSUPP for a PHY that isn't this driver's. + * + * apple_t6040_atcphy_sleep_prepare(): before any device suspends. 0 if the port can sleep + * this way now, and the PHY then holds on to that until the finish. + */ +int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy); + +/* + * Once xHCI has suspended, and before the controller's power domain may go off: USB3 down + * and its PIPE parked, USB2 off, the controller in reset. -EBUSY, with nothing touched, if + * the port has changed since the prepare; the sleep has to be refused then. + */ +int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy); + +/* + * After the sleep, with the controller's power domain on again and before anything touches + * its core. 0: the PHY was never taken down (the sleep was abandoned first), and all is as + * it was. APPLE_ATCPHY_SLEEP_RESET: the controller is out of reset again, which the first + * call after the sleep sees to. -ENODEV: the reset didn't release, so the core must not + * be touched. Any other error: the controller is out of reset, but the PHY didn't come back. + */ +int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy); + +/* + * After APPLE_ATCPHY_SLEEP_RESET, once the core is set up again and before its PIPE is + * routed: USB2 on, and USB3 up if it ran before the sleep. May sleep for the coprocessor's + * boot. An error leaves the controller out of reset for a restart of the host. + */ +int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy); + +/* For -ENODEV: tries the reset's release again. APPLE_ATCPHY_SLEEP_RESET, or -ENODEV. */ +int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy); + +/* Ends what the prepare took on. Without @kept, a failure of the PHY's own stops later ones. */ +void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept); +#else +static inline int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) +{ + return -EOPNOTSUPP; +} + +static inline int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) +{ + return -EOPNOTSUPP; +} + +static inline int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy) +{ + return -EOPNOTSUPP; +} + +static inline int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) +{ + return -EOPNOTSUPP; +} + +static inline int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy) +{ + return -EOPNOTSUPP; +} + +static inline void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) +{ +} +#endif + +/* + * What the controller's glue calls: the driver that @usb2_phy belongs to answers, and the + * other one doesn't know the PHY. A step that only one of them has is nothing to do for the + * other's PHYs. + */ +static inline int apple_typec_phy_sleep_prepare(struct phy *usb2_phy) +{ + int ret = apple_atcphy_sleep_prepare(usb2_phy); + + return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_prepare(usb2_phy) : ret; +} + +static inline int apple_typec_phy_sleep_enter(struct phy *usb2_phy) +{ + int ret = apple_t6040_atcphy_sleep_enter(usb2_phy); + + return ret == -EOPNOTSUPP ? 0 : ret; +} + +static inline int apple_typec_phy_sleep_result(struct phy *usb2_phy) +{ + int ret = apple_atcphy_sleep_result(usb2_phy); + + return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_result(usb2_phy) : ret; +} + +static inline int apple_typec_phy_sleep_power_up(struct phy *usb2_phy) +{ + int ret = apple_t6040_atcphy_sleep_power_up(usb2_phy); + + return ret == -EOPNOTSUPP ? 0 : ret; +} + +static inline int apple_typec_phy_sleep_recover(struct phy *usb2_phy) +{ + int ret = apple_atcphy_sleep_recover(usb2_phy); + + return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_recover(usb2_phy) : ret; +} + +static inline void apple_typec_phy_sleep_finish(struct phy *usb2_phy, bool kept) +{ + apple_atcphy_sleep_finish(usb2_phy, kept); + apple_t6040_atcphy_sleep_finish(usb2_phy, kept); +} + +/* The T6040 PHY's finish has nothing to wait for, so it ends at once. */ +static inline void apple_typec_phy_sleep_finish_async(struct phy *usb2_phy, bool kept) +{ + apple_atcphy_sleep_finish_async(usb2_phy, kept); + apple_t6040_atcphy_sleep_finish(usb2_phy, kept); +} + +static inline void apple_typec_phy_sleep_finish_flush(struct phy *usb2_phy) +{ + apple_atcphy_sleep_finish_flush(usb2_phy); +} + #endif From f032724b506acf3eb64463143b0a1e902634b9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 14:15:19 +0100 Subject: [PATCH 217/269] usb: dwc3: apple: keep a T6040 Type-C port's host through a deep sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On T6040 the host of every Type-C port stops before a deep sleep and starts again after it: sleeping in place was only for a controller with a fixed hub, whose PHY (atc.c on T8140) takes the core through hibernation. The USB devices are removed and enumerate again, and a mounted disk loses its block device. Keep the xHCI device and its USB devices on such a port too, with the T6040 PHY's stop and start below them: - Before the freezer the PHY says whether the port only does USB. The glue's device stops being a syscore device for this sleep: its power domain then goes off before the commit like a stopped controller's, and the PM core runs its suspend callback after xHCI's and orders xHCI's resume after the DARTs' and the PHY's. xHCI's wakeup is off for the sleep, so that xHCI halts in whatever state the sleep ends up, and a role the port controller asks for is kept for after it. - In the device suspend, after xHCI's, the PHY takes USB3 and USB2 down and the core into reset. The domain is marked as waking out of band, so that a USB device below that may wake the system doesn't keep it on: no wakeup of a powered-down SoC comes through the controller. - In xHCI's resume, before it touches the core, the PHY releases the reset, the core is set up again as at a start, the PHY brings USB2 and USB3 back and gets the PIPE, and xHCI is told its state is lost. The USB core then reset-resumes the devices (USB persist). - After the thaw the device is a syscore one again, and the role asked for meanwhile is applied. The same steps run when the sleep is refused late or is a dry run. If one fails, or xHCI doesn't resume, the host restarts as before once the system has resumed. A core whose reset didn't release is left alone, as a T8140 core that may be unpowered. A port in a DisplayPort mode or in USB4/Thunderbolt stops as before, and dwc3_apple.typec_sleep_in_place=0 makes every Type-C port do so, to compare the two in one boot. The fixed hub's path is the same as it was: its decisions moved into dwc3-apple-state.h with the new ones, where KUnit tests cover both. The tests pass under qemu. On a J616s the hosts of a dock and of a disk went through repeated suspend-to-RAM cycles this way; the PHY's commit has what was seen. Signed-off-by: František Balázsy --- drivers/usb/dwc3/Kconfig | 3 + drivers/usb/dwc3/dwc3-apple-state-test.c | 97 ++++- drivers/usb/dwc3/dwc3-apple-state.h | 100 ++++- drivers/usb/dwc3/dwc3-apple.c | 364 +++++++++++++++--- .../drivers/gpu/apple-dcp/dwc3/mocks.c | 2 + .../drivers/gpu/apple-dcp/lifecycle.py | 3 + 6 files changed, 509 insertions(+), 60 deletions(-) diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig index 0d5c41f1a7d5d..b1012cac1b153 100644 --- a/drivers/usb/dwc3/Kconfig +++ b/drivers/usb/dwc3/Kconfig @@ -242,6 +242,9 @@ config USB_DWC3_APPLE_KUNIT_TEST Test the helper with which the Apple Silicon DWC3 glue chooses the USB2 PHY mode it selects before the core leaves reset: device mode, never host mode, and nothing for other states. + Also test its decisions for a deep sleep with the host kept in + place: which ports may, and what the host's resume does for + each thing the PHY can say of the sleep. The tests need no Apple hardware or platform support. They are built under USB_DWC3, so UML without DMA emulation cannot run them. diff --git a/drivers/usb/dwc3/dwc3-apple-state-test.c b/drivers/usb/dwc3/dwc3-apple-state-test.c index 69abb0f32653f..bf959f1851235 100644 --- a/drivers/usb/dwc3/dwc3-apple-state-test.c +++ b/drivers/usb/dwc3/dwc3-apple-state-test.c @@ -46,11 +46,106 @@ static void dwc3_apple_role_during_sleep_test(struct kunit *test) KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_NONE), DWC3_APPLE_NO_CABLE); } +/* A host kept through a sleep is neither stopped nor started for a role: no device mode. */ +static void dwc3_apple_role_while_kept_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_HOST, true), DWC3_APPLE_HOST); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_NONE, true), + DWC3_APPLE_NO_CABLE); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_DEVICE, true), + DWC3_APPLE_NO_CABLE); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_DEVICE, false), + DWC3_APPLE_DEVICE); +} + +/* A fixed hub's host may always be kept; a Type-C port's only by a PHY that restarts it. */ +static void dwc3_apple_sleep_may_keep_test(struct kunit *test) +{ + /* fixed hub (T8140): as before, whatever the Type-C parameter says */ + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(true, false, true, DWC3_APPLE_HOST, + false, false)); + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(true, false, false, DWC3_APPLE_HOST, + false, false)); + /* Type-C port with the T6040 PHY, in host mode: kept unless switched off */ + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_HOST, + false, false)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, false, DWC3_APPLE_HOST, + false, false)); + /* any other Type-C port stops */ + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, false, true, DWC3_APPLE_HOST, + false, false)); + /* no host, no core, a glue that goes away, a core that didn't come back */ + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_NO_CABLE, + false, false)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, + DWC3_APPLE_PROBE_PENDING, false, false)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_DEVICE, + false, false)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_HOST, + true, false)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(true, false, true, DWC3_APPLE_HOST, + false, true)); +} + +/* The core's mark is read only where the core may be touched and wasn't reset. */ +static void dwc3_apple_sleep_reads_mark_test(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_reads_mark(0)); + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_reads_mark(-EBUSY)); + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_reads_mark(APPLE_ATCPHY_SLEEP_POWER_LOST)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_reads_mark(APPLE_ATCPHY_SLEEP_RESET)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_reads_mark(-ENODEV)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_reads_mark(-EIO)); + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_reads_mark(-EOPNOTSUPP)); +} + +/* A PHY that hibernated the core (T8140): the mark and the always-on side decide. */ +static void dwc3_apple_wake_hibernated_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(0, true), DWC3_APPLE_WAKE_AS_IS); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(0, false), DWC3_APPLE_WAKE_RESTORE); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(-EBUSY, true), DWC3_APPLE_WAKE_AS_IS); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(-EBUSY, false), DWC3_APPLE_WAKE_RESTORE); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(APPLE_ATCPHY_SLEEP_POWER_LOST, true), + DWC3_APPLE_WAKE_AS_IS); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(APPLE_ATCPHY_SLEEP_POWER_LOST, false), + DWC3_APPLE_WAKE_START_OVER); +} + +/* A PHY that held the core in reset (T6040): the host starts over whatever the mark reads. */ +static void dwc3_apple_wake_reset_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(APPLE_ATCPHY_SLEEP_RESET, false), + DWC3_APPLE_WAKE_PHY_START_OVER); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(APPLE_ATCPHY_SLEEP_RESET, true), + DWC3_APPLE_WAKE_PHY_START_OVER); +} + +/* A core that may be unpowered, or whose PHY didn't come back, is not resumed. */ +static void dwc3_apple_wake_failed_test(struct kunit *test) +{ + static const int errors[] = { -ENODEV, -EIO, -ETIMEDOUT, -EOPNOTSUPP, -ENODATA, 3 }; + int i; + + for (i = 0; i < ARRAY_SIZE(errors); i++) { + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(errors[i], false), + DWC3_APPLE_WAKE_FAILED); + KUNIT_EXPECT_EQ(test, dwc3_apple_sleep_wake_plan(errors[i], true), + DWC3_APPLE_WAKE_FAILED); + } +} + static struct kunit_case dwc3_apple_state_test_cases[] = { KUNIT_CASE(dwc3_apple_host_left_to_xhci_test), KUNIT_CASE(dwc3_apple_device_selected_before_reset_test), KUNIT_CASE(dwc3_apple_non_role_test), KUNIT_CASE(dwc3_apple_role_during_sleep_test), + KUNIT_CASE(dwc3_apple_role_while_kept_test), + KUNIT_CASE(dwc3_apple_sleep_may_keep_test), + KUNIT_CASE(dwc3_apple_sleep_reads_mark_test), + KUNIT_CASE(dwc3_apple_wake_hibernated_test), + KUNIT_CASE(dwc3_apple_wake_reset_test), + KUNIT_CASE(dwc3_apple_wake_failed_test), {} }; @@ -61,5 +156,5 @@ static struct kunit_suite dwc3_apple_state_test_suite = { kunit_test_suite(dwc3_apple_state_test_suite); -MODULE_DESCRIPTION("KUnit tests for the Apple Silicon DWC3 glue USB2 mode choice"); +MODULE_DESCRIPTION("KUnit tests for the Apple Silicon DWC3 glue's USB2 mode and sleep decisions"); MODULE_LICENSE("GPL"); diff --git a/drivers/usb/dwc3/dwc3-apple-state.h b/drivers/usb/dwc3/dwc3-apple-state.h index bb8d827fdec4c..7e5b00bc77a6d 100644 --- a/drivers/usb/dwc3/dwc3-apple-state.h +++ b/drivers/usb/dwc3/dwc3-apple-state.h @@ -1,13 +1,15 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* - * Apple Silicon DWC3 glue: controller states and the USB2 PHY role choice, - * kept in a header so that the choice can be tested without the hardware. + * Apple Silicon DWC3 glue: controller states, the USB2 PHY role choice and + * the decisions of a sleep with the host kept in place, in a header so that + * they can be tested without the hardware. */ #ifndef __DWC3_APPLE_STATE_H #define __DWC3_APPLE_STATE_H #include +#include #include #include @@ -66,4 +68,98 @@ static inline enum dwc3_apple_state dwc3_apple_state_for_role(enum usb_role role } } +/** + * dwc3_apple_state_for_role_kept() - the same for a host kept in place through the sleep + * @role: the role asked for + * @host_only: the controller has no device mode + * + * Nothing is stopped or started while the sleep lasts, so what dwc3_usb_role_switch_set() + * decides for a running controller is decided here: no device mode on a host-only port. + * + * Return: the state the port goes to once the sleep is over. + */ +static inline enum dwc3_apple_state dwc3_apple_state_for_role_kept(enum usb_role role, + bool host_only) +{ + if (role == USB_ROLE_DEVICE && host_only) + return DWC3_APPLE_NO_CABLE; + + return dwc3_apple_state_for_role(role); +} + +/** + * dwc3_apple_sleep_may_keep() - whether a deep sleep may find the host kept in place + * @fixed_hub: the controller serves a fixed hub + * @phy_restart: a Type-C port whose PHY keeps the host by the core's reset + * @typec_allowed: Type-C ports may sleep in place (the typec_sleep_in_place parameter) + * @state: the controller's state + * @shutting_down: the glue is going away + * @dead: the core didn't come back from an earlier sleep + * + * The glue's side of the question. The PHY then has the last word, as only it knows what + * runs on the port's lanes (apple_typec_phy_sleep_prepare()). + * + * Return: true if the PHY is to be asked. + */ +static inline bool dwc3_apple_sleep_may_keep(bool fixed_hub, bool phy_restart, bool typec_allowed, + enum dwc3_apple_state state, bool shutting_down, + bool dead) +{ + return (fixed_hub || (phy_restart && typec_allowed)) && state == DWC3_APPLE_HOST && + !shutting_down && !dead; +} + +/** + * dwc3_apple_sleep_reads_mark() - whether the core's mark says anything after the sleep + * @phy: what the PHY says of the sleep (apple_typec_phy_sleep_result()) + * + * The mark is a value written to a scratch register of the core before the sleep. After an + * error the core may be unpowered and must not be read, and a core the PHY took through its + * reset has lost the mark whatever the SoC did. + * + * Return: true if the register may be read and its value counts. + */ +static inline bool dwc3_apple_sleep_reads_mark(int phy) +{ + return !phy || phy == -EBUSY || phy == APPLE_ATCPHY_SLEEP_POWER_LOST; +} + +/* What the host's resume does after a sleep in place, see dwc3_apple_sleep_wake_plan(). */ +enum dwc3_apple_wake { + DWC3_APPLE_WAKE_AS_IS, /* Nothing was lost: xHCI resumes as usual */ + DWC3_APPLE_WAKE_RESTORE, /* The core is set up again, xHCI restores its state */ + DWC3_APPLE_WAKE_START_OVER, /* The core is set up again, xHCI starts over */ + DWC3_APPLE_WAKE_PHY_START_OVER, /* The same, and the PHY comes back after the core */ + DWC3_APPLE_WAKE_FAILED, /* xHCI must not resume, nor anything touch the core */ +}; + +/** + * dwc3_apple_sleep_wake_plan() - what the host's resume does after a sleep in place + * @phy: what the PHY says of the sleep (apple_typec_phy_sleep_result()) + * @core_kept: the core still holds its mark; false where dwc3_apple_sleep_reads_mark() + * says not to read it + * + * A sleep refused before the PHY did anything (-EBUSY) left all as it was. A PHY that took + * the core through hibernation brings it back with its registers (0), or without them if a + * power domain went off under it: xHCI's saved state then still counts unless the core's + * always-on side lost power as well. A PHY that held the core in reset says so, and the + * host starts over whatever the mark reads. + * + * Return: the plan. + */ +static inline enum dwc3_apple_wake dwc3_apple_sleep_wake_plan(int phy, bool core_kept) +{ + switch (phy) { + case APPLE_ATCPHY_SLEEP_RESET: + return DWC3_APPLE_WAKE_PHY_START_OVER; + case APPLE_ATCPHY_SLEEP_POWER_LOST: + return core_kept ? DWC3_APPLE_WAKE_AS_IS : DWC3_APPLE_WAKE_START_OVER; + case 0: + case -EBUSY: + return core_kept ? DWC3_APPLE_WAKE_AS_IS : DWC3_APPLE_WAKE_RESTORE; + default: + return DWC3_APPLE_WAKE_FAILED; + } +} + #endif /* __DWC3_APPLE_STATE_H */ diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index c3b02c3602d0a..ac49b5a93a67f 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -107,6 +107,36 @@ * everything before the freezer runs, the way a cable disconnect does, and brings it back * once the system has resumed. See dwc3_apple_pm_notify(). * + * A T6040 Type-C port in host mode sleeps in place as well, in another way, as its PHY has no + * hibernation to take the core through and can't bring itself back with interrupts off (it + * boots a coprocessor). Below the xHCI device, which stays with its USB devices, the port goes + * through the stop and the start of a cable session: + * + * - Before the freezer (dwc3_apple_pm_notify()): the PHY says whether the port only does USB. + * The glue's device stops being a syscore device for this sleep, so that its power domain + * goes off before the commit like a stopped controller's and the PM core runs its callbacks + * and orders it after the DARTs and the PHY. xHCI's own wakeup is off for it, so that xHCI + * halts whatever state the sleep ends up in. A role the port controller asks for from here + * on is kept for after the sleep. + * - Device suspend, after xHCI's (dwc3_apple_suspend(), the parent's callback): the PHY takes + * USB3 and USB2 down and the core into reset. This is what leaves the PHY parked at the + * commit. Nothing touches the core from here on. + * - The noirq phases: the power domain goes off and comes back on, and the DARTs with it, + * through genpd, whether or not the SoC lost power in between. + * - Device resume, in xHCI's before it touches the core (dwc3_apple_pre_host_resume()): the + * PHY releases the reset, the core is set up again as at a start, the PHY brings USB2 and + * USB3 back and gets the PIPE, and xHCI is told that its state is lost. It starts over, + * and the USB core reset-resumes the devices (USB persist): they keep their device numbers, + * and a mounted disk its block device. + * - After the thaw (dwc3_apple_pm_notify()): back to a syscore device, and the role the port + * controller asked for meanwhile. + * + * The same steps run when the sleep is refused late or is a dry run. If any of them fails, + * the host is restarted the old way once the system has resumed, and the devices enumerate + * again; a core that may be unpowered is left alone until its reset releases. A port in a + * DisplayPort mode or in USB4/Thunderbolt stops for the sleep as before, and + * dwc3_apple.typec_sleep_in_place=0 makes every Type-C port do so. + * * Runtime PM (T6040 and T8132, see dwc3_apple_core_probe()): the glue holds a reference from * the reset deassert to the end of the shutdown, so with no cable the controller can * runtime-suspend and its power domain drop, its DARTs (which save and restore their state) and, @@ -145,6 +175,9 @@ * @pre_core_rpm: Runtime PM is the glue's own until the first core bring-up * @dt_dis_u3_susphy: The device tree keeps USB3 PHY suspend off itself * @sleep_ddr: The host votes for DRAM in a sleep while it runs (apple,sleep-ddr) + * @sleep_restart: A Type-C port whose PHY keeps the host through a deep sleep by holding the + * core in reset, see dwc3_apple_suspend() + * @sleep_xhci_wakeup: xHCI's wakeup setting, which is off for such a sleep * @drd_regs: The controller's optional blocks outside dwc3-core (drd-cfg, drd-bulk, drd-ausbc), * mapped for their tunables * @tunables: Controller tunables from the device tree, see dwc3_apple_tunable_list @@ -189,10 +222,15 @@ struct dwc3_apple { struct delayed_work hub_check; /* The core didn't come back from a sleep or a power cycle: nothing may touch it. */ bool sleep_dead; - /* Stopped for a deep sleep: the state to come back to, and the wakeup setting. */ + /* + * Stopped for a deep sleep: the state to come back to, and the wakeup setting. The + * state also for a Type-C port's host kept through it, see dwc3_apple_suspend(). + */ bool sleep_stopped; bool sleep_wakeup; enum dwc3_apple_state sleep_state; + bool sleep_restart; + bool sleep_xhci_wakeup; void __iomem *drd_regs[DWC3_APPLE_NR_DRD_REGS]; struct apple_tunable *tunables[DWC3_APPLE_NR_TUNABLES]; @@ -200,6 +238,12 @@ struct dwc3_apple { #define to_dwc3_apple(d) container_of((d), struct dwc3_apple, dwc) +/* Read before the freezer of each sleep, so both ways can be compared in one boot. */ +static bool typec_sleep_in_place = true; +module_param(typec_sleep_in_place, bool, 0644); +MODULE_PARM_DESC(typec_sleep_in_place, + "Keep a Type-C port's host and its USB devices through a deep sleep where the PHY can (T6040); off, the host stops before the sleep and starts again after it (default Y)"); + /* * Apple Silicon dwc3 vendor-specific registers * @@ -424,13 +468,33 @@ static void dwc3_apple_set_ptrcap(struct dwc3_apple *appledwc, u32 mode) #define DWC3_APPLE_CFG_STATUS_PIPE_PHY_READY BIT(30) #define DWC3_APPLE_PIPE_PHY_READY_US 100000 +/* + * The T6040 core came up against the dummy PHY: reset its USB3 PIPE side once the PIPE has + * moved, as the tested bring-up does (USB2 is not affected). + */ +static void dwc3_apple_usb3_pipe_reset(struct dwc3_apple *appledwc) +{ + struct dwc3 *dwc = &appledwc->dwc; + u32 reg; + + reg = dwc3_readl(dwc, DWC3_GUSB3PIPECTL(0)); + dwc3_writel(dwc, DWC3_GUSB3PIPECTL(0), reg | DWC3_GUSB3PIPECTL_PHYSOFTRST); + usleep_range(1000, 1500); + dwc3_writel(dwc, DWC3_GUSB3PIPECTL(0), reg & ~DWC3_GUSB3PIPECTL_PHYSOFTRST); + msleep(100); +} + /* * The core is powered but its registers are at their reset values. Set it up again the way * dwc3_apple_core_start() and dwc3_apple_set_role() do, in place: the PHYs and the xHCI * device stay, so xHCI can restore its saved state, or start over. Runs in the resume, * while nothing else touches the core. + * + * With @phy_down the PHY held the core in reset with its own sides down (T6040). The phys' + * power counts never dropped, so dwc3_core_init() powers nothing on: the PHY brings USB2 and + * USB3 back itself, after the core's init as at a start, where they come up inside it. */ -static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc) +static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc, bool phy_down) { struct dwc3 *dwc = &appledwc->dwc; int i, ret; @@ -447,6 +511,11 @@ static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc) phy_power_off(dwc->usb2_generic_phy[i]); phy_exit(dwc->usb2_generic_phy[i]); } + if (phy_down) { + ret = apple_typec_phy_sleep_power_up(appledwc->usb2_phy); + if (ret) + return ret; + } dwc3_apple_setup_cio(appledwc); dwc3_apple_apply_tunables(appledwc); @@ -459,6 +528,9 @@ static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc) if (ret) dev_warn(appledwc->dev, "USB3 PHY setup failed after the sleep, USB2 only, err=%d\n", ret); + else if (phy_down && appledwc->usb3_phy && + of_device_is_compatible(appledwc->dev->of_node, "apple,t6040-dwc3")) + dwc3_apple_usb3_pipe_reset(appledwc); return 0; } @@ -470,43 +542,64 @@ static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc) * restores its saved state, as macOS's does, unless the always-on side lost power too: then * it's told it lost its state (a positive return). A sleep refused before the hibernation * left the core as it was. + * + * A PHY that held the core in reset for the sleep (T6040) has released it by now and comes + * back with the core, in dwc3_apple_core_reinit(): xHCI always starts over then. + * dwc3_apple_sleep_wake_plan() has the cases. */ static int dwc3_apple_pre_host_resume(struct dwc3 *dwc) { struct dwc3_apple *appledwc = to_dwc3_apple(dwc); - bool lost; + bool kept = false; + int phy, ret; u32 val; - int ret; if (!READ_ONCE(appledwc->sleep_in_place)) return 0; - ret = apple_atcphy_sleep_result(appledwc->usb2_phy); - lost = ret == APPLE_ATCPHY_SLEEP_POWER_LOST; - if (ret == -EBUSY || lost) - ret = 0; - if (!ret && appledwc->drd_regs[0] && /* drd-cfg */ - readl_poll_timeout(appledwc->drd_regs[0] + DWC3_APPLE_CFG_STATUS, val, - val & DWC3_APPLE_CFG_STATUS_PIPE_PHY_READY, 100, - DWC3_APPLE_PIPE_PHY_READY_US)) - dev_warn(appledwc->dev, "PIPE PHY not ready after the sleep: %#x\n", val); - if (!ret && dwc3_readl(dwc, DWC3_GUID) != DWC3_APPLE_SLEEP_GUID) { - ret = dwc3_apple_core_reinit(appledwc); - if (!ret && !lost) { - appledwc->sleep_reinit = true; - dev_dbg(appledwc->dev, - "core lost its registers in the sleep: set up again, xHCI restores its state\n"); - return 0; - } - if (!ret) { - appledwc->sleep_reinit = true; - dev_dbg(appledwc->dev, - "core and its always-on side lost power in the sleep: set up again, xHCI starts over\n"); - return 1; - } + phy = apple_typec_phy_sleep_result(appledwc->usb2_phy); + if (dwc3_apple_sleep_reads_mark(phy)) { + if (appledwc->drd_regs[0] && /* drd-cfg */ + readl_poll_timeout(appledwc->drd_regs[0] + DWC3_APPLE_CFG_STATUS, val, + val & DWC3_APPLE_CFG_STATUS_PIPE_PHY_READY, 100, + DWC3_APPLE_PIPE_PHY_READY_US)) + dev_warn(appledwc->dev, "PIPE PHY not ready after the sleep: %#x\n", val); + kept = dwc3_readl(dwc, DWC3_GUID) == DWC3_APPLE_SLEEP_GUID; } - if (ret) - dev_err(appledwc->dev, "not resuming xHCI, the core didn't come back: %d\n", ret); + + switch (dwc3_apple_sleep_wake_plan(phy, kept)) { + case DWC3_APPLE_WAKE_AS_IS: + return 0; + case DWC3_APPLE_WAKE_RESTORE: + ret = dwc3_apple_core_reinit(appledwc, false); + if (ret) + break; + appledwc->sleep_reinit = true; + dev_dbg(appledwc->dev, + "core lost its registers in the sleep: set up again, xHCI restores its state\n"); + return 0; + case DWC3_APPLE_WAKE_START_OVER: + ret = dwc3_apple_core_reinit(appledwc, false); + if (ret) + break; + appledwc->sleep_reinit = true; + dev_dbg(appledwc->dev, + "core and its always-on side lost power in the sleep: set up again, xHCI starts over\n"); + return 1; + case DWC3_APPLE_WAKE_PHY_START_OVER: + ret = dwc3_apple_core_reinit(appledwc, true); + if (ret) + break; + appledwc->sleep_reinit = true; + dev_info(appledwc->dev, + "host kept through the sleep: core and PHY set up again, xHCI starts over\n"); + return 1; + case DWC3_APPLE_WAKE_FAILED: + default: + ret = phy < 0 ? phy : -EINVAL; + break; + } + dev_err(appledwc->dev, "not resuming xHCI, the core didn't come back: %d\n", ret); return ret; } @@ -785,22 +878,6 @@ static int dwc3_apple_core_start(struct dwc3_apple *appledwc, enum dwc3_apple_st return 0; } -/* - * The T6040 core came up against the dummy PHY: reset its USB3 PIPE side once the PIPE has - * moved, as the tested bring-up does (USB2 is not affected). - */ -static void dwc3_apple_usb3_pipe_reset(struct dwc3_apple *appledwc) -{ - struct dwc3 *dwc = &appledwc->dwc; - u32 reg; - - reg = dwc3_readl(dwc, DWC3_GUSB3PIPECTL(0)); - dwc3_writel(dwc, DWC3_GUSB3PIPECTL(0), reg | DWC3_GUSB3PIPECTL_PHYSOFTRST); - usleep_range(1000, 1500); - dwc3_writel(dwc, DWC3_GUSB3PIPECTL(0), reg & ~DWC3_GUSB3PIPECTL_PHYSOFTRST); - msleep(100); -} - /* Select the role in the core and route the USB3 PHY, before xhci or the gadget is started */ static int dwc3_apple_set_role(struct dwc3_apple *appledwc, enum dwc3_apple_state state) { @@ -1134,6 +1211,16 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro return 0; } + /* + * Kept through a sleep, with the core in reset for some of it: nothing stops or starts + * until the sleep is over, see dwc3_apple_sleep_restart_end(). Nor while the core may + * be unpowered after one: dwc3_apple_recover() starts what is asked for here. + */ + if (appledwc->sleep_in_place || appledwc->sleep_dead) { + appledwc->sleep_state = dwc3_apple_state_for_role_kept(role, appledwc->host_only); + return 0; + } + /* * Stay off instead of starting device mode (see dwc3_apple_host_only()). Reporting success * keeps the port controller from retrying: get() then reports USB_ROLE_NONE. @@ -1188,7 +1275,9 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw) guard(mutex)(&appledwc->lock); - switch (appledwc->sleep_stopped ? appledwc->sleep_state : appledwc->state) { + switch (appledwc->sleep_stopped || (appledwc->sleep_restart && + (appledwc->sleep_in_place || appledwc->sleep_dead)) ? + appledwc->sleep_state : appledwc->state) { case DWC3_APPLE_HOST: return USB_ROLE_HOST; case DWC3_APPLE_DEVICE: @@ -1218,6 +1307,20 @@ static int dwc3_apple_setup_role_switch(struct dwc3_apple *appledwc) return 0; } +/* + * What a host lost in a sleep in place comes back as, once it is stopped: a host, or on a + * Type-C port whatever the port controller has asked for since the sleep began. + */ +static void dwc3_apple_restart_lost(struct dwc3_apple *appledwc) +{ + enum dwc3_apple_state state = appledwc->sleep_restart ? appledwc->sleep_state : + DWC3_APPLE_HOST; + + lockdep_assert_held(&appledwc->lock); + if (state == DWC3_APPLE_HOST || state == DWC3_APPLE_DEVICE) + dwc3_apple_start_after_sleep(appledwc, state); +} + /* * A core that may be unpowered gets one more power cycle, as macOS's Off and cold start, and * the host starts over once it's back. Until then nothing may touch it, and every sleep is @@ -1228,7 +1331,7 @@ static bool dwc3_apple_recover(struct dwc3_apple *appledwc) int ret; lockdep_assert_held(&appledwc->lock); - ret = apple_atcphy_sleep_recover(appledwc->usb2_phy); + ret = apple_typec_phy_sleep_recover(appledwc->usb2_phy); if (ret < 0) { if (!appledwc->sleep_dead) dev_err(appledwc->dev, @@ -1244,7 +1347,7 @@ static bool dwc3_apple_recover(struct dwc3_apple *appledwc) if (ret) dev_err(appledwc->dev, "Failed to stop after the power cycle, err=%d\n", ret); else - dwc3_apple_start_after_sleep(appledwc, DWC3_APPLE_HOST); + dwc3_apple_restart_lost(appledwc); return true; } @@ -1263,7 +1366,7 @@ static bool dwc3_apple_sleep_kept(struct dwc3_apple *appledwc) int ret; appledwc->sleep_in_place = false; - ret = apple_atcphy_sleep_result(appledwc->usb2_phy); + ret = apple_typec_phy_sleep_result(appledwc->usb2_phy); if (ret == -ENODEV) { dwc3_apple_recover(appledwc); return false; @@ -1274,7 +1377,7 @@ static bool dwc3_apple_sleep_kept(struct dwc3_apple *appledwc) dwc3_apple_check_hub_later(appledwc); return true; } - if (!ret || ret == -EBUSY || ret == APPLE_ATCPHY_SLEEP_POWER_LOST) { + if (dwc3_apple_sleep_reads_mark(ret)) { guid = dwc3_readl(dwc, DWC3_GUID); if (guid == DWC3_APPLE_SLEEP_GUID) { dwc3_writel(dwc, DWC3_GUID, appledwc->sleep_guid); @@ -1291,7 +1394,91 @@ static bool dwc3_apple_sleep_kept(struct dwc3_apple *appledwc) if (ret) dev_err(appledwc->dev, "Failed to stop after sleep, err=%d\n", ret); else - dwc3_apple_start_after_sleep(appledwc, DWC3_APPLE_HOST); + dwc3_apple_restart_lost(appledwc); + + return false; +} + +/* + * Whether xHCI is suspended: its controller halted, and nothing of its touching the core. + * An xHCI device without its driver touches nothing either. Takes the xHCI device's lock. + */ +static bool dwc3_apple_xhci_suspended(struct dwc3_apple *appledwc) +{ + struct platform_device *xhci = appledwc->dwc.xhci; + struct usb_hcd *hcd; + bool suspended; + + lockdep_assert_held(&appledwc->lock); + if (!xhci) + return true; + + device_lock(&xhci->dev); + hcd = xhci->dev.driver ? platform_get_drvdata(xhci) : NULL; + suspended = !hcd || !HCD_HW_ACCESSIBLE(hcd); + device_unlock(&xhci->dev); + + return suspended; +} + +/* After a sleep: still suspended, and not by runtime PM, so its resume failed. */ +static bool dwc3_apple_xhci_resume_failed(struct dwc3_apple *appledwc) +{ + struct platform_device *xhci = appledwc->dwc.xhci; + + return xhci && !pm_runtime_suspended(&xhci->dev) && dwc3_apple_xhci_suspended(appledwc); +} + +/* + * A Type-C port's host is kept through this sleep, by its PHY (T6040): see the comment at the + * top. From here to dwc3_apple_sleep_restart_end() the glue's device is an ordinary one for + * the PM core, which then runs dwc3_apple_suspend() after xHCI's suspend, lets the power + * domain go off and orders xHCI's resume after the DARTs' and the PHY's. xHCI has to halt + * for it in any state the sleep ends up in, so it can't be kept running for a USB wakeup. + */ +static void dwc3_apple_sleep_restart_begin(struct dwc3_apple *appledwc) +{ + struct platform_device *xhci = appledwc->dwc.xhci; + + lockdep_assert_held(&appledwc->lock); + appledwc->sleep_state = appledwc->state; + appledwc->sleep_xhci_wakeup = xhci && device_may_wakeup(&xhci->dev); + if (appledwc->sleep_xhci_wakeup) + device_set_wakeup_enable(&xhci->dev, false); + dev_pm_syscore_device(appledwc->dev, false); +} + +/* + * The end of such a sleep, with @kept from dwc3_apple_sleep_kept(), which has restarted a + * host that was lost. A host that was kept is the glue's to keep powered again. If xHCI + * didn't resume on it, or the port controller asked for another role meanwhile, it stops + * now, and what was asked for starts. Returns whether the host is still the one kept. + */ +static bool dwc3_apple_sleep_restart_end(struct dwc3_apple *appledwc, bool kept) +{ + struct platform_device *xhci = appledwc->dwc.xhci; + enum dwc3_apple_state state = appledwc->sleep_state; + int ret; + + lockdep_assert_held(&appledwc->lock); + if (!kept || appledwc->state != DWC3_APPLE_HOST) + return false; + + dev_pm_syscore_device(appledwc->dev, true); + if (xhci && appledwc->sleep_xhci_wakeup) + device_set_wakeup_enable(&xhci->dev, true); + if (state == DWC3_APPLE_HOST && !dwc3_apple_xhci_resume_failed(appledwc)) + return true; + + if (state == DWC3_APPLE_HOST) + dev_warn(appledwc->dev, "xHCI did not resume after the sleep: restarting the host\n"); + else + dev_info(appledwc->dev, "role changed during the sleep: stopping the host kept\n"); + ret = dwc3_apple_exit(appledwc); + if (ret) + dev_err(appledwc->dev, "Failed to stop after sleep, err=%d\n", ret); + else + dwc3_apple_restart_lost(appledwc); return false; } @@ -1322,11 +1509,22 @@ static int dwc3_apple_pm_notify_locked(struct dwc3_apple *appledwc, unsigned lon *kept = true; break; } + /* A Type-C port's role changed while its PHY was asked: stopped already. */ + if (in_place && appledwc->state != DWC3_APPLE_HOST) { + in_place = false; + *finish = true; + *kept = true; + } /* xHCI suspends and resumes as usual, and the wakeup setting stays. */ if (in_place) { appledwc->sleep_guid = dwc3_readl(&appledwc->dwc, DWC3_GUID); dwc3_writel(&appledwc->dwc, DWC3_GUID, DWC3_APPLE_SLEEP_GUID); appledwc->sleep_in_place = true; + /* On a Type-C port xHCI's wakeup is off for it: nothing there can wake. */ + if (appledwc->sleep_restart) { + dwc3_apple_sleep_restart_begin(appledwc); + break; + } /* For the port controllers' USB wake. Changes after this go unseen. */ apple_sleep_usb_set_wake_ports(dwc3_apple_hub_wake_ports(appledwc)); break; @@ -1344,6 +1542,8 @@ static int dwc3_apple_pm_notify_locked(struct dwc3_apple *appledwc, unsigned lon if (appledwc->sleep_in_place) { apple_sleep_usb_set_wake_ports(0); *kept = dwc3_apple_sleep_kept(appledwc); + if (appledwc->sleep_restart) + *kept = dwc3_apple_sleep_restart_end(appledwc, *kept); *finish = true; break; } @@ -1379,14 +1579,18 @@ static int dwc3_apple_pm_notify(struct notifier_block *nb, unsigned long action, * check takes the glue's lock, and has nothing to look at until the sleep is over. */ if (action == PM_SUSPEND_PREPARE) { - apple_atcphy_sleep_finish_flush(appledwc->usb2_phy); + apple_typec_phy_sleep_finish_flush(appledwc->usb2_phy); cancel_delayed_work_sync(&appledwc->hub_check); } if (action == PM_SUSPEND_PREPARE && !pm_suspend_default_s2idle()) { scoped_guard(mutex, &appledwc->lock) - in_place = appledwc->fixed_hub && appledwc->state == DWC3_APPLE_HOST && - !appledwc->shutting_down && !appledwc->sleep_dead; - in_place = in_place && !apple_atcphy_sleep_prepare(appledwc->usb2_phy); + in_place = dwc3_apple_sleep_may_keep(appledwc->fixed_hub, + appledwc->sleep_restart, + READ_ONCE(typec_sleep_in_place), + appledwc->state, + appledwc->shutting_down, + appledwc->sleep_dead); + in_place = in_place && !apple_typec_phy_sleep_prepare(appledwc->usb2_phy); } scoped_guard(mutex, &appledwc->lock) @@ -1397,13 +1601,56 @@ static int dwc3_apple_pm_notify(struct notifier_block *nb, unsigned long action, * from the PHY's work, which an unbind of this device doesn't wait for under its lock. */ if (finish && action == PM_POST_SUSPEND) - apple_atcphy_sleep_finish_async(appledwc->usb2_phy, kept); + apple_typec_phy_sleep_finish_async(appledwc->usb2_phy, kept); else if (finish) - apple_atcphy_sleep_finish(appledwc->usb2_phy, kept); + apple_typec_phy_sleep_finish(appledwc->usb2_phy, kept); return ret; } +/* + * Only for a Type-C port's host kept through a sleep (T6040, see the comment at the top): + * otherwise the core is either down or the device a syscore one, which the PM core skips. + * xHCI, the child, has suspended: its controller is halted and its state saved, for what + * that's worth. The PHY now takes USB3 and USB2 down and the core into reset, in the order + * of dwc3_apple_core_stop(), and nothing touches the core until dwc3_apple_pre_host_resume(). + * This may sleep, for the PHY's coprocessor, which is why it isn't a noirq callback. + * + * The power domain then goes off in the noirq phase like a stopped controller's, also with a + * USB device below that may wake the system: no wakeup of a powered-down SoC comes through + * the controller. An error refuses the sleep, with the PHY and the core as they were. + */ +static int dwc3_apple_suspend(struct device *dev) +{ + struct dwc3_apple *appledwc = to_dwc3_apple(dev_get_drvdata(dev)); + struct dwc3 *dwc = &appledwc->dwc; + int ret; + + guard(mutex)(&appledwc->lock); + if (!appledwc->sleep_in_place || !appledwc->sleep_restart) + return 0; + + if (!dwc3_apple_xhci_suspended(appledwc)) { + dev_err(dev, "xHCI still runs: refusing the sleep\n"); + return -EBUSY; + } + /* The PHY hands the PIPE back in P3, as it took it over */ + dwc->dis_u3_susphy_quirk = appledwc->dt_dis_u3_susphy; + dwc3_enable_susphy(dwc, true); + ret = apple_typec_phy_sleep_enter(appledwc->usb2_phy); + if (ret) { + dev_err(dev, "PHY did not take the host through the sleep: %d, refusing it\n", ret); + return ret; + } + device_set_out_band_wakeup(dev); + + return 0; +} + +static const struct dev_pm_ops dwc3_apple_pm_ops = { + .suspend = pm_sleep_ptr(dwc3_apple_suspend), +}; + static void dwc3_apple_pm_notifier_remove(void *data) { struct dwc3_apple *appledwc = data; @@ -1437,6 +1684,8 @@ static int dwc3_apple_probe(struct platform_device *pdev) appledwc->dev = &pdev->dev; appledwc->fixed_hub = dwc3_apple_has_fixed_hub(dev, &appledwc->hub_port); appledwc->host_only = dwc3_apple_host_only(dev); + appledwc->sleep_restart = !appledwc->fixed_hub && + of_device_is_compatible(dev->of_node, "apple,t6040-dwc3"); appledwc->dt_dis_u3_susphy = device_property_read_bool(dev, "snps,dis_u3_susphy_quirk"); appledwc->sleep_ddr = device_property_read_bool(dev, "apple,sleep-ddr"); /* The PHY driver and the DARTs follow runtime PM on these SoCs. */ @@ -1612,6 +1861,7 @@ static struct platform_driver dwc3_apple_driver = { .driver = { .name = "dwc3-apple", .of_match_table = dwc3_apple_of_match, + .pm = pm_ptr(&dwc3_apple_pm_ops), }, }; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c index 5db71a2f04bbd..0d898646821d0 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -93,6 +93,8 @@ struct dwc3_apple { /* Stopped for a deep sleep, and the state to bring back. */ bool sleep_stopped; enum dwc3_apple_state sleep_state; + /* Kept in place through a deep sleep, or lost in one; the fixtures do neither. */ + bool sleep_in_place, sleep_dead; /* Votes sleep DDR while the host is up; the fixtures leave it off. */ bool sleep_ddr; bool rpm_held, host_only; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py index 86a95e0f4178e..7669ecdc8e755 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py @@ -1080,6 +1080,9 @@ def fn(n, name): role = 'static inline enum dwc3_apple_state dwc3_apple_state_for_role(' if role in files['state']: decls.append(block(files['state'], files['state'].index(role))) + kept = 'static inline enum dwc3_apple_state dwc3_apple_state_for_role_kept(' + if kept in files['state']: + decls.append(block(files['state'], files['state'].index(kept))) start = files['atc'].index('static const struct {\n\tconst struct atcphy_mode_configuration normal;') end = files['atc'].index('\n};', start) + 3 decls.append(files['atc'][start:end]) From 9a922a98946b8bd18cc0095e63569c25acad51e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 14:34:27 +0100 Subject: [PATCH 218/269] phy: apple: t6040: let a port keep its host through a sleep again after a failed wake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A wake that fails with the host kept in place makes the PHY refuse that for every later sleep of the port, so that the host stops and starts as before from then on. Only binding the driver again took that back. Show the flag next to the port's fault injection in debugfs, where it can be cleared, and name it in the message that sets it. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 3259d8e50166e..e80b20e0e94f7 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2806,6 +2806,8 @@ static int t6040_debugfs_init(struct t6040_usb2_phy *tphy) } debugfs_create_atomic_t("drop_next_power_down_ack", 0600, tphy->debugfs, &tphy->drop_next_power_down_ack); + /* Set by a wake that failed with the host kept; clear it to let the port try again. */ + debugfs_create_bool("sleep_no_keep", 0600, tphy->debugfs, &tphy->sleep_no_keep); ret = devm_add_action_or_reset(tphy->dev, t6040_debugfs_remove, tphy->debugfs); if (ret) tphy->debugfs = NULL; @@ -3384,7 +3386,7 @@ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) if (!kept && tphy->sleep_ret < 0 && tphy->sleep_ret != -EBUSY) { tphy->sleep_no_keep = true; dev_warn(tphy->dev, - "PHY did not come back from a sleep with the host kept (%d): the host stops for sleeps from now on\n", + "PHY did not come back from a sleep with the host kept (%d): the host stops for sleeps from now on (debugfs sleep_no_keep)\n", tphy->sleep_ret); } tphy->sleep_armed = false; From c89e3941d3b39f5e6446ecac5af979e310189a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Wed, 11 Nov 2026 16:02:42 +0100 Subject: [PATCH 219/269] phy: apple: t6040: keep the host of a pin D port through a sleep while DP is down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A dock asks for DisplayPort alt mode with pin assignment D whether or not a display is on it, and a port in DP alt mode stopped its host for a deep sleep: the dock's hub, network adapter and disks left the bus at every S2R as before. Pin assignment D leaves USB3 its pair. Until the DCP powers the DP phy on, nothing is programmed for the other pair: the mode is only latched for that power-on, and the PHY is the one of a plain USB port. Let such a port sleep in place. With a DisplayPort link up, and with DisplayPort's four lanes (pin assignments C and E), the host stops as before. This makes one more order possible. After the sleep the Type-C controller can report a display before the host's resume has brought the PHY back: one that came to the dock during the sleep, or a DP adapter in the place of a USB device. DP's power-on would find the USB3 half down (pin D: no link), or would program lanes that the wake then takes for USB3. Mark the ports that are down when the system starts to resume (.resume_early, before the Type-C controller's resume can report anything) and have DP's power-on wait for the host's wake on them, 2 s at most: it then finds the port as on any running host. While the system still suspends nothing waits, and DP on a port that is down for the sleep gets no link. KUnit: pin D without a link sleeps in place, with USB2 alone or with USB3; a link, or DisplayPort's four lanes, do not. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 11 +++- drivers/phy/apple/t6040-usb2.c | 82 +++++++++++++++++++++++-- drivers/usb/dwc3/dwc3-apple.c | 6 +- 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 197338fdb007e..644b672cf39db 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -94,7 +94,11 @@ static void t6040_sleep_no_host_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); } -/* DisplayPort on the lanes, with any pin assignment, and USB4/Thunderbolt stop the host. */ +/* + * DisplayPort's four lanes (pin assignments C and E), a DisplayPort link and + * USB4/Thunderbolt stop the host. Pin assignment D without a link does not: + * USB2 alone or with USB3, it is a USB port until the DP phy powers on. + */ static void t6040_sleep_other_modes_test(struct kunit *test) { struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); @@ -102,12 +106,15 @@ static void t6040_sleep_other_modes_test(struct kunit *test) tphy->dp_alt = true; KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); tphy->dp_two_lanes = true; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); tphy->usb3_up = true; + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); tphy->pipe_switched = true; + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + tphy->dp_up = true; KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); tphy->dp_alt = false; tphy->dp_two_lanes = false; - tphy->dp_up = true; KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); tphy->dp_up = false; KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index e80b20e0e94f7..a0862e746e238 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -56,6 +56,7 @@ #include #include #include +#include #define T6040_USB2_USBCTL 0x00 #define T6040_USB2_USBCTL_MODE GENMASK(2, 0) @@ -176,6 +177,8 @@ #define T6040_CPU_MSG_POWER_DOWN 0x0100000000000000ULL #define T6040_RECOVERY_RETRIES 1 +/* DP's power-on waits this long for the wake of a host kept through a sleep */ +#define T6040_SLEEP_DP_WAIT_MS 2000 static unsigned int recovery_dwell_ms = 50; module_param(recovery_dwell_ms, uint, 0644); @@ -323,6 +326,8 @@ struct t6040_usb2_phy { bool sleep_reset; /* the controller held in reset for it, behind its consumer */ bool sleep_usb3; /* USB3 ran when it went down, and comes back with USB2 */ bool sleep_waking; /* in that wake: no power cycle of the PHY's domains */ + bool sleep_back; /* the system resumes and that wake is still to run */ + wait_queue_head_t sleep_wq; /* DP's power-on waits here for sleep_back to end */ bool sleep_no_keep; /* a wake of its own failed: the host stops for later sleeps */ int sleep_ret; /* what the wake has come to, for the controller's glue */ }; @@ -542,6 +547,16 @@ static int t6040_usb2_off_locked(struct t6040_usb2_phy *tphy) return 0; } +/* The wake of a sleep with the host kept has run, or won't: DP stops waiting for it. */ +static void t6040_sleep_back_done(struct t6040_usb2_phy *tphy) +{ + lockdep_assert_held(&tphy->lock); + if (!tphy->sleep_back) + return; + WRITE_ONCE(tphy->sleep_back, false); + wake_up_all(&tphy->sleep_wq); +} + static int t6040_usb2_power_off(struct phy *phy) { struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); @@ -559,6 +574,7 @@ static int t6040_usb2_power_off(struct phy *phy) return ret; tphy->powered = false; tphy->sleep_down = false; + t6040_sleep_back_done(tphy); return 0; } @@ -1734,11 +1750,31 @@ static void t6040_usb3_reclaim_locked(struct t6040_usb2_phy *tphy) /* ---- DP phy_ops ---- */ +/* + * A port that is down for a sleep with its host kept, while the system + * resumes: the Type-C controller can report a display before the host's + * resume has brought the PHY back, one that came to a dock during the sleep + * or an adapter in the place of the USB device. DP waits for that wake, and + * then finds the port as on any running host: USB3 up for pin assignment D + * to share, or to lend its lanes. If the wake fails or doesn't come, DP goes + * on and gets what it gets on a port without a host. + */ +static void t6040_dp_wait_host(struct t6040_usb2_phy *tphy) +{ + if (!READ_ONCE(tphy->sleep_back)) + return; + if (!wait_event_timeout(tphy->sleep_wq, !READ_ONCE(tphy->sleep_back), + msecs_to_jiffies(T6040_SLEEP_DP_WAIT_MS))) + dev_warn(tphy->dev, "DP: the host's wake from the sleep did not come in %u ms\n", + T6040_SLEEP_DP_WAIT_MS); +} + static int t6040_dp_power_on(struct phy *phy) { struct t6040_usb2_phy *tphy = phy_get_drvdata(phy); int ret; + t6040_dp_wait_host(tphy); guard(mutex)(&tphy->lock); if (!tphy->dp_ok || tphy->dp_up) return 0; @@ -1751,8 +1787,12 @@ static int t6040_dp_power_on(struct phy *phy) return -ENOLINK; if (!tphy->usb3_up) t6040_usb3_up_locked(tphy); - if (!tphy->usb3_up) + if (!tphy->usb3_up) { + if (tphy->sleep_down) + dev_info(tphy->dev, + "DP PHY pin D: the port is down for a sleep, no link\n"); return -ENOLINK; + } /* USB3 startup already applied these DPTX tunables before firmware. */ t6040_dp_aux_init(tphy); tphy->dp_rate = -1; @@ -3076,6 +3116,7 @@ static int t6040_usb2_probe(struct platform_device *pdev) */ tphy->mode = PHY_MODE_USB_HOST; mutex_init(&tphy->lock); + init_waitqueue_head(&tphy->sleep_wq); atomic_set(&tphy->drop_next_power_down_ack, 0); platform_set_drvdata(pdev, tphy); @@ -3187,9 +3228,16 @@ static struct t6040_usb2_phy *t6040_from_usb2_phy(struct phy *phy) /* * A USB2 host that runs, alone or with USB3 on this PHY and the PIPE on it. - * DisplayPort on the lanes, with any pin assignment, and USB4/Thunderbolt have - * clients of their own, which come back in their own order: such a port's - * host stops for the sleep. + * DisplayPort on the lanes and USB4/Thunderbolt have clients of their own, + * which come back in their own order: such a port's host stops for the + * sleep. So does one whose four lanes are DisplayPort's by its pin assignment + * (C, E), link or not. + * + * Pin assignment D without a DisplayPort link is a USB port to the PHY: a + * dock with no display on it. USB3 runs on its pair as on any other port, + * nothing is programmed for the other pair before the DCP powers the DP phy + * on, and the mode is only latched here for that. With a link up the port + * stops its host like the others. */ static bool t6040_can_sleep_in_place(const struct t6040_usb2_phy *tphy) { @@ -3197,8 +3245,8 @@ static bool t6040_can_sleep_in_place(const struct t6040_usb2_phy *tphy) tphy->mode == PHY_MODE_USB_HOST && !tphy->shutdown_failed && !tphy->sleep_no_keep && !tphy->sleep_down && !tphy->sleep_reset && !tphy->cio_up && !tphy->pipe_tunneled && !tphy->router_running && - !tphy->dp_up && !tphy->dp_alt && !tphy->usb3_lent && - tphy->usb3_up == tphy->pipe_switched; + !tphy->dp_up && (!tphy->dp_alt || tphy->dp_two_lanes) && + !tphy->usb3_lent && tphy->usb3_up == tphy->pipe_switched; } int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) @@ -3260,6 +3308,7 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) t6040_dwc3_reset_hold_locked(tphy); tphy->sleep_down = true; tphy->sleep_reset = true; + WRITE_ONCE(tphy->sleep_back, false); dev_info(tphy->dev, "PHY down for the sleep with the host kept, USB3 %s\n", tphy->sleep_usb3 ? "was up" : "was off"); return 0; @@ -3321,6 +3370,8 @@ int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) /* Only for a controller this sleep's result has taken out of reset. */ if (!tphy->sleep_armed || tphy->sleep_reset) return -EINVAL; + /* Whatever this comes to, DP has nothing more to wait for. */ + t6040_sleep_back_done(tphy); if (!tphy->sleep_down) return tphy->sleep_ret < 0 ? tphy->sleep_ret : 0; @@ -3380,6 +3431,7 @@ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) return; guard(mutex)(&tphy->lock); + t6040_sleep_back_done(tphy); if (!tphy->sleep_armed) return; /* A sleep refused for a USB change doesn't count, nor a restart for another reason. */ @@ -3536,8 +3588,26 @@ static int t6040_usb2_poweroff(struct device *dev) return t6040_usb2_sleep(dev, "poweroff"); } +/* + * A port that went down for the sleep with its host kept comes back in the + * host's resume. From here until then DP waits for it in its power-on, see + * t6040_dp_wait_host(). Early, before any .resume: the Type-C controller + * reports a display from its own. + */ +static int t6040_usb2_resume_early(struct device *dev) +{ + struct t6040_usb2_phy *tphy = dev_get_drvdata(dev); + + if (!tphy) + return 0; + guard(mutex)(&tphy->lock); + WRITE_ONCE(tphy->sleep_back, tphy->sleep_down); + return 0; +} + static const struct dev_pm_ops t6040_usb2_pm_ops = { .suspend = t6040_usb2_suspend, + .resume_early = t6040_usb2_resume_early, .freeze = t6040_usb2_freeze, .poweroff = t6040_usb2_poweroff, #ifdef CONFIG_HIBERNATION diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index ac49b5a93a67f..aab8636f221a5 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -133,9 +133,9 @@ * * The same steps run when the sleep is refused late or is a dry run. If any of them fails, * the host is restarted the old way once the system has resumed, and the devices enumerate - * again; a core that may be unpowered is left alone until its reset releases. A port in a - * DisplayPort mode or in USB4/Thunderbolt stops for the sleep as before, and - * dwc3_apple.typec_sleep_in_place=0 makes every Type-C port do so. + * again; a core that may be unpowered is left alone until its reset releases. A port with a + * DisplayPort link, or whose four lanes are DisplayPort's, or in USB4/Thunderbolt stops for + * the sleep as before, and dwc3_apple.typec_sleep_in_place=0 makes every Type-C port do so. * * Runtime PM (T6040 and T8132, see dwc3_apple_core_probe()): the glue holds a reference from * the reset deassert to the end of the shutdown, so with no cable the controller can From bd442d1203a4d298793d55c9ff238be4611ce683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:23:41 +0100 Subject: [PATCH 220/269] phy: apple: t6040: drop the two CIO test switches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cio_usb3_tunnel routed the controller's USB3 PIPE to a running USB4 router without the router's request, and cio_without_router let a USB4/Thunderbolt mux event take a port into CIO with no router enabled for it. Both were test switches and off. The PIPE now goes to the router only on the router's request, and a port enters CIO only while its router is enabled in the live device tree. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index a0862e746e238..3eadff2928134 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2261,14 +2261,9 @@ EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_open); * * USB3 runs tunnelled through the router: the controller's PIPE goes to the * USB4 backend (the USB3 mode 3, see t6040_cio_pipe_tunnel) - * while the PHY is in CIO, only with the cio_usb3_tunnel parameter set. + * while the PHY is in CIO and the router has asked for it. */ -static bool cio_usb3_tunnel; -module_param(cio_usb3_tunnel, bool, 0644); -MODULE_PARM_DESC(cio_usb3_tunnel, - "Route the USB3 PIPE to a running USB4 router without its request (testing)"); - /* The ADT tables of the CIO-only tunables, in order; lane0/1 are required. */ static const char * const t6040_cio_tunable_props[] = { "apple,tunable-cio-uc-regs", /* tunable_UC_REGS_CIO_DFLT, core + 0x400 */ @@ -2401,8 +2396,7 @@ static void t6040_cio_pipe_update_locked(struct t6040_usb2_phy *tphy) bool want; lockdep_assert_held(&tphy->lock); - want = tphy->cio_up && tphy->router_running && - (tphy->usb3_tunnel_req || cio_usb3_tunnel) && + want = tphy->cio_up && tphy->router_running && tphy->usb3_tunnel_req && tphy->usb3_on && tphy->usb3_pipe && tphy->dwc3_running && !tphy->pipe_switched; if (want && !tphy->pipe_tunneled) { t6040_cio_pipe_tunnel(tphy); @@ -2514,25 +2508,17 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) return ret; } -/* USB4 entered with Enter_USB in USB4 mode, or Thunderbolt alt mode. */ -static bool cio_without_router; -module_param(cio_without_router, bool, 0644); -MODULE_PARM_DESC(cio_without_router, - "bring-up test: enter CIO on a USB4/Thunderbolt mux event even while no USB4 router node for this port is enabled (default N)"); - /* * A USB4/Thunderbolt mux event only takes the port into CIO while the router * that runs over this PHY (a node whose apple,atc-phy points here) is enabled * in the live device tree. The default device trees keep the routers * disabled, so the port stays on its USB2/USB3/DP paths until a router is - * enabled (the USB4 bring-up tree, or at runtime). + * enabled. */ static bool t6040_cio_router_enabled(struct t6040_usb2_phy *tphy) { struct device_node *np; - if (READ_ONCE(cio_without_router)) - return true; for_each_compatible_node(np, NULL, "apple,t6040-usb4-acio") { struct device_node *phy = of_parse_phandle(np, "apple,atc-phy", 0); bool match = phy == tphy->dev->of_node && of_device_is_available(np); @@ -2546,6 +2532,7 @@ static bool t6040_cio_router_enabled(struct t6040_usb2_phy *tphy) return false; } +/* USB4 entered with Enter_USB in USB4 mode, or Thunderbolt alt mode. */ static bool t6040_cio_requested(const struct typec_mux_state *state) { const struct enter_usb_data *data = state->data; From a32a1b528e73224f9367027c0ef39b2133c103ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:24:41 +0100 Subject: [PATCH 221/269] usb: dwc3: apple: keep a Type-C port's host through a deep sleep without a switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typec_sleep_in_place let both ways of taking a T6040 Type-C port through a deep sleep be compared in one boot: on, the host and its USB devices are kept where the PHY can keep them; off, the host stops before the sleep and starts again after it. It was on. The host is now always kept where the PHY can, and dwc3_apple_sleep_may_keep() loses the argument that carried the switch. A port with a DisplayPort link or in USB4/Thunderbolt still stops for the sleep, and a failed step still restarts the host the old way. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple-state-test.c | 24 ++++++++++-------------- drivers/usb/dwc3/dwc3-apple-state.h | 5 ++--- drivers/usb/dwc3/dwc3-apple.c | 9 +-------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple-state-test.c b/drivers/usb/dwc3/dwc3-apple-state-test.c index bf959f1851235..ad9e4059f79a8 100644 --- a/drivers/usb/dwc3/dwc3-apple-state-test.c +++ b/drivers/usb/dwc3/dwc3-apple-state-test.c @@ -61,29 +61,25 @@ static void dwc3_apple_role_while_kept_test(struct kunit *test) /* A fixed hub's host may always be kept; a Type-C port's only by a PHY that restarts it. */ static void dwc3_apple_sleep_may_keep_test(struct kunit *test) { - /* fixed hub (T8140): as before, whatever the Type-C parameter says */ - KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(true, false, true, DWC3_APPLE_HOST, + /* fixed hub (T8140) */ + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(true, false, DWC3_APPLE_HOST, false, false)); - KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(true, false, false, DWC3_APPLE_HOST, + /* Type-C port with the T6040 PHY, in host mode */ + KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(false, true, DWC3_APPLE_HOST, false, false)); - /* Type-C port with the T6040 PHY, in host mode: kept unless switched off */ - KUNIT_EXPECT_TRUE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_HOST, - false, false)); - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, false, DWC3_APPLE_HOST, - false, false)); /* any other Type-C port stops */ - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, false, true, DWC3_APPLE_HOST, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, false, DWC3_APPLE_HOST, false, false)); /* no host, no core, a glue that goes away, a core that didn't come back */ - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_NO_CABLE, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, DWC3_APPLE_NO_CABLE, false, false)); - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, DWC3_APPLE_PROBE_PENDING, false, false)); - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_DEVICE, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, DWC3_APPLE_DEVICE, false, false)); - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, true, DWC3_APPLE_HOST, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(false, true, DWC3_APPLE_HOST, true, false)); - KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(true, false, true, DWC3_APPLE_HOST, + KUNIT_EXPECT_FALSE(test, dwc3_apple_sleep_may_keep(true, false, DWC3_APPLE_HOST, false, true)); } diff --git a/drivers/usb/dwc3/dwc3-apple-state.h b/drivers/usb/dwc3/dwc3-apple-state.h index 7e5b00bc77a6d..266d9c477bff9 100644 --- a/drivers/usb/dwc3/dwc3-apple-state.h +++ b/drivers/usb/dwc3/dwc3-apple-state.h @@ -91,7 +91,6 @@ static inline enum dwc3_apple_state dwc3_apple_state_for_role_kept(enum usb_role * dwc3_apple_sleep_may_keep() - whether a deep sleep may find the host kept in place * @fixed_hub: the controller serves a fixed hub * @phy_restart: a Type-C port whose PHY keeps the host by the core's reset - * @typec_allowed: Type-C ports may sleep in place (the typec_sleep_in_place parameter) * @state: the controller's state * @shutting_down: the glue is going away * @dead: the core didn't come back from an earlier sleep @@ -101,11 +100,11 @@ static inline enum dwc3_apple_state dwc3_apple_state_for_role_kept(enum usb_role * * Return: true if the PHY is to be asked. */ -static inline bool dwc3_apple_sleep_may_keep(bool fixed_hub, bool phy_restart, bool typec_allowed, +static inline bool dwc3_apple_sleep_may_keep(bool fixed_hub, bool phy_restart, enum dwc3_apple_state state, bool shutting_down, bool dead) { - return (fixed_hub || (phy_restart && typec_allowed)) && state == DWC3_APPLE_HOST && + return (fixed_hub || phy_restart) && state == DWC3_APPLE_HOST && !shutting_down && !dead; } diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index aab8636f221a5..5b5dbc6a6db33 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -135,7 +135,7 @@ * the host is restarted the old way once the system has resumed, and the devices enumerate * again; a core that may be unpowered is left alone until its reset releases. A port with a * DisplayPort link, or whose four lanes are DisplayPort's, or in USB4/Thunderbolt stops for - * the sleep as before, and dwc3_apple.typec_sleep_in_place=0 makes every Type-C port do so. + * the sleep as before. * * Runtime PM (T6040 and T8132, see dwc3_apple_core_probe()): the glue holds a reference from * the reset deassert to the end of the shutdown, so with no cable the controller can @@ -238,12 +238,6 @@ struct dwc3_apple { #define to_dwc3_apple(d) container_of((d), struct dwc3_apple, dwc) -/* Read before the freezer of each sleep, so both ways can be compared in one boot. */ -static bool typec_sleep_in_place = true; -module_param(typec_sleep_in_place, bool, 0644); -MODULE_PARM_DESC(typec_sleep_in_place, - "Keep a Type-C port's host and its USB devices through a deep sleep where the PHY can (T6040); off, the host stops before the sleep and starts again after it (default Y)"); - /* * Apple Silicon dwc3 vendor-specific registers * @@ -1586,7 +1580,6 @@ static int dwc3_apple_pm_notify(struct notifier_block *nb, unsigned long action, scoped_guard(mutex, &appledwc->lock) in_place = dwc3_apple_sleep_may_keep(appledwc->fixed_hub, appledwc->sleep_restart, - READ_ONCE(typec_sleep_in_place), appledwc->state, appledwc->shutting_down, appledwc->sleep_dead); From b40c25ca9370555d9ce94647e4def2aa99d6e824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 08:29:18 +0100 Subject: [PATCH 222/269] thunderbolt, usb: apple: say what the T6040 code does, not how it was reached MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three texts still described the T6040 USB4 and Type-C code by the phase it was written in: the description of thunderbolt_apple.t6040_dp_tunnels, the comment on the USB3 PIPE reset in dwc3-apple and the comment on a router that is described but disabled in tipd. Say why DP tunnelling is off by default and leave the rest as a plain description. No code changes. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 2 +- drivers/usb/dwc3/dwc3-apple.c | 2 +- drivers/usb/typec/tipd/core.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index c4f821e1d78ec..465dbaf5eb5eb 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -561,7 +561,7 @@ MODULE_PARM_DESC(dp_display, "Drive displays behind qualified Thunderbolt DP tun static bool t6040_dp_tunnels; module_param(t6040_dp_tunnels, bool, 0444); MODULE_PARM_DESC(t6040_dp_tunnels, - "T6040/T6041: tunnel DisplayPort from the routers' DP IN adapters (bring-up, default off)"); + "T6040/T6041: tunnel DisplayPort from the routers' DP IN adapters (default off: the DP IN adapter to dpin0/1 mapping is not decoded)"); /* * The M1 Pro/Max ATC is the t8103 generation: same DP IN adapter registers, diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 5b5dbc6a6db33..0b82e06cd5c58 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -464,7 +464,7 @@ static void dwc3_apple_set_ptrcap(struct dwc3_apple *appledwc, u32 mode) /* * The T6040 core came up against the dummy PHY: reset its USB3 PIPE side once the PIPE has - * moved, as the tested bring-up does (USB2 is not affected). + * moved (USB2 is not affected). */ static void dwc3_apple_usb3_pipe_reset(struct dwc3_apple *appledwc) { diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 61525a79bf617..a637a85358386 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -2455,8 +2455,8 @@ EXPORT_SYMBOL_IF_KUNIT(cd321x_tbt_switch_keep_waiting); /* * The connection lookup skips a router whose node is disabled. A router that is - * described but disabled in the device tree can still be enabled at runtime (an - * overlay during bring-up), so the port watches for it as for a late router. + * described but disabled in the device tree can still be enabled at runtime (by + * an overlay), so the port watches for it as for a late router. */ static bool cd321x_tbt_router_described(struct fwnode_handle *fwnode) { From 2703d5df264987f77a2db9b37e97a7debfd19297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 20:12:14 +0100 Subject: [PATCH 223/269] Documentation: ABI: sn201202x: give the kernel version of hib_resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The entry lacked the KernelVersion field the other entries of this tree carry. hib_resume is new in 7.4. Signed-off-by: František Balázsy --- Documentation/ABI/testing/sysfs-driver-sn201202x | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/ABI/testing/sysfs-driver-sn201202x b/Documentation/ABI/testing/sysfs-driver-sn201202x index 967f4abb0596f..486fafb911dbe 100644 --- a/Documentation/ABI/testing/sysfs-driver-sn201202x +++ b/Documentation/ABI/testing/sysfs-driver-sn201202x @@ -1,5 +1,6 @@ What: /sys/bus/spmi/devices//hib_resume Date: November 2026 +KernelVersion: 7.4 Contact: František Balázsy Description: (RW) State a resume from hibernation left an Apple SN201202x From 13b1b9ec736d06c1f18f5e7a8d1b8659d9b06355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:06:46 +0100 Subject: [PATCH 224/269] thunderbolt: apple: do not wait for the router stop in PM_POST_* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A T6040 port with a PCIe-C host is stopped for system sleep from a worker: a PCIe endpoint driver that the stop unbinds may unregister a PM notifier, which has to wait until the PM notifier chain is idle. The router's .prepare waits for the worker. A sleep that is abandoned before that .prepare (a wakeup event while tasks freeze, a task that does not freeze, a notifier or an earlier device that refuses the sleep) reaches PM_POST_* with the worker still running. The notifier then waited for it for up to 60 s, inside the chain, while the worker, with such an endpoint behind the port (the USB Ethernet of a Thunderbolt dock, for one), waited for the chain. The wake stalled for the whole 60 s. The notifier then gave up without queueing the restart, so the port stayed closed until the next system sleep: cable changes were only recorded, a replug included. Do not wait in the notifier, and do not take the lock there while the stop may hold it: mark the stop as left behind and let the restart work wait for it. The next sleep may begin before that work has run. The stop then serves that sleep as well, as its .prepare waits for it, and the restart work stands down, so that PM_*_PREPARE does not have to wait for the work, and through it for the stop, either. sleep_hibernating is written by the PM notifier and read by .complete, which the PM core runs one after the other, so it does not need the lock that the notifier now avoids. A deferred stop can still run while tasks freeze; say so where it is queued, and put the comment about unfrozen work back on the direct teardown it was written for. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 100 ++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 22 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 465dbaf5eb5eb..d2bf88b73c888 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -377,6 +377,19 @@ MODULE_PARM_DESC(cio_s2r, "Enable J713 attached-dock system sleep (default: on)" struct apple_dpin_binding; +/* + * hw->sleep_stop: where the stop of a port with a PCIe-C host stands, which + * the PM notifier leaves to sleep_stop_work. While that work runs it holds + * the lock, and a PCIe endpoint driver it unbinds may wait for the PM notifier + * chain to end. The notifier therefore waits neither for the work nor for the + * lock unless this says that the work is over. + */ +enum apple_cio_sleep_stop { + APPLE_CIO_SLEEP_STOP_NONE, /* not deferred, or seen to be over */ + APPLE_CIO_SLEEP_STOP_QUEUED, /* for the sleep that is under way */ + APPLE_CIO_SLEEP_STOP_ORPHAN, /* that sleep ended, the stop has not */ +}; + /** * struct apple_cio - Apple Converged I/O host router complex * @dev: ACIO device @@ -398,7 +411,7 @@ struct apple_dpin_binding; * @sleep_hibernating: Hibernation is under way; thaw does not reopen the port * @sleep_stop_work: Stops a port with a PCIe-C host for system sleep * @sleep_stop_done: Signals that @sleep_stop_work has finished - * @sleep_stop_queued: @sleep_stop_work was queued for this sleep + * @sleep_stop_state: Where @sleep_stop_work stands for the PM notifier * @phy_np: Type-C PHY the router runs over (hw->cio_phy) */ struct apple_cio { @@ -464,11 +477,11 @@ struct apple_cio { bool sleep_restart_owed; /* protected by lock */ bool sleep_aborted_early; /* protected by lock: devices never suspended */ /* hw->sleep_stop: hibernation is under way, thaw does not reopen */ - bool sleep_hibernating; /* protected by lock */ + bool sleep_hibernating; /* PM notifier and complete only */ /* hw->sleep_stop: the stop of a port with a PCIe-C host runs here */ struct work_struct sleep_stop_work; struct completion sleep_stop_done; - bool sleep_stop_queued; /* PM notifier and prepare only */ + atomic_t sleep_stop_state; /* enum apple_cio_sleep_stop */ /* hw->cio_phy: the Type-C PHY this router runs over */ struct device_node *phy_np; @@ -3682,7 +3695,7 @@ static int apple_cio_prepare(struct device *dev) * device suspends: the PHY refuses to sleep under a running router. * Notifiers are done and the PM core holds no lock the stop needs. */ - if (READ_ONCE(acio->sleep_stop_queued) && + if (atomic_read(&acio->sleep_stop_state) == APPLE_CIO_SLEEP_STOP_QUEUED && !wait_for_completion_timeout(&acio->sleep_stop_done, msecs_to_jiffies(APPLE_CIO_SLEEP_STOP_TIMEOUT_MS))) { dev_err(dev, "router did not stop for system sleep, aborting it\n"); @@ -3905,6 +3918,25 @@ static void apple_cio_sleep_restart_work(struct work_struct *work) struct apple_cio *acio = container_of(to_delayed_work(work), struct apple_cio, sleep_restart_work); + /* + * A sleep that was abandoned before this device's .prepare may have + * left its deferred stop running: wait for it here, outside the PM + * notifier chain. If another sleep has begun by then, the stop is + * that sleep's, whose PM_POST_* queues this work again. + */ + switch (atomic_read(&acio->sleep_stop_state)) { + case APPLE_CIO_SLEEP_STOP_QUEUED: + return; + case APPLE_CIO_SLEEP_STOP_ORPHAN: + flush_work(&acio->sleep_stop_work); + if (atomic_cmpxchg(&acio->sleep_stop_state, + APPLE_CIO_SLEEP_STOP_ORPHAN, + APPLE_CIO_SLEEP_STOP_NONE) != + APPLE_CIO_SLEEP_STOP_ORPHAN) + return; + break; + } + scoped_guard(mutex, &acio->lock) { acio->sleep_aborted_early = false; if (!acio->sleep_restart_owed) @@ -3963,16 +3995,28 @@ static void apple_cio_sleep_stop(struct apple_cio *acio, bool hibernate) { bool defer; + acio->sleep_hibernating = hibernate; + /* + * The stop of a sleep that was abandoned may still be running. It + * stops the router for this sleep as well, and the restart work + * behind it stands down; neither is waited for here. + */ + if (atomic_cmpxchg(&acio->sleep_stop_state, APPLE_CIO_SLEEP_STOP_ORPHAN, + APPLE_CIO_SLEEP_STOP_QUEUED) == APPLE_CIO_SLEEP_STOP_ORPHAN) + return; + + /* Nothing from an earlier sleep may restart the port during this one. */ + cancel_delayed_work_sync(&acio->sleep_restart_work); cancel_delayed_work_sync(&acio->resume_link_work); scoped_guard(mutex, &acio->lock) { - acio->sleep_hibernating = hibernate; acio->sleep_aborted_early = false; /* * PCIe endpoints may unregister PM notifiers on removal, which * would deadlock inside this notifier chain. A port with a PCIe-C - * host stops from a worker instead, which this device's .prepare - * waits for before any device suspends. + * host stops from a worker instead. That may still run while + * tasks freeze; this device's .prepare waits for it before any + * device suspends. */ scoped_guard(mutex, &acio->pcie_tunnel_lock) defer = acio->current_cable_info && acio->pcie_tunnel_populated; @@ -3983,10 +4027,29 @@ static void apple_cio_sleep_stop(struct apple_cio *acio, bool hibernate) } reinit_completion(&acio->sleep_stop_done); - WRITE_ONCE(acio->sleep_stop_queued, true); + atomic_set(&acio->sleep_stop_state, APPLE_CIO_SLEEP_STOP_QUEUED); queue_work(system_dfl_wq, &acio->sleep_stop_work); } +/* + * PM_POST_*: whether the notifier may take the lock. A deferred stop is over + * once this device's .prepare has run. A sleep abandoned before that can find + * it still running, and waiting for it here would keep a PCIe endpoint that + * unregisters its PM notifier, and so the stop itself, from finishing until + * the wait gives up. Such a stop is left to the restart work. + */ +static bool apple_cio_sleep_stop_over(struct apple_cio *acio) +{ + if (atomic_read(&acio->sleep_stop_state) == APPLE_CIO_SLEEP_STOP_NONE) + return true; + if (completion_done(&acio->sleep_stop_done)) { + atomic_set(&acio->sleep_stop_state, APPLE_CIO_SLEEP_STOP_NONE); + return true; + } + atomic_set(&acio->sleep_stop_state, APPLE_CIO_SLEEP_STOP_ORPHAN); + return false; +} + static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, void *unused) { @@ -4005,14 +4068,14 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, break; fallthrough; case PM_SUSPEND_PREPARE: - /* Nothing from an earlier sleep may restart the port during this one. */ - cancel_delayed_work_sync(&acio->sleep_restart_work); - /* Before tasks freeze: tearing down the NHI needs unfrozen work. */ if (stop) { apple_cio_sleep_stop(acio, event != PM_SUSPEND_PREPARE); break; } + /* Nothing from an earlier sleep may restart the port during this one. */ + cancel_delayed_work_sync(&acio->sleep_restart_work); apple_cio_sleep_close_owed(acio); + /* Before tasks freeze: tearing down the NHI needs unfrozen work. */ if (READ_ONCE(s2idle_teardown)) apple_cio_sleep_teardown(acio); break; @@ -4022,16 +4085,10 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, break; fallthrough; case PM_POST_SUSPEND: - /* A deferred stop is over once .prepare has run; not if it never did. */ - if (READ_ONCE(acio->sleep_stop_queued)) { - /* As .prepare: a stop that never ends must not hang PM forever. */ - if (!wait_for_completion_timeout(&acio->sleep_stop_done, - msecs_to_jiffies(APPLE_CIO_SLEEP_STOP_TIMEOUT_MS))) { - dev_err(acio->dev, - "router stop for system sleep did not finish, not restarting the port\n"); - break; - } - WRITE_ONCE(acio->sleep_stop_queued, false); + acio->sleep_hibernating = false; + if (!apple_cio_sleep_stop_over(acio)) { + queue_delayed_work(system_dfl_wq, &acio->sleep_restart_work, 0); + break; } scoped_guard(mutex, &acio->lock) { /* @@ -4044,7 +4101,6 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, acio->sleep_phase = APPLE_CIO_SLEEP_RESUMED; acio->sleep_aborted_early = true; } - acio->sleep_hibernating = false; owed = acio->sleep_restart_owed; /* A cable recorded while closed has nobody else to start it. */ early = acio->sleep_aborted_early || From 389e7d65b8c1a5c497adb0b9b8afc0558a98e3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:20:21 +0100 Subject: [PATCH 225/269] thunderbolt: apple: key the LSTX level at router stop on T6040 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a router stops, the AP takes LSTX back as a GPIO at the level the router left it. For T6040 that became "high unless the router drives the pin low", so that a pin its firmware released is held high as well, but the rule was keyed on type7. T6050 is Type 7 too: for it a released pin (LSTX control 0) changed from low to high, and for it and for T8132 the control word was now read at every stop, also with the cable gone, where it had only been read with the cable still in. Neither can be checked on a T6040. Give the rule its own match data field, set for T6040 only, and put back the statement that the other routers had. For T6040 nothing changes. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index d2bf88b73c888..1e31d35bd8eaa 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -154,6 +154,11 @@ struct apple_cio_hw { bool skip_run; /* Hand LSTX to the router after RTKit boot, not before it. */ bool lstx_after_boot; + /* + * A stopped router left LSTX high unless it drives the pin low: one + * its firmware released counts as high as well. + */ + bool lstx_released_high; bool strict_fw_ready; /* Keep the links of the host router out of CL states. */ bool no_host_clx; @@ -266,6 +271,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { .lstx_explicit = true, .skip_run = true, .lstx_after_boot = true, + .lstx_released_high = true, .strict_fw_ready = true, .no_host_clx = true, .cio_phy = true, @@ -3173,17 +3179,17 @@ static void apple_cio_stop(struct apple_cio *acio) acio->rtk = NULL; apple_cio_cpu_halt(acio); - /* - * High only if the router left it high with the cable still in. Type 7 - * follows the platform (its disable of the embedded CPU): anything but - * "driven low" (2) counts as high, including a pin the firmware released. - */ - if (acio->hw->lstx_after_boot) { + if (acio->hw->lstx_released_high) { u32 lstx = readl(acio->rc_base + APPLE_CIO_M3_CTRL); - bool high = acio->type7 ? lstx != APPLE_CIO_LSTX_CTRL_ENABLE : - lstx & APPLE_CIO_LSTX_CTRL_LEVEL; - apple_cio_lstx_hold(acio, acio->target_cable_info && high); + /* High unless the router drives it low, with the cable still in */ + apple_cio_lstx_hold(acio, acio->target_cable_info && + lstx != APPLE_CIO_LSTX_CTRL_ENABLE); + } else if (acio->hw->lstx_after_boot) { + /* High only if the router left it driven high with the cable still in */ + apple_cio_lstx_hold(acio, acio->target_cable_info && + (readl(acio->rc_base + APPLE_CIO_M3_CTRL) & + APPLE_CIO_LSTX_CTRL_LEVEL)); } /* Finally, remove the links to the PD domains to power everything off */ From 2d2ee876a5cf75892f625b2af7f71679f3424d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:51:03 +0100 Subject: [PATCH 226/269] thunderbolt: apple: keep the T6040 DP IN adapters disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Which DP IN adapter of a T6040 router reaches which display pipe is not known, so no DisplayPort tunnel can be set up through it. The policy for it was wired up all the same: the M3 policy in the match data, T6040 and T6041 in the list of SoCs with a qualified route, an override in probe that disabled it again, and a t6040_dp_tunnels parameter to lift the override. Describe the router as what it is, without a DP IN policy, as the match data had it before, and drop the parameter, the two entries, the override and the test cases for the entries. With the parameter at its default, which is the only way it was used, the router ends up with the same disabled policy as before. A display on a T6040 Type-C port is driven in DisplayPort alternate mode by the port's PHY and does not go through the router. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple-dpin-test.c | 3 --- drivers/thunderbolt/apple.c | 18 +----------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/drivers/thunderbolt/apple-dpin-test.c b/drivers/thunderbolt/apple-dpin-test.c index 119c2d4466970..dd532a2a051b5 100644 --- a/drivers/thunderbolt/apple-dpin-test.c +++ b/drivers/thunderbolt/apple-dpin-test.c @@ -362,9 +362,6 @@ static void apple_dpin_qualification_profiles(struct kunit *test) { "apple,t8132", &apple_dpin_m4, false, &apple_dpin_m4 }, { "apple,t6050", &apple_dpin_m5, true, &apple_dpin_m5 }, { "apple,t6050", &apple_dpin_m5, false, &apple_dpin_m5 }, - /* T6040/T6041 qualify; the router keeps them off without t6040_dp_tunnels. */ - { "apple,t6040", &apple_dpin_m3, true, &apple_dpin_m3 }, - { "apple,t6041", &apple_dpin_m3, true, &apple_dpin_m3 }, { "apple,unknown", &apple_dpin_m1, true, &apple_dpin_disabled }, }; unsigned int i; diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 1e31d35bd8eaa..7585993f92df2 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -277,13 +277,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { .cio_phy = true, .sleep_stop = true, .tbt_settle = true, - /* - * The DP IN adapter handshake is the t8103 one, as on M3 (the platform - * has one handshake for every SoC). Off unless - * t6040_dp_tunnels is set: the DP IN adapter to dpin0/1 mapping is - * not decoded. - */ - .dp = &apple_dpin_m3, + .dp = &apple_dpin_disabled, }; /* The platform sleeps 400 ms on the embedded CPU's enable if CIO generation < 3. */ @@ -577,11 +571,6 @@ static bool dp_display = true; module_param(dp_display, bool, 0444); MODULE_PARM_DESC(dp_display, "Drive displays behind qualified Thunderbolt DP tunnels (default: true)"); -static bool t6040_dp_tunnels; -module_param(t6040_dp_tunnels, bool, 0444); -MODULE_PARM_DESC(t6040_dp_tunnels, - "T6040/T6041: tunnel DisplayPort from the routers' DP IN adapters (default off: the DP IN adapter to dpin0/1 mapping is not decoded)"); - /* * The M1 Pro/Max ATC is the t8103 generation: same DP IN adapter registers, * same crossbar and the same tunnel pixel clock sequence. @@ -598,9 +587,6 @@ static const struct of_device_id apple_dpin_qualified_soc[] = { { .compatible = "apple,t8132" }, /* T6050: t8103 DP IN handshake, M3 flow, four DP IN adapters */ { .compatible = "apple,t6050" }, - /* M4 Pro/Max: only with t6040_dp_tunnels, see apple_cio_t6040_hw. */ - { .compatible = "apple,t6040" }, - { .compatible = "apple,t6041" }, {}, }; @@ -4331,8 +4317,6 @@ static int apple_cio_probe(struct platform_device *pdev) acio->dp_dual_stream = apple_dp_tunnel_dual_stream(acio->connector_np); /* Register layout, root qualification and connector wiring are separate. */ acio->dp = apple_dpin_policy_select(acio->hw->dp, of_root, acio->dp_dual_stream); - if (acio->hw->cio_phy && !t6040_dp_tunnels) - acio->dp = &apple_dpin_disabled; if (acio->connector_np) { ret = devm_add_action_or_reset(dev, apple_cio_of_node_put, acio->connector_np); From f3af78b09be219bbdbaffdab48cf2adec05a3975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:51:22 +0100 Subject: [PATCH 227/269] usb: typec: tipd: give back what tipd_suspend() took after a cold boot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The image of a hibernation is made after tipd_suspend(), which disables the event interrupt and arms it as a wakeup source on a controller that wakes the system, asserts the reset line on one that does not, and stops the poll of a controller without an interrupt. A resume from the image that restores the controller in place set the controller up again but gave none of that back: such a controller would have come out of hibernation with its event interrupt still disabled, or without its poll, and no port event after it. The SN201202x controllers of the Apple T6040, the only ones restored in place so far, have an event interrupt, no wakeup-source and no reset line, so nothing was left undone on them. Split the part of tipd_resume() that undoes tipd_suspend() out and call it at the end of the set-up after a cold boot as well. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/core.c | 45 ++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index a637a85358386..3b061bc0ba862 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -3658,6 +3658,24 @@ int tipd_suspend(struct tps6598x *tps) } EXPORT_SYMBOL_GPL(tipd_suspend); +/* Give back what tipd_suspend() took: the interrupt, the reset line, the poll. */ +static void tipd_resume_events(struct tps6598x *tps) +{ + if (tps->wakeup) { + if (!tps->sleep_event) { + disable_irq_wake(tps->irq); + enable_irq(tps->irq); + } + } else if (tps->reset) { + gpiod_set_value_cansleep(tps->reset, 0); + msleep(TPS_SETUP_MS); + } + + if (!tps->irq) + queue_delayed_work(system_power_efficient_wq, &tps->wq_poll, + msecs_to_jiffies(POLL_INTERVAL)); +} + int tipd_resume(struct tps6598x *tps) { int ret; @@ -3675,19 +3693,7 @@ int tipd_resume(struct tps6598x *tps) return ret; } - if (tps->wakeup) { - if (!tps->sleep_event) { - disable_irq_wake(tps->irq); - enable_irq(tps->irq); - } - } else if (tps->reset) { - gpiod_set_value_cansleep(tps->reset, 0); - msleep(TPS_SETUP_MS); - } - - if (!tps->irq) - queue_delayed_work(system_power_efficient_wq, &tps->wq_poll, - msecs_to_jiffies(POLL_INTERVAL)); + tipd_resume_events(tps); return 0; } @@ -3697,8 +3703,9 @@ EXPORT_SYMBOL_GPL(tipd_resume); * Bring a controller that went through a cold boot (a resume from disk) back * to the state its probe leaves it in, without registering anything again: * system power state S0, application firmware running (patch loaded if it - * waits for one) and the interrupt mask the driver uses. The port state is - * read afresh by the revalidation that follows. + * waits for one) and the interrupt mask the driver uses. The image was made + * after tipd_suspend(), so what that took is given back as in tipd_resume(). + * The port state is read afresh by the revalidation that follows. */ int tipd_reinit_after_cold_boot(struct tps6598x *tps) { @@ -3717,7 +3724,13 @@ int tipd_reinit_after_cold_boot(struct tps6598x *tps) if (ret) return ret; } - return tps6598x_write64(tps, TPS_REG_INT_MASK1, cd321x_irq_mask(tps)); + ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, cd321x_irq_mask(tps)); + if (ret) + return ret; + + tipd_resume_events(tps); + + return 0; } EXPORT_SYMBOL_GPL(tipd_reinit_after_cold_boot); From 89ad7db15bfec37dd2d402aa234b2b3084e20cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:51:41 +0100 Subject: [PATCH 228/269] usb: typec: tipd: sn201202x: revalidate a port restored from disk itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a resume from disk the port controller is set up again in place, but its port is only revalidated once the USB role switch of the connector is there: the USB controller that provides it is bound again after the resume and registers a new switch then. That last step was left to user space, which had to write "roles" to the hib_resume attribute after binding the USB controller. Without the write the port stayed as the image had left it, with no cable event applied, until the next system sleep; and two writers at once could both pass the unlocked test of the pending flag and revalidate twice. Do it in the kernel. If the lookup succeeds when the restore completes, as it does for a connector without a role switch, the port revalidates there. Otherwise the driver looks again whenever a USB controller with a role switch has bound, from a notifier on the platform bus that acts only while a port waits. The pending flag is tested and cleared under the controller's lock. hib_resume stays: it still reads "roles-pending" after an in-place restore until "roles" is written or the next sleep, which is what existing user space checks before it decides to probe the controller again, but the write no longer does the work and no longer fails while the USB controller is unbound. Signed-off-by: František Balázsy --- .../ABI/testing/sysfs-driver-sn201202x | 23 ++-- drivers/usb/typec/tipd/spmi.c | 117 ++++++++++++++---- drivers/usb/typec/tipd/tps6598x.h | 6 + 3 files changed, 113 insertions(+), 33 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-driver-sn201202x b/Documentation/ABI/testing/sysfs-driver-sn201202x index 486fafb911dbe..39d8040b6d32d 100644 --- a/Documentation/ABI/testing/sysfs-driver-sn201202x +++ b/Documentation/ABI/testing/sysfs-driver-sn201202x @@ -3,22 +3,23 @@ Date: November 2026 KernelVersion: 7.4 Contact: František Balázsy Description: - (RW) State a resume from hibernation left an Apple SN201202x - USB Type-C port controller in. Reads one of: + (RW) How the last resume from hibernation left an Apple + SN201202x USB Type-C port controller. Reads one of: none - No resume from hibernation is pending for this port. + No resume from hibernation since the last system + sleep, or "roles" was written since. roles-pending The controller was restored in place after the cold - boot behind the resume; the port is revalidated once - the USB controller that provides its role switch has - been bound again. + boot behind the resume. The kernel revalidates the + port, without taking the port or a display on it + down, once the USB controller that provides its role + switch is bound. failed The in-place restore failed; the controller has to be probed again (unbind and bind the device). - Writing "roles" while the state is "roles-pending" looks the - USB role switch up again and revalidates the port, without - taking the port, or a display on it, down. Other values fail - with -EINVAL, a write in another state with -EALREADY, and - -EAGAIN means the USB role switch is not registered yet. + Writing "roles" while the state is "roles-pending" takes note + of it: the state then reads "none". The write is not needed + for the port to be revalidated. Other values fail with + -EINVAL, a write in another state with -EALREADY. diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 229809c59ee3e..ea026021d2d2a 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -716,6 +717,60 @@ static bool sn201202x_sleep_event(struct tps6598x *tps) return true; } +/* + * A port restored from disk is revalidated once its USB role switch is there. + * The USB controller that provides it does not come through a resume from disk + * bound, and its new probe registers a new switch: the one looked up before + * the image was made is unregistered and only refuses requests. Take the new + * one and let the port read its cable afresh, without taking the port, or a + * display on it, down. + * + * Return: 0 once the port revalidates, or why the switch could not be taken. + */ +static int sn201202x_roles_finish(struct sn201202x *sn) +{ + struct tps6598x *tps = &sn->cd.tps; + int ret; + + ret = tipd_refresh_role_switch(tps); + if (ret) + return ret; + scoped_guard(mutex, &tps->lock) { + if (!sn->roles_pending) + return 0; + WRITE_ONCE(sn->roles_pending, false); + /* Refresh the detector snapshot before cable revalidation is thawed. */ + sn201202x_ldcm_refresh(tps); + } + tipd_complete(tps->dev); + dev_info(tps->dev, "resume from disk: USB role switch looked up again, port revalidates\n"); + return 0; +} + +static void sn201202x_role_work(struct work_struct *work) +{ + struct sn201202x *sn = container_of(work, struct sn201202x, role_work); + + if (READ_ONCE(sn->roles_pending)) + sn201202x_roles_finish(sn); +} + +/* + * A USB controller has bound, and its probe registers its role switch: a port + * that waits for one after a resume from disk looks again. + */ +static int sn201202x_role_notify(struct notifier_block *nb, unsigned long action, void *data) +{ + struct sn201202x *sn = container_of(nb, struct sn201202x, role_nb); + + if (action != BUS_NOTIFY_BOUND_DRIVER || !READ_ONCE(sn->roles_pending) || + !device_property_present(data, "usb-role-switch")) + return NOTIFY_DONE; + + queue_work(system_freezable_wq, &sn->role_work); + return NOTIFY_OK; +} + static int sn201202x_probe(struct spmi_device *device) { const struct of_device_id *match; @@ -761,6 +816,7 @@ static int sn201202x_probe(struct spmi_device *device) if (!of_property_read_bool(device->dev.of_node, "apple,no-liquid-detection")) tps->contaminant_ops = &sn201202x_contaminant_ops; INIT_DELAYED_WORK(&sn->ldcm_work, sn201202x_ldcm_work); + INIT_WORK(&sn->role_work, sn201202x_role_work); init_completion(&sn->select_completion); init_completion(&sn->sleep_completion); @@ -833,6 +889,12 @@ static int sn201202x_probe(struct spmi_device *device) tps->data->irq_handler(tps->irq, tps); sn201202x_idle_sleep_start(tps); + + sn->role_nb.notifier_call = sn201202x_role_notify; + ret = bus_register_notifier(&platform_bus_type, &sn->role_nb); + if (ret) + dev_warn(&device->dev, "cannot watch for the USB role switch: %d\n", ret); + sn->role_watch = !ret; return 0; } @@ -841,6 +903,9 @@ static void sn201202x_remove(struct spmi_device *device) struct tps6598x *tps = spmi_device_get_drvdata(device); struct sn201202x *sn = tps_to_sn(tps); + if (sn->role_watch) + bus_unregister_notifier(&platform_bus_type, &sn->role_nb); + disable_work_sync(&sn->role_work); /* The hold freeze took on one the restore left alone */ if (sn->cold_restored) pm_runtime_put_noidle(tps->dev); @@ -1021,7 +1086,10 @@ static int __maybe_unused sn201202x_restore(struct device *dev) enable_irq(tps->irq); /* Give back the hold freeze took, as resume does */ pm_runtime_put_autosuspend(dev); - sn->roles_pending = true; + scoped_guard(mutex, &tps->lock) { + WRITE_ONCE(sn->roles_pending, true); + sn->hib_in_place = true; + } dev_info(dev, "resume from disk: controller restored, port revalidation waits for the USB role switch\n"); return 0; } @@ -1038,7 +1106,10 @@ static int sn201202x_prepare(struct device *dev) struct sn201202x *sn = tps_to_sn(tps); int ret; - sn->roles_pending = false; + scoped_guard(mutex, &tps->lock) { + WRITE_ONCE(sn->roles_pending, false); + sn->hib_in_place = false; + } if (sn->cold_restored) return tipd_prepare(dev); @@ -1069,28 +1140,34 @@ static void sn201202x_complete(struct device *dev) /* A controller the restore from disk left alone is probed again instead. */ if (sn->cold_restored) return; + /* + * A port restored from disk waits for its USB role switch: now, if the + * USB controller is bound, or when sn201202x_role_notify() sees it bind. + */ + if (READ_ONCE(sn->roles_pending)) { + sn201202x_roles_finish(sn); + return; + } /* Refresh the detector snapshot before cable revalidation is thawed. */ scoped_guard(mutex, &tps->lock) sn201202x_ldcm_refresh(tps); - /* A port whose USB role switch is not back yet: hib_resume below starts it. */ - if (!sn->roles_pending) - tipd_complete(dev); + tipd_complete(dev); } /* - * hib_resume: the state a resume from disk left the controller in: "none", - * "roles-pending" (restored, waiting for the USB controller to be bound - * again) or "failed" (to be probed again). Writing "roles" once the USB - * controller is bound looks its role switch up again and revalidates the - * port, without taking the port (and a display on it) down. + * hib_resume: how the last resume from disk left the controller: "none", + * "roles-pending" (restored in place) or "failed" (to be probed again). The + * kernel revalidates a port restored in place by itself; writing "roles" only + * takes note of it, after which the attribute reads "none". */ static ssize_t hib_resume_show(struct device *dev, struct device_attribute *attr, char *buf) { struct tps6598x *tps = dev_get_drvdata(dev); struct sn201202x *sn = tps_to_sn(tps); + guard(mutex)(&tps->lock); return sysfs_emit(buf, "%s\n", sn->cold_restored ? "failed" : - sn->roles_pending ? "roles-pending" : "none"); + sn->hib_in_place ? "roles-pending" : "none"); } static ssize_t hib_resume_store(struct device *dev, struct device_attribute *attr, @@ -1098,20 +1175,16 @@ static ssize_t hib_resume_store(struct device *dev, struct device_attribute *att { struct tps6598x *tps = dev_get_drvdata(dev); struct sn201202x *sn = tps_to_sn(tps); - int ret; if (!sysfs_streq(buf, "roles")) return -EINVAL; - if (!sn->roles_pending) - return -EALREADY; - ret = tipd_refresh_role_switch(tps); - if (ret) - return ret == -EPROBE_DEFER ? -EAGAIN : ret; - sn->roles_pending = false; - scoped_guard(mutex, &tps->lock) - sn201202x_ldcm_refresh(tps); - tipd_complete(dev); - dev_info(dev, "resume from disk: USB role switch looked up again, port revalidates\n"); + scoped_guard(mutex, &tps->lock) { + if (!sn->hib_in_place) + return -EALREADY; + sn->hib_in_place = false; + } + /* A port that still waits looks again, as this write used to make it. */ + queue_work(system_freezable_wq, &sn->role_work); return count; } static DEVICE_ATTR_RW(hib_resume); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 2abaa1a6acf7b..04d9301955ddc 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -512,6 +512,12 @@ struct sn201202x { bool cold_restored; /* Restored from disk: revalidation waits for the new USB role switch. */ bool roles_pending; + /* Restored from disk in place, as hib_resume reports it (tps.lock) */ + bool hib_in_place; + /* Looks the role switch up again when a USB controller binds */ + struct notifier_block role_nb; + struct work_struct role_work; + bool role_watch; /* Restored from disk: the controller is driven polled until a new probe. */ bool cold_polled; }; From 2679a7333960cdcb28a837bb5d63a2cf4c67c962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:52:01 +0100 Subject: [PATCH 229/269] usb: typec: tipd: sn201202x: poll the controller only while restoring it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A controller restored after a resume from disk was driven polled from then on, for as long as it stayed bound: every register selection polled, and neither a system suspend nor a runtime suspend waited for the controller to confirm its sleep any more, so that one which did not go to sleep went unnoticed. The polling dates from when the SPMI controller's interrupt masks were not restored after the cold boot. They are rewritten in its early restore now: after a resume from disk a register selection raises the controller's select interrupt again, and a sleep command its sleep interrupt. What remains is that the cold boot leaves the controller awake, so that a wakeup command raises no wake interrupt: the wake in the restore stays polled, and with it the selections of the set-up that follows. Once the controller is set up, go back to its interrupts. A failed wake was tried a second time with the same call under another name; one attempt, of up to 500 ms, is what a controller that does not answer gets. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/spmi.c | 20 +++++++++----------- drivers/usb/typec/tipd/tps6598x.h | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index ea026021d2d2a..782e551627237 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -561,7 +561,7 @@ static int sn201202x_wake_poll(struct sn201202x *sn, bool poll) static int sn201202x_wake(struct sn201202x *sn) { - return sn201202x_wake_poll(sn, sn->polled || READ_ONCE(sn->cold_polled)); + return sn201202x_wake_poll(sn, sn->polled); } VISIBLE_IF_KUNIT int __maybe_unused sn201202x_runtime_suspend(struct device *dev) @@ -587,7 +587,7 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_runtime_suspend(struct device *dev reinit_completion(&sn->sleep_completion); if (spmi_command_sleep(sn->sdev)) goto retry; - if (!sn->polled && !READ_ONCE(sn->cold_polled) && + if (!sn->polled && !wait_for_completion_timeout(&sn->sleep_completion, msecs_to_jiffies(100))) { /* * It accepted SLEEP, so it may be asleep without having @@ -1001,7 +1001,7 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) err = spmi_command_sleep(sn->sdev); if (err) goto out_resume; - if (!sn->polled && !READ_ONCE(sn->cold_polled) && + if (!sn->polled && !wait_for_completion_timeout(&sn->sleep_completion, msecs_to_jiffies(100))) { err = -ETIMEDOUT; goto out_wake; @@ -1054,17 +1054,13 @@ static int __maybe_unused sn201202x_restore(struct device *dev) /* * The cold boot left the controller awake, so a wakeup command raises - * no wake interrupt, and its register selections do not complete by - * interrupt reliably: talk to it polled, as to a controller without - * interrupts (selections, wake and sleep), until it is probed again. + * no wake interrupt: it is woken polled. Until it is set up again its + * register selections are polled as well, as on a controller without + * interrupts. */ WRITE_ONCE(sn->cold_polled, true); step = "wake"; - err = sn201202x_wake(sn); - if (err == -ETIMEDOUT && !sn->polled) { - step = "awake check"; - err = sn201202x_wake_poll(sn, true); - } + err = sn201202x_wake_poll(sn, true); if (!err) { step = "set-up"; err = tipd_reinit_after_cold_boot(tps); @@ -1082,6 +1078,8 @@ static int __maybe_unused sn201202x_restore(struct device *dev) /* The cold boot reset the controller, which also removed its mitigation. */ scoped_guard(mutex, &tps->lock) sn201202x_ldcm_invalidate(tps); + /* Set up as after probe: from here on by its interrupts again. */ + WRITE_ONCE(sn->cold_polled, false); if (tps->irq) enable_irq(tps->irq); /* Give back the hold freeze took, as resume does */ diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 04d9301955ddc..0195611ab8cde 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -518,7 +518,7 @@ struct sn201202x { struct notifier_block role_nb; struct work_struct role_work; bool role_watch; - /* Restored from disk: the controller is driven polled until a new probe. */ + /* Restored from disk: register selections are polled until it is set up again. */ bool cold_polled; }; From f63f1e93b9fb69a0f93b4e5f28d3dc23decb6a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:52:20 +0100 Subject: [PATCH 230/269] usb: typec: tipd: do not watch for a router whose node is disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A port whose USB4/Thunderbolt router is described but disabled in the device tree registered the watch for a late router, in case the node would be enabled at run time. The device trees of these machines do not change at run time, so on a board that leaves a router disabled every port kept a watch, and looked the router up at every router registration, for one that cannot appear. A disabled router is no router: let the port probe without one, as it did before. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/core.c | 28 ------------------- .../selftests/drivers/gpu/apple-dcp/pm.py | 2 -- 2 files changed, 30 deletions(-) diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 3b061bc0ba862..b8536120fdf09 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -2453,29 +2453,6 @@ VISIBLE_IF_KUNIT bool cd321x_tbt_switch_keep_waiting(struct device *dev) } EXPORT_SYMBOL_IF_KUNIT(cd321x_tbt_switch_keep_waiting); -/* - * The connection lookup skips a router whose node is disabled. A router that is - * described but disabled in the device tree can still be enabled at runtime (by - * an overlay), so the port watches for it as for a late router. - */ -static bool cd321x_tbt_router_described(struct fwnode_handle *fwnode) -{ - struct fwnode_handle *ep, *node; - bool found = false; - - fwnode_graph_for_each_endpoint(fwnode, ep) { - node = fwnode_graph_get_remote_port_parent(ep); - found = fwnode_property_present(node, "thunderbolt-switch"); - fwnode_handle_put(node); - if (found) { - fwnode_handle_put(ep); - break; - } - } - - return found; -} - /* * The USB4/Thunderbolt router is only needed once a USB4 or Thunderbolt link * comes up. If its driver is not built or has not bound yet, do not leave power @@ -2491,11 +2468,6 @@ cd321x_tbt_switch_get(struct device *dev, struct fwnode_handle *fwnode, *late = false; sw = fwnode_typec_thunderbolt_switch_get(fwnode); - if (!sw && cd321x_tbt_router_described(fwnode)) { - dev_dbg(dev, "USB4/Thunderbolt router disabled, watching for it\n"); - *late = true; - return NULL; - } if (sw != ERR_PTR(-EPROBE_DEFER) || cd321x_tbt_switch_keep_waiting(dev)) return sw; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py index 52037e24b808a..6de376bb895a5 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/pm.py @@ -534,8 +534,6 @@ def function(text, name): static void *fwnode_typec_thunderbolt_switch_get(struct fwnode_handle *f) { (void)f;if(switch_error)return (void *)(intptr_t)switch_error;switches++;return &switches; } -/* The lookup above finds a router or fails, so none is taken for a disabled one. */ -static bool cd321x_tbt_router_described(struct fwnode_handle *f) { (void)f;assert(!"the router lookup found none");return false; } static int cd321x_mux_set(struct cd321x *c,bool *held) { assert(c->state.mode==TYPEC_STATE_SAFE); *held=mux_error==-ETIMEDOUT&&mux_transient;return mux_error; From e0670e443d7423435026d20dd33d229fb17df47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:52:39 +0100 Subject: [PATCH 231/269] thunderbolt: apple: owe a restart only to a port that had a cable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A T6040 router marked a restart as owed at every system sleep, also without a cable, so that a cable plugged in during the sleep would be read. The Type-C controller reads every port again after a sleep and applies what it finds, so that case needs no help; but with the restart owed, each of the three routers asked the controller for another read of its port 10 s after every wake. Owe the restart only when the router was running. A cable that the controller reports while the port is still closed, as after a hibernation or a sleep that was abandoned, has nobody else to start it: owe that one a restart as well, at once, as before. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 7585993f92df2..e6c0c52b5f3c4 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -3953,8 +3953,9 @@ static void apple_cio_sleep_restart_work(struct work_struct *work) * one, CIO reconfiguration (which restarts the firmware iBoot left in the * router's SRAM) and RTKit boot, after the Type-C controller has reapplied the * cable. - * Until the sleep is over, cable changes are only recorded, and a restart is - * owed even without a cable so that a cable plugged in during sleep is read. + * Until the sleep is over, cable changes are only recorded. Only a port that + * had a cable owes a restart: one plugged in during the sleep is found by the + * Type-C controller, which reads every port again after a sleep. * Called with acio->lock held. */ static void apple_cio_sleep_stop_locked(struct apple_cio *acio) @@ -3962,10 +3963,10 @@ static void apple_cio_sleep_stop_locked(struct apple_cio *acio) lockdep_assert_held(&acio->lock); acio->sleep_phase = APPLE_CIO_SLEEP_CLOSED; - WRITE_ONCE(acio->sleep_restart_owed, true); if (acio->removing || !acio->current_cable_info) return; + WRITE_ONCE(acio->sleep_restart_owed, true); /* Close PCIe admission; the next NHI probe reopens it. */ scoped_guard(mutex, &acio->pcie_tunnel_lock) acio->pcie_tunnel_stopping = true; @@ -4093,10 +4094,13 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, acio->sleep_phase = APPLE_CIO_SLEEP_RESUMED; acio->sleep_aborted_early = true; } - owed = acio->sleep_restart_owed; + early = acio->sleep_aborted_early; /* A cable recorded while closed has nobody else to start it. */ - early = acio->sleep_aborted_early || - (stop && acio->target_cable_info && !acio->current_cable_info); + if (stop && acio->target_cable_info && !acio->current_cable_info) { + WRITE_ONCE(acio->sleep_restart_owed, true); + early = true; + } + owed = acio->sleep_restart_owed; } /* Start from a worker: probing children inside this chain can deadlock. */ if (owed) From 3893f9154b43d4fde65757a9d0516013d2f2093f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:57:25 +0100 Subject: [PATCH 232/269] usb: typec: tipd: sn201202x: do not wait for a wake interrupt that came MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A controller that is awake answers a WAKEUP command with no wake interrupt. After a firmware-driven suspend three or four of the four controllers of a J616s are awake before the kernel resumes them: the wake interrupt of each is counted once per sleep, yet the driver, which clears the completion before it sends the command, waits its full 100 ms for another one and only then finds the controller awake at the first look. The controllers resume one after the other, so a wake from such a sleep takes 0.3 to 0.4 s longer than it has to. If the wake interrupt has come since the controller was sent to sleep, the controller woke by itself: skip the wait and check it at once, with the check that already follows a wait that timed out. For that the completion is cleared when the controller is sent to sleep, and at a resume whose wakeup watch has woken the controller and sent it to sleep again in between. A controller that sleeps until it is told to wake is waited for as before. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/spmi.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 782e551627237..2c552a65b9c3b 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -510,6 +510,10 @@ static const struct of_device_id sn201202x_of_match[] = { /* * In system sleep the wake interrupt is suspended, so the controller is * polled as without interrupts. + * + * A controller that is awake answers a WAKEUP with no interrupt. One whose + * wake interrupt has come since it was sent to sleep woke by itself: it is + * checked at once, as when polled, instead of waited for. */ static int sn201202x_wake_poll(struct sn201202x *sn, bool poll) { @@ -520,6 +524,8 @@ static int sn201202x_wake_poll(struct sn201202x *sn, bool poll) u8 size; int ret; + if (completion_done(&sn->wake_completion)) + poll = true; reinit_completion(&sn->wake_completion); ret = spmi_command_wakeup(sn->sdev); if (ret) @@ -585,6 +591,8 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_runtime_suspend(struct device *dev return -EBUSY; reinit_completion(&sn->sleep_completion); + /* A wake interrupt from here on is its own, see sn201202x_wake_poll() */ + reinit_completion(&sn->wake_completion); if (spmi_command_sleep(sn->sdev)) goto retry; if (!sn->polled && @@ -928,6 +936,8 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_resume(struct device *dev) /* Waits for a running sleep_event() */ disable_irq(tps->irq); WRITE_ONCE(sn->sleep_watch, false); + /* The watch woke it and sent it to sleep again: not its own wake */ + reinit_completion(&sn->wake_completion); } /* One that slept on from idle sleep wakes through runtime PM */ if (pm_runtime_status_suspended(dev)) { @@ -998,6 +1008,8 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) /* Registered before xHCI, so its root port has suspended by now. */ tipd_usb_wake(tps, true); reinit_completion(&sn->sleep_completion); + /* A wake interrupt from here on is its own, see sn201202x_wake_poll() */ + reinit_completion(&sn->wake_completion); err = spmi_command_sleep(sn->sdev); if (err) goto out_resume; From ca083b9016fe71dba9ca1b81cb09bcd78d2f21eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:36 +0100 Subject: [PATCH 233/269] thunderbolt: apple: describe the T6040 router by what the hardware does MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 comments said what the router needs by pointing at software that drives it: which driver layer handles the nodes, the mode and state numbers used, a virtual power device that is enabled. Say the same in terms of the hardware and its firmware: where the firmware comes from and what restarts the CPU, the settle time of a Thunderbolt 3 link, the order of the PHY and the router. Drop "Not hardware-tested" behind the CL state sentence, which already says that CL states are not qualified, and say when the restart work of a router that stops for sleep runs at once. Comments only. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 47 ++++++++++++++--------------- include/linux/phy/apple-t6040-cio.h | 4 +-- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index e6c0c52b5f3c4..493c74d68a630 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -253,17 +253,16 @@ static const struct apple_cio_hw apple_cio_t6050_hw = { }; /* - * T6040/T6041 (M4 Pro/Max) routers are Type 7 as on T8142: the platform drives their - * acio,v2 nodes with the Type 7 HAL, and the ADT has no per-direction NHI - * fabric tables. iBoot loads the CIO firmware into the router's SRAM; the platform - * never loads it and never writes RUN: starting the IOP enables acio-cpuN's - * clock gate, the PMGR virtual device CIOn-RECONFIG-V, i.e. the CIO - * reconfiguration this driver requests through its reset. After RTKit boot - * The platform waits 400 ms for Thunderbolt 3 (not USB4) links, hands LSTX to the - * router, then polls the firmware state for 1 (assert: bit 31 + code). The platform - * powers the router off for every system sleep, with or without a cable, and - * starts it cold on wake; so does this driver. The host links stay in CL0 - * until CL states are qualified (the platform allows them). Not hardware-tested. + * T6040/T6041 (M4 Pro/Max) routers are Type 7 (acio,v2) as on T8142, without + * per-direction NHI fabric tables. The boot loader leaves the CIO firmware in + * the router's SRAM, and the CIO reconfiguration this driver requests through + * its reset restarts the CPU from it: the firmware is not loaded again and + * RUN is never written. Once RTKit is up, a Thunderbolt 3 (not USB4) link + * gets 400 ms to settle, LSTX is handed to the router, and the firmware state + * is read until it is 1 (bit 31 set: the firmware asserted, with a code). The + * router is powered off for every system sleep, with or without a cable, and + * started cold on wake. The host links stay in CL0 until CL states are + * qualified. */ static const struct apple_cio_hw apple_cio_t6040_hw = { .type7 = true, @@ -280,7 +279,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { .dp = &apple_dpin_disabled, }; -/* The platform sleeps 400 ms on the embedded CPU's enable if CIO generation < 3. */ +/* Settle time of a Thunderbolt 3 or older link once the router's RTKit is up */ #define APPLE_CIO_TBT_SETTLE_MS 400 /* Upper bound for a deferred sleep stop before the suspend is aborted. */ #define APPLE_CIO_SLEEP_STOP_TIMEOUT_MS 60000 @@ -2463,7 +2462,7 @@ static const struct tb_nhi_ops apple_nhi_ops = { /* * hw->cio_phy: the port's USB controller reaches a dock's USB3 through the * router's host USB3 adapter, so the PHY points its PIPE at the router while - * such a tunnel is up (the USB3 mode 3) and back before the tunnel goes. + * such a tunnel is up and back before the tunnel goes. */ static int apple_nhi_usb3_tunnel_changed(struct tb_nhi *nhi, bool on) { @@ -3251,9 +3250,8 @@ static int apple_cio_start(struct apple_cio *acio) return apple_cio_power_up(acio); /* - * The PHY must be in CIO, tuned by its firmware, before ACIO powers up - * (the platform opens the PHY in mode 2 before the embedded CPU's enable). The - * Type-C controller sets the mux first and retries on its next event. + * The PHY must be in CIO, tuned by its firmware, before ACIO powers up. + * The Type-C controller sets the mux first and retries on its next event. */ ret = apple_t6040_cio_state(acio->phy_np); if (ret != 1) { @@ -3396,7 +3394,7 @@ static int apple_cio_power_up(struct apple_cio *acio) goto err_free_rtkit; } - /* The platform gives a Thunderbolt 3 or legacy link 400 ms here, USB4 none. */ + /* A Thunderbolt 3 or legacy link gets its settle time here, USB4 none. */ if (acio->hw->tbt_settle && (acio->target_cable_info & TB_VSE_CAP_APPLE_CABLE_INFO_TBT2_3)) msleep(APPLE_CIO_TBT_SETTLE_MS); @@ -3902,8 +3900,10 @@ static void apple_cio_sleep_close_owed(struct apple_cio *acio) /* * Runs when no post-resume revalidation has restarted the port: always - * after an aborted suspend, and as a fallback after a real one. Ask CD321x - * for a fresh read instead of starting from the state recorded before sleep. + * after an aborted suspend, and as a fallback after a real one; for a + * sleep_stop router also at once when a cable was only recorded while the + * port was closed. Ask CD321x for a fresh read instead of starting from the + * state recorded before sleep. */ static void apple_cio_sleep_restart_work(struct work_struct *work) { @@ -3946,13 +3946,12 @@ static void apple_cio_sleep_restart_work(struct work_struct *work) } /* - * hw->sleep_stop: the router goes down for every system sleep - * (its sleep sets hardware state 1 with a cable, hardware state 0 without), + * hw->sleep_stop: the router goes down for every system sleep, * whether the sleep is s2idle, a firmware-driven suspend or hibernation. * Nothing of the router survives that is used again: the next start is a cold - * one, CIO reconfiguration (which restarts the firmware iBoot left in the - * router's SRAM) and RTKit boot, after the Type-C controller has reapplied the - * cable. + * one, CIO reconfiguration (which restarts the firmware the boot loader left in + * the router's SRAM) and RTKit boot, after the Type-C controller has reapplied + * the cable. * Until the sleep is over, cable changes are only recorded. Only a port that * had a cable owes a restart: one plugged in during the sleep is found by the * Type-C controller, which reads every port again after a sleep. diff --git a/include/linux/phy/apple-t6040-cio.h b/include/linux/phy/apple-t6040-cio.h index 14a8132dbeac9..c632243ae4fa9 100644 --- a/include/linux/phy/apple-t6040-cio.h +++ b/include/linux/phy/apple-t6040-cio.h @@ -39,8 +39,8 @@ void apple_t6040_cio_router_stopped(struct device_node *phy_np); /* * The router carries (@on) or no longer carries USB3 for the port's USB - * controller: the controller's PIPE follows (the USB3 mode 3). -EAGAIN - * when @on and the router is not marked running. + * controller: the controller's PIPE follows. -EAGAIN when @on and the router + * is not marked running. */ int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on); #else From 999240676f9744e0bcbd8420f0638138d0e8a676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:36 +0100 Subject: [PATCH 234/269] thunderbolt: apple: demote two expected T6040 messages to dev_dbg() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every system sleep logged "powering off router for system sleep" for each port with a cable, and a start request that comes before the Type-C PHY is in CIO mode was logged as an error although the Type-C controller sets the mux first and simply asks again. Both are the normal course: dev_dbg(). Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 493c74d68a630..ff203eb908b86 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -3256,7 +3256,7 @@ static int apple_cio_start(struct apple_cio *acio) ret = apple_t6040_cio_state(acio->phy_np); if (ret != 1) { ret = ret < 0 ? ret : -EAGAIN; - dev_err(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); + dev_dbg(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); return ret; } ret = apple_t6040_cio_router_started(acio->phy_np); @@ -3969,8 +3969,8 @@ static void apple_cio_sleep_stop_locked(struct apple_cio *acio) /* Close PCIe admission; the next NHI probe reopens it. */ scoped_guard(mutex, &acio->pcie_tunnel_lock) acio->pcie_tunnel_stopping = true; - dev_info(acio->dev, "powering off router for system sleep (cable info 0x%x)\n", - acio->current_cable_info); + dev_dbg(acio->dev, "powering off router for system sleep (cable info 0x%x)\n", + acio->current_cable_info); apple_cio_stop(acio); } From 4b1d9c0e3a3bcff97ec1b16ad685ed0b59e35871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:37 +0100 Subject: [PATCH 235/269] thunderbolt: apple: leave type7 where it was in apple_cio_hw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The T6040 support moved the type7 member to the top of struct apple_cio_hw, reworded its comment and reordered the T6040 initializer, none of which it needed. Put all three back. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index ff203eb908b86..c6ee691214cc7 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -143,11 +143,6 @@ struct apple_cio_hw { /* DP IN adapter blocks of the router; 0 means the two of t8103 */ unsigned int dpin_count; - /* - * Type 7 (acio,v2) routers: firmware ready at state BIT(24), PDF - * masks in the receive ring registers, loader tuning required. - */ - bool type7; bool mxwrap; bool lstx_explicit; /* Leave MxWrap RUN alone: the CIO reconfig restarts the CPU. */ @@ -164,6 +159,8 @@ struct apple_cio_hw { bool no_host_clx; /* Keep the domain's links symmetric. */ bool no_asym; + /* Type 7 router: Enter_USB cable words, bootloader RC tuning, Type 7 NHI. */ + bool type7; /* * Release the CPU with a plain store of the RUN value rather than a * read-modify-write, and halt it again when the router stops. @@ -265,8 +262,8 @@ static const struct apple_cio_hw apple_cio_t6050_hw = { * qualified. */ static const struct apple_cio_hw apple_cio_t6040_hw = { - .type7 = true, .mxwrap = true, + .type7 = true, .lstx_explicit = true, .skip_run = true, .lstx_after_boot = true, From 266469db05276fd579413b6e23a2963f95de7e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:37 +0100 Subject: [PATCH 236/269] thunderbolt: apple: define apple_cio_start() below what it wraps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apple_cio_start() became a wrapper around the former function body, apple_cio_power_up(), but was left above it, which took a forward declaration and put the kernel-doc of one function on top of the code of the other. Move the wrapper and its kernel-doc below apple_cio_power_up(). No change to the code. Signed-off-by: František Balázsy --- drivers/thunderbolt/apple.c | 84 +++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index c6ee691214cc7..3f75b0e16e93c 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -3225,48 +3225,10 @@ static bool apple_cio_recovering(struct apple_cio *acio) return acio->resume_recovery_active; } -static int apple_cio_power_up(struct apple_cio *acio); - -/** - * apple_cio_start - Power up and start the ACIO complex - * @acio: ACIO complex to start - * - * The Type-C PHY must already be configured for USB4 or Thunderbolt. Power up - * ACIO, boot its RTKit co-processor, populate its child devices and wait for - * the NHI to register the USB4 domain. All completed steps are unwound on - * failure. The caller must hold @acio->lock. - * - * Return: 0 on success or a negative error code on failure. +/* + * Power ACIO up and start it: all of apple_cio_start() but the handshake with + * the Type-C PHY of a hw->cio_phy router. */ -static int apple_cio_start(struct apple_cio *acio) -{ - int ret; - - lockdep_assert_held(&acio->lock); - if (!acio->hw->cio_phy) - return apple_cio_power_up(acio); - - /* - * The PHY must be in CIO, tuned by its firmware, before ACIO powers up. - * The Type-C controller sets the mux first and retries on its next event. - */ - ret = apple_t6040_cio_state(acio->phy_np); - if (ret != 1) { - ret = ret < 0 ? ret : -EAGAIN; - dev_dbg(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); - return ret; - } - ret = apple_t6040_cio_router_started(acio->phy_np); - if (ret) { - dev_err(acio->dev, "Type-C PHY refused the router: %d\n", ret); - return ret; - } - ret = apple_cio_power_up(acio); - if (ret) - apple_t6040_cio_router_stopped(acio->phy_np); - return ret; -} - static int apple_cio_power_up(struct apple_cio *acio) { struct device_link *link; @@ -3489,6 +3451,46 @@ static int apple_cio_power_up(struct apple_cio *acio) return ret; } +/** + * apple_cio_start - Power up and start the ACIO complex + * @acio: ACIO complex to start + * + * The Type-C PHY must already be configured for USB4 or Thunderbolt. Power up + * ACIO, boot its RTKit co-processor, populate its child devices and wait for + * the NHI to register the USB4 domain. All completed steps are unwound on + * failure. The caller must hold @acio->lock. + * + * Return: 0 on success or a negative error code on failure. + */ +static int apple_cio_start(struct apple_cio *acio) +{ + int ret; + + lockdep_assert_held(&acio->lock); + if (!acio->hw->cio_phy) + return apple_cio_power_up(acio); + + /* + * The PHY must be in CIO, tuned by its firmware, before ACIO powers up. + * The Type-C controller sets the mux first and retries on its next event. + */ + ret = apple_t6040_cio_state(acio->phy_np); + if (ret != 1) { + ret = ret < 0 ? ret : -EAGAIN; + dev_dbg(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); + return ret; + } + ret = apple_t6040_cio_router_started(acio->phy_np); + if (ret) { + dev_err(acio->dev, "Type-C PHY refused the router: %d\n", ret); + return ret; + } + ret = apple_cio_power_up(acio); + if (ret) + apple_t6040_cio_router_stopped(acio->phy_np); + return ret; +} + static int apple_cio_check_connection(struct apple_cio *acio) { struct platform_device *pdev; From aa6a920cd4eae9058ddf1d1628b6c679b3592ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:38 +0100 Subject: [PATCH 237/269] usb: typec: tipd: sn201202x: say what the restore from disk does now MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment above sn201202x_restore() still described leaving the controller alone for a new probe by user space, which is only what happens when the restore in place fails. Describe the restore in place. Its two messages, four controllers each at every resume from disk, are the normal course: dev_dbg(). Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/spmi.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 2c552a65b9c3b..be7e9b37ae1b9 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -751,7 +751,7 @@ static int sn201202x_roles_finish(struct sn201202x *sn) sn201202x_ldcm_refresh(tps); } tipd_complete(tps->dev); - dev_info(tps->dev, "resume from disk: USB role switch looked up again, port revalidates\n"); + dev_dbg(tps->dev, "resume from disk: USB role switch looked up again, port revalidates\n"); return 0; } @@ -1043,9 +1043,10 @@ EXPORT_SYMBOL_IF_KUNIT(sn201202x_suspend); * A resume from hibernation is a cold boot: the boot loader and the kernel * that loaded the image have taken the controller through their own bring-up, * so the state saved in the image (its sleep, the latched events) no longer - * exists and waking it the suspend way times out. Leave it alone with its - * interrupt still masked; the controller is probed again from scratch after - * the restore (userspace unbinds and binds it, e.g. from a sleep hook). + * exists. Wake the controller and set it up again in place, as its probe + * would; its port is revalidated once the connector's USB role switch is + * there, see sn201202x_roles_finish(). A controller that does not take this + * is left alone, with its event interrupt still disabled, for a new probe. */ static int __maybe_unused sn201202x_restore(struct device *dev) { @@ -1100,7 +1101,7 @@ static int __maybe_unused sn201202x_restore(struct device *dev) WRITE_ONCE(sn->roles_pending, true); sn->hib_in_place = true; } - dev_info(dev, "resume from disk: controller restored, port revalidation waits for the USB role switch\n"); + dev_dbg(dev, "resume from disk: controller restored, port revalidation waits for the USB role switch\n"); return 0; } From 551053405fde657624db30e4915d9a7bee299a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:02:38 +0100 Subject: [PATCH 238/269] usb: typec: tipd: sn201202x: use pm_sleep_ptr() for the sleep callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The system sleep callbacks sat in an #ifdef CONFIG_PM_SLEEP block two lines below the pm_sleep_ptr() users. Use pm_sleep_ptr() for them as well; sn201202x_restore() is then always referenced and needs no __maybe_unused. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/spmi.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index be7e9b37ae1b9..c252cea7789f0 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -1048,7 +1048,7 @@ EXPORT_SYMBOL_IF_KUNIT(sn201202x_suspend); * there, see sn201202x_roles_finish(). A controller that does not take this * is left alone, with its event interrupt still disabled, for a new probe. */ -static int __maybe_unused sn201202x_restore(struct device *dev) +static int sn201202x_restore(struct device *dev) { struct tps6598x *tps = dev_get_drvdata(dev); struct sn201202x *sn = tps_to_sn(tps); @@ -1211,14 +1211,12 @@ MODULE_DEVICE_TABLE(of, sn201202x_of_match); static const struct dev_pm_ops sn201202x_pm_ops = { .prepare = pm_sleep_ptr(sn201202x_prepare), .complete = pm_sleep_ptr(sn201202x_complete), -#ifdef CONFIG_PM_SLEEP - .suspend = sn201202x_suspend, - .resume = sn201202x_resume, - .freeze = sn201202x_suspend, - .thaw = sn201202x_resume, - .poweroff = sn201202x_suspend, - .restore = sn201202x_restore, -#endif + .suspend = pm_sleep_ptr(sn201202x_suspend), + .resume = pm_sleep_ptr(sn201202x_resume), + .freeze = pm_sleep_ptr(sn201202x_suspend), + .thaw = pm_sleep_ptr(sn201202x_resume), + .poweroff = pm_sleep_ptr(sn201202x_suspend), + .restore = pm_sleep_ptr(sn201202x_restore), SET_RUNTIME_PM_OPS(sn201202x_runtime_suspend, sn201202x_runtime_resume, NULL) }; From de846d2548a0e9d690621cdef0d8714bb6eab5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:22:21 +0100 Subject: [PATCH 239/269] phy: apple: t6040: a powered-down sleep is the coprocessor's power cycle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the PHY's coprocessor does not stop cleanly as USB3 goes down (it does not acknowledge its idle state, or the Power Down message), the driver notes that the coprocessor's two power domains need a power cycle before its next start. A port that went down for a sleep with its host kept carried that note through the sleep, and its wake then left USB3 out, because a system resume cannot take the domains off. The PHY reported that it had not come back, and the host was restarted with every device on it. On a J616s with a dock, once in 25 sleeps: RTKit: AP idle not acknowledged: -62 (suspend) PHY not back after the sleep (-5, USB2 on) (wake) not resuming xHCI, the core didn't come back: -5 The same note also outlived the sleep of a port whose host was stopped for it, and a hibernation. A sleep that powers the SoC down takes the coprocessor and both domains off for as long as it lasts, with the RTKit transport released by the stop before it: that is the power cycle the note asks for. Drop the note where the system comes back from such a sleep, before a wake or a start of a host looks at it: in the early resume when the firmware woke the system, and at a restore from disk, which is a cold boot. A sleep that was refused, or a dry run, leaves the SoC powered and the note in place. The wake's choice to start USB3 moves into a helper, which the KUnit suite now covers together with the new rule. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 32 ++++++++++++++ drivers/phy/apple/t6040-usb2.c | 55 ++++++++++++++++++------- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 644b672cf39db..75aba274234fd 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -38,10 +38,23 @@ static void t6040_recovery_retry_exhaustion_test(struct kunit *test) KUNIT_EXPECT_EQ(test, state.retries, (u8)T6040_RECOVERY_RETRIES); } +/* A sleep that powers the SoC down is the power cycle an unclean stop asks for. */ +static void t6040_recovery_power_lost_test(struct kunit *test) +{ + struct t6040_recovery_state state = {}; + + t6040_recovery_power_down(&state, true, false); + KUNIT_EXPECT_TRUE(test, state.cycle_pending); + t6040_recovery_power_lost(&state); + KUNIT_EXPECT_FALSE(test, state.cycle_pending); + KUNIT_EXPECT_FALSE(test, t6040_recovery_start(&state)); +} + static struct kunit_case t6040_recovery_test_cases[] = { KUNIT_CASE(t6040_recovery_missed_ack_test), KUNIT_CASE(t6040_recovery_failed_tune_test), KUNIT_CASE(t6040_recovery_retry_exhaustion_test), + KUNIT_CASE(t6040_recovery_power_lost_test), {} }; @@ -155,11 +168,30 @@ static void t6040_sleep_failed_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); } +/* + * The wake starts USB3 if it ran before the sleep. Not while the coprocessor + * waits for a power cycle after a stop it didn't acknowledge, until a sleep + * that powered the SoC down has been that power cycle. + */ +static void t6040_sleep_wake_usb3_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + KUNIT_EXPECT_FALSE(test, t6040_sleep_wake_usb3(tphy)); + tphy->sleep_usb3 = true; + KUNIT_EXPECT_TRUE(test, t6040_sleep_wake_usb3(tphy)); + t6040_recovery_power_down(&tphy->recovery, true, false); + KUNIT_EXPECT_FALSE(test, t6040_sleep_wake_usb3(tphy)); + t6040_recovery_power_lost(&tphy->recovery); + KUNIT_EXPECT_TRUE(test, t6040_sleep_wake_usb3(tphy)); +} + static struct kunit_case t6040_sleep_test_cases[] = { KUNIT_CASE(t6040_sleep_usb_test), KUNIT_CASE(t6040_sleep_no_host_test), KUNIT_CASE(t6040_sleep_other_modes_test), KUNIT_CASE(t6040_sleep_failed_test), + KUNIT_CASE(t6040_sleep_wake_usb3_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 3eadff2928134..fa32e45531f19 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -218,6 +218,15 @@ static void t6040_recovery_complete(struct t6040_recovery_state *state) state->retries = 0; } +/* + * The SoC lost power in a sleep, and the coprocessor and both of its power + * domains with it: that is the power cycle a stop that wasn't clean asks for. + */ +static void __maybe_unused t6040_recovery_power_lost(struct t6040_recovery_state *state) +{ + state->cycle_pending = false; +} + enum t6040_usb3_region { T6040_USB3_AXI2AF, T6040_USB3_AXI2AF_LIOA, @@ -3340,6 +3349,17 @@ int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy) } EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_result); +/* + * Whether the wake of a sleep with the host kept starts USB3: it ran when the + * port went down, and the coprocessor doesn't wait for a power cycle of its + * domains, which a system resume can't give it. A sleep that powered the SoC + * down was that power cycle, see t6040_usb2_resume_early(). + */ +static bool t6040_sleep_wake_usb3(const struct t6040_usb2_phy *tphy) +{ + return tphy->sleep_usb3 && !tphy->recovery.cycle_pending; +} + int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) { struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); @@ -3367,17 +3387,14 @@ int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) if (ret) goto failed; tphy->sleep_down = false; - if (tphy->sleep_usb3) { - /* A coprocessor that didn't stop cleanly needs its domains' power cycle first. */ - if (!tphy->recovery.cycle_pending) { - tphy->sleep_waking = true; - t6040_usb3_up_locked(tphy); - tphy->sleep_waking = false; - } - if (!tphy->usb3_up) { - ret = -EIO; - goto failed; - } + if (t6040_sleep_wake_usb3(tphy)) { + tphy->sleep_waking = true; + t6040_usb3_up_locked(tphy); + tphy->sleep_waking = false; + } + if (tphy->sleep_usb3 && !tphy->usb3_up) { + ret = -EIO; + goto failed; } dev_info(tphy->dev, "PHY back after the sleep with the host kept, USB3 %s\n", tphy->usb3_up ? "up" : "off"); @@ -3453,9 +3470,11 @@ static int t6040_phy_count(struct phy *phy) * is kept, so that every count stays balanced; a phy still held is only * reported. The port's Type-C state (orientation, DP alt mode, pin * assignment) describes the partner, not the PHY, and stays for the next - * power-on. Early, before any .restore: the Type-C controller's may set - * the mux again. Nothing touches the hardware here. CIO is torn down - * before the image (.freeze), so the image never holds a CIO session. + * power-on. A power cycle that the coprocessor was waiting for when the + * image was made has happened by now. Early, before any .restore: the + * Type-C controller's may set the mux again. Nothing touches the hardware + * here. CIO is torn down before the image (.freeze), so the image never + * holds a CIO session. */ static int t6040_usb2_restore_early(struct device *dev) { @@ -3495,6 +3514,7 @@ static int t6040_usb2_restore_early(struct device *dev) tphy->dp_mode_flip = false; tphy->dp_rate = -1; tphy->shutdown_failed = false; + t6040_recovery_power_lost(&tphy->recovery); return 0; } @@ -3580,6 +3600,11 @@ static int t6040_usb2_poweroff(struct device *dev) * host's resume. From here until then DP waits for it in its power-on, see * t6040_dp_wait_host(). Early, before any .resume: the Type-C controller * reports a display from its own. + * + * If the firmware woke the system, the SoC was powered down, and the + * coprocessor and its domains with it: no start after this sleep has a power + * cycle of theirs to wait for or to run, the wake of a host kept through it + * included. */ static int t6040_usb2_resume_early(struct device *dev) { @@ -3588,6 +3613,8 @@ static int t6040_usb2_resume_early(struct device *dev) if (!tphy) return 0; guard(mutex)(&tphy->lock); + if (pm_resume_via_firmware()) + t6040_recovery_power_lost(&tphy->recovery); WRITE_ONCE(tphy->sleep_back, tphy->sleep_down); return 0; } From 14025bd27babda415d2289bf37d5ba3437e593b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:22:21 +0100 Subject: [PATCH 240/269] phy: apple: t6040: keep the host at USB2 when USB3 isn't back at a wake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the wake of a sleep with the host kept the PHY brings USB2 back, and USB3 if it ran before the sleep. When USB3 did not come up, the wake returned an error although USB2 was on and the controller out of reset: xHCI was not resumed, and the host was stopped and started again once the system had resumed, so every device on the port left the bus, the ones at USB2 included. At a start of the host the same PHY takes this differently: USB3 that doesn't come up is logged and the port runs at USB2, because the USB2 PHY and the core must still come up. Do the same at the wake. The host and what runs at USB2 on it are kept, the controller's PIPE stays on the dummy PHY, and only the SuperSpeed devices leave the bus. USB2 that doesn't come back still costs the host. A wake cannot do what a start does when the coprocessor fails to tune the PHY, a power cycle of its domains and one more start: the domains don't go off under a system resume. That is left to the port's next start. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 37 +++++++++++++++++++--------------- include/linux/phy/apple-atc.h | 6 ++++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index fa32e45531f19..d520249ea66b7 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -1216,7 +1216,7 @@ static void t6040_usb3_up_locked(struct t6040_usb2_phy *tphy) t6040_usb3_sleep(tphy); /* * The retry power-cycles the PHY's domains, which don't go off under a system - * resume: a wake with the host kept leaves it to the host's restart after it. + * resume: a wake with the host kept leaves the port at USB2 instead. */ if (t6040_recovery_retry(&tphy->recovery) && !tphy->sleep_waking) { cycle = true; @@ -3363,6 +3363,7 @@ static bool t6040_sleep_wake_usb3(const struct t6040_usb2_phy *tphy) int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) { struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + bool tried; int ret; if (!tphy) @@ -3384,27 +3385,31 @@ int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) /* As dwc3-apple's start, in the core's init: USB2, then USB3 if it ran. */ ret = tphy->shutdown_failed ? -EIO : t6040_usb2_on_locked(tphy); - if (ret) - goto failed; + if (ret) { + dev_err(tphy->dev, "USB2 not back after the sleep (%d): the host has to restart\n", + ret); + tphy->sleep_ret = ret; + return ret; + } tphy->sleep_down = false; - if (t6040_sleep_wake_usb3(tphy)) { + tried = t6040_sleep_wake_usb3(tphy); + if (tried) { tphy->sleep_waking = true; t6040_usb3_up_locked(tphy); tphy->sleep_waking = false; } - if (tphy->sleep_usb3 && !tphy->usb3_up) { - ret = -EIO; - goto failed; - } - dev_info(tphy->dev, "PHY back after the sleep with the host kept, USB3 %s\n", - tphy->usb3_up ? "up" : "off"); + /* + * As at a start, a port whose USB3 doesn't come up stays at USB2: the host + * is kept with what runs at USB2 on it, and only the SuperSpeed devices + * leave the bus. + */ + if (tphy->sleep_usb3 && !tphy->usb3_up) + dev_warn(tphy->dev, "USB3 not back after the sleep (%s): the host stays, at USB2\n", + tried ? "its bring-up failed" : "its coprocessor waits for a power cycle"); + else + dev_info(tphy->dev, "PHY back after the sleep with the host kept, USB3 %s\n", + tphy->usb3_up ? "up" : "off"); return 0; - -failed: - dev_err(tphy->dev, "PHY not back after the sleep (%d, USB2 %s): the host has to restart\n", - ret, tphy->sleep_down ? "off" : "on"); - tphy->sleep_ret = ret; - return ret; } EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_power_up); diff --git a/include/linux/phy/apple-atc.h b/include/linux/phy/apple-atc.h index 27960bbe3f0f3..bcf103b9af81f 100644 --- a/include/linux/phy/apple-atc.h +++ b/include/linux/phy/apple-atc.h @@ -137,14 +137,16 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy); * its core. 0: the PHY was never taken down (the sleep was abandoned first), and all is as * it was. APPLE_ATCPHY_SLEEP_RESET: the controller is out of reset again, which the first * call after the sleep sees to. -ENODEV: the reset didn't release, so the core must not - * be touched. Any other error: the controller is out of reset, but the PHY didn't come back. + * be touched. Any other error: the controller is out of reset, but USB2 didn't come back. */ int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy); /* * After APPLE_ATCPHY_SLEEP_RESET, once the core is set up again and before its PIPE is * routed: USB2 on, and USB3 up if it ran before the sleep. May sleep for the coprocessor's - * boot. An error leaves the controller out of reset for a restart of the host. + * boot. USB3 that doesn't come up leaves the port at USB2, as at a start of the host, and + * is not an error. An error is USB2's, and leaves the controller out of reset for a restart + * of the host. */ int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy); From cb7c3539d4763b3498655dd33f0f079cf25771a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:22:21 +0100 Subject: [PATCH 241/269] phy: apple: t6040: try USB3 again at the next wake of a host left at USB2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A wake that leaves a kept host at USB2 was final for its USB3: the port went down for the next sleep with USB3 off, so that sleep's wake did not bring it up either, however many sleeps followed. Only a new start of the host, at the next attach, tried USB3 again. Remember that USB3 ran at a sleep of this host until the host stops, and let every wake of it bring USB3 up. After a sleep that powered the SoC down the coprocessor starts from a power cycle, the best chance it gets: the port is back at SuperSpeed one sleep after it lost it. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index d520249ea66b7..f6cf650c8818a 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -333,7 +333,7 @@ struct t6040_usb2_phy { bool sleep_armed; /* taken on by the prepare, until the finish */ bool sleep_down; /* USB2 off and USB3 asleep for it, behind their consumers */ bool sleep_reset; /* the controller held in reset for it, behind its consumer */ - bool sleep_usb3; /* USB3 ran when it went down, and comes back with USB2 */ + bool sleep_usb3; /* USB3 ran at a sleep of this host, and comes back with USB2 */ bool sleep_waking; /* in that wake: no power cycle of the PHY's domains */ bool sleep_back; /* the system resumes and that wake is still to run */ wait_queue_head_t sleep_wq; /* DP's power-on waits here for sleep_back to end */ @@ -1279,6 +1279,8 @@ static int t6040_usb3_power_off(struct phy *phy) guard(mutex)(&tphy->lock); tphy->usb3_on = false; tphy->usb3_pipe = false; + /* The host stops: what its next start brings up is what a later wake brings back. */ + tphy->sleep_usb3 = false; t6040_usb3_down_locked(tphy); return 0; } @@ -3270,6 +3272,7 @@ EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_prepare); int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) { struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); + bool usb3; int ret; if (!tphy) @@ -3295,8 +3298,14 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) return -EBUSY; } + /* + * USB3 that an earlier wake of this host left down counts as well: this + * sleep's wake tries it again, with the SoC's power cycle behind it if + * the sleep is a real one. + */ + usb3 = tphy->usb3_up; + tphy->sleep_usb3 |= usb3; /* As dwc3-apple's stop: USB3 with its PIPE, USB2, then the controller's reset. */ - tphy->sleep_usb3 = tphy->usb3_up; t6040_usb3_down_locked(tphy); ret = t6040_usb2_off_locked(tphy); if (ret) @@ -3306,7 +3315,7 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) tphy->sleep_reset = true; WRITE_ONCE(tphy->sleep_back, false); dev_info(tphy->dev, "PHY down for the sleep with the host kept, USB3 %s\n", - tphy->sleep_usb3 ? "was up" : "was off"); + usb3 ? "was up" : "was off"); return 0; } EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_enter); From 35a28442cbb946565fcf9b3a92432c3568f6df33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:22:22 +0100 Subject: [PATCH 242/269] phy: apple: t6040: keep the host through sleeps again after a clean start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A wake that fails with the host kept in place makes the PHY refuse to keep the host for every later sleep of the port. Any failure of the wake did that, a coprocessor that did not tune the PHY once included, and only a write to a debugfs file or binding the driver again took it back: one bad wake, and the port's devices left the bus at every sleep until the next boot. USB3 no longer fails a wake. What is left are the two steps the keeping itself consists of, the controller's reset that does not release and USB2 that does not come back, and for those the host still stops for the next sleep. A start of the host runs the same two steps: once one has worked, let the port keep its host again. The debugfs file was the manual way to do that, and goes. The condition moves into a helper, which the KUnit suite covers. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 17 ++++++++++++++ drivers/phy/apple/t6040-usb2.c | 30 ++++++++++++++++++++----- include/linux/phy/apple-atc.h | 5 ++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 75aba274234fd..287f096784f10 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -168,6 +168,22 @@ static void t6040_sleep_failed_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); } +/* + * Only the controller's reset and USB2 can fail the keeping of a host, and + * only when the host was lost: not a sleep refused for a USB change, not what + * a wake that released the reset reports, and no host that was kept. + */ +static void t6040_sleep_keep_failed_test(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, t6040_sleep_keep_failed(false, -ENODEV)); + KUNIT_EXPECT_TRUE(test, t6040_sleep_keep_failed(false, -EIO)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_keep_failed(false, -EBUSY)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_keep_failed(false, 0)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_keep_failed(false, APPLE_ATCPHY_SLEEP_RESET)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_keep_failed(true, -ENODEV)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_keep_failed(true, APPLE_ATCPHY_SLEEP_RESET)); +} + /* * The wake starts USB3 if it ran before the sleep. Not while the coprocessor * waits for a power cycle after a stop it didn't acknowledge, until a sleep @@ -191,6 +207,7 @@ static struct kunit_case t6040_sleep_test_cases[] = { KUNIT_CASE(t6040_sleep_no_host_test), KUNIT_CASE(t6040_sleep_other_modes_test), KUNIT_CASE(t6040_sleep_failed_test), + KUNIT_CASE(t6040_sleep_keep_failed_test), KUNIT_CASE(t6040_sleep_wake_usb3_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index f6cf650c8818a..ee6f7203086b3 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -337,7 +337,7 @@ struct t6040_usb2_phy { bool sleep_waking; /* in that wake: no power cycle of the PHY's domains */ bool sleep_back; /* the system resumes and that wake is still to run */ wait_queue_head_t sleep_wq; /* DP's power-on waits here for sleep_back to end */ - bool sleep_no_keep; /* a wake of its own failed: the host stops for later sleeps */ + bool sleep_no_keep; /* keeping the host failed: it stops for the next sleep */ int sleep_ret; /* what the wake has come to, for the controller's glue */ }; @@ -501,6 +501,11 @@ static int t6040_usb2_power_on(struct phy *phy) if (ret) return ret; tphy->powered = true; + /* + * The controller's reset released and USB2 came up, which is all that + * keeping a host through a sleep needs to work again at its wake. + */ + tphy->sleep_no_keep = false; return 0; } @@ -2844,8 +2849,6 @@ static int t6040_debugfs_init(struct t6040_usb2_phy *tphy) } debugfs_create_atomic_t("drop_next_power_down_ack", 0600, tphy->debugfs, &tphy->drop_next_power_down_ack); - /* Set by a wake that failed with the host kept; clear it to let the port try again. */ - debugfs_create_bool("sleep_no_keep", 0600, tphy->debugfs, &tphy->sleep_no_keep); ret = devm_add_action_or_reset(tphy->dev, t6040_debugfs_remove, tphy->debugfs); if (ret) tphy->debugfs = NULL; @@ -3441,6 +3444,18 @@ int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy) } EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_recover); +/* + * Whether a sleep ended with keeping the host having failed: the host was + * lost, and what the wake came to is an error of the PHY's own, which is the + * controller's reset that didn't release or USB2 that didn't come back. USB3 + * never costs the host. A sleep refused for a USB change doesn't count, nor + * a host that was restarted for another reason. + */ +static bool t6040_sleep_keep_failed(bool kept, int sleep_ret) +{ + return !kept && sleep_ret < 0 && sleep_ret != -EBUSY; +} + void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) { struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); @@ -3452,11 +3467,14 @@ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) t6040_sleep_back_done(tphy); if (!tphy->sleep_armed) return; - /* A sleep refused for a USB change doesn't count, nor a restart for another reason. */ - if (!kept && tphy->sleep_ret < 0 && tphy->sleep_ret != -EBUSY) { + /* + * The host has been restarted by now. It stops for the next sleep, and + * the start after that one lets it be kept again. + */ + if (t6040_sleep_keep_failed(kept, tphy->sleep_ret)) { tphy->sleep_no_keep = true; dev_warn(tphy->dev, - "PHY did not come back from a sleep with the host kept (%d): the host stops for sleeps from now on (debugfs sleep_no_keep)\n", + "PHY did not come back from a sleep with the host kept (%d): the host stops for the next sleep\n", tphy->sleep_ret); } tphy->sleep_armed = false; diff --git a/include/linux/phy/apple-atc.h b/include/linux/phy/apple-atc.h index bcf103b9af81f..d16b6d6266e66 100644 --- a/include/linux/phy/apple-atc.h +++ b/include/linux/phy/apple-atc.h @@ -153,7 +153,10 @@ int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy); /* For -ENODEV: tries the reset's release again. APPLE_ATCPHY_SLEEP_RESET, or -ENODEV. */ int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy); -/* Ends what the prepare took on. Without @kept, a failure of the PHY's own stops later ones. */ +/* + * Ends what the prepare took on. Without @kept, a failure of the PHY's own makes the host + * stop for sleeps until it has started again. + */ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept); #else static inline int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) From 38f4ce5e5d49846cff82db24d793ccd9dc74f82b Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:16:43 +1100 Subject: [PATCH 243/269] phy: apple: t6040: tolerate a busy recovery domain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After an injected Power Down acknowledgement drop on the J616s, phymxwrap returned -EAGAIN from runtime suspend. The port then stayed USB2-only on every reattach until reboot, while a control reattach without injection restored USB3 normally. Release the RTKit transport before cycling the domains, report the mailbox provider usage and child counts, and retry a busy provider with bounded backoff. If another holder remains, restore common_dp and perform a clean normal firmware start instead of failing the attach. Keep requiring an observed phymxwrap suspend before claiming a completed hard recovery. Cover held usage and child references in the recovery KUnit suite. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> [František: kept both sides of the context conflict with T6040_SLEEP_DP_WAIT_MS] Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 20 ++++++ drivers/phy/apple/t6040-usb2.c | 89 ++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 8 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 287f096784f10..51ba747f9eab9 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -50,11 +50,31 @@ static void t6040_recovery_power_lost_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, t6040_recovery_start(&state)); } +static void t6040_recovery_held_reference_test(struct kunit *test) +{ + unsigned int attempt; + + for (attempt = 0; attempt < T6040_RECOVERY_PM_RETRIES; attempt++) + KUNIT_EXPECT_EQ(test, + t6040_recovery_pm_action(1, 0, attempt), + T6040_RECOVERY_PM_RETRY); + KUNIT_EXPECT_EQ(test, + t6040_recovery_pm_action(1, 0, attempt), + T6040_RECOVERY_PM_FALLBACK); + KUNIT_EXPECT_EQ(test, + t6040_recovery_pm_action(0, 1, attempt), + T6040_RECOVERY_PM_FALLBACK); + KUNIT_EXPECT_EQ(test, + t6040_recovery_pm_action(0, 0, attempt), + T6040_RECOVERY_PM_SUSPEND); +} + static struct kunit_case t6040_recovery_test_cases[] = { KUNIT_CASE(t6040_recovery_missed_ack_test), KUNIT_CASE(t6040_recovery_failed_tune_test), KUNIT_CASE(t6040_recovery_retry_exhaustion_test), KUNIT_CASE(t6040_recovery_power_lost_test), + KUNIT_CASE(t6040_recovery_held_reference_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index ee6f7203086b3..8ab5fb54b08c0 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -177,6 +177,8 @@ #define T6040_CPU_MSG_POWER_DOWN 0x0100000000000000ULL #define T6040_RECOVERY_RETRIES 1 +#define T6040_RECOVERY_PM_RETRIES 5 +#define T6040_RECOVERY_PM_BACKOFF_US 2000 /* DP's power-on waits this long for the wake of a host kept through a sleep */ #define T6040_SLEEP_DP_WAIT_MS 2000 @@ -227,6 +229,23 @@ static void __maybe_unused t6040_recovery_power_lost(struct t6040_recovery_state state->cycle_pending = false; } +enum t6040_recovery_pm_action { + T6040_RECOVERY_PM_SUSPEND, + T6040_RECOVERY_PM_RETRY, + T6040_RECOVERY_PM_FALLBACK, +}; + +static enum t6040_recovery_pm_action +t6040_recovery_pm_action(int usage_count, int child_count, + unsigned int attempt) +{ + if (!usage_count && !child_count) + return T6040_RECOVERY_PM_SUSPEND; + if (attempt < T6040_RECOVERY_PM_RETRIES) + return T6040_RECOVERY_PM_RETRY; + return T6040_RECOVERY_PM_FALLBACK; +} + enum t6040_usb3_region { T6040_USB3_AXI2AF, T6040_USB3_AXI2AF_LIOA, @@ -903,6 +922,62 @@ static int t6040_common_dp_force_resume(struct t6040_usb2_phy *tphy) return 0; } +/* + * The RTKit transport owns one phymxwrap runtime reference while its mailbox + * is started. apple_rtkit_free() releases it before this function runs. Give + * an unrelated user or active child a bounded interval to become idle, but do + * not steal its reference: recovery can still make a clean firmware start + * without cycling a domain which is legitimately shared. + * + * Return 0 after a real suspend, 1 when the caller should use that normal-start + * fallback, and a negative error for a provider failure unrelated to a holder. + */ +static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy) +{ + struct device *dev = tphy->mbox_dev; + unsigned int attempt; + int children, ret = -EBUSY, usage; + + for (attempt = 0; attempt <= T6040_RECOVERY_PM_RETRIES; attempt++) { + enum t6040_recovery_pm_action action; + + pm_runtime_barrier(dev); + usage = atomic_read(&dev->power.usage_count); + children = atomic_read(&dev->power.child_count); + dev_dbg(tphy->dev, + "USB3: phymxwrap recovery attempt %u: usage_count=%d child_count=%d\n", + attempt, usage, children); + + action = t6040_recovery_pm_action(usage, children, attempt); + if (action == T6040_RECOVERY_PM_SUSPEND) { + ret = pm_runtime_suspend(dev); + if (ret >= 0 && pm_runtime_status_suspended(dev)) + return 0; + if (ret >= 0) + ret = -EBUSY; + if (ret != -EAGAIN && ret != -EBUSY && ret != -EACCES) + return ret; + } else { + ret = usage ? -EAGAIN : -EBUSY; + if (action == T6040_RECOVERY_PM_FALLBACK) + break; + } + + if (attempt < T6040_RECOVERY_PM_RETRIES) { + unsigned int delay = T6040_RECOVERY_PM_BACKOFF_US << attempt; + + usleep_range(delay, delay + T6040_RECOVERY_PM_BACKOFF_US); + } + } + + usage = atomic_read(&dev->power.usage_count); + children = atomic_read(&dev->power.child_count); + dev_warn(tphy->dev, + "USB3: phymxwrap remains active (usage_count=%d child_count=%d, suspend %d); continuing with normal firmware start\n", + usage, children, ret); + return 1; +} + /* * Drop both domains with RTKit fully detached. phymxwrap is the mailbox * provider's runtime domain; common_dp is this device's runtime domain. Force @@ -915,22 +990,20 @@ static int t6040_usb3_hard_recover(struct t6040_usb2_phy *tphy) unsigned int dwell = max(recovery_dwell_ms, 1U); int ret, resume_ret; - if (WARN_ON(tphy->rtk)) - t6040_rtkit_stop_transport(tphy); + /* This also releases the mailbox provider reference synchronously. */ + t6040_rtkit_stop_transport(tphy); ret = pm_runtime_force_suspend(tphy->dev); if (ret < 0) return ret; - pm_runtime_barrier(tphy->mbox_dev); - ret = pm_runtime_suspend(tphy->mbox_dev); - if (ret < 0 && ret != -EACCES) { + ret = t6040_usb3_suspend_mbox(tphy); + if (ret < 0) { dev_err(tphy->dev, "USB3: phymxwrap suspend failed: %d\n", ret); goto resume_phy; } - if (!pm_runtime_status_suspended(tphy->mbox_dev)) { - ret = -EBUSY; - dev_err(tphy->dev, "USB3: phymxwrap did not power off\n"); + if (ret > 0) { + ret = 0; goto resume_phy; } From de1e9c618d6bcd7fa0038355de5f36963e8fa06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:22:27 +0100 Subject: [PATCH 244/269] phy: apple: t6040: don't wait for the PHY's own hold on the mailbox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The recovery of the PHY's coprocessor wants the mailbox's power domain off, and waits for whoever keeps the mailbox active, six looks over 62 to 72 ms, before it settles for a normal start of the firmware. When the PHY itself is still in use it is the one that keeps it active, and no wait ends that. pm_runtime_force_suspend() runs the PHY's suspend callback, but leaves a device with an active child or another user a consumer of its suppliers, and the mailbox is this PHY's supplier with a runtime PM device link. Every phy that is powered on is an active child: USB2, which a start of the host powers before USB3, and the phy whose power-on runs the recovery. On a J616s the recovery ended this way each time it ran: USB2 host sequence complete USB3: phymxwrap suspend failed: -11 USB3 PHY bring-up failed (-11), USB2 only Look once in that case, and go on to the normal start if the mailbox is held: the wait was dead time in every start of a host after a stop that was not clean. A PHY that the PM core did suspend has given its hold up, and waits for the mailbox's other users as before. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 31 +++++++++++++++++++++---- drivers/phy/apple/t6040-usb2.c | 30 ++++++++++++++++++------ 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 51ba747f9eab9..afb1673d396ca 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -53,19 +53,39 @@ static void t6040_recovery_power_lost_test(struct kunit *test) static void t6040_recovery_held_reference_test(struct kunit *test) { unsigned int attempt; + unsigned int retries = t6040_recovery_pm_retries(false); - for (attempt = 0; attempt < T6040_RECOVERY_PM_RETRIES; attempt++) + KUNIT_EXPECT_EQ(test, retries, (unsigned int)T6040_RECOVERY_PM_RETRIES); + for (attempt = 0; attempt < retries; attempt++) KUNIT_EXPECT_EQ(test, - t6040_recovery_pm_action(1, 0, attempt), + t6040_recovery_pm_action(1, 0, attempt, retries), T6040_RECOVERY_PM_RETRY); KUNIT_EXPECT_EQ(test, - t6040_recovery_pm_action(1, 0, attempt), + t6040_recovery_pm_action(1, 0, attempt, retries), T6040_RECOVERY_PM_FALLBACK); KUNIT_EXPECT_EQ(test, - t6040_recovery_pm_action(0, 1, attempt), + t6040_recovery_pm_action(0, 1, attempt, retries), T6040_RECOVERY_PM_FALLBACK); KUNIT_EXPECT_EQ(test, - t6040_recovery_pm_action(0, 0, attempt), + t6040_recovery_pm_action(0, 0, attempt, retries), + T6040_RECOVERY_PM_SUSPEND); +} + +/* + * A PHY that is still in use holds the mailbox itself: the recovery doesn't + * wait for that reference, and suspends the mailbox at once if nothing holds + * it after all. + */ +static void t6040_recovery_own_reference_test(struct kunit *test) +{ + unsigned int retries = t6040_recovery_pm_retries(true); + + KUNIT_EXPECT_EQ(test, retries, 0U); + KUNIT_EXPECT_EQ(test, t6040_recovery_pm_action(1, 0, 0, retries), + T6040_RECOVERY_PM_FALLBACK); + KUNIT_EXPECT_EQ(test, t6040_recovery_pm_action(0, 1, 0, retries), + T6040_RECOVERY_PM_FALLBACK); + KUNIT_EXPECT_EQ(test, t6040_recovery_pm_action(0, 0, 0, retries), T6040_RECOVERY_PM_SUSPEND); } @@ -75,6 +95,7 @@ static struct kunit_case t6040_recovery_test_cases[] = { KUNIT_CASE(t6040_recovery_retry_exhaustion_test), KUNIT_CASE(t6040_recovery_power_lost_test), KUNIT_CASE(t6040_recovery_held_reference_test), + KUNIT_CASE(t6040_recovery_own_reference_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 8ab5fb54b08c0..3faac12ee1e2c 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -237,15 +237,27 @@ enum t6040_recovery_pm_action { static enum t6040_recovery_pm_action t6040_recovery_pm_action(int usage_count, int child_count, - unsigned int attempt) + unsigned int attempt, unsigned int retries) { if (!usage_count && !child_count) return T6040_RECOVERY_PM_SUSPEND; - if (attempt < T6040_RECOVERY_PM_RETRIES) + if (attempt < retries) return T6040_RECOVERY_PM_RETRY; return T6040_RECOVERY_PM_FALLBACK; } +/* + * How often the recovery waits for the mailbox's users to go. Never when the + * PHY is one of them: forced into suspend while it is still in use, by a phy + * that is powered on or by the controller's reset, it stays a consumer of its + * suppliers, and the mailbox keeps the reference of that device link however + * long the recovery waits. + */ +static unsigned int t6040_recovery_pm_retries(bool phy_in_use) +{ + return phy_in_use ? 0 : T6040_RECOVERY_PM_RETRIES; +} + enum t6040_usb3_region { T6040_USB3_AXI2AF, T6040_USB3_AXI2AF_LIOA, @@ -931,14 +943,15 @@ static int t6040_common_dp_force_resume(struct t6040_usb2_phy *tphy) * * Return 0 after a real suspend, 1 when the caller should use that normal-start * fallback, and a negative error for a provider failure unrelated to a holder. + * @retries is how often to wait for a holder, see t6040_recovery_pm_retries(). */ -static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy) +static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy, unsigned int retries) { struct device *dev = tphy->mbox_dev; unsigned int attempt; int children, ret = -EBUSY, usage; - for (attempt = 0; attempt <= T6040_RECOVERY_PM_RETRIES; attempt++) { + for (attempt = 0; attempt <= retries; attempt++) { enum t6040_recovery_pm_action action; pm_runtime_barrier(dev); @@ -948,7 +961,7 @@ static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy) "USB3: phymxwrap recovery attempt %u: usage_count=%d child_count=%d\n", attempt, usage, children); - action = t6040_recovery_pm_action(usage, children, attempt); + action = t6040_recovery_pm_action(usage, children, attempt, retries); if (action == T6040_RECOVERY_PM_SUSPEND) { ret = pm_runtime_suspend(dev); if (ret >= 0 && pm_runtime_status_suspended(dev)) @@ -963,7 +976,7 @@ static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy) break; } - if (attempt < T6040_RECOVERY_PM_RETRIES) { + if (attempt < retries) { unsigned int delay = T6040_RECOVERY_PM_BACKOFF_US << attempt; usleep_range(delay, delay + T6040_RECOVERY_PM_BACKOFF_US); @@ -988,6 +1001,7 @@ static int t6040_usb3_suspend_mbox(struct t6040_usb2_phy *tphy) static int t6040_usb3_hard_recover(struct t6040_usb2_phy *tphy) { unsigned int dwell = max(recovery_dwell_ms, 1U); + unsigned int retries; int ret, resume_ret; /* This also releases the mailbox provider reference synchronously. */ @@ -997,7 +1011,9 @@ static int t6040_usb3_hard_recover(struct t6040_usb2_phy *tphy) if (ret < 0) return ret; - ret = t6040_usb3_suspend_mbox(tphy); + /* Left active by that: still in use, and so still the mailbox's consumer. */ + retries = t6040_recovery_pm_retries(!pm_runtime_status_suspended(tphy->dev)); + ret = t6040_usb3_suspend_mbox(tphy, retries); if (ret < 0) { dev_err(tphy->dev, "USB3: phymxwrap suspend failed: %d\n", ret); goto resume_phy; From d598aac540186b7e238adcdf6acb6c82df805a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:25 +0100 Subject: [PATCH 245/269] phy: apple: t6040: say which condition stops the host for a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A port keeps its host through a sleep when fifteen conditions hold. The line that reports a host that stops instead showed nine of the values behind them, so a port that was refused for one of the others (the controller in reset, the mode, a sleep it is still down or in reset from, the PIPE on the USB4 router, a router that runs, DisplayPort's pin assignment) could not be told from one that should have been kept. On a J616s with a display on the port: the host stops for the sleep: USB2 1, USB3 0, PIPE 0, DP 1/1, CIO 0, lent 1, failed 0/0 Name every condition that fails, as the Type-C PHY of the older SoCs does for its own list: here "because: dp_up dp_four_lanes usb3_lent". The line that refuses a sleep for a port that changed after the prepare gets the same names. KUnit: each condition alone gives its name and stops the host, a port that sleeps in place has none, and all of them fit into the buffer. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 120 ++++++++++++++++++++++++ drivers/phy/apple/t6040-usb2.c | 43 ++++++++- 2 files changed, 159 insertions(+), 4 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index afb1673d396ca..ff50107228c6b 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -209,6 +209,125 @@ static void t6040_sleep_failed_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); } +/* The port can't sleep in place, and the log names the conditions that fail as @names. */ +static void t6040_expect_miss(struct kunit *test, const struct t6040_usb2_phy *tphy, + const char *names) +{ + char why[T6040_SLEEP_MISSES_LEN]; + + t6040_sleep_in_place_misses(tphy, why, sizeof(why)); + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + KUNIT_EXPECT_STREQ(test, why, names); +} + +/* + * Each of the conditions of t6040_can_sleep_in_place() has its name in the + * log, all of them fit into the buffer, and a port that sleeps in place has + * none. + */ +static void t6040_sleep_misses_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + char why[T6040_SLEEP_MISSES_LEN]; + + t6040_sleep_in_place_misses(tphy, why, sizeof(why)); + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + KUNIT_EXPECT_STREQ(test, why, ""); + + tphy->pipehandler = NULL; + t6040_expect_miss(test, tphy, " no_pipehandler"); + tphy->pipehandler = (void __iomem *)tphy; + + tphy->powered = false; + t6040_expect_miss(test, tphy, " usb2_off"); + tphy->powered = true; + + tphy->dwc3_running = false; + t6040_expect_miss(test, tphy, " dwc3_in_reset"); + tphy->dwc3_running = true; + + tphy->mode = PHY_MODE_INVALID; + t6040_expect_miss(test, tphy, " mode"); + tphy->mode = PHY_MODE_USB_HOST; + + tphy->shutdown_failed = true; + t6040_expect_miss(test, tphy, " shutdown_failed"); + tphy->shutdown_failed = false; + + tphy->sleep_no_keep = true; + t6040_expect_miss(test, tphy, " sleep_no_keep"); + tphy->sleep_no_keep = false; + + tphy->sleep_down = true; + t6040_expect_miss(test, tphy, " sleep_down"); + tphy->sleep_down = false; + + tphy->sleep_reset = true; + t6040_expect_miss(test, tphy, " sleep_reset"); + tphy->sleep_reset = false; + + tphy->cio_up = true; + t6040_expect_miss(test, tphy, " cio_up"); + tphy->cio_up = false; + + tphy->pipe_tunneled = true; + t6040_expect_miss(test, tphy, " pipe_tunneled"); + tphy->pipe_tunneled = false; + + tphy->router_running = true; + t6040_expect_miss(test, tphy, " router_running"); + tphy->router_running = false; + + tphy->dp_up = true; + t6040_expect_miss(test, tphy, " dp_up"); + tphy->dp_up = false; + + tphy->dp_alt = true; + t6040_expect_miss(test, tphy, " dp_four_lanes"); + tphy->dp_alt = false; + + tphy->usb3_lent = true; + t6040_expect_miss(test, tphy, " usb3_lent"); + tphy->usb3_lent = false; + + tphy->usb3_up = true; + t6040_expect_miss(test, tphy, " pipe_state"); + tphy->usb3_up = false; + + tphy->pipe_switched = true; + t6040_expect_miss(test, tphy, " pipe_state"); + tphy->pipe_switched = false; + + /* Pin assignment D without a link fails nothing. */ + tphy->dp_alt = true; + tphy->dp_two_lanes = true; + t6040_sleep_in_place_misses(tphy, why, sizeof(why)); + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + KUNIT_EXPECT_STREQ(test, why, ""); + tphy->dp_two_lanes = false; + + /* A display on all four lanes, with USB3's lanes lent to it. */ + tphy->dp_up = true; + tphy->usb3_lent = true; + t6040_expect_miss(test, tphy, " dp_up dp_four_lanes usb3_lent"); + + /* Everything at once. */ + tphy->pipehandler = NULL; + tphy->powered = false; + tphy->dwc3_running = false; + tphy->mode = PHY_MODE_INVALID; + tphy->shutdown_failed = true; + tphy->sleep_no_keep = true; + tphy->sleep_down = true; + tphy->sleep_reset = true; + tphy->cio_up = true; + tphy->pipe_tunneled = true; + tphy->router_running = true; + tphy->usb3_up = true; + t6040_expect_miss(test, tphy, + " no_pipehandler usb2_off dwc3_in_reset mode shutdown_failed sleep_no_keep sleep_down sleep_reset cio_up pipe_tunneled router_running dp_up dp_four_lanes usb3_lent pipe_state"); +} + /* * Only the controller's reset and USB2 can fail the keeping of a host, and * only when the host was lost: not a sleep refused for a USB change, not what @@ -248,6 +367,7 @@ static struct kunit_case t6040_sleep_test_cases[] = { KUNIT_CASE(t6040_sleep_no_host_test), KUNIT_CASE(t6040_sleep_other_modes_test), KUNIT_CASE(t6040_sleep_failed_test), + KUNIT_CASE(t6040_sleep_misses_test), KUNIT_CASE(t6040_sleep_keep_failed_test), KUNIT_CASE(t6040_sleep_wake_usb3_test), {} diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 3faac12ee1e2c..0a04a09390e05 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -3339,6 +3339,35 @@ static bool t6040_can_sleep_in_place(const struct t6040_usb2_phy *tphy) !tphy->usb3_lent && tphy->usb3_up == tphy->pipe_switched; } +/* Room for every name of t6040_sleep_in_place_misses() at once. */ +#define T6040_SLEEP_MISSES_LEN 192 + +/* For the log: the conditions of t6040_can_sleep_in_place() that fail, by name. */ +static void t6040_sleep_in_place_misses(const struct t6040_usb2_phy *tphy, char *buf, size_t size) +{ + int len = 0; + +#define T6040_MISS(cond, name) \ + do { if (cond) len += scnprintf(buf + len, size - len, " %s", name); } while (0) + buf[0] = 0; + T6040_MISS(!tphy->pipehandler, "no_pipehandler"); + T6040_MISS(!tphy->powered, "usb2_off"); + T6040_MISS(!tphy->dwc3_running, "dwc3_in_reset"); + T6040_MISS(tphy->mode != PHY_MODE_USB_HOST, "mode"); + T6040_MISS(tphy->shutdown_failed, "shutdown_failed"); + T6040_MISS(tphy->sleep_no_keep, "sleep_no_keep"); + T6040_MISS(tphy->sleep_down, "sleep_down"); + T6040_MISS(tphy->sleep_reset, "sleep_reset"); + T6040_MISS(tphy->cio_up, "cio_up"); + T6040_MISS(tphy->pipe_tunneled, "pipe_tunneled"); + T6040_MISS(tphy->router_running, "router_running"); + T6040_MISS(tphy->dp_up, "dp_up"); + T6040_MISS(tphy->dp_alt && !tphy->dp_two_lanes, "dp_four_lanes"); + T6040_MISS(tphy->usb3_lent, "usb3_lent"); + T6040_MISS(tphy->usb3_up != tphy->pipe_switched, "pipe_state"); +#undef T6040_MISS +} + int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) { struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(usb2_phy); @@ -3348,11 +3377,14 @@ int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) guard(mutex)(&tphy->lock); if (!t6040_can_sleep_in_place(tphy)) { + char why[T6040_SLEEP_MISSES_LEN]; + + t6040_sleep_in_place_misses(tphy, why, sizeof(why)); dev_info(tphy->dev, - "the host stops for the sleep: USB2 %d, USB3 %d, PIPE %d, DP %d/%d, CIO %d, lent %d, failed %d/%d\n", + "the host stops for the sleep: USB2 %d, USB3 %d, PIPE %d, DP %d/%d, CIO %d, lent %d, failed %d/%d, because:%s\n", tphy->powered, tphy->usb3_up, tphy->pipe_switched, tphy->dp_alt, tphy->dp_up, tphy->cio_up, tphy->usb3_lent, tphy->shutdown_failed, - tphy->sleep_no_keep); + tphy->sleep_no_keep, why); return -EBUSY; } tphy->sleep_armed = true; @@ -3382,10 +3414,13 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) return 0; /* The mux moved since the prepare: that mode's clients don't come back this way. */ if (!t6040_can_sleep_in_place(tphy)) { + char why[T6040_SLEEP_MISSES_LEN]; + + t6040_sleep_in_place_misses(tphy, why, sizeof(why)); dev_err(tphy->dev, - "USB changed for the sleep (USB3 %d, PIPE %d, DP %d/%d, CIO %d): refusing it\n", + "USB changed for the sleep (USB3 %d, PIPE %d, DP %d/%d, CIO %d, because:%s): refusing it\n", tphy->usb3_up, tphy->pipe_switched, tphy->dp_alt, tphy->dp_up, - tphy->cio_up); + tphy->cio_up, why); tphy->sleep_ret = -EBUSY; return -EBUSY; } From 0af167419c1fc8b62c225798764d4272a4b1686a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:26 +0100 Subject: [PATCH 246/269] phy: apple: t6040: shut the lanes down when the CIO bring-up fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the PHY's coprocessor does not start or does not tune the PHY for USB4/Thunderbolt, the bring-up stops the coprocessor and puts the CFG block back into its boot state, and nothing else. By then both lanes' receivers are powered by override and the transmitters and the common block are released to the coprocessor. The teardown of a CIO session takes all of that down first, both TX lanes and the common block, then the receivers, and waits 200 us before the coprocessor stops; so did this error exit before the bring-up was rewritten for the USB4 router. The port then goes back to USB3, whose bring-up releases the overrides of the plug's lane only. Run the teardown's steps on the error exit again, so that a failed CIO attempt leaves the PHY as an ended CIO session does. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 0a04a09390e05..13a8c5542975c 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2550,6 +2550,7 @@ static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) { void __iomem *core = t6040_core(tphy); + unsigned int lane; bool flip; int ret; @@ -2603,6 +2604,16 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) return 0; firmware: + /* + * The lanes are powered by now: as t6040_cio_down_locked(), both TX lanes + * and the common block down, then the receivers, before the coprocessor + * stops and the CFG block goes back to its boot state. + */ + t6040_phy_tx_cmn_shutdown(tphy); + for (lane = 0; lane < 2; lane++) + t6040_cio_steps(tphy, t6040_cio_rx_powerdown, ARRAY_SIZE(t6040_cio_rx_powerdown), + lane); + fsleep(200); t6040_usb3_firmware_power_down(tphy); sleep: t6040_usb3_sleep(tphy); From a5eb6653beb5d356e1aa9ad0702ceac4ddc841d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:26 +0100 Subject: [PATCH 247/269] phy: apple: t6040: recover the coprocessor in the CIO bring-up too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PHY's coprocessor runs the same firmware for USB4/Thunderbolt as for USB3, and the driver's recovery belongs to the coprocessor, not to a mode: a power cycle of its domains before the start that follows a stop that was not clean, one more start when one does not tune the PHY, and the pending state ended by a start that does. The CIO bring-up had all three until it was rewritten for the USB4 router, and has had none since: - a CIO bring-up over a pending power cycle went ahead as if nothing was pending, and its clean start did not end that state, so the next USB3 bring-up still ran the recovery; - a start that failed once was final, and the port stayed out of USB4/Thunderbolt until the next attach. Run the recovery around the firmware start again, the way the USB3 bring-up does. A bring-up that works at once, with nothing pending, goes through no step it did not go through before. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 13a8c5542975c..f716f8f504472 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2551,6 +2551,7 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) { void __iomem *core = t6040_core(tphy); unsigned int lane; + bool cycle; bool flip; int ret; @@ -2584,6 +2585,16 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) t6040_usb3_reclaim_locked(tphy); return ret; } + + /* The coprocessor's recovery, as in t6040_usb3_up_locked(): it is the same firmware. */ + cycle = t6040_recovery_start(&tphy->recovery); +retry: + if (cycle) { + ret = t6040_usb3_hard_recover(tphy); + if (ret) + goto put; + } + ret = t6040_usb3_wake(tphy); if (ret) goto sleep; /* the coprocessor block may be unclocked: never touch it */ @@ -2592,8 +2603,9 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) t6040_cio_slpctrl(tphy); ret = t6040_phy_run_firmware(tphy, "CIO", flip); if (ret) - goto firmware; + goto retry_or_fail; + t6040_recovery_complete(&tphy->recovery); tphy->cio_up = true; tphy->cio_flip = flip; dev_info(tphy->dev, "CIO PHY up (mode 2), %s orientation: xbar %#x/%#x cfg %#x/%#x\n", @@ -2603,11 +2615,12 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) t6040_cio_pipe_update_locked(tphy); return 0; -firmware: +retry_or_fail: /* * The lanes are powered by now: as t6040_cio_down_locked(), both TX lanes * and the common block down, then the receivers, before the coprocessor - * stops and the CFG block goes back to its boot state. + * stops and the CFG block goes back to its boot state. That is the full + * teardown, and one complete fresh start may follow it. */ t6040_phy_tx_cmn_shutdown(tphy); for (lane = 0; lane < 2; lane++) @@ -2615,8 +2628,16 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) lane); fsleep(200); t6040_usb3_firmware_power_down(tphy); + t6040_usb3_sleep(tphy); + if (t6040_recovery_retry(&tphy->recovery)) { + cycle = true; + goto retry; + } + goto put; + sleep: t6040_usb3_sleep(tphy); +put: pm_runtime_put(tphy->dev); dev_warn(tphy->dev, "CIO PHY bring-up failed: %d\n", ret); /* The port stays out of CIO: give the lanes back to USB3. */ From 408b38d2189fb6def8f38400e0dd391f3013a539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:26 +0100 Subject: [PATCH 248/269] phy: apple: t6040: let DP wait for the host after an abandoned sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DP's power-on waits for the wake of a host that was kept through a sleep, from the PHY's early resume on. A sleep that is abandoned while devices still suspend, after the controller's glue has taken the port down, has no early resume phase: the devices that had suspended get their resume callback and nothing else. The host's resume brings the port back all the same, but a display that was reported before it found the port down and got no link, where it should have waited. Arm the wait in the resume callback as well. The PHY is a supplier of the controller, so this runs before the host's resume. After a sleep that was not abandoned it finds the port as the early phase left it and changes nothing. The wait is not armed earlier, when the port goes down, on purpose: while the system still suspends no wake is coming, and DP's power-on would stall for the whole 2 s. A sleep that is abandoned between the glue's suspend and the PHY's own gives the PHY no callback at all, and stays as it was. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index f716f8f504472..9aa32ee0b5246 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -3807,9 +3807,27 @@ static int t6040_usb2_resume_early(struct device *dev) return 0; } +/* + * A sleep that is abandoned while devices still suspend has no early phase, + * and the host's resume brings a port that went down for it back all the + * same. This runs before that resume, the PHY being its supplier: DP waits + * from here. After any other sleep it finds what the early phase left. + */ +static int t6040_usb2_resume(struct device *dev) +{ + struct t6040_usb2_phy *tphy = dev_get_drvdata(dev); + + if (!tphy) + return 0; + guard(mutex)(&tphy->lock); + WRITE_ONCE(tphy->sleep_back, tphy->sleep_down); + return 0; +} + static const struct dev_pm_ops t6040_usb2_pm_ops = { .suspend = t6040_usb2_suspend, .resume_early = t6040_usb2_resume_early, + .resume = t6040_usb2_resume, .freeze = t6040_usb2_freeze, .poweroff = t6040_usb2_poweroff, #ifdef CONFIG_HIBERNATION From 398da498357aac72ab59a034a103550d20be21e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:27 +0100 Subject: [PATCH 249/269] phy: apple: t6040: say who waits for the host's wake in DP's power-on MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DP's power-on can wait up to 2 s for the wake of a host that was kept through a sleep. Nothing said in which context that wait runs, and a device's resume callback that waited there would hold the whole resume up for as long. It doesn't come to that. The display's driver powers a port's DP phy on from its link work. After a sleep it queues that work from its resume on a workqueue that is not frozen, so it can run while devices still resume, and that is the wait's one customer. The Type-C controller reports a display from works that are frozen until every device has resumed, the host included, and then nothing is left to wait for. Write it down next to the wait. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 9aa32ee0b5246..059a0aadbc67c 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -1863,6 +1863,12 @@ static void t6040_usb3_reclaim_locked(struct t6040_usb2_phy *tphy) * then finds the port as on any running host: USB3 up for pin assignment D * to share, or to lend its lanes. If the wake fails or doesn't come, DP goes * on and gets what it gets on a port without a host. + * + * The wait runs in a work item, not under the PM core: the display's driver + * powers a port's DP phy on from its link work, and after a sleep it queues + * that from its own resume on a workqueue that isn't frozen, so the work can + * get here while devices still resume. What the Type-C controller reports + * comes from works that stay frozen until every device has resumed. */ static void t6040_dp_wait_host(struct t6040_usb2_phy *tphy) { From 2779ef41dabd393c2a9147f9bc6ae55aac5a9f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:27 +0100 Subject: [PATCH 250/269] phy: apple: t6040: describe the PIPE and tunnel-clock steps by what they do MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Eight comments of the USB4 and sleep code described what the PIPE handler and the tunnel clocks do by software mode numbers for USB3 and by the order in which code runs. Say what the hardware needs instead: where the PIPE goes and with which handshake, that the tunnel clocks go down first in the PHY's shutdown of mode 2, that the pixel clock index is the display controller's, and that the PHY's firmware can save its state for a sleep. No code changes. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 45 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 059a0aadbc67c..ec8da9e79ff5b 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -1344,7 +1344,7 @@ static void t6040_usb3_down_locked(struct t6040_usb2_phy *tphy) t6040_usb3_pipe_park(tphy); tphy->pipe_switched = false; } - /* The USB3 mode 0 from 3: the park from the USB4 router uses the same writes. */ + /* Off the USB4 router: the park from there uses the same writes. */ if (tphy->pipe_tunneled) { t6040_usb3_pipe_park(tphy); tphy->pipe_tunneled = false; @@ -2076,12 +2076,12 @@ static const struct phy_ops t6040_dp_phy_ops = { /* ---- DP tunnels: the pixel clocks of the router's DP IN adapters (PHY in CIO) ---- * * The DP 1.4 tunnel's init, update and shutdown and the DP clock - * configuration, as the platform sequences them: one fixed - * AUSPLL frequency feeds the DPTX pixel clocks, whose select field divides it - * per link rate. DP IN adapter 0 is taken to use PCLK1 and clock switch 1, - * adapter 1 PCLK2 and switch 2 (the PHY does not choose; the platform gets the index - * from the display controller). Driven only by appledrm through - * apple_atc_dp_tunnel_rate(), and only while the PHY is in CIO. + * configuration: one fixed AUSPLL frequency feeds the DPTX pixel clocks, + * whose select field divides it per link rate. DP IN adapter 0 is taken to + * use PCLK1 and clock switch 1, adapter 1 PCLK2 and switch 2 (the PHY does + * not choose: the index is the display controller's). Driven only by + * appledrm through apple_atc_dp_tunnel_rate(), and only while the PHY is in + * CIO. */ #define T6040_CORE_RCAL1 0x804 /* AUS40CMN_DIG RCAL1 */ #define T6040_CORE_RCAL1_DONE BIT(0) @@ -2261,7 +2261,7 @@ static int t6040_tun_switch(struct t6040_usb2_phy *tphy, unsigned int dpin, bool return 0; } -/* All tunnel clocks down, as the lane configuration does before the ACIO shutdown. */ +/* All tunnel clocks down, which the PHY's shutdown of mode 2 starts with. */ static void t6040_tun_stop_all_locked(struct t6040_usb2_phy *tphy) { unsigned int dpin; @@ -2371,8 +2371,8 @@ EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_open); * without them every mux state takes the path it took before. * * USB3 runs tunnelled through the router: the controller's PIPE goes to the - * USB4 backend (the USB3 mode 3, see t6040_cio_pipe_tunnel) - * while the PHY is in CIO and the router has asked for it. + * USB4 backend (see t6040_cio_pipe_tunnel()) while the PHY is in CIO and the + * router has asked for it. */ /* The ADT tables of the CIO-only tunables, in order; lane0/1 are required. */ @@ -2470,9 +2470,9 @@ static void t6040_cio_slpctrl(struct t6040_usb2_phy *tphy) } /* - * The USB3 mode 3 from 0: the PIPE from the dummy backend to the USB4 - * router, with the lock handshake only while the controller is out of reset. - * No non-selected override and no BIST bracket, unlike the PHY's own USB3. + * The PIPE from the dummy backend to the USB4 router, with the lock handshake + * only while the controller is out of reset. No non-selected override and no + * BIST bracket, unlike the PHY's own USB3. */ static void t6040_cio_pipe_tunnel(struct t6040_usb2_phy *tphy) { @@ -2498,9 +2498,9 @@ static void t6040_cio_pipe_tunnel(struct t6040_usb2_phy *tphy) /* * The PIPE is on the router while the router runs and carries USB3 for the - * controller (the platform asks for USB3 mode 3 only for a tunnelled USB3 - * transport, once it uses the tunnel), dwc3 holds the USB3 phy with its PIPE - * and the controller is out of reset. Otherwise it goes back to the dummy. + * controller (only a tunnelled USB3 transport needs it there, and only once + * it uses the tunnel), dwc3 holds the USB3 phy with its PIPE and the + * controller is out of reset. Otherwise it goes back to the dummy. */ static void t6040_cio_pipe_update_locked(struct t6040_usb2_phy *tphy) { @@ -2528,7 +2528,7 @@ static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) if (!tphy->cio_up) return; t6040_tun_stop_all_locked(tphy); - /* The USB3 mode 0 from 3: the same writes as the park from 2 */ + /* Off the router: the same writes as the park from this PHY */ if (tphy->pipe_tunneled) { t6040_usb3_pipe_park(tphy); tphy->pipe_tunneled = false; @@ -3334,12 +3334,11 @@ static int t6040_usb2_probe(struct platform_device *pdev) * * The register sequences are those of power_off, power_on and the reset, in * the order dwc3-apple's stop and start run them, so the CFG block is parked - * at the sleep's commit as after any detach. The platform has the PHY's firmware - * save its state instead and restore it at the wake; - * the devices stay without that, and it isn't - * done here. The steps are the same whether or not the SoC then loses power - * (a refused sleep, a dry run), and leave nothing the wake doesn't set up - * again. + * at the sleep's commit as after any detach. The PHY's firmware can save its + * state for a sleep and restore it at the wake instead; the devices stay + * without that, and it isn't done here. The steps are the same whether or + * not the SoC then loses power (a refused sleep, a dry run), and leave + * nothing the wake doesn't set up again. * * The phys' power counts and the reset's runtime PM reference are their * consumers' and stay as they are; sleep_down and sleep_reset say what the From ce572f2368d63b1abb39e28d7333e5bd3326a378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:28 +0100 Subject: [PATCH 251/269] usb: dwc3: apple: don't report a GUID that wasn't read after a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A host that is not kept through a sleep is reported with the core's mark register: "host lost its state in the sleep (-5, GUID 0)". When the PHY answered with an error, or the core was not set up again after the PHY had released its reset, the register is not read at all, and the line still showed a GUID of 0 next to the PHY's answer, as if the mark had been read and found gone. Say for that case that the host was not set up again, with what the PHY said. The line for a mark that was read and is gone stays as it is. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 0b82e06cd5c58..bba24571bb3e0 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -1380,10 +1380,14 @@ static bool dwc3_apple_sleep_kept(struct dwc3_apple *appledwc) return true; } ret = -EIO; + dev_warn(appledwc->dev, + "host lost its state in the sleep (%d, GUID %#x): restarting it\n", + ret, guid); + } else { + /* The mark wasn't read: what the PHY says, or the set-up after it, is why. */ + dev_warn(appledwc->dev, + "host not set up again after the sleep (PHY %d): restarting it\n", ret); } - - dev_warn(appledwc->dev, "host lost its state in the sleep (%d, GUID %#x): restarting it\n", - ret, guid); ret = dwc3_apple_exit(appledwc); if (ret) dev_err(appledwc->dev, "Failed to stop after sleep, err=%d\n", ret); From e33b305b9fb7fc170f51c9f3101af6a29b89452c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:28 +0100 Subject: [PATCH 252/269] usb: dwc3: apple: report a kept Type-C host's wake at the debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every wake of a T6040 Type-C port whose host was kept through the sleep logged "host kept through the sleep: core and PHY set up again, xHCI starts over" for each port, next to the PHY's own line for the same wake ("PHY back after the sleep with the host kept, USB3 up"). The two lines for a fixed hub's host, in the same switch, are at the debug level: put this one there as well. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index bba24571bb3e0..6f859072291ff 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -585,8 +585,8 @@ static int dwc3_apple_pre_host_resume(struct dwc3 *dwc) if (ret) break; appledwc->sleep_reinit = true; - dev_info(appledwc->dev, - "host kept through the sleep: core and PHY set up again, xHCI starts over\n"); + dev_dbg(appledwc->dev, + "host kept through the sleep: core and PHY set up again, xHCI starts over\n"); return 1; case DWC3_APPLE_WAKE_FAILED: default: From 990f40f192859cf52c65360b2da749d481585373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:29 +0100 Subject: [PATCH 253/269] phy: apple: t6040: drop the unused cio_flip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CIO bring-up stored the plug orientation it had programmed in cio_flip, and nothing ever read it: the teardown of mode 2 is the same for both orientations, and the next bring-up takes the orientation from the Type-C switch again. Drop the field and its one store. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index ec8da9e79ff5b..41080404d48cf 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -358,7 +358,6 @@ struct t6040_usb2_phy { bool tun_pll; /* AUSPLL programmed for DP tunnels */ bool router_running; /* the USB4 router runs on this port's CIO link */ bool usb3_tunnel_req; /* the router carries USB3 for the controller */ - bool cio_flip; /* A sleep with the host kept, see apple_t6040_atcphy_sleep_prepare() */ bool sleep_armed; /* taken on by the prepare, until the finish */ @@ -2613,7 +2612,6 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) t6040_recovery_complete(&tphy->recovery); tphy->cio_up = true; - tphy->cio_flip = flip; dev_info(tphy->dev, "CIO PHY up (mode 2), %s orientation: xbar %#x/%#x cfg %#x/%#x\n", flip ? "reverse" : "normal", readl(core + T6040_CORE_XBAR), readl(core + T6040_CORE_XBAR_LANES), readl(t6040_cfg(tphy) + T6040_CFG_CTRL), From 8a6ef15b84d17710c26018ee29c5678048528b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:29 +0100 Subject: [PATCH 254/269] phy: apple: t6040: give the tunable tables designated indices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PHY's tunable tables are applied by their position in one list, and three places depended on positions written out as numbers: the AUSPLL table that USB3 applies once more (7), the lane tables that DP writes again after each link power-up (4 and 5), and the generic tables DP starts with (the first 6). A static_assert checked that the last two agree with each other, not that the list has those tables at those positions. Name the positions in an enum and build the list with designated initialisers, so that a table that moves takes its users with it. The generated code doesn't change. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 46 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 41080404d48cf..ebe5bae0f31a5 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -283,24 +283,34 @@ static const char * const t6040_usb3_region_names[T6040_USB3_NR_REGIONS] = { * The ADT tables AppleT6040TypeCPhy applies at every USB3 power-on (mode 4), * in its order; the device tree carries them per port, as calibration. */ +enum t6040_usb3_tunable { + T6040_USB3_TUNABLE_AXI2AF, + T6040_USB3_TUNABLE_FABRIC, + T6040_USB3_TUNABLE_COMMON_CFG, + T6040_USB3_TUNABLE_AXI2AF_LIOA, + T6040_USB3_TUNABLE_LANE0, + T6040_USB3_TUNABLE_LANE1, + T6040_USB3_TUNABLE_AUS40CMN_SHM, + T6040_USB3_TUNABLE_AUSPLL, + T6040_USB3_TUNABLE_CIO4PLL, + T6040_USB3_NR_TUNABLES, +}; + static const struct { const char *prop; enum t6040_usb3_region region; -} t6040_usb3_tunable_list[] = { - { "apple,tunable-axi2af", T6040_USB3_AXI2AF }, - { "apple,tunable-fabric", T6040_USB3_FABRIC }, - { "apple,tunable-common-cfg", T6040_USB3_COMMON_CFG }, - { "apple,tunable-axi2af-lioa", T6040_USB3_AXI2AF_LIOA }, - { "apple,tunable-lane0", T6040_USB3_CORE }, - { "apple,tunable-lane1", T6040_USB3_CORE }, - { "apple,tunable-aus40cmn-shm", T6040_USB3_CORE }, - { "apple,tunable-auspll", T6040_USB3_CORE }, - { "apple,tunable-cio4pll", T6040_USB3_CORE }, +} t6040_usb3_tunable_list[T6040_USB3_NR_TUNABLES] = { + [T6040_USB3_TUNABLE_AXI2AF] = { "apple,tunable-axi2af", T6040_USB3_AXI2AF }, + [T6040_USB3_TUNABLE_FABRIC] = { "apple,tunable-fabric", T6040_USB3_FABRIC }, + [T6040_USB3_TUNABLE_COMMON_CFG] = { "apple,tunable-common-cfg", T6040_USB3_COMMON_CFG }, + [T6040_USB3_TUNABLE_AXI2AF_LIOA] = { "apple,tunable-axi2af-lioa", T6040_USB3_AXI2AF_LIOA }, + [T6040_USB3_TUNABLE_LANE0] = { "apple,tunable-lane0", T6040_USB3_CORE }, + [T6040_USB3_TUNABLE_LANE1] = { "apple,tunable-lane1", T6040_USB3_CORE }, + [T6040_USB3_TUNABLE_AUS40CMN_SHM] = { "apple,tunable-aus40cmn-shm", T6040_USB3_CORE }, + [T6040_USB3_TUNABLE_AUSPLL] = { "apple,tunable-auspll", T6040_USB3_CORE }, + [T6040_USB3_TUNABLE_CIO4PLL] = { "apple,tunable-cio4pll", T6040_USB3_CORE }, }; -#define T6040_USB3_TUNABLE_AUSPLL 7 -#define T6040_USB3_NR_TUNABLES ARRAY_SIZE(t6040_usb3_tunable_list) - struct t6040_usb2_phy { struct device *dev; void __iomem *usb2; @@ -1586,8 +1596,8 @@ static bool t6040_dp_only(struct t6040_usb2_phy *tphy) return !tphy->usb3_ok; } -/* apply_tunables' generic tables: the first six of t6040_usb3_tunable_list. */ -#define T6040_DP_NR_GENERIC_TUNABLES 6 +/* apply_tunables' generic tables: those of t6040_usb3_tunable_list up to the lanes'. */ +#define T6040_DP_NR_GENERIC_TUNABLES (T6040_USB3_TUNABLE_LANE1 + 1) /* * slpctrl_init for DP on both lanes: the cmn_rm_ovrd/txrx_rm_ovrd releases of @@ -1733,10 +1743,6 @@ static int t6040_dp_seq_step(struct t6040_usb2_phy *tphy, const struct atc_op *s return ret; } -/* The ADT's lane0 and lane1 tables in t6040_usb3_tunable_list. */ -#define T6040_USB3_TUNABLE_LANE0 4 -static_assert(T6040_USB3_TUNABLE_LANE0 + 2 == T6040_DP_NR_GENERIC_TUNABLES); - /* * A lane's registers are back at their reset values each time the lane has been powered * down: after one link at a rate, one DPMS cycle or one failed training, the lane tables @@ -1748,7 +1754,7 @@ static void t6040_dp_lane_tunables(struct t6040_usb2_phy *tphy) void __iomem *core = t6040_core(tphy); size_t i; - for (i = T6040_USB3_TUNABLE_LANE0; i < T6040_USB3_TUNABLE_LANE0 + 2; i++) + for (i = T6040_USB3_TUNABLE_LANE0; i <= T6040_USB3_TUNABLE_LANE1; i++) apple_tunable_apply(core, tphy->tunables[i]); for (i = ARRAY_SIZE(t6040_dptx_tunables) - T6040_DPTX_LANE_TUNABLES; i < ARRAY_SIZE(t6040_dptx_tunables); i++) From 87b5f64e4c067cca37551afdb189e581755a5225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:30 +0100 Subject: [PATCH 255/269] phy: apple: t6040: one assignment per statement in the restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit checkpatch --strict: "multiple assignments should be avoided" for the two DP tunnel rates that a restore from disk clears. Clear them one by one. The generated code doesn't change. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index ebe5bae0f31a5..8386c1140fbd3 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -3697,7 +3697,8 @@ static int t6040_usb2_restore_early(struct device *dev) tphy->usb3_tunnel_req = false; tphy->pipe_tunneled = false; tphy->tun_pll = false; - tphy->tun_rate[0] = tphy->tun_rate[1] = 0; + tphy->tun_rate[0] = 0; + tphy->tun_rate[1] = 0; pm_runtime_put_noidle(dev); } dev_info(dev, "restore: PHY state reset (usb3 up %d, pipe %d, lent %d, dp up %d, 2-lane %d)\n", From 4d767e9ef236129dd0fedc9349d9cf47985dfe40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:38:30 +0100 Subject: [PATCH 256/269] phy: apple: t6040: one PHY shutdown for mode 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The teardown of a CIO session and the error exit of the CIO bring-up run the same shutdown: both TX lanes and the common block down, the two receivers powered down, 200 us, the coprocessor stopped, the CFG block back in its boot state. Put it into one function for both. The register accesses of either path and their order are as before. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 45 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 8386c1140fbd3..7ce416591aed8 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2523,11 +2523,28 @@ static void t6040_cio_pipe_update_locked(struct t6040_usb2_phy *tphy) } } -/* The PHY shutdown of mode 2: part A, the coprocessor down, part B. */ +/* + * The PHY shutdown of mode 2 once the PIPE is off the router: part A (both TX + * lanes and the common block, then the receivers), the coprocessor down, part + * B. Also what a bring-up that failed after the lanes' power-up has to undo. + */ +static void t6040_cio_phy_shutdown(struct t6040_usb2_phy *tphy) +{ + unsigned int lane; + + t6040_phy_tx_cmn_shutdown(tphy); + for (lane = 0; lane < 2; lane++) + t6040_cio_steps(tphy, t6040_cio_rx_powerdown, ARRAY_SIZE(t6040_cio_rx_powerdown), + lane); + fsleep(200); + t6040_usb3_firmware_power_down(tphy); + t6040_usb3_sleep(tphy); +} + +/* The PHY out of mode 2: the tunnel clocks, the PIPE, then the PHY shutdown. */ static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) { u32 uc; - unsigned int lane; lockdep_assert_held(&tphy->lock); if (!tphy->cio_up) @@ -2539,13 +2556,7 @@ static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) tphy->pipe_tunneled = false; } uc = readl(t6040_core(tphy) + T6040_CORE_UC_CTRL); - t6040_phy_tx_cmn_shutdown(tphy); - for (lane = 0; lane < 2; lane++) - t6040_cio_steps(tphy, t6040_cio_rx_powerdown, ARRAY_SIZE(t6040_cio_rx_powerdown), - lane); - fsleep(200); - t6040_usb3_firmware_power_down(tphy); - t6040_usb3_sleep(tphy); + t6040_cio_phy_shutdown(tphy); dev_info(tphy->dev, "CIO PHY asleep: uc %#x -> %#x, cfg %#x/%#x\n", uc, readl(t6040_core(tphy) + T6040_CORE_UC_CTRL), readl(t6040_cfg(tphy) + T6040_CFG_CTRL), @@ -2561,7 +2572,6 @@ static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) { void __iomem *core = t6040_core(tphy); - unsigned int lane; bool cycle; bool flip; int ret; @@ -2626,19 +2636,8 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) return 0; retry_or_fail: - /* - * The lanes are powered by now: as t6040_cio_down_locked(), both TX lanes - * and the common block down, then the receivers, before the coprocessor - * stops and the CFG block goes back to its boot state. That is the full - * teardown, and one complete fresh start may follow it. - */ - t6040_phy_tx_cmn_shutdown(tphy); - for (lane = 0; lane < 2; lane++) - t6040_cio_steps(tphy, t6040_cio_rx_powerdown, ARRAY_SIZE(t6040_cio_rx_powerdown), - lane); - fsleep(200); - t6040_usb3_firmware_power_down(tphy); - t6040_usb3_sleep(tphy); + /* The lanes are powered by now: the full shutdown, and one fresh start may follow. */ + t6040_cio_phy_shutdown(tphy); if (t6040_recovery_retry(&tphy->recovery)) { cycle = true; goto retry; From a22d41e778dda0648950e02b84ad32c3d0e4c037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Thu, 12 Nov 2026 23:56:45 +0100 Subject: [PATCH 257/269] phy: apple: t6040: a powered-down sleep ends a failed USB2 power-down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a write of the USB2 power-down sequence does not read back, the PHY marks itself failed and refuses every later operation: no power cycle of a PHY in an unknown state. A port that goes down for a sleep with its host kept logs that and lets the sleep go on, which is right, as the sleep powers the SoC down and the USB2 PHY with it. But the mark outlived the sleep. The wake refused to bring USB2 up on a PHY that had just been through its reset, the host was restarted, its stop failed on the same mark, and from then on the stop of that host failed before every sleep, which refuses the sleep: one write that did not land, and the machine did not sleep again until it was rebooted. A restore from disk already drops the mark, as the PHY comes back cold. A sleep that powers the SoC down is the same for the PHY: drop the mark there too, next to the coprocessor's pending power cycle, in one helper for both. After a sleep that left the SoC powered the mark stays, and the wake reports the port as before. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 21 ++++++++++++++++ drivers/phy/apple/t6040-usb2.c | 33 +++++++++++++++++++------ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index ff50107228c6b..355cad2854eee 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -362,6 +362,26 @@ static void t6040_sleep_wake_usb3_test(struct kunit *test) KUNIT_EXPECT_TRUE(test, t6040_sleep_wake_usb3(tphy)); } +/* + * A sleep that powered the SoC down has reset the USB2 PHY and power-cycled + * the coprocessor: a power-down that didn't land before it and a power cycle + * that was pending are behind the port, which sleeps in place again. + */ +static void t6040_sleep_power_lost_test(struct kunit *test) +{ + struct t6040_usb2_phy *tphy = t6040_sleep_test_phy(test); + + tphy->shutdown_failed = true; + tphy->sleep_usb3 = true; + t6040_recovery_power_down(&tphy->recovery, true, false); + KUNIT_EXPECT_FALSE(test, t6040_can_sleep_in_place(tphy)); + KUNIT_EXPECT_FALSE(test, t6040_sleep_wake_usb3(tphy)); + t6040_power_lost(tphy); + KUNIT_EXPECT_FALSE(test, tphy->shutdown_failed); + KUNIT_EXPECT_TRUE(test, t6040_can_sleep_in_place(tphy)); + KUNIT_EXPECT_TRUE(test, t6040_sleep_wake_usb3(tphy)); +} + static struct kunit_case t6040_sleep_test_cases[] = { KUNIT_CASE(t6040_sleep_usb_test), KUNIT_CASE(t6040_sleep_no_host_test), @@ -370,6 +390,7 @@ static struct kunit_case t6040_sleep_test_cases[] = { KUNIT_CASE(t6040_sleep_misses_test), KUNIT_CASE(t6040_sleep_keep_failed_test), KUNIT_CASE(t6040_sleep_wake_usb3_test), + KUNIT_CASE(t6040_sleep_power_lost_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 7ce416591aed8..ba14b09564db2 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -224,7 +224,7 @@ static void t6040_recovery_complete(struct t6040_recovery_state *state) * The SoC lost power in a sleep, and the coprocessor and both of its power * domains with it: that is the power cycle a stop that wasn't clean asks for. */ -static void __maybe_unused t6040_recovery_power_lost(struct t6040_recovery_state *state) +static void t6040_recovery_power_lost(struct t6040_recovery_state *state) { state->cycle_pending = false; } @@ -3475,6 +3475,12 @@ int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) /* As dwc3-apple's stop: USB3 with its PIPE, USB2, then the controller's reset. */ t6040_usb3_down_locked(tphy); ret = t6040_usb2_off_locked(tphy); + /* + * No reason to refuse the sleep: one that powers the SoC down resets the + * USB2 PHY whatever a write that didn't land has left of it, and the wake + * starts from there (t6040_power_lost()). After any other sleep the PHY + * stays failed, and the wake says so. + */ if (ret) dev_err(tphy->dev, "USB2 did not power down for the sleep: %d\n", ret); t6040_dwc3_reset_hold_locked(tphy); @@ -3646,6 +3652,18 @@ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) } EXPORT_SYMBOL_GPL(apple_t6040_atcphy_sleep_finish); +/* + * The SoC lost power in a sleep or for a resume from disk, and the PHY with + * it. That was the power cycle its coprocessor may have been waiting for, and + * it put the USB2 PHY back into its reset state: a power-down or a start that + * didn't land before it says nothing about the PHY any more. + */ +static void __maybe_unused t6040_power_lost(struct t6040_usb2_phy *tphy) +{ + t6040_recovery_power_lost(&tphy->recovery); + tphy->shutdown_failed = false; +} + #ifdef CONFIG_HIBERNATION static int t6040_phy_count(struct phy *phy) { @@ -3710,8 +3728,7 @@ static int t6040_usb2_restore_early(struct device *dev) tphy->dp_mode_two_lanes = false; tphy->dp_mode_flip = false; tphy->dp_rate = -1; - tphy->shutdown_failed = false; - t6040_recovery_power_lost(&tphy->recovery); + t6040_power_lost(tphy); return 0; } @@ -3798,10 +3815,10 @@ static int t6040_usb2_poweroff(struct device *dev) * t6040_dp_wait_host(). Early, before any .resume: the Type-C controller * reports a display from its own. * - * If the firmware woke the system, the SoC was powered down, and the - * coprocessor and its domains with it: no start after this sleep has a power - * cycle of theirs to wait for or to run, the wake of a host kept through it - * included. + * If the firmware woke the system, the SoC was powered down, and the PHY and + * its coprocessor with it: no start after this sleep has a power cycle to + * wait for or to run, and none finds the USB2 PHY failed by a write from + * before the sleep, the wake of a host kept through it included. */ static int t6040_usb2_resume_early(struct device *dev) { @@ -3811,7 +3828,7 @@ static int t6040_usb2_resume_early(struct device *dev) return 0; guard(mutex)(&tphy->lock); if (pm_resume_via_firmware()) - t6040_recovery_power_lost(&tphy->recovery); + t6040_power_lost(tphy); WRITE_ONCE(tphy->sleep_back, tphy->sleep_down); return 0; } From 2b1b8ded787c2b51bb410406ff5a15f6a01a2fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 00:09:19 +0100 Subject: [PATCH 258/269] phy: apple: t6040: keep the coprocessor powered through a suspend-to-idle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A suspend-to-idle leaves a USB host running: xHCI keeps going so that a device can wake the system, and the controller's glue keeps the controller's power domain on by being a syscore device. On T8132 the Type-C PHY shares that domain. On T6040 it does not: the PHY sits in atcN_common_dp, below atcN_phymxwrap, which powers the PHY's coprocessor and its mailbox, and in the noirq phase of every system sleep the PM domain core powers off the domain of each device that has suspended. So a suspend-to-idle took the power from a coprocessor whose firmware ran, and the driver went on taking it for running. The next stop of USB3 then asked a coprocessor that was no longer there: RTKit: AP idle not acknowledged: -62 USB3: PHY coprocessor idle failed: -62 USB3: no asynchronous Power Down acknowledge On a J616s this came with the first deep sleep after one suspend-to-idle of 15 s, on both ports with USB3 up at once, and with no sleep of a boot that had no suspend-to-idle before it. Keep the domain on for as long as the firmware runs: refuse the power-off in the domain's notifier while the coprocessor is between its start and the end of its stop. The mailbox's domain stays with it, as the parent of a domain that is on. A host that goes down for a deep sleep stops the coprocessor first, and so do CIO's teardown for every sleep and the recovery before its power cycle: none of them is held up. The other way, to stop the coprocessor for the suspend-to-idle and to start it again after it, would have the firmware tune the PHY once more under a link that is up, with devices that are only suspended. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-recovery-test.c | 15 +++++++ drivers/phy/apple/t6040-usb2.c | 57 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/drivers/phy/apple/t6040-recovery-test.c b/drivers/phy/apple/t6040-recovery-test.c index 355cad2854eee..76b50f35cba2b 100644 --- a/drivers/phy/apple/t6040-recovery-test.c +++ b/drivers/phy/apple/t6040-recovery-test.c @@ -89,6 +89,20 @@ static void t6040_recovery_own_reference_test(struct kunit *test) T6040_RECOVERY_PM_SUSPEND); } +/* + * The coprocessor's power domain stays on while its firmware runs, whoever + * asks for the power-off: a suspend-to-idle with a host running does. A + * stopped coprocessor lets it go, and the other notifications pass. + */ +static void t6040_pd_stays_on_test(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, t6040_pd_stays_on(GENPD_NOTIFY_PRE_OFF, true)); + KUNIT_EXPECT_FALSE(test, t6040_pd_stays_on(GENPD_NOTIFY_PRE_OFF, false)); + KUNIT_EXPECT_FALSE(test, t6040_pd_stays_on(GENPD_NOTIFY_OFF, true)); + KUNIT_EXPECT_FALSE(test, t6040_pd_stays_on(GENPD_NOTIFY_PRE_ON, true)); + KUNIT_EXPECT_FALSE(test, t6040_pd_stays_on(GENPD_NOTIFY_ON, true)); +} + static struct kunit_case t6040_recovery_test_cases[] = { KUNIT_CASE(t6040_recovery_missed_ack_test), KUNIT_CASE(t6040_recovery_failed_tune_test), @@ -96,6 +110,7 @@ static struct kunit_case t6040_recovery_test_cases[] = { KUNIT_CASE(t6040_recovery_power_lost_test), KUNIT_CASE(t6040_recovery_held_reference_test), KUNIT_CASE(t6040_recovery_own_reference_test), + KUNIT_CASE(t6040_pd_stays_on_test), {} }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index ba14b09564db2..070223f81ba7a 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -335,6 +336,8 @@ struct t6040_usb2_phy { /* ADT tunable_USB2PHY_HOST, applied after the signal setup. */ struct apple_tunable *tunable_usb2_host; struct apple_rtkit *rtk; + struct notifier_block pd_nb; /* keeps the power domain on under a running coprocessor */ + bool fw_live; /* the coprocessor runs; pd_nb reads it under the domain's lock */ struct device *mbox_dev; struct completion app_reply; struct t6040_recovery_state recovery; @@ -788,6 +791,8 @@ static int t6040_usb3_cpu_start(struct t6040_usb2_phy *tphy) dev_err(tphy->dev, "USB3: the PHY coprocessor runs without an RTKit session\n"); return -EBUSY; } + /* From here to the end of its stop the coprocessor's power domains stay on. */ + WRITE_ONCE(tphy->fw_live, true); writel(T6040_CPU_RUN_START, cpu + T6040_CPU_RUN); usleep_range(5000, 6000); return apple_rtkit_boot(tphy->rtk); @@ -893,6 +898,8 @@ static void t6040_usb3_firmware_power_down(struct t6040_usb2_phy *tphy) /* Leave the transport stopped until the next firmware start. Free drains RX. */ t6040_rtkit_stop_transport(tphy); + /* Stopped, or to be power-cycled: either way its domains may go off again. */ + WRITE_ONCE(tphy->fw_live, false); /* Power Down is asynchronous; never wait solely for its application reply. */ acknowledged = completion_done(&tphy->app_reply); if (!acknowledged) @@ -2931,6 +2938,51 @@ static void t6040_rtkit_cleanup(void *data) t6040_rtkit_stop_transport(data); } +/* + * Whether the PHY's power domain has to stay on at a power-off that is about + * to happen: while the coprocessor's firmware runs. + * + * The PHY's domain, and the mailbox's above it, power the coprocessor. A + * system sleep takes the domain of every device that has suspended off, a + * suspend-to-idle included, which leaves a host running: its controller + * keeps its own domain as a syscore device, and nothing kept this one. The + * coprocessor then lost its power under a firmware the driver took for + * running, and did not acknowledge the idle request of its next stop. + */ +static bool t6040_pd_stays_on(unsigned long action, bool fw_live) +{ + return action == GENPD_NOTIFY_PRE_OFF && fw_live; +} + +/* Called by the PM domain with its lock held, which is a spinlock for these domains. */ +static int t6040_pd_notify(struct notifier_block *nb, unsigned long action, void *data) +{ + struct t6040_usb2_phy *tphy = container_of(nb, struct t6040_usb2_phy, pd_nb); + + if (t6040_pd_stays_on(action, READ_ONCE(tphy->fw_live))) + return notifier_from_errno(-EBUSY); + return NOTIFY_OK; +} + +static void t6040_pd_notifier_remove(void *data) +{ + dev_pm_genpd_remove_notifier(data); +} + +/* Without a single PM domain nothing takes the coprocessor's power away under it. */ +static int t6040_pd_notifier_init(struct t6040_usb2_phy *tphy) +{ + int ret; + + tphy->pd_nb.notifier_call = t6040_pd_notify; + ret = dev_pm_genpd_add_notifier(tphy->dev, &tphy->pd_nb); + if (ret == -ENODEV || ret == -EOPNOTSUPP) + return 0; + if (ret) + return ret; + return devm_add_action_or_reset(tphy->dev, t6040_pd_notifier_remove, tphy->dev); +} + static void t6040_mbox_dev_put(void *data) { put_device(data); @@ -3103,6 +3155,10 @@ static int t6040_usb3_probe(struct t6040_usb2_phy *tphy, struct platform_device if (ret) return ret; + ret = t6040_pd_notifier_init(tphy); + if (ret) + return dev_err_probe(dev, ret, "failed to follow the PHY's power domain\n"); + tphy->usb3_ok = true; return 0; } @@ -3662,6 +3718,7 @@ static void __maybe_unused t6040_power_lost(struct t6040_usb2_phy *tphy) { t6040_recovery_power_lost(&tphy->recovery); tphy->shutdown_failed = false; + WRITE_ONCE(tphy->fw_live, false); } #ifdef CONFIG_HIBERNATION From f54115fea34ee59fee328f44003269bec434d7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 03:41:43 +0100 Subject: [PATCH 259/269] usb: dwc3: apple: suspend and resume asynchronously MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The glue device has a suspend callback and no resume callback, and was not marked asynchronous. Its xHCI child is, but an asynchronous device starts to resume only once the PM core has reached it in the list, and the core gets past the glue device only in list order. On a J616s wake from suspend-to-RAM the main PM thread waited 522 ms in a synchronous device ahead of the glue (an rfkill device behind the Wi-Fi function's restart), and both USB hosts with a dock's hub tree below them started only then. On the way in the two glue devices' suspends ran one after the other, 120 and 68 ms. Mark the glue asynchronous. What it depends on is given by its parent and its device links (PHY, DARTs, power domains), which the PM core keeps for asynchronous devices too. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 6f859072291ff..a180898e0ac2c 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -1689,6 +1689,11 @@ static int dwc3_apple_probe(struct platform_device *pdev) appledwc->idle_rpm = of_device_is_compatible(dev->of_node, "apple,t6040-dwc3") || of_device_is_compatible(dev->of_node, "apple,t8132-dwc3"); mutex_init(&appledwc->lock); + /* + * Its suspend takes 70-120 ms per controller, and its xHCI child, which is asynchronous, + * can't start to resume before the PM core has reached this device in its list. + */ + device_enable_async_suspend(dev); /* A never-connected port has no generic core probe to publish this. */ appledwc->dwc.dev = dev; platform_set_drvdata(pdev, &appledwc->dwc); From 9a958439519f032d7a8fbd55d5800713c652170a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:39:01 +0100 Subject: [PATCH 260/269] phy: apple: t6040: use one DPTX tunable table for USB3 and DP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit t6040_usb3_tunables() carried a private copy of the nine fixed DPTX tunable writes that the DP path already keeps in t6040_dptx_tunables[]. Move that table and its writer above the USB3 tunables and use them from both places, so the two paths cannot drift apart. The writes and their order are unchanged: the object code of the USB3 bring-up now calls the shared writer at the same point, between the tunable lists and the AUSPLL tunable, and the duplicate table is gone from the object. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 64 ++++++++++++++-------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 070223f81ba7a..c56edf9581135 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -685,30 +685,43 @@ static int t6040_usb3_wake(struct t6040_usb2_phy *tphy) return 0; } +/* The fixed DPTX tunable writes (apply_dptx_tunables, shared with USB3). */ +static const struct { u32 off, clear, set; } t6040_dptx_tunables[] = { + { 0x00800, 0x000003f0, 0x000001c0 }, /* AUS40CMN_DIG RCAL0 */ + { 0x00818, 0x0000f000, 0x00003000 }, /* AUS40CMN_DIG RCAL5 */ + { 0x03888, 0x000000ff, 0x00000027 }, /* CIO4PLL FREQDESCRIPTOR_0_C */ + { 0x28004, 0x00000038, 0x00000008 }, /* AUX_LDO_CTRL */ + { 0x03adc, 0x00003ff0, 0x00001760 }, /* CIO4PLL CFG_FRACN_FUNC */ + /* The lane entries, T6040_DPTX_LANE_TUNABLES of them, come last. */ + { 0x0e0cc, 0x01f80000, 0x00380000 }, /* LANE0 TX_EQ_CTRL_REG11 */ + { 0x1e0cc, 0x01f80000, 0x00380000 }, /* LANE1 TX_EQ_CTRL_REG11 */ + { 0x0d090, 0x01f80000, 0x00380000 }, /* LANE0 RX_CFG TX_EQ_CTRL_REG11 */ + { 0x1d090, 0x01f80000, 0x00380000 }, /* LANE1 RX_CFG TX_EQ_CTRL_REG11 */ +}; + +#define T6040_DPTX_LANE_TUNABLES 4 + +static void t6040_dp_fixed_tunables(void __iomem *core) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(t6040_dptx_tunables); i++) + t6040_usb2_mask32(core + t6040_dptx_tunables[i].off, t6040_dptx_tunables[i].clear, + t6040_dptx_tunables[i].set); +} + /* * B1: the tables, then apply_dptx_tunables' fixed writes (atc-phy reg[] offsets * relative to the core) and AUSPLL_CORE once more. */ static void t6040_usb3_tunables(struct t6040_usb2_phy *tphy) { - static const struct { u32 off, clear, set; } dptx[] = { - { 0x00800, 0x000003f0, 0x000001c0 }, /* AUS40CMN_DIG RCAL0 */ - { 0x00818, 0x0000f000, 0x00003000 }, /* AUS40CMN_DIG RCAL5 */ - { 0x03888, 0x000000ff, 0x00000027 }, /* CIO4PLL FREQDESCRIPTOR_0_C */ - { 0x28004, 0x00000038, 0x00000008 }, /* AUX_LDO_CTRL */ - { 0x03adc, 0x00003ff0, 0x00001760 }, /* CIO4PLL CFG_FRACN_FUNC */ - { 0x0e0cc, 0x01f80000, 0x00380000 }, /* LANE0 TX_EQ_CTRL_REG11 */ - { 0x1e0cc, 0x01f80000, 0x00380000 }, /* LANE1 TX_EQ_CTRL_REG11 */ - { 0x0d090, 0x01f80000, 0x00380000 }, /* LANE0 RX_CFG TX_EQ_CTRL_REG11 */ - { 0x1d090, 0x01f80000, 0x00380000 }, /* LANE1 RX_CFG TX_EQ_CTRL_REG11 */ - }; size_t i; for (i = 0; i < T6040_USB3_NR_TUNABLES; i++) apple_tunable_apply(tphy->regs[t6040_usb3_tunable_list[i].region], tphy->tunables[i]); - for (i = 0; i < ARRAY_SIZE(dptx); i++) - t6040_usb2_mask32(t6040_core(tphy) + dptx[i].off, dptx[i].clear, dptx[i].set); + t6040_dp_fixed_tunables(t6040_core(tphy)); apple_tunable_apply(t6040_core(tphy), tphy->tunables[T6040_USB3_TUNABLE_AUSPLL]); } @@ -1459,31 +1472,6 @@ static void t6040_usb3_switch_unregister(void *sw) * the two-lane group link sequences below. */ -/* The fixed DPTX tunable writes (apply_dptx_tunables, shared with USB3). */ -static const struct { u32 off, clear, set; } t6040_dptx_tunables[] = { - { 0x00800, 0x000003f0, 0x000001c0 }, /* AUS40CMN_DIG RCAL0 */ - { 0x00818, 0x0000f000, 0x00003000 }, /* AUS40CMN_DIG RCAL5 */ - { 0x03888, 0x000000ff, 0x00000027 }, /* CIO4PLL FREQDESCRIPTOR_0_C */ - { 0x28004, 0x00000038, 0x00000008 }, /* AUX_LDO_CTRL */ - { 0x03adc, 0x00003ff0, 0x00001760 }, /* CIO4PLL CFG_FRACN_FUNC */ - /* The lane entries, T6040_DPTX_LANE_TUNABLES of them, come last. */ - { 0x0e0cc, 0x01f80000, 0x00380000 }, /* LANE0 TX_EQ_CTRL_REG11 */ - { 0x1e0cc, 0x01f80000, 0x00380000 }, /* LANE1 TX_EQ_CTRL_REG11 */ - { 0x0d090, 0x01f80000, 0x00380000 }, /* LANE0 RX_CFG TX_EQ_CTRL_REG11 */ - { 0x1d090, 0x01f80000, 0x00380000 }, /* LANE1 RX_CFG TX_EQ_CTRL_REG11 */ -}; - -#define T6040_DPTX_LANE_TUNABLES 4 - -static void t6040_dp_fixed_tunables(void __iomem *core) -{ - size_t i; - - for (i = 0; i < ARRAY_SIZE(t6040_dptx_tunables); i++) - t6040_usb2_mask32(core + t6040_dptx_tunables[i].off, t6040_dptx_tunables[i].clear, - t6040_dptx_tunables[i].set); -} - /* aciophy_xbar_config for PHY mode 3 (DP on all four lanes). */ static void t6040_dp_xbar_config(struct t6040_usb2_phy *tphy, bool flip) { From 29a98f4783d7f26ab6d91a77666da887ab6122d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:39:01 +0100 Subject: [PATCH 261/269] usb: dwc3: apple: let the PHY decide on the PIPE reset after a sleep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a sleep in place, dwc3_apple_core_reinit() resets the PIPE only if the controller is a T6040 one. The phy_down argument is already set only when the PHY reports that it held the core in reset for the sleep, and only the T6040 USB2 PHY ever does so; the other Type-C PHY never returns that result. So the compatible check adds nothing: drop it and key the PIPE reset on what the PHY reported. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index a180898e0ac2c..7b8a6f82f7b6e 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -522,8 +522,7 @@ static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc, bool phy_down) if (ret) dev_warn(appledwc->dev, "USB3 PHY setup failed after the sleep, USB2 only, err=%d\n", ret); - else if (phy_down && appledwc->usb3_phy && - of_device_is_compatible(appledwc->dev->of_node, "apple,t6040-dwc3")) + else if (phy_down && appledwc->usb3_phy) dwc3_apple_usb3_pipe_reset(appledwc); return 0; From bae65fe8262ee9c42a115cd9c3cb54df81dbdfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:43:30 +0100 Subject: [PATCH 262/269] phy: apple, soc: apple: give the DP tunnel pixel clock a symbol of its own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/phy/apple/t6040-usb2.c, include/linux/soc/apple/dp-tunnel.h part of "phy: apple: t6040: give the DP tunnel pixel clock a symbol of its own", which goes with the USB, Type-C and USB4 changes; the rest of that commit is with the display changes. The T6040 PHY exported its tunnel pixel clock as apple_atc_dp_tunnel_rate() only when phy-apple-atc is not built. With both PHY drivers in one kernel the T6040 tunnel had no provider: the ATC driver's symbol refuses a PHY that is not its own, so the clock of a Thunderbolt DisplayPort tunnel was never started. Export apple_t6040_dp_tunnel_rate() unconditionally, as for T6050, and let the display fabric call it on T6040 and T6041 for the clock, and for the stop of a split tunnel. The rate call starts everything there, so the separate "open" step is left out on those SoCs. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 25 +++++++------------------ include/linux/soc/apple/dp-tunnel.h | 6 ++++++ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index c56edf9581135..560e57ef76b67 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2080,7 +2080,7 @@ static const struct phy_ops t6040_dp_phy_ops = { * whose select field divides it per link rate. DP IN adapter 0 is taken to * use PCLK1 and clock switch 1, adapter 1 PCLK2 and switch 2 (the PHY does * not choose: the index is the display controller's). Driven only by - * appledrm through apple_atc_dp_tunnel_rate(), and only while the PHY is in + * appledrm through apple_t6040_dp_tunnel_rate(), and only while the PHY is in * CIO. */ #define T6040_CORE_RCAL1 0x804 /* AUS40CMN_DIG RCAL1 */ @@ -2326,13 +2326,12 @@ static int t6040_tun_rate_locked(struct t6040_usb2_phy *tphy, unsigned int dpin, } /* - * The dp-tunnel.h provider for appledrm, which looks it up with symbol_get(). - * phy-apple-atc exports the same names for the older SoCs; a kernel that - * builds both gets them from there (and T6040 tunnels then need appledrm to - * pick a T6040 provider instead). + * The pixel clock of a DP tunnel for appledrm, which looks it up with symbol_get(): start the + * clock of DP IN adapter @dpin (0 or 1) at DP link rate code @rate, or stop it with 0. The + * PHY must be in CIO. A symbol of its own, as for T6050: phy-apple-atc exports + * apple_atc_dp_tunnel_rate() for the older SoCs, and both can be built into one kernel. */ -#if !IS_ENABLED(CONFIG_PHY_APPLE_ATC) -int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) +int apple_t6040_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) { struct t6040_usb2_phy *tphy; @@ -2342,17 +2341,7 @@ int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) guard(mutex)(&tphy->lock); return t6040_tun_rate_locked(tphy, dpin, rate); } -EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_rate); - -/* The rate call does all of it, as on every SoC but t600x. */ -int apple_atc_dp_tunnel_open(struct phy *phy) -{ - if (!phy || phy->ops != &t6040_dp_phy_ops) - return -EINVAL; - return -EOPNOTSUPP; -} -EXPORT_SYMBOL_GPL(apple_atc_dp_tunnel_open); -#endif +EXPORT_SYMBOL_GPL(apple_t6040_dp_tunnel_rate); /* ---- CIO: USB4/Thunderbolt (PHY mode 2) ---- * diff --git a/include/linux/soc/apple/dp-tunnel.h b/include/linux/soc/apple/dp-tunnel.h index 034a74b852ebc..6118de23d143d 100644 --- a/include/linux/soc/apple/dp-tunnel.h +++ b/include/linux/soc/apple/dp-tunnel.h @@ -59,6 +59,12 @@ int apple_dcp_tb_dp_event(struct device_node *connector_np, unsigned int dpin, */ int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate); +/* + * T6040 ATC PHY: the same for DP IN adapter @dpin (0 or 1), a symbol of its own because + * phy-apple-atc and phy-apple-t6040-usb2 can both be built. + */ +int apple_t6040_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate); + /* T6030's external DPTX port 0 sources feed DP IN0/1 through PCLK1/2. */ static inline int apple_dp_tunnel_t6030_pclk(unsigned int source, unsigned int dpin) { From f5fbd1f817b4fee83eabb00108943d55307a445c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 14:31:38 +0100 Subject: [PATCH 263/269] usb: typec: balance the module reference of an active altmode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typec_altmode_update_active() takes a reference on the module of the bound driver when a partner or plug mode becomes active and puts it back when the mode becomes inactive. A mode that the port controller activates before its driver has bound (typec_displayport is loaded by the uevent of the altmode device) takes no reference, but the put on the next deactivation still runs. The module's count then falls to zero while it is in use (lsmod shows -1), and the next activation, at the next plug of the display, fails try_module_get() and warns: WARNING: at typec_altmode_update_active+0xfc [typec] cd321x_update_work+0x32c [tps6598x_core] Remember in struct altmode whether the reference is held. Put it back only when it is held, take it when the driver binds a mode that is already active, and never take it twice. Seen on the J616s, whose Type-C controller activates the DisplayPort partner mode itself: count -1 after the first DisplayPort plug of a boot, the warning at the following replug. Signed-off-by: František Balázsy --- drivers/usb/typec/bus.c | 10 ++++++++-- drivers/usb/typec/bus.h | 1 + drivers/usb/typec/class.c | 21 +++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c index e84b134a33813..ceae6ab6584e1 100644 --- a/drivers/usb/typec/bus.c +++ b/drivers/usb/typec/bus.c @@ -535,10 +535,16 @@ static int typec_probe(struct device *dev) } ret = drv->probe(adev); - if (ret) + if (ret) { typec_altmode_remove_links(altmode); + return ret; + } + + /* A mode that was activated before this driver bound pins it now. */ + if (adev->active && !altmode->owner_held) + altmode->owner_held = try_module_get(dev->driver->owner); - return ret; + return 0; } static void typec_remove(struct device *dev) diff --git a/drivers/usb/typec/bus.h b/drivers/usb/typec/bus.h index 7df5deb1dd3a5..bae9885ab68d8 100644 --- a/drivers/usb/typec/bus.h +++ b/drivers/usb/typec/bus.h @@ -22,6 +22,7 @@ struct altmode { const struct attribute_group *groups[2]; struct altmode *partner; + bool owner_held; /* holds the driver's module */ struct altmode *plug[2]; }; diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c index 71cf604e25bc4..519637e6e09b2 100644 --- a/drivers/usb/typec/class.c +++ b/drivers/usb/typec/class.c @@ -310,11 +310,24 @@ void typec_altmode_update_active(struct typec_altmode *adev, bool active) if (adev->active == active) return; + /* + * The reference pins the driver's module while the mode is active. Only + * a driver that is bound can be pinned: a mode activated before its + * driver bound gets its reference in typec_probe(), and only a taken + * reference is put back. + */ if (!is_typec_port(adev->dev.parent) && adev->dev.driver) { - if (!active) - module_put(adev->dev.driver->owner); - else - WARN_ON(!try_module_get(adev->dev.driver->owner)); + struct altmode *alt = to_altmode(adev); + + if (!active) { + if (alt->owner_held) { + module_put(adev->dev.driver->owner); + alt->owner_held = false; + } + } else if (!alt->owner_held) { + alt->owner_held = try_module_get(adev->dev.driver->owner); + WARN_ON(!alt->owner_held); + } } adev->active = active; From 1fb1d40a3812b59acbe45a8eb7b03f50c76d5b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:43:22 +0100 Subject: [PATCH 264/269] usb: dwc3: apple: decide a role during a sleep in one place MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A role the port controller asks for while the controller is stopped for a sleep, or kept through one, was turned into the state to come back in by two helpers: one mapped the role directly, the other additionally refused device mode on a host-only port, as a running controller does. The stopped case missed the host-only rule, so a device role asked for during a sleep would have started device mode on a T6040 port after the wake, where the live path stays off. Use one helper that takes host_only for both cases and a single branch in the role switch. A host-only port now stays off after a sleep it was stopped for, as it does when running; the other cases are unchanged. Signed-off-by: František Balázsy --- drivers/usb/dwc3/dwc3-apple-state-test.c | 24 +++++++----------- drivers/usb/dwc3/dwc3-apple-state.h | 32 +++++++----------------- drivers/usb/dwc3/dwc3-apple.c | 17 +++++-------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple-state-test.c b/drivers/usb/dwc3/dwc3-apple-state-test.c index ad9e4059f79a8..cf86153b55e64 100644 --- a/drivers/usb/dwc3/dwc3-apple-state-test.c +++ b/drivers/usb/dwc3/dwc3-apple-state-test.c @@ -38,24 +38,19 @@ static void dwc3_apple_non_role_test(struct kunit *test) } } -/* A role that arrives during a sleep decides what comes back after it. */ +/* A role that arrives during a sleep decides what comes back; a host-only port has no device. */ static void dwc3_apple_role_during_sleep_test(struct kunit *test) { - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_HOST), DWC3_APPLE_HOST); - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_DEVICE), DWC3_APPLE_DEVICE); - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_NONE), DWC3_APPLE_NO_CABLE); -} - -/* A host kept through a sleep is neither stopped nor started for a role: no device mode. */ -static void dwc3_apple_role_while_kept_test(struct kunit *test) -{ - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_HOST, true), DWC3_APPLE_HOST); - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_NONE, true), + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_HOST, false), DWC3_APPLE_HOST); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_DEVICE, false), + DWC3_APPLE_DEVICE); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_NONE, false), DWC3_APPLE_NO_CABLE); - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_DEVICE, true), + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_HOST, true), DWC3_APPLE_HOST); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_NONE, true), + DWC3_APPLE_NO_CABLE); + KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role(USB_ROLE_DEVICE, true), DWC3_APPLE_NO_CABLE); - KUNIT_EXPECT_EQ(test, dwc3_apple_state_for_role_kept(USB_ROLE_DEVICE, false), - DWC3_APPLE_DEVICE); } /* A fixed hub's host may always be kept; a Type-C port's only by a PHY that restarts it. */ @@ -136,7 +131,6 @@ static struct kunit_case dwc3_apple_state_test_cases[] = { KUNIT_CASE(dwc3_apple_device_selected_before_reset_test), KUNIT_CASE(dwc3_apple_non_role_test), KUNIT_CASE(dwc3_apple_role_during_sleep_test), - KUNIT_CASE(dwc3_apple_role_while_kept_test), KUNIT_CASE(dwc3_apple_sleep_may_keep_test), KUNIT_CASE(dwc3_apple_sleep_reads_mark_test), KUNIT_CASE(dwc3_apple_wake_hibernated_test), diff --git a/drivers/usb/dwc3/dwc3-apple-state.h b/drivers/usb/dwc3/dwc3-apple-state.h index 266d9c477bff9..c0f0840916fe5 100644 --- a/drivers/usb/dwc3/dwc3-apple-state.h +++ b/drivers/usb/dwc3/dwc3-apple-state.h @@ -51,42 +51,28 @@ static inline int dwc3_apple_usb2_mode_before_reset(enum dwc3_apple_state target } /** - * dwc3_apple_state_for_role() - state a role asks for while the core is stopped for sleep + * dwc3_apple_state_for_role() - state a role asks for while a sleep holds the core * @role: the role asked for + * @host_only: the controller has no device mode * - * Return: the state to bring the controller up in after the sleep. + * Nothing is stopped or started while the sleep lasts, so what dwc3_usb_role_switch_set() + * decides for a running controller is decided here: no device mode on a host-only port. + * + * Return: the state to bring the controller up in once the sleep is over. */ -static inline enum dwc3_apple_state dwc3_apple_state_for_role(enum usb_role role) +static inline enum dwc3_apple_state dwc3_apple_state_for_role(enum usb_role role, + bool host_only) { switch (role) { case USB_ROLE_HOST: return DWC3_APPLE_HOST; case USB_ROLE_DEVICE: - return DWC3_APPLE_DEVICE; + return host_only ? DWC3_APPLE_NO_CABLE : DWC3_APPLE_DEVICE; default: return DWC3_APPLE_NO_CABLE; } } -/** - * dwc3_apple_state_for_role_kept() - the same for a host kept in place through the sleep - * @role: the role asked for - * @host_only: the controller has no device mode - * - * Nothing is stopped or started while the sleep lasts, so what dwc3_usb_role_switch_set() - * decides for a running controller is decided here: no device mode on a host-only port. - * - * Return: the state the port goes to once the sleep is over. - */ -static inline enum dwc3_apple_state dwc3_apple_state_for_role_kept(enum usb_role role, - bool host_only) -{ - if (role == USB_ROLE_DEVICE && host_only) - return DWC3_APPLE_NO_CABLE; - - return dwc3_apple_state_for_role(role); -} - /** * dwc3_apple_sleep_may_keep() - whether a deep sleep may find the host kept in place * @fixed_hub: the controller serves a fixed hub diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 7b8a6f82f7b6e..c89293aaf482d 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -1198,19 +1198,14 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro if (appledwc->fixed_hub) return 0; - /* Stopped for a sleep: this is what comes back after it. */ - if (appledwc->sleep_stopped) { - appledwc->sleep_state = dwc3_apple_state_for_role(role); - return 0; - } - /* - * Kept through a sleep, with the core in reset for some of it: nothing stops or starts - * until the sleep is over, see dwc3_apple_sleep_restart_end(). Nor while the core may - * be unpowered after one: dwc3_apple_recover() starts what is asked for here. + * Held by a sleep: this is what comes back after it. Stopped for it, or kept through it + * with the core in reset for some of it, nothing stops or starts until the sleep is + * over, see dwc3_apple_sleep_restart_end(). Nor while the core may be unpowered after + * one: dwc3_apple_recover() starts what is asked for here. */ - if (appledwc->sleep_in_place || appledwc->sleep_dead) { - appledwc->sleep_state = dwc3_apple_state_for_role_kept(role, appledwc->host_only); + if (appledwc->sleep_stopped || appledwc->sleep_in_place || appledwc->sleep_dead) { + appledwc->sleep_state = dwc3_apple_state_for_role(role, appledwc->host_only); return 0; } From 1197ec84360f4c6290baffba7d8aed195f601d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:44:11 +0100 Subject: [PATCH 265/269] phy: apple: pick the PHY sleep provider by its ops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The controller's glue asked the ATC PHY driver first and the T6040 PHY driver second, each call chained on -EOPNOTSUPP, and every provider needed an inline stub for a kernel that does not build it. Both drivers now export the ops of their USB2 PHY, and the glue's helpers choose the provider by comparing the PHY's ops, as the providers do when they look their own state up. A PHY of neither driver gets the answers it got from the chain: -EOPNOTSUPP where an answer is needed, nothing to do otherwise. The stubs go, since a provider that is not built is never selected. Signed-off-by: František Balázsy --- drivers/phy/apple/atc.c | 3 +- drivers/phy/apple/t6040-usb2.c | 7 +- include/linux/phy/apple-atc.h | 122 ++++++++++++--------------------- 3 files changed, 49 insertions(+), 83 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index 414ce695f37a2..a8beddfe9b8fa 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -4584,11 +4584,12 @@ static int atcphy_rpm_usb2_set_mode(struct phy *phy, enum phy_mode mode, int sub return ret; } -static const struct phy_ops apple_atc_usb2_phy_ops = { +const struct phy_ops apple_atc_usb2_phy_ops = { .owner = THIS_MODULE, .init = atcphy_rpm_usb2_init, .set_mode = atcphy_rpm_usb2_set_mode, }; +EXPORT_SYMBOL_GPL(apple_atc_usb2_phy_ops); static int atcphy_usb3_power_off(struct phy *phy) { diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 560e57ef76b67..652ebc64f6f62 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -635,12 +635,13 @@ static int t6040_usb2_power_off(struct phy *phy) return 0; } -static const struct phy_ops t6040_usb2_phy_ops = { +const struct phy_ops apple_t6040_usb2_phy_ops = { .owner = THIS_MODULE, .set_mode = t6040_usb2_set_mode, .power_on = t6040_usb2_power_on, .power_off = t6040_usb2_power_off, }; +EXPORT_SYMBOL_GPL(apple_t6040_usb2_phy_ops); /* ---- USB3 ---- */ @@ -3309,7 +3310,7 @@ static int t6040_usb2_probe(struct platform_device *pdev) if (ret) return ret; - tphy->phy = devm_phy_create(dev, NULL, &t6040_usb2_phy_ops); + tphy->phy = devm_phy_create(dev, NULL, &apple_t6040_usb2_phy_ops); if (IS_ERR(tphy->phy)) return PTR_ERR(tphy->phy); phy_set_drvdata(tphy->phy, tphy); @@ -3384,7 +3385,7 @@ static int t6040_usb2_probe(struct platform_device *pdev) static struct t6040_usb2_phy *t6040_from_usb2_phy(struct phy *phy) { - if (!phy || phy->ops != &t6040_usb2_phy_ops) + if (!phy || phy->ops != &apple_t6040_usb2_phy_ops) return NULL; return phy_get_drvdata(phy); } diff --git a/include/linux/phy/apple-atc.h b/include/linux/phy/apple-atc.h index d16b6d6266e66..2605d43bc6904 100644 --- a/include/linux/phy/apple-atc.h +++ b/include/linux/phy/apple-atc.h @@ -8,8 +8,7 @@ #include #include - -struct phy; +#include /* In D0 after the sleep, but its always-on side lost power, so xHCI's saved state is gone. */ #define APPLE_ATCPHY_SLEEP_POWER_LOST 1 @@ -22,7 +21,8 @@ struct phy; */ #define APPLE_ATCPHY_SLEEP_RESET 2 -#if IS_REACHABLE(CONFIG_PHY_APPLE_ATC) +extern const struct phy_ops apple_atc_usb2_phy_ops; + /* * For a firmware-driven system sleep, before any device suspends and without the * controller's locks held. 0 means the PHY takes the controller through hibernation after @@ -57,38 +57,12 @@ void apple_atcphy_sleep_finish_async(struct phy *usb2_phy, bool kept); void apple_atcphy_sleep_finish_flush(struct phy *usb2_phy); /* ACIO owns this transaction; @phy is its shared USB3 lane PHY handle. */ +#if IS_REACHABLE(CONFIG_PHY_APPLE_ATC) int apple_atcphy_cio_prepare(struct phy *phy); int apple_atcphy_cio_off(struct phy *phy); int apple_atcphy_cio_on(struct phy *phy); int apple_atcphy_cio_finish(struct phy *phy); #else -static inline int apple_atcphy_sleep_prepare(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline int apple_atcphy_sleep_result(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline int apple_atcphy_sleep_recover(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline void apple_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) -{ -} - -static inline void apple_atcphy_sleep_finish_async(struct phy *usb2_phy, bool kept) -{ -} - -static inline void apple_atcphy_sleep_finish_flush(struct phy *usb2_phy) -{ -} - static inline int apple_atcphy_cio_prepare(struct phy *phy) { return -EOPNOTSUPP; @@ -108,10 +82,10 @@ static inline int apple_atcphy_cio_finish(struct phy *phy) { return -EOPNOTSUPP; } - #endif -#if IS_REACHABLE(CONFIG_PHY_APPLE_T6040_USB2) +extern const struct phy_ops apple_t6040_usb2_phy_ops; + /* * The T6040 Type-C PHY's side of the same sleep, for a Type-C port in host mode with nothing * but USB on its lanes. It has no hibernation to take the controller through: it takes its @@ -158,93 +132,83 @@ int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy); * stop for sleeps until it has started again. */ void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept); -#else -static inline int apple_t6040_atcphy_sleep_prepare(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline int apple_t6040_atcphy_sleep_enter(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline int apple_t6040_atcphy_sleep_result(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} - -static inline int apple_t6040_atcphy_sleep_power_up(struct phy *usb2_phy) -{ - return -EOPNOTSUPP; -} -static inline int apple_t6040_atcphy_sleep_recover(struct phy *usb2_phy) +/* Whether @usb2_phy is the USB2 PHY of a driver that is built for this kernel. */ +static inline bool apple_atcphy_is_atc(struct phy *usb2_phy) { - return -EOPNOTSUPP; + return IS_REACHABLE(CONFIG_PHY_APPLE_ATC) && usb2_phy && + usb2_phy->ops == &apple_atc_usb2_phy_ops; } -static inline void apple_t6040_atcphy_sleep_finish(struct phy *usb2_phy, bool kept) +static inline bool apple_atcphy_is_t6040(struct phy *usb2_phy) { + return IS_REACHABLE(CONFIG_PHY_APPLE_T6040_USB2) && usb2_phy && + usb2_phy->ops == &apple_t6040_usb2_phy_ops; } -#endif /* - * What the controller's glue calls: the driver that @usb2_phy belongs to answers, and the - * other one doesn't know the PHY. A step that only one of them has is nothing to do for the - * other's PHYs. + * What the controller's glue calls: the driver that @usb2_phy belongs to, found by the PHY's + * ops, answers. A step that only one of the drivers has is nothing to do for the other's PHYs, + * and for any other PHY. */ static inline int apple_typec_phy_sleep_prepare(struct phy *usb2_phy) { - int ret = apple_atcphy_sleep_prepare(usb2_phy); - - return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_prepare(usb2_phy) : ret; + if (apple_atcphy_is_atc(usb2_phy)) + return apple_atcphy_sleep_prepare(usb2_phy); + if (apple_atcphy_is_t6040(usb2_phy)) + return apple_t6040_atcphy_sleep_prepare(usb2_phy); + return -EOPNOTSUPP; } static inline int apple_typec_phy_sleep_enter(struct phy *usb2_phy) { - int ret = apple_t6040_atcphy_sleep_enter(usb2_phy); - - return ret == -EOPNOTSUPP ? 0 : ret; + return apple_atcphy_is_t6040(usb2_phy) ? apple_t6040_atcphy_sleep_enter(usb2_phy) : 0; } static inline int apple_typec_phy_sleep_result(struct phy *usb2_phy) { - int ret = apple_atcphy_sleep_result(usb2_phy); - - return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_result(usb2_phy) : ret; + if (apple_atcphy_is_atc(usb2_phy)) + return apple_atcphy_sleep_result(usb2_phy); + if (apple_atcphy_is_t6040(usb2_phy)) + return apple_t6040_atcphy_sleep_result(usb2_phy); + return -EOPNOTSUPP; } static inline int apple_typec_phy_sleep_power_up(struct phy *usb2_phy) { - int ret = apple_t6040_atcphy_sleep_power_up(usb2_phy); - - return ret == -EOPNOTSUPP ? 0 : ret; + return apple_atcphy_is_t6040(usb2_phy) ? apple_t6040_atcphy_sleep_power_up(usb2_phy) : 0; } static inline int apple_typec_phy_sleep_recover(struct phy *usb2_phy) { - int ret = apple_atcphy_sleep_recover(usb2_phy); - - return ret == -EOPNOTSUPP ? apple_t6040_atcphy_sleep_recover(usb2_phy) : ret; + if (apple_atcphy_is_atc(usb2_phy)) + return apple_atcphy_sleep_recover(usb2_phy); + if (apple_atcphy_is_t6040(usb2_phy)) + return apple_t6040_atcphy_sleep_recover(usb2_phy); + return -EOPNOTSUPP; } static inline void apple_typec_phy_sleep_finish(struct phy *usb2_phy, bool kept) { - apple_atcphy_sleep_finish(usb2_phy, kept); - apple_t6040_atcphy_sleep_finish(usb2_phy, kept); + if (apple_atcphy_is_atc(usb2_phy)) + apple_atcphy_sleep_finish(usb2_phy, kept); + else if (apple_atcphy_is_t6040(usb2_phy)) + apple_t6040_atcphy_sleep_finish(usb2_phy, kept); } /* The T6040 PHY's finish has nothing to wait for, so it ends at once. */ static inline void apple_typec_phy_sleep_finish_async(struct phy *usb2_phy, bool kept) { - apple_atcphy_sleep_finish_async(usb2_phy, kept); - apple_t6040_atcphy_sleep_finish(usb2_phy, kept); + if (apple_atcphy_is_atc(usb2_phy)) + apple_atcphy_sleep_finish_async(usb2_phy, kept); + else if (apple_atcphy_is_t6040(usb2_phy)) + apple_t6040_atcphy_sleep_finish(usb2_phy, kept); } static inline void apple_typec_phy_sleep_finish_flush(struct phy *usb2_phy) { - apple_atcphy_sleep_finish_flush(usb2_phy); + if (apple_atcphy_is_atc(usb2_phy)) + apple_atcphy_sleep_finish_flush(usb2_phy); } #endif From 0998448f98038f0401488cb8b49ff7e1dd701293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:44:37 +0100 Subject: [PATCH 266/269] usb: typec: tipd: share the start of the application firmware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The probe and the restore after a resume from disk both put the controller into system power state S0, check that application firmware runs and load the patch if the controller waits for one. The restore repeated the probe's sequence step by step. Move the sequence into one helper that both call. The order of the steps, their error returns and what the probe does with the mode it found are unchanged. Signed-off-by: František Balázsy --- drivers/usb/typec/tipd/core.c | 63 +++++++++++-------- .../drivers/gpu/apple-dcp/transaction.py | 2 +- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index b8536120fdf09..a7ff2155365d6 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -3306,6 +3306,41 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) return 0; } +/* + * The controller's system power state S0, and application firmware running: the patch is + * loaded if the controller waits for one (@patched, if given, says so). What a probe and a + * resume from disk both start from. + * + * Return: the controller's mode after the check (0 once a patch was loaded), or a negative + * error code. + */ +static int tipd_start_application(struct tps6598x *tps, bool *patched) +{ + int ret; + + if (patched) + *patched = false; + if (tps->data->switch_power_state) { + ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); + if (ret) + return ret; + } + + ret = tps6598x_check_mode(tps); + if (ret < 0) + return ret; + + if (ret == TPS_MODE_PTCH) { + if (patched) + *patched = true; + ret = tps->data->init(tps); + if (ret) + return ret; + } + + return ret; +} + int tipd_init(struct tps6598x *tps) { struct fwnode_handle *fwnode; @@ -3332,24 +3367,10 @@ int tipd_init(struct tps6598x *tps) } } - if (tps->data->switch_power_state) { - ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); - if (ret) - return ret; - } - - /* Make sure the controller has application firmware running */ - ret = tps6598x_check_mode(tps); + ret = tipd_start_application(tps, &patch_loaded); if (ret < 0) return ret; - if (ret == TPS_MODE_PTCH) { - patch_loaded = true; - ret = tps->data->init(tps); - if (ret) - return ret; - } - /* * A controller found in "DBMa" is used as it is. Nothing below issues a * mode-changing 4CC command (the S0 power state switch above only does @@ -3683,19 +3704,9 @@ int tipd_reinit_after_cold_boot(struct tps6598x *tps) { int ret; - if (tps->data->switch_power_state) { - ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); - if (ret) - return ret; - } - ret = tps6598x_check_mode(tps); + ret = tipd_start_application(tps, NULL); if (ret < 0) return ret; - if (ret == TPS_MODE_PTCH) { - ret = tps->data->init(tps); - if (ret) - return ret; - } ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, cd321x_irq_mask(tps)); if (ret) return ret; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py index f70e13245e35d..eae4f541e4dc1 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py @@ -415,7 +415,7 @@ def probe(root): """ helpers += '\n#define of_machine_is_compatible(name) (!strcmp((name), \"apple,t8140\"))\n#define BIT_ULL(n) (1ULL << (n))\n#define SN201202X_INT_DP_SID_UPDATE BIT_ULL(37)\n' helpers += block(core, 'static bool cd321x_uses_dp_sid(') + '\n' + block(core, 'static u64 cd321x_irq_mask(') + '\n' - unit += helpers + block(core, 'int tipd_init(') + r""" + unit += helpers + block(core, 'static int tipd_start_application(') + '\n' + block(core, 'int tipd_init(') + r""" static void probe_initial(struct cd321x *c) { initial(c);ordinary_port=true;phy_provider.safe_after_role=true;c->tps.irq=1;data.register_port=register_port;data.read_data_status=read_data_status;data.read_power_status=tps6598x_read_power_status;data.connect=connect; connect_error=irq_error=register_error=node_puts=mux_get_error=0; From e000e5a41eb8d3ff66a18f43a80727237a8dcca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:46:36 +0100 Subject: [PATCH 267/269] thunderbolt: apple: reach the Type-C PHY through struct phy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The router called the T6040 Type-C PHY through four exported functions that took the PHY's device node, found the platform device by that node on every call and kept the driver bound with the device lock. Nothing ordered the router's probe or power management after the PHY. Get the PHY at probe with devm_phy_get() from the router's phys property and pass the struct phy to the same four calls, as the USB controller does for its sleep steps. The PHY framework links the router to the PHY as its consumer, and a router whose PHY is not bound yet defers its probe instead of failing its first start. The PHY side checks that the PHY is a T6040 Type-C PHY by its ops, takes its lock and runs the same steps. The check for an enabled router in the PHY driver reads the same property. Not run on a USB4/Thunderbolt device. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 50 ++++++++++------------------- drivers/thunderbolt/apple.c | 29 ++++++++--------- include/linux/phy/apple-t6040-cio.h | 29 +++++++++-------- 3 files changed, 46 insertions(+), 62 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 652ebc64f6f62..c5d0f6bd22e9d 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -2641,7 +2641,7 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) /* * A USB4/Thunderbolt mux event only takes the port into CIO while the router - * that runs over this PHY (a node whose apple,atc-phy points here) is enabled + * that runs over this PHY (a node whose phys property names it) is enabled * in the live device tree. The default device trees keep the routers * disabled, so the port stays on its USB2/USB3/DP paths until a router is * enabled. @@ -2651,7 +2651,7 @@ static bool t6040_cio_router_enabled(struct t6040_usb2_phy *tphy) struct device_node *np; for_each_compatible_node(np, NULL, "apple,t6040-usb4-acio") { - struct device_node *phy = of_parse_phandle(np, "apple,atc-phy", 0); + struct device_node *phy = of_parse_phandle(np, "phys", 0); bool match = phy == tphy->dev->of_node && of_device_is_available(np); of_node_put(phy); @@ -3917,34 +3917,18 @@ static struct platform_driver t6040_usb2_driver = { /* ---- the USB4 router's view of the PHY (linux/phy/apple-t6040-cio.h) ---- */ -/* - * Run @fn on the bound PHY of @phy_np with its lock held. The device lock - * keeps the driver bound for the call. - */ -static int t6040_cio_call(struct device_node *phy_np, +/* Run @fn on the PHY behind @phy with its lock held. */ +static int t6040_cio_call(struct phy *phy, int (*fn)(struct t6040_usb2_phy *tphy, unsigned long arg), unsigned long arg) { - struct platform_device *pdev; - struct t6040_usb2_phy *tphy; - int ret; + struct t6040_usb2_phy *tphy = t6040_from_usb2_phy(phy); - if (!phy_np || !of_device_is_compatible(phy_np, "apple,t6040-atcphy")) + if (!tphy) return -ENODEV; - pdev = of_find_device_by_node(phy_np); - if (!pdev) - return -EPROBE_DEFER; - device_lock(&pdev->dev); - tphy = platform_get_drvdata(pdev); - if (pdev->dev.driver != &t6040_usb2_driver.driver || !tphy) { - ret = -EPROBE_DEFER; - } else { - guard(mutex)(&tphy->lock); - ret = fn(tphy, arg); - } - device_unlock(&pdev->dev); - put_device(&pdev->dev); - return ret; + + guard(mutex)(&tphy->lock); + return fn(tphy, arg); } static int t6040_cio_state_fn(struct t6040_usb2_phy *tphy, unsigned long arg) @@ -3980,27 +3964,27 @@ static int t6040_cio_usb3_fn(struct t6040_usb2_phy *tphy, unsigned long on) return 0; } -int apple_t6040_cio_state(struct device_node *phy_np) +int apple_t6040_cio_state(struct phy *phy) { - return t6040_cio_call(phy_np, t6040_cio_state_fn, 0); + return t6040_cio_call(phy, t6040_cio_state_fn, 0); } EXPORT_SYMBOL_GPL(apple_t6040_cio_state); -int apple_t6040_cio_router_started(struct device_node *phy_np) +int apple_t6040_cio_router_started(struct phy *phy) { - return t6040_cio_call(phy_np, t6040_cio_router_fn, 1); + return t6040_cio_call(phy, t6040_cio_router_fn, 1); } EXPORT_SYMBOL_GPL(apple_t6040_cio_router_started); -void apple_t6040_cio_router_stopped(struct device_node *phy_np) +void apple_t6040_cio_router_stopped(struct phy *phy) { - t6040_cio_call(phy_np, t6040_cio_router_fn, 0); + t6040_cio_call(phy, t6040_cio_router_fn, 0); } EXPORT_SYMBOL_GPL(apple_t6040_cio_router_stopped); -int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on) +int apple_t6040_cio_usb3_tunnel(struct phy *phy, bool on) { - return t6040_cio_call(phy_np, t6040_cio_usb3_fn, on); + return t6040_cio_call(phy, t6040_cio_usb3_fn, on); } EXPORT_SYMBOL_GPL(apple_t6040_cio_usb3_tunnel); diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 3f75b0e16e93c..ec4414f3ca762 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -96,6 +96,7 @@ #include #include #include +#include #include #include #include @@ -175,7 +176,7 @@ struct apple_cio_hw { /* Leave the host router's TMU to its firmware; host TMU constants. */ bool host_tmu; /* - * The router runs only over its Type-C PHY in CIO mode ("apple,atc-phy") + * The router runs only over its Type-C PHY in CIO mode ("phys") * and tells the PHY when it starts and stops. */ bool cio_phy; @@ -408,7 +409,7 @@ enum apple_cio_sleep_stop { * @sleep_stop_work: Stops a port with a PCIe-C host for system sleep * @sleep_stop_done: Signals that @sleep_stop_work has finished * @sleep_stop_state: Where @sleep_stop_work stands for the PM notifier - * @phy_np: Type-C PHY the router runs over (hw->cio_phy) + * @phy: Type-C PHY the router runs over (hw->cio_phy) */ struct apple_cio { struct device *dev; @@ -479,7 +480,7 @@ struct apple_cio { struct completion sleep_stop_done; atomic_t sleep_stop_state; /* enum apple_cio_sleep_stop */ /* hw->cio_phy: the Type-C PHY this router runs over */ - struct device_node *phy_np; + struct phy *phy; u32 current_cable_info; u32 target_cable_info; @@ -2465,7 +2466,7 @@ static int apple_nhi_usb3_tunnel_changed(struct tb_nhi *nhi, bool on) { struct apple_cio *acio = nhi_to_anhi(nhi)->acio; - return apple_t6040_cio_usb3_tunnel(acio->phy_np, on); + return apple_t6040_cio_usb3_tunnel(acio->phy, on); } static const struct tb_nhi_ring_layout apple_nhi_ring_layout = { @@ -3149,7 +3150,7 @@ static void apple_cio_stop(struct apple_cio *acio) * one: give the USB controller its PIPE back before the router goes. */ if (acio->hw->cio_phy) - apple_t6040_cio_usb3_tunnel(acio->phy_np, false); + apple_t6040_cio_usb3_tunnel(acio->phy, false); apple_cio_remove_children(acio); /* Try to shut down and power off the co-processor gracefully */ @@ -3187,7 +3188,7 @@ static void apple_cio_stop(struct apple_cio *acio) acio->link_lost_restart = false; /* ACIO is off: the PHY may leave CIO now (unplug, removal, sleep). */ if (acio->hw->cio_phy) - apple_t6040_cio_router_stopped(acio->phy_np); + apple_t6040_cio_router_stopped(acio->phy); } /* @@ -3474,20 +3475,20 @@ static int apple_cio_start(struct apple_cio *acio) * The PHY must be in CIO, tuned by its firmware, before ACIO powers up. * The Type-C controller sets the mux first and retries on its next event. */ - ret = apple_t6040_cio_state(acio->phy_np); + ret = apple_t6040_cio_state(acio->phy); if (ret != 1) { ret = ret < 0 ? ret : -EAGAIN; dev_dbg(acio->dev, "Type-C PHY is not in CIO mode (%d), not starting\n", ret); return ret; } - ret = apple_t6040_cio_router_started(acio->phy_np); + ret = apple_t6040_cio_router_started(acio->phy); if (ret) { dev_err(acio->dev, "Type-C PHY refused the router: %d\n", ret); return ret; } ret = apple_cio_power_up(acio); if (ret) - apple_t6040_cio_router_stopped(acio->phy_np); + apple_t6040_cio_router_stopped(acio->phy); return ret; } @@ -4154,12 +4155,10 @@ static int apple_cio_probe(struct platform_device *pdev) return -EINVAL; if (acio->hw->cio_phy) { - acio->phy_np = of_parse_phandle(acio->np, "apple,atc-phy", 0); - if (!acio->phy_np) - return dev_err_probe(dev, -EINVAL, "Missing apple,atc-phy\n"); - ret = devm_add_action_or_reset(dev, apple_cio_of_node_put, acio->phy_np); - if (ret) - return ret; + acio->phy = devm_phy_get(dev, "cio"); + if (IS_ERR(acio->phy)) + return dev_err_probe(dev, PTR_ERR(acio->phy), + "Missing or unbound Type-C PHY (phys, \"cio\")\n"); } acio->type7 = acio->hw->type7; diff --git a/include/linux/phy/apple-t6040-cio.h b/include/linux/phy/apple-t6040-cio.h index c632243ae4fa9..d041a3a3db9f8 100644 --- a/include/linux/phy/apple-t6040-cio.h +++ b/include/linux/phy/apple-t6040-cio.h @@ -5,8 +5,9 @@ * through the mux before it hands the cable to the router, and takes it out * after the router has stopped. The router uses these calls to check that * order and to tell the PHY whether it runs, which the PHY needs before it - * takes CIO down for system sleep. @phy_np is the PHY node the router's - * "apple,atc-phy" property points to. + * takes CIO down for system sleep. @phy is the PHY the router's "phys" + * property names, got with devm_phy_get(): the PHY core links the router to it + * as its consumer, so the PHY is bound before the router probes. */ #ifndef __LINUX_PHY_APPLE_T6040_CIO_H #define __LINUX_PHY_APPLE_T6040_CIO_H @@ -15,50 +16,50 @@ #include #include -struct device_node; +struct phy; #if IS_REACHABLE(CONFIG_PHY_APPLE_T6040_USB2) /* - * 1: CIO up and tuned by the PHY firmware; 0: not in CIO; -EPROBE_DEFER: - * the PHY is not bound yet; -ENODEV: @phy_np is not a T6040 Type-C PHY. + * 1: CIO up and tuned by the PHY firmware; 0: not in CIO; -ENODEV: @phy is + * not a T6040 Type-C PHY. */ -int apple_t6040_cio_state(struct device_node *phy_np); +int apple_t6040_cio_state(struct phy *phy); /* * Before the router starts: marks it running on the PHY's CIO link. - * -EAGAIN unless CIO is up; -EPROBE_DEFER / -ENODEV as above. + * -EAGAIN unless CIO is up; -ENODEV as above. */ -int apple_t6040_cio_router_started(struct device_node *phy_np); +int apple_t6040_cio_router_started(struct phy *phy); /* * After every router stop, once ACIO is powered down (unplug, error, and the * stop for system sleep, which must come before any device's .suspend, * .freeze or .poweroff). Undoes apple_t6040_cio_router_started(). */ -void apple_t6040_cio_router_stopped(struct device_node *phy_np); +void apple_t6040_cio_router_stopped(struct phy *phy); /* * The router carries (@on) or no longer carries USB3 for the port's USB * controller: the controller's PIPE follows. -EAGAIN when @on and the router * is not marked running. */ -int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on); +int apple_t6040_cio_usb3_tunnel(struct phy *phy, bool on); #else -static inline int apple_t6040_cio_state(struct device_node *phy_np) +static inline int apple_t6040_cio_state(struct phy *phy) { return -ENODEV; } -static inline int apple_t6040_cio_router_started(struct device_node *phy_np) +static inline int apple_t6040_cio_router_started(struct phy *phy) { return -ENODEV; } -static inline void apple_t6040_cio_router_stopped(struct device_node *phy_np) +static inline void apple_t6040_cio_router_stopped(struct phy *phy) { } -static inline int apple_t6040_cio_usb3_tunnel(struct device_node *phy_np, bool on) +static inline int apple_t6040_cio_usb3_tunnel(struct phy *phy, bool on) { return -ENODEV; } From 1ed2752f4ddd8106bf0a166218ca08e62744c304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 15:58:52 +0100 Subject: [PATCH 268/269] phy: apple, usb: typec: drop the restore callbacks' checks for an abandoned hibernation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drivers/phy/apple/t6040-usb2.c, drivers/usb/typec/tipd/spmi.c part of "soc: apple: drop the restore callbacks' checks for an abandoned hibernation", which goes with the USB, Type-C and USB4 changes; the rest of that commit is with the sleep controller changes. An abandoned hibernation resumed its devices with PMSG_RESTORE, so every restore callback that must not act on a state it never lost began with a pm_hibernate_is_restored() check, and most forwarded to their thaw. The PM core now sends PMSG_RECOVER there, which reaches the thaw callbacks, so the check and the forward go from macsmc, the RTKit helper, apple-dart, the sn201202x Type-C controller, the SPMI, GPIO and NVMe restore paths, both PCIe restore callbacks, the DCP and AOP restores, the t6040 USB2 PHY's two and the RTC; cs42l84's restore passes that the codec lost its state. Each callback runs for a restored kernel exactly as before. For an abandoned hibernation the old path was already the thaw or nothing, and the thaw now runs on its own: macsmc, the RTKit helper, apple-dart, sn201202x, NVMe and cs42l84 forwarded to the same function (cs42l84's resume asks pm_resume_via_firmware(), which hibernation_snapshot() has cleared); PCIe's restore_noirq did for a bridge without hibernation tunnelling what its thaw_noirq does, and the same call with it; the AOP, t6040, RTC, GPIO and SPMI restores did nothing and have no thaw. DCP keeps its power-off-failed branch (PMSG_RESTORE after a failed platform power-off, which only a platform with hibernation ops reaches) and loses the abandoned one, whose only other effect was a log line. One path changes: macsmc's thaw, after the SMC was handed over and the image could not be entered, calls apple_smc_restore() to start a new session, and its own check sent that back to the thaw, which did nothing. It now starts the session as the comment says it should. The flag keeps its other users, which are not restore callbacks: the AIC syscore resume, the dockchannel receive interrupt, the SEP's PM notifier. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 5 ----- drivers/usb/typec/tipd/spmi.c | 3 --- 2 files changed, 8 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index c5d0f6bd22e9d..308aab16591c2 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -3731,9 +3731,6 @@ static int t6040_usb2_restore_early(struct device *dev) if (!tphy) return 0; - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return 0; guard(mutex)(&tphy->lock); if (tphy->powered || tphy->usb3_on || tphy->dwc3_running || t6040_phy_count(tphy->dp)) @@ -3780,8 +3777,6 @@ static int t6040_usb2_restore(struct device *dev) if (!tphy || IS_ERR_OR_NULL(tphy->rtk)) return 0; - if (!pm_hibernate_is_restored()) - return 0; guard(mutex)(&tphy->lock); if (!apple_rtkit_is_running(tphy->rtk)) return 0; diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index c252cea7789f0..cb730e11e614a 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -1055,9 +1055,6 @@ static int sn201202x_restore(struct device *dev) int err; const char *step; - /* Hibernation aborted before the image (PMSG_RESTORE then too): nothing restarted. */ - if (!pm_hibernate_is_restored()) - return sn201202x_resume(dev); /* * An earlier restore left it for a new probe: freeze took no hold * and left its interrupt alone, so there is nothing to give back. From 346a417f780b35d7cd3f6001ca401cce5bbbf755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bal=C3=A1zsy?= Date: Fri, 13 Nov 2026 16:25:13 +0100 Subject: [PATCH 269/269] phy: apple: t6040: give DP IN 1 of a tunnel its PCLK1 prerequisite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DP IN 1 of a Thunderbolt tunnel runs on PCLK2, and on T6030 and T8132 its video stops unless PCLK1 runs as well, at the same rate when DP IN 0 does not use it. The T6040 tunnel clock enabled PCLK2 alone for DP IN 1, so the second stream of a two-stream display (a tiled one) got no video. Enable PCLK1 whenever either adapter runs, and write DP IN 1's rate into the PCLK1 select while DP IN 0 is off. Hold clock switch 1 for it, and release the switch and the enable when DP IN 1 stops, or hand PCLK1 over to DP IN 1's rate when DP IN 0 stops first. A stop of the whole tunnel releases the held switch. Not run on a Thunderbolt display; this follows the two SoCs where the requirement was measured. Signed-off-by: František Balázsy --- drivers/phy/apple/t6040-usb2.c | 51 +++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 308aab16591c2..08c36aed331d3 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -368,6 +368,7 @@ struct t6040_usb2_phy { bool cio_up; /* PHY in mode 2, holds a runtime PM reference */ bool pipe_tunneled; /* the controller's PIPE runs on the USB4 router */ u8 tun_rate[2]; /* DP tunnel link rate per DP IN adapter, 0 = off */ + bool tun_pre; /* switch 1 is held for DP IN 1 alone */ bool tun_pll; /* AUSPLL programmed for DP tunnels */ bool router_running; /* the USB4 router runs on this port's CIO link */ bool usb3_tunnel_req; /* the router carries USB3 for the controller */ @@ -2128,9 +2129,10 @@ static int t6040_tun_select(u8 rate) } } +/* DP IN 1 runs on PCLK2 and needs PCLK1 as well: without it its video stops. */ static u32 t6040_tun_enables(struct t6040_usb2_phy *tphy) { - return (tphy->tun_rate[0] ? t6040_tun_pclk[0].en : 0) | + return ((tphy->tun_rate[0] || tphy->tun_rate[1]) ? t6040_tun_pclk[0].en : 0) | (tphy->tun_rate[1] ? t6040_tun_pclk[1].en : 0); } @@ -2220,18 +2222,23 @@ static int t6040_tun_pll_start(struct t6040_usb2_phy *tphy) return 0; } +static void t6040_tun_select_field(u32 *v, const struct t6040_tun_pclk *p, int sel, bool en) +{ + if (!!(*v & p->en) != en || ((*v >> p->sel_shift) & 0xf) != sel) + *v = (*v & ~(p->en | (0xf << p->sel_shift))) | (sel << p->sel_shift); +} + /* The DP 1.4 tunnel update for one pclk: select with its enable off, then the enables. */ static void t6040_tun_select_write(struct t6040_usb2_phy *tphy, unsigned int dpin, int sel) { - const struct t6040_tun_pclk *p = &t6040_tun_pclk[dpin]; void __iomem *ctrl0 = t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0; u32 v = readl(ctrl0); - if (!!(v & p->en) != !!tphy->tun_rate[dpin] || - ((v >> p->sel_shift) & 0xf) != sel) { - v = (v & ~(p->en | (0xf << p->sel_shift))) | (sel << p->sel_shift); - writel(v, ctrl0); - } + t6040_tun_select_field(&v, &t6040_tun_pclk[dpin], sel, !!tphy->tun_rate[dpin]); + /* PCLK1 is DP IN 1's prerequisite at its own rate, unless DP IN 0 runs on it. */ + if (dpin == 1 && !tphy->tun_rate[0]) + t6040_tun_select_field(&v, &t6040_tun_pclk[0], sel, true); + writel(v, ctrl0); writel((v & ~T6040_CORE_TX_DP_CTRL0_ENABLES) | t6040_tun_enables(tphy), ctrl0); } @@ -2276,6 +2283,10 @@ static void t6040_tun_stop_all_locked(struct t6040_usb2_phy *tphy) tphy->tun_rate[dpin] = 0; t6040_tun_switch(tphy, dpin, false); } + if (tphy->tun_pre) { + tphy->tun_pre = false; + t6040_tun_switch(tphy, 0, false); + } t6040_tun_pll_stop(tphy); dev_info(tphy->dev, "DP tunnel clocks off\n"); } @@ -2290,9 +2301,19 @@ static int t6040_tun_rate_locked(struct t6040_usb2_phy *tphy, unsigned int dpin, if (!old) return 0; tphy->tun_rate[dpin] = 0; - t6040_tun_switch(tphy, dpin, false); - t6040_usb2_clear32(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0, - t6040_tun_pclk[dpin].en); + if (dpin == 0 && tphy->tun_rate[1]) { + /* DP IN 1 keeps PCLK1 and its switch, now at its own rate. */ + tphy->tun_pre = true; + t6040_tun_select_write(tphy, 1, t6040_tun_select(tphy->tun_rate[1])); + } else { + t6040_tun_switch(tphy, dpin, false); + if (dpin == 1 && tphy->tun_pre) { + tphy->tun_pre = false; + t6040_tun_switch(tphy, 0, false); + } + } + t6040_usb2_mask32(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0, + T6040_CORE_TX_DP_CTRL0_ENABLES, t6040_tun_enables(tphy)); if (!tphy->tun_rate[!dpin]) t6040_tun_pll_stop(tphy); dev_dbg(tphy->dev, "DP tunnel %u off\n", dpin); @@ -2312,7 +2333,17 @@ static int t6040_tun_rate_locked(struct t6040_usb2_phy *tphy, unsigned int dpin, } tphy->tun_rate[dpin] = rate; t6040_tun_select_write(tphy, dpin, sel); + /* DP IN 1 alone: switch 1 passes PCLK1, its prerequisite. */ + if (dpin == 1 && !tphy->tun_rate[0] && !tphy->tun_pre) { + ret = t6040_tun_switch(tphy, 0, true); + if (ret) + goto switch_failed; + tphy->tun_pre = true; + } else if (dpin == 0) { + tphy->tun_pre = false; /* DP IN 0 owns switch 1 again */ + } ret = t6040_tun_switch(tphy, dpin, true); +switch_failed: if (ret) { dev_err(tphy->dev, "DP tunnel %u: clock switch timed out (ctrl %#x status %#x)\n", dpin, readl(t6040_cfg(tphy) + T6040_CFG_DP_CLKSWITCH_CTRL),