]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Rework timer frquency reading
authorJan Janssen <medhefgo@web.de>
Wed, 12 Apr 2023 21:45:04 +0000 (23:45 +0200)
committerJan Janssen <medhefgo@web.de>
Thu, 13 Apr 2023 13:39:14 +0000 (15:39 +0200)
This is in preparation for the next commit.

src/boot/efi/ticks.c

index 84dfdc454263d7db1a85c61095869b874e72ac8b..7dd808ae19b067265d3f79e166e0cb959b2f30c1 100644 (file)
@@ -5,7 +5,8 @@
 #include "vmm.h"
 
 #if defined(__i386__) || defined(__x86_64__)
-static uint64_t ticks_read(void) {
+
+static uint64_t ticks_read_arch(void) {
         /* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero
          * at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful
          * for keeping time, hence don't attempt to use it. */
@@ -14,36 +15,51 @@ static uint64_t ticks_read(void) {
 
         return __builtin_ia32_rdtsc();
 }
+
+static uint64_t ticks_freq_arch(void) {
+        return 0;
+}
+
 #elif defined(__aarch64__)
-static uint64_t ticks_read(void) {
+
+static uint64_t ticks_read_arch(void) {
         uint64_t val;
         asm volatile("mrs %0, cntvct_el0" : "=r"(val));
         return val;
 }
-#else
-static uint64_t ticks_read(void) {
-        return 0;
-}
-#endif
 
-#if defined(__aarch64__)
-static uint64_t ticks_freq(void) {
+static uint64_t ticks_freq_arch(void) {
         uint64_t freq;
         asm volatile("mrs %0, cntfrq_el0" : "=r"(freq));
         return freq;
 }
+
 #else
-/* count TSC ticks during a millisecond delay */
+
+static uint64_t ticks_read_arch(void) {
+        return 0;
+}
+
+static uint64_t ticks_freq_arch(void) {
+        return 0;
+}
+
+#endif
+
 static uint64_t ticks_freq(void) {
-        uint64_t ticks_start, ticks_end;
         static uint64_t cache = 0;
 
         if (cache != 0)
                 return cache;
 
-        ticks_start = ticks_read();
+        cache = ticks_freq_arch();
+        if (cache != 0)
+                return cache;
+
+        /* As a fallback, count ticks during a millisecond delay. */
+        uint64_t ticks_start = ticks_read_arch();
         BS->Stall(1000);
-        ticks_end = ticks_read();
+        uint64_t ticks_end = ticks_read_arch();
 
         if (ticks_end < ticks_start) /* Check for an overflow (which is not that unlikely, given on some
                                       * archs the value is 32bit) */
@@ -52,16 +68,13 @@ static uint64_t ticks_freq(void) {
         cache = (ticks_end - ticks_start) * 1000UL;
         return cache;
 }
-#endif
 
 uint64_t time_usec(void) {
-        uint64_t ticks, freq;
-
-        ticks = ticks_read();
+        uint64_t ticks = ticks_read_arch();
         if (ticks == 0)
                 return 0;
 
-        freq = ticks_freq();
+        uint64_t freq = ticks_freq();
         if (freq == 0)
                 return 0;