arm-runtime.c 3.1 KB

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