trace_mmiotrace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Memory mapped I/O tracing
  3. *
  4. * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
  5. */
  6. #define DEBUG 1
  7. #include <linux/kernel.h>
  8. #include <linux/mmiotrace.h>
  9. #include <linux/pci.h>
  10. #include "trace.h"
  11. static struct trace_array *mmio_trace_array;
  12. static void mmio_reset_data(struct trace_array *tr)
  13. {
  14. int cpu;
  15. tr->time_start = ftrace_now(tr->cpu);
  16. for_each_online_cpu(cpu)
  17. tracing_reset(tr->data[cpu]);
  18. }
  19. static void mmio_trace_init(struct trace_array *tr)
  20. {
  21. pr_debug("in %s\n", __func__);
  22. mmio_trace_array = tr;
  23. if (tr->ctrl) {
  24. mmio_reset_data(tr);
  25. enable_mmiotrace();
  26. }
  27. }
  28. static void mmio_trace_reset(struct trace_array *tr)
  29. {
  30. pr_debug("in %s\n", __func__);
  31. if (tr->ctrl)
  32. disable_mmiotrace();
  33. mmio_reset_data(tr);
  34. mmio_trace_array = NULL;
  35. }
  36. static void mmio_trace_ctrl_update(struct trace_array *tr)
  37. {
  38. pr_debug("in %s\n", __func__);
  39. if (tr->ctrl) {
  40. mmio_reset_data(tr);
  41. enable_mmiotrace();
  42. } else {
  43. disable_mmiotrace();
  44. }
  45. }
  46. static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
  47. {
  48. int ret = 0;
  49. int i;
  50. resource_size_t start, end;
  51. const struct pci_driver *drv = pci_dev_driver(dev);
  52. /* XXX: incomplete checks for trace_seq_printf() return value */
  53. ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
  54. dev->bus->number, dev->devfn,
  55. dev->vendor, dev->device, dev->irq);
  56. /*
  57. * XXX: is pci_resource_to_user() appropriate, since we are
  58. * supposed to interpret the __ioremap() phys_addr argument based on
  59. * these printed values?
  60. */
  61. for (i = 0; i < 7; i++) {
  62. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  63. ret += trace_seq_printf(s, " %llx",
  64. (unsigned long long)(start |
  65. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  66. }
  67. for (i = 0; i < 7; i++) {
  68. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  69. ret += trace_seq_printf(s, " %llx",
  70. dev->resource[i].start < dev->resource[i].end ?
  71. (unsigned long long)(end - start) + 1 : 0);
  72. }
  73. if (drv)
  74. ret += trace_seq_printf(s, " %s\n", drv->name);
  75. else
  76. ret += trace_seq_printf(s, " \n");
  77. return ret;
  78. }
  79. /* XXX: This is not called for trace_pipe file! */
  80. static void mmio_print_header(struct trace_iterator *iter)
  81. {
  82. struct trace_seq *s = &iter->seq;
  83. struct pci_dev *dev = NULL;
  84. trace_seq_printf(s, "VERSION 20070824\n");
  85. for_each_pci_dev(dev)
  86. mmio_print_pcidev(s, dev);
  87. /* XXX: return value? What if header is very long? */
  88. }
  89. static int mmio_print_rw(struct trace_iterator *iter)
  90. {
  91. struct trace_entry *entry = iter->ent;
  92. struct mmiotrace_rw *rw = &entry->mmiorw;
  93. struct trace_seq *s = &iter->seq;
  94. unsigned long long t = ns2usecs(entry->t);
  95. unsigned long usec_rem = do_div(t, 1000000ULL);
  96. unsigned secs = (unsigned long)t;
  97. int ret = 1;
  98. switch (entry->mmiorw.opcode) {
  99. case MMIO_READ:
  100. ret = trace_seq_printf(s,
  101. "R %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
  102. rw->width, secs, usec_rem, rw->map_id, rw->phys,
  103. rw->value, rw->pc, 0);
  104. break;
  105. case MMIO_WRITE:
  106. ret = trace_seq_printf(s,
  107. "W %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
  108. rw->width, secs, usec_rem, rw->map_id, rw->phys,
  109. rw->value, rw->pc, 0);
  110. break;
  111. case MMIO_UNKNOWN_OP:
  112. ret = trace_seq_printf(s,
  113. "UNKNOWN %lu.%06lu %d 0x%lx %02x,%02x,%02x 0x%lx %d\n",
  114. secs, usec_rem, rw->map_id, rw->phys,
  115. (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
  116. (rw->value >> 0) & 0xff, rw->pc, 0);
  117. break;
  118. default:
  119. ret = trace_seq_printf(s, "rw what?\n");
  120. break;
  121. }
  122. if (ret)
  123. return 1;
  124. return 0;
  125. }
  126. static int mmio_print_map(struct trace_iterator *iter)
  127. {
  128. struct trace_entry *entry = iter->ent;
  129. struct mmiotrace_map *m = &entry->mmiomap;
  130. struct trace_seq *s = &iter->seq;
  131. unsigned long long t = ns2usecs(entry->t);
  132. unsigned long usec_rem = do_div(t, 1000000ULL);
  133. unsigned secs = (unsigned long)t;
  134. int ret = 1;
  135. switch (entry->mmiorw.opcode) {
  136. case MMIO_PROBE:
  137. ret = trace_seq_printf(s,
  138. "MAP %lu.%06lu %d 0x%lx 0x%lx 0x%lx 0x%lx %d\n",
  139. secs, usec_rem, m->map_id, m->phys, m->virt, m->len,
  140. 0UL, entry->pid);
  141. break;
  142. case MMIO_UNPROBE:
  143. ret = trace_seq_printf(s,
  144. "UNMAP %lu.%06lu %d 0x%lx %d\n",
  145. secs, usec_rem, m->map_id, 0UL, entry->pid);
  146. break;
  147. default:
  148. ret = trace_seq_printf(s, "map what?\n");
  149. break;
  150. }
  151. if (ret)
  152. return 1;
  153. return 0;
  154. }
  155. /* return 0 to abort printing without consuming current entry in pipe mode */
  156. static int mmio_print_line(struct trace_iterator *iter)
  157. {
  158. switch (iter->ent->type) {
  159. case TRACE_MMIO_RW:
  160. return mmio_print_rw(iter);
  161. case TRACE_MMIO_MAP:
  162. return mmio_print_map(iter);
  163. default:
  164. return 1; /* ignore unknown entries */
  165. }
  166. }
  167. static struct tracer mmio_tracer __read_mostly =
  168. {
  169. .name = "mmiotrace",
  170. .init = mmio_trace_init,
  171. .reset = mmio_trace_reset,
  172. .open = mmio_print_header,
  173. .ctrl_update = mmio_trace_ctrl_update,
  174. .print_line = mmio_print_line,
  175. };
  176. __init static int init_mmio_trace(void)
  177. {
  178. return register_tracer(&mmio_tracer);
  179. }
  180. device_initcall(init_mmio_trace);
  181. void mmio_trace_rw(struct mmiotrace_rw *rw)
  182. {
  183. struct trace_array *tr = mmio_trace_array;
  184. struct trace_array_cpu *data = tr->data[smp_processor_id()];
  185. __trace_mmiotrace_rw(tr, data, rw);
  186. }
  187. void mmio_trace_mapping(struct mmiotrace_map *map)
  188. {
  189. struct trace_array *tr = mmio_trace_array;
  190. struct trace_array_cpu *data;
  191. preempt_disable();
  192. data = tr->data[smp_processor_id()];
  193. __trace_mmiotrace_map(tr, data, map);
  194. preempt_enable();
  195. }