|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only OR MIT |
| 2 | +/* |
| 3 | + * Copyright The Asahi Linux Contributors |
| 4 | + * |
| 5 | + * CPU idle support for Apple SoCs |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/init.h> |
| 9 | +#include <linux/bitfield.h> |
| 10 | +#include <linux/cpuidle.h> |
| 11 | +#include <linux/cpu_pm.h> |
| 12 | +#include <linux/platform_device.h> |
| 13 | +#include <linux/of.h> |
| 14 | +#include <asm/cpuidle.h> |
| 15 | + |
| 16 | +#define DEEP_WFI_STATE_RETENTION BIT(2) // retains base CPU registers in deep WFI |
| 17 | + |
| 18 | +enum idle_state { |
| 19 | + STATE_WFI, |
| 20 | + STATE_PWRDOWN, |
| 21 | + STATE_COUNT |
| 22 | +}; |
| 23 | + |
| 24 | +asm( |
| 25 | + ".pushsection .cpuidle.text, \"ax\"\n" |
| 26 | + ".type apple_cpu_deep_wfi, @function\n" |
| 27 | + "apple_cpu_deep_wfi:\n" |
| 28 | + "str x30, [sp, #-16]!\n" |
| 29 | + "stp x28, x29, [sp, #-16]!\n" |
| 30 | + "stp x26, x27, [sp, #-16]!\n" |
| 31 | + "stp x24, x25, [sp, #-16]!\n" |
| 32 | + "stp x22, x23, [sp, #-16]!\n" |
| 33 | + "stp x20, x21, [sp, #-16]!\n" |
| 34 | + "stp x18, x19, [sp, #-16]!\n" |
| 35 | + |
| 36 | + "mrs x0, s3_5_c15_c5_0\n" |
| 37 | + "orr x0, x0, #(3L << 24)\n" |
| 38 | + "msr s3_5_c15_c5_0, x0\n" |
| 39 | + |
| 40 | + "1:\n" |
| 41 | + "dsb sy\n" |
| 42 | + "wfi\n" |
| 43 | + |
| 44 | + "mrs x0, ISR_EL1\n" |
| 45 | + "cbz x0, 1b\n" |
| 46 | + |
| 47 | + "mrs x0, s3_5_c15_c5_0\n" |
| 48 | + "bic x0, x0, #(1L << 24)\n" |
| 49 | + "msr s3_5_c15_c5_0, x0\n" |
| 50 | + |
| 51 | + "ldp x18, x19, [sp], #16\n" |
| 52 | + "ldp x20, x21, [sp], #16\n" |
| 53 | + "ldp x22, x23, [sp], #16\n" |
| 54 | + "ldp x24, x25, [sp], #16\n" |
| 55 | + "ldp x26, x27, [sp], #16\n" |
| 56 | + "ldp x28, x29, [sp], #16\n" |
| 57 | + "ldr x30, [sp], #16\n" |
| 58 | + |
| 59 | + "ret\n" |
| 60 | + ".popsection\n" |
| 61 | +); |
| 62 | + |
| 63 | +void apple_cpu_deep_wfi(void); |
| 64 | + |
| 65 | +static __cpuidle int apple_enter_wfi(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) |
| 66 | +{ |
| 67 | + cpu_do_idle(); |
| 68 | + return index; |
| 69 | +} |
| 70 | + |
| 71 | +static __cpuidle int apple_enter_idle(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) |
| 72 | +{ |
| 73 | + /* |
| 74 | + * Deep WFI will clobber FP state, among other things. |
| 75 | + * The CPU PM notifier will take care of saving that and anything else |
| 76 | + * that needs to be notified of the CPU powering down. |
| 77 | + */ |
| 78 | + if (cpu_pm_enter()) |
| 79 | + return -1; |
| 80 | + |
| 81 | + ct_cpuidle_enter(); |
| 82 | + |
| 83 | + switch(index) { |
| 84 | + case STATE_PWRDOWN: |
| 85 | + apple_cpu_deep_wfi(); |
| 86 | + break; |
| 87 | + default: |
| 88 | + WARN_ON(1); |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + ct_cpuidle_exit(); |
| 93 | + |
| 94 | + cpu_pm_exit(); |
| 95 | + |
| 96 | + return index; |
| 97 | +} |
| 98 | + |
| 99 | +static struct cpuidle_driver apple_idle_driver = { |
| 100 | + .name = "apple_idle", |
| 101 | + .owner = THIS_MODULE, |
| 102 | + .states = { |
| 103 | + [STATE_WFI] = { |
| 104 | + .enter = apple_enter_wfi, |
| 105 | + .enter_s2idle = apple_enter_wfi, |
| 106 | + .exit_latency = 1, |
| 107 | + .target_residency = 1, |
| 108 | + .power_usage = UINT_MAX, |
| 109 | + .name = "WFI", |
| 110 | + .desc = "CPU clock-gated", |
| 111 | + .flags = 0, |
| 112 | + }, |
| 113 | + [STATE_PWRDOWN] = { |
| 114 | + .enter = apple_enter_idle, |
| 115 | + .enter_s2idle = apple_enter_idle, |
| 116 | + .exit_latency = 10, |
| 117 | + .target_residency = 10000, |
| 118 | + .power_usage = 0, |
| 119 | + .name = "CPU PD", |
| 120 | + .desc = "CPU/cluster powered down", |
| 121 | + .flags = CPUIDLE_FLAG_RCU_IDLE, |
| 122 | + }, |
| 123 | + }, |
| 124 | + .safe_state_index = STATE_WFI, |
| 125 | + .state_count = STATE_COUNT, |
| 126 | +}; |
| 127 | + |
| 128 | +static int apple_cpuidle_probe(struct platform_device *pdev) |
| 129 | +{ |
| 130 | + return cpuidle_register(&apple_idle_driver, NULL); |
| 131 | +} |
| 132 | + |
| 133 | +static struct platform_driver apple_cpuidle_driver = { |
| 134 | + .driver = { |
| 135 | + .name = "cpuidle-apple", |
| 136 | + }, |
| 137 | + .probe = apple_cpuidle_probe, |
| 138 | +}; |
| 139 | + |
| 140 | +static int __init apple_cpuidle_init(void) |
| 141 | +{ |
| 142 | + struct platform_device *pdev; |
| 143 | + int ret; |
| 144 | + |
| 145 | + ret = platform_driver_register(&apple_cpuidle_driver); |
| 146 | + if (ret) |
| 147 | + return ret; |
| 148 | + |
| 149 | + if (!of_machine_is_compatible("apple,arm-platform")) |
| 150 | + return 0; |
| 151 | + |
| 152 | + if (!FIELD_GET(DEEP_WFI_STATE_RETENTION, read_sysreg(aidr_el1))) { |
| 153 | + pr_info("cpuidle-apple: CPU does not retain state in deep WFI\n"); |
| 154 | + return 0; |
| 155 | + } |
| 156 | + |
| 157 | + pdev = platform_device_register_simple("cpuidle-apple", -1, NULL, 0); |
| 158 | + if (IS_ERR(pdev)) { |
| 159 | + platform_driver_unregister(&apple_cpuidle_driver); |
| 160 | + return PTR_ERR(pdev); |
| 161 | + } |
| 162 | + |
| 163 | + return 0; |
| 164 | +} |
| 165 | +device_initcall(apple_cpuidle_init); |
0 commit comments