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
;
22 static void mmio_reset_data(struct trace_array
*tr
)
26 overrun_detected
= false;
27 tr
->time_start
= ftrace_now(tr
->cpu
);
29 for_each_online_cpu(cpu
)
30 tracing_reset(tr
->data
[cpu
]);
33 static void 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_ctrl_update(struct trace_array
*tr
)
54 pr_debug("in %s\n", __func__
);
63 static int mmio_print_pcidev(struct trace_seq
*s
, const struct pci_dev
*dev
)
67 resource_size_t start
, end
;
68 const struct pci_driver
*drv
= pci_dev_driver(dev
);
70 /* XXX: incomplete checks for trace_seq_printf() return value */
71 ret
+= trace_seq_printf(s
, "PCIDEV %02x%02x %04x%04x %x",
72 dev
->bus
->number
, dev
->devfn
,
73 dev
->vendor
, dev
->device
, dev
->irq
);
75 * XXX: is pci_resource_to_user() appropriate, since we are
76 * supposed to interpret the __ioremap() phys_addr argument based on
77 * these printed values?
79 for (i
= 0; i
< 7; i
++) {
80 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
81 ret
+= trace_seq_printf(s
, " %llx",
82 (unsigned long long)(start
|
83 (dev
->resource
[i
].flags
& PCI_REGION_FLAG_MASK
)));
85 for (i
= 0; i
< 7; i
++) {
86 pci_resource_to_user(dev
, i
, &dev
->resource
[i
], &start
, &end
);
87 ret
+= trace_seq_printf(s
, " %llx",
88 dev
->resource
[i
].start
< dev
->resource
[i
].end
?
89 (unsigned long long)(end
- start
) + 1 : 0);
92 ret
+= trace_seq_printf(s
, " %s\n", drv
->name
);
94 ret
+= trace_seq_printf(s
, " \n");
98 static void destroy_header_iter(struct header_iter
*hiter
)
102 pci_dev_put(hiter
->dev
);
106 static void mmio_pipe_open(struct trace_iterator
*iter
)
108 struct header_iter
*hiter
;
109 struct trace_seq
*s
= &iter
->seq
;
111 trace_seq_printf(s
, "VERSION 20070824\n");
113 hiter
= kzalloc(sizeof(*hiter
), GFP_KERNEL
);
117 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
118 iter
->private = hiter
;
121 /* XXX: This is not called when the pipe is closed! */
122 static void mmio_close(struct trace_iterator
*iter
)
124 struct header_iter
*hiter
= iter
->private;
125 destroy_header_iter(hiter
);
126 iter
->private = NULL
;
129 static unsigned long count_overruns(struct trace_iterator
*iter
)
132 unsigned long cnt
= 0;
133 for_each_online_cpu(cpu
) {
134 cnt
+= iter
->overrun
[cpu
];
135 iter
->overrun
[cpu
] = 0;
140 static ssize_t
mmio_read(struct trace_iterator
*iter
, struct file
*filp
,
141 char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
144 struct header_iter
*hiter
= iter
->private;
145 struct trace_seq
*s
= &iter
->seq
;
148 n
= count_overruns(iter
);
150 /* XXX: This is later than where events were lost. */
151 trace_seq_printf(s
, "MARK 0.000000 Lost %lu events.\n", n
);
152 if (!overrun_detected
)
153 pr_warning("mmiotrace has lost events.\n");
154 overrun_detected
= true;
161 mmio_print_pcidev(s
, hiter
->dev
);
162 hiter
->dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, hiter
->dev
);
165 destroy_header_iter(hiter
);
166 iter
->private = NULL
;
170 ret
= trace_seq_to_user(s
, ubuf
, cnt
);
171 return (ret
== -EBUSY
) ? 0 : ret
;
174 static int mmio_print_rw(struct trace_iterator
*iter
)
176 struct trace_entry
*entry
= iter
->ent
;
177 struct mmiotrace_rw
*rw
= &entry
->mmiorw
;
178 struct trace_seq
*s
= &iter
->seq
;
179 unsigned long long t
= ns2usecs(entry
->t
);
180 unsigned long usec_rem
= do_div(t
, 1000000ULL);
181 unsigned secs
= (unsigned long)t
;
184 switch (entry
->mmiorw
.opcode
) {
186 ret
= trace_seq_printf(s
,
187 "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
188 rw
->width
, secs
, usec_rem
, rw
->map_id
,
189 (unsigned long long)rw
->phys
,
190 rw
->value
, rw
->pc
, 0);
193 ret
= trace_seq_printf(s
,
194 "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
195 rw
->width
, secs
, usec_rem
, rw
->map_id
,
196 (unsigned long long)rw
->phys
,
197 rw
->value
, rw
->pc
, 0);
199 case MMIO_UNKNOWN_OP
:
200 ret
= trace_seq_printf(s
,
201 "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
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 ret
= trace_seq_printf(s
, "rw what?\n");
216 static int mmio_print_map(struct trace_iterator
*iter
)
218 struct trace_entry
*entry
= iter
->ent
;
219 struct mmiotrace_map
*m
= &entry
->mmiomap
;
220 struct trace_seq
*s
= &iter
->seq
;
221 unsigned long long t
= ns2usecs(entry
->t
);
222 unsigned long usec_rem
= do_div(t
, 1000000ULL);
223 unsigned secs
= (unsigned long)t
;
226 switch (entry
->mmiorw
.opcode
) {
228 ret
= trace_seq_printf(s
,
229 "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
230 secs
, usec_rem
, m
->map_id
,
231 (unsigned long long)m
->phys
, m
->virt
, m
->len
,
235 ret
= trace_seq_printf(s
,
236 "UNMAP %lu.%06lu %d 0x%lx %d\n",
237 secs
, usec_rem
, m
->map_id
, 0UL, 0);
240 ret
= trace_seq_printf(s
, "map what?\n");
248 /* return 0 to abort printing without consuming current entry in pipe mode */
249 static int mmio_print_line(struct trace_iterator
*iter
)
251 switch (iter
->ent
->type
) {
253 return mmio_print_rw(iter
);
255 return mmio_print_map(iter
);
257 return 1; /* ignore unknown entries */
261 static struct tracer mmio_tracer __read_mostly
=
264 .init
= mmio_trace_init
,
265 .reset
= mmio_trace_reset
,
266 .pipe_open
= mmio_pipe_open
,
269 .ctrl_update
= mmio_trace_ctrl_update
,
270 .print_line
= mmio_print_line
,
273 __init
static int init_mmio_trace(void)
275 return register_tracer(&mmio_tracer
);
277 device_initcall(init_mmio_trace
);
279 void mmio_trace_rw(struct mmiotrace_rw
*rw
)
281 struct trace_array
*tr
= mmio_trace_array
;
282 struct trace_array_cpu
*data
= tr
->data
[smp_processor_id()];
283 __trace_mmiotrace_rw(tr
, data
, rw
);
286 void mmio_trace_mapping(struct mmiotrace_map
*map
)
288 struct trace_array
*tr
= mmio_trace_array
;
289 struct trace_array_cpu
*data
;
292 data
= tr
->data
[smp_processor_id()];
293 __trace_mmiotrace_map(tr
, data
, map
);