git: d6f6160b8ced - stable/15 - tpm: crb: Correct the Pluton mailbox handshake

From: Kevin Bowling <kbowling_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 01:16:48 UTC
The branch stable/15 has been updated by kbowling:

URL: https://cgit.FreeBSD.org/src/commit/?id=d6f6160b8ced163d024c4abeb78aaac11a0b1428

commit d6f6160b8ced163d024c4abeb78aaac11a0b1428
Author:     Kevin Bowling <kbowling@FreeBSD.org>
AuthorDate: 2026-11-04 19:43:28 +0000
Commit:     Kevin Bowling <kbowling@FreeBSD.org>
CommitDate: 2026-11-13 01:16:39 +0000

    tpm: crb: Correct the Pluton mailbox handshake
    
    Pluton's REPLY register indicates readiness for a new notification, not
    completion of the notification just sent.  Wait for REPLY=1 before
    ringing the mailbox, then wait for START=0 when a synchronous state
    change needs acknowledgement.
    
    Use a separate command notification callback that waits for mailbox
    readiness but leaves command completion to the existing CRB_CTRL_START
    polling.  Bound readiness waits by TPM_TIMEOUT_C, including asynchronous
    idle notifications, while retaining the command's execution timeout.
    
    This resolves reported command timeouts on a Framework AMD laptop.
    
    Tested by:      olivier, tuexen
    Sponsored by:   BBOX.io
    Differential Revision:  https://reviews.freebsd.org/D61128
    
    (cherry picked from commit 4995fd4bf2c61e30363e588c5b90d7fc9145bf5e)
---
 sys/dev/tpm/tpm_crb.c | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/sys/dev/tpm/tpm_crb.c b/sys/dev/tpm/tpm_crb.c
index ac472ce40eda..6e77655c3aab 100644
--- a/sys/dev/tpm/tpm_crb.c
+++ b/sys/dev/tpm/tpm_crb.c
@@ -81,7 +81,7 @@ struct tpmcrb_sc;
 /* Attach/detach callbacks */
 typedef bool (sm_attach_t)(struct tpmcrb_sc *, void *, size_t);
 typedef void (sm_detach_t)(struct tpmcrb_sc *);
-/* State change notification (timeout == 0 for 'no timeout') */
+/* State change notification (timeout == 0 for no completion wait). */
 typedef bool (sm_statechange_t)(struct tpmcrb_sc *, int);
 
 struct tpmcrb_sm_cfg {
@@ -94,6 +94,7 @@ struct tpmcrb_sm_cfg {
 static sm_attach_t		pluton_attach;
 static sm_detach_t		pluton_detach;
 static sm_statechange_t		pluton_doorbell;
+static sm_statechange_t		pluton_cmdready;
 
 static const struct tpmcrb_sm_cfg_map {
 	int				acpi_sm;
@@ -112,7 +113,7 @@ static const struct tpmcrb_sm_cfg_map {
 			.sm_attach = &pluton_attach,
 			.sm_detach = &pluton_detach,
 			.sm_statechange = &pluton_doorbell,
-			.sm_cmdready = &pluton_doorbell,
+			.sm_cmdready = &pluton_cmdready,
 		},
 	},
 };
@@ -830,15 +831,15 @@ pluton_detach(struct tpmcrb_sc *crb_sc)
 }
 
 static bool
-pluton_wait_reply(struct tpmcrb_sc *crb_sc, int32_t timeout)
+pluton_wait_reg(struct tpmcrb_sc *crb_sc, bus_space_handle_t bsh,
+    uint32_t value, int32_t timeout)
 {
 	for (;;) {
 		/*
 		 * Always read at least once, and try one more time after we hit
 		 * the timeout.
 		 */
-		if (bus_space_read_4(crb_sc->pluton.bst,
-		    crb_sc->pluton.reply_bsh, 0) == 1)
+		if (bus_space_read_4(crb_sc->pluton.bst, bsh, 0) == value)
 			return (true);
 		else if (timeout <= 0)
 			break;
@@ -853,18 +854,38 @@ pluton_wait_reply(struct tpmcrb_sc *crb_sc, int32_t timeout)
 static bool
 pluton_doorbell(struct tpmcrb_sc *crb_sc, int timeout)
 {
+
+	/*
+	 * REPLY indicates readiness for a new notification, not its completion.
+	 */
+	if (!pluton_wait_reg(crb_sc, crb_sc->pluton.reply_bsh, 1,
+	    TPM_TIMEOUT_C)) {
+		device_printf(crb_sc->base.dev,
+		    "Pluton mailbox readiness timed out\n");
+		return (false);
+	}
+
 	bus_space_write_4(crb_sc->pluton.bst, crb_sc->pluton.start_bsh, 0, 1);
 	bus_space_barrier(crb_sc->pluton.bst, crb_sc->pluton.start_bsh, 0,
 	    sizeof(uint32_t), BUS_SPACE_BARRIER_WRITE);
 
-	/*
-	 * We assume at timeout == 0 that they're not really interested in a
-	 * reply immediately.  Otherwise, we poll for a reply at least once,
-	 * even with low timeouts.
-	 */
+	/* Wait for consumption of synchronous state-change notifications. */
 	if (timeout <= 0)
 		return (true);
-	return (pluton_wait_reply(crb_sc, timeout));
+	if (!pluton_wait_reg(crb_sc, crb_sc->pluton.start_bsh, 0, timeout)) {
+		device_printf(crb_sc->base.dev,
+		    "Pluton mailbox acknowledgement timed out\n");
+		return (false);
+	}
+	return (true);
+}
+
+static bool
+pluton_cmdready(struct tpmcrb_sc *crb_sc, int timeout __unused)
+{
+
+	/* Command completion is polled separately through CRB_CTRL_START. */
+	return (pluton_doorbell(crb_sc, 0));
 }
 
 /* ACPI Driver */