Skip to contentSkip to conversation

regmap: clairify max_register meaning

RFC In review Stefan Agner 2 participants 4 messages Jan 18, 2016 – Jan 19, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 regmap: clairify max_register meaning

The exact meaning of max_register is not entirely clear. Define it to be the maximum address offset a register in a regmap can actually have. This seems to be the interpretation most commonly used.

Fix regcache-flat to follow this definition.

---
Hi Mark,

I stumbled upon this while switching to regmap cache FLAT for the FSL DCU and PWM FTM drivers.

Maybe I see something completely wrong here, but to me it seems that the regcache-flat.c is the only code interpreting max_register as register indexes rather then the maximum relative address... At least regmap.c compares with range_max, which seems to use addresses.

If this is right, we should probably check the max_register values of the drivers using REGCACHE_FLAT before fixing this... Some drivers I checked seem to pass the maximum register address already.

--
Stefan

 drivers/base/regmap/regcache-flat.c | 3 +--
 include/linux/regmap.h              | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/base/regmap/regcache-flat.c +1 -2
index 686c9e0..fe997c1 100644
@@ -21,8 +21,7 @@ static int regcache_flat_init(struct regmap *map)
 	int i;
 	unsigned int *cache;
 
-	map->cache = kcalloc(map->max_register + 1, sizeof(unsigned int),
-			     GFP_KERNEL);
+	map->cache = kzalloc(map->max_register + map->reg_stride, GFP_KERNEL);
 	if (!map->cache)
 		return -ENOMEM;
 
include/linux/regmap.h +1 -1
index 1839434..27aaac9 100644
@@ -162,7 +162,7 @@ typedef void (*regmap_unlock)(void *);
  *		  This field is a duplicate of a similar file in
  *		  'struct regmap_bus' and serves exact same purpose.
  *		   Use it only for "no-bus" cases.
- * @max_register: Optional, specifies the maximum valid register index.
+ * @max_register: Optional, specifies the maximum valid register address.
  * @wr_table:     Optional, points to a struct regmap_access_table specifying
  *                valid ranges for write access.
  * @rd_table:     As above, for read access.
Signature · 1 line
2.7.0
maintainer
#1 lore raw

On Sun, Jan 17, 2016 at 10:52:56PM -0800, Stefan Agner wrote:

Please submit one change per patch. You've mixed a change to the documentation with a code here and it's really unclear what either of them are supposed to do.

Quoted 3 lines

Maybe I see something completely wrong here, but to me it seems that the regcache-flat.c is the only code interpreting max_register as register indexes rather then the maximum relative address...

I don't think I understand what the above means, sorry. What is a "relative address" in the context of a register map?

Quoted 2 lines

At least regmap.c compares with range_max, which seems to use addresses.

That's a fairly large source file, can you be more specific please?

drivers/base/regmap/regcache-flat.c static int regcache_flat_init(struct regmap *map) · line 24

Quoted 3 lines
drivers/base/regmap/regcache-flat.c +1 -2
-	map->cache = kcalloc(map->max_register + 1, sizeof(unsigned int),
-			     GFP_KERNEL);
+	map->cache = kzalloc(map->max_register + map->reg_stride, GFP_KERNEL);

This isn't very obvious and appears broken for a lot of devices. It does two things, it converts from allocating an array of unsigned integers to an array of bytes and it replaces the handling of address 0 by adding an extra element to the array with adding stride bytes on the end of the array. This means that for a regmap with 16 bit values and a stride of 1 we'll end up allocating nowhere the space we need as we'll only allocate one byte per register plus an extra byte at the end but the implementation of the flat cache is treating the cache as an array of unsigned ints so needs at least four bytes per register. It should wash out as the same thing if the stride is equal to the size of an unsigned int but most other cases will be broken.

Your changelog doesn't actually say this but I think what this is trying to do is make the cache more memory efficient by not allocating space for the registers that don't exist due to striding (ie, if we have a 4 byte stride then currently we'll allocate space for 4 times as many registers as actually exist). That's a reasonable goal but I don't immediately have a good idea for doing it bearing in mind that we are very performance sensitive here.

Quoted 2 lines

- * @max_register: Optional, specifies the maximum valid register index.
+ * @max_register: Optional, specifies the maximum valid register address.

This is reasonable (index and address are synonyms here).

#2 lore raw
Quoted 5 lines · Mark Brown

On Sun, Jan 17, 2016 at 10:52:56PM -0800, Stefan Agner wrote:

Please submit one change per patch. You've mixed a change to the documentation with a code here and it's really unclear what either of them are supposed to do.

Sure, one change per patch. I interpreted this issue as "one change" since the interpretation of the doc lead to this implementation issue...

But I see, the issue in regcache-flat.c could be a completely independent issue.

Quoted 7 lines
Quoted 4 lines

Maybe I see something completely wrong here, but to me it seems that the regcache-flat.c is the only code interpreting max_register as register indexes rather then the maximum relative address...

I don't think I understand what the above means, sorry. What is a "relative address" in the context of a register map?

My interpretation:

relative address = index * stride
absolute address = base address + relative address

For the driver at hand I was about to set max_register to (max relative address / 4), since that matches my interpretation of index and at first sight seem to mach what is done in regcache-flat.c (it even works fine, at least for 4 byte registers)....

Quoted 6 lines
Quoted 3 lines

At least regmap.c compares with range_max, which seems to use addresses.

That's a fairly large source file, can you be more specific please?

e.g. in __regmap_init, just after the label skip_format_initialization. (more on that see below)

Quoted 7 lines

drivers/base/regmap/regcache-flat.c static int regcache_flat_init(struct regmap *map) · line 24

Quoted 3 lines
drivers/base/regmap/regcache-flat.c +1 -2
-	map->cache = kcalloc(map->max_register + 1, sizeof(unsigned int),
-			     GFP_KERNEL);
+	map->cache = kzalloc(map->max_register + map->reg_stride, GFP_KERNEL);

This isn't very obvious and appears broken for a lot of devices. It does two things, it converts from allocating an array of unsigned integers to an array of bytes and it replaces the handling of address 0

Quoted 8 lines

by adding an extra element to the array with adding stride bytes on the end of the array. This means that for a regmap with 16 bit values and a stride of 1 we'll end up allocating nowhere the space we need as we'll only allocate one byte per register plus an extra byte at the end but the implementation of the flat cache is treating the cache as an array of unsigned ints so needs at least four bytes per register. It should wash out as the same thing if the stride is equal to the size of an unsigned int but most other cases will be broken.

I see, agreed, my code is certainly broken if strides < sizeof(*reg_value).

Quoted 8 lines

Your changelog doesn't actually say this but I think what this is trying to do is make the cache more memory efficient by not allocating space for the registers that don't exist due to striding (ie, if we have a 4 byte stride then currently we'll allocate space for 4 times as many registers as actually exist). That's a reasonable goal but I don't immediately have a good idea for doing it bearing in mind that we are very performance sensitive here.

Yeah I saw a bug in that, but I see that this is actually just a not very space efficient implementation detail.

Quoted 5 lines
Quoted 3 lines

- * @max_register: Optional, specifies the maximum valid register index.
+ * @max_register: Optional, specifies the maximum valid register address.

This is reasonable (index and address are synonyms here).

Ok, in that case I clearly interpreted index wrong.

Btw, range_min and range_max in struct regmap_range_cfg are defined as memory addresses:
...
* @range_min: Address of the lowest register address in virtual range.

* @range_max: Address of the highest register in virtual range.
...

Will send out a patch for the documentation change only, if you think that this is a reasonable change too?

--
Stefan

maintainer
#3 lore raw
Quoted 6 lines · Stefan Agner
Quoted 3 lines · Mark Brown
Quoted 3 lines

Maybe I see something completely wrong here, but to me it seems that the regcache-flat.c is the only code interpreting max_register as register indexes rather then the maximum relative address...

Quoted 7 lines
Quoted 2 lines

I don't think I understand what the above means, sorry. What is a "relative address" in the context of a register map?

My interpretation:

relative address = index * stride
absolute address = base address + relative address

Oh, OK. I would have got that for absolute address for MMIO but definitely not relative address. For relative addresses I'd interpret index and address as synonyms.

Quoted 10 lines
Quoted 7 lines

Your changelog doesn't actually say this but I think what this is trying to do is make the cache more memory efficient by not allocating space for the registers that don't exist due to striding (ie, if we have a 4 byte stride then currently we'll allocate space for 4 times as many registers as actually exist). That's a reasonable goal but I don't immediately have a good idea for doing it bearing in mind that we are very performance sensitive here.

Yeah I saw a bug in that, but I see that this is actually just a not very space efficient implementation detail.

Indeed. It is true that having that 300% overhead in a common case is a bit annoying so if you do have any ideas for fixing that without taking a performance hit that'd be really good.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

- * @max_register: Optional, specifies the maximum valid register index.
+ * @max_register: Optional, specifies the maximum valid register address.

This is reasonable (index and address are synonyms here).

Ok, in that case I clearly interpreted index wrong.

Quoted 7 lines

Btw, range_min and range_max in struct regmap_range_cfg are defined as memory addresses:
...
* @range_min: Address of the lowest register address in virtual range.

* @range_max: Address of the highest register in virtual range.
...

Not memory addresses, register addresses. Remember that most regmap register maps are not memory mapped at all but rather are completely separate ranges on some external device (especially things using ranges, you're normally not short enough on addresses on a memory mapped device to bother with a window) so when looking at memory mapped devices it's best to think only of the area that's actually mapped into the regmap.

Quoted 2 lines

Will send out a patch for the documentation change only, if you think that this is a reasonable change too?

Yes, please do.