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 | 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> |
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 | |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 27 | static int max_idle_state; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 28 | static struct cpuidle_state *cpuidle_state_table; |
| 29 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 30 | static inline void idle_loop_prolog(unsigned long *in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 31 | { |
Nicolas Pitre | d8c6ad3 | 2014-01-29 12:45:10 -0500 | [diff] [blame^] | 32 | ppc64_runlatch_off(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 33 | *in_purr = mfspr(SPRN_PURR); |
| 34 | /* |
| 35 | * Indicate to the HV that we are idle. Now would be |
| 36 | * a good time to find other work to dispatch. |
| 37 | */ |
| 38 | get_lppaca()->idle = 1; |
| 39 | } |
| 40 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 41 | static inline void idle_loop_epilog(unsigned long in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 42 | { |
Anton Blanchard | 7ffcf8e | 2013-08-07 02:01:46 +1000 | [diff] [blame] | 43 | u64 wait_cycles; |
| 44 | |
| 45 | wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); |
| 46 | wait_cycles += mfspr(SPRN_PURR) - in_purr; |
| 47 | get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 48 | get_lppaca()->idle = 0; |
Nicolas Pitre | d8c6ad3 | 2014-01-29 12:45:10 -0500 | [diff] [blame^] | 49 | |
| 50 | if (irqs_disabled()) |
| 51 | local_irq_enable(); |
| 52 | ppc64_runlatch_on(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int snooze_loop(struct cpuidle_device *dev, |
| 56 | struct cpuidle_driver *drv, |
| 57 | int index) |
| 58 | { |
| 59 | unsigned long in_purr; |
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 | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 65 | while (!need_resched()) { |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 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 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 105 | HMT_medium(); |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 106 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 107 | |
| 108 | get_lppaca()->donate_dedicated_cpu = 0; |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 109 | |
| 110 | idle_loop_epilog(in_purr); |
| 111 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 112 | return index; |
| 113 | } |
| 114 | |
| 115 | static int shared_cede_loop(struct cpuidle_device *dev, |
| 116 | struct cpuidle_driver *drv, |
| 117 | int index) |
| 118 | { |
| 119 | unsigned long in_purr; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 120 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 121 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Yield the processor to the hypervisor. We return if |
| 125 | * an external interrupt occurs (which are driven prior |
| 126 | * to returning here) or if a prod occurs from another |
| 127 | * processor. When returning here, external interrupts |
| 128 | * are enabled. |
| 129 | */ |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 130 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 131 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 132 | idle_loop_epilog(in_purr); |
| 133 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 134 | return index; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * States for dedicated partition case. |
| 139 | */ |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 140 | static struct cpuidle_state dedicated_states[] = { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 141 | { /* Snooze */ |
| 142 | .name = "snooze", |
| 143 | .desc = "snooze", |
| 144 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 145 | .exit_latency = 0, |
| 146 | .target_residency = 0, |
| 147 | .enter = &snooze_loop }, |
| 148 | { /* CEDE */ |
| 149 | .name = "CEDE", |
| 150 | .desc = "CEDE", |
| 151 | .flags = CPUIDLE_FLAG_TIME_VALID, |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 152 | .exit_latency = 10, |
| 153 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 154 | .enter = &dedicated_cede_loop }, |
| 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * States for shared partition case. |
| 159 | */ |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 160 | static struct cpuidle_state shared_states[] = { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 161 | { /* Shared Cede */ |
| 162 | .name = "Shared Cede", |
| 163 | .desc = "Shared Cede", |
| 164 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 165 | .exit_latency = 0, |
| 166 | .target_residency = 0, |
| 167 | .enter = &shared_cede_loop }, |
| 168 | }; |
| 169 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 170 | static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n, |
| 171 | unsigned long action, void *hcpu) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 172 | { |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 173 | int hotcpu = (unsigned long)hcpu; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 174 | struct cpuidle_device *dev = |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 175 | per_cpu(cpuidle_devices, hotcpu); |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 176 | |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 177 | if (dev && cpuidle_get_driver()) { |
| 178 | switch (action) { |
| 179 | case CPU_ONLINE: |
| 180 | case CPU_ONLINE_FROZEN: |
| 181 | cpuidle_pause_and_lock(); |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 182 | cpuidle_enable_device(dev); |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 183 | cpuidle_resume_and_unlock(); |
| 184 | break; |
| 185 | |
| 186 | case CPU_DEAD: |
| 187 | case CPU_DEAD_FROZEN: |
| 188 | cpuidle_pause_and_lock(); |
| 189 | cpuidle_disable_device(dev); |
| 190 | cpuidle_resume_and_unlock(); |
| 191 | break; |
| 192 | |
| 193 | default: |
| 194 | return NOTIFY_DONE; |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 195 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 196 | } |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 197 | return NOTIFY_OK; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 200 | static struct notifier_block setup_hotplug_notifier = { |
| 201 | .notifier_call = pseries_cpuidle_add_cpu_notifier, |
| 202 | }; |
| 203 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 204 | /* |
| 205 | * pseries_cpuidle_driver_init() |
| 206 | */ |
| 207 | static int pseries_cpuidle_driver_init(void) |
| 208 | { |
| 209 | int idle_state; |
| 210 | struct cpuidle_driver *drv = &pseries_idle_driver; |
| 211 | |
| 212 | drv->state_count = 0; |
| 213 | |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 214 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 215 | /* Is the state not enabled? */ |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 216 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 217 | continue; |
| 218 | |
| 219 | drv->states[drv->state_count] = /* structure copy */ |
| 220 | cpuidle_state_table[idle_state]; |
| 221 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 222 | drv->state_count += 1; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 228 | /* |
| 229 | * pseries_idle_probe() |
| 230 | * Choose state table for shared versus dedicated partition |
| 231 | */ |
| 232 | static int pseries_idle_probe(void) |
| 233 | { |
| 234 | |
Deepthi Dharwar | e8bb3e0 | 2011-11-30 02:47:03 +0000 | [diff] [blame] | 235 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 236 | return -ENODEV; |
| 237 | |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 238 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 239 | if (lppaca_shared_proc(get_lppaca())) { |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 240 | cpuidle_state_table = shared_states; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 241 | max_idle_state = ARRAY_SIZE(shared_states); |
| 242 | } else { |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 243 | cpuidle_state_table = dedicated_states; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 244 | max_idle_state = ARRAY_SIZE(dedicated_states); |
| 245 | } |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 246 | } else |
| 247 | return -ENODEV; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int __init pseries_processor_idle_init(void) |
| 253 | { |
| 254 | int retval; |
| 255 | |
| 256 | retval = pseries_idle_probe(); |
| 257 | if (retval) |
| 258 | return retval; |
| 259 | |
| 260 | pseries_cpuidle_driver_init(); |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 261 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 262 | if (retval) { |
| 263 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); |
| 264 | return retval; |
| 265 | } |
| 266 | |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 267 | register_cpu_notifier(&setup_hotplug_notifier); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 268 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Deepthi Dharwar | 12431c6 | 2014-01-14 16:26:18 +0530 | [diff] [blame] | 272 | device_initcall(pseries_processor_idle_init); |