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> |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 22 | |
| 23 | struct cpuidle_driver powernv_idle_driver = { |
| 24 | .name = "powernv_idle", |
| 25 | .owner = THIS_MODULE, |
| 26 | }; |
| 27 | |
| 28 | static int max_idle_state; |
| 29 | static struct cpuidle_state *cpuidle_state_table; |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 30 | static u64 snooze_timeout; |
| 31 | static bool snooze_timeout_en; |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 32 | |
| 33 | static int snooze_loop(struct cpuidle_device *dev, |
| 34 | struct cpuidle_driver *drv, |
| 35 | int index) |
| 36 | { |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 37 | u64 snooze_exit_time; |
| 38 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 39 | local_irq_enable(); |
| 40 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 41 | |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 42 | snooze_exit_time = get_tb() + snooze_timeout; |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 43 | ppc64_runlatch_off(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 44 | while (!need_resched()) { |
| 45 | HMT_low(); |
| 46 | HMT_very_low(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 47 | if (snooze_timeout_en && get_tb() > snooze_exit_time) |
| 48 | break; |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | HMT_medium(); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 52 | ppc64_runlatch_on(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 53 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 54 | smp_mb(); |
| 55 | return index; |
| 56 | } |
| 57 | |
| 58 | static int nap_loop(struct cpuidle_device *dev, |
| 59 | struct cpuidle_driver *drv, |
| 60 | int index) |
| 61 | { |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 62 | ppc64_runlatch_off(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 63 | power7_idle(); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 64 | ppc64_runlatch_on(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 65 | return index; |
| 66 | } |
| 67 | |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 68 | /* Register for fastsleep only in oneshot mode of broadcast */ |
| 69 | #ifdef CONFIG_TICK_ONESHOT |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 70 | static int fastsleep_loop(struct cpuidle_device *dev, |
| 71 | struct cpuidle_driver *drv, |
| 72 | int index) |
| 73 | { |
| 74 | unsigned long old_lpcr = mfspr(SPRN_LPCR); |
| 75 | unsigned long new_lpcr; |
| 76 | |
| 77 | if (unlikely(system_state < SYSTEM_RUNNING)) |
| 78 | return index; |
| 79 | |
| 80 | new_lpcr = old_lpcr; |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 81 | /* Do not exit powersave upon decrementer as we've setup the timer |
| 82 | * offload. |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 83 | */ |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 84 | new_lpcr &= ~LPCR_PECE1; |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 85 | |
| 86 | mtspr(SPRN_LPCR, new_lpcr); |
| 87 | power7_sleep(); |
| 88 | |
| 89 | mtspr(SPRN_LPCR, old_lpcr); |
| 90 | |
| 91 | return index; |
| 92 | } |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 93 | #endif |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 94 | /* |
| 95 | * States for dedicated partition case. |
| 96 | */ |
Shreyas B. Prabhu | 169f3fa | 2016-07-08 11:50:50 +0530 | [diff] [blame] | 97 | static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 98 | { /* Snooze */ |
| 99 | .name = "snooze", |
| 100 | .desc = "snooze", |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 101 | .exit_latency = 0, |
| 102 | .target_residency = 0, |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 103 | .enter = snooze_loop }, |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n, |
| 107 | unsigned long action, void *hcpu) |
| 108 | { |
| 109 | int hotcpu = (unsigned long)hcpu; |
| 110 | struct cpuidle_device *dev = |
| 111 | per_cpu(cpuidle_devices, hotcpu); |
| 112 | |
| 113 | if (dev && cpuidle_get_driver()) { |
| 114 | switch (action) { |
| 115 | case CPU_ONLINE: |
| 116 | case CPU_ONLINE_FROZEN: |
| 117 | cpuidle_pause_and_lock(); |
| 118 | cpuidle_enable_device(dev); |
| 119 | cpuidle_resume_and_unlock(); |
| 120 | break; |
| 121 | |
| 122 | case CPU_DEAD: |
| 123 | case CPU_DEAD_FROZEN: |
| 124 | cpuidle_pause_and_lock(); |
| 125 | cpuidle_disable_device(dev); |
| 126 | cpuidle_resume_and_unlock(); |
| 127 | break; |
| 128 | |
| 129 | default: |
| 130 | return NOTIFY_DONE; |
| 131 | } |
| 132 | } |
| 133 | return NOTIFY_OK; |
| 134 | } |
| 135 | |
| 136 | static struct notifier_block setup_hotplug_notifier = { |
| 137 | .notifier_call = powernv_cpuidle_add_cpu_notifier, |
| 138 | }; |
| 139 | |
| 140 | /* |
| 141 | * powernv_cpuidle_driver_init() |
| 142 | */ |
| 143 | static int powernv_cpuidle_driver_init(void) |
| 144 | { |
| 145 | int idle_state; |
| 146 | struct cpuidle_driver *drv = &powernv_idle_driver; |
| 147 | |
| 148 | drv->state_count = 0; |
| 149 | |
| 150 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 151 | /* Is the state not enabled? */ |
| 152 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 153 | continue; |
| 154 | |
| 155 | drv->states[drv->state_count] = /* structure copy */ |
| 156 | cpuidle_state_table[idle_state]; |
| 157 | |
| 158 | drv->state_count += 1; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 164 | static int powernv_add_idle_states(void) |
| 165 | { |
| 166 | struct device_node *power_mgt; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 167 | int nr_idle_states = 1; /* Snooze */ |
| 168 | int dt_idle_states; |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 169 | u32 latency_ns[CPUIDLE_STATE_MAX]; |
| 170 | u32 residency_ns[CPUIDLE_STATE_MAX]; |
| 171 | u32 flags[CPUIDLE_STATE_MAX]; |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 172 | int i, rc; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 173 | |
| 174 | /* Currently we have snooze statically defined */ |
| 175 | |
| 176 | power_mgt = of_find_node_by_path("/ibm,opal/power-mgt"); |
| 177 | if (!power_mgt) { |
| 178 | pr_warn("opal: PowerMgmt Node not found\n"); |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 179 | goto out; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 180 | } |
| 181 | |
Preeti U Murthy | 70734a7 | 2015-02-18 23:24:53 -0600 | [diff] [blame] | 182 | /* Read values of any property to determine the num of idle states */ |
| 183 | dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags"); |
| 184 | if (dt_idle_states < 0) { |
| 185 | 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] | 186 | goto out; |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 187 | } |
| 188 | |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 189 | /* |
| 190 | * Since snooze is used as first idle state, max idle states allowed is |
| 191 | * CPUIDLE_STATE_MAX -1 |
| 192 | */ |
| 193 | if (dt_idle_states > CPUIDLE_STATE_MAX - 1) { |
| 194 | pr_warn("cpuidle-powernv: discovered idle states more than allowed"); |
| 195 | dt_idle_states = CPUIDLE_STATE_MAX - 1; |
| 196 | } |
| 197 | |
Preeti U Murthy | 70734a7 | 2015-02-18 23:24:53 -0600 | [diff] [blame] | 198 | if (of_property_read_u32_array(power_mgt, |
| 199 | "ibm,cpu-idle-state-flags", flags, dt_idle_states)) { |
| 200 | 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^] | 201 | goto out; |
Preeti U Murthy | 70734a7 | 2015-02-18 23:24:53 -0600 | [diff] [blame] | 202 | } |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 203 | |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 204 | if (of_property_read_u32_array(power_mgt, |
| 205 | "ibm,cpu-idle-state-latencies-ns", latency_ns, |
| 206 | dt_idle_states)) { |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 207 | 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^] | 208 | goto out; |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 211 | rc = of_property_read_u32_array(power_mgt, |
| 212 | "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states); |
| 213 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 214 | for (i = 0; i < dt_idle_states; i++) { |
| 215 | |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 216 | /* |
| 217 | * Cpuidle accepts exit_latency and target_residency in us. |
| 218 | * Use default target_residency values if f/w does not expose it. |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 219 | */ |
Preeti U Murthy | 70734a7 | 2015-02-18 23:24:53 -0600 | [diff] [blame] | 220 | if (flags[i] & OPAL_PM_NAP_ENABLED) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 221 | /* Add NAP state */ |
| 222 | strcpy(powernv_states[nr_idle_states].name, "Nap"); |
| 223 | strcpy(powernv_states[nr_idle_states].desc, "Nap"); |
Daniel Lezcano | b82b6cc | 2014-11-12 16:03:50 +0100 | [diff] [blame] | 224 | powernv_states[nr_idle_states].flags = 0; |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 225 | powernv_states[nr_idle_states].target_residency = 100; |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 226 | powernv_states[nr_idle_states].enter = nap_loop; |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come |
| 231 | * within this config dependency check. |
| 232 | */ |
| 233 | #ifdef CONFIG_TICK_ONESHOT |
| 234 | if (flags[i] & OPAL_PM_SLEEP_ENABLED || |
Preeti U Murthy | 70734a7 | 2015-02-18 23:24:53 -0600 | [diff] [blame] | 235 | flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 236 | /* Add FASTSLEEP state */ |
| 237 | strcpy(powernv_states[nr_idle_states].name, "FastSleep"); |
| 238 | strcpy(powernv_states[nr_idle_states].desc, "FastSleep"); |
Daniel Lezcano | b82b6cc | 2014-11-12 16:03:50 +0100 | [diff] [blame] | 239 | powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP; |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 240 | powernv_states[nr_idle_states].target_residency = 300000; |
Shreyas B. Prabhu | 957efce | 2016-07-08 11:50:51 +0530 | [diff] [blame^] | 241 | powernv_states[nr_idle_states].enter = fastsleep_loop; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 242 | } |
preeti | cc5a2f7 | 2015-06-24 01:48:01 -0500 | [diff] [blame] | 243 | #endif |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 244 | powernv_states[nr_idle_states].exit_latency = |
| 245 | ((unsigned int)latency_ns[i]) / 1000; |
| 246 | |
| 247 | if (!rc) { |
| 248 | powernv_states[nr_idle_states].target_residency = |
| 249 | ((unsigned int)residency_ns[i]) / 1000; |
| 250 | } |
| 251 | |
| 252 | nr_idle_states++; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 253 | } |
Preeti U Murthy | 92c83ff5 | 2015-02-18 06:34:17 +0100 | [diff] [blame] | 254 | out: |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 255 | return nr_idle_states; |
| 256 | } |
| 257 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 258 | /* |
| 259 | * powernv_idle_probe() |
| 260 | * Choose state table for shared versus dedicated partition |
| 261 | */ |
| 262 | static int powernv_idle_probe(void) |
| 263 | { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 264 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 265 | return -ENODEV; |
| 266 | |
Stewart Smith | e4d54f7 | 2015-12-09 17:18:20 +1100 | [diff] [blame] | 267 | if (firmware_has_feature(FW_FEATURE_OPAL)) { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 268 | cpuidle_state_table = powernv_states; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 269 | /* Device tree can indicate more idle states */ |
| 270 | max_idle_state = powernv_add_idle_states(); |
Shilpasri G Bhat | 78eaa10 | 2015-06-18 16:53:11 +0530 | [diff] [blame] | 271 | if (max_idle_state > 1) { |
| 272 | snooze_timeout_en = true; |
| 273 | snooze_timeout = powernv_states[1].target_residency * |
| 274 | tb_ticks_per_usec; |
| 275 | } |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 276 | } else |
| 277 | return -ENODEV; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int __init powernv_processor_idle_init(void) |
| 283 | { |
| 284 | int retval; |
| 285 | |
| 286 | retval = powernv_idle_probe(); |
| 287 | if (retval) |
| 288 | return retval; |
| 289 | |
| 290 | powernv_cpuidle_driver_init(); |
| 291 | retval = cpuidle_register(&powernv_idle_driver, NULL); |
| 292 | if (retval) { |
| 293 | printk(KERN_DEBUG "Registration of powernv driver failed.\n"); |
| 294 | return retval; |
| 295 | } |
| 296 | |
| 297 | register_cpu_notifier(&setup_hotplug_notifier); |
| 298 | printk(KERN_DEBUG "powernv_idle_driver registered\n"); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | device_initcall(powernv_processor_idle_init); |