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