Hi,

tpm(4) is the last driver in the tree using tvtohz(9).  There are no
remaining callers using tstohz(9), so if and when we remove tvtohz(9)
from tpm(4) we can remove both interfaces from the tree.

tpm(4) is tricky because it converts timeouts from milliseconds to
ticks and then doesn't use tsleep(9) at all.  It uses delay(9), which
takes a count of microseconds as argument.  This complicates the
conversion to tsleep_nsec(9) because the units don't match up for any
of these delays.  Also, delay(9) is incompatible with tsleep(9)
because tsleep(9) yields the CPU while delay(9) busy-waits.

I don't know if we *need* to delay(9) here.  What would happen if we
yielded the CPU with e.g. tsleep(9)?

The attached patch changes the delays to use the correct units.  This
is not the right thing, these timeouts are probably too large to spin
for in delay(9).  I'm just guessing here.

Aside: TPM_READ_TMO is *huge*.  2 minutes for a read timeout seems a
bit large.  NetBSD's TPM_READ_TMO has been dropped to 2 seconds, like
the other timeouts.

Also perhaps of note is that NetBSD's tpm(4) driver mostly no longer
uses delay(9).  They use tsleep(9) in all but one spot:

https://github.com/NetBSD/src/blob/fc83762bc464be0bf351901b2c387a8cfedff7c4/sys/dev/ic/tpm.c

Index: tpm.c
===================================================================
RCS file: /cvs/src/sys/dev/acpi/tpm.c,v
retrieving revision 1.10
diff -u -p -r1.10 tpm.c
--- tpm.c       22 May 2020 10:16:37 -0000      1.10
+++ tpm.c       19 Dec 2020 00:56:02 -0000
@@ -158,7 +158,6 @@ int tpm_request_locality(struct tpm_soft
 void   tpm_release_locality(struct tpm_softc *);
 int    tpm_getburst(struct tpm_softc *);
 uint8_t        tpm_status(struct tpm_softc *);
-int    tpm_tmotohz(int);
 
 struct cfattach tpm_ca = {
        sizeof(struct tpm_softc),
@@ -372,7 +371,7 @@ int
 tpm_request_locality(struct tpm_softc *sc, int l)
 {
        uint32_t r;
-       int to;
+       int msecs;
 
        if (l != 0)
                return EINVAL;
@@ -385,12 +384,12 @@ tpm_request_locality(struct tpm_softc *s
        bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
            TPM_ACCESS_REQUEST_USE);
 
-       to = tpm_tmotohz(TPM_ACCESS_TMO);
-
-       while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
-               DELAY(10);
+       for (msecs = 0; msecs < TPM_ACCESS_TMO; msecs++) {
+               r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS);
+               if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
+                   (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
+                       break;
+               DELAY(1000);
        }
 
        if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
@@ -418,12 +417,10 @@ tpm_release_locality(struct tpm_softc *s
 int
 tpm_getburst(struct tpm_softc *sc)
 {
-       int burst, burst2, to;
-
-       to = tpm_tmotohz(TPM_BURST_TMO);
+       int burst, burst2, msecs;
 
        burst = 0;
-       while (burst == 0 && to--) {
+       for (msecs = 0; msecs < TPM_BURST_TMO; msecs++) {
                /*
                 * Burst count has to be read from bits 8 to 23 without
                 * touching any other bits, eg. the actual status bits 0 to 7.
@@ -438,7 +435,7 @@ tpm_getburst(struct tpm_softc *sc)
                if (burst)
                        return burst;
 
-               DELAY(10);
+               DELAY(1000);
        }
 
        DPRINTF(("%s: getburst timed out\n", sc->sc_dev.dv_xname));
@@ -453,30 +450,19 @@ tpm_status(struct tpm_softc *sc)
 }
 
 int
-tpm_tmotohz(int tmo)
-{
-       struct timeval tv;
-
-       tv.tv_sec = tmo / 1000;
-       tv.tv_usec = 1000 * (tmo % 1000);
-
-       return tvtohz(&tv);
-}
-
-int
-tpm_waitfor(struct tpm_softc *sc, uint8_t mask, int tries)
+tpm_waitfor(struct tpm_softc *sc, uint8_t mask, int msecs)
 {
        uint8_t status;
 
        while (((status = tpm_status(sc)) & mask) != mask) {
-               if (tries == 0) {
+               if (msecs <= 0) {
                        DPRINTF(("%s: %s: timed out, status 0x%x != 0x%x\n",
                            sc->sc_dev.dv_xname, __func__, status, mask));
                        return status;
                }
 
-               tries--;
-               DELAY(1);
+               msecs -= 1000;
+               DELAY(1000);
        }
 
        return 0;

Reply via email to