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