Rev 305 | Rev 353 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
120 | giacomo | 22 | /* Added Advanced Timer Code |
23 | * |
||
24 | * Date: 8.4.2003 |
||
25 | * Author: Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
26 | * |
||
27 | */ |
||
28 | |||
2 | pj | 29 | /* Time Event routines */ |
30 | |||
31 | #include <ll/i386/stdlib.h> |
||
32 | #include <ll/i386/mem.h> |
||
33 | #include <ll/i386/error.h> |
||
34 | #include <ll/i386/hw-arch.h> |
||
40 | pj | 35 | #include <ll/i386/pic.h> |
2 | pj | 36 | #include <ll/i386/pit.h> |
299 | giacomo | 37 | #include <ll/i386/apic.h> |
131 | giacomo | 38 | #include <ll/i386/advtimer.h> |
305 | giacomo | 39 | #include <ll/i386/64bit.h> |
131 | giacomo | 40 | |
2 | pj | 41 | #include <ll/sys/ll/ll-data.h> |
42 | #include <ll/sys/ll/ll-instr.h> |
||
304 | giacomo | 43 | #include <ll/sys/ll/ll-func.h> |
2 | pj | 44 | #include <ll/sys/ll/time.h> |
45 | #include <ll/sys/ll/event.h> |
||
46 | |||
47 | FILE(Event); |
||
48 | |||
49 | extern LL_ARCH ll_arch; |
||
50 | |||
51 | BYTE frc; |
||
52 | |||
53 | /* Timer 0 usec base tick */ |
||
54 | DWORD ticksize; |
||
55 | |||
56 | /* Timer 0 loaded time constant (= ticksize * 1.197) */ |
||
57 | WORD pit_time_const; |
||
58 | DWORD timermode; |
||
59 | |||
60 | static DWORD nts; /* System tick in nanoSeconds... */ |
||
61 | struct timespec actTime; /* Time (in nanosecs)... */ |
||
62 | extern int activeInt; |
||
63 | |||
64 | WORD lastTime; |
||
65 | struct pitspec globalCounter; |
||
66 | |||
67 | struct event eventlist[MAX_EVENT]; |
||
68 | |||
69 | struct event *freeevents; |
||
70 | struct event *firstevent; |
||
71 | |||
72 | extern void *last_handler; |
||
73 | extern void (*evt_prol) (void); |
||
74 | extern void (*evt_epil) (void); |
||
75 | |||
305 | giacomo | 76 | extern unsigned int apic_clk_per_msec; |
120 | giacomo | 77 | |
40 | pj | 78 | void event_setlasthandler(void *p) |
79 | { |
||
80 | last_handler = p; |
||
81 | } |
||
2 | pj | 82 | |
83 | void event_setprologue(void *p) |
||
84 | { |
||
85 | evt_prol = p; |
||
86 | } |
||
87 | |||
88 | void event_setepilogue(void *p) |
||
89 | { |
||
90 | evt_epil = p; |
||
91 | } |
||
92 | |||
93 | /* Switched to timespec */ |
||
94 | int periodic_event_post(struct timespec time, void (*handler) (void *p), |
||
95 | void *par) |
||
96 | { |
||
97 | struct event *p; |
||
98 | struct event *p1, *t; |
||
99 | |||
100 | if (!freeevents) { |
||
304 | giacomo | 101 | message("NO FREE EVENTS !\n"); |
102 | ll_abort(20); |
||
2 | pj | 103 | return -1; |
104 | } |
||
105 | |||
106 | /* Extract from the ``free events'' queue */ |
||
107 | p = freeevents; |
||
108 | freeevents = p->next; |
||
109 | |||
110 | /* Fill the event fields */ |
||
111 | p->handler = handler; |
||
112 | TIMESPEC_ASSIGN(&(p->time), &time); |
||
113 | p->par = par; |
||
114 | |||
115 | /* ...And insert it in the event queue!!! */ |
||
116 | |||
117 | t = NULL; |
||
118 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
119 | for (p1 = firstevent; p1; p1 = t->next) { |
||
120 | /* |
||
121 | SUBTIMESPEC(&time, &(p1->time), &tmp); |
||
122 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
123 | */ |
||
124 | if (TIMESPEC_A_GT_B(&time, &p1->time)) |
||
125 | t = p1; |
||
126 | else |
||
127 | break; |
||
128 | } |
||
129 | |||
130 | /* adjust next entry */ |
||
131 | if (t) { |
||
132 | t->next = p; |
||
133 | } else { |
||
134 | firstevent = p; |
||
135 | } |
||
136 | p->next = p1; |
||
137 | |||
138 | return p->index; |
||
139 | } |
||
140 | |||
141 | int periodic_event_delete(int index) |
||
142 | { |
||
143 | struct event *p1, *t; |
||
144 | |||
145 | t = NULL; |
||
146 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
147 | for (p1 = firstevent; (p1) && (index != p1->index); p1 = t->next) { |
||
148 | t = p1; |
||
149 | } |
||
150 | |||
151 | if (p1 == NULL) { |
||
152 | return -1; |
||
153 | } |
||
154 | |||
155 | if (t == NULL) { |
||
156 | firstevent = p1->next; |
||
157 | } else { |
||
158 | t->next = p1->next; |
||
159 | } |
||
160 | p1->next = freeevents; |
||
161 | freeevents = p1; |
||
162 | |||
163 | return 1; |
||
164 | } |
||
165 | |||
166 | void periodic_wake_up(void) |
||
167 | { /* CHANGE the NAME, please... */ |
||
168 | struct event *p, *pp; |
||
305 | giacomo | 169 | #ifndef __TSC__ |
170 | WORD tmp; |
||
171 | #endif |
||
2 | pj | 172 | |
305 | giacomo | 173 | #ifndef __TSC__ |
120 | giacomo | 174 | tmp = pit_read(frc); |
175 | ADDPITSPEC((WORD) (lastTime - tmp), &globalCounter); |
||
176 | lastTime = tmp; |
||
305 | giacomo | 177 | #endif |
120 | giacomo | 178 | |
2 | pj | 179 | activeInt++; |
180 | if (activeInt == 1 && evt_prol != NULL) { |
||
181 | evt_prol(); |
||
182 | } |
||
183 | |||
305 | giacomo | 184 | #ifndef __TSC__ |
120 | giacomo | 185 | ADDNANO2TIMESPEC(nts, &actTime); |
305 | giacomo | 186 | #else |
131 | giacomo | 187 | ll_read_timespec(&actTime); |
305 | giacomo | 188 | #endif |
120 | giacomo | 189 | |
2 | pj | 190 | for (p = firstevent; p != NULL; p = pp) { |
191 | /* |
||
192 | SUBTIMESPEC(&(p->time), &actTime, &tmp); |
||
193 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
194 | break; |
||
195 | } */ |
||
196 | if ((p->time.tv_sec > actTime.tv_sec) || |
||
197 | ((p->time.tv_sec == actTime.tv_sec) |
||
198 | && (p->time.tv_nsec > actTime.tv_nsec))) { |
||
199 | break; |
||
200 | } |
||
201 | pp = p->next; |
||
202 | p->next = freeevents; |
||
203 | freeevents = p; |
||
204 | firstevent = pp; |
||
205 | p->handler(p->par); |
||
206 | } |
||
207 | |||
208 | if (activeInt == 1 && evt_epil != NULL) { |
||
209 | evt_epil(); |
||
210 | } |
||
211 | activeInt--; |
||
212 | } |
||
213 | |||
214 | void event_init(struct ll_initparms *l) |
||
215 | { |
||
216 | extern void ll_timer(void); |
||
299 | giacomo | 217 | extern void ll_apic_timer(void); |
2 | pj | 218 | int i; |
219 | BYTE mask; |
||
220 | TIME t; |
||
305 | giacomo | 221 | #ifdef __APIC__ |
222 | DWORD apic_clk; |
||
223 | #endif |
||
2 | pj | 224 | |
305 | giacomo | 225 | #ifdef __TSC__ |
226 | ll_init_advtimer(); |
||
227 | #endif |
||
265 | giacomo | 228 | |
305 | giacomo | 229 | #ifndef __APIC__ |
299 | giacomo | 230 | IDT_place(0x40,ll_timer); |
305 | giacomo | 231 | #else |
304 | giacomo | 232 | IDT_place(0x39,ll_apic_timer); |
305 | giacomo | 233 | #endif |
2 | pj | 234 | |
235 | if (l->mode != LL_PERIODIC) { |
||
120 | giacomo | 236 | message("One-shot mode\n"); |
2 | pj | 237 | t = 0; |
305 | giacomo | 238 | #ifndef __APIC__ |
299 | giacomo | 239 | /* Mode: Binary/Mode 4/16 bit Time_const/Counter 0 */ |
240 | pit_init(0, TMR_MD4, 0xFFFF); /* Timer 0, Mode 4, constant 0xFFFF */ |
||
305 | giacomo | 241 | #else |
303 | giacomo | 242 | set_APIC_timer(0xFFFFFFFF); |
243 | enable_APIC_timer(); |
||
305 | giacomo | 244 | #endif |
2 | pj | 245 | } else { |
246 | t = l->tick; |
||
299 | giacomo | 247 | |
248 | /* Translate the tick value in usec into a suitable time constant */ |
||
2 | pj | 249 | /* for 8254 timer chip; the chip is driven with a 1.19718 MHz */ |
250 | /* frequency; then the effective frequency is given by the base */ |
||
251 | /* frequency divided for the time constant; the tick is the inverse */ |
||
252 | /* of this effective frequency (in usec!) */ |
||
253 | /* Time-Constant = f_base (MHz) * tick (usec) */ |
||
254 | /* If T-C == 0 -> T-C = 65536 (Max available) */ |
||
255 | ticksize = t; |
||
265 | giacomo | 256 | |
305 | giacomo | 257 | #ifndef __APIC__ |
2 | pj | 258 | |
329 | giacomo | 259 | mul32div32to32(t,1193182,1000000,t); |
305 | giacomo | 260 | |
299 | giacomo | 261 | /* Only for security! This should cause timer overrun */ |
262 | /* While 0 would set maximum period on timer */ |
||
263 | if (t == 0) |
||
264 | t = 1; |
||
265 | pit_time_const = (WORD) (t & 0xFFFF); |
||
266 | /* Mode: Binary/Mode 2/16 bit Time_const/Counter 0 */ |
||
267 | pit_init(0, TMR_MD2, t); /* Timer 0, Mode 2, Time constant t */ |
||
268 | |||
305 | giacomo | 269 | #else |
2 | pj | 270 | |
305 | giacomo | 271 | mul32div32to32(t,apic_clk_per_msec,1000,apic_clk); |
303 | giacomo | 272 | set_APIC_timer(apic_clk); |
273 | enable_APIC_timer(); |
||
299 | giacomo | 274 | |
305 | giacomo | 275 | #endif |
2 | pj | 276 | } |
277 | timermode = l->mode; |
||
301 | giacomo | 278 | |
305 | giacomo | 279 | #ifndef __APIC__ |
299 | giacomo | 280 | if (ll_arch.x86.cpu > 4) { |
301 | giacomo | 281 | /* Timer1: mode 0, time const 0... */ |
282 | pit_init(1, TMR_MD0, 0); |
||
283 | frc = 1; |
||
299 | giacomo | 284 | } else { |
301 | giacomo | 285 | frc = 2; |
286 | pit_init(2, TMR_MD0, 0); |
||
287 | outp(0x61, 3); |
||
299 | giacomo | 288 | } |
305 | giacomo | 289 | #endif |
2 | pj | 290 | |
301 | giacomo | 291 | mask = ll_in(0x21); |
292 | mask &= 0xFE; /* 0xFE = ~0x01 */ |
||
293 | ll_out(0x21, mask); |
||
294 | |||
2 | pj | 295 | /* Init the event list... */ |
296 | for (i = 0; i < MAX_EVENT; i++) { |
||
297 | if (i < MAX_EVENT - 1) { |
||
298 | eventlist[i].next = &(eventlist[i + 1]); |
||
299 | } |
||
300 | eventlist[i].index = i; |
||
301 | } |
||
302 | eventlist[MAX_EVENT - 1].next = NULL; |
||
303 | freeevents = &(eventlist[0]); |
||
304 | |||
305 | evt_prol = NULL; |
||
306 | evt_epil = NULL; |
||
307 | |||
308 | /* Initialization of the time variables for periodic mode */ |
||
309 | nts = ticksize * 1000; |
||
310 | NULL_TIMESPEC(&actTime); |
||
120 | giacomo | 311 | |
2 | pj | 312 | /* Initialization of the general time variables */ |
313 | NULLPITSPEC(&globalCounter); |
||
314 | lastTime = 0; |
||
315 | |||
316 | if (timermode == LL_PERIODIC) { |
||
317 | event_post = periodic_event_post; |
||
318 | event_delete = periodic_event_delete; |
||
319 | } else { |
||
320 | event_post = oneshot_event_post; |
||
321 | event_delete = oneshot_event_delete; |
||
322 | } |
||
40 | pj | 323 | |
324 | /* Last but not least... */ |
||
305 | giacomo | 325 | #ifndef __APIC__ |
326 | irq_unmask(0); |
||
327 | #endif |
||
299 | giacomo | 328 | |
2 | pj | 329 | } |