init.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 ("rdcycle %0" : "=r" (tmr));
  33. #else
  34. grub_uint32_t lo, hi, tmp;
  35. asm volatile (
  36. "1:\n"
  37. "rdcycleh %0\n"
  38. "rdcycle %1\n"
  39. "rdcycleh %2\n"
  40. "bne %0, %2, 1b"
  41. : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
  42. tmr = ((grub_uint64_t)hi << 32) | lo;
  43. #endif
  44. return tmr / timer_frequency_in_khz;
  45. }
  46. void
  47. grub_machine_init (void)
  48. {
  49. grub_uint64_t time_before, time_after;
  50. grub_efi_init ();
  51. /* Calculate timer frequency */
  52. timer_frequency_in_khz = 1;
  53. time_before = grub_efi_get_time_ms();
  54. grub_efi_stall(1000);
  55. time_after = grub_efi_get_time_ms();
  56. timer_frequency_in_khz = time_after - time_before;
  57. grub_install_get_time_ms (grub_efi_get_time_ms);
  58. }
  59. void
  60. grub_machine_fini (int flags)
  61. {
  62. if (!(flags & GRUB_LOADER_FLAG_NORETURN))
  63. return;
  64. grub_efi_fini ();
  65. }