blob: 017fa376505d29a43fe1d69103047fe0365accb4 [file] [log] [blame]
Pekka Paalanenf984b512008-05-12 21:20:57 +02001/*
2 * Memory mapped I/O tracing
3 *
4 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
5 */
6
7#define DEBUG 1
8
9#include <linux/kernel.h>
10#include <linux/mmiotrace.h>
Pekka Paalanen13829532008-05-12 21:20:58 +020011#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Li Zefana5dec552009-03-27 14:55:44 +080013#include <linux/time.h>
14
Pekka Paalanen173ed242009-01-06 13:57:11 +020015#include <asm/atomic.h>
Pekka Paalanenf984b512008-05-12 21:20:57 +020016
17#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050018#include "trace_output.h"
Pekka Paalanenf984b512008-05-12 21:20:57 +020019
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +020020struct header_iter {
21 struct pci_dev *dev;
22};
23
Pekka Paalanenf984b512008-05-12 21:20:57 +020024static struct trace_array *mmio_trace_array;
Pekka Paalanen20392382008-05-12 21:21:02 +020025static bool overrun_detected;
Pekka Paalanen7ee17682008-11-23 21:24:30 +020026static unsigned long prev_overruns;
Pekka Paalanen173ed242009-01-06 13:57:11 +020027static atomic_t dropped_count;
Pekka Paalanenf984b512008-05-12 21:20:57 +020028
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020029static void mmio_reset_data(struct trace_array *tr)
30{
Pekka Paalanen20392382008-05-12 21:21:02 +020031 overrun_detected = false;
Pekka Paalanen7ee17682008-11-23 21:24:30 +020032 prev_overruns = 0;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020033
Pekka J Enberg213cc062008-12-19 12:08:39 +020034 tracing_reset_online_cpus(tr);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020035}
Pekka Paalanenf984b512008-05-12 21:20:57 +020036
Frederic Weisbecker1c800252008-11-16 05:57:26 +010037static int mmio_trace_init(struct trace_array *tr)
Pekka Paalanenf984b512008-05-12 21:20:57 +020038{
39 pr_debug("in %s\n", __func__);
40 mmio_trace_array = tr;
Steven Rostedtc76f0692008-11-07 22:36:02 -050041
42 mmio_reset_data(tr);
43 enable_mmiotrace();
Frederic Weisbecker1c800252008-11-16 05:57:26 +010044 return 0;
Pekka Paalanenf984b512008-05-12 21:20:57 +020045}
46
47static void mmio_trace_reset(struct trace_array *tr)
48{
49 pr_debug("in %s\n", __func__);
Steven Rostedtc76f0692008-11-07 22:36:02 -050050
51 disable_mmiotrace();
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020052 mmio_reset_data(tr);
53 mmio_trace_array = NULL;
Pekka Paalanenf984b512008-05-12 21:20:57 +020054}
55
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -050056static void mmio_trace_start(struct trace_array *tr)
Pekka Paalanenf984b512008-05-12 21:20:57 +020057{
58 pr_debug("in %s\n", __func__);
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -050059 mmio_reset_data(tr);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020060}
61
Pekka Paalanen13829532008-05-12 21:20:58 +020062static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
63{
64 int ret = 0;
65 int i;
66 resource_size_t start, end;
67 const struct pci_driver *drv = pci_dev_driver(dev);
68
69 /* XXX: incomplete checks for trace_seq_printf() return value */
70 ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
71 dev->bus->number, dev->devfn,
72 dev->vendor, dev->device, dev->irq);
73 /*
74 * XXX: is pci_resource_to_user() appropriate, since we are
75 * supposed to interpret the __ioremap() phys_addr argument based on
76 * these printed values?
77 */
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 (unsigned long long)(start |
82 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
83 }
84 for (i = 0; i < 7; i++) {
85 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
86 ret += trace_seq_printf(s, " %llx",
87 dev->resource[i].start < dev->resource[i].end ?
88 (unsigned long long)(end - start) + 1 : 0);
89 }
90 if (drv)
91 ret += trace_seq_printf(s, " %s\n", drv->name);
92 else
93 ret += trace_seq_printf(s, " \n");
94 return ret;
95}
96
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +020097static void destroy_header_iter(struct header_iter *hiter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020098{
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +020099 if (!hiter)
100 return;
101 pci_dev_put(hiter->dev);
102 kfree(hiter);
103}
104
105static void mmio_pipe_open(struct trace_iterator *iter)
106{
107 struct header_iter *hiter;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200108 struct trace_seq *s = &iter->seq;
Pekka Paalanen13829532008-05-12 21:20:58 +0200109
110 trace_seq_printf(s, "VERSION 20070824\n");
111
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +0200112 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
113 if (!hiter)
114 return;
115
116 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
117 iter->private = hiter;
118}
119
120/* XXX: This is not called when the pipe is closed! */
121static void mmio_close(struct trace_iterator *iter)
122{
123 struct header_iter *hiter = iter->private;
124 destroy_header_iter(hiter);
125 iter->private = NULL;
126}
127
Pekka Paalanen20392382008-05-12 21:21:02 +0200128static unsigned long count_overruns(struct trace_iterator *iter)
129{
Pekka Paalanen173ed242009-01-06 13:57:11 +0200130 unsigned long cnt = atomic_xchg(&dropped_count, 0);
Pekka Paalanen7ee17682008-11-23 21:24:30 +0200131 unsigned long over = ring_buffer_overruns(iter->tr->buffer);
132
133 if (over > prev_overruns)
Pekka Paalanen173ed242009-01-06 13:57:11 +0200134 cnt += over - prev_overruns;
Pekka Paalanen7ee17682008-11-23 21:24:30 +0200135 prev_overruns = over;
Pekka Paalanen20392382008-05-12 21:21:02 +0200136 return cnt;
137}
138
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +0200139static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
140 char __user *ubuf, size_t cnt, loff_t *ppos)
141{
142 ssize_t ret;
143 struct header_iter *hiter = iter->private;
144 struct trace_seq *s = &iter->seq;
Pekka Paalanen20392382008-05-12 21:21:02 +0200145 unsigned long n;
146
147 n = count_overruns(iter);
148 if (n) {
149 /* XXX: This is later than where events were lost. */
150 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
151 if (!overrun_detected)
152 pr_warning("mmiotrace has lost events.\n");
153 overrun_detected = true;
154 goto print_out;
155 }
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +0200156
157 if (!hiter)
158 return 0;
159
160 mmio_print_pcidev(s, hiter->dev);
161 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
162
163 if (!hiter->dev) {
164 destroy_header_iter(hiter);
165 iter->private = NULL;
166 }
167
Pekka Paalanen20392382008-05-12 21:21:02 +0200168print_out:
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +0200169 ret = trace_seq_to_user(s, ubuf, cnt);
170 return (ret == -EBUSY) ? 0 : ret;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200171}
172
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200173static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200174{
175 struct trace_entry *entry = iter->ent;
Steven Rostedt7104f302008-10-01 10:52:51 -0400176 struct trace_mmiotrace_rw *field;
177 struct mmiotrace_rw *rw;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200178 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400179 unsigned long long t = ns2usecs(iter->ts);
Li Zefana5dec552009-03-27 14:55:44 +0800180 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200181 unsigned secs = (unsigned long)t;
182 int ret = 1;
183
Steven Rostedt7104f302008-10-01 10:52:51 -0400184 trace_assign_type(field, entry);
185 rw = &field->rw;
186
Steven Rostedt777e2082008-09-29 23:02:42 -0400187 switch (rw->opcode) {
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200188 case MMIO_READ:
189 ret = trace_seq_printf(s,
Steven Rostedt5e4abc92009-01-15 21:00:50 -0500190 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
Pekka Paalanendee310d2008-05-12 21:21:03 +0200191 rw->width, secs, usec_rem, rw->map_id,
192 (unsigned long long)rw->phys,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200193 rw->value, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200194 break;
195 case MMIO_WRITE:
196 ret = trace_seq_printf(s,
Steven Rostedt5e4abc92009-01-15 21:00:50 -0500197 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
Pekka Paalanendee310d2008-05-12 21:21:03 +0200198 rw->width, secs, usec_rem, rw->map_id,
199 (unsigned long long)rw->phys,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200200 rw->value, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200201 break;
202 case MMIO_UNKNOWN_OP:
203 ret = trace_seq_printf(s,
Steven Rostedt5e4abc92009-01-15 21:00:50 -0500204 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
205 "%02lx 0x%lx %d\n",
Pekka Paalanendee310d2008-05-12 21:21:03 +0200206 secs, usec_rem, rw->map_id,
207 (unsigned long long)rw->phys,
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200208 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
Pekka Paalanen736ca612008-05-12 21:20:59 +0200209 (rw->value >> 0) & 0xff, rw->pc, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200210 break;
211 default:
212 ret = trace_seq_printf(s, "rw what?\n");
213 break;
214 }
215 if (ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200216 return TRACE_TYPE_HANDLED;
217 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200218}
219
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200220static enum print_line_t mmio_print_map(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200221{
222 struct trace_entry *entry = iter->ent;
Steven Rostedt7104f302008-10-01 10:52:51 -0400223 struct trace_mmiotrace_map *field;
224 struct mmiotrace_map *m;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200225 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400226 unsigned long long t = ns2usecs(iter->ts);
Li Zefana5dec552009-03-27 14:55:44 +0800227 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200228 unsigned secs = (unsigned long)t;
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200229 int ret;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200230
Steven Rostedt7104f302008-10-01 10:52:51 -0400231 trace_assign_type(field, entry);
232 m = &field->map;
233
Steven Rostedt777e2082008-09-29 23:02:42 -0400234 switch (m->opcode) {
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200235 case MMIO_PROBE:
236 ret = trace_seq_printf(s,
Steven Rostedt5e4abc92009-01-15 21:00:50 -0500237 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
Pekka Paalanendee310d2008-05-12 21:21:03 +0200238 secs, usec_rem, m->map_id,
239 (unsigned long long)m->phys, m->virt, m->len,
Pekka Paalanene0fd5c22008-05-12 21:21:02 +0200240 0UL, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200241 break;
242 case MMIO_UNPROBE:
243 ret = trace_seq_printf(s,
Steven Rostedt5e4abc92009-01-15 21:00:50 -0500244 "UNMAP %u.%06lu %d 0x%lx %d\n",
Pekka Paalanene0fd5c22008-05-12 21:21:02 +0200245 secs, usec_rem, m->map_id, 0UL, 0);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200246 break;
247 default:
248 ret = trace_seq_printf(s, "map what?\n");
249 break;
250 }
251 if (ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200252 return TRACE_TYPE_HANDLED;
253 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200254}
255
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200256static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300257{
258 struct trace_entry *entry = iter->ent;
Steven Rostedt777e2082008-09-29 23:02:42 -0400259 struct print_entry *print = (struct print_entry *)entry;
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100260 const char *msg = print->buf;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300261 struct trace_seq *s = &iter->seq;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400262 unsigned long long t = ns2usecs(iter->ts);
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100263 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300264 unsigned secs = (unsigned long)t;
265 int ret;
266
267 /* The trailing newline must be in the message. */
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100268 ret = trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300269 if (!ret)
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200270 return TRACE_TYPE_PARTIAL_LINE;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300271
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200272 return TRACE_TYPE_HANDLED;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300273}
274
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200275static enum print_line_t mmio_print_line(struct trace_iterator *iter)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200276{
277 switch (iter->ent->type) {
278 case TRACE_MMIO_RW:
279 return mmio_print_rw(iter);
280 case TRACE_MMIO_MAP:
281 return mmio_print_map(iter);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +0300282 case TRACE_PRINT:
283 return mmio_print_mark(iter);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200284 default:
Frederic Weisbecker07f4e4f2008-09-29 20:27:42 +0200285 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200286 }
Pekka Paalanenf984b512008-05-12 21:20:57 +0200287}
288
289static struct tracer mmio_tracer __read_mostly =
290{
291 .name = "mmiotrace",
292 .init = mmio_trace_init,
293 .reset = mmio_trace_reset,
Steven Rostedtbbf5b1a2008-11-07 22:36:02 -0500294 .start = mmio_trace_start,
Pekka Paalanend0a7e8c2008-05-12 21:21:02 +0200295 .pipe_open = mmio_pipe_open,
296 .close = mmio_close,
297 .read = mmio_read,
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200298 .print_line = mmio_print_line,
Pekka Paalanenf984b512008-05-12 21:20:57 +0200299};
300
301__init static int init_mmio_trace(void)
302{
Pekka Paalanenf984b512008-05-12 21:20:57 +0200303 return register_tracer(&mmio_tracer);
304}
305device_initcall(init_mmio_trace);
306
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300307static void __trace_mmiotrace_rw(struct trace_array *tr,
308 struct trace_array_cpu *data,
309 struct mmiotrace_rw *rw)
310{
Steven Rostedt60ba7702009-09-12 23:34:04 -0400311 struct ftrace_event_call *call = &event_mmiotrace_rw;
Steven Rostedte77405a2009-09-02 14:17:06 -0400312 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400313 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400314 struct trace_mmiotrace_rw *entry;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200315 int pc = preempt_count();
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300316
Steven Rostedte77405a2009-09-02 14:17:06 -0400317 event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_RW,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200318 sizeof(*entry), 0, pc);
Pekka Paalanen173ed242009-01-06 13:57:11 +0200319 if (!event) {
320 atomic_inc(&dropped_count);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400321 return;
Pekka Paalanen173ed242009-01-06 13:57:11 +0200322 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400323 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400324 entry->rw = *rw;
Steven Rostedt60ba7702009-09-12 23:34:04 -0400325
326 if (!filter_check_discard(call, entry, buffer, event))
327 trace_buffer_unlock_commit(buffer, event, 0, pc);
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300328}
329
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200330void mmio_trace_rw(struct mmiotrace_rw *rw)
Pekka Paalanenf984b512008-05-12 21:20:57 +0200331{
332 struct trace_array *tr = mmio_trace_array;
333 struct trace_array_cpu *data = tr->data[smp_processor_id()];
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200334 __trace_mmiotrace_rw(tr, data, rw);
335}
Pekka Paalanenf984b512008-05-12 21:20:57 +0200336
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300337static void __trace_mmiotrace_map(struct trace_array *tr,
338 struct trace_array_cpu *data,
339 struct mmiotrace_map *map)
340{
Steven Rostedt60ba7702009-09-12 23:34:04 -0400341 struct ftrace_event_call *call = &event_mmiotrace_map;
Steven Rostedte77405a2009-09-02 14:17:06 -0400342 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400343 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400344 struct trace_mmiotrace_map *entry;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200345 int pc = preempt_count();
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300346
Steven Rostedte77405a2009-09-02 14:17:06 -0400347 event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_MAP,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200348 sizeof(*entry), 0, pc);
Pekka Paalanen173ed242009-01-06 13:57:11 +0200349 if (!event) {
350 atomic_inc(&dropped_count);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400351 return;
Pekka Paalanen173ed242009-01-06 13:57:11 +0200352 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400353 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400354 entry->map = *map;
Steven Rostedt60ba7702009-09-12 23:34:04 -0400355
356 if (!filter_check_discard(call, entry, buffer, event))
357 trace_buffer_unlock_commit(buffer, event, 0, pc);
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300358}
359
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200360void mmio_trace_mapping(struct mmiotrace_map *map)
361{
362 struct trace_array *tr = mmio_trace_array;
363 struct trace_array_cpu *data;
364
365 preempt_disable();
366 data = tr->data[smp_processor_id()];
367 __trace_mmiotrace_map(tr, data, map);
368 preempt_enable();
Pekka Paalanenf984b512008-05-12 21:20:57 +0200369}
Pekka Paalanen9e57fb32008-09-16 22:00:34 +0300370
371int mmio_trace_printk(const char *fmt, va_list args)
372{
Steven Rostedt40ce74f2009-03-19 14:03:53 -0400373 return trace_vprintk(0, fmt, args);
Pekka Paalanen9e57fb32008-09-16 22:00:34 +0300374}