init.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* init.c - initialize a riscv-based EFI system */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2018 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/env.h>
  20. #include <grub/kernel.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/efi/efi.h>
  25. #include <grub/loader.h>
  26. static grub_uint64_t timer_frequency_in_khz;
  27. static grub_uint64_t
  28. grub_efi_get_time_ms (void)
  29. {
  30. grub_uint64_t tmr;
  31. #if __riscv_xlen == 64
  32. asm volatile ("rdtime %0" : "=r"(tmr));
  33. #else
  34. grub_uint32_t lo, hi, tmp;
  35. asm volatile ("1:\n"
  36. "rdtimeh %0\n"
  37. "rdtime %1\n"
  38. "rdtimeh %2\n"
  39. "bne %0, %2, 1b"
  40. : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
  41. tmr = ((grub_uint64_t)hi << 32) | lo;
  42. #endif
  43. return tmr / timer_frequency_in_khz;
  44. }
  45. void
  46. grub_machine_init (void)
  47. {
  48. grub_uint64_t time_before, time_after;
  49. grub_efi_init ();
  50. /* Calculate timer frequency */
  51. timer_frequency_in_khz = 1;
  52. time_before = grub_efi_get_time_ms();
  53. grub_efi_stall(1000);
  54. time_after = grub_efi_get_time_ms();
  55. timer_frequency_in_khz = time_after - time_before;
  56. grub_install_get_time_ms (grub_efi_get_time_ms);
  57. }
  58. void
  59. grub_machine_fini (int flags)
  60. {
  61. if (!(flags & GRUB_LOADER_FLAG_NORETURN))
  62. return;
  63. grub_efi_fini ();
  64. if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
  65. grub_efi_memory_fini ();
  66. }