1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc.
12 #include <linux/bug.h>
13 #include <u-boot/crc.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 * struct iotrace - current trace status and checksum
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
29 static struct iotrace {
40 static void add_record(int flags, const void *ptr, ulong value)
42 struct iotrace_record srec, *rec = &srec;
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.
50 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
53 if (iotrace.region_size)
54 if ((ulong)ptr < iotrace.region_start ||
55 (ulong)ptr > iotrace.region_start + iotrace.region_size)
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,
64 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
65 iotrace.needed_size += sizeof(struct iotrace_record);
69 rec->timestamp = timer_get_us();
71 rec->addr = map_to_sysmem(ptr);
74 /* Update our checksum */
75 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
78 iotrace.needed_size += sizeof(struct iotrace_record);
79 iotrace.offset += sizeof(struct iotrace_record);
82 u32 iotrace_readl(const void *ptr)
87 add_record(IOT_32 | IOT_READ, ptr, v);
92 void iotrace_writel(ulong value, void *ptr)
94 add_record(IOT_32 | IOT_WRITE, ptr, value);
98 u16 iotrace_readw(const void *ptr)
103 add_record(IOT_16 | IOT_READ, ptr, v);
108 void iotrace_writew(ulong value, void *ptr)
110 add_record(IOT_16 | IOT_WRITE, ptr, value);
114 u8 iotrace_readb(const void *ptr)
119 add_record(IOT_8 | IOT_READ, ptr, v);
124 void iotrace_writeb(ulong value, void *ptr)
126 add_record(IOT_8 | IOT_WRITE, ptr, value);
130 void iotrace_reset_checksum(void)
135 u32 iotrace_get_checksum(void)
137 return iotrace.crc32;
140 void iotrace_set_region(ulong start, ulong size)
142 iotrace.region_start = start;
143 iotrace.region_size = size;
146 void iotrace_reset_region(void)
148 iotrace.region_start = 0;
149 iotrace.region_size = 0;
152 void iotrace_get_region(ulong *start, ulong *size)
154 *start = iotrace.region_start;
155 *size = iotrace.region_size;
158 void iotrace_set_enabled(int enable)
160 iotrace.enabled = enable;
163 int iotrace_get_enabled(void)
165 return iotrace.enabled;
168 void iotrace_set_buffer(ulong start, ulong size)
170 iotrace.start = start;
176 void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
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);