Merge branches 'tracing/branch-tracer', 'tracing/fastboot', 'tracing/ftrace', 'tracin...
[linux-2.6] / kernel / trace / trace_mmiotrace.c
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>
11 #include <linux/pci.h>
12
13 #include "trace.h"
14
15 struct header_iter {
16         struct pci_dev *dev;
17 };
18
19 static struct trace_array *mmio_trace_array;
20 static bool overrun_detected;
21 static unsigned long prev_overruns;
22
23 static void mmio_reset_data(struct trace_array *tr)
24 {
25         int cpu;
26
27         overrun_detected = false;
28         prev_overruns = 0;
29         tr->time_start = ftrace_now(tr->cpu);
30
31         for_each_online_cpu(cpu)
32                 tracing_reset(tr, cpu);
33 }
34
35 static int mmio_trace_init(struct trace_array *tr)
36 {
37         pr_debug("in %s\n", __func__);
38         mmio_trace_array = tr;
39
40         mmio_reset_data(tr);
41         enable_mmiotrace();
42         return 0;
43 }
44
45 static void mmio_trace_reset(struct trace_array *tr)
46 {
47         pr_debug("in %s\n", __func__);
48
49         disable_mmiotrace();
50         mmio_reset_data(tr);
51         mmio_trace_array = NULL;
52 }
53
54 static void mmio_trace_start(struct trace_array *tr)
55 {
56         pr_debug("in %s\n", __func__);
57         mmio_reset_data(tr);
58 }
59
60 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
61 {
62         int ret = 0;
63         int i;
64         resource_size_t start, end;
65         const struct pci_driver *drv = pci_dev_driver(dev);
66
67         /* XXX: incomplete checks for trace_seq_printf() return value */
68         ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
69                                 dev->bus->number, dev->devfn,
70                                 dev->vendor, dev->device, dev->irq);
71         /*
72          * XXX: is pci_resource_to_user() appropriate, since we are
73          * supposed to interpret the __ioremap() phys_addr argument based on
74          * these printed values?
75          */
76         for (i = 0; i < 7; i++) {
77                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
78                 ret += trace_seq_printf(s, " %llx",
79                         (unsigned long long)(start |
80                         (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
81         }
82         for (i = 0; i < 7; i++) {
83                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
84                 ret += trace_seq_printf(s, " %llx",
85                         dev->resource[i].start < dev->resource[i].end ?
86                         (unsigned long long)(end - start) + 1 : 0);
87         }
88         if (drv)
89                 ret += trace_seq_printf(s, " %s\n", drv->name);
90         else
91                 ret += trace_seq_printf(s, " \n");
92         return ret;
93 }
94
95 static void destroy_header_iter(struct header_iter *hiter)
96 {
97         if (!hiter)
98                 return;
99         pci_dev_put(hiter->dev);
100         kfree(hiter);
101 }
102
103 static void mmio_pipe_open(struct trace_iterator *iter)
104 {
105         struct header_iter *hiter;
106         struct trace_seq *s = &iter->seq;
107
108         trace_seq_printf(s, "VERSION 20070824\n");
109
110         hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
111         if (!hiter)
112                 return;
113
114         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
115         iter->private = hiter;
116 }
117
118 /* XXX: This is not called when the pipe is closed! */
119 static void mmio_close(struct trace_iterator *iter)
120 {
121         struct header_iter *hiter = iter->private;
122         destroy_header_iter(hiter);
123         iter->private = NULL;
124 }
125
126 static unsigned long count_overruns(struct trace_iterator *iter)
127 {
128         unsigned long cnt = 0;
129         unsigned long over = ring_buffer_overruns(iter->tr->buffer);
130
131         if (over > prev_overruns)
132                 cnt = over - prev_overruns;
133         prev_overruns = over;
134         return cnt;
135 }
136
137 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
138                                 char __user *ubuf, size_t cnt, loff_t *ppos)
139 {
140         ssize_t ret;
141         struct header_iter *hiter = iter->private;
142         struct trace_seq *s = &iter->seq;
143         unsigned long n;
144
145         n = count_overruns(iter);
146         if (n) {
147                 /* XXX: This is later than where events were lost. */
148                 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
149                 if (!overrun_detected)
150                         pr_warning("mmiotrace has lost events.\n");
151                 overrun_detected = true;
152                 goto print_out;
153         }
154
155         if (!hiter)
156                 return 0;
157
158         mmio_print_pcidev(s, hiter->dev);
159         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
160
161         if (!hiter->dev) {
162                 destroy_header_iter(hiter);
163                 iter->private = NULL;
164         }
165
166 print_out:
167         ret = trace_seq_to_user(s, ubuf, cnt);
168         return (ret == -EBUSY) ? 0 : ret;
169 }
170
171 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
172 {
173         struct trace_entry *entry = iter->ent;
174         struct trace_mmiotrace_rw *field;
175         struct mmiotrace_rw *rw;
176         struct trace_seq *s     = &iter->seq;
177         unsigned long long t    = ns2usecs(iter->ts);
178         unsigned long usec_rem  = do_div(t, 1000000ULL);
179         unsigned secs           = (unsigned long)t;
180         int ret = 1;
181
182         trace_assign_type(field, entry);
183         rw = &field->rw;
184
185         switch (rw->opcode) {
186         case MMIO_READ:
187                 ret = trace_seq_printf(s,
188                         "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
189                         rw->width, secs, usec_rem, rw->map_id,
190                         (unsigned long long)rw->phys,
191                         rw->value, rw->pc, 0);
192                 break;
193         case MMIO_WRITE:
194                 ret = trace_seq_printf(s,
195                         "W %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);
199                 break;
200         case MMIO_UNKNOWN_OP:
201                 ret = trace_seq_printf(s,
202                         "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
203                         secs, usec_rem, rw->map_id,
204                         (unsigned long long)rw->phys,
205                         (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
206                         (rw->value >> 0) & 0xff, rw->pc, 0);
207                 break;
208         default:
209                 ret = trace_seq_printf(s, "rw what?\n");
210                 break;
211         }
212         if (ret)
213                 return TRACE_TYPE_HANDLED;
214         return TRACE_TYPE_PARTIAL_LINE;
215 }
216
217 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
218 {
219         struct trace_entry *entry = iter->ent;
220         struct trace_mmiotrace_map *field;
221         struct mmiotrace_map *m;
222         struct trace_seq *s     = &iter->seq;
223         unsigned long long t    = ns2usecs(iter->ts);
224         unsigned long usec_rem  = do_div(t, 1000000ULL);
225         unsigned secs           = (unsigned long)t;
226         int ret;
227
228         trace_assign_type(field, entry);
229         m = &field->map;
230
231         switch (m->opcode) {
232         case MMIO_PROBE:
233                 ret = trace_seq_printf(s,
234                         "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
235                         secs, usec_rem, m->map_id,
236                         (unsigned long long)m->phys, m->virt, m->len,
237                         0UL, 0);
238                 break;
239         case MMIO_UNPROBE:
240                 ret = trace_seq_printf(s,
241                         "UNMAP %lu.%06lu %d 0x%lx %d\n",
242                         secs, usec_rem, m->map_id, 0UL, 0);
243                 break;
244         default:
245                 ret = trace_seq_printf(s, "map what?\n");
246                 break;
247         }
248         if (ret)
249                 return TRACE_TYPE_HANDLED;
250         return TRACE_TYPE_PARTIAL_LINE;
251 }
252
253 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
254 {
255         struct trace_entry *entry = iter->ent;
256         struct print_entry *print = (struct print_entry *)entry;
257         const char *msg         = print->buf;
258         struct trace_seq *s     = &iter->seq;
259         unsigned long long t    = ns2usecs(iter->ts);
260         unsigned long usec_rem  = do_div(t, 1000000ULL);
261         unsigned secs           = (unsigned long)t;
262         int ret;
263
264         /* The trailing newline must be in the message. */
265         ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
266         if (!ret)
267                 return TRACE_TYPE_PARTIAL_LINE;
268
269         if (entry->flags & TRACE_FLAG_CONT)
270                 trace_seq_print_cont(s, iter);
271
272         return TRACE_TYPE_HANDLED;
273 }
274
275 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
276 {
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);
282         case TRACE_PRINT:
283                 return mmio_print_mark(iter);
284         default:
285                 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
286         }
287 }
288
289 static struct tracer mmio_tracer __read_mostly =
290 {
291         .name           = "mmiotrace",
292         .init           = mmio_trace_init,
293         .reset          = mmio_trace_reset,
294         .start          = mmio_trace_start,
295         .pipe_open      = mmio_pipe_open,
296         .close          = mmio_close,
297         .read           = mmio_read,
298         .print_line     = mmio_print_line,
299 };
300
301 __init static int init_mmio_trace(void)
302 {
303         return register_tracer(&mmio_tracer);
304 }
305 device_initcall(init_mmio_trace);
306
307 static void __trace_mmiotrace_rw(struct trace_array *tr,
308                                 struct trace_array_cpu *data,
309                                 struct mmiotrace_rw *rw)
310 {
311         struct ring_buffer_event *event;
312         struct trace_mmiotrace_rw *entry;
313         unsigned long irq_flags;
314
315         event   = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
316                                            &irq_flags);
317         if (!event)
318                 return;
319         entry   = ring_buffer_event_data(event);
320         tracing_generic_entry_update(&entry->ent, 0, preempt_count());
321         entry->ent.type                 = TRACE_MMIO_RW;
322         entry->rw                       = *rw;
323         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
324
325         trace_wake_up();
326 }
327
328 void mmio_trace_rw(struct mmiotrace_rw *rw)
329 {
330         struct trace_array *tr = mmio_trace_array;
331         struct trace_array_cpu *data = tr->data[smp_processor_id()];
332         __trace_mmiotrace_rw(tr, data, rw);
333 }
334
335 static void __trace_mmiotrace_map(struct trace_array *tr,
336                                 struct trace_array_cpu *data,
337                                 struct mmiotrace_map *map)
338 {
339         struct ring_buffer_event *event;
340         struct trace_mmiotrace_map *entry;
341         unsigned long irq_flags;
342
343         event   = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
344                                            &irq_flags);
345         if (!event)
346                 return;
347         entry   = ring_buffer_event_data(event);
348         tracing_generic_entry_update(&entry->ent, 0, preempt_count());
349         entry->ent.type                 = TRACE_MMIO_MAP;
350         entry->map                      = *map;
351         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
352
353         trace_wake_up();
354 }
355
356 void mmio_trace_mapping(struct mmiotrace_map *map)
357 {
358         struct trace_array *tr = mmio_trace_array;
359         struct trace_array_cpu *data;
360
361         preempt_disable();
362         data = tr->data[smp_processor_id()];
363         __trace_mmiotrace_map(tr, data, map);
364         preempt_enable();
365 }
366
367 int mmio_trace_printk(const char *fmt, va_list args)
368 {
369         return trace_vprintk(0, fmt, args);
370 }