c789522
From 0b158c30810db86f549cabd6e6021ab15962370d Mon Sep 17 00:00:00 2001
da63b36
From: Peter Jones <pjones@redhat.com>
da63b36
Date: Tue, 7 Nov 2017 17:12:17 -0500
c789522
Subject: [PATCH 197/229] Make pmtimer tsc calibration not take 51 seconds to
da63b36
 fail.
da63b36
da63b36
On my laptop running at 2.4GHz, if I run a VM where tsc calibration
da63b36
using pmtimer will fail presuming a broken pmtimer, it takes ~51 seconds
da63b36
to do so (as measured with the stopwatch on my phone), with a tsc delta
da63b36
of 0x1cd1c85300, or around 125 billion cycles.
da63b36
da63b36
If instead of trying to wait for 5-200ms to show up on the pmtimer, we try
ec4acbb
to wait for 5-200us, it decides it's broken in ~0x2626aa0 TSCs, aka ~2.4
da63b36
million cycles, or more or less instantly.
da63b36
da63b36
Additionally, this reading the pmtimer was returning 0xffffffff anyway,
da63b36
and that's obviously an invalid return.  I've added a check for that and
da63b36
0 so we don't bother waiting for the test if what we're seeing is dead
da63b36
pins with no response at all.
da63b36
ec4acbb
If "debug" is includes "pmtimer", you will see one of the following
ec4acbb
three outcomes.  If pmtimer gives all 0 or all 1 bits, you will see:
ec4acbb
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 1
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 2
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 3
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 4
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 5
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 6
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 7
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 8
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 9
ec4acbb
kern/i386/tsc_pmtimer.c:77: pmtimer: 0xffffff bad_reads: 10
ec4acbb
kern/i386/tsc_pmtimer.c:78: timer is broken; giving up.
ec4acbb
ec4acbb
This outcome was tested using qemu+kvm with UEFI (OVMF) firmware and
ec4acbb
these options: -machine pc-q35-2.10 -cpu Broadwell-noTSX
ec4acbb
ec4acbb
If pmtimer gives any other bit patterns but is not actually marching
ec4acbb
forward fast enough to use for clock calibration, you will see:
ec4acbb
ec4acbb
kern/i386/tsc_pmtimer.c:121: pmtimer delta is 0x0 (1904 iterations)
ec4acbb
kern/i386/tsc_pmtimer.c:124: tsc delta is implausible: 0x2626aa0
ec4acbb
ec4acbb
This outcome was tested using grub compiled with GRUB_PMTIMER_IGNORE_BAD_READS
ec4acbb
defined (so as not to trip the bad read test) using qemu+kvm with UEFI
ec4acbb
(OVMF) firmware, and these options: -machine pc-q35-2.10 -cpu Broadwell-noTSX
ec4acbb
ec4acbb
If pmtimer actually works, you'll see something like:
ec4acbb
ec4acbb
kern/i386/tsc_pmtimer.c:121: pmtimer delta is 0x0 (1904 iterations)
ec4acbb
kern/i386/tsc_pmtimer.c:124: tsc delta is implausible: 0x2626aa0
ec4acbb
ec4acbb
This outcome was tested using qemu+kvm with UEFI (OVMF) firmware, and
ec4acbb
these options: -machine pc-i440fx-2.4 -cpu Broadwell-noTSX
ec4acbb
ec4acbb
I've also tested this outcome on a real Intel Xeon E3-1275v3 on an Intel
ec4acbb
Server Board S1200V3RPS using the SDV.RP.B8 "Release" build here:
ec4acbb
https://firmware.intel.com/sites/default/files/UEFIDevKit_S1200RP_vB8.zip
ec4acbb
da63b36
Signed-off-by: Peter Jones <pjones@redhat.com>
da63b36
---
ec4acbb
 grub-core/kern/i386/tsc_pmtimer.c | 109 +++++++++++++++++++++++++++++++-------
ec4acbb
 1 file changed, 89 insertions(+), 20 deletions(-)
da63b36
da63b36
diff --git a/grub-core/kern/i386/tsc_pmtimer.c b/grub-core/kern/i386/tsc_pmtimer.c
ec4acbb
index c9c36169978..ca15c3aacd7 100644
da63b36
--- a/grub-core/kern/i386/tsc_pmtimer.c
da63b36
+++ b/grub-core/kern/i386/tsc_pmtimer.c
ec4acbb
@@ -28,40 +28,101 @@
ec4acbb
 #include <grub/acpi.h>
ec4acbb
 #include <grub/cpu/io.h>
ec4acbb
 
ec4acbb
+/*
ec4acbb
+ * Define GRUB_PMTIMER_IGNORE_BAD_READS if you're trying to test a timer that's
ec4acbb
+ * present but doesn't keep time well.
ec4acbb
+ */
ec4acbb
+// #define GRUB_PMTIMER_IGNORE_BAD_READS
ec4acbb
+
ec4acbb
 grub_uint64_t
ec4acbb
 grub_pmtimer_wait_count_tsc (grub_port_t pmtimer,
ec4acbb
 			     grub_uint16_t num_pm_ticks)
ec4acbb
 {
ec4acbb
   grub_uint32_t start;
ec4acbb
-  grub_uint32_t last;
ec4acbb
-  grub_uint32_t cur, end;
ec4acbb
+  grub_uint64_t cur, end;
da63b36
   grub_uint64_t start_tsc;
da63b36
   grub_uint64_t end_tsc;
ec4acbb
-  int num_iter = 0;
ec4acbb
+  unsigned int num_iter = 0;
ec4acbb
+#ifndef GRUB_PMTIMER_IGNORE_BAD_READS
da63b36
+  int bad_reads = 0;
ec4acbb
+#endif
da63b36
 
da63b36
-  start = grub_inl (pmtimer) & 0xffffff;
ec4acbb
-  last = start;
ec4acbb
+  /*
ec4acbb
+   * Some timers are 24-bit and some are 32-bit, but it doesn't make much
ec4acbb
+   * difference to us.  Caring which one we have isn't really worth it since
ec4acbb
+   * the low-order digits will give us enough data to calibrate TSC.  So just
ec4acbb
+   * mask the top-order byte off.
ec4acbb
+   */
ec4acbb
+  cur = start = grub_inl (pmtimer) & 0xffffffUL;
da63b36
   end = start + num_pm_ticks;
da63b36
   start_tsc = grub_get_tsc ();
da63b36
   while (1)
da63b36
     {
da63b36
-      cur = grub_inl (pmtimer) & 0xffffff;
ec4acbb
-      if (cur < last)
ec4acbb
-	cur |= 0x1000000;
ec4acbb
-      num_iter++;
ec4acbb
+      cur &= 0xffffffffff000000ULL;
ec4acbb
+      cur |= grub_inl (pmtimer) & 0xffffffUL;
ec4acbb
+
ec4acbb
+      end_tsc = grub_get_tsc();
da63b36
+
ec4acbb
+#ifndef GRUB_PMTIMER_IGNORE_BAD_READS
ec4acbb
+      /*
ec4acbb
+       * If we get 10 reads in a row that are obviously dead pins, there's no
ec4acbb
+       * reason to do this thousands of times.
da63b36
+       */
ec4acbb
+      if (cur == 0xffffffUL || cur == 0)
da63b36
+	{
da63b36
+	  bad_reads++;
ec4acbb
+	  grub_dprintf ("pmtimer",
ec4acbb
+			"pmtimer: 0x%"PRIxGRUB_UINT64_T" bad_reads: %d\n",
ec4acbb
+			cur, bad_reads);
ec4acbb
+	  grub_dprintf ("pmtimer", "timer is broken; giving up.\n");
da63b36
+
da63b36
+	  if (bad_reads == 10)
da63b36
+	    return 0;
da63b36
+	}
ec4acbb
+#endif
da63b36
+
ec4acbb
+      if (cur < start)
ec4acbb
+	cur += 0x1000000;
da63b36
+
da63b36
       if (cur >= end)
da63b36
 	{
ec4acbb
-	  end_tsc = grub_get_tsc ();
ec4acbb
+	  grub_dprintf ("pmtimer", "pmtimer delta is 0x%"PRIxGRUB_UINT64_T"\n",
ec4acbb
+			cur - start);
ec4acbb
+	  grub_dprintf ("pmtimer", "tsc delta is 0x%"PRIxGRUB_UINT64_T"\n",
da63b36
+			end_tsc - start_tsc);
da63b36
 	  return end_tsc - start_tsc;
da63b36
 	}
da63b36
-      /* Check for broken PM timer.
da63b36
-	 50000000 TSCs is between 5 ms (10GHz) and 200 ms (250 MHz)
da63b36
-	 if after this time we still don't have 1 ms on pmtimer, then
da63b36
-	 pmtimer is broken.
ec4acbb
+
ec4acbb
+      /*
ec4acbb
+       * Check for broken PM timer.  1ms at 10GHz should be 1E+7 TSCs; at
ec4acbb
+       * 250MHz it should be 2.5E6.  So if after 4E+7 TSCs on a 10GHz machine,
ec4acbb
+       * we should have seen pmtimer show 4ms of change (i.e. cur =~
ec4acbb
+       * start+14320); on a 250MHz machine that should be 16ms (start+57280).
ec4acbb
+       * If after this a time we still don't have 1ms on pmtimer, then pmtimer
ec4acbb
+       * is broken.
ec4acbb
+       *
ec4acbb
+       * Likewise, if our code is perfectly efficient and introduces no delays
ec4acbb
+       * whatsoever, on a 10GHz system we should see a TSC delta of 3580 in
ec4acbb
+       * ~3580 iterations.  On a 250MHz machine that should be ~900 iterations.
ec4acbb
+       *
ec4acbb
+       * With those factors in mind, there are two limits here.  There's a hard
ec4acbb
+       * limit here at 8x our desired pm timer delta, picked as an arbitrarily
ec4acbb
+       * large value that's still not a lot of time to humans, because if we
ec4acbb
+       * get that far this is either an implausibly fast machine or the pmtimer
ec4acbb
+       * is not running.  And there's another limit on 4x our 10GHz tsc delta
ec4acbb
+       * without seeing cur converge on our target value.
da63b36
        */
da63b36
-      if ((num_iter & 0xffffff) == 0 && grub_get_tsc () - start_tsc > 5000000) {
da63b36
-	return 0;
da63b36
-      }
ec4acbb
+      if ((++num_iter > (grub_uint32_t)num_pm_ticks << 3UL) ||
ec4acbb
+	  end_tsc - start_tsc > 40000000)
da63b36
+	{
ec4acbb
+	  grub_dprintf ("pmtimer",
ec4acbb
+			"pmtimer delta is 0x%"PRIxGRUB_UINT64_T" (%u iterations)\n",
ec4acbb
+			cur - start, num_iter);
ec4acbb
+	  grub_dprintf ("pmtimer",
ec4acbb
+			"tsc delta is implausible: 0x%"PRIxGRUB_UINT64_T"\n",
da63b36
+			end_tsc - start_tsc);
da63b36
+	  return 0;
da63b36
+	}
da63b36
     }
da63b36
 }
da63b36
 
ec4acbb
@@ -74,12 +135,20 @@ grub_tsc_calibrate_from_pmtimer (void)
ec4acbb
 
ec4acbb
   fadt = grub_acpi_find_fadt ();
ec4acbb
   if (!fadt)
ec4acbb
-    return 0;
ec4acbb
+    {
ec4acbb
+      grub_dprintf ("pmtimer", "No FADT found; not using pmtimer.\n");
ec4acbb
+      return 0;
ec4acbb
+    }
ec4acbb
   pmtimer = fadt->pmtimer;
ec4acbb
   if (!pmtimer)
ec4acbb
-    return 0;
ec4acbb
+    {
ec4acbb
+      grub_dprintf ("pmtimer", "FADT does not specify pmtimer; skipping.\n");
ec4acbb
+      return 0;
ec4acbb
+    }
ec4acbb
 
ec4acbb
-  /* It's 3.579545 MHz clock. Wait 1 ms.  */
ec4acbb
+  /*
ec4acbb
+   * It's 3.579545 MHz clock. Wait 1 ms.
ec4acbb
+   */
ec4acbb
   tsc_diff = grub_pmtimer_wait_count_tsc (pmtimer, 3580);
ec4acbb
   if (tsc_diff == 0)
ec4acbb
     return 0;
da63b36
-- 
ec4acbb
2.15.0
da63b36