Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Emulate a local clock event device via a pseudo clock device.
0004  */
0005 #include <linux/cpu.h>
0006 #include <linux/err.h>
0007 #include <linux/hrtimer.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/percpu.h>
0010 #include <linux/profile.h>
0011 #include <linux/clockchips.h>
0012 #include <linux/sched.h>
0013 #include <linux/smp.h>
0014 #include <linux/module.h>
0015 
0016 #include "tick-internal.h"
0017 
0018 static struct hrtimer bctimer;
0019 
0020 static int bc_shutdown(struct clock_event_device *evt)
0021 {
0022     /*
0023      * Note, we cannot cancel the timer here as we might
0024      * run into the following live lock scenario:
0025      *
0026      * cpu 0        cpu1
0027      * lock(broadcast_lock);
0028      *          hrtimer_interrupt()
0029      *          bc_handler()
0030      *             tick_handle_oneshot_broadcast();
0031      *              lock(broadcast_lock);
0032      * hrtimer_cancel()
0033      *  wait_for_callback()
0034      */
0035     hrtimer_try_to_cancel(&bctimer);
0036     return 0;
0037 }
0038 
0039 /*
0040  * This is called from the guts of the broadcast code when the cpu
0041  * which is about to enter idle has the earliest broadcast timer event.
0042  */
0043 static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
0044 {
0045     /*
0046      * This is called either from enter/exit idle code or from the
0047      * broadcast handler. In all cases tick_broadcast_lock is held.
0048      *
0049      * hrtimer_cancel() cannot be called here neither from the
0050      * broadcast handler nor from the enter/exit idle code. The idle
0051      * code can run into the problem described in bc_shutdown() and the
0052      * broadcast handler cannot wait for itself to complete for obvious
0053      * reasons.
0054      *
0055      * Each caller tries to arm the hrtimer on its own CPU, but if the
0056      * hrtimer callback function is currently running, then
0057      * hrtimer_start() cannot move it and the timer stays on the CPU on
0058      * which it is assigned at the moment.
0059      *
0060      * As this can be called from idle code, the hrtimer_start()
0061      * invocation has to be wrapped with RCU_NONIDLE() as
0062      * hrtimer_start() can call into tracing.
0063      */
0064     RCU_NONIDLE( {
0065         hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED_HARD);
0066         /*
0067          * The core tick broadcast mode expects bc->bound_on to be set
0068          * correctly to prevent a CPU which has the broadcast hrtimer
0069          * armed from going deep idle.
0070          *
0071          * As tick_broadcast_lock is held, nothing can change the cpu
0072          * base which was just established in hrtimer_start() above. So
0073          * the below access is safe even without holding the hrtimer
0074          * base lock.
0075          */
0076         bc->bound_on = bctimer.base->cpu_base->cpu;
0077     } );
0078     return 0;
0079 }
0080 
0081 static struct clock_event_device ce_broadcast_hrtimer = {
0082     .name           = "bc_hrtimer",
0083     .set_state_shutdown = bc_shutdown,
0084     .set_next_ktime     = bc_set_next,
0085     .features       = CLOCK_EVT_FEAT_ONESHOT |
0086                   CLOCK_EVT_FEAT_KTIME |
0087                   CLOCK_EVT_FEAT_HRTIMER,
0088     .rating         = 0,
0089     .bound_on       = -1,
0090     .min_delta_ns       = 1,
0091     .max_delta_ns       = KTIME_MAX,
0092     .min_delta_ticks    = 1,
0093     .max_delta_ticks    = ULONG_MAX,
0094     .mult           = 1,
0095     .shift          = 0,
0096     .cpumask        = cpu_possible_mask,
0097 };
0098 
0099 static enum hrtimer_restart bc_handler(struct hrtimer *t)
0100 {
0101     ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
0102 
0103     return HRTIMER_NORESTART;
0104 }
0105 
0106 void tick_setup_hrtimer_broadcast(void)
0107 {
0108     hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
0109     bctimer.function = bc_handler;
0110     clockevents_register_device(&ce_broadcast_hrtimer);
0111 }