1 // SPDX-License-Identifier: Intel
2 /*
3  * Copyright (C) 2013, Intel Corporation
4  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
5  */
6 
7 #include <common.h>
8 #include <log.h>
9 #include <asm/fsp/fsp_support.h>
10 #include <asm/post.h>
11 
fsp_get_usable_lowmem_top(const void * hob_list)12 u32 fsp_get_usable_lowmem_top(const void *hob_list)
13 {
14 	const struct hob_header *hdr;
15 	struct hob_res_desc *res_desc;
16 	phys_addr_t phys_start;
17 	u32 top;
18 #ifdef CONFIG_FSP_BROKEN_HOB
19 	struct hob_mem_alloc *res_mem;
20 	phys_addr_t mem_base = 0;
21 #endif
22 
23 	/* Get the HOB list for processing */
24 	hdr = hob_list;
25 
26 	/* * Collect memory ranges */
27 	top = FSP_LOWMEM_BASE;
28 	while (!end_of_hob(hdr)) {
29 		if (hdr->type == HOB_TYPE_RES_DESC) {
30 			res_desc = (struct hob_res_desc *)hdr;
31 			if (res_desc->type == RES_SYS_MEM) {
32 				phys_start = res_desc->phys_start;
33 				/* Need memory above 1MB to be collected here */
34 				if (phys_start >= FSP_LOWMEM_BASE &&
35 				    phys_start < (phys_addr_t)FSP_HIGHMEM_BASE)
36 					top += (u32)(res_desc->len);
37 			}
38 		}
39 
40 #ifdef CONFIG_FSP_BROKEN_HOB
41 		/*
42 		 * Find out the lowest memory base address allocated by FSP
43 		 * for the boot service data
44 		 */
45 		if (hdr->type == HOB_TYPE_MEM_ALLOC) {
46 			res_mem = (struct hob_mem_alloc *)hdr;
47 			if (!mem_base)
48 				mem_base = res_mem->mem_base;
49 			if (res_mem->mem_base < mem_base)
50 				mem_base = res_mem->mem_base;
51 		}
52 #endif
53 
54 		hdr = get_next_hob(hdr);
55 	}
56 
57 #ifdef CONFIG_FSP_BROKEN_HOB
58 	/*
59 	 * Check whether the memory top address is below the FSP HOB list.
60 	 * If not, use the lowest memory base address allocated by FSP as
61 	 * the memory top address. This is to prevent U-Boot relocation
62 	 * overwrites the important boot service data which is used by FSP,
63 	 * otherwise the subsequent call to fsp_notify() will fail.
64 	 */
65 	if (top > (u32)hob_list) {
66 		debug("Adjust memory top address due to a buggy FSP\n");
67 		top = (u32)mem_base;
68 	}
69 #endif
70 
71 	return top;
72 }
73 
fsp_get_usable_highmem_top(const void * hob_list)74 u64 fsp_get_usable_highmem_top(const void *hob_list)
75 {
76 	const struct hob_header *hdr;
77 	struct hob_res_desc *res_desc;
78 	phys_addr_t phys_start;
79 	u64 top;
80 
81 	/* Get the HOB list for processing */
82 	hdr = hob_list;
83 
84 	/* Collect memory ranges */
85 	top = FSP_HIGHMEM_BASE;
86 	while (!end_of_hob(hdr)) {
87 		if (hdr->type == HOB_TYPE_RES_DESC) {
88 			res_desc = (struct hob_res_desc *)hdr;
89 			if (res_desc->type == RES_SYS_MEM) {
90 				phys_start = res_desc->phys_start;
91 				/* Need memory above 4GB to be collected here */
92 				if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE)
93 					top += (u32)(res_desc->len);
94 			}
95 		}
96 		hdr = get_next_hob(hdr);
97 	}
98 
99 	return top;
100 }
101 
fsp_get_reserved_mem_from_guid(const void * hob_list,u64 * len,const efi_guid_t * guid)102 u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len,
103 				   const efi_guid_t *guid)
104 {
105 	const struct hob_header *hdr;
106 	struct hob_res_desc *res_desc;
107 
108 	/* Get the HOB list for processing */
109 	hdr = hob_list;
110 
111 	/* Collect memory ranges */
112 	while (!end_of_hob(hdr)) {
113 		if (hdr->type == HOB_TYPE_RES_DESC) {
114 			res_desc = (struct hob_res_desc *)hdr;
115 			if (res_desc->type == RES_MEM_RESERVED) {
116 				if (!guidcmp(&res_desc->owner, guid)) {
117 					if (len)
118 						*len = (u32)(res_desc->len);
119 
120 					return (u64)(res_desc->phys_start);
121 				}
122 			}
123 		}
124 		hdr = get_next_hob(hdr);
125 	}
126 
127 	return 0;
128 }
129 
fsp_get_fsp_reserved_mem(const void * hob_list,u32 * len)130 u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len)
131 {
132 	const efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
133 	u64 length;
134 	u32 base;
135 
136 	base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
137 			&length, &guid);
138 	if (len && base)
139 		*len = (u32)length;
140 
141 	return base;
142 }
143 
fsp_get_tseg_reserved_mem(const void * hob_list,u32 * len)144 u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len)
145 {
146 	const efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
147 	u64 length;
148 	u32 base;
149 
150 	base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
151 			&length, &guid);
152 	if (len && base)
153 		*len = (u32)length;
154 
155 	return base;
156 }
157 
fsp_get_nvs_data(const void * hob_list,u32 * len)158 void *fsp_get_nvs_data(const void *hob_list, u32 *len)
159 {
160 	const efi_guid_t guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
161 
162 	return hob_get_guid_hob_data(hob_list, len, &guid);
163 }
164 
fsp_get_var_nvs_data(const void * hob_list,u32 * len)165 void *fsp_get_var_nvs_data(const void *hob_list, u32 *len)
166 {
167 	const efi_guid_t guid = FSP_VARIABLE_NV_DATA_HOB_GUID;
168 
169 	return hob_get_guid_hob_data(hob_list, len, &guid);
170 }
171 
fsp_get_bootloader_tmp_mem(const void * hob_list,u32 * len)172 void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len)
173 {
174 	const efi_guid_t guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
175 
176 	return hob_get_guid_hob_data(hob_list, len, &guid);
177 }
178 
fsp_get_graphics_info(const void * hob_list,u32 * len)179 void *fsp_get_graphics_info(const void *hob_list, u32 *len)
180 {
181 	const efi_guid_t guid = FSP_GRAPHICS_INFO_HOB_GUID;
182 
183 	return hob_get_guid_hob_data(hob_list, len, &guid);
184 }
185