tpm: Set timeouts and durations to microsecond values

Fix the timeouts and durations -- they are provided in microseconds.
Adapt the TPM driver for it.

Get TPM specific timeout and duration values earlier from the device.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
diff --git a/src/hw/tpm_drivers.c b/src/hw/tpm_drivers.c
index 0bf5997..0932797 100644
--- a/src/hw/tpm_drivers.c
+++ b/src/hw/tpm_drivers.c
@@ -10,8 +10,10 @@
 #include "config.h" // CONFIG_TPM_TIS_SHA1THRESHOLD
 #include "hw/tpm_drivers.h" // struct tpm_driver
 #include "std/tcg.h" // TCG_NO_RESPONSE
+#include "output.h" // warn_timeout
+#include "stacks.h" // yield
 #include "string.h" // memcpy
-#include "util.h" // msleep
+#include "util.h" // timer_calc_usec
 #include "x86.h" // readl
 
 static const u32 tis_default_timeouts[4] = {
@@ -93,15 +95,19 @@
         return 0;
 
     u32 rc = 1;
+    u32 end = timer_calc_usec(time);
 
-    while (time > 0) {
+    for (;;) {
         u8 sts = readb(TIS_REG(locty, TIS_REG_STS));
         if ((sts & mask) == expect) {
             rc = 0;
             break;
         }
-        msleep(1);
-        time--;
+        if (timer_check(end)) {
+            warn_timeout();
+            break;
+        }
+        yield();
     }
     return rc;
 }
@@ -178,18 +184,21 @@
 
     u32 rc = 0;
     u32 offset = 0;
-    u32 end = 0;
+    u32 end_loop = 0;
     u16 burst = 0;
-    u32 ctr = 0;
     u8 locty = tis_find_active_locality();
     u32 timeout_d = tpm_drivers[TIS_DRIVER_IDX].timeouts[TIS_TIMEOUT_TYPE_D];
+    u32 end = timer_calc_usec(timeout_d);
 
     do {
-        while (burst == 0 && ctr < timeout_d) {
+        while (burst == 0) {
                burst = readl(TIS_REG(locty, TIS_REG_STS)) >> 8;
             if (burst == 0) {
-                msleep(1);
-                ctr++;
+                if (timer_check(end)) {
+                    warn_timeout();
+                    break;
+                }
+                yield();
             }
         }
 
@@ -207,8 +216,8 @@
         }
 
         if (offset == len)
-            end = 1;
-    } while (end == 0);
+            end_loop = 1;
+    } while (end_loop == 0);
 
     return rc;
 }
diff --git a/src/hw/tpm_drivers.h b/src/hw/tpm_drivers.h
index 6357d02..ec50cca 100644
--- a/src/hw/tpm_drivers.h
+++ b/src/hw/tpm_drivers.h
@@ -66,12 +66,13 @@
 #define TIS_ACCESS_REQUEST_USE         (1 << 1) /* 0x02 */
 #define TIS_ACCESS_TPM_ESTABLISHMENT   (1 << 0) /* 0x01 */
 
-#define SCALER 10
-
-#define TIS_DEFAULT_TIMEOUT_A          (750  * SCALER)
-#define TIS_DEFAULT_TIMEOUT_B          (2000 * SCALER)
-#define TIS_DEFAULT_TIMEOUT_C          (750  * SCALER)
-#define TIS_DEFAULT_TIMEOUT_D          (750  * SCALER)
+/*
+ * Default TIS timeouts used before getting them from the TPM itself
+ */
+#define TIS_DEFAULT_TIMEOUT_A           750000 /* us */
+#define TIS_DEFAULT_TIMEOUT_B          2000000 /* us */
+#define TIS_DEFAULT_TIMEOUT_C           750000 /* us */
+#define TIS_DEFAULT_TIMEOUT_D           750000 /* us */
 
 enum tisTimeoutType {
     TIS_TIMEOUT_TYPE_A = 0,
@@ -80,8 +81,12 @@
     TIS_TIMEOUT_TYPE_D,
 };
 
-#define TPM_DEFAULT_DURATION_SHORT     (2000  * SCALER)
-#define TPM_DEFAULT_DURATION_MEDIUM    (20000 * SCALER)
-#define TPM_DEFAULT_DURATION_LONG      (60000 * SCALER)
+/*
+ * Default command durations used before getting them from the
+ * TPM itself
+ */
+#define TPM_DEFAULT_DURATION_SHORT      2000000 /* us */
+#define TPM_DEFAULT_DURATION_MEDIUM    20000000 /* us */
+#define TPM_DEFAULT_DURATION_LONG      60000000 /* us */
 
 #endif /* TPM_DRIVERS_H */
diff --git a/src/tcgbios.c b/src/tcgbios.c
index decf0fd..38bc737 100644
--- a/src/tcgbios.c
+++ b/src/tcgbios.c
@@ -459,6 +459,10 @@
     if (rc || returnCode)
         goto err_exit;
 
+    rc = determine_timeouts();
+    if (rc)
+        goto err_exit;
+
     rc = build_and_send_cmd(0, TPM_ORD_SelfTestFull, NULL, 0,
                             NULL, 0, &returnCode, TPM_DURATION_TYPE_LONG);
 
@@ -477,10 +481,6 @@
     if (rc || (returnCode != 0 && returnCode != TPM_BAD_LOCALITY))
         goto err_exit;
 
-    rc = determine_timeouts();
-    if (rc)
-        goto err_exit;
-
     rc = tpm_smbios_measure();
     if (rc)
         goto err_exit;