blob: 49bee3c92a094903c24213e83a7387549b583cdd [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
Simon Glassaa532332014-06-11 23:29:41 -060014/**
15 * struct iotrace - current trace status and checksum
16 *
17 * @start: Start address of iotrace buffer
Ramon Friede0212df2018-06-08 20:53:27 +030018 * @size: Actual size of iotrace buffer in bytes
19 * @needed_size: Needed of iotrace buffer in bytes
Simon Glassaa532332014-06-11 23:29:41 -060020 * @offset: Current write offset into iotrace buffer
Ramon Frieda74440b2018-05-30 23:09:58 +030021 * @region_start: Address of IO region to trace
22 * @region_size: Size of region to trace. if 0 will trace all address space
Simon Glassaa532332014-06-11 23:29:41 -060023 * @crc32: Current value of CRC chceksum of trace records
24 * @enabled: true if enabled, false if disabled
25 */
26static struct iotrace {
27 ulong start;
28 ulong size;
Ramon Friede0212df2018-06-08 20:53:27 +030029 ulong needed_size;
Simon Glassaa532332014-06-11 23:29:41 -060030 ulong offset;
Ramon Frieda74440b2018-05-30 23:09:58 +030031 ulong region_start;
32 ulong region_size;
Simon Glassaa532332014-06-11 23:29:41 -060033 u32 crc32;
34 bool enabled;
35} iotrace;
36
37static void add_record(int flags, const void *ptr, ulong value)
38{
39 struct iotrace_record srec, *rec = &srec;
40
41 /*
42 * We don't support iotrace before relocation. Since the trace buffer
43 * is set up by a command, it can't be enabled at present. To change
44 * this we would need to set the iotrace buffer at build-time. See
45 * lib/trace.c for how this might be done if you are interested.
46 */
47 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
48 return;
49
Ramon Frieda74440b2018-05-30 23:09:58 +030050 if (iotrace.region_size)
51 if ((ulong)ptr < iotrace.region_start ||
52 (ulong)ptr > iotrace.region_start + iotrace.region_size)
53 return;
54
Simon Glassaa532332014-06-11 23:29:41 -060055 /* Store it if there is room */
56 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
57 rec = (struct iotrace_record *)map_sysmem(
58 iotrace.start + iotrace.offset,
59 sizeof(value));
Ramon Friede0212df2018-06-08 20:53:27 +030060 } else {
61 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
62 iotrace.needed_size += sizeof(struct iotrace_record);
63 return;
Simon Glassaa532332014-06-11 23:29:41 -060064 }
Ramon Friede0212df2018-06-08 20:53:27 +030065
Ramon Friedb5e0e362018-05-30 23:09:59 +030066 rec->timestamp = timer_get_us();
Simon Glassaa532332014-06-11 23:29:41 -060067 rec->flags = flags;
68 rec->addr = map_to_sysmem(ptr);
69 rec->value = value;
70
71 /* Update our checksum */
72 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
73 sizeof(*rec));
74
Ramon Friede0212df2018-06-08 20:53:27 +030075 iotrace.needed_size += sizeof(struct iotrace_record);
Simon Glassaa532332014-06-11 23:29:41 -060076 iotrace.offset += sizeof(struct iotrace_record);
77}
78
79u32 iotrace_readl(const void *ptr)
80{
81 u32 v;
82
83 v = readl(ptr);
84 add_record(IOT_32 | IOT_READ, ptr, v);
85
86 return v;
87}
88
89void iotrace_writel(ulong value, const void *ptr)
90{
91 add_record(IOT_32 | IOT_WRITE, ptr, value);
92 writel(value, ptr);
93}
94
95u16 iotrace_readw(const void *ptr)
96{
97 u32 v;
98
99 v = readw(ptr);
100 add_record(IOT_16 | IOT_READ, ptr, v);
101
102 return v;
103}
104
105void iotrace_writew(ulong value, const void *ptr)
106{
107 add_record(IOT_16 | IOT_WRITE, ptr, value);
108 writew(value, ptr);
109}
110
111u8 iotrace_readb(const void *ptr)
112{
113 u32 v;
114
115 v = readb(ptr);
116 add_record(IOT_8 | IOT_READ, ptr, v);
117
118 return v;
119}
120
121void iotrace_writeb(ulong value, const void *ptr)
122{
123 add_record(IOT_8 | IOT_WRITE, ptr, value);
124 writeb(value, ptr);
125}
126
127void iotrace_reset_checksum(void)
128{
129 iotrace.crc32 = 0;
130}
131
132u32 iotrace_get_checksum(void)
133{
134 return iotrace.crc32;
135}
136
Ramon Frieda74440b2018-05-30 23:09:58 +0300137void iotrace_set_region(ulong start, ulong size)
138{
139 iotrace.region_start = start;
140 iotrace.region_size = size;
141}
142
143void iotrace_reset_region(void)
144{
145 iotrace.region_start = 0;
146 iotrace.region_size = 0;
147}
148
149void iotrace_get_region(ulong *start, ulong *size)
150{
151 *start = iotrace.region_start;
152 *size = iotrace.region_size;
153}
154
Simon Glassaa532332014-06-11 23:29:41 -0600155void iotrace_set_enabled(int enable)
156{
157 iotrace.enabled = enable;
158}
159
160int iotrace_get_enabled(void)
161{
162 return iotrace.enabled;
163}
164
165void iotrace_set_buffer(ulong start, ulong size)
166{
167 iotrace.start = start;
168 iotrace.size = size;
169 iotrace.offset = 0;
170 iotrace.crc32 = 0;
171}
172
Ramon Friede0212df2018-06-08 20:53:27 +0300173void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
Simon Glassaa532332014-06-11 23:29:41 -0600174{
175 *start = iotrace.start;
176 *size = iotrace.size;
Ramon Friede0212df2018-06-08 20:53:27 +0300177 *needed_size = iotrace.needed_size;
Simon Glassaa532332014-06-11 23:29:41 -0600178 *offset = iotrace.offset;
179 *count = iotrace.offset / sizeof(struct iotrace_record);
180}