diff --git a/Documentation/devicetree/bindings/mfd/apple,smc.yaml b/Documentation/devicetree/bindings/mfd/apple,smc.yaml index ca3c28e2f2556..156435f126932 100644 --- a/Documentation/devicetree/bindings/mfd/apple,smc.yaml +++ b/Documentation/devicetree/bindings/mfd/apple,smc.yaml @@ -69,6 +69,19 @@ properties: what the SoC's sleep sequence powers on itself. Without the property pmVC is not written. + apple,sleep-low-pins: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 1 + maxItems: 4 + items: + maximum: 0xff + description: + SMC GPIO numbers (the number in the GPIO's gPxx key) that are driven + low before the SMC is told of a suspend-to-RAM and high again after the + wake, for a supply that has to be off while the machine sleeps and that + no driver switches at that point. On the J314s this is GPIO 26, the SD + card reader's power enable. + gpio: oneOf: - $ref: /schemas/gpio/apple,smc-gpio.yaml diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index ad24b561a8d5b..622495655a4a6 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml @@ -19,6 +19,7 @@ description: | properties: compatible: enum: + - apple,t6000-sleep - apple,t8103-sleep - apple,t8112-sleep - apple,t8122-sleep @@ -125,6 +126,42 @@ required: - apple,display-coprocessors allOf: + - if: + properties: + compatible: + contains: + const: apple,t6000-sleep + then: + required: + - apple,wake-enable + properties: + reg: + minItems: 22 + maxItems: 22 + reg-names: + items: + - const: ps + - const: ps-east + - const: ps-aon + - const: cpu-start + - const: cluster + - const: commit + - const: wdt + - const: wake + - const: sep-pg + - const: acc0 + - const: acc1 + - const: acc2 + - const: clkgen + - const: pll0 + - const: dcs + - const: apcie-q + - const: amcc0 + - const: amcc1 + - const: amcc2 + - const: amcc3 + - const: ptd + - const: spmi - if: properties: compatible: diff --git a/arch/arm64/boot/dts/apple/t6000-j314s.dts b/arch/arm64/boot/dts/apple/t6000-j314s.dts index 763a99dba58b0..d8e377d7b30b1 100644 --- a/arch/arm64/boot/dts/apple/t6000-j314s.dts +++ b/arch/arm64/boot/dts/apple/t6000-j314s.dts @@ -77,6 +77,47 @@ adj-height-mm = <189>; }; +/* + * Suspend-to-RAM, qualified on this board. What follows is what its sequence + * needs from the other devices. + */ +&sleep { + /* + * Left alone across a sleep, as in the board's ADT no-restore-gpio-pins. + */ + apple,pinctrl-skip-pins = <114 115 119>; + status = "okay"; +}; + +&smc { + /* VDD_CIO rail mask before the sleep notice: no port's rail kept up */ + apple,sleep-pmvc = <0x100>; + /* The SD card reader's power enable (port01's pwren), off while asleep */ + apple,sleep-low-pins = <26>; +}; + +&pmp { + /* The sleep takes the firmware's SRAM: all of it is kept and written back */ + apple,sram-keep = <0x0 0x100000>; +}; + +/* macOS moves each port controller to S3 before the sleep and to S0 after it. */ +&hpm0 { + apple,sleep-ssps; +}; + +&hpm1 { + apple,sleep-ssps; +}; + +&hpm2 { + apple,sleep-ssps; +}; + +&hpm5 { + apple,sleep-ssps; +}; + &aop_audio { apple,chassis-name = "J314"; apple,machine-kind = "MacBook Pro"; diff --git a/arch/arm64/boot/dts/apple/t600x-die0.dtsi b/arch/arm64/boot/dts/apple/t600x-die0.dtsi index 6d14d3e3f9701..95ac6ce1fbcc1 100644 --- a/arch/arm64/boot/dts/apple/t600x-die0.dtsi +++ b/arch/arm64/boot/dts/apple/t600x-die0.dtsi @@ -121,6 +121,50 @@ #clock-cells = <1>; }; + /* + * The registers of a suspend-to-RAM, in blocks other drivers also use. + * Only qualified on T6000: a board that sleeps enables it. + */ + sleep: sleep-controller@28e080000 { + compatible = "apple,t6000-sleep"; + reg = <0x2 0x8e080000 0x0 0x1000>, + <0x2 0x8e580000 0x0 0x1000>, + <0x2 0x92280000 0x0 0x1000>, + <0x2 0x8e0d4000 0x0 0x14>, + <0x2 0x8e098000 0x0 0x24>, + <0x2 0x925c4000 0x0 0x4>, + <0x2 0x922b0000 0x0 0x40>, + <0x2 0x92004000 0x0 0x40>, + <0x2 0x8e0cc200 0x0 0x4>, + <0x2 0x10e20000 0x0 0x29000>, + <0x2 0x11e20000 0x0 0x29000>, + <0x2 0x12e20000 0x0 0x29000>, + <0x2 0x8e038000 0x0 0x200>, + <0x2 0x8e224000 0x0 0x4>, + <0x2 0x8e20c800 0x0 0x4>, + <0x5 0x8c008000 0x0 0x300>, + <0x2 0x00000000 0x0 0x160000>, + <0x2 0x01000000 0x0 0x160000>, + <0x2 0x02000000 0x0 0x160000>, + <0x2 0x03000000 0x0 0x160000>, + <0x2 0x8e3c0000 0x0 0x19000>, + <0x2 0x920a1300 0x0 0x100>; + reg-names = "ps", "ps-east", "ps-aon", + "cpu-start", "cluster", "commit", + "wdt", "wake", "sep-pg", + "acc0", "acc1", "acc2", + "clkgen", "pll0", "dcs", "apcie-q", + "amcc0", "amcc1", "amcc2", "amcc3", + "ptd", "spmi"; + apple,wake-enable = <12 14 26 27 + 40 43 49 50 51 52 54 + 85 90 + 126 127 128 129 130 131 132 133>; + apple,display-coprocessors = <&dcp>; + apple,pinctrl = <&pinctrl_ap>; + status = "disabled"; + }; + aic: interrupt-controller@28e100000 { compatible = "apple,t6000-aic", "apple,aic2"; #interrupt-cells = <4>; diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index 81a7ee1da1224..139958309944b 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -729,6 +729,16 @@ static const struct sio_hw_data sio_hw_generic = { .cfg_word4 = 0x800, }; +/* + * The generic words, and stopped by the sleep's commit where the board has a sleep + * controller: idled any earlier, a sleep refused after that would leave it stopped. + */ +static const struct sio_hw_data sio_hw_t6000 = { + .stop_at_commit = true, + .fifo_threshold = 0x800, + .cfg_word4 = 0x800, +}; + static const struct sio_hw_data sio_hw_t6030 = { .stop_at_commit = true, .commit_no_cpu_down = true, @@ -815,6 +825,7 @@ static const struct of_device_id sio_of_match[] = { */ { .compatible = "apple,t8122-sio", .data = &sio_hw_t8140 }, { .compatible = "apple,t6030-sio", .data = &sio_hw_t6030 }, + { .compatible = "apple,t6000-sio", .data = &sio_hw_t6000 }, { .compatible = "apple,sio", .data = &sio_hw_generic }, { } }; @@ -3314,6 +3325,10 @@ static __maybe_unused int sio_suspend_late(struct device *dev) return -EBUSY; } + /* A dry run ends before the commit: the firmware would be stopped for nothing. */ + if (apple_sleep_dry_run()) + return 0; + ret = apple_rtkit_sleep_stop(sio->rtk, sio->base + REG_CPU_CONTROL); if (ret) { dev_err(dev, "firmware did not stop for sleep: %d\n", ret); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 9fcc4d697000d..111bb93b3232e 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -431,6 +431,11 @@ struct apple_dcp_hw_data { */ bool s2r; bool s2r_ext; + /* + * Firmware 13.5: a firmware-driven sleep takes the internal processor's power. It is + * stopped before the sleep and booted from scratch after it, see dcp_sleep_13_5_stop(). + */ + bool sleep_13_5; /* An external display processor (a dcpext block), not the internal display's */ bool external_processor; /* The firmware crashes on any mode but the 120 Hz boot timing, see dcp_mode_settable(). */ @@ -1482,6 +1487,8 @@ struct apple_dcp { struct gpio_desc *hdmi_hpd; struct gpio_desc *hdmi_pwren; struct gpio_desc *dp2hdmi_pwren; + /* The converter was powered down for a firmware sleep: resume powers it up again. */ + bool dp2hdmi_sleep_off; struct mutex hpd_mutex; @@ -1699,6 +1706,7 @@ int iomfb_queue_callback(struct apple_dcp *dcp, dcp_callback_t callback, void iomfb_queue_init(struct apple_dcp *dcp); void iomfb_queue_stop(struct apple_dcp *dcp); void iomfb_reset_transport(struct apple_dcp *dcp); +void iomfb_channels_reset(struct apple_dcp *dcp); bool iomfb_queue_drain(struct apple_dcp *dcp, unsigned long timeout); int iomfb_queue_settle(struct apple_dcp *dcp, unsigned long timeout); void iomfb_pause_presentation(struct apple_dcp *dcp, bool pause); diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 83aa944329063..5971179bb632e 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -13304,6 +13304,38 @@ bool dcp_sleep_keeps_firmware(struct device *dev) } EXPORT_SYMBOL_GPL(dcp_sleep_keeps_firmware); +/* + * hw.sleep_13_5: A500, as macOS sends it, then the firmware is parked and its CPU stopped. + * Not in a dry run. Returns whether the firmware was dealt with here. + */ +static bool dcp_sleep_13_5_stop(struct apple_dcp *dcp) +{ + int ret; + + if (!dcp->hw.sleep_13_5 || !pm_suspend_via_firmware() || apple_sleep_dry_run() || + dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || + IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg) + return false; + /* Not started since an earlier sleep stopped it: a sleep that happens starts it. */ + if (READ_ONCE(dcp->s2r_fw_down)) { + dcp->sleep_fw_runs = false; + return true; + } + if (READ_ONCE(dcp->crashed) || !apple_rtkit_is_running(dcp->rtk)) + return false; + + ret = iomfb_s2r_500_v13_3(dcp); + if (ret) + dev_warn(dcp->dev, "A500 before the sleep: %d\n", ret); + ret = apple_rtkit_sleep_stop(dcp->rtk, dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL); + if (ret) + dev_warn(dcp->dev, "firmware did not stop for the sleep: %d\n", ret); + else + WRITE_ONCE(dcp->s2r_fw_down, true); + dcp->sleep_fw_runs = false; + return true; +} + static int dcp_platform_suspend(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -13540,6 +13572,11 @@ static int dcp_platform_suspend(struct device *dev) */ dcp_hdmi_suspend(dcp); } + /* The converter draws power through a suspend-to-RAM; macOS has it off. */ + if (dcp->dp2hdmi_pwren && pm_suspend_via_firmware()) { + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 0); + dcp->dp2hdmi_sleep_off = true; + } /* * Set the device as a wakeup device, which forces its power * domains to stay on. We need this as we do not support full @@ -13548,6 +13585,8 @@ static int dcp_platform_suspend(struct device *dev) dcp->sleep_fw_runs = true; device_set_wakeup_path(dev); dcp_sleep_hold_phys(dcp); + if (dcp_sleep_13_5_stop(dcp)) + return 0; /* Last: the steps above still talk to the coprocessor. */ dcp_hold_for_sleep(dcp); dcp_commit_stop_arm(dcp); @@ -13561,9 +13600,67 @@ void dcp_hdmi_disconnect(struct apple_dcp *dcp) dcp_dptx_disconnect(dcp, 0); } +/* + * hw.sleep_13_5: a real sleep took the firmware's power, and it starts as from a wake. A + * refused sleep left it parked with its power kept, after A500: it neither starts from that + * nor completes a page flip again. A panel's firmware is then given the power loss of a real + * sleep first, its CPU domain off and on again through the sleep driver. Its DARTs keep + * their registers across that, and nothing maps through them meanwhile: the display + * subsystem resumes after this device. + * + * A firmware that does not start is left stopped, as dcp_sleep_13_5_stop() left it, and + * marked crashed, so that nothing calls it: its display stays dark, and the next sleep that + * happens starts it again from here. Returns whether the firmware runs. + */ +static bool dcp_sleep_13_5_start(struct apple_dcp *dcp) +{ + void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + bool real = pm_resume_via_firmware(); + int ret; + + if (!dcp->hw.sleep_13_5 || dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || + !READ_ONCE(dcp->s2r_fw_down)) + return true; + + if (dcp_has_panel(dcp) && !real) { + ret = apple_sleep_display_power_cycle(dcp->dev); + /* Not started, so still stopped: after -EIO its registers may have no power. */ + if (ret) { + dev_err(dcp->dev, "no power cycle after the refused sleep: %d; the display stays dark until %s\n", + ret, ret == -EIO ? "a reboot" : "a sleep that happens"); + WRITE_ONCE(dcp->crashed, true); + return false; + } + } + + /* No call of the last session is answered by this one. */ + iomfb_channels_reset(dcp); + if (dcp_has_panel(dcp)) + ret = apple_rtkit_sleep_return_with_init(dcp->rtk, control); + else + ret = apple_rtkit_sleep_return(dcp->rtk, control); + if (ret) { + dev_err(dcp->dev, "firmware did not start after the %s: %d; the display stays dark until a sleep that happens\n", + real ? "sleep" : "refused sleep", ret); + WRITE_ONCE(dcp->crashed, true); + apple_rtkit_mailbox_off(dcp->rtk); + /* Twice, as apple_rtkit_sleep_stop() clears it. */ + writel(readl(control) & ~APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + writel(readl(control) & ~APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + return false; + } + WRITE_ONCE(dcp->crashed, false); + WRITE_ONCE(dcp->s2r_fw_down, false); + if (!real) + dev_info(dcp->dev, "sleep refused: firmware restarted after a power cycle\n"); + + return true; +} + static int dcp_platform_resume(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); + bool fw_runs; dcp->sleep_fw_runs = false; /* Still down: the display subsystem's resume, after this one, starts this DCP. */ @@ -13600,6 +13697,14 @@ static int dcp_platform_resume(struct device *dev) if (dcp->s2r_stopped) return dcp_s2r_resume(dcp); + /* The converter first, so that it is up by the time HPD is sampled below. */ + if (dcp->dp2hdmi_sleep_off) { + dcp->dp2hdmi_sleep_off = false; + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 1); + } + + fw_runs = dcp_sleep_13_5_start(dcp); + if (iomfb_is_m4(dcp)) { int ret = iomfb_m4_resume(dcp); @@ -13641,7 +13746,7 @@ static int dcp_platform_resume(struct device *dev) if (dcp->external_native) enable_work(&dcp->external_audio_work); - if (dcp->avep) + if (dcp->avep && fw_runs) av_service_connect(dcp); if (dcp_ext_idle_capable(dcp)) @@ -13784,6 +13889,12 @@ static const struct apple_dcp_hw_data apple_dcp_hw_dcp = { .num_dptx_ports = 0, }; +/* T6000's DCP: as the generic one, with the firmware 13.5 stop around a suspend-to-RAM */ +static const struct apple_dcp_hw_data apple_dcp_hw_t6000 = { + .num_dptx_ports = 0, + .sleep_13_5 = true, +}; + static const struct apple_dcp_hw_data apple_dcp_hw_t6030 = { .iboot_handoff = "apple,t6030-handoff", .num_dptx_ports = 1, @@ -13947,6 +14058,7 @@ static const struct of_device_id of_match[] = { { .compatible = "apple,t6041-dcpext", .data = &apple_dcp_hw_t6041_dcpext, }, { .compatible = "apple,t6050-dcp", .data = &apple_dcp_hw_t6050 }, { .compatible = "apple,t6050-dcpext", .data = &apple_dcp_hw_t6050_dcpext }, + { .compatible = "apple,t6000-dcp", .data = &apple_dcp_hw_t6000, }, { .compatible = "apple,t6020-dcp", .data = &apple_dcp_hw_t6020, }, { .compatible = "apple,t6020-dcpext", .data = &apple_dcp_hw_t6020_dcpext, }, { .compatible = "apple,t8103-dcp", .data = &apple_dcp_hw_t8103, }, diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index fb2411fc3a50d..a60d77a071344 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -757,6 +757,40 @@ void iomfb_reset_transport(struct apple_dcp *dcp) iomfb_channels_idle_wake(dcp); } +/* + * The call stacks of a firmware without the queue, emptied before it starts again, with its + * mailbox off. A call the last session left unanswered would stay on its channel: every + * command after it then goes out on the callback context (dcp_call_context()), and an ack + * can be paired with a slot whose record is gone. Its callback is not run, as that may send + * the next call of a sequence: what waited for it has timed out, and its cookie is not + * released. + */ +void iomfb_channels_reset(struct apple_dcp *dcp) +{ + struct dcp_channel *channels[] = { + &dcp->ch_cmd, &dcp->ch_oobcmd, &dcp->ch_cb, + &dcp->ch_oobcb, &dcp->ch_async, &dcp->ch_oobasync, + }; + unsigned int i, dropped = 0; + + if (iomfb_uses_queue(dcp)) + return; + + for (i = 0; i < ARRAY_SIZE(channels); i++) { + struct dcp_channel *ch = channels[i]; + + dropped += ch->depth; + memset(ch->callbacks, 0, sizeof(ch->callbacks)); + memset(ch->cookies, 0, sizeof(ch->cookies)); + memset(ch->output, 0, sizeof(ch->output)); + WRITE_ONCE(ch->depth, 0); + ch->warned_busy = false; + } + if (dropped) + dev_warn(dcp->dev, "%u calls the firmware never answered dropped at its restart\n", + dropped); +} + void iomfb_queue_stop(struct apple_dcp *dcp) { struct iomfb_callback *item; @@ -3088,6 +3122,13 @@ static void dcpep_handle_ack(struct apple_dcp *dcp, enum dcp_context_id context) dev_warn(dcp->dev, "ignoring ack on idle context %X\n", context); return; } + /* Its slot would be past the call stack. */ + if (ch->depth > DCP_MAX_CALL_DEPTH) { + dev_err(dcp->dev, "call stack of context %X is %u deep: ack dropped\n", + context, ch->depth); + WRITE_ONCE(dcp->crashed, true); + return; + } dcp_pop_depth(&ch->depth); iomfb_channels_idle_wake(dcp); @@ -3101,6 +3142,13 @@ static void dcpep_handle_ack(struct apple_dcp *dcp, enum dcp_context_id context) ch->cookies[ch->depth] = NULL; ch->output[ch->depth] = NULL; + /* dcp_push() saves a record's address with every command: this slot holds none. */ + if (!out) { + dev_warn(dcp->dev, "ignoring ack for an empty call slot on context %X\n", + context); + return; + } + /* * An ack completes the innermost pending command on its context. The * firmware writes the reply in place into that AP command record, whose diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index 46f0a62770e38..4e4bcc35d358d 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -2979,6 +2979,44 @@ int DCP_FW_NAME(iomfb_system_power)(struct apple_dcp *dcp, bool on) } #endif +#if DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) +DCP_THUNK_IN(dcp_unnamed_500, dcpep_unnamed_500, u32); + +static void dcp_s2r_500_done(struct apple_dcp *dcp, void *out, void *cookie) +{ + struct dcp_wait_cookie *wait = cookie; + + complete(&wait->done); + kref_put(&wait->refcount, release_wait_cookie); +} + +/* + * A500 on firmware 13.5, sent before the firmware is stopped for a suspend-to-RAM. The + * argument is zero, as T6000's sleep was brought up; H17P and 26.6 send 0xaaaa0000. + */ +int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp) +{ + struct dcp_wait_cookie *cookie; + unsigned long left; + u32 val = 0; + + cookie = kzalloc_obj(*cookie); + if (!cookie) + return -ENOMEM; + init_completion(&cookie->done); + kref_init(&cookie->refcount); + /* increase refcount to ensure the receiver has a reference */ + kref_get(&cookie->refcount); + + dcp_unnamed_500(dcp, false, &val, dcp_s2r_500_done, cookie); + + left = wait_for_completion_timeout(&cookie->done, msecs_to_jiffies(2000)); + kref_put(&cookie->refcount, release_wait_cookie); + + return left ? 0 : -ETIMEDOUT; +} +#endif + static bool dcp_power_stop_confirmed(struct dcp_wait_cookie *cookie) { /* H17P does not confirm that scanout has stopped. */ diff --git a/drivers/gpu/drm/apple/iomfb_template.h b/drivers/gpu/drm/apple/iomfb_template.h index 2cd64bffc3863..68c5ddac262da 100644 --- a/drivers/gpu/drm/apple/iomfb_template.h +++ b/drivers/gpu/drm/apple/iomfb_template.h @@ -394,7 +394,7 @@ void DCP_FW_NAME(iomfb_poweroff)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_sleep)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_start)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_shutdown)(struct apple_dcp *dcp); -#if DCP_FW_DESKTOP_26(DCP_FW_VER) +#if DCP_FW_DESKTOP_26(DCP_FW_VER) || DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp); int DCP_FW_NAME(iomfb_system_power)(struct apple_dcp *dcp, bool on); #endif diff --git a/drivers/gpu/drm/apple/iomfb_v13_3.c b/drivers/gpu/drm/apple/iomfb_v13_3.c index 3521abccf038e..b9d77950bf349 100644 --- a/drivers/gpu/drm/apple/iomfb_v13_3.c +++ b/drivers/gpu/drm/apple/iomfb_v13_3.c @@ -39,6 +39,8 @@ static const struct dcp_method_entry dcp_methods[dcpep_num_methods] = { IOMFB_METHOD("A466", dcpep_flush_supports_power), IOMFB_METHOD("A467", iomfbep_abort_swaps_dcp), IOMFB_METHOD("A472", dcpep_set_power_state), + /* Unnamed here as on the later firmwares, see iomfb_s2r_500_v13_3(). */ + IOMFB_METHOD("A500", dcpep_unnamed_500), }; #define DCP_FW v13_3 diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index 31227d28e035b..994ad63bc98db 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -62,6 +62,9 @@ pub(crate) struct AsahiPlatformDriverData<'bound> { raw_stats: *mut core::ffi::c_void, // Own the allocation until unregister has drained all sysfs readers. stats: Option>, + /// The first-generation driver's GPU and its ASC block, for [`legacy_sleep`]. + legacy_gpu: Option>, + legacy_asc: Option>, } // SAFETY: `raw_dev` is only passed back to the sysfs shim, which serializes its own state. @@ -288,10 +291,81 @@ unsafe extern "C" fn sleep_prepare(dev: *mut bindings::device) -> core::ffi::c_i } match data.g17.as_ref() { Some(gpu) => kernel::error::from_result(|| gpu.sleep_prepare().map(|_| 0)), + // A GPU whose firmware is parked at .suspend and restarted at .resume has nothing to + // refuse here. + None if legacy_sleep(data).is_some() => 0, None => EBUSY.to_errno(), } } +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +extern "C" { + fn apple_sleep_dry_run() -> bool; +} + +/// Whether this firmware-driven sleep stops before its commit. +fn sleep_dry_run() -> bool { + #[cfg(CONFIG_APPLE_SOC_SLEEP)] + // SAFETY: Takes no arguments and reads the sleep driver's state. + return unsafe { apple_sleep_dry_run() }; + #[cfg(not(CONFIG_APPLE_SOC_SLEEP))] + false +} + +/// The offset of the ASC's CPU control word in its `asc` block. +const ASC_CPU_CONTROL_OFFSET: usize = 0x44; + +/// The GPU and its ASC CPU control word, where the firmware is parked for a firmware-driven +/// sleep and restarted after it ([`hw::HwConfig::sleep_restart`]). +fn legacy_sleep<'a>( + data: &'a AsahiPlatformDriverData<'_>, +) -> Option<(&'a Arc, *mut core::ffi::c_void)> { + let gpu = data.legacy_gpu.as_ref()?; + let asc = data.legacy_asc.as_ref()?; + + if !gpu.sleep_supported() { + return None; + } + let control = + ::as_ptr(kernel::io::IoBase::as_view(asc)) + .cast::() + .wrapping_add(ASC_CPU_CONTROL_OFFSET) + .cast::(); + Some((gpu, control)) +} + +/// Parks the firmware for a sleep that takes its power. Not in a dry run: a parked firmware +/// only comes back through a real sleep. +unsafe extern "C" fn sleep_suspend(dev: *mut bindings::device) -> core::ffi::c_int { + // SAFETY: Reads a global flag word. + if !unsafe { bindings::pm_suspend_via_firmware() } || sleep_dry_run() { + return 0; + } + // SAFETY: Called by the PM core. + let data = unsafe { sleep_data(dev) }; + match legacy_sleep(data) { + Some((gpu, control)) => { + let ret = kernel::error::from_result(|| gpu.sleep_suspend(control).map(|_| 0)); + // SAFETY: Probe attached the hold state to this device. + unsafe { asahi_sleep_pd_hold_deep(dev.cast(), ret == 0 && gpu.sleep_commit_pending()) }; + ret + } + None => 0, + } +} + +/// Runs once the power domains and the PMP are back, before user space thaws. +unsafe extern "C" fn sleep_resume(dev: *mut bindings::device) -> core::ffi::c_int { + // SAFETY: Called by the PM core. + let data = unsafe { sleep_data(dev) }; + if let Some((gpu, control)) = legacy_sleep(data) { + gpu.sleep_resume(control); + // SAFETY: As in `sleep_suspend()`. + unsafe { asahi_sleep_pd_hold_deep(dev.cast(), false) }; + } + 0 +} + extern "C" { fn asahi_sleep_pd_init(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_sleep_pd_suspend_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; @@ -300,6 +374,7 @@ extern "C" { hold_firmware: bool, ) -> core::ffi::c_int; fn asahi_sleep_pd_resume_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; + fn asahi_sleep_pd_hold_deep(dev: *mut core::ffi::c_void, hold: bool); } /// Allocates per-device sleep power-domain hold state tied to `dev`'s binding. @@ -368,6 +443,8 @@ unsafe extern "C" fn sleep_complete(dev: *mut bindings::device) { static SLEEP_OPS: SleepOps = SleepOps(bindings::dev_pm_ops { prepare: Some(sleep_prepare), complete: Some(sleep_complete), + suspend: Some(sleep_suspend), + resume: Some(sleep_resume), suspend_noirq: Some(sleep_suspend_noirq_driver), resume_noirq: Some(sleep_resume_noirq), // SAFETY: All-zero is the absence of every other callback. @@ -528,6 +605,10 @@ impl platform::Driver for AsahiPlatformDriver { }; gpu.init()?; + let legacy_gpu = Some(gpu.clone()); + let legacy_asc = pdev + .io_request_by_name(c"asc") + .and_then(|req| req.iomap_sized::<{ regs::ASC_CTL_SIZE }>().ok()); let stats = gpu.stats_snapshot(); let debug_stats = gpu.raw_stats_snapshot(); let data = try_pin_init!(AsahiDrmRegistrationData { gpu, resources }); @@ -566,6 +647,8 @@ impl platform::Driver for AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu, + legacy_asc, }; Ok(driver) @@ -610,6 +693,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } @@ -656,6 +741,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu: None, + legacy_asc: None, }) } @@ -712,6 +799,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu: None, + legacy_asc: None, }) } @@ -797,6 +886,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -846,6 +937,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -887,6 +980,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats: None, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -946,6 +1041,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }), }) } diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index 8da8a428ad72a..05ac26fb192f0 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -16,6 +16,7 @@ use core::ops::Range; use core::slice; use core::sync::atomic::{ AtomicBool, + AtomicPtr, AtomicU32, AtomicU64, Ordering, // @@ -165,6 +166,106 @@ pub(crate) const IOVA_KERN_GPU_BUFMGR_HIGH: u64 = 0xffffffaeffff0000; /// Timeout for entering the halt state after a fault or request. const HALT_ENTER_TIMEOUT: Delta = Delta::from_millis(100); +/// The run bit of an ASC's CPU control word. +const SLEEP_ASC_RUN: u32 = 0x10; + +/// What parks the firmware at the commit of a firmware-driven sleep. +pub(crate) struct SleepCommit { + rtk: *mut kernel::bindings::apple_rtkit, + control: *mut core::ffi::c_void, + ran: bool, + ret: core::ffi::c_int, +} + +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +#[allow(improper_ctypes)] +unsafe extern "C" { + fn apple_sleep_coproc_register_first( + dev: *mut kernel::bindings::device, + last_stop: Option core::ffi::c_int>, + data: *mut core::ffi::c_void, + ) -> core::ffi::c_int; + fn apple_sleep_coproc_set_ready(dev: *mut kernel::bindings::device, ready: bool); + fn apple_sleep_coproc_stopped(dev: *mut kernel::bindings::device) -> bool; + fn apple_sleep_coproc_unregister(dev: *mut kernel::bindings::device); +} + +/// Interrupts off, one CPU, every refusal behind it: what `apple_rtkit_sleep_stop()` does, +/// polled. It can't refuse: the sleep goes on whatever it returns. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe extern "C" fn sleep_commit_stop(data: *mut core::ffi::c_void) -> core::ffi::c_int { + // SAFETY: `data` is the `SleepCommit` registered with this callback, allocated until + // `sleep_commit_unregister()` has returned. + let c = unsafe { &mut *data.cast::() }; + let mut us = 0u32; + + c.ran = true; + // SAFETY: `rtk` and `control` belong to the bound GPU, which the PM core keeps bound + // across the sleep. + unsafe { + c.ret = kernel::bindings::apple_rtkit_sleep_idle_polled(c.rtk, c.control, &mut us); + kernel::bindings::apple_rtkit_mailbox_recv_off(c.rtk); + // Twice, each time from what it reads back. + for _ in 0..2 { + let ctl = kernel::bindings::readl(c.control); + kernel::bindings::writel(ctl & !SLEEP_ASC_RUN, c.control); + } + } + c.ret +} + +/// Leaves the firmware's stop to the commit of the sleep under way. +/// +/// # Safety +/// +/// `dev` must be the bound GPU device, and `data` must stay allocated until +/// [`sleep_commit_unregister`] has returned. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe fn sleep_commit_register( + dev: *mut kernel::bindings::device, + data: *mut SleepCommit, +) -> core::ffi::c_int { + // SAFETY: Per the caller. + let ret = + unsafe { apple_sleep_coproc_register_first(dev, Some(sleep_commit_stop), data.cast()) }; + if ret == 0 { + // SAFETY: Registered just above. + unsafe { apple_sleep_coproc_set_ready(dev, true) }; + } + ret +} + +/// Takes the stop back. Returns whether the commit ran it. +/// +/// # Safety +/// +/// `dev` must have been registered with [`sleep_commit_register`]. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe fn sleep_commit_unregister(dev: *mut kernel::bindings::device) -> bool { + // SAFETY: Per the caller. + let stopped = unsafe { apple_sleep_coproc_stopped(dev) }; + // SAFETY: Per the caller. + unsafe { apple_sleep_coproc_unregister(dev) }; + stopped +} + +#[cfg(not(CONFIG_APPLE_SOC_SLEEP))] +unsafe fn sleep_commit_register( + _dev: *mut kernel::bindings::device, + _data: *mut SleepCommit, +) -> core::ffi::c_int { + ENODEV.to_errno() +} + +#[cfg(not(CONFIG_APPLE_SOC_SLEEP))] +unsafe fn sleep_commit_unregister(_dev: *mut kernel::bindings::device) -> bool { + false +} + +/// How long submissions in flight get to finish before a sleep, and how often they are polled. +const SLEEP_DRAIN_MS: i64 = 500; +const SLEEP_DRAIN_POLL_MS: i64 = 5; + /// Minimum interval between GPU fault / timeout / channel-error reports. /// /// Each report is a dozen-odd error lines, and a single misbehaving client can @@ -345,6 +446,9 @@ pub(crate) struct GpuManager { /// The firmware's timestamp aperture in the kernel VM, where timestamp buffers are bound. timestamp_range: Range, crashed: AtomicBool, + sleep_parked: AtomicBool, + /// What the commit of the sleep under way parks the firmware with, null when none. + sleep_commit: AtomicPtr, /// Set once the over-limit firmware-private garbage was reported as held. garbage_held: AtomicBool, #[pin] @@ -446,6 +550,21 @@ pub(crate) trait Gpu: Send + Sync { fn ids(&self) -> &SequenceIDs; /// Check whether the GPU is crashed fn is_crashed(&self) -> bool; + /// Whether the firmware can be parked for a firmware-driven sleep and restarted after it. + fn sleep_supported(&self) -> bool { + false + } + /// Parks the firmware and stops its CPU, with user space frozen and before the noirq phase. + /// `asc_cpu_control` is the mapped CPU control word of the GPU's ASC. + fn sleep_suspend(&self, _asc_cpu_control: *mut core::ffi::c_void) -> Result { + Err(EBUSY) + } + /// Whether [`Gpu::sleep_suspend`] left the firmware running for the sleep's commit to park. + fn sleep_commit_pending(&self) -> bool { + false + } + /// Restarts a firmware the sleep parked, after a sleep that took its power. + fn sleep_resume(&self, _asc_cpu_control: *mut core::ffi::c_void) {} /// Fill in the hardware description reported to userspace. fn get_params(&self, params: &mut uapi::drm_asahi_params_global) -> Result; /// Whether unmapped GPU reads return zero (fault_control programmed into the firmware). @@ -1156,6 +1275,8 @@ impl GpuManager::ver { next_mmio_iova: IOVA_KERN_MMIO_RANGE.start, rtkit <- new_mutex!(None, "rtkit"), crashed: AtomicBool::new(false), + sleep_parked: AtomicBool::new(false), + sleep_commit: AtomicPtr::new(core::ptr::null_mut()), garbage_held: AtomicBool::new(false), event_manager, stats: stats_snap.clone(), @@ -1765,6 +1886,144 @@ impl Gpu for GpuManager::ver { self.crashed.load(Ordering::Relaxed) } + fn sleep_supported(&self) -> bool { + self.cfg.sleep_restart + } + + fn sleep_suspend(&self, asc_cpu_control: *mut core::ffi::c_void) -> Result { + if !self.sleep_supported() || self.is_crashed() || asc_cpu_control.is_null() { + return Err(EBUSY); + } + + // Parked for a sleep that was then refused: still down, and this sleep restarts it. + if self.sleep_parked.load(Ordering::Acquire) { + return Ok(()); + } + + // User space is frozen, so nothing new is submitted: what the firmware still has in + // flight gets SLEEP_DRAIN_MS to finish, then the sleep is refused. + let mut pending = 0u32; + for _ in 0..SLEEP_DRAIN_MS / SLEEP_DRAIN_POLL_MS { + pending = self + .initdata + .globals + .with(|raw, _inner| raw.pending_submissions.load(Ordering::Acquire)); + if pending == 0 { + break; + } + kernel::time::delay::fsleep(Delta::from_millis(SLEEP_DRAIN_POLL_MS)); + } + if pending != 0 { + dev_warn!( + self.dev.as_ref(), + "{} submission(s) still pending: refusing the sleep\n", + pending + ); + return Err(EBUSY); + } + + // The sleep's commit parks the firmware, past the last refusal: parked here, it would + // stay down after a sleep that is then refused. + let rtk = { + let guard = self.rtkit.lock(); + (*guard).as_ref().ok_or(ENODEV)?.as_raw() + }; + let commit = KBox::new( + SleepCommit { + rtk, + control: asc_cpu_control, + ran: false, + ret: 0, + }, + GFP_KERNEL, + )?; + let data = KBox::into_raw(commit); + let pdev: &kernel::platform::Device = self.dev.as_ref(); + let dev: &kernel::device::Device = pdev.as_ref(); + let dev = dev.as_raw(); + // SAFETY: `dev` is this GPU's bound device; `data` stays allocated until + // `sleep_resume()` has unregistered the callback. + let ret = unsafe { sleep_commit_register(dev, data) }; + if ret != 0 { + // SAFETY: Not registered, so nothing else holds `data`. + drop(unsafe { KBox::from_raw(data) }); + dev_warn!( + self.dev.as_ref(), + "no stop at the sleep's commit ({}): refusing the sleep\n", + ret + ); + return Err(Error::from_errno(ret)); + } + self.sleep_commit.store(data, Ordering::Release); + Ok(()) + } + + fn sleep_commit_pending(&self) -> bool { + !self.sleep_commit.load(Ordering::Acquire).is_null() + } + + fn sleep_resume(&self, asc_cpu_control: *mut core::ffi::c_void) { + let data = self.sleep_commit.swap(core::ptr::null_mut(), Ordering::AcqRel); + if !data.is_null() { + let pdev: &kernel::platform::Device = self.dev.as_ref(); + let dev: &kernel::device::Device = pdev.as_ref(); + let dev = dev.as_raw(); + // SAFETY: Registered in `sleep_suspend()`; the callback can no longer run. + let stopped = unsafe { sleep_commit_unregister(dev) }; + // SAFETY: Allocated in `sleep_suspend()` and no longer registered. + let commit = unsafe { KBox::from_raw(data) }; + // SAFETY: Reads a global flag word. + if !stopped && !commit.ran && !unsafe { kernel::bindings::pm_resume_via_firmware() } { + dev_info!( + self.dev.as_ref(), + "sleep refused before the commit: firmware never stopped\n" + ); + return; + } + if commit.ret != 0 { + dev_warn!( + self.dev.as_ref(), + "firmware did not idle at the sleep's commit: {}\n", + commit.ret + ); + } + self.sleep_parked.store(true, Ordering::Release); + } + if !self.sleep_parked.load(Ordering::Acquire) || asc_cpu_control.is_null() { + return; + } + // SAFETY: Reads a global flag word. + if !unsafe { kernel::bindings::pm_resume_via_firmware() } { + dev_warn!( + self.dev.as_ref(), + "the sleep was refused with the firmware parked: it stays down until a sleep that happens\n" + ); + return; + } + self.sleep_parked.store(false, Ordering::Release); + + let ret = { + let mut guard = self.rtkit.lock(); + match guard.as_mut().as_pin_mut() { + // SAFETY: As in `sleep_suspend()`. + Some(rtk) => unsafe { rtk.sleep_return_with_init(asc_cpu_control) }, + None => Err(ENODEV), + } + }; + match ret { + Ok(()) => { + let _ = self.kick_firmware(); + } + Err(e) => { + dev_err!( + self.dev.as_ref(), + "firmware did not restart after the sleep: {:?}\n", + e + ); + } + } + } + fn get_params(&self, params: &mut uapi::drm_asahi_params_global) -> Result { params.gpu_generation = self.dyncfg.id.gpu_gen as u32; params.gpu_variant = self.dyncfg.id.gpu_variant as u32; diff --git a/drivers/gpu/drm/asahi/hw/mod.rs b/drivers/gpu/drm/asahi/hw/mod.rs index aee820abaef56..6bf569bba8136 100644 --- a/drivers/gpu/drm/asahi/hw/mod.rs +++ b/drivers/gpu/drm/asahi/hw/mod.rs @@ -248,6 +248,9 @@ pub(crate) struct HwClusteringConfig { pub(crate) struct HwConfig { /// Chip ID in hex format (e.g. 0x8103 for t8103). pub(crate) chip_id: u32, + /// A firmware-driven system sleep takes the GPU's power, and its firmware can be parked + /// before the sleep and restarted from scratch after it, see `Gpu::sleep_suspend()`. + pub(crate) sleep_restart: bool, /// GPU generation. pub(crate) gpu_gen: GpuGen, /// GPU variant type. diff --git a/drivers/gpu/drm/asahi/hw/t600x.rs b/drivers/gpu/drm/asahi/hw/t600x.rs index 702474e47b1d7..592db8ae04196 100644 --- a/drivers/gpu/drm/asahi/hw/t600x.rs +++ b/drivers/gpu/drm/asahi/hw/t600x.rs @@ -44,6 +44,7 @@ const fn iomaps(mcc_count: usize, has_die1: bool) -> [Option; 20] { } pub(crate) const HWCONFIG_T6002: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x6002, gpu_gen: GpuGen::G13, gpu_variant: GpuVariant::D, @@ -152,6 +153,8 @@ pub(crate) const HWCONFIG_T6001: super::HwConfig = HwConfig { pub(crate) const HWCONFIG_T6000: super::HwConfig = HwConfig { chip_id: 0x6000, + // Qualified on the M1 Pro alone; T6001 and T6002 have no sleep controller. + sleep_restart: true, gpu_variant: GpuVariant::S, gpu_core: GpuCore::G13S, diff --git a/drivers/gpu/drm/asahi/hw/t602x.rs b/drivers/gpu/drm/asahi/hw/t602x.rs index 9047e8c609846..52318d1fb59b1 100644 --- a/drivers/gpu/drm/asahi/hw/t602x.rs +++ b/drivers/gpu/drm/asahi/hw/t602x.rs @@ -47,6 +47,7 @@ const fn iomaps(chip_id: u32, mcc_count: usize) -> [Option; 24] { // TODO: Tentative pub(crate) const HWCONFIG_T6022: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x6022, gpu_gen: GpuGen::G14, gpu_variant: GpuVariant::D, diff --git a/drivers/gpu/drm/asahi/hw/t8103.rs b/drivers/gpu/drm/asahi/hw/t8103.rs index 62771231e57c0..4de29e02c4d08 100644 --- a/drivers/gpu/drm/asahi/hw/t8103.rs +++ b/drivers/gpu/drm/asahi/hw/t8103.rs @@ -7,6 +7,7 @@ use crate::f32; use super::*; pub(crate) const HWCONFIG: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x8103, gpu_gen: GpuGen::G13, gpu_variant: GpuVariant::G, diff --git a/drivers/gpu/drm/asahi/hw/t8112.rs b/drivers/gpu/drm/asahi/hw/t8112.rs index 0f99285abc4f6..e0cf523793a2e 100644 --- a/drivers/gpu/drm/asahi/hw/t8112.rs +++ b/drivers/gpu/drm/asahi/hw/t8112.rs @@ -7,6 +7,7 @@ use crate::f32; use super::*; pub(crate) const HWCONFIG: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x8112, gpu_gen: GpuGen::G14, gpu_variant: GpuVariant::G, diff --git a/drivers/gpu/drm/asahi/sleep_pd.c b/drivers/gpu/drm/asahi/sleep_pd.c index 059269f84c847..c0789171b3dc5 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.c +++ b/drivers/gpu/drm/asahi/sleep_pd.c @@ -31,6 +31,8 @@ struct asahi_sleep_pd { struct notifier_block nb; bool active; + /* The firmware runs until the commit of a firmware-driven sleep stops it. */ + bool deep; }; /* IRQ-safe domains invoke power notifiers in atomic context. */ @@ -70,7 +72,8 @@ int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware) struct asahi_sleep_pd *hold; int ret; - if (pm_suspend_via_firmware() && !hold_firmware) + hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); + if (pm_suspend_via_firmware() && !hold_firmware && !(hold && hold->deep)) return 0; /* @@ -115,6 +118,18 @@ int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware) return 0; } +/* + * A firmware-driven sleep whose commit stops the firmware, past the last refusal: the + * domain stays up under it until then, as across a suspend-to-idle. + */ +void asahi_sleep_pd_hold_deep(struct device *dev, bool hold_deep) +{ + struct asahi_sleep_pd *hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); + + if (hold) + hold->deep = hold_deep; +} + int asahi_sleep_pd_suspend_noirq(struct device *dev) { return asahi_sleep_pd_suspend_noirq_hold(dev, false); diff --git a/drivers/gpu/drm/asahi/sleep_pd.h b/drivers/gpu/drm/asahi/sleep_pd.h index f9ce7f989d069..b07b73f55eb82 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.h +++ b/drivers/gpu/drm/asahi/sleep_pd.h @@ -11,5 +11,6 @@ int asahi_sleep_pd_init(struct device *dev); int asahi_sleep_pd_suspend_noirq(struct device *dev); int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware); int asahi_sleep_pd_resume_noirq(struct device *dev); +void asahi_sleep_pd_hold_deep(struct device *dev, bool hold); #endif diff --git a/drivers/hid/spi-hid/spi-hid-apple-core.c b/drivers/hid/spi-hid/spi-hid-apple-core.c index 3f369b99f28c4..977ba54b43aaf 100644 --- a/drivers/hid/spi-hid/spi-hid-apple-core.c +++ b/drivers/hid/spi-hid/spi-hid-apple-core.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1332,6 +1333,11 @@ static int spihid_apple_core_suspend(struct device *dev) { int ret; struct spihid_apple *spihid = spi_get_drvdata(to_spi_device(dev)); + /* + * A firmware sleep wakes through the SMC, and an armed interrupt would be + * counted as a wakeup event after every wake. + */ + bool wake = device_may_wakeup(dev) && !pm_suspend_via_firmware(); if (spihid->tp.hid) { ret = hid_driver_suspend(spihid->tp.hid, PMSG_SUSPEND); @@ -1365,7 +1371,7 @@ static int spihid_apple_core_suspend(struct device *dev) * system. Its state is kept, so resume does not reboot it. */ spihid->irq_wake_enabled = false; - if (device_may_wakeup(dev)) { + if (wake) { ret = spihid->ops->enable_irq_wake(spihid->ops); if (!ret) { spihid->irq_wake_enabled = true; diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 20e82332ade01..825c0647bcdf0 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -294,6 +294,8 @@ struct apple_dart_hw { u32 lock; u32 lock_bit; + /* A firmware sleep resets the DART and its lock: a locked DART is saved and restored. */ + bool sleep_clears_lock; u32 error; @@ -605,6 +607,10 @@ struct apple_dart { u32 save_tcr[DART_MAX_STREAMS]; u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; + /* A locked DART's lock word and stream enables, see apple_dart_hw.sleep_clears_lock */ + u32 save_lock; + u32 save_enable[DART_MAX_STREAMS / 32]; + bool locked_saved; u64 *locked_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; /* Values published by this driver into initially vacant root slots. */ @@ -7547,6 +7553,7 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = { .enable_streams = DART_T8020_STREAMS_ENABLE, .lock = DART_T8020_CONFIG, .lock_bit = DART_T8020_CONFIG_LOCK, + .sleep_clears_lock = true, .error = DART_T8020_ERROR, @@ -7895,8 +7902,21 @@ static __maybe_unused int apple_dart_suspend(struct device *dev) return 0; /* Locked DARTs can't be restored so skip saving their registers. */ - if (dart->locked) - return 0; + if (dart->locked) { + unsigned int idx; + + /* + * Unless a sleep takes the lock with the registers. Saved at every suspend: + * a DART that is runtime suspended when the system sleeps isn't asked again. + */ + if (!dart->hw->sleep_clears_lock) + return 0; + dart->save_lock = apple_dart_readl(dart, dart->hw->lock); + for (idx = 0; idx < BITS_TO_U32(dart->num_streams); idx++) + dart->save_enable[idx] = + apple_dart_readl(dart, dart->hw->enable_streams + 4 * idx); + dart->locked_saved = true; + } apple_dart_save_state(dart); return 0; @@ -8276,8 +8296,30 @@ static __maybe_unused int apple_dart_resume(struct device *dev) return 0; /* Locked DARTs can't be restored, and they should not need it */ - if (dart->locked) + if (dart->locked) { + /* + * Except one the sleep reset, lock included: the translations and stream + * enables go back as they were, then the lock word, which closes them again. + */ + if (!dart->locked_saved || !pm_resume_via_firmware() || + apple_dart_state_retained(dart)) + return 0; + for (sid = 0; sid < dart->num_streams; sid++) { + for (idx = 0; idx < dart->hw->ttbr_count; idx++) + apple_dart_writel(dart, dart->save_ttbr[sid][idx], + DART_TTBR(dart, sid, idx)); + apple_dart_writel(dart, dart->save_tcr[sid], DART_TCR(dart, sid)); + } + for (idx = 0; idx < BITS_TO_U32(dart->num_streams); idx++) + apple_dart_writel(dart, dart->save_enable[idx], + dart->hw->enable_streams + 4 * idx); + apple_dart_writel(dart, dart->save_lock, dart->hw->lock); + ret = apple_dart_invalidate_each_stream(dart); + if (ret) + dev_warn(dev, "locked DART restored after the sleep, TLB flush failed: %d\n", + ret); return 0; + } /* * Not for a DART restored at every power-up, as macOS's SPTM does (its diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index ef8c31817b9ac..1e890ab5eaf95 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -317,6 +317,8 @@ static const struct aic_info aic2_info __initconst = { .fast_ipi = true, .local_fast_ipi = true, + /* As AIC3: the configuration goes with the SoC's power in a firmware-driven suspend. */ + .fw_suspend = true, }; static const struct aic_info aic2_t8112_info __initconst = { diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 25daa975c1fbe..b5558827ef538 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -723,6 +724,16 @@ static void apple_smc_probe_wktp(struct apple_smc *smc) apple_smc_write_full_wake(smc); } +/* Whether the device tree has the sleep controller @compatible and leaves it enabled. */ +static bool apple_smc_sleep_controller_available(const char *compatible) +{ + struct device_node *np = of_find_compatible_node(NULL, NULL, compatible); + bool available = np && of_device_is_available(np); + + of_node_put(np); + return available; +} + /* pmVC as the boot left it, next to what apple,sleep-pmvc has the sleep commit write. */ static void apple_smc_sleep_pmvc_report(struct apple_smc *smc) { @@ -757,6 +768,10 @@ static int apple_smc_probe(struct platform_device *pdev) smc->sleep_wake_type = of_property_read_bool(dev->of_node, "apple,sleep-wake-type"); smc->sleep_pmvc_set = !of_property_read_u32(dev->of_node, "apple,sleep-pmvc", &smc->sleep_pmvc); + ret = of_property_read_variable_u32_array(dev->of_node, "apple,sleep-low-pins", + smc->sleep_low_pins, 1, + ARRAY_SIZE(smc->sleep_low_pins)); + smc->sleep_low_pin_count = ret > 0 ? ret : 0; apple_smc_init_state(smc); smc->dev = &pdev->dev; @@ -774,6 +789,10 @@ static int apple_smc_probe(struct platform_device *pdev) of_device_is_compatible(dev->of_node, "apple,t8103-smc") || of_device_is_compatible(dev->of_node, "apple,t8112-smc")) smc->saved_enabled = true; + /* As is T6000's, on a board whose device tree enables its sleep controller. */ + if (of_device_is_compatible(dev->of_node, "apple,t6000-smc") && + apple_smc_sleep_controller_available("apple,t6000-sleep")) + smc->saved_enabled = true; smc->sram_base = devm_platform_get_and_ioremap_resource(pdev, 1, &smc->sram); if (IS_ERR(smc->sram_base)) return dev_err_probe(dev, PTR_ERR(smc->sram_base), "Failed to map SRAM region"); @@ -1345,6 +1364,34 @@ static void apple_smc_dark_wake(struct apple_smc *smc) pm_set_dark_wake_irq(apple_smc_wake_irq(smc)); } +/* + * apple,sleep-low-pins: key "gP" plus the GPIO number as two hex digits. Written 0 before + * the sleep notice, as macOS does, and switched on again with the SMC GPIO output command. + */ +#define SMC_GPIO_CMD_OUTPUT BIT(24) + +static smc_key apple_smc_gpio_key(u32 gpio) +{ + return __SMC_KEY('g', 'P', hex_asc_hi(gpio), hex_asc_lo(gpio)); +} + +/* After waka: the ones this sleep lowered, on again. */ +static void apple_smc_sleep_gpios_high(struct apple_smc *smc) +{ + unsigned long lowered = smc->sleep_low_pins_lowered; + unsigned int i; + int ret; + + smc->sleep_low_pins_lowered = 0; + for_each_set_bit(i, &lowered, ARRAY_SIZE(smc->sleep_low_pins)) { + ret = apple_smc_write_u32(smc, apple_smc_gpio_key(smc->sleep_low_pins[i]), + SMC_GPIO_CMD_OUTPUT | 1); + if (ret) + dev_err(smc->dev, "Failed to switch SMC GPIO %u back on: %d\n", + smc->sleep_low_pins[i], ret); + } +} + /* * The SMC's first wake commands after AP ON: MBSE and MBSW read, then MBSE = waka. With a sleep * policy, waka only after a clean read, and when it was answered for the pmVC that follows. @@ -1356,6 +1403,7 @@ static int apple_smc_wake_record(struct apple_smc *smc, bool *mbsw, bool later) smc->wake_lid_read = false; if (!smc->sleep_policy) { apple_smc_write_mbse(smc, SMC_KEY(waka)); + apple_smc_sleep_gpios_high(smc); return ret; } if (!ret) @@ -1798,6 +1846,22 @@ static int apple_smc_sleep_saved_pmvc(void *data) return 0; } +/* In the saved lane, after the peers stopped. The ones lowered are raised after the wake. */ +static int apple_smc_sleep_gpios_low(struct apple_smc *smc) +{ + unsigned int i; + int ret; + + for (i = 0; i < smc->sleep_low_pin_count; i++) { + ret = apple_smc_saved_write_u32(smc, apple_smc_gpio_key(smc->sleep_low_pins[i]), + 0); + if (ret) + return ret; + smc->sleep_low_pins_lowered |= BIT(i); + } + return 0; +} + static void apple_smc_sleep_until_before_slpa(void *data, u32 us) { struct apple_smc *smc = data; @@ -1841,10 +1905,22 @@ static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack) return -EINVAL; smc->saved_committed = true; - /* Gen 1 (T8103): WKTP between slpw and slpa; this SMC has no pmVC. */ + /* apple,sleep-low-pins: off before the keys below, as macOS has them. */ + ret = apple_smc_sleep_gpios_low(smc); + if (ret) + goto out; + /* + * Gen 1: WKTP between slpw and slpa. T8103's SMC has no pmVC; T6000's takes it + * before WKTP (apple,sleep-pmvc). + */ if (apple_sleep_smc_gen1_keys()) { u8 wktp = 2; + if (smc->sleep_pmvc_set) { + ret = apple_smc_saved_write_u32(smc, SMC_KEY(pmVC), smc->sleep_pmvc); + if (ret) + goto out; + } ret = apple_smc_saved_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); if (ret) goto out; diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 5aa4367dd6113..89e0f244905e6 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -23,6 +23,7 @@ #include #include #include +#include /* * T602x register interface is cleary different so most of the names below are @@ -110,12 +111,15 @@ struct apple_dpxbar_hw { bool t604x_dpin_atc; /* power domain held only while an output is selected */ bool runtime_pm; + /* a firmware sleep resets the block: the tunable and each selection are written again */ + bool restore_after_fw_sleep; /* T8142 layout: a connection status still busy after its poll is only logged */ bool status_log_only; }; struct apple_dpxbar { struct device *dev; + struct mux_chip *chip; const struct apple_dpxbar_hw *hw; void __iomem *regs; struct device *common_pd; @@ -1463,6 +1467,7 @@ static int apple_dpxbar_probe(struct platform_device *pdev) spin_lock_init(&dpxbar->lock); dpxbar->dev = dev; + dpxbar->chip = mux_chip; if (hw->ops == &apple_dpxbar_t8142_ops) { ret = apple_dpxbar_domains(dev, dpxbar); if (ret) @@ -1526,7 +1531,23 @@ static int apple_dpxbar_resume(struct device *dev) unsigned long restored = 0, flags; unsigned int i; - if (!xbar || !xbar->t6030_dpin) + if (!xbar) + return 0; + + /* As at probe, then each output's route and clocks as its last selection set them. */ + if (xbar->hw->restore_after_fw_sleep && pm_resume_via_firmware()) { + writel(xbar->hw->tunable, xbar->regs + UNK_TUNABLE); + + for (i = 0; i < MUX_MAX; i++) { + int state = xbar->selected_dispext[i]; + + if (state < 0) + continue; + apple_dpxbar_set(&xbar->chip->mux[i], state); + } + } + + if (!xbar->t6030_dpin) return 0; spin_lock_irqsave(&xbar->lock, flags); @@ -1603,6 +1624,7 @@ static const struct apple_dpxbar_hw apple_dpxbar_hw_t6000 = { .tunable = 5, .ops = &apple_dpxbar_ops, .dpin_no_cycle_slip = true, + .restore_after_fw_sleep = true, }; static const struct apple_dpxbar_hw apple_dpxbar_hw_t6020 = { diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 84b06ded5acdd..2fb56ad352f9f 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -10055,8 +10055,6 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por ret = apple_pcie_hib_port_trained(pcie, port); if (ret) return ret; - /* 100 ms after the link before configuration requests (PCIe r5.0, 6.6.1). */ - msleep(100); return 0; } @@ -10150,13 +10148,6 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) } port->hib_retrained = true; } - /* 100 ms after the links before configuration requests (PCIe r5.0, 6.6.1). */ - list_for_each_entry(port, &pcie->ports, entry) { - if (port->hib_retrained) { - msleep(100); - break; - } - } } /* What apple_pcie_hib_ports_phased() does not cover takes the probe path, one port at a time. */ @@ -10195,6 +10186,13 @@ static void apple_pcie_hib_ports_up_noirq(struct apple_pcie *pcie) port->hib_retrained = true; } } + /* 100 ms after the links before configuration requests (PCIe r5.0, 6.6.1). */ + list_for_each_entry(port, &pcie->ports, entry) { + if (port->hib_retrained) { + msleep(100); + break; + } + } list_for_each_entry(port, &pcie->ports, entry) { if (port->hib_retrained) dev_info(pcie->dev, "S2R: %pOF link back before its functions resume\n", @@ -10319,6 +10317,7 @@ static int apple_pcie_resume(struct device *dev) struct apple_pcie *pcie = apple_pcie_lookup(dev); struct apple_pcie_port *port; struct apple_pcie_hib_fn *fn; + bool trained = false; int ret; if (!pcie->hib_saved) @@ -10354,9 +10353,16 @@ static int apple_pcie_resume(struct device *dev) continue; } apple_pcie_port_enable_irq(port); + trained = true; dev_info(dev, "S2R: %pOF link back, PWREN and PERST# cycled as at probe\n", port->np); } + /* + * 100 ms after the last link before configuration requests (PCIe r5.0, + * 6.6.1); every earlier link has waited longer. + */ + if (trained) + msleep(100); /* Reopen each bridge before restoring any downstream config or ASPM. */ list_for_each_entry(fn, &pcie->hib_fns, entry) { diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 3099c44a084d1..09d5cc7fea3f5 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -3160,6 +3160,9 @@ const HW_CFG_T8112: AopHwConfig = AopHwConfig { s2r_notice: AOP_S2R_ENDPOINTS | AOP_S2R_AP_IDLE, }; /// `nCal` keeps the firmware's own value, as on T8112. +/// +/// The sleep and wake lists are the twelve AFK endpoints the traced profiles name; the order +/// is untraced on this SoC. const HW_CFG_T6000: AopHwConfig = AopHwConfig { ec0p: 0x020000, ncal: None, @@ -3168,8 +3171,8 @@ const HW_CFG_T6000: AopHwConfig = AopHwConfig { patch_bootargs: true, epic_v4: false, setup_port: false, - sleep_endpoints: &[], - wake_endpoints: &[], + sleep_endpoints: &[0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b], + wake_endpoints: &[0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b], setup_epmap_endpoints: false, setup_wait_system_endpoints: true, quiesce_on_unbind: false, diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index a7d965603a5ae..b4e622a885a36 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -313,7 +313,7 @@ static int sep_pm_commit_stop(void *data) if (ret) goto out; c->sent = true; - while (!c->answered && !((c->state = readl(c->ps)) & SEP_PS_DEV_DISABLE) && + while (!c->answered && !sep_ps_down(c->state = readl(c->ps)) && c->answer_us < SEP_PM_TIMEOUT_US) { ret = apple_mbox_poll_one(c->mbox); if (ret < 0) @@ -332,8 +332,17 @@ static int sep_pm_commit_stop(void *data) return ret; } /* Preserve the failure reason without another hardware query. */ - if (ret >= 0 && !c->answered) - ret = c->state & SEP_PS_DEV_DISABLE ? -EHOSTDOWN : -ETIMEDOUT; + if (ret >= 0 && !c->answered) { + if (c->state & SEP_PS_DEV_DISABLE) + ret = -EHOSTDOWN; + else if (FIELD_GET(SEP_PS_ACTUAL, c->state)) + ret = -ETIMEDOUT; + /* + * Otherwise the domain reads off without DEV_DISABLE: it took + * the request and gated before the answer was polled for. Never + * in place of an answer that reports an error. + */ + } if (c->answered && c->reply_result) ret = -EREMOTEIO; /* Native cmsgSLEEP errors/abort/timeout are fatal, never cancellation. */ diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 8697450b4281a..de881b5328c3c 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2191,16 +2191,14 @@ void apple_rtkit_adopt_ep(struct apple_rtkit *rtk, u8 ep) } EXPORT_SYMBOL_GPL(apple_rtkit_adopt_ep); -int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) +/* The start message alone, for an endpoint the firmware's endpoint map announced. */ +static int apple_rtkit_start_announced_ep(struct apple_rtkit *rtk, u8 endpoint) { u64 msg; int ret; if (!test_bit(endpoint, rtk->endpoints)) return -EINVAL; - if (endpoint >= APPLE_RTKIT_APP_ENDPOINT_START && - !apple_rtkit_is_running(rtk)) - return -EINVAL; msg = FIELD_PREP(APPLE_RTKIT_MGMT_STARTEP_EP, endpoint); msg |= APPLE_RTKIT_MGMT_STARTEP_FLAG; @@ -2215,8 +2213,32 @@ int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) set_bit(endpoint, rtk->started); return ret; } + +int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) +{ + if (endpoint >= APPLE_RTKIT_APP_ENDPOINT_START && + !apple_rtkit_is_running(rtk)) + return -EINVAL; + + return apple_rtkit_start_announced_ep(rtk, endpoint); +} EXPORT_SYMBOL_GPL(apple_rtkit_start_ep); +/* Every application endpoint that was started before, started again in endpoint order. */ +static int apple_rtkit_restart_app_endpoints(struct apple_rtkit *rtk) +{ + unsigned long endpoint = APPLE_RTKIT_APP_ENDPOINT_START; + int ret; + + for_each_set_bit_from(endpoint, rtk->started, APPLE_RTKIT_MAX_ENDPOINTS) { + ret = apple_rtkit_start_announced_ep(rtk, endpoint); + if (ret) + return ret; + } + + return 0; +} + static void apple_rtkit_mark_running(struct apple_rtkit *rtk) { static const u8 system_endpoints[] = { @@ -2668,8 +2690,11 @@ EXPORT_SYMBOL_GPL(apple_rtkit_set_handshake_timeout); * given and its rings, so unlike apple_rtkit_reinit() nothing is freed, and * endpoints aren't started again. The caller then sets the run bit and calls * apple_rtkit_boot(). + * + * With @full_handshake the firmware sends its endpoint map again: the map is cleared, and + * the caller starts the endpoints. */ -int apple_rtkit_warm_restart(struct apple_rtkit *rtk) +static int apple_rtkit_prepare_restart(struct apple_rtkit *rtk, bool full_handshake) { unsigned long flags; int ret; @@ -2681,8 +2706,13 @@ int apple_rtkit_warm_restart(struct apple_rtkit *rtk) reinit_completion(&rtk->iop_pwr_ack_completion); reinit_completion(&rtk->ap_pwr_ack_completion); + if (full_handshake) { + bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); + set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); + rtk->boot_result = -EINPROGRESS; + } rtk->crashed = false; - rtk->warm_restart = true; + rtk->warm_restart = !full_handshake; rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_OFF; rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_OFF; kfifo_reset(&rtk->sync_ack_fifo); @@ -2701,6 +2731,11 @@ int apple_rtkit_warm_restart(struct apple_rtkit *rtk) } return ret; } + +int apple_rtkit_warm_restart(struct apple_rtkit *rtk) +{ + return apple_rtkit_prepare_restart(rtk, false); +} EXPORT_SYMBOL_GPL(apple_rtkit_warm_restart); void apple_rtkit_set_warm_epmap_mask(struct apple_rtkit *rtk, u32 last_mask) @@ -3594,6 +3629,10 @@ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control) apple_rtkit_resume_rx(rtk); if (ret) return ret; + /* apple_mbox_start() skips a started mailbox: write the enables a sleep may have reset. */ + ret = apple_rtkit_mbox_restore(rtk); + if (ret) + return ret; if (cpu_control) { u32 ctl = readl(cpu_control); @@ -3609,6 +3648,42 @@ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control) } EXPORT_SYMBOL_GPL(apple_rtkit_sleep_return); +int apple_rtkit_sleep_return_with_init(struct apple_rtkit *rtk, void __iomem *cpu_control) +{ + int ret, ap_ret; + u64 msg; + + ret = apple_rtkit_prepare_restart(rtk, true); + if (ret) + return ret; + ret = apple_rtkit_mbox_restore(rtk); + if (ret) + return ret; + + writel(readl(cpu_control) | APPLE_ASC_CPU_CONTROL_RUN, cpu_control); + msg = FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_INIT); + ret = apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE, msg); + if (ret) + return ret; + + ret = apple_rtkit_wait(rtk, &rtk->epmap_completion); + if (ret) + return ret; + if (rtk->boot_result) + return rtk->boot_result; + ret = apple_rtkit_wait(rtk, &rtk->iop_pwr_ack_completion); + if (ret) + return ret; + if ((rtk->iop_power_state & 0xff) != APPLE_RTKIT_PWR_STATE_ON) + return -EPROTO; + + /* The AP's state goes on even if an endpoint didn't start: the first error is returned. */ + ret = apple_rtkit_restart_app_endpoints(rtk); + ap_ret = apple_rtkit_set_ap_power_state(rtk, APPLE_RTKIT_PWR_STATE_ON); + return ret ?: ap_ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_sleep_return_with_init); + int apple_rtkit_sleep_return_held(struct apple_rtkit *rtk) { int ret; diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h new file mode 100644 index 0000000000000..7b560cefa0e23 --- /dev/null +++ b/drivers/soc/apple/sleep-t6000.h @@ -0,0 +1,2029 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T6000 (M1 Pro): suspend-to-RAM through the PMGR's S2R timer, in the order macOS has on this + * SoC. Included by sleep.c after the other profiles, so every shared helper is in scope. + * + * Based on the t6000 suspend-to-RAM reference by Andreas Fernandez. + */ + +/* Per-core power-down controls, as on the other Firestorm/Icestorm SoCs */ +#define T6000_CYC_OVRD sys_reg(3, 5, 15, 5, 0) +#define T6000_CYC_DISABLE_WFI_RET BIT_ULL(0) +#define T6000_CYC_FIQ_MODE GENMASK_ULL(21, 20) +#define T6000_CYC_IRQ_MODE GENMASK_ULL(23, 22) +#define T6000_CYC_WFI_MODE GENMASK_ULL(25, 24) +#define T6000_ACC_OVRD sys_reg(3, 5, 15, 6, 0) +#define T6000_ACC_PWR_DN_SRM GENMASK_ULL(14, 13) +#define T6000_ACC_DIS_L2_FLUSH GENMASK_ULL(16, 15) +#define T6000_ACC_TRAIN_DOWN_LINK GENMASK_ULL(18, 17) +#define T6000_ACC_POWER_DOWN_CPM GENMASK_ULL(26, 25) +#define T6000_ACC_DIS_PIO_WFI BIT_ULL(32) +#define T6000_ACC_DEEP_SLEEP BIT_ULL(34) + +/* + * The watchdog is WD1. Each step of the entry arms it again, with the time in seconds the + * steps up to the next arm are given, and the wake arms it once more for the resume. + */ +#define T6000_WDT_WAKE_SECS 170 + +/* "wake": seven mask words (205 valid sources), then seven status words */ +#define T6000_WAKE_MASK 0x08 +#define T6000_WAKE_STATUS 0x24 +#define T6000_WAKE_WORDS 7 +#define T6000_WAKE_LAST_WORD GENMASK(12, 0) + +/* "sep-pg": bit 2 makes the S2R sequencer wait for SEP's power gate */ +#define T6000_SEP_PG_WAIT BIT(2) + +/* + * "cluster": one word per complex, at four times its index. The index is the first byte of + * the complex's entry in the ADT's soc-clusters. Target in bits 3:0, busy in bit 4. + */ +#define T6000_CLUSTERS 9 +#define T6000_CLUSTER_EACC 0 +#define T6000_CLUSTER_PACC0 1 +#define T6000_CLUSTER_PACC1 2 +#define T6000_CLUSTER_ANE0 3 +#define T6000_CLUSTER_AFR 5 +#define T6000_CLUSTER_DISPLAY 6 +#define T6000_CLUSTER_AVEMSR 8 +#define T6000_CLUSTERS_CPU (BIT(T6000_CLUSTER_EACC) | BIT(T6000_CLUSTER_PACC0) | \ + BIT(T6000_CLUSTER_PACC1)) +#define T6000_CLUSTERS_VALID (T6000_CLUSTERS_CPU | BIT(T6000_CLUSTER_ANE0) | \ + BIT(T6000_CLUSTER_AFR) | BIT(T6000_CLUSTER_DISPLAY) | \ + BIT(T6000_CLUSTER_AVEMSR)) +#define T6000_CLUSTER_PS_TARGET GENMASK(3, 0) +#define T6000_CLUSTER_PS_BUSY BIT(4) + +/* + * "acc0".."acc2": the CPU complexes' blocks (E, P0, P1). The two DVFS command values are the + * ones recorded from macOS; APSC_OFF and BUSY are bits of the same word. + */ +#define T6000_ACCS 3 +#define T6000_ACC_DVFS_CMD 0x00020 +#define T6000_ACC_DVFS_APSC_OFF BIT_ULL(22) +#define T6000_ACC_DVFS_BUSY BIT_ULL(7) +#define T6000_ACC_DVFS_RELOCK 0x100101ULL +#define T6000_ACC_DVFS_APSC 0x40000500101ULL +#define T6000_ACC_PERF_CTL 0x00660 +#define T6000_ACC_PERF_EN 0x15ULL +#define T6000_ACC_LLC_THRTL 0x20240 +#define T6000_ACC_AMX_THRTL 0x20250 +#define T6000_ACC_THRTL_EN BIT_ULL(63) +#define T6000_ACC_ENERGY_CTL 0x28080 +#define T6000_ACC_ENERGY_EN (BIT_ULL(63) | BIT_ULL(61)) +#define T6000_ACC_PPT_THRTL 0x28400 + +/* + * "clkgen": muxes parked onto the reference clock before PLL0 goes off, plus four muxes moved + * to source 7 and four muxes disabled at commit. Select in bits 27:24, enable in bit 31, busy + * in bit 30. + */ +#define T6000_CLKGEN_MUX_ON BIT(31) +#define T6000_CLKGEN_MUX_BUSY BIT(30) +#define T6000_CLKGEN_MUX_SEL GENMASK(27, 24) +#define T6000_CLKGEN_LATE_SEL 7 + +/* "pll0": off in three writes, each waited for: off mode 3, off allowed, then the enable */ +#define T6000_PLL_ON BIT(0) +#define T6000_PLL_OFF_OK BIT(1) +#define T6000_PLL_OFF_MODE GENMASK(5, 3) +#define T6000_PLL_BUSY BIT(31) + +/* "dcs": the memory clock's performance state, target in bits 3:0, busy in bit 31 */ +#define T6000_DCS_PS_TARGET GENMASK(3, 0) +#define T6000_DCS_PS_BUSY BIT(31) +/* The states the commit steps through on the way down, and the first on the way back */ +#define T6000_DCS_PS_STEP 7 +#define T6000_DCS_PS_SLEEP 5 + +/* + * "amcc0".."amcc3": the memory controllers, each with four DCS channel blocks 2 MiB apart from + * 2 MiB above its base. Bits 12 and 8 enable dynamic low-power gating across the commit. + */ +#define T6000_AMCCS 4 +#define T6000_AMCC_DRAIN_CTL 0x1c00 +#define T6000_AMCC_DRAIN_COUNT 0x1c04 +#define T6000_AMCC_LOCK 0x10000c +#define T6000_DCS_PER_AMCC 4 +#define T6000_DCS_BLOCK(k) (0x200000ULL * ((k) + 1)) +#define T6000_DCS_BLOCK_LEN 0x20 +#define T6000_DCS_CTL 0x10 +#define T6000_DCS_CTL_LP (BIT(12) | BIT(8)) + +/* + * A fabric bridge's handshake before its domain powers off, as on T8103: 1 at +0x14, then 1, + * 2 and 4 at +0x2bc. "apcie-q" is PCIe's; the others are 0x8000 into their fabric blocks. + */ +#define T6000_BRIDGE_8014 0x014 +#define T6000_BRIDGE_82BC 0x2bc +#define T6000_BRIDGE_LEN 0x300 + +/* + * "ptd", the PMP's telemetry: entry n is read at 0x10 * n and written at 0x10000 + 8 * n. + * 0xf8 is the device power requests, one bit per pmp_report entry; 0x100 acknowledges them. + */ +#define T6000_PTD_PS_REQ_RD 0xf80 +#define T6000_PTD_PS_ACK_RD 0x1000 +#define T6000_PTD_PS_REQ_WR 0x107c0 +#define T6000_PTD_DVFS_WR(n) ((n) == 3 ? 0x10860 : 0x10840 + 8 * (n)) +#define T6000_PTD_DVFS_RD(n) ((n) == 3 ? 0x10c0 : 0x1080 + 0x10 * (n)) +#define T6000_PTD_DVFS_IDLE 0x2000000000000000ULL +/* The requests of the GPU and of the display coprocessors */ +#define T6000_PTD_REQ_GFX BIT_ULL(0x9) +#define T6000_PTD_REQ_DISP0 BIT_ULL(0xc) +#define T6000_PTD_REQ_DISPEXT0 BIT_ULL(0xd) +#define T6000_PTD_REQ_DISPEXT1 BIT_ULL(0xe) +/* The PMP's sleep word: 4 from .begin, 1 at .prepare, back as it was at .finish */ +#define T6000_PTD_SLEEP 0x18328 +#define T6000_PTD_SLEEP_BEGIN 4 +#define T6000_PTD_SLEEP_COMMIT 1 + +/* + * "spmi" (nub, gen 1): vector enable words from +0x20, their acknowledgment 0x40 above. The + * PMU RTC's alarm is vector 2. + */ +#define T6000_SPMI_VEC_ENABLE 0x20 +#define T6000_SPMI_VEC_ACK 0x60 +#define T6000_SPMI_VEC_WORDS 7 +#define T6000_SPMI_VEC_RTC BIT(2) + +/* Power words in "ps" (pmgr), "ps-east" (pmgr_east), and "ps-aon" (pmgr_mini) */ +#define T6000_PS_GPIO 0x120 +#define T6000_PS_PMGR_SOC_OCLA 0x130 +#define T6000_PS_PCIE0_REF 0x138 +#define T6000_PS_PCIE1_REF 0x140 +#define T6000_PS_APCIE_ST 0x148 +#define T6000_PS_AFR 0x1e8 +#define T6000_PS_AFNC1_LW2 0x220 +#define T6000_PS_ATC0_COMMON 0x238 +#define T6000_PS_DISPEXT0_FE 0x258 +#define T6000_PS_DISPEXT1_FE 0x260 +#define T6000_PS_DISPEXT0_CPU0 0x280 +#define T6000_PS_DISPEXT1_CPU0 0x2a8 +#define T6000_PS_PMP 0x2d8 +#define T6000_PS_PMS_SRAM 0x2e0 +#define T6000_PS_APCIE_ST_SYS 0x2e8 +#define T6000_PS_APCIE_ST1_SYS 0x2f0 +#define T6000_PS_ATC2_COMMON 0x2f8 +#define T6000_PS_ATC3_COMMON 0x300 +#define T6000_PS_APCIE_GP_SYS 0x320 +#define T6000_PS_ATC0_PCIE 0x330 +#define T6000_PS_ATC1_PCIE 0x340 +#define T6000_PS_ATC2_PCIE 0x350 +#define T6000_PS_ATC3_PCIE 0x360 + +#define T6000_PS_EAST_AFNC2_LW1 0x160 +#define T6000_PS_EAST_AFNC3_LW0 0x178 +#define T6000_PS_EAST_SIO 0x180 +#define T6000_PS_EAST_SIO_CPU 0x188 +#define T6000_PS_EAST_FPWM0 0x190 +#define T6000_PS_EAST_SPI_P 0x1e8 +#define T6000_PS_EAST_SIO_SPMI0 0x218 +#define T6000_PS_EAST_SIO_SPMI1 0x220 +#define T6000_PS_EAST_SIO_SPMI2 0x228 +#define T6000_PS_EAST_UART_N 0x238 +#define T6000_PS_EAST_UART1 0x248 +#define T6000_PS_EAST_UART2 0x250 +#define T6000_PS_EAST_UART3 0x258 +#define T6000_PS_EAST_UART4 0x260 +#define T6000_PS_EAST_UART6 0x268 +#define T6000_PS_EAST_UART7 0x270 +#define T6000_PS_EAST_AUDIO_P 0x278 +#define T6000_PS_EAST_AES 0x2d0 +#define T6000_PS_EAST_DISP0_FE 0x328 +#define T6000_PS_EAST_DISP0_CPU0 0x350 +#define T6000_PS_EAST_ANS2 0x3b8 +#define T6000_PS_EAST_GFX 0x3c0 +#define T6000_PS_EAST_SEP 0xc00 + +#define T6000_PS_AON_MSG 0x078 +#define T6000_PS_AON_ATC0_USB_AON 0x088 +#define T6000_PS_AON_ATC1_USB_AON 0x090 +#define T6000_PS_AON_ATC2_USB_AON 0x098 +#define T6000_PS_AON_ATC3_USB_AON 0x0a0 +#define T6000_PS_AON_ATC0_USB 0x0d0 +#define T6000_PS_AON_ATC1_USB 0x0d8 +#define T6000_PS_AON_ATC2_USB 0x0e0 + +/* PS word fields the core doesn't name, as pmgr-pwrstate has them */ +#define T6000_PS_RESET BIT(31) +#define T6000_PS_AUTO GENMASK(27, 24) +#define T6000_PS_MIN GENMASK(19, 16) +#define T6000_PS_BUSY BIT(11) +#define T6000_PS_WAS_CLKGATED BIT(9) +/* Bits 12:8: the latches and status bits a restoring write leaves at 0 */ +#define T6000_PS_LATCHES (PS_UNLATCH | T6000_PS_BUSY | PS_DEV_DISABLE | PS_FLAGS) + +#define T6000_PS_WORD(auto_en, auto_st, flags, target) \ + (((auto_en) ? PS_AUTO_ENABLE : 0) | \ + FIELD_PREP_CONST(T6000_PS_AUTO, (auto_st)) | \ + (flags) | \ + FIELD_PREP_CONST(PS_TARGET, (target))) + +#define T6000_PS_WORD_MIN(auto_en, auto_st, min_st, flags, target) \ + (T6000_PS_WORD((auto_en), (auto_st), (flags), (target)) | \ + FIELD_PREP_CONST(T6000_PS_MIN, (min_st))) + +/* + * Blocks with no register entry in the sleep node. Each belongs to a device with its own + * node or to none, and only the words named here are touched. + */ +/* The fabric bridges of dispext0, disp0, dispext1, and the GPU (+0x8000 into each fabric) */ +static const u64 t6000_disp_bridge_pa[] = { + 0x28a008000ULL, 0x38c008000ULL, 0x28d008000ULL, +}; +#define T6000_GFX_BRIDGE_PA 0x405008000ULL +/* + * The enable word of the thermal/voltage monitor in front of the GFX domain, the T6000's + * counterpart of the T8132 GFX TVM gate. As macOS: 1 before AFR and GFX are powered for the + * bridge quiesce, 0 once both are lowered again. Zeroed with the other monitors too, in + * t6000_quiesce_zero_pa. + */ +#define T6000_AFR_GATE 0x28e5e4000ULL +/* SIO's CPU control, mailbox I2A control, and its two DARTs' stream enables */ +static const u64 t6000_sio_stop_pa[] = { + 0x39bc00044ULL, /* sio ASC_CPU_CONTROL */ + 0x39bc08114ULL, /* sio mailbox I2A_CONTROL */ + 0x39b0040fcULL, /* sio_dart_0 stream enable */ + 0x39b0080fcULL, /* sio_dart_1 stream enable */ +}; +/* + * The PMP's address filter (16 entries of six words, in use with bits 1:0 of the first set) + * and reset vector. The sleep loses both and the PMP's restart needs them: saved at probe. + */ +#define T6000_PMP_DAPF 0x28e304000ULL +#define T6000_PMP_DAPF_ENTRIES 16 +#define T6000_PMP_DAPF_STRIDE 0x40 +#define T6000_PMP_DAPF_WORDS 6 +#define T6000_PMP_DAPF_IN_USE GENMASK(1, 0) +#define T6000_PMP_IORVBAR 0x28e850000ULL +#define T6000_PMP_IORVBAR_VALID BIT_ULL(0) + +/* + * The NCO's channels, five words each as in clk-apple-nco. A channel is enabled across the + * commit, and its words are written back at the wake in that driver's order. + */ +#define T6000_NCO_CHANNELS 5 +#define T6000_NCO_STRIDE 0x4000 +#define T6000_NCO_WORDS 5 +#define T6000_NCO_CTRL 0 +#define T6000_NCO_CTRL_ENABLE BIT(31) + +enum { + T6000_ACCR_PERF, + T6000_ACCR_ENERGY, + T6000_ACCR_AMX, + T6000_ACCR_LLC, + T6000_ACCR_PPT, + T6000_ACCR_DVFS, + T6000_ACCR_N, +}; + +static const u32 t6000_acc_regs[T6000_ACCR_N] = { + [T6000_ACCR_PERF] = T6000_ACC_PERF_CTL, + [T6000_ACCR_ENERGY] = T6000_ACC_ENERGY_CTL, + [T6000_ACCR_AMX] = T6000_ACC_AMX_THRTL, + [T6000_ACCR_LLC] = T6000_ACC_LLC_THRTL, + [T6000_ACCR_PPT] = T6000_ACC_PPT_THRTL, + [T6000_ACCR_DVFS] = T6000_ACC_DVFS_CMD, +}; + +/* What the quiesce clears in each of the first five */ +static const u64 t6000_acc_enable[T6000_ACCR_DVFS] = { + [T6000_ACCR_PERF] = T6000_ACC_PERF_EN, + [T6000_ACCR_ENERGY] = T6000_ACC_ENERGY_EN, + [T6000_ACCR_AMX] = T6000_ACC_THRTL_EN, + [T6000_ACCR_LLC] = T6000_ACC_THRTL_EN, + [T6000_ACCR_PPT] = T6000_ACC_THRTL_EN, +}; + +/* In "clkgen": muxes parked onto refclk before PLL0 off, late source-7 muxes, and off muxes */ +static const u16 t6000_clkgen_mux[] = { 0x164, 0x16c, 0x168, 0x15c, 0x170 }; +static const u16 t6000_clkgen_late_mux[] = { 0x180, 0x184, 0x188, 0x18c }; +static const u16 t6000_clkgen_off_mux[] = { 0x98, 0x9c, 0xa0, 0xa4 }; + +/* + * Words zeroed at the head of the quiesce and restored on wake: + * - 4 fabric error-handler enables (south/west/east/gfx fabrics) + * - 9 PMGR performance/telemetry controls + * - 6 thermal/voltage monitor enables (two east monitors, the second of them the GFX + * monitor t6000_gfx_down() switches as T6000_AFR_GATE, + four SoC TVMs) + */ +static const u64 t6000_quiesce_zero_pa[] = { + /* Fabric error-handler enables */ + 0x282000a00ULL, 0x304000a00ULL, 0x383000a00ULL, 0x402000a00ULL, + /* PMGR performance/telemetry controls */ + 0x28e0ad000ULL, 0x28e0f8000ULL, 0x28e0f9000ULL, 0x28e0fa000ULL, + 0x28e0fb000ULL, 0x28e0fc000ULL, 0x28e0fd000ULL, 0x28e070000ULL, + 0x28e5d8000ULL, + /* Thermal/voltage monitors */ + 0x28e5e0000ULL, T6000_AFR_GATE, + 0x28e0c8200ULL, 0x28e0c8400ULL, 0x28e0c8600ULL, 0x28e0c8000ULL, +}; + +/* ADT /arm-io/pmgr ap-wake-sources on T6000, for wake reporting and SMC-only detection */ +static const struct apple_sleep_wake_name t6000_wake_names[] = { + { 12, "AOP.GPIO.IRQ4" }, + { 14, "AOP.GPIO.IRQ6" }, + { 26, "AOP.OutboxNotEmpty" }, + { 27, "AOP.CPUWakeupAP" }, + { 40, "NUB.WakeupAP" }, + { 43, "NUB.GPIO.IRQ2" }, + { 49, "NUB.APWatchdog" }, + { 50, "NUB.APWakeupTime" }, + { 51, "NUB.SPMISw0IRQ" }, + { 52, "NUB.SPMISw1IRQ" }, + { 54, "NUB.SPMISw3IRQ" }, + { 85, "SMC.CPUWakeupAP" }, + { 90, "SMC.OutboxNotEmpty" }, + { 126, "ATC0.CIOWakeup" }, + { 127, "ATC1.CIOWakeup" }, + { 128, "ATC2.CIOWakeup" }, + { 129, "ATC3.CIOWakeup" }, + { 130, "ATC0.USBWakeup" }, + { 131, "ATC1.USBWakeup" }, + { 132, "ATC2.USBWakeup" }, + { 133, "ATC3.USBWakeup" }, +}; + +/* The register blocks a power word is in */ +enum { + T6000_PS, /* "ps": pmgr */ + T6000_PS_EAST, /* "ps-east": pmgr_east */ + T6000_PS_AON, /* "ps-aon": pmgr_mini, the always-on (NUB) domains */ + T6000_BLOCKS, +}; + +struct t6000_ps_word { + u8 block; + u16 offset; +}; + +/* Each display coprocessor's CPU and front-end words: dispext0, disp0, dispext1 */ +static const struct t6000_ps_word t6000_disp_ps[6] = { + { T6000_PS, T6000_PS_DISPEXT0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT0_FE }, + { T6000_PS_EAST, T6000_PS_EAST_DISP0_CPU0 }, + { T6000_PS_EAST, T6000_PS_EAST_DISP0_FE }, + { T6000_PS, T6000_PS_DISPEXT1_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT1_FE }, +}; + +/* disp0's CPU word, the internal display coprocessor's: entry 0 of apple,display-coprocessors */ +#define T6000_DISP0_CPU 2 +/* + * A display coprocessor's CPU word on its way off, the two words t6000_sleep_display_off() + * writes: AUTO_ENABLE off at full power, with a minimum state of clock gating, then the + * target off and the status flags written. + */ +#define T6000_DISP_CPU_WORD_MANUAL T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf) +#define T6000_DISP_CPU_WORD_OFF T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0) +/* What that word gets to read off, as at the entry, and full power again, as at the wake */ +#define T6000_DISP_OFF_TIMEOUT_US 10000 +#define T6000_DISP_ON_TIMEOUT_US 20000 + +/* The order macOS lowers them in: disp0, dispext0, dispext1, each CPU before its front end */ +static const u8 t6000_disp_order[6] = { 2, 3, 0, 1, 4, 5 }; + +/* The PMP's request of each, by the index of its pair in t6000_disp_ps[] */ +static const u64 t6000_disp_request[3] = { + T6000_PTD_REQ_DISPEXT0, T6000_PTD_REQ_DISP0, T6000_PTD_REQ_DISPEXT1, +}; + +/* The domains that must read off before a complex's target is taken off (offset 0 ends) */ +static const struct t6000_ps_word t6000_gate_ane0[] = { + { T6000_PS, 0x268 }, /* ane_sys */ + { T6000_PS, 0x2c8 }, /* ane_sys_cpu */ + { } +}; + +static const struct t6000_ps_word t6000_gate_afr[] = { + { T6000_PS, T6000_PS_AFR }, + { T6000_PS_EAST, T6000_PS_EAST_GFX }, + { T6000_PS, 0x268 }, /* ane_sys */ + { T6000_PS, 0x270 }, /* avd_sys */ + { T6000_PS_EAST, 0x390 }, /* jpg */ + { T6000_PS_EAST, 0x398 }, /* msr0 */ + { T6000_PS_EAST, 0x3b0 }, /* venc_sys */ + { } +}; + +static const struct t6000_ps_word t6000_gate_display[] = { + { T6000_PS_EAST, T6000_PS_EAST_DISP0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT1_CPU0 }, + { } +}; + +static const struct t6000_ps_word t6000_gate_avemsr[] = { + { T6000_PS, 0x270 }, /* avd_sys */ + { T6000_PS_EAST, 0x398 }, /* msr0 */ + { T6000_PS_EAST, 0x3b0 }, /* venc_sys */ + { } +}; + +/* + * Always-on ("ps-aon") domains lowered at commit: they keep their written state across the + * sleep, so each is saved at .prepare and restored on wake. + */ +static const struct { + u16 offset; + u32 value; +} t6000_aon_commit_off[] = { + { T6000_PS_AON_ATC0_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC2_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC1_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC0_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC2_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC1_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC3_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_MSG, T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, +}; + +/* Non-AON PMGR domains programmed into their sequencer commit state before the S2R timer */ +static const struct { + u8 block; + u16 offset; + u32 value; +} t6000_ps_commit_state[] = { + /* Lower stopped USB-C PCIe/common and UART domains */ + { T6000_PS, T6000_PS_ATC0_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC1_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC2_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC3_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC0_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC2_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC3_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART2, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART3, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART4, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART6, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART7, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + /* Hand fabric link-wrapper and PCIe/storage/SIO domains to the S2R sequencer */ + { T6000_PS, T6000_PS_AFNC1_LW2, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AFNC2_LW1, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AFNC3_LW0, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_GP_SYS, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_PCIE0_REF, + T6000_PS_WORD(true, 4, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS, T6000_PS_PCIE1_REF, + T6000_PS_WORD(true, 4, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST_SYS, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST1_SYS, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS, T6000_PS_GPIO, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS, T6000_PS_PMGR_SOC_OCLA, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_CPU, + T6000_PS_WORD(false, 0xf, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_FPWM0, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SPI_P, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI0, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI1, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI2, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_UART_N, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_UART1, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AUDIO_P, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AES, T6000_PS_WORD(false, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_ANS2, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, +}; + +/* + * Every PMGR power domain of the device tree: the sleep cuts their power while genpd counts + * them on, so each is raised again at the wake, parents first. Not SEP's, afc or afi. + */ +#define T6000_DOMAINS_MAX 256 +#define T6000_DOMAIN_BLOCKS 8 +#define T6000_DOMAIN_PASSES 30 + +/* + * Steps of the entry that were left out or did not finish, kept for the report after the + * wake: the commit runs with one CPU and no console. + */ +#define T6000_INCOMPLETE_SEP BIT(0) +#define T6000_INCOMPLETE_PMP BIT(1) +#define T6000_INCOMPLETE_PMS_SRAM BIT(2) +#define T6000_INCOMPLETE_DCS_KEPT BIT(3) +#define T6000_INCOMPLETE_DCS_BUSY BIT(4) +#define T6000_INCOMPLETE_PLL_KEPT BIT(5) +#define T6000_INCOMPLETE_LATE_MUX BIT(6) + +static const char *const t6000_incomplete_names[] = { + "SEP not power-gated", + "PMP not off", + "PMP SRAM not off", + "memory clock state not lowered", + "memory clock state not reached", + "clock mux busy and PLL0 left on", + "late clock mux busy", +}; + +static struct t6000_domain { + struct device_node *np; + void __iomem *reg; + const char *name; + u32 saved; + /* A display word: its driver lowers it in its suspend, so it is read at .begin */ + bool save_at_begin; + /* In "ps-aon": it keeps its state, and isn't raised here */ + bool always_on; + /* A dcs_* word: what it read at the commit of the first sleep since boot */ + u32 commit_ref; +} t6000_domains[T6000_DOMAINS_MAX]; + +static struct { + resource_size_t start; + void __iomem *base; +} t6000_domain_blocks[T6000_DOMAIN_BLOCKS]; + +static struct t6000_sleep { + void __iomem *ps_east, *sep_pg, *clkgen, *pll0, *dcs, *apcie_q, *ptd; + void __iomem *acc[T6000_ACCS]; + void __iomem *amcc[T6000_AMCCS]; + void __iomem *dcs_block[T6000_AMCCS][T6000_DCS_PER_AMCC]; + void __iomem *disp_bridge[ARRAY_SIZE(t6000_disp_bridge_pa)]; + void __iomem *gfx_bridge, *afr_gate, *pmp_dapf, *pmp_iorvbar, *nco, *fabric_ps; + void __iomem *sio_stop[ARRAY_SIZE(t6000_sio_stop_pa)]; + void __iomem *zero[ARRAY_SIZE(t6000_quiesce_zero_pa)]; + /* Where "ps", "ps-east" and "ps-aon" start, by their enum */ + resource_size_t block_start[T6000_BLOCKS]; + + unsigned int ndomains, ndomain_blocks; + bool domains_saved; + unsigned int domain_pass, domains_stuck; + + u32 aon_saved[ARRAY_SIZE(t6000_aon_commit_off)]; + bool aon_saved_valid; + + u32 sep_pg_saved; + bool sep_pg_cleared; + + u32 spmi_saved[T6000_SPMI_VEC_WORDS]; + bool spmi_armed; + + u64 ptd_req_saved; + u64 dvfs_saved[4]; + bool ptd_written; + u32 ptd_sleep_saved; + bool ptd_sleep_set; + + u32 afr_cluster_before; + bool afr_cluster_valid; + + u32 dcs_ctl_saved[T6000_AMCCS][T6000_DCS_PER_AMCC]; + u32 zero_saved[ARRAY_SIZE(t6000_quiesce_zero_pa)]; + u32 apcie_saved[2]; + u64 acc_saved[T6000_ACCS][T6000_ACCR_N]; + u64 relock_saved[2]; + u32 dcs_saved; + /* The dcs_* words: the first sleep's values taken, and what the commit did to them */ + bool dcs_ref_taken; + unsigned int dcs_floors, dcs_changed; + const struct t6000_domain *dcs_changed_first; + u32 dcs_changed_was; + u32 afr_saved; + u32 cluster_saved[T6000_CLUSTERS]; + u32 cluster_written; + /* T6000_INCOMPLETE_*, and the complexes whose target stayed: a domain of theirs was on */ + u32 incomplete, clusters_kept; + u32 mux_saved[ARRAY_SIZE(t6000_clkgen_mux)]; + u32 mux_n; + u32 pll_saved; + bool pll_done, dcs_done, afr_done, mcc_done, zeros_done, apcie_done, acc_done, relock_done; + + u32 wdt_saved_ctl, wdt_saved_bite; + bool wdt_saved; + bool aic_masked; + + u32 nco_saved[T6000_NCO_CHANNELS][T6000_NCO_WORDS]; + bool nco_saved_valid; + + u32 pmp_dapf_saved[T6000_PMP_DAPF_ENTRIES][T6000_PMP_DAPF_WORDS]; + unsigned int pmp_dapf_n; + u64 pmp_iorvbar_saved; + bool pmp_iorvbar_valid; +} t6000_sleep; + +static void __iomem *t6000_map(struct apple_sleep *s, const char *name, struct resource *res) +{ + struct device_node *np = s->dev->of_node; + int index = of_property_match_string(np, "reg-names", name); + + if (index < 0 || of_address_to_resource(np, index, res)) + return IOMEM_ERR_PTR(-ENODEV); + /* Shared with the drivers of these blocks, so not claimed. */ + return apple_sleep_map(s->dev, name); +} + +/* Mapped for good, non-posted (a posted write to these blocks is an SError), or NULL */ +static void __iomem *t6000_ioremap(struct apple_sleep *s, phys_addr_t pa, size_t len) +{ + void __iomem *base = ioremap_np(pa, len); + + if (base && devm_add_action_or_reset(s->dev, apple_sleep_iounmap, (void __force *)base)) + return NULL; + return base; +} + +static void __iomem *t6000_block(struct apple_sleep *s, unsigned int block) +{ + switch (block) { + case T6000_PS: return s->ps; + case T6000_PS_EAST: return t6000_sleep.ps_east; + case T6000_PS_AON: return s->ps_aon; + default: return NULL; + } +} + +static void __iomem *t6000_ps_reg(struct apple_sleep *s, struct t6000_ps_word word) +{ + return t6000_block(s, word.block) + word.offset; +} + +/* A PMGR node's block, mapped once however many of its domains are tracked */ +static void __iomem *t6000_domain_block(struct apple_sleep *s, struct device_node *parent, + resource_size_t *start) +{ + struct t6000_sleep *t = &t6000_sleep; + struct resource res; + unsigned int b; + + if (of_address_to_resource(parent, 0, &res)) + return NULL; + *start = res.start; + for (b = 0; b < t->ndomain_blocks; b++) + if (t6000_domain_blocks[b].start == res.start) + return t6000_domain_blocks[b].base; + if (t->ndomain_blocks == T6000_DOMAIN_BLOCKS) + return NULL; + t6000_domain_blocks[b].start = res.start; + t6000_domain_blocks[b].base = t6000_ioremap(s, res.start, resource_size(&res)); + if (t6000_domain_blocks[b].base) + t->ndomain_blocks++; + return t6000_domain_blocks[b].base; +} + +static void t6000_domains_init(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + struct device_node *np, *parent; + resource_size_t start; + void __iomem *base; + const char *label; + unsigned int i; + u32 off; + + for_each_compatible_node(np, NULL, "apple,pmgr-pwrstate") { + struct t6000_domain *d = &t6000_domains[t->ndomains]; + + if (t->ndomains == T6000_DOMAINS_MAX) { + of_node_put(np); + break; + } + label = NULL; + of_property_read_string(np, "label", &label); + if (!label || !strcmp(label, "sep") || !strcmp(label, "afc") || + !strcmp(label, "afi")) + continue; + if (of_property_read_u32(np, "reg", &off)) + continue; + parent = of_get_parent(np); + base = parent ? t6000_domain_block(s, parent, &start) : NULL; + of_node_put(parent); + if (!base) + continue; + d->np = of_node_get(np); + d->reg = base + off; + d->name = label; + d->always_on = start == t->block_start[T6000_PS_AON]; + for (i = 0; i < ARRAY_SIZE(t6000_disp_ps); i++) + if (t->block_start[t6000_disp_ps[i].block] + t6000_disp_ps[i].offset == + start + off) + d->save_at_begin = true; + t->ndomains++; + } +} + +static void t6000_domains_save(bool at_begin) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + + t->domains_saved = false; + for (i = 0; i < t->ndomains; i++) + if (t6000_domains[i].save_at_begin == at_begin) + t6000_domains[i].saved = readl(t6000_domains[i].reg); + t->domains_saved = !at_begin; +} + +static int t6000_domain_find(struct device_node *np) +{ + unsigned int i; + + for (i = 0; i < t6000_sleep.ndomains; i++) + if (t6000_domains[i].np == np) + return i; + return -1; +} + +/* + * One domain back to its saved state, parents first: minimum state, target with AUTO_ENABLE + * off, then AUTO_ENABLE. A domain that doesn't follow is taken to 0 first on later passes. + */ +static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *seen) +{ + struct t6000_sleep *t = &t6000_sleep; + struct t6000_domain *d = &t6000_domains[i]; + struct of_phandle_iterator it; + u32 saved = d->saved, cur; + unsigned int n; + int err, parent; + bool followed; + + if (test_and_set_bit(i, seen) || depth > 8) + return; + if (d->always_on) + return; + if (!FIELD_GET(PS_ACTUAL, saved)) + return; + /* On with no target: not this word's to ask for, as a core's, which its complex powers. */ + if (!FIELD_GET(PS_TARGET, saved)) + return; + of_for_each_phandle(&it, err, d->np, "power-domains", "#power-domain-cells", 0) { + parent = t6000_domain_find(it.node); + if (parent >= 0) + t6000_domain_on(parent, depth + 1, seen); + } + cur = readl(d->reg); + if ((cur ^ saved) & T6000_PS_MIN) { + writel((cur & ~(T6000_PS_LATCHES | T6000_PS_MIN)) | (saved & T6000_PS_MIN), + d->reg); + cur = readl(d->reg); + } + if (FIELD_GET(PS_TARGET, cur) == FIELD_GET(PS_TARGET, saved) && + FIELD_GET(PS_ACTUAL, cur) >= FIELD_GET(PS_ACTUAL, saved)) + return; + cur &= ~(T6000_PS_LATCHES | PS_AUTO_ENABLE | PS_TARGET | T6000_PS_AUTO); + cur |= saved & T6000_PS_AUTO; + if (t->domain_pass && !FIELD_GET(PS_ACTUAL, readl(d->reg))) { + writel(cur & ~PS_ACTUAL, d->reg); + udelay(100); + } + writel(cur | (saved & PS_TARGET), d->reg); + for (n = 0; n < 2000 && FIELD_GET(PS_ACTUAL, readl(d->reg)) < + FIELD_GET(PS_ACTUAL, saved); n++) + udelay(10); + /* Judged before AUTO_ENABLE goes back: with it, an idle domain gates itself at once. */ + cur = readl(d->reg); + followed = FIELD_GET(PS_ACTUAL, cur) >= FIELD_GET(PS_ACTUAL, saved); + cur &= ~(T6000_PS_LATCHES | PS_ACTUAL); + if ((saved & PS_AUTO_ENABLE) && !t->domain_pass) + writel(cur | PS_AUTO_ENABLE, d->reg); + cur = readl(d->reg); + if ((cur & T6000_PS_RESET) && !(saved & T6000_PS_RESET) && FIELD_GET(PS_ACTUAL, cur)) { + writel(cur & ~(T6000_PS_RESET | T6000_PS_BUSY | PS_FLAGS | PS_ACTUAL), d->reg); + udelay(1); + cur = readl(d->reg); + } + if (!followed) + t->domains_stuck++; +} + +/* Until every domain is back, or T6000_DOMAIN_PASSES passes 10 ms apart */ +static void t6000_domains_restore(void) +{ + struct t6000_sleep *t = &t6000_sleep; + DECLARE_BITMAP(seen, T6000_DOMAINS_MAX); + unsigned int i, pass; + + if (!t->domains_saved) + return; + t->domains_saved = false; + for (pass = 0; pass < T6000_DOMAIN_PASSES; pass++) { + if (pass) + mdelay(10); + t->domain_pass = pass; + t->domains_stuck = 0; + bitmap_zero(seen, T6000_DOMAINS_MAX); + for (i = 0; i < t->ndomains; i++) + t6000_domain_on(i, 0, seen); + if (!t->domains_stuck) + break; + } +} + +static int t6000_sleep_map_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + static const char *const acc_names[T6000_ACCS] = { "acc0", "acc1", "acc2" }; + static const char *const amcc_names[T6000_AMCCS] = { "amcc0", "amcc1", "amcc2", "amcc3" }; + struct resource res, ps_east, ps_aon, amcc; + struct device_node *np; + unsigned int i, j, a, k; + int ret; + + s->ps = t6000_map(s, "ps", &s->ps_res); + t->ps_east = t6000_map(s, "ps-east", &ps_east); + s->ps_aon = t6000_map(s, "ps-aon", &ps_aon); + s->cpu_start = t6000_map(s, "cpu-start", &res); + s->cluster = t6000_map(s, "cluster", &res); + s->commit = t6000_map(s, "commit", &res); + s->wdt = t6000_map(s, "wdt", &res); + s->wake = t6000_map(s, "wake", &res); + s->spmi = t6000_map(s, "spmi", &res); + t->sep_pg = t6000_map(s, "sep-pg", &res); + t->clkgen = t6000_map(s, "clkgen", &res); + t->pll0 = t6000_map(s, "pll0", &res); + t->dcs = t6000_map(s, "dcs", &res); + t->apcie_q = t6000_map(s, "apcie-q", &res); + t->ptd = t6000_map(s, "ptd", &res); + if (IS_ERR(s->ps) || IS_ERR(t->ps_east) || IS_ERR(s->ps_aon) || IS_ERR(s->cpu_start) || + IS_ERR(s->cluster) || IS_ERR(s->commit) || IS_ERR(s->wdt) || IS_ERR(s->wake) || + IS_ERR(s->spmi) || IS_ERR(t->sep_pg) || IS_ERR(t->clkgen) || IS_ERR(t->pll0) || + IS_ERR(t->dcs) || IS_ERR(t->apcie_q) || IS_ERR(t->ptd)) + return dev_err_probe(s->dev, -EINVAL, + "ps, ps-east, ps-aon, cpu-start, cluster, commit, wdt, wake, spmi, sep-pg, clkgen, pll0, dcs, apcie-q and ptd must all be mapped\n"); + t->block_start[T6000_PS] = s->ps_res.start; + t->block_start[T6000_PS_EAST] = ps_east.start; + t->block_start[T6000_PS_AON] = ps_aon.start; + + ret = apple_sleep_read_wake_sources(s); + if (ret) + return ret; + + for (i = 0; i < T6000_ACCS; i++) { + t->acc[i] = t6000_map(s, acc_names[i], &res); + if (IS_ERR(t->acc[i])) + return dev_err_probe(s->dev, PTR_ERR(t->acc[i]), "no %s registers\n", + acc_names[i]); + } + for (a = 0; a < T6000_AMCCS; a++) { + t->amcc[a] = t6000_map(s, amcc_names[a], &amcc); + if (IS_ERR(t->amcc[a])) + return dev_err_probe(s->dev, PTR_ERR(t->amcc[a]), "no %s registers\n", + amcc_names[a]); + /* Its DCS channel blocks, above the controller's own: left alone if unmapped */ + for (k = 0; k < T6000_DCS_PER_AMCC; k++) + t->dcs_block[a][k] = t6000_ioremap(s, amcc.start + T6000_DCS_BLOCK(k), + T6000_DCS_BLOCK_LEN); + } + + /* + * The blocks with no register entry here. Each is used only if it mapped, as the + * steps that write them say. + */ + for (i = 0; i < ARRAY_SIZE(t6000_disp_bridge_pa); i++) + t->disp_bridge[i] = t6000_ioremap(s, t6000_disp_bridge_pa[i], T6000_BRIDGE_LEN); + t->gfx_bridge = t6000_ioremap(s, T6000_GFX_BRIDGE_PA, T6000_BRIDGE_LEN); + t->afr_gate = t6000_ioremap(s, T6000_AFR_GATE, 4); + for (i = 0; i < ARRAY_SIZE(t6000_sio_stop_pa); i++) + t->sio_stop[i] = t6000_ioremap(s, t6000_sio_stop_pa[i], 4); + for (i = 0; i < ARRAY_SIZE(t6000_quiesce_zero_pa); i++) + t->zero[i] = t6000_ioremap(s, t6000_quiesce_zero_pa[i], 4); + + s->driver_down = devm_bitmap_zalloc(s->dev, resource_size(&s->ps_res) / 4, GFP_KERNEL); + if (!s->driver_down) + return -ENOMEM; + + t->pmp_dapf = t6000_ioremap(s, T6000_PMP_DAPF, + T6000_PMP_DAPF_ENTRIES * T6000_PMP_DAPF_STRIDE); + if (t->pmp_dapf) { + for (i = 0; i < T6000_PMP_DAPF_ENTRIES; i++) { + void __iomem *entry = t->pmp_dapf + i * T6000_PMP_DAPF_STRIDE; + + if (!(readl(entry) & T6000_PMP_DAPF_IN_USE)) + break; + for (j = 0; j < T6000_PMP_DAPF_WORDS; j++) + t->pmp_dapf_saved[i][j] = readl(entry + 4 * j); + } + t->pmp_dapf_n = i; + } + t->pmp_iorvbar = t6000_ioremap(s, T6000_PMP_IORVBAR, 8); + if (t->pmp_iorvbar) { + t->pmp_iorvbar_saved = readq(t->pmp_iorvbar); + t->pmp_iorvbar_valid = true; + } + + np = of_find_compatible_node(NULL, NULL, "apple,t6000-nco"); + if (np && !of_address_to_resource(np, 0, &res) && + resource_size(&res) >= T6000_NCO_CHANNELS * T6000_NCO_STRIDE) + t->nco = t6000_ioremap(s, res.start, T6000_NCO_CHANNELS * T6000_NCO_STRIDE); + of_node_put(np); + + np = of_find_compatible_node(NULL, NULL, "apple,t6000-pmgr-misc"); + if (np && !of_address_to_resource(np, 0, &res)) + t->fabric_ps = t6000_ioremap(s, res.start, 4); + of_node_put(np); + + t6000_domains_init(s); + + return apple_sleep_alloc_books(s); +} + +/* @secs from now. What WD1 had before the first arm of a sleep is kept for .finish. */ +static void t6000_wdt_arm(struct apple_sleep *s, unsigned int secs) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (!t->wdt_saved) { + t->wdt_saved_ctl = readl(s->wdt + WDT_WD1_CTL); + t->wdt_saved_bite = readl(s->wdt + WDT_WD1_BITE); + t->wdt_saved = true; + } + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD1_BITE); + writel(WDT_WD1_RESET_EN, s->wdt + WDT_WD1_CTL); + dsb(sy); +} + +static void t6000_wdt_restore(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (!t->wdt_saved) + return; + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(t->wdt_saved_bite, s->wdt + WDT_WD1_BITE); + writel(t->wdt_saved_ctl, s->wdt + WDT_WD1_CTL); + t->wdt_saved = false; +} + +/* + * The L1 data cache written back by set and way, the EL02 timers masked, and the overrides + * for a WFI without retention set, as XNU has them on T6000. + */ +static void t6000_deep_wfi_arm(void) +{ + u64 acc, cyc; + + if (is_kernel_in_hyp_mode()) { + write_sysreg_s(0, SYS_CNTP_CTL_EL02); + write_sysreg_s(0, SYS_CNTV_CTL_EL02); + } + + acc = read_sysreg_s(T6000_ACC_OVRD); + acc &= ~(T6000_ACC_PWR_DN_SRM | T6000_ACC_DIS_L2_FLUSH | + T6000_ACC_TRAIN_DOWN_LINK | T6000_ACC_POWER_DOWN_CPM); + acc |= FIELD_PREP(T6000_ACC_PWR_DN_SRM, 3) | + FIELD_PREP(T6000_ACC_DIS_L2_FLUSH, 2) | + FIELD_PREP(T6000_ACC_TRAIN_DOWN_LINK, 3) | + FIELD_PREP(T6000_ACC_POWER_DOWN_CPM, 3) | + T6000_ACC_DIS_PIO_WFI | T6000_ACC_DEEP_SLEEP; + + cyc = read_sysreg_s(T6000_CYC_OVRD); + cyc &= ~(T6000_CYC_WFI_MODE | T6000_CYC_IRQ_MODE | T6000_CYC_FIQ_MODE); + cyc |= FIELD_PREP(T6000_CYC_WFI_MODE, 3) | T6000_CYC_DISABLE_WFI_RET; + + /* + * No return once SCTLR_EL1.C is clear: the write-back only reaches L2, so a stack pop + * with the data cache off could read stale DRAM before the WFI. + */ + asm volatile( + "msr\tdaifset, #0xf\n\t" + "mrs\tx3, sctlr_el1\n\t" + "bic\tx3, x3, %[cbit]\n\t" + "msr\tsctlr_el1, x3\n\t" + "isb\n\t" + "msr\tcsselr_el1, xzr\n\t" + "isb\n\t" + "mrs\tx3, ccsidr_el1\n\t" + "and\tx4, x3, #7\n\t" + "add\tx4, x4, #4\n\t" + "ubfx\tx5, x3, #3, #10\n\t" + "ubfx\tx6, x3, #13, #15\n\t" + "clz\tw7, w5\n\t" + "1:\n\t" + "mov\tx8, x6\n\t" + "2:\n\t" + "lsl\tx9, x5, x7\n\t" + "lsl\tx10, x8, x4\n\t" + "orr\tx9, x9, x10\n\t" + "dc\tcisw, x9\n\t" + "subs\tx8, x8, #1\n\t" + "b.ge\t2b\n\t" + "subs\tx5, x5, #1\n\t" + "b.ge\t1b\n\t" + "dsb\tsy\n\t" + "isb\n\t" + "msr\tcntp_ctl_el0, xzr\n\t" + "msr\tcntv_ctl_el0, xzr\n\t" + "msr\ts3_1_c15_c0_0, xzr\n\t" + "msr\ts3_5_c15_c6_0, %[acc]\n\t" + "msr\ts3_5_c15_c5_0, %[cyc]\n\t" + "isb\n\t" + "mov\tx3, #1\n\t" + "3:\n\t" + "dsb\tsy\n\t" + "isb\n\t" + "wfi\n\t" + "msr\ts3_5_c15_c1_1, x3\n\t" + "b\t3b\n\t" + : + : [cbit] "i" (SCTLR_ELx_C), [acc] "r" (acc), [cyc] "r" (cyc) + : "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "memory" + ); + unreachable(); +} + +/* Masked once, before the timer: the shared commit's call after it then changes nothing. */ +static void t6000_aic_mask_all(void) +{ + if (t6000_sleep.aic_masked) + return; + aic_sleep_mask_all(); + t6000_sleep.aic_masked = true; +} + +static void t6000_aic_restore(void) +{ + if (!t6000_sleep.aic_masked) + return; + aic_sleep_restore(); + t6000_sleep.aic_masked = false; +} + +/* The bridge's handshake, see T6000_BRIDGE_8014 */ +static void t6000_bridge_quiesce(void __iomem *bridge) +{ + writel(1, bridge + T6000_BRIDGE_8014); + writel(1, bridge + T6000_BRIDGE_82BC); + writel(2, bridge + T6000_BRIDGE_82BC); + writel(4, bridge + T6000_BRIDGE_82BC); +} + +/* A device's request to the PMP set or cleared, and its acknowledgment waited for */ +static int t6000_ptd_request(u64 request, bool on) +{ + void __iomem *ptd = t6000_sleep.ptd; + u64 val = readq(ptd + T6000_PTD_PS_REQ_RD); + + writeq(on ? val | request : val & ~request, ptd + T6000_PTD_PS_REQ_WR); + return readq_poll_timeout_atomic(ptd + T6000_PTD_PS_ACK_RD, val, + on ? val & request : !(val & request), 10, 200000); +} + +/* + * The GPU's way down: AFR and GFX up, GFX released from the PMP, the bridge quiesced, then + * GFX and AFR lowered again. + */ +static void t6000_gfx_down(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + void __iomem *gfx = t->ps_east + T6000_PS_EAST_GFX; + void __iomem *afr = s->ps + T6000_PS_AFR; + void __iomem *afr_cluster = s->cluster + 4 * T6000_CLUSTER_AFR; + bool gfx_on = true; + u32 val; + + if (!t->gfx_bridge) + return; + + t->afr_cluster_before = readl(afr_cluster); + t->afr_cluster_valid = true; + writel(CLUSTER_ON, afr_cluster); + readl_poll_timeout_atomic(afr_cluster, val, val & CLUSTER_UP_DONE, 1, 50000); + writel(CLUSTER_ON | CLUSTER_UP_DONE, afr_cluster); + + if (t->afr_gate) + writel(1, t->afr_gate); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 0xf, 4, 0, 0xf), afr); + readl_poll_timeout_atomic(afr, val, FIELD_GET(PS_ACTUAL, val), 1, 50000); + + writel(T6000_PS_WORD(false, 0xf, 0, 0), gfx); + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), gfx); + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), gfx); + + t6000_ptd_request(T6000_PTD_REQ_GFX, true); + + if (readl_poll_timeout_atomic(gfx, val, FIELD_GET(PS_ACTUAL, val), 1, 50000)) + gfx_on = false; + + t6000_ptd_request(T6000_PTD_REQ_GFX, false); + + if (gfx_on && FIELD_GET(PS_ACTUAL, readl(gfx))) + t6000_bridge_quiesce(t->gfx_bridge); + + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), gfx); + writel(T6000_PS_WORD(false, 0xf, PS_FLAGS, 0), gfx); + readl_poll_timeout_atomic(gfx, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000); + + writel(T6000_PS_WORD_MIN(false, 4, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 0xf, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 4, 0xf, T6000_PS_WAS_CLKGATED, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 0xf, T6000_PS_WAS_CLKGATED, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 4, 4, PS_FLAGS, 0), afr); + readl_poll_timeout_atomic(afr, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000); + + if (t->afr_gate) + writel(0, t->afr_gate); + writel(0, afr_cluster); +} + +static int t6000_sleep_begin(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->ptd_sleep_set = false; + if (s->dry_run) + return 0; + /* The display drivers power their domains off in their own suspend. */ + t6000_domains_save(true); + t->ptd_sleep_saved = readl(t->ptd + T6000_PTD_SLEEP); + writel(T6000_PTD_SLEEP_BEGIN, t->ptd + T6000_PTD_SLEEP); + t->ptd_sleep_set = true; + return 0; +} + +/* SEP answers its sleep request only with the GPIO domain on. */ +static void t6000_before_enclave(struct apple_sleep *s) +{ + sleep_ps_raise(s, T6000_PS_GPIO); + sleep_note(s, 0x29); +} + +static int t6000_sleep_display_off(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int k, i; + u32 val, stuck = 0; + + /* Before anything in .enter touches the hardware */ + t6000_wdt_arm(s, 20); + + /* As macOS: SEP gets up to 5 s to power-gate after its sleep request's answer. */ + if (apple_sleep_enclave.stop) { + void __iomem *sep_ps = t->ps_east + T6000_PS_EAST_SEP; + + sleep_note(s, 0x2a); + if (readl_poll_timeout_atomic(sep_ps, val, !FIELD_GET(PS_ACTUAL, val), 100, + 5000000)) + t->incomplete |= T6000_INCOMPLETE_SEP; + sleep_note(s, 0x2b); + } + + /* Every PMGR domain as the drivers left it, before the steps below lower any */ + t6000_domains_save(false); + + /* The internal display's coprocessor, if its driver left it powered: its CPU off */ + if (s->display[0].asc && + FIELD_GET(PS_ACTUAL, readl(t6000_ps_reg(s, t6000_disp_ps[2])))) { + writel(readl(s->display[0].asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, + s->display[0].asc + ASC_CPU_CONTROL); + dsb(sy); + } + + t6000_wdt_arm(s, 35); + t6000_gfx_down(s); + t6000_wdt_arm(s, 50); + + for (k = 0; k < ARRAY_SIZE(t6000_disp_ps); k++) { + void __iomem *reg, *fe, *bridge; + unsigned int disp; + bool cpu; + + i = t6000_disp_order[k]; + disp = i / 2; + cpu = !(i & 1); + reg = t6000_ps_reg(s, t6000_disp_ps[i]); + fe = t6000_ps_reg(s, t6000_disp_ps[i | 1]); + bridge = t->disp_bridge[disp]; + + /* dispext0 is brought up first: its front end if it is off, then its CPU */ + if (i == 0) { + if (!FIELD_GET(PS_ACTUAL, readl(fe))) + sleep_ps_raise_start(s, T6000_PS_DISPEXT0_FE); + if (FIELD_GET(PS_ACTUAL, readl(reg)) != 0xf) + sleep_ps_raise_start(s, T6000_PS_DISPEXT0_CPU0); + } + /* A front end that is off has no bridge to quiesce. */ + if (bridge && !FIELD_GET(PS_ACTUAL, readl(fe))) + bridge = NULL; + + if (cpu) { + t6000_ptd_request(t6000_disp_request[disp], false); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf), reg); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0), reg); + } else { + if (bridge) + t6000_bridge_quiesce(bridge); + writel(T6000_PS_WORD(false, 0, PS_FLAGS, 0), reg); + } + if (readl_poll_timeout_atomic(reg, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000)) + stuck |= BIT(i); + } + + if (stuck) { + dev_err(s->dev, "T6000: display power words still on (mask %#x): refusing the sleep\n", + stuck); + t6000_domains_restore(); + t6000_wdt_restore(s); + return -EBUSY; + } + t6000_wdt_arm(s, 80); + return 0; +} + +/* + * The internal display coprocessor's CPU domain off and on again with the system running, + * for apple_sleep_display_power_cycle(). Its word goes off as the entry writes it and comes + * back to the word it holds now, in the wake's order: the target with AUTO_ENABLE off until + * it reads full power, then AUTO_ENABLE. The front end stays on. + * + * The coprocessor's request to the PMP is withdrawn meanwhile, as at the entry: the PMP + * takes a block whose request it holds as running. It goes through the report's driver, + * which serializes it with the requests other devices make at this point. + * + * Returns -EIO if the domain is not on at the end: what is behind it may have no power. Any + * other error leaves it on. + */ +static int t6000_display_cycle(struct apple_sleep *s, unsigned int display) +{ + /* Its power domain where that is its report entry: any other node has no request. */ + struct device_node *request __free(device_node) = + of_parse_phandle(s->display[display].np, "power-domains", 0); + void __iomem *reg = t6000_ps_reg(s, t6000_disp_ps[T6000_DISP0_CPU]); + u32 was, val; + int ret, back; + + if (display) + return -EOPNOTSUPP; + + was = readl(reg); + /* Not on to begin with: nothing to cycle. */ + if (FIELD_GET(PS_TARGET, was) != FIELD_MAX(PS_TARGET)) + return -EIO; + + ret = request ? apple_pmp_report_entry_request(request, false) : 0; + if (ret) { + apple_pmp_report_entry_request(request, true); + return ret; + } + + writel(T6000_DISP_CPU_WORD_MANUAL, reg); + writel(T6000_DISP_CPU_WORD_OFF, reg); + ret = readl_poll_timeout_atomic(reg, val, !FIELD_GET(PS_ACTUAL, val), 1, + T6000_DISP_OFF_TIMEOUT_US); + + writel(was & ~(T6000_PS_LATCHES | PS_ACTUAL | PS_AUTO_ENABLE), reg); + back = readl_poll_timeout_atomic(reg, val, + FIELD_GET(PS_ACTUAL, val) == FIELD_GET(PS_TARGET, was), + 10, T6000_DISP_ON_TIMEOUT_US); + if (back) { + dev_err(s->dev, "T6000: display coprocessor power word not back on: %#x (was %#x)\n", + val, was); + return -EIO; + } + if (was & PS_AUTO_ENABLE) + writel(was & ~(T6000_PS_LATCHES | PS_ACTUAL), reg); + + back = request ? apple_pmp_report_entry_request(request, true) : 0; + if (back) + dev_warn(s->dev, "T6000: the PMP did not take the display's request again: %d\n", + back); + return ret ?: back; +} + +static int t6000_sleep_final_check(struct apple_sleep *s) +{ + t6000_wdt_arm(s, 90); + return 0; +} + +static void t6000_sleep_prepare_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i, n; + u64 val; + + /* A hang in the PMP's or the SMC's last steps or in the quiesce resets the SoC. */ + t6000_wdt_arm(s, 100); + sleep_note(s, 0x70); + + for (i = 0; i < ARRAY_SIZE(t6000_aon_commit_off); i++) + t->aon_saved[i] = readl(s->ps_aon + t6000_aon_commit_off[i].offset); + t->aon_saved_valid = true; + + /* + * Only a SEP that took its sleep request in this entry gives the sequencer the power + * gate it waits for. An idle, running SEP reads an actual state of 0 as well. + */ + t->sep_pg_saved = readl(t->sep_pg); + t->sep_pg_cleared = !apple_sleep_enclave.stop && (t->sep_pg_saved & T6000_SEP_PG_WAIT); + if (t->sep_pg_cleared) + writel(t->sep_pg_saved & ~T6000_SEP_PG_WAIT, t->sep_pg); + + sleep_note(s, 0x71); + /* The PMP's sleep tail, right before the coprocessors' last stop idles it */ + t->ptd_req_saved = readq(t->ptd + T6000_PTD_PS_REQ_RD); + writeq(0, t->ptd + T6000_PTD_PS_REQ_WR); + readq_poll_timeout_atomic(t->ptd + T6000_PTD_PS_ACK_RD, val, !(u32)val, 100, 200000); + for (i = 0; i < 4; i++) + t->dvfs_saved[i] = readq(t->ptd + T6000_PTD_DVFS_RD(i)); + /* As recorded: all four twice, the sleep word, then the first three once more */ + for (n = 0; n < 2; n++) + for (i = 0; i < 4; i++) + writeq(T6000_PTD_DVFS_IDLE, t->ptd + T6000_PTD_DVFS_WR(i)); + writel(T6000_PTD_SLEEP_COMMIT, t->ptd + T6000_PTD_SLEEP); + for (i = 0; i < 3; i++) + writeq(T6000_PTD_DVFS_IDLE, t->ptd + T6000_PTD_DVFS_WR(i)); + t->ptd_written = true; + sleep_note(s, 0x72); +} + +static void t6000_sleep_undo_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (t->sep_pg_cleared) { + writel(t->sep_pg_saved, t->sep_pg); + t->sep_pg_cleared = false; + } +} + +/* A complex's target goes off only with every domain it powers reading off. */ +static bool t6000_cluster_gate_ok(struct apple_sleep *s, unsigned int cluster) +{ + const struct t6000_ps_word *word; + + switch (cluster) { + case T6000_CLUSTER_ANE0: + word = t6000_gate_ane0; + break; + case T6000_CLUSTER_AFR: + word = t6000_gate_afr; + break; + case T6000_CLUSTER_DISPLAY: + word = t6000_gate_display; + break; + case T6000_CLUSTER_AVEMSR: + word = t6000_gate_avemsr; + break; + default: + return true; + } + + for (; word->offset; word++) + if (FIELD_GET(PS_ACTUAL, readl(t6000_ps_reg(s, *word)))) + return false; + return true; +} + +static bool t6000_dcs_set(u32 state) +{ + void __iomem *dcs = t6000_sleep.dcs; + u32 val = readl(dcs); + + writel((val & ~T6000_DCS_PS_TARGET) | FIELD_PREP(T6000_DCS_PS_TARGET, state), dcs); + return !readl_poll_timeout_atomic(dcs, val, !(val & T6000_DCS_PS_BUSY), 1, 8000); +} + +/* + * The fields of a dcs_* word that are compared with its first sleep's: the state it has + * reached only while it isn't automatic in both. + */ +static u32 t6000_dcs_word_mask(u32 a, u32 b) +{ + u32 mask = PS_TARGET | PS_ACTUAL | PS_DEV_DISABLE | T6000_PS_MIN | PS_AUTO_ENABLE; + + if (a & b & PS_AUTO_ENABLE) + mask &= ~PS_ACTUAL; + return mask; +} + +/* + * The dcs_* words before the commit, as the reference has them: the wake waits for each to + * reach 0. The first sleep clears a minimum state and keeps what each word reads; later + * sleeps put a differing word back to that. + */ +static void t6000_dcs_words_commit(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + u32 val, ref; + + for (i = 0; i < t->ndomains; i++) { + struct t6000_domain *d = &t6000_domains[i]; + + if (strncmp(d->name, "dcs_", 4)) + continue; + val = readl(d->reg); + if (!t->dcs_ref_taken) { + if (val & T6000_PS_MIN) { + writel(val & ~(T6000_PS_MIN | T6000_PS_RESET | PS_FLAGS), d->reg); + udelay(100); + t->dcs_floors++; + val = readl(d->reg); + } + d->commit_ref = val; + continue; + } + ref = d->commit_ref; + if (!((val ^ ref) & t6000_dcs_word_mask(val, ref)) || (ref & T6000_PS_MIN)) + continue; + writel((val & ~(PS_TARGET | PS_AUTO_ENABLE | T6000_PS_MIN | T6000_PS_RESET | + PS_FLAGS)) | + (ref & (PS_TARGET | PS_AUTO_ENABLE | T6000_PS_MIN)), d->reg); + udelay(100); + if (!t->dcs_changed++) { + t->dcs_changed_first = d; + t->dcs_changed_was = val; + } + } + t->dcs_ref_taken = true; +} + +/* A channel enabled for the commit; every word as it was, for the wake */ +static void t6000_nco_enable(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int ch, w; + + if (!t->nco) + return; + for (ch = 0; ch < T6000_NCO_CHANNELS; ch++) { + void __iomem *chan = t->nco + ch * T6000_NCO_STRIDE; + + for (w = 0; w < T6000_NCO_WORDS; w++) + t->nco_saved[ch][w] = readl(chan + 4 * w); + if (!(t->nco_saved[ch][T6000_NCO_CTRL] & T6000_NCO_CTRL_ENABLE)) + writel(t->nco_saved[ch][T6000_NCO_CTRL] | T6000_NCO_CTRL_ENABLE, + chan + T6000_NCO_CTRL); + } + t->nco_saved_valid = true; +} + +/* The control word with the enable off, the other four words, then the control word */ +static void t6000_nco_restore(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int ch, w; + + if (!t->nco_saved_valid) + return; + t->nco_saved_valid = false; + for (ch = 0; ch < T6000_NCO_CHANNELS; ch++) { + void __iomem *chan = t->nco + ch * T6000_NCO_STRIDE; + + writel(t->nco_saved[ch][T6000_NCO_CTRL] & ~T6000_NCO_CTRL_ENABLE, + chan + T6000_NCO_CTRL); + for (w = 1; w < T6000_NCO_WORDS; w++) + writel(t->nco_saved[ch][w], chan + 4 * w); + writel(t->nco_saved[ch][T6000_NCO_CTRL], chan + T6000_NCO_CTRL); + } +} + +/* + * Final PMGR, clock, fabric p-state and SIO commit programming immediately before the S2R + * timer. Only domains and muxes whose state differs from the running system are written. + */ +static void t6000_commit_late_state(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + u32 val; + + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_AFNC2_LW1); + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO); + + for (i = 0; i < ARRAY_SIZE(t6000_aon_commit_off); i++) + writel(t6000_aon_commit_off[i].value, + s->ps_aon + t6000_aon_commit_off[i].offset); + + for (i = 0; i < ARRAY_SIZE(t6000_ps_commit_state); i++) + writel(t6000_ps_commit_state[i].value, + t6000_block(s, t6000_ps_commit_state[i].block) + + t6000_ps_commit_state[i].offset); + + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_late_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_late_mux[i]; + + val = readl(reg); + writel((val & ~T6000_CLKGEN_MUX_SEL) | + FIELD_PREP(T6000_CLKGEN_MUX_SEL, T6000_CLKGEN_LATE_SEL), reg); + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000); + } + + if (t->fabric_ps) { + writel(SOC_SLEEP_STATE, t->fabric_ps); + sleep_perf_wait(t->fabric_ps, SOC_SLEEP_STATE); + } + + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO_CPU); + if (!readl_poll_timeout_atomic(t->ps_east + T6000_PS_EAST_SIO_CPU, val, + FIELD_GET(PS_ACTUAL, val) == 0xf, 1, 2000)) { + for (i = 0; i < ARRAY_SIZE(t6000_sio_stop_pa); i++) + if (t->sio_stop[i]) + writel(0, t->sio_stop[i]); + } + + writel(0, s->ps + T6000_PS_PCIE0_REF); + writel(0, s->ps + T6000_PS_APCIE_GP_SYS); + + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_off_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_off_mux[i]; + + writel(0, reg); + if (readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 100)) + t->incomplete |= T6000_INCOMPLETE_LATE_MUX; + } +} + +static void t6000_sleep_commit_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int a, i, r, n; + u32 val; + u64 qval; + + sleep_note(s, 0x73); + + /* The halted PMP and its SRAM down, so the memory clock and the sequencer can follow */ + sleep_ps_lower(s, T6000_PS_PMP); + if (sleep_ps_wait_off(s, T6000_PS_PMP)) + t->incomplete |= T6000_INCOMPLETE_PMP; + sleep_ps_lower(s, T6000_PS_PMS_SRAM); + if (sleep_ps_wait_off(s, T6000_PS_PMS_SRAM)) + t->incomplete |= T6000_INCOMPLETE_PMS_SRAM; + + /* 1. The memory controllers: enable DCS low-power gating, then drain */ + t->mcc_done = true; + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_block[a][i]) { + t->dcs_ctl_saved[a][i] = + readl(t->dcs_block[a][i] + T6000_DCS_CTL); + writel(t->dcs_ctl_saved[a][i] | T6000_DCS_CTL_LP, + t->dcs_block[a][i] + T6000_DCS_CTL); + } + for (a = 0; a < T6000_AMCCS; a++) + writel(1, t->amcc[a] + T6000_AMCC_LOCK); + for (a = 0; a < T6000_AMCCS; a++) + writel(0, t->amcc[a] + T6000_AMCC_DRAIN_CTL); + for (a = 0; a < T6000_AMCCS; a++) + writel(0, t->amcc[a] + T6000_AMCC_LOCK); + for (a = 0; a < T6000_AMCCS; a++) + readl_poll_timeout_atomic(t->amcc[a] + T6000_AMCC_DRAIN_COUNT, val, !val, + 1, 20000); + + /* 2. The error handlers, counters and voltage monitors */ + t->zeros_done = true; + for (i = 0; i < ARRAY_SIZE(t6000_quiesce_zero_pa); i++) { + if (!t->zero[i]) + continue; + t->zero_saved[i] = readl(t->zero[i]); + writel(0, t->zero[i]); + } + + sleep_note(s, 0x74); + /* 3. The PCIe bridge */ + t->apcie_done = true; + t->apcie_saved[0] = readl(t->apcie_q + T6000_BRIDGE_8014); + t->apcie_saved[1] = readl(t->apcie_q + T6000_BRIDGE_82BC); + t6000_bridge_quiesce(t->apcie_q); + + sleep_note(s, 0x75); + /* 4. The CPU complexes: counters and throttlers off, the PLL relock, APSC */ + t->acc_done = true; + for (i = 0; i < T6000_ACCS; i++) { + for (r = 0; r < T6000_ACCR_DVFS; r++) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + t->acc_saved[i][r] = readq(reg); + writeq(t->acc_saved[i][r] & ~t6000_acc_enable[r], reg); + } + } + t->relock_done = true; + for (i = 0; i < 2; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + t->relock_saved[i] = readq(reg); + writeq(T6000_ACC_DVFS_RELOCK, reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), 1, 8000); + } + for (i = 0; i < T6000_ACCS; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + t->acc_saved[i][T6000_ACCR_DVFS] = readq(reg); + for (n = 0; n < 2; n++) { + writeq(T6000_ACC_DVFS_APSC, reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } + } + + sleep_note(s, 0x76); + /* 5. The memory clock's state down, only with the PMP off, and AFR */ + t->dcs_done = false; + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + T6000_PS_PMP))) { + t->dcs_saved = readl(t->dcs); + t->dcs_done = true; + if (!t6000_dcs_set(T6000_DCS_PS_STEP)) + t->incomplete |= T6000_INCOMPLETE_DCS_BUSY; + if (!t6000_dcs_set(T6000_DCS_PS_SLEEP)) + t->incomplete |= T6000_INCOMPLETE_DCS_BUSY; + } else { + t->incomplete |= T6000_INCOMPLETE_DCS_KEPT; + } + t->afr_saved = readl(s->ps + T6000_PS_AFR); + t->afr_done = true; + writel(0, s->ps + T6000_PS_AFR); + readl_poll_timeout_atomic(s->ps + T6000_PS_AFR, val, !(val & PS_ACTUAL), 1, 8000); + + sleep_note(s, 0x77); + /* 6. The complexes' targets, all but the E cluster's, which goes last */ + t->cluster_written = 0; + for (i = 1; i < T6000_CLUSTERS; i++) { + void __iomem *reg = s->cluster + 4 * i; + + if (!(T6000_CLUSTERS_VALID & BIT(i))) + continue; + if (!t6000_cluster_gate_ok(s, i)) { + t->clusters_kept |= BIT(i); + continue; + } + t->cluster_saved[i] = readl(reg); + writel(t->cluster_saved[i] & ~T6000_CLUSTER_PS_TARGET, reg); + t->cluster_written |= BIT(i); + if (!(T6000_CLUSTERS_CPU & BIT(i))) + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_PS_BUSY), + 1, 10000); + } + + sleep_note(s, 0x78); + /* + * 7. The SoC clock muxes onto the reference clock, then PLL0 off, the three writes + * twice over as recorded. A mux that stays busy leaves PLL0 on. + */ + t->mux_n = 0; + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux[i]; + + val = readl(reg); + t->mux_saved[i] = val; + writel((val & ~T6000_CLKGEN_MUX_SEL) | T6000_CLKGEN_MUX_ON, reg); + t->mux_n = i + 1; + if (readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000)) { + t->incomplete |= T6000_INCOMPLETE_PLL_KEPT; + goto skip_pll; + } + } + t->pll_saved = readl(t->pll0); + t->pll_done = true; + for (n = 0; n < 2; n++) { + val = readl(t->pll0); + writel((val & ~T6000_PLL_OFF_MODE) | FIELD_PREP(T6000_PLL_OFF_MODE, 3), t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + val = readl(t->pll0); + writel(val | T6000_PLL_OFF_OK, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + val = readl(t->pll0); + writel(val & ~T6000_PLL_ON, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + } +skip_pll: + + sleep_note(s, 0x79); + /* 8. The other cores stopped, as macOS does it here, then the E cluster's target */ + s->cpus_stop_ret = spin_table_stop_parked(); + t->cluster_saved[T6000_CLUSTER_EACC] = readl(s->cluster); + writel(t->cluster_saved[T6000_CLUSTER_EACC] & ~T6000_CLUSTER_PS_TARGET, s->cluster); + t->cluster_written |= BIT(T6000_CLUSTER_EACC); + dsb(sy); + + /* 9. The dcs_* domains' words as the first sleep had them, and NCO channels enabled */ + t6000_dcs_words_commit(); + t6000_nco_enable(); + + sleep_note(s, 0x7a); + t6000_wdt_arm(s, 125); + + /* 10. Final PMGR, clock, fabric p-state and SIO state for the S2R sequencer */ + t6000_commit_late_state(s); + dsb(sy); + + /* 11. The PMU RTC alarm's SPMI vector: its latched status cleared, then enabled */ + for (i = 0; i < T6000_SPMI_VEC_WORDS; i++) + t->spmi_saved[i] = readl(s->spmi + T6000_SPMI_VEC_ENABLE + 4 * i); + writel(T6000_SPMI_VEC_RTC, s->spmi + T6000_SPMI_VEC_ACK); + writel(t->spmi_saved[0] | T6000_SPMI_VEC_RTC, s->spmi + T6000_SPMI_VEC_ENABLE); + t->spmi_armed = true; + + /* 12. Every interrupt line masked here, so the timer's is the last write */ + t6000_aic_mask_all(); + t6000_wdt_arm(s, 150); +} + +static void t6000_sleep_wake(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int a, i, j, r; + u32 val; + u64 qval; + + sleep_note(s, 0x7b); + /* For the rest of the wake, until .finish puts WD1 back */ + t6000_wdt_arm(s, T6000_WDT_WAKE_SECS); + + if (t->sep_pg_cleared) { + writel(t->sep_pg_saved, t->sep_pg); + t->sep_pg_cleared = false; + } + + if (t->spmi_armed) { + for (i = 0; i < T6000_SPMI_VEC_WORDS; i++) + writel(t->spmi_saved[i], s->spmi + T6000_SPMI_VEC_ENABLE + 4 * i); + t->spmi_armed = false; + } + + /* The DCS channels' control as before the commit */ + if (t->mcc_done) { + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_block[a][i]) + writel(t->dcs_ctl_saved[a][i], + t->dcs_block[a][i] + T6000_DCS_CTL); + t->mcc_done = false; + } + + /* PLL0 on, then the SoC clock muxes back, last first */ + if (t->pll_done) { + val = readl(t->pll0); + writel(val | T6000_PLL_ON, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + writel(t->pll_saved, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + t->pll_done = false; + } + while (t->mux_n) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux[--t->mux_n]; + + writel(t->mux_saved[t->mux_n], reg); + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000); + } + + /* + * The complexes' targets. AFR's was already 0 when the commit read it, after the GPU's + * way down: it goes back to what it was before that. + */ + for (i = 0; i < T6000_CLUSTERS; i++) { + void __iomem *reg = s->cluster + 4 * i; + + if (!(t->cluster_written & BIT(i))) + continue; + if (i == T6000_CLUSTER_AFR && t->afr_cluster_valid && + !(t->cluster_saved[i] & T6000_CLUSTER_PS_TARGET)) { + if ((readl(reg) ^ t->afr_cluster_before) & T6000_CLUSTER_PS_TARGET) + writel(t->afr_cluster_before, reg); + } else { + writel(t->cluster_saved[i], reg); + } + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_PS_BUSY), 1, 10000); + } + t->cluster_written = 0; + + if (t->afr_done) { + writel(t->afr_saved, s->ps + T6000_PS_AFR); + readl_poll_timeout_atomic(s->ps + T6000_PS_AFR, val, + FIELD_GET(PS_ACTUAL, val) == FIELD_GET(PS_TARGET, val), + 1, 8000); + t->afr_done = false; + } + if (t->dcs_done) { + t6000_dcs_set(T6000_DCS_PS_STEP); + t6000_dcs_set(FIELD_GET(T6000_DCS_PS_TARGET, t->dcs_saved)); + t->dcs_done = false; + } + + /* The CPU complexes: APSC as it was, then the other words, last first */ + if (t->acc_done) { + for (i = 0; i < T6000_ACCS; i++) { + for (r = T6000_ACCR_N; r-- > 0;) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + if (r == T6000_ACCR_DVFS) { + qval = readq(reg) & ~T6000_ACC_DVFS_APSC_OFF; + writeq(qval | (t->acc_saved[i][r] & + T6000_ACC_DVFS_APSC_OFF), reg); + readq_poll_timeout_atomic(reg, qval, + !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } else { + writeq(t->acc_saved[i][r], reg); + } + } + } + t->acc_done = false; + } + if (t->relock_done) { + for (i = 2; i-- > 0;) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + writeq(t->relock_saved[i], reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } + t->relock_done = false; + } + + if (t->zeros_done) { + for (i = ARRAY_SIZE(t6000_quiesce_zero_pa); i-- > 0;) + if (t->zero[i]) + writel(t->zero_saved[i], t->zero[i]); + t->zeros_done = false; + } + + /* The always-on power words the commit lowered: they kept that through the sleep */ + if (t->aon_saved_valid) { + for (i = ARRAY_SIZE(t6000_aon_commit_off); i-- > 0;) { + void __iomem *reg = s->ps_aon + t6000_aon_commit_off[i].offset; + u32 saved = t->aon_saved[i]; + + writel(saved & ~(PS_FLAGS | PS_ACTUAL), reg); + readl_poll_timeout_atomic(reg, val, + FIELD_GET(PS_ACTUAL, val) == + FIELD_GET(PS_TARGET, saved), 1, 5000); + } + t->aon_saved_valid = false; + } + + /* Every other PMGR domain that was on, back on */ + t6000_domains_restore(); + + /* The PMP's address filter and reset vector, before its driver restarts it */ + for (i = 0; t->pmp_dapf && i < t->pmp_dapf_n; i++) { + void __iomem *entry = t->pmp_dapf + i * T6000_PMP_DAPF_STRIDE; + + for (j = 1; j < T6000_PMP_DAPF_WORDS; j++) + writel(t->pmp_dapf_saved[i][j], entry + 4 * j); + writel(t->pmp_dapf_saved[i][0], entry); + } + if (t->pmp_iorvbar_valid && + !(readq(t->pmp_iorvbar) & T6000_PMP_IORVBAR_VALID)) + writeq(t->pmp_iorvbar_saved, t->pmp_iorvbar); + + t6000_nco_restore(); + + /* SEP's domain up before its DART resumes: automatic state and target 0xf */ + if (apple_sleep_enclave.stop) { + void __iomem *sep_ps = t->ps_east + T6000_PS_EAST_SEP; + + val = readl(sep_ps); + writel((val & ~(T6000_PS_AUTO | PS_DEV_DISABLE | PS_FLAGS)) | + FIELD_PREP(T6000_PS_AUTO, 0xf) | T6000_PS_WAS_CLKGATED | PS_TARGET, sep_ps); + readl_poll_timeout_atomic(sep_ps, val, FIELD_GET(PS_ACTUAL, val) == 0xf, + 10, 50000); + } + + /* SIO, then GPIO, on before the AP pin state is put back */ + val = readl(t->ps_east + T6000_PS_EAST_SIO); + writel((val & ~GENMASK(11, 0)) | PS_TARGET, t->ps_east + T6000_PS_EAST_SIO); + readl_poll_timeout_atomic(t->ps_east + T6000_PS_EAST_SIO, val, + FIELD_GET(PS_ACTUAL, val) == 0xf, 10, 50000); + sleep_ps_raise(s, T6000_PS_GPIO); + readl_poll_timeout_atomic(s->ps + T6000_PS_GPIO, val, FIELD_GET(PS_ACTUAL, val) == 0xf, + 10, 50000); + + /* + * The sleep resets every AP pin with the gpio domain, and the pin controller's driver + * restores only pins it configured: put all of them back now, before drivers resume. + */ + s->pins_lost = s->pins_saved_valid; + apple_sleep_restore_pins(); + + sleep_note(s, 0x7d); +} + +/* One line for the steps of the entry that were left out or did not finish, if any */ +static void t6000_report_incomplete(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + char steps[192] = ""; + unsigned int i, len = 0; + + if (!t->incomplete && !t->clusters_kept) + return; + for (i = 0; i < ARRAY_SIZE(t6000_incomplete_names); i++) + if (t->incomplete & BIT(i)) + len += scnprintf(steps + len, sizeof(steps) - len, "%s%s", + len ? ", " : "", t6000_incomplete_names[i]); + if (t->clusters_kept) + scnprintf(steps + len, sizeof(steps) - len, "%scomplexes %#x left on", + len ? ", " : "", t->clusters_kept); + dev_warn(s->dev, "T6000: sleep entry incomplete: %s\n", steps); + t->incomplete = 0; + t->clusters_kept = 0; +} + +/* At .wake and again at .end: what the entry changed in the PMP and the watchdog, once */ +static void t6000_sleep_finish(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + + if (t->ptd_sleep_set) { + writel(t->ptd_sleep_saved, t->ptd + T6000_PTD_SLEEP); + t->ptd_sleep_set = false; + } + + /* The devices' requests and the fabric DVFS words, with the PMP running again */ + if (t->ptd_written) { + u64 cur = readq(t->ptd + T6000_PTD_PS_REQ_RD); + + if (t->ptd_req_saved & ~cur) + writeq(cur | t->ptd_req_saved, t->ptd + T6000_PTD_PS_REQ_WR); + for (i = 0; i < 4; i++) + writeq(t->dvfs_saved[i], t->ptd + T6000_PTD_DVFS_WR(i)); + mdelay(10); + t->ptd_written = false; + } + + t6000_wdt_restore(s); + + t6000_report_incomplete(s); + if (t->dcs_floors) { + dev_dbg(s->dev, "T6000: minimum state cleared in %u dcs power words at the first sleep\n", + t->dcs_floors); + t->dcs_floors = 0; + } + if (t->dcs_changed) { + dev_info(s->dev, "T6000: %u dcs power words put back to their first sleep's state, %s from %#010x to %#010x\n", + t->dcs_changed, t->dcs_changed_first->name, t->dcs_changed_was, + readl(t->dcs_changed_first->reg)); + t->dcs_changed = 0; + } +} + +/* + * Nothing per core before its stop or after its start: on the deep sleep path the reference + * leaves each core's interrupt routing as it is. + */ +static int t6000_sleep_cpu_prepare(u64 mpidr) +{ + return 0; +} + +static void t6000_sleep_cpu_restore(u64 mpidr) +{ +} + +static const struct apple_sleep_ops t6000_sleep_ops = { + .deep_wfi_arm = t6000_deep_wfi_arm, + .resume = cpu_resume, + .aic_mask_all = t6000_aic_mask_all, + .aic_ack = aic_sleep_ack, + .aic_restore = t6000_aic_restore, + .map_regs = t6000_sleep_map_regs, + .begin = t6000_sleep_begin, + .before_enclave = t6000_before_enclave, + .display_off = t6000_sleep_display_off, + .display_cycle = t6000_display_cycle, + .final_check = t6000_sleep_final_check, + .prepare_regs = t6000_sleep_prepare_regs, + .undo_regs = t6000_sleep_undo_regs, + .commit_regs = t6000_sleep_commit_regs, + .wake = t6000_sleep_wake, + .finish = t6000_sleep_finish, + .cpu_prepare = t6000_sleep_cpu_prepare, + .cpu_restore = t6000_sleep_cpu_restore, +}; + +static const struct apple_sleep_soc t6000_sleep_soc = { + .ops = &t6000_sleep_ops, + .early_context = true, + /* The safety net is WD1, armed by the operations themselves. */ + .no_wd2 = true, + .no_pmu_notes = true, + .smc_gen1_keys = true, + .enclave_first = true, + .commit_stops_cpus = true, + .pmu_usid = 0xf, + .ndisp = 1, + /* MPIDR affinity 1 is the cluster: E, P0, P1; start bits 4 * cluster + core */ + .cpu_cluster_affinity = 1, + .cpu_clusters = 3, + .cpu_cores_per_cluster = 4, + .cpu_start_stride = 4, + .cluster_cpu = { 4 * T6000_CLUSTER_EACC, 4 * T6000_CLUSTER_PACC0, 4 * T6000_CLUSTER_PACC1 }, + .ncluster_cpu = 3, + .wake_mask = T6000_WAKE_MASK, + .wake_status = T6000_WAKE_STATUS, + .wake_words = T6000_WAKE_WORDS, + .wake_last_word = T6000_WAKE_LAST_WORD, + .wake_names = t6000_wake_names, + .nwake_names = ARRAY_SIZE(t6000_wake_names), + .ps = { + .gpio = T6000_PS_GPIO, + }, +}; diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index b86da75784b20..6e334f328aeac 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -13,6 +13,7 @@ static const char *const apple_sleep_test_profiles[] = { "apple,t6040-sleep", "apple,t6050-sleep", "apple,t6050-j716c-sleep", + "apple,t6000-sleep", "apple,t8103-sleep", "apple,t8112-sleep", "apple,t8122-sleep", diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index e6eaa106f88a1..cc48254c70598 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -631,6 +631,8 @@ struct apple_sleep_ops { void (*prepare_regs)(struct apple_sleep *s); void (*undo_regs)(struct apple_sleep *s); int (*display_off)(struct apple_sleep *s); + /* Optional: apple_sleep_display_power_cycle() for display coprocessor @display */ + int (*display_cycle)(struct apple_sleep *s, unsigned int display); int (*display_stop)(struct apple_sleep *s); /* After the always-on GPU stop, right before SEP's sleep request. */ void (*before_enclave)(struct apple_sleep *s); @@ -883,6 +885,13 @@ struct apple_sleep_soc { bool wdt_wd1; /* The SMC gets pmVC and WKTP before slpa (gen 1, as t6000 has them) */ bool smc_gen1_keys; + /* + * SEP's sleep request before ops->display_off: on T6000 the display's lowering waits + * for SEP's power gate. Neither can be taken back, so SEP's request is the last + * refusal, after every other check, and ops->display_off runs past it. + * ops->before_enclave runs before the request. + */ + bool enclave_first; /* The wait after the SMC's answer to slpa, if not the driver's */ unsigned int smc_settle_ms; /* SEP's word: DEV_DISABLE, and not an actual state of 0, says that it is down */ @@ -2372,6 +2381,39 @@ void apple_sleep_display_quiesced(struct device *dev, bool quiesced) } EXPORT_SYMBOL_GPL(apple_sleep_display_quiesced); +/** + * apple_sleep_display_power_cycle() - power a display coprocessor's CPU domain off and on again + * @dev: the coprocessor, one of the sleep controller's apple,display-coprocessors + * + * For its driver's resume after a refused sleep, when the firmware it stopped for the sleep + * only starts again from the power loss a real sleep gives it. The domain goes off as the + * sleep's entry takes it and comes back as it was. The caller has stopped the coprocessor's + * CPU and keeps the coprocessor's registers from being accessed until this returns. + * + * Context: process context, with the system's devices resuming or resumed. + * Return: 0 with the domain power-cycled and on. -ENODEV with no sleep controller, or for a + * device that is not one of its display coprocessors, and -EOPNOTSUPP where the SoC has no + * such sequence: nothing was written. -EIO if the domain is not on: the coprocessor's + * registers may have no power and must not be accessed. Any other error: the cycle did not + * complete, and the domain is on. + */ +int apple_sleep_display_power_cycle(struct device *dev) +{ + struct apple_sleep *s = apple_sleep; + unsigned int i; + + if (!s) + return -ENODEV; + if (!s->soc->ops->display_cycle) + return -EOPNOTSUPP; + for (i = 0; i < s->soc->ndisp; i++) + if (s->display[i].np == dev->of_node) + return s->soc->ops->display_cycle(s, i); + + return -ENODEV; +} +EXPORT_SYMBOL_GPL(apple_sleep_display_power_cycle); + /* * MPIDR's cluster is affinity 2, its core affinity 0, as 0.0.n and 1.1.n on T8140, unless the * SoC names another level for its clusters. @@ -6857,7 +6899,7 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) return apple_sleep_enter_dry(s); ret = apple_sleep_cpus_hold(s); late_display = sleep_display_late(s); - if (!ret && !late_display) { + if (!ret && !late_display && !s->soc->enclave_first) { step = "display prepare"; if (s->soc->regions) s->regs_staged = true; @@ -6922,6 +6964,12 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) /* No returning error path below this point. */ if (!s->soc->ops->display_stop) apple_sleep_coprocs_stop(s, true); + /* After the first stops: T6000's takes the GPU's domain down with the display's. */ + if (s->soc->enclave_first) { + ret = s->soc->ops->display_off(s); + if (ret) + panic("Apple display power-down after the SEP stop failed: %d", ret); + } /* Its driver's stop ran just above; past the last refusal, a failure goes on. */ if (late_display) { ret = s->soc->ops->display_off(s); @@ -10825,6 +10873,7 @@ static const struct apple_sleep_soc t8142_sleep_soc = { .wake_last_word = GENMASK(11, 0), }; +#include "sleep-t6000.h" #include "sleep-t8103.h" #include "sleep-t8122.h" #include "sleep-t8112.h" @@ -10843,6 +10892,7 @@ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t6040-sleep", .data = &t6040_sleep_soc }, { .compatible = "apple,t6050-sleep", .data = &t6050_sleep_soc }, { .compatible = "apple,t6050-j716c-sleep", .data = &t6050_j716c_sleep_soc }, + { .compatible = "apple,t6000-sleep", .data = &t6000_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/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index a6d07b2a7b6bc..cb06b87e832a7 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -155,6 +155,11 @@ struct apple_smc { /* apple,sleep-pmvc: when set, the pmVC value written at the sleep commit. */ bool sleep_pmvc_set; u32 sleep_pmvc; + /* apple,sleep-low-pins: SMC GPIOs low from the sleep notice to after waka. */ + u32 sleep_low_pins[4]; + unsigned int sleep_low_pin_count; + /* Bit n: sleep_low_pins[n] was lowered for this sleep. */ + unsigned long sleep_low_pins_lowered; /* To be told at the noirq suspend, after the debug link's cut. */ bool sleep_warn_late; /* In a noirq phase: replies are polled for. */ diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 47e646644b429..43a841f064888 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -435,6 +435,12 @@ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control) */ int apple_rtkit_sleep_return_held(struct apple_rtkit *rtk); +/* + * As apple_rtkit_sleep_return(), for a firmware that boots from scratch: the whole handshake, + * then the endpoints that were started. The caller checks pm_resume_via_firmware(). + */ +int apple_rtkit_sleep_return_with_init(struct apple_rtkit *rtk, void __iomem *cpu_control); + /* * A start as after a real sleep, for a co-processor its driver power-cycled without one: * the warm restart, the run bit set, then RTKit's handshake. diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 39ab320931c91..6e8c76922fdeb 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -103,6 +103,8 @@ int apple_sleep_smc_register_saved(struct device *dev, void apple_sleep_smc_unregister(struct device *dev); /* Driver attests firmware idle AND its DART/GAPF release, never a generic stop request. */ void apple_sleep_display_quiesced(struct device *dev, bool quiesced); +/* A display coprocessor's CPU domain off and on again, for its restart after a refused sleep. */ +int apple_sleep_display_power_cycle(struct device *dev); /* Read-only handoff: provider restored domains/gates, prior errors refuse. */ int apple_sleep_display_v27_power_ready(struct device *dev); /* T6050: the internal display's power words around its coprocessor's restart after a wake. */ @@ -376,6 +378,7 @@ static inline int apple_sleep_smc_register_saved(struct device *dev, const struct apple_sleep_smc_saved_ops *ops, void *data) { return -EOPNOTSUPP; } static inline void apple_sleep_smc_unregister(struct device *dev) {} static inline void apple_sleep_display_quiesced(struct device *dev, bool quiesced) {} +static inline int apple_sleep_display_power_cycle(struct device *dev) { return -EOPNOTSUPP; } static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, int (*last_stop)(void *data), void *data) diff --git a/rust/kernel/soc/apple/rtkit.rs b/rust/kernel/soc/apple/rtkit.rs index 22d0bf9bbcfc3..ffdcdcdd301c3 100644 --- a/rust/kernel/soc/apple/rtkit.rs +++ b/rust/kernel/soc/apple/rtkit.rs @@ -427,6 +427,31 @@ impl RtKit { unsafe { bindings::apple_rtkit_set_oslog_echo(self.rtk, echo) }; } + /// Parks a running coprocessor for a system sleep: AP and IOP state idle, the mailbox off, + /// the CPU's run bit cleared. + /// + /// # Safety + /// + /// `cpu_control` must be the mapped CPU control word of this coprocessor's ASC. + pub unsafe fn sleep_stop(self: Pin<&mut Self>, cpu_control: *mut core::ffi::c_void) -> Result { + // SAFETY: `rtk` is valid per the type invariant, `cpu_control` per the caller. + to_result(unsafe { bindings::apple_rtkit_sleep_stop(self.rtk, cpu_control) }) + } + + /// Restarts a parked coprocessor whose firmware lost its memory in the sleep: the whole + /// boot handshake, the application endpoints started again, then the AP's state on. + /// + /// # Safety + /// + /// `cpu_control` must be the mapped CPU control word of this coprocessor's ASC. + pub unsafe fn sleep_return_with_init( + self: Pin<&mut Self>, + cpu_control: *mut core::ffi::c_void, + ) -> Result { + // SAFETY: `rtk` is valid per the type invariant, `cpu_control` per the caller. + to_result(unsafe { bindings::apple_rtkit_sleep_return_with_init(self.rtk, cpu_control) }) + } + /// Checks if an endpoint is present pub fn has_endpoint(self: Pin<&mut Self>, endpoint: u8) -> bool { unsafe { bindings::apple_rtkit_has_endpoint(self.rtk, endpoint) }