Fixed udelay_pit to be aware of the PIT divisor.
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 20 May 2009 23:00:04 +0000 (16:00 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 20 May 2009 23:00:04 +0000 (16:00 -0700)
kern/apic.c
kern/apic.h
kern/env.c
kern/testing.c

index 203001d..aae2291 100644 (file)
@@ -9,8 +9,7 @@
 
 #include <kern/apic.h>
 
-uint64_t tsc_freq = 0;
-uint64_t bus_freq = 0;
+system_timing_t system_timing = {0, 0, 0xffff, 0};
 
 /*
  * Remaps the Programmable Interrupt Controller to use IRQs 32-47
@@ -97,17 +96,17 @@ void timer_init(void){
        tscval[0] = read_tsc();
        udelay_pit(1000000);
        tscval[1] = read_tsc();
-       tsc_freq = tscval[1] - tscval[0];
+       system_timing.tsc_freq = tscval[1] - tscval[0];
        
-       cprintf("tsc_freq %lu\n", tsc_freq);
+       cprintf("TSC Frequency: %llu\n", system_timing.tsc_freq);
        
        lapic_set_timer(1000000000,0xeb, TRUE);
        timercount[0] = read_mmreg32(LAPIC_TIMER_CURRENT);
        udelay_pit(1000000);
        timercount[1] = read_mmreg32(LAPIC_TIMER_CURRENT);
-       bus_freq = (timercount[0] - timercount[1])*128;
+       system_timing.bus_freq = (timercount[0] - timercount[1])*128;
                
-       cprintf("bus_freq %u\n", bus_freq);
+       cprintf("Bus Frequency: %llu\n", system_timing.bus_freq);
        
 }
 
@@ -119,6 +118,8 @@ void pit_set_timer(uint32_t divisor, uint32_t mode)
        outb(TIMER_MODE, mode); 
        outb(TIMER_CNTR0, divisor & 0xff);
        outb(TIMER_CNTR0, (divisor >> 8) );
+       system_timing.pit_mode = mode;
+       system_timing.pit_divisor = divisor;
        // cprintf("timer mode set to %d, divisor %d\n",mode, divisor);
 }
 
@@ -140,12 +141,12 @@ static int getpit()
 void udelay(uint64_t usec)
 {
        #if !defined(__BOCHS__)
-       if (tsc_freq != 0)
+       if (system_timing.tsc_freq != 0)
        {
                uint64_t start, end, now;
 
                start = read_tsc();
-        end = start + (tsc_freq * usec) / 1000000;
+        end = start + (system_timing.tsc_freq * usec) / 1000000;
         //cprintf("start %llu, end %llu\n", start, end);
                if (end == 0) cprintf("This is terribly wrong \n");
                do {
@@ -192,7 +193,7 @@ void udelay_pit(uint64_t usec)
                prev_tick = tick;
                if (delta < 0) {
                        // counter looped around during the delta time period
-                       delta += 0xffff; // maximum count 
+                       delta += system_timing.pit_divisor; // maximum count 
                        if (delta < 0)
                                delta = 0;
                }
@@ -207,6 +208,6 @@ uint64_t gettimer(void)
 
 uint64_t getfreq(void)
 {
-       return tsc_freq;
+       return system_timing.tsc_freq;
 }
 
index c27516e..a7c7c8c 100644 (file)
 #define TIMER_CNTR2 (IO_TIMER1 + TIMER_REG_CNTR2)
 #define TIMER_MODE  (IO_TIMER1 + TIMER_REG_MODE)
 
-extern uint64_t tsc_freq;
-extern uint64_t bus_freq;
+typedef struct system_timing {
+       uint64_t tsc_freq;
+       uint64_t bus_freq;
+       uint16_t pit_divisor;
+       uint8_t pit_mode;
+} system_timing_t;
+
+extern system_timing_t system_timing;
 
 void pic_remap(void);
 void pic_mask_irq(uint8_t irq);
index 6b308c7..f6c6072 100644 (file)
@@ -240,7 +240,7 @@ env_alloc(env_t **newenv_store, envid_t parent_id)
        LIST_REMOVE(e, env_link);
        *newenv_store = e;
 
-       e->env_tscfreq = tsc_freq;
+       e->env_tscfreq = system_timing.tsc_freq;
        // TODO: for now, the only info at procinfo is this env's struct
        // note that we need to copy this over every time we make a change to env
        // that we want userspace to see.  also note that we don't even want to
index 6008879..10cf601 100644 (file)
@@ -470,7 +470,7 @@ void test_pit(void)
        
        cprintf("Starting test for LAPIC (if stable) now (10s)\n");
        enable_irq();
-       lapic_set_timer(10* bus_freq/128, test_vector, TRUE);
+       lapic_set_timer(10* system_timing.bus_freq/128, test_vector, TRUE);
        
        uint32_t waiting = 1;
        register_interrupt_handler(interrupt_handlers, test_vector,