⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time.c

📁 一个2.4.21版本的嵌入式linux内核
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/arch/i386/kernel/time.c * *  Copyright (C) 1991, 1992, 1995  Linus Torvalds * * This file contains the PC-specific time handling details: * reading the RTC at bootup, etc.. * 1994-07-02    Alan Modra *	fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime * 1995-03-26    Markus Kuhn *      fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887 *      precision CMOS clock update * 1996-05-03    Ingo Molnar *      fixed time warps in do_[slow|fast]_gettimeoffset() * 1997-09-10	Updated NTP code according to technical memorandum Jan '96 *		"A Kernel Model for Precision Timekeeping" by Dave Mills * 1998-09-05    (Various) *	More robust do_fast_gettimeoffset() algorithm implemented *	(works with APM, Cyrix 6x86MX and Centaur C6), *	monotonic gettimeofday() with fast_get_timeoffset(), *	drift-proof precision TSC calibration on boot *	(C. Scott Ananian <cananian@alumni.princeton.edu>, Andrew D. *	Balsa <andrebalsa@altern.org>, Philip Gladstone <philip@raptor.com>; *	ported from 2.0.35 Jumbo-9 by Michael Krause <m.krause@tu-harburg.de>). * 1998-12-16    Andrea Arcangeli *	Fixed Jumbo-9 code in 2.1.131: do_gettimeofday was missing 1 jiffy *	because was not accounting lost_ticks. * 1998-12-24 Copyright (C) 1998  Andrea Arcangeli *	Fixed a xtime SMP race (we need the xtime_lock rw spinlock to *	serialize accesses to xtime/lost_ticks). */#include <linux/errno.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/param.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/time.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/smp.h>#include <asm/io.h>#include <asm/smp.h>#include <asm/irq.h>#include <asm/msr.h>#include <asm/delay.h>#include <asm/mpspec.h>#include <asm/uaccess.h>#include <asm/processor.h>#include <linux/mc146818rtc.h>#include <linux/timex.h>#include <linux/config.h>#include <asm/fixmap.h>#include <asm/cobalt.h>/* * for x86_do_profile() */#include <linux/irq.h>unsigned long cpu_khz;	/* Detected as we calibrate the TSC *//* Number of usecs that the last interrupt was delayed */static int delay_at_last_interrupt;static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter *//* Cached *multiplier* to convert TSC counts to microseconds. * (see the equation below). * Equal to 2^32 * (1 / (clocks per usec) ). * Initialized in time_init. */unsigned long fast_gettimeoffset_quotient;extern rwlock_t xtime_lock;extern unsigned long wall_jiffies;spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;static inline unsigned long do_fast_gettimeoffset(void){	register unsigned long eax, edx;	/* Read the Time Stamp Counter */	rdtsc(eax,edx);	/* .. relative to previous jiffy (32 bits is enough) */	eax -= last_tsc_low;	/* tsc_low delta */	/*         * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient         *             = (tsc_low delta) * (usecs_per_clock)         *             = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)	 *	 * Using a mull instead of a divl saves up to 31 clock cycles	 * in the critical path.         */	__asm__("mull %2"		:"=a" (eax), "=d" (edx)		:"rm" (fast_gettimeoffset_quotient),		 "0" (eax));	/* our adjusted time offset in microseconds */	return delay_at_last_interrupt + edx;}#define TICK_SIZE tickspinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;EXPORT_SYMBOL(i8253_lock);extern spinlock_t i8259A_lock;#ifndef CONFIG_X86_TSC/* This function must be called with interrupts disabled  * It was inspired by Steve McCanne's microtime-i386 for BSD.  -- jrs *  * However, the pc-audio speaker driver changes the divisor so that * it gets interrupted rather more often - it loads 64 into the * counter rather than 11932! This has an adverse impact on * do_gettimeoffset() -- it stops working! What is also not * good is that the interval that our timer function gets called * is no longer 10.0002 ms, but 9.9767 ms. To get around this * would require using a different timing source. Maybe someone * could use the RTC - I know that this can interrupt at frequencies * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix * it so that at startup, the timer code in sched.c would select * using either the RTC or the 8253 timer. The decision would be * based on whether there was any other device around that needed * to trample on the 8253. I'd set up the RTC to interrupt at 1024 Hz, * and then do some jiggery to have a version of do_timer that  * advanced the clock by 1/1024 s. Every time that reached over 1/100 * of a second, then do all the old code. If the time was kept correct * then do_gettimeoffset could just return 0 - there is no low order * divider that can be accessed. * * Ideally, you would be able to use the RTC for the speaker driver, * but it appears that the speaker driver really needs interrupt more * often than every 120 us or so. * * Anyway, this needs more thought....		pjsg (1993-08-28) *  * If you are really that interested, you should be reading * comp.protocols.time.ntp! */static unsigned long do_slow_gettimeoffset(void){	int count;	static int count_p = LATCH;    /* for the first call after boot */	static unsigned long jiffies_p = 0;	/*	 * cache volatile jiffies temporarily; we have IRQs turned off. 	 */	unsigned long jiffies_t;	/* gets recalled with irq locally disabled */	spin_lock(&i8253_lock);	/* timer count may underflow right here */	outb_p(0x00, 0x43);	/* latch the count ASAP */	count = inb_p(0x40);	/* read the latched count */	/*	 * We do this guaranteed double memory access instead of a _p 	 * postfix in the previous port access. Wheee, hackady hack	 */ 	jiffies_t = jiffies;	count |= inb_p(0x40) << 8;	        /* VIA686a test code... reset the latch if count > max + 1 */        if (count > LATCH) {                outb_p(0x34, 0x43);                outb_p(LATCH & 0xff, 0x40);                outb(LATCH >> 8, 0x40);                count = LATCH - 1;        }		spin_unlock(&i8253_lock);	/*	 * avoiding timer inconsistencies (they are rare, but they happen)...	 * there are two kinds of problems that must be avoided here:	 *  1. the timer counter underflows	 *  2. hardware problem with the timer, not giving us continuous time,	 *     the counter does small "jumps" upwards on some Pentium systems,	 *     (see c't 95/10 page 335 for Neptun bug.)	 *//* you can safely undefine this if you don't have the Neptune chipset */#define BUGGY_NEPTUN_TIMER	if( jiffies_t == jiffies_p ) {		if( count > count_p ) {			/* the nutcase */			int i;			spin_lock(&i8259A_lock);			/*			 * This is tricky when I/O APICs are used;			 * see do_timer_interrupt().			 */			i = inb(0x20);			spin_unlock(&i8259A_lock);			/* assumption about timer being IRQ0 */			if (i & 0x01) {				/*				 * We cannot detect lost timer interrupts ... 				 * well, that's why we call them lost, don't we? :)				 * [hmm, on the Pentium and Alpha we can ... sort of]				 */				count -= LATCH;			} else {#ifdef BUGGY_NEPTUN_TIMER				/*				 * for the Neptun bug we know that the 'latch'				 * command doesnt latch the high and low value				 * of the counter atomically. Thus we have to 				 * substract 256 from the counter 				 * ... funny, isnt it? :)				 */				count -= 256;#else				printk("do_slow_gettimeoffset(): hardware timer problem?\n");#endif			}		}	} else		jiffies_p = jiffies_t;	count_p = count;	count = ((LATCH-1) - count) * TICK_SIZE;	count = (count + LATCH/2) / LATCH;	return count;}static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;/* IBM Summit (EXA) Cyclone Timer code*/#ifdef CONFIG_X86_SUMMIT#define CYCLONE_CBAR_ADDR 0xFEB00CD0#define CYCLONE_PMCC_OFFSET 0x51A0#define CYCLONE_MPMC_OFFSET 0x51D0#define CYCLONE_MPCS_OFFSET 0x51A8#define CYCLONE_TIMER_FREQ 100000000int use_cyclone = 0;int __init cyclone_setup(char *str) {	use_cyclone = 1;	return 1;}static u32* volatile cyclone_timer;	/* Cyclone MPMC0 register */static u32 last_cyclone_timer;static inline void mark_timeoffset_cyclone(void){	int count;	unsigned long delta = last_cyclone_timer;	spin_lock(&i8253_lock);	/* quickly read the cyclone timer */	if(cyclone_timer)		last_cyclone_timer = cyclone_timer[0];	/* calculate delay_at_last_interrupt */	outb_p(0x00, 0x43);     /* latch the count ASAP */	count = inb_p(0x40);    /* read the latched count */	count |= inb(0x40) << 8;	spin_unlock(&i8253_lock);	/*lost tick compensation*/	delta = last_cyclone_timer - delta;	if(delta > loops_per_jiffy+2000){		delta = (delta/loops_per_jiffy)-1;		jiffies += delta;	}               	count = ((LATCH-1) - count) * TICK_SIZE;	delay_at_last_interrupt = (count + LATCH/2) / LATCH;}static unsigned long do_gettimeoffset_cyclone(void){	u32 offset;	if(!cyclone_timer)		return delay_at_last_interrupt;	/* Read the cyclone timer */	offset = cyclone_timer[0];	/* .. relative to previous jiffy */	offset = offset - last_cyclone_timer;	/* convert cyclone ticks to microseconds */		/* XXX slow, can we speed this up? */	offset = offset/(CYCLONE_TIMER_FREQ/1000000);	/* our adjusted time offset in microseconds */	return delay_at_last_interrupt + offset;}static void __init init_cyclone_clock(void){	u32* reg;		u32 base;		/* saved cyclone base address */	u32 pageaddr;	/* page that contains cyclone_timer register */	u32 offset;		/* offset from pageaddr to cyclone_timer register */	int i;		printk(KERN_INFO "Summit chipset: Starting Cyclone Counter.\n");	/* find base address */	pageaddr = (CYCLONE_CBAR_ADDR)&PAGE_MASK;	offset = (CYCLONE_CBAR_ADDR)&(~PAGE_MASK);	set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);	reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);	if(!reg){		printk(KERN_ERR "Summit chipset: Could not find valid CBAR register.\n");		use_cyclone = 0;		return;	}	base = *reg;		if(!base){		printk(KERN_ERR "Summit chipset: Could not find valid CBAR value.\n");		use_cyclone = 0;		return;	}		/* setup PMCC */	pageaddr = (base + CYCLONE_PMCC_OFFSET)&PAGE_MASK;	offset = (base + CYCLONE_PMCC_OFFSET)&(~PAGE_MASK);	set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);	reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);	if(!reg){		printk(KERN_ERR "Summit chipset: Could not find valid PMCC register.\n");		use_cyclone = 0;		return;	}	reg[0] = 0x00000001;	/* setup MPCS */	pageaddr = (base + CYCLONE_MPCS_OFFSET)&PAGE_MASK;	offset = (base + CYCLONE_MPCS_OFFSET)&(~PAGE_MASK);	set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);	reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);	if(!reg){		printk(KERN_ERR "Summit chipset: Could not find valid MPCS register.\n");		use_cyclone = 0;		return;	}	reg[0] = 0x00000001;	/* map in cyclone_timer */	pageaddr = (base + CYCLONE_MPMC_OFFSET)&PAGE_MASK;	offset = (base + CYCLONE_MPMC_OFFSET)&(~PAGE_MASK);	set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);	cyclone_timer = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);	if(!cyclone_timer){		printk(KERN_ERR "Summit chipset: Could not find valid MPMC register.\n");		use_cyclone = 0;		return;	}	/*quick test to make sure its ticking*/	for(i=0; i<3; i++){		u32 old = cyclone_timer[0];		int stall = 100;		while(stall--) barrier();		if(cyclone_timer[0] == old){			printk(KERN_ERR "Summit chipset: Counter not counting! DISABLED\n");			cyclone_timer = 0;			use_cyclone = 0;			return;		}	}	/* Everything looks good, so set do_gettimeoffset */	do_gettimeoffset = do_gettimeoffset_cyclone;	}void __cyclone_delay(unsigned long loops){	unsigned long bclock, now;	if(!cyclone_timer)		return;	bclock = cyclone_timer[0];	do {		rep_nop();		now = cyclone_timer[0];	} while ((now-bclock) < loops);}#endif /* CONFIG_X86_SUMMIT */#else#define do_gettimeoffset()	do_fast_gettimeoffset()#endif/* No-cyclone stubs */#ifndef CONFIG_X86_SUMMITint __init cyclone_setup(char *str) {	printk(KERN_ERR "cyclone: Kernel not compiled with CONFIG_X86_SUMMIT, cannot use the cyclone-timer.\n");	return 1;}const int use_cyclone = 0;static void mark_timeoffset_cyclone(void) {}static unsigned long do_gettimeoffset_cyclone(void) {return 0;}static void init_cyclone_clock(void) {}void __cyclone_delay(unsigned long loops) {}#endif /* CONFIG_X86_SUMMIT *//* * This version of gettimeofday has microsecond resolution * and better than microsecond precision on fast x86 machines with TSC. */void do_gettimeofday(struct timeval *tv){	unsigned long flags;	unsigned long usec, sec;	read_lock_irqsave(&xtime_lock, flags);	usec = do_gettimeoffset();	{		unsigned long lost = jiffies - wall_jiffies;		if (lost)			usec += lost * (1000000 / HZ);	}	sec = xtime.tv_sec;	usec += xtime.tv_usec;	read_unlock_irqrestore(&xtime_lock, flags);	while (usec >= 1000000) {		usec -= 1000000;		sec++;	}	tv->tv_sec = sec;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -