qaic_debugfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020, The Linux Foundation. All rights reserved. */
  3. /* Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved. */
  4. #include <linux/debugfs.h>
  5. #include <linux/device.h>
  6. #include <linux/fs.h>
  7. #include <linux/list.h>
  8. #include <linux/mhi.h>
  9. #include <linux/mutex.h>
  10. #include <linux/overflow.h>
  11. #include <linux/pci.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/sprintf.h>
  14. #include <linux/string.h>
  15. #include <linux/types.h>
  16. #include <linux/workqueue.h>
  17. #include "qaic.h"
  18. #include "qaic_debugfs.h"
  19. #define BOOTLOG_POOL_SIZE 16
  20. #define BOOTLOG_MSG_SIZE 512
  21. #define QAIC_DBC_DIR_NAME 9
  22. struct bootlog_msg {
  23. /* Buffer for bootlog messages */
  24. char str[BOOTLOG_MSG_SIZE];
  25. /* Root struct of device, used to access device resources */
  26. struct qaic_device *qdev;
  27. /* Work struct to schedule work coming on QAIC_LOGGING channel */
  28. struct work_struct work;
  29. };
  30. struct bootlog_page {
  31. /* Node in list of bootlog pages maintained by root device struct */
  32. struct list_head node;
  33. /* Total size of the buffer that holds the bootlogs. It is PAGE_SIZE */
  34. unsigned int size;
  35. /* Offset for the next bootlog */
  36. unsigned int offset;
  37. };
  38. static int bootlog_show(struct seq_file *s, void *unused)
  39. {
  40. struct bootlog_page *page;
  41. struct qaic_device *qdev;
  42. void *page_end;
  43. void *log;
  44. qdev = s->private;
  45. mutex_lock(&qdev->bootlog_mutex);
  46. list_for_each_entry(page, &qdev->bootlog, node) {
  47. log = page + 1;
  48. page_end = (void *)page + page->offset;
  49. while (log < page_end) {
  50. seq_printf(s, "%s", (char *)log);
  51. log += strlen(log) + 1;
  52. }
  53. }
  54. mutex_unlock(&qdev->bootlog_mutex);
  55. return 0;
  56. }
  57. DEFINE_SHOW_ATTRIBUTE(bootlog);
  58. static int fifo_size_show(struct seq_file *s, void *unused)
  59. {
  60. struct dma_bridge_chan *dbc = s->private;
  61. seq_printf(s, "%u\n", dbc->nelem);
  62. return 0;
  63. }
  64. DEFINE_SHOW_ATTRIBUTE(fifo_size);
  65. static int queued_show(struct seq_file *s, void *unused)
  66. {
  67. struct dma_bridge_chan *dbc = s->private;
  68. u32 tail = 0, head = 0;
  69. qaic_data_get_fifo_info(dbc, &head, &tail);
  70. if (head == U32_MAX || tail == U32_MAX)
  71. seq_printf(s, "%u\n", 0);
  72. else if (head > tail)
  73. seq_printf(s, "%u\n", dbc->nelem - head + tail);
  74. else
  75. seq_printf(s, "%u\n", tail - head);
  76. return 0;
  77. }
  78. DEFINE_SHOW_ATTRIBUTE(queued);
  79. void qaic_debugfs_init(struct qaic_drm_device *qddev)
  80. {
  81. struct qaic_device *qdev = qddev->qdev;
  82. struct dentry *debugfs_root;
  83. struct dentry *debugfs_dir;
  84. char name[QAIC_DBC_DIR_NAME];
  85. u32 i;
  86. debugfs_root = to_drm(qddev)->debugfs_root;
  87. debugfs_create_file("bootlog", 0400, debugfs_root, qdev, &bootlog_fops);
  88. /*
  89. * 256 dbcs per device is likely the max we will ever see and lets static checking see a
  90. * reasonable range.
  91. */
  92. for (i = 0; i < qdev->num_dbc && i < 256; ++i) {
  93. snprintf(name, QAIC_DBC_DIR_NAME, "dbc%03u", i);
  94. debugfs_dir = debugfs_create_dir(name, debugfs_root);
  95. debugfs_create_file("fifo_size", 0400, debugfs_dir, &qdev->dbc[i], &fifo_size_fops);
  96. debugfs_create_file("queued", 0400, debugfs_dir, &qdev->dbc[i], &queued_fops);
  97. }
  98. }
  99. static struct bootlog_page *alloc_bootlog_page(struct qaic_device *qdev)
  100. {
  101. struct bootlog_page *page;
  102. page = (struct bootlog_page *)devm_get_free_pages(&qdev->pdev->dev, GFP_KERNEL, 0);
  103. if (!page)
  104. return page;
  105. page->size = PAGE_SIZE;
  106. page->offset = sizeof(*page);
  107. list_add_tail(&page->node, &qdev->bootlog);
  108. return page;
  109. }
  110. static int reset_bootlog(struct qaic_device *qdev)
  111. {
  112. struct bootlog_page *page;
  113. struct bootlog_page *i;
  114. mutex_lock(&qdev->bootlog_mutex);
  115. list_for_each_entry_safe(page, i, &qdev->bootlog, node) {
  116. list_del(&page->node);
  117. devm_free_pages(&qdev->pdev->dev, (unsigned long)page);
  118. }
  119. page = alloc_bootlog_page(qdev);
  120. mutex_unlock(&qdev->bootlog_mutex);
  121. if (!page)
  122. return -ENOMEM;
  123. return 0;
  124. }
  125. static void *bootlog_get_space(struct qaic_device *qdev, unsigned int size)
  126. {
  127. struct bootlog_page *page;
  128. page = list_last_entry(&qdev->bootlog, struct bootlog_page, node);
  129. if (size_add(size, sizeof(*page)) > page->size)
  130. return NULL;
  131. if (page->offset + size > page->size) {
  132. page = alloc_bootlog_page(qdev);
  133. if (!page)
  134. return NULL;
  135. }
  136. return (void *)page + page->offset;
  137. }
  138. static void bootlog_commit(struct qaic_device *qdev, unsigned int size)
  139. {
  140. struct bootlog_page *page;
  141. page = list_last_entry(&qdev->bootlog, struct bootlog_page, node);
  142. page->offset += size;
  143. }
  144. static void bootlog_log(struct work_struct *work)
  145. {
  146. struct bootlog_msg *msg = container_of(work, struct bootlog_msg, work);
  147. unsigned int len = strlen(msg->str) + 1;
  148. struct qaic_device *qdev = msg->qdev;
  149. void *log;
  150. mutex_lock(&qdev->bootlog_mutex);
  151. log = bootlog_get_space(qdev, len);
  152. if (log) {
  153. memcpy(log, msg, len);
  154. bootlog_commit(qdev, len);
  155. }
  156. mutex_unlock(&qdev->bootlog_mutex);
  157. if (mhi_queue_buf(qdev->bootlog_ch, DMA_FROM_DEVICE, msg, BOOTLOG_MSG_SIZE, MHI_EOT))
  158. devm_kfree(&qdev->pdev->dev, msg);
  159. }
  160. static int qaic_bootlog_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
  161. {
  162. struct qaic_device *qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev));
  163. struct bootlog_msg *msg;
  164. int i, ret;
  165. qdev->bootlog_wq = alloc_ordered_workqueue("qaic_bootlog", 0);
  166. if (!qdev->bootlog_wq) {
  167. ret = -ENOMEM;
  168. goto out;
  169. }
  170. ret = reset_bootlog(qdev);
  171. if (ret)
  172. goto destroy_workqueue;
  173. ret = mhi_prepare_for_transfer(mhi_dev);
  174. if (ret)
  175. goto destroy_workqueue;
  176. dev_set_drvdata(&mhi_dev->dev, qdev);
  177. qdev->bootlog_ch = mhi_dev;
  178. for (i = 0; i < BOOTLOG_POOL_SIZE; i++) {
  179. msg = devm_kzalloc(&qdev->pdev->dev, sizeof(*msg), GFP_KERNEL);
  180. if (!msg) {
  181. ret = -ENOMEM;
  182. goto mhi_unprepare;
  183. }
  184. msg->qdev = qdev;
  185. INIT_WORK(&msg->work, bootlog_log);
  186. ret = mhi_queue_buf(mhi_dev, DMA_FROM_DEVICE, msg, BOOTLOG_MSG_SIZE, MHI_EOT);
  187. if (ret)
  188. goto mhi_unprepare;
  189. }
  190. return 0;
  191. mhi_unprepare:
  192. mhi_unprepare_from_transfer(mhi_dev);
  193. destroy_workqueue:
  194. destroy_workqueue(qdev->bootlog_wq);
  195. out:
  196. return ret;
  197. }
  198. static void qaic_bootlog_mhi_remove(struct mhi_device *mhi_dev)
  199. {
  200. struct qaic_device *qdev;
  201. qdev = dev_get_drvdata(&mhi_dev->dev);
  202. mhi_unprepare_from_transfer(qdev->bootlog_ch);
  203. destroy_workqueue(qdev->bootlog_wq);
  204. qdev->bootlog_ch = NULL;
  205. }
  206. static void qaic_bootlog_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  207. {
  208. }
  209. static void qaic_bootlog_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  210. {
  211. struct qaic_device *qdev = dev_get_drvdata(&mhi_dev->dev);
  212. struct bootlog_msg *msg = mhi_result->buf_addr;
  213. if (mhi_result->transaction_status) {
  214. devm_kfree(&qdev->pdev->dev, msg);
  215. return;
  216. }
  217. /* Force a null at the end of the transferred string */
  218. msg->str[mhi_result->bytes_xferd - 1] = 0;
  219. queue_work(qdev->bootlog_wq, &msg->work);
  220. }
  221. static const struct mhi_device_id qaic_bootlog_mhi_match_table[] = {
  222. { .chan = "QAIC_LOGGING", },
  223. {},
  224. };
  225. static struct mhi_driver qaic_bootlog_mhi_driver = {
  226. .id_table = qaic_bootlog_mhi_match_table,
  227. .remove = qaic_bootlog_mhi_remove,
  228. .probe = qaic_bootlog_mhi_probe,
  229. .ul_xfer_cb = qaic_bootlog_mhi_ul_xfer_cb,
  230. .dl_xfer_cb = qaic_bootlog_mhi_dl_xfer_cb,
  231. .driver = {
  232. .name = "qaic_bootlog",
  233. },
  234. };
  235. int qaic_bootlog_register(void)
  236. {
  237. return mhi_driver_register(&qaic_bootlog_mhi_driver);
  238. }
  239. void qaic_bootlog_unregister(void)
  240. {
  241. mhi_driver_unregister(&qaic_bootlog_mhi_driver);
  242. }