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>
19 static struct trace_array *mmio_trace_array;
20 static bool overrun_detected;
22 static void mmio_reset_data(struct trace_array *tr)
26 overrun_detected = false;
27 tr->time_start = ftrace_now(tr->cpu);
29 for_each_online_cpu(cpu)
30 tracing_reset(tr, cpu);
33 static void mmio_trace_init(struct trace_array *tr)
35 pr_debug("in %s\n", __func__);
36 mmio_trace_array = tr;
43 static void mmio_trace_reset(struct trace_array *tr)
45 pr_debug("in %s\n", __func__);
49 mmio_trace_array = NULL;
52 static void mmio_trace_ctrl_update(struct trace_array *tr)
54 pr_debug("in %s\n", __func__);
63 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
67 resource_size_t start, end;
68 const struct pci_driver *drv = pci_dev_driver(dev);
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);
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?
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)));
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);
92 ret += trace_seq_printf(s, " %s\n", drv->name);
94 ret += trace_seq_printf(s, " \n");
98 static void destroy_header_iter(struct header_iter *hiter)
102 pci_dev_put(hiter->dev);
106 static void mmio_pipe_open(struct trace_iterator *iter)
108 struct header_iter *hiter;
109 struct trace_seq *s = &iter->seq;
111 trace_seq_printf(s, "VERSION 20070824\n");
113 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
117 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
118 iter->private = hiter;
121 /* XXX: This is not called when the pipe is closed! */
122 static void mmio_close(struct trace_iterator *iter)
124 struct header_iter *hiter = iter->private;
125 destroy_header_iter(hiter);
126 iter->private = NULL;
129 static unsigned long count_overruns(struct trace_iterator *iter)
132 unsigned long cnt = 0;
135 for_each_online_cpu(cpu) {
136 cnt += iter->overrun[cpu];
137 iter->overrun[cpu] = 0;
144 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
145 char __user *ubuf, size_t cnt, loff_t *ppos)
148 struct header_iter *hiter = iter->private;
149 struct trace_seq *s = &iter->seq;
152 n = count_overruns(iter);
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;
165 mmio_print_pcidev(s, hiter->dev);
166 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
169 destroy_header_iter(hiter);
170 iter->private = NULL;
174 ret = trace_seq_to_user(s, ubuf, cnt);
175 return (ret == -EBUSY) ? 0 : ret;
178 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
180 struct trace_entry *entry = iter->ent;
181 struct trace_mmiotrace_rw *field;
182 struct mmiotrace_rw *rw;
183 struct trace_seq *s = &iter->seq;
184 unsigned long long t = ns2usecs(iter->ts);
185 unsigned long usec_rem = do_div(t, 1000000ULL);
186 unsigned secs = (unsigned long)t;
189 trace_assign_type(field, entry);
192 switch (rw->opcode) {
194 ret = trace_seq_printf(s,
195 "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,
198 rw->value, rw->pc, 0);
201 ret = trace_seq_printf(s,
202 "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,
205 rw->value, rw->pc, 0);
207 case MMIO_UNKNOWN_OP:
208 ret = trace_seq_printf(s,
209 "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,
212 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
213 (rw->value >> 0) & 0xff, rw->pc, 0);
216 ret = trace_seq_printf(s, "rw what?\n");
220 return TRACE_TYPE_HANDLED;
221 return TRACE_TYPE_PARTIAL_LINE;
224 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
226 struct trace_entry *entry = iter->ent;
227 struct trace_mmiotrace_map *field;
228 struct mmiotrace_map *m;
229 struct trace_seq *s = &iter->seq;
230 unsigned long long t = ns2usecs(iter->ts);
231 unsigned long usec_rem = do_div(t, 1000000ULL);
232 unsigned secs = (unsigned long)t;
235 trace_assign_type(field, entry);
240 ret = trace_seq_printf(s,
241 "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,
247 ret = trace_seq_printf(s,
248 "UNMAP %lu.%06lu %d 0x%lx %d\n",
249 secs, usec_rem, m->map_id, 0UL, 0);
252 ret = trace_seq_printf(s, "map what?\n");
256 return TRACE_TYPE_HANDLED;
257 return TRACE_TYPE_PARTIAL_LINE;
260 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
262 struct trace_entry *entry = iter->ent;
263 struct print_entry *print = (struct print_entry *)entry;
264 const char *msg = print->buf;
265 struct trace_seq *s = &iter->seq;
266 unsigned long long t = ns2usecs(iter->ts);
267 unsigned long usec_rem = do_div(t, 1000000ULL);
268 unsigned secs = (unsigned long)t;
271 /* The trailing newline must be in the message. */
272 ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
274 return TRACE_TYPE_PARTIAL_LINE;
276 if (entry->flags & TRACE_FLAG_CONT)
277 trace_seq_print_cont(s, iter);
279 return TRACE_TYPE_HANDLED;
282 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
284 switch (iter->ent->type) {
286 return mmio_print_rw(iter);
288 return mmio_print_map(iter);
290 return mmio_print_mark(iter);
292 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
296 static struct tracer mmio_tracer __read_mostly =
299 .init = mmio_trace_init,
300 .reset = mmio_trace_reset,
301 .pipe_open = mmio_pipe_open,
304 .ctrl_update = mmio_trace_ctrl_update,
305 .print_line = mmio_print_line,
308 __init static int init_mmio_trace(void)
310 return register_tracer(&mmio_tracer);
312 device_initcall(init_mmio_trace);
314 static void __trace_mmiotrace_rw(struct trace_array *tr,
315 struct trace_array_cpu *data,
316 struct mmiotrace_rw *rw)
318 struct ring_buffer_event *event;
319 struct trace_mmiotrace_rw *entry;
320 unsigned long irq_flags;
322 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
326 entry = ring_buffer_event_data(event);
327 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
328 entry->ent.type = TRACE_MMIO_RW;
330 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
335 void mmio_trace_rw(struct mmiotrace_rw *rw)
337 struct trace_array *tr = mmio_trace_array;
338 struct trace_array_cpu *data = tr->data[smp_processor_id()];
339 __trace_mmiotrace_rw(tr, data, rw);
342 static void __trace_mmiotrace_map(struct trace_array *tr,
343 struct trace_array_cpu *data,
344 struct mmiotrace_map *map)
346 struct ring_buffer_event *event;
347 struct trace_mmiotrace_map *entry;
348 unsigned long irq_flags;
350 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
354 entry = ring_buffer_event_data(event);
355 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
356 entry->ent.type = TRACE_MMIO_MAP;
358 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
363 void mmio_trace_mapping(struct mmiotrace_map *map)
365 struct trace_array *tr = mmio_trace_array;
366 struct trace_array_cpu *data;
369 data = tr->data[smp_processor_id()];
370 __trace_mmiotrace_map(tr, data, map);
374 int mmio_trace_printk(const char *fmt, va_list args)
376 return trace_vprintk(0, fmt, args);