blob: 8b8f963e4c9346328920fe6ee622048f910149c3 [file] [log] [blame]
Ravi Sarawadi9d903a12016-03-04 21:33:04 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015-2016 Intel Corp.
5 * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.)
6 * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Martin Rothebabfad2016-04-10 11:09:16 -060012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080017 */
18
19#include <console/console.h>
20#include <cpu/cpu.h>
21#include <cpu/x86/cache.h>
22#include <cpu/x86/mp.h>
John Zhao31569342016-08-23 16:38:05 -070023#include <cpu/intel/microcode.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080024#include <cpu/x86/msr.h>
25#include <cpu/x86/mtrr.h>
26#include <device/device.h>
27#include <device/pci.h>
Ravi Sarawadiec7293652016-09-09 14:08:50 -070028#include <reg_script.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080029#include <soc/cpu.h>
Ravi Sarawadiec7293652016-09-09 14:08:50 -070030#include <soc/iomap.h>
Andrey Petrov3b637532016-11-30 17:39:16 -080031#include <soc/pm.h>
Hannah Williamsd9c84ca2016-05-13 00:47:14 -070032#include <soc/smm.h>
Venkateswarlu Vinjamuri362180a2016-10-31 17:03:55 -070033#include <cpu/intel/turbo.h>
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080034
Ravi Sarawadiec7293652016-09-09 14:08:50 -070035static const struct reg_script core_msr_script[] = {
36 /* Enable C-state and IO/MWAIT redirect */
37 REG_MSR_WRITE(MSR_PMG_CST_CONFIG_CONTROL,
38 (PKG_C_STATE_LIMIT_C2_MASK | CORE_C_STATE_LIMIT_C10_MASK
39 | IO_MWAIT_REDIRECT_MASK | CST_CFG_LOCK_MASK)),
40 /* Power Management I/O base address for I/O trapping to C-states */
41 REG_MSR_WRITE(MSR_PMG_IO_CAPTURE_BASE,
42 (ACPI_PMIO_CST_REG | (PMG_IO_BASE_CST_RNG_BLK_SIZE << 16))),
43 /* Disable C1E */
44 REG_MSR_RMW(MSR_POWER_CTL, ~0x2, 0),
Venkateswarlu Vinjamuri362180a2016-10-31 17:03:55 -070045 /* Disable support for MONITOR and MWAIT instructions */
46 REG_MSR_RMW(MSR_IA32_MISC_ENABLES, ~MONITOR_MWAIT_DIS_MASK, 0),
Nelson, Cole1cf5b872016-11-11 14:17:37 -080047 /*
48 * Enable and Lock the Advanced Encryption Standard (AES-NI)
49 * feature register
50 */
51 REG_MSR_RMW(MSR_FEATURE_CONFIG, ~FEATURE_CONFIG_RESERVED_MASK,
52 FEATURE_CONFIG_LOCK),
Ravi Sarawadiec7293652016-09-09 14:08:50 -070053 REG_SCRIPT_END
54};
55
Andrey Petrov89e39b52016-11-30 17:58:38 -080056static void enable_untrusted_mode(void)
57{
58 msr_t msr = rdmsr(MSR_POWER_MISC);
59 msr.lo |= ENABLE_IA_UNTRUSTED;
60 wrmsr(MSR_POWER_MISC, msr);
61}
62
Ravi Sarawadiec7293652016-09-09 14:08:50 -070063static void soc_core_init(device_t cpu)
64{
65 /* Set core MSRs */
66 reg_script_run(core_msr_script);
Andrey Petrov3b637532016-11-30 17:39:16 -080067 /*
68 * Enable ACPI PM timer emulation, which also lets microcode know
69 * location of ACPI_PMIO_BASE. This also enables other features
70 * implemented in microcode.
71 */
72 enable_pm_timer_emulation();
Andrey Petrov89e39b52016-11-30 17:58:38 -080073 /* Drop privilege level */
74 enable_untrusted_mode();
Ravi Sarawadiec7293652016-09-09 14:08:50 -070075}
76
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080077static struct device_operations cpu_dev_ops = {
Ravi Sarawadiec7293652016-09-09 14:08:50 -070078 .init = soc_core_init,
Ravi Sarawadi9d903a12016-03-04 21:33:04 -080079};
80
81static struct cpu_device_id cpu_table[] = {
82 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_A0 },
83 { X86_VENDOR_INTEL, CPUID_APOLLOLAKE_B0 },
84 { 0, 0 },
85};
86
87static const struct cpu_driver driver __cpu_driver = {
88 .ops = &cpu_dev_ops,
89 .id_table = cpu_table,
90};
91
Hannah Williamsd9c84ca2016-05-13 00:47:14 -070092/*
93 * MP and SMM loading initialization.
94 */
95struct smm_relocation_attrs {
96 uint32_t smbase;
97 uint32_t smrr_base;
98 uint32_t smrr_mask;
99};
100
101static struct smm_relocation_attrs relo_attrs;
102
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800103static void read_cpu_topology(unsigned int *num_phys, unsigned int *num_virt)
104{
105 msr_t msr;
106 msr = rdmsr(MSR_CORE_THREAD_COUNT);
107 *num_virt = (msr.lo >> 0) & 0xffff;
108 *num_phys = (msr.lo >> 16) & 0xffff;
109}
110
111/*
112 * Do essential initialization tasks before APs can be fired up
113 *
114 * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
115 * creates the MTRR solution that the APs will use. Otherwise APs will try to
116 * apply the incomplete solution as the BSP is calculating it.
117 */
Aaron Durbine72b9d42016-05-03 15:56:24 -0500118static void pre_mp_init(void)
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800119{
120 x86_setup_mtrrs_with_detect();
121 x86_mtrr_check();
Martin Roth3674c822016-09-30 08:59:58 -0600122
123 /* Make sure BSP is using the microcode from cbfs */
124 intel_update_microcode_from_cbfs();
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800125}
126
Aaron Durbine72b9d42016-05-03 15:56:24 -0500127/* Find CPU topology */
128static int get_cpu_count(void)
129{
130 unsigned int num_virt_cores, num_phys_cores;
131
132 read_cpu_topology(&num_phys_cores, &num_virt_cores);
133
134 printk(BIOS_DEBUG, "Detected %u core, %u thread CPU.\n",
135 num_phys_cores, num_virt_cores);
136
137 return num_virt_cores;
138}
139
John Zhao31569342016-08-23 16:38:05 -0700140static void get_microcode_info(const void **microcode, int *parallel)
141{
142 *microcode = intel_microcode_find();
143 *parallel = 1;
144}
145
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700146static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
147 size_t *smm_save_state_size)
148{
149 void *smm_base;
150 size_t smm_size;
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700151 void *handler_base;
152 size_t handler_size;
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700153
154 /* All range registers are aligned to 4KiB */
155 const uint32_t rmask = ~((1 << 12) - 1);
156
157 /* Initialize global tracking state. */
158 smm_region(&smm_base, &smm_size);
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700159 smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
160
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700161 relo_attrs.smbase = (uint32_t)smm_base;
162 relo_attrs.smrr_base = relo_attrs.smbase | MTRR_TYPE_WRBACK;
163 relo_attrs.smrr_mask = ~(smm_size - 1) & rmask;
164 relo_attrs.smrr_mask |= MTRR_PHYS_MASK_VALID;
165
Brandon Breitenstein135eae92016-09-30 13:57:12 -0700166 *perm_smbase = (uintptr_t)handler_base;
167 *perm_smsize = handler_size;
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700168 *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t);
169}
170
171static void relocation_handler(int cpu, uintptr_t curr_smbase,
172 uintptr_t staggered_smbase)
173{
174 msr_t smrr;
175 em64t100_smm_state_save_area_t *smm_state;
176 /* Set up SMRR. */
177 smrr.lo = relo_attrs.smrr_base;
178 smrr.hi = 0;
179 wrmsr(SMRR_PHYS_BASE, smrr);
180 smrr.lo = relo_attrs.smrr_mask;
181 smrr.hi = 0;
182 wrmsr(SMRR_PHYS_MASK, smrr);
183 smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase);
184 smm_state->smbase = staggered_smbase;
185}
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800186/*
187 * CPU initialization recipe
188 *
189 * Note that no microcode update is passed to the init function. CSE updates
190 * the microcode on all cores before releasing them from reset. That means that
191 * the BSP and all APs will come up with the same microcode revision.
192 */
Aaron Durbine72b9d42016-05-03 15:56:24 -0500193static const struct mp_ops mp_ops = {
194 .pre_mp_init = pre_mp_init,
195 .get_cpu_count = get_cpu_count,
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700196 .get_smm_info = get_smm_info,
John Zhao31569342016-08-23 16:38:05 -0700197 .get_microcode_info = get_microcode_info,
Hannah Williamsd9c84ca2016-05-13 00:47:14 -0700198 .pre_mp_smm_init = southbridge_smm_clear_state,
199 .relocation_handler = relocation_handler,
200 .post_mp_init = southbridge_smm_enable_smi,
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800201};
202
203void apollolake_init_cpus(device_t dev)
204{
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800205 /* Clear for take-off */
Aaron Durbine72b9d42016-05-03 15:56:24 -0500206 if (mp_init_with_smm(dev->link_list, &mp_ops) < 0)
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800207 printk(BIOS_ERR, "MP initialization failure.\n");
Aaron Durbinbf696f52016-11-10 20:04:19 -0600208
209 /* Temporarily cache the memory-mapped boot media. */
210 if (IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED))
211 mtrr_use_temp_range(-CONFIG_ROM_SIZE, CONFIG_ROM_SIZE,
212 MTRR_TYPE_WRPROT);
Ravi Sarawadi9d903a12016-03-04 21:33:04 -0800213}