git: 4ce90d56a853 - main - asmc: notify the SMC of shutdown and restart

From: Abdelkader Boudih <seuros_at_FreeBSD.org>
Date: Sat, 07 Nov 2026 09:14:39 UTC
The branch main has been updated by seuros:

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

commit 4ce90d56a853431668aac4493eac9cc13ab79f26
Author:     Abdelkader Boudih <seuros@FreeBSD.org>
AuthorDate: 2026-11-07 09:14:28 +0000
Commit:     Abdelkader Boudih <seuros@FreeBSD.org>
CommitDate: 2026-11-07 09:14:28 +0000

    asmc: notify the SMC of shutdown and restart
    
    Nothing tells the SMC that a power off or restart was user requested, so
    the T2 keeps rebooting the machine thinking the system is wedged.
    
    Approved-by: ngie (co-mentor)
    
    Reviewed by:    ngie, adrian
    Differential Revision:  https://reviews.freebsd.org/D61260
---
 sys/dev/asmc/asmc.c    | 23 +++++++++++++++++++++++
 sys/dev/asmc/asmcvar.h |  7 +++++++
 2 files changed, 30 insertions(+)

diff --git a/sys/dev/asmc/asmc.c b/sys/dev/asmc/asmc.c
index 4eaa3bf06252..182f6d585080 100644
--- a/sys/dev/asmc/asmc.c
+++ b/sys/dev/asmc/asmc.c
@@ -40,11 +40,13 @@
 #include <sys/bus.h>
 #include <sys/conf.h>
 #include <sys/endian.h>
+#include <sys/eventhandler.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/module.h>
 #include <sys/mutex.h>
+#include <sys/reboot.h>
 #include <sys/sbuf.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
@@ -70,6 +72,7 @@ static int 	asmc_probe(device_t dev);
 static int 	asmc_attach(device_t dev);
 static int 	asmc_detach(device_t dev);
 static int 	asmc_resume(device_t dev);
+static void	asmc_shutdown(void *arg, int howto);
 
 /*
  * Backlight interface.
@@ -736,6 +739,10 @@ asmc_attach(device_t dev)
 			    "Apple security chip generation (RGEN; 3=T2)");
 	}
 
+	if (asmc_key_getinfo(dev, ASMC_KEY_MBSS, NULL, NULL) == 0)
+		sc->sc_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_pre_sync,
+		    asmc_shutdown, dev, SHUTDOWN_PRI_FIRST);
+
 	if (!sc->sc_has_sms)
 		goto nosms;
 
@@ -821,6 +828,11 @@ asmc_detach(device_t dev)
 {
 	struct asmc_softc *sc = device_get_softc(dev);
 
+	if (sc->sc_shutdown_tag != NULL) {
+		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_tag);
+		sc->sc_shutdown_tag = NULL;
+	}
+
 	if (sc->sc_kbd_bkl != NULL)
 		backlight_destroy(sc->sc_kbd_bkl);
 
@@ -877,6 +889,17 @@ asmc_resume(device_t dev)
 	return (0);
 }
 
+static void
+asmc_shutdown(void *arg, int howto)
+{
+	device_t dev = (device_t)arg;
+	uint8_t buf[2];
+
+	be16enc(buf, (howto & (RB_POWEROFF | RB_HALT)) != 0 ?
+	    ASMC_MBSS_POWEROFF : ASMC_MBSS_RESTART);
+	asmc_key_write(dev, ASMC_KEY_MBSS, buf, sizeof(buf));
+}
+
 #ifdef ASMC_DEBUG
 void
 asmc_dumpall(device_t dev)
diff --git a/sys/dev/asmc/asmcvar.h b/sys/dev/asmc/asmcvar.h
index 76c17f4ba2dd..32449a72cc48 100644
--- a/sys/dev/asmc/asmcvar.h
+++ b/sys/dev/asmc/asmcvar.h
@@ -90,6 +90,7 @@ struct asmc_softc {
 	int			sc_light_len;	/* ASMC_LIGHT_{SHORT,LONG}LEN */
 	int			sc_has_safespeed;
 	int			sc_has_alsl;	/* ALSL interrupt source */
+	eventhandler_tag	sc_shutdown_tag;
 };
 
 /*
@@ -197,6 +198,12 @@ struct asmc_softc {
  */
 #define ASMC_KEY_AUPO		"AUPO"	/* RW; 1 byte */
 
+/* Mac Boot State Status. */
+#define ASMC_KEY_MBSS		"MBSS"	/* RW; 2 bytes */
+
+#define ASMC_MBSS_POWEROFF	0x0001
+#define ASMC_MBSS_RESTART	0x0003
+
 /*
  * Interrupt keys.
  */