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