Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 2 | |
Sven Schnelle | 180f81e | 2011-06-28 08:06:18 +0200 | [diff] [blame] | 3 | #ifndef CPU_X86_SMM_H |
| 4 | #define CPU_X86_SMM_H |
| 5 | |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 6 | #include <arch/cpu.h> |
Patrick Rudolph | 41fec86 | 2020-05-06 10:55:12 +0200 | [diff] [blame] | 7 | #include <commonlib/region.h> |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 8 | #include <device/pci_type.h> |
| 9 | #include <device/resource.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 10 | #include <types.h> |
| 11 | |
Aaron Durbin | 1fef1f5 | 2012-12-19 17:15:43 -0600 | [diff] [blame] | 12 | #define SMM_DEFAULT_BASE 0x30000 |
| 13 | #define SMM_DEFAULT_SIZE 0x10000 |
| 14 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 15 | /* used only by C programs so far */ |
| 16 | #define SMM_BASE 0xa0000 |
| 17 | |
Aaron Durbin | 62f100b | 2012-11-07 12:27:29 -0600 | [diff] [blame] | 18 | #define SMM_ENTRY_OFFSET 0x8000 |
| 19 | #define SMM_SAVE_STATE_BEGIN(x) (SMM_ENTRY_OFFSET + (x)) |
| 20 | |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 21 | #define APM_CNT 0xb2 |
Kyösti Mälkki | d151831 | 2020-06-18 13:51:31 +0300 | [diff] [blame] | 22 | #define APM_CNT_NOOP_SMI 0x00 |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 23 | #define APM_CNT_ACPI_DISABLE 0x1e |
| 24 | #define APM_CNT_ACPI_ENABLE 0xe1 |
Kyösti Mälkki | b486f29 | 2020-06-18 14:05:35 +0300 | [diff] [blame] | 25 | #define APM_CNT_ROUTE_ALL_XHCI 0xca |
Duncan Laurie | 911cedf | 2013-07-30 16:05:55 -0700 | [diff] [blame] | 26 | #define APM_CNT_FINALIZE 0xcb |
Marc Jones | 9afc5c0 | 2014-09-24 10:53:48 -0600 | [diff] [blame] | 27 | #define APM_CNT_LEGACY 0xcc |
Kyösti Mälkki | b8cf039 | 2020-06-17 01:00:10 +0300 | [diff] [blame] | 28 | #define APM_CNT_MBI_UPDATE 0xeb |
Marshall Dawson | d2d9382 | 2020-03-16 20:05:05 -0600 | [diff] [blame] | 29 | #define APM_CNT_SMMINFO 0xec |
Patrick Georgi | 9fca297 | 2018-12-03 16:13:17 +0100 | [diff] [blame] | 30 | #define APM_CNT_SMMSTORE 0xed |
Patrick Georgi | d61839c | 2018-12-03 16:10:33 +0100 | [diff] [blame] | 31 | #define APM_CNT_ELOG_GSMI 0xef |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 32 | #define APM_STS 0xb3 |
Stefan Reinauer | 3b38745 | 2009-03-06 19:52:36 +0000 | [diff] [blame] | 33 | |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 34 | #define SMM_PCI_RESOURCE_STORE_NUM_RESOURCES 6 |
| 35 | |
Eugene D. Myers | d205cf7 | 2023-11-02 13:34:56 -0700 | [diff] [blame] | 36 | /* |
| 37 | * SMI Transfer Monitor (STM) descriptor reserved in SMM save state. |
| 38 | */ |
| 39 | #if CONFIG(STM) |
| 40 | #define STM_PSD_SIZE ALIGN_UP(sizeof(TXT_PROCESSOR_SMM_DESCRIPTOR), 0x100) |
| 41 | #else |
| 42 | #define STM_PSD_SIZE 0 |
| 43 | #endif |
| 44 | |
Kyösti Mälkki | b658548 | 2020-06-01 15:11:14 +0300 | [diff] [blame] | 45 | /* Send cmd to APM_CNT with HAVE_SMI_HANDLER checking. */ |
Felix Held | 21710ea | 2024-01-05 19:16:48 +0100 | [diff] [blame] | 46 | enum cb_err apm_control(u8 cmd); |
Kyösti Mälkki | 9a1620f | 2021-01-08 13:27:33 +0200 | [diff] [blame] | 47 | u8 apm_get_apmc(void); |
Kyösti Mälkki | b658548 | 2020-06-01 15:11:14 +0300 | [diff] [blame] | 48 | |
Stefan Reinauer | be7f798 | 2009-03-13 15:42:27 +0000 | [diff] [blame] | 49 | void io_trap_handler(int smif); |
Kyösti Mälkki | 48b3dbc | 2014-12-29 19:36:50 +0200 | [diff] [blame] | 50 | int mainboard_io_trap_handler(int smif); |
Stefan Reinauer | 3b38745 | 2009-03-06 19:52:36 +0000 | [diff] [blame] | 51 | |
Stefan Reinauer | 348a1ba | 2010-03-17 01:51:11 +0000 | [diff] [blame] | 52 | void southbridge_smi_set_eos(void); |
| 53 | |
Kyösti Mälkki | 21ba5ee | 2020-06-10 10:52:24 +0300 | [diff] [blame] | 54 | void global_smi_enable(void); |
| 55 | void global_smi_enable_no_pwrbtn(void); |
| 56 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 57 | void cpu_smi_handler(void); |
| 58 | void northbridge_smi_handler(void); |
| 59 | void southbridge_smi_handler(void); |
Kyösti Mälkki | e31ec29 | 2019-08-10 17:27:01 +0300 | [diff] [blame] | 60 | |
Duncan Laurie | 0edc224 | 2013-04-29 15:04:30 -0700 | [diff] [blame] | 61 | void mainboard_smi_gpi(u32 gpi_sts); |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 62 | int mainboard_smi_apmc(u8 data); |
| 63 | void mainboard_smi_sleep(u8 slp_typ); |
Aseda Aboagye | 6335605 | 2021-06-17 12:10:33 -0700 | [diff] [blame] | 64 | void mainboard_smi_finalize(void); |
Johnny Lin | 107e7aa | 2021-01-14 17:49:08 +0800 | [diff] [blame] | 65 | int mainboard_set_smm_log_level(void); |
Stefan Reinauer | bf34e94 | 2012-04-27 00:44:04 +0200 | [diff] [blame] | 66 | |
Raul E Rangel | e6cd6ca | 2022-02-25 12:40:32 -0700 | [diff] [blame] | 67 | void smm_soc_early_init(void); |
| 68 | void smm_soc_exit(void); |
| 69 | |
Kyösti Mälkki | 9d8adc0 | 2016-12-04 22:17:37 +0200 | [diff] [blame] | 70 | /* This is the SMM handler. */ |
| 71 | extern unsigned char _binary_smm_start[]; |
| 72 | extern unsigned char _binary_smm_end[]; |
| 73 | |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 74 | struct smm_pci_resource_info { |
| 75 | pci_devfn_t pci_addr; |
Patrick Rudolph | 61ec6e9 | 2024-01-25 14:52:01 +0100 | [diff] [blame^] | 76 | uint16_t vendor_id; |
| 77 | uint16_t device_id; |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 78 | uint16_t class_device; |
| 79 | uint8_t class_prog; |
| 80 | struct resource resources[SMM_PCI_RESOURCE_STORE_NUM_RESOURCES]; |
| 81 | }; |
| 82 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 83 | struct smm_runtime { |
| 84 | u32 smbase; |
Arthur Heymans | a3eb3df | 2019-11-27 16:21:46 +0100 | [diff] [blame] | 85 | u32 smm_size; |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 86 | u32 save_state_size; |
Eugene Myers | ae438be | 2020-01-21 17:01:47 -0500 | [diff] [blame] | 87 | u32 num_cpus; |
Kyösti Mälkki | e769bce | 2020-06-16 21:37:05 +0300 | [diff] [blame] | 88 | u32 gnvs_ptr; |
Raul E Rangel | c516098 | 2022-02-24 16:02:49 -0700 | [diff] [blame] | 89 | u32 cbmemc_size; |
| 90 | void *cbmemc; |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 91 | #if CONFIG(SMM_PCI_RESOURCE_STORE) |
| 92 | struct smm_pci_resource_info pci_resources[CONFIG_SMM_PCI_RESOURCE_STORE_NUM_SLOTS]; |
| 93 | #endif |
Arthur Heymans | 64d9e85 | 2021-02-15 18:55:40 +0100 | [diff] [blame] | 94 | uintptr_t save_state_top[CONFIG_MAX_CPUS]; |
Johnny Lin | 107e7aa | 2021-01-14 17:49:08 +0800 | [diff] [blame] | 95 | int smm_log_level; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 96 | } __packed; |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 97 | |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 98 | struct smm_module_params { |
Patrick Rudolph | 24bb803 | 2019-12-01 07:04:04 +0100 | [diff] [blame] | 99 | size_t cpu; |
Raul E Rangel | eb5d76a | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 100 | /* A canary value that has been placed at the end of the stack. |
| 101 | * If (uintptr_t)canary != *canary then a stack overflow has occurred. |
| 102 | */ |
| 103 | const uintptr_t *canary; |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
Arthur Heymans | 5dfb331 | 2021-02-15 13:08:43 +0100 | [diff] [blame] | 106 | /* These parameters are used by the SMM stub code. A pointer to the params |
| 107 | * is also passed to the C-base handler. */ |
| 108 | struct smm_stub_params { |
| 109 | u32 stack_size; |
| 110 | u32 stack_top; |
| 111 | u32 c_handler; |
Arthur Heymans | ed4be45 | 2021-02-15 13:20:35 +0100 | [diff] [blame] | 112 | /* The apic_id_to_cpu provides a mapping from APIC id to CPU number. |
| 113 | * The CPU number is indicated by the index into the array by matching |
| 114 | * the default APIC id and value at the index. The stub loader |
| 115 | * initializes this array with a 1:1 mapping. If the APIC ids are not |
| 116 | * contiguous like the 1:1 mapping it is up to the caller of the stub |
| 117 | * loader to adjust this mapping. */ |
Wonkyu Kim | 26ab9bf | 2021-03-22 19:59:18 -0700 | [diff] [blame] | 118 | u16 apic_id_to_cpu[CONFIG_MAX_CPUS]; |
Arthur Heymans | 5dfb331 | 2021-02-15 13:08:43 +0100 | [diff] [blame] | 119 | } __packed; |
| 120 | |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 121 | /* smm_handler_t is called with arg of smm_module_params pointer. */ |
Lee Leahy | 746d4af | 2017-03-07 15:31:49 -0800 | [diff] [blame] | 122 | typedef asmlinkage void (*smm_handler_t)(void *); |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 123 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 124 | /* SMM Runtime helpers. */ |
Kyösti Mälkki | 239abaf | 2020-06-28 12:12:01 +0300 | [diff] [blame] | 125 | #if ENV_SMM |
| 126 | extern struct global_nvs *gnvs; |
| 127 | #endif |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 128 | |
| 129 | /* Entry point for SMM modules. */ |
Lee Leahy | 22c28e0 | 2017-03-07 15:47:44 -0800 | [diff] [blame] | 130 | asmlinkage void smm_handler_start(void *params); |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 131 | |
| 132 | /* Retrieve SMM save state for a given CPU. WARNING: This does not take into |
| 133 | * account CPUs which are configured to not save their state to RAM. */ |
| 134 | void *smm_get_save_state(int cpu); |
| 135 | |
Patrick Rudolph | 41fec86 | 2020-05-06 10:55:12 +0200 | [diff] [blame] | 136 | /* Returns true if the region overlaps with the SMM */ |
| 137 | bool smm_region_overlaps_handler(const struct region *r); |
| 138 | |
| 139 | /* Returns true if the memory pointed to overlaps with SMM reserved memory. */ |
| 140 | static inline bool smm_points_to_smram(const void *ptr, const size_t len) |
| 141 | { |
| 142 | const struct region r = {(uintptr_t)ptr, len}; |
| 143 | |
| 144 | return smm_region_overlaps_handler(&r); |
| 145 | } |
| 146 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 147 | /* SMM Module Loading API */ |
| 148 | |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 149 | /* The smm_loader_params structure provides direction to the SMM loader: |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 150 | * - num_cpus - number of concurrent cpus in handler needing stack |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 151 | * optional for setting up relocation handler. |
Arthur Heymans | 1684b0a | 2022-04-07 21:50:16 +0200 | [diff] [blame] | 152 | * - cpu_save_state_size - the SMM save state size per cpu |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 153 | * - num_concurrent_save_states - number of concurrent cpus needing save state |
| 154 | * space |
| 155 | * - handler - optional handler to call. Only used during SMM relocation setup. |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 156 | * - runtime - this field is a result only. The SMM runtime location is filled |
| 157 | * into this field so the code doing the loading can manipulate the |
Elyes HAOUAS | ccf78f0 | 2016-08-21 10:49:19 +0200 | [diff] [blame] | 158 | * runtime's assumptions. e.g. updating the APIC id to CPU map to |
| 159 | * handle sparse APIC id space. |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 160 | */ |
| 161 | struct smm_loader_params { |
Arthur Heymans | 2412c81 | 2021-10-28 15:19:39 +0200 | [diff] [blame] | 162 | size_t num_cpus; |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 163 | |
Arthur Heymans | 1684b0a | 2022-04-07 21:50:16 +0200 | [diff] [blame] | 164 | size_t cpu_save_state_size; |
Aaron Durbin | 25a885b | 2017-06-16 11:45:32 -0500 | [diff] [blame] | 165 | size_t num_concurrent_save_states; |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 166 | |
| 167 | smm_handler_t handler; |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 168 | }; |
| 169 | |
Arthur Heymans | 96451a7 | 2021-10-28 15:14:18 +0200 | [diff] [blame] | 170 | /* All of these return 0 on success, < 0 on failure. */ |
| 171 | int smm_setup_stack(const uintptr_t perm_smbase, const size_t perm_smram_size, |
| 172 | const unsigned int total_cpus, const size_t stack_size); |
| 173 | int smm_setup_relocation_handler(struct smm_loader_params *params); |
Arthur Heymans | cfd3224 | 2021-10-28 13:59:54 +0200 | [diff] [blame] | 174 | int smm_load_module(uintptr_t smram_base, size_t smram_size, struct smm_loader_params *params); |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 175 | |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 176 | u32 smm_get_cpu_smbase(unsigned int cpu_num); |
Rocky Phagura | afb7a81 | 2020-07-21 14:48:48 -0700 | [diff] [blame] | 177 | |
Aaron Durbin | b4b9eb3 | 2014-02-13 10:26:18 -0600 | [diff] [blame] | 178 | /* Backup and restore default SMM region. */ |
| 179 | void *backup_default_smm_area(void); |
| 180 | void restore_default_smm_area(void *smm_save_area); |
| 181 | |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 182 | /* |
| 183 | * Fills in the arguments for the entire SMM region covered by chipset |
| 184 | * protections. e.g. TSEG. |
| 185 | */ |
Kyösti Mälkki | 14222d8 | 2019-08-05 15:10:18 +0300 | [diff] [blame] | 186 | void smm_region(uintptr_t *start, size_t *size); |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 187 | |
Kyösti Mälkki | bbba201 | 2022-11-11 19:46:05 +0200 | [diff] [blame] | 188 | static inline void aseg_region(uintptr_t *start, size_t *size) |
| 189 | { |
| 190 | *start = SMM_BASE; |
| 191 | *size = SMM_DEFAULT_SIZE; /* SMM_CODE_SEGMENT_SIZE ? */ |
| 192 | } |
| 193 | |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 194 | enum { |
| 195 | /* SMM handler area. */ |
| 196 | SMM_SUBREGION_HANDLER, |
| 197 | /* SMM cache region. */ |
| 198 | SMM_SUBREGION_CACHE, |
| 199 | /* Chipset specific area. */ |
| 200 | SMM_SUBREGION_CHIPSET, |
| 201 | /* Total sub regions supported. */ |
| 202 | SMM_SUBREGION_NUM, |
| 203 | }; |
| 204 | |
| 205 | /* Fills in the start and size for the requested SMM subregion. Returns |
| 206 | * 0 on success, < 0 on failure. */ |
Kyösti Mälkki | 14222d8 | 2019-08-05 15:10:18 +0300 | [diff] [blame] | 207 | int smm_subregion(int sub, uintptr_t *start, size_t *size); |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 208 | |
Kyösti Mälkki | 7cdb047 | 2019-08-08 11:16:06 +0300 | [diff] [blame] | 209 | /* Print the SMM memory layout on console. */ |
| 210 | void smm_list_regions(void); |
| 211 | |
Arthur Heymans | 342d0a8 | 2020-08-09 14:58:05 +0200 | [diff] [blame] | 212 | #define SMM_REVISION_OFFSET_FROM_TOP (0x8000 - 0x7efc) |
| 213 | /* Return the SMM save state revision. The revision can be fetched from the smm savestate |
| 214 | which is always at the same offset downward from the top of the save state. */ |
| 215 | uint32_t smm_revision(void); |
Arthur Heymans | 3428027 | 2020-09-29 11:51:34 +0200 | [diff] [blame] | 216 | /* Returns the PM ACPI SMI port. On Intel systems this typically not configurable (APM_CNT, 0xb2). |
| 217 | On AMD systems it is sometimes configurable. */ |
| 218 | uint16_t pm_acpi_smi_cmd_port(void); |
Arthur Heymans | 342d0a8 | 2020-08-09 14:58:05 +0200 | [diff] [blame] | 219 | |
Robert Zieba | ac8c378 | 2022-09-07 16:25:15 -0600 | [diff] [blame] | 220 | const volatile struct smm_pci_resource_info *smm_get_pci_resource_store(void); |
| 221 | |
| 222 | void smm_pci_get_stored_resources(const volatile struct smm_pci_resource_info **out_slots, |
| 223 | size_t *out_size); |
| 224 | /* Weak handler function to store PCI BARs. */ |
| 225 | void smm_mainboard_pci_resource_store_init(struct smm_pci_resource_info *slots, size_t size); |
| 226 | /* Helper function to fill BARs from an array of device pointers. */ |
| 227 | bool smm_pci_resource_store_fill_resources(struct smm_pci_resource_info *slots, size_t num_slots, |
| 228 | const struct device **devices, size_t num_devices); |
| 229 | |
| 230 | void smm_pci_resource_store_init(struct smm_runtime *smm_runtime); |
| 231 | |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 232 | #endif /* CPU_X86_SMM_H */ |