remoteproc_debugfs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 const struct file_operations trace_rproc_ops = {
  48. .read = rproc_trace_read,
  49. .open = simple_open,
  50. .llseek = generic_file_llseek,
  51. };
  52. /*
  53. * A state-to-string lookup table, for exposing a human readable state
  54. * via debugfs. Always keep in sync with enum rproc_state
  55. */
  56. static const char * const rproc_state_string[] = {
  57. "offline",
  58. "suspended",
  59. "running",
  60. "crashed",
  61. "invalid",
  62. };
  63. /* expose the state of the remote processor via debugfs */
  64. static ssize_t rproc_state_read(struct file *filp, char __user *userbuf,
  65. size_t count, loff_t *ppos)
  66. {
  67. struct rproc *rproc = filp->private_data;
  68. unsigned int state;
  69. char buf[30];
  70. int i;
  71. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  72. i = snprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state],
  73. rproc->state);
  74. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  75. }
  76. static const struct file_operations rproc_state_ops = {
  77. .read = rproc_state_read,
  78. .open = simple_open,
  79. .llseek = generic_file_llseek,
  80. };
  81. /* expose the name of the remote processor via debugfs */
  82. static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
  83. size_t count, loff_t *ppos)
  84. {
  85. struct rproc *rproc = filp->private_data;
  86. /* need room for the name, a newline and a terminating null */
  87. char buf[100];
  88. int i;
  89. i = snprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
  90. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  91. }
  92. static const struct file_operations rproc_name_ops = {
  93. .read = rproc_name_read,
  94. .open = simple_open,
  95. .llseek = generic_file_llseek,
  96. };
  97. void rproc_remove_trace_file(struct dentry *tfile)
  98. {
  99. debugfs_remove(tfile);
  100. }
  101. struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
  102. struct rproc_mem_entry *trace)
  103. {
  104. struct dentry *tfile;
  105. tfile = debugfs_create_file(name, 0400, rproc->dbg_dir,
  106. trace, &trace_rproc_ops);
  107. if (!tfile) {
  108. dev_err(rproc->dev, "failed to create debugfs trace entry\n");
  109. return NULL;
  110. }
  111. return tfile;
  112. }
  113. void rproc_delete_debug_dir(struct rproc *rproc)
  114. {
  115. if (!rproc->dbg_dir)
  116. return;
  117. debugfs_remove_recursive(rproc->dbg_dir);
  118. }
  119. void rproc_create_debug_dir(struct rproc *rproc)
  120. {
  121. struct device *dev = rproc->dev;
  122. if (!rproc_dbg)
  123. return;
  124. rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
  125. if (!rproc->dbg_dir)
  126. return;
  127. debugfs_create_file("name", 0400, rproc->dbg_dir,
  128. rproc, &rproc_name_ops);
  129. debugfs_create_file("state", 0400, rproc->dbg_dir,
  130. rproc, &rproc_state_ops);
  131. }
  132. void __init rproc_init_debugfs(void)
  133. {
  134. if (debugfs_initialized()) {
  135. rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  136. if (!rproc_dbg)
  137. pr_err("can't create debugfs dir\n");
  138. }
  139. }
  140. void __exit rproc_exit_debugfs(void)
  141. {
  142. if (rproc_dbg)
  143. debugfs_remove(rproc_dbg);
  144. }