debugfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. *
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/device.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/list.h>
  10. #include <linux/mhi.h>
  11. #include <linux/module.h>
  12. #include <linux/string_choices.h>
  13. #include "internal.h"
  14. static int mhi_debugfs_states_show(struct seq_file *m, void *d)
  15. {
  16. struct mhi_controller *mhi_cntrl = m->private;
  17. /* states */
  18. seq_printf(m, "PM state: %s Device: %s MHI state: %s EE: %s wake: %s\n",
  19. to_mhi_pm_state_str(mhi_cntrl->pm_state),
  20. mhi_is_active(mhi_cntrl) ? "Active" : "Inactive",
  21. mhi_state_str(mhi_cntrl->dev_state),
  22. TO_MHI_EXEC_STR(mhi_cntrl->ee),
  23. str_true_false(mhi_cntrl->wake_set));
  24. /* counters */
  25. seq_printf(m, "M0: %u M2: %u M3: %u", mhi_cntrl->M0, mhi_cntrl->M2,
  26. mhi_cntrl->M3);
  27. seq_printf(m, " device wake: %u pending packets: %u\n",
  28. atomic_read(&mhi_cntrl->dev_wake),
  29. atomic_read(&mhi_cntrl->pending_pkts));
  30. return 0;
  31. }
  32. static int mhi_debugfs_events_show(struct seq_file *m, void *d)
  33. {
  34. struct mhi_controller *mhi_cntrl = m->private;
  35. struct mhi_event *mhi_event;
  36. struct mhi_event_ctxt *er_ctxt;
  37. int i;
  38. if (!mhi_is_active(mhi_cntrl)) {
  39. seq_puts(m, "Device not ready\n");
  40. return -ENODEV;
  41. }
  42. er_ctxt = mhi_cntrl->mhi_ctxt->er_ctxt;
  43. mhi_event = mhi_cntrl->mhi_event;
  44. for (i = 0; i < mhi_cntrl->total_ev_rings;
  45. i++, er_ctxt++, mhi_event++) {
  46. struct mhi_ring *ring = &mhi_event->ring;
  47. if (mhi_event->offload_ev) {
  48. seq_printf(m, "Index: %d is an offload event ring\n",
  49. i);
  50. continue;
  51. }
  52. seq_printf(m, "Index: %d intmod count: %lu time: %lu",
  53. i, (le32_to_cpu(er_ctxt->intmod) & EV_CTX_INTMODC_MASK) >>
  54. __ffs(EV_CTX_INTMODC_MASK),
  55. (le32_to_cpu(er_ctxt->intmod) & EV_CTX_INTMODT_MASK) >>
  56. __ffs(EV_CTX_INTMODT_MASK));
  57. seq_printf(m, " base: 0x%0llx len: 0x%llx", le64_to_cpu(er_ctxt->rbase),
  58. le64_to_cpu(er_ctxt->rlen));
  59. seq_printf(m, " rp: 0x%llx wp: 0x%llx", le64_to_cpu(er_ctxt->rp),
  60. le64_to_cpu(er_ctxt->wp));
  61. seq_printf(m, " local rp: 0x%pK db: 0x%pad\n", ring->rp,
  62. &mhi_event->db_cfg.db_val);
  63. }
  64. return 0;
  65. }
  66. static int mhi_debugfs_channels_show(struct seq_file *m, void *d)
  67. {
  68. struct mhi_controller *mhi_cntrl = m->private;
  69. struct mhi_chan *mhi_chan;
  70. struct mhi_chan_ctxt *chan_ctxt;
  71. int i;
  72. if (!mhi_is_active(mhi_cntrl)) {
  73. seq_puts(m, "Device not ready\n");
  74. return -ENODEV;
  75. }
  76. mhi_chan = mhi_cntrl->mhi_chan;
  77. chan_ctxt = mhi_cntrl->mhi_ctxt->chan_ctxt;
  78. for (i = 0; i < mhi_cntrl->max_chan; i++, chan_ctxt++, mhi_chan++) {
  79. struct mhi_ring *ring = &mhi_chan->tre_ring;
  80. if (mhi_chan->offload_ch) {
  81. seq_printf(m, "%s(%u) is an offload channel\n",
  82. mhi_chan->name, mhi_chan->chan);
  83. continue;
  84. }
  85. if (!mhi_chan->mhi_dev)
  86. continue;
  87. seq_printf(m,
  88. "%s(%u) state: 0x%lx brstmode: 0x%lx pollcfg: 0x%lx",
  89. mhi_chan->name, mhi_chan->chan, (le32_to_cpu(chan_ctxt->chcfg) &
  90. CHAN_CTX_CHSTATE_MASK) >> __ffs(CHAN_CTX_CHSTATE_MASK),
  91. (le32_to_cpu(chan_ctxt->chcfg) & CHAN_CTX_BRSTMODE_MASK) >>
  92. __ffs(CHAN_CTX_BRSTMODE_MASK), (le32_to_cpu(chan_ctxt->chcfg) &
  93. CHAN_CTX_POLLCFG_MASK) >> __ffs(CHAN_CTX_POLLCFG_MASK));
  94. seq_printf(m, " type: 0x%x event ring: %u", le32_to_cpu(chan_ctxt->chtype),
  95. le32_to_cpu(chan_ctxt->erindex));
  96. seq_printf(m, " base: 0x%llx len: 0x%llx rp: 0x%llx wp: 0x%llx",
  97. le64_to_cpu(chan_ctxt->rbase), le64_to_cpu(chan_ctxt->rlen),
  98. le64_to_cpu(chan_ctxt->rp), le64_to_cpu(chan_ctxt->wp));
  99. seq_printf(m, " local rp: 0x%pK local wp: 0x%pK db: 0x%pad\n",
  100. ring->rp, ring->wp,
  101. &mhi_chan->db_cfg.db_val);
  102. }
  103. return 0;
  104. }
  105. static int mhi_device_info_show(struct device *dev, void *data)
  106. {
  107. struct mhi_device *mhi_dev;
  108. if (dev->bus != &mhi_bus_type)
  109. return 0;
  110. mhi_dev = to_mhi_device(dev);
  111. seq_printf((struct seq_file *)data, "%s: type: %s dev_wake: %u",
  112. mhi_dev->name, mhi_dev->dev_type ? "Controller" : "Transfer",
  113. mhi_dev->dev_wake);
  114. /* for transfer device types only */
  115. if (mhi_dev->dev_type == MHI_DEVICE_XFER)
  116. seq_printf((struct seq_file *)data, " channels: %u(UL)/%u(DL)",
  117. mhi_dev->ul_chan_id, mhi_dev->dl_chan_id);
  118. seq_puts((struct seq_file *)data, "\n");
  119. return 0;
  120. }
  121. static int mhi_debugfs_devices_show(struct seq_file *m, void *d)
  122. {
  123. struct mhi_controller *mhi_cntrl = m->private;
  124. if (!mhi_is_active(mhi_cntrl)) {
  125. seq_puts(m, "Device not ready\n");
  126. return -ENODEV;
  127. }
  128. /* Show controller and client(s) info */
  129. mhi_device_info_show(&mhi_cntrl->mhi_dev->dev, m);
  130. device_for_each_child(&mhi_cntrl->mhi_dev->dev, m, mhi_device_info_show);
  131. return 0;
  132. }
  133. static int mhi_debugfs_regdump_show(struct seq_file *m, void *d)
  134. {
  135. struct mhi_controller *mhi_cntrl = m->private;
  136. enum mhi_state state;
  137. enum mhi_ee_type ee;
  138. int i, ret = -EIO;
  139. u32 val;
  140. void __iomem *mhi_base = mhi_cntrl->regs;
  141. void __iomem *bhi_base = mhi_cntrl->bhi;
  142. void __iomem *bhie_base = mhi_cntrl->bhie;
  143. void __iomem *wake_db = mhi_cntrl->wake_db;
  144. struct {
  145. const char *name;
  146. int offset;
  147. void __iomem *base;
  148. } regs[] = {
  149. { "MHI_REGLEN", MHIREGLEN, mhi_base},
  150. { "MHI_VER", MHIVER, mhi_base},
  151. { "MHI_CFG", MHICFG, mhi_base},
  152. { "MHI_CTRL", MHICTRL, mhi_base},
  153. { "MHI_STATUS", MHISTATUS, mhi_base},
  154. { "MHI_WAKE_DB", 0, wake_db},
  155. { "BHI_EXECENV", BHI_EXECENV, bhi_base},
  156. { "BHI_STATUS", BHI_STATUS, bhi_base},
  157. { "BHI_ERRCODE", BHI_ERRCODE, bhi_base},
  158. { "BHI_ERRDBG1", BHI_ERRDBG1, bhi_base},
  159. { "BHI_ERRDBG2", BHI_ERRDBG2, bhi_base},
  160. { "BHI_ERRDBG3", BHI_ERRDBG3, bhi_base},
  161. { "BHIE_TXVEC_DB", BHIE_TXVECDB_OFFS, bhie_base},
  162. { "BHIE_TXVEC_STATUS", BHIE_TXVECSTATUS_OFFS, bhie_base},
  163. { "BHIE_RXVEC_DB", BHIE_RXVECDB_OFFS, bhie_base},
  164. { "BHIE_RXVEC_STATUS", BHIE_RXVECSTATUS_OFFS, bhie_base},
  165. { NULL },
  166. };
  167. if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state))
  168. return ret;
  169. seq_printf(m, "Host PM state: %s Device state: %s EE: %s\n",
  170. to_mhi_pm_state_str(mhi_cntrl->pm_state),
  171. mhi_state_str(mhi_cntrl->dev_state),
  172. TO_MHI_EXEC_STR(mhi_cntrl->ee));
  173. state = mhi_get_mhi_state(mhi_cntrl);
  174. ee = mhi_get_exec_env(mhi_cntrl);
  175. seq_printf(m, "Device EE: %s state: %s\n", TO_MHI_EXEC_STR(ee),
  176. mhi_state_str(state));
  177. for (i = 0; regs[i].name; i++) {
  178. if (!regs[i].base)
  179. continue;
  180. ret = mhi_read_reg(mhi_cntrl, regs[i].base, regs[i].offset,
  181. &val);
  182. if (ret)
  183. continue;
  184. seq_printf(m, "%s: 0x%x\n", regs[i].name, val);
  185. }
  186. return 0;
  187. }
  188. static int mhi_debugfs_device_wake_show(struct seq_file *m, void *d)
  189. {
  190. struct mhi_controller *mhi_cntrl = m->private;
  191. struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
  192. if (!mhi_is_active(mhi_cntrl)) {
  193. seq_puts(m, "Device not ready\n");
  194. return -ENODEV;
  195. }
  196. seq_printf(m,
  197. "Wake count: %d\n%s\n", mhi_dev->dev_wake,
  198. "Usage: echo get/put > device_wake to vote/unvote for M0");
  199. return 0;
  200. }
  201. static ssize_t mhi_debugfs_device_wake_write(struct file *file,
  202. const char __user *ubuf,
  203. size_t count, loff_t *ppos)
  204. {
  205. struct seq_file *m = file->private_data;
  206. struct mhi_controller *mhi_cntrl = m->private;
  207. struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
  208. char buf[16];
  209. int ret = -EINVAL;
  210. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  211. return -EFAULT;
  212. if (!strncmp(buf, "get", 3)) {
  213. ret = mhi_device_get_sync(mhi_dev);
  214. } else if (!strncmp(buf, "put", 3)) {
  215. mhi_device_put(mhi_dev);
  216. ret = 0;
  217. }
  218. return ret ? ret : count;
  219. }
  220. static int mhi_debugfs_timeout_ms_show(struct seq_file *m, void *d)
  221. {
  222. struct mhi_controller *mhi_cntrl = m->private;
  223. seq_printf(m, "%u ms\n", mhi_cntrl->timeout_ms);
  224. return 0;
  225. }
  226. static ssize_t mhi_debugfs_timeout_ms_write(struct file *file,
  227. const char __user *ubuf,
  228. size_t count, loff_t *ppos)
  229. {
  230. struct seq_file *m = file->private_data;
  231. struct mhi_controller *mhi_cntrl = m->private;
  232. u32 timeout_ms;
  233. if (kstrtou32_from_user(ubuf, count, 0, &timeout_ms))
  234. return -EINVAL;
  235. mhi_cntrl->timeout_ms = timeout_ms;
  236. return count;
  237. }
  238. static int mhi_debugfs_states_open(struct inode *inode, struct file *fp)
  239. {
  240. return single_open(fp, mhi_debugfs_states_show, inode->i_private);
  241. }
  242. static int mhi_debugfs_events_open(struct inode *inode, struct file *fp)
  243. {
  244. return single_open(fp, mhi_debugfs_events_show, inode->i_private);
  245. }
  246. static int mhi_debugfs_channels_open(struct inode *inode, struct file *fp)
  247. {
  248. return single_open(fp, mhi_debugfs_channels_show, inode->i_private);
  249. }
  250. static int mhi_debugfs_devices_open(struct inode *inode, struct file *fp)
  251. {
  252. return single_open(fp, mhi_debugfs_devices_show, inode->i_private);
  253. }
  254. static int mhi_debugfs_regdump_open(struct inode *inode, struct file *fp)
  255. {
  256. return single_open(fp, mhi_debugfs_regdump_show, inode->i_private);
  257. }
  258. static int mhi_debugfs_device_wake_open(struct inode *inode, struct file *fp)
  259. {
  260. return single_open(fp, mhi_debugfs_device_wake_show, inode->i_private);
  261. }
  262. static int mhi_debugfs_timeout_ms_open(struct inode *inode, struct file *fp)
  263. {
  264. return single_open(fp, mhi_debugfs_timeout_ms_show, inode->i_private);
  265. }
  266. static const struct file_operations debugfs_states_fops = {
  267. .open = mhi_debugfs_states_open,
  268. .release = single_release,
  269. .read = seq_read,
  270. };
  271. static const struct file_operations debugfs_events_fops = {
  272. .open = mhi_debugfs_events_open,
  273. .release = single_release,
  274. .read = seq_read,
  275. };
  276. static const struct file_operations debugfs_channels_fops = {
  277. .open = mhi_debugfs_channels_open,
  278. .release = single_release,
  279. .read = seq_read,
  280. };
  281. static const struct file_operations debugfs_devices_fops = {
  282. .open = mhi_debugfs_devices_open,
  283. .release = single_release,
  284. .read = seq_read,
  285. };
  286. static const struct file_operations debugfs_regdump_fops = {
  287. .open = mhi_debugfs_regdump_open,
  288. .release = single_release,
  289. .read = seq_read,
  290. };
  291. static const struct file_operations debugfs_device_wake_fops = {
  292. .open = mhi_debugfs_device_wake_open,
  293. .write = mhi_debugfs_device_wake_write,
  294. .release = single_release,
  295. .read = seq_read,
  296. };
  297. static const struct file_operations debugfs_timeout_ms_fops = {
  298. .open = mhi_debugfs_timeout_ms_open,
  299. .write = mhi_debugfs_timeout_ms_write,
  300. .release = single_release,
  301. .read = seq_read,
  302. };
  303. static struct dentry *mhi_debugfs_root;
  304. void mhi_create_debugfs(struct mhi_controller *mhi_cntrl)
  305. {
  306. mhi_cntrl->debugfs_dentry =
  307. debugfs_create_dir(dev_name(&mhi_cntrl->mhi_dev->dev),
  308. mhi_debugfs_root);
  309. debugfs_create_file("states", 0444, mhi_cntrl->debugfs_dentry,
  310. mhi_cntrl, &debugfs_states_fops);
  311. debugfs_create_file("events", 0444, mhi_cntrl->debugfs_dentry,
  312. mhi_cntrl, &debugfs_events_fops);
  313. debugfs_create_file("channels", 0444, mhi_cntrl->debugfs_dentry,
  314. mhi_cntrl, &debugfs_channels_fops);
  315. debugfs_create_file("devices", 0444, mhi_cntrl->debugfs_dentry,
  316. mhi_cntrl, &debugfs_devices_fops);
  317. debugfs_create_file("regdump", 0444, mhi_cntrl->debugfs_dentry,
  318. mhi_cntrl, &debugfs_regdump_fops);
  319. debugfs_create_file("device_wake", 0644, mhi_cntrl->debugfs_dentry,
  320. mhi_cntrl, &debugfs_device_wake_fops);
  321. debugfs_create_file("timeout_ms", 0644, mhi_cntrl->debugfs_dentry,
  322. mhi_cntrl, &debugfs_timeout_ms_fops);
  323. }
  324. void mhi_destroy_debugfs(struct mhi_controller *mhi_cntrl)
  325. {
  326. debugfs_remove_recursive(mhi_cntrl->debugfs_dentry);
  327. mhi_cntrl->debugfs_dentry = NULL;
  328. }
  329. void mhi_debugfs_init(void)
  330. {
  331. mhi_debugfs_root = debugfs_create_dir(mhi_bus_type.name, NULL);
  332. }
  333. void mhi_debugfs_exit(void)
  334. {
  335. debugfs_remove_recursive(mhi_debugfs_root);
  336. }