blob: 2f03a6082e893f0c2493c16c749a17e21442c692 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassaa532332014-06-11 23:29:41 -06002/*
3 * Copyright (c) 2014 Google, Inc.
Simon Glassaa532332014-06-11 23:29:41 -06004 */
5
6#define IOTRACE_IMPL
7
8#include <common.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -05009#include <mapmem.h>
Simon Glassaa532332014-06-11 23:29:41 -060010#include <asm/io.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14/* Support up to the machine word length for now */
15typedef ulong iovalue_t;
16
17enum iotrace_flags {
18 IOT_8 = 0,
19 IOT_16,
20 IOT_32,
21
22 IOT_READ = 0 << 3,
23 IOT_WRITE = 1 << 3,
24};
25
26/**
27 * struct iotrace_record - Holds a single I/O trace record
28 *
29 * @flags: I/O access type
Ramon Friedb5e0e362018-05-30 23:09:59 +030030 * @timestamp: Timestamp of access
Simon Glassaa532332014-06-11 23:29:41 -060031 * @addr: Address of access
32 * @value: Value written or read
33 */
34struct iotrace_record {
35 enum iotrace_flags flags;
Ramon Friedb5e0e362018-05-30 23:09:59 +030036 u64 timestamp;
Simon Glassaa532332014-06-11 23:29:41 -060037 phys_addr_t addr;
38 iovalue_t value;
39};
40
41/**
42 * struct iotrace - current trace status and checksum
43 *
44 * @start: Start address of iotrace buffer
45 * @size: Size of iotrace buffer in bytes
46 * @offset: Current write offset into iotrace buffer
Ramon Frieda74440b2018-05-30 23:09:58 +030047 * @region_start: Address of IO region to trace
48 * @region_size: Size of region to trace. if 0 will trace all address space
Simon Glassaa532332014-06-11 23:29:41 -060049 * @crc32: Current value of CRC chceksum of trace records
50 * @enabled: true if enabled, false if disabled
51 */
52static struct iotrace {
53 ulong start;
54 ulong size;
55 ulong offset;
Ramon Frieda74440b2018-05-30 23:09:58 +030056 ulong region_start;
57 ulong region_size;
Simon Glassaa532332014-06-11 23:29:41 -060058 u32 crc32;
59 bool enabled;
60} iotrace;
61
62static void add_record(int flags, const void *ptr, ulong value)
63{
64 struct iotrace_record srec, *rec = &srec;
65
66 /*
67 * We don't support iotrace before relocation. Since the trace buffer
68 * is set up by a command, it can't be enabled at present. To change
69 * this we would need to set the iotrace buffer at build-time. See
70 * lib/trace.c for how this might be done if you are interested.
71 */
72 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
73 return;
74
Ramon Frieda74440b2018-05-30 23:09:58 +030075 if (iotrace.region_size)
76 if ((ulong)ptr < iotrace.region_start ||
77 (ulong)ptr > iotrace.region_start + iotrace.region_size)
78 return;
79
Simon Glassaa532332014-06-11 23:29:41 -060080 /* Store it if there is room */
81 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
82 rec = (struct iotrace_record *)map_sysmem(
83 iotrace.start + iotrace.offset,
84 sizeof(value));
85 }
Ramon Friedb5e0e362018-05-30 23:09:59 +030086 rec->timestamp = timer_get_us();
Simon Glassaa532332014-06-11 23:29:41 -060087 rec->flags = flags;
88 rec->addr = map_to_sysmem(ptr);
89 rec->value = value;
90
91 /* Update our checksum */
92 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
93 sizeof(*rec));
94
95 iotrace.offset += sizeof(struct iotrace_record);
96}
97
98u32 iotrace_readl(const void *ptr)
99{
100 u32 v;
101
102 v = readl(ptr);
103 add_record(IOT_32 | IOT_READ, ptr, v);
104
105 return v;
106}
107
108void iotrace_writel(ulong value, const void *ptr)
109{
110 add_record(IOT_32 | IOT_WRITE, ptr, value);
111 writel(value, ptr);
112}
113
114u16 iotrace_readw(const void *ptr)
115{
116 u32 v;
117
118 v = readw(ptr);
119 add_record(IOT_16 | IOT_READ, ptr, v);
120
121 return v;
122}
123
124void iotrace_writew(ulong value, const void *ptr)
125{
126 add_record(IOT_16 | IOT_WRITE, ptr, value);
127 writew(value, ptr);
128}
129
130u8 iotrace_readb(const void *ptr)
131{
132 u32 v;
133
134 v = readb(ptr);
135 add_record(IOT_8 | IOT_READ, ptr, v);
136
137 return v;
138}
139
140void iotrace_writeb(ulong value, const void *ptr)
141{
142 add_record(IOT_8 | IOT_WRITE, ptr, value);
143 writeb(value, ptr);
144}
145
146void iotrace_reset_checksum(void)
147{
148 iotrace.crc32 = 0;
149}
150
151u32 iotrace_get_checksum(void)
152{
153 return iotrace.crc32;
154}
155
Ramon Frieda74440b2018-05-30 23:09:58 +0300156void iotrace_set_region(ulong start, ulong size)
157{
158 iotrace.region_start = start;
159 iotrace.region_size = size;
160}
161
162void iotrace_reset_region(void)
163{
164 iotrace.region_start = 0;
165 iotrace.region_size = 0;
166}
167
168void iotrace_get_region(ulong *start, ulong *size)
169{
170 *start = iotrace.region_start;
171 *size = iotrace.region_size;
172}
173
Simon Glassaa532332014-06-11 23:29:41 -0600174void iotrace_set_enabled(int enable)
175{
176 iotrace.enabled = enable;
177}
178
179int iotrace_get_enabled(void)
180{
181 return iotrace.enabled;
182}
183
184void iotrace_set_buffer(ulong start, ulong size)
185{
186 iotrace.start = start;
187 iotrace.size = size;
188 iotrace.offset = 0;
189 iotrace.crc32 = 0;
190}
191
192void iotrace_get_buffer(ulong *start, ulong *size, ulong *offset, ulong *count)
193{
194 *start = iotrace.start;
195 *size = iotrace.size;
196 *offset = iotrace.offset;
197 *count = iotrace.offset / sizeof(struct iotrace_record);
198}