debugfs.c 11 KB

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