trace_mmiotrace.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. struct header_iter {
  12. struct pci_dev *dev;
  13. };
  14. static struct trace_array *mmio_trace_array;
  15. static bool overrun_detected;
  16. static void mmio_reset_data(struct trace_array *tr)
  17. {
  18. int cpu;
  19. overrun_detected = false;
  20. tr->time_start = ftrace_now(tr->cpu);
  21. for_each_online_cpu(cpu)
  22. tracing_reset(tr->data[cpu]);
  23. }
  24. static void mmio_trace_init(struct trace_array *tr)
  25. {
  26. pr_debug("in %s\n", __func__);
  27. mmio_trace_array = tr;
  28. if (tr->ctrl) {
  29. mmio_reset_data(tr);
  30. enable_mmiotrace();
  31. }
  32. }
  33. static void mmio_trace_reset(struct trace_array *tr)
  34. {
  35. pr_debug("in %s\n", __func__);
  36. if (tr->ctrl)
  37. disable_mmiotrace();
  38. mmio_reset_data(tr);
  39. mmio_trace_array = NULL;
  40. }
  41. static void mmio_trace_ctrl_update(struct trace_array *tr)
  42. {
  43. pr_debug("in %s\n", __func__);
  44. if (tr->ctrl) {
  45. mmio_reset_data(tr);
  46. enable_mmiotrace();
  47. } else {
  48. disable_mmiotrace();
  49. }
  50. }
  51. static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
  52. {
  53. int ret = 0;
  54. int i;
  55. resource_size_t start, end;
  56. const struct pci_driver *drv = pci_dev_driver(dev);
  57. /* XXX: incomplete checks for trace_seq_printf() return value */
  58. ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
  59. dev->bus->number, dev->devfn,
  60. dev->vendor, dev->device, dev->irq);
  61. /*
  62. * XXX: is pci_resource_to_user() appropriate, since we are
  63. * supposed to interpret the __ioremap() phys_addr argument based on
  64. * these printed values?
  65. */
  66. for (i = 0; i < 7; i++) {
  67. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  68. ret += trace_seq_printf(s, " %llx",
  69. (unsigned long long)(start |
  70. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  71. }
  72. for (i = 0; i < 7; i++) {
  73. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  74. ret += trace_seq_printf(s, " %llx",
  75. dev->resource[i].start < dev->resource[i].end ?
  76. (unsigned long long)(end - start) + 1 : 0);
  77. }
  78. if (drv)
  79. ret += trace_seq_printf(s, " %s\n", drv->name);
  80. else
  81. ret += trace_seq_printf(s, " \n");
  82. return ret;
  83. }
  84. static void destroy_header_iter(struct header_iter *hiter)
  85. {
  86. if (!hiter)
  87. return;
  88. pci_dev_put(hiter->dev);
  89. kfree(hiter);
  90. }
  91. static void mmio_pipe_open(struct trace_iterator *iter)
  92. {
  93. struct header_iter *hiter;
  94. struct trace_seq *s = &iter->seq;
  95. trace_seq_printf(s, "VERSION 20070824\n");
  96. hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
  97. if (!hiter)
  98. return;
  99. hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
  100. iter->private = hiter;
  101. }
  102. /* XXX: This is not called when the pipe is closed! */
  103. static void mmio_close(struct trace_iterator *iter)
  104. {
  105. struct header_iter *hiter = iter->private;
  106. destroy_header_iter(hiter);
  107. iter->private = NULL;
  108. }
  109. static unsigned long count_overruns(struct trace_iterator *iter)
  110. {
  111. int cpu;
  112. unsigned long cnt = 0;
  113. for_each_online_cpu(cpu) {
  114. cnt += iter->overrun[cpu];
  115. iter->overrun[cpu] = 0;
  116. }
  117. return cnt;
  118. }
  119. static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
  120. char __user *ubuf, size_t cnt, loff_t *ppos)
  121. {
  122. ssize_t ret;
  123. struct header_iter *hiter = iter->private;
  124. struct trace_seq *s = &iter->seq;
  125. unsigned long n;
  126. n = count_overruns(iter);
  127. if (n) {
  128. /* XXX: This is later than where events were lost. */
  129. trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
  130. if (!overrun_detected)
  131. pr_warning("mmiotrace has lost events.\n");
  132. overrun_detected = true;
  133. goto print_out;
  134. }
  135. if (!hiter)
  136. return 0;
  137. mmio_print_pcidev(s, hiter->dev);
  138. hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
  139. if (!hiter->dev) {
  140. destroy_header_iter(hiter);
  141. iter->private = NULL;
  142. }
  143. print_out:
  144. ret = trace_seq_to_user(s, ubuf, cnt);
  145. return (ret == -EBUSY) ? 0 : ret;
  146. }
  147. static int mmio_print_rw(struct trace_iterator *iter)
  148. {
  149. struct trace_entry *entry = iter->ent;
  150. struct mmiotrace_rw *rw = &entry->mmiorw;
  151. struct trace_seq *s = &iter->seq;
  152. unsigned long long t = ns2usecs(entry->t);
  153. unsigned long usec_rem = do_div(t, 1000000ULL);
  154. unsigned secs = (unsigned long)t;
  155. int ret = 1;
  156. switch (entry->mmiorw.opcode) {
  157. case MMIO_READ:
  158. ret = trace_seq_printf(s,
  159. "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
  160. rw->width, secs, usec_rem, rw->map_id,
  161. (unsigned long long)rw->phys,
  162. rw->value, rw->pc, 0);
  163. break;
  164. case MMIO_WRITE:
  165. ret = trace_seq_printf(s,
  166. "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
  167. rw->width, secs, usec_rem, rw->map_id,
  168. (unsigned long long)rw->phys,
  169. rw->value, rw->pc, 0);
  170. break;
  171. case MMIO_UNKNOWN_OP:
  172. ret = trace_seq_printf(s,
  173. "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
  174. secs, usec_rem, rw->map_id,
  175. (unsigned long long)rw->phys,
  176. (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
  177. (rw->value >> 0) & 0xff, rw->pc, 0);
  178. break;
  179. default:
  180. ret = trace_seq_printf(s, "rw what?\n");
  181. break;
  182. }
  183. if (ret)
  184. return 1;
  185. return 0;
  186. }
  187. static int mmio_print_map(struct trace_iterator *iter)
  188. {
  189. struct trace_entry *entry = iter->ent;
  190. struct mmiotrace_map *m = &entry->mmiomap;
  191. struct trace_seq *s = &iter->seq;
  192. unsigned long long t = ns2usecs(entry->t);
  193. unsigned long usec_rem = do_div(t, 1000000ULL);
  194. unsigned secs = (unsigned long)t;
  195. int ret = 1;
  196. switch (entry->mmiorw.opcode) {
  197. case MMIO_PROBE:
  198. ret = trace_seq_printf(s,
  199. "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
  200. secs, usec_rem, m->map_id,
  201. (unsigned long long)m->phys, m->virt, m->len,
  202. 0UL, 0);
  203. break;
  204. case MMIO_UNPROBE:
  205. ret = trace_seq_printf(s,
  206. "UNMAP %lu.%06lu %d 0x%lx %d\n",
  207. secs, usec_rem, m->map_id, 0UL, 0);
  208. break;
  209. default:
  210. ret = trace_seq_printf(s, "map what?\n");
  211. break;
  212. }
  213. if (ret)
  214. return 1;
  215. return 0;
  216. }
  217. /* return 0 to abort printing without consuming current entry in pipe mode */
  218. static int mmio_print_line(struct trace_iterator *iter)
  219. {
  220. switch (iter->ent->type) {
  221. case TRACE_MMIO_RW:
  222. return mmio_print_rw(iter);
  223. case TRACE_MMIO_MAP:
  224. return mmio_print_map(iter);
  225. default:
  226. return 1; /* ignore unknown entries */
  227. }
  228. }
  229. static struct tracer mmio_tracer __read_mostly =
  230. {
  231. .name = "mmiotrace",
  232. .init = mmio_trace_init,
  233. .reset = mmio_trace_reset,
  234. .pipe_open = mmio_pipe_open,
  235. .close = mmio_close,
  236. .read = mmio_read,
  237. .ctrl_update = mmio_trace_ctrl_update,
  238. .print_line = mmio_print_line,
  239. };
  240. __init static int init_mmio_trace(void)
  241. {
  242. return register_tracer(&mmio_tracer);
  243. }
  244. device_initcall(init_mmio_trace);
  245. void mmio_trace_rw(struct mmiotrace_rw *rw)
  246. {
  247. struct trace_array *tr = mmio_trace_array;
  248. struct trace_array_cpu *data = tr->data[smp_processor_id()];
  249. __trace_mmiotrace_rw(tr, data, rw);
  250. }
  251. void mmio_trace_mapping(struct mmiotrace_map *map)
  252. {
  253. struct trace_array *tr = mmio_trace_array;
  254. struct trace_array_cpu *data;
  255. preempt_disable();
  256. data = tr->data[smp_processor_id()];
  257. __trace_mmiotrace_map(tr, data, map);
  258. preempt_enable();
  259. }