xref: /linux/drivers/base/regmap/regmap-debugfs.c (revision 2dee91ee94eec85d09c13c7ca83e3005906d3732)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Register map access API - debugfs
4 //
5 // Copyright 2011 Wolfson Microelectronics plc
6 //
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 
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>
15 #include <linux/idr.h>
16 
17 #include "internal.h"
18 
19 struct regmap_debugfs_node {
20 	struct regmap *map;
21 	struct list_head link;
22 };
23 
24 static DEFINE_IDA(dummy_ida);
25 static struct dentry *regmap_debugfs_root;
26 static LIST_HEAD(regmap_debugfs_early_list);
27 static DEFINE_MUTEX(regmap_debugfs_early_lock);
28 
29 /* Calculate the length of a fixed format  */
30 static size_t regmap_calc_reg_len(int max_val)
31 {
32 	return snprintf(NULL, 0, "%x", max_val);
33 }
34 
35 static ssize_t regmap_name_read_file(struct file *file,
36 				     char __user *user_buf, size_t count,
37 				     loff_t *ppos)
38 {
39 	struct regmap *map = file->private_data;
40 	const char *name = "nodev";
41 	char *buf __free(kfree) = kmalloc(PAGE_SIZE, GFP_KERNEL);
42 	int ret;
43 
44 	if (!buf)
45 		return -ENOMEM;
46 
47 	if (map->dev && map->dev->driver)
48 		name = map->dev->driver->name;
49 
50 	ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
51 	if (ret >= PAGE_SIZE)
52 		return ret;
53 
54 	return simple_read_from_buffer(user_buf, count, ppos, buf, ret);
55 }
56 
57 static const struct file_operations regmap_name_fops = {
58 	.open = simple_open,
59 	.read = regmap_name_read_file,
60 	.llseek = default_llseek,
61 };
62 
63 static void regmap_debugfs_free_dump_cache(struct regmap *map)
64 {
65 	struct regmap_debugfs_off_cache *c;
66 
67 	while (!list_empty(&map->debugfs_off_cache)) {
68 		c = list_first_entry(&map->debugfs_off_cache,
69 				     struct regmap_debugfs_off_cache,
70 				     list);
71 		list_del(&c->list);
72 		kfree(c);
73 	}
74 }
75 
76 static bool regmap_printable(struct regmap *map, unsigned int reg)
77 {
78 	if (regmap_precious(map, reg))
79 		return false;
80 
81 	if (!regmap_readable(map, reg) && !regmap_cached(map, reg))
82 		return false;
83 
84 	return true;
85 }
86 
87 /*
88  * Work out where the start offset maps into register numbers, bearing
89  * in mind that we suppress hidden registers.
90  */
91 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
92 						  unsigned int base,
93 						  loff_t from,
94 						  loff_t *pos)
95 {
96 	struct regmap_debugfs_off_cache *c = NULL;
97 	loff_t p = 0;
98 	unsigned int i, ret;
99 	unsigned int fpos_offset;
100 	unsigned int reg_offset;
101 
102 	/* Suppress the cache if we're using a subrange */
103 	if (base)
104 		return base;
105 
106 	/*
107 	 * If we don't have a cache build one so we don't have to do a
108 	 * linear scan each time.
109 	 */
110 	guard(mutex)(&map->cache_lock);
111 	i = base;
112 	if (list_empty(&map->debugfs_off_cache)) {
113 		for (; i <= map->max_register; i += map->reg_stride) {
114 			/* Skip unprinted registers, closing off cache entry */
115 			if (!regmap_printable(map, i)) {
116 				if (c) {
117 					c->max = p - 1;
118 					c->max_reg = i - map->reg_stride;
119 					list_add_tail(&c->list,
120 						      &map->debugfs_off_cache);
121 					c = NULL;
122 				}
123 
124 				continue;
125 			}
126 
127 			/* No cache entry?  Start a new one */
128 			if (!c) {
129 				c = kzalloc_obj(*c);
130 				if (!c) {
131 					regmap_debugfs_free_dump_cache(map);
132 					return base;
133 				}
134 				c->min = p;
135 				c->base_reg = i;
136 			}
137 
138 			p += map->debugfs_tot_len;
139 		}
140 	}
141 
142 	/* Close the last entry off if we didn't scan beyond it */
143 	if (c) {
144 		c->max = p - 1;
145 		c->max_reg = i - map->reg_stride;
146 		list_add_tail(&c->list,
147 			      &map->debugfs_off_cache);
148 	}
149 
150 	/*
151 	 * This should never happen; we return above if we fail to
152 	 * allocate and we should never be in this code if there are
153 	 * no registers at all.
154 	 */
155 	WARN_ON(list_empty(&map->debugfs_off_cache));
156 	ret = base;
157 
158 	/* Find the relevant block:offset */
159 	list_for_each_entry(c, &map->debugfs_off_cache, list) {
160 		if (from >= c->min && from <= c->max) {
161 			fpos_offset = from - c->min;
162 			reg_offset = fpos_offset / map->debugfs_tot_len;
163 			*pos = c->min + (reg_offset * map->debugfs_tot_len);
164 			return c->base_reg + (reg_offset * map->reg_stride);
165 		}
166 
167 		*pos = c->max;
168 		ret = c->max_reg;
169 	}
170 
171 	return ret;
172 }
173 
174 static inline void regmap_calc_tot_len(struct regmap *map,
175 				       void *buf, size_t count)
176 {
177 	/* Calculate the length of a fixed format  */
178 	if (!map->debugfs_tot_len) {
179 		map->debugfs_reg_len = regmap_calc_reg_len(map->max_register);
180 		map->debugfs_val_len = 2 * map->format.val_bytes;
181 		map->debugfs_tot_len = map->debugfs_reg_len +
182 			map->debugfs_val_len + 3;      /* : \n */
183 	}
184 }
185 
186 static int regmap_next_readable_reg(struct regmap *map, int reg)
187 {
188 	struct regmap_debugfs_off_cache *c;
189 	int ret = -EINVAL;
190 
191 	if (regmap_printable(map, reg + map->reg_stride))
192 		return reg + map->reg_stride;
193 
194 	scoped_guard(mutex, &map->cache_lock) {
195 		list_for_each_entry(c, &map->debugfs_off_cache, list) {
196 			if (reg > c->max_reg)
197 				continue;
198 			if (reg < c->base_reg) {
199 				ret = c->base_reg;
200 				break;
201 			}
202 		}
203 	}
204 
205 	return ret;
206 }
207 
208 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
209 				   unsigned int to, char __user *user_buf,
210 				   size_t count, loff_t *ppos)
211 {
212 	size_t buf_pos = 0;
213 	loff_t p = *ppos;
214 	ssize_t ret;
215 	int i;
216 	char *buf;
217 	unsigned int val, start_reg;
218 
219 	if (*ppos < 0 || !count)
220 		return -EINVAL;
221 
222 	if (count > (PAGE_SIZE << MAX_PAGE_ORDER))
223 		count = PAGE_SIZE << MAX_PAGE_ORDER;
224 
225 	buf = kmalloc(count, GFP_KERNEL);
226 	if (!buf)
227 		return -ENOMEM;
228 
229 	regmap_calc_tot_len(map, buf, count);
230 
231 	/* Work out which register we're starting at */
232 	start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
233 
234 	for (i = start_reg; i >= 0 && i <= to;
235 	     i = regmap_next_readable_reg(map, i)) {
236 
237 		/* If we're in the region the user is trying to read */
238 		if (p >= *ppos) {
239 			/* ...but not beyond it */
240 			if (buf_pos + map->debugfs_tot_len > count)
241 				break;
242 
243 			/* Format the register */
244 			snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
245 				 map->debugfs_reg_len, i - from);
246 			buf_pos += map->debugfs_reg_len + 2;
247 
248 			/* Format the value, write all X if we can't read */
249 			ret = regmap_read(map, i, &val);
250 			if (ret == 0)
251 				snprintf(buf + buf_pos, count - buf_pos,
252 					 "%.*x", map->debugfs_val_len, val);
253 			else
254 				memset(buf + buf_pos, 'X',
255 				       map->debugfs_val_len);
256 			buf_pos += 2 * map->format.val_bytes;
257 
258 			buf[buf_pos++] = '\n';
259 		}
260 		p += map->debugfs_tot_len;
261 	}
262 
263 	ret = buf_pos;
264 
265 	if (copy_to_user(user_buf, buf, buf_pos)) {
266 		ret = -EFAULT;
267 		goto out;
268 	}
269 
270 	*ppos += buf_pos;
271 
272 out:
273 	kfree(buf);
274 	return ret;
275 }
276 
277 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
278 				    size_t count, loff_t *ppos)
279 {
280 	struct regmap *map = file->private_data;
281 
282 	return regmap_read_debugfs(map, 0, map->max_register, user_buf,
283 				   count, ppos);
284 }
285 
286 #undef REGMAP_ALLOW_WRITE_DEBUGFS
287 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
288 /*
289  * This can be dangerous especially when we have clients such as
290  * PMICs, therefore don't provide any real compile time configuration option
291  * for this feature, people who want to use this will need to modify
292  * the source code directly.
293  */
294 static ssize_t regmap_map_write_file(struct file *file,
295 				     const char __user *user_buf,
296 				     size_t count, loff_t *ppos)
297 {
298 	char buf[32];
299 	size_t buf_size;
300 	char *start = buf;
301 	unsigned long reg, value;
302 	struct regmap *map = file->private_data;
303 	int ret;
304 
305 	buf_size = min(count, (sizeof(buf)-1));
306 	if (copy_from_user(buf, user_buf, buf_size))
307 		return -EFAULT;
308 	buf[buf_size] = 0;
309 
310 	while (*start == ' ')
311 		start++;
312 	reg = simple_strtoul(start, &start, 16);
313 	while (*start == ' ')
314 		start++;
315 	if (kstrtoul(start, 16, &value))
316 		return -EINVAL;
317 
318 	/* Userspace has been fiddling around behind the kernel's back */
319 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
320 
321 	ret = regmap_write(map, reg, value);
322 	if (ret < 0)
323 		return ret;
324 	return buf_size;
325 }
326 #else
327 #define regmap_map_write_file NULL
328 #endif
329 
330 static const struct file_operations regmap_map_fops = {
331 	.open = simple_open,
332 	.read = regmap_map_read_file,
333 	.write = regmap_map_write_file,
334 	.llseek = default_llseek,
335 };
336 
337 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
338 				      size_t count, loff_t *ppos)
339 {
340 	struct regmap_range_node *range = file->private_data;
341 	struct regmap *map = range->map;
342 
343 	return regmap_read_debugfs(map, range->range_min, range->range_max,
344 				   user_buf, count, ppos);
345 }
346 
347 static const struct file_operations regmap_range_fops = {
348 	.open = simple_open,
349 	.read = regmap_range_read_file,
350 	.llseek = default_llseek,
351 };
352 
353 static ssize_t regmap_reg_ranges_read_file(struct file *file,
354 					   char __user *user_buf, size_t count,
355 					   loff_t *ppos)
356 {
357 	struct regmap *map = file->private_data;
358 	struct regmap_debugfs_off_cache *c;
359 	loff_t p = 0;
360 	size_t buf_pos = 0;
361 	int ret;
362 	unsigned int entry_len;
363 
364 	if (*ppos < 0 || !count)
365 		return -EINVAL;
366 
367 	if (count > (PAGE_SIZE << MAX_PAGE_ORDER))
368 		count = PAGE_SIZE << MAX_PAGE_ORDER;
369 
370 	char *buf __free(kfree) = kmalloc(count, GFP_KERNEL);
371 	if (!buf)
372 		return -ENOMEM;
373 
374 	char *entry __free(kfree) = kmalloc(PAGE_SIZE, GFP_KERNEL);
375 	if (!entry)
376 		return -ENOMEM;
377 
378 	/* While we are at it, build the register dump cache
379 	 * now so the read() operation on the `registers' file
380 	 * can benefit from using the cache.  We do not care
381 	 * about the file position information that is contained
382 	 * in the cache, just about the actual register blocks */
383 	regmap_calc_tot_len(map, buf, count);
384 	regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
385 
386 	/* Reset file pointer as the fixed-format of the `registers'
387 	 * file is not compatible with the `range' file */
388 	p = 0;
389 	scoped_guard(mutex, &map->cache_lock) {
390 		list_for_each_entry(c, &map->debugfs_off_cache, list) {
391 			entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
392 					     c->base_reg, c->max_reg);
393 			if (p >= *ppos) {
394 				if (buf_pos + entry_len > count)
395 					break;
396 				memcpy(buf + buf_pos, entry, entry_len);
397 				buf_pos += entry_len;
398 			}
399 			p += entry_len;
400 		}
401 	}
402 
403 	ret = buf_pos;
404 
405 	if (copy_to_user(user_buf, buf, buf_pos))
406 		return -EFAULT;
407 
408 	*ppos += buf_pos;
409 
410 	return ret;
411 }
412 
413 static const struct file_operations regmap_reg_ranges_fops = {
414 	.open = simple_open,
415 	.read = regmap_reg_ranges_read_file,
416 	.llseek = default_llseek,
417 };
418 
419 static int regmap_access_show(struct seq_file *s, void *ignored)
420 {
421 	struct regmap *map = s->private;
422 	int i, reg_len;
423 
424 	reg_len = regmap_calc_reg_len(map->max_register);
425 
426 	for (i = 0; i <= map->max_register; i += map->reg_stride) {
427 		/* Ignore registers which are neither readable nor writable */
428 		if (!regmap_readable(map, i) && !regmap_writeable(map, i))
429 			continue;
430 
431 		/* Format the register */
432 		seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i,
433 			   regmap_readable(map, i) ? 'y' : 'n',
434 			   regmap_writeable(map, i) ? 'y' : 'n',
435 			   regmap_volatile(map, i) ? 'y' : 'n',
436 			   regmap_precious(map, i) ? 'y' : 'n');
437 	}
438 
439 	return 0;
440 }
441 
442 DEFINE_SHOW_ATTRIBUTE(regmap_access);
443 
444 static ssize_t regmap_cache_only_write_file(struct file *file,
445 					    const char __user *user_buf,
446 					    size_t count, loff_t *ppos)
447 {
448 	struct regmap *map = container_of(file->private_data,
449 					  struct regmap, cache_only);
450 	bool new_val, require_sync = false;
451 	int err;
452 
453 	err = kstrtobool_from_user(user_buf, count, &new_val);
454 	/* Ignore malforned data like debugfs_write_file_bool() */
455 	if (err)
456 		return count;
457 
458 	scoped_guard(regmap, map) {
459 		if (new_val && !map->cache_only) {
460 			dev_warn(map->dev, "debugfs cache_only=Y forced\n");
461 			add_taint(TAINT_USER, LOCKDEP_STILL_OK);
462 		} else if (!new_val && map->cache_only) {
463 			dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
464 			require_sync = true;
465 		}
466 		map->cache_only = new_val;
467 	}
468 
469 	if (require_sync) {
470 		err = regcache_sync(map);
471 		if (err)
472 			dev_err(map->dev, "Failed to sync cache %d\n", err);
473 	}
474 
475 	return count;
476 }
477 
478 static const struct file_operations regmap_cache_only_fops = {
479 	.open = simple_open,
480 	.read = debugfs_read_file_bool,
481 	.write = regmap_cache_only_write_file,
482 };
483 
484 static ssize_t regmap_cache_bypass_write_file(struct file *file,
485 					      const char __user *user_buf,
486 					      size_t count, loff_t *ppos)
487 {
488 	struct regmap *map = container_of(file->private_data,
489 					  struct regmap, cache_bypass);
490 	bool new_val;
491 	int err;
492 
493 	err = kstrtobool_from_user(user_buf, count, &new_val);
494 	/* Ignore malforned data like debugfs_write_file_bool() */
495 	if (err)
496 		return count;
497 
498 	guard(regmap)(map);
499 
500 	if (new_val && !map->cache_bypass) {
501 		dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
502 		add_taint(TAINT_USER, LOCKDEP_STILL_OK);
503 	} else if (!new_val && map->cache_bypass) {
504 		dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
505 	}
506 	map->cache_bypass = new_val;
507 
508 	return count;
509 }
510 
511 static const struct file_operations regmap_cache_bypass_fops = {
512 	.open = simple_open,
513 	.read = debugfs_read_file_bool,
514 	.write = regmap_cache_bypass_write_file,
515 };
516 
517 void regmap_debugfs_init(struct regmap *map)
518 {
519 	struct rb_node *next;
520 	struct regmap_range_node *range_node;
521 	const char *devname = "dummy";
522 	const char *name = map->name;
523 	int id;
524 
525 	/*
526 	 * Userspace can initiate reads from the hardware over debugfs.
527 	 * Normally internal regmap structures and buffers are protected with
528 	 * a mutex or a spinlock, but if the regmap owner decided to disable
529 	 * all locking mechanisms, this is no longer the case. For safety:
530 	 * don't create the debugfs entries if locking is disabled.
531 	 */
532 	if (map->debugfs_disable) {
533 		dev_dbg(map->dev, "regmap locking disabled - not creating debugfs entries\n");
534 		return;
535 	}
536 
537 	/* If we don't have the debugfs root yet, postpone init */
538 	if (!regmap_debugfs_root) {
539 		struct regmap_debugfs_node *node;
540 		node = kzalloc_obj(*node);
541 		if (!node)
542 			return;
543 		node->map = map;
544 		scoped_guard(mutex, &regmap_debugfs_early_lock)
545 			list_add(&node->link, &regmap_debugfs_early_list);
546 		return;
547 	}
548 
549 	INIT_LIST_HEAD(&map->debugfs_off_cache);
550 	mutex_init(&map->cache_lock);
551 	map->debugfs_dummy_id = -1;
552 
553 	if (map->dev)
554 		devname = dev_name(map->dev);
555 
556 	if (name) {
557 		if (!map->debugfs_name) {
558 			map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
559 					      devname, name);
560 			if (!map->debugfs_name)
561 				return;
562 		}
563 		name = map->debugfs_name;
564 	} else {
565 		name = devname;
566 	}
567 
568 	if (!strcmp(name, "dummy")) {
569 		kfree(map->debugfs_name);
570 		id = ida_alloc(&dummy_ida, GFP_KERNEL);
571 		if (id < 0)
572 			return;
573 		map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d", id);
574 		if (!map->debugfs_name) {
575 			ida_free(&dummy_ida, id);
576 			return;
577 		}
578 		map->debugfs_dummy_id = id;
579 		name = map->debugfs_name;
580 	}
581 
582 	map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
583 
584 	debugfs_create_file("name", 0400, map->debugfs,
585 			    map, &regmap_name_fops);
586 
587 	debugfs_create_file("range", 0400, map->debugfs,
588 			    map, &regmap_reg_ranges_fops);
589 
590 	if (map->max_register || regmap_readable(map, 0)) {
591 		umode_t registers_mode;
592 
593 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
594 		registers_mode = 0600;
595 #else
596 		registers_mode = 0400;
597 #endif
598 
599 		debugfs_create_file("registers", registers_mode, map->debugfs,
600 				    map, &regmap_map_fops);
601 		debugfs_create_file("access", 0400, map->debugfs,
602 				    map, &regmap_access_fops);
603 	}
604 
605 	if (map->cache_type) {
606 		debugfs_create_file("cache_only", 0600, map->debugfs,
607 				    &map->cache_only, &regmap_cache_only_fops);
608 		debugfs_create_bool("cache_dirty", 0400, map->debugfs,
609 				    &map->cache_dirty);
610 		debugfs_create_file("cache_bypass", 0600, map->debugfs,
611 				    &map->cache_bypass,
612 				    &regmap_cache_bypass_fops);
613 	}
614 
615 	/*
616 	 * This could interfere with driver operation. Therefore, don't provide
617 	 * any real compile time configuration option for this feature. One will
618 	 * have to modify the source code directly in order to use it.
619 	 */
620 #undef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS
621 #ifdef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS
622 	debugfs_create_bool("force_write_field", 0600, map->debugfs,
623 			    &map->force_write_field);
624 #endif
625 
626 	next = rb_first(&map->range_tree);
627 	while (next) {
628 		range_node = rb_entry(next, struct regmap_range_node, node);
629 
630 		if (range_node->name)
631 			debugfs_create_file(range_node->name, 0400,
632 					    map->debugfs, range_node,
633 					    &regmap_range_fops);
634 
635 		next = rb_next(&range_node->node);
636 	}
637 
638 	if (map->cache_ops && map->cache_ops->debugfs_init)
639 		map->cache_ops->debugfs_init(map);
640 }
641 
642 void regmap_debugfs_exit(struct regmap *map)
643 {
644 	struct regmap_debugfs_node *node, *tmp;
645 
646 	if (map->debugfs) {
647 		debugfs_remove_recursive(map->debugfs);
648 		map->debugfs = NULL;
649 		scoped_guard(mutex, &map->cache_lock)
650 			regmap_debugfs_free_dump_cache(map);
651 		if (map->debugfs_dummy_id >= 0) {
652 			ida_free(&dummy_ida, map->debugfs_dummy_id);
653 			map->debugfs_dummy_id = -1;
654 		}
655 		kfree(map->debugfs_name);
656 		map->debugfs_name = NULL;
657 
658 		return;
659 	}
660 
661 	scoped_guard(mutex, &regmap_debugfs_early_lock) {
662 		list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
663 			if (node->map == map) {
664 				list_del(&node->link);
665 				kfree(node);
666 			}
667 		}
668 	}
669 }
670 
671 void regmap_debugfs_initcall(void)
672 {
673 	struct regmap_debugfs_node *node, *tmp;
674 
675 	regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
676 
677 	guard(mutex)(&regmap_debugfs_early_lock);
678 	list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
679 		regmap_debugfs_init(node->map);
680 		list_del(&node->link);
681 		kfree(node);
682 	}
683 }
684