Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc. |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define IOTRACE_IMPL |
| 7 | |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 8 | #include <mapmem.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 9 | #include <time.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 12 | #include <linux/bug.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 13 | #include <u-boot/crc.h> |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 17 | /** |
| 18 | * struct iotrace - current trace status and checksum |
| 19 | * |
| 20 | * @start: Start address of iotrace buffer |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 21 | * @size: Actual size of iotrace buffer in bytes |
| 22 | * @needed_size: Needed of iotrace buffer in bytes |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 23 | * @offset: Current write offset into iotrace buffer |
Ramon Fried | a74440b | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 24 | * @region_start: Address of IO region to trace |
| 25 | * @region_size: Size of region to trace. if 0 will trace all address space |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 26 | * @crc32: Current value of CRC chceksum of trace records |
| 27 | * @enabled: true if enabled, false if disabled |
| 28 | */ |
| 29 | static struct iotrace { |
| 30 | ulong start; |
| 31 | ulong size; |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 32 | ulong needed_size; |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 33 | ulong offset; |
Ramon Fried | a74440b | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 34 | ulong region_start; |
| 35 | ulong region_size; |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 36 | u32 crc32; |
| 37 | bool enabled; |
| 38 | } iotrace; |
| 39 | |
| 40 | static 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 Fried | a74440b | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 53 | if (iotrace.region_size) |
| 54 | if ((ulong)ptr < iotrace.region_start || |
| 55 | (ulong)ptr > iotrace.region_start + iotrace.region_size) |
| 56 | return; |
| 57 | |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 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)); |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 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; |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 67 | } |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 68 | |
Ramon Fried | b5e0e36 | 2018-05-30 23:09:59 +0300 | [diff] [blame] | 69 | rec->timestamp = timer_get_us(); |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 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 | |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 78 | iotrace.needed_size += sizeof(struct iotrace_record); |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 79 | iotrace.offset += sizeof(struct iotrace_record); |
| 80 | } |
| 81 | |
| 82 | u32 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 Glass | 9859dc7 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 92 | void iotrace_writel(ulong value, void *ptr) |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 93 | { |
| 94 | add_record(IOT_32 | IOT_WRITE, ptr, value); |
| 95 | writel(value, ptr); |
| 96 | } |
| 97 | |
| 98 | u16 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 Glass | 9859dc7 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 108 | void iotrace_writew(ulong value, void *ptr) |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 109 | { |
| 110 | add_record(IOT_16 | IOT_WRITE, ptr, value); |
| 111 | writew(value, ptr); |
| 112 | } |
| 113 | |
| 114 | u8 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 Glass | 9859dc7 | 2019-09-25 08:56:57 -0600 | [diff] [blame] | 124 | void iotrace_writeb(ulong value, void *ptr) |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 125 | { |
| 126 | add_record(IOT_8 | IOT_WRITE, ptr, value); |
| 127 | writeb(value, ptr); |
| 128 | } |
| 129 | |
| 130 | void iotrace_reset_checksum(void) |
| 131 | { |
| 132 | iotrace.crc32 = 0; |
| 133 | } |
| 134 | |
| 135 | u32 iotrace_get_checksum(void) |
| 136 | { |
| 137 | return iotrace.crc32; |
| 138 | } |
| 139 | |
Ramon Fried | a74440b | 2018-05-30 23:09:58 +0300 | [diff] [blame] | 140 | void iotrace_set_region(ulong start, ulong size) |
| 141 | { |
| 142 | iotrace.region_start = start; |
| 143 | iotrace.region_size = size; |
| 144 | } |
| 145 | |
| 146 | void iotrace_reset_region(void) |
| 147 | { |
| 148 | iotrace.region_start = 0; |
| 149 | iotrace.region_size = 0; |
| 150 | } |
| 151 | |
| 152 | void iotrace_get_region(ulong *start, ulong *size) |
| 153 | { |
| 154 | *start = iotrace.region_start; |
| 155 | *size = iotrace.region_size; |
| 156 | } |
| 157 | |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 158 | void iotrace_set_enabled(int enable) |
| 159 | { |
| 160 | iotrace.enabled = enable; |
| 161 | } |
| 162 | |
| 163 | int iotrace_get_enabled(void) |
| 164 | { |
| 165 | return iotrace.enabled; |
| 166 | } |
| 167 | |
| 168 | void 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 Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 176 | void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count) |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 177 | { |
| 178 | *start = iotrace.start; |
| 179 | *size = iotrace.size; |
Ramon Fried | e0212df | 2018-06-08 20:53:27 +0300 | [diff] [blame] | 180 | *needed_size = iotrace.needed_size; |
Simon Glass | aa53233 | 2014-06-11 23:29:41 -0600 | [diff] [blame] | 181 | *offset = iotrace.offset; |
| 182 | *count = iotrace.offset / sizeof(struct iotrace_record); |
| 183 | } |