]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/initcpu.c
MFC @r254374.
[FreeBSD/FreeBSD.git] / sys / i386 / 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_cpu.h"
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/sysctl.h>
39
40 #include <machine/cputypes.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46
47 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
48 #define CPU_ENABLE_SSE
49 #endif
50
51 #if defined(I586_CPU) && defined(CPU_WT_ALLOC)
52 void    enable_K5_wt_alloc(void);
53 void    enable_K6_wt_alloc(void);
54 void    enable_K6_2_wt_alloc(void);
55 #endif
56
57 #ifdef I486_CPU
58 static void init_5x86(void);
59 static void init_bluelightning(void);
60 static void init_486dlc(void);
61 static void init_cy486dx(void);
62 #ifdef CPU_I486_ON_386
63 static void init_i486_on_386(void);
64 #endif
65 static void init_6x86(void);
66 #endif /* I486_CPU */
67
68 #ifdef I686_CPU
69 static void     init_6x86MX(void);
70 static void     init_ppro(void);
71 static void     init_mendocino(void);
72 #endif
73
74 static int      hw_instruction_sse;
75 SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD,
76     &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU");
77 /*
78  * -1: automatic (default)
79  *  0: keep enable CLFLUSH
80  *  1: force disable CLFLUSH
81  */
82 static int      hw_clflush_disable = -1;
83
84 /* Must *NOT* be BSS or locore will bzero these after setting them */
85 int     cpu = 0;                /* Are we 386, 386sx, 486, etc? */
86 u_int   cpu_feature = 0;        /* Feature flags */
87 u_int   cpu_feature2 = 0;       /* Feature flags */
88 u_int   amd_feature = 0;        /* AMD feature flags */
89 u_int   amd_feature2 = 0;       /* AMD feature flags */
90 u_int   amd_pminfo = 0;         /* AMD advanced power management info */
91 u_int   via_feature_rng = 0;    /* VIA RNG features */
92 u_int   via_feature_xcrypt = 0; /* VIA ACE features */
93 u_int   cpu_high = 0;           /* Highest arg to CPUID */
94 u_int   cpu_id = 0;             /* Stepping ID */
95 u_int   cpu_procinfo = 0;       /* HyperThreading Info / Brand Index / CLFUSH */
96 u_int   cpu_procinfo2 = 0;      /* Multicore info */
97 char    cpu_vendor[20] = "";    /* CPU Origin code */
98 u_int   cpu_vendor_id = 0;      /* CPU vendor ID */
99 u_int   cpu_clflush_line_size = 32;
100 u_int   cpu_mon_mwait_flags;    /* MONITOR/MWAIT flags (CPUID.05H.ECX) */
101 u_int   cpu_mon_min_size;       /* MONITOR minimum range size, bytes */
102 u_int   cpu_mon_max_size;       /* MONITOR minimum range size, bytes */
103
104 SYSCTL_UINT(_hw, OID_AUTO, via_feature_rng, CTLFLAG_RD,
105         &via_feature_rng, 0, "VIA RNG feature available in CPU");
106 SYSCTL_UINT(_hw, OID_AUTO, via_feature_xcrypt, CTLFLAG_RD,
107         &via_feature_xcrypt, 0, "VIA xcrypt feature available in CPU");
108
109 #ifdef CPU_ENABLE_SSE
110 u_int   cpu_fxsr;               /* SSE enabled */
111 u_int   cpu_mxcsr_mask;         /* valid bits in mxcsr */
112 #endif
113
114 #ifdef I486_CPU
115 /*
116  * IBM Blue Lightning
117  */
118 static void
119 init_bluelightning(void)
120 {
121         register_t saveintr;
122
123 #if defined(PC98) && !defined(CPU_UPGRADE_HW_CACHE)
124         need_post_dma_flush = 1;
125 #endif
126
127         saveintr = intr_disable();
128
129         load_cr0(rcr0() | CR0_CD | CR0_NW);
130         invd();
131
132 #ifdef CPU_BLUELIGHTNING_FPU_OP_CACHE
133         wrmsr(0x1000, 0x9c92LL);        /* FP operand can be cacheable on Cyrix FPU */
134 #else
135         wrmsr(0x1000, 0x1c92LL);        /* Intel FPU */
136 #endif
137         /* Enables 13MB and 0-640KB cache. */
138         wrmsr(0x1001, (0xd0LL << 32) | 0x3ff);
139 #ifdef CPU_BLUELIGHTNING_3X
140         wrmsr(0x1002, 0x04000000LL);    /* Enables triple-clock mode. */
141 #else
142         wrmsr(0x1002, 0x03000000LL);    /* Enables double-clock mode. */
143 #endif
144
145         /* Enable caching in CR0. */
146         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
147         invd();
148         intr_restore(saveintr);
149 }
150
151 /*
152  * Cyrix 486SLC/DLC/SR/DR series
153  */
154 static void
155 init_486dlc(void)
156 {
157         register_t saveintr;
158         u_char  ccr0;
159
160         saveintr = intr_disable();
161         invd();
162
163         ccr0 = read_cyrix_reg(CCR0);
164 #ifndef CYRIX_CACHE_WORKS
165         ccr0 |= CCR0_NC1 | CCR0_BARB;
166         write_cyrix_reg(CCR0, ccr0);
167         invd();
168 #else
169         ccr0 &= ~CCR0_NC0;
170 #ifndef CYRIX_CACHE_REALLY_WORKS
171         ccr0 |= CCR0_NC1 | CCR0_BARB;
172 #else
173         ccr0 |= CCR0_NC1;
174 #endif
175 #ifdef CPU_DIRECT_MAPPED_CACHE
176         ccr0 |= CCR0_CO;                        /* Direct mapped mode. */
177 #endif
178         write_cyrix_reg(CCR0, ccr0);
179
180         /* Clear non-cacheable region. */
181         write_cyrix_reg(NCR1+2, NCR_SIZE_0K);
182         write_cyrix_reg(NCR2+2, NCR_SIZE_0K);
183         write_cyrix_reg(NCR3+2, NCR_SIZE_0K);
184         write_cyrix_reg(NCR4+2, NCR_SIZE_0K);
185
186         write_cyrix_reg(0, 0);  /* dummy write */
187
188         /* Enable caching in CR0. */
189         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
190         invd();
191 #endif /* !CYRIX_CACHE_WORKS */
192         intr_restore(saveintr);
193 }
194
195
196 /*
197  * Cyrix 486S/DX series
198  */
199 static void
200 init_cy486dx(void)
201 {
202         register_t saveintr;
203         u_char  ccr2;
204
205         saveintr = intr_disable();
206         invd();
207
208         ccr2 = read_cyrix_reg(CCR2);
209 #ifdef CPU_SUSP_HLT
210         ccr2 |= CCR2_SUSP_HLT;
211 #endif
212
213 #ifdef PC98
214         /* Enables WB cache interface pin and Lock NW bit in CR0. */
215         ccr2 |= CCR2_WB | CCR2_LOCK_NW;
216         /* Unlock NW bit in CR0. */
217         write_cyrix_reg(CCR2, ccr2 & ~CCR2_LOCK_NW);
218         load_cr0((rcr0() & ~CR0_CD) | CR0_NW);  /* CD = 0, NW = 1 */
219 #endif
220
221         write_cyrix_reg(CCR2, ccr2);
222         intr_restore(saveintr);
223 }
224
225
226 /*
227  * Cyrix 5x86
228  */
229 static void
230 init_5x86(void)
231 {
232         register_t saveintr;
233         u_char  ccr2, ccr3, ccr4, pcr0;
234
235         saveintr = intr_disable();
236
237         load_cr0(rcr0() | CR0_CD | CR0_NW);
238         wbinvd();
239
240         (void)read_cyrix_reg(CCR3);             /* dummy */
241
242         /* Initialize CCR2. */
243         ccr2 = read_cyrix_reg(CCR2);
244         ccr2 |= CCR2_WB;
245 #ifdef CPU_SUSP_HLT
246         ccr2 |= CCR2_SUSP_HLT;
247 #else
248         ccr2 &= ~CCR2_SUSP_HLT;
249 #endif
250         ccr2 |= CCR2_WT1;
251         write_cyrix_reg(CCR2, ccr2);
252
253         /* Initialize CCR4. */
254         ccr3 = read_cyrix_reg(CCR3);
255         write_cyrix_reg(CCR3, CCR3_MAPEN0);
256
257         ccr4 = read_cyrix_reg(CCR4);
258         ccr4 |= CCR4_DTE;
259         ccr4 |= CCR4_MEM;
260 #ifdef CPU_FASTER_5X86_FPU
261         ccr4 |= CCR4_FASTFPE;
262 #else
263         ccr4 &= ~CCR4_FASTFPE;
264 #endif
265         ccr4 &= ~CCR4_IOMASK;
266         /********************************************************************
267          * WARNING: The "BIOS Writers Guide" mentions that I/O recovery time
268          * should be 0 for errata fix.
269          ********************************************************************/
270 #ifdef CPU_IORT
271         ccr4 |= CPU_IORT & CCR4_IOMASK;
272 #endif
273         write_cyrix_reg(CCR4, ccr4);
274
275         /* Initialize PCR0. */
276         /****************************************************************
277          * WARNING: RSTK_EN and LOOP_EN could make your system unstable.
278          * BTB_EN might make your system unstable.
279          ****************************************************************/
280         pcr0 = read_cyrix_reg(PCR0);
281 #ifdef CPU_RSTK_EN
282         pcr0 |= PCR0_RSTK;
283 #else
284         pcr0 &= ~PCR0_RSTK;
285 #endif
286 #ifdef CPU_BTB_EN
287         pcr0 |= PCR0_BTB;
288 #else
289         pcr0 &= ~PCR0_BTB;
290 #endif
291 #ifdef CPU_LOOP_EN
292         pcr0 |= PCR0_LOOP;
293 #else
294         pcr0 &= ~PCR0_LOOP;
295 #endif
296
297         /****************************************************************
298          * WARNING: if you use a memory mapped I/O device, don't use
299          * DISABLE_5X86_LSSER option, which may reorder memory mapped
300          * I/O access.
301          * IF YOUR MOTHERBOARD HAS PCI BUS, DON'T DISABLE LSSER.
302          ****************************************************************/
303 #ifdef CPU_DISABLE_5X86_LSSER
304         pcr0 &= ~PCR0_LSSER;
305 #else
306         pcr0 |= PCR0_LSSER;
307 #endif
308         write_cyrix_reg(PCR0, pcr0);
309
310         /* Restore CCR3. */
311         write_cyrix_reg(CCR3, ccr3);
312
313         (void)read_cyrix_reg(0x80);             /* dummy */
314
315         /* Unlock NW bit in CR0. */
316         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
317         load_cr0((rcr0() & ~CR0_CD) | CR0_NW);  /* CD = 0, NW = 1 */
318         /* Lock NW bit in CR0. */
319         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
320
321         intr_restore(saveintr);
322 }
323
324 #ifdef CPU_I486_ON_386
325 /*
326  * There are i486 based upgrade products for i386 machines.
327  * In this case, BIOS doesn't enable CPU cache.
328  */
329 static void
330 init_i486_on_386(void)
331 {
332         register_t saveintr;
333
334 #if defined(PC98) && !defined(CPU_UPGRADE_HW_CACHE)
335         need_post_dma_flush = 1;
336 #endif
337
338         saveintr = intr_disable();
339
340         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0, NW = 0 */
341
342         intr_restore(saveintr);
343 }
344 #endif
345
346 /*
347  * Cyrix 6x86
348  *
349  * XXX - What should I do here?  Please let me know.
350  */
351 static void
352 init_6x86(void)
353 {
354         register_t saveintr;
355         u_char  ccr3, ccr4;
356
357         saveintr = intr_disable();
358
359         load_cr0(rcr0() | CR0_CD | CR0_NW);
360         wbinvd();
361
362         /* Initialize CCR0. */
363         write_cyrix_reg(CCR0, read_cyrix_reg(CCR0) | CCR0_NC1);
364
365         /* Initialize CCR1. */
366 #ifdef CPU_CYRIX_NO_LOCK
367         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) | CCR1_NO_LOCK);
368 #else
369         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) & ~CCR1_NO_LOCK);
370 #endif
371
372         /* Initialize CCR2. */
373 #ifdef CPU_SUSP_HLT
374         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_SUSP_HLT);
375 #else
376         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_SUSP_HLT);
377 #endif
378
379         ccr3 = read_cyrix_reg(CCR3);
380         write_cyrix_reg(CCR3, CCR3_MAPEN0);
381
382         /* Initialize CCR4. */
383         ccr4 = read_cyrix_reg(CCR4);
384         ccr4 |= CCR4_DTE;
385         ccr4 &= ~CCR4_IOMASK;
386 #ifdef CPU_IORT
387         write_cyrix_reg(CCR4, ccr4 | (CPU_IORT & CCR4_IOMASK));
388 #else
389         write_cyrix_reg(CCR4, ccr4 | 7);
390 #endif
391
392         /* Initialize CCR5. */
393 #ifdef CPU_WT_ALLOC
394         write_cyrix_reg(CCR5, read_cyrix_reg(CCR5) | CCR5_WT_ALLOC);
395 #endif
396
397         /* Restore CCR3. */
398         write_cyrix_reg(CCR3, ccr3);
399
400         /* Unlock NW bit in CR0. */
401         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
402
403         /*
404          * Earlier revision of the 6x86 CPU could crash the system if
405          * L1 cache is in write-back mode.
406          */
407         if ((cyrix_did & 0xff00) > 0x1600)
408                 load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
409         else {
410                 /* Revision 2.6 and lower. */
411 #ifdef CYRIX_CACHE_REALLY_WORKS
412                 load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
413 #else
414                 load_cr0((rcr0() & ~CR0_CD) | CR0_NW);  /* CD = 0 and NW = 1 */
415 #endif
416         }
417
418         /* Lock NW bit in CR0. */
419         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
420
421         intr_restore(saveintr);
422 }
423 #endif /* I486_CPU */
424
425 #ifdef I586_CPU
426 /*
427  * IDT WinChip C6/2/2A/2B/3
428  *
429  * http://www.centtech.com/winchip_bios_writers_guide_v4_0.pdf
430  */
431 static void
432 init_winchip(void)
433 {
434         u_int regs[4];
435         uint64_t fcr;
436
437         fcr = rdmsr(0x0107);
438
439         /*
440          * Set ECX8, DSMC, DTLOCK/EDCTLB, EMMX, and ERETSTK and clear DPDC.
441          */
442         fcr |= (1 << 1) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 16);
443         fcr &= ~(1ULL << 11);
444
445         /*
446          * Additioanlly, set EBRPRED, E2MMX and EAMD3D for WinChip 2 and 3.
447          */
448         if (CPUID_TO_MODEL(cpu_id) >= 8)
449                 fcr |= (1 << 12) | (1 << 19) | (1 << 20);
450
451         wrmsr(0x0107, fcr);
452         do_cpuid(1, regs);
453         cpu_feature = regs[3];
454 }
455 #endif
456
457 #ifdef I686_CPU
458 /*
459  * Cyrix 6x86MX (code-named M2)
460  *
461  * XXX - What should I do here?  Please let me know.
462  */
463 static void
464 init_6x86MX(void)
465 {
466         register_t saveintr;
467         u_char  ccr3, ccr4;
468
469         saveintr = intr_disable();
470
471         load_cr0(rcr0() | CR0_CD | CR0_NW);
472         wbinvd();
473
474         /* Initialize CCR0. */
475         write_cyrix_reg(CCR0, read_cyrix_reg(CCR0) | CCR0_NC1);
476
477         /* Initialize CCR1. */
478 #ifdef CPU_CYRIX_NO_LOCK
479         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) | CCR1_NO_LOCK);
480 #else
481         write_cyrix_reg(CCR1, read_cyrix_reg(CCR1) & ~CCR1_NO_LOCK);
482 #endif
483
484         /* Initialize CCR2. */
485 #ifdef CPU_SUSP_HLT
486         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_SUSP_HLT);
487 #else
488         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_SUSP_HLT);
489 #endif
490
491         ccr3 = read_cyrix_reg(CCR3);
492         write_cyrix_reg(CCR3, CCR3_MAPEN0);
493
494         /* Initialize CCR4. */
495         ccr4 = read_cyrix_reg(CCR4);
496         ccr4 &= ~CCR4_IOMASK;
497 #ifdef CPU_IORT
498         write_cyrix_reg(CCR4, ccr4 | (CPU_IORT & CCR4_IOMASK));
499 #else
500         write_cyrix_reg(CCR4, ccr4 | 7);
501 #endif
502
503         /* Initialize CCR5. */
504 #ifdef CPU_WT_ALLOC
505         write_cyrix_reg(CCR5, read_cyrix_reg(CCR5) | CCR5_WT_ALLOC);
506 #endif
507
508         /* Restore CCR3. */
509         write_cyrix_reg(CCR3, ccr3);
510
511         /* Unlock NW bit in CR0. */
512         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) & ~CCR2_LOCK_NW);
513
514         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  /* CD = 0 and NW = 0 */
515
516         /* Lock NW bit in CR0. */
517         write_cyrix_reg(CCR2, read_cyrix_reg(CCR2) | CCR2_LOCK_NW);
518
519         intr_restore(saveintr);
520 }
521
522 static void
523 init_ppro(void)
524 {
525         u_int64_t       apicbase;
526
527         /*
528          * Local APIC should be disabled if it is not going to be used.
529          */
530         apicbase = rdmsr(MSR_APICBASE);
531         apicbase &= ~APICBASE_ENABLED;
532         wrmsr(MSR_APICBASE, apicbase);
533 }
534
535 /*
536  * Initialize BBL_CR_CTL3 (Control register 3: used to configure the
537  * L2 cache).
538  */
539 static void
540 init_mendocino(void)
541 {
542 #ifdef CPU_PPRO2CELERON
543         register_t      saveintr;
544         u_int64_t       bbl_cr_ctl3;
545
546         saveintr = intr_disable();
547
548         load_cr0(rcr0() | CR0_CD | CR0_NW);
549         wbinvd();
550
551         bbl_cr_ctl3 = rdmsr(MSR_BBL_CR_CTL3);
552
553         /* If the L2 cache is configured, do nothing. */
554         if (!(bbl_cr_ctl3 & 1)) {
555                 bbl_cr_ctl3 = 0x134052bLL;
556
557                 /* Set L2 Cache Latency (Default: 5). */
558 #ifdef  CPU_CELERON_L2_LATENCY
559 #if CPU_L2_LATENCY > 15
560 #error invalid CPU_L2_LATENCY.
561 #endif
562                 bbl_cr_ctl3 |= CPU_L2_LATENCY << 1;
563 #else
564                 bbl_cr_ctl3 |= 5 << 1;
565 #endif
566                 wrmsr(MSR_BBL_CR_CTL3, bbl_cr_ctl3);
567         }
568
569         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));
570         intr_restore(saveintr);
571 #endif /* CPU_PPRO2CELERON */
572 }
573
574 /*
575  * Initialize special VIA features
576  */
577 static void
578 init_via(void)
579 {
580         u_int regs[4], val;
581         uint64_t fcr;
582
583         /*
584          * Explicitly enable CX8 and PGE on C3.
585          *
586          * http://www.via.com.tw/download/mainboards/6/13/VIA_C3_EBGA%20datasheet110.pdf
587          */
588         if (CPUID_TO_MODEL(cpu_id) <= 9)
589                 fcr = (1 << 1) | (1 << 7);
590         else
591                 fcr = 0;
592
593         /*
594          * Check extended CPUID for PadLock features.
595          *
596          * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
597          */
598         do_cpuid(0xc0000000, regs);
599         if (regs[0] >= 0xc0000001) {
600                 do_cpuid(0xc0000001, regs);
601                 val = regs[3];
602         } else
603                 val = 0;
604
605         /* Enable RNG if present. */
606         if ((val & VIA_CPUID_HAS_RNG) != 0) {
607                 via_feature_rng = VIA_HAS_RNG;
608                 wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG);
609         }
610
611         /* Enable PadLock if present. */
612         if ((val & VIA_CPUID_HAS_ACE) != 0)
613                 via_feature_xcrypt |= VIA_HAS_AES;
614         if ((val & VIA_CPUID_HAS_ACE2) != 0)
615                 via_feature_xcrypt |= VIA_HAS_AESCTR;
616         if ((val & VIA_CPUID_HAS_PHE) != 0)
617                 via_feature_xcrypt |= VIA_HAS_SHA;
618         if ((val & VIA_CPUID_HAS_PMM) != 0)
619                 via_feature_xcrypt |= VIA_HAS_MM;
620         if (via_feature_xcrypt != 0)
621                 fcr |= 1 << 28;
622
623         wrmsr(0x1107, rdmsr(0x1107) | fcr);
624 }
625
626 #endif /* I686_CPU */
627
628 #if defined(I586_CPU) || defined(I686_CPU)
629 static void
630 init_transmeta(void)
631 {
632         u_int regs[0];
633
634         /* Expose all hidden features. */
635         wrmsr(0x80860004, rdmsr(0x80860004) | ~0UL);
636         do_cpuid(1, regs);
637         cpu_feature = regs[3];
638 }
639 #endif
640
641 /*
642  * Initialize CR4 (Control register 4) to enable SSE instructions.
643  */
644 void
645 enable_sse(void)
646 {
647 #if defined(CPU_ENABLE_SSE)
648         if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) {
649                 load_cr4(rcr4() | CR4_FXSR | CR4_XMM);
650                 cpu_fxsr = hw_instruction_sse = 1;
651         }
652 #endif
653 }
654
655 extern int elf32_nxstack;
656
657 void
658 initializecpu(void)
659 {
660
661         switch (cpu) {
662 #ifdef I486_CPU
663         case CPU_BLUE:
664                 init_bluelightning();
665                 break;
666         case CPU_486DLC:
667                 init_486dlc();
668                 break;
669         case CPU_CY486DX:
670                 init_cy486dx();
671                 break;
672         case CPU_M1SC:
673                 init_5x86();
674                 break;
675 #ifdef CPU_I486_ON_386
676         case CPU_486:
677                 init_i486_on_386();
678                 break;
679 #endif
680         case CPU_M1:
681                 init_6x86();
682                 break;
683 #endif /* I486_CPU */
684 #ifdef I586_CPU
685         case CPU_586:
686                 switch (cpu_vendor_id) {
687                 case CPU_VENDOR_CENTAUR:
688                         init_winchip();
689                         break;
690                 case CPU_VENDOR_TRANSMETA:
691                         init_transmeta();
692                         break;
693                 }
694                 break;
695 #endif
696 #ifdef I686_CPU
697         case CPU_M2:
698                 init_6x86MX();
699                 break;
700         case CPU_686:
701                 switch (cpu_vendor_id) {
702                 case CPU_VENDOR_INTEL:
703                         switch (cpu_id & 0xff0) {
704                         case 0x610:
705                                 init_ppro();
706                                 break;
707                         case 0x660:
708                                 init_mendocino();
709                                 break;
710                         }
711                         break;
712 #ifdef CPU_ATHLON_SSE_HACK
713                 case CPU_VENDOR_AMD:
714                         /*
715                          * Sometimes the BIOS doesn't enable SSE instructions.
716                          * According to AMD document 20734, the mobile
717                          * Duron, the (mobile) Athlon 4 and the Athlon MP
718                          * support SSE. These correspond to cpu_id 0x66X
719                          * or 0x67X.
720                          */
721                         if ((cpu_feature & CPUID_XMM) == 0 &&
722                             ((cpu_id & ~0xf) == 0x660 ||
723                              (cpu_id & ~0xf) == 0x670 ||
724                              (cpu_id & ~0xf) == 0x680)) {
725                                 u_int regs[4];
726                                 wrmsr(MSR_HWCR, rdmsr(MSR_HWCR) & ~0x08000);
727                                 do_cpuid(1, regs);
728                                 cpu_feature = regs[3];
729                         }
730                         break;
731 #endif
732                 case CPU_VENDOR_CENTAUR:
733                         init_via();
734                         break;
735                 case CPU_VENDOR_TRANSMETA:
736                         init_transmeta();
737                         break;
738                 }
739 #ifdef PAE
740                 if ((amd_feature & AMDID_NX) != 0) {
741                         uint64_t msr;
742
743                         msr = rdmsr(MSR_EFER) | EFER_NXE;
744                         wrmsr(MSR_EFER, msr);
745                         pg_nx = PG_NX;
746                         elf32_nxstack = 1;
747                 }
748 #endif
749                 break;
750 #endif
751         default:
752                 break;
753         }
754         enable_sse();
755
756         /*
757          * CPUID with %eax = 1, %ebx returns
758          * Bits 15-8: CLFLUSH line size
759          *      (Value * 8 = cache line size in bytes)
760          */
761         if ((cpu_feature & CPUID_CLFSH) != 0)
762                 cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8;
763         /*
764          * XXXKIB: (temporary) hack to work around traps generated
765          * when CLFLUSHing APIC register window under virtualization
766          * environments.  These environments tend to disable the
767          * CPUID_SS feature even though the native CPU supports it.
768          */
769         TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable);
770         if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1)
771                 cpu_feature &= ~CPUID_CLFSH;
772         /*
773          * Allow to disable CLFLUSH feature manually by
774          * hw.clflush_disable tunable.
775          */
776         if (hw_clflush_disable == 1)
777                 cpu_feature &= ~CPUID_CLFSH;
778
779 #if defined(PC98) && !defined(CPU_UPGRADE_HW_CACHE)
780         /*
781          * OS should flush L1 cache by itself because no PC-98 supports
782          * non-Intel CPUs.  Use wbinvd instruction before DMA transfer
783          * when need_pre_dma_flush = 1, use invd instruction after DMA
784          * transfer when need_post_dma_flush = 1.  If your CPU upgrade
785          * product supports hardware cache control, you can add the
786          * CPU_UPGRADE_HW_CACHE option in your kernel configuration file.
787          * This option eliminates unneeded cache flush instruction(s).
788          */
789         if (cpu_vendor_id == CPU_VENDOR_CYRIX) {
790                 switch (cpu) {
791 #ifdef I486_CPU
792                 case CPU_486DLC:
793                         need_post_dma_flush = 1;
794                         break;
795                 case CPU_M1SC:
796                         need_pre_dma_flush = 1;
797                         break;
798                 case CPU_CY486DX:
799                         need_pre_dma_flush = 1;
800 #ifdef CPU_I486_ON_386
801                         need_post_dma_flush = 1;
802 #endif
803                         break;
804 #endif
805                 default:
806                         break;
807                 }
808         } else if (cpu_vendor_id == CPU_VENDOR_AMD) {
809                 switch (cpu_id & 0xFF0) {
810                 case 0x470:             /* Enhanced Am486DX2 WB */
811                 case 0x490:             /* Enhanced Am486DX4 WB */
812                 case 0x4F0:             /* Am5x86 WB */
813                         need_pre_dma_flush = 1;
814                         break;
815                 }
816         } else if (cpu_vendor_id == CPU_VENDOR_IBM) {
817                 need_post_dma_flush = 1;
818         } else {
819 #ifdef CPU_I486_ON_386
820                 need_pre_dma_flush = 1;
821 #endif
822         }
823 #endif /* PC98 && !CPU_UPGRADE_HW_CACHE */
824 }
825
826 #if defined(I586_CPU) && defined(CPU_WT_ALLOC)
827 /*
828  * Enable write allocate feature of AMD processors.
829  * Following two functions require the Maxmem variable being set.
830  */
831 void
832 enable_K5_wt_alloc(void)
833 {
834         u_int64_t       msr;
835         register_t      saveintr;
836
837         /*
838          * Write allocate is supported only on models 1, 2, and 3, with
839          * a stepping of 4 or greater.
840          */
841         if (((cpu_id & 0xf0) > 0) && ((cpu_id & 0x0f) > 3)) {
842                 saveintr = intr_disable();
843                 msr = rdmsr(0x83);              /* HWCR */
844                 wrmsr(0x83, msr & !(0x10));
845
846                 /*
847                  * We have to tell the chip where the top of memory is,
848                  * since video cards could have frame bufferes there,
849                  * memory-mapped I/O could be there, etc.
850                  */
851                 if(Maxmem > 0)
852                   msr = Maxmem / 16;
853                 else
854                   msr = 0;
855                 msr |= AMD_WT_ALLOC_TME | AMD_WT_ALLOC_FRE;
856 #ifdef PC98
857                 if (!(inb(0x43b) & 4)) {
858                         wrmsr(0x86, 0x0ff00f0);
859                         msr |= AMD_WT_ALLOC_PRE;
860                 }
861 #else
862                 /*
863                  * There is no way to know wheter 15-16M hole exists or not. 
864                  * Therefore, we disable write allocate for this range.
865                  */
866                         wrmsr(0x86, 0x0ff00f0);
867                         msr |= AMD_WT_ALLOC_PRE;
868 #endif
869                 wrmsr(0x85, msr);
870
871                 msr=rdmsr(0x83);
872                 wrmsr(0x83, msr|0x10); /* enable write allocate */
873                 intr_restore(saveintr);
874         }
875 }
876
877 void
878 enable_K6_wt_alloc(void)
879 {
880         quad_t  size;
881         u_int64_t       whcr;
882         register_t      saveintr;
883
884         saveintr = intr_disable();
885         wbinvd();
886
887 #ifdef CPU_DISABLE_CACHE
888         /*
889          * Certain K6-2 box becomes unstable when write allocation is
890          * enabled.
891          */
892         /*
893          * The AMD-K6 processer provides the 64-bit Test Register 12(TR12),
894          * but only the Cache Inhibit(CI) (bit 3 of TR12) is suppported.
895          * All other bits in TR12 have no effect on the processer's operation.
896          * The I/O Trap Restart function (bit 9 of TR12) is always enabled
897          * on the AMD-K6.
898          */
899         wrmsr(0x0000000e, (u_int64_t)0x0008);
900 #endif
901         /* Don't assume that memory size is aligned with 4M. */
902         if (Maxmem > 0)
903           size = ((Maxmem >> 8) + 3) >> 2;
904         else
905           size = 0;
906
907         /* Limit is 508M bytes. */
908         if (size > 0x7f)
909                 size = 0x7f;
910         whcr = (rdmsr(0xc0000082) & ~(0x7fLL << 1)) | (size << 1);
911
912 #if defined(PC98) || defined(NO_MEMORY_HOLE)
913         if (whcr & (0x7fLL << 1)) {
914 #ifdef PC98
915                 /*
916                  * If bit 2 of port 0x43b is 0, disable wrte allocate for the
917                  * 15-16M range.
918                  */
919                 if (!(inb(0x43b) & 4))
920                         whcr &= ~0x0001LL;
921                 else
922 #endif
923                         whcr |=  0x0001LL;
924         }
925 #else
926         /*
927          * There is no way to know wheter 15-16M hole exists or not. 
928          * Therefore, we disable write allocate for this range.
929          */
930         whcr &= ~0x0001LL;
931 #endif
932         wrmsr(0x0c0000082, whcr);
933
934         intr_restore(saveintr);
935 }
936
937 void
938 enable_K6_2_wt_alloc(void)
939 {
940         quad_t  size;
941         u_int64_t       whcr;
942         register_t      saveintr;
943
944         saveintr = intr_disable();
945         wbinvd();
946
947 #ifdef CPU_DISABLE_CACHE
948         /*
949          * Certain K6-2 box becomes unstable when write allocation is
950          * enabled.
951          */
952         /*
953          * The AMD-K6 processer provides the 64-bit Test Register 12(TR12),
954          * but only the Cache Inhibit(CI) (bit 3 of TR12) is suppported.
955          * All other bits in TR12 have no effect on the processer's operation.
956          * The I/O Trap Restart function (bit 9 of TR12) is always enabled
957          * on the AMD-K6.
958          */
959         wrmsr(0x0000000e, (u_int64_t)0x0008);
960 #endif
961         /* Don't assume that memory size is aligned with 4M. */
962         if (Maxmem > 0)
963           size = ((Maxmem >> 8) + 3) >> 2;
964         else
965           size = 0;
966
967         /* Limit is 4092M bytes. */
968         if (size > 0x3fff)
969                 size = 0x3ff;
970         whcr = (rdmsr(0xc0000082) & ~(0x3ffLL << 22)) | (size << 22);
971
972 #if defined(PC98) || defined(NO_MEMORY_HOLE)
973         if (whcr & (0x3ffLL << 22)) {
974 #ifdef PC98
975                 /*
976                  * If bit 2 of port 0x43b is 0, disable wrte allocate for the
977                  * 15-16M range.
978                  */
979                 if (!(inb(0x43b) & 4))
980                         whcr &= ~(1LL << 16);
981                 else
982 #endif
983                         whcr |=  1LL << 16;
984         }
985 #else
986         /*
987          * There is no way to know wheter 15-16M hole exists or not. 
988          * Therefore, we disable write allocate for this range.
989          */
990         whcr &= ~(1LL << 16);
991 #endif
992         wrmsr(0x0c0000082, whcr);
993
994         intr_restore(saveintr);
995 }
996 #endif /* I585_CPU && CPU_WT_ALLOC */
997
998 #include "opt_ddb.h"
999 #ifdef DDB
1000 #include <ddb/ddb.h>
1001
1002 DB_SHOW_COMMAND(cyrixreg, cyrixreg)
1003 {
1004         register_t saveintr;
1005         u_int   cr0;
1006         u_char  ccr1, ccr2, ccr3;
1007         u_char  ccr0 = 0, ccr4 = 0, ccr5 = 0, pcr0 = 0;
1008
1009         cr0 = rcr0();
1010         if (cpu_vendor_id == CPU_VENDOR_CYRIX) {
1011                 saveintr = intr_disable();
1012
1013
1014                 if ((cpu != CPU_M1SC) && (cpu != CPU_CY486DX)) {
1015                         ccr0 = read_cyrix_reg(CCR0);
1016                 }
1017                 ccr1 = read_cyrix_reg(CCR1);
1018                 ccr2 = read_cyrix_reg(CCR2);
1019                 ccr3 = read_cyrix_reg(CCR3);
1020                 if ((cpu == CPU_M1SC) || (cpu == CPU_M1) || (cpu == CPU_M2)) {
1021                         write_cyrix_reg(CCR3, CCR3_MAPEN0);
1022                         ccr4 = read_cyrix_reg(CCR4);
1023                         if ((cpu == CPU_M1) || (cpu == CPU_M2))
1024                                 ccr5 = read_cyrix_reg(CCR5);
1025                         else
1026                                 pcr0 = read_cyrix_reg(PCR0);
1027                         write_cyrix_reg(CCR3, ccr3);            /* Restore CCR3. */
1028                 }
1029                 intr_restore(saveintr);
1030
1031                 if ((cpu != CPU_M1SC) && (cpu != CPU_CY486DX))
1032                         printf("CCR0=%x, ", (u_int)ccr0);
1033
1034                 printf("CCR1=%x, CCR2=%x, CCR3=%x",
1035                         (u_int)ccr1, (u_int)ccr2, (u_int)ccr3);
1036                 if ((cpu == CPU_M1SC) || (cpu == CPU_M1) || (cpu == CPU_M2)) {
1037                         printf(", CCR4=%x, ", (u_int)ccr4);
1038                         if (cpu == CPU_M1SC)
1039                                 printf("PCR0=%x\n", pcr0);
1040                         else
1041                                 printf("CCR5=%x\n", ccr5);
1042                 }
1043         }
1044         printf("CR0=%x\n", cr0);
1045 }
1046 #endif /* DDB */