perf_event.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Performance events x86 architecture header
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #if 0
  16. #undef wrmsrl
  17. #define wrmsrl(msr, val) \
  18. do { \
  19. unsigned int _msr = (msr); \
  20. u64 _val = (val); \
  21. trace_printk("wrmsrl(%x, %Lx)\n", (unsigned int)(_msr), \
  22. (unsigned long long)(_val)); \
  23. native_write_msr((_msr), (u32)(_val), (u32)(_val >> 32)); \
  24. } while (0)
  25. #endif
  26. /*
  27. * | NHM/WSM | SNB |
  28. * register -------------------------------
  29. * | HT | no HT | HT | no HT |
  30. *-----------------------------------------
  31. * offcore | core | core | cpu | core |
  32. * lbr_sel | core | core | cpu | core |
  33. * ld_lat | cpu | core | cpu | core |
  34. *-----------------------------------------
  35. *
  36. * Given that there is a small number of shared regs,
  37. * we can pre-allocate their slot in the per-cpu
  38. * per-core reg tables.
  39. */
  40. enum extra_reg_type {
  41. EXTRA_REG_NONE = -1, /* not used */
  42. EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
  43. EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
  44. EXTRA_REG_LBR = 2, /* lbr_select */
  45. EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */
  46. EXTRA_REG_MAX /* number of entries needed */
  47. };
  48. struct event_constraint {
  49. union {
  50. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  51. u64 idxmsk64;
  52. };
  53. u64 code;
  54. u64 cmask;
  55. int weight;
  56. int overlap;
  57. int flags;
  58. };
  59. /*
  60. * struct hw_perf_event.flags flags
  61. */
  62. #define PERF_X86_EVENT_PEBS_LDLAT 0x0001 /* ld+ldlat data address sampling */
  63. #define PERF_X86_EVENT_PEBS_ST 0x0002 /* st data address sampling */
  64. #define PERF_X86_EVENT_PEBS_ST_HSW 0x0004 /* haswell style datala, store */
  65. #define PERF_X86_EVENT_COMMITTED 0x0008 /* event passed commit_txn */
  66. #define PERF_X86_EVENT_PEBS_LD_HSW 0x0010 /* haswell style datala, load */
  67. #define PERF_X86_EVENT_PEBS_NA_HSW 0x0020 /* haswell style datala, unknown */
  68. #define PERF_X86_EVENT_EXCL 0x0040 /* HT exclusivity on counter */
  69. #define PERF_X86_EVENT_DYNAMIC 0x0080 /* dynamic alloc'd constraint */
  70. #define PERF_X86_EVENT_RDPMC_ALLOWED 0x0100 /* grant rdpmc permission */
  71. #define PERF_X86_EVENT_EXCL_ACCT 0x0200 /* accounted EXCL event */
  72. struct amd_nb {
  73. int nb_id; /* NorthBridge id */
  74. int refcnt; /* reference count */
  75. struct perf_event *owners[X86_PMC_IDX_MAX];
  76. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  77. };
  78. /* The maximal number of PEBS events: */
  79. #define MAX_PEBS_EVENTS 8
  80. /*
  81. * A debug store configuration.
  82. *
  83. * We only support architectures that use 64bit fields.
  84. */
  85. struct debug_store {
  86. u64 bts_buffer_base;
  87. u64 bts_index;
  88. u64 bts_absolute_maximum;
  89. u64 bts_interrupt_threshold;
  90. u64 pebs_buffer_base;
  91. u64 pebs_index;
  92. u64 pebs_absolute_maximum;
  93. u64 pebs_interrupt_threshold;
  94. u64 pebs_event_reset[MAX_PEBS_EVENTS];
  95. };
  96. /*
  97. * Per register state.
  98. */
  99. struct er_account {
  100. raw_spinlock_t lock; /* per-core: protect structure */
  101. u64 config; /* extra MSR config */
  102. u64 reg; /* extra MSR number */
  103. atomic_t ref; /* reference count */
  104. };
  105. /*
  106. * Per core/cpu state
  107. *
  108. * Used to coordinate shared registers between HT threads or
  109. * among events on a single PMU.
  110. */
  111. struct intel_shared_regs {
  112. struct er_account regs[EXTRA_REG_MAX];
  113. int refcnt; /* per-core: #HT threads */
  114. unsigned core_id; /* per-core: core id */
  115. };
  116. enum intel_excl_state_type {
  117. INTEL_EXCL_UNUSED = 0, /* counter is unused */
  118. INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */
  119. INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */
  120. };
  121. struct intel_excl_states {
  122. enum intel_excl_state_type init_state[X86_PMC_IDX_MAX];
  123. enum intel_excl_state_type state[X86_PMC_IDX_MAX];
  124. bool sched_started; /* true if scheduling has started */
  125. };
  126. struct intel_excl_cntrs {
  127. raw_spinlock_t lock;
  128. struct intel_excl_states states[2];
  129. union {
  130. u16 has_exclusive[2];
  131. u32 exclusive_present;
  132. };
  133. int refcnt; /* per-core: #HT threads */
  134. unsigned core_id; /* per-core: core id */
  135. };
  136. #define MAX_LBR_ENTRIES 16
  137. enum {
  138. X86_PERF_KFREE_SHARED = 0,
  139. X86_PERF_KFREE_EXCL = 1,
  140. X86_PERF_KFREE_MAX
  141. };
  142. struct cpu_hw_events {
  143. /*
  144. * Generic x86 PMC bits
  145. */
  146. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  147. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  148. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  149. int enabled;
  150. int n_events; /* the # of events in the below arrays */
  151. int n_added; /* the # last events in the below arrays;
  152. they've never been enabled yet */
  153. int n_txn; /* the # last events in the below arrays;
  154. added in the current transaction */
  155. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  156. u64 tags[X86_PMC_IDX_MAX];
  157. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  158. struct event_constraint *event_constraint[X86_PMC_IDX_MAX];
  159. int n_excl; /* the number of exclusive events */
  160. unsigned int group_flag;
  161. int is_fake;
  162. /*
  163. * Intel DebugStore bits
  164. */
  165. struct debug_store *ds;
  166. u64 pebs_enabled;
  167. /*
  168. * Intel LBR bits
  169. */
  170. int lbr_users;
  171. void *lbr_context;
  172. struct perf_branch_stack lbr_stack;
  173. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  174. struct er_account *lbr_sel;
  175. u64 br_sel;
  176. /*
  177. * Intel host/guest exclude bits
  178. */
  179. u64 intel_ctrl_guest_mask;
  180. u64 intel_ctrl_host_mask;
  181. struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
  182. /*
  183. * Intel checkpoint mask
  184. */
  185. u64 intel_cp_status;
  186. /*
  187. * manage shared (per-core, per-cpu) registers
  188. * used on Intel NHM/WSM/SNB
  189. */
  190. struct intel_shared_regs *shared_regs;
  191. /*
  192. * manage exclusive counter access between hyperthread
  193. */
  194. struct event_constraint *constraint_list; /* in enable order */
  195. struct intel_excl_cntrs *excl_cntrs;
  196. int excl_thread_id; /* 0 or 1 */
  197. /*
  198. * AMD specific bits
  199. */
  200. struct amd_nb *amd_nb;
  201. /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
  202. u64 perf_ctr_virt_mask;
  203. void *kfree_on_online[X86_PERF_KFREE_MAX];
  204. };
  205. #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
  206. { .idxmsk64 = (n) }, \
  207. .code = (c), \
  208. .cmask = (m), \
  209. .weight = (w), \
  210. .overlap = (o), \
  211. .flags = f, \
  212. }
  213. #define EVENT_CONSTRAINT(c, n, m) \
  214. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
  215. #define INTEL_EXCLEVT_CONSTRAINT(c, n) \
  216. __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
  217. 0, PERF_X86_EVENT_EXCL)
  218. /*
  219. * The overlap flag marks event constraints with overlapping counter
  220. * masks. This is the case if the counter mask of such an event is not
  221. * a subset of any other counter mask of a constraint with an equal or
  222. * higher weight, e.g.:
  223. *
  224. * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  225. * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
  226. * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
  227. *
  228. * The event scheduler may not select the correct counter in the first
  229. * cycle because it needs to know which subsequent events will be
  230. * scheduled. It may fail to schedule the events then. So we set the
  231. * overlap flag for such constraints to give the scheduler a hint which
  232. * events to select for counter rescheduling.
  233. *
  234. * Care must be taken as the rescheduling algorithm is O(n!) which
  235. * will increase scheduling cycles for an over-commited system
  236. * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
  237. * and its counter masks must be kept at a minimum.
  238. */
  239. #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
  240. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
  241. /*
  242. * Constraint on the Event code.
  243. */
  244. #define INTEL_EVENT_CONSTRAINT(c, n) \
  245. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  246. /*
  247. * Constraint on the Event code + UMask + fixed-mask
  248. *
  249. * filter mask to validate fixed counter events.
  250. * the following filters disqualify for fixed counters:
  251. * - inv
  252. * - edge
  253. * - cnt-mask
  254. * - in_tx
  255. * - in_tx_checkpointed
  256. * The other filters are supported by fixed counters.
  257. * The any-thread option is supported starting with v3.
  258. */
  259. #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
  260. #define FIXED_EVENT_CONSTRAINT(c, n) \
  261. EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
  262. /*
  263. * Constraint on the Event code + UMask
  264. */
  265. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  266. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  267. /* Like UEVENT_CONSTRAINT, but match flags too */
  268. #define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
  269. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
  270. #define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
  271. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
  272. HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
  273. #define INTEL_PLD_CONSTRAINT(c, n) \
  274. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  275. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
  276. #define INTEL_PST_CONSTRAINT(c, n) \
  277. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  278. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
  279. /* Event constraint, but match on all event flags too. */
  280. #define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
  281. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
  282. /* Check only flags, but allow all event/umask */
  283. #define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
  284. EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
  285. /* Check flags and event code, and set the HSW store flag */
  286. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
  287. __EVENT_CONSTRAINT(code, n, \
  288. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  289. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  290. /* Check flags and event code, and set the HSW load flag */
  291. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
  292. __EVENT_CONSTRAINT(code, n, \
  293. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  294. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  295. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
  296. __EVENT_CONSTRAINT(code, n, \
  297. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  298. HWEIGHT(n), 0, \
  299. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  300. /* Check flags and event code/umask, and set the HSW store flag */
  301. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
  302. __EVENT_CONSTRAINT(code, n, \
  303. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  304. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  305. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
  306. __EVENT_CONSTRAINT(code, n, \
  307. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  308. HWEIGHT(n), 0, \
  309. PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
  310. /* Check flags and event code/umask, and set the HSW load flag */
  311. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
  312. __EVENT_CONSTRAINT(code, n, \
  313. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  314. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  315. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
  316. __EVENT_CONSTRAINT(code, n, \
  317. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  318. HWEIGHT(n), 0, \
  319. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  320. /* Check flags and event code/umask, and set the HSW N/A flag */
  321. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
  322. __EVENT_CONSTRAINT(code, n, \
  323. INTEL_ARCH_EVENT_MASK|INTEL_ARCH_EVENT_MASK, \
  324. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
  325. /*
  326. * We define the end marker as having a weight of -1
  327. * to enable blacklisting of events using a counter bitmask
  328. * of zero and thus a weight of zero.
  329. * The end marker has a weight that cannot possibly be
  330. * obtained from counting the bits in the bitmask.
  331. */
  332. #define EVENT_CONSTRAINT_END { .weight = -1 }
  333. /*
  334. * Check for end marker with weight == -1
  335. */
  336. #define for_each_event_constraint(e, c) \
  337. for ((e) = (c); (e)->weight != -1; (e)++)
  338. /*
  339. * Extra registers for specific events.
  340. *
  341. * Some events need large masks and require external MSRs.
  342. * Those extra MSRs end up being shared for all events on
  343. * a PMU and sometimes between PMU of sibling HT threads.
  344. * In either case, the kernel needs to handle conflicting
  345. * accesses to those extra, shared, regs. The data structure
  346. * to manage those registers is stored in cpu_hw_event.
  347. */
  348. struct extra_reg {
  349. unsigned int event;
  350. unsigned int msr;
  351. u64 config_mask;
  352. u64 valid_mask;
  353. int idx; /* per_xxx->regs[] reg index */
  354. bool extra_msr_access;
  355. };
  356. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  357. .event = (e), \
  358. .msr = (ms), \
  359. .config_mask = (m), \
  360. .valid_mask = (vm), \
  361. .idx = EXTRA_REG_##i, \
  362. .extra_msr_access = true, \
  363. }
  364. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  365. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  366. #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
  367. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
  368. ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
  369. #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
  370. INTEL_UEVENT_EXTRA_REG(c, \
  371. MSR_PEBS_LD_LAT_THRESHOLD, \
  372. 0xffff, \
  373. LDLAT)
  374. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  375. union perf_capabilities {
  376. struct {
  377. u64 lbr_format:6;
  378. u64 pebs_trap:1;
  379. u64 pebs_arch_reg:1;
  380. u64 pebs_format:4;
  381. u64 smm_freeze:1;
  382. /*
  383. * PMU supports separate counter range for writing
  384. * values > 32bit.
  385. */
  386. u64 full_width_write:1;
  387. };
  388. u64 capabilities;
  389. };
  390. struct x86_pmu_quirk {
  391. struct x86_pmu_quirk *next;
  392. void (*func)(void);
  393. };
  394. union x86_pmu_config {
  395. struct {
  396. u64 event:8,
  397. umask:8,
  398. usr:1,
  399. os:1,
  400. edge:1,
  401. pc:1,
  402. interrupt:1,
  403. __reserved1:1,
  404. en:1,
  405. inv:1,
  406. cmask:8,
  407. event2:4,
  408. __reserved2:4,
  409. go:1,
  410. ho:1;
  411. } bits;
  412. u64 value;
  413. };
  414. #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
  415. enum {
  416. x86_lbr_exclusive_lbr,
  417. x86_lbr_exclusive_bts,
  418. x86_lbr_exclusive_pt,
  419. x86_lbr_exclusive_max,
  420. };
  421. /*
  422. * struct x86_pmu - generic x86 pmu
  423. */
  424. struct x86_pmu {
  425. /*
  426. * Generic x86 PMC bits
  427. */
  428. const char *name;
  429. int version;
  430. int (*handle_irq)(struct pt_regs *);
  431. void (*disable_all)(void);
  432. void (*enable_all)(int added);
  433. void (*enable)(struct perf_event *);
  434. void (*disable)(struct perf_event *);
  435. int (*hw_config)(struct perf_event *event);
  436. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  437. unsigned eventsel;
  438. unsigned perfctr;
  439. int (*addr_offset)(int index, bool eventsel);
  440. int (*rdpmc_index)(int index);
  441. u64 (*event_map)(int);
  442. int max_events;
  443. int num_counters;
  444. int num_counters_fixed;
  445. int cntval_bits;
  446. u64 cntval_mask;
  447. union {
  448. unsigned long events_maskl;
  449. unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
  450. };
  451. int events_mask_len;
  452. int apic;
  453. u64 max_period;
  454. struct event_constraint *
  455. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  456. int idx,
  457. struct perf_event *event);
  458. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  459. struct perf_event *event);
  460. void (*commit_scheduling)(struct cpu_hw_events *cpuc, int idx, int cntr);
  461. void (*start_scheduling)(struct cpu_hw_events *cpuc);
  462. void (*stop_scheduling)(struct cpu_hw_events *cpuc);
  463. struct event_constraint *event_constraints;
  464. struct x86_pmu_quirk *quirks;
  465. int perfctr_second_write;
  466. bool late_ack;
  467. unsigned (*limit_period)(struct perf_event *event, unsigned l);
  468. /*
  469. * sysfs attrs
  470. */
  471. int attr_rdpmc_broken;
  472. int attr_rdpmc;
  473. struct attribute **format_attrs;
  474. struct attribute **event_attrs;
  475. ssize_t (*events_sysfs_show)(char *page, u64 config);
  476. struct attribute **cpu_events;
  477. /*
  478. * CPU Hotplug hooks
  479. */
  480. int (*cpu_prepare)(int cpu);
  481. void (*cpu_starting)(int cpu);
  482. void (*cpu_dying)(int cpu);
  483. void (*cpu_dead)(int cpu);
  484. void (*check_microcode)(void);
  485. void (*sched_task)(struct perf_event_context *ctx,
  486. bool sched_in);
  487. /*
  488. * Intel Arch Perfmon v2+
  489. */
  490. u64 intel_ctrl;
  491. union perf_capabilities intel_cap;
  492. /*
  493. * Intel DebugStore bits
  494. */
  495. unsigned int bts :1,
  496. bts_active :1,
  497. pebs :1,
  498. pebs_active :1,
  499. pebs_broken :1;
  500. int pebs_record_size;
  501. void (*drain_pebs)(struct pt_regs *regs);
  502. struct event_constraint *pebs_constraints;
  503. void (*pebs_aliases)(struct perf_event *event);
  504. int max_pebs_events;
  505. /*
  506. * Intel LBR
  507. */
  508. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  509. int lbr_nr; /* hardware stack size */
  510. u64 lbr_sel_mask; /* LBR_SELECT valid bits */
  511. const int *lbr_sel_map; /* lbr_select mappings */
  512. bool lbr_double_abort; /* duplicated lbr aborts */
  513. /*
  514. * Intel PT/LBR/BTS are exclusive
  515. */
  516. atomic_t lbr_exclusive[x86_lbr_exclusive_max];
  517. /*
  518. * Extra registers for events
  519. */
  520. struct extra_reg *extra_regs;
  521. unsigned int flags;
  522. /*
  523. * Intel host/guest support (KVM)
  524. */
  525. struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
  526. };
  527. struct x86_perf_task_context {
  528. u64 lbr_from[MAX_LBR_ENTRIES];
  529. u64 lbr_to[MAX_LBR_ENTRIES];
  530. int lbr_callstack_users;
  531. int lbr_stack_state;
  532. };
  533. #define x86_add_quirk(func_) \
  534. do { \
  535. static struct x86_pmu_quirk __quirk __initdata = { \
  536. .func = func_, \
  537. }; \
  538. __quirk.next = x86_pmu.quirks; \
  539. x86_pmu.quirks = &__quirk; \
  540. } while (0)
  541. /*
  542. * x86_pmu flags
  543. */
  544. #define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
  545. #define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
  546. #define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
  547. #define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
  548. #define EVENT_VAR(_id) event_attr_##_id
  549. #define EVENT_PTR(_id) &event_attr_##_id.attr.attr
  550. #define EVENT_ATTR(_name, _id) \
  551. static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
  552. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  553. .id = PERF_COUNT_HW_##_id, \
  554. .event_str = NULL, \
  555. };
  556. #define EVENT_ATTR_STR(_name, v, str) \
  557. static struct perf_pmu_events_attr event_attr_##v = { \
  558. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  559. .id = 0, \
  560. .event_str = str, \
  561. };
  562. extern struct x86_pmu x86_pmu __read_mostly;
  563. static inline bool x86_pmu_has_lbr_callstack(void)
  564. {
  565. return x86_pmu.lbr_sel_map &&
  566. x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
  567. }
  568. DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  569. int x86_perf_event_set_period(struct perf_event *event);
  570. /*
  571. * Generalized hw caching related hw_event table, filled
  572. * in on a per model basis. A value of 0 means
  573. * 'not supported', -1 means 'hw_event makes no sense on
  574. * this CPU', any other value means the raw hw_event
  575. * ID.
  576. */
  577. #define C(x) PERF_COUNT_HW_CACHE_##x
  578. extern u64 __read_mostly hw_cache_event_ids
  579. [PERF_COUNT_HW_CACHE_MAX]
  580. [PERF_COUNT_HW_CACHE_OP_MAX]
  581. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  582. extern u64 __read_mostly hw_cache_extra_regs
  583. [PERF_COUNT_HW_CACHE_MAX]
  584. [PERF_COUNT_HW_CACHE_OP_MAX]
  585. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  586. u64 x86_perf_event_update(struct perf_event *event);
  587. static inline unsigned int x86_pmu_config_addr(int index)
  588. {
  589. return x86_pmu.eventsel + (x86_pmu.addr_offset ?
  590. x86_pmu.addr_offset(index, true) : index);
  591. }
  592. static inline unsigned int x86_pmu_event_addr(int index)
  593. {
  594. return x86_pmu.perfctr + (x86_pmu.addr_offset ?
  595. x86_pmu.addr_offset(index, false) : index);
  596. }
  597. static inline int x86_pmu_rdpmc_index(int index)
  598. {
  599. return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
  600. }
  601. int x86_add_exclusive(unsigned int what);
  602. void x86_del_exclusive(unsigned int what);
  603. void hw_perf_lbr_event_destroy(struct perf_event *event);
  604. int x86_setup_perfctr(struct perf_event *event);
  605. int x86_pmu_hw_config(struct perf_event *event);
  606. void x86_pmu_disable_all(void);
  607. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  608. u64 enable_mask)
  609. {
  610. u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
  611. if (hwc->extra_reg.reg)
  612. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  613. wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
  614. }
  615. void x86_pmu_enable_all(int added);
  616. int perf_assign_events(struct event_constraint **constraints, int n,
  617. int wmin, int wmax, int gpmax, int *assign);
  618. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
  619. void x86_pmu_stop(struct perf_event *event, int flags);
  620. static inline void x86_pmu_disable_event(struct perf_event *event)
  621. {
  622. struct hw_perf_event *hwc = &event->hw;
  623. wrmsrl(hwc->config_base, hwc->config);
  624. }
  625. void x86_pmu_enable_event(struct perf_event *event);
  626. int x86_pmu_handle_irq(struct pt_regs *regs);
  627. extern struct event_constraint emptyconstraint;
  628. extern struct event_constraint unconstrained;
  629. static inline bool kernel_ip(unsigned long ip)
  630. {
  631. #ifdef CONFIG_X86_32
  632. return ip > PAGE_OFFSET;
  633. #else
  634. return (long)ip < 0;
  635. #endif
  636. }
  637. /*
  638. * Not all PMUs provide the right context information to place the reported IP
  639. * into full context. Specifically segment registers are typically not
  640. * supplied.
  641. *
  642. * Assuming the address is a linear address (it is for IBS), we fake the CS and
  643. * vm86 mode using the known zero-based code segment and 'fix up' the registers
  644. * to reflect this.
  645. *
  646. * Intel PEBS/LBR appear to typically provide the effective address, nothing
  647. * much we can do about that but pray and treat it like a linear address.
  648. */
  649. static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
  650. {
  651. regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
  652. if (regs->flags & X86_VM_MASK)
  653. regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
  654. regs->ip = ip;
  655. }
  656. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
  657. ssize_t intel_event_sysfs_show(char *page, u64 config);
  658. #ifdef CONFIG_CPU_SUP_AMD
  659. int amd_pmu_init(void);
  660. #else /* CONFIG_CPU_SUP_AMD */
  661. static inline int amd_pmu_init(void)
  662. {
  663. return 0;
  664. }
  665. #endif /* CONFIG_CPU_SUP_AMD */
  666. #ifdef CONFIG_CPU_SUP_INTEL
  667. static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
  668. {
  669. /* user explicitly requested branch sampling */
  670. if (has_branch_stack(event))
  671. return true;
  672. /* implicit branch sampling to correct PEBS skid */
  673. if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
  674. x86_pmu.intel_cap.pebs_format < 2)
  675. return true;
  676. return false;
  677. }
  678. static inline bool intel_pmu_has_bts(struct perf_event *event)
  679. {
  680. if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  681. !event->attr.freq && event->hw.sample_period == 1)
  682. return true;
  683. return false;
  684. }
  685. int intel_pmu_save_and_restart(struct perf_event *event);
  686. struct event_constraint *
  687. x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  688. struct perf_event *event);
  689. struct intel_shared_regs *allocate_shared_regs(int cpu);
  690. int intel_pmu_init(void);
  691. void init_debug_store_on_cpu(int cpu);
  692. void fini_debug_store_on_cpu(int cpu);
  693. void release_ds_buffers(void);
  694. void reserve_ds_buffers(void);
  695. extern struct event_constraint bts_constraint;
  696. void intel_pmu_enable_bts(u64 config);
  697. void intel_pmu_disable_bts(void);
  698. int intel_pmu_drain_bts_buffer(void);
  699. extern struct event_constraint intel_core2_pebs_event_constraints[];
  700. extern struct event_constraint intel_atom_pebs_event_constraints[];
  701. extern struct event_constraint intel_slm_pebs_event_constraints[];
  702. extern struct event_constraint intel_nehalem_pebs_event_constraints[];
  703. extern struct event_constraint intel_westmere_pebs_event_constraints[];
  704. extern struct event_constraint intel_snb_pebs_event_constraints[];
  705. extern struct event_constraint intel_ivb_pebs_event_constraints[];
  706. extern struct event_constraint intel_hsw_pebs_event_constraints[];
  707. struct event_constraint *intel_pebs_constraints(struct perf_event *event);
  708. void intel_pmu_pebs_enable(struct perf_event *event);
  709. void intel_pmu_pebs_disable(struct perf_event *event);
  710. void intel_pmu_pebs_enable_all(void);
  711. void intel_pmu_pebs_disable_all(void);
  712. void intel_ds_init(void);
  713. void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
  714. void intel_pmu_lbr_reset(void);
  715. void intel_pmu_lbr_enable(struct perf_event *event);
  716. void intel_pmu_lbr_disable(struct perf_event *event);
  717. void intel_pmu_lbr_enable_all(bool pmi);
  718. void intel_pmu_lbr_disable_all(void);
  719. void intel_pmu_lbr_read(void);
  720. void intel_pmu_lbr_init_core(void);
  721. void intel_pmu_lbr_init_nhm(void);
  722. void intel_pmu_lbr_init_atom(void);
  723. void intel_pmu_lbr_init_snb(void);
  724. void intel_pmu_lbr_init_hsw(void);
  725. int intel_pmu_setup_lbr_filter(struct perf_event *event);
  726. void intel_pt_interrupt(void);
  727. int intel_bts_interrupt(void);
  728. void intel_bts_enable_local(void);
  729. void intel_bts_disable_local(void);
  730. int p4_pmu_init(void);
  731. int p6_pmu_init(void);
  732. int knc_pmu_init(void);
  733. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
  734. char *page);
  735. static inline int is_ht_workaround_enabled(void)
  736. {
  737. return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
  738. }
  739. #else /* CONFIG_CPU_SUP_INTEL */
  740. static inline void reserve_ds_buffers(void)
  741. {
  742. }
  743. static inline void release_ds_buffers(void)
  744. {
  745. }
  746. static inline int intel_pmu_init(void)
  747. {
  748. return 0;
  749. }
  750. static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
  751. {
  752. return NULL;
  753. }
  754. static inline int is_ht_workaround_enabled(void)
  755. {
  756. return 0;
  757. }
  758. #endif /* CONFIG_CPU_SUP_INTEL */