Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 2 | /* |
Deepthi Dharwar | 962e7bd | 2014-01-14 16:26:02 +0530 | [diff] [blame] | 3 | * cpuidle-pseries - idle state cpuidle driver. |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 4 | * Adapted from drivers/idle/intel_idle.c and |
| 5 | * drivers/acpi/processor_idle.c |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/cpuidle.h> |
| 14 | #include <linux/cpu.h> |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 15 | #include <linux/notifier.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 16 | |
| 17 | #include <asm/paca.h> |
| 18 | #include <asm/reg.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 19 | #include <asm/machdep.h> |
| 20 | #include <asm/firmware.h> |
Preeti U Murthy | 3f67d96 | 2014-02-12 10:18:45 +0530 | [diff] [blame] | 21 | #include <asm/runlatch.h> |
Deepthi Dharwar | 212bebb | 2013-08-22 15:23:52 +0530 | [diff] [blame] | 22 | #include <asm/plpar_wrappers.h> |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 23 | |
| 24 | struct cpuidle_driver pseries_idle_driver = { |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 25 | .name = "pseries_idle", |
| 26 | .owner = THIS_MODULE, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
Nicholas Piggin | 624e46d | 2017-06-14 23:02:40 +1000 | [diff] [blame] | 29 | static int max_idle_state __read_mostly; |
| 30 | static struct cpuidle_state *cpuidle_state_table __read_mostly; |
| 31 | static u64 snooze_timeout __read_mostly; |
| 32 | static bool snooze_timeout_en __read_mostly; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 33 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 34 | static inline void idle_loop_prolog(unsigned long *in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 35 | { |
Nicolas Pitre | d8c6ad3 | 2014-01-29 12:45:10 -0500 | [diff] [blame] | 36 | ppc64_runlatch_off(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 37 | *in_purr = mfspr(SPRN_PURR); |
| 38 | /* |
| 39 | * Indicate to the HV that we are idle. Now would be |
| 40 | * a good time to find other work to dispatch. |
| 41 | */ |
| 42 | get_lppaca()->idle = 1; |
| 43 | } |
| 44 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 45 | static inline void idle_loop_epilog(unsigned long in_purr) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 46 | { |
Anton Blanchard | 7ffcf8e | 2013-08-07 02:01:46 +1000 | [diff] [blame] | 47 | u64 wait_cycles; |
| 48 | |
| 49 | wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); |
| 50 | wait_cycles += mfspr(SPRN_PURR) - in_purr; |
| 51 | get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 52 | get_lppaca()->idle = 0; |
Nicolas Pitre | d8c6ad3 | 2014-01-29 12:45:10 -0500 | [diff] [blame] | 53 | |
Nicolas Pitre | d8c6ad3 | 2014-01-29 12:45:10 -0500 | [diff] [blame] | 54 | ppc64_runlatch_on(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int snooze_loop(struct cpuidle_device *dev, |
| 58 | struct cpuidle_driver *drv, |
| 59 | int index) |
| 60 | { |
| 61 | unsigned long in_purr; |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 62 | u64 snooze_exit_time; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 63 | |
Nicholas Piggin | 3fc5ee92 | 2017-06-14 23:02:39 +1000 | [diff] [blame] | 64 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 65 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 66 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 67 | local_irq_enable(); |
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 | b69dbba0 | 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(); |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 73 | if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { |
| 74 | /* |
| 75 | * Task has not woken up but we are exiting the polling |
| 76 | * loop anyway. Require a barrier after polling is |
| 77 | * cleared to order subsequent test of need_resched(). |
| 78 | */ |
| 79 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 80 | smp_mb(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 81 | break; |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 82 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 85 | HMT_medium(); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 86 | clear_thread_flag(TIF_POLLING_NRFLAG); |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 87 | |
Nicholas Piggin | ced54c0 | 2017-11-17 02:00:52 +1000 | [diff] [blame] | 88 | local_irq_disable(); |
| 89 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 90 | idle_loop_epilog(in_purr); |
| 91 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 92 | return index; |
| 93 | } |
| 94 | |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 95 | static void check_and_cede_processor(void) |
| 96 | { |
| 97 | /* |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 98 | * Ensure our interrupt state is properly tracked, |
| 99 | * also checks if no interrupt has occurred while we |
| 100 | * were soft-disabled |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 101 | */ |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 102 | if (prep_irq_for_idle()) { |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 103 | cede_processor(); |
Benjamin Herrenschmidt | be2cf20 | 2012-07-10 18:36:40 +1000 | [diff] [blame] | 104 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 105 | /* Ensure that H_CEDE returns with IRQs on */ |
| 106 | if (WARN_ON(!(mfmsr() & MSR_EE))) |
| 107 | __hard_irq_enable(); |
| 108 | #endif |
| 109 | } |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 110 | } |
| 111 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 112 | static int dedicated_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 | get_lppaca()->donate_dedicated_cpu = 1; |
| 120 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 121 | HMT_medium(); |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 122 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 123 | |
Nicholas Piggin | ced54c0 | 2017-11-17 02:00:52 +1000 | [diff] [blame] | 124 | local_irq_disable(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 125 | get_lppaca()->donate_dedicated_cpu = 0; |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 126 | |
| 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 | static int shared_cede_loop(struct cpuidle_device *dev, |
| 133 | struct cpuidle_driver *drv, |
| 134 | int index) |
| 135 | { |
| 136 | unsigned long in_purr; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 137 | |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 138 | idle_loop_prolog(&in_purr); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Yield the processor to the hypervisor. We return if |
| 142 | * an external interrupt occurs (which are driven prior |
| 143 | * to returning here) or if a prod occurs from another |
| 144 | * processor. When returning here, external interrupts |
| 145 | * are enabled. |
| 146 | */ |
Benjamin Herrenschmidt | 7230c56 | 2012-03-06 18:27:59 +1100 | [diff] [blame] | 147 | check_and_cede_processor(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 148 | |
Nicholas Piggin | ced54c0 | 2017-11-17 02:00:52 +1000 | [diff] [blame] | 149 | local_irq_disable(); |
Daniel Lezcano | 1ca8094 | 2013-04-03 12:15:22 +0000 | [diff] [blame] | 150 | idle_loop_epilog(in_purr); |
| 151 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 152 | return index; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * States for dedicated partition case. |
| 157 | */ |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 158 | static struct cpuidle_state dedicated_states[] = { |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 159 | { /* Snooze */ |
| 160 | .name = "snooze", |
| 161 | .desc = "snooze", |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 162 | .exit_latency = 0, |
| 163 | .target_residency = 0, |
| 164 | .enter = &snooze_loop }, |
| 165 | { /* CEDE */ |
| 166 | .name = "CEDE", |
| 167 | .desc = "CEDE", |
Deepthi Dharwar | 83dac59 | 2012-10-03 18:42:26 +0000 | [diff] [blame] | 168 | .exit_latency = 10, |
| 169 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 170 | .enter = &dedicated_cede_loop }, |
| 171 | }; |
| 172 | |
| 173 | /* |
| 174 | * States for shared partition case. |
| 175 | */ |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 176 | static struct cpuidle_state shared_states[] = { |
Nicholas Piggin | f2ac428 | 2017-10-10 17:11:09 +1000 | [diff] [blame] | 177 | { /* Snooze */ |
| 178 | .name = "snooze", |
| 179 | .desc = "snooze", |
| 180 | .exit_latency = 0, |
| 181 | .target_residency = 0, |
| 182 | .enter = &snooze_loop }, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 183 | { /* Shared Cede */ |
| 184 | .name = "Shared Cede", |
| 185 | .desc = "Shared Cede", |
Nicholas Piggin | f2ac428 | 2017-10-10 17:11:09 +1000 | [diff] [blame] | 186 | .exit_latency = 10, |
| 187 | .target_residency = 100, |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 188 | .enter = &shared_cede_loop }, |
| 189 | }; |
| 190 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 191 | static int pseries_cpuidle_cpu_online(unsigned int cpu) |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 192 | { |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 193 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 194 | |
Deepthi Dharwar | 852d8cb | 2012-07-03 20:07:22 +0000 | [diff] [blame] | 195 | if (dev && cpuidle_get_driver()) { |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 196 | cpuidle_pause_and_lock(); |
| 197 | cpuidle_enable_device(dev); |
| 198 | cpuidle_resume_and_unlock(); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 199 | } |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 200 | return 0; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 203 | static int pseries_cpuidle_cpu_dead(unsigned int cpu) |
| 204 | { |
| 205 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
| 206 | |
| 207 | if (dev && cpuidle_get_driver()) { |
| 208 | cpuidle_pause_and_lock(); |
| 209 | cpuidle_disable_device(dev); |
| 210 | cpuidle_resume_and_unlock(); |
| 211 | } |
| 212 | return 0; |
| 213 | } |
Deepthi Dharwar | 16aaaff | 2012-05-20 18:34:27 +0000 | [diff] [blame] | 214 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 215 | /* |
| 216 | * pseries_cpuidle_driver_init() |
| 217 | */ |
| 218 | static int pseries_cpuidle_driver_init(void) |
| 219 | { |
| 220 | int idle_state; |
| 221 | struct cpuidle_driver *drv = &pseries_idle_driver; |
| 222 | |
| 223 | drv->state_count = 0; |
| 224 | |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 225 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 226 | /* Is the state not enabled? */ |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 227 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 228 | continue; |
| 229 | |
| 230 | drv->states[drv->state_count] = /* structure copy */ |
| 231 | cpuidle_state_table[idle_state]; |
| 232 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 233 | drv->state_count += 1; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 239 | /* |
| 240 | * pseries_idle_probe() |
| 241 | * Choose state table for shared versus dedicated partition |
| 242 | */ |
| 243 | static int pseries_idle_probe(void) |
| 244 | { |
| 245 | |
Deepthi Dharwar | e8bb3e0 | 2011-11-30 02:47:03 +0000 | [diff] [blame] | 246 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 247 | return -ENODEV; |
| 248 | |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 249 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 250 | if (lppaca_shared_proc(get_lppaca())) { |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 251 | cpuidle_state_table = shared_states; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 252 | max_idle_state = ARRAY_SIZE(shared_states); |
| 253 | } else { |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 254 | cpuidle_state_table = dedicated_states; |
Deepthi Dharwar | bf7f61f | 2014-01-14 16:26:28 +0530 | [diff] [blame] | 255 | max_idle_state = ARRAY_SIZE(dedicated_states); |
| 256 | } |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 257 | } else |
| 258 | return -ENODEV; |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 259 | |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 260 | if (max_idle_state > 1) { |
| 261 | snooze_timeout_en = true; |
| 262 | snooze_timeout = cpuidle_state_table[1].target_residency * |
| 263 | tb_ticks_per_usec; |
| 264 | } |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int __init pseries_processor_idle_init(void) |
| 269 | { |
| 270 | int retval; |
| 271 | |
| 272 | retval = pseries_idle_probe(); |
| 273 | if (retval) |
| 274 | return retval; |
| 275 | |
| 276 | pseries_cpuidle_driver_init(); |
Deepthi Dharwar | b69dbba0 | 2014-01-14 16:26:09 +0530 | [diff] [blame] | 277 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 278 | if (retval) { |
| 279 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); |
| 280 | return retval; |
| 281 | } |
| 282 | |
Sebastian Andrzej Siewior | 529351f | 2016-08-18 14:57:25 +0200 | [diff] [blame] | 283 | retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 284 | "cpuidle/pseries:online", |
| 285 | pseries_cpuidle_cpu_online, NULL); |
| 286 | WARN_ON(retval < 0); |
| 287 | retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD, |
| 288 | "cpuidle/pseries:DEAD", NULL, |
| 289 | pseries_cpuidle_cpu_dead); |
| 290 | WARN_ON(retval < 0); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 291 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
Deepthi Dharwar | 707827f | 2011-11-30 02:46:42 +0000 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Deepthi Dharwar | 12431c6 | 2014-01-14 16:26:18 +0530 | [diff] [blame] | 295 | device_initcall(pseries_processor_idle_init); |