1 // SPDX-License-Identifier: GPL-2.0
3 * Emulate a local clock event device via a pseudo clock device.
7 #include <linux/hrtimer.h>
8 #include <linux/interrupt.h>
9 #include <linux/percpu.h>
10 #include <linux/profile.h>
11 #include <linux/clockchips.h>
12 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/module.h>
16 #include "tick-internal.h"
18 static struct hrtimer bctimer;
20 static int bc_shutdown(struct clock_event_device *evt)
23 * Note, we cannot cancel the timer here as we might
24 * run into the following live lock scenario:
27 * lock(broadcast_lock);
30 * tick_handle_oneshot_broadcast();
31 * lock(broadcast_lock);
35 hrtimer_try_to_cancel(&bctimer);
40 * This is called from the guts of the broadcast code when the cpu
41 * which is about to enter idle has the earliest broadcast timer event.
43 static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
46 * This is called either from enter/exit idle code or from the
47 * broadcast handler. In all cases tick_broadcast_lock is held.
49 * hrtimer_cancel() cannot be called here neither from the
50 * broadcast handler nor from the enter/exit idle code. The idle
51 * code can run into the problem described in bc_shutdown() and the
52 * broadcast handler cannot wait for itself to complete for obvious
55 * Each caller tries to arm the hrtimer on its own CPU, but if the
56 * hrtimer callback function is currently running, then
57 * hrtimer_start() cannot move it and the timer stays on the CPU on
58 * which it is assigned at the moment.
60 hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED_HARD);
62 * The core tick broadcast mode expects bc->bound_on to be set
63 * correctly to prevent a CPU which has the broadcast hrtimer
64 * armed from going deep idle.
66 * As tick_broadcast_lock is held, nothing can change the cpu
67 * base which was just established in hrtimer_start() above. So
68 * the below access is safe even without holding the hrtimer
71 bc->bound_on = bctimer.base->cpu_base->cpu;
76 static struct clock_event_device ce_broadcast_hrtimer = {
78 .set_state_shutdown = bc_shutdown,
79 .set_next_ktime = bc_set_next,
80 .features = CLOCK_EVT_FEAT_ONESHOT |
81 CLOCK_EVT_FEAT_KTIME |
82 CLOCK_EVT_FEAT_HRTIMER,
86 .max_delta_ns = KTIME_MAX,
88 .max_delta_ticks = ULONG_MAX,
91 .cpumask = cpu_possible_mask,
94 static enum hrtimer_restart bc_handler(struct hrtimer *t)
96 ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
98 return HRTIMER_NORESTART;
101 void tick_setup_hrtimer_broadcast(void)
103 hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
104 bctimer.function = bc_handler;
105 clockevents_register_device(&ce_broadcast_hrtimer);