kern

view src/timer.c @ 51:b1e8c8251884

lalalala
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Aug 2011 06:45:29 +0300
parents f65b348780e3
children 88a6c4e192f9
line source
1 #include <stdio.h>
2 #include <time.h>
3 #include "intr.h"
4 #include "asmops.h"
5 #include "timer.h"
6 #include "proc.h"
7 #include "sched.h"
8 #include "config.h"
10 /* frequency of the oscillator driving the 8254 timer */
11 #define OSC_FREQ_HZ 1193182
13 /* macro to divide and round to the nearest integer */
14 #define DIV_ROUND(a, b) ((a) / (b) + ((a) % (b)) / ((b) / 2))
16 /* I/O ports connected to the 8254 */
17 #define PORT_DATA0 0x40
18 #define PORT_DATA1 0x41
19 #define PORT_DATA2 0x42
20 #define PORT_CMD 0x43
22 /* command bits */
23 #define CMD_CHAN0 0
24 #define CMD_CHAN1 (1 << 6)
25 #define CMD_CHAN2 (2 << 6)
26 #define CMD_RDBACK (3 << 6)
28 #define CMD_LATCH 0
29 #define CMD_ACCESS_LOW (1 << 4)
30 #define CMD_ACCESS_HIGH (2 << 4)
31 #define CMD_ACCESS_BOTH (3 << 4)
33 #define CMD_OP_INT_TERM 0
34 #define CMD_OP_ONESHOT (1 << 1)
35 #define CMD_OP_RATE (2 << 1)
36 #define CMD_OP_SQWAVE (3 << 1)
37 #define CMD_OP_SOFT_STROBE (4 << 1)
38 #define CMD_OP_HW_STROBE (5 << 1)
40 #define CMD_MODE_BIN 0
41 #define CMD_MODE_BCD 1
44 #define MSEC_TO_TICKS(ms) ((ms) * TICK_FREQ_HZ / 1000)
46 struct timer_event {
47 int dt; /* remaining ticks delta from the previous event */
49 void (*callback)(void*);
50 void *cbarg;
52 struct timer_event *next;
53 };
56 static void intr_handler();
59 static struct timer_event *evlist;
62 void init_timer(void)
63 {
64 /* calculate the reload count: round(osc / freq) */
65 int reload_count = DIV_ROUND(OSC_FREQ_HZ, TICK_FREQ_HZ);
67 /* set the mode to square wave for channel 0, both low
68 * and high reload count bytes will follow...
69 */
70 outb(CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE, PORT_CMD);
72 /* write the low and high bytes of the reload count to the
73 * port for channel 0
74 */
75 outb(reload_count & 0xff, PORT_DATA0);
76 outb((reload_count >> 8) & 0xff, PORT_DATA0);
78 /* set the timer interrupt handler */
79 interrupt(IRQ_TO_INTR(0), intr_handler);
80 }
82 int start_timer(unsigned long msec, timer_func_t cbfunc, void *cbarg)
83 {
84 int ticks, tsum, istate;
85 struct timer_event *ev, *node;
87 printf("start_timer(%lu)\n", msec);
89 if((ticks = MSEC_TO_TICKS(msec)) <= 0) {
90 cbfunc(cbarg);
91 return 0;
92 }
94 if(!(ev = malloc(sizeof *ev))) {
95 printf("start_timer: failed to allocate timer_event structure\n");
96 return -1;
97 }
98 ev->callback = cbfunc;
99 ev->cbarg = cbarg;
101 istate = get_intr_state();
102 disable_intr();
104 /* insert at the beginning */
105 if(!evlist || ticks <= evlist->dt) {
106 ev->next = evlist;
107 evlist = ev;
109 ev->dt = ticks;
110 if(ev->next) {
111 ev->next->dt -= ticks;
112 }
113 set_intr_state(istate);
114 return 0;
115 }
117 tsum = evlist->dt;
118 node = evlist;
120 while(node->next && ticks > tsum + node->next->dt) {
121 tsum += node->next->dt;
122 node = node->next;
123 }
125 ev->next = node->next;
126 node->next = ev;
128 /* fix the relative times */
129 ev->dt = ticks - tsum;
130 if(ev->next) {
131 ev->next->dt -= ev->dt;
132 }
134 set_intr_state(istate);
135 return 0;
136 }
138 /* This will be called by the interrupt dispatcher approximately
139 * every 1/250th of a second, so it must be extremely fast.
140 * For now, just increasing a tick counter will suffice.
141 */
142 static void intr_handler(int inum)
143 {
144 int istate;
145 struct process *cur_proc;
147 nticks++;
149 printf("TICKS: %d\n", nticks);
151 istate = get_intr_state();
152 disable_intr();
154 /* find out if there are any timers that have to go off */
155 if(evlist) {
156 evlist->dt--;
158 while(evlist->dt <= 0) {
159 struct timer_event *ev = evlist;
160 evlist = evlist->next;
162 printf("timer going off!!!\n");
163 ev->callback(ev->cbarg);
164 free(ev);
165 }
166 }
168 if((cur_proc = get_current_proc())) {
169 if(--cur_proc->ticks_left <= 0) {
170 /* since schedule will not return, we have to notify
171 * the PIC that we're done with the IRQ handling
172 */
173 end_of_irq(INTR_TO_IRQ(inum));
174 schedule();
175 }
176 }
178 set_intr_state(istate);
179 }