kernel -- CLFLUSH support
[dragonfly.git] / sys / platform / pc32 / i386 / initcpu.c
1 /*
2  * Copyright (c) KATO Takenori, 1997, 1998.
3  * 
4  * All rights reserved.  Unpublished rights reserved under the copyright
5  * laws of Japan.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer as
13  *    the first lines of this file unmodified.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/i386/initcpu.c,v 1.19.2.9 2003/04/05 13:47:19 dwmalone Exp $
30  */
31
32 #include "opt_cpu.h"
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/sysctl.h>
38
39 #include <machine/cputypes.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42
43 void initializecpu(void);
44 #if defined(I586_CPU) && defined(CPU_WT_ALLOC)
45 void    enable_K5_wt_alloc(void);
46 void    enable_K6_wt_alloc(void);
47 void    enable_K6_2_wt_alloc(void);
48 #endif
49
50 #ifdef I486_CPU
51 static void init_5x86(void);
52 static void init_bluelightning(void);
53 static void init_486dlc(void);
54 static void init_cy486dx(void);
55 #ifdef CPU_I486_ON_386
56 static void init_i486_on_386(void);
57 #endif
58 static void init_6x86(void);
59 #endif /* I486_CPU */
60
61 #ifdef I686_CPU
62 static void     init_6x86MX(void);
63 static void     init_ppro(void);
64 static void     init_mendocino(void);
65 #endif
66
67 static int      hw_instruction_sse;
68 SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD,
69     &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU");
70
71 /* Must *NOT* be BSS or locore will bzero these after setting them */
72 int     cpu = 0;                /* Are we 386, 386sx, 486, etc? */
73 u_int   cpu_feature = 0;        /* Feature flags */
74 u_int   cpu_feature2 = 0;       /* Feature flags */
75 u_int   amd_feature = 0;        /* AMD feature flags */
76 u_int   amd_feature2 = 0;       /* AMD feature flags */
77 u_int   amd_pminfo = 0;         /* AMD advanced power management info */
78 u_int   via_feature_rng = 0;    /* VIA RNG features */
79 u_int   via_feature_xcrypt = 0; /* VIA ACE features */
80 u_int   cpu_high = 0;           /* Highest arg to CPUID */
81 u_int   cpu_id = 0;             /* Stepping ID */
82 u_int   cpu_procinfo = 0;       /* HyperThreading Info / Brand Index / CLFUSH */
83 u_int   cpu_procinfo2 = 0;      /* Multicore info */
84 char    cpu_vendor[20] = "";    /* CPU Origin code */
85 u_int   cpu_vendor_id = 0;      /* CPU vendor ID */
86 u_int   cpu_clflush_line_size = 32;     /* Default CLFLUSH line size */
87
88 /*
89  * -1: automatic (enable on h/w, disable on VMs)
90  * 0: disable
91  * 1: enable (where available)
92  */
93 static int hw_clflush_enable = -1;
94
95 SYSCTL_INT(_hw, OID_AUTO, clflush_enable, CTLFLAG_RD, &hw_clflush_enable, 0,
96            "");
97
98
99 SYSCTL_UINT(_hw, OID_AUTO, via_feature_rng, CTLFLAG_RD,
100         &via_feature_rng, 0, "VIA C3/C7 RNG feature available in CPU");
101 SYSCTL_UINT(_hw, OID_AUTO, via_feature_xcrypt, CTLFLAG_RD,
102         &via_feature_xcrypt, 0, "VIA C3/C7 xcrypt feature available in CPU");
103
104 #ifndef CPU_DISABLE_SSE
105 u_int   cpu_fxsr;               /* SSE enabled */
106 #endif
107
108 #ifdef I486_CPU
109 /*
110  * IBM Blue Lightning
111  */
112 static void
113 init_bluelightning(void)
114 {
115         u_long  eflags;
116
117         eflags = read_eflags();
118         cpu_disable_intr();
119
120         load_cr0(rcr0() | CR0_CD | CR0_NW);
121         invd();
122
123 #ifdef CPU_BLUELIGHTNING_FPU_OP_CACHE
124         wrmsr(0x1000, 0x9c92LL);        /* FP operand can be cacheable on Cyrix FPU */
125 #else
126         wrmsr(0x1000, 0x1c92LL);        /* Intel FPU */
127 #endif
128         /* Enables 13MB and 0-640KB cache. */
129         wrmsr(0x1001, (0xd0LL << 32) | 0x3ff);
130 #ifdef CPU_BLUELIGHTNING_3X
131         wrmsr(0x1002, 0x04000000LL);    /* Enables triple-clock mode. */
132 #else
133         wrmsr(0x1002, 0x03000000LL);    /* Enables double-clock mode. */
134 #endif
135
136         /* Enable caching in CR0. */
137         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
138         invd();
139         write_eflags(eflags);
140 }
141
142 /*
143  * Cyrix 486SLC/DLC/SR/DR series
144  */
145 static void
146 init_486dlc(void)
147 {
148         u_long  eflags;
149         u_char  ccr0;
150
151         eflags = read_eflags();
152         cpu_disable_intr();
153         invd();
154
155         ccr0 = read_cyrix_reg(CCR0);
156 #ifndef CYRIX_CACHE_WORKS
157         ccr0 |= CCR0_NC1 | CCR0_BARB;
158         write_cyrix_reg(CCR0, ccr0);
159         invd();
160 #else
161         ccr0 &= ~CCR0_NC0;
162 #ifndef CYRIX_CACHE_REALLY_WORKS
163         ccr0 |= CCR0_NC1 | CCR0_BARB;
164 #else
165         ccr0 |= CCR0_NC1;
166 #endif
167 #ifdef CPU_DIRECT_MAPPED_CACHE
168         ccr0 |= CCR0_CO;                        /* Direct mapped mode. */
169 #endif
170         write_cyrix_reg(CCR0, ccr0);
171
172         /* Clear non-cacheable region. */
173         write_cyrix_reg(NCR1+2, NCR_SIZE_0K);
174         write_cyrix_reg(NCR2+2, NCR_SIZE_0K);
175         write_cyrix_reg(NCR3+2, NCR_SIZE_0K);
176         write_cyrix_reg(NCR4+2, NCR_SIZE_0K);
177
178         write_cyrix_reg(0, 0);  /* dummy write */
179
180         /* Enable caching in CR0. */
181         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
182         invd();
183 #endif /* !CYRIX_CACHE_WORKS */
184         write_eflags(eflags);
185 }
186
187
188 /*
189  * Cyrix 486S/DX series
190  */
191 static void
192 init_cy486dx(void)
193 {
194         u_long  eflags;
195         u_char  ccr2;
196
197         eflags = read_eflags();
198         cpu_disable_intr();
199         invd();
200
201         ccr2 = read_cyrix_reg(CCR2);
202 #ifdef CPU_SUSP_HLT
203         ccr2 |= CCR2_SUSP_HLT;
204 #endif
205
206         write_cyrix_reg(CCR2, ccr2);
207         write_eflags(eflags);
208 }
209
210
211 /*
212  * Cyrix 5x86
213  */
214 static void
215 init_5x86(void)
216 {
217         u_long  eflags;
218         u_char  ccr2, ccr3, ccr4, pcr0;
219
220         eflags = read_eflags();
221         cpu_disable_intr();
222
223         load_cr0(rcr0() | CR0_CD | CR0_NW);
224         wbinvd();
225
226         read_cyrix_reg(CCR3);           /* dummy */
227
228         /* Initialize CCR2. */
229         ccr2 = read_cyrix_reg(CCR2);
230         ccr2 |= CCR2_WB;
231 #ifdef CPU_SUSP_HLT
232         ccr2 |= CCR2_SUSP_HLT;
233 #else
234         ccr2 &= ~CCR2_SUSP_HLT;
235 #endif
236         ccr2 |= CCR2_WT1;
237         write_cyrix_reg(CCR2, ccr2);
238
239         /* Initialize CCR4. */
240         ccr3 = read_cyrix_reg(CCR3);
241         write_cyrix_reg(CCR3, CCR3_MAPEN0);
242
243         ccr4 = read_cyrix_reg(CCR4);
244         ccr4 |= CCR4_DTE;
245         ccr4 |= CCR4_MEM;
246 #ifdef CPU_FASTER_5X86_FPU
247         ccr4 |= CCR4_FASTFPE;
248 #else
249         ccr4 &= ~CCR4_FASTFPE;
250 #endif
251         ccr4 &= ~CCR4_IOMASK;
252         /********************************************************************
253          * WARNING: The "BIOS Writers Guide" mentions that I/O recovery time
254          * should be 0 for errata fix.
255          ********************************************************************/
256 #ifdef CPU_IORT
257         ccr4 |= CPU_IORT & CCR4_IOMASK;
258 #endif
259         write_cyrix_reg(CCR4, ccr4);
260
261         /* Initialize PCR0. */
262         /****************************************************************
263          * WARNING: RSTK_EN and LOOP_EN could make your system unstable.
264          * BTB_EN might make your system unstable.
265          ****************************************************************/
266         pcr0 = read_cyrix_reg(PCR0);
267 #ifdef CPU_RSTK_EN
268         pcr0 |= PCR0_RSTK;
269 #else
270         pcr0 &= ~PCR0_RSTK;
271 #endif
272 #ifdef CPU_BTB_EN
273         pcr0 |= PCR0_BTB;
274 #else
275         pcr0 &= ~PCR0_BTB;
276 #endif
277 #ifdef CPU_LOOP_EN
278         pcr0 |= PCR0_LOOP;
279 #else
280         pcr0 &= ~PCR0_LOOP;
281 #endif
282
283         /****************************************************************
284          * WARNING: if you use a memory mapped I/O device, don't use
285          * DISABLE_5X86_LSSER option, which may reorder memory mapped
286          * I/O access.
287          * IF YOUR MOTHERBOARD HAS PCI BUS, DON'T DISABLE LSSER.
288          ****************************************************************/
289 #ifdef CPU_DISABLE_5X86_LSSER
290         pcr0 &= ~PCR0_LSSER;
291 #else
292         pcr0 |= PCR0_LSSER;
293 #endif
294         write_cyrix_reg(PCR0, pcr0);
295
296         /* Restore CCR3. */
297         write_cyrix_reg(CCR3, ccr3);
298
299         read_cyrix_reg(0x80);           /* dummy */
300
301         /* Unlock NW bit in CR0. */
302         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
303         load_cr0((rcr0() & ~CR0_CD) | CR0_NW);  /* CD = 0, NW = 1 */
304         /* Lock NW bit in CR0. */
305         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
306
307         write_eflags(eflags);
308 }
309
310 #ifdef CPU_I486_ON_386
311 /*
312  * There are i486 based upgrade products for i386 machines.
313  * In this case, BIOS doesn't enables CPU cache.
314  */
315 void
316 init_i486_on_386(void)
317 {
318         u_long  eflags;
319
320         eflags = read_eflags();
321         cpu_disable_intr();
322
323         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0, NW = 0 */
324
325         write_eflags(eflags);
326 }
327 #endif
328
329 /*
330  * Cyrix 6x86
331  *
332  * XXX - What should I do here?  Please let me know.
333  */
334 static void
335 init_6x86(void)
336 {
337         u_long  eflags;
338         u_char  ccr3, ccr4;
339
340         eflags = read_eflags();
341         cpu_disable_intr();
342
343         load_cr0(rcr0() | CR0_CD | CR0_NW);
344         wbinvd();
345
346         /* Initialize CCR0. */
347         write_cyrix_reg(CCR0, read_cyrix_reg(CCR0) | CCR0_NC1);
348
349         /* Initialize CCR1. */
350 #ifdef CPU_CYRIX_NO_LOCK
351         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) | CCR1_NO_LOCK);
352 #else
353         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) & ~CCR1_NO_LOCK);
354 #endif
355
356         /* Initialize CCR2. */
357 #ifdef CPU_SUSP_HLT
358         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_SUSP_HLT);
359 #else
360         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_SUSP_HLT);
361 #endif
362
363         ccr3 = read_cyrix_reg(CCR3);
364         write_cyrix_reg(CCR3, CCR3_MAPEN0);
365
366         /* Initialize CCR4. */
367         ccr4 = read_cyrix_reg(CCR4);
368         ccr4 |= CCR4_DTE;
369         ccr4 &= ~CCR4_IOMASK;
370 #ifdef CPU_IORT
371         write_cyrix_reg(CCR4, ccr4 | (CPU_IORT & CCR4_IOMASK));
372 #else
373         write_cyrix_reg(CCR4, ccr4 | 7);
374 #endif
375
376         /* Initialize CCR5. */
377 #ifdef CPU_WT_ALLOC
378         write_cyrix_reg(CCR5, read_cyrix_reg(CCR5) | CCR5_WT_ALLOC);
379 #endif
380
381         /* Restore CCR3. */
382         write_cyrix_reg(CCR3, ccr3);
383
384         /* Unlock NW bit in CR0. */
385         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
386
387         /*
388          * Earlier revision of the 6x86 CPU could crash the system if
389          * L1 cache is in write-back mode.
390          */
391         if ((cyrix_did & 0xff00) > 0x1600)
392                 load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
393         else {
394                 /* Revision 2.6 and lower. */
395 #ifdef CYRIX_CACHE_REALLY_WORKS
396                 load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
397 #else
398                 load_cr0((rcr0() & ~CR0_CD) | CR0_NW);  /* CD = 0 and NW = 1 */
399 #endif
400         }
401
402         /* Lock NW bit in CR0. */
403         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
404
405         write_eflags(eflags);
406 }
407 #endif /* I486_CPU */
408
409 #ifdef I686_CPU
410 /*
411  * Cyrix 6x86MX (code-named M2)
412  *
413  * XXX - What should I do here?  Please let me know.
414  */
415 static void
416 init_6x86MX(void)
417 {
418         u_long  eflags;
419         u_char  ccr3, ccr4;
420
421         eflags = read_eflags();
422         cpu_disable_intr();
423
424         load_cr0(rcr0() | CR0_CD | CR0_NW);
425         wbinvd();
426
427         /* Initialize CCR0. */
428         write_cyrix_reg(CCR0, read_cyrix_reg(CCR0) | CCR0_NC1);
429
430         /* Initialize CCR1. */
431 #ifdef CPU_CYRIX_NO_LOCK
432         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) | CCR1_NO_LOCK);
433 #else
434         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) & ~CCR1_NO_LOCK);
435 #endif
436
437         /* Initialize CCR2. */
438 #ifdef CPU_SUSP_HLT
439         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_SUSP_HLT);
440 #else
441         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_SUSP_HLT);
442 #endif
443
444         ccr3 = read_cyrix_reg(CCR3);
445         write_cyrix_reg(CCR3, CCR3_MAPEN0);
446
447         /* Initialize CCR4. */
448         ccr4 = read_cyrix_reg(CCR4);
449         ccr4 &= ~CCR4_IOMASK;
450 #ifdef CPU_IORT
451         write_cyrix_reg(CCR4, ccr4 | (CPU_IORT & CCR4_IOMASK));
452 #else
453         write_cyrix_reg(CCR4, ccr4 | 7);
454 #endif
455
456         /* Initialize CCR5. */
457 #ifdef CPU_WT_ALLOC
458         write_cyrix_reg(CCR5, read_cyrix_reg(CCR5) | CCR5_WT_ALLOC);
459 #endif
460
461         /* Restore CCR3. */
462         write_cyrix_reg(CCR3, ccr3);
463
464         /* Unlock NW bit in CR0. */
465         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
466
467         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
468
469         /* Lock NW bit in CR0. */
470         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
471
472         write_eflags(eflags);
473 }
474
475 static void
476 init_ppro(void)
477 {
478 #ifndef SMP
479         u_int64_t       apicbase;
480
481         /*
482          * Local APIC should be diabled in UP kernel.
483          */
484         apicbase = rdmsr(0x1b);
485         apicbase &= ~0x800LL;
486         wrmsr(0x1b, apicbase);
487 #endif
488 }
489
490 /*
491  * Initialize BBL_CR_CTL3 (Control register 3: used to configure the
492  * L2 cache).
493  */
494 void
495 init_mendocino(void)
496 {
497 #ifdef CPU_PPRO2CELERON
498         u_long  eflags;
499         u_int64_t       bbl_cr_ctl3;
500
501         eflags = read_eflags();
502         cpu_disable_intr();
503
504         load_cr0(rcr0() | CR0_CD | CR0_NW);
505         wbinvd();
506
507         bbl_cr_ctl3 = rdmsr(0x11e);
508
509         /* If the L2 cache is configured, do nothing. */
510         if (!(bbl_cr_ctl3 & 1)) {
511                 bbl_cr_ctl3 = 0x134052bLL;
512
513                 /* Set L2 Cache Latency (Default: 5). */
514 #ifdef  CPU_CELERON_L2_LATENCY
515 #if CPU_L2_LATENCY > 15
516 #error invalid CPU_L2_LATENCY.
517 #endif
518                 bbl_cr_ctl3 |= CPU_L2_LATENCY << 1;
519 #else
520                 bbl_cr_ctl3 |= 5 << 1;
521 #endif
522                 wrmsr(0x11e, bbl_cr_ctl3);
523         }
524
525         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));
526         write_eflags(eflags);
527 #endif /* CPU_PPRO2CELERON */
528 }
529
530 /*
531  * Initialize special VIA C3/C7 features
532  */
533 static void
534 init_via(void)
535 {
536         u_int regs[4], val;
537         u_int64_t msreg;
538
539         do_cpuid(0xc0000000, regs);
540         val = regs[0];
541         if (val >= 0xc0000001) {
542                 do_cpuid(0xc0000001, regs);
543                 val = regs[3];
544         } else
545                 val = 0;
546
547         /* Enable RNG if present and disabled */
548         if (val & VIA_CPUID_HAS_RNG) {
549                 if (!(val & VIA_CPUID_DO_RNG)) {
550                         msreg = rdmsr(0x110B);
551                         msreg |= 0x40;
552                         wrmsr(0x110B, msreg);
553                 }
554                 via_feature_rng = VIA_HAS_RNG;
555         }
556         /* Enable AES engine if present and disabled */
557         if (val & VIA_CPUID_HAS_ACE) {
558                 if (!(val & VIA_CPUID_DO_ACE)) {
559                         msreg = rdmsr(0x1107);
560                         msreg |= (0x01 << 28);
561                         wrmsr(0x1107, msreg);
562                 }
563                 via_feature_xcrypt |= VIA_HAS_AES;
564         }
565         /* Enable ACE2 engine if present and disabled */
566         if (val & VIA_CPUID_HAS_ACE2) {
567                 if (!(val & VIA_CPUID_DO_ACE2)) {
568                         msreg = rdmsr(0x1107);
569                         msreg |= (0x01 << 28);
570                         wrmsr(0x1107, msreg);
571                 }
572                 via_feature_xcrypt |= VIA_HAS_AESCTR;
573         }
574         /* Enable SHA engine if present and disabled */
575         if (val & VIA_CPUID_HAS_PHE) {
576                 if (!(val & VIA_CPUID_DO_PHE)) {
577                         msreg = rdmsr(0x1107);
578                         msreg |= (0x01 << 28/**/);
579                         wrmsr(0x1107, msreg);
580                 }
581                 via_feature_xcrypt |= VIA_HAS_SHA;
582         }
583         /* Enable MM engine if present and disabled */
584         if (val & VIA_CPUID_HAS_PMM) {
585                 if (!(val & VIA_CPUID_DO_PMM)) {
586                         msreg = rdmsr(0x1107);
587                         msreg |= (0x01 << 28/**/);
588                         wrmsr(0x1107, msreg);
589                 }
590                 via_feature_xcrypt |= VIA_HAS_MM;
591         }
592 }
593
594 #endif /* I686_CPU */
595
596 /*
597  * Initialize CR4 (Control register 4) to enable SSE instructions.
598  */
599 void
600 enable_sse(void)
601 {
602 #ifndef CPU_DISABLE_SSE
603         if ((cpu_feature & CPUID_SSE) && (cpu_feature & CPUID_FXSR)) {
604                 load_cr4(rcr4() | CR4_FXSR | CR4_XMM);
605                 cpu_fxsr = hw_instruction_sse = 1;
606         }
607 #endif
608 }
609
610 #ifdef I686_CPU
611 static
612 void
613 init_686_amd(void)
614 {
615 #ifdef CPU_ATHLON_SSE_HACK
616         /*
617          * Sometimes the BIOS doesn't enable SSE instructions.
618          * According to AMD document 20734, the mobile
619          * Duron, the (mobile) Athlon 4 and the Athlon MP
620          * support SSE. These correspond to cpu_id 0x66X
621          * or 0x67X.
622          */
623         if ((cpu_feature & CPUID_XMM) == 0 &&
624             ((cpu_id & ~0xf) == 0x660 ||
625              (cpu_id & ~0xf) == 0x670 ||
626              (cpu_id & ~0xf) == 0x680)) {
627                 u_int regs[4];
628                 wrmsr(0xC0010015, rdmsr(0xC0010015) & ~0x08000);
629                 do_cpuid(1, regs);
630                 cpu_feature = regs[3];
631         }
632 #endif
633 }
634 #endif /* I686_CPU */
635
636 void
637 initializecpu(void)
638 {
639         switch (cpu) {
640 #ifdef I486_CPU
641         case CPU_BLUE:
642                 init_bluelightning();
643                 break;
644         case CPU_486DLC:
645                 init_486dlc();
646                 break;
647         case CPU_CY486DX:
648                 init_cy486dx();
649                 break;
650         case CPU_M1SC:
651                 init_5x86();
652                 break;
653 #ifdef CPU_I486_ON_386
654         case CPU_486:
655                 init_i486_on_386();
656                 break;
657 #endif
658         case CPU_M1:
659                 init_6x86();
660                 break;
661 #endif /* I486_CPU */
662 #ifdef I686_CPU
663         case CPU_M2:
664                 init_6x86MX();
665                 break;
666         case CPU_686:
667                 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
668                         switch (cpu_id & 0xff0) {
669                         case 0x610:
670                                 init_ppro();
671                                 break;
672                         case 0x660:
673                                 init_mendocino();
674                                 break;
675                         }
676                 } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
677                         init_686_amd();
678                 } else if (cpu_vendor_id == CPU_VENDOR_CENTAUR) {
679                         switch (cpu_id & 0xff0) {
680                         case 0x690:
681                                 if ((cpu_id & 0xf) < 3)
682                                         break;
683                                 /* fall through. */
684                         case 0x6a0:
685                         case 0x6d0:
686                         case 0x6f0:
687                                 init_via();
688                                 break;
689                         default:
690                                 break;
691                         }
692                 }
693                 break;
694 #endif
695         default:
696                 break;
697         }
698         enable_sse();
699
700         if (cpu_feature2 & CPUID2_VMM)
701                 vmm_guest = 1;
702
703         TUNABLE_INT_FETCH("hw.clflush_enable", &hw_clflush_enable);
704         if (cpu_feature & CPUID_CLFSH) {
705                 cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8;
706
707                 if (hw_clflush_enable == 0 ||
708                     ((hw_clflush_enable == -1) && vmm_guest))
709                         cpu_feature &= ~CPUID_CLFSH;
710         }
711
712 }
713
714 #if defined(I586_CPU) && defined(CPU_WT_ALLOC)
715 /*
716  * Enable write allocate feature of AMD processors.
717  * Following two functions require the Maxmem variable being set.
718  */
719 void
720 enable_K5_wt_alloc(void)
721 {
722         u_int64_t       msr;
723
724         /*
725          * Write allocate is supported only on models 1, 2, and 3, with
726          * a stepping of 4 or greater.
727          */
728         if (((cpu_id & 0xf0) > 0) && ((cpu_id & 0x0f) > 3)) {
729                 cpu_disable_intr();
730                 msr = rdmsr(0x83);              /* HWCR */
731                 wrmsr(0x83, msr & !(0x10));
732
733                 /*
734                  * We have to tell the chip where the top of memory is,
735                  * since video cards could have frame bufferes there,
736                  * memory-mapped I/O could be there, etc.
737                  */
738                 if(Maxmem > 0)
739                   msr = Maxmem / 16;
740                 else
741                   msr = 0;
742                 msr |= AMD_WT_ALLOC_TME | AMD_WT_ALLOC_FRE;
743
744                 /*
745                  * There is no way to know wheter 15-16M hole exists or not. 
746                  * Therefore, we disable write allocate for this range.
747                  */
748                 wrmsr(0x86, 0x0ff00f0);
749                 msr |= AMD_WT_ALLOC_PRE;
750                 wrmsr(0x85, msr);
751
752                 msr=rdmsr(0x83);
753                 wrmsr(0x83, msr|0x10); /* enable write allocate */
754
755                 cpu_enable_intr();
756         }
757 }
758
759 void
760 enable_K6_wt_alloc(void)
761 {
762         quad_t  size;
763         u_int64_t       whcr;
764         u_long  eflags;
765
766         eflags = read_eflags();
767         cpu_disable_intr();
768         wbinvd();
769
770 #ifdef CPU_DISABLE_CACHE
771         /*
772          * Certain K6-2 box becomes unstable when write allocation is
773          * enabled.
774          */
775         /*
776          * The AMD-K6 processer provides the 64-bit Test Register 12(TR12),
777          * but only the Cache Inhibit(CI) (bit 3 of TR12) is suppported.
778          * All other bits in TR12 have no effect on the processer's operation.
779          * The I/O Trap Restart function (bit 9 of TR12) is always enabled
780          * on the AMD-K6.
781          */
782         wrmsr(0x0000000e, (u_int64_t)0x0008);
783 #endif
784         /* Don't assume that memory size is aligned with 4M. */
785         if (Maxmem > 0)
786           size = ((Maxmem >> 8) + 3) >> 2;
787         else
788           size = 0;
789
790         /* Limit is 508M bytes. */
791         if (size > 0x7f)
792                 size = 0x7f;
793         whcr = (rdmsr(0xc0000082) & ~(0x7fLL << 1)) | (size << 1);
794
795 #if defined(NO_MEMORY_HOLE)
796         if (whcr & (0x7fLL << 1))
797                 whcr |=  0x0001LL;
798 #else
799         /*
800          * There is no way to know wheter 15-16M hole exists or not. 
801          * Therefore, we disable write allocate for this range.
802          */
803         whcr &= ~0x0001LL;
804 #endif
805         wrmsr(0x0c0000082, whcr);
806
807         write_eflags(eflags);
808 }
809
810 void
811 enable_K6_2_wt_alloc(void)
812 {
813         quad_t  size;
814         u_int64_t       whcr;
815         u_long  eflags;
816
817         eflags = read_eflags();
818         cpu_disable_intr();
819         wbinvd();
820
821 #ifdef CPU_DISABLE_CACHE
822         /*
823          * Certain K6-2 box becomes unstable when write allocation is
824          * enabled.
825          */
826         /*
827          * The AMD-K6 processer provides the 64-bit Test Register 12(TR12),
828          * but only the Cache Inhibit(CI) (bit 3 of TR12) is suppported.
829          * All other bits in TR12 have no effect on the processer's operation.
830          * The I/O Trap Restart function (bit 9 of TR12) is always enabled
831          * on the AMD-K6.
832          */
833         wrmsr(0x0000000e, (u_int64_t)0x0008);
834 #endif
835         /* Don't assume that memory size is aligned with 4M. */
836         if (Maxmem > 0)
837           size = ((Maxmem >> 8) + 3) >> 2;
838         else
839           size = 0;
840
841         /* Limit is 4092M bytes. */
842         if (size > 0x3fff)
843                 size = 0x3ff;
844         whcr = (rdmsr(0xc0000082) & ~(0x3ffLL << 22)) | (size << 22);
845
846 #if defined(NO_MEMORY_HOLE)
847         if (whcr & (0x3ffLL << 22))
848                 whcr |=  1LL << 16;
849 #else
850         /*
851          * There is no way to know wheter 15-16M hole exists or not. 
852          * Therefore, we disable write allocate for this range.
853          */
854         whcr &= ~(1LL << 16);
855 #endif
856         wrmsr(0x0c0000082, whcr);
857
858         write_eflags(eflags);
859 }
860 #endif /* I585_CPU && CPU_WT_ALLOC */
861
862 #include "opt_ddb.h"
863 #ifdef DDB
864 #include <ddb/ddb.h>
865
866 DB_SHOW_COMMAND(cyrixreg, cyrixreg)
867 {
868         u_long  eflags;
869         u_int   cr0;
870         u_char  ccr1, ccr2, ccr3;
871         u_char  ccr0 = 0, ccr4 = 0, ccr5 = 0, pcr0 = 0;
872
873         cr0 = rcr0();
874         if (cpu_vendor_id == CPU_VENDOR_CYRIX) {
875                 eflags = read_eflags();
876                 cpu_disable_intr();
877
878
879                 if ((cpu != CPU_M1SC) && (cpu != CPU_CY486DX)) {
880                         ccr0 = read_cyrix_reg(CCR0);
881                 }
882                 ccr1 = read_cyrix_reg(CCR1);
883                 ccr2 = read_cyrix_reg(CCR2);
884                 ccr3 = read_cyrix_reg(CCR3);
885                 if ((cpu == CPU_M1SC) || (cpu == CPU_M1) || (cpu == CPU_M2)) {
886                         write_cyrix_reg(CCR3, CCR3_MAPEN0);
887                         ccr4 = read_cyrix_reg(CCR4);
888                         if ((cpu == CPU_M1) || (cpu == CPU_M2))
889                                 ccr5 = read_cyrix_reg(CCR5);
890                         else
891                                 pcr0 = read_cyrix_reg(PCR0);
892                         write_cyrix_reg(CCR3, ccr3);            /* Restore CCR3. */
893                 }
894                 write_eflags(eflags);
895
896                 if ((cpu != CPU_M1SC) && (cpu != CPU_CY486DX))
897                         kprintf("CCR0=%x, ", (u_int)ccr0);
898
899                 kprintf("CCR1=%x, CCR2=%x, CCR3=%x",
900                         (u_int)ccr1, (u_int)ccr2, (u_int)ccr3);
901                 if ((cpu == CPU_M1SC) || (cpu == CPU_M1) || (cpu == CPU_M2)) {
902                         kprintf(", CCR4=%x, ", (u_int)ccr4);
903                         if (cpu == CPU_M1SC)
904                                 kprintf("PCR0=%x\n", pcr0);
905                         else
906                                 kprintf("CCR5=%x\n", ccr5);
907                 }
908         }
909         kprintf("CR0=%x\n", cr0);
910 }
911 #endif /* DDB */