blob: f28484618ff0de99b0c9d5062f23fe8eec25070a [file] [log] [blame]
Pekka Paalanenf984b512008-05-12 21:20:57 +02001/*
2 * Memory mapped I/O tracing
3 *
4 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
5 */
6
7#define DEBUG 1
8
9#include <linux/kernel.h>
10#include <linux/mmiotrace.h>
Pekka Paalanen13829532008-05-12 21:20:58 +020011#include <linux/pci.h>
Pekka Paalanenf984b512008-05-12 21:20:57 +020012
13#include "trace.h"
14
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +020015struct header_iter {
16 struct pci_dev *dev;
17};
18
Pekka Paalanenf984b512008-05-12 21:20:57 +020019static struct trace_array *mmio_trace_array;
Pekka Paalanen20392382008-05-12 21:21:02 +020020static bool overrun_detected;
Pekka Paalanenf984b512008-05-12 21:20:57 +020021
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020022static void mmio_reset_data(struct trace_array *tr)
23{
24 int cpu;
25
Pekka Paalanen20392382008-05-12 21:21:02 +020026 overrun_detected = false;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020027 tr->time_start = ftrace_now(tr->cpu);
28
29 for_each_online_cpu(cpu)
Steven Rostedt3928a8a2008-09-29 23:02:41 -040030 tracing_reset(tr, cpu);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020031}
Pekka Paalanenf984b512008-05-12 21:20:57 +020032
33static void mmio_trace_init(struct trace_array *tr)
34{
35 pr_debug("in %s\n", __func__);
36 mmio_trace_array = tr;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020037 if (tr->ctrl) {
38 mmio_reset_data(tr);
Pekka Paalanenf984b512008-05-12 21:20:57 +020039 enable_mmiotrace();
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020040 }
Pekka Paalanenf984b512008-05-12 21:20:57 +020041}
42
43static void mmio_trace_reset(struct trace_array *tr)
44{
45 pr_debug("in %s\n", __func__);
46 if (tr->ctrl)
47 disable_mmiotrace();
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020048 mmio_reset_data(tr);
49 mmio_trace_array = NULL;
Pekka Paalanenf984b512008-05-12 21:20:57 +020050}
51
52static void mmio_trace_ctrl_update(struct trace_array *tr)
53{
54 pr_debug("in %s\n", __func__);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020055 if (tr->ctrl) {
56 mmio_reset_data(tr);
Pekka Paalanenf984b512008-05-12 21:20:57 +020057 enable_mmiotrace();
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020058 } else {
Pekka Paalanenf984b512008-05-12 21:20:57 +020059 disable_mmiotrace();
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020060 }
61}
62
Pekka Paalanen13829532008-05-12 21:20:58 +020063static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
64{
65 int ret = 0;
66 int i;
67 resource_size_t start, end;
68 const struct pci_driver *drv = pci_dev_driver(dev);
69
70 /* XXX: incomplete checks for trace_seq_printf() return value */
71 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
72 dev->bus->number, dev->devfn,
73 dev->vendor, dev->device, dev->irq);
74 /*
75 * XXX: is pci_resource_to_user() appropriate, since we are
76 * supposed to interpret the __ioremap() phys_addr argument based on
77 * these printed values?
78 */
79 for (i = 0; i < 7; i++) {
80 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
81 ret += trace_seq_printf(s, " %llx",
82 (unsigned long long)(start |
83 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
84 }
85 for (i = 0; i < 7; i++) {
86 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
87 ret += trace_seq_printf(s, " %llx",
88 dev->resource[i].start < dev->resource[i].end ?
89 (unsigned long long)(end - start) + 1 : 0);
90 }
91 if (drv)
92 ret += trace_seq_printf(s, " %s\n", drv->name);
93 else
94 ret += trace_seq_printf(s, " \n");
95 return ret;
96}
97
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +020098static void destroy_header_iter(struct header_iter *hiter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020099{
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200100 if (!hiter)
101 return;
102 pci_dev_put(hiter->dev);
103 kfree(hiter);
104}
105
106static void mmio_pipe_open(struct trace_iterator *iter)
107{
108 struct header_iter *hiter;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200109 struct trace_seq *s = &iter->seq;
Pekka Paalanen13829532008-05-12 21:20:58 +0200110
111 trace_seq_printf(s, "VERSION 20070824\n");
112
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200113 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
114 if (!hiter)
115 return;
116
117 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
118 iter->private = hiter;
119}
120
121/* XXX: This is not called when the pipe is closed! */
122static void mmio_close(struct trace_iterator *iter)
123{
124 struct header_iter *hiter = iter->private;
125 destroy_header_iter(hiter);
126 iter->private = NULL;
127}
128
Pekka Paalanen20392382008-05-12 21:21:02 +0200129static unsigned long count_overruns(struct trace_iterator *iter)
130{
131 int cpu;
132 unsigned long cnt = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400133/* FIXME: */
134#if 0
Pekka Paalanen20392382008-05-12 21:21:02 +0200135 for_each_online_cpu(cpu) {
136 cnt += iter->overrun[cpu];
137 iter->overrun[cpu] = 0;
138 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400139#endif
140 (void)cpu;
Pekka Paalanen20392382008-05-12 21:21:02 +0200141 return cnt;
142}
143
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200144static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
145 char __user *ubuf, size_t cnt, loff_t *ppos)
146{
147 ssize_t ret;
148 struct header_iter *hiter = iter->private;
149 struct trace_seq *s = &iter->seq;
Pekka Paalanen20392382008-05-12 21:21:02 +0200150 unsigned long n;
151
152 n = count_overruns(iter);
153 if (n) {
154 /* XXX: This is later than where events were lost. */
155 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
156 if (!overrun_detected)
157 pr_warning("mmiotrace has lost events.\n");
158 overrun_detected = true;
159 goto print_out;
160 }
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200161
162 if (!hiter)
163 return 0;
164
165 mmio_print_pcidev(s, hiter->dev);
166 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
167
168 if (!hiter->dev) {
169 destroy_header_iter(hiter);
170 iter->private = NULL;
171 }
172
Pekka Paalanen20392382008-05-12 21:21:02 +0200173print_out:
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200174 ret = trace_seq_to_user(s, ubuf, cnt);
175 return (ret == -EBUSY) ? 0 : ret;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200176}
177
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200178static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200179{
180 struct trace_entry *entry = iter->ent;
Steven Rostedt7104f302008-10-01 10:52:51 -0400181 struct trace_mmiotrace_rw *field;
182 struct mmiotrace_rw *rw;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200183 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400184 unsigned long long t = ns2usecs(iter->ts);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200185 unsigned long usec_rem = do_div(t, 1000000ULL);
186 unsigned secs = (unsigned long)t;
187 int ret = 1;
188
Steven Rostedt7104f302008-10-01 10:52:51 -0400189 trace_assign_type(field, entry);
190 rw = &field->rw;
191
Steven Rostedt777e2082008-09-29 23:02:42 -0400192 switch (rw->opcode) {
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200193 case MMIO_READ:
194 ret = trace_seq_printf(s,
Pekka Paalanendee310d2008-05-12 21:21:03 +0200195 "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
196 rw->width, secs, usec_rem, rw->map_id,
197 (unsigned long long)rw->phys,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200198 rw->value, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200199 break;
200 case MMIO_WRITE:
201 ret = trace_seq_printf(s,
Pekka Paalanendee310d2008-05-12 21:21:03 +0200202 "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
203 rw->width, secs, usec_rem, rw->map_id,
204 (unsigned long long)rw->phys,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200205 rw->value, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200206 break;
207 case MMIO_UNKNOWN_OP:
208 ret = trace_seq_printf(s,
Pekka Paalanendee310d2008-05-12 21:21:03 +0200209 "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
210 secs, usec_rem, rw->map_id,
211 (unsigned long long)rw->phys,
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200212 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200213 (rw->value >> 0) & 0xff, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200214 break;
215 default:
216 ret = trace_seq_printf(s, "rw what?\n");
217 break;
218 }
219 if (ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200220 return TRACE_TYPE_HANDLED;
221 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200222}
223
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200224static enum print_line_t mmio_print_map(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200225{
226 struct trace_entry *entry = iter->ent;
Steven Rostedt7104f302008-10-01 10:52:51 -0400227 struct trace_mmiotrace_map *field;
228 struct mmiotrace_map *m;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200229 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400230 unsigned long long t = ns2usecs(iter->ts);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200231 unsigned long usec_rem = do_div(t, 1000000ULL);
232 unsigned secs = (unsigned long)t;
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200233 int ret;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200234
Steven Rostedt7104f302008-10-01 10:52:51 -0400235 trace_assign_type(field, entry);
236 m = &field->map;
237
Steven Rostedt777e2082008-09-29 23:02:42 -0400238 switch (m->opcode) {
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200239 case MMIO_PROBE:
240 ret = trace_seq_printf(s,
Pekka Paalanendee310d2008-05-12 21:21:03 +0200241 "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
242 secs, usec_rem, m->map_id,
243 (unsigned long long)m->phys, m->virt, m->len,
Pekka Paalanene0fd5c22008-05-12 21:21:02 +0200244 0UL, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200245 break;
246 case MMIO_UNPROBE:
247 ret = trace_seq_printf(s,
248 "UNMAP %lu.%06lu %d 0x%lx %d\n",
Pekka Paalanene0fd5c22008-05-12 21:21:02 +0200249 secs, usec_rem, m->map_id, 0UL, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200250 break;
251 default:
252 ret = trace_seq_printf(s, "map what?\n");
253 break;
254 }
255 if (ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200256 return TRACE_TYPE_HANDLED;
257 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200258}
259
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200260static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300261{
262 struct trace_entry *entry = iter->ent;
Steven Rostedt777e2082008-09-29 23:02:42 -0400263 struct print_entry *print = (struct print_entry *)entry;
264 const char *msg = print->buf;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300265 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400266 unsigned long long t = ns2usecs(iter->ts);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300267 unsigned long usec_rem = do_div(t, 1000000ULL);
268 unsigned secs = (unsigned long)t;
269 int ret;
270
271 /* The trailing newline must be in the message. */
272 ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
273 if (!ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200274 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300275
Steven Rostedt777e2082008-09-29 23:02:42 -0400276 if (entry->flags & TRACE_FLAG_CONT)
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300277 trace_seq_print_cont(s, iter);
278
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200279 return TRACE_TYPE_HANDLED;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300280}
281
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200282static enum print_line_t mmio_print_line(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200283{
284 switch (iter->ent->type) {
285 case TRACE_MMIO_RW:
286 return mmio_print_rw(iter);
287 case TRACE_MMIO_MAP:
288 return mmio_print_map(iter);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300289 case TRACE_PRINT:
290 return mmio_print_mark(iter);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200291 default:
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200292 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200293 }
Pekka Paalanenf984b512008-05-12 21:20:57 +0200294}
295
296static struct tracer mmio_tracer __read_mostly =
297{
298 .name = "mmiotrace",
299 .init = mmio_trace_init,
300 .reset = mmio_trace_reset,
Pekka Paalanend0a7e8ca2008-05-12 21:21:02 +0200301 .pipe_open = mmio_pipe_open,
302 .close = mmio_close,
303 .read = mmio_read,
Pekka Paalanenf984b512008-05-12 21:20:57 +0200304 .ctrl_update = mmio_trace_ctrl_update,
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200305 .print_line = mmio_print_line,
Pekka Paalanenf984b512008-05-12 21:20:57 +0200306};
307
308__init static int init_mmio_trace(void)
309{
Pekka Paalanenf984b512008-05-12 21:20:57 +0200310 return register_tracer(&mmio_tracer);
311}
312device_initcall(init_mmio_trace);
313
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300314static void __trace_mmiotrace_rw(struct trace_array *tr,
315 struct trace_array_cpu *data,
316 struct mmiotrace_rw *rw)
317{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400318 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400319 struct trace_mmiotrace_rw *entry;
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300320 unsigned long irq_flags;
321
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400322 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
323 &irq_flags);
324 if (!event)
325 return;
326 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400327 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
Steven Rostedt777e2082008-09-29 23:02:42 -0400328 entry->ent.type = TRACE_MMIO_RW;
329 entry->rw = *rw;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400330 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300331
332 trace_wake_up();
333}
334
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200335void mmio_trace_rw(struct mmiotrace_rw *rw)
Pekka Paalanenf984b512008-05-12 21:20:57 +0200336{
337 struct trace_array *tr = mmio_trace_array;
338 struct trace_array_cpu *data = tr->data[smp_processor_id()];
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200339 __trace_mmiotrace_rw(tr, data, rw);
340}
Pekka Paalanenf984b512008-05-12 21:20:57 +0200341
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300342static void __trace_mmiotrace_map(struct trace_array *tr,
343 struct trace_array_cpu *data,
344 struct mmiotrace_map *map)
345{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400346 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400347 struct trace_mmiotrace_map *entry;
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300348 unsigned long irq_flags;
349
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400350 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
351 &irq_flags);
352 if (!event)
353 return;
354 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400355 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
Steven Rostedt777e2082008-09-29 23:02:42 -0400356 entry->ent.type = TRACE_MMIO_MAP;
357 entry->map = *map;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400358 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300359
360 trace_wake_up();
361}
362
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200363void mmio_trace_mapping(struct mmiotrace_map *map)
364{
365 struct trace_array *tr = mmio_trace_array;
366 struct trace_array_cpu *data;
367
368 preempt_disable();
369 data = tr->data[smp_processor_id()];
370 __trace_mmiotrace_map(tr, data, map);
371 preempt_enable();
Pekka Paalanenf984b512008-05-12 21:20:57 +0200372}
Pekka Paalanen9e57fb32008-09-16 22:00:34 +0300373
374int mmio_trace_printk(const char *fmt, va_list args)
375{
376 return trace_vprintk(0, fmt, args);
377}