Subversion Repositories shark

Rev

Rev 299 | Rev 303 | 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 */
232
        } else
233
          setup_APIC_LVTT(0xFFFFFFFF);
2 pj 234
    } else {
235
        t = l->tick;
299 giacomo 236
 
237
        /* Translate the tick value in usec into a suitable time constant   */
2 pj 238
        /* for 8254 timer chip; the chip is driven with a 1.19718 MHz       */
239
        /* frequency; then the effective frequency is given by the base     */
240
        /* frequency divided for the time constant; the tick is the inverse */
241
        /* of this effective frequency (in usec!)                           */
242
        /* Time-Constant = f_base (MHz) * tick (usec)                       */
243
        /* If T-C == 0 -> T-C = 65536 (Max available)                       */
244
        ticksize = t;
265 giacomo 245
 
299 giacomo 246
        if (!use_apic) {
247
 
248
           t = (signed long long)(t) * 1193182 / 1000000;
2 pj 249
 
299 giacomo 250
           /* Only for security! This should cause timer overrun */
251
           /* While 0 would set maximum period on timer          */
252
          if (t == 0)
253
              t = 1;
254
          pit_time_const = (WORD) (t & 0xFFFF);
255
          /* Mode: Binary/Mode 2/16 bit Time_const/Counter 0 */
256
          pit_init(0, TMR_MD2, t);      /* Timer 0, Mode 2, Time constant t */
257
 
258
        } else {
2 pj 259
 
299 giacomo 260
          apic_clk = (signed long long)(t) * apic_clk_per_msec / 1000;
261
 
262
          setup_APIC_LVTT(apic_clk);
263
 
264
        }
2 pj 265
    }
266
    timermode = l->mode;
301 giacomo 267
 
268
    if (!use_apic) {
299 giacomo 269
      if (ll_arch.x86.cpu > 4) {
301 giacomo 270
        /* Timer1: mode 0, time const 0... */
271
        pit_init(1, TMR_MD0, 0);
272
        frc = 1;
299 giacomo 273
      } else {
301 giacomo 274
        frc = 2;
275
        pit_init(2, TMR_MD0, 0);
276
        outp(0x61, 3);
299 giacomo 277
      }
2 pj 278
    }
279
 
301 giacomo 280
    mask = ll_in(0x21);
281
    mask &= 0xFE;               /* 0xFE = ~0x01 */
282
    ll_out(0x21, mask);
283
 
2 pj 284
    /* Init the event list... */
285
    for (i = 0; i < MAX_EVENT; i++) {
286
        if (i < MAX_EVENT - 1) {
287
            eventlist[i].next = &(eventlist[i + 1]);
288
        }
289
        eventlist[i].index = i;
290
    }
291
    eventlist[MAX_EVENT - 1].next = NULL;
292
    freeevents = &(eventlist[0]);
293
 
294
    evt_prol = NULL;
295
    evt_epil = NULL;
296
 
297
    /* Initialization of the time variables for periodic mode */
298
    nts = ticksize * 1000;
299
    NULL_TIMESPEC(&actTime);
120 giacomo 300
 
2 pj 301
    /* Initialization of the general time variables */
302
    NULLPITSPEC(&globalCounter);
303
    lastTime = 0;
304
 
305
    if (timermode == LL_PERIODIC) {
306
        event_post = periodic_event_post;
307
        event_delete = periodic_event_delete;
308
    } else {
309
        event_post = oneshot_event_post;
310
        event_delete = oneshot_event_delete;
311
    }
40 pj 312
 
313
    /* Last but not least... */
299 giacomo 314
    if (!use_apic) irq_unmask(0);
315
 
2 pj 316
}