efi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * EFI initialization
  4. *
  5. * Author: Jianmin Lv <lvjianmin@loongson.cn>
  6. * Huacai Chen <chenhuacai@loongson.cn>
  7. *
  8. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/efi.h>
  12. #include <linux/efi-bgrt.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/export.h>
  16. #include <linux/io.h>
  17. #include <linux/kobject.h>
  18. #include <linux/memblock.h>
  19. #include <linux/reboot.h>
  20. #include <linux/screen_info.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/early_ioremap.h>
  23. #include <asm/efi.h>
  24. #include <asm/loongson.h>
  25. static unsigned long efi_nr_tables;
  26. static unsigned long efi_config_table;
  27. static unsigned long __initdata boot_memmap = EFI_INVALID_TABLE_ADDR;
  28. static unsigned long __initdata fdt_pointer = EFI_INVALID_TABLE_ADDR;
  29. static efi_system_table_t *efi_systab;
  30. static efi_config_table_type_t arch_tables[] __initdata = {
  31. {LINUX_EFI_BOOT_MEMMAP_GUID, &boot_memmap, "MEMMAP" },
  32. {DEVICE_TREE_GUID, &fdt_pointer, "FDTPTR" },
  33. {},
  34. };
  35. void __init *efi_fdt_pointer(void)
  36. {
  37. if (!efi_systab)
  38. return NULL;
  39. if (fdt_pointer == EFI_INVALID_TABLE_ADDR)
  40. return NULL;
  41. return early_memremap_ro(fdt_pointer, SZ_64K);
  42. }
  43. void __init efi_runtime_init(void)
  44. {
  45. if (!efi_enabled(EFI_BOOT) || !efi_systab->runtime)
  46. return;
  47. if (efi_runtime_disabled()) {
  48. pr_info("EFI runtime services will be disabled.\n");
  49. return;
  50. }
  51. efi.runtime = (efi_runtime_services_t *)efi_systab->runtime;
  52. efi.runtime_version = (unsigned int)efi.runtime->hdr.revision;
  53. efi_native_runtime_setup();
  54. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  55. }
  56. bool efi_poweroff_required(void)
  57. {
  58. return efi_enabled(EFI_RUNTIME_SERVICES) &&
  59. (acpi_gbl_reduced_hardware || acpi_no_s5);
  60. }
  61. unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
  62. #if defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON)
  63. struct screen_info screen_info __section(".data");
  64. EXPORT_SYMBOL_GPL(screen_info);
  65. #endif
  66. static void __init init_screen_info(void)
  67. {
  68. struct screen_info *si;
  69. if (screen_info_table == EFI_INVALID_TABLE_ADDR)
  70. return;
  71. si = early_memremap(screen_info_table, sizeof(*si));
  72. if (!si) {
  73. pr_err("Could not map screen_info config table\n");
  74. return;
  75. }
  76. screen_info = *si;
  77. memset(si, 0, sizeof(*si));
  78. early_memunmap(si, sizeof(*si));
  79. memblock_reserve(__screen_info_lfb_base(&screen_info), screen_info.lfb_size);
  80. }
  81. void __init efi_init(void)
  82. {
  83. int size;
  84. void *config_tables;
  85. struct efi_boot_memmap *tbl;
  86. if (!efi_system_table)
  87. return;
  88. efi_systab = (efi_system_table_t *)early_memremap_ro(efi_system_table, sizeof(*efi_systab));
  89. if (!efi_systab) {
  90. pr_err("Can't find EFI system table.\n");
  91. return;
  92. }
  93. efi_systab_report_header(&efi_systab->hdr, efi_systab->fw_vendor);
  94. set_bit(EFI_64BIT, &efi.flags);
  95. efi_nr_tables = efi_systab->nr_tables;
  96. efi_config_table = (unsigned long)efi_systab->tables;
  97. size = sizeof(efi_config_table_t);
  98. config_tables = early_memremap(efi_config_table, efi_nr_tables * size);
  99. efi_config_parse_tables(config_tables, efi_systab->nr_tables, arch_tables);
  100. early_memunmap(config_tables, efi_nr_tables * size);
  101. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  102. if (IS_ENABLED(CONFIG_EFI_EARLYCON) || IS_ENABLED(CONFIG_SYSFB))
  103. init_screen_info();
  104. if (boot_memmap == EFI_INVALID_TABLE_ADDR)
  105. return;
  106. tbl = early_memremap_ro(boot_memmap, sizeof(*tbl));
  107. if (tbl) {
  108. struct efi_memory_map_data data;
  109. data.phys_map = boot_memmap + sizeof(*tbl);
  110. data.size = tbl->map_size;
  111. data.desc_size = tbl->desc_size;
  112. data.desc_version = tbl->desc_ver;
  113. if (efi_memmap_init_early(&data) < 0)
  114. panic("Unable to map EFI memory map.\n");
  115. early_memunmap(tbl, sizeof(*tbl));
  116. }
  117. efi_esrt_init();
  118. }