time.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,154 行 · 第 1/2 页

C
1,154
字号
{	unsigned int cpu;	for_each_online_cpu(cpu) {		cpufreq_get(cpu);	}	cpufreq_delayed_issched = 0;}/* if we notice lost ticks, schedule a call to cpufreq_get() as it tries * to verify the CPU frequency the timing core thinks the CPU is running * at is still correct. */static void cpufreq_delayed_get(void){	static int warned;	if (cpufreq_init && !cpufreq_delayed_issched) {		cpufreq_delayed_issched = 1;		if (!warned) {			warned = 1;			printk(KERN_DEBUG "Losing some ticks... checking if CPU frequency changed.\n");		}		schedule_work(&cpufreq_delayed_get_work);	}}static unsigned int  ref_freq = 0;static unsigned long loops_per_jiffy_ref = 0;static unsigned long cpu_khz_ref = 0;static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,				 void *data){        struct cpufreq_freqs *freq = data;	unsigned long *lpj, dummy;	lpj = &dummy;	if (!(freq->flags & CPUFREQ_CONST_LOOPS))#ifdef CONFIG_SMP	lpj = &cpu_data[freq->cpu].loops_per_jiffy;#else	lpj = &boot_cpu_data.loops_per_jiffy;#endif	if (!ref_freq) {		ref_freq = freq->old;		loops_per_jiffy_ref = *lpj;		cpu_khz_ref = cpu_khz;	}        if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||            (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||	    (val == CPUFREQ_RESUMECHANGE)) {                *lpj =		cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);		cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);		if (!(freq->flags & CPUFREQ_CONST_LOOPS))			vxtime.tsc_quot = (1000L << 32) / cpu_khz;	}		set_cyc2ns_scale(cpu_khz_ref / 1000);	return 0;} static struct notifier_block time_cpufreq_notifier_block = {         .notifier_call  = time_cpufreq_notifier};static int __init cpufreq_tsc(void){	INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);	if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,				       CPUFREQ_TRANSITION_NOTIFIER))		cpufreq_init = 1;	return 0;}core_initcall(cpufreq_tsc);#endif/* * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing * it to the HPET timer of known frequency. */#define TICK_COUNT 100000000static unsigned int __init hpet_calibrate_tsc(void){	int tsc_start, hpet_start;	int tsc_now, hpet_now;	unsigned long flags;	local_irq_save(flags);	local_irq_disable();	hpet_start = hpet_readl(HPET_COUNTER);	rdtscl(tsc_start);	do {		local_irq_disable();		hpet_now = hpet_readl(HPET_COUNTER);		sync_core();		rdtscl(tsc_now);		local_irq_restore(flags);	} while ((tsc_now - tsc_start) < TICK_COUNT &&		 (hpet_now - hpet_start) < TICK_COUNT);	return (tsc_now - tsc_start) * 1000000000L		/ ((hpet_now - hpet_start) * hpet_period / 1000);}/* * pit_calibrate_tsc() uses the speaker output (channel 2) of * the PIT. This is better than using the timer interrupt output, * because we can read the value of the speaker with just one inb(), * where we need three i/o operations for the interrupt channel. * We count how many ticks the TSC does in 50 ms. */static unsigned int __init pit_calibrate_tsc(void){	unsigned long start, end;	unsigned long flags;	spin_lock_irqsave(&i8253_lock, flags);	outb((inb(0x61) & ~0x02) | 0x01, 0x61);	outb(0xb0, 0x43);	outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);	outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);	rdtscll(start);	sync_core();	while ((inb(0x61) & 0x20) == 0);	sync_core();	rdtscll(end);	spin_unlock_irqrestore(&i8253_lock, flags);		return (end - start) / 50;}static int hpet_init(void){	unsigned int cfg, id;	if (!vxtime.hpet_address)		return -1;	set_fixmap_nocache(FIX_HPET_BASE, vxtime.hpet_address);	__set_fixmap(VSYSCALL_HPET, vxtime.hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);/* * Read the period, compute tick and quotient. */	id = hpet_readl(HPET_ID);	if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER) ||	    !(id & HPET_ID_LEGSUP))		return -1;	hpet_period = hpet_readl(HPET_PERIOD);	if (hpet_period < 100000 || hpet_period > 100000000)		return -1;	hpet_tick = (1000000000L * (USEC_PER_SEC / HZ) + hpet_period / 2) /		hpet_period;/* * Stop the timers and reset the main counter. */	cfg = hpet_readl(HPET_CFG);	cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);	hpet_writel(cfg, HPET_CFG);	hpet_writel(0, HPET_COUNTER);	hpet_writel(0, HPET_COUNTER + 4);/* * Set up timer 0, as periodic with first interrupt to happen at hpet_tick, * and period also hpet_tick. */	hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |		    HPET_TN_32BIT, HPET_T0_CFG);	hpet_writel(hpet_tick, HPET_T0_CMP);	hpet_writel(hpet_tick, HPET_T0_CMP); /* AK: why twice? *//* * Go! */	cfg |= HPET_CFG_ENABLE | HPET_CFG_LEGACY;	hpet_writel(cfg, HPET_CFG);	return 0;}void __init pit_init(void){	unsigned long flags;	spin_lock_irqsave(&i8253_lock, flags);	outb_p(0x34, 0x43);		/* binary, mode 2, LSB/MSB, ch 0 */	outb_p(LATCH & 0xff, 0x40);	/* LSB */	outb_p(LATCH >> 8, 0x40);	/* MSB */	spin_unlock_irqrestore(&i8253_lock, flags);}int __init time_setup(char *str){	report_lost_ticks = 1;	return 1;}static struct irqaction irq0 = {	timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL};extern void __init config_acpi_tables(void);void __init time_init(void){	char *timename;#ifdef HPET_HACK_ENABLE_DANGEROUS        if (!vxtime.hpet_address) {		printk(KERN_WARNING "time.c: WARNING: Enabling HPET base "		       "manually!\n");                outl(0x800038a0, 0xcf8);                outl(0xff000001, 0xcfc);                outl(0x800038a0, 0xcf8);                vxtime.hpet_address = inl(0xcfc) & 0xfffffffe;		printk(KERN_WARNING "time.c: WARNING: Enabled HPET "		       "at %#lx.\n", vxtime.hpet_address);        }#endif	if (nohpet)		vxtime.hpet_address = 0;	xtime.tv_sec = get_cmos_time();	xtime.tv_nsec = 0;	set_normalized_timespec(&wall_to_monotonic,	                        -xtime.tv_sec, -xtime.tv_nsec);	if (!hpet_init()) {                vxtime_hz = (1000000000000000L + hpet_period / 2) /			hpet_period;		cpu_khz = hpet_calibrate_tsc();		timename = "HPET";	} else {		pit_init();		cpu_khz = pit_calibrate_tsc();		timename = "PIT";	}	printk(KERN_INFO "time.c: Using %ld.%06ld MHz %s timer.\n",	       vxtime_hz / 1000000, vxtime_hz % 1000000, timename);	printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",		cpu_khz / 1000, cpu_khz % 1000);	vxtime.mode = VXTIME_TSC;	vxtime.quot = (1000000L << 32) / vxtime_hz;	vxtime.tsc_quot = (1000L << 32) / cpu_khz;	vxtime.hz = vxtime_hz;	rdtscll_sync(&vxtime.last_tsc);	setup_irq(0, &irq0);	set_cyc2ns_scale(cpu_khz / 1000);}void __init time_init_smp(void){	char *timetype;	if (vxtime.hpet_address) {		timetype = "HPET";		vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;		vxtime.mode = VXTIME_HPET;		do_gettimeoffset = do_gettimeoffset_hpet;	} else {		timetype = "PIT/TSC";		vxtime.mode = VXTIME_TSC;	}	printk(KERN_INFO "time.c: Using %s based timekeeping.\n", timetype);}__setup("report_lost_ticks", time_setup);static long clock_cmos_diff;static int time_suspend(struct sys_device *dev, u32 state){	/*	 * Estimate time zone so that set_time can update the clock	 */	clock_cmos_diff = -get_cmos_time();	clock_cmos_diff += get_seconds();	return 0;}static int time_resume(struct sys_device *dev){	unsigned long flags;	unsigned long sec = get_cmos_time() + clock_cmos_diff;	write_seqlock_irqsave(&xtime_lock,flags);	xtime.tv_sec = sec;	xtime.tv_nsec = 0;	write_sequnlock_irqrestore(&xtime_lock,flags);	return 0;}static struct sysdev_class pit_sysclass = {	.resume = time_resume,	.suspend = time_suspend,	set_kset_name("pit"),};/* XXX this driverfs stuff should probably go elsewhere later -john */static struct sys_device device_i8253 = {	.id	= 0,	.cls	= &pit_sysclass,};static int time_init_device(void){	int error = sysdev_class_register(&pit_sysclass);	if (!error)		error = sysdev_register(&device_i8253);	return error;}device_initcall(time_init_device);#ifdef CONFIG_HPET_EMULATE_RTC/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET * is enabled, we support RTC interrupt functionality in software. * RTC has 3 kinds of interrupts: * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock *    is updated * 2) Alarm Interrupt - generate an interrupt at a specific time of day * 3) Periodic Interrupt - generate periodic interrupt, with frequencies *    2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2) * (1) and (2) above are implemented using polling at a frequency of * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt * overhead. (DEFAULT_RTC_INT_FREQ) * For (3), we use interrupts at 64Hz or user specified periodic * frequency, whichever is higher. */#include <linux/mc146818rtc.h>#include <linux/rtc.h>extern irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);#define DEFAULT_RTC_INT_FREQ 	64#define RTC_NUM_INTS 		1static unsigned long UIE_on;static unsigned long prev_update_sec;static unsigned long AIE_on;static struct rtc_time alarm_time;static unsigned long PIE_on;static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;static unsigned long PIE_count;static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */int is_hpet_enabled(void){	return vxtime.hpet_address != 0;}/* * Timer 1 for RTC, we do not use periodic interrupt feature, * even if HPET supports periodic interrupts on Timer 1. * The reason being, to set up a periodic interrupt in HPET, we need to * stop the main counter. And if we do that everytime someone diables/enables * RTC, we will have adverse effect on main kernel timer running on Timer 0. * So, for the time being, simulate the periodic interrupt in software. * * hpet_rtc_timer_init() is called for the first time and during subsequent * interuppts reinit happens through hpet_rtc_timer_reinit(). */int hpet_rtc_timer_init(void){	unsigned int cfg, cnt;	unsigned long flags;	if (!is_hpet_enabled())		return 0;	/*	 * Set the counter 1 and enable the interrupts.	 */	if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))		hpet_rtc_int_freq = PIE_freq;	else		hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;	local_irq_save(flags);	cnt = hpet_readl(HPET_COUNTER);	cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);	hpet_writel(cnt, HPET_T1_CMP);	local_irq_restore(flags);	cfg = hpet_readl(HPET_T1_CFG);	cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;	hpet_writel(cfg, HPET_T1_CFG);	return 1;}static void hpet_rtc_timer_reinit(void){	unsigned int cfg, cnt;	if (!(PIE_on | AIE_on | UIE_on))		return;	if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))		hpet_rtc_int_freq = PIE_freq;	else		hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;	/* It is more accurate to use the comparator value than current count.*/	cnt = hpet_readl(HPET_T1_CMP);	cnt += hpet_tick*HZ/hpet_rtc_int_freq;	hpet_writel(cnt, HPET_T1_CMP);	cfg = hpet_readl(HPET_T1_CFG);	cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;	hpet_writel(cfg, HPET_T1_CFG);	return;}/* * The functions below are called from rtc driver. * Return 0 if HPET is not being used. * Otherwise do the necessary changes and return 1. */int hpet_mask_rtc_irq_bit(unsigned long bit_mask){	if (!is_hpet_enabled())		return 0;	if (bit_mask & RTC_UIE)		UIE_on = 0;	if (bit_mask & RTC_PIE)		PIE_on = 0;	if (bit_mask & RTC_AIE)		AIE_on = 0;	return 1;}int hpet_set_rtc_irq_bit(unsigned long bit_mask){	int timer_init_reqd = 0;	if (!is_hpet_enabled())		return 0;	if (!(PIE_on | AIE_on | UIE_on))		timer_init_reqd = 1;	if (bit_mask & RTC_UIE) {		UIE_on = 1;	}	if (bit_mask & RTC_PIE) {		PIE_on = 1;		PIE_count = 0;	}	if (bit_mask & RTC_AIE) {		AIE_on = 1;	}	if (timer_init_reqd)		hpet_rtc_timer_init();	return 1;}int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec){	if (!is_hpet_enabled())		return 0;	alarm_time.tm_hour = hrs;	alarm_time.tm_min = min;	alarm_time.tm_sec = sec;	return 1;}int hpet_set_periodic_freq(unsigned long freq){	if (!is_hpet_enabled())		return 0;	PIE_freq = freq;	PIE_count = 0;	return 1;}int hpet_rtc_dropped_irq(void){	if (!is_hpet_enabled())		return 0;	return 1;}irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs){	struct rtc_time curr_time;	unsigned long rtc_int_flag = 0;	int call_rtc_interrupt = 0;	hpet_rtc_timer_reinit();	if (UIE_on | AIE_on) {		rtc_get_rtc_time(&curr_time);	}	if (UIE_on) {		if (curr_time.tm_sec != prev_update_sec) {			/* Set update int info, call real rtc int routine */			call_rtc_interrupt = 1;			rtc_int_flag = RTC_UF;			prev_update_sec = curr_time.tm_sec;		}	}	if (PIE_on) {		PIE_count++;		if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {			/* Set periodic int info, call real rtc int routine */			call_rtc_interrupt = 1;			rtc_int_flag |= RTC_PF;			PIE_count = 0;		}	}	if (AIE_on) {		if ((curr_time.tm_sec == alarm_time.tm_sec) &&		    (curr_time.tm_min == alarm_time.tm_min) &&		    (curr_time.tm_hour == alarm_time.tm_hour)) {			/* Set alarm int info, call real rtc int routine */			call_rtc_interrupt = 1;			rtc_int_flag |= RTC_AF;		}	}	if (call_rtc_interrupt) {		rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));		rtc_interrupt(rtc_int_flag, dev_id, regs);	}	return IRQ_HANDLED;}#endifstatic int __init nohpet_setup(char *s) { 	nohpet = 1;	return 0;} __setup("nohpet", nohpet_setup);

⌨️ 快捷键说明

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