Skip to contentSkip to conversation

xtf: add a msleep helper

PATCH New Roger Pau Monne 1 participant 2 messages Mar 30, 2018 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 xtf: add a msleep helper

This requires importing some of the PV timecounter functionality from FreeBSD in order to access the PV time info.

---
Cc: Andrew Cooper <[email protected]>
---
Changes since v1:
- New in this version.

---
 arch/x86/include/arch/lib.h |  14 +++++
 build/files.mk              |   1 +
 common/pvtimer.c            | 136 ++++++++++++++++++++++++++++++++++++++++++++
 include/xtf/lib.h           |   5 ++
 4 files changed, 156 insertions(+)
 create mode 100644 common/pvtimer.c
arch/x86/include/arch/lib.h +14 -0
index 6714bdc..dedab70 100644
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline void pause(void)
+{
+    asm volatile ("pause");
+}
+
+static inline uint64_t rdtsc_ordered(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("lfence; mfence; rdtsc" : "=a" (low), "=d" (high));
+
+    return ((uint64_t)high << 32) | low;
+}
+
 #endif /* XTF_X86_LIB_H */
 
 /*
build/files.mk +1 -0
index 46b42d6..489f28a 100644
@@ -13,6 +13,7 @@ obj-perarch += $(ROOT)/common/lib.o
 obj-perarch += $(ROOT)/common/libc/stdio.o
 obj-perarch += $(ROOT)/common/libc/string.o
 obj-perarch += $(ROOT)/common/libc/vsnprintf.o
+obj-perarch += $(ROOT)/common/pvtimer.o
 obj-perarch += $(ROOT)/common/report.o
 obj-perarch += $(ROOT)/common/setup.o
 obj-perarch += $(ROOT)/common/xenbus.o
common/pvtimer.c new file +136 -0

136 lines not on this page · raw message

include/xtf/lib.h +5 -0
index abf8f25..17c4e5a 100644
@@ -101,6 +101,11 @@ int xtf_probe_sysctl_interface_version(void);
  */
 int xtf_get_domid(void);
 
+/**
+ * Sleep for ms milliseconds.
+ */
+void msleep(unsigned int ms);
+
 #endif /* XTF_LIB_H */
 
 /*
Signature · 7 lines
2.16.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 2/2 xtf: add minimal HPET functionality test

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

---
Cc: Andrew Cooper <[email protected]>
---
Changes since v1:
- Split HPET code into an HPET specific driver.
- Use the pv timecounter for busy waiting.
- Change test category to functional.
- Reduce timer test period to 1ms.

---
 arch/x86/hpet.c              | 117 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/include/arch/hpet.h |  69 +++++++++++++++++++++++++
 build/files.mk               |   1 +
 docs/all-tests.dox           |   2 +
 tests/hpet/Makefile          |   9 ++++
 tests/hpet/main.c            | 100 ++++++++++++++++++++++++++++++++++++
 6 files changed, 298 insertions(+)
 create mode 100644 arch/x86/hpet.c
 create mode 100644 arch/x86/include/arch/hpet.h
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/hpet.c new file +117 -0
new file mode 100644
index 0000000..6a84f3f
@@ -0,0 +1,117 @@
+/**
+ * @file arch/x86/hpet.c
+ *
+ * Basic x86 HPET driver.
+ */
+
+#include <xtf/lib.h>
+
+#include <xen/errno.h>
+
+#include <arch/hpet.h>
+
+static uint32_t period;
+unsigned int hpet_nr_timers;
+
+union hpet_timer {
+    uint32_t raw;
+    struct {
+        uint8_t                 :1;
+        bool level              :1;
+        bool enabled            :1;
+        bool periodic           :1;
+        uint8_t                 :4;
+        bool mode_32bit         :1;
+        uint8_t irq             :5;
+        uint32_t                :18;
+    };
+};
+
+#define MS_TO_NS        1000000
+#define NS_TO_FS        1000000
+
+uint32_t hpet_ms_to_ticks(unsigned int ms)
+{
+    return ((ms) * MS_TO_NS) / ((period) / 1000000);
+}
+
+int hpet_init(void)
+{
+    /* Sanity check main counter tick period. */
+    period = hpet_mmio_read(HPET_PERIOD);
+    if ( period == 0 || period > HPET_MAX_PERIOD )
+        return -ENODEV;
+
+    /* Get number of timers. */
+    hpet_nr_timers = ((hpet_mmio_read(HPET_ID) & HPET_ID_NUMBER) >>
+                      HPET_ID_NUMBER_SHIFT) + 1;
+
+    return 0;
+}
+
+uint32_t hpet_timer_avail_irqs(unsigned int nr)
+{
+    return hpet_mmio_read(HPET_Tn_CFG(nr) + 4);
+}
+
+bool hpet_timer_supports_periodic(unsigned int nr)
+{
+    return hpet_mmio_read(HPET_Tn_CFG(nr)) & HPET_TN_PERIODIC_CAP;
+}
+
+void hpet_init_timer(unsigned int nr, unsigned int irq, unsigned int ms,
+                     bool level, bool periodic)
+{
+    union hpet_timer tm = {
+        .level = level,
+        .enabled = true,
+        .periodic = periodic,
+        .mode_32bit = true,
+        .irq = irq,
+    };
+
+    /*
+     * Disable interrupts and reset main counter.
+     *
+     * Note that this is a testing HPET implementation and ATM we only expect
+     * a single timer to be tested simultaneously.
+     */
+    hpet_mmio_write(HPET_CFG, hpet_mmio_read(HPET_CFG) & ~HPET_CFG_ENABLE);
+    hpet_mmio_write(HPET_COUNTER, 0);
+
+    /* Configure timer and setup comparator. */
+    hpet_mmio_write(HPET_Tn_CFG(nr), tm.raw);
+    hpet_mmio_write(HPET_Tn_CMP(nr), hpet_ms_to_ticks(ms));
+
+    /* Enable main counter. */
+    hpet_mmio_write(HPET_CFG, hpet_mmio_read(HPET_CFG) | HPET_CFG_ENABLE);
+}
+
+void hpet_disable_timer(unsigned int nr)
+{
+    union hpet_timer tm = {
+        .enabled = false,
+    };
+
+    hpet_mmio_write(HPET_Tn_CFG(nr), tm.raw);
+}
+
+bool hpet_intr_pending(unsigned int nr)
+{
+    return (hpet_mmio_read(HPET_STATUS) >> nr) & 1;
+}
+
+void hpet_intr_pending_clear(unsigned int nr)
+{
+    hpet_mmio_write(HPET_STATUS, 1u << nr);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
arch/x86/include/arch/hpet.h new file +69 -0

69 lines not on this page · raw message

build/files.mk +1 -0
index 489f28a..9fa6d85 100644
@@ -30,6 +30,7 @@ obj-perenv += $(ROOT)/arch/x86/traps.o
 
 # HVM specific objects
 obj-hvm += $(ROOT)/arch/x86/apic.o
+obj-hvm += $(ROOT)/arch/x86/hpet.o
 obj-hvm += $(ROOT)/arch/x86/hvm/pagetables.o
 obj-hvm += $(ROOT)/arch/x86/hvm/traps.o
 
docs/all-tests.dox +2 -0
index 355cb80..1d0422f 100644
@@ -20,6 +20,8 @@ and functionality.
 
 @subpage test-fpu-exception-emulation - FPU Exception Emulation.  Covers XSA-190.
 
+@subpage test-hpet - HPET test.
+
 @subpage test-invlpg - `invlpg` instruction behaviour.
 
 @subpage test-lbr-tsx-vmentry - Haswell and later LBR/TSX Vmentry failure test.
tests/hpet/Makefile new file +9 -0
new file mode 100644
index 0000000..bb78b09
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := hpet
+CATEGORY  := functional
+TEST-ENVS := hvm32
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/hpet/main.c new file +100 -0

100 lines not on this page · raw message

Signature · 7 lines
2.16.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel