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