Module Name:    src
Committed By:   ad
Date:           Tue May 19 21:43:36 UTC 2020

Modified Files:
        src/sys/arch/x86/x86: tsc.c

Log Message:
If the the TSC timecounter is good then use the TSC for DELAY() too.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/arch/x86/x86/tsc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/tsc.c
diff -u src/sys/arch/x86/x86/tsc.c:1.44 src/sys/arch/x86/x86/tsc.c:1.45
--- src/sys/arch/x86/x86/tsc.c:1.44	Fri May  8 22:01:55 2020
+++ src/sys/arch/x86/x86/tsc.c	Tue May 19 21:43:36 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: tsc.c,v 1.44 2020/05/08 22:01:55 ad Exp $	*/
+/*	$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.44 2020/05/08 22:01:55 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.45 2020/05/19 21:43:36 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -52,6 +52,8 @@ __KERNEL_RCSID(0, "$NetBSD: tsc.c,v 1.44
 
 u_int	tsc_get_timecount(struct timecounter *);
 
+static void	tsc_delay(unsigned int);
+
 uint64_t	tsc_freq; /* exported for sysctl */
 static int64_t	tsc_drift_max = 1000;	/* max cycles */
 static int64_t	tsc_drift_observed;
@@ -145,9 +147,11 @@ tsc_is_invariant(void)
 }
 
 /*
- * Initialize timecounter(9) of TSC.
- * This function is called after all secondary processors were up and
- * calculated the drift.
+ * Initialize timecounter(9) and DELAY() function of TSC.
+ *
+ * This function is called after all secondary processors were brought up
+ * and drift has been measured, and after any other potential delay funcs
+ * have been installed (e.g. lapic_delay()).
  */
 void
 tsc_tc_init(void)
@@ -169,6 +173,9 @@ tsc_tc_init(void)
 		    (long long)tsc_drift_observed);
 		tsc_timecounter.tc_quality = -100;
 		invariant = false;
+	} else if (vm_guest == VM_GUEST_NO) {
+		delay_func = tsc_delay;
+		x86_delay = tsc_delay;
 	}
 
 	if (tsc_freq != 0) {
@@ -324,3 +331,16 @@ cpu_hascounter(void)
 
 	return cpu_feature[0] & CPUID_TSC;
 }
+
+static void
+tsc_delay(unsigned int us)
+{
+	uint64_t start, delta;
+
+	start = cpu_counter();
+	delta = (uint64_t)us * cpu_frequency(&cpu_info_primary) / 1000000;
+
+	while ((cpu_counter() - start) < delta) {
+		x86_pause();
+	}
+}

Reply via email to