2 * cros_ec_debugfs - debug logs for Chrome OS EC
4 * Copyright 2015 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/circ_buf.h>
21 #include <linux/debugfs.h>
22 #include <linux/delay.h>
24 #include <linux/mfd/cros_ec.h>
25 #include <linux/mfd/cros_ec_commands.h>
26 #include <linux/mutex.h>
27 #include <linux/poll.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/wait.h>
32 #include "cros_ec_dev.h"
33 #include "cros_ec_debugfs.h"
36 #define LOG_SIZE (1 << LOG_SHIFT)
37 #define LOG_POLL_SEC 10
39 #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
41 /* struct cros_ec_debugfs - ChromeOS EC debugging information
43 * @ec: EC device this debugfs information belongs to
44 * @dir: dentry for debugfs files
45 * @log_buffer: circular buffer for console log information
46 * @read_msg: preallocated EC command and buffer to read console log
47 * @log_mutex: mutex to protect circular buffer
48 * @log_wq: waitqueue for log readers
49 * @log_poll_work: recurring task to poll EC for new console log data
50 * @panicinfo_blob: panicinfo debugfs blob
52 struct cros_ec_debugfs {
53 struct cros_ec_dev *ec;
56 struct circ_buf log_buffer;
57 struct cros_ec_command *read_msg;
58 struct mutex log_mutex;
59 wait_queue_head_t log_wq;
60 struct delayed_work log_poll_work;
62 struct debugfs_blob_wrapper panicinfo_blob;
66 * We need to make sure that the EC log buffer on the UART is large enough,
67 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
69 static void cros_ec_console_log_work(struct work_struct *__work)
71 struct cros_ec_debugfs *debug_info =
72 container_of(to_delayed_work(__work),
73 struct cros_ec_debugfs,
75 struct cros_ec_dev *ec = debug_info->ec;
76 struct circ_buf *cb = &debug_info->log_buffer;
77 struct cros_ec_command snapshot_msg = {
78 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
81 struct ec_params_console_read_v1 *read_params =
82 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
83 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
88 ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
90 dev_err(ec->dev, "EC communication failed\n");
93 if (snapshot_msg.result != EC_RES_SUCCESS) {
94 dev_err(ec->dev, "EC failed to snapshot the console log\n");
98 /* Loop until we have read everything, or there's an error. */
99 mutex_lock(&debug_info->log_mutex);
100 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
104 dev_info_once(ec->dev,
105 "Some logs may have been dropped...\n");
109 memset(read_params, '\0', sizeof(*read_params));
110 read_params->subcmd = CONSOLE_READ_RECENT;
111 ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
113 dev_err(ec->dev, "EC communication failed\n");
116 if (debug_info->read_msg->result != EC_RES_SUCCESS) {
118 "EC failed to read the console log\n");
122 /* If the buffer is empty, we're done here. */
123 if (ret == 0 || ec_buffer[0] == '\0')
127 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
128 cb->buf[cb->head] = ec_buffer[idx];
129 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
134 wake_up(&debug_info->log_wq);
137 mutex_unlock(&debug_info->log_mutex);
140 schedule_delayed_work(&debug_info->log_poll_work,
141 msecs_to_jiffies(LOG_POLL_SEC * 1000));
144 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
146 file->private_data = inode->i_private;
148 return nonseekable_open(inode, file);
151 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
152 size_t count, loff_t *ppos)
154 struct cros_ec_debugfs *debug_info = file->private_data;
155 struct circ_buf *cb = &debug_info->log_buffer;
158 mutex_lock(&debug_info->log_mutex);
160 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
161 if (file->f_flags & O_NONBLOCK) {
166 mutex_unlock(&debug_info->log_mutex);
168 ret = wait_event_interruptible(debug_info->log_wq,
169 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
173 mutex_lock(&debug_info->log_mutex);
176 /* Only copy until the end of the circular buffer, and let userspace
177 * retry to get the rest of the data.
179 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
182 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
187 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
190 mutex_unlock(&debug_info->log_mutex);
194 static unsigned int cros_ec_console_log_poll(struct file *file,
197 struct cros_ec_debugfs *debug_info = file->private_data;
198 unsigned int mask = 0;
200 poll_wait(file, &debug_info->log_wq, wait);
202 mutex_lock(&debug_info->log_mutex);
203 if (CIRC_CNT(debug_info->log_buffer.head,
204 debug_info->log_buffer.tail,
206 mask |= POLLIN | POLLRDNORM;
207 mutex_unlock(&debug_info->log_mutex);
212 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
217 const struct file_operations cros_ec_console_log_fops = {
218 .owner = THIS_MODULE,
219 .open = cros_ec_console_log_open,
220 .read = cros_ec_console_log_read,
222 .poll = cros_ec_console_log_poll,
223 .release = cros_ec_console_log_release,
226 static int ec_read_version_supported(struct cros_ec_dev *ec)
228 struct ec_params_get_cmd_versions_v1 *params;
229 struct ec_response_get_cmd_versions *response;
232 struct cros_ec_command *msg;
234 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
239 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
240 msg->outsize = sizeof(*params);
241 msg->insize = sizeof(*response);
243 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
244 params->cmd = EC_CMD_CONSOLE_READ;
245 response = (struct ec_response_get_cmd_versions *)msg->data;
247 ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
248 msg->result == EC_RES_SUCCESS &&
249 (response->version_mask & EC_VER_MASK(1));
256 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
258 struct cros_ec_dev *ec = debug_info->ec;
260 int read_params_size;
261 int read_response_size;
263 if (!ec_read_version_supported(ec)) {
265 "device does not support reading the console log\n");
269 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
273 read_params_size = sizeof(struct ec_params_console_read_v1);
274 read_response_size = ec->ec_dev->max_response;
275 debug_info->read_msg = devm_kzalloc(ec->dev,
276 sizeof(*debug_info->read_msg) +
277 max(read_params_size, read_response_size), GFP_KERNEL);
278 if (!debug_info->read_msg)
281 debug_info->read_msg->version = 1;
282 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
283 debug_info->read_msg->outsize = read_params_size;
284 debug_info->read_msg->insize = read_response_size;
286 debug_info->log_buffer.buf = buf;
287 debug_info->log_buffer.head = 0;
288 debug_info->log_buffer.tail = 0;
290 mutex_init(&debug_info->log_mutex);
291 init_waitqueue_head(&debug_info->log_wq);
293 if (!debugfs_create_file("console_log",
297 &cros_ec_console_log_fops))
300 INIT_DELAYED_WORK(&debug_info->log_poll_work,
301 cros_ec_console_log_work);
302 schedule_delayed_work(&debug_info->log_poll_work, 0);
307 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
309 if (debug_info->log_buffer.buf) {
310 cancel_delayed_work_sync(&debug_info->log_poll_work);
311 mutex_destroy(&debug_info->log_mutex);
315 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
317 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
319 struct cros_ec_command *msg;
322 insize = ec_dev->max_response;
324 msg = devm_kzalloc(debug_info->ec->dev,
325 sizeof(*msg) + insize, GFP_KERNEL);
329 msg->command = EC_CMD_GET_PANIC_INFO;
330 msg->insize = insize;
332 ret = cros_ec_cmd_xfer(ec_dev, msg);
334 dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n");
343 debug_info->panicinfo_blob.data = msg->data;
344 debug_info->panicinfo_blob.size = ret;
346 if (!debugfs_create_blob("panicinfo",
349 &debug_info->panicinfo_blob)) {
357 devm_kfree(debug_info->ec->dev, msg);
361 int cros_ec_debugfs_init(struct cros_ec_dev *ec)
363 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
364 const char *name = ec_platform->ec_name;
365 struct cros_ec_debugfs *debug_info;
368 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
373 debug_info->dir = debugfs_create_dir(name, NULL);
374 if (!debug_info->dir)
377 ret = cros_ec_create_panicinfo(debug_info);
381 ret = cros_ec_create_console_log(debug_info);
385 ec->debug_info = debug_info;
390 debugfs_remove_recursive(debug_info->dir);
394 void cros_ec_debugfs_remove(struct cros_ec_dev *ec)
399 debugfs_remove_recursive(ec->debug_info->dir);
400 cros_ec_cleanup_console_log(ec->debug_info);