diff --git a/Documentation/devicetree/bindings/display/apple,t8140-display-power.yaml b/Documentation/devicetree/bindings/display/apple,t8140-display-power.yaml index c5ec6e05e1631..0cd143f781dce 100644 --- a/Documentation/devicetree/bindings/display/apple,t8140-display-power.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8140-display-power.yaml @@ -28,6 +28,9 @@ properties: - items: - const: apple,t6040-display-power - const: apple,t6041-display-power + - items: + - const: apple,t8112-display-power + - const: apple,t8103-display-power reg: minItems: 1 @@ -116,7 +119,8 @@ allOf: - if: properties: compatible: - const: apple,t8103-display-power + contains: + const: apple,t8103-display-power then: # T8103's display has no retention words and its PMC no count of the coprocessors. properties: diff --git a/Documentation/devicetree/bindings/gpu/apple,agx.yaml b/Documentation/devicetree/bindings/gpu/apple,agx.yaml index efa0ee56dda01..fb9657fe07f4e 100644 --- a/Documentation/devicetree/bindings/gpu/apple,agx.yaml +++ b/Documentation/devicetree/bindings/gpu/apple,agx.yaml @@ -228,6 +228,22 @@ properties: Relative frequency of the populated A ladder above the base state, in percent, followed by zeros for the five unused selectors. + apple,pwr-filter-time-constant: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Time constant of the GPU power controller's input filter, from the ADT + + apple,pwr-integral-gain: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Integral gain of the GPU power controller, an IEEE 754 single from the ADT + + apple,pwr-integral-min-clamp: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Lower clamp of the GPU power controller's integral term, from the ADT + + apple,pwr-proportional-gain: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Proportional gain of the GPU power controller, an IEEE 754 single from the ADT + apple,core-leak-coef: $ref: /schemas/types.yaml#/definitions/uint32-array description: diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index d83c4e312f20e..de3fb04a87331 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml @@ -20,6 +20,7 @@ properties: compatible: enum: - apple,t8103-sleep + - apple,t8112-sleep - apple,t8122-sleep - apple,t8132-sleep - apple,t8140-sleep @@ -129,6 +130,37 @@ allOf: - const: cpu-start - const: wdt - const: spmi + - if: + properties: + compatible: + contains: + const: apple,t8112-sleep + then: + required: + - apple,pinctrl + properties: + reg: + minItems: 17 + maxItems: 17 + reg-names: + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: spmi + - const: wake + - const: ps-aon + - const: clk-mux + - const: mcc-ctl + - const: mcc-a + - const: mcc-b + - const: mcc-c + - const: mcc-d + - const: mcc-e - if: properties: compatible: diff --git a/arch/arm64/boot/dts/apple/t8112-j413.dts b/arch/arm64/boot/dts/apple/t8112-j413.dts index dfbb28b89d3bf..2088e124c1ffb 100644 --- a/arch/arm64/boot/dts/apple/t8112-j413.dts +++ b/arch/arm64/boot/dts/apple/t8112-j413.dts @@ -596,3 +596,8 @@ &hpm1 { apple,sleep-ssps; }; + +/* The SoC's wake sources plus the MTP's (keyboard and trackpad). */ +&sleep { + apple,wake-enable = <14 26 27 44 51 53 54 58 69 73 81 86 147 154 155 156 157 229 231 248>; +}; diff --git a/arch/arm64/boot/dts/apple/t8112.dtsi b/arch/arm64/boot/dts/apple/t8112.dtsi index b5c6bdc54f8c8..08c346ada3b17 100644 --- a/arch/arm64/boot/dts/apple/t8112.dtsi +++ b/arch/arm64/boot/dts/apple/t8112.dtsi @@ -606,7 +606,11 @@ apple,ppm-filter-time-constant-ms = <34>; apple,ppm-ki = <205.0>; apple,ppm-kp = <0.75>; + apple,pwr-filter-time-constant = <313>; + apple,pwr-integral-gain = <0.0202129>; + apple,pwr-integral-min-clamp = <0>; apple,pwr-min-duty-cycle = <40>; + apple,pwr-proportional-gain = <5.2831855>; apple,core-leak-coef = <1920.0>; apple,sram-leak-coef = <74.0>; }; @@ -792,6 +796,7 @@ dcp: dcp@231c00000 { compatible = "apple,t8112-dcp", "apple,dcp"; + apple,display-power = <&dcp_power>; mboxes = <&dcp_mbox>; mbox-names = "mbox"; iommus = <&dcp_dart 5 0x8 0x00000000 0x7 0xffff0000>; @@ -1311,6 +1316,47 @@ /* child nodes are added in t8103-pmgr.dtsi */ }; + dcp_power: display-power@23b700000 { + compatible = "apple,t8112-display-power", "apple,t8103-display-power"; + reg = <0x2 0x3b700000 0x0 0x14000>; + reg-names = "ps"; + }; + + sleep: sleep@23b700000 { + compatible = "apple,t8112-sleep"; + reg = <0x2 0x3b700000 0x0 0x14000>, + <0x2 0x3b724000 0x0 0x4000>, + <0x2 0x3b734000 0x0 0x10>, + <0x2 0x3b760000 0x0 0x4000>, + <0x2 0x3b780000 0x0 0x8>, + <0x2 0x3d2d4000 0x0 0x4>, + <0x2 0x3d2b0000 0x0 0x30>, + <0x2 0x3d714000 0x0 0x100>, + <0x2 0x3d004000 0x0 0x48>, + <0x2 0x3d280000 0x0 0x4000>, + <0x2 0x3b000000 0x0 0x70000>, + <0x2 0x0001c000 0x0 0x4000>, + <0x2 0x00024000 0x0 0x1000>, + <0x2 0x00064000 0x0 0x1000>, + <0x2 0x000a4000 0x0 0x1000>, + <0x2 0x000e4000 0x0 0x1000>, + <0x2 0x01000000 0x0 0x1000>; + reg-names = "ps", "pwrgate", "cpu-start", "cllt", + "cluster", "commit", "wdt", "spmi", + "wake", "ps-aon", "clk-mux", + "mcc-ctl", "mcc-a", "mcc-b", "mcc-c", + "mcc-d", "mcc-e"; + apple,display-coprocessors = <&dcp>; + apple,pinctrl = <&pinctrl_ap>; + /* + * The SoC's wake controller sources every T8112 board has: the AOP, NUB + * (PMU RTC alarm 58), SMC and both ATC ports. Boards with an MTP add its + * sources (229, 231, 248). + */ + apple,wake-enable = <14 26 27 44 51 53 54 58 69 73 81 86 147 + 154 155 156 157>; + }; + cio_reset: reset-controller@23b790000 { compatible = "apple,t8112-cio-reset", "apple,t8103-cio-reset"; reg = <0x2 0x3b790000 0x0 0x8000>; @@ -1870,6 +1916,7 @@ mboxes = <&sep_mbox>; mbox-names = "mbox"; iommus = <&sep_dart 0>; + power-domains = <&ps_sep>, <&ps_gpio>; status = "disabled"; }; @@ -2006,6 +2053,7 @@ dcpext: dcp@271c00000 { compatible = "apple,t8112-dcpext", "apple,dcpext"; + apple,display-power = <&dcp_power>; mboxes = <&dcpext_mbox>; mbox-names = "mbox"; iommus = <&dcpext_dart 5 0x8 0x00000000 0x7 0xffff0000>; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index a4f5310176b43..9d581387981ca 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -9913,6 +9913,22 @@ static const struct dcp_s2r_layout dcp_s2r_t8103_ext = { .disp_streams = BIT(0) | BIT(4), }; +static const struct dcp_s2r_layout dcp_s2r_t8112 = { + .disp_cpu = 0x10000, + .disp_fe = 0x378, + .disp_sys = 0x370, + .dcp_streams = BIT(5), + .disp_streams = BIT(0) | BIT(4), +}; + +static const struct dcp_s2r_layout dcp_s2r_t8112_ext = { + .disp_cpu = 0x3c8, + .disp_fe = 0x388, + .disp_sys = 0x380, + .dcp_streams = BIT(5), + .disp_streams = BIT(0) | BIT(4), +}; + /* TEST: J813 external processors, ADT DISPEXT0/1 power words. */ static const struct dcp_s2r_layout dcp_s2r_t8142_ext[] = { { .disp_cpu = 0x428, .disp_fe = 0x418, .disp_sys = 0x308, @@ -11922,6 +11938,20 @@ static const struct apple_dcp_hw_data apple_dcp_hw_t6020_dcpext = { static const struct apple_dcp_hw_data apple_dcp_hw_t8112 = { .num_dptx_ports = 2, + .s2r = true, + .s2r_no_secure_port = true, + .map_reg_dart = true, + .s2r_disp_dart_with_dcp = true, + .s2r_layout = &dcp_s2r_t8112, +}; + +static const struct apple_dcp_hw_data apple_dcp_hw_t8112_dcpext = { + .num_dptx_ports = 2, + .s2r_ext = true, + .s2r_no_secure_port = true, + .map_reg_dart = true, + .s2r_disp_dart_with_dcp = true, + .s2r_layout = &dcp_s2r_t8112_ext, }; /* The internal T8140 endpoint uses the H17P method profile. */ @@ -12140,6 +12170,7 @@ static const struct of_device_id of_match[] = { { .compatible = "apple,t8103-dcp", .data = &apple_dcp_hw_t8103, }, { .compatible = "apple,t8103-dcpext", .data = &apple_dcp_hw_t8103_dcpext, }, { .compatible = "apple,t8112-dcp", .data = &apple_dcp_hw_t8112, }, + { .compatible = "apple,t8112-dcpext", .data = &apple_dcp_hw_t8112_dcpext, }, { .compatible = "apple,t8140-dcp", .data = &apple_dcp_hw_t8140, }, { .compatible = "apple,t8140-dcpext", .data = &apple_dcp_hw_t8140_external, }, { .compatible = "apple,t8142-dcp", .data = &apple_dcp_hw_t8142, }, diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index c7b7ce1cb17ad..d215c868bf1f0 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -9090,6 +9090,84 @@ int apple_dart_display_keep(struct device *dev, u32 streams, bool save) } EXPORT_SYMBOL_GPL(apple_dart_display_keep); +static int apple_dart_t8112_display_streams(struct apple_dart *dart, u32 streams, bool on, + bool flush) +{ + struct apple_dart_stream_map map = { .dart = dart }; + unsigned long bits = streams; + unsigned int sid, idx; + u32 error; + int ret = 0; + + if (on) { + if (!READ_ONCE(dart->host_off) && !READ_ONCE(dart->display_dark)) + return 0; + if (dart->display_saved && !apple_dart_state_retained(dart)) { + if (apple_dart_is_locked(dart)) { + dev_err(dart->dev, "translations lost at the power-down, and locked\n"); + return -EIO; + } + ret = apple_dart_hw_reset(dart); + if (ret && ret != -EBUSY) + dev_warn(dart->dev, "reset at power-on: %d\n", ret); + for_each_set_bit(sid, &bits, dart->num_streams) + writel(BIT(sid), dart->regs + DART_T8110_DISABLE_STREAMS); + for (sid = 0; sid < dart->num_streams; sid++) { + for (idx = 0; idx < dart->hw->ttbr_count; idx++) + writel(dart->save_ttbr[sid][idx], + dart->regs + DART_TTBR(dart, sid, idx)); + writel(dart->save_tcr[sid], dart->regs + DART_TCR(dart, sid)); + } + ret = 0; + dev_dbg(dart->dev, "translations put back at power-on\n"); + } + error = readl(dart->regs + DART_T8110_ERROR); + if (error & DART_T8110_ERROR_FLAG) + writel(error, dart->regs + DART_T8110_ERROR); + scoped_guard(spinlock_irqsave, &dart->managed_lock) + WRITE_ONCE(dart->display_tlb_stale, true); + ret = apple_dart_display_dark_exit(dart); + if (ret) + return ret; + if (dart->display_saved) + writel(dart->display_enable & streams, + dart->regs + DART_T8110_ENABLE_STREAMS); + else + writel(streams, dart->regs + DART_T8110_ENABLE_STREAMS); + dart->host_off_slept = false; + dart->display_saved = false; + dart->sleep_flushed = false; + WRITE_ONCE(dart->host_off, false); + return 0; + } + + if (READ_ONCE(dart->host_off)) + return 0; + if (!READ_ONCE(dart->display_dark)) { + apple_dart_save_state(dart); + dart->display_enable = readl(dart->regs + DART_T8110_ENABLE_STREAMS); + dart->display_saved = true; + } + ret = apple_dart_display_dark_enter(dart); + if (ret) + return ret; + for_each_set_bit(sid, &bits, dart->num_streams) + writel(BIT(sid), dart->regs + DART_T8110_DISABLE_STREAMS); + if (flush) { + for_each_set_bit(sid, &bits, dart->num_streams) + set_bit(sid, map.sidmap); + ret = dart->hw->invalidate_tlb(&map); + if (ret) { + dev_err(dart->dev, "flush for a power-down failed: %d\n", ret); + return ret; + } + dart->sleep_flushed = true; + } + WRITE_ONCE(dart->host_off, true); + + return 0; +} + int apple_dart_display_streams(struct device *dev, u32 streams, bool on, bool flush) { struct apple_dart *dart = dev_get_drvdata(dev); @@ -9100,6 +9178,8 @@ int apple_dart_display_streams(struct device *dev, u32 streams, bool on, bool fl if (dart->hw->type == DART_T8020) return apple_dart_t8020_display_streams(dart, streams, on, flush); + if (of_device_is_compatible(dev->of_node, "apple,t8112-dart")) + return apple_dart_t8112_display_streams(dart, streams, on, flush); if (on) { apple_dart_gapf_protect(dart); diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index a13eb5c1fcb2c..cb4924c9f03c1 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -319,6 +319,16 @@ static const struct aic_info aic2_info __initconst = { .local_fast_ipi = true, }; +static const struct aic_info aic2_t8112_info __initconst = { + .version = 2, + + .irq_cfg = AIC2_IRQ_CFG, + + .fast_ipi = true, + .local_fast_ipi = true, + .fw_suspend = true, +}; + static const struct aic_info aic3_info __initconst = { .version = 3, @@ -412,6 +422,10 @@ static const struct of_device_id aic_info_match[] = { .compatible = "apple,aic", .data = &aic1_info, }, + { + .compatible = "apple,t8112-aic", + .data = &aic2_t8112_info, + }, { .compatible = "apple,aic2", .data = &aic2_info, diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 999765733b68f..781dc7699438b 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -768,7 +768,8 @@ static int apple_smc_probe(struct platform_device *pdev) * its SMC hears last, after SEP and the other coprocessors stopped. */ if (of_machine_is_compatible("apple,j813") || - of_device_is_compatible(dev->of_node, "apple,t8103-smc")) + of_device_is_compatible(dev->of_node, "apple,t8103-smc") || + of_device_is_compatible(dev->of_node, "apple,t8112-smc")) smc->saved_enabled = true; smc->sram_base = devm_platform_get_and_ioremap_resource(pdev, 1, &smc->sram); if (IS_ERR(smc->sram_base)) diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index 2e753840fb88e..58973eab3c9a5 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -1053,6 +1053,14 @@ const HW_CFG_T6050: PmpHwConfig = PmpHwConfig { confirm_ioreg_len: false, }; +/// T8112 (M2): the sleep engine reloads `__TEXT` from DRAM at the wake, and only `__DATA` +/// (the ADT's second segment, offset 0x2f000, size 0x46000 on J413) has to be kept. When the +/// boot loader supplies `apple,sram-keep`, both offset and size come from the DT instead. +const HW_CFG_T8112: PmpHwConfig = PmpHwConfig { + sram_backup: Some((0x2f000, 0x46000)), + ..HW_CFG_DEFAULT +}; + /// T8122: the sleep engine reloads `__TEXT` from DRAM at the wake, and only `__DATA` /// (the ADT's second segment, offset 0x36000, size 0x47000) has to be kept. When the /// boot loader supplies `apple,sram-keep`, both offset and size come from the DT instead. @@ -1086,6 +1094,7 @@ kernel::of_device_table!( OF_TABLE, ::IdInfo, [ + (of::DeviceId::new(c"apple,t8112-pmp-v2"), &HW_CFG_T8112), (of::DeviceId::new(c"apple,t8122-pmp-v2"), &HW_CFG_T8122), (of::DeviceId::new(c"apple,t8132-pmp-v2"), &HW_CFG_T8132), (of::DeviceId::new(c"apple,t8140-pmp-v2"), &HW_CFG_T8140), diff --git a/drivers/soc/apple/sleep-t8112.h b/drivers/soc/apple/sleep-t8112.h new file mode 100644 index 0000000000000..24ef5ab871b39 --- /dev/null +++ b/drivers/soc/apple/sleep-t8112.h @@ -0,0 +1,1269 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T8112 (Apple M2) system suspend-to-RAM dry-run profile. + * + * Phase 2 bring-up profile: maps the verified T8112 PMGR, PMGR_MINI, clock mux, + * wake, CPU start, WDT, and SPMI register windows and registers the full + * T8112 PMGR power-state inventory and coprocessor gates for d1 dry-run + * validation. Physical commit is explicitly refused until Phase 3 hardware + * entry/wake sequences are verified. + */ + +#define T8112_PS_SBR 0x100 +#define T8112_PS_AIC 0x108 +#define T8112_PS_DWI 0x110 +#define T8112_PS_GPIO 0x120 +#define T8112_PS_SIO_CPU 0x1c0 +#define T8112_PS_APCIE 0x348 +#define T8112_PS_DISP0_SYS 0x370 +#define T8112_PS_DISP0_FE 0x378 +#define T8112_PS_APCIE_GP 0x400 +#define T8112_PS_ANS 0x428 +#define T8112_PS_GFX 0x430 +#define T8112_PS_APCIE_ST 0x450 +#define T8112_PS_ATC0_COMMON 0x458 +#define T8112_PS_ATC1_COMMON 0x480 +#define T8112_PS_SEP 0xc00 +#define T8112_PS_DISP0_CPU0 0x10000 + +#define T8112_AON_ATC0_USB_AON 0x88 +#define T8112_AON_ATC1_USB_AON 0x90 +#define T8112_AON_ATC0_USB 0x98 +#define T8112_AON_ATC1_USB 0xa0 +#define T8112_AON_NUB_FABRIC 0xa8 +#define T8112_AON_DEBUG_SWITCH 0xb8 + +#define T8112_PWRGATE_E 0x80 +#define T8112_PWRGATE_P 0x88 +#define T8112_CLUSTER_E 0x0 +#define T8112_CLUSTER_P 0x4 + +#define T8112_MUX_PARK_VAL 0x80100000 +#define T8112_DCS_PERF_OFF 0x5c000 +#define T8112_DCS_SLEEP 3 +#define T8112_TVM_IDLE_OFF 0x3e0 +#define T8112_TVM_BUSY BIT(1) +#define T8112_TVM_TIMEOUT_US 10000 + +#define T8112_MUX_SOC 0x40000 +#define T8112_MUX_SOC_WORDS 64 + +#define T8112_MCC_PLANES_BASE 0x200000000ULL +#define T8112_MCC_PLANE_STRIDE 0x40000 +#define T8112_MCC_NPLANES 4 +#define T8112_MCC_CACHE_DISABLE 0x424 +#define T8112_MCC_WAYS 0x1c00 +#define T8112_MCC_WAYS_STATUS 0x1c04 +#define T8112_MCC_WAYS_DATA GENMASK(14, 10) +#define T8112_MCC_WAYS_TAG GENMASK(9, 5) +#define T8112_MCC_TIMEOUT_US 100000 + +/* AGX fabric tunables programmed by the boot loader; lost when ps_gfx powers off. */ +#define T8112_AGX_FABRIC 0x205000000ULL +static const u32 t8112_agx_regs[] = { 0x0, 0x34, 0x38, 0x400, 0x600, 0x738, 0x798, 0x800 }; + +static const u32 t8112_mux_park[] = { 0x40040, 0x40048, 0x40044, 0x40038, 0x40034 }; +static const u32 t8112_tvm[] = { 0x400, 0x800, 0xc00, 0x000 }; +static const u32 t8112_auto_off[] = { 0x1b0, 0x1a8, 0x1b8 }; + +#define T8112_SYS_HID4 sys_reg(3, 0, 15, 4, 0) +#define T8112_SYS_EHID4 sys_reg(3, 0, 15, 4, 1) +#define T8112_HID4_DISABLE_DC_MVA BIT_ULL(11) +#define T8112_HID4_DISABLE_DC_SW_L2 BIT_ULL(44) +#define T8112_HID4_DC_MASK (T8112_HID4_DISABLE_DC_MVA | T8112_HID4_DISABLE_DC_SW_L2) +#define T8112_MPIDR_P_CORE BIT_ULL(16) + +#define T8112_SYS_CYC_OVRD sys_reg(3, 5, 15, 5, 0) +#define T8112_CYC_WFI_MODE GENMASK_ULL(25, 24) + +/* Every PMGR power-state word in the T8112 device tree (t8112-pmgr.dtsi). */ +static const u32 t8112_ps_all[] = { + 0x100, 0x108, 0x110, 0x118, 0x120, 0x128, 0x130, 0x160, 0x168, 0x170, + 0x178, 0x180, 0x188, 0x190, 0x198, 0x1a0, 0x1a8, 0x1b0, 0x1b8, 0x1c0, + 0x1c8, 0x1d0, 0x1d8, 0x1e0, 0x1e8, 0x1f0, 0x1f8, 0x200, 0x208, 0x210, + 0x218, 0x220, 0x228, 0x230, 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, + 0x268, 0x270, 0x278, 0x280, 0x288, 0x290, 0x298, 0x2a0, 0x2a8, 0x2b0, + 0x2b8, 0x2c0, 0x2c8, 0x2d0, 0x2d8, 0x2e0, 0x2e8, 0x2f0, 0x2f8, 0x300, + 0x308, 0x310, 0x318, 0x320, 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, + 0x358, 0x360, 0x368, 0x370, 0x378, 0x380, 0x388, 0x3c8, 0x3d8, 0x3e0, + 0x3e8, 0x3f0, 0x3f8, 0x400, 0x408, 0x410, 0x418, 0x420, 0x428, 0x430, + 0x438, 0x440, 0x448, 0x450, 0x458, 0x460, 0x468, 0x470, 0x478, 0x480, + 0x488, 0x490, 0x498, 0x4a0, 0x4a8, 0x4b0, 0xc00, 0x4000, 0x4008, 0x4010, + 0x4018, 0x4020, 0x4028, 0x4030, 0x4038, 0x4040, 0x4048, 0x4050, 0x4058, + 0x4060, 0x8000, 0x8008, 0x8010, 0x8018, 0x8020, 0xc008, 0xc010, 0xc018, + 0xc020, 0xc028, 0xc030, 0xc038, 0x10000, +}; + +/* Every PMGR_MINI (ps-aon) power-state word in t8112-pmgr.dtsi. */ +static const u32 t8112_ps_aon_all[] = { + 0x58, 0x60, 0x68, 0x70, 0x78, 0x80, 0x88, 0x90, 0x98, 0xa0, 0xa8, 0xb0, + 0xb8, +}; + +/* Parent-first wake order for core, PCIe/ANS, and ATC domains (t8112-pmgr.dtsi). */ +static const u32 t8112_ps_wake_order[] = { + T8112_PS_SEP, T8112_PS_SBR, T8112_PS_AIC, T8112_PS_DWI, T8112_PS_GPIO, + T8112_PS_APCIE, T8112_PS_ANS, T8112_PS_APCIE_GP, T8112_PS_APCIE_ST, + T8112_PS_ATC0_COMMON, T8112_PS_ATC1_COMMON, +}; + +static const u32 t8112_ps_aon_wake_order[] = { + T8112_AON_NUB_FABRIC, T8112_AON_ATC0_USB_AON, T8112_AON_ATC1_USB_AON, +}; + +#define T8112_PS_PCIE_REF 0x198 +#define T8112_PS_RMX0 0x350 +#define T8112_PS_RMX1 0x358 +#define T8112_PS_DISPEXT_SYS 0x380 +#define T8112_PS_DISPEXT_FE 0x388 +#define T8112_PS_DISPEXT_CPU0 0x3c8 +#define T8112_PS_PMP 0x410 +#define T8112_PS_ISP_SYS 0x438 +#define T8112_PS_ATC0_PCIE 0x460 +#define T8112_PS_ATC1_PCIE 0x488 + +/* Domains lowered children-before-parents at commit (GFX first while PMP is running). */ +static const u32 t8112_commit_off[] = { + T8112_PS_GFX, /* 0: lowered before PMP stop */ + T8112_PS_DISP0_CPU0, + T8112_PS_DISP0_FE, + T8112_PS_DISP0_SYS, + T8112_PS_DISPEXT_CPU0, + T8112_PS_DISPEXT_FE, + T8112_PS_DISPEXT_SYS, + T8112_PS_ISP_SYS, + T8112_PS_ATC1_PCIE, + T8112_PS_ATC1_COMMON, + T8112_PS_ATC0_PCIE, + T8112_PS_ATC0_COMMON, + T8112_PS_RMX0, + T8112_PS_RMX1, + T8112_PS_PMP, + T8112_PS_APCIE_ST, + T8112_PS_APCIE_GP, + T8112_PS_APCIE, + T8112_PS_PCIE_REF, + T8112_PS_GPIO, +}; + +static const u32 t8112_aon_commit_off[] = { + T8112_AON_ATC0_USB, + T8112_AON_ATC1_USB, + T8112_AON_ATC0_USB_AON, + T8112_AON_ATC1_USB_AON, + T8112_AON_DEBUG_SWITCH, + 0x58, /* debug_gated */ + 0x78, /* msg */ +}; + +static struct { + void __iomem *clk_mux; + void __iomem *mcc_ctl; + void __iomem *mcc[5]; + void __iomem *mcc_planes; + u32 slc_ways; + u32 slc_off_bad; + u32 slc_on_bad; + bool slc_disabled; + void __iomem *pmp_asc; + void __iomem *pmp_mbox; + void __iomem *nco; + void __iomem *agx_fabric; + struct platform_device *gpu; + u32 agx_saved[ARRAY_SIZE(t8112_agx_regs)]; + bool agx_saved_valid; + int gfx_wake_err; + void __iomem *ed[2][4]; + u32 notify_saved; + bool notify_staged; + u32 spmi_vec_saved; + u32 nco_forced; + u32 commit_saved_ps[ARRAY_SIZE(t8112_commit_off)]; + u32 commit_lowered; + u32 aon_lowered; + u32 mux_snap[T8112_MUX_SOC_WORDS]; + u32 mux_saved[ARRAY_SIZE(t8112_mux_park)]; + u32 tvm_saved[ARRAY_SIZE(t8112_tvm)]; + u32 auto_saved[ARRAY_SIZE(t8112_auto_off)]; + u32 dcs_saved; + u32 dcs_down_val; + u32 dcs_wake; + u32 dcs_up_val; + bool dcs_down_ok; + bool dcs_up_ok; + u32 wake_entry[8]; + u32 spmi_vec_commit; + u32 spmi_ack_commit; + u32 spmi_notify_commit; + u32 spmi_vec_wake; + u32 spmi_ack_wake; + bool commit_saved; + bool committed; +} t8112; + +static void __iomem *t8112_map(struct apple_sleep *s, const char *name, struct resource *out) +{ + struct device_node *np = s->dev->of_node; + struct resource res; + void __iomem *base; + int index; + + index = of_property_match_string(np, "reg-names", name); + if (index < 0) + return IOMEM_ERR_PTR(index); + if (of_address_to_resource(np, index, &res)) + return IOMEM_ERR_PTR(-EINVAL); + base = ioremap_np(res.start, resource_size(&res)); + if (!base) + return IOMEM_ERR_PTR(-ENOMEM); + if (out) + *out = res; + + return base; +} + +static int t8112_sleep_map_regs(struct apple_sleep *s) +{ + static const struct { + const char *name; + size_t offset; + } regs[] = { + { "pwrgate", offsetof(struct apple_sleep, pwrgate) }, + { "cpu-start", offsetof(struct apple_sleep, cpu_start) }, + { "cllt", offsetof(struct apple_sleep, cllt) }, + { "cluster", offsetof(struct apple_sleep, cluster) }, + { "commit", offsetof(struct apple_sleep, commit) }, + { "wdt", offsetof(struct apple_sleep, wdt) }, + { "spmi", offsetof(struct apple_sleep, spmi) }, + { "wake", offsetof(struct apple_sleep, wake) }, + { "ps-aon", offsetof(struct apple_sleep, ps_aon) }, + }; + unsigned int i; + + s->ps = t8112_map(s, "ps", &s->ps_res); + if (IS_ERR(s->ps)) + return dev_err_probe(s->dev, PTR_ERR(s->ps), "no ps registers\n"); + for (i = 0; i < ARRAY_SIZE(regs); i++) { + void __iomem **field = (void __iomem **)((u8 *)s + regs[i].offset); + + *field = t8112_map(s, regs[i].name, NULL); + if (IS_ERR(*field)) + return dev_err_probe(s->dev, PTR_ERR(*field), "no %s registers\n", + regs[i].name); + } + t8112.clk_mux = t8112_map(s, "clk-mux", NULL); + if (IS_ERR(t8112.clk_mux)) + return dev_err_probe(s->dev, PTR_ERR(t8112.clk_mux), "no clk-mux registers\n"); + + t8112.mcc_ctl = t8112_map(s, "mcc-ctl", NULL); + if (IS_ERR(t8112.mcc_ctl)) { + dev_warn(s->dev, "no mcc-ctl registers: MCC quiesce unavailable\n"); + t8112.mcc_ctl = NULL; + } + { + static const char *const mcc_names[5] = { + "mcc-a", "mcc-b", "mcc-c", "mcc-d", "mcc-e", + }; + + for (i = 0; i < ARRAY_SIZE(mcc_names); i++) { + t8112.mcc[i] = t8112_map(s, mcc_names[i], NULL); + if (IS_ERR(t8112.mcc[i])) { + dev_warn(s->dev, "no %s registers: MCC channel stop unavailable\n", + mcc_names[i]); + t8112.mcc[i] = NULL; + } + } + } + + { + int ret = apple_sleep_read_wake_sources(s); + + if (ret) + return ret; + } + + /* Ensure the EL1 deep-WFI stub is clean in DRAM before cores jump to it. */ + { + bool pcore = t8122_is_pcore(); + u64 hid4_orig = t8122_read_hid4(pcore); + + t8122_write_hid4(pcore, hid4_orig & ~T8112_HID4_DISABLE_DC_MVA); + t8122_clean_range_slc((unsigned long)t8142_el1_wfi_stub, + (unsigned long)t8142_el1_wfi_stub_end); + t8122_write_hid4(pcore, hid4_orig); + } + + t8112.mcc_planes = t8103_ioremap(s, T8112_MCC_PLANES_BASE, + T8112_MCC_NPLANES * T8112_MCC_PLANE_STRIDE); + t8112.pmp_asc = t8103_ioremap(s, T8103_PMP_ASC, 0x100); + t8112.pmp_mbox = t8103_ioremap(s, T8103_PMP_MBOX, 0x1000); + t8112.nco = t8103_ioremap(s, T8103_NCO, T8103_NCO_CHANS * T8103_NCO_STRIDE); + t8112.agx_fabric = t8103_ioremap(s, T8112_AGX_FABRIC, 0x1000); + { + struct device_node *np = of_find_compatible_node(NULL, NULL, "apple,agx-t8112"); + if (np) { + t8112.gpu = of_find_device_by_node(np); + of_node_put(np); + } + if (t8112.gpu && + devm_add_action_or_reset(s->dev, apple_sleep_put_device, &t8112.gpu->dev)) + return -ENOMEM; + } + for (i = 0; i < 2; i++) { + unsigned int c; + + for (c = 0; c < 4; c++) + t8112.ed[i][c] = t8103_ioremap(s, T8103_ED + i * T8103_ED_CLUSTER + + c * T8103_ED_CORE, 0x1000); + } + + s->driver_down = devm_bitmap_zalloc(s->dev, resource_size(&s->ps_res) / 4, GFP_KERNEL); + if (!s->driver_down) + return -ENOMEM; + if (apple_sleep_alloc_books(s)) + return -ENOMEM; + + return 0; +} + +static int t8112_sleep_check(struct apple_sleep *s) +{ + if (t8112.gpu && device_is_bound(&t8112.gpu->dev)) { + if (!apple_sleep_coproc_ready_node(t8112.gpu->dev.of_node)) { + dev_err(s->dev, "refused: the GPU's driver is bound\n"); + return -EBUSY; + } + if (!t8112.agx_fabric) { + dev_err(s->dev, "refused: GPU fabric registers not mapped\n"); + return -ENODEV; + } + } + return 0; +} + +static void t8112_ps_lower_if_on(struct apple_sleep *s, u32 off, u32 first, u32 second) +{ + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + off))) + return; + if (first) + writel(first, s->ps + off); + writel(second, s->ps + off); +} + +static int t8112_sleep_display_off(struct apple_sleep *s) +{ + u64 start, hz = arch_timer_get_cntfrq(); + bool off = false; + + if (FIELD_GET(PS_ACTUAL, readl(s->ps + T8112_PS_DISP0_CPU0))) { + writel(0x0f0000ff, s->ps + T8112_PS_DISP0_CPU0); + writel(0x0f0003f0, s->ps + T8112_PS_DISP0_CPU0); + } + t8112_ps_lower_if_on(s, T8112_PS_DISP0_FE, 0, 0x23f0); + t8112_ps_lower_if_on(s, T8112_PS_DISP0_SYS, 0, 0x23f0); + + start = read_sysreg(cntvct_el0); + while (read_sysreg(cntvct_el0) - start < hz / 10) { + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + T8112_PS_DISP0_CPU0))) { + off = true; + break; + } + cpu_relax(); + } + if (!off) { + dev_err(s->dev, "refused: DISP0_CPU0 stayed on: %#x\n", + readl(s->ps + T8112_PS_DISP0_CPU0)); + return -EBUSY; + } + + return 0; +} + +static __always_inline bool t8112_is_pcore(void) +{ + return !!(read_sysreg(mpidr_el1) & T8112_MPIDR_P_CORE); +} + +static __always_inline u64 t8112_read_hid4(bool pcore) +{ + return pcore ? read_sysreg_s(T8112_SYS_HID4) + : read_sysreg_s(T8112_SYS_EHID4); +} + +static __always_inline void t8112_write_hid4(bool pcore, u64 val) +{ + if (pcore) + write_sysreg_s(val, T8112_SYS_HID4); + else + write_sysreg_s(val, T8112_SYS_EHID4); + isb(); +} + +static __always_inline void t8112_clean_range_slc(unsigned long start, unsigned long end) +{ + unsigned long line = 4UL << ((read_sysreg(ctr_el0) >> 16) & 0xf); + unsigned int dpb = read_sysreg(id_aa64isar1_el1) & 0xf; + unsigned long addr = start & ~(line - 1); + + if (dpb >= 2) { + for (; addr < end; addr += line) + asm volatile("sys #3, c7, c13, #1, %0" : : "r"(addr) : "memory"); + } else if (dpb >= 1) { + for (; addr < end; addr += line) + asm volatile("sys #3, c7, c12, #1, %0" : : "r"(addr) : "memory"); + } else { + for (; addr < end; addr += line) + asm volatile("dc civac, %0" : : "r"(addr) : "memory"); + } + dsb(sy); +} + +/* + * Register-only dc csw across all implemented data/unified cache levels + * (L1 + L2 on T8112). Must run with HID4/EHID4 DISABLE_DC_SW_L2 cleared so + * set/way L2 maintenance instructions are not ignored as NOPs. + */ +static __always_inline void t8112_dcache_clean_all_levels(void) +{ + u64 r_clidr, r_ccidx, r_lvl, r_ctype, r_ccsidr, r_lshift; + u64 r_way, r_mset, r_wshift, r_set, r_tmp; + + asm volatile( + "dsb sy\n\t" + "mrs %[clidr], clidr_el1\n\t" + "mrs %[ccidx], id_aa64mmfr2_el1\n\t" + "ubfx %[ccidx], %[ccidx], #20, #4\n\t" + "mov %[lvl], #0\n\t" + "1:\n\t" + "cmp %[lvl], #7\n\t" + "b.hs 5f\n\t" + "add %[tmp], %[lvl], %[lvl], lsl #1\n\t" + "lsr %[ctype], %[clidr], %[tmp]\n\t" + "and %[ctype], %[ctype], #7\n\t" + "cbz %[ctype], 5f\n\t" + "cmp %[ctype], #2\n\t" + "b.lo 4f\n\t" + "cmp %[ctype], #4\n\t" + "b.hi 4f\n\t" + "lsl %[tmp], %[lvl], #1\n\t" + "msr csselr_el1, %[tmp]\n\t" + "isb\n\t" + "mrs %[ccsidr], ccsidr_el1\n\t" + "and %[lshift], %[ccsidr], #7\n\t" + "add %[lshift], %[lshift], #4\n\t" + "cbnz %[ccidx], 20f\n\t" + "ubfx %[way], %[ccsidr], #3, #10\n\t" + "ubfx %[mset], %[ccsidr], #13, #15\n\t" + "b 21f\n\t" + "20:\n\t" + "ubfx %[way], %[ccsidr], #3, #21\n\t" + "ubfx %[mset], %[ccsidr], #32, #24\n\t" + "21:\n\t" + "clz %w[wshift], %w[way]\n\t" + "2:\n\t" + "mov %[set], %[mset]\n\t" + "lsl %[ctype], %[way], %[wshift]\n\t" + "orr %[ctype], %[ctype], %[lvl], lsl #1\n\t" + "3:\n\t" + "lsl %[tmp], %[set], %[lshift]\n\t" + "orr %[tmp], %[ctype], %[tmp]\n\t" + "dc csw, %[tmp]\n\t" + "subs %[set], %[set], #1\n\t" + "b.ge 3b\n\t" + "subs %[way], %[way], #1\n\t" + "b.ge 2b\n\t" + "dsb sy\n\t" + "4:\n\t" + "add %[lvl], %[lvl], #1\n\t" + "b 1b\n\t" + "5:\n\t" + "msr csselr_el1, xzr\n\t" + "dsb sy\n\t" + "isb\n\t" + : [clidr] "=&r"(r_clidr), + [ccidx] "=&r"(r_ccidx), + [lvl] "=&r"(r_lvl), + [ctype] "=&r"(r_ctype), + [ccsidr] "=&r"(r_ccsidr), + [lshift] "=&r"(r_lshift), + [way] "=&r"(r_way), + [mset] "=&r"(r_mset), + [wshift] "=&r"(r_wshift), + [set] "=&r"(r_set), + [tmp] "=&r"(r_tmp) + : + : "cc", "memory"); +} + +static void t8112_sleep_prepare_regs(struct apple_sleep *s) +{ +} + +static void t8112_sleep_undo_regs(struct apple_sleep *s) +{ +} + +static void t8112_aon_lower(struct apple_sleep *s, u32 off) +{ + u32 val = readl(s->ps_aon + off); + + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + writel(val, s->ps_aon + off); + } + writel((val & ~PS_TARGET) | PS_FLAGS, s->ps_aon + off); +} + +/* + * Stop SLC allocation and power down all 4 MCC planes so every dirty SLC line + * is written back to DRAM before deep sleep loses MCC power. + */ +static void t8112_slc_off(void) +{ + unsigned int p; + u32 v, ways; + + t8112.slc_disabled = false; + t8112.slc_off_bad = 0; + if (!t8112.mcc_planes) + return; + + v = readl(t8112.mcc_planes + T8112_MCC_WAYS_STATUS); + ways = FIELD_GET(T8112_MCC_WAYS_DATA, v); + if (!ways) + ways = FIELD_GET(T8112_MCC_WAYS_TAG, v); + if (!ways) + return; + + t8112.slc_ways = ways; + for (p = 0; p < T8112_MCC_NPLANES; p++) { + void __iomem *base = t8112.mcc_planes + p * T8112_MCC_PLANE_STRIDE; + + writel(1, base + T8112_MCC_CACHE_DISABLE); + } + dsb(sy); + + for (p = 0; p < T8112_MCC_NPLANES; p++) { + void __iomem *base = t8112.mcc_planes + p * T8112_MCC_PLANE_STRIDE; + + writel(0, base + T8112_MCC_WAYS); + } + dsb(sy); + + for (p = 0; p < T8112_MCC_NPLANES; p++) { + void __iomem *reg = t8112.mcc_planes + p * T8112_MCC_PLANE_STRIDE + + T8112_MCC_WAYS_STATUS; + + if (readl_poll_timeout_atomic(reg, v, + !FIELD_GET(T8112_MCC_WAYS_DATA, v) && + !FIELD_GET(T8112_MCC_WAYS_TAG, v), + 1, T8112_MCC_TIMEOUT_US)) + t8112.slc_off_bad |= BIT(p); + } + dsb(sy); + t8112.slc_disabled = true; +} + +static void t8112_slc_on(void) +{ + unsigned int p; + u32 v, ways; + + t8112.slc_on_bad = 0; + if (!t8112.mcc_planes || !t8112.slc_disabled) + return; + + t8112.slc_disabled = false; + ways = t8112.slc_ways; + + for (p = 0; p < T8112_MCC_NPLANES; p++) { + void __iomem *base = t8112.mcc_planes + p * T8112_MCC_PLANE_STRIDE; + + writel(ways, base + T8112_MCC_WAYS); + } + dsb(sy); + + for (p = 0; p < T8112_MCC_NPLANES; p++) { + void __iomem *base = t8112.mcc_planes + p * T8112_MCC_PLANE_STRIDE; + + if (readl_poll_timeout_atomic(base + T8112_MCC_WAYS_STATUS, v, + FIELD_GET(T8112_MCC_WAYS_DATA, v) == ways && + FIELD_GET(T8112_MCC_WAYS_TAG, v) == ways, + 1, T8112_MCC_TIMEOUT_US)) + t8112.slc_on_bad |= BIT(p); + else + writel(0, base + T8112_MCC_CACHE_DISABLE); + } + dsb(sy); +} + +static void t8112_mcc_clear_bits64(void __iomem *reg) +{ + u64 q = readq(reg); + + while (q) { + q &= q - 1; + writeq(q, reg); + } +} + +static void t8112_mcc_clear_bits32(void __iomem *reg) +{ + u32 l = readl(reg); + + while (l) { + l &= l - 1; + writel(l, reg); + } +} + +static void t8112_mcc_channel_stop(void) +{ + unsigned int i; + + if (!t8112.mcc[0] || !t8112.mcc[1] || !t8112.mcc[2] || !t8112.mcc[3] || !t8112.mcc[4]) + return; + + if (t8112.mcc_ctl) { + u64 q; + + writel(0x3e, t8112.mcc_ctl + 0x10); + writel(0, t8112.mcc_ctl + 0x0c); + writel(2, t8112.mcc_ctl + 0x2004); + q = readq(t8112.mcc[0]); + if (q) { + q &= q - 1; + writeq(q, t8112.mcc[0]); + } + if (q) { + q &= q - 1; + writeq(q, t8112.mcc[0]); + } + writel(0x3e, t8112.mcc_ctl + 0x10); + writel(0, t8112.mcc_ctl + 0x0c); + writel(0, t8112.mcc_ctl + 0x2004); + } + + for (i = 0; i < 3; i++) + t8112_mcc_clear_bits64(t8112.mcc[i]); + for (i = 0; i < 4; i++) + t8112_mcc_clear_bits64(t8112.mcc[i] + 0x800); + t8112_mcc_clear_bits32(t8112.mcc[4] + 0x584); + t8112_mcc_clear_bits32(t8112.mcc[4] + 0x588); +} + +static void t8112_sleep_commit_regs(struct apple_sleep *s) +{ + bool pcore; + u64 hid4_orig; + unsigned int i; + u32 val; + + apple_sleep_set_wd2(s, T8103_WDT_COMMIT_SECS); + + sleep_ps_save(s); + + /* Wait for SEP to gate after its sleep message, as on T8103. */ + if (apple_sleep_enclave.stop) { + val = readl(s->ps + T8112_PS_SEP); + writel((val & ~0x0f000700U) | BIT(10), s->ps + T8112_PS_SEP); + readl_poll_timeout_atomic(s->ps + T8112_PS_SEP, val, + !FIELD_GET(PS_ACTUAL, val), 10, + T8103_SEP_GATE_US); + } + + /* Check committed GPU stop, save AGX fabric tunables, and lower GFX before PMP stop. */ + t8112.commit_lowered = 0; + t8112.agx_saved_valid = false; + t8112.gfx_wake_err = 0; + t8112.committed = true; + bool gpu_bound = t8112.gpu && device_is_bound(&t8112.gpu->dev); + + if (gpu_bound) { + struct apple_sleep_coproc *gc = apple_sleep_coproc_find(&t8112.gpu->dev); + + if (gc && gc->stop_ret) + panic("T8112 committed GPU stop failed: %d", gc->stop_ret); + } + if (sleep_ps_on(s, t8112_commit_off[0])) { + if (t8112.agx_fabric) { + for (i = 0; i < ARRAY_SIZE(t8112_agx_regs); i++) + t8112.agx_saved[i] = readl(t8112.agx_fabric + t8112_agx_regs[i]); + t8112.agx_saved_valid = true; + } + t8112.commit_saved_ps[0] = readl(s->ps + t8112_commit_off[0]); + t8112.commit_lowered |= BIT(0); + sleep_ps_lower(s, t8112_commit_off[0]); + if (sleep_ps_wait_off(s, t8112_commit_off[0]) && gpu_bound) + panic("T8112 committed GFX power-off timed out"); + } + t8103_asc_stop(t8112.pmp_asc, t8112.pmp_mbox); + + /* Lower pmgr_mini (ps_aon) domains left on by USB/debug/msg. */ + t8112.aon_lowered = 0; + if (s->ps_aon) { + for (i = 0; i < ARRAY_SIZE(t8112_aon_commit_off); i++) { + void __iomem *reg = s->ps_aon + t8112_aon_commit_off[i]; + u32 av = readl(reg); + + if (!FIELD_GET(PS_ACTUAL, av)) + continue; + t8112_aon_lower(s, t8112_aon_commit_off[i]); + if (!readl_poll_timeout_atomic(reg, av, !FIELD_GET(PS_ACTUAL, av), 1, 10000)) + t8112.aon_lowered |= BIT(i); + } + } + + /* Lower remaining leftover ps domains children-before-parents. */ + for (i = 1; i < ARRAY_SIZE(t8112_commit_off); i++) { + if (!sleep_ps_on(s, t8112_commit_off[i])) + continue; + t8112.commit_saved_ps[i] = readl(s->ps + t8112_commit_off[i]); + t8112.commit_lowered |= BIT(i); + sleep_ps_lower(s, t8112_commit_off[i]); + sleep_ps_wait_off(s, t8112_commit_off[i]); + } + + /* Enable disabled NCO channels so externally-clocked ps_mca0..5 don't hang BUSY. */ + t8112.nco_forced = 0; + if (t8112.nco) { + for (i = 0; i < T8103_NCO_CHANS; i++) { + void __iomem *reg = t8112.nco + i * T8103_NCO_STRIDE; + + val = readl(reg); + if (!(val & T8103_NCO_CTRL_EN)) { + writel(val | T8103_NCO_CTRL_EN, reg); + t8112.nco_forced |= BIT(i); + } + } + } + + writel(0, s->wdt + WDT_WD0_CTL); + + /* + * Snapshot all wake-restore state AND mask AIC BEFORE cleaning RAM past SLC + * and stopping MCC channels, so ic->sleep_masks and t8112.* are guaranteed + * to survive deep sleep in DRAM! + */ + for (i = 0; i < ARRAY_SIZE(t8112.mux_snap); i++) + t8112.mux_snap[i] = readl(t8112.clk_mux + T8112_MUX_SOC + i * 4); + for (i = 0; i < ARRAY_SIZE(t8112_mux_park); i++) + t8112.mux_saved[i] = readl(t8112.clk_mux + t8112_mux_park[i]); + for (i = 0; i < ARRAY_SIZE(t8112_tvm); i++) + t8112.tvm_saved[i] = readl(s->cllt + t8112_tvm[i]); + t8112.dcs_saved = readl(t8112.clk_mux + T8112_DCS_PERF_OFF); + for (i = 0; i < ARRAY_SIZE(t8112_auto_off); i++) + t8112.auto_saved[i] = readl(s->ps + t8112_auto_off[i]); + t8112.spmi_vec_saved = readl(s->spmi + T8103_SPMI_VEC_ENABLE); + t8112.commit_saved = true; + + aic_sleep_mask_all(); + aic_sleep_ack(); + + if (s->mbox) { + s->resumes_before = READ_ONCE(s->mbox->resumes); + WRITE_ONCE(s->mbox->resume_pa, __pa_symbol(s->soc->ops->resume)); + } + t8103_pac_saved[0] = read_sysreg_s(sys_reg(3, 4, 15, 0, 4)); + t8103_pac_saved[1] = read_sysreg_s(sys_reg(3, 6, 15, 12, 2)); + t8103_pac_saved[2] = 1; + + pcore = t8112_is_pcore(); + hid4_orig = t8112_read_hid4(pcore); + t8112_write_hid4(pcore, hid4_orig & ~T8112_HID4_DC_MASK); + t8112_dcache_clean_all_levels(); + dsb(sy); + t8112_slc_off(); + t8112_clean_range_slc((unsigned long)t8103_pac_saved, + (unsigned long)(t8103_pac_saved + ARRAY_SIZE(t8103_pac_saved))); + if (s->mbox) + t8112_clean_range_slc((unsigned long)s->mbox, + (unsigned long)(s->mbox + 1)); + t8112_clean_range_slc((unsigned long)s, (unsigned long)(s + 1)); + t8112_clean_range_slc((unsigned long)&t8112, (unsigned long)(&t8112 + 1)); + t8112_clean_range_slc((unsigned long)current, + (unsigned long)current + sizeof(*current)); + t8112_clean_range_slc((unsigned long)task_stack_page(current), + (unsigned long)task_stack_page(current) + THREAD_SIZE); + t8112_dcache_clean_all_levels(); + dsb(sy); + t8112_write_hid4(pcore, hid4_orig); + + t8112_mcc_channel_stop(); + + for (i = 0; i < ARRAY_SIZE(t8112_mux_park); i++) + writel(T8112_MUX_PARK_VAL, t8112.clk_mux + t8112_mux_park[i]); + + for (i = 0; i < ARRAY_SIZE(t8112_tvm); i++) { + writel(0, s->cllt + t8112_tvm[i]); + readl_poll_timeout_atomic(s->cllt + t8112_tvm[i] + T8112_TVM_IDLE_OFF, + val, !(val & T8112_TVM_BUSY), 1, + T8112_TVM_TIMEOUT_US); + } + + t8112.dcs_down_ok = t8103_dcs_step_to(t8112.clk_mux + T8112_DCS_PERF_OFF, + T8112_DCS_SLEEP, &t8112.dcs_down_val); + + writel(PWRGATE_ON, s->pwrgate + T8112_PWRGATE_E); + writel(PWRGATE_ON, s->pwrgate + T8112_PWRGATE_P); + writel(0, s->cluster + T8112_CLUSTER_E); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntp_ctl_el0); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntv_ctl_el0); + writel(0, s->cluster + T8112_CLUSTER_P); + + for (i = 0; i < ARRAY_SIZE(t8112_auto_off); i++) { + val = t8112.auto_saved[i]; + if (val & PS_AUTO_ENABLE) + writel(val & ~PS_AUTO_ENABLE, s->ps + t8112_auto_off[i]); + } + + /* Ack and enable PMU RTC alarm SPMI vector so rtcwake reaches wake source 58. */ + writel(T8103_SPMI_VEC_RTC, s->spmi + T8103_SPMI_VEC_ACK); + writel(t8112.spmi_vec_saved | T8103_SPMI_VEC_RTC, s->spmi + T8103_SPMI_VEC_ENABLE); + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, + s->soc->pmu_sleep_reg, s->soc->pmu_sleep_val); + + t8112.spmi_vec_commit = readl(s->spmi + T8103_SPMI_VEC_ENABLE); + t8112.spmi_ack_commit = readl(s->spmi + T8103_SPMI_VEC_ACK); + t8112.spmi_notify_commit = readl(s->spmi + SPMI_NOTIFY_ENABLE); + + /* Snapshot wake status before committing WFI (read-only). */ + for (i = 0; i < s->soc->wake_words; i++) + t8112.wake_entry[i] = readl(s->wake + s->soc->wake_status + i * 4); + + aic_sleep_ack(); + dsb(sy); +} + +static void t8112_post_timer(struct apple_sleep *s) +{ + dsb(sy); + writel(1, s->commit); +} + +static void t8112_sleep_pre_commit(struct apple_sleep *s) +{ + bool pcore = t8112_is_pcore(); + u64 hid4_orig = t8112_read_hid4(pcore); + + t8103_pac_saved[0] = read_sysreg_s(sys_reg(3, 4, 15, 0, 4)); + t8103_pac_saved[1] = read_sysreg_s(sys_reg(3, 6, 15, 12, 2)); + t8103_pac_saved[2] = 1; + + t8112_write_hid4(pcore, hid4_orig & ~T8112_HID4_DISABLE_DC_MVA); + t8112_clean_range_slc((unsigned long)t8103_pac_saved, + (unsigned long)(t8103_pac_saved + ARRAY_SIZE(t8103_pac_saved))); + if (s->mbox) + t8112_clean_range_slc((unsigned long)s->mbox, + (unsigned long)(s->mbox + 1)); + t8112_clean_range_slc((unsigned long)s, (unsigned long)(s + 1)); + t8112_clean_range_slc((unsigned long)&t8112, (unsigned long)(&t8112 + 1)); + t8112_clean_range_slc((unsigned long)task_stack_page(current), + (unsigned long)task_stack_page(current) + THREAD_SIZE); + dsb(sy); + t8112_write_hid4(pcore, hid4_orig); +} + +static void t8112_sleep_wake(struct apple_sleep *s) +{ + u32 val; + int i, pmp_ret = 0; + + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, s->soc->pmu_sleep_reg, 0x51); + + writel(CLUSTER_ON, s->cluster + T8112_CLUSTER_E); + s->cluster_e_ret = readl_poll_timeout_atomic(s->cluster + T8112_CLUSTER_E, val, + !(val & T8103_CLUSTER_BUSY), 1, + CLUSTER_TIMEOUT_US); + writel(CLUSTER_ON, s->cluster + T8112_CLUSTER_P); + + writel(PWRGATE_ON, s->pwrgate + s->soc->pwrgate_wake[0]); + writel(PWRGATE_ON, s->pwrgate + s->soc->pwrgate_wake[1]); + + if (t8112.commit_saved) { + for (i = ARRAY_SIZE(t8112_auto_off) - 1; i >= 0; i--) + if (t8112.auto_saved[i] & PS_AUTO_ENABLE) + writel(t8112.auto_saved[i], s->ps + t8112_auto_off[i]); + + for (i = ARRAY_SIZE(t8112_tvm) - 1; i >= 0; i--) + writel(t8112.tvm_saved[i], s->cllt + t8112_tvm[i]); + + /* Restore any SoC clock mux that the sleep engine parked or turned off. */ + for (i = 0; i < ARRAY_SIZE(t8112.mux_snap); i++) { + void __iomem *reg = t8112.clk_mux + T8112_MUX_SOC + i * 4; + + if (readl(reg) == t8112.mux_snap[i] || !(t8112.mux_snap[i] & BIT(31))) + continue; + writel(t8112.mux_snap[i], reg); + readl_poll_timeout_atomic(reg, val, !(val & T8103_MUX_BUSY), 1, + T8103_MUX_TIMEOUT_US); + } + for (i = ARRAY_SIZE(t8112_mux_park) - 1; i >= 0; i--) { + void __iomem *reg = t8112.clk_mux + t8112_mux_park[i]; + + writel(t8112.mux_saved[i], reg); + readl_poll_timeout_atomic(reg, val, !(val & T8103_MUX_BUSY), 1, + T8103_MUX_TIMEOUT_US); + } + + t8112_slc_on(); + t8112.dcs_wake = readl(t8112.clk_mux + T8112_DCS_PERF_OFF); + t8112.dcs_up_ok = t8103_dcs_step_to(t8112.clk_mux + T8112_DCS_PERF_OFF, + t8112.dcs_saved & 0xf, + &t8112.dcs_up_val); + + if (t8112.nco) { + for (i = 0; i < T8103_NCO_CHANS; i++) { + if (t8112.nco_forced & BIT(i)) { + void __iomem *reg = t8112.nco + i * T8103_NCO_STRIDE; + + writel(readl(reg) & ~T8103_NCO_CTRL_EN, reg); + } + } + t8112.nco_forced = 0; + } + + t8112.spmi_vec_wake = readl(s->spmi + T8103_SPMI_VEC_ENABLE); + t8112.spmi_ack_wake = readl(s->spmi + T8103_SPMI_VEC_ACK); + writel(t8112.spmi_vec_saved, s->spmi + T8103_SPMI_VEC_ENABLE); + + if (apple_sleep_enclave.stop) { + val = readl(s->ps + T8112_PS_SEP); + writel((val & ~0x0f000700U) | (0xfU << 24) | BIT(9), s->ps + T8112_PS_SEP); + } + + /* + * Restore parent domains (GPIO, APCIE, PMP, RMX, ATC, GFX) parents-first; + * leave DISP0/DISPEXT/ISP to their own drivers' resume paths. + */ + for (i = ARRAY_SIZE(t8112_commit_off) - 1; i >= 0; i--) { + u32 off = t8112_commit_off[i]; + + if (!(t8112.commit_lowered & BIT(i))) + continue; + if (off == T8112_PS_DISP0_CPU0 || off == T8112_PS_DISP0_FE || + off == T8112_PS_DISP0_SYS || off == T8112_PS_DISPEXT_CPU0 || + off == T8112_PS_DISPEXT_FE || off == T8112_PS_DISPEXT_SYS || + off == T8112_PS_ISP_SYS) + continue; + if (off == T8112_PS_PMP) { + pmp_ret = sleep_ps_restore(s->ps + off, t8112.commit_saved_ps[i]); + } else if (off == T8112_PS_GFX) { + int gfx_ret = pmp_ret ? pmp_ret : + sleep_ps_restore(s->ps + off, t8112.commit_saved_ps[i]); + + if (!gfx_ret && t8112.agx_saved_valid && t8112.agx_fabric) { + unsigned int j; + + for (j = 0; j < ARRAY_SIZE(t8112_agx_regs); j++) + writel(t8112.agx_saved[j], + t8112.agx_fabric + t8112_agx_regs[j]); + } + t8112.gfx_wake_err = gfx_ret; + if (gfx_ret && t8112.gpu) { + struct apple_sleep_coproc *gc = + apple_sleep_coproc_find(&t8112.gpu->dev); + + if (gc) + gc->wake_err = gfx_ret; + } + } else { + sleep_ps_restore(s->ps + off, t8112.commit_saved_ps[i]); + } + } + t8112.commit_lowered = 0; + + if (s->ps_aon) { + for (i = ARRAY_SIZE(t8112_aon_commit_off) - 1; i >= 0; i--) { + if (t8112.aon_lowered & BIT(i)) + sleep_ps_restore(s->ps_aon + t8112_aon_commit_off[i], 0xf); + } + t8112.aon_lowered = 0; + } + + t8112.commit_saved = false; + } + + if (s->ps_aon) + sleep_ps_rewake(s); + sleep_ps_raise(s, T8112_PS_GPIO); + s->pins_lost = s->pins_saved_valid; + apple_sleep_restore_pins(); + + val = readl(s->ps + T8112_PS_SEP); + if (val & PS_DEV_DISABLE) + writel(val & ~PS_DEV_DISABLE, s->ps + T8112_PS_SEP); + + writel(PWRGATE_ON, s->pwrgate + T8112_PWRGATE_E); + writel(PWRGATE_ON, s->pwrgate + T8112_PWRGATE_P); + + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, s->soc->pmu_sleep_reg, 0x52); + apple_sleep_set_wd2(s, T8103_WDT_RESUME_SECS); +} + +static void t8112_aic_mask_noop(void) +{ +} + +/* + * void t8112_cpu_off(u64 cyc_ovrd, u64 acc_ovrd): CCIDX-aware version of + * t8103_cpu_off(). Must be called with HID4/EHID4 DISABLE_DC_SW_L2 cleared so + * L2 set/way cleans are not dropped as NOPs, and turns SCTLR_EL1.C off before + * cleaning+invalidating L1 and cleaning L2+ with zero stack accesses. + */ +asm( +" .pushsection .text, \"ax\"\n" +" .type t8112_cpu_off, %function\n" +"t8112_cpu_off:\n" +" msr daifset, #0xf\n" +" mrs x3, sctlr_el1\n" +" bic x3, x3, #(1 << 2)\n" +" msr sctlr_el1, x3\n" +" isb\n" +" dsb sy\n" +" mrs x3, clidr_el1\n" +" mrs x4, id_aa64mmfr2_el1\n" +" ubfx x4, x4, #20, #4\n" +" mov x5, #0\n" +"1: cmp x5, #7\n" +" b.hs 5f\n" +" add x6, x5, x5, lsl #1\n" +" lsr x7, x3, x6\n" +" and x7, x7, #7\n" +" cbz x7, 5f\n" +" cmp x7, #2\n" +" b.lo 4f\n" +" cmp x7, #4\n" +" b.hi 4f\n" +" lsl x6, x5, #1\n" +" msr csselr_el1, x6\n" +" isb\n" +" mrs x8, ccsidr_el1\n" +" and x9, x8, #7\n" +" add x9, x9, #4\n" +" cbnz x4, 20f\n" +" ubfx x10, x8, #3, #10\n" +" ubfx x11, x8, #13, #15\n" +" b 21f\n" +"20: ubfx x10, x8, #3, #21\n" +" ubfx x11, x8, #32, #24\n" +"21: clz w12, w10\n" +"2: mov x13, x11\n" +" lsl x14, x10, x12\n" +" orr x14, x14, x5, lsl #1\n" +"3: lsl x15, x13, x9\n" +" orr x15, x14, x15\n" +" cbnz x5, 30f\n" +" dc cisw, x15\n" +" b 31f\n" +"30: dc csw, x15\n" +"31: subs x13, x13, #1\n" +" b.ge 3b\n" +" subs x10, x10, #1\n" +" b.ge 2b\n" +" dsb sy\n" +"4: add x5, x5, #1\n" +" b 1b\n" +"5: msr csselr_el1, xzr\n" +" dsb sy\n" +" isb\n" +" msr cntp_ctl_el0, xzr\n" +" msr cntv_ctl_el0, xzr\n" +" mrs x3, CurrentEL\n" +" cmp x3, #(2 << 2)\n" +" b.ne 6f\n" +" msr s3_5_c14_c2_1, xzr\n" +" msr s3_5_c14_c3_1, xzr\n" +"6: msr s3_5_c15_c6_0, x1\n" +" msr s3_5_c15_c5_0, x0\n" +" isb\n" +" mov x9, #1\n" +"7: msr s3_5_c15_c1_1, x9\n" +" dsb sy\n" +" isb\n" +" wfi\n" +" b 7b\n" +" .size t8112_cpu_off, . - t8112_cpu_off\n" +" .popsection\n" +); +void t8112_cpu_off(u64 cyc_ovrd, u64 acc_ovrd); + +static void t8112_deep_wfi_arm(void) +{ + u64 mpidr = read_cpuid_mpidr(); + unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1); + unsigned int core = MPIDR_AFFINITY_LEVEL(mpidr, 0); + bool pcore = t8112_is_pcore(); + u64 hid4 = t8112_read_hid4(pcore); + u64 cyc = read_sysreg_s(T8103_CYC_OVRD); + u64 acc = read_sysreg_s(T8103_ACC_OVRD); + + if (cluster < 2 && core < 4 && t8112.ed[cluster][core]) { + writel_relaxed(0xc5acce55U, t8112.ed[cluster][core] + 0xfb0); + writel_relaxed(0, t8112.ed[cluster][core] + 0x310); + dsb(sy); + } + + cyc &= ~(T8103_CYC_WFI_MODE | T8103_CYC_IRQ_MODE | T8103_CYC_FIQ_MODE); + cyc |= FIELD_PREP(T8103_CYC_WFI_MODE, 3) | T8103_CYC_DISABLE_WFI_RET; + write_sysreg_s(0, T8103_PMCR0); + acc &= ~(T8103_ACC_PWR_DN_SRM | T8103_ACC_DIS_L2_FLUSH | + T8103_ACC_TRAIN_DOWN_LINK | T8103_ACC_POWER_DOWN_CPM); + acc |= FIELD_PREP(T8103_ACC_PWR_DN_SRM, 3) | + FIELD_PREP(T8103_ACC_DIS_L2_FLUSH, 2) | + FIELD_PREP(T8103_ACC_TRAIN_DOWN_LINK, 3) | + FIELD_PREP(T8103_ACC_POWER_DOWN_CPM, 3) | + T8103_ACC_DISABLE_PIO_ON_WFI | T8103_ACC_DEEP_SLEEP; + + /* Leave DISABLE_DC_SW_L2 cleared across t8112_cpu_off() so L2 cleans take effect. */ + t8112_write_hid4(pcore, hid4 & ~T8112_HID4_DC_MASK); + t8112_cpu_off(cyc, acc); + unreachable(); +} + +static void t8112_sleep_finish(struct apple_sleep *s) +{ + if (!t8112.committed) + return; + t8112.committed = false; + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, s->soc->pmu_sleep_reg, 0x30); + dev_info(s->dev, "T8112: wake status %08x %08x %08x %08x %08x %08x %08x %08x (armed %08x %08x %08x %08x %08x %08x %08x %08x, entry %08x %08x %08x %08x %08x %08x %08x %08x)\n", + s->wake_status[0], s->wake_status[1], s->wake_status[2], + s->wake_status[3], s->wake_status[4], s->wake_status[5], + s->wake_status[6], s->wake_status[7], + s->wake_armed[0], s->wake_armed[1], s->wake_armed[2], + s->wake_armed[3], s->wake_armed[4], s->wake_armed[5], + s->wake_armed[6], s->wake_armed[7], + t8112.wake_entry[0], t8112.wake_entry[1], t8112.wake_entry[2], + t8112.wake_entry[3], t8112.wake_entry[4], t8112.wake_entry[5], + t8112.wake_entry[6], t8112.wake_entry[7]); + dev_info(s->dev, "T8112: spmi vec=%#x->%#x wake=%#x ack=%#x->%#x dcs=%#x->%#x(%s)->%#x->%#x(%s) agx=%u gfx_err=%d\n", + t8112.spmi_vec_saved, t8112.spmi_vec_commit, t8112.spmi_vec_wake, + t8112.spmi_ack_commit, t8112.spmi_ack_wake, + t8112.dcs_saved, t8112.dcs_down_val, t8112.dcs_down_ok ? "ok" : "stuck", + t8112.dcs_wake, t8112.dcs_up_val, t8112.dcs_up_ok ? "ok" : "stuck", + t8112.agx_saved_valid, t8112.gfx_wake_err); + if (t8112.slc_off_bad) { + dev_warn(s->dev, "SLC write-back timed out on planes %#x\n", + t8112.slc_off_bad); + t8112.slc_off_bad = 0; + } + if (t8112.slc_on_bad) { + dev_warn(s->dev, "SLC restore timed out on planes %#x\n", + t8112.slc_on_bad); + t8112.slc_on_bad = 0; + } +} + +static int t8112_sleep_cpu_prepare(u64 mpidr) +{ + return 0; +} + +static void t8112_sleep_cpu_restore(u64 mpidr) +{ +} + +static const struct apple_sleep_gate_coproc t8112_gate_coprocs[] = { + { "/soc/dcp@231c00000", true, T8112_PS_DISP0_CPU0 }, + { "/soc/pmp@23b500000", true }, + { "/soc/gpu@206400000", true, T8112_PS_GFX }, + { "/soc/sep@25e400000" }, + { "/soc/nvme@27bcc0000" }, + { "/soc/smc@23e400000" }, +}; + +static const struct apple_sleep_wake_name t8112_wake_names[] = { + { 14, "AOP.GPIO.IRQ6" }, + { 26, "AOP.OutboxNotEmpty" }, + { 27, "AOP.CPUWakeupAP" }, + { 44, "NUB.WakeupAP" }, + { 51, "NUB.GPIO.IRQ6" }, + { 53, "NUB.APWatchdog" }, + { 54, "NUB.APWakeupTime" }, + { 58, "NUB.SPMI0.SW3" }, + { 69, "AOP.SPMI0.SW3" }, + { 73, "AOP.SPMI1.SW3" }, + { 81, "SMC.CPUWakeupAP" }, + { 86, "SMC.OutboxNotEmpty" }, + { 147, "NUB.SPMI1.SW3" }, + { 154, "ATC0.CIOWakeup" }, + { 155, "ATC1.CIOWakeup" }, + { 156, "ATC0.USBWakeup" }, + { 157, "ATC1.USBWakeup" }, + { 229, "MTP.GPIO.IRQ4" }, + { 231, "MTP.GPIO.IRQ6" }, + { 248, "MTP.DOCK.CHANNELS.AP0.IRQ" }, +}; + +static const u32 t8112_wake_required[] = { 58 }; + +static const struct apple_sleep_ops t8112_sleep_ops = { + .deep_wfi_arm = t8112_deep_wfi_arm, + .resume = t8103_resume_stub, + .aic_mask_all = t8112_aic_mask_noop, + .aic_ack = aic_sleep_ack, + .aic_restore = aic_sleep_restore, + .map_regs = t8112_sleep_map_regs, + .check = t8112_sleep_check, + .display_off = t8112_sleep_display_off, + .prepare_regs = t8112_sleep_prepare_regs, + .undo_regs = t8112_sleep_undo_regs, + .commit_regs = t8112_sleep_commit_regs, + .pre_commit = t8112_sleep_pre_commit, + .post_timer = t8112_post_timer, + .wake = t8112_sleep_wake, + .finish = t8112_sleep_finish, + .cpu_prepare = t8112_sleep_cpu_prepare, + .cpu_restore = t8112_sleep_cpu_restore, +}; + +static const struct apple_sleep_soc t8112_sleep_soc = { + .ops = &t8112_sleep_ops, + .untested = true, + .early_context = true, + .early_cpu_wfi = true, + .boot_wfi_delay_ms = 2, + .no_pmu_notes = true, + .wdt_wd1 = true, + .smc_gen1_keys = true, + .boot_mpidr = 0x0, + .cpu_count = 8, + .cpu_clusters = 2, + .cpu_cores_per_cluster = 4, + .cpu_start_stride = 4, + .cluster_cpu = { T8112_CLUSTER_E, T8112_CLUSTER_P }, + .ncluster_cpu = 2, + .pwrgate_wake = { 0x000, 0x040 }, + .pwrgate_cpm = { T8112_PWRGATE_E, T8112_PWRGATE_P }, + .ndisp = 1, + .nmcc = 4, + .pmu_usid = 0xe, + /* Stowe PMU on nub-spmi0:0e (info-leg_scrpad = 0xf700, boot-stage = 0xf701). */ + .pmu_sleep_reg = 0xf701, + .pmu_sleep_val = 0x50, + .spmi_notify_sleep = 0x2000001, + .wake_mask = 0x08, + .wake_status = 0x28, + .wake_words = 8, + .wake_last_word = GENMASK(30, 0), + .wake_names = t8112_wake_names, + .nwake_names = ARRAY_SIZE(t8112_wake_names), + .wake_required = t8112_wake_required, + .nwake_required = ARRAY_SIZE(t8112_wake_required), + .ps_aon_debug_switch = T8112_AON_DEBUG_SWITCH, + .ps = { + .disp = { { T8112_PS_DISP0_CPU0, T8112_PS_DISP0_FE, T8112_PS_DISP0_SYS } }, + .sio_cpu = T8112_PS_SIO_CPU, + .sep = T8112_PS_SEP, + .gpio = T8112_PS_GPIO, + .apcie_gp = T8112_PS_APCIE_GP, + .apcie_st = T8112_PS_APCIE_ST, + }, + .ps_all = SLEEP_WORDS(t8112_ps_all), + .ps_aon_all = SLEEP_WORDS(t8112_ps_aon_all), + .ps_wake_order = SLEEP_WORDS(t8112_ps_wake_order), + .ps_aon_wake_order = SLEEP_WORDS(t8112_ps_aon_wake_order), + .gate_coprocs = t8112_gate_coprocs, + .ngate_coprocs = ARRAY_SIZE(t8112_gate_coprocs), +}; diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index 6c878accb5597..7518398a2f933 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -14,6 +14,7 @@ static const char *const apple_sleep_test_profiles[] = { "apple,t6050-sleep", "apple,t6050-j716c-sleep", "apple,t8103-sleep", + "apple,t8112-sleep", "apple,t8122-sleep", }; @@ -72,6 +73,7 @@ static void apple_sleep_test_known_profiles(struct kunit *test) KUNIT_ASSERT_LT(test, n, ARRAY_SIZE(apple_sleep_test_profiles)); KUNIT_EXPECT_STREQ(test, id->compatible, apple_sleep_test_profiles[n]); if (!strcmp(id->compatible, "apple,t8142-sleep") || + !strcmp(id->compatible, "apple,t8112-sleep") || !strcmp(id->compatible, "apple,t8122-sleep")) { const struct apple_sleep_soc *soc = id->data; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c385bf1e58b4e..a9ad1a2c7cf8e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -10179,6 +10179,7 @@ static const struct apple_sleep_soc t8142_sleep_soc = { #include "sleep-t8103.h" #include "sleep-t8122.h" +#include "sleep-t8112.h" /* * Qualified profiles expose system sleep. Inventory-only profiles match for a @@ -10193,6 +10194,7 @@ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t6050-sleep", .data = &t6050_sleep_soc }, { .compatible = "apple,t6050-j716c-sleep", .data = &t6050_j716c_sleep_soc }, { .compatible = "apple,t8103-sleep", .data = &t8103_sleep_soc }, + { .compatible = "apple,t8112-sleep", .data = &t8112_sleep_soc }, { .compatible = "apple,t8122-sleep", .data = &t8122_sleep_soc }, { } }; diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index c9e486f5d79d1..6f0ad99257761 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -211,6 +211,20 @@ static const struct apple_cio_hw apple_cio_t8103_hw = { .dp = &apple_dpin_m1, }; +/* + * T8112 (M2) shares the T8103 ACIO register layout and DP IN policy, while + * deep sleep removes the CIO power domains and shares the ATC PHY with DWC3. + * Tear down a connected router before tasks freeze (ahead of DWC3's PM + * notifier) and request fresh cable state immediately on wake; s2idle and + * freeze leave the port up. + */ +static const struct apple_cio_hw apple_cio_t8112_hw = { + .sleep_removes_domains = true, + .restart_on_wake = true, + .phy_shared_with_dwc3 = true, + .dp = &apple_dpin_m1, +}; + /* * T8122 shares the T8103 ACIO register layout and DP IN policy, while deep * sleep removes the CIO power domains and shares the ATC PHY with DWC3. Tear @@ -5202,6 +5216,10 @@ static const struct of_device_id apple_acio_match[] = { .compatible = "apple,t8122-usb4-acio", .data = &apple_cio_t8122_hw, }, + { + .compatible = "apple,t8112-usb4-acio", + .data = &apple_cio_t8112_hw, + }, { .compatible = "apple,t6020-usb4-acio", .data = &apple_cio_t6020_hw, diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index f059dde8cf6b6..e98bc0d3d69a6 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -1408,10 +1408,12 @@ def test_pm_order(out, acio): ''' -def test_t8122_cio_sleep(out, acio): +def test_shared_phy_cio_sleep(out, acio, soc): dwc = (ROOT / 'drivers/usb/dwc3/dwc3-apple.c').read_text() + hw_sym = f'apple_cio_{soc}_hw' + compat = f'apple,{soc}-usb4-acio' hw_defs = [] - for sym in ('apple_cio_t8103_hw', 'apple_cio_t8122_hw', 'apple_cio_t6020_hw'): + for sym in ('apple_cio_t8103_hw', hw_sym, 'apple_cio_t6020_hw'): m = re.search(r'^static const struct apple_cio_hw ' + sym + r' = \{.*?\n\};', acio, re.M | re.S) assert m, sym @@ -1424,10 +1426,9 @@ def test_t8122_cio_sleep(out, acio): r'\.compatible\s*=\s*"([^"]+)"\s*,\s*\.data\s*=\s*(&apple_cio_\w+_hw)', match_block.group()) match_lines = [f' {{ "{c}", {d} }},' for c, d in entries - if c in ('apple,t8122-usb4-acio', 'apple,t6020-usb4-acio', - 'apple,t8103-usb4-acio')] + if c in (compat, 'apple,t6020-usb4-acio', 'apple,t8103-usb4-acio')] assert len(match_lines) == 3, match_lines - code = T8122_SLEEP_FIXTURE + code = T8122_SLEEP_FIXTURE.replace('t8122', soc).replace('T8122', soc.upper()) code = code.replace('/* PRODUCTION_HW */', '\n'.join(hw_defs)) code = code.replace('/* MATCH_ENTRIES */', '\n'.join(match_lines)) code = code.replace('/* PRODUCTION_FN */', @@ -1439,30 +1440,32 @@ def test_t8122_cio_sleep(out, acio): dwc, 'appledwc->pm_nb.notifier_call = dwc3_apple_pm_notify;', 'ret = register_pm_notifier(&appledwc->pm_nb);')) muts = { - 't8122-drop-match': (' { "apple,t8122-usb4-acio", &apple_cio_t8122_hw },', ''), - 't8103-drop-sleep-removes-domains': ( - 'static const struct apple_cio_hw apple_cio_t8103_hw = {\n' - '\t.sleep_removes_domains = true,\n', - 'static const struct apple_cio_hw apple_cio_t8103_hw = {\n'), - 't8122-drop-sleep-removes-domains': ( - 'static const struct apple_cio_hw apple_cio_t8122_hw = {\n' + f'{soc}-drop-match': (f' {{ "{compat}", &{hw_sym} }},', ''), + f'{soc}-drop-sleep-removes-domains': ( + f'static const struct apple_cio_hw {hw_sym} = {{\n' '\t.sleep_removes_domains = true,\n', - 'static const struct apple_cio_hw apple_cio_t8122_hw = {\n'), + f'static const struct apple_cio_hw {hw_sym} = {{\n'), 't6020-drop-sleep-removes-domains': ( 'static const struct apple_cio_hw apple_cio_t6020_hw = {\n' '\t.sleep_removes_domains = true,\n', 'static const struct apple_cio_hw apple_cio_t6020_hw = {\n'), 't6020-drop-displays-first': ('\t.sleep_displays_first = true,\n', ''), - 't8122-drop-phy-shared': ( - 'static const struct apple_cio_hw apple_cio_t8122_hw = {\n' + f'{soc}-drop-phy-shared': ( + f'static const struct apple_cio_hw {hw_sym} = {{\n' '\t.sleep_removes_domains = true,\n' '\t.restart_on_wake = true,\n' '\t.phy_shared_with_dwc3 = true,\n', - 'static const struct apple_cio_hw apple_cio_t8122_hw = {\n' + f'static const struct apple_cio_hw {hw_sym} = {{\n' '\t.sleep_removes_domains = true,\n' '\t.restart_on_wake = true,\n'), } - cases = {'t8122-cio-sleep': code} + if soc == 't8122': + muts['t8103-drop-sleep-removes-domains'] = ( + 'static const struct apple_cio_hw apple_cio_t8103_hw = {\n' + '\t.sleep_removes_domains = true,\n', + 'static const struct apple_cio_hw apple_cio_t8103_hw = {\n') + base_name = f'{soc}-cio-sleep' + cases = {base_name: code} for name, (old, new) in muts.items(): assert code.count(old) == 1, name cases[name] = code.replace(old, new) @@ -1473,7 +1476,7 @@ def test_t8122_cio_sleep(out, acio): 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 == 't8122-cio-sleep': + if name == base_name: assert run.returncode == 0, run.stderr print(run.stdout, end='') else: @@ -1841,7 +1844,8 @@ def main(): out = args.out or Path(tmp) out.mkdir(parents=True, exist_ok=True) test_pm_order(out, acio) - test_t8122_cio_sleep(out, acio) + test_shared_phy_cio_sleep(out, acio, 't8122') + test_shared_phy_cio_sleep(out, acio, 't8112') test_cio_stop(out, acio) cases = { 'production': code,