blob: 3b6cfede2fd9f0b2a5f69d5ed786a7100f6b23a0 [file] [log] [blame]
Dimitris Papastamos28644c802011-09-19 14:34:02 +01001/*
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 Brownbad2ab42011-11-21 19:44:44 +000013#include <linux/debugfs.h>
Xiubo Lie39be3a2014-10-09 17:02:52 +080014#include <linux/device.h>
Dimitris Papastamos28644c802011-09-19 14:34:02 +010015#include <linux/rbtree.h>
Mark Brownbad2ab42011-11-21 19:44:44 +000016#include <linux/seq_file.h>
Xiubo Lie39be3a2014-10-09 17:02:52 +080017#include <linux/slab.h>
Dimitris Papastamos28644c802011-09-19 14:34:02 +010018
19#include "internal.h"
20
21static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
22 unsigned int value);
Lars-Peter Clausen462a1852011-11-15 13:34:40 +010023static int regcache_rbtree_exit(struct regmap *map);
Dimitris Papastamos28644c802011-09-19 14:34:02 +010024
25struct regcache_rbtree_node {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010026 /* block of adjacent registers */
27 void *block;
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +020028 /* Which registers are present */
29 long *cache_present;
Jean-Christophe PINCE70d383b2014-04-01 13:26:48 -070030 /* base register handled by this block */
31 unsigned int base_reg;
Dimitris Papastamos28644c802011-09-19 14:34:02 +010032 /* number of registers available in the block */
33 unsigned int blklen;
Jean-Christophe PINCE70d383b2014-04-01 13:26:48 -070034 /* the actual rbtree node holding this block */
35 struct rb_node node;
Dimitris Papastamos28644c802011-09-19 14:34:02 +010036} __attribute__ ((packed));
37
38struct regcache_rbtree_ctx {
39 struct rb_root root;
40 struct regcache_rbtree_node *cached_rbnode;
41};
42
43static inline void regcache_rbtree_get_base_top_reg(
Stephen Warrenf01ee602012-04-09 13:40:24 -060044 struct regmap *map,
Dimitris Papastamos28644c802011-09-19 14:34:02 +010045 struct regcache_rbtree_node *rbnode,
46 unsigned int *base, unsigned int *top)
47{
48 *base = rbnode->base_reg;
Stephen Warrenf01ee602012-04-09 13:40:24 -060049 *top = rbnode->base_reg + ((rbnode->blklen - 1) * map->reg_stride);
Dimitris Papastamos28644c802011-09-19 14:34:02 +010050}
51
Mark Brown879082c2013-02-21 18:03:13 +000052static unsigned int regcache_rbtree_get_register(struct regmap *map,
53 struct regcache_rbtree_node *rbnode, unsigned int idx)
Dimitris Papastamos28644c802011-09-19 14:34:02 +010054{
Mark Brown879082c2013-02-21 18:03:13 +000055 return regcache_get_val(map, rbnode->block, idx);
Dimitris Papastamos28644c802011-09-19 14:34:02 +010056}
57
Mark Brown879082c2013-02-21 18:03:13 +000058static void regcache_rbtree_set_register(struct regmap *map,
59 struct regcache_rbtree_node *rbnode,
60 unsigned int idx, unsigned int val)
Dimitris Papastamos28644c802011-09-19 14:34:02 +010061{
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +020062 set_bit(idx, rbnode->cache_present);
Mark Brown879082c2013-02-21 18:03:13 +000063 regcache_set_val(map, rbnode->block, idx, val);
Dimitris Papastamos28644c802011-09-19 14:34:02 +010064}
65
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020066static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map,
Mark Brown879082c2013-02-21 18:03:13 +000067 unsigned int reg)
Dimitris Papastamos28644c802011-09-19 14:34:02 +010068{
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020069 struct regcache_rbtree_ctx *rbtree_ctx = map->cache;
Dimitris Papastamos28644c802011-09-19 14:34:02 +010070 struct rb_node *node;
71 struct regcache_rbtree_node *rbnode;
72 unsigned int base_reg, top_reg;
73
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020074 rbnode = rbtree_ctx->cached_rbnode;
75 if (rbnode) {
Stephen Warrenf01ee602012-04-09 13:40:24 -060076 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
77 &top_reg);
Dimitris Papastamos28644c802011-09-19 14:34:02 +010078 if (reg >= base_reg && reg <= top_reg)
79 return rbnode;
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020080 }
81
82 node = rbtree_ctx->root.rb_node;
83 while (node) {
84 rbnode = container_of(node, struct regcache_rbtree_node, node);
Stephen Warrenf01ee602012-04-09 13:40:24 -060085 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
86 &top_reg);
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020087 if (reg >= base_reg && reg <= top_reg) {
88 rbtree_ctx->cached_rbnode = rbnode;
89 return rbnode;
90 } else if (reg > top_reg) {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010091 node = node->rb_right;
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020092 } else if (reg < base_reg) {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010093 node = node->rb_left;
Lars-Peter Clausen3405add2011-09-27 20:15:38 +020094 }
Dimitris Papastamos28644c802011-09-19 14:34:02 +010095 }
96
97 return NULL;
98}
99
Stephen Warrenf01ee602012-04-09 13:40:24 -0600100static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root,
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100101 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 Warrenf01ee602012-04-09 13:40:24 -0600114 regcache_rbtree_get_base_top_reg(map, rbnode_tmp, &base_reg_tmp,
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100115 &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 Brownbad2ab42011-11-21 19:44:44 +0000136#ifdef CONFIG_DEBUG_FS
137static 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 Papastamosa42277c2013-03-12 17:26:49 +0000144 size_t mem_size;
Mark Brownbad2ab42011-11-21 19:44:44 +0000145 int nodes = 0;
146 int registers = 0;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600147 int this_registers, average;
Mark Brownbad2ab42011-11-21 19:44:44 +0000148
Lars-Peter Clausenf20c7832013-05-23 17:23:49 +0200149 map->lock(map->lock_arg);
Mark Brownbad2ab42011-11-21 19:44:44 +0000150
Dimitris Papastamosa42277c2013-03-12 17:26:49 +0000151 mem_size = sizeof(*rbtree_ctx);
152
Mark Brownbad2ab42011-11-21 19:44:44 +0000153 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 Papastamosa42277c2013-03-12 17:26:49 +0000156 mem_size += sizeof(*n);
157 mem_size += (n->blklen * map->cache_word_size);
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200158 mem_size += BITS_TO_LONGS(n->blklen) * sizeof(long);
Mark Brownbad2ab42011-11-21 19:44:44 +0000159
Stephen Warrenf01ee602012-04-09 13:40:24 -0600160 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 Brownbad2ab42011-11-21 19:44:44 +0000163
164 nodes++;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600165 registers += this_registers;
Mark Brownbad2ab42011-11-21 19:44:44 +0000166 }
167
Stephen Warrenc04c1b92012-04-04 15:48:33 -0600168 if (nodes)
169 average = registers / nodes;
170 else
171 average = 0;
172
Dimitris Papastamosa42277c2013-03-12 17:26:49 +0000173 seq_printf(s, "%d nodes, %d registers, average %d registers, used %zu bytes\n",
174 nodes, registers, average, mem_size);
Mark Brownbad2ab42011-11-21 19:44:44 +0000175
Lars-Peter Clausenf20c7832013-05-23 17:23:49 +0200176 map->unlock(map->lock_arg);
Mark Brownbad2ab42011-11-21 19:44:44 +0000177
178 return 0;
179}
180
181static int rbtree_open(struct inode *inode, struct file *file)
182{
183 return single_open(file, rbtree_show, inode->i_private);
184}
185
186static const struct file_operations rbtree_fops = {
187 .open = rbtree_open,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = single_release,
191};
Mark Browncce585c2011-11-22 11:33:31 +0000192
193static void rbtree_debugfs_init(struct regmap *map)
194{
195 debugfs_create_file("rbtree", 0400, map->debugfs, map, &rbtree_fops);
196}
Mark Brownbad2ab42011-11-21 19:44:44 +0000197#endif
198
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100199static 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
223err:
Lars-Peter Clausen462a1852011-11-15 13:34:40 +0100224 regcache_rbtree_exit(map);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100225 return ret;
226}
227
228static 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 Clausen3f4ff562013-08-29 10:26:34 +0200245 kfree(rbtree_node->cache_present);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100246 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
257static int regcache_rbtree_read(struct regmap *map,
258 unsigned int reg, unsigned int *value)
259{
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100260 struct regcache_rbtree_node *rbnode;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100261 unsigned int reg_tmp;
262
Lars-Peter Clausen3405add2011-09-27 20:15:38 +0200263 rbnode = regcache_rbtree_lookup(map, reg);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100264 if (rbnode) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600265 reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200266 if (!test_bit(reg_tmp, rbnode->cache_present))
Dimitris Papastamos0c7ed8562013-03-15 14:54:35 +0000267 return -ENOENT;
Mark Brown879082c2013-02-21 18:03:13 +0000268 *value = regcache_rbtree_get_register(map, rbnode, reg_tmp);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100269 } else {
Mark Brown6e6ace02011-10-09 13:23:31 +0100270 return -ENOENT;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100271 }
272
273 return 0;
274}
275
276
Mark Brown879082c2013-02-21 18:03:13 +0000277static int regcache_rbtree_insert_to_block(struct regmap *map,
278 struct regcache_rbtree_node *rbnode,
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200279 unsigned int base_reg,
280 unsigned int top_reg,
281 unsigned int reg,
Mark Brown879082c2013-02-21 18:03:13 +0000282 unsigned int value)
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100283{
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200284 unsigned int blklen;
285 unsigned int pos, offset;
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200286 unsigned long *present;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100287 u8 *blk;
288
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200289 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 Papastamos28644c802011-09-19 14:34:02 +0100293 blk = krealloc(rbnode->block,
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200294 blklen * map->cache_word_size,
Mark Brown879082c2013-02-21 18:03:13 +0000295 GFP_KERNEL);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100296 if (!blk)
297 return -ENOMEM;
298
Guenter Roeck8ef97242015-07-26 21:34:50 -0700299 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 Clausen3f4ff562013-08-29 10:26:34 +0200313 }
314
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100315 /* insert the register value in the correct place in the rbnode block */
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200316 if (pos == 0) {
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200317 memmove(blk + offset * map->cache_word_size,
318 blk, rbnode->blklen * map->cache_word_size);
Lars-Peter Clausen328f4942015-03-07 17:10:01 +0100319 bitmap_shift_left(present, present, offset, blklen);
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200320 }
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100321
322 /* update the rbnode block, its size and the base register */
323 rbnode->block = blk;
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200324 rbnode->blklen = blklen;
325 rbnode->base_reg = base_reg;
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200326 rbnode->cache_present = present;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100327
Mark Brown879082c2013-02-21 18:03:13 +0000328 regcache_rbtree_set_register(map, rbnode, pos, value);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100329 return 0;
330}
331
Mark Brown01866452013-05-08 13:55:24 +0100332static struct regcache_rbtree_node *
333regcache_rbtree_node_alloc(struct regmap *map, unsigned int reg)
334{
335 struct regcache_rbtree_node *rbnode;
Mark Brown7278af52013-05-08 13:55:25 +0100336 const struct regmap_range *range;
337 int i;
Mark Brown01866452013-05-08 13:55:24 +0100338
339 rbnode = kzalloc(sizeof(*rbnode), GFP_KERNEL);
340 if (!rbnode)
341 return NULL;
342
Mark Brown7278af52013-05-08 13:55:25 +0100343 /* 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 Clausenb6752122013-08-27 13:03:03 +0200353 rbnode->blklen = (range->range_max - range->range_min) /
354 map->reg_stride + 1;
Mark Brown7278af52013-05-08 13:55:25 +0100355 rbnode->base_reg = range->range_min;
356 }
357 }
358
359 if (!rbnode->blklen) {
David Jander4e67fb52013-08-21 17:37:22 +0200360 rbnode->blklen = 1;
Mark Brown7278af52013-05-08 13:55:25 +0100361 rbnode->base_reg = reg;
362 }
363
Mark Brown01866452013-05-08 13:55:24 +0100364 rbnode->block = kmalloc(rbnode->blklen * map->cache_word_size,
365 GFP_KERNEL);
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200366 if (!rbnode->block)
367 goto err_free;
368
lixiuboeeda1bd2015-11-20 18:06:29 +0800369 rbnode->cache_present = kcalloc(BITS_TO_LONGS(rbnode->blklen),
370 sizeof(*rbnode->cache_present),
371 GFP_KERNEL);
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200372 if (!rbnode->cache_present)
373 goto err_free_block;
Mark Brown01866452013-05-08 13:55:24 +0100374
375 return rbnode;
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200376
377err_free_block:
378 kfree(rbnode->block);
379err_free:
380 kfree(rbnode);
381 return NULL;
Mark Brown01866452013-05-08 13:55:24 +0100382}
383
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100384static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
385 unsigned int value)
386{
387 struct regcache_rbtree_ctx *rbtree_ctx;
388 struct regcache_rbtree_node *rbnode, *rbnode_tmp;
389 struct rb_node *node;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100390 unsigned int reg_tmp;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100391 int ret;
392
393 rbtree_ctx = map->cache;
Dimitris Papastamos0c7ed8562013-03-15 14:54:35 +0000394
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100395 /* if we can't locate it in the cached rbnode we'll have
396 * to traverse the rbtree looking for it.
397 */
Lars-Peter Clausen3405add2011-09-27 20:15:38 +0200398 rbnode = regcache_rbtree_lookup(map, reg);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100399 if (rbnode) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600400 reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
Mark Brown879082c2013-02-21 18:03:13 +0000401 regcache_rbtree_set_register(map, rbnode, reg_tmp, value);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100402 } else {
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200403 unsigned int base_reg, top_reg;
404 unsigned int new_base_reg, new_top_reg;
405 unsigned int min, max;
406 unsigned int max_dist;
407
408 max_dist = map->reg_stride * sizeof(*rbnode_tmp) /
409 map->cache_word_size;
410 if (reg < max_dist)
411 min = 0;
412 else
413 min = reg - max_dist;
414 max = reg + max_dist;
415
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100416 /* look for an adjacent register to the one we are about to add */
417 for (node = rb_first(&rbtree_ctx->root); node;
418 node = rb_next(node)) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600419 rbnode_tmp = rb_entry(node, struct regcache_rbtree_node,
420 node);
Lars-Peter Clausen194c7532013-08-29 10:26:32 +0200421
422 regcache_rbtree_get_base_top_reg(map, rbnode_tmp,
423 &base_reg, &top_reg);
424
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200425 if (base_reg <= max && top_reg >= min) {
426 new_base_reg = min(reg, base_reg);
427 new_top_reg = max(reg, top_reg);
428 } else {
Lars-Peter Clausen194c7532013-08-29 10:26:32 +0200429 continue;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100430 }
Lars-Peter Clausen194c7532013-08-29 10:26:32 +0200431
432 ret = regcache_rbtree_insert_to_block(map, rbnode_tmp,
Lars-Peter Clausen472fdec2013-08-29 10:26:33 +0200433 new_base_reg,
434 new_top_reg, reg,
435 value);
Lars-Peter Clausen194c7532013-08-29 10:26:32 +0200436 if (ret)
437 return ret;
438 rbtree_ctx->cached_rbnode = rbnode_tmp;
439 return 0;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100440 }
Mark Brown01866452013-05-08 13:55:24 +0100441
442 /* We did not manage to find a place to insert it in
443 * an existing block so create a new rbnode.
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100444 */
Mark Brown01866452013-05-08 13:55:24 +0100445 rbnode = regcache_rbtree_node_alloc(map, reg);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100446 if (!rbnode)
447 return -ENOMEM;
Mark Brown01866452013-05-08 13:55:24 +0100448 regcache_rbtree_set_register(map, rbnode,
449 reg - rbnode->base_reg, value);
Stephen Warrenf01ee602012-04-09 13:40:24 -0600450 regcache_rbtree_insert(map, &rbtree_ctx->root, rbnode);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100451 rbtree_ctx->cached_rbnode = rbnode;
452 }
453
454 return 0;
455}
456
Mark Brownac8d91c2012-02-23 19:31:04 +0000457static int regcache_rbtree_sync(struct regmap *map, unsigned int min,
458 unsigned int max)
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100459{
460 struct regcache_rbtree_ctx *rbtree_ctx;
461 struct rb_node *node;
462 struct regcache_rbtree_node *rbnode;
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200463 unsigned int base_reg, top_reg;
464 unsigned int start, end;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100465 int ret;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100466
467 rbtree_ctx = map->cache;
468 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
469 rbnode = rb_entry(node, struct regcache_rbtree_node, node);
Mark Brownac8d91c2012-02-23 19:31:04 +0000470
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200471 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
472 &top_reg);
473 if (base_reg > max)
Mark Brownac8d91c2012-02-23 19:31:04 +0000474 break;
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200475 if (top_reg < min)
Mark Brownac8d91c2012-02-23 19:31:04 +0000476 continue;
477
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200478 if (min > base_reg)
479 start = (min - base_reg) / map->reg_stride;
Mark Brownac8d91c2012-02-23 19:31:04 +0000480 else
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200481 start = 0;
Mark Brownac8d91c2012-02-23 19:31:04 +0000482
Lars-Peter Clausenb6752122013-08-27 13:03:03 +0200483 if (max < top_reg)
484 end = (max - base_reg) / map->reg_stride + 1;
Mark Brownac8d91c2012-02-23 19:31:04 +0000485 else
486 end = rbnode->blklen;
487
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200488 ret = regcache_sync_block(map, rbnode->block,
489 rbnode->cache_present,
490 rbnode->base_reg, start, end);
Mark Brownf8bd8222013-03-29 19:32:28 +0000491 if (ret != 0)
492 return ret;
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100493 }
494
Mark Brownf8bd8222013-03-29 19:32:28 +0000495 return regmap_async_complete(map);
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100496}
497
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200498static int regcache_rbtree_drop(struct regmap *map, unsigned int min,
499 unsigned int max)
500{
501 struct regcache_rbtree_ctx *rbtree_ctx;
502 struct regcache_rbtree_node *rbnode;
503 struct rb_node *node;
504 unsigned int base_reg, top_reg;
505 unsigned int start, end;
506
507 rbtree_ctx = map->cache;
508 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
509 rbnode = rb_entry(node, struct regcache_rbtree_node, node);
510
511 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
512 &top_reg);
513 if (base_reg > max)
514 break;
515 if (top_reg < min)
516 continue;
517
518 if (min > base_reg)
519 start = (min - base_reg) / map->reg_stride;
520 else
521 start = 0;
522
523 if (max < top_reg)
524 end = (max - base_reg) / map->reg_stride + 1;
525 else
526 end = rbnode->blklen;
527
528 bitmap_clear(rbnode->cache_present, start, end - start);
529 }
530
531 return 0;
532}
533
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100534struct regcache_ops regcache_rbtree_ops = {
535 .type = REGCACHE_RBTREE,
536 .name = "rbtree",
537 .init = regcache_rbtree_init,
538 .exit = regcache_rbtree_exit,
Lars-Peter Clausen5e0cbe72014-08-24 15:32:27 +0200539#ifdef CONFIG_DEBUG_FS
540 .debugfs_init = rbtree_debugfs_init,
541#endif
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100542 .read = regcache_rbtree_read,
543 .write = regcache_rbtree_write,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200544 .sync = regcache_rbtree_sync,
545 .drop = regcache_rbtree_drop,
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100546};