]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/time/clockevents.c
Merge branch 'topic/asoc' into for-linus
[mirror_ubuntu-bionic-kernel.git] / kernel / time / clockevents.c
CommitLineData
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>
21
22/* The registered clock event devices */
23static LIST_HEAD(clockevent_devices);
24static LIST_HEAD(clockevents_released);
25
26/* Notification for clock events */
27static RAW_NOTIFIER_HEAD(clockevents_chain);
28
29/* Protection for the above */
30static DEFINE_SPINLOCK(clockevents_lock);
31
32/**
33 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
34 * @latch: value to convert
35 * @evt: pointer to clock event device descriptor
36 *
37 * Math helper, returns latch value converted to nanoseconds (bound checked)
38 */
39unsigned long clockevent_delta2ns(unsigned long latch,
40 struct clock_event_device *evt)
41{
42 u64 clc = ((u64) latch << evt->shift);
43
45fe4fe1
IM
44 if (unlikely(!evt->mult)) {
45 evt->mult = 1;
46 WARN_ON(1);
47 }
48
d316c57f
TG
49 do_div(clc, evt->mult);
50 if (clc < 1000)
51 clc = 1000;
52 if (clc > LONG_MAX)
53 clc = LONG_MAX;
54
55 return (unsigned long) clc;
56}
57
58/**
59 * clockevents_set_mode - set the operating mode of a clock event device
60 * @dev: device to modify
61 * @mode: new mode
62 *
63 * Must be called with interrupts disabled !
64 */
65void clockevents_set_mode(struct clock_event_device *dev,
66 enum clock_event_mode mode)
67{
68 if (dev->mode != mode) {
69 dev->set_mode(mode, dev);
70 dev->mode = mode;
71 }
72}
73
2344abbc
TG
74/**
75 * clockevents_shutdown - shutdown the device and clear next_event
76 * @dev: device to shutdown
77 */
78void clockevents_shutdown(struct clock_event_device *dev)
79{
80 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
81 dev->next_event.tv64 = KTIME_MAX;
82}
83
d316c57f
TG
84/**
85 * clockevents_program_event - Reprogram the clock event device.
86 * @expires: absolute expiry time (monotonic clock)
87 *
88 * Returns 0 on success, -ETIME when the event is in the past.
89 */
90int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
91 ktime_t now)
92{
93 unsigned long long clc;
94 int64_t delta;
95
167b1de3
TG
96 if (unlikely(expires.tv64 < 0)) {
97 WARN_ON_ONCE(1);
98 return -ETIME;
99 }
100
d316c57f
TG
101 delta = ktime_to_ns(ktime_sub(expires, now));
102
103 if (delta <= 0)
104 return -ETIME;
105
106 dev->next_event = expires;
107
108 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
109 return 0;
110
111 if (delta > dev->max_delta_ns)
112 delta = dev->max_delta_ns;
113 if (delta < dev->min_delta_ns)
114 delta = dev->min_delta_ns;
115
116 clc = delta * dev->mult;
117 clc >>= dev->shift;
118
119 return dev->set_next_event((unsigned long) clc, dev);
120}
121
122/**
123 * clockevents_register_notifier - register a clock events change listener
124 */
125int clockevents_register_notifier(struct notifier_block *nb)
126{
127 int ret;
128
129 spin_lock(&clockevents_lock);
130 ret = raw_notifier_chain_register(&clockevents_chain, nb);
131 spin_unlock(&clockevents_lock);
132
133 return ret;
134}
135
d316c57f
TG
136/*
137 * Notify about a clock event change. Called with clockevents_lock
138 * held.
139 */
140static void clockevents_do_notify(unsigned long reason, void *dev)
141{
142 raw_notifier_call_chain(&clockevents_chain, reason, dev);
143}
144
145/*
3eb05676 146 * Called after a notify add to make devices available which were
d316c57f
TG
147 * released from the notifier call.
148 */
149static void clockevents_notify_released(void)
150{
151 struct clock_event_device *dev;
152
153 while (!list_empty(&clockevents_released)) {
154 dev = list_entry(clockevents_released.next,
155 struct clock_event_device, list);
156 list_del(&dev->list);
157 list_add(&dev->list, &clockevent_devices);
158 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
159 }
160}
161
162/**
163 * clockevents_register_device - register a clock event device
164 * @dev: device to register
165 */
166void clockevents_register_device(struct clock_event_device *dev)
167{
168 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
320ab2b0
RR
169 BUG_ON(!dev->cpumask);
170
45fe4fe1
IM
171 /*
172 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
173 * on it, so fix it up and emit a warning:
174 */
175 if (unlikely(!dev->mult)) {
176 dev->mult = 1;
177 WARN_ON(1);
178 }
d316c57f
TG
179
180 spin_lock(&clockevents_lock);
181
182 list_add(&dev->list, &clockevent_devices);
183 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
184 clockevents_notify_released();
185
186 spin_unlock(&clockevents_lock);
187}
188
189/*
190 * Noop handler when we shut down an event device
191 */
7c1e7689 192void clockevents_handle_noop(struct clock_event_device *dev)
d316c57f
TG
193{
194}
195
196/**
197 * clockevents_exchange_device - release and request clock devices
198 * @old: device to release (can be NULL)
199 * @new: device to request (can be NULL)
200 *
201 * Called from the notifier chain. clockevents_lock is held already
202 */
203void clockevents_exchange_device(struct clock_event_device *old,
204 struct clock_event_device *new)
205{
206 unsigned long flags;
207
208 local_irq_save(flags);
209 /*
210 * Caller releases a clock event device. We queue it into the
211 * released list and do a notify add later.
212 */
213 if (old) {
d316c57f
TG
214 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
215 list_del(&old->list);
216 list_add(&old->list, &clockevents_released);
217 }
218
219 if (new) {
220 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
2344abbc 221 clockevents_shutdown(new);
d316c57f
TG
222 }
223 local_irq_restore(flags);
224}
225
de68d9b1 226#ifdef CONFIG_GENERIC_CLOCKEVENTS
d316c57f
TG
227/**
228 * clockevents_notify - notification about relevant events
229 */
230void clockevents_notify(unsigned long reason, void *arg)
231{
0b858e6f
LZ
232 struct list_head *node, *tmp;
233
d316c57f
TG
234 spin_lock(&clockevents_lock);
235 clockevents_do_notify(reason, arg);
236
237 switch (reason) {
238 case CLOCK_EVT_NOTIFY_CPU_DEAD:
239 /*
240 * Unregister the clock event devices which were
241 * released from the users in the notify chain.
242 */
0b858e6f
LZ
243 list_for_each_safe(node, tmp, &clockevents_released)
244 list_del(node);
d316c57f
TG
245 break;
246 default:
247 break;
248 }
249 spin_unlock(&clockevents_lock);
250}
251EXPORT_SYMBOL_GPL(clockevents_notify);
de68d9b1 252#endif