powernv/cpuidle: Redesign idle states management
[deliverable/linux.git] / drivers / cpuidle / cpuidle-powernv.c
1 /*
2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
4 *
5 */
6
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/moduleparam.h>
11 #include <linux/cpuidle.h>
12 #include <linux/cpu.h>
13 #include <linux/notifier.h>
14 #include <linux/clockchips.h>
15 #include <linux/of.h>
16
17 #include <asm/machdep.h>
18 #include <asm/firmware.h>
19 #include <asm/opal.h>
20 #include <asm/runlatch.h>
21
22 #define MAX_POWERNV_IDLE_STATES 8
23
24 struct cpuidle_driver powernv_idle_driver = {
25 .name = "powernv_idle",
26 .owner = THIS_MODULE,
27 };
28
29 static int max_idle_state;
30 static struct cpuidle_state *cpuidle_state_table;
31
32 static int snooze_loop(struct cpuidle_device *dev,
33 struct cpuidle_driver *drv,
34 int index)
35 {
36 local_irq_enable();
37 set_thread_flag(TIF_POLLING_NRFLAG);
38
39 ppc64_runlatch_off();
40 while (!need_resched()) {
41 HMT_low();
42 HMT_very_low();
43 }
44
45 HMT_medium();
46 ppc64_runlatch_on();
47 clear_thread_flag(TIF_POLLING_NRFLAG);
48 smp_mb();
49 return index;
50 }
51
52 static int nap_loop(struct cpuidle_device *dev,
53 struct cpuidle_driver *drv,
54 int index)
55 {
56 ppc64_runlatch_off();
57 power7_idle();
58 ppc64_runlatch_on();
59 return index;
60 }
61
62 static int fastsleep_loop(struct cpuidle_device *dev,
63 struct cpuidle_driver *drv,
64 int index)
65 {
66 unsigned long old_lpcr = mfspr(SPRN_LPCR);
67 unsigned long new_lpcr;
68
69 if (unlikely(system_state < SYSTEM_RUNNING))
70 return index;
71
72 new_lpcr = old_lpcr;
73 /* Do not exit powersave upon decrementer as we've setup the timer
74 * offload.
75 */
76 new_lpcr &= ~LPCR_PECE1;
77
78 mtspr(SPRN_LPCR, new_lpcr);
79 power7_sleep();
80
81 mtspr(SPRN_LPCR, old_lpcr);
82
83 return index;
84 }
85
86 /*
87 * States for dedicated partition case.
88 */
89 static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
90 { /* Snooze */
91 .name = "snooze",
92 .desc = "snooze",
93 .flags = CPUIDLE_FLAG_TIME_VALID,
94 .exit_latency = 0,
95 .target_residency = 0,
96 .enter = &snooze_loop },
97 };
98
99 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
100 unsigned long action, void *hcpu)
101 {
102 int hotcpu = (unsigned long)hcpu;
103 struct cpuidle_device *dev =
104 per_cpu(cpuidle_devices, hotcpu);
105
106 if (dev && cpuidle_get_driver()) {
107 switch (action) {
108 case CPU_ONLINE:
109 case CPU_ONLINE_FROZEN:
110 cpuidle_pause_and_lock();
111 cpuidle_enable_device(dev);
112 cpuidle_resume_and_unlock();
113 break;
114
115 case CPU_DEAD:
116 case CPU_DEAD_FROZEN:
117 cpuidle_pause_and_lock();
118 cpuidle_disable_device(dev);
119 cpuidle_resume_and_unlock();
120 break;
121
122 default:
123 return NOTIFY_DONE;
124 }
125 }
126 return NOTIFY_OK;
127 }
128
129 static struct notifier_block setup_hotplug_notifier = {
130 .notifier_call = powernv_cpuidle_add_cpu_notifier,
131 };
132
133 /*
134 * powernv_cpuidle_driver_init()
135 */
136 static int powernv_cpuidle_driver_init(void)
137 {
138 int idle_state;
139 struct cpuidle_driver *drv = &powernv_idle_driver;
140
141 drv->state_count = 0;
142
143 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
144 /* Is the state not enabled? */
145 if (cpuidle_state_table[idle_state].enter == NULL)
146 continue;
147
148 drv->states[drv->state_count] = /* structure copy */
149 cpuidle_state_table[idle_state];
150
151 drv->state_count += 1;
152 }
153
154 return 0;
155 }
156
157 static int powernv_add_idle_states(void)
158 {
159 struct device_node *power_mgt;
160 int nr_idle_states = 1; /* Snooze */
161 int dt_idle_states;
162 const __be32 *idle_state_flags;
163 const __be32 *idle_state_latency;
164 u32 len_flags, flags, latency_ns;
165 int i;
166
167 /* Currently we have snooze statically defined */
168
169 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
170 if (!power_mgt) {
171 pr_warn("opal: PowerMgmt Node not found\n");
172 return nr_idle_states;
173 }
174
175 idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags);
176 if (!idle_state_flags) {
177 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
178 return nr_idle_states;
179 }
180
181 idle_state_latency = of_get_property(power_mgt,
182 "ibm,cpu-idle-state-latencies-ns", NULL);
183 if (!idle_state_latency) {
184 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
185 return nr_idle_states;
186 }
187
188 dt_idle_states = len_flags / sizeof(u32);
189
190 for (i = 0; i < dt_idle_states; i++) {
191
192 flags = be32_to_cpu(idle_state_flags[i]);
193
194 /* Cpuidle accepts exit_latency in us and we estimate
195 * target residency to be 10x exit_latency
196 */
197 latency_ns = be32_to_cpu(idle_state_latency[i]);
198 if (flags & OPAL_PM_NAP_ENABLED) {
199 /* Add NAP state */
200 strcpy(powernv_states[nr_idle_states].name, "Nap");
201 strcpy(powernv_states[nr_idle_states].desc, "Nap");
202 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
203 powernv_states[nr_idle_states].exit_latency =
204 ((unsigned int)latency_ns) / 1000;
205 powernv_states[nr_idle_states].target_residency =
206 ((unsigned int)latency_ns / 100);
207 powernv_states[nr_idle_states].enter = &nap_loop;
208 nr_idle_states++;
209 }
210
211 if (flags & OPAL_PM_SLEEP_ENABLED ||
212 flags & OPAL_PM_SLEEP_ENABLED_ER1) {
213 /* Add FASTSLEEP state */
214 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
215 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
216 powernv_states[nr_idle_states].flags =
217 CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
218 powernv_states[nr_idle_states].exit_latency =
219 ((unsigned int)latency_ns) / 1000;
220 powernv_states[nr_idle_states].target_residency =
221 ((unsigned int)latency_ns / 100);
222 powernv_states[nr_idle_states].enter = &fastsleep_loop;
223 nr_idle_states++;
224 }
225 }
226
227 return nr_idle_states;
228 }
229
230 /*
231 * powernv_idle_probe()
232 * Choose state table for shared versus dedicated partition
233 */
234 static int powernv_idle_probe(void)
235 {
236 if (cpuidle_disable != IDLE_NO_OVERRIDE)
237 return -ENODEV;
238
239 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
240 cpuidle_state_table = powernv_states;
241 /* Device tree can indicate more idle states */
242 max_idle_state = powernv_add_idle_states();
243 } else
244 return -ENODEV;
245
246 return 0;
247 }
248
249 static int __init powernv_processor_idle_init(void)
250 {
251 int retval;
252
253 retval = powernv_idle_probe();
254 if (retval)
255 return retval;
256
257 powernv_cpuidle_driver_init();
258 retval = cpuidle_register(&powernv_idle_driver, NULL);
259 if (retval) {
260 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
261 return retval;
262 }
263
264 register_cpu_notifier(&setup_hotplug_notifier);
265 printk(KERN_DEBUG "powernv_idle_driver registered\n");
266 return 0;
267 }
268
269 device_initcall(powernv_processor_idle_init);
This page took 0.042555 seconds and 5 git commands to generate.