Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Register cache access API - rbtree caching support |
| 3 | * |
| 4 | * Copyright 2011 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 13 | #include <linux/debugfs.h> |
Xiubo Li | e39be3a | 2014-10-09 17:02:52 +0800 | [diff] [blame] | 14 | #include <linux/device.h> |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 15 | #include <linux/rbtree.h> |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
Xiubo Li | e39be3a | 2014-10-09 17:02:52 +0800 | [diff] [blame] | 17 | #include <linux/slab.h> |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 18 | |
| 19 | #include "internal.h" |
| 20 | |
| 21 | static int regcache_rbtree_write(struct regmap *map, unsigned int reg, |
| 22 | unsigned int value); |
Lars-Peter Clausen | 462a185 | 2011-11-15 13:34:40 +0100 | [diff] [blame] | 23 | static int regcache_rbtree_exit(struct regmap *map); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 24 | |
| 25 | struct regcache_rbtree_node { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 26 | /* block of adjacent registers */ |
| 27 | void *block; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 28 | /* Which registers are present */ |
| 29 | long *cache_present; |
Jean-Christophe PINCE | 70d383b | 2014-04-01 13:26:48 -0700 | [diff] [blame] | 30 | /* base register handled by this block */ |
| 31 | unsigned int base_reg; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 32 | /* number of registers available in the block */ |
| 33 | unsigned int blklen; |
Jean-Christophe PINCE | 70d383b | 2014-04-01 13:26:48 -0700 | [diff] [blame] | 34 | /* the actual rbtree node holding this block */ |
| 35 | struct rb_node node; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 36 | } __attribute__ ((packed)); |
| 37 | |
| 38 | struct regcache_rbtree_ctx { |
| 39 | struct rb_root root; |
| 40 | struct regcache_rbtree_node *cached_rbnode; |
| 41 | }; |
| 42 | |
| 43 | static inline void regcache_rbtree_get_base_top_reg( |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 44 | struct regmap *map, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 45 | struct regcache_rbtree_node *rbnode, |
| 46 | unsigned int *base, unsigned int *top) |
| 47 | { |
| 48 | *base = rbnode->base_reg; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 49 | *top = rbnode->base_reg + ((rbnode->blklen - 1) * map->reg_stride); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 52 | static unsigned int regcache_rbtree_get_register(struct regmap *map, |
| 53 | struct regcache_rbtree_node *rbnode, unsigned int idx) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 54 | { |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 55 | return regcache_get_val(map, rbnode->block, idx); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 58 | static void regcache_rbtree_set_register(struct regmap *map, |
| 59 | struct regcache_rbtree_node *rbnode, |
| 60 | unsigned int idx, unsigned int val) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 61 | { |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 62 | set_bit(idx, rbnode->cache_present); |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 63 | regcache_set_val(map, rbnode->block, idx, val); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 66 | static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map, |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 67 | unsigned int reg) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 68 | { |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 69 | struct regcache_rbtree_ctx *rbtree_ctx = map->cache; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 70 | struct rb_node *node; |
| 71 | struct regcache_rbtree_node *rbnode; |
| 72 | unsigned int base_reg, top_reg; |
| 73 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 74 | rbnode = rbtree_ctx->cached_rbnode; |
| 75 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 76 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 77 | &top_reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 78 | if (reg >= base_reg && reg <= top_reg) |
| 79 | return rbnode; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | node = rbtree_ctx->root.rb_node; |
| 83 | while (node) { |
| 84 | rbnode = container_of(node, struct regcache_rbtree_node, node); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 85 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 86 | &top_reg); |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 87 | if (reg >= base_reg && reg <= top_reg) { |
| 88 | rbtree_ctx->cached_rbnode = rbnode; |
| 89 | return rbnode; |
| 90 | } else if (reg > top_reg) { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 91 | node = node->rb_right; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 92 | } else if (reg < base_reg) { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 93 | node = node->rb_left; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 94 | } |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 100 | static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 101 | struct regcache_rbtree_node *rbnode) |
| 102 | { |
| 103 | struct rb_node **new, *parent; |
| 104 | struct regcache_rbtree_node *rbnode_tmp; |
| 105 | unsigned int base_reg_tmp, top_reg_tmp; |
| 106 | unsigned int base_reg; |
| 107 | |
| 108 | parent = NULL; |
| 109 | new = &root->rb_node; |
| 110 | while (*new) { |
| 111 | rbnode_tmp = container_of(*new, struct regcache_rbtree_node, |
| 112 | node); |
| 113 | /* base and top registers of the current rbnode */ |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 114 | regcache_rbtree_get_base_top_reg(map, rbnode_tmp, &base_reg_tmp, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 115 | &top_reg_tmp); |
| 116 | /* base register of the rbnode to be added */ |
| 117 | base_reg = rbnode->base_reg; |
| 118 | parent = *new; |
| 119 | /* if this register has already been inserted, just return */ |
| 120 | if (base_reg >= base_reg_tmp && |
| 121 | base_reg <= top_reg_tmp) |
| 122 | return 0; |
| 123 | else if (base_reg > top_reg_tmp) |
| 124 | new = &((*new)->rb_right); |
| 125 | else if (base_reg < base_reg_tmp) |
| 126 | new = &((*new)->rb_left); |
| 127 | } |
| 128 | |
| 129 | /* insert the node into the rbtree */ |
| 130 | rb_link_node(&rbnode->node, parent, new); |
| 131 | rb_insert_color(&rbnode->node, root); |
| 132 | |
| 133 | return 1; |
| 134 | } |
| 135 | |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 136 | #ifdef CONFIG_DEBUG_FS |
| 137 | static int rbtree_show(struct seq_file *s, void *ignored) |
| 138 | { |
| 139 | struct regmap *map = s->private; |
| 140 | struct regcache_rbtree_ctx *rbtree_ctx = map->cache; |
| 141 | struct regcache_rbtree_node *n; |
| 142 | struct rb_node *node; |
| 143 | unsigned int base, top; |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 144 | size_t mem_size; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 145 | int nodes = 0; |
| 146 | int registers = 0; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 147 | int this_registers, average; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 148 | |
Lars-Peter Clausen | f20c783 | 2013-05-23 17:23:49 +0200 | [diff] [blame] | 149 | map->lock(map->lock_arg); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 150 | |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 151 | mem_size = sizeof(*rbtree_ctx); |
| 152 | |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 153 | for (node = rb_first(&rbtree_ctx->root); node != NULL; |
| 154 | node = rb_next(node)) { |
| 155 | n = container_of(node, struct regcache_rbtree_node, node); |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 156 | mem_size += sizeof(*n); |
| 157 | mem_size += (n->blklen * map->cache_word_size); |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 158 | mem_size += BITS_TO_LONGS(n->blklen) * sizeof(long); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 159 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 160 | regcache_rbtree_get_base_top_reg(map, n, &base, &top); |
| 161 | this_registers = ((top - base) / map->reg_stride) + 1; |
| 162 | seq_printf(s, "%x-%x (%d)\n", base, top, this_registers); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 163 | |
| 164 | nodes++; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 165 | registers += this_registers; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Stephen Warren | c04c1b9 | 2012-04-04 15:48:33 -0600 | [diff] [blame] | 168 | if (nodes) |
| 169 | average = registers / nodes; |
| 170 | else |
| 171 | average = 0; |
| 172 | |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 173 | seq_printf(s, "%d nodes, %d registers, average %d registers, used %zu bytes\n", |
| 174 | nodes, registers, average, mem_size); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 175 | |
Lars-Peter Clausen | f20c783 | 2013-05-23 17:23:49 +0200 | [diff] [blame] | 176 | map->unlock(map->lock_arg); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int rbtree_open(struct inode *inode, struct file *file) |
| 182 | { |
| 183 | return single_open(file, rbtree_show, inode->i_private); |
| 184 | } |
| 185 | |
| 186 | static const struct file_operations rbtree_fops = { |
| 187 | .open = rbtree_open, |
| 188 | .read = seq_read, |
| 189 | .llseek = seq_lseek, |
| 190 | .release = single_release, |
| 191 | }; |
Mark Brown | cce585c | 2011-11-22 11:33:31 +0000 | [diff] [blame] | 192 | |
| 193 | static void rbtree_debugfs_init(struct regmap *map) |
| 194 | { |
| 195 | debugfs_create_file("rbtree", 0400, map->debugfs, map, &rbtree_fops); |
| 196 | } |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 197 | #endif |
| 198 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 199 | static int regcache_rbtree_init(struct regmap *map) |
| 200 | { |
| 201 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 202 | int i; |
| 203 | int ret; |
| 204 | |
| 205 | map->cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL); |
| 206 | if (!map->cache) |
| 207 | return -ENOMEM; |
| 208 | |
| 209 | rbtree_ctx = map->cache; |
| 210 | rbtree_ctx->root = RB_ROOT; |
| 211 | rbtree_ctx->cached_rbnode = NULL; |
| 212 | |
| 213 | for (i = 0; i < map->num_reg_defaults; i++) { |
| 214 | ret = regcache_rbtree_write(map, |
| 215 | map->reg_defaults[i].reg, |
| 216 | map->reg_defaults[i].def); |
| 217 | if (ret) |
| 218 | goto err; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | |
| 223 | err: |
Lars-Peter Clausen | 462a185 | 2011-11-15 13:34:40 +0100 | [diff] [blame] | 224 | regcache_rbtree_exit(map); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static int regcache_rbtree_exit(struct regmap *map) |
| 229 | { |
| 230 | struct rb_node *next; |
| 231 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 232 | struct regcache_rbtree_node *rbtree_node; |
| 233 | |
| 234 | /* if we've already been called then just return */ |
| 235 | rbtree_ctx = map->cache; |
| 236 | if (!rbtree_ctx) |
| 237 | return 0; |
| 238 | |
| 239 | /* free up the rbtree */ |
| 240 | next = rb_first(&rbtree_ctx->root); |
| 241 | while (next) { |
| 242 | rbtree_node = rb_entry(next, struct regcache_rbtree_node, node); |
| 243 | next = rb_next(&rbtree_node->node); |
| 244 | rb_erase(&rbtree_node->node, &rbtree_ctx->root); |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 245 | kfree(rbtree_node->cache_present); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 246 | kfree(rbtree_node->block); |
| 247 | kfree(rbtree_node); |
| 248 | } |
| 249 | |
| 250 | /* release the resources */ |
| 251 | kfree(map->cache); |
| 252 | map->cache = NULL; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int regcache_rbtree_read(struct regmap *map, |
| 258 | unsigned int reg, unsigned int *value) |
| 259 | { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 260 | struct regcache_rbtree_node *rbnode; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 261 | unsigned int reg_tmp; |
| 262 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 263 | rbnode = regcache_rbtree_lookup(map, reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 264 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 265 | reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 266 | if (!test_bit(reg_tmp, rbnode->cache_present)) |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 267 | return -ENOENT; |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 268 | *value = regcache_rbtree_get_register(map, rbnode, reg_tmp); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 269 | } else { |
Mark Brown | 6e6ace0 | 2011-10-09 13:23:31 +0100 | [diff] [blame] | 270 | return -ENOENT; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 277 | static int regcache_rbtree_insert_to_block(struct regmap *map, |
| 278 | struct regcache_rbtree_node *rbnode, |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 279 | unsigned int base_reg, |
| 280 | unsigned int top_reg, |
| 281 | unsigned int reg, |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 282 | unsigned int value) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 283 | { |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 284 | unsigned int blklen; |
| 285 | unsigned int pos, offset; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 286 | unsigned long *present; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 287 | u8 *blk; |
| 288 | |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 289 | blklen = (top_reg - base_reg) / map->reg_stride + 1; |
| 290 | pos = (reg - base_reg) / map->reg_stride; |
| 291 | offset = (rbnode->base_reg - base_reg) / map->reg_stride; |
| 292 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 293 | blk = krealloc(rbnode->block, |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 294 | blklen * map->cache_word_size, |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 295 | GFP_KERNEL); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 296 | if (!blk) |
| 297 | return -ENOMEM; |
| 298 | |
Guenter Roeck | 8ef9724 | 2015-07-26 21:34:50 -0700 | [diff] [blame] | 299 | if (BITS_TO_LONGS(blklen) > BITS_TO_LONGS(rbnode->blklen)) { |
| 300 | present = krealloc(rbnode->cache_present, |
| 301 | BITS_TO_LONGS(blklen) * sizeof(*present), |
| 302 | GFP_KERNEL); |
| 303 | if (!present) { |
| 304 | kfree(blk); |
| 305 | return -ENOMEM; |
| 306 | } |
| 307 | |
| 308 | memset(present + BITS_TO_LONGS(rbnode->blklen), 0, |
| 309 | (BITS_TO_LONGS(blklen) - BITS_TO_LONGS(rbnode->blklen)) |
| 310 | * sizeof(*present)); |
| 311 | } else { |
| 312 | present = rbnode->cache_present; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 315 | /* insert the register value in the correct place in the rbnode block */ |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 316 | if (pos == 0) { |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 317 | memmove(blk + offset * map->cache_word_size, |
| 318 | blk, rbnode->blklen * map->cache_word_size); |
Lars-Peter Clausen | 328f494 | 2015-03-07 17:10:01 +0100 | [diff] [blame] | 319 | bitmap_shift_left(present, present, offset, blklen); |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 320 | } |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 321 | |
| 322 | /* update the rbnode block, its size and the base register */ |
| 323 | rbnode->block = blk; |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 324 | rbnode->blklen = blklen; |
| 325 | rbnode->base_reg = base_reg; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 326 | rbnode->cache_present = present; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 327 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 328 | regcache_rbtree_set_register(map, rbnode, pos, value); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 332 | static struct regcache_rbtree_node * |
| 333 | regcache_rbtree_node_alloc(struct regmap *map, unsigned int reg) |
| 334 | { |
| 335 | struct regcache_rbtree_node *rbnode; |
Mark Brown | 7278af5 | 2013-05-08 13:55:25 +0100 | [diff] [blame] | 336 | const struct regmap_range *range; |
| 337 | int i; |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 338 | |
| 339 | rbnode = kzalloc(sizeof(*rbnode), GFP_KERNEL); |
| 340 | if (!rbnode) |
| 341 | return NULL; |
| 342 | |
Mark Brown | 7278af5 | 2013-05-08 13:55:25 +0100 | [diff] [blame] | 343 | /* If there is a read table then use it to guess at an allocation */ |
| 344 | if (map->rd_table) { |
| 345 | for (i = 0; i < map->rd_table->n_yes_ranges; i++) { |
| 346 | if (regmap_reg_in_range(reg, |
| 347 | &map->rd_table->yes_ranges[i])) |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | if (i != map->rd_table->n_yes_ranges) { |
| 352 | range = &map->rd_table->yes_ranges[i]; |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 353 | rbnode->blklen = (range->range_max - range->range_min) / |
| 354 | map->reg_stride + 1; |
Mark Brown | 7278af5 | 2013-05-08 13:55:25 +0100 | [diff] [blame] | 355 | rbnode->base_reg = range->range_min; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (!rbnode->blklen) { |
David Jander | 4e67fb5 | 2013-08-21 17:37:22 +0200 | [diff] [blame] | 360 | rbnode->blklen = 1; |
Mark Brown | 7278af5 | 2013-05-08 13:55:25 +0100 | [diff] [blame] | 361 | rbnode->base_reg = reg; |
| 362 | } |
| 363 | |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 364 | rbnode->block = kmalloc(rbnode->blklen * map->cache_word_size, |
| 365 | GFP_KERNEL); |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 366 | if (!rbnode->block) |
| 367 | goto err_free; |
| 368 | |
| 369 | rbnode->cache_present = kzalloc(BITS_TO_LONGS(rbnode->blklen) * |
| 370 | sizeof(*rbnode->cache_present), GFP_KERNEL); |
| 371 | if (!rbnode->cache_present) |
| 372 | goto err_free_block; |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 373 | |
| 374 | return rbnode; |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 375 | |
| 376 | err_free_block: |
| 377 | kfree(rbnode->block); |
| 378 | err_free: |
| 379 | kfree(rbnode); |
| 380 | return NULL; |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 383 | static int regcache_rbtree_write(struct regmap *map, unsigned int reg, |
| 384 | unsigned int value) |
| 385 | { |
| 386 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 387 | struct regcache_rbtree_node *rbnode, *rbnode_tmp; |
| 388 | struct rb_node *node; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 389 | unsigned int reg_tmp; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 390 | int ret; |
| 391 | |
| 392 | rbtree_ctx = map->cache; |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 393 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 394 | /* if we can't locate it in the cached rbnode we'll have |
| 395 | * to traverse the rbtree looking for it. |
| 396 | */ |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 397 | rbnode = regcache_rbtree_lookup(map, reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 398 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 399 | reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 400 | regcache_rbtree_set_register(map, rbnode, reg_tmp, value); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 401 | } else { |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 402 | unsigned int base_reg, top_reg; |
| 403 | unsigned int new_base_reg, new_top_reg; |
| 404 | unsigned int min, max; |
| 405 | unsigned int max_dist; |
| 406 | |
| 407 | max_dist = map->reg_stride * sizeof(*rbnode_tmp) / |
| 408 | map->cache_word_size; |
| 409 | if (reg < max_dist) |
| 410 | min = 0; |
| 411 | else |
| 412 | min = reg - max_dist; |
| 413 | max = reg + max_dist; |
| 414 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 415 | /* look for an adjacent register to the one we are about to add */ |
Nikesh Oswal | 6399aea | 2015-10-21 14:16:14 +0100 | [diff] [blame^] | 416 | node = rbtree_ctx->root.rb_node; |
| 417 | while (node) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 418 | rbnode_tmp = rb_entry(node, struct regcache_rbtree_node, |
| 419 | node); |
Lars-Peter Clausen | 194c753 | 2013-08-29 10:26:32 +0200 | [diff] [blame] | 420 | |
| 421 | regcache_rbtree_get_base_top_reg(map, rbnode_tmp, |
| 422 | &base_reg, &top_reg); |
| 423 | |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 424 | if (base_reg <= max && top_reg >= min) { |
| 425 | new_base_reg = min(reg, base_reg); |
| 426 | new_top_reg = max(reg, top_reg); |
| 427 | } else { |
Nikesh Oswal | 6399aea | 2015-10-21 14:16:14 +0100 | [diff] [blame^] | 428 | if (max < base_reg) |
| 429 | node = node->rb_left; |
| 430 | else |
| 431 | node = node->rb_right; |
| 432 | |
Lars-Peter Clausen | 194c753 | 2013-08-29 10:26:32 +0200 | [diff] [blame] | 433 | continue; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 434 | } |
Lars-Peter Clausen | 194c753 | 2013-08-29 10:26:32 +0200 | [diff] [blame] | 435 | |
| 436 | ret = regcache_rbtree_insert_to_block(map, rbnode_tmp, |
Lars-Peter Clausen | 472fdec | 2013-08-29 10:26:33 +0200 | [diff] [blame] | 437 | new_base_reg, |
| 438 | new_top_reg, reg, |
| 439 | value); |
Lars-Peter Clausen | 194c753 | 2013-08-29 10:26:32 +0200 | [diff] [blame] | 440 | if (ret) |
| 441 | return ret; |
| 442 | rbtree_ctx->cached_rbnode = rbnode_tmp; |
| 443 | return 0; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 444 | } |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 445 | |
| 446 | /* We did not manage to find a place to insert it in |
| 447 | * an existing block so create a new rbnode. |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 448 | */ |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 449 | rbnode = regcache_rbtree_node_alloc(map, reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 450 | if (!rbnode) |
| 451 | return -ENOMEM; |
Mark Brown | 0186645 | 2013-05-08 13:55:24 +0100 | [diff] [blame] | 452 | regcache_rbtree_set_register(map, rbnode, |
| 453 | reg - rbnode->base_reg, value); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 454 | regcache_rbtree_insert(map, &rbtree_ctx->root, rbnode); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 455 | rbtree_ctx->cached_rbnode = rbnode; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 461 | static int regcache_rbtree_sync(struct regmap *map, unsigned int min, |
| 462 | unsigned int max) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 463 | { |
| 464 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 465 | struct rb_node *node; |
| 466 | struct regcache_rbtree_node *rbnode; |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 467 | unsigned int base_reg, top_reg; |
| 468 | unsigned int start, end; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 469 | int ret; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 470 | |
| 471 | rbtree_ctx = map->cache; |
| 472 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 473 | rbnode = rb_entry(node, struct regcache_rbtree_node, node); |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 474 | |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 475 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 476 | &top_reg); |
| 477 | if (base_reg > max) |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 478 | break; |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 479 | if (top_reg < min) |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 480 | continue; |
| 481 | |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 482 | if (min > base_reg) |
| 483 | start = (min - base_reg) / map->reg_stride; |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 484 | else |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 485 | start = 0; |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 486 | |
Lars-Peter Clausen | b675212 | 2013-08-27 13:03:03 +0200 | [diff] [blame] | 487 | if (max < top_reg) |
| 488 | end = (max - base_reg) / map->reg_stride + 1; |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 489 | else |
| 490 | end = rbnode->blklen; |
| 491 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 492 | ret = regcache_sync_block(map, rbnode->block, |
| 493 | rbnode->cache_present, |
| 494 | rbnode->base_reg, start, end); |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 495 | if (ret != 0) |
| 496 | return ret; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 499 | return regmap_async_complete(map); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 502 | static int regcache_rbtree_drop(struct regmap *map, unsigned int min, |
| 503 | unsigned int max) |
| 504 | { |
| 505 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 506 | struct regcache_rbtree_node *rbnode; |
| 507 | struct rb_node *node; |
| 508 | unsigned int base_reg, top_reg; |
| 509 | unsigned int start, end; |
| 510 | |
| 511 | rbtree_ctx = map->cache; |
| 512 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 513 | rbnode = rb_entry(node, struct regcache_rbtree_node, node); |
| 514 | |
| 515 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 516 | &top_reg); |
| 517 | if (base_reg > max) |
| 518 | break; |
| 519 | if (top_reg < min) |
| 520 | continue; |
| 521 | |
| 522 | if (min > base_reg) |
| 523 | start = (min - base_reg) / map->reg_stride; |
| 524 | else |
| 525 | start = 0; |
| 526 | |
| 527 | if (max < top_reg) |
| 528 | end = (max - base_reg) / map->reg_stride + 1; |
| 529 | else |
| 530 | end = rbnode->blklen; |
| 531 | |
| 532 | bitmap_clear(rbnode->cache_present, start, end - start); |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 538 | struct regcache_ops regcache_rbtree_ops = { |
| 539 | .type = REGCACHE_RBTREE, |
| 540 | .name = "rbtree", |
| 541 | .init = regcache_rbtree_init, |
| 542 | .exit = regcache_rbtree_exit, |
Lars-Peter Clausen | 5e0cbe7 | 2014-08-24 15:32:27 +0200 | [diff] [blame] | 543 | #ifdef CONFIG_DEBUG_FS |
| 544 | .debugfs_init = rbtree_debugfs_init, |
| 545 | #endif |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 546 | .read = regcache_rbtree_read, |
| 547 | .write = regcache_rbtree_write, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 548 | .sync = regcache_rbtree_sync, |
| 549 | .drop = regcache_rbtree_drop, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 550 | }; |