1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
4 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
7 #include <linux/debugfs.h>
8 #include "vchiq_core.h"
10 #include "vchiq_debugfs.h"
12 #ifdef CONFIG_DEBUG_FS
14 /****************************************************************************
16 * log category entries
18 ***************************************************************************/
19 #define DEBUGFS_WRITE_BUF_SIZE 256
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"
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;
30 /* Log category debugfs entries */
31 struct vchiq_debugfs_log_entry {
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 },
43 static int n_log_entries = ARRAY_SIZE(vchiq_debugfs_log_entries);
45 static int debugfs_log_show(struct seq_file *f, void *offset)
47 int *levp = f->private;
48 char *log_value = NULL;
52 log_value = VCHIQ_LOG_ERROR_STR;
54 case VCHIQ_LOG_WARNING:
55 log_value = VCHIQ_LOG_WARNING_STR;
58 log_value = VCHIQ_LOG_INFO_STR;
61 log_value = VCHIQ_LOG_TRACE_STR;
67 seq_printf(f, "%s\n", log_value ? log_value : "(null)");
72 static int debugfs_log_open(struct inode *inode, struct file *file)
74 return single_open(file, debugfs_log_show, inode->i_private);
77 static ssize_t debugfs_log_write(struct file *file,
78 const char __user *buffer,
79 size_t count, loff_t *ppos)
81 struct seq_file *f = (struct seq_file *)file->private_data;
82 int *levp = f->private;
83 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
85 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
86 if (count >= DEBUGFS_WRITE_BUF_SIZE)
87 count = DEBUGFS_WRITE_BUF_SIZE;
89 if (copy_from_user(kbuf, buffer, count) != 0)
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;
102 *levp = VCHIQ_LOG_DEFAULT;
109 static const struct file_operations debugfs_log_fops = {
110 .owner = THIS_MODULE,
111 .open = debugfs_log_open,
112 .write = debugfs_log_write,
115 .release = single_release,
118 static int debugfs_usecount_show(struct seq_file *f, void *offset)
120 VCHIQ_INSTANCE_T instance = f->private;
123 use_count = vchiq_instance_get_use_count(instance);
124 seq_printf(f, "%d\n", use_count);
128 DEFINE_SHOW_ATTRIBUTE(debugfs_usecount);
130 static int debugfs_trace_show(struct seq_file *f, void *offset)
132 VCHIQ_INSTANCE_T instance = f->private;
135 trace = vchiq_instance_get_trace(instance);
136 seq_printf(f, "%s\n", trace ? "Y" : "N");
141 static int debugfs_trace_open(struct inode *inode, struct file *file)
143 return single_open(file, debugfs_trace_show, inode->i_private);
146 static ssize_t debugfs_trace_write(struct file *file,
147 const char __user *buffer,
148 size_t count, loff_t *ppos)
150 struct seq_file *f = (struct seq_file *)file->private_data;
151 VCHIQ_INSTANCE_T instance = f->private;
154 if (copy_from_user(&firstchar, buffer, 1) != 0)
161 vchiq_instance_set_trace(instance, 1);
166 vchiq_instance_set_trace(instance, 0);
177 static const struct file_operations debugfs_trace_fops = {
178 .owner = THIS_MODULE,
179 .open = debugfs_trace_open,
180 .write = debugfs_trace_write,
183 .release = single_release,
186 /* add an instance (process) to the debugfs entries */
187 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
192 snprintf(pidstr, sizeof(pidstr), "%d",
193 vchiq_instance_get_pid(instance));
195 top = debugfs_create_dir(pidstr, vchiq_dbg_clients);
197 debugfs_create_file("use_count", 0444, top, instance,
198 &debugfs_usecount_fops);
199 debugfs_create_file("trace", 0644, top, instance, &debugfs_trace_fops);
201 vchiq_instance_get_debugfs_node(instance)->dentry = top;
204 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
206 struct vchiq_debugfs_node *node =
207 vchiq_instance_get_debugfs_node(instance);
209 debugfs_remove_recursive(node->dentry);
212 void vchiq_debugfs_init(void)
217 vchiq_dbg_dir = debugfs_create_dir("vchiq", NULL);
218 vchiq_dbg_clients = debugfs_create_dir("clients", vchiq_dbg_dir);
220 /* create an entry under <debugfs>/vchiq/log for each log category */
221 dir = debugfs_create_dir("log", vchiq_dbg_dir);
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,
229 /* remove all the debugfs entries */
230 void vchiq_debugfs_deinit(void)
232 debugfs_remove_recursive(vchiq_dbg_dir);
235 #else /* CONFIG_DEBUG_FS */
237 void vchiq_debugfs_init(void)
241 void vchiq_debugfs_deinit(void)
245 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
249 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
253 #endif /* CONFIG_DEBUG_FS */