blob: 05e8aeedcdf344088df3643eecb67015d50d8b3d [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>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053021#include <linux/tick.h>
Thomas Gleixnerd316c572007-02-16 01:28:00 -080022
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_SPINLOCK(clockevents_lock);
32
33/**
34 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
35 * @latch: value to convert
36 * @evt: pointer to clock event device descriptor
37 *
38 * Math helper, returns latch value converted to nanoseconds (bound checked)
39 */
Jon Hunter97813f22009-08-18 12:45:11 -050040u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080041{
Jon Hunter97813f22009-08-18 12:45:11 -050042 u64 clc = (u64) latch << evt->shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080043
Ingo Molnar45fe4fe2008-01-30 13:30:03 +010044 if (unlikely(!evt->mult)) {
45 evt->mult = 1;
46 WARN_ON(1);
47 }
48
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049 do_div(clc, evt->mult);
50 if (clc < 1000)
51 clc = 1000;
Jon Hunter97813f22009-08-18 12:45:11 -050052 if (clc > KTIME_MAX)
53 clc = KTIME_MAX;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080054
Jon Hunter97813f22009-08-18 12:45:11 -050055 return clc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080056}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090057EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080058
59/**
60 * clockevents_set_mode - set the operating mode of a clock event device
61 * @dev: device to modify
62 * @mode: new mode
63 *
64 * Must be called with interrupts disabled !
65 */
66void clockevents_set_mode(struct clock_event_device *dev,
67 enum clock_event_mode mode)
68{
69 if (dev->mode != mode) {
70 dev->set_mode(mode, dev);
71 dev->mode = mode;
Magnus Damm2d682592009-01-16 17:14:38 +090072
73 /*
74 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
75 * on it, so fix it up and emit a warning:
76 */
77 if (mode == CLOCK_EVT_MODE_ONESHOT) {
78 if (unlikely(!dev->mult)) {
79 dev->mult = 1;
80 WARN_ON(1);
81 }
82 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -080083 }
84}
85
86/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -070087 * clockevents_shutdown - shutdown the device and clear next_event
88 * @dev: device to shutdown
89 */
90void clockevents_shutdown(struct clock_event_device *dev)
91{
92 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
93 dev->next_event.tv64 = KTIME_MAX;
94}
95
96/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -080097 * clockevents_program_event - Reprogram the clock event device.
98 * @expires: absolute expiry time (monotonic clock)
99 *
100 * Returns 0 on success, -ETIME when the event is in the past.
101 */
102int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
103 ktime_t now)
104{
105 unsigned long long clc;
106 int64_t delta;
107
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100108 if (unlikely(expires.tv64 < 0)) {
109 WARN_ON_ONCE(1);
110 return -ETIME;
111 }
112
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800113 delta = ktime_to_ns(ktime_sub(expires, now));
114
115 if (delta <= 0)
116 return -ETIME;
117
118 dev->next_event = expires;
119
120 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
121 return 0;
122
123 if (delta > dev->max_delta_ns)
124 delta = dev->max_delta_ns;
125 if (delta < dev->min_delta_ns)
126 delta = dev->min_delta_ns;
127
128 clc = delta * dev->mult;
129 clc >>= dev->shift;
130
131 return dev->set_next_event((unsigned long) clc, dev);
132}
133
134/**
135 * clockevents_register_notifier - register a clock events change listener
136 */
137int clockevents_register_notifier(struct notifier_block *nb)
138{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700139 unsigned long flags;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800140 int ret;
141
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700142 spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800143 ret = raw_notifier_chain_register(&clockevents_chain, nb);
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700144 spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800145
146 return ret;
147}
148
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800149/*
150 * Notify about a clock event change. Called with clockevents_lock
151 * held.
152 */
153static void clockevents_do_notify(unsigned long reason, void *dev)
154{
155 raw_notifier_call_chain(&clockevents_chain, reason, dev);
156}
157
158/*
Li Zefan3eb05672008-02-08 04:19:25 -0800159 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800160 * released from the notifier call.
161 */
162static void clockevents_notify_released(void)
163{
164 struct clock_event_device *dev;
165
166 while (!list_empty(&clockevents_released)) {
167 dev = list_entry(clockevents_released.next,
168 struct clock_event_device, list);
169 list_del(&dev->list);
170 list_add(&dev->list, &clockevent_devices);
171 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
172 }
173}
174
175/**
176 * clockevents_register_device - register a clock event device
177 * @dev: device to register
178 */
179void clockevents_register_device(struct clock_event_device *dev)
180{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700181 unsigned long flags;
182
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800183 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030184 BUG_ON(!dev->cpumask);
185
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700186 spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800187
188 list_add(&dev->list, &clockevent_devices);
189 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
190 clockevents_notify_released();
191
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700192 spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800193}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900194EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800195
196/*
197 * Noop handler when we shut down an event device
198 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000199void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800200{
201}
202
203/**
204 * clockevents_exchange_device - release and request clock devices
205 * @old: device to release (can be NULL)
206 * @new: device to request (can be NULL)
207 *
208 * Called from the notifier chain. clockevents_lock is held already
209 */
210void clockevents_exchange_device(struct clock_event_device *old,
211 struct clock_event_device *new)
212{
213 unsigned long flags;
214
215 local_irq_save(flags);
216 /*
217 * Caller releases a clock event device. We queue it into the
218 * released list and do a notify add later.
219 */
220 if (old) {
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800221 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
222 list_del(&old->list);
223 list_add(&old->list, &clockevents_released);
224 }
225
226 if (new) {
227 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700228 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800229 }
230 local_irq_restore(flags);
231}
232
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200233#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800234/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800235 * clockevents_notify - notification about relevant events
236 */
237void clockevents_notify(unsigned long reason, void *arg)
238{
Li Zefan0b858e62008-02-08 04:19:24 -0800239 struct list_head *node, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700240 unsigned long flags;
Li Zefan0b858e62008-02-08 04:19:24 -0800241
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700242 spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800243 clockevents_do_notify(reason, arg);
244
245 switch (reason) {
246 case CLOCK_EVT_NOTIFY_CPU_DEAD:
247 /*
248 * Unregister the clock event devices which were
249 * released from the users in the notify chain.
250 */
Li Zefan0b858e62008-02-08 04:19:24 -0800251 list_for_each_safe(node, tmp, &clockevents_released)
252 list_del(node);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800253 break;
254 default:
255 break;
256 }
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700257 spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800258}
259EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200260#endif