Subversion Repositories shark

Rev

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