blob: e5bfafef3d771f067ae0b4351c0a57e74090b08f [file] [log] [blame]
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001/*
2 * Intel(R) Processor Trace PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
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.
8 *
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.
13 *
14 * Intel PT is specified in the Intel Architecture Instruction Set Extensions
15 * Programming Reference:
16 * http://software.intel.com/en-us/intel-isa-extensions
17 */
18
19#undef DEBUG
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/types.h>
24#include <linux/slab.h>
25#include <linux/device.h>
26
27#include <asm/perf_event.h>
28#include <asm/insn.h>
29#include <asm/io.h>
Takao Indoh24cc12b2015-11-04 14:22:32 +090030#include <asm/intel_pt.h>
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020031
Borislav Petkov27f6d222016-02-10 10:55:23 +010032#include "../perf_event.h"
Borislav Petkovfd1c6012016-02-10 10:55:13 +010033#include "pt.h"
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020034
35static DEFINE_PER_CPU(struct pt, pt_ctx);
36
37static struct pt_pmu pt_pmu;
38
39enum cpuid_regs {
40 CR_EAX = 0,
41 CR_ECX,
42 CR_EDX,
43 CR_EBX
44};
45
46/*
47 * Capabilities of Intel PT hardware, such as number of address bits or
48 * supported output schemes, are cached and exported to userspace as "caps"
49 * attribute group of pt pmu device
50 * (/sys/bus/event_source/devices/intel_pt/caps/) so that userspace can store
51 * relevant bits together with intel_pt traces.
52 *
53 * These are necessary for both trace decoding (payloads_lip, contains address
54 * width encoded in IP-related packets), and event configuration (bitmasks with
55 * permitted values for certain bit fields).
56 */
57#define PT_CAP(_n, _l, _r, _m) \
58 [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
59 .reg = _r, .mask = _m }
60
61static struct pt_cap_desc {
62 const char *name;
63 u32 leaf;
64 u8 reg;
65 u32 mask;
66} pt_caps[] = {
67 PT_CAP(max_subleaf, 0, CR_EAX, 0xffffffff),
68 PT_CAP(cr3_filtering, 0, CR_EBX, BIT(0)),
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +030069 PT_CAP(psb_cyc, 0, CR_EBX, BIT(1)),
Alexander Shishkinf127fa02016-04-27 18:44:44 +030070 PT_CAP(ip_filtering, 0, CR_EBX, BIT(2)),
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +030071 PT_CAP(mtc, 0, CR_EBX, BIT(3)),
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020072 PT_CAP(topa_output, 0, CR_ECX, BIT(0)),
73 PT_CAP(topa_multiple_entries, 0, CR_ECX, BIT(1)),
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +030074 PT_CAP(single_range_output, 0, CR_ECX, BIT(2)),
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020075 PT_CAP(payloads_lip, 0, CR_ECX, BIT(31)),
Alexander Shishkinf127fa02016-04-27 18:44:44 +030076 PT_CAP(num_address_ranges, 1, CR_EAX, 0x3),
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +030077 PT_CAP(mtc_periods, 1, CR_EAX, 0xffff0000),
78 PT_CAP(cycle_thresholds, 1, CR_EBX, 0xffff),
79 PT_CAP(psb_periods, 1, CR_EBX, 0xffff0000),
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020080};
81
82static u32 pt_cap_get(enum pt_capabilities cap)
83{
84 struct pt_cap_desc *cd = &pt_caps[cap];
Takao Indoh709bc872015-08-04 18:36:55 +090085 u32 c = pt_pmu.caps[cd->leaf * PT_CPUID_REGS_NUM + cd->reg];
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020086 unsigned int shift = __ffs(cd->mask);
87
88 return (c & cd->mask) >> shift;
89}
90
91static ssize_t pt_cap_show(struct device *cdev,
92 struct device_attribute *attr,
93 char *buf)
94{
95 struct dev_ext_attribute *ea =
96 container_of(attr, struct dev_ext_attribute, attr);
97 enum pt_capabilities cap = (long)ea->var;
98
99 return snprintf(buf, PAGE_SIZE, "%x\n", pt_cap_get(cap));
100}
101
102static struct attribute_group pt_cap_group = {
103 .name = "caps",
104};
105
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300106PMU_FORMAT_ATTR(cyc, "config:1" );
107PMU_FORMAT_ATTR(mtc, "config:9" );
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200108PMU_FORMAT_ATTR(tsc, "config:10" );
109PMU_FORMAT_ATTR(noretcomp, "config:11" );
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300110PMU_FORMAT_ATTR(mtc_period, "config:14-17" );
111PMU_FORMAT_ATTR(cyc_thresh, "config:19-22" );
112PMU_FORMAT_ATTR(psb_period, "config:24-27" );
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200113
114static struct attribute *pt_formats_attr[] = {
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300115 &format_attr_cyc.attr,
116 &format_attr_mtc.attr,
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200117 &format_attr_tsc.attr,
118 &format_attr_noretcomp.attr,
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300119 &format_attr_mtc_period.attr,
120 &format_attr_cyc_thresh.attr,
121 &format_attr_psb_period.attr,
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200122 NULL,
123};
124
125static struct attribute_group pt_format_group = {
126 .name = "format",
127 .attrs = pt_formats_attr,
128};
129
130static const struct attribute_group *pt_attr_groups[] = {
131 &pt_cap_group,
132 &pt_format_group,
133 NULL,
134};
135
136static int __init pt_pmu_hw_init(void)
137{
138 struct dev_ext_attribute *de_attrs;
139 struct attribute **attrs;
140 size_t size;
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300141 u64 reg;
Ingo Molnar066450b2015-04-12 11:11:21 +0200142 int ret;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200143 long i;
144
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300145 if (boot_cpu_has(X86_FEATURE_VMX)) {
146 /*
147 * Intel SDM, 36.5 "Tracing post-VMXON" says that
148 * "IA32_VMX_MISC[bit 14]" being 1 means PT can trace
149 * post-VMXON.
150 */
151 rdmsrl(MSR_IA32_VMX_MISC, reg);
152 if (reg & BIT(14))
153 pt_pmu.vmx = true;
154 }
155
Ingo Molnar066450b2015-04-12 11:11:21 +0200156 attrs = NULL;
Ingo Molnar066450b2015-04-12 11:11:21 +0200157
158 for (i = 0; i < PT_CPUID_LEAVES; i++) {
159 cpuid_count(20, i,
Takao Indoh709bc872015-08-04 18:36:55 +0900160 &pt_pmu.caps[CR_EAX + i*PT_CPUID_REGS_NUM],
161 &pt_pmu.caps[CR_EBX + i*PT_CPUID_REGS_NUM],
162 &pt_pmu.caps[CR_ECX + i*PT_CPUID_REGS_NUM],
163 &pt_pmu.caps[CR_EDX + i*PT_CPUID_REGS_NUM]);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200164 }
165
Ingo Molnar066450b2015-04-12 11:11:21 +0200166 ret = -ENOMEM;
167 size = sizeof(struct attribute *) * (ARRAY_SIZE(pt_caps)+1);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200168 attrs = kzalloc(size, GFP_KERNEL);
169 if (!attrs)
Ingo Molnar066450b2015-04-12 11:11:21 +0200170 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200171
Ingo Molnar066450b2015-04-12 11:11:21 +0200172 size = sizeof(struct dev_ext_attribute) * (ARRAY_SIZE(pt_caps)+1);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200173 de_attrs = kzalloc(size, GFP_KERNEL);
174 if (!de_attrs)
Ingo Molnar066450b2015-04-12 11:11:21 +0200175 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200176
177 for (i = 0; i < ARRAY_SIZE(pt_caps); i++) {
Ingo Molnar066450b2015-04-12 11:11:21 +0200178 struct dev_ext_attribute *de_attr = de_attrs + i;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200179
Ingo Molnar066450b2015-04-12 11:11:21 +0200180 de_attr->attr.attr.name = pt_caps[i].name;
181
Alexander Shishkinb44a2b52015-06-04 16:31:47 +0300182 sysfs_attr_init(&de_attr->attr.attr);
Ingo Molnar066450b2015-04-12 11:11:21 +0200183
184 de_attr->attr.attr.mode = S_IRUGO;
185 de_attr->attr.show = pt_cap_show;
186 de_attr->var = (void *)i;
187
188 attrs[i] = &de_attr->attr.attr;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200189 }
190
191 pt_cap_group.attrs = attrs;
Ingo Molnar066450b2015-04-12 11:11:21 +0200192
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200193 return 0;
194
Ingo Molnar066450b2015-04-12 11:11:21 +0200195fail:
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200196 kfree(attrs);
197
Ingo Molnar066450b2015-04-12 11:11:21 +0200198 return ret;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200199}
200
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300201#define RTIT_CTL_CYC_PSB (RTIT_CTL_CYCLEACC | \
202 RTIT_CTL_CYC_THRESH | \
203 RTIT_CTL_PSB_FREQ)
204
205#define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
206 RTIT_CTL_MTC_RANGE)
207
208#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN | \
209 RTIT_CTL_DISRETC | \
210 RTIT_CTL_CYC_PSB | \
211 RTIT_CTL_MTC)
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200212
213static bool pt_event_valid(struct perf_event *event)
214{
215 u64 config = event->attr.config;
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300216 u64 allowed, requested;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200217
218 if ((config & PT_CONFIG_MASK) != config)
219 return false;
220
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300221 if (config & RTIT_CTL_CYC_PSB) {
222 if (!pt_cap_get(PT_CAP_psb_cyc))
223 return false;
224
225 allowed = pt_cap_get(PT_CAP_psb_periods);
226 requested = (config & RTIT_CTL_PSB_FREQ) >>
227 RTIT_CTL_PSB_FREQ_OFFSET;
228 if (requested && (!(allowed & BIT(requested))))
229 return false;
230
231 allowed = pt_cap_get(PT_CAP_cycle_thresholds);
232 requested = (config & RTIT_CTL_CYC_THRESH) >>
233 RTIT_CTL_CYC_THRESH_OFFSET;
234 if (requested && (!(allowed & BIT(requested))))
235 return false;
236 }
237
238 if (config & RTIT_CTL_MTC) {
239 /*
240 * In the unlikely case that CPUID lists valid mtc periods,
241 * but not the mtc capability, drop out here.
242 *
243 * Spec says that setting mtc period bits while mtc bit in
244 * CPUID is 0 will #GP, so better safe than sorry.
245 */
246 if (!pt_cap_get(PT_CAP_mtc))
247 return false;
248
249 allowed = pt_cap_get(PT_CAP_mtc_periods);
250 if (!allowed)
251 return false;
252
253 requested = (config & RTIT_CTL_MTC_RANGE) >>
254 RTIT_CTL_MTC_RANGE_OFFSET;
255
256 if (!(allowed & BIT(requested)))
257 return false;
258 }
259
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200260 return true;
261}
262
263/*
264 * PT configuration helpers
265 * These all are cpu affine and operate on a local PT
266 */
267
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200268static void pt_config(struct perf_event *event)
269{
270 u64 reg;
271
Alexander Shishkin9a6694c2015-07-30 16:48:24 +0300272 if (!event->hw.itrace_started) {
273 event->hw.itrace_started = 1;
274 wrmsrl(MSR_IA32_RTIT_STATUS, 0);
275 }
276
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200277 reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
278
279 if (!event->attr.exclude_kernel)
280 reg |= RTIT_CTL_OS;
281 if (!event->attr.exclude_user)
282 reg |= RTIT_CTL_USR;
283
284 reg |= (event->attr.config & PT_CONFIG_MASK);
285
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300286 event->hw.config = reg;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200287 wrmsrl(MSR_IA32_RTIT_CTL, reg);
288}
289
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300290static void pt_config_stop(struct perf_event *event)
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200291{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300292 u64 ctl = READ_ONCE(event->hw.config);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200293
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300294 /* may be already stopped by a PMI */
295 if (!(ctl & RTIT_CTL_TRACEEN))
296 return;
297
298 ctl &= ~RTIT_CTL_TRACEEN;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200299 wrmsrl(MSR_IA32_RTIT_CTL, ctl);
300
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300301 WRITE_ONCE(event->hw.config, ctl);
302
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200303 /*
304 * A wrmsr that disables trace generation serializes other PT
305 * registers and causes all data packets to be written to memory,
306 * but a fence is required for the data to become globally visible.
307 *
308 * The below WMB, separating data store and aux_head store matches
309 * the consumer's RMB that separates aux_head load and data load.
310 */
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300311 wmb();
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200312}
313
314static void pt_config_buffer(void *buf, unsigned int topa_idx,
315 unsigned int output_off)
316{
317 u64 reg;
318
319 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, virt_to_phys(buf));
320
321 reg = 0x7f | ((u64)topa_idx << 7) | ((u64)output_off << 32);
322
323 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, reg);
324}
325
326/*
327 * Keep ToPA table-related metadata on the same page as the actual table,
328 * taking up a few words from the top
329 */
330
331#define TENTS_PER_PAGE (((PAGE_SIZE - 40) / sizeof(struct topa_entry)) - 1)
332
333/**
334 * struct topa - page-sized ToPA table with metadata at the top
335 * @table: actual ToPA table entries, as understood by PT hardware
336 * @list: linkage to struct pt_buffer's list of tables
337 * @phys: physical address of this page
338 * @offset: offset of the first entry in this table in the buffer
339 * @size: total size of all entries in this table
340 * @last: index of the last initialized entry in this table
341 */
342struct topa {
343 struct topa_entry table[TENTS_PER_PAGE];
344 struct list_head list;
345 u64 phys;
346 u64 offset;
347 size_t size;
348 int last;
349};
350
351/* make -1 stand for the last table entry */
352#define TOPA_ENTRY(t, i) ((i) == -1 ? &(t)->table[(t)->last] : &(t)->table[(i)])
353
354/**
355 * topa_alloc() - allocate page-sized ToPA table
356 * @cpu: CPU on which to allocate.
357 * @gfp: Allocation flags.
358 *
359 * Return: On success, return the pointer to ToPA table page.
360 */
361static struct topa *topa_alloc(int cpu, gfp_t gfp)
362{
363 int node = cpu_to_node(cpu);
364 struct topa *topa;
365 struct page *p;
366
367 p = alloc_pages_node(node, gfp | __GFP_ZERO, 0);
368 if (!p)
369 return NULL;
370
371 topa = page_address(p);
372 topa->last = 0;
373 topa->phys = page_to_phys(p);
374
375 /*
376 * In case of singe-entry ToPA, always put the self-referencing END
377 * link as the 2nd entry in the table
378 */
379 if (!pt_cap_get(PT_CAP_topa_multiple_entries)) {
380 TOPA_ENTRY(topa, 1)->base = topa->phys >> TOPA_SHIFT;
381 TOPA_ENTRY(topa, 1)->end = 1;
382 }
383
384 return topa;
385}
386
387/**
388 * topa_free() - free a page-sized ToPA table
389 * @topa: Table to deallocate.
390 */
391static void topa_free(struct topa *topa)
392{
393 free_page((unsigned long)topa);
394}
395
396/**
397 * topa_insert_table() - insert a ToPA table into a buffer
398 * @buf: PT buffer that's being extended.
399 * @topa: New topa table to be inserted.
400 *
401 * If it's the first table in this buffer, set up buffer's pointers
402 * accordingly; otherwise, add a END=1 link entry to @topa to the current
403 * "last" table and adjust the last table pointer to @topa.
404 */
405static void topa_insert_table(struct pt_buffer *buf, struct topa *topa)
406{
407 struct topa *last = buf->last;
408
409 list_add_tail(&topa->list, &buf->tables);
410
411 if (!buf->first) {
412 buf->first = buf->last = buf->cur = topa;
413 return;
414 }
415
416 topa->offset = last->offset + last->size;
417 buf->last = topa;
418
419 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
420 return;
421
422 BUG_ON(last->last != TENTS_PER_PAGE - 1);
423
424 TOPA_ENTRY(last, -1)->base = topa->phys >> TOPA_SHIFT;
425 TOPA_ENTRY(last, -1)->end = 1;
426}
427
428/**
429 * topa_table_full() - check if a ToPA table is filled up
430 * @topa: ToPA table.
431 */
432static bool topa_table_full(struct topa *topa)
433{
434 /* single-entry ToPA is a special case */
435 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
436 return !!topa->last;
437
438 return topa->last == TENTS_PER_PAGE - 1;
439}
440
441/**
442 * topa_insert_pages() - create a list of ToPA tables
443 * @buf: PT buffer being initialized.
444 * @gfp: Allocation flags.
445 *
446 * This initializes a list of ToPA tables with entries from
447 * the data_pages provided by rb_alloc_aux().
448 *
449 * Return: 0 on success or error code.
450 */
451static int topa_insert_pages(struct pt_buffer *buf, gfp_t gfp)
452{
453 struct topa *topa = buf->last;
454 int order = 0;
455 struct page *p;
456
457 p = virt_to_page(buf->data_pages[buf->nr_pages]);
458 if (PagePrivate(p))
459 order = page_private(p);
460
461 if (topa_table_full(topa)) {
462 topa = topa_alloc(buf->cpu, gfp);
463 if (!topa)
464 return -ENOMEM;
465
466 topa_insert_table(buf, topa);
467 }
468
469 TOPA_ENTRY(topa, -1)->base = page_to_phys(p) >> TOPA_SHIFT;
470 TOPA_ENTRY(topa, -1)->size = order;
471 if (!buf->snapshot && !pt_cap_get(PT_CAP_topa_multiple_entries)) {
472 TOPA_ENTRY(topa, -1)->intr = 1;
473 TOPA_ENTRY(topa, -1)->stop = 1;
474 }
475
476 topa->last++;
477 topa->size += sizes(order);
478
479 buf->nr_pages += 1ul << order;
480
481 return 0;
482}
483
484/**
485 * pt_topa_dump() - print ToPA tables and their entries
486 * @buf: PT buffer.
487 */
488static void pt_topa_dump(struct pt_buffer *buf)
489{
490 struct topa *topa;
491
492 list_for_each_entry(topa, &buf->tables, list) {
493 int i;
494
Ingo Molnar2e54a5b2015-04-02 17:57:59 +0200495 pr_debug("# table @%p (%016Lx), off %llx size %zx\n", topa->table,
496 topa->phys, topa->offset, topa->size);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200497 for (i = 0; i < TENTS_PER_PAGE; i++) {
498 pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
499 &topa->table[i],
500 (unsigned long)topa->table[i].base << TOPA_SHIFT,
501 sizes(topa->table[i].size),
502 topa->table[i].end ? 'E' : ' ',
503 topa->table[i].intr ? 'I' : ' ',
504 topa->table[i].stop ? 'S' : ' ',
505 *(u64 *)&topa->table[i]);
506 if ((pt_cap_get(PT_CAP_topa_multiple_entries) &&
507 topa->table[i].stop) ||
508 topa->table[i].end)
509 break;
510 }
511 }
512}
513
514/**
515 * pt_buffer_advance() - advance to the next output region
516 * @buf: PT buffer.
517 *
518 * Advance the current pointers in the buffer to the next ToPA entry.
519 */
520static void pt_buffer_advance(struct pt_buffer *buf)
521{
522 buf->output_off = 0;
523 buf->cur_idx++;
524
525 if (buf->cur_idx == buf->cur->last) {
526 if (buf->cur == buf->last)
527 buf->cur = buf->first;
528 else
529 buf->cur = list_entry(buf->cur->list.next, struct topa,
530 list);
531 buf->cur_idx = 0;
532 }
533}
534
535/**
536 * pt_update_head() - calculate current offsets and sizes
537 * @pt: Per-cpu pt context.
538 *
539 * Update buffer's current write pointer position and data size.
540 */
541static void pt_update_head(struct pt *pt)
542{
543 struct pt_buffer *buf = perf_get_aux(&pt->handle);
544 u64 topa_idx, base, old;
545
546 /* offset of the first region in this table from the beginning of buf */
547 base = buf->cur->offset + buf->output_off;
548
549 /* offset of the current output region within this table */
550 for (topa_idx = 0; topa_idx < buf->cur_idx; topa_idx++)
551 base += sizes(buf->cur->table[topa_idx].size);
552
553 if (buf->snapshot) {
554 local_set(&buf->data_size, base);
555 } else {
556 old = (local64_xchg(&buf->head, base) &
557 ((buf->nr_pages << PAGE_SHIFT) - 1));
558 if (base < old)
559 base += buf->nr_pages << PAGE_SHIFT;
560
561 local_add(base - old, &buf->data_size);
562 }
563}
564
565/**
566 * pt_buffer_region() - obtain current output region's address
567 * @buf: PT buffer.
568 */
569static void *pt_buffer_region(struct pt_buffer *buf)
570{
571 return phys_to_virt(buf->cur->table[buf->cur_idx].base << TOPA_SHIFT);
572}
573
574/**
575 * pt_buffer_region_size() - obtain current output region's size
576 * @buf: PT buffer.
577 */
578static size_t pt_buffer_region_size(struct pt_buffer *buf)
579{
580 return sizes(buf->cur->table[buf->cur_idx].size);
581}
582
583/**
584 * pt_handle_status() - take care of possible status conditions
585 * @pt: Per-cpu pt context.
586 */
587static void pt_handle_status(struct pt *pt)
588{
589 struct pt_buffer *buf = perf_get_aux(&pt->handle);
590 int advance = 0;
591 u64 status;
592
593 rdmsrl(MSR_IA32_RTIT_STATUS, status);
594
595 if (status & RTIT_STATUS_ERROR) {
596 pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
597 pt_topa_dump(buf);
598 status &= ~RTIT_STATUS_ERROR;
599 }
600
601 if (status & RTIT_STATUS_STOPPED) {
602 status &= ~RTIT_STATUS_STOPPED;
603
604 /*
605 * On systems that only do single-entry ToPA, hitting STOP
606 * means we are already losing data; need to let the decoder
607 * know.
608 */
609 if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
610 buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
611 local_inc(&buf->lost);
612 advance++;
613 }
614 }
615
616 /*
617 * Also on single-entry ToPA implementations, interrupt will come
618 * before the output reaches its output region's boundary.
619 */
620 if (!pt_cap_get(PT_CAP_topa_multiple_entries) && !buf->snapshot &&
621 pt_buffer_region_size(buf) - buf->output_off <= TOPA_PMI_MARGIN) {
622 void *head = pt_buffer_region(buf);
623
624 /* everything within this margin needs to be zeroed out */
625 memset(head + buf->output_off, 0,
626 pt_buffer_region_size(buf) -
627 buf->output_off);
628 advance++;
629 }
630
631 if (advance)
632 pt_buffer_advance(buf);
633
634 wrmsrl(MSR_IA32_RTIT_STATUS, status);
635}
636
637/**
638 * pt_read_offset() - translate registers into buffer pointers
639 * @buf: PT buffer.
640 *
641 * Set buffer's output pointers from MSR values.
642 */
643static void pt_read_offset(struct pt_buffer *buf)
644{
645 u64 offset, base_topa;
646
647 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, base_topa);
648 buf->cur = phys_to_virt(base_topa);
649
650 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, offset);
651 /* offset within current output region */
652 buf->output_off = offset >> 32;
653 /* index of current output region within this table */
654 buf->cur_idx = (offset & 0xffffff80) >> 7;
655}
656
657/**
658 * pt_topa_next_entry() - obtain index of the first page in the next ToPA entry
659 * @buf: PT buffer.
660 * @pg: Page offset in the buffer.
661 *
662 * When advancing to the next output region (ToPA entry), given a page offset
663 * into the buffer, we need to find the offset of the first page in the next
664 * region.
665 */
666static unsigned int pt_topa_next_entry(struct pt_buffer *buf, unsigned int pg)
667{
668 struct topa_entry *te = buf->topa_index[pg];
669
670 /* one region */
671 if (buf->first == buf->last && buf->first->last == 1)
672 return pg;
673
674 do {
675 pg++;
676 pg &= buf->nr_pages - 1;
677 } while (buf->topa_index[pg] == te);
678
679 return pg;
680}
681
682/**
683 * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
684 * @buf: PT buffer.
685 * @handle: Current output handle.
686 *
687 * Place INT and STOP marks to prevent overwriting old data that the consumer
Alexander Shishkincf302bf2015-04-21 16:16:15 +0300688 * hasn't yet collected and waking up the consumer after a certain fraction of
689 * the buffer has filled up. Only needed and sensible for non-snapshot counters.
690 *
691 * This obviously relies on buf::head to figure out buffer markers, so it has
692 * to be called after pt_buffer_reset_offsets() and before the hardware tracing
693 * is enabled.
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200694 */
695static int pt_buffer_reset_markers(struct pt_buffer *buf,
696 struct perf_output_handle *handle)
697
698{
Alexander Shishkinf73ec482015-05-22 18:30:22 +0300699 unsigned long head = local64_read(&buf->head);
700 unsigned long idx, npages, wakeup;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200701
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200702 /* can't stop in the middle of an output region */
703 if (buf->output_off + handle->size + 1 <
704 sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
705 return -EINVAL;
706
707
708 /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
709 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
710 return 0;
711
712 /* clear STOP and INT from current entry */
713 buf->topa_index[buf->stop_pos]->stop = 0;
714 buf->topa_index[buf->intr_pos]->intr = 0;
715
Alexander Shishkinf73ec482015-05-22 18:30:22 +0300716 /* how many pages till the STOP marker */
717 npages = handle->size >> PAGE_SHIFT;
718
719 /* if it's on a page boundary, fill up one more page */
720 if (!offset_in_page(head + handle->size + 1))
721 npages++;
722
723 idx = (head >> PAGE_SHIFT) + npages;
724 idx &= buf->nr_pages - 1;
725 buf->stop_pos = idx;
726
727 wakeup = handle->wakeup >> PAGE_SHIFT;
728
729 /* in the worst case, wake up the consumer one page before hard stop */
730 idx = (head >> PAGE_SHIFT) + npages - 1;
731 if (idx > wakeup)
732 idx = wakeup;
733
734 idx &= buf->nr_pages - 1;
735 buf->intr_pos = idx;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200736
737 buf->topa_index[buf->stop_pos]->stop = 1;
738 buf->topa_index[buf->intr_pos]->intr = 1;
739
740 return 0;
741}
742
743/**
744 * pt_buffer_setup_topa_index() - build topa_index[] table of regions
745 * @buf: PT buffer.
746 *
747 * topa_index[] references output regions indexed by offset into the
748 * buffer for purposes of quick reverse lookup.
749 */
750static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
751{
752 struct topa *cur = buf->first, *prev = buf->last;
753 struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
754 *te_prev = TOPA_ENTRY(prev, prev->last - 1);
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300755 int pg = 0, idx = 0;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200756
757 while (pg < buf->nr_pages) {
758 int tidx;
759
760 /* pages within one topa entry */
761 for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
762 buf->topa_index[pg] = te_prev;
763
764 te_prev = te_cur;
765
766 if (idx == cur->last - 1) {
767 /* advance to next topa table */
768 idx = 0;
769 cur = list_entry(cur->list.next, struct topa, list);
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300770 } else {
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200771 idx++;
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300772 }
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200773 te_cur = TOPA_ENTRY(cur, idx);
774 }
775
776}
777
778/**
779 * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
780 * @buf: PT buffer.
781 * @head: Write pointer (aux_head) from AUX buffer.
782 *
783 * Find the ToPA table and entry corresponding to given @head and set buffer's
Alexander Shishkin5b1dbd12015-04-21 16:16:16 +0300784 * "current" pointers accordingly. This is done after we have obtained the
785 * current aux_head position from a successful call to perf_aux_output_begin()
786 * to make sure the hardware is writing to the right place.
787 *
788 * This function modifies buf::{cur,cur_idx,output_off} that will be programmed
789 * into PT msrs when the tracing is enabled and buf::head and buf::data_size,
790 * which are used to determine INT and STOP markers' locations by a subsequent
791 * call to pt_buffer_reset_markers().
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200792 */
793static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
794{
795 int pg;
796
797 if (buf->snapshot)
798 head &= (buf->nr_pages << PAGE_SHIFT) - 1;
799
800 pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
801 pg = pt_topa_next_entry(buf, pg);
802
803 buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
804 buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
805 (unsigned long)buf->cur) / sizeof(struct topa_entry);
806 buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
807
808 local64_set(&buf->head, head);
809 local_set(&buf->data_size, 0);
810}
811
812/**
813 * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
814 * @buf: PT buffer.
815 */
816static void pt_buffer_fini_topa(struct pt_buffer *buf)
817{
818 struct topa *topa, *iter;
819
820 list_for_each_entry_safe(topa, iter, &buf->tables, list) {
821 /*
822 * right now, this is in free_aux() path only, so
823 * no need to unlink this table from the list
824 */
825 topa_free(topa);
826 }
827}
828
829/**
830 * pt_buffer_init_topa() - initialize ToPA table for pt buffer
831 * @buf: PT buffer.
832 * @size: Total size of all regions within this ToPA.
833 * @gfp: Allocation flags.
834 */
835static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
836 gfp_t gfp)
837{
838 struct topa *topa;
839 int err;
840
841 topa = topa_alloc(buf->cpu, gfp);
842 if (!topa)
843 return -ENOMEM;
844
845 topa_insert_table(buf, topa);
846
847 while (buf->nr_pages < nr_pages) {
848 err = topa_insert_pages(buf, gfp);
849 if (err) {
850 pt_buffer_fini_topa(buf);
851 return -ENOMEM;
852 }
853 }
854
855 pt_buffer_setup_topa_index(buf);
856
857 /* link last table to the first one, unless we're double buffering */
858 if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
859 TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
860 TOPA_ENTRY(buf->last, -1)->end = 1;
861 }
862
863 pt_topa_dump(buf);
864 return 0;
865}
866
867/**
868 * pt_buffer_setup_aux() - set up topa tables for a PT buffer
869 * @cpu: Cpu on which to allocate, -1 means current.
870 * @pages: Array of pointers to buffer pages passed from perf core.
871 * @nr_pages: Number of pages in the buffer.
872 * @snapshot: If this is a snapshot/overwrite counter.
873 *
874 * This is a pmu::setup_aux callback that sets up ToPA tables and all the
875 * bookkeeping for an AUX buffer.
876 *
877 * Return: Our private PT buffer structure.
878 */
879static void *
880pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
881{
882 struct pt_buffer *buf;
883 int node, ret;
884
885 if (!nr_pages)
886 return NULL;
887
888 if (cpu == -1)
889 cpu = raw_smp_processor_id();
890 node = cpu_to_node(cpu);
891
892 buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
893 GFP_KERNEL, node);
894 if (!buf)
895 return NULL;
896
897 buf->cpu = cpu;
898 buf->snapshot = snapshot;
899 buf->data_pages = pages;
900
901 INIT_LIST_HEAD(&buf->tables);
902
903 ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
904 if (ret) {
905 kfree(buf);
906 return NULL;
907 }
908
909 return buf;
910}
911
912/**
913 * pt_buffer_free_aux() - perf AUX deallocation path callback
914 * @data: PT buffer.
915 */
916static void pt_buffer_free_aux(void *data)
917{
918 struct pt_buffer *buf = data;
919
920 pt_buffer_fini_topa(buf);
921 kfree(buf);
922}
923
924/**
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200925 * intel_pt_interrupt() - PT PMI handler
926 */
927void intel_pt_interrupt(void)
928{
929 struct pt *pt = this_cpu_ptr(&pt_ctx);
930 struct pt_buffer *buf;
931 struct perf_event *event = pt->handle.event;
932
933 /*
934 * There may be a dangling PT bit in the interrupt status register
935 * after PT has been disabled by pt_event_stop(). Make sure we don't
936 * do anything (particularly, re-enable) for this event here.
937 */
938 if (!ACCESS_ONCE(pt->handle_nmi))
939 return;
940
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300941 /*
942 * If VMX is on and PT does not support it, don't touch anything.
943 */
944 if (READ_ONCE(pt->vmx_on))
945 return;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200946
947 if (!event)
948 return;
949
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300950 pt_config_stop(event);
951
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200952 buf = perf_get_aux(&pt->handle);
953 if (!buf)
954 return;
955
956 pt_read_offset(buf);
957
958 pt_handle_status(pt);
959
960 pt_update_head(pt);
961
962 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
963 local_xchg(&buf->lost, 0));
964
965 if (!event->hw.state) {
966 int ret;
967
968 buf = perf_aux_output_begin(&pt->handle, event);
969 if (!buf) {
970 event->hw.state = PERF_HES_STOPPED;
971 return;
972 }
973
974 pt_buffer_reset_offsets(buf, pt->handle.head);
Alexander Shishkincf302bf2015-04-21 16:16:15 +0300975 /* snapshot counters don't use PMI, so it's safe */
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200976 ret = pt_buffer_reset_markers(buf, &pt->handle);
977 if (ret) {
978 perf_aux_output_end(&pt->handle, 0, true);
979 return;
980 }
981
982 pt_config_buffer(buf->cur->table, buf->cur_idx,
983 buf->output_off);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200984 pt_config(event);
985 }
986}
987
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300988void intel_pt_handle_vmx(int on)
989{
990 struct pt *pt = this_cpu_ptr(&pt_ctx);
991 struct perf_event *event;
992 unsigned long flags;
993
994 /* PT plays nice with VMX, do nothing */
995 if (pt_pmu.vmx)
996 return;
997
998 /*
999 * VMXON will clear RTIT_CTL.TraceEn; we need to make
1000 * sure to not try to set it while VMX is on. Disable
1001 * interrupts to avoid racing with pmu callbacks;
1002 * concurrent PMI should be handled fine.
1003 */
1004 local_irq_save(flags);
1005 WRITE_ONCE(pt->vmx_on, on);
1006
1007 if (on) {
1008 /* prevent pt_config_stop() from writing RTIT_CTL */
1009 event = pt->handle.event;
1010 if (event)
1011 event->hw.config = 0;
1012 }
1013 local_irq_restore(flags);
1014}
1015EXPORT_SYMBOL_GPL(intel_pt_handle_vmx);
1016
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001017/*
1018 * PMU callbacks
1019 */
1020
1021static void pt_event_start(struct perf_event *event, int mode)
1022{
Alexander Shishkin66d21902016-03-04 15:42:48 +02001023 struct hw_perf_event *hwc = &event->hw;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001024 struct pt *pt = this_cpu_ptr(&pt_ctx);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001025 struct pt_buffer *buf;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001026
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001027 if (READ_ONCE(pt->vmx_on))
1028 return;
1029
Alexander Shishkin66d21902016-03-04 15:42:48 +02001030 buf = perf_aux_output_begin(&pt->handle, event);
1031 if (!buf)
1032 goto fail_stop;
1033
1034 pt_buffer_reset_offsets(buf, pt->handle.head);
1035 if (!buf->snapshot) {
1036 if (pt_buffer_reset_markers(buf, &pt->handle))
1037 goto fail_end_stop;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001038 }
1039
1040 ACCESS_ONCE(pt->handle_nmi) = 1;
Alexander Shishkin66d21902016-03-04 15:42:48 +02001041 hwc->state = 0;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001042
1043 pt_config_buffer(buf->cur->table, buf->cur_idx,
1044 buf->output_off);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001045 pt_config(event);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001046
1047 return;
1048
1049fail_end_stop:
1050 perf_aux_output_end(&pt->handle, 0, true);
1051fail_stop:
1052 hwc->state = PERF_HES_STOPPED;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001053}
1054
1055static void pt_event_stop(struct perf_event *event, int mode)
1056{
1057 struct pt *pt = this_cpu_ptr(&pt_ctx);
1058
1059 /*
1060 * Protect against the PMI racing with disabling wrmsr,
1061 * see comment in intel_pt_interrupt().
1062 */
1063 ACCESS_ONCE(pt->handle_nmi) = 0;
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001064
1065 pt_config_stop(event);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001066
1067 if (event->hw.state == PERF_HES_STOPPED)
1068 return;
1069
1070 event->hw.state = PERF_HES_STOPPED;
1071
1072 if (mode & PERF_EF_UPDATE) {
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001073 struct pt_buffer *buf = perf_get_aux(&pt->handle);
1074
1075 if (!buf)
1076 return;
1077
1078 if (WARN_ON_ONCE(pt->handle.event != event))
1079 return;
1080
1081 pt_read_offset(buf);
1082
1083 pt_handle_status(pt);
1084
1085 pt_update_head(pt);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001086
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001087 if (buf->snapshot)
1088 pt->handle.head =
1089 local_xchg(&buf->data_size,
1090 buf->nr_pages << PAGE_SHIFT);
1091 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
1092 local_xchg(&buf->lost, 0));
1093 }
1094}
1095
Alexander Shishkin66d21902016-03-04 15:42:48 +02001096static void pt_event_del(struct perf_event *event, int mode)
1097{
1098 pt_event_stop(event, PERF_EF_UPDATE);
1099}
1100
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001101static int pt_event_add(struct perf_event *event, int mode)
1102{
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001103 struct pt *pt = this_cpu_ptr(&pt_ctx);
1104 struct hw_perf_event *hwc = &event->hw;
1105 int ret = -EBUSY;
1106
1107 if (pt->handle.event)
Ingo Molnar0c992412015-04-16 12:38:30 +02001108 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001109
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001110 if (mode & PERF_EF_START) {
1111 pt_event_start(event, 0);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001112 ret = -EINVAL;
Ingo Molnar0c992412015-04-16 12:38:30 +02001113 if (hwc->state == PERF_HES_STOPPED)
Alexander Shishkin66d21902016-03-04 15:42:48 +02001114 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001115 } else {
1116 hwc->state = PERF_HES_STOPPED;
1117 }
1118
Alexander Shishkin66d21902016-03-04 15:42:48 +02001119 ret = 0;
Ingo Molnar0c992412015-04-16 12:38:30 +02001120fail:
Alexander Shishkin66d21902016-03-04 15:42:48 +02001121
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001122 return ret;
1123}
1124
1125static void pt_event_read(struct perf_event *event)
1126{
1127}
1128
1129static void pt_event_destroy(struct perf_event *event)
1130{
1131 x86_del_exclusive(x86_lbr_exclusive_pt);
1132}
1133
1134static int pt_event_init(struct perf_event *event)
1135{
1136 if (event->attr.type != pt_pmu.pmu.type)
1137 return -ENOENT;
1138
1139 if (!pt_event_valid(event))
1140 return -EINVAL;
1141
1142 if (x86_add_exclusive(x86_lbr_exclusive_pt))
1143 return -EBUSY;
1144
1145 event->destroy = pt_event_destroy;
1146
1147 return 0;
1148}
1149
Takao Indoh24cc12b2015-11-04 14:22:32 +09001150void cpu_emergency_stop_pt(void)
1151{
1152 struct pt *pt = this_cpu_ptr(&pt_ctx);
1153
1154 if (pt->handle.event)
1155 pt_event_stop(pt->handle.event, PERF_EF_UPDATE);
1156}
1157
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001158static __init int pt_init(void)
1159{
1160 int ret, cpu, prior_warn = 0;
1161
1162 BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
Huaitong Han73fdeb62015-08-31 16:21:02 +08001163
Alexander Shishkine465de12016-04-06 17:35:07 +03001164 if (!boot_cpu_has(X86_FEATURE_INTEL_PT))
Huaitong Han73fdeb62015-08-31 16:21:02 +08001165 return -ENODEV;
1166
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001167 get_online_cpus();
1168 for_each_online_cpu(cpu) {
1169 u64 ctl;
1170
1171 ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
1172 if (!ret && (ctl & RTIT_CTL_TRACEEN))
1173 prior_warn++;
1174 }
1175 put_online_cpus();
1176
1177 if (prior_warn) {
1178 x86_add_exclusive(x86_lbr_exclusive_pt);
1179 pr_warn("PT is enabled at boot time, doing nothing\n");
1180
1181 return -EBUSY;
1182 }
1183
1184 ret = pt_pmu_hw_init();
1185 if (ret)
1186 return ret;
1187
1188 if (!pt_cap_get(PT_CAP_topa_output)) {
1189 pr_warn("ToPA output is not supported on this CPU\n");
1190 return -ENODEV;
1191 }
1192
1193 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
1194 pt_pmu.pmu.capabilities =
1195 PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
1196
1197 pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
1198 pt_pmu.pmu.attr_groups = pt_attr_groups;
1199 pt_pmu.pmu.task_ctx_nr = perf_sw_context;
1200 pt_pmu.pmu.event_init = pt_event_init;
1201 pt_pmu.pmu.add = pt_event_add;
1202 pt_pmu.pmu.del = pt_event_del;
1203 pt_pmu.pmu.start = pt_event_start;
1204 pt_pmu.pmu.stop = pt_event_stop;
1205 pt_pmu.pmu.read = pt_event_read;
1206 pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
1207 pt_pmu.pmu.free_aux = pt_buffer_free_aux;
1208 ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
1209
1210 return ret;
1211}
Paul Gortmaker5b00c1e2015-05-01 21:57:34 -04001212arch_initcall(pt_init);