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/module.h>
11 #include <linux/mutex.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.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 /* waitqueue for log readers */
29 static DECLARE_WAIT_QUEUE_HEAD(cros_ec_debugfs_log_wq);
32 * struct cros_ec_debugfs - EC debugging information.
34 * @ec: EC device this debugfs information belongs to
35 * @dir: dentry for debugfs files
36 * @log_buffer: circular buffer for console log information
37 * @read_msg: preallocated EC command and buffer to read console log
38 * @log_mutex: mutex to protect circular buffer
39 * @log_poll_work: recurring task to poll EC for new console log data
40 * @panicinfo_blob: panicinfo debugfs blob
42 struct cros_ec_debugfs {
43 struct cros_ec_dev *ec;
46 struct circ_buf log_buffer;
47 struct cros_ec_command *read_msg;
48 struct mutex log_mutex;
49 struct delayed_work log_poll_work;
51 struct debugfs_blob_wrapper panicinfo_blob;
55 * We need to make sure that the EC log buffer on the UART is large enough,
56 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
58 static void cros_ec_console_log_work(struct work_struct *__work)
60 struct cros_ec_debugfs *debug_info =
61 container_of(to_delayed_work(__work),
62 struct cros_ec_debugfs,
64 struct cros_ec_dev *ec = debug_info->ec;
65 struct circ_buf *cb = &debug_info->log_buffer;
66 struct cros_ec_command snapshot_msg = {
67 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
70 struct ec_params_console_read_v1 *read_params =
71 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
72 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
77 ret = cros_ec_cmd_xfer_status(ec->ec_dev, &snapshot_msg);
81 /* Loop until we have read everything, or there's an error. */
82 mutex_lock(&debug_info->log_mutex);
83 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
87 dev_info_once(ec->dev,
88 "Some logs may have been dropped...\n");
92 memset(read_params, '\0', sizeof(*read_params));
93 read_params->subcmd = CONSOLE_READ_RECENT;
94 ret = cros_ec_cmd_xfer_status(ec->ec_dev,
95 debug_info->read_msg);
99 /* If the buffer is empty, we're done here. */
100 if (ret == 0 || ec_buffer[0] == '\0')
104 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
105 cb->buf[cb->head] = ec_buffer[idx];
106 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
111 wake_up(&cros_ec_debugfs_log_wq);
114 mutex_unlock(&debug_info->log_mutex);
117 schedule_delayed_work(&debug_info->log_poll_work,
118 msecs_to_jiffies(LOG_POLL_SEC * 1000));
121 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
123 file->private_data = inode->i_private;
125 return stream_open(inode, file);
128 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
129 size_t count, loff_t *ppos)
131 struct cros_ec_debugfs *debug_info = file->private_data;
132 struct circ_buf *cb = &debug_info->log_buffer;
135 mutex_lock(&debug_info->log_mutex);
137 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
138 if (file->f_flags & O_NONBLOCK) {
143 mutex_unlock(&debug_info->log_mutex);
145 ret = wait_event_interruptible(cros_ec_debugfs_log_wq,
146 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
150 mutex_lock(&debug_info->log_mutex);
153 /* Only copy until the end of the circular buffer, and let userspace
154 * retry to get the rest of the data.
156 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
159 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
164 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
167 mutex_unlock(&debug_info->log_mutex);
171 static __poll_t cros_ec_console_log_poll(struct file *file,
174 struct cros_ec_debugfs *debug_info = file->private_data;
177 poll_wait(file, &cros_ec_debugfs_log_wq, wait);
179 mutex_lock(&debug_info->log_mutex);
180 if (CIRC_CNT(debug_info->log_buffer.head,
181 debug_info->log_buffer.tail,
183 mask |= EPOLLIN | EPOLLRDNORM;
184 mutex_unlock(&debug_info->log_mutex);
189 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
194 static ssize_t cros_ec_pdinfo_read(struct file *file,
195 char __user *user_buf,
199 char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
200 struct cros_ec_debugfs *debug_info = file->private_data;
201 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
203 struct cros_ec_command msg;
205 struct ec_response_usb_pd_control_v1 resp;
206 struct ec_params_usb_pd_control params;
209 struct cros_ec_command *msg;
210 struct ec_response_usb_pd_control_v1 *resp;
211 struct ec_params_usb_pd_control *params;
215 params = (struct ec_params_usb_pd_control *)msg->data;
216 resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
218 msg->command = EC_CMD_USB_PD_CONTROL;
220 msg->insize = sizeof(*resp);
221 msg->outsize = sizeof(*params);
224 * Read status from all PD ports until failure, typically caused
225 * by attempting to read status on a port that doesn't exist.
227 for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
233 if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
236 p += scnprintf(p, sizeof(read_buf) + read_buf - p,
237 "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
238 resp->state, resp->enabled, resp->role,
242 return simple_read_from_buffer(user_buf, count, ppos,
243 read_buf, p - read_buf);
246 static bool cros_ec_uptime_is_supported(struct cros_ec_device *ec_dev)
249 struct cros_ec_command cmd;
250 struct ec_response_uptime_info resp;
254 msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
255 msg.cmd.insize = sizeof(msg.resp);
257 ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
258 if (ret == -EPROTO && msg.cmd.result == EC_RES_INVALID_COMMAND)
261 /* Other errors maybe a transient error, do not rule about support. */
265 static ssize_t cros_ec_uptime_read(struct file *file, char __user *user_buf,
266 size_t count, loff_t *ppos)
268 struct cros_ec_debugfs *debug_info = file->private_data;
269 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
271 struct cros_ec_command cmd;
272 struct ec_response_uptime_info resp;
274 struct ec_response_uptime_info *resp;
278 resp = (struct ec_response_uptime_info *)&msg.resp;
280 msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
281 msg.cmd.insize = sizeof(*resp);
283 ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
287 ret = scnprintf(read_buf, sizeof(read_buf), "%u\n",
288 resp->time_since_ec_boot_ms);
290 return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
293 static const struct file_operations cros_ec_console_log_fops = {
294 .owner = THIS_MODULE,
295 .open = cros_ec_console_log_open,
296 .read = cros_ec_console_log_read,
298 .poll = cros_ec_console_log_poll,
299 .release = cros_ec_console_log_release,
302 static const struct file_operations cros_ec_pdinfo_fops = {
303 .owner = THIS_MODULE,
305 .read = cros_ec_pdinfo_read,
306 .llseek = default_llseek,
309 static const struct file_operations cros_ec_uptime_fops = {
310 .owner = THIS_MODULE,
312 .read = cros_ec_uptime_read,
313 .llseek = default_llseek,
316 static int ec_read_version_supported(struct cros_ec_dev *ec)
318 struct ec_params_get_cmd_versions_v1 *params;
319 struct ec_response_get_cmd_versions *response;
322 struct cros_ec_command *msg;
324 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
329 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
330 msg->outsize = sizeof(*params);
331 msg->insize = sizeof(*response);
333 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
334 params->cmd = EC_CMD_CONSOLE_READ;
335 response = (struct ec_response_get_cmd_versions *)msg->data;
337 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
338 response->version_mask & EC_VER_MASK(1);
345 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
347 struct cros_ec_dev *ec = debug_info->ec;
349 int read_params_size;
350 int read_response_size;
353 * If the console log feature is not supported return silently and
354 * don't create the console_log entry.
356 if (!ec_read_version_supported(ec))
359 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
363 read_params_size = sizeof(struct ec_params_console_read_v1);
364 read_response_size = ec->ec_dev->max_response;
365 debug_info->read_msg = devm_kzalloc(ec->dev,
366 sizeof(*debug_info->read_msg) +
367 max(read_params_size, read_response_size), GFP_KERNEL);
368 if (!debug_info->read_msg)
371 debug_info->read_msg->version = 1;
372 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
373 debug_info->read_msg->outsize = read_params_size;
374 debug_info->read_msg->insize = read_response_size;
376 debug_info->log_buffer.buf = buf;
377 debug_info->log_buffer.head = 0;
378 debug_info->log_buffer.tail = 0;
380 mutex_init(&debug_info->log_mutex);
382 debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
383 debug_info, &cros_ec_console_log_fops);
385 INIT_DELAYED_WORK(&debug_info->log_poll_work,
386 cros_ec_console_log_work);
387 schedule_delayed_work(&debug_info->log_poll_work, 0);
392 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
394 if (debug_info->log_buffer.buf) {
395 cancel_delayed_work_sync(&debug_info->log_poll_work);
396 mutex_destroy(&debug_info->log_mutex);
400 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
402 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
404 struct cros_ec_command *msg;
407 insize = ec_dev->max_response;
409 msg = devm_kzalloc(debug_info->ec->dev,
410 sizeof(*msg) + insize, GFP_KERNEL);
414 msg->command = EC_CMD_GET_PANIC_INFO;
415 msg->insize = insize;
417 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
427 debug_info->panicinfo_blob.data = msg->data;
428 debug_info->panicinfo_blob.size = ret;
430 debugfs_create_blob("panicinfo", S_IFREG | 0444, debug_info->dir,
431 &debug_info->panicinfo_blob);
436 devm_kfree(debug_info->ec->dev, msg);
440 static int cros_ec_debugfs_probe(struct platform_device *pd)
442 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
443 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
444 const char *name = ec_platform->ec_name;
445 struct cros_ec_debugfs *debug_info;
448 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
453 debug_info->dir = debugfs_create_dir(name, NULL);
455 ret = cros_ec_create_panicinfo(debug_info);
459 ret = cros_ec_create_console_log(debug_info);
463 debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
464 &cros_ec_pdinfo_fops);
466 if (cros_ec_uptime_is_supported(ec->ec_dev))
467 debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
468 &cros_ec_uptime_fops);
470 debugfs_create_x32("last_resume_result", 0444, debug_info->dir,
471 &ec->ec_dev->last_resume_result);
473 debugfs_create_u16("suspend_timeout_ms", 0664, debug_info->dir,
474 &ec->ec_dev->suspend_timeout_ms);
476 ec->debug_info = debug_info;
478 dev_set_drvdata(&pd->dev, ec);
483 debugfs_remove_recursive(debug_info->dir);
487 static int cros_ec_debugfs_remove(struct platform_device *pd)
489 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
491 debugfs_remove_recursive(ec->debug_info->dir);
492 cros_ec_cleanup_console_log(ec->debug_info);
497 static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
499 struct cros_ec_dev *ec = dev_get_drvdata(dev);
501 if (ec->debug_info->log_buffer.buf)
502 cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
507 static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
509 struct cros_ec_dev *ec = dev_get_drvdata(dev);
511 if (ec->debug_info->log_buffer.buf)
512 schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
517 static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
518 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
520 static struct platform_driver cros_ec_debugfs_driver = {
523 .pm = &cros_ec_debugfs_pm_ops,
525 .probe = cros_ec_debugfs_probe,
526 .remove = cros_ec_debugfs_remove,
529 module_platform_driver(cros_ec_debugfs_driver);
531 MODULE_LICENSE("GPL");
532 MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
533 MODULE_ALIAS("platform:" DRV_NAME);