bnx2x: Get gso_segs from FW
[linux-2.6/btrfs-unstable.git] / kernel / time / tick-broadcast.c
blob2fb8cb88df8d0296e655f320bc7d5cbce1a858c8
1 /*
2 * linux/kernel/time/tick-broadcast.c
4 * This file contains functions which emulate a local clock-event
5 * device via a broadcast event source.
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
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
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>
23 #include "tick-internal.h"
26 * Broadcast support for broken x86 hardware, where the local apic
27 * timer stops in C3 state.
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;
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
44 * Debugging: see timer_list.c
46 struct tick_device *tick_get_broadcast_device(void)
48 return &tick_broadcast_device;
51 struct cpumask *tick_get_broadcast_mask(void)
53 return to_cpumask(tick_broadcast_mask);
57 * Start the device in periodic mode
59 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
61 if (bc)
62 tick_setup_periodic(bc, 1);
66 * Check, if the device can be utilized as broadcast device:
68 int tick_check_broadcast_device(struct clock_event_device *dev)
70 if ((tick_broadcast_device.evtdev &&
71 tick_broadcast_device.evtdev->rating >= dev->rating) ||
72 (dev->features & CLOCK_EVT_FEAT_C3STOP))
73 return 0;
75 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
76 tick_broadcast_device.evtdev = dev;
77 if (!cpumask_empty(tick_get_broadcast_mask()))
78 tick_broadcast_start_periodic(dev);
79 return 1;
83 * Check, if the device is the broadcast device
85 int tick_is_broadcast_device(struct clock_event_device *dev)
87 return (dev && tick_broadcast_device.evtdev == dev);
90 static void err_broadcast(const struct cpumask *mask)
92 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
95 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
97 if (!dev->broadcast)
98 dev->broadcast = tick_broadcast;
99 if (!dev->broadcast) {
100 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
101 dev->name);
102 dev->broadcast = err_broadcast;
107 * Check, if the device is disfunctional and a place holder, which
108 * needs to be handled by the broadcast device.
110 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
112 unsigned long flags;
113 int ret = 0;
115 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
118 * Devices might be registered with both periodic and oneshot
119 * mode disabled. This signals, that the device needs to be
120 * operated from the broadcast device and is a placeholder for
121 * the cpu local device.
123 if (!tick_device_is_functional(dev)) {
124 dev->event_handler = tick_handle_periodic;
125 tick_device_setup_broadcast_func(dev);
126 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
127 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
128 ret = 1;
129 } else {
131 * When the new device is not affected by the stop
132 * feature and the cpu is marked in the broadcast mask
133 * then clear the broadcast bit.
135 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
136 int cpu = smp_processor_id();
137 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
138 tick_broadcast_clear_oneshot(cpu);
139 } else {
140 tick_device_setup_broadcast_func(dev);
143 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
144 return ret;
147 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
148 int tick_receive_broadcast(void)
150 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
151 struct clock_event_device *evt = td->evtdev;
153 if (!evt)
154 return -ENODEV;
156 if (!evt->event_handler)
157 return -EINVAL;
159 evt->event_handler(evt);
160 return 0;
162 #endif
165 * Broadcast the event to the cpus, which are set in the mask (mangled).
167 static void tick_do_broadcast(struct cpumask *mask)
169 int cpu = smp_processor_id();
170 struct tick_device *td;
173 * Check, if the current cpu is in the mask
175 if (cpumask_test_cpu(cpu, mask)) {
176 cpumask_clear_cpu(cpu, mask);
177 td = &per_cpu(tick_cpu_device, cpu);
178 td->evtdev->event_handler(td->evtdev);
181 if (!cpumask_empty(mask)) {
183 * It might be necessary to actually check whether the devices
184 * have different broadcast functions. For now, just use the
185 * one of the first device. This works as long as we have this
186 * misfeature only on x86 (lapic)
188 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
189 td->evtdev->broadcast(mask);
194 * Periodic broadcast:
195 * - invoke the broadcast handlers
197 static void tick_do_periodic_broadcast(void)
199 raw_spin_lock(&tick_broadcast_lock);
201 cpumask_and(to_cpumask(tmpmask),
202 cpu_online_mask, tick_get_broadcast_mask());
203 tick_do_broadcast(to_cpumask(tmpmask));
205 raw_spin_unlock(&tick_broadcast_lock);
209 * Event handler for periodic broadcast ticks
211 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
213 ktime_t next;
215 tick_do_periodic_broadcast();
218 * The device is in periodic mode. No reprogramming necessary:
220 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
221 return;
224 * Setup the next period for devices, which do not have
225 * periodic mode. We read dev->next_event first and add to it
226 * when the event already expired. clockevents_program_event()
227 * sets dev->next_event only when the event is really
228 * programmed to the device.
230 for (next = dev->next_event; ;) {
231 next = ktime_add(next, tick_period);
233 if (!clockevents_program_event(dev, next, false))
234 return;
235 tick_do_periodic_broadcast();
240 * Powerstate information: The system enters/leaves a state, where
241 * affected devices might stop
243 static void tick_do_broadcast_on_off(unsigned long *reason)
245 struct clock_event_device *bc, *dev;
246 struct tick_device *td;
247 unsigned long flags;
248 int cpu, bc_stopped;
250 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
252 cpu = smp_processor_id();
253 td = &per_cpu(tick_cpu_device, cpu);
254 dev = td->evtdev;
255 bc = tick_broadcast_device.evtdev;
258 * Is the device not affected by the powerstate ?
260 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
261 goto out;
263 if (!tick_device_is_functional(dev))
264 goto out;
266 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
268 switch (*reason) {
269 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
270 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
271 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
272 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
273 if (tick_broadcast_device.mode ==
274 TICKDEV_MODE_PERIODIC)
275 clockevents_shutdown(dev);
277 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
278 tick_broadcast_force = 1;
279 break;
280 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
281 if (!tick_broadcast_force &&
282 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
283 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
284 if (tick_broadcast_device.mode ==
285 TICKDEV_MODE_PERIODIC)
286 tick_setup_periodic(dev, 0);
288 break;
291 if (cpumask_empty(tick_get_broadcast_mask())) {
292 if (!bc_stopped)
293 clockevents_shutdown(bc);
294 } else if (bc_stopped) {
295 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
296 tick_broadcast_start_periodic(bc);
297 else
298 tick_broadcast_setup_oneshot(bc);
300 out:
301 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
305 * Powerstate information: The system enters/leaves a state, where
306 * affected devices might stop.
308 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
310 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
311 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
312 "offline CPU #%d\n", *oncpu);
313 else
314 tick_do_broadcast_on_off(&reason);
318 * Set the periodic handler depending on broadcast on/off
320 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
322 if (!broadcast)
323 dev->event_handler = tick_handle_periodic;
324 else
325 dev->event_handler = tick_handle_periodic_broadcast;
329 * Remove a CPU from broadcasting
331 void tick_shutdown_broadcast(unsigned int *cpup)
333 struct clock_event_device *bc;
334 unsigned long flags;
335 unsigned int cpu = *cpup;
337 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
339 bc = tick_broadcast_device.evtdev;
340 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
342 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
343 if (bc && cpumask_empty(tick_get_broadcast_mask()))
344 clockevents_shutdown(bc);
347 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
350 void tick_suspend_broadcast(void)
352 struct clock_event_device *bc;
353 unsigned long flags;
355 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
357 bc = tick_broadcast_device.evtdev;
358 if (bc)
359 clockevents_shutdown(bc);
361 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
364 int tick_resume_broadcast(void)
366 struct clock_event_device *bc;
367 unsigned long flags;
368 int broadcast = 0;
370 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
372 bc = tick_broadcast_device.evtdev;
374 if (bc) {
375 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
377 switch (tick_broadcast_device.mode) {
378 case TICKDEV_MODE_PERIODIC:
379 if (!cpumask_empty(tick_get_broadcast_mask()))
380 tick_broadcast_start_periodic(bc);
381 broadcast = cpumask_test_cpu(smp_processor_id(),
382 tick_get_broadcast_mask());
383 break;
384 case TICKDEV_MODE_ONESHOT:
385 if (!cpumask_empty(tick_get_broadcast_mask()))
386 broadcast = tick_resume_broadcast_oneshot(bc);
387 break;
390 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
392 return broadcast;
396 #ifdef CONFIG_TICK_ONESHOT
398 /* FIXME: use cpumask_var_t. */
399 static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
402 * Exposed for debugging: see timer_list.c
404 struct cpumask *tick_get_broadcast_oneshot_mask(void)
406 return to_cpumask(tick_broadcast_oneshot_mask);
409 static int tick_broadcast_set_event(ktime_t expires, int force)
411 struct clock_event_device *bc = tick_broadcast_device.evtdev;
413 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
414 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
416 return clockevents_program_event(bc, expires, force);
419 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
421 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
422 return 0;
426 * Called from irq_enter() when idle was interrupted to reenable the
427 * per cpu device.
429 void tick_check_oneshot_broadcast(int cpu)
431 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
432 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
434 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
439 * Handle oneshot mode broadcasting
441 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
443 struct tick_device *td;
444 ktime_t now, next_event;
445 int cpu;
447 raw_spin_lock(&tick_broadcast_lock);
448 again:
449 dev->next_event.tv64 = KTIME_MAX;
450 next_event.tv64 = KTIME_MAX;
451 cpumask_clear(to_cpumask(tmpmask));
452 now = ktime_get();
453 /* Find all expired events */
454 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
455 td = &per_cpu(tick_cpu_device, cpu);
456 if (td->evtdev->next_event.tv64 <= now.tv64)
457 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
458 else if (td->evtdev->next_event.tv64 < next_event.tv64)
459 next_event.tv64 = td->evtdev->next_event.tv64;
463 * Wakeup the cpus which have an expired event.
465 tick_do_broadcast(to_cpumask(tmpmask));
468 * Two reasons for reprogram:
470 * - The global event did not expire any CPU local
471 * events. This happens in dyntick mode, as the maximum PIT
472 * delta is quite small.
474 * - There are pending events on sleeping CPUs which were not
475 * in the event mask
477 if (next_event.tv64 != KTIME_MAX) {
479 * Rearm the broadcast device. If event expired,
480 * repeat the above
482 if (tick_broadcast_set_event(next_event, 0))
483 goto again;
485 raw_spin_unlock(&tick_broadcast_lock);
489 * Powerstate information: The system enters/leaves a state, where
490 * affected devices might stop
492 void tick_broadcast_oneshot_control(unsigned long reason)
494 struct clock_event_device *bc, *dev;
495 struct tick_device *td;
496 unsigned long flags;
497 int cpu;
500 * Periodic mode does not care about the enter/exit of power
501 * states
503 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
504 return;
507 * We are called with preemtion disabled from the depth of the
508 * idle code, so we can't be moved away.
510 cpu = smp_processor_id();
511 td = &per_cpu(tick_cpu_device, cpu);
512 dev = td->evtdev;
514 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
515 return;
517 bc = tick_broadcast_device.evtdev;
519 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
520 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
521 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
522 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
523 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
524 if (dev->next_event.tv64 < bc->next_event.tv64)
525 tick_broadcast_set_event(dev->next_event, 1);
527 } else {
528 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
529 cpumask_clear_cpu(cpu,
530 tick_get_broadcast_oneshot_mask());
531 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
532 if (dev->next_event.tv64 != KTIME_MAX)
533 tick_program_event(dev->next_event, 1);
536 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
540 * Reset the one shot broadcast for a cpu
542 * Called with tick_broadcast_lock held
544 static void tick_broadcast_clear_oneshot(int cpu)
546 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
549 static void tick_broadcast_init_next_event(struct cpumask *mask,
550 ktime_t expires)
552 struct tick_device *td;
553 int cpu;
555 for_each_cpu(cpu, mask) {
556 td = &per_cpu(tick_cpu_device, cpu);
557 if (td->evtdev)
558 td->evtdev->next_event = expires;
563 * tick_broadcast_setup_oneshot - setup the broadcast device
565 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
567 int cpu = smp_processor_id();
569 /* Set it up only once ! */
570 if (bc->event_handler != tick_handle_oneshot_broadcast) {
571 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
573 bc->event_handler = tick_handle_oneshot_broadcast;
575 /* Take the do_timer update */
576 tick_do_timer_cpu = cpu;
579 * We must be careful here. There might be other CPUs
580 * waiting for periodic broadcast. We need to set the
581 * oneshot_mask bits for those and program the
582 * broadcast device to fire.
584 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
585 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
586 cpumask_or(tick_get_broadcast_oneshot_mask(),
587 tick_get_broadcast_oneshot_mask(),
588 to_cpumask(tmpmask));
590 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
591 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
592 tick_broadcast_init_next_event(to_cpumask(tmpmask),
593 tick_next_period);
594 tick_broadcast_set_event(tick_next_period, 1);
595 } else
596 bc->next_event.tv64 = KTIME_MAX;
597 } else {
599 * The first cpu which switches to oneshot mode sets
600 * the bit for all other cpus which are in the general
601 * (periodic) broadcast mask. So the bit is set and
602 * would prevent the first broadcast enter after this
603 * to program the bc device.
605 tick_broadcast_clear_oneshot(cpu);
610 * Select oneshot operating mode for the broadcast device
612 void tick_broadcast_switch_to_oneshot(void)
614 struct clock_event_device *bc;
615 unsigned long flags;
617 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
619 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
620 bc = tick_broadcast_device.evtdev;
621 if (bc)
622 tick_broadcast_setup_oneshot(bc);
624 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
629 * Remove a dead CPU from broadcasting
631 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
633 unsigned long flags;
634 unsigned int cpu = *cpup;
636 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
639 * Clear the broadcast mask flag for the dead cpu, but do not
640 * stop the broadcast device!
642 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
644 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
648 * Check, whether the broadcast device is in one shot mode
650 int tick_broadcast_oneshot_active(void)
652 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
656 * Check whether the broadcast device supports oneshot.
658 bool tick_broadcast_oneshot_available(void)
660 struct clock_event_device *bc = tick_broadcast_device.evtdev;
662 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
665 #endif