Skip to content

Commit 4c135ef

Browse files
marcanjannau
authored andcommitted
cpuidle: apple: Add Apple SoC cpuidle driver
May the PSCI conversation happen some day. Until it does, this will make the user experience a lot less painful in downstream kernels. Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent 3d04555 commit 4c135ef

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

drivers/cpuidle/Kconfig.arm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,11 @@ config ARM_QCOM_SPM_CPUIDLE
130130
The Subsystem Power Manager (SPM) controls low power modes for the
131131
CPU and L2 cores. It interface with various system drivers to put
132132
the cores in low power modes.
133+
134+
config ARM_APPLE_CPUIDLE
135+
bool "Apple SoC CPU idle driver"
136+
depends on ARM64
137+
default ARCH_APPLE
138+
select CPU_IDLE_MULTIPLE_DRIVERS
139+
help
140+
Select this to enable cpuidle on Apple SoCs.

drivers/cpuidle/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ obj-$(CONFIG_ARM_PSCI_CPUIDLE) += cpuidle-psci.o
2929
obj-$(CONFIG_ARM_PSCI_CPUIDLE_DOMAIN) += cpuidle-psci-domain.o
3030
obj-$(CONFIG_ARM_TEGRA_CPUIDLE) += cpuidle-tegra.o
3131
obj-$(CONFIG_ARM_QCOM_SPM_CPUIDLE) += cpuidle-qcom-spm.o
32+
obj-$(CONFIG_ARM_APPLE_CPUIDLE) += cpuidle-apple.o
3233

3334
###############################################################################
3435
# MIPS drivers

drivers/cpuidle/cpuidle-apple.c

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

0 commit comments

Comments
 (0)