Skip to contentSkip to conversation

nvmem: regmap: Fix nvmem size

PATCH Applied Robin van der Gracht 3 participants 7 messages Dec 20, 2023 – Jan 2, 2024 NVMEM framework

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvmem: regmap: Fix nvmem size

We should add 1 to the max_register index since counting is zero based.

i.e. the stm32mp151 bsec has registers 0 - 95 with reg_stride 4. Size should be (95 + 1) * 4 = 384 bytes otherwise we can't access bsec register 95 (last one).

regmap_size_bytes() does take the +1 into account so we can use that.

---

v2: Fix the size calculation in nvmem_regmap_read() as well.

 drivers/nvmem/regmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvmem/regmap.c +2 -2
index fa5405d7a8..b923c5787d 100644
@@ -38,7 +38,7 @@ static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes
 	skip_bytes = offset & (stride - 1);
 	rbytes = roundup(bytes + skip_bytes, stride);
 
-	if (roffset + rbytes > stride * regmap_get_max_register(map))
+	if (roffset + rbytes > regmap_size_bytes(map) * stride)
 		return -EINVAL;
 
 	for (i = roffset; i < roffset + rbytes; i += stride) {
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
 	config.priv = map;
 	config.stride = 1;
 	config.word_size = 1;
-	config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+	config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
 	config.cell_post_process = cell_post_process;
 	config.reg_write = nvmem_regmap_write;
 	config.reg_read = nvmem_regmap_read;
Signature · 1 line
2.40.1
#1 lore raw

Hello Robin,

Thanks for the fix.

drivers/nvmem/regmap.c static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes · line 41

Quoted 2 lines · Robin van der Gracht
drivers/nvmem/regmap.c +1 -1
-	if (roffset + rbytes > stride * regmap_get_max_register(map))
+	if (roffset + rbytes > regmap_size_bytes(map) * stride)

Shouldn't stride on the right hand side be dropped?

Cheers,
Ahmad

Quoted 12 lines
drivers/nvmem/regmap.c +1 -1
 		return -EINVAL;
 
 	for (i = roffset; i < roffset + rbytes; i += stride) {
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
 	config.priv = map;
 	config.stride = 1;
 	config.word_size = 1;
-	config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+	config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
 	config.cell_post_process = cell_post_process;
 	config.reg_write = nvmem_regmap_write;
 	config.reg_read = nvmem_regmap_read;
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw

Hi Ahmad,

Quoted 9 lines · Ahmad Fatoum

Hello Robin,

Thanks for the fix.

drivers/nvmem/regmap.c static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes · line 41

Quoted 2 lines · Robin van der Gracht
drivers/nvmem/regmap.c +1 -1
-	if (roffset + rbytes > stride * regmap_get_max_register(map))
+	if (roffset + rbytes > regmap_size_bytes(map) * stride)

Shouldn't stride on the right hand side be dropped?

roffset = register index * stride.

I.e. 380 for register with index 95.

For stm32mp1x bsec:
map->format.val_bytes = 4
map->reg_stride = 4

regmap_size_bytes() = map->format.val_bytes * (95 + 1) / map->reg_stride = 96

So the result with the stride on the right size is correct.

I moved stride from left to right to be consistent with the size calculation in nvmem_regmap_register_with_pp()

Kind regards,
Robin

Quoted 18 lines

Cheers,
Ahmad

Quoted 14 lines
drivers/nvmem/regmap.c +1 -1
 		return -EINVAL;
 
 	for (i = roffset; i < roffset + rbytes; i += stride) {
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, 
 const char *name,
 	config.priv = map;
 	config.stride = 1;
 	config.word_size = 1;
-	config.size = regmap_get_max_register(map) * 
 regmap_get_reg_stride(map);
+	config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
 	config.cell_post_process = cell_post_process;
drivers/nvmem/regmap.c +0 -0
 	config.reg_write = nvmem_regmap_write;
 	config.reg_read = nvmem_regmap_read;
#3 lore raw
Quoted 15 lines · Robin van der Gracht

Hi Ahmad,

Quoted 9 lines · Ahmad Fatoum

Hello Robin,

Thanks for the fix.

Quoted 2 lines · Robin van der Gracht

-    if (roffset + rbytes > stride * regmap_get_max_register(map)) +    if (roffset + rbytes > regmap_size_bytes(map) * stride)

Shouldn't stride on the right hand side be dropped?

roffset = register index * stride.

Quoted 7 lines

I.e. 380 for register with index 95.

For stm32mp1x bsec:
 map->format.val_bytes = 4
 map->reg_stride = 4

regmap_size_bytes() = map->format.val_bytes * (95 + 1) / map->reg_stride = 96

Ouch. What have I been thinking when I named regmap_size_bytes() this way...

Change looks ok then, but we should really rename regmap_size_bytes()...

Quoted 27 lines

So the result with the stride on the right size is correct.

I moved stride from left to right to be consistent with the size calculation in nvmem_regmap_register_with_pp()

Kind regards,
Robin

Quoted 16 lines

Cheers,
Ahmad

Quoted 12 lines

         return -EINVAL;

     for (i = roffset; i < roffset + rbytes; i += stride) {

drivers/nvmem/regmap.c +1 -1
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
      config.priv = map;
      config.stride = 1;
      config.word_size = 1;
-    config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+    config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
      config.cell_post_process = cell_post_process;
      config.reg_write = nvmem_regmap_write;
      config.reg_read = nvmem_regmap_read;
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Hello Robin,

Quoted 18 lines · Ahmad Fatoum
Quoted 17 lines · Robin van der Gracht

Hi Ahmad,

Quoted 9 lines · Ahmad Fatoum

Hello Robin,

Thanks for the fix.

Quoted 2 lines · Robin van der Gracht

-    if (roffset + rbytes > stride * regmap_get_max_register(map)) +    if (roffset + rbytes > regmap_size_bytes(map) * stride)

Shouldn't stride on the right hand side be dropped?

roffset = register index * stride.

I.e. 380 for register with index 95.

Quoted 8 lines
Quoted 5 lines

For stm32mp1x bsec:
 map->format.val_bytes = 4
 map->reg_stride = 4

regmap_size_bytes() = map->format.val_bytes * (95 + 1) / map->reg_stride = 96

Ouch. What have I been thinking when I named regmap_size_bytes() this way... Change looks ok then, but we should really rename regmap_size_bytes()...

After talking it over with Sascha, I see that the name is correct with regard to how the function was initially used. What's wrong it the implementation and that goes back to 2016..

I just sent out a series fixing this. md -s /dev/stm32-bsec 380 now works. Please verify if it's behaving as you expect.

Thanks,
Ahmad

Quoted 29 lines
Quoted 28 lines

So the result with the stride on the right size is correct.

I moved stride from left to right to be consistent with the size calculation in nvmem_regmap_register_with_pp()

Kind regards,
Robin

Quoted 16 lines

Cheers,
Ahmad

Quoted 12 lines

         return -EINVAL;

     for (i = roffset; i < roffset + rbytes; i += stride) {

drivers/nvmem/regmap.c +1 -1
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
      config.priv = map;
      config.stride = 1;
      config.word_size = 1;
-    config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+    config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
      config.cell_post_process = cell_post_process;
      config.reg_write = nvmem_regmap_write;
      config.reg_read = nvmem_regmap_read;
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Applied
#5 lore raw
In reply to Robin van der Gracht
Quoted 8 lines · Robin van der Gracht

We should add 1 to the max_register index since counting is zero based.

i.e. the stm32mp151 bsec has registers 0 - 95 with reg_stride 4. Size should be (95 + 1) * 4 = 384 bytes otherwise we can't access bsec register 95 (last one).

regmap_size_bytes() does take the +1 into account so we can use that.

Quoted 7 lines

---

v2: Fix the size calculation in nvmem_regmap_read() as well.

 drivers/nvmem/regmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks

Sascha

Quoted 28 lines
drivers/nvmem/regmap.c +2 -2
index fa5405d7a8..b923c5787d 100644
@@ -38,7 +38,7 @@ static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes
 	skip_bytes = offset & (stride - 1);
 	rbytes = roundup(bytes + skip_bytes, stride);
 
-	if (roffset + rbytes > stride * regmap_get_max_register(map))
+	if (roffset + rbytes > regmap_size_bytes(map) * stride)
 		return -EINVAL;
 
 	for (i = roffset; i < roffset + rbytes; i += stride) {
@@ -78,7 +78,7 @@ nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
 	config.priv = map;
 	config.stride = 1;
 	config.word_size = 1;
-	config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+	config.size = regmap_size_bytes(map) * regmap_get_reg_stride(map);
 	config.cell_post_process = cell_post_process;
 	config.reg_write = nvmem_regmap_write;
 	config.reg_read = nvmem_regmap_read;
Signature · 1 line
2.40.1
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw
Quoted 12 lines · Sascha Hauer
Quoted 11 lines · Robin van der Gracht

We should add 1 to the max_register index since counting is zero based.

i.e. the stm32mp151 bsec has registers 0 - 95 with reg_stride 4. Size should be (95 + 1) * 4 = 384 bytes otherwise we can't access bsec register 95 (last one).

regmap_size_bytes() does take the +1 into account so we can use that.

---

Quoted 6 lines
Quoted 4 lines

v2: Fix the size calculation in nvmem_regmap_read() as well.

 drivers/nvmem/regmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks

And un-applied again, see Ahmads patch

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |