xref: /aosp_15_r20/external/coreboot/src/drivers/intel/fsp2_0/include/fsp/util.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _FSP2_0_UTIL_H_
4 #define _FSP2_0_UTIL_H_
5 
6 #include <boot/coreboot_tables.h>
7 #include <cbfs.h>
8 #include <commonlib/region.h>
9 #include <cpu/cpu.h>
10 #include <fsp/api.h>
11 #include <efi/efi_datatype.h>
12 #include <fsp/info_header.h>
13 #include <memrange.h>
14 #include <program_loading.h>
15 #include <types.h>
16 
17 #define FSP_VER_LEN	30
18 
19 #if CONFIG(PLATFORM_USES_FSP2_4)
20 #define FSPM_ARCHx_UPD FSPM_ARCH2_UPD
21 #define FSPS_ARCHx_UPD FSPS_ARCH2_UPD
22 #else
23 #define FSPM_ARCHx_UPD FSPM_ARCH_UPD
24 #define FSPS_ARCHx_UPD FSPS_ARCH_UPD
25 #endif
26 
27 /* Macro for checking and loading array type configs into array type UPDs */
28 #define FSP_ARRAY_LOAD(dst, src) \
29 do { \
30 	_Static_assert(ARRAY_SIZE(dst) >= ARRAY_SIZE(src), "copy buffer overflow!"); \
31 	memcpy(dst, src, sizeof(src)); \
32 } while (0)
33 
34 /* Helper function to print a message concatenated with the a FSP return status. */
35 #if CONFIG(PLATFORM_USES_FSP2_X86_32)
36 #define FSP_STATUS_FMT "0x%08x"
37 #else
38 #define FSP_STATUS_FMT "0x%016llx"
39 #endif
40 #define fsp_printk(status, msg_level, fmt, ...)	\
41 	printk(msg_level, fmt ", status=" FSP_STATUS_FMT "\n", ##__VA_ARGS__, status)
42 #define fsp_die_with_post_code(status, postcode, fmt, ...) \
43 	die_with_post_code(postcode, fmt ", status=" FSP_STATUS_FMT "\n", ##__VA_ARGS__, status)
44 
45 struct hob_header {
46 	uint16_t type;
47 	uint16_t length;
48 } __packed;
49 
50 struct fsp_nvs_hob2_data_region_header {
51 	efi_physical_address nvs_data_ptr;
52 	uint64_t nvs_data_length;
53 };
54 
55 struct fsp_notify_params {
56 	enum fsp_notify_phase phase;
57 };
58 
59 enum fsp_multi_phase_action {
60 	GET_NUMBER_OF_PHASES = 0,
61 	EXECUTE_PHASE = 1
62 };
63 
64 struct fsp_multi_phase_params {
65 	enum fsp_multi_phase_action multi_phase_action;
66 	uint32_t phase_index;
67 	void *multi_phase_param_ptr;
68 };
69 
70 struct fsp_multi_phase_get_number_of_phases_params {
71 	uint32_t number_of_phases;
72 	uint32_t phases_executed;
73 };
74 
75 struct hob_resource {
76 	uint8_t owner_guid[16];
77 	uint32_t type;
78 	uint32_t attribute_type;
79 	uint64_t addr;
80 	uint64_t length;
81 } __packed;
82 
83 union fsp_revision {
84 	uint32_t val;
85 	struct {
86 		uint8_t bld_num;
87 		uint8_t revision;
88 		uint8_t minor;
89 		uint8_t major;
90 	} rev;
91 };
92 
93 union extended_fsp_revision {
94 	uint16_t val;
95 	struct {
96 		uint8_t bld_num;
97 		uint8_t revision;
98 	} rev;
99 };
100 
101 #if CONFIG_UDK_VERSION < 2017
102 enum resource_type {
103 	EFI_RESOURCE_SYSTEM_MEMORY		= 0,
104 	EFI_RESOURCE_MEMORY_MAPPED_IO		= 1,
105 	EFI_RESOURCE_IO				= 2,
106 	EFI_RESOURCE_FIRMWARE_DEVICE		= 3,
107 	EFI_RESOURCE_MEMORY_MAPPED_IO_PORT	= 4,
108 	EFI_RESOURCE_MEMORY_RESERVED		= 5,
109 	EFI_RESOURCE_IO_RESERVED		= 6,
110 	EFI_RESOURCE_MAX_MEMORY_TYPE		= 7,
111 };
112 #endif
113 
114 enum hob_type {
115 	HOB_TYPE_HANDOFF			= 0x0001,
116 	HOB_TYPE_MEMORY_ALLOCATION		= 0x0002,
117 	HOB_TYPE_RESOURCE_DESCRIPTOR		= 0x0003,
118 	HOB_TYPE_GUID_EXTENSION			= 0x0004,
119 	HOB_TYPE_FV				= 0x0005,
120 	HOB_TYPE_CPU				= 0x0006,
121 	HOB_TYPE_MEMORY_POOL			= 0x0007,
122 	HOB_TYPE_FV2				= 0x0009,
123 	HOB_TYPE_LOAD_PEIM_UNUSED		= 0x000A,
124 	HOB_TYPE_UCAPSULE			= 0x000B,
125 	HOB_TYPE_UNUSED				= 0xFFFE,
126 	HOB_TYPE_END_OF_HOB_LIST		= 0xFFFF,
127 };
128 
129 extern const uint8_t fsp_bootloader_tolum_guid[16];
130 extern const uint8_t fsp_nv_storage_guid[16];
131 extern const uint8_t fsp_reserved_memory_guid[16];
132 
133 /*
134  * Functions to iterate over the HOBs and get resource structs or extension HOB data. It's
135  * required to initialize the hob_iterator struct by a fsp_hob_iterator_init call before
136  * passing the fsp_hob_iterator_get_next_* functions. The fsp_hob_iterator_get_next_* functions
137  * will update the hob_iterator to point to the next HOB header, so the iterators can be called
138  * multiple times to get the data from multiple HOB instances.
139  */
140 enum cb_err fsp_hob_iterator_init(const struct hob_header **hob_iterator);
141 enum cb_err fsp_hob_iterator_get_next_resource(const struct hob_header **hob_iterator,
142 					       const struct hob_resource **res);
143 enum cb_err fsp_hob_iterator_get_next_guid_resource(const struct hob_header **hob_iterator,
144 						    const uint8_t guid[16],
145 						    const struct hob_resource **res);
146 enum cb_err fsp_hob_iterator_get_next_guid_extension(const struct hob_header **hob_iterator,
147 						     const uint8_t guid[16],
148 						     const void **data, size_t *size);
149 
150 /* Function to extract the FSP timestamp from FPDT HOB and display */
151 void fsp_display_timestamp(void);
152 const void *fsp_get_hob_list(void);
153 void *fsp_get_hob_list_ptr(void);
154 const void *fsp_find_extension_hob_by_guid(const uint8_t *guid, size_t *size);
155 const void *fsp_find_nv_storage_data(size_t *size);
156 enum cb_err fsp_find_range_hob(struct range_entry *re, const uint8_t guid[16]);
157 void fsp_display_fvi_version_hob(void);
158 void fsp_find_reserved_memory(struct range_entry *re);
159 const struct hob_resource *fsp_hob_header_to_resource(
160 	const struct hob_header *hob);
161 const struct hob_header *fsp_next_hob(const struct hob_header *parent);
162 bool fsp_guid_compare(const uint8_t guid1[16], const uint8_t guid2[16]);
163 void fsp_find_bootloader_tolum(struct range_entry *re);
164 bool fsp_display_error_info(void);
165 void display_fsp_error_info_hob(const void *hob);
166 void fsp_get_version(char *buf);
167 /* fsp_verify_upd_header_signature calls die() on signature mismatch */
168 void fsp_verify_upd_header_signature(uint64_t upd_signature, uint64_t expected_signature);
169 void lb_string_platform_blob_version(struct lb_header *header);
170 void report_fspt_output(void);
171 void soc_validate_fspm_header(const struct fsp_header *hdr);
172 /*
173  * This function finds the FSP resource HOB for the given GUID.
174  * Returns the pointer to the HOB if found, otherwise NULL
175  */
176 const void *fsp_find_resource_hob_by_guid(const uint8_t *guid);
177 
178 /* Fill in header and validate a loaded FSP component. */
179 enum cb_err fsp_validate_component(struct fsp_header *hdr, void *fsp_blob, size_t size);
180 
181 struct fsp_load_descriptor {
182 	/* fsp_prog object will have region_device initialized to final
183 	 * load location in memory. */
184 	struct prog fsp_prog;
185 	/* CBFS allocator to place loaded FSP. NULL to map flash directly. */
186 	cbfs_allocator_t alloc;
187 	/* Optional argument to be utilized by get_destination() callback. */
188 	void *arg;
189 };
190 
191 /* Load the FSP component described by fsp_load_descriptor from cbfs. The FSP
192  * header object will be validated and filled in on successful load. */
193 enum cb_err fsp_load_component(struct fsp_load_descriptor *fspld, struct fsp_header *hdr);
194 
195 /*
196  * Handle FSP reboot request status. Chipset/soc is expected to provide
197  * chipset_handle_reset() that deals with reset type codes specific to given
198  * SoC. If the requested status is not a reboot status or unhandled, this
199  * function does nothing.
200  */
201 void fsp_handle_reset(efi_return_status_t status);
202 
203 /* SoC/chipset must provide this to handle platform-specific reset codes */
204 void chipset_handle_reset(efi_return_status_t status);
205 
206 #if CONFIG(PLATFORM_USES_SECOND_FSP)
207 /* The SoC must implement these to choose the appropriate FSP-M/FSP-S binary. */
208 const char *soc_select_fsp_m_cbfs(void);
209 const char *soc_select_fsp_s_cbfs(void);
210 #else
soc_select_fsp_m_cbfs(void)211 static inline const char *soc_select_fsp_m_cbfs(void)
212 {
213 	return CONFIG_FSP_M_CBFS;
214 }
soc_select_fsp_s_cbfs(void)215 static inline const char *soc_select_fsp_s_cbfs(void)
216 {
217 	return CONFIG_FSP_S_CBFS;
218 }
219 #endif
220 
221 typedef __efiapi efi_return_status_t (*temp_ram_exit_fn)(void *param);
222 typedef __efiapi efi_return_status_t (*fsp_memory_init_fn)
223 				   (void *raminit_upd, void **hob_list);
224 typedef __efiapi efi_return_status_t (*fsp_silicon_init_fn)(void *silicon_upd);
225 typedef __efiapi efi_return_status_t (*fsp_multi_phase_init_fn)(struct fsp_multi_phase_params *);
226 typedef __efiapi efi_return_status_t (*fsp_notify_fn)(struct fsp_notify_params *);
227 #include <fsp/debug.h>
228 
229 #endif /* _FSP2_0_UTIL_H_ */
230