Debian Codegraph @

grub-core/kern/riscv/efi/init.c

raw
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
20 #include <grub/env.h>
21 #include <grub/kernel.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/time.h>
25 #include <grub/efi/efi.h>
26 #include <grub/loader.h>
27
28 static grub_uint64_t timer_frequency_in_khz;
29
30 static grub_uint64_t
31 grub_efi_get_time_ms (void)
32 {
33 grub_uint64_t tmr;
34
35 #if __riscv_xlen == 64
36 asm volatile ("rdtime %0" : "=r"(tmr));
37 #else
38 grub_uint32_t lo, hi, tmp;
39 asm volatile ("1:\n"
40 "rdtimeh %0\n"
41 "rdtime %1\n"
42 "rdtimeh %2\n"
43 "bne %0, %2, 1b"
44 : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
45 tmr = ((grub_uint64_t)hi << 32) | lo;
46 #endif
47
48 return tmr / timer_frequency_in_khz;
49 }
50
51 void
52 grub_machine_init (void)
53 {
54 grub_uint64_t time_before, time_after;
55
56 grub_efi_init ();
57
58 /* Calculate timer frequency */
59 timer_frequency_in_khz = 1;
60 time_before = grub_efi_get_time_ms();
61 grub_efi_stall(1000);
62 time_after = grub_efi_get_time_ms();
63 timer_frequency_in_khz = time_after - time_before;
64
65 grub_install_get_time_ms (grub_efi_get_time_ms);
66 }
67
68 void
69 grub_machine_fini (int flags)
70 {
71 if (!(flags & GRUB_LOADER_FLAG_NORETURN))
72 return;
73
74 grub_efi_fini ();
75
76 if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
77 grub_efi_memory_fini ();
78 }