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