Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/time/tick-broadcast.c |
| 3 | * |
| 4 | * This file contains functions which emulate a local clock-event |
| 5 | * device via a broadcast event source. |
| 6 | * |
| 7 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
| 8 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
| 9 | * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner |
| 10 | * |
| 11 | * This code is licenced under the GPL version 2. For details see |
| 12 | * kernel-base/COPYING. |
| 13 | */ |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/hrtimer.h> |
Russell King | d7b9068 | 2008-04-17 07:46:24 +0200 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 18 | #include <linux/percpu.h> |
| 19 | #include <linux/profile.h> |
| 20 | #include <linux/sched.h> |
Mark Rutland | 12ad100 | 2013-01-14 17:05:22 +0000 | [diff] [blame] | 21 | #include <linux/smp.h> |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 22 | #include <linux/module.h> |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 23 | |
| 24 | #include "tick-internal.h" |
| 25 | |
| 26 | /* |
| 27 | * Broadcast support for broken x86 hardware, where the local apic |
| 28 | * timer stops in C3 state. |
| 29 | */ |
| 30 | |
Dmitri Vorobiev | a52f5c5 | 2009-05-01 13:10:21 -0700 | [diff] [blame] | 31 | static struct tick_device tick_broadcast_device; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 32 | static cpumask_var_t tick_broadcast_mask; |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 33 | static cpumask_var_t tick_broadcast_on; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 34 | static cpumask_var_t tmpmask; |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 35 | static DEFINE_RAW_SPINLOCK(tick_broadcast_lock); |
Thomas Gleixner | aa276e1 | 2008-06-09 19:15:00 +0200 | [diff] [blame] | 36 | static int tick_broadcast_force; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 37 | |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 38 | #ifdef CONFIG_TICK_ONESHOT |
| 39 | static void tick_broadcast_clear_oneshot(int cpu); |
| 40 | #else |
| 41 | static inline void tick_broadcast_clear_oneshot(int cpu) { } |
| 42 | #endif |
| 43 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 44 | /* |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 45 | * Debugging: see timer_list.c |
| 46 | */ |
| 47 | struct tick_device *tick_get_broadcast_device(void) |
| 48 | { |
| 49 | return &tick_broadcast_device; |
| 50 | } |
| 51 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 52 | struct cpumask *tick_get_broadcast_mask(void) |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 53 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 54 | return tick_broadcast_mask; |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 58 | * Start the device in periodic mode |
| 59 | */ |
| 60 | static void tick_broadcast_start_periodic(struct clock_event_device *bc) |
| 61 | { |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 62 | if (bc) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 63 | tick_setup_periodic(bc, 1); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Check, if the device can be utilized as broadcast device: |
| 68 | */ |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 69 | static bool tick_check_broadcast_device(struct clock_event_device *curdev, |
| 70 | struct clock_event_device *newdev) |
| 71 | { |
| 72 | if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) || |
Soren Brinkmann | 245a349 | 2013-09-18 11:48:37 -0700 | [diff] [blame] | 73 | (newdev->features & CLOCK_EVT_FEAT_PERCPU) || |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 74 | (newdev->features & CLOCK_EVT_FEAT_C3STOP)) |
| 75 | return false; |
| 76 | |
| 77 | if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT && |
| 78 | !(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) |
| 79 | return false; |
| 80 | |
| 81 | return !curdev || newdev->rating > curdev->rating; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Conditionally install/replace broadcast device |
| 86 | */ |
Thomas Gleixner | 7172a28 | 2013-04-25 20:31:47 +0000 | [diff] [blame] | 87 | void tick_install_broadcast_device(struct clock_event_device *dev) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 88 | { |
Thomas Gleixner | 6f7a05d | 2013-04-25 11:45:53 +0200 | [diff] [blame] | 89 | struct clock_event_device *cur = tick_broadcast_device.evtdev; |
| 90 | |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 91 | if (!tick_check_broadcast_device(cur, dev)) |
Thomas Gleixner | 7172a28 | 2013-04-25 20:31:47 +0000 | [diff] [blame] | 92 | return; |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 93 | |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 94 | if (!try_module_get(dev->owner)) |
| 95 | return; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 96 | |
Thomas Gleixner | 45cb8e0 | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 97 | clockevents_exchange_device(cur, dev); |
Thomas Gleixner | 6f7a05d | 2013-04-25 11:45:53 +0200 | [diff] [blame] | 98 | if (cur) |
| 99 | cur->event_handler = clockevents_handle_noop; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 100 | tick_broadcast_device.evtdev = dev; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 101 | if (!cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 102 | tick_broadcast_start_periodic(dev); |
Stephen Boyd | c038c1c | 2013-04-17 10:26:06 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Inform all cpus about this. We might be in a situation |
| 105 | * where we did not switch to oneshot mode because the per cpu |
| 106 | * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack |
| 107 | * of a oneshot capable broadcast device. Without that |
| 108 | * notification the systems stays stuck in periodic mode |
| 109 | * forever. |
| 110 | */ |
| 111 | if (dev->features & CLOCK_EVT_FEAT_ONESHOT) |
| 112 | tick_clock_notify(); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Check, if the device is the broadcast device |
| 117 | */ |
| 118 | int tick_is_broadcast_device(struct clock_event_device *dev) |
| 119 | { |
| 120 | return (dev && tick_broadcast_device.evtdev == dev); |
| 121 | } |
| 122 | |
Mark Rutland | 12ad100 | 2013-01-14 17:05:22 +0000 | [diff] [blame] | 123 | static void err_broadcast(const struct cpumask *mask) |
| 124 | { |
| 125 | pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n"); |
| 126 | } |
| 127 | |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 128 | static void tick_device_setup_broadcast_func(struct clock_event_device *dev) |
| 129 | { |
| 130 | if (!dev->broadcast) |
| 131 | dev->broadcast = tick_broadcast; |
| 132 | if (!dev->broadcast) { |
| 133 | pr_warn_once("%s depends on broadcast, but no broadcast function available\n", |
| 134 | dev->name); |
| 135 | dev->broadcast = err_broadcast; |
| 136 | } |
| 137 | } |
| 138 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 139 | /* |
| 140 | * Check, if the device is disfunctional and a place holder, which |
| 141 | * needs to be handled by the broadcast device. |
| 142 | */ |
| 143 | int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) |
| 144 | { |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 145 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 146 | unsigned long flags; |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 147 | int ret; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 148 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 149 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Devices might be registered with both periodic and oneshot |
| 153 | * mode disabled. This signals, that the device needs to be |
| 154 | * operated from the broadcast device and is a placeholder for |
| 155 | * the cpu local device. |
| 156 | */ |
| 157 | if (!tick_device_is_functional(dev)) { |
| 158 | dev->event_handler = tick_handle_periodic; |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 159 | tick_device_setup_broadcast_func(dev); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 160 | cpumask_set_cpu(cpu, tick_broadcast_mask); |
Stephen Boyd | a272dcc | 2013-07-11 07:00:59 -0700 | [diff] [blame] | 161 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
| 162 | tick_broadcast_start_periodic(bc); |
| 163 | else |
| 164 | tick_broadcast_setup_oneshot(bc); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 165 | ret = 1; |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 166 | } else { |
| 167 | /* |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 168 | * Clear the broadcast bit for this cpu if the |
| 169 | * device is not power state affected. |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 170 | */ |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 171 | if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 172 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 173 | else |
Mark Rutland | 5d1d9a2 | 2013-02-08 15:24:07 +0000 | [diff] [blame] | 174 | tick_device_setup_broadcast_func(dev); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Clear the broadcast bit if the CPU is not in |
| 178 | * periodic broadcast on state. |
| 179 | */ |
| 180 | if (!cpumask_test_cpu(cpu, tick_broadcast_on)) |
| 181 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
| 182 | |
| 183 | switch (tick_broadcast_device.mode) { |
| 184 | case TICKDEV_MODE_ONESHOT: |
| 185 | /* |
| 186 | * If the system is in oneshot mode we can |
| 187 | * unconditionally clear the oneshot mask bit, |
| 188 | * because the CPU is running and therefore |
| 189 | * not in an idle state which causes the power |
| 190 | * state affected device to stop. Let the |
| 191 | * caller initialize the device. |
| 192 | */ |
| 193 | tick_broadcast_clear_oneshot(cpu); |
| 194 | ret = 0; |
| 195 | break; |
| 196 | |
| 197 | case TICKDEV_MODE_PERIODIC: |
| 198 | /* |
| 199 | * If the system is in periodic mode, check |
| 200 | * whether the broadcast device can be |
| 201 | * switched off now. |
| 202 | */ |
| 203 | if (cpumask_empty(tick_broadcast_mask) && bc) |
| 204 | clockevents_shutdown(bc); |
| 205 | /* |
| 206 | * If we kept the cpu in the broadcast mask, |
| 207 | * tell the caller to leave the per cpu device |
| 208 | * in shutdown state. The periodic interrupt |
| 209 | * is delivered by the broadcast device. |
| 210 | */ |
| 211 | ret = cpumask_test_cpu(cpu, tick_broadcast_mask); |
| 212 | break; |
| 213 | default: |
| 214 | /* Nothing to do */ |
| 215 | ret = 0; |
| 216 | break; |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 219 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
Mark Rutland | 12572db | 2013-01-14 17:05:21 +0000 | [diff] [blame] | 223 | #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST |
| 224 | int tick_receive_broadcast(void) |
| 225 | { |
| 226 | struct tick_device *td = this_cpu_ptr(&tick_cpu_device); |
| 227 | struct clock_event_device *evt = td->evtdev; |
| 228 | |
| 229 | if (!evt) |
| 230 | return -ENODEV; |
| 231 | |
| 232 | if (!evt->event_handler) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | evt->event_handler(evt); |
| 236 | return 0; |
| 237 | } |
| 238 | #endif |
| 239 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 240 | /* |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 241 | * 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] | 242 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 243 | static void tick_do_broadcast(struct cpumask *mask) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 244 | { |
Thomas Gleixner | 186e3cb | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 245 | int cpu = smp_processor_id(); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 246 | struct tick_device *td; |
| 247 | |
| 248 | /* |
| 249 | * Check, if the current cpu is in the mask |
| 250 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 251 | if (cpumask_test_cpu(cpu, mask)) { |
| 252 | cpumask_clear_cpu(cpu, mask); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 253 | td = &per_cpu(tick_cpu_device, cpu); |
| 254 | td->evtdev->event_handler(td->evtdev); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 257 | if (!cpumask_empty(mask)) { |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 258 | /* |
| 259 | * It might be necessary to actually check whether the devices |
| 260 | * have different broadcast functions. For now, just use the |
| 261 | * one of the first device. This works as long as we have this |
| 262 | * misfeature only on x86 (lapic) |
| 263 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 264 | td = &per_cpu(tick_cpu_device, cpumask_first(mask)); |
| 265 | td->evtdev->broadcast(mask); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 266 | } |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Periodic broadcast: |
| 271 | * - invoke the broadcast handlers |
| 272 | */ |
| 273 | static void tick_do_periodic_broadcast(void) |
| 274 | { |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 275 | raw_spin_lock(&tick_broadcast_lock); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 276 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 277 | cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask); |
| 278 | tick_do_broadcast(tmpmask); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 279 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 280 | raw_spin_unlock(&tick_broadcast_lock); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Event handler for periodic broadcast ticks |
| 285 | */ |
| 286 | static void tick_handle_periodic_broadcast(struct clock_event_device *dev) |
| 287 | { |
Thomas Gleixner | d4496b3 | 2008-09-03 21:36:57 +0000 | [diff] [blame] | 288 | ktime_t next; |
| 289 | |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 290 | tick_do_periodic_broadcast(); |
| 291 | |
| 292 | /* |
| 293 | * The device is in periodic mode. No reprogramming necessary: |
| 294 | */ |
| 295 | if (dev->mode == CLOCK_EVT_MODE_PERIODIC) |
| 296 | return; |
| 297 | |
| 298 | /* |
| 299 | * Setup the next period for devices, which do not have |
Thomas Gleixner | d4496b3 | 2008-09-03 21:36:57 +0000 | [diff] [blame] | 300 | * periodic mode. We read dev->next_event first and add to it |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 301 | * when the event already expired. clockevents_program_event() |
Thomas Gleixner | d4496b3 | 2008-09-03 21:36:57 +0000 | [diff] [blame] | 302 | * sets dev->next_event only when the event is really |
| 303 | * programmed to the device. |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 304 | */ |
Thomas Gleixner | d4496b3 | 2008-09-03 21:36:57 +0000 | [diff] [blame] | 305 | for (next = dev->next_event; ;) { |
| 306 | next = ktime_add(next, tick_period); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 307 | |
Martin Schwidefsky | d174830 | 2011-08-23 15:29:42 +0200 | [diff] [blame] | 308 | if (!clockevents_program_event(dev, next, false)) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 309 | return; |
| 310 | tick_do_periodic_broadcast(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Powerstate information: The system enters/leaves a state, where |
| 316 | * affected devices might stop |
| 317 | */ |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 318 | static void tick_do_broadcast_on_off(unsigned long *reason) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 319 | { |
| 320 | struct clock_event_device *bc, *dev; |
| 321 | struct tick_device *td; |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 322 | unsigned long flags; |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 323 | int cpu, bc_stopped; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 324 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 325 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 326 | |
| 327 | cpu = smp_processor_id(); |
| 328 | td = &per_cpu(tick_cpu_device, cpu); |
| 329 | dev = td->evtdev; |
| 330 | bc = tick_broadcast_device.evtdev; |
| 331 | |
| 332 | /* |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 333 | * Is the device not affected by the powerstate ? |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 334 | */ |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 335 | if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP)) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 336 | goto out; |
| 337 | |
Thomas Gleixner | 3dfbc88 | 2007-10-17 18:04:32 +0200 | [diff] [blame] | 338 | if (!tick_device_is_functional(dev)) |
| 339 | goto out; |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 340 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 341 | bc_stopped = cpumask_empty(tick_broadcast_mask); |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 342 | |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 343 | switch (*reason) { |
| 344 | case CLOCK_EVT_NOTIFY_BROADCAST_ON: |
| 345 | case CLOCK_EVT_NOTIFY_BROADCAST_FORCE: |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 346 | cpumask_set_cpu(cpu, tick_broadcast_on); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 347 | if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) { |
Thomas Gleixner | 07454bf | 2008-10-04 10:51:07 +0200 | [diff] [blame] | 348 | if (tick_broadcast_device.mode == |
| 349 | TICKDEV_MODE_PERIODIC) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 350 | clockevents_shutdown(dev); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 351 | } |
Thomas Gleixner | 3dfbc88 | 2007-10-17 18:04:32 +0200 | [diff] [blame] | 352 | if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE) |
Thomas Gleixner | aa276e1 | 2008-06-09 19:15:00 +0200 | [diff] [blame] | 353 | tick_broadcast_force = 1; |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 354 | break; |
| 355 | case CLOCK_EVT_NOTIFY_BROADCAST_OFF: |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 356 | if (tick_broadcast_force) |
| 357 | break; |
| 358 | cpumask_clear_cpu(cpu, tick_broadcast_on); |
| 359 | if (!tick_device_is_functional(dev)) |
| 360 | break; |
| 361 | if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) { |
Thomas Gleixner | 07454bf | 2008-10-04 10:51:07 +0200 | [diff] [blame] | 362 | if (tick_broadcast_device.mode == |
| 363 | TICKDEV_MODE_PERIODIC) |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 364 | tick_setup_periodic(dev, 0); |
| 365 | } |
Thomas Gleixner | 1595f45 | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 366 | break; |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 369 | if (cpumask_empty(tick_broadcast_mask)) { |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 370 | if (!bc_stopped) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 371 | clockevents_shutdown(bc); |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 372 | } else if (bc_stopped) { |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 373 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
| 374 | tick_broadcast_start_periodic(bc); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 375 | else |
| 376 | tick_broadcast_setup_oneshot(bc); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 377 | } |
| 378 | out: |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 379 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Powerstate information: The system enters/leaves a state, where |
| 384 | * affected devices might stop. |
| 385 | */ |
| 386 | void tick_broadcast_on_off(unsigned long reason, int *oncpu) |
| 387 | { |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 388 | if (!cpumask_test_cpu(*oncpu, cpu_online_mask)) |
Glauber Costa | 833df31 | 2008-04-18 13:38:58 -0700 | [diff] [blame] | 389 | printk(KERN_ERR "tick-broadcast: ignoring broadcast for " |
Thomas Gleixner | 72fcde9 | 2007-05-23 13:57:30 -0700 | [diff] [blame] | 390 | "offline CPU #%d\n", *oncpu); |
Avi Kivity | bf020cb | 2007-10-16 23:26:24 -0700 | [diff] [blame] | 391 | else |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 392 | tick_do_broadcast_on_off(&reason); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Set the periodic handler depending on broadcast on/off |
| 397 | */ |
| 398 | void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast) |
| 399 | { |
| 400 | if (!broadcast) |
| 401 | dev->event_handler = tick_handle_periodic; |
| 402 | else |
| 403 | dev->event_handler = tick_handle_periodic_broadcast; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Remove a CPU from broadcasting |
| 408 | */ |
| 409 | void tick_shutdown_broadcast(unsigned int *cpup) |
| 410 | { |
| 411 | struct clock_event_device *bc; |
| 412 | unsigned long flags; |
| 413 | unsigned int cpu = *cpup; |
| 414 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 415 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 416 | |
| 417 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 418 | cpumask_clear_cpu(cpu, tick_broadcast_mask); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 419 | cpumask_clear_cpu(cpu, tick_broadcast_on); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 420 | |
| 421 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 422 | if (bc && cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 423 | clockevents_shutdown(bc); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 426 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | f8381cb | 2007-02-16 01:28:02 -0800 | [diff] [blame] | 427 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 428 | |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 429 | void tick_suspend_broadcast(void) |
| 430 | { |
| 431 | struct clock_event_device *bc; |
| 432 | unsigned long flags; |
| 433 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 434 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 435 | |
| 436 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 437 | if (bc) |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 438 | clockevents_shutdown(bc); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 439 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 440 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | int tick_resume_broadcast(void) |
| 444 | { |
| 445 | struct clock_event_device *bc; |
| 446 | unsigned long flags; |
| 447 | int broadcast = 0; |
| 448 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 449 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 450 | |
| 451 | bc = tick_broadcast_device.evtdev; |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 452 | |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 453 | if (bc) { |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 454 | clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME); |
| 455 | |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 456 | switch (tick_broadcast_device.mode) { |
| 457 | case TICKDEV_MODE_PERIODIC: |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 458 | if (!cpumask_empty(tick_broadcast_mask)) |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 459 | tick_broadcast_start_periodic(bc); |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 460 | broadcast = cpumask_test_cpu(smp_processor_id(), |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 461 | tick_broadcast_mask); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 462 | break; |
| 463 | case TICKDEV_MODE_ONESHOT: |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 464 | if (!cpumask_empty(tick_broadcast_mask)) |
Suresh Siddha | a6371f8 | 2012-04-18 19:27:39 -0700 | [diff] [blame] | 465 | broadcast = tick_resume_broadcast_oneshot(bc); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 466 | break; |
| 467 | } |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 468 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 469 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 470 | |
| 471 | return broadcast; |
| 472 | } |
| 473 | |
| 474 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 475 | #ifdef CONFIG_TICK_ONESHOT |
| 476 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 477 | static cpumask_var_t tick_broadcast_oneshot_mask; |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 478 | static cpumask_var_t tick_broadcast_pending_mask; |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 479 | static cpumask_var_t tick_broadcast_force_mask; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 480 | |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 481 | /* |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 482 | * Exposed for debugging: see timer_list.c |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 483 | */ |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 484 | struct cpumask *tick_get_broadcast_oneshot_mask(void) |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 485 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 486 | return tick_broadcast_oneshot_mask; |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 489 | /* |
Thomas Gleixner | eaa907c | 2013-03-06 11:18:36 +0000 | [diff] [blame] | 490 | * Called before going idle with interrupts disabled. Checks whether a |
| 491 | * broadcast event from the other core is about to happen. We detected |
| 492 | * that in tick_broadcast_oneshot_control(). The callsite can use this |
| 493 | * to avoid a deep idle transition as we are about to get the |
| 494 | * broadcast IPI right away. |
| 495 | */ |
| 496 | int tick_check_broadcast_expired(void) |
| 497 | { |
| 498 | return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask); |
| 499 | } |
| 500 | |
| 501 | /* |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 502 | * Set broadcast interrupt affinity |
| 503 | */ |
| 504 | static void tick_broadcast_set_affinity(struct clock_event_device *bc, |
| 505 | const struct cpumask *cpumask) |
| 506 | { |
| 507 | if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ)) |
| 508 | return; |
| 509 | |
| 510 | if (cpumask_equal(bc->cpumask, cpumask)) |
| 511 | return; |
| 512 | |
| 513 | bc->cpumask = cpumask; |
| 514 | irq_set_affinity(bc->irq, bc->cpumask); |
| 515 | } |
| 516 | |
| 517 | static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu, |
Daniel Lezcano | f9ae39d | 2013-03-02 11:10:10 +0100 | [diff] [blame] | 518 | ktime_t expires, int force) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 519 | { |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 520 | int ret; |
| 521 | |
Thomas Gleixner | b9a6a235 | 2012-04-18 17:31:58 +0200 | [diff] [blame] | 522 | if (bc->mode != CLOCK_EVT_MODE_ONESHOT) |
| 523 | clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); |
| 524 | |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 525 | ret = clockevents_program_event(bc, expires, force); |
| 526 | if (!ret) |
| 527 | tick_broadcast_set_affinity(bc, cpumask_of(cpu)); |
| 528 | return ret; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 531 | int tick_resume_broadcast_oneshot(struct clock_event_device *bc) |
| 532 | { |
| 533 | clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); |
Thomas Gleixner | b7e113d | 2007-09-22 22:29:06 +0000 | [diff] [blame] | 534 | return 0; |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 537 | /* |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 538 | * Called from irq_enter() when idle was interrupted to reenable the |
| 539 | * per cpu device. |
| 540 | */ |
Frederic Weisbecker | e8fcaa5 | 2013-08-07 22:28:01 +0200 | [diff] [blame] | 541 | void tick_check_oneshot_broadcast_this_cpu(void) |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 542 | { |
Frederic Weisbecker | e8fcaa5 | 2013-08-07 22:28:01 +0200 | [diff] [blame] | 543 | if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) { |
| 544 | struct tick_device *td = &__get_cpu_var(tick_cpu_device); |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 545 | |
Thomas Gleixner | 1f73a98 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 546 | /* |
| 547 | * We might be in the middle of switching over from |
| 548 | * periodic to oneshot. If the CPU has not yet |
| 549 | * switched over, leave the device alone. |
| 550 | */ |
| 551 | if (td->mode == TICKDEV_MODE_ONESHOT) { |
| 552 | clockevents_set_mode(td->evtdev, |
| 553 | CLOCK_EVT_MODE_ONESHOT); |
| 554 | } |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | /* |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 559 | * Handle oneshot mode broadcasting |
| 560 | */ |
| 561 | static void tick_handle_oneshot_broadcast(struct clock_event_device *dev) |
| 562 | { |
| 563 | struct tick_device *td; |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 564 | ktime_t now, next_event; |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 565 | int cpu, next_cpu = 0; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 566 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 567 | raw_spin_lock(&tick_broadcast_lock); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 568 | again: |
| 569 | dev->next_event.tv64 = KTIME_MAX; |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 570 | next_event.tv64 = KTIME_MAX; |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 571 | cpumask_clear(tmpmask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 572 | now = ktime_get(); |
| 573 | /* Find all expired events */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 574 | for_each_cpu(cpu, tick_broadcast_oneshot_mask) { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 575 | td = &per_cpu(tick_cpu_device, cpu); |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 576 | if (td->evtdev->next_event.tv64 <= now.tv64) { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 577 | cpumask_set_cpu(cpu, tmpmask); |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 578 | /* |
| 579 | * Mark the remote cpu in the pending mask, so |
| 580 | * it can avoid reprogramming the cpu local |
| 581 | * timer in tick_broadcast_oneshot_control(). |
| 582 | */ |
| 583 | cpumask_set_cpu(cpu, tick_broadcast_pending_mask); |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 584 | } else if (td->evtdev->next_event.tv64 < next_event.tv64) { |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 585 | next_event.tv64 = td->evtdev->next_event.tv64; |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 586 | next_cpu = cpu; |
| 587 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Thomas Gleixner | 2938d27 | 2013-05-28 09:33:01 +0200 | [diff] [blame] | 590 | /* |
| 591 | * Remove the current cpu from the pending mask. The event is |
| 592 | * delivered immediately in tick_do_broadcast() ! |
| 593 | */ |
| 594 | cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask); |
| 595 | |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 596 | /* Take care of enforced broadcast requests */ |
| 597 | cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask); |
| 598 | cpumask_clear(tick_broadcast_force_mask); |
| 599 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 600 | /* |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 601 | * Sanity check. Catch the case where we try to broadcast to |
| 602 | * offline cpus. |
| 603 | */ |
| 604 | if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask))) |
| 605 | cpumask_and(tmpmask, tmpmask, cpu_online_mask); |
| 606 | |
| 607 | /* |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 608 | * Wakeup the cpus which have an expired event. |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 609 | */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 610 | tick_do_broadcast(tmpmask); |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * Two reasons for reprogram: |
| 614 | * |
| 615 | * - The global event did not expire any CPU local |
| 616 | * events. This happens in dyntick mode, as the maximum PIT |
| 617 | * delta is quite small. |
| 618 | * |
| 619 | * - There are pending events on sleeping CPUs which were not |
| 620 | * in the event mask |
| 621 | */ |
| 622 | if (next_event.tv64 != KTIME_MAX) { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 623 | /* |
Thomas Gleixner | cdc6f27 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 624 | * Rearm the broadcast device. If event expired, |
| 625 | * repeat the above |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 626 | */ |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 627 | if (tick_broadcast_set_event(dev, next_cpu, next_event, 0)) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 628 | goto again; |
| 629 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 630 | raw_spin_unlock(&tick_broadcast_lock); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Powerstate information: The system enters/leaves a state, where |
| 635 | * affected devices might stop |
| 636 | */ |
| 637 | void tick_broadcast_oneshot_control(unsigned long reason) |
| 638 | { |
| 639 | struct clock_event_device *bc, *dev; |
| 640 | struct tick_device *td; |
| 641 | unsigned long flags; |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 642 | ktime_t now; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 643 | int cpu; |
| 644 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 645 | /* |
| 646 | * Periodic mode does not care about the enter/exit of power |
| 647 | * states |
| 648 | */ |
| 649 | if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 650 | return; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 651 | |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 652 | /* |
| 653 | * We are called with preemtion disabled from the depth of the |
| 654 | * idle code, so we can't be moved away. |
| 655 | */ |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 656 | cpu = smp_processor_id(); |
| 657 | td = &per_cpu(tick_cpu_device, cpu); |
| 658 | dev = td->evtdev; |
| 659 | |
| 660 | if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 661 | return; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 662 | |
Andi Kleen | 7372b0b | 2011-05-04 15:09:27 -0700 | [diff] [blame] | 663 | bc = tick_broadcast_device.evtdev; |
| 664 | |
| 665 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 666 | if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 667 | if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) { |
Thomas Gleixner | 2938d27 | 2013-05-28 09:33:01 +0200 | [diff] [blame] | 668 | WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask)); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 669 | clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN); |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 670 | /* |
| 671 | * We only reprogram the broadcast timer if we |
| 672 | * did not mark ourself in the force mask and |
| 673 | * if the cpu local event is earlier than the |
| 674 | * broadcast event. If the current CPU is in |
| 675 | * the force mask, then we are going to be |
| 676 | * woken by the IPI right away. |
| 677 | */ |
| 678 | if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) && |
| 679 | dev->next_event.tv64 < bc->next_event.tv64) |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 680 | tick_broadcast_set_event(bc, cpu, dev->next_event, 1); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 681 | } |
| 682 | } else { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 683 | if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 684 | clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 685 | /* |
| 686 | * The cpu which was handling the broadcast |
| 687 | * timer marked this cpu in the broadcast |
| 688 | * pending mask and fired the broadcast |
| 689 | * IPI. So we are going to handle the expired |
| 690 | * event anyway via the broadcast IPI |
| 691 | * handler. No need to reprogram the timer |
| 692 | * with an already expired event. |
| 693 | */ |
| 694 | if (cpumask_test_and_clear_cpu(cpu, |
| 695 | tick_broadcast_pending_mask)) |
| 696 | goto out; |
| 697 | |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 698 | /* |
Daniel Lezcano | ea8deb8 | 2013-06-17 18:15:35 +0200 | [diff] [blame] | 699 | * Bail out if there is no next event. |
| 700 | */ |
| 701 | if (dev->next_event.tv64 == KTIME_MAX) |
| 702 | goto out; |
| 703 | /* |
Thomas Gleixner | 989dcb6 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 704 | * If the pending bit is not set, then we are |
| 705 | * either the CPU handling the broadcast |
| 706 | * interrupt or we got woken by something else. |
| 707 | * |
| 708 | * We are not longer in the broadcast mask, so |
| 709 | * if the cpu local expiry time is already |
| 710 | * reached, we would reprogram the cpu local |
| 711 | * timer with an already expired event. |
| 712 | * |
| 713 | * This can lead to a ping-pong when we return |
| 714 | * to idle and therefor rearm the broadcast |
| 715 | * timer before the cpu local timer was able |
| 716 | * to fire. This happens because the forced |
| 717 | * reprogramming makes sure that the event |
| 718 | * will happen in the future and depending on |
| 719 | * the min_delta setting this might be far |
| 720 | * enough out that the ping-pong starts. |
| 721 | * |
| 722 | * If the cpu local next_event has expired |
| 723 | * then we know that the broadcast timer |
| 724 | * next_event has expired as well and |
| 725 | * broadcast is about to be handled. So we |
| 726 | * avoid reprogramming and enforce that the |
| 727 | * broadcast handler, which did not run yet, |
| 728 | * will invoke the cpu local handler. |
| 729 | * |
| 730 | * We cannot call the handler directly from |
| 731 | * here, because we might be in a NOHZ phase |
| 732 | * and we did not go through the irq_enter() |
| 733 | * nohz fixups. |
| 734 | */ |
| 735 | now = ktime_get(); |
| 736 | if (dev->next_event.tv64 <= now.tv64) { |
| 737 | cpumask_set_cpu(cpu, tick_broadcast_force_mask); |
| 738 | goto out; |
| 739 | } |
| 740 | /* |
| 741 | * We got woken by something else. Reprogram |
| 742 | * the cpu local timer device. |
| 743 | */ |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 744 | tick_program_event(dev->next_event, 1); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 745 | } |
| 746 | } |
Thomas Gleixner | 26517f3 | 2013-03-06 11:18:35 +0000 | [diff] [blame] | 747 | out: |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 748 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 751 | /* |
| 752 | * Reset the one shot broadcast for a cpu |
| 753 | * |
| 754 | * Called with tick_broadcast_lock held |
| 755 | */ |
| 756 | static void tick_broadcast_clear_oneshot(int cpu) |
| 757 | { |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 758 | cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask); |
Thomas Gleixner | 5590a53 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 761 | static void tick_broadcast_init_next_event(struct cpumask *mask, |
| 762 | ktime_t expires) |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 763 | { |
| 764 | struct tick_device *td; |
| 765 | int cpu; |
| 766 | |
Rusty Russell | 5db0e1e | 2009-01-01 10:12:29 +1030 | [diff] [blame] | 767 | for_each_cpu(cpu, mask) { |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 768 | td = &per_cpu(tick_cpu_device, cpu); |
| 769 | if (td->evtdev) |
| 770 | td->evtdev->next_event = expires; |
| 771 | } |
| 772 | } |
| 773 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 774 | /** |
Li Zefan | 8dce39c | 2007-11-05 14:51:10 -0800 | [diff] [blame] | 775 | * tick_broadcast_setup_oneshot - setup the broadcast device |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 776 | */ |
| 777 | void tick_broadcast_setup_oneshot(struct clock_event_device *bc) |
| 778 | { |
Thomas Gleixner | 07f4beb | 2011-05-16 11:07:48 +0200 | [diff] [blame] | 779 | int cpu = smp_processor_id(); |
| 780 | |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 781 | /* Set it up only once ! */ |
| 782 | if (bc->event_handler != tick_handle_oneshot_broadcast) { |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 783 | int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC; |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 784 | |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 785 | bc->event_handler = tick_handle_oneshot_broadcast; |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 786 | |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 787 | /* |
| 788 | * We must be careful here. There might be other CPUs |
| 789 | * waiting for periodic broadcast. We need to set the |
| 790 | * oneshot_mask bits for those and program the |
| 791 | * broadcast device to fire. |
| 792 | */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 793 | cpumask_copy(tmpmask, tick_broadcast_mask); |
| 794 | cpumask_clear_cpu(cpu, tmpmask); |
| 795 | cpumask_or(tick_broadcast_oneshot_mask, |
| 796 | tick_broadcast_oneshot_mask, tmpmask); |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 797 | |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 798 | if (was_periodic && !cpumask_empty(tmpmask)) { |
Thomas Gleixner | b435092 | 2012-04-18 12:08:23 +0200 | [diff] [blame] | 799 | clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 800 | tick_broadcast_init_next_event(tmpmask, |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 801 | tick_next_period); |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 802 | tick_broadcast_set_event(bc, cpu, tick_next_period, 1); |
Thomas Gleixner | 7300711 | 2008-09-06 03:01:45 +0200 | [diff] [blame] | 803 | } else |
| 804 | bc->next_event.tv64 = KTIME_MAX; |
Thomas Gleixner | 07f4beb | 2011-05-16 11:07:48 +0200 | [diff] [blame] | 805 | } else { |
| 806 | /* |
| 807 | * The first cpu which switches to oneshot mode sets |
| 808 | * the bit for all other cpus which are in the general |
| 809 | * (periodic) broadcast mask. So the bit is set and |
| 810 | * would prevent the first broadcast enter after this |
| 811 | * to program the bc device. |
| 812 | */ |
| 813 | tick_broadcast_clear_oneshot(cpu); |
Thomas Gleixner | 9c17bcd | 2008-09-03 21:37:08 +0000 | [diff] [blame] | 814 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Select oneshot operating mode for the broadcast device |
| 819 | */ |
| 820 | void tick_broadcast_switch_to_oneshot(void) |
| 821 | { |
| 822 | struct clock_event_device *bc; |
| 823 | unsigned long flags; |
| 824 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 825 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Suresh Siddha | fa4da36 | 2012-04-09 15:41:44 -0700 | [diff] [blame] | 826 | |
| 827 | tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 828 | bc = tick_broadcast_device.evtdev; |
| 829 | if (bc) |
| 830 | tick_broadcast_setup_oneshot(bc); |
Suresh Siddha | 77b0d60 | 2011-11-04 17:18:21 -0700 | [diff] [blame] | 831 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 832 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | |
| 836 | /* |
| 837 | * Remove a dead CPU from broadcasting |
| 838 | */ |
| 839 | void tick_shutdown_broadcast_oneshot(unsigned int *cpup) |
| 840 | { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 841 | unsigned long flags; |
| 842 | unsigned int cpu = *cpup; |
| 843 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 844 | raw_spin_lock_irqsave(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 845 | |
Thomas Gleixner | 31d9b39 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 846 | /* |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 847 | * Clear the broadcast masks for the dead cpu, but do not stop |
| 848 | * the broadcast device! |
Thomas Gleixner | 31d9b39 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 849 | */ |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 850 | cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask); |
Thomas Gleixner | c9b5a26 | 2013-06-26 12:17:32 +0200 | [diff] [blame] | 851 | cpumask_clear_cpu(cpu, tick_broadcast_pending_mask); |
| 852 | cpumask_clear_cpu(cpu, tick_broadcast_force_mask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 853 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 854 | raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Thomas Gleixner | 27ce4cb | 2008-09-22 19:04:02 +0200 | [diff] [blame] | 857 | /* |
| 858 | * Check, whether the broadcast device is in one shot mode |
| 859 | */ |
| 860 | int tick_broadcast_oneshot_active(void) |
| 861 | { |
| 862 | return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT; |
| 863 | } |
| 864 | |
Thomas Gleixner | 3a142a0 | 2011-02-25 22:34:23 +0100 | [diff] [blame] | 865 | /* |
| 866 | * Check whether the broadcast device supports oneshot. |
| 867 | */ |
| 868 | bool tick_broadcast_oneshot_available(void) |
| 869 | { |
| 870 | struct clock_event_device *bc = tick_broadcast_device.evtdev; |
| 871 | |
| 872 | return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false; |
| 873 | } |
| 874 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 875 | #endif |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 876 | |
| 877 | void __init tick_broadcast_init(void) |
| 878 | { |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 879 | zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT); |
Thomas Gleixner | 07bd117 | 2013-07-01 22:14:10 +0200 | [diff] [blame] | 880 | zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT); |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 881 | zalloc_cpumask_var(&tmpmask, GFP_NOWAIT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 882 | #ifdef CONFIG_TICK_ONESHOT |
Thomas Gleixner | fbd44a6 | 2013-05-03 20:22:36 +0200 | [diff] [blame] | 883 | zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT); |
| 884 | zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT); |
| 885 | zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT); |
Thomas Gleixner | b352bc1 | 2013-03-05 14:25:32 +0100 | [diff] [blame] | 886 | #endif |
| 887 | } |