2 * GeekOS timer interrupt support
3 * Copyright (c) 2001,2003 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
7 * This is free software. You are permitted to use,
8 * redistribute, and modify it as specified in the file "COPYING".
12 #include <geekos/io.h>
13 #include <geekos/int.h>
14 #include <geekos/irq.h>
15 #include <geekos/kthread.h>
16 #include <geekos/timer.h>
18 #include <geekos/serial.h>
19 #include <geekos/debug.h>
22 /* JRL Add a cpu frequency measurement */
26 #define __SLOW_DOWN_IO "\noutb %%al,$0x80"
29 #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
31 #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO
37 static inline void out##s(unsigned x value, unsigned short port) {
39 #define __OUT2(s,s1,s2) \
40 __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
42 #define __OUT(s,s1,x) \
43 __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "Nd" (port)); } \
44 __OUT1(s##_p,x) __OUT2(s,s1,"w") __FULL_SLOW_DOWN_IO : : "a" (value), "Nd" (port));} \
48 static inline RETURN_TYPE in##s(unsigned short port) { RETURN_TYPE _v;
50 #define __IN2(s,s1,s2) \
51 __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
53 #define __IN(s,s1,i...) \
54 __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "Nd" (port) ,##i ); return _v; } \
55 __IN1(s##_p) __IN2(s,s1,"w") __FULL_SLOW_DOWN_IO : "=a" (_v) : "Nd" (port) ,##i ); return _v; } \
58 #define RETURN_TYPE unsigned char
61 #define RETURN_TYPE unsigned short
64 #define RETURN_TYPE unsigned int
79 #define rdtscll(val) \
80 __asm__ __volatile__("rdtsc" : "=A" (val))
82 #elif defined(__x86_64__)
84 #define rdtscll(val) do { \
86 asm volatile("rdtsc" : "=a" (a), "=d" (d)); \
87 (val) = ((unsigned long)a) | (((unsigned long)d)<<32); \
92 #define do_div(n,base) ({ \
93 unsigned long __upper, __low, __high, __mod, __base; \
95 asm("":"=a" (__low), "=d" (__high):"A" (n)); \
98 __upper = __high % (__base); \
99 __high = __high / (__base); \
101 asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
102 asm("":"=A" (n):"a" (__low),"d" (__high)); \
108 * This uses the Programmable Interval Timer that is standard on all
109 * PC-compatible systems to determine the time stamp counter frequency.
111 * This uses the speaker output (channel 2) of the PIT. This is better than
112 * using the timer interrupt output because we can read the value of the
113 * speaker with just one inb(), where we need three i/o operations for the
114 * interrupt channel. We count how many ticks the TSC does in 50 ms.
116 * Returns the detected time stamp counter frequency in KHz.
121 pit_calibrate_tsc(void)
125 // unsigned long flags;
126 unsigned long pit_tick_rate = 1193182UL; /* 1.193182 MHz */
128 // spin_lock_irqsave(&pit_lock, flags);
130 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
133 outb((pit_tick_rate / (1000 / 50)) & 0xff, 0x42);
134 outb((pit_tick_rate / (1000 / 50)) >> 8, 0x42);
135 // start = get_cycles_sync();
137 while ((inb(0x61) & 0x20) == 0);
139 // end = get_cycles_sync();
141 // spin_unlock_irqrestore(&pit_lock, flags);
144 // return (end - start) / 50;
160 * Global tick counter
162 volatile ulong_t g_numTicks;
165 * Number of times the spin loop can execute during one timer tick
167 static int s_spinCountPerTick;
170 * Number of ticks to wait before calibrating the delay loop.
172 #define CALIBRATE_NUM_TICKS 3
175 * The default quantum; maximum number of ticks a thread can use before
176 * we suspend it and choose another.
178 #define DEFAULT_MAX_TICKS 4
183 int g_Quantum = DEFAULT_MAX_TICKS;
187 * FIXME: should set this to something more reasonable, like 100.
190 //#define TICKS_PER_SEC 18
192 /*#define DEBUG_TIMER */
194 # define Debug(args...) Print(args)
196 # define Debug(args...)
199 /* ----------------------------------------------------------------------
201 * ---------------------------------------------------------------------- */
203 static void Timer_Interrupt_Handler(struct Interrupt_State* state)
205 struct Kernel_Thread* current = g_currentThread;
212 #define STACK_LEN 256
214 SerialPrint("Host Timer Interrupt Handler running\n");
215 SerialPrint("Timer====\n");
216 Dump_Interrupt_State(state);
217 // SerialMemDump((unsigned char*)(¤t),STACK_LEN);
218 SerialPrint("Timer done===\n");
221 /* Update global and per-thread number of ticks */
226 * If thread has been running for an entire quantum,
227 * inform the interrupt return code that we want
228 * to choose a new thread.
230 if (current->numTicks >= g_Quantum) {
231 g_needReschedule = true;
239 * Temporary timer interrupt handler used to calibrate
242 static void Timer_Calibrate(struct Interrupt_State* state)
245 if (g_numTicks < CALIBRATE_NUM_TICKS)
249 * Now we can look at EAX, which reflects how many times
250 * the loop has executed
252 /*Print("Timer_Calibrate: eax==%d\n", state->eax);*/
253 s_spinCountPerTick = INT_MAX - state->eax;
254 state->eax = 0; /* make the loop terminate */
260 * Delay loop; spins for given number of iterations.
262 static void Spin(int count)
265 * The assembly code is the logical equivalent of
266 * while (count-- > 0) { // waste some time }
267 * We rely on EAX being used as the counter
272 __asm__ __volatile__ (
275 "nop; nop; nop; nop; nop; nop\n\t"
276 "nop; nop; nop; nop; nop; nop\n\t"
284 * Calibrate the delay loop.
285 * This will initialize s_spinCountPerTick, which indicates
286 * how many iterations of the loop are executed per timer tick.
288 static void Calibrate_Delay(void)
290 Disable_Interrupts();
292 /* Install temporarily interrupt handler */
293 Install_IRQ(TIMER_IRQ, &Timer_Calibrate);
294 Enable_IRQ(TIMER_IRQ);
298 /* Wait a few ticks */
299 while (g_numTicks < CALIBRATE_NUM_TICKS)
303 * Execute the spin loop.xs
304 * The temporary interrupt handler will overwrite the
305 * loop counter when the next tick occurs.
313 Disable_Interrupts();
316 * Mask out the timer IRQ again,
317 * since we will be installing a real timer interrupt handler.
319 Disable_IRQ(TIMER_IRQ);
323 /* ----------------------------------------------------------------------
325 * ---------------------------------------------------------------------- */
327 void Init_Timer(void)
329 ushort_t foo = 1193182L / HZ;
331 cpu_khz_freq = pit_calibrate_tsc();
332 PrintBoth("CPU KHZ=%lu\n", (ulong_t)cpu_khz_freq);
334 PrintBoth("Initializing timer and setting to %d Hz...\n",HZ);
336 /* Calibrate for delay loop */
338 PrintBoth("Delay loop: %d iterations per tick\n", s_spinCountPerTick);
342 Out_Byte(0x43,0x36); // channel 0, LSB/MSB, mode 3, binary
343 Out_Byte(0x40, foo & 0xff); // LSB
344 Out_Byte(0x40, foo >>8); // MSB
346 /* Install an interrupt handler for the timer IRQ */
348 Install_IRQ(TIMER_IRQ, &Timer_Interrupt_Handler);
349 Enable_IRQ(TIMER_IRQ);
353 #define US_PER_TICK (HZ * 1000000)
356 * Spin for at least given number of microseconds.
357 * FIXME: I'm sure this implementation leaves a lot to
360 void Micro_Delay(int us)
362 int num = us * s_spinCountPerTick;
363 int denom = US_PER_TICK;
365 int numSpins = num / denom;
366 int rem = num % denom;
371 Debug("Micro_Delay(): num=%d, denom=%d, spin count = %d\n", num, denom, numSpins);