1 /* SPDX-License-Identifier: GPL-2.0-only */
7 #include <commonlib/region.h>
8 #include <device/pci_type.h>
9 #include <device/resource.h>
12 #define SMM_DEFAULT_BASE 0x30000
13 #define SMM_DEFAULT_SIZE 0x10000
15 /* used only by C programs so far */
16 #define SMM_BASE 0xa0000
18 #define SMM_ENTRY_OFFSET 0x8000
19 #define SMM_SAVE_STATE_BEGIN(x) (SMM_ENTRY_OFFSET + (x))
22 #define APM_CNT_NOOP_SMI 0x00
23 #define APM_CNT_ACPI_DISABLE 0x1e
24 #define APM_CNT_ACPI_ENABLE 0xe1
25 #define APM_CNT_ROUTE_ALL_XHCI 0xca
26 #define APM_CNT_FINALIZE 0xcb
27 #define APM_CNT_LEGACY 0xcc
28 #define APM_CNT_MBI_UPDATE 0xeb
29 #define APM_CNT_SMMINFO 0xec
30 #define APM_CNT_SMMSTORE 0xed
31 #define APM_CNT_ELOG_GSMI 0xef
34 #define SMM_PCI_RESOURCE_STORE_NUM_RESOURCES 6
37 * SMI Transfer Monitor (STM) descriptor reserved in SMM save state.
40 #define STM_PSD_SIZE ALIGN_UP(sizeof(TXT_PROCESSOR_SMM_DESCRIPTOR), 0x100)
42 #define STM_PSD_SIZE 0
45 /* Send cmd to APM_CNT with HAVE_SMI_HANDLER checking. */
46 enum cb_err
apm_control(u8 cmd
);
47 u8
apm_get_apmc(void);
49 void io_trap_handler(int smif
);
50 int mainboard_io_trap_handler(int smif
);
52 void southbridge_smi_set_eos(void);
54 void global_smi_enable(void);
55 void global_smi_enable_no_pwrbtn(void);
57 void cpu_smi_handler(void);
58 void northbridge_smi_handler(void);
59 void southbridge_smi_handler(void);
61 void mainboard_smi_gpi(u32 gpi_sts
);
62 int mainboard_smi_apmc(u8 data
);
63 void mainboard_smi_sleep(u8 slp_typ
);
64 void mainboard_smi_finalize(void);
65 int mainboard_set_smm_log_level(void);
67 void smm_soc_early_init(void);
68 void smm_soc_exit(void);
70 /* This is the SMM handler. */
71 extern unsigned char _binary_smm_start
[];
72 extern unsigned char _binary_smm_end
[];
74 struct smm_pci_resource_info
{
78 uint16_t class_device
;
80 struct resource resources
[SMM_PCI_RESOURCE_STORE_NUM_RESOURCES
];
91 #if CONFIG(SMM_PCI_RESOURCE_STORE)
92 struct smm_pci_resource_info pci_resources
[CONFIG_SMM_PCI_RESOURCE_STORE_NUM_SLOTS
];
94 uintptr_t save_state_top
[CONFIG_MAX_CPUS
];
98 struct smm_module_params
{
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.
103 const uintptr_t *canary
;
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
{
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. */
118 u16 apic_id_to_cpu
[CONFIG_MAX_CPUS
];
121 /* smm_handler_t is called with arg of smm_module_params pointer. */
122 typedef asmlinkage
void (*smm_handler_t
)(void *);
124 /* SMM Runtime helpers. */
126 extern struct global_nvs
*gnvs
;
129 /* Entry point for SMM modules. */
130 asmlinkage
void smm_handler_start(void *params
);
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
);
136 /* Returns true if the region overlaps with the SMM */
137 bool smm_region_overlaps_handler(const struct region
*r
);
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
)
142 const struct region r
= {(uintptr_t)ptr
, len
};
144 return smm_region_overlaps_handler(&r
);
147 /* SMM Module Loading API */
149 /* The smm_loader_params structure provides direction to the SMM loader:
150 * - num_cpus - number of concurrent cpus in handler needing stack
151 * optional for setting up relocation handler.
152 * - cpu_save_state_size - the SMM save state size per cpu
153 * - num_concurrent_save_states - number of concurrent cpus needing save state
155 * - handler - optional handler to call. Only used during SMM relocation setup.
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
158 * runtime's assumptions. e.g. updating the APIC id to CPU map to
159 * handle sparse APIC id space.
161 struct smm_loader_params
{
164 size_t cpu_save_state_size
;
165 size_t num_concurrent_save_states
;
167 smm_handler_t handler
;
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
);
174 int smm_load_module(uintptr_t smram_base
, size_t smram_size
, struct smm_loader_params
*params
);
176 u32
smm_get_cpu_smbase(unsigned int cpu_num
);
178 /* Backup and restore default SMM region. */
179 void *backup_default_smm_area(void);
180 void restore_default_smm_area(void *smm_save_area
);
183 * Fills in the arguments for the entire SMM region covered by chipset
184 * protections. e.g. TSEG.
186 void smm_region(uintptr_t *start
, size_t *size
);
188 static inline void aseg_region(uintptr_t *start
, size_t *size
)
191 *size
= SMM_DEFAULT_SIZE
; /* SMM_CODE_SEGMENT_SIZE ? */
195 /* SMM handler area. */
196 SMM_SUBREGION_HANDLER
,
197 /* SMM cache region. */
199 /* Chipset specific area. */
200 SMM_SUBREGION_CHIPSET
,
201 /* Total sub regions supported. */
205 /* Fills in the start and size for the requested SMM subregion. Returns
206 * 0 on success, < 0 on failure. */
207 int smm_subregion(int sub
, uintptr_t *start
, size_t *size
);
209 /* Print the SMM memory layout on console. */
210 void smm_list_regions(void);
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);
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);
220 const volatile struct smm_pci_resource_info
*smm_get_pci_resource_store(void);
222 void smm_pci_get_stored_resources(const volatile struct smm_pci_resource_info
**out_slots
,
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
);
230 void smm_pci_resource_store_init(struct smm_runtime
*smm_runtime
);
232 #endif /* CPU_X86_SMM_H */