1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc.
11 #include <asm/global_data.h>
13 #include <linux/bug.h>
14 #include <u-boot/crc.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 * struct iotrace - current trace status and checksum
21 * @start: Start address of iotrace buffer
22 * @size: Actual size of iotrace buffer in bytes
23 * @needed_size: Needed of iotrace buffer in bytes
24 * @offset: Current write offset into iotrace buffer
25 * @region_start: Address of IO region to trace
26 * @region_size: Size of region to trace. if 0 will trace all address space
27 * @crc32: Current value of CRC chceksum of trace records
28 * @enabled: true if enabled, false if disabled
30 static struct iotrace {
41 static void add_record(int flags, const void *ptr, ulong value)
43 struct iotrace_record srec, *rec = &srec;
46 * We don't support iotrace before relocation. Since the trace buffer
47 * is set up by a command, it can't be enabled at present. To change
48 * this we would need to set the iotrace buffer at build-time. See
49 * lib/trace.c for how this might be done if you are interested.
51 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
54 if (iotrace.region_size)
55 if ((ulong)ptr < iotrace.region_start ||
56 (ulong)ptr > iotrace.region_start + iotrace.region_size)
59 /* Store it if there is room */
60 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
61 rec = (struct iotrace_record *)map_sysmem(
62 iotrace.start + iotrace.offset,
65 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
66 iotrace.needed_size += sizeof(struct iotrace_record);
70 rec->timestamp = timer_get_us();
72 rec->addr = map_to_sysmem(ptr);
75 /* Update our checksum */
76 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
79 iotrace.needed_size += sizeof(struct iotrace_record);
80 iotrace.offset += sizeof(struct iotrace_record);
83 u32 iotrace_readl(const void *ptr)
88 add_record(IOT_32 | IOT_READ, ptr, v);
93 void iotrace_writel(ulong value, void *ptr)
95 add_record(IOT_32 | IOT_WRITE, ptr, value);
99 u16 iotrace_readw(const void *ptr)
104 add_record(IOT_16 | IOT_READ, ptr, v);
109 void iotrace_writew(ulong value, void *ptr)
111 add_record(IOT_16 | IOT_WRITE, ptr, value);
115 u8 iotrace_readb(const void *ptr)
120 add_record(IOT_8 | IOT_READ, ptr, v);
125 void iotrace_writeb(ulong value, void *ptr)
127 add_record(IOT_8 | IOT_WRITE, ptr, value);
131 void iotrace_reset_checksum(void)
136 u32 iotrace_get_checksum(void)
138 return iotrace.crc32;
141 void iotrace_set_region(ulong start, ulong size)
143 iotrace.region_start = start;
144 iotrace.region_size = size;
147 void iotrace_reset_region(void)
149 iotrace.region_start = 0;
150 iotrace.region_size = 0;
153 void iotrace_get_region(ulong *start, ulong *size)
155 *start = iotrace.region_start;
156 *size = iotrace.region_size;
159 void iotrace_set_enabled(int enable)
161 iotrace.enabled = enable;
164 int iotrace_get_enabled(void)
166 return iotrace.enabled;
169 void iotrace_set_buffer(ulong start, ulong size)
171 iotrace.start = start;
177 void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
179 *start = iotrace.start;
180 *size = iotrace.size;
181 *needed_size = iotrace.needed_size;
182 *offset = iotrace.offset;
183 *count = iotrace.offset / sizeof(struct iotrace_record);