Rev 329 | 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 | |||
353 | giacomo | 47 | #include <tracer.h> |
48 | extern unsigned short int currCtx; |
||
49 | |||
2 | pj | 50 | FILE(Event); |
51 | |||
52 | extern LL_ARCH ll_arch; |
||
53 | |||
54 | BYTE frc; |
||
55 | |||
56 | /* Timer 0 usec base tick */ |
||
57 | DWORD ticksize; |
||
58 | |||
59 | /* Timer 0 loaded time constant (= ticksize * 1.197) */ |
||
60 | WORD pit_time_const; |
||
61 | DWORD timermode; |
||
62 | |||
63 | static DWORD nts; /* System tick in nanoSeconds... */ |
||
64 | struct timespec actTime; /* Time (in nanosecs)... */ |
||
65 | extern int activeInt; |
||
66 | |||
67 | WORD lastTime; |
||
68 | struct pitspec globalCounter; |
||
69 | |||
70 | struct event eventlist[MAX_EVENT]; |
||
71 | |||
72 | struct event *freeevents; |
||
73 | struct event *firstevent; |
||
74 | |||
75 | extern void *last_handler; |
||
76 | extern void (*evt_prol) (void); |
||
77 | extern void (*evt_epil) (void); |
||
78 | |||
305 | giacomo | 79 | extern unsigned int apic_clk_per_msec; |
120 | giacomo | 80 | |
40 | pj | 81 | void event_setlasthandler(void *p) |
82 | { |
||
83 | last_handler = p; |
||
84 | } |
||
2 | pj | 85 | |
86 | void event_setprologue(void *p) |
||
87 | { |
||
88 | evt_prol = p; |
||
89 | } |
||
90 | |||
91 | void event_setepilogue(void *p) |
||
92 | { |
||
93 | evt_epil = p; |
||
94 | } |
||
95 | |||
96 | /* Switched to timespec */ |
||
97 | int periodic_event_post(struct timespec time, void (*handler) (void *p), |
||
98 | void *par) |
||
99 | { |
||
100 | struct event *p; |
||
101 | struct event *p1, *t; |
||
102 | |||
353 | giacomo | 103 | TRACER_LOGEVENT(FTrace_EVT_timer_post, 0, 0, 0); |
104 | |||
2 | pj | 105 | if (!freeevents) { |
304 | giacomo | 106 | message("NO FREE EVENTS !\n"); |
107 | ll_abort(20); |
||
2 | pj | 108 | return -1; |
109 | } |
||
110 | |||
111 | /* Extract from the ``free events'' queue */ |
||
112 | p = freeevents; |
||
113 | freeevents = p->next; |
||
114 | |||
115 | /* Fill the event fields */ |
||
116 | p->handler = handler; |
||
117 | TIMESPEC_ASSIGN(&(p->time), &time); |
||
118 | p->par = par; |
||
119 | |||
120 | /* ...And insert it in the event queue!!! */ |
||
121 | |||
122 | t = NULL; |
||
123 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
124 | for (p1 = firstevent; p1; p1 = t->next) { |
||
125 | /* |
||
126 | SUBTIMESPEC(&time, &(p1->time), &tmp); |
||
127 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
128 | */ |
||
129 | if (TIMESPEC_A_GT_B(&time, &p1->time)) |
||
130 | t = p1; |
||
131 | else |
||
132 | break; |
||
133 | } |
||
134 | |||
135 | /* adjust next entry */ |
||
136 | if (t) { |
||
137 | t->next = p; |
||
138 | } else { |
||
139 | firstevent = p; |
||
140 | } |
||
141 | p->next = p1; |
||
142 | |||
143 | return p->index; |
||
144 | } |
||
145 | |||
146 | int periodic_event_delete(int index) |
||
147 | { |
||
148 | struct event *p1, *t; |
||
149 | |||
353 | giacomo | 150 | TRACER_LOGEVENT(FTrace_EVT_timer_delete, 0, 0, 0); |
151 | |||
2 | pj | 152 | t = NULL; |
153 | /* walk through list, finding spot, adjusting ticks parameter */ |
||
154 | for (p1 = firstevent; (p1) && (index != p1->index); p1 = t->next) { |
||
155 | t = p1; |
||
156 | } |
||
157 | |||
158 | if (p1 == NULL) { |
||
159 | return -1; |
||
160 | } |
||
161 | |||
162 | if (t == NULL) { |
||
163 | firstevent = p1->next; |
||
164 | } else { |
||
165 | t->next = p1->next; |
||
166 | } |
||
167 | p1->next = freeevents; |
||
168 | freeevents = p1; |
||
169 | |||
170 | return 1; |
||
171 | } |
||
172 | |||
173 | void periodic_wake_up(void) |
||
174 | { /* CHANGE the NAME, please... */ |
||
175 | struct event *p, *pp; |
||
305 | giacomo | 176 | #ifndef __TSC__ |
177 | WORD tmp; |
||
178 | #endif |
||
2 | pj | 179 | |
353 | giacomo | 180 | TRACER_LOGEVENT(FTrace_EVT_timer_wakeup_start, 0, 0, 0); |
181 | |||
305 | giacomo | 182 | #ifndef __TSC__ |
120 | giacomo | 183 | tmp = pit_read(frc); |
184 | ADDPITSPEC((WORD) (lastTime - tmp), &globalCounter); |
||
185 | lastTime = tmp; |
||
305 | giacomo | 186 | #endif |
120 | giacomo | 187 | |
2 | pj | 188 | activeInt++; |
189 | if (activeInt == 1 && evt_prol != NULL) { |
||
190 | evt_prol(); |
||
191 | } |
||
192 | |||
305 | giacomo | 193 | #ifndef __TSC__ |
120 | giacomo | 194 | ADDNANO2TIMESPEC(nts, &actTime); |
305 | giacomo | 195 | #else |
131 | giacomo | 196 | ll_read_timespec(&actTime); |
305 | giacomo | 197 | #endif |
120 | giacomo | 198 | |
2 | pj | 199 | for (p = firstevent; p != NULL; p = pp) { |
200 | /* |
||
201 | SUBTIMESPEC(&(p->time), &actTime, &tmp); |
||
202 | if ((tmp.tv_sec > 0) && (tmp.tv_nsec > 0)) { |
||
203 | break; |
||
204 | } */ |
||
205 | if ((p->time.tv_sec > actTime.tv_sec) || |
||
206 | ((p->time.tv_sec == actTime.tv_sec) |
||
207 | && (p->time.tv_nsec > actTime.tv_nsec))) { |
||
208 | break; |
||
209 | } |
||
210 | pp = p->next; |
||
211 | p->next = freeevents; |
||
212 | freeevents = p; |
||
213 | firstevent = pp; |
||
214 | p->handler(p->par); |
||
215 | } |
||
216 | |||
217 | if (activeInt == 1 && evt_epil != NULL) { |
||
218 | evt_epil(); |
||
219 | } |
||
220 | activeInt--; |
||
353 | giacomo | 221 | |
222 | TRACER_LOGEVENT(FTrace_EVT_timer_wakeup_end, 1, (int)currCtx, 0); |
||
223 | |||
2 | pj | 224 | } |
225 | |||
226 | void event_init(struct ll_initparms *l) |
||
227 | { |
||
228 | extern void ll_timer(void); |
||
299 | giacomo | 229 | extern void ll_apic_timer(void); |
2 | pj | 230 | int i; |
231 | BYTE mask; |
||
232 | TIME t; |
||
305 | giacomo | 233 | #ifdef __APIC__ |
234 | DWORD apic_clk; |
||
235 | #endif |
||
2 | pj | 236 | |
305 | giacomo | 237 | #ifdef __TSC__ |
238 | ll_init_advtimer(); |
||
239 | #endif |
||
265 | giacomo | 240 | |
305 | giacomo | 241 | #ifndef __APIC__ |
299 | giacomo | 242 | IDT_place(0x40,ll_timer); |
305 | giacomo | 243 | #else |
304 | giacomo | 244 | IDT_place(0x39,ll_apic_timer); |
305 | giacomo | 245 | #endif |
2 | pj | 246 | |
247 | if (l->mode != LL_PERIODIC) { |
||
120 | giacomo | 248 | message("One-shot mode\n"); |
2 | pj | 249 | t = 0; |
305 | giacomo | 250 | #ifndef __APIC__ |
299 | giacomo | 251 | /* Mode: Binary/Mode 4/16 bit Time_const/Counter 0 */ |
252 | pit_init(0, TMR_MD4, 0xFFFF); /* Timer 0, Mode 4, constant 0xFFFF */ |
||
305 | giacomo | 253 | #else |
303 | giacomo | 254 | set_APIC_timer(0xFFFFFFFF); |
255 | enable_APIC_timer(); |
||
305 | giacomo | 256 | #endif |
2 | pj | 257 | } else { |
258 | t = l->tick; |
||
299 | giacomo | 259 | |
260 | /* Translate the tick value in usec into a suitable time constant */ |
||
2 | pj | 261 | /* for 8254 timer chip; the chip is driven with a 1.19718 MHz */ |
262 | /* frequency; then the effective frequency is given by the base */ |
||
263 | /* frequency divided for the time constant; the tick is the inverse */ |
||
264 | /* of this effective frequency (in usec!) */ |
||
265 | /* Time-Constant = f_base (MHz) * tick (usec) */ |
||
266 | /* If T-C == 0 -> T-C = 65536 (Max available) */ |
||
267 | ticksize = t; |
||
265 | giacomo | 268 | |
305 | giacomo | 269 | #ifndef __APIC__ |
2 | pj | 270 | |
329 | giacomo | 271 | mul32div32to32(t,1193182,1000000,t); |
305 | giacomo | 272 | |
299 | giacomo | 273 | /* Only for security! This should cause timer overrun */ |
274 | /* While 0 would set maximum period on timer */ |
||
275 | if (t == 0) |
||
276 | t = 1; |
||
277 | pit_time_const = (WORD) (t & 0xFFFF); |
||
278 | /* Mode: Binary/Mode 2/16 bit Time_const/Counter 0 */ |
||
279 | pit_init(0, TMR_MD2, t); /* Timer 0, Mode 2, Time constant t */ |
||
280 | |||
305 | giacomo | 281 | #else |
2 | pj | 282 | |
305 | giacomo | 283 | mul32div32to32(t,apic_clk_per_msec,1000,apic_clk); |
303 | giacomo | 284 | set_APIC_timer(apic_clk); |
285 | enable_APIC_timer(); |
||
299 | giacomo | 286 | |
305 | giacomo | 287 | #endif |
2 | pj | 288 | } |
289 | timermode = l->mode; |
||
301 | giacomo | 290 | |
305 | giacomo | 291 | #ifndef __APIC__ |
299 | giacomo | 292 | if (ll_arch.x86.cpu > 4) { |
301 | giacomo | 293 | /* Timer1: mode 0, time const 0... */ |
294 | pit_init(1, TMR_MD0, 0); |
||
295 | frc = 1; |
||
299 | giacomo | 296 | } else { |
301 | giacomo | 297 | frc = 2; |
298 | pit_init(2, TMR_MD0, 0); |
||
299 | outp(0x61, 3); |
||
299 | giacomo | 300 | } |
305 | giacomo | 301 | #endif |
2 | pj | 302 | |
301 | giacomo | 303 | mask = ll_in(0x21); |
304 | mask &= 0xFE; /* 0xFE = ~0x01 */ |
||
305 | ll_out(0x21, mask); |
||
306 | |||
2 | pj | 307 | /* Init the event list... */ |
308 | for (i = 0; i < MAX_EVENT; i++) { |
||
309 | if (i < MAX_EVENT - 1) { |
||
310 | eventlist[i].next = &(eventlist[i + 1]); |
||
311 | } |
||
312 | eventlist[i].index = i; |
||
313 | } |
||
314 | eventlist[MAX_EVENT - 1].next = NULL; |
||
315 | freeevents = &(eventlist[0]); |
||
316 | |||
317 | evt_prol = NULL; |
||
318 | evt_epil = NULL; |
||
319 | |||
320 | /* Initialization of the time variables for periodic mode */ |
||
321 | nts = ticksize * 1000; |
||
322 | NULL_TIMESPEC(&actTime); |
||
120 | giacomo | 323 | |
2 | pj | 324 | /* Initialization of the general time variables */ |
325 | NULLPITSPEC(&globalCounter); |
||
326 | lastTime = 0; |
||
327 | |||
328 | if (timermode == LL_PERIODIC) { |
||
329 | event_post = periodic_event_post; |
||
330 | event_delete = periodic_event_delete; |
||
331 | } else { |
||
332 | event_post = oneshot_event_post; |
||
333 | event_delete = oneshot_event_delete; |
||
334 | } |
||
40 | pj | 335 | |
336 | /* Last but not least... */ |
||
305 | giacomo | 337 | #ifndef __APIC__ |
338 | irq_unmask(0); |
||
339 | #endif |
||
299 | giacomo | 340 | |
2 | pj | 341 | } |