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> |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 16 | |
| 17 | #include <asm/machdep.h> |
| 18 | #include <asm/firmware.h> |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 19 | #include <asm/runlatch.h> |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 20 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 21 | /* Flags and constants used in PowerNV platform */ |
| 22 | |
| 23 | #define MAX_POWERNV_IDLE_STATES 8 |
| 24 | #define IDLE_USE_INST_NAP 0x00010000 /* Use nap instruction */ |
| 25 | #define IDLE_USE_INST_SLEEP 0x00020000 /* Use sleep instruction */ |
| 26 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 27 | struct cpuidle_driver powernv_idle_driver = { |
| 28 | .name = "powernv_idle", |
| 29 | .owner = THIS_MODULE, |
| 30 | }; |
| 31 | |
| 32 | static int max_idle_state; |
| 33 | static struct cpuidle_state *cpuidle_state_table; |
| 34 | |
| 35 | static int snooze_loop(struct cpuidle_device *dev, |
| 36 | struct cpuidle_driver *drv, |
| 37 | int index) |
| 38 | { |
| 39 | local_irq_enable(); |
| 40 | set_thread_flag(TIF_POLLING_NRFLAG); |
| 41 | |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 42 | ppc64_runlatch_off(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 43 | while (!need_resched()) { |
| 44 | HMT_low(); |
| 45 | HMT_very_low(); |
| 46 | } |
| 47 | |
| 48 | HMT_medium(); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 49 | ppc64_runlatch_on(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 50 | clear_thread_flag(TIF_POLLING_NRFLAG); |
| 51 | smp_mb(); |
| 52 | return index; |
| 53 | } |
| 54 | |
| 55 | static int nap_loop(struct cpuidle_device *dev, |
| 56 | struct cpuidle_driver *drv, |
| 57 | int index) |
| 58 | { |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 59 | ppc64_runlatch_off(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 60 | power7_idle(); |
Nicolas Pitre | 591ac0c | 2014-02-17 10:59:29 -0500 | [diff] [blame] | 61 | ppc64_runlatch_on(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 62 | return index; |
| 63 | } |
| 64 | |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 65 | static int fastsleep_loop(struct cpuidle_device *dev, |
| 66 | struct cpuidle_driver *drv, |
| 67 | int index) |
| 68 | { |
| 69 | unsigned long old_lpcr = mfspr(SPRN_LPCR); |
| 70 | unsigned long new_lpcr; |
| 71 | |
| 72 | if (unlikely(system_state < SYSTEM_RUNNING)) |
| 73 | return index; |
| 74 | |
| 75 | new_lpcr = old_lpcr; |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 76 | /* Do not exit powersave upon decrementer as we've setup the timer |
| 77 | * offload. |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 78 | */ |
Michael Neuling | 9b6a68d | 2014-06-11 15:59:27 +1000 | [diff] [blame] | 79 | new_lpcr &= ~LPCR_PECE1; |
Preeti U Murthy | 0d94873 | 2014-02-26 05:39:06 +0530 | [diff] [blame] | 80 | |
| 81 | mtspr(SPRN_LPCR, new_lpcr); |
| 82 | power7_sleep(); |
| 83 | |
| 84 | mtspr(SPRN_LPCR, old_lpcr); |
| 85 | |
| 86 | return index; |
| 87 | } |
| 88 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 89 | /* |
| 90 | * States for dedicated partition case. |
| 91 | */ |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 92 | static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 93 | { /* Snooze */ |
| 94 | .name = "snooze", |
| 95 | .desc = "snooze", |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 96 | .exit_latency = 0, |
| 97 | .target_residency = 0, |
| 98 | .enter = &snooze_loop }, |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n, |
| 102 | unsigned long action, void *hcpu) |
| 103 | { |
| 104 | int hotcpu = (unsigned long)hcpu; |
| 105 | struct cpuidle_device *dev = |
| 106 | per_cpu(cpuidle_devices, hotcpu); |
| 107 | |
| 108 | if (dev && cpuidle_get_driver()) { |
| 109 | switch (action) { |
| 110 | case CPU_ONLINE: |
| 111 | case CPU_ONLINE_FROZEN: |
| 112 | cpuidle_pause_and_lock(); |
| 113 | cpuidle_enable_device(dev); |
| 114 | cpuidle_resume_and_unlock(); |
| 115 | break; |
| 116 | |
| 117 | case CPU_DEAD: |
| 118 | case CPU_DEAD_FROZEN: |
| 119 | cpuidle_pause_and_lock(); |
| 120 | cpuidle_disable_device(dev); |
| 121 | cpuidle_resume_and_unlock(); |
| 122 | break; |
| 123 | |
| 124 | default: |
| 125 | return NOTIFY_DONE; |
| 126 | } |
| 127 | } |
| 128 | return NOTIFY_OK; |
| 129 | } |
| 130 | |
| 131 | static struct notifier_block setup_hotplug_notifier = { |
| 132 | .notifier_call = powernv_cpuidle_add_cpu_notifier, |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * powernv_cpuidle_driver_init() |
| 137 | */ |
| 138 | static int powernv_cpuidle_driver_init(void) |
| 139 | { |
| 140 | int idle_state; |
| 141 | struct cpuidle_driver *drv = &powernv_idle_driver; |
| 142 | |
| 143 | drv->state_count = 0; |
| 144 | |
| 145 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
| 146 | /* Is the state not enabled? */ |
| 147 | if (cpuidle_state_table[idle_state].enter == NULL) |
| 148 | continue; |
| 149 | |
| 150 | drv->states[drv->state_count] = /* structure copy */ |
| 151 | cpuidle_state_table[idle_state]; |
| 152 | |
| 153 | drv->state_count += 1; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 159 | static int powernv_add_idle_states(void) |
| 160 | { |
| 161 | struct device_node *power_mgt; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 162 | int nr_idle_states = 1; /* Snooze */ |
| 163 | int dt_idle_states; |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 164 | const __be32 *idle_state_flags; |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 165 | const __be32 *idle_state_latency; |
| 166 | u32 len_flags, flags, latency_ns; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 167 | int i; |
| 168 | |
| 169 | /* Currently we have snooze statically defined */ |
| 170 | |
| 171 | power_mgt = of_find_node_by_path("/ibm,opal/power-mgt"); |
| 172 | if (!power_mgt) { |
| 173 | pr_warn("opal: PowerMgmt Node not found\n"); |
| 174 | return nr_idle_states; |
| 175 | } |
| 176 | |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 177 | idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags); |
| 178 | if (!idle_state_flags) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 179 | pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n"); |
| 180 | return nr_idle_states; |
| 181 | } |
| 182 | |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 183 | idle_state_latency = of_get_property(power_mgt, |
| 184 | "ibm,cpu-idle-state-latencies-ns", NULL); |
| 185 | if (!idle_state_latency) { |
| 186 | pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n"); |
| 187 | return nr_idle_states; |
| 188 | } |
| 189 | |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 190 | dt_idle_states = len_flags / sizeof(u32); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 191 | |
| 192 | for (i = 0; i < dt_idle_states; i++) { |
| 193 | |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 194 | flags = be32_to_cpu(idle_state_flags[i]); |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 195 | |
| 196 | /* Cpuidle accepts exit_latency in us and we estimate |
| 197 | * target residency to be 10x exit_latency |
| 198 | */ |
| 199 | latency_ns = be32_to_cpu(idle_state_latency[i]); |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 200 | if (flags & IDLE_USE_INST_NAP) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 201 | /* Add NAP state */ |
| 202 | strcpy(powernv_states[nr_idle_states].name, "Nap"); |
| 203 | strcpy(powernv_states[nr_idle_states].desc, "Nap"); |
Daniel Lezcano | b82b6cc | 2014-11-12 16:03:50 +0100 | [diff] [blame] | 204 | powernv_states[nr_idle_states].flags = 0; |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 205 | powernv_states[nr_idle_states].exit_latency = |
| 206 | ((unsigned int)latency_ns) / 1000; |
| 207 | powernv_states[nr_idle_states].target_residency = |
| 208 | ((unsigned int)latency_ns / 100); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 209 | powernv_states[nr_idle_states].enter = &nap_loop; |
| 210 | nr_idle_states++; |
| 211 | } |
| 212 | |
Vaidyanathan Srinivasan | 95707d8 | 2014-08-03 13:23:08 +0530 | [diff] [blame] | 213 | if (flags & IDLE_USE_INST_SLEEP) { |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 214 | /* Add FASTSLEEP state */ |
| 215 | strcpy(powernv_states[nr_idle_states].name, "FastSleep"); |
| 216 | strcpy(powernv_states[nr_idle_states].desc, "FastSleep"); |
Daniel Lezcano | b82b6cc | 2014-11-12 16:03:50 +0100 | [diff] [blame] | 217 | powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP; |
Preeti U. Murthy | 74aa51b | 2014-10-14 13:23:00 +0530 | [diff] [blame] | 218 | powernv_states[nr_idle_states].exit_latency = |
| 219 | ((unsigned int)latency_ns) / 1000; |
| 220 | powernv_states[nr_idle_states].target_residency = |
| 221 | ((unsigned int)latency_ns / 100); |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 222 | powernv_states[nr_idle_states].enter = &fastsleep_loop; |
| 223 | nr_idle_states++; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return nr_idle_states; |
| 228 | } |
| 229 | |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 230 | /* |
| 231 | * powernv_idle_probe() |
| 232 | * Choose state table for shared versus dedicated partition |
| 233 | */ |
| 234 | static int powernv_idle_probe(void) |
| 235 | { |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 236 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
| 237 | return -ENODEV; |
| 238 | |
| 239 | if (firmware_has_feature(FW_FEATURE_OPALv3)) { |
| 240 | cpuidle_state_table = powernv_states; |
Preeti U Murthy | 0888839 | 2014-02-26 05:39:20 +0530 | [diff] [blame] | 241 | /* Device tree can indicate more idle states */ |
| 242 | max_idle_state = powernv_add_idle_states(); |
Deepthi Dharwar | 2c2e6ec | 2014-01-14 16:32:40 +0530 | [diff] [blame] | 243 | } else |
| 244 | return -ENODEV; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int __init powernv_processor_idle_init(void) |
| 250 | { |
| 251 | int retval; |
| 252 | |
| 253 | retval = powernv_idle_probe(); |
| 254 | if (retval) |
| 255 | return retval; |
| 256 | |
| 257 | powernv_cpuidle_driver_init(); |
| 258 | retval = cpuidle_register(&powernv_idle_driver, NULL); |
| 259 | if (retval) { |
| 260 | printk(KERN_DEBUG "Registration of powernv driver failed.\n"); |
| 261 | return retval; |
| 262 | } |
| 263 | |
| 264 | register_cpu_notifier(&setup_hotplug_notifier); |
| 265 | printk(KERN_DEBUG "powernv_idle_driver registered\n"); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | device_initcall(powernv_processor_idle_init); |