From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas Leonard Subject: [PATCH ARM v7 09/13] mini-os: arm: time Date: Fri, 8 Aug 2014 16:47:38 +0100 Message-ID: <1407512862-9373-10-git-send-email-talex5@gmail.com> References: <1407512862-9373-1-git-send-email-talex5@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XFmOJ-00078v-Ui for xen-devel@lists.xenproject.org; Fri, 08 Aug 2014 15:47:40 +0000 Received: by mail-wg0-f52.google.com with SMTP id a1so5680239wgh.11 for ; Fri, 08 Aug 2014 08:47:38 -0700 (PDT) In-Reply-To: <1407512862-9373-1-git-send-email-talex5@gmail.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Thomas Leonard , Dave.Scott@eu.citrix.com, anil@recoil.org, stefano.stabellini@eu.citrix.com, samuel.thibault@ens-lyon.org List-Id: xen-devel@lists.xenproject.org Based on an initial patch by Karim Raslan. Signed-off-by: Karim Allah Ahmed Signed-off-by: Thomas Leonard --- Addressed Julien Grall's comments: - Remove wall-clock time support (doesn't work on ARM anyway) --- extras/mini-os/arch/arm/time.c | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 extras/mini-os/arch/arm/time.c diff --git a/extras/mini-os/arch/arm/time.c b/extras/mini-os/arch/arm/time.c new file mode 100644 index 0000000..98e931f --- /dev/null +++ b/extras/mini-os/arch/arm/time.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include + +//#define VTIMER_DEBUG +#ifdef VTIMER_DEBUG +#define DEBUG(_f, _a...) \ + printk("MINI_OS(file=vtimer.c, line=%d) " _f , __LINE__, ## _a) +#else +#define DEBUG(_f, _a...) ((void)0) +#endif + +/************************************************************************ + * Time functions + *************************************************************************/ + +static uint64_t cntvct_at_init; +static uint32_t counter_freq; + +/* Compute with 96 bit intermediate result: (a*b)/c */ +uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) +{ + union { + uint64_t ll; + struct { + uint32_t low, high; + } l; + } u, res; + uint64_t rl, rh; + + u.ll = a; + rl = (uint64_t)u.l.low * (uint64_t)b; + rh = (uint64_t)u.l.high * (uint64_t)b; + rh += (rl >> 32); + res.l.high = rh / c; + res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; + return res.ll; +} + +static inline s_time_t ticks_to_ns(uint64_t ticks) +{ + return muldiv64(ticks, SECONDS(1), counter_freq); +} + +static inline uint64_t ns_to_ticks(s_time_t ns) +{ + return muldiv64(ns, counter_freq, SECONDS(1)); +} + +/* Wall-clock time is not currently available on ARM, so this is always zero for now: + * http://wiki.xenproject.org/wiki/Xen_ARM_TODO#Expose_Wallclock_time_to_guests + */ +static struct timespec shadow_ts; + +static inline uint64_t read_virtual_count(void) +{ + uint32_t c_lo, c_hi; + __asm__ __volatile__("isb;mrrc p15, 1, %0, %1, c14":"=r"(c_lo), "=r"(c_hi)); + return (((uint64_t) c_hi) << 32) + c_lo; +} + +/* monotonic_clock(): returns # of nanoseconds passed since time_init() + * Note: This function is required to return accurate + * time even in the absence of multiple timer ticks. + */ +uint64_t monotonic_clock(void) +{ + s_time_t time = ticks_to_ns(read_virtual_count() - cntvct_at_init); + //printk("monotonic_clock: %llu (%llu)\n", time, NSEC_TO_SEC(time)); + return time; +} + +int gettimeofday(struct timeval *tv, void *tz) +{ + uint64_t nsec = monotonic_clock(); + nsec += shadow_ts.tv_nsec; + + tv->tv_sec = shadow_ts.tv_sec; + tv->tv_sec += NSEC_TO_SEC(nsec); + tv->tv_usec = NSEC_TO_USEC(nsec % 1000000000UL); + + return 0; +} + +void set_vtimer_compare(uint64_t value) { + uint32_t x, y; + + DEBUG("New CompareValue : %llx\n", value); + x = 0xFFFFFFFFULL & value; + y = (value >> 32) & 0xFFFFFFFF; + + __asm__ __volatile__("mcrr p15, 3, %0, %1, c14" + ::"r"(x), "r"(y)); + + __asm__ __volatile__("mov %0, #0x1\n" + "mcr p15, 0, %0, c14, c3, 1\n" /* Enable timer and unmask the output signal */ + "isb":"=r"(x)); +} + +void unset_vtimer_compare(void) { + uint32_t x; + + __asm__ __volatile__("mov %0, #0x2\n" + "mcr p15, 0, %0, c14, c3, 1\n" /* Disable timer and mask the output signal */ + "isb":"=r"(x)); +} + +void block_domain(s_time_t until) +{ + uint64_t until_count = ns_to_ticks(until) + cntvct_at_init; + ASSERT(irqs_disabled()); + if (read_virtual_count() < until_count) + { + set_vtimer_compare(until_count); + //char buf[] = "sleep\n"; (void)HYPERVISOR_console_io(CONSOLEIO_write, strlen(buf), buf); + __asm__ __volatile__("wfi"); + //char wake[] = "wake\n"; (void)HYPERVISOR_console_io(CONSOLEIO_write, strlen(wake), wake); + unset_vtimer_compare(); + + /* Give the IRQ handler a chance to handle whatever woke us up. */ + local_irq_enable(); + local_irq_disable(); + } +} + +void init_time(void) +{ + printk("Initialising timer interface\n"); + + __asm__ __volatile__("mrc p15, 0, %0, c14, c0, 0":"=r"(counter_freq)); + cntvct_at_init = read_virtual_count(); + printk("Virtual Count register is %llx, freq = %d Hz\n", cntvct_at_init, counter_freq); +} + +void fini_time(void) +{ +} -- 2.0.3