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 tick_broadcast_device
.evtdev
->rating
>= dev
->rating
) ||
69 (dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
72 clockevents_exchange_device(NULL
, dev
);
73 tick_broadcast_device
.evtdev
= dev
;
74 if (!cpus_empty(tick_broadcast_mask
))
75 tick_broadcast_start_periodic(dev
);
80 * Check, if the device is the broadcast device
82 int tick_is_broadcast_device(struct clock_event_device
*dev
)
84 return (dev
&& tick_broadcast_device
.evtdev
== dev
);
88 * Check, if the device is disfunctional and a place holder, which
89 * needs to be handled by the broadcast device.
91 int tick_device_uses_broadcast(struct clock_event_device
*dev
, int cpu
)
96 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
99 * Devices might be registered with both periodic and oneshot
100 * mode disabled. This signals, that the device needs to be
101 * operated from the broadcast device and is a placeholder for
102 * the cpu local device.
104 if (!tick_device_is_functional(dev
)) {
105 dev
->event_handler
= tick_handle_periodic
;
106 cpu_set(cpu
, tick_broadcast_mask
);
107 tick_broadcast_start_periodic(tick_broadcast_device
.evtdev
);
111 * When the new device is not affected by the stop
112 * feature and the cpu is marked in the broadcast mask
113 * then clear the broadcast bit.
115 if (!(dev
->features
& CLOCK_EVT_FEAT_C3STOP
)) {
116 int cpu
= smp_processor_id();
118 cpu_clear(cpu
, tick_broadcast_mask
);
119 tick_broadcast_clear_oneshot(cpu
);
122 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
127 * Broadcast the event to the cpus, which are set in the mask
129 int tick_do_broadcast(cpumask_t mask
)
131 int ret
= 0, cpu
= smp_processor_id();
132 struct tick_device
*td
;
135 * Check, if the current cpu is in the mask
137 if (cpu_isset(cpu
, mask
)) {
138 cpu_clear(cpu
, mask
);
139 td
= &per_cpu(tick_cpu_device
, cpu
);
140 td
->evtdev
->event_handler(td
->evtdev
);
144 if (!cpus_empty(mask
)) {
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)
151 cpu
= first_cpu(mask
);
152 td
= &per_cpu(tick_cpu_device
, cpu
);
153 td
->evtdev
->broadcast(mask
);
160 * Periodic broadcast:
161 * - invoke the broadcast handlers
163 static void tick_do_periodic_broadcast(void)
167 spin_lock(&tick_broadcast_lock
);
169 cpus_and(mask
, cpu_online_map
, tick_broadcast_mask
);
170 tick_do_broadcast(mask
);
172 spin_unlock(&tick_broadcast_lock
);
176 * Event handler for periodic broadcast ticks
178 static void tick_handle_periodic_broadcast(struct clock_event_device
*dev
)
180 tick_do_periodic_broadcast();
183 * The device is in periodic mode. No reprogramming necessary:
185 if (dev
->mode
== CLOCK_EVT_MODE_PERIODIC
)
189 * Setup the next period for devices, which do not have
193 ktime_t next
= ktime_add(dev
->next_event
, tick_period
);
195 if (!clockevents_program_event(dev
, next
, ktime_get()))
197 tick_do_periodic_broadcast();
202 * Powerstate information: The system enters/leaves a state, where
203 * affected devices might stop
205 static void tick_do_broadcast_on_off(void *why
)
207 struct clock_event_device
*bc
, *dev
;
208 struct tick_device
*td
;
209 unsigned long flags
, *reason
= why
;
212 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
214 cpu
= smp_processor_id();
215 td
= &per_cpu(tick_cpu_device
, cpu
);
217 bc
= tick_broadcast_device
.evtdev
;
220 * Is the device not affected by the powerstate ?
222 if (!dev
|| !(dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
225 if (!tick_device_is_functional(dev
))
229 case CLOCK_EVT_NOTIFY_BROADCAST_ON
:
230 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE
:
231 if (!cpu_isset(cpu
, tick_broadcast_mask
)) {
232 cpu_set(cpu
, tick_broadcast_mask
);
233 if (td
->mode
== TICKDEV_MODE_PERIODIC
)
234 clockevents_set_mode(dev
,
235 CLOCK_EVT_MODE_SHUTDOWN
);
237 if (*reason
== CLOCK_EVT_NOTIFY_BROADCAST_FORCE
)
238 dev
->features
|= CLOCK_EVT_FEAT_DUMMY
;
240 case CLOCK_EVT_NOTIFY_BROADCAST_OFF
:
241 if (cpu_isset(cpu
, tick_broadcast_mask
)) {
242 cpu_clear(cpu
, tick_broadcast_mask
);
243 if (td
->mode
== TICKDEV_MODE_PERIODIC
)
244 tick_setup_periodic(dev
, 0);
249 if (cpus_empty(tick_broadcast_mask
))
250 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
252 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
)
253 tick_broadcast_start_periodic(bc
);
255 tick_broadcast_setup_oneshot(bc
);
258 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
262 * Powerstate information: The system enters/leaves a state, where
263 * affected devices might stop.
265 void tick_broadcast_on_off(unsigned long reason
, int *oncpu
)
267 if (!cpu_isset(*oncpu
, cpu_online_map
))
268 printk(KERN_ERR
"tick-braodcast: ignoring broadcast for "
269 "offline CPU #%d\n", *oncpu
);
271 smp_call_function_single(*oncpu
, tick_do_broadcast_on_off
,
276 * Set the periodic handler depending on broadcast on/off
278 void tick_set_periodic_handler(struct clock_event_device
*dev
, int broadcast
)
281 dev
->event_handler
= tick_handle_periodic
;
283 dev
->event_handler
= tick_handle_periodic_broadcast
;
287 * Remove a CPU from broadcasting
289 void tick_shutdown_broadcast(unsigned int *cpup
)
291 struct clock_event_device
*bc
;
293 unsigned int cpu
= *cpup
;
295 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
297 bc
= tick_broadcast_device
.evtdev
;
298 cpu_clear(cpu
, tick_broadcast_mask
);
300 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
) {
301 if (bc
&& cpus_empty(tick_broadcast_mask
))
302 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
305 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
308 void tick_suspend_broadcast(void)
310 struct clock_event_device
*bc
;
313 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
315 bc
= tick_broadcast_device
.evtdev
;
317 clockevents_set_mode(bc
, CLOCK_EVT_MODE_SHUTDOWN
);
319 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
322 int tick_resume_broadcast(void)
324 struct clock_event_device
*bc
;
328 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
330 bc
= tick_broadcast_device
.evtdev
;
333 clockevents_set_mode(bc
, CLOCK_EVT_MODE_RESUME
);
335 switch (tick_broadcast_device
.mode
) {
336 case TICKDEV_MODE_PERIODIC
:
337 if(!cpus_empty(tick_broadcast_mask
))
338 tick_broadcast_start_periodic(bc
);
339 broadcast
= cpu_isset(smp_processor_id(),
340 tick_broadcast_mask
);
342 case TICKDEV_MODE_ONESHOT
:
343 broadcast
= tick_resume_broadcast_oneshot(bc
);
347 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
353 #ifdef CONFIG_TICK_ONESHOT
355 static cpumask_t tick_broadcast_oneshot_mask
;
358 * Debugging: see timer_list.c
360 cpumask_t
*tick_get_broadcast_oneshot_mask(void)
362 return &tick_broadcast_oneshot_mask
;
365 static int tick_broadcast_set_event(ktime_t expires
, int force
)
367 struct clock_event_device
*bc
= tick_broadcast_device
.evtdev
;
368 ktime_t now
= ktime_get();
372 res
= clockevents_program_event(bc
, expires
, now
);
376 expires
= ktime_add(now
, ktime_set(0, bc
->min_delta_ns
));
380 int tick_resume_broadcast_oneshot(struct clock_event_device
*bc
)
382 clockevents_set_mode(bc
, CLOCK_EVT_MODE_ONESHOT
);
387 * Handle oneshot mode broadcasting
389 static void tick_handle_oneshot_broadcast(struct clock_event_device
*dev
)
391 struct tick_device
*td
;
393 ktime_t now
, next_event
;
396 spin_lock(&tick_broadcast_lock
);
398 dev
->next_event
.tv64
= KTIME_MAX
;
399 next_event
.tv64
= KTIME_MAX
;
400 mask
= CPU_MASK_NONE
;
402 /* Find all expired events */
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
<= now
.tv64
)
408 else if (td
->evtdev
->next_event
.tv64
< next_event
.tv64
)
409 next_event
.tv64
= td
->evtdev
->next_event
.tv64
;
413 * Wakeup the cpus which have an expired event.
415 tick_do_broadcast(mask
);
418 * Two reasons for reprogram:
420 * - The global event did not expire any CPU local
421 * events. This happens in dyntick mode, as the maximum PIT
422 * delta is quite small.
424 * - There are pending events on sleeping CPUs which were not
427 if (next_event
.tv64
!= KTIME_MAX
) {
429 * Rearm the broadcast device. If event expired,
432 if (tick_broadcast_set_event(next_event
, 0))
435 spin_unlock(&tick_broadcast_lock
);
439 * Powerstate information: The system enters/leaves a state, where
440 * affected devices might stop
442 void tick_broadcast_oneshot_control(unsigned long reason
)
444 struct clock_event_device
*bc
, *dev
;
445 struct tick_device
*td
;
449 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
452 * Periodic mode does not care about the enter/exit of power
455 if (tick_broadcast_device
.mode
== TICKDEV_MODE_PERIODIC
)
458 bc
= tick_broadcast_device
.evtdev
;
459 cpu
= smp_processor_id();
460 td
= &per_cpu(tick_cpu_device
, cpu
);
463 if (!(dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
466 if (reason
== CLOCK_EVT_NOTIFY_BROADCAST_ENTER
) {
467 if (!cpu_isset(cpu
, tick_broadcast_oneshot_mask
)) {
468 cpu_set(cpu
, tick_broadcast_oneshot_mask
);
469 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
470 if (dev
->next_event
.tv64
< bc
->next_event
.tv64
)
471 tick_broadcast_set_event(dev
->next_event
, 1);
474 if (cpu_isset(cpu
, tick_broadcast_oneshot_mask
)) {
475 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
476 clockevents_set_mode(dev
, CLOCK_EVT_MODE_ONESHOT
);
477 if (dev
->next_event
.tv64
!= KTIME_MAX
)
478 tick_program_event(dev
->next_event
, 1);
483 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
487 * Reset the one shot broadcast for a cpu
489 * Called with tick_broadcast_lock held
491 static void tick_broadcast_clear_oneshot(int cpu
)
493 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
497 * tick_broadcast_setup_oneshot - setup the broadcast device
499 void tick_broadcast_setup_oneshot(struct clock_event_device
*bc
)
501 bc
->event_handler
= tick_handle_oneshot_broadcast
;
502 clockevents_set_mode(bc
, CLOCK_EVT_MODE_ONESHOT
);
503 bc
->next_event
.tv64
= KTIME_MAX
;
507 * Select oneshot operating mode for the broadcast device
509 void tick_broadcast_switch_to_oneshot(void)
511 struct clock_event_device
*bc
;
514 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
516 tick_broadcast_device
.mode
= TICKDEV_MODE_ONESHOT
;
517 bc
= tick_broadcast_device
.evtdev
;
519 tick_broadcast_setup_oneshot(bc
);
520 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);
525 * Remove a dead CPU from broadcasting
527 void tick_shutdown_broadcast_oneshot(unsigned int *cpup
)
530 unsigned int cpu
= *cpup
;
532 spin_lock_irqsave(&tick_broadcast_lock
, flags
);
535 * Clear the broadcast mask flag for the dead cpu, but do not
536 * stop the broadcast device!
538 cpu_clear(cpu
, tick_broadcast_oneshot_mask
);
540 spin_unlock_irqrestore(&tick_broadcast_lock
, flags
);