riscv-runtime.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/preempt.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pgtable.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/efi.h>
  25. #include <asm/mmu.h>
  26. #include <asm/pgalloc.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. phys_addr_t phys = md->phys_addr;
  35. int ret;
  36. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  37. continue;
  38. if (md->virt_addr == 0)
  39. return false;
  40. ret = efi_create_mapping(&efi_mm, md);
  41. if (ret) {
  42. pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
  43. &phys, ret);
  44. return false;
  45. }
  46. }
  47. if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
  48. return false;
  49. return true;
  50. }
  51. /*
  52. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  53. * non-early mapping of the UEFI system table and virtual mappings for all
  54. * EFI_MEMORY_RUNTIME regions.
  55. */
  56. static int __init riscv_enable_runtime_services(void)
  57. {
  58. u64 mapsize;
  59. if (!efi_enabled(EFI_BOOT)) {
  60. pr_info("EFI services will not be available.\n");
  61. return 0;
  62. }
  63. efi_memmap_unmap();
  64. mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
  65. if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
  66. pr_err("Failed to remap EFI memory map\n");
  67. return 0;
  68. }
  69. if (efi_soft_reserve_enabled()) {
  70. efi_memory_desc_t *md;
  71. for_each_efi_memory_desc(md) {
  72. int md_size = md->num_pages << EFI_PAGE_SHIFT;
  73. struct resource *res;
  74. if (!(md->attribute & EFI_MEMORY_SP))
  75. continue;
  76. res = kzalloc(sizeof(*res), GFP_KERNEL);
  77. if (WARN_ON(!res))
  78. break;
  79. res->start = md->phys_addr;
  80. res->end = md->phys_addr + md_size - 1;
  81. res->name = "Soft Reserved";
  82. res->flags = IORESOURCE_MEM;
  83. res->desc = IORES_DESC_SOFT_RESERVED;
  84. insert_resource(&iomem_resource, res);
  85. }
  86. }
  87. if (efi_runtime_disabled()) {
  88. pr_info("EFI runtime services will be disabled.\n");
  89. return 0;
  90. }
  91. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  92. pr_info("EFI runtime services access via paravirt.\n");
  93. return 0;
  94. }
  95. pr_info("Remapping and enabling EFI services.\n");
  96. if (!efi_virtmap_init()) {
  97. pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
  98. return -ENOMEM;
  99. }
  100. /* Set up runtime services function pointers */
  101. efi_native_runtime_setup();
  102. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  103. return 0;
  104. }
  105. early_initcall(riscv_enable_runtime_services);
  106. void efi_virtmap_load(void)
  107. {
  108. preempt_disable();
  109. switch_mm(current->active_mm, &efi_mm, NULL);
  110. }
  111. void efi_virtmap_unload(void)
  112. {
  113. switch_mm(&efi_mm, current->active_mm, NULL);
  114. preempt_enable();
  115. }