irqchip: Fix dependencies for archs w/o HAS_IOMEM
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / perf_event_intel_bts.c
blob2cad71d1b14cfb36df83674dac992f51c37db63a
1 /*
2 * BTS PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
15 #undef DEBUG
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/bitops.h>
20 #include <linux/types.h>
21 #include <linux/slab.h>
22 #include <linux/debugfs.h>
23 #include <linux/device.h>
24 #include <linux/coredump.h>
26 #include <asm-generic/sizes.h>
27 #include <asm/perf_event.h>
29 #include "perf_event.h"
31 struct bts_ctx {
32 struct perf_output_handle handle;
33 struct debug_store ds_back;
34 int started;
37 static DEFINE_PER_CPU(struct bts_ctx, bts_ctx);
39 #define BTS_RECORD_SIZE 24
40 #define BTS_SAFETY_MARGIN 4080
42 struct bts_phys {
43 struct page *page;
44 unsigned long size;
45 unsigned long offset;
46 unsigned long displacement;
49 struct bts_buffer {
50 size_t real_size; /* multiple of BTS_RECORD_SIZE */
51 unsigned int nr_pages;
52 unsigned int nr_bufs;
53 unsigned int cur_buf;
54 bool snapshot;
55 local_t data_size;
56 local_t lost;
57 local_t head;
58 unsigned long end;
59 void **data_pages;
60 struct bts_phys buf[0];
63 struct pmu bts_pmu;
65 static size_t buf_size(struct page *page)
67 return 1 << (PAGE_SHIFT + page_private(page));
70 static void *
71 bts_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool overwrite)
73 struct bts_buffer *buf;
74 struct page *page;
75 int node = (cpu == -1) ? cpu : cpu_to_node(cpu);
76 unsigned long offset;
77 size_t size = nr_pages << PAGE_SHIFT;
78 int pg, nbuf, pad;
80 /* count all the high order buffers */
81 for (pg = 0, nbuf = 0; pg < nr_pages;) {
82 page = virt_to_page(pages[pg]);
83 if (WARN_ON_ONCE(!PagePrivate(page) && nr_pages > 1))
84 return NULL;
85 pg += 1 << page_private(page);
86 nbuf++;
90 * to avoid interrupts in overwrite mode, only allow one physical
92 if (overwrite && nbuf > 1)
93 return NULL;
95 buf = kzalloc_node(offsetof(struct bts_buffer, buf[nbuf]), GFP_KERNEL, node);
96 if (!buf)
97 return NULL;
99 buf->nr_pages = nr_pages;
100 buf->nr_bufs = nbuf;
101 buf->snapshot = overwrite;
102 buf->data_pages = pages;
103 buf->real_size = size - size % BTS_RECORD_SIZE;
105 for (pg = 0, nbuf = 0, offset = 0, pad = 0; nbuf < buf->nr_bufs; nbuf++) {
106 unsigned int __nr_pages;
108 page = virt_to_page(pages[pg]);
109 __nr_pages = PagePrivate(page) ? 1 << page_private(page) : 1;
110 buf->buf[nbuf].page = page;
111 buf->buf[nbuf].offset = offset;
112 buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
113 buf->buf[nbuf].size = buf_size(page) - buf->buf[nbuf].displacement;
114 pad = buf->buf[nbuf].size % BTS_RECORD_SIZE;
115 buf->buf[nbuf].size -= pad;
117 pg += __nr_pages;
118 offset += __nr_pages << PAGE_SHIFT;
121 return buf;
124 static void bts_buffer_free_aux(void *data)
126 kfree(data);
129 static unsigned long bts_buffer_offset(struct bts_buffer *buf, unsigned int idx)
131 return buf->buf[idx].offset + buf->buf[idx].displacement;
134 static void
135 bts_config_buffer(struct bts_buffer *buf)
137 int cpu = raw_smp_processor_id();
138 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
139 struct bts_phys *phys = &buf->buf[buf->cur_buf];
140 unsigned long index, thresh = 0, end = phys->size;
141 struct page *page = phys->page;
143 index = local_read(&buf->head);
145 if (!buf->snapshot) {
146 if (buf->end < phys->offset + buf_size(page))
147 end = buf->end - phys->offset - phys->displacement;
149 index -= phys->offset + phys->displacement;
151 if (end - index > BTS_SAFETY_MARGIN)
152 thresh = end - BTS_SAFETY_MARGIN;
153 else if (end - index > BTS_RECORD_SIZE)
154 thresh = end - BTS_RECORD_SIZE;
155 else
156 thresh = end;
159 ds->bts_buffer_base = (u64)(long)page_address(page) + phys->displacement;
160 ds->bts_index = ds->bts_buffer_base + index;
161 ds->bts_absolute_maximum = ds->bts_buffer_base + end;
162 ds->bts_interrupt_threshold = !buf->snapshot
163 ? ds->bts_buffer_base + thresh
164 : ds->bts_absolute_maximum + BTS_RECORD_SIZE;
167 static void bts_buffer_pad_out(struct bts_phys *phys, unsigned long head)
169 unsigned long index = head - phys->offset;
171 memset(page_address(phys->page) + index, 0, phys->size - index);
174 static bool bts_buffer_is_full(struct bts_buffer *buf, struct bts_ctx *bts)
176 if (buf->snapshot)
177 return false;
179 if (local_read(&buf->data_size) >= bts->handle.size ||
180 bts->handle.size - local_read(&buf->data_size) < BTS_RECORD_SIZE)
181 return true;
183 return false;
186 static void bts_update(struct bts_ctx *bts)
188 int cpu = raw_smp_processor_id();
189 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
190 struct bts_buffer *buf = perf_get_aux(&bts->handle);
191 unsigned long index = ds->bts_index - ds->bts_buffer_base, old, head;
193 if (!buf)
194 return;
196 head = index + bts_buffer_offset(buf, buf->cur_buf);
197 old = local_xchg(&buf->head, head);
199 if (!buf->snapshot) {
200 if (old == head)
201 return;
203 if (ds->bts_index >= ds->bts_absolute_maximum)
204 local_inc(&buf->lost);
207 * old and head are always in the same physical buffer, so we
208 * can subtract them to get the data size.
210 local_add(head - old, &buf->data_size);
211 } else {
212 local_set(&buf->data_size, head);
216 static void __bts_event_start(struct perf_event *event)
218 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
219 struct bts_buffer *buf = perf_get_aux(&bts->handle);
220 u64 config = 0;
222 if (!buf || bts_buffer_is_full(buf, bts))
223 return;
225 event->hw.itrace_started = 1;
226 event->hw.state = 0;
228 if (!buf->snapshot)
229 config |= ARCH_PERFMON_EVENTSEL_INT;
230 if (!event->attr.exclude_kernel)
231 config |= ARCH_PERFMON_EVENTSEL_OS;
232 if (!event->attr.exclude_user)
233 config |= ARCH_PERFMON_EVENTSEL_USR;
235 bts_config_buffer(buf);
238 * local barrier to make sure that ds configuration made it
239 * before we enable BTS
241 wmb();
243 intel_pmu_enable_bts(config);
246 static void bts_event_start(struct perf_event *event, int flags)
248 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
250 __bts_event_start(event);
252 /* PMI handler: this counter is running and likely generating PMIs */
253 ACCESS_ONCE(bts->started) = 1;
256 static void __bts_event_stop(struct perf_event *event)
259 * No extra synchronization is mandated by the documentation to have
260 * BTS data stores globally visible.
262 intel_pmu_disable_bts();
264 if (event->hw.state & PERF_HES_STOPPED)
265 return;
267 ACCESS_ONCE(event->hw.state) |= PERF_HES_STOPPED;
270 static void bts_event_stop(struct perf_event *event, int flags)
272 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
274 /* PMI handler: don't restart this counter */
275 ACCESS_ONCE(bts->started) = 0;
277 __bts_event_stop(event);
279 if (flags & PERF_EF_UPDATE)
280 bts_update(bts);
283 void intel_bts_enable_local(void)
285 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
287 if (bts->handle.event && bts->started)
288 __bts_event_start(bts->handle.event);
291 void intel_bts_disable_local(void)
293 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
295 if (bts->handle.event)
296 __bts_event_stop(bts->handle.event);
299 static int
300 bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle)
302 unsigned long head, space, next_space, pad, gap, skip, wakeup;
303 unsigned int next_buf;
304 struct bts_phys *phys, *next_phys;
305 int ret;
307 if (buf->snapshot)
308 return 0;
310 head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
311 if (WARN_ON_ONCE(head != local_read(&buf->head)))
312 return -EINVAL;
314 phys = &buf->buf[buf->cur_buf];
315 space = phys->offset + phys->displacement + phys->size - head;
316 pad = space;
317 if (space > handle->size) {
318 space = handle->size;
319 space -= space % BTS_RECORD_SIZE;
321 if (space <= BTS_SAFETY_MARGIN) {
322 /* See if next phys buffer has more space */
323 next_buf = buf->cur_buf + 1;
324 if (next_buf >= buf->nr_bufs)
325 next_buf = 0;
326 next_phys = &buf->buf[next_buf];
327 gap = buf_size(phys->page) - phys->displacement - phys->size +
328 next_phys->displacement;
329 skip = pad + gap;
330 if (handle->size >= skip) {
331 next_space = next_phys->size;
332 if (next_space + skip > handle->size) {
333 next_space = handle->size - skip;
334 next_space -= next_space % BTS_RECORD_SIZE;
336 if (next_space > space || !space) {
337 if (pad)
338 bts_buffer_pad_out(phys, head);
339 ret = perf_aux_output_skip(handle, skip);
340 if (ret)
341 return ret;
342 /* Advance to next phys buffer */
343 phys = next_phys;
344 space = next_space;
345 head = phys->offset + phys->displacement;
347 * After this, cur_buf and head won't match ds
348 * anymore, so we must not be racing with
349 * bts_update().
351 buf->cur_buf = next_buf;
352 local_set(&buf->head, head);
357 /* Don't go far beyond wakeup watermark */
358 wakeup = BTS_SAFETY_MARGIN + BTS_RECORD_SIZE + handle->wakeup -
359 handle->head;
360 if (space > wakeup) {
361 space = wakeup;
362 space -= space % BTS_RECORD_SIZE;
365 buf->end = head + space;
368 * If we have no space, the lost notification would have been sent when
369 * we hit absolute_maximum - see bts_update()
371 if (!space)
372 return -ENOSPC;
374 return 0;
377 int intel_bts_interrupt(void)
379 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
380 struct perf_event *event = bts->handle.event;
381 struct bts_buffer *buf;
382 s64 old_head;
383 int err;
385 if (!event || !bts->started)
386 return 0;
388 buf = perf_get_aux(&bts->handle);
390 * Skip snapshot counters: they don't use the interrupt, but
391 * there's no other way of telling, because the pointer will
392 * keep moving
394 if (!buf || buf->snapshot)
395 return 0;
397 old_head = local_read(&buf->head);
398 bts_update(bts);
400 /* no new data */
401 if (old_head == local_read(&buf->head))
402 return 0;
404 perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
405 !!local_xchg(&buf->lost, 0));
407 buf = perf_aux_output_begin(&bts->handle, event);
408 if (!buf)
409 return 1;
411 err = bts_buffer_reset(buf, &bts->handle);
412 if (err)
413 perf_aux_output_end(&bts->handle, 0, false);
415 return 1;
418 static void bts_event_del(struct perf_event *event, int mode)
420 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
421 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
422 struct bts_buffer *buf = perf_get_aux(&bts->handle);
424 bts_event_stop(event, PERF_EF_UPDATE);
426 if (buf) {
427 if (buf->snapshot)
428 bts->handle.head =
429 local_xchg(&buf->data_size,
430 buf->nr_pages << PAGE_SHIFT);
431 perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
432 !!local_xchg(&buf->lost, 0));
435 cpuc->ds->bts_index = bts->ds_back.bts_buffer_base;
436 cpuc->ds->bts_buffer_base = bts->ds_back.bts_buffer_base;
437 cpuc->ds->bts_absolute_maximum = bts->ds_back.bts_absolute_maximum;
438 cpuc->ds->bts_interrupt_threshold = bts->ds_back.bts_interrupt_threshold;
441 static int bts_event_add(struct perf_event *event, int mode)
443 struct bts_buffer *buf;
444 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
445 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
446 struct hw_perf_event *hwc = &event->hw;
447 int ret = -EBUSY;
449 event->hw.state = PERF_HES_STOPPED;
451 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
452 return -EBUSY;
454 if (bts->handle.event)
455 return -EBUSY;
457 buf = perf_aux_output_begin(&bts->handle, event);
458 if (!buf)
459 return -EINVAL;
461 ret = bts_buffer_reset(buf, &bts->handle);
462 if (ret) {
463 perf_aux_output_end(&bts->handle, 0, false);
464 return ret;
467 bts->ds_back.bts_buffer_base = cpuc->ds->bts_buffer_base;
468 bts->ds_back.bts_absolute_maximum = cpuc->ds->bts_absolute_maximum;
469 bts->ds_back.bts_interrupt_threshold = cpuc->ds->bts_interrupt_threshold;
471 if (mode & PERF_EF_START) {
472 bts_event_start(event, 0);
473 if (hwc->state & PERF_HES_STOPPED) {
474 bts_event_del(event, 0);
475 return -EBUSY;
479 return 0;
482 static void bts_event_destroy(struct perf_event *event)
484 x86_release_hardware();
485 x86_del_exclusive(x86_lbr_exclusive_bts);
488 static int bts_event_init(struct perf_event *event)
490 int ret;
492 if (event->attr.type != bts_pmu.type)
493 return -ENOENT;
495 if (x86_add_exclusive(x86_lbr_exclusive_bts))
496 return -EBUSY;
499 * BTS leaks kernel addresses even when CPL0 tracing is
500 * disabled, so disallow intel_bts driver for unprivileged
501 * users on paranoid systems since it provides trace data
502 * to the user in a zero-copy fashion.
504 * Note that the default paranoia setting permits unprivileged
505 * users to profile the kernel.
507 if (event->attr.exclude_kernel && perf_paranoid_kernel() &&
508 !capable(CAP_SYS_ADMIN))
509 return -EACCES;
511 ret = x86_reserve_hardware();
512 if (ret) {
513 x86_del_exclusive(x86_lbr_exclusive_bts);
514 return ret;
517 event->destroy = bts_event_destroy;
519 return 0;
522 static void bts_event_read(struct perf_event *event)
526 static __init int bts_init(void)
528 if (!boot_cpu_has(X86_FEATURE_DTES64) || !x86_pmu.bts)
529 return -ENODEV;
531 bts_pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_ITRACE;
532 bts_pmu.task_ctx_nr = perf_sw_context;
533 bts_pmu.event_init = bts_event_init;
534 bts_pmu.add = bts_event_add;
535 bts_pmu.del = bts_event_del;
536 bts_pmu.start = bts_event_start;
537 bts_pmu.stop = bts_event_stop;
538 bts_pmu.read = bts_event_read;
539 bts_pmu.setup_aux = bts_buffer_setup_aux;
540 bts_pmu.free_aux = bts_buffer_free_aux;
542 return perf_pmu_register(&bts_pmu, "intel_bts", -1);
544 arch_initcall(bts_init);