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