1 // SPDX-License-Identifier: GPL-2.0
3 // Register map access API - debugfs
5 // Copyright 2011 Wolfson Microelectronics plc
9 #include <linux/slab.h>
10 #include <linux/mutex.h>
11 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
18 struct regmap_debugfs_node {
20 struct list_head link;
23 static unsigned int dummy_index;
24 static struct dentry *regmap_debugfs_root;
25 static LIST_HEAD(regmap_debugfs_early_list);
26 static DEFINE_MUTEX(regmap_debugfs_early_lock);
28 /* Calculate the length of a fixed format */
29 static size_t regmap_calc_reg_len(int max_val)
31 return snprintf(NULL, 0, "%x", max_val);
34 static ssize_t regmap_name_read_file(struct file *file,
35 char __user *user_buf, size_t count,
38 struct regmap *map = file->private_data;
39 const char *name = "nodev";
43 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
47 if (map->dev && map->dev->driver)
48 name = map->dev->driver->name;
50 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
56 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
61 static const struct file_operations regmap_name_fops = {
63 .read = regmap_name_read_file,
64 .llseek = default_llseek,
67 static void regmap_debugfs_free_dump_cache(struct regmap *map)
69 struct regmap_debugfs_off_cache *c;
71 while (!list_empty(&map->debugfs_off_cache)) {
72 c = list_first_entry(&map->debugfs_off_cache,
73 struct regmap_debugfs_off_cache,
80 static bool regmap_printable(struct regmap *map, unsigned int reg)
82 if (regmap_precious(map, reg))
85 if (!regmap_readable(map, reg) && !regmap_cached(map, reg))
92 * Work out where the start offset maps into register numbers, bearing
93 * in mind that we suppress hidden registers.
95 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
100 struct regmap_debugfs_off_cache *c = NULL;
103 unsigned int fpos_offset;
104 unsigned int reg_offset;
106 /* Suppress the cache if we're using a subrange */
111 * If we don't have a cache build one so we don't have to do a
112 * linear scan each time.
114 mutex_lock(&map->cache_lock);
116 if (list_empty(&map->debugfs_off_cache)) {
117 for (; i <= map->max_register; i += map->reg_stride) {
118 /* Skip unprinted registers, closing off cache entry */
119 if (!regmap_printable(map, i)) {
122 c->max_reg = i - map->reg_stride;
123 list_add_tail(&c->list,
124 &map->debugfs_off_cache);
131 /* No cache entry? Start a new one */
133 c = kzalloc(sizeof(*c), GFP_KERNEL);
135 regmap_debugfs_free_dump_cache(map);
136 mutex_unlock(&map->cache_lock);
143 p += map->debugfs_tot_len;
147 /* Close the last entry off if we didn't scan beyond it */
150 c->max_reg = i - map->reg_stride;
151 list_add_tail(&c->list,
152 &map->debugfs_off_cache);
156 * This should never happen; we return above if we fail to
157 * allocate and we should never be in this code if there are
158 * no registers at all.
160 WARN_ON(list_empty(&map->debugfs_off_cache));
163 /* Find the relevant block:offset */
164 list_for_each_entry(c, &map->debugfs_off_cache, list) {
165 if (from >= c->min && from <= c->max) {
166 fpos_offset = from - c->min;
167 reg_offset = fpos_offset / map->debugfs_tot_len;
168 *pos = c->min + (reg_offset * map->debugfs_tot_len);
169 mutex_unlock(&map->cache_lock);
170 return c->base_reg + (reg_offset * map->reg_stride);
176 mutex_unlock(&map->cache_lock);
181 static inline void regmap_calc_tot_len(struct regmap *map,
182 void *buf, size_t count)
184 /* Calculate the length of a fixed format */
185 if (!map->debugfs_tot_len) {
186 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register);
187 map->debugfs_val_len = 2 * map->format.val_bytes;
188 map->debugfs_tot_len = map->debugfs_reg_len +
189 map->debugfs_val_len + 3; /* : \n */
193 static int regmap_next_readable_reg(struct regmap *map, int reg)
195 struct regmap_debugfs_off_cache *c;
198 if (regmap_printable(map, reg + map->reg_stride)) {
199 ret = reg + map->reg_stride;
201 mutex_lock(&map->cache_lock);
202 list_for_each_entry(c, &map->debugfs_off_cache, list) {
203 if (reg > c->max_reg)
205 if (reg < c->base_reg) {
210 mutex_unlock(&map->cache_lock);
215 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
216 unsigned int to, char __user *user_buf,
217 size_t count, loff_t *ppos)
224 unsigned int val, start_reg;
226 if (*ppos < 0 || !count)
229 if (count > (PAGE_SIZE << MAX_ORDER))
230 count = PAGE_SIZE << MAX_ORDER;
232 buf = kmalloc(count, GFP_KERNEL);
236 regmap_calc_tot_len(map, buf, count);
238 /* Work out which register we're starting at */
239 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
241 for (i = start_reg; i >= 0 && i <= to;
242 i = regmap_next_readable_reg(map, i)) {
244 /* If we're in the region the user is trying to read */
246 /* ...but not beyond it */
247 if (buf_pos + map->debugfs_tot_len > count)
250 /* Format the register */
251 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
252 map->debugfs_reg_len, i - from);
253 buf_pos += map->debugfs_reg_len + 2;
255 /* Format the value, write all X if we can't read */
256 ret = regmap_read(map, i, &val);
258 snprintf(buf + buf_pos, count - buf_pos,
259 "%.*x", map->debugfs_val_len, val);
261 memset(buf + buf_pos, 'X',
262 map->debugfs_val_len);
263 buf_pos += 2 * map->format.val_bytes;
265 buf[buf_pos++] = '\n';
267 p += map->debugfs_tot_len;
272 if (copy_to_user(user_buf, buf, buf_pos)) {
284 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
285 size_t count, loff_t *ppos)
287 struct regmap *map = file->private_data;
289 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
293 #undef REGMAP_ALLOW_WRITE_DEBUGFS
294 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
296 * This can be dangerous especially when we have clients such as
297 * PMICs, therefore don't provide any real compile time configuration option
298 * for this feature, people who want to use this will need to modify
299 * the source code directly.
301 static ssize_t regmap_map_write_file(struct file *file,
302 const char __user *user_buf,
303 size_t count, loff_t *ppos)
308 unsigned long reg, value;
309 struct regmap *map = file->private_data;
312 buf_size = min(count, (sizeof(buf)-1));
313 if (copy_from_user(buf, user_buf, buf_size))
317 while (*start == ' ')
319 reg = simple_strtoul(start, &start, 16);
320 while (*start == ' ')
322 if (kstrtoul(start, 16, &value))
325 /* Userspace has been fiddling around behind the kernel's back */
326 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
328 ret = regmap_write(map, reg, value);
334 #define regmap_map_write_file NULL
337 static const struct file_operations regmap_map_fops = {
339 .read = regmap_map_read_file,
340 .write = regmap_map_write_file,
341 .llseek = default_llseek,
344 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
347 struct regmap_range_node *range = file->private_data;
348 struct regmap *map = range->map;
350 return regmap_read_debugfs(map, range->range_min, range->range_max,
351 user_buf, count, ppos);
354 static const struct file_operations regmap_range_fops = {
356 .read = regmap_range_read_file,
357 .llseek = default_llseek,
360 static ssize_t regmap_reg_ranges_read_file(struct file *file,
361 char __user *user_buf, size_t count,
364 struct regmap *map = file->private_data;
365 struct regmap_debugfs_off_cache *c;
371 unsigned int entry_len;
373 if (*ppos < 0 || !count)
376 if (count > (PAGE_SIZE << MAX_ORDER))
377 count = PAGE_SIZE << MAX_ORDER;
379 buf = kmalloc(count, GFP_KERNEL);
383 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
389 /* While we are at it, build the register dump cache
390 * now so the read() operation on the `registers' file
391 * can benefit from using the cache. We do not care
392 * about the file position information that is contained
393 * in the cache, just about the actual register blocks */
394 regmap_calc_tot_len(map, buf, count);
395 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
397 /* Reset file pointer as the fixed-format of the `registers'
398 * file is not compatible with the `range' file */
400 mutex_lock(&map->cache_lock);
401 list_for_each_entry(c, &map->debugfs_off_cache, list) {
402 entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
403 c->base_reg, c->max_reg);
405 if (buf_pos + entry_len > count)
407 memcpy(buf + buf_pos, entry, entry_len);
408 buf_pos += entry_len;
412 mutex_unlock(&map->cache_lock);
417 if (copy_to_user(user_buf, buf, buf_pos)) {
428 static const struct file_operations regmap_reg_ranges_fops = {
430 .read = regmap_reg_ranges_read_file,
431 .llseek = default_llseek,
434 static int regmap_access_show(struct seq_file *s, void *ignored)
436 struct regmap *map = s->private;
439 reg_len = regmap_calc_reg_len(map->max_register);
441 for (i = 0; i <= map->max_register; i += map->reg_stride) {
442 /* Ignore registers which are neither readable nor writable */
443 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
446 /* Format the register */
447 seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i,
448 regmap_readable(map, i) ? 'y' : 'n',
449 regmap_writeable(map, i) ? 'y' : 'n',
450 regmap_volatile(map, i) ? 'y' : 'n',
451 regmap_precious(map, i) ? 'y' : 'n');
457 DEFINE_SHOW_ATTRIBUTE(regmap_access);
459 static ssize_t regmap_cache_only_write_file(struct file *file,
460 const char __user *user_buf,
461 size_t count, loff_t *ppos)
463 struct regmap *map = container_of(file->private_data,
464 struct regmap, cache_only);
465 bool new_val, require_sync = false;
468 err = kstrtobool_from_user(user_buf, count, &new_val);
469 /* Ignore malforned data like debugfs_write_file_bool() */
473 err = debugfs_file_get(file->f_path.dentry);
477 map->lock(map->lock_arg);
479 if (new_val && !map->cache_only) {
480 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
481 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
482 } else if (!new_val && map->cache_only) {
483 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
486 map->cache_only = new_val;
488 map->unlock(map->lock_arg);
489 debugfs_file_put(file->f_path.dentry);
492 err = regcache_sync(map);
494 dev_err(map->dev, "Failed to sync cache %d\n", err);
500 static const struct file_operations regmap_cache_only_fops = {
502 .read = debugfs_read_file_bool,
503 .write = regmap_cache_only_write_file,
506 static ssize_t regmap_cache_bypass_write_file(struct file *file,
507 const char __user *user_buf,
508 size_t count, loff_t *ppos)
510 struct regmap *map = container_of(file->private_data,
511 struct regmap, cache_bypass);
515 err = kstrtobool_from_user(user_buf, count, &new_val);
516 /* Ignore malforned data like debugfs_write_file_bool() */
520 err = debugfs_file_get(file->f_path.dentry);
524 map->lock(map->lock_arg);
526 if (new_val && !map->cache_bypass) {
527 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
528 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
529 } else if (!new_val && map->cache_bypass) {
530 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
532 map->cache_bypass = new_val;
534 map->unlock(map->lock_arg);
535 debugfs_file_put(file->f_path.dentry);
540 static const struct file_operations regmap_cache_bypass_fops = {
542 .read = debugfs_read_file_bool,
543 .write = regmap_cache_bypass_write_file,
546 void regmap_debugfs_init(struct regmap *map)
548 struct rb_node *next;
549 struct regmap_range_node *range_node;
550 const char *devname = "dummy";
551 const char *name = map->name;
554 * Userspace can initiate reads from the hardware over debugfs.
555 * Normally internal regmap structures and buffers are protected with
556 * a mutex or a spinlock, but if the regmap owner decided to disable
557 * all locking mechanisms, this is no longer the case. For safety:
558 * don't create the debugfs entries if locking is disabled.
560 if (map->debugfs_disable) {
561 dev_dbg(map->dev, "regmap locking disabled - not creating debugfs entries\n");
565 /* If we don't have the debugfs root yet, postpone init */
566 if (!regmap_debugfs_root) {
567 struct regmap_debugfs_node *node;
568 node = kzalloc(sizeof(*node), GFP_KERNEL);
572 mutex_lock(®map_debugfs_early_lock);
573 list_add(&node->link, ®map_debugfs_early_list);
574 mutex_unlock(®map_debugfs_early_lock);
578 INIT_LIST_HEAD(&map->debugfs_off_cache);
579 mutex_init(&map->cache_lock);
582 devname = dev_name(map->dev);
585 if (!map->debugfs_name) {
586 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
588 if (!map->debugfs_name)
591 name = map->debugfs_name;
596 if (!strcmp(name, "dummy")) {
597 kfree(map->debugfs_name);
598 map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d",
600 if (!map->debugfs_name)
602 name = map->debugfs_name;
606 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
608 debugfs_create_file("name", 0400, map->debugfs,
609 map, ®map_name_fops);
611 debugfs_create_file("range", 0400, map->debugfs,
612 map, ®map_reg_ranges_fops);
614 if (map->max_register || regmap_readable(map, 0)) {
615 umode_t registers_mode;
617 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
618 registers_mode = 0600;
620 registers_mode = 0400;
623 debugfs_create_file("registers", registers_mode, map->debugfs,
624 map, ®map_map_fops);
625 debugfs_create_file("access", 0400, map->debugfs,
626 map, ®map_access_fops);
629 if (map->cache_type) {
630 debugfs_create_file("cache_only", 0600, map->debugfs,
631 &map->cache_only, ®map_cache_only_fops);
632 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
634 debugfs_create_file("cache_bypass", 0600, map->debugfs,
636 ®map_cache_bypass_fops);
639 next = rb_first(&map->range_tree);
641 range_node = rb_entry(next, struct regmap_range_node, node);
643 if (range_node->name)
644 debugfs_create_file(range_node->name, 0400,
645 map->debugfs, range_node,
648 next = rb_next(&range_node->node);
651 if (map->cache_ops && map->cache_ops->debugfs_init)
652 map->cache_ops->debugfs_init(map);
655 void regmap_debugfs_exit(struct regmap *map)
658 debugfs_remove_recursive(map->debugfs);
659 mutex_lock(&map->cache_lock);
660 regmap_debugfs_free_dump_cache(map);
661 mutex_unlock(&map->cache_lock);
662 kfree(map->debugfs_name);
663 map->debugfs_name = NULL;
665 struct regmap_debugfs_node *node, *tmp;
667 mutex_lock(®map_debugfs_early_lock);
668 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list,
670 if (node->map == map) {
671 list_del(&node->link);
675 mutex_unlock(®map_debugfs_early_lock);
679 void regmap_debugfs_initcall(void)
681 struct regmap_debugfs_node *node, *tmp;
683 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
685 mutex_lock(®map_debugfs_early_lock);
686 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) {
687 regmap_debugfs_init(node->map);
688 list_del(&node->link);
691 mutex_unlock(®map_debugfs_early_lock);