git: 3e9ff26d2ac7 - main - sdhci: Do not reprogram an unchanged clock

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 05 Nov 2026 14:51:13 UTC
The branch main has been updated by manu:

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

commit 3e9ff26d2ac7979ab840b9efcf3fdebec1a289db
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-27 06:41:57 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-05 14:50:42 +0000

    sdhci: Do not reprogram an unchanged clock
    
    sdhci_set_clock() compared the requested clock with slot->clock, which holds
    the rate that the host driver actually applied.  Some drivers do not apply
    the requested rate: the RK3568 one maps the 400kHz identification frequency
    to 375kHz.  The two values then never match and every update_ios call goes
    through SDHCI_SET_CLOCK() again, even when the clock did not change.
    
    That ends in clk_set_freq(), which takes a sleepable lock, from the done
    context of the mmc probe with the CAM locks held.  With another thread
    holding the clock topology lock this panics with "sleeping thread holds CAM
    device lock".
    
    Remember the last requested clock in a new field and compare against it.
    Clear it wherever slot->clock is cleared so that a reset still forces an
    update.
    
    Tested on a Quartz64 Model A with an NVMe drive on the PCIe slot, where the
    panic was seen: 5 cold boots out of 5 attached everything.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D61012
---
 sys/dev/sdhci/sdhci.c | 5 ++++-
 sys/dev/sdhci/sdhci.h | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/sys/dev/sdhci/sdhci.c b/sys/dev/sdhci/sdhci.c
index 88e790ad882a..5e2e868ccd7a 100644
--- a/sys/dev/sdhci/sdhci.c
+++ b/sys/dev/sdhci/sdhci.c
@@ -416,8 +416,9 @@ sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
 	uint16_t div;
 	int timeout;
 
-	if (clock == slot->clock)
+	if (clock == slot->req_clock)
 		return;
+	slot->req_clock = clock;
 	clock = SDHCI_SET_CLOCK(slot->bus, slot, clock);
 	slot->clock = clock;
 
@@ -1275,11 +1276,13 @@ sdhci_generic_reset(device_t brdev __unused, struct sdhci_slot *slot,
 		/* This is to force an update */
 		clock = slot->clock;
 		slot->clock = 0;
+		slot->req_clock = 0;
 		sdhci_set_clock(slot, clock);
 	}
 
 	if (mask & SDHCI_RESET_ALL) {
 		slot->clock = 0;
+		slot->req_clock = 0;
 		slot->power = 0;
 	}
 
diff --git a/sys/dev/sdhci/sdhci.h b/sys/dev/sdhci/sdhci.h
index 423e2d642149..0ce67bfad208 100644
--- a/sys/dev/sdhci/sdhci.h
+++ b/sys/dev/sdhci/sdhci.h
@@ -392,6 +392,7 @@ struct sdhci_slot {
 	uint32_t	retune_ticks;	/* Re-tuning callout ticks [hz] */
 	uint32_t	intmask;	/* Current interrupt mask */
 	uint32_t	clock;		/* Current clock freq. */
+	uint32_t	req_clock;	/* Last requested clock freq. */
 	size_t		offset;		/* Data buffer offset */
 	uint8_t		hostctrl;	/* Current host control register */
 	uint8_t		retune_count;	/* Controller re-tuning count [s] */