plan 9 kernel history: overview | file list | diff list

1994/0810/pc/clock.c (diff list | history)

pc/clock.c on 1991/0704
1991/0704    
#include	"u.h" 
1992/0321    
#include	"../port/lib.h" 
1991/0704    
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
#include	"io.h" 
1991/0706    
#include	"ureg.h" 
1991/0704    
 
1991/0705    
/* 
 *  8253 timer 
 */ 
enum 
{ 
1991/0709    
	T0cntr=	0x40,		/* counter ports */ 
	T1cntr=	0x41,		/* ... */ 
	T2cntr=	0x42,		/* ... */ 
	Tmode=	0x43,		/* mode port */ 
1991/0705    
 
1992/0923    
	/* commands */ 
	Latch0=	0x00,		/* latch counter 0's value */ 
	Load0=	0x30,		/* load counter 0 with 2 bytes */ 
 
	/* modes */ 
	Square=	0x36,		/* perioic square wave */ 
1994/0512    
	Trigger= 0x30,		/* interrupt on terminal count */ 
1992/0923    
 
	Freq=	1193182,	/* Real clock frequency */ 
1991/0705    
}; 
 
1994/0809    
static int cpufreq = 66000000; 
static int cpumhz = 66; 
1994/0716    
static int cputype = 486; 
static int loopconst = 100; 
1992/0923    
 
1993/1124    
static void 
clock(Ureg *ur, void *arg) 
{ 
	Proc *p; 
	int nrun = 0; 
 
	USED(arg); 
 
	m->ticks++; 
 
	checkalarms(); 
1994/0128    
	hardclock(); 
1994/0715    
	uartclock(); 
1993/1124    
 
	/* 
	 *  process time accounting 
	 */ 
	p = m->proc; 
	if(p){ 
		nrun = 1; 
		p->pc = ur->pc; 
		if (p->state==Running) 
			p->time[p->insyscall]++; 
	} 
	nrun = (nrdy+nrun)*1000; 
	MACHP(0)->load = (MACHP(0)->load*19+nrun)/20; 
 
1994/0809    
	if(up && up->state == Running){ 
1994/0507    
		if(anyready()) 
			sched(); 
	 
		/* user profiling clock */ 
1994/0810    
		if((ur->cs&0xffff) == UESEL) 
1994/0809    
			(*(ulong*)(USTKTOP-BY2WD)) += TK2MS(1); 
1994/0507    
	} 
1993/1124    
 
1994/0525    
	mouseclock(); 
1993/1124    
} 
 
1994/0302    
/* 
1994/0716    
 *  delay for l milliseconds more or less.  delayloop is set by 
 *  clockinit() to match the actual CPU speed. 
1994/0302    
 */ 
1994/0716    
void 
delay(int l) 
1994/0302    
{ 
1994/0716    
	aamloop(l*loopconst); 
} 
1994/0302    
 
void 
printcpufreq(void) 
{ 
1994/0809    
	print("CPU is a %ud MHz (%ud Hz) %d\n", cpumhz, cpufreq, cputype); 
1994/0302    
} 
 
1991/0704    
void 
clockinit(void) 
{ 
1992/0923    
	ulong x, y;	/* change in counter */ 
1994/0716    
	ulong cycles, loops; 
1992/0922    
 
1994/0809    
	switch(cputype = x86()){ 
	case 386: 
		loops = 10000; 
		cycles = 32; 
		break; 
	case 486: 
		loops = 10000; 
		cycles = 22; 
		break; 
	default: 
		loops = 30000; 
		cycles = 23; 
		break; 
	} 
 
1991/0709    
	/* 
	 *  set vector for clock interrupts 
	 */ 
1993/1124    
	setvec(Clockvec, clock, 0); 
1991/0709    
 
	/* 
1994/0512    
	 *  set clock for 1/HZ seconds 
1991/0709    
	 */ 
1994/0603    
	outb(Tmode, Load0|Square); 
1991/0709    
	outb(T0cntr, (Freq/HZ));	/* low byte */ 
	outb(T0cntr, (Freq/HZ)>>8);	/* high byte */ 
1992/0922    
 
1994/0809    
 
1992/0922    
	/* 
1994/0716    
	 *  measure time for the loop 
	 * 
	 *			MOVL	loops,CX 
	 *	aaml1:	 	AAM 
	 *			LOOP	aaml1 
	 * 
	 *  the time for the loop should be independent from external 
	 *  cache's and memory system since it fits in the execution 
	 *  prefetch buffer. 
	 * 
1992/0922    
	 */ 
	outb(Tmode, Latch0); 
1992/0923    
	x = inb(T0cntr); 
	x |= inb(T0cntr)<<8; 
1994/0716    
	aamloop(loops); 
1992/0923    
	outb(Tmode, Latch0); 
	y = inb(T0cntr); 
	y |= inb(T0cntr)<<8; 
	x -= y; 
 
	/* 
1994/0716    
	 *  counter  goes at twice the frequency, once per transition, 
1994/0809    
	 *  i.e., twice per square wave 
1992/0923    
	 */ 
1994/0716    
	x >>= 1; 
 
	/* 
 	 *  figure out clock frequency and a loop multiplier for delay(). 
	 */ 
1994/0717    
	cpufreq = loops*((cycles*Freq)/x); 
1994/0716    
	loopconst = (cpufreq/1000)/cycles;	/* AAM+LOOP's for 1 ms */ 
1994/0719    
 
1994/0809    
	/* 
	 *  add in possible .1% error and convert to MHz 
	 */ 
	cpumhz = (cpufreq + cpufreq/1000)/1000000; 
1991/0808    
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)