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