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