Subversion Repositories shark

Rev

Rev 330 | 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
23
 *      Date: 8.4.2003
24
 *      Author: Giacomo Guidi <giacomo@gandalf.sssup.it>
25
 *
26
 */
27
 
28
#ifndef __ADVTIMER_H__
29
#define __ADVTIMER_H__
30
 
31
#include <ll/i386/defs.h>
32
BEGIN_DEF
33
 
305 giacomo 34
#include <ll/sys/ll/time.h>
35
 
120 giacomo 36
/* TSC */
37
 
38
#define rdtsc(low,high) \
239 giacomo 39
     __asm__ __volatile__("xorl %%eax,%%eax\n\t" \
242 giacomo 40
                          "cpuid\n\t"            \
41
                          "rdtsc\n\t"            \
42
                          "movl %%eax,%%esi\n\t" \
43
                          "movl %%edx,%%edi\n\t" \
44
                          "xorl %%eax,%%eax\n\t" \
45
                          "cpuid"                \
46
                          : "=S" (low), "=D" (high) \
239 giacomo 47
                          :: "ebx", "ecx")
120 giacomo 48
 
49
#define rdtscll(val) \
239 giacomo 50
     __asm__ __volatile__("xorl %%eax,%%eax\n\t" \
242 giacomo 51
                          "cpuid\n\t"            \
52
                          "rdtsc\n\t"            \
53
                          "pushl %%eax\n\t"      \
54
                          "pushl %%edx\n\t"      \
55
                          "xorl %%eax,%%eax\n\t" \
56
                          "cpuid\n\t"            \
57
                          "popl %%edx\n\t"       \
58
                          "popl %%eax\n\t"       \
59
                          : "=A" (val)           \
239 giacomo 60
                          :: "ebx","ecx")
120 giacomo 61
 
330 giacomo 62
#ifdef __O1000__
63
  #define ll_read_timespec ll_read_timespec_1000
64
#else 
65
  #ifdef __02000__
66
    #define ll_read_timespec ll_read_timespec_2000
67
  #else
68
    #ifdef __O4000__
69
      #define ll_read_timespec ll_read_timespec_4000
70
    #else
71
      #define ll_read_timespec ll_read_timespec_slow
72
    #endif
73
  #endif
74
#endif
75
 
76
//Low level time read function: Optimized for CPU < 1 GHz
77
extern __inline__ void ll_read_timespec_1000(struct timespec *tspec)
78
{
79
    extern unsigned int clk_opt_1,clk_opt_2;
80
    extern unsigned long long *ptr_init_tsc;
81
 
82
   if (clk_opt_1 == 0) {
83
            NULL_TIMESPEC(tspec);
84
            return;
85
    }
86
 
87
    __asm__("xchgl %%eax,%%edx\n\t"
88
            "rdtsc\n\t"
89
            "subl (%%edi),%%eax\n\t"
90
            "sbbl 4(%%edi),%%edx\n\t"
91
            "divl %%ebx\n\t"
92
            "movl %%eax,%%ebx\n\t"
93
            "xorl %%eax,%%eax\n\t"
94
            "divl %%ecx\n\t"
95
            : "=a" (tspec->tv_nsec), "=b" (tspec->tv_sec)
96
            : "D" (ptr_init_tsc) , "b" (clk_opt_1), "c" (clk_opt_2)
97
            : "edx" );            
98
 
99
}
100
 
101
//Low level time read function: Optimized for CPU < 2 GHz
102
extern __inline__ void ll_read_timespec_2000(struct timespec *tspec)
103
{
104
    extern unsigned int clk_opt_1,clk_opt_3;
105
    extern unsigned long long *ptr_init_tsc;
106
 
107
    if (clk_opt_1 == 0) {
108
            NULL_TIMESPEC(tspec);
109
            return;
110
    }
111
 
112
    __asm__("xchgl %%eax,%%edx\n\t"
113
            "rdtsc\n\t"
114
            "subl (%%edi),%%eax\n\t"
115
            "sbbl 4(%%edi),%%edx\n\t"
116
            "divl %%ebx\n\t"
117
            "movl %%eax,%%ebx\n\t"
118
            "shrl %%edx\n\t"
119
            "divl %%ecx\n\t"
120
            : "=a" (tspec->tv_nsec), "=b" (tspec->tv_sec)
121
            : "D" (ptr_init_tsc) , "b" (clk_opt_1), "c" (clk_opt_3)
122
            : "edx" );
123
 
124
}
125
 
126
//Low level time read function: Optimized for CPU < 4 GHz
127
extern __inline__ void ll_read_timespec_4000(struct timespec *tspec)
128
{
129
    extern unsigned int clk_opt_1,clk_opt_4;
130
    extern unsigned long long *ptr_init_tsc;
131
 
132
    if (clk_opt_1 == 0) {
133
            NULL_TIMESPEC(tspec);
134
            return;
135
    }
136
 
137
    __asm__("xchgl %%eax,%%edx\n\t"
138
            "rdtsc\n\t"
139
            "subl (%%edi),%%eax\n\t"
140
            "sbbl 4(%%edi),%%edx\n\t"
141
            "divl %%ebx\n\t"
142
            "movl %%eax,%%ebx\n\t"
143
            "shrl %%edx\n\t"
144
            "shrl %%edx\n\t"
145
            "divl %%ecx\n\t"
146
            : "=a" (tspec->tv_nsec), "=b" (tspec->tv_sec)
147
            : "D" (ptr_init_tsc) , "b" (clk_opt_1), "c" (clk_opt_4)
148
            : "edx" );
149
 
150
}
151
 
305 giacomo 152
//Low level time read function
330 giacomo 153
extern __inline__ void ll_read_timespec_slow(struct timespec *tspec)
305 giacomo 154
{
155
    extern unsigned int clk_per_msec;
156
    extern unsigned long long *ptr_init_tsc;
157
 
158
    if (clk_per_msec == 0) {
159
            NULL_TIMESPEC(tspec);
160
            return;
161
    }
162
 
330 giacomo 163
    __asm__("xchgl %%eax,%%edx\n\t"
305 giacomo 164
            "rdtsc\n\t"
165
            "subl (%%edi),%%eax\n\t"
166
            "sbbl 4(%%edi),%%edx\n\t"
167
            "movl %%edx,%%ecx\n\t"
168
            "movl $1000000,%%edi\n\t"
169
            "mull %%edi\n\t"
330 giacomo 170
            "movl %%eax,%%esi\n\t"
305 giacomo 171
            "movl %%ecx,%%eax\n\t"
172
            "movl %%edx,%%ecx\n\t"
173
            "mull %%edi\n\t"
174
            "addl %%ecx,%%eax\n\t"
175
            "adcl $0,%%edx\n\t"
176
            "divl %%ebx\n\t"
177
            "movl %%eax,%%ecx\n\t"
330 giacomo 178
            "movl %%esi,%%eax\n\t"
305 giacomo 179
            "divl %%ebx\n\t"
180
            "movl %%ecx,%%edx\n\t"
181
            "movl $1000000000,%%edi\n\t"
182
            "divl %%edi\n\t"       
183
            : "=a" (tspec->tv_sec), "=d" (tspec->tv_nsec)
330 giacomo 184
            : "D" (ptr_init_tsc), "b" (clk_per_msec)
185
            : "ecx", "esi");
305 giacomo 186
 
187
}
188
 
299 giacomo 189
#define rdmsr(msr,val1,val2) \
190
     __asm__ __volatile__("rdmsr" \
191
                          : "=a" (val1), "=d" (val2) \
192
                          : "c" (msr))
193
 
194
#define wrmsr(msr,val1,val2) \
195
     __asm__ __volatile__("wrmsr" \
196
                          : /* no outputs */ \
197
                          : "c" (msr), "a" (val1), "d" (val2))
198
 
120 giacomo 199
/* RTC */
200
 
201
#define RTC_PORT(x)     (0x70 + (x))
202
 
203
#define CMOS_READ(addr,val)     \
204
{                               \
205
outp(RTC_PORT(0),(addr));       \
206
val = inp(RTC_PORT(1));         \
207
}
208
 
209
#define CMOS_WRITE(addr,val)    \
210
{                               \
211
outp(RTC_PORT(0),(addr));       \
212
outp(RTC_PORT(1),(val));        \
213
}
214
 
215
#define RTC_IRQ 8
216
 
131 giacomo 217
void ll_init_advtimer(void);
301 giacomo 218
void ll_restore_adv(void);
131 giacomo 219
 
120 giacomo 220
END_DEF
221
#endif