blob: c7ee5825d78fa5e8cb3e67d2557777e21e19601c [file] [log] [blame]
Martin Roth433659a2014-05-12 21:55:00 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 * Copyright (C) 2014 Sage Electronic Engineering, LLC.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Martin Roth433659a2014-05-12 21:55:00 -060019 */
20
21#include <stdlib.h>
22#include <console/console.h>
23#include <cpu/cpu.h>
24#include <cpu/intel/microcode.h>
25#include <cpu/intel/turbo.h>
26#include <cpu/x86/cache.h>
27#include <cpu/x86/lapic.h>
28#include <cpu/x86/mp.h>
29#include <cpu/x86/msr.h>
30#include <cpu/x86/mtrr.h>
31#include <cpu/x86/smm.h>
32#include <reg_script.h>
33
34#include <baytrail/msr.h>
35#include <baytrail/pattrs.h>
36#include <baytrail/ramstage.h>
37#if IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)
38#include <baytrail/smm.h>
39
40static void smm_relocate(void *unused);
41static void enable_smis(void *unused);
42
43static struct mp_flight_record mp_steps[] = {
44 MP_FR_BLOCK_APS(smm_relocate, NULL, smm_relocate, NULL),
45 MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL),
46 /* Wait for APs to finish initialization before proceeding. */
47 MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
48};
49#else /* CONFIG_HAVE_SMI_HANDLER */
50static struct mp_flight_record mp_steps[] = {
51 MP_FR_BLOCK_APS(mp_initialize_cpu, NULL, mp_initialize_cpu, NULL),
52};
53#endif
54
55/* The APIC id space on Bay Trail is sparse. Each id is separated by 2. */
56static int adjust_apic_id(int index, int apic_id)
57{
58 return 2 * index;
59}
60
61/* Core level MSRs */
62const struct reg_script core_msr_script[] = {
63 /* Dynamic L2 shrink enable and threshold */
64 REG_MSR_RMW(MSR_PMG_CST_CONFIG_CONTROL, ~0x3f000f, 0xe0008),
65 /* Disable C1E */
66 REG_MSR_RMW(MSR_POWER_CTL, ~0x2, 0),
67 REG_MSR_OR(MSR_POWER_MISC, 0x44),
68 REG_SCRIPT_END
69};
70
71void baytrail_init_cpus(device_t dev)
72{
73 struct bus *cpu_bus = dev->link_list;
74 const struct pattrs *pattrs = pattrs_get();
75 struct mp_params mp_params;
76
77 x86_mtrr_check();
78
79 /* Enable the local cpu apics */
80 setup_lapic();
81
82 mp_params.num_cpus = pattrs->num_cpus,
83 mp_params.parallel_microcode_load = 0,
84 mp_params.adjust_apic_id = adjust_apic_id;
85 mp_params.flight_plan = &mp_steps[0];
86 mp_params.num_records = ARRAY_SIZE(mp_steps);
87 mp_params.microcode_pointer = 0;
88
89 if (mp_init(cpu_bus, &mp_params)) {
90 printk(BIOS_ERR, "MP initialization failure.\n");
91 }
92}
93
94static void baytrail_core_init(device_t cpu)
95{
96 printk(BIOS_DEBUG, "Init BayTrail core.\n");
97
98 /* On bay trail the turbo disable bit is actually scoped at building
99 * block level -- not package. For non-bsp cores that are within a
100 * building block enable turbo. The cores within the BSP's building
101 * block will just see it already enabled and move on. */
102 if (lapicid())
103 enable_turbo();
104
105 /* Set core MSRs */
106 reg_script_run(core_msr_script);
107
108 /* Set this core to max frequency ratio */
109 set_max_freq();
110}
111
112static struct device_operations cpu_dev_ops = {
113 .init = baytrail_core_init,
114};
115
116static struct cpu_device_id cpu_table[] = {
117 { X86_VENDOR_INTEL, 0x30671 },
118 { X86_VENDOR_INTEL, 0x30672 },
119 { X86_VENDOR_INTEL, 0x30673 },
120 { X86_VENDOR_INTEL, 0x30678 },
Herve ELterc7e6cae2014-11-19 16:05:28 +0100121 { X86_VENDOR_INTEL, 0x30679 },
Martin Roth433659a2014-05-12 21:55:00 -0600122 { 0, 0 },
123};
124
125static const struct cpu_driver driver __cpu_driver = {
126 .ops = &cpu_dev_ops,
127 .id_table = cpu_table,
128};
129
130#if IS_ENABLED(CONFIG_HAVE_SMI_HANDLER)
131/*
132 * SMM loading and initialization.
133 */
134
135struct smm_relocation_attrs {
136 uint32_t smbase;
137 uint32_t smrr_base;
138 uint32_t smrr_mask;
139};
140
141static struct smm_relocation_attrs relo_attrs;
142
143static void adjust_apic_id_map(struct smm_loader_params *smm_params)
144{
145 int i;
146 struct smm_runtime *runtime = smm_params->runtime;
147
148 for (i = 0; i < CONFIG_MAX_CPUS; i++)
149 runtime->apic_id_to_cpu[i] = mp_get_apic_id(i);
150}
151
Kayalvizhi Dhandapani454625c2014-10-07 14:34:01 -0400152static void asmlinkage cpu_smm_do_relocation(void *arg)
Martin Roth433659a2014-05-12 21:55:00 -0600153{
154#ifndef CONFIG_MAX_CPUS
155#error CONFIG_MAX_CPUS must be set.
156#endif
157 msr_t smrr;
158 em64t100_smm_state_save_area_t *smm_state;
Kayalvizhi Dhandapani454625c2014-10-07 14:34:01 -0400159 const struct smm_module_params *p;
160 const struct smm_runtime *runtime;
161 int cpu;
162
163 p = arg;
164 runtime = p->runtime;
165 cpu = p->cpu;
Martin Roth433659a2014-05-12 21:55:00 -0600166
167 if (cpu >= CONFIG_MAX_CPUS) {
168 printk(BIOS_CRIT,
169 "Invalid CPU number assigned in SMM stub: %d\n", cpu);
170 return;
171 }
172
173 /* Set up SMRR. */
174 smrr.lo = relo_attrs.smrr_base;
175 smrr.hi = 0;
176 wrmsr(SMRRphysBase_MSR, smrr);
177 smrr.lo = relo_attrs.smrr_mask;
178 smrr.hi = 0;
179 wrmsr(SMRRphysMask_MSR, smrr);
180
181 /* The relocated handler runs with all CPUs concurrently. Therefore
182 * stagger the entry points adjusting SMBASE downwards by save state
183 * size * CPU num. */
184 smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + runtime->smbase);
185 smm_state->smbase = relo_attrs.smbase - cpu * runtime->save_state_size;
186 printk(BIOS_DEBUG, "New SMBASE 0x%08x\n", smm_state->smbase);
187}
188
189static int install_relocation_handler(int num_cpus)
190{
191 const int save_state_size = sizeof(em64t100_smm_state_save_area_t);
192
193 struct smm_loader_params smm_params = {
194 .per_cpu_stack_size = save_state_size,
195 .num_concurrent_stacks = num_cpus,
196 .per_cpu_save_state_size = save_state_size,
197 .num_concurrent_save_states = 1,
198 .handler = (smm_handler_t)&cpu_smm_do_relocation,
199 };
200
201 if (smm_setup_relocation_handler(&smm_params))
202 return -1;
203
204 adjust_apic_id_map(&smm_params);
205
206 return 0;
207}
208
209static int install_permanent_handler(int num_cpus)
210{
211#ifndef CONFIG_SMM_RESERVED_SIZE
212#error CONFIG_SMM_RESERVED_SIZE must be set.
213#endif
214 /* There are num_cpus concurrent stacks and num_cpus concurrent save
215 * state areas. Lastly, set the stack size to the save state size. */
216 int save_state_size = sizeof(em64t100_smm_state_save_area_t);
217 struct smm_loader_params smm_params = {
218 .per_cpu_stack_size = save_state_size,
219 .num_concurrent_stacks = num_cpus,
220 .per_cpu_save_state_size = save_state_size,
221 .num_concurrent_save_states = num_cpus,
222 };
223 const int tseg_size = smm_region_size() - CONFIG_SMM_RESERVED_SIZE;
224
225 printk(BIOS_DEBUG, "Installing SMM handler to 0x%08x\n",
226 relo_attrs.smbase);
227
228 if (smm_load_module((void *)relo_attrs.smbase, tseg_size, &smm_params))
229 return -1;
230
231 adjust_apic_id_map(&smm_params);
232
233 return 0;
234}
235
236static int smm_load_handlers(void)
237{
238 /* All range registers are aligned to 4KiB */
239 const uint32_t rmask = ~((1 << 12) - 1);
240 const struct pattrs *pattrs = pattrs_get();
241
242 /* Initialize global tracking state. */
243 relo_attrs.smbase = (uint32_t)smm_region_start();
244 relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK;
245 relo_attrs.smrr_mask = ~(smm_region_size() - 1) & rmask;
246 relo_attrs.smrr_mask |= MTRRphysMaskValid;
247
248 /* Install handlers. */
249 if (install_relocation_handler(pattrs->num_cpus) < 0) {
250 printk(BIOS_ERR, "Unable to install SMM relocation handler.\n");
251 return -1;
252 }
253
254 if (install_permanent_handler(pattrs->num_cpus) < 0) {
255 printk(BIOS_ERR, "Unable to install SMM permanent handler.\n");
256 return -1;
257 }
258
259 /* Ensure the SMM handlers hit DRAM before performing first SMI. */
260 wbinvd();
261
262 return 0;
263}
264
265static void smm_relocate(void *unused)
266{
267
268 /* Load relocation and permanent handler. */
269 if (boot_cpu()) {
270 if (smm_load_handlers() < 0) {
271 printk(BIOS_ERR, "Error loading SMM handlers.\n");
272 return;
273 }
274 southcluster_smm_clear_state();
275 }
276
277 /* Relocate SMM space. */
278 smm_initiate_relocation();
279}
280
281static void enable_smis(void *unused)
282{
283 southcluster_smm_enable_smi();
284}
285#endif