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 22struct regmap_debugfs_node { 23 struct regmap *map; 24 const char *name; 25 struct list_head link; 26}; 27 28static struct dentry *regmap_debugfs_root; 29static LIST_HEAD(regmap_debugfs_early_list); 30static DEFINE_MUTEX(regmap_debugfs_early_lock); 31 32/* Calculate the length of a fixed format */ 33static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size) 34{ 35 return snprintf(NULL, 0, "%x", max_val); 36} 37 38static 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 61static const struct file_operations regmap_name_fops = { 62 .open = simple_open, 63 .read = regmap_name_read_file, 64 .llseek = default_llseek, 65}; 66 67static 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 */ 84static 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 171static 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 buf, count); 178 map->debugfs_val_len = 2 * map->format.val_bytes; 179 map->debugfs_tot_len = map->debugfs_reg_len + 180 map->debugfs_val_len + 3; /* : \n */ 181 } 182} 183 184static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, 185 unsigned int to, char __user *user_buf, 186 size_t count, loff_t *ppos) 187{ 188 size_t buf_pos = 0; 189 loff_t p = *ppos; 190 ssize_t ret; 191 int i; 192 char *buf; 193 unsigned int val, start_reg; 194 195 if (*ppos < 0 || !count) 196 return -EINVAL; 197 198 buf = kmalloc(count, GFP_KERNEL); 199 if (!buf) 200 return -ENOMEM; 201 202 regmap_calc_tot_len(map, buf, count); 203 204 /* Work out which register we're starting at */ 205 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); 206 207 for (i = start_reg; i <= to; i += map->reg_stride) { 208 if (!regmap_readable(map, i)) 209 continue; 210 211 if (regmap_precious(map, i)) 212 continue; 213 214 /* If we're in the region the user is trying to read */ 215 if (p >= *ppos) { 216 /* ...but not beyond it */ 217 if (buf_pos + map->debugfs_tot_len > count) 218 break; 219 220 /* Format the register */ 221 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", 222 map->debugfs_reg_len, i - from); 223 buf_pos += map->debugfs_reg_len + 2; 224 225 /* Format the value, write all X if we can't read */ 226 ret = regmap_read(map, i, &val); 227 if (ret == 0) 228 snprintf(buf + buf_pos, count - buf_pos, 229 "%.*x", map->debugfs_val_len, val); 230 else 231 memset(buf + buf_pos, 'X', 232 map->debugfs_val_len); 233 buf_pos += 2 * map->format.val_bytes; 234 235 buf[buf_pos++] = '\n'; 236 } 237 p += map->debugfs_tot_len; 238 } 239 240 ret = buf_pos; 241 242 if (copy_to_user(user_buf, buf, buf_pos)) { 243 ret = -EFAULT; 244 goto out; 245 } 246 247 *ppos += buf_pos; 248 249out: 250 kfree(buf); 251 return ret; 252} 253 254static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, 255 size_t count, loff_t *ppos) 256{ 257 struct regmap *map = file->private_data; 258 259 return regmap_read_debugfs(map, 0, map->max_register, user_buf, 260 count, ppos); 261} 262 263#undef REGMAP_ALLOW_WRITE_DEBUGFS 264#ifdef REGMAP_ALLOW_WRITE_DEBUGFS 265/* 266 * This can be dangerous especially when we have clients such as 267 * PMICs, therefore don't provide any real compile time configuration option 268 * for this feature, people who want to use this will need to modify 269 * the source code directly. 270 */ 271static ssize_t regmap_map_write_file(struct file *file, 272 const char __user *user_buf, 273 size_t count, loff_t *ppos) 274{ 275 char buf[32]; 276 size_t buf_size; 277 char *start = buf; 278 unsigned long reg, value; 279 struct regmap *map = file->private_data; 280 int ret; 281 282 buf_size = min(count, (sizeof(buf)-1)); 283 if (copy_from_user(buf, user_buf, buf_size)) 284 return -EFAULT; 285 buf[buf_size] = 0; 286 287 while (*start == ' ') 288 start++; 289 reg = simple_strtoul(start, &start, 16); 290 while (*start == ' ') 291 start++; 292 if (kstrtoul(start, 16, &value)) 293 return -EINVAL; 294 295 /* Userspace has been fiddling around behind the kernel's back */ 296 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 297 298 ret = regmap_write(map, reg, value); 299 if (ret < 0) 300 return ret; 301 return buf_size; 302} 303#else 304#define regmap_map_write_file NULL 305#endif 306 307static const struct file_operations regmap_map_fops = { 308 .open = simple_open, 309 .read = regmap_map_read_file, 310 .write = regmap_map_write_file, 311 .llseek = default_llseek, 312}; 313 314static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, 315 size_t count, loff_t *ppos) 316{ 317 struct regmap_range_node *range = file->private_data; 318 struct regmap *map = range->map; 319 320 return regmap_read_debugfs(map, range->range_min, range->range_max, 321 user_buf, count, ppos); 322} 323 324static const struct file_operations regmap_range_fops = { 325 .open = simple_open, 326 .read = regmap_range_read_file, 327 .llseek = default_llseek, 328}; 329 330static ssize_t regmap_reg_ranges_read_file(struct file *file, 331 char __user *user_buf, size_t count, 332 loff_t *ppos) 333{ 334 struct regmap *map = file->private_data; 335 struct regmap_debugfs_off_cache *c; 336 loff_t p = 0; 337 size_t buf_pos = 0; 338 char *buf; 339 char *entry; 340 int ret; 341 342 if (*ppos < 0 || !count) 343 return -EINVAL; 344 345 buf = kmalloc(count, GFP_KERNEL); 346 if (!buf) 347 return -ENOMEM; 348 349 entry = kmalloc(PAGE_SIZE, GFP_KERNEL); 350 if (!entry) { 351 kfree(buf); 352 return -ENOMEM; 353 } 354 355 /* While we are at it, build the register dump cache 356 * now so the read() operation on the `registers' file 357 * can benefit from using the cache. We do not care 358 * about the file position information that is contained 359 * in the cache, just about the actual register blocks */ 360 regmap_calc_tot_len(map, buf, count); 361 regmap_debugfs_get_dump_start(map, 0, *ppos, &p); 362 363 /* Reset file pointer as the fixed-format of the `registers' 364 * file is not compatible with the `range' file */ 365 p = 0; 366 mutex_lock(&map->cache_lock); 367 list_for_each_entry(c, &map->debugfs_off_cache, list) { 368 snprintf(entry, PAGE_SIZE, "%x-%x", 369 c->base_reg, c->max_reg); 370 if (p >= *ppos) { 371 if (buf_pos + 1 + strlen(entry) > count) 372 break; 373 snprintf(buf + buf_pos, count - buf_pos, 374 "%s", entry); 375 buf_pos += strlen(entry); 376 buf[buf_pos] = '\n'; 377 buf_pos++; 378 } 379 p += strlen(entry) + 1; 380 } 381 mutex_unlock(&map->cache_lock); 382 383 kfree(entry); 384 ret = buf_pos; 385 386 if (copy_to_user(user_buf, buf, buf_pos)) { 387 ret = -EFAULT; 388 goto out_buf; 389 } 390 391 *ppos += buf_pos; 392out_buf: 393 kfree(buf); 394 return ret; 395} 396 397static const struct file_operations regmap_reg_ranges_fops = { 398 .open = simple_open, 399 .read = regmap_reg_ranges_read_file, 400 .llseek = default_llseek, 401}; 402 403static ssize_t regmap_access_read_file(struct file *file, 404 char __user *user_buf, size_t count, 405 loff_t *ppos) 406{ 407 int reg_len, tot_len; 408 size_t buf_pos = 0; 409 loff_t p = 0; 410 ssize_t ret; 411 int i; 412 struct regmap *map = file->private_data; 413 char *buf; 414 415 if (*ppos < 0 || !count) 416 return -EINVAL; 417 418 buf = kmalloc(count, GFP_KERNEL); 419 if (!buf) 420 return -ENOMEM; 421 422 /* Calculate the length of a fixed format */ 423 reg_len = regmap_calc_reg_len(map->max_register, buf, count); 424 tot_len = reg_len + 10; /* ': R W V P\n' */ 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 /* If we're in the region the user is trying to read */ 432 if (p >= *ppos) { 433 /* ...but not beyond it */ 434 if (buf_pos + tot_len + 1 >= count) 435 break; 436 437 /* Format the register */ 438 snprintf(buf + buf_pos, count - buf_pos, 439 "%.*x: %c %c %c %c\n", 440 reg_len, i, 441 regmap_readable(map, i) ? 'y' : 'n', 442 regmap_writeable(map, i) ? 'y' : 'n', 443 regmap_volatile(map, i) ? 'y' : 'n', 444 regmap_precious(map, i) ? 'y' : 'n'); 445 446 buf_pos += tot_len; 447 } 448 p += tot_len; 449 } 450 451 ret = buf_pos; 452 453 if (copy_to_user(user_buf, buf, buf_pos)) { 454 ret = -EFAULT; 455 goto out; 456 } 457 458 *ppos += buf_pos; 459 460out: 461 kfree(buf); 462 return ret; 463} 464 465static const struct file_operations regmap_access_fops = { 466 .open = simple_open, 467 .read = regmap_access_read_file, 468 .llseek = default_llseek, 469}; 470 471void regmap_debugfs_init(struct regmap *map, const char *name) 472{ 473 struct rb_node *next; 474 struct regmap_range_node *range_node; 475 const char *devname = "dummy"; 476 477 /* If we don't have the debugfs root yet, postpone init */ 478 if (!regmap_debugfs_root) { 479 struct regmap_debugfs_node *node; 480 node = kzalloc(sizeof(*node), GFP_KERNEL); 481 if (!node) 482 return; 483 node->map = map; 484 node->name = name; 485 mutex_lock(®map_debugfs_early_lock); 486 list_add(&node->link, ®map_debugfs_early_list); 487 mutex_unlock(®map_debugfs_early_lock); 488 return; 489 } 490 491 INIT_LIST_HEAD(&map->debugfs_off_cache); 492 mutex_init(&map->cache_lock); 493 494 if (map->dev) 495 devname = dev_name(map->dev); 496 497 if (name) { 498 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", 499 devname, name); 500 name = map->debugfs_name; 501 } else { 502 name = devname; 503 } 504 505 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); 506 if (!map->debugfs) { 507 dev_warn(map->dev, "Failed to create debugfs directory\n"); 508 return; 509 } 510 511 debugfs_create_file("name", 0400, map->debugfs, 512 map, ®map_name_fops); 513 514 debugfs_create_file("range", 0400, map->debugfs, 515 map, ®map_reg_ranges_fops); 516 517 if (map->max_register || regmap_readable(map, 0)) { 518 umode_t registers_mode; 519 520 if (IS_ENABLED(REGMAP_ALLOW_WRITE_DEBUGFS)) 521 registers_mode = 0600; 522 else 523 registers_mode = 0400; 524 525 debugfs_create_file("registers", registers_mode, map->debugfs, 526 map, ®map_map_fops); 527 debugfs_create_file("access", 0400, map->debugfs, 528 map, ®map_access_fops); 529 } 530 531 if (map->cache_type) { 532 debugfs_create_bool("cache_only", 0400, map->debugfs, 533 &map->cache_only); 534 debugfs_create_bool("cache_dirty", 0400, map->debugfs, 535 &map->cache_dirty); 536 debugfs_create_bool("cache_bypass", 0400, map->debugfs, 537 &map->cache_bypass); 538 } 539 540 next = rb_first(&map->range_tree); 541 while (next) { 542 range_node = rb_entry(next, struct regmap_range_node, node); 543 544 if (range_node->name) 545 debugfs_create_file(range_node->name, 0400, 546 map->debugfs, range_node, 547 ®map_range_fops); 548 549 next = rb_next(&range_node->node); 550 } 551 552 if (map->cache_ops && map->cache_ops->debugfs_init) 553 map->cache_ops->debugfs_init(map); 554} 555 556void regmap_debugfs_exit(struct regmap *map) 557{ 558 if (map->debugfs) { 559 debugfs_remove_recursive(map->debugfs); 560 mutex_lock(&map->cache_lock); 561 regmap_debugfs_free_dump_cache(map); 562 mutex_unlock(&map->cache_lock); 563 kfree(map->debugfs_name); 564 } else { 565 struct regmap_debugfs_node *node, *tmp; 566 567 mutex_lock(®map_debugfs_early_lock); 568 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, 569 link) { 570 if (node->map == map) { 571 list_del(&node->link); 572 kfree(node); 573 } 574 } 575 mutex_unlock(®map_debugfs_early_lock); 576 } 577} 578 579void regmap_debugfs_initcall(void) 580{ 581 struct regmap_debugfs_node *node, *tmp; 582 583 regmap_debugfs_root = debugfs_create_dir("regmap", NULL); 584 if (!regmap_debugfs_root) { 585 pr_warn("regmap: Failed to create debugfs root\n"); 586 return; 587 } 588 589 mutex_lock(®map_debugfs_early_lock); 590 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) { 591 regmap_debugfs_init(node->map, node->name); 592 list_del(&node->link); 593 kfree(node); 594 } 595 mutex_unlock(®map_debugfs_early_lock); 596} 597