regmap: rbtree: Simplify adjacent node look-up
Subsystems (2)
PATCH v2 1/3 regmap: rbtree: Simplify adjacent node look-up
A register which is adjacent to a node will either be left to the first register or right to the last register. It will not be within the node's range, so there is no point in checking for each register cached by the node whether the new register is next to it. It is sufficient to check whether the register comes before the first register or after the last register of the node.
---
No changes since v1.
--- drivers/base/regmap/regcache-rbtree.c | 39 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-)
drivers/base/regmap/regcache-rbtree.c +19 -20
@@ -352,9 +352,9 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,struct regcache_rbtree_ctx *rbtree_ctx;struct regcache_rbtree_node *rbnode, *rbnode_tmp;struct rb_node *node; +unsigned int base_reg, top_reg;unsigned int reg_tmp;unsigned int pos; -int i;int ret; rbtree_ctx = map->cache;
@@ -376,25 +376,24 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg, node = rb_next(node)) { rbnode_tmp = rb_entry(node, struct regcache_rbtree_node, node); -for (i =0 ; i < rbnode_tmp->blklen; i++) { - reg_tmp = rbnode_tmp->base_reg + - (i * map->reg_stride); -if (abs (reg_tmp - reg) != map->reg_stride) -continue ; -/* decide where in the block to place our register */ -if (reg_tmp + map->reg_stride == reg) - pos = i +1 ; -else - pos = i; - ret =regcache_rbtree_insert_to_block (map, - rbnode_tmp, - pos, reg, - value); -if (ret) -return ret; - rbtree_ctx->cached_rbnode = rbnode_tmp; -return 0 ; - } + +regcache_rbtree_get_base_top_reg (map, rbnode_tmp, + &base_reg, &top_reg); + +/* decide where in the block to place our register */ +if (base_reg >0 && reg == base_reg - map->reg_stride) + pos =0 ; +else if (reg >0 && reg - map->reg_stride == top_reg) + pos = rbnode_tmp->blklen; +else +continue ; + + ret =regcache_rbtree_insert_to_block (map, rbnode_tmp, + pos, reg, value); +if (ret) +return ret; + rbtree_ctx->cached_rbnode = rbnode_tmp; +return 0 ; }/* We did not manage to find a place to insert it in
Signature · 1 line
1.8.0
PATCH v2 2/3 regmap: rbtree: Reduce number of nodes, take 2
Support for reducing the number of nodes and memory consumption of the rbtree cache by allowing for small unused holes in the node's register cache block was initially added in commit 0c7ed856 ("regmap: Cut down on the average # of nodes in the rbtree cache"). But the commit had problems and so its effect was reverted again in commit 4e67fb5 ("regmap: rbtree: Fix overlapping rbnodes."). This patch brings the feature back of reducing the average number of nodes, which will speedup node look-up, while at the same time also reducing the memory usage of the rbtree cache. This patch takes a slightly different approach than the original patch though. It modifies the adjacent node look-up to not only consider nodes that are just one to the left or the right of the register but any node that falls in a certain range around the register. The range is calculated based on how much memory it would take to allocate a new node compared to how much memory it takes adding a set of unused registers to an existing node. E.g. if a node takes up 24 bytes and each register in a block uses 1 byte the range will be from the register address - 24 to the register address + 24. If we find a node that falls within this range it is cheaper or as expensive to add the register to the existing node and have a couple of unused registers in the node's cache compared to allocating a new node.
---
Changes since v1:
* Repair commit message
--- drivers/base/regmap/regcache-rbtree.c | 53 ++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 17 deletions(-)
drivers/base/regmap/regcache-rbtree.c +36 -17
@@ -278,27 +278,34 @@ static int regcache_rbtree_read(struct regmap *map,static int regcache_rbtree_insert_to_block (struct regmap *map,struct regcache_rbtree_node *rbnode, -unsigned int pos,unsigned int reg, +unsigned int base_reg, +unsigned int top_reg, +unsigned int reg,unsigned int value) { +unsigned int blklen; +unsigned int pos, offset;u8 *blk; + blklen = (top_reg - base_reg) / map->reg_stride +1 ; + pos = (reg - base_reg) / map->reg_stride; + offset = (rbnode->base_reg - base_reg) / map->reg_stride; + blk =krealloc (rbnode->block, - (rbnode->blklen +1 ) * map->cache_word_size, + blklen * map->cache_word_size,GFP_KERNEL );if (!blk)return -ENOMEM ;/* insert the register value in the correct place in the rbnode block */ -memmove (blk + (pos +1 ) * map->cache_word_size, - blk + pos * map->cache_word_size, - (rbnode->blklen - pos) * map->cache_word_size); +if (pos ==0 ) +memmove (blk + offset * map->cache_word_size, + blk, rbnode->blklen * map->cache_word_size);/* update the rbnode block, its size and the base register */ rbnode->block = blk; - rbnode->blklen++; -if (!pos) - rbnode->base_reg = reg; + rbnode->blklen = blklen; + rbnode->base_reg = base_reg;regcache_rbtree_set_register (map, rbnode, pos, value);return 0 ;
@@ -352,9 +359,7 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,struct regcache_rbtree_ctx *rbtree_ctx;struct regcache_rbtree_node *rbnode, *rbnode_tmp;struct rb_node *node; -unsigned int base_reg, top_reg;unsigned int reg_tmp; -unsigned int pos;int ret; rbtree_ctx = map->cache;
@@ -371,6 +376,19 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg, reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;regcache_rbtree_set_register (map, rbnode, reg_tmp, value); }else { +unsigned int base_reg, top_reg; +unsigned int new_base_reg, new_top_reg; +unsigned int min, max; +unsigned int max_dist; + + max_dist = map->reg_stride *sizeof (*rbnode_tmp) / + map->cache_word_size; +if (reg < max_dist) + min =0 ; +else + min = reg - max_dist; + max = reg + max_dist; +/* look for an adjacent register to the one we are about to add */ for (node =rb_first (&rbtree_ctx->root); node; node =rb_next (node)) {
@@ -380,16 +398,17 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,regcache_rbtree_get_base_top_reg (map, rbnode_tmp, &base_reg, &top_reg); -/* decide where in the block to place our register */ -if (base_reg >0 && reg == base_reg - map->reg_stride) - pos =0 ; -else if (reg >0 && reg - map->reg_stride == top_reg) - pos = rbnode_tmp->blklen; -else +if (base_reg <= max && top_reg >= min) { + new_base_reg =min (reg, base_reg); + new_top_reg =max (reg, top_reg); + }else {continue ; + } ret =regcache_rbtree_insert_to_block (map, rbnode_tmp, - pos, reg, value); + new_base_reg, + new_top_reg, reg, + value);if (ret)return ret; rbtree_ctx->cached_rbnode = rbnode_tmp;
Signature · 1 line
1.8.0
PATCH v2 3/3 regmap: rbtree: Make cache_present bitmap per node
In reply to Lars-Peter ClausenWith devices which have a dense and small register map but placed at a large offset the global cache_present bitmap imposes a huge memory overhead. Making the cache_present per rbtree node avoids the issue and easily reduces the memory footprint by a factor of ten. For devices with a more sparse map or without a large base register offset the memory usage might increase slightly by a few bytes, but not significantly. E.g. for a device which has ~50 registers at offset 0x4000 the memory footprint of the register cache goes down form 2496 bytes to 175 bytes.
Moving the bitmap to a per node basis means that the handling of the bitmap is now cache implementation specific and can no longer be managed by the core. The regcache_sync_block() function is extended by a additional parameter so that the cache implementation can tell the core which registers in the block are set and which are not. The parameter is optional and if NULL the core assumes that all registers are set. The rbtree cache also needs to implement its own drop callback instead of relying on the core to handle this.
---
No changes since v1
--- drivers/base/regmap/internal.h | 14 +----- drivers/base/regmap/regcache-rbtree.c | 86 +++++++++++++++++++++++++++++------ drivers/base/regmap/regcache.c | 72 ++++++++--------------------- 3 files changed, 92 insertions(+), 80 deletions(-)
drivers/base/regmap/internal.h +1 -13
@@ -128,9 +128,6 @@ struct regmap {void *cache;u32 cache_dirty; -unsigned long *cache_present; -unsigned int cache_present_nbits; -struct reg_default *patch;int patch_regs;
@@ -203,6 +200,7 @@ int regcache_write(struct regmap *map, unsigned int reg, unsigned int value);int regcache_sync (struct regmap *map);int regcache_sync_block (struct regmap *map,void *block, +unsigned long *cache_present,unsigned int block_base,unsigned int start,unsigned int end);
@@ -218,16 +216,6 @@ unsigned int regcache_get_val(struct regmap *map, const void *base,bool regcache_set_val (struct regmap *map,void *base,unsigned int idx,unsigned int val);int regcache_lookup_reg (struct regmap *map,unsigned int reg); -int regcache_set_reg_present (struct regmap *map,unsigned int reg); - -static inline bool regcache_reg_present (struct regmap *map,unsigned int reg) -{ -if (!map->cache_present) -return false; -if (reg > map->cache_present_nbits) -return false; -return map->cache_present[BIT_WORD (reg)] &BIT_MASK (reg); -}int _regmap_raw_write (struct regmap *map,unsigned int reg,const void *val,size_t val_len,bool async);
drivers/base/regmap/regcache-rbtree.c +72 -14
172 lines not on this page · raw message
drivers/base/regmap/regcache.c +19 -53
138 lines not on this page · raw message
Signature · 1 line
1.8.0
Quoted 5 lines · Lars-Peter Clausen
A register which is adjacent to a node will either be left to the first register or right to the last register. It will not be within the node's range, so there is no point in checking for each register cached by the node whether the new register is next to it. It is sufficient to check whether the register comes before the first register or after the last register of the node.
Applied all, thanks.