blob: a08f1f0352c15f26dc526a1c91f54e4278c94086 [file] [log] [blame]
Ravi Sarawadi9d903a12016-03-04 21:33:04 -08001/*
2 * This file is part of the coreboot project.
3 *
Pratik Prajapatidc194e22017-08-29 14:27:07 -07004 * Copyright (C) 2015-2017 Intel Corp.
Mario Scheithauerd0e51332017-10-24 16:57:26 +02005 * Copyright (C) 2017 Siemens AG, Inc.
Ravi Sarawadi9d903a12016-03-04 21:33:04 -08006 * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.)
7 * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Martin Rothebabfad2016-04-10 11:09:16 -060013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080018 */
19
Nico Huberaa4d9b92019-02-01 14:20:37 +010020#include <arch/acpi.h>
Pratik Prajapatidc194e22017-08-29 14:27:07 -070021#include <assert.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080022#include <console/console.h>
Pratik Prajapatidc194e22017-08-29 14:27:07 -070023#include "chip.h"
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080024#include <cpu/cpu.h>
25#include <cpu/x86/cache.h>
26#include <cpu/x86/mp.h>
John Zhao31569342016-08-23 16:38:05 -070027#include <cpu/intel/microcode.h>
Barnali Sarkar1e6b9802017-08-07 18:26:31 +053028#include <cpu/intel/turbo.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080029#include <cpu/x86/msr.h>
30#include <cpu/x86/mtrr.h>
31#include <device/device.h>
32#include <device/pci.h>
Barnali Sarkar1e6b9802017-08-07 18:26:31 +053033#include <fsp/api.h>
Brandon Breitensteina86d1b82017-06-08 17:32:02 -070034#include <fsp/memmap.h>
Barnali Sarkar66fe0c42017-05-23 18:17:14 +053035#include <intelblocks/cpulib.h>
Aaron Durbinefc92a82017-06-08 10:54:59 -050036#include <intelblocks/fast_spi.h>
Barnali Sarkar1e6b9802017-08-07 18:26:31 +053037#include <intelblocks/mp_init.h>
Barnali Sarkar66fe0c42017-05-23 18:17:14 +053038#include <intelblocks/msr.h>
Pratik Prajapatidc194e22017-08-29 14:27:07 -070039#include <intelblocks/sgx.h>
Brandon Breitensteina86d1b82017-06-08 17:32:02 -070040#include <intelblocks/smm.h>
Ravi Sarawadiec7293652016-09-09 14:08:50 -070041#include <reg_script.h>
Barnali Sarkar1e6b9802017-08-07 18:26:31 +053042#include <romstage_handoff.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080043#include <soc/cpu.h>
Ravi Sarawadiec7293652016-09-09 14:08:50 -070044#include <soc/iomap.h>
Pratik Prajapatidc194e22017-08-29 14:27:07 -070045#include <soc/pci_devs.h>
Andrey Petrov3b637532016-11-30 17:39:16 -080046#include <soc/pm.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080047
Ravi Sarawadiec7293652016-09-09 14:08:50 -070048static const struct reg_script core_msr_script[] = {
Cole Nelsonf357c252017-05-16 11:38:59 -070049#if !IS_ENABLED(CONFIG_SOC_INTEL_GLK)
Ravi Sarawadiec7293652016-09-09 14:08:50 -070050 /* Enable C-state and IO/MWAIT redirect */
Elyes HAOUAS4e6b7902018-10-02 08:44:47 +020051 REG_MSR_WRITE(MSR_PKG_CST_CONFIG_CONTROL,
Ravi Sarawadiec7293652016-09-09 14:08:50 -070052 (PKG_C_STATE_LIMIT_C2_MASK | CORE_C_STATE_LIMIT_C10_MASK
53 | IO_MWAIT_REDIRECT_MASK | CST_CFG_LOCK_MASK)),
54 /* Power Management I/O base address for I/O trapping to C-states */
55 REG_MSR_WRITE(MSR_PMG_IO_CAPTURE_BASE,
56 (ACPI_PMIO_CST_REG | (PMG_IO_BASE_CST_RNG_BLK_SIZE << 16))),
Venkateswarlu Vinjamuri362180a2016-10-31 17:03:55 -070057 /* Disable support for MONITOR and MWAIT instructions */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +020058 REG_MSR_RMW(IA32_MISC_ENABLE, ~MONITOR_MWAIT_DIS_MASK, 0),
Cole Nelsonf357c252017-05-16 11:38:59 -070059#endif
Cole Nelson9d0950f2018-06-12 10:02:49 -070060 /* Disable C1E */
61 REG_MSR_RMW(MSR_POWER_CTL, ~POWER_CTL_C1E_MASK, 0),
Nelson, Cole1cf5b872016-11-11 14:17:37 -080062 /*
63 * Enable and Lock the Advanced Encryption Standard (AES-NI)
64 * feature register
65 */
66 REG_MSR_RMW(MSR_FEATURE_CONFIG, ~FEATURE_CONFIG_RESERVED_MASK,
67 FEATURE_CONFIG_LOCK),
Ravi Sarawadiec7293652016-09-09 14:08:50 -070068 REG_SCRIPT_END
69};
70
Elyes HAOUAS06e83152018-05-24 22:48:14 +020071void soc_core_init(struct device *cpu)
Ravi Sarawadiec7293652016-09-09 14:08:50 -070072{
Pratik Prajapatidc194e22017-08-29 14:27:07 -070073 /* Clear out pending MCEs */
Nico Huberaa4d9b92019-02-01 14:20:37 +010074 /* TODO(adurbin): Some of these banks are core vs package
75 scope. For now every CPU clears every bank. */
Julius Werner175aa692019-03-04 18:21:50 -080076 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_SGX) ||
Nico Huberaa4d9b92019-02-01 14:20:37 +010077 acpi_get_sleep_type() == ACPI_S5)
78 mca_configure(NULL);
Pratik Prajapatidc194e22017-08-29 14:27:07 -070079
Ravi Sarawadiec7293652016-09-09 14:08:50 -070080 /* Set core MSRs */
81 reg_script_run(core_msr_script);
Andrey Petrov3b637532016-11-30 17:39:16 -080082 /*
83 * Enable ACPI PM timer emulation, which also lets microcode know
Barnali Sarkar9e55ff62017-06-05 20:01:14 +053084 * location of ACPI_BASE_ADDRESS. This also enables other features
Andrey Petrov3b637532016-11-30 17:39:16 -080085 * implemented in microcode.
86 */
87 enable_pm_timer_emulation();
Pratik Prajapatidc194e22017-08-29 14:27:07 -070088
89 /* Configure Core PRMRR for SGX. */
90 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_SGX))
91 prmrr_core_configure();
Mario Scheithauerd0e51332017-10-24 16:57:26 +020092
93 /* Set Max Non-Turbo ratio if RAPL is disabled. */
94 if (IS_ENABLED(CONFIG_APL_SKIP_SET_POWER_LIMITS)) {
95 cpu_set_p_state_to_max_non_turbo_ratio();
96 cpu_disable_eist();
Werner Zeh26361862018-11-21 12:36:21 +010097 } else if (IS_ENABLED(CONFIG_APL_SET_MIN_CLOCK_RATIO)) {
98 cpu_set_p_state_to_min_clock_ratio();
99 cpu_disable_eist();
Mario Scheithauerd0e51332017-10-24 16:57:26 +0200100 }
Ravi Sarawadiec7293652016-09-09 14:08:50 -0700101}
102
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530103#if !IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)
Elyes HAOUAS06e83152018-05-24 22:48:14 +0200104static void soc_init_core(struct device *cpu)
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530105{
Pratik Prajapati9cd6a262017-08-14 13:57:46 -0700106 soc_core_init(cpu);
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530107}
108
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800109static struct device_operations cpu_dev_ops = {
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530110 .init = soc_init_core,
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800111};
112
Jonathan Neuschäfer8f06ce32017-11-20 01:56:44 +0100113static const struct cpu_device_id cpu_table[] = {
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800114 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_A0 },
115 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_B0 },
Mario Scheithauer545593d2017-10-24 17:41:19 +0200116 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_E0 },
Hannah Williams3ff14a02017-05-05 16:30:22 -0700117 { X86_VENDOR_INTEL, CPUID_GLK_A0 },
118 { X86_VENDOR_INTEL, CPUID_GLK_B0 },
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800119 { 0, 0 },
120};
121
122static const struct cpu_driver driver __cpu_driver = {
123 .ops = &cpu_dev_ops,
124 .id_table = cpu_table,
125};
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530126#endif
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800127
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700128/*
129 * MP and SMM loading initialization.
130 */
131struct smm_relocation_attrs {
132 uint32_t smbase;
133 uint32_t smrr_base;
134 uint32_t smrr_mask;
135};
136
137static struct smm_relocation_attrs relo_attrs;
138
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530139/*
140 * Do essential initialization tasks before APs can be fired up.
141 *
142 * IF (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)) -
143 * Skip Pre MP init MTRR programming, as MTRRs are mirrored from BSP,
144 * that are set prior to ramstage.
145 * Real MTRRs are programmed after resource allocation.
146 *
147 * Do FSP loading before MP Init to ensure that the FSP component stored in
148 * external stage cache in TSEG does not flush off due to SMM relocation
149 * during MP Init stage.
150 *
151 * ELSE -
152 * Enable MTRRs on the BSP. This creates the MTRR solution that the
153 * APs will use. Otherwise APs will try to apply the incomplete solution
154 * as the BSP is calculating it.
155 */
156static void pre_mp_init(void)
157{
158 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)) {
159 fsps_load(romstage_handoff_is_resume());
160 return;
161 }
162 x86_setup_mtrrs_with_detect();
163 x86_mtrr_check();
164}
165
166#if !IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_CPU_MPINIT)
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800167static void read_cpu_topology(unsigned int *num_phys, unsigned int *num_virt)
168{
169 msr_t msr;
170 msr = rdmsr(MSR_CORE_THREAD_COUNT);
171 *num_virt = (msr.lo >> 0) & 0xffff;
172 *num_phys = (msr.lo >> 16) & 0xffff;
173}
174
Aaron Durbine72b9d42016-05-03 15:56:24 -0500175/* Find CPU topology */
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530176int get_cpu_count(void)
Aaron Durbine72b9d42016-05-03 15:56:24 -0500177{
178 unsigned int num_virt_cores, num_phys_cores;
179
180 read_cpu_topology(&num_phys_cores, &num_virt_cores);
181
182 printk(BIOS_DEBUG, "Detected %u core, %u thread CPU.\n",
183 num_phys_cores, num_virt_cores);
184
185 return num_virt_cores;
186}
187
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530188void get_microcode_info(const void **microcode, int *parallel)
John Zhao31569342016-08-23 16:38:05 -0700189{
190 *microcode = intel_microcode_find();
191 *parallel = 1;
Barnali Sarkar97daf982017-06-07 13:47:51 +0530192
193 /* Make sure BSP is using the microcode from cbfs */
194 intel_microcode_load_unlocked(*microcode);
John Zhao31569342016-08-23 16:38:05 -0700195}
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530196#endif
John Zhao31569342016-08-23 16:38:05 -0700197
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700198static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
199 size_t *smm_save_state_size)
200{
201 void *smm_base;
202 size_t smm_size;
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700203 void *handler_base;
204 size_t handler_size;
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700205
206 /* All range registers are aligned to 4KiB */
207 const uint32_t rmask = ~((1 << 12) - 1);
208
209 /* Initialize global tracking state. */
Subrata Banikce90c782017-08-30 16:53:20 +0530210 smm_region_info(&smm_base, &smm_size);
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700211 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
212
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700213 relo_attrs.smbase = (uint32_t)smm_base;
214 relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK;
215 relo_attrs.smrr_mask = ~(smm_size - 1) & rmask;
216 relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID;
217
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700218 *perm_smbase = (uintptr_t)handler_base;
219 *perm_smsize = handler_size;
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700220 *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t);
221}
222
223static void relocation_handler(int cpu, uintptr_t curr_smbase,
224 uintptr_t staggered_smbase)
225{
226 msr_t smrr;
227 em64t100_smm_state_save_area_t *smm_state;
228 /* Set up SMRR. */
229 smrr.lo = relo_attrs.smrr_base;
230 smrr.hi = 0;
Arthur Heymanse750b38e2018-07-20 23:31:59 +0200231 wrmsr(IA32_SMRR_PHYS_BASE, smrr);
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700232 smrr.lo = relo_attrs.smrr_mask;
233 smrr.hi = 0;
Arthur Heymanse750b38e2018-07-20 23:31:59 +0200234 wrmsr(IA32_SMRR_PHYS_MASK, smrr);
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700235 smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase);
236 smm_state->smbase = staggered_smbase;
237}
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800238/*
239 * CPU initialization recipe
240 *
241 * Note that no microcode update is passed to the init function. CSE updates
242 * the microcode on all cores before releasing them from reset. That means that
243 * the BSP and all APs will come up with the same microcode revision.
244 */
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700245
246static void post_mp_init(void)
247{
Subrata Banik47a655c2017-12-14 18:22:13 +0530248 smm_southbridge_enable(PWRBTN_EN | GBL_EN);
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700249
250 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_SGX))
Subrata Banik33374972018-04-24 13:45:30 +0530251 mp_run_on_all_cpus(sgx_configure, NULL, 2000);
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700252}
253
Aaron Durbine72b9d42016-05-03 15:56:24 -0500254static const struct mp_ops mp_ops = {
255 .pre_mp_init = pre_mp_init,
256 .get_cpu_count = get_cpu_count,
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700257 .get_smm_info = get_smm_info,
John Zhao31569342016-08-23 16:38:05 -0700258 .get_microcode_info = get_microcode_info,
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700259 .pre_mp_smm_init = smm_southbridge_clear_state,
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700260 .relocation_handler = relocation_handler,
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700261 .post_mp_init = post_mp_init,
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800262};
263
Pratik Prajapati9cd6a262017-08-14 13:57:46 -0700264void soc_init_cpus(struct bus *cpu_bus)
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800265{
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800266 /* Clear for take-off */
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530267 if (mp_init_with_smm(cpu_bus, &mp_ops))
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800268 printk(BIOS_ERR, "MP initialization failure.\n");
Barnali Sarkar1e6b9802017-08-07 18:26:31 +0530269}
270
271void apollolake_init_cpus(struct device *dev)
272{
273 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_CPU_MPINIT))
274 return;
Pratik Prajapati9cd6a262017-08-14 13:57:46 -0700275 soc_init_cpus(dev->link_list);
Aaron Durbinbf696f52016-11-10 20:04:19 -0600276
277 /* Temporarily cache the memory-mapped boot media. */
Aaron Durbinefc92a82017-06-08 10:54:59 -0500278 if (IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED) &&
279 IS_ENABLED(CONFIG_BOOT_DEVICE_SPI_FLASH))
280 fast_spi_cache_bios_region();
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800281}
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700282
283void cpu_lock_sgx_memory(void)
284{
285 /* Do nothing because MCHECK while loading microcode and enabling
286 * IA untrusted mode takes care of necessary locking */
287}
288
289int soc_fill_sgx_param(struct sgx_param *sgx_param)
290{
Elyes HAOUAS06e83152018-05-24 22:48:14 +0200291 struct device *dev = SA_DEV_ROOT;
Pratik Prajapatidc194e22017-08-29 14:27:07 -0700292 assert(dev != NULL);
293 config_t *conf = dev->chip_info;
294
295 if (!conf) {
296 printk(BIOS_ERR, "Failed to get chip_info for SGX param\n");
297 return -1;
298 }
299
300 sgx_param->enable = conf->sgx_enable;
301 return 0;
302}