1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
4 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5 */
6
7 #include <linux/debugfs.h>
8 #include "vchiq_core.h"
9 #include "vchiq_arm.h"
10 #include "vchiq_debugfs.h"
11
12 #ifdef CONFIG_DEBUG_FS
13
14 /****************************************************************************
15 *
16 * log category entries
17 *
18 ***************************************************************************/
19 #define DEBUGFS_WRITE_BUF_SIZE 256
20
21 #define VCHIQ_LOG_ERROR_STR "error"
22 #define VCHIQ_LOG_WARNING_STR "warning"
23 #define VCHIQ_LOG_INFO_STR "info"
24 #define VCHIQ_LOG_TRACE_STR "trace"
25
26 /* Global 'vchiq' debugfs and clients entry used by all instances */
27 static struct dentry *vchiq_dbg_dir;
28 static struct dentry *vchiq_dbg_clients;
29
30 /* Log category debugfs entries */
31 struct vchiq_debugfs_log_entry {
32 const char *name;
33 void *plevel;
34 };
35
36 static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
37 { "core", &vchiq_core_log_level },
38 { "msg", &vchiq_core_msg_log_level },
39 { "sync", &vchiq_sync_log_level },
40 { "susp", &vchiq_susp_log_level },
41 { "arm", &vchiq_arm_log_level },
42 };
43 static int n_log_entries = ARRAY_SIZE(vchiq_debugfs_log_entries);
44
debugfs_log_show(struct seq_file * f,void * offset)45 static int debugfs_log_show(struct seq_file *f, void *offset)
46 {
47 int *levp = f->private;
48 char *log_value = NULL;
49
50 switch (*levp) {
51 case VCHIQ_LOG_ERROR:
52 log_value = VCHIQ_LOG_ERROR_STR;
53 break;
54 case VCHIQ_LOG_WARNING:
55 log_value = VCHIQ_LOG_WARNING_STR;
56 break;
57 case VCHIQ_LOG_INFO:
58 log_value = VCHIQ_LOG_INFO_STR;
59 break;
60 case VCHIQ_LOG_TRACE:
61 log_value = VCHIQ_LOG_TRACE_STR;
62 break;
63 default:
64 break;
65 }
66
67 seq_printf(f, "%s\n", log_value ? log_value : "(null)");
68
69 return 0;
70 }
71
debugfs_log_open(struct inode * inode,struct file * file)72 static int debugfs_log_open(struct inode *inode, struct file *file)
73 {
74 return single_open(file, debugfs_log_show, inode->i_private);
75 }
76
debugfs_log_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)77 static ssize_t debugfs_log_write(struct file *file,
78 const char __user *buffer,
79 size_t count, loff_t *ppos)
80 {
81 struct seq_file *f = (struct seq_file *)file->private_data;
82 int *levp = f->private;
83 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
84
85 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
86 if (count >= DEBUGFS_WRITE_BUF_SIZE)
87 count = DEBUGFS_WRITE_BUF_SIZE;
88
89 if (copy_from_user(kbuf, buffer, count))
90 return -EFAULT;
91 kbuf[count - 1] = 0;
92
93 if (strncmp("error", kbuf, strlen("error")) == 0)
94 *levp = VCHIQ_LOG_ERROR;
95 else if (strncmp("warning", kbuf, strlen("warning")) == 0)
96 *levp = VCHIQ_LOG_WARNING;
97 else if (strncmp("info", kbuf, strlen("info")) == 0)
98 *levp = VCHIQ_LOG_INFO;
99 else if (strncmp("trace", kbuf, strlen("trace")) == 0)
100 *levp = VCHIQ_LOG_TRACE;
101 else
102 *levp = VCHIQ_LOG_DEFAULT;
103
104 *ppos += count;
105
106 return count;
107 }
108
109 static const struct file_operations debugfs_log_fops = {
110 .owner = THIS_MODULE,
111 .open = debugfs_log_open,
112 .write = debugfs_log_write,
113 .read = seq_read,
114 .llseek = seq_lseek,
115 .release = single_release,
116 };
117
debugfs_usecount_show(struct seq_file * f,void * offset)118 static int debugfs_usecount_show(struct seq_file *f, void *offset)
119 {
120 struct vchiq_instance *instance = f->private;
121 int use_count;
122
123 use_count = vchiq_instance_get_use_count(instance);
124 seq_printf(f, "%d\n", use_count);
125
126 return 0;
127 }
128 DEFINE_SHOW_ATTRIBUTE(debugfs_usecount);
129
debugfs_trace_show(struct seq_file * f,void * offset)130 static int debugfs_trace_show(struct seq_file *f, void *offset)
131 {
132 struct vchiq_instance *instance = f->private;
133 int trace;
134
135 trace = vchiq_instance_get_trace(instance);
136 seq_printf(f, "%s\n", trace ? "Y" : "N");
137
138 return 0;
139 }
140
debugfs_trace_open(struct inode * inode,struct file * file)141 static int debugfs_trace_open(struct inode *inode, struct file *file)
142 {
143 return single_open(file, debugfs_trace_show, inode->i_private);
144 }
145
debugfs_trace_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)146 static ssize_t debugfs_trace_write(struct file *file,
147 const char __user *buffer,
148 size_t count, loff_t *ppos)
149 {
150 struct seq_file *f = (struct seq_file *)file->private_data;
151 struct vchiq_instance *instance = f->private;
152 char firstchar;
153
154 if (copy_from_user(&firstchar, buffer, 1))
155 return -EFAULT;
156
157 switch (firstchar) {
158 case 'Y':
159 case 'y':
160 case '1':
161 vchiq_instance_set_trace(instance, 1);
162 break;
163 case 'N':
164 case 'n':
165 case '0':
166 vchiq_instance_set_trace(instance, 0);
167 break;
168 default:
169 break;
170 }
171
172 *ppos += count;
173
174 return count;
175 }
176
177 static const struct file_operations debugfs_trace_fops = {
178 .owner = THIS_MODULE,
179 .open = debugfs_trace_open,
180 .write = debugfs_trace_write,
181 .read = seq_read,
182 .llseek = seq_lseek,
183 .release = single_release,
184 };
185
186 /* add an instance (process) to the debugfs entries */
vchiq_debugfs_add_instance(struct vchiq_instance * instance)187 void vchiq_debugfs_add_instance(struct vchiq_instance *instance)
188 {
189 char pidstr[16];
190 struct dentry *top;
191
192 snprintf(pidstr, sizeof(pidstr), "%d",
193 vchiq_instance_get_pid(instance));
194
195 top = debugfs_create_dir(pidstr, vchiq_dbg_clients);
196
197 debugfs_create_file("use_count", 0444, top, instance,
198 &debugfs_usecount_fops);
199 debugfs_create_file("trace", 0644, top, instance, &debugfs_trace_fops);
200
201 vchiq_instance_get_debugfs_node(instance)->dentry = top;
202 }
203
vchiq_debugfs_remove_instance(struct vchiq_instance * instance)204 void vchiq_debugfs_remove_instance(struct vchiq_instance *instance)
205 {
206 struct vchiq_debugfs_node *node =
207 vchiq_instance_get_debugfs_node(instance);
208
209 debugfs_remove_recursive(node->dentry);
210 }
211
vchiq_debugfs_init(void)212 void vchiq_debugfs_init(void)
213 {
214 struct dentry *dir;
215 int i;
216
217 vchiq_dbg_dir = debugfs_create_dir("vchiq", NULL);
218 vchiq_dbg_clients = debugfs_create_dir("clients", vchiq_dbg_dir);
219
220 /* create an entry under <debugfs>/vchiq/log for each log category */
221 dir = debugfs_create_dir("log", vchiq_dbg_dir);
222
223 for (i = 0; i < n_log_entries; i++)
224 debugfs_create_file(vchiq_debugfs_log_entries[i].name, 0644,
225 dir, vchiq_debugfs_log_entries[i].plevel,
226 &debugfs_log_fops);
227 }
228
229 /* remove all the debugfs entries */
vchiq_debugfs_deinit(void)230 void vchiq_debugfs_deinit(void)
231 {
232 debugfs_remove_recursive(vchiq_dbg_dir);
233 }
234
235 #else /* CONFIG_DEBUG_FS */
236
vchiq_debugfs_init(void)237 void vchiq_debugfs_init(void)
238 {
239 }
240
vchiq_debugfs_deinit(void)241 void vchiq_debugfs_deinit(void)
242 {
243 }
244
vchiq_debugfs_add_instance(struct vchiq_instance * instance)245 void vchiq_debugfs_add_instance(struct vchiq_instance *instance)
246 {
247 }
248
vchiq_debugfs_remove_instance(struct vchiq_instance * instance)249 void vchiq_debugfs_remove_instance(struct vchiq_instance *instance)
250 {
251 }
252
253 #endif /* CONFIG_DEBUG_FS */
254