regmap-debugfs.c 16 KB

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