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