[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCHv5 25/46] plat/kvm: Add Arm64 virtual timer library to provide ticks



Hi Wei,

On 08/10/2018 08:08 AM, Wei Chen wrote:
From: Wei Chen <Wei.Chen@xxxxxxx>

On KVM platform, print debug message will use ukplat_monotonic_clock
to provide timestamp. So we implement this simple virtual timer
library for timestamp.

Signed-off-by: Wei Chen <Wei.Chen@xxxxxxx>
---
  plat/common/arm/time.c | 123 +++++++++++++++++++++++++++++++++++++++++
  plat/kvm/Makefile.uk   |   1 +
  2 files changed, 124 insertions(+)
  create mode 100644 plat/common/arm/time.c

diff --git a/plat/common/arm/time.c b/plat/common/arm/time.c
new file mode 100644
index 0000000..4d955f3
--- /dev/null
+++ b/plat/common/arm/time.c
@@ -0,0 +1,123 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Wei Chen <Wei.Chen@xxxxxxx>
+ *
+ * Copyright (c) 2018, Arm Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+#include <uk/assert.h>
+#include <uk/plat/time.h>
+#include <cpu.h>
+
+static uint64_t boot_ticks;
+static uint32_t counter_freq;
+
+/*
+ * Shift factor for counter scaling multiplier; referred to as S in the
+ * following comments.
+ */
+static uint8_t counter_shift;
+
+/* Multiplier for converting counter ticks to nsecs. (0.S) fixed point. */
+static uint32_t ns_per_tick;
+
+/* How many nanoseconds per second */
+#define NSEC_PER_SEC ukarch_time_sec_to_nsec(1)
+
+static inline uint64_t ticks_to_ns(uint64_t ticks)
+{
+       return (ns_per_tick * ticks) >> counter_shift;
+}
+
+static inline uint64_t get_counter_frequency(void)

The frequency is a 32-bit value.

+{
+       return SYSREG_READ(cntfrq_el0);

Also, on a few platforms the frequency is not configured correctly by the firmware. A property in the DT (clock-frequency) has been introduced
to workaround those firmware. I would consider to support it here.

+}
+
+static inline uint64_t read_virtual_count(void)
+{
+       return SYSREG_READ(cntvct_el0);

On arm, this register is 64-bit as well. So I would introduce SYSREG_READ32 and SYSREG_READ64 to differentiate the size here.

SYSREG_READ would still be kept around for native size.

+}
+
+/*
+ * monotonic_clock(): returns # of nanoseconds passed since
+ * generic_timer_time_init()
+ */
+static __nsec generic_timer_monotonic(void)
+{
+       return (__nsec)ticks_to_ns(read_virtual_count() - boot_ticks);
+}
+
+/*
+ * Return epoch offset (wall time offset to monotonic clock start).
+ */
+static __u64  generic_timer_epochoffset(void)
+{
+       return 0;
+} > +
+static int generic_timer_init(void)
+{
+       /*
+        * Calculate counter shift factor and scaling multiplier.
+        *
+        * counter_shift (S) needs to be the largest (<=32) shift factor where
+        * the result of the counter_mult calculation below fits into uint32_t

counter_mult does not exist below.

+        * without truncation. Note that we disallow an S of zero to ensure
+        * the loop always terminates.
+        *
+        * (0.S) counter_mult = NSEC_PER_SEC (S.S) / counter_freq (S.0)
+        */
+       uint64_t tmp;
+
+       counter_freq = get_counter_frequency();
+       counter_shift = 32;
+       do {
+               tmp = (NSEC_PER_SEC << counter_shift) / counter_freq;
+               if ((tmp & 0xFFFFFFFF00000000L) == 0L)
+                       ns_per_tick = (uint32_t)tmp;
+               else
+                       counter_shift--;
+       } while (counter_shift > 0 && ns_per_tick == 0L);
+       UK_BUGON(!ns_per_tick);
+
+       /*
+        * Monotonic time begins at boot_ticks (first read of counter
+        * before calibration).
+        */
+       boot_ticks = read_virtual_count();
+
+       return 0;
+}
+
+struct ukplat_time_ops arch_timer_ops = {
+       .init = generic_timer_init,
+       .monotonic = generic_timer_monotonic,
+       .epochoffset = generic_timer_epochoffset,
+};
diff --git a/plat/kvm/Makefile.uk b/plat/kvm/Makefile.uk
index efeea31..2dc6db5 100644
--- a/plat/kvm/Makefile.uk
+++ b/plat/kvm/Makefile.uk
@@ -54,6 +54,7 @@ ifeq ($(findstring y,$(CONFIG_KVM_KERNEL_SERIAL_CONSOLE) 
$(CONFIG_KVM_DEBUG_SERI
  LIBKVMPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += 
$(UK_PLAT_COMMON_BASE)/arm/console.c|common
  endif
  LIBKVMPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += 
$(UK_PLAT_COMMON_BASE)/arm/cache64.S|common
+LIBKVMPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += 
$(UK_PLAT_COMMON_BASE)/arm/time.c|common
  LIBKVMPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(LIBKVMPLAT_BASE)/arm/entry64.S
  LIBKVMPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(LIBKVMPLAT_BASE)/arm/setup.c
  endif


Cheers,


--
Julien Grall

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.