blob: 9992fc54aa53a09379e243bf83b02a69d7239138 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -06002
Marshall Dawsonb6172112017-09-13 17:47:31 -06003#include <cpu/cpu.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -06004#include <cpu/x86/mp.h>
5#include <cpu/x86/mtrr.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -06006#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +03007#include <cpu/x86/smm.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +02008#include <cpu/amd/msr.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +03009#include <cpu/amd/amd64_save_state.h>
Marshall Dawson178e65d2017-10-20 13:20:25 -060010#include <cpu/x86/lapic.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060011#include <device/device.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +020012#include <device/pci_ops.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060013#include <soc/pci_devs.h>
14#include <soc/cpu.h>
15#include <soc/northbridge.h>
Marshall Dawsonb6172112017-09-13 17:47:31 -060016#include <soc/smi.h>
Marshall Dawson0814b122018-01-10 11:35:24 -070017#include <soc/iomap.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060018#include <console/console.h>
Felix Heldaecca752021-02-08 22:14:17 +010019#include <amdblocks/psp.h>
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060020
21/*
Marshall Dawsonb6172112017-09-13 17:47:31 -060022 * MP and SMM loading initialization.
23 */
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030024struct smm_relocation_params {
25 msr_t tseg_base;
26 msr_t tseg_mask;
Marshall Dawsonb6172112017-09-13 17:47:31 -060027};
28
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030029static struct smm_relocation_params smm_reloc_params;
Marshall Dawsonb6172112017-09-13 17:47:31 -060030
31/*
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060032 * 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{
40 x86_setup_mtrrs_with_detect();
41 x86_mtrr_check();
42}
43
44static int get_cpu_count(void)
45{
Martin Roth1956a002018-10-30 22:31:40 -060046 return (pci_read_config16(SOC_HT_DEV, D18F0_CPU_CNT) & CPU_CNT_MASK)
Richard Spiegel41baf0c2018-10-22 13:57:18 -070047 + 1;
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060048}
49
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030050static void fill_in_relocation_params(struct smm_relocation_params *params)
51{
52 uintptr_t tseg_base;
53 size_t tseg_size;
54
55 smm_region(&tseg_base, &tseg_size);
56
57 params->tseg_base.lo = ALIGN_DOWN(tseg_base, 128 * KiB);
58 params->tseg_base.hi = 0;
59 params->tseg_mask.lo = ALIGN_DOWN(~(tseg_size - 1), 128 * KiB);
60 params->tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
61
62 params->tseg_mask.lo |= SMM_TSEG_WB;
63}
64
Marshall Dawsonb6172112017-09-13 17:47:31 -060065static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
66 size_t *smm_save_state_size)
67{
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030068 printk(BIOS_DEBUG, "Setting up SMI for CPU\n");
Marshall Dawsonb6172112017-09-13 17:47:31 -060069
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030070 fill_in_relocation_params(&smm_reloc_params);
Marshall Dawsonb6172112017-09-13 17:47:31 -060071
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030072 smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
Marshall Dawsonb6172112017-09-13 17:47:31 -060073 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
74}
75
76static void relocation_handler(int cpu, uintptr_t curr_smbase,
77 uintptr_t staggered_smbase)
78{
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030079 struct smm_relocation_params *relo_params = &smm_reloc_params;
Marshall Dawsonb6172112017-09-13 17:47:31 -060080 amd64_smm_state_save_area_t *smm_state;
81
Kyösti Mälkki0d4d09c2019-08-06 01:44:58 +030082 wrmsr(SMM_ADDR_MSR, relo_params->tseg_base);
83 wrmsr(SMM_MASK_MSR, relo_params->tseg_mask);
84
Marshall Dawsonb6172112017-09-13 17:47:31 -060085 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
86 smm_state->smbase = staggered_smbase;
87}
88
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060089static const struct mp_ops mp_ops = {
90 .pre_mp_init = pre_mp_init,
91 .get_cpu_count = get_cpu_count,
Marshall Dawsonb6172112017-09-13 17:47:31 -060092 .get_smm_info = get_smm_info,
93 .relocation_handler = relocation_handler,
Kyösti Mälkki87e67962020-05-31 09:59:14 +030094 .post_mp_init = global_smi_enable,
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060095};
96
Kyösti Mälkki79e12ab2020-05-31 09:21:07 +030097void mp_init_cpus(struct bus *cpu_bus)
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -060098{
99 /* Clear for take-off */
Kyösti Mälkki79e12ab2020-05-31 09:21:07 +0300100 if (mp_init_with_smm(cpu_bus, &mp_ops) < 0)
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600101 printk(BIOS_ERR, "MP initialization failure.\n");
Marshall Dawson8f031d82018-04-09 22:15:06 -0600102
103 /* The flash is now no longer cacheable. Reset to WP for performance. */
104 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
Marshall Dawson2e49cf122018-08-03 17:05:22 -0600105
106 set_warm_reset_flag();
Marshall Dawsona7bfbbe2017-09-13 17:24:53 -0600107}
Marshall Dawson178e65d2017-10-20 13:20:25 -0600108
Marshall Dawson74473ec2018-08-05 10:42:17 -0600109static void model_15_init(struct device *dev)
110{
111 check_mca();
Marshall Dawson178e65d2017-10-20 13:20:25 -0600112 setup_lapic();
Marshall Dawson638bd132018-09-14 10:16:40 -0600113
114 /*
115 * Per AMD, sync an undocumented MSR with the PSP base address.
116 * Experiments showed that if you write to the MSR after it has
117 * been previously programmed, it causes a general protection fault.
118 * Also, the MSR survives warm reset and S3 cycles, so we need to
119 * test if it was previously written before writing to it.
120 */
121 msr_t psp_msr;
122 uint32_t psp_bar; /* Note: NDA BKDG names this 32-bit register BAR3 */
123 psp_bar = pci_read_config32(SOC_PSP_DEV, PCI_BASE_ADDRESS_4);
124 psp_bar &= ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Felix Heldaecca752021-02-08 22:14:17 +0100125 psp_msr = rdmsr(MSR_PSP_ADDR);
Marshall Dawson638bd132018-09-14 10:16:40 -0600126 if (psp_msr.lo == 0) {
127 psp_msr.lo = psp_bar;
Felix Heldaecca752021-02-08 22:14:17 +0100128 wrmsr(MSR_PSP_ADDR, psp_msr);
Marshall Dawson638bd132018-09-14 10:16:40 -0600129 }
Marshall Dawson178e65d2017-10-20 13:20:25 -0600130}
131
132static struct device_operations cpu_dev_ops = {
133 .init = model_15_init,
134};
135
136static struct cpu_device_id cpu_table[] = {
Richard Spiegel9247e862019-06-28 09:18:47 -0700137 { X86_VENDOR_AMD, 0x660f01 },
Marshall Dawson178e65d2017-10-20 13:20:25 -0600138 { X86_VENDOR_AMD, 0x670f00 },
139 { 0, 0 },
140};
141
142static const struct cpu_driver model_15 __cpu_driver = {
143 .ops = &cpu_dev_ops,
144 .id_table = cpu_table,
145};