blob: f8961bf973628837410321abd44aa072ae748370 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
22#include "tick-internal.h"
23
24/*
25 * Broadcast support for broken x86 hardware, where the local apic
26 * timer stops in C3 state.
27 */
28
29static struct tick_device tick_broadcast_device;
30/* FIXME: Use cpumask_var_t. */
31static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
32static DECLARE_BITMAP(tmpmask, NR_CPUS);
33static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
34static int tick_broadcast_force;
35
36#ifdef CONFIG_TICK_ONESHOT
37static void tick_broadcast_clear_oneshot(int cpu);
38#else
39static inline void tick_broadcast_clear_oneshot(int cpu) { }
40#endif
41
42/*
43 * Debugging: see timer_list.c
44 */
45struct tick_device *tick_get_broadcast_device(void)
46{
47 return &tick_broadcast_device;
48}
49
50struct cpumask *tick_get_broadcast_mask(void)
51{
52 return to_cpumask(tick_broadcast_mask);
53}
54
55/*
56 * Start the device in periodic mode
57 */
58static void tick_broadcast_start_periodic(struct clock_event_device *bc)
59{
60 if (bc)
61 tick_setup_periodic(bc, 1);
62}
63
64/*
65 * Check, if the device can be utilized as broadcast device:
66 */
67int tick_check_broadcast_device(struct clock_event_device *dev)
68{
69 struct clock_event_device *cur = tick_broadcast_device.evtdev;
70
71 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
72 (tick_broadcast_device.evtdev &&
73 tick_broadcast_device.evtdev->rating >= dev->rating) ||
74 (dev->features & CLOCK_EVT_FEAT_C3STOP))
75 return 0;
76
77 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
78 if (cur)
79 cur->event_handler = clockevents_handle_noop;
80 tick_broadcast_device.evtdev = dev;
81 if (!cpumask_empty(tick_get_broadcast_mask()))
82 tick_broadcast_start_periodic(dev);
83 return 1;
84}
85
86/*
87 * Check, if the device is the broadcast device
88 */
89int tick_is_broadcast_device(struct clock_event_device *dev)
90{
91 return (dev && tick_broadcast_device.evtdev == dev);
92}
93
94/*
95 * Check, if the device is disfunctional and a place holder, which
96 * needs to be handled by the broadcast device.
97 */
98int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
99{
100 unsigned long flags;
101 int ret = 0;
102
103 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
104
105 /*
106 * Devices might be registered with both periodic and oneshot
107 * mode disabled. This signals, that the device needs to be
108 * operated from the broadcast device and is a placeholder for
109 * the cpu local device.
110 */
111 if (!tick_device_is_functional(dev)) {
112 dev->event_handler = tick_handle_periodic;
113 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
114 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
115 ret = 1;
116 } else {
117 /*
118 * When the new device is not affected by the stop
119 * feature and the cpu is marked in the broadcast mask
120 * then clear the broadcast bit.
121 */
122 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
123 int cpu = smp_processor_id();
124
125 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
126 tick_broadcast_clear_oneshot(cpu);
127 }
128 }
129 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
130 return ret;
131}
132
133/*
134 * Broadcast the event to the cpus, which are set in the mask (mangled).
135 */
136static void tick_do_broadcast(struct cpumask *mask)
137{
138 int cpu = smp_processor_id();
139 struct tick_device *td;
140
141 /*
142 * Check, if the current cpu is in the mask
143 */
144 if (cpumask_test_cpu(cpu, mask)) {
145 cpumask_clear_cpu(cpu, mask);
146 td = &per_cpu(tick_cpu_device, cpu);
147 td->evtdev->event_handler(td->evtdev);
148 }
149
150 if (!cpumask_empty(mask)) {
151 /*
152 * It might be necessary to actually check whether the devices
153 * have different broadcast functions. For now, just use the
154 * one of the first device. This works as long as we have this
155 * misfeature only on x86 (lapic)
156 */
157 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
158 td->evtdev->broadcast(mask);
159 }
160}
161
162/*
163 * Periodic broadcast:
164 * - invoke the broadcast handlers
165 */
166static void tick_do_periodic_broadcast(void)
167{
168 raw_spin_lock(&tick_broadcast_lock);
169
170 cpumask_and(to_cpumask(tmpmask),
171 cpu_online_mask, tick_get_broadcast_mask());
172 tick_do_broadcast(to_cpumask(tmpmask));
173
174 raw_spin_unlock(&tick_broadcast_lock);
175}
176
177/*
178 * Event handler for periodic broadcast ticks
179 */
180static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
181{
182 ktime_t next;
183
184 tick_do_periodic_broadcast();
185
186 /*
187 * The device is in periodic mode. No reprogramming necessary:
188 */
189 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
190 return;
191
192 /*
193 * Setup the next period for devices, which do not have
194 * periodic mode. We read dev->next_event first and add to it
195 * when the event already expired. clockevents_program_event()
196 * sets dev->next_event only when the event is really
197 * programmed to the device.
198 */
199 for (next = dev->next_event; ;) {
200 next = ktime_add(next, tick_period);
201
202 if (!clockevents_program_event(dev, next, false))
203 return;
204 tick_do_periodic_broadcast();
205 }
206}
207
208/*
209 * Powerstate information: The system enters/leaves a state, where
210 * affected devices might stop
211 */
212static void tick_do_broadcast_on_off(unsigned long *reason)
213{
214 struct clock_event_device *bc, *dev;
215 struct tick_device *td;
216 unsigned long flags;
217 int cpu, bc_stopped;
218
219 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
220
221 cpu = smp_processor_id();
222 td = &per_cpu(tick_cpu_device, cpu);
223 dev = td->evtdev;
224 bc = tick_broadcast_device.evtdev;
225
226 /*
227 * Is the device not affected by the powerstate ?
228 */
229 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
230 goto out;
231
232 if (!tick_device_is_functional(dev))
233 goto out;
234
235 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
236
237 switch (*reason) {
238 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
239 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
240 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
241 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
242 if (tick_broadcast_device.mode ==
243 TICKDEV_MODE_PERIODIC)
244 clockevents_shutdown(dev);
245 }
246 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
247 tick_broadcast_force = 1;
248 break;
249 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
250 if (!tick_broadcast_force &&
251 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
252 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
253 if (tick_broadcast_device.mode ==
254 TICKDEV_MODE_PERIODIC)
255 tick_setup_periodic(dev, 0);
256 }
257 break;
258 }
259
260 if (cpumask_empty(tick_get_broadcast_mask())) {
261 if (!bc_stopped)
262 clockevents_shutdown(bc);
263 } else if (bc_stopped) {
264 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
265 tick_broadcast_start_periodic(bc);
266 else
267 tick_broadcast_setup_oneshot(bc);
268 }
269out:
270 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
271}
272
273/*
274 * Powerstate information: The system enters/leaves a state, where
275 * affected devices might stop.
276 */
277void tick_broadcast_on_off(unsigned long reason, int *oncpu)
278{
279 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
280 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
281 "offline CPU #%d\n", *oncpu);
282 else
283 tick_do_broadcast_on_off(&reason);
284}
285
286/*
287 * Set the periodic handler depending on broadcast on/off
288 */
289void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
290{
291 if (!broadcast)
292 dev->event_handler = tick_handle_periodic;
293 else
294 dev->event_handler = tick_handle_periodic_broadcast;
295}
296
297/*
298 * Remove a CPU from broadcasting
299 */
300void tick_shutdown_broadcast(unsigned int *cpup)
301{
302 struct clock_event_device *bc;
303 unsigned long flags;
304 unsigned int cpu = *cpup;
305
306 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
307
308 bc = tick_broadcast_device.evtdev;
309 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
310
311 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
312 if (bc && cpumask_empty(tick_get_broadcast_mask()))
313 clockevents_shutdown(bc);
314 }
315
316 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
317}
318
319void tick_suspend_broadcast(void)
320{
321 struct clock_event_device *bc;
322 unsigned long flags;
323
324 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
325
326 bc = tick_broadcast_device.evtdev;
327 if (bc)
328 clockevents_shutdown(bc);
329
330 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
331}
332
333int tick_resume_broadcast(void)
334{
335 struct clock_event_device *bc;
336 unsigned long flags;
337 int broadcast = 0;
338
339 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
340
341 bc = tick_broadcast_device.evtdev;
342
343 if (bc) {
344 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
345
346 switch (tick_broadcast_device.mode) {
347 case TICKDEV_MODE_PERIODIC:
348 if (!cpumask_empty(tick_get_broadcast_mask()))
349 tick_broadcast_start_periodic(bc);
350 broadcast = cpumask_test_cpu(smp_processor_id(),
351 tick_get_broadcast_mask());
352 break;
353 case TICKDEV_MODE_ONESHOT:
354 if (!cpumask_empty(tick_get_broadcast_mask()))
355 broadcast = tick_resume_broadcast_oneshot(bc);
356 break;
357 }
358 }
359 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
360
361 return broadcast;
362}
363
364
365#ifdef CONFIG_TICK_ONESHOT
366
367/* FIXME: use cpumask_var_t. */
368static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
369
370/*
371 * Exposed for debugging: see timer_list.c
372 */
373struct cpumask *tick_get_broadcast_oneshot_mask(void)
374{
375 return to_cpumask(tick_broadcast_oneshot_mask);
376}
377
378static int tick_broadcast_set_event(ktime_t expires, int force)
379{
380 struct clock_event_device *bc = tick_broadcast_device.evtdev;
381
382 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
383 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
384
385 return clockevents_program_event(bc, expires, force);
386}
387
388int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
389{
390 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
391 return 0;
392}
393
394/*
395 * Called from irq_enter() when idle was interrupted to reenable the
396 * per cpu device.
397 */
398void tick_check_oneshot_broadcast(int cpu)
399{
400 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
401 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
402
403 /*
404 * We might be in the middle of switching over from
405 * periodic to oneshot. If the CPU has not yet
406 * switched over, leave the device alone.
407 */
408 if (td->mode == TICKDEV_MODE_ONESHOT) {
409 clockevents_set_mode(td->evtdev,
410 CLOCK_EVT_MODE_ONESHOT);
411 }
412 }
413}
414
415/*
416 * Handle oneshot mode broadcasting
417 */
418static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
419{
420 struct tick_device *td;
421 ktime_t now, next_event;
422 int cpu;
423
424 raw_spin_lock(&tick_broadcast_lock);
425again:
426 dev->next_event.tv64 = KTIME_MAX;
427 next_event.tv64 = KTIME_MAX;
428 cpumask_clear(to_cpumask(tmpmask));
429 now = ktime_get();
430 /* Find all expired events */
431 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
432 td = &per_cpu(tick_cpu_device, cpu);
433 if (td->evtdev->next_event.tv64 <= now.tv64)
434 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
435 else if (td->evtdev->next_event.tv64 < next_event.tv64)
436 next_event.tv64 = td->evtdev->next_event.tv64;
437 }
438
439 /*
440 * Wakeup the cpus which have an expired event.
441 */
442 tick_do_broadcast(to_cpumask(tmpmask));
443
444 /*
445 * Two reasons for reprogram:
446 *
447 * - The global event did not expire any CPU local
448 * events. This happens in dyntick mode, as the maximum PIT
449 * delta is quite small.
450 *
451 * - There are pending events on sleeping CPUs which were not
452 * in the event mask
453 */
454 if (next_event.tv64 != KTIME_MAX) {
455 /*
456 * Rearm the broadcast device. If event expired,
457 * repeat the above
458 */
459 if (tick_broadcast_set_event(next_event, 0))
460 goto again;
461 }
462 raw_spin_unlock(&tick_broadcast_lock);
463}
464
465/*
466 * Powerstate information: The system enters/leaves a state, where
467 * affected devices might stop
468 */
469void tick_broadcast_oneshot_control(unsigned long reason)
470{
471 struct clock_event_device *bc, *dev;
472 struct tick_device *td;
473 unsigned long flags;
474 int cpu;
475
476 /*
477 * Periodic mode does not care about the enter/exit of power
478 * states
479 */
480 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
481 return;
482
483 /*
484 * We are called with preemtion disabled from the depth of the
485 * idle code, so we can't be moved away.
486 */
487 cpu = smp_processor_id();
488 td = &per_cpu(tick_cpu_device, cpu);
489 dev = td->evtdev;
490
491 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
492 return;
493
494 bc = tick_broadcast_device.evtdev;
495
496 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
497 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
498 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
499 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
500 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
501 if (dev->next_event.tv64 < bc->next_event.tv64)
502 tick_broadcast_set_event(dev->next_event, 1);
503 }
504 } else {
505 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
506 cpumask_clear_cpu(cpu,
507 tick_get_broadcast_oneshot_mask());
508 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
509 if (dev->next_event.tv64 != KTIME_MAX)
510 tick_program_event(dev->next_event, 1);
511 }
512 }
513 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
514}
515
516/*
517 * Reset the one shot broadcast for a cpu
518 *
519 * Called with tick_broadcast_lock held
520 */
521static void tick_broadcast_clear_oneshot(int cpu)
522{
523 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
524}
525
526static void tick_broadcast_init_next_event(struct cpumask *mask,
527 ktime_t expires)
528{
529 struct tick_device *td;
530 int cpu;
531
532 for_each_cpu(cpu, mask) {
533 td = &per_cpu(tick_cpu_device, cpu);
534 if (td->evtdev)
535 td->evtdev->next_event = expires;
536 }
537}
538
539/**
540 * tick_broadcast_setup_oneshot - setup the broadcast device
541 */
542void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
543{
544 int cpu = smp_processor_id();
545
546 /* Set it up only once ! */
547 if (bc->event_handler != tick_handle_oneshot_broadcast) {
548 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
549
550 bc->event_handler = tick_handle_oneshot_broadcast;
551
552 /* Take the do_timer update */
553 tick_do_timer_cpu = cpu;
554
555 /*
556 * We must be careful here. There might be other CPUs
557 * waiting for periodic broadcast. We need to set the
558 * oneshot_mask bits for those and program the
559 * broadcast device to fire.
560 */
561 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
562 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
563 cpumask_or(tick_get_broadcast_oneshot_mask(),
564 tick_get_broadcast_oneshot_mask(),
565 to_cpumask(tmpmask));
566
567 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
568 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
569 tick_broadcast_init_next_event(to_cpumask(tmpmask),
570 tick_next_period);
571 tick_broadcast_set_event(tick_next_period, 1);
572 } else
573 bc->next_event.tv64 = KTIME_MAX;
574 } else {
575 /*
576 * The first cpu which switches to oneshot mode sets
577 * the bit for all other cpus which are in the general
578 * (periodic) broadcast mask. So the bit is set and
579 * would prevent the first broadcast enter after this
580 * to program the bc device.
581 */
582 tick_broadcast_clear_oneshot(cpu);
583 }
584}
585
586/*
587 * Select oneshot operating mode for the broadcast device
588 */
589void tick_broadcast_switch_to_oneshot(void)
590{
591 struct clock_event_device *bc;
592 unsigned long flags;
593
594 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
595
596 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
597 bc = tick_broadcast_device.evtdev;
598 if (bc)
599 tick_broadcast_setup_oneshot(bc);
600
601 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
602}
603
604
605/*
606 * Remove a dead CPU from broadcasting
607 */
608void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
609{
610 unsigned long flags;
611 unsigned int cpu = *cpup;
612
613 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
614
615 /*
616 * Clear the broadcast mask flag for the dead cpu, but do not
617 * stop the broadcast device!
618 */
619 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
620
621 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
622}
623
624/*
625 * Check, whether the broadcast device is in one shot mode
626 */
627int tick_broadcast_oneshot_active(void)
628{
629 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
630}
631
632/*
633 * Check whether the broadcast device supports oneshot.
634 */
635bool tick_broadcast_oneshot_available(void)
636{
637 struct clock_event_device *bc = tick_broadcast_device.evtdev;
638
639 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
640}
641
642#endif