arm-runtime.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Extensible Firmware Interface
  3. *
  4. * Based on Extensible Firmware Interface Specification version 2.4
  5. *
  6. * Copyright (C) 2013, 2014 Linaro Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/dmi.h>
  14. #include <linux/efi.h>
  15. #include <linux/io.h>
  16. #include <linux/memblock.h>
  17. #include <linux/mm_types.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. #include <asm/pgalloc.h>
  28. #include <asm/pgtable.h>
  29. extern u64 efi_system_table;
  30. #ifdef CONFIG_ARM64_PTDUMP_DEBUGFS
  31. #include <asm/ptdump.h>
  32. static struct ptdump_info efi_ptdump_info = {
  33. .mm = &efi_mm,
  34. .markers = (struct addr_marker[]){
  35. { 0, "UEFI runtime start" },
  36. { TASK_SIZE_64, "UEFI runtime end" }
  37. },
  38. .base_addr = 0,
  39. };
  40. static int __init ptdump_init(void)
  41. {
  42. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  43. return 0;
  44. return ptdump_debugfs_register(&efi_ptdump_info, "efi_page_tables");
  45. }
  46. device_initcall(ptdump_init);
  47. #endif
  48. static bool __init efi_virtmap_init(void)
  49. {
  50. efi_memory_desc_t *md;
  51. bool systab_found;
  52. efi_mm.pgd = pgd_alloc(&efi_mm);
  53. mm_init_cpumask(&efi_mm);
  54. init_new_context(NULL, &efi_mm);
  55. systab_found = false;
  56. for_each_efi_memory_desc(md) {
  57. phys_addr_t phys = md->phys_addr;
  58. int ret;
  59. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  60. continue;
  61. if (md->virt_addr == 0)
  62. return false;
  63. ret = efi_create_mapping(&efi_mm, md);
  64. if (ret) {
  65. pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
  66. &phys, ret);
  67. return false;
  68. }
  69. /*
  70. * If this entry covers the address of the UEFI system table,
  71. * calculate and record its virtual address.
  72. */
  73. if (efi_system_table >= phys &&
  74. efi_system_table < phys + (md->num_pages * EFI_PAGE_SIZE)) {
  75. efi.systab = (void *)(unsigned long)(efi_system_table -
  76. phys + md->virt_addr);
  77. systab_found = true;
  78. }
  79. }
  80. if (!systab_found) {
  81. pr_err("No virtual mapping found for the UEFI System Table\n");
  82. return false;
  83. }
  84. if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
  85. return false;
  86. return true;
  87. }
  88. /*
  89. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  90. * non-early mapping of the UEFI system table and virtual mappings for all
  91. * EFI_MEMORY_RUNTIME regions.
  92. */
  93. static int __init arm_enable_runtime_services(void)
  94. {
  95. u64 mapsize;
  96. if (!efi_enabled(EFI_BOOT)) {
  97. pr_info("EFI services will not be available.\n");
  98. return 0;
  99. }
  100. efi_memmap_unmap();
  101. mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
  102. if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
  103. pr_err("Failed to remap EFI memory map\n");
  104. return 0;
  105. }
  106. if (efi_runtime_disabled()) {
  107. pr_info("EFI runtime services will be disabled.\n");
  108. return 0;
  109. }
  110. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  111. pr_info("EFI runtime services access via paravirt.\n");
  112. return 0;
  113. }
  114. pr_info("Remapping and enabling EFI services.\n");
  115. if (!efi_virtmap_init()) {
  116. pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
  117. return -ENOMEM;
  118. }
  119. /* Set up runtime services function pointers */
  120. efi_native_runtime_setup();
  121. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  122. return 0;
  123. }
  124. early_initcall(arm_enable_runtime_services);
  125. void efi_virtmap_load(void)
  126. {
  127. preempt_disable();
  128. efi_set_pgd(&efi_mm);
  129. }
  130. void efi_virtmap_unload(void)
  131. {
  132. efi_set_pgd(current->active_mm);
  133. preempt_enable();
  134. }
  135. static int __init arm_dmi_init(void)
  136. {
  137. /*
  138. * On arm64/ARM, DMI depends on UEFI, and dmi_scan_machine() needs to
  139. * be called early because dmi_id_init(), which is an arch_initcall
  140. * itself, depends on dmi_scan_machine() having been called already.
  141. */
  142. dmi_scan_machine();
  143. if (dmi_available)
  144. dmi_set_dump_stack_arch_desc();
  145. return 0;
  146. }
  147. core_initcall(arm_dmi_init);