]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/time/tick-broadcast.c
x86, clockevents: add C1E aware idle function
[mirror_ubuntu-artful-kernel.git] / kernel / time / tick-broadcast.c
CommitLineData
f8381cba
TG
1/*
2 * linux/kernel/time/tick-broadcast.c
3 *
4 * This file contains functions which emulate a local clock-event
5 * device via a broadcast event source.
6 *
7 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
8 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
9 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10 *
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
13 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
d7b90689 17#include <linux/interrupt.h>
f8381cba
TG
18#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
21#include <linux/tick.h>
22
23#include "tick-internal.h"
24
25/*
26 * Broadcast support for broken x86 hardware, where the local apic
27 * timer stops in C3 state.
28 */
29
30struct tick_device tick_broadcast_device;
31static cpumask_t tick_broadcast_mask;
79bf2bb3 32static DEFINE_SPINLOCK(tick_broadcast_lock);
aa276e1c 33static int tick_broadcast_force;
f8381cba 34
5590a536
TG
35#ifdef CONFIG_TICK_ONESHOT
36static void tick_broadcast_clear_oneshot(int cpu);
37#else
38static inline void tick_broadcast_clear_oneshot(int cpu) { }
39#endif
40
289f480a
IM
41/*
42 * Debugging: see timer_list.c
43 */
44struct tick_device *tick_get_broadcast_device(void)
45{
46 return &tick_broadcast_device;
47}
48
49cpumask_t *tick_get_broadcast_mask(void)
50{
51 return &tick_broadcast_mask;
52}
53
f8381cba
TG
54/*
55 * Start the device in periodic mode
56 */
57static void tick_broadcast_start_periodic(struct clock_event_device *bc)
58{
18de5bc4 59 if (bc)
f8381cba
TG
60 tick_setup_periodic(bc, 1);
61}
62
63/*
64 * Check, if the device can be utilized as broadcast device:
65 */
66int tick_check_broadcast_device(struct clock_event_device *dev)
67{
4a93232d
VP
68 if ((tick_broadcast_device.evtdev &&
69 tick_broadcast_device.evtdev->rating >= dev->rating) ||
70 (dev->features & CLOCK_EVT_FEAT_C3STOP))
f8381cba
TG
71 return 0;
72
73 clockevents_exchange_device(NULL, dev);
74 tick_broadcast_device.evtdev = dev;
75 if (!cpus_empty(tick_broadcast_mask))
76 tick_broadcast_start_periodic(dev);
77 return 1;
78}
79
80/*
81 * Check, if the device is the broadcast device
82 */
83int tick_is_broadcast_device(struct clock_event_device *dev)
84{
85 return (dev && tick_broadcast_device.evtdev == dev);
86}
87
88/*
89 * Check, if the device is disfunctional and a place holder, which
90 * needs to be handled by the broadcast device.
91 */
92int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
93{
94 unsigned long flags;
95 int ret = 0;
96
97 spin_lock_irqsave(&tick_broadcast_lock, flags);
98
99 /*
100 * Devices might be registered with both periodic and oneshot
101 * mode disabled. This signals, that the device needs to be
102 * operated from the broadcast device and is a placeholder for
103 * the cpu local device.
104 */
105 if (!tick_device_is_functional(dev)) {
106 dev->event_handler = tick_handle_periodic;
107 cpu_set(cpu, tick_broadcast_mask);
108 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
109 ret = 1;
5590a536
TG
110 } else {
111 /*
112 * When the new device is not affected by the stop
113 * feature and the cpu is marked in the broadcast mask
114 * then clear the broadcast bit.
115 */
116 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
117 int cpu = smp_processor_id();
f8381cba 118
5590a536
TG
119 cpu_clear(cpu, tick_broadcast_mask);
120 tick_broadcast_clear_oneshot(cpu);
121 }
122 }
f8381cba
TG
123 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
124 return ret;
125}
126
127/*
128 * Broadcast the event to the cpus, which are set in the mask
129 */
186e3cb8 130static void tick_do_broadcast(cpumask_t mask)
f8381cba 131{
186e3cb8 132 int cpu = smp_processor_id();
f8381cba
TG
133 struct tick_device *td;
134
135 /*
136 * Check, if the current cpu is in the mask
137 */
138 if (cpu_isset(cpu, mask)) {
139 cpu_clear(cpu, mask);
140 td = &per_cpu(tick_cpu_device, cpu);
141 td->evtdev->event_handler(td->evtdev);
f8381cba
TG
142 }
143
144 if (!cpus_empty(mask)) {
145 /*
146 * It might be necessary to actually check whether the devices
147 * have different broadcast functions. For now, just use the
148 * one of the first device. This works as long as we have this
149 * misfeature only on x86 (lapic)
150 */
151 cpu = first_cpu(mask);
152 td = &per_cpu(tick_cpu_device, cpu);
153 td->evtdev->broadcast(mask);
f8381cba 154 }
f8381cba
TG
155}
156
157/*
158 * Periodic broadcast:
159 * - invoke the broadcast handlers
160 */
161static void tick_do_periodic_broadcast(void)
162{
163 cpumask_t mask;
164
165 spin_lock(&tick_broadcast_lock);
166
167 cpus_and(mask, cpu_online_map, tick_broadcast_mask);
168 tick_do_broadcast(mask);
169
170 spin_unlock(&tick_broadcast_lock);
171}
172
173/*
174 * Event handler for periodic broadcast ticks
175 */
176static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
177{
f8381cba
TG
178 tick_do_periodic_broadcast();
179
180 /*
181 * The device is in periodic mode. No reprogramming necessary:
182 */
183 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
184 return;
185
186 /*
187 * Setup the next period for devices, which do not have
188 * periodic mode:
189 */
190 for (;;) {
191 ktime_t next = ktime_add(dev->next_event, tick_period);
192
193 if (!clockevents_program_event(dev, next, ktime_get()))
194 return;
195 tick_do_periodic_broadcast();
196 }
197}
198
199/*
200 * Powerstate information: The system enters/leaves a state, where
201 * affected devices might stop
202 */
203static void tick_do_broadcast_on_off(void *why)
204{
205 struct clock_event_device *bc, *dev;
206 struct tick_device *td;
207 unsigned long flags, *reason = why;
208 int cpu;
209
210 spin_lock_irqsave(&tick_broadcast_lock, flags);
211
212 cpu = smp_processor_id();
213 td = &per_cpu(tick_cpu_device, cpu);
214 dev = td->evtdev;
215 bc = tick_broadcast_device.evtdev;
216
217 /*
1595f452 218 * Is the device not affected by the powerstate ?
f8381cba 219 */
1595f452 220 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
f8381cba
TG
221 goto out;
222
3dfbc884
TG
223 if (!tick_device_is_functional(dev))
224 goto out;
1595f452
TG
225
226 switch (*reason) {
227 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
228 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
f8381cba
TG
229 if (!cpu_isset(cpu, tick_broadcast_mask)) {
230 cpu_set(cpu, tick_broadcast_mask);
231 if (td->mode == TICKDEV_MODE_PERIODIC)
232 clockevents_set_mode(dev,
233 CLOCK_EVT_MODE_SHUTDOWN);
234 }
3dfbc884 235 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
aa276e1c 236 tick_broadcast_force = 1;
1595f452
TG
237 break;
238 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
aa276e1c
TG
239 if (!tick_broadcast_force &&
240 cpu_isset(cpu, tick_broadcast_mask)) {
f8381cba
TG
241 cpu_clear(cpu, tick_broadcast_mask);
242 if (td->mode == TICKDEV_MODE_PERIODIC)
243 tick_setup_periodic(dev, 0);
244 }
1595f452 245 break;
f8381cba
TG
246 }
247
248 if (cpus_empty(tick_broadcast_mask))
249 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
250 else {
251 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
252 tick_broadcast_start_periodic(bc);
79bf2bb3
TG
253 else
254 tick_broadcast_setup_oneshot(bc);
f8381cba
TG
255 }
256out:
257 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
258}
259
260/*
261 * Powerstate information: The system enters/leaves a state, where
262 * affected devices might stop.
263 */
264void tick_broadcast_on_off(unsigned long reason, int *oncpu)
265{
bf020cb7 266 if (!cpu_isset(*oncpu, cpu_online_map))
833df317 267 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
72fcde96 268 "offline CPU #%d\n", *oncpu);
bf020cb7
AK
269 else
270 smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
271 &reason, 1, 1);
f8381cba
TG
272}
273
274/*
275 * Set the periodic handler depending on broadcast on/off
276 */
277void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
278{
279 if (!broadcast)
280 dev->event_handler = tick_handle_periodic;
281 else
282 dev->event_handler = tick_handle_periodic_broadcast;
283}
284
285/*
286 * Remove a CPU from broadcasting
287 */
288void tick_shutdown_broadcast(unsigned int *cpup)
289{
290 struct clock_event_device *bc;
291 unsigned long flags;
292 unsigned int cpu = *cpup;
293
294 spin_lock_irqsave(&tick_broadcast_lock, flags);
295
296 bc = tick_broadcast_device.evtdev;
297 cpu_clear(cpu, tick_broadcast_mask);
298
299 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
300 if (bc && cpus_empty(tick_broadcast_mask))
301 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
302 }
303
304 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
305}
79bf2bb3 306
6321dd60
TG
307void tick_suspend_broadcast(void)
308{
309 struct clock_event_device *bc;
310 unsigned long flags;
311
312 spin_lock_irqsave(&tick_broadcast_lock, flags);
313
314 bc = tick_broadcast_device.evtdev;
18de5bc4 315 if (bc)
6321dd60
TG
316 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
317
318 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
319}
320
321int tick_resume_broadcast(void)
322{
323 struct clock_event_device *bc;
324 unsigned long flags;
325 int broadcast = 0;
326
327 spin_lock_irqsave(&tick_broadcast_lock, flags);
328
329 bc = tick_broadcast_device.evtdev;
6321dd60 330
cd05a1f8 331 if (bc) {
18de5bc4
TG
332 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
333
cd05a1f8
TG
334 switch (tick_broadcast_device.mode) {
335 case TICKDEV_MODE_PERIODIC:
336 if(!cpus_empty(tick_broadcast_mask))
337 tick_broadcast_start_periodic(bc);
338 broadcast = cpu_isset(smp_processor_id(),
339 tick_broadcast_mask);
340 break;
341 case TICKDEV_MODE_ONESHOT:
342 broadcast = tick_resume_broadcast_oneshot(bc);
343 break;
344 }
6321dd60
TG
345 }
346 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
347
348 return broadcast;
349}
350
351
79bf2bb3
TG
352#ifdef CONFIG_TICK_ONESHOT
353
354static cpumask_t tick_broadcast_oneshot_mask;
355
289f480a
IM
356/*
357 * Debugging: see timer_list.c
358 */
359cpumask_t *tick_get_broadcast_oneshot_mask(void)
360{
361 return &tick_broadcast_oneshot_mask;
362}
363
79bf2bb3
TG
364static int tick_broadcast_set_event(ktime_t expires, int force)
365{
366 struct clock_event_device *bc = tick_broadcast_device.evtdev;
367 ktime_t now = ktime_get();
368 int res;
369
370 for(;;) {
371 res = clockevents_program_event(bc, expires, now);
372 if (!res || !force)
373 return res;
374 now = ktime_get();
375 expires = ktime_add(now, ktime_set(0, bc->min_delta_ns));
376 }
377}
378
cd05a1f8
TG
379int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
380{
381 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
b7e113dc 382 return 0;
cd05a1f8
TG
383}
384
79bf2bb3
TG
385/*
386 * Handle oneshot mode broadcasting
387 */
388static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
389{
390 struct tick_device *td;
391 cpumask_t mask;
cdc6f27d 392 ktime_t now, next_event;
79bf2bb3
TG
393 int cpu;
394
395 spin_lock(&tick_broadcast_lock);
396again:
397 dev->next_event.tv64 = KTIME_MAX;
cdc6f27d 398 next_event.tv64 = KTIME_MAX;
79bf2bb3
TG
399 mask = CPU_MASK_NONE;
400 now = ktime_get();
401 /* Find all expired events */
402 for (cpu = first_cpu(tick_broadcast_oneshot_mask); cpu != NR_CPUS;
403 cpu = next_cpu(cpu, tick_broadcast_oneshot_mask)) {
404 td = &per_cpu(tick_cpu_device, cpu);
405 if (td->evtdev->next_event.tv64 <= now.tv64)
406 cpu_set(cpu, mask);
cdc6f27d
TG
407 else if (td->evtdev->next_event.tv64 < next_event.tv64)
408 next_event.tv64 = td->evtdev->next_event.tv64;
79bf2bb3
TG
409 }
410
411 /*
cdc6f27d
TG
412 * Wakeup the cpus which have an expired event.
413 */
414 tick_do_broadcast(mask);
415
416 /*
417 * Two reasons for reprogram:
418 *
419 * - The global event did not expire any CPU local
420 * events. This happens in dyntick mode, as the maximum PIT
421 * delta is quite small.
422 *
423 * - There are pending events on sleeping CPUs which were not
424 * in the event mask
79bf2bb3 425 */
cdc6f27d 426 if (next_event.tv64 != KTIME_MAX) {
79bf2bb3 427 /*
cdc6f27d
TG
428 * Rearm the broadcast device. If event expired,
429 * repeat the above
79bf2bb3 430 */
cdc6f27d 431 if (tick_broadcast_set_event(next_event, 0))
79bf2bb3
TG
432 goto again;
433 }
434 spin_unlock(&tick_broadcast_lock);
435}
436
437/*
438 * Powerstate information: The system enters/leaves a state, where
439 * affected devices might stop
440 */
441void tick_broadcast_oneshot_control(unsigned long reason)
442{
443 struct clock_event_device *bc, *dev;
444 struct tick_device *td;
445 unsigned long flags;
446 int cpu;
447
448 spin_lock_irqsave(&tick_broadcast_lock, flags);
449
450 /*
451 * Periodic mode does not care about the enter/exit of power
452 * states
453 */
454 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
455 goto out;
456
457 bc = tick_broadcast_device.evtdev;
458 cpu = smp_processor_id();
459 td = &per_cpu(tick_cpu_device, cpu);
460 dev = td->evtdev;
461
462 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
463 goto out;
464
465 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
466 if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
467 cpu_set(cpu, tick_broadcast_oneshot_mask);
468 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
469 if (dev->next_event.tv64 < bc->next_event.tv64)
470 tick_broadcast_set_event(dev->next_event, 1);
471 }
472 } else {
473 if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
474 cpu_clear(cpu, tick_broadcast_oneshot_mask);
475 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
476 if (dev->next_event.tv64 != KTIME_MAX)
477 tick_program_event(dev->next_event, 1);
478 }
479 }
480
481out:
482 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
483}
484
5590a536
TG
485/*
486 * Reset the one shot broadcast for a cpu
487 *
488 * Called with tick_broadcast_lock held
489 */
490static void tick_broadcast_clear_oneshot(int cpu)
491{
492 cpu_clear(cpu, tick_broadcast_oneshot_mask);
493}
494
79bf2bb3 495/**
8dce39c2 496 * tick_broadcast_setup_oneshot - setup the broadcast device
79bf2bb3
TG
497 */
498void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
499{
4a93232d
VP
500 bc->event_handler = tick_handle_oneshot_broadcast;
501 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
502 bc->next_event.tv64 = KTIME_MAX;
79bf2bb3
TG
503}
504
505/*
506 * Select oneshot operating mode for the broadcast device
507 */
508void tick_broadcast_switch_to_oneshot(void)
509{
510 struct clock_event_device *bc;
511 unsigned long flags;
512
513 spin_lock_irqsave(&tick_broadcast_lock, flags);
514
515 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
516 bc = tick_broadcast_device.evtdev;
517 if (bc)
518 tick_broadcast_setup_oneshot(bc);
519 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
520}
521
522
523/*
524 * Remove a dead CPU from broadcasting
525 */
526void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
527{
79bf2bb3
TG
528 unsigned long flags;
529 unsigned int cpu = *cpup;
530
531 spin_lock_irqsave(&tick_broadcast_lock, flags);
532
31d9b393
TG
533 /*
534 * Clear the broadcast mask flag for the dead cpu, but do not
535 * stop the broadcast device!
536 */
79bf2bb3
TG
537 cpu_clear(cpu, tick_broadcast_oneshot_mask);
538
79bf2bb3
TG
539 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
540}
541
542#endif