]>
Commit | Line | Data |
---|---|---|
2c2e6ecf DD |
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> | |
0d948730 | 14 | #include <linux/clockchips.h> |
0888839c | 15 | #include <linux/of.h> |
92c83ff5 | 16 | #include <linux/slab.h> |
2c2e6ecf DD |
17 | |
18 | #include <asm/machdep.h> | |
19 | #include <asm/firmware.h> | |
8eb8ac89 | 20 | #include <asm/opal.h> |
591ac0cb | 21 | #include <asm/runlatch.h> |
2c2e6ecf | 22 | |
0888839c | 23 | #define MAX_POWERNV_IDLE_STATES 8 |
0888839c | 24 | |
2c2e6ecf DD |
25 | struct cpuidle_driver powernv_idle_driver = { |
26 | .name = "powernv_idle", | |
27 | .owner = THIS_MODULE, | |
28 | }; | |
29 | ||
30 | static int max_idle_state; | |
31 | static struct cpuidle_state *cpuidle_state_table; | |
32 | ||
33 | static int snooze_loop(struct cpuidle_device *dev, | |
34 | struct cpuidle_driver *drv, | |
35 | int index) | |
36 | { | |
37 | local_irq_enable(); | |
38 | set_thread_flag(TIF_POLLING_NRFLAG); | |
39 | ||
591ac0cb | 40 | ppc64_runlatch_off(); |
2c2e6ecf DD |
41 | while (!need_resched()) { |
42 | HMT_low(); | |
43 | HMT_very_low(); | |
44 | } | |
45 | ||
46 | HMT_medium(); | |
591ac0cb | 47 | ppc64_runlatch_on(); |
2c2e6ecf DD |
48 | clear_thread_flag(TIF_POLLING_NRFLAG); |
49 | smp_mb(); | |
50 | return index; | |
51 | } | |
52 | ||
53 | static int nap_loop(struct cpuidle_device *dev, | |
54 | struct cpuidle_driver *drv, | |
55 | int index) | |
56 | { | |
591ac0cb | 57 | ppc64_runlatch_off(); |
2c2e6ecf | 58 | power7_idle(); |
591ac0cb | 59 | ppc64_runlatch_on(); |
2c2e6ecf DD |
60 | return index; |
61 | } | |
62 | ||
0d948730 PM |
63 | static int fastsleep_loop(struct cpuidle_device *dev, |
64 | struct cpuidle_driver *drv, | |
65 | int index) | |
66 | { | |
67 | unsigned long old_lpcr = mfspr(SPRN_LPCR); | |
68 | unsigned long new_lpcr; | |
69 | ||
70 | if (unlikely(system_state < SYSTEM_RUNNING)) | |
71 | return index; | |
72 | ||
73 | new_lpcr = old_lpcr; | |
9b6a68d9 MN |
74 | /* Do not exit powersave upon decrementer as we've setup the timer |
75 | * offload. | |
0d948730 | 76 | */ |
9b6a68d9 | 77 | new_lpcr &= ~LPCR_PECE1; |
0d948730 PM |
78 | |
79 | mtspr(SPRN_LPCR, new_lpcr); | |
80 | power7_sleep(); | |
81 | ||
82 | mtspr(SPRN_LPCR, old_lpcr); | |
83 | ||
84 | return index; | |
85 | } | |
86 | ||
2c2e6ecf DD |
87 | /* |
88 | * States for dedicated partition case. | |
89 | */ | |
0888839c | 90 | static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = { |
2c2e6ecf DD |
91 | { /* Snooze */ |
92 | .name = "snooze", | |
93 | .desc = "snooze", | |
2c2e6ecf DD |
94 | .exit_latency = 0, |
95 | .target_residency = 0, | |
96 | .enter = &snooze_loop }, | |
2c2e6ecf DD |
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 | ||
0888839c PM |
157 | static int powernv_add_idle_states(void) |
158 | { | |
159 | struct device_node *power_mgt; | |
0888839c PM |
160 | int nr_idle_states = 1; /* Snooze */ |
161 | int dt_idle_states; | |
95707d85 | 162 | const __be32 *idle_state_flags; |
92c83ff5 PM |
163 | u32 len_flags, flags; |
164 | u32 *latency_ns, *residency_ns; | |
165 | int i, rc; | |
0888839c PM |
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"); | |
92c83ff5 | 172 | goto out; |
0888839c PM |
173 | } |
174 | ||
92c83ff5 PM |
175 | idle_state_flags = of_get_property(power_mgt, |
176 | "ibm,cpu-idle-state-flags", &len_flags); | |
95707d85 | 177 | if (!idle_state_flags) { |
92c83ff5 PM |
178 | pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n"); |
179 | goto out; | |
0888839c PM |
180 | } |
181 | ||
92c83ff5 PM |
182 | dt_idle_states = len_flags / sizeof(u32); |
183 | ||
184 | latency_ns = kzalloc(sizeof(*latency_ns) * dt_idle_states, GFP_KERNEL); | |
185 | rc = of_property_read_u32_array(power_mgt, | |
186 | "ibm,cpu-idle-state-latencies-ns", latency_ns, dt_idle_states); | |
187 | if (rc) { | |
188 | pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n"); | |
189 | goto out_free_latency; | |
74aa51b5 PM |
190 | } |
191 | ||
92c83ff5 PM |
192 | residency_ns = kzalloc(sizeof(*residency_ns) * dt_idle_states, GFP_KERNEL); |
193 | rc = of_property_read_u32_array(power_mgt, | |
194 | "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states); | |
0888839c PM |
195 | |
196 | for (i = 0; i < dt_idle_states; i++) { | |
197 | ||
95707d85 | 198 | flags = be32_to_cpu(idle_state_flags[i]); |
74aa51b5 | 199 | |
92c83ff5 PM |
200 | /* |
201 | * Cpuidle accepts exit_latency and target_residency in us. | |
202 | * Use default target_residency values if f/w does not expose it. | |
74aa51b5 | 203 | */ |
8eb8ac89 | 204 | if (flags & OPAL_PM_NAP_ENABLED) { |
0888839c PM |
205 | /* Add NAP state */ |
206 | strcpy(powernv_states[nr_idle_states].name, "Nap"); | |
207 | strcpy(powernv_states[nr_idle_states].desc, "Nap"); | |
b82b6cca | 208 | powernv_states[nr_idle_states].flags = 0; |
92c83ff5 | 209 | powernv_states[nr_idle_states].target_residency = 100; |
0888839c | 210 | powernv_states[nr_idle_states].enter = &nap_loop; |
92c83ff5 | 211 | } else if (flags & OPAL_PM_SLEEP_ENABLED || |
7cba160a | 212 | flags & OPAL_PM_SLEEP_ENABLED_ER1) { |
0888839c PM |
213 | /* Add FASTSLEEP state */ |
214 | strcpy(powernv_states[nr_idle_states].name, "FastSleep"); | |
215 | strcpy(powernv_states[nr_idle_states].desc, "FastSleep"); | |
b82b6cca | 216 | powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP; |
92c83ff5 | 217 | powernv_states[nr_idle_states].target_residency = 300000; |
0888839c | 218 | powernv_states[nr_idle_states].enter = &fastsleep_loop; |
0888839c | 219 | } |
92c83ff5 PM |
220 | |
221 | powernv_states[nr_idle_states].exit_latency = | |
222 | ((unsigned int)latency_ns[i]) / 1000; | |
223 | ||
224 | if (!rc) { | |
225 | powernv_states[nr_idle_states].target_residency = | |
226 | ((unsigned int)residency_ns[i]) / 1000; | |
227 | } | |
228 | ||
229 | nr_idle_states++; | |
0888839c PM |
230 | } |
231 | ||
92c83ff5 PM |
232 | kfree(residency_ns); |
233 | out_free_latency: | |
234 | kfree(latency_ns); | |
235 | out: | |
0888839c PM |
236 | return nr_idle_states; |
237 | } | |
238 | ||
2c2e6ecf DD |
239 | /* |
240 | * powernv_idle_probe() | |
241 | * Choose state table for shared versus dedicated partition | |
242 | */ | |
243 | static int powernv_idle_probe(void) | |
244 | { | |
2c2e6ecf DD |
245 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
246 | return -ENODEV; | |
247 | ||
248 | if (firmware_has_feature(FW_FEATURE_OPALv3)) { | |
249 | cpuidle_state_table = powernv_states; | |
0888839c PM |
250 | /* Device tree can indicate more idle states */ |
251 | max_idle_state = powernv_add_idle_states(); | |
2c2e6ecf DD |
252 | } else |
253 | return -ENODEV; | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | static int __init powernv_processor_idle_init(void) | |
259 | { | |
260 | int retval; | |
261 | ||
262 | retval = powernv_idle_probe(); | |
263 | if (retval) | |
264 | return retval; | |
265 | ||
266 | powernv_cpuidle_driver_init(); | |
267 | retval = cpuidle_register(&powernv_idle_driver, NULL); | |
268 | if (retval) { | |
269 | printk(KERN_DEBUG "Registration of powernv driver failed.\n"); | |
270 | return retval; | |
271 | } | |
272 | ||
273 | register_cpu_notifier(&setup_hotplug_notifier); | |
274 | printk(KERN_DEBUG "powernv_idle_driver registered\n"); | |
275 | return 0; | |
276 | } | |
277 | ||
278 | device_initcall(powernv_processor_idle_init); |