crc32: Use the crc.h header for crc functions
[oweals/u-boot.git] / common / iotrace.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc.
4  */
5
6 #define IOTRACE_IMPL
7
8 #include <common.h>
9 #include <mapmem.h>
10 #include <asm/io.h>
11 #include <u-boot/crc.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /**
16  * struct iotrace - current trace status and checksum
17  *
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
26  */
27 static struct iotrace {
28         ulong start;
29         ulong size;
30         ulong needed_size;
31         ulong offset;
32         ulong region_start;
33         ulong region_size;
34         u32 crc32;
35         bool enabled;
36 } iotrace;
37
38 static void add_record(int flags, const void *ptr, ulong value)
39 {
40         struct iotrace_record srec, *rec = &srec;
41
42         /*
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.
47          */
48         if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
49                 return;
50
51         if (iotrace.region_size)
52                 if ((ulong)ptr < iotrace.region_start ||
53                     (ulong)ptr > iotrace.region_start + iotrace.region_size)
54                         return;
55
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,
60                                         sizeof(value));
61         } else {
62                 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
63                 iotrace.needed_size += sizeof(struct iotrace_record);
64                 return;
65         }
66
67         rec->timestamp = timer_get_us();
68         rec->flags = flags;
69         rec->addr = map_to_sysmem(ptr);
70         rec->value = value;
71
72         /* Update our checksum */
73         iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
74                               sizeof(*rec));
75
76         iotrace.needed_size += sizeof(struct iotrace_record);
77         iotrace.offset += sizeof(struct iotrace_record);
78 }
79
80 u32 iotrace_readl(const void *ptr)
81 {
82         u32 v;
83
84         v = readl(ptr);
85         add_record(IOT_32 | IOT_READ, ptr, v);
86
87         return v;
88 }
89
90 void iotrace_writel(ulong value, void *ptr)
91 {
92         add_record(IOT_32 | IOT_WRITE, ptr, value);
93         writel(value, ptr);
94 }
95
96 u16 iotrace_readw(const void *ptr)
97 {
98         u32 v;
99
100         v = readw(ptr);
101         add_record(IOT_16 | IOT_READ, ptr, v);
102
103         return v;
104 }
105
106 void iotrace_writew(ulong value, void *ptr)
107 {
108         add_record(IOT_16 | IOT_WRITE, ptr, value);
109         writew(value, ptr);
110 }
111
112 u8 iotrace_readb(const void *ptr)
113 {
114         u32 v;
115
116         v = readb(ptr);
117         add_record(IOT_8 | IOT_READ, ptr, v);
118
119         return v;
120 }
121
122 void iotrace_writeb(ulong value, void *ptr)
123 {
124         add_record(IOT_8 | IOT_WRITE, ptr, value);
125         writeb(value, ptr);
126 }
127
128 void iotrace_reset_checksum(void)
129 {
130         iotrace.crc32 = 0;
131 }
132
133 u32 iotrace_get_checksum(void)
134 {
135         return iotrace.crc32;
136 }
137
138 void iotrace_set_region(ulong start, ulong size)
139 {
140         iotrace.region_start = start;
141         iotrace.region_size = size;
142 }
143
144 void iotrace_reset_region(void)
145 {
146         iotrace.region_start = 0;
147         iotrace.region_size = 0;
148 }
149
150 void iotrace_get_region(ulong *start, ulong *size)
151 {
152         *start = iotrace.region_start;
153         *size = iotrace.region_size;
154 }
155
156 void iotrace_set_enabled(int enable)
157 {
158         iotrace.enabled = enable;
159 }
160
161 int iotrace_get_enabled(void)
162 {
163         return iotrace.enabled;
164 }
165
166 void iotrace_set_buffer(ulong start, ulong size)
167 {
168         iotrace.start = start;
169         iotrace.size = size;
170         iotrace.offset = 0;
171         iotrace.crc32 = 0;
172 }
173
174 void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
175 {
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);
181 }