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
);
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?
76 for (i
= 0; i
< 7; i
++) {
77 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
78 trace_seq_printf(s
, " %llx",
79 (unsigned long long)(start
|
80 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
82 for (i
= 0; i
< 7; i
++) {
83 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
84 trace_seq_printf(s
, " %llx",
85 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
86 (unsigned long long)(end
- start
) + 1 : 0);
89 trace_seq_printf(s
, " %s\n", drv
->name
);
91 trace_seq_puts(s
, " \n");
94 static void destroy_header_iter(struct header_iter
*hiter
)
98 pci_dev_put(hiter
->dev
);
102 static void mmio_pipe_open(struct trace_iterator
*iter
)
104 struct header_iter
*hiter
;
105 struct trace_seq
*s
= &iter
->seq
;
107 trace_seq_puts(s
, "VERSION 20070824\n");
109 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
113 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
114 iter
->private = hiter
;
117 /* XXX: This is not called when the pipe is closed! */
118 static void mmio_close(struct trace_iterator
*iter
)
120 struct header_iter
*hiter
= iter
->private;
121 destroy_header_iter(hiter
);
122 iter
->private = NULL
;
125 static unsigned long count_overruns(struct trace_iterator
*iter
)
127 unsigned long cnt
= atomic_xchg(&dropped_count
, 0);
128 unsigned long over
= ring_buffer_overruns(iter
->trace_buffer
->buffer
);
130 if (over
> prev_overruns
)
131 cnt
+= over
- prev_overruns
;
132 prev_overruns
= over
;
136 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
137 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
140 struct header_iter
*hiter
= iter
->private;
141 struct trace_seq
*s
= &iter
->seq
;
144 n
= count_overruns(iter
);
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_warn("mmiotrace has lost events\n");
150 overrun_detected
= true;
157 mmio_print_pcidev(s
, hiter
->dev
);
158 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
161 destroy_header_iter(hiter
);
162 iter
->private = NULL
;
166 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
167 return (ret
== -EBUSY
) ? 0 : ret
;
170 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
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
, USEC_PER_SEC
);
178 unsigned secs
= (unsigned long)t
;
180 trace_assign_type(field
, entry
);
183 switch (rw
->opcode
) {
186 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
187 rw
->width
, secs
, usec_rem
, rw
->map_id
,
188 (unsigned long long)rw
->phys
,
189 rw
->value
, rw
->pc
, 0);
193 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
194 rw
->width
, secs
, usec_rem
, rw
->map_id
,
195 (unsigned long long)rw
->phys
,
196 rw
->value
, rw
->pc
, 0);
198 case MMIO_UNKNOWN_OP
:
200 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
202 secs
, usec_rem
, rw
->map_id
,
203 (unsigned long long)rw
->phys
,
204 (rw
->value
>> 16) & 0xff, (rw
->value
>> 8) & 0xff,
205 (rw
->value
>> 0) & 0xff, rw
->pc
, 0);
208 trace_seq_puts(s
, "rw what?\n");
212 return trace_handle_return(s
);
215 static enum print_line_t
mmio_print_map(struct trace_iterator
*iter
)
217 struct trace_entry
*entry
= iter
->ent
;
218 struct trace_mmiotrace_map
*field
;
219 struct mmiotrace_map
*m
;
220 struct trace_seq
*s
= &iter
->seq
;
221 unsigned long long t
= ns2usecs(iter
->ts
);
222 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
223 unsigned secs
= (unsigned long)t
;
225 trace_assign_type(field
, entry
);
231 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
232 secs
, usec_rem
, m
->map_id
,
233 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
238 "UNMAP %u.%06lu %d 0x%lx %d\n",
239 secs
, usec_rem
, m
->map_id
, 0UL, 0);
242 trace_seq_puts(s
, "map what?\n");
246 return trace_handle_return(s
);
249 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
251 struct trace_entry
*entry
= iter
->ent
;
252 struct print_entry
*print
= (struct print_entry
*)entry
;
253 const char *msg
= print
->buf
;
254 struct trace_seq
*s
= &iter
->seq
;
255 unsigned long long t
= ns2usecs(iter
->ts
);
256 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
257 unsigned secs
= (unsigned long)t
;
259 /* The trailing newline must be in the message. */
260 trace_seq_printf(s
, "MARK %u.%06lu %s", secs
, usec_rem
, msg
);
262 return trace_handle_return(s
);
265 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
267 switch (iter
->ent
->type
) {
269 return mmio_print_rw(iter
);
271 return mmio_print_map(iter
);
273 return mmio_print_mark(iter
);
275 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
279 static struct tracer mmio_tracer __read_mostly
=
282 .init
= mmio_trace_init
,
283 .reset
= mmio_trace_reset
,
284 .start
= mmio_trace_start
,
285 .pipe_open
= mmio_pipe_open
,
288 .print_line
= mmio_print_line
,
291 __init
static int init_mmio_trace(void)
293 return register_tracer(&mmio_tracer
);
295 device_initcall(init_mmio_trace
);
297 static void __trace_mmiotrace_rw(struct trace_array
*tr
,
298 struct trace_array_cpu
*data
,
299 struct mmiotrace_rw
*rw
)
301 struct trace_event_call
*call
= &event_mmiotrace_rw
;
302 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
303 struct ring_buffer_event
*event
;
304 struct trace_mmiotrace_rw
*entry
;
305 int pc
= preempt_count();
307 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_RW
,
308 sizeof(*entry
), 0, pc
);
310 atomic_inc(&dropped_count
);
313 entry
= ring_buffer_event_data(event
);
316 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
317 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
320 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
322 struct trace_array
*tr
= mmio_trace_array
;
323 struct trace_array_cpu
*data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
324 __trace_mmiotrace_rw(tr
, data
, rw
);
327 static void __trace_mmiotrace_map(struct trace_array
*tr
,
328 struct trace_array_cpu
*data
,
329 struct mmiotrace_map
*map
)
331 struct trace_event_call
*call
= &event_mmiotrace_map
;
332 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
333 struct ring_buffer_event
*event
;
334 struct trace_mmiotrace_map
*entry
;
335 int pc
= preempt_count();
337 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_MAP
,
338 sizeof(*entry
), 0, pc
);
340 atomic_inc(&dropped_count
);
343 entry
= ring_buffer_event_data(event
);
346 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
347 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
350 void mmio_trace_mapping(struct mmiotrace_map
*map
)
352 struct trace_array
*tr
= mmio_trace_array
;
353 struct trace_array_cpu
*data
;
356 data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
357 __trace_mmiotrace_map(tr
, data
, map
);
361 int mmio_trace_printk(const char *fmt
, va_list args
)
363 return trace_vprintk(0, fmt
, args
);