arm32-stub.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Linaro Ltd; <[email protected]>
  4. */
  5. #include <linux/efi.h>
  6. #include <asm/efi.h>
  7. #include "efistub.h"
  8. static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
  9. struct efi_arm_entry_state *efi_entry_state;
  10. static void get_cpu_state(u32 *cpsr, u32 *sctlr)
  11. {
  12. asm("mrs %0, cpsr" : "=r"(*cpsr));
  13. if ((*cpsr & MODE_MASK) == HYP_MODE)
  14. asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
  15. else
  16. asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
  17. }
  18. efi_status_t check_platform_features(void)
  19. {
  20. efi_status_t status;
  21. u32 cpsr, sctlr;
  22. int block;
  23. get_cpu_state(&cpsr, &sctlr);
  24. efi_info("Entering in %s mode with MMU %sabled\n",
  25. ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
  26. (sctlr & 1) ? "en" : "dis");
  27. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
  28. sizeof(*efi_entry_state),
  29. (void **)&efi_entry_state);
  30. if (status != EFI_SUCCESS) {
  31. efi_err("allocate_pool() failed\n");
  32. return status;
  33. }
  34. efi_entry_state->cpsr_before_ebs = cpsr;
  35. efi_entry_state->sctlr_before_ebs = sctlr;
  36. status = efi_bs_call(install_configuration_table, &cpu_state_guid,
  37. efi_entry_state);
  38. if (status != EFI_SUCCESS) {
  39. efi_err("install_configuration_table() failed\n");
  40. goto free_state;
  41. }
  42. /* non-LPAE kernels can run anywhere */
  43. if (!IS_ENABLED(CONFIG_ARM_LPAE))
  44. return EFI_SUCCESS;
  45. /* LPAE kernels need compatible hardware */
  46. block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
  47. if (block < 5) {
  48. efi_err("This LPAE kernel is not supported by your CPU\n");
  49. status = EFI_UNSUPPORTED;
  50. goto drop_table;
  51. }
  52. return EFI_SUCCESS;
  53. drop_table:
  54. efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
  55. free_state:
  56. efi_bs_call(free_pool, efi_entry_state);
  57. return status;
  58. }
  59. void efi_handle_post_ebs_state(void)
  60. {
  61. get_cpu_state(&efi_entry_state->cpsr_after_ebs,
  62. &efi_entry_state->sctlr_after_ebs);
  63. }
  64. static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
  65. struct screen_info *alloc_screen_info(void)
  66. {
  67. struct screen_info *si;
  68. efi_status_t status;
  69. /*
  70. * Unlike on arm64, where we can directly fill out the screen_info
  71. * structure from the stub, we need to allocate a buffer to hold
  72. * its contents while we hand over to the kernel proper from the
  73. * decompressor.
  74. */
  75. status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
  76. sizeof(*si), (void **)&si);
  77. if (status != EFI_SUCCESS)
  78. return NULL;
  79. status = efi_bs_call(install_configuration_table,
  80. &screen_info_guid, si);
  81. if (status == EFI_SUCCESS)
  82. return si;
  83. efi_bs_call(free_pool, si);
  84. return NULL;
  85. }
  86. void free_screen_info(struct screen_info *si)
  87. {
  88. if (!si)
  89. return;
  90. efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
  91. efi_bs_call(free_pool, si);
  92. }
  93. efi_status_t handle_kernel_image(unsigned long *image_addr,
  94. unsigned long *image_size,
  95. unsigned long *reserve_addr,
  96. unsigned long *reserve_size,
  97. efi_loaded_image_t *image,
  98. efi_handle_t image_handle)
  99. {
  100. const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
  101. int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
  102. unsigned long alloc_base, kernel_base;
  103. efi_status_t status;
  104. /*
  105. * Allocate space for the decompressed kernel as low as possible.
  106. * The region should be 16 MiB aligned, but the first 'slack' bytes
  107. * are not used by Linux, so we allow those to be occupied by the
  108. * firmware.
  109. */
  110. status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);
  111. if (status != EFI_SUCCESS) {
  112. efi_err("Unable to allocate memory for uncompressed kernel.\n");
  113. return status;
  114. }
  115. if ((alloc_base % EFI_PHYS_ALIGN) > slack) {
  116. /*
  117. * More than 'slack' bytes are already occupied at the base of
  118. * the allocation, so we need to advance to the next 16 MiB block.
  119. */
  120. kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);
  121. efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
  122. alloc_base, kernel_base);
  123. } else {
  124. kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN);
  125. }
  126. *reserve_addr = kernel_base + slack;
  127. *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
  128. /* now free the parts that we will not use */
  129. if (*reserve_addr > alloc_base) {
  130. efi_bs_call(free_pages, alloc_base,
  131. (*reserve_addr - alloc_base) / EFI_PAGE_SIZE);
  132. alloc_size -= *reserve_addr - alloc_base;
  133. }
  134. efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
  135. (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
  136. *image_addr = kernel_base + TEXT_OFFSET;
  137. *image_size = 0;
  138. efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
  139. *image_addr, *reserve_addr);
  140. return EFI_SUCCESS;
  141. }