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 <asm/atomic.h>
20 static struct trace_array
*mmio_trace_array
;
21 static bool overrun_detected
;
22 static unsigned long prev_overruns
;
23 static atomic_t dropped_count
;
25 static void mmio_reset_data(struct trace_array
*tr
)
27 overrun_detected
= false;
30 tracing_reset_online_cpus(tr
);
33 static int mmio_trace_init(struct trace_array
*tr
)
35 pr_debug("in %s\n", __func__
);
36 mmio_trace_array
= tr
;
43 static void mmio_trace_reset(struct trace_array
*tr
)
45 pr_debug("in %s\n", __func__
);
49 mmio_trace_array
= NULL
;
52 static void mmio_trace_start(struct trace_array
*tr
)
54 pr_debug("in %s\n", __func__
);
58 static int mmio_print_pcidev(struct trace_seq
*s
, const struct pci_dev
*dev
)
62 resource_size_t start
, end
;
63 const struct pci_driver
*drv
= pci_dev_driver(dev
);
65 /* XXX: incomplete checks for trace_seq_printf() return value */
66 ret
+= trace_seq_printf(s
, "PCIDEV %02x%02x %04x%04x %x",
67 dev
->bus
->number
, dev
->devfn
,
68 dev
->vendor
, dev
->device
, dev
->irq
);
70 * XXX: is pci_resource_to_user() appropriate, since we are
71 * supposed to interpret the __ioremap() phys_addr argument based on
72 * these printed values?
74 for (i
= 0; i
< 7; i
++) {
75 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
76 ret
+= trace_seq_printf(s
, " %llx",
77 (unsigned long long)(start
|
78 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
80 for (i
= 0; i
< 7; i
++) {
81 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
82 ret
+= trace_seq_printf(s
, " %llx",
83 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
84 (unsigned long long)(end
- start
) + 1 : 0);
87 ret
+= trace_seq_printf(s
, " %s\n", drv
->name
);
89 ret
+= trace_seq_printf(s
, " \n");
93 static void destroy_header_iter(struct header_iter
*hiter
)
97 pci_dev_put(hiter
->dev
);
101 static void mmio_pipe_open(struct trace_iterator
*iter
)
103 struct header_iter
*hiter
;
104 struct trace_seq
*s
= &iter
->seq
;
106 trace_seq_printf(s
, "VERSION 20070824\n");
108 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
112 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
113 iter
->private = hiter
;
116 /* XXX: This is not called when the pipe is closed! */
117 static void mmio_close(struct trace_iterator
*iter
)
119 struct header_iter
*hiter
= iter
->private;
120 destroy_header_iter(hiter
);
121 iter
->private = NULL
;
124 static unsigned long count_overruns(struct trace_iterator
*iter
)
126 unsigned long cnt
= atomic_xchg(&dropped_count
, 0);
127 unsigned long over
= ring_buffer_overruns(iter
->tr
->buffer
);
129 if (over
> prev_overruns
)
130 cnt
+= over
- prev_overruns
;
131 prev_overruns
= over
;
135 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
136 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
139 struct header_iter
*hiter
= iter
->private;
140 struct trace_seq
*s
= &iter
->seq
;
143 n
= count_overruns(iter
);
145 /* XXX: This is later than where events were lost. */
146 trace_seq_printf(s
, "MARK 0.000000 Lost %lu events.\n", n
);
147 if (!overrun_detected
)
148 pr_warning("mmiotrace has lost events.\n");
149 overrun_detected
= true;
156 mmio_print_pcidev(s
, hiter
->dev
);
157 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
160 destroy_header_iter(hiter
);
161 iter
->private = NULL
;
165 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
166 return (ret
== -EBUSY
) ? 0 : ret
;
169 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
171 struct trace_entry
*entry
= iter
->ent
;
172 struct trace_mmiotrace_rw
*field
;
173 struct mmiotrace_rw
*rw
;
174 struct trace_seq
*s
= &iter
->seq
;
175 unsigned long long t
= ns2usecs(iter
->ts
);
176 unsigned long usec_rem
= do_div(t
, 1000000ULL);
177 unsigned secs
= (unsigned long)t
;
180 trace_assign_type(field
, entry
);
183 switch (rw
->opcode
) {
185 ret
= trace_seq_printf(s
,
186 "R %d %lu.%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);
192 ret
= trace_seq_printf(s
,
193 "W %d %lu.%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
:
199 ret
= trace_seq_printf(s
,
200 "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
201 secs
, usec_rem
, rw
->map_id
,
202 (unsigned long long)rw
->phys
,
203 (rw
->value
>> 16) & 0xff, (rw
->value
>> 8) & 0xff,
204 (rw
->value
>> 0) & 0xff, rw
->pc
, 0);
207 ret
= trace_seq_printf(s
, "rw what?\n");
211 return TRACE_TYPE_HANDLED
;
212 return TRACE_TYPE_PARTIAL_LINE
;
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
, 1000000ULL);
223 unsigned secs
= (unsigned long)t
;
226 trace_assign_type(field
, entry
);
231 ret
= trace_seq_printf(s
,
232 "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
233 secs
, usec_rem
, m
->map_id
,
234 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
238 ret
= trace_seq_printf(s
,
239 "UNMAP %lu.%06lu %d 0x%lx %d\n",
240 secs
, usec_rem
, m
->map_id
, 0UL, 0);
243 ret
= trace_seq_printf(s
, "map what?\n");
247 return TRACE_TYPE_HANDLED
;
248 return TRACE_TYPE_PARTIAL_LINE
;
251 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
253 struct trace_entry
*entry
= iter
->ent
;
254 struct print_entry
*print
= (struct print_entry
*)entry
;
255 const char *msg
= print
->buf
;
256 struct trace_seq
*s
= &iter
->seq
;
257 unsigned long long t
= ns2usecs(iter
->ts
);
258 unsigned long usec_rem
= do_div(t
, 1000000ULL);
259 unsigned secs
= (unsigned long)t
;
262 /* The trailing newline must be in the message. */
263 ret
= trace_seq_printf(s
, "MARK %lu.%06lu %s", secs
, usec_rem
, msg
);
265 return TRACE_TYPE_PARTIAL_LINE
;
267 if (entry
->flags
& TRACE_FLAG_CONT
)
268 trace_seq_print_cont(s
, iter
);
270 return TRACE_TYPE_HANDLED
;
273 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
275 switch (iter
->ent
->type
) {
277 return mmio_print_rw(iter
);
279 return mmio_print_map(iter
);
281 return mmio_print_mark(iter
);
283 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
287 static struct tracer mmio_tracer __read_mostly
=
290 .init
= mmio_trace_init
,
291 .reset
= mmio_trace_reset
,
292 .start
= mmio_trace_start
,
293 .pipe_open
= mmio_pipe_open
,
296 .print_line
= mmio_print_line
,
299 __init
static int init_mmio_trace(void)
301 return register_tracer(&mmio_tracer
);
303 device_initcall(init_mmio_trace
);
305 static void __trace_mmiotrace_rw(struct trace_array
*tr
,
306 struct trace_array_cpu
*data
,
307 struct mmiotrace_rw
*rw
)
309 struct ring_buffer_event
*event
;
310 struct trace_mmiotrace_rw
*entry
;
311 unsigned long irq_flags
;
313 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
316 atomic_inc(&dropped_count
);
319 entry
= ring_buffer_event_data(event
);
320 tracing_generic_entry_update(&entry
->ent
, 0, preempt_count());
321 entry
->ent
.type
= TRACE_MMIO_RW
;
323 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
328 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
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
);
335 static void __trace_mmiotrace_map(struct trace_array
*tr
,
336 struct trace_array_cpu
*data
,
337 struct mmiotrace_map
*map
)
339 struct ring_buffer_event
*event
;
340 struct trace_mmiotrace_map
*entry
;
341 unsigned long irq_flags
;
343 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
346 atomic_inc(&dropped_count
);
349 entry
= ring_buffer_event_data(event
);
350 tracing_generic_entry_update(&entry
->ent
, 0, preempt_count());
351 entry
->ent
.type
= TRACE_MMIO_MAP
;
353 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
358 void mmio_trace_mapping(struct mmiotrace_map
*map
)
360 struct trace_array
*tr
= mmio_trace_array
;
361 struct trace_array_cpu
*data
;
364 data
= tr
->data
[smp_processor_id()];
365 __trace_mmiotrace_map(tr
, data
, map
);
369 int mmio_trace_printk(const char *fmt
, va_list args
)
371 return trace_vprintk(0, -1, fmt
, args
);