trace_mmiotrace.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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%lx 0x%lx 0x%lx %d\n",
  160. rw->width, secs, usec_rem, rw->map_id, rw->phys,
  161. rw->value, rw->pc, 0);
  162. break;
  163. case MMIO_WRITE:
  164. ret = trace_seq_printf(s,
  165. "W %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
  166. rw->width, secs, usec_rem, rw->map_id, rw->phys,
  167. rw->value, rw->pc, 0);
  168. break;
  169. case MMIO_UNKNOWN_OP:
  170. ret = trace_seq_printf(s,
  171. "UNKNOWN %lu.%06lu %d 0x%lx %02x,%02x,%02x 0x%lx %d\n",
  172. secs, usec_rem, rw->map_id, rw->phys,
  173. (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
  174. (rw->value >> 0) & 0xff, rw->pc, 0);
  175. break;
  176. default:
  177. ret = trace_seq_printf(s, "rw what?\n");
  178. break;
  179. }
  180. if (ret)
  181. return 1;
  182. return 0;
  183. }
  184. static int mmio_print_map(struct trace_iterator *iter)
  185. {
  186. struct trace_entry *entry = iter->ent;
  187. struct mmiotrace_map *m = &entry->mmiomap;
  188. struct trace_seq *s = &iter->seq;
  189. unsigned long long t = ns2usecs(entry->t);
  190. unsigned long usec_rem = do_div(t, 1000000ULL);
  191. unsigned secs = (unsigned long)t;
  192. int ret = 1;
  193. switch (entry->mmiorw.opcode) {
  194. case MMIO_PROBE:
  195. ret = trace_seq_printf(s,
  196. "MAP %lu.%06lu %d 0x%lx 0x%lx 0x%lx 0x%lx %d\n",
  197. secs, usec_rem, m->map_id, m->phys, m->virt, m->len,
  198. 0UL, entry->pid);
  199. break;
  200. case MMIO_UNPROBE:
  201. ret = trace_seq_printf(s,
  202. "UNMAP %lu.%06lu %d 0x%lx %d\n",
  203. secs, usec_rem, m->map_id, 0UL, entry->pid);
  204. break;
  205. default:
  206. ret = trace_seq_printf(s, "map what?\n");
  207. break;
  208. }
  209. if (ret)
  210. return 1;
  211. return 0;
  212. }
  213. /* return 0 to abort printing without consuming current entry in pipe mode */
  214. static int mmio_print_line(struct trace_iterator *iter)
  215. {
  216. switch (iter->ent->type) {
  217. case TRACE_MMIO_RW:
  218. return mmio_print_rw(iter);
  219. case TRACE_MMIO_MAP:
  220. return mmio_print_map(iter);
  221. default:
  222. return 1; /* ignore unknown entries */
  223. }
  224. }
  225. static struct tracer mmio_tracer __read_mostly =
  226. {
  227. .name = "mmiotrace",
  228. .init = mmio_trace_init,
  229. .reset = mmio_trace_reset,
  230. .pipe_open = mmio_pipe_open,
  231. .close = mmio_close,
  232. .read = mmio_read,
  233. .ctrl_update = mmio_trace_ctrl_update,
  234. .print_line = mmio_print_line,
  235. };
  236. __init static int init_mmio_trace(void)
  237. {
  238. return register_tracer(&mmio_tracer);
  239. }
  240. device_initcall(init_mmio_trace);
  241. void mmio_trace_rw(struct mmiotrace_rw *rw)
  242. {
  243. struct trace_array *tr = mmio_trace_array;
  244. struct trace_array_cpu *data = tr->data[smp_processor_id()];
  245. __trace_mmiotrace_rw(tr, data, rw);
  246. }
  247. void mmio_trace_mapping(struct mmiotrace_map *map)
  248. {
  249. struct trace_array *tr = mmio_trace_array;
  250. struct trace_array_cpu *data;
  251. preempt_disable();
  252. data = tr->data[smp_processor_id()];
  253. __trace_mmiotrace_map(tr, data, map);
  254. preempt_enable();
  255. }