regcache-rbtree.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Register cache access API - rbtree caching support
 *
 * Copyright 2011 Wolfson Microelectronics plc
 *
 * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/slab.h>
14
#include <linux/device.h>
15
#include <linux/debugfs.h>
16
#include <linux/rbtree.h>
17
#include <linux/seq_file.h>
18 19 20 21 22

#include "internal.h"

static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
				 unsigned int value);
23
static int regcache_rbtree_exit(struct regmap *map);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

struct regcache_rbtree_node {
	/* the actual rbtree node holding this block */
	struct rb_node node;
	/* base register handled by this block */
	unsigned int base_reg;
	/* block of adjacent registers */
	void *block;
	/* number of registers available in the block */
	unsigned int blklen;
} __attribute__ ((packed));

struct regcache_rbtree_ctx {
	struct rb_root root;
	struct regcache_rbtree_node *cached_rbnode;
};

static inline void regcache_rbtree_get_base_top_reg(
42
	struct regmap *map,
43 44 45 46
	struct regcache_rbtree_node *rbnode,
	unsigned int *base, unsigned int *top)
{
	*base = rbnode->base_reg;
47
	*top = rbnode->base_reg + ((rbnode->blklen - 1) * map->reg_stride);
48 49
}

50 51
static unsigned int regcache_rbtree_get_register(struct regmap *map,
	struct regcache_rbtree_node *rbnode, unsigned int idx)
52
{
53
	return regcache_get_val(map, rbnode->block, idx);
54 55
}

56 57 58
static void regcache_rbtree_set_register(struct regmap *map,
					 struct regcache_rbtree_node *rbnode,
					 unsigned int idx, unsigned int val)
59
{
60
	regcache_set_val(map, rbnode->block, idx, val);
61 62
}

63
static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map,
64
							   unsigned int reg)
65
{
66
	struct regcache_rbtree_ctx *rbtree_ctx = map->cache;
67 68 69 70
	struct rb_node *node;
	struct regcache_rbtree_node *rbnode;
	unsigned int base_reg, top_reg;

71 72
	rbnode = rbtree_ctx->cached_rbnode;
	if (rbnode) {
73 74
		regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
						 &top_reg);
75 76 77 78 79
		if (reg >= base_reg && reg <= top_reg)
			return rbnode;
	}

	node = rbtree_ctx->root.rb_node;
80 81
	while (node) {
		rbnode = container_of(node, struct regcache_rbtree_node, node);
82 83
		regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
						 &top_reg);
84 85
		if (reg >= base_reg && reg <= top_reg) {
			rbtree_ctx->cached_rbnode = rbnode;
86
			return rbnode;
87
		} else if (reg > top_reg) {
88
			node = node->rb_right;
89
		} else if (reg < base_reg) {
90
			node = node->rb_left;
91
		}
92 93 94 95 96
	}

	return NULL;
}

97
static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root,
98 99 100 101 102 103 104 105 106 107 108 109 110
				  struct regcache_rbtree_node *rbnode)
{
	struct rb_node **new, *parent;
	struct regcache_rbtree_node *rbnode_tmp;
	unsigned int base_reg_tmp, top_reg_tmp;
	unsigned int base_reg;

	parent = NULL;
	new = &root->rb_node;
	while (*new) {
		rbnode_tmp = container_of(*new, struct regcache_rbtree_node,
					  node);
		/* base and top registers of the current rbnode */
111
		regcache_rbtree_get_base_top_reg(map, rbnode_tmp, &base_reg_tmp,
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
						 &top_reg_tmp);
		/* base register of the rbnode to be added */
		base_reg = rbnode->base_reg;
		parent = *new;
		/* if this register has already been inserted, just return */
		if (base_reg >= base_reg_tmp &&
		    base_reg <= top_reg_tmp)
			return 0;
		else if (base_reg > top_reg_tmp)
			new = &((*new)->rb_right);
		else if (base_reg < base_reg_tmp)
			new = &((*new)->rb_left);
	}

	/* insert the node into the rbtree */
	rb_link_node(&rbnode->node, parent, new);
	rb_insert_color(&rbnode->node, root);

	return 1;
}

133 134 135 136 137 138 139 140
#ifdef CONFIG_DEBUG_FS
static int rbtree_show(struct seq_file *s, void *ignored)
{
	struct regmap *map = s->private;
	struct regcache_rbtree_ctx *rbtree_ctx = map->cache;
	struct regcache_rbtree_node *n;
	struct rb_node *node;
	unsigned int base, top;
141
	size_t mem_size;
142 143
	int nodes = 0;
	int registers = 0;
144
	int this_registers, average;
145

146
	map->lock(map->lock_arg);
147

148
	mem_size = sizeof(*rbtree_ctx);
149
	mem_size += BITS_TO_LONGS(map->cache_present_nbits) * sizeof(long);
150

151 152 153
	for (node = rb_first(&rbtree_ctx->root); node != NULL;
	     node = rb_next(node)) {
		n = container_of(node, struct regcache_rbtree_node, node);
154 155
		mem_size += sizeof(*n);
		mem_size += (n->blklen * map->cache_word_size);
156

157 158 159
		regcache_rbtree_get_base_top_reg(map, n, &base, &top);
		this_registers = ((top - base) / map->reg_stride) + 1;
		seq_printf(s, "%x-%x (%d)\n", base, top, this_registers);
160 161

		nodes++;
162
		registers += this_registers;
163 164
	}

165 166 167 168 169
	if (nodes)
		average = registers / nodes;
	else
		average = 0;

170 171
	seq_printf(s, "%d nodes, %d registers, average %d registers, used %zu bytes\n",
		   nodes, registers, average, mem_size);
172

173
	map->unlock(map->lock_arg);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

	return 0;
}

static int rbtree_open(struct inode *inode, struct file *file)
{
	return single_open(file, rbtree_show, inode->i_private);
}

static const struct file_operations rbtree_fops = {
	.open		= rbtree_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
189 190 191 192 193 194 195 196 197

static void rbtree_debugfs_init(struct regmap *map)
{
	debugfs_create_file("rbtree", 0400, map->debugfs, map, &rbtree_fops);
}
#else
static void rbtree_debugfs_init(struct regmap *map)
{
}
198 199
#endif

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static int regcache_rbtree_init(struct regmap *map)
{
	struct regcache_rbtree_ctx *rbtree_ctx;
	int i;
	int ret;

	map->cache = kmalloc(sizeof *rbtree_ctx, GFP_KERNEL);
	if (!map->cache)
		return -ENOMEM;

	rbtree_ctx = map->cache;
	rbtree_ctx->root = RB_ROOT;
	rbtree_ctx->cached_rbnode = NULL;

	for (i = 0; i < map->num_reg_defaults; i++) {
		ret = regcache_rbtree_write(map,
					    map->reg_defaults[i].reg,
					    map->reg_defaults[i].def);
		if (ret)
			goto err;
	}

222
	rbtree_debugfs_init(map);
223

224 225 226
	return 0;

err:
227
	regcache_rbtree_exit(map);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	return ret;
}

static int regcache_rbtree_exit(struct regmap *map)
{
	struct rb_node *next;
	struct regcache_rbtree_ctx *rbtree_ctx;
	struct regcache_rbtree_node *rbtree_node;

	/* if we've already been called then just return */
	rbtree_ctx = map->cache;
	if (!rbtree_ctx)
		return 0;

	/* free up the rbtree */
	next = rb_first(&rbtree_ctx->root);
	while (next) {
		rbtree_node = rb_entry(next, struct regcache_rbtree_node, node);
		next = rb_next(&rbtree_node->node);
		rb_erase(&rbtree_node->node, &rbtree_ctx->root);
		kfree(rbtree_node->block);
		kfree(rbtree_node);
	}

	/* release the resources */
	kfree(map->cache);
	map->cache = NULL;

	return 0;
}

static int regcache_rbtree_read(struct regmap *map,
				unsigned int reg, unsigned int *value)
{
	struct regcache_rbtree_node *rbnode;
	unsigned int reg_tmp;

265
	rbnode = regcache_rbtree_lookup(map, reg);
266
	if (rbnode) {
267
		reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
268 269
		if (!regcache_reg_present(map, reg))
			return -ENOENT;
270
		*value = regcache_rbtree_get_register(map, rbnode, reg_tmp);
271
	} else {
272
		return -ENOENT;
273 274 275 276 277 278
	}

	return 0;
}


279 280
static int regcache_rbtree_insert_to_block(struct regmap *map,
					   struct regcache_rbtree_node *rbnode,
281
					   unsigned int pos, unsigned int reg,
282
					   unsigned int value)
283 284 285 286
{
	u8 *blk;

	blk = krealloc(rbnode->block,
287 288
		       (rbnode->blklen + 1) * map->cache_word_size,
		       GFP_KERNEL);
289 290 291 292
	if (!blk)
		return -ENOMEM;

	/* insert the register value in the correct place in the rbnode block */
293 294 295
	memmove(blk + (pos + 1) * map->cache_word_size,
		blk + pos * map->cache_word_size,
		(rbnode->blklen - pos) * map->cache_word_size);
296 297 298 299 300 301 302

	/* update the rbnode block, its size and the base register */
	rbnode->block = blk;
	rbnode->blklen++;
	if (!pos)
		rbnode->base_reg = reg;

303
	regcache_rbtree_set_register(map, rbnode, pos, value);
304 305 306
	return 0;
}

307 308 309 310
static struct regcache_rbtree_node *
regcache_rbtree_node_alloc(struct regmap *map, unsigned int reg)
{
	struct regcache_rbtree_node *rbnode;
311 312
	const struct regmap_range *range;
	int i;
313 314 315 316 317

	rbnode = kzalloc(sizeof(*rbnode), GFP_KERNEL);
	if (!rbnode)
		return NULL;

318 319 320 321 322 323 324 325 326 327
	/* If there is a read table then use it to guess at an allocation */
	if (map->rd_table) {
		for (i = 0; i < map->rd_table->n_yes_ranges; i++) {
			if (regmap_reg_in_range(reg,
						&map->rd_table->yes_ranges[i]))
				break;
		}

		if (i != map->rd_table->n_yes_ranges) {
			range = &map->rd_table->yes_ranges[i];
328 329
			rbnode->blklen = (range->range_max - range->range_min) /
				map->reg_stride	+ 1;
330 331 332 333 334 335 336 337 338
			rbnode->base_reg = range->range_min;
		}
	}

	if (!rbnode->blklen) {
		rbnode->blklen = sizeof(*rbnode);
		rbnode->base_reg = reg;
	}

339 340 341 342 343 344 345 346 347 348
	rbnode->block = kmalloc(rbnode->blklen * map->cache_word_size,
				GFP_KERNEL);
	if (!rbnode->block) {
		kfree(rbnode);
		return NULL;
	}

	return rbnode;
}

349 350 351 352 353 354
static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
				 unsigned int value)
{
	struct regcache_rbtree_ctx *rbtree_ctx;
	struct regcache_rbtree_node *rbnode, *rbnode_tmp;
	struct rb_node *node;
355
	unsigned int base_reg, top_reg;
356 357 358 359 360
	unsigned int reg_tmp;
	unsigned int pos;
	int ret;

	rbtree_ctx = map->cache;
361
	/* update the reg_present bitmap, make space if necessary */
362
	ret = regcache_set_reg_present(map, reg);
363 364 365
	if (ret < 0)
		return ret;

366 367 368
	/* if we can't locate it in the cached rbnode we'll have
	 * to traverse the rbtree looking for it.
	 */
369
	rbnode = regcache_rbtree_lookup(map, reg);
370
	if (rbnode) {
371
		reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
372
		regcache_rbtree_set_register(map, rbnode, reg_tmp, value);
373 374 375 376
	} else {
		/* 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)) {
377 378
			rbnode_tmp = rb_entry(node, struct regcache_rbtree_node,
					      node);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

			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;
397
		}
398 399 400

		/* We did not manage to find a place to insert it in
		 * an existing block so create a new rbnode.
401
		 */
402
		rbnode = regcache_rbtree_node_alloc(map, reg);
403 404
		if (!rbnode)
			return -ENOMEM;
405 406
		regcache_rbtree_set_register(map, rbnode,
					     reg - rbnode->base_reg, value);
407
		regcache_rbtree_insert(map, &rbtree_ctx->root, rbnode);
408 409 410 411 412 413
		rbtree_ctx->cached_rbnode = rbnode;
	}

	return 0;
}

414 415
static int regcache_rbtree_sync(struct regmap *map, unsigned int min,
				unsigned int max)
416 417 418 419
{
	struct regcache_rbtree_ctx *rbtree_ctx;
	struct rb_node *node;
	struct regcache_rbtree_node *rbnode;
420 421
	unsigned int base_reg, top_reg;
	unsigned int start, end;
422 423 424 425 426
	int ret;

	rbtree_ctx = map->cache;
	for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
		rbnode = rb_entry(node, struct regcache_rbtree_node, node);
427

428 429 430
		regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg,
			&top_reg);
		if (base_reg > max)
431
			break;
432
		if (top_reg < min)
433 434
			continue;

435 436
		if (min > base_reg)
			start = (min - base_reg) / map->reg_stride;
437
		else
438
			start = 0;
439

440 441
		if (max < top_reg)
			end = (max - base_reg) / map->reg_stride + 1;
442 443 444
		else
			end = rbnode->blklen;

445
		ret = regcache_sync_block(map, rbnode->block, rbnode->base_reg,
446
					  start, end);
447 448
		if (ret != 0)
			return ret;
449 450
	}

451
	return regmap_async_complete(map);
452 453 454 455 456 457 458 459 460 461 462
}

struct regcache_ops regcache_rbtree_ops = {
	.type = REGCACHE_RBTREE,
	.name = "rbtree",
	.init = regcache_rbtree_init,
	.exit = regcache_rbtree_exit,
	.read = regcache_rbtree_read,
	.write = regcache_rbtree_write,
	.sync = regcache_rbtree_sync
};