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