127f6d22bSBorislav Petkov /* 227f6d22bSBorislav Petkov * Performance events x86 architecture header 327f6d22bSBorislav Petkov * 427f6d22bSBorislav Petkov * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> 527f6d22bSBorislav Petkov * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar 627f6d22bSBorislav Petkov * Copyright (C) 2009 Jaswinder Singh Rajput 727f6d22bSBorislav Petkov * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter 827f6d22bSBorislav Petkov * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra 927f6d22bSBorislav Petkov * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> 1027f6d22bSBorislav Petkov * Copyright (C) 2009 Google, Inc., Stephane Eranian 1127f6d22bSBorislav Petkov * 1227f6d22bSBorislav Petkov * For licencing details see kernel-base/COPYING 1327f6d22bSBorislav Petkov */ 1427f6d22bSBorislav Petkov 1527f6d22bSBorislav Petkov #include <linux/perf_event.h> 1627f6d22bSBorislav Petkov 1710043e02SThomas Gleixner #include <asm/intel_ds.h> 1810043e02SThomas Gleixner 1927f6d22bSBorislav Petkov /* To enable MSR tracing please use the generic trace points. */ 2027f6d22bSBorislav Petkov 2127f6d22bSBorislav Petkov /* 2227f6d22bSBorislav Petkov * | NHM/WSM | SNB | 2327f6d22bSBorislav Petkov * register ------------------------------- 2427f6d22bSBorislav Petkov * | HT | no HT | HT | no HT | 2527f6d22bSBorislav Petkov *----------------------------------------- 2627f6d22bSBorislav Petkov * offcore | core | core | cpu | core | 2727f6d22bSBorislav Petkov * lbr_sel | core | core | cpu | core | 2827f6d22bSBorislav Petkov * ld_lat | cpu | core | cpu | core | 2927f6d22bSBorislav Petkov *----------------------------------------- 3027f6d22bSBorislav Petkov * 3127f6d22bSBorislav Petkov * Given that there is a small number of shared regs, 3227f6d22bSBorislav Petkov * we can pre-allocate their slot in the per-cpu 3327f6d22bSBorislav Petkov * per-core reg tables. 3427f6d22bSBorislav Petkov */ 3527f6d22bSBorislav Petkov enum extra_reg_type { 3627f6d22bSBorislav Petkov EXTRA_REG_NONE = -1, /* not used */ 3727f6d22bSBorislav Petkov 3827f6d22bSBorislav Petkov EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */ 3927f6d22bSBorislav Petkov EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */ 4027f6d22bSBorislav Petkov EXTRA_REG_LBR = 2, /* lbr_select */ 4127f6d22bSBorislav Petkov EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */ 4227f6d22bSBorislav Petkov EXTRA_REG_FE = 4, /* fe_* */ 4327f6d22bSBorislav Petkov 4427f6d22bSBorislav Petkov EXTRA_REG_MAX /* number of entries needed */ 4527f6d22bSBorislav Petkov }; 4627f6d22bSBorislav Petkov 4727f6d22bSBorislav Petkov struct event_constraint { 4827f6d22bSBorislav Petkov union { 4927f6d22bSBorislav Petkov unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; 5027f6d22bSBorislav Petkov u64 idxmsk64; 5127f6d22bSBorislav Petkov }; 5227f6d22bSBorislav Petkov u64 code; 5327f6d22bSBorislav Petkov u64 cmask; 5427f6d22bSBorislav Petkov int weight; 5527f6d22bSBorislav Petkov int overlap; 5627f6d22bSBorislav Petkov int flags; 5727f6d22bSBorislav Petkov }; 5827f6d22bSBorislav Petkov /* 5927f6d22bSBorislav Petkov * struct hw_perf_event.flags flags 6027f6d22bSBorislav Petkov */ 6127f6d22bSBorislav Petkov #define PERF_X86_EVENT_PEBS_LDLAT 0x0001 /* ld+ldlat data address sampling */ 6227f6d22bSBorislav Petkov #define PERF_X86_EVENT_PEBS_ST 0x0002 /* st data address sampling */ 6327f6d22bSBorislav Petkov #define PERF_X86_EVENT_PEBS_ST_HSW 0x0004 /* haswell style datala, store */ 6427f6d22bSBorislav Petkov #define PERF_X86_EVENT_COMMITTED 0x0008 /* event passed commit_txn */ 6527f6d22bSBorislav Petkov #define PERF_X86_EVENT_PEBS_LD_HSW 0x0010 /* haswell style datala, load */ 6627f6d22bSBorislav Petkov #define PERF_X86_EVENT_PEBS_NA_HSW 0x0020 /* haswell style datala, unknown */ 6727f6d22bSBorislav Petkov #define PERF_X86_EVENT_EXCL 0x0040 /* HT exclusivity on counter */ 6827f6d22bSBorislav Petkov #define PERF_X86_EVENT_DYNAMIC 0x0080 /* dynamic alloc'd constraint */ 6927f6d22bSBorislav Petkov #define PERF_X86_EVENT_RDPMC_ALLOWED 0x0100 /* grant rdpmc permission */ 7027f6d22bSBorislav Petkov #define PERF_X86_EVENT_EXCL_ACCT 0x0200 /* accounted EXCL event */ 7127f6d22bSBorislav Petkov #define PERF_X86_EVENT_AUTO_RELOAD 0x0400 /* use PEBS auto-reload */ 72*174afc3eSKan Liang #define PERF_X86_EVENT_LARGE_PEBS 0x0800 /* use large PEBS */ 7327f6d22bSBorislav Petkov 7427f6d22bSBorislav Petkov 7527f6d22bSBorislav Petkov struct amd_nb { 7627f6d22bSBorislav Petkov int nb_id; /* NorthBridge id */ 7727f6d22bSBorislav Petkov int refcnt; /* reference count */ 7827f6d22bSBorislav Petkov struct perf_event *owners[X86_PMC_IDX_MAX]; 7927f6d22bSBorislav Petkov struct event_constraint event_constraints[X86_PMC_IDX_MAX]; 8027f6d22bSBorislav Petkov }; 8127f6d22bSBorislav Petkov 82fd583ad1SKan Liang #define PEBS_COUNTER_MASK ((1ULL << MAX_PEBS_EVENTS) - 1) 8327f6d22bSBorislav Petkov 8427f6d22bSBorislav Petkov /* 8527f6d22bSBorislav Petkov * Flags PEBS can handle without an PMI. 8627f6d22bSBorislav Petkov * 8727f6d22bSBorislav Petkov * TID can only be handled by flushing at context switch. 882fe1bc1fSAndi Kleen * REGS_USER can be handled for events limited to ring 3. 8927f6d22bSBorislav Petkov * 9027f6d22bSBorislav Petkov */ 91*174afc3eSKan Liang #define LARGE_PEBS_FLAGS \ 9227f6d22bSBorislav Petkov (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \ 9327f6d22bSBorislav Petkov PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \ 9427f6d22bSBorislav Petkov PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \ 952fe1bc1fSAndi Kleen PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR | \ 9611974914SJiri Olsa PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER | \ 9711974914SJiri Olsa PERF_SAMPLE_PERIOD) 9827f6d22bSBorislav Petkov 992fe1bc1fSAndi Kleen #define PEBS_REGS \ 1002fe1bc1fSAndi Kleen (PERF_REG_X86_AX | \ 1012fe1bc1fSAndi Kleen PERF_REG_X86_BX | \ 1022fe1bc1fSAndi Kleen PERF_REG_X86_CX | \ 1032fe1bc1fSAndi Kleen PERF_REG_X86_DX | \ 1042fe1bc1fSAndi Kleen PERF_REG_X86_DI | \ 1052fe1bc1fSAndi Kleen PERF_REG_X86_SI | \ 1062fe1bc1fSAndi Kleen PERF_REG_X86_SP | \ 1072fe1bc1fSAndi Kleen PERF_REG_X86_BP | \ 1082fe1bc1fSAndi Kleen PERF_REG_X86_IP | \ 1092fe1bc1fSAndi Kleen PERF_REG_X86_FLAGS | \ 1102fe1bc1fSAndi Kleen PERF_REG_X86_R8 | \ 1112fe1bc1fSAndi Kleen PERF_REG_X86_R9 | \ 1122fe1bc1fSAndi Kleen PERF_REG_X86_R10 | \ 1132fe1bc1fSAndi Kleen PERF_REG_X86_R11 | \ 1142fe1bc1fSAndi Kleen PERF_REG_X86_R12 | \ 1152fe1bc1fSAndi Kleen PERF_REG_X86_R13 | \ 1162fe1bc1fSAndi Kleen PERF_REG_X86_R14 | \ 1172fe1bc1fSAndi Kleen PERF_REG_X86_R15) 1182fe1bc1fSAndi Kleen 11927f6d22bSBorislav Petkov /* 12027f6d22bSBorislav Petkov * Per register state. 12127f6d22bSBorislav Petkov */ 12227f6d22bSBorislav Petkov struct er_account { 12327f6d22bSBorislav Petkov raw_spinlock_t lock; /* per-core: protect structure */ 12427f6d22bSBorislav Petkov u64 config; /* extra MSR config */ 12527f6d22bSBorislav Petkov u64 reg; /* extra MSR number */ 12627f6d22bSBorislav Petkov atomic_t ref; /* reference count */ 12727f6d22bSBorislav Petkov }; 12827f6d22bSBorislav Petkov 12927f6d22bSBorislav Petkov /* 13027f6d22bSBorislav Petkov * Per core/cpu state 13127f6d22bSBorislav Petkov * 13227f6d22bSBorislav Petkov * Used to coordinate shared registers between HT threads or 13327f6d22bSBorislav Petkov * among events on a single PMU. 13427f6d22bSBorislav Petkov */ 13527f6d22bSBorislav Petkov struct intel_shared_regs { 13627f6d22bSBorislav Petkov struct er_account regs[EXTRA_REG_MAX]; 13727f6d22bSBorislav Petkov int refcnt; /* per-core: #HT threads */ 13827f6d22bSBorislav Petkov unsigned core_id; /* per-core: core id */ 13927f6d22bSBorislav Petkov }; 14027f6d22bSBorislav Petkov 14127f6d22bSBorislav Petkov enum intel_excl_state_type { 14227f6d22bSBorislav Petkov INTEL_EXCL_UNUSED = 0, /* counter is unused */ 14327f6d22bSBorislav Petkov INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */ 14427f6d22bSBorislav Petkov INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */ 14527f6d22bSBorislav Petkov }; 14627f6d22bSBorislav Petkov 14727f6d22bSBorislav Petkov struct intel_excl_states { 14827f6d22bSBorislav Petkov enum intel_excl_state_type state[X86_PMC_IDX_MAX]; 14927f6d22bSBorislav Petkov bool sched_started; /* true if scheduling has started */ 15027f6d22bSBorislav Petkov }; 15127f6d22bSBorislav Petkov 15227f6d22bSBorislav Petkov struct intel_excl_cntrs { 15327f6d22bSBorislav Petkov raw_spinlock_t lock; 15427f6d22bSBorislav Petkov 15527f6d22bSBorislav Petkov struct intel_excl_states states[2]; 15627f6d22bSBorislav Petkov 15727f6d22bSBorislav Petkov union { 15827f6d22bSBorislav Petkov u16 has_exclusive[2]; 15927f6d22bSBorislav Petkov u32 exclusive_present; 16027f6d22bSBorislav Petkov }; 16127f6d22bSBorislav Petkov 16227f6d22bSBorislav Petkov int refcnt; /* per-core: #HT threads */ 16327f6d22bSBorislav Petkov unsigned core_id; /* per-core: core id */ 16427f6d22bSBorislav Petkov }; 16527f6d22bSBorislav Petkov 16627f6d22bSBorislav Petkov #define MAX_LBR_ENTRIES 32 16727f6d22bSBorislav Petkov 16827f6d22bSBorislav Petkov enum { 16927f6d22bSBorislav Petkov X86_PERF_KFREE_SHARED = 0, 17027f6d22bSBorislav Petkov X86_PERF_KFREE_EXCL = 1, 17127f6d22bSBorislav Petkov X86_PERF_KFREE_MAX 17227f6d22bSBorislav Petkov }; 17327f6d22bSBorislav Petkov 17427f6d22bSBorislav Petkov struct cpu_hw_events { 17527f6d22bSBorislav Petkov /* 17627f6d22bSBorislav Petkov * Generic x86 PMC bits 17727f6d22bSBorislav Petkov */ 17827f6d22bSBorislav Petkov struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ 17927f6d22bSBorislav Petkov unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; 18027f6d22bSBorislav Petkov unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; 18127f6d22bSBorislav Petkov int enabled; 18227f6d22bSBorislav Petkov 18327f6d22bSBorislav Petkov int n_events; /* the # of events in the below arrays */ 18427f6d22bSBorislav Petkov int n_added; /* the # last events in the below arrays; 18527f6d22bSBorislav Petkov they've never been enabled yet */ 18627f6d22bSBorislav Petkov int n_txn; /* the # last events in the below arrays; 18727f6d22bSBorislav Petkov added in the current transaction */ 18827f6d22bSBorislav Petkov int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ 18927f6d22bSBorislav Petkov u64 tags[X86_PMC_IDX_MAX]; 19027f6d22bSBorislav Petkov 19127f6d22bSBorislav Petkov struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ 19227f6d22bSBorislav Petkov struct event_constraint *event_constraint[X86_PMC_IDX_MAX]; 19327f6d22bSBorislav Petkov 19427f6d22bSBorislav Petkov int n_excl; /* the number of exclusive events */ 19527f6d22bSBorislav Petkov 19627f6d22bSBorislav Petkov unsigned int txn_flags; 19727f6d22bSBorislav Petkov int is_fake; 19827f6d22bSBorislav Petkov 19927f6d22bSBorislav Petkov /* 20027f6d22bSBorislav Petkov * Intel DebugStore bits 20127f6d22bSBorislav Petkov */ 20227f6d22bSBorislav Petkov struct debug_store *ds; 203c1961a46SHugh Dickins void *ds_pebs_vaddr; 204c1961a46SHugh Dickins void *ds_bts_vaddr; 20527f6d22bSBorislav Petkov u64 pebs_enabled; 20609e61b4fSPeter Zijlstra int n_pebs; 20709e61b4fSPeter Zijlstra int n_large_pebs; 20827f6d22bSBorislav Petkov 20927f6d22bSBorislav Petkov /* 21027f6d22bSBorislav Petkov * Intel LBR bits 21127f6d22bSBorislav Petkov */ 21227f6d22bSBorislav Petkov int lbr_users; 21327f6d22bSBorislav Petkov struct perf_branch_stack lbr_stack; 21427f6d22bSBorislav Petkov struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; 21527f6d22bSBorislav Petkov struct er_account *lbr_sel; 21627f6d22bSBorislav Petkov u64 br_sel; 21727f6d22bSBorislav Petkov 21827f6d22bSBorislav Petkov /* 21927f6d22bSBorislav Petkov * Intel host/guest exclude bits 22027f6d22bSBorislav Petkov */ 22127f6d22bSBorislav Petkov u64 intel_ctrl_guest_mask; 22227f6d22bSBorislav Petkov u64 intel_ctrl_host_mask; 22327f6d22bSBorislav Petkov struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX]; 22427f6d22bSBorislav Petkov 22527f6d22bSBorislav Petkov /* 22627f6d22bSBorislav Petkov * Intel checkpoint mask 22727f6d22bSBorislav Petkov */ 22827f6d22bSBorislav Petkov u64 intel_cp_status; 22927f6d22bSBorislav Petkov 23027f6d22bSBorislav Petkov /* 23127f6d22bSBorislav Petkov * manage shared (per-core, per-cpu) registers 23227f6d22bSBorislav Petkov * used on Intel NHM/WSM/SNB 23327f6d22bSBorislav Petkov */ 23427f6d22bSBorislav Petkov struct intel_shared_regs *shared_regs; 23527f6d22bSBorislav Petkov /* 23627f6d22bSBorislav Petkov * manage exclusive counter access between hyperthread 23727f6d22bSBorislav Petkov */ 23827f6d22bSBorislav Petkov struct event_constraint *constraint_list; /* in enable order */ 23927f6d22bSBorislav Petkov struct intel_excl_cntrs *excl_cntrs; 24027f6d22bSBorislav Petkov int excl_thread_id; /* 0 or 1 */ 24127f6d22bSBorislav Petkov 24227f6d22bSBorislav Petkov /* 24327f6d22bSBorislav Petkov * AMD specific bits 24427f6d22bSBorislav Petkov */ 24527f6d22bSBorislav Petkov struct amd_nb *amd_nb; 24627f6d22bSBorislav Petkov /* Inverted mask of bits to clear in the perf_ctr ctrl registers */ 24727f6d22bSBorislav Petkov u64 perf_ctr_virt_mask; 24827f6d22bSBorislav Petkov 24927f6d22bSBorislav Petkov void *kfree_on_online[X86_PERF_KFREE_MAX]; 25027f6d22bSBorislav Petkov }; 25127f6d22bSBorislav Petkov 25227f6d22bSBorislav Petkov #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\ 25327f6d22bSBorislav Petkov { .idxmsk64 = (n) }, \ 25427f6d22bSBorislav Petkov .code = (c), \ 25527f6d22bSBorislav Petkov .cmask = (m), \ 25627f6d22bSBorislav Petkov .weight = (w), \ 25727f6d22bSBorislav Petkov .overlap = (o), \ 25827f6d22bSBorislav Petkov .flags = f, \ 25927f6d22bSBorislav Petkov } 26027f6d22bSBorislav Petkov 26127f6d22bSBorislav Petkov #define EVENT_CONSTRAINT(c, n, m) \ 26227f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0) 26327f6d22bSBorislav Petkov 26427f6d22bSBorislav Petkov #define INTEL_EXCLEVT_CONSTRAINT(c, n) \ 26527f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\ 26627f6d22bSBorislav Petkov 0, PERF_X86_EVENT_EXCL) 26727f6d22bSBorislav Petkov 26827f6d22bSBorislav Petkov /* 26927f6d22bSBorislav Petkov * The overlap flag marks event constraints with overlapping counter 27027f6d22bSBorislav Petkov * masks. This is the case if the counter mask of such an event is not 27127f6d22bSBorislav Petkov * a subset of any other counter mask of a constraint with an equal or 27227f6d22bSBorislav Petkov * higher weight, e.g.: 27327f6d22bSBorislav Petkov * 27427f6d22bSBorislav Petkov * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0); 27527f6d22bSBorislav Petkov * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0); 27627f6d22bSBorislav Petkov * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0); 27727f6d22bSBorislav Petkov * 27827f6d22bSBorislav Petkov * The event scheduler may not select the correct counter in the first 27927f6d22bSBorislav Petkov * cycle because it needs to know which subsequent events will be 28027f6d22bSBorislav Petkov * scheduled. It may fail to schedule the events then. So we set the 28127f6d22bSBorislav Petkov * overlap flag for such constraints to give the scheduler a hint which 28227f6d22bSBorislav Petkov * events to select for counter rescheduling. 28327f6d22bSBorislav Petkov * 28427f6d22bSBorislav Petkov * Care must be taken as the rescheduling algorithm is O(n!) which 28500f52685SIngo Molnar * will increase scheduling cycles for an over-committed system 28627f6d22bSBorislav Petkov * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros 28727f6d22bSBorislav Petkov * and its counter masks must be kept at a minimum. 28827f6d22bSBorislav Petkov */ 28927f6d22bSBorislav Petkov #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \ 29027f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0) 29127f6d22bSBorislav Petkov 29227f6d22bSBorislav Petkov /* 29327f6d22bSBorislav Petkov * Constraint on the Event code. 29427f6d22bSBorislav Petkov */ 29527f6d22bSBorislav Petkov #define INTEL_EVENT_CONSTRAINT(c, n) \ 29627f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) 29727f6d22bSBorislav Petkov 29827f6d22bSBorislav Petkov /* 29927f6d22bSBorislav Petkov * Constraint on the Event code + UMask + fixed-mask 30027f6d22bSBorislav Petkov * 30127f6d22bSBorislav Petkov * filter mask to validate fixed counter events. 30227f6d22bSBorislav Petkov * the following filters disqualify for fixed counters: 30327f6d22bSBorislav Petkov * - inv 30427f6d22bSBorislav Petkov * - edge 30527f6d22bSBorislav Petkov * - cnt-mask 30627f6d22bSBorislav Petkov * - in_tx 30727f6d22bSBorislav Petkov * - in_tx_checkpointed 30827f6d22bSBorislav Petkov * The other filters are supported by fixed counters. 30927f6d22bSBorislav Petkov * The any-thread option is supported starting with v3. 31027f6d22bSBorislav Petkov */ 31127f6d22bSBorislav Petkov #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED) 31227f6d22bSBorislav Petkov #define FIXED_EVENT_CONSTRAINT(c, n) \ 31327f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS) 31427f6d22bSBorislav Petkov 31527f6d22bSBorislav Petkov /* 31627f6d22bSBorislav Petkov * Constraint on the Event code + UMask 31727f6d22bSBorislav Petkov */ 31827f6d22bSBorislav Petkov #define INTEL_UEVENT_CONSTRAINT(c, n) \ 31927f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) 32027f6d22bSBorislav Petkov 32127f6d22bSBorislav Petkov /* Constraint on specific umask bit only + event */ 32227f6d22bSBorislav Petkov #define INTEL_UBIT_EVENT_CONSTRAINT(c, n) \ 32327f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT|(c)) 32427f6d22bSBorislav Petkov 32527f6d22bSBorislav Petkov /* Like UEVENT_CONSTRAINT, but match flags too */ 32627f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \ 32727f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS) 32827f6d22bSBorislav Petkov 32927f6d22bSBorislav Petkov #define INTEL_EXCLUEVT_CONSTRAINT(c, n) \ 33027f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \ 33127f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_EXCL) 33227f6d22bSBorislav Petkov 33327f6d22bSBorislav Petkov #define INTEL_PLD_CONSTRAINT(c, n) \ 33427f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 33527f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT) 33627f6d22bSBorislav Petkov 33727f6d22bSBorislav Petkov #define INTEL_PST_CONSTRAINT(c, n) \ 33827f6d22bSBorislav Petkov __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 33927f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST) 34027f6d22bSBorislav Petkov 34127f6d22bSBorislav Petkov /* Event constraint, but match on all event flags too. */ 34227f6d22bSBorislav Petkov #define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \ 34327f6d22bSBorislav Petkov EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS) 34427f6d22bSBorislav Petkov 34527f6d22bSBorislav Petkov /* Check only flags, but allow all event/umask */ 34627f6d22bSBorislav Petkov #define INTEL_ALL_EVENT_CONSTRAINT(code, n) \ 34727f6d22bSBorislav Petkov EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS) 34827f6d22bSBorislav Petkov 34927f6d22bSBorislav Petkov /* Check flags and event code, and set the HSW store flag */ 35027f6d22bSBorislav Petkov #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \ 35127f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 35227f6d22bSBorislav Petkov ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ 35327f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) 35427f6d22bSBorislav Petkov 35527f6d22bSBorislav Petkov /* Check flags and event code, and set the HSW load flag */ 35627f6d22bSBorislav Petkov #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \ 35727f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 35827f6d22bSBorislav Petkov ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ 35927f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) 36027f6d22bSBorislav Petkov 36127f6d22bSBorislav Petkov #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \ 36227f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 36327f6d22bSBorislav Petkov ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ 36427f6d22bSBorislav Petkov HWEIGHT(n), 0, \ 36527f6d22bSBorislav Petkov PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL) 36627f6d22bSBorislav Petkov 36727f6d22bSBorislav Petkov /* Check flags and event code/umask, and set the HSW store flag */ 36827f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \ 36927f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 37027f6d22bSBorislav Petkov INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 37127f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) 37227f6d22bSBorislav Petkov 37327f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \ 37427f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 37527f6d22bSBorislav Petkov INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 37627f6d22bSBorislav Petkov HWEIGHT(n), 0, \ 37727f6d22bSBorislav Petkov PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL) 37827f6d22bSBorislav Petkov 37927f6d22bSBorislav Petkov /* Check flags and event code/umask, and set the HSW load flag */ 38027f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \ 38127f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 38227f6d22bSBorislav Petkov INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 38327f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) 38427f6d22bSBorislav Petkov 38527f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \ 38627f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 38727f6d22bSBorislav Petkov INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 38827f6d22bSBorislav Petkov HWEIGHT(n), 0, \ 38927f6d22bSBorislav Petkov PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL) 39027f6d22bSBorislav Petkov 39127f6d22bSBorislav Petkov /* Check flags and event code/umask, and set the HSW N/A flag */ 39227f6d22bSBorislav Petkov #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \ 39327f6d22bSBorislav Petkov __EVENT_CONSTRAINT(code, n, \ 39427f6d22bSBorislav Petkov INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 39527f6d22bSBorislav Petkov HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW) 39627f6d22bSBorislav Petkov 39727f6d22bSBorislav Petkov 39827f6d22bSBorislav Petkov /* 39927f6d22bSBorislav Petkov * We define the end marker as having a weight of -1 40027f6d22bSBorislav Petkov * to enable blacklisting of events using a counter bitmask 40127f6d22bSBorislav Petkov * of zero and thus a weight of zero. 40227f6d22bSBorislav Petkov * The end marker has a weight that cannot possibly be 40327f6d22bSBorislav Petkov * obtained from counting the bits in the bitmask. 40427f6d22bSBorislav Petkov */ 40527f6d22bSBorislav Petkov #define EVENT_CONSTRAINT_END { .weight = -1 } 40627f6d22bSBorislav Petkov 40727f6d22bSBorislav Petkov /* 40827f6d22bSBorislav Petkov * Check for end marker with weight == -1 40927f6d22bSBorislav Petkov */ 41027f6d22bSBorislav Petkov #define for_each_event_constraint(e, c) \ 41127f6d22bSBorislav Petkov for ((e) = (c); (e)->weight != -1; (e)++) 41227f6d22bSBorislav Petkov 41327f6d22bSBorislav Petkov /* 41427f6d22bSBorislav Petkov * Extra registers for specific events. 41527f6d22bSBorislav Petkov * 41627f6d22bSBorislav Petkov * Some events need large masks and require external MSRs. 41727f6d22bSBorislav Petkov * Those extra MSRs end up being shared for all events on 41827f6d22bSBorislav Petkov * a PMU and sometimes between PMU of sibling HT threads. 41927f6d22bSBorislav Petkov * In either case, the kernel needs to handle conflicting 42027f6d22bSBorislav Petkov * accesses to those extra, shared, regs. The data structure 42127f6d22bSBorislav Petkov * to manage those registers is stored in cpu_hw_event. 42227f6d22bSBorislav Petkov */ 42327f6d22bSBorislav Petkov struct extra_reg { 42427f6d22bSBorislav Petkov unsigned int event; 42527f6d22bSBorislav Petkov unsigned int msr; 42627f6d22bSBorislav Petkov u64 config_mask; 42727f6d22bSBorislav Petkov u64 valid_mask; 42827f6d22bSBorislav Petkov int idx; /* per_xxx->regs[] reg index */ 42927f6d22bSBorislav Petkov bool extra_msr_access; 43027f6d22bSBorislav Petkov }; 43127f6d22bSBorislav Petkov 43227f6d22bSBorislav Petkov #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \ 43327f6d22bSBorislav Petkov .event = (e), \ 43427f6d22bSBorislav Petkov .msr = (ms), \ 43527f6d22bSBorislav Petkov .config_mask = (m), \ 43627f6d22bSBorislav Petkov .valid_mask = (vm), \ 43727f6d22bSBorislav Petkov .idx = EXTRA_REG_##i, \ 43827f6d22bSBorislav Petkov .extra_msr_access = true, \ 43927f6d22bSBorislav Petkov } 44027f6d22bSBorislav Petkov 44127f6d22bSBorislav Petkov #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \ 44227f6d22bSBorislav Petkov EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx) 44327f6d22bSBorislav Petkov 44427f6d22bSBorislav Petkov #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \ 44527f6d22bSBorislav Petkov EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \ 44627f6d22bSBorislav Petkov ARCH_PERFMON_EVENTSEL_UMASK, vm, idx) 44727f6d22bSBorislav Petkov 44827f6d22bSBorislav Petkov #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \ 44927f6d22bSBorislav Petkov INTEL_UEVENT_EXTRA_REG(c, \ 45027f6d22bSBorislav Petkov MSR_PEBS_LD_LAT_THRESHOLD, \ 45127f6d22bSBorislav Petkov 0xffff, \ 45227f6d22bSBorislav Petkov LDLAT) 45327f6d22bSBorislav Petkov 45427f6d22bSBorislav Petkov #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0) 45527f6d22bSBorislav Petkov 45627f6d22bSBorislav Petkov union perf_capabilities { 45727f6d22bSBorislav Petkov struct { 45827f6d22bSBorislav Petkov u64 lbr_format:6; 45927f6d22bSBorislav Petkov u64 pebs_trap:1; 46027f6d22bSBorislav Petkov u64 pebs_arch_reg:1; 46127f6d22bSBorislav Petkov u64 pebs_format:4; 46227f6d22bSBorislav Petkov u64 smm_freeze:1; 46327f6d22bSBorislav Petkov /* 46427f6d22bSBorislav Petkov * PMU supports separate counter range for writing 46527f6d22bSBorislav Petkov * values > 32bit. 46627f6d22bSBorislav Petkov */ 46727f6d22bSBorislav Petkov u64 full_width_write:1; 46827f6d22bSBorislav Petkov }; 46927f6d22bSBorislav Petkov u64 capabilities; 47027f6d22bSBorislav Petkov }; 47127f6d22bSBorislav Petkov 47227f6d22bSBorislav Petkov struct x86_pmu_quirk { 47327f6d22bSBorislav Petkov struct x86_pmu_quirk *next; 47427f6d22bSBorislav Petkov void (*func)(void); 47527f6d22bSBorislav Petkov }; 47627f6d22bSBorislav Petkov 47727f6d22bSBorislav Petkov union x86_pmu_config { 47827f6d22bSBorislav Petkov struct { 47927f6d22bSBorislav Petkov u64 event:8, 48027f6d22bSBorislav Petkov umask:8, 48127f6d22bSBorislav Petkov usr:1, 48227f6d22bSBorislav Petkov os:1, 48327f6d22bSBorislav Petkov edge:1, 48427f6d22bSBorislav Petkov pc:1, 48527f6d22bSBorislav Petkov interrupt:1, 48627f6d22bSBorislav Petkov __reserved1:1, 48727f6d22bSBorislav Petkov en:1, 48827f6d22bSBorislav Petkov inv:1, 48927f6d22bSBorislav Petkov cmask:8, 49027f6d22bSBorislav Petkov event2:4, 49127f6d22bSBorislav Petkov __reserved2:4, 49227f6d22bSBorislav Petkov go:1, 49327f6d22bSBorislav Petkov ho:1; 49427f6d22bSBorislav Petkov } bits; 49527f6d22bSBorislav Petkov u64 value; 49627f6d22bSBorislav Petkov }; 49727f6d22bSBorislav Petkov 49827f6d22bSBorislav Petkov #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value 49927f6d22bSBorislav Petkov 50027f6d22bSBorislav Petkov enum { 50127f6d22bSBorislav Petkov x86_lbr_exclusive_lbr, 50227f6d22bSBorislav Petkov x86_lbr_exclusive_bts, 50327f6d22bSBorislav Petkov x86_lbr_exclusive_pt, 50427f6d22bSBorislav Petkov x86_lbr_exclusive_max, 50527f6d22bSBorislav Petkov }; 50627f6d22bSBorislav Petkov 50727f6d22bSBorislav Petkov /* 50827f6d22bSBorislav Petkov * struct x86_pmu - generic x86 pmu 50927f6d22bSBorislav Petkov */ 51027f6d22bSBorislav Petkov struct x86_pmu { 51127f6d22bSBorislav Petkov /* 51227f6d22bSBorislav Petkov * Generic x86 PMC bits 51327f6d22bSBorislav Petkov */ 51427f6d22bSBorislav Petkov const char *name; 51527f6d22bSBorislav Petkov int version; 51627f6d22bSBorislav Petkov int (*handle_irq)(struct pt_regs *); 51727f6d22bSBorislav Petkov void (*disable_all)(void); 51827f6d22bSBorislav Petkov void (*enable_all)(int added); 51927f6d22bSBorislav Petkov void (*enable)(struct perf_event *); 52027f6d22bSBorislav Petkov void (*disable)(struct perf_event *); 52168f7082fSPeter Zijlstra void (*add)(struct perf_event *); 52268f7082fSPeter Zijlstra void (*del)(struct perf_event *); 52327f6d22bSBorislav Petkov int (*hw_config)(struct perf_event *event); 52427f6d22bSBorislav Petkov int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); 52527f6d22bSBorislav Petkov unsigned eventsel; 52627f6d22bSBorislav Petkov unsigned perfctr; 52727f6d22bSBorislav Petkov int (*addr_offset)(int index, bool eventsel); 52827f6d22bSBorislav Petkov int (*rdpmc_index)(int index); 52927f6d22bSBorislav Petkov u64 (*event_map)(int); 53027f6d22bSBorislav Petkov int max_events; 53127f6d22bSBorislav Petkov int num_counters; 53227f6d22bSBorislav Petkov int num_counters_fixed; 53327f6d22bSBorislav Petkov int cntval_bits; 53427f6d22bSBorislav Petkov u64 cntval_mask; 53527f6d22bSBorislav Petkov union { 53627f6d22bSBorislav Petkov unsigned long events_maskl; 53727f6d22bSBorislav Petkov unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)]; 53827f6d22bSBorislav Petkov }; 53927f6d22bSBorislav Petkov int events_mask_len; 54027f6d22bSBorislav Petkov int apic; 54127f6d22bSBorislav Petkov u64 max_period; 54227f6d22bSBorislav Petkov struct event_constraint * 54327f6d22bSBorislav Petkov (*get_event_constraints)(struct cpu_hw_events *cpuc, 54427f6d22bSBorislav Petkov int idx, 54527f6d22bSBorislav Petkov struct perf_event *event); 54627f6d22bSBorislav Petkov 54727f6d22bSBorislav Petkov void (*put_event_constraints)(struct cpu_hw_events *cpuc, 54827f6d22bSBorislav Petkov struct perf_event *event); 54927f6d22bSBorislav Petkov 55027f6d22bSBorislav Petkov void (*start_scheduling)(struct cpu_hw_events *cpuc); 55127f6d22bSBorislav Petkov 55227f6d22bSBorislav Petkov void (*commit_scheduling)(struct cpu_hw_events *cpuc, int idx, int cntr); 55327f6d22bSBorislav Petkov 55427f6d22bSBorislav Petkov void (*stop_scheduling)(struct cpu_hw_events *cpuc); 55527f6d22bSBorislav Petkov 55627f6d22bSBorislav Petkov struct event_constraint *event_constraints; 55727f6d22bSBorislav Petkov struct x86_pmu_quirk *quirks; 55827f6d22bSBorislav Petkov int perfctr_second_write; 55927f6d22bSBorislav Petkov bool late_ack; 56027f6d22bSBorislav Petkov unsigned (*limit_period)(struct perf_event *event, unsigned l); 56127f6d22bSBorislav Petkov 56227f6d22bSBorislav Petkov /* 56327f6d22bSBorislav Petkov * sysfs attrs 56427f6d22bSBorislav Petkov */ 56527f6d22bSBorislav Petkov int attr_rdpmc_broken; 56627f6d22bSBorislav Petkov int attr_rdpmc; 56727f6d22bSBorislav Petkov struct attribute **format_attrs; 56827f6d22bSBorislav Petkov struct attribute **event_attrs; 569b00233b5SAndi Kleen struct attribute **caps_attrs; 57027f6d22bSBorislav Petkov 57127f6d22bSBorislav Petkov ssize_t (*events_sysfs_show)(char *page, u64 config); 57227f6d22bSBorislav Petkov struct attribute **cpu_events; 57327f6d22bSBorislav Petkov 5746089327fSKan Liang unsigned long attr_freeze_on_smi; 5756089327fSKan Liang struct attribute **attrs; 5766089327fSKan Liang 57727f6d22bSBorislav Petkov /* 57827f6d22bSBorislav Petkov * CPU Hotplug hooks 57927f6d22bSBorislav Petkov */ 58027f6d22bSBorislav Petkov int (*cpu_prepare)(int cpu); 58127f6d22bSBorislav Petkov void (*cpu_starting)(int cpu); 58227f6d22bSBorislav Petkov void (*cpu_dying)(int cpu); 58327f6d22bSBorislav Petkov void (*cpu_dead)(int cpu); 58427f6d22bSBorislav Petkov 58527f6d22bSBorislav Petkov void (*check_microcode)(void); 58627f6d22bSBorislav Petkov void (*sched_task)(struct perf_event_context *ctx, 58727f6d22bSBorislav Petkov bool sched_in); 58827f6d22bSBorislav Petkov 58927f6d22bSBorislav Petkov /* 59027f6d22bSBorislav Petkov * Intel Arch Perfmon v2+ 59127f6d22bSBorislav Petkov */ 59227f6d22bSBorislav Petkov u64 intel_ctrl; 59327f6d22bSBorislav Petkov union perf_capabilities intel_cap; 59427f6d22bSBorislav Petkov 59527f6d22bSBorislav Petkov /* 59627f6d22bSBorislav Petkov * Intel DebugStore bits 59727f6d22bSBorislav Petkov */ 59827f6d22bSBorislav Petkov unsigned int bts :1, 59927f6d22bSBorislav Petkov bts_active :1, 60027f6d22bSBorislav Petkov pebs :1, 60127f6d22bSBorislav Petkov pebs_active :1, 60227f6d22bSBorislav Petkov pebs_broken :1, 60395298355SAndi Kleen pebs_prec_dist :1, 60495298355SAndi Kleen pebs_no_tlb :1; 60527f6d22bSBorislav Petkov int pebs_record_size; 606e72daf3fSJiri Olsa int pebs_buffer_size; 60727f6d22bSBorislav Petkov void (*drain_pebs)(struct pt_regs *regs); 60827f6d22bSBorislav Petkov struct event_constraint *pebs_constraints; 60927f6d22bSBorislav Petkov void (*pebs_aliases)(struct perf_event *event); 61027f6d22bSBorislav Petkov int max_pebs_events; 611*174afc3eSKan Liang unsigned long large_pebs_flags; 61227f6d22bSBorislav Petkov 61327f6d22bSBorislav Petkov /* 61427f6d22bSBorislav Petkov * Intel LBR 61527f6d22bSBorislav Petkov */ 61627f6d22bSBorislav Petkov unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ 61727f6d22bSBorislav Petkov int lbr_nr; /* hardware stack size */ 61827f6d22bSBorislav Petkov u64 lbr_sel_mask; /* LBR_SELECT valid bits */ 61927f6d22bSBorislav Petkov const int *lbr_sel_map; /* lbr_select mappings */ 62027f6d22bSBorislav Petkov bool lbr_double_abort; /* duplicated lbr aborts */ 621b0c1ef52SAndi Kleen bool lbr_pt_coexist; /* (LBR|BTS) may coexist with PT */ 62227f6d22bSBorislav Petkov 62327f6d22bSBorislav Petkov /* 62427f6d22bSBorislav Petkov * Intel PT/LBR/BTS are exclusive 62527f6d22bSBorislav Petkov */ 62627f6d22bSBorislav Petkov atomic_t lbr_exclusive[x86_lbr_exclusive_max]; 62727f6d22bSBorislav Petkov 62827f6d22bSBorislav Petkov /* 62932b62f44SPeter Zijlstra * AMD bits 63032b62f44SPeter Zijlstra */ 63132b62f44SPeter Zijlstra unsigned int amd_nb_constraints : 1; 63232b62f44SPeter Zijlstra 63332b62f44SPeter Zijlstra /* 63427f6d22bSBorislav Petkov * Extra registers for events 63527f6d22bSBorislav Petkov */ 63627f6d22bSBorislav Petkov struct extra_reg *extra_regs; 63727f6d22bSBorislav Petkov unsigned int flags; 63827f6d22bSBorislav Petkov 63927f6d22bSBorislav Petkov /* 64027f6d22bSBorislav Petkov * Intel host/guest support (KVM) 64127f6d22bSBorislav Petkov */ 64227f6d22bSBorislav Petkov struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr); 64327f6d22bSBorislav Petkov }; 64427f6d22bSBorislav Petkov 64527f6d22bSBorislav Petkov struct x86_perf_task_context { 64627f6d22bSBorislav Petkov u64 lbr_from[MAX_LBR_ENTRIES]; 64727f6d22bSBorislav Petkov u64 lbr_to[MAX_LBR_ENTRIES]; 64827f6d22bSBorislav Petkov u64 lbr_info[MAX_LBR_ENTRIES]; 64927f6d22bSBorislav Petkov int tos; 65027f6d22bSBorislav Petkov int lbr_callstack_users; 65127f6d22bSBorislav Petkov int lbr_stack_state; 65227f6d22bSBorislav Petkov }; 65327f6d22bSBorislav Petkov 65427f6d22bSBorislav Petkov #define x86_add_quirk(func_) \ 65527f6d22bSBorislav Petkov do { \ 65627f6d22bSBorislav Petkov static struct x86_pmu_quirk __quirk __initdata = { \ 65727f6d22bSBorislav Petkov .func = func_, \ 65827f6d22bSBorislav Petkov }; \ 65927f6d22bSBorislav Petkov __quirk.next = x86_pmu.quirks; \ 66027f6d22bSBorislav Petkov x86_pmu.quirks = &__quirk; \ 66127f6d22bSBorislav Petkov } while (0) 66227f6d22bSBorislav Petkov 66327f6d22bSBorislav Petkov /* 66427f6d22bSBorislav Petkov * x86_pmu flags 66527f6d22bSBorislav Petkov */ 66627f6d22bSBorislav Petkov #define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */ 66727f6d22bSBorislav Petkov #define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */ 66827f6d22bSBorislav Petkov #define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */ 66927f6d22bSBorislav Petkov #define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */ 67027f6d22bSBorislav Petkov 67127f6d22bSBorislav Petkov #define EVENT_VAR(_id) event_attr_##_id 67227f6d22bSBorislav Petkov #define EVENT_PTR(_id) &event_attr_##_id.attr.attr 67327f6d22bSBorislav Petkov 67427f6d22bSBorislav Petkov #define EVENT_ATTR(_name, _id) \ 67527f6d22bSBorislav Petkov static struct perf_pmu_events_attr EVENT_VAR(_id) = { \ 67627f6d22bSBorislav Petkov .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ 67727f6d22bSBorislav Petkov .id = PERF_COUNT_HW_##_id, \ 67827f6d22bSBorislav Petkov .event_str = NULL, \ 67927f6d22bSBorislav Petkov }; 68027f6d22bSBorislav Petkov 68127f6d22bSBorislav Petkov #define EVENT_ATTR_STR(_name, v, str) \ 68227f6d22bSBorislav Petkov static struct perf_pmu_events_attr event_attr_##v = { \ 68327f6d22bSBorislav Petkov .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ 68427f6d22bSBorislav Petkov .id = 0, \ 68527f6d22bSBorislav Petkov .event_str = str, \ 68627f6d22bSBorislav Petkov }; 68727f6d22bSBorislav Petkov 688fc07e9f9SAndi Kleen #define EVENT_ATTR_STR_HT(_name, v, noht, ht) \ 689fc07e9f9SAndi Kleen static struct perf_pmu_events_ht_attr event_attr_##v = { \ 690fc07e9f9SAndi Kleen .attr = __ATTR(_name, 0444, events_ht_sysfs_show, NULL),\ 691fc07e9f9SAndi Kleen .id = 0, \ 692fc07e9f9SAndi Kleen .event_str_noht = noht, \ 693fc07e9f9SAndi Kleen .event_str_ht = ht, \ 694fc07e9f9SAndi Kleen } 695fc07e9f9SAndi Kleen 69627f6d22bSBorislav Petkov extern struct x86_pmu x86_pmu __read_mostly; 69727f6d22bSBorislav Petkov 69827f6d22bSBorislav Petkov static inline bool x86_pmu_has_lbr_callstack(void) 69927f6d22bSBorislav Petkov { 70027f6d22bSBorislav Petkov return x86_pmu.lbr_sel_map && 70127f6d22bSBorislav Petkov x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0; 70227f6d22bSBorislav Petkov } 70327f6d22bSBorislav Petkov 70427f6d22bSBorislav Petkov DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); 70527f6d22bSBorislav Petkov 70627f6d22bSBorislav Petkov int x86_perf_event_set_period(struct perf_event *event); 70727f6d22bSBorislav Petkov 70827f6d22bSBorislav Petkov /* 70927f6d22bSBorislav Petkov * Generalized hw caching related hw_event table, filled 71027f6d22bSBorislav Petkov * in on a per model basis. A value of 0 means 71127f6d22bSBorislav Petkov * 'not supported', -1 means 'hw_event makes no sense on 71227f6d22bSBorislav Petkov * this CPU', any other value means the raw hw_event 71327f6d22bSBorislav Petkov * ID. 71427f6d22bSBorislav Petkov */ 71527f6d22bSBorislav Petkov 71627f6d22bSBorislav Petkov #define C(x) PERF_COUNT_HW_CACHE_##x 71727f6d22bSBorislav Petkov 71827f6d22bSBorislav Petkov extern u64 __read_mostly hw_cache_event_ids 71927f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_MAX] 72027f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX] 72127f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX]; 72227f6d22bSBorislav Petkov extern u64 __read_mostly hw_cache_extra_regs 72327f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_MAX] 72427f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_OP_MAX] 72527f6d22bSBorislav Petkov [PERF_COUNT_HW_CACHE_RESULT_MAX]; 72627f6d22bSBorislav Petkov 72727f6d22bSBorislav Petkov u64 x86_perf_event_update(struct perf_event *event); 72827f6d22bSBorislav Petkov 72927f6d22bSBorislav Petkov static inline unsigned int x86_pmu_config_addr(int index) 73027f6d22bSBorislav Petkov { 73127f6d22bSBorislav Petkov return x86_pmu.eventsel + (x86_pmu.addr_offset ? 73227f6d22bSBorislav Petkov x86_pmu.addr_offset(index, true) : index); 73327f6d22bSBorislav Petkov } 73427f6d22bSBorislav Petkov 73527f6d22bSBorislav Petkov static inline unsigned int x86_pmu_event_addr(int index) 73627f6d22bSBorislav Petkov { 73727f6d22bSBorislav Petkov return x86_pmu.perfctr + (x86_pmu.addr_offset ? 73827f6d22bSBorislav Petkov x86_pmu.addr_offset(index, false) : index); 73927f6d22bSBorislav Petkov } 74027f6d22bSBorislav Petkov 74127f6d22bSBorislav Petkov static inline int x86_pmu_rdpmc_index(int index) 74227f6d22bSBorislav Petkov { 74327f6d22bSBorislav Petkov return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index; 74427f6d22bSBorislav Petkov } 74527f6d22bSBorislav Petkov 74627f6d22bSBorislav Petkov int x86_add_exclusive(unsigned int what); 74727f6d22bSBorislav Petkov 74827f6d22bSBorislav Petkov void x86_del_exclusive(unsigned int what); 74927f6d22bSBorislav Petkov 75027f6d22bSBorislav Petkov int x86_reserve_hardware(void); 75127f6d22bSBorislav Petkov 75227f6d22bSBorislav Petkov void x86_release_hardware(void); 75327f6d22bSBorislav Petkov 754b00233b5SAndi Kleen int x86_pmu_max_precise(void); 755b00233b5SAndi Kleen 75627f6d22bSBorislav Petkov void hw_perf_lbr_event_destroy(struct perf_event *event); 75727f6d22bSBorislav Petkov 75827f6d22bSBorislav Petkov int x86_setup_perfctr(struct perf_event *event); 75927f6d22bSBorislav Petkov 76027f6d22bSBorislav Petkov int x86_pmu_hw_config(struct perf_event *event); 76127f6d22bSBorislav Petkov 76227f6d22bSBorislav Petkov void x86_pmu_disable_all(void); 76327f6d22bSBorislav Petkov 76427f6d22bSBorislav Petkov static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, 76527f6d22bSBorislav Petkov u64 enable_mask) 76627f6d22bSBorislav Petkov { 76727f6d22bSBorislav Petkov u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask); 76827f6d22bSBorislav Petkov 76927f6d22bSBorislav Petkov if (hwc->extra_reg.reg) 77027f6d22bSBorislav Petkov wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config); 77127f6d22bSBorislav Petkov wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask); 77227f6d22bSBorislav Petkov } 77327f6d22bSBorislav Petkov 77427f6d22bSBorislav Petkov void x86_pmu_enable_all(int added); 77527f6d22bSBorislav Petkov 77627f6d22bSBorislav Petkov int perf_assign_events(struct event_constraint **constraints, int n, 77727f6d22bSBorislav Petkov int wmin, int wmax, int gpmax, int *assign); 77827f6d22bSBorislav Petkov int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign); 77927f6d22bSBorislav Petkov 78027f6d22bSBorislav Petkov void x86_pmu_stop(struct perf_event *event, int flags); 78127f6d22bSBorislav Petkov 78227f6d22bSBorislav Petkov static inline void x86_pmu_disable_event(struct perf_event *event) 78327f6d22bSBorislav Petkov { 78427f6d22bSBorislav Petkov struct hw_perf_event *hwc = &event->hw; 78527f6d22bSBorislav Petkov 78627f6d22bSBorislav Petkov wrmsrl(hwc->config_base, hwc->config); 78727f6d22bSBorislav Petkov } 78827f6d22bSBorislav Petkov 78927f6d22bSBorislav Petkov void x86_pmu_enable_event(struct perf_event *event); 79027f6d22bSBorislav Petkov 79127f6d22bSBorislav Petkov int x86_pmu_handle_irq(struct pt_regs *regs); 79227f6d22bSBorislav Petkov 79327f6d22bSBorislav Petkov extern struct event_constraint emptyconstraint; 79427f6d22bSBorislav Petkov 79527f6d22bSBorislav Petkov extern struct event_constraint unconstrained; 79627f6d22bSBorislav Petkov 79727f6d22bSBorislav Petkov static inline bool kernel_ip(unsigned long ip) 79827f6d22bSBorislav Petkov { 79927f6d22bSBorislav Petkov #ifdef CONFIG_X86_32 80027f6d22bSBorislav Petkov return ip > PAGE_OFFSET; 80127f6d22bSBorislav Petkov #else 80227f6d22bSBorislav Petkov return (long)ip < 0; 80327f6d22bSBorislav Petkov #endif 80427f6d22bSBorislav Petkov } 80527f6d22bSBorislav Petkov 80627f6d22bSBorislav Petkov /* 80727f6d22bSBorislav Petkov * Not all PMUs provide the right context information to place the reported IP 80827f6d22bSBorislav Petkov * into full context. Specifically segment registers are typically not 80927f6d22bSBorislav Petkov * supplied. 81027f6d22bSBorislav Petkov * 81127f6d22bSBorislav Petkov * Assuming the address is a linear address (it is for IBS), we fake the CS and 81227f6d22bSBorislav Petkov * vm86 mode using the known zero-based code segment and 'fix up' the registers 81327f6d22bSBorislav Petkov * to reflect this. 81427f6d22bSBorislav Petkov * 81527f6d22bSBorislav Petkov * Intel PEBS/LBR appear to typically provide the effective address, nothing 81627f6d22bSBorislav Petkov * much we can do about that but pray and treat it like a linear address. 81727f6d22bSBorislav Petkov */ 81827f6d22bSBorislav Petkov static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip) 81927f6d22bSBorislav Petkov { 82027f6d22bSBorislav Petkov regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS; 82127f6d22bSBorislav Petkov if (regs->flags & X86_VM_MASK) 82227f6d22bSBorislav Petkov regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK); 82327f6d22bSBorislav Petkov regs->ip = ip; 82427f6d22bSBorislav Petkov } 82527f6d22bSBorislav Petkov 82627f6d22bSBorislav Petkov ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event); 82727f6d22bSBorislav Petkov ssize_t intel_event_sysfs_show(char *page, u64 config); 82827f6d22bSBorislav Petkov 82927f6d22bSBorislav Petkov struct attribute **merge_attr(struct attribute **a, struct attribute **b); 83027f6d22bSBorislav Petkov 831a49ac9f8SHuang Rui ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, 832a49ac9f8SHuang Rui char *page); 833fc07e9f9SAndi Kleen ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr, 834fc07e9f9SAndi Kleen char *page); 835a49ac9f8SHuang Rui 83627f6d22bSBorislav Petkov #ifdef CONFIG_CPU_SUP_AMD 83727f6d22bSBorislav Petkov 83827f6d22bSBorislav Petkov int amd_pmu_init(void); 83927f6d22bSBorislav Petkov 84027f6d22bSBorislav Petkov #else /* CONFIG_CPU_SUP_AMD */ 84127f6d22bSBorislav Petkov 84227f6d22bSBorislav Petkov static inline int amd_pmu_init(void) 84327f6d22bSBorislav Petkov { 84427f6d22bSBorislav Petkov return 0; 84527f6d22bSBorislav Petkov } 84627f6d22bSBorislav Petkov 84727f6d22bSBorislav Petkov #endif /* CONFIG_CPU_SUP_AMD */ 84827f6d22bSBorislav Petkov 84927f6d22bSBorislav Petkov #ifdef CONFIG_CPU_SUP_INTEL 85027f6d22bSBorislav Petkov 85127f6d22bSBorislav Petkov static inline bool intel_pmu_has_bts(struct perf_event *event) 85227f6d22bSBorislav Petkov { 85327f6d22bSBorislav Petkov if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS && 85427f6d22bSBorislav Petkov !event->attr.freq && event->hw.sample_period == 1) 85527f6d22bSBorislav Petkov return true; 85627f6d22bSBorislav Petkov 85727f6d22bSBorislav Petkov return false; 85827f6d22bSBorislav Petkov } 85927f6d22bSBorislav Petkov 86027f6d22bSBorislav Petkov int intel_pmu_save_and_restart(struct perf_event *event); 86127f6d22bSBorislav Petkov 86227f6d22bSBorislav Petkov struct event_constraint * 86327f6d22bSBorislav Petkov x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx, 86427f6d22bSBorislav Petkov struct perf_event *event); 86527f6d22bSBorislav Petkov 86627f6d22bSBorislav Petkov struct intel_shared_regs *allocate_shared_regs(int cpu); 86727f6d22bSBorislav Petkov 86827f6d22bSBorislav Petkov int intel_pmu_init(void); 86927f6d22bSBorislav Petkov 87027f6d22bSBorislav Petkov void init_debug_store_on_cpu(int cpu); 87127f6d22bSBorislav Petkov 87227f6d22bSBorislav Petkov void fini_debug_store_on_cpu(int cpu); 87327f6d22bSBorislav Petkov 87427f6d22bSBorislav Petkov void release_ds_buffers(void); 87527f6d22bSBorislav Petkov 87627f6d22bSBorislav Petkov void reserve_ds_buffers(void); 87727f6d22bSBorislav Petkov 87827f6d22bSBorislav Petkov extern struct event_constraint bts_constraint; 87927f6d22bSBorislav Petkov 88027f6d22bSBorislav Petkov void intel_pmu_enable_bts(u64 config); 88127f6d22bSBorislav Petkov 88227f6d22bSBorislav Petkov void intel_pmu_disable_bts(void); 88327f6d22bSBorislav Petkov 88427f6d22bSBorislav Petkov int intel_pmu_drain_bts_buffer(void); 88527f6d22bSBorislav Petkov 88627f6d22bSBorislav Petkov extern struct event_constraint intel_core2_pebs_event_constraints[]; 88727f6d22bSBorislav Petkov 88827f6d22bSBorislav Petkov extern struct event_constraint intel_atom_pebs_event_constraints[]; 88927f6d22bSBorislav Petkov 89027f6d22bSBorislav Petkov extern struct event_constraint intel_slm_pebs_event_constraints[]; 89127f6d22bSBorislav Petkov 8928b92c3a7SKan Liang extern struct event_constraint intel_glm_pebs_event_constraints[]; 8938b92c3a7SKan Liang 894dd0b06b5SKan Liang extern struct event_constraint intel_glp_pebs_event_constraints[]; 895dd0b06b5SKan Liang 89627f6d22bSBorislav Petkov extern struct event_constraint intel_nehalem_pebs_event_constraints[]; 89727f6d22bSBorislav Petkov 89827f6d22bSBorislav Petkov extern struct event_constraint intel_westmere_pebs_event_constraints[]; 89927f6d22bSBorislav Petkov 90027f6d22bSBorislav Petkov extern struct event_constraint intel_snb_pebs_event_constraints[]; 90127f6d22bSBorislav Petkov 90227f6d22bSBorislav Petkov extern struct event_constraint intel_ivb_pebs_event_constraints[]; 90327f6d22bSBorislav Petkov 90427f6d22bSBorislav Petkov extern struct event_constraint intel_hsw_pebs_event_constraints[]; 90527f6d22bSBorislav Petkov 906b3e62463SStephane Eranian extern struct event_constraint intel_bdw_pebs_event_constraints[]; 907b3e62463SStephane Eranian 90827f6d22bSBorislav Petkov extern struct event_constraint intel_skl_pebs_event_constraints[]; 90927f6d22bSBorislav Petkov 91027f6d22bSBorislav Petkov struct event_constraint *intel_pebs_constraints(struct perf_event *event); 91127f6d22bSBorislav Petkov 91268f7082fSPeter Zijlstra void intel_pmu_pebs_add(struct perf_event *event); 91368f7082fSPeter Zijlstra 91468f7082fSPeter Zijlstra void intel_pmu_pebs_del(struct perf_event *event); 91568f7082fSPeter Zijlstra 91627f6d22bSBorislav Petkov void intel_pmu_pebs_enable(struct perf_event *event); 91727f6d22bSBorislav Petkov 91827f6d22bSBorislav Petkov void intel_pmu_pebs_disable(struct perf_event *event); 91927f6d22bSBorislav Petkov 92027f6d22bSBorislav Petkov void intel_pmu_pebs_enable_all(void); 92127f6d22bSBorislav Petkov 92227f6d22bSBorislav Petkov void intel_pmu_pebs_disable_all(void); 92327f6d22bSBorislav Petkov 92427f6d22bSBorislav Petkov void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in); 92527f6d22bSBorislav Petkov 92627f6d22bSBorislav Petkov void intel_ds_init(void); 92727f6d22bSBorislav Petkov 92827f6d22bSBorislav Petkov void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in); 92927f6d22bSBorislav Petkov 93019fc9dddSDavid Carrillo-Cisneros u64 lbr_from_signext_quirk_wr(u64 val); 93119fc9dddSDavid Carrillo-Cisneros 93227f6d22bSBorislav Petkov void intel_pmu_lbr_reset(void); 93327f6d22bSBorislav Petkov 93468f7082fSPeter Zijlstra void intel_pmu_lbr_add(struct perf_event *event); 93527f6d22bSBorislav Petkov 93668f7082fSPeter Zijlstra void intel_pmu_lbr_del(struct perf_event *event); 93727f6d22bSBorislav Petkov 93827f6d22bSBorislav Petkov void intel_pmu_lbr_enable_all(bool pmi); 93927f6d22bSBorislav Petkov 94027f6d22bSBorislav Petkov void intel_pmu_lbr_disable_all(void); 94127f6d22bSBorislav Petkov 94227f6d22bSBorislav Petkov void intel_pmu_lbr_read(void); 94327f6d22bSBorislav Petkov 94427f6d22bSBorislav Petkov void intel_pmu_lbr_init_core(void); 94527f6d22bSBorislav Petkov 94627f6d22bSBorislav Petkov void intel_pmu_lbr_init_nhm(void); 94727f6d22bSBorislav Petkov 94827f6d22bSBorislav Petkov void intel_pmu_lbr_init_atom(void); 94927f6d22bSBorislav Petkov 950f21d5adcSKan Liang void intel_pmu_lbr_init_slm(void); 951f21d5adcSKan Liang 95227f6d22bSBorislav Petkov void intel_pmu_lbr_init_snb(void); 95327f6d22bSBorislav Petkov 95427f6d22bSBorislav Petkov void intel_pmu_lbr_init_hsw(void); 95527f6d22bSBorislav Petkov 95627f6d22bSBorislav Petkov void intel_pmu_lbr_init_skl(void); 95727f6d22bSBorislav Petkov 95827f6d22bSBorislav Petkov void intel_pmu_lbr_init_knl(void); 95927f6d22bSBorislav Petkov 960e17dc653SAndi Kleen void intel_pmu_pebs_data_source_nhm(void); 961e17dc653SAndi Kleen 9626ae5fa61SAndi Kleen void intel_pmu_pebs_data_source_skl(bool pmem); 9636ae5fa61SAndi Kleen 96427f6d22bSBorislav Petkov int intel_pmu_setup_lbr_filter(struct perf_event *event); 96527f6d22bSBorislav Petkov 96627f6d22bSBorislav Petkov void intel_pt_interrupt(void); 96727f6d22bSBorislav Petkov 96827f6d22bSBorislav Petkov int intel_bts_interrupt(void); 96927f6d22bSBorislav Petkov 97027f6d22bSBorislav Petkov void intel_bts_enable_local(void); 97127f6d22bSBorislav Petkov 97227f6d22bSBorislav Petkov void intel_bts_disable_local(void); 97327f6d22bSBorislav Petkov 97427f6d22bSBorislav Petkov int p4_pmu_init(void); 97527f6d22bSBorislav Petkov 97627f6d22bSBorislav Petkov int p6_pmu_init(void); 97727f6d22bSBorislav Petkov 97827f6d22bSBorislav Petkov int knc_pmu_init(void); 97927f6d22bSBorislav Petkov 98027f6d22bSBorislav Petkov static inline int is_ht_workaround_enabled(void) 98127f6d22bSBorislav Petkov { 98227f6d22bSBorislav Petkov return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED); 98327f6d22bSBorislav Petkov } 98427f6d22bSBorislav Petkov 98527f6d22bSBorislav Petkov #else /* CONFIG_CPU_SUP_INTEL */ 98627f6d22bSBorislav Petkov 98727f6d22bSBorislav Petkov static inline void reserve_ds_buffers(void) 98827f6d22bSBorislav Petkov { 98927f6d22bSBorislav Petkov } 99027f6d22bSBorislav Petkov 99127f6d22bSBorislav Petkov static inline void release_ds_buffers(void) 99227f6d22bSBorislav Petkov { 99327f6d22bSBorislav Petkov } 99427f6d22bSBorislav Petkov 99527f6d22bSBorislav Petkov static inline int intel_pmu_init(void) 99627f6d22bSBorislav Petkov { 99727f6d22bSBorislav Petkov return 0; 99827f6d22bSBorislav Petkov } 99927f6d22bSBorislav Petkov 100027f6d22bSBorislav Petkov static inline struct intel_shared_regs *allocate_shared_regs(int cpu) 100127f6d22bSBorislav Petkov { 100227f6d22bSBorislav Petkov return NULL; 100327f6d22bSBorislav Petkov } 100427f6d22bSBorislav Petkov 100527f6d22bSBorislav Petkov static inline int is_ht_workaround_enabled(void) 100627f6d22bSBorislav Petkov { 100727f6d22bSBorislav Petkov return 0; 100827f6d22bSBorislav Petkov } 100927f6d22bSBorislav Petkov #endif /* CONFIG_CPU_SUP_INTEL */ 1010