Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * processor_idle - idle state cpuidle driver. |
| 3 | * Adapted from drivers/idle/intel_idle.c and |
| 4 | * drivers/acpi/processor_idle.c |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/cpuidle.h> |
| 13 | #include <linux/cpu.h> |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 14 | #include <linux/notifier.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 15 | |
| 16 | #include <asm/paca.h> |
| 17 | #include <asm/reg.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 18 | #include <asm/machdep.h> |
| 19 | #include <asm/firmware.h> |
David Howells | ae3a197e | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 20 | #include <asm/runlatch.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 21 | |
| 22 | #include "plpar_wrappers.h" |
| 23 | #include "pseries.h" |
| 24 | |
| 25 | struct cpuidle_driver pseries_idle_driver = { |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 26 | .name = "pseries_idle", |
| 27 | .owner = THIS_MODULE, |
| 28 | .en_core_tk_irqen = 1, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | #define MAX_IDLE_STATE_COUNT 2 |
| 32 | |
| 33 | static int max_idle_state = MAX_IDLE_STATE_COUNT - 1; |
| 34 | static struct cpuidle_device __percpu *pseries_cpuidle_devices; |
| 35 | static struct cpuidle_state *cpuidle_state_table; |
| 36 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 37 | static inline void idle_loop_prolog(unsigned long *in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 38 | { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 39 | *in_purr = mfspr(SPRN_PURR); |
| 40 | /* |
| 41 | * Indicate to the HV that we are idle. Now would be |
| 42 | * a good time to find other work to dispatch. |
| 43 | */ |
| 44 | get_lppaca()->idle = 1; |
| 45 | } |
| 46 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 47 | static inline void idle_loop_epilog(unsigned long in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 48 | { |
| 49 | get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr; |
| 50 | get_lppaca()->idle = 0; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int snooze_loop(struct cpuidle_device *dev, |
| 54 | struct cpuidle_driver *drv, |
| 55 | int index) |
| 56 | { |
| 57 | unsigned long in_purr; |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 58 | int cpu = dev->cpu; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 59 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 60 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 61 | local_irq_enable(); |
| 62 | set_thread_flag(TIF_POLLING_NRFLAG); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 63 | |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 64 | while ((!need_resched()) && cpu_online(cpu)) { |
| 65 | ppc64_runlatch_off(); |
| 66 | HMT_low(); |
| 67 | HMT_very_low(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 70 | HMT_medium(); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 71 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 72 | smp_mb(); |
| 73 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 74 | idle_loop_epilog(in_purr); |
| 75 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 76 | return index; |
| 77 | } |
| 78 | |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 79 | static void check_and_cede_processor(void) |
| 80 | { |
| 81 | /* |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 82 | * Ensure our interrupt state is properly tracked, |
| 83 | * also checks if no interrupt has occurred while we |
| 84 | * were soft-disabled |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 85 | */ |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 86 | if (prep_irq_for_idle()) { |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 87 | cede_processor(); |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 88 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 89 | /* Ensure that H_CEDE returns with IRQs on */ |
| 90 | if (WARN_ON(!(mfmsr() & MSR_EE))) |
| 91 | __hard_irq_enable(); |
| 92 | #endif |
| 93 | } |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 94 | } |
| 95 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 96 | static int dedicated_cede_loop(struct cpuidle_device *dev, |
| 97 | struct cpuidle_driver *drv, |
| 98 | int index) |
| 99 | { |
| 100 | unsigned long in_purr; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 101 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 102 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 103 | get_lppaca()->donate_dedicated_cpu = 1; |
| 104 | |
| 105 | ppc64_runlatch_off(); |
| 106 | HMT_medium(); |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 107 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 108 | |
| 109 | get_lppaca()->donate_dedicated_cpu = 0; |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 110 | |
| 111 | idle_loop_epilog(in_purr); |
| 112 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 113 | return index; |
| 114 | } |
| 115 | |
| 116 | static int shared_cede_loop(struct cpuidle_device *dev, |
| 117 | struct cpuidle_driver *drv, |
| 118 | int index) |
| 119 | { |
| 120 | unsigned long in_purr; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 121 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 122 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * Yield the processor to the hypervisor. We return if |
| 126 | * an external interrupt occurs (which are driven prior |
| 127 | * to returning here) or if a prod occurs from another |
| 128 | * processor. When returning here, external interrupts |
| 129 | * are enabled. |
| 130 | */ |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 131 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 132 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame^] | 133 | idle_loop_epilog(in_purr); |
| 134 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 135 | return index; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * States for dedicated partition case. |
| 140 | */ |
| 141 | static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = { |
| 142 | { /* Snooze */ |
| 143 | .name = "snooze", |
| 144 | .desc = "snooze", |
| 145 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 146 | .exit_latency = 0, |
| 147 | .target_residency = 0, |
| 148 | .enter = &snooze_loop }, |
| 149 | { /* CEDE */ |
| 150 | .name = "CEDE", |
| 151 | .desc = "CEDE", |
| 152 | .flags = CPUIDLE_FLAG_TIME_VALID, |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 153 | .exit_latency = 10, |
| 154 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 155 | .enter = &dedicated_cede_loop }, |
| 156 | }; |
| 157 | |
| 158 | /* |
| 159 | * States for shared partition case. |
| 160 | */ |
| 161 | static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = { |
| 162 | { /* Shared Cede */ |
| 163 | .name = "Shared Cede", |
| 164 | .desc = "Shared Cede", |
| 165 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 166 | .exit_latency = 0, |
| 167 | .target_residency = 0, |
| 168 | .enter = &shared_cede_loop }, |
| 169 | }; |
| 170 | |
Deepthi Dharwar | 8ea959a | 2012-10-03 18:42:18 +0000 | [diff] [blame] | 171 | void update_smt_snooze_delay(int cpu, int residency) |
| 172 | { |
| 173 | struct cpuidle_driver *drv = cpuidle_get_driver(); |
| 174 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
| 175 | |
| 176 | if (cpuidle_state_table != dedicated_states) |
| 177 | return; |
| 178 | |
| 179 | if (residency < 0) { |
| 180 | /* Disable the Nap state on that cpu */ |
| 181 | if (dev) |
| 182 | dev->states_usage[1].disable = 1; |
| 183 | } else |
| 184 | if (drv) |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 185 | drv->states[1].target_residency = residency; |
Deepthi Dharwar | 8ea959a | 2012-10-03 18:42:18 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 188 | static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n, |
| 189 | unsigned long action, void *hcpu) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 190 | { |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 191 | int hotcpu = (unsigned long)hcpu; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 192 | struct cpuidle_device *dev = |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 193 | per_cpu_ptr(pseries_cpuidle_devices, hotcpu); |
| 194 | |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 195 | if (dev && cpuidle_get_driver()) { |
| 196 | switch (action) { |
| 197 | case CPU_ONLINE: |
| 198 | case CPU_ONLINE_FROZEN: |
| 199 | cpuidle_pause_and_lock(); |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 200 | cpuidle_enable_device(dev); |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 201 | cpuidle_resume_and_unlock(); |
| 202 | break; |
| 203 | |
| 204 | case CPU_DEAD: |
| 205 | case CPU_DEAD_FROZEN: |
| 206 | cpuidle_pause_and_lock(); |
| 207 | cpuidle_disable_device(dev); |
| 208 | cpuidle_resume_and_unlock(); |
| 209 | break; |
| 210 | |
| 211 | default: |
| 212 | return NOTIFY_DONE; |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 213 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 214 | } |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 215 | return NOTIFY_OK; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 218 | static struct notifier_block setup_hotplug_notifier = { |
| 219 | .notifier_call = pseries_cpuidle_add_cpu_notifier, |
| 220 | }; |
| 221 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 222 | /* |
| 223 | * pseries_cpuidle_driver_init() |
| 224 | */ |
| 225 | static int pseries_cpuidle_driver_init(void) |
| 226 | { |
| 227 | int idle_state; |
| 228 | struct cpuidle_driver *drv = &pseries_idle_driver; |
| 229 | |
| 230 | drv->state_count = 0; |
| 231 | |
| 232 | for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) { |
| 233 | |
| 234 | if (idle_state > max_idle_state) |
| 235 | break; |
| 236 | |
| 237 | /* is the state not enabled? */ |
| 238 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 239 | continue; |
| 240 | |
| 241 | drv->states[drv->state_count] = /* structure copy */ |
| 242 | cpuidle_state_table[idle_state]; |
| 243 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 244 | drv->state_count += 1; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* pseries_idle_devices_uninit(void) |
| 251 | * unregister cpuidle devices and de-allocate memory |
| 252 | */ |
| 253 | static void pseries_idle_devices_uninit(void) |
| 254 | { |
| 255 | int i; |
| 256 | struct cpuidle_device *dev; |
| 257 | |
| 258 | for_each_possible_cpu(i) { |
| 259 | dev = per_cpu_ptr(pseries_cpuidle_devices, i); |
| 260 | cpuidle_unregister_device(dev); |
| 261 | } |
| 262 | |
| 263 | free_percpu(pseries_cpuidle_devices); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /* pseries_idle_devices_init() |
| 268 | * allocate, initialize and register cpuidle device |
| 269 | */ |
| 270 | static int pseries_idle_devices_init(void) |
| 271 | { |
| 272 | int i; |
| 273 | struct cpuidle_driver *drv = &pseries_idle_driver; |
| 274 | struct cpuidle_device *dev; |
| 275 | |
| 276 | pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device); |
| 277 | if (pseries_cpuidle_devices == NULL) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | for_each_possible_cpu(i) { |
| 281 | dev = per_cpu_ptr(pseries_cpuidle_devices, i); |
| 282 | dev->state_count = drv->state_count; |
| 283 | dev->cpu = i; |
| 284 | if (cpuidle_register_device(dev)) { |
| 285 | printk(KERN_DEBUG \ |
| 286 | "cpuidle_register_device %d failed!\n", i); |
| 287 | return -EIO; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * pseries_idle_probe() |
| 296 | * Choose state table for shared versus dedicated partition |
| 297 | */ |
| 298 | static int pseries_idle_probe(void) |
| 299 | { |
| 300 | |
| 301 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) |
| 302 | return -ENODEV; |
| 303 | |
Deepthi Dharwar | e8bb3e0 | 2011-11-30 02:47:03 +0000 | [diff] [blame] | 304 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 305 | return -ENODEV; |
| 306 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 307 | if (max_idle_state == 0) { |
| 308 | printk(KERN_DEBUG "pseries processor idle disabled.\n"); |
| 309 | return -EPERM; |
| 310 | } |
| 311 | |
| 312 | if (get_lppaca()->shared_proc) |
| 313 | cpuidle_state_table = shared_states; |
| 314 | else |
| 315 | cpuidle_state_table = dedicated_states; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int __init pseries_processor_idle_init(void) |
| 321 | { |
| 322 | int retval; |
| 323 | |
| 324 | retval = pseries_idle_probe(); |
| 325 | if (retval) |
| 326 | return retval; |
| 327 | |
| 328 | pseries_cpuidle_driver_init(); |
| 329 | retval = cpuidle_register_driver(&pseries_idle_driver); |
| 330 | if (retval) { |
| 331 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); |
| 332 | return retval; |
| 333 | } |
| 334 | |
| 335 | retval = pseries_idle_devices_init(); |
| 336 | if (retval) { |
| 337 | pseries_idle_devices_uninit(); |
| 338 | cpuidle_unregister_driver(&pseries_idle_driver); |
| 339 | return retval; |
| 340 | } |
| 341 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 342 | register_cpu_notifier(&setup_hotplug_notifier); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 343 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static void __exit pseries_processor_idle_exit(void) |
| 349 | { |
| 350 | |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 351 | unregister_cpu_notifier(&setup_hotplug_notifier); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 352 | pseries_idle_devices_uninit(); |
| 353 | cpuidle_unregister_driver(&pseries_idle_driver); |
| 354 | |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | module_init(pseries_processor_idle_init); |
| 359 | module_exit(pseries_processor_idle_exit); |
| 360 | |
| 361 | MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>"); |
| 362 | MODULE_DESCRIPTION("Cpuidle driver for POWER"); |
| 363 | MODULE_LICENSE("GPL"); |