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>
12 #include <linux/slab.h>
13 #include <linux/time.h>
15 #include <linux/atomic.h>
18 #include "trace_output.h"
24 static struct trace_array
*mmio_trace_array
;
25 static bool overrun_detected
;
26 static unsigned long prev_overruns
;
27 static atomic_t dropped_count
;
29 static void mmio_reset_data(struct trace_array
*tr
)
31 overrun_detected
= false;
34 tracing_reset_online_cpus(&tr
->trace_buffer
);
37 static int mmio_trace_init(struct trace_array
*tr
)
39 pr_debug("in %s\n", __func__
);
40 mmio_trace_array
= tr
;
47 static void mmio_trace_reset(struct trace_array
*tr
)
49 pr_debug("in %s\n", __func__
);
53 mmio_trace_array
= NULL
;
56 static void mmio_trace_start(struct trace_array
*tr
)
58 pr_debug("in %s\n", __func__
);
62 static void mmio_print_pcidev(struct trace_seq
*s
, const struct pci_dev
*dev
)
65 resource_size_t start
, end
;
66 const struct pci_driver
*drv
= pci_dev_driver(dev
);
68 trace_seq_printf(s
, "PCIDEV %02x%02x %04x%04x %x",
69 dev
->bus
->number
, dev
->devfn
,
70 dev
->vendor
, dev
->device
, dev
->irq
);
71 for (i
= 0; i
< 7; i
++) {
72 start
= dev
->resource
[i
].start
;
73 trace_seq_printf(s
, " %llx",
74 (unsigned long long)(start
|
75 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
77 for (i
= 0; i
< 7; i
++) {
78 start
= dev
->resource
[i
].start
;
79 end
= dev
->resource
[i
].end
;
80 trace_seq_printf(s
, " %llx",
81 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
82 (unsigned long long)(end
- start
) + 1 : 0);
85 trace_seq_printf(s
, " %s\n", drv
->name
);
87 trace_seq_puts(s
, " \n");
90 static void destroy_header_iter(struct header_iter
*hiter
)
94 pci_dev_put(hiter
->dev
);
98 static void mmio_pipe_open(struct trace_iterator
*iter
)
100 struct header_iter
*hiter
;
101 struct trace_seq
*s
= &iter
->seq
;
103 trace_seq_puts(s
, "VERSION 20070824\n");
105 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
109 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
110 iter
->private = hiter
;
113 /* XXX: This is not called when the pipe is closed! */
114 static void mmio_close(struct trace_iterator
*iter
)
116 struct header_iter
*hiter
= iter
->private;
117 destroy_header_iter(hiter
);
118 iter
->private = NULL
;
121 static unsigned long count_overruns(struct trace_iterator
*iter
)
123 unsigned long cnt
= atomic_xchg(&dropped_count
, 0);
124 unsigned long over
= ring_buffer_overruns(iter
->trace_buffer
->buffer
);
126 if (over
> prev_overruns
)
127 cnt
+= over
- prev_overruns
;
128 prev_overruns
= over
;
132 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
133 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
136 struct header_iter
*hiter
= iter
->private;
137 struct trace_seq
*s
= &iter
->seq
;
140 n
= count_overruns(iter
);
142 /* XXX: This is later than where events were lost. */
143 trace_seq_printf(s
, "MARK 0.000000 Lost %lu events.\n", n
);
144 if (!overrun_detected
)
145 pr_warn("mmiotrace has lost events\n");
146 overrun_detected
= true;
153 mmio_print_pcidev(s
, hiter
->dev
);
154 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
157 destroy_header_iter(hiter
);
158 iter
->private = NULL
;
162 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
163 return (ret
== -EBUSY
) ? 0 : ret
;
166 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
168 struct trace_entry
*entry
= iter
->ent
;
169 struct trace_mmiotrace_rw
*field
;
170 struct mmiotrace_rw
*rw
;
171 struct trace_seq
*s
= &iter
->seq
;
172 unsigned long long t
= ns2usecs(iter
->ts
);
173 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
174 unsigned secs
= (unsigned long)t
;
176 trace_assign_type(field
, entry
);
179 switch (rw
->opcode
) {
182 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
183 rw
->width
, secs
, usec_rem
, rw
->map_id
,
184 (unsigned long long)rw
->phys
,
185 rw
->value
, rw
->pc
, 0);
189 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
190 rw
->width
, secs
, usec_rem
, rw
->map_id
,
191 (unsigned long long)rw
->phys
,
192 rw
->value
, rw
->pc
, 0);
194 case MMIO_UNKNOWN_OP
:
196 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
198 secs
, usec_rem
, rw
->map_id
,
199 (unsigned long long)rw
->phys
,
200 (rw
->value
>> 16) & 0xff, (rw
->value
>> 8) & 0xff,
201 (rw
->value
>> 0) & 0xff, rw
->pc
, 0);
204 trace_seq_puts(s
, "rw what?\n");
208 return trace_handle_return(s
);
211 static enum print_line_t
mmio_print_map(struct trace_iterator
*iter
)
213 struct trace_entry
*entry
= iter
->ent
;
214 struct trace_mmiotrace_map
*field
;
215 struct mmiotrace_map
*m
;
216 struct trace_seq
*s
= &iter
->seq
;
217 unsigned long long t
= ns2usecs(iter
->ts
);
218 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
219 unsigned secs
= (unsigned long)t
;
221 trace_assign_type(field
, entry
);
227 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
228 secs
, usec_rem
, m
->map_id
,
229 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
234 "UNMAP %u.%06lu %d 0x%lx %d\n",
235 secs
, usec_rem
, m
->map_id
, 0UL, 0);
238 trace_seq_puts(s
, "map what?\n");
242 return trace_handle_return(s
);
245 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
247 struct trace_entry
*entry
= iter
->ent
;
248 struct print_entry
*print
= (struct print_entry
*)entry
;
249 const char *msg
= print
->buf
;
250 struct trace_seq
*s
= &iter
->seq
;
251 unsigned long long t
= ns2usecs(iter
->ts
);
252 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
253 unsigned secs
= (unsigned long)t
;
255 /* The trailing newline must be in the message. */
256 trace_seq_printf(s
, "MARK %u.%06lu %s", secs
, usec_rem
, msg
);
258 return trace_handle_return(s
);
261 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
263 switch (iter
->ent
->type
) {
265 return mmio_print_rw(iter
);
267 return mmio_print_map(iter
);
269 return mmio_print_mark(iter
);
271 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
275 static struct tracer mmio_tracer __read_mostly
=
278 .init
= mmio_trace_init
,
279 .reset
= mmio_trace_reset
,
280 .start
= mmio_trace_start
,
281 .pipe_open
= mmio_pipe_open
,
284 .print_line
= mmio_print_line
,
287 __init
static int init_mmio_trace(void)
289 return register_tracer(&mmio_tracer
);
291 device_initcall(init_mmio_trace
);
293 static void __trace_mmiotrace_rw(struct trace_array
*tr
,
294 struct trace_array_cpu
*data
,
295 struct mmiotrace_rw
*rw
)
297 struct trace_event_call
*call
= &event_mmiotrace_rw
;
298 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
299 struct ring_buffer_event
*event
;
300 struct trace_mmiotrace_rw
*entry
;
301 int pc
= preempt_count();
303 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_RW
,
304 sizeof(*entry
), 0, pc
);
306 atomic_inc(&dropped_count
);
309 entry
= ring_buffer_event_data(event
);
312 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
313 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
316 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
318 struct trace_array
*tr
= mmio_trace_array
;
319 struct trace_array_cpu
*data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
320 __trace_mmiotrace_rw(tr
, data
, rw
);
323 static void __trace_mmiotrace_map(struct trace_array
*tr
,
324 struct trace_array_cpu
*data
,
325 struct mmiotrace_map
*map
)
327 struct trace_event_call
*call
= &event_mmiotrace_map
;
328 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
329 struct ring_buffer_event
*event
;
330 struct trace_mmiotrace_map
*entry
;
331 int pc
= preempt_count();
333 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_MAP
,
334 sizeof(*entry
), 0, pc
);
336 atomic_inc(&dropped_count
);
339 entry
= ring_buffer_event_data(event
);
342 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
343 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
346 void mmio_trace_mapping(struct mmiotrace_map
*map
)
348 struct trace_array
*tr
= mmio_trace_array
;
349 struct trace_array_cpu
*data
;
352 data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
353 __trace_mmiotrace_map(tr
, data
, map
);
357 int mmio_trace_printk(const char *fmt
, va_list args
)
359 return trace_vprintk(0, fmt
, args
);