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 #define DEBUGFS_WRITE_BUF_SIZE 256
15
16 /* Global 'vchiq' debugfs and clients entry used by all instances */
17 static struct dentry *vchiq_dbg_dir;
18 static struct dentry *vchiq_dbg_clients;
19
debugfs_usecount_show(struct seq_file * f,void * offset)20 static int debugfs_usecount_show(struct seq_file *f, void *offset)
21 {
22 struct vchiq_instance *instance = f->private;
23 int use_count;
24
25 use_count = vchiq_instance_get_use_count(instance);
26 seq_printf(f, "%d\n", use_count);
27
28 return 0;
29 }
30 DEFINE_SHOW_ATTRIBUTE(debugfs_usecount);
31
debugfs_trace_show(struct seq_file * f,void * offset)32 static int debugfs_trace_show(struct seq_file *f, void *offset)
33 {
34 struct vchiq_instance *instance = f->private;
35 int trace;
36
37 trace = vchiq_instance_get_trace(instance);
38 seq_printf(f, "%s\n", trace ? "Y" : "N");
39
40 return 0;
41 }
42
vchiq_dump_show(struct seq_file * f,void * offset)43 static int vchiq_dump_show(struct seq_file *f, void *offset)
44 {
45 struct vchiq_state *state = f->private;
46
47 vchiq_dump_state(f, state);
48
49 return 0;
50 }
51 DEFINE_SHOW_ATTRIBUTE(vchiq_dump);
52
debugfs_trace_open(struct inode * inode,struct file * file)53 static int debugfs_trace_open(struct inode *inode, struct file *file)
54 {
55 return single_open(file, debugfs_trace_show, inode->i_private);
56 }
57
debugfs_trace_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)58 static ssize_t debugfs_trace_write(struct file *file,
59 const char __user *buffer,
60 size_t count, loff_t *ppos)
61 {
62 struct seq_file *f = (struct seq_file *)file->private_data;
63 struct vchiq_instance *instance = f->private;
64 char firstchar;
65
66 if (copy_from_user(&firstchar, buffer, 1))
67 return -EFAULT;
68
69 switch (firstchar) {
70 case 'Y':
71 case 'y':
72 case '1':
73 vchiq_instance_set_trace(instance, 1);
74 break;
75 case 'N':
76 case 'n':
77 case '0':
78 vchiq_instance_set_trace(instance, 0);
79 break;
80 default:
81 break;
82 }
83
84 *ppos += count;
85
86 return count;
87 }
88
89 static const struct file_operations debugfs_trace_fops = {
90 .owner = THIS_MODULE,
91 .open = debugfs_trace_open,
92 .write = debugfs_trace_write,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = single_release,
96 };
97
98 /* add an instance (process) to the debugfs entries */
vchiq_debugfs_add_instance(struct vchiq_instance * instance)99 void vchiq_debugfs_add_instance(struct vchiq_instance *instance)
100 {
101 char pidstr[16];
102 struct dentry *top;
103
104 snprintf(pidstr, sizeof(pidstr), "%d",
105 vchiq_instance_get_pid(instance));
106
107 top = debugfs_create_dir(pidstr, vchiq_dbg_clients);
108
109 debugfs_create_file("use_count", 0444, top, instance,
110 &debugfs_usecount_fops);
111 debugfs_create_file("trace", 0644, top, instance, &debugfs_trace_fops);
112
113 vchiq_instance_get_debugfs_node(instance)->dentry = top;
114 }
115
vchiq_debugfs_remove_instance(struct vchiq_instance * instance)116 void vchiq_debugfs_remove_instance(struct vchiq_instance *instance)
117 {
118 struct vchiq_debugfs_node *node =
119 vchiq_instance_get_debugfs_node(instance);
120
121 debugfs_remove_recursive(node->dentry);
122 }
123
vchiq_debugfs_init(struct vchiq_state * state)124 void vchiq_debugfs_init(struct vchiq_state *state)
125 {
126 vchiq_dbg_dir = debugfs_create_dir("vchiq", NULL);
127 vchiq_dbg_clients = debugfs_create_dir("clients", vchiq_dbg_dir);
128
129 debugfs_create_file("state", S_IFREG | 0444, vchiq_dbg_dir, state,
130 &vchiq_dump_fops);
131 }
132
133 /* remove all the debugfs entries */
vchiq_debugfs_deinit(void)134 void vchiq_debugfs_deinit(void)
135 {
136 debugfs_remove_recursive(vchiq_dbg_dir);
137 }
138
139 #else /* CONFIG_DEBUG_FS */
140
vchiq_debugfs_init(struct vchiq_state * state)141 void vchiq_debugfs_init(struct vchiq_state *state)
142 {
143 }
144
vchiq_debugfs_deinit(void)145 void vchiq_debugfs_deinit(void)
146 {
147 }
148
vchiq_debugfs_add_instance(struct vchiq_instance * instance)149 void vchiq_debugfs_add_instance(struct vchiq_instance *instance)
150 {
151 }
152
vchiq_debugfs_remove_instance(struct vchiq_instance * instance)153 void vchiq_debugfs_remove_instance(struct vchiq_instance *instance)
154 {
155 }
156
157 #endif /* CONFIG_DEBUG_FS */
158