2 * Memory mapped I/O tracing
4 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
9 #include <linux/kernel.h>
10 #include <linux/mmiotrace.h>
11 #include <linux/pci.h>
12 #include <linux/time.h>
14 #include <asm/atomic.h>
17 #include "trace_output.h"
23 static struct trace_array *mmio_trace_array;
24 static bool overrun_detected;
25 static unsigned long prev_overruns;
26 static atomic_t dropped_count;
28 static void mmio_reset_data(struct trace_array *tr)
30 overrun_detected = false;
33 tracing_reset_online_cpus(tr);
36 static int mmio_trace_init(struct trace_array *tr)
38 pr_debug("in %s\n", __func__);
39 mmio_trace_array = tr;
46 static void mmio_trace_reset(struct trace_array *tr)
48 pr_debug("in %s\n", __func__);
52 mmio_trace_array = NULL;
55 static void mmio_trace_start(struct trace_array *tr)
57 pr_debug("in %s\n", __func__);
61 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
65 resource_size_t start, end;
66 const struct pci_driver *drv = pci_dev_driver(dev);
68 /* XXX: incomplete checks for trace_seq_printf() return value */
69 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
70 dev->bus->number, dev->devfn,
71 dev->vendor, dev->device, dev->irq);
73 * XXX: is pci_resource_to_user() appropriate, since we are
74 * supposed to interpret the __ioremap() phys_addr argument based on
75 * these printed values?
77 for (i = 0; i < 7; i++) {
78 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
79 ret += trace_seq_printf(s, " %llx",
80 (unsigned long long)(start |
81 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
83 for (i = 0; i < 7; i++) {
84 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
85 ret += trace_seq_printf(s, " %llx",
86 dev->resource[i].start < dev->resource[i].end ?
87 (unsigned long long)(end - start) + 1 : 0);
90 ret += trace_seq_printf(s, " %s\n", drv->name);
92 ret += trace_seq_printf(s, " \n");
96 static void destroy_header_iter(struct header_iter *hiter)
100 pci_dev_put(hiter->dev);
104 static void mmio_pipe_open(struct trace_iterator *iter)
106 struct header_iter *hiter;
107 struct trace_seq *s = &iter->seq;
109 trace_seq_printf(s, "VERSION 20070824\n");
111 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
115 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
116 iter->private = hiter;
119 /* XXX: This is not called when the pipe is closed! */
120 static void mmio_close(struct trace_iterator *iter)
122 struct header_iter *hiter = iter->private;
123 destroy_header_iter(hiter);
124 iter->private = NULL;
127 static unsigned long count_overruns(struct trace_iterator *iter)
129 unsigned long cnt = atomic_xchg(&dropped_count, 0);
130 unsigned long over = ring_buffer_overruns(iter->tr->buffer);
132 if (over > prev_overruns)
133 cnt += over - prev_overruns;
134 prev_overruns = over;
138 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
139 char __user *ubuf, size_t cnt, loff_t *ppos)
142 struct header_iter *hiter = iter->private;
143 struct trace_seq *s = &iter->seq;
146 n = count_overruns(iter);
148 /* XXX: This is later than where events were lost. */
149 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
150 if (!overrun_detected)
151 pr_warning("mmiotrace has lost events.\n");
152 overrun_detected = true;
159 mmio_print_pcidev(s, hiter->dev);
160 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
163 destroy_header_iter(hiter);
164 iter->private = NULL;
168 ret = trace_seq_to_user(s, ubuf, cnt);
169 return (ret == -EBUSY) ? 0 : ret;
172 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
174 struct trace_entry *entry = iter->ent;
175 struct trace_mmiotrace_rw *field;
176 struct mmiotrace_rw *rw;
177 struct trace_seq *s = &iter->seq;
178 unsigned long long t = ns2usecs(iter->ts);
179 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
180 unsigned secs = (unsigned long)t;
183 trace_assign_type(field, entry);
186 switch (rw->opcode) {
188 ret = trace_seq_printf(s,
189 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
190 rw->width, secs, usec_rem, rw->map_id,
191 (unsigned long long)rw->phys,
192 rw->value, rw->pc, 0);
195 ret = trace_seq_printf(s,
196 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
197 rw->width, secs, usec_rem, rw->map_id,
198 (unsigned long long)rw->phys,
199 rw->value, rw->pc, 0);
201 case MMIO_UNKNOWN_OP:
202 ret = trace_seq_printf(s,
203 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
205 secs, usec_rem, rw->map_id,
206 (unsigned long long)rw->phys,
207 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
208 (rw->value >> 0) & 0xff, rw->pc, 0);
211 ret = trace_seq_printf(s, "rw what?\n");
215 return TRACE_TYPE_HANDLED;
216 return TRACE_TYPE_PARTIAL_LINE;
219 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
221 struct trace_entry *entry = iter->ent;
222 struct trace_mmiotrace_map *field;
223 struct mmiotrace_map *m;
224 struct trace_seq *s = &iter->seq;
225 unsigned long long t = ns2usecs(iter->ts);
226 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
227 unsigned secs = (unsigned long)t;
230 trace_assign_type(field, entry);
235 ret = trace_seq_printf(s,
236 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
237 secs, usec_rem, m->map_id,
238 (unsigned long long)m->phys, m->virt, m->len,
242 ret = trace_seq_printf(s,
243 "UNMAP %u.%06lu %d 0x%lx %d\n",
244 secs, usec_rem, m->map_id, 0UL, 0);
247 ret = trace_seq_printf(s, "map what?\n");
251 return TRACE_TYPE_HANDLED;
252 return TRACE_TYPE_PARTIAL_LINE;
255 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
257 struct trace_entry *entry = iter->ent;
258 struct print_entry *print = (struct print_entry *)entry;
259 const char *msg = print->buf;
260 struct trace_seq *s = &iter->seq;
261 unsigned long long t = ns2usecs(iter->ts);
262 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
263 unsigned secs = (unsigned long)t;
266 /* The trailing newline must be in the message. */
267 ret = trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
269 return TRACE_TYPE_PARTIAL_LINE;
271 return TRACE_TYPE_HANDLED;
274 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
276 switch (iter->ent->type) {
278 return mmio_print_rw(iter);
280 return mmio_print_map(iter);
282 return mmio_print_mark(iter);
284 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
288 static struct tracer mmio_tracer __read_mostly =
291 .init = mmio_trace_init,
292 .reset = mmio_trace_reset,
293 .start = mmio_trace_start,
294 .pipe_open = mmio_pipe_open,
297 .print_line = mmio_print_line,
300 __init static int init_mmio_trace(void)
302 return register_tracer(&mmio_tracer);
304 device_initcall(init_mmio_trace);
306 static void __trace_mmiotrace_rw(struct trace_array *tr,
307 struct trace_array_cpu *data,
308 struct mmiotrace_rw *rw)
310 struct ring_buffer_event *event;
311 struct trace_mmiotrace_rw *entry;
312 int pc = preempt_count();
314 event = trace_buffer_lock_reserve(tr, TRACE_MMIO_RW,
315 sizeof(*entry), 0, pc);
317 atomic_inc(&dropped_count);
320 entry = ring_buffer_event_data(event);
322 trace_buffer_unlock_commit(tr, event, 0, pc);
325 void mmio_trace_rw(struct mmiotrace_rw *rw)
327 struct trace_array *tr = mmio_trace_array;
328 struct trace_array_cpu *data = tr->data[smp_processor_id()];
329 __trace_mmiotrace_rw(tr, data, rw);
332 static void __trace_mmiotrace_map(struct trace_array *tr,
333 struct trace_array_cpu *data,
334 struct mmiotrace_map *map)
336 struct ring_buffer_event *event;
337 struct trace_mmiotrace_map *entry;
338 int pc = preempt_count();
340 event = trace_buffer_lock_reserve(tr, TRACE_MMIO_MAP,
341 sizeof(*entry), 0, pc);
343 atomic_inc(&dropped_count);
346 entry = ring_buffer_event_data(event);
348 trace_buffer_unlock_commit(tr, event, 0, pc);
351 void mmio_trace_mapping(struct mmiotrace_map *map)
353 struct trace_array *tr = mmio_trace_array;
354 struct trace_array_cpu *data;
357 data = tr->data[smp_processor_id()];
358 __trace_mmiotrace_map(tr, data, map);
362 int mmio_trace_printk(const char *fmt, va_list args)
364 return trace_vprintk(0, fmt, args);