riscv-stub.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Western Digital Corporation or its affiliates.
  4. */
  5. #include <linux/efi.h>
  6. #include <linux/libfdt.h>
  7. #include <asm/efi.h>
  8. #include <asm/sections.h>
  9. #include <asm/unaligned.h>
  10. #include "efistub.h"
  11. /*
  12. * RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and
  13. * 4MB for 32 bit.
  14. */
  15. #ifdef CONFIG_64BIT
  16. #define MIN_KIMG_ALIGN SZ_2M
  17. #else
  18. #define MIN_KIMG_ALIGN SZ_4M
  19. #endif
  20. typedef void __noreturn (*jump_kernel_func)(unsigned long, unsigned long);
  21. static unsigned long hartid;
  22. static int get_boot_hartid_from_fdt(void)
  23. {
  24. const void *fdt;
  25. int chosen_node, len;
  26. const void *prop;
  27. fdt = get_efi_config_table(DEVICE_TREE_GUID);
  28. if (!fdt)
  29. return -EINVAL;
  30. chosen_node = fdt_path_offset(fdt, "/chosen");
  31. if (chosen_node < 0)
  32. return -EINVAL;
  33. prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len);
  34. if (!prop)
  35. return -EINVAL;
  36. if (len == sizeof(u32))
  37. hartid = (unsigned long) fdt32_to_cpu(*(fdt32_t *)prop);
  38. else if (len == sizeof(u64))
  39. hartid = (unsigned long) fdt64_to_cpu(__get_unaligned_t(fdt64_t, prop));
  40. else
  41. return -EINVAL;
  42. return 0;
  43. }
  44. static efi_status_t get_boot_hartid_from_efi(void)
  45. {
  46. efi_guid_t boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID;
  47. struct riscv_efi_boot_protocol *boot_protocol;
  48. efi_status_t status;
  49. status = efi_bs_call(locate_protocol, &boot_protocol_guid, NULL,
  50. (void **)&boot_protocol);
  51. if (status != EFI_SUCCESS)
  52. return status;
  53. return efi_call_proto(boot_protocol, get_boot_hartid, &hartid);
  54. }
  55. efi_status_t check_platform_features(void)
  56. {
  57. efi_status_t status;
  58. int ret;
  59. status = get_boot_hartid_from_efi();
  60. if (status != EFI_SUCCESS) {
  61. ret = get_boot_hartid_from_fdt();
  62. if (ret) {
  63. efi_err("Failed to get boot hartid!\n");
  64. return EFI_UNSUPPORTED;
  65. }
  66. }
  67. return EFI_SUCCESS;
  68. }
  69. void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
  70. unsigned long fdt_size)
  71. {
  72. unsigned long stext_offset = _start_kernel - _start;
  73. unsigned long kernel_entry = entrypoint + stext_offset;
  74. jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
  75. /*
  76. * Jump to real kernel here with following constraints.
  77. * 1. MMU should be disabled.
  78. * 2. a0 should contain hartid
  79. * 3. a1 should DT address
  80. */
  81. csr_write(CSR_SATP, 0);
  82. jump_kernel(hartid, fdt);
  83. }
  84. efi_status_t handle_kernel_image(unsigned long *image_addr,
  85. unsigned long *image_size,
  86. unsigned long *reserve_addr,
  87. unsigned long *reserve_size,
  88. efi_loaded_image_t *image,
  89. efi_handle_t image_handle)
  90. {
  91. unsigned long kernel_size = 0;
  92. unsigned long preferred_addr;
  93. efi_status_t status;
  94. kernel_size = _edata - _start;
  95. *image_addr = (unsigned long)_start;
  96. *image_size = kernel_size + (_end - _edata);
  97. /*
  98. * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
  99. * address where kernel is booted. That's why kernel should boot from
  100. * as low as possible to avoid wastage of memory. Currently, dram_base
  101. * is occupied by the firmware. So the preferred address for kernel to
  102. * boot is next aligned address. If preferred address is not available,
  103. * relocate_kernel will fall back to efi_low_alloc_above to allocate
  104. * lowest possible memory region as long as the address and size meets
  105. * the alignment constraints.
  106. */
  107. preferred_addr = MIN_KIMG_ALIGN;
  108. status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
  109. preferred_addr, MIN_KIMG_ALIGN, 0x0);
  110. if (status != EFI_SUCCESS) {
  111. efi_err("Failed to relocate kernel\n");
  112. *image_size = 0;
  113. }
  114. return status;
  115. }