iotrace.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2014 Google, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #define IOTRACE_IMPL
  7. #include <common.h>
  8. #include <mapmem.h>
  9. #include <asm/io.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. /* Support up to the machine word length for now */
  12. typedef ulong iovalue_t;
  13. enum iotrace_flags {
  14. IOT_8 = 0,
  15. IOT_16,
  16. IOT_32,
  17. IOT_READ = 0 << 3,
  18. IOT_WRITE = 1 << 3,
  19. };
  20. /**
  21. * struct iotrace_record - Holds a single I/O trace record
  22. *
  23. * @flags: I/O access type
  24. * @addr: Address of access
  25. * @value: Value written or read
  26. */
  27. struct iotrace_record {
  28. enum iotrace_flags flags;
  29. phys_addr_t addr;
  30. iovalue_t value;
  31. };
  32. /**
  33. * struct iotrace - current trace status and checksum
  34. *
  35. * @start: Start address of iotrace buffer
  36. * @size: Size of iotrace buffer in bytes
  37. * @offset: Current write offset into iotrace buffer
  38. * @crc32: Current value of CRC chceksum of trace records
  39. * @enabled: true if enabled, false if disabled
  40. */
  41. static struct iotrace {
  42. ulong start;
  43. ulong size;
  44. ulong offset;
  45. u32 crc32;
  46. bool enabled;
  47. } iotrace;
  48. static void add_record(int flags, const void *ptr, ulong value)
  49. {
  50. struct iotrace_record srec, *rec = &srec;
  51. /*
  52. * We don't support iotrace before relocation. Since the trace buffer
  53. * is set up by a command, it can't be enabled at present. To change
  54. * this we would need to set the iotrace buffer at build-time. See
  55. * lib/trace.c for how this might be done if you are interested.
  56. */
  57. if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
  58. return;
  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));
  64. }
  65. rec->flags = flags;
  66. rec->addr = map_to_sysmem(ptr);
  67. rec->value = value;
  68. /* Update our checksum */
  69. iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
  70. sizeof(*rec));
  71. iotrace.offset += sizeof(struct iotrace_record);
  72. }
  73. u32 iotrace_readl(const void *ptr)
  74. {
  75. u32 v;
  76. v = readl(ptr);
  77. add_record(IOT_32 | IOT_READ, ptr, v);
  78. return v;
  79. }
  80. void iotrace_writel(ulong value, const void *ptr)
  81. {
  82. add_record(IOT_32 | IOT_WRITE, ptr, value);
  83. writel(value, ptr);
  84. }
  85. u16 iotrace_readw(const void *ptr)
  86. {
  87. u32 v;
  88. v = readw(ptr);
  89. add_record(IOT_16 | IOT_READ, ptr, v);
  90. return v;
  91. }
  92. void iotrace_writew(ulong value, const void *ptr)
  93. {
  94. add_record(IOT_16 | IOT_WRITE, ptr, value);
  95. writew(value, ptr);
  96. }
  97. u8 iotrace_readb(const void *ptr)
  98. {
  99. u32 v;
  100. v = readb(ptr);
  101. add_record(IOT_8 | IOT_READ, ptr, v);
  102. return v;
  103. }
  104. void iotrace_writeb(ulong value, const void *ptr)
  105. {
  106. add_record(IOT_8 | IOT_WRITE, ptr, value);
  107. writeb(value, ptr);
  108. }
  109. void iotrace_reset_checksum(void)
  110. {
  111. iotrace.crc32 = 0;
  112. }
  113. u32 iotrace_get_checksum(void)
  114. {
  115. return iotrace.crc32;
  116. }
  117. void iotrace_set_enabled(int enable)
  118. {
  119. iotrace.enabled = enable;
  120. }
  121. int iotrace_get_enabled(void)
  122. {
  123. return iotrace.enabled;
  124. }
  125. void iotrace_set_buffer(ulong start, ulong size)
  126. {
  127. iotrace.start = start;
  128. iotrace.size = size;
  129. iotrace.offset = 0;
  130. iotrace.crc32 = 0;
  131. }
  132. void iotrace_get_buffer(ulong *start, ulong *size, ulong *offset, ulong *count)
  133. {
  134. *start = iotrace.start;
  135. *size = iotrace.size;
  136. *offset = iotrace.offset;
  137. *count = iotrace.offset / sizeof(struct iotrace_record);
  138. }