blob: c84b7565875ed80cb3d0b1d242dc40a53a0b2261 [file] [log] [blame]
Arthur Heymanse69d2df2020-12-01 18:29:13 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
Kyösti Mälkki93d759f2022-11-11 19:46:05 +02004#include <cpu/amd/amd64_save_state.h>
5#include <cpu/intel/smm_reloc.h>
6#include <cpu/x86/legacy_save_state.h>
Arthur Heymanse69d2df2020-12-01 18:29:13 +01007#include <cpu/x86/mp.h>
Kyösti Mälkki93d759f2022-11-11 19:46:05 +02008#include <cpu/x86/smm.h>
9#include <mainboard/emulation/qemu-i440fx/fw_cfg.h>
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020010#include <stddef.h>
Arthur Heymanse69d2df2020-12-01 18:29:13 +010011#include <stdint.h>
Arthur Heymanse69d2df2020-12-01 18:29:13 +010012
13static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
14 size_t *smm_save_state_size)
15{
16 printk(BIOS_DEBUG, "Setting up SMI for CPU\n");
17
Arthur Heymans4db2e8e2021-10-28 16:48:36 +020018 if (CONFIG(SMM_TSEG))
19 smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
20
21 if (CONFIG(SMM_ASEG)) {
22 smm_open_aseg();
23 *perm_smbase = 0xa0000;
24 *perm_smsize = 0x10000;
25 }
Arthur Heymanse69d2df2020-12-01 18:29:13 +010026
27 /* FIXME: on X86_64 the save state size is smaller than the size of the SMM stub */
28 *smm_save_state_size = sizeof(amd64_smm_state_save_area_t);
Benjamin Doron8c5994f2021-03-20 15:41:57 +000029 printk(BIOS_DEBUG, "Save state size: 0x%zx bytes\n", *smm_save_state_size);
Arthur Heymanse69d2df2020-12-01 18:29:13 +010030}
31
32/*
33 * The relocation work is actually performed in SMM context, but the code
34 * resides in the ramstage module. This occurs by trampolining from the default
35 * SMRAM entry point to here.
36 */
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020037
Martin Rothe13b2632022-11-12 21:09:08 -070038union __packed save_state {
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020039 amd64_smm_state_save_area_t amd64;
40 struct {
41 char _reserved[sizeof(amd64_smm_state_save_area_t)
42 - sizeof(legacy_smm_state_save_area_t)];
43 legacy_smm_state_save_area_t legacy;
44 };
45};
46
47_Static_assert(sizeof(union save_state) == sizeof(amd64_smm_state_save_area_t),
48 "Incorrect save state union size");
49
50_Static_assert(offsetof(union save_state, amd64.smm_revision)
51 == offsetof(union save_state, legacy.smm_revision),
52 "Incompatible SMM save state revision offset");
53
Arthur Heymanse69d2df2020-12-01 18:29:13 +010054static void relocation_handler(int cpu, uintptr_t curr_smbase,
55 uintptr_t staggered_smbase)
56{
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020057 union save_state *save_state =
58 (void *)(curr_smbase + SMM_DEFAULT_SIZE - sizeof(*save_state));
59
Arthur Heymanse69d2df2020-12-01 18:29:13 +010060 u32 smbase = staggered_smbase;
61
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020062 /* The SMM save state revision is always at a compatible offset */
63 const u32 revision = save_state->legacy.smm_revision;
64 switch (revision) {
65 case 0x00020000:
66 save_state->legacy.smbase = smbase;
67 break;
68 case 0x00020064:
69 save_state->amd64.smbase = smbase;
70 break;
71 default:
72 printk(BIOS_ERR, "Unknown SMM revision 0x%x, not relocating SMM\n", revision);
73 return;
74 };
Arthur Heymanse69d2df2020-12-01 18:29:13 +010075
76 printk(BIOS_DEBUG, "In relocation handler: cpu %d\n", cpu);
Arthur Heymans0c9fa6f2021-06-02 11:46:20 +020077 printk(BIOS_DEBUG, "SMM revision: 0x%08x\n", revision);
Arthur Heymanse69d2df2020-12-01 18:29:13 +010078 printk(BIOS_DEBUG, "New SMBASE=0x%08x\n", smbase);
79}
80
81static void post_mp_init(void)
82{
83 /* Now that all APs have been relocated as well as the BSP let SMIs start flowing. */
84 global_smi_enable();
85
86 /* Lock down the SMRAM space. */
87 smm_lock();
88}
89
90const struct mp_ops mp_ops_with_smm = {
91 .get_cpu_count = fw_cfg_max_cpus,
92 .get_smm_info = get_smm_info,
93 .pre_mp_smm_init = smm_southbridge_clear_state,
94 .relocation_handler = relocation_handler,
95 .post_mp_init = post_mp_init,
96};