Make AMD SMM SMP aware
[coreboot.git] / src / cpu / amd / model_10xxx / model_10xxx_init.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <cpu/x86/msr.h>
22 #include <cpu/amd/mtrr.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <string.h>
26 #include <cpu/x86/msr.h>
27 #include <cpu/x86/smm.h>
28 #include <cpu/x86/pae.h>
29 #include <pc80/mc146818rtc.h>
30 #include <cpu/x86/lapic.h>
31 #include "northbridge/amd/amdfam10/amdfam10.h"
32 #include <cpu/amd/model_10xxx_rev.h>
33 #include <cpu/cpu.h>
34 #include <cpu/x86/cache.h>
35 #include <cpu/x86/mtrr.h>
36 #include <cpu/amd/multicore.h>
37 #include <cpu/amd/model_10xxx_msr.h>
38
39 #define MCI_STATUS 0x401
40
41 msr_t rdmsr_amd(u32 index)
42 {
43         msr_t result;
44         __asm__ __volatile__(
45                 "rdmsr"
46                 :"=a"(result.lo), "=d"(result.hi)
47                 :"c"(index), "D"(0x9c5a203a)
48         );
49         return result;
50 }
51
52 void wrmsr_amd(u32 index, msr_t msr)
53 {
54         __asm__ __volatile__(
55                 "wrmsr"
56                 :       /* No outputs */
57                 :"c"(index), "a"(msr.lo), "d"(msr.hi), "D"(0x9c5a203a)
58         );
59 }
60
61 static void model_10xxx_init(device_t dev)
62 {
63         u8 i;
64         msr_t msr;
65         struct node_core_id id;
66 #if CONFIG_LOGICAL_CPUS == 1
67         u32 siblings;
68 #endif
69
70         id = get_node_core_id(read_nb_cfg_54());        /* nb_cfg_54 can not be set */
71         printk(BIOS_DEBUG, "nodeid = %02d, coreid = %02d\n", id.nodeid, id.coreid);
72
73         /* Turn on caching if we haven't already */
74         x86_enable_cache();
75         amd_setup_mtrrs();
76         x86_mtrr_check();
77
78         disable_cache();
79
80         /* zero the machine check error status registers */
81         msr.lo = 0;
82         msr.hi = 0;
83         for (i = 0; i < 5; i++) {
84                 wrmsr(MCI_STATUS + (i * 4), msr);
85         }
86
87         enable_cache();
88
89         /* Enable the local cpu apics */
90         setup_lapic();
91
92         /* Set the processor name string */
93         init_processor_name();
94
95 #if CONFIG_LOGICAL_CPUS == 1
96         siblings = cpuid_ecx(0x80000008) & 0xff;
97
98         if (siblings > 0) {
99                 msr = rdmsr_amd(CPU_ID_FEATURES_MSR);
100                 msr.lo |= 1 << 28;
101                 wrmsr_amd(CPU_ID_FEATURES_MSR, msr);
102
103                 msr = rdmsr_amd(CPU_ID_EXT_FEATURES_MSR);
104                 msr.hi |= 1 << (33 - 32);
105                 wrmsr_amd(CPU_ID_EXT_FEATURES_MSR, msr);
106         }
107         printk(BIOS_DEBUG, "siblings = %02d, ", siblings);
108 #endif
109
110         /* DisableCf8ExtCfg */
111         msr = rdmsr(NB_CFG_MSR);
112         msr.hi &= ~(1 << (46 - 32));
113         wrmsr(NB_CFG_MSR, msr);
114
115         msr = rdmsr(BU_CFG2_MSR);
116         /* Clear ClLinesToNbDis */
117         msr.lo &= ~(1 << 15);
118         /* Clear bit 35 as per Erratum 343 */
119         msr.hi &= ~(1 << (35-32));
120         wrmsr(BU_CFG2_MSR, msr);
121
122         /* Set SMM base address for this CPU */
123         msr = rdmsr(SMM_BASE_MSR);
124         msr.lo = SMM_BASE - (lapicid() * 0x400);
125         wrmsr(SMM_BASE_MSR, msr);
126
127         /* Enable the SMM memory window */
128         msr = rdmsr(SMM_MASK_MSR);
129         msr.lo |= (1 << 0); /* Enable ASEG SMRAM Range */
130         wrmsr(SMM_MASK_MSR, msr);
131
132         /* Set SMMLOCK to avoid exploits messing with SMM */
133         msr = rdmsr(HWCR_MSR);
134         msr.lo |= (1 << 0);
135         wrmsr(HWCR_MSR, msr);
136
137 }
138
139 static struct device_operations cpu_dev_ops = {
140         .init = model_10xxx_init,
141 };
142
143 static struct cpu_device_id cpu_table[] = {
144 //AMD_GH_SUPPORT
145         { X86_VENDOR_AMD, 0x100f00 },           /* SH-F0 L1 */
146         { X86_VENDOR_AMD, 0x100f10 },           /* M2 */
147         { X86_VENDOR_AMD, 0x100f20 },           /* S1g1 */
148         { X86_VENDOR_AMD, 0x100f21 },
149         { X86_VENDOR_AMD, 0x100f2A },
150         { X86_VENDOR_AMD, 0x100f22 },
151         { X86_VENDOR_AMD, 0x100f23 },
152         { X86_VENDOR_AMD, 0x100f40 },           /* RB-C0 */
153         { X86_VENDOR_AMD, 0x100F42 },           /* RB-C2 */
154         { X86_VENDOR_AMD, 0x100F43 },           /* RB-C3 */
155         { X86_VENDOR_AMD, 0x100F52 },           /* BL-C2 */
156         { X86_VENDOR_AMD, 0x100F62 },           /* DA-C2 */
157         { X86_VENDOR_AMD, 0x100F63 },           /* DA-C3 */
158         { X86_VENDOR_AMD, 0x100F80 },           /* HY-D0 */
159         { X86_VENDOR_AMD, 0x100F81 },           /* HY-D1 */
160         { 0, 0 },
161 };
162
163 static const struct cpu_driver model_10xxx __cpu_driver = {
164         .ops      = &cpu_dev_ops,
165         .id_table = cpu_table,
166 };