arm-runtime.c 3.5 KB

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