debugfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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, M3_fast: %u", mhi_cntrl->M0,
  25. mhi_cntrl->M2, mhi_cntrl->M3, mhi_cntrl->M3_fast);
  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%llx db: 0x%pad\n",
  61. (u64)mhi_to_physical(ring, 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%llx local wp: 0x%llx db: 0x%pad\n",
  100. (u64)mhi_to_physical(ring, ring->rp),
  101. (u64)mhi_to_physical(ring, ring->wp),
  102. &mhi_chan->db_cfg.db_val);
  103. }
  104. return 0;
  105. }
  106. static int mhi_device_info_show(struct device *dev, void *data)
  107. {
  108. struct mhi_device *mhi_dev;
  109. if (dev->bus != &mhi_bus_type)
  110. return 0;
  111. mhi_dev = to_mhi_device(dev);
  112. seq_printf((struct seq_file *)data, "%s: type: %s dev_wake: %u",
  113. mhi_dev->name, mhi_dev->dev_type ? "Controller" : "Transfer",
  114. mhi_dev->dev_wake);
  115. /* for transfer device types only */
  116. if (mhi_dev->dev_type == MHI_DEVICE_XFER)
  117. seq_printf((struct seq_file *)data, " channels: %u(UL)/%u(DL)",
  118. mhi_dev->ul_chan_id, mhi_dev->dl_chan_id);
  119. seq_puts((struct seq_file *)data, "\n");
  120. return 0;
  121. }
  122. static int mhi_debugfs_devices_show(struct seq_file *m, void *d)
  123. {
  124. struct mhi_controller *mhi_cntrl = m->private;
  125. if (!mhi_is_active(mhi_cntrl)) {
  126. seq_puts(m, "Device not ready\n");
  127. return -ENODEV;
  128. }
  129. /* Show controller and client(s) info */
  130. mhi_device_info_show(&mhi_cntrl->mhi_dev->dev, m);
  131. device_for_each_child(&mhi_cntrl->mhi_dev->dev, m, mhi_device_info_show);
  132. return 0;
  133. }
  134. static int mhi_debugfs_regdump_show(struct seq_file *m, void *d)
  135. {
  136. struct mhi_controller *mhi_cntrl = m->private;
  137. enum mhi_state state;
  138. enum mhi_ee_type ee;
  139. int i, ret = -EIO;
  140. u32 val;
  141. void __iomem *mhi_base = mhi_cntrl->regs;
  142. void __iomem *bhi_base = mhi_cntrl->bhi;
  143. void __iomem *bhie_base = mhi_cntrl->bhie;
  144. void __iomem *wake_db = mhi_cntrl->wake_db;
  145. struct {
  146. const char *name;
  147. int offset;
  148. void __iomem *base;
  149. } regs[] = {
  150. { "MHI_REGLEN", MHIREGLEN, mhi_base},
  151. { "MHI_VER", MHIVER, mhi_base},
  152. { "MHI_CFG", MHICFG, mhi_base},
  153. { "MHI_CTRL", MHICTRL, mhi_base},
  154. { "MHI_STATUS", MHISTATUS, mhi_base},
  155. { "MHI_WAKE_DB", 0, wake_db},
  156. { "BHI_EXECENV", BHI_EXECENV, bhi_base},
  157. { "BHI_STATUS", BHI_STATUS, bhi_base},
  158. { "BHI_ERRCODE", BHI_ERRCODE, bhi_base},
  159. { "BHI_ERRDBG1", BHI_ERRDBG1, bhi_base},
  160. { "BHI_ERRDBG2", BHI_ERRDBG2, bhi_base},
  161. { "BHI_ERRDBG3", BHI_ERRDBG3, bhi_base},
  162. { "BHIE_TXVEC_DB", BHIE_TXVECDB_OFFS, bhie_base},
  163. { "BHIE_TXVEC_STATUS", BHIE_TXVECSTATUS_OFFS, bhie_base},
  164. { "BHIE_RXVEC_DB", BHIE_RXVECDB_OFFS, bhie_base},
  165. { "BHIE_RXVEC_STATUS", BHIE_RXVECSTATUS_OFFS, bhie_base},
  166. { NULL },
  167. };
  168. if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state))
  169. return ret;
  170. seq_printf(m, "Host PM state: %s Device state: %s EE: %s\n",
  171. to_mhi_pm_state_str(mhi_cntrl->pm_state),
  172. mhi_state_str(mhi_cntrl->dev_state),
  173. TO_MHI_EXEC_STR(mhi_cntrl->ee));
  174. state = mhi_get_mhi_state(mhi_cntrl);
  175. ee = mhi_get_exec_env(mhi_cntrl);
  176. seq_printf(m, "Device EE: %s state: %s\n", TO_MHI_EXEC_STR(ee),
  177. mhi_state_str(state));
  178. for (i = 0; regs[i].name; i++) {
  179. if (!regs[i].base)
  180. continue;
  181. ret = mhi_read_reg(mhi_cntrl, regs[i].base, regs[i].offset,
  182. &val);
  183. if (ret)
  184. continue;
  185. seq_printf(m, "%s: 0x%x\n", regs[i].name, val);
  186. }
  187. return 0;
  188. }
  189. static int mhi_debugfs_device_wake_show(struct seq_file *m, void *d)
  190. {
  191. struct mhi_controller *mhi_cntrl = m->private;
  192. struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
  193. if (!mhi_is_active(mhi_cntrl)) {
  194. seq_puts(m, "Device not ready\n");
  195. return -ENODEV;
  196. }
  197. seq_printf(m,
  198. "Wake count: %d\n%s\n", mhi_dev->dev_wake,
  199. "Usage: echo get/put > device_wake to vote/unvote for M0");
  200. return 0;
  201. }
  202. static ssize_t mhi_debugfs_device_wake_write(struct file *file,
  203. const char __user *ubuf,
  204. size_t count, loff_t *ppos)
  205. {
  206. struct seq_file *m = file->private_data;
  207. struct mhi_controller *mhi_cntrl = m->private;
  208. struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
  209. char buf[16];
  210. int ret = -EINVAL;
  211. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  212. return -EFAULT;
  213. if (!strncmp(buf, "get", 3)) {
  214. ret = mhi_device_get_sync(mhi_dev);
  215. } else if (!strncmp(buf, "put", 3)) {
  216. mhi_device_put(mhi_dev);
  217. ret = 0;
  218. }
  219. return ret ? ret : count;
  220. }
  221. static int mhi_debugfs_timeout_ms_show(struct seq_file *m, void *d)
  222. {
  223. struct mhi_controller *mhi_cntrl = m->private;
  224. seq_printf(m, "%u ms\n", mhi_cntrl->timeout_ms);
  225. return 0;
  226. }
  227. static ssize_t mhi_debugfs_timeout_ms_write(struct file *file,
  228. const char __user *ubuf,
  229. size_t count, loff_t *ppos)
  230. {
  231. struct seq_file *m = file->private_data;
  232. struct mhi_controller *mhi_cntrl = m->private;
  233. u32 timeout_ms;
  234. if (kstrtou32_from_user(ubuf, count, 0, &timeout_ms))
  235. return -EINVAL;
  236. mhi_cntrl->timeout_ms = timeout_ms;
  237. return count;
  238. }
  239. static int mhi_debugfs_states_open(struct inode *inode, struct file *fp)
  240. {
  241. return single_open(fp, mhi_debugfs_states_show, inode->i_private);
  242. }
  243. static int mhi_debugfs_events_open(struct inode *inode, struct file *fp)
  244. {
  245. return single_open(fp, mhi_debugfs_events_show, inode->i_private);
  246. }
  247. static int mhi_debugfs_channels_open(struct inode *inode, struct file *fp)
  248. {
  249. return single_open(fp, mhi_debugfs_channels_show, inode->i_private);
  250. }
  251. static int mhi_debugfs_devices_open(struct inode *inode, struct file *fp)
  252. {
  253. return single_open(fp, mhi_debugfs_devices_show, inode->i_private);
  254. }
  255. static int mhi_debugfs_regdump_open(struct inode *inode, struct file *fp)
  256. {
  257. return single_open(fp, mhi_debugfs_regdump_show, inode->i_private);
  258. }
  259. static int mhi_debugfs_device_wake_open(struct inode *inode, struct file *fp)
  260. {
  261. return single_open(fp, mhi_debugfs_device_wake_show, inode->i_private);
  262. }
  263. static int mhi_debugfs_timeout_ms_open(struct inode *inode, struct file *fp)
  264. {
  265. return single_open(fp, mhi_debugfs_timeout_ms_show, inode->i_private);
  266. }
  267. static const struct file_operations debugfs_states_fops = {
  268. .open = mhi_debugfs_states_open,
  269. .release = single_release,
  270. .read = seq_read,
  271. };
  272. static const struct file_operations debugfs_events_fops = {
  273. .open = mhi_debugfs_events_open,
  274. .release = single_release,
  275. .read = seq_read,
  276. };
  277. static const struct file_operations debugfs_channels_fops = {
  278. .open = mhi_debugfs_channels_open,
  279. .release = single_release,
  280. .read = seq_read,
  281. };
  282. static const struct file_operations debugfs_devices_fops = {
  283. .open = mhi_debugfs_devices_open,
  284. .release = single_release,
  285. .read = seq_read,
  286. };
  287. static const struct file_operations debugfs_regdump_fops = {
  288. .open = mhi_debugfs_regdump_open,
  289. .release = single_release,
  290. .read = seq_read,
  291. };
  292. static const struct file_operations debugfs_device_wake_fops = {
  293. .open = mhi_debugfs_device_wake_open,
  294. .write = mhi_debugfs_device_wake_write,
  295. .release = single_release,
  296. .read = seq_read,
  297. };
  298. static const struct file_operations debugfs_timeout_ms_fops = {
  299. .open = mhi_debugfs_timeout_ms_open,
  300. .write = mhi_debugfs_timeout_ms_write,
  301. .release = single_release,
  302. .read = seq_read,
  303. };
  304. static struct dentry *mhi_debugfs_root;
  305. void mhi_create_debugfs(struct mhi_controller *mhi_cntrl)
  306. {
  307. mhi_cntrl->debugfs_dentry =
  308. debugfs_create_dir(dev_name(&mhi_cntrl->mhi_dev->dev),
  309. mhi_debugfs_root);
  310. debugfs_create_file("states", 0444, mhi_cntrl->debugfs_dentry,
  311. mhi_cntrl, &debugfs_states_fops);
  312. debugfs_create_file("events", 0444, mhi_cntrl->debugfs_dentry,
  313. mhi_cntrl, &debugfs_events_fops);
  314. debugfs_create_file("channels", 0444, mhi_cntrl->debugfs_dentry,
  315. mhi_cntrl, &debugfs_channels_fops);
  316. debugfs_create_file("devices", 0444, mhi_cntrl->debugfs_dentry,
  317. mhi_cntrl, &debugfs_devices_fops);
  318. debugfs_create_file("regdump", 0444, mhi_cntrl->debugfs_dentry,
  319. mhi_cntrl, &debugfs_regdump_fops);
  320. debugfs_create_file("device_wake", 0644, mhi_cntrl->debugfs_dentry,
  321. mhi_cntrl, &debugfs_device_wake_fops);
  322. debugfs_create_file("timeout_ms", 0644, mhi_cntrl->debugfs_dentry,
  323. mhi_cntrl, &debugfs_timeout_ms_fops);
  324. }
  325. void mhi_destroy_debugfs(struct mhi_controller *mhi_cntrl)
  326. {
  327. debugfs_remove_recursive(mhi_cntrl->debugfs_dentry);
  328. mhi_cntrl->debugfs_dentry = NULL;
  329. }
  330. void mhi_debugfs_init(void)
  331. {
  332. mhi_debugfs_root = debugfs_create_dir(mhi_bus_type.name, NULL);
  333. }
  334. void mhi_debugfs_exit(void)
  335. {
  336. debugfs_remove_recursive(mhi_debugfs_root);
  337. }