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>
14 #include <linux/clockchips.h>
15 #include <linux/of.h>
16
17 #include <asm/machdep.h>
18 #include <asm/firmware.h>
19 #include <asm/runlatch.h>
20
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
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
snooze_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)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
42 ppc64_runlatch_off();
43 while (!need_resched()) {
44 HMT_low();
45 HMT_very_low();
46 }
47
48 HMT_medium();
49 ppc64_runlatch_on();
50 clear_thread_flag(TIF_POLLING_NRFLAG);
51 smp_mb();
52 return index;
53 }
54
nap_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)55 static int nap_loop(struct cpuidle_device *dev,
56 struct cpuidle_driver *drv,
57 int index)
58 {
59 ppc64_runlatch_off();
60 power7_idle();
61 ppc64_runlatch_on();
62 return index;
63 }
64
fastsleep_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)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;
76 /* Do not exit powersave upon decrementer as we've setup the timer
77 * offload.
78 */
79 new_lpcr &= ~LPCR_PECE1;
80
81 mtspr(SPRN_LPCR, new_lpcr);
82 power7_sleep();
83
84 mtspr(SPRN_LPCR, old_lpcr);
85
86 return index;
87 }
88
89 /*
90 * States for dedicated partition case.
91 */
92 static struct cpuidle_state powernv_states[MAX_POWERNV_IDLE_STATES] = {
93 { /* Snooze */
94 .name = "snooze",
95 .desc = "snooze",
96 .flags = CPUIDLE_FLAG_TIME_VALID,
97 .exit_latency = 0,
98 .target_residency = 0,
99 .enter = &snooze_loop },
100 };
101
powernv_cpuidle_add_cpu_notifier(struct notifier_block * n,unsigned long action,void * hcpu)102 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
103 unsigned long action, void *hcpu)
104 {
105 int hotcpu = (unsigned long)hcpu;
106 struct cpuidle_device *dev =
107 per_cpu(cpuidle_devices, hotcpu);
108
109 if (dev && cpuidle_get_driver()) {
110 switch (action) {
111 case CPU_ONLINE:
112 case CPU_ONLINE_FROZEN:
113 cpuidle_pause_and_lock();
114 cpuidle_enable_device(dev);
115 cpuidle_resume_and_unlock();
116 break;
117
118 case CPU_DEAD:
119 case CPU_DEAD_FROZEN:
120 cpuidle_pause_and_lock();
121 cpuidle_disable_device(dev);
122 cpuidle_resume_and_unlock();
123 break;
124
125 default:
126 return NOTIFY_DONE;
127 }
128 }
129 return NOTIFY_OK;
130 }
131
132 static struct notifier_block setup_hotplug_notifier = {
133 .notifier_call = powernv_cpuidle_add_cpu_notifier,
134 };
135
136 /*
137 * powernv_cpuidle_driver_init()
138 */
powernv_cpuidle_driver_init(void)139 static int powernv_cpuidle_driver_init(void)
140 {
141 int idle_state;
142 struct cpuidle_driver *drv = &powernv_idle_driver;
143
144 drv->state_count = 0;
145
146 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
147 /* Is the state not enabled? */
148 if (cpuidle_state_table[idle_state].enter == NULL)
149 continue;
150
151 drv->states[drv->state_count] = /* structure copy */
152 cpuidle_state_table[idle_state];
153
154 drv->state_count += 1;
155 }
156
157 /*
158 * On the PowerNV platform cpu_present may be less than cpu_possible in
159 * cases when firmware detects the CPU, but it is not available to the
160 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
161 * run time and hence cpu_devices are not created for those CPUs by the
162 * generic topology_init().
163 *
164 * drv->cpumask defaults to cpu_possible_mask in
165 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
166 * cpu_devices are not created for CPUs in cpu_possible_mask that
167 * cannot be hot-added later at run time.
168 *
169 * Trying cpuidle_register_device() on a CPU without a cpu_device is
170 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
171 */
172
173 drv->cpumask = (struct cpumask *)cpu_present_mask;
174
175 return 0;
176 }
177
powernv_add_idle_states(void)178 static int powernv_add_idle_states(void)
179 {
180 struct device_node *power_mgt;
181 int nr_idle_states = 1; /* Snooze */
182 int dt_idle_states;
183 const __be32 *idle_state_flags;
184 const __be32 *idle_state_latency;
185 u32 len_flags, flags, latency_ns;
186 int i;
187
188 /* Currently we have snooze statically defined */
189
190 power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
191 if (!power_mgt) {
192 pr_warn("opal: PowerMgmt Node not found\n");
193 return nr_idle_states;
194 }
195
196 idle_state_flags = of_get_property(power_mgt, "ibm,cpu-idle-state-flags", &len_flags);
197 if (!idle_state_flags) {
198 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-flags\n");
199 return nr_idle_states;
200 }
201
202 idle_state_latency = of_get_property(power_mgt,
203 "ibm,cpu-idle-state-latencies-ns", NULL);
204 if (!idle_state_latency) {
205 pr_warn("DT-PowerMgmt: missing ibm,cpu-idle-state-latencies-ns\n");
206 return nr_idle_states;
207 }
208
209 dt_idle_states = len_flags / sizeof(u32);
210
211 for (i = 0; i < dt_idle_states; i++) {
212
213 flags = be32_to_cpu(idle_state_flags[i]);
214
215 /* Cpuidle accepts exit_latency in us and we estimate
216 * target residency to be 10x exit_latency
217 */
218 latency_ns = be32_to_cpu(idle_state_latency[i]);
219 if (flags & IDLE_USE_INST_NAP) {
220 /* Add NAP state */
221 strcpy(powernv_states[nr_idle_states].name, "Nap");
222 strcpy(powernv_states[nr_idle_states].desc, "Nap");
223 powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIME_VALID;
224 powernv_states[nr_idle_states].exit_latency =
225 ((unsigned int)latency_ns) / 1000;
226 powernv_states[nr_idle_states].target_residency =
227 ((unsigned int)latency_ns / 100);
228 powernv_states[nr_idle_states].enter = &nap_loop;
229 nr_idle_states++;
230 }
231
232 if (flags & IDLE_USE_INST_SLEEP) {
233 /* Add FASTSLEEP state */
234 strcpy(powernv_states[nr_idle_states].name, "FastSleep");
235 strcpy(powernv_states[nr_idle_states].desc, "FastSleep");
236 powernv_states[nr_idle_states].flags =
237 CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
238 powernv_states[nr_idle_states].exit_latency =
239 ((unsigned int)latency_ns) / 1000;
240 powernv_states[nr_idle_states].target_residency =
241 ((unsigned int)latency_ns / 100);
242 powernv_states[nr_idle_states].enter = &fastsleep_loop;
243 nr_idle_states++;
244 }
245 }
246
247 return nr_idle_states;
248 }
249
250 /*
251 * powernv_idle_probe()
252 * Choose state table for shared versus dedicated partition
253 */
powernv_idle_probe(void)254 static int powernv_idle_probe(void)
255 {
256 if (cpuidle_disable != IDLE_NO_OVERRIDE)
257 return -ENODEV;
258
259 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
260 cpuidle_state_table = powernv_states;
261 /* Device tree can indicate more idle states */
262 max_idle_state = powernv_add_idle_states();
263 } else
264 return -ENODEV;
265
266 return 0;
267 }
268
powernv_processor_idle_init(void)269 static int __init powernv_processor_idle_init(void)
270 {
271 int retval;
272
273 retval = powernv_idle_probe();
274 if (retval)
275 return retval;
276
277 powernv_cpuidle_driver_init();
278 retval = cpuidle_register(&powernv_idle_driver, NULL);
279 if (retval) {
280 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
281 return retval;
282 }
283
284 register_cpu_notifier(&setup_hotplug_notifier);
285 printk(KERN_DEBUG "powernv_idle_driver registered\n");
286 return 0;
287 }
288
289 device_initcall(powernv_processor_idle_init);
290