fsp_support.c 11 KB

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