Merge branch 'topic/sc6000' into for-linus
[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 #include <asm/atomic.h>
13
14 #include "trace.h"
15 #include "trace_output.h"
16
17 struct header_iter {
18         struct pci_dev *dev;
19 };
20
21 static struct trace_array *mmio_trace_array;
22 static bool overrun_detected;
23 static unsigned long prev_overruns;
24 static atomic_t dropped_count;
25
26 static void mmio_reset_data(struct trace_array *tr)
27 {
28         overrun_detected = false;
29         prev_overruns = 0;
30
31         tracing_reset_online_cpus(tr);
32 }
33
34 static int mmio_trace_init(struct trace_array *tr)
35 {
36         pr_debug("in %s\n", __func__);
37         mmio_trace_array = tr;
38
39         mmio_reset_data(tr);
40         enable_mmiotrace();
41         return 0;
42 }
43
44 static void mmio_trace_reset(struct trace_array *tr)
45 {
46         pr_debug("in %s\n", __func__);
47
48         disable_mmiotrace();
49         mmio_reset_data(tr);
50         mmio_trace_array = NULL;
51 }
52
53 static void mmio_trace_start(struct trace_array *tr)
54 {
55         pr_debug("in %s\n", __func__);
56         mmio_reset_data(tr);
57 }
58
59 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
60 {
61         int ret = 0;
62         int i;
63         resource_size_t start, end;
64         const struct pci_driver *drv = pci_dev_driver(dev);
65
66         /* XXX: incomplete checks for trace_seq_printf() return value */
67         ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
68                                 dev->bus->number, dev->devfn,
69                                 dev->vendor, dev->device, dev->irq);
70         /*
71          * XXX: is pci_resource_to_user() appropriate, since we are
72          * supposed to interpret the __ioremap() phys_addr argument based on
73          * these printed values?
74          */
75         for (i = 0; i < 7; i++) {
76                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
77                 ret += trace_seq_printf(s, " %llx",
78                         (unsigned long long)(start |
79                         (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
80         }
81         for (i = 0; i < 7; i++) {
82                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
83                 ret += trace_seq_printf(s, " %llx",
84                         dev->resource[i].start < dev->resource[i].end ?
85                         (unsigned long long)(end - start) + 1 : 0);
86         }
87         if (drv)
88                 ret += trace_seq_printf(s, " %s\n", drv->name);
89         else
90                 ret += trace_seq_printf(s, " \n");
91         return ret;
92 }
93
94 static void destroy_header_iter(struct header_iter *hiter)
95 {
96         if (!hiter)
97                 return;
98         pci_dev_put(hiter->dev);
99         kfree(hiter);
100 }
101
102 static void mmio_pipe_open(struct trace_iterator *iter)
103 {
104         struct header_iter *hiter;
105         struct trace_seq *s = &iter->seq;
106
107         trace_seq_printf(s, "VERSION 20070824\n");
108
109         hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
110         if (!hiter)
111                 return;
112
113         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
114         iter->private = hiter;
115 }
116
117 /* XXX: This is not called when the pipe is closed! */
118 static void mmio_close(struct trace_iterator *iter)
119 {
120         struct header_iter *hiter = iter->private;
121         destroy_header_iter(hiter);
122         iter->private = NULL;
123 }
124
125 static unsigned long count_overruns(struct trace_iterator *iter)
126 {
127         unsigned long cnt = atomic_xchg(&dropped_count, 0);
128         unsigned long over = ring_buffer_overruns(iter->tr->buffer);
129
130         if (over > prev_overruns)
131                 cnt += over - prev_overruns;
132         prev_overruns = over;
133         return cnt;
134 }
135
136 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
137                                 char __user *ubuf, size_t cnt, loff_t *ppos)
138 {
139         ssize_t ret;
140         struct header_iter *hiter = iter->private;
141         struct trace_seq *s = &iter->seq;
142         unsigned long n;
143
144         n = count_overruns(iter);
145         if (n) {
146                 /* XXX: This is later than where events were lost. */
147                 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
148                 if (!overrun_detected)
149                         pr_warning("mmiotrace has lost events.\n");
150                 overrun_detected = true;
151                 goto print_out;
152         }
153
154         if (!hiter)
155                 return 0;
156
157         mmio_print_pcidev(s, hiter->dev);
158         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
159
160         if (!hiter->dev) {
161                 destroy_header_iter(hiter);
162                 iter->private = NULL;
163         }
164
165 print_out:
166         ret = trace_seq_to_user(s, ubuf, cnt);
167         return (ret == -EBUSY) ? 0 : ret;
168 }
169
170 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
171 {
172         struct trace_entry *entry = iter->ent;
173         struct trace_mmiotrace_rw *field;
174         struct mmiotrace_rw *rw;
175         struct trace_seq *s     = &iter->seq;
176         unsigned long long t    = ns2usecs(iter->ts);
177         unsigned long usec_rem  = do_div(t, 1000000ULL);
178         unsigned secs           = (unsigned long)t;
179         int ret = 1;
180
181         trace_assign_type(field, entry);
182         rw = &field->rw;
183
184         switch (rw->opcode) {
185         case MMIO_READ:
186                 ret = trace_seq_printf(s,
187                         "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
188                         rw->width, secs, usec_rem, rw->map_id,
189                         (unsigned long long)rw->phys,
190                         rw->value, rw->pc, 0);
191                 break;
192         case MMIO_WRITE:
193                 ret = trace_seq_printf(s,
194                         "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
195                         rw->width, secs, usec_rem, rw->map_id,
196                         (unsigned long long)rw->phys,
197                         rw->value, rw->pc, 0);
198                 break;
199         case MMIO_UNKNOWN_OP:
200                 ret = trace_seq_printf(s,
201                         "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
202                         "%02lx 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 %u.%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 %u.%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, USEC_PER_SEC);
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 %u.%06lu %s", secs, usec_rem, msg);
266         if (!ret)
267                 return TRACE_TYPE_PARTIAL_LINE;
268
269         return TRACE_TYPE_HANDLED;
270 }
271
272 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
273 {
274         switch (iter->ent->type) {
275         case TRACE_MMIO_RW:
276                 return mmio_print_rw(iter);
277         case TRACE_MMIO_MAP:
278                 return mmio_print_map(iter);
279         case TRACE_PRINT:
280                 return mmio_print_mark(iter);
281         default:
282                 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
283         }
284 }
285
286 static struct tracer mmio_tracer __read_mostly =
287 {
288         .name           = "mmiotrace",
289         .init           = mmio_trace_init,
290         .reset          = mmio_trace_reset,
291         .start          = mmio_trace_start,
292         .pipe_open      = mmio_pipe_open,
293         .close          = mmio_close,
294         .read           = mmio_read,
295         .print_line     = mmio_print_line,
296 };
297
298 __init static int init_mmio_trace(void)
299 {
300         return register_tracer(&mmio_tracer);
301 }
302 device_initcall(init_mmio_trace);
303
304 static void __trace_mmiotrace_rw(struct trace_array *tr,
305                                 struct trace_array_cpu *data,
306                                 struct mmiotrace_rw *rw)
307 {
308         struct ring_buffer_event *event;
309         struct trace_mmiotrace_rw *entry;
310         int pc = preempt_count();
311
312         event = trace_buffer_lock_reserve(tr, TRACE_MMIO_RW,
313                                           sizeof(*entry), 0, pc);
314         if (!event) {
315                 atomic_inc(&dropped_count);
316                 return;
317         }
318         entry   = ring_buffer_event_data(event);
319         entry->rw                       = *rw;
320         trace_buffer_unlock_commit(tr, event, 0, pc);
321 }
322
323 void mmio_trace_rw(struct mmiotrace_rw *rw)
324 {
325         struct trace_array *tr = mmio_trace_array;
326         struct trace_array_cpu *data = tr->data[smp_processor_id()];
327         __trace_mmiotrace_rw(tr, data, rw);
328 }
329
330 static void __trace_mmiotrace_map(struct trace_array *tr,
331                                 struct trace_array_cpu *data,
332                                 struct mmiotrace_map *map)
333 {
334         struct ring_buffer_event *event;
335         struct trace_mmiotrace_map *entry;
336         int pc = preempt_count();
337
338         event = trace_buffer_lock_reserve(tr, TRACE_MMIO_MAP,
339                                           sizeof(*entry), 0, pc);
340         if (!event) {
341                 atomic_inc(&dropped_count);
342                 return;
343         }
344         entry   = ring_buffer_event_data(event);
345         entry->map                      = *map;
346         trace_buffer_unlock_commit(tr, event, 0, pc);
347 }
348
349 void mmio_trace_mapping(struct mmiotrace_map *map)
350 {
351         struct trace_array *tr = mmio_trace_array;
352         struct trace_array_cpu *data;
353
354         preempt_disable();
355         data = tr->data[smp_processor_id()];
356         __trace_mmiotrace_map(tr, data, map);
357         preempt_enable();
358 }
359
360 int mmio_trace_printk(const char *fmt, va_list args)
361 {
362         return trace_vprintk(0, fmt, args);
363 }