]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/time/tick-broadcast.c
0e374cd2e0efeec1a5528d96106253477097db5e
[linux.git] / kernel / time / tick-broadcast.c
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>
17 #include <linux/interrupt.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/module.h>
23
24 #include "tick-internal.h"
25
26 /*
27  * Broadcast support for broken x86 hardware, where the local apic
28  * timer stops in C3 state.
29  */
30
31 static struct tick_device tick_broadcast_device;
32 static cpumask_var_t tick_broadcast_mask;
33 static cpumask_var_t tmpmask;
34 static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
35 static int tick_broadcast_force;
36
37 #ifdef CONFIG_TICK_ONESHOT
38 static void tick_broadcast_clear_oneshot(int cpu);
39 #else
40 static inline void tick_broadcast_clear_oneshot(int cpu) { }
41 #endif
42
43 /*
44  * Debugging: see timer_list.c
45  */
46 struct tick_device *tick_get_broadcast_device(void)
47 {
48         return &tick_broadcast_device;
49 }
50
51 struct cpumask *tick_get_broadcast_mask(void)
52 {
53         return tick_broadcast_mask;
54 }
55
56 /*
57  * Start the device in periodic mode
58  */
59 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
60 {
61         if (bc)
62                 tick_setup_periodic(bc, 1);
63 }
64
65 /*
66  * Check, if the device can be utilized as broadcast device:
67  */
68 void tick_install_broadcast_device(struct clock_event_device *dev)
69 {
70         struct clock_event_device *cur = tick_broadcast_device.evtdev;
71
72         if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
73             (tick_broadcast_device.evtdev &&
74              tick_broadcast_device.evtdev->rating >= dev->rating) ||
75              (dev->features & CLOCK_EVT_FEAT_C3STOP))
76                 return;
77         if (!try_module_get(dev->owner))
78                 return;
79
80         clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
81         if (cur)
82                 cur->event_handler = clockevents_handle_noop;
83         tick_broadcast_device.evtdev = dev;
84         if (!cpumask_empty(tick_broadcast_mask))
85                 tick_broadcast_start_periodic(dev);
86         /*
87          * Inform all cpus about this. We might be in a situation
88          * where we did not switch to oneshot mode because the per cpu
89          * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
90          * of a oneshot capable broadcast device. Without that
91          * notification the systems stays stuck in periodic mode
92          * forever.
93          */
94         if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
95                 tick_clock_notify();
96 }
97
98 /*
99  * Check, if the device is the broadcast device
100  */
101 int tick_is_broadcast_device(struct clock_event_device *dev)
102 {
103         return (dev && tick_broadcast_device.evtdev == dev);
104 }
105
106 static void err_broadcast(const struct cpumask *mask)
107 {
108         pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
109 }
110
111 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
112 {
113         if (!dev->broadcast)
114                 dev->broadcast = tick_broadcast;
115         if (!dev->broadcast) {
116                 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
117                              dev->name);
118                 dev->broadcast = err_broadcast;
119         }
120 }
121
122 /*
123  * Check, if the device is disfunctional and a place holder, which
124  * needs to be handled by the broadcast device.
125  */
126 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
127 {
128         unsigned long flags;
129         int ret = 0;
130
131         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
132
133         /*
134          * Devices might be registered with both periodic and oneshot
135          * mode disabled. This signals, that the device needs to be
136          * operated from the broadcast device and is a placeholder for
137          * the cpu local device.
138          */
139         if (!tick_device_is_functional(dev)) {
140                 dev->event_handler = tick_handle_periodic;
141                 tick_device_setup_broadcast_func(dev);
142                 cpumask_set_cpu(cpu, tick_broadcast_mask);
143                 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
144                 ret = 1;
145         } else {
146                 /*
147                  * When the new device is not affected by the stop
148                  * feature and the cpu is marked in the broadcast mask
149                  * then clear the broadcast bit.
150                  */
151                 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
152                         int cpu = smp_processor_id();
153                         cpumask_clear_cpu(cpu, tick_broadcast_mask);
154                         tick_broadcast_clear_oneshot(cpu);
155                 } else {
156                         tick_device_setup_broadcast_func(dev);
157                 }
158         }
159         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
160         return ret;
161 }
162
163 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
164 int tick_receive_broadcast(void)
165 {
166         struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
167         struct clock_event_device *evt = td->evtdev;
168
169         if (!evt)
170                 return -ENODEV;
171
172         if (!evt->event_handler)
173                 return -EINVAL;
174
175         evt->event_handler(evt);
176         return 0;
177 }
178 #endif
179
180 /*
181  * Broadcast the event to the cpus, which are set in the mask (mangled).
182  */
183 static void tick_do_broadcast(struct cpumask *mask)
184 {
185         int cpu = smp_processor_id();
186         struct tick_device *td;
187
188         /*
189          * Check, if the current cpu is in the mask
190          */
191         if (cpumask_test_cpu(cpu, mask)) {
192                 cpumask_clear_cpu(cpu, mask);
193                 td = &per_cpu(tick_cpu_device, cpu);
194                 td->evtdev->event_handler(td->evtdev);
195         }
196
197         if (!cpumask_empty(mask)) {
198                 /*
199                  * It might be necessary to actually check whether the devices
200                  * have different broadcast functions. For now, just use the
201                  * one of the first device. This works as long as we have this
202                  * misfeature only on x86 (lapic)
203                  */
204                 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
205                 td->evtdev->broadcast(mask);
206         }
207 }
208
209 /*
210  * Periodic broadcast:
211  * - invoke the broadcast handlers
212  */
213 static void tick_do_periodic_broadcast(void)
214 {
215         raw_spin_lock(&tick_broadcast_lock);
216
217         cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
218         tick_do_broadcast(tmpmask);
219
220         raw_spin_unlock(&tick_broadcast_lock);
221 }
222
223 /*
224  * Event handler for periodic broadcast ticks
225  */
226 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
227 {
228         ktime_t next;
229
230         tick_do_periodic_broadcast();
231
232         /*
233          * The device is in periodic mode. No reprogramming necessary:
234          */
235         if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
236                 return;
237
238         /*
239          * Setup the next period for devices, which do not have
240          * periodic mode. We read dev->next_event first and add to it
241          * when the event already expired. clockevents_program_event()
242          * sets dev->next_event only when the event is really
243          * programmed to the device.
244          */
245         for (next = dev->next_event; ;) {
246                 next = ktime_add(next, tick_period);
247
248                 if (!clockevents_program_event(dev, next, false))
249                         return;
250                 tick_do_periodic_broadcast();
251         }
252 }
253
254 /*
255  * Powerstate information: The system enters/leaves a state, where
256  * affected devices might stop
257  */
258 static void tick_do_broadcast_on_off(unsigned long *reason)
259 {
260         struct clock_event_device *bc, *dev;
261         struct tick_device *td;
262         unsigned long flags;
263         int cpu, bc_stopped;
264
265         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
266
267         cpu = smp_processor_id();
268         td = &per_cpu(tick_cpu_device, cpu);
269         dev = td->evtdev;
270         bc = tick_broadcast_device.evtdev;
271
272         /*
273          * Is the device not affected by the powerstate ?
274          */
275         if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
276                 goto out;
277
278         if (!tick_device_is_functional(dev))
279                 goto out;
280
281         bc_stopped = cpumask_empty(tick_broadcast_mask);
282
283         switch (*reason) {
284         case CLOCK_EVT_NOTIFY_BROADCAST_ON:
285         case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
286                 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
287                         if (tick_broadcast_device.mode ==
288                             TICKDEV_MODE_PERIODIC)
289                                 clockevents_shutdown(dev);
290                 }
291                 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
292                         tick_broadcast_force = 1;
293                 break;
294         case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
295                 if (!tick_broadcast_force &&
296                     cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
297                         if (tick_broadcast_device.mode ==
298                             TICKDEV_MODE_PERIODIC)
299                                 tick_setup_periodic(dev, 0);
300                 }
301                 break;
302         }
303
304         if (cpumask_empty(tick_broadcast_mask)) {
305                 if (!bc_stopped)
306                         clockevents_shutdown(bc);
307         } else if (bc_stopped) {
308                 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
309                         tick_broadcast_start_periodic(bc);
310                 else
311                         tick_broadcast_setup_oneshot(bc);
312         }
313 out:
314         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
315 }
316
317 /*
318  * Powerstate information: The system enters/leaves a state, where
319  * affected devices might stop.
320  */
321 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
322 {
323         if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
324                 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
325                        "offline CPU #%d\n", *oncpu);
326         else
327                 tick_do_broadcast_on_off(&reason);
328 }
329
330 /*
331  * Set the periodic handler depending on broadcast on/off
332  */
333 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
334 {
335         if (!broadcast)
336                 dev->event_handler = tick_handle_periodic;
337         else
338                 dev->event_handler = tick_handle_periodic_broadcast;
339 }
340
341 /*
342  * Remove a CPU from broadcasting
343  */
344 void tick_shutdown_broadcast(unsigned int *cpup)
345 {
346         struct clock_event_device *bc;
347         unsigned long flags;
348         unsigned int cpu = *cpup;
349
350         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
351
352         bc = tick_broadcast_device.evtdev;
353         cpumask_clear_cpu(cpu, tick_broadcast_mask);
354
355         if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
356                 if (bc && cpumask_empty(tick_broadcast_mask))
357                         clockevents_shutdown(bc);
358         }
359
360         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
361 }
362
363 void tick_suspend_broadcast(void)
364 {
365         struct clock_event_device *bc;
366         unsigned long flags;
367
368         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
369
370         bc = tick_broadcast_device.evtdev;
371         if (bc)
372                 clockevents_shutdown(bc);
373
374         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
375 }
376
377 int tick_resume_broadcast(void)
378 {
379         struct clock_event_device *bc;
380         unsigned long flags;
381         int broadcast = 0;
382
383         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
384
385         bc = tick_broadcast_device.evtdev;
386
387         if (bc) {
388                 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
389
390                 switch (tick_broadcast_device.mode) {
391                 case TICKDEV_MODE_PERIODIC:
392                         if (!cpumask_empty(tick_broadcast_mask))
393                                 tick_broadcast_start_periodic(bc);
394                         broadcast = cpumask_test_cpu(smp_processor_id(),
395                                                      tick_broadcast_mask);
396                         break;
397                 case TICKDEV_MODE_ONESHOT:
398                         if (!cpumask_empty(tick_broadcast_mask))
399                                 broadcast = tick_resume_broadcast_oneshot(bc);
400                         break;
401                 }
402         }
403         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
404
405         return broadcast;
406 }
407
408
409 #ifdef CONFIG_TICK_ONESHOT
410
411 static cpumask_var_t tick_broadcast_oneshot_mask;
412 static cpumask_var_t tick_broadcast_pending_mask;
413 static cpumask_var_t tick_broadcast_force_mask;
414
415 /*
416  * Exposed for debugging: see timer_list.c
417  */
418 struct cpumask *tick_get_broadcast_oneshot_mask(void)
419 {
420         return tick_broadcast_oneshot_mask;
421 }
422
423 /*
424  * Called before going idle with interrupts disabled. Checks whether a
425  * broadcast event from the other core is about to happen. We detected
426  * that in tick_broadcast_oneshot_control(). The callsite can use this
427  * to avoid a deep idle transition as we are about to get the
428  * broadcast IPI right away.
429  */
430 int tick_check_broadcast_expired(void)
431 {
432         return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
433 }
434
435 /*
436  * Set broadcast interrupt affinity
437  */
438 static void tick_broadcast_set_affinity(struct clock_event_device *bc,
439                                         const struct cpumask *cpumask)
440 {
441         if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
442                 return;
443
444         if (cpumask_equal(bc->cpumask, cpumask))
445                 return;
446
447         bc->cpumask = cpumask;
448         irq_set_affinity(bc->irq, bc->cpumask);
449 }
450
451 static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
452                                     ktime_t expires, int force)
453 {
454         int ret;
455
456         if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
457                 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
458
459         ret = clockevents_program_event(bc, expires, force);
460         if (!ret)
461                 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
462         return ret;
463 }
464
465 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
466 {
467         clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
468         return 0;
469 }
470
471 /*
472  * Called from irq_enter() when idle was interrupted to reenable the
473  * per cpu device.
474  */
475 void tick_check_oneshot_broadcast(int cpu)
476 {
477         if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
478                 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
479
480                 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
481         }
482 }
483
484 /*
485  * Handle oneshot mode broadcasting
486  */
487 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
488 {
489         struct tick_device *td;
490         ktime_t now, next_event;
491         int cpu, next_cpu = 0;
492
493         raw_spin_lock(&tick_broadcast_lock);
494 again:
495         dev->next_event.tv64 = KTIME_MAX;
496         next_event.tv64 = KTIME_MAX;
497         cpumask_clear(tmpmask);
498         now = ktime_get();
499         /* Find all expired events */
500         for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
501                 td = &per_cpu(tick_cpu_device, cpu);
502                 if (td->evtdev->next_event.tv64 <= now.tv64) {
503                         cpumask_set_cpu(cpu, tmpmask);
504                         /*
505                          * Mark the remote cpu in the pending mask, so
506                          * it can avoid reprogramming the cpu local
507                          * timer in tick_broadcast_oneshot_control().
508                          */
509                         cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
510                 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
511                         next_event.tv64 = td->evtdev->next_event.tv64;
512                         next_cpu = cpu;
513                 }
514         }
515
516         /* Take care of enforced broadcast requests */
517         cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
518         cpumask_clear(tick_broadcast_force_mask);
519
520         /*
521          * Wakeup the cpus which have an expired event.
522          */
523         tick_do_broadcast(tmpmask);
524
525         /*
526          * Two reasons for reprogram:
527          *
528          * - The global event did not expire any CPU local
529          * events. This happens in dyntick mode, as the maximum PIT
530          * delta is quite small.
531          *
532          * - There are pending events on sleeping CPUs which were not
533          * in the event mask
534          */
535         if (next_event.tv64 != KTIME_MAX) {
536                 /*
537                  * Rearm the broadcast device. If event expired,
538                  * repeat the above
539                  */
540                 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
541                         goto again;
542         }
543         raw_spin_unlock(&tick_broadcast_lock);
544 }
545
546 /*
547  * Powerstate information: The system enters/leaves a state, where
548  * affected devices might stop
549  */
550 void tick_broadcast_oneshot_control(unsigned long reason)
551 {
552         struct clock_event_device *bc, *dev;
553         struct tick_device *td;
554         unsigned long flags;
555         ktime_t now;
556         int cpu;
557
558         /*
559          * Periodic mode does not care about the enter/exit of power
560          * states
561          */
562         if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
563                 return;
564
565         /*
566          * We are called with preemtion disabled from the depth of the
567          * idle code, so we can't be moved away.
568          */
569         cpu = smp_processor_id();
570         td = &per_cpu(tick_cpu_device, cpu);
571         dev = td->evtdev;
572
573         if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
574                 return;
575
576         bc = tick_broadcast_device.evtdev;
577
578         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
579         if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
580                 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
581                 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
582                         clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
583                         /*
584                          * We only reprogram the broadcast timer if we
585                          * did not mark ourself in the force mask and
586                          * if the cpu local event is earlier than the
587                          * broadcast event. If the current CPU is in
588                          * the force mask, then we are going to be
589                          * woken by the IPI right away.
590                          */
591                         if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
592                             dev->next_event.tv64 < bc->next_event.tv64)
593                                 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
594                 }
595         } else {
596                 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
597                         clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
598                         if (dev->next_event.tv64 == KTIME_MAX)
599                                 goto out;
600                         /*
601                          * The cpu which was handling the broadcast
602                          * timer marked this cpu in the broadcast
603                          * pending mask and fired the broadcast
604                          * IPI. So we are going to handle the expired
605                          * event anyway via the broadcast IPI
606                          * handler. No need to reprogram the timer
607                          * with an already expired event.
608                          */
609                         if (cpumask_test_and_clear_cpu(cpu,
610                                        tick_broadcast_pending_mask))
611                                 goto out;
612
613                         /*
614                          * If the pending bit is not set, then we are
615                          * either the CPU handling the broadcast
616                          * interrupt or we got woken by something else.
617                          *
618                          * We are not longer in the broadcast mask, so
619                          * if the cpu local expiry time is already
620                          * reached, we would reprogram the cpu local
621                          * timer with an already expired event.
622                          *
623                          * This can lead to a ping-pong when we return
624                          * to idle and therefor rearm the broadcast
625                          * timer before the cpu local timer was able
626                          * to fire. This happens because the forced
627                          * reprogramming makes sure that the event
628                          * will happen in the future and depending on
629                          * the min_delta setting this might be far
630                          * enough out that the ping-pong starts.
631                          *
632                          * If the cpu local next_event has expired
633                          * then we know that the broadcast timer
634                          * next_event has expired as well and
635                          * broadcast is about to be handled. So we
636                          * avoid reprogramming and enforce that the
637                          * broadcast handler, which did not run yet,
638                          * will invoke the cpu local handler.
639                          *
640                          * We cannot call the handler directly from
641                          * here, because we might be in a NOHZ phase
642                          * and we did not go through the irq_enter()
643                          * nohz fixups.
644                          */
645                         now = ktime_get();
646                         if (dev->next_event.tv64 <= now.tv64) {
647                                 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
648                                 goto out;
649                         }
650                         /*
651                          * We got woken by something else. Reprogram
652                          * the cpu local timer device.
653                          */
654                         tick_program_event(dev->next_event, 1);
655                 }
656         }
657 out:
658         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
659 }
660
661 /*
662  * Reset the one shot broadcast for a cpu
663  *
664  * Called with tick_broadcast_lock held
665  */
666 static void tick_broadcast_clear_oneshot(int cpu)
667 {
668         cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
669 }
670
671 static void tick_broadcast_init_next_event(struct cpumask *mask,
672                                            ktime_t expires)
673 {
674         struct tick_device *td;
675         int cpu;
676
677         for_each_cpu(cpu, mask) {
678                 td = &per_cpu(tick_cpu_device, cpu);
679                 if (td->evtdev)
680                         td->evtdev->next_event = expires;
681         }
682 }
683
684 /**
685  * tick_broadcast_setup_oneshot - setup the broadcast device
686  */
687 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
688 {
689         int cpu = smp_processor_id();
690
691         /* Set it up only once ! */
692         if (bc->event_handler != tick_handle_oneshot_broadcast) {
693                 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
694
695                 bc->event_handler = tick_handle_oneshot_broadcast;
696
697                 /* Take the do_timer update */
698                 if (!tick_nohz_full_cpu(cpu))
699                         tick_do_timer_cpu = cpu;
700
701                 /*
702                  * We must be careful here. There might be other CPUs
703                  * waiting for periodic broadcast. We need to set the
704                  * oneshot_mask bits for those and program the
705                  * broadcast device to fire.
706                  */
707                 cpumask_copy(tmpmask, tick_broadcast_mask);
708                 cpumask_clear_cpu(cpu, tmpmask);
709                 cpumask_or(tick_broadcast_oneshot_mask,
710                            tick_broadcast_oneshot_mask, tmpmask);
711
712                 if (was_periodic && !cpumask_empty(tmpmask)) {
713                         clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
714                         tick_broadcast_init_next_event(tmpmask,
715                                                        tick_next_period);
716                         tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
717                 } else
718                         bc->next_event.tv64 = KTIME_MAX;
719         } else {
720                 /*
721                  * The first cpu which switches to oneshot mode sets
722                  * the bit for all other cpus which are in the general
723                  * (periodic) broadcast mask. So the bit is set and
724                  * would prevent the first broadcast enter after this
725                  * to program the bc device.
726                  */
727                 tick_broadcast_clear_oneshot(cpu);
728         }
729 }
730
731 /*
732  * Select oneshot operating mode for the broadcast device
733  */
734 void tick_broadcast_switch_to_oneshot(void)
735 {
736         struct clock_event_device *bc;
737         unsigned long flags;
738
739         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
740
741         tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
742         bc = tick_broadcast_device.evtdev;
743         if (bc)
744                 tick_broadcast_setup_oneshot(bc);
745
746         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
747 }
748
749
750 /*
751  * Remove a dead CPU from broadcasting
752  */
753 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
754 {
755         unsigned long flags;
756         unsigned int cpu = *cpup;
757
758         raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
759
760         /*
761          * Clear the broadcast mask flag for the dead cpu, but do not
762          * stop the broadcast device!
763          */
764         cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
765
766         raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
767 }
768
769 /*
770  * Check, whether the broadcast device is in one shot mode
771  */
772 int tick_broadcast_oneshot_active(void)
773 {
774         return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
775 }
776
777 /*
778  * Check whether the broadcast device supports oneshot.
779  */
780 bool tick_broadcast_oneshot_available(void)
781 {
782         struct clock_event_device *bc = tick_broadcast_device.evtdev;
783
784         return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
785 }
786
787 #endif
788
789 void __init tick_broadcast_init(void)
790 {
791         zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
792         zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
793 #ifdef CONFIG_TICK_ONESHOT
794         zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
795         zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
796         zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
797 #endif
798 }