riscv-runtime.c 3.4 KB

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