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