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