1 // SPDX-License-Identifier: GPL-2.0
3 * Memory mapped I/O tracing
5 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
8 #include <linux/kernel.h>
9 #include <linux/mmiotrace.h>
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/time.h>
14 #include <linux/atomic.h>
17 #include "trace_output.h"
23 static struct trace_array
*mmio_trace_array
;
24 static bool overrun_detected
;
25 static unsigned long prev_overruns
;
26 static atomic_t dropped_count
;
28 static void mmio_reset_data(struct trace_array
*tr
)
30 overrun_detected
= false;
33 tracing_reset_online_cpus(&tr
->array_buffer
);
36 static int mmio_trace_init(struct trace_array
*tr
)
38 pr_debug("in %s\n", __func__
);
39 mmio_trace_array
= tr
;
46 static void mmio_trace_reset(struct trace_array
*tr
)
48 pr_debug("in %s\n", __func__
);
52 mmio_trace_array
= NULL
;
55 static void mmio_trace_start(struct trace_array
*tr
)
57 pr_debug("in %s\n", __func__
);
61 static void 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 trace_seq_printf(s
, "PCIDEV %02x%02x %04x%04x %x",
68 dev
->bus
->number
, dev
->devfn
,
69 dev
->vendor
, dev
->device
, dev
->irq
);
70 for (i
= 0; i
< 7; i
++) {
71 start
= dev
->resource
[i
].start
;
72 trace_seq_printf(s
, " %llx",
73 (unsigned long long)(start
|
74 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
76 for (i
= 0; i
< 7; i
++) {
77 start
= dev
->resource
[i
].start
;
78 end
= dev
->resource
[i
].end
;
79 trace_seq_printf(s
, " %llx",
80 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
81 (unsigned long long)(end
- start
) + 1 : 0);
84 trace_seq_printf(s
, " %s\n", drv
->name
);
86 trace_seq_puts(s
, " \n");
89 static void destroy_header_iter(struct header_iter
*hiter
)
93 pci_dev_put(hiter
->dev
);
97 static void mmio_pipe_open(struct trace_iterator
*iter
)
99 struct header_iter
*hiter
;
100 struct trace_seq
*s
= &iter
->seq
;
102 trace_seq_puts(s
, "VERSION 20070824\n");
104 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
108 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
109 iter
->private = hiter
;
112 /* XXX: This is not called when the pipe is closed! */
113 static void mmio_close(struct trace_iterator
*iter
)
115 struct header_iter
*hiter
= iter
->private;
116 destroy_header_iter(hiter
);
117 iter
->private = NULL
;
120 static unsigned long count_overruns(struct trace_iterator
*iter
)
122 unsigned long cnt
= atomic_xchg(&dropped_count
, 0);
123 unsigned long over
= ring_buffer_overruns(iter
->array_buffer
->buffer
);
125 if (over
> prev_overruns
)
126 cnt
+= over
- prev_overruns
;
127 prev_overruns
= over
;
131 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
132 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
135 struct header_iter
*hiter
= iter
->private;
136 struct trace_seq
*s
= &iter
->seq
;
139 n
= count_overruns(iter
);
141 /* XXX: This is later than where events were lost. */
142 trace_seq_printf(s
, "MARK 0.000000 Lost %lu events.\n", n
);
143 if (!overrun_detected
)
144 pr_warn("mmiotrace has lost events\n");
145 overrun_detected
= true;
152 mmio_print_pcidev(s
, hiter
->dev
);
153 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
156 destroy_header_iter(hiter
);
157 iter
->private = NULL
;
161 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
162 return (ret
== -EBUSY
) ? 0 : ret
;
165 static enum print_line_t
mmio_print_rw(struct trace_iterator
*iter
)
167 struct trace_entry
*entry
= iter
->ent
;
168 struct trace_mmiotrace_rw
*field
;
169 struct mmiotrace_rw
*rw
;
170 struct trace_seq
*s
= &iter
->seq
;
171 unsigned long long t
= ns2usecs(iter
->ts
);
172 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
173 unsigned secs
= (unsigned long)t
;
175 trace_assign_type(field
, entry
);
178 switch (rw
->opcode
) {
181 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
182 rw
->width
, secs
, usec_rem
, rw
->map_id
,
183 (unsigned long long)rw
->phys
,
184 rw
->value
, rw
->pc
, 0);
188 "W %d %u.%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);
193 case MMIO_UNKNOWN_OP
:
195 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
197 secs
, usec_rem
, rw
->map_id
,
198 (unsigned long long)rw
->phys
,
199 (rw
->value
>> 16) & 0xff, (rw
->value
>> 8) & 0xff,
200 (rw
->value
>> 0) & 0xff, rw
->pc
, 0);
203 trace_seq_puts(s
, "rw what?\n");
207 return trace_handle_return(s
);
210 static enum print_line_t
mmio_print_map(struct trace_iterator
*iter
)
212 struct trace_entry
*entry
= iter
->ent
;
213 struct trace_mmiotrace_map
*field
;
214 struct mmiotrace_map
*m
;
215 struct trace_seq
*s
= &iter
->seq
;
216 unsigned long long t
= ns2usecs(iter
->ts
);
217 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
218 unsigned secs
= (unsigned long)t
;
220 trace_assign_type(field
, entry
);
226 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
227 secs
, usec_rem
, m
->map_id
,
228 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
233 "UNMAP %u.%06lu %d 0x%lx %d\n",
234 secs
, usec_rem
, m
->map_id
, 0UL, 0);
237 trace_seq_puts(s
, "map what?\n");
241 return trace_handle_return(s
);
244 static enum print_line_t
mmio_print_mark(struct trace_iterator
*iter
)
246 struct trace_entry
*entry
= iter
->ent
;
247 struct print_entry
*print
= (struct print_entry
*)entry
;
248 const char *msg
= print
->buf
;
249 struct trace_seq
*s
= &iter
->seq
;
250 unsigned long long t
= ns2usecs(iter
->ts
);
251 unsigned long usec_rem
= do_div(t
, USEC_PER_SEC
);
252 unsigned secs
= (unsigned long)t
;
254 /* The trailing newline must be in the message. */
255 trace_seq_printf(s
, "MARK %u.%06lu %s", secs
, usec_rem
, msg
);
257 return trace_handle_return(s
);
260 static enum print_line_t
mmio_print_line(struct trace_iterator
*iter
)
262 switch (iter
->ent
->type
) {
264 return mmio_print_rw(iter
);
266 return mmio_print_map(iter
);
268 return mmio_print_mark(iter
);
270 return TRACE_TYPE_HANDLED
; /* ignore unknown entries */
274 static struct tracer mmio_tracer __read_mostly
=
277 .init
= mmio_trace_init
,
278 .reset
= mmio_trace_reset
,
279 .start
= mmio_trace_start
,
280 .pipe_open
= mmio_pipe_open
,
283 .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 trace_buffer
*buffer
= tr
->array_buffer
.buffer
;
299 struct ring_buffer_event
*event
;
300 struct trace_mmiotrace_rw
*entry
;
301 unsigned int trace_ctx
;
303 trace_ctx
= tracing_gen_ctx_flags(0);
304 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_RW
,
305 sizeof(*entry
), trace_ctx
);
307 atomic_inc(&dropped_count
);
310 entry
= ring_buffer_event_data(event
);
313 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
314 trace_buffer_unlock_commit(tr
, buffer
, event
, trace_ctx
);
317 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
319 struct trace_array
*tr
= mmio_trace_array
;
320 struct trace_array_cpu
*data
= per_cpu_ptr(tr
->array_buffer
.data
, smp_processor_id());
321 __trace_mmiotrace_rw(tr
, data
, rw
);
324 static void __trace_mmiotrace_map(struct trace_array
*tr
,
325 struct trace_array_cpu
*data
,
326 struct mmiotrace_map
*map
)
328 struct trace_event_call
*call
= &event_mmiotrace_map
;
329 struct trace_buffer
*buffer
= tr
->array_buffer
.buffer
;
330 struct ring_buffer_event
*event
;
331 struct trace_mmiotrace_map
*entry
;
332 unsigned int trace_ctx
;
334 trace_ctx
= tracing_gen_ctx_flags(0);
335 event
= trace_buffer_lock_reserve(buffer
, TRACE_MMIO_MAP
,
336 sizeof(*entry
), trace_ctx
);
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
, trace_ctx
);
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
->array_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
);