1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc.
11 #include <u-boot/crc.h>
13 DECLARE_GLOBAL_DATA_PTR;
16 * struct iotrace - current trace status and checksum
18 * @start: Start address of iotrace buffer
19 * @size: Actual size of iotrace buffer in bytes
20 * @needed_size: Needed of iotrace buffer in bytes
21 * @offset: Current write offset into iotrace buffer
22 * @region_start: Address of IO region to trace
23 * @region_size: Size of region to trace. if 0 will trace all address space
24 * @crc32: Current value of CRC chceksum of trace records
25 * @enabled: true if enabled, false if disabled
27 static struct iotrace {
38 static void add_record(int flags, const void *ptr, ulong value)
40 struct iotrace_record srec, *rec = &srec;
43 * We don't support iotrace before relocation. Since the trace buffer
44 * is set up by a command, it can't be enabled at present. To change
45 * this we would need to set the iotrace buffer at build-time. See
46 * lib/trace.c for how this might be done if you are interested.
48 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
51 if (iotrace.region_size)
52 if ((ulong)ptr < iotrace.region_start ||
53 (ulong)ptr > iotrace.region_start + iotrace.region_size)
56 /* Store it if there is room */
57 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
58 rec = (struct iotrace_record *)map_sysmem(
59 iotrace.start + iotrace.offset,
62 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
63 iotrace.needed_size += sizeof(struct iotrace_record);
67 rec->timestamp = timer_get_us();
69 rec->addr = map_to_sysmem(ptr);
72 /* Update our checksum */
73 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
76 iotrace.needed_size += sizeof(struct iotrace_record);
77 iotrace.offset += sizeof(struct iotrace_record);
80 u32 iotrace_readl(const void *ptr)
85 add_record(IOT_32 | IOT_READ, ptr, v);
90 void iotrace_writel(ulong value, void *ptr)
92 add_record(IOT_32 | IOT_WRITE, ptr, value);
96 u16 iotrace_readw(const void *ptr)
101 add_record(IOT_16 | IOT_READ, ptr, v);
106 void iotrace_writew(ulong value, void *ptr)
108 add_record(IOT_16 | IOT_WRITE, ptr, value);
112 u8 iotrace_readb(const void *ptr)
117 add_record(IOT_8 | IOT_READ, ptr, v);
122 void iotrace_writeb(ulong value, void *ptr)
124 add_record(IOT_8 | IOT_WRITE, ptr, value);
128 void iotrace_reset_checksum(void)
133 u32 iotrace_get_checksum(void)
135 return iotrace.crc32;
138 void iotrace_set_region(ulong start, ulong size)
140 iotrace.region_start = start;
141 iotrace.region_size = size;
144 void iotrace_reset_region(void)
146 iotrace.region_start = 0;
147 iotrace.region_size = 0;
150 void iotrace_get_region(ulong *start, ulong *size)
152 *start = iotrace.region_start;
153 *size = iotrace.region_size;
156 void iotrace_set_enabled(int enable)
158 iotrace.enabled = enable;
161 int iotrace_get_enabled(void)
163 return iotrace.enabled;
166 void iotrace_set_buffer(ulong start, ulong size)
168 iotrace.start = start;
174 void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
176 *start = iotrace.start;
177 *size = iotrace.size;
178 *needed_size = iotrace.needed_size;
179 *offset = iotrace.offset;
180 *count = iotrace.offset / sizeof(struct iotrace_record);