arm-runtime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Extensible Firmware Interface
  4. *
  5. * Based on Extensible Firmware Interface Specification version 2.4
  6. *
  7. * Copyright (C) 2013, 2014 Linaro Ltd.
  8. */
  9. #include <linux/dmi.h>
  10. #include <linux/efi.h>
  11. #include <linux/io.h>
  12. #include <linux/memblock.h>
  13. #include <linux/mm_types.h>
  14. #include <linux/preempt.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/pgtable.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/efi.h>
  23. #include <asm/mmu.h>
  24. #include <asm/pgalloc.h>
  25. #if defined(CONFIG_PTDUMP_DEBUGFS) && defined(CONFIG_ARM64)
  26. #include <asm/ptdump.h>
  27. static struct ptdump_info efi_ptdump_info = {
  28. .mm = &efi_mm,
  29. .markers = (struct addr_marker[]){
  30. { 0, "UEFI runtime start" },
  31. { DEFAULT_MAP_WINDOW_64, "UEFI runtime end" },
  32. { -1, NULL }
  33. },
  34. .base_addr = 0,
  35. };
  36. static int __init ptdump_init(void)
  37. {
  38. if (efi_enabled(EFI_RUNTIME_SERVICES))
  39. ptdump_debugfs_register(&efi_ptdump_info, "efi_page_tables");
  40. return 0;
  41. }
  42. device_initcall(ptdump_init);
  43. #endif
  44. static bool __init efi_virtmap_init(void)
  45. {
  46. efi_memory_desc_t *md;
  47. efi_mm.pgd = pgd_alloc(&efi_mm);
  48. mm_init_cpumask(&efi_mm);
  49. init_new_context(NULL, &efi_mm);
  50. for_each_efi_memory_desc(md) {
  51. phys_addr_t phys = md->phys_addr;
  52. int ret;
  53. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  54. continue;
  55. if (md->virt_addr == U64_MAX)
  56. return false;
  57. ret = efi_create_mapping(&efi_mm, md);
  58. if (ret) {
  59. pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
  60. &phys, ret);
  61. return false;
  62. }
  63. }
  64. if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
  65. return false;
  66. return true;
  67. }
  68. /*
  69. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  70. * non-early mapping of the UEFI system table and virtual mappings for all
  71. * EFI_MEMORY_RUNTIME regions.
  72. */
  73. static int __init arm_enable_runtime_services(void)
  74. {
  75. u64 mapsize;
  76. if (!efi_enabled(EFI_BOOT)) {
  77. pr_info("EFI services will not be available.\n");
  78. return 0;
  79. }
  80. efi_memmap_unmap();
  81. mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
  82. if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
  83. pr_err("Failed to remap EFI memory map\n");
  84. return 0;
  85. }
  86. if (efi_soft_reserve_enabled()) {
  87. efi_memory_desc_t *md;
  88. for_each_efi_memory_desc(md) {
  89. int md_size = md->num_pages << EFI_PAGE_SHIFT;
  90. struct resource *res;
  91. if (!(md->attribute & EFI_MEMORY_SP))
  92. continue;
  93. res = kzalloc(sizeof(*res), GFP_KERNEL);
  94. if (WARN_ON(!res))
  95. break;
  96. res->start = md->phys_addr;
  97. res->end = md->phys_addr + md_size - 1;
  98. res->name = "Soft Reserved";
  99. res->flags = IORESOURCE_MEM;
  100. res->desc = IORES_DESC_SOFT_RESERVED;
  101. insert_resource(&iomem_resource, res);
  102. }
  103. }
  104. if (efi_runtime_disabled()) {
  105. pr_info("EFI runtime services will be disabled.\n");
  106. return 0;
  107. }
  108. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  109. pr_info("EFI runtime services access via paravirt.\n");
  110. return 0;
  111. }
  112. pr_info("Remapping and enabling EFI services.\n");
  113. if (!efi_virtmap_init()) {
  114. pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
  115. return -ENOMEM;
  116. }
  117. /* Set up runtime services function pointers */
  118. efi_native_runtime_setup();
  119. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  120. return 0;
  121. }
  122. early_initcall(arm_enable_runtime_services);
  123. void efi_virtmap_load(void)
  124. {
  125. preempt_disable();
  126. efi_set_pgd(&efi_mm);
  127. }
  128. void efi_virtmap_unload(void)
  129. {
  130. efi_set_pgd(current->active_mm);
  131. preempt_enable();
  132. }
  133. static int __init arm_dmi_init(void)
  134. {
  135. /*
  136. * On arm64/ARM, DMI depends on UEFI, and dmi_setup() needs to
  137. * be called early because dmi_id_init(), which is an arch_initcall
  138. * itself, depends on dmi_scan_machine() having been called already.
  139. */
  140. dmi_setup();
  141. return 0;
  142. }
  143. core_initcall(arm_dmi_init);