Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5d1638ac | 2 | /* |
58c5fc2b | 3 | * Emulate a local clock event device via a pseudo clock device. |
5d1638ac PM |
4 | */ |
5 | #include <linux/cpu.h> | |
6 | #include <linux/err.h> | |
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> | |
15 | ||
16 | #include "tick-internal.h" | |
17 | ||
18 | static struct hrtimer bctimer; | |
19 | ||
ecbebcb8 | 20 | static int bc_shutdown(struct clock_event_device *evt) |
5d1638ac | 21 | { |
ecbebcb8 VK |
22 | /* |
23 | * Note, we cannot cancel the timer here as we might | |
24 | * run into the following live lock scenario: | |
25 | * | |
26 | * cpu 0 cpu1 | |
27 | * lock(broadcast_lock); | |
28 | * hrtimer_interrupt() | |
29 | * bc_handler() | |
30 | * tick_handle_oneshot_broadcast(); | |
31 | * lock(broadcast_lock); | |
32 | * hrtimer_cancel() | |
33 | * wait_for_callback() | |
34 | */ | |
35 | hrtimer_try_to_cancel(&bctimer); | |
36 | return 0; | |
5d1638ac PM |
37 | } |
38 | ||
39 | /* | |
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. | |
42 | */ | |
43 | static int bc_set_next(ktime_t expires, struct clock_event_device *bc) | |
44 | { | |
a127d2bc | 45 | int bc_moved; |
5d1638ac PM |
46 | /* |
47 | * We try to cancel the timer first. If the callback is on | |
48 | * flight on some other cpu then we let it handle it. If we | |
49 | * were able to cancel the timer nothing can rearm it as we | |
50 | * own broadcast_lock. | |
51 | * | |
52 | * However we can also be called from the event handler of | |
53 | * ce_broadcast_hrtimer itself when it expires. We cannot | |
54 | * restart the timer because we are in the callback, but we | |
55 | * can set the expiry time and let the callback return | |
56 | * HRTIMER_RESTART. | |
a127d2bc PM |
57 | * |
58 | * Since we are in the idle loop at this point and because | |
59 | * hrtimer_{start/cancel} functions call into tracing, | |
60 | * calls to these functions must be bound within RCU_NONIDLE. | |
5d1638ac | 61 | */ |
902a9f9c SAS |
62 | RCU_NONIDLE( |
63 | { | |
b8a62f1f | 64 | bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0; |
902a9f9c | 65 | if (bc_moved) { |
b8a62f1f | 66 | hrtimer_start(&bctimer, expires, |
902a9f9c SAS |
67 | HRTIMER_MODE_ABS_PINNED_HARD); |
68 | } | |
69 | } | |
70 | ); | |
71 | ||
a127d2bc | 72 | if (bc_moved) { |
5d1638ac PM |
73 | /* Bind the "device" to the cpu */ |
74 | bc->bound_on = smp_processor_id(); | |
75 | } else if (bc->bound_on == smp_processor_id()) { | |
76 | hrtimer_set_expires(&bctimer, expires); | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static struct clock_event_device ce_broadcast_hrtimer = { | |
51302137 | 82 | .name = "bc_hrtimer", |
ecbebcb8 | 83 | .set_state_shutdown = bc_shutdown, |
5d1638ac PM |
84 | .set_next_ktime = bc_set_next, |
85 | .features = CLOCK_EVT_FEAT_ONESHOT | | |
86 | CLOCK_EVT_FEAT_KTIME | | |
87 | CLOCK_EVT_FEAT_HRTIMER, | |
88 | .rating = 0, | |
89 | .bound_on = -1, | |
90 | .min_delta_ns = 1, | |
91 | .max_delta_ns = KTIME_MAX, | |
92 | .min_delta_ticks = 1, | |
849401b6 | 93 | .max_delta_ticks = ULONG_MAX, |
5d1638ac PM |
94 | .mult = 1, |
95 | .shift = 0, | |
234b3840 | 96 | .cpumask = cpu_possible_mask, |
5d1638ac PM |
97 | }; |
98 | ||
99 | static enum hrtimer_restart bc_handler(struct hrtimer *t) | |
100 | { | |
101 | ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer); | |
102 | ||
ecbebcb8 | 103 | if (clockevent_state_oneshot(&ce_broadcast_hrtimer)) |
2456e855 | 104 | if (ce_broadcast_hrtimer.next_event != KTIME_MAX) |
38d23a6c | 105 | return HRTIMER_RESTART; |
ecbebcb8 VK |
106 | |
107 | return HRTIMER_NORESTART; | |
5d1638ac PM |
108 | } |
109 | ||
110 | void tick_setup_hrtimer_broadcast(void) | |
111 | { | |
902a9f9c | 112 | hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); |
5d1638ac PM |
113 | bctimer.function = bc_handler; |
114 | clockevents_register_device(&ce_broadcast_hrtimer); | |
115 | } |