debugfs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic OPP debugfs interface
  4. *
  5. * Copyright (C) 2015-2016 Viresh Kumar <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/debugfs.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/of.h>
  12. #include <linux/init.h>
  13. #include <linux/limits.h>
  14. #include <linux/slab.h>
  15. #include "opp.h"
  16. static struct dentry *rootdir;
  17. static void opp_set_dev_name(const struct device *dev, char *name)
  18. {
  19. if (dev->parent)
  20. snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
  21. dev_name(dev));
  22. else
  23. snprintf(name, NAME_MAX, "%s", dev_name(dev));
  24. }
  25. void opp_debug_remove_one(struct dev_pm_opp *opp)
  26. {
  27. debugfs_remove_recursive(opp->dentry);
  28. }
  29. static ssize_t bw_name_read(struct file *fp, char __user *userbuf,
  30. size_t count, loff_t *ppos)
  31. {
  32. struct icc_path *path = fp->private_data;
  33. char buf[64];
  34. int i;
  35. i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
  36. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  37. }
  38. static const struct file_operations bw_name_fops = {
  39. .open = simple_open,
  40. .read = bw_name_read,
  41. .llseek = default_llseek,
  42. };
  43. static void opp_debug_create_bw(struct dev_pm_opp *opp,
  44. struct opp_table *opp_table,
  45. struct dentry *pdentry)
  46. {
  47. struct dentry *d;
  48. char name[11];
  49. int i;
  50. for (i = 0; i < opp_table->path_count; i++) {
  51. snprintf(name, sizeof(name), "icc-path-%.1d", i);
  52. /* Create per-path directory */
  53. d = debugfs_create_dir(name, pdentry);
  54. debugfs_create_file("name", S_IRUGO, d, opp_table->paths[i],
  55. &bw_name_fops);
  56. debugfs_create_u32("peak_bw", S_IRUGO, d,
  57. &opp->bandwidth[i].peak);
  58. debugfs_create_u32("avg_bw", S_IRUGO, d,
  59. &opp->bandwidth[i].avg);
  60. }
  61. }
  62. static void opp_debug_create_clks(struct dev_pm_opp *opp,
  63. struct opp_table *opp_table,
  64. struct dentry *pdentry)
  65. {
  66. char name[12];
  67. int i;
  68. if (opp_table->clk_count == 1) {
  69. debugfs_create_ulong("rate_hz", S_IRUGO, pdentry, &opp->rates[0]);
  70. return;
  71. }
  72. for (i = 0; i < opp_table->clk_count; i++) {
  73. snprintf(name, sizeof(name), "rate_hz_%d", i);
  74. debugfs_create_ulong(name, S_IRUGO, pdentry, &opp->rates[i]);
  75. }
  76. }
  77. static void opp_debug_create_supplies(struct dev_pm_opp *opp,
  78. struct opp_table *opp_table,
  79. struct dentry *pdentry)
  80. {
  81. struct dentry *d;
  82. int i;
  83. for (i = 0; i < opp_table->regulator_count; i++) {
  84. char name[15];
  85. snprintf(name, sizeof(name), "supply-%d", i);
  86. /* Create per-opp directory */
  87. d = debugfs_create_dir(name, pdentry);
  88. debugfs_create_ulong("u_volt_target", S_IRUGO, d,
  89. &opp->supplies[i].u_volt);
  90. debugfs_create_ulong("u_volt_min", S_IRUGO, d,
  91. &opp->supplies[i].u_volt_min);
  92. debugfs_create_ulong("u_volt_max", S_IRUGO, d,
  93. &opp->supplies[i].u_volt_max);
  94. debugfs_create_ulong("u_amp", S_IRUGO, d,
  95. &opp->supplies[i].u_amp);
  96. debugfs_create_ulong("u_watt", S_IRUGO, d,
  97. &opp->supplies[i].u_watt);
  98. }
  99. }
  100. void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
  101. {
  102. struct dentry *pdentry = opp_table->dentry;
  103. struct dentry *d;
  104. unsigned long id;
  105. char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
  106. /*
  107. * Get directory name for OPP.
  108. *
  109. * - Normally rate is unique to each OPP, use it to get unique opp-name.
  110. * - For some devices rate isn't available or there are multiple, use
  111. * index instead for them.
  112. */
  113. if (likely(opp_table->clk_count == 1 && opp->rates[0]))
  114. id = opp->rates[0];
  115. else
  116. id = _get_opp_count(opp_table);
  117. snprintf(name, sizeof(name), "opp:%lu", id);
  118. /* Create per-opp directory */
  119. d = debugfs_create_dir(name, pdentry);
  120. debugfs_create_bool("available", S_IRUGO, d, &opp->available);
  121. debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic);
  122. debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo);
  123. debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend);
  124. debugfs_create_u32("performance_state", S_IRUGO, d, &opp->pstate);
  125. debugfs_create_u32("level", S_IRUGO, d, &opp->level);
  126. debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
  127. &opp->clock_latency_ns);
  128. opp->of_name = of_node_full_name(opp->np);
  129. debugfs_create_str("of_name", S_IRUGO, d, (char **)&opp->of_name);
  130. opp_debug_create_clks(opp, opp_table, d);
  131. opp_debug_create_supplies(opp, opp_table, d);
  132. opp_debug_create_bw(opp, opp_table, d);
  133. opp->dentry = d;
  134. }
  135. static void opp_list_debug_create_dir(struct opp_device *opp_dev,
  136. struct opp_table *opp_table)
  137. {
  138. const struct device *dev = opp_dev->dev;
  139. struct dentry *d;
  140. opp_set_dev_name(dev, opp_table->dentry_name);
  141. /* Create device specific directory */
  142. d = debugfs_create_dir(opp_table->dentry_name, rootdir);
  143. opp_dev->dentry = d;
  144. opp_table->dentry = d;
  145. }
  146. static void opp_list_debug_create_link(struct opp_device *opp_dev,
  147. struct opp_table *opp_table)
  148. {
  149. char name[NAME_MAX];
  150. opp_set_dev_name(opp_dev->dev, name);
  151. /* Create device specific directory link */
  152. opp_dev->dentry = debugfs_create_symlink(name, rootdir,
  153. opp_table->dentry_name);
  154. }
  155. /**
  156. * opp_debug_register - add a device opp node to the debugfs 'opp' directory
  157. * @opp_dev: opp-dev pointer for device
  158. * @opp_table: the device-opp being added
  159. *
  160. * Dynamically adds device specific directory in debugfs 'opp' directory. If the
  161. * device-opp is shared with other devices, then links will be created for all
  162. * devices except the first.
  163. */
  164. void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
  165. {
  166. if (opp_table->dentry)
  167. opp_list_debug_create_link(opp_dev, opp_table);
  168. else
  169. opp_list_debug_create_dir(opp_dev, opp_table);
  170. }
  171. static void opp_migrate_dentry(struct opp_device *opp_dev,
  172. struct opp_table *opp_table)
  173. {
  174. struct opp_device *new_dev = NULL, *iter;
  175. const struct device *dev;
  176. struct dentry *dentry;
  177. /* Look for next opp-dev */
  178. list_for_each_entry(iter, &opp_table->dev_list, node)
  179. if (iter != opp_dev) {
  180. new_dev = iter;
  181. break;
  182. }
  183. BUG_ON(!new_dev);
  184. /* new_dev is guaranteed to be valid here */
  185. dev = new_dev->dev;
  186. debugfs_remove_recursive(new_dev->dentry);
  187. opp_set_dev_name(dev, opp_table->dentry_name);
  188. dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
  189. opp_table->dentry_name);
  190. if (IS_ERR(dentry)) {
  191. dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
  192. __func__, dev_name(opp_dev->dev), dev_name(dev));
  193. return;
  194. }
  195. new_dev->dentry = dentry;
  196. opp_table->dentry = dentry;
  197. }
  198. /**
  199. * opp_debug_unregister - remove a device opp node from debugfs opp directory
  200. * @opp_dev: opp-dev pointer for device
  201. * @opp_table: the device-opp being removed
  202. *
  203. * Dynamically removes device specific directory from debugfs 'opp' directory.
  204. */
  205. void opp_debug_unregister(struct opp_device *opp_dev,
  206. struct opp_table *opp_table)
  207. {
  208. if (opp_dev->dentry == opp_table->dentry) {
  209. /* Move the real dentry object under another device */
  210. if (!list_is_singular(&opp_table->dev_list)) {
  211. opp_migrate_dentry(opp_dev, opp_table);
  212. goto out;
  213. }
  214. opp_table->dentry = NULL;
  215. }
  216. debugfs_remove_recursive(opp_dev->dentry);
  217. out:
  218. opp_dev->dentry = NULL;
  219. }
  220. static int __init opp_debug_init(void)
  221. {
  222. /* Create /sys/kernel/debug/opp directory */
  223. rootdir = debugfs_create_dir("opp", NULL);
  224. return 0;
  225. }
  226. core_initcall(opp_debug_init);