]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/trace_mmiotrace.c
tracing: Erase irqsoff trace with empty write
[mirror_ubuntu-bionic-kernel.git] / kernel / trace / trace_mmiotrace.c
CommitLineData
f984b51e
PP
1/*
2 * Memory mapped I/O tracing
3 *
4 * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
5 */
6
7#define DEBUG 1
8
9#include <linux/kernel.h>
10#include <linux/mmiotrace.h>
13829537 11#include <linux/pci.h>
5a0e3ad6 12#include <linux/slab.h>
a5dec557
LZ
13#include <linux/time.h>
14
60063497 15#include <linux/atomic.h>
f984b51e
PP
16
17#include "trace.h"
f0868d1e 18#include "trace_output.h"
f984b51e 19
d0a7e8ca
PP
20struct header_iter {
21 struct pci_dev *dev;
22};
23
f984b51e 24static struct trace_array *mmio_trace_array;
2039238b 25static bool overrun_detected;
7ee1768d 26static unsigned long prev_overruns;
173ed24e 27static atomic_t dropped_count;
f984b51e 28
bd8ac686
PP
29static void mmio_reset_data(struct trace_array *tr)
30{
2039238b 31 overrun_detected = false;
7ee1768d 32 prev_overruns = 0;
bd8ac686 33
12883efb 34 tracing_reset_online_cpus(&tr->trace_buffer);
bd8ac686 35}
f984b51e 36
1c80025a 37static int mmio_trace_init(struct trace_array *tr)
f984b51e
PP
38{
39 pr_debug("in %s\n", __func__);
40 mmio_trace_array = tr;
c76f0694
SR
41
42 mmio_reset_data(tr);
43 enable_mmiotrace();
1c80025a 44 return 0;
f984b51e
PP
45}
46
47static void mmio_trace_reset(struct trace_array *tr)
48{
49 pr_debug("in %s\n", __func__);
c76f0694
SR
50
51 disable_mmiotrace();
bd8ac686
PP
52 mmio_reset_data(tr);
53 mmio_trace_array = NULL;
f984b51e
PP
54}
55
bbf5b1a0 56static void mmio_trace_start(struct trace_array *tr)
f984b51e
PP
57{
58 pr_debug("in %s\n", __func__);
bbf5b1a0 59 mmio_reset_data(tr);
bd8ac686
PP
60}
61
a72e10af 62static void mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
13829537 63{
13829537
PP
64 int i;
65 resource_size_t start, end;
66 const struct pci_driver *drv = pci_dev_driver(dev);
67
a72e10af
SRRH
68 trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
69 dev->bus->number, dev->devfn,
70 dev->vendor, dev->device, dev->irq);
13829537 71 for (i = 0; i < 7; i++) {
9a51933e 72 start = dev->resource[i].start;
a72e10af 73 trace_seq_printf(s, " %llx",
13829537
PP
74 (unsigned long long)(start |
75 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
76 }
77 for (i = 0; i < 7; i++) {
9a51933e
BH
78 start = dev->resource[i].start;
79 end = dev->resource[i].end;
a72e10af 80 trace_seq_printf(s, " %llx",
13829537
PP
81 dev->resource[i].start < dev->resource[i].end ?
82 (unsigned long long)(end - start) + 1 : 0);
83 }
84 if (drv)
a72e10af 85 trace_seq_printf(s, " %s\n", drv->name);
13829537 86 else
a72e10af 87 trace_seq_puts(s, " \n");
13829537
PP
88}
89
d0a7e8ca
PP
90static void destroy_header_iter(struct header_iter *hiter)
91{
92 if (!hiter)
93 return;
94 pci_dev_put(hiter->dev);
95 kfree(hiter);
96}
97
98static void mmio_pipe_open(struct trace_iterator *iter)
bd8ac686 99{
d0a7e8ca 100 struct header_iter *hiter;
bd8ac686 101 struct trace_seq *s = &iter->seq;
13829537 102
146c3442 103 trace_seq_puts(s, "VERSION 20070824\n");
13829537 104
d0a7e8ca
PP
105 hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
106 if (!hiter)
107 return;
108
109 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
110 iter->private = hiter;
111}
112
113/* XXX: This is not called when the pipe is closed! */
114static void mmio_close(struct trace_iterator *iter)
115{
116 struct header_iter *hiter = iter->private;
117 destroy_header_iter(hiter);
118 iter->private = NULL;
119}
120
2039238b
PP
121static unsigned long count_overruns(struct trace_iterator *iter)
122{
173ed24e 123 unsigned long cnt = atomic_xchg(&dropped_count, 0);
12883efb 124 unsigned long over = ring_buffer_overruns(iter->trace_buffer->buffer);
7ee1768d
PP
125
126 if (over > prev_overruns)
173ed24e 127 cnt += over - prev_overruns;
7ee1768d 128 prev_overruns = over;
2039238b
PP
129 return cnt;
130}
131
d0a7e8ca
PP
132static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
133 char __user *ubuf, size_t cnt, loff_t *ppos)
134{
135 ssize_t ret;
136 struct header_iter *hiter = iter->private;
137 struct trace_seq *s = &iter->seq;
2039238b
PP
138 unsigned long n;
139
140 n = count_overruns(iter);
141 if (n) {
142 /* XXX: This is later than where events were lost. */
143 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
144 if (!overrun_detected)
a395d6a7 145 pr_warn("mmiotrace has lost events\n");
2039238b
PP
146 overrun_detected = true;
147 goto print_out;
148 }
d0a7e8ca
PP
149
150 if (!hiter)
151 return 0;
152
153 mmio_print_pcidev(s, hiter->dev);
154 hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
155
156 if (!hiter->dev) {
157 destroy_header_iter(hiter);
158 iter->private = NULL;
159 }
160
2039238b 161print_out:
d0a7e8ca
PP
162 ret = trace_seq_to_user(s, ubuf, cnt);
163 return (ret == -EBUSY) ? 0 : ret;
bd8ac686
PP
164}
165
07f4e4f7 166static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
bd8ac686
PP
167{
168 struct trace_entry *entry = iter->ent;
7104f300
SR
169 struct trace_mmiotrace_rw *field;
170 struct mmiotrace_rw *rw;
bd8ac686 171 struct trace_seq *s = &iter->seq;
3928a8a2 172 unsigned long long t = ns2usecs(iter->ts);
a5dec557 173 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
bd8ac686 174 unsigned secs = (unsigned long)t;
bd8ac686 175
7104f300
SR
176 trace_assign_type(field, entry);
177 rw = &field->rw;
178
777e208d 179 switch (rw->opcode) {
bd8ac686 180 case MMIO_READ:
a72e10af 181 trace_seq_printf(s,
5e4abc98 182 "R %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
dee310d0
PP
183 rw->width, secs, usec_rem, rw->map_id,
184 (unsigned long long)rw->phys,
736ca61f 185 rw->value, rw->pc, 0);
bd8ac686
PP
186 break;
187 case MMIO_WRITE:
a72e10af 188 trace_seq_printf(s,
5e4abc98 189 "W %d %u.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
dee310d0
PP
190 rw->width, secs, usec_rem, rw->map_id,
191 (unsigned long long)rw->phys,
736ca61f 192 rw->value, rw->pc, 0);
bd8ac686
PP
193 break;
194 case MMIO_UNKNOWN_OP:
a72e10af 195 trace_seq_printf(s,
5e4abc98
SR
196 "UNKNOWN %u.%06lu %d 0x%llx %02lx,%02lx,"
197 "%02lx 0x%lx %d\n",
dee310d0
PP
198 secs, usec_rem, rw->map_id,
199 (unsigned long long)rw->phys,
bd8ac686 200 (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
736ca61f 201 (rw->value >> 0) & 0xff, rw->pc, 0);
bd8ac686
PP
202 break;
203 default:
a72e10af 204 trace_seq_puts(s, "rw what?\n");
bd8ac686
PP
205 break;
206 }
a72e10af
SRRH
207
208 return trace_handle_return(s);
bd8ac686
PP
209}
210
07f4e4f7 211static enum print_line_t mmio_print_map(struct trace_iterator *iter)
bd8ac686
PP
212{
213 struct trace_entry *entry = iter->ent;
7104f300
SR
214 struct trace_mmiotrace_map *field;
215 struct mmiotrace_map *m;
bd8ac686 216 struct trace_seq *s = &iter->seq;
3928a8a2 217 unsigned long long t = ns2usecs(iter->ts);
a5dec557 218 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
bd8ac686 219 unsigned secs = (unsigned long)t;
bd8ac686 220
7104f300
SR
221 trace_assign_type(field, entry);
222 m = &field->map;
223
777e208d 224 switch (m->opcode) {
bd8ac686 225 case MMIO_PROBE:
a72e10af 226 trace_seq_printf(s,
5e4abc98 227 "MAP %u.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
dee310d0
PP
228 secs, usec_rem, m->map_id,
229 (unsigned long long)m->phys, m->virt, m->len,
e0fd5c2f 230 0UL, 0);
bd8ac686
PP
231 break;
232 case MMIO_UNPROBE:
a72e10af 233 trace_seq_printf(s,
5e4abc98 234 "UNMAP %u.%06lu %d 0x%lx %d\n",
e0fd5c2f 235 secs, usec_rem, m->map_id, 0UL, 0);
bd8ac686
PP
236 break;
237 default:
a72e10af 238 trace_seq_puts(s, "map what?\n");
bd8ac686
PP
239 break;
240 }
a72e10af
SRRH
241
242 return trace_handle_return(s);
bd8ac686
PP
243}
244
07f4e4f7 245static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
fc5e27ae
PP
246{
247 struct trace_entry *entry = iter->ent;
777e208d 248 struct print_entry *print = (struct print_entry *)entry;
48ead020 249 const char *msg = print->buf;
fc5e27ae 250 struct trace_seq *s = &iter->seq;
3928a8a2 251 unsigned long long t = ns2usecs(iter->ts);
769b0441 252 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
fc5e27ae 253 unsigned secs = (unsigned long)t;
fc5e27ae
PP
254
255 /* The trailing newline must be in the message. */
a72e10af 256 trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
fc5e27ae 257
a72e10af 258 return trace_handle_return(s);
fc5e27ae
PP
259}
260
07f4e4f7 261static enum print_line_t mmio_print_line(struct trace_iterator *iter)
bd8ac686
PP
262{
263 switch (iter->ent->type) {
264 case TRACE_MMIO_RW:
265 return mmio_print_rw(iter);
266 case TRACE_MMIO_MAP:
267 return mmio_print_map(iter);
fc5e27ae
PP
268 case TRACE_PRINT:
269 return mmio_print_mark(iter);
bd8ac686 270 default:
07f4e4f7 271 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
bd8ac686 272 }
f984b51e
PP
273}
274
275static struct tracer mmio_tracer __read_mostly =
276{
277 .name = "mmiotrace",
278 .init = mmio_trace_init,
279 .reset = mmio_trace_reset,
bbf5b1a0 280 .start = mmio_trace_start,
d0a7e8ca
PP
281 .pipe_open = mmio_pipe_open,
282 .close = mmio_close,
283 .read = mmio_read,
bd8ac686 284 .print_line = mmio_print_line,
f984b51e
PP
285};
286
287__init static int init_mmio_trace(void)
288{
f984b51e
PP
289 return register_tracer(&mmio_tracer);
290}
291device_initcall(init_mmio_trace);
292
45dcd8b8
PP
293static void __trace_mmiotrace_rw(struct trace_array *tr,
294 struct trace_array_cpu *data,
295 struct mmiotrace_rw *rw)
296{
2425bcb9 297 struct trace_event_call *call = &event_mmiotrace_rw;
12883efb 298 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 299 struct ring_buffer_event *event;
777e208d 300 struct trace_mmiotrace_rw *entry;
51a763dd 301 int pc = preempt_count();
45dcd8b8 302
e77405ad 303 event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_RW,
51a763dd 304 sizeof(*entry), 0, pc);
173ed24e
PP
305 if (!event) {
306 atomic_inc(&dropped_count);
3928a8a2 307 return;
173ed24e 308 }
3928a8a2 309 entry = ring_buffer_event_data(event);
777e208d 310 entry->rw = *rw;
60ba7702 311
f306cc82 312 if (!call_filter_check_discard(call, entry, buffer, event))
b7f0c959 313 trace_buffer_unlock_commit(tr, buffer, event, 0, pc);
45dcd8b8
PP
314}
315
bd8ac686 316void mmio_trace_rw(struct mmiotrace_rw *rw)
f984b51e
PP
317{
318 struct trace_array *tr = mmio_trace_array;
12883efb 319 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, smp_processor_id());
bd8ac686
PP
320 __trace_mmiotrace_rw(tr, data, rw);
321}
f984b51e 322
45dcd8b8
PP
323static void __trace_mmiotrace_map(struct trace_array *tr,
324 struct trace_array_cpu *data,
325 struct mmiotrace_map *map)
326{
2425bcb9 327 struct trace_event_call *call = &event_mmiotrace_map;
12883efb 328 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 329 struct ring_buffer_event *event;
777e208d 330 struct trace_mmiotrace_map *entry;
51a763dd 331 int pc = preempt_count();
45dcd8b8 332
e77405ad 333 event = trace_buffer_lock_reserve(buffer, TRACE_MMIO_MAP,
51a763dd 334 sizeof(*entry), 0, pc);
173ed24e
PP
335 if (!event) {
336 atomic_inc(&dropped_count);
3928a8a2 337 return;
173ed24e 338 }
3928a8a2 339 entry = ring_buffer_event_data(event);
777e208d 340 entry->map = *map;
60ba7702 341
f306cc82 342 if (!call_filter_check_discard(call, entry, buffer, event))
b7f0c959 343 trace_buffer_unlock_commit(tr, buffer, event, 0, pc);
45dcd8b8
PP
344}
345
bd8ac686
PP
346void mmio_trace_mapping(struct mmiotrace_map *map)
347{
348 struct trace_array *tr = mmio_trace_array;
349 struct trace_array_cpu *data;
350
351 preempt_disable();
12883efb 352 data = per_cpu_ptr(tr->trace_buffer.data, smp_processor_id());
bd8ac686
PP
353 __trace_mmiotrace_map(tr, data, map);
354 preempt_enable();
f984b51e 355}
9e57fb35
PP
356
357int mmio_trace_printk(const char *fmt, va_list args)
358{
40ce74f1 359 return trace_vprintk(0, fmt, args);
9e57fb35 360}