trace_mmiotrace.c 8.6 KB

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