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