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