1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2014 Google, Inc.
4 */
5
6 #define IOTRACE_IMPL
7
8 #include <mapmem.h>
9 #include <time.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <linux/bug.h>
13 #include <u-boot/crc.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 /**
18 * struct iotrace - current trace status and checksum
19 *
20 * @start: Start address of iotrace buffer
21 * @size: Actual size of iotrace buffer in bytes
22 * @needed_size: Needed of iotrace buffer in bytes
23 * @offset: Current write offset into iotrace buffer
24 * @region_start: Address of IO region to trace
25 * @region_size: Size of region to trace. if 0 will trace all address space
26 * @crc32: Current value of CRC chceksum of trace records
27 * @enabled: true if enabled, false if disabled
28 */
29 static struct iotrace {
30 ulong start;
31 ulong size;
32 ulong needed_size;
33 ulong offset;
34 ulong region_start;
35 ulong region_size;
36 u32 crc32;
37 bool enabled;
38 } iotrace;
39
add_record(int flags,const void * ptr,ulong value)40 static void add_record(int flags, const void *ptr, ulong value)
41 {
42 struct iotrace_record srec, *rec = &srec;
43
44 /*
45 * We don't support iotrace before relocation. Since the trace buffer
46 * is set up by a command, it can't be enabled at present. To change
47 * this we would need to set the iotrace buffer at build-time. See
48 * lib/trace.c for how this might be done if you are interested.
49 */
50 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
51 return;
52
53 if (iotrace.region_size)
54 if ((ulong)ptr < iotrace.region_start ||
55 (ulong)ptr > iotrace.region_start + iotrace.region_size)
56 return;
57
58 /* Store it if there is room */
59 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
60 rec = (struct iotrace_record *)map_sysmem(
61 iotrace.start + iotrace.offset,
62 sizeof(value));
63 } else {
64 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
65 iotrace.needed_size += sizeof(struct iotrace_record);
66 return;
67 }
68
69 rec->timestamp = timer_get_us();
70 rec->flags = flags;
71 rec->addr = map_to_sysmem(ptr);
72 rec->value = value;
73
74 /* Update our checksum */
75 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
76 sizeof(*rec));
77
78 iotrace.needed_size += sizeof(struct iotrace_record);
79 iotrace.offset += sizeof(struct iotrace_record);
80 }
81
iotrace_readl(const void * ptr)82 u32 iotrace_readl(const void *ptr)
83 {
84 u32 v;
85
86 v = readl(ptr);
87 add_record(IOT_32 | IOT_READ, ptr, v);
88
89 return v;
90 }
91
iotrace_writel(ulong value,void * ptr)92 void iotrace_writel(ulong value, void *ptr)
93 {
94 add_record(IOT_32 | IOT_WRITE, ptr, value);
95 writel(value, ptr);
96 }
97
iotrace_readw(const void * ptr)98 u16 iotrace_readw(const void *ptr)
99 {
100 u32 v;
101
102 v = readw(ptr);
103 add_record(IOT_16 | IOT_READ, ptr, v);
104
105 return v;
106 }
107
iotrace_writew(ulong value,void * ptr)108 void iotrace_writew(ulong value, void *ptr)
109 {
110 add_record(IOT_16 | IOT_WRITE, ptr, value);
111 writew(value, ptr);
112 }
113
iotrace_readb(const void * ptr)114 u8 iotrace_readb(const void *ptr)
115 {
116 u32 v;
117
118 v = readb(ptr);
119 add_record(IOT_8 | IOT_READ, ptr, v);
120
121 return v;
122 }
123
iotrace_writeb(ulong value,void * ptr)124 void iotrace_writeb(ulong value, void *ptr)
125 {
126 add_record(IOT_8 | IOT_WRITE, ptr, value);
127 writeb(value, ptr);
128 }
129
iotrace_reset_checksum(void)130 void iotrace_reset_checksum(void)
131 {
132 iotrace.crc32 = 0;
133 }
134
iotrace_get_checksum(void)135 u32 iotrace_get_checksum(void)
136 {
137 return iotrace.crc32;
138 }
139
iotrace_set_region(ulong start,ulong size)140 void iotrace_set_region(ulong start, ulong size)
141 {
142 iotrace.region_start = start;
143 iotrace.region_size = size;
144 }
145
iotrace_reset_region(void)146 void iotrace_reset_region(void)
147 {
148 iotrace.region_start = 0;
149 iotrace.region_size = 0;
150 }
151
iotrace_get_region(ulong * start,ulong * size)152 void iotrace_get_region(ulong *start, ulong *size)
153 {
154 *start = iotrace.region_start;
155 *size = iotrace.region_size;
156 }
157
iotrace_set_enabled(int enable)158 void iotrace_set_enabled(int enable)
159 {
160 iotrace.enabled = enable;
161 }
162
iotrace_get_enabled(void)163 int iotrace_get_enabled(void)
164 {
165 return iotrace.enabled;
166 }
167
iotrace_set_buffer(ulong start,ulong size)168 void iotrace_set_buffer(ulong start, ulong size)
169 {
170 iotrace.start = start;
171 iotrace.size = size;
172 iotrace.offset = 0;
173 iotrace.crc32 = 0;
174 }
175
iotrace_get_buffer(ulong * start,ulong * size,ulong * needed_size,ulong * offset,ulong * count)176 void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
177 {
178 *start = iotrace.start;
179 *size = iotrace.size;
180 *needed_size = iotrace.needed_size;
181 *offset = iotrace.offset;
182 *count = iotrace.offset / sizeof(struct iotrace_record);
183 }
184