xref: /linux/drivers/cpuidle/cpuidle-arm.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23299b63dSLorenzo Pieralisi /*
369e6cb3dSDaniel Lezcano  * ARM/ARM64 generic CPU idle driver.
43299b63dSLorenzo Pieralisi  *
53299b63dSLorenzo Pieralisi  * Copyright (C) 2014 ARM Ltd.
63299b63dSLorenzo Pieralisi  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
73299b63dSLorenzo Pieralisi  */
83299b63dSLorenzo Pieralisi 
969e6cb3dSDaniel Lezcano #define pr_fmt(fmt) "CPUidle arm: " fmt
103299b63dSLorenzo Pieralisi 
11*fc7a3d9eSDaniel Lezcano #include <linux/cpu_cooling.h>
123299b63dSLorenzo Pieralisi #include <linux/cpuidle.h>
133299b63dSLorenzo Pieralisi #include <linux/cpumask.h>
143299b63dSLorenzo Pieralisi #include <linux/cpu_pm.h>
153299b63dSLorenzo Pieralisi #include <linux/kernel.h>
163299b63dSLorenzo Pieralisi #include <linux/module.h>
173299b63dSLorenzo Pieralisi #include <linux/of.h>
18a0d46a3dSDaniel Lezcano #include <linux/slab.h>
193299b63dSLorenzo Pieralisi 
203299b63dSLorenzo Pieralisi #include <asm/cpuidle.h>
213299b63dSLorenzo Pieralisi 
223299b63dSLorenzo Pieralisi #include "dt_idle_states.h"
233299b63dSLorenzo Pieralisi 
243299b63dSLorenzo Pieralisi /*
2569e6cb3dSDaniel Lezcano  * arm_enter_idle_state - Programs CPU to enter the specified state
263299b63dSLorenzo Pieralisi  *
273299b63dSLorenzo Pieralisi  * dev: cpuidle device
283299b63dSLorenzo Pieralisi  * drv: cpuidle driver
293299b63dSLorenzo Pieralisi  * idx: state index
303299b63dSLorenzo Pieralisi  *
313299b63dSLorenzo Pieralisi  * Called from the CPUidle framework to program the device to the
323299b63dSLorenzo Pieralisi  * specified target state selected by the governor.
333299b63dSLorenzo Pieralisi  */
3469e6cb3dSDaniel Lezcano static int arm_enter_idle_state(struct cpuidle_device *dev,
353299b63dSLorenzo Pieralisi 				struct cpuidle_driver *drv, int idx)
363299b63dSLorenzo Pieralisi {
373299b63dSLorenzo Pieralisi 	/*
38220276e0SSudeep Holla 	 * Pass idle state index to arm_cpuidle_suspend which in turn
39220276e0SSudeep Holla 	 * will call the CPU ops suspend protocol with idle index as a
403299b63dSLorenzo Pieralisi 	 * parameter.
413299b63dSLorenzo Pieralisi 	 */
42220276e0SSudeep Holla 	return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
433299b63dSLorenzo Pieralisi }
443299b63dSLorenzo Pieralisi 
45d50a7d8aSDaniel Lezcano static struct cpuidle_driver arm_idle_driver __initdata = {
4669e6cb3dSDaniel Lezcano 	.name = "arm_idle",
473299b63dSLorenzo Pieralisi 	.owner = THIS_MODULE,
483299b63dSLorenzo Pieralisi 	/*
493299b63dSLorenzo Pieralisi 	 * State at index 0 is standby wfi and considered standard
503299b63dSLorenzo Pieralisi 	 * on all ARM platforms. If in some platforms simple wfi
513299b63dSLorenzo Pieralisi 	 * can't be used as "state 0", DT bindings must be implemented
523299b63dSLorenzo Pieralisi 	 * to work around this issue and allow installing a special
533299b63dSLorenzo Pieralisi 	 * handler for idle state index 0.
543299b63dSLorenzo Pieralisi 	 */
553299b63dSLorenzo Pieralisi 	.states[0] = {
5669e6cb3dSDaniel Lezcano 		.enter                  = arm_enter_idle_state,
573299b63dSLorenzo Pieralisi 		.exit_latency           = 1,
583299b63dSLorenzo Pieralisi 		.target_residency       = 1,
593299b63dSLorenzo Pieralisi 		.power_usage		= UINT_MAX,
603299b63dSLorenzo Pieralisi 		.name                   = "WFI",
6169e6cb3dSDaniel Lezcano 		.desc                   = "ARM WFI",
623299b63dSLorenzo Pieralisi 	}
633299b63dSLorenzo Pieralisi };
643299b63dSLorenzo Pieralisi 
6569e6cb3dSDaniel Lezcano static const struct of_device_id arm_idle_state_match[] __initconst = {
663299b63dSLorenzo Pieralisi 	{ .compatible = "arm,idle-state",
6769e6cb3dSDaniel Lezcano 	  .data = arm_enter_idle_state },
683299b63dSLorenzo Pieralisi 	{ },
693299b63dSLorenzo Pieralisi };
703299b63dSLorenzo Pieralisi 
713299b63dSLorenzo Pieralisi /*
727943bfaeSLeo Yan  * arm_idle_init_cpu
733299b63dSLorenzo Pieralisi  *
7469e6cb3dSDaniel Lezcano  * Registers the arm specific cpuidle driver with the cpuidle
753299b63dSLorenzo Pieralisi  * framework. It relies on core code to parse the idle states
763299b63dSLorenzo Pieralisi  * and initialize them using driver data structures accordingly.
773299b63dSLorenzo Pieralisi  */
787943bfaeSLeo Yan static int __init arm_idle_init_cpu(int cpu)
793299b63dSLorenzo Pieralisi {
807943bfaeSLeo Yan 	int ret;
81d50a7d8aSDaniel Lezcano 	struct cpuidle_driver *drv;
823299b63dSLorenzo Pieralisi 
83d50a7d8aSDaniel Lezcano 	drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL);
847943bfaeSLeo Yan 	if (!drv)
857943bfaeSLeo Yan 		return -ENOMEM;
86d50a7d8aSDaniel Lezcano 
87d50a7d8aSDaniel Lezcano 	drv->cpumask = (struct cpumask *)cpumask_of(cpu);
88d50a7d8aSDaniel Lezcano 
893299b63dSLorenzo Pieralisi 	/*
90d50a7d8aSDaniel Lezcano 	 * Initialize idle states data, starting at index 1.  This
91d50a7d8aSDaniel Lezcano 	 * driver is DT only, if no DT idle states are detected (ret
92d50a7d8aSDaniel Lezcano 	 * == 0) let the driver initialization fail accordingly since
93d50a7d8aSDaniel Lezcano 	 * there is no reason to initialize the idle driver if only
94d50a7d8aSDaniel Lezcano 	 * wfi is supported.
953299b63dSLorenzo Pieralisi 	 */
9669e6cb3dSDaniel Lezcano 	ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
97d50a7d8aSDaniel Lezcano 	if (ret <= 0) {
98d50a7d8aSDaniel Lezcano 		ret = ret ? : -ENODEV;
990f87855dSLeo Yan 		goto out_kfree_drv;
100d50a7d8aSDaniel Lezcano 	}
1013299b63dSLorenzo Pieralisi 
1023299b63dSLorenzo Pieralisi 	/*
1033299b63dSLorenzo Pieralisi 	 * Call arch CPU operations in order to initialize
1043299b63dSLorenzo Pieralisi 	 * idle states suspend back-end specific data
1053299b63dSLorenzo Pieralisi 	 */
106c9d62161SDaniel Lezcano 	ret = arm_cpuidle_init(cpu);
107a0d46a3dSDaniel Lezcano 
108a0d46a3dSDaniel Lezcano 	/*
1096460d7baSLorenzo Pieralisi 	 * Allow the initialization to continue for other CPUs, if the
1106460d7baSLorenzo Pieralisi 	 * reported failure is a HW misconfiguration/breakage (-ENXIO).
1116460d7baSLorenzo Pieralisi 	 *
1126460d7baSLorenzo Pieralisi 	 * Some platforms do not support idle operations
1136460d7baSLorenzo Pieralisi 	 * (arm_cpuidle_init() returning -EOPNOTSUPP), we should
1146460d7baSLorenzo Pieralisi 	 * not flag this case as an error, it is a valid
1156460d7baSLorenzo Pieralisi 	 * configuration.
116a0d46a3dSDaniel Lezcano 	 */
1173299b63dSLorenzo Pieralisi 	if (ret) {
1186460d7baSLorenzo Pieralisi 		if (ret != -EOPNOTSUPP)
1193299b63dSLorenzo Pieralisi 			pr_err("CPU %d failed to init idle CPU ops\n", cpu);
120763f191aSUlf Hansson 		ret = ret == -ENXIO ? 0 : ret;
121763f191aSUlf Hansson 		goto out_kfree_drv;
122763f191aSUlf Hansson 	}
123763f191aSUlf Hansson 
1243e452e63SUlf Hansson 	ret = cpuidle_register(drv, NULL);
1253e452e63SUlf Hansson 	if (ret)
126763f191aSUlf Hansson 		goto out_kfree_drv;
1273299b63dSLorenzo Pieralisi 
128*fc7a3d9eSDaniel Lezcano 	cpuidle_cooling_register(drv);
129*fc7a3d9eSDaniel Lezcano 
130a0d46a3dSDaniel Lezcano 	return 0;
1310f87855dSLeo Yan 
1320f87855dSLeo Yan out_kfree_drv:
133ed40fad9SStefan Wahren 	kfree(drv);
1347943bfaeSLeo Yan 	return ret;
1357943bfaeSLeo Yan }
1367943bfaeSLeo Yan 
1377943bfaeSLeo Yan /*
1387943bfaeSLeo Yan  * arm_idle_init - Initializes arm cpuidle driver
1397943bfaeSLeo Yan  *
1407943bfaeSLeo Yan  * Initializes arm cpuidle driver for all CPUs, if any CPU fails
1417943bfaeSLeo Yan  * to register cpuidle driver then rollback to cancel all CPUs
1427943bfaeSLeo Yan  * registeration.
1437943bfaeSLeo Yan  */
1447943bfaeSLeo Yan static int __init arm_idle_init(void)
1457943bfaeSLeo Yan {
1467943bfaeSLeo Yan 	int cpu, ret;
1477943bfaeSLeo Yan 	struct cpuidle_driver *drv;
1487943bfaeSLeo Yan 	struct cpuidle_device *dev;
1497943bfaeSLeo Yan 
1507943bfaeSLeo Yan 	for_each_possible_cpu(cpu) {
1517943bfaeSLeo Yan 		ret = arm_idle_init_cpu(cpu);
1527943bfaeSLeo Yan 		if (ret)
1537943bfaeSLeo Yan 			goto out_fail;
1547943bfaeSLeo Yan 	}
1557943bfaeSLeo Yan 
1567943bfaeSLeo Yan 	return 0;
1577943bfaeSLeo Yan 
158a0d46a3dSDaniel Lezcano out_fail:
159a0d46a3dSDaniel Lezcano 	while (--cpu >= 0) {
160a0d46a3dSDaniel Lezcano 		dev = per_cpu(cpuidle_devices, cpu);
1610f87855dSLeo Yan 		drv = cpuidle_get_cpu_driver(dev);
1623e452e63SUlf Hansson 		cpuidle_unregister(drv);
163d50a7d8aSDaniel Lezcano 		kfree(drv);
164d50a7d8aSDaniel Lezcano 	}
165a0d46a3dSDaniel Lezcano 
166a0d46a3dSDaniel Lezcano 	return ret;
1673299b63dSLorenzo Pieralisi }
16869e6cb3dSDaniel Lezcano device_initcall(arm_idle_init);
169