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>
19 static struct trace_array
*mmio_trace_array
;
20 static bool overrun_detected
;
21 static unsigned long prev_overruns
;
23 static void mmio_reset_data(struct trace_array
*tr
)
27 overrun_detected
= false;
29 tr
->time_start
= ftrace_now(tr
->cpu
);
31 for_each_online_cpu(cpu
)
32 tracing_reset(tr
, cpu
);
35 static int mmio_trace_init(struct trace_array
*tr
)
37 pr_debug("in %s\n", __func__
);
38 mmio_trace_array
= tr
;
45 static void mmio_trace_reset(struct trace_array
*tr
)
47 pr_debug("in %s\n", __func__
);
51 mmio_trace_array
= NULL
;
54 static void mmio_trace_start(struct trace_array
*tr
)
56 pr_debug("in %s\n", __func__
);
60 static int mmio_print_pcidev(struct trace_seq
*s
, const struct pci_dev
*dev
)
64 resource_size_t start
, end
;
65 const struct pci_driver
*drv
= pci_dev_driver(dev
);
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
);
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 ret
+= 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 ret
+= trace_seq_printf(s
, " %llx",
85 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
86 (unsigned long long)(end
- start
) + 1 : 0);
89 ret
+= trace_seq_printf(s
, " %s\n", drv
->name
);
91 ret
+= trace_seq_printf(s
, " \n");
95 static void destroy_header_iter(struct header_iter
*hiter
)
99 pci_dev_put(hiter
->dev
);
103 static void mmio_pipe_open(struct trace_iterator
*iter
)
105 struct header_iter
*hiter
;
106 struct trace_seq
*s
= &iter
->seq
;
108 trace_seq_printf(s
, "VERSION 20070824\n");
110 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
114 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
115 iter
->private = hiter
;
118 /* XXX: This is not called when the pipe is closed! */
119 static void mmio_close(struct trace_iterator
*iter
)
121 struct header_iter
*hiter
= iter
->private;
122 destroy_header_iter(hiter
);
123 iter
->private = NULL
;
126 static unsigned long count_overruns(struct trace_iterator
*iter
)
128 unsigned long cnt
= 0;
129 unsigned long over
= ring_buffer_overruns(iter
->tr
->buffer
);
131 if (over
> prev_overruns
)
132 cnt
= over
- prev_overruns
;
133 prev_overruns
= over
;
137 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
138 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
141 struct header_iter
*hiter
= iter
->private;
142 struct trace_seq
*s
= &iter
->seq
;
145 n
= count_overruns(iter
);
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;
158 mmio_print_pcidev(s
, hiter
->dev
);
159 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
162 destroy_header_iter(hiter
);
163 iter
->private = NULL
;
167 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
168 return (ret
== -EBUSY
) ? 0 : ret
;
171 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
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
;
182 trace_assign_type(field
, entry
);
185 switch (rw
->opcode
) {
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);
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);
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);
209 ret
= trace_seq_printf(s
, "rw what?\n");
213 return TRACE_TYPE_HANDLED
;
214 return TRACE_TYPE_PARTIAL_LINE
;
217 static enum print_line_t
mmio_print_map(struct trace_iterator
*iter
)
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
;
228 trace_assign_type(field
, entry
);
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
,
240 ret
= trace_seq_printf(s
,
241 "UNMAP %lu.%06lu %d 0x%lx %d\n",
242 secs
, usec_rem
, m
->map_id
, 0UL, 0);
245 ret
= trace_seq_printf(s
, "map what?\n");
249 return TRACE_TYPE_HANDLED
;
250 return TRACE_TYPE_PARTIAL_LINE
;
253 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
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
;
264 /* The trailing newline must be in the message. */
265 ret
= trace_seq_printf(s
, "MARK %lu.%06lu %s", secs
, usec_rem
, msg
);
267 return TRACE_TYPE_PARTIAL_LINE
;
269 if (entry
->flags
& TRACE_FLAG_CONT
)
270 trace_seq_print_cont(s
, iter
);
272 return TRACE_TYPE_HANDLED
;
275 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
277 switch (iter
->ent
->type
) {
279 return mmio_print_rw(iter
);
281 return mmio_print_map(iter
);
283 return mmio_print_mark(iter
);
285 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
289 static struct tracer mmio_tracer __read_mostly
=
292 .init
= mmio_trace_init
,
293 .reset
= mmio_trace_reset
,
294 .start
= mmio_trace_start
,
295 .pipe_open
= mmio_pipe_open
,
298 .print_line
= mmio_print_line
,
301 __init
static int init_mmio_trace(void)
303 return register_tracer(&mmio_tracer
);
305 device_initcall(init_mmio_trace
);
307 static void __trace_mmiotrace_rw(struct trace_array
*tr
,
308 struct trace_array_cpu
*data
,
309 struct mmiotrace_rw
*rw
)
311 struct ring_buffer_event
*event
;
312 struct trace_mmiotrace_rw
*entry
;
313 unsigned long irq_flags
;
315 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
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
),
347 entry
= ring_buffer_event_data(event
);
348 tracing_generic_entry_update(&entry
->ent
, 0, preempt_count());
349 entry
->ent
.type
= TRACE_MMIO_MAP
;
351 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
356 void mmio_trace_mapping(struct mmiotrace_map
*map
)
358 struct trace_array
*tr
= mmio_trace_array
;
359 struct trace_array_cpu
*data
;
362 data
= tr
->data
[smp_processor_id()];
363 __trace_mmiotrace_map(tr
, data
, map
);
367 int mmio_trace_printk(const char *fmt
, va_list args
)
369 return trace_vprintk(0, fmt
, args
);