1734efb46Sjohn stultz /* 2734efb46Sjohn stultz * linux/kernel/time/clocksource.c 3734efb46Sjohn stultz * 4734efb46Sjohn stultz * This file contains the functions which manage clocksource drivers. 5734efb46Sjohn stultz * 6734efb46Sjohn stultz * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) 7734efb46Sjohn stultz * 8734efb46Sjohn stultz * This program is free software; you can redistribute it and/or modify 9734efb46Sjohn stultz * it under the terms of the GNU General Public License as published by 10734efb46Sjohn stultz * the Free Software Foundation; either version 2 of the License, or 11734efb46Sjohn stultz * (at your option) any later version. 12734efb46Sjohn stultz * 13734efb46Sjohn stultz * This program is distributed in the hope that it will be useful, 14734efb46Sjohn stultz * but WITHOUT ANY WARRANTY; without even the implied warranty of 15734efb46Sjohn stultz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16734efb46Sjohn stultz * GNU General Public License for more details. 17734efb46Sjohn stultz * 18734efb46Sjohn stultz * You should have received a copy of the GNU General Public License 19734efb46Sjohn stultz * along with this program; if not, write to the Free Software 20734efb46Sjohn stultz * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21734efb46Sjohn stultz * 22734efb46Sjohn stultz * TODO WishList: 23734efb46Sjohn stultz * o Allow clocksource drivers to be unregistered 24734efb46Sjohn stultz */ 25734efb46Sjohn stultz 26d369a5d8SKay Sievers #include <linux/device.h> 27734efb46Sjohn stultz #include <linux/clocksource.h> 28734efb46Sjohn stultz #include <linux/init.h> 29734efb46Sjohn stultz #include <linux/module.h> 30dc29a365SMathieu Desnoyers #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ 3179bf2bb3SThomas Gleixner #include <linux/tick.h> 3201548f4dSMartin Schwidefsky #include <linux/kthread.h> 33734efb46Sjohn stultz 34a038a353SPatrick Ohly void timecounter_init(struct timecounter *tc, 35a038a353SPatrick Ohly const struct cyclecounter *cc, 36a038a353SPatrick Ohly u64 start_tstamp) 37a038a353SPatrick Ohly { 38a038a353SPatrick Ohly tc->cc = cc; 39a038a353SPatrick Ohly tc->cycle_last = cc->read(cc); 40a038a353SPatrick Ohly tc->nsec = start_tstamp; 41a038a353SPatrick Ohly } 423586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_init); 43a038a353SPatrick Ohly 44a038a353SPatrick Ohly /** 45a038a353SPatrick Ohly * timecounter_read_delta - get nanoseconds since last call of this function 46a038a353SPatrick Ohly * @tc: Pointer to time counter 47a038a353SPatrick Ohly * 48a038a353SPatrick Ohly * When the underlying cycle counter runs over, this will be handled 49a038a353SPatrick Ohly * correctly as long as it does not run over more than once between 50a038a353SPatrick Ohly * calls. 51a038a353SPatrick Ohly * 52a038a353SPatrick Ohly * The first call to this function for a new time counter initializes 53a038a353SPatrick Ohly * the time tracking and returns an undefined result. 54a038a353SPatrick Ohly */ 55a038a353SPatrick Ohly static u64 timecounter_read_delta(struct timecounter *tc) 56a038a353SPatrick Ohly { 57a038a353SPatrick Ohly cycle_t cycle_now, cycle_delta; 58a038a353SPatrick Ohly u64 ns_offset; 59a038a353SPatrick Ohly 60a038a353SPatrick Ohly /* read cycle counter: */ 61a038a353SPatrick Ohly cycle_now = tc->cc->read(tc->cc); 62a038a353SPatrick Ohly 63a038a353SPatrick Ohly /* calculate the delta since the last timecounter_read_delta(): */ 64a038a353SPatrick Ohly cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; 65a038a353SPatrick Ohly 66a038a353SPatrick Ohly /* convert to nanoseconds: */ 67a038a353SPatrick Ohly ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta); 68a038a353SPatrick Ohly 69a038a353SPatrick Ohly /* update time stamp of timecounter_read_delta() call: */ 70a038a353SPatrick Ohly tc->cycle_last = cycle_now; 71a038a353SPatrick Ohly 72a038a353SPatrick Ohly return ns_offset; 73a038a353SPatrick Ohly } 74a038a353SPatrick Ohly 75a038a353SPatrick Ohly u64 timecounter_read(struct timecounter *tc) 76a038a353SPatrick Ohly { 77a038a353SPatrick Ohly u64 nsec; 78a038a353SPatrick Ohly 79a038a353SPatrick Ohly /* increment time by nanoseconds since last call */ 80a038a353SPatrick Ohly nsec = timecounter_read_delta(tc); 81a038a353SPatrick Ohly nsec += tc->nsec; 82a038a353SPatrick Ohly tc->nsec = nsec; 83a038a353SPatrick Ohly 84a038a353SPatrick Ohly return nsec; 85a038a353SPatrick Ohly } 863586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_read); 87a038a353SPatrick Ohly 88a038a353SPatrick Ohly u64 timecounter_cyc2time(struct timecounter *tc, 89a038a353SPatrick Ohly cycle_t cycle_tstamp) 90a038a353SPatrick Ohly { 91a038a353SPatrick Ohly u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; 92a038a353SPatrick Ohly u64 nsec; 93a038a353SPatrick Ohly 94a038a353SPatrick Ohly /* 95a038a353SPatrick Ohly * Instead of always treating cycle_tstamp as more recent 96a038a353SPatrick Ohly * than tc->cycle_last, detect when it is too far in the 97a038a353SPatrick Ohly * future and treat it as old time stamp instead. 98a038a353SPatrick Ohly */ 99a038a353SPatrick Ohly if (cycle_delta > tc->cc->mask / 2) { 100a038a353SPatrick Ohly cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; 101a038a353SPatrick Ohly nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta); 102a038a353SPatrick Ohly } else { 103a038a353SPatrick Ohly nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec; 104a038a353SPatrick Ohly } 105a038a353SPatrick Ohly 106a038a353SPatrick Ohly return nsec; 107a038a353SPatrick Ohly } 1083586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_cyc2time); 109a038a353SPatrick Ohly 1107d2f944aSThomas Gleixner /** 1117d2f944aSThomas Gleixner * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks 1127d2f944aSThomas Gleixner * @mult: pointer to mult variable 1137d2f944aSThomas Gleixner * @shift: pointer to shift variable 1147d2f944aSThomas Gleixner * @from: frequency to convert from 1157d2f944aSThomas Gleixner * @to: frequency to convert to 1165fdade95SNicolas Pitre * @maxsec: guaranteed runtime conversion range in seconds 1177d2f944aSThomas Gleixner * 1187d2f944aSThomas Gleixner * The function evaluates the shift/mult pair for the scaled math 1197d2f944aSThomas Gleixner * operations of clocksources and clockevents. 1207d2f944aSThomas Gleixner * 1217d2f944aSThomas Gleixner * @to and @from are frequency values in HZ. For clock sources @to is 1227d2f944aSThomas Gleixner * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock 1237d2f944aSThomas Gleixner * event @to is the counter frequency and @from is NSEC_PER_SEC. 1247d2f944aSThomas Gleixner * 1255fdade95SNicolas Pitre * The @maxsec conversion range argument controls the time frame in 1267d2f944aSThomas Gleixner * seconds which must be covered by the runtime conversion with the 1277d2f944aSThomas Gleixner * calculated mult and shift factors. This guarantees that no 64bit 1287d2f944aSThomas Gleixner * overflow happens when the input value of the conversion is 1297d2f944aSThomas Gleixner * multiplied with the calculated mult factor. Larger ranges may 1307d2f944aSThomas Gleixner * reduce the conversion accuracy by chosing smaller mult and shift 1317d2f944aSThomas Gleixner * factors. 1327d2f944aSThomas Gleixner */ 1337d2f944aSThomas Gleixner void 1345fdade95SNicolas Pitre clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec) 1357d2f944aSThomas Gleixner { 1367d2f944aSThomas Gleixner u64 tmp; 1377d2f944aSThomas Gleixner u32 sft, sftacc= 32; 1387d2f944aSThomas Gleixner 1397d2f944aSThomas Gleixner /* 1407d2f944aSThomas Gleixner * Calculate the shift factor which is limiting the conversion 1417d2f944aSThomas Gleixner * range: 1427d2f944aSThomas Gleixner */ 1435fdade95SNicolas Pitre tmp = ((u64)maxsec * from) >> 32; 1447d2f944aSThomas Gleixner while (tmp) { 1457d2f944aSThomas Gleixner tmp >>=1; 1467d2f944aSThomas Gleixner sftacc--; 1477d2f944aSThomas Gleixner } 1487d2f944aSThomas Gleixner 1497d2f944aSThomas Gleixner /* 1507d2f944aSThomas Gleixner * Find the conversion shift/mult pair which has the best 1517d2f944aSThomas Gleixner * accuracy and fits the maxsec conversion range: 1527d2f944aSThomas Gleixner */ 1537d2f944aSThomas Gleixner for (sft = 32; sft > 0; sft--) { 1547d2f944aSThomas Gleixner tmp = (u64) to << sft; 155b5776c4aSjohn stultz tmp += from / 2; 1567d2f944aSThomas Gleixner do_div(tmp, from); 1577d2f944aSThomas Gleixner if ((tmp >> sftacc) == 0) 1587d2f944aSThomas Gleixner break; 1597d2f944aSThomas Gleixner } 1607d2f944aSThomas Gleixner *mult = tmp; 1617d2f944aSThomas Gleixner *shift = sft; 1627d2f944aSThomas Gleixner } 1637d2f944aSThomas Gleixner 164734efb46Sjohn stultz /*[Clocksource internal variables]--------- 165734efb46Sjohn stultz * curr_clocksource: 166f1b82746SMartin Schwidefsky * currently selected clocksource. 167734efb46Sjohn stultz * clocksource_list: 168734efb46Sjohn stultz * linked list with the registered clocksources 16975c5158fSMartin Schwidefsky * clocksource_mutex: 17075c5158fSMartin Schwidefsky * protects manipulations to curr_clocksource and the clocksource_list 171734efb46Sjohn stultz * override_name: 172734efb46Sjohn stultz * Name of the user-specified clocksource. 173734efb46Sjohn stultz */ 174f1b82746SMartin Schwidefsky static struct clocksource *curr_clocksource; 175734efb46Sjohn stultz static LIST_HEAD(clocksource_list); 17675c5158fSMartin Schwidefsky static DEFINE_MUTEX(clocksource_mutex); 17729b54078SThomas Gleixner #define CS_NAME_LEN 32 17829b54078SThomas Gleixner static char override_name[CS_NAME_LEN]; 17954a6bc0bSThomas Gleixner static int finished_booting; 180734efb46Sjohn stultz 1815d8b34fdSThomas Gleixner #ifdef CONFIG_CLOCKSOURCE_WATCHDOG 182f79e0258SMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work); 183f79e0258SMartin Schwidefsky 1845d8b34fdSThomas Gleixner static LIST_HEAD(watchdog_list); 1855d8b34fdSThomas Gleixner static struct clocksource *watchdog; 1865d8b34fdSThomas Gleixner static struct timer_list watchdog_timer; 187f79e0258SMartin Schwidefsky static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); 1885d8b34fdSThomas Gleixner static DEFINE_SPINLOCK(watchdog_lock); 189fb63a0ebSMartin Schwidefsky static int watchdog_running; 1909fb60336SThomas Gleixner static atomic_t watchdog_reset_pending; 191b52f52a0SThomas Gleixner 19201548f4dSMartin Schwidefsky static int clocksource_watchdog_kthread(void *data); 193d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating); 194c55c87c8SMartin Schwidefsky 1955d8b34fdSThomas Gleixner /* 19635c35d1aSDaniel Walker * Interval: 0.5sec Threshold: 0.0625s 1975d8b34fdSThomas Gleixner */ 1985d8b34fdSThomas Gleixner #define WATCHDOG_INTERVAL (HZ >> 1) 19935c35d1aSDaniel Walker #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) 2005d8b34fdSThomas Gleixner 20101548f4dSMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work) 20201548f4dSMartin Schwidefsky { 20301548f4dSMartin Schwidefsky /* 20401548f4dSMartin Schwidefsky * If kthread_run fails the next watchdog scan over the 20501548f4dSMartin Schwidefsky * watchdog_list will find the unstable clock again. 20601548f4dSMartin Schwidefsky */ 20701548f4dSMartin Schwidefsky kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog"); 20801548f4dSMartin Schwidefsky } 20901548f4dSMartin Schwidefsky 2107285dd7fSThomas Gleixner static void __clocksource_unstable(struct clocksource *cs) 2117285dd7fSThomas Gleixner { 2127285dd7fSThomas Gleixner cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); 2137285dd7fSThomas Gleixner cs->flags |= CLOCK_SOURCE_UNSTABLE; 21454a6bc0bSThomas Gleixner if (finished_booting) 2157285dd7fSThomas Gleixner schedule_work(&watchdog_work); 2167285dd7fSThomas Gleixner } 2177285dd7fSThomas Gleixner 2188cf4e750SMartin Schwidefsky static void clocksource_unstable(struct clocksource *cs, int64_t delta) 2195d8b34fdSThomas Gleixner { 2205d8b34fdSThomas Gleixner printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n", 2215d8b34fdSThomas Gleixner cs->name, delta); 2227285dd7fSThomas Gleixner __clocksource_unstable(cs); 2237285dd7fSThomas Gleixner } 2247285dd7fSThomas Gleixner 2257285dd7fSThomas Gleixner /** 2267285dd7fSThomas Gleixner * clocksource_mark_unstable - mark clocksource unstable via watchdog 2277285dd7fSThomas Gleixner * @cs: clocksource to be marked unstable 2287285dd7fSThomas Gleixner * 2297285dd7fSThomas Gleixner * This function is called instead of clocksource_change_rating from 2307285dd7fSThomas Gleixner * cpu hotplug code to avoid a deadlock between the clocksource mutex 2317285dd7fSThomas Gleixner * and the cpu hotplug mutex. It defers the update of the clocksource 2327285dd7fSThomas Gleixner * to the watchdog thread. 2337285dd7fSThomas Gleixner */ 2347285dd7fSThomas Gleixner void clocksource_mark_unstable(struct clocksource *cs) 2357285dd7fSThomas Gleixner { 2367285dd7fSThomas Gleixner unsigned long flags; 2377285dd7fSThomas Gleixner 2387285dd7fSThomas Gleixner spin_lock_irqsave(&watchdog_lock, flags); 2397285dd7fSThomas Gleixner if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) { 2407285dd7fSThomas Gleixner if (list_empty(&cs->wd_list)) 2417285dd7fSThomas Gleixner list_add(&cs->wd_list, &watchdog_list); 2427285dd7fSThomas Gleixner __clocksource_unstable(cs); 2437285dd7fSThomas Gleixner } 2447285dd7fSThomas Gleixner spin_unlock_irqrestore(&watchdog_lock, flags); 2455d8b34fdSThomas Gleixner } 2465d8b34fdSThomas Gleixner 2475d8b34fdSThomas Gleixner static void clocksource_watchdog(unsigned long data) 2485d8b34fdSThomas Gleixner { 249c55c87c8SMartin Schwidefsky struct clocksource *cs; 2505d8b34fdSThomas Gleixner cycle_t csnow, wdnow; 2515d8b34fdSThomas Gleixner int64_t wd_nsec, cs_nsec; 2529fb60336SThomas Gleixner int next_cpu, reset_pending; 2535d8b34fdSThomas Gleixner 2545d8b34fdSThomas Gleixner spin_lock(&watchdog_lock); 255fb63a0ebSMartin Schwidefsky if (!watchdog_running) 256fb63a0ebSMartin Schwidefsky goto out; 2575d8b34fdSThomas Gleixner 2589fb60336SThomas Gleixner reset_pending = atomic_read(&watchdog_reset_pending); 2599fb60336SThomas Gleixner 260c55c87c8SMartin Schwidefsky list_for_each_entry(cs, &watchdog_list, wd_list) { 261c55c87c8SMartin Schwidefsky 262c55c87c8SMartin Schwidefsky /* Clocksource already marked unstable? */ 26301548f4dSMartin Schwidefsky if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 26454a6bc0bSThomas Gleixner if (finished_booting) 26501548f4dSMartin Schwidefsky schedule_work(&watchdog_work); 266c55c87c8SMartin Schwidefsky continue; 26701548f4dSMartin Schwidefsky } 268c55c87c8SMartin Schwidefsky 269b5199515SThomas Gleixner local_irq_disable(); 2708e19608eSMagnus Damm csnow = cs->read(cs); 271b5199515SThomas Gleixner wdnow = watchdog->read(watchdog); 272b5199515SThomas Gleixner local_irq_enable(); 273b52f52a0SThomas Gleixner 2748cf4e750SMartin Schwidefsky /* Clocksource initialized ? */ 2759fb60336SThomas Gleixner if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) || 2769fb60336SThomas Gleixner atomic_read(&watchdog_reset_pending)) { 2778cf4e750SMartin Schwidefsky cs->flags |= CLOCK_SOURCE_WATCHDOG; 278b5199515SThomas Gleixner cs->wd_last = wdnow; 279b5199515SThomas Gleixner cs->cs_last = csnow; 280b52f52a0SThomas Gleixner continue; 281b52f52a0SThomas Gleixner } 282b52f52a0SThomas Gleixner 283b5199515SThomas Gleixner wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask, 284b5199515SThomas Gleixner watchdog->mult, watchdog->shift); 285b5199515SThomas Gleixner 286b5199515SThomas Gleixner cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) & 287155ec602SMartin Schwidefsky cs->mask, cs->mult, cs->shift); 288b5199515SThomas Gleixner cs->cs_last = csnow; 289b5199515SThomas Gleixner cs->wd_last = wdnow; 290b5199515SThomas Gleixner 2919fb60336SThomas Gleixner if (atomic_read(&watchdog_reset_pending)) 2929fb60336SThomas Gleixner continue; 2939fb60336SThomas Gleixner 294b5199515SThomas Gleixner /* Check the deviation from the watchdog clocksource. */ 2959fb60336SThomas Gleixner if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) { 2968cf4e750SMartin Schwidefsky clocksource_unstable(cs, cs_nsec - wd_nsec); 2978cf4e750SMartin Schwidefsky continue; 2988cf4e750SMartin Schwidefsky } 2998cf4e750SMartin Schwidefsky 3008cf4e750SMartin Schwidefsky if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && 3018cf4e750SMartin Schwidefsky (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && 3025d8b34fdSThomas Gleixner (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { 3035d8b34fdSThomas Gleixner cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 30479bf2bb3SThomas Gleixner /* 3058cf4e750SMartin Schwidefsky * We just marked the clocksource as highres-capable, 3068cf4e750SMartin Schwidefsky * notify the rest of the system as well so that we 3078cf4e750SMartin Schwidefsky * transition into high-res mode: 30879bf2bb3SThomas Gleixner */ 30979bf2bb3SThomas Gleixner tick_clock_notify(); 3105d8b34fdSThomas Gleixner } 3115d8b34fdSThomas Gleixner } 3125d8b34fdSThomas Gleixner 3136993fc5bSAndi Kleen /* 3149fb60336SThomas Gleixner * We only clear the watchdog_reset_pending, when we did a 3159fb60336SThomas Gleixner * full cycle through all clocksources. 3169fb60336SThomas Gleixner */ 3179fb60336SThomas Gleixner if (reset_pending) 3189fb60336SThomas Gleixner atomic_dec(&watchdog_reset_pending); 3199fb60336SThomas Gleixner 3209fb60336SThomas Gleixner /* 321c55c87c8SMartin Schwidefsky * Cycle through CPUs to check if the CPUs stay synchronized 322c55c87c8SMartin Schwidefsky * to each other. 3236993fc5bSAndi Kleen */ 324c55c87c8SMartin Schwidefsky next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask); 325cad0e458SMike Travis if (next_cpu >= nr_cpu_ids) 3266b954823SRusty Russell next_cpu = cpumask_first(cpu_online_mask); 3276993fc5bSAndi Kleen watchdog_timer.expires += WATCHDOG_INTERVAL; 3286993fc5bSAndi Kleen add_timer_on(&watchdog_timer, next_cpu); 329fb63a0ebSMartin Schwidefsky out: 3305d8b34fdSThomas Gleixner spin_unlock(&watchdog_lock); 3315d8b34fdSThomas Gleixner } 3320f8e8ef7SMartin Schwidefsky 333fb63a0ebSMartin Schwidefsky static inline void clocksource_start_watchdog(void) 334fb63a0ebSMartin Schwidefsky { 335fb63a0ebSMartin Schwidefsky if (watchdog_running || !watchdog || list_empty(&watchdog_list)) 336fb63a0ebSMartin Schwidefsky return; 337fb63a0ebSMartin Schwidefsky init_timer(&watchdog_timer); 338fb63a0ebSMartin Schwidefsky watchdog_timer.function = clocksource_watchdog; 339fb63a0ebSMartin Schwidefsky watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; 340fb63a0ebSMartin Schwidefsky add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); 341fb63a0ebSMartin Schwidefsky watchdog_running = 1; 342fb63a0ebSMartin Schwidefsky } 343fb63a0ebSMartin Schwidefsky 344fb63a0ebSMartin Schwidefsky static inline void clocksource_stop_watchdog(void) 345fb63a0ebSMartin Schwidefsky { 346fb63a0ebSMartin Schwidefsky if (!watchdog_running || (watchdog && !list_empty(&watchdog_list))) 347fb63a0ebSMartin Schwidefsky return; 348fb63a0ebSMartin Schwidefsky del_timer(&watchdog_timer); 349fb63a0ebSMartin Schwidefsky watchdog_running = 0; 350fb63a0ebSMartin Schwidefsky } 351fb63a0ebSMartin Schwidefsky 3520f8e8ef7SMartin Schwidefsky static inline void clocksource_reset_watchdog(void) 3530f8e8ef7SMartin Schwidefsky { 3540f8e8ef7SMartin Schwidefsky struct clocksource *cs; 3550f8e8ef7SMartin Schwidefsky 3560f8e8ef7SMartin Schwidefsky list_for_each_entry(cs, &watchdog_list, wd_list) 3570f8e8ef7SMartin Schwidefsky cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 3580f8e8ef7SMartin Schwidefsky } 3590f8e8ef7SMartin Schwidefsky 360b52f52a0SThomas Gleixner static void clocksource_resume_watchdog(void) 361b52f52a0SThomas Gleixner { 3629fb60336SThomas Gleixner atomic_inc(&watchdog_reset_pending); 363b52f52a0SThomas Gleixner } 364b52f52a0SThomas Gleixner 365fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs) 3665d8b34fdSThomas Gleixner { 3675d8b34fdSThomas Gleixner unsigned long flags; 3685d8b34fdSThomas Gleixner 3695d8b34fdSThomas Gleixner spin_lock_irqsave(&watchdog_lock, flags); 3705d8b34fdSThomas Gleixner if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 371fb63a0ebSMartin Schwidefsky /* cs is a clocksource to be watched. */ 3725d8b34fdSThomas Gleixner list_add(&cs->wd_list, &watchdog_list); 373fb63a0ebSMartin Schwidefsky cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 374948ac6d7SThomas Gleixner } else { 375fb63a0ebSMartin Schwidefsky /* cs is a watchdog. */ 376948ac6d7SThomas Gleixner if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 3775d8b34fdSThomas Gleixner cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 378fb63a0ebSMartin Schwidefsky /* Pick the best watchdog. */ 3795d8b34fdSThomas Gleixner if (!watchdog || cs->rating > watchdog->rating) { 3805d8b34fdSThomas Gleixner watchdog = cs; 3815d8b34fdSThomas Gleixner /* Reset watchdog cycles */ 3820f8e8ef7SMartin Schwidefsky clocksource_reset_watchdog(); 3835d8b34fdSThomas Gleixner } 3845d8b34fdSThomas Gleixner } 385fb63a0ebSMartin Schwidefsky /* Check if the watchdog timer needs to be started. */ 386fb63a0ebSMartin Schwidefsky clocksource_start_watchdog(); 3875d8b34fdSThomas Gleixner spin_unlock_irqrestore(&watchdog_lock, flags); 3885d8b34fdSThomas Gleixner } 389fb63a0ebSMartin Schwidefsky 390fb63a0ebSMartin Schwidefsky static void clocksource_dequeue_watchdog(struct clocksource *cs) 391fb63a0ebSMartin Schwidefsky { 392fb63a0ebSMartin Schwidefsky struct clocksource *tmp; 393fb63a0ebSMartin Schwidefsky unsigned long flags; 394fb63a0ebSMartin Schwidefsky 395fb63a0ebSMartin Schwidefsky spin_lock_irqsave(&watchdog_lock, flags); 396fb63a0ebSMartin Schwidefsky if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 397fb63a0ebSMartin Schwidefsky /* cs is a watched clocksource. */ 398fb63a0ebSMartin Schwidefsky list_del_init(&cs->wd_list); 399fb63a0ebSMartin Schwidefsky } else if (cs == watchdog) { 400fb63a0ebSMartin Schwidefsky /* Reset watchdog cycles */ 401fb63a0ebSMartin Schwidefsky clocksource_reset_watchdog(); 402fb63a0ebSMartin Schwidefsky /* Current watchdog is removed. Find an alternative. */ 403fb63a0ebSMartin Schwidefsky watchdog = NULL; 404fb63a0ebSMartin Schwidefsky list_for_each_entry(tmp, &clocksource_list, list) { 405fb63a0ebSMartin Schwidefsky if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY) 406fb63a0ebSMartin Schwidefsky continue; 407fb63a0ebSMartin Schwidefsky if (!watchdog || tmp->rating > watchdog->rating) 408fb63a0ebSMartin Schwidefsky watchdog = tmp; 409fb63a0ebSMartin Schwidefsky } 410fb63a0ebSMartin Schwidefsky } 411fb63a0ebSMartin Schwidefsky cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 412fb63a0ebSMartin Schwidefsky /* Check if the watchdog timer needs to be stopped. */ 413fb63a0ebSMartin Schwidefsky clocksource_stop_watchdog(); 414fb63a0ebSMartin Schwidefsky spin_unlock_irqrestore(&watchdog_lock, flags); 415fb63a0ebSMartin Schwidefsky } 416fb63a0ebSMartin Schwidefsky 41701548f4dSMartin Schwidefsky static int clocksource_watchdog_kthread(void *data) 418c55c87c8SMartin Schwidefsky { 419c55c87c8SMartin Schwidefsky struct clocksource *cs, *tmp; 420c55c87c8SMartin Schwidefsky unsigned long flags; 4216ea41d25SThomas Gleixner LIST_HEAD(unstable); 422c55c87c8SMartin Schwidefsky 423d0981a1bSThomas Gleixner mutex_lock(&clocksource_mutex); 424c55c87c8SMartin Schwidefsky spin_lock_irqsave(&watchdog_lock, flags); 425c55c87c8SMartin Schwidefsky list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) 426c55c87c8SMartin Schwidefsky if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 427c55c87c8SMartin Schwidefsky list_del_init(&cs->wd_list); 4286ea41d25SThomas Gleixner list_add(&cs->wd_list, &unstable); 429c55c87c8SMartin Schwidefsky } 430c55c87c8SMartin Schwidefsky /* Check if the watchdog timer needs to be stopped. */ 431c55c87c8SMartin Schwidefsky clocksource_stop_watchdog(); 4326ea41d25SThomas Gleixner spin_unlock_irqrestore(&watchdog_lock, flags); 4336ea41d25SThomas Gleixner 4346ea41d25SThomas Gleixner /* Needs to be done outside of watchdog lock */ 4356ea41d25SThomas Gleixner list_for_each_entry_safe(cs, tmp, &unstable, wd_list) { 4366ea41d25SThomas Gleixner list_del_init(&cs->wd_list); 437d0981a1bSThomas Gleixner __clocksource_change_rating(cs, 0); 4386ea41d25SThomas Gleixner } 439d0981a1bSThomas Gleixner mutex_unlock(&clocksource_mutex); 44001548f4dSMartin Schwidefsky return 0; 441c55c87c8SMartin Schwidefsky } 442c55c87c8SMartin Schwidefsky 443fb63a0ebSMartin Schwidefsky #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ 444fb63a0ebSMartin Schwidefsky 445fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs) 4465d8b34fdSThomas Gleixner { 4475d8b34fdSThomas Gleixner if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 4485d8b34fdSThomas Gleixner cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 4495d8b34fdSThomas Gleixner } 450b52f52a0SThomas Gleixner 451fb63a0ebSMartin Schwidefsky static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } 452b52f52a0SThomas Gleixner static inline void clocksource_resume_watchdog(void) { } 45354a6bc0bSThomas Gleixner static inline int clocksource_watchdog_kthread(void *data) { return 0; } 454fb63a0ebSMartin Schwidefsky 455fb63a0ebSMartin Schwidefsky #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ 4565d8b34fdSThomas Gleixner 457734efb46Sjohn stultz /** 458c54a42b1SMagnus Damm * clocksource_suspend - suspend the clocksource(s) 459c54a42b1SMagnus Damm */ 460c54a42b1SMagnus Damm void clocksource_suspend(void) 461c54a42b1SMagnus Damm { 462c54a42b1SMagnus Damm struct clocksource *cs; 463c54a42b1SMagnus Damm 464c54a42b1SMagnus Damm list_for_each_entry_reverse(cs, &clocksource_list, list) 465c54a42b1SMagnus Damm if (cs->suspend) 466c54a42b1SMagnus Damm cs->suspend(cs); 467c54a42b1SMagnus Damm } 468c54a42b1SMagnus Damm 469c54a42b1SMagnus Damm /** 470b52f52a0SThomas Gleixner * clocksource_resume - resume the clocksource(s) 471b52f52a0SThomas Gleixner */ 472b52f52a0SThomas Gleixner void clocksource_resume(void) 473b52f52a0SThomas Gleixner { 4742e197586SMatthias Kaehlcke struct clocksource *cs; 475b52f52a0SThomas Gleixner 47675c5158fSMartin Schwidefsky list_for_each_entry(cs, &clocksource_list, list) 477b52f52a0SThomas Gleixner if (cs->resume) 47817622339SMagnus Damm cs->resume(cs); 479b52f52a0SThomas Gleixner 480b52f52a0SThomas Gleixner clocksource_resume_watchdog(); 481b52f52a0SThomas Gleixner } 482b52f52a0SThomas Gleixner 483b52f52a0SThomas Gleixner /** 4847c3078b6SJason Wessel * clocksource_touch_watchdog - Update watchdog 4857c3078b6SJason Wessel * 4867c3078b6SJason Wessel * Update the watchdog after exception contexts such as kgdb so as not 4877b7422a5SThomas Gleixner * to incorrectly trip the watchdog. This might fail when the kernel 4887b7422a5SThomas Gleixner * was stopped in code which holds watchdog_lock. 4897c3078b6SJason Wessel */ 4907c3078b6SJason Wessel void clocksource_touch_watchdog(void) 4917c3078b6SJason Wessel { 4927c3078b6SJason Wessel clocksource_resume_watchdog(); 4937c3078b6SJason Wessel } 4947c3078b6SJason Wessel 495734efb46Sjohn stultz /** 496d65670a7SJohn Stultz * clocksource_max_adjustment- Returns max adjustment amount 497d65670a7SJohn Stultz * @cs: Pointer to clocksource 498d65670a7SJohn Stultz * 499d65670a7SJohn Stultz */ 500d65670a7SJohn Stultz static u32 clocksource_max_adjustment(struct clocksource *cs) 501d65670a7SJohn Stultz { 502d65670a7SJohn Stultz u64 ret; 503d65670a7SJohn Stultz /* 50488b28adfSJim Cromie * We won't try to correct for more than 11% adjustments (110,000 ppm), 505d65670a7SJohn Stultz */ 506d65670a7SJohn Stultz ret = (u64)cs->mult * 11; 507d65670a7SJohn Stultz do_div(ret,100); 508d65670a7SJohn Stultz return (u32)ret; 509d65670a7SJohn Stultz } 510d65670a7SJohn Stultz 511d65670a7SJohn Stultz /** 51298962465SJon Hunter * clocksource_max_deferment - Returns max time the clocksource can be deferred 51398962465SJon Hunter * @cs: Pointer to clocksource 51498962465SJon Hunter * 51598962465SJon Hunter */ 51698962465SJon Hunter static u64 clocksource_max_deferment(struct clocksource *cs) 51798962465SJon Hunter { 51898962465SJon Hunter u64 max_nsecs, max_cycles; 51998962465SJon Hunter 52098962465SJon Hunter /* 52198962465SJon Hunter * Calculate the maximum number of cycles that we can pass to the 52298962465SJon Hunter * cyc2ns function without overflowing a 64-bit signed result. The 523d65670a7SJohn Stultz * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj) 524d65670a7SJohn Stultz * which is equivalent to the below. 525d65670a7SJohn Stultz * max_cycles < (2^63)/(cs->mult + cs->maxadj) 526d65670a7SJohn Stultz * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj))) 527d65670a7SJohn Stultz * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj)) 528d65670a7SJohn Stultz * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj)) 529d65670a7SJohn Stultz * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj)) 53098962465SJon Hunter * Please note that we add 1 to the result of the log2 to account for 53198962465SJon Hunter * any rounding errors, ensure the above inequality is satisfied and 53298962465SJon Hunter * no overflow will occur. 53398962465SJon Hunter */ 534d65670a7SJohn Stultz max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1)); 53598962465SJon Hunter 53698962465SJon Hunter /* 53798962465SJon Hunter * The actual maximum number of cycles we can defer the clocksource is 53898962465SJon Hunter * determined by the minimum of max_cycles and cs->mask. 539d65670a7SJohn Stultz * Note: Here we subtract the maxadj to make sure we don't sleep for 540d65670a7SJohn Stultz * too long if there's a large negative adjustment. 54198962465SJon Hunter */ 54298962465SJon Hunter max_cycles = min_t(u64, max_cycles, (u64) cs->mask); 543d65670a7SJohn Stultz max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj, 544d65670a7SJohn Stultz cs->shift); 54598962465SJon Hunter 54698962465SJon Hunter /* 54798962465SJon Hunter * To ensure that the clocksource does not wrap whilst we are idle, 54898962465SJon Hunter * limit the time the clocksource can be deferred by 12.5%. Please 54998962465SJon Hunter * note a margin of 12.5% is used because this can be computed with 55098962465SJon Hunter * a shift, versus say 10% which would require division. 55198962465SJon Hunter */ 552b1f91966SYang Honggang (Joseph) return max_nsecs - (max_nsecs >> 3); 55398962465SJon Hunter } 55498962465SJon Hunter 555592913ecSJohn Stultz #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET 556734efb46Sjohn stultz 557f5a2e343SThomas Gleixner static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) 5585d33b883SThomas Gleixner { 5595d33b883SThomas Gleixner struct clocksource *cs; 5605d33b883SThomas Gleixner 5615d33b883SThomas Gleixner if (!finished_booting || list_empty(&clocksource_list)) 5625d33b883SThomas Gleixner return NULL; 5635d33b883SThomas Gleixner 5645d33b883SThomas Gleixner /* 5655d33b883SThomas Gleixner * We pick the clocksource with the highest rating. If oneshot 5665d33b883SThomas Gleixner * mode is active, we pick the highres valid clocksource with 5675d33b883SThomas Gleixner * the best rating. 5685d33b883SThomas Gleixner */ 5695d33b883SThomas Gleixner list_for_each_entry(cs, &clocksource_list, list) { 570f5a2e343SThomas Gleixner if (skipcur && cs == curr_clocksource) 571f5a2e343SThomas Gleixner continue; 5725d33b883SThomas Gleixner if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 5735d33b883SThomas Gleixner continue; 5745d33b883SThomas Gleixner return cs; 5755d33b883SThomas Gleixner } 5765d33b883SThomas Gleixner return NULL; 5775d33b883SThomas Gleixner } 5785d33b883SThomas Gleixner 579f5a2e343SThomas Gleixner static void __clocksource_select(bool skipcur) 580734efb46Sjohn stultz { 5815d33b883SThomas Gleixner bool oneshot = tick_oneshot_mode_active(); 582f1b82746SMartin Schwidefsky struct clocksource *best, *cs; 5835d8b34fdSThomas Gleixner 5845d33b883SThomas Gleixner /* Find the best suitable clocksource */ 585f5a2e343SThomas Gleixner best = clocksource_find_best(oneshot, skipcur); 5865d33b883SThomas Gleixner if (!best) 587f1b82746SMartin Schwidefsky return; 5885d33b883SThomas Gleixner 589f1b82746SMartin Schwidefsky /* Check for the override clocksource. */ 590f1b82746SMartin Schwidefsky list_for_each_entry(cs, &clocksource_list, list) { 591f5a2e343SThomas Gleixner if (skipcur && cs == curr_clocksource) 592f5a2e343SThomas Gleixner continue; 593f1b82746SMartin Schwidefsky if (strcmp(cs->name, override_name) != 0) 594f1b82746SMartin Schwidefsky continue; 595f1b82746SMartin Schwidefsky /* 596f1b82746SMartin Schwidefsky * Check to make sure we don't switch to a non-highres 597f1b82746SMartin Schwidefsky * capable clocksource if the tick code is in oneshot 598f1b82746SMartin Schwidefsky * mode (highres or nohz) 599f1b82746SMartin Schwidefsky */ 6005d33b883SThomas Gleixner if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { 601f1b82746SMartin Schwidefsky /* Override clocksource cannot be used. */ 602f1b82746SMartin Schwidefsky printk(KERN_WARNING "Override clocksource %s is not " 603f1b82746SMartin Schwidefsky "HRT compatible. Cannot switch while in " 604f1b82746SMartin Schwidefsky "HRT/NOHZ mode\n", cs->name); 605f1b82746SMartin Schwidefsky override_name[0] = 0; 606f1b82746SMartin Schwidefsky } else 607f1b82746SMartin Schwidefsky /* Override clocksource can be used. */ 608f1b82746SMartin Schwidefsky best = cs; 609f1b82746SMartin Schwidefsky break; 610734efb46Sjohn stultz } 611ba919d1cSThomas Gleixner 612ba919d1cSThomas Gleixner if (curr_clocksource != best && !timekeeping_notify(best)) { 613ba919d1cSThomas Gleixner pr_info("Switched to clocksource %s\n", best->name); 61475c5158fSMartin Schwidefsky curr_clocksource = best; 615f1b82746SMartin Schwidefsky } 61675c5158fSMartin Schwidefsky } 61775c5158fSMartin Schwidefsky 618f5a2e343SThomas Gleixner /** 619f5a2e343SThomas Gleixner * clocksource_select - Select the best clocksource available 620f5a2e343SThomas Gleixner * 621f5a2e343SThomas Gleixner * Private function. Must hold clocksource_mutex when called. 622f5a2e343SThomas Gleixner * 623f5a2e343SThomas Gleixner * Select the clocksource with the best rating, or the clocksource, 624f5a2e343SThomas Gleixner * which is selected by userspace override. 625f5a2e343SThomas Gleixner */ 626f5a2e343SThomas Gleixner static void clocksource_select(void) 627f5a2e343SThomas Gleixner { 628f5a2e343SThomas Gleixner return __clocksource_select(false); 629f5a2e343SThomas Gleixner } 630f5a2e343SThomas Gleixner 631592913ecSJohn Stultz #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */ 63254a6bc0bSThomas Gleixner 63354a6bc0bSThomas Gleixner static inline void clocksource_select(void) { } 63454a6bc0bSThomas Gleixner 63554a6bc0bSThomas Gleixner #endif 63654a6bc0bSThomas Gleixner 63775c5158fSMartin Schwidefsky /* 63875c5158fSMartin Schwidefsky * clocksource_done_booting - Called near the end of core bootup 63975c5158fSMartin Schwidefsky * 64075c5158fSMartin Schwidefsky * Hack to avoid lots of clocksource churn at boot time. 64175c5158fSMartin Schwidefsky * We use fs_initcall because we want this to start before 64275c5158fSMartin Schwidefsky * device_initcall but after subsys_initcall. 64375c5158fSMartin Schwidefsky */ 64475c5158fSMartin Schwidefsky static int __init clocksource_done_booting(void) 64575c5158fSMartin Schwidefsky { 646ad6759fbSjohn stultz mutex_lock(&clocksource_mutex); 647ad6759fbSjohn stultz curr_clocksource = clocksource_default_clock(); 648ad6759fbSjohn stultz mutex_unlock(&clocksource_mutex); 649ad6759fbSjohn stultz 65075c5158fSMartin Schwidefsky finished_booting = 1; 65154a6bc0bSThomas Gleixner 65254a6bc0bSThomas Gleixner /* 65354a6bc0bSThomas Gleixner * Run the watchdog first to eliminate unstable clock sources 65454a6bc0bSThomas Gleixner */ 65554a6bc0bSThomas Gleixner clocksource_watchdog_kthread(NULL); 65654a6bc0bSThomas Gleixner 657e6c73305SThomas Gleixner mutex_lock(&clocksource_mutex); 65875c5158fSMartin Schwidefsky clocksource_select(); 659e6c73305SThomas Gleixner mutex_unlock(&clocksource_mutex); 66075c5158fSMartin Schwidefsky return 0; 66175c5158fSMartin Schwidefsky } 66275c5158fSMartin Schwidefsky fs_initcall(clocksource_done_booting); 663f1b82746SMartin Schwidefsky 66492c7e002SThomas Gleixner /* 66592c7e002SThomas Gleixner * Enqueue the clocksource sorted by rating 666734efb46Sjohn stultz */ 667f1b82746SMartin Schwidefsky static void clocksource_enqueue(struct clocksource *cs) 668734efb46Sjohn stultz { 669f1b82746SMartin Schwidefsky struct list_head *entry = &clocksource_list; 670f1b82746SMartin Schwidefsky struct clocksource *tmp; 671734efb46Sjohn stultz 672f1b82746SMartin Schwidefsky list_for_each_entry(tmp, &clocksource_list, list) 67392c7e002SThomas Gleixner /* Keep track of the place, where to insert */ 674f1b82746SMartin Schwidefsky if (tmp->rating >= cs->rating) 675f1b82746SMartin Schwidefsky entry = &tmp->list; 676f1b82746SMartin Schwidefsky list_add(&cs->list, entry); 677734efb46Sjohn stultz } 678734efb46Sjohn stultz 679d7e81c26SJohn Stultz /** 680852db46dSJohn Stultz * __clocksource_updatefreq_scale - Used update clocksource with new freq 681b1b73d09SKusanagi Kouichi * @cs: clocksource to be registered 682852db46dSJohn Stultz * @scale: Scale factor multiplied against freq to get clocksource hz 683852db46dSJohn Stultz * @freq: clocksource frequency (cycles per second) divided by scale 684852db46dSJohn Stultz * 685852db46dSJohn Stultz * This should only be called from the clocksource->enable() method. 686852db46dSJohn Stultz * 687852db46dSJohn Stultz * This *SHOULD NOT* be called directly! Please use the 688852db46dSJohn Stultz * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions. 689852db46dSJohn Stultz */ 690852db46dSJohn Stultz void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) 691852db46dSJohn Stultz { 692c0e299b1SThomas Gleixner u64 sec; 693852db46dSJohn Stultz /* 694724ed53eSThomas Gleixner * Calc the maximum number of seconds which we can run before 695724ed53eSThomas Gleixner * wrapping around. For clocksources which have a mask > 32bit 696724ed53eSThomas Gleixner * we need to limit the max sleep time to have a good 697724ed53eSThomas Gleixner * conversion precision. 10 minutes is still a reasonable 698724ed53eSThomas Gleixner * amount. That results in a shift value of 24 for a 699724ed53eSThomas Gleixner * clocksource with mask >= 40bit and f >= 4GHz. That maps to 700724ed53eSThomas Gleixner * ~ 0.06ppm granularity for NTP. We apply the same 12.5% 701724ed53eSThomas Gleixner * margin as we do in clocksource_max_deferment() 702852db46dSJohn Stultz */ 703b1f91966SYang Honggang (Joseph) sec = (cs->mask - (cs->mask >> 3)); 704724ed53eSThomas Gleixner do_div(sec, freq); 705724ed53eSThomas Gleixner do_div(sec, scale); 706724ed53eSThomas Gleixner if (!sec) 707724ed53eSThomas Gleixner sec = 1; 708724ed53eSThomas Gleixner else if (sec > 600 && cs->mask > UINT_MAX) 709724ed53eSThomas Gleixner sec = 600; 710724ed53eSThomas Gleixner 711852db46dSJohn Stultz clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 712724ed53eSThomas Gleixner NSEC_PER_SEC / scale, sec * scale); 713d65670a7SJohn Stultz 714d65670a7SJohn Stultz /* 715d65670a7SJohn Stultz * for clocksources that have large mults, to avoid overflow. 716d65670a7SJohn Stultz * Since mult may be adjusted by ntp, add an safety extra margin 717d65670a7SJohn Stultz * 718d65670a7SJohn Stultz */ 719d65670a7SJohn Stultz cs->maxadj = clocksource_max_adjustment(cs); 720d65670a7SJohn Stultz while ((cs->mult + cs->maxadj < cs->mult) 721d65670a7SJohn Stultz || (cs->mult - cs->maxadj > cs->mult)) { 722d65670a7SJohn Stultz cs->mult >>= 1; 723d65670a7SJohn Stultz cs->shift--; 724d65670a7SJohn Stultz cs->maxadj = clocksource_max_adjustment(cs); 725d65670a7SJohn Stultz } 726d65670a7SJohn Stultz 727852db46dSJohn Stultz cs->max_idle_ns = clocksource_max_deferment(cs); 728852db46dSJohn Stultz } 729852db46dSJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); 730852db46dSJohn Stultz 731852db46dSJohn Stultz /** 732d7e81c26SJohn Stultz * __clocksource_register_scale - Used to install new clocksources 733b1b73d09SKusanagi Kouichi * @cs: clocksource to be registered 734d7e81c26SJohn Stultz * @scale: Scale factor multiplied against freq to get clocksource hz 735d7e81c26SJohn Stultz * @freq: clocksource frequency (cycles per second) divided by scale 736d7e81c26SJohn Stultz * 737d7e81c26SJohn Stultz * Returns -EBUSY if registration fails, zero otherwise. 738d7e81c26SJohn Stultz * 739d7e81c26SJohn Stultz * This *SHOULD NOT* be called directly! Please use the 740d7e81c26SJohn Stultz * clocksource_register_hz() or clocksource_register_khz helper functions. 741d7e81c26SJohn Stultz */ 742d7e81c26SJohn Stultz int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) 743d7e81c26SJohn Stultz { 744d7e81c26SJohn Stultz 745b595076aSUwe Kleine-König /* Initialize mult/shift and max_idle_ns */ 746852db46dSJohn Stultz __clocksource_updatefreq_scale(cs, scale, freq); 747d7e81c26SJohn Stultz 748852db46dSJohn Stultz /* Add clocksource to the clcoksource list */ 749d7e81c26SJohn Stultz mutex_lock(&clocksource_mutex); 750d7e81c26SJohn Stultz clocksource_enqueue(cs); 751d7e81c26SJohn Stultz clocksource_enqueue_watchdog(cs); 752e05b2efbSjohn stultz clocksource_select(); 753d7e81c26SJohn Stultz mutex_unlock(&clocksource_mutex); 754d7e81c26SJohn Stultz return 0; 755d7e81c26SJohn Stultz } 756d7e81c26SJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_register_scale); 757d7e81c26SJohn Stultz 758d7e81c26SJohn Stultz 759734efb46Sjohn stultz /** 760a2752549Sjohn stultz * clocksource_register - Used to install new clocksources 761b1b73d09SKusanagi Kouichi * @cs: clocksource to be registered 762734efb46Sjohn stultz * 763734efb46Sjohn stultz * Returns -EBUSY if registration fails, zero otherwise. 764734efb46Sjohn stultz */ 765f1b82746SMartin Schwidefsky int clocksource_register(struct clocksource *cs) 766734efb46Sjohn stultz { 767d65670a7SJohn Stultz /* calculate max adjustment for given mult/shift */ 768d65670a7SJohn Stultz cs->maxadj = clocksource_max_adjustment(cs); 769d65670a7SJohn Stultz WARN_ONCE(cs->mult + cs->maxadj < cs->mult, 770d65670a7SJohn Stultz "Clocksource %s might overflow on 11%% adjustment\n", 771d65670a7SJohn Stultz cs->name); 772d65670a7SJohn Stultz 77398962465SJon Hunter /* calculate max idle time permitted for this clocksource */ 77498962465SJon Hunter cs->max_idle_ns = clocksource_max_deferment(cs); 77598962465SJon Hunter 77675c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 777f1b82746SMartin Schwidefsky clocksource_enqueue(cs); 778fb63a0ebSMartin Schwidefsky clocksource_enqueue_watchdog(cs); 779e05b2efbSjohn stultz clocksource_select(); 78075c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 781f1b82746SMartin Schwidefsky return 0; 782734efb46Sjohn stultz } 783a2752549Sjohn stultz EXPORT_SYMBOL(clocksource_register); 784734efb46Sjohn stultz 785d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating) 786d0981a1bSThomas Gleixner { 787d0981a1bSThomas Gleixner list_del(&cs->list); 788d0981a1bSThomas Gleixner cs->rating = rating; 789d0981a1bSThomas Gleixner clocksource_enqueue(cs); 790d0981a1bSThomas Gleixner clocksource_select(); 791d0981a1bSThomas Gleixner } 792d0981a1bSThomas Gleixner 793734efb46Sjohn stultz /** 79492c7e002SThomas Gleixner * clocksource_change_rating - Change the rating of a registered clocksource 795b1b73d09SKusanagi Kouichi * @cs: clocksource to be changed 796b1b73d09SKusanagi Kouichi * @rating: new rating 797734efb46Sjohn stultz */ 79892c7e002SThomas Gleixner void clocksource_change_rating(struct clocksource *cs, int rating) 799734efb46Sjohn stultz { 80075c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 801d0981a1bSThomas Gleixner __clocksource_change_rating(cs, rating); 80275c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 803734efb46Sjohn stultz } 804fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_change_rating); 805734efb46Sjohn stultz 8064713e22cSThomas Gleixner /** 8074713e22cSThomas Gleixner * clocksource_unregister - remove a registered clocksource 808b1b73d09SKusanagi Kouichi * @cs: clocksource to be unregistered 8094713e22cSThomas Gleixner */ 8104713e22cSThomas Gleixner void clocksource_unregister(struct clocksource *cs) 8114713e22cSThomas Gleixner { 81275c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 813fb63a0ebSMartin Schwidefsky clocksource_dequeue_watchdog(cs); 8144713e22cSThomas Gleixner list_del(&cs->list); 815f1b82746SMartin Schwidefsky clocksource_select(); 81675c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 8174713e22cSThomas Gleixner } 818fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_unregister); 8194713e22cSThomas Gleixner 8202b013700SDaniel Walker #ifdef CONFIG_SYSFS 821734efb46Sjohn stultz /** 822734efb46Sjohn stultz * sysfs_show_current_clocksources - sysfs interface for current clocksource 823734efb46Sjohn stultz * @dev: unused 824b1b73d09SKusanagi Kouichi * @attr: unused 825734efb46Sjohn stultz * @buf: char buffer to be filled with clocksource list 826734efb46Sjohn stultz * 827734efb46Sjohn stultz * Provides sysfs interface for listing current clocksource. 828734efb46Sjohn stultz */ 829734efb46Sjohn stultz static ssize_t 830d369a5d8SKay Sievers sysfs_show_current_clocksources(struct device *dev, 831d369a5d8SKay Sievers struct device_attribute *attr, char *buf) 832734efb46Sjohn stultz { 8335e2cb101SMiao Xie ssize_t count = 0; 834734efb46Sjohn stultz 83575c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 8365e2cb101SMiao Xie count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name); 83775c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 838734efb46Sjohn stultz 8395e2cb101SMiao Xie return count; 840734efb46Sjohn stultz } 841734efb46Sjohn stultz 84229b54078SThomas Gleixner static size_t clocksource_get_uname(const char *buf, char *dst, size_t cnt) 84329b54078SThomas Gleixner { 84429b54078SThomas Gleixner size_t ret = cnt; 84529b54078SThomas Gleixner 84629b54078SThomas Gleixner /* strings from sysfs write are not 0 terminated! */ 84729b54078SThomas Gleixner if (!cnt || cnt >= CS_NAME_LEN) 84829b54078SThomas Gleixner return -EINVAL; 84929b54078SThomas Gleixner 85029b54078SThomas Gleixner /* strip of \n: */ 85129b54078SThomas Gleixner if (buf[cnt-1] == '\n') 85229b54078SThomas Gleixner cnt--; 85329b54078SThomas Gleixner if (cnt > 0) 85429b54078SThomas Gleixner memcpy(dst, buf, cnt); 85529b54078SThomas Gleixner dst[cnt] = 0; 85629b54078SThomas Gleixner return ret; 85729b54078SThomas Gleixner } 85829b54078SThomas Gleixner 859734efb46Sjohn stultz /** 860734efb46Sjohn stultz * sysfs_override_clocksource - interface for manually overriding clocksource 861734efb46Sjohn stultz * @dev: unused 862b1b73d09SKusanagi Kouichi * @attr: unused 863734efb46Sjohn stultz * @buf: name of override clocksource 864734efb46Sjohn stultz * @count: length of buffer 865734efb46Sjohn stultz * 866734efb46Sjohn stultz * Takes input from sysfs interface for manually overriding the default 867b71a8eb0SUwe Kleine-König * clocksource selection. 868734efb46Sjohn stultz */ 869d369a5d8SKay Sievers static ssize_t sysfs_override_clocksource(struct device *dev, 870d369a5d8SKay Sievers struct device_attribute *attr, 871734efb46Sjohn stultz const char *buf, size_t count) 872734efb46Sjohn stultz { 87329b54078SThomas Gleixner size_t ret; 874734efb46Sjohn stultz 87575c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 876734efb46Sjohn stultz 87729b54078SThomas Gleixner ret = clocksource_get_uname(buf, override_name, count); 87829b54078SThomas Gleixner if (ret >= 0) 879f1b82746SMartin Schwidefsky clocksource_select(); 880734efb46Sjohn stultz 88175c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 882734efb46Sjohn stultz 883734efb46Sjohn stultz return ret; 884734efb46Sjohn stultz } 885734efb46Sjohn stultz 886734efb46Sjohn stultz /** 887734efb46Sjohn stultz * sysfs_show_available_clocksources - sysfs interface for listing clocksource 888734efb46Sjohn stultz * @dev: unused 889b1b73d09SKusanagi Kouichi * @attr: unused 890734efb46Sjohn stultz * @buf: char buffer to be filled with clocksource list 891734efb46Sjohn stultz * 892734efb46Sjohn stultz * Provides sysfs interface for listing registered clocksources 893734efb46Sjohn stultz */ 894734efb46Sjohn stultz static ssize_t 895d369a5d8SKay Sievers sysfs_show_available_clocksources(struct device *dev, 896d369a5d8SKay Sievers struct device_attribute *attr, 8974a0b2b4dSAndi Kleen char *buf) 898734efb46Sjohn stultz { 8992e197586SMatthias Kaehlcke struct clocksource *src; 9005e2cb101SMiao Xie ssize_t count = 0; 901734efb46Sjohn stultz 90275c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 9032e197586SMatthias Kaehlcke list_for_each_entry(src, &clocksource_list, list) { 904cd6d95d8SThomas Gleixner /* 905cd6d95d8SThomas Gleixner * Don't show non-HRES clocksource if the tick code is 906cd6d95d8SThomas Gleixner * in one shot mode (highres=on or nohz=on) 907cd6d95d8SThomas Gleixner */ 908cd6d95d8SThomas Gleixner if (!tick_oneshot_mode_active() || 9093f68535aSjohn stultz (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 9105e2cb101SMiao Xie count += snprintf(buf + count, 9115e2cb101SMiao Xie max((ssize_t)PAGE_SIZE - count, (ssize_t)0), 9125e2cb101SMiao Xie "%s ", src->name); 913734efb46Sjohn stultz } 91475c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 915734efb46Sjohn stultz 9165e2cb101SMiao Xie count += snprintf(buf + count, 9175e2cb101SMiao Xie max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); 918734efb46Sjohn stultz 9195e2cb101SMiao Xie return count; 920734efb46Sjohn stultz } 921734efb46Sjohn stultz 922734efb46Sjohn stultz /* 923734efb46Sjohn stultz * Sysfs setup bits: 924734efb46Sjohn stultz */ 925d369a5d8SKay Sievers static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, 926734efb46Sjohn stultz sysfs_override_clocksource); 927734efb46Sjohn stultz 928d369a5d8SKay Sievers static DEVICE_ATTR(available_clocksource, 0444, 929734efb46Sjohn stultz sysfs_show_available_clocksources, NULL); 930734efb46Sjohn stultz 931d369a5d8SKay Sievers static struct bus_type clocksource_subsys = { 932af5ca3f4SKay Sievers .name = "clocksource", 933d369a5d8SKay Sievers .dev_name = "clocksource", 934734efb46Sjohn stultz }; 935734efb46Sjohn stultz 936d369a5d8SKay Sievers static struct device device_clocksource = { 937734efb46Sjohn stultz .id = 0, 938d369a5d8SKay Sievers .bus = &clocksource_subsys, 939734efb46Sjohn stultz }; 940734efb46Sjohn stultz 941ad596171Sjohn stultz static int __init init_clocksource_sysfs(void) 942734efb46Sjohn stultz { 943d369a5d8SKay Sievers int error = subsys_system_register(&clocksource_subsys, NULL); 944734efb46Sjohn stultz 945734efb46Sjohn stultz if (!error) 946d369a5d8SKay Sievers error = device_register(&device_clocksource); 947734efb46Sjohn stultz if (!error) 948d369a5d8SKay Sievers error = device_create_file( 949734efb46Sjohn stultz &device_clocksource, 950d369a5d8SKay Sievers &dev_attr_current_clocksource); 951734efb46Sjohn stultz if (!error) 952d369a5d8SKay Sievers error = device_create_file( 953734efb46Sjohn stultz &device_clocksource, 954d369a5d8SKay Sievers &dev_attr_available_clocksource); 955734efb46Sjohn stultz return error; 956734efb46Sjohn stultz } 957734efb46Sjohn stultz 958734efb46Sjohn stultz device_initcall(init_clocksource_sysfs); 9592b013700SDaniel Walker #endif /* CONFIG_SYSFS */ 960734efb46Sjohn stultz 961734efb46Sjohn stultz /** 962734efb46Sjohn stultz * boot_override_clocksource - boot clock override 963734efb46Sjohn stultz * @str: override name 964734efb46Sjohn stultz * 965734efb46Sjohn stultz * Takes a clocksource= boot argument and uses it 966734efb46Sjohn stultz * as the clocksource override name. 967734efb46Sjohn stultz */ 968734efb46Sjohn stultz static int __init boot_override_clocksource(char* str) 969734efb46Sjohn stultz { 97075c5158fSMartin Schwidefsky mutex_lock(&clocksource_mutex); 971734efb46Sjohn stultz if (str) 972734efb46Sjohn stultz strlcpy(override_name, str, sizeof(override_name)); 97375c5158fSMartin Schwidefsky mutex_unlock(&clocksource_mutex); 974734efb46Sjohn stultz return 1; 975734efb46Sjohn stultz } 976734efb46Sjohn stultz 977734efb46Sjohn stultz __setup("clocksource=", boot_override_clocksource); 978734efb46Sjohn stultz 979734efb46Sjohn stultz /** 980734efb46Sjohn stultz * boot_override_clock - Compatibility layer for deprecated boot option 981734efb46Sjohn stultz * @str: override name 982734efb46Sjohn stultz * 983734efb46Sjohn stultz * DEPRECATED! Takes a clock= boot argument and uses it 984734efb46Sjohn stultz * as the clocksource override name 985734efb46Sjohn stultz */ 986734efb46Sjohn stultz static int __init boot_override_clock(char* str) 987734efb46Sjohn stultz { 9885d0cf410Sjohn stultz if (!strcmp(str, "pmtmr")) { 9895d0cf410Sjohn stultz printk("Warning: clock=pmtmr is deprecated. " 9905d0cf410Sjohn stultz "Use clocksource=acpi_pm.\n"); 9915d0cf410Sjohn stultz return boot_override_clocksource("acpi_pm"); 9925d0cf410Sjohn stultz } 9935d0cf410Sjohn stultz printk("Warning! clock= boot option is deprecated. " 9945d0cf410Sjohn stultz "Use clocksource=xyz\n"); 995734efb46Sjohn stultz return boot_override_clocksource(str); 996734efb46Sjohn stultz } 997734efb46Sjohn stultz 998734efb46Sjohn stultz __setup("clock=", boot_override_clock); 999