ef1916b2e72c3df046584622f47ba96752069e3c
[platform/kernel/u-boot.git] / arch / x86 / cpu / queensbay / fsp_support.c
1 /*
2  * Copyright (C) 2013, Intel Corporation
3  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * SPDX-License-Identifier:     Intel
6  */
7
8 #include <common.h>
9 #include <asm/arch/fsp/fsp_support.h>
10 #include <asm/post.h>
11
12 /**
13  * Compares two GUIDs
14  *
15  * If the GUIDs are identical then true is returned.
16  * If there are any bit differences in the two GUIDs, then false is returned.
17  *
18  * @guid1:        A pointer to a 128 bit GUID.
19  * @guid2:        A pointer to a 128 bit GUID.
20  *
21  * @retval true:  guid1 and guid2 are identical.
22  * @retval false: guid1 and guid2 are not identical.
23  */
24 static bool compare_guid(const struct efi_guid *guid1,
25                          const struct efi_guid *guid2)
26 {
27         if (memcmp(guid1, guid2, sizeof(struct efi_guid)) == 0)
28                 return true;
29         else
30                 return false;
31 }
32
33 u32 __attribute__((optimize("O0"))) find_fsp_header(void)
34 {
35         /*
36          * This function may be called before the a stack is established,
37          * so special care must be taken. First, it cannot declare any local
38          * variable using stack. Only register variable can be used here.
39          * Secondly, some compiler version will add prolog or epilog code
40          * for the C function. If so the function call may not work before
41          * stack is ready.
42          *
43          * GCC 4.8.1 has been verified to be working for the following codes.
44          */
45         volatile register u8 *fsp asm("eax");
46
47         /* Initalize the FSP base */
48         fsp = (u8 *)CONFIG_FSP_ADDR;
49
50         /* Check the FV signature, _FVH */
51         if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
52                 /* Go to the end of the FV header and align the address */
53                 fsp += ((struct fv_header *)fsp)->ext_hdr_off;
54                 fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
55                 fsp  = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
56         } else {
57                 fsp  = 0;
58         }
59
60         /* Check the FFS GUID */
61         if (fsp &&
62             ((struct ffs_file_header *)fsp)->name.data1 == FSP_GUID_DATA1 &&
63             ((struct ffs_file_header *)fsp)->name.data2 == FSP_GUID_DATA2 &&
64             ((struct ffs_file_header *)fsp)->name.data3 == FSP_GUID_DATA3 &&
65             ((struct ffs_file_header *)fsp)->name.data4[0] == FSP_GUID_DATA4_0 &&
66             ((struct ffs_file_header *)fsp)->name.data4[1] == FSP_GUID_DATA4_1 &&
67             ((struct ffs_file_header *)fsp)->name.data4[2] == FSP_GUID_DATA4_2 &&
68             ((struct ffs_file_header *)fsp)->name.data4[3] == FSP_GUID_DATA4_3 &&
69             ((struct ffs_file_header *)fsp)->name.data4[4] == FSP_GUID_DATA4_4 &&
70             ((struct ffs_file_header *)fsp)->name.data4[5] == FSP_GUID_DATA4_5 &&
71             ((struct ffs_file_header *)fsp)->name.data4[6] == FSP_GUID_DATA4_6 &&
72             ((struct ffs_file_header *)fsp)->name.data4[7] == FSP_GUID_DATA4_7) {
73                 /* Add the FFS header size to find the raw section header */
74                 fsp += sizeof(struct ffs_file_header);
75         } else {
76                 fsp = 0;
77         }
78
79         if (fsp &&
80             ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
81                 /* Add the raw section header size to find the FSP header */
82                 fsp += sizeof(struct raw_section);
83         } else {
84                 fsp = 0;
85         }
86
87         return (u32)fsp;
88 }
89
90 void fsp_continue(struct shared_data *shared_data, u32 status, void *hob_list)
91 {
92         u32 stack_len;
93         u32 stack_base;
94         u32 stack_top;
95
96         post_code(POST_MRC);
97
98         assert(status == 0);
99
100         /* Get the migrated stack in normal memory */
101         stack_base = (u32)fsp_get_bootloader_tmp_mem(hob_list, &stack_len);
102         assert(stack_base != 0);
103         stack_top  = stack_base + stack_len - sizeof(u32);
104
105         /*
106          * Old stack base is stored at the very end of the stack top,
107          * use it to calculate the migrated shared data base
108          */
109         shared_data = (struct shared_data *)(stack_base +
110                         ((u32)shared_data - *(u32 *)stack_top));
111
112         /* The boot loader main function entry */
113         fsp_init_done(hob_list);
114 }
115
116 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
117 {
118         struct shared_data shared_data;
119         fsp_init_f init;
120         struct fsp_init_params params;
121         struct fspinit_rtbuf rt_buf;
122         struct vpd_region *fsp_vpd;
123         struct fsp_header *fsp_hdr;
124         struct fsp_init_params *params_ptr;
125         struct upd_region *fsp_upd;
126
127         fsp_hdr = (struct fsp_header *)find_fsp_header();
128         if (fsp_hdr == NULL) {
129                 /* No valid FSP info header was found */
130                 panic("Invalid FSP header");
131         }
132
133         fsp_upd = (struct upd_region *)&shared_data.fsp_upd;
134         memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
135
136         /* Reserve a gap in stack top */
137         rt_buf.common.stack_top = (u32 *)stack_top - 32;
138         rt_buf.common.boot_mode = boot_mode;
139         rt_buf.common.upd_data = (struct upd_region *)fsp_upd;
140
141         /* Get VPD region start */
142         fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
143                         fsp_hdr->cfg_region_off);
144
145         /* Verifify the VPD data region is valid */
146         assert((fsp_vpd->img_rev == VPD_IMAGE_REV) &&
147                (fsp_vpd->sign == VPD_IMAGE_ID));
148
149         /* Copy default data from Flash */
150         memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
151                sizeof(struct upd_region));
152
153         /* Verifify the UPD data region is valid */
154         assert(fsp_upd->terminator == UPD_TERMINATOR);
155
156         /* Override any UPD setting if required */
157         update_fsp_upd(fsp_upd);
158
159         memset(&params, 0, sizeof(struct fsp_init_params));
160         params.nvs_buf = nvs_buf;
161         params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
162         params.continuation = (fsp_continuation_f)asm_continuation;
163
164         init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
165         params_ptr = &params;
166
167         shared_data.fsp_hdr = fsp_hdr;
168         shared_data.stack_top = (u32 *)stack_top;
169
170         post_code(POST_PRE_MRC);
171
172         /*
173          * Use ASM code to ensure the register value in EAX & ECX
174          * will be passed into BlContinuationFunc
175          */
176         asm volatile (
177                 "pushl  %0;"
178                 "call   *%%eax;"
179                 ".global asm_continuation;"
180                 "asm_continuation:;"
181                 "movl   %%ebx, %%eax;"          /* shared_data */
182                 "movl   4(%%esp), %%edx;"       /* status */
183                 "movl   8(%%esp), %%ecx;"       /* hob_list */
184                 "jmp    fsp_continue;"
185                 : : "m"(params_ptr), "a"(init), "b"(&shared_data)
186         );
187
188         /*
189          * Should never get here.
190          * Control will continue from fsp_continue.
191          * This line below is to prevent the compiler from optimizing
192          * structure intialization.
193          *
194          * DO NOT REMOVE!
195          */
196         init(&params);
197 }
198
199 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
200 {
201         fsp_notify_f notify;
202         struct fsp_notify_params params;
203         struct fsp_notify_params *params_ptr;
204         u32 status;
205
206         if (!fsp_hdr)
207                 fsp_hdr = (struct fsp_header *)find_fsp_header();
208
209         if (fsp_hdr == NULL) {
210                 /* No valid FSP info header */
211                 panic("Invalid FSP header");
212         }
213
214         notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
215         params.phase = phase;
216         params_ptr = &params;
217
218         /*
219          * Use ASM code to ensure correct parameter is on the stack for
220          * FspNotify as U-Boot is using different ABI from FSP
221          */
222         asm volatile (
223                 "pushl  %1;"            /* push notify phase */
224                 "call   *%%eax;"        /* call FspNotify */
225                 "addl   $4, %%esp;"     /* clean up the stack */
226                 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
227         );
228
229         return status;
230 }
231
232 u32 fsp_get_usable_lowmem_top(const void *hob_list)
233 {
234         union hob_pointers hob;
235         phys_addr_t phys_start;
236         u32 top;
237
238         /* Get the HOB list for processing */
239         hob.raw = (void *)hob_list;
240
241         /* * Collect memory ranges */
242         top = FSP_LOWMEM_BASE;
243         while (!end_of_hob(hob)) {
244                 if (get_hob_type(hob) == HOB_TYPE_RES_DESC) {
245                         if (hob.res_desc->type == RES_SYS_MEM) {
246                                 phys_start = hob.res_desc->phys_start;
247                                 /* Need memory above 1MB to be collected here */
248                                 if (phys_start >= FSP_LOWMEM_BASE &&
249                                     phys_start < (phys_addr_t)FSP_HIGHMEM_BASE)
250                                         top += (u32)(hob.res_desc->len);
251                         }
252                 }
253                 hob.raw = get_next_hob(hob);
254         }
255
256         return top;
257 }
258
259 u64 fsp_get_usable_highmem_top(const void *hob_list)
260 {
261         union hob_pointers hob;
262         phys_addr_t phys_start;
263         u64 top;
264
265         /* Get the HOB list for processing */
266         hob.raw = (void *)hob_list;
267
268         /* Collect memory ranges */
269         top = FSP_HIGHMEM_BASE;
270         while (!end_of_hob(hob)) {
271                 if (get_hob_type(hob) == HOB_TYPE_RES_DESC) {
272                         if (hob.res_desc->type == RES_SYS_MEM) {
273                                 phys_start = hob.res_desc->phys_start;
274                                 /* Need memory above 1MB to be collected here */
275                                 if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE)
276                                         top += (u32)(hob.res_desc->len);
277                         }
278                 }
279                 hob.raw = get_next_hob(hob);
280         }
281
282         return top;
283 }
284
285 u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len,
286                                    struct efi_guid *guid)
287 {
288         union hob_pointers hob;
289
290         /* Get the HOB list for processing */
291         hob.raw = (void *)hob_list;
292
293         /* Collect memory ranges */
294         while (!end_of_hob(hob)) {
295                 if (get_hob_type(hob) == HOB_TYPE_RES_DESC) {
296                         if (hob.res_desc->type == RES_MEM_RESERVED) {
297                                 if (compare_guid(&hob.res_desc->owner, guid)) {
298                                         if (len)
299                                                 *len = (u32)(hob.res_desc->len);
300
301                                         return (u64)(hob.res_desc->phys_start);
302                                 }
303                         }
304                 }
305                 hob.raw = get_next_hob(hob);
306         }
307
308         return 0;
309 }
310
311 u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len)
312 {
313         const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
314         u64 length;
315         u32 base;
316
317         base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
318                         &length, (struct efi_guid *)&guid);
319         if ((len != 0) && (base != 0))
320                 *len = (u32)length;
321
322         return base;
323 }
324
325 u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len)
326 {
327         const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
328         u64 length;
329         u32 base;
330
331         base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
332                         &length, (struct efi_guid *)&guid);
333         if ((len != 0) && (base != 0))
334                 *len = (u32)length;
335
336         return base;
337 }
338
339 void *fsp_get_next_hob(u16 type, const void *hob_list)
340 {
341         union hob_pointers hob;
342
343         assert(hob_list != NULL);
344
345         hob.raw = (u8 *)hob_list;
346
347         /* Parse the HOB list until end of list or matching type is found */
348         while (!end_of_hob(hob)) {
349                 if (get_hob_type(hob) == type)
350                         return hob.raw;
351
352                 hob.raw = get_next_hob(hob);
353         }
354
355         return NULL;
356 }
357
358 void *fsp_get_next_guid_hob(const struct efi_guid *guid, const void *hob_list)
359 {
360         union hob_pointers hob;
361
362         hob.raw = (u8 *)hob_list;
363         while ((hob.raw = fsp_get_next_hob(HOB_TYPE_GUID_EXT,
364                         hob.raw)) != NULL) {
365                 if (compare_guid(guid, &hob.guid->name))
366                         break;
367                 hob.raw = get_next_hob(hob);
368         }
369
370         return hob.raw;
371 }
372
373 void *fsp_get_guid_hob_data(const void *hob_list, u32 *len,
374                             struct efi_guid *guid)
375 {
376         u8 *guid_hob;
377
378         guid_hob = fsp_get_next_guid_hob(guid, hob_list);
379         if (guid_hob == NULL) {
380                 return NULL;
381         } else {
382                 if (len)
383                         *len = get_guid_hob_data_size(guid_hob);
384
385                 return get_guid_hob_data(guid_hob);
386         }
387 }
388
389 void *fsp_get_nvs_data(const void *hob_list, u32 *len)
390 {
391         const struct efi_guid guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
392
393         return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
394 }
395
396 void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len)
397 {
398         const struct efi_guid guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
399
400         return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
401 }