]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5d1638ac PM |
2 | /* |
3 | * linux/kernel/time/tick-broadcast-hrtimer.c | |
4 | * This file emulates a local clock event device | |
5 | * via a pseudo clock device. | |
6 | */ | |
7 | #include <linux/cpu.h> | |
8 | #include <linux/err.h> | |
9 | #include <linux/hrtimer.h> | |
10 | #include <linux/interrupt.h> | |
11 | #include <linux/percpu.h> | |
12 | #include <linux/profile.h> | |
13 | #include <linux/clockchips.h> | |
14 | #include <linux/sched.h> | |
15 | #include <linux/smp.h> | |
16 | #include <linux/module.h> | |
17 | ||
18 | #include "tick-internal.h" | |
19 | ||
20 | static struct hrtimer bctimer; | |
21 | ||
ecbebcb8 | 22 | static int bc_shutdown(struct clock_event_device *evt) |
5d1638ac | 23 | { |
ecbebcb8 VK |
24 | /* |
25 | * Note, we cannot cancel the timer here as we might | |
26 | * run into the following live lock scenario: | |
27 | * | |
28 | * cpu 0 cpu1 | |
29 | * lock(broadcast_lock); | |
30 | * hrtimer_interrupt() | |
31 | * bc_handler() | |
32 | * tick_handle_oneshot_broadcast(); | |
33 | * lock(broadcast_lock); | |
34 | * hrtimer_cancel() | |
35 | * wait_for_callback() | |
36 | */ | |
37 | hrtimer_try_to_cancel(&bctimer); | |
38 | return 0; | |
5d1638ac PM |
39 | } |
40 | ||
41 | /* | |
42 | * This is called from the guts of the broadcast code when the cpu | |
43 | * which is about to enter idle has the earliest broadcast timer event. | |
44 | */ | |
45 | static int bc_set_next(ktime_t expires, struct clock_event_device *bc) | |
46 | { | |
47 | /* | |
10cf96c6 BV |
48 | * This is called either from enter/exit idle code or from the |
49 | * broadcast handler. In all cases tick_broadcast_lock is held. | |
5d1638ac | 50 | * |
10cf96c6 BV |
51 | * hrtimer_cancel() cannot be called here neither from the |
52 | * broadcast handler nor from the enter/exit idle code. The idle | |
53 | * code can run into the problem described in bc_shutdown() and the | |
54 | * broadcast handler cannot wait for itself to complete for obvious | |
55 | * reasons. | |
a127d2bc | 56 | * |
10cf96c6 BV |
57 | * Each caller tries to arm the hrtimer on its own CPU, but if the |
58 | * hrtimer callbback function is currently running, then | |
59 | * hrtimer_start() cannot move it and the timer stays on the CPU on | |
60 | * which it is assigned at the moment. | |
61 | * | |
62 | * As this can be called from idle code, the hrtimer_start() | |
63 | * invocation has to be wrapped with RCU_NONIDLE() as | |
64 | * hrtimer_start() can call into tracing. | |
5d1638ac | 65 | */ |
10cf96c6 BV |
66 | RCU_NONIDLE( { |
67 | hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED); | |
68 | /* | |
69 | * The core tick broadcast mode expects bc->bound_on to be set | |
70 | * correctly to prevent a CPU which has the broadcast hrtimer | |
71 | * armed from going deep idle. | |
72 | * | |
73 | * As tick_broadcast_lock is held, nothing can change the cpu | |
74 | * base which was just established in hrtimer_start() above. So | |
75 | * the below access is safe even without holding the hrtimer | |
76 | * base lock. | |
77 | */ | |
78 | bc->bound_on = bctimer.base->cpu_base->cpu; | |
79 | } ); | |
5d1638ac PM |
80 | return 0; |
81 | } | |
82 | ||
83 | static struct clock_event_device ce_broadcast_hrtimer = { | |
51302137 | 84 | .name = "bc_hrtimer", |
ecbebcb8 | 85 | .set_state_shutdown = bc_shutdown, |
5d1638ac PM |
86 | .set_next_ktime = bc_set_next, |
87 | .features = CLOCK_EVT_FEAT_ONESHOT | | |
88 | CLOCK_EVT_FEAT_KTIME | | |
89 | CLOCK_EVT_FEAT_HRTIMER, | |
90 | .rating = 0, | |
91 | .bound_on = -1, | |
92 | .min_delta_ns = 1, | |
93 | .max_delta_ns = KTIME_MAX, | |
94 | .min_delta_ticks = 1, | |
849401b6 | 95 | .max_delta_ticks = ULONG_MAX, |
5d1638ac PM |
96 | .mult = 1, |
97 | .shift = 0, | |
98 | .cpumask = cpu_all_mask, | |
99 | }; | |
100 | ||
101 | static enum hrtimer_restart bc_handler(struct hrtimer *t) | |
102 | { | |
103 | ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer); | |
104 | ||
ecbebcb8 | 105 | return HRTIMER_NORESTART; |
5d1638ac PM |
106 | } |
107 | ||
108 | void tick_setup_hrtimer_broadcast(void) | |
109 | { | |
110 | hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); | |
111 | bctimer.function = bc_handler; | |
112 | clockevents_register_device(&ce_broadcast_hrtimer); | |
113 | } |