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