2 * linux/kernel/time/tick-broadcast.c
4 * This file contains functions which emulate a local clock-event
5 * device via a broadcast event source.
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
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
14 #include <linux/cpu.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/irq.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/tick.h>
23 #include "tick-internal.h"
26 * Broadcast support for broken x86 hardware, where the local apic
27 * timer stops in C3 state.
30 struct tick_device tick_broadcast_device
;
31 static cpumask_t tick_broadcast_mask
;
32 static DEFINE_SPINLOCK(tick_broadcast_lock
);
34 #ifdef CONFIG_TICK_ONESHOT
35 static void tick_broadcast_clear_oneshot(int cpu
);
37 static inline void tick_broadcast_clear_oneshot(int cpu
) { }
41 * Debugging: see timer_list.c
43 struct tick_device
*tick_get_broadcast_device(void)
45 return &tick_broadcast_device
;
48 cpumask_t
*tick_get_broadcast_mask(void)
50 return &tick_broadcast_mask
;
54 * Start the device in periodic mode
56 static void tick_broadcast_start_periodic(struct clock_event_device
*bc
)
59 tick_setup_periodic(bc
, 1);
63 * Check, if the device can be utilized as broadcast device:
65 int tick_check_broadcast_device(struct clock_event_device
*dev
)
67 if (tick_broadcast_device
.evtdev
||
68 (dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
71 clockevents_exchange_device(NULL
, dev
);
72 tick_broadcast_device
.evtdev
= dev
;
73 if (!cpus_empty(tick_broadcast_mask
))
74 tick_broadcast_start_periodic(dev
);
79 * Check, if the device is the broadcast device
81 int tick_is_broadcast_device(struct clock_event_device
*dev
)
83 return (dev
&& tick_broadcast_device
.evtdev
== dev
);
87 * Check, if the device is disfunctional and a place holder, which
88 * needs to be handled by the broadcast device.
90 int tick_device_uses_broadcast(struct clock_event_device
*dev
, int cpu
)
95 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
98 * Devices might be registered with both periodic and oneshot
99 * mode disabled. This signals, that the device needs to be
100 * operated from the broadcast device and is a placeholder for
101 * the cpu local device.
103 if (!tick_device_is_functional(dev
)) {
104 dev
->event_handler
= tick_handle_periodic
;
105 cpu_set(cpu
, tick_broadcast_mask
);
106 tick_broadcast_start_periodic(tick_broadcast_device
.evtdev
);
110 * When the new device is not affected by the stop
111 * feature and the cpu is marked in the broadcast mask
112 * then clear the broadcast bit.
114 if (!(dev
->features
& CLOCK_EVT_FEAT_C3STOP
)) {
115 int cpu
= smp_processor_id();
117 cpu_clear(cpu
, tick_broadcast_mask
);
118 tick_broadcast_clear_oneshot(cpu
);
121 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
126 * Broadcast the event to the cpus, which are set in the mask
128 int tick_do_broadcast(cpumask_t mask
)
130 int ret
= 0, cpu
= smp_processor_id();
131 struct tick_device
*td
;
134 * Check, if the current cpu is in the mask
136 if (cpu_isset(cpu
, mask
)) {
137 cpu_clear(cpu
, mask
);
138 td
= &per_cpu(tick_cpu_device
, cpu
);
139 td
->evtdev
->event_handler(td
->evtdev
);
143 if (!cpus_empty(mask
)) {
145 * It might be necessary to actually check whether the devices
146 * have different broadcast functions. For now, just use the
147 * one of the first device. This works as long as we have this
148 * misfeature only on x86 (lapic)
150 cpu
= first_cpu(mask
);
151 td
= &per_cpu(tick_cpu_device
, cpu
);
152 td
->evtdev
->broadcast(mask
);
159 * Periodic broadcast:
160 * - invoke the broadcast handlers
162 static void tick_do_periodic_broadcast(void)
166 spin_lock(&tick_broadcast_lock
);
168 cpus_and(mask
, cpu_online_map
, tick_broadcast_mask
);
169 tick_do_broadcast(mask
);
171 spin_unlock(&tick_broadcast_lock
);
175 * Event handler for periodic broadcast ticks
177 static void tick_handle_periodic_broadcast(struct clock_event_device
*dev
)
179 dev
->next_event
.tv64
= KTIME_MAX
;
181 tick_do_periodic_broadcast();
184 * The device is in periodic mode. No reprogramming necessary:
186 if (dev
->mode
== CLOCK_EVT_MODE_PERIODIC
)
190 * Setup the next period for devices, which do not have
194 ktime_t next
= ktime_add(dev
->next_event
, tick_period
);
196 if (!clockevents_program_event(dev
, next
, ktime_get()))
198 tick_do_periodic_broadcast();
203 * Powerstate information: The system enters/leaves a state, where
204 * affected devices might stop
206 static void tick_do_broadcast_on_off(void *why
)
208 struct clock_event_device
*bc
, *dev
;
209 struct tick_device
*td
;
210 unsigned long flags
, *reason
= why
;
213 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
215 cpu
= smp_processor_id();
216 td
= &per_cpu(tick_cpu_device
, cpu
);
218 bc
= tick_broadcast_device
.evtdev
;
221 * Is the device in broadcast mode forever or is it not
222 * affected by the powerstate ?
224 if (!dev
|| !tick_device_is_functional(dev
) ||
225 !(dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
228 if (*reason
== CLOCK_EVT_NOTIFY_BROADCAST_ON
) {
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
);
236 if (cpu_isset(cpu
, tick_broadcast_mask
)) {
237 cpu_clear(cpu
, tick_broadcast_mask
);
238 if (td
->mode
== TICKDEV_MODE_PERIODIC
)
239 tick_setup_periodic(dev
, 0);
243 if (cpus_empty(tick_broadcast_mask
))
244 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
246 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
)
247 tick_broadcast_start_periodic(bc
);
249 tick_broadcast_setup_oneshot(bc
);
252 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
256 * Powerstate information: The system enters/leaves a state, where
257 * affected devices might stop.
259 void tick_broadcast_on_off(unsigned long reason
, int *oncpu
)
263 if (!cpu_isset(*oncpu
, cpu_online_map
)) {
264 printk(KERN_ERR
"tick-braodcast: ignoring broadcast for "
265 "offline CPU #%d\n", *oncpu
);
269 tick_do_broadcast_on_off(&reason
);
271 smp_call_function_single(*oncpu
,
272 tick_do_broadcast_on_off
,
279 * Set the periodic handler depending on broadcast on/off
281 void tick_set_periodic_handler(struct clock_event_device
*dev
, int broadcast
)
284 dev
->event_handler
= tick_handle_periodic
;
286 dev
->event_handler
= tick_handle_periodic_broadcast
;
290 * Remove a CPU from broadcasting
292 void tick_shutdown_broadcast(unsigned int *cpup
)
294 struct clock_event_device
*bc
;
296 unsigned int cpu
= *cpup
;
298 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
300 bc
= tick_broadcast_device
.evtdev
;
301 cpu_clear(cpu
, tick_broadcast_mask
);
303 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
) {
304 if (bc
&& cpus_empty(tick_broadcast_mask
))
305 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
308 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
311 void tick_suspend_broadcast(void)
313 struct clock_event_device
*bc
;
316 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
318 bc
= tick_broadcast_device
.evtdev
;
320 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
322 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
325 int tick_resume_broadcast(void)
327 struct clock_event_device
*bc
;
331 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
333 bc
= tick_broadcast_device
.evtdev
;
336 clockevents_set_mode(bc
, CLOCK_EVT_MODE_RESUME
);
338 switch (tick_broadcast_device
.mode
) {
339 case TICKDEV_MODE_PERIODIC
:
340 if(!cpus_empty(tick_broadcast_mask
))
341 tick_broadcast_start_periodic(bc
);
342 broadcast
= cpu_isset(smp_processor_id(),
343 tick_broadcast_mask
);
345 case TICKDEV_MODE_ONESHOT
:
346 broadcast
= tick_resume_broadcast_oneshot(bc
);
350 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
356 #ifdef CONFIG_TICK_ONESHOT
358 static cpumask_t tick_broadcast_oneshot_mask
;
361 * Debugging: see timer_list.c
363 cpumask_t
*tick_get_broadcast_oneshot_mask(void)
365 return &tick_broadcast_oneshot_mask
;
368 static int tick_broadcast_set_event(ktime_t expires
, int force
)
370 struct clock_event_device
*bc
= tick_broadcast_device
.evtdev
;
371 ktime_t now
= ktime_get();
375 res
= clockevents_program_event(bc
, expires
, now
);
379 expires
= ktime_add(now
, ktime_set(0, bc
->min_delta_ns
));
383 int tick_resume_broadcast_oneshot(struct clock_event_device
*bc
)
385 clockevents_set_mode(bc
, CLOCK_EVT_MODE_ONESHOT
);
390 * Reprogram the broadcast device:
392 * Called with tick_broadcast_lock held and interrupts disabled.
394 static int tick_broadcast_reprogram(void)
396 ktime_t expires
= { .tv64
= KTIME_MAX
};
397 struct tick_device
*td
;
401 * Find the event which expires next:
403 for (cpu
= first_cpu(tick_broadcast_oneshot_mask
); cpu
!= NR_CPUS
;
404 cpu
= next_cpu(cpu
, tick_broadcast_oneshot_mask
)) {
405 td
= &per_cpu(tick_cpu_device
, cpu
);
406 if (td
->evtdev
->next_event
.tv64
< expires
.tv64
)
407 expires
= td
->evtdev
->next_event
;
410 if (expires
.tv64
== KTIME_MAX
)
413 return tick_broadcast_set_event(expires
, 0);
417 * Handle oneshot mode broadcasting
419 static void tick_handle_oneshot_broadcast(struct clock_event_device
*dev
)
421 struct tick_device
*td
;
426 spin_lock(&tick_broadcast_lock
);
428 dev
->next_event
.tv64
= KTIME_MAX
;
429 mask
= CPU_MASK_NONE
;
431 /* Find all expired events */
432 for (cpu
= first_cpu(tick_broadcast_oneshot_mask
); cpu
!= NR_CPUS
;
433 cpu
= next_cpu(cpu
, tick_broadcast_oneshot_mask
)) {
434 td
= &per_cpu(tick_cpu_device
, cpu
);
435 if (td
->evtdev
->next_event
.tv64
<= now
.tv64
)
440 * Wakeup the cpus which have an expired event. The broadcast
441 * device is reprogrammed in the return from idle code.
443 if (!tick_do_broadcast(mask
)) {
445 * The global event did not expire any CPU local
446 * events. This happens in dyntick mode, as the
447 * maximum PIT delta is quite small.
449 if (tick_broadcast_reprogram())
452 spin_unlock(&tick_broadcast_lock
);
456 * Powerstate information: The system enters/leaves a state, where
457 * affected devices might stop
459 void tick_broadcast_oneshot_control(unsigned long reason
)
461 struct clock_event_device
*bc
, *dev
;
462 struct tick_device
*td
;
466 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
469 * Periodic mode does not care about the enter/exit of power
472 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
)
475 bc
= tick_broadcast_device
.evtdev
;
476 cpu
= smp_processor_id();
477 td
= &per_cpu(tick_cpu_device
, cpu
);
480 if (!(dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
483 if (reason
== CLOCK_EVT_NOTIFY_BROADCAST_ENTER
) {
484 if (!cpu_isset(cpu
, tick_broadcast_oneshot_mask
)) {
485 cpu_set(cpu
, tick_broadcast_oneshot_mask
);
486 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
487 if (dev
->next_event
.tv64
< bc
->next_event
.tv64
)
488 tick_broadcast_set_event(dev
->next_event
, 1);
491 if (cpu_isset(cpu
, tick_broadcast_oneshot_mask
)) {
492 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
493 clockevents_set_mode(dev
, CLOCK_EVT_MODE_ONESHOT
);
494 if (dev
->next_event
.tv64
!= KTIME_MAX
)
495 tick_program_event(dev
->next_event
, 1);
500 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
504 * Reset the one shot broadcast for a cpu
506 * Called with tick_broadcast_lock held
508 static void tick_broadcast_clear_oneshot(int cpu
)
510 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
514 * tick_broadcast_setup_highres - setup the broadcast device for highres
516 void tick_broadcast_setup_oneshot(struct clock_event_device
*bc
)
518 if (bc
->mode
!= CLOCK_EVT_MODE_ONESHOT
) {
519 bc
->event_handler
= tick_handle_oneshot_broadcast
;
520 clockevents_set_mode(bc
, CLOCK_EVT_MODE_ONESHOT
);
521 bc
->next_event
.tv64
= KTIME_MAX
;
526 * Select oneshot operating mode for the broadcast device
528 void tick_broadcast_switch_to_oneshot(void)
530 struct clock_event_device
*bc
;
533 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
535 tick_broadcast_device
.mode
= TICKDEV_MODE_ONESHOT
;
536 bc
= tick_broadcast_device
.evtdev
;
538 tick_broadcast_setup_oneshot(bc
);
539 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
544 * Remove a dead CPU from broadcasting
546 void tick_shutdown_broadcast_oneshot(unsigned int *cpup
)
549 unsigned int cpu
= *cpup
;
551 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
554 * Clear the broadcast mask flag for the dead cpu, but do not
555 * stop the broadcast device!
557 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
559 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);