blob: 719f6fb5b1c35d00108c47a61918741200eae75d [file] [log] [blame]
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +05301/*
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 Murthy0d948732014-02-26 05:39:06 +053014#include <linux/clockchips.h>
Preeti U Murthy08888392014-02-26 05:39:20 +053015#include <linux/of.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053016
17#include <asm/machdep.h>
18#include <asm/firmware.h>
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050019#include <asm/runlatch.h>
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053020
Preeti U Murthy08888392014-02-26 05:39:20 +053021/* 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 Dharwar2c2e6ec2014-01-14 16:32:40 +053027struct cpuidle_driver powernv_idle_driver = {
28 .name = "powernv_idle",
29 .owner = THIS_MODULE,
30};
31
32static int max_idle_state;
33static struct cpuidle_state *cpuidle_state_table;
34
35static 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 Pitre591ac0c2014-02-17 10:59:29 -050042 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053043 while (!need_resched()) {
44 HMT_low();
45 HMT_very_low();
46 }
47
48 HMT_medium();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050049 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053050 clear_thread_flag(TIF_POLLING_NRFLAG);
51 smp_mb();
52 return index;
53}
54
55static int nap_loop(struct cpuidle_device *dev,
56 struct cpuidle_driver *drv,
57 int index)
58{
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050059 ppc64_runlatch_off();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053060 power7_idle();
Nicolas Pitre591ac0c2014-02-17 10:59:29 -050061 ppc64_runlatch_on();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053062 return index;
63}
64
Preeti U Murthy0d948732014-02-26 05:39:06 +053065static 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;
76 new_lpcr &= ~(LPCR_MER | LPCR_PECE); /* lpcr[mer] must be 0 */
77
78 /* exit powersave upon external interrupt, but not decrementer
79 * interrupt.
80 */
81 new_lpcr |= LPCR_PECE0;
82
83 mtspr(SPRN_LPCR, new_lpcr);
84 power7_sleep();
85
86 mtspr(SPRN_LPCR, old_lpcr);
87
88 return index;
89}
90
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053091/*
92 * States for dedicated partition case.
93 */
Preeti U Murthy08888392014-02-26 05:39:20 +053094static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +053095 { /* Snooze */
96 .name = "snooze",
97 .desc = "snooze",
98 .flags = CPUIDLE_FLAG_TIME_VALID,
99 .exit_latency = 0,
100 .target_residency = 0,
101 .enter = &snooze_loop },
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530102};
103
104static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
105 unsigned long action, void *hcpu)
106{
107 int hotcpu = (unsigned long)hcpu;
108 struct cpuidle_device *dev =
109 per_cpu(cpuidle_devices, hotcpu);
110
111 if (dev && cpuidle_get_driver()) {
112 switch (action) {
113 case CPU_ONLINE:
114 case CPU_ONLINE_FROZEN:
115 cpuidle_pause_and_lock();
116 cpuidle_enable_device(dev);
117 cpuidle_resume_and_unlock();
118 break;
119
120 case CPU_DEAD:
121 case CPU_DEAD_FROZEN:
122 cpuidle_pause_and_lock();
123 cpuidle_disable_device(dev);
124 cpuidle_resume_and_unlock();
125 break;
126
127 default:
128 return NOTIFY_DONE;
129 }
130 }
131 return NOTIFY_OK;
132}
133
134static struct notifier_block setup_hotplug_notifier = {
135 .notifier_call = powernv_cpuidle_add_cpu_notifier,
136};
137
138/*
139 * powernv_cpuidle_driver_init()
140 */
141static int powernv_cpuidle_driver_init(void)
142{
143 int idle_state;
144 struct cpuidle_driver *drv = &powernv_idle_driver;
145
146 drv->state_count = 0;
147
148 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
149 /* Is the state not enabled? */
150 if (cpuidle_state_table[idle_state].enter == NULL)
151 continue;
152
153 drv->states[drv->state_count] = /* structure copy */
154 cpuidle_state_table[idle_state];
155
156 drv->state_count += 1;
157 }
158
159 return 0;
160}
161
Preeti U Murthy08888392014-02-26 05:39:20 +0530162static int powernv_add_idle_states(void)
163{
164 struct device_node *power_mgt;
165 struct property *prop;
166 int nr_idle_states = 1; /* Snooze */
167 int dt_idle_states;
168 u32 *flags;
169 int i;
170
171 /* Currently we have snooze statically defined */
172
173 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
174 if (!power_mgt) {
175 pr_warn("opal: PowerMgmt Node not found\n");
176 return nr_idle_states;
177 }
178
179 prop = of_find_property(power_mgt, "ibm,cpu-idle-state-flags", NULL);
180 if (!prop) {
181 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
182 return nr_idle_states;
183 }
184
185 dt_idle_states = prop->length / sizeof(u32);
186 flags = (u32 *) prop->value;
187
188 for (i = 0; i < dt_idle_states; i++) {
189
190 if (flags[i] & IDLE_USE_INST_NAP) {
191 /* Add NAP state */
192 strcpy(powernv_states[nr_idle_states].name, "Nap");
193 strcpy(powernv_states[nr_idle_states].desc, "Nap");
194 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
195 powernv_states[nr_idle_states].exit_latency = 10;
196 powernv_states[nr_idle_states].target_residency = 100;
197 powernv_states[nr_idle_states].enter = &nap_loop;
198 nr_idle_states++;
199 }
200
201 if (flags[i] & IDLE_USE_INST_SLEEP) {
202 /* Add FASTSLEEP state */
203 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
204 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
205 powernv_states[nr_idle_states].flags =
206 CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
207 powernv_states[nr_idle_states].exit_latency = 300;
208 powernv_states[nr_idle_states].target_residency = 1000000;
209 powernv_states[nr_idle_states].enter = &fastsleep_loop;
210 nr_idle_states++;
211 }
212 }
213
214 return nr_idle_states;
215}
216
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530217/*
218 * powernv_idle_probe()
219 * Choose state table for shared versus dedicated partition
220 */
221static int powernv_idle_probe(void)
222{
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530223 if (cpuidle_disable != IDLE_NO_OVERRIDE)
224 return -ENODEV;
225
226 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
227 cpuidle_state_table = powernv_states;
Preeti U Murthy08888392014-02-26 05:39:20 +0530228 /* Device tree can indicate more idle states */
229 max_idle_state = powernv_add_idle_states();
Deepthi Dharwar2c2e6ec2014-01-14 16:32:40 +0530230 } else
231 return -ENODEV;
232
233 return 0;
234}
235
236static int __init powernv_processor_idle_init(void)
237{
238 int retval;
239
240 retval = powernv_idle_probe();
241 if (retval)
242 return retval;
243
244 powernv_cpuidle_driver_init();
245 retval = cpuidle_register(&powernv_idle_driver, NULL);
246 if (retval) {
247 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
248 return retval;
249 }
250
251 register_cpu_notifier(&setup_hotplug_notifier);
252 printk(KERN_DEBUG "powernv_idle_driver registered\n");
253 return 0;
254}
255
256device_initcall(powernv_processor_idle_init);