26 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/sched.h>
40 tc->
nsec = start_tstamp;
61 cycle_now = tc->
cc->read(tc->
cc);
64 cycle_delta = (cycle_now - tc->
cycle_last) & tc->
cc->mask;
67 ns_offset = cyclecounter_cyc2ns(tc->
cc, cycle_delta);
80 nsec = timecounter_read_delta(tc);
99 if (cycle_delta > tc->
cc->mask / 2) {
100 cycle_delta = (tc->
cycle_last - cycle_tstamp) & tc->
cc->mask;
101 nsec = tc->
nsec - cyclecounter_cyc2ns(tc->
cc, cycle_delta);
103 nsec = cyclecounter_cyc2ns(tc->
cc, cycle_delta) + tc->
nsec;
143 tmp = ((
u64)maxsec * from) >> 32;
153 for (sft = 32; sft > 0; sft--) {
154 tmp = (
u64) to << sft;
157 if ((tmp >> sftacc) == 0)
177 static char override_name[32];
178 static int finished_booting;
180 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
186 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
188 static int watchdog_running;
189 static atomic_t watchdog_reset_pending;
191 static int clocksource_watchdog_kthread(
void *
data);
192 static void __clocksource_change_rating(
struct clocksource *
cs,
int rating);
197 #define WATCHDOG_INTERVAL (HZ >> 1)
198 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
213 if (finished_booting)
221 __clocksource_unstable(cs);
239 if (list_empty(&cs->wd_list))
240 list_add(&cs->wd_list, &watchdog_list);
241 __clocksource_unstable(cs);
243 spin_unlock_irqrestore(&watchdog_lock, flags);
246 static void clocksource_watchdog(
unsigned long data)
250 int64_t wd_nsec, cs_nsec;
253 spin_lock(&watchdog_lock);
254 if (!watchdog_running)
257 reset_pending =
atomic_read(&watchdog_reset_pending);
263 if (finished_booting)
269 csnow = cs->
read(cs);
270 wdnow = watchdog->
read(watchdog);
282 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->
mask,
285 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
294 if ((
abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
295 clocksource_unstable(cs, cs_nsec - wd_nsec);
324 if (next_cpu >= nr_cpu_ids)
325 next_cpu = cpumask_first(cpu_online_mask);
329 spin_unlock(&watchdog_lock);
332 static inline void clocksource_start_watchdog(
void)
334 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
337 watchdog_timer.function = clocksource_watchdog;
339 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
340 watchdog_running = 1;
343 static inline void clocksource_stop_watchdog(
void)
345 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
348 watchdog_running = 0;
351 static inline void clocksource_reset_watchdog(
void)
359 static
void clocksource_resume_watchdog(
void)
364 static void clocksource_enqueue_watchdog(
struct clocksource *cs)
371 list_add(&cs->wd_list, &watchdog_list);
381 clocksource_reset_watchdog();
385 clocksource_start_watchdog();
386 spin_unlock_irqrestore(&watchdog_lock, flags);
389 static void clocksource_dequeue_watchdog(
struct clocksource *cs)
397 list_del_init(&cs->wd_list);
398 }
else if (cs == watchdog) {
400 clocksource_reset_watchdog();
412 clocksource_stop_watchdog();
413 spin_unlock_irqrestore(&watchdog_lock, flags);
416 static int clocksource_watchdog_kthread(
void *data)
426 list_del_init(&cs->wd_list);
427 list_add(&cs->wd_list, &unstable);
430 clocksource_stop_watchdog();
431 spin_unlock_irqrestore(&watchdog_lock, flags);
435 list_del_init(&cs->wd_list);
436 __clocksource_change_rating(cs, 0);
444 static void clocksource_enqueue_watchdog(
struct clocksource *cs)
450 static inline void clocksource_dequeue_watchdog(
struct clocksource *cs) { }
451 static inline void clocksource_resume_watchdog(
void) { }
452 static inline int clocksource_watchdog_kthread(
void *data) {
return 0; }
479 clocksource_resume_watchdog();
491 clocksource_resume_watchdog();
517 u64 max_nsecs, max_cycles;
542 max_nsecs = clocksource_cyc2ns(max_cycles, cs->
mult - cs->
maxadj,
551 return max_nsecs - (max_nsecs >> 3);
554 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
564 static void clocksource_select(
void)
568 if (!finished_booting || list_empty(&clocksource_list))
585 "HRT compatible. Cannot switch while in "
586 "HRT/NOHZ mode\n", cs->
name);
587 override_name[0] = 0;
593 if (curr_clocksource != best) {
595 curr_clocksource = best;
602 static inline void clocksource_select(
void) { }
613 static int __init clocksource_done_booting(
void)
619 finished_booting = 1;
624 clocksource_watchdog_kthread(
NULL);
627 clocksource_select();
636 static void clocksource_enqueue(
struct clocksource *cs)
645 list_add(&cs->list, entry);
672 sec = (cs->mask - (cs->mask >> 3));
677 else if (sec > 600 && cs->mask >
UINT_MAX)
688 cs->maxadj = clocksource_max_adjustment(cs);
689 while ((cs->mult + cs->maxadj < cs->mult)
690 || (cs->mult - cs->maxadj > cs->mult)) {
693 cs->maxadj = clocksource_max_adjustment(cs);
696 cs->max_idle_ns = clocksource_max_deferment(cs);
719 clocksource_enqueue(cs);
720 clocksource_enqueue_watchdog(cs);
721 clocksource_select();
737 cs->
maxadj = clocksource_max_adjustment(cs);
739 "Clocksource %s might overflow on 11%% adjustment\n",
746 clocksource_enqueue(cs);
747 clocksource_enqueue_watchdog(cs);
748 clocksource_select();
758 clocksource_enqueue(cs);
759 clocksource_select();
770 __clocksource_change_rating(cs, rating);
782 clocksource_dequeue_watchdog(cs);
784 clocksource_select();
799 sysfs_show_current_clocksources(
struct device *
dev,
828 if (count >=
sizeof(override_name))
832 if (buf[count-1] ==
'\n')
838 memcpy(override_name, buf, count);
839 override_name[
count] = 0;
840 clocksource_select();
856 sysfs_show_available_clocksources(
struct device *dev,
886 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
887 sysfs_override_clocksource);
890 sysfs_show_available_clocksources,
NULL);
892 static struct bus_type clocksource_subsys = {
893 .
name =
"clocksource",
894 .dev_name =
"clocksource",
897 static struct device device_clocksource = {
899 .bus = &clocksource_subsys,
902 static int __init init_clocksource_sysfs(
void)
911 &dev_attr_current_clocksource);
915 &dev_attr_available_clocksource);
929 static int __init boot_override_clocksource(
char*
str)
933 strlcpy(override_name, str,
sizeof(override_name));
938 __setup(
"clocksource=", boot_override_clocksource);
947 static int __init boot_override_clock(
char* str)
949 if (!
strcmp(str,
"pmtmr")) {
950 printk(
"Warning: clock=pmtmr is deprecated. "
951 "Use clocksource=acpi_pm.\n");
952 return boot_override_clocksource(
"acpi_pm");
954 printk(
"Warning! clock= boot option is deprecated. "
955 "Use clocksource=xyz\n");
956 return boot_override_clocksource(str);
959 __setup(
"clock=", boot_override_clock);