1 // SPDX-License-Identifier: GPL-2.0-only
3 /* Copyright (c) 2020, The Linux Foundation. All rights reserved. */
4 /* Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved. */
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
9 #include <linux/list.h>
10 #include <linux/mhi.h>
11 #include <linux/mutex.h>
12 #include <linux/overflow.h>
13 #include <linux/pci.h>
14 #include <linux/seq_file.h>
15 #include <linux/sprintf.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
21 #include "qaic_debugfs.h"
23 #define BOOTLOG_POOL_SIZE 16
24 #define BOOTLOG_MSG_SIZE 512
25 #define QAIC_DBC_DIR_NAME 9
28 /* Buffer for bootlog messages */
29 char str[BOOTLOG_MSG_SIZE];
30 /* Root struct of device, used to access device resources */
31 struct qaic_device *qdev;
32 /* Work struct to schedule work coming on QAIC_LOGGING channel */
33 struct work_struct work;
37 /* Node in list of bootlog pages maintained by root device struct */
38 struct list_head node;
39 /* Total size of the buffer that holds the bootlogs. It is PAGE_SIZE */
41 /* Offset for the next bootlog */
45 static int bootlog_show(struct seq_file *s, void *unused)
47 struct bootlog_page *page;
48 struct qaic_device *qdev;
53 mutex_lock(&qdev->bootlog_mutex);
54 list_for_each_entry(page, &qdev->bootlog, node) {
56 page_end = (void *)page + page->offset;
57 while (log < page_end) {
58 seq_printf(s, "%s", (char *)log);
59 log += strlen(log) + 1;
62 mutex_unlock(&qdev->bootlog_mutex);
67 static int bootlog_fops_open(struct inode *inode, struct file *file)
69 return single_open(file, bootlog_show, inode->i_private);
72 static const struct file_operations bootlog_fops = {
74 .open = bootlog_fops_open,
77 .release = single_release,
80 static int read_dbc_fifo_size(struct seq_file *s, void *unused)
82 struct dma_bridge_chan *dbc = s->private;
84 seq_printf(s, "%u\n", dbc->nelem);
88 static int fifo_size_open(struct inode *inode, struct file *file)
90 return single_open(file, read_dbc_fifo_size, inode->i_private);
93 static const struct file_operations fifo_size_fops = {
95 .open = fifo_size_open,
98 .release = single_release,
101 static int read_dbc_queued(struct seq_file *s, void *unused)
103 struct dma_bridge_chan *dbc = s->private;
104 u32 tail = 0, head = 0;
106 qaic_data_get_fifo_info(dbc, &head, &tail);
108 if (head == U32_MAX || tail == U32_MAX)
109 seq_printf(s, "%u\n", 0);
110 else if (head > tail)
111 seq_printf(s, "%u\n", dbc->nelem - head + tail);
113 seq_printf(s, "%u\n", tail - head);
118 static int queued_open(struct inode *inode, struct file *file)
120 return single_open(file, read_dbc_queued, inode->i_private);
123 static const struct file_operations queued_fops = {
124 .owner = THIS_MODULE,
128 .release = single_release,
131 void qaic_debugfs_init(struct qaic_drm_device *qddev)
133 struct qaic_device *qdev = qddev->qdev;
134 struct dentry *debugfs_root;
135 struct dentry *debugfs_dir;
136 char name[QAIC_DBC_DIR_NAME];
139 debugfs_root = to_drm(qddev)->debugfs_root;
141 debugfs_create_file("bootlog", 0400, debugfs_root, qdev, &bootlog_fops);
143 * 256 dbcs per device is likely the max we will ever see and lets static checking see a
146 for (i = 0; i < qdev->num_dbc && i < 256; ++i) {
147 snprintf(name, QAIC_DBC_DIR_NAME, "dbc%03u", i);
148 debugfs_dir = debugfs_create_dir(name, debugfs_root);
149 debugfs_create_file("fifo_size", 0400, debugfs_dir, &qdev->dbc[i], &fifo_size_fops);
150 debugfs_create_file("queued", 0400, debugfs_dir, &qdev->dbc[i], &queued_fops);
154 static struct bootlog_page *alloc_bootlog_page(struct qaic_device *qdev)
156 struct bootlog_page *page;
158 page = (struct bootlog_page *)devm_get_free_pages(&qdev->pdev->dev, GFP_KERNEL, 0);
162 page->size = PAGE_SIZE;
163 page->offset = sizeof(*page);
164 list_add_tail(&page->node, &qdev->bootlog);
169 static int reset_bootlog(struct qaic_device *qdev)
171 struct bootlog_page *page;
172 struct bootlog_page *i;
174 mutex_lock(&qdev->bootlog_mutex);
175 list_for_each_entry_safe(page, i, &qdev->bootlog, node) {
176 list_del(&page->node);
177 devm_free_pages(&qdev->pdev->dev, (unsigned long)page);
180 page = alloc_bootlog_page(qdev);
181 mutex_unlock(&qdev->bootlog_mutex);
188 static void *bootlog_get_space(struct qaic_device *qdev, unsigned int size)
190 struct bootlog_page *page;
192 page = list_last_entry(&qdev->bootlog, struct bootlog_page, node);
194 if (size_add(size, sizeof(*page)) > page->size)
197 if (page->offset + size > page->size) {
198 page = alloc_bootlog_page(qdev);
203 return (void *)page + page->offset;
206 static void bootlog_commit(struct qaic_device *qdev, unsigned int size)
208 struct bootlog_page *page;
210 page = list_last_entry(&qdev->bootlog, struct bootlog_page, node);
212 page->offset += size;
215 static void bootlog_log(struct work_struct *work)
217 struct bootlog_msg *msg = container_of(work, struct bootlog_msg, work);
218 unsigned int len = strlen(msg->str) + 1;
219 struct qaic_device *qdev = msg->qdev;
222 mutex_lock(&qdev->bootlog_mutex);
223 log = bootlog_get_space(qdev, len);
225 memcpy(log, msg, len);
226 bootlog_commit(qdev, len);
228 mutex_unlock(&qdev->bootlog_mutex);
230 if (mhi_queue_buf(qdev->bootlog_ch, DMA_FROM_DEVICE, msg, BOOTLOG_MSG_SIZE, MHI_EOT))
231 devm_kfree(&qdev->pdev->dev, msg);
234 static int qaic_bootlog_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
236 struct qaic_device *qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev));
237 struct bootlog_msg *msg;
240 qdev->bootlog_wq = alloc_ordered_workqueue("qaic_bootlog", 0);
241 if (!qdev->bootlog_wq) {
246 ret = reset_bootlog(qdev);
248 goto destroy_workqueue;
250 ret = mhi_prepare_for_transfer(mhi_dev);
252 goto destroy_workqueue;
254 for (i = 0; i < BOOTLOG_POOL_SIZE; i++) {
255 msg = devm_kzalloc(&qdev->pdev->dev, sizeof(*msg), GFP_KERNEL);
262 INIT_WORK(&msg->work, bootlog_log);
264 ret = mhi_queue_buf(mhi_dev, DMA_FROM_DEVICE, msg, BOOTLOG_MSG_SIZE, MHI_EOT);
269 dev_set_drvdata(&mhi_dev->dev, qdev);
270 qdev->bootlog_ch = mhi_dev;
274 mhi_unprepare_from_transfer(mhi_dev);
276 flush_workqueue(qdev->bootlog_wq);
277 destroy_workqueue(qdev->bootlog_wq);
282 static void qaic_bootlog_mhi_remove(struct mhi_device *mhi_dev)
284 struct qaic_device *qdev;
286 qdev = dev_get_drvdata(&mhi_dev->dev);
288 mhi_unprepare_from_transfer(qdev->bootlog_ch);
289 flush_workqueue(qdev->bootlog_wq);
290 destroy_workqueue(qdev->bootlog_wq);
291 qdev->bootlog_ch = NULL;
294 static void qaic_bootlog_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
298 static void qaic_bootlog_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
300 struct qaic_device *qdev = dev_get_drvdata(&mhi_dev->dev);
301 struct bootlog_msg *msg = mhi_result->buf_addr;
303 if (mhi_result->transaction_status) {
304 devm_kfree(&qdev->pdev->dev, msg);
308 /* Force a null at the end of the transferred string */
309 msg->str[mhi_result->bytes_xferd - 1] = 0;
311 queue_work(qdev->bootlog_wq, &msg->work);
314 static const struct mhi_device_id qaic_bootlog_mhi_match_table[] = {
315 { .chan = "QAIC_LOGGING", },
319 static struct mhi_driver qaic_bootlog_mhi_driver = {
320 .id_table = qaic_bootlog_mhi_match_table,
321 .remove = qaic_bootlog_mhi_remove,
322 .probe = qaic_bootlog_mhi_probe,
323 .ul_xfer_cb = qaic_bootlog_mhi_ul_xfer_cb,
324 .dl_xfer_cb = qaic_bootlog_mhi_dl_xfer_cb,
326 .name = "qaic_bootlog",
330 int qaic_bootlog_register(void)
332 return mhi_driver_register(&qaic_bootlog_mhi_driver);
335 void qaic_bootlog_unregister(void)
337 mhi_driver_unregister(&qaic_bootlog_mhi_driver);