Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  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 #include <linux/ftrace.h>
 22 
 23 /* The registered clock event devices */
 24 static LIST_HEAD(clockevent_devices);
 25 static LIST_HEAD(clockevents_released);
 26 
 27 /* Notification for clock events */
 28 static RAW_NOTIFIER_HEAD(clockevents_chain);
 29 
 30 /* Protection for the above */
 31 static DEFINE_RAW_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  */
 40 unsigned long clockevent_delta2ns(unsigned long latch,
 41                                   struct clock_event_device *evt)
 42 {
 43         u64 clc = ((u64) latch << evt->shift);
 44 
 45         if (unlikely(!evt->mult)) {
 46                 evt->mult = 1;
 47                 WARN_ON(1);
 48         }
 49 
 50         do_div(clc, evt->mult);
 51         if (clc < 1000)
 52                 clc = 1000;
 53         if (clc > LONG_MAX)
 54                 clc = LONG_MAX;
 55 
 56         return (unsigned long) clc;
 57 }
 58 
 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  */
 66 void 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;
 72         }
 73 }
 74 
 75 /**
 76  * clockevents_program_event - Reprogram the clock event device.
 77  * @expires:    absolute expiry time (monotonic clock)
 78  *
 79  * Returns 0 on success, -ETIME when the event is in the past.
 80  */
 81 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
 82                               ktime_t now)
 83 {
 84         unsigned long long clc;
 85         int64_t delta;
 86 
 87         if (unlikely(expires.tv64 < 0)) {
 88                 WARN_ON_ONCE(1);
 89                 return -ETIME;
 90         }
 91 
 92         delta = ktime_to_ns(ktime_sub(expires, now));
 93 
 94         ftrace_event_program_event(&expires, &delta);
 95 
 96         if (delta <= 0)
 97                 return -ETIME;
 98 
 99         dev->next_event = expires;
100 
101         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
102                 return 0;
103 
104         if (delta > dev->max_delta_ns)
105                 delta = dev->max_delta_ns;
106         if (delta < dev->min_delta_ns)
107                 delta = dev->min_delta_ns;
108 
109         clc = delta * dev->mult;
110         clc >>= dev->shift;
111 
112         return dev->set_next_event((unsigned long) clc, dev);
113 }
114 
115 /**
116  * clockevents_register_notifier - register a clock events change listener
117  */
118 int clockevents_register_notifier(struct notifier_block *nb)
119 {
120         int ret;
121 
122         spin_lock(&clockevents_lock);
123         ret = raw_notifier_chain_register(&clockevents_chain, nb);
124         spin_unlock(&clockevents_lock);
125 
126         return ret;
127 }
128 
129 /*
130  * Notify about a clock event change. Called with clockevents_lock
131  * held.
132  */
133 static void clockevents_do_notify(unsigned long reason, void *dev)
134 {
135         raw_notifier_call_chain(&clockevents_chain, reason, dev);
136 }
137 
138 /*
139  * Called after a notify add to make devices available which were
140  * released from the notifier call.
141  */
142 static void clockevents_notify_released(void)
143 {
144         struct clock_event_device *dev;
145 
146         while (!list_empty(&clockevents_released)) {
147                 dev = list_entry(clockevents_released.next,
148                                  struct clock_event_device, list);
149                 list_del(&dev->list);
150                 list_add(&dev->list, &clockevent_devices);
151                 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
152         }
153 }
154 
155 /**
156  * clockevents_register_device - register a clock event device
157  * @dev:        device to register
158  */
159 void clockevents_register_device(struct clock_event_device *dev)
160 {
161         BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
162         /*
163          * A nsec2cyc multiplicator of 0 is invalid and we'd crash
164          * on it, so fix it up and emit a warning:
165          */
166         if (unlikely(!dev->mult)) {
167                 dev->mult = 1;
168                 WARN_ON(1);
169         }
170 
171         spin_lock(&clockevents_lock);
172 
173         list_add(&dev->list, &clockevent_devices);
174         clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
175         clockevents_notify_released();
176 
177         spin_unlock(&clockevents_lock);
178 }
179 
180 /*
181  * Noop handler when we shut down an event device
182  */
183 static void clockevents_handle_noop(struct clock_event_device *dev)
184 {
185 }
186 
187 /**
188  * clockevents_exchange_device - release and request clock devices
189  * @old:        device to release (can be NULL)
190  * @new:        device to request (can be NULL)
191  *
192  * Called from the notifier chain. clockevents_lock is held already
193  */
194 void clockevents_exchange_device(struct clock_event_device *old,
195                                  struct clock_event_device *new)
196 {
197         unsigned long flags;
198 
199         local_irq_save(flags);
200         /*
201          * Caller releases a clock event device. We queue it into the
202          * released list and do a notify add later.
203          */
204         if (old) {
205                 old->event_handler = clockevents_handle_noop;
206                 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
207                 list_del(&old->list);
208                 list_add(&old->list, &clockevents_released);
209         }
210 
211         if (new) {
212                 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
213                 clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN);
214         }
215         local_irq_restore(flags);
216 }
217 
218 #ifdef CONFIG_GENERIC_CLOCKEVENTS
219 /**
220  * clockevents_notify - notification about relevant events
221  */
222 void clockevents_notify(unsigned long reason, void *arg)
223 {
224         struct list_head *node, *tmp;
225 
226         spin_lock(&clockevents_lock);
227         clockevents_do_notify(reason, arg);
228 
229         switch (reason) {
230         case CLOCK_EVT_NOTIFY_CPU_DEAD:
231                 /*
232                  * Unregister the clock event devices which were
233                  * released from the users in the notify chain.
234                  */
235                 list_for_each_safe(node, tmp, &clockevents_released)
236                         list_del(node);
237                 break;
238         default:
239                 break;
240         }
241         spin_unlock(&clockevents_lock);
242 }
243 EXPORT_SYMBOL_GPL(clockevents_notify);
244 #endif
245 
  This page was automatically generated by the LXR engine.