]> Git Repo - J-u-boot.git/blame - common/iotrace.c
image: android: fix abootimg support
[J-u-boot.git] / common / iotrace.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
aa53233a
SG
2/*
3 * Copyright (c) 2014 Google, Inc.
aa53233a
SG
4 */
5
6#define IOTRACE_IMPL
7
8#include <common.h>
0eb25b61 9#include <mapmem.h>
1045315d 10#include <time.h>
aa53233a 11#include <asm/io.h>
eb41d8a1 12#include <linux/bug.h>
3db71108 13#include <u-boot/crc.h>
aa53233a
SG
14
15DECLARE_GLOBAL_DATA_PTR;
16
aa53233a
SG
17/**
18 * struct iotrace - current trace status and checksum
19 *
20 * @start: Start address of iotrace buffer
e0212dfa
RF
21 * @size: Actual size of iotrace buffer in bytes
22 * @needed_size: Needed of iotrace buffer in bytes
aa53233a 23 * @offset: Current write offset into iotrace buffer
a74440b2
RF
24 * @region_start: Address of IO region to trace
25 * @region_size: Size of region to trace. if 0 will trace all address space
aa53233a
SG
26 * @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;
e0212dfa 32 ulong needed_size;
aa53233a 33 ulong offset;
a74440b2
RF
34 ulong region_start;
35 ulong region_size;
aa53233a
SG
36 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
a74440b2
RF
53 if (iotrace.region_size)
54 if ((ulong)ptr < iotrace.region_start ||
55 (ulong)ptr > iotrace.region_start + iotrace.region_size)
56 return;
57
aa53233a
SG
58 /* 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));
e0212dfa
RF
63 } 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;
aa53233a 67 }
e0212dfa 68
b5e0e360 69 rec->timestamp = timer_get_us();
aa53233a
SG
70 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
e0212dfa 78 iotrace.needed_size += sizeof(struct iotrace_record);
aa53233a
SG
79 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
9859dc76 92void iotrace_writel(ulong value, void *ptr)
aa53233a
SG
93{
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
9859dc76 108void iotrace_writew(ulong value, void *ptr)
aa53233a
SG
109{
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
9859dc76 124void iotrace_writeb(ulong value, void *ptr)
aa53233a
SG
125{
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
a74440b2
RF
140void 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
aa53233a
SG
158void 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
e0212dfa 176void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
aa53233a
SG
177{
178 *start = iotrace.start;
179 *size = iotrace.size;
e0212dfa 180 *needed_size = iotrace.needed_size;
aa53233a
SG
181 *offset = iotrace.offset;
182 *count = iotrace.offset / sizeof(struct iotrace_record);
183}
This page took 0.337067 seconds and 4 git commands to generate.