blob: 1d327f6db4241330990060ee235037baf6df2fdd [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
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
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;
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
74/**
75 * clockevents_program_event - Reprogram the clock event device.
76 * @expires: absolute expiry time (monotonic clock)
77 *
78 * Returns 0 on success, -ETIME when the event is in the past.
79 */
80int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
81 ktime_t now)
82{
83 unsigned long long clc;
84 int64_t delta;
85
Thomas Gleixner167b1de2007-12-07 19:16:17 +010086 if (unlikely(expires.tv64 < 0)) {
87 WARN_ON_ONCE(1);
88 return -ETIME;
89 }
90
Thomas Gleixnerd316c572007-02-16 01:28:00 -080091 delta = ktime_to_ns(ktime_sub(expires, now));
92
93 if (delta <= 0)
94 return -ETIME;
95
96 dev->next_event = expires;
97
98 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
99 return 0;
100
101 if (delta > dev->max_delta_ns)
102 delta = dev->max_delta_ns;
103 if (delta < dev->min_delta_ns)
104 delta = dev->min_delta_ns;
105
106 clc = delta * dev->mult;
107 clc >>= dev->shift;
108
109 return dev->set_next_event((unsigned long) clc, dev);
110}
111
112/**
113 * clockevents_register_notifier - register a clock events change listener
114 */
115int clockevents_register_notifier(struct notifier_block *nb)
116{
117 int ret;
118
119 spin_lock(&clockevents_lock);
120 ret = raw_notifier_chain_register(&clockevents_chain, nb);
121 spin_unlock(&clockevents_lock);
122
123 return ret;
124}
125
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800126/*
127 * Notify about a clock event change. Called with clockevents_lock
128 * held.
129 */
130static void clockevents_do_notify(unsigned long reason, void *dev)
131{
132 raw_notifier_call_chain(&clockevents_chain, reason, dev);
133}
134
135/*
136 * Called after a notify add to make devices availble which were
137 * released from the notifier call.
138 */
139static void clockevents_notify_released(void)
140{
141 struct clock_event_device *dev;
142
143 while (!list_empty(&clockevents_released)) {
144 dev = list_entry(clockevents_released.next,
145 struct clock_event_device, list);
146 list_del(&dev->list);
147 list_add(&dev->list, &clockevent_devices);
148 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
149 }
150}
151
152/**
153 * clockevents_register_device - register a clock event device
154 * @dev: device to register
155 */
156void clockevents_register_device(struct clock_event_device *dev)
157{
158 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Ingo Molnar45fe4fe2008-01-30 13:30:03 +0100159 /*
160 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
161 * on it, so fix it up and emit a warning:
162 */
163 if (unlikely(!dev->mult)) {
164 dev->mult = 1;
165 WARN_ON(1);
166 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800167
168 spin_lock(&clockevents_lock);
169
170 list_add(&dev->list, &clockevent_devices);
171 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
172 clockevents_notify_released();
173
174 spin_unlock(&clockevents_lock);
175}
176
177/*
178 * Noop handler when we shut down an event device
179 */
180static void clockevents_handle_noop(struct clock_event_device *dev)
181{
182}
183
184/**
185 * clockevents_exchange_device - release and request clock devices
186 * @old: device to release (can be NULL)
187 * @new: device to request (can be NULL)
188 *
189 * Called from the notifier chain. clockevents_lock is held already
190 */
191void clockevents_exchange_device(struct clock_event_device *old,
192 struct clock_event_device *new)
193{
194 unsigned long flags;
195
196 local_irq_save(flags);
197 /*
198 * Caller releases a clock event device. We queue it into the
199 * released list and do a notify add later.
200 */
201 if (old) {
202 old->event_handler = clockevents_handle_noop;
203 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
204 list_del(&old->list);
205 list_add(&old->list, &clockevents_released);
206 }
207
208 if (new) {
209 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
210 clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN);
211 }
212 local_irq_restore(flags);
213}
214
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200215#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800216/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800217 * clockevents_notify - notification about relevant events
218 */
219void clockevents_notify(unsigned long reason, void *arg)
220{
Li Zefan0b858e62008-02-08 04:19:24 -0800221 struct list_head *node, *tmp;
222
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800223 spin_lock(&clockevents_lock);
224 clockevents_do_notify(reason, arg);
225
226 switch (reason) {
227 case CLOCK_EVT_NOTIFY_CPU_DEAD:
228 /*
229 * Unregister the clock event devices which were
230 * released from the users in the notify chain.
231 */
Li Zefan0b858e62008-02-08 04:19:24 -0800232 list_for_each_safe(node, tmp, &clockevents_released)
233 list_del(node);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800234 break;
235 default:
236 break;
237 }
238 spin_unlock(&clockevents_lock);
239}
240EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200241#endif