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