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 <asm/atomic.h>
15 #include "trace_output.h"
21 static struct trace_array *mmio_trace_array;
22 static bool overrun_detected;
23 static unsigned long prev_overruns;
24 static atomic_t dropped_count;
26 static void mmio_reset_data(struct trace_array *tr)
28 overrun_detected = false;
31 tracing_reset_online_cpus(tr);
34 static int mmio_trace_init(struct trace_array *tr)
36 pr_debug("in %s\n", __func__);
37 mmio_trace_array = tr;
44 static void mmio_trace_reset(struct trace_array *tr)
46 pr_debug("in %s\n", __func__);
50 mmio_trace_array = NULL;
53 static void mmio_trace_start(struct trace_array *tr)
55 pr_debug("in %s\n", __func__);
59 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
63 resource_size_t start, end;
64 const struct pci_driver *drv = pci_dev_driver(dev);
66 /* XXX: incomplete checks for trace_seq_printf() return value */
67 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
68 dev->bus->number, dev->devfn,
69 dev->vendor, dev->device, dev->irq);
71 * XXX: is pci_resource_to_user() appropriate, since we are
72 * supposed to interpret the __ioremap() phys_addr argument based on
73 * these printed values?
75 for (i = 0; i < 7; i++) {
76 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
77 ret += trace_seq_printf(s, " %llx",
78 (unsigned long long)(start |
79 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
81 for (i = 0; i < 7; i++) {
82 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
83 ret += trace_seq_printf(s, " %llx",
84 dev->resource[i].start < dev->resource[i].end ?
85 (unsigned long long)(end - start) + 1 : 0);
88 ret += trace_seq_printf(s, " %s\n", drv->name);
90 ret += trace_seq_printf(s, " \n");
94 static void destroy_header_iter(struct header_iter *hiter)
98 pci_dev_put(hiter->dev);
102 static void mmio_pipe_open(struct trace_iterator *iter)
104 struct header_iter *hiter;
105 struct trace_seq *s = &iter->seq;
107 trace_seq_printf(s, "VERSION 20070824\n");
109 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
113 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
114 iter->private = hiter;
117 /* XXX: This is not called when the pipe is closed! */
118 static void mmio_close(struct trace_iterator *iter)
120 struct header_iter *hiter = iter->private;
121 destroy_header_iter(hiter);
122 iter->private = NULL;
125 static unsigned long count_overruns(struct trace_iterator *iter)
127 unsigned long cnt = atomic_xchg(&dropped_count, 0);
128 unsigned long over = ring_buffer_overruns(iter->tr->buffer);
130 if (over > prev_overruns)
131 cnt += over - prev_overruns;
132 prev_overruns = over;
136 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
137 char __user *ubuf, size_t cnt, loff_t *ppos)
140 struct header_iter *hiter = iter->private;
141 struct trace_seq *s = &iter->seq;
144 n = count_overruns(iter);
146 /* XXX: This is later than where events were lost. */
147 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
148 if (!overrun_detected)
149 pr_warning("mmiotrace has lost events.\n");
150 overrun_detected = true;
157 mmio_print_pcidev(s, hiter->dev);
158 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
161 destroy_header_iter(hiter);
162 iter->private = NULL;
166 ret = trace_seq_to_user(s, ubuf, cnt);
167 return (ret == -EBUSY) ? 0 : ret;
170 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
172 struct trace_entry *entry = iter->ent;
173 struct trace_mmiotrace_rw *field;
174 struct mmiotrace_rw *rw;
175 struct trace_seq *s = &iter->seq;
176 unsigned long long t = ns2usecs(iter->ts);
177 unsigned long usec_rem = do_div(t, 1000000ULL);
178 unsigned secs = (unsigned long)t;
181 trace_assign_type(field, entry);
184 switch (rw->opcode) {
186 ret = trace_seq_printf(s,
187 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
188 rw->width, secs, usec_rem, rw->map_id,
189 (unsigned long long)rw->phys,
190 rw->value, rw->pc, 0);
193 ret = trace_seq_printf(s,
194 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
195 rw->width, secs, usec_rem, rw->map_id,
196 (unsigned long long)rw->phys,
197 rw->value, rw->pc, 0);
199 case MMIO_UNKNOWN_OP:
200 ret = trace_seq_printf(s,
201 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
203 secs, usec_rem, rw->map_id,
204 (unsigned long long)rw->phys,
205 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
206 (rw->value >> 0) & 0xff, rw->pc, 0);
209 ret = trace_seq_printf(s, "rw what?\n");
213 return TRACE_TYPE_HANDLED;
214 return TRACE_TYPE_PARTIAL_LINE;
217 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
219 struct trace_entry *entry = iter->ent;
220 struct trace_mmiotrace_map *field;
221 struct mmiotrace_map *m;
222 struct trace_seq *s = &iter->seq;
223 unsigned long long t = ns2usecs(iter->ts);
224 unsigned long usec_rem = do_div(t, 1000000ULL);
225 unsigned secs = (unsigned long)t;
228 trace_assign_type(field, entry);
233 ret = trace_seq_printf(s,
234 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
235 secs, usec_rem, m->map_id,
236 (unsigned long long)m->phys, m->virt, m->len,
240 ret = trace_seq_printf(s,
241 "UNMAP %u.%06lu %d 0x%lx %d\n",
242 secs, usec_rem, m->map_id, 0UL, 0);
245 ret = trace_seq_printf(s, "map what?\n");
249 return TRACE_TYPE_HANDLED;
250 return TRACE_TYPE_PARTIAL_LINE;
253 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
255 struct trace_entry *entry = iter->ent;
256 struct print_entry *print = (struct print_entry *)entry;
257 const char *msg = print->buf;
258 struct trace_seq *s = &iter->seq;
259 unsigned long long t = ns2usecs(iter->ts);
260 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
261 unsigned secs = (unsigned long)t;
264 /* The trailing newline must be in the message. */
265 ret = trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
267 return TRACE_TYPE_PARTIAL_LINE;
269 return TRACE_TYPE_HANDLED;
272 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
274 switch (iter->ent->type) {
276 return mmio_print_rw(iter);
278 return mmio_print_map(iter);
280 return mmio_print_mark(iter);
282 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
286 static struct tracer mmio_tracer __read_mostly =
289 .init = mmio_trace_init,
290 .reset = mmio_trace_reset,
291 .start = mmio_trace_start,
292 .pipe_open = mmio_pipe_open,
295 .print_line = mmio_print_line,
298 __init static int init_mmio_trace(void)
300 return register_tracer(&mmio_tracer);
302 device_initcall(init_mmio_trace);
304 static void __trace_mmiotrace_rw(struct trace_array *tr,
305 struct trace_array_cpu *data,
306 struct mmiotrace_rw *rw)
308 struct ring_buffer_event *event;
309 struct trace_mmiotrace_rw *entry;
310 int pc = preempt_count();
312 event = trace_buffer_lock_reserve(tr, TRACE_MMIO_RW,
313 sizeof(*entry), 0, pc);
315 atomic_inc(&dropped_count);
318 entry = ring_buffer_event_data(event);
320 trace_buffer_unlock_commit(tr, event, 0, pc);
323 void mmio_trace_rw(struct mmiotrace_rw *rw)
325 struct trace_array *tr = mmio_trace_array;
326 struct trace_array_cpu *data = tr->data[smp_processor_id()];
327 __trace_mmiotrace_rw(tr, data, rw);
330 static void __trace_mmiotrace_map(struct trace_array *tr,
331 struct trace_array_cpu *data,
332 struct mmiotrace_map *map)
334 struct ring_buffer_event *event;
335 struct trace_mmiotrace_map *entry;
336 int pc = preempt_count();
338 event = trace_buffer_lock_reserve(tr, TRACE_MMIO_MAP,
339 sizeof(*entry), 0, pc);
341 atomic_inc(&dropped_count);
344 entry = ring_buffer_event_data(event);
346 trace_buffer_unlock_commit(tr, event, 0, pc);
349 void mmio_trace_mapping(struct mmiotrace_map *map)
351 struct trace_array *tr = mmio_trace_array;
352 struct trace_array_cpu *data;
355 data = tr->data[smp_processor_id()];
356 __trace_mmiotrace_map(tr, data, map);
360 int mmio_trace_printk(const char *fmt, va_list args)
362 return trace_vprintk(0, fmt, args);