2021-06-12 21:15:21

by Sander Vanheule

[permalink] [raw]
Subject: [PATCH v5 1/8] regmap: Support atomic forced uncached reads

When a user wants to read a single uncached register, cache bypassing
can be enabled. However, this is not atomic unless an external lock is
used for the regmap. When using regcache_cache_bypass, the original
bypass state also cannot be restored.

Add support to atomically read a single uncached value, bypassing any
regmap cache.

Signed-off-by: Sander Vanheule <[email protected]>
---
drivers/base/regmap/regmap.c | 33 +++++++++++++++++++++++++++++++++
include/linux/regmap.h | 8 ++++++++
2 files changed, 41 insertions(+)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index fe3e38dd5324..a828f05535b7 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -2745,6 +2745,39 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
}
EXPORT_SYMBOL_GPL(regmap_read);

+/**
+ * regmap_read_bypassed() - Read a value from a single register, bypassing the cache
+ *
+ * @map: Register map to read from
+ * @reg: Register to be read from
+ * @val: Pointer to store read value
+ *
+ * A value of zero will be returned on success, a negative errno will
+ * be returned in error cases.
+ */
+int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val)
+{
+ bool bypass;
+ int ret;
+
+ if (!IS_ALIGNED(reg, map->reg_stride))
+ return -EINVAL;
+
+ map->lock(map->lock_arg);
+
+ bypass = map->cache_bypass;
+ map->cache_bypass = true;
+
+ ret = _regmap_read(map, reg, val);
+
+ map->cache_bypass = bypass;
+
+ map->unlock(map->lock_arg);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(regmap_read_bypassed);
+
/**
* regmap_raw_read() - Read raw data from the device
*
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index f5f08dd0a116..a54dc00326ba 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -1133,6 +1133,7 @@ int regmap_multi_reg_write_bypassed(struct regmap *map,
int regmap_raw_write_async(struct regmap *map, unsigned int reg,
const void *val, size_t val_len);
int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
+int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val);
int regmap_raw_read(struct regmap *map, unsigned int reg,
void *val, size_t val_len);
int regmap_noinc_read(struct regmap *map, unsigned int reg,
@@ -1607,6 +1608,13 @@ static inline int regmap_read(struct regmap *map, unsigned int reg,
return -EINVAL;
}

+static inline int regmap_read_bypassed(struct regmap *map, unsigned int reg,
+ unsigned int *val)
+{
+ WARN_ONCE(1, "regmap API is disabled");
+ return -EINVAL;
+}
+
static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
void *val, size_t val_len)
{
--
2.31.1


2021-06-12 21:31:30

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 1/8] regmap: Support atomic forced uncached reads

On Sun, Jun 13, 2021 at 12:13 AM Sander Vanheule <[email protected]> wrote:
>
> When a user wants to read a single uncached register, cache bypassing
> can be enabled. However, this is not atomic unless an external lock is
> used for the regmap. When using regcache_cache_bypass, the original
> bypass state also cannot be restored.
>
> Add support to atomically read a single uncached value, bypassing any
> regmap cache.

> +int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val)

If this is acceptable in general, I will rather name the function like
regmap_nocache_read() to be aligned with the other API naming pattern
(see below).

> int regmap_raw_write_async(struct regmap *map, unsigned int reg,
> const void *val, size_t val_len);
> int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
> +int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val);
> int regmap_raw_read(struct regmap *map, unsigned int reg,
> void *val, size_t val_len);
> int regmap_noinc_read(struct regmap *map, unsigned int reg,


--
With Best Regards,
Andy Shevchenko

2021-06-14 10:45:52

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH v5 1/8] regmap: Support atomic forced uncached reads

On Sat, Jun 12, 2021 at 11:12:31PM +0200, Sander Vanheule wrote:

> When a user wants to read a single uncached register, cache bypassing
> can be enabled. However, this is not atomic unless an external lock is
> used for the regmap. When using regcache_cache_bypass, the original
> bypass state also cannot be restored.

> Add support to atomically read a single uncached value, bypassing any
> regmap cache.

The expectation here is that if there is a need to do this for some
reason the user can arrange to do this for itself - if something is
happening that makes a normally non-volatile register volatile then
it probably needs higher level coordination. What's the use case?

> +int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val)

Bypassed what? I think Andy's naming suggestion was much better.

Please also keep to 80 columns if you can, I know the requirements got
relaxed a bit but no need to do it excessively.


Attachments:
(No filename) (977.00 B)
signature.asc (499.00 B)
Download all attachments