trace_mmiotrace.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <linux/slab.h>
  11. #include <linux/time.h>
  12. #include <linux/atomic.h>
  13. #include "trace.h"
  14. #include "trace_output.h"
  15. struct header_iter {
  16. struct pci_dev *dev;
  17. };
  18. static struct trace_array *mmio_trace_array;
  19. static bool overrun_detected;
  20. static unsigned long prev_overruns;
  21. static atomic_t dropped_count;
  22. static void mmio_reset_data(struct trace_array *tr)
  23. {
  24. overrun_detected = false;
  25. prev_overruns = 0;
  26. tracing_reset_online_cpus(&tr->trace_buffer);
  27. }
  28. static int mmio_trace_init(struct trace_array *tr)
  29. {
  30. pr_debug("in %s\n", __func__);
  31. mmio_trace_array = tr;
  32. mmio_reset_data(tr);
  33. enable_mmiotrace();
  34. return 0;
  35. }
  36. static void mmio_trace_reset(struct trace_array *tr)
  37. {
  38. pr_debug("in %s\n", __func__);
  39. disable_mmiotrace();
  40. mmio_reset_data(tr);
  41. mmio_trace_array = NULL;
  42. }
  43. static void mmio_trace_start(struct trace_array *tr)
  44. {
  45. pr_debug("in %s\n", __func__);
  46. mmio_reset_data(tr);
  47. }
  48. static void mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
  49. {
  50. int i;
  51. resource_size_t start, end;
  52. const struct pci_driver *drv = pci_dev_driver(dev);
  53. trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
  54. dev->bus->number, dev->devfn,
  55. dev->vendor, dev->device, dev->irq);
  56. for (i = 0; i < 7; i++) {
  57. start = dev->resource[i].start;
  58. trace_seq_printf(s, " %llx",
  59. (unsigned long long)(start |
  60. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  61. }
  62. for (i = 0; i < 7; i++) {
  63. start = dev->resource[i].start;
  64. end = dev->resource[i].end;
  65. trace_seq_printf(s, " %llx",
  66. dev->resource[i].start < dev->resource[i].end ?
  67. (unsigned long long)(end - start) + 1 : 0);
  68. }
  69. if (drv)
  70. trace_seq_printf(s, " %s\n", drv->name);
  71. else
  72. trace_seq_puts(s, " \n");
  73. }
  74. static void destroy_header_iter(struct header_iter *hiter)
  75. {
  76. if (!hiter)
  77. return;
  78. pci_dev_put(hiter->dev);
  79. kfree(hiter);
  80. }
  81. static void mmio_pipe_open(struct trace_iterator *iter)
  82. {
  83. struct header_iter *hiter;
  84. struct trace_seq *s = &iter->seq;
  85. trace_seq_puts(s, "VERSION 20070824\n");
  86. hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
  87. if (!hiter)
  88. return;
  89. hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
  90. iter->private = hiter;
  91. }
  92. /* XXX: This is not called when the pipe is closed! */
  93. static void mmio_close(struct trace_iterator *iter)
  94. {
  95. struct header_iter *hiter = iter->private;
  96. destroy_header_iter(hiter);
  97. iter->private = NULL;
  98. }
  99. static unsigned long count_overruns(struct trace_iterator *iter)
  100. {
  101. unsigned long cnt = atomic_xchg(&dropped_count, 0);
  102. unsigned long over = ring_buffer_overruns(iter->trace_buffer->buffer);
  103. if (over > prev_overruns)
  104. cnt += over - prev_overruns;
  105. prev_overruns = over;
  106. return cnt;
  107. }
  108. static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
  109. char __user *ubuf, size_t cnt, loff_t *ppos)
  110. {
  111. ssize_t ret;
  112. struct header_iter *hiter = iter->private;
  113. struct trace_seq *s = &iter->seq;
  114. unsigned long n;
  115. n = count_overruns(iter);
  116. if (n) {
  117. /* XXX: This is later than where events were lost. */
  118. trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
  119. if (!overrun_detected)
  120. pr_warn("mmiotrace has lost events\n");
  121. overrun_detected = true;
  122. goto print_out;
  123. }
  124. if (!hiter)
  125. return 0;
  126. mmio_print_pcidev(s, hiter->dev);
  127. hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
  128. if (!hiter->dev) {
  129. destroy_header_iter(hiter);
  130. iter->private = NULL;
  131. }
  132. print_out:
  133. ret = trace_seq_to_user(s, ubuf, cnt);
  134. return (ret == -EBUSY) ? 0 : ret;
  135. }
  136. static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
  137. {
  138. struct trace_entry *entry = iter->ent;
  139. struct trace_mmiotrace_rw *field;
  140. struct mmiotrace_rw *rw;
  141. struct trace_seq *s = &iter->seq;
  142. unsigned long long t = ns2usecs(iter->ts);
  143. unsigned long usec_rem = do_div(t, USEC_PER_SEC);
  144. unsigned secs = (unsigned long)t;
  145. trace_assign_type(field, entry);
  146. rw = &field->rw;
  147. switch (rw->opcode) {
  148. case MMIO_READ:
  149. trace_seq_printf(s,
  150. "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
  151. rw->width, secs, usec_rem, rw->map_id,
  152. (unsigned long long)rw->phys,
  153. rw->value, rw->pc, 0);
  154. break;
  155. case MMIO_WRITE:
  156. trace_seq_printf(s,
  157. "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
  158. rw->width, secs, usec_rem, rw->map_id,
  159. (unsigned long long)rw->phys,
  160. rw->value, rw->pc, 0);
  161. break;
  162. case MMIO_UNKNOWN_OP:
  163. trace_seq_printf(s,
  164. "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
  165. "%02lx 0x%lx %d\n",
  166. secs, usec_rem, rw->map_id,
  167. (unsigned long long)rw->phys,
  168. (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
  169. (rw->value >> 0) & 0xff, rw->pc, 0);
  170. break;
  171. default:
  172. trace_seq_puts(s, "rw what?\n");
  173. break;
  174. }
  175. return trace_handle_return(s);
  176. }
  177. static enum print_line_t mmio_print_map(struct trace_iterator *iter)
  178. {
  179. struct trace_entry *entry = iter->ent;
  180. struct trace_mmiotrace_map *field;
  181. struct mmiotrace_map *m;
  182. struct trace_seq *s = &iter->seq;
  183. unsigned long long t = ns2usecs(iter->ts);
  184. unsigned long usec_rem = do_div(t, USEC_PER_SEC);
  185. unsigned secs = (unsigned long)t;
  186. trace_assign_type(field, entry);
  187. m = &field->map;
  188. switch (m->opcode) {
  189. case MMIO_PROBE:
  190. trace_seq_printf(s,
  191. "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
  192. secs, usec_rem, m->map_id,
  193. (unsigned long long)m->phys, m->virt, m->len,
  194. 0UL, 0);
  195. break;
  196. case MMIO_UNPROBE:
  197. trace_seq_printf(s,
  198. "UNMAP %u.%06lu %d 0x%lx %d\n",
  199. secs, usec_rem, m->map_id, 0UL, 0);
  200. break;
  201. default:
  202. trace_seq_puts(s, "map what?\n");
  203. break;
  204. }
  205. return trace_handle_return(s);
  206. }
  207. static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
  208. {
  209. struct trace_entry *entry = iter->ent;
  210. struct print_entry *print = (struct print_entry *)entry;
  211. const char *msg = print->buf;
  212. struct trace_seq *s = &iter->seq;
  213. unsigned long long t = ns2usecs(iter->ts);
  214. unsigned long usec_rem = do_div(t, USEC_PER_SEC);
  215. unsigned secs = (unsigned long)t;
  216. /* The trailing newline must be in the message. */
  217. trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
  218. return trace_handle_return(s);
  219. }
  220. static enum print_line_t mmio_print_line(struct trace_iterator *iter)
  221. {
  222. switch (iter->ent->type) {
  223. case TRACE_MMIO_RW:
  224. return mmio_print_rw(iter);
  225. case TRACE_MMIO_MAP:
  226. return mmio_print_map(iter);
  227. case TRACE_PRINT:
  228. return mmio_print_mark(iter);
  229. default:
  230. return TRACE_TYPE_HANDLED; /* ignore unknown entries */
  231. }
  232. }
  233. static struct tracer mmio_tracer __read_mostly =
  234. {
  235. .name = "mmiotrace",
  236. .init = mmio_trace_init,
  237. .reset = mmio_trace_reset,
  238. .start = mmio_trace_start,
  239. .pipe_open = mmio_pipe_open,
  240. .close = mmio_close,
  241. .read = mmio_read,
  242. .print_line = mmio_print_line,
  243. };
  244. __init static int init_mmio_trace(void)
  245. {
  246. return register_tracer(&mmio_tracer);
  247. }
  248. device_initcall(init_mmio_trace);
  249. static void __trace_mmiotrace_rw(struct trace_array *tr,
  250. struct trace_array_cpu *data,
  251. struct mmiotrace_rw *rw)
  252. {
  253. struct trace_event_call *call = &event_mmiotrace_rw;
  254. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  255. struct ring_buffer_event *event;
  256. struct trace_mmiotrace_rw *entry;
  257. int pc = preempt_count();
  258. event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_RW,
  259. sizeof(*entry), 0, pc);
  260. if (!event) {
  261. atomic_inc(&dropped_count);
  262. return;
  263. }
  264. entry = ring_buffer_event_data(event);
  265. entry->rw = *rw;
  266. if (!call_filter_check_discard(call, entry, buffer, event))
  267. trace_buffer_unlock_commit(tr, buffer, event, 0, pc);
  268. }
  269. void mmio_trace_rw(struct mmiotrace_rw *rw)
  270. {
  271. struct trace_array *tr = mmio_trace_array;
  272. struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, smp_processor_id());
  273. __trace_mmiotrace_rw(tr, data, rw);
  274. }
  275. static void __trace_mmiotrace_map(struct trace_array *tr,
  276. struct trace_array_cpu *data,
  277. struct mmiotrace_map *map)
  278. {
  279. struct trace_event_call *call = &event_mmiotrace_map;
  280. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  281. struct ring_buffer_event *event;
  282. struct trace_mmiotrace_map *entry;
  283. int pc = preempt_count();
  284. event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_MAP,
  285. sizeof(*entry), 0, pc);
  286. if (!event) {
  287. atomic_inc(&dropped_count);
  288. return;
  289. }
  290. entry = ring_buffer_event_data(event);
  291. entry->map = *map;
  292. if (!call_filter_check_discard(call, entry, buffer, event))
  293. trace_buffer_unlock_commit(tr, buffer, event, 0, pc);
  294. }
  295. void mmio_trace_mapping(struct mmiotrace_map *map)
  296. {
  297. struct trace_array *tr = mmio_trace_array;
  298. struct trace_array_cpu *data;
  299. preempt_disable();
  300. data = per_cpu_ptr(tr->trace_buffer.data, smp_processor_id());
  301. __trace_mmiotrace_map(tr, data, map);
  302. preempt_enable();
  303. }
  304. int mmio_trace_printk(const char *fmt, va_list args)
  305. {
  306. return trace_vprintk(0, fmt, args);
  307. }