* [PATCH v2 0/2] nvmem: Add byte-mask support for non-contiguous byte selection
@ 2026-11-16 2:58 Hongmei Xu
2026-11-16 2:58 ` [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property " Hongmei Xu
2026-11-16 2:58 ` [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes Hongmei Xu
0 siblings, 2 replies; 5+ messages in thread
From: Hongmei Xu @ 2026-11-16 2:58 UTC (permalink / raw)
To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Rafał Miłecki
Cc: devicetree, linux-kernel, Hongmei Xu
Some hardware stores NVMEM cell data in non-contiguous bytes. For
example, a MAC address cell may span 7 physical bytes with one byte
unused, requiring the driver to skip that byte during readout.
The existing 'bits' property only handles sub-byte granularity and
cannot express byte-level gaps. This series adds a 'byte-mask'
property to the fixed-cell binding and implements it in the nvmem
core, allowing consumers to describe and read such cells.
An example: assume a MAC address is assembled from efuse registers
0xFFFF4320[31:0] (bytes 0-3), 0xFFFF4324[7:0] (byte 4), and
0xFFFF4324[23:16] (byte 6), with 0xFFFF4324[15:8] unused (byte 5).
Map the efuse region as an 8-byte nvmem device and define the cell
skipping byte 5:
efuse: efuse@ffff4320 {
compatible = "...";
reg = <0x0 0xffff4320 0x0 0x8>;
#address-cells = <1>;
#size-cells = <1>;
mac_addr: mac-addr@0 {
reg = <0x0 0x6>;
byte-mask = <0x5f>;
};
};
With byte-mask = 0x5f (0101_1111), bytes 0-4 and 6 are selected
(byte 5 is skipped), forming the 6-byte MAC address output.
Signed-off-by: Hongmei Xu <hongmei.xu@oss.qualcomm.com>
---
Changes in v2:
- Move nvmem_validate_byte_mask() before the nbits block so byte_mask+nbits
is rejected even when nbits is set (mutual exclusion bypass fix).
- Run nvmem_validate_byte_mask() after fixup_dt_cell_info() in the DT path
so the provider callback can adjust cell info before validation.
- Set cell->raw_len = fls(byte_mask) instead of nvmem->size - cell->offset,
limiting the physical read to only the bytes needed; avoids large
allocations and read failures on providers with protected trailing regions
(e.g. sec-qfprom).
- Tighten the device-size check to ALIGN(fls(byte_mask), word_size) so
word-alignment cannot push the read past the device end.
- Zero-fill output bytes beyond the last selected byte with memset.
- Simplify compaction loop guard from i < raw_len && i < 32 to i < raw_len
since raw_len is now bounded by fls(byte_mask) <= 32.
- Link to v1: https://patch.msgid.link/20261113-nvmem-byte-mask-for-split-macaddr-v1-0-138f715f18c1@oss.qualcomm.com
---
Hongmei Xu (2):
dt-bindings: nvmem: add byte-mask property for non-contiguous byte selection
nvmem: core: add byte-mask support for non-contiguous cell bytes
.../bindings/nvmem/layouts/fixed-cell.yaml | 21 +++++
drivers/nvmem/core.c | 105 +++++++++++++++++++--
include/linux/nvmem-provider.h | 8 ++
3 files changed, 127 insertions(+), 7 deletions(-)
---
base-commit: d26955e57475434b59d43ba372b2a50d33d5e0ed
change-id: 20261110-nvmem-byte-mask-for-split-macaddr-a4698a128057
Best regards,
--
Hongmei Xu <hongmei.xu@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property for non-contiguous byte selection 2026-11-16 2:58 [PATCH v2 0/2] nvmem: Add byte-mask support for non-contiguous byte selection Hongmei Xu @ 2026-11-16 2:58 ` Hongmei Xu 2026-11-16 3:05 ` sashiko-bot 2026-11-16 2:58 ` [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes Hongmei Xu 1 sibling, 1 reply; 5+ messages in thread From: Hongmei Xu @ 2026-11-16 2:58 UTC (permalink / raw) To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Rafał Miłecki Cc: devicetree, linux-kernel, Hongmei Xu Add the byte-mask property to the fixed-cell binding. This allows selecting non-contiguous physical bytes from an NVMEM region to form a logical cell value. Some SoCs (e.g. Qualcomm efuse controllers) store a logical value such as a MAC address across non-contiguous physical bytes, with one or more bytes in between reserved or unused. Multiple reg entries would require the consumer to assemble the value from separate cells; a device-specific layout would need a custom provider for a common pattern. byte-mask lets the DT describe this directly in the fixed-cell binding. Bit N set in byte-mask means physical byte N relative to the cell's base offset is included in the output; selected bytes are packed into the output buffer in ascending byte order. The number of set bits must not exceed the cell's logical size given by reg. byte-mask and bits are mutually exclusive. Cells using byte-mask are read-only. Signed-off-by: Hongmei Xu <hongmei.xu@oss.qualcomm.com> --- .../bindings/nvmem/layouts/fixed-cell.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml index 38e3ad50ff4f..46e1101ee98a 100644 --- a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml +++ b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml @@ -34,6 +34,20 @@ properties: description: Size in bit within the address range specified by reg. + byte-mask: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + description: > + Bitmask selecting which physical bytes within the NVMEM device region + contribute to this cell's output. Bit N set means physical byte N + relative to the cell's base offset is included; selected bytes are + packed into the output buffer in ascending byte order. Byte positions + are relative to the cell's base offset and limited to a 32-byte window. + The highest set bit must not reference a byte beyond the device size. + The number of set bits must not exceed the cell's logical size given by + reg. Cannot be used together with bits. Cells using byte-mask are + read-only; write operations are not supported. + allOf: - if: properties: @@ -48,6 +62,13 @@ allOf: const: 1 required: - "#nvmem-cell-cells" + - if: + required: [ byte-mask ] + then: + properties: + bits: false + description: + byte-mask and bits are mutually exclusive. required: - reg -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property for non-contiguous byte selection 2026-11-16 2:58 ` [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property " Hongmei Xu @ 2026-11-16 3:05 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-16 3:05 UTC (permalink / raw) To: Hongmei Xu; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dt-bindings: nvmem: byte-mask redefines reg size and accesses beyond reg -- commit 66779220f836f809e1148ff64ede0801ef303fad Author: Hongmei Xu <hongmei.xu@oss.qualcomm.com> dt-bindings: nvmem: add byte-mask property for non-contiguous byte selection This commit adds the byte-mask property to the fixed-cell NVMEM binding to select non-contiguous physical bytes within a 32-byte window relative to a cell's base offset. > diff --git a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml > index 38e3ad50ff4fb..46e1101ee98ae 100644 > --- a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml > +++ b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml > @@ -34,6 +34,20 @@ properties: > description: > Size in bit within the address range specified by reg. > > + byte-mask: > + $ref: /schemas/types.yaml#/definitions/uint32 > + minimum: 1 > + description: > > + Bitmask selecting which physical bytes within the NVMEM device region > + contribute to this cell's output. Bit N set means physical byte N > + relative to the cell's base offset is included; selected bytes are > + packed into the output buffer in ascending byte order. Byte positions > + are relative to the cell's base offset and limited to a 32-byte window. > + The highest set bit must not reference a byte beyond the device size. > + The number of set bits must not exceed the cell's logical size given by > + reg. Cannot be used together with bits. Cells using byte-mask are > + read-only; write operations are not supported. [Severity: Medium] Does byte-mask redefine the size cell of reg as a logical output size while allowing physical reads past the declared reg range? Any non-contiguous selection of K bytes has a highest set bit H >= K, which references physical bytes at offset + H outside a reg = <offset K> range. Unlike the existing bits property, which stays within the address range specified by reg, setting reg = <offset K> causes physical accesses beyond the cell's declared reg range and hides overlaps between sibling cells. Conversely, setting reg = <offset M> to cover the full physical span M > K leaves the contents of the trailing M - K output bytes unspecified in the binding and causes consumers such as nvmem_get_mac_address() and nvmem_cell_read_u32() to see an unexpected cell size M instead of K. Also, the commit message notes that multiple reg entries would require the consumer to assemble the value from separate cells, but multiple reg tuples on a single cell node (for example, reg = <offset1 len1>, <offset2 len2>) describe one cell with discontiguous physical ranges without redefining reg or limiting the span to 32 bytes. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261116-nvmem-byte-mask-for-split-macaddr-v2-0-96f4a0c08264@oss.qualcomm.com?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes 2026-11-16 2:58 [PATCH v2 0/2] nvmem: Add byte-mask support for non-contiguous byte selection Hongmei Xu 2026-11-16 2:58 ` [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property " Hongmei Xu @ 2026-11-16 2:58 ` Hongmei Xu 2026-11-16 3:13 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Hongmei Xu @ 2026-11-16 2:58 UTC (permalink / raw) To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Rafał Miłecki Cc: devicetree, linux-kernel, Hongmei Xu Add support for a byte-mask cell property that selects non-contiguous physical bytes from an NVMEM device region to form a logical cell value. When byte-mask is set, bit N set in the mask means physical byte N is included; selected bytes are packed into the output buffer in ascending byte order. If fewer bytes are selected than the cell's logical size given by reg, the remaining output bytes are zero-filled. Four checks are performed at cell registration: - byte-mask and bits/bit-offset are mutually exclusive. - The cell offset must not exceed the device size. - The number of set bits must not exceed the cell's logical size (reg), to prevent output buffer overflow. - The highest set bit must not reference a byte beyond the NVMEM device size. Signed-off-by: Hongmei Xu <hongmei.xu@oss.qualcomm.com> --- drivers/nvmem/core.c | 105 ++++++++++++++++++++++++++++++++++++++--- include/linux/nvmem-provider.h | 8 ++++ 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 7d4cce8e2742..7e96b84bc4e2 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -33,6 +33,7 @@ struct nvmem_cell_entry { int bytes; int bit_offset; int nbits; + u32 byte_mask; nvmem_cell_post_process_t read_post_process; void *priv; struct device_node *np; @@ -589,6 +590,47 @@ static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell) blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); } +static int nvmem_validate_byte_mask(struct nvmem_device *nvmem, + const struct nvmem_cell_info *info) +{ + int out_bytes; + + if (!info->byte_mask) + return 0; + + if (info->nbits || info->bit_offset) { + dev_err(&nvmem->dev, + "cell %s byte-mask and bits are mutually exclusive\n", + info->name ?: "<unknown>"); + return -EINVAL; + } + + if (info->offset >= nvmem->size) { + dev_err(&nvmem->dev, + "cell %s offset %u exceeds device size %zu\n", + info->name ?: "<unknown>", info->offset, nvmem->size); + return -EINVAL; + } + + out_bytes = hweight32(info->byte_mask); + if (out_bytes > info->bytes) { + dev_err(&nvmem->dev, + "cell %s byte-mask set-bit count %d exceeds reg size %d\n", + info->name ?: "<unknown>", out_bytes, info->bytes); + return -EINVAL; + } + + if (ALIGN(fls(info->byte_mask), nvmem->word_size) > nvmem->size - info->offset) { + dev_err(&nvmem->dev, + "cell %s byte-mask bit %d exceeds device size %zu\n", + info->name ?: "<unknown>", + fls(info->byte_mask) - 1, nvmem->size); + return -EINVAL; + } + + return 0; +} + static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem, const struct nvmem_cell_info *info, struct nvmem_cell_entry *cell) @@ -603,12 +645,22 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem, cell->bit_offset = info->bit_offset; cell->nbits = info->nbits; + cell->byte_mask = info->byte_mask; cell->np = info->np; + if (cell->byte_mask) { + int rc = nvmem_validate_byte_mask(nvmem, info); + + if (rc) + return rc; + } + if (cell->nbits) { cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, BITS_PER_BYTE); cell->raw_len = ALIGN(cell->bytes, nvmem->word_size); + } else if (cell->byte_mask) { + cell->raw_len = fls(info->byte_mask); } if (!IS_ALIGNED(cell->offset, nvmem->stride)) { @@ -804,7 +856,7 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) { struct device *dev = &nvmem->dev; const __be32 *addr; - int len, ret; + int len, ret = 0; for_each_child_of_node_scoped(np, child) { struct nvmem_cell_info info = {0}; @@ -829,16 +881,27 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) info.nbits < 1 || info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) { dev_err(dev, "nvmem: invalid bits on %pOF\n", child); - return -EINVAL; + ret = -EINVAL; + goto err_free_name; } } + of_property_read_u32(child, "byte-mask", &info.byte_mask); + info.np = of_node_get(child); if (nvmem->fixup_dt_cell_info) nvmem->fixup_dt_cell_info(nvmem, &info); + if (info.byte_mask) { + ret = nvmem_validate_byte_mask(nvmem, &info); + if (ret) + goto err_free_name; + } + ret = nvmem_add_one_cell(nvmem, &info); + +err_free_name: kfree(info.name); if (ret) { of_node_put(info.np); @@ -1648,28 +1711,52 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem, struct nvmem_cell_entry *cell, void *buf, size_t *len, const char *id, int index) { + u8 *rawbuf = NULL; + void *readbuf = buf; int rc; - rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len); + if (cell->byte_mask) { + rawbuf = kzalloc(cell->raw_len, GFP_KERNEL); + if (!rawbuf) + return -ENOMEM; + readbuf = rawbuf; + } + + rc = nvmem_reg_read(nvmem, cell->offset, readbuf, cell->raw_len); if (rc) - return rc; + goto err_exit; /* shift bits in-place */ if (cell->bit_offset || cell->nbits) nvmem_shift_read_buffer_in_place(cell, buf); + /* compact non-contiguous bytes selected by byte_mask */ + if (cell->byte_mask) { + int out_idx = 0; + int i; + + for (i = 0; i < cell->raw_len; i++) { + if (cell->byte_mask & BIT(i)) + ((u8 *)buf)[out_idx++] = rawbuf[i]; + } + memset((u8 *)buf + out_idx, 0, cell->bytes - out_idx); + } + if (cell->read_post_process) { rc = cell->read_post_process(cell->priv, id, index, - cell->offset, buf, cell->raw_len); + cell->offset, buf, + cell->byte_mask ? cell->bytes : cell->raw_len); if (rc) - return rc; + goto err_exit; } if (len) *len = cell->bytes; - return 0; +err_exit: + kfree(rawbuf); + return rc; } /** @@ -1776,6 +1863,10 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si if (cell->read_post_process) return -EINVAL; + /* byte-mask cells have no scatter-write implementation yet */ + if (cell->byte_mask) + return -EINVAL; + if (cell->bit_offset || cell->nbits) { if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes) return -EINVAL; diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 6063fe5b7784..066d27bb4284 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -57,6 +57,13 @@ struct nvmem_keepout { * @bytes: Length of the cell. * @bit_offset: Bit offset if cell is smaller than a byte. * @nbits: Number of bits. + * @byte_mask: Bitmask selecting physical bytes to include in the output. + * Bit N=1 means physical byte N relative to the cell's base + * offset is packed into the output buffer; bit N=0 means skip. + * The physical read window extends to the highest set bit, + * aligned to the device word size. The number of set bits must + * not exceed the cell's logical size (bytes). Unselected output + * positions are zero-filled. Zero means no masking. * @np: Optional device_node pointer. * @read_post_process: Callback for optional post processing of cell data * on reads. @@ -69,6 +76,7 @@ struct nvmem_cell_info { unsigned int bytes; unsigned int bit_offset; unsigned int nbits; + u32 byte_mask; struct device_node *np; nvmem_cell_post_process_t read_post_process; void *priv; -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes 2026-11-16 2:58 ` [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes Hongmei Xu @ 2026-11-16 3:13 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-11-16 3:13 UTC (permalink / raw) To: Hongmei Xu; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] nvmem: core: unaligned raw_len in nvmem_cell_info_to_nvmem_cell_entry_nodup() - [Low] nvmem: core: redundant nvmem_validate_byte_mask() in nvmem_add_cells_from_dt() -- commit ef65d0ead813d5be4bc32f2283c1a2c9aee24659 Author: Hongmei Xu <hongmei.xu@oss.qualcomm.com> nvmem: core: add byte-mask support for non-contiguous cell bytes This commit adds a byte-mask cell property to select and pack non-contiguous physical bytes from an NVMEM region into a logical cell value. > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > index 7d4cce8e27424..7e96b84bc4e28 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c [ ... ] > @@ -603,12 +645,22 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem, > > cell->bit_offset = info->bit_offset; > cell->nbits = info->nbits; > + cell->byte_mask = info->byte_mask; > cell->np = info->np; > > + if (cell->byte_mask) { > + int rc = nvmem_validate_byte_mask(nvmem, info); > + > + if (rc) > + return rc; > + } > + > if (cell->nbits) { > cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, > BITS_PER_BYTE); > cell->raw_len = ALIGN(cell->bytes, nvmem->word_size); > + } else if (cell->byte_mask) { > + cell->raw_len = fls(info->byte_mask); > } [Severity: Medium] Should cell->raw_len be aligned to nvmem->word_size here, as the cell->nbits branch and nvmem_validate_byte_mask() do? Leaving cell->raw_len as fls(info->byte_mask) triggers the !IS_ALIGNED(cell->raw_len, nvmem->word_size) check just below whenever nvmem->word_size > 1 and fls(info->byte_mask) is not a multiple of word_size: drivers/nvmem/core.c:nvmem_cell_info_to_nvmem_cell_entry_nodup() { ... if (!IS_ALIGNED(cell->raw_len, nvmem->word_size)) { dev_err(&nvmem->dev, "cell %s raw len %zd unaligned to nvmem word size %d\n", cell->name ?: "<unknown>", cell->raw_len, nvmem->word_size); if (info->raw_len) return -EINVAL; cell->raw_len = ALIGN(cell->raw_len, nvmem->word_size); } } That logs a false error and, if info->raw_len was set (for example by a fixup_dt_cell_info callback), fails cell registration with -EINVAL. [ ... ] > @@ -829,16 +881,27 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) [ ... ] > + of_property_read_u32(child, "byte-mask", &info.byte_mask); > + > info.np = of_node_get(child); > > if (nvmem->fixup_dt_cell_info) > nvmem->fixup_dt_cell_info(nvmem, &info); > > + if (info.byte_mask) { > + ret = nvmem_validate_byte_mask(nvmem, &info); > + if (ret) > + goto err_free_name; > + } > + > ret = nvmem_add_one_cell(nvmem, &info); [Severity: Low] This isn't a bug, but nvmem_add_one_cell() -> nvmem_cell_info_to_nvmem_cell_entry() -> nvmem_cell_info_to_nvmem_cell_entry_nodup() already calls nvmem_validate_byte_mask(nvmem, &info), so this check in nvmem_add_cells_from_dt() is redundant. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261116-nvmem-byte-mask-for-split-macaddr-v2-0-96f4a0c08264@oss.qualcomm.com?part=2 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-16 3:13 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-16 2:58 [PATCH v2 0/2] nvmem: Add byte-mask support for non-contiguous byte selection Hongmei Xu 2026-11-16 2:58 ` [PATCH v2 1/2] dt-bindings: nvmem: add byte-mask property " Hongmei Xu 2026-11-16 3:05 ` sashiko-bot 2026-11-16 2:58 ` [PATCH v2 2/2] nvmem: core: add byte-mask support for non-contiguous cell bytes Hongmei Xu 2026-11-16 3:13 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox