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 %lu.%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 %lu.%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 %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
202 secs, usec_rem, rw->map_id,
203 (unsigned long long)rw->phys,
204 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
205 (rw->value >> 0) & 0xff, rw->pc, 0);
208 ret = trace_seq_printf(s, "rw what?\n");
212 return TRACE_TYPE_HANDLED;
213 return TRACE_TYPE_PARTIAL_LINE;
216 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
218 struct trace_entry *entry = iter->ent;
219 struct trace_mmiotrace_map *field;
220 struct mmiotrace_map *m;
221 struct trace_seq *s = &iter->seq;
222 unsigned long long t = ns2usecs(iter->ts);
223 unsigned long usec_rem = do_div(t, 1000000ULL);
224 unsigned secs = (unsigned long)t;
227 trace_assign_type(field, entry);
232 ret = trace_seq_printf(s,
233 "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
234 secs, usec_rem, m->map_id,
235 (unsigned long long)m->phys, m->virt, m->len,
239 ret = trace_seq_printf(s,
240 "UNMAP %lu.%06lu %d 0x%lx %d\n",
241 secs, usec_rem, m->map_id, 0UL, 0);
244 ret = trace_seq_printf(s, "map what?\n");
248 return TRACE_TYPE_HANDLED;
249 return TRACE_TYPE_PARTIAL_LINE;
252 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
254 struct trace_entry *entry = iter->ent;
255 struct print_entry *print = (struct print_entry *)entry;
256 const char *msg = print->buf;
257 struct trace_seq *s = &iter->seq;
258 unsigned long long t = ns2usecs(iter->ts);
259 unsigned long usec_rem = do_div(t, 1000000ULL);
260 unsigned secs = (unsigned long)t;
263 /* The trailing newline must be in the message. */
264 ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
266 return TRACE_TYPE_PARTIAL_LINE;
268 return TRACE_TYPE_HANDLED;
271 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
273 switch (iter->ent->type) {
275 return mmio_print_rw(iter);
277 return mmio_print_map(iter);
279 return mmio_print_mark(iter);
281 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
285 static struct tracer mmio_tracer __read_mostly =
288 .init = mmio_trace_init,
289 .reset = mmio_trace_reset,
290 .start = mmio_trace_start,
291 .pipe_open = mmio_pipe_open,
294 .print_line = mmio_print_line,
297 __init static int init_mmio_trace(void)
299 return register_tracer(&mmio_tracer);
301 device_initcall(init_mmio_trace);
303 static void __trace_mmiotrace_rw(struct trace_array *tr,
304 struct trace_array_cpu *data,
305 struct mmiotrace_rw *rw)
307 struct ring_buffer_event *event;
308 struct trace_mmiotrace_rw *entry;
309 unsigned long irq_flags;
311 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
314 atomic_inc(&dropped_count);
317 entry = ring_buffer_event_data(event);
318 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
319 entry->ent.type = TRACE_MMIO_RW;
321 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
326 void mmio_trace_rw(struct mmiotrace_rw *rw)
328 struct trace_array *tr = mmio_trace_array;
329 struct trace_array_cpu *data = tr->data[smp_processor_id()];
330 __trace_mmiotrace_rw(tr, data, rw);
333 static void __trace_mmiotrace_map(struct trace_array *tr,
334 struct trace_array_cpu *data,
335 struct mmiotrace_map *map)
337 struct ring_buffer_event *event;
338 struct trace_mmiotrace_map *entry;
339 unsigned long irq_flags;
341 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
344 atomic_inc(&dropped_count);
347 entry = ring_buffer_event_data(event);
348 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
349 entry->ent.type = TRACE_MMIO_MAP;
351 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
356 void mmio_trace_mapping(struct mmiotrace_map *map)
358 struct trace_array *tr = mmio_trace_array;
359 struct trace_array_cpu *data;
362 data = tr->data[smp_processor_id()];
363 __trace_mmiotrace_map(tr, data, map);
367 int mmio_trace_printk(const char *fmt, va_list args)
369 return trace_vprintk(0, -1, fmt, args);