From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH v3 1/9] nvmem: Add 'protect' operation to core framework
Date: Thu, 12 Jun 2025 08:58:04 +0200 [thread overview]
Message-ID: <20250612065812.2025665-2-o.rempel@pengutronix.de> (raw)
In-Reply-To: <20250612065812.2025665-1-o.rempel@pengutronix.de>
Introduce a generic "protect" operation to the NVMEM framework.
This allows NVMEM providers to expose hardware-specific protection or
locking mechanisms through the character device interface. Existing
read/write operations do not cover this type of state-altering
protection.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
changes v2:
- rebase against latest nvmem changes
- use (offset + count > nvmem->size) instead of (offset >= nvmem->size
|| count > nvmem->size - offset)
- remove NVMEM_PROTECT_ENABLE_WRITE and NVMEM_PROTECT_DISABLE_WRITE
they will be added later as non nvmem specific defines
---
drivers/nvmem/core.c | 32 ++++++++++++++++++++++++++++++++
drivers/nvmem/partition.c | 7 +++++++
include/linux/nvmem-provider.h | 2 ++
3 files changed, 41 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 7acd26474b50..68cb27093227 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -30,6 +30,8 @@ struct nvmem_device {
const void *val, size_t val_size);
int (*reg_read)(void *ctx, unsigned int reg,
void *val, size_t val_size);
+ int (*reg_protect)(void *ctx, unsigned int reg,
+ size_t bytes, int prot);
};
struct nvmem_cell {
@@ -87,9 +89,38 @@ static ssize_t nvmem_cdev_write(struct cdev *cdev, const void *buf, size_t count
return retlen;
}
+static int nvmem_cdev_protect(struct cdev *cdev, size_t count, loff_t offset,
+ int prot)
+{
+ struct nvmem_device *nvmem;
+
+ nvmem = container_of(cdev, struct nvmem_device, cdev);
+
+ dev_dbg(cdev->dev, "protect ofs: 0x%08llx count: 0x%08zx prot: %d\n",
+ offset, count, prot);
+
+ if (!nvmem->reg_protect) {
+ dev_warn(cdev->dev, "NVMEM device %s does not support protect operation\n",
+ nvmem->name);
+ return -EOPNOTSUPP;
+ }
+
+ if (!count)
+ return 0;
+
+ if (offset + count > nvmem->size) {
+ dev_err(cdev->dev, "protect range out of bounds (ofs: 0x%08llx, count 0x%08zx, size 0x%08zx)\n",
+ offset, count, nvmem->size);
+ return -EINVAL;
+ }
+
+ return nvmem->reg_protect(nvmem->priv, offset, count, prot);
+}
+
static struct cdev_operations nvmem_chrdev_ops = {
.read = nvmem_cdev_read,
.write = nvmem_cdev_write,
+ .protect = nvmem_cdev_protect,
};
static int nvmem_register_cdev(struct nvmem_device *nvmem, const char *name)
@@ -207,6 +238,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.parent = config->dev;
nvmem->reg_read = config->reg_read;
nvmem->reg_write = config->reg_write;
+ nvmem->reg_protect = config->reg_protect;
np = config->cdev ? cdev_of_node(config->cdev) : config->dev->of_node;
nvmem->dev.of_node = np;
nvmem->priv = config->priv;
diff --git a/drivers/nvmem/partition.c b/drivers/nvmem/partition.c
index d1127c1401af..bb6dbad7b5f6 100644
--- a/drivers/nvmem/partition.c
+++ b/drivers/nvmem/partition.c
@@ -18,6 +18,12 @@ static int nvmem_cdev_read(void *ctx, unsigned offset, void *buf, size_t bytes)
return cdev_read(ctx, buf, bytes, offset, 0);
}
+static int nvmem_cdev_protect(void *ctx, unsigned int offset, size_t bytes,
+ int prot)
+{
+ return cdev_protect(ctx, bytes, offset, prot);
+}
+
static int nvmem_cells_probe(struct device *dev)
{
struct nvmem_config config = {};
@@ -37,6 +43,7 @@ static int nvmem_cells_probe(struct device *dev)
config.size = cdev->size;
config.reg_read = nvmem_cdev_read;
config.reg_write = nvmem_cdev_write;
+ config.reg_protect = nvmem_cdev_protect;
return PTR_ERR_OR_ZERO(nvmem_register(&config));
}
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index c1765673eb23..4d44468456f0 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -34,6 +34,8 @@ struct nvmem_config {
const void *val, size_t val_size);
int (*reg_read)(void *ctx, unsigned int reg,
void *val, size_t val_size);
+ int (*reg_protect)(void *ctx, unsigned int offset,
+ size_t bytes, int prot);
void *priv;
nvmem_cell_post_process_t cell_post_process;
};
--
2.39.5
next prev parent reply other threads:[~2025-06-12 6:59 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-12 6:58 [PATCH v3 0/9] NVMEM: Introduce write protection support Oleksij Rempel
2025-06-12 6:58 ` Oleksij Rempel [this message]
2025-06-12 6:58 ` [PATCH v3 2/9] nvmem: rmem: add write and protect support Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 3/9] commands: nvmem: Add support for creating dynamic rmem devices Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 4/9] regmap: Add reg_seal operation for hardware protection Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 5/9] nvmem: regmap: Implement protect operation using regmap_seal Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 6/9] nvmem: bsec: Implement NVMEM protect via regmap_seal for OTP locking Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 7/9] nvmem: rmem: ensure unique device name per instance Oleksij Rempel
2025-06-12 6:58 ` [PATCH v3 8/9] fs: Report errors for out-of-bounds protect operations Oleksij Rempel
2025-06-17 7:56 ` Sascha Hauer
2025-06-12 6:58 ` [PATCH v3 9/9] test: Add pytest suite for NVMEM framework Oleksij Rempel
2025-06-17 8:03 ` Sascha Hauer
2025-06-17 8:17 ` Oleksij Rempel
2025-06-17 8:04 ` (subset) [PATCH v3 0/9] NVMEM: Introduce write protection support Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250612065812.2025665-2-o.rempel@pengutronix.de \
--to=o.rempel@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox