blob: 2325994a9c55bd4eeecf4f69bd082736f84a4504 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Roth5c354b92019-04-22 14:55:16 -06003
4#include <cpu/cpu.h>
5#include <cpu/x86/mp.h>
6#include <cpu/x86/mtrr.h>
7#include <cpu/x86/msr.h>
Kyösti Mälkkib2a5f0b2019-08-04 19:54:32 +03008#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -06009#include <cpu/amd/msr.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +030010#include <cpu/amd/amd64_save_state.h>
Martin Roth5c354b92019-04-22 14:55:16 -060011#include <cpu/x86/lapic.h>
12#include <device/device.h>
13#include <device/pci_ops.h>
14#include <soc/pci_devs.h>
15#include <soc/cpu.h>
16#include <soc/northbridge.h>
17#include <soc/smi.h>
18#include <soc/iomap.h>
19#include <console/console.h>
20
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{
40 x86_setup_mtrrs_with_detect();
41 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
Kyösti Mälkki86997242019-08-06 01:44:58 +030049static void fill_in_relocation_params(struct smm_relocation_params *params)
50{
51 uintptr_t tseg_base;
52 size_t tseg_size;
53
54 smm_region(&tseg_base, &tseg_size);
55
56 params->tseg_base.lo = ALIGN_DOWN(tseg_base, 128 * KiB);
57 params->tseg_base.hi = 0;
58 params->tseg_mask.lo = ALIGN_DOWN(~(tseg_size - 1), 128 * KiB);
59 params->tseg_mask.hi = ((1 << (cpu_phys_address_size() - 32)) - 1);
60
61 params->tseg_mask.lo |= SMM_TSEG_WB;
62}
63
Martin Roth5c354b92019-04-22 14:55:16 -060064static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
65 size_t *smm_save_state_size)
66{
Kyösti Mälkki86997242019-08-06 01:44:58 +030067 printk(BIOS_DEBUG, "Setting up SMI for CPU\n");
Martin Roth5c354b92019-04-22 14:55:16 -060068
Kyösti Mälkki86997242019-08-06 01:44:58 +030069 fill_in_relocation_params(&smm_reloc_params);
Martin Roth5c354b92019-04-22 14:55:16 -060070
Kyösti Mälkki86997242019-08-06 01:44:58 +030071 smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
Martin Roth5c354b92019-04-22 14:55:16 -060072 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
73}
74
75static void relocation_handler(int cpu, uintptr_t curr_smbase,
76 uintptr_t staggered_smbase)
77{
Kyösti Mälkki86997242019-08-06 01:44:58 +030078 struct smm_relocation_params *relo_params = &smm_reloc_params;
Martin Roth5c354b92019-04-22 14:55:16 -060079 amd64_smm_state_save_area_t *smm_state;
80
Kyösti Mälkki86997242019-08-06 01:44:58 +030081 wrmsr(SMM_ADDR_MSR, relo_params->tseg_base);
82 wrmsr(SMM_MASK_MSR, relo_params->tseg_mask);
83
Martin Roth5c354b92019-04-22 14:55:16 -060084 smm_state = (void *)(SMM_AMD64_SAVE_STATE_OFFSET + curr_smbase);
85 smm_state->smbase = staggered_smbase;
86}
87
88static const struct mp_ops mp_ops = {
89 .pre_mp_init = pre_mp_init,
90 .get_cpu_count = get_cpu_count,
91 .get_smm_info = get_smm_info,
92 .relocation_handler = relocation_handler,
93 .post_mp_init = enable_smi_generation,
94};
95
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060096void picasso_init_cpus(struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -060097{
98 /* Clear for take-off */
99 if (mp_init_with_smm(dev->link_list, &mp_ops) < 0)
100 printk(BIOS_ERR, "MP initialization failure.\n");
101
102 /* The flash is now no longer cacheable. Reset to WP for performance. */
103 mtrr_use_temp_range(FLASH_BASE_ADDR, CONFIG_ROM_SIZE, MTRR_TYPE_WRPROT);
104
105 set_warm_reset_flag();
106}
107
Marshall Dawson34c30562019-07-16 15:18:00 -0600108static void model_17_init(struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -0600109{
110 check_mca();
111 setup_lapic();
Martin Roth5c354b92019-04-22 14:55:16 -0600112}
113
114static struct device_operations cpu_dev_ops = {
Marshall Dawson34c30562019-07-16 15:18:00 -0600115 .init = model_17_init,
Martin Roth5c354b92019-04-22 14:55:16 -0600116};
117
118static struct cpu_device_id cpu_table[] = {
Marshall Dawson04b41772019-09-04 09:40:50 -0600119 { X86_VENDOR_AMD, 0x810f80 },
Martin Rotheb30e1a2019-12-10 21:50:10 -0700120 { X86_VENDOR_AMD, PICASSO_CPUID },
121 { X86_VENDOR_AMD, RAVEN2_CPUID },
Martin Roth5c354b92019-04-22 14:55:16 -0600122 { 0, 0 },
123};
124
Marshall Dawson34c30562019-07-16 15:18:00 -0600125static const struct cpu_driver model_17 __cpu_driver = {
Martin Roth5c354b92019-04-22 14:55:16 -0600126 .ops = &cpu_dev_ops,
127 .id_table = cpu_table,
128};