xref: /linux/kernel/time/tick-broadcast.c (revision c75c5ab575af7db707689cdbb5a5c458e9a034bb)
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>
17 #include <linux/interrupt.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/smp.h>
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 
30 static struct tick_device tick_broadcast_device;
31 /* FIXME: Use cpumask_var_t. */
32 static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
33 static DECLARE_BITMAP(tmpmask, NR_CPUS);
34 static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
35 static int tick_broadcast_force;
36 
37 #ifdef CONFIG_TICK_ONESHOT
38 static void tick_broadcast_clear_oneshot(int cpu);
39 #else
40 static inline void tick_broadcast_clear_oneshot(int cpu) { }
41 #endif
42 
43 /*
44  * Debugging: see timer_list.c
45  */
46 struct tick_device *tick_get_broadcast_device(void)
47 {
48 	return &tick_broadcast_device;
49 }
50 
51 struct cpumask *tick_get_broadcast_mask(void)
52 {
53 	return to_cpumask(tick_broadcast_mask);
54 }
55 
56 /*
57  * Start the device in periodic mode
58  */
59 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
60 {
61 	if (bc)
62 		tick_setup_periodic(bc, 1);
63 }
64 
65 /*
66  * Check, if the device can be utilized as broadcast device:
67  */
68 int tick_check_broadcast_device(struct clock_event_device *dev)
69 {
70 	if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
71 	    (tick_broadcast_device.evtdev &&
72 	     tick_broadcast_device.evtdev->rating >= dev->rating) ||
73 	     (dev->features & CLOCK_EVT_FEAT_C3STOP))
74 		return 0;
75 
76 	clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
77 	tick_broadcast_device.evtdev = dev;
78 	if (!cpumask_empty(tick_get_broadcast_mask()))
79 		tick_broadcast_start_periodic(dev);
80 	return 1;
81 }
82 
83 /*
84  * Check, if the device is the broadcast device
85  */
86 int tick_is_broadcast_device(struct clock_event_device *dev)
87 {
88 	return (dev && tick_broadcast_device.evtdev == dev);
89 }
90 
91 static void err_broadcast(const struct cpumask *mask)
92 {
93 	pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
94 }
95 
96 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
97 {
98 	if (!dev->broadcast)
99 		dev->broadcast = tick_broadcast;
100 	if (!dev->broadcast) {
101 		pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
102 			     dev->name);
103 		dev->broadcast = err_broadcast;
104 	}
105 }
106 
107 /*
108  * Check, if the device is disfunctional and a place holder, which
109  * needs to be handled by the broadcast device.
110  */
111 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
112 {
113 	unsigned long flags;
114 	int ret = 0;
115 
116 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
117 
118 	/*
119 	 * Devices might be registered with both periodic and oneshot
120 	 * mode disabled. This signals, that the device needs to be
121 	 * operated from the broadcast device and is a placeholder for
122 	 * the cpu local device.
123 	 */
124 	if (!tick_device_is_functional(dev)) {
125 		dev->event_handler = tick_handle_periodic;
126 		tick_device_setup_broadcast_func(dev);
127 		cpumask_set_cpu(cpu, tick_get_broadcast_mask());
128 		tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
129 		ret = 1;
130 	} else {
131 		/*
132 		 * When the new device is not affected by the stop
133 		 * feature and the cpu is marked in the broadcast mask
134 		 * then clear the broadcast bit.
135 		 */
136 		if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
137 			int cpu = smp_processor_id();
138 			cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
139 			tick_broadcast_clear_oneshot(cpu);
140 		} else {
141 			tick_device_setup_broadcast_func(dev);
142 		}
143 	}
144 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
145 	return ret;
146 }
147 
148 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
149 int tick_receive_broadcast(void)
150 {
151 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
152 	struct clock_event_device *evt = td->evtdev;
153 
154 	if (!evt)
155 		return -ENODEV;
156 
157 	if (!evt->event_handler)
158 		return -EINVAL;
159 
160 	evt->event_handler(evt);
161 	return 0;
162 }
163 #endif
164 
165 /*
166  * Broadcast the event to the cpus, which are set in the mask (mangled).
167  */
168 static void tick_do_broadcast(struct cpumask *mask)
169 {
170 	int cpu = smp_processor_id();
171 	struct tick_device *td;
172 
173 	/*
174 	 * Check, if the current cpu is in the mask
175 	 */
176 	if (cpumask_test_cpu(cpu, mask)) {
177 		cpumask_clear_cpu(cpu, mask);
178 		td = &per_cpu(tick_cpu_device, cpu);
179 		td->evtdev->event_handler(td->evtdev);
180 	}
181 
182 	if (!cpumask_empty(mask)) {
183 		/*
184 		 * It might be necessary to actually check whether the devices
185 		 * have different broadcast functions. For now, just use the
186 		 * one of the first device. This works as long as we have this
187 		 * misfeature only on x86 (lapic)
188 		 */
189 		td = &per_cpu(tick_cpu_device, cpumask_first(mask));
190 		td->evtdev->broadcast(mask);
191 	}
192 }
193 
194 /*
195  * Periodic broadcast:
196  * - invoke the broadcast handlers
197  */
198 static void tick_do_periodic_broadcast(void)
199 {
200 	raw_spin_lock(&tick_broadcast_lock);
201 
202 	cpumask_and(to_cpumask(tmpmask),
203 		    cpu_online_mask, tick_get_broadcast_mask());
204 	tick_do_broadcast(to_cpumask(tmpmask));
205 
206 	raw_spin_unlock(&tick_broadcast_lock);
207 }
208 
209 /*
210  * Event handler for periodic broadcast ticks
211  */
212 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
213 {
214 	ktime_t next;
215 
216 	tick_do_periodic_broadcast();
217 
218 	/*
219 	 * The device is in periodic mode. No reprogramming necessary:
220 	 */
221 	if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
222 		return;
223 
224 	/*
225 	 * Setup the next period for devices, which do not have
226 	 * periodic mode. We read dev->next_event first and add to it
227 	 * when the event already expired. clockevents_program_event()
228 	 * sets dev->next_event only when the event is really
229 	 * programmed to the device.
230 	 */
231 	for (next = dev->next_event; ;) {
232 		next = ktime_add(next, tick_period);
233 
234 		if (!clockevents_program_event(dev, next, false))
235 			return;
236 		tick_do_periodic_broadcast();
237 	}
238 }
239 
240 /*
241  * Powerstate information: The system enters/leaves a state, where
242  * affected devices might stop
243  */
244 static void tick_do_broadcast_on_off(unsigned long *reason)
245 {
246 	struct clock_event_device *bc, *dev;
247 	struct tick_device *td;
248 	unsigned long flags;
249 	int cpu, bc_stopped;
250 
251 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
252 
253 	cpu = smp_processor_id();
254 	td = &per_cpu(tick_cpu_device, cpu);
255 	dev = td->evtdev;
256 	bc = tick_broadcast_device.evtdev;
257 
258 	/*
259 	 * Is the device not affected by the powerstate ?
260 	 */
261 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
262 		goto out;
263 
264 	if (!tick_device_is_functional(dev))
265 		goto out;
266 
267 	bc_stopped = cpumask_empty(tick_get_broadcast_mask());
268 
269 	switch (*reason) {
270 	case CLOCK_EVT_NOTIFY_BROADCAST_ON:
271 	case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
272 		if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
273 			cpumask_set_cpu(cpu, tick_get_broadcast_mask());
274 			if (tick_broadcast_device.mode ==
275 			    TICKDEV_MODE_PERIODIC)
276 				clockevents_shutdown(dev);
277 		}
278 		if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
279 			tick_broadcast_force = 1;
280 		break;
281 	case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
282 		if (!tick_broadcast_force &&
283 		    cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
284 			cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
285 			if (tick_broadcast_device.mode ==
286 			    TICKDEV_MODE_PERIODIC)
287 				tick_setup_periodic(dev, 0);
288 		}
289 		break;
290 	}
291 
292 	if (cpumask_empty(tick_get_broadcast_mask())) {
293 		if (!bc_stopped)
294 			clockevents_shutdown(bc);
295 	} else if (bc_stopped) {
296 		if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
297 			tick_broadcast_start_periodic(bc);
298 		else
299 			tick_broadcast_setup_oneshot(bc);
300 	}
301 out:
302 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
303 }
304 
305 /*
306  * Powerstate information: The system enters/leaves a state, where
307  * affected devices might stop.
308  */
309 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
310 {
311 	if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
312 		printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
313 		       "offline CPU #%d\n", *oncpu);
314 	else
315 		tick_do_broadcast_on_off(&reason);
316 }
317 
318 /*
319  * Set the periodic handler depending on broadcast on/off
320  */
321 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
322 {
323 	if (!broadcast)
324 		dev->event_handler = tick_handle_periodic;
325 	else
326 		dev->event_handler = tick_handle_periodic_broadcast;
327 }
328 
329 /*
330  * Remove a CPU from broadcasting
331  */
332 void tick_shutdown_broadcast(unsigned int *cpup)
333 {
334 	struct clock_event_device *bc;
335 	unsigned long flags;
336 	unsigned int cpu = *cpup;
337 
338 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
339 
340 	bc = tick_broadcast_device.evtdev;
341 	cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
342 
343 	if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
344 		if (bc && cpumask_empty(tick_get_broadcast_mask()))
345 			clockevents_shutdown(bc);
346 	}
347 
348 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
349 }
350 
351 void tick_suspend_broadcast(void)
352 {
353 	struct clock_event_device *bc;
354 	unsigned long flags;
355 
356 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
357 
358 	bc = tick_broadcast_device.evtdev;
359 	if (bc)
360 		clockevents_shutdown(bc);
361 
362 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
363 }
364 
365 int tick_resume_broadcast(void)
366 {
367 	struct clock_event_device *bc;
368 	unsigned long flags;
369 	int broadcast = 0;
370 
371 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
372 
373 	bc = tick_broadcast_device.evtdev;
374 
375 	if (bc) {
376 		clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
377 
378 		switch (tick_broadcast_device.mode) {
379 		case TICKDEV_MODE_PERIODIC:
380 			if (!cpumask_empty(tick_get_broadcast_mask()))
381 				tick_broadcast_start_periodic(bc);
382 			broadcast = cpumask_test_cpu(smp_processor_id(),
383 						     tick_get_broadcast_mask());
384 			break;
385 		case TICKDEV_MODE_ONESHOT:
386 			if (!cpumask_empty(tick_get_broadcast_mask()))
387 				broadcast = tick_resume_broadcast_oneshot(bc);
388 			break;
389 		}
390 	}
391 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
392 
393 	return broadcast;
394 }
395 
396 
397 #ifdef CONFIG_TICK_ONESHOT
398 
399 /* FIXME: use cpumask_var_t. */
400 static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
401 
402 /*
403  * Exposed for debugging: see timer_list.c
404  */
405 struct cpumask *tick_get_broadcast_oneshot_mask(void)
406 {
407 	return to_cpumask(tick_broadcast_oneshot_mask);
408 }
409 
410 static int tick_broadcast_set_event(ktime_t expires, int force)
411 {
412 	struct clock_event_device *bc = tick_broadcast_device.evtdev;
413 
414 	if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
415 		clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
416 
417 	return clockevents_program_event(bc, expires, force);
418 }
419 
420 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
421 {
422 	clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
423 	return 0;
424 }
425 
426 /*
427  * Called from irq_enter() when idle was interrupted to reenable the
428  * per cpu device.
429  */
430 void tick_check_oneshot_broadcast(int cpu)
431 {
432 	if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
433 		struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
434 
435 		clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
436 	}
437 }
438 
439 /*
440  * Handle oneshot mode broadcasting
441  */
442 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
443 {
444 	struct tick_device *td;
445 	ktime_t now, next_event;
446 	int cpu;
447 
448 	raw_spin_lock(&tick_broadcast_lock);
449 again:
450 	dev->next_event.tv64 = KTIME_MAX;
451 	next_event.tv64 = KTIME_MAX;
452 	cpumask_clear(to_cpumask(tmpmask));
453 	now = ktime_get();
454 	/* Find all expired events */
455 	for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
456 		td = &per_cpu(tick_cpu_device, cpu);
457 		if (td->evtdev->next_event.tv64 <= now.tv64)
458 			cpumask_set_cpu(cpu, to_cpumask(tmpmask));
459 		else if (td->evtdev->next_event.tv64 < next_event.tv64)
460 			next_event.tv64 = td->evtdev->next_event.tv64;
461 	}
462 
463 	/*
464 	 * Wakeup the cpus which have an expired event.
465 	 */
466 	tick_do_broadcast(to_cpumask(tmpmask));
467 
468 	/*
469 	 * Two reasons for reprogram:
470 	 *
471 	 * - The global event did not expire any CPU local
472 	 * events. This happens in dyntick mode, as the maximum PIT
473 	 * delta is quite small.
474 	 *
475 	 * - There are pending events on sleeping CPUs which were not
476 	 * in the event mask
477 	 */
478 	if (next_event.tv64 != KTIME_MAX) {
479 		/*
480 		 * Rearm the broadcast device. If event expired,
481 		 * repeat the above
482 		 */
483 		if (tick_broadcast_set_event(next_event, 0))
484 			goto again;
485 	}
486 	raw_spin_unlock(&tick_broadcast_lock);
487 }
488 
489 /*
490  * Powerstate information: The system enters/leaves a state, where
491  * affected devices might stop
492  */
493 void tick_broadcast_oneshot_control(unsigned long reason)
494 {
495 	struct clock_event_device *bc, *dev;
496 	struct tick_device *td;
497 	unsigned long flags;
498 	int cpu;
499 
500 	/*
501 	 * Periodic mode does not care about the enter/exit of power
502 	 * states
503 	 */
504 	if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
505 		return;
506 
507 	/*
508 	 * We are called with preemtion disabled from the depth of the
509 	 * idle code, so we can't be moved away.
510 	 */
511 	cpu = smp_processor_id();
512 	td = &per_cpu(tick_cpu_device, cpu);
513 	dev = td->evtdev;
514 
515 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
516 		return;
517 
518 	bc = tick_broadcast_device.evtdev;
519 
520 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
521 	if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
522 		if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
523 			cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
524 			clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
525 			if (dev->next_event.tv64 < bc->next_event.tv64)
526 				tick_broadcast_set_event(dev->next_event, 1);
527 		}
528 	} else {
529 		if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
530 			cpumask_clear_cpu(cpu,
531 					  tick_get_broadcast_oneshot_mask());
532 			clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
533 			if (dev->next_event.tv64 != KTIME_MAX)
534 				tick_program_event(dev->next_event, 1);
535 		}
536 	}
537 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
538 }
539 
540 /*
541  * Reset the one shot broadcast for a cpu
542  *
543  * Called with tick_broadcast_lock held
544  */
545 static void tick_broadcast_clear_oneshot(int cpu)
546 {
547 	cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
548 }
549 
550 static void tick_broadcast_init_next_event(struct cpumask *mask,
551 					   ktime_t expires)
552 {
553 	struct tick_device *td;
554 	int cpu;
555 
556 	for_each_cpu(cpu, mask) {
557 		td = &per_cpu(tick_cpu_device, cpu);
558 		if (td->evtdev)
559 			td->evtdev->next_event = expires;
560 	}
561 }
562 
563 /**
564  * tick_broadcast_setup_oneshot - setup the broadcast device
565  */
566 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
567 {
568 	int cpu = smp_processor_id();
569 
570 	/* Set it up only once ! */
571 	if (bc->event_handler != tick_handle_oneshot_broadcast) {
572 		int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
573 
574 		bc->event_handler = tick_handle_oneshot_broadcast;
575 
576 		/* Take the do_timer update */
577 		tick_do_timer_cpu = cpu;
578 
579 		/*
580 		 * We must be careful here. There might be other CPUs
581 		 * waiting for periodic broadcast. We need to set the
582 		 * oneshot_mask bits for those and program the
583 		 * broadcast device to fire.
584 		 */
585 		cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
586 		cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
587 		cpumask_or(tick_get_broadcast_oneshot_mask(),
588 			   tick_get_broadcast_oneshot_mask(),
589 			   to_cpumask(tmpmask));
590 
591 		if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
592 			clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
593 			tick_broadcast_init_next_event(to_cpumask(tmpmask),
594 						       tick_next_period);
595 			tick_broadcast_set_event(tick_next_period, 1);
596 		} else
597 			bc->next_event.tv64 = KTIME_MAX;
598 	} else {
599 		/*
600 		 * The first cpu which switches to oneshot mode sets
601 		 * the bit for all other cpus which are in the general
602 		 * (periodic) broadcast mask. So the bit is set and
603 		 * would prevent the first broadcast enter after this
604 		 * to program the bc device.
605 		 */
606 		tick_broadcast_clear_oneshot(cpu);
607 	}
608 }
609 
610 /*
611  * Select oneshot operating mode for the broadcast device
612  */
613 void tick_broadcast_switch_to_oneshot(void)
614 {
615 	struct clock_event_device *bc;
616 	unsigned long flags;
617 
618 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
619 
620 	tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
621 	bc = tick_broadcast_device.evtdev;
622 	if (bc)
623 		tick_broadcast_setup_oneshot(bc);
624 
625 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
626 }
627 
628 
629 /*
630  * Remove a dead CPU from broadcasting
631  */
632 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
633 {
634 	unsigned long flags;
635 	unsigned int cpu = *cpup;
636 
637 	raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
638 
639 	/*
640 	 * Clear the broadcast mask flag for the dead cpu, but do not
641 	 * stop the broadcast device!
642 	 */
643 	cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
644 
645 	raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
646 }
647 
648 /*
649  * Check, whether the broadcast device is in one shot mode
650  */
651 int tick_broadcast_oneshot_active(void)
652 {
653 	return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
654 }
655 
656 /*
657  * Check whether the broadcast device supports oneshot.
658  */
659 bool tick_broadcast_oneshot_available(void)
660 {
661 	struct clock_event_device *bc = tick_broadcast_device.evtdev;
662 
663 	return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
664 }
665 
666 #endif
667