Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <console/console.h> |
Kyösti Mälkki | 93d759f | 2022-11-11 19:46:05 +0200 | [diff] [blame^] | 4 | #include <cpu/amd/amd64_save_state.h> |
| 5 | #include <cpu/intel/smm_reloc.h> |
| 6 | #include <cpu/x86/legacy_save_state.h> |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 7 | #include <cpu/x86/mp.h> |
Kyösti Mälkki | 93d759f | 2022-11-11 19:46:05 +0200 | [diff] [blame^] | 8 | #include <cpu/x86/smm.h> |
| 9 | #include <mainboard/emulation/qemu-i440fx/fw_cfg.h> |
Arthur Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 10 | #include <stddef.h> |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 12 | |
| 13 | static 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 Heymans | 4db2e8e | 2021-10-28 16:48:36 +0200 | [diff] [blame] | 18 | 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 Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 26 | |
| 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 Doron | 8c5994f | 2021-03-20 15:41:57 +0000 | [diff] [blame] | 29 | printk(BIOS_DEBUG, "Save state size: 0x%zx bytes\n", *smm_save_state_size); |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 30 | } |
| 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 Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 37 | |
Martin Roth | e13b263 | 2022-11-12 21:09:08 -0700 | [diff] [blame] | 38 | union __packed save_state { |
Arthur Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 39 | 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 Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 54 | static void relocation_handler(int cpu, uintptr_t curr_smbase, |
| 55 | uintptr_t staggered_smbase) |
| 56 | { |
Arthur Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 57 | union save_state *save_state = |
| 58 | (void *)(curr_smbase + SMM_DEFAULT_SIZE - sizeof(*save_state)); |
| 59 | |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 60 | u32 smbase = staggered_smbase; |
| 61 | |
Arthur Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 62 | /* 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 Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 75 | |
| 76 | printk(BIOS_DEBUG, "In relocation handler: cpu %d\n", cpu); |
Arthur Heymans | 0c9fa6f | 2021-06-02 11:46:20 +0200 | [diff] [blame] | 77 | printk(BIOS_DEBUG, "SMM revision: 0x%08x\n", revision); |
Arthur Heymans | e69d2df | 2020-12-01 18:29:13 +0100 | [diff] [blame] | 78 | printk(BIOS_DEBUG, "New SMBASE=0x%08x\n", smbase); |
| 79 | } |
| 80 | |
| 81 | static 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 | |
| 90 | const 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 | }; |