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>
15 static struct trace_array *mmio_trace_array;
17 static void mmio_reset_data(struct trace_array *tr)
21 tr->time_start = ftrace_now(tr->cpu);
23 for_each_online_cpu(cpu)
24 tracing_reset(tr->data[cpu]);
27 static void mmio_trace_init(struct trace_array *tr)
29 pr_debug("in %s\n", __func__);
30 mmio_trace_array = tr;
37 static void mmio_trace_reset(struct trace_array *tr)
39 pr_debug("in %s\n", __func__);
43 mmio_trace_array = NULL;
46 static void mmio_trace_ctrl_update(struct trace_array *tr)
48 pr_debug("in %s\n", __func__);
57 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
61 resource_size_t start, end;
62 const struct pci_driver *drv = pci_dev_driver(dev);
64 /* XXX: incomplete checks for trace_seq_printf() return value */
65 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
66 dev->bus->number, dev->devfn,
67 dev->vendor, dev->device, dev->irq);
69 * XXX: is pci_resource_to_user() appropriate, since we are
70 * supposed to interpret the __ioremap() phys_addr argument based on
71 * these printed values?
73 for (i = 0; i < 7; i++) {
74 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
75 ret += trace_seq_printf(s, " %llx",
76 (unsigned long long)(start |
77 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
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 dev->resource[i].start < dev->resource[i].end ?
83 (unsigned long long)(end - start) + 1 : 0);
86 ret += trace_seq_printf(s, " %s\n", drv->name);
88 ret += trace_seq_printf(s, " \n");
92 /* XXX: This is not called for trace_pipe file! */
93 static void mmio_print_header(struct trace_iterator *iter)
95 struct trace_seq *s = &iter->seq;
96 struct pci_dev *dev = NULL;
98 trace_seq_printf(s, "VERSION 20070824\n");
100 for_each_pci_dev(dev)
101 mmio_print_pcidev(s, dev);
102 /* XXX: return value? What if header is very long? */
105 static int mmio_print_rw(struct trace_iterator *iter)
107 struct trace_entry *entry = iter->ent;
108 struct mmiotrace_rw *rw = &entry->mmiorw;
109 struct trace_seq *s = &iter->seq;
110 unsigned long long t = ns2usecs(entry->t);
111 unsigned long usec_rem = do_div(t, 1000000ULL);
112 unsigned secs = (unsigned long)t;
115 switch (entry->mmiorw.opcode) {
117 ret = trace_seq_printf(s,
118 "R %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
119 rw->width, secs, usec_rem, rw->map_id, rw->phys,
120 rw->value, rw->pc, 0);
123 ret = trace_seq_printf(s,
124 "W %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
125 rw->width, secs, usec_rem, rw->map_id, rw->phys,
126 rw->value, rw->pc, 0);
128 case MMIO_UNKNOWN_OP:
129 ret = trace_seq_printf(s,
130 "UNKNOWN %lu.%06lu %d 0x%lx %02x,%02x,%02x 0x%lx %d\n",
131 secs, usec_rem, rw->map_id, rw->phys,
132 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
133 (rw->value >> 0) & 0xff, rw->pc, 0);
136 ret = trace_seq_printf(s, "rw what?\n");
144 static int mmio_print_map(struct trace_iterator *iter)
146 struct trace_entry *entry = iter->ent;
147 struct mmiotrace_map *m = &entry->mmiomap;
148 struct trace_seq *s = &iter->seq;
149 unsigned long long t = ns2usecs(entry->t);
150 unsigned long usec_rem = do_div(t, 1000000ULL);
151 unsigned secs = (unsigned long)t;
154 switch (entry->mmiorw.opcode) {
156 ret = trace_seq_printf(s,
157 "MAP %lu.%06lu %d 0x%lx 0x%lx 0x%lx 0x%lx %d\n",
158 secs, usec_rem, m->map_id, m->phys, m->virt, m->len,
162 ret = trace_seq_printf(s,
163 "UNMAP %lu.%06lu %d 0x%lx %d\n",
164 secs, usec_rem, m->map_id, 0UL, entry->pid);
167 ret = trace_seq_printf(s, "map what?\n");
175 /* return 0 to abort printing without consuming current entry in pipe mode */
176 static int mmio_print_line(struct trace_iterator *iter)
178 switch (iter->ent->type) {
180 return mmio_print_rw(iter);
182 return mmio_print_map(iter);
184 return 1; /* ignore unknown entries */
188 static struct tracer mmio_tracer __read_mostly =
191 .init = mmio_trace_init,
192 .reset = mmio_trace_reset,
193 .open = mmio_print_header,
194 .ctrl_update = mmio_trace_ctrl_update,
195 .print_line = mmio_print_line,
198 __init static int init_mmio_trace(void)
200 return register_tracer(&mmio_tracer);
202 device_initcall(init_mmio_trace);
204 void mmio_trace_rw(struct mmiotrace_rw *rw)
206 struct trace_array *tr = mmio_trace_array;
207 struct trace_array_cpu *data = tr->data[smp_processor_id()];
208 __trace_mmiotrace_rw(tr, data, rw);
211 void mmio_trace_mapping(struct mmiotrace_map *map)
213 struct trace_array *tr = mmio_trace_array;
214 struct trace_array_cpu *data;
217 data = tr->data[smp_processor_id()];
218 __trace_mmiotrace_map(tr, data, map);