arm-runtime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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
  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_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. mm_init_cpumask(&efi_mm);
  59. init_new_context(NULL, &efi_mm);
  60. systab_found = false;
  61. for_each_efi_memory_desc(md) {
  62. phys_addr_t phys = md->phys_addr;
  63. int ret;
  64. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  65. continue;
  66. if (md->virt_addr == 0)
  67. return false;
  68. ret = efi_create_mapping(&efi_mm, md);
  69. if (!ret) {
  70. pr_info(" EFI remap %pa => %p\n",
  71. &phys, (void *)(unsigned long)md->virt_addr);
  72. } else {
  73. pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
  74. &phys, ret);
  75. return false;
  76. }
  77. /*
  78. * If this entry covers the address of the UEFI system table,
  79. * calculate and record its virtual address.
  80. */
  81. if (efi_system_table >= phys &&
  82. efi_system_table < phys + (md->num_pages * EFI_PAGE_SIZE)) {
  83. efi.systab = (void *)(unsigned long)(efi_system_table -
  84. phys + md->virt_addr);
  85. systab_found = true;
  86. }
  87. }
  88. if (!systab_found) {
  89. pr_err("No virtual mapping found for the UEFI System Table\n");
  90. return false;
  91. }
  92. if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
  93. return false;
  94. return true;
  95. }
  96. /*
  97. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  98. * non-early mapping of the UEFI system table and virtual mappings for all
  99. * EFI_MEMORY_RUNTIME regions.
  100. */
  101. static int __init arm_enable_runtime_services(void)
  102. {
  103. u64 mapsize;
  104. if (!efi_enabled(EFI_BOOT)) {
  105. pr_info("EFI services will not be available.\n");
  106. return 0;
  107. }
  108. if (efi_runtime_disabled()) {
  109. pr_info("EFI runtime services will be disabled.\n");
  110. return 0;
  111. }
  112. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  113. pr_info("EFI runtime services access via paravirt.\n");
  114. return 0;
  115. }
  116. pr_info("Remapping and enabling EFI services.\n");
  117. mapsize = efi.memmap.desc_size * efi.memmap.nr_map;
  118. if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) {
  119. pr_err("Failed to remap EFI memory map\n");
  120. return -ENOMEM;
  121. }
  122. if (!efi_virtmap_init()) {
  123. pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
  124. return -ENOMEM;
  125. }
  126. /* Set up runtime services function pointers */
  127. efi_native_runtime_setup();
  128. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  129. return 0;
  130. }
  131. early_initcall(arm_enable_runtime_services);
  132. void efi_virtmap_load(void)
  133. {
  134. preempt_disable();
  135. efi_set_pgd(&efi_mm);
  136. }
  137. void efi_virtmap_unload(void)
  138. {
  139. efi_set_pgd(current->active_mm);
  140. preempt_enable();
  141. }