blob: 9e5156d3962769e54f6742ea5f72a30f2bd565b7 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +00002/*
Daniel Lezcano69e6cb32015-02-02 16:32:46 +01003 * ARM/ARM64 generic CPU idle driver.
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +00004 *
5 * Copyright (C) 2014 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +00007 */
8
Daniel Lezcano69e6cb32015-02-02 16:32:46 +01009#define pr_fmt(fmt) "CPUidle arm: " fmt
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000010
11#include <linux/cpuidle.h>
12#include <linux/cpumask.h>
13#include <linux/cpu_pm.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +010017#include <linux/slab.h>
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000018
19#include <asm/cpuidle.h>
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000020
21#include "dt_idle_states.h"
22
23/*
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010024 * arm_enter_idle_state - Programs CPU to enter the specified state
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000025 *
26 * dev: cpuidle device
27 * drv: cpuidle driver
28 * idx: state index
29 *
30 * Called from the CPUidle framework to program the device to the
31 * specified target state selected by the governor.
32 */
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010033static int arm_enter_idle_state(struct cpuidle_device *dev,
34 struct cpuidle_driver *drv, int idx)
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000035{
Sudeep Holla220276e2016-07-19 18:52:56 +010036 /*
37 * Pass idle state index to arm_cpuidle_suspend which in turn
38 * will call the CPU ops suspend protocol with idle index as a
39 * parameter.
40 */
41 return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000042}
43
Daniel Lezcanod50a7d82017-06-12 17:55:10 +020044static struct cpuidle_driver arm_idle_driver __initdata = {
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010045 .name = "arm_idle",
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000046 .owner = THIS_MODULE,
47 /*
48 * State at index 0 is standby wfi and considered standard
49 * on all ARM platforms. If in some platforms simple wfi
50 * can't be used as "state 0", DT bindings must be implemented
51 * to work around this issue and allow installing a special
52 * handler for idle state index 0.
53 */
54 .states[0] = {
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010055 .enter = arm_enter_idle_state,
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000056 .exit_latency = 1,
57 .target_residency = 1,
58 .power_usage = UINT_MAX,
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000059 .name = "WFI",
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010060 .desc = "ARM WFI",
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000061 }
62};
63
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010064static const struct of_device_id arm_idle_state_match[] __initconst = {
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000065 { .compatible = "arm,idle-state",
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010066 .data = arm_enter_idle_state },
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000067 { },
68};
69
70/*
Leo Yan7943bfa2017-10-10 13:47:56 +080071 * arm_idle_init_cpu
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000072 *
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010073 * Registers the arm specific cpuidle driver with the cpuidle
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000074 * framework. It relies on core code to parse the idle states
75 * and initialize them using driver data structures accordingly.
76 */
Leo Yan7943bfa2017-10-10 13:47:56 +080077static int __init arm_idle_init_cpu(int cpu)
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000078{
Leo Yan7943bfa2017-10-10 13:47:56 +080079 int ret;
Daniel Lezcanod50a7d82017-06-12 17:55:10 +020080 struct cpuidle_driver *drv;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000081
Leo Yan7943bfa2017-10-10 13:47:56 +080082 drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL);
83 if (!drv)
84 return -ENOMEM;
Daniel Lezcanod50a7d82017-06-12 17:55:10 +020085
Leo Yan7943bfa2017-10-10 13:47:56 +080086 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
Daniel Lezcanod50a7d82017-06-12 17:55:10 +020087
Leo Yan7943bfa2017-10-10 13:47:56 +080088 /*
89 * Initialize idle states data, starting at index 1. This
90 * driver is DT only, if no DT idle states are detected (ret
91 * == 0) let the driver initialization fail accordingly since
92 * there is no reason to initialize the idle driver if only
93 * wfi is supported.
94 */
95 ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
96 if (ret <= 0) {
97 ret = ret ? : -ENODEV;
98 goto out_kfree_drv;
99 }
Daniel Lezcanod50a7d82017-06-12 17:55:10 +0200100
Leo Yan7943bfa2017-10-10 13:47:56 +0800101 /*
102 * Call arch CPU operations in order to initialize
103 * idle states suspend back-end specific data
104 */
105 ret = arm_cpuidle_init(cpu);
Daniel Lezcanod50a7d82017-06-12 17:55:10 +0200106
Leo Yan7943bfa2017-10-10 13:47:56 +0800107 /*
Lorenzo Pieralisi6460d7b2019-08-09 12:03:08 +0100108 * Allow the initialization to continue for other CPUs, if the
109 * reported failure is a HW misconfiguration/breakage (-ENXIO).
110 *
111 * Some platforms do not support idle operations
112 * (arm_cpuidle_init() returning -EOPNOTSUPP), we should
113 * not flag this case as an error, it is a valid
114 * configuration.
Leo Yan7943bfa2017-10-10 13:47:56 +0800115 */
Leo Yan7943bfa2017-10-10 13:47:56 +0800116 if (ret) {
Lorenzo Pieralisi6460d7b2019-08-09 12:03:08 +0100117 if (ret != -EOPNOTSUPP)
118 pr_err("CPU %d failed to init idle CPU ops\n", cpu);
Ulf Hansson763f1912018-11-01 13:22:38 +0100119 ret = ret == -ENXIO ? 0 : ret;
120 goto out_kfree_drv;
121 }
122
Ulf Hansson3e452e62018-11-01 11:15:58 +0100123 ret = cpuidle_register(drv, NULL);
124 if (ret)
Ulf Hansson763f1912018-11-01 13:22:38 +0100125 goto out_kfree_drv;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +0000126
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100127 return 0;
Leo Yan0f878552017-10-10 13:47:55 +0800128
Leo Yan0f878552017-10-10 13:47:55 +0800129out_kfree_drv:
Stefan Wahrened40fad2017-08-31 22:24:36 +0200130 kfree(drv);
Leo Yan7943bfa2017-10-10 13:47:56 +0800131 return ret;
132}
133
134/*
135 * arm_idle_init - Initializes arm cpuidle driver
136 *
137 * Initializes arm cpuidle driver for all CPUs, if any CPU fails
138 * to register cpuidle driver then rollback to cancel all CPUs
139 * registeration.
140 */
141static int __init arm_idle_init(void)
142{
143 int cpu, ret;
144 struct cpuidle_driver *drv;
145 struct cpuidle_device *dev;
146
147 for_each_possible_cpu(cpu) {
148 ret = arm_idle_init_cpu(cpu);
149 if (ret)
150 goto out_fail;
151 }
152
153 return 0;
154
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100155out_fail:
156 while (--cpu >= 0) {
157 dev = per_cpu(cpuidle_devices, cpu);
Leo Yan0f878552017-10-10 13:47:55 +0800158 drv = cpuidle_get_cpu_driver(dev);
Ulf Hansson3e452e62018-11-01 11:15:58 +0100159 cpuidle_unregister(drv);
Daniel Lezcanod50a7d82017-06-12 17:55:10 +0200160 kfree(drv);
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100161 }
162
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100163 return ret;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +0000164}
Daniel Lezcano69e6cb32015-02-02 16:32:46 +0100165device_initcall(arm_idle_init);