remoteproc_debugfs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Remote Processor Framework
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Mark Grosen <mgrosen@ti.com>
  9. * Brian Swetland <swetland@google.com>
  10. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11. * Suman Anna <s-anna@ti.com>
  12. * Robert Tivy <rtivy@ti.com>
  13. * Armando Uribe De Leon <x0095078@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #define pr_fmt(fmt) "%s: " fmt, __func__
  25. #include <linux/kernel.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/remoteproc.h>
  28. #include <linux/device.h>
  29. /* remoteproc debugfs parent dir */
  30. static struct dentry *rproc_dbg;
  31. /*
  32. * Some remote processors may support dumping trace logs into a shared
  33. * memory buffer. We expose this trace buffer using debugfs, so users
  34. * can easily tell what's going on remotely.
  35. *
  36. * We will most probably improve the rproc tracing facilities later on,
  37. * but this kind of lightweight and simple mechanism is always good to have,
  38. * as it provides very early tracing with little to no dependencies at all.
  39. */
  40. static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
  41. size_t count, loff_t *ppos)
  42. {
  43. struct rproc_mem_entry *trace = filp->private_data;
  44. int len = strnlen(trace->va, trace->len);
  45. return simple_read_from_buffer(userbuf, count, ppos, trace->va, len);
  46. }
  47. static int rproc_open_generic(struct inode *inode, struct file *file)
  48. {
  49. file->private_data = inode->i_private;
  50. return 0;
  51. }
  52. static const struct file_operations trace_rproc_ops = {
  53. .read = rproc_trace_read,
  54. .open = rproc_open_generic,
  55. .llseek = generic_file_llseek,
  56. };
  57. /*
  58. * A state-to-string lookup table, for exposing a human readable state
  59. * via debugfs. Always keep in sync with enum rproc_state
  60. */
  61. static const char * const rproc_state_string[] = {
  62. "offline",
  63. "suspended",
  64. "running",
  65. "crashed",
  66. "invalid",
  67. };
  68. /* expose the state of the remote processor via debugfs */
  69. static ssize_t rproc_state_read(struct file *filp, char __user *userbuf,
  70. size_t count, loff_t *ppos)
  71. {
  72. struct rproc *rproc = filp->private_data;
  73. unsigned int state;
  74. char buf[30];
  75. int i;
  76. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  77. i = snprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state],
  78. rproc->state);
  79. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  80. }
  81. static const struct file_operations rproc_state_ops = {
  82. .read = rproc_state_read,
  83. .open = rproc_open_generic,
  84. .llseek = generic_file_llseek,
  85. };
  86. /* expose the name of the remote processor via debugfs */
  87. static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
  88. size_t count, loff_t *ppos)
  89. {
  90. struct rproc *rproc = filp->private_data;
  91. /* need room for the name, a newline and a terminating null */
  92. char buf[100];
  93. int i;
  94. i = snprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
  95. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  96. }
  97. static const struct file_operations rproc_name_ops = {
  98. .read = rproc_name_read,
  99. .open = rproc_open_generic,
  100. .llseek = generic_file_llseek,
  101. };
  102. void rproc_remove_trace_file(struct dentry *tfile)
  103. {
  104. debugfs_remove(tfile);
  105. }
  106. struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
  107. struct rproc_mem_entry *trace)
  108. {
  109. struct dentry *tfile;
  110. tfile = debugfs_create_file(name, 0400, rproc->dbg_dir,
  111. trace, &trace_rproc_ops);
  112. if (!tfile) {
  113. dev_err(rproc->dev, "failed to create debugfs trace entry\n");
  114. return NULL;
  115. }
  116. return tfile;
  117. }
  118. void rproc_delete_debug_dir(struct rproc *rproc)
  119. {
  120. if (!rproc->dbg_dir)
  121. return;
  122. debugfs_remove_recursive(rproc->dbg_dir);
  123. }
  124. void rproc_create_debug_dir(struct rproc *rproc)
  125. {
  126. struct device *dev = rproc->dev;
  127. if (!rproc_dbg)
  128. return;
  129. rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
  130. if (!rproc->dbg_dir)
  131. return;
  132. debugfs_create_file("name", 0400, rproc->dbg_dir,
  133. rproc, &rproc_name_ops);
  134. debugfs_create_file("state", 0400, rproc->dbg_dir,
  135. rproc, &rproc_state_ops);
  136. }
  137. void __init rproc_init_debugfs(void)
  138. {
  139. if (debugfs_initialized()) {
  140. rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  141. if (!rproc_dbg)
  142. pr_err("can't create debugfs dir\n");
  143. }
  144. }
  145. void __exit rproc_exit_debugfs(void)
  146. {
  147. if (rproc_dbg)
  148. debugfs_remove(rproc_dbg);
  149. }