blob: f47c545467522e48eb118b0930416081af617acc [file] [log] [blame]
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +00001/*
Daniel Lezcano69e6cb32015-02-02 16:32:46 +01002 * ARM/ARM64 generic CPU idle driver.
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +00003 *
4 * Copyright (C) 2014 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010012#define pr_fmt(fmt) "CPUidle arm: " fmt
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000013
14#include <linux/cpuidle.h>
15#include <linux/cpumask.h>
16#include <linux/cpu_pm.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +010020#include <linux/slab.h>
Daniel Lezcano089f0462017-06-12 17:55:10 +020021#include <linux/topology.h>
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000022
23#include <asm/cpuidle.h>
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000024
25#include "dt_idle_states.h"
26
27/*
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010028 * arm_enter_idle_state - Programs CPU to enter the specified state
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000029 *
30 * dev: cpuidle device
31 * drv: cpuidle driver
32 * idx: state index
33 *
34 * Called from the CPUidle framework to program the device to the
35 * specified target state selected by the governor.
36 */
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010037static int arm_enter_idle_state(struct cpuidle_device *dev,
38 struct cpuidle_driver *drv, int idx)
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000039{
Sudeep Holla220276e2016-07-19 18:52:56 +010040 /*
41 * Pass idle state index to arm_cpuidle_suspend which in turn
42 * will call the CPU ops suspend protocol with idle index as a
43 * parameter.
44 */
45 return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000046}
47
Daniel Lezcano089f0462017-06-12 17:55:10 +020048static struct cpuidle_driver arm_idle_driver __initdata = {
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010049 .name = "arm_idle",
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000050 .owner = THIS_MODULE,
51 /*
52 * State at index 0 is standby wfi and considered standard
53 * on all ARM platforms. If in some platforms simple wfi
54 * can't be used as "state 0", DT bindings must be implemented
55 * to work around this issue and allow installing a special
56 * handler for idle state index 0.
57 */
58 .states[0] = {
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010059 .enter = arm_enter_idle_state,
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000060 .exit_latency = 1,
61 .target_residency = 1,
62 .power_usage = UINT_MAX,
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000063 .name = "WFI",
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010064 .desc = "ARM WFI",
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000065 }
66};
67
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010068static const struct of_device_id arm_idle_state_match[] __initconst = {
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000069 { .compatible = "arm,idle-state",
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010070 .data = arm_enter_idle_state },
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000071 { },
72};
73
74/*
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010075 * arm_idle_init
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000076 *
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010077 * Registers the arm specific cpuidle driver with the cpuidle
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000078 * framework. It relies on core code to parse the idle states
79 * and initialize them using driver data structures accordingly.
80 */
Daniel Lezcano69e6cb32015-02-02 16:32:46 +010081static int __init arm_idle_init(void)
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000082{
83 int cpu, ret;
Daniel Lezcano089f0462017-06-12 17:55:10 +020084 struct cpuidle_driver *drv;
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +010085 struct cpuidle_device *dev;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000086
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +000087 for_each_possible_cpu(cpu) {
Daniel Lezcano089f0462017-06-12 17:55:10 +020088
89 drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL);
90 if (!drv) {
91 ret = -ENOMEM;
92 goto out_fail;
93 }
94
95 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
96
97 /*
98 * Initialize idle states data, starting at index 1. This
99 * driver is DT only, if no DT idle states are detected (ret
100 * == 0) let the driver initialization fail accordingly since
101 * there is no reason to initialize the idle driver if only
102 * wfi is supported.
103 */
104 ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
105 if (ret <= 0) {
106 ret = ret ? : -ENODEV;
Leo Yan19bd3e32017-10-10 13:47:55 +0800107 goto out_kfree_drv;
Daniel Lezcano089f0462017-06-12 17:55:10 +0200108 }
109
110 ret = cpuidle_register_driver(drv);
111 if (ret) {
112 pr_err("Failed to register cpuidle driver\n");
Leo Yan19bd3e32017-10-10 13:47:55 +0800113 goto out_kfree_drv;
Daniel Lezcano089f0462017-06-12 17:55:10 +0200114 }
115
116 /*
117 * Call arch CPU operations in order to initialize
118 * idle states suspend back-end specific data
119 */
Daniel Lezcanoc9d62162015-02-02 16:32:46 +0100120 ret = arm_cpuidle_init(cpu);
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100121
122 /*
123 * Skip the cpuidle device initialization if the reported
124 * failure is a HW misconfiguration/breakage (-ENXIO).
125 */
126 if (ret == -ENXIO)
127 continue;
128
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +0000129 if (ret) {
130 pr_err("CPU %d failed to init idle CPU ops\n", cpu);
Leo Yan19bd3e32017-10-10 13:47:55 +0800131 goto out_unregister_drv;
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100132 }
133
134 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
135 if (!dev) {
136 pr_err("Failed to allocate cpuidle device\n");
Christophe Jailletaf48d7b2016-08-11 15:02:30 +0200137 ret = -ENOMEM;
Leo Yan19bd3e32017-10-10 13:47:55 +0800138 goto out_unregister_drv;
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100139 }
140 dev->cpu = cpu;
141
142 ret = cpuidle_register_device(dev);
143 if (ret) {
144 pr_err("Failed to register cpuidle device for CPU %d\n",
145 cpu);
Leo Yan19bd3e32017-10-10 13:47:55 +0800146 goto out_kfree_dev;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +0000147 }
148 }
149
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100150 return 0;
Leo Yan19bd3e32017-10-10 13:47:55 +0800151
152out_kfree_dev:
153 kfree(dev);
154out_unregister_drv:
155 cpuidle_unregister_driver(drv);
156out_kfree_drv:
Stefan Wahren9817c0e2017-08-31 22:24:36 +0200157 kfree(drv);
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100158out_fail:
159 while (--cpu >= 0) {
160 dev = per_cpu(cpuidle_devices, cpu);
Leo Yan19bd3e32017-10-10 13:47:55 +0800161 drv = cpuidle_get_cpu_driver(dev);
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100162 cpuidle_unregister_device(dev);
Daniel Lezcano089f0462017-06-12 17:55:10 +0200163 cpuidle_unregister_driver(drv);
Leo Yan19bd3e32017-10-10 13:47:55 +0800164 kfree(dev);
Daniel Lezcano089f0462017-06-12 17:55:10 +0200165 kfree(drv);
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100166 }
167
Daniel Lezcanoa0d46a32015-03-05 16:44:42 +0100168 return ret;
Lorenzo Pieralisi3299b632014-02-28 13:03:44 +0000169}
Daniel Lezcano69e6cb32015-02-02 16:32:46 +0100170device_initcall(arm_idle_init);