vchiq_debugfs.c revision 290245
1/**
2 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The names of the above-listed copyright holders may not be used
15 *    to endorse or promote products derived from this software without
16 *    specific prior written permission.
17 *
18 * ALTERNATIVELY, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2, as published by the Free
20 * Software Foundation.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35
36#include <linux/debugfs.h>
37#include "vchiq_core.h"
38#include "vchiq_arm.h"
39#include "vchiq_debugfs.h"
40
41#ifdef CONFIG_DEBUG_FS
42
43/****************************************************************************
44*
45*   log category entries
46*
47***************************************************************************/
48#define DEBUGFS_WRITE_BUF_SIZE 256
49
50#define VCHIQ_LOG_ERROR_STR   "error"
51#define VCHIQ_LOG_WARNING_STR "warning"
52#define VCHIQ_LOG_INFO_STR    "info"
53#define VCHIQ_LOG_TRACE_STR   "trace"
54
55
56/* Top-level debug info */
57struct vchiq_debugfs_info {
58	/* Global 'vchiq' debugfs entry used by all instances */
59	struct dentry *vchiq_cfg_dir;
60
61	/* one entry per client process */
62	struct dentry *clients;
63
64	/* log categories */
65	struct dentry *log_categories;
66};
67
68static struct vchiq_debugfs_info debugfs_info;
69
70/* Log category debugfs entries */
71struct vchiq_debugfs_log_entry {
72	const char *name;
73	int *plevel;
74	struct dentry *dir;
75};
76
77static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
78	{ "core", &vchiq_core_log_level },
79	{ "msg",  &vchiq_core_msg_log_level },
80	{ "sync", &vchiq_sync_log_level },
81	{ "susp", &vchiq_susp_log_level },
82	{ "arm",  &vchiq_arm_log_level },
83};
84static int n_log_entries =
85	sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
86
87
88static struct dentry *vchiq_clients_top(void);
89static struct dentry *vchiq_debugfs_top(void);
90
91static int debugfs_log_show(struct seq_file *f, void *offset)
92{
93	int *levp = f->private;
94	char *log_value = NULL;
95
96	switch (*levp) {
97	case VCHIQ_LOG_ERROR:
98		log_value = VCHIQ_LOG_ERROR_STR;
99		break;
100	case VCHIQ_LOG_WARNING:
101		log_value = VCHIQ_LOG_WARNING_STR;
102		break;
103	case VCHIQ_LOG_INFO:
104		log_value = VCHIQ_LOG_INFO_STR;
105		break;
106	case VCHIQ_LOG_TRACE:
107		log_value = VCHIQ_LOG_TRACE_STR;
108		break;
109	default:
110		break;
111	}
112
113	seq_printf(f, "%s\n", log_value ? log_value : "(null)");
114
115	return 0;
116}
117
118static int debugfs_log_open(struct inode *inode, struct file *file)
119{
120	return single_open(file, debugfs_log_show, inode->i_private);
121}
122
123static int debugfs_log_write(struct file *file,
124	const char __user *buffer,
125	size_t count, loff_t *ppos)
126{
127	struct seq_file *f = (struct seq_file *)file->private_data;
128	int *levp = f->private;
129	char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
130
131	memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
132	if (count >= DEBUGFS_WRITE_BUF_SIZE)
133		count = DEBUGFS_WRITE_BUF_SIZE;
134
135	if (copy_from_user(kbuf, buffer, count) != 0)
136		return -EFAULT;
137	kbuf[count - 1] = 0;
138
139	if (strncmp("error", kbuf, strlen("error")) == 0)
140		*levp = VCHIQ_LOG_ERROR;
141	else if (strncmp("warning", kbuf, strlen("warning")) == 0)
142		*levp = VCHIQ_LOG_WARNING;
143	else if (strncmp("info", kbuf, strlen("info")) == 0)
144		*levp = VCHIQ_LOG_INFO;
145	else if (strncmp("trace", kbuf, strlen("trace")) == 0)
146		*levp = VCHIQ_LOG_TRACE;
147	else
148		*levp = VCHIQ_LOG_DEFAULT;
149
150	*ppos += count;
151
152	return count;
153}
154
155static const struct file_operations debugfs_log_fops = {
156	.owner		= THIS_MODULE,
157	.open		= debugfs_log_open,
158	.write		= debugfs_log_write,
159	.read		= seq_read,
160	.llseek		= seq_lseek,
161	.release	= single_release,
162};
163
164/* create an entry under <debugfs>/vchiq/log for each log category */
165static int vchiq_debugfs_create_log_entries(struct dentry *top)
166{
167	struct dentry *dir;
168	size_t i;
169	int ret = 0;
170	dir = debugfs_create_dir("log", vchiq_debugfs_top());
171	if (!dir)
172		return -ENOMEM;
173	debugfs_info.log_categories = dir;
174
175	for (i = 0; i < n_log_entries; i++) {
176		void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
177		dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
178					  0644,
179					  debugfs_info.log_categories,
180					  levp,
181					  &debugfs_log_fops);
182		if (!dir) {
183			ret = -ENOMEM;
184			break;
185		}
186
187		vchiq_debugfs_log_entries[i].dir = dir;
188	}
189	return ret;
190}
191
192static int debugfs_usecount_show(struct seq_file *f, void *offset)
193{
194	VCHIQ_INSTANCE_T instance = f->private;
195	int use_count;
196
197	use_count = vchiq_instance_get_use_count(instance);
198	seq_printf(f, "%d\n", use_count);
199
200	return 0;
201}
202
203static int debugfs_usecount_open(struct inode *inode, struct file *file)
204{
205	return single_open(file, debugfs_usecount_show, inode->i_private);
206}
207
208static const struct file_operations debugfs_usecount_fops = {
209	.owner		= THIS_MODULE,
210	.open		= debugfs_usecount_open,
211	.read		= seq_read,
212	.llseek		= seq_lseek,
213	.release	= single_release,
214};
215
216static int debugfs_trace_show(struct seq_file *f, void *offset)
217{
218	VCHIQ_INSTANCE_T instance = f->private;
219	int trace;
220
221	trace = vchiq_instance_get_trace(instance);
222	seq_printf(f, "%s\n", trace ? "Y" : "N");
223
224	return 0;
225}
226
227static int debugfs_trace_open(struct inode *inode, struct file *file)
228{
229	return single_open(file, debugfs_trace_show, inode->i_private);
230}
231
232static int debugfs_trace_write(struct file *file,
233	const char __user *buffer,
234	size_t count, loff_t *ppos)
235{
236	struct seq_file *f = (struct seq_file *)file->private_data;
237	VCHIQ_INSTANCE_T instance = f->private;
238	char firstchar;
239
240	if (copy_from_user(&firstchar, buffer, 1) != 0)
241		return -EFAULT;
242
243	switch (firstchar) {
244	case 'Y':
245	case 'y':
246	case '1':
247		vchiq_instance_set_trace(instance, 1);
248		break;
249	case 'N':
250	case 'n':
251	case '0':
252		vchiq_instance_set_trace(instance, 0);
253		break;
254	default:
255		break;
256	}
257
258	*ppos += count;
259
260	return count;
261}
262
263static const struct file_operations debugfs_trace_fops = {
264	.owner		= THIS_MODULE,
265	.open		= debugfs_trace_open,
266	.write		= debugfs_trace_write,
267	.read		= seq_read,
268	.llseek		= seq_lseek,
269	.release	= single_release,
270};
271
272/* add an instance (process) to the debugfs entries */
273int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
274{
275	char pidstr[16];
276	struct dentry *top, *use_count, *trace;
277	struct dentry *clients = vchiq_clients_top();
278
279	snprintf(pidstr, sizeof(pidstr), "%d",
280		 vchiq_instance_get_pid(instance));
281
282	top = debugfs_create_dir(pidstr, clients);
283	if (!top)
284		goto fail_top;
285
286	use_count = debugfs_create_file("use_count",
287					0444, top,
288					instance,
289					&debugfs_usecount_fops);
290	if (!use_count)
291		goto fail_use_count;
292
293	trace = debugfs_create_file("trace",
294				    0644, top,
295				    instance,
296				    &debugfs_trace_fops);
297	if (!trace)
298		goto fail_trace;
299
300	vchiq_instance_get_debugfs_node(instance)->dentry = top;
301
302	return 0;
303
304fail_trace:
305	debugfs_remove(use_count);
306fail_use_count:
307	debugfs_remove(top);
308fail_top:
309	return -ENOMEM;
310}
311
312void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
313{
314	VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
315	debugfs_remove_recursive(node->dentry);
316}
317
318
319int vchiq_debugfs_init(void)
320{
321	BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
322
323	debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
324	if (debugfs_info.vchiq_cfg_dir == NULL)
325		goto fail;
326
327	debugfs_info.clients = debugfs_create_dir("clients",
328				vchiq_debugfs_top());
329	if (!debugfs_info.clients)
330		goto fail;
331
332	if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
333		goto fail;
334
335	return 0;
336
337fail:
338	vchiq_debugfs_deinit();
339	vchiq_log_error(vchiq_arm_log_level,
340		"%s: failed to create debugfs directory",
341		__func__);
342
343	return -ENOMEM;
344}
345
346/* remove all the debugfs entries */
347void vchiq_debugfs_deinit(void)
348{
349	debugfs_remove_recursive(vchiq_debugfs_top());
350}
351
352static struct dentry *vchiq_clients_top(void)
353{
354	return debugfs_info.clients;
355}
356
357static struct dentry *vchiq_debugfs_top(void)
358{
359	BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
360	return debugfs_info.vchiq_cfg_dir;
361}
362
363#else /* CONFIG_DEBUG_FS */
364
365int vchiq_debugfs_init(void)
366{
367	return 0;
368}
369
370void vchiq_debugfs_deinit(void)
371{
372}
373
374int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
375{
376	return 0;
377}
378
379void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
380{
381}
382
383#endif /* CONFIG_DEBUG_FS */
384