regmap-debugfs.c 16 KB

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