blob: 0d74b9ba90c84e0474b28a71ea9d43b76e4263f9 [file] [log] [blame]
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001/*
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
H Hartley Sweeten8e1a9282009-10-16 18:19:01 -040022#include "tick-internal.h"
23
Thomas Gleixnerd316c572007-02-16 01:28:00 -080024/* The registered clock event devices */
25static LIST_HEAD(clockevent_devices);
26static LIST_HEAD(clockevents_released);
27
28/* Notification for clock events */
29static RAW_NOTIFIER_HEAD(clockevents_chain);
30
31/* Protection for the above */
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010032static DEFINE_RAW_SPINLOCK(clockevents_lock);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080033
34/**
35 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
36 * @latch: value to convert
37 * @evt: pointer to clock event device descriptor
38 *
39 * Math helper, returns latch value converted to nanoseconds (bound checked)
40 */
Jon Hunter97813f22009-08-18 12:45:11 -050041u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080042{
Jon Hunter97813f22009-08-18 12:45:11 -050043 u64 clc = (u64) latch << evt->shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080044
Ingo Molnar45fe4fe2008-01-30 13:30:03 +010045 if (unlikely(!evt->mult)) {
46 evt->mult = 1;
47 WARN_ON(1);
48 }
49
Thomas Gleixnerd316c572007-02-16 01:28:00 -080050 do_div(clc, evt->mult);
51 if (clc < 1000)
52 clc = 1000;
Jon Hunter97813f22009-08-18 12:45:11 -050053 if (clc > KTIME_MAX)
54 clc = KTIME_MAX;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080055
Jon Hunter97813f22009-08-18 12:45:11 -050056 return clc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080057}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090058EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080059
60/**
61 * clockevents_set_mode - set the operating mode of a clock event device
62 * @dev: device to modify
63 * @mode: new mode
64 *
65 * Must be called with interrupts disabled !
66 */
67void clockevents_set_mode(struct clock_event_device *dev,
68 enum clock_event_mode mode)
69{
70 if (dev->mode != mode) {
71 dev->set_mode(mode, dev);
72 dev->mode = mode;
Magnus Damm2d682592009-01-16 17:14:38 +090073
74 /*
75 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
76 * on it, so fix it up and emit a warning:
77 */
78 if (mode == CLOCK_EVT_MODE_ONESHOT) {
79 if (unlikely(!dev->mult)) {
80 dev->mult = 1;
81 WARN_ON(1);
82 }
83 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -080084 }
85}
86
87/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -070088 * clockevents_shutdown - shutdown the device and clear next_event
89 * @dev: device to shutdown
90 */
91void clockevents_shutdown(struct clock_event_device *dev)
92{
93 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
94 dev->next_event.tv64 = KTIME_MAX;
95}
96
97/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -080098 * clockevents_program_event - Reprogram the clock event device.
99 * @expires: absolute expiry time (monotonic clock)
100 *
101 * Returns 0 on success, -ETIME when the event is in the past.
102 */
103int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
104 ktime_t now)
105{
106 unsigned long long clc;
107 int64_t delta;
108
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100109 if (unlikely(expires.tv64 < 0)) {
110 WARN_ON_ONCE(1);
111 return -ETIME;
112 }
113
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800114 delta = ktime_to_ns(ktime_sub(expires, now));
115
116 if (delta <= 0)
117 return -ETIME;
118
119 dev->next_event = expires;
120
121 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
122 return 0;
123
124 if (delta > dev->max_delta_ns)
125 delta = dev->max_delta_ns;
126 if (delta < dev->min_delta_ns)
127 delta = dev->min_delta_ns;
128
129 clc = delta * dev->mult;
130 clc >>= dev->shift;
131
132 return dev->set_next_event((unsigned long) clc, dev);
133}
134
135/**
136 * clockevents_register_notifier - register a clock events change listener
137 */
138int clockevents_register_notifier(struct notifier_block *nb)
139{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700140 unsigned long flags;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800141 int ret;
142
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100143 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800144 ret = raw_notifier_chain_register(&clockevents_chain, nb);
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100145 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800146
147 return ret;
148}
149
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800150/*
151 * Notify about a clock event change. Called with clockevents_lock
152 * held.
153 */
154static void clockevents_do_notify(unsigned long reason, void *dev)
155{
156 raw_notifier_call_chain(&clockevents_chain, reason, dev);
157}
158
159/*
Li Zefan3eb05672008-02-08 04:19:25 -0800160 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800161 * released from the notifier call.
162 */
163static void clockevents_notify_released(void)
164{
165 struct clock_event_device *dev;
166
167 while (!list_empty(&clockevents_released)) {
168 dev = list_entry(clockevents_released.next,
169 struct clock_event_device, list);
170 list_del(&dev->list);
171 list_add(&dev->list, &clockevent_devices);
172 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
173 }
174}
175
176/**
177 * clockevents_register_device - register a clock event device
178 * @dev: device to register
179 */
180void clockevents_register_device(struct clock_event_device *dev)
181{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700182 unsigned long flags;
183
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800184 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030185 BUG_ON(!dev->cpumask);
186
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100187 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800188
189 list_add(&dev->list, &clockevent_devices);
190 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
191 clockevents_notify_released();
192
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100193 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800194}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900195EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800196
197/*
198 * Noop handler when we shut down an event device
199 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000200void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800201{
202}
203
204/**
205 * clockevents_exchange_device - release and request clock devices
206 * @old: device to release (can be NULL)
207 * @new: device to request (can be NULL)
208 *
209 * Called from the notifier chain. clockevents_lock is held already
210 */
211void clockevents_exchange_device(struct clock_event_device *old,
212 struct clock_event_device *new)
213{
214 unsigned long flags;
215
216 local_irq_save(flags);
217 /*
218 * Caller releases a clock event device. We queue it into the
219 * released list and do a notify add later.
220 */
221 if (old) {
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800222 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
223 list_del(&old->list);
224 list_add(&old->list, &clockevents_released);
225 }
226
227 if (new) {
228 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700229 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800230 }
231 local_irq_restore(flags);
232}
233
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200234#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800235/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800236 * clockevents_notify - notification about relevant events
237 */
238void clockevents_notify(unsigned long reason, void *arg)
239{
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100240 struct clock_event_device *dev, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700241 unsigned long flags;
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100242 int cpu;
Li Zefan0b858e62008-02-08 04:19:24 -0800243
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100244 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800245 clockevents_do_notify(reason, arg);
246
247 switch (reason) {
248 case CLOCK_EVT_NOTIFY_CPU_DEAD:
249 /*
250 * Unregister the clock event devices which were
251 * released from the users in the notify chain.
252 */
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100253 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
254 list_del(&dev->list);
255 /*
256 * Now check whether the CPU has left unused per cpu devices
257 */
258 cpu = *((int *)arg);
259 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
260 if (cpumask_test_cpu(cpu, dev->cpumask) &&
Xiaotian Fengea9d8e32010-01-07 11:22:44 +0800261 cpumask_weight(dev->cpumask) == 1 &&
262 !tick_is_broadcast_device(dev)) {
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100263 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
264 list_del(&dev->list);
265 }
266 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800267 break;
268 default:
269 break;
270 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100271 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800272}
273EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200274#endif