]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - kernel/trace/trace_mmiotrace.c
ftrace: make work with new ring buffer
[mirror_ubuntu-kernels.git] / kernel / trace / trace_mmiotrace.c
CommitLineData
f984b51e
PP
1/*
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>
13829537 11#include <linux/pci.h>
f984b51e
PP
12
13#include "trace.h"
14
d0a7e8ca
PP
15struct header_iter {
16 struct pci_dev *dev;
17};
18
f984b51e 19static struct trace_array *mmio_trace_array;
2039238b 20static bool overrun_detected;
f984b51e 21
bd8ac686
PP
22static void mmio_reset_data(struct trace_array *tr)
23{
24 int cpu;
25
2039238b 26 overrun_detected = false;
bd8ac686
PP
27 tr->time_start = ftrace_now(tr->cpu);
28
29 for_each_online_cpu(cpu)
3928a8a2 30 tracing_reset(tr, cpu);
bd8ac686 31}
f984b51e
PP
32
33static void mmio_trace_init(struct trace_array *tr)
34{
35 pr_debug("in %s\n", __func__);
36 mmio_trace_array = tr;
bd8ac686
PP
37 if (tr->ctrl) {
38 mmio_reset_data(tr);
f984b51e 39 enable_mmiotrace();
bd8ac686 40 }
f984b51e
PP
41}
42
43static void mmio_trace_reset(struct trace_array *tr)
44{
45 pr_debug("in %s\n", __func__);
46 if (tr->ctrl)
47 disable_mmiotrace();
bd8ac686
PP
48 mmio_reset_data(tr);
49 mmio_trace_array = NULL;
f984b51e
PP
50}
51
52static void mmio_trace_ctrl_update(struct trace_array *tr)
53{
54 pr_debug("in %s\n", __func__);
bd8ac686
PP
55 if (tr->ctrl) {
56 mmio_reset_data(tr);
f984b51e 57 enable_mmiotrace();
bd8ac686 58 } else {
f984b51e 59 disable_mmiotrace();
bd8ac686
PP
60 }
61}
62
13829537
PP
63static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
64{
65 int ret = 0;
66 int i;
67 resource_size_t start, end;
68 const struct pci_driver *drv = pci_dev_driver(dev);
69
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);
74 /*
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?
78 */
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)));
84 }
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);
90 }
91 if (drv)
92 ret += trace_seq_printf(s, " %s\n", drv->name);
93 else
94 ret += trace_seq_printf(s, " \n");
95 return ret;
96}
97
d0a7e8ca
PP
98static void destroy_header_iter(struct header_iter *hiter)
99{
100 if (!hiter)
101 return;
102 pci_dev_put(hiter->dev);
103 kfree(hiter);
104}
105
106static void mmio_pipe_open(struct trace_iterator *iter)
bd8ac686 107{
d0a7e8ca 108 struct header_iter *hiter;
bd8ac686 109 struct trace_seq *s = &iter->seq;
13829537
PP
110
111 trace_seq_printf(s, "VERSION 20070824\n");
112
d0a7e8ca
PP
113 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
114 if (!hiter)
115 return;
116
117 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
118 iter->private = hiter;
119}
120
121/* XXX: This is not called when the pipe is closed! */
122static void mmio_close(struct trace_iterator *iter)
123{
124 struct header_iter *hiter = iter->private;
125 destroy_header_iter(hiter);
126 iter->private = NULL;
127}
128
2039238b
PP
129static unsigned long count_overruns(struct trace_iterator *iter)
130{
131 int cpu;
132 unsigned long cnt = 0;
3928a8a2
SR
133/* FIXME: */
134#if 0
2039238b
PP
135 for_each_online_cpu(cpu) {
136 cnt += iter->overrun[cpu];
137 iter->overrun[cpu] = 0;
138 }
3928a8a2
SR
139#endif
140 (void)cpu;
2039238b
PP
141 return cnt;
142}
143
d0a7e8ca
PP
144static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
145 char __user *ubuf, size_t cnt, loff_t *ppos)
146{
147 ssize_t ret;
148 struct header_iter *hiter = iter->private;
149 struct trace_seq *s = &iter->seq;
2039238b
PP
150 unsigned long n;
151
152 n = count_overruns(iter);
153 if (n) {
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;
159 goto print_out;
160 }
d0a7e8ca
PP
161
162 if (!hiter)
163 return 0;
164
165 mmio_print_pcidev(s, hiter->dev);
166 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
167
168 if (!hiter->dev) {
169 destroy_header_iter(hiter);
170 iter->private = NULL;
171 }
172
2039238b 173print_out:
d0a7e8ca
PP
174 ret = trace_seq_to_user(s, ubuf, cnt);
175 return (ret == -EBUSY) ? 0 : ret;
bd8ac686
PP
176}
177
178static int mmio_print_rw(struct trace_iterator *iter)
179{
180 struct trace_entry *entry = iter->ent;
2e2ca155 181 struct mmiotrace_rw *rw = &entry->field.mmiorw;
bd8ac686 182 struct trace_seq *s = &iter->seq;
3928a8a2 183 unsigned long long t = ns2usecs(iter->ts);
bd8ac686
PP
184 unsigned long usec_rem = do_div(t, 1000000ULL);
185 unsigned secs = (unsigned long)t;
186 int ret = 1;
187
2e2ca155 188 switch (entry->field.mmiorw.opcode) {
bd8ac686
PP
189 case MMIO_READ:
190 ret = trace_seq_printf(s,
dee310d0
PP
191 "R %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,
736ca61f 194 rw->value, rw->pc, 0);
bd8ac686
PP
195 break;
196 case MMIO_WRITE:
197 ret = trace_seq_printf(s,
dee310d0
PP
198 "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
199 rw->width, secs, usec_rem, rw->map_id,
200 (unsigned long long)rw->phys,
736ca61f 201 rw->value, rw->pc, 0);
bd8ac686
PP
202 break;
203 case MMIO_UNKNOWN_OP:
204 ret = trace_seq_printf(s,
dee310d0
PP
205 "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
206 secs, usec_rem, rw->map_id,
207 (unsigned long long)rw->phys,
bd8ac686 208 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
736ca61f 209 (rw->value >> 0) & 0xff, rw->pc, 0);
bd8ac686
PP
210 break;
211 default:
212 ret = trace_seq_printf(s, "rw what?\n");
213 break;
214 }
215 if (ret)
216 return 1;
217 return 0;
218}
219
220static int mmio_print_map(struct trace_iterator *iter)
221{
222 struct trace_entry *entry = iter->ent;
2e2ca155 223 struct mmiotrace_map *m = &entry->field.mmiomap;
bd8ac686 224 struct trace_seq *s = &iter->seq;
3928a8a2 225 unsigned long long t = ns2usecs(iter->ts);
bd8ac686
PP
226 unsigned long usec_rem = do_div(t, 1000000ULL);
227 unsigned secs = (unsigned long)t;
228 int ret = 1;
229
2e2ca155 230 switch (entry->field.mmiorw.opcode) {
bd8ac686
PP
231 case MMIO_PROBE:
232 ret = trace_seq_printf(s,
dee310d0
PP
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,
e0fd5c2f 236 0UL, 0);
bd8ac686
PP
237 break;
238 case MMIO_UNPROBE:
239 ret = trace_seq_printf(s,
240 "UNMAP %lu.%06lu %d 0x%lx %d\n",
e0fd5c2f 241 secs, usec_rem, m->map_id, 0UL, 0);
bd8ac686
PP
242 break;
243 default:
244 ret = trace_seq_printf(s, "map what?\n");
245 break;
246 }
247 if (ret)
248 return 1;
249 return 0;
250}
251
fc5e27ae
PP
252static int mmio_print_mark(struct trace_iterator *iter)
253{
254 struct trace_entry *entry = iter->ent;
255 const char *msg = entry->field.print.buf;
256 struct trace_seq *s = &iter->seq;
3928a8a2 257 unsigned long long t = ns2usecs(iter->ts);
fc5e27ae
PP
258 unsigned long usec_rem = do_div(t, 1000000ULL);
259 unsigned secs = (unsigned long)t;
260 int ret;
261
262 /* The trailing newline must be in the message. */
263 ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
264 if (!ret)
265 return 0;
266
267 if (entry->field.flags & TRACE_FLAG_CONT)
268 trace_seq_print_cont(s, iter);
269
270 return 1;
271}
272
bd8ac686
PP
273/* return 0 to abort printing without consuming current entry in pipe mode */
274static int mmio_print_line(struct trace_iterator *iter)
275{
276 switch (iter->ent->type) {
277 case TRACE_MMIO_RW:
278 return mmio_print_rw(iter);
279 case TRACE_MMIO_MAP:
280 return mmio_print_map(iter);
fc5e27ae
PP
281 case TRACE_PRINT:
282 return mmio_print_mark(iter);
bd8ac686
PP
283 default:
284 return 1; /* ignore unknown entries */
285 }
f984b51e
PP
286}
287
288static struct tracer mmio_tracer __read_mostly =
289{
290 .name = "mmiotrace",
291 .init = mmio_trace_init,
292 .reset = mmio_trace_reset,
d0a7e8ca
PP
293 .pipe_open = mmio_pipe_open,
294 .close = mmio_close,
295 .read = mmio_read,
f984b51e 296 .ctrl_update = mmio_trace_ctrl_update,
bd8ac686 297 .print_line = mmio_print_line,
f984b51e
PP
298};
299
300__init static int init_mmio_trace(void)
301{
f984b51e
PP
302 return register_tracer(&mmio_tracer);
303}
304device_initcall(init_mmio_trace);
305
45dcd8b8
PP
306static void __trace_mmiotrace_rw(struct trace_array *tr,
307 struct trace_array_cpu *data,
308 struct mmiotrace_rw *rw)
309{
3928a8a2 310 struct ring_buffer_event *event;
45dcd8b8
PP
311 struct trace_entry *entry;
312 unsigned long irq_flags;
313
3928a8a2
SR
314 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
315 &irq_flags);
316 if (!event)
317 return;
318 entry = ring_buffer_event_data(event);
45dcd8b8
PP
319 tracing_generic_entry_update(entry, 0);
320 entry->type = TRACE_MMIO_RW;
321 entry->field.mmiorw = *rw;
3928a8a2 322 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
45dcd8b8
PP
323
324 trace_wake_up();
325}
326
bd8ac686 327void mmio_trace_rw(struct mmiotrace_rw *rw)
f984b51e
PP
328{
329 struct trace_array *tr = mmio_trace_array;
330 struct trace_array_cpu *data = tr->data[smp_processor_id()];
bd8ac686
PP
331 __trace_mmiotrace_rw(tr, data, rw);
332}
f984b51e 333
45dcd8b8
PP
334static void __trace_mmiotrace_map(struct trace_array *tr,
335 struct trace_array_cpu *data,
336 struct mmiotrace_map *map)
337{
3928a8a2 338 struct ring_buffer_event *event;
45dcd8b8
PP
339 struct trace_entry *entry;
340 unsigned long irq_flags;
341
3928a8a2
SR
342 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
343 &irq_flags);
344 if (!event)
345 return;
346 entry = ring_buffer_event_data(event);
45dcd8b8
PP
347 tracing_generic_entry_update(entry, 0);
348 entry->type = TRACE_MMIO_MAP;
349 entry->field.mmiomap = *map;
3928a8a2 350 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
45dcd8b8
PP
351
352 trace_wake_up();
353}
354
bd8ac686
PP
355void mmio_trace_mapping(struct mmiotrace_map *map)
356{
357 struct trace_array *tr = mmio_trace_array;
358 struct trace_array_cpu *data;
359
360 preempt_disable();
361 data = tr->data[smp_processor_id()];
362 __trace_mmiotrace_map(tr, data, map);
363 preempt_enable();
f984b51e 364}
9e57fb35
PP
365
366int mmio_trace_printk(const char *fmt, va_list args)
367{
368 return trace_vprintk(0, fmt, args);
369}