Skip to contentSkip to conversation

Add macsmc support for Intel Macs, replacing applesmc

PATCH In review Atharva Tiwari 8 participants 57 messages Dec 4

Subsystems (20)
x86 ACPI PCI GPIO +16 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Add macsmc support for Intel Macs, replacing applesmc

applesmc is very old, and all of its code is lumped into one driver. Fan control, sensors, the light sensor, the motion sensor and the keyboard backlight in a single file with its own way of talking to the SMC. It is hard to extend and to support new machines.

This series adds Intel Mac support to the macsmc drivers that were written for Apple Silicon (the Asahi implementation). The SMC works the same way on both platforms, so we share a lot of code with Asahi. Only the way of talking to the SMC differs.

To make that work, patch 1 splits the macsmc driver into a common core and a backend. The existing RTKit code moves into its own backend. Patch 2 adds an ACPI backend for Intel Macs with I/O port, MMIO and interrupt support. After that, each function gets its own small driver using the standard kernel interfaces (hwmon, IIO, LEDs, power supply, RTC, watchdog), instead of one large driver doing everything.

On Intel Macs these drivers replace applesmc (hwmon, light sensor, motion sensor and keyboard backlight) and acpi-sbs (battery and adapter). They are mutually exclusive with the drivers they replace, so only one is used at a time.

The series is organised as follows:

  1      Split macsmc into a core and backends
  2      ACPI backend for Intel Macs
  3-6    New drivers: watchdog, IIO light and accelerometer,
         keyboard backlight LED
  7-9    RTC support for T2 Macs, a T2 detection flag, and
         disabling the broken ACPI RTCs
  10-11  Power supply support, replacing acpi-sbs
  12-13  hwmon support for Intel Macs
  14     ioctl for userspace access to SMC keys

Tested-on: MacBookPro16,1 , iMac20,1 , MacBookAir4,2 , MacBookPro12,1 , MacBookPro15,1, MacBookPro9,2, MacBookAir5,2.

Atharva Tiwari (14):
treewide: Add backend for macsmc
platform/x86: add backend for macsmc in intel macs
watchdog: add driver for apple smc
iio/light: add new driver for macsmc
iio/accel: add driver for apple SMS
leds: add support for macsmc kbl
rtc: add support for x86 macs in macsmc-rtc
treewide: Add a flag to detect the Apple T2 chip
treewide: disable other rtc's in t2 macs power/supply: add support for x86 macs in macsmc-power
acpi: disable battery and adapter for x86 apple machines hwmon/macsmc: add more types
hwmon: add support for x86 macs in macsmc-hwmon mfd/macsmc: add ioctl to access smc keys

 MAINTAINERS                                   |    2 +-
 arch/x86/kernel/quirks.c                      |   37 +
 arch/x86/kernel/rtc.c                         |    5 +
 drivers/acpi/acpi_tad.c                       |    5 +
 drivers/acpi/sbs.c                            |    5 +-
 drivers/acpi/x86/utils.c                      |    5 +
 drivers/gpio/Kconfig                          |    2 +-
 drivers/gpio/gpio-macsmc.c                    |   13 +-
 drivers/hwmon/Kconfig                         |    7 +-
 drivers/hwmon/macsmc-hwmon.c                  | 1152 ++++++++++++++++-
 drivers/iio/accel/Kconfig                     |   10 +
 drivers/iio/accel/Makefile                    |    1 +
 drivers/iio/accel/macsmc-accel.c              |  260 ++++
 drivers/iio/light/Kconfig                     |   11 +
 drivers/iio/light/Makefile                    |    1 +
 drivers/iio/light/macsmc-light.c              |  209 +++
 drivers/leds/Kconfig                          |   11 +
 drivers/leds/Makefile                         |    1 +
 drivers/leds/leds-macsmc.c                    |   91 ++
 drivers/mfd/Kconfig                           |   15 +-
 drivers/mfd/Makefile                          |    2 +-
 drivers/mfd/macsmc-core.c                     |  347 +++++
 drivers/platform/x86/Kconfig                  |   10 +
 drivers/platform/x86/Makefile                 |    1 +
 drivers/platform/x86/macsmc-acpi.c            |  695 ++++++++++
 drivers/power/reset/Kconfig                   |    2 +-
 drivers/power/supply/Kconfig                  |    2 +-
 drivers/power/supply/macsmc-power.c           |  803 +++++++++---
 drivers/rtc/Kconfig                           |    2 +-
 drivers/rtc/rtc-cmos.c                        |    5 +
 drivers/rtc/rtc-macsmc.c                      |  132 +-
 drivers/soc/apple/Kconfig                     |   14 +
 drivers/soc/apple/Makefile                    |    2 +
 .../macsmc.c => soc/apple/macsmc-rtkit.c}     |  185 ++-
 drivers/watchdog/Kconfig                      |   11 +
 drivers/watchdog/Makefile                     |    1 +
 drivers/watchdog/macsmc_wdt.c                 |  158 +++
 include/linux/mfd/macsmc.h                    |  153 +--
 include/linux/platform_data/x86/apple.h       |    5 +
 include/uapi/linux/macsmc.h                   |  119 ++
 .../platform_certs/keyring_handler.h          |    8 -
 security/integrity/platform_certs/load_uefi.c |   38 +-
 42 files changed, 4090 insertions(+), 448 deletions(-)
 create mode 100644 drivers/iio/accel/macsmc-accel.c
 create mode 100644 drivers/iio/light/macsmc-light.c
 create mode 100644 drivers/leds/leds-macsmc.c
 create mode 100644 drivers/mfd/macsmc-core.c
 create mode 100644 drivers/platform/x86/macsmc-acpi.c
 rename drivers/{mfd/macsmc.c => soc/apple/macsmc-rtkit.c} (70%)
 create mode 100644 drivers/watchdog/macsmc_wdt.c
 create mode 100644 include/uapi/linux/macsmc.h
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/14 treewide: Add backend for macsmc

Split the Apple SMC driver into a common core and a backend that handles the actual communication. The existing RTKit code moves to its own backend, and nothing changes for Apple Silicon.

This lets the same SMC drivers run on other kinds of hardware, starting with Intel Macs, as a replacement for applesmc.

---
 MAINTAINERS                                   |   2 +-
 drivers/gpio/Kconfig                          |   2 +-
 drivers/gpio/gpio-macsmc.c                    |  13 +-
 drivers/hwmon/Kconfig                         |   2 +-
 drivers/mfd/Kconfig                           |  15 +-
 drivers/mfd/Makefile                          |   2 +-
 drivers/mfd/macsmc-core.c                     | 164 ++++++++++++++++
 drivers/power/reset/Kconfig                   |   2 +-
 drivers/power/supply/Kconfig                  |   2 +-
 drivers/power/supply/macsmc-power.c           |   4 +-
 drivers/rtc/Kconfig                           |   2 +-
 drivers/soc/apple/Kconfig                     |  14 ++
 drivers/soc/apple/Makefile                    |   2 +
 .../macsmc.c => soc/apple/macsmc-rtkit.c}     | 185 ++++++++----------
 include/linux/mfd/macsmc.h                    |  92 ++++++---
 15 files changed, 337 insertions(+), 166 deletions(-)
 create mode 100644 drivers/mfd/macsmc-core.c
 rename drivers/{mfd/macsmc.c => soc/apple/macsmc-rtkit.c} (70%)
MAINTAINERS +1 -1
index 03649f08c..1e5ae12ce 100644
@@ -2727,7 +2727,7 @@ F:	drivers/input/touchscreen/apple_z2.c
 F:	drivers/iommu/apple-dart.c
 F:	drivers/iommu/io-pgtable-dart.c
 F:	drivers/irqchip/irq-apple-aic.c
-F:	drivers/mfd/macsmc.c
+F:	drivers/mfd/macsmc-core.c
 F:	drivers/nvme/host/apple.c
 F:	drivers/nvmem/apple-efuses.c
 F:	drivers/nvmem/apple-spmi-nvmem.c
drivers/gpio/Kconfig +1 -1
index 55e428002..63a2b1e2e 100644
@@ -1582,7 +1582,7 @@ config GPIO_LP87565
 
 config GPIO_MACSMC
 	tristate "Apple Mac SMC GPIO"
-	depends on MFD_MACSMC
+	depends on MACSMC_RTKIT
 	help
 	  Support for GPIOs controlled by the SMC microcontroller on Apple Mac
 	  systems.
drivers/gpio/gpio-macsmc.c +1 -12
index b0952d066..8c7a942ad 100644
@@ -97,20 +97,9 @@ static int macsmc_gpio_find_first_gpio_index(struct macsmc_gpio *smcgp)
 {
 	struct apple_smc *smc = smcgp->smc;
 	smc_key key = macsmc_gpio_key(0);
-	smc_key first_key, last_key;
 	int start, count, ret;
 
-	/* Return early if the key is out of bounds */
-	ret = apple_smc_get_key_by_index(smc, 0, &first_key);
-	if (ret)
-		return ret;
-	if (key <= first_key)
-		return -ENODEV;
-
-	ret = apple_smc_get_key_by_index(smc, smc->key_count - 1, &last_key);
-	if (ret)
-		return ret;
-	if (key > last_key)
+	if (key <= smc->first_key || key > smc->last_key)
 		return -ENODEV;
 
 	/* Binary search to find index of first SMC key bigger or equal to key */
drivers/hwmon/Kconfig +1 -1
index 18c682e1a..91ca7031c 100644
@@ -1245,7 +1245,7 @@ config SENSORS_LTQ_CPUTEMP
 
 config SENSORS_MACSMC_HWMON
 	tristate "Apple SMC (Apple Silicon)"
-	depends on MFD_MACSMC && OF
+	depends on MFD_MACSMC_CORE
 	help
 	  This driver enables hwmon support for current, power, temperature,
 	  and voltage sensors, as well as fan speed reporting and control
drivers/mfd/Kconfig +3 -12
index 87ed55c15..c58cc3315 100644
@@ -339,24 +339,15 @@ config MFD_LOCHNAGAR
 	help
 	  Support for Cirrus Logic Lochnagar audio development board.
 
-config MFD_MACSMC
-	tristate "Apple Silicon System Management Controller (SMC)"
-	depends on ARCH_APPLE || COMPILE_TEST
-	depends on OF
-	depends on APPLE_RTKIT
+config MFD_MACSMC_CORE
+	tristate "Apple System Management Controller (SMC) Core"
 	select MFD_CORE
 	help
-	  The System Management Controller (SMC) on Apple Silicon machines is a
+	  The System Management Controller (SMC) on Apple machines is a
 	  piece of hardware that exposes various functionalities such as
 	  temperature sensors, voltage/power meters, shutdown/reboot handling,
 	  GPIOs and more.
 
-	  Communication happens via a shared mailbox using the RTKit protocol
-	  which is also used for other co-processors. The SMC protocol then
-	  allows reading and writing many different keys which implement the
-	  various features. The MFD core device handles this protocol and
-	  exposes it to the sub-devices.
-
 config MFD_MADERA
 	tristate "Cirrus Logic Madera codecs"
 	select MFD_CORE
drivers/mfd/Makefile +1 -1
index f8101d2a9..0b0ccf91e 100644
@@ -22,7 +22,7 @@ obj-$(CONFIG_MFD_CS42L43_SDW)	+= cs42l43-sdw.o
 obj-$(CONFIG_MFD_ENE_KB3930)	+= ene-kb3930.o
 obj-$(CONFIG_MFD_EXYNOS_LPASS)	+= exynos-lpass.o
 obj-$(CONFIG_MFD_GATEWORKS_GSC)	+= gateworks-gsc.o
-obj-$(CONFIG_MFD_MACSMC)	+= macsmc.o
+obj-$(CONFIG_MFD_MACSMC_CORE)	+= macsmc-core.o
 
 obj-$(CONFIG_MFD_TI_LP873X)	+= lp873x.o
 obj-$(CONFIG_MFD_TI_LP87565)	+= lp87565.o
drivers/mfd/macsmc-core.c new file +164 -0

164 lines not on this page · raw message

drivers/power/reset/Kconfig +1 -1
index bce996bbe..14d62afc1 100644
@@ -142,7 +142,7 @@ config POWER_RESET_MCF_RCM
 
 config POWER_RESET_MACSMC
 	tristate "Apple SMC reset/power-off driver"
-	depends on MFD_MACSMC
+	depends on MACSMC_RTKIT
 	help
 	  This driver supports reset and power-off on Apple Mac machines
 	  that implement this functionality via the SMC.
drivers/power/supply/Kconfig +1 -1
index b89ef40df..6e944cc59 100644
@@ -1193,7 +1193,7 @@ config FUEL_GAUGE_MM8013
 
 config MACSMC_POWER
 	tristate "Apple SMC Battery and Power Driver"
-	depends on MFD_MACSMC
+	depends on MFD_MACSMC_CORE
 	help
 	  This driver provides support for the battery and AC adapter on
 	  Apple Silicon machines. It exposes battery telemetry (voltage,
drivers/power/supply/macsmc-power.c +2 -2
index ef735f863..ced37a701 100644
@@ -859,7 +859,7 @@ static int macsmc_power_probe(struct platform_device *pdev)
 		return -ENODEV;
 
 	power->nb.notifier_call = macsmc_power_event;
-	blocking_notifier_chain_register(&smc->event_handlers, &power->nb);
+	blocking_notifier_chain_register(smc->event_handlers, &power->nb);
 
 	return 0;
 }
@@ -868,7 +868,7 @@ static void macsmc_power_remove(struct platform_device *pdev)
 {
 	struct macsmc_power *power = dev_get_drvdata(&pdev->dev);
 
-	blocking_notifier_chain_unregister(&power->smc->event_handlers, &power->nb);
+	blocking_notifier_chain_unregister(power->smc->event_handlers, &power->nb);
 }
 
 static const struct platform_device_id macsmc_power_id[] = {
drivers/rtc/Kconfig +1 -1
index 05b9233b9..8cc27891b 100644
@@ -2096,7 +2096,7 @@ config RTC_DRV_WILCO_EC
 
 config RTC_DRV_MACSMC
 	tristate "Apple Mac System Management Controller RTC"
-	depends on MFD_MACSMC
+	depends on MFD_MACSMC_CORE
 	help
 	  If you say yes here you get support for RTC functions
 	  inside Apple SPMI PMUs accessed through the SoC's
drivers/soc/apple/Kconfig +14 -0
index db30c0ade..e89bc9cb4 100644
@@ -52,6 +52,20 @@ config APPLE_TUNABLE
 	tristate
 	depends on ARCH_APPLE || COMPILE_TEST
 
+config MACSMC_RTKIT
+	tristate "Apple Silicon System Management Controller (SMC) Backend"
+	depends on ARCH_APPLE || COMPILE_TEST
+	depends on OF
+	depends on APPLE_RTKIT
+	depends on MFD_MACSMC_CORE
+	help
+	  MACSMC RTKIT allows communicatation with the System Management
+	  Controller on M-series Macs. It is required to use GPIO, control fans
+	  and to control battery start and end threshold.
+
+	  Say 'y' here if you have an Apple Soc
+
+
 endmenu
 
 endif
drivers/soc/apple/Makefile +2 -0
index 0481f7f43..7227cf69e 100644
@@ -13,3 +13,5 @@ apple-sart-y = sart.o
 
 obj-$(CONFIG_APPLE_TUNABLE) += apple-tunable.o
 apple-tunable-y = tunable.o
+
+obj-$(CONFIG_MACSMC_RTKIT) += macsmc-rtkit.o
drivers/soc/apple/macsmc-rtkit.c renamed from drivers/mfd/macsmc.c +80 -105

347 lines not on this page · raw message

include/linux/mfd/macsmc.h +64 -28

123 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 01/14] treewide: Add backend for macsmc

Thank you for your contribution! Sashiko AI review found 9 potential issues to consider: - [High] mfd: macsmc: Kconfig split disables Apple SMC drivers in arm64 defconfig - [Medium] mfd: macsmc…

Thank you for your contribution! Sashiko AI review found 9 potential issues to consider:
- [High] mfd: macsmc: Kconfig split disables Apple SMC drivers in arm64 defconfig
- [Medium] mfd: macsmc: notifications left enabled if apple_smc_probe() fails early
- [Low] mfd: macsmc: endianness type mismatch in apple_smc_probe()
- [Low] mfd: macsmc: signed %d format specifier used for u32 key_count
- [Low] mfd: macsmc: non-standard subject prefix and incomplete commit message
- [Low] mfd: macsmc-core: apple_smc_probe() passes raw -1 to devm_mfd_add_devices()
- [Low] mfd: macsmc: undocumented swab32() in core apple_smc_get_key_by_index()
- [Low] mfd: macsmc: missing NULL checks on optional ops and backend locking docs
- [Low] mfd: macsmc.h retains unused RTKit header include and RTKit-only definitions
--

commit 97275525c07f52eb987554687e4771a43c630a9d
Author: Atharva Tiwari <[email protected]>

Quoted 7 lines

treewide: Add backend for macsmc

Split the Apple SMC driver into a common core and a backend that handles the actual communication. The existing RTKit code moves to its own backend, and nothing changes for Apple Silicon.

Quoted 8 lines

This lets the same SMC drivers run on other kinds of hardware, starting with Intel Macs, as a replacement for applesmc.

[Severity: Low]
The MFD convention for commit subjects is "mfd: macsmc: <Capitalized description>" rather than "treewide:". The changelog also states that nothing changes for Apple Silicon, omitting the Kconfig rename, dropping the OF dependency from SENSORS_MACSMC_HWMON, renaming the RTKit platform driver from macsmc to macsmc-rtkit, caching first_key and last_key in apple_smc_probe(), and adding the ACPI MFD cells and SMC_INT_* macros.

[ ... ]

Quoted 6 lines
drivers/mfd/Kconfig +0 -0
index 87ed55c155f03..c58cc3315f22a 100644
@@ -339,24 +339,15 @@ config MFD_LOCHNAGAR
 	help

drivers/mfd/Kconfig config MFD_LOCHNAGAR · line 344

Quoted 10 lines
drivers/mfd/Kconfig +2 -5
 	  Support for Cirrus Logic Lochnagar audio development board.
 
-config MFD_MACSMC
-	tristate "Apple Silicon System Management Controller (SMC)"
-	depends on ARCH_APPLE || COMPILE_TEST
-	depends on OF
-	depends on APPLE_RTKIT
+config MFD_MACSMC_CORE
+	tristate "Apple System Management Controller (SMC) Core"
 	select MFD_CORE

[Severity: High]
Does deleting CONFIG_MFD_MACSMC silently disable the Apple Silicon SMC driver stack in arm64 defconfig and existing configs?

arch/arm64/configs/defconfig still sets CONFIG_MFD_MACSMC=m and is not updated in this series, so make defconfig and olddefconfig drop the SMC core, RTKit backend, GPIO, reboot/power-off, hwmon, and RTC drivers on Apple Silicon.

Making MFD_MACSMC_CORE a hidden symbol selected by MACSMC_RTKIT and updating arm64 defconfig would avoid breaking existing builds. Renaming the RTKit platform driver from macsmc to macsmc-rtkit also changes the sysfs driver path and module name on Apple Silicon.

[ ... ]

Quoted 5 lines
drivers/mfd/macsmc-core.c new file +0 -0
new file mode 100644
index 0000000000000..b8905c834c448

[ ... ]

Quoted 11 lines
drivers/mfd/macsmc-core.c new file +11 -0
+int apple_smc_enter_atomic(struct apple_smc *smc)
+{
+	guard(mutex)(&smc->mutex);
+	return smc->be->enter_atomic(smc->be_cookie);
+}
+EXPORT_SYMBOL(apple_smc_enter_atomic);
+
+int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, size_t size)
+{
+	guard(spinlock_irqsave)(&smc->lock);
+	return smc->be->write_atomic(smc->be_cookie, key, buf, size);

drivers/mfd/macsmc-core.c line 70

Quoted 10 lines
drivers/mfd/macsmc-core.c new file +10 -0
+}
+EXPORT_SYMBOL(apple_smc_write_atomic);
+
+int apple_smc_rw(struct apple_smc *smc, smc_key key, const void *wbuf, size_t wsize,
+		void *rbuf, size_t rsize)
+{
+	guard(mutex)(&smc->mutex);
+	return smc->be->rw(smc->be_cookie, key, wbuf, wsize, rbuf, rsize);
+}
+EXPORT_SYMBOL(apple_smc_rw);

[Severity: Low]
Should these wrappers check for NULL before invoking enter_atomic, write_atomic, and rw, and return -EOPNOTSUPP when unimplemented?

Commit "platform/x86: add backend for macsmc in intel macs" leaves those three callbacks NULL in apple_smc_acpi_be_ops, so calling them on an ACPI backend would cause a NULL pointer dereference.

The kernel-doc for struct apple_smc_backend_ops in include/linux/mfd/macsmc.h could also document which callbacks are optional and their locking context (smc->mutex for sleeping ops vs smc->lock with spin_lock_irqsave for write_atomic).

drivers/mfd/macsmc-core.c line 81

Quoted 11 lines
drivers/mfd/macsmc-core.c new file +11 -0
+
+int apple_smc_get_key_by_index(struct apple_smc *smc, int index, smc_key *key)
+{
+	guard(mutex)(&smc->mutex);
+	int ret;
+
+	ret = smc->be->get_key_by_index(smc->be_cookie, index, key);
+	*key = swab32(*key);
+
+	return ret;
+}

[Severity: Low]
Should the byte swap in apple_smc_get_key_by_index() live in the backends rather than the generic core?

Every other callback in struct apple_smc_backend_ops uses host-order smc_key values, and get_key_by_index does not document that it must return a byte-swapped key. Moving the conversion into each backend would keep the interface consistent with typedef smc_key.

[ ... ]

Quoted 6 lines
drivers/mfd/macsmc-core.c new file +6 -0
+int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
+		void *cookie, struct blocking_notifier_head *event_handlers, bool is_acpi)
+{
+	struct apple_smc *smc;
+	__be32 count;
+	int ret;

[ ... ]

drivers/mfd/macsmc-core.c line 121

Quoted 4 lines
drivers/mfd/macsmc-core.c new file +4 -0
+	ret = apple_smc_read_u32(smc, SMC_KEY(#KEY), &count);
+	if (ret < 0)
+		return ret;
+	smc->key_count = be32_to_cpu(count);

[Severity: Low]
Does passing __be32 *count to apple_smc_read_u32() reintroduce a Sparse -Wbitwise warning?

apple_smc_read_u32() takes a host-endian u32 *, which regresses 0a29aa605286 ("mfd: macsmc: Fix key count endianness annotation"). Reading into count with apple_smc_read() and checking the returned byte count avoids the type mismatch.

[ ... ]

Quoted 8 lines
drivers/mfd/macsmc-core.c new file +8 -0
+	if (smc->is_acpi) {
+		ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_common,
+			ARRAY_SIZE(apple_smc_acpi_common), NULL, 0, NULL);
+		if (ret < 0)
+			return ret;
+
+		if (apple_smc_key_exists(smc, SMC_KEY(CLKO))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_rtc,

drivers/mfd/macsmc-core.c line 154

Quoted 10 lines
drivers/mfd/macsmc-core.c new file +10 -0
+				ARRAY_SIZE(apple_smc_acpi_rtc), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
+
+	} else
+		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
+			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
+	if (ret < 0)
+		return ret;

[Severity: Low]
These devm_mfd_add_devices() calls pass a raw -1 instead of PLATFORM_DEVID_AUTO (preferred for new MFD code) or PLATFORM_DEVID_NONE, which the driver used before this split.

drivers/mfd/macsmc-core.c line 157

Quoted 3 lines
drivers/mfd/macsmc-core.c new file +3 -0
+
+	dev_info(dev, "Successfully Initialized (%d keys First key: %p4ch Last key: %p4ch)\n",
+			smc->key_count, &smc->first_key, &smc->last_key);

[Severity: Low]
smc->key_count is a u32, so %u would match its type better than %d.

[ ... ]

Quoted 7 lines
drivers/soc/apple/macsmc-rtkit.c renamed from drivers/mfd/macsmc.c +0 -0
similarity index 70%
rename from drivers/mfd/macsmc.c
rename to drivers/soc/apple/macsmc-rtkit.c
index 514cba7dc897a..eaebeb1be3734 100644

[ ... ]

Quoted 22 lines
drivers/soc/apple/macsmc-rtkit.c renamed from drivers/mfd/macsmc.c +1 -13
@@ -458,49 +450,32 @@ static int apple_smc_probe(struct platform_device *pdev)
 		return -EIO;
 	}
 
-	dev_set_drvdata(&pdev->dev, smc);
 	BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers);
 
-	ret = apple_smc_read(smc, SMC_KEY(#KEY), &count, sizeof(count));
-	if (ret >= 0 && ret != sizeof(count))
-		ret = -EINVAL;
-	if (ret < 0)
-		return dev_err_probe(smc->dev, ret, "Failed to get key count");
-	smc->key_count = be32_to_cpu(count);
-
 	/* Enable notifications */
-	apple_smc_write_flag(smc, SMC_KEY(NTAP), true);
-	ret = devm_add_action_or_reset(dev, apple_smc_disable_notifications, smc);
-	if (ret)
-		return ret;
+	apple_smc_rtkit_rw_locked(smc, SMC_KEY(NTAP), &flag, sizeof(flag), NULL, 0);
 
-	ret = devm_mfd_add_devices(smc->dev, PLATFORM_DEVID_NONE,

drivers/soc/apple/macsmc-rtkit.c static int apple_smc_probe(struct platform_device *pdev) · line 460

Quoted 10 lines
drivers/soc/apple/macsmc-rtkit.c renamed from drivers/mfd/macsmc.c +2 -7
-				   apple_smc_devs, ARRAY_SIZE(apple_smc_devs),
-				   NULL, 0, NULL);
-	if (ret)
-		return dev_err_probe(smc->dev, ret, "Failed to register sub-devices");
-
-
-	return 0;
+	return apple_smc_probe(dev, &apple_smc_rtkit_backend_ops, smc,
+			&smc->event_handlers, false);
 }

[Severity: Medium]
Does enabling NTAP notifications here leave them enabled if apple_smc_probe() fails early?

In apple_smc_probe(), devm_kzalloc() and the initial #KEY, first_key, and last_key queries all run before devm_add_action_or_reset() registers apple_smc_disable_notifications(). If any of those fail, probe unwinds without disabling notifications in firmware.

Enabling notifications through a backend callback right before devm_add_action_or_reset() in apple_smc_probe() (or registering the cleanup action immediately here) would keep enable and disable balanced.

[ ... ]

Quoted 5 lines
include/linux/mfd/macsmc.h +0 -0
index cc09ecce0df74..cdd040f0c010f 100644

include/linux/mfd/macsmc.h typedef u32 smc_key; · line 56

Quoted 10 lines
include/linux/mfd/macsmc.h +7 -0
@@ -47,6 +47,25 @@ typedef u32 smc_key;
 #define APPLE_SMC_WRITABLE BIT(6)
 #define APPLE_SMC_FUNCTION BIT(4)
 
+#define SMC_MSG_READ_KEY                0x10
+#define SMC_MSG_WRITE_KEY               0x11
+#define SMC_MSG_GET_KEY_BY_INDEX        0x12
+#define SMC_MSG_GET_KEY_INFO            0x13
+#define SMC_MSG_INITIALIZE              0x17
+#define SMC_MSG_NOTIFICATION            0x18
+#define SMC_MSG_RW_KEY                  0x20

[Severity: Low]
Can SMC_MSG_INITIALIZE, SMC_MSG_NOTIFICATION, SMC_MSG_RW_KEY, and enum apple_smc_boot_stage stay private to drivers/soc/apple/macsmc-rtkit.c?

They are only used by the RTKit backend across the series. This header also still includes <linux/soc/apple/rtkit.h> even though all RTKit fields were removed from struct apple_smc, pulling RTKit, mailbox, and OF headers into generic and x86 consumers.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 01/14] treewide: Add backend for macsmc

In reply to Atharva Tiwari
Quoted 8 lines · Atharva Tiwari

Split the Apple SMC driver into a common core and a backend that handles the actual communication. The existing RTKit code moves to its own backend, and nothing changes for Apple Silicon.

This lets the same SMC drivers run on other kinds of hardware, starting with Intel Macs, as a replacement for applesmc.

There are plenty of style related issues in this code, besides the fact that it begs for a split. Read mailing list archives to understand what the problems are.

...

drivers/mfd/macsmc-core.c line 12

Quoted 6 lines
drivers/mfd/macsmc-core.c new file +6 -0
+#include <linux/bitfield.h>
+#include <linux/device.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/mutex.h>
+#include <linux/spinlock.h>

Does it follow IWYU?

...

Quoted 1 line

+EXPORT_SYMBOL(apple_smc_enter_atomic);

Why no namespace is used?

...

drivers/mfd/macsmc-core.c line 59

Quoted 3 lines
drivers/mfd/macsmc-core.c new file +3 -0
+int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, size_t size)
+{
+	guard(spinlock_irqsave)(&smc->lock);

Delimit guard()() with blank line(s) so it will be easier to catch when reading the code.

Quoted 2 lines
(quoted diff) +2 -0
+	return smc->be->write_atomic(smc->be_cookie, key, buf, size);
+}

...

drivers/mfd/macsmc-core.c line 75

Quoted 4 lines
drivers/mfd/macsmc-core.c new file +4 -0
+int apple_smc_get_key_by_index(struct apple_smc *smc, int index, smc_key *key)
+{
+	guard(mutex)(&smc->mutex);
+	int ret;

No, we define variables at the top (yes, still).

drivers/mfd/macsmc-core.c line 81

Quoted 5 lines
drivers/mfd/macsmc-core.c new file +5 -0
+	ret = smc->be->get_key_by_index(smc->be_cookie, index, key);
+	*key = swab32(*key);
+
+	return ret;
+}

...

The above just something that I see briefly. But you need to revisit the whole code base.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw

Re: [PATCH 01/14] treewide: Add backend for macsmc

In reply to Atharva Tiwari
Quoted 9 lines · Atharva Tiwari

Split the Apple SMC driver into a common core and a backend that handles the actual communication. The existing RTKit code moves to its own backend, and nothing changes for Apple Silicon.

This lets the same SMC drivers run on other kinds of hardware, starting with Intel Macs, as a replacement for applesmc.

Quoted 5 lines

---

Why is this is a single patch? That makes review very hard and I'm pretty sure there's no need for "treewide: " here. You probably want at least few mfd patches that prepare the split, then one that moves the rtkit stuff to soc/apple and then probably another patch to add the ACPI specific stuff that's in this commit. And the gpio changes here probably should be separate as well.

Sven

#5 lore raw

PATCH 2/14 platform/x86: add backend for macsmc in intel macs

In reply to Atharva Tiwari

Add a backend that talks to the SMC on Intel Macs over ACPI. It supports the old I/O port interface, and also MMIO and interrupt-driven completion, which applesmc doesn't have. If an interrupt is missed it falls back to polling.

This is the base that lets the macsmc drivers replace applesmc.

---
 drivers/platform/x86/Kconfig       |  10 +
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/macsmc-acpi.c | 695 +++++++++++++++++++++++++++++
 3 files changed, 706 insertions(+)
 create mode 100644 drivers/platform/x86/macsmc-acpi.c
drivers/platform/x86/Kconfig +10 -0
index 4851bf451..00474f31b 100644
@@ -202,6 +202,16 @@ config ADV_SWBUTTON
 	  To compile this driver as a module, choose M here. The module will
 	  be called adv_swbutton.
 
+config MACSMC_ACPI
+	tristate "Apple SMC (System Management Controller) ACPI Backend"
+	depends on MFD_MACSMC_CORE && ACPI
+	help
+	  This driver allows communication with the SMC (System Management Controller)
+	  on Intel-Macs. It is used to control fans, control battery, and more.
+
+	  To compile this driver as a module, choose M here. The module will
+	  be called macsmc_acpi.
+
 config APPLE_GMUX
 	tristate "Apple Gmux Driver"
 	depends on ACPI && PCI
drivers/platform/x86/Makefile +1 -0
index cab20a414..933aed4a8 100644
@@ -28,6 +28,7 @@ obj-y				+= amd/
 obj-$(CONFIG_ADV_SWBUTTON)	+= adv_swbutton.o
 
 # Apple
+obj-$(CONFIG_MACSMC_ACPI)	+= macsmc-acpi.o
 obj-$(CONFIG_APPLE_GMUX)	+= apple-gmux.o
 
 # ASUS
drivers/platform/x86/macsmc-acpi.c new file +695 -0

695 lines not on this page · raw message

Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

Thank you for your contribution! Sashiko AI review found 20 potential issues to consider: - [High] platform/x86: macsmc-acpi: I/O port conflict with applesmc lacks Kconfig exclusion - [High]…

Thank you for your contribution! Sashiko AI review found 20 potential issues to consider:
- [High] platform/x86: macsmc-acpi: I/O port conflict with applesmc lacks Kconfig exclusion
- [High] platform/x86: macsmc-acpi: broken cmd_done completion wait in apple_smc_acpi_port_wait()
- [Medium] platform/x86: macsmc-acpi: completion race and missing status check in MMIO wait
- [Medium] platform/x86: macsmc-acpi: 1 ms poll interval inflates PIO/MMIO wait latency
- [Medium] platform/x86: macsmc-acpi: broken status check in apple_smc_acpi_irq()
- [Medium] platform/x86: macsmc-acpi: NTOK left enabled on early apple_smc_probe() fail
- [Medium] platform/x86: macsmc-acpi: unlocked flag read in apple_smc_acpi_resume()
- [Medium] platform/x86: macsmc-acpi: unchecked ACPI_COMPANION() in apple_smc_acpi_probe()
- [Medium] platform/x86: macsmc-acpi: omit PIO data flush in apple_smc_acpi_port_read()
- [Medium] platform/x86: apple_smc_acpi_resume() can skip restoring NTOK after resume
- [Medium] platform/x86: raw ACPI IRQ and missing flags in apple_smc_acpi_probe()
- [Medium] platform/x86: unchecked ACPI_COMPANION() in apple_smc_acpi_probe()
- [Medium] platform/x86: macsmc-acpi races with applesmc on SMC I/O ports
- [Low] platform/x86: macsmc-acpi: undocumented multi-value plimit sysfs attribute
- [Low] platform/x86: __be32 to u32 in apple_smc_acpi_mmio_get_key_info()
- [Low] platform/x86: void return expression in apple_smc_acpi_mmio_clear()
- [Low] platform/x86: macsmc-acpi: apple_smc_acpi_write() returns size instead of 0
- [Low] platform/x86: macsmc-acpi: devm_ioremap() without devm_request_mem_region()
- [Low] platform/x86: macsmc-acpi exposes undocumented plimit sysfs attribute
- [Low] platform/x86: raw byte array kept in apple_smc_acpi_port_get_key_info()
--

commit eb95b0b0da7b2552eb2f4941cef2db07a98b0710
Author: Atharva Tiwari <[email protected]>

platform/x86: add backend for macsmc in intel macs

This commit adds an ACPI backend driver for the Apple SMC on Intel Macs, supporting I/O port and MMIO transfers as well as interrupt-driven completion with polling fallback.

drivers/platform/x86/Kconfig config ADV_SWBUTTON · line 207

Quoted 11 lines
drivers/platform/x86/Kconfig +3 -0
index 9ce384e0475ea..7a061e48465b4 100644
@@ -202,6 +202,16 @@ config ADV_SWBUTTON
 	  To compile this driver as a module, choose M here. The module will
 	  be called adv_swbutton.
 
+config MACSMC_ACPI
+	tristate "Apple SMC (System Management Controller) ACPI Backend"
+	depends on MFD_MACSMC_CORE && ACPI

[Severity: High]
Can CONFIG_MACSMC_ACPI and CONFIG_SENSORS_APPLESMC be enabled at the same time without mutual exclusion?

Both drivers auto-load on Intel Macs and claim the same 32-byte I/O port region (0x300-0x31f). When both are built-in, drivers/Makefile links hwmon/ before platform/, so applesmc_init() always reserves the ports first and apple_smc_acpi_probe() fails with -EBUSY; when built as modules, they race at boot. Because later patches in this series disable acpi-sbs, ACPI battery/AC, rtc-cmos, and acpi-tad whenever CONFIG_MACSMC_ACPI is enabled, applesmc winning the port claim leaves those devices without any driver.

[ ... ]

Quoted 5 lines
drivers/platform/x86/macsmc-acpi.c new file +0 -0
new file mode 100644
index 0000000000000..8a93245c9d293

[ ... ]

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static int apple_smc_acpi_port_wait(struct apple_smc_acpi *smc, u8 val, u8 mask)
+{
+	u8 status;
+	int ret;

drivers/platform/x86/macsmc-acpi.c line 101

Quoted 10 lines
drivers/platform/x86/macsmc-acpi.c new file +10 -0
+
+	if (smc->interrupt_enabled) {
+		reinit_completion(&smc->cmd_done);
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}

[Severity: High]
Does apple_smc_acpi_port_wait() unconditionally block on cmd_done without checking the port status register first?

Every PIO read and write begins with apple_smc_acpi_port_valid() -> apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY) while the SMC is idle and no command is in flight, so the first PIO transaction on an interrupt-capable PIO Mac always stalls for 1000 ms under smc->mutex and permanently clears smc->interrupt_enabled.

In addition, calling reinit_completion(&smc->cmd_done) after outb() races with complete(&smc->cmd_done) in apple_smc_acpi_irq() and can lose the wakeup. Returning 0 on wakeup also skips verifying (status & mask) == val, and the fallback debug message says "mmio/wait" in the PIO path.

drivers/platform/x86/macsmc-acpi.c line 105

Quoted 3 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+	ret = read_poll_timeout(inb, status, (status & mask) == val,
+				USEC_PER_MSEC, SMC_TIMEOUT_MS * USEC_PER_MSEC,
+				false, smc->port_base + SMC_IO_CMD_PORT);

[Severity: Medium]
Does polling with a 1 ms sleep interval inflate PIO and MMIO wait latency?

read_poll_timeout() with USEC_PER_MSEC sleeps 251-1000 us on the first failed status check, both here and in apple_smc_acpi_mmio_wait(). Because each PIO key access performs ~10-13 byte-level status handshakes while holding smc->mutex, per-key latency grows from ~0.1-0.5 ms in applesmc (which starts at 8 us with backoff in wait_status()) to several milliseconds per key, stalling multi-key hwmon and power_supply queries.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 196

Quoted 11 lines
drivers/platform/x86/macsmc-acpi.c new file +11 -0
+	for (ret = 0; ret < len; ret++) {
+		if (apple_smc_acpi_port_wait(smc,
+				SMC_IO_STATUS_AWAITING_DATA | SMC_IO_STATUS_BUSY,
+				SMC_IO_STATUS_AWAITING_DATA | SMC_IO_STATUS_BUSY)) {
+			dev_dbg(smc->dev, "port/read: read failed, key: 0x%X\n", key);
+			return -EIO;
+		}
+		buffer[ret] = inb(smc->port_base + SMC_IO_DATA_PORT);
+	}
+
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);

[Severity: Medium]
Should apple_smc_acpi_port_read() drain unread bytes while SMC_IO_STATUS_AWAITING_DATA is set before waiting for SMC_IO_STATUS_BUSY to clear?

As described in commit 25f2bd7f5add ("hwmon: (applesmc) Always read until end of data"), 2012+ Intel SMCs can leave trailing bytes in the output FIFO, keeping SMC_IO_STATUS_BUSY asserted until drained. Without the flush loop from applesmc.c read_smc(), waiting directly for BUSY to clear times out after 1000 ms and leaves the SMC PIO state machine desynchronized, especially since apple_smc_read() does not validate the caller's length against key info.size.

[ ... ]

Quoted 7 lines
drivers/platform/x86/macsmc-acpi.c new file +7 -0
+static int apple_smc_acpi_port_get_key_info(struct apple_smc_acpi *smc,
+			smc_key key, struct apple_smc_key_info *info)
+{
+	u8 buf[6];
+
+	if (apple_smc_acpi_port_read(smc, SMC_MSG_GET_KEY_INFO, key, buf, sizeof(buf)))
+		return -EIO;

drivers/platform/x86/macsmc-acpi.c line 245

Quoted 10 lines
drivers/platform/x86/macsmc-acpi.c new file +10 -0
+
+	if (!info)
+		return 0;
+
+	info->size = buf[0];
+	info->type_code = get_unaligned_be32(&buf[1]);
+	info->flags = buf[5];
+
+	return 0;
+}

[Severity: Low]
As reported by Ilpo Järvinen on RFC [1], this still reads into a raw u8 buf[6] array with magic offsets instead of a __packed struct with named members and a __be32 type_code field.

The #include <linux/compiler.h> at the top of the file is also unused (__packed comes from linux/compiler_attributes.h), and the commit message still wraps lines early at 51-60 columns instead of 72-75 columns.

drivers/platform/x86/macsmc-acpi.c line 251

Quoted 5 lines
drivers/platform/x86/macsmc-acpi.c new file +5 -0
+static void apple_smc_acpi_mmio_clear(struct apple_smc_acpi *smc)
+{
+	if (ioread8(smc->iomem_base + SMC_MMIO_KEY_STATUS))
+		return iowrite8(0, smc->iomem_base + SMC_MMIO_KEY_STATUS);
+}

[Severity: Low]
Returning the void result of iowrite8() from a void function violates ISO C and triggers sparse/pedantic warnings; dropping the return keyword fixes it.

Also, dev_dbg(smc->dev, "mmio/wait: timeout"); in apple_smc_acpi_mmio_wait() is missing a trailing newline.

drivers/platform/x86/macsmc-acpi.c line 265

Quoted 13 lines
drivers/platform/x86/macsmc-acpi.c new file +13 -0
+static int apple_smc_acpi_mmio_wait(struct apple_smc_acpi *smc)
+{
+	u8 status;
+	int ret;
+
+	if (smc->interrupt_enabled) {
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}

[Severity: Medium]
Can a delayed or spurious SMC_INT_CMD_DONE wake apple_smc_acpi_mmio_wait() before the in-flight MMIO command finishes?

apple_smc_acpi_try_enable_interrupts() polls the NTOK=1 write while the IRQ handler is already active and sets smc->interrupt_enabled = 1 without draining any resulting SMC_INT_CMD_DONE. Because apple_smc_acpi_mmio_wait() returns 0 immediately on wakeup without checking SMC_MMIO_STATUS_DONE, an early wakeup causes apple_smc_acpi_mmio_result() to read an unfinished command opcode (-EIO) or stale SMC_MMIO_KEY_DATA.

In addition, once a timeout clears smc->interrupt_enabled = 0, NTOK stays enabled in hardware, so the IRQ handler still calls complete() while the polled MMIO paths unconditionally call unlocked reinit_completion():

CPU0 (polled MMIO) CPU1 (<hardirq>)
apple_smc_acpi_mmio_read()
reinit_completion(&smc->cmd_done)

    cmd_done.done = 0;            apple_smc_acpi_irq()
                                    complete(&smc->cmd_done)
                                      cmd_done.done++;  <-- data race

Disabling NTOK (or gating complete() and reinit_completion() on interrupt_enabled) and verifying SMC_MMIO_STATUS_DONE after wakeup would avoid both issues.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 343

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	info->type_code = cpu_to_be32(ioread32(smc->iomem_base + SMC_MMIO_KEY_DATA));

[Severity: Low]
Does assigning cpu_to_be32() to host-order u32 info->type_code trigger a sparse endianness warning? Using ioread32be() reads the big-endian MMIO register directly into u32 and matches iowrite32be() above.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 385

Quoted 9 lines
drivers/platform/x86/macsmc-acpi.c new file +9 -0
+	if (smc->is_mmio) {
+		status = ioread8(smc->iomem_base + SMC_MMIO_KEY_INTERRUPT_STATUS);
+		if (!(status & SMC_MMIO_IRQ_PENDING) && status)
+			return IRQ_NONE;
+	} else {
+		status = inb(smc->port_base + SMC_IO_CMD_PORT);
+		if (!(status & SMC_IO_STATUS_IRQ_PENDING) && status)
+			return IRQ_NONE;
+	}

[Severity: Medium]
Why do both status checks include "&& status"?

When no SMC interrupt is pending and the status register reads 0, the condition evaluates to false and apple_smc_acpi_irq() does not return IRQ_NONE. On a spurious or shared interrupt, it falls through to read SMC_IO_INT_ID_PORT (risking re-dispatching a stale ID such as SMC_INT_SHUTDOWN or SMC_INT_CMD_DONE) and unconditionally returns IRQ_HANDLED even for unknown or zero int_id values. Conversely, if an event arrives without IRQ_PENDING while another status bit such as SMC_IO_STATUS_BUSY is set, this check drops it with IRQ_NONE.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 447

Quoted 3 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+	case ACPI_RESOURCE_TYPE_IRQ:
+		smc->irq = res->data.irq.interrupts[0];
+		return AE_OK;

[Severity: Medium]
Does storing the raw ACPI IRQ number from _CRS bypass Linux IRQ translation and ignore ExtendedIrq descriptors and IRQ flags?

acpi_create_platform_device() already translates both ACPI_RESOURCE_TYPE_IRQ and ACPI_RESOURCE_TYPE_EXTENDED_IRQ into IORESOURCE_IRQ resources with trigger and shareability flags. Passing the raw GSI to devm_request_irq() with hardcoded flags 0 in apple_smc_acpi_probe() can register on the wrong Linux IRQ when the GSI does not map 1:1, fail with -EBUSY on shared lines, or miss ExtendedIrq descriptors and disable SMC notifications. Using platform_get_irq_optional() handles translation and trigger setup.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 468

Quoted 2 lines
drivers/platform/x86/macsmc-acpi.c new file +2 -0
+	smc->iomem_base = devm_ioremap(smc->dev, smc->iomem_base_addr,
+					smc->iomem_base_size);

[Severity: Low]
Should this claim the MMIO window with devm_request_mem_region() (or devm_ioremap_resource()) before mapping it, as apple_smc_acpi_probe() does for the PIO ports with devm_request_region()? Without an IORESOURCE_BUSY reservation, conflicting drivers will not get -EBUSY, CONFIG_IO_STRICT_DEVMEM protection is bypassed, and /proc/iomem does not show macsmc-acpi owning the window.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 535

Quoted 5 lines
drivers/platform/x86/macsmc-acpi.c new file +5 -0
+	if (ret < 0)
+		return ret;
+
+	return size;
+}

[Severity: Low]
Should apple_smc_acpi_write() return 0 on success instead of size?

include/linux/mfd/macsmc.h documents apple_smc_write() as returning 0 on success (matching macsmc-rtkit), and APPLE_SMC_TYPE_OPS passes its return value directly through. Returning size leaks positive byte counts out of callbacks documented to return 0 or negative errno, such as macsmc_hwmon_write_fan() (hwmon_ops.write) and macsmc_battery_set_property().

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 602

Quoted 10 lines
drivers/platform/x86/macsmc-acpi.c new file +10 -0
+static ssize_t plimit_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct apple_smc *smc = dev_get_drvdata(dev);
+	struct apple_smc_acpi *smc_acpi = smc->be_cookie;
+
+	return sysfs_emit(buf, "%08x %02x\n",
+			inl(smc_acpi->port_base + SMC_IO_INT_PLIMIT),
+			inb(smc_acpi->port_base + SMC_IO_INT_PLIMIT_GPU));
+}
+static DEVICE_ATTR_RO(plimit);

[Severity: Low]
This adds a new userspace sysfs attribute that formats two values in one file (violating the sysfs one-value-per-file convention), has no Documentation/ABI/ entry, and is not mentioned in the commit message.

plimit_show() and apple_smc_acpi_resume() also reach directly into core struct apple_smc internals (smc->be_cookie and smc->mutex) instead of using core helpers.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 612

Quoted 3 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+static int apple_smc_acpi_probe(struct platform_device *pdev)
+{
+	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);

[Severity: Medium]
Can ACPI_COMPANION(&pdev->dev) return NULL here?

Because platform_bus_type allows driver_override, binding a non-ACPI platform device to macsmc-acpi via sysfs enters apple_smc_acpi_probe() with a NULL adev, causing a NULL pointer dereference oops when adev->handle is evaluated below.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 624

Quoted 2 lines
drivers/platform/x86/macsmc-acpi.c new file +2 -0
+	status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
+				apple_smc_acpi_walk_resources, smc);

[Severity: Medium]
Is adev->handle dereferenced without checking if adev is non-NULL? Checking if (!adev) return -ENODEV; or passing ACPI_HANDLE(&pdev->dev) avoids a NULL pointer dereference if a non-ACPI platform device is bound to this driver.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 630

Quoted 2 lines
drivers/platform/x86/macsmc-acpi.c new file +2 -0
+	if (!devm_request_region(smc->dev, smc->port_base, SMC_IO_NR_PORTS, "macsmc-acpi"))
+		return -EBUSY;

[Severity: Medium]
Can macsmc-acpi and applesmc race for this 32-byte I/O port region at boot?

Both drivers auto-load on Intel Macs (APP0001 ACPI modalias vs applesmc DMI whitelist) and request 0x300-0x31f. When both are built as modules, udev loads them concurrently, non-deterministically switching userspace between the legacy applesmc ABI and the macsmc MFD ABIs across boots; when both are built-in, applesmc always claims the region first.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 656

Quoted 3 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+	apple_smc_acpi_try_enable_interrupts(smc);
+
+	return apple_smc_probe(smc->dev, &apple_smc_acpi_be_ops, smc, &smc->event_handlers, true);

[Severity: Medium]
Does enabling NTOK=1 before apple_smc_probe() leave hardware notifications armed if apple_smc_probe() fails early?

apple_smc_probe() does not register devm_add_action_or_reset(..., apple_smc_disable_notifications, ...) until after devm_kzalloc() and the initial #KEY, first_key, and last_key reads. If any of those fail, devres unregisters apple_smc_acpi_irq() while NTOK remains 1 in SMC hardware.

Also, during normal teardown, apple_smc_acpi_notifications() writes NTOK=0 while smc->interrupt_enabled is still 1, which risks a 1000 ms timeout if clearing NTOK suppresses its own CMD_DONE interrupt.

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static int apple_smc_acpi_resume(struct device *dev)
+{
+	struct apple_smc *smc = dev_get_drvdata(dev);
+	struct apple_smc_acpi *smc_acpi = smc->be_cookie;

drivers/platform/x86/macsmc-acpi.c line 672

Quoted 10 lines
drivers/platform/x86/macsmc-acpi.c new file +10 -0
+
+	/* NTOK may be set to 0 on resume */
+	if (smc_acpi->interrupt_enabled) {
+		mutex_lock(&smc->mutex);
+		smc_acpi->interrupt_enabled = 0;
+		apple_smc_acpi_try_enable_interrupts(smc_acpi);
+		mutex_unlock(&smc->mutex);
+	}
+	return 0;
+}

[Severity: Medium]
Is checking smc_acpi->interrupt_enabled before acquiring smc->mutex a data race and TOCTOU?

There is no .suspend callback and IRQs are re-enabled before dpm_resume() runs, so a concurrent SMC transaction on a non-freezable workqueue can clear interrupt_enabled under smc->mutex while apple_smc_acpi_resume() reads it unlocked:

CPU0 (dpm_resume) CPU1 (kworker)
apple_smc_acpi_resume()
if (smc_acpi->interrupt_enabled)

                                  apple_smc_read()
                                    mutex_lock(&smc->mutex)
                                    apple_smc_acpi_mmio_wait()
                                      smc->interrupt_enabled = 0;
                                    mutex_unlock(&smc->mutex)

mutex_lock(&smc->mutex)
smc_acpi->interrupt_enabled = 0; apple_smc_acpi_try_enable_interrupts() <-- TOCTOU / data race

Acquiring smc->mutex before checking smc_acpi->interrupt_enabled closes the race.

[Severity: Medium]
Can apple_smc_acpi_resume() permanently skip restoring NTOK=1 after suspend/resume?

ACPI runtime GPEs are re-enabled in platform_resume_early() or platform_resume_finish() before dpm_resume() runs apple_smc_acpi_resume(). If a non-freezable ACPI notification (such as ACPI0003 triggering power_supply_changed() -> apple_smc_read*()) accesses the SMC while firmware has reset NTOK to 0, apple_smc_acpi_mmio_wait() stalls for 1000 ms and clears smc->interrupt_enabled = 0. When apple_smc_acpi_resume() then runs, it sees interrupt_enabled == 0 and skips apple_smc_acpi_try_enable_interrupts(), leaving SMC accesses stuck in polling mode and all asynchronous SMC notifications disabled across subsequent resumes. The same occurs after any single transient missed IRQ followed by suspend/resume.

[ ... ]

drivers/platform/x86/macsmc-acpi.c line 688

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+		.dev_groups = apple_smc_acpi_groups,

[Severity: Low]
Registering apple_smc_acpi_groups exposes the undocumented plimit sysfs attribute to userspace without a Documentation/ABI/ entry or commit message description; once merged, it becomes permanent kernel ABI.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

In reply to Atharva Tiwari
Quoted 7 lines · Atharva Tiwari

Add a backend that talks to the SMC on Intel Macs over ACPI. It supports the old I/O port interface, and also MMIO and interrupt-driven completion, which applesmc doesn't have. If an interrupt is missed it falls back to polling.

This is the base that lets the macsmc drivers replace applesmc.

...

drivers/platform/x86/macsmc-acpi.c line 13

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+#include <linux/acpi.h>
+#include <linux/bits.h>
 
+#include <linux/byteorder/generic.h>

Wrong, the asm/byteorder.h has to be used.

drivers/platform/x86/macsmc-acpi.c line 22

Quoted 11 lines
drivers/platform/x86/macsmc-acpi.c new file +9 -0
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/devm-helpers.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
 
+#include <linux/ioport.h>
 
+#include <linux/iopoll.h>

With this, io.h is not required.

Quoted 12 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+#include <linux/kfifo.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/spinlock.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+#include <linux/workqueue.h>

...

Quoted 1 line

+#define SMC_TIMEOUT_MS 1000

Why not (1 * MSEC_PER_SEC) here ?

...

drivers/platform/x86/macsmc-acpi.c line 74

Quoted 13 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+struct apple_smc_acpi {
+	struct device *dev;
+
+	u8 __iomem *iomem_base;
+	u32 iomem_base_addr, iomem_base_size;
+	bool is_mmio;
+
+	u16 port_base;
+	bool has_port;
+
+	int irq;
 
+	bool has_interrupt;

Why? Isn't non-positive value means no interrupt?

drivers/platform/x86/macsmc-acpi.c line 75

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool interrupt_enabled;

Why? Isn't IRQ core provides something for this?

drivers/platform/x86/macsmc-acpi.c line 87

Quoted 12 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+	struct completion cmd_done;
+
+	struct blocking_notifier_head event_handlers;
+	struct work_struct shutdown_work;
+	struct work_struct notifier_work;
+
+	DECLARE_KFIFO(event_fifo, u8, 64);
+	/* Protects event_fifo, shared between the IRQ handler and notifier_work */
+	spinlock_t event_lock;
+};
+
+

Why double blank line?

Quoted 23 lines
drivers/platform/x86/macsmc-acpi.c new file +23 -0
+static int apple_smc_acpi_port_wait(struct apple_smc_acpi *smc, u8 val, u8 mask)
+{
+	u8 status;
+	int ret;
+
+	if (smc->interrupt_enabled) {
+		reinit_completion(&smc->cmd_done);
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}
+
+	ret = read_poll_timeout(inb, status, (status & mask) == val,
+				USEC_PER_MSEC, SMC_TIMEOUT_MS * USEC_PER_MSEC,
+				false, smc->port_base + SMC_IO_CMD_PORT);
+	if (ret)
+		dev_dbg(smc->dev, "port/wait: timeout value:%u, mask:%u\n", val, mask);
+
+	return ret;
+}

...

drivers/platform/x86/macsmc-acpi.c line 147

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +8 -0
+static int apple_smc_acpi_port_write_key(struct apple_smc_acpi *smc, smc_key key)
+{
+	int i;
+
+	for (i = 3; i >= 0; i--) {
+		if (apple_smc_acpi_port_write_byte(smc, (key >> (i * 8)) & 0xff))
+			return -EIO;
+	}

Missing blank line.

Review

Quoted 1 line
(quoted diff) +1 -0
+	return 0;

This should use proper __be32 type instead.

drivers/platform/x86/macsmc-acpi.c line 156

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +7 -0
+}
+
+static int apple_smc_acpi_port_valid(struct apple_smc_acpi *smc)
+{
+	int ret;
 
+	ret = apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+	if (!ret)

Huh?! This is way non-standard. Don't do that.
This is a semantic mistake.

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +8 -0
+		return ret;
+
+	ret = apple_smc_acpi_port_write_cmd(smc, SMC_MSG_READ_KEY);
+	if (ret)
+		return ret;
+
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+}

...

drivers/platform/x86/macsmc-acpi.c line 679

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static const struct acpi_device_id apple_smc_acpi_ids[] = {
+	{"APP0001", 0},
+	{}
+};

Where have you been last half a year?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

Quoted 152 lines · Andy Shevchenko
Quoted 7 lines · Atharva Tiwari

Add a backend that talks to the SMC on Intel Macs over ACPI. It supports the old I/O port interface, and also MMIO and interrupt-driven completion, which applesmc doesn't have. If an interrupt is missed it falls back to polling.

This is the base that lets the macsmc drivers replace applesmc.

...

drivers/platform/x86/macsmc-acpi.c line 13

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +3 -0
+#include <linux/acpi.h>
+#include <linux/bits.h>
 
+#include <linux/byteorder/generic.h>

Wrong, the asm/byteorder.h has to be used.

drivers/platform/x86/macsmc-acpi.c line 22

Quoted 11 lines
drivers/platform/x86/macsmc-acpi.c new file +9 -0
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/devm-helpers.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
 
+#include <linux/ioport.h>
 
+#include <linux/iopoll.h>

With this, io.h is not required.

Quoted 12 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+#include <linux/kfifo.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/spinlock.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+#include <linux/workqueue.h>

...

Quoted 1 line

+#define SMC_TIMEOUT_MS 1000

Why not (1 * MSEC_PER_SEC) here ?

...

drivers/platform/x86/macsmc-acpi.c line 74

Quoted 13 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+struct apple_smc_acpi {
+	struct device *dev;
+
+	u8 __iomem *iomem_base;
+	u32 iomem_base_addr, iomem_base_size;
+	bool is_mmio;
+
+	u16 port_base;
+	bool has_port;
+
+	int irq;
 
+	bool has_interrupt;

Why? Isn't non-positive value means no interrupt?

drivers/platform/x86/macsmc-acpi.c line 75

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool interrupt_enabled;

Why? Isn't IRQ core provides something for this?

drivers/platform/x86/macsmc-acpi.c line 87

Quoted 12 lines
drivers/platform/x86/macsmc-acpi.c new file +12 -0
+	struct completion cmd_done;
+
+	struct blocking_notifier_head event_handlers;
+	struct work_struct shutdown_work;
+	struct work_struct notifier_work;
+
+	DECLARE_KFIFO(event_fifo, u8, 64);
+	/* Protects event_fifo, shared between the IRQ handler and notifier_work */
+	spinlock_t event_lock;
+};
+
+

Why double blank line?

Quoted 23 lines
drivers/platform/x86/macsmc-acpi.c new file +23 -0
+static int apple_smc_acpi_port_wait(struct apple_smc_acpi *smc, u8 val, u8 mask)
+{
+	u8 status;
+	int ret;
+
+	if (smc->interrupt_enabled) {
+		reinit_completion(&smc->cmd_done);
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}
+
+	ret = read_poll_timeout(inb, status, (status & mask) == val,
+				USEC_PER_MSEC, SMC_TIMEOUT_MS * USEC_PER_MSEC,
+				false, smc->port_base + SMC_IO_CMD_PORT);
+	if (ret)
+		dev_dbg(smc->dev, "port/wait: timeout value:%u, mask:%u\n", val, mask);
+
+	return ret;
+}

...

drivers/platform/x86/macsmc-acpi.c line 147

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +8 -0
+static int apple_smc_acpi_port_write_key(struct apple_smc_acpi *smc, smc_key key)
+{
+	int i;
+
+	for (i = 3; i >= 0; i--) {
+		if (apple_smc_acpi_port_write_byte(smc, (key >> (i * 8)) & 0xff))
+			return -EIO;
+	}

Missing blank line.

Review

Quoted 1 line
(quoted diff) +1 -0
+	return 0;

This should use proper __be32 type instead.

drivers/platform/x86/macsmc-acpi.c line 156

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +7 -0
+}
+
+static int apple_smc_acpi_port_valid(struct apple_smc_acpi *smc)
+{
+	int ret;
 
+	ret = apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+	if (!ret)

Huh?! This is way non-standard. Don't do that.
This is a semantic mistake.

Quoted 8 lines
drivers/platform/x86/macsmc-acpi.c new file +8 -0
+		return ret;
+
+	ret = apple_smc_acpi_port_write_cmd(smc, SMC_MSG_READ_KEY);
+	if (ret)
+		return ret;
+
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+}

...

Quoted 6 lines

drivers/platform/x86/macsmc-acpi.c line 679

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static const struct acpi_device_id apple_smc_acpi_ids[] = {
+	{"APP0001", 0},
+	{}
+};

Where have you been last half a year?

He is a new kernel dev. Not your fault for assuming he is a seasoned one. Can the maintainers be friendly and point out code errors rather than the RTFM thing for LKML as well? Or atleast point out some examples rather than just ranting.

#9 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

Quoted 2 lines · Aditya Garg
Quoted 1 line · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:42AM -0500, Atharva Tiwari wrote:

...

Quoted 8 lines
Quoted 6 lines

drivers/platform/x86/macsmc-acpi.c line 679

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static const struct acpi_device_id apple_smc_acpi_ids[] = {
+	{"APP0001", 0},
+	{}
+};

Where have you been last half a year?

He is a new kernel dev. Not your fault for assuming he is a seasoned one.

Oh, sorry for that. Do you have any internal mailing list to discuss patches before sending, or is it just this mailing list for the fully open discussion?

Quoted 3 lines

Can the maintainers be friendly and point out code errors rather than the RTFM thing for LKML as well? Or atleast point out some examples rather than just ranting.

Sure. The C99 initialisers is a requirement nowadays for the ID tables. As well as common style. I highly recommend to check with Git history for each of the subsystem and check the last contributions. AI actually may be helpful with collecting and summarising such an info.

The above should be

static const struct acpi_device_id apple_smc_acpi_ids[] = {
	{ .id = "APP0001" },
	{ }
};

P.S. Sorry for my tone, I'm so grumpy on the later AI slop here and there, especially when people do not read anything.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

Quoted 13 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 1 line · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:42AM -0500, Atharva Tiwari wrote:

...

Quoted 7 lines
Quoted 6 lines

drivers/platform/x86/macsmc-acpi.c line 679

Quoted 4 lines
drivers/platform/x86/macsmc-acpi.c new file +4 -0
+static const struct acpi_device_id apple_smc_acpi_ids[] = {
+	{"APP0001", 0},
+	{}
+};

Where have you been last half a year?

Quoted 4 lines
Quoted 1 line

He is a new kernel dev. Not your fault for assuming he is a seasoned one.

Oh, sorry for that. Do you have any internal mailing list to discuss patches before sending, or is it just this mailing list for the fully open discussion?

There has been an internal discussion. Infact an RFC was also sent for this series.

Quoted 10 lines
Quoted 4 lines

Can the maintainers be friendly and point out code errors rather than the RTFM thing for LKML as well? Or atleast point out some examples rather than just ranting.

Sure. The C99 initialisers is a requirement nowadays for the ID tables. As well as common style. I highly recommend to check with Git history for each of the subsystem and check the last contributions. AI actually may be helpful with collecting and summarising such an info.

Quoted 6 lines

The above should be

static const struct acpi_device_id apple_smc_acpi_ids[] = {
	{ .id = "APP0001" },
	{ }
};

Thank you very much for this. I appreciate it.

Quoted 3 lines

P.S. Sorry for my tone, I'm so grumpy on the later AI slop here and there, especially when people do not read anything.

I understand your feeling here. I myself am frustrated because of a pile load of slop PRs on my projects xD.

#11 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

In reply to Andy Shevchenko
Quoted 7 lines

drivers/platform/x86/macsmc-acpi.c line 74

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool has_interrupt;

Why? Isn't non-positive value means no interrupt?

drivers/platform/x86/macsmc-acpi.c line 75

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool interrupt_enabled;

Why? Isn't IRQ core provides something for this?

Sorry if i was not clear, but basically

has_interrupt means that there is a interrupt line present in ACPI and the driver should write to NTOK key.

interrupt_enabled means that interrupts are enabled (NTOK written successfully) and interrupts can be used for reading and writing, thats why when a interrupt is missed, the driver sets interrupt_enabled to 0:

+ if (smc->interrupt_enabled) {
+ if (wait_for_completion_timeout(&smc->cmd_done,
+ msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+ return 0;
+
+ smc->interrupt_enabled = 0;
+ dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+ }

interrupt_enabled is not used anywhere else except in the resume code.

I will remove the has_interrupt variable, because you said non-positive irq means no interrupt.

#12 lore raw

Re: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs

On Fri, Dec 04, 2026 at 02:57:44PM -0500, Atharva Tiwari wrote:

...

Quoted 8 lines
Quoted 7 lines

drivers/platform/x86/macsmc-acpi.c line 74

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool has_interrupt;

Why? Isn't non-positive value means no interrupt?

drivers/platform/x86/macsmc-acpi.c line 75

Quoted 1 line
drivers/platform/x86/macsmc-acpi.c new file +1 -0
+	bool interrupt_enabled;

Why? Isn't IRQ core provides something for this?

Quoted 8 lines

Sorry if i was not clear, but basically

has_interrupt means that there is a interrupt line present in ACPI and the driver should write to NTOK key.

interrupt_enabled means that interrupts are enabled (NTOK written successfully) and interrupts can be used for reading and writing, thats why when a interrupt is missed, the driver sets interrupt_enabled to 0:

Okay, so this is the state of the communication with the remote side.

Quoted 9 lines
drivers/platform/x86/macsmc-acpi.c new file +8 -0
+	if (smc->interrupt_enabled) {
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}
Quoted 4 lines

interrupt_enabled is not used anywhere else except in the resume code.

I will remove the has_interrupt variable, because you said non-positive irq means no interrupt.

This is usual interpretation of a value in IRQ (that's why in many places in the code it's signed; where it's unsigned 0 means NOIRQ).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

PATCH 3/14 watchdog: add driver for apple smc

In reply to Atharva Tiwari

Add a watchdog driver for the SMC on Intel Macs. It can reset the machine if the system stops responding. It works on both newer and older SMC versions.

---
 drivers/mfd/macsmc-core.c     |  11 +++
 drivers/watchdog/Kconfig      |  11 +++
 drivers/watchdog/Makefile     |   1 +
 drivers/watchdog/macsmc_wdt.c | 158 ++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+)
 create mode 100644 drivers/watchdog/macsmc_wdt.c
drivers/mfd/macsmc-core.c +11 -0
index b8905c834..1c1469828 100644
@@ -33,6 +33,10 @@ static const struct mfd_cell apple_smc_acpi_rtc[] = {
 	MFD_CELL_NAME("macsmc-rtc"),
 };
 
+static const struct mfd_cell apple_smc_acpi_wdt[] = {
+	MFD_CELL_NAME("macsmc-wdt"),
+};
+
 int apple_smc_read(struct apple_smc *smc, smc_key key, void *buf, size_t size)
 {
 	guard(mutex)(&smc->mutex);
@@ -147,6 +151,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(NATJ))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_wdt,
+				ARRAY_SIZE(apple_smc_acpi_wdt), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
+
 	} else
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
 			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
drivers/watchdog/Kconfig +11 -0
index f7d0a0c2c..d80949bff 100644
@@ -1517,6 +1517,17 @@ config KEMPLD_WDT
 	  This driver can also be built as a module. If so, the module will be
 	  called kempld_wdt.
 
+config MACSMC_WDT
+	tristate "Apple SMC Watchdog"
+	depends on X86 && MACSMC_ACPI
+	select WATCHDOG_CORE
+	help
+	  This driver adds support for the watchdog in Intel Macs using
+	  the Apple SMC.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called macsmc_wdt.
+
 config SC1200_WDT
 	tristate "National Semiconductor PC87307/PC97307 (ala SC1200) Watchdog"
 	depends on (X86 || COMPILE_TEST) && HAS_IOPORT
drivers/watchdog/Makefile +1 -0
index 9ac406677..275e6aad8 100644
@@ -134,6 +134,7 @@ obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
 obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
+obj-$(CONFIG_MACSMC_WDT) += macsmc_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/macsmc_wdt.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.55.0
#14 lore raw

Re: [PATCH 03/14] watchdog: add driver for apple smc

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] watchdog: macsmc_wdt: missing suspend/resume PM callbacks - [Medium] mfd: macsmc: apple_smc_probe()…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] watchdog: macsmc_wdt: missing suspend/resume PM callbacks
- [Medium] mfd: macsmc: apple_smc_probe() checks only NATJ for macsmc-wdt
- [Medium] watchdog: macsmc_wdt: unsynchronized NATJ/NATi writes can force reset
- [Medium] watchdog: macsmc_wdt: apple_smc_key_exists() causes 1s PIO timeout stalls
- [Low] watchdog: macsmc_wdt uses obsolete __MODULE_STRING() and omits direct headers
--

commit dd87f5854d512b3a6e66e9d3cd77fe418fb3e73a
Author: Atharva Tiwari <[email protected]>

watchdog: add driver for apple smc

This commit adds a watchdog driver for the Apple SMC on Intel Macs, supporting both newer OSWD and legacy NATJ/NATi SMC keys, and registers the platform device from macsmc-core.

Quoted 4 lines
drivers/mfd/macsmc-core.c +0 -0
index b8905c834c448..1c14698285456 100644

[ ... ]

drivers/mfd/macsmc-core.c int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, · line 161

Quoted 12 lines
drivers/mfd/macsmc-core.c +7 -0
@@ -147,6 +151,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(NATJ))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_wdt,
+				ARRAY_SIZE(apple_smc_acpi_wdt), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
+
 	} else

[Severity: Medium]
Should this check also accept SMC_KEY(OSWD) and require SMC_KEY(NATi) alongside SMC_KEY(NATJ)?

On newer Intel Macs that expose OSWD without the legacy NATJ key, apple_smc_probe() never registers macsmc-wdt. Conversely, on an SMC with NATJ but without OSWD or NATi, macsmc_wdt_probe() falls back to has_oswd = false and registers a watchdog whose operations fail with -EIO.

Checking for OSWD or both NATJ and NATi in apple_smc_probe() and macsmc_wdt_probe() avoids both cases.

[ ... ]

Quoted 6 lines
drivers/watchdog/macsmc_wdt.c new file +0 -0
new file mode 100644
index 0000000000000..e2fc7f4e3e4b7
@@ -0,0 +1,158 @@

[ ... ]

Quoted 6 lines
drivers/watchdog/macsmc_wdt.c new file +6 -0
+#include <linux/mfd/macsmc.h>
+#include <linux/platform_device.h>
+#include <linux/watchdog.h>
+
+#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
+

drivers/watchdog/macsmc_wdt.c line 23

Quoted 10 lines
drivers/watchdog/macsmc_wdt.c new file +10 -0
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static int timeout = WATCHDOG_TIMEOUT; /* in secounds */
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
+		 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");

[Severity: Low]
Can __MODULE_STRING() be replaced with __stringify() here? It is marked obsolete and scheduled for removal in include/linux/module.h, and no other callers remain in the tree.

The file also omits direct includes of linux/module.h and linux/stringify.h despite using module macros, and the nowayout MODULE_PARM_DESC() has an unmatched closing parenthesis before the end of the string.

[ ... ]

Quoted 4 lines
drivers/watchdog/macsmc_wdt.c new file +4 -0
+		if (timeout == 0) {
+			ret = apple_smc_write_u8(drv->smc, SMC_KEY(NATJ), 0);
+			if (ret < 0)
+				return ret;

drivers/watchdog/macsmc_wdt.c line 57

Quoted 10 lines
drivers/watchdog/macsmc_wdt.c new file +10 -0
+		}
+		ret = apple_smc_write_u16(drv->smc, SMC_KEY(NATi), (u16)(timeout << 8));
+		if (ret < 0)
+			return ret;
+
+		if (timeout != 0) {
+			ret = apple_smc_write_u8(drv->smc, SMC_KEY(NATJ), 2);
+			if (ret < 0)
+				return ret;
+		}

[Severity: Medium]
Can concurrent stop and start/set_timeout calls interleave these NATJ and NATi writes into a forced reset?

macsmc_wdt_probe() enables watchdog_stop_on_reboot(), which calls macsmc_wdt_stop() without wd_data->lock while userspace can still issue watchdog ioctls. Because apple_smc_write() only holds smc->mutex per key write, the two-key sequence can interleave:

CPU0 (ioctl) CPU1 (reboot notifier)
macsmc_wdt_set_timeout() watchdog_reboot_notifier()
macsmc_wdt_write_timeout(t) macsmc_wdt_stop()

    write(NATi, t << 8)               macsmc_wdt_write_timeout(0)
                                        write(NATJ, 0)
                                        write(NATi, 0)
    write(NATJ, 2)  <-- NATi == 0, NATJ == 2: forced reset

Leaving NATi == 0 with NATJ == 2 triggers an immediate forced restart on legacy !has_oswd Macs. Serializing macsmc_wdt_write_timeout() and macsmc_wdt_ping() with a mutex in struct macsmc_wdt prevents this race.

[ ... ]

drivers/watchdog/macsmc_wdt.c line 130

Quoted 5 lines
drivers/watchdog/macsmc_wdt.c new file +5 -0
+	drv->smc = dev_get_drvdata(pdev->dev.parent);
+	if (apple_smc_key_exists(drv->smc, SMC_KEY(OSWD)))
+		drv->has_oswd = true;
+	else
+		drv->has_oswd = false;

[Severity: Medium]
Does probing a missing key via apple_smc_key_exists() stall for 1 second on PIO-based Intel Macs?

On PIO Macs (!smc->is_mmio), apple_smc_get_key_info() dispatches to apple_smc_acpi_port_read(), which has no error-status check and only fails when apple_smc_acpi_port_wait() times out after SMC_TIMEOUT_MS (1000 ms).

This deterministically stalls probe for 1 second while holding smc->mutex on every older PIO Mac without OSWD (and likewise for the NATJ check in apple_smc_probe() on PIO Macs without NATJ), blocking concurrent SMC accesses from sibling MFD drivers.

[ ... ]

drivers/watchdog/macsmc_wdt.c line 152

Quoted 6 lines
drivers/watchdog/macsmc_wdt.c new file +6 -0
+static struct platform_driver macsmc_wdt_driver = {
+	.probe = macsmc_wdt_probe,
+	.driver = {
+		.name = "macsmc-wdt",
+	},
+};

[Severity: High]
Does macsmc_wdt_driver need suspend and resume PM callbacks to stop an active watchdog across system sleep?

The watchdog core does not stop hardware watchdogs in watchdog_dev_suspend(), so an armed SMC watchdog (at most 255 seconds) will expire while the host CPU is suspended and force a hard reset.

Conversely, if the SMC clears its watchdog keys across sleep, the lack of a resume callback leaves the hardware watchdog silently disabled after resume while WDOG_ACTIVE remains set.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#15 lore raw

PATCH 4/14 iio/light: add new driver for macsmc

In reply to Atharva Tiwari

Add a driver for the ambient light sensors, exposed through the standard IIO interface with left and right channels.

This replaces the light sensor support in applesmc.

---
 drivers/iio/light/Kconfig        |  11 ++
 drivers/iio/light/Makefile       |   1 +
 drivers/iio/light/macsmc-light.c | 209 +++++++++++++++++++++++++++++++
 drivers/mfd/macsmc-core.c        |  11 ++
 4 files changed, 232 insertions(+)
 create mode 100644 drivers/iio/light/macsmc-light.c
drivers/iio/light/Kconfig +11 -0
index 462fff1dd..f84707edc 100644
@@ -457,6 +457,17 @@ config LV0104CS
 	  To compile this driver as a module, choose M here:
 	  the module will be called lv0104cs.
 
+config MACSMC_LIGHT
+	tristate "Apple SMC Light Sensor"
+	depends on MACSMC_ACPI
+	help
+	  Say Y here if you have a Intel-based Mac. and you
+	  want support for light sensors.
+
+	  To compile this driver as a module, choose M here:
+	  the module will be called macsmc_light
+
+
 config MAX44000
 	tristate "MAX44000 Ambient and Infrared Proximity Sensor"
 	depends on I2C
drivers/iio/light/Makefile +1 -0
index 6672ed01a..2ef4c054d 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_LTR390)		+= ltr390.o
 obj-$(CONFIG_LTR501)		+= ltr501.o
 obj-$(CONFIG_LTRF216A)		+= ltrf216a.o
 obj-$(CONFIG_LV0104CS)		+= lv0104cs.o
+obj-$(CONFIG_MACSMC_LIGHT)	+= macsmc-light.o
 obj-$(CONFIG_MAX44000)		+= max44000.o
 obj-$(CONFIG_MAX44009)		+= max44009.o
 obj-$(CONFIG_NOA1305)		+= noa1305.o
drivers/iio/light/macsmc-light.c new file +209 -0

209 lines not on this page · raw message

drivers/mfd/macsmc-core.c +11 -0
index 1c1469828..6014df4fe 100644
@@ -29,6 +29,10 @@ static const struct mfd_cell apple_smc_acpi_common[] = {
 	MFD_CELL_NAME("macsmc-power"),
 };
 
+static const struct mfd_cell apple_smc_acpi_light[] = {
+	MFD_CELL_NAME("macsmc-light"),
+};
+
 static const struct mfd_cell apple_smc_acpi_rtc[] = {
 	MFD_CELL_NAME("macsmc-rtc"),
 };
@@ -158,6 +162,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(ALV0))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_light,
+				ARRAY_SIZE(apple_smc_acpi_light), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
+
 	} else
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
 			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH 04/14] iio/light: add new driver for macsmc

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] iio: light: notifier registered before devm_iio_device_register() causes UAF - [Medium] iio…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] iio: light: notifier registered before devm_iio_device_register() causes UAF
- [Medium] iio: light: unconditional RIGHT_LIGHT channel and missing ALV1 validation
- [Medium] iio: light: macsmc-light exposes unitless raw SMC counts as IIO_LIGHT
- [Low] iio: light: Kconfig module name mismatch and MACSMC_ACPI dependency
- [Low] mfd: macsmc: literal -1 passed to devm_mfd_add_devices() for light cell
--

commit e9ed4cb9e8bdd5a53203bb98eb77007e4876b53e
Author: Atharva Tiwari <[email protected]>

iio/light: add new driver for macsmc

This commit adds an IIO ambient light sensor driver for Apple SMC (macsmc-light) exposing left and right channels, and registers its MFD cell from macsmc-core when the ALV0 key is present.

Quoted 7 lines
drivers/iio/light/Kconfig +0 -0
index 462fff1ddef20..f84707edc6772 100644
@@ -457,6 +457,17 @@ config LV0104CS
 	  To compile this driver as a module, choose M here:
 	  the module will be called lv0104cs.

drivers/iio/light/Kconfig config LV0104CS · line 468

Quoted 10 lines
drivers/iio/light/Kconfig +9 -0
 
+config MACSMC_LIGHT
+	tristate "Apple SMC Light Sensor"
+	depends on MACSMC_ACPI
+	help
+	  Say Y here if you have a Intel-based Mac. and you
+	  want support for light sensors.
+
+	  To compile this driver as a module, choose M here:
+	  the module will be called macsmc_light

[Severity: Low]
The Makefile builds macsmc-light.o, so the help text should say macsmc-light (with a hyphen and trailing period) rather than macsmc_light.

Also, macsmc-light only uses architecture-independent interfaces from MFD_MACSMC_CORE. Should this depend on MFD_MACSMC_CORE (or MACSMC_ACPI || (MFD_MACSMC_CORE && COMPILE_TEST)) like the sibling macsmc drivers so it can be compile-tested on non-x86 builds?

Quoted 5 lines
drivers/iio/light/macsmc-light.c new file +0 -0
new file mode 100644
index 0000000000000..12d88035ad746

[ ... ]

Quoted 8 lines
drivers/iio/light/macsmc-light.c new file +8 -0
+static const struct iio_chan_spec macsmc_light_channels[] = {
+	{
+		.type = IIO_LIGHT,
+		.channel = LEFT_LIGHT,
+		.indexed = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.event_spec = macsmc_light_events,
+		.num_event_specs = ARRAY_SIZE(macsmc_light_events),

drivers/iio/light/macsmc-light.c line 142

Quoted 10 lines
drivers/iio/light/macsmc-light.c new file +10 -0
+	},
+	{
+		.type = IIO_LIGHT,
+		.channel = RIGHT_LIGHT,
+		.indexed = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.event_spec = macsmc_light_events,
+		.num_event_specs = ARRAY_SIZE(macsmc_light_events),
+	},
+};

[Severity: Medium]
Should these channels use IIO_INTENSITY instead of IIO_LIGHT?

Per Documentation/ABI/testing/sysfs-bus-iio, IIO_LIGHT (in_illuminanceY_raw) must report illuminance in lux after scale and offset. macsmc_light_read_raw() instead returns uncalibrated, unit-less raw SMC counts with no scale attribute.

Exposing unit-less counts under in_illuminanceY_raw locks in wrong units for userspace. Without a lux conversion or scale factor, IIO_INTENSITY (in_intensityY_raw) is the documented type for unit-less light readings.

[ ... ]

drivers/iio/light/macsmc-light.c line 178

Quoted 9 lines
drivers/iio/light/macsmc-light.c new file +9 -0
+	ret = apple_smc_get_key_info(drv->smc, SMC_KEY(ALV0), &info);
+	if (ret < 0)
+		return -ENODEV;
+
+	if (info.size < 3 || info.size > 10)
+		return -ENODEV;
+
+	drv->size = info.size;
+	drv->single_sensor = info.size == 10;

[Severity: Medium]
Does macsmc_light_probe() need to check ALV1 separately and register only one channel when a single sensor is present?

On single-sensor Macs (10-byte ALV0), registering both channels exposes a nonexistent right channel that always returns 0 and never emits events.

When ALV0 is 3 to 9 bytes on hardware without a matching-length ALV1, reading RIGHT_LIGHT with drv->size fails or reads past ALV1 while macsmc_light_event() still pushes RIGHT_LIGHT events. Querying ALV1 in probe and sizing indio_dev->num_channels accordingly would avoid both.

Quoted 7 lines
drivers/iio/light/macsmc-light.c new file +7 -0
+	drv->indio_dev = indio_dev;
+	drv->nb.notifier_call = macsmc_light_event;
+	ret = blocking_notifier_chain_register(drv->smc->event_handlers, &drv->nb);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, macsmc_light_unregister_notifier, drv);

drivers/iio/light/macsmc-light.c line 195

Quoted 10 lines
drivers/iio/light/macsmc-light.c new file +10 -0
+	if (ret)
+		return ret;
+
+	indio_dev->name = "macsmc-light";
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &macsmc_light_info;
+	indio_dev->channels = macsmc_light_channels;
+	indio_dev->num_channels = ARRAY_SIZE(macsmc_light_channels);
+
+	return devm_iio_device_register(dev, indio_dev);

[Severity: Medium]
Can registering the SMC notifier before devm_iio_device_register() cause a use-after-free on probe failure and inverted teardown on unbind?

apple_smc_acpi_probe() enables SMC interrupts before adding MFD children, so SMC_INT_ALS_CHANGE can invoke macsmc_light_event() while devm_iio_device_register() is still running or unwinding:

CPU0 CPU1
macsmc_light_probe()
blocking_notifier_chain_register()
devm_iio_device_register()

    iio_device_register_eventset()
      event_interface = ev_int
    ... (cdev_device_add() fails)
    iio_device_unregister_eventset()
                                    apple_smc_acpi_notifier_work()
                                      macsmc_light_event()
                                        iio_push_event()
                                          ev_int = ...->event_interface
      kfree(ev_int)
      event_interface = NULL
                                          iio_event_enabled(ev_int)
                                            <-- use-after-free

On normal unbind, devres LIFO order also runs iio_device_unregister() before macsmc_light_unregister_notifier(), leaving the notifier pushing events after the IIO device is torn down. Registering the notifier after devm_iio_device_register() succeeds fixes both.

Quoted 4 lines
drivers/mfd/macsmc-core.c +0 -0
index 1c14698285456..6014df4fe1c0a 100644

[ ... ]

drivers/mfd/macsmc-core.c int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, · line 170

Quoted 10 lines
drivers/mfd/macsmc-core.c +6 -0
@@ -158,6 +162,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(ALV0))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_light,
+				ARRAY_SIZE(apple_smc_acpi_light), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}

[Severity: Low]
This isn't a bug, but devm_mfd_add_devices() here passes a literal -1 instead of PLATFORM_DEVID_AUTO (or PLATFORM_DEVID_NONE if a fixed single-instance name is required).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#17 lore raw

PATCH 5/14 iio/accel: add driver for apple SMS

In reply to Atharva Tiwari

Add a driver for the Sudden Motion Sensor accelerometer, exposed through IIO. It reports the three axes and also drop, orientation and shock events.

This replaces the motion sensor support in applesmc.

---
 drivers/iio/accel/Kconfig        |  10 ++
 drivers/iio/accel/Makefile       |   1 +
 drivers/iio/accel/macsmc-accel.c | 260 +++++++++++++++++++++++++++++++
 drivers/mfd/macsmc-core.c        |  10 ++
 4 files changed, 281 insertions(+)
 create mode 100644 drivers/iio/accel/macsmc-accel.c
drivers/iio/accel/Kconfig +10 -0
index 141ac9842..50bc127a4 100644
@@ -532,6 +532,16 @@ config KXCJK1013
 	  To compile this driver as a module, choose M here: the module will
 	  be called kxcjk-1013.
 
+config MACSMC_ACCEL
+	tristate "Apple SMC Motion Sensor Driver"
+	depends on MACSMC_ACPI
+	help
+	  Say yes here if you want support for Apple Motion Sensors, that uses
+	  the System Management Controller.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called macsmc-accel.
+
 config MC3230
 	tristate "mCube MC3230 Digital Accelerometer Driver"
 	depends on I2C
drivers/iio/accel/Makefile +1 -0
index fa440a859..52fe661de 100644
@@ -54,6 +54,7 @@ obj-$(CONFIG_KXSD9)	+= kxsd9.o
 obj-$(CONFIG_KXSD9_SPI)	+= kxsd9-spi.o
 obj-$(CONFIG_KXSD9_I2C)	+= kxsd9-i2c.o
 obj-$(CONFIG_MC3230)	+= mc3230.o
+obj-$(CONFIG_MACSMC_ACCEL)	+= macsmc-accel.o
 
 obj-$(CONFIG_MMA7455)		+= mma7455_core.o
 obj-$(CONFIG_MMA7455_I2C)	+= mma7455_i2c.o
drivers/iio/accel/macsmc-accel.c new file +260 -0

260 lines not on this page · raw message

drivers/mfd/macsmc-core.c +10 -0
index 6014df4fe..6567d2f1f 100644
@@ -24,6 +24,10 @@ static const struct mfd_cell apple_smc_rtkit_devs[] = {
  * There isn't device tree on acpi backend, so we need to use
  * apple_smc_key_exists to register mfd devs
  */
+static const struct mfd_cell apple_smc_acpi_accel[] = {
+	MFD_CELL_NAME("macsmc-accel"),
+};
+
 static const struct mfd_cell apple_smc_acpi_common[] = {
 	MFD_CELL_NAME("macsmc-hwmon"),
 	MFD_CELL_NAME("macsmc-power"),
@@ -169,6 +173,12 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(MOCN))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_accel,
+				ARRAY_SIZE(apple_smc_acpi_accel), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
 	} else
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
 			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
Signature · 1 line
2.55.0
#18 lore raw

Re: [PATCH 05/14] iio/accel: add driver for apple SMS

Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Medium] iio: accel: macsmc-accel: missing event config callbacks in macsmc_accel_info - [Medium] iio…

Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [Medium] iio: accel: macsmc-accel: missing event config callbacks in macsmc_accel_info
- [Medium] iio: accel: macsmc_accel_probe() enables notifier before IIO registration
- [Medium] iio: accel: macsmc_accel_dev_init() ignores SMC errors and misorders poll
- [Medium] iio: accel: macsmc-accel: undocumented orientation change event ABI
- [Medium] mfd: macsmc: probing absent MOCN via apple_smc_key_exists() stalls PIO boot
- [Low] iio: accel: macsmc-accel: s16 passed to be16_to_cpu() in macsmc_accel_read()
- [Low] iio: accel: macsmc-accel: Makefile order, MFD cell ID, and subject conventions
--

commit 72edf11305348abc7eba7049f44352095cfc9e6d
Author: Atharva Tiwari <[email protected]>

iio/accel: add driver for apple SMS

This commit adds an IIO accelerometer driver for the Apple SMC Sudden Motion Sensor (reporting X/Y/Z axes and drop, orientation, and shock events) and registers it from the ACPI SMC MFD core when the MOCN key exists.

drivers/iio/accel/Makefile obj-$(CONFIG_KXSD9) += kxsd9.o · line 57

Quoted 9 lines
drivers/iio/accel/Makefile +1 -0
index fa440a8592839..52fe661dec666 100644
@@ -54,6 +54,7 @@ obj-$(CONFIG_KXSD9)	+= kxsd9.o
 obj-$(CONFIG_KXSD9_SPI)	+= kxsd9-spi.o
 obj-$(CONFIG_KXSD9_I2C)	+= kxsd9-i2c.o
 obj-$(CONFIG_MC3230)	+= mc3230.o
+obj-$(CONFIG_MACSMC_ACCEL)	+= macsmc-accel.o

[Severity: Low]
This isn't a bug, but CONFIG_MACSMC_ACCEL is placed after CONFIG_MC3230, breaking the alphabetical ordering in this Makefile. Similarly, the new devm_mfd_add_devices() call in drivers/mfd/macsmc-core.c passes -1 instead of PLATFORM_DEVID_AUTO (or PLATFORM_DEVID_NONE), and the commit subject uses "iio/accel:" instead of the standard "iio: accel:" prefix.

Quoted 5 lines
drivers/iio/accel/macsmc-accel.c new file +0 -0
new file mode 100644
index 0000000000000..79891fd13c56a

[ ... ]

drivers/iio/accel/macsmc-accel.c line 45

Quoted 11 lines
drivers/iio/accel/macsmc-accel.c new file +11 -0
+static int macsmc_accel_read(struct macsmc_accel *drv, smc_key key, s16 *p)
+{
+	int ret;
+
+	ret = apple_smc_read_s16(drv->smc, key, p);
+	if (ret < 0)
+		return ret;
+
+	*p = (s16)be16_to_cpu(*p);
+	return 0;
+}

[Severity: Low]
Does passing host-endian s16 *p directly to be16_to_cpu() trigger a sparse endianness warning? __be16_to_cpu() expects a restricted __be16 value, so reading into a local __be16 (e.g., via apple_smc_read()) and converting with be16_to_cpu() into *p keeps the endianness types sparse-clean.

Quoted 13 lines
drivers/iio/accel/macsmc-accel.c new file +13 -0
+static int macsmc_accel_dev_init(struct macsmc_accel *drv)
+{
+	int ret, total;
+	u16 buf, write_value;
+
+	ret = apple_smc_read_u16(drv->smc, SMC_KEY(MOCN), &buf);
+	if (ret < 0)
+		return ret;
+
+	/* 0xE0 enables the interrupts and the actual motion sensors */
+	write_value = buf | 0xE0;
+
+	for (total = 0; total < 100; total++) {

drivers/iio/accel/macsmc-accel.c line 69

Quoted 10 lines
drivers/iio/accel/macsmc-accel.c new file +10 -0
+		ret = apple_smc_read_u16(drv->smc, SMC_KEY(MOCN), &buf);
+		if (!ret && (buf & 0xE0) == 0xE0)
+			return 0;
+
+		apple_smc_write_u16(drv->smc, SMC_KEY(MOCN), write_value);
+		msleep(50);
+	}
+
+	return -ENODEV;
+}

[Severity: Medium]
Can this retry loop stall probe or hibernation restore for minutes while holding smc->mutex?

Both read errors from apple_smc_read_u16() and the return value of apple_smc_write_u16() are ignored across 100 iterations. Each SMC transaction holds smc->mutex and can wait up to SMC_TIMEOUT_MS (1000 ms), turning persistent timeouts into a multi-minute stall for sibling drivers.

The loop also re-reads MOCN on iteration 0 without checking the initial read for (buf & 0xE0) == 0xE0, and returns -ENODEV after the 100th write and sleep without checking whether that final write took effect. Checking the initial read, propagating I/O errors (or bounding wall-clock time), and doing write, sleep, then read in the loop avoids these issues.

[ ... ]

drivers/iio/accel/macsmc-accel.c line 118

Quoted 4 lines
drivers/iio/accel/macsmc-accel.c new file +4 -0
+	case SMC_INT_SMS_ORIENTATION:
+		code = IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
+					  IIO_EV_TYPE_CHANGE, IIO_EV_DIR_NONE);
+		break;

[Severity: Medium]
As reported by Jonathan Cameron on RFC [1], what acceleration condition triggers SMC_INT_SMS_ORIENTATION? No other IIO_ACCEL driver uses IIO_EV_TYPE_CHANGE, and in_accel_x|y|z_change_en is undocumented in Documentation/ABI/testing/sysfs-bus-iio, so userspace cannot interpret it.

[ ... ]

Quoted 20 lines
drivers/iio/accel/macsmc-accel.c new file +20 -0
+static const struct iio_event_spec macsmc_accel_drop_events[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
+static const struct iio_event_spec macsmc_accel_any_axis_events[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_RISING,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	},
+	{
+		.type = IIO_EV_TYPE_CHANGE,
+		.dir = IIO_EV_DIR_NONE,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+	},
+};

[ ... ]

drivers/iio/accel/macsmc-accel.c line 197

Quoted 3 lines
drivers/iio/accel/macsmc-accel.c new file +3 -0
+static const struct iio_info macsmc_accel_info = {
+	.read_raw = macsmc_accel_read_raw
+};

[Severity: Medium]
Will all three exported *_en event sysfs attributes unconditionally fail with -EINVAL?

Setting BIT(IIO_EV_INFO_ENABLE) registers the *_en attributes via iio_ev_state_show() and iio_ev_state_store(), which both return -EINVAL when read_event_config and write_event_config are NULL in macsmc_accel_info. This either needs those callbacks implemented or BIT(IIO_EV_INFO_ENABLE) omitted if the events are always enabled.

[ ... ]

Quoted 14 lines
drivers/iio/accel/macsmc-accel.c new file +14 -0
+	ret = macsmc_accel_dev_init(drv);
+	if (ret)
+		return ret;
+
+	drv->indio_dev = indio_dev;
+	drv->nb.notifier_call = macsmc_accel_event;
+	ret = blocking_notifier_chain_register(drv->smc->event_handlers, &drv->nb);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(&pdev->dev, macsmc_accel_unregister_notifier, drv);
+	if (ret)
+		return ret;
+

drivers/iio/accel/macsmc-accel.c line 246

Quoted 10 lines
drivers/iio/accel/macsmc-accel.c new file +10 -0
+	platform_set_drvdata(pdev, drv);
+
+	indio_dev->name = "macsmc-accel";
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &macsmc_accel_info;
+	indio_dev->channels = macsmc_accel_channels;
+	indio_dev->num_channels = ARRAY_SIZE(macsmc_accel_channels);
+
+	return devm_iio_device_register(&pdev->dev, indio_dev);
+}

[Severity: Medium]
Can macsmc_accel_event() hit a use-after-free on ev_int if devm_iio_device_register() fails?

The parent SMC IRQ and notifier work are already active before macsmc_accel_probe() runs, and macsmc_accel_dev_init() enables motion sensor interrupts before the IIO device is registered. If a late step in __iio_device_register() fails, it frees ev_int before devres unregisters the notifier:

CPU0 CPU1 (kworker)
macsmc_accel_probe()
macsmc_accel_dev_init()
blocking_notifier_chain_register()
devm_iio_device_register()

    iio_device_register_eventset()
      iio_dev_opaque->event_interface = ev_int
                                  apple_smc_acpi_notifier_work()
                                    macsmc_accel_event()
                                      iio_push_event()
                                        ev_int = ...->event_interface
    ... (error unwind)
    kfree(ev_int)
    iio_dev_opaque->event_interface = NULL
                                        iio_event_enabled(ev_int)
                                          <-- use-after-free

If the freed slab object is reused with its enabled bit set, kfifo_put() and wake_up_poll() can also corrupt memory. Registering the IIO device before registering the SMC notifier closes this window and ensures the notifier is torn down before iio_device_unregister() on remove.

Quoted 4 lines
drivers/mfd/macsmc-core.c +0 -0
index 6014df4fe1c0a..6567d2f1f8f53 100644

[ ... ]

drivers/mfd/macsmc-core.c int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, · line 184

Quoted 13 lines
drivers/mfd/macsmc-core.c +6 -0
@@ -169,6 +173,12 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(MOCN))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_accel,
+				ARRAY_SIZE(apple_smc_acpi_accel), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
 	} else
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
 			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);

[Severity: Medium]
Does probing SMC_KEY(MOCN) via apple_smc_key_exists() add a 1-second synchronous boot stall on PIO-based Intel Macs without a motion sensor?

On PIO Macs, apple_smc_acpi_port_get_key_info() has no non-timeout error check, so an absent key only fails after apple_smc_acpi_port_wait() polls SMC_IO_CMD_PORT for the full SMC_TIMEOUT_MS (1000 ms). Binary searching via GET_KEY_BY_INDEX (like applesmc_has_key()) avoids this timeout.

[1] https://lore.kernel.org/r/20261101013540.3af16ea0@jic23-hlaptop

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#19 lore raw

PATCH 6/14 leds: add support for macsmc kbl

In reply to Atharva Tiwari

Add a LED class driver to control the keyboard backlight.

This replaces the keyboard backlight support in applesmc.

---
 drivers/leds/Kconfig       | 11 +++++
 drivers/leds/Makefile      |  1 +
 drivers/leds/leds-macsmc.c | 91 ++++++++++++++++++++++++++++++++++++++
 drivers/mfd/macsmc-core.c  | 11 +++++
 4 files changed, 114 insertions(+)
 create mode 100644 drivers/leds/leds-macsmc.c
drivers/leds/Kconfig +11 -0
index 7ec2c8d78..30cd64d41 100644
@@ -729,6 +729,17 @@ config LEDS_ADP5520
 	  To compile this driver as a module, choose M here: the module will
 	  be called leds-adp5520.
 
+config LEDS_MACSMC
+	tristate "Keyboard Backlight support for Intel-Macs"
+	depends on LEDS_CLASS
+	depends on MACSMC_ACPI
+	help
+	  This option enables support for the Keyboard backlight in
+	  Intel-Macs using macsmc.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called leds-macsmc.
+
 config LEDS_MAX5970
 	tristate "LED Support for Maxim 5970"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index 4d4b08915..1ef8b970d 100644
@@ -63,6 +63,7 @@ obj-$(CONFIG_LEDS_LP8864)		+= leds-lp8864.o
 obj-$(CONFIG_LEDS_LT3593)		+= leds-lt3593.o
 obj-$(CONFIG_LEDS_LTC3208)		+= leds-ltc3208.o
 obj-$(CONFIG_LEDS_LTC3220)		+= leds-ltc3220.o
+obj-$(CONFIG_LEDS_MACSMC)		+= leds-macsmc.o
 obj-$(CONFIG_LEDS_MAX5970)		+= leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)		+= leds-max77650.o
 obj-$(CONFIG_LEDS_MAX77705)		+= leds-max77705.o
drivers/leds/leds-macsmc.c new file +91 -0

91 lines not on this page · raw message

drivers/mfd/macsmc-core.c +11 -0
index 6567d2f1f..bc4200f57 100644
@@ -33,6 +33,10 @@ static const struct mfd_cell apple_smc_acpi_common[] = {
 	MFD_CELL_NAME("macsmc-power"),
 };
 
+static const struct mfd_cell apple_smc_acpi_led[] = {
+	MFD_CELL_NAME("macsmc-led"),
+};
+
 static const struct mfd_cell apple_smc_acpi_light[] = {
 	MFD_CELL_NAME("macsmc-light"),
 };
@@ -179,6 +183,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 			if (ret < 0)
 				return ret;
 		}
+
+		if (apple_smc_key_exists(smc, SMC_KEY(LKSB))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_led,
+				ARRAY_SIZE(apple_smc_acpi_led), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}
 	} else
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
 			ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
Signature · 1 line
2.55.0
#20 lore raw

Re: [PATCH 06/14] leds: add support for macsmc kbl

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] leds: macsmc: stale brightness cache races with async brightness_set_blocking - [Medium] leds…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] leds: macsmc: stale brightness cache races with async brightness_set_blocking
- [Medium] leds: macsmc: missing suspend handling races with resume and parent SMC PM
- [Medium] mfd: macsmc: apple_smc_key_exists() for LKSB stalls PIO probe for 1000 ms
- [Low] leds: macsmc: commit subject, changelog, and ddata naming convention issues
- [Low] leds: macsmc: macsmc_led_probe() sets internal led_classdev.dev field
--

commit 37d499b0477cc7b3e4f2a80df75efcdc8c547b85
Author: Atharva Tiwari <[email protected]>

Quoted 9 lines

leds: add support for macsmc kbl

Add a LED class driver to control the keyboard backlight.

This replaces the keyboard backlight support in applesmc.

[Severity: Low]
This isn't a bug, but the commit subject does not follow the LEDs subsystem convention ("leds: <driver>: <Capitalized description>", for example "leds: macsmc: Add keyboard backlight driver") and uses the unexplained abbreviation "kbl".

The commit message also omits the change in drivers/mfd/macsmc-core.c that registers the macsmc-led MFD cell when SMC_KEY(LKSB) exists.

A few minor subsystem conventions: LED drivers typically name their private data pointer ddata instead of drv and report probe errors with dev_err_probe(), and MFD drivers prefer PLATFORM_DEVID_AUTO over literal -1 in devm_mfd_add_devices().

Quoted 5 lines
drivers/leds/leds-macsmc.c new file +0 -0
new file mode 100644
index 0000000000000..095653765cc80

[ ... ]

Quoted 10 lines
drivers/leds/leds-macsmc.c new file +10 -0
+static enum led_brightness macsmc_led_get_brightness(struct led_classdev *cdev)
+{
+	struct macsmc_led *drv = container_of(cdev, struct macsmc_led, cdev);
+
+	return drv->brightness;
+}
+
+static int macsmc_led_set_brightness(struct led_classdev *cdev, enum led_brightness value)
+{
+	struct macsmc_led *drv = container_of(cdev, struct macsmc_led, cdev);

drivers/leds/leds-macsmc.c line 42

Quoted 10 lines
drivers/leds/leds-macsmc.c new file +10 -0
+	int ret;
+
+	ret = apple_smc_write_u16(drv->smc, SMC_KEY(LKSB), (u16) (value & 0xFF));
+	if (ret < 0)
+		return ret;
+
+	drv->brightness = value & 0xFF;
+
+	return 0;
+}

[Severity: Medium]
Can macsmc_led_get_brightness() race with the asynchronous brightness_set_blocking work and overwrite led_cdev->brightness with a stale value?

When userspace writes a new brightness, brightness_store() updates led_cdev->brightness synchronously, queues set_brightness_work on leds_wq, and drops led_cdev->led_access. A subsequent read of brightness then calls led_update_brightness() -> macsmc_led_get_brightness() without any lock shared with macsmc_led_set_brightness():

CPU0 kworker (leds_wq)
brightness_store(new)
led_cdev->brightness = new
queue_work(set_brightness_work)

                                  set_brightness_delayed()
                                    macsmc_led_set_brightness(new)
                                      apple_smc_write_u16(LKSB, new)
brightness_show()
  led_update_brightness()
    macsmc_led_get_brightness()
      return drv->brightness  <-- stale read / data race
    led_cdev->brightness = old
                                      drv->brightness = new

Because set_brightness_delayed() does not update led_cdev->brightness afterwards, led_cdev->brightness stays stuck at the stale value until another read or write occurs, and the unlocked access to drv->brightness is a data race. Since SMC key LKSB is write-only (as in drivers/hwmon/applesmc.c), leaving brightness_get NULL lets the LED core report its synchronously maintained led_cdev->brightness.

[ ... ]

drivers/leds/leds-macsmc.c line 58

Quoted 5 lines
drivers/leds/leds-macsmc.c new file +5 -0
+	drv->cdev.name = "smc::kbd_backlight";
+	drv->cdev.max_brightness = 255;
+	drv->cdev.brightness_get = macsmc_led_get_brightness;
+	drv->cdev.brightness_set_blocking = macsmc_led_set_brightness;
+	drv->cdev.dev = &pdev->dev;

[Severity: Low]
Should drv->cdev.dev = &pdev->dev be dropped?

led_classdev.dev is an internal field populated by led_classdev_register_ext() with the newly created leds_class child device and checked by led_classdev_unregister(); the parent device is already passed to devm_led_classdev_register().

[ ... ]

drivers/leds/leds-macsmc.c line 77

Quoted 13 lines
drivers/leds/leds-macsmc.c new file +13 -0
+static int macsmc_led_resume(struct device *dev)
+{
+	struct macsmc_led *drv = dev_get_drvdata(dev);
+	int ret;
+
+	ret = apple_smc_write_u16(drv->smc, SMC_KEY(LKSB), (u16) (drv->brightness));
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(macsmc_led_pm, NULL, macsmc_led_resume);

[Severity: Medium]
Does omitting suspend handling allow set_brightness_work to race with parent SMC resume and with macsmc_led_resume()?

Because LED_CORE_SUSPENDRESUME is unset and macsmc_led_pm has no suspend callback, set_brightness_work on the non-freezable leds_wq is never quiesced across sleep. An active trigger or pending brightness update can issue an SMC write during suspend or early resume before apple_smc_acpi_resume() re-enables NTOK; if that write times out, macsmc-acpi clears interrupt_enabled to 0, preventing apple_smc_acpi_resume() from re-enabling SMC interrupts until reboot.

In addition, macsmc_led_resume() reads drv->brightness without a lock shared with macsmc_led_set_brightness():

CPU0 (resume) kworker (leds_wq)
macsmc_led_resume()
reads old drv->brightness

                                  macsmc_led_set_brightness(new)
                                    apple_smc_write_u16(LKSB, new)
                                    drv->brightness = new

apple_smc_write_u16(LKSB, old) <-- overwrites newer brightness

Setting LED_CORE_SUSPENDRESUME in drv->cdev.flags and dropping macsmc_led_resume(), macsmc_led_pm, and drv->brightness lets the LED core set LED_SUSPENDED and flush set_brightness_work on suspend, then restore brightness on resume.

Quoted 4 lines
drivers/mfd/macsmc-core.c +0 -0
index 6567d2f1f8f53..bc4200f574bd5 100644

[ ... ]

drivers/mfd/macsmc-core.c int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, · line 192

Quoted 11 lines
drivers/mfd/macsmc-core.c +7 -0
@@ -179,6 +183,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 			if (ret < 0)
 				return ret;
 		}
+
+		if (apple_smc_key_exists(smc, SMC_KEY(LKSB))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_led,
+				ARRAY_SIZE(apple_smc_acpi_led), NULL, 0, NULL);
+			if (ret < 0)
+				return ret;
+		}

[Severity: Medium]
Does probing SMC_KEY(LKSB) via apple_smc_key_exists() stall boot for a full second on PIO-based Intel Macs without a keyboard backlight?

On PIO Macs (such as desktop iMac, Mac Mini, or Mac Pro models), apple_smc_key_exists() calls apple_smc_get_key_info() -> apple_smc_acpi_port_read(), which has no error-status check and only returns an error when apple_smc_acpi_port_wait() hits the 1000 ms SMC_TIMEOUT_MS timeout. Legacy drivers/hwmon/applesmc.c avoids this stall in applesmc_has_key() by binary-searching valid key indices instead.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#21 lore raw

PATCH 7/14 rtc: add support for x86 macs in macsmc-rtc

In reply to Atharva Tiwari

Make macsmc-rtc work on Intel Macs. The ACPI RTC doesn't work correctly on T2 Macs, so the clock from the SMC is used instead.

On these machines the time offset is kept in the SMC instead of NVMEM, and the clock rate is read from the SMC too.

---
 drivers/rtc/rtc-macsmc.c | 132 +++++++++++++++++++++++++++++++++------
 1 file changed, 112 insertions(+), 20 deletions(-)
drivers/rtc/rtc-macsmc.c +112 -20
index 8fe883066..900a46339 100644
@@ -2,9 +2,12 @@
 /*
  * Apple SMC RTC driver
  * Copyright The Asahi Linux Contributors
+ *
+ * Copyright (C) 2026 Atharva Tiwari <[email protected]>
  */
 
 #include <linux/bitops.h>
+#include <linux/delay.h>
 #include <linux/mfd/macsmc.h>
 #include <linux/module.h>
 #include <linux/nvmem-consumer.h>
@@ -20,13 +23,54 @@
 /* 32768 Hz clock */
 #define RTC_SEC_SHIFT 15
 
+static bool force_clkm;
+module_param(force_clkm, bool, 0644);
+MODULE_PARM_DESC(force_clkm, "Force CLKM Key, instead of trying to use CLKL");
+
 struct macsmc_rtc {
 	struct device *dev;
 	struct apple_smc *smc;
 	struct rtc_device *rtc_dev;
 	struct nvmem_cell *rtc_offset;
+	uint8_t shift;
+	bool async_rtc;
 };
 
+static int macsmc_rtc_read_clkl(struct macsmc_rtc *rtc, u64 *ctr)
+{
+	u64 request = 0xffffffffffffULL;
+	u64 response = 0;
+	int i, j, ret;
+
+	for (i = 0; i < 4; i++) {
+		for (j = 0; j < 40; j++) {
+			ret = apple_smc_write(rtc->smc, SMC_KEY(CLKL), &request, RTC_BYTES);
+			if (ret == RTC_BYTES)
+				break;
+		}
+		if (ret != RTC_BYTES)
+			continue;
+
+		msleep(20);
+
+		for (j = 0; j < 20; j++) {
+			ret = apple_smc_read(rtc->smc, SMC_KEY(CLKL), &response, RTC_BYTES);
+			if (ret == RTC_BYTES) {
+				if ((response & 0x7fff) == 5 || (response & 0x7fff) == 6)
+					return -EAGAIN;
+
+				if (response != 0xffffffffffffULL) {
+					*ctr = response;
+					return RTC_BYTES;
+				}
+			}
+			fsleep(1000);
+		}
+	}
+
+	return -EIO;
+}
+
 static int macsmc_rtc_get_time(struct device *dev, struct rtc_time *tm)
 {
 	struct macsmc_rtc *rtc = dev_get_drvdata(dev);
@@ -36,25 +80,36 @@ static int macsmc_rtc_get_time(struct device *dev, struct rtc_time *tm)
 	size_t len;
 	int ret;
 
-	ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
+	if (rtc->async_rtc && !force_clkm)
+		ret = macsmc_rtc_read_clkl(rtc, &ctr);
+	else
+		ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
+
 	if (ret < 0)
 		return ret;
 	if (ret != RTC_BYTES)
 		return -EIO;
 
-	p_off = nvmem_cell_read(rtc->rtc_offset, &len);
-	if (IS_ERR(p_off))
-		return PTR_ERR(p_off);
-	if (len < RTC_BYTES) {
+	if (rtc->smc->is_acpi) {
+		ret = apple_smc_read(rtc->smc, SMC_KEY(CLKO), &off, RTC_BYTES);
+		if (ret < 0)
+			return ret;
+		if (ret != RTC_BYTES)
+			return -EIO;
+	} else {
+		p_off = nvmem_cell_read(rtc->rtc_offset, &len);
+		if (IS_ERR(p_off))
+			return PTR_ERR(p_off);
+		if (len < RTC_BYTES) {
+			kfree(p_off);
+			return -EIO;
+		}
+
+		memcpy(&off, p_off, RTC_BYTES);
 		kfree(p_off);
-		return -EIO;
 	}
-
-	memcpy(&off, p_off, RTC_BYTES);
-	kfree(p_off);
-
 	/* Sign extend from 48 to 64 bits, then arithmetic shift right 15 bits to get seconds */
-	now = sign_extend64(ctr + off, RTC_BITS - 1) >> RTC_SEC_SHIFT;
+	now = sign_extend64(ctr + off, RTC_BITS - 1) >> rtc->shift;
 	rtc_time64_to_tm(now, tm);
 
 	return ret;
@@ -66,14 +121,26 @@ static int macsmc_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	u64 ctr = 0, off = 0;
 	int ret;
 
-	ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
+	if (rtc->async_rtc && !force_clkm)
+		ret = macsmc_rtc_read_clkl(rtc, &ctr);
+	else
+		ret = apple_smc_read(rtc->smc, SMC_KEY(CLKM), &ctr, RTC_BYTES);
 	if (ret < 0)
 		return ret;
 	if (ret != RTC_BYTES)
 		return -EIO;
 
 	/* This sets the offset such that the set second begins now */
-	off = (rtc_tm_to_time64(tm) << RTC_SEC_SHIFT) - ctr;
+	off = (rtc_tm_to_time64(tm) << rtc->shift) - ctr;
+	if (rtc->smc->is_acpi) {
+		ret = apple_smc_write(rtc->smc, SMC_KEY(CLKO), &off, RTC_BYTES);
+		if (ret < 0)
+			return ret;
+		if (ret != RTC_BYTES)
+			return -EIO;
+
+		return 0;
+	}
 	return nvmem_cell_write(rtc->rtc_offset, &off, RTC_BYTES);
 }
 
@@ -86,13 +153,15 @@ static int macsmc_rtc_probe(struct platform_device *pdev)
 {
 	struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
 	struct macsmc_rtc *rtc;
+	int ret;
+	u32 clkr = 0xFFFFFFFF;
 
 	/*
 	 * MFD will probe this device even without a node in the device tree,
 	 * thus bail out early if the SMC on the current machines does not
 	 * support RTC and has no node in the device tree.
 	 */
-	if (!pdev->dev.of_node)
+	if (!pdev->dev.of_node && !smc->is_acpi)
 		return -ENODEV;
 
 	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
@@ -102,18 +171,40 @@ static int macsmc_rtc_probe(struct platform_device *pdev)
 	rtc->dev = &pdev->dev;
 	rtc->smc = smc;
 
-	rtc->rtc_offset = devm_nvmem_cell_get(&pdev->dev, "rtc_offset");
-	if (IS_ERR(rtc->rtc_offset))
-		return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_offset),
-				     "Failed to get rtc_offset NVMEM cell\n");
+	if (!rtc->smc->is_acpi) {
+		rtc->rtc_offset = devm_nvmem_cell_get(&pdev->dev, "rtc_offset");
+		if (IS_ERR(rtc->rtc_offset))
+			return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_offset),
+					     "Failed to get rtc_offset NVMEM cell\n");
+	}
+
+	if (rtc->smc->is_acpi) {
+		ret = apple_smc_read_u32(rtc->smc, SMC_KEY(CLKR), &clkr);
+		if (ret < 0 || clkr == 0xFFFFFFFF)
+			rtc->shift = RTC_SEC_SHIFT;
+		else
+			rtc->shift = ffs(clkr) - 1;
+
+		if (apple_smc_key_exists(rtc->smc, SMC_KEY(CLKL))) {
+			u64 clkl = 0xffffffffffffULL;
+
+			ret = apple_smc_read(rtc->smc, SMC_KEY(CLKL), &clkl, RTC_BYTES);
+			if (ret == RTC_BYTES && clkl != 0xffffffffffffULL)
+				rtc->async_rtc = true;
+		}
+	} else
+		rtc->shift = RTC_SEC_SHIFT;
+
+	dev_info(&pdev->dev, "%u clock, using %s key\n", (clkr != 0xFFFFFFFF) ? clkr : 32768,
+				rtc->async_rtc ? "CLKL" : "CLKM");
 
 	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
 	if (IS_ERR(rtc->rtc_dev))
 		return PTR_ERR(rtc->rtc_dev);
 
 	rtc->rtc_dev->ops = &macsmc_rtc_ops;
-	rtc->rtc_dev->range_min = S64_MIN >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
-	rtc->rtc_dev->range_max = S64_MAX >> (RTC_SEC_SHIFT + (64 - RTC_BITS));
+	rtc->rtc_dev->range_min = S64_MIN >> (rtc->shift + (64 - RTC_BITS));
+	rtc->rtc_dev->range_max = S64_MAX >> (rtc->shift + (64 - RTC_BITS));
 
 	platform_set_drvdata(pdev, rtc);
 
@@ -138,3 +229,4 @@ module_platform_driver(macsmc_rtc_driver);
 MODULE_LICENSE("Dual MIT/GPL");
 MODULE_DESCRIPTION("Apple SMC RTC driver");
 MODULE_AUTHOR("Hector Martin <[email protected]>");
+MODULE_ALIAS("platform:macsmc-rtc");
Signature · 1 line
2.55.0
#22 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

Quoted 6 lines · Atharva Tiwari

Make macsmc-rtc work on Intel Macs. The ACPI RTC doesn't work correctly on T2 Macs, so the clock from the SMC is used instead.

On these machines the time offset is kept in the SMC instead of NVMEM, and the clock rate is read from the SMC too.

...

drivers/rtc/rtc-macsmc.c line 28

Quoted 3 lines
drivers/rtc/rtc-macsmc.c +3 -0
+static bool force_clkm;
+module_param(force_clkm, bool, 0644);
+MODULE_PARM_DESC(force_clkm, "Force CLKM Key, instead of trying to use CLKL");

No, why?!

...

drivers/rtc/rtc-macsmc.c line 35

Quoted 6 lines

struct macsmc_rtc {

drivers/rtc/rtc-macsmc.c +1 -0
 	struct device *dev;
 	struct apple_smc *smc;
 	struct rtc_device *rtc_dev;
 	struct nvmem_cell *rtc_offset;
+	uint8_t shift;

No, use kernel types.

Quoted 2 lines
drivers/rtc/rtc-macsmc.c +1 -0
+	bool async_rtc;
 };

...

Quoted 24 lines
drivers/rtc/rtc-macsmc.c +24 -0
+static int macsmc_rtc_read_clkl(struct macsmc_rtc *rtc, u64 *ctr)
+{
+	u64 request = 0xffffffffffffULL;
+	u64 response = 0;
+	int i, j, ret;
+
+	for (i = 0; i < 4; i++) {
+		for (j = 0; j < 40; j++) {
+			ret = apple_smc_write(rtc->smc, SMC_KEY(CLKL), &request, RTC_BYTES);
+			if (ret == RTC_BYTES)
+				break;
+		}
+		if (ret != RTC_BYTES)
+			continue;
+
+		msleep(20);
+
+		for (j = 0; j < 20; j++) {
+			ret = apple_smc_read(rtc->smc, SMC_KEY(CLKL), &response, RTC_BYTES);
+			if (ret == RTC_BYTES) {
+				if ((response & 0x7fff) == 5 || (response & 0x7fff) == 6)
+					return -EAGAIN;
+
+				if (response != 0xffffffffffffULL) {

drivers/rtc/rtc-macsmc.c line 72

Quoted 10 lines
drivers/rtc/rtc-macsmc.c +10 -0
+					*ctr = response;
+					return RTC_BYTES;
+				}
+			}
+			fsleep(1000);
+		}
+	}
+
+	return -EIO;
+}

Really, folks, read the last a year code or so to understand why the above is no go (style-wise). It's your homework, you can find all of the typical issues in the mailing list archives.

...

Quoted 1 line

+MODULE_ALIAS("platform:macsmc-rtc");

Are you sure it's a best approach?

...

Overall impression that you copied'n'pasted AI slop without much effort made on editing and understanding. Also looks like each patch requires a logical split to a mini-series.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#23 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

Quoted 75 lines · Andy Shevchenko
Quoted 6 lines · Atharva Tiwari

Make macsmc-rtc work on Intel Macs. The ACPI RTC doesn't work correctly on T2 Macs, so the clock from the SMC is used instead.

On these machines the time offset is kept in the SMC instead of NVMEM, and the clock rate is read from the SMC too.

...

drivers/rtc/rtc-macsmc.c line 28

Quoted 3 lines
drivers/rtc/rtc-macsmc.c +3 -0
+static bool force_clkm;
+module_param(force_clkm, bool, 0644);
+MODULE_PARM_DESC(force_clkm, "Force CLKM Key, instead of trying to use CLKL");

No, why?!

...

drivers/rtc/rtc-macsmc.c line 35

Quoted 6 lines

struct macsmc_rtc {

drivers/rtc/rtc-macsmc.c +1 -0
 	struct device *dev;
 	struct apple_smc *smc;
 	struct rtc_device *rtc_dev;
 	struct nvmem_cell *rtc_offset;
+	uint8_t shift;

No, use kernel types.

Quoted 2 lines
drivers/rtc/rtc-macsmc.c +1 -0
+	bool async_rtc;
 };

...

Quoted 24 lines
drivers/rtc/rtc-macsmc.c +24 -0
+static int macsmc_rtc_read_clkl(struct macsmc_rtc *rtc, u64 *ctr)
+{
+	u64 request = 0xffffffffffffULL;
+	u64 response = 0;
+	int i, j, ret;
+
+	for (i = 0; i < 4; i++) {
+		for (j = 0; j < 40; j++) {
+			ret = apple_smc_write(rtc->smc, SMC_KEY(CLKL), &request, RTC_BYTES);
+			if (ret == RTC_BYTES)
+				break;
+		}
+		if (ret != RTC_BYTES)
+			continue;
+
+		msleep(20);
+
+		for (j = 0; j < 20; j++) {
+			ret = apple_smc_read(rtc->smc, SMC_KEY(CLKL), &response, RTC_BYTES);
+			if (ret == RTC_BYTES) {
+				if ((response & 0x7fff) == 5 || (response & 0x7fff) == 6)
+					return -EAGAIN;
+
+				if (response != 0xffffffffffffULL) {

drivers/rtc/rtc-macsmc.c line 72

Quoted 10 lines
drivers/rtc/rtc-macsmc.c +10 -0
+					*ctr = response;
+					return RTC_BYTES;
+				}
+			}
+			fsleep(1000);
+		}
+	}
+
+	return -EIO;
+}

Really, folks, read the last a year code or so to understand why the above is no go (style-wise). It's your homework, you can find all of the typical issues in the mailing list archives.

...

Quoted 1 line

+MODULE_ALIAS("platform:macsmc-rtc");

Quoted 8 lines

Are you sure it's a best approach?

...

Overall impression that you copied'n'pasted AI slop without much effort made on editing and understanding. Also looks like each patch requires a logical split to a mini-series.

The whole code is being developed before AI was compatible enough to generate all this. Thanks for the AI insight. The logical split is good enough IMO. Pretty much same as what Asahi Linux did. I would be nicer if you could elaborate the split by a few examples rather than just making a statement.

#24 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

Quoted 2 lines · Aditya Garg
Quoted 1 line · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:47AM -0500, Atharva Tiwari wrote:

...

Quoted 6 lines
Quoted 3 lines

Overall impression that you copied'n'pasted AI slop without much effort made on editing and understanding. Also looks like each patch requires a logical split to a mini-series.

The whole code is being developed before AI was compatible enough to generate all this. Thanks for the AI insight. The logical split is good enough IMO.

Not only me thinks otherwise.

Quoted 1 line

Pretty much same as what Asahi Linux did.

Is Asahi Linux now the Golden Standard for the Linux kernel patches?

Quoted 2 lines

I would be nicer if you could elaborate the split by a few examples rather than just making a statement.

Examples? Hmm... I'm puzzled a bit by the request, but if you really want something, take any of my 8+ patch series in the past from Git history.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#25 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

Quoted 10 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 2 lines · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:47AM -0500, Atharva Tiwari wrote:

...

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Overall impression that you copied'n'pasted AI slop without much effort made on editing and understanding. Also looks like each patch requires a logical split to a mini-series.

The whole code is being developed before AI was compatible enough to generate all this. Thanks for the AI insight. The logical split is good enough IMO.

Not only me thinks otherwise.

No worries. It's probably not worth discussing AI here anyways.

Quoted 4 lines
Quoted 2 lines

Pretty much same as what Asahi Linux did.

Is Asahi Linux now the Golden Standard for the Linux kernel patches?

What I am quoting are patches that are upstreamed into the kernel. I believe they had similar vigorous review like these. Nevertheless, Sven has pointed out the errors here.

Quoted 6 lines
Quoted 3 lines

I would be nicer if you could elaborate the split by a few examples rather than just making a statement.

Examples? Hmm... I'm puzzled a bit by the request, but if you really want something, take any of my 8+ patch series in the past from Git history.

Well this was a reply to a comment where you were asking where the author was for the past half year. My bad, I replied to the wrong email here.

Quoted 6 lines

--
With Best Regards,
Andy Shevchenko

#26 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

Quoted 4 lines · Aditya Garg
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 2 lines · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:47AM -0500, Atharva Tiwari wrote:

...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Pretty much same as what Asahi Linux did.

Is Asahi Linux now the Golden Standard for the Linux kernel patches?

What I am quoting are patches that are upstreamed into the kernel. I believe they had similar vigorous review like these. Nevertheless, Sven has pointed out the errors here.

I understand, but sometimes bad code appears in the kernel because of "business needs", I saw so many times that even authored by Intel.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#27 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

In reply to Aditya Garg
Quoted 9 lines · Aditya Garg
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Atharva Tiwari

Make macsmc-rtc work on Intel Macs. The ACPI RTC doesn't work correctly on T2 Macs, so the clock from the SMC is used instead.

On these machines the time offset is kept in the SMC instead

[...]

Quoted 2 lines

The logical split is good enough IMO. Pretty much same as what Asahi Linux did.

Nope, this series here is *very* different. Please take a look at those series again if you truly believe that they are similar. There was no treewide and similar weird things in there. There were at least three separate series for SMC support because a single series would've been much too large.

I've only looked at the first commit which could probably be its own mini series that could land first here but I wouldn't be surprised if the other commits have similar issues and should also be split into smaller logical chunks.

Sven

#28 lore raw

Re: [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc

In reply to Atharva Tiwari
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] rtc: macsmc: shift underflow and UB on invalid CLKR in macsmc_rtc_probe() - [Medium] rtc: macsmc…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] rtc: macsmc: shift underflow and UB on invalid CLKR in macsmc_rtc_probe()
- [Medium] rtc: macsmc: valid timestamps rejected with -EAGAIN in macsmc_rtc_read_clkl()
- [Medium] rtc: macsmc: unthrottled CLKL retries amplify SMC timeouts to 160+ seconds
- [Low] rtc: macsmc: inconsistent CLKR and key logging in macsmc_rtc_probe()
- [Low] rtc: macsmc: undocumented CLKL protocol, magic values, and force_clkm param
--

commit 6ab7cff8bec4fb4d17c73d18ad219c5e9e888129
Author: Atharva Tiwari <[email protected]>

Quoted 11 lines

rtc: add support for x86 macs in macsmc-rtc

Make macsmc-rtc work on Intel Macs. The ACPI RTC doesn't work correctly on T2 Macs, so the clock from the SMC is used instead.

On these machines the time offset is kept in the SMC instead of NVMEM, and the clock rate is read from the SMC too.

[Severity: Low]
This isn't a bug, but the commit message does not explain the new CLKL latch/poll protocol or the force_clkm module parameter. Documenting why CLKL is needed over CLKM and what its magic values (0xffffffffffffULL, 0x7fff, 5,
6) mean in the changelog and comments would help maintainability.

Also, the CLKL/CLKM read logic is duplicated in macsmc_rtc_get_time() and macsmc_rtc_set_time(), and macsmc_rtc_probe() splits the rtc->smc->is_acpi check across two adjacent if blocks.

Quoted 4 lines
drivers/rtc/rtc-macsmc.c +0 -0
index 8fe8830669569..900a46339f72f 100644

[ ... ]

Quoted 9 lines
drivers/rtc/rtc-macsmc.c +9 -0
+static int macsmc_rtc_read_clkl(struct macsmc_rtc *rtc, u64 *ctr)
+{
+	u64 request = 0xffffffffffffULL;
+	u64 response = 0;
+	int i, j, ret;
+
+	for (i = 0; i < 4; i++) {
+		for (j = 0; j < 40; j++) {
+			ret = apple_smc_write(rtc->smc, SMC_KEY(CLKL), &request, RTC_BYTES);

drivers/rtc/rtc-macsmc.c line 57

Quoted 10 lines
drivers/rtc/rtc-macsmc.c +10 -0
+			if (ret == RTC_BYTES)
+				break;
+		}
+		if (ret != RTC_BYTES)
+			continue;
+
+		msleep(20);
+
+		for (j = 0; j < 20; j++) {
+			ret = apple_smc_read(rtc->smc, SMC_KEY(CLKL), &response, RTC_BYTES);

[Severity: Medium]
Can these retry loops block for 160+ seconds when the SMC is unresponsive?

The outer and inner loops perform up to 160 apple_smc_write() attempts with no delay, backoff, timeout bailout, or signal check, followed by msleep(20) and up to 80 apple_smc_read() attempts. In macsmc-acpi.c, SMC_TIMEOUT_MS is 1000 ms, so timed-out CLKL transactions amplify into multi-minute stalls.

Because RTC callers hold rtc->ops_lock and every SMC transaction takes the shared smc->mutex, a stall exceeds the hung-task timeout and blocks sibling SMC subdevices as well as boot, suspend/resume, and sysfs RTC readers. Even on healthy hardware, msleep(20) adds at least 20 ms to every CLKL access.

drivers/rtc/rtc-macsmc.c line 66

Quoted 9 lines
drivers/rtc/rtc-macsmc.c +9 -0
+			if (ret == RTC_BYTES) {
+				if ((response & 0x7fff) == 5 || (response & 0x7fff) == 6)
+					return -EAGAIN;
+
+				if (response != 0xffffffffffffULL) {
+					*ctr = response;
+					return RTC_BYTES;
+				}
+			}

[Severity: Medium]
Will masking response with 0x7fff reject valid timestamps with -EAGAIN?

The lower 15 bits of the 48-bit counter are the sub-second ticks at 32768 Hz, so any valid counter value whose low 15 bits equal 5 or 6 (twice every second) aborts immediately. Neither macsmc_rtc_read_clkl() nor RTC core callers such as rtc_hctosys() retry on -EAGAIN, causing sporadic RTC read/write failures.

[ ... ]

Quoted 1 line
drivers/rtc/rtc-macsmc.c +0 -0
@@ -102,18 +171,40 @@ static int macsmc_rtc_probe(struct platform_device *pdev)

[ ... ]

drivers/rtc/rtc-macsmc.c static int macsmc_rtc_probe(struct platform_device *pdev) · line 186

Quoted 6 lines
drivers/rtc/rtc-macsmc.c +6 -0
+	if (rtc->smc->is_acpi) {
+		ret = apple_smc_read_u32(rtc->smc, SMC_KEY(CLKR), &clkr);
+		if (ret < 0 || clkr == 0xFFFFFFFF)
+			rtc->shift = RTC_SEC_SHIFT;
+		else
+			rtc->shift = ffs(clkr) - 1;

[Severity: Medium]
Can an invalid CLKR value underflow rtc->shift and cause shift-out-of-bounds undefined behavior?

If firmware returns clkr == 0, ffs(0) - 1 wraps uint8_t rtc->shift to 255. That makes the 64-bit shifts for range_min/range_max in macsmc_rtc_probe() (by 271 bits) and the shifts in macsmc_rtc_get_time() and macsmc_rtc_set_time() (by 255 bits) undefined behavior.

Any non-power-of-two clkr also silently computes the wrong shift. Checking ret < 0 || !is_power_of_2(clkr) rejects 0, 0xFFFFFFFF, and non-powers of two while keeping rtc->shift in 0..31.

[ ... ]

drivers/rtc/rtc-macsmc.c static int macsmc_rtc_probe(struct platform_device *pdev) · line 199

Quoted 2 lines
drivers/rtc/rtc-macsmc.c +2 -0
+	dev_info(&pdev->dev, "%u clock, using %s key\n", (clkr != 0xFFFFFFFF) ? clkr : 32768,
+				rtc->async_rtc ? "CLKL" : "CLKM");

[Severity: Low]
Can this dev_info() log a bogus clock rate or the wrong key?

If apple_smc_acpi_port_read() fails partway through reading CLKR, it has already written partial bytes into clkr before returning -EIO, so clkr is no longer 0xFFFFFFFF even though rtc->shift fell back to RTC_SEC_SHIFT. Using 1U << rtc->shift avoids printing that corrupted value.

Also, rtc->async_rtc ? "CLKL" : "CLKM" ignores force_clkm, so with force_clkm=1 on a CLKL system probe logs "using CLKL key" while get/set_time actually use CLKM.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#29 lore raw

PATCH 8/14 treewide: Add a flag to detect the Apple T2 chip

In reply to Atharva Tiwari

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

---
 arch/x86/kernel/quirks.c                      | 37 ++++++++++++++++++
 include/linux/platform_data/x86/apple.h       |  5 +++
 .../platform_certs/keyring_handler.h          |  8 ----
 security/integrity/platform_certs/load_uefi.c | 38 ++++---------------
 4 files changed, 50 insertions(+), 38 deletions(-)
arch/x86/kernel/quirks.c +37 -0
index a92f18db9..6651e5d4d 100644
@@ -664,8 +664,45 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras
 bool x86_apple_machine;
 EXPORT_SYMBOL(x86_apple_machine);
 
+bool has_apple_t2_chip;
+EXPORT_SYMBOL(has_apple_t2_chip);
+
+static const char * const __initconst apple_t2_models[] = {
+	"MacBookPro15,1",
+	"MacBookPro15,2",
+	"MacBookPro15,3",
+	"MacBookPro15,4",
+	"MacBookPro16,1",
+	"MacBookPro16,2",
+	"MacBookPro16,3",
+	"MacBookPro16,4",
+	"MacBookAir8,1",
+	"MacBookAir8,2",
+	"MacBookAir9,1",
+	"Macmini8,1",
+	"MacPro7,1",
+	"iMac20,1",
+	"iMac20,2",
+	"iMacPro1,1",
+};
+
 void __init early_platform_quirks(void)
 {
 	x86_apple_machine = dmi_match(DMI_SYS_VENDOR, "Apple Inc.") ||
 			    dmi_match(DMI_SYS_VENDOR, "Apple Computer, Inc.");
+
+	if (x86_apple_machine) {
+		const char *product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
+		int i;
+
+		if (!product_name)
+			return;
+
+		for (i = 0; i < ARRAY_SIZE(apple_t2_models); i++) {
+			if (!strcmp(product_name, apple_t2_models[i])) {
+				has_apple_t2_chip = true;
+				break;
+			}
+		}
+	}
 }
include/linux/platform_data/x86/apple.h +5 -0
index 079e816c3..47b27dcea 100644
@@ -6,8 +6,13 @@
  * x86_apple_machine - whether the machine is an x86 Apple Macintosh
  */
 extern bool x86_apple_machine;
+/**
+ * has_apple_t2_chip - whether the machine has the Apple T2 chip
+ */
+extern bool has_apple_t2_chip;
 #else
 #define x86_apple_machine false
+#define has_apple_t2_chip false
 #endif
 
 #endif
security/integrity/platform_certs/keyring_handler.h +0 -8
index f92895cc5..a355f4400 100644
@@ -45,11 +45,3 @@ efi_element_handler_t get_handler_for_code_signing_keys(const efi_guid_t *sig_ty
 efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type);
 
 #endif
-
-#ifndef UEFI_QUIRK_SKIP_CERT
-#define UEFI_QUIRK_SKIP_CERT(vendor, product) \
-		 .matches = { \
-			DMI_MATCH(DMI_BOARD_VENDOR, vendor), \
-			DMI_MATCH(DMI_PRODUCT_NAME, product), \
-		},
-#endif
security/integrity/platform_certs/load_uefi.c +8 -30
index c0d694844..b4096d4c8 100644
@@ -3,42 +3,16 @@
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/cred.h>
-#include <linux/dmi.h>
 #include <linux/err.h>
 #include <linux/efi.h>
 #include <linux/slab.h>
 #include <linux/ima.h>
+#include <linux/platform_data/x86/apple.h>
 #include <keys/asymmetric-type.h>
 #include <keys/system_keyring.h>
 #include "../integrity.h"
 #include "keyring_handler.h"
 
-/*
- * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
- * certificates causes occurrence of a page fault in Apple's firmware and
- * a crash disabling EFI runtime services. The following quirk skips reading
- * these variables.
- */
-static const struct dmi_system_id uefi_skip_cert[] = {
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,3") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro15,4") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,3") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookPro16,4") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir8,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacBookAir9,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "Macmini8,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "MacPro7,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,1") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMac20,2") },
-	{ UEFI_QUIRK_SKIP_CERT("Apple Inc.", "iMacPro1,1") },
-	{ }
-};
-
 /*
  * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
  * it does.
@@ -165,10 +139,14 @@ static int __init load_uefi_certs(void)
 	unsigned long dbsize = 0, dbxsize = 0, mokxsize = 0;
 	efi_status_t status;
 	int rc = 0;
-	const struct dmi_system_id *dmi_id;
 
-	dmi_id = dmi_first_match(uefi_skip_cert);
-	if (dmi_id) {
+	/*
+	 * On T2 Macs reading the db and dbx efi variables to load UEFI Secure Boot
+	 * certificates causes occurrence of a page fault in Apple's firmware and
+	 * a crash disabling EFI runtime services. The following quirk skips reading
+	 * these variables.
+	 */
+	if (has_apple_t2_chip) {
 		pr_err("Reading UEFI Secure Boot Certs is not supported on T2 Macs.\n");
 		return false;
 	}
Signature · 1 line
2.55.0
#30 lore raw

Re: [PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Low] x86: inconsistent naming and missing __ro_after_init on has_apple_t2_chip - [Low] x86: open-coded…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Low] x86: inconsistent naming and missing __ro_after_init on has_apple_t2_chip
- [Low] x86: open-coded match_string(), early return, and __initconst style
- [Low] x86/apple: missing var keyword in has_apple_t2_chip kernel-doc comment
- [Low] treewide: commit message omits load_uefi.c refactoring and export context
--

commit a2285f7954298eb43ab5023f81b0b6900c8dde80
Author: Atharva Tiwari <[email protected]>

Quoted 8 lines

treewide: Add a flag to detect the Apple T2 chip

Add a flag to detect Apple T2 chips on Intel Macs. Cache the result to avoid repeated checks. That will be used in upcoming patches.

[Severity: Low]
Should the commit message mention that this patch also converts load_uefi_certs() in security/integrity/platform_certs/load_uefi.c to use has_apple_t2_chip and removes uefi_skip_cert and UEFI_QUIRK_SKIP_CERT? It would also help reviewers to note which upcoming drivers or runtime paths need has_apple_t2_chip exported globally.

arch/x86/kernel/quirks.c DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras · line 668

Quoted 10 lines
arch/x86/kernel/quirks.c +2 -0
index a92f18db96100..6651e5d4d106f 100644
@@ -664,8 +664,45 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras
 bool x86_apple_machine;
 EXPORT_SYMBOL(x86_apple_machine);
 
+bool has_apple_t2_chip;
+EXPORT_SYMBOL(has_apple_t2_chip);

[Severity: Low]
Should this flag keep the x86_apple_ prefix from x86_apple_machine (such as x86_apple_t2 or x86_apple_t2_machine)? Because x86/apple.h defines it to false when CONFIG_X86 is disabled, an unprefixed name is misleading on arm64 kernels running on Apple T2 SoC hardware.

Since has_apple_t2_chip is only written during early_platform_quirks(), it can also be marked __ro_after_init.

Quoted 3 lines
arch/x86/kernel/quirks.c +3 -0
+
+static const char * const __initconst apple_t2_models[] = {
+	"MacBookPro15,1",

[ ... ]

Quoted 13 lines
arch/x86/kernel/quirks.c +9 -0
+	"iMacPro1,1",
+};
+
 void __init early_platform_quirks(void)
 {
 	x86_apple_machine = dmi_match(DMI_SYS_VENDOR, "Apple Inc.") ||
 			    dmi_match(DMI_SYS_VENDOR, "Apple Computer, Inc.");
+
+	if (x86_apple_machine) {
+		const char *product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
+		int i;
+
+		if (!product_name)

arch/x86/kernel/quirks.c DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x2083, quirk_intel_purley_xeon_ras · line 708

Quoted 10 lines
arch/x86/kernel/quirks.c +9 -0
+			return;
+
+		for (i = 0; i < ARRAY_SIZE(apple_t2_models); i++) {
+			if (!strcmp(product_name, apple_t2_models[i])) {
+				has_apple_t2_chip = true;
+				break;
+			}
+		}
+	}
 }

[Severity: Low]
Could this loop reuse match_string(apple_t2_models, ARRAY_SIZE(apple_t2_models), product_name) >= 0? Returning early when product_name is NULL also exits early_platform_quirks() entirely, which would skip any future quirks added after this block.

Also, per include/linux/init.h, __initconst belongs between the variable name and "=" (apple_t2_models[] __initconst =). Note that an array of pointers only puts the pointer table in .init.rodata while leaving the model strings in permanent .rodata.

include/linux/platform_data/x86/apple.h line 12

Quoted 12 lines
include/linux/platform_data/x86/apple.h +4 -0
index 079e816c3c219..47b27dceab18c 100644
@@ -6,8 +6,13 @@
  * x86_apple_machine - whether the machine is an x86 Apple Macintosh
  */
 extern bool x86_apple_machine;
+/**
+ * has_apple_t2_chip - whether the machine has the Apple T2 chip
+ */
+extern bool has_apple_t2_chip;

[Severity: Low]
Does this kernel-doc comment need the "var" keyword ("* var has_apple_t2_chip - ...")? Without it, scripts/kernel-doc parses the declaration as a function and emits a "cannot understand function prototype" warning.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#31 lore raw

PATCH 9/14 treewide: disable other rtc's in t2 macs

In reply to Atharva Tiwari

The CMOS RTC and the ACPI TAD clock don't work correctly on T2 Macs. When macsmc-rtc is enabled, turn them off on these machines so macsmc-rtc is the only clock.

Apple also uses RTC to update NTP instead of wallclock so we do that also.

---
 arch/x86/kernel/rtc.c   | 5 +++++
 drivers/acpi/acpi_tad.c | 5 +++++
 drivers/rtc/rtc-cmos.c  | 5 +++++
 3 files changed, 15 insertions(+)
arch/x86/kernel/rtc.c +5 -0
index 314b062a1..90f24e6ce 100644
@@ -3,6 +3,7 @@
  * RTC related functions
  */
 #include <linux/acpi.h>
+#include <linux/platform_data/x86/apple.h>
 #include <linux/platform_device.h>
 #include <linux/mc146818rtc.h>
 #include <linux/export.h>
@@ -101,6 +102,10 @@ EXPORT_SYMBOL(rtc_cmos_write);
 
 int update_persistent_clock64(struct timespec64 now)
 {
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		return -ENODEV;
+
 	return x86_platform.set_wallclock(&now);
 }
 
drivers/acpi/acpi_tad.c +5 -0
index 45459a4ca..a13716451 100644
@@ -28,6 +28,7 @@
 #include <linux/ktime.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/platform_data/x86/apple.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/rtc.h>
@@ -845,6 +846,10 @@ static int acpi_tad_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		caps &= ~ACPI_TAD_RT;
+
 	if (!acpi_has_method(handle, "_PRW")) {
 		dev_info(dev, "Missing _PRW\n");
 		caps &= ~(ACPI_TAD_AC_WAKE | ACPI_TAD_DC_WAKE);
drivers/rtc/rtc-cmos.c +5 -0
index fa04ece15..f930a36d0 100644
@@ -33,6 +33,7 @@
 #include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
+#include <linux/platform_data/x86/apple.h>
 #include <linux/platform_device.h>
 #include <linux/log2.h>
 #include <linux/pm.h>
@@ -944,6 +945,10 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
 		.priv = &cmos_rtc,
 	};
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		return -ENODEV;
+
 	/* there can be only one ... */
 	if (cmos_rtc.dev)
 		return -EBUSY;
Signature · 1 line
2.55.0
#32 lore raw

Re: [PATCH 09/14] treewide: disable other rtc's in t2 macs

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] x86/rtc: read_persistent_clock64() still reads broken CMOS RTC on T2 Macs - [Medium] rtc…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] x86/rtc: read_persistent_clock64() still reads broken CMOS RTC on T2 Macs
- [Medium] rtc: compile-time IS_ENABLED() disables T2 Mac RTCs if macsmc-rtc fails
- [Low] ACPI: TAD: acpi_tad_probe() clears ACPI_TAD_RT without ACPI_TAD_RT_IN_MS
- [Low] treewide: duplicated leaf-driver Kconfig guard across x86, ACPI, and RTC
- [Low] treewide: commit message reverses NTP-to-RTC synchronization direction
--

commit 4cff46337e95a353ab01666127ac4401097aabbb
Author: Atharva Tiwari <[email protected]>

treewide: disable other rtc's in t2 macs

This commit disables the CMOS RTC, the ACPI TAD real-time clock capability, and legacy x86 persistent clock updates on Apple T2 Macs when CONFIG_MACSMC_ACPI and CONFIG_RTC_DRV_MACSMC are enabled.

Quoted 2 lines

Apple also uses RTC to update NTP instead of wallclock so we do that also.

[Severity: Low]
Does this sentence have the synchronization direction reversed? Returning -ENODEV from update_persistent_clock64() causes sync_hw_clock() in kernel/time/ntp.c to fall back to update_rtc(), so NTP updates the RTC class device instead of the legacy x86 wallclock, rather than the RTC updating NTP.

Quoted 4 lines
arch/x86/kernel/rtc.c +0 -0
index 314b062a15de9..90f24e6cebfda 100644

[ ... ]

arch/x86/kernel/rtc.c EXPORT_SYMBOL(rtc_cmos_write); · line 110

Quoted 10 lines
arch/x86/kernel/rtc.c +4 -0
@@ -101,6 +102,10 @@ EXPORT_SYMBOL(rtc_cmos_write);
 
 int update_persistent_clock64(struct timespec64 now)
 {
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		return -ENODEV;
+
 	return x86_platform.set_wallclock(&now);
 }

[Severity: High]
Should read_persistent_clock64() also bypass the CMOS RTC on T2 Macs? Right now it still calls x86_platform.get_wallclock(ts), which invokes mach_get_cmos_time() on the broken CMOS clock that NTP will no longer update.

On boot, if that read returns any valid non-zero timestamp, timekeeping_init() initializes wall time from it and sets persistent_clock_exists = true. That makes timekeeping_rtc_skipsuspend() return true, so rtc_suspend() skips snapshotting macsmc-rtc.

On S3 resume, timekeeping_resume() then either injects a bogus CMOS delta and skips rtc_resume(), or leaves rtc_resume() to compute sleep time against zeroed old_rtc and old_system snapshots. If CMOS UIP is stuck, mc146818_get_time() also busy-waits for 1000 ms on boot, suspend, and resume.

Returning early from read_persistent_clock64() with ts->tv_sec and ts->tv_nsec zeroed under the same T2 guard keeps persistent_clock_exists false so boot and suspend/resume use macsmc-rtc.

Quoted 4 lines
drivers/acpi/acpi_tad.c +0 -0
index 45459a4cafae3..a137164511f7b 100644

[ ... ]

drivers/acpi/acpi_tad.c static int acpi_tad_probe(struct platform_device *pdev) · line 855

Quoted 11 lines
drivers/acpi/acpi_tad.c +4 -0
@@ -845,6 +846,10 @@ static int acpi_tad_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		caps &= ~ACPI_TAD_RT;
+
 	if (!acpi_has_method(handle, "_PRW")) {
 		dev_info(dev, "Missing _PRW\n");
 		caps &= ~(ACPI_TAD_AC_WAKE | ACPI_TAD_DC_WAKE);

[Severity: Low]
Should this also clear ACPI_TAD_RT_IN_MS? Leaving it set in dd->capabilities lets caps_show() report millisecond real-time accuracy in sysfs even though ACPI_TAD_RT has been disabled, unlike the ACPI_TAD_AC_WAKE / ACPI_TAD_DC_WAKE handling just below.

Quoted 4 lines
drivers/rtc/rtc-cmos.c +0 -0
index 209aa4ec732c8..9a6cecc192955 100644

[ ... ]

drivers/rtc/rtc-cmos.c cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) · line 978

Quoted 11 lines
drivers/rtc/rtc-cmos.c +4 -0
@@ -968,6 +969,10 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
 		.priv = &cmos_rtc,
 	};
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_RTC_DRV_MACSMC) && has_apple_t2_chip)
+		return -ENODEV;
+
 	/* there can be only one ... */
 	if (cmos_rtc.dev)
 		return -EBUSY;

[Severity: Medium]
Can gating these checks on compile-time IS_ENABLED() leave a T2 Mac with no working RTC when CONFIG_MACSMC_ACPI=m and CONFIG_RTC_DRV_MACSMC=m?

drivers/hwmon/applesmc.c still matches all T2 Macs via applesmc_whitelist[] with no exclusion against macsmc-acpi, and reserves the same SMC I/O ports (0x300..0x31f). If applesmc loads first, apple_smc_acpi_probe() fails with -EBUSY in devm_request_region(), so macsmc-rtc is never instantiated.

Because cmos_do_probe(), acpi_tad_probe(), and update_persistent_clock64() already returned early, the system ends up with no /dev/rtc* device (and no cmos_nvram or acpi_cmos_wake_setup()), and sync_hw_clock() fails with -ENODEV.

Either the broken T2 RTCs should be quirked directly while resolving the applesmc port conflict, or the fallback should not be disabled based solely on tristate Kconfig symbols.

[Severity: Low]
This isn't a bug, but repeating the leaf-driver Kconfig check across arch/x86, ACPI, and RTC couples generic drivers to downstream symbol names. Moving it to a static inline helper in include/linux/platform_data/x86/apple.h (and splitting the changes per subsystem) would keep the checks consistent and easier to review.

The commit message would also benefit from explaining how the CMOS RTC and ACPI TAD fail on T2 Macs, and why disabling all of rtc-cmos (including cmos_nvram and acpi_cmos_wake_setup()) is safe there.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#33 lore raw

PATCH 10/14 power/supply: add support for x86 macs in macsmc-power

In reply to Atharva Tiwari

Make macsmc-power work on Intel Macs, as a replacement for acpi-sbs. It exposes a lot more sensors than acpi-sbs, such as voltage, current, power, temperature, charge and health info, and also the adapter state.

It also adds a charge limit, which acpi-sbs doesn't offer.

And now we have power stats on non-mobile macs as well (iMacs, MacPros,etc), using macsmc-ac.

---
 drivers/power/supply/macsmc-power.c | 803 ++++++++++++++++++++++------
 1 file changed, 654 insertions(+), 149 deletions(-)
drivers/power/supply/macsmc-power.c +654 -149
index ced37a701..2ebf620a2 100644
@@ -7,8 +7,11 @@
  * on Apple Silicon systems.
  *
  * Copyright The Asahi Linux Contributors
+ *
+ * Copyright (C) 2026 Atharva Tiwari <[email protected]>
  */
 
+#include <linux/acpi.h>
 #include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/devm-helpers.h>
@@ -80,12 +83,17 @@ struct macsmc_power {
 	char serial_number[MAX_STRING_LENGTH];
 	char mfg_date[MAX_STRING_LENGTH];
 
+	bool has_battery;
+
 	/* Supported feature flags based on SMC key presence */
+	bool has_b0ap; /* Power Now (New intel macs and Apple silicon) */
 	bool has_chwa; /* Charge limit (Modern firmware) */
-	bool has_chls; /* Charge limit (Older firmware) */
+	bool has_chls; /* Charge limit (Older firmware And New firmware on Intel macs) */
 	bool has_ch0i; /* Force discharge (Older firmware) */
 	bool has_ch0c; /* Inhibit charge (Older firmware) */
 	bool has_chte; /* Inhibit charge (Modern firmware) */
+	bool has_pd0r; /* AC Adapter current power in watts (Old Intel macs) */
+
 	/*
 	 * Battery critical key is 1 byte and charge key is little endian
 	 * (Modern firmware)
@@ -95,6 +103,7 @@ struct macsmc_power {
 	u8 num_cells;
 	int nominal_voltage_mv;
 
+	struct acpi_device *adapter;
 	struct notifier_block nb;
 	struct work_struct critical_work;
 	bool emergency_shutdown_triggered;
@@ -195,6 +204,41 @@ static int macsmc_battery_get_status(struct macsmc_power *power)
 	return POWER_SUPPLY_STATUS_CHARGING;
 }
 
+static int macsmc_battery_acpi_get_status(struct macsmc_power *power)
+{
+	acpi_status status;
+	int ret;
+	s16 vs16;
+	u16 vu16;
+	unsigned long long adapter_status;
+
+	ret = apple_smc_read_s16(power->smc, SMC_KEY(B0AC), &vs16);
+	if (ret < 0)
+		return ret;
+
+	ret = apple_smc_read_u16(power->smc, SMC_KEY(BRSC), &vu16);
+	if (ret < 0)
+		return ret;
+
+	vu16 = swab16(vu16);
+	vs16 = (s16)swab16(vs16);
+
+	status = acpi_evaluate_integer(power->adapter->handle, "_PSR", NULL, &adapter_status);
+
+	if (ACPI_SUCCESS(status) && !!adapter_status) {
+		if (vu16 == 100 && vs16 <= 0)
+			return POWER_SUPPLY_STATUS_FULL;
+
+		if (vs16 > 0)
+			return POWER_SUPPLY_STATUS_CHARGING;
+
+		return POWER_SUPPLY_STATUS_NOT_CHARGING;
+	}
+
+	return POWER_SUPPLY_STATUS_DISCHARGING;
+}
+
+
 static int macsmc_battery_get_charge_behaviour(struct macsmc_power *power)
 {
 	int ret;
@@ -322,6 +366,210 @@ static int macsmc_battery_get_capacity_level(struct macsmc_power *power)
 		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 }
 
+static int macsmc_battery_acpi_get_capacity_level(struct macsmc_power *power)
+{
+	int ret;
+	u16 vu16;
+
+	ret = apple_smc_read_u16(power->smc, SMC_KEY(BRSC), &vu16);
+	if (ret < 0)
+		return ret;
+
+	vu16 = swab16(vu16);
+
+	if (vu16 <= 5)
+		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
+	if (vu16 <= 20)
+		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
+	if (vu16 <= 80)
+		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
+	if (vu16 < 100)
+		return POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
+
+	return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
+}
+
+static int macsmc_battery_acpi_read_f32_scaled(struct apple_smc *smc, smc_key key,
+	int *p, int scale)
+{
+	u32 fval;
+	u64 val;
+	int ret, exp;
+
+	ret = apple_smc_read_u32(smc, key, &fval);
+	if (ret < 0)
+		return ret;
+
+	val = (u64)((fval & GENMASK(22, 0)) | BIT(23));
+	exp = ((fval >> 23) & 0xff) - 127 - 23;
+
+	val *= (u64)scale;
+
+	if (exp > 63)
+		val = U64_MAX;
+	else if (exp < -63)
+		val = 0;
+	else if (exp < 0)
+		val >>= -exp;
+	else if (exp != 0 &&
+		 (val & ~((1ULL << (64 - exp)) - 1)))
+		val = U64_MAX;
+	else
+		val <<= exp;
+
+	if (fval & BIT(31)) {
+		if (val > (u64)INT_MAX + 1)
+			*p = INT_MIN;
+		else
+			*p = -(int)val;
+	} else {
+		if (val > INT_MAX)
+			*p = INT_MAX;
+		else
+			*p = (int)val;
+	}
+
+	return 0;
+}
+
+static int macsmc_battery_acpi_read(struct apple_smc *smc, smc_key key, int *val,
+	int scale)
+{
+	int ret;
+	struct apple_smc_key_info info;
+
+	ret = apple_smc_get_key_info(smc, key, &info);
+	if (ret)
+		return ret;
+
+	switch (info.type_code) {
+	/* 32-bit IEEE 754 float */
+	case __SMC_KEY('f', 'l', 't', ' '): {
+		int flt_ = 0;
+
+		ret = macsmc_battery_acpi_read_f32_scaled(smc, key, &flt_, scale);
+		if (ret)
+			return ret;
+
+		*val = flt_;
+		break;
+	}
+
+	/* 3.13 fixed point decimal */
+	case __SMC_KEY('f', 'p', '3', 'd'): {
+		u16 fp3d;
+
+		ret = apple_smc_read_u16(smc, key, &fp3d);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13));
+		break;
+	}
+
+	/* 5.11 fixed point decimal */
+	case __SMC_KEY('f', 'p', '5', 'b'): {
+		u16 fp5b;
+
+		ret = apple_smc_read_u16(smc, key, &fp5b);
+		if (ret)
+			return ret;
+
+		*val = mult_frac(be16_to_cpu(fp5b), scale, BIT(11));
+		break;
+	}
+
+	/* 8.8 fixed point decimal */
+	case __SMC_KEY('f', 'p', '8', '8'): {
+		u16 fp88;
+
+		ret = apple_smc_read_u16(smc, key, &fp88);
+		if (ret)
+			return ret;
+
+		*val = mult_frac(be16_to_cpu(fp88), scale, BIT(8));
+		break;
+	}
+
+	/* 3.12 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '3', 'c'): {
+		s16 sp3c;
+
+		ret = apple_smc_read_s16(smc, key, &sp3c);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp3c), scale, BIT(12));
+		break;
+	}
+
+	/* 4.11 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '4', 'b'): {
+		s16 sp4b;
+
+		ret = apple_smc_read_s16(smc, key, &sp4b);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s16)be16_to_cpu(sp4b), scale, BIT(11));
+		break;
+	}
+
+	/* 5.10 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '5', 'a'): {
+		s16 sp5a;
+
+		ret = apple_smc_read_s16(smc, key, &sp5a);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s16)be16_to_cpu(sp5a), scale, BIT(10));
+		break;
+	}
+
+	/* 6.9 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '6', '9'): {
+		s16 sp69;
+
+		ret = apple_smc_read_s16(smc, key, &sp69);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s16)be16_to_cpu(sp69), scale, BIT(9));
+		break;
+	}
+
+	/* 7.8 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '7', '8'): {
+		s16 sp78;
+
+		ret = apple_smc_read_s16(smc, key, &sp78);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s16)be16_to_cpu(sp78), scale, BIT(8));
+		break;
+	}
+
+	/* 8.7 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '8', '7'): {
+		s16 sp87;
+
+		ret = apple_smc_read_s16(smc, key, &sp87);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s16)be16_to_cpu(sp87), scale, BIT(7));
+		break;
+	}
+
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
 static s16 macsmc_swap_b0rm(struct macsmc_power *power, s16 b0rm)
 {
 	/* B0RM was Big Endian, likely pass through from TI gas gauge */
@@ -343,7 +591,10 @@ static int macsmc_battery_get_property(struct power_supply *psy,
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_STATUS:
-		val->intval = macsmc_battery_get_status(power);
+		if (power->smc->is_acpi)
+			val->intval = macsmc_battery_acpi_get_status(power);
+		else
+			val->intval = macsmc_battery_get_status(power);
 		ret = val->intval < 0 ? val->intval : 0;
 		break;
 	case POWER_SUPPLY_PROP_PRESENT:
@@ -359,36 +610,82 @@ static int macsmc_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0TF), &vu16);
-		val->intval = vu16 == 0xffff ? 0 : vu16 * 60;
+		if (power->smc->is_acpi)
+			val->intval = vu16 == 0xffff ? 0 : swab16(vu16) * 60;
+		else
+			val->intval = vu16 == 0xffff ? 0 : vu16 * 60;
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY:
-		ret = apple_smc_read_u8(power->smc, SMC_KEY(BUIC), &vu8);
-		val->intval = vu8;
+		if (power->smc->is_acpi) {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(BRSC), &vu16);
+			val->intval = swab16(vu16);
+		} else {
+			ret = apple_smc_read_u8(power->smc, SMC_KEY(BUIC), &vu8);
+			val->intval = vu8;
+		}
 		break;
 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
-		val->intval = macsmc_battery_get_capacity_level(power);
+		if (power->smc->is_acpi)
+			val->intval = macsmc_battery_acpi_get_capacity_level(power);
+		else
+			val->intval = macsmc_battery_get_capacity_level(power);
 		ret = val->intval < 0 ? val->intval : 0;
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi)
+			val->intval = swab16(vu16) * 1000;
+		else
+			val->intval = vu16 * 1000;
 		break;
 	case POWER_SUPPLY_PROP_CURRENT_NOW:
 		ret = apple_smc_read_s16(power->smc, SMC_KEY(B0AC), &vs16);
-		val->intval = vs16 * 1000;
+		if (power->smc->is_acpi)
+			val->intval = (s16)swab16(vs16) * 1000;
+		else
+			val->intval = vs16 * 1000;
 		break;
 	case POWER_SUPPLY_PROP_POWER_NOW:
-		ret = apple_smc_read_s32(power->smc, SMC_KEY(B0AP), &vs32);
-		val->intval = vs32 * 1000;
+		if (power->smc->is_acpi) {
+			if (power->has_b0ap) {
+				ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(B0AP),
+						&val->intval, 1000000);
+				if (ret < 0)
+					return ret;
+
+				val->intval = -val->intval;
+			} else {
+				ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16);
+				if (ret < 0)
+					return ret;
+
+				ret = apple_smc_read_s16(power->smc, SMC_KEY(B0AC), &vs16);
+				if (ret < 0)
+					return ret;
+
+				val->intval = (s16)swab16(vs16) * swab16(vu16);
+			}
+		} else {
+			ret = apple_smc_read_s32(power->smc, SMC_KEY(B0AP), &vs32);
+			val->intval = vs32 * 1000;
+		}
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(BITV), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi)
+			val->intval = swab16(vu16) * 1000;
+		else
+			val->intval = vu16 * 1000;
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
 		/* Calculate total max design voltage from per-cell maximum voltage */
-		ret = apple_smc_read_u16(power->smc, SMC_KEY(BVVN), &vu16);
-		val->intval = vu16 * 1000 * power->num_cells;
+		if (power->smc->is_acpi) {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(BCMV), &vu16);
+			val->intval = swab16(vu16) * 1000 * power->num_cells;
+		} else {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(BVVN), &vu16);
+			val->intval = vu16 * 1000 * power->num_cells;
+		}
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
 		/* Lifetime min */
@@ -400,17 +697,36 @@ static int macsmc_battery_get_property(struct power_supply *psy,
 		ret = apple_smc_read_s16(power->smc, SMC_KEY(BLPX), &vs16);
 		val->intval = vs16 * 1000;
 		break;
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+		if (power->has_chls) {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(CHLS), &vu16);
+			val->intval = vu16 & 0xFF;
+		} else {
+			ret = apple_smc_read_u8(power->smc, SMC_KEY(BCLM), &vu8);
+			val->intval = vu8;
+		}
+		break;
 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RC), &vu16);
 		val->intval = vu16 * 1000;
 		break;
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
-		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RI), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi) {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(CHBI), &vu16);
+			val->intval = swab16(vu16) * 1000;
+		} else {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RI), &vu16);
+			val->intval = vu16 * 1000;
+		}
 		break;
 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
-		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RV), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi) {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(CHBV), &vu16);
+			val->intval = swab16(vu16) * 1000;
+		} else {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RV), &vu16);
+			val->intval = vu16 * 1000;
+		}
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16);
@@ -418,27 +734,64 @@ static int macsmc_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_FULL:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0FC), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi)
+			val->intval = swab16(vu16) * 1000;
+		else
+			val->intval = vu16 * 1000;
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_NOW:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16);
-		val->intval = macsmc_swap_b0rm(power, vu16) * 1000;
+		if (power->smc->is_acpi)
+			val->intval = swab16(vu16) * 1000;
+		else
+			val->intval = macsmc_swap_b0rm(power, vu16) * 1000;
 		break;
 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16);
-		val->intval = vu16 * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = vu16 * swab16(voltage);
+		} else
+			val->intval = vu16 * power->nominal_voltage_mv;
 		break;
 	case POWER_SUPPLY_PROP_ENERGY_FULL:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0FC), &vu16);
-		val->intval = vu16 * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = swab16(vu16) * swab16(voltage);
+		} else
+			val->intval = vu16 * power->nominal_voltage_mv;
 		break;
 	case POWER_SUPPLY_PROP_ENERGY_NOW:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16);
-		val->intval = macsmc_swap_b0rm(power, vu16) * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = swab16(vu16) * swab16(voltage);
+		} else
+			val->intval = macsmc_swap_b0rm(power, vu16) * power->nominal_voltage_mv;
 		break;
 	case POWER_SUPPLY_PROP_TEMP:
-		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AT), &vu16);
-		val->intval = vu16 - 2732; /* Kelvin x10 to Celsius x10 */
+		if (power->smc->is_acpi)
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(TB0T), &val->intval, 10);
+		else {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AT), &vu16);
+			val->intval = vu16 - 2732; /* Kelvin x10 to Celsius x10 */
+		}
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
 		ret = apple_smc_read_s64(power->smc, SMC_KEY(BAAC), &vs64);
@@ -446,7 +799,10 @@ static int macsmc_battery_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0CT), &vu16);
-		val->intval = vu16;
+		if (power->smc->is_acpi)
+			val->intval = swab16(vu16);
+		else
+			val->intval = vu16;
 		break;
 	case POWER_SUPPLY_PROP_SCOPE:
 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
@@ -489,6 +845,15 @@ static int macsmc_battery_set_property(struct power_supply *psy,
 	switch (psp) {
 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
 		return macsmc_battery_set_charge_behaviour(power, val->intval);
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+		if (val->intval > 100 || val->intval < 1)
+			return -EINVAL;
+		if (power->has_chls) {
+			u16 vu16 = val->intval & 0xFF;
+
+			return apple_smc_write_u16(power->smc, SMC_KEY(CHLS), vu16);
+		}
+		return apple_smc_write_u8(power->smc, SMC_KEY(BCLM), (u8) val->intval);
 	default:
 		return -EINVAL;
 	}
@@ -500,6 +865,8 @@ static int macsmc_battery_property_is_writeable(struct power_supply *psy,
 	switch (psp) {
 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
 		return true;
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+		return true;
 	default:
 		return false;
 	}
@@ -524,12 +891,55 @@ static int macsmc_ac_get_property(struct power_supply *psy,
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_ONLINE:
-		ret = apple_smc_read_u32(power->smc, SMC_KEY(CHIS), &vu32);
-		val->intval = !!vu32;
+		if (power->smc->is_acpi) {
+			if (power->has_battery) {
+				acpi_status status;
+				unsigned long long adapter_status;
+
+				status = acpi_evaluate_integer(power->adapter->handle,
+								"_PSR", NULL, &adapter_status);
+				if (ACPI_FAILURE(status))
+					return -EIO;
+
+				val->intval = !!adapter_status;
+			} else
+				val->intval = 1;
+		} else {
+			ret = apple_smc_read_u32(power->smc, SMC_KEY(CHIS), &vu32);
+			val->intval = !!vu32;
+		}
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		if (power->has_battery)
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(ID0R),
+					&val->intval, 1000000);
+		else
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(ID2R),
+					&val->intval, 1000000);
+		break;
+	case POWER_SUPPLY_PROP_POWER_NOW:
+		if (power->has_pd0r)
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(PD0R),
+					&val->intval, 1000000);
+		else if (power->has_battery)
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(PDTR),
+					&val->intval, 1000000);
+		else
+			ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(PD2R),
+					&val->intval, 1000000);
 		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
-		ret = apple_smc_read_u16(power->smc, SMC_KEY(AC-n), &vu16);
-		val->intval = vu16 * 1000;
+		if (power->smc->is_acpi) {
+			if (power->has_battery)
+				ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(VD0R),
+						&val->intval, 1000000);
+			else
+				ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(VD2R),
+						&val->intval, 1000000);
+		} else {
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(AC-n), &vu16);
+			val->intval = vu16 * 1000;
+		}
 		break;
 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(AC-i), &vu16);
@@ -537,7 +947,10 @@ static int macsmc_ac_get_property(struct power_supply *psy,
 		break;
 	case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
 		ret = apple_smc_read_u32(power->smc, SMC_KEY(ACPW), &vu32);
-		val->intval = vu32 * 1000;
+		if (power->smc->is_acpi)
+			val->intval = swab32(vu32) * 1000;
+		else
+			val->intval = vu32 * 1000;
 		break;
 	default:
 		return -EINVAL;
@@ -634,6 +1047,19 @@ static int macsmc_power_event(struct notifier_block *nb, unsigned long event, vo
 	return NOTIFY_DONE;
 }
 
+static void macsmc_power_acpi_notify(acpi_handle handle, u32 event, void *context)
+{
+	struct macsmc_power *power = context;
+
+	if (event == 0x80) {
+		if (power->batt)
+			power_supply_changed(power->batt);
+		if (power->ac)
+			power_supply_changed(power->ac);
+	}
+}
+
+
 static int macsmc_power_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -667,18 +1093,23 @@ static int macsmc_power_probe(struct platform_device *pdev)
 		return ret;
 
 	/*
-	 * Check for battery presence.
-	 * B0AV is a fundamental key.
+	 * On Intel Mac's we have a key that indicates if the device has a battery,
+	 * but on Apple silicon Mac's we dont, so we check for B0AV
 	 */
-	if (apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16) == 0 &&
+	if (smc->is_acpi && apple_smc_read_flag(power->smc, SMC_KEY(BBIN), &flag) == 0 && flag)
+		has_battery = true;
+
+	else if (!smc->is_acpi &&
+	    apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16) == 0 &&
 	    macsmc_battery_get_status(power) > POWER_SUPPLY_STATUS_UNKNOWN)
 		has_battery = true;
 
+	power->has_battery = has_battery;
 	/*
 	 * Check for AC adapter presence.
 	 * CHIS is a fundamental key.
 	 */
-	if (apple_smc_key_exists(smc, SMC_KEY(CHIS)))
+	if (apple_smc_key_exists(smc, SMC_KEY(CHIS)) || smc->is_acpi)
 		has_ac_adapter = true;
 
 	if (!has_battery && !has_ac_adapter)
@@ -694,118 +1125,161 @@ static int macsmc_power_probe(struct platform_device *pdev)
 
 		nprops = 0;
 
-		/* Fundamental properties */
-		props[nprops++] = POWER_SUPPLY_PROP_STATUS;
-		props[nprops++] = POWER_SUPPLY_PROP_PRESENT;
-		props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_CAPACITY;
-		props[nprops++] = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
-		props[nprops++] = POWER_SUPPLY_PROP_TEMP;
-		props[nprops++] = POWER_SUPPLY_PROP_CYCLE_COUNT;
-		props[nprops++] = POWER_SUPPLY_PROP_HEALTH;
-		props[nprops++] = POWER_SUPPLY_PROP_SCOPE;
-		props[nprops++] = POWER_SUPPLY_PROP_MODEL_NAME;
-		props[nprops++] = POWER_SUPPLY_PROP_SERIAL_NUMBER;
-		props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_YEAR;
-		props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_MONTH;
-		props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_DAY;
-
-		/* Extended properties usually present */
-		props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
-		props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
-		props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
-		props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
-		props[nprops++] = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
-		props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
-		props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
-		props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
-		props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL;
-		props[nprops++] = POWER_SUPPLY_PROP_CHARGE_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
-		props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL;
-		props[nprops++] = POWER_SUPPLY_PROP_ENERGY_NOW;
-		props[nprops++] = POWER_SUPPLY_PROP_CHARGE_COUNTER;
-
-		/* Detect features based on key availability */
-		if (apple_smc_key_exists(smc, SMC_KEY(CHTE)))
-			power->has_chte = true;
-		if (apple_smc_key_exists(smc, SMC_KEY(CH0C)))
-			power->has_ch0c = true;
-		if (apple_smc_key_exists(smc, SMC_KEY(CH0I)))
-			power->has_ch0i = true;
-
-		ret = apple_smc_get_key_info(power->smc, SMC_KEY(BCF0), &info);
-		if (ret) {
-			dev_err(&pdev->dev, "Failed to determine BCF0 key size\n");
-			return ret;
-		}
-		if (info.size == 1)
-			power->fw_ge_27 = true;
-		else if (info.size == 4)
-			power->fw_ge_27 = false;
-		else {
-			dev_err(&pdev->dev, "Unexpected BCF0 key size %d\n", info.size);
-			return -EIO;
-		}
-
-		/* Reset "Optimised Battery Charging" flags to default state */
-		if (power->has_chte)
-			apple_smc_write_u32(smc, SMC_KEY(CHTE), 0);
-		else if (power->has_ch0c)
-			apple_smc_write_u8(smc, SMC_KEY(CH0C), 0);
-
-		if (power->has_ch0i)
-			apple_smc_write_u8(smc, SMC_KEY(CH0I), 0);
-
-		apple_smc_write_u8(smc, SMC_KEY(CH0K), 0);
-		apple_smc_write_u8(smc, SMC_KEY(CH0B), 0);
-
-		/* Configure charge behaviour if supported */
-		if (power->has_ch0i || power->has_ch0c || power->has_chte) {
-			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR;
-
-			power->batt_desc.charge_behaviours =
-				BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
+		if (smc->is_acpi) {
+			props[nprops++] = POWER_SUPPLY_PROP_STATUS;
+			props[nprops++] = POWER_SUPPLY_PROP_PRESENT;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CAPACITY;
+			props[nprops++] = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
+			props[nprops++] = POWER_SUPPLY_PROP_TEMP;
+			props[nprops++] = POWER_SUPPLY_PROP_CYCLE_COUNT;
+			props[nprops++] = POWER_SUPPLY_PROP_HEALTH;
+			props[nprops++] = POWER_SUPPLY_PROP_SCOPE;
+			props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
+			props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
+			props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL;
+			props[nprops++] = POWER_SUPPLY_PROP_ENERGY_NOW;
+
+			if (apple_smc_key_exists(smc, SMC_KEY(B0DC))) {
+				props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
+				props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
+			}
+			if (apple_smc_key_exists(smc, SMC_KEY(CHLS)))
+				power->has_chls = true;
+			if (power->has_chls || apple_smc_key_exists(smc, SMC_KEY(BCLM)))
+				props[nprops++] = POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD;
+			if (apple_smc_key_exists(smc, SMC_KEY(B0AP)))
+				power->has_b0ap = true;
+			if (apple_smc_key_exists(smc, SMC_KEY(B0TE)))
+				props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
+			if (apple_smc_key_exists(smc, SMC_KEY(BITV)))
+				props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
+			if (apple_smc_key_exists(smc, SMC_KEY(BCMV)))
+				props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
+
+			if (apple_smc_key_exists(smc, SMC_KEY(BC3V)))
+				power->num_cells = 3;
+			else
+				power->num_cells = 2;
+		} else {
+			/* Fundamental properties */
+			props[nprops++] = POWER_SUPPLY_PROP_STATUS;
+			props[nprops++] = POWER_SUPPLY_PROP_PRESENT;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CAPACITY;
+			props[nprops++] = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
+			props[nprops++] = POWER_SUPPLY_PROP_TEMP;
+			props[nprops++] = POWER_SUPPLY_PROP_CYCLE_COUNT;
+			props[nprops++] = POWER_SUPPLY_PROP_HEALTH;
+			props[nprops++] = POWER_SUPPLY_PROP_SCOPE;
+			props[nprops++] = POWER_SUPPLY_PROP_MODEL_NAME;
+			props[nprops++] = POWER_SUPPLY_PROP_SERIAL_NUMBER;
+			props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_YEAR;
+			props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_MONTH;
+			props[nprops++] = POWER_SUPPLY_PROP_MANUFACTURE_DAY;
+
+			/* Extended properties usually present */
+			props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
+			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT;
+			props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
+			props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
+			props[nprops++] = POWER_SUPPLY_PROP_ENERGY_FULL;
+			props[nprops++] = POWER_SUPPLY_PROP_ENERGY_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_CHARGE_COUNTER;
+
+			/* Detect features based on key availability */
+			if (apple_smc_key_exists(smc, SMC_KEY(CHTE)))
+				power->has_chte = true;
+			if (apple_smc_key_exists(smc, SMC_KEY(CH0C)))
+				power->has_ch0c = true;
+			if (apple_smc_key_exists(smc, SMC_KEY(CH0I)))
+				power->has_ch0i = true;
+
+			ret = apple_smc_get_key_info(power->smc, SMC_KEY(BCF0), &info);
+			if (ret) {
+				dev_err(&pdev->dev, "Failed to determine BCF0 key size\n");
+				return ret;
+			}
+			if (info.size == 1)
+				power->fw_ge_27 = true;
+			else if (info.size == 4)
+				power->fw_ge_27 = false;
+			else {
+				dev_err(&pdev->dev, "Unexpected BCF0 key size %d\n", info.size);
+				return -EIO;
+			}
+
+			/* Reset "Optimised Battery Charging" flags to default state */
+			if (power->has_chte)
+				apple_smc_write_u32(smc, SMC_KEY(CHTE), 0);
+			else if (power->has_ch0c)
+				apple_smc_write_u8(smc, SMC_KEY(CH0C), 0);
 
 			if (power->has_ch0i)
-				power->batt_desc.charge_behaviours |=
-					BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
-
-			if (power->has_chte || power->has_ch0c)
-				power->batt_desc.charge_behaviours |=
-					BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
+				apple_smc_write_u8(smc, SMC_KEY(CH0I), 0);
+
+			apple_smc_write_u8(smc, SMC_KEY(CH0K), 0);
+			apple_smc_write_u8(smc, SMC_KEY(CH0B), 0);
+
+			/* Configure charge behaviour if supported */
+			if (power->has_ch0i || power->has_ch0c || power->has_chte) {
+				props[nprops++] = POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR;
+
+				power->batt_desc.charge_behaviours =
+					BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
+
+				if (power->has_ch0i)
+					power->batt_desc.charge_behaviours |=
+						BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
+
+				if (power->has_chte || power->has_ch0c)
+					power->batt_desc.charge_behaviours |=
+						BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
+			}
+
+			/* Detect charge limit method (CHWA vs CHLS) */
+			if (apple_smc_read_flag(power->smc, SMC_KEY(CHWA), &flag) == 0)
+				power->has_chwa = true;
+			else if (apple_smc_read_u16(power->smc, SMC_KEY(CHLS), &vu16) >= 0)
+				power->has_chls = true;
+
+			/* Fetch identity strings */
+			apple_smc_read(smc, SMC_KEY(BMDN), power->model_name,
+				       sizeof(power->model_name) - 1);
+			apple_smc_read(smc, SMC_KEY(BMSN), power->serial_number,
+				       sizeof(power->serial_number) - 1);
+			apple_smc_read(smc, SMC_KEY(BMDT), power->mfg_date,
+				       sizeof(power->mfg_date) - 1);
+
+			apple_smc_read_u8(power->smc, SMC_KEY(BNCB), &power->num_cells);
+			power->nominal_voltage_mv = MACSMC_NOMINAL_CELL_VOLTAGE_MV *
+							power->num_cells;
+
+			/* Enable critical shutdown notifications by reading status once */
+			macsmc_battery_read_bcf0(power, &val32);
 		}
-
-		/* Detect charge limit method (CHWA vs CHLS) */
-		if (apple_smc_read_flag(power->smc, SMC_KEY(CHWA), &flag) == 0)
-			power->has_chwa = true;
-		else if (apple_smc_read_u16(power->smc, SMC_KEY(CHLS), &vu16) >= 0)
-			power->has_chls = true;
-
 		if (nprops > MACSMC_MAX_BATT_PROPS)
 			return -ENOMEM;
 
 		power->batt_desc.properties = props;
 		power->batt_desc.num_properties = nprops;
 
-		/* Fetch identity strings */
-		apple_smc_read(smc, SMC_KEY(BMDN), power->model_name,
-			       sizeof(power->model_name) - 1);
-		apple_smc_read(smc, SMC_KEY(BMSN), power->serial_number,
-			       sizeof(power->serial_number) - 1);
-		apple_smc_read(smc, SMC_KEY(BMDT), power->mfg_date,
-			       sizeof(power->mfg_date) - 1);
-
-		apple_smc_read_u8(power->smc, SMC_KEY(BNCB), &power->num_cells);
-		power->nominal_voltage_mv = MACSMC_NOMINAL_CELL_VOLTAGE_MV * power->num_cells;
-
-		/* Enable critical shutdown notifications by reading status once */
-		macsmc_battery_read_bcf0(power, &val32);
-
 		psy_cfg.drv_data = power;
 		power->batt = devm_power_supply_register(dev, &power->batt_desc, &psy_cfg);
 		if (IS_ERR(power->batt)) {
@@ -829,16 +1303,27 @@ static int macsmc_power_probe(struct platform_device *pdev)
 		/* Online status is fundamental */
 		props[nprops++] = POWER_SUPPLY_PROP_ONLINE;
 
-		/* Input power limits are usually available */
-		if (apple_smc_key_exists(power->smc, SMC_KEY(ACPW)))
-			props[nprops++] = POWER_SUPPLY_PROP_INPUT_POWER_LIMIT;
-
-		/* macOS 15.4+ firmware dropped legacy AC keys (AC-n, AC-i) */
-		if (apple_smc_read_u16(power->smc, SMC_KEY(AC-n), &vu16) >= 0) {
-			props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
-			props[nprops++] = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
+		if (smc->is_acpi) {
+			props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+			if (apple_smc_key_exists(power->smc, SMC_KEY(PD0R)))
+				power->has_pd0r = true;
+			/* MacBookPro5,x and 7,x dosent have VD0R key or any equivalent */
+			if (!power->has_battery || apple_smc_key_exists(power->smc, SMC_KEY(VD0R)))
+				props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+			if (apple_smc_key_exists(power->smc, SMC_KEY(ACPW)))
+				props[nprops++] = POWER_SUPPLY_PROP_INPUT_POWER_LIMIT;
+		} else {
+			/* Input power limits are usually available */
+			if (apple_smc_key_exists(power->smc, SMC_KEY(ACPW)))
+				props[nprops++] = POWER_SUPPLY_PROP_INPUT_POWER_LIMIT;
+
+			/* macOS 15.4+ firmware dropped legacy AC keys (AC-n, AC-i) */
+			if (apple_smc_read_u16(power->smc, SMC_KEY(AC-n), &vu16) >= 0) {
+				props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+				props[nprops++] = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
+			}
 		}
-
 		if (nprops > MACSMC_MAX_AC_PROPS)
 			return -ENOMEM;
 
@@ -858,9 +1343,22 @@ static int macsmc_power_probe(struct platform_device *pdev)
 	if (!power->batt && !power->ac)
 		return -ENODEV;
 
-	power->nb.notifier_call = macsmc_power_event;
-	blocking_notifier_chain_register(smc->event_handlers, &power->nb);
+	if (!smc->is_acpi) {
+		power->nb.notifier_call = macsmc_power_event;
+		blocking_notifier_chain_register(smc->event_handlers, &power->nb);
+	} else if (power->has_battery) {
+		power->adapter = acpi_dev_get_first_match_dev("ACPI0003", NULL, -1);
+		if (!power->adapter)
+			return -ENODEV;
 
+		ret = acpi_dev_install_notify_handler(power->adapter, ACPI_DEVICE_NOTIFY,
+					macsmc_power_acpi_notify, power);
+		if (ret) {
+			acpi_dev_put(power->adapter);
+			power->adapter = NULL;
+			return ret;
+		}
+	}
 	return 0;
 }
 
@@ -868,7 +1366,13 @@ static void macsmc_power_remove(struct platform_device *pdev)
 {
 	struct macsmc_power *power = dev_get_drvdata(&pdev->dev);
 
-	blocking_notifier_chain_unregister(power->smc->event_handlers, &power->nb);
+	if (!power->smc->is_acpi)
+		blocking_notifier_chain_unregister(power->smc->event_handlers, &power->nb);
+	else if (power->has_battery) {
+		acpi_dev_remove_notify_handler(power->adapter, ACPI_DEVICE_NOTIFY,
+				macsmc_power_acpi_notify);
+		acpi_dev_put(power->adapter);
+	}
 }
 
 static const struct platform_device_id macsmc_power_id[] = {
@@ -891,3 +1395,4 @@ MODULE_LICENSE("Dual MIT/GPL");
 MODULE_DESCRIPTION("Apple SMC battery and power management driver");
 MODULE_AUTHOR("Hector Martin <[email protected]>");
 MODULE_AUTHOR("Michael Reeves <[email protected]>");
+MODULE_ALIAS("platform:macsmc-power");
Signature · 1 line
2.55.0
#34 lore raw

Re: [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power

Thank you for your contribution! Sashiko AI review found 12 potential issues to consider: - [High] power: supply: macsmc: NULL deref of power->adapter in macsmc_power_probe() - [Medium] power…

Thank you for your contribution! Sashiko AI review found 12 potential issues to consider:
- [High] power: supply: macsmc: NULL deref of power->adapter in macsmc_power_probe()
- [Medium] power: supply: macsmc: build failure when CONFIG_ACPI is disabled
- [Medium] power: supply: macsmc: premature acpi_dev_put() in macsmc_power_remove()
- [Medium] power: supply: macsmc: signed 64-bit overflow via BIT() in mult_frac()
- [Medium] power: supply: macsmc: ENERGY_FULL* uses live B0AV instead of nominal voltage
- [Medium] power: supply: macsmc: missing swab16() for B0TE on ACPI Macs
- [Medium] power: supply: macsmc: AC properties registered without SMC key checks
- [Medium] power: supply: macsmc: reversed OR probes non-existent CHIS on Intel Macs
- [Low] power: supply: macsmc: duplicate float decoder in macsmc-power
- [Low] power: supply: macsmc: sparse __be16 mismatch in macsmc_battery_acpi_read()
- [Low] power: supply: macsmc: uninitialized voltage read on B0AV failure
- [Low] power: supply: macsmc: repeated apple_smc_get_key_info() on runtime reads
--

commit 82e1fded22961a0b758f2413ade4066e910b5ff5
Author: Atharva Tiwari <[email protected]>

power/supply: add support for x86 macs in macsmc-power

This commit adds Intel x86 Mac support to the macsmc-power driver using ACPI adapter status and notifications along with big-endian and fixed-point SMC keys, exposing battery charge thresholds and AC power telemetry.

Quoted 4 lines
drivers/power/supply/macsmc-power.c +0 -0
index ced37a7019981..2ebf620a22c9c 100644

[ ... ]

Quoted 6 lines
drivers/power/supply/macsmc-power.c +2 -0
@@ -195,6 +204,41 @@ static int macsmc_battery_get_status(struct macsmc_power *power)
 	return POWER_SUPPLY_STATUS_CHARGING;
 }
 
+static int macsmc_battery_acpi_get_status(struct macsmc_power *power)
+{

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_status(struct macsmc_power *power) · line 226

Quoted 1 line
drivers/power/supply/macsmc-power.c +1 -0
+	status = acpi_evaluate_integer(power->adapter->handle, "_PSR", NULL, &adapter_status);

[Severity: Medium]
Does this build when CONFIG_ACPI is disabled?

CONFIG_MACSMC_POWER only depends on MFD_MACSMC_CORE, which does not depend on ACPI (for example on DeviceTree-only Apple Silicon ARM64 kernels). When CONFIG_ACPI is disabled, struct acpi_device is only forward-declared and acpi_evaluate_integer(), acpi_dev_install_notify_handler(), and acpi_dev_remove_notify_handler() have no stubs in include/acpi/acpi_bus.h. The same unguarded ACPI calls also appear in macsmc_ac_get_property(), macsmc_power_probe(), and macsmc_power_remove().

[ ... ]

Quoted 1 line
drivers/power/supply/macsmc-power.c +0 -0
@@ -322,6 +366,210 @@ static int macsmc_battery_get_capacity_level(struct macsmc_power *power)

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_capacity_level(struct macsmc_power *power) · line 393

Quoted 2 lines
drivers/power/supply/macsmc-power.c +2 -0
+static int macsmc_battery_acpi_read_f32_scaled(struct apple_smc *smc, smc_key key,
+	int *p, int scale)

[Severity: Low]
Can macsmc_battery_acpi_read_f32_scaled() and the fixed-point decoders in macsmc_battery_acpi_read() be shared in macsmc-core instead of duplicating macsmc_hwmon_read_f32_scaled() and the fixed-point decoding from drivers/hwmon/macsmc-hwmon.c?

Keeping separate copies has already led to drift between the two drivers, such as the unsigned BIT() divisor issue below.

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_capacity_level(struct macsmc_power *power) · line 443

Quoted 9 lines
drivers/power/supply/macsmc-power.c +9 -0
+static int macsmc_battery_acpi_read(struct apple_smc *smc, smc_key key, int *val,
+	int scale)
+{
+	int ret;
+	struct apple_smc_key_info info;
+
+	ret = apple_smc_get_key_info(smc, key, &info);
+	if (ret)
+		return ret;

[Severity: Low]
Can the key info for these SMC keys be cached at probe time instead of calling apple_smc_get_key_info() on every property read?

apple_smc_get_key_info() is uncached and performs a full synchronous SMC transaction under smc->mutex, doubling the hardware IPC round-trips for each runtime read of B0AP, TB0T, and the AC telemetry keys.

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_capacity_level(struct macsmc_power *power) · line 468

Quoted 11 lines
drivers/power/supply/macsmc-power.c +11 -0
+	/* 3.13 fixed point decimal */
+	case __SMC_KEY('f', 'p', '3', 'd'): {
+		u16 fp3d;
+
+		ret = apple_smc_read_u16(smc, key, &fp3d);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13));
+		break;
+	}

[Severity: Low]
Will sparse warn about passing host-endian u16 and s16 values to be16_to_cpu() across all nine fixed-point cases in macsmc_battery_acpi_read()?

be16_to_cpu() expects __be16, whereas fp3d through sp87 are declared as plain u16 or s16.

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_capacity_level(struct macsmc_power *power) · line 504

Quoted 11 lines
drivers/power/supply/macsmc-power.c +11 -0
+	/* 3.12 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '3', 'c'): {
+		s16 sp3c;
+
+		ret = apple_smc_read_s16(smc, key, &sp3c);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp3c), scale, BIT(12));
+		break;
+	}

[Severity: Medium]
Can BIT(12) as the divisor in mult_frac() trigger signed 64-bit overflow when sp3c is negative?

BIT(12) has type unsigned long (64-bit on x86_64), so inside mult_frac() the division x_ / d_ converts negative s64 x_ to unsigned 64-bit and produces a quotient around 4.5e15. Multiplying that by scale = 1000000 in s64 overflows S64_MAX (triggering UBSAN under CONFIG_UBSAN_SIGNED_WRAP). The remaining signed fixed-point cases (sp4b through sp87) similarly pass unsigned BIT() divisors to mult_frac(), forcing unsigned division on negative s16 values. Using a signed divisor such as (1 << 12) avoids the unsigned promotion.

[ ... ]

Quoted 1 line
drivers/power/supply/macsmc-power.c +0 -0
@@ -418,27 +734,64 @@ static int macsmc_battery_get_property(struct power_supply *psy,

[ ... ]

Quoted 18 lines
drivers/power/supply/macsmc-power.c +11 -2
 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16);
-		val->intval = vu16 * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = vu16 * swab16(voltage);
+		} else
+			val->intval = vu16 * power->nominal_voltage_mv;
 		break;
 	case POWER_SUPPLY_PROP_ENERGY_FULL:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0FC), &vu16);
-		val->intval = vu16 * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_property(struct power_supply *psy, · line 774

Quoted 10 lines
drivers/power/supply/macsmc-power.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = swab16(vu16) * swab16(voltage);
+		} else
+			val->intval = vu16 * power->nominal_voltage_mv;
 		break;

[Severity: Medium]
Should ENERGY_FULL_DESIGN and ENERGY_FULL on ACPI Macs use power->nominal_voltage_mv instead of the live battery voltage B0AV?

Multiplying B0DC and B0FC by live B0AV makes the reported design and full energy fluctuate with state of charge and load sag, distorting userspace battery wear calculations. macsmc_power_probe() already sets power->num_cells on ACPI Macs, so power->nominal_voltage_mv can be computed the same way as on Apple Silicon.

Quoted 4 lines
drivers/power/supply/macsmc-power.c +1 -1
 	case POWER_SUPPLY_PROP_ENERGY_NOW:
 		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16);
-		val->intval = macsmc_swap_b0rm(power, vu16) * power->nominal_voltage_mv;
+		if (power->smc->is_acpi) {

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_property(struct power_supply *psy, · line 787

Quoted 10 lines
drivers/power/supply/macsmc-power.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			u16 voltage;
+
+			ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage);
+			val->intval = swab16(vu16) * swab16(voltage);
+		} else
+			val->intval = macsmc_swap_b0rm(power, vu16) * power->nominal_voltage_mv;
 		break;

[Severity: Low]
Does this read an uninitialized stack variable voltage if reading B0AV fails?

In all three ENERGY_* cases, ret is not checked after apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage), so an SMC I/O error leaves voltage uninitialized when passed to swab16().

[ ... ]

Quoted 1 line
drivers/power/supply/macsmc-power.c +0 -0
@@ -667,18 +1093,23 @@ static int macsmc_power_probe(struct platform_device *pdev)

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_power_probe(struct platform_device *pdev) · line 1113

Quoted 3 lines
drivers/power/supply/macsmc-power.c +1 -1
-	if (apple_smc_key_exists(smc, SMC_KEY(CHIS)))
+	if (apple_smc_key_exists(smc, SMC_KEY(CHIS)) || smc->is_acpi)
 		has_ac_adapter = true;

[Severity: Medium]
Should smc->is_acpi be checked before apple_smc_key_exists(smc, SMC_KEY(CHIS))?

With CHIS on the left side of ||, every PIO-mode Intel Mac probes the Apple-Silicon-only CHIS key and discards the result. When CHIS is missing, apple_smc_acpi_port_wait() stalls for 1-2 seconds while holding smc->mutex and clears smc->interrupt_enabled, forcing all subsequent SMC commands to fall back to 1 ms polling.

[ ... ]

Quoted 1 line
drivers/power/supply/macsmc-power.c +0 -0
@@ -694,118 +1125,161 @@ static int macsmc_power_probe(struct platform_device *pdev)

[ ... ]

drivers/power/supply/macsmc-power.c

Quoted 2 lines
drivers/power/supply/macsmc-power.c +2 -0
+			if (apple_smc_key_exists(smc, SMC_KEY(B0TE)))
+				props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW;

[Severity: Medium]
Is POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW missing a swab16() conversion for ACPI Macs?

macsmc_battery_get_property() was updated to byte-swap B0TF for TIME_TO_FULL_NOW, but B0TE was left unchanged:

drivers/power/supply/macsmc-power.c:macsmc_battery_get_property() {
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
		ret = apple_smc_read_u16(power->smc, SMC_KEY(B0TE), &vu16);
		val->intval = vu16 == 0xffff ? 0 : vu16 * 60;
		break;
}

On Intel ACPI Macs where B0TE is big-endian, reading time_to_empty_now returns corrupted byte-swapped values whenever B0TE is not 0xffff.

[ ... ]

Quoted 1 line
drivers/power/supply/macsmc-power.c +0 -0
@@ -829,16 +1303,27 @@ static int macsmc_power_probe(struct platform_device *pdev)

[ ... ]

drivers/power/supply/macsmc-power.c static int macsmc_power_probe(struct platform_device *pdev) · line 1313

Quoted 8 lines
drivers/power/supply/macsmc-power.c +8 -0
+		if (smc->is_acpi) {
+			props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+			props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+			if (apple_smc_key_exists(power->smc, SMC_KEY(PD0R)))
+				power->has_pd0r = true;
+			/* MacBookPro5,x and 7,x dosent have VD0R key or any equivalent */
+			if (!power->has_battery || apple_smc_key_exists(power->smc, SMC_KEY(VD0R)))
+				props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;

[Severity: Medium]
Can registering CURRENT_NOW, POWER_NOW, and desktop VOLTAGE_NOW without checking for their backing SMC keys break macsmc-ac uevents?

If ID0R/ID2R, PDTR/PD2R, or VD2R is absent (for example on a laptop with its battery disconnected where power->has_battery is false and macsmc_ac_get_property() queries desktop keys ID2R, PD2R, and VD2R), apple_smc_get_key_info() returns -EIO. Because add_prop_uevent() in drivers/power/supply/power_supply_sysfs.c only ignores -ENODEV, -ENODATA, and -EINVAL, returning -EIO fails power_supply_uevent() for the entire macsmc-ac device.

[ ... ]

Quoted 14 lines
drivers/power/supply/macsmc-power.c +7 -2
@@ -858,9 +1343,22 @@ static int macsmc_power_probe(struct platform_device *pdev)
 	if (!power->batt && !power->ac)
 		return -ENODEV;
 
-	power->nb.notifier_call = macsmc_power_event;
-	blocking_notifier_chain_register(smc->event_handlers, &power->nb);
+	if (!smc->is_acpi) {
+		power->nb.notifier_call = macsmc_power_event;
+		blocking_notifier_chain_register(smc->event_handlers, &power->nb);
+	} else if (power->has_battery) {
+		power->adapter = acpi_dev_get_first_match_dev("ACPI0003", NULL, -1);
+		if (!power->adapter)
+			return -ENODEV;
 

drivers/power/supply/macsmc-power.c static int macsmc_power_probe(struct platform_device *pdev) · line 1363

Quoted 10 lines
drivers/power/supply/macsmc-power.c +8 -0
+		ret = acpi_dev_install_notify_handler(power->adapter, ACPI_DEVICE_NOTIFY,
+					macsmc_power_acpi_notify, power);
+		if (ret) {
+			acpi_dev_put(power->adapter);
+			power->adapter = NULL;
+			return ret;
+		}
+	}
 	return 0;
 }

[Severity: High]
Can power->adapter be dereferenced while NULL during macsmc_power_probe()?

Both power->batt and power->ac are registered with devm_power_supply_register() before power->adapter is acquired. As soon as device_add() emits KOBJ_ADD, userspace (such as udevd or upowerd) or deferred_register_work can read status or online before power->adapter is assigned:

CPU0 CPU1
macsmc_power_probe()
devm_power_supply_register(batt)
sysfs read: macsmc-battery/status
macsmc_battery_acpi_get_status()
power->adapter->handle <-- NULL deref
power->adapter = acpi_dev_get_first_match_dev(...)

The same NULL pointer dereference happens if acpi_dev_get_first_match_dev() or acpi_dev_install_notify_handler() fails and leaves power->adapter NULL until devres unregisters the power supplies. Acquiring power->adapter with a devm cleanup action before calling devm_power_supply_register() avoids both windows.

drivers/power/supply/macsmc-power.c static void macsmc_power_remove(struct platform_device *pdev) · line 1376

Quoted 13 lines
drivers/power/supply/macsmc-power.c +7 -1
@@ -868,7 +1366,13 @@ static void macsmc_power_remove(struct platform_device *pdev)
 {
 	struct macsmc_power *power = dev_get_drvdata(&pdev->dev);
 
-	blocking_notifier_chain_unregister(power->smc->event_handlers, &power->nb);
+	if (!power->smc->is_acpi)
+		blocking_notifier_chain_unregister(power->smc->event_handlers, &power->nb);
+	else if (power->has_battery) {
+		acpi_dev_remove_notify_handler(power->adapter, ACPI_DEVICE_NOTIFY,
+				macsmc_power_acpi_notify);
+		acpi_dev_put(power->adapter);
+	}
 }

[Severity: Medium]
Does calling acpi_dev_put(power->adapter) in macsmc_power_remove() drop the reference before power->batt and power->ac are unregistered?

__device_release_driver() runs macsmc_power_remove() before devres_release_all() unregisters devm-managed power supplies, leaving a window where sysfs reads still dereference power->adapter->handle without a reference:

CPU0 CPU1
__device_release_driver()
macsmc_power_remove()

    acpi_dev_put(power->adapter)
                                sysfs read: macsmc-battery/status
                                  macsmc_battery_acpi_get_status()
                                    power->adapter->handle <-- use-after-put

devres_release_all()
power_supply_unregister(...)

Releasing power->adapter via a devm_add_action_or_reset() callback registered before devm_power_supply_register() guarantees both power supplies are unregistered before acpi_dev_put().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#35 lore raw

Re: [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power

In reply to Atharva Tiwari
Quoted 9 lines · Atharva Tiwari

Make macsmc-power work on Intel Macs, as a replacement for acpi-sbs. It exposes a lot more sensors than acpi-sbs, such as voltage, current, power, temperature, charge and health info, and also the adapter state.

[...]

+#include <linux/acpi.h>

I do not see a dependency on CONFIG_ACPI. And it should not have one anyway, AS does not need ACPI.

Quoted 14 lines
drivers/power/supply/macsmc-power.c +14 -0
+static int macsmc_battery_acpi_read(struct apple_smc *smc, smc_key key, int *val,
+ int scale)
+{
+ int ret;
+ struct apple_smc_key_info info;
+
+ ret = apple_smc_get_key_info(smc, key, &info);
+ if (ret)
+ return ret;
+
+ switch (info.type_code) {
+ /* 32-bit IEEE 754 float */
+ case __SMC_KEY('f', 'l', 't', ' '): {
+ int flt_ = 0;

drivers/power/supply/macsmc-power.c static int macsmc_battery_get_capacity_level(struct macsmc_power *power) · line 458

Quoted 10 lines
drivers/power/supply/macsmc-power.c +10 -0
+
+ ret = macsmc_battery_acpi_read_f32_scaled(smc, key, &flt_, scale);
+ if (ret)
+ return ret;
+
+ *val = flt_;
+ break;
+ }
+
+ /* 3.13 fixed point decimal */

Is Apple actually using every single one of those formats?

Quoted 8 lines

case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16);
- val->intval = vu16 * 1000;
+ if (power->smc->is_acpi)
+ val->intval = swab16(vu16) * 1000;
+ else
+ val->intval = vu16 * 1000;
break;

This pattern happens way too often to leave open-coded.

Quoted 26 lines

case POWER_SUPPLY_PROP_POWER_NOW:
- ret = apple_smc_read_s32(power->smc, SMC_KEY(B0AP), &vs32);
- val->intval = vs32 * 1000;
+ if (power->smc->is_acpi) {
+ if (power->has_b0ap) {
+ ret = macsmc_battery_acpi_read(power->smc, SMC_KEY(B0AP),
+ &val->intval, 1000000);
+ if (ret < 0)
+ return ret;

drivers/power/supply/macsmc-power.c +17 -0
+
+ val->intval = -val->intval;
+ } else {
+ ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16);
+ if (ret < 0)
+ return ret;
+
+ ret = apple_smc_read_s16(power->smc, SMC_KEY(B0AC), &vs16);
+ if (ret < 0)
+ return ret;
+
+ val->intval = (s16)swab16(vs16) * swab16(vu16);
+ }
+ } else {
+ ret = apple_smc_read_s32(power->smc, SMC_KEY(B0AP), &vs32);
+ val->intval = vs32 * 1000;
+ }
Quoted 1 line

break;

Please put this somewhere else and not inline

Quoted 12 lines

switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
- ret = apple_smc_read_u32(power->smc, SMC_KEY(CHIS), &vu32);
- val->intval = !!vu32;
+ if (power->smc->is_acpi) {
+ if (power->has_battery) {
+ acpi_status status;
+ unsigned long long adapter_status;

drivers/power/supply/macsmc-power.c +4 -0
+
+ status = acpi_evaluate_integer(power->adapter->handle,
+ "_PSR", NULL, &adapter_status);
+ if (ACPI_FAILURE(status))

drivers/power/supply/macsmc-power.c static int macsmc_ac_get_property(struct power_supply *psy, · line 911

Quoted 10 lines
drivers/power/supply/macsmc-power.c +10 -0
+ return -EIO;
+
+ val->intval = !!adapter_status;
+ } else
+ val->intval = 1;
+ } else {
+ ret = apple_smc_read_u32(power->smc, SMC_KEY(CHIS), &vu32);
+ val->intval = !!vu32;
+ }
+ break;

Same

Quoted 15 lines

+ if (smc->is_acpi) {
+ props[nprops++] = POWER_SUPPLY_PROP_STATUS;
+ props[nprops++] = POWER_SUPPLY_PROP_PRESENT;
+ props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_CAPACITY;
+ props[nprops++] = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
+ props[nprops++] = POWER_SUPPLY_PROP_TEMP;
+ props[nprops++] = POWER_SUPPLY_PROP_CYCLE_COUNT;
+ props[nprops++] = POWER_SUPPLY_PROP_HEALTH;
+ props[nprops++] = POWER_SUPPLY_PROP_SCOPE;

[...]

Quoted 11 lines

+ props[nprops++] = POWER_SUPPLY_PROP_STATUS;
+ props[nprops++] = POWER_SUPPLY_PROP_PRESENT;
+ props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_CURRENT_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_POWER_NOW;
+ props[nprops++] = POWER_SUPPLY_PROP_CAPACITY;
+ props[nprops++] = POWER_SUPPLY_PROP_CAPACITY_LEVEL;
+ props[nprops++] = POWER_SUPPLY_PROP_TEMP;
+ props[nprops++] = POWER_SUPPLY_PROP_CYCLE_COUNT;
+ props[nprops++] = POWER_SUPPLY_PROP_HEALTH;
+ props[nprops++] = POWER_SUPPLY_PROP_SCOPE;

They are the same picture

#36 lore raw

Re: [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power

Quoted 2 lines

I do not see a dependency on CONFIG_ACPI. And it should not have one anyway, AS does not need ACPI.

I agree that AS does not require ACPI for anything but Intel macs require it for adapter interrupts so i will just guard it using CONFIG_MACSMC_ACPI.

Quoted 1 line

Is Apple actually using every single one of those formats?

Yes, most of these formats are used by very old Intel Macs (pre-2015).

#37 lore raw

PATCH 11/14 acpi: disable battery and adapter for x86 apple machines

In reply to Atharva Tiwari

When macsmc-power is enabled on an Intel Mac, skip acpi-sbs and the ACPI battery and adapter drivers, since macsmc-power now provides them. This avoids duplicate devices.

---
 drivers/acpi/sbs.c       | 5 ++++-
 drivers/acpi/x86/utils.c | 5 +++++
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/acpi/sbs.c +4 -1
index f10bbf13c..368bcae72 100644
@@ -634,6 +634,10 @@ static int acpi_sbs_probe(struct platform_device *pdev)
 	int result = 0;
 	int id;
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return -ENODEV;
+
 	device = ACPI_COMPANION(&pdev->dev);
 	if (!device)
 		return -ENODEV;
@@ -664,7 +668,6 @@ static int acpi_sbs_probe(struct platform_device *pdev)
 					acpi_battery_add(sbs, id);
 		}
 	}
-
 	if (!sbs->manager_present)
 		acpi_battery_add(sbs, 0);
 
drivers/acpi/x86/utils.c +5 -0
index 418951639..2c54c0f67 100644
@@ -14,6 +14,7 @@
 #include <linux/dmi.h>
 #include <linux/pci.h>
 #include <linux/platform_device.h>
+#include <linux/platform_data/x86/apple.h>
 #include <asm/cpu_device_id.h>
 #include <asm/intel-family.h>
 #include "../internal.h"
@@ -608,6 +609,10 @@ bool acpi_quirk_skip_acpi_ac_and_battery(void)
 	long quirks = 0;
 	int i;
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return true;
+
 	dmi_id = dmi_first_match(acpi_quirk_skip_dmi_ids);
 	if (dmi_id)
 		quirks = (unsigned long)dmi_id->driver_data;
Signature · 1 line
2.55.0
#38 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 3 lines · Atharva Tiwari

When macsmc-power is enabled on an Intel Mac, skip acpi-sbs and the ACPI battery and adapter drivers, since macsmc-power now provides them. This avoids duplicate devices.

...

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return -ENODEV;

This is a hack. And it's even indented wrongly.

...

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 672

Quoted 7 lines
drivers/acpi/sbs.c +0 -1
@@ -664,7 +668,6 @@ static int acpi_sbs_probe(struct platform_device *pdev)
 					acpi_battery_add(sbs, id);
 		}
 	}
-
 	if (!sbs->manager_present)
 		acpi_battery_add(sbs, 0);

What the heck is this?

...

Please, read what you is about to send,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#39 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Atharva Tiwari

When macsmc-power is enabled on an Intel Mac, skip acpi-sbs and the ACPI battery and adapter drivers, since macsmc-power now provides them. This avoids duplicate devices.

Quoted 8 lines

...

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return -ENODEV;

This is a hack. And it's even indented wrongly.

Do you have a better idea to avoid using duplicate devices?

Quoted 17 lines

...

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 672

Quoted 7 lines
drivers/acpi/sbs.c +0 -1
@@ -664,7 +668,6 @@ static int acpi_sbs_probe(struct platform_device *pdev)
 					acpi_battery_add(sbs, id);
 		}
 	}
-
 	if (!sbs->manager_present)
 		acpi_battery_add(sbs, 0);

What the heck is this?

...

Please, read what you is about to send,

#40 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 2 lines · Aditya Garg
Quoted 1 line · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:51AM -0500, Atharva Tiwari wrote:

...

Quoted 7 lines
Quoted 5 lines

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return -ENODEV;

This is a hack. And it's even indented wrongly.

Do you have a better idea to avoid using duplicate devices?

The commit message is poorly written to advice anything.
It's unclear why we have duplicate drivers for the same ACPI HID to begin with. Can MAC case be enumerated differently? When the SMS driver is not compiled this one is still in use, why is it not a problem? So, there are many unaswered questions...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#41 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 16 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 2 lines · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:51AM -0500, Atharva Tiwari wrote:

...

Quoted 6 lines
Quoted 5 lines

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+    if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+            IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+        return -ENODEV;

This is a hack. And it's even indented wrongly.

Quoted 7 lines
Quoted 1 line

Do you have a better idea to avoid using duplicate devices?

The commit message is poorly written to advice anything.
It's unclear why we have duplicate drivers for the same ACPI HID to begin with. Can MAC case be enumerated differently? When the SMS driver is not compiled this one is still in use, why is it not a problem? So, there are many unaswered questions...

On Macs you can get battery and adapter stats using both ACPI and SMC. The SMC reported values are more reliable so we developed a driver for that. But if we use that driver, Linux thinks there are 2 batteries, one from ACPI and other from SMC. So we need to disable it from ACPI.

One way is to simply check is the device is a Mac and disable ACPI permanently. Other way is check if SMC is compiled or not. If not, fall back to ACPI so that we don't loose battery.

I understand the issue with commit messages. The author's native language isn't English so he is unable to communicate properly and thus is using AI to write the messages. I'll try and help him here.

#42 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 24 lines · Aditya Garg
Quoted 16 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 2 lines · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:51AM -0500, Atharva Tiwari wrote:

...

Quoted 6 lines
Quoted 5 lines

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+    if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+            IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+        return -ENODEV;

This is a hack. And it's even indented wrongly.

Quoted 7 lines
Quoted 1 line

Do you have a better idea to avoid using duplicate devices?

The commit message is poorly written to advice anything.
It's unclear why we have duplicate drivers for the same ACPI HID to begin with. Can MAC case be enumerated differently? When the SMS driver is not compiled this one is still in use, why is it not a problem? So, there are many unaswered questions...

Quoted 8 lines

On Macs you can get battery and adapter stats using both ACPI and SMC. The SMC reported values are more reliable so we developed a driver for that. But if we use that driver, Linux thinks there are 2 batteries, one from ACPI and other from SMC. So we need to disable it from ACPI.

One way is to simply check is the device is a Mac and disable ACPI permanently. Other way is check if SMC is compiled or not. If not, fall back to ACPI so that we don't loose battery.

Should we just disable ACPI0002 enumeration via a quirk in somewhere drivers/acpi/ ? Like acpi_create_platform_device() blocks the devices in the forbidden_id_list.

Quoted 3 lines

I understand the issue with commit messages. The author's native language isn't English so he is unable to communicate properly and thus is using AI to write the messages. I'll try and help him here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#43 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Aditya Garg
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Aditya Garg
Quoted 2 lines · Andy Shevchenko

On Fri, Dec 04, 2026 at 08:12:51AM -0500, Atharva Tiwari wrote:

...

Quoted 19 lines
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 639

Quoted 3 lines
drivers/acpi/sbs.c +3 -0
+    if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+            IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+        return -ENODEV;

This is a hack. And it's even indented wrongly.

Quoted 7 lines
Quoted 1 line

Do you have a better idea to avoid using duplicate devices?

The commit message is poorly written to advice anything.
It's unclear why we have duplicate drivers for the same ACPI HID to begin with. Can MAC case be enumerated differently? When the SMS driver is not compiled this one is still in use, why is it not a problem? So, there are many unaswered questions...

On Macs you can get battery and adapter stats using both ACPI and SMC. The SMC reported values are more reliable so we developed a driver for that. But if we use that driver, Linux thinks there are 2 batteries, one from ACPI and other from SMC. So we need to disable it from ACPI.

Quoted 6 lines
Quoted 3 lines

One way is to simply check is the device is a Mac and disable ACPI permanently. Other way is check if SMC is compiled or not. If not, fall back to ACPI so that we don't loose battery.

Should we just disable ACPI0002 enumeration via a quirk in somewhere drivers/acpi/ ? Like acpi_create_platform_device() blocks the devices in the forbidden_id_list.

Since we have already acpi_quirk_skip_acpi_ac_and_battery() quirk, perhaps the proper place is to have it only in utils.c and apply in sbs.c via a call to acpi_quirk_skip_acpi_ac_and_battery(). Would that work?

Quoted 3 lines
Quoted 3 lines

I understand the issue with commit messages. The author's native language isn't English so he is unable to communicate properly and thus is using AI to write the messages. I'll try and help him here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#44 lore raw

Re: [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines

In reply to Atharva Tiwari
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] acpi: unconditional skip leaves Intel Macs without battery or AC drivers - [High] power: supply…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] acpi: unconditional skip leaves Intel Macs without battery or AC drivers
- [High] power: supply: NULL power->adapter deref in macsmc_power_probe()
- [Low] acpi: Apple skip check bypasses DMI overrides and is duplicated in sbs.c
--

commit 1f23bca9ef468a05d91753f57047443718511acf
Author: Atharva Tiwari <[email protected]>

acpi: disable battery and adapter for x86 apple machines

This commit skips probing acpi-sbs and disables the ACPI AC and battery drivers on x86 Apple machines when CONFIG_MACSMC_ACPI and CONFIG_MACSMC_POWER are enabled.

Quoted 5 lines
drivers/acpi/sbs.c +0 -0
index f10bbf13c242f..368bcae7223f3 100644

drivers/acpi/sbs.c static int acpi_sbs_probe(struct platform_device *pdev) · line 643

Quoted 10 lines
drivers/acpi/sbs.c +4 -0
@@ -634,6 +634,10 @@ static int acpi_sbs_probe(struct platform_device *pdev)
 	int result = 0;
 	int id;
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return -ENODEV;
+
 	device = ACPI_COMPANION(&pdev->dev);
 	if (!device)
 		return -ENODEV;

[Severity: High]
Can this leave Intel Macs without any battery or AC adapter drivers?

Both acpi_sbs_probe() here and acpi_quirk_skip_acpi_ac_and_battery() in drivers/acpi/x86/utils.c disable the ACPI drivers using compile-time IS_ENABLED() checks and x86_apple_machine, without verifying that macsmc-acpi and macsmc-power actually bind or that APP0001 and ACPI0003 are present.

Because CONFIG_MACSMC_ACPI, CONFIG_MACSMC_POWER, and CONFIG_SENSORS_APPLESMC are tristate options with no Kconfig exclusion, a distribution or allmodconfig kernel can load applesmc first. applesmc_init() then claims the SMC I/O ports with request_region(), causing apple_smc_acpi_probe() to fail with -EBUSY so macsmc-power is never instantiated.

Similarly, macsmc_power_probe() fails with -ENODEV on battery-equipped Intel Macs that lack ACPI0003. Since acpi_ac_init() and acpi_battery_init() only check acpi_quirk_skip_acpi_ac_and_battery() once at init and return -ENODEV, the system ends up with no battery or AC power_supply devices.

Quoted 4 lines
drivers/acpi/x86/utils.c +0 -0
index 418951639f511..2c54c0f679bfd 100644

[ ... ]

drivers/acpi/x86/utils.c bool acpi_quirk_skip_acpi_ac_and_battery(void) · line 618

Quoted 11 lines
drivers/acpi/x86/utils.c +4 -0
@@ -608,6 +609,10 @@ bool acpi_quirk_skip_acpi_ac_and_battery(void)
 	long quirks = 0;
 	int i;
 
+	if (IS_ENABLED(CONFIG_MACSMC_ACPI) &&
+			IS_ENABLED(CONFIG_MACSMC_POWER) && x86_apple_machine)
+		return true;
+
 	dmi_id = dmi_first_match(acpi_quirk_skip_dmi_ids);
 	if (dmi_id)
 		quirks = (unsigned long)dmi_id->driver_data;

[Severity: Low]
Does returning before dmi_first_match(acpi_quirk_skip_dmi_ids) bypass the ACPI_QUIRK_USE_ACPI_AC_AND_BATTERY override mechanism?

Unlike the existing PMIC checks further down in this function, this early return would prevent any future Apple DMI entry from opting back into the ACPI AC and battery drivers.

Also, acpi_sbs_probe() duplicates this check instead of reusing acpi_quirk_skip_acpi_ac_and_battery(), and the drivers/acpi/sbs.c diff drops an unrelated blank line.

[Severity: High]
With this commit relying on macsmc-power to replace the ACPI drivers, can macsmc_power_probe() from commit "power/supply: add support for x86 macs in macsmc-power" dereference a NULL power->adapter during probe?

macsmc_power_probe() registers power->batt and power->ac with devm_power_supply_register() before assigning power->adapter via acpi_dev_get_first_match_dev("ACPI0003", NULL, -1):

CPU0 CPU1
macsmc_power_probe()
devm_power_supply_register()
sysfs read (status)
macsmc_battery_acpi_get_status()
power->adapter->handle <-- NULL deref
power->adapter = ...

Any read of POWER_SUPPLY_PROP_STATUS in macsmc_battery_acpi_get_status() or POWER_SUPPLY_PROP_ONLINE in macsmc_ac_get_property() during this window, or during devm teardown if the ACPI0003 lookup or notify installation fails, hits a NULL pointer dereference and kernel oops. Looking up power->adapter before registering the power_supply devices would avoid this window.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#45 lore raw

PATCH 12/14 hwmon/macsmc: add more types

In reply to Atharva Tiwari

SMC keys come in many data types, and Intel Macs use a lot that macsmc-hwmon couldn't read, such as the signed fixed point types like sp1e.

Add support for the common integer and fixed point types, and allow writing fan speeds.

---
 drivers/hwmon/macsmc-hwmon.c | 310 +++++++++++++++++++++++++++++++++++
 1 file changed, 310 insertions(+)
drivers/hwmon/macsmc-hwmon.c +310 -0
index 1500ec2cc..14b68cf8c 100644
@@ -221,6 +221,313 @@ static int macsmc_hwmon_read_key(struct apple_smc *smc,
 			*val = (long)ioft;
 		break;
 	}
+	/* unsigned 8-bit integer */
+	case __SMC_KEY('u', 'i', '8', ' '): {
+		u8 ui8 = 0;
+
+		ret = apple_smc_read_u8(smc, sensor->macsmc_key, &ui8);
+		if (ret)
+			return ret;
+
+		*val = ui8 * scale;
+		break;
+	}
+	/* signed 8-bit integer */
+	case __SMC_KEY('s', 'i', '8', ' '): {
+		s8 si8 = 0;
+
+		ret = apple_smc_read_s8(smc, sensor->macsmc_key, &si8);
+		if (ret)
+			return ret;
+
+		*val = si8 * scale;
+		break;
+	}
+	/* unsigned 16-bit integer */
+	case __SMC_KEY('u', 'i', '1', '6'): {
+		u16 ui16 = 0;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &ui16);
+		if (ret)
+			return ret;
+
+		*val = (s64)ui16 * scale;
+		break;
+	}
+	/* signed 16-bit integer */
+	case __SMC_KEY('s', 'i', '1', '6'): {
+		s16 si16 = 0;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &si16);
+		if (ret)
+			return ret;
+
+		*val = (s64)si16 * scale;
+		break;
+	}
+	/* unsigned 32-bit integer */
+	case __SMC_KEY('u', 'i', '3', '2'): {
+		u32 ui32 = 0;
+
+		ret = apple_smc_read_u32(smc, sensor->macsmc_key, &ui32);
+		if (ret)
+			return ret;
+
+		*val = (s64)ui32 * scale;
+		break;
+	}
+	/* signed 32-bit integer */
+	case __SMC_KEY('s', 'i', '3', '2'): {
+		s32 si32;
+
+		ret = apple_smc_read_s32(smc, sensor->macsmc_key, &si32);
+		if (ret)
+			return ret;
+
+		*val = (s64)si32 * scale;
+		break;
+	}
+	/* 2.14 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '2', 'e'): {
+		u16 fp2e;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp2e);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp2e), scale, BIT(14));
+		break;
+	}
+
+	/* 3.13 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '3', 'd'): {
+		u16 fp3d;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp3d);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13));
+		break;
+	}
+
+	/* 4.12 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '4', 'c'): {
+		u16 fp4c;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp4c);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp4c), scale, BIT(12));
+		break;
+	}
+
+	/* 5.11 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '5', 'b'): {
+		u16 fp5b;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp5b);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp5b), scale, BIT(11));
+		break;
+	}
+
+	/* 6.10 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '6', 'a'): {
+		u16 fp6a;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp6a);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp6a), scale, BIT(10));
+		break;
+	}
+
+	/* 8.8 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '8', '8'): {
+		u16 fp88;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp88);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp88), scale, BIT(8));
+		break;
+	}
+	/* 14.2 fixed point decimal */
+	case __SMC_KEY('f', 'p', 'e', '2'): {
+		/*
+		 * fpe2 is only used for fan stats and fan control
+		 * so precision is not needed
+		 */
+		u16 fpe2;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fpe2);
+		if (ret)
+			return ret;
+
+		*val = (be16_to_cpu(fpe2) >> 2);
+		break;
+	}
+	/* 1.14 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '1', 'e'): {
+		s16 sp1e;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp1e);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp1e), scale, 1 << 14);
+		break;
+	}
+
+	/* 2.13 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '2', 'd'): {
+		s16 sp2d;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp2d);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp2d), scale, 1 << 13);
+		break;
+	}
+
+	/* 3.12 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '3', 'c'): {
+		s16 sp3c;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp3c);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp3c), scale, 1 << 12);
+		break;
+	}
+
+	/* 4.11 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '4', 'b'): {
+		s16 sp4b;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp4b);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp4b), scale, 1 << 11);
+		break;
+	}
+
+	/* 5.10 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '5', 'a'): {
+		s16 sp5a;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp5a);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp5a), scale, 1 << 10);
+		break;
+	}
+
+	/* 6.9 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '6', '9'): {
+		s16 sp69;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp69);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp69), scale, 1 << 9);
+		break;
+	}
+
+	/* 7.8 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '7', '8'): {
+		s16 sp78;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp78);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp78), scale, 1 << 8);
+		break;
+	}
+
+	/* 8.7 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '8', '7'): {
+		s16 sp87;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp87);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp87), scale, 1 << 7);
+		break;
+	}
+
+	/* 9.6 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', '9', '6'): {
+		s16 sp96;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp96);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(sp96), scale, 1 << 6);
+		break;
+	}
+
+	/* 10.5 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', 'a', '5'): {
+		s16 spa5;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &spa5);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(spa5), scale, 1 << 5);
+		break;
+	}
+
+	/* 12.3 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', 'c', '3'): {
+		s16 spc3;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &spc3);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(spc3), scale, 1 << 3);
+		break;
+	}
+
+	/* 13.2 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', 'd', '2'): {
+		s16 spd2;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &spd2);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)(s16)be16_to_cpu(spd2), scale, 1 << 2);
+		break;
+	}
+
+	/* 15.0 signed fixed point decimal */
+	case __SMC_KEY('s', 'p', 'f', '0'): {
+		s16 spf0;
+
+		ret = apple_smc_read_s16(smc, sensor->macsmc_key, &spf0);
+		if (ret)
+			return ret;
+
+		*val = (s64)(s16)be16_to_cpu(spf0) * scale;
+		break;
+	}
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -263,6 +570,9 @@ static int macsmc_hwmon_write_key(struct apple_smc *smc,
 	/* unsigned 8-bit integer */
 	case __SMC_KEY('u', 'i', '8', ' '):
 		return apple_smc_write_u8(smc, sensor->macsmc_key, val);
+	/* 14.2 fixed point decimal */
+	case __SMC_KEY('f', 'p', 'e', '2'):
+		return apple_smc_write_u16(smc, sensor->macsmc_key, cpu_to_be16(val << 2));
 	default:
 		return -EOPNOTSUPP;
 	}
Signature · 1 line
2.55.0
#46 lore raw

Re: [PATCH 12/14] hwmon/macsmc: add more types

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] hwmon: (macsmc) missing endian conversion in macsmc_hwmon_read_key() - [Medium] hwmon: (macsmc)…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] hwmon: (macsmc) missing endian conversion in macsmc_hwmon_read_key()
- [Medium] hwmon: (macsmc) unclamped s64 to long truncation in macsmc_hwmon_read_key()
- [Low] hwmon: macsmc: sparse __be16 type mismatches on SMC fixed-point I/O
- [Low] hwmon: macsmc: duplicate fixed-point decoding in macsmc_hwmon_read_key()
- [Low] hwmon: missing range check for fpe2 write in macsmc_hwmon_write_key()
--

commit fa0827d4a4896bab5a321d44d03d23f333c92b8e
Author: Atharva Tiwari <[email protected]>

hwmon/macsmc: add more types

This commit adds read support for common integer and fixed-point SMC key types in macsmc_hwmon_read_key() and write support for fpe2 fan speed keys in macsmc_hwmon_write_key().

Quoted 5 lines
drivers/hwmon/macsmc-hwmon.c +0 -0
index 1500ec2cc9f83..14b68cf8c283e 100644
@@ -221,6 +221,313 @@ static int macsmc_hwmon_read_key(struct apple_smc *smc,

[ ... ]

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_read_key(struct apple_smc *smc, · line 256

Quoted 11 lines
drivers/hwmon/macsmc-hwmon.c +11 -0
+	/* unsigned 16-bit integer */
+	case __SMC_KEY('u', 'i', '1', '6'): {
+		u16 ui16 = 0;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &ui16);
+		if (ret)
+			return ret;
+
+		*val = (s64)ui16 * scale;
+		break;
+	}

[Severity: High]
Do the ui16, si16, ui32, and si32 cases need endianness conversion?

The ACPI SMC backend copies raw big-endian firmware bytes without swapping, as macsmc_battery_get_property() accounts for when smc->is_acpi is true. Using the raw values directly returns byte-swapped readings and corrupted signs on Intel ACPI Macs.

Converting from big-endian when smc->is_acpi is true and from little-endian otherwise (sign-extending si16 and si32 afterward) would handle both Intel and Apple Silicon backends.

[ ... ]

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_read_key(struct apple_smc *smc, · line 278

Quoted 11 lines
drivers/hwmon/macsmc-hwmon.c +11 -0
+	/* unsigned 32-bit integer */
+	case __SMC_KEY('u', 'i', '3', '2'): {
+		u32 ui32 = 0;
+
+		ret = apple_smc_read_u32(smc, sensor->macsmc_key, &ui32);
+		if (ret)
+			return ret;
+
+		*val = (s64)ui32 * scale;
+		break;
+	}

[Severity: Medium]
Can assigning the scaled s64 product directly to long *val truncate on 32-bit kernels?

SENSORS_MACSMC_HWMON does not depend on 64BIT, and macsmc_hwmon_read() passes scale = 1000000 for power sensors. On 32-bit builds where long is 32 bits, scaled values for ui16, si16, ui32, si32, spc3, spd2, and spf0 can exceed [LONG_MIN, LONG_MAX] and silently wrap or invert sign.

Clamping with clamp_t(s64, ..., LONG_MIN, LONG_MAX) before assigning to *val would match the saturation done for flt and ioft above.

[ ... ]

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_read_key(struct apple_smc *smc, · line 300

Quoted 11 lines
drivers/hwmon/macsmc-hwmon.c +11 -0
+	/* 2.14 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '2', 'e'): {
+		u16 fp2e;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp2e);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp2e), scale, BIT(14));
+		break;
+	}

[Severity: Low]
Will sparse warn about __be16 type mismatches across the fixed-point cases?

All 20 new fp* and sp* cases pass host-endian u16 or s16 variables to be16_to_cpu(), and the fpe2 case in macsmc_hwmon_write_key() passes cpu_to_be16() to apple_smc_write_u16().

Using __be16 buffers with apple_smc_read()/apple_smc_write() or dedicated __be16 helpers would eliminate these sparse warnings.

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_read_key(struct apple_smc *smc, · line 312

Quoted 12 lines
drivers/hwmon/macsmc-hwmon.c +12 -0
+
+	/* 3.13 unsigned fixed point decimal */
+	case __SMC_KEY('f', 'p', '3', 'd'): {
+		u16 fp3d;
+
+		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp3d);
+		if (ret)
+			return ret;
+
+		*val = mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13));
+		break;
+	}

[Severity: Low]
Can the 20 fp* and sp* cases be collapsed into a parameterized helper instead of repeating the read and scaling boilerplate?

Aside from the repetition (and minor inconsistencies between BIT(n) and 1 << n divisors and variable initialization), similar fixed-point decoding also exists in macsmc_battery_acpi_read() in drivers/power/supply/macsmc-power.c and could potentially be shared.

[ ... ]

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_write_key(struct apple_smc *smc, · line 575

Quoted 7 lines
drivers/hwmon/macsmc-hwmon.c +3 -0
@@ -263,6 +570,9 @@ static int macsmc_hwmon_write_key(struct apple_smc *smc,
 	/* unsigned 8-bit integer */
 	case __SMC_KEY('u', 'i', '8', ' '):
 		return apple_smc_write_u8(smc, sensor->macsmc_key, val);
+	/* 14.2 fixed point decimal */
+	case __SMC_KEY('f', 'p', 'e', '2'):
+		return apple_smc_write_u16(smc, sensor->macsmc_key, cpu_to_be16(val << 2));

[Severity: Low]
Should this check that val is within [0, 0x3fff] before shifting and narrowing to 16 bits?

macsmc_hwmon_write_fan() only bounds val against fan->min and fan->max, which macsmc_hwmon_create_fan() parses independently without requiring them to be fpe2. If a negative value reaches this path, val << 2 is signed left-shift undefined behavior, and val >= 0x4000 silently wraps.

Returning -EINVAL when val < 0 || val >= 0x4000, matching applesmc_hwmon_write() in drivers/hwmon/applesmc.c, would prevent both.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
maintainer
#47 lore raw

Re: [PATCH 12/14] hwmon/macsmc: add more types

Quoted 5 lines · On 12/4/26 06:37, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] hwmon: (macsmc) missing endian conversion in macsmc_hwmon_read_key()
- [Medium] hwmon: (macsmc) unclamped s64 to long truncation in

Quoted 9 lines

macsmc_hwmon_read_key()
- [Low] hwmon: macsmc: sparse __be16 type mismatches on SMC fixed-point I/O
- [Low] hwmon: macsmc: duplicate fixed-point decoding in macsmc_hwmon_read_key()
- [Low] hwmon: missing range check for fpe2 write in macsmc_hwmon_write_key()
--

Too much for me to even look at. Please fix.

Guenter

#48 lore raw

PATCH 13/14 hwmon: add support for x86 macs in macsmc-hwmon

In reply to Atharva Tiwari

Make macsmc-hwmon work on Intel Macs, as a replacement for the hwmon part of applesmc.

Sensors now have readable names instead of raw keys like TA0P. It also adds power, current and voltage sensors that applesmc doesn't provide, and supports fan control on newer machines aswell.

---
 drivers/hwmon/Kconfig        |   5 +-
 drivers/hwmon/macsmc-hwmon.c | 842 ++++++++++++++++++++++++++++++++++-
 2 files changed, 827 insertions(+), 20 deletions(-)
drivers/hwmon/Kconfig +2 -3
index 91ca7031c..4d0bb87ff 100644
@@ -1244,13 +1244,12 @@ config SENSORS_LTQ_CPUTEMP
 	  sensor inside your CPU.
 
 config SENSORS_MACSMC_HWMON
-	tristate "Apple SMC (Apple Silicon)"
+	tristate "Apple SMC Hardware monitoring"
 	depends on MFD_MACSMC_CORE
 	help
 	  This driver enables hwmon support for current, power, temperature,
 	  and voltage sensors, as well as fan speed reporting and control
-	  on Apple Silicon devices. Say Y here if you have an Apple Silicon
-	  device.
+	  on Apple devices. Say Y here if you have an Apple device.
 
 	  This driver can also be built as a module. If so, the module will
 	  be called macsmc-hwmon.
drivers/hwmon/macsmc-hwmon.c +825 -17

969 lines not on this page · raw message

Signature · 1 line
2.55.0
#49 lore raw

Re: [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon

Thank you for your contribution! Sashiko AI review found 11 potential issues to consider: - [High] hwmon: macsmc: NULL pointer dereference in DT sensor label handling - [High] hwmon: macsmc…

Thank you for your contribution! Sashiko AI review found 11 potential issues to consider:
- [High] hwmon: macsmc: NULL pointer dereference in DT sensor label handling
- [High] hwmon: macsmc: redundant SMC probes cause multi-minute PIO probe stalls
- [Medium] hwmon: macsmc: uninitialized count read in macsmc_hwmon_populate_sensors_acpi()
- [Medium] hwmon: macsmc: heap OOB write in macsmc_hwmon_register_sensors_acpi()
- [Medium] hwmon: macsmc: invalid sensor keys and missing key validation on ACPI
- [Medium] hwmon: macsmc: unvalidated F%dID read and fatal optional fan key errors
- [Medium] hwmon: macsmc: missing big-endian conversion for integer keys on ACPI
- [Medium] hwmon: macsmc_hwmon_resume() skips FS! and target restore, uses legacy PM
- [Low] hwmon: macsmc: outdated docs, MODULE_DESCRIPTION, and whitespace changes
- [Low] hwmon: macsmc: u16/__be16 mismatch in macsmc_hwmon_write_old_fan_manual()
- [Low] hwmon: macsmc-hwmon uses deprecated legacy platform_driver.resume callback
--

commit cda7f94f03b9c07639e3fb001f94e54d739b4e68
Author: Atharva Tiwari <[email protected]>

hwmon: add support for x86 macs in macsmc-hwmon

This commit extends macsmc-hwmon to support Intel Macs over ACPI by adding static sensor descriptor tables, ACPI sensor and fan discovery, legacy FS! manual fan control, and a resume callback.

Quoted 4 lines
drivers/hwmon/macsmc-hwmon.c +0 -0
index 14b68cf8c283e..d531eb4122f89 100644

[ ... ]

drivers/hwmon/macsmc-hwmon.c MODULE_PARM_DESC(fan_control, · line 57

Quoted 8 lines
drivers/hwmon/macsmc-hwmon.c +1 -1
@@ -47,7 +52,7 @@ MODULE_PARM_DESC(fan_control,
 struct macsmc_hwmon_sensor {
 	struct apple_smc_key_info info;
 	smc_key macsmc_key;
-	char label[MAX_LABEL_LENGTH];
+	char *label;
 	u32 attrs;
 };

[Severity: High]
Does changing label to a char * pointer cause a NULL pointer dereference on Apple Silicon Device Tree systems?

macsmc_hwmon_populate_sensors() zero-allocates each sensor array with devm_kcalloc(), leaving sensor->label NULL. When a Device Tree sensor node has a label property, macsmc_hwmon_create_sensor() calls strscpy_pad(sensor->label, label, sizeof(sensor->label)), writing to NULL with a pointer size; when a node omits label, macsmc_hwmon_populate_sensors() dereferences NULL via if (*sensor->label).

Making label (and macsmc_sensor_desc.name) const char *, assigning the DT label pointer directly (or via devm_kstrdup_const()), and checking sensor->label && *sensor->label would avoid this.

[ ... ]

Quoted 12 lines
drivers/hwmon/macsmc-hwmon.c +8 -0
@@ -599,6 +605,25 @@ static int macsmc_hwmon_read_fan(struct macsmc_hwmon *hwmon, u32 attr, int chan,
 	}
 }
 
+static int macsmc_hwmon_write_old_fan_manual(struct macsmc_hwmon *hwmon, u8 val, int channel)
+{
+	int ret;
+	u16 buf;
+
+	ret = apple_smc_read_u16(hwmon->smc, __SMC_KEY('F', 'S', '!', ' '), &buf);
+	if (ret)
+		return ret;

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_read_fan(struct macsmc_hwmon *hwmon, u32 attr, int chan, · line 625

Quoted 10 lines
drivers/hwmon/macsmc-hwmon.c +10 -0
+
+	buf = be16_to_cpu(buf);
+
+	if (val)
+		buf |= BIT(channel);
+	else
+		buf &= ~BIT(channel);
+
+	return apple_smc_write_u16(hwmon->smc, __SMC_KEY('F', 'S', '!', ' '), cpu_to_be16(buf));
+}

[Severity: Low]
Will buf trigger sparse endianness warnings here? buf is declared as host-endian u16, passed directly to be16_to_cpu(), and then cpu_to_be16(buf) is passed to apple_smc_write_u16(), which takes a plain u16. Reading and writing a __be16 variable via apple_smc_read() and apple_smc_write() would keep endianness annotations consistent.

[ ... ]

Quoted 5 lines
drivers/hwmon/macsmc-hwmon.c +5 -0
+static int macsmc_hwmon_create_sensor_acpi(struct apple_smc *smc,
+		struct macsmc_hwmon_sensor *sensor, smc_key key, char *name, u32 attrs)
+{
+	int ret;
+

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_create_sensor(struct device *dev, struct apple_smc *smc, · line 1432

Quoted 10 lines
drivers/hwmon/macsmc-hwmon.c +10 -0
+	ret = apple_smc_get_key_info(smc, key, &sensor->info);
+	if (ret)
+		return ret;
+
+	sensor->macsmc_key = key;
+	sensor->label = name;
+	sensor->attrs = attrs;
+
+	return 0;
+}

[Severity: Medium]
Can this register unreadable keys, unsupported SMC types, or non-sensor keys as hwmon attributes and thermal zones?

macsmc_hwmon_create_sensor_acpi() never checks APPLE_SMC_READABLE or whether sensor->info.type_code is handled by macsmc_hwmon_read_key(), so keys with types such as flag, hex_, or ch8* fail every read with -EOPNOTSUPP or -EIO.

The static tables also include non-sensor entries such as audio scratchpads (MST1, MST2), power limits and throttles (MSTc, MSTg, PZ0T..PZ5T), and non-temperature keys (TCFC, TMBS, TH0e) in temp_sensors[], which are exposed as kernel thermal zones because HWMON_C_REGISTER_TZ is set.

[ ... ]

Quoted 27 lines
drivers/hwmon/macsmc-hwmon.c +27 -0
+static int macsmc_hwmon_create_fan_acpi(struct apple_smc *smc,
+					struct macsmc_hwmon_fan *fan, int num)
+{
+	const char num_char = num + '0';
+	int ret;
+
+	ret = macsmc_hwmon_create_sensor_acpi(smc, &fan->now,
+			__SMC_KEY('F', num_char, 'A', 'c'), NULL, 0);
+	if (ret)
+		return ret;
+
+	if (apple_smc_key_exists(smc, __SMC_KEY('F', num_char, 'I', 'D'))) {
+		char buf[17];
+
+		ret = apple_smc_read(smc, __SMC_KEY('F', num_char, 'I', 'D'), buf, 16);
+		if (ret < 0)
+			return ret;
+
+		buf[16] = '\0';
+		snprintf(fan->label, sizeof(fan->label), "%s", buf + 4);
+	} else
+		snprintf(fan->label, sizeof(fan->label), "Fan %d", num);
+
+	ret = macsmc_hwmon_create_sensor_acpi(smc, &fan->min,
+			__SMC_KEY('F', num_char, 'M', 'n'), NULL, 0);
+	if (ret)
+		return ret;

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_create_fan(struct device *dev, struct apple_smc *smc, · line 1540

Quoted 10 lines
drivers/hwmon/macsmc-hwmon.c +10 -0
+
+	ret = macsmc_hwmon_create_sensor_acpi(smc, &fan->max,
+			__SMC_KEY('F', num_char, 'M', 'x'), NULL, 0);
+	if (ret)
+		return ret;
+
+	ret = macsmc_hwmon_create_sensor_acpi(smc, &fan->set,
+			__SMC_KEY('F', num_char, 'T', 'g'), NULL, 0);
+	if (ret)
+		return ret;

[Severity: Medium]
Can a short or unreadable F%dID key copy stale MMIO bytes or abort the entire hwmon probe?

apple_smc_key_exists() discards info.size, so apple_smc_read() requests 16 bytes without checking that F%dID is 16 bytes long; apple_smc_acpi_mmio_read() copies len bytes unconditionally from SRAM, and PIO mode times out with -EIO. On read failure, or if any of F%dMn, F%dMx, or F%dTg is absent, returning ret aborts macsmc_hwmon_probe() completely instead of falling back to "Fan %d" or treating those keys as optional like macsmc_hwmon_create_fan() does.

[ ... ]

Quoted 34 lines
drivers/hwmon/macsmc-hwmon.c +34 -0
+static int macsmc_hwmon_register_sensors_acpi(
+	struct macsmc_hwmon *hwmon,
+	struct macsmc_hwmon_sensors *sensors,
+	const struct macsmc_sensor_desc *descs,
+	size_t num_descs,
+	u32 attrs)
+{
+	size_t count = 0;
+	size_t i;
+	int ret;
+
+	for (i = 0; i < num_descs; i++) {
+		if (apple_smc_key_exists(hwmon->smc, descs[i].key))
+			count++;
+	}
+
+	if (!count)
+		return 0;
+
+	sensors->sensors = devm_kcalloc(hwmon->dev, count,
+					sizeof(*sensors->sensors),
+					GFP_KERNEL);
+	if (!sensors->sensors)
+		return -ENOMEM;
+
+	sensors->count = 0;
+
+	for (i = 0; i < num_descs; i++) {
+		if (!apple_smc_key_exists(hwmon->smc, descs[i].key))
+			continue;
+
+		ret = macsmc_hwmon_create_sensor_acpi(hwmon->smc,
+						&sensors->sensors[sensors->count],
+						descs[i].key,

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_populate_sensors(struct macsmc_hwmon *hwmon, · line 1717

Quoted 10 lines
drivers/hwmon/macsmc-hwmon.c +10 -0
+						descs[i].name,
+						attrs);
+		if (ret)
+			return ret;
+
+		sensors->count++;
+	}
+
+	return 0;
+}

[Severity: Medium]
Can a transient SMC timeout cause a heap out-of-bounds write past sensors->sensors?

apple_smc_key_exists() treats transient -EIO timeouts the same as missing keys. If a key query fails during the counting pass and succeeds during the second pass, sensors->count exceeds the allocated count without any bounds check, overflowing sensors->sensors.

[Severity: High]
Will probing all 558 static keys twice over live SMC hardware stall boot for minutes on PIO Intel Macs?

This function queries every key twice via apple_smc_key_exists() and a third time in macsmc_hwmon_create_sensor_acpi() for present keys (macsmc_hwmon_create_fan_acpi() similarly double-queries F%dMd). On PIO Macs, apple_smc_acpi_port_read() detects nonexistent keys only by timing out for 1,000 ms in apple_smc_acpi_port_wait() and disabling SMC interrupts on the first miss, so probing hundreds of absent keys twice stalls probe for over 15 minutes.

Enumerating or binary-searching the sorted SMC key table via apple_smc_get_key_by_index() (like applesmc.c does) would avoid probing missing keys.

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_populate_sensors(struct macsmc_hwmon *hwmon, · line 1725

Quoted 7 lines
drivers/hwmon/macsmc-hwmon.c +7 -0
+static int macsmc_hwmon_populate_sensors_acpi(struct macsmc_hwmon *hwmon)
+{
+	int ret, i;
+	u8 count;
+
+	ret = apple_smc_read_u8(hwmon->smc, SMC_KEY(FNum), &count);
+	if (count && !ret) {

[Severity: Medium]
Does if (count && !ret) read uninitialized stack variable count when reading FNum fails?

On error, both apple_smc_acpi_mmio_read() and apple_smc_acpi_port_read() return without writing to the output buffer, leaving count uninitialized on fanless Macs without FNum and triggering KMSAN uninit-value reports. Checking if (!ret && count) avoids this.

[ ... ]

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_probe(struct platform_device *pdev) · line 1888

Quoted 4 lines
drivers/hwmon/macsmc-hwmon.c +4 -0
+	if (hwmon->smc->is_acpi)
+		ret = macsmc_hwmon_populate_sensors_acpi(hwmon);
+	else
+		ret = macsmc_hwmon_populate_sensors(hwmon, hwmon->dev->of_node);

[Severity: Medium]
Will Intel Mac sensors backed by ui16, si16, ui32, or si32 SMC keys return byte-swapped values?

Multi-byte integer keys on Intel ACPI SMCs are big-endian (as handled for FS! in macsmc_hwmon_write_old_fan_manual() and in macsmc-power.c), but macsmc_hwmon_read_key() reads them in host byte order without converting when smc->is_acpi is true:

drivers/hwmon/macsmc-hwmon.c:macsmc_hwmon_read_key() {
	...
	case __SMC_KEY('u', 'i', '1', '6'): {
		u16 ui16 = 0;

		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &ui16);
		if (ret)
			return ret;

		*val = (s64)ui16 * scale;
		break;
	}
	...
}

The same applies to si16, ui32, and si32, so any ACPI sensor using those types reports garbage on little-endian x86 hosts.

[ ... ]

Quoted 13 lines
drivers/hwmon/macsmc-hwmon.c +13 -0
+static int macsmc_hwmon_resume(struct platform_device *pdev)
+{
+	struct macsmc_hwmon *hwmon = dev_get_drvdata(&pdev->dev);
+
+	if (hwmon->smc->is_acpi && !hwmon->fan.has_old_manual) {
+		int idx;
+		int ret;
+
+		for (idx = 0; idx < hwmon->fan.count; idx++)
+			if (hwmon->fan.fans[idx].manual) {
+				ret = macsmc_hwmon_write_key(hwmon->smc,
+						&hwmon->fan.fans[idx].mode, 1);
+				if (ret < 0) {

drivers/hwmon/macsmc-hwmon.c static int macsmc_hwmon_probe(struct platform_device *pdev) · line 1949

Quoted 10 lines
drivers/hwmon/macsmc-hwmon.c +10 -0
+					dev_warn(&pdev->dev,
+						"Unable to reenable fan manual, disabling\n");
+					hwmon->fan.fans[idx].manual = false;
+				}
+			}
+
+	}
+
+	return 0;
+}

[Severity: Medium]
Does macsmc_hwmon_resume() leave fan control state desynchronized with the SMC after suspend or hibernation?

Checking !hwmon->fan.has_old_manual skips older Intel Macs that use FS!, so fans[idx].manual stays true in memory after the SMC resets to automatic mode and later fanX_target writes will not re-set FS! until userspace writes 0 first. On newer Intel Macs, re-enabling manual mode (F%dMd = 1) without restoring the saved F%dTg target speed locks fans in manual mode at the SMC's post-sleep or post-boot default target.

[ ... ]

drivers/hwmon/macsmc-hwmon.c MODULE_DEVICE_TABLE(of, macsmc_hwmon_of_table); · line 1962

Quoted 6 lines

static struct platform_driver macsmc_hwmon_driver = {

drivers/hwmon/macsmc-hwmon.c +1 -0
 	.probe = macsmc_hwmon_probe,
+	.resume = macsmc_hwmon_resume,
 	.driver = {
 		.name = "macsmc-hwmon",
 		.of_match_table = macsmc_hwmon_of_table,

[Severity: Low]
Should this use DEFINE_SIMPLE_DEV_PM_OPS() with .driver.pm = pm_sleep_ptr() instead of the deprecated legacy platform_driver.resume callback? Using dev_pm_ops matches the other macsmc drivers in this series and allows the compiler to drop macsmc_hwmon_resume() when CONFIG_PM_SLEEP is disabled.

[ ... ]

Quoted 4 lines

MODULE_DESCRIPTION("Apple Silicon SMC hwmon driver"); MODULE_AUTHOR("James Calligeros <[email protected]>"); MODULE_LICENSE("Dual MIT/GPL");
+MODULE_ALIAS("platform:macsmc-hwmon");

[Severity: Low]
This isn't a bug, but Documentation/hwmon/macsmc-hwmon.rst, MODULE_DESCRIPTION, and the file header comment still describe the driver as Apple Silicon only. The patch also includes unrelated whitespace changes in macsmc_hwmon_write_f32(), macsmc_hwmon_is_visible(), and macsmc_hwmon_populate_sensors() that misalign continuation lines away from the opening parenthesis.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
maintainer
#50 lore raw

Re: [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon

Quoted 17 lines · On 12/4/26 06:48, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 11 potential issues to consider:
- [High] hwmon: macsmc: NULL pointer dereference in DT sensor label handling
- [High] hwmon: macsmc: redundant SMC probes cause multi-minute PIO probe stalls
- [Medium] hwmon: macsmc: uninitialized count read in macsmc_hwmon_populate_sensors_acpi()
- [Medium] hwmon: macsmc: heap OOB write in macsmc_hwmon_register_sensors_acpi()
- [Medium] hwmon: macsmc: invalid sensor keys and missing key validation on ACPI
- [Medium] hwmon: macsmc: unvalidated F%dID read and fatal optional fan key errors
- [Medium] hwmon: macsmc: missing big-endian conversion for integer keys on ACPI
- [Medium] hwmon: macsmc_hwmon_resume() skips FS! and target restore,

Quoted 8 lines

uses legacy PM
- [Low] hwmon: macsmc: outdated docs, MODULE_DESCRIPTION, and whitespace changes
- [Low] hwmon: macsmc: u16/__be16 mismatch in macsmc_hwmon_write_old_fan_manual()
- [Low] hwmon: macsmc-hwmon uses deprecated legacy platform_driver.resume callback
--

Too much for me to even look at. Please fix.

Guenter

maintainer
#51 lore raw

Re: [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon

Quoted 27 lines · Guenter Roeck
Quoted 17 lines · On 12/4/26 06:48, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 11 potential issues to consider:
- [High] hwmon: macsmc: NULL pointer dereference in DT sensor label handling
- [High] hwmon: macsmc: redundant SMC probes cause multi-minute PIO probe stalls
- [Medium] hwmon: macsmc: uninitialized count read in macsmc_hwmon_populate_sensors_acpi()
- [Medium] hwmon: macsmc: heap OOB write in macsmc_hwmon_register_sensors_acpi()
- [Medium] hwmon: macsmc: invalid sensor keys and missing key validation on ACPI
- [Medium] hwmon: macsmc: unvalidated F%dID read and fatal optional fan key errors
- [Medium] hwmon: macsmc: missing big-endian conversion for integer keys on ACPI
- [Medium] hwmon: macsmc_hwmon_resume() skips FS! and target restore,

Quoted 8 lines

uses legacy PM
- [Low] hwmon: macsmc: outdated docs, MODULE_DESCRIPTION, and whitespace changes
- [Low] hwmon: macsmc: u16/__be16 mismatch in macsmc_hwmon_write_old_fan_manual()
- [Low] hwmon: macsmc-hwmon uses deprecated legacy platform_driver.resume callback
--

Quoted 1 line

Too much for me to even look at. Please fix.

It's the longest dump from Sashiko I've seen so far so totally understandable...

Br, Jarkko

#52 lore raw

PATCH 14/14 mfd/macsmc: add ioctl to access smc keys

In reply to Atharva Tiwari

Add a /dev/smc device so userspace can talk to the SMC directly. Some keys that need to be written aren't handled by any driver, or affect several drivers at once, so they can't be exposed cleanly through the kernel interfaces.

Keys already managed by kernel drivers can't be written this way, to avoid conflicts.

---
 drivers/mfd/macsmc-core.c   | 140 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/macsmc.h  |  61 ++--------------
 include/uapi/linux/macsmc.h | 119 ++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+), 56 deletions(-)
 create mode 100644 include/uapi/linux/macsmc.h
drivers/mfd/macsmc-core.c +140 -0
index bc4200f57..a53df6f6c 100644
@@ -6,10 +6,13 @@
 
 #include <linux/bitfield.h>
 #include <linux/device.h>
+#include <linux/fs.h>
 #include <linux/mfd/core.h>
 #include <linux/mfd/macsmc.h>
+#include <linux/miscdevice.h>
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
+#include <linux/uaccess.h>
 
 static const struct mfd_cell apple_smc_rtkit_devs[] = {
 	MFD_CELL_NAME("macsmc-input"),
@@ -112,6 +115,129 @@ static void apple_smc_disable_notifications(void *data)
 	smc->be->disable_notifications(smc->be_cookie);
 }
 
+static long apple_smc_fops_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct apple_smc *smc = file->private_data;
+	void __user *argp = (void __user *)arg;
+	int ret;
+
+	switch (cmd) {
+	case IOCTL_SMC_READ: {
+		struct ioctl_smc_rw_payload payload;
+		struct apple_smc_key_info info;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;
+
+		ret = apple_smc_get_key_info(smc, payload.key, &info);
+		if (ret < 0)
+			return ret;
+
+		if (payload.size != info.size)
+			return -EINVAL;
+
+		if (payload.size > 32 || !(info.flags & APPLE_SMC_READABLE))
+			return -EINVAL;
+
+		ret = apple_smc_read(smc, payload.key, payload.buf, payload.size);
+		if (ret < 0)
+			return ret;
+
+		if (copy_to_user(argp, &payload, sizeof(payload)))
+			return -EFAULT;
+		return 0;
+	}
+	case IOCTL_SMC_WRITE: {
+		struct ioctl_smc_rw_payload payload;
+		struct apple_smc_key_info info;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;
+
+		ret = apple_smc_get_key_info(smc, payload.key, &info);
+		if (ret < 0)
+			return ret;
+
+		if (payload.size != info.size)
+			return -EINVAL;
+
+		if (payload.size > 32)
+			return -EINVAL;
+
+		switch (payload.key) {
+		case __SMC_KEY('H', 'E', '0', 'N'):
+		case __SMC_KEY('H', 'E', '2', 'N'):
+		case __SMC_KEY('H', 'I', '0', 'N'):
+		case __SMC_KEY('M', 'S', 'T', 'S'):
+		case __SMC_KEY('M', 'S', 'P', 'C'):
+		case __SMC_KEY('Q', 'E', 'N', 'A'):
+			break;
+		default:
+			return -EPERM;
+		}
+
+		ret = apple_smc_write(smc, payload.key, payload.buf, payload.size);
+		if (ret < 0)
+			return ret;
+
+		return 0;
+	}
+	case IOCTL_SMC_GET_KEY_INDEX: {
+		struct ioctl_smc_index_payload payload;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;
+
+		ret = apple_smc_get_key_by_index(smc, payload.index, &payload.key);
+		if (ret < 0)
+			return ret;
+
+		if (copy_to_user(argp, &payload, sizeof(payload)))
+			return -EFAULT;
+
+		return 0;
+	}
+	case IOCTL_SMC_GET_KEY_INFO: {
+		struct ioctl_smc_info_payload payload;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;
+
+		ret = apple_smc_get_key_info(smc, payload.key, &payload.info);
+		if (ret < 0)
+			return ret;
+
+		if (copy_to_user(argp, &payload, sizeof(payload)))
+			return -EFAULT;
+
+		return 0;
+	}
+	default:
+		return -EINVAL;
+	}
+}
+
+static int apple_smc_fops_open(struct inode *inode, struct file *file)
+{
+	struct miscdevice *misc = file->private_data;
+
+	file->private_data = container_of(misc, struct apple_smc, miscdev);
+	return 0;
+}
+
+static const struct file_operations apple_smc_fops = {
+	.owner		= THIS_MODULE,
+	.open		= apple_smc_fops_open,
+	.unlocked_ioctl	= apple_smc_fops_ioctl,
+	.compat_ioctl	= compat_ptr_ioctl,
+	.llseek		= noop_llseek,
+};
+
+static void apple_smc_misc_unregister(void *data)
+{
+	misc_deregister(data);
+}
+
 int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 		void *cookie, struct blocking_notifier_head *event_handlers, bool is_acpi)
 {
@@ -150,6 +276,20 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 	if (ret)
 		return ret;
 
+	smc->miscdev.minor = MISC_DYNAMIC_MINOR;
+	smc->miscdev.name = "smc";
+	smc->miscdev.fops = &apple_smc_fops;
+	smc->miscdev.parent = dev;
+	smc->miscdev.mode = 0600;
+
+	ret = misc_register(&smc->miscdev);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, apple_smc_misc_unregister, &smc->miscdev);
+	if (ret)
+		return ret;
+
 	if (smc->is_acpi) {
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_common,
 			ARRAY_SIZE(apple_smc_acpi_common), NULL, 0, NULL);
include/linux/mfd/macsmc.h +5 -56

86 lines not on this page · raw message

include/uapi/linux/macsmc.h new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.55.0
#53 lore raw

Re: [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys

Quoted 18 lines · Atharva Tiwari

Add a /dev/smc device so userspace can talk to the SMC directly. Some keys that need to be written aren't handled by any driver, or affect several drivers at once, so they can't be exposed cleanly through the kernel interfaces.

Keys already managed by kernel drivers can't be written this way, to avoid conflicts.

[...]
+ switch (cmd) {
+ case IOCTL_SMC_READ: {
+ struct ioctl_smc_rw_payload payload;
+ struct apple_smc_key_info info;

drivers/mfd/macsmc-core.c +4 -0
+
+ if (copy_from_user(&payload, argp, sizeof(payload)))
+ return -EFAULT;
+

drivers/mfd/macsmc-core.c static void apple_smc_disable_notifications(void *data) · line 141

Quoted 10 lines
drivers/mfd/macsmc-core.c +10 -0
+ ret = apple_smc_get_key_info(smc, payload.key, &info);
+ if (ret < 0)
+ return ret;
+
+ if (payload.size != info.size)
+ return -EINVAL;
+
+ if (payload.size > 32 || !(info.flags & APPLE_SMC_READABLE))
+ return -EINVAL;
+

This needs a key whitelist too, AS has read-sensitive keys.

#54 lore raw

Re: [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys

Quoted 1 line

This needs a key whitelist too, AS has read-sensitive keys.

If thats the case, i will just disable reading for AS, or if you can provide the keys that are read-sensitive i can add a whitelist.

#55 lore raw

Re: [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys

In reply to Atharva Tiwari
Thank you for your contribution! Sashiko AI review found 17 potential issues to consider: - [High] mfd: macsmc: use-after-free in apple_smc_fops_ioctl() after device unbind - [Medium] mfd: macsmc…

Thank you for your contribution! Sashiko AI review found 17 potential issues to consider:
- [High] mfd: macsmc: use-after-free in apple_smc_fops_ioctl() after device unbind
- [Medium] mfd: macsmc: IOCTL_SMC_WRITE ignores FMODE_WRITE on /dev/smc fd
- [Medium] mfd: macsmc: missing APPLE_SMC_WRITABLE check and late allowlist validation
- [Medium] mfd: macsmc: raw /dev/smc ioctl UAPI in MFD core with undocumented allowlist
- [Medium] mfd: macsmc: commit message mischaracterizes 6-key write allowlist
- [Medium] mfd: macsmc: Missing index bounds check in IOCTL_SMC_GET_KEY_INDEX
- [Medium] mfd: macsmc: apple_smc_probe() omits OSWD and NATi for macsmc-wdt
- [Medium] mfd: macsmc: kernel-only BIT() macro used in UAPI header
- [Medium] mfd: macsmc: UAPI ioctl structs have implicit padding and 32-byte key cap
- [Medium] mfd: macsmc: unconditional /dev/smc registration and unfiltered key reads
- [Low] mfd: macsmc: return -ENOTTY for unknown cmd in apple_smc_fops_ioctl()
- [Low] mfd: macsmc: implicit padding holes in new UAPI ioctl structs
- [Low] mfd: macsmc: commit subject violates MFD prefix and capitalization rules
- [Low] mfd: macsmc: invalid SPDX license expression in UAPI header
- [Low] mfd: macsmc: apple_smc_fops_ioctl() returns -EINVAL for unknown commands
- [Low] mfd: macsmc: ioctl magic 'S' conflicts and lacks ioctl-number.rst entry
- [Low] mfd: macsmc: commit message misdescribes IOCTL_SMC_WRITE key allowlist
--

commit 4104c5546b08cdfee61eacef164c47b7d69ce901
Author: Atharva Tiwari <[email protected]>

mfd/macsmc: add ioctl to access smc keys

This commit registers a /dev/smc miscdevice in macsmc-core and adds a UAPI header exposing ioctls to read keys, write a fixed set of keys, enumerate keys by index, and query key metadata.

Quoted 1 line

mfd/macsmc: add ioctl to access smc keys

[Severity: Low]
This isn't a bug, but MFD commit subjects use the format "mfd: <driver>: <Capitalized description>", e.g. "mfd: macsmc: Add ioctl to access SMC keys".

Quoted 2 lines

Keys already managed by kernel drivers can't be written this way, to avoid conflicts.

[Severity: Medium]
Does this accurately describe the write restriction? The changelog describes a denylist of driver-managed keys, whereas apple_smc_fops_ioctl() enforces a strict hardcoded allowlist of six keys (HE0N, HE2N, HI0N, MSTS, MSPC, QENA) and rejects every other key with -EPERM.

Quoted 4 lines
drivers/mfd/macsmc-core.c +0 -0
index bc4200f574bd5..a53df6f6cd3f2 100644

[ ... ]

Quoted 6 lines
drivers/mfd/macsmc-core.c +2 -0
@@ -112,6 +115,129 @@ static void apple_smc_disable_notifications(void *data)
 	smc->be->disable_notifications(smc->be_cookie);
 }
 
+static long apple_smc_fops_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{

[ ... ]

drivers/mfd/macsmc-core.c static void apple_smc_disable_notifications(void *data) · line 155

Quoted 6 lines
drivers/mfd/macsmc-core.c +6 -0
+	case IOCTL_SMC_WRITE: {
+		struct ioctl_smc_rw_payload payload;
+		struct apple_smc_key_info info;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;

[Severity: Medium]
Can an O_RDONLY file descriptor write SMC keys here? vfs_ioctl() does not enforce FMODE_WRITE on behalf of drivers, so IOCTL_SMC_WRITE should reject callers whose file->f_mode lacks FMODE_WRITE.

Quoted 11 lines
drivers/mfd/macsmc-core.c +11 -0
+		ret = apple_smc_get_key_info(smc, payload.key, &info);
+		if (ret < 0)
+			return ret;
+
+		if (payload.size != info.size)
+			return -EINVAL;
+
+		if (payload.size > 32)
+			return -EINVAL;
+
+		switch (payload.key) {

drivers/mfd/macsmc-core.c static void apple_smc_disable_notifications(void *data) · line 177

Quoted 10 lines
drivers/mfd/macsmc-core.c +10 -0
+		case __SMC_KEY('H', 'E', '0', 'N'):
+		case __SMC_KEY('H', 'E', '2', 'N'):
+		case __SMC_KEY('H', 'I', '0', 'N'):
+		case __SMC_KEY('M', 'S', 'T', 'S'):
+		case __SMC_KEY('M', 'S', 'P', 'C'):
+		case __SMC_KEY('Q', 'E', 'N', 'A'):
+			break;
+		default:
+			return -EPERM;
+		}

[Severity: Medium]
Should IOCTL_SMC_WRITE check (info.flags & APPLE_SMC_WRITABLE) before writing, and validate payload.size and the key allowlist before calling apple_smc_get_key_info()?

Unlike IOCTL_SMC_READ, IOCTL_SMC_WRITE omits the writability flag check; on the ACPI PIO backend, apple_smc_acpi_port_write() does not read back a firmware error status, so a write to a read-only key can silently succeed. Running apple_smc_get_key_info() before the static size and allowlist checks also acquires smc->mutex and performs hardware IPC for disallowed or oversized requests (and on ACPI PIO, a nonexistent key stalls for 1000 ms and clears smc->interrupt_enabled).

[Severity: Low]
Neither the commit message nor comments here explain what these six allowlisted keys control or why userspace needs write access to them; the changelog only mentions excluding driver-managed keys.

[ ... ]

drivers/mfd/macsmc-core.c static void apple_smc_disable_notifications(void *data) · line 191

Quoted 7 lines
drivers/mfd/macsmc-core.c +7 -0
+	case IOCTL_SMC_GET_KEY_INDEX: {
+		struct ioctl_smc_index_payload payload;
+
+		if (copy_from_user(&payload, argp, sizeof(payload)))
+			return -EFAULT;
+
+		ret = apple_smc_get_key_by_index(smc, payload.index, &payload.key);

[Severity: Medium]
Does IOCTL_SMC_GET_KEY_INDEX need to check payload.index >= smc->key_count before calling apple_smc_get_key_by_index()?

Because the UAPI documents sequential enumeration with increasing index values, normal callers will pass payload.index == smc->key_count to find the end of the table. On ACPI PIO Intel Macs, an out-of-range query stalls apple_smc_acpi_port_wait() for up to 2 seconds while holding smc->mutex and permanently clears smc->interrupt_enabled = 0, reverting subsequent SMC PIO transactions to polling.

[ ... ]

drivers/mfd/macsmc-core.c static void apple_smc_disable_notifications(void *data) · line 218

Quoted 4 lines
drivers/mfd/macsmc-core.c +4 -0
+	default:
+		return -EINVAL;
+	}
+}

[Severity: Low]
Should unrecognized ioctl commands return -ENOTTY instead of -EINVAL? Returning -EINVAL breaks standard userspace ioctl probing such as isatty() / TCGETS on /dev/smc.

[Severity: Low]
Per Documentation/driver-api/ioctl.rst, unlocked_ioctl handlers should return -ENOTTY (or -ENOIOCTLCMD) for unknown command numbers so userspace can distinguish unsupported ioctls from invalid payload arguments (-EINVAL).

[ ... ]

Quoted 11 lines
drivers/mfd/macsmc-core.c +7 -0
@@ -150,6 +276,20 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 	if (ret)
 		return ret;
 
+	smc->miscdev.minor = MISC_DYNAMIC_MINOR;
+	smc->miscdev.name = "smc";
+	smc->miscdev.fops = &apple_smc_fops;
+	smc->miscdev.parent = dev;
+	smc->miscdev.mode = 0600;
+
+	ret = misc_register(&smc->miscdev);

drivers/mfd/macsmc-core.c int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, · line 295

Quoted 10 lines
drivers/mfd/macsmc-core.c +7 -0
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, apple_smc_misc_unregister, &smc->miscdev);
+	if (ret)
+		return ret;
+
 	if (smc->is_acpi) {
 		ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_common,
 			ARRAY_SIZE(apple_smc_acpi_common), NULL, 0, NULL);

[Severity: High]
Can apple_smc_fops_ioctl() hit a use-after-free when the SMC device is unbound or a backend module is unloaded while /dev/smc is open?

struct apple_smc and its backend context are devm-allocated, apple_smc_fops_open() caches a raw pointer to smc in file->private_data with no reference counting or revocation lock, and apple_smc_fops.owner pins only macsmc-core rather than the loadable macsmc-acpi or macsmc-rtkit backend modules. misc_deregister() does not revoke open file descriptors or wait for in-flight ioctls:

CPU0 CPU1
sysfs unbind / rmmod macsmc-acpi open("/dev/smc")
devres_release_all() file->private_data = smc

    misc_deregister()
    kfree(smc) & backend teardown
                                  ioctl(fd, IOCTL_SMC_READ, ...)
                                    apple_smc_fops_ioctl()
                                      mutex_lock(&smc->mutex) <-- UAF
                                      smc->be->read(...)

The same race also exists if devm_mfd_add_devices() fails after misc_register() exposes /dev/smc. Registering the miscdevice after MFD child registration, refcounting the open-file context independently of devm, and synchronizing deregistration with a lock (such as an rw_semaphore) before tearing down backend resources would prevent this.

[Severity: Medium]
As reported by Jonathan Cameron on RFC [1], should a raw firmware pass-through miscdevice and custom ioctl UAPI live in drivers/mfd/?

Core MFD drivers normally handle only shared resources and child registration, and exposing a permanent opaque ioctl interface for six undocumented keys bypasses standard kernel subsystems (or fwctl) without explaining what those keys do or why standard interfaces cannot be used.

[Severity: Medium]
As reported by Sasha Finkelstein on RFC [2], why is /dev/smc registered unconditionally here instead of being restricted to T2 Macs, and why does IOCTL_SMC_READ allow reading arbitrary readable keys?

Because misc_register() runs before checking smc->is_acpi or has_apple_t2_chip (added in commit "treewide: Add a flag to detect the Apple T2 chip"), /dev/smc is exposed on Apple Silicon RTKit and non-T2 Intel Macs as well. On the read path, IOCTL_SMC_READ only checks APPLE_SMC_READABLE and size <= 32, without a read allowlist or filtering read-sensitive keys such as APPLE_SMC_FUNCTION or APPLE_SMC_PRIVATE_READ.

[Severity: Medium]
In the smc->is_acpi branch right below, apple_smc_probe() gates apple_smc_acpi_wdt registration solely on SMC_KEY(NATJ).

macsmc_wdt_probe() in drivers/watchdog/macsmc_wdt.c supports newer SMCs via SMC_KEY(OSWD) and otherwise requires both SMC_KEY(NATJ) and SMC_KEY(NATi). Checking only NATJ leaves the watchdog unregistered on OSWD-only Macs and registers it even when NATi is absent.

include/uapi/linux/macsmc.h line 1

Quoted 7 lines
include/uapi/linux/macsmc.h new file +1 -0
new file mode 100644
index 0000000000000..ef784539d3252
@@ -0,0 +1,119 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR MIT WITH Linux-syscall-note */

[Severity: Low]
Is this SPDX expression valid? WITH binds more tightly than OR, so this attaches Linux-syscall-note to MIT (which scripts/spdxcheck.py rejects) and leaves GPL-2.0-only without the syscall note. It should be "(GPL-2.0-only WITH Linux-syscall-note) OR MIT".

[ ... ]

include/uapi/linux/macsmc.h line 53

Quoted 7 lines
include/uapi/linux/macsmc.h new file +7 -0
+#define APPLE_SMC_READABLE		BIT(7)
+#define APPLE_SMC_WRITABLE		BIT(6)
+#define APPLE_SMC_FUNCTION		BIT(4)
+#define APPLE_SMC_CONST		BIT(3)
+#define APPLE_SMC_ATOMIC		BIT(2)
+#define APPLE_SMC_PRIVATE_READ		BIT(1)
+#define APPLE_SMC_PRIVATE_WRITE		BIT(0)

[Severity: Medium]
Does this break userspace compilation when any APPLE_SMC_* flag is referenced? BIT() is a kernel-internal macro not provided by UAPI headers; UAPI constants should use _BITUL() (with <linux/const.h>) or plain unsigned shifts such as (1U << 7).

[ ... ]

include/uapi/linux/macsmc.h line 89

Quoted 5 lines
include/uapi/linux/macsmc.h new file +5 -0
+struct ioctl_smc_rw_payload {
+	smc_key key;
+	__u32 size;
+	__u8 buf[32];
+};

[Severity: Medium]
Does hardcoding __u8 buf[32] permanently prevent accessing SMC keys larger than 32 bytes? struct apple_smc_key_info.size is __u8 and the RTKit backend defines SMC_MAX_SIZE as 255, so baking sizeof(struct ioctl_smc_rw_payload) into the ioctl ABI makes larger keys inaccessible without new ioctls.

In addition, struct ioctl_smc_index_payload narrows index to __u16 despite u32 smc->key_count (with a 2-byte internal padding hole), and struct apple_smc_key_info leaves 2 bytes of implicit tail padding when embedded in struct ioctl_smc_info_payload.

[ ... ]

include/uapi/linux/macsmc.h line 101

Quoted 4 lines
include/uapi/linux/macsmc.h new file +4 -0
+struct ioctl_smc_index_payload {
+	__u16 index;
+	smc_key key;
+};

[Severity: Low]
Both struct ioctl_smc_index_payload (between __u16 index and smc_key key) and struct apple_smc_key_info (trailing 2 bytes) contain implicit compiler padding holes. Widening index to __u32 and adding explicit reserved padding fields avoids unnamed holes in the UAPI structs.

[ ... ]

include/uapi/linux/macsmc.h line 117

Quoted 5 lines
include/uapi/linux/macsmc.h new file +5 -0
+#define APPLE_SMC_MAGIC 'S'
+#define IOCTL_SMC_READ    _IOWR(APPLE_SMC_MAGIC, 1, struct ioctl_smc_rw_payload)
+#define IOCTL_SMC_WRITE   _IOW(APPLE_SMC_MAGIC, 2, struct ioctl_smc_rw_payload)
+#define IOCTL_SMC_GET_KEY_INDEX  _IOWR(APPLE_SMC_MAGIC, 3, struct ioctl_smc_index_payload)
+#define IOCTL_SMC_GET_KEY_INFO   _IOWR(APPLE_SMC_MAGIC, 4, struct ioctl_smc_info_payload)

[Severity: Low]
Does APPLE_SMC_MAGIC 'S' conflict with existing ioctl numbers? Documentation/userspace-api/ioctl/ioctl-number.rst already lists 'S' as conflicted across cdrom.h, asequencer.h, scsi_ioctl.h, and scsi.h, and this new range is not registered there.

[1] https://lore.kernel.org/r/20261101013817.22d67ea7@jic23-hlaptop
[2] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#56 lore raw
In reply to Atharva Tiwari
Quoted 27 lines · Atharva Tiwari

applesmc is very old, and all of its code is lumped into one driver. Fan control, sensors, the light sensor, the motion sensor and the keyboard backlight in a single file with its own way of talking to the SMC. It is hard to extend and to support new machines.

This series adds Intel Mac support to the macsmc drivers that were written for Apple Silicon (the Asahi implementation). The SMC works the same way on both platforms, so we share a lot of code with Asahi. Only the way of talking to the SMC differs.

To make that work, patch 1 splits the macsmc driver into a common core and a backend. The existing RTKit code moves into its own backend. Patch 2 adds an ACPI backend for Intel Macs with I/O port, MMIO and interrupt support. After that, each function gets its own small driver using the standard kernel interfaces (hwmon, IIO, LEDs, power supply, RTC, watchdog), instead of one large driver doing everything.

On Intel Macs these drivers replace applesmc (hwmon, light sensor, motion sensor and keyboard backlight) and acpi-sbs (battery and adapter). They are mutually exclusive with the drivers they replace, so only one is used at a time.

The series is organised as follows:

Quoted 9 lines
  1      Split macsmc into a core and backends
  2      ACPI backend for Intel Macs
  3-6    New drivers: watchdog, IIO light and accelerometer,
         keyboard backlight LED
  7-9    RTC support for T2 Macs, a T2 detection flag, and
         disabling the broken ACPI RTCs
  10-11  Power supply support, replacing acpi-sbs
  12-13  hwmon support for Intel Macs
  14     ioctl for userspace access to SMC keys

This AI slop needs a lot more *human* work. Take your time, polish the output. This series anyways missed v7.5.

Signature · 2 lines
With Best Regards,
Andy Shevchenko