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