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