1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ARM/ARM64 generic CPU idle driver.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014 ARM Ltd.
6*4882a593Smuzhiyun * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #define pr_fmt(fmt) "CPUidle arm: " fmt
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/cpu_cooling.h>
12*4882a593Smuzhiyun #include <linux/cpuidle.h>
13*4882a593Smuzhiyun #include <linux/cpumask.h>
14*4882a593Smuzhiyun #include <linux/cpu_pm.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <asm/cpuidle.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "dt_idle_states.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * arm_enter_idle_state - Programs CPU to enter the specified state
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * dev: cpuidle device
28*4882a593Smuzhiyun * drv: cpuidle driver
29*4882a593Smuzhiyun * idx: state index
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Called from the CPUidle framework to program the device to the
32*4882a593Smuzhiyun * specified target state selected by the governor.
33*4882a593Smuzhiyun */
arm_enter_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)34*4882a593Smuzhiyun static int arm_enter_idle_state(struct cpuidle_device *dev,
35*4882a593Smuzhiyun struct cpuidle_driver *drv, int idx)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Pass idle state index to arm_cpuidle_suspend which in turn
39*4882a593Smuzhiyun * will call the CPU ops suspend protocol with idle index as a
40*4882a593Smuzhiyun * parameter.
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static struct cpuidle_driver arm_idle_driver __initdata = {
46*4882a593Smuzhiyun .name = "arm_idle",
47*4882a593Smuzhiyun .owner = THIS_MODULE,
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * State at index 0 is standby wfi and considered standard
50*4882a593Smuzhiyun * on all ARM platforms. If in some platforms simple wfi
51*4882a593Smuzhiyun * can't be used as "state 0", DT bindings must be implemented
52*4882a593Smuzhiyun * to work around this issue and allow installing a special
53*4882a593Smuzhiyun * handler for idle state index 0.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun .states[0] = {
56*4882a593Smuzhiyun .enter = arm_enter_idle_state,
57*4882a593Smuzhiyun .exit_latency = 1,
58*4882a593Smuzhiyun .target_residency = 1,
59*4882a593Smuzhiyun .power_usage = UINT_MAX,
60*4882a593Smuzhiyun .name = "WFI",
61*4882a593Smuzhiyun .desc = "ARM WFI",
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun static const struct of_device_id arm_idle_state_match[] __initconst = {
66*4882a593Smuzhiyun { .compatible = "arm,idle-state",
67*4882a593Smuzhiyun .data = arm_enter_idle_state },
68*4882a593Smuzhiyun { },
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * arm_idle_init_cpu
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Registers the arm specific cpuidle driver with the cpuidle
75*4882a593Smuzhiyun * framework. It relies on core code to parse the idle states
76*4882a593Smuzhiyun * and initialize them using driver data structures accordingly.
77*4882a593Smuzhiyun */
arm_idle_init_cpu(int cpu)78*4882a593Smuzhiyun static int __init arm_idle_init_cpu(int cpu)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun int ret;
81*4882a593Smuzhiyun struct cpuidle_driver *drv;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL);
84*4882a593Smuzhiyun if (!drv)
85*4882a593Smuzhiyun return -ENOMEM;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun drv->cpumask = (struct cpumask *)cpumask_of(cpu);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun * Initialize idle states data, starting at index 1. This
91*4882a593Smuzhiyun * driver is DT only, if no DT idle states are detected (ret
92*4882a593Smuzhiyun * == 0) let the driver initialization fail accordingly since
93*4882a593Smuzhiyun * there is no reason to initialize the idle driver if only
94*4882a593Smuzhiyun * wfi is supported.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
97*4882a593Smuzhiyun if (ret <= 0) {
98*4882a593Smuzhiyun ret = ret ? : -ENODEV;
99*4882a593Smuzhiyun goto out_kfree_drv;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Call arch CPU operations in order to initialize
104*4882a593Smuzhiyun * idle states suspend back-end specific data
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun ret = arm_cpuidle_init(cpu);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * Allow the initialization to continue for other CPUs, if the
110*4882a593Smuzhiyun * reported failure is a HW misconfiguration/breakage (-ENXIO).
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * Some platforms do not support idle operations
113*4882a593Smuzhiyun * (arm_cpuidle_init() returning -EOPNOTSUPP), we should
114*4882a593Smuzhiyun * not flag this case as an error, it is a valid
115*4882a593Smuzhiyun * configuration.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun if (ret) {
118*4882a593Smuzhiyun if (ret != -EOPNOTSUPP)
119*4882a593Smuzhiyun pr_err("CPU %d failed to init idle CPU ops\n", cpu);
120*4882a593Smuzhiyun ret = ret == -ENXIO ? 0 : ret;
121*4882a593Smuzhiyun goto out_kfree_drv;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun ret = cpuidle_register(drv, NULL);
125*4882a593Smuzhiyun if (ret)
126*4882a593Smuzhiyun goto out_kfree_drv;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun cpuidle_cooling_register(drv);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return 0;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun out_kfree_drv:
133*4882a593Smuzhiyun kfree(drv);
134*4882a593Smuzhiyun return ret;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * arm_idle_init - Initializes arm cpuidle driver
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * Initializes arm cpuidle driver for all CPUs, if any CPU fails
141*4882a593Smuzhiyun * to register cpuidle driver then rollback to cancel all CPUs
142*4882a593Smuzhiyun * registeration.
143*4882a593Smuzhiyun */
arm_idle_init(void)144*4882a593Smuzhiyun static int __init arm_idle_init(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun int cpu, ret;
147*4882a593Smuzhiyun struct cpuidle_driver *drv;
148*4882a593Smuzhiyun struct cpuidle_device *dev;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun for_each_possible_cpu(cpu) {
151*4882a593Smuzhiyun ret = arm_idle_init_cpu(cpu);
152*4882a593Smuzhiyun if (ret)
153*4882a593Smuzhiyun goto out_fail;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return 0;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun out_fail:
159*4882a593Smuzhiyun while (--cpu >= 0) {
160*4882a593Smuzhiyun dev = per_cpu(cpuidle_devices, cpu);
161*4882a593Smuzhiyun drv = cpuidle_get_cpu_driver(dev);
162*4882a593Smuzhiyun cpuidle_unregister(drv);
163*4882a593Smuzhiyun kfree(drv);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun return ret;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun device_initcall(arm_idle_init);
169