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