USB: usb-storage: unusual_devs entry for Kingston DT 101 G2
[linux/fpc-iii.git] / kernel / time / clockevents.c
blob0d809ae02d609310b34c5f98051ae893f663c295
1 /*
2 * linux/kernel/time/clockevents.c
4 * This file contains functions which manage clock event devices.
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
10 * This code is licenced under the GPL version 2. For details see
11 * kernel-base/COPYING.
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>
23 #include "tick-internal.h"
25 /* The registered clock event devices */
26 static LIST_HEAD(clockevent_devices);
27 static LIST_HEAD(clockevents_released);
29 /* Notification for clock events */
30 static RAW_NOTIFIER_HEAD(clockevents_chain);
32 /* Protection for the above */
33 static DEFINE_SPINLOCK(clockevents_lock);
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
40 * Math helper, returns latch value converted to nanoseconds (bound checked)
42 unsigned long clockevent_delta2ns(unsigned long latch,
43 struct clock_event_device *evt)
45 u64 clc = ((u64) latch << evt->shift);
47 if (unlikely(!evt->mult)) {
48 evt->mult = 1;
49 WARN_ON(1);
52 do_div(clc, evt->mult);
53 if (clc < 1000)
54 clc = 1000;
55 if (clc > LONG_MAX)
56 clc = LONG_MAX;
58 return (unsigned long) clc;
60 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
62 /**
63 * clockevents_set_mode - set the operating mode of a clock event device
64 * @dev: device to modify
65 * @mode: new mode
67 * Must be called with interrupts disabled !
69 void clockevents_set_mode(struct clock_event_device *dev,
70 enum clock_event_mode mode)
72 if (dev->mode != mode) {
73 dev->set_mode(mode, dev);
74 dev->mode = mode;
77 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
78 * on it, so fix it up and emit a warning:
80 if (mode == CLOCK_EVT_MODE_ONESHOT) {
81 if (unlikely(!dev->mult)) {
82 dev->mult = 1;
83 WARN_ON(1);
89 /**
90 * clockevents_shutdown - shutdown the device and clear next_event
91 * @dev: device to shutdown
93 void clockevents_shutdown(struct clock_event_device *dev)
95 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
96 dev->next_event.tv64 = KTIME_MAX;
99 /**
100 * clockevents_program_event - Reprogram the clock event device.
101 * @expires: absolute expiry time (monotonic clock)
103 * Returns 0 on success, -ETIME when the event is in the past.
105 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
106 ktime_t now)
108 unsigned long long clc;
109 int64_t delta;
111 if (unlikely(expires.tv64 < 0)) {
112 WARN_ON_ONCE(1);
113 return -ETIME;
116 delta = ktime_to_ns(ktime_sub(expires, now));
118 if (delta <= 0)
119 return -ETIME;
121 dev->next_event = expires;
123 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
124 return 0;
126 if (delta > dev->max_delta_ns)
127 delta = dev->max_delta_ns;
128 if (delta < dev->min_delta_ns)
129 delta = dev->min_delta_ns;
131 clc = delta * dev->mult;
132 clc >>= dev->shift;
134 return dev->set_next_event((unsigned long) clc, dev);
138 * clockevents_register_notifier - register a clock events change listener
140 int clockevents_register_notifier(struct notifier_block *nb)
142 unsigned long flags;
143 int ret;
145 spin_lock_irqsave(&clockevents_lock, flags);
146 ret = raw_notifier_chain_register(&clockevents_chain, nb);
147 spin_unlock_irqrestore(&clockevents_lock, flags);
149 return ret;
153 * Notify about a clock event change. Called with clockevents_lock
154 * held.
156 static void clockevents_do_notify(unsigned long reason, void *dev)
158 raw_notifier_call_chain(&clockevents_chain, reason, dev);
162 * Called after a notify add to make devices available which were
163 * released from the notifier call.
165 static void clockevents_notify_released(void)
167 struct clock_event_device *dev;
169 while (!list_empty(&clockevents_released)) {
170 dev = list_entry(clockevents_released.next,
171 struct clock_event_device, list);
172 list_del(&dev->list);
173 list_add(&dev->list, &clockevent_devices);
174 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
179 * clockevents_register_device - register a clock event device
180 * @dev: device to register
182 void clockevents_register_device(struct clock_event_device *dev)
184 unsigned long flags;
186 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
187 BUG_ON(!dev->cpumask);
189 spin_lock_irqsave(&clockevents_lock, flags);
191 list_add(&dev->list, &clockevent_devices);
192 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
193 clockevents_notify_released();
195 spin_unlock_irqrestore(&clockevents_lock, flags);
197 EXPORT_SYMBOL_GPL(clockevents_register_device);
200 * Noop handler when we shut down an event device
202 void clockevents_handle_noop(struct clock_event_device *dev)
207 * clockevents_exchange_device - release and request clock devices
208 * @old: device to release (can be NULL)
209 * @new: device to request (can be NULL)
211 * Called from the notifier chain. clockevents_lock is held already
213 void clockevents_exchange_device(struct clock_event_device *old,
214 struct clock_event_device *new)
216 unsigned long flags;
218 local_irq_save(flags);
220 * Caller releases a clock event device. We queue it into the
221 * released list and do a notify add later.
223 if (old) {
224 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
225 list_del(&old->list);
226 list_add(&old->list, &clockevents_released);
229 if (new) {
230 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
231 clockevents_shutdown(new);
233 local_irq_restore(flags);
236 #ifdef CONFIG_GENERIC_CLOCKEVENTS
238 * clockevents_notify - notification about relevant events
240 void clockevents_notify(unsigned long reason, void *arg)
242 struct clock_event_device *dev, *tmp;
243 unsigned long flags;
244 int cpu;
246 spin_lock_irqsave(&clockevents_lock, flags);
247 clockevents_do_notify(reason, arg);
249 switch (reason) {
250 case CLOCK_EVT_NOTIFY_CPU_DEAD:
252 * Unregister the clock event devices which were
253 * released from the users in the notify chain.
255 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
256 list_del(&dev->list);
258 * Now check whether the CPU has left unused per cpu devices
260 cpu = *((int *)arg);
261 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
262 if (cpumask_test_cpu(cpu, dev->cpumask) &&
263 cpumask_weight(dev->cpumask) == 1 &&
264 !tick_is_broadcast_device(dev)) {
265 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
266 list_del(&dev->list);
269 break;
270 default:
271 break;
273 spin_unlock_irqrestore(&clockevents_lock, flags);
275 EXPORT_SYMBOL_GPL(clockevents_notify);
276 #endif