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>
33 #define LOG_SIZE (1 << LOG_SHIFT)
34 #define LOG_POLL_SEC 10
36 #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
38 /* struct cros_ec_debugfs - ChromeOS EC debugging information
40 * @ec: EC device this debugfs information belongs to
41 * @dir: dentry for debugfs files
42 * @log_buffer: circular buffer for console log information
43 * @read_msg: preallocated EC command and buffer to read console log
44 * @log_mutex: mutex to protect circular buffer
45 * @log_wq: waitqueue for log readers
46 * @log_poll_work: recurring task to poll EC for new console log data
47 * @panicinfo_blob: panicinfo debugfs blob
49 struct cros_ec_debugfs
{
50 struct cros_ec_dev
*ec
;
53 struct circ_buf log_buffer
;
54 struct cros_ec_command
*read_msg
;
55 struct mutex log_mutex
;
56 wait_queue_head_t log_wq
;
57 struct delayed_work log_poll_work
;
59 struct debugfs_blob_wrapper panicinfo_blob
;
63 * We need to make sure that the EC log buffer on the UART is large enough,
64 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
66 static void cros_ec_console_log_work(struct work_struct
*__work
)
68 struct cros_ec_debugfs
*debug_info
=
69 container_of(to_delayed_work(__work
),
70 struct cros_ec_debugfs
,
72 struct cros_ec_dev
*ec
= debug_info
->ec
;
73 struct circ_buf
*cb
= &debug_info
->log_buffer
;
74 struct cros_ec_command snapshot_msg
= {
75 .command
= EC_CMD_CONSOLE_SNAPSHOT
+ ec
->cmd_offset
,
78 struct ec_params_console_read_v1
*read_params
=
79 (struct ec_params_console_read_v1
*)debug_info
->read_msg
->data
;
80 uint8_t *ec_buffer
= (uint8_t *)debug_info
->read_msg
->data
;
85 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, &snapshot_msg
);
87 dev_err(ec
->dev
, "EC communication failed\n");
90 if (snapshot_msg
.result
!= EC_RES_SUCCESS
) {
91 dev_err(ec
->dev
, "EC failed to snapshot the console log\n");
95 /* Loop until we have read everything, or there's an error. */
96 mutex_lock(&debug_info
->log_mutex
);
97 buf_space
= CIRC_SPACE(cb
->head
, cb
->tail
, LOG_SIZE
);
101 dev_info_once(ec
->dev
,
102 "Some logs may have been dropped...\n");
106 memset(read_params
, '\0', sizeof(*read_params
));
107 read_params
->subcmd
= CONSOLE_READ_RECENT
;
108 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, debug_info
->read_msg
);
110 dev_err(ec
->dev
, "EC communication failed\n");
113 if (debug_info
->read_msg
->result
!= EC_RES_SUCCESS
) {
115 "EC failed to read the console log\n");
119 /* If the buffer is empty, we're done here. */
120 if (ret
== 0 || ec_buffer
[0] == '\0')
124 while (idx
< ret
&& ec_buffer
[idx
] != '\0' && buf_space
> 0) {
125 cb
->buf
[cb
->head
] = ec_buffer
[idx
];
126 cb
->head
= CIRC_ADD(cb
->head
, LOG_SIZE
, 1);
131 wake_up(&debug_info
->log_wq
);
134 mutex_unlock(&debug_info
->log_mutex
);
137 schedule_delayed_work(&debug_info
->log_poll_work
,
138 msecs_to_jiffies(LOG_POLL_SEC
* 1000));
141 static int cros_ec_console_log_open(struct inode
*inode
, struct file
*file
)
143 file
->private_data
= inode
->i_private
;
145 return nonseekable_open(inode
, file
);
148 static ssize_t
cros_ec_console_log_read(struct file
*file
, char __user
*buf
,
149 size_t count
, loff_t
*ppos
)
151 struct cros_ec_debugfs
*debug_info
= file
->private_data
;
152 struct circ_buf
*cb
= &debug_info
->log_buffer
;
155 mutex_lock(&debug_info
->log_mutex
);
157 while (!CIRC_CNT(cb
->head
, cb
->tail
, LOG_SIZE
)) {
158 if (file
->f_flags
& O_NONBLOCK
) {
163 mutex_unlock(&debug_info
->log_mutex
);
165 ret
= wait_event_interruptible(debug_info
->log_wq
,
166 CIRC_CNT(cb
->head
, cb
->tail
, LOG_SIZE
));
170 mutex_lock(&debug_info
->log_mutex
);
173 /* Only copy until the end of the circular buffer, and let userspace
174 * retry to get the rest of the data.
176 ret
= min_t(size_t, CIRC_CNT_TO_END(cb
->head
, cb
->tail
, LOG_SIZE
),
179 if (copy_to_user(buf
, cb
->buf
+ cb
->tail
, ret
)) {
184 cb
->tail
= CIRC_ADD(cb
->tail
, LOG_SIZE
, ret
);
187 mutex_unlock(&debug_info
->log_mutex
);
191 static __poll_t
cros_ec_console_log_poll(struct file
*file
,
194 struct cros_ec_debugfs
*debug_info
= file
->private_data
;
197 poll_wait(file
, &debug_info
->log_wq
, wait
);
199 mutex_lock(&debug_info
->log_mutex
);
200 if (CIRC_CNT(debug_info
->log_buffer
.head
,
201 debug_info
->log_buffer
.tail
,
203 mask
|= EPOLLIN
| EPOLLRDNORM
;
204 mutex_unlock(&debug_info
->log_mutex
);
209 static int cros_ec_console_log_release(struct inode
*inode
, struct file
*file
)
214 const struct file_operations cros_ec_console_log_fops
= {
215 .owner
= THIS_MODULE
,
216 .open
= cros_ec_console_log_open
,
217 .read
= cros_ec_console_log_read
,
219 .poll
= cros_ec_console_log_poll
,
220 .release
= cros_ec_console_log_release
,
223 static int ec_read_version_supported(struct cros_ec_dev
*ec
)
225 struct ec_params_get_cmd_versions_v1
*params
;
226 struct ec_response_get_cmd_versions
*response
;
229 struct cros_ec_command
*msg
;
231 msg
= kzalloc(sizeof(*msg
) + max(sizeof(*params
), sizeof(*response
)),
236 msg
->command
= EC_CMD_GET_CMD_VERSIONS
+ ec
->cmd_offset
;
237 msg
->outsize
= sizeof(*params
);
238 msg
->insize
= sizeof(*response
);
240 params
= (struct ec_params_get_cmd_versions_v1
*)msg
->data
;
241 params
->cmd
= EC_CMD_CONSOLE_READ
;
242 response
= (struct ec_response_get_cmd_versions
*)msg
->data
;
244 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
) >= 0 &&
245 msg
->result
== EC_RES_SUCCESS
&&
246 (response
->version_mask
& EC_VER_MASK(1));
253 static int cros_ec_create_console_log(struct cros_ec_debugfs
*debug_info
)
255 struct cros_ec_dev
*ec
= debug_info
->ec
;
257 int read_params_size
;
258 int read_response_size
;
260 if (!ec_read_version_supported(ec
)) {
262 "device does not support reading the console log\n");
266 buf
= devm_kzalloc(ec
->dev
, LOG_SIZE
, GFP_KERNEL
);
270 read_params_size
= sizeof(struct ec_params_console_read_v1
);
271 read_response_size
= ec
->ec_dev
->max_response
;
272 debug_info
->read_msg
= devm_kzalloc(ec
->dev
,
273 sizeof(*debug_info
->read_msg
) +
274 max(read_params_size
, read_response_size
), GFP_KERNEL
);
275 if (!debug_info
->read_msg
)
278 debug_info
->read_msg
->version
= 1;
279 debug_info
->read_msg
->command
= EC_CMD_CONSOLE_READ
+ ec
->cmd_offset
;
280 debug_info
->read_msg
->outsize
= read_params_size
;
281 debug_info
->read_msg
->insize
= read_response_size
;
283 debug_info
->log_buffer
.buf
= buf
;
284 debug_info
->log_buffer
.head
= 0;
285 debug_info
->log_buffer
.tail
= 0;
287 mutex_init(&debug_info
->log_mutex
);
288 init_waitqueue_head(&debug_info
->log_wq
);
290 if (!debugfs_create_file("console_log",
294 &cros_ec_console_log_fops
))
297 INIT_DELAYED_WORK(&debug_info
->log_poll_work
,
298 cros_ec_console_log_work
);
299 schedule_delayed_work(&debug_info
->log_poll_work
, 0);
304 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs
*debug_info
)
306 if (debug_info
->log_buffer
.buf
) {
307 cancel_delayed_work_sync(&debug_info
->log_poll_work
);
308 mutex_destroy(&debug_info
->log_mutex
);
312 static int cros_ec_create_panicinfo(struct cros_ec_debugfs
*debug_info
)
314 struct cros_ec_device
*ec_dev
= debug_info
->ec
->ec_dev
;
316 struct cros_ec_command
*msg
;
319 insize
= ec_dev
->max_response
;
321 msg
= devm_kzalloc(debug_info
->ec
->dev
,
322 sizeof(*msg
) + insize
, GFP_KERNEL
);
326 msg
->command
= EC_CMD_GET_PANIC_INFO
;
327 msg
->insize
= insize
;
329 ret
= cros_ec_cmd_xfer(ec_dev
, msg
);
331 dev_warn(debug_info
->ec
->dev
, "Cannot read panicinfo.\n");
340 debug_info
->panicinfo_blob
.data
= msg
->data
;
341 debug_info
->panicinfo_blob
.size
= ret
;
343 if (!debugfs_create_blob("panicinfo",
346 &debug_info
->panicinfo_blob
)) {
354 devm_kfree(debug_info
->ec
->dev
, msg
);
358 int cros_ec_debugfs_init(struct cros_ec_dev
*ec
)
360 struct cros_ec_platform
*ec_platform
= dev_get_platdata(ec
->dev
);
361 const char *name
= ec_platform
->ec_name
;
362 struct cros_ec_debugfs
*debug_info
;
365 debug_info
= devm_kzalloc(ec
->dev
, sizeof(*debug_info
), GFP_KERNEL
);
370 debug_info
->dir
= debugfs_create_dir(name
, NULL
);
371 if (!debug_info
->dir
)
374 ret
= cros_ec_create_panicinfo(debug_info
);
378 ret
= cros_ec_create_console_log(debug_info
);
382 ec
->debug_info
= debug_info
;
387 debugfs_remove_recursive(debug_info
->dir
);
390 EXPORT_SYMBOL(cros_ec_debugfs_init
);
392 void cros_ec_debugfs_remove(struct cros_ec_dev
*ec
)
397 debugfs_remove_recursive(ec
->debug_info
->dir
);
398 cros_ec_cleanup_console_log(ec
->debug_info
);
400 EXPORT_SYMBOL(cros_ec_debugfs_remove
);