Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 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 | |
Viresh Kumar | ecbebcb | 2015-07-16 16:56:35 +0530 | [diff] [blame] | 22 | static int bc_shutdown(struct clock_event_device *evt) |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 23 | { |
Viresh Kumar | ecbebcb | 2015-07-16 16:56:35 +0530 | [diff] [blame] | 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; |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 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 | { |
Preeti U Murthy | a127d2b | 2015-03-18 16:19:27 +0530 | [diff] [blame] | 47 | int bc_moved; |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 48 | /* |
| 49 | * We try to cancel the timer first. If the callback is on |
| 50 | * flight on some other cpu then we let it handle it. If we |
| 51 | * were able to cancel the timer nothing can rearm it as we |
| 52 | * own broadcast_lock. |
| 53 | * |
| 54 | * However we can also be called from the event handler of |
| 55 | * ce_broadcast_hrtimer itself when it expires. We cannot |
| 56 | * restart the timer because we are in the callback, but we |
| 57 | * can set the expiry time and let the callback return |
| 58 | * HRTIMER_RESTART. |
Preeti U Murthy | a127d2b | 2015-03-18 16:19:27 +0530 | [diff] [blame] | 59 | * |
| 60 | * Since we are in the idle loop at this point and because |
| 61 | * hrtimer_{start/cancel} functions call into tracing, |
| 62 | * calls to these functions must be bound within RCU_NONIDLE. |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 63 | */ |
Thomas Gleixner | b8a62f1 | 2015-04-14 21:09:22 +0000 | [diff] [blame] | 64 | RCU_NONIDLE({ |
| 65 | bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0; |
| 66 | if (bc_moved) |
| 67 | hrtimer_start(&bctimer, expires, |
| 68 | HRTIMER_MODE_ABS_PINNED);}); |
Preeti U Murthy | a127d2b | 2015-03-18 16:19:27 +0530 | [diff] [blame] | 69 | if (bc_moved) { |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 70 | /* Bind the "device" to the cpu */ |
| 71 | bc->bound_on = smp_processor_id(); |
| 72 | } else if (bc->bound_on == smp_processor_id()) { |
| 73 | hrtimer_set_expires(&bctimer, expires); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static struct clock_event_device ce_broadcast_hrtimer = { |
Jisheng Zhang | 5130213 | 2016-07-05 16:57:51 +0800 | [diff] [blame] | 79 | .name = "bc_hrtimer", |
Viresh Kumar | ecbebcb | 2015-07-16 16:56:35 +0530 | [diff] [blame] | 80 | .set_state_shutdown = bc_shutdown, |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 81 | .set_next_ktime = bc_set_next, |
| 82 | .features = CLOCK_EVT_FEAT_ONESHOT | |
| 83 | CLOCK_EVT_FEAT_KTIME | |
| 84 | CLOCK_EVT_FEAT_HRTIMER, |
| 85 | .rating = 0, |
| 86 | .bound_on = -1, |
| 87 | .min_delta_ns = 1, |
| 88 | .max_delta_ns = KTIME_MAX, |
| 89 | .min_delta_ticks = 1, |
Preeti U Murthy | 849401b | 2014-02-09 11:32:22 +0530 | [diff] [blame] | 90 | .max_delta_ticks = ULONG_MAX, |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 91 | .mult = 1, |
| 92 | .shift = 0, |
| 93 | .cpumask = cpu_all_mask, |
| 94 | }; |
| 95 | |
| 96 | static enum hrtimer_restart bc_handler(struct hrtimer *t) |
| 97 | { |
| 98 | ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer); |
| 99 | |
Viresh Kumar | ecbebcb | 2015-07-16 16:56:35 +0530 | [diff] [blame] | 100 | if (clockevent_state_oneshot(&ce_broadcast_hrtimer)) |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 101 | if (ce_broadcast_hrtimer.next_event != KTIME_MAX) |
Andreas Sandberg | 38d23a6 | 2015-04-24 13:06:05 +0000 | [diff] [blame] | 102 | return HRTIMER_RESTART; |
Viresh Kumar | ecbebcb | 2015-07-16 16:56:35 +0530 | [diff] [blame] | 103 | |
| 104 | return HRTIMER_NORESTART; |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void tick_setup_hrtimer_broadcast(void) |
| 108 | { |
| 109 | hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 110 | bctimer.function = bc_handler; |
| 111 | clockevents_register_device(&ce_broadcast_hrtimer); |
| 112 | } |