blob: 4d7ee79ec46946db1f48e1b75e1eae7379319ae1 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
21#include "tick-internal.h"
22
23/* The registered clock event devices */
24static LIST_HEAD(clockevent_devices);
25static LIST_HEAD(clockevents_released);
26
27/* Notification for clock events */
28static RAW_NOTIFIER_HEAD(clockevents_chain);
29
30/* Protection for the above */
31static DEFINE_RAW_SPINLOCK(clockevents_lock);
32
33static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
34 bool ismax)
35{
36 u64 clc = (u64) latch << evt->shift;
37 u64 rnd;
38
39 if (unlikely(!evt->mult)) {
40 evt->mult = 1;
41 WARN_ON(1);
42 }
43 rnd = (u64) evt->mult - 1;
44
45 /*
46 * Upper bound sanity check. If the backwards conversion is
47 * not equal latch, we know that the above shift overflowed.
48 */
49 if ((clc >> evt->shift) != (u64)latch)
50 clc = ~0ULL;
51
52 /*
53 * Scaled math oddities:
54 *
55 * For mult <= (1 << shift) we can safely add mult - 1 to
56 * prevent integer rounding loss. So the backwards conversion
57 * from nsec to device ticks will be correct.
58 *
59 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
60 * need to be careful. Adding mult - 1 will result in a value
61 * which when converted back to device ticks can be larger
62 * than latch by up to (mult - 1) >> shift. For the min_delta
63 * calculation we still want to apply this in order to stay
64 * above the minimum device ticks limit. For the upper limit
65 * we would end up with a latch value larger than the upper
66 * limit of the device, so we omit the add to stay below the
67 * device upper boundary.
68 *
69 * Also omit the add if it would overflow the u64 boundary.
70 */
71 if ((~0ULL - clc > rnd) &&
72 (!ismax || evt->mult <= (1U << evt->shift)))
73 clc += rnd;
74
75 do_div(clc, evt->mult);
76
77 /* Deltas less than 1usec are pointless noise */
78 return clc > 1000 ? clc : 1000;
79}
80
81/**
82 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
83 * @latch: value to convert
84 * @evt: pointer to clock event device descriptor
85 *
86 * Math helper, returns latch value converted to nanoseconds (bound checked)
87 */
88u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
89{
90 return cev_delta2ns(latch, evt, false);
91}
92EXPORT_SYMBOL_GPL(clockevent_delta2ns);
93
94/**
95 * clockevents_set_mode - set the operating mode of a clock event device
96 * @dev: device to modify
97 * @mode: new mode
98 *
99 * Must be called with interrupts disabled !
100 */
101void clockevents_set_mode(struct clock_event_device *dev,
102 enum clock_event_mode mode)
103{
104 if (dev->mode != mode) {
105 dev->set_mode(mode, dev);
106 dev->mode = mode;
107
108 /*
109 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
110 * on it, so fix it up and emit a warning:
111 */
112 if (mode == CLOCK_EVT_MODE_ONESHOT) {
113 if (unlikely(!dev->mult)) {
114 dev->mult = 1;
115 WARN_ON(1);
116 }
117 }
118 }
119}
120
121/**
122 * clockevents_shutdown - shutdown the device and clear next_event
123 * @dev: device to shutdown
124 */
125void clockevents_shutdown(struct clock_event_device *dev)
126{
127 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
128 dev->next_event.tv64 = KTIME_MAX;
129}
130
131#ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
132
133/* Limit min_delta to a jiffie */
134#define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
135
136/**
137 * clockevents_increase_min_delta - raise minimum delta of a clock event device
138 * @dev: device to increase the minimum delta
139 *
140 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
141 */
142static int clockevents_increase_min_delta(struct clock_event_device *dev)
143{
144 /* Nothing to do if we already reached the limit */
145 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
146 printk_deferred(KERN_WARNING
147 "CE: Reprogramming failure. Giving up\n");
148 dev->next_event.tv64 = KTIME_MAX;
149 return -ETIME;
150 }
151
152 if (dev->min_delta_ns < 5000)
153 dev->min_delta_ns = 5000;
154 else
155 dev->min_delta_ns += dev->min_delta_ns >> 1;
156
157 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
158 dev->min_delta_ns = MIN_DELTA_LIMIT;
159
160 printk_deferred(KERN_WARNING
161 "CE: %s increased min_delta_ns to %llu nsec\n",
162 dev->name ? dev->name : "?",
163 (unsigned long long) dev->min_delta_ns);
164 return 0;
165}
166
167/**
168 * clockevents_program_min_delta - Set clock event device to the minimum delay.
169 * @dev: device to program
170 *
171 * Returns 0 on success, -ETIME when the retry loop failed.
172 */
173static int clockevents_program_min_delta(struct clock_event_device *dev)
174{
175 unsigned long long clc;
176 int64_t delta;
177 int i;
178
179 for (i = 0;;) {
180 delta = dev->min_delta_ns;
181 dev->next_event = ktime_add_ns(ktime_get(), delta);
182
183 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
184 return 0;
185
186 dev->retries++;
187 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
188 if (dev->set_next_event((unsigned long) clc, dev) == 0)
189 return 0;
190
191 if (++i > 2) {
192 /*
193 * We tried 3 times to program the device with the
194 * given min_delta_ns. Try to increase the minimum
195 * delta, if that fails as well get out of here.
196 */
197 if (clockevents_increase_min_delta(dev))
198 return -ETIME;
199 i = 0;
200 }
201 }
202}
203
204#else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
205
206/**
207 * clockevents_program_min_delta - Set clock event device to the minimum delay.
208 * @dev: device to program
209 *
210 * Returns 0 on success, -ETIME when the retry loop failed.
211 */
212static int clockevents_program_min_delta(struct clock_event_device *dev)
213{
214 unsigned long long clc;
215 int64_t delta;
216
217 delta = dev->min_delta_ns;
218 dev->next_event = ktime_add_ns(ktime_get(), delta);
219
220 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
221 return 0;
222
223 dev->retries++;
224 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
225 return dev->set_next_event((unsigned long) clc, dev);
226}
227
228#endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
229
230/**
231 * clockevents_program_event - Reprogram the clock event device.
232 * @dev: device to program
233 * @expires: absolute expiry time (monotonic clock)
234 * @force: program minimum delay if expires can not be set
235 *
236 * Returns 0 on success, -ETIME when the event is in the past.
237 */
238int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
239 bool force)
240{
241 unsigned long long clc;
242 int64_t delta;
243 int rc;
244
245 if (unlikely(expires.tv64 < 0)) {
246 WARN_ON_ONCE(1);
247 return -ETIME;
248 }
249
250 dev->next_event = expires;
251
252 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
253 return 0;
254
255 /* Shortcut for clockevent devices that can deal with ktime. */
256 if (dev->features & CLOCK_EVT_FEAT_KTIME)
257 return dev->set_next_ktime(expires, dev);
258
259 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
260 if (delta <= 0)
261 return force ? clockevents_program_min_delta(dev) : -ETIME;
262
263 delta = min(delta, (int64_t) dev->max_delta_ns);
264 delta = max(delta, (int64_t) dev->min_delta_ns);
265
266 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
267 rc = dev->set_next_event((unsigned long) clc, dev);
268
269 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
270}
271
272/**
273 * clockevents_register_notifier - register a clock events change listener
274 */
275int clockevents_register_notifier(struct notifier_block *nb)
276{
277 unsigned long flags;
278 int ret;
279
280 raw_spin_lock_irqsave(&clockevents_lock, flags);
281 ret = raw_notifier_chain_register(&clockevents_chain, nb);
282 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
283
284 return ret;
285}
286
287/*
288 * Notify about a clock event change. Called with clockevents_lock
289 * held.
290 */
291static void clockevents_do_notify(unsigned long reason, void *dev)
292{
293 raw_notifier_call_chain(&clockevents_chain, reason, dev);
294}
295
296/*
297 * Called after a notify add to make devices available which were
298 * released from the notifier call.
299 */
300static void clockevents_notify_released(void)
301{
302 struct clock_event_device *dev;
303
304 while (!list_empty(&clockevents_released)) {
305 dev = list_entry(clockevents_released.next,
306 struct clock_event_device, list);
307 list_del(&dev->list);
308 list_add(&dev->list, &clockevent_devices);
309 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
310 }
311}
312
313/**
314 * clockevents_register_device - register a clock event device
315 * @dev: device to register
316 */
317void clockevents_register_device(struct clock_event_device *dev)
318{
319 unsigned long flags;
320
321 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
322 if (!dev->cpumask) {
323 WARN_ON(num_possible_cpus() > 1);
324 dev->cpumask = cpumask_of(smp_processor_id());
325 }
326
327 raw_spin_lock_irqsave(&clockevents_lock, flags);
328
329 list_add(&dev->list, &clockevent_devices);
330 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
331 clockevents_notify_released();
332
333 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
334}
335EXPORT_SYMBOL_GPL(clockevents_register_device);
336
337static void clockevents_config(struct clock_event_device *dev,
338 u32 freq)
339{
340 u64 sec;
341
342 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
343 return;
344
345 /*
346 * Calculate the maximum number of seconds we can sleep. Limit
347 * to 10 minutes for hardware which can program more than
348 * 32bit ticks so we still get reasonable conversion values.
349 */
350 sec = dev->max_delta_ticks;
351 do_div(sec, freq);
352 if (!sec)
353 sec = 1;
354 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
355 sec = 600;
356
357 clockevents_calc_mult_shift(dev, freq, sec);
358 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
359 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
360}
361
362/**
363 * clockevents_config_and_register - Configure and register a clock event device
364 * @dev: device to register
365 * @freq: The clock frequency
366 * @min_delta: The minimum clock ticks to program in oneshot mode
367 * @max_delta: The maximum clock ticks to program in oneshot mode
368 *
369 * min/max_delta can be 0 for devices which do not support oneshot mode.
370 */
371void clockevents_config_and_register(struct clock_event_device *dev,
372 u32 freq, unsigned long min_delta,
373 unsigned long max_delta)
374{
375 dev->min_delta_ticks = min_delta;
376 dev->max_delta_ticks = max_delta;
377 clockevents_config(dev, freq);
378 clockevents_register_device(dev);
379}
380
381/**
382 * clockevents_update_freq - Update frequency and reprogram a clock event device.
383 * @dev: device to modify
384 * @freq: new device frequency
385 *
386 * Reconfigure and reprogram a clock event device in oneshot
387 * mode. Must be called on the cpu for which the device delivers per
388 * cpu timer events with interrupts disabled! Returns 0 on success,
389 * -ETIME when the event is in the past.
390 */
391int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
392{
393 clockevents_config(dev, freq);
394
395 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
396 return 0;
397
398 return clockevents_program_event(dev, dev->next_event, false);
399}
400
401/*
402 * Noop handler when we shut down an event device
403 */
404void clockevents_handle_noop(struct clock_event_device *dev)
405{
406}
407
408/**
409 * clockevents_exchange_device - release and request clock devices
410 * @old: device to release (can be NULL)
411 * @new: device to request (can be NULL)
412 *
413 * Called from the notifier chain. clockevents_lock is held already
414 */
415void clockevents_exchange_device(struct clock_event_device *old,
416 struct clock_event_device *new)
417{
418 unsigned long flags;
419
420 local_irq_save(flags);
421 /*
422 * Caller releases a clock event device. We queue it into the
423 * released list and do a notify add later.
424 */
425 if (old) {
426 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
427 list_del(&old->list);
428 list_add(&old->list, &clockevents_released);
429 }
430
431 if (new) {
432 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
433 clockevents_shutdown(new);
434 }
435 local_irq_restore(flags);
436}
437
438#ifdef CONFIG_GENERIC_CLOCKEVENTS
439/**
440 * clockevents_notify - notification about relevant events
441 */
442void clockevents_notify(unsigned long reason, void *arg)
443{
444 struct clock_event_device *dev, *tmp;
445 unsigned long flags;
446 int cpu;
447
448 raw_spin_lock_irqsave(&clockevents_lock, flags);
449 clockevents_do_notify(reason, arg);
450
451 switch (reason) {
452 case CLOCK_EVT_NOTIFY_CPU_DEAD:
453 /*
454 * Unregister the clock event devices which were
455 * released from the users in the notify chain.
456 */
457 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
458 list_del(&dev->list);
459 /*
460 * Now check whether the CPU has left unused per cpu devices
461 */
462 cpu = *((int *)arg);
463 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
464 if (cpumask_test_cpu(cpu, dev->cpumask) &&
465 cpumask_weight(dev->cpumask) == 1 &&
466 !tick_is_broadcast_device(dev)) {
467 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
468 list_del(&dev->list);
469 }
470 }
471 break;
472 default:
473 break;
474 }
475 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
476}
477EXPORT_SYMBOL_GPL(clockevents_notify);
478#endif