1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2014 Google, Inc. 4 */ 5 6 #define IOTRACE_IMPL 7 8 #include <common.h> 9 #include <mapmem.h> 10 #include <asm/io.h> 11 12 DECLARE_GLOBAL_DATA_PTR; 13 14 /* Support up to the machine word length for now */ 15 typedef ulong iovalue_t; 16 17 enum iotrace_flags { 18 IOT_8 = 0, 19 IOT_16, 20 IOT_32, 21 22 IOT_READ = 0 << 3, 23 IOT_WRITE = 1 << 3, 24 }; 25 26 /** 27 * struct iotrace_record - Holds a single I/O trace record 28 * 29 * @flags: I/O access type 30 * @timestamp: Timestamp of access 31 * @addr: Address of access 32 * @value: Value written or read 33 */ 34 struct iotrace_record { 35 enum iotrace_flags flags; 36 u64 timestamp; 37 phys_addr_t addr; 38 iovalue_t value; 39 }; 40 41 /** 42 * struct iotrace - current trace status and checksum 43 * 44 * @start: Start address of iotrace buffer 45 * @size: Size of iotrace buffer in bytes 46 * @offset: Current write offset into iotrace buffer 47 * @region_start: Address of IO region to trace 48 * @region_size: Size of region to trace. if 0 will trace all address space 49 * @crc32: Current value of CRC chceksum of trace records 50 * @enabled: true if enabled, false if disabled 51 */ 52 static struct iotrace { 53 ulong start; 54 ulong size; 55 ulong offset; 56 ulong region_start; 57 ulong region_size; 58 u32 crc32; 59 bool enabled; 60 } iotrace; 61 62 static void add_record(int flags, const void *ptr, ulong value) 63 { 64 struct iotrace_record srec, *rec = &srec; 65 66 /* 67 * We don't support iotrace before relocation. Since the trace buffer 68 * is set up by a command, it can't be enabled at present. To change 69 * this we would need to set the iotrace buffer at build-time. See 70 * lib/trace.c for how this might be done if you are interested. 71 */ 72 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled) 73 return; 74 75 if (iotrace.region_size) 76 if ((ulong)ptr < iotrace.region_start || 77 (ulong)ptr > iotrace.region_start + iotrace.region_size) 78 return; 79 80 /* Store it if there is room */ 81 if (iotrace.offset + sizeof(*rec) < iotrace.size) { 82 rec = (struct iotrace_record *)map_sysmem( 83 iotrace.start + iotrace.offset, 84 sizeof(value)); 85 } 86 rec->timestamp = timer_get_us(); 87 rec->flags = flags; 88 rec->addr = map_to_sysmem(ptr); 89 rec->value = value; 90 91 /* Update our checksum */ 92 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec, 93 sizeof(*rec)); 94 95 iotrace.offset += sizeof(struct iotrace_record); 96 } 97 98 u32 iotrace_readl(const void *ptr) 99 { 100 u32 v; 101 102 v = readl(ptr); 103 add_record(IOT_32 | IOT_READ, ptr, v); 104 105 return v; 106 } 107 108 void iotrace_writel(ulong value, const void *ptr) 109 { 110 add_record(IOT_32 | IOT_WRITE, ptr, value); 111 writel(value, ptr); 112 } 113 114 u16 iotrace_readw(const void *ptr) 115 { 116 u32 v; 117 118 v = readw(ptr); 119 add_record(IOT_16 | IOT_READ, ptr, v); 120 121 return v; 122 } 123 124 void iotrace_writew(ulong value, const void *ptr) 125 { 126 add_record(IOT_16 | IOT_WRITE, ptr, value); 127 writew(value, ptr); 128 } 129 130 u8 iotrace_readb(const void *ptr) 131 { 132 u32 v; 133 134 v = readb(ptr); 135 add_record(IOT_8 | IOT_READ, ptr, v); 136 137 return v; 138 } 139 140 void iotrace_writeb(ulong value, const void *ptr) 141 { 142 add_record(IOT_8 | IOT_WRITE, ptr, value); 143 writeb(value, ptr); 144 } 145 146 void iotrace_reset_checksum(void) 147 { 148 iotrace.crc32 = 0; 149 } 150 151 u32 iotrace_get_checksum(void) 152 { 153 return iotrace.crc32; 154 } 155 156 void iotrace_set_region(ulong start, ulong size) 157 { 158 iotrace.region_start = start; 159 iotrace.region_size = size; 160 } 161 162 void iotrace_reset_region(void) 163 { 164 iotrace.region_start = 0; 165 iotrace.region_size = 0; 166 } 167 168 void iotrace_get_region(ulong *start, ulong *size) 169 { 170 *start = iotrace.region_start; 171 *size = iotrace.region_size; 172 } 173 174 void iotrace_set_enabled(int enable) 175 { 176 iotrace.enabled = enable; 177 } 178 179 int iotrace_get_enabled(void) 180 { 181 return iotrace.enabled; 182 } 183 184 void iotrace_set_buffer(ulong start, ulong size) 185 { 186 iotrace.start = start; 187 iotrace.size = size; 188 iotrace.offset = 0; 189 iotrace.crc32 = 0; 190 } 191 192 void iotrace_get_buffer(ulong *start, ulong *size, ulong *offset, ulong *count) 193 { 194 *start = iotrace.start; 195 *size = iotrace.size; 196 *offset = iotrace.offset; 197 *count = iotrace.offset / sizeof(struct iotrace_record); 198 } 199