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