blob: f98a1b7b16e942018ffe9e5998756405695da9a4 [file] [log] [blame]
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -08001/*
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>
Russell Kingd7b90682008-04-17 07:46:24 +020017#include <linux/interrupt.h>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080018#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;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080032static DEFINE_SPINLOCK(tick_broadcast_lock);
Thomas Gleixneraa276e12008-06-09 19:15:00 +020033static int tick_broadcast_force;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080034
Thomas Gleixner5590a532007-07-21 04:37:35 -070035#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
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080041/*
Ingo Molnar289f4802007-02-16 01:28:15 -080042 * 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
54/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080055 * Start the device in periodic mode
56 */
57static void tick_broadcast_start_periodic(struct clock_event_device *bc)
58{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070059 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080060 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{
Venki Pallipadi4a932322007-10-12 23:04:23 +020068 if ((tick_broadcast_device.evtdev &&
69 tick_broadcast_device.evtdev->rating >= dev->rating) ||
70 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080071 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;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700110 } 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();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800118
Thomas Gleixner5590a532007-07-21 04:37:35 -0700119 cpu_clear(cpu, tick_broadcast_mask);
120 tick_broadcast_clear_oneshot(cpu);
121 }
122 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800123 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 */
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100130static void tick_do_broadcast(cpumask_t mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800131{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100132 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800133 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);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800142 }
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);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800154 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800155}
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{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000178 ktime_t next;
179
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180 tick_do_periodic_broadcast();
181
182 /*
183 * The device is in periodic mode. No reprogramming necessary:
184 */
185 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
186 return;
187
188 /*
189 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000190 * periodic mode. We read dev->next_event first and add to it
191 * when the event alrady expired. clockevents_program_event()
192 * sets dev->next_event only when the event is really
193 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800194 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000195 for (next = dev->next_event; ;) {
196 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800197
198 if (!clockevents_program_event(dev, next, ktime_get()))
199 return;
200 tick_do_periodic_broadcast();
201 }
202}
203
204/*
205 * Powerstate information: The system enters/leaves a state, where
206 * affected devices might stop
207 */
208static void tick_do_broadcast_on_off(void *why)
209{
210 struct clock_event_device *bc, *dev;
211 struct tick_device *td;
212 unsigned long flags, *reason = why;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000213 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800214
215 spin_lock_irqsave(&tick_broadcast_lock, flags);
216
217 cpu = smp_processor_id();
218 td = &per_cpu(tick_cpu_device, cpu);
219 dev = td->evtdev;
220 bc = tick_broadcast_device.evtdev;
221
222 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200223 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800224 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200225 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800226 goto out;
227
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200228 if (!tick_device_is_functional(dev))
229 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200230
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000231 bc_stopped = cpus_empty(tick_broadcast_mask);
232
Thomas Gleixner1595f452007-10-14 22:57:45 +0200233 switch (*reason) {
234 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
235 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800236 if (!cpu_isset(cpu, tick_broadcast_mask)) {
237 cpu_set(cpu, tick_broadcast_mask);
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200238 if (tick_broadcast_device.mode ==
239 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700240 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800241 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200242 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200243 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200244 break;
245 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200246 if (!tick_broadcast_force &&
247 cpu_isset(cpu, tick_broadcast_mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800248 cpu_clear(cpu, tick_broadcast_mask);
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200249 if (tick_broadcast_device.mode ==
250 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800251 tick_setup_periodic(dev, 0);
252 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200253 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800254 }
255
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000256 if (cpus_empty(tick_broadcast_mask)) {
257 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700258 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000259 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800260 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
261 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800262 else
263 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800264 }
265out:
266 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
267}
268
269/*
270 * Powerstate information: The system enters/leaves a state, where
271 * affected devices might stop.
272 */
273void tick_broadcast_on_off(unsigned long reason, int *oncpu)
274{
Avi Kivitybf020cb2007-10-16 23:26:24 -0700275 if (!cpu_isset(*oncpu, cpu_online_map))
Glauber Costa833df312008-04-18 13:38:58 -0700276 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700277 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700278 else
279 smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200280 &reason, 1);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800281}
282
283/*
284 * Set the periodic handler depending on broadcast on/off
285 */
286void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
287{
288 if (!broadcast)
289 dev->event_handler = tick_handle_periodic;
290 else
291 dev->event_handler = tick_handle_periodic_broadcast;
292}
293
294/*
295 * Remove a CPU from broadcasting
296 */
297void tick_shutdown_broadcast(unsigned int *cpup)
298{
299 struct clock_event_device *bc;
300 unsigned long flags;
301 unsigned int cpu = *cpup;
302
303 spin_lock_irqsave(&tick_broadcast_lock, flags);
304
305 bc = tick_broadcast_device.evtdev;
306 cpu_clear(cpu, tick_broadcast_mask);
307
308 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
309 if (bc && cpus_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700310 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800311 }
312
313 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
314}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800315
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100316void tick_suspend_broadcast(void)
317{
318 struct clock_event_device *bc;
319 unsigned long flags;
320
321 spin_lock_irqsave(&tick_broadcast_lock, flags);
322
323 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700324 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700325 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100326
327 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
328}
329
330int tick_resume_broadcast(void)
331{
332 struct clock_event_device *bc;
333 unsigned long flags;
334 int broadcast = 0;
335
336 spin_lock_irqsave(&tick_broadcast_lock, flags);
337
338 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100339
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100340 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700341 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
342
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100343 switch (tick_broadcast_device.mode) {
344 case TICKDEV_MODE_PERIODIC:
345 if(!cpus_empty(tick_broadcast_mask))
346 tick_broadcast_start_periodic(bc);
347 broadcast = cpu_isset(smp_processor_id(),
348 tick_broadcast_mask);
349 break;
350 case TICKDEV_MODE_ONESHOT:
351 broadcast = tick_resume_broadcast_oneshot(bc);
352 break;
353 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100354 }
355 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
356
357 return broadcast;
358}
359
360
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800361#ifdef CONFIG_TICK_ONESHOT
362
363static cpumask_t tick_broadcast_oneshot_mask;
364
Ingo Molnar289f4802007-02-16 01:28:15 -0800365/*
366 * Debugging: see timer_list.c
367 */
368cpumask_t *tick_get_broadcast_oneshot_mask(void)
369{
370 return &tick_broadcast_oneshot_mask;
371}
372
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800373static int tick_broadcast_set_event(ktime_t expires, int force)
374{
375 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800376
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +0000377 return tick_dev_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800378}
379
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100380int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
381{
382 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000383 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100384}
385
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800386/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200387 * Called from irq_enter() when idle was interrupted to reenable the
388 * per cpu device.
389 */
390void tick_check_oneshot_broadcast(int cpu)
391{
392 if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
393 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
394
395 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
396 }
397}
398
399/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800400 * Handle oneshot mode broadcasting
401 */
402static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
403{
404 struct tick_device *td;
405 cpumask_t mask;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100406 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800407 int cpu;
408
409 spin_lock(&tick_broadcast_lock);
410again:
411 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100412 next_event.tv64 = KTIME_MAX;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800413 mask = CPU_MASK_NONE;
414 now = ktime_get();
415 /* Find all expired events */
Mike Traviscad0e452008-05-12 21:21:13 +0200416 for_each_cpu_mask_nr(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800417 td = &per_cpu(tick_cpu_device, cpu);
418 if (td->evtdev->next_event.tv64 <= now.tv64)
419 cpu_set(cpu, mask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100420 else if (td->evtdev->next_event.tv64 < next_event.tv64)
421 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800422 }
423
424 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100425 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800426 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100427 tick_do_broadcast(mask);
428
429 /*
430 * Two reasons for reprogram:
431 *
432 * - The global event did not expire any CPU local
433 * events. This happens in dyntick mode, as the maximum PIT
434 * delta is quite small.
435 *
436 * - There are pending events on sleeping CPUs which were not
437 * in the event mask
438 */
439 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800440 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100441 * Rearm the broadcast device. If event expired,
442 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800443 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100444 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800445 goto again;
446 }
447 spin_unlock(&tick_broadcast_lock);
448}
449
450/*
451 * Powerstate information: The system enters/leaves a state, where
452 * affected devices might stop
453 */
454void tick_broadcast_oneshot_control(unsigned long reason)
455{
456 struct clock_event_device *bc, *dev;
457 struct tick_device *td;
458 unsigned long flags;
459 int cpu;
460
461 spin_lock_irqsave(&tick_broadcast_lock, flags);
462
463 /*
464 * Periodic mode does not care about the enter/exit of power
465 * states
466 */
467 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
468 goto out;
469
470 bc = tick_broadcast_device.evtdev;
471 cpu = smp_processor_id();
472 td = &per_cpu(tick_cpu_device, cpu);
473 dev = td->evtdev;
474
475 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
476 goto out;
477
478 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
479 if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
480 cpu_set(cpu, tick_broadcast_oneshot_mask);
481 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
482 if (dev->next_event.tv64 < bc->next_event.tv64)
483 tick_broadcast_set_event(dev->next_event, 1);
484 }
485 } else {
486 if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
487 cpu_clear(cpu, tick_broadcast_oneshot_mask);
488 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
489 if (dev->next_event.tv64 != KTIME_MAX)
490 tick_program_event(dev->next_event, 1);
491 }
492 }
493
494out:
495 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
496}
497
Thomas Gleixner5590a532007-07-21 04:37:35 -0700498/*
499 * Reset the one shot broadcast for a cpu
500 *
501 * Called with tick_broadcast_lock held
502 */
503static void tick_broadcast_clear_oneshot(int cpu)
504{
505 cpu_clear(cpu, tick_broadcast_oneshot_mask);
506}
507
Thomas Gleixner73007112008-09-06 03:01:45 +0200508static void tick_broadcast_init_next_event(cpumask_t *mask, ktime_t expires)
509{
510 struct tick_device *td;
511 int cpu;
512
513 for_each_cpu_mask_nr(cpu, *mask) {
514 td = &per_cpu(tick_cpu_device, cpu);
515 if (td->evtdev)
516 td->evtdev->next_event = expires;
517 }
518}
519
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800520/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800521 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800522 */
523void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
524{
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000525 /* Set it up only once ! */
526 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200527 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
528 int cpu = smp_processor_id();
529 cpumask_t mask;
530
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000531 bc->event_handler = tick_handle_oneshot_broadcast;
532 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner73007112008-09-06 03:01:45 +0200533
534 /* Take the do_timer update */
535 tick_do_timer_cpu = cpu;
536
537 /*
538 * We must be careful here. There might be other CPUs
539 * waiting for periodic broadcast. We need to set the
540 * oneshot_mask bits for those and program the
541 * broadcast device to fire.
542 */
543 mask = tick_broadcast_mask;
544 cpu_clear(cpu, mask);
545 cpus_or(tick_broadcast_oneshot_mask,
546 tick_broadcast_oneshot_mask, mask);
547
548 if (was_periodic && !cpus_empty(mask)) {
549 tick_broadcast_init_next_event(&mask, tick_next_period);
550 tick_broadcast_set_event(tick_next_period, 1);
551 } else
552 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000553 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800554}
555
556/*
557 * Select oneshot operating mode for the broadcast device
558 */
559void tick_broadcast_switch_to_oneshot(void)
560{
561 struct clock_event_device *bc;
562 unsigned long flags;
563
564 spin_lock_irqsave(&tick_broadcast_lock, flags);
565
566 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
567 bc = tick_broadcast_device.evtdev;
568 if (bc)
569 tick_broadcast_setup_oneshot(bc);
570 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
571}
572
573
574/*
575 * Remove a dead CPU from broadcasting
576 */
577void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
578{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800579 unsigned long flags;
580 unsigned int cpu = *cpup;
581
582 spin_lock_irqsave(&tick_broadcast_lock, flags);
583
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200584 /*
585 * Clear the broadcast mask flag for the dead cpu, but do not
586 * stop the broadcast device!
587 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800588 cpu_clear(cpu, tick_broadcast_oneshot_mask);
589
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800590 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
591}
592
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200593/*
594 * Check, whether the broadcast device is in one shot mode
595 */
596int tick_broadcast_oneshot_active(void)
597{
598 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
599}
600
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800601#endif