Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/apple-build-kunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ on:
- 'drivers/media/platform/apple/**'
- 'drivers/mfd/**'
- 'drivers/nvme/host/**'
- 'drivers/nvmem/**'
- 'drivers/pci/controller/**'
- 'drivers/power/**'
- 'drivers/soc/apple/**'
Expand Down Expand Up @@ -49,6 +50,7 @@ on:
- 'drivers/media/platform/apple/**'
- 'drivers/mfd/**'
- 'drivers/nvme/host/**'
- 'drivers/nvmem/**'
- 'drivers/pci/controller/**'
- 'drivers/power/**'
- 'drivers/soc/apple/**'
Expand Down Expand Up @@ -124,6 +126,7 @@ jobs:
drivers/media/platform/apple/ \
drivers/mfd/ \
drivers/nvme/host/ \
drivers/nvmem/ \
drivers/pci/controller/ \
drivers/power/ \
drivers/soc/apple/ \
Expand Down Expand Up @@ -158,6 +161,7 @@ jobs:
- { suite: scaler, kunitconfig: drivers/media/platform/apple/scaler/.kunitconfig }
- { suite: macsmc, kunitconfig: drivers/mfd/macsmc.kunitconfig }
- { suite: clpc, kunitconfig: drivers/soc/apple/clpc.kunitconfig }
- { suite: apple-spmi-nvmem, kunitconfig: drivers/nvmem/apple-spmi-nvmem.kunitconfig }
- { suite: trusted-keys, kunitconfig: security/keys/trusted-keys/tests/.kunitconfig }
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,9 @@ F: drivers/nfc/.kunitconfig
F: drivers/nfc/apple-stockholm*.c
F: drivers/nvme/host/apple.c
F: drivers/nvmem/apple-efuses.c
F: drivers/nvmem/apple-spmi-nvmem-test.c
F: drivers/nvmem/apple-spmi-nvmem.c
F: drivers/nvmem/apple-spmi-nvmem.kunitconfig
F: drivers/phy/apple/
F: drivers/phy/apple/.kunitconfig.apple-atc
F: drivers/pinctrl/pinctrl-apple-gpio.c
Expand Down
10 changes: 10 additions & 0 deletions drivers/nvmem/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ config NVMEM_APPLE_SPMI
This driver can also be built as a module. If so, the module
will be called apple-nvmem-spmi.

config NVMEM_APPLE_SPMI_KUNIT_TEST
bool "KUnit tests for the Apple SPMI NVMEM driver" if !KUNIT_ALL_TESTS
depends on NVMEM_APPLE_SPMI=y && NVMEM_SYSFS && KUNIT=y
default KUNIT_ALL_TESTS
help
Check, on a register map in memory, that the PMIC's whole-device
sysfs file cannot be written, even with force_ro cleared, while
the kernel still writes its byte and bit-field cells. No Apple
hardware is needed.

config NVMEM_BCM_OCOTP
tristate "Broadcom On-Chip OTP Controller support"
depends on ARCH_BCM_IPROC || COMPILE_TEST
Expand Down
153 changes: 153 additions & 0 deletions drivers/nvmem/apple-spmi-nvmem-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* Included by apple-spmi-nvmem.c: the provider as the driver registers it, on a
* register map in memory. Userspace cannot write the PMIC's registers through
* the whole-device sysfs file, whatever force_ro says, while the kernel still
* writes the cells its consumers declare.
*/
#include <kunit/device.h>
#include <kunit/test.h>
#include <linux/string.h>

#include "internals.h"

struct apple_spmi_nvmem_test {
u8 regs[0x10000];
};

static int apple_spmi_nvmem_test_reg_read(void *context, unsigned int reg, unsigned int *val)
{
struct apple_spmi_nvmem_test *t = context;

*val = t->regs[reg];
return 0;
}

static int apple_spmi_nvmem_test_reg_write(void *context, unsigned int reg, unsigned int val)
{
struct apple_spmi_nvmem_test *t = context;

t->regs[reg] = val;
return 0;
}

/* The driver's register layout (apple_spmi_regmap_config), on the array above */
static const struct regmap_config apple_spmi_nvmem_test_regmap_config = {
.reg_bits = 16,
.val_bits = 8,
.max_register = 0xffff,
.reg_read = apple_spmi_nvmem_test_reg_read,
.reg_write = apple_spmi_nvmem_test_reg_write,
};

static struct nvmem_device *apple_spmi_nvmem_test_register(struct kunit *test,
struct apple_spmi_nvmem_test **regs)
{
struct apple_spmi_nvmem_test *t;
struct nvmem_device *nvmem;
struct regmap *regmap;
struct device *dev;

dev = kunit_device_register(test, "apple-spmi-nvmem-test");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, t);

regmap = devm_regmap_init(dev, NULL, t, &apple_spmi_nvmem_test_regmap_config);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, regmap);

nvmem = apple_spmi_nvmem_register(dev, regmap);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, nvmem);
*regs = t;
return nvmem;
}

/*
* The whole-device file: root reads it, nobody writes it, not with read_only
* cleared either, and force_ro is shown but cannot be stored.
*/
static void apple_spmi_nvmem_sysfs_read_only_test(struct kunit *test)
{
u8 offset[6] = { 1, 2, 3, 4, 5, 6 }, out[6] = {};
const struct attribute_group *group;
const struct bin_attribute *raw;
struct device_attribute *force_ro;
struct apple_spmi_nvmem_test *t;
struct nvmem_device *nvmem;
struct kobject *kobj;
int i;

nvmem = apple_spmi_nvmem_test_register(test, &t);
kobj = &nvmem->dev.kobj;
KUNIT_ASSERT_NOT_NULL(test, nvmem->dev.groups);
group = nvmem->dev.groups[0];
raw = group->bin_attrs[0];
KUNIT_ASSERT_STREQ(test, raw->attr.name, "nvmem");

KUNIT_EXPECT_EQ(test, group->is_bin_visible(kobj, raw, 0), (umode_t)0400);

t->regs[0x2100] = 0x5a;
KUNIT_EXPECT_EQ(test, raw->write(NULL, kobj, raw, (char *)offset, 0x2100, sizeof(offset)),
(ssize_t)-EPERM);
KUNIT_EXPECT_EQ(test, t->regs[0x2100], 0x5a);

for (i = 0; group->attrs[i]; i++)
if (!strcmp(group->attrs[i]->name, "force_ro"))
break;
KUNIT_ASSERT_NOT_NULL(test, group->attrs[i]);
KUNIT_EXPECT_EQ(test, group->is_visible(kobj, group->attrs[i], i), (umode_t)0444);
force_ro = container_of(group->attrs[i], struct device_attribute, attr);
KUNIT_EXPECT_EQ(test, force_ro->store(&nvmem->dev, force_ro, "0", 1), (ssize_t)-EPERM);

/* As if read_only were cleared, by force_ro or otherwise */
nvmem->read_only = false;
KUNIT_EXPECT_EQ(test, group->is_bin_visible(kobj, raw, 0), (umode_t)0400);
KUNIT_EXPECT_EQ(test, raw->write(NULL, kobj, raw, (char *)offset, 0x2100, sizeof(offset)),
(ssize_t)-EPERM);
KUNIT_EXPECT_EQ(test, t->regs[0x2100], 0x5a);

KUNIT_EXPECT_EQ(test, raw->read(NULL, kobj, raw, (char *)out, 0x2100, sizeof(out)),
(ssize_t)sizeof(out));
KUNIT_EXPECT_EQ(test, out[0], 0x5a);
}

/*
* The kernel's consumers still write their cells: a byte cell (the RTC offset)
* and a bit-field cell sharing its byte (the panic count above the boot error
* count).
*/
static void apple_spmi_nvmem_cell_write_test(struct kunit *test)
{
struct nvmem_cell_info rtc_offset = {
.name = "rtc-offset", .offset = 0x2100, .bytes = 6,
};
struct nvmem_cell_info panic_count = {
.name = "panic-count", .offset = 0xf802, .bytes = 1, .bit_offset = 4, .nbits = 4,
};
u8 offset[6] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
struct apple_spmi_nvmem_test *t;
struct nvmem_device *nvmem;
u8 count = 3;

nvmem = apple_spmi_nvmem_test_register(test, &t);

KUNIT_EXPECT_EQ(test, nvmem_device_cell_write(nvmem, &rtc_offset, offset), 6);
KUNIT_EXPECT_MEMEQ(test, &t->regs[0x2100], offset, sizeof(offset));

t->regs[0xf802] = 0x05;
KUNIT_EXPECT_EQ(test, nvmem_device_cell_write(nvmem, &panic_count, &count), 1);
KUNIT_EXPECT_EQ(test, t->regs[0xf802], 0x35);
}

static struct kunit_case apple_spmi_nvmem_test_cases[] = {
KUNIT_CASE(apple_spmi_nvmem_sysfs_read_only_test),
KUNIT_CASE(apple_spmi_nvmem_cell_write_test),
{}
};

static struct kunit_suite apple_spmi_nvmem_test_suite = {
.name = "apple-spmi-nvmem",
.test_cases = apple_spmi_nvmem_test_cases,
};

kunit_test_suite(apple_spmi_nvmem_test_suite);
30 changes: 22 additions & 8 deletions drivers/nvmem/apple-spmi-nvmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,40 @@ static int apple_spmi_nvmem_write(void *priv, unsigned int offset, void *val,
return regmap_bulk_write(map, offset, val, bytes);
}

static int apple_spmi_nvmem_probe(struct spmi_device *sdev)
/*
* The PMIC's register file. Its kernel consumers (the RTC, the reboot and boot
* state) write the cells the device tree declares; through sysfs, only root may
* read the registers, and nobody may write them.
*/
static struct nvmem_device *apple_spmi_nvmem_register(struct device *dev, struct regmap *regmap)
{
struct regmap *regmap;
struct nvmem_device *provider;
struct nvmem_config nvmem_cfg = {
.dev = &sdev->dev,
.dev = dev,
.name = "spmi_nvmem",
.id = NVMEM_DEVID_AUTO,
.word_size = 1,
.stride = 1,
.size = 0xffff,
/* PMIC registers: only root may read them through sysfs. */
.root_only = true,
.sysfs_read_only = true,
.reg_read = apple_spmi_nvmem_read,
.reg_write = apple_spmi_nvmem_write,
.priv = regmap,
};

return devm_nvmem_register(dev, &nvmem_cfg);
}

static int apple_spmi_nvmem_probe(struct spmi_device *sdev)
{
struct regmap *regmap;
struct nvmem_device *provider;

regmap = devm_regmap_init_spmi_ext(sdev, &apple_spmi_regmap_config);
if (IS_ERR(regmap))
return PTR_ERR(regmap);

nvmem_cfg.priv = regmap;

provider = devm_nvmem_register(&sdev->dev, &nvmem_cfg);
provider = apple_spmi_nvmem_register(&sdev->dev, regmap);
if (IS_ERR(provider))
return PTR_ERR(provider);
if (!of_device_is_compatible(sdev->dev.of_node, "apple,baku-pmic") &&
Expand Down Expand Up @@ -91,3 +101,7 @@ module_spmi_driver(apple_spmi_nvmem_driver);
MODULE_LICENSE("Dual MIT/GPL");
MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
MODULE_DESCRIPTION("Apple SPMI NVMEM driver");

#if IS_ENABLED(CONFIG_NVMEM_APPLE_SPMI_KUNIT_TEST)
#include "apple-spmi-nvmem-test.c"
#endif
7 changes: 7 additions & 0 deletions drivers/nvmem/apple-spmi-nvmem.kunitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CONFIG_KUNIT=y
CONFIG_ARCH_APPLE=y
CONFIG_SPMI=y
CONFIG_NVMEM=y
CONFIG_NVMEM_SYSFS=y
CONFIG_NVMEM_APPLE_SPMI=y
CONFIG_NVMEM_APPLE_SPMI_KUNIT_TEST=y
18 changes: 16 additions & 2 deletions drivers/nvmem/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,13 @@ static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct nvmem_device *nvmem = to_nvmem_device(dev);
int ret = kstrtobool(buf, &nvmem->read_only);
int ret;

/* It would only govern the kernel's cell writes. */
if (nvmem->sysfs_read_only)
return -EPERM;

ret = kstrtobool(buf, &nvmem->read_only);
if (ret < 0)
return ret;

Expand Down Expand Up @@ -266,6 +271,10 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
dev = kobj_to_dev(kobj);
nvmem = to_nvmem_device(dev);

/* Whatever the file's mode, and whatever force_ro says */
if (nvmem->sysfs_read_only)
return -EPERM;

if (!IS_ALIGNED(pos, nvmem->stride))
return -EINVAL;

Expand Down Expand Up @@ -296,7 +305,7 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
if (!nvmem->root_only)
mode |= 0044;

if (!nvmem->read_only)
if (!nvmem->read_only && !nvmem->sysfs_read_only)
mode |= 0200;

if (!ops->reg_write)
Expand Down Expand Up @@ -345,6 +354,10 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
if (attr == &dev_attr_force_ro.attr && !ops->reg_write)
return 0; /* Attribute not visible */

/* Shown, but it cannot make the device's file writable. */
if (attr == &dev_attr_force_ro.attr && nvmem->sysfs_read_only)
return 0444;

return attr->mode;
}

Expand Down Expand Up @@ -945,6 +958,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->word_size = config->word_size ?: 1;
nvmem->size = config->size;
nvmem->root_only = config->root_only;
nvmem->sysfs_read_only = config->sysfs_read_only;
nvmem->priv = config->priv;
nvmem->type = config->type;
nvmem->keepout = config->keepout;
Expand Down
2 changes: 2 additions & 0 deletions drivers/nvmem/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct nvmem_device {
size_t size;
bool read_only;
bool root_only;
/* See nvmem_config.sysfs_read_only */
bool sysfs_read_only;
int flags;
enum nvmem_type type;
struct bin_attribute eeprom;
Expand Down
4 changes: 4 additions & 0 deletions include/linux/nvmem-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ struct nvmem_cell_info {
* @type: Type of the nvmem storage
* @read_only: Device is read-only.
* @root_only: Device is accessibly to root only.
* @sysfs_read_only: The device's whole-device sysfs file is read-only for
* good: force_ro cannot make it writable. Kernel consumers still
* write its cells.
* @of_node: If given, this will be used instead of the parent's of_node.
* @reg_read: Callback to read data; return zero if successful.
* @reg_write: Callback to write data; return zero if successful.
Expand Down Expand Up @@ -123,6 +126,7 @@ struct nvmem_config {
enum nvmem_type type;
bool read_only;
bool root_only;
bool sysfs_read_only;
bool ignore_wp;
struct nvmem_layout *layout;
struct device_node *of_node;
Expand Down
Loading