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;
21 static unsigned long prev_overruns;
23 static void mmio_reset_data(struct trace_array *tr)
25 overrun_detected = false;
28 tracing_reset_online_cpus(tr);
31 static int mmio_trace_init(struct trace_array *tr)
33 pr_debug("in %s\n", __func__);
34 mmio_trace_array = tr;
41 static void mmio_trace_reset(struct trace_array *tr)
43 pr_debug("in %s\n", __func__);
47 mmio_trace_array = NULL;
50 static void mmio_trace_start(struct trace_array *tr)
52 pr_debug("in %s\n", __func__);
56 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
60 resource_size_t start, end;
61 const struct pci_driver *drv = pci_dev_driver(dev);
63 /* XXX: incomplete checks for trace_seq_printf() return value */
64 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
65 dev->bus->number, dev->devfn,
66 dev->vendor, dev->device, dev->irq);
68 * XXX: is pci_resource_to_user() appropriate, since we are
69 * supposed to interpret the __ioremap() phys_addr argument based on
70 * these printed values?
72 for (i = 0; i < 7; i++) {
73 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
74 ret += trace_seq_printf(s, " %llx",
75 (unsigned long long)(start |
76 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
78 for (i = 0; i < 7; i++) {
79 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
80 ret += trace_seq_printf(s, " %llx",
81 dev->resource[i].start < dev->resource[i].end ?
82 (unsigned long long)(end - start) + 1 : 0);
85 ret += trace_seq_printf(s, " %s\n", drv->name);
87 ret += trace_seq_printf(s, " \n");
91 static void destroy_header_iter(struct header_iter *hiter)
95 pci_dev_put(hiter->dev);
99 static void mmio_pipe_open(struct trace_iterator *iter)
101 struct header_iter *hiter;
102 struct trace_seq *s = &iter->seq;
104 trace_seq_printf(s, "VERSION 20070824\n");
106 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
110 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
111 iter->private = hiter;
114 /* XXX: This is not called when the pipe is closed! */
115 static void mmio_close(struct trace_iterator *iter)
117 struct header_iter *hiter = iter->private;
118 destroy_header_iter(hiter);
119 iter->private = NULL;
122 static unsigned long count_overruns(struct trace_iterator *iter)
124 unsigned long cnt = 0;
125 unsigned long over = ring_buffer_overruns(iter->tr->buffer);
127 if (over > prev_overruns)
128 cnt = over - prev_overruns;
129 prev_overruns = over;
133 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
134 char __user *ubuf, size_t cnt, loff_t *ppos)
137 struct header_iter *hiter = iter->private;
138 struct trace_seq *s = &iter->seq;
141 n = count_overruns(iter);
143 /* XXX: This is later than where events were lost. */
144 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
145 if (!overrun_detected)
146 pr_warning("mmiotrace has lost events.\n");
147 overrun_detected = true;
154 mmio_print_pcidev(s, hiter->dev);
155 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
158 destroy_header_iter(hiter);
159 iter->private = NULL;
163 ret = trace_seq_to_user(s, ubuf, cnt);
164 return (ret == -EBUSY) ? 0 : ret;
167 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
169 struct trace_entry *entry = iter->ent;
170 struct trace_mmiotrace_rw *field;
171 struct mmiotrace_rw *rw;
172 struct trace_seq *s = &iter->seq;
173 unsigned long long t = ns2usecs(iter->ts);
174 unsigned long usec_rem = do_div(t, 1000000ULL);
175 unsigned secs = (unsigned long)t;
178 trace_assign_type(field, entry);
181 switch (rw->opcode) {
183 ret = trace_seq_printf(s,
184 "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
185 rw->width, secs, usec_rem, rw->map_id,
186 (unsigned long long)rw->phys,
187 rw->value, rw->pc, 0);
190 ret = trace_seq_printf(s,
191 "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
192 rw->width, secs, usec_rem, rw->map_id,
193 (unsigned long long)rw->phys,
194 rw->value, rw->pc, 0);
196 case MMIO_UNKNOWN_OP:
197 ret = trace_seq_printf(s,
198 "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
199 secs, usec_rem, rw->map_id,
200 (unsigned long long)rw->phys,
201 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
202 (rw->value >> 0) & 0xff, rw->pc, 0);
205 ret = trace_seq_printf(s, "rw what?\n");
209 return TRACE_TYPE_HANDLED;
210 return TRACE_TYPE_PARTIAL_LINE;
213 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
215 struct trace_entry *entry = iter->ent;
216 struct trace_mmiotrace_map *field;
217 struct mmiotrace_map *m;
218 struct trace_seq *s = &iter->seq;
219 unsigned long long t = ns2usecs(iter->ts);
220 unsigned long usec_rem = do_div(t, 1000000ULL);
221 unsigned secs = (unsigned long)t;
224 trace_assign_type(field, entry);
229 ret = trace_seq_printf(s,
230 "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
231 secs, usec_rem, m->map_id,
232 (unsigned long long)m->phys, m->virt, m->len,
236 ret = trace_seq_printf(s,
237 "UNMAP %lu.%06lu %d 0x%lx %d\n",
238 secs, usec_rem, m->map_id, 0UL, 0);
241 ret = trace_seq_printf(s, "map what?\n");
245 return TRACE_TYPE_HANDLED;
246 return TRACE_TYPE_PARTIAL_LINE;
249 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
251 struct trace_entry *entry = iter->ent;
252 struct print_entry *print = (struct print_entry *)entry;
253 const char *msg = print->buf;
254 struct trace_seq *s = &iter->seq;
255 unsigned long long t = ns2usecs(iter->ts);
256 unsigned long usec_rem = do_div(t, 1000000ULL);
257 unsigned secs = (unsigned long)t;
260 /* The trailing newline must be in the message. */
261 ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
263 return TRACE_TYPE_PARTIAL_LINE;
265 if (entry->flags & TRACE_FLAG_CONT)
266 trace_seq_print_cont(s, iter);
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),
315 entry = ring_buffer_event_data(event);
316 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
317 entry->ent.type = TRACE_MMIO_RW;
319 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
324 void mmio_trace_rw(struct mmiotrace_rw *rw)
326 struct trace_array *tr = mmio_trace_array;
327 struct trace_array_cpu *data = tr->data[smp_processor_id()];
328 __trace_mmiotrace_rw(tr, data, rw);
331 static void __trace_mmiotrace_map(struct trace_array *tr,
332 struct trace_array_cpu *data,
333 struct mmiotrace_map *map)
335 struct ring_buffer_event *event;
336 struct trace_mmiotrace_map *entry;
337 unsigned long irq_flags;
339 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
343 entry = ring_buffer_event_data(event);
344 tracing_generic_entry_update(&entry->ent, 0, preempt_count());
345 entry->ent.type = TRACE_MMIO_MAP;
347 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
352 void mmio_trace_mapping(struct mmiotrace_map *map)
354 struct trace_array *tr = mmio_trace_array;
355 struct trace_array_cpu *data;
358 data = tr->data[smp_processor_id()];
359 __trace_mmiotrace_map(tr, data, map);
363 int mmio_trace_printk(const char *fmt, va_list args)
365 return trace_vprintk(0, -1, fmt, args);