xref: /linux/drivers/platform/chrome/cros_ec_debugfs.c (revision a10ea943356b9d70c5616a0a06f6fa97cfdaccb1)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Debug logs for the ChromeOS EC
3 //
4 // Copyright (C) 2015 Google, Inc.
5 
6 #include <linux/circ_buf.h>
7 #include <linux/debugfs.h>
8 #include <linux/delay.h>
9 #include <linux/fs.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>
19 
20 #define DRV_NAME "cros-ec-debugfs"
21 
22 #define LOG_SHIFT		14
23 #define LOG_SIZE		(1 << LOG_SHIFT)
24 #define LOG_POLL_SEC		10
25 
26 #define CIRC_ADD(idx, size, value)	(((idx) + (value)) & ((size) - 1))
27 
28 static unsigned int log_poll_period_ms = LOG_POLL_SEC * MSEC_PER_SEC;
29 module_param(log_poll_period_ms, uint, 0644);
30 MODULE_PARM_DESC(log_poll_period_ms, "EC log polling period(ms)");
31 
32 /* waitqueue for log readers */
33 static DECLARE_WAIT_QUEUE_HEAD(cros_ec_debugfs_log_wq);
34 
35 /**
36  * struct cros_ec_debugfs - EC debugging information.
37  *
38  * @ec: EC device this debugfs information belongs to
39  * @dir: dentry for debugfs files
40  * @log_buffer: circular buffer for console log information
41  * @read_msg: preallocated EC command and buffer to read console log
42  * @log_mutex: mutex to protect circular buffer
43  * @log_poll_work: recurring task to poll EC for new console log data
44  * @panicinfo_blob: panicinfo debugfs blob
45  * @notifier_panic: notifier_block to let kernel to flush buffered log
46  *                  when EC panic
47  */
48 struct cros_ec_debugfs {
49 	struct cros_ec_dev *ec;
50 	struct dentry *dir;
51 	/* EC log */
52 	struct circ_buf log_buffer;
53 	struct cros_ec_command *read_msg;
54 	struct mutex log_mutex;
55 	struct delayed_work log_poll_work;
56 	/* EC panicinfo */
57 	struct debugfs_blob_wrapper panicinfo_blob;
58 	struct notifier_block notifier_panic;
59 };
60 
61 /*
62  * We need to make sure that the EC log buffer on the UART is large enough,
63  * so that it is unlikely enough to overlow within log_poll_period_ms.
64  */
65 static void cros_ec_console_log_work(struct work_struct *__work)
66 {
67 	struct cros_ec_debugfs *debug_info =
68 		container_of(to_delayed_work(__work),
69 			     struct cros_ec_debugfs,
70 			     log_poll_work);
71 	struct cros_ec_dev *ec = debug_info->ec;
72 	struct circ_buf *cb = &debug_info->log_buffer;
73 	struct cros_ec_command snapshot_msg = {
74 		.command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
75 	};
76 
77 	struct ec_params_console_read_v1 *read_params =
78 		(struct ec_params_console_read_v1 *)debug_info->read_msg->data;
79 	uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
80 	int idx;
81 	int buf_space;
82 	int ret;
83 
84 	ret = cros_ec_cmd_xfer_status(ec->ec_dev, &snapshot_msg);
85 	if (ret < 0)
86 		goto resched;
87 
88 	/* Loop until we have read everything, or there's an error. */
89 	mutex_lock(&debug_info->log_mutex);
90 	buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
91 
92 	while (1) {
93 		if (!buf_space) {
94 			dev_info_once(ec->dev,
95 				      "Some logs may have been dropped...\n");
96 			break;
97 		}
98 
99 		memset(read_params, '\0', sizeof(*read_params));
100 		read_params->subcmd = CONSOLE_READ_RECENT;
101 		ret = cros_ec_cmd_xfer_status(ec->ec_dev,
102 					      debug_info->read_msg);
103 		if (ret < 0)
104 			break;
105 
106 		/* If the buffer is empty, we're done here. */
107 		if (ret == 0 || ec_buffer[0] == '\0')
108 			break;
109 
110 		idx = 0;
111 		while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
112 			cb->buf[cb->head] = ec_buffer[idx];
113 			cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
114 			idx++;
115 			buf_space--;
116 		}
117 
118 		wake_up(&cros_ec_debugfs_log_wq);
119 	}
120 
121 	mutex_unlock(&debug_info->log_mutex);
122 
123 resched:
124 	schedule_delayed_work(&debug_info->log_poll_work,
125 			      msecs_to_jiffies(log_poll_period_ms));
126 }
127 
128 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
129 {
130 	file->private_data = inode->i_private;
131 
132 	return stream_open(inode, file);
133 }
134 
135 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
136 					size_t count, loff_t *ppos)
137 {
138 	struct cros_ec_debugfs *debug_info = file->private_data;
139 	struct circ_buf *cb = &debug_info->log_buffer;
140 	ssize_t ret;
141 
142 	mutex_lock(&debug_info->log_mutex);
143 
144 	while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
145 		if (file->f_flags & O_NONBLOCK) {
146 			ret = -EAGAIN;
147 			goto error;
148 		}
149 
150 		mutex_unlock(&debug_info->log_mutex);
151 
152 		ret = wait_event_interruptible(cros_ec_debugfs_log_wq,
153 					CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
154 		if (ret < 0)
155 			return ret;
156 
157 		mutex_lock(&debug_info->log_mutex);
158 	}
159 
160 	/* Only copy until the end of the circular buffer, and let userspace
161 	 * retry to get the rest of the data.
162 	 */
163 	ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
164 		    count);
165 
166 	if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
167 		ret = -EFAULT;
168 		goto error;
169 	}
170 
171 	cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
172 
173 error:
174 	mutex_unlock(&debug_info->log_mutex);
175 	return ret;
176 }
177 
178 static __poll_t cros_ec_console_log_poll(struct file *file,
179 					     poll_table *wait)
180 {
181 	struct cros_ec_debugfs *debug_info = file->private_data;
182 	__poll_t mask = 0;
183 
184 	poll_wait(file, &cros_ec_debugfs_log_wq, wait);
185 
186 	mutex_lock(&debug_info->log_mutex);
187 	if (CIRC_CNT(debug_info->log_buffer.head,
188 		     debug_info->log_buffer.tail,
189 		     LOG_SIZE))
190 		mask |= EPOLLIN | EPOLLRDNORM;
191 	mutex_unlock(&debug_info->log_mutex);
192 
193 	return mask;
194 }
195 
196 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
197 {
198 	return 0;
199 }
200 
201 static ssize_t cros_ec_pdinfo_read(struct file *file,
202 				   char __user *user_buf,
203 				   size_t count,
204 				   loff_t *ppos)
205 {
206 	char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
207 	struct cros_ec_debugfs *debug_info = file->private_data;
208 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
209 	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
210 			MAX(sizeof(struct ec_response_usb_pd_control_v1),
211 			    sizeof(struct ec_params_usb_pd_control)));
212 	struct ec_response_usb_pd_control_v1 *resp =
213 			(struct ec_response_usb_pd_control_v1 *)msg->data;
214 	struct ec_params_usb_pd_control *params =
215 			(struct ec_params_usb_pd_control *)msg->data;
216 	int i;
217 
218 	msg->command = EC_CMD_USB_PD_CONTROL;
219 	msg->version = 1;
220 	msg->insize = sizeof(*resp);
221 	msg->outsize = sizeof(*params);
222 
223 	/*
224 	 * Read status from all PD ports until failure, typically caused
225 	 * by attempting to read status on a port that doesn't exist.
226 	 */
227 	for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
228 		params->port = i;
229 		params->role = 0;
230 		params->mux = 0;
231 		params->swap = 0;
232 
233 		if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
234 			break;
235 
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,
239 			       resp->polarity);
240 	}
241 
242 	return simple_read_from_buffer(user_buf, count, ppos,
243 				       read_buf, p - read_buf);
244 }
245 
246 static bool cros_ec_uptime_is_supported(struct cros_ec_device *ec_dev)
247 {
248 	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
249 			sizeof(struct ec_response_uptime_info));
250 	int ret;
251 
252 	msg->command = EC_CMD_GET_UPTIME_INFO;
253 	msg->insize = sizeof(struct ec_response_uptime_info);
254 
255 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
256 	if (ret == -EPROTO && msg->result == EC_RES_INVALID_COMMAND)
257 		return false;
258 
259 	/* Other errors maybe a transient error, do not rule about support. */
260 	return true;
261 }
262 
263 static ssize_t cros_ec_uptime_read(struct file *file, char __user *user_buf,
264 				   size_t count, loff_t *ppos)
265 {
266 	struct cros_ec_debugfs *debug_info = file->private_data;
267 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
268 	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
269 			sizeof(struct ec_response_uptime_info));
270 	struct ec_response_uptime_info *resp =
271 				(struct ec_response_uptime_info *)msg->data;
272 	char read_buf[32];
273 	int ret;
274 
275 	msg->command = EC_CMD_GET_UPTIME_INFO;
276 	msg->insize = sizeof(*resp);
277 
278 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
279 	if (ret < 0)
280 		return ret;
281 
282 	ret = scnprintf(read_buf, sizeof(read_buf), "%u\n",
283 			resp->time_since_ec_boot_ms);
284 
285 	return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
286 }
287 
288 static const struct file_operations cros_ec_console_log_fops = {
289 	.owner = THIS_MODULE,
290 	.open = cros_ec_console_log_open,
291 	.read = cros_ec_console_log_read,
292 	.poll = cros_ec_console_log_poll,
293 	.release = cros_ec_console_log_release,
294 };
295 
296 static const struct file_operations cros_ec_pdinfo_fops = {
297 	.owner = THIS_MODULE,
298 	.open = simple_open,
299 	.read = cros_ec_pdinfo_read,
300 	.llseek = default_llseek,
301 };
302 
303 static const struct file_operations cros_ec_uptime_fops = {
304 	.owner = THIS_MODULE,
305 	.open = simple_open,
306 	.read = cros_ec_uptime_read,
307 	.llseek = default_llseek,
308 };
309 
310 static int ec_read_version_supported(struct cros_ec_dev *ec)
311 {
312 	struct ec_params_get_cmd_versions_v1 *params;
313 	struct ec_response_get_cmd_versions *response;
314 	int ret;
315 
316 	struct cros_ec_command *msg;
317 
318 	msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
319 		GFP_KERNEL);
320 	if (!msg)
321 		return 0;
322 
323 	msg->version = 1;
324 	msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
325 	msg->outsize = sizeof(*params);
326 	msg->insize = sizeof(*response);
327 
328 	params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
329 	params->cmd = EC_CMD_CONSOLE_READ;
330 	response = (struct ec_response_get_cmd_versions *)msg->data;
331 
332 	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
333 	      response->version_mask & EC_VER_MASK(1);
334 
335 	kfree(msg);
336 
337 	return ret;
338 }
339 
340 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
341 {
342 	struct cros_ec_dev *ec = debug_info->ec;
343 	char *buf;
344 	int read_params_size;
345 	int read_response_size;
346 
347 	/*
348 	 * If the console log feature is not supported return silently and
349 	 * don't create the console_log entry.
350 	 */
351 	if (!ec_read_version_supported(ec))
352 		return 0;
353 
354 	buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
355 	if (!buf)
356 		return -ENOMEM;
357 
358 	read_params_size = sizeof(struct ec_params_console_read_v1);
359 	read_response_size = ec->ec_dev->max_response;
360 	debug_info->read_msg = devm_kzalloc(ec->dev,
361 		sizeof(*debug_info->read_msg) +
362 			max(read_params_size, read_response_size), GFP_KERNEL);
363 	if (!debug_info->read_msg)
364 		return -ENOMEM;
365 
366 	debug_info->read_msg->version = 1;
367 	debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
368 	debug_info->read_msg->outsize = read_params_size;
369 	debug_info->read_msg->insize = read_response_size;
370 
371 	debug_info->log_buffer.buf = buf;
372 	debug_info->log_buffer.head = 0;
373 	debug_info->log_buffer.tail = 0;
374 
375 	mutex_init(&debug_info->log_mutex);
376 
377 	debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
378 			    debug_info, &cros_ec_console_log_fops);
379 
380 	INIT_DELAYED_WORK(&debug_info->log_poll_work,
381 			  cros_ec_console_log_work);
382 	schedule_delayed_work(&debug_info->log_poll_work, 0);
383 
384 	return 0;
385 }
386 
387 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
388 {
389 	if (debug_info->log_buffer.buf) {
390 		cancel_delayed_work_sync(&debug_info->log_poll_work);
391 		mutex_destroy(&debug_info->log_mutex);
392 	}
393 }
394 
395 /*
396  * Returns the size of the panicinfo data fetched from the EC
397  */
398 static int cros_ec_get_panicinfo(struct cros_ec_device *ec_dev, uint8_t *data,
399 				 int data_size)
400 {
401 	int ret;
402 	struct cros_ec_command *msg;
403 
404 	if (!data || data_size <= 0 || data_size > ec_dev->max_response)
405 		return -EINVAL;
406 
407 	msg = kzalloc(sizeof(*msg) + data_size, GFP_KERNEL);
408 	if (!msg)
409 		return -ENOMEM;
410 
411 	msg->command = EC_CMD_GET_PANIC_INFO;
412 	msg->insize = data_size;
413 
414 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
415 	if (ret < 0)
416 		goto free;
417 
418 	memcpy(data, msg->data, data_size);
419 
420 free:
421 	kfree(msg);
422 	return ret;
423 }
424 
425 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
426 {
427 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
428 	int ret;
429 	void *data;
430 
431 	data = devm_kzalloc(debug_info->ec->dev, ec_dev->max_response,
432 			    GFP_KERNEL);
433 	if (!data)
434 		return -ENOMEM;
435 
436 	ret = cros_ec_get_panicinfo(ec_dev, data, ec_dev->max_response);
437 	if (ret < 0) {
438 		ret = 0;
439 		goto free;
440 	}
441 
442 	/* No panic data */
443 	if (ret == 0)
444 		goto free;
445 
446 	debug_info->panicinfo_blob.data = data;
447 	debug_info->panicinfo_blob.size = ret;
448 
449 	debugfs_create_blob("panicinfo", 0444, debug_info->dir,
450 			    &debug_info->panicinfo_blob);
451 
452 	return 0;
453 
454 free:
455 	devm_kfree(debug_info->ec->dev, data);
456 	return ret;
457 }
458 
459 static int cros_ec_debugfs_panic_event(struct notifier_block *nb,
460 				       unsigned long queued_during_suspend, void *_notify)
461 {
462 	struct cros_ec_debugfs *debug_info =
463 		container_of(nb, struct cros_ec_debugfs, notifier_panic);
464 
465 	if (debug_info->log_buffer.buf) {
466 		/* Force log poll work to run immediately */
467 		mod_delayed_work(debug_info->log_poll_work.wq, &debug_info->log_poll_work, 0);
468 		/* Block until log poll work finishes */
469 		flush_delayed_work(&debug_info->log_poll_work);
470 	}
471 
472 	return NOTIFY_DONE;
473 }
474 
475 static int cros_ec_debugfs_probe(struct platform_device *pd)
476 {
477 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
478 	struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
479 	const char *name = ec_platform->ec_name;
480 	struct cros_ec_debugfs *debug_info;
481 	int ret;
482 
483 	debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
484 	if (!debug_info)
485 		return -ENOMEM;
486 
487 	debug_info->ec = ec;
488 	debug_info->dir = debugfs_create_dir(name, NULL);
489 
490 	ret = cros_ec_create_panicinfo(debug_info);
491 	if (ret)
492 		goto remove_debugfs;
493 
494 	ret = cros_ec_create_console_log(debug_info);
495 	if (ret)
496 		goto remove_debugfs;
497 
498 	debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
499 			    &cros_ec_pdinfo_fops);
500 
501 	if (cros_ec_uptime_is_supported(ec->ec_dev))
502 		debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
503 				    &cros_ec_uptime_fops);
504 
505 	debugfs_create_x32("last_resume_result", 0444, debug_info->dir,
506 			   &ec->ec_dev->last_resume_result);
507 
508 	debugfs_create_u16("suspend_timeout_ms", 0664, debug_info->dir,
509 			   &ec->ec_dev->suspend_timeout_ms);
510 
511 	debug_info->notifier_panic.notifier_call = cros_ec_debugfs_panic_event;
512 	ret = blocking_notifier_chain_register(&ec->ec_dev->panic_notifier,
513 					       &debug_info->notifier_panic);
514 	if (ret)
515 		goto remove_debugfs;
516 
517 	ec->debug_info = debug_info;
518 
519 	dev_set_drvdata(&pd->dev, ec);
520 
521 	return 0;
522 
523 remove_debugfs:
524 	debugfs_remove_recursive(debug_info->dir);
525 	return ret;
526 }
527 
528 static void cros_ec_debugfs_remove(struct platform_device *pd)
529 {
530 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
531 
532 	debugfs_remove_recursive(ec->debug_info->dir);
533 	cros_ec_cleanup_console_log(ec->debug_info);
534 }
535 
536 static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
537 {
538 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
539 
540 	if (ec->debug_info->log_buffer.buf)
541 		cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
542 
543 	return 0;
544 }
545 
546 static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
547 {
548 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
549 
550 	if (ec->debug_info->log_buffer.buf)
551 		schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
552 
553 	return 0;
554 }
555 
556 static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
557 			 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
558 
559 static const struct platform_device_id cros_ec_debugfs_id[] = {
560 	{ DRV_NAME, 0 },
561 	{}
562 };
563 MODULE_DEVICE_TABLE(platform, cros_ec_debugfs_id);
564 
565 static struct platform_driver cros_ec_debugfs_driver = {
566 	.driver = {
567 		.name = DRV_NAME,
568 		.pm = &cros_ec_debugfs_pm_ops,
569 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
570 	},
571 	.probe = cros_ec_debugfs_probe,
572 	.remove = cros_ec_debugfs_remove,
573 	.id_table = cros_ec_debugfs_id,
574 };
575 
576 module_platform_driver(cros_ec_debugfs_driver);
577 
578 MODULE_LICENSE("GPL");
579 MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
580