1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic OPP debugfs interface
4 *
5 * Copyright (C) 2015-2016 Viresh Kumar <viresh.kumar@linaro.org>
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/debugfs.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/limits.h>
15 #include <linux/slab.h>
16
17 #include "opp.h"
18
19 static struct dentry *rootdir;
20
opp_set_dev_name(const struct device * dev,char * name)21 static void opp_set_dev_name(const struct device *dev, char *name)
22 {
23 if (dev->parent) {
24 snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent), dev_name(dev));
25 } else {
26 snprintf(name, NAME_MAX, "%s", dev_name(dev));
27 }
28 }
29
opp_debug_remove_one(struct dev_pm_opp * opp)30 void opp_debug_remove_one(struct dev_pm_opp *opp)
31 {
32 debugfs_remove_recursive(opp->dentry);
33 }
34
bw_name_read(struct file * fp,char __user * userbuf,size_t count,loff_t * ppos)35 static ssize_t bw_name_read(struct file *fp, char __user *userbuf, size_t count, loff_t *ppos)
36 {
37 struct icc_path *path = fp->private_data;
38 char buf[64];
39 int i;
40
41 i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
42
43 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
44 }
45
46 static const struct file_operations bw_name_fops = {
47 .open = simple_open,
48 .read = bw_name_read,
49 .llseek = default_llseek,
50 };
51
opp_debug_create_bw(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)52 static void opp_debug_create_bw(struct dev_pm_opp *opp, struct opp_table *opp_table, struct dentry *pdentry)
53 {
54 struct dentry *d;
55 char name[11];
56 int i, ret;
57
58 for (i = 0; i < opp_table->path_count; i++) {
59 ret = snprintf(name, sizeof(name), "icc-path-%.1d", i);
60
61 /* Create per-path directory */
62 d = debugfs_create_dir(name, pdentry);
63
64 debugfs_create_file("name", S_IRUGO, d, opp_table->paths[i], &bw_name_fops);
65 debugfs_create_u32("peak_bw", S_IRUGO, d, &opp->bandwidth[i].peak);
66 debugfs_create_u32("avg_bw", S_IRUGO, d, &opp->bandwidth[i].avg);
67 }
68 }
69
opp_debug_create_supplies(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)70 static void opp_debug_create_supplies(struct dev_pm_opp *opp, struct opp_table *opp_table, struct dentry *pdentry)
71 {
72 struct dentry *d;
73 int i, ret;
74
75 for (i = 0; i < opp_table->regulator_count; i++) {
76 char name[15];
77
78 ret = snprintf(name, sizeof(name), "supply-%d", i);
79
80 /* Create per-opp directory */
81 d = debugfs_create_dir(name, pdentry);
82
83 debugfs_create_ulong("u_volt_target", S_IRUGO, d, &opp->supplies[i].u_volt);
84
85 debugfs_create_ulong("u_volt_min", S_IRUGO, d, &opp->supplies[i].u_volt_min);
86
87 debugfs_create_ulong("u_volt_max", S_IRUGO, d, &opp->supplies[i].u_volt_max);
88
89 debugfs_create_ulong("u_amp", S_IRUGO, d, &opp->supplies[i].u_amp);
90 }
91 }
92
opp_debug_create_one(struct dev_pm_opp * opp,struct opp_table * opp_table)93 void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
94 {
95 struct dentry *pdentry = opp_table->dentry;
96 struct dentry *d;
97 unsigned long id;
98 char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
99 int ret;
100 /*
101 * Get directory name for OPP.
102 *
103 * - Normally rate is unique to each OPP, use it to get unique opp-name.
104 * - For some devices rate isn't available, use index instead.
105 */
106 if (likely(opp->rate)) {
107 id = opp->rate;
108 } else {
109 id = _get_opp_count(opp_table);
110 }
111
112 ret = snprintf(name, sizeof(name), "opp:%lu", id);
113
114 /* Create per-opp directory */
115 d = debugfs_create_dir(name, pdentry);
116
117 debugfs_create_bool("available", S_IRUGO, d, &opp->available);
118 debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic);
119 debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo);
120 debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend);
121 debugfs_create_u32("performance_state", S_IRUGO, d, &opp->pstate);
122 debugfs_create_ulong("rate_hz", S_IRUGO, d, &opp->rate);
123 debugfs_create_ulong("clock_latency_ns", S_IRUGO, d, &opp->clock_latency_ns);
124
125 opp_debug_create_supplies(opp, opp_table, d);
126 opp_debug_create_bw(opp, opp_table, d);
127
128 opp->dentry = d;
129 }
130
opp_list_debug_create_dir(struct opp_device * opp_dev,struct opp_table * opp_table)131 static void opp_list_debug_create_dir(struct opp_device *opp_dev, struct opp_table *opp_table)
132 {
133 const struct device *dev = opp_dev->dev;
134 struct dentry *d;
135
136 opp_set_dev_name(dev, opp_table->dentry_name);
137
138 /* Create device specific directory */
139 d = debugfs_create_dir(opp_table->dentry_name, rootdir);
140
141 opp_dev->dentry = d;
142 opp_table->dentry = d;
143 }
144
opp_list_debug_create_link(struct opp_device * opp_dev,struct opp_table * opp_table)145 static void opp_list_debug_create_link(struct opp_device *opp_dev, struct opp_table *opp_table)
146 {
147 char name[NAME_MAX];
148
149 opp_set_dev_name(opp_dev->dev, name);
150
151 /* Create device specific directory link */
152 opp_dev->dentry = debugfs_create_symlink(name, rootdir, opp_table->dentry_name);
153 }
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 */
opp_debug_register(struct opp_device * opp_dev,struct opp_table * opp_table)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 }
172
opp_migrate_dentry(struct opp_device * opp_dev,struct opp_table * opp_table)173 static void opp_migrate_dentry(struct opp_device *opp_dev, struct opp_table *opp_table)
174 {
175 struct opp_device *new_dev;
176 const struct device *dev;
177 struct dentry *dentry;
178
179 /* Look for next opp-dev */
180 list_for_each_entry(new_dev, &opp_table->dev_list, node) if (new_dev != opp_dev) break;
181
182 /* new_dev is guaranteed to be valid here */
183 dev = new_dev->dev;
184 debugfs_remove_recursive(new_dev->dentry);
185
186 opp_set_dev_name(dev, opp_table->dentry_name);
187
188 dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir, opp_table->dentry_name);
189 if (!dentry) {
190 dev_err(dev, "%s: Failed to rename link from: %s to %s\n", __func__, dev_name(opp_dev->dev), dev_name(dev));
191 return;
192 }
193
194 new_dev->dentry = dentry;
195 opp_table->dentry = dentry;
196 }
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 */
opp_debug_unregister(struct opp_device * opp_dev,struct opp_table * opp_table)205 void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table)
206 {
207 if (opp_dev->dentry == opp_table->dentry) {
208 /* Move the real dentry object under another device */
209 if (!list_is_singular(&opp_table->dev_list)) {
210 opp_migrate_dentry(opp_dev, opp_table);
211 goto out;
212 }
213 opp_table->dentry = NULL;
214 }
215
216 debugfs_remove_recursive(opp_dev->dentry);
217
218 out:
219 opp_dev->dentry = NULL;
220 }
221
opp_summary_show(struct seq_file * s,void * data)222 static int opp_summary_show(struct seq_file *s, void *data)
223 {
224 struct list_head *lists = (struct list_head *)s->private;
225 struct opp_table *opp_table;
226 struct dev_pm_opp *opp;
227
228 mutex_lock(&opp_table_lock);
229
230 seq_puts(s, " device rate(Hz) target(uV) min(uV) max(uV)\n");
231 seq_puts(s, "-------------------------------------------------------------------\n");
232
233 list_for_each_entry(opp_table, lists, node)
234 {
235 seq_printf(s, " %s\n", opp_table->dentry_name);
236 mutex_lock(&opp_table->lock);
237 list_for_each_entry(opp, &opp_table->opp_list, node)
238 {
239 seq_printf(s, "%31lu %12lu %11lu %11lu\n", opp->rate, opp->supplies[0].u_volt, opp->supplies[0].u_volt_min,
240 opp->supplies[0].u_volt_max);
241 }
242 mutex_unlock(&opp_table->lock);
243 }
244
245 mutex_unlock(&opp_table_lock);
246
247 return 0;
248 }
249
opp_summary_open(struct inode * inode,struct file * file)250 static int opp_summary_open(struct inode *inode, struct file *file)
251 {
252 return single_open(file, opp_summary_show, inode->i_private);
253 }
254
255 static const struct file_operations opp_summary_fops = {
256 .open = opp_summary_open,
257 .read = seq_read,
258 .llseek = seq_lseek,
259 .release = single_release,
260 };
261
opp_debug_init(void)262 static int __init opp_debug_init(void)
263 {
264 /* Create /sys/kernel/debug/opp directory */
265 rootdir = debugfs_create_dir("opp", NULL);
266
267 debugfs_create_file("opp_summary", 0444, rootdir, &opp_tables, &opp_summary_fops);
268
269 return 0;
270 }
271 core_initcall(opp_debug_init);
272