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