blob: 4d6e98d221447684cc48d7de9dfb30a6a46f528d [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth5c354b92019-04-22 14:55:16 -06002
3#include <cpu/cpu.h>
4#include <cpu/x86/mp.h>
5#include <cpu/x86/mtrr.h>
6#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +03007#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -06008#include <cpu/amd/msr.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +03009#include <cpu/amd/amd64_save_state.h>
Martin Roth5c354b92019-04-22 14:55:16 -060010#include <cpu/x86/lapic.h>
11#include <device/device.h>
12#include <device/pci_ops.h>
13#include <soc/pci_devs.h>
14#include <soc/cpu.h>
Raul E Rangelcd39a412020-05-07 15:16:15 -060015#include <soc/reset.h>
Martin Roth5c354b92019-04-22 14:55:16 -060016#include <soc/smi.h>
17#include <soc/iomap.h>
18#include <console/console.h>
Zheng Bao6ba591b2020-06-09 09:47:06 +080019#include <cpu/amd/microcode.h>
Martin Roth5c354b92019-04-22 14:55:16 -060020
21/*
22 * MP and SMM loading initialization.
23 */
Kyösti Mälkki86997242019-08-06 01:44:58 +030024struct smm_relocation_params {
25 msr_t tseg_base;
26 msr_t tseg_mask;
Martin Roth5c354b92019-04-22 14:55:16 -060027};
28
Kyösti Mälkki86997242019-08-06 01:44:58 +030029static struct smm_relocation_params smm_reloc_params;
Martin Roth5c354b92019-04-22 14:55:16 -060030
31/*
32 * Do essential initialization tasks before APs can be fired up -
33 *
34 * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
35 * creates the MTRR solution that the APs will use. Otherwise APs will try to
36 * apply the incomplete solution as the BSP is calculating it.
37 */
38static void pre_mp_init(void)
39{
Aaron Durbina2c045b2020-05-28 10:19:18 -060040 x86_setup_mtrrs_with_detect_no_above_4gb();
Martin Roth5c354b92019-04-22 14:55:16 -060041 x86_mtrr_check();
42}
43
Marshall Dawson34c30562019-07-16 15:18:00 -060044int get_cpu_count(void)
Martin Roth5c354b92019-04-22 14:55:16 -060045{
Marshall Dawson34c30562019-07-16 15:18:00 -060046 return 1 + (cpuid_ecx(0x80000008) & 0xff);
Martin Roth5c354b92019-04-22 14:55:16 -060047}
48
Chris Wange2497d02020-08-03 22:36:13 +080049static void set_cstate_io_addr(void)
50{
51 msr_t cst_addr;
52
53 cst_addr.hi = 0;
54 cst_addr.lo = ACPI_CPU_CONTROL;
55 wrmsr(MSR_CSTATE_ADDRESS, cst_addr);
56}
57
Kyösti Mälkki86997242019-08-06 01:44:58 +030058static void fill_in_relocation_params(struct smm_relocation_params *params)
59{
60 uintptr_t tseg_base;
61 size_t tseg_size;
62
63 smm_region(&tseg_base, &tseg_size);
64
65 params->tseg_base.lo = ALIGN_DOWN(tseg_base, 128 * KiB);
66 params->tseg_base.hi = 0;
67 params->tseg_mask.lo = ALIGN_DOWN(~(tseg_size - 1), 128 * KiB);
68 params->tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
69
70 params->tseg_mask.lo |= SMM_TSEG_WB;
71}
72
Martin Roth5c354b92019-04-22 14:55:16 -060073static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
74 size_t *smm_save_state_size)
75{
Kyösti Mälkki86997242019-08-06 01:44:58 +030076 printk(BIOS_DEBUG, "Setting up SMI for CPU\n");
Martin Roth5c354b92019-04-22 14:55:16 -060077
Kyösti Mälkki86997242019-08-06 01:44:58 +030078 fill_in_relocation_params(&smm_reloc_params);
Martin Roth5c354b92019-04-22 14:55:16 -060079
Kyösti Mälkki86997242019-08-06 01:44:58 +030080 smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
Martin Roth5c354b92019-04-22 14:55:16 -060081 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
82}
83
84static void relocation_handler(int cpu, uintptr_t curr_smbase,
85 uintptr_t staggered_smbase)
86{
Kyösti Mälkki86997242019-08-06 01:44:58 +030087 struct smm_relocation_params *relo_params = &smm_reloc_params;
Martin Roth5c354b92019-04-22 14:55:16 -060088 amd64_smm_state_save_area_t *smm_state;
89
Kyösti Mälkki86997242019-08-06 01:44:58 +030090 wrmsr(SMM_ADDR_MSR, relo_params->tseg_base);
91 wrmsr(SMM_MASK_MSR, relo_params->tseg_mask);
92
Martin Roth5c354b92019-04-22 14:55:16 -060093 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
94 smm_state->smbase = staggered_smbase;
95}
96
97static const struct mp_ops mp_ops = {
98 .pre_mp_init = pre_mp_init,
99 .get_cpu_count = get_cpu_count,
100 .get_smm_info = get_smm_info,
101 .relocation_handler = relocation_handler,
Kyösti Mälkki87e67962020-05-31 09:59:14 +0300102 .post_mp_init = global_smi_enable,
Martin Roth5c354b92019-04-22 14:55:16 -0600103};
104
Kyösti Mälkki79e12ab2020-05-31 09:21:07 +0300105void mp_init_cpus(struct bus *cpu_bus)
Martin Roth5c354b92019-04-22 14:55:16 -0600106{
107 /* Clear for take-off */
Kyösti Mälkki79e12ab2020-05-31 09:21:07 +0300108 if (mp_init_with_smm(cpu_bus, &mp_ops) < 0)
Martin Roth5c354b92019-04-22 14:55:16 -0600109 printk(BIOS_ERR, "MP initialization failure.\n");
110
Raul E Rangel93375f22020-06-05 15:48:21 -0600111 /* pre_mp_init made the flash not cacheable. Reset to WP for performance. */
Martin Roth5c354b92019-04-22 14:55:16 -0600112 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
113
114 set_warm_reset_flag();
115}
116
Marshall Dawson34c30562019-07-16 15:18:00 -0600117static void model_17_init(struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -0600118{
119 check_mca();
120 setup_lapic();
Chris Wange2497d02020-08-03 22:36:13 +0800121 set_cstate_io_addr();
Zheng Bao6ba591b2020-06-09 09:47:06 +0800122
123 amd_update_microcode_from_cbfs();
Martin Roth5c354b92019-04-22 14:55:16 -0600124}
125
126static struct device_operations cpu_dev_ops = {
Marshall Dawson34c30562019-07-16 15:18:00 -0600127 .init = model_17_init,
Martin Roth5c354b92019-04-22 14:55:16 -0600128};
129
130static struct cpu_device_id cpu_table[] = {
Felix Heldab114c92020-05-22 02:40:40 +0200131 { X86_VENDOR_AMD, PICASSO_B0_CPUID },
132 { X86_VENDOR_AMD, PICASSO_B1_CPUID },
133 { X86_VENDOR_AMD, RAVEN2_A0_CPUID },
134 { X86_VENDOR_AMD, RAVEN2_A1_CPUID },
Martin Roth5c354b92019-04-22 14:55:16 -0600135 { 0, 0 },
136};
137
Marshall Dawson34c30562019-07-16 15:18:00 -0600138static const struct cpu_driver model_17 __cpu_driver = {
Martin Roth5c354b92019-04-22 14:55:16 -0600139 .ops = &cpu_dev_ops,
140 .id_table = cpu_table,
141};