Commit | Line | Data |
---|---|---|
d316c57f TG |
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> | |
eea08f32 | 21 | #include <linux/tick.h> |
d316c57f | 22 | |
8e1a928a HS |
23 | #include "tick-internal.h" |
24 | ||
d316c57f TG |
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 */ | |
b5f91da0 | 33 | static DEFINE_RAW_SPINLOCK(clockevents_lock); |
d316c57f TG |
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 | */ | |
97813f2f | 42 | u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt) |
d316c57f | 43 | { |
97813f2f | 44 | u64 clc = (u64) latch << evt->shift; |
d316c57f | 45 | |
45fe4fe1 IM |
46 | if (unlikely(!evt->mult)) { |
47 | evt->mult = 1; | |
48 | WARN_ON(1); | |
49 | } | |
50 | ||
d316c57f TG |
51 | do_div(clc, evt->mult); |
52 | if (clc < 1000) | |
53 | clc = 1000; | |
97813f2f JH |
54 | if (clc > KTIME_MAX) |
55 | clc = KTIME_MAX; | |
d316c57f | 56 | |
97813f2f | 57 | return clc; |
d316c57f | 58 | } |
c81fc2c3 | 59 | EXPORT_SYMBOL_GPL(clockevent_delta2ns); |
d316c57f TG |
60 | |
61 | /** | |
62 | * clockevents_set_mode - set the operating mode of a clock event device | |
63 | * @dev: device to modify | |
64 | * @mode: new mode | |
65 | * | |
66 | * Must be called with interrupts disabled ! | |
67 | */ | |
68 | void clockevents_set_mode(struct clock_event_device *dev, | |
69 | enum clock_event_mode mode) | |
70 | { | |
71 | if (dev->mode != mode) { | |
72 | dev->set_mode(mode, dev); | |
73 | dev->mode = mode; | |
2d68259d MD |
74 | |
75 | /* | |
76 | * A nsec2cyc multiplicator of 0 is invalid and we'd crash | |
77 | * on it, so fix it up and emit a warning: | |
78 | */ | |
79 | if (mode == CLOCK_EVT_MODE_ONESHOT) { | |
80 | if (unlikely(!dev->mult)) { | |
81 | dev->mult = 1; | |
82 | WARN_ON(1); | |
83 | } | |
84 | } | |
d316c57f TG |
85 | } |
86 | } | |
87 | ||
2344abbc TG |
88 | /** |
89 | * clockevents_shutdown - shutdown the device and clear next_event | |
90 | * @dev: device to shutdown | |
91 | */ | |
92 | void clockevents_shutdown(struct clock_event_device *dev) | |
93 | { | |
94 | clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN); | |
95 | dev->next_event.tv64 = KTIME_MAX; | |
96 | } | |
97 | ||
d316c57f TG |
98 | /** |
99 | * clockevents_program_event - Reprogram the clock event device. | |
100 | * @expires: absolute expiry time (monotonic clock) | |
101 | * | |
102 | * Returns 0 on success, -ETIME when the event is in the past. | |
103 | */ | |
104 | int clockevents_program_event(struct clock_event_device *dev, ktime_t expires, | |
105 | ktime_t now) | |
106 | { | |
107 | unsigned long long clc; | |
108 | int64_t delta; | |
109 | ||
167b1de3 TG |
110 | if (unlikely(expires.tv64 < 0)) { |
111 | WARN_ON_ONCE(1); | |
112 | return -ETIME; | |
113 | } | |
114 | ||
d316c57f TG |
115 | delta = ktime_to_ns(ktime_sub(expires, now)); |
116 | ||
117 | if (delta <= 0) | |
118 | return -ETIME; | |
119 | ||
120 | dev->next_event = expires; | |
121 | ||
122 | if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN) | |
123 | return 0; | |
124 | ||
125 | if (delta > dev->max_delta_ns) | |
126 | delta = dev->max_delta_ns; | |
127 | if (delta < dev->min_delta_ns) | |
128 | delta = dev->min_delta_ns; | |
129 | ||
130 | clc = delta * dev->mult; | |
131 | clc >>= dev->shift; | |
132 | ||
133 | return dev->set_next_event((unsigned long) clc, dev); | |
134 | } | |
135 | ||
136 | /** | |
137 | * clockevents_register_notifier - register a clock events change listener | |
138 | */ | |
139 | int clockevents_register_notifier(struct notifier_block *nb) | |
140 | { | |
f833bab8 | 141 | unsigned long flags; |
d316c57f TG |
142 | int ret; |
143 | ||
b5f91da0 | 144 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
d316c57f | 145 | ret = raw_notifier_chain_register(&clockevents_chain, nb); |
b5f91da0 | 146 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
d316c57f TG |
147 | |
148 | return ret; | |
149 | } | |
150 | ||
d316c57f TG |
151 | /* |
152 | * Notify about a clock event change. Called with clockevents_lock | |
153 | * held. | |
154 | */ | |
155 | static void clockevents_do_notify(unsigned long reason, void *dev) | |
156 | { | |
157 | raw_notifier_call_chain(&clockevents_chain, reason, dev); | |
158 | } | |
159 | ||
160 | /* | |
3eb05676 | 161 | * Called after a notify add to make devices available which were |
d316c57f TG |
162 | * released from the notifier call. |
163 | */ | |
164 | static void clockevents_notify_released(void) | |
165 | { | |
166 | struct clock_event_device *dev; | |
167 | ||
168 | while (!list_empty(&clockevents_released)) { | |
169 | dev = list_entry(clockevents_released.next, | |
170 | struct clock_event_device, list); | |
171 | list_del(&dev->list); | |
172 | list_add(&dev->list, &clockevent_devices); | |
173 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); | |
174 | } | |
175 | } | |
176 | ||
177 | /** | |
178 | * clockevents_register_device - register a clock event device | |
179 | * @dev: device to register | |
180 | */ | |
181 | void clockevents_register_device(struct clock_event_device *dev) | |
182 | { | |
f833bab8 SS |
183 | unsigned long flags; |
184 | ||
d316c57f | 185 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
320ab2b0 RR |
186 | BUG_ON(!dev->cpumask); |
187 | ||
b5f91da0 | 188 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
d316c57f TG |
189 | |
190 | list_add(&dev->list, &clockevent_devices); | |
191 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); | |
192 | clockevents_notify_released(); | |
193 | ||
b5f91da0 | 194 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
d316c57f | 195 | } |
c81fc2c3 | 196 | EXPORT_SYMBOL_GPL(clockevents_register_device); |
d316c57f TG |
197 | |
198 | /* | |
199 | * Noop handler when we shut down an event device | |
200 | */ | |
7c1e7689 | 201 | void clockevents_handle_noop(struct clock_event_device *dev) |
d316c57f TG |
202 | { |
203 | } | |
204 | ||
205 | /** | |
206 | * clockevents_exchange_device - release and request clock devices | |
207 | * @old: device to release (can be NULL) | |
208 | * @new: device to request (can be NULL) | |
209 | * | |
210 | * Called from the notifier chain. clockevents_lock is held already | |
211 | */ | |
212 | void clockevents_exchange_device(struct clock_event_device *old, | |
213 | struct clock_event_device *new) | |
214 | { | |
215 | unsigned long flags; | |
216 | ||
217 | local_irq_save(flags); | |
218 | /* | |
219 | * Caller releases a clock event device. We queue it into the | |
220 | * released list and do a notify add later. | |
221 | */ | |
222 | if (old) { | |
d316c57f TG |
223 | clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); |
224 | list_del(&old->list); | |
225 | list_add(&old->list, &clockevents_released); | |
226 | } | |
227 | ||
228 | if (new) { | |
229 | BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED); | |
2344abbc | 230 | clockevents_shutdown(new); |
d316c57f TG |
231 | } |
232 | local_irq_restore(flags); | |
233 | } | |
234 | ||
de68d9b1 | 235 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
d316c57f TG |
236 | /** |
237 | * clockevents_notify - notification about relevant events | |
238 | */ | |
239 | void clockevents_notify(unsigned long reason, void *arg) | |
240 | { | |
bb6eddf7 | 241 | struct clock_event_device *dev, *tmp; |
f833bab8 | 242 | unsigned long flags; |
bb6eddf7 | 243 | int cpu; |
0b858e6f | 244 | |
b5f91da0 | 245 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
d316c57f TG |
246 | clockevents_do_notify(reason, arg); |
247 | ||
248 | switch (reason) { | |
249 | case CLOCK_EVT_NOTIFY_CPU_DEAD: | |
250 | /* | |
251 | * Unregister the clock event devices which were | |
252 | * released from the users in the notify chain. | |
253 | */ | |
bb6eddf7 TG |
254 | list_for_each_entry_safe(dev, tmp, &clockevents_released, list) |
255 | list_del(&dev->list); | |
256 | /* | |
257 | * Now check whether the CPU has left unused per cpu devices | |
258 | */ | |
259 | cpu = *((int *)arg); | |
260 | list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) { | |
261 | if (cpumask_test_cpu(cpu, dev->cpumask) && | |
ea9d8e3f XF |
262 | cpumask_weight(dev->cpumask) == 1 && |
263 | !tick_is_broadcast_device(dev)) { | |
bb6eddf7 TG |
264 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
265 | list_del(&dev->list); | |
266 | } | |
267 | } | |
d316c57f TG |
268 | break; |
269 | default: | |
270 | break; | |
271 | } | |
b5f91da0 | 272 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
d316c57f TG |
273 | } |
274 | EXPORT_SYMBOL_GPL(clockevents_notify); | |
de68d9b1 | 275 | #endif |