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