/arch/arm/mach-msm/cpuidle.c
C | 197 lines | 140 code | 41 blank | 16 comment | 10 complexity | 416665a8218039c75fb706067bba100d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
1/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/cpuidle.h>
22
23#include "cpuidle.h"
24#include "pm.h"
25
26static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuidle_device, msm_cpuidle_devs);
27static struct cpuidle_driver msm_cpuidle_driver = {
28 .name = "msm_idle",
29 .owner = THIS_MODULE,
30};
31
32#ifdef CONFIG_MSM_SLEEP_STATS
33static void (*pre_idle_cb)(int cpu, unsigned int microsec);
34static void (*post_idle_cb)(int cpu, unsigned int microsec);
35
36static DEFINE_PER_CPU(struct timespec, ts_busy);
37static DEFINE_PER_CPU(struct atomic_notifier_head, msm_cpuidle_notifiers);
38
39static int pre_idle(int cpu)
40{
41 struct timespec *ts_notidle = &per_cpu(ts_busy, cpu);
42 struct timespec ts_now, ts_diff;
43
44 getnstimeofday(&ts_now);
45 ts_diff = timespec_sub(ts_now, *ts_notidle);
46
47 if (pre_idle_cb)
48 pre_idle_cb(cpu, (u32)timespec_to_ns(&ts_diff)/1000);
49
50 return 0;
51}
52
53static int post_idle(int cpu, unsigned int microsec)
54{
55 struct timespec *ts_notidle = &per_cpu(ts_busy, cpu);
56
57 getnstimeofday(ts_notidle);
58
59 if (post_idle_cb)
60 post_idle_cb(cpu, microsec);
61
62 return 0;
63}
64
65int msm_idle_register_cb(void (*pre)(int, unsigned int),
66 void (*post)(int, unsigned int))
67{
68 pre_idle_cb = pre;
69 post_idle_cb = post;
70 return 0;
71}
72EXPORT_SYMBOL(msm_idle_register_cb);
73
74int msm_cpuidle_register_notifier(unsigned int cpu, struct notifier_block *nb)
75{
76 struct atomic_notifier_head *head =
77 &per_cpu(msm_cpuidle_notifiers, cpu);
78
79 return atomic_notifier_chain_register(head, nb);
80}
81EXPORT_SYMBOL(msm_cpuidle_register_notifier);
82
83int msm_cpuidle_unregister_notifier(unsigned int cpu, struct notifier_block *nb)
84{
85 struct atomic_notifier_head *head =
86 &per_cpu(msm_cpuidle_notifiers, cpu);
87
88 return atomic_notifier_chain_unregister(head, nb);
89}
90EXPORT_SYMBOL(msm_cpuidle_unregister_notifier);
91#endif
92
93static int msm_cpuidle_enter(
94 struct cpuidle_device *dev, struct cpuidle_state *state)
95{
96 int ret;
97#ifdef CONFIG_MSM_SLEEP_STATS
98 struct atomic_notifier_head *head =
99 &__get_cpu_var(msm_cpuidle_notifiers);
100#endif
101
102 local_irq_disable();
103
104#ifdef CONFIG_MSM_SLEEP_STATS
105 pre_idle(dev->cpu);
106 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_ENTER, NULL);
107#endif
108
109 ret = msm_pm_idle_enter((enum msm_pm_sleep_mode) (state->driver_data));
110
111#ifdef CONFIG_MSM_SLEEP_STATS
112 post_idle(dev->cpu, ret);
113 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_EXIT, NULL);
114#endif
115
116 local_irq_enable();
117
118 return ret;
119}
120
121void __init msm_cpuidle_set_states(struct msm_cpuidle_state *states,
122 int nr_states, struct msm_pm_platform_data *pm_data)
123{
124 unsigned int cpu;
125
126 for_each_possible_cpu(cpu) {
127 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
128 int i;
129
130 dev->cpu = cpu;
131 dev->prepare = msm_pm_idle_prepare;
132
133 for (i = 0; i < nr_states; i++) {
134 struct msm_cpuidle_state *cstate = &states[i];
135 struct cpuidle_state *state;
136 struct msm_pm_platform_data *pm_mode;
137
138 if (cstate->cpu != cpu)
139 continue;
140
141 state = &dev->states[cstate->state_nr];
142 pm_mode = &pm_data[MSM_PM_MODE(cpu, cstate->mode_nr)];
143
144 snprintf(state->name, CPUIDLE_NAME_LEN, cstate->name);
145 snprintf(state->desc, CPUIDLE_DESC_LEN, cstate->desc);
146 state->driver_data = (void *) cstate->mode_nr;
147 state->flags = CPUIDLE_FLAG_TIME_VALID;
148 state->exit_latency = pm_mode->latency;
149 state->power_usage = 0;
150 state->target_residency = pm_mode->residency;
151 state->enter = msm_cpuidle_enter;
152 }
153
154 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
155 if (dev->states[i].enter == NULL)
156 break;
157 dev->state_count = i + 1;
158 }
159 }
160}
161
162int __init msm_cpuidle_init(void)
163{
164 unsigned int cpu;
165 int ret;
166
167 ret = cpuidle_register_driver(&msm_cpuidle_driver);
168 if (ret)
169 pr_err("%s: failed to register cpuidle driver: %d\n",
170 __func__, ret);
171
172 for_each_possible_cpu(cpu) {
173 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
174
175 ret = cpuidle_register_device(dev);
176 if (ret) {
177 pr_err("%s: failed to register cpuidle device for "
178 "cpu %u: %d\n", __func__, cpu, ret);
179 return ret;
180 }
181 }
182
183 return 0;
184}
185
186static int __init msm_cpuidle_early_init(void)
187{
188#ifdef CONFIG_MSM_SLEEP_STATS
189 unsigned int cpu;
190
191 for_each_possible_cpu(cpu)
192 ATOMIC_INIT_NOTIFIER_HEAD(&per_cpu(msm_cpuidle_notifiers, cpu));
193#endif
194 return 0;
195}
196
197early_initcall(msm_cpuidle_early_init);