iotrace.c 3.8 KB

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