1 /* 2 * linux/kernel/time/tick-broadcast-hrtimer.c 3 * This file emulates a local clock event device 4 * via a pseudo clock device. 5 */ 6 #include <linux/cpu.h> 7 #include <linux/err.h> 8 #include <linux/hrtimer.h> 9 #include <linux/interrupt.h> 10 #include <linux/percpu.h> 11 #include <linux/profile.h> 12 #include <linux/clockchips.h> 13 #include <linux/sched.h> 14 #include <linux/smp.h> 15 #include <linux/module.h> 16 17 #include "tick-internal.h" 18 19 static struct hrtimer bctimer; 20 21 static void bc_set_mode(enum clock_event_mode mode, 22 struct clock_event_device *bc) 23 { 24 switch (mode) { 25 case CLOCK_EVT_MODE_UNUSED: 26 case CLOCK_EVT_MODE_SHUTDOWN: 27 /* 28 * Note, we cannot cancel the timer here as we might 29 * run into the following live lock scenario: 30 * 31 * cpu 0 cpu1 32 * lock(broadcast_lock); 33 * hrtimer_interrupt() 34 * bc_handler() 35 * tick_handle_oneshot_broadcast(); 36 * lock(broadcast_lock); 37 * hrtimer_cancel() 38 * wait_for_callback() 39 */ 40 hrtimer_try_to_cancel(&bctimer); 41 break; 42 default: 43 break; 44 } 45 } 46 47 /* 48 * This is called from the guts of the broadcast code when the cpu 49 * which is about to enter idle has the earliest broadcast timer event. 50 */ 51 static int bc_set_next(ktime_t expires, struct clock_event_device *bc) 52 { 53 int bc_moved; 54 /* 55 * We try to cancel the timer first. If the callback is on 56 * flight on some other cpu then we let it handle it. If we 57 * were able to cancel the timer nothing can rearm it as we 58 * own broadcast_lock. 59 * 60 * However we can also be called from the event handler of 61 * ce_broadcast_hrtimer itself when it expires. We cannot 62 * restart the timer because we are in the callback, but we 63 * can set the expiry time and let the callback return 64 * HRTIMER_RESTART. 65 * 66 * Since we are in the idle loop at this point and because 67 * hrtimer_{start/cancel} functions call into tracing, 68 * calls to these functions must be bound within RCU_NONIDLE. 69 */ 70 RCU_NONIDLE({ 71 bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0; 72 if (bc_moved) 73 hrtimer_start(&bctimer, expires, 74 HRTIMER_MODE_ABS_PINNED);}); 75 if (bc_moved) { 76 /* Bind the "device" to the cpu */ 77 bc->bound_on = smp_processor_id(); 78 } else if (bc->bound_on == smp_processor_id()) { 79 hrtimer_set_expires(&bctimer, expires); 80 } 81 return 0; 82 } 83 84 static struct clock_event_device ce_broadcast_hrtimer = { 85 .set_mode = bc_set_mode, 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, 95 .max_delta_ticks = ULONG_MAX, 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 105 switch (ce_broadcast_hrtimer.mode) { 106 case CLOCK_EVT_MODE_ONESHOT: 107 if (ce_broadcast_hrtimer.next_event.tv64 != KTIME_MAX) 108 return HRTIMER_RESTART; 109 default: 110 return HRTIMER_NORESTART; 111 } 112 } 113 114 void tick_setup_hrtimer_broadcast(void) 115 { 116 hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 117 bctimer.function = bc_handler; 118 clockevents_register_device(&ce_broadcast_hrtimer); 119 } 120