Thomas Gleixner | 35728b8 | 2018-10-31 19:21:09 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 2 | /* |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 3 | * This file contains functions which emulate a local clock-event |
| 4 | * device via a broadcast event source. |
| 5 | * |
| 6 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
| 7 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
| 8 | * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/cpu.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/hrtimer.h> |
Russell King | d7b9068 | 2008-04-17 07:46:24 +0200 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 14 | #include <linux/percpu.h> |
| 15 | #include <linux/profile.h> |
| 16 | #include <linux/sched.h> |
Mark Rutland | 12ad100 | 2013-01-14 17:05:22 +0000 | [diff] [blame] | 17 | #include <linux/smp.h> |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 18 | #include <linux/module.h> |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 19 | |
| 20 | #include "tick-internal.h" |
| 21 | |
| 22 | /* |
| 23 | * Broadcast support for broken x86 hardware, where the local apic |
| 24 | * timer stops in C3 state. |
| 25 | */ |
| 26 | |
Dmitri Vorobiev | a52f5c5 | 2009-05-01 13:10:21 -0700 | [diff] [blame] | 27 | static struct tick_device tick_broadcast_device; |
Waiman Long | 668802c | 2017-01-30 12:57:43 -0500 | [diff] [blame] | 28 | static cpumask_var_t tick_broadcast_mask __cpumask_var_read_mostly; |
| 29 | static cpumask_var_t tick_broadcast_on __cpumask_var_read_mostly; |
| 30 | static cpumask_var_t tmpmask __cpumask_var_read_mostly; |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 31 | static int tick_broadcast_forced; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 32 | |
Waiman Long | 668802c | 2017-01-30 12:57:43 -0500 | [diff] [blame] | 33 | static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(tick_broadcast_lock); |
| 34 | |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 35 | #ifdef CONFIG_TICK_ONESHOT |
Stephen Boyd | 94114c3 | 2017-06-07 23:36:03 -0700 | [diff] [blame] | 36 | static void tick_broadcast_setup_oneshot(struct clock_event_device *bc); |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 37 | static void tick_broadcast_clear_oneshot(int cpu); |
Thomas Gleixner | 080873c | 2015-03-25 13:09:55 +0100 | [diff] [blame] | 38 | static void tick_resume_broadcast_oneshot(struct clock_event_device *bc); |
Borislav Petkov | aba09543 | 2019-03-29 11:28:52 +0100 | [diff] [blame] | 39 | # ifdef CONFIG_HOTPLUG_CPU |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 40 | static void tick_broadcast_oneshot_offline(unsigned int cpu); |
Borislav Petkov | aba09543 | 2019-03-29 11:28:52 +0100 | [diff] [blame] | 41 | # endif |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 42 | #else |
Stephen Boyd | 94114c3 | 2017-06-07 23:36:03 -0700 | [diff] [blame] | 43 | static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc) { BUG(); } |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 44 | static inline void tick_broadcast_clear_oneshot(int cpu) { } |
Thomas Gleixner | 080873c | 2015-03-25 13:09:55 +0100 | [diff] [blame] | 45 | static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { } |
Borislav Petkov | aba09543 | 2019-03-29 11:28:52 +0100 | [diff] [blame] | 46 | # ifdef CONFIG_HOTPLUG_CPU |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 47 | static inline void tick_broadcast_oneshot_offline(unsigned int cpu) { } |
Borislav Petkov | aba09543 | 2019-03-29 11:28:52 +0100 | [diff] [blame] | 48 | # endif |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 49 | #endif |
| 50 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 51 | /* |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 52 | * Debugging: see timer_list.c |
| 53 | */ |
| 54 | struct tick_device *tick_get_broadcast_device(void) |
| 55 | { |
| 56 | return &tick_broadcast_device; |
| 57 | } |
| 58 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 59 | struct cpumask *tick_get_broadcast_mask(void) |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 60 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 61 | return tick_broadcast_mask; |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 65 | * Start the device in periodic mode |
| 66 | */ |
| 67 | static void tick_broadcast_start_periodic(struct clock_event_device *bc) |
| 68 | { |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 69 | if (bc) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 70 | tick_setup_periodic(bc, 1); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Check, if the device can be utilized as broadcast device: |
| 75 | */ |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 76 | static bool tick_check_broadcast_device(struct clock_event_device *curdev, |
| 77 | struct clock_event_device *newdev) |
| 78 | { |
| 79 | if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) || |
Soren Brinkmann | 245a349 | 2013-09-18 11:48:37 -0700 | [diff] [blame] | 80 | (newdev->features & CLOCK_EVT_FEAT_PERCPU) || |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 81 | (newdev->features & CLOCK_EVT_FEAT_C3STOP)) |
| 82 | return false; |
| 83 | |
| 84 | if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT && |
| 85 | !(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) |
| 86 | return false; |
| 87 | |
| 88 | return !curdev || newdev->rating > curdev->rating; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Conditionally install/replace broadcast device |
| 93 | */ |
Thomas Gleixner | 7172a286 | 2013-04-25 20:31:47 +0000 | [diff] [blame] | 94 | void tick_install_broadcast_device(struct clock_event_device *dev) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 95 | { |
Thomas Gleixner | 6f7a05d | 2013-04-25 11:45:53 +0200 | [diff] [blame] | 96 | struct clock_event_device *cur = tick_broadcast_device.evtdev; |
| 97 | |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 98 | if (!tick_check_broadcast_device(cur, dev)) |
Thomas Gleixner | 7172a286 | 2013-04-25 20:31:47 +0000 | [diff] [blame] | 99 | return; |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 100 | |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 101 | if (!try_module_get(dev->owner)) |
| 102 | return; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 103 | |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 104 | clockevents_exchange_device(cur, dev); |
Thomas Gleixner | 6f7a05d | 2013-04-25 11:45:53 +0200 | [diff] [blame] | 105 | if (cur) |
| 106 | cur->event_handler = clockevents_handle_noop; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 107 | tick_broadcast_device.evtdev = dev; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 108 | if (!cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 109 | tick_broadcast_start_periodic(dev); |
Stephen Boyd | c038c1c | 2013-04-17 10:26:06 -0700 | [diff] [blame] | 110 | /* |
| 111 | * Inform all cpus about this. We might be in a situation |
| 112 | * where we did not switch to oneshot mode because the per cpu |
| 113 | * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack |
| 114 | * of a oneshot capable broadcast device. Without that |
| 115 | * notification the systems stays stuck in periodic mode |
| 116 | * forever. |
| 117 | */ |
| 118 | if (dev->features & CLOCK_EVT_FEAT_ONESHOT) |
| 119 | tick_clock_notify(); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Check, if the device is the broadcast device |
| 124 | */ |
| 125 | int tick_is_broadcast_device(struct clock_event_device *dev) |
| 126 | { |
| 127 | return (dev && tick_broadcast_device.evtdev == dev); |
| 128 | } |
| 129 | |
Thomas Gleixner | 627ee79 | 2014-02-03 14:34:31 -0800 | [diff] [blame] | 130 | int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq) |
| 131 | { |
| 132 | int ret = -ENODEV; |
| 133 | |
| 134 | if (tick_is_broadcast_device(dev)) { |
| 135 | raw_spin_lock(&tick_broadcast_lock); |
| 136 | ret = __clockevents_update_freq(dev, freq); |
| 137 | raw_spin_unlock(&tick_broadcast_lock); |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | |
Mark Rutland | 12ad100 | 2013-01-14 17:05:22 +0000 | [diff] [blame] | 143 | static void err_broadcast(const struct cpumask *mask) |
| 144 | { |
| 145 | pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n"); |
| 146 | } |
| 147 | |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 148 | static void tick_device_setup_broadcast_func(struct clock_event_device *dev) |
| 149 | { |
| 150 | if (!dev->broadcast) |
| 151 | dev->broadcast = tick_broadcast; |
| 152 | if (!dev->broadcast) { |
| 153 | pr_warn_once("%s depends on broadcast, but no broadcast function available\n", |
| 154 | dev->name); |
| 155 | dev->broadcast = err_broadcast; |
| 156 | } |
| 157 | } |
| 158 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 159 | /* |
| 160 | * Check, if the device is disfunctional and a place holder, which |
| 161 | * needs to be handled by the broadcast device. |
| 162 | */ |
| 163 | int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) |
| 164 | { |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 165 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 166 | unsigned long flags; |
Thomas Gleixner | e045431 | 2015-07-07 14:07:27 +0200 | [diff] [blame] | 167 | int ret = 0; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 168 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 169 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Devices might be registered with both periodic and oneshot |
| 173 | * mode disabled. This signals, that the device needs to be |
| 174 | * operated from the broadcast device and is a placeholder for |
| 175 | * the cpu local device. |
| 176 | */ |
| 177 | if (!tick_device_is_functional(dev)) { |
| 178 | dev->event_handler = tick_handle_periodic; |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 179 | tick_device_setup_broadcast_func(dev); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 180 | cpumask_set_cpu(cpu, tick_broadcast_mask); |
Stephen Boyd | a272dcc | 2013-07-11 07:00:59 -0700 | [diff] [blame] | 181 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
| 182 | tick_broadcast_start_periodic(bc); |
| 183 | else |
| 184 | tick_broadcast_setup_oneshot(bc); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 185 | ret = 1; |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 186 | } else { |
| 187 | /* |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 188 | * Clear the broadcast bit for this cpu if the |
| 189 | * device is not power state affected. |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 190 | */ |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 191 | if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 192 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 193 | else |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 194 | tick_device_setup_broadcast_func(dev); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Clear the broadcast bit if the CPU is not in |
| 198 | * periodic broadcast on state. |
| 199 | */ |
| 200 | if (!cpumask_test_cpu(cpu, tick_broadcast_on)) |
| 201 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
| 202 | |
| 203 | switch (tick_broadcast_device.mode) { |
| 204 | case TICKDEV_MODE_ONESHOT: |
| 205 | /* |
| 206 | * If the system is in oneshot mode we can |
| 207 | * unconditionally clear the oneshot mask bit, |
| 208 | * because the CPU is running and therefore |
| 209 | * not in an idle state which causes the power |
| 210 | * state affected device to stop. Let the |
| 211 | * caller initialize the device. |
| 212 | */ |
| 213 | tick_broadcast_clear_oneshot(cpu); |
| 214 | ret = 0; |
| 215 | break; |
| 216 | |
| 217 | case TICKDEV_MODE_PERIODIC: |
| 218 | /* |
| 219 | * If the system is in periodic mode, check |
| 220 | * whether the broadcast device can be |
| 221 | * switched off now. |
| 222 | */ |
| 223 | if (cpumask_empty(tick_broadcast_mask) && bc) |
| 224 | clockevents_shutdown(bc); |
| 225 | /* |
| 226 | * If we kept the cpu in the broadcast mask, |
| 227 | * tell the caller to leave the per cpu device |
| 228 | * in shutdown state. The periodic interrupt |
Thomas Gleixner | e045431 | 2015-07-07 14:07:27 +0200 | [diff] [blame] | 229 | * is delivered by the broadcast device, if |
| 230 | * the broadcast device exists and is not |
| 231 | * hrtimer based. |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 232 | */ |
Thomas Gleixner | e045431 | 2015-07-07 14:07:27 +0200 | [diff] [blame] | 233 | if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER)) |
| 234 | ret = cpumask_test_cpu(cpu, tick_broadcast_mask); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 235 | break; |
| 236 | default: |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 237 | break; |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 240 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 241 | return ret; |
| 242 | } |
| 243 | |
Mark Rutland | 12572db | 2013-01-14 17:05:21 +0000 | [diff] [blame] | 244 | #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST |
| 245 | int tick_receive_broadcast(void) |
| 246 | { |
| 247 | struct tick_device *td = this_cpu_ptr(&tick_cpu_device); |
| 248 | struct clock_event_device *evt = td->evtdev; |
| 249 | |
| 250 | if (!evt) |
| 251 | return -ENODEV; |
| 252 | |
| 253 | if (!evt->event_handler) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | evt->event_handler(evt); |
| 257 | return 0; |
| 258 | } |
| 259 | #endif |
| 260 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 261 | /* |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 262 | * Broadcast the event to the cpus, which are set in the mask (mangled). |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 263 | */ |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 264 | static bool tick_do_broadcast(struct cpumask *mask) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 265 | { |
Thomas Gleixner | 186e3cb | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 266 | int cpu = smp_processor_id(); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 267 | struct tick_device *td; |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 268 | bool local = false; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * Check, if the current cpu is in the mask |
| 272 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 273 | if (cpumask_test_cpu(cpu, mask)) { |
Thomas Gleixner | 8eb2312 | 2015-07-07 14:11:00 +0200 | [diff] [blame] | 274 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
| 275 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 276 | cpumask_clear_cpu(cpu, mask); |
Thomas Gleixner | 8eb2312 | 2015-07-07 14:11:00 +0200 | [diff] [blame] | 277 | /* |
| 278 | * We only run the local handler, if the broadcast |
| 279 | * device is not hrtimer based. Otherwise we run into |
| 280 | * a hrtimer recursion. |
| 281 | * |
| 282 | * local timer_interrupt() |
| 283 | * local_handler() |
| 284 | * expire_hrtimers() |
| 285 | * bc_handler() |
| 286 | * local_handler() |
| 287 | * expire_hrtimers() |
| 288 | */ |
| 289 | local = !(bc->features & CLOCK_EVT_FEAT_HRTIMER); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 292 | if (!cpumask_empty(mask)) { |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 293 | /* |
| 294 | * It might be necessary to actually check whether the devices |
| 295 | * have different broadcast functions. For now, just use the |
| 296 | * one of the first device. This works as long as we have this |
| 297 | * misfeature only on x86 (lapic) |
| 298 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 299 | td = &per_cpu(tick_cpu_device, cpumask_first(mask)); |
| 300 | td->evtdev->broadcast(mask); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 301 | } |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 302 | return local; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Periodic broadcast: |
| 307 | * - invoke the broadcast handlers |
| 308 | */ |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 309 | static bool tick_do_periodic_broadcast(void) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 310 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 311 | cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask); |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 312 | return tick_do_broadcast(tmpmask); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Event handler for periodic broadcast ticks |
| 317 | */ |
| 318 | static void tick_handle_periodic_broadcast(struct clock_event_device *dev) |
| 319 | { |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 320 | struct tick_device *td = this_cpu_ptr(&tick_cpu_device); |
| 321 | bool bc_local; |
Thomas Gleixner | d4496b3 | 2008-09-03 21:36:57 +0000 | [diff] [blame] | 322 | |
Thomas Gleixner | 627ee79 | 2014-02-03 14:34:31 -0800 | [diff] [blame] | 323 | raw_spin_lock(&tick_broadcast_lock); |
Thomas Gleixner | c428833 | 2015-07-05 20:53:17 +0000 | [diff] [blame] | 324 | |
| 325 | /* Handle spurious interrupts gracefully */ |
| 326 | if (clockevent_state_shutdown(tick_broadcast_device.evtdev)) { |
| 327 | raw_spin_unlock(&tick_broadcast_lock); |
| 328 | return; |
| 329 | } |
| 330 | |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 331 | bc_local = tick_do_periodic_broadcast(); |
Thomas Gleixner | 627ee79 | 2014-02-03 14:34:31 -0800 | [diff] [blame] | 332 | |
Viresh Kumar | 472c4a9 | 2015-05-21 13:33:46 +0530 | [diff] [blame] | 333 | if (clockevent_state_oneshot(dev)) { |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 334 | ktime_t next = ktime_add(dev->next_event, tick_period); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 335 | |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 336 | clockevents_program_event(dev, next, true); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 337 | } |
Thomas Gleixner | 627ee79 | 2014-02-03 14:34:31 -0800 | [diff] [blame] | 338 | raw_spin_unlock(&tick_broadcast_lock); |
Thomas Gleixner | 2951d5c | 2015-05-05 10:00:13 +0200 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * We run the handler of the local cpu after dropping |
| 342 | * tick_broadcast_lock because the handler might deadlock when |
| 343 | * trying to switch to oneshot mode. |
| 344 | */ |
| 345 | if (bc_local) |
| 346 | td->evtdev->event_handler(td->evtdev); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 349 | /** |
| 350 | * tick_broadcast_control - Enable/disable or force broadcast mode |
| 351 | * @mode: The selected broadcast mode |
| 352 | * |
| 353 | * Called when the system enters a state where affected tick devices |
| 354 | * might stop. Note: TICK_BROADCAST_FORCE cannot be undone. |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 355 | */ |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 356 | void tick_broadcast_control(enum tick_broadcast_mode mode) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 357 | { |
| 358 | struct clock_event_device *bc, *dev; |
| 359 | struct tick_device *td; |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 360 | int cpu, bc_stopped; |
Mike Galbraith | 202461e | 2017-02-13 03:31:55 +0100 | [diff] [blame] | 361 | unsigned long flags; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 362 | |
Mike Galbraith | 202461e | 2017-02-13 03:31:55 +0100 | [diff] [blame] | 363 | /* Protects also the local clockevent device. */ |
| 364 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 365 | td = this_cpu_ptr(&tick_cpu_device); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 366 | dev = td->evtdev; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 367 | |
| 368 | /* |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 369 | * Is the device not affected by the powerstate ? |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 370 | */ |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 371 | if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP)) |
Mike Galbraith | 202461e | 2017-02-13 03:31:55 +0100 | [diff] [blame] | 372 | goto out; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 373 | |
Thomas Gleixner | 3dfbc88 | 2007-10-17 18:04:32 +0200 | [diff] [blame] | 374 | if (!tick_device_is_functional(dev)) |
Mike Galbraith | 202461e | 2017-02-13 03:31:55 +0100 | [diff] [blame] | 375 | goto out; |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 376 | |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 377 | cpu = smp_processor_id(); |
| 378 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 379 | bc_stopped = cpumask_empty(tick_broadcast_mask); |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 380 | |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 381 | switch (mode) { |
| 382 | case TICK_BROADCAST_FORCE: |
| 383 | tick_broadcast_forced = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 384 | fallthrough; |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 385 | case TICK_BROADCAST_ON: |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 386 | cpumask_set_cpu(cpu, tick_broadcast_on); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 387 | if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) { |
Thomas Gleixner | e045431 | 2015-07-07 14:07:27 +0200 | [diff] [blame] | 388 | /* |
| 389 | * Only shutdown the cpu local device, if: |
| 390 | * |
| 391 | * - the broadcast device exists |
| 392 | * - the broadcast device is not a hrtimer based one |
| 393 | * - the broadcast device is in periodic mode to |
| 394 | * avoid a hickup during switch to oneshot mode |
| 395 | */ |
| 396 | if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) && |
| 397 | tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 398 | clockevents_shutdown(dev); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 399 | } |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 400 | break; |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 401 | |
| 402 | case TICK_BROADCAST_OFF: |
| 403 | if (tick_broadcast_forced) |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 404 | break; |
| 405 | cpumask_clear_cpu(cpu, tick_broadcast_on); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 406 | if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) { |
Thomas Gleixner | 07454bf | 2008-10-04 10:51:07 +0200 | [diff] [blame] | 407 | if (tick_broadcast_device.mode == |
| 408 | TICKDEV_MODE_PERIODIC) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 409 | tick_setup_periodic(dev, 0); |
| 410 | } |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 411 | break; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Thomas Gleixner | c4d029f | 2015-07-11 14:26:34 +0200 | [diff] [blame] | 414 | if (bc) { |
| 415 | if (cpumask_empty(tick_broadcast_mask)) { |
| 416 | if (!bc_stopped) |
| 417 | clockevents_shutdown(bc); |
| 418 | } else if (bc_stopped) { |
| 419 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
| 420 | tick_broadcast_start_periodic(bc); |
| 421 | else |
| 422 | tick_broadcast_setup_oneshot(bc); |
| 423 | } |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 424 | } |
Mike Galbraith | 202461e | 2017-02-13 03:31:55 +0100 | [diff] [blame] | 425 | out: |
| 426 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 427 | } |
Thomas Gleixner | 592a438 | 2015-04-03 02:01:10 +0200 | [diff] [blame] | 428 | EXPORT_SYMBOL_GPL(tick_broadcast_control); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * Set the periodic handler depending on broadcast on/off |
| 432 | */ |
| 433 | void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast) |
| 434 | { |
| 435 | if (!broadcast) |
| 436 | dev->event_handler = tick_handle_periodic; |
| 437 | else |
| 438 | dev->event_handler = tick_handle_periodic_broadcast; |
| 439 | } |
| 440 | |
Thomas Gleixner | a49b116 | 2015-04-03 02:38:05 +0200 | [diff] [blame] | 441 | #ifdef CONFIG_HOTPLUG_CPU |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 442 | static void tick_shutdown_broadcast(void) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 443 | { |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 444 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 445 | |
| 446 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 447 | if (bc && cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 448 | clockevents_shutdown(bc); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 449 | } |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 450 | } |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * Remove a CPU from broadcasting |
| 454 | */ |
| 455 | void tick_broadcast_offline(unsigned int cpu) |
| 456 | { |
| 457 | raw_spin_lock(&tick_broadcast_lock); |
| 458 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
| 459 | cpumask_clear_cpu(cpu, tick_broadcast_on); |
| 460 | tick_broadcast_oneshot_offline(cpu); |
| 461 | tick_shutdown_broadcast(); |
| 462 | raw_spin_unlock(&tick_broadcast_lock); |
| 463 | } |
| 464 | |
Thomas Gleixner | a49b116 | 2015-04-03 02:38:05 +0200 | [diff] [blame] | 465 | #endif |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 466 | |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 467 | void tick_suspend_broadcast(void) |
| 468 | { |
| 469 | struct clock_event_device *bc; |
| 470 | unsigned long flags; |
| 471 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 472 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 473 | |
| 474 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 475 | if (bc) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 476 | clockevents_shutdown(bc); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 477 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 478 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Thomas Gleixner | f46481d | 2015-03-25 13:11:04 +0100 | [diff] [blame] | 481 | /* |
| 482 | * This is called from tick_resume_local() on a resuming CPU. That's |
| 483 | * called from the core resume function, tick_unfreeze() and the magic XEN |
| 484 | * resume hackery. |
| 485 | * |
| 486 | * In none of these cases the broadcast device mode can change and the |
| 487 | * bit of the resuming CPU in the broadcast mask is safe as well. |
| 488 | */ |
| 489 | bool tick_resume_check_broadcast(void) |
| 490 | { |
| 491 | if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT) |
| 492 | return false; |
| 493 | else |
| 494 | return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask); |
| 495 | } |
| 496 | |
| 497 | void tick_resume_broadcast(void) |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 498 | { |
| 499 | struct clock_event_device *bc; |
| 500 | unsigned long flags; |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 501 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 502 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 503 | |
| 504 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 505 | |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 506 | if (bc) { |
Viresh Kumar | 554ef38 | 2015-02-27 17:21:32 +0530 | [diff] [blame] | 507 | clockevents_tick_resume(bc); |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 508 | |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 509 | switch (tick_broadcast_device.mode) { |
| 510 | case TICKDEV_MODE_PERIODIC: |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 511 | if (!cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 512 | tick_broadcast_start_periodic(bc); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 513 | break; |
| 514 | case TICKDEV_MODE_ONESHOT: |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 515 | if (!cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | 080873c | 2015-03-25 13:09:55 +0100 | [diff] [blame] | 516 | tick_resume_broadcast_oneshot(bc); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 517 | break; |
| 518 | } |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 519 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 520 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 523 | #ifdef CONFIG_TICK_ONESHOT |
| 524 | |
Waiman Long | 668802c | 2017-01-30 12:57:43 -0500 | [diff] [blame] | 525 | static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly; |
| 526 | static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly; |
| 527 | static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 528 | |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 529 | /* |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 530 | * Exposed for debugging: see timer_list.c |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 531 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 532 | struct cpumask *tick_get_broadcast_oneshot_mask(void) |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 533 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 534 | return tick_broadcast_oneshot_mask; |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 537 | /* |
Thomas Gleixner | eaa907c | 2013-03-06 11:18:36 +0000 | [diff] [blame] | 538 | * Called before going idle with interrupts disabled. Checks whether a |
| 539 | * broadcast event from the other core is about to happen. We detected |
| 540 | * that in tick_broadcast_oneshot_control(). The callsite can use this |
| 541 | * to avoid a deep idle transition as we are about to get the |
| 542 | * broadcast IPI right away. |
| 543 | */ |
| 544 | int tick_check_broadcast_expired(void) |
| 545 | { |
| 546 | return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask); |
| 547 | } |
| 548 | |
| 549 | /* |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 550 | * Set broadcast interrupt affinity |
| 551 | */ |
| 552 | static void tick_broadcast_set_affinity(struct clock_event_device *bc, |
| 553 | const struct cpumask *cpumask) |
| 554 | { |
| 555 | if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ)) |
| 556 | return; |
| 557 | |
| 558 | if (cpumask_equal(bc->cpumask, cpumask)) |
| 559 | return; |
| 560 | |
| 561 | bc->cpumask = cpumask; |
| 562 | irq_set_affinity(bc->irq, bc->cpumask); |
| 563 | } |
| 564 | |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 565 | static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu, |
| 566 | ktime_t expires) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 567 | { |
Viresh Kumar | 472c4a9 | 2015-05-21 13:33:46 +0530 | [diff] [blame] | 568 | if (!clockevent_state_oneshot(bc)) |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 569 | clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | b9a6a235 | 2012-04-18 17:31:58 +0200 | [diff] [blame] | 570 | |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 571 | clockevents_program_event(bc, expires, 1); |
| 572 | tick_broadcast_set_affinity(bc, cpumask_of(cpu)); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Thomas Gleixner | 080873c | 2015-03-25 13:09:55 +0100 | [diff] [blame] | 575 | static void tick_resume_broadcast_oneshot(struct clock_event_device *bc) |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 576 | { |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 577 | clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 580 | /* |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 581 | * Called from irq_enter() when idle was interrupted to reenable the |
| 582 | * per cpu device. |
| 583 | */ |
Frederic Weisbecker | e8fcaa5 | 2013-08-07 22:28:01 +0200 | [diff] [blame] | 584 | void tick_check_oneshot_broadcast_this_cpu(void) |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 585 | { |
Frederic Weisbecker | e8fcaa5 | 2013-08-07 22:28:01 +0200 | [diff] [blame] | 586 | if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) { |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 587 | struct tick_device *td = this_cpu_ptr(&tick_cpu_device); |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 588 | |
Thomas Gleixner | 1f73a98 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 589 | /* |
| 590 | * We might be in the middle of switching over from |
| 591 | * periodic to oneshot. If the CPU has not yet |
| 592 | * switched over, leave the device alone. |
| 593 | */ |
| 594 | if (td->mode == TICKDEV_MODE_ONESHOT) { |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 595 | clockevents_switch_state(td->evtdev, |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 596 | CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | 1f73a98 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 597 | } |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | /* |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 602 | * Handle oneshot mode broadcasting |
| 603 | */ |
| 604 | static void tick_handle_oneshot_broadcast(struct clock_event_device *dev) |
| 605 | { |
| 606 | struct tick_device *td; |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 607 | ktime_t now, next_event; |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 608 | int cpu, next_cpu = 0; |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 609 | bool bc_local; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 610 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 611 | raw_spin_lock(&tick_broadcast_lock); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 612 | dev->next_event = KTIME_MAX; |
| 613 | next_event = KTIME_MAX; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 614 | cpumask_clear(tmpmask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 615 | now = ktime_get(); |
| 616 | /* Find all expired events */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 617 | for_each_cpu(cpu, tick_broadcast_oneshot_mask) { |
Dexuan Cui | 5596fe3 | 2018-05-15 19:52:50 +0000 | [diff] [blame] | 618 | /* |
| 619 | * Required for !SMP because for_each_cpu() reports |
| 620 | * unconditionally CPU0 as set on UP kernels. |
| 621 | */ |
| 622 | if (!IS_ENABLED(CONFIG_SMP) && |
| 623 | cpumask_empty(tick_broadcast_oneshot_mask)) |
| 624 | break; |
| 625 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 626 | td = &per_cpu(tick_cpu_device, cpu); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 627 | if (td->evtdev->next_event <= now) { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 628 | cpumask_set_cpu(cpu, tmpmask); |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 629 | /* |
| 630 | * Mark the remote cpu in the pending mask, so |
| 631 | * it can avoid reprogramming the cpu local |
| 632 | * timer in tick_broadcast_oneshot_control(). |
| 633 | */ |
| 634 | cpumask_set_cpu(cpu, tick_broadcast_pending_mask); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 635 | } else if (td->evtdev->next_event < next_event) { |
| 636 | next_event = td->evtdev->next_event; |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 637 | next_cpu = cpu; |
| 638 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Thomas Gleixner | 2938d27 | 2013-05-28 09:33:01 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Remove the current cpu from the pending mask. The event is |
| 643 | * delivered immediately in tick_do_broadcast() ! |
| 644 | */ |
| 645 | cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask); |
| 646 | |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 647 | /* Take care of enforced broadcast requests */ |
| 648 | cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask); |
| 649 | cpumask_clear(tick_broadcast_force_mask); |
| 650 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 651 | /* |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 652 | * Sanity check. Catch the case where we try to broadcast to |
| 653 | * offline cpus. |
| 654 | */ |
| 655 | if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask))) |
| 656 | cpumask_and(tmpmask, tmpmask, cpu_online_mask); |
| 657 | |
| 658 | /* |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 659 | * Wakeup the cpus which have an expired event. |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 660 | */ |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 661 | bc_local = tick_do_broadcast(tmpmask); |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * Two reasons for reprogram: |
| 665 | * |
| 666 | * - The global event did not expire any CPU local |
| 667 | * events. This happens in dyntick mode, as the maximum PIT |
| 668 | * delta is quite small. |
| 669 | * |
| 670 | * - There are pending events on sleeping CPUs which were not |
| 671 | * in the event mask |
| 672 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 673 | if (next_event != KTIME_MAX) |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 674 | tick_broadcast_set_event(dev, next_cpu, next_event); |
| 675 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 676 | raw_spin_unlock(&tick_broadcast_lock); |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 677 | |
| 678 | if (bc_local) { |
| 679 | td = this_cpu_ptr(&tick_cpu_device); |
| 680 | td->evtdev->event_handler(td->evtdev); |
| 681 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 684 | static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu) |
| 685 | { |
| 686 | if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER)) |
| 687 | return 0; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 688 | if (bc->next_event == KTIME_MAX) |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 689 | return 0; |
| 690 | return bc->bound_on == cpu ? -EBUSY : 0; |
| 691 | } |
| 692 | |
| 693 | static void broadcast_shutdown_local(struct clock_event_device *bc, |
| 694 | struct clock_event_device *dev) |
| 695 | { |
| 696 | /* |
| 697 | * For hrtimer based broadcasting we cannot shutdown the cpu |
| 698 | * local device if our own event is the first one to expire or |
| 699 | * if we own the broadcast timer. |
| 700 | */ |
| 701 | if (bc->features & CLOCK_EVT_FEAT_HRTIMER) { |
| 702 | if (broadcast_needs_cpu(bc, smp_processor_id())) |
| 703 | return; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 704 | if (dev->next_event < bc->next_event) |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 705 | return; |
| 706 | } |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 707 | clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN); |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 708 | } |
| 709 | |
Thomas Gleixner | f32dd11 | 2015-07-07 16:29:38 +0200 | [diff] [blame] | 710 | int __tick_broadcast_oneshot_control(enum tick_broadcast_state state) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 711 | { |
| 712 | struct clock_event_device *bc, *dev; |
Preeti U Murthy | da7e6f4 | 2014-02-07 13:36:06 +0530 | [diff] [blame] | 713 | int cpu, ret = 0; |
Thomas Gleixner | 1fe5d5c3 | 2015-04-03 02:05:15 +0200 | [diff] [blame] | 714 | ktime_t now; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 715 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 716 | /* |
Thomas Gleixner | b78f3f3 | 2015-07-07 16:34:32 +0200 | [diff] [blame] | 717 | * If there is no broadcast device, tell the caller not to go |
| 718 | * into deep idle. |
| 719 | */ |
| 720 | if (!tick_broadcast_device.evtdev) |
| 721 | return -EBUSY; |
| 722 | |
Thomas Gleixner | e3ac79e | 2015-07-07 16:38:11 +0200 | [diff] [blame] | 723 | dev = this_cpu_ptr(&tick_cpu_device)->evtdev; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 724 | |
Thomas Gleixner | 1fe5d5c3 | 2015-04-03 02:05:15 +0200 | [diff] [blame] | 725 | raw_spin_lock(&tick_broadcast_lock); |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 726 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | 1fe5d5c3 | 2015-04-03 02:05:15 +0200 | [diff] [blame] | 727 | cpu = smp_processor_id(); |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 728 | |
Thomas Gleixner | 1fe5d5c3 | 2015-04-03 02:05:15 +0200 | [diff] [blame] | 729 | if (state == TICK_BROADCAST_ENTER) { |
Thomas Gleixner | e3ac79e | 2015-07-07 16:38:11 +0200 | [diff] [blame] | 730 | /* |
Thomas Gleixner | d5113e1 | 2015-07-07 16:43:04 +0200 | [diff] [blame] | 731 | * If the current CPU owns the hrtimer broadcast |
| 732 | * mechanism, it cannot go deep idle and we do not add |
| 733 | * the CPU to the broadcast mask. We don't have to go |
| 734 | * through the EXIT path as the local timer is not |
| 735 | * shutdown. |
| 736 | */ |
| 737 | ret = broadcast_needs_cpu(bc, cpu); |
| 738 | if (ret) |
| 739 | goto out; |
| 740 | |
| 741 | /* |
Thomas Gleixner | e3ac79e | 2015-07-07 16:38:11 +0200 | [diff] [blame] | 742 | * If the broadcast device is in periodic mode, we |
| 743 | * return. |
| 744 | */ |
Thomas Gleixner | d332572 | 2015-07-07 17:45:22 +0200 | [diff] [blame] | 745 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) { |
| 746 | /* If it is a hrtimer based broadcast, return busy */ |
| 747 | if (bc->features & CLOCK_EVT_FEAT_HRTIMER) |
| 748 | ret = -EBUSY; |
Thomas Gleixner | e3ac79e | 2015-07-07 16:38:11 +0200 | [diff] [blame] | 749 | goto out; |
Thomas Gleixner | d332572 | 2015-07-07 17:45:22 +0200 | [diff] [blame] | 750 | } |
Thomas Gleixner | e3ac79e | 2015-07-07 16:38:11 +0200 | [diff] [blame] | 751 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 752 | if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) { |
Thomas Gleixner | 2938d27 | 2013-05-28 09:33:01 +0200 | [diff] [blame] | 753 | WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask)); |
Thomas Gleixner | d5113e1 | 2015-07-07 16:43:04 +0200 | [diff] [blame] | 754 | |
| 755 | /* Conditionally shut down the local timer. */ |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 756 | broadcast_shutdown_local(bc, dev); |
Thomas Gleixner | d5113e1 | 2015-07-07 16:43:04 +0200 | [diff] [blame] | 757 | |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 758 | /* |
| 759 | * We only reprogram the broadcast timer if we |
| 760 | * did not mark ourself in the force mask and |
| 761 | * if the cpu local event is earlier than the |
| 762 | * broadcast event. If the current CPU is in |
| 763 | * the force mask, then we are going to be |
Thomas Gleixner | 0cc5281 | 2015-07-07 16:45:15 +0200 | [diff] [blame] | 764 | * woken by the IPI right away; we return |
| 765 | * busy, so the CPU does not try to go deep |
| 766 | * idle. |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 767 | */ |
Thomas Gleixner | 0cc5281 | 2015-07-07 16:45:15 +0200 | [diff] [blame] | 768 | if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) { |
| 769 | ret = -EBUSY; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 770 | } else if (dev->next_event < bc->next_event) { |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 771 | tick_broadcast_set_event(bc, cpu, dev->next_event); |
Thomas Gleixner | d5113e1 | 2015-07-07 16:43:04 +0200 | [diff] [blame] | 772 | /* |
| 773 | * In case of hrtimer broadcasts the |
| 774 | * programming might have moved the |
| 775 | * timer to this cpu. If yes, remove |
| 776 | * us from the broadcast mask and |
| 777 | * return busy. |
| 778 | */ |
| 779 | ret = broadcast_needs_cpu(bc, cpu); |
| 780 | if (ret) { |
| 781 | cpumask_clear_cpu(cpu, |
| 782 | tick_broadcast_oneshot_mask); |
| 783 | } |
Thomas Gleixner | 0cc5281 | 2015-07-07 16:45:15 +0200 | [diff] [blame] | 784 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 785 | } |
| 786 | } else { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 787 | if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) { |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 788 | clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 789 | /* |
| 790 | * The cpu which was handling the broadcast |
| 791 | * timer marked this cpu in the broadcast |
| 792 | * pending mask and fired the broadcast |
| 793 | * IPI. So we are going to handle the expired |
| 794 | * event anyway via the broadcast IPI |
| 795 | * handler. No need to reprogram the timer |
| 796 | * with an already expired event. |
| 797 | */ |
| 798 | if (cpumask_test_and_clear_cpu(cpu, |
| 799 | tick_broadcast_pending_mask)) |
| 800 | goto out; |
| 801 | |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 802 | /* |
Daniel Lezcano | ea8deb8 | 2013-06-17 18:15:35 +0200 | [diff] [blame] | 803 | * Bail out if there is no next event. |
| 804 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 805 | if (dev->next_event == KTIME_MAX) |
Daniel Lezcano | ea8deb8 | 2013-06-17 18:15:35 +0200 | [diff] [blame] | 806 | goto out; |
| 807 | /* |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 808 | * If the pending bit is not set, then we are |
| 809 | * either the CPU handling the broadcast |
| 810 | * interrupt or we got woken by something else. |
| 811 | * |
Laurent Gauthier | 13e792a | 2019-01-05 00:07:45 +0100 | [diff] [blame] | 812 | * We are no longer in the broadcast mask, so |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 813 | * if the cpu local expiry time is already |
| 814 | * reached, we would reprogram the cpu local |
| 815 | * timer with an already expired event. |
| 816 | * |
| 817 | * This can lead to a ping-pong when we return |
Laurent Gauthier | 13e792a | 2019-01-05 00:07:45 +0100 | [diff] [blame] | 818 | * to idle and therefore rearm the broadcast |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 819 | * timer before the cpu local timer was able |
| 820 | * to fire. This happens because the forced |
| 821 | * reprogramming makes sure that the event |
| 822 | * will happen in the future and depending on |
| 823 | * the min_delta setting this might be far |
| 824 | * enough out that the ping-pong starts. |
| 825 | * |
| 826 | * If the cpu local next_event has expired |
| 827 | * then we know that the broadcast timer |
| 828 | * next_event has expired as well and |
| 829 | * broadcast is about to be handled. So we |
| 830 | * avoid reprogramming and enforce that the |
| 831 | * broadcast handler, which did not run yet, |
| 832 | * will invoke the cpu local handler. |
| 833 | * |
| 834 | * We cannot call the handler directly from |
| 835 | * here, because we might be in a NOHZ phase |
| 836 | * and we did not go through the irq_enter() |
| 837 | * nohz fixups. |
| 838 | */ |
| 839 | now = ktime_get(); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 840 | if (dev->next_event <= now) { |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 841 | cpumask_set_cpu(cpu, tick_broadcast_force_mask); |
| 842 | goto out; |
| 843 | } |
| 844 | /* |
| 845 | * We got woken by something else. Reprogram |
| 846 | * the cpu local timer device. |
| 847 | */ |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 848 | tick_program_event(dev->next_event, 1); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 851 | out: |
Thomas Gleixner | 1fe5d5c3 | 2015-04-03 02:05:15 +0200 | [diff] [blame] | 852 | raw_spin_unlock(&tick_broadcast_lock); |
Preeti U Murthy | da7e6f4 | 2014-02-07 13:36:06 +0530 | [diff] [blame] | 853 | return ret; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 856 | /* |
| 857 | * Reset the one shot broadcast for a cpu |
| 858 | * |
| 859 | * Called with tick_broadcast_lock held |
| 860 | */ |
| 861 | static void tick_broadcast_clear_oneshot(int cpu) |
| 862 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 863 | cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask); |
Thomas Gleixner | dd5fd9b | 2014-02-11 14:35:40 +0100 | [diff] [blame] | 864 | cpumask_clear_cpu(cpu, tick_broadcast_pending_mask); |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 867 | static void tick_broadcast_init_next_event(struct cpumask *mask, |
| 868 | ktime_t expires) |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 869 | { |
| 870 | struct tick_device *td; |
| 871 | int cpu; |
| 872 | |
Rusty Russell | 5db0e1e | 2009-01-01 10:12:29 +1030 | [diff] [blame] | 873 | for_each_cpu(cpu, mask) { |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 874 | td = &per_cpu(tick_cpu_device, cpu); |
| 875 | if (td->evtdev) |
| 876 | td->evtdev->next_event = expires; |
| 877 | } |
| 878 | } |
| 879 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 880 | /** |
Li Zefan | 8dce39c | 2007-11-05 14:51:10 -0800 | [diff] [blame] | 881 | * tick_broadcast_setup_oneshot - setup the broadcast device |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 882 | */ |
Stephen Boyd | 94114c3 | 2017-06-07 23:36:03 -0700 | [diff] [blame] | 883 | static void tick_broadcast_setup_oneshot(struct clock_event_device *bc) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 884 | { |
Thomas Gleixner | 07f4beb | 2011-05-16 11:07:48 +0200 | [diff] [blame] | 885 | int cpu = smp_processor_id(); |
| 886 | |
Thomas Gleixner | c1a9eeb | 2016-12-15 12:10:37 +0100 | [diff] [blame] | 887 | if (!bc) |
| 888 | return; |
| 889 | |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 890 | /* Set it up only once ! */ |
| 891 | if (bc->event_handler != tick_handle_oneshot_broadcast) { |
Viresh Kumar | 472c4a9 | 2015-05-21 13:33:46 +0530 | [diff] [blame] | 892 | int was_periodic = clockevent_state_periodic(bc); |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 893 | |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 894 | bc->event_handler = tick_handle_oneshot_broadcast; |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 895 | |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 896 | /* |
| 897 | * We must be careful here. There might be other CPUs |
| 898 | * waiting for periodic broadcast. We need to set the |
| 899 | * oneshot_mask bits for those and program the |
| 900 | * broadcast device to fire. |
| 901 | */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 902 | cpumask_copy(tmpmask, tick_broadcast_mask); |
| 903 | cpumask_clear_cpu(cpu, tmpmask); |
| 904 | cpumask_or(tick_broadcast_oneshot_mask, |
| 905 | tick_broadcast_oneshot_mask, tmpmask); |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 906 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 907 | if (was_periodic && !cpumask_empty(tmpmask)) { |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 908 | clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 909 | tick_broadcast_init_next_event(tmpmask, |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 910 | tick_next_period); |
Thomas Gleixner | 298dbd1 | 2015-05-05 09:44:24 +0200 | [diff] [blame] | 911 | tick_broadcast_set_event(bc, cpu, tick_next_period); |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 912 | } else |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 913 | bc->next_event = KTIME_MAX; |
Thomas Gleixner | 07f4beb | 2011-05-16 11:07:48 +0200 | [diff] [blame] | 914 | } else { |
| 915 | /* |
| 916 | * The first cpu which switches to oneshot mode sets |
| 917 | * the bit for all other cpus which are in the general |
| 918 | * (periodic) broadcast mask. So the bit is set and |
| 919 | * would prevent the first broadcast enter after this |
| 920 | * to program the bc device. |
| 921 | */ |
| 922 | tick_broadcast_clear_oneshot(cpu); |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 923 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Select oneshot operating mode for the broadcast device |
| 928 | */ |
| 929 | void tick_broadcast_switch_to_oneshot(void) |
| 930 | { |
| 931 | struct clock_event_device *bc; |
| 932 | unsigned long flags; |
| 933 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 934 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Suresh Siddha | fa4da36 | 2012-04-09 15:41:44 -0700 | [diff] [blame] | 935 | |
| 936 | tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 937 | bc = tick_broadcast_device.evtdev; |
| 938 | if (bc) |
| 939 | tick_broadcast_setup_oneshot(bc); |
Suresh Siddha | 77b0d60 | 2011-11-04 17:18:21 -0700 | [diff] [blame] | 940 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 941 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Thomas Gleixner | a49b116 | 2015-04-03 02:38:05 +0200 | [diff] [blame] | 944 | #ifdef CONFIG_HOTPLUG_CPU |
| 945 | void hotplug_cpu__broadcast_tick_pull(int deadcpu) |
| 946 | { |
| 947 | struct clock_event_device *bc; |
| 948 | unsigned long flags; |
| 949 | |
| 950 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
| 951 | bc = tick_broadcast_device.evtdev; |
| 952 | |
| 953 | if (bc && broadcast_needs_cpu(bc, deadcpu)) { |
| 954 | /* This moves the broadcast assignment to this CPU: */ |
| 955 | clockevents_program_event(bc, bc->next_event, 1); |
| 956 | } |
| 957 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
| 958 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 959 | |
| 960 | /* |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 961 | * Remove a dying CPU from broadcasting |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 962 | */ |
Thomas Gleixner | 1b72d43 | 2019-03-21 16:39:20 +0100 | [diff] [blame] | 963 | static void tick_broadcast_oneshot_offline(unsigned int cpu) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 964 | { |
Thomas Gleixner | 31d9b39 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 965 | /* |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 966 | * Clear the broadcast masks for the dead cpu, but do not stop |
| 967 | * the broadcast device! |
Thomas Gleixner | 31d9b39 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 968 | */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 969 | cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask); |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 970 | cpumask_clear_cpu(cpu, tick_broadcast_pending_mask); |
| 971 | cpumask_clear_cpu(cpu, tick_broadcast_force_mask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 972 | } |
Thomas Gleixner | a49b116 | 2015-04-03 02:38:05 +0200 | [diff] [blame] | 973 | #endif |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 974 | |
Thomas Gleixner | 27ce4cb | 2008-09-22 19:04:02 +0200 | [diff] [blame] | 975 | /* |
| 976 | * Check, whether the broadcast device is in one shot mode |
| 977 | */ |
| 978 | int tick_broadcast_oneshot_active(void) |
| 979 | { |
| 980 | return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT; |
| 981 | } |
| 982 | |
Thomas Gleixner | 3a142a0 | 2011-02-25 22:34:23 +0100 | [diff] [blame] | 983 | /* |
| 984 | * Check whether the broadcast device supports oneshot. |
| 985 | */ |
| 986 | bool tick_broadcast_oneshot_available(void) |
| 987 | { |
| 988 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
| 989 | |
| 990 | return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false; |
| 991 | } |
| 992 | |
Thomas Gleixner | f32dd11 | 2015-07-07 16:29:38 +0200 | [diff] [blame] | 993 | #else |
| 994 | int __tick_broadcast_oneshot_control(enum tick_broadcast_state state) |
| 995 | { |
| 996 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
| 997 | |
| 998 | if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER)) |
| 999 | return -EBUSY; |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1003 | #endif |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 1004 | |
| 1005 | void __init tick_broadcast_init(void) |
| 1006 | { |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 1007 | zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 1008 | zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT); |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 1009 | zalloc_cpumask_var(&tmpmask, GFP_NOWAIT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 1010 | #ifdef CONFIG_TICK_ONESHOT |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 1011 | zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT); |
| 1012 | zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT); |
| 1013 | zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 1014 | #endif |
| 1015 | } |