1 // SPDX-License-Identifier: GPL-2.0+
2 // Debug logs for the ChromeOS EC
4 // Copyright (C) 2015 Google, Inc.
6 #include <linux/circ_buf.h>
7 #include <linux/debugfs.h>
8 #include <linux/delay.h>
10 #include <linux/mfd/cros_ec.h>
11 #include <linux/mfd/cros_ec_commands.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/wait.h>
20 #define DRV_NAME "cros-ec-debugfs"
23 #define LOG_SIZE (1 << LOG_SHIFT)
24 #define LOG_POLL_SEC 10
26 #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
28 /* struct cros_ec_debugfs - ChromeOS EC debugging information
30 * @ec: EC device this debugfs information belongs to
31 * @dir: dentry for debugfs files
32 * @log_buffer: circular buffer for console log information
33 * @read_msg: preallocated EC command and buffer to read console log
34 * @log_mutex: mutex to protect circular buffer
35 * @log_wq: waitqueue for log readers
36 * @log_poll_work: recurring task to poll EC for new console log data
37 * @panicinfo_blob: panicinfo debugfs blob
39 struct cros_ec_debugfs {
40 struct cros_ec_dev *ec;
43 struct circ_buf log_buffer;
44 struct cros_ec_command *read_msg;
45 struct mutex log_mutex;
46 wait_queue_head_t log_wq;
47 struct delayed_work log_poll_work;
49 struct debugfs_blob_wrapper panicinfo_blob;
53 * We need to make sure that the EC log buffer on the UART is large enough,
54 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
56 static void cros_ec_console_log_work(struct work_struct *__work)
58 struct cros_ec_debugfs *debug_info =
59 container_of(to_delayed_work(__work),
60 struct cros_ec_debugfs,
62 struct cros_ec_dev *ec = debug_info->ec;
63 struct circ_buf *cb = &debug_info->log_buffer;
64 struct cros_ec_command snapshot_msg = {
65 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
68 struct ec_params_console_read_v1 *read_params =
69 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
70 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
75 ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
77 dev_err(ec->dev, "EC communication failed\n");
80 if (snapshot_msg.result != EC_RES_SUCCESS) {
81 dev_err(ec->dev, "EC failed to snapshot the console log\n");
85 /* Loop until we have read everything, or there's an error. */
86 mutex_lock(&debug_info->log_mutex);
87 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
91 dev_info_once(ec->dev,
92 "Some logs may have been dropped...\n");
96 memset(read_params, '\0', sizeof(*read_params));
97 read_params->subcmd = CONSOLE_READ_RECENT;
98 ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
100 dev_err(ec->dev, "EC communication failed\n");
103 if (debug_info->read_msg->result != EC_RES_SUCCESS) {
105 "EC failed to read the console log\n");
109 /* If the buffer is empty, we're done here. */
110 if (ret == 0 || ec_buffer[0] == '\0')
114 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
115 cb->buf[cb->head] = ec_buffer[idx];
116 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
121 wake_up(&debug_info->log_wq);
124 mutex_unlock(&debug_info->log_mutex);
127 schedule_delayed_work(&debug_info->log_poll_work,
128 msecs_to_jiffies(LOG_POLL_SEC * 1000));
131 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
133 file->private_data = inode->i_private;
135 return nonseekable_open(inode, file);
138 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
139 size_t count, loff_t *ppos)
141 struct cros_ec_debugfs *debug_info = file->private_data;
142 struct circ_buf *cb = &debug_info->log_buffer;
145 mutex_lock(&debug_info->log_mutex);
147 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
148 if (file->f_flags & O_NONBLOCK) {
153 mutex_unlock(&debug_info->log_mutex);
155 ret = wait_event_interruptible(debug_info->log_wq,
156 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
160 mutex_lock(&debug_info->log_mutex);
163 /* Only copy until the end of the circular buffer, and let userspace
164 * retry to get the rest of the data.
166 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
169 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
174 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
177 mutex_unlock(&debug_info->log_mutex);
181 static __poll_t cros_ec_console_log_poll(struct file *file,
184 struct cros_ec_debugfs *debug_info = file->private_data;
187 poll_wait(file, &debug_info->log_wq, wait);
189 mutex_lock(&debug_info->log_mutex);
190 if (CIRC_CNT(debug_info->log_buffer.head,
191 debug_info->log_buffer.tail,
193 mask |= EPOLLIN | EPOLLRDNORM;
194 mutex_unlock(&debug_info->log_mutex);
199 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
204 static ssize_t cros_ec_pdinfo_read(struct file *file,
205 char __user *user_buf,
209 char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
210 struct cros_ec_debugfs *debug_info = file->private_data;
211 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
213 struct cros_ec_command msg;
215 struct ec_response_usb_pd_control_v1 resp;
216 struct ec_params_usb_pd_control params;
219 struct cros_ec_command *msg;
220 struct ec_response_usb_pd_control_v1 *resp;
221 struct ec_params_usb_pd_control *params;
225 params = (struct ec_params_usb_pd_control *)msg->data;
226 resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
228 msg->command = EC_CMD_USB_PD_CONTROL;
230 msg->insize = sizeof(*resp);
231 msg->outsize = sizeof(*params);
234 * Read status from all PD ports until failure, typically caused
235 * by attempting to read status on a port that doesn't exist.
237 for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
243 if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
246 p += scnprintf(p, sizeof(read_buf) + read_buf - p,
247 "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
248 resp->state, resp->enabled, resp->role,
252 return simple_read_from_buffer(user_buf, count, ppos,
253 read_buf, p - read_buf);
256 const struct file_operations cros_ec_console_log_fops = {
257 .owner = THIS_MODULE,
258 .open = cros_ec_console_log_open,
259 .read = cros_ec_console_log_read,
261 .poll = cros_ec_console_log_poll,
262 .release = cros_ec_console_log_release,
265 const struct file_operations cros_ec_pdinfo_fops = {
266 .owner = THIS_MODULE,
268 .read = cros_ec_pdinfo_read,
269 .llseek = default_llseek,
272 static int ec_read_version_supported(struct cros_ec_dev *ec)
274 struct ec_params_get_cmd_versions_v1 *params;
275 struct ec_response_get_cmd_versions *response;
278 struct cros_ec_command *msg;
280 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
285 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
286 msg->outsize = sizeof(*params);
287 msg->insize = sizeof(*response);
289 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
290 params->cmd = EC_CMD_CONSOLE_READ;
291 response = (struct ec_response_get_cmd_versions *)msg->data;
293 ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
294 msg->result == EC_RES_SUCCESS &&
295 (response->version_mask & EC_VER_MASK(1));
302 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
304 struct cros_ec_dev *ec = debug_info->ec;
306 int read_params_size;
307 int read_response_size;
309 if (!ec_read_version_supported(ec)) {
311 "device does not support reading the console log\n");
315 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
319 read_params_size = sizeof(struct ec_params_console_read_v1);
320 read_response_size = ec->ec_dev->max_response;
321 debug_info->read_msg = devm_kzalloc(ec->dev,
322 sizeof(*debug_info->read_msg) +
323 max(read_params_size, read_response_size), GFP_KERNEL);
324 if (!debug_info->read_msg)
327 debug_info->read_msg->version = 1;
328 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
329 debug_info->read_msg->outsize = read_params_size;
330 debug_info->read_msg->insize = read_response_size;
332 debug_info->log_buffer.buf = buf;
333 debug_info->log_buffer.head = 0;
334 debug_info->log_buffer.tail = 0;
336 mutex_init(&debug_info->log_mutex);
337 init_waitqueue_head(&debug_info->log_wq);
339 if (!debugfs_create_file("console_log",
343 &cros_ec_console_log_fops))
346 INIT_DELAYED_WORK(&debug_info->log_poll_work,
347 cros_ec_console_log_work);
348 schedule_delayed_work(&debug_info->log_poll_work, 0);
353 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
355 if (debug_info->log_buffer.buf) {
356 cancel_delayed_work_sync(&debug_info->log_poll_work);
357 mutex_destroy(&debug_info->log_mutex);
361 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
363 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
365 struct cros_ec_command *msg;
368 insize = ec_dev->max_response;
370 msg = devm_kzalloc(debug_info->ec->dev,
371 sizeof(*msg) + insize, GFP_KERNEL);
375 msg->command = EC_CMD_GET_PANIC_INFO;
376 msg->insize = insize;
378 ret = cros_ec_cmd_xfer(ec_dev, msg);
380 dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n");
389 debug_info->panicinfo_blob.data = msg->data;
390 debug_info->panicinfo_blob.size = ret;
392 if (!debugfs_create_blob("panicinfo",
395 &debug_info->panicinfo_blob)) {
403 devm_kfree(debug_info->ec->dev, msg);
407 static int cros_ec_create_pdinfo(struct cros_ec_debugfs *debug_info)
409 if (!debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
410 &cros_ec_pdinfo_fops))
416 static int cros_ec_debugfs_probe(struct platform_device *pd)
418 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
419 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
420 const char *name = ec_platform->ec_name;
421 struct cros_ec_debugfs *debug_info;
424 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
429 debug_info->dir = debugfs_create_dir(name, NULL);
430 if (!debug_info->dir)
433 ret = cros_ec_create_panicinfo(debug_info);
437 ret = cros_ec_create_console_log(debug_info);
441 ret = cros_ec_create_pdinfo(debug_info);
445 ec->debug_info = debug_info;
447 dev_set_drvdata(&pd->dev, ec);
452 cros_ec_cleanup_console_log(debug_info);
454 debugfs_remove_recursive(debug_info->dir);
458 static int cros_ec_debugfs_remove(struct platform_device *pd)
460 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
462 debugfs_remove_recursive(ec->debug_info->dir);
463 cros_ec_cleanup_console_log(ec->debug_info);
468 static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
470 struct cros_ec_dev *ec = dev_get_drvdata(dev);
472 if (ec->debug_info->log_buffer.buf)
473 cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
478 static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
480 struct cros_ec_dev *ec = dev_get_drvdata(dev);
482 if (ec->debug_info->log_buffer.buf)
483 schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
488 static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
489 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
491 static struct platform_driver cros_ec_debugfs_driver = {
494 .pm = &cros_ec_debugfs_pm_ops,
496 .probe = cros_ec_debugfs_probe,
497 .remove = cros_ec_debugfs_remove,
500 module_platform_driver(cros_ec_debugfs_driver);
502 MODULE_LICENSE("GPL");
503 MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
504 MODULE_ALIAS("platform:" DRV_NAME);