1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #include <xen/acpi.h>
3 #include <xen/device_tree.h>
4 #include <xen/init.h>
5 #include <xen/lib.h>
6 #include <xen/sections.h>
7 #include <xen/types.h>
8 
9 unsigned long __ro_after_init cpu_khz; /* CPU clock frequency in kHz. */
10 uint64_t __ro_after_init boot_clock_cycles;
11 
get_s_time(void)12 s_time_t get_s_time(void)
13 {
14     uint64_t ticks = get_cycles() - boot_clock_cycles;
15 
16     return ticks_to_ns(ticks);
17 }
18 
19 /* Set up the timer on the boot CPU (early init function) */
preinit_dt_xen_time(void)20 static void __init preinit_dt_xen_time(void)
21 {
22     static const struct dt_device_match __initconstrel timer_ids[] =
23     {
24         DT_MATCH_PATH("/cpus"),
25         { /* sentinel */ },
26     };
27     struct dt_device_node *timer;
28     uint32_t rate;
29 
30     timer = dt_find_matching_node(NULL, timer_ids);
31     if ( !timer )
32         panic("Unable to find a compatible timer in the device tree\n");
33 
34     dt_device_set_used_by(timer, DOMID_XEN);
35 
36     if ( !dt_property_read_u32(timer, "timebase-frequency", &rate) )
37         panic("Unable to find clock frequency\n");
38 
39     cpu_khz = rate / 1000;
40 }
41 
preinit_xen_time(void)42 void __init preinit_xen_time(void)
43 {
44     if ( acpi_disabled )
45         preinit_dt_xen_time();
46     else
47         panic("%s: ACPI isn't supported\n", __func__);
48 
49     boot_clock_cycles = get_cycles();
50 }
51