Rev 305 | Rev 333 | Go to most recent revision | 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_nsec; |
||
157 | extern unsigned long long *ptr_init_tsc; |
||
158 | |||
159 | if (clk_per_msec == 0) { |
||
160 | NULL_TIMESPEC(tspec); |
||
161 | return; |
||
162 | } |
||
163 | |||
330 | giacomo | 164 | __asm__("xchgl %%eax,%%edx\n\t" |
305 | giacomo | 165 | "rdtsc\n\t" |
166 | "subl (%%edi),%%eax\n\t" |
||
167 | "sbbl 4(%%edi),%%edx\n\t" |
||
168 | "movl %%edx,%%ecx\n\t" |
||
169 | "movl $1000000,%%edi\n\t" |
||
170 | "mull %%edi\n\t" |
||
330 | giacomo | 171 | "movl %%eax,%%esi\n\t" |
305 | giacomo | 172 | "movl %%ecx,%%eax\n\t" |
173 | "movl %%edx,%%ecx\n\t" |
||
174 | "mull %%edi\n\t" |
||
175 | "addl %%ecx,%%eax\n\t" |
||
176 | "adcl $0,%%edx\n\t" |
||
177 | "divl %%ebx\n\t" |
||
178 | "movl %%eax,%%ecx\n\t" |
||
330 | giacomo | 179 | "movl %%esi,%%eax\n\t" |
305 | giacomo | 180 | "divl %%ebx\n\t" |
181 | "movl %%ecx,%%edx\n\t" |
||
182 | "movl $1000000000,%%edi\n\t" |
||
183 | "divl %%edi\n\t" |
||
184 | : "=a" (tspec->tv_sec), "=d" (tspec->tv_nsec) |
||
330 | giacomo | 185 | : "D" (ptr_init_tsc), "b" (clk_per_msec) |
186 | : "ecx", "esi"); |
||
305 | giacomo | 187 | |
188 | } |
||
189 | |||
299 | giacomo | 190 | #define rdmsr(msr,val1,val2) \ |
191 | __asm__ __volatile__("rdmsr" \ |
||
192 | : "=a" (val1), "=d" (val2) \ |
||
193 | : "c" (msr)) |
||
194 | |||
195 | #define wrmsr(msr,val1,val2) \ |
||
196 | __asm__ __volatile__("wrmsr" \ |
||
197 | : /* no outputs */ \ |
||
198 | : "c" (msr), "a" (val1), "d" (val2)) |
||
199 | |||
120 | giacomo | 200 | /* RTC */ |
201 | |||
202 | #define RTC_PORT(x) (0x70 + (x)) |
||
203 | |||
204 | #define CMOS_READ(addr,val) \ |
||
205 | { \ |
||
206 | outp(RTC_PORT(0),(addr)); \ |
||
207 | val = inp(RTC_PORT(1)); \ |
||
208 | } |
||
209 | |||
210 | #define CMOS_WRITE(addr,val) \ |
||
211 | { \ |
||
212 | outp(RTC_PORT(0),(addr)); \ |
||
213 | outp(RTC_PORT(1),(val)); \ |
||
214 | } |
||
215 | |||
216 | #define RTC_IRQ 8 |
||
217 | |||
131 | giacomo | 218 | void ll_init_advtimer(void); |
301 | giacomo | 219 | void ll_restore_adv(void); |
131 | giacomo | 220 | |
120 | giacomo | 221 | END_DEF |
222 | #endif |