* [PATCH 1/2] regmap: Support accelerated noinc operations
@ 2022-07-25 8:58 Linus Walleij
2022-07-25 8:58 ` [PATCH 2/2] regmap: mmio: Support accelerared " Linus Walleij
2022-07-25 11:45 ` [PATCH 1/2] regmap: Support accelerated " Mark Brown
0 siblings, 2 replies; 3+ messages in thread
From: Linus Walleij @ 2022-07-25 8:58 UTC (permalink / raw)
To: Mark Brown; +Cc: linux-kernel, Linus Walleij
Several architectures have accelerated operations for MMIO
operations writing to a single register, such as writesb, writesw,
writesl, writesq, readsb, readsw, readsl and readsq but regmap
currently cannot use them because we have no hooks for providing
an accelerated noinc back-end for MMIO.
Solve this by providing reg_[read/write]_noinc callbacks for
the bus abstraction, so that the regmap-mmio bus can use this.
Currently I do not see a need to support this for custom regmaps
so it is only added to the bus.
Callbacks are passed a void * with the array of values and a
count which is the number of items of the byte chunk size for
the specific register width.
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
drivers/base/regmap/regmap.c | 164 ++++++++++++++++++++++++++++++++++-
include/linux/regmap.h | 8 ++
2 files changed, 171 insertions(+), 1 deletion(-)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 2221d9863831..2923bb63ab95 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -2157,7 +2157,7 @@ int regmap_noinc_write(struct regmap *map, unsigned int reg,
if (!map->bus)
return -EINVAL;
- if (!map->bus->write)
+ if (!map->bus->write && !map->bus->reg_noinc_write)
return -ENOTSUPP;
if (val_len % map->format.val_bytes)
return -EINVAL;
@@ -2173,6 +2173,92 @@ int regmap_noinc_write(struct regmap *map, unsigned int reg,
goto out_unlock;
}
+ /* Use the accelerated operation if we can */
+ if (map->bus->reg_noinc_write) {
+ size_t val_bytes = map->format.val_bytes;
+ size_t val_count = val_len / val_bytes;
+ unsigned int lastval;
+ const u8 *u8p;
+ const u16 *u16p;
+ const u32 *u32p;
+#ifdef CONFIG_64BIT
+ const u64 *u64p;
+#endif
+ int i;
+
+ switch (val_bytes) {
+ case 1:
+ u8p = val;
+ lastval = (unsigned int)u8p[val_count - 1];
+ break;
+ case 2:
+ u16p = val;
+ lastval = (unsigned int)u16p[val_count - 1];
+ break;
+ case 4:
+ u32p = val;
+ lastval = (unsigned int)u32p[val_count - 1];
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ u64p = val;
+ lastval = (unsigned int)u64p[val_count - 1];
+ break;
+#endif
+ default:
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ /*
+ * Update the cache with the last value we write, the rest is just
+ * gone down in the hardware FIFO. We can't cache FIFOs. This makes
+ * sure a single read from the cache will work.
+ */
+ if (!map->cache_bypass && !map->defer_caching) {
+ ret = regcache_write(map, reg, lastval);
+ if (ret != 0)
+ goto out_unlock;
+ if (map->cache_only) {
+ map->cache_dirty = true;
+ ret = 0;
+ goto out_unlock;
+ }
+ }
+
+ ret = map->bus->reg_noinc_write(map->bus_context, reg, val, val_count);
+
+ if (!ret && regmap_should_log(map)) {
+ dev_info(map->dev, "%x <= [", reg);
+ for (i = 0; i < val_len; i++) {
+ switch (val_bytes) {
+ case 1:
+ pr_cont("%x", u8p[i]);
+ break;
+ case 2:
+ pr_cont("%x", u16p[i]);
+ break;
+ case 4:
+ pr_cont("%x", u32p[i]);
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ pr_cont("%llx", u64p[i]);
+ break;
+#endif
+ default:
+ break;
+ }
+ if (i == (val_len - 1))
+ pr_cont("]\n");
+ else
+ pr_cont(",");
+ }
+ }
+ ret = 0;
+ goto out_unlock;
+ }
+
while (val_len) {
if (map->max_raw_write && map->max_raw_write < val_len)
write_len = map->max_raw_write;
@@ -2918,6 +3004,82 @@ int regmap_noinc_read(struct regmap *map, unsigned int reg,
goto out_unlock;
}
+ /* Use the accelerated operation if we can */
+ if (map->bus->reg_noinc_read) {
+ size_t val_bytes = map->format.val_bytes;
+ size_t val_count = val_len / val_bytes;
+ const u8 *u8p;
+ const u16 *u16p;
+ const u32 *u32p;
+#ifdef CONFIG_64BIT
+ const u64 *u64p;
+#endif
+ int i;
+
+ /*
+ * We have not defined the FIFO semantics for cache, as the
+ * cache is just one value deep. Should we return the last
+ * written value? Just avoid this by always reading the FIFO
+ * even when using cache. Cache only will not work.
+ */
+ if (map->cache_only) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
+ switch (val_bytes) {
+ case 1:
+ u8p = val;
+ break;
+ case 2:
+ u16p = val;
+ break;
+ case 4:
+ u32p = val;
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ u64p = val;
+ break;
+#endif
+ default:
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ ret = map->bus->reg_noinc_read(map->bus_context, reg, val, val_count);
+
+ if (!ret && regmap_should_log(map)) {
+ dev_info(map->dev, "%x => [", reg);
+ for (i = 0; i < val_len; i++) {
+ switch (val_bytes) {
+ case 1:
+ pr_cont("%x", u8p[i]);
+ break;
+ case 2:
+ pr_cont("%x", u16p[i]);
+ break;
+ case 4:
+ pr_cont("%x", u32p[i]);
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ pr_cont("%llx", u64p[i]);
+ break;
+#endif
+ default:
+ break;
+ }
+ if (i == (val_len - 1))
+ pr_cont("]\n");
+ else
+ pr_cont(",");
+ }
+ }
+ ret = 0;
+ goto out_unlock;
+ }
+
while (val_len) {
if (map->max_raw_read && map->max_raw_read < val_len)
read_len = map->max_raw_read;
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index 8952fa3d0d59..c60bd3a4b098 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -489,8 +489,12 @@ typedef int (*regmap_hw_read)(void *context,
void *val_buf, size_t val_size);
typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
unsigned int *val);
+typedef int (*regmap_hw_reg_noinc_read)(void *context, unsigned int reg,
+ void *val, size_t val_count);
typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
unsigned int val);
+typedef int (*regmap_hw_reg_noinc_write)(void *context, unsigned int reg,
+ const void *val, size_t val_count);
typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
unsigned int mask, unsigned int val);
typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
@@ -511,6 +515,8 @@ typedef void (*regmap_hw_free_context)(void *context);
* must serialise with respect to non-async I/O.
* @reg_write: Write a single register value to the given register address. This
* write operation has to complete when returning from the function.
+ * @reg_write_noinc: Write multiple register value to the same register. This
+ * write operation has to complete when returning from the function.
* @reg_update_bits: Update bits operation to be used against volatile
* registers, intended for devices supporting some mechanism
* for setting clearing bits without having to
@@ -538,9 +544,11 @@ struct regmap_bus {
regmap_hw_gather_write gather_write;
regmap_hw_async_write async_write;
regmap_hw_reg_write reg_write;
+ regmap_hw_reg_noinc_write reg_noinc_write;
regmap_hw_reg_update_bits reg_update_bits;
regmap_hw_read read;
regmap_hw_reg_read reg_read;
+ regmap_hw_reg_noinc_read reg_noinc_read;
regmap_hw_free_context free_context;
regmap_hw_async_alloc async_alloc;
u8 read_flag_mask;
--
2.36.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] regmap: mmio: Support accelerared noinc operations
2022-07-25 8:58 [PATCH 1/2] regmap: Support accelerated noinc operations Linus Walleij
@ 2022-07-25 8:58 ` Linus Walleij
2022-07-25 11:45 ` [PATCH 1/2] regmap: Support accelerated " Mark Brown
1 sibling, 0 replies; 3+ messages in thread
From: Linus Walleij @ 2022-07-25 8:58 UTC (permalink / raw)
To: Mark Brown; +Cc: linux-kernel, Linus Walleij
Use the newly added callback for accelerated noinc MMIO
to provide writesb, writesw, writesl, writesq, readsb, readsw,
readsl and readsq.
A special quirk is needed to deal with big endian regmaps: there
are no accelerated operations defined for big endian, so fall
back to calling the big endian operations itereatively for this
case.
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
drivers/base/regmap/regmap-mmio.c | 153 ++++++++++++++++++++++++++++++
1 file changed, 153 insertions(+)
diff --git a/drivers/base/regmap/regmap-mmio.c b/drivers/base/regmap/regmap-mmio.c
index 71f16be7e717..031ee91020e8 100644
--- a/drivers/base/regmap/regmap-mmio.c
+++ b/drivers/base/regmap/regmap-mmio.c
@@ -17,6 +17,7 @@ struct regmap_mmio_context {
void __iomem *regs;
unsigned int val_bytes;
bool relaxed_mmio;
+ bool big_endian;
bool attached_clk;
struct clk *clk;
@@ -160,6 +161,79 @@ static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
return 0;
}
+static int regmap_mmio_noinc_write(void *context, unsigned int reg,
+ const void *val, size_t val_count)
+{
+ struct regmap_mmio_context *ctx = context;
+ int ret = 0;
+ int i;
+
+ if (!IS_ERR(ctx->clk)) {
+ ret = clk_enable(ctx->clk);
+ if (ret < 0)
+ return ret;
+ }
+
+ /*
+ * There are no native, assembly-optimized write single register
+ * operations for big endian, so fall back to emulation if this
+ * is needed. (Single bytes are fine, they are not affected by
+ * endianness.)
+ */
+ if (ctx->big_endian && (ctx->val_bytes > 1)) {
+ switch (ctx->val_bytes) {
+ case 2:
+ {
+ const u16 *valp = (const u16 *)val;
+ for (i = 0; i < val_count; i++)
+ iowrite16be(valp[i], ctx->regs + reg);
+ break;
+ }
+ case 4:
+ {
+ const u32 *valp = (const u32 *)val;
+ for (i = 0; i < val_count; i++)
+ iowrite32be(valp[i], ctx->regs + reg);
+ break;
+ }
+#ifdef CONFIG_64BIT
+ case 8:
+ /* This is just too esoteric */
+ fallthrough;
+#endif
+ default:
+ ret = -EINVAL;
+ goto out_clk;
+ }
+ }
+
+ switch (ctx->val_bytes) {
+ case 1:
+ writesb(ctx->regs + reg, (const u8 *)val, val_count);
+ break;
+ case 2:
+ writesw(ctx->regs + reg, (const u16 *)val, val_count);
+ break;
+ case 4:
+ writesl(ctx->regs + reg, (const u32 *)val, val_count);
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ writesq(ctx->regs + reg, (const u64 *)val, val_count);
+ break;
+#endif
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+out_clk:
+ if (!IS_ERR(ctx->clk))
+ clk_disable(ctx->clk);
+
+ return ret;
+}
+
static unsigned int regmap_mmio_read8(struct regmap_mmio_context *ctx,
unsigned int reg)
{
@@ -241,6 +315,82 @@ static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
return 0;
}
+static int regmap_mmio_noinc_read(void *context, unsigned int reg,
+ void *val, size_t val_count)
+{
+ struct regmap_mmio_context *ctx = context;
+ int ret = 0;
+ int i;
+
+ if (!IS_ERR(ctx->clk)) {
+ ret = clk_enable(ctx->clk);
+ if (ret < 0)
+ return ret;
+ }
+
+ /*
+ * There are no native, assembly-optimized write single register
+ * operations for big endian, so fall back to emulation if this
+ * is needed. (Single bytes are fine, they are not affected by
+ * endianness.)
+ */
+ if (ctx->big_endian && (ctx->val_bytes > 1)) {
+ switch (ctx->val_bytes) {
+ case 2:
+ {
+ u16 *valp = (u16 *)val;
+ for (i = 0; i < val_count; i++)
+ valp[i] = ioread16be(ctx->regs + reg);
+ break;
+ }
+ case 4:
+ {
+ u32 *valp = (u32 *)val;
+ for (i = 0; i < val_count; i++)
+ valp[i] = ioread32be(ctx->regs + reg);
+ break;
+ }
+#ifdef CONFIG_64BIT
+ case 8:
+ /* This is just too esoteric */
+ fallthrough;
+#endif
+ default:
+ ret = -EINVAL;
+ goto out_clk;
+ }
+ }
+
+ switch (ctx->val_bytes) {
+ case 1:
+ readsb(ctx->regs + reg, (u8 *)val, val_count);
+ break;
+ case 2:
+ readsw(ctx->regs + reg, (u16 *)val, val_count);
+ break;
+ case 4:
+ readsl(ctx->regs + reg, (u32 *)val, val_count);
+ break;
+#ifdef CONFIG_64BIT
+ case 8:
+ readsq(ctx->regs + reg, (u64 *)val, val_count);
+ break;
+#endif
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+out_clk:
+ if (!IS_ERR(ctx->clk))
+ clk_disable(ctx->clk);
+
+ return ret;
+
+ return 0;
+}
+
+
static void regmap_mmio_free_context(void *context)
{
struct regmap_mmio_context *ctx = context;
@@ -257,6 +407,8 @@ static const struct regmap_bus regmap_mmio = {
.fast_io = true,
.reg_write = regmap_mmio_write,
.reg_read = regmap_mmio_read,
+ .reg_noinc_write = regmap_mmio_noinc_write,
+ .reg_noinc_read = regmap_mmio_noinc_read,
.free_context = regmap_mmio_free_context,
.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
};
@@ -347,6 +499,7 @@ static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
#ifdef __BIG_ENDIAN
case REGMAP_ENDIAN_NATIVE:
#endif
+ ctx->big_endian = true;
switch (config->val_bits) {
case 8:
ctx->reg_read = regmap_mmio_read8;
--
2.36.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] regmap: Support accelerated noinc operations
2022-07-25 8:58 [PATCH 1/2] regmap: Support accelerated noinc operations Linus Walleij
2022-07-25 8:58 ` [PATCH 2/2] regmap: mmio: Support accelerared " Linus Walleij
@ 2022-07-25 11:45 ` Mark Brown
1 sibling, 0 replies; 3+ messages in thread
From: Mark Brown @ 2022-07-25 11:45 UTC (permalink / raw)
To: Linus Walleij; +Cc: linux-kernel
[-- Attachment #1: Type: text/plain, Size: 363 bytes --]
On Mon, Jul 25, 2022 at 10:58:21AM +0200, Linus Walleij wrote:
> + /* Use the accelerated operation if we can */
> + if (map->bus->reg_noinc_write) {
> + size_t val_bytes = map->format.val_bytes;
A lot of duplicated buffer formatting in here... that should probably
be factored out rather than copied, otherwise the noinc stuff is going
to drift out of sync.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-07-25 11:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-25 8:58 [PATCH 1/2] regmap: Support accelerated noinc operations Linus Walleij
2022-07-25 8:58 ` [PATCH 2/2] regmap: mmio: Support accelerared " Linus Walleij
2022-07-25 11:45 ` [PATCH 1/2] regmap: Support accelerated " Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).