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