Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 2 | /* |
| 3 | * cpuidle-powernv - idle state cpuidle driver. |
| 4 | * Adapted from drivers/cpuidle/cpuidle-pseries |
| 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> |
| 14 | #include <linux/notifier.h> |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 15 | #include <linux/clockchips.h> |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 18 | |
| 19 | #include <asm/machdep.h> |
| 20 | #include <asm/firmware.h> |
Shreyas B. Prabhu | 8eb8ac8 | 2014-12-10 00:26:51 +0530 | [diff] [blame] | 21 | #include <asm/opal.h> |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 22 | #include <asm/runlatch.h> |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 23 | #include <asm/cpuidle.h> |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 24 | |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 25 | /* |
| 26 | * Expose only those Hardware idle states via the cpuidle framework |
| 27 | * that have latency value below POWERNV_THRESHOLD_LATENCY_NS. |
| 28 | */ |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 29 | #define POWERNV_THRESHOLD_LATENCY_NS 200000 |
| 30 | |
Andrew Donnellan | ed61390 | 2016-11-22 16:08:06 +1100 | [diff] [blame] | 31 | static struct cpuidle_driver powernv_idle_driver = { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 32 | .name = "powernv_idle", |
| 33 | .owner = THIS_MODULE, |
| 34 | }; |
| 35 | |
Nicholas Piggin | 624e46d | 2017-06-14 23:02:40 +1000 | [diff] [blame] | 36 | static int max_idle_state __read_mostly; |
| 37 | static struct cpuidle_state *cpuidle_state_table __read_mostly; |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 38 | |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 39 | struct stop_psscr_table { |
| 40 | u64 val; |
| 41 | u64 mask; |
| 42 | }; |
| 43 | |
Nicholas Piggin | 624e46d | 2017-06-14 23:02:40 +1000 | [diff] [blame] | 44 | static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly; |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 45 | |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 46 | static u64 default_snooze_timeout __read_mostly; |
Nicholas Piggin | 624e46d | 2017-06-14 23:02:40 +1000 | [diff] [blame] | 47 | static bool snooze_timeout_en __read_mostly; |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 48 | |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 49 | static u64 get_snooze_timeout(struct cpuidle_device *dev, |
| 50 | struct cpuidle_driver *drv, |
| 51 | int index) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | if (unlikely(!snooze_timeout_en)) |
| 56 | return default_snooze_timeout; |
| 57 | |
| 58 | for (i = index + 1; i < drv->state_count; i++) { |
Rafael J. Wysocki | 99e98d3 | 2019-11-04 12:16:17 +0100 | [diff] [blame] | 59 | if (dev->states_usage[i].disable) |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 60 | continue; |
| 61 | |
Rafael J. Wysocki | 99e98d3 | 2019-11-04 12:16:17 +0100 | [diff] [blame] | 62 | return drv->states[i].target_residency * tb_ticks_per_usec; |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return default_snooze_timeout; |
| 66 | } |
| 67 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 68 | static int snooze_loop(struct cpuidle_device *dev, |
| 69 | struct cpuidle_driver *drv, |
| 70 | int index) |
| 71 | { |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 72 | u64 snooze_exit_time; |
| 73 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 74 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 75 | |
Nicholas Piggin | 3fc5ee92 | 2017-06-14 23:02:39 +1000 | [diff] [blame] | 76 | local_irq_enable(); |
| 77 | |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 78 | snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 79 | ppc64_runlatch_off(); |
Anton Blanchard | 26eb48a | 2017-04-04 07:54:13 +1000 | [diff] [blame] | 80 | HMT_very_low(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 81 | while (!need_resched()) { |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 82 | if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { |
| 83 | /* |
| 84 | * Task has not woken up but we are exiting the polling |
| 85 | * loop anyway. Require a barrier after polling is |
| 86 | * cleared to order subsequent test of need_resched(). |
| 87 | */ |
| 88 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 89 | smp_mb(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 90 | break; |
Nicholas Piggin | 7ded429 | 2017-06-14 23:02:41 +1000 | [diff] [blame] | 91 | } |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | HMT_medium(); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 95 | ppc64_runlatch_on(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 96 | clear_thread_flag(TIF_POLLING_NRFLAG); |
Nicholas Piggin | 3fc5ee92 | 2017-06-14 23:02:39 +1000 | [diff] [blame] | 97 | |
Nicholas Piggin | f1343d0 | 2017-11-17 02:00:51 +1000 | [diff] [blame] | 98 | local_irq_disable(); |
| 99 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 100 | return index; |
| 101 | } |
| 102 | |
| 103 | static int nap_loop(struct cpuidle_device *dev, |
| 104 | struct cpuidle_driver *drv, |
| 105 | int index) |
| 106 | { |
Nicholas Piggin | 2201f99 | 2017-06-13 23:05:45 +1000 | [diff] [blame] | 107 | power7_idle_type(PNV_THREAD_NAP); |
| 108 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 109 | return index; |
| 110 | } |
| 111 | |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 112 | /* Register for fastsleep only in oneshot mode of broadcast */ |
| 113 | #ifdef CONFIG_TICK_ONESHOT |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 114 | static int fastsleep_loop(struct cpuidle_device *dev, |
| 115 | struct cpuidle_driver *drv, |
| 116 | int index) |
| 117 | { |
| 118 | unsigned long old_lpcr = mfspr(SPRN_LPCR); |
| 119 | unsigned long new_lpcr; |
| 120 | |
| 121 | if (unlikely(system_state < SYSTEM_RUNNING)) |
| 122 | return index; |
| 123 | |
| 124 | new_lpcr = old_lpcr; |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 125 | /* Do not exit powersave upon decrementer as we've setup the timer |
| 126 | * offload. |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 127 | */ |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 128 | new_lpcr &= ~LPCR_PECE1; |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 129 | |
| 130 | mtspr(SPRN_LPCR, new_lpcr); |
Nicholas Piggin | 2201f99 | 2017-06-13 23:05:45 +1000 | [diff] [blame] | 131 | |
| 132 | power7_idle_type(PNV_THREAD_SLEEP); |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 133 | |
| 134 | mtspr(SPRN_LPCR, old_lpcr); |
| 135 | |
| 136 | return index; |
| 137 | } |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 138 | #endif |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 139 | |
| 140 | static int stop_loop(struct cpuidle_device *dev, |
| 141 | struct cpuidle_driver *drv, |
| 142 | int index) |
| 143 | { |
Nicholas Piggin | 2201f99 | 2017-06-13 23:05:45 +1000 | [diff] [blame] | 144 | power9_idle_type(stop_psscr_table[index].val, |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 145 | stop_psscr_table[index].mask); |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 146 | return index; |
| 147 | } |
| 148 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 149 | /* |
| 150 | * States for dedicated partition case. |
| 151 | */ |
Shreyas B. Prabhu | 169f3fa | 2016-07-08 11:50:50 +0530 | [diff] [blame] | 152 | static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 153 | { /* Snooze */ |
| 154 | .name = "snooze", |
| 155 | .desc = "snooze", |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 156 | .exit_latency = 0, |
| 157 | .target_residency = 0, |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame] | 158 | .enter = snooze_loop }, |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 159 | }; |
| 160 | |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 161 | static int powernv_cpuidle_cpu_online(unsigned int cpu) |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 162 | { |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 163 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 164 | |
| 165 | if (dev && cpuidle_get_driver()) { |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 166 | cpuidle_pause_and_lock(); |
| 167 | cpuidle_enable_device(dev); |
| 168 | cpuidle_resume_and_unlock(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 169 | } |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 170 | return 0; |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 171 | } |
| 172 | |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 173 | static int powernv_cpuidle_cpu_dead(unsigned int cpu) |
| 174 | { |
| 175 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
| 176 | |
| 177 | if (dev && cpuidle_get_driver()) { |
| 178 | cpuidle_pause_and_lock(); |
| 179 | cpuidle_disable_device(dev); |
| 180 | cpuidle_resume_and_unlock(); |
| 181 | } |
| 182 | return 0; |
| 183 | } |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * powernv_cpuidle_driver_init() |
| 187 | */ |
| 188 | static int powernv_cpuidle_driver_init(void) |
| 189 | { |
| 190 | int idle_state; |
| 191 | struct cpuidle_driver *drv = &powernv_idle_driver; |
| 192 | |
| 193 | drv->state_count = 0; |
| 194 | |
| 195 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 196 | /* Is the state not enabled? */ |
| 197 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 198 | continue; |
| 199 | |
| 200 | drv->states[drv->state_count] = /* structure copy */ |
| 201 | cpuidle_state_table[idle_state]; |
| 202 | |
| 203 | drv->state_count += 1; |
| 204 | } |
| 205 | |
Vaidyanathan Srinivasan | 293d264 | 2017-03-23 20:52:46 +0530 | [diff] [blame] | 206 | /* |
| 207 | * On the PowerNV platform cpu_present may be less than cpu_possible in |
| 208 | * cases when firmware detects the CPU, but it is not available to the |
| 209 | * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at |
| 210 | * run time and hence cpu_devices are not created for those CPUs by the |
| 211 | * generic topology_init(). |
| 212 | * |
| 213 | * drv->cpumask defaults to cpu_possible_mask in |
| 214 | * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where |
| 215 | * cpu_devices are not created for CPUs in cpu_possible_mask that |
| 216 | * cannot be hot-added later at run time. |
| 217 | * |
| 218 | * Trying cpuidle_register_device() on a CPU without a cpu_device is |
| 219 | * incorrect, so pass a correct CPU mask to the generic cpuidle driver. |
| 220 | */ |
| 221 | |
| 222 | drv->cpumask = (struct cpumask *)cpu_present_mask; |
| 223 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 227 | static inline void add_powernv_state(int index, const char *name, |
| 228 | unsigned int flags, |
| 229 | int (*idle_fn)(struct cpuidle_device *, |
| 230 | struct cpuidle_driver *, |
| 231 | int), |
| 232 | unsigned int target_residency, |
| 233 | unsigned int exit_latency, |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 234 | u64 psscr_val, u64 psscr_mask) |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 235 | { |
| 236 | strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN); |
| 237 | strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN); |
| 238 | powernv_states[index].flags = flags; |
| 239 | powernv_states[index].target_residency = target_residency; |
| 240 | powernv_states[index].exit_latency = exit_latency; |
| 241 | powernv_states[index].enter = idle_fn; |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 242 | /* For power8 and below psscr_* will be 0 */ |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 243 | stop_psscr_table[index].val = psscr_val; |
| 244 | stop_psscr_table[index].mask = psscr_mask; |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 245 | } |
| 246 | |
Gautham R. Shenoy | 785a12a | 2017-08-08 14:13:15 +0530 | [diff] [blame] | 247 | extern u32 pnv_get_supported_cpuidle_states(void); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 248 | static int powernv_add_idle_states(void) |
| 249 | { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 250 | int nr_idle_states = 1; /* Snooze */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 251 | int dt_idle_states; |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 252 | u32 has_stop_states = 0; |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 253 | int i; |
Gautham R. Shenoy | 785a12a | 2017-08-08 14:13:15 +0530 | [diff] [blame] | 254 | u32 supported_flags = pnv_get_supported_cpuidle_states(); |
| 255 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 256 | |
| 257 | /* Currently we have snooze statically defined */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 258 | if (nr_pnv_idle_states <= 0) { |
| 259 | pr_warn("cpuidle-powernv : Only Snooze is available\n"); |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 260 | goto out; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 261 | } |
| 262 | |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 263 | /* TODO: Count only states which are eligible for cpuidle */ |
| 264 | dt_idle_states = nr_pnv_idle_states; |
Gautham R. Shenoy | ecad450 | 2017-03-15 13:45:53 +0530 | [diff] [blame] | 265 | |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame] | 266 | /* |
| 267 | * Since snooze is used as first idle state, max idle states allowed is |
| 268 | * CPUIDLE_STATE_MAX -1 |
| 269 | */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 270 | if (nr_pnv_idle_states > CPUIDLE_STATE_MAX - 1) { |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame] | 271 | pr_warn("cpuidle-powernv: discovered idle states more than allowed"); |
| 272 | dt_idle_states = CPUIDLE_STATE_MAX - 1; |
| 273 | } |
| 274 | |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 275 | /* |
| 276 | * If the idle states use stop instruction, probe for psscr values |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 277 | * and psscr mask which are necessary to specify required stop level. |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 278 | */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 279 | has_stop_states = (pnv_idle_states[0].flags & |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 280 | (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)); |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 281 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 282 | for (i = 0; i < dt_idle_states; i++) { |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 283 | unsigned int exit_latency, target_residency; |
Gautham R. Shenoy | f9122ee | 2017-05-16 14:19:48 +0530 | [diff] [blame] | 284 | bool stops_timebase = false; |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 285 | struct pnv_idle_states_t *state = &pnv_idle_states[i]; |
Gautham R. Shenoy | 785a12a | 2017-08-08 14:13:15 +0530 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * Skip the platform idle state whose flag isn't in |
| 289 | * the supported_cpuidle_states flag mask. |
| 290 | */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 291 | if ((state->flags & supported_flags) != state->flags) |
Gautham R. Shenoy | 785a12a | 2017-08-08 14:13:15 +0530 | [diff] [blame] | 292 | continue; |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 293 | /* |
| 294 | * If an idle state has exit latency beyond |
| 295 | * POWERNV_THRESHOLD_LATENCY_NS then don't use it |
| 296 | * in cpu-idle. |
| 297 | */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 298 | if (state->latency_ns > POWERNV_THRESHOLD_LATENCY_NS) |
Shreyas B. Prabhu | 3005c59 | 2016-07-08 11:50:52 +0530 | [diff] [blame] | 299 | continue; |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 300 | /* |
| 301 | * Firmware passes residency and latency values in ns. |
| 302 | * cpuidle expects it in us. |
| 303 | */ |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 304 | exit_latency = DIV_ROUND_UP(state->latency_ns, 1000); |
| 305 | target_residency = DIV_ROUND_UP(state->residency_ns, 1000); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 306 | |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 307 | if (has_stop_states && !(state->valid)) |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 308 | continue; |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 309 | |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 310 | if (state->flags & OPAL_PM_TIMEBASE_STOP) |
Gautham R. Shenoy | f9122ee | 2017-05-16 14:19:48 +0530 | [diff] [blame] | 311 | stops_timebase = true; |
| 312 | |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 313 | if (state->flags & OPAL_PM_NAP_ENABLED) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 314 | /* Add NAP state */ |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 315 | add_powernv_state(nr_idle_states, "Nap", |
| 316 | CPUIDLE_FLAG_NONE, nap_loop, |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 317 | target_residency, exit_latency, 0, 0); |
Gautham R. Shenoy | f9122ee | 2017-05-16 14:19:48 +0530 | [diff] [blame] | 318 | } else if (has_stop_states && !stops_timebase) { |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 319 | add_powernv_state(nr_idle_states, state->name, |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 320 | CPUIDLE_FLAG_NONE, stop_loop, |
| 321 | target_residency, exit_latency, |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 322 | state->psscr_val, |
| 323 | state->psscr_mask); |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come |
| 328 | * within this config dependency check. |
| 329 | */ |
| 330 | #ifdef CONFIG_TICK_ONESHOT |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 331 | else if (state->flags & OPAL_PM_SLEEP_ENABLED || |
| 332 | state->flags & OPAL_PM_SLEEP_ENABLED_ER1) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 333 | /* Add FASTSLEEP state */ |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 334 | add_powernv_state(nr_idle_states, "FastSleep", |
| 335 | CPUIDLE_FLAG_TIMER_STOP, |
| 336 | fastsleep_loop, |
Gautham R. Shenoy | 09206b6 | 2017-01-25 14:06:28 +0530 | [diff] [blame] | 337 | target_residency, exit_latency, 0, 0); |
Gautham R. Shenoy | f9122ee | 2017-05-16 14:19:48 +0530 | [diff] [blame] | 338 | } else if (has_stop_states && stops_timebase) { |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 339 | add_powernv_state(nr_idle_states, state->name, |
Gautham R. Shenoy | 9e9fc6f | 2017-01-25 14:06:27 +0530 | [diff] [blame] | 340 | CPUIDLE_FLAG_TIMER_STOP, stop_loop, |
| 341 | target_residency, exit_latency, |
Akshay Adiga | 1961aca | 2018-07-05 17:10:22 +0530 | [diff] [blame] | 342 | state->psscr_val, |
| 343 | state->psscr_mask); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 344 | } |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 345 | #endif |
Gautham R. Shenoy | f9122ee | 2017-05-16 14:19:48 +0530 | [diff] [blame] | 346 | else |
| 347 | continue; |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 348 | nr_idle_states++; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 349 | } |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 350 | out: |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 351 | return nr_idle_states; |
| 352 | } |
| 353 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 354 | /* |
| 355 | * powernv_idle_probe() |
| 356 | * Choose state table for shared versus dedicated partition |
| 357 | */ |
| 358 | static int powernv_idle_probe(void) |
| 359 | { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 360 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 361 | return -ENODEV; |
| 362 | |
Stewart Smith | e4d54f7 | 2015-12-09 17:18:20 +1100 | [diff] [blame] | 363 | if (firmware_has_feature(FW_FEATURE_OPAL)) { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 364 | cpuidle_state_table = powernv_states; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 365 | /* Device tree can indicate more idle states */ |
| 366 | max_idle_state = powernv_add_idle_states(); |
Gautham R. Shenoy | 0a4ec6a | 2018-05-31 17:45:09 +0530 | [diff] [blame] | 367 | default_snooze_timeout = TICK_USEC * tb_ticks_per_usec; |
| 368 | if (max_idle_state > 1) |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 369 | snooze_timeout_en = true; |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 370 | } else |
| 371 | return -ENODEV; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int __init powernv_processor_idle_init(void) |
| 377 | { |
| 378 | int retval; |
| 379 | |
| 380 | retval = powernv_idle_probe(); |
| 381 | if (retval) |
| 382 | return retval; |
| 383 | |
| 384 | powernv_cpuidle_driver_init(); |
| 385 | retval = cpuidle_register(&powernv_idle_driver, NULL); |
| 386 | if (retval) { |
| 387 | printk(KERN_DEBUG "Registration of powernv driver failed.\n"); |
| 388 | return retval; |
| 389 | } |
| 390 | |
Sebastian Andrzej Siewior | 10fcca9 | 2016-08-24 11:12:59 +0200 | [diff] [blame] | 391 | retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 392 | "cpuidle/powernv:online", |
| 393 | powernv_cpuidle_cpu_online, NULL); |
| 394 | WARN_ON(retval < 0); |
| 395 | retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD, |
| 396 | "cpuidle/powernv:dead", NULL, |
| 397 | powernv_cpuidle_cpu_dead); |
| 398 | WARN_ON(retval < 0); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 399 | printk(KERN_DEBUG "powernv_idle_driver registered\n"); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | device_initcall(powernv_processor_idle_init); |