| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/kernel/time/clocksource.c | 
 | 3 |  * | 
 | 4 |  * This file contains the functions which manage clocksource drivers. | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) | 
 | 7 |  * | 
 | 8 |  * This program is free software; you can redistribute it and/or modify | 
 | 9 |  * it under the terms of the GNU General Public License as published by | 
 | 10 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 11 |  * (at your option) any later version. | 
 | 12 |  * | 
 | 13 |  * This program is distributed in the hope that it will be useful, | 
 | 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 16 |  * GNU General Public License for more details. | 
 | 17 |  * | 
 | 18 |  * You should have received a copy of the GNU General Public License | 
 | 19 |  * along with this program; if not, write to the Free Software | 
 | 20 |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 21 |  * | 
 | 22 |  * TODO WishList: | 
 | 23 |  *   o Allow clocksource drivers to be unregistered | 
 | 24 |  *   o get rid of clocksource_jiffies extern | 
 | 25 |  */ | 
 | 26 |  | 
 | 27 | #include <linux/clocksource.h> | 
 | 28 | #include <linux/sysdev.h> | 
 | 29 | #include <linux/init.h> | 
 | 30 | #include <linux/module.h> | 
| Mathieu Desnoyers | dc29a36 | 2007-02-10 01:43:43 -0800 | [diff] [blame] | 31 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 32 | #include <linux/tick.h> | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 33 |  | 
 | 34 | /* XXX - Would like a better way for initializing curr_clocksource */ | 
 | 35 | extern struct clocksource clocksource_jiffies; | 
 | 36 |  | 
 | 37 | /*[Clocksource internal variables]--------- | 
 | 38 |  * curr_clocksource: | 
 | 39 |  *	currently selected clocksource. Initialized to clocksource_jiffies. | 
 | 40 |  * next_clocksource: | 
 | 41 |  *	pending next selected clocksource. | 
 | 42 |  * clocksource_list: | 
 | 43 |  *	linked list with the registered clocksources | 
 | 44 |  * clocksource_lock: | 
 | 45 |  *	protects manipulations to curr_clocksource and next_clocksource | 
 | 46 |  *	and the clocksource_list | 
 | 47 |  * override_name: | 
 | 48 |  *	Name of the user-specified clocksource. | 
 | 49 |  */ | 
 | 50 | static struct clocksource *curr_clocksource = &clocksource_jiffies; | 
 | 51 | static struct clocksource *next_clocksource; | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 52 | static struct clocksource *clocksource_override; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 53 | static LIST_HEAD(clocksource_list); | 
 | 54 | static DEFINE_SPINLOCK(clocksource_lock); | 
 | 55 | static char override_name[32]; | 
 | 56 | static int finished_booting; | 
 | 57 |  | 
| john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 58 | /* clocksource_done_booting - Called near the end of core bootup | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 59 |  * | 
| john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 60 |  * Hack to avoid lots of clocksource churn at boot time. | 
 | 61 |  * We use fs_initcall because we want this to start before | 
 | 62 |  * device_initcall but after subsys_initcall. | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 63 |  */ | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 64 | static int __init clocksource_done_booting(void) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 65 | { | 
 | 66 | 	finished_booting = 1; | 
 | 67 | 	return 0; | 
 | 68 | } | 
| john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 69 | fs_initcall(clocksource_done_booting); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 70 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 71 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG | 
 | 72 | static LIST_HEAD(watchdog_list); | 
 | 73 | static struct clocksource *watchdog; | 
 | 74 | static struct timer_list watchdog_timer; | 
 | 75 | static DEFINE_SPINLOCK(watchdog_lock); | 
 | 76 | static cycle_t watchdog_last; | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 77 | static int watchdog_resumed; | 
 | 78 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 79 | /* | 
| Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 80 |  * Interval: 0.5sec Threshold: 0.0625s | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 81 |  */ | 
 | 82 | #define WATCHDOG_INTERVAL (HZ >> 1) | 
| Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 83 | #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 84 |  | 
 | 85 | static void clocksource_ratewd(struct clocksource *cs, int64_t delta) | 
 | 86 | { | 
| Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 87 | 	if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD) | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 88 | 		return; | 
 | 89 |  | 
 | 90 | 	printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n", | 
 | 91 | 	       cs->name, delta); | 
 | 92 | 	cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); | 
 | 93 | 	clocksource_change_rating(cs, 0); | 
 | 94 | 	cs->flags &= ~CLOCK_SOURCE_WATCHDOG; | 
 | 95 | 	list_del(&cs->wd_list); | 
 | 96 | } | 
 | 97 |  | 
 | 98 | static void clocksource_watchdog(unsigned long data) | 
 | 99 | { | 
 | 100 | 	struct clocksource *cs, *tmp; | 
 | 101 | 	cycle_t csnow, wdnow; | 
 | 102 | 	int64_t wd_nsec, cs_nsec; | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 103 | 	int resumed; | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 104 |  | 
 | 105 | 	spin_lock(&watchdog_lock); | 
 | 106 |  | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 107 | 	resumed = watchdog_resumed; | 
 | 108 | 	if (unlikely(resumed)) | 
 | 109 | 		watchdog_resumed = 0; | 
 | 110 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 111 | 	wdnow = watchdog->read(); | 
 | 112 | 	wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask); | 
 | 113 | 	watchdog_last = wdnow; | 
 | 114 |  | 
 | 115 | 	list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { | 
 | 116 | 		csnow = cs->read(); | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 117 |  | 
 | 118 | 		if (unlikely(resumed)) { | 
 | 119 | 			cs->wd_last = csnow; | 
 | 120 | 			continue; | 
 | 121 | 		} | 
 | 122 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 123 | 		/* Initialized ? */ | 
 | 124 | 		if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) { | 
 | 125 | 			if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && | 
 | 126 | 			    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { | 
 | 127 | 				cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 128 | 				/* | 
 | 129 | 				 * We just marked the clocksource as | 
 | 130 | 				 * highres-capable, notify the rest of the | 
 | 131 | 				 * system as well so that we transition | 
 | 132 | 				 * into high-res mode: | 
 | 133 | 				 */ | 
 | 134 | 				tick_clock_notify(); | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 135 | 			} | 
 | 136 | 			cs->flags |= CLOCK_SOURCE_WATCHDOG; | 
 | 137 | 			cs->wd_last = csnow; | 
 | 138 | 		} else { | 
 | 139 | 			cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask); | 
 | 140 | 			cs->wd_last = csnow; | 
 | 141 | 			/* Check the delta. Might remove from the list ! */ | 
 | 142 | 			clocksource_ratewd(cs, cs_nsec - wd_nsec); | 
 | 143 | 		} | 
 | 144 | 	} | 
 | 145 |  | 
 | 146 | 	if (!list_empty(&watchdog_list)) { | 
 | 147 | 		__mod_timer(&watchdog_timer, | 
 | 148 | 			    watchdog_timer.expires + WATCHDOG_INTERVAL); | 
 | 149 | 	} | 
 | 150 | 	spin_unlock(&watchdog_lock); | 
 | 151 | } | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 152 | static void clocksource_resume_watchdog(void) | 
 | 153 | { | 
 | 154 | 	spin_lock(&watchdog_lock); | 
 | 155 | 	watchdog_resumed = 1; | 
 | 156 | 	spin_unlock(&watchdog_lock); | 
 | 157 | } | 
 | 158 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 159 | static void clocksource_check_watchdog(struct clocksource *cs) | 
 | 160 | { | 
 | 161 | 	struct clocksource *cse; | 
 | 162 | 	unsigned long flags; | 
 | 163 |  | 
 | 164 | 	spin_lock_irqsave(&watchdog_lock, flags); | 
 | 165 | 	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { | 
 | 166 | 		int started = !list_empty(&watchdog_list); | 
 | 167 |  | 
 | 168 | 		list_add(&cs->wd_list, &watchdog_list); | 
 | 169 | 		if (!started && watchdog) { | 
 | 170 | 			watchdog_last = watchdog->read(); | 
 | 171 | 			watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; | 
 | 172 | 			add_timer(&watchdog_timer); | 
 | 173 | 		} | 
| Thomas Gleixner | 948ac6d | 2007-03-25 14:42:51 +0200 | [diff] [blame] | 174 | 	} else { | 
 | 175 | 		if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 176 | 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | 
 | 177 |  | 
 | 178 | 		if (!watchdog || cs->rating > watchdog->rating) { | 
 | 179 | 			if (watchdog) | 
 | 180 | 				del_timer(&watchdog_timer); | 
 | 181 | 			watchdog = cs; | 
 | 182 | 			init_timer(&watchdog_timer); | 
 | 183 | 			watchdog_timer.function = clocksource_watchdog; | 
 | 184 |  | 
 | 185 | 			/* Reset watchdog cycles */ | 
 | 186 | 			list_for_each_entry(cse, &watchdog_list, wd_list) | 
 | 187 | 				cse->flags &= ~CLOCK_SOURCE_WATCHDOG; | 
 | 188 | 			/* Start if list is not empty */ | 
 | 189 | 			if (!list_empty(&watchdog_list)) { | 
 | 190 | 				watchdog_last = watchdog->read(); | 
 | 191 | 				watchdog_timer.expires = | 
 | 192 | 					jiffies + WATCHDOG_INTERVAL; | 
 | 193 | 				add_timer(&watchdog_timer); | 
 | 194 | 			} | 
 | 195 | 		} | 
 | 196 | 	} | 
 | 197 | 	spin_unlock_irqrestore(&watchdog_lock, flags); | 
 | 198 | } | 
 | 199 | #else | 
 | 200 | static void clocksource_check_watchdog(struct clocksource *cs) | 
 | 201 | { | 
 | 202 | 	if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) | 
 | 203 | 		cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | 
 | 204 | } | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 205 |  | 
 | 206 | static inline void clocksource_resume_watchdog(void) { } | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 207 | #endif | 
 | 208 |  | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 209 | /** | 
| Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 210 |  * clocksource_resume - resume the clocksource(s) | 
 | 211 |  */ | 
 | 212 | void clocksource_resume(void) | 
 | 213 | { | 
 | 214 | 	struct list_head *tmp; | 
 | 215 | 	unsigned long flags; | 
 | 216 |  | 
 | 217 | 	spin_lock_irqsave(&clocksource_lock, flags); | 
 | 218 |  | 
 | 219 | 	list_for_each(tmp, &clocksource_list) { | 
 | 220 | 		struct clocksource *cs; | 
 | 221 |  | 
 | 222 | 		cs = list_entry(tmp, struct clocksource, list); | 
 | 223 | 		if (cs->resume) | 
 | 224 | 			cs->resume(); | 
 | 225 | 	} | 
 | 226 |  | 
 | 227 | 	clocksource_resume_watchdog(); | 
 | 228 |  | 
 | 229 | 	spin_unlock_irqrestore(&clocksource_lock, flags); | 
 | 230 | } | 
 | 231 |  | 
 | 232 | /** | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 233 |  * clocksource_get_next - Returns the selected clocksource | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 234 |  * | 
 | 235 |  */ | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 236 | struct clocksource *clocksource_get_next(void) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 237 | { | 
 | 238 | 	unsigned long flags; | 
 | 239 |  | 
 | 240 | 	spin_lock_irqsave(&clocksource_lock, flags); | 
 | 241 | 	if (next_clocksource && finished_booting) { | 
 | 242 | 		curr_clocksource = next_clocksource; | 
 | 243 | 		next_clocksource = NULL; | 
 | 244 | 	} | 
 | 245 | 	spin_unlock_irqrestore(&clocksource_lock, flags); | 
 | 246 |  | 
 | 247 | 	return curr_clocksource; | 
 | 248 | } | 
 | 249 |  | 
 | 250 | /** | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 251 |  * select_clocksource - Selects the best registered clocksource. | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 252 |  * | 
 | 253 |  * Private function. Must hold clocksource_lock when called. | 
 | 254 |  * | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 255 |  * Select the clocksource with the best rating, or the clocksource, | 
 | 256 |  * which is selected by userspace override. | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 257 |  */ | 
 | 258 | static struct clocksource *select_clocksource(void) | 
 | 259 | { | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 260 | 	struct clocksource *next; | 
 | 261 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 262 | 	if (list_empty(&clocksource_list)) | 
 | 263 | 		return NULL; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 264 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 265 | 	if (clocksource_override) | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 266 | 		next = clocksource_override; | 
 | 267 | 	else | 
 | 268 | 		next = list_entry(clocksource_list.next, struct clocksource, | 
 | 269 | 				  list); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 270 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 271 | 	if (next == curr_clocksource) | 
 | 272 | 		return NULL; | 
 | 273 |  | 
 | 274 | 	return next; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 275 | } | 
 | 276 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 277 | /* | 
 | 278 |  * Enqueue the clocksource sorted by rating | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 279 |  */ | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 280 | static int clocksource_enqueue(struct clocksource *c) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 281 | { | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 282 | 	struct list_head *tmp, *entry = &clocksource_list; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 283 |  | 
 | 284 | 	list_for_each(tmp, &clocksource_list) { | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 285 | 		struct clocksource *cs; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 286 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 287 | 		cs = list_entry(tmp, struct clocksource, list); | 
 | 288 | 		if (cs == c) | 
 | 289 | 			return -EBUSY; | 
 | 290 | 		/* Keep track of the place, where to insert */ | 
 | 291 | 		if (cs->rating >= c->rating) | 
 | 292 | 			entry = tmp; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 293 | 	} | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 294 | 	list_add(&c->list, entry); | 
 | 295 |  | 
 | 296 | 	if (strlen(c->name) == strlen(override_name) && | 
 | 297 | 	    !strcmp(c->name, override_name)) | 
 | 298 | 		clocksource_override = c; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 299 |  | 
 | 300 | 	return 0; | 
 | 301 | } | 
 | 302 |  | 
 | 303 | /** | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 304 |  * clocksource_register - Used to install new clocksources | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 305 |  * @t:		clocksource to be registered | 
 | 306 |  * | 
 | 307 |  * Returns -EBUSY if registration fails, zero otherwise. | 
 | 308 |  */ | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 309 | int clocksource_register(struct clocksource *c) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 310 | { | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 311 | 	unsigned long flags; | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 312 | 	int ret; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 313 |  | 
 | 314 | 	spin_lock_irqsave(&clocksource_lock, flags); | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 315 | 	ret = clocksource_enqueue(c); | 
 | 316 | 	if (!ret) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 317 | 		next_clocksource = select_clocksource(); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 318 | 	spin_unlock_irqrestore(&clocksource_lock, flags); | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 319 | 	if (!ret) | 
 | 320 | 		clocksource_check_watchdog(c); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 321 | 	return ret; | 
 | 322 | } | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 323 | EXPORT_SYMBOL(clocksource_register); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 324 |  | 
 | 325 | /** | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 326 |  * clocksource_change_rating - Change the rating of a registered clocksource | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 327 |  * | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 328 |  */ | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 329 | void clocksource_change_rating(struct clocksource *cs, int rating) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 330 | { | 
 | 331 | 	unsigned long flags; | 
 | 332 |  | 
 | 333 | 	spin_lock_irqsave(&clocksource_lock, flags); | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 334 | 	list_del(&cs->list); | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 335 | 	cs->rating = rating; | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 336 | 	clocksource_enqueue(cs); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 337 | 	next_clocksource = select_clocksource(); | 
 | 338 | 	spin_unlock_irqrestore(&clocksource_lock, flags); | 
 | 339 | } | 
 | 340 |  | 
| Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 341 | #ifdef CONFIG_SYSFS | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 342 | /** | 
 | 343 |  * sysfs_show_current_clocksources - sysfs interface for current clocksource | 
 | 344 |  * @dev:	unused | 
 | 345 |  * @buf:	char buffer to be filled with clocksource list | 
 | 346 |  * | 
 | 347 |  * Provides sysfs interface for listing current clocksource. | 
 | 348 |  */ | 
 | 349 | static ssize_t | 
 | 350 | sysfs_show_current_clocksources(struct sys_device *dev, char *buf) | 
 | 351 | { | 
 | 352 | 	char *curr = buf; | 
 | 353 |  | 
 | 354 | 	spin_lock_irq(&clocksource_lock); | 
 | 355 | 	curr += sprintf(curr, "%s ", curr_clocksource->name); | 
 | 356 | 	spin_unlock_irq(&clocksource_lock); | 
 | 357 |  | 
 | 358 | 	curr += sprintf(curr, "\n"); | 
 | 359 |  | 
 | 360 | 	return curr - buf; | 
 | 361 | } | 
 | 362 |  | 
 | 363 | /** | 
 | 364 |  * sysfs_override_clocksource - interface for manually overriding clocksource | 
 | 365 |  * @dev:	unused | 
 | 366 |  * @buf:	name of override clocksource | 
 | 367 |  * @count:	length of buffer | 
 | 368 |  * | 
 | 369 |  * Takes input from sysfs interface for manually overriding the default | 
 | 370 |  * clocksource selction. | 
 | 371 |  */ | 
 | 372 | static ssize_t sysfs_override_clocksource(struct sys_device *dev, | 
 | 373 | 					  const char *buf, size_t count) | 
 | 374 | { | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 375 | 	struct clocksource *ovr = NULL; | 
 | 376 | 	struct list_head *tmp; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 377 | 	size_t ret = count; | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 378 | 	int len; | 
 | 379 |  | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 380 | 	/* strings from sysfs write are not 0 terminated! */ | 
 | 381 | 	if (count >= sizeof(override_name)) | 
 | 382 | 		return -EINVAL; | 
 | 383 |  | 
 | 384 | 	/* strip of \n: */ | 
 | 385 | 	if (buf[count-1] == '\n') | 
 | 386 | 		count--; | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 387 |  | 
 | 388 | 	spin_lock_irq(&clocksource_lock); | 
 | 389 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 390 | 	if (count > 0) | 
 | 391 | 		memcpy(override_name, buf, count); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 392 | 	override_name[count] = 0; | 
 | 393 |  | 
| Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 394 | 	len = strlen(override_name); | 
 | 395 | 	if (len) { | 
 | 396 | 		ovr = clocksource_override; | 
 | 397 | 		/* try to select it: */ | 
 | 398 | 		list_for_each(tmp, &clocksource_list) { | 
 | 399 | 			struct clocksource *cs; | 
 | 400 |  | 
 | 401 | 			cs = list_entry(tmp, struct clocksource, list); | 
 | 402 | 			if (strlen(cs->name) == len && | 
 | 403 | 			    !strcmp(cs->name, override_name)) | 
 | 404 | 				ovr = cs; | 
 | 405 | 		} | 
 | 406 | 	} | 
 | 407 |  | 
 | 408 | 	/* Reselect, when the override name has changed */ | 
 | 409 | 	if (ovr != clocksource_override) { | 
 | 410 | 		clocksource_override = ovr; | 
 | 411 | 		next_clocksource = select_clocksource(); | 
 | 412 | 	} | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 413 |  | 
 | 414 | 	spin_unlock_irq(&clocksource_lock); | 
 | 415 |  | 
 | 416 | 	return ret; | 
 | 417 | } | 
 | 418 |  | 
 | 419 | /** | 
 | 420 |  * sysfs_show_available_clocksources - sysfs interface for listing clocksource | 
 | 421 |  * @dev:	unused | 
 | 422 |  * @buf:	char buffer to be filled with clocksource list | 
 | 423 |  * | 
 | 424 |  * Provides sysfs interface for listing registered clocksources | 
 | 425 |  */ | 
 | 426 | static ssize_t | 
 | 427 | sysfs_show_available_clocksources(struct sys_device *dev, char *buf) | 
 | 428 | { | 
 | 429 | 	struct list_head *tmp; | 
 | 430 | 	char *curr = buf; | 
 | 431 |  | 
 | 432 | 	spin_lock_irq(&clocksource_lock); | 
 | 433 | 	list_for_each(tmp, &clocksource_list) { | 
 | 434 | 		struct clocksource *src; | 
 | 435 |  | 
 | 436 | 		src = list_entry(tmp, struct clocksource, list); | 
 | 437 | 		curr += sprintf(curr, "%s ", src->name); | 
 | 438 | 	} | 
 | 439 | 	spin_unlock_irq(&clocksource_lock); | 
 | 440 |  | 
 | 441 | 	curr += sprintf(curr, "\n"); | 
 | 442 |  | 
 | 443 | 	return curr - buf; | 
 | 444 | } | 
 | 445 |  | 
 | 446 | /* | 
 | 447 |  * Sysfs setup bits: | 
 | 448 |  */ | 
 | 449 | static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources, | 
| Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 450 | 		   sysfs_override_clocksource); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 451 |  | 
 | 452 | static SYSDEV_ATTR(available_clocksource, 0600, | 
| Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 453 | 		   sysfs_show_available_clocksources, NULL); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 454 |  | 
 | 455 | static struct sysdev_class clocksource_sysclass = { | 
 | 456 | 	set_kset_name("clocksource"), | 
 | 457 | }; | 
 | 458 |  | 
 | 459 | static struct sys_device device_clocksource = { | 
 | 460 | 	.id	= 0, | 
 | 461 | 	.cls	= &clocksource_sysclass, | 
 | 462 | }; | 
 | 463 |  | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 464 | static int __init init_clocksource_sysfs(void) | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 465 | { | 
 | 466 | 	int error = sysdev_class_register(&clocksource_sysclass); | 
 | 467 |  | 
 | 468 | 	if (!error) | 
 | 469 | 		error = sysdev_register(&device_clocksource); | 
 | 470 | 	if (!error) | 
 | 471 | 		error = sysdev_create_file( | 
 | 472 | 				&device_clocksource, | 
 | 473 | 				&attr_current_clocksource); | 
 | 474 | 	if (!error) | 
 | 475 | 		error = sysdev_create_file( | 
 | 476 | 				&device_clocksource, | 
 | 477 | 				&attr_available_clocksource); | 
 | 478 | 	return error; | 
 | 479 | } | 
 | 480 |  | 
 | 481 | device_initcall(init_clocksource_sysfs); | 
| Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 482 | #endif /* CONFIG_SYSFS */ | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 483 |  | 
 | 484 | /** | 
 | 485 |  * boot_override_clocksource - boot clock override | 
 | 486 |  * @str:	override name | 
 | 487 |  * | 
 | 488 |  * Takes a clocksource= boot argument and uses it | 
 | 489 |  * as the clocksource override name. | 
 | 490 |  */ | 
 | 491 | static int __init boot_override_clocksource(char* str) | 
 | 492 | { | 
 | 493 | 	unsigned long flags; | 
 | 494 | 	spin_lock_irqsave(&clocksource_lock, flags); | 
 | 495 | 	if (str) | 
 | 496 | 		strlcpy(override_name, str, sizeof(override_name)); | 
 | 497 | 	spin_unlock_irqrestore(&clocksource_lock, flags); | 
 | 498 | 	return 1; | 
 | 499 | } | 
 | 500 |  | 
 | 501 | __setup("clocksource=", boot_override_clocksource); | 
 | 502 |  | 
 | 503 | /** | 
 | 504 |  * boot_override_clock - Compatibility layer for deprecated boot option | 
 | 505 |  * @str:	override name | 
 | 506 |  * | 
 | 507 |  * DEPRECATED! Takes a clock= boot argument and uses it | 
 | 508 |  * as the clocksource override name | 
 | 509 |  */ | 
 | 510 | static int __init boot_override_clock(char* str) | 
 | 511 | { | 
| john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 512 | 	if (!strcmp(str, "pmtmr")) { | 
 | 513 | 		printk("Warning: clock=pmtmr is deprecated. " | 
 | 514 | 			"Use clocksource=acpi_pm.\n"); | 
 | 515 | 		return boot_override_clocksource("acpi_pm"); | 
 | 516 | 	} | 
 | 517 | 	printk("Warning! clock= boot option is deprecated. " | 
 | 518 | 		"Use clocksource=xyz\n"); | 
| john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 519 | 	return boot_override_clocksource(str); | 
 | 520 | } | 
 | 521 |  | 
 | 522 | __setup("clock=", boot_override_clock); |