trace_mmiotrace.c 9.0 KB

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