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

📄 time.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 1991, 1992, 1995  Linus Torvalds * Copyright (C) 1996 - 2000  Ralf Baechle * * This file contains the time handling details for PC-style clocks as * found in some MIPS systems. */#include <linux/config.h>#include <linux/errno.h>#include <linux/init.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/kernel_stat.h>#include <asm/bootinfo.h>#include <asm/mipsregs.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/ddb5074.h>#include <linux/mc146818rtc.h>#include <linux/timex.h>extern volatile unsigned long wall_jiffies;unsigned long r4k_interval;extern rwlock_t xtime_lock;/* * Change this if you have some constant time drift *//* This is the value for the PC-style PICs. *//* #define USECS_PER_JIFFY (1000020/HZ) *//* This is for machines which generate the exact clock. */#define USECS_PER_JIFFY (1000000/HZ)/* Cycle counter value at the previous timer interrupt.. */static unsigned int timerhi, timerlo;/* * On MIPS only R4000 and better have a cycle counter. * * FIXME: Does playing with the RP bit in c0_status interfere with this code? */static unsigned long do_fast_gettimeoffset(void){	u32 count;	unsigned long res, tmp;	/* Last jiffy when do_fast_gettimeoffset() was called. */	static unsigned long last_jiffies;	unsigned long quotient;	/*	 * Cached "1/(clocks per usec)*2^32" value.	 * It has to be recalculated once each jiffy.	 */	static unsigned long cached_quotient;	tmp = jiffies;	quotient = cached_quotient;	if (tmp && last_jiffies != tmp) {		last_jiffies = tmp;		__asm__(".set\tnoreorder\n\t"			".set\tnoat\n\t"			".set\tmips3\n\t"			"lwu\t%0,%2\n\t"			"dsll32\t$1,%1,0\n\t"			"or\t$1,$1,%0\n\t"			"ddivu\t$0,$1,%3\n\t"			"mflo\t$1\n\t"			"dsll32\t%0,%4,0\n\t"			"nop\n\t"			"ddivu\t$0,%0,$1\n\t"			"mflo\t%0\n\t"			".set\tmips0\n\t"			".set\tat\n\t"			".set\treorder"			:"=&r" (quotient)			:"r" (timerhi),			 "m" (timerlo),			 "r" (tmp),			 "r" (USECS_PER_JIFFY)			:"$1");		cached_quotient = quotient;	}	/* Get last timer tick in absolute kernel time */	count = read_32bit_cp0_register(CP0_COUNT);	/* .. relative to previous jiffy (32 bits is enough) */	count -= timerlo;//printk("count: %08lx, %08lx:%08lx\n", count, timerhi, timerlo);	__asm__("multu\t%1,%2\n\t"		"mfhi\t%0"		:"=r" (res)		:"r" (count),		 "r" (quotient));	/* 	 * Due to possible jiffies inconsistencies, we need to check 	 * the result so that we'll get a timer that is monotonic.	 */	if (res >= USECS_PER_JIFFY)		res = USECS_PER_JIFFY-1;	return res;}/* 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! */#define TICK_SIZE tickstatic unsigned long do_slow_gettimeoffset(void){	int count;	static int count_p = LATCH;    /* for the first call after boot */	static unsigned long jiffies_p;	/*	 * cache volatile jiffies temporarily; we have IRQs turned off. 	 */	unsigned long jiffies_t;	/* 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;	/*	 * 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.)	 */	if( jiffies_t == jiffies_p ) {		if( count > count_p ) {			/* the nutcase */			outb_p(0x0A, 0x20);			/* assumption about timer being IRQ1 */			if (inb(0x20) & 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 {				printk("do_slow_gettimeoffset(): hardware timer problem?\n");			}		}	} 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;/* * This version of gettimeofday has near microsecond resolution. */void do_gettimeofday(struct timeval *tv){	unsigned long flags;	read_lock_irqsave (&xtime_lock, flags);	*tv = xtime;	tv->tv_usec += do_gettimeoffset();	/*	 * xtime is atomically updated in timer_bh. jiffies - wall_jiffies	 * is nonzero if the timer bottom half hasnt executed yet.	 */	if (jiffies - wall_jiffies)		tv->tv_usec += USECS_PER_JIFFY;	read_unlock_irqrestore (&xtime_lock, flags);	if (tv->tv_usec >= 1000000) {		tv->tv_usec -= 1000000;		tv->tv_sec++;	}}void do_settimeofday(struct timeval *tv){	write_lock_irq (&xtime_lock);	/* This is revolting. We need to set the xtime.tv_usec	 * correctly. However, the value in this location is	 * is value at the last tick.	 * Discover what correction gettimeofday	 * would have done, and then undo it!	 */	tv->tv_usec -= do_gettimeoffset();	if (tv->tv_usec < 0) {		tv->tv_usec += 1000000;		tv->tv_sec--;	}	xtime = *tv;	time_adjust = 0;		/* stop active adjtime() */	time_status |= STA_UNSYNC;	time_maxerror = NTP_PHASE_LIMIT;	time_esterror = NTP_PHASE_LIMIT;	write_unlock_irq (&xtime_lock);}/* * In order to set the CMOS clock precisely, set_rtc_mmss has to be * called 500 ms after the second nowtime has started, because when * nowtime is written into the registers of the CMOS clock, it will * jump to the next second precisely 500 ms later. Check the Motorola * MC146818A or Dallas DS12887 data sheet for details. * * BUG: This routine does not handle hour overflow properly; it just *      sets the minutes. Usually you won't notice until after reboot! */

⌨️ 快捷键说明

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