blob: 3d5fc0fd1ccac7648f743519a5851ed08c4bcb19 [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
H Hartley Sweeten8e1a9282009-10-16 18:19:01 -040023#include "tick-internal.h"
24
Thomas Gleixnerd316c572007-02-16 01:28:00 -080025/* The registered clock event devices */
26static LIST_HEAD(clockevent_devices);
27static LIST_HEAD(clockevents_released);
28
29/* Notification for clock events */
30static RAW_NOTIFIER_HEAD(clockevents_chain);
31
32/* Protection for the above */
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010033static DEFINE_RAW_SPINLOCK(clockevents_lock);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080034
35/**
36 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
37 * @latch: value to convert
38 * @evt: pointer to clock event device descriptor
39 *
40 * Math helper, returns latch value converted to nanoseconds (bound checked)
41 */
Jon Hunter97813f22009-08-18 12:45:11 -050042u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080043{
Jon Hunter97813f22009-08-18 12:45:11 -050044 u64 clc = (u64) latch << evt->shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080045
Ingo Molnar45fe4fe2008-01-30 13:30:03 +010046 if (unlikely(!evt->mult)) {
47 evt->mult = 1;
48 WARN_ON(1);
49 }
50
Thomas Gleixnerd316c572007-02-16 01:28:00 -080051 do_div(clc, evt->mult);
52 if (clc < 1000)
53 clc = 1000;
Jon Hunter97813f22009-08-18 12:45:11 -050054 if (clc > KTIME_MAX)
55 clc = KTIME_MAX;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080056
Jon Hunter97813f22009-08-18 12:45:11 -050057 return clc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080058}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090059EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080060
61/**
62 * clockevents_set_mode - set the operating mode of a clock event device
63 * @dev: device to modify
64 * @mode: new mode
65 *
66 * Must be called with interrupts disabled !
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;
Magnus Damm2d682592009-01-16 17:14:38 +090074
75 /*
76 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
77 * on it, so fix it up and emit a warning:
78 */
79 if (mode == CLOCK_EVT_MODE_ONESHOT) {
80 if (unlikely(!dev->mult)) {
81 dev->mult = 1;
82 WARN_ON(1);
83 }
84 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -080085 }
86}
87
88/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -070089 * clockevents_shutdown - shutdown the device and clear next_event
90 * @dev: device to shutdown
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/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -080099 * clockevents_program_event - Reprogram the clock event device.
100 * @expires: absolute expiry time (monotonic clock)
101 *
102 * Returns 0 on success, -ETIME when the event is in the past.
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
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100110 if (unlikely(expires.tv64 < 0)) {
111 WARN_ON_ONCE(1);
112 return -ETIME;
113 }
114
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800115 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 * clockevents_register_notifier - register a clock events change listener
138 */
139int clockevents_register_notifier(struct notifier_block *nb)
140{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700141 unsigned long flags;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800142 int ret;
143
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100144 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800145 ret = raw_notifier_chain_register(&clockevents_chain, nb);
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100146 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800147
148 return ret;
149}
150
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800151/*
152 * Notify about a clock event change. Called with clockevents_lock
153 * held.
154 */
155static void clockevents_do_notify(unsigned long reason, void *dev)
156{
157 raw_notifier_call_chain(&clockevents_chain, reason, dev);
158}
159
160/*
Li Zefan3eb05672008-02-08 04:19:25 -0800161 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800162 * released from the notifier call.
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 * clockevents_register_device - register a clock event device
179 * @dev: device to register
180 */
181void clockevents_register_device(struct clock_event_device *dev)
182{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700183 unsigned long flags;
184
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800185 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030186 BUG_ON(!dev->cpumask);
187
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100188 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800189
190 list_add(&dev->list, &clockevent_devices);
191 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
192 clockevents_notify_released();
193
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100194 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800195}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900196EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800197
198/*
199 * Noop handler when we shut down an event device
200 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000201void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800202{
203}
204
205/**
206 * clockevents_exchange_device - release and request clock devices
207 * @old: device to release (can be NULL)
208 * @new: device to request (can be NULL)
209 *
210 * Called from the notifier chain. clockevents_lock is held already
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 * Caller releases a clock event device. We queue it into the
220 * released list and do a notify add later.
221 */
222 if (old) {
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800223 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);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700230 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800231 }
232 local_irq_restore(flags);
233}
234
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200235#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800236/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800237 * clockevents_notify - notification about relevant events
238 */
239void clockevents_notify(unsigned long reason, void *arg)
240{
Li Zefan0b858e62008-02-08 04:19:24 -0800241 struct list_head *node, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700242 unsigned long flags;
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 */
Li Zefan0b858e62008-02-08 04:19:24 -0800253 list_for_each_safe(node, tmp, &clockevents_released)
254 list_del(node);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800255 break;
256 default:
257 break;
258 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100259 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800260}
261EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200262#endif