1 // SPDX-License-Identifier: GPL-2.0
3 * Memory mapped I/O tracing
5 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
10 #include <linux/kernel.h>
11 #include <linux/mmiotrace.h>
12 #include <linux/pci.h>
13 #include <linux/slab.h>
14 #include <linux/time.h>
16 #include <linux/atomic.h>
19 #include "trace_output.h"
25 static struct trace_array
*mmio_trace_array
;
26 static bool overrun_detected
;
27 static unsigned long prev_overruns
;
28 static atomic_t dropped_count
;
30 static void mmio_reset_data(struct trace_array
*tr
)
32 overrun_detected
= false;
35 tracing_reset_online_cpus(&tr
->trace_buffer
);
38 static int mmio_trace_init(struct trace_array
*tr
)
40 pr_debug("in %s\n", __func__
);
41 mmio_trace_array
= tr
;
48 static void mmio_trace_reset(struct trace_array
*tr
)
50 pr_debug("in %s\n", __func__
);
54 mmio_trace_array
= NULL
;
57 static void mmio_trace_start(struct trace_array
*tr
)
59 pr_debug("in %s\n", __func__
);
63 static void mmio_print_pcidev(struct trace_seq
*s
, const struct pci_dev
*dev
)
66 resource_size_t start
, end
;
67 const struct pci_driver
*drv
= pci_dev_driver(dev
);
69 trace_seq_printf(s
, "PCIDEV %02x%02x %04x%04x %x",
70 dev
->bus
->number
, dev
->devfn
,
71 dev
->vendor
, dev
->device
, dev
->irq
);
72 for (i
= 0; i
< 7; i
++) {
73 start
= dev
->resource
[i
].start
;
74 trace_seq_printf(s
, " %llx",
75 (unsigned long long)(start
|
76 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
78 for (i
= 0; i
< 7; i
++) {
79 start
= dev
->resource
[i
].start
;
80 end
= dev
->resource
[i
].end
;
81 trace_seq_printf(s
, " %llx",
82 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
83 (unsigned long long)(end
- start
) + 1 : 0);
86 trace_seq_printf(s
, " %s\n", drv
->name
);
88 trace_seq_puts(s
, " \n");
91 static void destroy_header_iter(struct header_iter
*hiter
)
95 pci_dev_put(hiter
->dev
);
99 static void mmio_pipe_open(struct trace_iterator
*iter
)
101 struct header_iter
*hiter
;
102 struct trace_seq
*s
= &iter
->seq
;
104 trace_seq_puts(s
, "VERSION 20070824\n");
106 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
110 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
111 iter
->private = hiter
;
114 /* XXX: This is not called when the pipe is closed! */
115 static void mmio_close(struct trace_iterator
*iter
)
117 struct header_iter
*hiter
= iter
->private;
118 destroy_header_iter(hiter
);
119 iter
->private = NULL
;
122 static unsigned long count_overruns(struct trace_iterator
*iter
)
124 unsigned long cnt
= atomic_xchg(&dropped_count
, 0);
125 unsigned long over
= ring_buffer_overruns(iter
->trace_buffer
->buffer
);
127 if (over
> prev_overruns
)
128 cnt
+= over
- prev_overruns
;
129 prev_overruns
= over
;
133 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
134 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
137 struct header_iter
*hiter
= iter
->private;
138 struct trace_seq
*s
= &iter
->seq
;
141 n
= count_overruns(iter
);
143 /* XXX: This is later than where events were lost. */
144 trace_seq_printf(s
, "MARK 0.000000 Lost %lu events.\n", n
);
145 if (!overrun_detected
)
146 pr_warn("mmiotrace has lost events\n");
147 overrun_detected
= true;
154 mmio_print_pcidev(s
, hiter
->dev
);
155 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
158 destroy_header_iter(hiter
);
159 iter
->private = NULL
;
163 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
164 return (ret
== -EBUSY
) ? 0 : ret
;
167 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
169 struct trace_entry
*entry
= iter
->ent
;
170 struct trace_mmiotrace_rw
*field
;
171 struct mmiotrace_rw
*rw
;
172 struct trace_seq
*s
= &iter
->seq
;
173 unsigned long long t
= ns2usecs(iter
->ts
);
174 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
175 unsigned secs
= (unsigned long)t
;
177 trace_assign_type(field
, entry
);
180 switch (rw
->opcode
) {
183 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
184 rw
->width
, secs
, usec_rem
, rw
->map_id
,
185 (unsigned long long)rw
->phys
,
186 rw
->value
, rw
->pc
, 0);
190 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
191 rw
->width
, secs
, usec_rem
, rw
->map_id
,
192 (unsigned long long)rw
->phys
,
193 rw
->value
, rw
->pc
, 0);
195 case MMIO_UNKNOWN_OP
:
197 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
199 secs
, usec_rem
, rw
->map_id
,
200 (unsigned long long)rw
->phys
,
201 (rw
->value
>> 16) & 0xff, (rw
->value
>> 8) & 0xff,
202 (rw
->value
>> 0) & 0xff, rw
->pc
, 0);
205 trace_seq_puts(s
, "rw what?\n");
209 return trace_handle_return(s
);
212 static enum print_line_t
mmio_print_map(struct trace_iterator
*iter
)
214 struct trace_entry
*entry
= iter
->ent
;
215 struct trace_mmiotrace_map
*field
;
216 struct mmiotrace_map
*m
;
217 struct trace_seq
*s
= &iter
->seq
;
218 unsigned long long t
= ns2usecs(iter
->ts
);
219 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
220 unsigned secs
= (unsigned long)t
;
222 trace_assign_type(field
, entry
);
228 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
229 secs
, usec_rem
, m
->map_id
,
230 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
235 "UNMAP %u.%06lu %d 0x%lx %d\n",
236 secs
, usec_rem
, m
->map_id
, 0UL, 0);
239 trace_seq_puts(s
, "map what?\n");
243 return trace_handle_return(s
);
246 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
248 struct trace_entry
*entry
= iter
->ent
;
249 struct print_entry
*print
= (struct print_entry
*)entry
;
250 const char *msg
= print
->buf
;
251 struct trace_seq
*s
= &iter
->seq
;
252 unsigned long long t
= ns2usecs(iter
->ts
);
253 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
254 unsigned secs
= (unsigned long)t
;
256 /* The trailing newline must be in the message. */
257 trace_seq_printf(s
, "MARK %u.%06lu %s", secs
, usec_rem
, msg
);
259 return trace_handle_return(s
);
262 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
264 switch (iter
->ent
->type
) {
266 return mmio_print_rw(iter
);
268 return mmio_print_map(iter
);
270 return mmio_print_mark(iter
);
272 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
276 static struct tracer mmio_tracer __read_mostly
=
279 .init
= mmio_trace_init
,
280 .reset
= mmio_trace_reset
,
281 .start
= mmio_trace_start
,
282 .pipe_open
= mmio_pipe_open
,
285 .print_line
= mmio_print_line
,
289 __init
static int init_mmio_trace(void)
291 return register_tracer(&mmio_tracer
);
293 device_initcall(init_mmio_trace
);
295 static void __trace_mmiotrace_rw(struct trace_array
*tr
,
296 struct trace_array_cpu
*data
,
297 struct mmiotrace_rw
*rw
)
299 struct trace_event_call
*call
= &event_mmiotrace_rw
;
300 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
301 struct ring_buffer_event
*event
;
302 struct trace_mmiotrace_rw
*entry
;
303 int pc
= preempt_count();
305 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_RW
,
306 sizeof(*entry
), 0, pc
);
308 atomic_inc(&dropped_count
);
311 entry
= ring_buffer_event_data(event
);
314 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
315 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
318 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
320 struct trace_array
*tr
= mmio_trace_array
;
321 struct trace_array_cpu
*data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
322 __trace_mmiotrace_rw(tr
, data
, rw
);
325 static void __trace_mmiotrace_map(struct trace_array
*tr
,
326 struct trace_array_cpu
*data
,
327 struct mmiotrace_map
*map
)
329 struct trace_event_call
*call
= &event_mmiotrace_map
;
330 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
331 struct ring_buffer_event
*event
;
332 struct trace_mmiotrace_map
*entry
;
333 int pc
= preempt_count();
335 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_MAP
,
336 sizeof(*entry
), 0, pc
);
338 atomic_inc(&dropped_count
);
341 entry
= ring_buffer_event_data(event
);
344 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
345 trace_buffer_unlock_commit(tr
, buffer
, event
, 0, pc
);
348 void mmio_trace_mapping(struct mmiotrace_map
*map
)
350 struct trace_array
*tr
= mmio_trace_array
;
351 struct trace_array_cpu
*data
;
354 data
= per_cpu_ptr(tr
->trace_buffer
.data
, smp_processor_id());
355 __trace_mmiotrace_map(tr
, data
, map
);
359 int mmio_trace_printk(const char *fmt
, va_list args
)
361 return trace_vprintk(0, fmt
, args
);