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 */ 57 struct 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 68 static struct vchiq_debugfs_info debugfs_info; 69 70 /* Log category debugfs entries */ 71 struct vchiq_debugfs_log_entry { 72 const char *name; 73 int *plevel; 74 struct dentry *dir; 75 }; 76 77 static 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 }; 84 static int n_log_entries = ARRAY_SIZE(vchiq_debugfs_log_entries); 85 86 static struct dentry *vchiq_clients_top(void); 87 static struct dentry *vchiq_debugfs_top(void); 88 89 static int debugfs_log_show(struct seq_file *f, void *offset) 90 { 91 int *levp = f->private; 92 char *log_value = NULL; 93 94 switch (*levp) { 95 case VCHIQ_LOG_ERROR: 96 log_value = VCHIQ_LOG_ERROR_STR; 97 break; 98 case VCHIQ_LOG_WARNING: 99 log_value = VCHIQ_LOG_WARNING_STR; 100 break; 101 case VCHIQ_LOG_INFO: 102 log_value = VCHIQ_LOG_INFO_STR; 103 break; 104 case VCHIQ_LOG_TRACE: 105 log_value = VCHIQ_LOG_TRACE_STR; 106 break; 107 default: 108 break; 109 } 110 111 seq_printf(f, "%s\n", log_value ? log_value : "(null)"); 112 113 return 0; 114 } 115 116 static int debugfs_log_open(struct inode *inode, struct file *file) 117 { 118 return single_open(file, debugfs_log_show, inode->i_private); 119 } 120 121 static ssize_t debugfs_log_write(struct file *file, 122 const char __user *buffer, 123 size_t count, loff_t *ppos) 124 { 125 struct seq_file *f = (struct seq_file *)file->private_data; 126 int *levp = f->private; 127 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1]; 128 129 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1); 130 if (count >= DEBUGFS_WRITE_BUF_SIZE) 131 count = DEBUGFS_WRITE_BUF_SIZE; 132 133 if (copy_from_user(kbuf, buffer, count) != 0) 134 return -EFAULT; 135 kbuf[count - 1] = 0; 136 137 if (strncmp("error", kbuf, strlen("error")) == 0) 138 *levp = VCHIQ_LOG_ERROR; 139 else if (strncmp("warning", kbuf, strlen("warning")) == 0) 140 *levp = VCHIQ_LOG_WARNING; 141 else if (strncmp("info", kbuf, strlen("info")) == 0) 142 *levp = VCHIQ_LOG_INFO; 143 else if (strncmp("trace", kbuf, strlen("trace")) == 0) 144 *levp = VCHIQ_LOG_TRACE; 145 else 146 *levp = VCHIQ_LOG_DEFAULT; 147 148 *ppos += count; 149 150 return count; 151 } 152 153 static const struct file_operations debugfs_log_fops = { 154 .owner = THIS_MODULE, 155 .open = debugfs_log_open, 156 .write = debugfs_log_write, 157 .read = seq_read, 158 .llseek = seq_lseek, 159 .release = single_release, 160 }; 161 162 /* create an entry under <debugfs>/vchiq/log for each log category */ 163 static int vchiq_debugfs_create_log_entries(struct dentry *top) 164 { 165 struct dentry *dir; 166 size_t i; 167 int ret = 0; 168 169 dir = debugfs_create_dir("log", vchiq_debugfs_top()); 170 if (!dir) 171 return -ENOMEM; 172 debugfs_info.log_categories = dir; 173 174 for (i = 0; i < n_log_entries; i++) { 175 void *levp = (void *)vchiq_debugfs_log_entries[i].plevel; 176 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 192 static 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 203 static int debugfs_usecount_open(struct inode *inode, struct file *file) 204 { 205 return single_open(file, debugfs_usecount_show, inode->i_private); 206 } 207 208 static 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 216 static 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 227 static int debugfs_trace_open(struct inode *inode, struct file *file) 228 { 229 return single_open(file, debugfs_trace_show, inode->i_private); 230 } 231 232 static ssize_t 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 263 static 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 */ 273 int 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 304 fail_trace: 305 debugfs_remove(use_count); 306 fail_use_count: 307 debugfs_remove(top); 308 fail_top: 309 return -ENOMEM; 310 } 311 312 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance) 313 { 314 VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance); 315 316 debugfs_remove_recursive(node->dentry); 317 } 318 319 320 int vchiq_debugfs_init(void) 321 { 322 BUG_ON(debugfs_info.vchiq_cfg_dir != NULL); 323 324 debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL); 325 if (debugfs_info.vchiq_cfg_dir == NULL) 326 goto fail; 327 328 debugfs_info.clients = debugfs_create_dir("clients", 329 vchiq_debugfs_top()); 330 if (!debugfs_info.clients) 331 goto fail; 332 333 if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0) 334 goto fail; 335 336 return 0; 337 338 fail: 339 vchiq_debugfs_deinit(); 340 vchiq_log_error(vchiq_arm_log_level, 341 "%s: failed to create debugfs directory", 342 __func__); 343 344 return -ENOMEM; 345 } 346 347 /* remove all the debugfs entries */ 348 void vchiq_debugfs_deinit(void) 349 { 350 debugfs_remove_recursive(vchiq_debugfs_top()); 351 } 352 353 static struct dentry *vchiq_clients_top(void) 354 { 355 return debugfs_info.clients; 356 } 357 358 static struct dentry *vchiq_debugfs_top(void) 359 { 360 BUG_ON(debugfs_info.vchiq_cfg_dir == NULL); 361 return debugfs_info.vchiq_cfg_dir; 362 } 363 364 #else /* CONFIG_DEBUG_FS */ 365 366 int vchiq_debugfs_init(void) 367 { 368 return 0; 369 } 370 371 void vchiq_debugfs_deinit(void) 372 { 373 } 374 375 int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance) 376 { 377 return 0; 378 } 379 380 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance) 381 { 382 } 383 384 #endif /* CONFIG_DEBUG_FS */ 385