Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc. |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define IOTRACE_IMPL |
| 7 | |
| 8 | #include <common.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 9 | #include <mapmem.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 10 | #include <time.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 13 | #include <linux/bug.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 14 | #include <u-boot/crc.h> |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 18 | /** |
| 19 | * struct iotrace - current trace status and checksum |
| 20 | * |
| 21 | * @start: Start address of iotrace buffer |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 22 | * @size: Actual size of iotrace buffer in bytes |
| 23 | * @needed_size: Needed of iotrace buffer in bytes |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 24 | * @offset: Current write offset into iotrace buffer |
Ramon Fried | d01a417 | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 25 | * @region_start: Address of IO region to trace |
| 26 | * @region_size: Size of region to trace. if 0 will trace all address space |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 27 | * @crc32: Current value of CRC chceksum of trace records |
| 28 | * @enabled: true if enabled, false if disabled |
| 29 | */ |
| 30 | static struct iotrace { |
| 31 | ulong start; |
| 32 | ulong size; |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 33 | ulong needed_size; |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 34 | ulong offset; |
Ramon Fried | d01a417 | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 35 | ulong region_start; |
| 36 | ulong region_size; |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 37 | u32 crc32; |
| 38 | bool enabled; |
| 39 | } iotrace; |
| 40 | |
| 41 | static void add_record(int flags, const void *ptr, ulong value) |
| 42 | { |
| 43 | struct iotrace_record srec, *rec = &srec; |
| 44 | |
| 45 | /* |
| 46 | * We don't support iotrace before relocation. Since the trace buffer |
| 47 | * is set up by a command, it can't be enabled at present. To change |
| 48 | * this we would need to set the iotrace buffer at build-time. See |
| 49 | * lib/trace.c for how this might be done if you are interested. |
| 50 | */ |
| 51 | if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled) |
| 52 | return; |
| 53 | |
Ramon Fried | d01a417 | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 54 | if (iotrace.region_size) |
| 55 | if ((ulong)ptr < iotrace.region_start || |
| 56 | (ulong)ptr > iotrace.region_start + iotrace.region_size) |
| 57 | return; |
| 58 | |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 59 | /* Store it if there is room */ |
| 60 | if (iotrace.offset + sizeof(*rec) < iotrace.size) { |
| 61 | rec = (struct iotrace_record *)map_sysmem( |
| 62 | iotrace.start + iotrace.offset, |
| 63 | sizeof(value)); |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 64 | } else { |
| 65 | WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n"); |
| 66 | iotrace.needed_size += sizeof(struct iotrace_record); |
| 67 | return; |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 68 | } |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 69 | |
Ramon Fried | a55c6a0 | 2018-05-30 23:09:59 +0300 | [diff] [blame] | 70 | rec->timestamp = timer_get_us(); |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 71 | rec->flags = flags; |
| 72 | rec->addr = map_to_sysmem(ptr); |
| 73 | rec->value = value; |
| 74 | |
| 75 | /* Update our checksum */ |
| 76 | iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec, |
| 77 | sizeof(*rec)); |
| 78 | |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 79 | iotrace.needed_size += sizeof(struct iotrace_record); |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 80 | iotrace.offset += sizeof(struct iotrace_record); |
| 81 | } |
| 82 | |
| 83 | u32 iotrace_readl(const void *ptr) |
| 84 | { |
| 85 | u32 v; |
| 86 | |
| 87 | v = readl(ptr); |
| 88 | add_record(IOT_32 | IOT_READ, ptr, v); |
| 89 | |
| 90 | return v; |
| 91 | } |
| 92 | |
Simon Glass | 815d170 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 93 | void iotrace_writel(ulong value, void *ptr) |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 94 | { |
| 95 | add_record(IOT_32 | IOT_WRITE, ptr, value); |
| 96 | writel(value, ptr); |
| 97 | } |
| 98 | |
| 99 | u16 iotrace_readw(const void *ptr) |
| 100 | { |
| 101 | u32 v; |
| 102 | |
| 103 | v = readw(ptr); |
| 104 | add_record(IOT_16 | IOT_READ, ptr, v); |
| 105 | |
| 106 | return v; |
| 107 | } |
| 108 | |
Simon Glass | 815d170 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 109 | void iotrace_writew(ulong value, void *ptr) |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 110 | { |
| 111 | add_record(IOT_16 | IOT_WRITE, ptr, value); |
| 112 | writew(value, ptr); |
| 113 | } |
| 114 | |
| 115 | u8 iotrace_readb(const void *ptr) |
| 116 | { |
| 117 | u32 v; |
| 118 | |
| 119 | v = readb(ptr); |
| 120 | add_record(IOT_8 | IOT_READ, ptr, v); |
| 121 | |
| 122 | return v; |
| 123 | } |
| 124 | |
Simon Glass | 815d170 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 125 | void iotrace_writeb(ulong value, void *ptr) |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 126 | { |
| 127 | add_record(IOT_8 | IOT_WRITE, ptr, value); |
| 128 | writeb(value, ptr); |
| 129 | } |
| 130 | |
| 131 | void iotrace_reset_checksum(void) |
| 132 | { |
| 133 | iotrace.crc32 = 0; |
| 134 | } |
| 135 | |
| 136 | u32 iotrace_get_checksum(void) |
| 137 | { |
| 138 | return iotrace.crc32; |
| 139 | } |
| 140 | |
Ramon Fried | d01a417 | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 141 | void iotrace_set_region(ulong start, ulong size) |
| 142 | { |
| 143 | iotrace.region_start = start; |
| 144 | iotrace.region_size = size; |
| 145 | } |
| 146 | |
| 147 | void iotrace_reset_region(void) |
| 148 | { |
| 149 | iotrace.region_start = 0; |
| 150 | iotrace.region_size = 0; |
| 151 | } |
| 152 | |
| 153 | void iotrace_get_region(ulong *start, ulong *size) |
| 154 | { |
| 155 | *start = iotrace.region_start; |
| 156 | *size = iotrace.region_size; |
| 157 | } |
| 158 | |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 159 | void iotrace_set_enabled(int enable) |
| 160 | { |
| 161 | iotrace.enabled = enable; |
| 162 | } |
| 163 | |
| 164 | int iotrace_get_enabled(void) |
| 165 | { |
| 166 | return iotrace.enabled; |
| 167 | } |
| 168 | |
| 169 | void iotrace_set_buffer(ulong start, ulong size) |
| 170 | { |
| 171 | iotrace.start = start; |
| 172 | iotrace.size = size; |
| 173 | iotrace.offset = 0; |
| 174 | iotrace.crc32 = 0; |
| 175 | } |
| 176 | |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 177 | void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count) |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 178 | { |
| 179 | *start = iotrace.start; |
| 180 | *size = iotrace.size; |
Ramon Fried | 75aa898 | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 181 | *needed_size = iotrace.needed_size; |
Simon Glass | 4dc47ca | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 182 | *offset = iotrace.offset; |
| 183 | *count = iotrace.offset / sizeof(struct iotrace_record); |
| 184 | } |