diff --git a/Documentation/devicetree/bindings/pci/apple,pcie.yaml b/Documentation/devicetree/bindings/pci/apple,pcie.yaml index a9fd4822fd127..06405f7179fe8 100644 --- a/Documentation/devicetree/bindings/pci/apple,pcie.yaml +++ b/Documentation/devicetree/bindings/pci/apple,pcie.yaml @@ -101,6 +101,7 @@ properties: - const: apple,t8103-pciec - const: apple,t6000-pciec - const: apple,t6050-pciec + - const: apple,t8142-pciec - items: - const: apple,t8132-pciec - const: apple,t8122-pciec @@ -374,6 +375,7 @@ allOf: contains: enum: - apple,t8132-pciec + - apple,t8142-pciec - apple,t6040-pciec then: required: @@ -501,6 +503,7 @@ allOf: - apple,t6000-pciec - apple,t6040-pciec - apple,t6050-pciec + - apple,t8142-pciec then: properties: reg: diff --git a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml index 01592811223d2..29c64e3b3c33e 100644 --- a/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml +++ b/Documentation/devicetree/bindings/thunderbolt/apple,t8103-usb4-acio.yaml @@ -52,8 +52,8 @@ properties: PCIe adapter configuration space of the USB4 router. Present where the root complex exposes the adapter separately, so that the tunnel can be configured before a PCIe-C link is brought up. - - description: MxWrap co-processor control registers, on T8132 and T6050 - - description: PCIe router AP firmware registers, on T6050 + - description: MxWrap co-processor control registers, on T8132, T8142 and T6050 + - description: PCIe router AP firmware registers, on T8142 and T6050 reg-names: oneOf: @@ -253,6 +253,7 @@ allOf: enum: - apple,t6040-usb4-acio - apple,t8132-usb4-acio + - apple,t8142-usb4-acio then: properties: reg: @@ -267,6 +268,7 @@ allOf: - apple,t6040-usb4-acio - apple,t6050-usb4-acio - apple,t8132-usb4-acio + - apple,t8142-usb4-acio then: properties: pinctrl-names: @@ -309,7 +311,28 @@ allOf: properties: power-domains: maxItems: 4 + - if: + properties: + compatible: + not: + contains: + enum: + - apple,t6050-usb4-acio + - apple,t8142-usb4-acio + then: + properties: apple,tunable-router-ap-fw: false + - if: + properties: + compatible: + contains: + const: apple,t8142-usb4-acio + then: + properties: + reg: + minItems: 5 + reg-names: + minItems: 5 additionalProperties: false diff --git a/arch/arm64/boot/dts/apple/t8142-j813-usb4.dtsi b/arch/arm64/boot/dts/apple/t8142-j813-usb4.dtsi new file mode 100644 index 0000000000000..a0107a4613eed --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8142-j813-usb4.dtsi @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* J813 rear USB-C port: the ACIO1 router and its PCIe-C tunnel. */ + +/ { + aliases { + + usb4-1-acio = &acio1; + + usb4-1-pcie-adapter = &acio1; + + usb4-1-nhi = &acio1_nhi; + + /* aura copies the PCIe-C tunables here (needs aura#302). */ + usb4-1-pcie = &apciec1; + }; +}; + +&pmgr { + + ps_atc1_pcie: power-controller@3d8 { + compatible = "apple,t8142-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x3d8 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + power-domains = <&ps_atc1_common>, <&ps_atc1_usb_aon>; + label = "atc1-pcie"; + }; + + ps_atc1_cio: power-controller@3e0 { + compatible = "apple,t8142-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x3e0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + power-domains = <&ps_atc1_common>; + label = "atc1-cio"; + }; + + ps_atc1_cio_pcie: power-controller@4e8 { + compatible = "apple,t8142-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x4e8 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + power-domains = <&ps_atc1_cio>; + label = "atc1-cio-pcie"; + }; + + ps_atc1_cio_usb: power-controller@4f0 { + compatible = "apple,t8142-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x4f0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + power-domains = <&ps_atc1_cio>; + label = "atc1-cio-usb"; + }; +}; + +/* Populate these DARTs only after their parent is powered and configured. */ +/delete-node/ &acio1_dart; +/delete-node/ &pciec1_dart; + +&{/soc} { + + cio_reset: reset-controller@380780000 { + compatible = "apple,t8142-cio-reset"; + reg = <0x3 0x80780000 0x0 0x4000>; + #reset-cells = <1>; + }; + + pinctrl_aop: pinctrl@390824000 { + compatible = "apple,t8142-pinctrl", "apple,t8140-pinctrl", "apple,t8103-pinctrl"; + reg = <0x3 0x90824000 0x0 0x4000>; + gpio-controller; + #gpio-cells = <2>; + gpio-ranges = <&pinctrl_aop 0 0 77>; + apple,npins = <77>; + + /* Only LSTX pin 21 is ours; the AOP owns the rest of this bank. */ + gpio-reserved-ranges = <0 21>, <22 55>; + interrupt-controller; + #interrupt-cells = <2>; + interrupts = , + , + , + ; + apple,irq-group-base = <3>; + + acio1_lstx_pins: acio1-lstx-pins { + pinmux = <0x10015>; + }; + }; + + acio1_mbox: mailbox@409108000 { + compatible = "apple,t8142-asc-mailbox", "apple,asc-mailbox-v4"; + reg = <0x4 0x09108000 0x0 0x4000>; + interrupts = , + , + , + ; + interrupt-names = "send-empty", "send-not-empty", + "recv-empty", "recv-not-empty"; + #mbox-cells = <0>; + }; + + /* acio@ avoids the bootloader's legacy cio@ register-index fixup. */ + acio1: acio@409ac0000 { + compatible = "apple,t8142-usb4-acio"; + reg = <0x4 0x09ac0000 0x0 0xc000>, + <0x4 0x09080000 0x0 0x26000>, + <0x4 0x09e44000 0x0 0x4000>, + <0x4 0x09100000 0x0 0x4000>, + <0x4 0x09c80000 0x0 0x4000>; + reg-names = "rc", "sram", "pcie-adapter", "cpu", "router-ap-fw"; + mboxes = <&acio1_mbox>; + resets = <&cio_reset 1>; + + power-domains = <&ps_atc1_cio>, <&ps_atc1_cio_pcie>, + <&ps_atc1_cio_usb>, <&ps_atc1_pcie>; + thunderbolt-switch; + apple,pcie-tunnel = <&acio1_pcie_tunnel>; + + pinctrl-0 = <&acio1_lstx_pins>; + pinctrl-names = "lstx"; + lstx-gpios = <&pinctrl_aop 21 GPIO_ACTIVE_HIGH>; + + /* Other router tunables are supplied by the bootloader. */ + apple,tunable-router-ap-fw = <0x8 0xff2 0x482>; + + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x4 0x09000000 0x1000000>; + nonposted-mmio; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@1 { + reg = <1>; + acio1_tbt: endpoint { + remote-endpoint = <&typec1_usb4>; + }; + }; + }; + + acio1_dart: iommu@a80000 { + compatible = "apple,t8142-dart", "apple,t8110-dart"; + reg = <0xa80000 0xc000>; + interrupts = ; + power-domains = <&ps_atc1_cio>; + #iommu-cells = <1>; + apple,dma-range = <0x100 0x4000 0x0 0xffff0000>; + apple,bypass-sids = <15>; + apple,full-error-reporting; + apple,ctc-flush; + + /* ACIO DART tunables are not supplied by the bootloader. */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0xf0f 0xf0f>, + <0x224 0xffff 0x909>, + <0x300 0x1f31 0x1>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x800 0xf003f 0x10000>, + <0x804 0xf003f 0x10002>, + <0x808 0xf003f 0x10004>, + <0x80c 0xf003f 0x20006>, + <0x810 0xf003f 0x2000a>, + <0x814 0xf003f 0x2000e>, + <0x818 0xf003f 0x20012>, + <0x81c 0xf003f 0x20016>, + <0x820 0xf003f 0x2001a>, + <0x824 0xf003f 0x2001e>, + <0x828 0xf003f 0x20022>, + <0x82c 0xf003f 0x20026>, + <0x830 0xf003f 0x2002a>, + <0x834 0xf003f 0x2002e>, + <0x838 0xf003f 0x20032>, + <0x83c 0xf003f 0x10036>; + }; + + acio1_nhi: nhi@f00000 { + compatible = "apple,t8142-usb4-nhi"; + reg = <0xf00000 0x100000>; + reg-names = "nhi"; + interrupts = , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + , + ; + interrupt-names = "txring0", "txring1", "txring2", "txring3", + "txring4", "txring5", "txring6", "txring7", + "txring8", "txring9", "txring10", "txring11", + "rxring0", "rxring1", "rxring2", "rxring3", + "rxring4", "rxring5", "rxring6", "rxring7", + "rxring8", "rxring9", "rxring10", "rxring11"; + + iommus = <&acio1_dart 0>, <&acio1_dart 1>, <&acio1_dart 2>, + <&acio1_dart 3>, <&acio1_dart 4>, <&acio1_dart 5>, + <&acio1_dart 6>, <&acio1_dart 7>, <&acio1_dart 8>, + <&acio1_dart 9>, <&acio1_dart 10>, <&acio1_dart 11>, + <&acio1_dart 16>, <&acio1_dart 17>, <&acio1_dart 18>, + <&acio1_dart 19>, <&acio1_dart 20>, <&acio1_dart 21>, + <&acio1_dart 22>, <&acio1_dart 23>, <&acio1_dart 24>, + <&acio1_dart 25>, <&acio1_dart 26>, <&acio1_dart 27>; + + /* Filled by the bootloader. */ + apple,thunderbolt-drom = [00]; + }; + }; +}; + +/ { + acio1_pcie_tunnel: pcie-tunnel-1 { + #address-cells = <2>; + #size-cells = <2>; + ranges; + nonposted-mmio; + + pciec1_dart: iommu@40c000000 { + compatible = "apple,t8142-dart", "apple,t8110-dart"; + reg = <0x4 0x0c000000 0x0 0xc000>; + interrupts = ; + power-domains = <&ps_atc1_pcie>; + #iommu-cells = <1>; + apple,bypass-sids = <253 255>; + apple,full-error-reporting; + apple,ctc-flush; + }; + + apciec1: pcie@1c60000000 { + compatible = "apple,t8142-pciec"; + device_type = "pci"; + reg = <0x1c 0x60000000 0x0 0x10000000>, + <0x4 0x0c034000 0x0 0x4000>, + <0x4 0x0c020000 0x0 0x8000>, + <0x4 0x0c038000 0x0 0x4000>, + <0x4 0x0c060000 0x0 0x4000>, + <0x4 0x0c02c000 0x0 0x4000>, + <0x4 0x0c028000 0x0 0x4000>; + reg-names = "config", "rc", "port0", "fabric", "debug", "ltssm", "intr2axi"; + interrupts = ; + power-domains = <&ps_atc1_pcie>; + + apple,pciec-kernel-init; + + msi-controller; + msi-parent = <&apciec1>; + msi-ranges = <&aic AIC_IRQ 1869 IRQ_TYPE_EDGE_RISING 32>; + apple,msi-vector-base = <128>; + + iommu-map = <0x0 &pciec1_dart 0x0 0x40>; + iommu-map-mask = <0x3f>; + + linux,pci-domain = <2>; + + bus-range = <0x0 0x7f>; + #address-cells = <3>; + #size-cells = <2>; + + ranges = <0x43000000 0xc 0x0 0xc 0x0 0x2 0x0>, + <0x02000000 0x0 0x00100000 0xe 0x00100000 0x0 0x3ff00000>, + <0x42000000 0x0 0x40000000 0xe 0x40000000 0x0 0x40000000>; + + apciec1_port: pci@0,0 { + device_type = "pci"; + reg = <0x0 0x0 0x0 0x0 0x0>; + external-facing; + + #address-cells = <3>; + #size-cells = <2>; + ranges; + + interrupt-controller; + #interrupt-cells = <1>; + interrupt-map-mask = <0 0 0 7>; + interrupt-map = <0 0 0 1 &apciec1_port 0 0 0 0>, + <0 0 0 2 &apciec1_port 0 0 0 1>, + <0 0 0 3 &apciec1_port 0 0 0 2>, + <0 0 0 4 &apciec1_port 0 0 0 3>; + }; + }; + }; +}; + +/* Port 3 is reserved for the board's DisplayPort alternate-mode route. */ +&typec1 { + ports { + port@4 { + reg = <4>; + typec1_usb4: endpoint { + remote-endpoint = <&acio1_tbt>; + }; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8142-j813.dts b/arch/arm64/boot/dts/apple/t8142-j813.dts index fe9e3b8ce77db..4b3ce1a391ce1 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813.dts +++ b/arch/arm64/boot/dts/apple/t8142-j813.dts @@ -405,3 +405,5 @@ "shared-l2", "gfx-data", "gfx1-data", "fw-data", "fw-text"; }; + +#include "t8142-j813-usb4.dtsi" diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 8e20a36cc575e..35f984f4eace5 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -473,6 +473,22 @@ static const struct hw_info t6050_pciec_hw = { .mps_timers = true, }; +/* T8142 shares this register layout, with its own three-cell AIC MSI slice. */ +static const struct hw_info t8142_pciec_hw = { + .port_msiaddr = PORT_MSIADDR, + .port_msiaddr_hi = PORT_MSIADDR + 4, + .port_perst = PORT_PERST, + .port_rid2sid = PORT_RID2SID, + .port_msimap = PORT_T602X_MSIMAP, + .port_tunctrl = PORT_T6050_TUNCTRL, + .max_rid2sid = 256, + .tunneled = true, + .tunnel_intr2axi = true, + .no_port_reset = true, + .msimap_by_vector = true, + .mps_timers = true, +}; + static const struct of_device_id apple_pcie_of_match[]; /** @@ -3384,8 +3400,25 @@ static int apple_msi_init(struct apple_pcie *pcie) * 32-vector slice. Keep endpoint MSI data local, and route its table * entry to the corresponding shared-window vector. */ - if (pcie->hw == &t8122_pciec_hw || pcie->hw == &t8132_pciec_hw || - pcie->hw == &t6040_pciec_hw) { + if (pcie->hw == &t8132_pciec_hw || pcie->hw == &t8142_pciec_hw) { + /* + * msi-ranges names the AIC input of global vector + * apple,msi-vector-base in a linear window, so the slice is + * checked by its shape only: an edge-rising SPI, 32 vectors, + * 32-aligned, above the internal port's vectors and inside the + * 8-bit map target. + */ + if (args.args_count != 3 || args.args[0] != 0 || + args.args[2] != IRQ_TYPE_EDGE_RISING || + of_property_read_u32(to_of_node(fwnode), + "apple,msi-vector-base", + &pcie->msi_vector_base) || + args.args[1] < pcie->msi_vector_base || + !apple_t6030_msi_slice_valid(pcie->msi_vector_base, + pcie->nvecs, args.args[1], + args.args[1] - pcie->msi_vector_base)) + goto invalid_slice; + } else if (pcie->hw == &t8122_pciec_hw || pcie->hw == &t6040_pciec_hw) { struct resource ecam; u32 expected_base, aic_base; @@ -3422,19 +3455,6 @@ static int apple_msi_init(struct apple_pcie *pcie) default: goto invalid_slice; } - } else if (of_machine_is_compatible("apple,t8132")) { - aic_base = 1506; - switch (ecam.start) { - /* The internal port's bridges own vectors 0..95. */ - case 0x1c50000000ULL: - expected_base = 0x60; - break; - case 0x1c60000000ULL: - expected_base = 0x80; - break; - default: - goto invalid_slice; - } } else { goto invalid_slice; } @@ -3468,7 +3488,7 @@ static int apple_msi_init(struct apple_pcie *pcie) /* Keep map targets within the supported 8-bit range. */ if ((pcie->hw == &t8122_pciec_hw || pcie->hw == &t8132_pciec_hw || - pcie->hw == &t6040_pciec_hw) && + pcie->hw == &t6040_pciec_hw || pcie->hw == &t8142_pciec_hw) && (!pcie->nvecs || pcie->nvecs > 256 || pcie->msi_vector_base > FIELD_MAX(PORT_MSIMAP_TARGET) + 1 - pcie->nvecs)) { @@ -7484,6 +7504,7 @@ static const struct dev_pm_ops apple_pcie_pm_ops = { static const struct of_device_id apple_pcie_of_match[] = { { .compatible = "apple,t6040-pciec", .data = &t6040_pciec_hw }, { .compatible = "apple,t6050-pciec", .data = &t6050_pciec_hw }, + { .compatible = "apple,t8142-pciec", .data = &t8142_pciec_hw }, { .compatible = "apple,t8132-pciec", .data = &t8132_pciec_hw }, { .compatible = "apple,t8122-pciec", .data = &t8122_pciec_hw }, { .compatible = "apple,t8112-pciec", .data = &t8112_pciec_hw }, diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index ac8e1a4205098..013f259b35c16 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -171,6 +171,15 @@ struct apple_cio_hw { bool gen4_link_recovery; /* Leave the host router's TMU to its firmware; host TMU constants. */ bool host_tmu; + /* Deep sleep removes the CIO domains, including an attached router. */ + bool sleep_removes_domains; + /* Start the router again at once after a wake. */ + bool restart_on_wake; + /* + * The PHY is shared with DWC3: release PCIe, or refuse the sleep, + * before DWC3's default-priority PM notifier queues its PHY shutdown. + */ + bool phy_shared_with_dwc3; const struct apple_dpin_policy *dp; }; @@ -206,9 +215,22 @@ static const struct apple_cio_hw apple_cio_t8132_hw = { .dp = &apple_dpin_m4, }; -/* Type 7 uses its existing ready-state check and has no qualified DP route. */ +/* + * T8142 has a Type 7 router with an MxWrap CPU already running after CIO + * reset. Hand LSTX to firmware only after RTKit is ready. DP IN is not yet + * qualified; the connector's DisplayPort alternate mode is independent. + */ static const struct apple_cio_hw apple_cio_t8142_hw = { + .mxwrap = true, .type7 = true, + .lstx_explicit = true, + .skip_run = true, + .lstx_after_boot = true, + .strict_fw_ready = true, + .no_host_clx = true, + .sleep_removes_domains = true, + .restart_on_wake = true, + .phy_shared_with_dwc3 = true, .dp = &apple_dpin_disabled, }; @@ -1308,7 +1330,8 @@ static bool apple_cio_pciec_eligible(const struct device_node *np) return of_device_is_compatible(np, "apple,t6040-pciec") || of_device_is_compatible(np, "apple,t8103-pciec") || of_device_is_compatible(np, "apple,t6000-pciec") || - of_device_is_compatible(np, "apple,t6050-pciec"); + of_device_is_compatible(np, "apple,t6050-pciec") || + of_device_is_compatible(np, "apple,t8142-pciec"); } static bool @@ -3994,6 +4017,12 @@ static bool apple_cio_t6050_sleep(struct apple_cio *acio) apple_sleep_v27_late() && !pm_suspend_default_s2idle(); } +/* Deep sleep removes the CIO domains, including an attached router. */ +static bool apple_cio_sleep_removes_domains(struct apple_cio *acio) +{ + return acio->hw->sleep_removes_domains && !pm_suspend_default_s2idle(); +} + /* The longest the displays behind a port may take to go before its router does. */ #define APPLE_CIO_SLEEP_DP_RELEASE_MS 3000 @@ -4096,7 +4125,8 @@ static int apple_cio_sleep_teardown(struct apple_cio *acio) scoped_guard(mutex, &acio->pcie_tunnel_lock) { if (READ_ONCE(acio->pcie_tunnel_requested) || acio->pcie_tunnel_populated) { struct platform_device *pcie; - int mode = READ_ONCE(t6050_sleep_pcie); + bool t8142 = apple_cio_sleep_removes_domains(acio); + int mode = t8142 ? 1 : READ_ONCE(t6050_sleep_pcie); int ret = 0; /* @@ -4105,7 +4135,7 @@ static int apple_cio_sleep_teardown(struct apple_cio *acio) * still carries them: only NVMe controllers, whose driver touches no * notifier chain on removal, and by default only with no disk in use. */ - if (!mode || !apple_cio_t6050_sleep(acio)) + if (!mode || (!t8142 && !apple_cio_t6050_sleep(acio))) return 0; pcie = apple_cio_find_pcie_tunnel(acio); if (pcie) { @@ -4116,7 +4146,8 @@ static int apple_cio_sleep_teardown(struct apple_cio *acio) dev_warn(acio->dev, "PCIe devices behind this port stay for the sleep (%d): the port is not powered off\n", ret); - return 0; + /* T8142 cannot retain this port through deep sleep. */ + return t8142 ? ret : 0; } } acio->pcie_tunnel_stopping = true; @@ -4125,7 +4156,7 @@ static int apple_cio_sleep_teardown(struct apple_cio *acio) dev_info(acio->dev, "powering off port for system sleep (cable info 0x%x)\n", acio->current_cable_info); t6050 = apple_cio_t6050_sleep(acio); - if (t6050) { + if (t6050 || apple_cio_sleep_removes_domains(acio)) { u64 activity; /* With no NHI the answer is "yes": then nothing is expected back. */ @@ -4193,7 +4224,7 @@ static void apple_cio_sleep_restart_work(struct work_struct *work) /* Is this port powered off for the sleep about to start? Asked at PM_SUSPEND_PREPARE. */ static bool apple_cio_sleep_tears_down(struct apple_cio *acio) { - if (READ_ONCE(s2idle_teardown)) + if (READ_ONCE(s2idle_teardown) || apple_cio_sleep_removes_domains(acio)) return true; /* A port whose router sleeps in place keeps its tunnels: its NHI owns the sleep. */ return apple_cio_t6050_sleep(acio) && !READ_ONCE(acio->sleep_in_place); @@ -4220,7 +4251,7 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, apple_cio_sleep_close_owed(acio); /* Before tasks freeze: tearing down the NHI needs unfrozen work. */ if (tears_down) { - /* Only the displays' release refuses, with the port as it was. */ + /* Refuse before powering off a port that cannot be released. */ ret = apple_cio_sleep_teardown(acio); if (ret) return notifier_from_errno(ret); @@ -4235,6 +4266,8 @@ static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, } owed = acio->sleep_restart_owed; early = acio->sleep_aborted_early; + if (acio->hw->restart_on_wake) + delay = 0; /* TEST: macOS starts the router again straight after the wake. */ if (acio->sleep_t6050) { acio->sleep_t6050 = false; @@ -4714,6 +4747,9 @@ static int apple_cio_probe(struct platform_device *pdev) } acio->pm_notifier.notifier_call = apple_cio_pm_notify; + /* Before DWC3's default-priority notifier queues its PHY shutdown. */ + if (acio->hw->phy_shared_with_dwc3) + acio->pm_notifier.priority = 40; register_pm_notifier(&acio->pm_notifier); /* Before the notifiers of the default priority, after the port controllers' (30). */ acio->pm_notifier_dp.notifier_call = apple_cio_dp_pm_notify; diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index a5a8fb96e143d..9441119a26e93 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -100,10 +100,11 @@ def function(source, name): int main(void) { struct device_node dart = { .tunables = true }; - const char *intr2axi[] = { "apple,t8112-pciec", "apple,t6000-pciec" }; - for (unsigned int i = 0; i < 2; i++) { + const char *intr2axi[] = { "apple,t8112-pciec", "apple,t6000-pciec", + "apple,t8142-pciec" }; + for (unsigned int i = 0; i < 3; i++) { /* M2 must win over its T8103 fallback, without a board allowlist. */ - struct device_node host = { .compat = { intr2axi[i], "apple,t8103-pciec" }, + struct device_node host = { .compat = { intr2axi[i], i < 2 ? "apple,t8103-pciec" : NULL }, .status = -1, .dart = &dart }; const struct of_device_id *id = of_match_node(apple_pcie_of_match, &host); assert(id && ((const struct hw_info *)id->data)->tunnel_intr2axi); @@ -132,12 +133,112 @@ def function(source, name): expect(®ular, false, false); regular.compat[0] = "apple,unknown-pciec"; expect(®ular, false, false); - puts("PASS: M1, M1 Pro, M2, firmware handoff, missing DART and DT opt-in"); + puts("PASS: M1, M1 Pro, M2, native T8142, firmware handoff, missing DART and DT opt-in"); return 0; } ''' +def pm_registration(source, start, end): + first = source.index(start) + return source[first:source.index(end, first) + len(end)] + + +PM_FIXTURE = r''' +#include +#include +#include +struct notifier_block { int (*notifier_call)(void); int priority; }; +struct apple_cio_hw { bool phy_shared_with_dwc3; }; +struct apple_cio { const struct apple_cio_hw *hw; struct notifier_block pm_notifier; }; +struct dwc3_apple { struct notifier_block pm_nb; }; +static const struct apple_cio_hw apple_cio_t8142_hw = { .phy_shared_with_dwc3 = true }; +static struct notifier_block *chain[2]; +static int count; +static bool busy, phy_off, pci_off; +static int register_pm_notifier(struct notifier_block *nb) +{ + int i = count++; + assert(count <= 2); + while (i && chain[i - 1]->priority < nb->priority) { + chain[i] = chain[i - 1]; + i--; + } + chain[i] = nb; + return 0; +} +static int apple_cio_pm_notify(void) +{ + /* An open disk must be rejected while its PCIe link still works. */ + assert(!phy_off); + if (busy) + return 1; + pci_off = true; + return 0; +} +static int dwc3_apple_pm_notify(void) +{ + assert(pci_off); + phy_off = true; + return 0; +} +static void add_cio(struct apple_cio *acio) +{ + /* ACIO_REGISTRATION */ +} +static void add_dwc(struct dwc3_apple *appledwc) +{ + int ret; + /* DWC_REGISTRATION */ + assert(!ret); +} +int main(void) +{ + struct apple_cio acio = { .hw = &apple_cio_t8142_hw }; + struct dwc3_apple dwc = { 0 }; + /* DWC3 probes first on J813. Equal priority reproduces the failure. */ + add_dwc(&dwc); + add_cio(&acio); + for (int open = 0; open < 2; open++) { + busy = open; + phy_off = pci_off = false; + for (int i = 0; i < count; i++) + if (chain[i]->notifier_call()) + break; + assert(phy_off == !busy && pci_off == !busy); + } + puts("PASS: T8142 refuses an open disk before PHY shutdown; idle PCIe stops first"); + return 0; +} +''' + + +def test_pm_order(out, acio): + dwc = (ROOT / 'drivers/usb/dwc3/dwc3-apple.c').read_text() + code = PM_FIXTURE.replace('/* ACIO_REGISTRATION */', pm_registration( + acio, 'acio->pm_notifier.notifier_call = apple_cio_pm_notify;', + 'register_pm_notifier(&acio->pm_notifier);')) + code = code.replace('/* DWC_REGISTRATION */', pm_registration( + dwc, 'appledwc->pm_nb.notifier_call = dwc3_apple_pm_notify;', + 'ret = register_pm_notifier(&appledwc->pm_nb);')) + mutation = code.replace('acio->pm_notifier.priority = 40;', + 'acio->pm_notifier.priority = 0;') + assert mutation != code + for name, body in [('pm-order', code), ('phy-before-pcie', mutation)]: + source, binary = out / (name + '.c'), out / name + source.write_text(body) + subprocess.run(['cc', '-std=gnu11', '-Wall', '-Werror', str(source), + '-o', str(binary)], check=True) + run = subprocess.run([str(binary)], capture_output=True, text=True) + (out / (name + '.log')).write_text(run.stdout + run.stderr) + if name == 'pm-order': + assert run.returncode == 0, run.stderr + print(run.stdout, end='') + else: + assert run.returncode < 0 and 'Assertion' in run.stderr + print('PASS negative control:', name) + + def main(): parser = argparse.ArgumentParser() parser.add_argument('--out', type=Path) @@ -155,8 +256,10 @@ def main(): with tempfile.TemporaryDirectory(prefix='pciec-policy-') as tmp: out = args.out or Path(tmp) out.mkdir(parents=True, exist_ok=True) + test_pm_order(out, acio) cases = {'production': code, 'm2-fallback': code.replace('{ .compatible = "apple,t8112-pciec",\t.data = &t8112_pciec_hw },', ''), + 't8142-admission': code.replace('of_device_is_compatible(np, \"apple,t8142-pciec\")', 'false'), 'dart-bypass': code.replace('tunables = of_property_present(iommu_spec.np, "apple,tunable");', 'tunables = true;')} for name, body in cases.items(): source = out / (name + '.c')