Subversion Repositories shark

Rev

Rev 314 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
120 giacomo 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
 
22
/*      Advanced Timer Managment
23
 *      Author: Giacomo Guidi <giacomo@gandalf.sssup.it>
24
 */
25
 
26
#include <ll/i386/stdlib.h>
27
#include <ll/i386/error.h>
28
#include <ll/sys/ll/ll-data.h>
29
#include <ll/sys/ll/ll-func.h>
30
#include <ll/i386/pic.h>
299 giacomo 31
#include <ll/i386/apic.h>
305 giacomo 32
#include <ll/i386/64bit.h>
120 giacomo 33
#include <ll/sys/ll/event.h>
34
#include <ll/sys/ll/time.h>
305 giacomo 35
#include <ll/i386/advtimer.h>
120 giacomo 36
 
264 giacomo 37
#define CALIBRATE_USING_CMOS
38
 
305 giacomo 39
unsigned long long init_tsc;
40
unsigned long long * ptr_init_tsc = &init_tsc;
120 giacomo 41
 
305 giacomo 42
unsigned long long init_nsec; //Wraparound 292 years
43
unsigned long long * ptr_init_nsec = &init_nsec;
120 giacomo 44
 
305 giacomo 45
unsigned int clk_per_msec = 0;
46
unsigned int apic_clk_per_msec = 0;
47
unsigned int apic_set_limit = 0;
194 giacomo 48
 
120 giacomo 49
unsigned char save_CMOS_regA;
50
unsigned char save_CMOS_regB;
51
 
52
#ifdef CONFIG_MELAN
53
#  define CLOCK_TICK_RATE 1189200 /* AMD Elan has different frequency! */
54
#else
249 giacomo 55
#  define CLOCK_TICK_RATE 1193182 /* Underlying HZ */
120 giacomo 56
#endif
57
 
248 giacomo 58
#define COUNTER_END 100
120 giacomo 59
 
245 giacomo 60
#define barrier() __asm__ __volatile__("" ::: "memory");
61
 
120 giacomo 62
//TSC Calibration (idea from the linux kernel code)
63
void ll_calibrate_tsc(void)
64
{
65
 
305 giacomo 66
        unsigned long long start;
67
        unsigned long long end;
68
        unsigned long long dtsc;
120 giacomo 69
 
305 giacomo 70
        unsigned int start_8253, end_8253, delta_8253;
120 giacomo 71
 
248 giacomo 72
        outp(0x61, (inp(0x61) & ~0x02) | 0x01);
120 giacomo 73
 
74
        outp(0x43,0xB0);                        /* binary, mode 0, LSB/MSB, Ch 2 */
238 giacomo 75
        outp(0x42,0xFF);                        /* LSB of count */
76
        outp(0x42,0xFF);                        /* MSB of count */
242 giacomo 77
 
245 giacomo 78
        barrier();
79
        rdtscll(start);
80
        barrier();
243 giacomo 81
        outp(0x43,0x00);
82
        start_8253 = inp(0x42);
83
        start_8253 |= inp(0x42) << 8;
245 giacomo 84
        barrier();
264 giacomo 85
        rdtscll(start);
86
        barrier();
243 giacomo 87
 
88
        do {
89
 
90
            outp(0x43,0x00);
91
            end_8253 = inp(0x42);
92
            end_8253 |= inp(0x42) << 8;
93
 
94
        } while (end_8253 > COUNTER_END);
95
 
245 giacomo 96
        barrier();
97
        rdtscll(end);
98
        barrier();
243 giacomo 99
        outp(0x43,0x00);
100
        end_8253 = inp(0x42);
101
        end_8253 |= inp(0x42) << 8;
245 giacomo 102
        barrier();
264 giacomo 103
        rdtscll(end);
104
        barrier();
243 giacomo 105
 
106
        //Delta TSC
244 giacomo 107
        dtsc = end - start;
243 giacomo 108
 
109
        //Delta PIT
264 giacomo 110
        delta_8253 = start_8253 - end_8253;
243 giacomo 111
 
242 giacomo 112
        if (delta_8253 > 0x20000) {
120 giacomo 113
                message("Error calculating Delta PIT\n");
114
                ll_abort(10);
115
        }
116
 
305 giacomo 117
        message("Delta TSC               = %10d\n",(int)dtsc);
120 giacomo 118
 
305 giacomo 119
        message("Delta PIT               = %10d\n",delta_8253);
120 giacomo 120
 
252 giacomo 121
        clk_per_msec = dtsc * CLOCK_TICK_RATE / delta_8253 / 1000;
120 giacomo 122
 
305 giacomo 123
        message("Calibrated Clk_per_msec = %10d\n",clk_per_msec);
120 giacomo 124
 
125
}
126
 
264 giacomo 127
#define CMOS_INIT  0
128
#define CMOS_BEGIN 1
129
#define CMOS_START 2
130
#define CMOS_END   3
131
 
132
int cmos_calibrate_status = CMOS_INIT;
305 giacomo 133
unsigned long long irq8_start;
134
unsigned long long irq8_end;
264 giacomo 135
 
299 giacomo 136
void calibrate_tsc_IRQ8(void *p)
264 giacomo 137
{
138
 
139
  unsigned char set;
140
 
141
  CMOS_READ(0x0C,set);
142
 
143
  barrier();
144
  rdtscll(irq8_end);
145
  barrier();
146
 
147
  if (cmos_calibrate_status == CMOS_START) {
148
    cmos_calibrate_status = CMOS_END;
149
  }
150
 
151
  if (cmos_calibrate_status == CMOS_BEGIN) {
152
    irq8_start = irq8_end;
153
    cmos_calibrate_status = CMOS_START;
154
  }
155
 
156
  if (cmos_calibrate_status == CMOS_INIT) {
157
    cmos_calibrate_status = CMOS_BEGIN;
158
  }
159
 
160
}
161
 
162
//TSC Calibration using RTC
163
void ll_calibrate_tsc_cmos(void)
164
{
165
 
305 giacomo 166
  unsigned long long dtsc;
264 giacomo 167
 
299 giacomo 168
  irq_bind(8, calibrate_tsc_IRQ8, INT_FORCE);
264 giacomo 169
 
170
  CMOS_READ(0x0A,save_CMOS_regA);
171
  CMOS_READ(0x0B,save_CMOS_regB);
172
 
173
  CMOS_WRITE(0x0A,0x2F); // Set 2 Hz Periodic Interrupt
174
  CMOS_WRITE(0x0B,0x42); // Enable Interrupt
175
 
176
  irq_unmask(8);
317 giacomo 177
 
264 giacomo 178
  sti();
179
 
180
  while (cmos_calibrate_status != CMOS_END) {
181
    barrier();
182
  }
317 giacomo 183
 
184
  cli();
264 giacomo 185
 
186
  dtsc = irq8_end - irq8_start;
187
 
188
  clk_per_msec = dtsc / 500;
189
 
305 giacomo 190
  message("Calibrated CPU Clk/msec  = %10d\n",clk_per_msec);
264 giacomo 191
 
192
  irq_mask(8);
193
 
194
  CMOS_WRITE(0x0A,save_CMOS_regA);
195
  CMOS_WRITE(0x0B,save_CMOS_regB);
196
 
197
}
198
 
299 giacomo 199
int apic_get_maxlvt(void)
200
{
201
        unsigned int v, ver, maxlvt;
202
 
203
        v = apic_read(APIC_LVR);
204
        ver = GET_APIC_VERSION(v);
205
        /* 82489DXs do not report # of LVT entries. */
206
        maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
207
        return maxlvt;
208
}
209
 
304 giacomo 210
/* Clear local APIC, from Linux kernel */
299 giacomo 211
void clear_local_APIC(void)
212
{
213
        int maxlvt;
214
        unsigned long v;
215
 
216
        maxlvt = apic_get_maxlvt();
217
 
218
        /*
219
         * Masking an LVT entry on a P6 can trigger a local APIC error
220
         * if the vector is zero. Mask LVTERR first to prevent this.
221
         */
222
        if (maxlvt >= 3) {
223
                v = 0xFF; /* any non-zero vector will do */
224
                apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
225
        }
226
        /*
227
         * Careful: we have to set masks only first to deassert
228
         * any level-triggered sources.
229
         */
230
        v = apic_read(APIC_LVTT);
231
        apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
232
        v = apic_read(APIC_LVT0);
233
        apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
234
        v = apic_read(APIC_LVT1);
235
        apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
236
        if (maxlvt >= 4) {
237
                v = apic_read(APIC_LVTPC);
238
                apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
239
        }
240
 
241
        /*
242
         * Clean APIC state for other OSs:
243
         */
244
        apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
245
        apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
246
        apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
247
        if (maxlvt >= 3)
248
                apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
249
        if (maxlvt >= 4)
250
                apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
251
        v = GET_APIC_VERSION(apic_read(APIC_LVR));
252
        if (APIC_INTEGRATED(v)) {       /* !82489DX */
253
                if (maxlvt > 3)
254
                        apic_write(APIC_ESR, 0);
255
                apic_read(APIC_ESR);
256
        }
257
}
258
 
259
void disable_local_APIC(void)
260
{
261
        unsigned long value;
262
 
263
        clear_local_APIC();
264
 
265
        /*
266
         * Disable APIC (implies clearing of registers
267
         * for 82489DX!).
268
         */
269
        value = apic_read(APIC_SPIV);
270
        value &= ~APIC_SPIV_APIC_ENABLED;
271
        apic_write_around(APIC_SPIV, value);
272
}
273
 
274
#define SPURIOUS_APIC_VECTOR 0xFF
275
 
276
/*
302 giacomo 277
 * Setup the local APIC, minimal code to run P6 APIC
299 giacomo 278
 */
279
void setup_local_APIC (void)
280
{
301 giacomo 281
        unsigned long value;
299 giacomo 282
 
283
        /* Pound the ESR really hard over the head with a big hammer - mbligh */
284
 
285
        apic_write(APIC_ESR, 0);
286
        apic_write(APIC_ESR, 0);
287
        apic_write(APIC_ESR, 0);
288
        apic_write(APIC_ESR, 0);
289
 
301 giacomo 290
        value = APIC_SPIV_FOCUS_DISABLED | APIC_SPIV_APIC_ENABLED | SPURIOUS_APIC_VECTOR;
291
        apic_write_around(APIC_SPIV, value);
299 giacomo 292
 
301 giacomo 293
        value = APIC_DM_EXTINT | APIC_LVT_LEVEL_TRIGGER;
294
        apic_write_around(APIC_LVT0, value);
299 giacomo 295
 
301 giacomo 296
        value = APIC_DM_NMI;
297
        apic_write_around(APIC_LVT1, value);
299 giacomo 298
 
301 giacomo 299
        apic_write(APIC_ESR, 0);
299 giacomo 300
 
301
}
302
 
303
void disable_APIC_timer(void)
304
{
305
        unsigned long v;
306
 
307
        v = apic_read(APIC_LVTT);
308
        apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
309
 
310
}
311
 
312
void enable_APIC_timer(void)
313
{
314
        unsigned long v;
315
 
316
        v = apic_read(APIC_LVTT);
317
        apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
318
 
319
}
320
 
304 giacomo 321
#define LOCAL_TIMER_VECTOR 0x39
302 giacomo 322
 
303 giacomo 323
/* Set APIC Timer... from Linux kernel */
324
void setup_APIC_timer()
299 giacomo 325
{
314 giacomo 326
        unsigned int lvtt1_value;
299 giacomo 327
 
328
        lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
329
                        APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
330
        apic_write_around(APIC_LVTT, lvtt1_value);
331
 
332
        /*
333
         * Divide PICLK by 1
334
         */
312 giacomo 335
        apic_write_around(APIC_TDCR, APIC_TDR_DIV_1);
303 giacomo 336
 
314 giacomo 337
        apic_write_around(APIC_TMICT, MAX_DWORD);
303 giacomo 338
 
339
        disable_APIC_timer();                                                                                                                            
299 giacomo 340
}
341
 
342
#define APIC_LIMIT 0xFF000000
303 giacomo 343
#define APIC_SET_LIMIT 10
299 giacomo 344
 
345
void ll_calibrate_apic(void)
346
{
347
 
348
  unsigned int apic_start = 0, apic_end = 0, dapic;
305 giacomo 349
  unsigned long long tsc_start = 0, tsc_end = 0, dtsc;
299 giacomo 350
  unsigned int tmp_value;
351
 
312 giacomo 352
  tmp_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) | LOCAL_TIMER_VECTOR;
353
  apic_write_around(APIC_LVTT, tmp_value);
299 giacomo 354
 
312 giacomo 355
  apic_write_around(APIC_TDCR, APIC_TDR_DIV_1);
356
 
299 giacomo 357
  apic_write(APIC_TMICT, MAX_DWORD);
358
 
312 giacomo 359
  enable_APIC_timer();
360
 
299 giacomo 361
  barrier();
362
  rdtscll(tsc_start);
363
  barrier();
364
  apic_start = apic_read(APIC_TMCCT);
365
  barrier();            
366
 
367
  while (apic_read(APIC_TMCCT) > APIC_LIMIT) {
368
    barrier();
369
    rdtscll(tsc_end);
370
  }
371
 
372
  barrier();
373
  rdtscll(tsc_end);
374
  barrier();
375
  apic_end = apic_read(APIC_TMCCT);
376
  barrier();    
377
 
312 giacomo 378
  disable_APIC_timer();
379
 
299 giacomo 380
  dtsc = tsc_end - tsc_start;
381
  dapic = apic_start - apic_end;
382
 
305 giacomo 383
  apic_clk_per_msec = (unsigned long long)(clk_per_msec) * (unsigned long long)(dapic) / dtsc;
312 giacomo 384
  apic_set_limit = ((apic_clk_per_msec / 100) != 0) ? (apic_clk_per_msec/100) : APIC_SET_LIMIT;  
303 giacomo 385
 
305 giacomo 386
  message("Calibrated APIC Clk/msec = %10d\n",apic_clk_per_msec);
299 giacomo 387
 
388
}
389
 
120 giacomo 390
void ll_init_advtimer()
391
{
305 giacomo 392
    #ifdef __APIC__
393
      unsigned long msr_low_orig, tmp;
394
    #endif
120 giacomo 395
 
305 giacomo 396
    #ifdef __TSC__
120 giacomo 397
 
264 giacomo 398
        #ifdef CALIBRATE_USING_CMOS
399
          ll_calibrate_tsc_cmos();
400
        #else
401
          ll_calibrate_tsc();
402
        #endif  
403
 
120 giacomo 404
        rdtscll(init_tsc); // Read start TSC
405
        init_nsec = 0;
406
 
305 giacomo 407
        #ifdef __APIC__
299 giacomo 408
 
409
          rdmsr(APIC_BASE_MSR, msr_low_orig, tmp);
410
          wrmsr(APIC_BASE_MSR, msr_low_orig|(1<<11), 0);
411
 
301 giacomo 412
          clear_local_APIC();
299 giacomo 413
 
301 giacomo 414
          ll_calibrate_apic();
415
 
299 giacomo 416
          setup_local_APIC();
303 giacomo 417
 
418
          setup_APIC_timer();
299 giacomo 419
 
305 giacomo 420
        #endif
299 giacomo 421
 
305 giacomo 422
    #endif
120 giacomo 423
 
424
}
425
 
301 giacomo 426
void ll_restore_adv()
120 giacomo 427
{
302 giacomo 428
        /* Disable APIC */
305 giacomo 429
        #ifdef __APIC__
430
                unsigned int msr_low_orig, tmp;
301 giacomo 431
 
317 giacomo 432
                cli();
301 giacomo 433
 
434
                disable_APIC_timer();
435
 
436
                rdmsr(APIC_BASE_MSR, msr_low_orig, tmp);
437
                wrmsr(APIC_BASE_MSR, msr_low_orig&~(1<<11), 0);
438
 
439
                sti();
440
 
305 giacomo 441
        #endif
301 giacomo 442
 
120 giacomo 443
}