cmd: iotrace: add dump trace command
[platform/kernel/u-boot.git] / cmd / iotrace.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <iotrace.h>
9
10 static void do_print_stats(void)
11 {
12         ulong start, size, offset, count;
13
14         printf("iotrace is %sabled\n", iotrace_get_enabled() ? "en" : "dis");
15         iotrace_get_buffer(&start, &size, &offset, &count);
16         printf("Start:  %08lx\n", start);
17         printf("Size:   %08lx\n", size);
18         iotrace_get_region(&start, &size);
19         printf("Region: %08lx\n", start);
20         printf("Size:   %08lx\n", size);
21         printf("Offset: %08lx\n", offset);
22         printf("Output: %08lx\n", start + offset);
23         printf("Count:  %08lx\n", count);
24         printf("CRC32:  %08lx\n", (ulong)iotrace_get_checksum());
25 }
26
27 static void do_print_trace(void)
28 {
29         ulong start, size, offset, count;
30
31         struct iotrace_record *cur_record;
32
33         iotrace_get_buffer(&start, &size, &offset, &count);
34
35         if (!start || !size || !count)
36                 return;
37
38         printf("Timestamp  Value          Address\n");
39
40         cur_record = (struct iotrace_record *)start;
41         for (int i = 0; i < count; i++) {
42                 if (cur_record->flags & IOT_WRITE)
43                         printf("%08llu: 0x%08lx --> 0x%08llx\n",
44                                cur_record->timestamp,
45                                         cur_record->value,
46                                         (unsigned long long)cur_record->addr);
47                 else
48                         printf("%08llu: 0x%08lx <-- 0x%08llx\n",
49                                cur_record->timestamp,
50                                         cur_record->value,
51                                         (unsigned long long)cur_record->addr);
52
53                 cur_record++;
54         }
55 }
56
57 static int do_set_buffer(int argc, char * const argv[])
58 {
59         ulong addr = 0, size = 0;
60
61         if (argc == 2) {
62                 addr = simple_strtoul(*argv++, NULL, 16);
63                 size = simple_strtoul(*argv++, NULL, 16);
64         } else if (argc != 0) {
65                 return CMD_RET_USAGE;
66         }
67
68         iotrace_set_buffer(addr, size);
69
70         return 0;
71 }
72
73 static int do_set_region(int argc, char * const argv[])
74 {
75         ulong addr = 0, size = 0;
76
77         if (argc == 2) {
78                 addr = simple_strtoul(*argv++, NULL, 16);
79                 size = simple_strtoul(*argv++, NULL, 16);
80         } else if (argc != 0) {
81                 return CMD_RET_USAGE;
82         }
83
84         iotrace_set_region(addr, size);
85
86         return 0;
87 }
88
89 int do_iotrace(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
90 {
91         const char *cmd = argc < 2 ? NULL : argv[1];
92
93         if (!cmd)
94                 return cmd_usage(cmdtp);
95         switch (*cmd) {
96         case 'b':
97                 return do_set_buffer(argc - 2, argv + 2);
98         case 'l':
99                 return do_set_region(argc - 2, argv + 2);
100         case 'p':
101                 iotrace_set_enabled(0);
102                 break;
103         case 'r':
104                 iotrace_set_enabled(1);
105                 break;
106         case 's':
107                 do_print_stats();
108                 break;
109         case 'd':
110                 do_print_trace();
111                 break;
112         default:
113                 return CMD_RET_USAGE;
114         }
115
116         return 0;
117 }
118
119 U_BOOT_CMD(
120         iotrace,        4,      1,      do_iotrace,
121         "iotrace utility commands",
122         "stats                        - display iotrace stats\n"
123         "iotrace buffer <address> <size>      - set iotrace buffer\n"
124         "iotrace limit <address> <size>       - set iotrace region limit\n"
125         "iotrace pause                        - pause tracing\n"
126         "iotrace resume                       - resume tracing\n"
127         "iotrace dump                         - dump iotrace buffer"
128 );