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 | |
| 13 | #include <linux/slab.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 14 | #include <linux/device.h> |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 15 | #include <linux/debugfs.h> |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 16 | #include <linux/rbtree.h> |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 17 | #include <linux/seq_file.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 { |
| 26 | /* the actual rbtree node holding this block */ |
| 27 | struct rb_node node; |
| 28 | /* base register handled by this block */ |
| 29 | unsigned int base_reg; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 30 | /* block of adjacent registers */ |
| 31 | void *block; |
| 32 | /* number of registers available in the block */ |
| 33 | unsigned int blklen; |
| 34 | } __attribute__ ((packed)); |
| 35 | |
| 36 | struct regcache_rbtree_ctx { |
| 37 | struct rb_root root; |
| 38 | struct regcache_rbtree_node *cached_rbnode; |
| 39 | }; |
| 40 | |
| 41 | static inline void regcache_rbtree_get_base_top_reg( |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 42 | struct regmap *map, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 43 | struct regcache_rbtree_node *rbnode, |
| 44 | unsigned int *base, unsigned int *top) |
| 45 | { |
| 46 | *base = rbnode->base_reg; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 47 | *top = rbnode->base_reg + ((rbnode->blklen - 1) * map->reg_stride); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 50 | static unsigned int regcache_rbtree_get_register(struct regmap *map, |
| 51 | struct regcache_rbtree_node *rbnode, unsigned int idx) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 52 | { |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 53 | return regcache_get_val(map, rbnode->block, idx); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 56 | static void regcache_rbtree_set_register(struct regmap *map, |
| 57 | struct regcache_rbtree_node *rbnode, |
| 58 | unsigned int idx, unsigned int val) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 59 | { |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 60 | regcache_set_val(map, rbnode->block, idx, val); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 63 | static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map, |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 64 | unsigned int reg) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 65 | { |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 66 | struct regcache_rbtree_ctx *rbtree_ctx = map->cache; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 67 | struct rb_node *node; |
| 68 | struct regcache_rbtree_node *rbnode; |
| 69 | unsigned int base_reg, top_reg; |
| 70 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 71 | rbnode = rbtree_ctx->cached_rbnode; |
| 72 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 73 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 74 | &top_reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 75 | if (reg >= base_reg && reg <= top_reg) |
| 76 | return rbnode; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | node = rbtree_ctx->root.rb_node; |
| 80 | while (node) { |
| 81 | rbnode = container_of(node, struct regcache_rbtree_node, node); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 82 | regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, |
| 83 | &top_reg); |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 84 | if (reg >= base_reg && reg <= top_reg) { |
| 85 | rbtree_ctx->cached_rbnode = rbnode; |
| 86 | return rbnode; |
| 87 | } else if (reg > top_reg) { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 88 | node = node->rb_right; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 89 | } else if (reg < base_reg) { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 90 | node = node->rb_left; |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 91 | } |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 97 | static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 98 | struct regcache_rbtree_node *rbnode) |
| 99 | { |
| 100 | struct rb_node **new, *parent; |
| 101 | struct regcache_rbtree_node *rbnode_tmp; |
| 102 | unsigned int base_reg_tmp, top_reg_tmp; |
| 103 | unsigned int base_reg; |
| 104 | |
| 105 | parent = NULL; |
| 106 | new = &root->rb_node; |
| 107 | while (*new) { |
| 108 | rbnode_tmp = container_of(*new, struct regcache_rbtree_node, |
| 109 | node); |
| 110 | /* base and top registers of the current rbnode */ |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 111 | regcache_rbtree_get_base_top_reg(map, rbnode_tmp, &base_reg_tmp, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 112 | &top_reg_tmp); |
| 113 | /* base register of the rbnode to be added */ |
| 114 | base_reg = rbnode->base_reg; |
| 115 | parent = *new; |
| 116 | /* if this register has already been inserted, just return */ |
| 117 | if (base_reg >= base_reg_tmp && |
| 118 | base_reg <= top_reg_tmp) |
| 119 | return 0; |
| 120 | else if (base_reg > top_reg_tmp) |
| 121 | new = &((*new)->rb_right); |
| 122 | else if (base_reg < base_reg_tmp) |
| 123 | new = &((*new)->rb_left); |
| 124 | } |
| 125 | |
| 126 | /* insert the node into the rbtree */ |
| 127 | rb_link_node(&rbnode->node, parent, new); |
| 128 | rb_insert_color(&rbnode->node, root); |
| 129 | |
| 130 | return 1; |
| 131 | } |
| 132 | |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 133 | #ifdef CONFIG_DEBUG_FS |
| 134 | static int rbtree_show(struct seq_file *s, void *ignored) |
| 135 | { |
| 136 | struct regmap *map = s->private; |
| 137 | struct regcache_rbtree_ctx *rbtree_ctx = map->cache; |
| 138 | struct regcache_rbtree_node *n; |
| 139 | struct rb_node *node; |
| 140 | unsigned int base, top; |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 141 | size_t mem_size; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 142 | int nodes = 0; |
| 143 | int registers = 0; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 144 | int this_registers, average; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 145 | |
Lars-Peter Clausen | f20c783 | 2013-05-23 17:23:49 +0200 | [diff] [blame] | 146 | map->lock(map->lock_arg); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 147 | |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 148 | mem_size = sizeof(*rbtree_ctx); |
Mark Brown | 78493f2 | 2013-03-29 19:18:59 +0000 | [diff] [blame] | 149 | mem_size += BITS_TO_LONGS(map->cache_present_nbits) * sizeof(long); |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 150 | |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 151 | for (node = rb_first(&rbtree_ctx->root); node != NULL; |
| 152 | node = rb_next(node)) { |
| 153 | n = container_of(node, struct regcache_rbtree_node, node); |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 154 | mem_size += sizeof(*n); |
| 155 | mem_size += (n->blklen * map->cache_word_size); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 156 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 157 | regcache_rbtree_get_base_top_reg(map, n, &base, &top); |
| 158 | this_registers = ((top - base) / map->reg_stride) + 1; |
| 159 | seq_printf(s, "%x-%x (%d)\n", base, top, this_registers); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 160 | |
| 161 | nodes++; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 162 | registers += this_registers; |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Warren | c04c1b9 | 2012-04-04 15:48:33 -0600 | [diff] [blame] | 165 | if (nodes) |
| 166 | average = registers / nodes; |
| 167 | else |
| 168 | average = 0; |
| 169 | |
Dimitris Papastamos | a42277c | 2013-03-12 17:26:49 +0000 | [diff] [blame] | 170 | seq_printf(s, "%d nodes, %d registers, average %d registers, used %zu bytes\n", |
| 171 | nodes, registers, average, mem_size); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 172 | |
Lars-Peter Clausen | f20c783 | 2013-05-23 17:23:49 +0200 | [diff] [blame] | 173 | map->unlock(map->lock_arg); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int rbtree_open(struct inode *inode, struct file *file) |
| 179 | { |
| 180 | return single_open(file, rbtree_show, inode->i_private); |
| 181 | } |
| 182 | |
| 183 | static const struct file_operations rbtree_fops = { |
| 184 | .open = rbtree_open, |
| 185 | .read = seq_read, |
| 186 | .llseek = seq_lseek, |
| 187 | .release = single_release, |
| 188 | }; |
Mark Brown | cce585c | 2011-11-22 11:33:31 +0000 | [diff] [blame] | 189 | |
| 190 | static void rbtree_debugfs_init(struct regmap *map) |
| 191 | { |
| 192 | debugfs_create_file("rbtree", 0400, map->debugfs, map, &rbtree_fops); |
| 193 | } |
| 194 | #else |
| 195 | static void rbtree_debugfs_init(struct regmap *map) |
| 196 | { |
| 197 | } |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 198 | #endif |
| 199 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 200 | static int regcache_rbtree_init(struct regmap *map) |
| 201 | { |
| 202 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 203 | int i; |
| 204 | int ret; |
| 205 | |
| 206 | map->cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL); |
| 207 | if (!map->cache) |
| 208 | return -ENOMEM; |
| 209 | |
| 210 | rbtree_ctx = map->cache; |
| 211 | rbtree_ctx->root = RB_ROOT; |
| 212 | rbtree_ctx->cached_rbnode = NULL; |
| 213 | |
| 214 | for (i = 0; i < map->num_reg_defaults; i++) { |
| 215 | ret = regcache_rbtree_write(map, |
| 216 | map->reg_defaults[i].reg, |
| 217 | map->reg_defaults[i].def); |
| 218 | if (ret) |
| 219 | goto err; |
| 220 | } |
| 221 | |
Mark Brown | cce585c | 2011-11-22 11:33:31 +0000 | [diff] [blame] | 222 | rbtree_debugfs_init(map); |
Mark Brown | bad2ab4 | 2011-11-21 19:44:44 +0000 | [diff] [blame] | 223 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 224 | return 0; |
| 225 | |
| 226 | err: |
Lars-Peter Clausen | 462a185 | 2011-11-15 13:34:40 +0100 | [diff] [blame] | 227 | regcache_rbtree_exit(map); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int regcache_rbtree_exit(struct regmap *map) |
| 232 | { |
| 233 | struct rb_node *next; |
| 234 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 235 | struct regcache_rbtree_node *rbtree_node; |
| 236 | |
| 237 | /* if we've already been called then just return */ |
| 238 | rbtree_ctx = map->cache; |
| 239 | if (!rbtree_ctx) |
| 240 | return 0; |
| 241 | |
| 242 | /* free up the rbtree */ |
| 243 | next = rb_first(&rbtree_ctx->root); |
| 244 | while (next) { |
| 245 | rbtree_node = rb_entry(next, struct regcache_rbtree_node, node); |
| 246 | next = rb_next(&rbtree_node->node); |
| 247 | rb_erase(&rbtree_node->node, &rbtree_ctx->root); |
| 248 | kfree(rbtree_node->block); |
| 249 | kfree(rbtree_node); |
| 250 | } |
| 251 | |
| 252 | /* release the resources */ |
| 253 | kfree(map->cache); |
| 254 | map->cache = NULL; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int regcache_rbtree_read(struct regmap *map, |
| 260 | unsigned int reg, unsigned int *value) |
| 261 | { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 262 | struct regcache_rbtree_node *rbnode; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 263 | unsigned int reg_tmp; |
| 264 | |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 265 | rbnode = regcache_rbtree_lookup(map, reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 266 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 267 | reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 268 | if (!regcache_reg_present(map, reg)) |
| 269 | return -ENOENT; |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 270 | *value = regcache_rbtree_get_register(map, rbnode, reg_tmp); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 271 | } else { |
Mark Brown | 6e6ace0 | 2011-10-09 13:23:31 +0100 | [diff] [blame] | 272 | return -ENOENT; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 279 | static int regcache_rbtree_insert_to_block(struct regmap *map, |
| 280 | struct regcache_rbtree_node *rbnode, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 281 | unsigned int pos, 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 | { |
| 284 | u8 *blk; |
| 285 | |
| 286 | blk = krealloc(rbnode->block, |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 287 | (rbnode->blklen + 1) * map->cache_word_size, |
| 288 | GFP_KERNEL); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 289 | if (!blk) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | /* insert the register value in the correct place in the rbnode block */ |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 293 | memmove(blk + (pos + 1) * map->cache_word_size, |
| 294 | blk + pos * map->cache_word_size, |
| 295 | (rbnode->blklen - pos) * map->cache_word_size); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 296 | |
| 297 | /* update the rbnode block, its size and the base register */ |
| 298 | rbnode->block = blk; |
| 299 | rbnode->blklen++; |
| 300 | if (!pos) |
| 301 | rbnode->base_reg = reg; |
| 302 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 303 | regcache_rbtree_set_register(map, rbnode, pos, value); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int regcache_rbtree_write(struct regmap *map, unsigned int reg, |
| 308 | unsigned int value) |
| 309 | { |
| 310 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 311 | struct regcache_rbtree_node *rbnode, *rbnode_tmp; |
| 312 | struct rb_node *node; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 313 | unsigned int reg_tmp; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 314 | unsigned int pos; |
| 315 | int i; |
| 316 | int ret; |
| 317 | |
| 318 | rbtree_ctx = map->cache; |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 319 | /* update the reg_present bitmap, make space if necessary */ |
Mark Brown | 78493f2 | 2013-03-29 19:18:59 +0000 | [diff] [blame] | 320 | ret = regcache_set_reg_present(map, reg); |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 321 | if (ret < 0) |
| 322 | return ret; |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 323 | |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 324 | /* if we can't locate it in the cached rbnode we'll have |
| 325 | * to traverse the rbtree looking for it. |
| 326 | */ |
Lars-Peter Clausen | 3405add | 2011-09-27 20:15:38 +0200 | [diff] [blame] | 327 | rbnode = regcache_rbtree_lookup(map, reg); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 328 | if (rbnode) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 329 | reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 330 | regcache_rbtree_set_register(map, rbnode, reg_tmp, value); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 331 | } else { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 332 | /* look for an adjacent register to the one we are about to add */ |
| 333 | for (node = rb_first(&rbtree_ctx->root); node; |
| 334 | node = rb_next(node)) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 335 | rbnode_tmp = rb_entry(node, struct regcache_rbtree_node, |
| 336 | node); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 337 | for (i = 0; i < rbnode_tmp->blklen; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 338 | reg_tmp = rbnode_tmp->base_reg + |
| 339 | (i * map->reg_stride); |
| 340 | if (abs(reg_tmp - reg) != map->reg_stride) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 341 | continue; |
| 342 | /* decide where in the block to place our register */ |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 343 | if (reg_tmp + map->reg_stride == reg) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 344 | pos = i + 1; |
| 345 | else |
| 346 | pos = i; |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 347 | ret = regcache_rbtree_insert_to_block(map, |
| 348 | rbnode_tmp, |
| 349 | pos, reg, |
| 350 | value); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 351 | if (ret) |
| 352 | return ret; |
| 353 | rbtree_ctx->cached_rbnode = rbnode_tmp; |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | /* we did not manage to find a place to insert it in an existing |
| 358 | * block so create a new rbnode with a single register in its block. |
| 359 | * This block will get populated further if any other adjacent |
| 360 | * registers get modified in the future. |
| 361 | */ |
| 362 | rbnode = kzalloc(sizeof *rbnode, GFP_KERNEL); |
| 363 | if (!rbnode) |
| 364 | return -ENOMEM; |
Dimitris Papastamos | 0c7ed856 | 2013-03-15 14:54:35 +0000 | [diff] [blame] | 365 | rbnode->blklen = sizeof(*rbnode); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 366 | rbnode->base_reg = reg; |
Dimitris Papastamos | 25ed115 | 2011-09-27 11:25:07 +0100 | [diff] [blame] | 367 | rbnode->block = kmalloc(rbnode->blklen * map->cache_word_size, |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 368 | GFP_KERNEL); |
| 369 | if (!rbnode->block) { |
| 370 | kfree(rbnode); |
| 371 | return -ENOMEM; |
| 372 | } |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 373 | regcache_rbtree_set_register(map, rbnode, 0, value); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 374 | regcache_rbtree_insert(map, &rbtree_ctx->root, rbnode); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 375 | rbtree_ctx->cached_rbnode = rbnode; |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 381 | static int regcache_rbtree_sync(struct regmap *map, unsigned int min, |
| 382 | unsigned int max) |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 383 | { |
| 384 | struct regcache_rbtree_ctx *rbtree_ctx; |
| 385 | struct rb_node *node; |
| 386 | struct regcache_rbtree_node *rbnode; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 387 | int ret; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 388 | int base, end; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 389 | |
| 390 | rbtree_ctx = map->cache; |
| 391 | for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { |
| 392 | rbnode = rb_entry(node, struct regcache_rbtree_node, node); |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 393 | |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 394 | if (rbnode->base_reg > max) |
| 395 | break; |
| 396 | if (rbnode->base_reg + rbnode->blklen < min) |
| 397 | continue; |
| 398 | |
Mark Brown | f9353e7 | 2012-03-05 23:28:49 +0000 | [diff] [blame] | 399 | if (min > rbnode->base_reg) |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 400 | base = min - rbnode->base_reg; |
| 401 | else |
| 402 | base = 0; |
| 403 | |
| 404 | if (max < rbnode->base_reg + rbnode->blklen) |
Lars-Peter Clausen | 8abac3b | 2013-03-13 16:38:33 +0100 | [diff] [blame] | 405 | end = max - rbnode->base_reg + 1; |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 406 | else |
| 407 | end = rbnode->blklen; |
| 408 | |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 409 | ret = regcache_sync_block(map, rbnode->block, rbnode->base_reg, |
| 410 | base, end); |
| 411 | if (ret != 0) |
| 412 | return ret; |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 415 | return regmap_async_complete(map); |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | struct regcache_ops regcache_rbtree_ops = { |
| 419 | .type = REGCACHE_RBTREE, |
| 420 | .name = "rbtree", |
| 421 | .init = regcache_rbtree_init, |
| 422 | .exit = regcache_rbtree_exit, |
| 423 | .read = regcache_rbtree_read, |
| 424 | .write = regcache_rbtree_write, |
| 425 | .sync = regcache_rbtree_sync |
| 426 | }; |