Skip to content

Commit

Permalink
platform/apple: Add new Apple Mac SMC driver
Browse files Browse the repository at this point in the history
This driver implements support for the SMC (System Management
Controller) in Apple Macs. In contrast to the existing applesmc driver,
it uses pluggable backends that allow it to support different SMC
implementations, and uses the MFD subsystem to expose the core SMC
functionality so that specific features (gpio, hwmon, battery, etc.) can
be implemented by separate drivers in their respective downstream
subsystems.

The initial RTKit backend adds support for Apple Silicon Macs (M1 et
al). We hope a backend for T2 Macs will be written in the future
(since those are not supported by applesmc), and eventually an x86
backend would allow us to fully deprecate applesmc in favor of this
driver.

Signed-off-by: Hector Martin <marcan@marcan.st>
  • Loading branch information
marcan committed Jul 8, 2022
1 parent 4451155 commit 092d508
Show file tree
Hide file tree
Showing 8 changed files with 877 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/platform/Kconfig
Expand Up @@ -15,3 +15,5 @@ source "drivers/platform/mellanox/Kconfig"
source "drivers/platform/olpc/Kconfig"

source "drivers/platform/surface/Kconfig"

source "drivers/platform/apple/Kconfig"
1 change: 1 addition & 0 deletions drivers/platform/Makefile
Expand Up @@ -10,3 +10,4 @@ obj-$(CONFIG_OLPC_EC) += olpc/
obj-$(CONFIG_GOLDFISH) += goldfish/
obj-$(CONFIG_CHROME_PLATFORMS) += chrome/
obj-$(CONFIG_SURFACE_PLATFORMS) += surface/
obj-$(CONFIG_APPLE_PLATFORMS) += apple/
49 changes: 49 additions & 0 deletions drivers/platform/apple/Kconfig
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: GPL-2.0
#
# Apple Platform-Specific Drivers
#

menuconfig APPLE_PLATFORMS
bool "Apple Mac Platform-Specific Device Drivers"
default y
help
Say Y here to get to see options for platform-specific device drivers
for Apple devices. This option alone does not add any kernel code.

If you say N, all options in this submenu will be skipped and disabled.

if APPLE_PLATFORMS

config APPLE_SMC
tristate "Apple SMC Driver"
depends on ARCH_APPLE || COMPILE_TEST
default ARCH_APPLE
select MFD_CORE
help
Build support for the Apple System Management Controller present in
Apple Macs. This driver currently supports the SMC in Apple Silicon
Macs. For x86 Macs, see the applesmc driver (SENSORS_APPLESMC).

Say Y here if you have an Apple Silicon Mac.

To compile this driver as a module, choose M here: the module will
be called macsmc.

if APPLE_SMC

config APPLE_SMC_RTKIT
tristate "RTKit (Apple Silicon) backend"
depends on ARCH_APPLE || COMPILE_TEST
depends on APPLE_RTKIT
default ARCH_APPLE
help
Build support for SMC communications via the RTKit backend. This is
required for Apple Silicon Macs.

Say Y here if you have an Apple Silicon Mac.

To compile this driver as a module, choose M here: the module will
be called macsmc-rtkit.

endif
endif
11 changes: 11 additions & 0 deletions drivers/platform/apple/Makefile
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for linux/drivers/platform/apple
# Apple Platform-Specific Drivers
#

macsmc-y += smc_core.o
macsmc-rtkit-y += smc_rtkit.o

obj-$(CONFIG_APPLE_SMC) += macsmc.o
obj-$(CONFIG_APPLE_SMC_RTKIT) += macsmc-rtkit.o
28 changes: 28 additions & 0 deletions drivers/platform/apple/smc.h
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* Apple SMC internal core definitions
* Copyright (C) The Asahi Linux Contributors
*/

#ifndef _SMC_H
#define _SMC_H

#include <linux/mfd/macsmc.h>

struct apple_smc_backend_ops {
int (*read_key)(void *cookie, smc_key key, void *buf, size_t size);
int (*write_key)(void *cookie, smc_key key, void *buf, size_t size);
int (*write_key_atomic)(void *cookie, smc_key key, void *buf, size_t size);
int (*rw_key)(void *cookie, smc_key key, void *wbuf, size_t wsize,
void *rbuf, size_t rsize);
int (*get_key_by_index)(void *cookie, int index, smc_key *key);
int (*get_key_info)(void *cookie, smc_key key, struct apple_smc_key_info *info);
};

struct apple_smc *apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
void *cookie);
void *apple_smc_get_cookie(struct apple_smc *smc);
int apple_smc_remove(struct apple_smc *smc);
void apple_smc_event_received(struct apple_smc *smc, uint32_t event);

#endif
249 changes: 249 additions & 0 deletions drivers/platform/apple/smc_core.c
@@ -0,0 +1,249 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* Apple SMC core framework
* Copyright The Asahi Linux Contributors
*/

#include <linux/device.h>
#include <linux/mfd/core.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
#include "smc.h"

struct apple_smc {
struct device *dev;

void *be_cookie;
const struct apple_smc_backend_ops *be;

struct mutex mutex;

u32 key_count;
smc_key first_key;
smc_key last_key;

struct blocking_notifier_head event_handlers;
};

static const struct mfd_cell apple_smc_devs[] = {
{
.name = "macsmc-gpio",
},
{
.name = "macsmc-hid",
},
{
.name = "macsmc-power",
},
{
.name = "macsmc-reboot",
},
{
.name = "macsmc-rtc",
},
};

int apple_smc_read(struct apple_smc *smc, smc_key key, void *buf, size_t size)
{
int ret;

mutex_lock(&smc->mutex);
ret = smc->be->read_key(smc->be_cookie, key, buf, size);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_read);

int apple_smc_write(struct apple_smc *smc, smc_key key, void *buf, size_t size)
{
int ret;

mutex_lock(&smc->mutex);
ret = smc->be->write_key(smc->be_cookie, key, buf, size);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_write);

int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, void *buf, size_t size)
{
int ret;

/*
* Will fail if SMC is busy. This is only used by SMC reboot/poweroff
* final calls, so it doesn't really matter at that point.
*/
if (!mutex_trylock(&smc->mutex))
return -EBUSY;

ret = smc->be->write_key_atomic(smc->be_cookie, key, buf, size);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_write_atomic);

int apple_smc_rw(struct apple_smc *smc, smc_key key, void *wbuf, size_t wsize,
void *rbuf, size_t rsize)
{
int ret;

mutex_lock(&smc->mutex);
ret = smc->be->rw_key(smc->be_cookie, key, wbuf, wsize, rbuf, rsize);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_rw);

int apple_smc_get_key_by_index(struct apple_smc *smc, int index, smc_key *key)
{
int ret;

mutex_lock(&smc->mutex);
ret = smc->be->get_key_by_index(smc->be_cookie, index, key);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_get_key_by_index);

int apple_smc_get_key_info(struct apple_smc *smc, smc_key key, struct apple_smc_key_info *info)
{
int ret;

mutex_lock(&smc->mutex);
ret = smc->be->get_key_info(smc->be_cookie, key, info);
mutex_unlock(&smc->mutex);

return ret;
}
EXPORT_SYMBOL(apple_smc_get_key_info);

int apple_smc_find_first_key_index(struct apple_smc *smc, smc_key key)
{
int start = 0, count = smc->key_count;
int ret;

if (key <= smc->first_key)
return 0;
if (key > smc->last_key)
return smc->key_count;

while (count > 1) {
int pivot = start + ((count - 1) >> 1);
smc_key pkey;

ret = apple_smc_get_key_by_index(smc, pivot, &pkey);
if (ret < 0)
return ret;

if (pkey == key)
return pivot;

pivot++;

if (pkey < key) {
count -= pivot - start;
start = pivot;
} else {
count = pivot - start;
}
}

return start;
}
EXPORT_SYMBOL(apple_smc_find_first_key_index);

int apple_smc_get_key_count(struct apple_smc *smc)
{
return smc->key_count;
}
EXPORT_SYMBOL(apple_smc_get_key_count);

void apple_smc_event_received(struct apple_smc *smc, uint32_t event)
{
dev_dbg(smc->dev, "Event: 0x%08x\n", event);
blocking_notifier_call_chain(&smc->event_handlers, event, NULL);
}
EXPORT_SYMBOL(apple_smc_event_received);

int apple_smc_register_notifier(struct apple_smc *smc, struct notifier_block *n)
{
return blocking_notifier_chain_register(&smc->event_handlers, n);
}
EXPORT_SYMBOL(apple_smc_register_notifier);

int apple_smc_unregister_notifier(struct apple_smc *smc, struct notifier_block *n)
{
return blocking_notifier_chain_unregister(&smc->event_handlers, n);
}
EXPORT_SYMBOL(apple_smc_unregister_notifier);

void *apple_smc_get_cookie(struct apple_smc *smc)
{
return smc->be_cookie;
}
EXPORT_SYMBOL(apple_smc_get_cookie);

struct apple_smc *apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops, void *cookie)
{
struct apple_smc *smc;
u32 count;
int ret;

smc = devm_kzalloc(dev, sizeof(*smc), GFP_KERNEL);
if (!smc)
return ERR_PTR(-ENOMEM);

smc->dev = dev;
smc->be_cookie = cookie;
smc->be = ops;
mutex_init(&smc->mutex);
BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers);

ret = apple_smc_read_u32(smc, SMC_KEY(#KEY), &count);
if (ret)
return ERR_PTR(dev_err_probe(dev, ret, "Failed to get key count"));
smc->key_count = be32_to_cpu(count);

ret = apple_smc_get_key_by_index(smc, 0, &smc->first_key);
if (ret)
return ERR_PTR(dev_err_probe(dev, ret, "Failed to get first key"));

ret = apple_smc_get_key_by_index(smc, smc->key_count - 1, &smc->last_key);
if (ret)
return ERR_PTR(dev_err_probe(dev, ret, "Failed to get last key"));

/* Enable notifications */
apple_smc_write_flag(smc, SMC_KEY(NTAP), 1);

dev_info(dev, "Initialized (%d keys %p4ch..%p4ch)\n",
smc->key_count, &smc->first_key, &smc->last_key);

dev_set_drvdata(dev, smc);

ret = mfd_add_devices(dev, -1, apple_smc_devs, ARRAY_SIZE(apple_smc_devs), NULL, 0, NULL);
if (ret)
return ERR_PTR(dev_err_probe(dev, ret, "Subdevice initialization failed"));

return smc;
}
EXPORT_SYMBOL(apple_smc_probe);

int apple_smc_remove(struct apple_smc *smc)
{
mfd_remove_devices(smc->dev);

/* Disable notifications */
apple_smc_write_flag(smc, SMC_KEY(NTAP), 1);

return 0;
}
EXPORT_SYMBOL(apple_smc_remove);

MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
MODULE_LICENSE("Dual MIT/GPL");
MODULE_DESCRIPTION("Apple SMC core");

0 comments on commit 092d508

Please sign in to comment.