blob: 7225ee440418c5e4312c966c293ddca737de39e7 [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 Glass10453152019-11-14 12:57:30 -070010#include <time.h>
Simon Glassaa532332014-06-11 23:29:41 -060011#include <asm/io.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060012#include <linux/bug.h>
Simon Glass3db71102019-11-14 12:57:16 -070013#include <u-boot/crc.h>
Simon Glassaa532332014-06-11 23:29:41 -060014
15DECLARE_GLOBAL_DATA_PTR;
16
Simon Glassaa532332014-06-11 23:29:41 -060017/**
18 * struct iotrace - current trace status and checksum
19 *
20 * @start: Start address of iotrace buffer
Ramon Friede0212df2018-06-08 20:53:27 +030021 * @size: Actual size of iotrace buffer in bytes
22 * @needed_size: Needed of iotrace buffer in bytes
Simon Glassaa532332014-06-11 23:29:41 -060023 * @offset: Current write offset into iotrace buffer
Ramon Frieda74440b2018-05-30 23:09:58 +030024 * @region_start: Address of IO region to trace
25 * @region_size: Size of region to trace. if 0 will trace all address space
Simon Glassaa532332014-06-11 23:29:41 -060026 * @crc32: Current value of CRC chceksum of trace records
27 * @enabled: true if enabled, false if disabled
28 */
29static struct iotrace {
30 ulong start;
31 ulong size;
Ramon Friede0212df2018-06-08 20:53:27 +030032 ulong needed_size;
Simon Glassaa532332014-06-11 23:29:41 -060033 ulong offset;
Ramon Frieda74440b2018-05-30 23:09:58 +030034 ulong region_start;
35 ulong region_size;
Simon Glassaa532332014-06-11 23:29:41 -060036 u32 crc32;
37 bool enabled;
38} iotrace;
39
40static void add_record(int flags, const void *ptr, ulong value)
41{
42 struct iotrace_record srec, *rec = &srec;
43
44 /*
45 * We don't support iotrace before relocation. Since the trace buffer
46 * is set up by a command, it can't be enabled at present. To change
47 * this we would need to set the iotrace buffer at build-time. See
48 * lib/trace.c for how this might be done if you are interested.
49 */
50 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
51 return;
52
Ramon Frieda74440b2018-05-30 23:09:58 +030053 if (iotrace.region_size)
54 if ((ulong)ptr < iotrace.region_start ||
55 (ulong)ptr > iotrace.region_start + iotrace.region_size)
56 return;
57
Simon Glassaa532332014-06-11 23:29:41 -060058 /* Store it if there is room */
59 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
60 rec = (struct iotrace_record *)map_sysmem(
61 iotrace.start + iotrace.offset,
62 sizeof(value));
Ramon Friede0212df2018-06-08 20:53:27 +030063 } else {
64 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
65 iotrace.needed_size += sizeof(struct iotrace_record);
66 return;
Simon Glassaa532332014-06-11 23:29:41 -060067 }
Ramon Friede0212df2018-06-08 20:53:27 +030068
Ramon Friedb5e0e362018-05-30 23:09:59 +030069 rec->timestamp = timer_get_us();
Simon Glassaa532332014-06-11 23:29:41 -060070 rec->flags = flags;
71 rec->addr = map_to_sysmem(ptr);
72 rec->value = value;
73
74 /* Update our checksum */
75 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
76 sizeof(*rec));
77
Ramon Friede0212df2018-06-08 20:53:27 +030078 iotrace.needed_size += sizeof(struct iotrace_record);
Simon Glassaa532332014-06-11 23:29:41 -060079 iotrace.offset += sizeof(struct iotrace_record);
80}
81
82u32 iotrace_readl(const void *ptr)
83{
84 u32 v;
85
86 v = readl(ptr);
87 add_record(IOT_32 | IOT_READ, ptr, v);
88
89 return v;
90}
91
Simon Glass9859dc72019-09-25 08:56:57 -060092void iotrace_writel(ulong value, void *ptr)
Simon Glassaa532332014-06-11 23:29:41 -060093{
94 add_record(IOT_32 | IOT_WRITE, ptr, value);
95 writel(value, ptr);
96}
97
98u16 iotrace_readw(const void *ptr)
99{
100 u32 v;
101
102 v = readw(ptr);
103 add_record(IOT_16 | IOT_READ, ptr, v);
104
105 return v;
106}
107
Simon Glass9859dc72019-09-25 08:56:57 -0600108void iotrace_writew(ulong value, void *ptr)
Simon Glassaa532332014-06-11 23:29:41 -0600109{
110 add_record(IOT_16 | IOT_WRITE, ptr, value);
111 writew(value, ptr);
112}
113
114u8 iotrace_readb(const void *ptr)
115{
116 u32 v;
117
118 v = readb(ptr);
119 add_record(IOT_8 | IOT_READ, ptr, v);
120
121 return v;
122}
123
Simon Glass9859dc72019-09-25 08:56:57 -0600124void iotrace_writeb(ulong value, void *ptr)
Simon Glassaa532332014-06-11 23:29:41 -0600125{
126 add_record(IOT_8 | IOT_WRITE, ptr, value);
127 writeb(value, ptr);
128}
129
130void iotrace_reset_checksum(void)
131{
132 iotrace.crc32 = 0;
133}
134
135u32 iotrace_get_checksum(void)
136{
137 return iotrace.crc32;
138}
139
Ramon Frieda74440b2018-05-30 23:09:58 +0300140void iotrace_set_region(ulong start, ulong size)
141{
142 iotrace.region_start = start;
143 iotrace.region_size = size;
144}
145
146void iotrace_reset_region(void)
147{
148 iotrace.region_start = 0;
149 iotrace.region_size = 0;
150}
151
152void iotrace_get_region(ulong *start, ulong *size)
153{
154 *start = iotrace.region_start;
155 *size = iotrace.region_size;
156}
157
Simon Glassaa532332014-06-11 23:29:41 -0600158void iotrace_set_enabled(int enable)
159{
160 iotrace.enabled = enable;
161}
162
163int iotrace_get_enabled(void)
164{
165 return iotrace.enabled;
166}
167
168void iotrace_set_buffer(ulong start, ulong size)
169{
170 iotrace.start = start;
171 iotrace.size = size;
172 iotrace.offset = 0;
173 iotrace.crc32 = 0;
174}
175
Ramon Friede0212df2018-06-08 20:53:27 +0300176void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
Simon Glassaa532332014-06-11 23:29:41 -0600177{
178 *start = iotrace.start;
179 *size = iotrace.size;
Ramon Friede0212df2018-06-08 20:53:27 +0300180 *needed_size = iotrace.needed_size;
Simon Glassaa532332014-06-11 23:29:41 -0600181 *offset = iotrace.offset;
182 *count = iotrace.offset / sizeof(struct iotrace_record);
183}