1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * BTS PMU driver for perf
4*4882a593Smuzhiyun * Copyright (c) 2013-2014, Intel Corporation.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #undef DEBUG
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/bitops.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/debugfs.h>
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/coredump.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/sizes.h>
19*4882a593Smuzhiyun #include <asm/perf_event.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "../perf_event.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct bts_ctx {
24*4882a593Smuzhiyun struct perf_output_handle handle;
25*4882a593Smuzhiyun struct debug_store ds_back;
26*4882a593Smuzhiyun int state;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* BTS context states: */
30*4882a593Smuzhiyun enum {
31*4882a593Smuzhiyun /* no ongoing AUX transactions */
32*4882a593Smuzhiyun BTS_STATE_STOPPED = 0,
33*4882a593Smuzhiyun /* AUX transaction is on, BTS tracing is disabled */
34*4882a593Smuzhiyun BTS_STATE_INACTIVE,
35*4882a593Smuzhiyun /* AUX transaction is on, BTS tracing is running */
36*4882a593Smuzhiyun BTS_STATE_ACTIVE,
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static DEFINE_PER_CPU(struct bts_ctx, bts_ctx);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define BTS_RECORD_SIZE 24
42*4882a593Smuzhiyun #define BTS_SAFETY_MARGIN 4080
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct bts_phys {
45*4882a593Smuzhiyun struct page *page;
46*4882a593Smuzhiyun unsigned long size;
47*4882a593Smuzhiyun unsigned long offset;
48*4882a593Smuzhiyun unsigned long displacement;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct bts_buffer {
52*4882a593Smuzhiyun size_t real_size; /* multiple of BTS_RECORD_SIZE */
53*4882a593Smuzhiyun unsigned int nr_pages;
54*4882a593Smuzhiyun unsigned int nr_bufs;
55*4882a593Smuzhiyun unsigned int cur_buf;
56*4882a593Smuzhiyun bool snapshot;
57*4882a593Smuzhiyun local_t data_size;
58*4882a593Smuzhiyun local_t head;
59*4882a593Smuzhiyun unsigned long end;
60*4882a593Smuzhiyun void **data_pages;
61*4882a593Smuzhiyun struct bts_phys buf[];
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun static struct pmu bts_pmu;
65*4882a593Smuzhiyun
buf_nr_pages(struct page * page)66*4882a593Smuzhiyun static int buf_nr_pages(struct page *page)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun if (!PagePrivate(page))
69*4882a593Smuzhiyun return 1;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return 1 << page_private(page);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
buf_size(struct page * page)74*4882a593Smuzhiyun static size_t buf_size(struct page *page)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun return buf_nr_pages(page) * PAGE_SIZE;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static void *
bts_buffer_setup_aux(struct perf_event * event,void ** pages,int nr_pages,bool overwrite)80*4882a593Smuzhiyun bts_buffer_setup_aux(struct perf_event *event, void **pages,
81*4882a593Smuzhiyun int nr_pages, bool overwrite)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct bts_buffer *buf;
84*4882a593Smuzhiyun struct page *page;
85*4882a593Smuzhiyun int cpu = event->cpu;
86*4882a593Smuzhiyun int node = (cpu == -1) ? cpu : cpu_to_node(cpu);
87*4882a593Smuzhiyun unsigned long offset;
88*4882a593Smuzhiyun size_t size = nr_pages << PAGE_SHIFT;
89*4882a593Smuzhiyun int pg, nbuf, pad;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* count all the high order buffers */
92*4882a593Smuzhiyun for (pg = 0, nbuf = 0; pg < nr_pages;) {
93*4882a593Smuzhiyun page = virt_to_page(pages[pg]);
94*4882a593Smuzhiyun pg += buf_nr_pages(page);
95*4882a593Smuzhiyun nbuf++;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * to avoid interrupts in overwrite mode, only allow one physical
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun if (overwrite && nbuf > 1)
102*4882a593Smuzhiyun return NULL;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun buf = kzalloc_node(offsetof(struct bts_buffer, buf[nbuf]), GFP_KERNEL, node);
105*4882a593Smuzhiyun if (!buf)
106*4882a593Smuzhiyun return NULL;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun buf->nr_pages = nr_pages;
109*4882a593Smuzhiyun buf->nr_bufs = nbuf;
110*4882a593Smuzhiyun buf->snapshot = overwrite;
111*4882a593Smuzhiyun buf->data_pages = pages;
112*4882a593Smuzhiyun buf->real_size = size - size % BTS_RECORD_SIZE;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun for (pg = 0, nbuf = 0, offset = 0, pad = 0; nbuf < buf->nr_bufs; nbuf++) {
115*4882a593Smuzhiyun unsigned int __nr_pages;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun page = virt_to_page(pages[pg]);
118*4882a593Smuzhiyun __nr_pages = buf_nr_pages(page);
119*4882a593Smuzhiyun buf->buf[nbuf].page = page;
120*4882a593Smuzhiyun buf->buf[nbuf].offset = offset;
121*4882a593Smuzhiyun buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
122*4882a593Smuzhiyun buf->buf[nbuf].size = buf_size(page) - buf->buf[nbuf].displacement;
123*4882a593Smuzhiyun pad = buf->buf[nbuf].size % BTS_RECORD_SIZE;
124*4882a593Smuzhiyun buf->buf[nbuf].size -= pad;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun pg += __nr_pages;
127*4882a593Smuzhiyun offset += __nr_pages << PAGE_SHIFT;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return buf;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
bts_buffer_free_aux(void * data)133*4882a593Smuzhiyun static void bts_buffer_free_aux(void *data)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun kfree(data);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
bts_buffer_offset(struct bts_buffer * buf,unsigned int idx)138*4882a593Smuzhiyun static unsigned long bts_buffer_offset(struct bts_buffer *buf, unsigned int idx)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun return buf->buf[idx].offset + buf->buf[idx].displacement;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static void
bts_config_buffer(struct bts_buffer * buf)144*4882a593Smuzhiyun bts_config_buffer(struct bts_buffer *buf)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun int cpu = raw_smp_processor_id();
147*4882a593Smuzhiyun struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
148*4882a593Smuzhiyun struct bts_phys *phys = &buf->buf[buf->cur_buf];
149*4882a593Smuzhiyun unsigned long index, thresh = 0, end = phys->size;
150*4882a593Smuzhiyun struct page *page = phys->page;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun index = local_read(&buf->head);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (!buf->snapshot) {
155*4882a593Smuzhiyun if (buf->end < phys->offset + buf_size(page))
156*4882a593Smuzhiyun end = buf->end - phys->offset - phys->displacement;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun index -= phys->offset + phys->displacement;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun if (end - index > BTS_SAFETY_MARGIN)
161*4882a593Smuzhiyun thresh = end - BTS_SAFETY_MARGIN;
162*4882a593Smuzhiyun else if (end - index > BTS_RECORD_SIZE)
163*4882a593Smuzhiyun thresh = end - BTS_RECORD_SIZE;
164*4882a593Smuzhiyun else
165*4882a593Smuzhiyun thresh = end;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun ds->bts_buffer_base = (u64)(long)page_address(page) + phys->displacement;
169*4882a593Smuzhiyun ds->bts_index = ds->bts_buffer_base + index;
170*4882a593Smuzhiyun ds->bts_absolute_maximum = ds->bts_buffer_base + end;
171*4882a593Smuzhiyun ds->bts_interrupt_threshold = !buf->snapshot
172*4882a593Smuzhiyun ? ds->bts_buffer_base + thresh
173*4882a593Smuzhiyun : ds->bts_absolute_maximum + BTS_RECORD_SIZE;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
bts_buffer_pad_out(struct bts_phys * phys,unsigned long head)176*4882a593Smuzhiyun static void bts_buffer_pad_out(struct bts_phys *phys, unsigned long head)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun unsigned long index = head - phys->offset;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun memset(page_address(phys->page) + index, 0, phys->size - index);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
bts_update(struct bts_ctx * bts)183*4882a593Smuzhiyun static void bts_update(struct bts_ctx *bts)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun int cpu = raw_smp_processor_id();
186*4882a593Smuzhiyun struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
187*4882a593Smuzhiyun struct bts_buffer *buf = perf_get_aux(&bts->handle);
188*4882a593Smuzhiyun unsigned long index = ds->bts_index - ds->bts_buffer_base, old, head;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if (!buf)
191*4882a593Smuzhiyun return;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun head = index + bts_buffer_offset(buf, buf->cur_buf);
194*4882a593Smuzhiyun old = local_xchg(&buf->head, head);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (!buf->snapshot) {
197*4882a593Smuzhiyun if (old == head)
198*4882a593Smuzhiyun return;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun if (ds->bts_index >= ds->bts_absolute_maximum)
201*4882a593Smuzhiyun perf_aux_output_flag(&bts->handle,
202*4882a593Smuzhiyun PERF_AUX_FLAG_TRUNCATED);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun * old and head are always in the same physical buffer, so we
206*4882a593Smuzhiyun * can subtract them to get the data size.
207*4882a593Smuzhiyun */
208*4882a593Smuzhiyun local_add(head - old, &buf->data_size);
209*4882a593Smuzhiyun } else {
210*4882a593Smuzhiyun local_set(&buf->data_size, head);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun static int
215*4882a593Smuzhiyun bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun * Ordering PMU callbacks wrt themselves and the PMI is done by means
219*4882a593Smuzhiyun * of bts::state, which:
220*4882a593Smuzhiyun * - is set when bts::handle::event is valid, that is, between
221*4882a593Smuzhiyun * perf_aux_output_begin() and perf_aux_output_end();
222*4882a593Smuzhiyun * - is zero otherwise;
223*4882a593Smuzhiyun * - is ordered against bts::handle::event with a compiler barrier.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun
__bts_event_start(struct perf_event * event)226*4882a593Smuzhiyun static void __bts_event_start(struct perf_event *event)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
229*4882a593Smuzhiyun struct bts_buffer *buf = perf_get_aux(&bts->handle);
230*4882a593Smuzhiyun u64 config = 0;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if (!buf->snapshot)
233*4882a593Smuzhiyun config |= ARCH_PERFMON_EVENTSEL_INT;
234*4882a593Smuzhiyun if (!event->attr.exclude_kernel)
235*4882a593Smuzhiyun config |= ARCH_PERFMON_EVENTSEL_OS;
236*4882a593Smuzhiyun if (!event->attr.exclude_user)
237*4882a593Smuzhiyun config |= ARCH_PERFMON_EVENTSEL_USR;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun bts_config_buffer(buf);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * local barrier to make sure that ds configuration made it
243*4882a593Smuzhiyun * before we enable BTS and bts::state goes ACTIVE
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun wmb();
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* INACTIVE/STOPPED -> ACTIVE */
248*4882a593Smuzhiyun WRITE_ONCE(bts->state, BTS_STATE_ACTIVE);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun intel_pmu_enable_bts(config);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
bts_event_start(struct perf_event * event,int flags)254*4882a593Smuzhiyun static void bts_event_start(struct perf_event *event, int flags)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
257*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
258*4882a593Smuzhiyun struct bts_buffer *buf;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun buf = perf_aux_output_begin(&bts->handle, event);
261*4882a593Smuzhiyun if (!buf)
262*4882a593Smuzhiyun goto fail_stop;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun if (bts_buffer_reset(buf, &bts->handle))
265*4882a593Smuzhiyun goto fail_end_stop;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun bts->ds_back.bts_buffer_base = cpuc->ds->bts_buffer_base;
268*4882a593Smuzhiyun bts->ds_back.bts_absolute_maximum = cpuc->ds->bts_absolute_maximum;
269*4882a593Smuzhiyun bts->ds_back.bts_interrupt_threshold = cpuc->ds->bts_interrupt_threshold;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun perf_event_itrace_started(event);
272*4882a593Smuzhiyun event->hw.state = 0;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun __bts_event_start(event);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun return;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun fail_end_stop:
279*4882a593Smuzhiyun perf_aux_output_end(&bts->handle, 0);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun fail_stop:
282*4882a593Smuzhiyun event->hw.state = PERF_HES_STOPPED;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
__bts_event_stop(struct perf_event * event,int state)285*4882a593Smuzhiyun static void __bts_event_stop(struct perf_event *event, int state)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* ACTIVE -> INACTIVE(PMI)/STOPPED(->stop()) */
290*4882a593Smuzhiyun WRITE_ONCE(bts->state, state);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /*
293*4882a593Smuzhiyun * No extra synchronization is mandated by the documentation to have
294*4882a593Smuzhiyun * BTS data stores globally visible.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun intel_pmu_disable_bts();
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
bts_event_stop(struct perf_event * event,int flags)299*4882a593Smuzhiyun static void bts_event_stop(struct perf_event *event, int flags)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
302*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
303*4882a593Smuzhiyun struct bts_buffer *buf = NULL;
304*4882a593Smuzhiyun int state = READ_ONCE(bts->state);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (state == BTS_STATE_ACTIVE)
307*4882a593Smuzhiyun __bts_event_stop(event, BTS_STATE_STOPPED);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun if (state != BTS_STATE_STOPPED)
310*4882a593Smuzhiyun buf = perf_get_aux(&bts->handle);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun event->hw.state |= PERF_HES_STOPPED;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun if (flags & PERF_EF_UPDATE) {
315*4882a593Smuzhiyun bts_update(bts);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (buf) {
318*4882a593Smuzhiyun if (buf->snapshot)
319*4882a593Smuzhiyun bts->handle.head =
320*4882a593Smuzhiyun local_xchg(&buf->data_size,
321*4882a593Smuzhiyun buf->nr_pages << PAGE_SHIFT);
322*4882a593Smuzhiyun perf_aux_output_end(&bts->handle,
323*4882a593Smuzhiyun local_xchg(&buf->data_size, 0));
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun cpuc->ds->bts_index = bts->ds_back.bts_buffer_base;
327*4882a593Smuzhiyun cpuc->ds->bts_buffer_base = bts->ds_back.bts_buffer_base;
328*4882a593Smuzhiyun cpuc->ds->bts_absolute_maximum = bts->ds_back.bts_absolute_maximum;
329*4882a593Smuzhiyun cpuc->ds->bts_interrupt_threshold = bts->ds_back.bts_interrupt_threshold;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
intel_bts_enable_local(void)333*4882a593Smuzhiyun void intel_bts_enable_local(void)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
336*4882a593Smuzhiyun int state = READ_ONCE(bts->state);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun * Here we transition from INACTIVE to ACTIVE;
340*4882a593Smuzhiyun * if we instead are STOPPED from the interrupt handler,
341*4882a593Smuzhiyun * stay that way. Can't be ACTIVE here though.
342*4882a593Smuzhiyun */
343*4882a593Smuzhiyun if (WARN_ON_ONCE(state == BTS_STATE_ACTIVE))
344*4882a593Smuzhiyun return;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun if (state == BTS_STATE_STOPPED)
347*4882a593Smuzhiyun return;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun if (bts->handle.event)
350*4882a593Smuzhiyun __bts_event_start(bts->handle.event);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
intel_bts_disable_local(void)353*4882a593Smuzhiyun void intel_bts_disable_local(void)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun * Here we transition from ACTIVE to INACTIVE;
359*4882a593Smuzhiyun * do nothing for STOPPED or INACTIVE.
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun if (READ_ONCE(bts->state) != BTS_STATE_ACTIVE)
362*4882a593Smuzhiyun return;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (bts->handle.event)
365*4882a593Smuzhiyun __bts_event_stop(bts->handle.event, BTS_STATE_INACTIVE);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun static int
bts_buffer_reset(struct bts_buffer * buf,struct perf_output_handle * handle)369*4882a593Smuzhiyun bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun unsigned long head, space, next_space, pad, gap, skip, wakeup;
372*4882a593Smuzhiyun unsigned int next_buf;
373*4882a593Smuzhiyun struct bts_phys *phys, *next_phys;
374*4882a593Smuzhiyun int ret;
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun if (buf->snapshot)
377*4882a593Smuzhiyun return 0;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun phys = &buf->buf[buf->cur_buf];
382*4882a593Smuzhiyun space = phys->offset + phys->displacement + phys->size - head;
383*4882a593Smuzhiyun pad = space;
384*4882a593Smuzhiyun if (space > handle->size) {
385*4882a593Smuzhiyun space = handle->size;
386*4882a593Smuzhiyun space -= space % BTS_RECORD_SIZE;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun if (space <= BTS_SAFETY_MARGIN) {
389*4882a593Smuzhiyun /* See if next phys buffer has more space */
390*4882a593Smuzhiyun next_buf = buf->cur_buf + 1;
391*4882a593Smuzhiyun if (next_buf >= buf->nr_bufs)
392*4882a593Smuzhiyun next_buf = 0;
393*4882a593Smuzhiyun next_phys = &buf->buf[next_buf];
394*4882a593Smuzhiyun gap = buf_size(phys->page) - phys->displacement - phys->size +
395*4882a593Smuzhiyun next_phys->displacement;
396*4882a593Smuzhiyun skip = pad + gap;
397*4882a593Smuzhiyun if (handle->size >= skip) {
398*4882a593Smuzhiyun next_space = next_phys->size;
399*4882a593Smuzhiyun if (next_space + skip > handle->size) {
400*4882a593Smuzhiyun next_space = handle->size - skip;
401*4882a593Smuzhiyun next_space -= next_space % BTS_RECORD_SIZE;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun if (next_space > space || !space) {
404*4882a593Smuzhiyun if (pad)
405*4882a593Smuzhiyun bts_buffer_pad_out(phys, head);
406*4882a593Smuzhiyun ret = perf_aux_output_skip(handle, skip);
407*4882a593Smuzhiyun if (ret)
408*4882a593Smuzhiyun return ret;
409*4882a593Smuzhiyun /* Advance to next phys buffer */
410*4882a593Smuzhiyun phys = next_phys;
411*4882a593Smuzhiyun space = next_space;
412*4882a593Smuzhiyun head = phys->offset + phys->displacement;
413*4882a593Smuzhiyun /*
414*4882a593Smuzhiyun * After this, cur_buf and head won't match ds
415*4882a593Smuzhiyun * anymore, so we must not be racing with
416*4882a593Smuzhiyun * bts_update().
417*4882a593Smuzhiyun */
418*4882a593Smuzhiyun buf->cur_buf = next_buf;
419*4882a593Smuzhiyun local_set(&buf->head, head);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /* Don't go far beyond wakeup watermark */
425*4882a593Smuzhiyun wakeup = BTS_SAFETY_MARGIN + BTS_RECORD_SIZE + handle->wakeup -
426*4882a593Smuzhiyun handle->head;
427*4882a593Smuzhiyun if (space > wakeup) {
428*4882a593Smuzhiyun space = wakeup;
429*4882a593Smuzhiyun space -= space % BTS_RECORD_SIZE;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun buf->end = head + space;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun /*
435*4882a593Smuzhiyun * If we have no space, the lost notification would have been sent when
436*4882a593Smuzhiyun * we hit absolute_maximum - see bts_update()
437*4882a593Smuzhiyun */
438*4882a593Smuzhiyun if (!space)
439*4882a593Smuzhiyun return -ENOSPC;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun return 0;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
intel_bts_interrupt(void)444*4882a593Smuzhiyun int intel_bts_interrupt(void)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun struct debug_store *ds = this_cpu_ptr(&cpu_hw_events)->ds;
447*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
448*4882a593Smuzhiyun struct perf_event *event = bts->handle.event;
449*4882a593Smuzhiyun struct bts_buffer *buf;
450*4882a593Smuzhiyun s64 old_head;
451*4882a593Smuzhiyun int err = -ENOSPC, handled = 0;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /*
454*4882a593Smuzhiyun * The only surefire way of knowing if this NMI is ours is by checking
455*4882a593Smuzhiyun * the write ptr against the PMI threshold.
456*4882a593Smuzhiyun */
457*4882a593Smuzhiyun if (ds && (ds->bts_index >= ds->bts_interrupt_threshold))
458*4882a593Smuzhiyun handled = 1;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /*
461*4882a593Smuzhiyun * this is wrapped in intel_bts_enable_local/intel_bts_disable_local,
462*4882a593Smuzhiyun * so we can only be INACTIVE or STOPPED
463*4882a593Smuzhiyun */
464*4882a593Smuzhiyun if (READ_ONCE(bts->state) == BTS_STATE_STOPPED)
465*4882a593Smuzhiyun return handled;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun buf = perf_get_aux(&bts->handle);
468*4882a593Smuzhiyun if (!buf)
469*4882a593Smuzhiyun return handled;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /*
472*4882a593Smuzhiyun * Skip snapshot counters: they don't use the interrupt, but
473*4882a593Smuzhiyun * there's no other way of telling, because the pointer will
474*4882a593Smuzhiyun * keep moving
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun if (buf->snapshot)
477*4882a593Smuzhiyun return 0;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun old_head = local_read(&buf->head);
480*4882a593Smuzhiyun bts_update(bts);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /* no new data */
483*4882a593Smuzhiyun if (old_head == local_read(&buf->head))
484*4882a593Smuzhiyun return handled;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0));
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun buf = perf_aux_output_begin(&bts->handle, event);
489*4882a593Smuzhiyun if (buf)
490*4882a593Smuzhiyun err = bts_buffer_reset(buf, &bts->handle);
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun if (err) {
493*4882a593Smuzhiyun WRITE_ONCE(bts->state, BTS_STATE_STOPPED);
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun if (buf) {
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun * BTS_STATE_STOPPED should be visible before
498*4882a593Smuzhiyun * cleared handle::event
499*4882a593Smuzhiyun */
500*4882a593Smuzhiyun barrier();
501*4882a593Smuzhiyun perf_aux_output_end(&bts->handle, 0);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun return 1;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
bts_event_del(struct perf_event * event,int mode)508*4882a593Smuzhiyun static void bts_event_del(struct perf_event *event, int mode)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun bts_event_stop(event, PERF_EF_UPDATE);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun
bts_event_add(struct perf_event * event,int mode)513*4882a593Smuzhiyun static int bts_event_add(struct perf_event *event, int mode)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
516*4882a593Smuzhiyun struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
517*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun event->hw.state = PERF_HES_STOPPED;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
522*4882a593Smuzhiyun return -EBUSY;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun if (bts->handle.event)
525*4882a593Smuzhiyun return -EBUSY;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun if (mode & PERF_EF_START) {
528*4882a593Smuzhiyun bts_event_start(event, 0);
529*4882a593Smuzhiyun if (hwc->state & PERF_HES_STOPPED)
530*4882a593Smuzhiyun return -EINVAL;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
bts_event_destroy(struct perf_event * event)536*4882a593Smuzhiyun static void bts_event_destroy(struct perf_event *event)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun x86_release_hardware();
539*4882a593Smuzhiyun x86_del_exclusive(x86_lbr_exclusive_bts);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
bts_event_init(struct perf_event * event)542*4882a593Smuzhiyun static int bts_event_init(struct perf_event *event)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun int ret;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun if (event->attr.type != bts_pmu.type)
547*4882a593Smuzhiyun return -ENOENT;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /*
550*4882a593Smuzhiyun * BTS leaks kernel addresses even when CPL0 tracing is
551*4882a593Smuzhiyun * disabled, so disallow intel_bts driver for unprivileged
552*4882a593Smuzhiyun * users on paranoid systems since it provides trace data
553*4882a593Smuzhiyun * to the user in a zero-copy fashion.
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * Note that the default paranoia setting permits unprivileged
556*4882a593Smuzhiyun * users to profile the kernel.
557*4882a593Smuzhiyun */
558*4882a593Smuzhiyun if (event->attr.exclude_kernel) {
559*4882a593Smuzhiyun ret = perf_allow_kernel(&event->attr);
560*4882a593Smuzhiyun if (ret)
561*4882a593Smuzhiyun return ret;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun if (x86_add_exclusive(x86_lbr_exclusive_bts))
565*4882a593Smuzhiyun return -EBUSY;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun ret = x86_reserve_hardware();
568*4882a593Smuzhiyun if (ret) {
569*4882a593Smuzhiyun x86_del_exclusive(x86_lbr_exclusive_bts);
570*4882a593Smuzhiyun return ret;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun event->destroy = bts_event_destroy;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun return 0;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
bts_event_read(struct perf_event * event)578*4882a593Smuzhiyun static void bts_event_read(struct perf_event *event)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
bts_init(void)582*4882a593Smuzhiyun static __init int bts_init(void)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun if (!boot_cpu_has(X86_FEATURE_DTES64) || !x86_pmu.bts)
585*4882a593Smuzhiyun return -ENODEV;
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun if (boot_cpu_has(X86_FEATURE_PTI)) {
588*4882a593Smuzhiyun /*
589*4882a593Smuzhiyun * BTS hardware writes through a virtual memory map we must
590*4882a593Smuzhiyun * either use the kernel physical map, or the user mapping of
591*4882a593Smuzhiyun * the AUX buffer.
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * However, since this driver supports per-CPU and per-task inherit
594*4882a593Smuzhiyun * we cannot use the user mapping since it will not be available
595*4882a593Smuzhiyun * if we're not running the owning process.
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun * With PTI we can't use the kernal map either, because its not
598*4882a593Smuzhiyun * there when we run userspace.
599*4882a593Smuzhiyun *
600*4882a593Smuzhiyun * For now, disable this driver when using PTI.
601*4882a593Smuzhiyun */
602*4882a593Smuzhiyun return -ENODEV;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun bts_pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_ITRACE |
606*4882a593Smuzhiyun PERF_PMU_CAP_EXCLUSIVE;
607*4882a593Smuzhiyun bts_pmu.task_ctx_nr = perf_sw_context;
608*4882a593Smuzhiyun bts_pmu.event_init = bts_event_init;
609*4882a593Smuzhiyun bts_pmu.add = bts_event_add;
610*4882a593Smuzhiyun bts_pmu.del = bts_event_del;
611*4882a593Smuzhiyun bts_pmu.start = bts_event_start;
612*4882a593Smuzhiyun bts_pmu.stop = bts_event_stop;
613*4882a593Smuzhiyun bts_pmu.read = bts_event_read;
614*4882a593Smuzhiyun bts_pmu.setup_aux = bts_buffer_setup_aux;
615*4882a593Smuzhiyun bts_pmu.free_aux = bts_buffer_free_aux;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun return perf_pmu_register(&bts_pmu, "intel_bts", -1);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun arch_initcall(bts_init);
620