arm-runtime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/efi.h>
  14. #include <linux/io.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mm_types.h>
  17. #include <linux/preempt.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/efi.h>
  25. #include <asm/mmu.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/pgtable.h>
  28. extern u64 efi_system_table;
  29. static struct mm_struct efi_mm = {
  30. .mm_rb = RB_ROOT,
  31. .mm_users = ATOMIC_INIT(2),
  32. .mm_count = ATOMIC_INIT(1),
  33. .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
  34. .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
  35. .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
  36. };
  37. #ifdef CONFIG_ARM64_PTDUMP_DEBUGFS
  38. #include <asm/ptdump.h>
  39. static struct ptdump_info efi_ptdump_info = {
  40. .mm = &efi_mm,
  41. .markers = (struct addr_marker[]){
  42. { 0, "UEFI runtime start" },
  43. { TASK_SIZE_64, "UEFI runtime end" }
  44. },
  45. .base_addr = 0,
  46. };
  47. static int __init ptdump_init(void)
  48. {
  49. return ptdump_debugfs_register(&efi_ptdump_info, "efi_page_tables");
  50. }
  51. device_initcall(ptdump_init);
  52. #endif
  53. static bool __init efi_virtmap_init(void)
  54. {
  55. efi_memory_desc_t *md;
  56. bool systab_found;
  57. efi_mm.pgd = pgd_alloc(&efi_mm);
  58. init_new_context(NULL, &efi_mm);
  59. systab_found = false;
  60. for_each_efi_memory_desc(md) {
  61. phys_addr_t phys = md->phys_addr;
  62. int ret;
  63. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  64. continue;
  65. if (md->virt_addr == 0)
  66. return false;
  67. ret = efi_create_mapping(&efi_mm, md);
  68. if (!ret) {
  69. pr_info(" EFI remap %pa => %p\n",
  70. &phys, (void *)(unsigned long)md->virt_addr);
  71. } else {
  72. pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
  73. &phys, ret);
  74. return false;
  75. }
  76. /*
  77. * If this entry covers the address of the UEFI system table,
  78. * calculate and record its virtual address.
  79. */
  80. if (efi_system_table >= phys &&
  81. efi_system_table < phys + (md->num_pages * EFI_PAGE_SIZE)) {
  82. efi.systab = (void *)(unsigned long)(efi_system_table -
  83. phys + md->virt_addr);
  84. systab_found = true;
  85. }
  86. }
  87. if (!systab_found) {
  88. pr_err("No virtual mapping found for the UEFI System Table\n");
  89. return false;
  90. }
  91. if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
  92. return false;
  93. return true;
  94. }
  95. /*
  96. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  97. * non-early mapping of the UEFI system table and virtual mappings for all
  98. * EFI_MEMORY_RUNTIME regions.
  99. */
  100. static int __init arm_enable_runtime_services(void)
  101. {
  102. u64 mapsize;
  103. if (!efi_enabled(EFI_BOOT)) {
  104. pr_info("EFI services will not be available.\n");
  105. return 0;
  106. }
  107. if (efi_runtime_disabled()) {
  108. pr_info("EFI runtime services will be disabled.\n");
  109. return 0;
  110. }
  111. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  112. pr_info("EFI runtime services access via paravirt.\n");
  113. return 0;
  114. }
  115. pr_info("Remapping and enabling EFI services.\n");
  116. mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
  117. if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
  118. pr_err("Failed to remap EFI memory map\n");
  119. return -ENOMEM;
  120. }
  121. if (!efi_virtmap_init()) {
  122. pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
  123. return -ENOMEM;
  124. }
  125. /* Set up runtime services function pointers */
  126. efi_native_runtime_setup();
  127. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  128. return 0;
  129. }
  130. early_initcall(arm_enable_runtime_services);
  131. void efi_virtmap_load(void)
  132. {
  133. preempt_disable();
  134. efi_set_pgd(&efi_mm);
  135. }
  136. void efi_virtmap_unload(void)
  137. {
  138. efi_set_pgd(current->active_mm);
  139. preempt_enable();
  140. }