Skip to contentSkip to conversation

regmap: allow to define reg_update_bits for no bus configuration

PATCH Superseded Ansuel Smith 2 participants 7 messages Nov 2, 2021 – Nov 4, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regmap: allow to define reg_update_bits for no bus configuration

Some device requires a special handling for reg_update_bits and can't use the normal regmap read write logic. An example is when locking is handled by the device and rmw operations requires to do atomic operations. Allow to declare a special function in regmap_config for reg_update_bits as we do with the bus config and append it to the regmap_volatile logic in _regmap_update_bits.

---
 drivers/base/regmap/regmap.c | 3 ++-
 include/linux/regmap.h       | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap.c +2 -1
index 21a0c2562ec0..d6fcb45194b0 100644
@@ -876,6 +876,7 @@ struct regmap *__regmap_init(struct device *dev,
 	if (!bus) {
 		map->reg_read  = config->reg_read;
 		map->reg_write = config->reg_write;
+		map->reg_update_bits = config->reg_update_bits;
 
 		map->defer_caching = false;
 		goto skip_format_initialization;
@@ -3064,7 +3065,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
 	if (change)
 		*change = false;
 
-	if (regmap_volatile(map, reg) && map->reg_update_bits) {
+	if ((regmap_volatile(map, reg) || !map->bus) && map->reg_update_bits) {
 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
 		if (ret == 0 && change)
 			*change = true;
include/linux/regmap.h +7 -0
index e3c9a25a853a..22652e5fbc38 100644
@@ -290,6 +290,11 @@ typedef void (*regmap_unlock)(void *);
  *		  read operation on a bus such as SPI, I2C, etc. Most of the
  *		  devices do not need this.
  * @reg_write:	  Same as above for writing.
+ * @reg_update_bits: Optional callback that if filled will be used to perform
+ *		     all the update_bits(rmw) operation. Should only be provided
+ *		     if the function require special handling with lock and reg
+ *		     handling and the operation cannot be represented as a simple
+ *		     update_bits operation on a bus such as SPI, I2C, etc.
  * @fast_io:	  Register IO is fast. Use a spinlock instead of a mutex
  *	     	  to perform locking. This field is ignored if custom lock/unlock
  *	     	  functions are used (see fields lock/unlock of struct regmap_config).
@@ -372,6 +377,8 @@ struct regmap_config {
 
 	int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
 	int (*reg_write)(void *context, unsigned int reg, unsigned int val);
+	int (*reg_update_bits)(void *context, unsigned int reg,
+			       unsigned int mask, unsigned int val);
 
 	bool fast_io;
 
Signature · 1 line
2.32.0
maintainer
#1 lore raw

drivers/base/regmap/regmap.c static int _regmap_update_bits(struct regmap *map, unsigned int reg, · line 3071

Quoted 9 lines · Ansuel Smith
drivers/base/regmap/regmap.c +1 -1
@@ -3064,7 +3065,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
 	if (change)
 		*change = false;
 
-	if (regmap_volatile(map, reg) && map->reg_update_bits) {
+	if ((regmap_volatile(map, reg) || !map->bus) && map->reg_update_bits) {
 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
 		if (ret == 0 && change)
 			*change = true;

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

#2 lore raw
Quoted 6 lines · Mark Brown
Quoted 4 lines · Ansuel Smith
drivers/base/regmap/regmap.c +0 -0
@@ -3064,7 +3065,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
 	if (change)
 		*change = false;
 
Quoted 11 lines

drivers/base/regmap/regmap.c static int _regmap_update_bits(struct regmap *map, unsigned int reg, · line 3071

Quoted 5 lines
drivers/base/regmap/regmap.c +1 -1
-	if (regmap_volatile(map, reg) && map->reg_update_bits) {
+	if ((regmap_volatile(map, reg) || !map->bus) && map->reg_update_bits) {
 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
 		if (ret == 0 && change)
 			*change = true;

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

I put the check there to not duplicate the code. The idea here is:
if we are doing a regmap_update_bits operation on a no bus configuration and the function have a dedicated reg_update_bits function, use that instead of the normal _reg_read, check and _reg_write.
To workaround the CACHE problem, I can add a check and detect if cache is disabled and only with that option permit to add a reg_update_bits function to the map (for no bus configuration).

Again the problem here is in situation where lock is handled outside of the read/write and the read/modify/write operation has to be done in one go so splitting this operation in 2 step (like it's done for regmap_update_bits) would be problematic.

Another solution would be to expose a way to change the cache externally to the regmap operation so that if someone require cache operation and require also a dedicated reg_update_bits, he can do that by implementing that in his own function.
A third solution would be check if it's possible to cache the value externally to function... Something that calls the reg_update_bits dedicated function and then update the cache if needed.

But do we really need to add all this complexity when we can just deny an option with cache enabled and force to use the normal way (else part in this function)

Hope I was able to explain better why we need this change.

Signature · 1 line
	Ansuel
maintainer
#3 lore raw
Quoted 15 lines · Ansuel Smith
Quoted 13 lines · Mark Brown

drivers/base/regmap/regmap.c static int _regmap_update_bits(struct regmap *map, unsigned int reg, · line 3071

Quoted 5 lines · Ansuel Smith
drivers/base/regmap/regmap.c +1 -1
-	if (regmap_volatile(map, reg) && map->reg_update_bits) {
+	if ((regmap_volatile(map, reg) || !map->bus) && map->reg_update_bits) {
 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
 		if (ret == 0 && change)
 			*change = true;

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

Quoted 4 lines

I put the check there to not duplicate the code. The idea here is:
if we are doing a regmap_update_bits operation on a no bus configuration and the function have a dedicated reg_update_bits function, use that instead of the normal _reg_read, check and _reg_write.

Yes, I can see that this is what the change does - the problem is that it's buggy.

Quoted 3 lines

To workaround the CACHE problem, I can add a check and detect if cache is disabled and only with that option permit to add a reg_update_bits function to the map (for no bus configuration).

That's what the volatile check that is already there does - if there is no cache or that particular register is uncached then the register is volatile and we don't need to worry about updating the cache. There is not and should not be any connection to how the device is physically accessed, any connection there is clearly an abstraction problem.

Quoted 4 lines

Again the problem here is in situation where lock is handled outside of the read/write and the read/modify/write operation has to be done in one go so splitting this operation in 2 step (like it's done for regmap_update_bits) would be problematic.

Unconditionally introducing a data corruption or power management bug for any device that provides an update bits operation regardless of their requirements to use that operation for a specific register is not a good idea. If an individual device can't cope with some or all registers being cached then the driver for that device should configure it's regmap appropriately to ensure that the registers in question won't be cached.

Quoted 4 lines

Another solution would be to expose a way to change the cache externally to the regmap operation so that if someone require cache operation and require also a dedicated reg_update_bits, he can do that by implementing that in his own function.

I'm struggling to see a case where this would be useful without the register also being volatile in which case it's totally redundant. If the register can change underneath us then it is by definition volatile, if the register can't be changed underneath us then with a cache there's unlikely to be any meaningful upside to using a specific read/modify/write operation in the first place. You might have some case for wider registers where you can do a smaller transfer but that's got to be rare and I'd expect we'd have to be doing a lot of register I/O to care about the performance diffrence.

Quoted 3 lines

A third solution would be check if it's possible to cache the value externally to function... Something that calls the reg_update_bits dedicated function and then update the cache if needed.

That's exactly what the existing volatility check does,

Quoted 5 lines

But do we really need to add all this complexity when we can just deny an option with cache enabled and force to use the normal way (else part in this function)

Hope I was able to explain better why we need this change.

We do not need this change. The change that is being proposed will cause bugs, my best guess is that it's trying to work around a bug in the driver you're developing where it's enabling caching but not marking all the volatile registers properly. If there is any change needed in the _update_bits() implementation then where we get a device specific _update_bits() operation from should have no influence on our decision to use it, doing that is a clear sign that there's an abstraction problem.

#4 lore raw
Quoted 74 lines · Mark Brown
Quoted 15 lines · Ansuel Smith
Quoted 13 lines · Mark Brown

drivers/base/regmap/regmap.c static int _regmap_update_bits(struct regmap *map, unsigned int reg, · line 3071

Quoted 5 lines · Ansuel Smith
drivers/base/regmap/regmap.c +1 -1
-	if (regmap_volatile(map, reg) && map->reg_update_bits) {
+	if ((regmap_volatile(map, reg) || !map->bus) && map->reg_update_bits) {
 		ret = map->reg_update_bits(map->bus_context, reg, mask, val);
 		if (ret == 0 && change)
 			*change = true;

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

Quoted 4 lines

I put the check there to not duplicate the code. The idea here is:
if we are doing a regmap_update_bits operation on a no bus configuration and the function have a dedicated reg_update_bits function, use that instead of the normal _reg_read, check and _reg_write.

Yes, I can see that this is what the change does - the problem is that it's buggy.

Quoted 3 lines

To workaround the CACHE problem, I can add a check and detect if cache is disabled and only with that option permit to add a reg_update_bits function to the map (for no bus configuration).

That's what the volatile check that is already there does - if there is no cache or that particular register is uncached then the register is volatile and we don't need to worry about updating the cache. There is not and should not be any connection to how the device is physically accessed, any connection there is clearly an abstraction problem.

Quoted 4 lines

Again the problem here is in situation where lock is handled outside of the read/write and the read/modify/write operation has to be done in one go so splitting this operation in 2 step (like it's done for regmap_update_bits) would be problematic.

Unconditionally introducing a data corruption or power management bug for any device that provides an update bits operation regardless of their requirements to use that operation for a specific register is not a good idea. If an individual device can't cope with some or all registers being cached then the driver for that device should configure it's regmap appropriately to ensure that the registers in question won't be cached.

Quoted 4 lines

Another solution would be to expose a way to change the cache externally to the regmap operation so that if someone require cache operation and require also a dedicated reg_update_bits, he can do that by implementing that in his own function.

I'm struggling to see a case where this would be useful without the register also being volatile in which case it's totally redundant. If the register can change underneath us then it is by definition volatile, if the register can't be changed underneath us then with a cache there's unlikely to be any meaningful upside to using a specific read/modify/write operation in the first place. You might have some case for wider registers where you can do a smaller transfer but that's got to be rare and I'd expect we'd have to be doing a lot of register I/O to care about the performance diffrence.

Quoted 3 lines

A third solution would be check if it's possible to cache the value externally to function... Something that calls the reg_update_bits dedicated function and then update the cache if needed.

That's exactly what the existing volatility check does,

Quoted 5 lines

But do we really need to add all this complexity when we can just deny an option with cache enabled and force to use the normal way (else part in this function)

Hope I was able to explain better why we need this change.

Quoted 8 lines

We do not need this change. The change that is being proposed will cause bugs, my best guess is that it's trying to work around a bug in the driver you're developing where it's enabling caching but not marking all the volatile registers properly. If there is any change needed in the _update_bits() implementation then where we get a device specific _update_bits() operation from should have no influence on our decision to use it, doing that is a clear sign that there's an abstraction problem.

I think I'm missing something. The user case is a driver that have CACHE DISABLED. The !map->bus check is added just to limit this to a no bus configuration not to permit this with CACHE enabled. The limit I was referring was in the init function where the update_bits is assigned to the map. I honestly didn't notice that anything with cache disabled was flagged as volatile.

So the rest of the changes permit to declare a update_bits function for a no bus configuration is good?

Again that was the main reason of this patch. Obviously I didn't want to introduce bugs or an hack to bypass the cache thing. Just I notice this was permitted with a bus configuration but the no bus configuration lacks any way to configure a update_bits function.

Signature · 1 line
	Ansuel
maintainer
#5 lore raw
Quoted 8 lines · Ansuel Smith
Quoted 5 lines · Mark Brown
Quoted 5 lines
Quoted 5 lines

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

Quoted 6 lines

I think I'm missing something. The user case is a driver that have CACHE DISABLED. The !map->bus check is added just to limit this to a no bus configuration not to permit this with CACHE enabled. The limit I was referring was in the init function where the update_bits is assigned to the map. I honestly didn't notice that anything with cache disabled was flagged as volatile.

In what way would the presence or absence of a bus be relevant to a decision about being able to safely use an _update_bits() operation?

Quoted 2 lines

So the rest of the changes permit to declare a update_bits function for a no bus configuration is good?

Probably, I'd need to look again.

#6 lore raw
Quoted 16 lines · Mark Brown
Quoted 8 lines · Ansuel Smith
Quoted 5 lines · Mark Brown
Quoted 5 lines
Quoted 5 lines

I don't understand this change. The point of the check for volatile there is that if the register isn't volatile then we need to ensure that the cache gets updated with any change that happens so we need to go through paths that include cache updates. The presence or otherwise of a bus does not seem at all relevant here.

Quoted 6 lines

I think I'm missing something. The user case is a driver that have CACHE DISABLED. The !map->bus check is added just to limit this to a no bus configuration not to permit this with CACHE enabled. The limit I was referring was in the init function where the update_bits is assigned to the map. I honestly didn't notice that anything with cache disabled was flagged as volatile.

Quoted 3 lines

In what way would the presence or absence of a bus be relevant to a decision about being able to safely use an _update_bits() operation?

No reason. It was just to make changes only to a no bus configuration and doesn't cause any problem/error to other regmap configuration. (since currently we can declare a custom update_bits function only for bus configuration)

Quoted 4 lines
Quoted 2 lines

So the rest of the changes permit to declare a update_bits function for a no bus configuration is good?

Probably, I'd need to look again.

Let me send a v2 so you can check the final patch.

Signature · 1 line
	Ansuel