]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/time/clockevents.c
0d809ae02d609310b34c5f98051ae893f663c295
[karo-tx-linux.git] / kernel / time / clockevents.c
1 /*
2  * linux/kernel/time/clockevents.c
3  *
4  * This file contains functions which manage clock event devices.
5  *
6  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
9  *
10  * This code is licenced under the GPL version 2. For details see
11  * kernel-base/COPYING.
12  */
13
14 #include <linux/clockchips.h>
15 #include <linux/hrtimer.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/notifier.h>
19 #include <linux/smp.h>
20 #include <linux/sysdev.h>
21 #include <linux/tick.h>
22
23 #include "tick-internal.h"
24
25 /* The registered clock event devices */
26 static LIST_HEAD(clockevent_devices);
27 static LIST_HEAD(clockevents_released);
28
29 /* Notification for clock events */
30 static RAW_NOTIFIER_HEAD(clockevents_chain);
31
32 /* Protection for the above */
33 static DEFINE_SPINLOCK(clockevents_lock);
34
35 /**
36  * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
37  * @latch:      value to convert
38  * @evt:        pointer to clock event device descriptor
39  *
40  * Math helper, returns latch value converted to nanoseconds (bound checked)
41  */
42 unsigned long clockevent_delta2ns(unsigned long latch,
43                                   struct clock_event_device *evt)
44 {
45         u64 clc = ((u64) latch << evt->shift);
46
47         if (unlikely(!evt->mult)) {
48                 evt->mult = 1;
49                 WARN_ON(1);
50         }
51
52         do_div(clc, evt->mult);
53         if (clc < 1000)
54                 clc = 1000;
55         if (clc > LONG_MAX)
56                 clc = LONG_MAX;
57
58         return (unsigned long) clc;
59 }
60 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
61
62 /**
63  * clockevents_set_mode - set the operating mode of a clock event device
64  * @dev:        device to modify
65  * @mode:       new mode
66  *
67  * Must be called with interrupts disabled !
68  */
69 void clockevents_set_mode(struct clock_event_device *dev,
70                                  enum clock_event_mode mode)
71 {
72         if (dev->mode != mode) {
73                 dev->set_mode(mode, dev);
74                 dev->mode = mode;
75
76                 /*
77                  * A nsec2cyc multiplicator of 0 is invalid and we'd crash
78                  * on it, so fix it up and emit a warning:
79                  */
80                 if (mode == CLOCK_EVT_MODE_ONESHOT) {
81                         if (unlikely(!dev->mult)) {
82                                 dev->mult = 1;
83                                 WARN_ON(1);
84                         }
85                 }
86         }
87 }
88
89 /**
90  * clockevents_shutdown - shutdown the device and clear next_event
91  * @dev:        device to shutdown
92  */
93 void clockevents_shutdown(struct clock_event_device *dev)
94 {
95         clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
96         dev->next_event.tv64 = KTIME_MAX;
97 }
98
99 /**
100  * clockevents_program_event - Reprogram the clock event device.
101  * @expires:    absolute expiry time (monotonic clock)
102  *
103  * Returns 0 on success, -ETIME when the event is in the past.
104  */
105 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
106                               ktime_t now)
107 {
108         unsigned long long clc;
109         int64_t delta;
110
111         if (unlikely(expires.tv64 < 0)) {
112                 WARN_ON_ONCE(1);
113                 return -ETIME;
114         }
115
116         delta = ktime_to_ns(ktime_sub(expires, now));
117
118         if (delta <= 0)
119                 return -ETIME;
120
121         dev->next_event = expires;
122
123         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
124                 return 0;
125
126         if (delta > dev->max_delta_ns)
127                 delta = dev->max_delta_ns;
128         if (delta < dev->min_delta_ns)
129                 delta = dev->min_delta_ns;
130
131         clc = delta * dev->mult;
132         clc >>= dev->shift;
133
134         return dev->set_next_event((unsigned long) clc, dev);
135 }
136
137 /**
138  * clockevents_register_notifier - register a clock events change listener
139  */
140 int clockevents_register_notifier(struct notifier_block *nb)
141 {
142         unsigned long flags;
143         int ret;
144
145         spin_lock_irqsave(&clockevents_lock, flags);
146         ret = raw_notifier_chain_register(&clockevents_chain, nb);
147         spin_unlock_irqrestore(&clockevents_lock, flags);
148
149         return ret;
150 }
151
152 /*
153  * Notify about a clock event change. Called with clockevents_lock
154  * held.
155  */
156 static void clockevents_do_notify(unsigned long reason, void *dev)
157 {
158         raw_notifier_call_chain(&clockevents_chain, reason, dev);
159 }
160
161 /*
162  * Called after a notify add to make devices available which were
163  * released from the notifier call.
164  */
165 static void clockevents_notify_released(void)
166 {
167         struct clock_event_device *dev;
168
169         while (!list_empty(&clockevents_released)) {
170                 dev = list_entry(clockevents_released.next,
171                                  struct clock_event_device, list);
172                 list_del(&dev->list);
173                 list_add(&dev->list, &clockevent_devices);
174                 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
175         }
176 }
177
178 /**
179  * clockevents_register_device - register a clock event device
180  * @dev:        device to register
181  */
182 void clockevents_register_device(struct clock_event_device *dev)
183 {
184         unsigned long flags;
185
186         BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
187         BUG_ON(!dev->cpumask);
188
189         spin_lock_irqsave(&clockevents_lock, flags);
190
191         list_add(&dev->list, &clockevent_devices);
192         clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
193         clockevents_notify_released();
194
195         spin_unlock_irqrestore(&clockevents_lock, flags);
196 }
197 EXPORT_SYMBOL_GPL(clockevents_register_device);
198
199 /*
200  * Noop handler when we shut down an event device
201  */
202 void clockevents_handle_noop(struct clock_event_device *dev)
203 {
204 }
205
206 /**
207  * clockevents_exchange_device - release and request clock devices
208  * @old:        device to release (can be NULL)
209  * @new:        device to request (can be NULL)
210  *
211  * Called from the notifier chain. clockevents_lock is held already
212  */
213 void clockevents_exchange_device(struct clock_event_device *old,
214                                  struct clock_event_device *new)
215 {
216         unsigned long flags;
217
218         local_irq_save(flags);
219         /*
220          * Caller releases a clock event device. We queue it into the
221          * released list and do a notify add later.
222          */
223         if (old) {
224                 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
225                 list_del(&old->list);
226                 list_add(&old->list, &clockevents_released);
227         }
228
229         if (new) {
230                 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
231                 clockevents_shutdown(new);
232         }
233         local_irq_restore(flags);
234 }
235
236 #ifdef CONFIG_GENERIC_CLOCKEVENTS
237 /**
238  * clockevents_notify - notification about relevant events
239  */
240 void clockevents_notify(unsigned long reason, void *arg)
241 {
242         struct clock_event_device *dev, *tmp;
243         unsigned long flags;
244         int cpu;
245
246         spin_lock_irqsave(&clockevents_lock, flags);
247         clockevents_do_notify(reason, arg);
248
249         switch (reason) {
250         case CLOCK_EVT_NOTIFY_CPU_DEAD:
251                 /*
252                  * Unregister the clock event devices which were
253                  * released from the users in the notify chain.
254                  */
255                 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
256                         list_del(&dev->list);
257                 /*
258                  * Now check whether the CPU has left unused per cpu devices
259                  */
260                 cpu = *((int *)arg);
261                 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
262                         if (cpumask_test_cpu(cpu, dev->cpumask) &&
263                             cpumask_weight(dev->cpumask) == 1 &&
264                             !tick_is_broadcast_device(dev)) {
265                                 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
266                                 list_del(&dev->list);
267                         }
268                 }
269                 break;
270         default:
271                 break;
272         }
273         spin_unlock_irqrestore(&clockevents_lock, flags);
274 }
275 EXPORT_SYMBOL_GPL(clockevents_notify);
276 #endif