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