]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/time/tick-broadcast.c
clockevents: prevent multiple init/shutdown
[linux-2.6-omap-h63xx.git] / kernel / time / tick-broadcast.c
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/tick.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 struct tick_device tick_broadcast_device;
31 static cpumask_t tick_broadcast_mask;
32 static DEFINE_SPINLOCK(tick_broadcast_lock);
33 static int tick_broadcast_force;
34
35 #ifdef CONFIG_TICK_ONESHOT
36 static void tick_broadcast_clear_oneshot(int cpu);
37 #else
38 static inline void tick_broadcast_clear_oneshot(int cpu) { }
39 #endif
40
41 /*
42  * Debugging: see timer_list.c
43  */
44 struct tick_device *tick_get_broadcast_device(void)
45 {
46         return &tick_broadcast_device;
47 }
48
49 cpumask_t *tick_get_broadcast_mask(void)
50 {
51         return &tick_broadcast_mask;
52 }
53
54 /*
55  * Start the device in periodic mode
56  */
57 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
58 {
59         if (bc)
60                 tick_setup_periodic(bc, 1);
61 }
62
63 /*
64  * Check, if the device can be utilized as broadcast device:
65  */
66 int tick_check_broadcast_device(struct clock_event_device *dev)
67 {
68         if ((tick_broadcast_device.evtdev &&
69              tick_broadcast_device.evtdev->rating >= dev->rating) ||
70              (dev->features & CLOCK_EVT_FEAT_C3STOP))
71                 return 0;
72
73         clockevents_exchange_device(NULL, dev);
74         tick_broadcast_device.evtdev = dev;
75         if (!cpus_empty(tick_broadcast_mask))
76                 tick_broadcast_start_periodic(dev);
77         return 1;
78 }
79
80 /*
81  * Check, if the device is the broadcast device
82  */
83 int tick_is_broadcast_device(struct clock_event_device *dev)
84 {
85         return (dev && tick_broadcast_device.evtdev == dev);
86 }
87
88 /*
89  * Check, if the device is disfunctional and a place holder, which
90  * needs to be handled by the broadcast device.
91  */
92 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
93 {
94         unsigned long flags;
95         int ret = 0;
96
97         spin_lock_irqsave(&tick_broadcast_lock, flags);
98
99         /*
100          * Devices might be registered with both periodic and oneshot
101          * mode disabled. This signals, that the device needs to be
102          * operated from the broadcast device and is a placeholder for
103          * the cpu local device.
104          */
105         if (!tick_device_is_functional(dev)) {
106                 dev->event_handler = tick_handle_periodic;
107                 cpu_set(cpu, tick_broadcast_mask);
108                 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
109                 ret = 1;
110         } else {
111                 /*
112                  * When the new device is not affected by the stop
113                  * feature and the cpu is marked in the broadcast mask
114                  * then clear the broadcast bit.
115                  */
116                 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
117                         int cpu = smp_processor_id();
118
119                         cpu_clear(cpu, tick_broadcast_mask);
120                         tick_broadcast_clear_oneshot(cpu);
121                 }
122         }
123         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
124         return ret;
125 }
126
127 /*
128  * Broadcast the event to the cpus, which are set in the mask
129  */
130 static void tick_do_broadcast(cpumask_t mask)
131 {
132         int cpu = smp_processor_id();
133         struct tick_device *td;
134
135         /*
136          * Check, if the current cpu is in the mask
137          */
138         if (cpu_isset(cpu, mask)) {
139                 cpu_clear(cpu, mask);
140                 td = &per_cpu(tick_cpu_device, cpu);
141                 td->evtdev->event_handler(td->evtdev);
142         }
143
144         if (!cpus_empty(mask)) {
145                 /*
146                  * It might be necessary to actually check whether the devices
147                  * have different broadcast functions. For now, just use the
148                  * one of the first device. This works as long as we have this
149                  * misfeature only on x86 (lapic)
150                  */
151                 cpu = first_cpu(mask);
152                 td = &per_cpu(tick_cpu_device, cpu);
153                 td->evtdev->broadcast(mask);
154         }
155 }
156
157 /*
158  * Periodic broadcast:
159  * - invoke the broadcast handlers
160  */
161 static void tick_do_periodic_broadcast(void)
162 {
163         cpumask_t mask;
164
165         spin_lock(&tick_broadcast_lock);
166
167         cpus_and(mask, cpu_online_map, tick_broadcast_mask);
168         tick_do_broadcast(mask);
169
170         spin_unlock(&tick_broadcast_lock);
171 }
172
173 /*
174  * Event handler for periodic broadcast ticks
175  */
176 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
177 {
178         ktime_t next;
179
180         tick_do_periodic_broadcast();
181
182         /*
183          * The device is in periodic mode. No reprogramming necessary:
184          */
185         if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
186                 return;
187
188         /*
189          * Setup the next period for devices, which do not have
190          * periodic mode. We read dev->next_event first and add to it
191          * when the event alrady expired. clockevents_program_event()
192          * sets dev->next_event only when the event is really
193          * programmed to the device.
194          */
195         for (next = dev->next_event; ;) {
196                 next = ktime_add(next, tick_period);
197
198                 if (!clockevents_program_event(dev, next, ktime_get()))
199                         return;
200                 tick_do_periodic_broadcast();
201         }
202 }
203
204 /*
205  * Powerstate information: The system enters/leaves a state, where
206  * affected devices might stop
207  */
208 static void tick_do_broadcast_on_off(void *why)
209 {
210         struct clock_event_device *bc, *dev;
211         struct tick_device *td;
212         unsigned long flags, *reason = why;
213         int cpu, bc_stopped;
214
215         spin_lock_irqsave(&tick_broadcast_lock, flags);
216
217         cpu = smp_processor_id();
218         td = &per_cpu(tick_cpu_device, cpu);
219         dev = td->evtdev;
220         bc = tick_broadcast_device.evtdev;
221
222         /*
223          * Is the device not affected by the powerstate ?
224          */
225         if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
226                 goto out;
227
228         if (!tick_device_is_functional(dev))
229                 goto out;
230
231         bc_stopped = cpus_empty(tick_broadcast_mask);
232
233         switch (*reason) {
234         case CLOCK_EVT_NOTIFY_BROADCAST_ON:
235         case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
236                 if (!cpu_isset(cpu, tick_broadcast_mask)) {
237                         cpu_set(cpu, tick_broadcast_mask);
238                         if (td->mode == TICKDEV_MODE_PERIODIC)
239                                 clockevents_set_mode(dev,
240                                                      CLOCK_EVT_MODE_SHUTDOWN);
241                 }
242                 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
243                         tick_broadcast_force = 1;
244                 break;
245         case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
246                 if (!tick_broadcast_force &&
247                     cpu_isset(cpu, tick_broadcast_mask)) {
248                         cpu_clear(cpu, tick_broadcast_mask);
249                         if (td->mode == TICKDEV_MODE_PERIODIC)
250                                 tick_setup_periodic(dev, 0);
251                 }
252                 break;
253         }
254
255         if (cpus_empty(tick_broadcast_mask)) {
256                 if (!bc_stopped)
257                         clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
258         } else if (bc_stopped) {
259                 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
260                         tick_broadcast_start_periodic(bc);
261                 else
262                         tick_broadcast_setup_oneshot(bc);
263         }
264 out:
265         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
266 }
267
268 /*
269  * Powerstate information: The system enters/leaves a state, where
270  * affected devices might stop.
271  */
272 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
273 {
274         if (!cpu_isset(*oncpu, cpu_online_map))
275                 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
276                        "offline CPU #%d\n", *oncpu);
277         else
278                 smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
279                                          &reason, 1);
280 }
281
282 /*
283  * Set the periodic handler depending on broadcast on/off
284  */
285 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
286 {
287         if (!broadcast)
288                 dev->event_handler = tick_handle_periodic;
289         else
290                 dev->event_handler = tick_handle_periodic_broadcast;
291 }
292
293 /*
294  * Remove a CPU from broadcasting
295  */
296 void tick_shutdown_broadcast(unsigned int *cpup)
297 {
298         struct clock_event_device *bc;
299         unsigned long flags;
300         unsigned int cpu = *cpup;
301
302         spin_lock_irqsave(&tick_broadcast_lock, flags);
303
304         bc = tick_broadcast_device.evtdev;
305         cpu_clear(cpu, tick_broadcast_mask);
306
307         if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
308                 if (bc && cpus_empty(tick_broadcast_mask))
309                         clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
310         }
311
312         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
313 }
314
315 void tick_suspend_broadcast(void)
316 {
317         struct clock_event_device *bc;
318         unsigned long flags;
319
320         spin_lock_irqsave(&tick_broadcast_lock, flags);
321
322         bc = tick_broadcast_device.evtdev;
323         if (bc)
324                 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
325
326         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
327 }
328
329 int tick_resume_broadcast(void)
330 {
331         struct clock_event_device *bc;
332         unsigned long flags;
333         int broadcast = 0;
334
335         spin_lock_irqsave(&tick_broadcast_lock, flags);
336
337         bc = tick_broadcast_device.evtdev;
338
339         if (bc) {
340                 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
341
342                 switch (tick_broadcast_device.mode) {
343                 case TICKDEV_MODE_PERIODIC:
344                         if(!cpus_empty(tick_broadcast_mask))
345                                 tick_broadcast_start_periodic(bc);
346                         broadcast = cpu_isset(smp_processor_id(),
347                                               tick_broadcast_mask);
348                         break;
349                 case TICKDEV_MODE_ONESHOT:
350                         broadcast = tick_resume_broadcast_oneshot(bc);
351                         break;
352                 }
353         }
354         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
355
356         return broadcast;
357 }
358
359
360 #ifdef CONFIG_TICK_ONESHOT
361
362 static cpumask_t tick_broadcast_oneshot_mask;
363
364 /*
365  * Debugging: see timer_list.c
366  */
367 cpumask_t *tick_get_broadcast_oneshot_mask(void)
368 {
369         return &tick_broadcast_oneshot_mask;
370 }
371
372 static int tick_broadcast_set_event(ktime_t expires, int force)
373 {
374         struct clock_event_device *bc = tick_broadcast_device.evtdev;
375         ktime_t now = ktime_get();
376         int res;
377
378         for(;;) {
379                 res = clockevents_program_event(bc, expires, now);
380                 if (!res || !force)
381                         return res;
382                 now = ktime_get();
383                 expires = ktime_add(now, ktime_set(0, bc->min_delta_ns));
384         }
385 }
386
387 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
388 {
389         clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
390         return 0;
391 }
392
393 /*
394  * Handle oneshot mode broadcasting
395  */
396 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
397 {
398         struct tick_device *td;
399         cpumask_t mask;
400         ktime_t now, next_event;
401         int cpu;
402
403         spin_lock(&tick_broadcast_lock);
404 again:
405         dev->next_event.tv64 = KTIME_MAX;
406         next_event.tv64 = KTIME_MAX;
407         mask = CPU_MASK_NONE;
408         now = ktime_get();
409         /* Find all expired events */
410         for_each_cpu_mask_nr(cpu, tick_broadcast_oneshot_mask) {
411                 td = &per_cpu(tick_cpu_device, cpu);
412                 if (td->evtdev->next_event.tv64 <= now.tv64)
413                         cpu_set(cpu, mask);
414                 else if (td->evtdev->next_event.tv64 < next_event.tv64)
415                         next_event.tv64 = td->evtdev->next_event.tv64;
416         }
417
418         /*
419          * Wakeup the cpus which have an expired event.
420          */
421         tick_do_broadcast(mask);
422
423         /*
424          * Two reasons for reprogram:
425          *
426          * - The global event did not expire any CPU local
427          * events. This happens in dyntick mode, as the maximum PIT
428          * delta is quite small.
429          *
430          * - There are pending events on sleeping CPUs which were not
431          * in the event mask
432          */
433         if (next_event.tv64 != KTIME_MAX) {
434                 /*
435                  * Rearm the broadcast device. If event expired,
436                  * repeat the above
437                  */
438                 if (tick_broadcast_set_event(next_event, 0))
439                         goto again;
440         }
441         spin_unlock(&tick_broadcast_lock);
442 }
443
444 /*
445  * Powerstate information: The system enters/leaves a state, where
446  * affected devices might stop
447  */
448 void tick_broadcast_oneshot_control(unsigned long reason)
449 {
450         struct clock_event_device *bc, *dev;
451         struct tick_device *td;
452         unsigned long flags;
453         int cpu;
454
455         spin_lock_irqsave(&tick_broadcast_lock, flags);
456
457         /*
458          * Periodic mode does not care about the enter/exit of power
459          * states
460          */
461         if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
462                 goto out;
463
464         bc = tick_broadcast_device.evtdev;
465         cpu = smp_processor_id();
466         td = &per_cpu(tick_cpu_device, cpu);
467         dev = td->evtdev;
468
469         if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
470                 goto out;
471
472         if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
473                 if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
474                         cpu_set(cpu, tick_broadcast_oneshot_mask);
475                         clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
476                         if (dev->next_event.tv64 < bc->next_event.tv64)
477                                 tick_broadcast_set_event(dev->next_event, 1);
478                 }
479         } else {
480                 if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
481                         cpu_clear(cpu, tick_broadcast_oneshot_mask);
482                         clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
483                         if (dev->next_event.tv64 != KTIME_MAX)
484                                 tick_program_event(dev->next_event, 1);
485                 }
486         }
487
488 out:
489         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
490 }
491
492 /*
493  * Reset the one shot broadcast for a cpu
494  *
495  * Called with tick_broadcast_lock held
496  */
497 static void tick_broadcast_clear_oneshot(int cpu)
498 {
499         cpu_clear(cpu, tick_broadcast_oneshot_mask);
500 }
501
502 /**
503  * tick_broadcast_setup_oneshot - setup the broadcast device
504  */
505 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
506 {
507         /* Set it up only once ! */
508         if (bc->event_handler != tick_handle_oneshot_broadcast) {
509                 bc->event_handler = tick_handle_oneshot_broadcast;
510                 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
511                 bc->next_event.tv64 = KTIME_MAX;
512         }
513 }
514
515 /*
516  * Select oneshot operating mode for the broadcast device
517  */
518 void tick_broadcast_switch_to_oneshot(void)
519 {
520         struct clock_event_device *bc;
521         unsigned long flags;
522
523         spin_lock_irqsave(&tick_broadcast_lock, flags);
524
525         tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
526         bc = tick_broadcast_device.evtdev;
527         if (bc)
528                 tick_broadcast_setup_oneshot(bc);
529         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
530 }
531
532
533 /*
534  * Remove a dead CPU from broadcasting
535  */
536 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
537 {
538         unsigned long flags;
539         unsigned int cpu = *cpup;
540
541         spin_lock_irqsave(&tick_broadcast_lock, flags);
542
543         /*
544          * Clear the broadcast mask flag for the dead cpu, but do not
545          * stop the broadcast device!
546          */
547         cpu_clear(cpu, tick_broadcast_oneshot_mask);
548
549         spin_unlock_irqrestore(&tick_broadcast_lock, flags);
550 }
551
552 #endif