1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc.
12 DECLARE_GLOBAL_DATA_PTR;
14 /* Support up to the machine word length for now */
15 typedef ulong iovalue_t;
27 * struct iotrace_record - Holds a single I/O trace record
29 * @flags: I/O access type
30 * @addr: Address of access
31 * @value: Value written or read
33 struct iotrace_record {
34 enum iotrace_flags flags;
40 * struct iotrace - current trace status and checksum
42 * @start: Start address of iotrace buffer
43 * @size: Size of iotrace buffer in bytes
44 * @offset: Current write offset into iotrace buffer
45 * @region_start: Address of IO region to trace
46 * @region_size: Size of region to trace. if 0 will trace all address space
47 * @crc32: Current value of CRC chceksum of trace records
48 * @enabled: true if enabled, false if disabled
50 static struct iotrace {
60 static void add_record(int flags, const void *ptr, ulong value)
62 struct iotrace_record srec, *rec = &srec;
65 * We don't support iotrace before relocation. Since the trace buffer
66 * is set up by a command, it can't be enabled at present. To change
67 * this we would need to set the iotrace buffer at build-time. See
68 * lib/trace.c for how this might be done if you are interested.
70 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
73 if (iotrace.region_size)
74 if ((ulong)ptr < iotrace.region_start ||
75 (ulong)ptr > iotrace.region_start + iotrace.region_size)
78 /* Store it if there is room */
79 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
80 rec = (struct iotrace_record *)map_sysmem(
81 iotrace.start + iotrace.offset,
86 rec->addr = map_to_sysmem(ptr);
89 /* Update our checksum */
90 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
93 iotrace.offset += sizeof(struct iotrace_record);
96 u32 iotrace_readl(const void *ptr)
101 add_record(IOT_32 | IOT_READ, ptr, v);
106 void iotrace_writel(ulong value, const void *ptr)
108 add_record(IOT_32 | IOT_WRITE, ptr, value);
112 u16 iotrace_readw(const void *ptr)
117 add_record(IOT_16 | IOT_READ, ptr, v);
122 void iotrace_writew(ulong value, const void *ptr)
124 add_record(IOT_16 | IOT_WRITE, ptr, value);
128 u8 iotrace_readb(const void *ptr)
133 add_record(IOT_8 | IOT_READ, ptr, v);
138 void iotrace_writeb(ulong value, const void *ptr)
140 add_record(IOT_8 | IOT_WRITE, ptr, value);
144 void iotrace_reset_checksum(void)
149 u32 iotrace_get_checksum(void)
151 return iotrace.crc32;
154 void iotrace_set_region(ulong start, ulong size)
156 iotrace.region_start = start;
157 iotrace.region_size = size;
160 void iotrace_reset_region(void)
162 iotrace.region_start = 0;
163 iotrace.region_size = 0;
166 void iotrace_get_region(ulong *start, ulong *size)
168 *start = iotrace.region_start;
169 *size = iotrace.region_size;
172 void iotrace_set_enabled(int enable)
174 iotrace.enabled = enable;
177 int iotrace_get_enabled(void)
179 return iotrace.enabled;
182 void iotrace_set_buffer(ulong start, ulong size)
184 iotrace.start = start;
190 void iotrace_get_buffer(ulong *start, ulong *size, ulong *offset, ulong *count)
192 *start = iotrace.start;
193 *size = iotrace.size;
194 *offset = iotrace.offset;
195 *count = iotrace.offset / sizeof(struct iotrace_record);