efi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
  4. */
  5. #include <linux/efi.h>
  6. #include <linux/memblock.h>
  7. #include <linux/screen_info.h>
  8. #include <asm/efi.h>
  9. #include <asm/mach/map.h>
  10. #include <asm/mmu_context.h>
  11. static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
  12. {
  13. efi_memory_desc_t *md = data;
  14. pte_t pte = *ptep;
  15. if (md->attribute & EFI_MEMORY_RO)
  16. pte = set_pte_bit(pte, __pgprot(L_PTE_RDONLY));
  17. if (md->attribute & EFI_MEMORY_XP)
  18. pte = set_pte_bit(pte, __pgprot(L_PTE_XN));
  19. set_pte_ext(ptep, pte, PTE_EXT_NG);
  20. return 0;
  21. }
  22. int __init efi_set_mapping_permissions(struct mm_struct *mm,
  23. efi_memory_desc_t *md,
  24. bool ignored)
  25. {
  26. unsigned long base, size;
  27. base = md->virt_addr;
  28. size = md->num_pages << EFI_PAGE_SHIFT;
  29. /*
  30. * We can only use apply_to_page_range() if we can guarantee that the
  31. * entire region was mapped using pages. This should be the case if the
  32. * region does not cover any naturally aligned SECTION_SIZE sized
  33. * blocks.
  34. */
  35. if (round_down(base + size, SECTION_SIZE) <
  36. round_up(base, SECTION_SIZE) + SECTION_SIZE)
  37. return apply_to_page_range(mm, base, size, set_permissions, md);
  38. return 0;
  39. }
  40. int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
  41. {
  42. struct map_desc desc = {
  43. .virtual = md->virt_addr,
  44. .pfn = __phys_to_pfn(md->phys_addr),
  45. .length = md->num_pages * EFI_PAGE_SIZE,
  46. };
  47. /*
  48. * Order is important here: memory regions may have all of the
  49. * bits below set (and usually do), so we check them in order of
  50. * preference.
  51. */
  52. if (md->attribute & EFI_MEMORY_WB)
  53. desc.type = MT_MEMORY_RWX;
  54. else if (md->attribute & EFI_MEMORY_WT)
  55. desc.type = MT_MEMORY_RWX_NONCACHED;
  56. else if (md->attribute & EFI_MEMORY_WC)
  57. desc.type = MT_DEVICE_WC;
  58. else
  59. desc.type = MT_DEVICE;
  60. create_mapping_late(mm, &desc, true);
  61. /*
  62. * If stricter permissions were specified, apply them now.
  63. */
  64. if (md->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))
  65. return efi_set_mapping_permissions(mm, md, false);
  66. return 0;
  67. }
  68. static unsigned long __initdata cpu_state_table = EFI_INVALID_TABLE_ADDR;
  69. const efi_config_table_type_t efi_arch_tables[] __initconst = {
  70. {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
  71. {}
  72. };
  73. static void __init load_cpu_state_table(void)
  74. {
  75. if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
  76. struct efi_arm_entry_state *state;
  77. bool dump_state = true;
  78. state = early_memremap_ro(cpu_state_table,
  79. sizeof(struct efi_arm_entry_state));
  80. if (state == NULL) {
  81. pr_warn("Unable to map CPU entry state table.\n");
  82. return;
  83. }
  84. if ((state->sctlr_before_ebs & 1) == 0)
  85. pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
  86. else if ((state->sctlr_after_ebs & 1) == 0)
  87. pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
  88. else
  89. dump_state = false;
  90. if (dump_state || efi_enabled(EFI_DBG)) {
  91. pr_info("CPSR at EFI stub entry : 0x%08x\n",
  92. state->cpsr_before_ebs);
  93. pr_info("SCTLR at EFI stub entry : 0x%08x\n",
  94. state->sctlr_before_ebs);
  95. pr_info("CPSR after ExitBootServices() : 0x%08x\n",
  96. state->cpsr_after_ebs);
  97. pr_info("SCTLR after ExitBootServices(): 0x%08x\n",
  98. state->sctlr_after_ebs);
  99. }
  100. early_memunmap(state, sizeof(struct efi_arm_entry_state));
  101. }
  102. }
  103. void __init arm_efi_init(void)
  104. {
  105. efi_init();
  106. /* ARM does not permit early mappings to persist across paging_init() */
  107. efi_memmap_unmap();
  108. load_cpu_state_table();
  109. }