1
2
3
4
5
6
7
8
9
10
11
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
26static LIST_HEAD(clockevent_devices);
27static LIST_HEAD(clockevents_released);
28
29
30static RAW_NOTIFIER_HEAD(clockevents_chain);
31
32
33static DEFINE_RAW_SPINLOCK(clockevents_lock);
34
35
36
37
38
39
40
41
42u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
43{
44 u64 clc = (u64) latch << evt->shift;
45
46 if (unlikely(!evt->mult)) {
47 evt->mult = 1;
48 WARN_ON(1);
49 }
50
51 do_div(clc, evt->mult);
52 if (clc < 1000)
53 clc = 1000;
54 if (clc > KTIME_MAX)
55 clc = KTIME_MAX;
56
57 return clc;
58}
59EXPORT_SYMBOL_GPL(clockevent_delta2ns);
60
61
62
63
64
65
66
67
68void 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;
74
75
76
77
78
79 if (mode == CLOCK_EVT_MODE_ONESHOT) {
80 if (unlikely(!dev->mult)) {
81 dev->mult = 1;
82 WARN_ON(1);
83 }
84 }
85 }
86}
87
88
89
90
91
92void 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
98
99
100
101
102
103
104int 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
110 if (unlikely(expires.tv64 < 0)) {
111 WARN_ON_ONCE(1);
112 return -ETIME;
113 }
114
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
138
139int clockevents_register_notifier(struct notifier_block *nb)
140{
141 unsigned long flags;
142 int ret;
143
144 raw_spin_lock_irqsave(&clockevents_lock, flags);
145 ret = raw_notifier_chain_register(&clockevents_chain, nb);
146 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
147
148 return ret;
149}
150
151
152
153
154
155static void clockevents_do_notify(unsigned long reason, void *dev)
156{
157 raw_notifier_call_chain(&clockevents_chain, reason, dev);
158}
159
160
161
162
163
164static 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
179
180
181void clockevents_register_device(struct clock_event_device *dev)
182{
183 unsigned long flags;
184
185 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
186 BUG_ON(!dev->cpumask);
187
188 raw_spin_lock_irqsave(&clockevents_lock, flags);
189
190 list_add(&dev->list, &clockevent_devices);
191 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
192 clockevents_notify_released();
193
194 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
195}
196EXPORT_SYMBOL_GPL(clockevents_register_device);
197
198
199
200
201void clockevents_handle_noop(struct clock_event_device *dev)
202{
203}
204
205
206
207
208
209
210
211
212void 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
220
221
222 if (old) {
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);
230 clockevents_shutdown(new);
231 }
232 local_irq_restore(flags);
233}
234
235#ifdef CONFIG_GENERIC_CLOCKEVENTS
236
237
238
239void clockevents_notify(unsigned long reason, void *arg)
240{
241 struct clock_event_device *dev, *tmp;
242 unsigned long flags;
243 int cpu;
244
245 raw_spin_lock_irqsave(&clockevents_lock, flags);
246 clockevents_do_notify(reason, arg);
247
248 switch (reason) {
249 case CLOCK_EVT_NOTIFY_CPU_DEAD:
250
251
252
253
254 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
255 list_del(&dev->list);
256
257
258
259 cpu = *((int *)arg);
260 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
261 if (cpumask_test_cpu(cpu, dev->cpumask) &&
262 cpumask_weight(dev->cpumask) == 1 &&
263 !tick_is_broadcast_device(dev)) {
264 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
265 list_del(&dev->list);
266 }
267 }
268 break;
269 default:
270 break;
271 }
272 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
273}
274EXPORT_SYMBOL_GPL(clockevents_notify);
275#endif
276