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