perf_event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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_MAX /* number of entries needed */
  46. };
  47. struct event_constraint {
  48. union {
  49. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  50. u64 idxmsk64;
  51. };
  52. u64 code;
  53. u64 cmask;
  54. int weight;
  55. int overlap;
  56. };
  57. struct amd_nb {
  58. int nb_id; /* NorthBridge id */
  59. int refcnt; /* reference count */
  60. struct perf_event *owners[X86_PMC_IDX_MAX];
  61. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  62. };
  63. /* The maximal number of PEBS events: */
  64. #define MAX_PEBS_EVENTS 8
  65. /*
  66. * A debug store configuration.
  67. *
  68. * We only support architectures that use 64bit fields.
  69. */
  70. struct debug_store {
  71. u64 bts_buffer_base;
  72. u64 bts_index;
  73. u64 bts_absolute_maximum;
  74. u64 bts_interrupt_threshold;
  75. u64 pebs_buffer_base;
  76. u64 pebs_index;
  77. u64 pebs_absolute_maximum;
  78. u64 pebs_interrupt_threshold;
  79. u64 pebs_event_reset[MAX_PEBS_EVENTS];
  80. };
  81. /*
  82. * Per register state.
  83. */
  84. struct er_account {
  85. raw_spinlock_t lock; /* per-core: protect structure */
  86. u64 config; /* extra MSR config */
  87. u64 reg; /* extra MSR number */
  88. atomic_t ref; /* reference count */
  89. };
  90. /*
  91. * Per core/cpu state
  92. *
  93. * Used to coordinate shared registers between HT threads or
  94. * among events on a single PMU.
  95. */
  96. struct intel_shared_regs {
  97. struct er_account regs[EXTRA_REG_MAX];
  98. int refcnt; /* per-core: #HT threads */
  99. unsigned core_id; /* per-core: core id */
  100. };
  101. #define MAX_LBR_ENTRIES 16
  102. struct cpu_hw_events {
  103. /*
  104. * Generic x86 PMC bits
  105. */
  106. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  107. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  108. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  109. int enabled;
  110. int n_events;
  111. int n_added;
  112. int n_txn;
  113. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  114. u64 tags[X86_PMC_IDX_MAX];
  115. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  116. unsigned int group_flag;
  117. int is_fake;
  118. /*
  119. * Intel DebugStore bits
  120. */
  121. struct debug_store *ds;
  122. u64 pebs_enabled;
  123. /*
  124. * Intel LBR bits
  125. */
  126. int lbr_users;
  127. void *lbr_context;
  128. struct perf_branch_stack lbr_stack;
  129. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  130. struct er_account *lbr_sel;
  131. u64 br_sel;
  132. /*
  133. * Intel host/guest exclude bits
  134. */
  135. u64 intel_ctrl_guest_mask;
  136. u64 intel_ctrl_host_mask;
  137. struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
  138. /*
  139. * manage shared (per-core, per-cpu) registers
  140. * used on Intel NHM/WSM/SNB
  141. */
  142. struct intel_shared_regs *shared_regs;
  143. /*
  144. * AMD specific bits
  145. */
  146. struct amd_nb *amd_nb;
  147. /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
  148. u64 perf_ctr_virt_mask;
  149. void *kfree_on_online;
  150. };
  151. #define __EVENT_CONSTRAINT(c, n, m, w, o) {\
  152. { .idxmsk64 = (n) }, \
  153. .code = (c), \
  154. .cmask = (m), \
  155. .weight = (w), \
  156. .overlap = (o), \
  157. }
  158. #define EVENT_CONSTRAINT(c, n, m) \
  159. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0)
  160. /*
  161. * The overlap flag marks event constraints with overlapping counter
  162. * masks. This is the case if the counter mask of such an event is not
  163. * a subset of any other counter mask of a constraint with an equal or
  164. * higher weight, e.g.:
  165. *
  166. * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  167. * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
  168. * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
  169. *
  170. * The event scheduler may not select the correct counter in the first
  171. * cycle because it needs to know which subsequent events will be
  172. * scheduled. It may fail to schedule the events then. So we set the
  173. * overlap flag for such constraints to give the scheduler a hint which
  174. * events to select for counter rescheduling.
  175. *
  176. * Care must be taken as the rescheduling algorithm is O(n!) which
  177. * will increase scheduling cycles for an over-commited system
  178. * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
  179. * and its counter masks must be kept at a minimum.
  180. */
  181. #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
  182. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1)
  183. /*
  184. * Constraint on the Event code.
  185. */
  186. #define INTEL_EVENT_CONSTRAINT(c, n) \
  187. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  188. /*
  189. * Constraint on the Event code + UMask + fixed-mask
  190. *
  191. * filter mask to validate fixed counter events.
  192. * the following filters disqualify for fixed counters:
  193. * - inv
  194. * - edge
  195. * - cnt-mask
  196. * The other filters are supported by fixed counters.
  197. * The any-thread option is supported starting with v3.
  198. */
  199. #define FIXED_EVENT_CONSTRAINT(c, n) \
  200. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  201. /*
  202. * Constraint on the Event code + UMask
  203. */
  204. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  205. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  206. #define EVENT_CONSTRAINT_END \
  207. EVENT_CONSTRAINT(0, 0, 0)
  208. #define for_each_event_constraint(e, c) \
  209. for ((e) = (c); (e)->weight; (e)++)
  210. /*
  211. * Extra registers for specific events.
  212. *
  213. * Some events need large masks and require external MSRs.
  214. * Those extra MSRs end up being shared for all events on
  215. * a PMU and sometimes between PMU of sibling HT threads.
  216. * In either case, the kernel needs to handle conflicting
  217. * accesses to those extra, shared, regs. The data structure
  218. * to manage those registers is stored in cpu_hw_event.
  219. */
  220. struct extra_reg {
  221. unsigned int event;
  222. unsigned int msr;
  223. u64 config_mask;
  224. u64 valid_mask;
  225. int idx; /* per_xxx->regs[] reg index */
  226. };
  227. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  228. .event = (e), \
  229. .msr = (ms), \
  230. .config_mask = (m), \
  231. .valid_mask = (vm), \
  232. .idx = EXTRA_REG_##i \
  233. }
  234. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  235. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  236. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  237. union perf_capabilities {
  238. struct {
  239. u64 lbr_format:6;
  240. u64 pebs_trap:1;
  241. u64 pebs_arch_reg:1;
  242. u64 pebs_format:4;
  243. u64 smm_freeze:1;
  244. };
  245. u64 capabilities;
  246. };
  247. struct x86_pmu_quirk {
  248. struct x86_pmu_quirk *next;
  249. void (*func)(void);
  250. };
  251. union x86_pmu_config {
  252. struct {
  253. u64 event:8,
  254. umask:8,
  255. usr:1,
  256. os:1,
  257. edge:1,
  258. pc:1,
  259. interrupt:1,
  260. __reserved1:1,
  261. en:1,
  262. inv:1,
  263. cmask:8,
  264. event2:4,
  265. __reserved2:4,
  266. go:1,
  267. ho:1;
  268. } bits;
  269. u64 value;
  270. };
  271. #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
  272. /*
  273. * struct x86_pmu - generic x86 pmu
  274. */
  275. struct x86_pmu {
  276. /*
  277. * Generic x86 PMC bits
  278. */
  279. const char *name;
  280. int version;
  281. int (*handle_irq)(struct pt_regs *);
  282. void (*disable_all)(void);
  283. void (*enable_all)(int added);
  284. void (*enable)(struct perf_event *);
  285. void (*disable)(struct perf_event *);
  286. int (*hw_config)(struct perf_event *event);
  287. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  288. unsigned eventsel;
  289. unsigned perfctr;
  290. u64 (*event_map)(int);
  291. int max_events;
  292. int num_counters;
  293. int num_counters_fixed;
  294. int cntval_bits;
  295. u64 cntval_mask;
  296. union {
  297. unsigned long events_maskl;
  298. unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
  299. };
  300. int events_mask_len;
  301. int apic;
  302. u64 max_period;
  303. struct event_constraint *
  304. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  305. struct perf_event *event);
  306. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  307. struct perf_event *event);
  308. struct event_constraint *event_constraints;
  309. struct x86_pmu_quirk *quirks;
  310. int perfctr_second_write;
  311. /*
  312. * sysfs attrs
  313. */
  314. int attr_rdpmc;
  315. struct attribute **format_attrs;
  316. /*
  317. * CPU Hotplug hooks
  318. */
  319. int (*cpu_prepare)(int cpu);
  320. void (*cpu_starting)(int cpu);
  321. void (*cpu_dying)(int cpu);
  322. void (*cpu_dead)(int cpu);
  323. void (*check_microcode)(void);
  324. void (*flush_branch_stack)(void);
  325. /*
  326. * Intel Arch Perfmon v2+
  327. */
  328. u64 intel_ctrl;
  329. union perf_capabilities intel_cap;
  330. /*
  331. * Intel DebugStore bits
  332. */
  333. unsigned int bts :1,
  334. bts_active :1,
  335. pebs :1,
  336. pebs_active :1,
  337. pebs_broken :1;
  338. int pebs_record_size;
  339. void (*drain_pebs)(struct pt_regs *regs);
  340. struct event_constraint *pebs_constraints;
  341. void (*pebs_aliases)(struct perf_event *event);
  342. int max_pebs_events;
  343. /*
  344. * Intel LBR
  345. */
  346. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  347. int lbr_nr; /* hardware stack size */
  348. u64 lbr_sel_mask; /* LBR_SELECT valid bits */
  349. const int *lbr_sel_map; /* lbr_select mappings */
  350. /*
  351. * Extra registers for events
  352. */
  353. struct extra_reg *extra_regs;
  354. unsigned int er_flags;
  355. /*
  356. * Intel host/guest support (KVM)
  357. */
  358. struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
  359. };
  360. #define x86_add_quirk(func_) \
  361. do { \
  362. static struct x86_pmu_quirk __quirk __initdata = { \
  363. .func = func_, \
  364. }; \
  365. __quirk.next = x86_pmu.quirks; \
  366. x86_pmu.quirks = &__quirk; \
  367. } while (0)
  368. #define ERF_NO_HT_SHARING 1
  369. #define ERF_HAS_RSP_1 2
  370. extern struct x86_pmu x86_pmu __read_mostly;
  371. DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  372. int x86_perf_event_set_period(struct perf_event *event);
  373. /*
  374. * Generalized hw caching related hw_event table, filled
  375. * in on a per model basis. A value of 0 means
  376. * 'not supported', -1 means 'hw_event makes no sense on
  377. * this CPU', any other value means the raw hw_event
  378. * ID.
  379. */
  380. #define C(x) PERF_COUNT_HW_CACHE_##x
  381. extern u64 __read_mostly hw_cache_event_ids
  382. [PERF_COUNT_HW_CACHE_MAX]
  383. [PERF_COUNT_HW_CACHE_OP_MAX]
  384. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  385. extern u64 __read_mostly hw_cache_extra_regs
  386. [PERF_COUNT_HW_CACHE_MAX]
  387. [PERF_COUNT_HW_CACHE_OP_MAX]
  388. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  389. u64 x86_perf_event_update(struct perf_event *event);
  390. static inline int x86_pmu_addr_offset(int index)
  391. {
  392. int offset;
  393. /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
  394. alternative_io(ASM_NOP2,
  395. "shll $1, %%eax",
  396. X86_FEATURE_PERFCTR_CORE,
  397. "=a" (offset),
  398. "a" (index));
  399. return offset;
  400. }
  401. static inline unsigned int x86_pmu_config_addr(int index)
  402. {
  403. return x86_pmu.eventsel + x86_pmu_addr_offset(index);
  404. }
  405. static inline unsigned int x86_pmu_event_addr(int index)
  406. {
  407. return x86_pmu.perfctr + x86_pmu_addr_offset(index);
  408. }
  409. int x86_setup_perfctr(struct perf_event *event);
  410. int x86_pmu_hw_config(struct perf_event *event);
  411. void x86_pmu_disable_all(void);
  412. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  413. u64 enable_mask)
  414. {
  415. u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
  416. if (hwc->extra_reg.reg)
  417. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  418. wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
  419. }
  420. void x86_pmu_enable_all(int added);
  421. int perf_assign_events(struct event_constraint **constraints, int n,
  422. int wmin, int wmax, int *assign);
  423. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
  424. void x86_pmu_stop(struct perf_event *event, int flags);
  425. static inline void x86_pmu_disable_event(struct perf_event *event)
  426. {
  427. struct hw_perf_event *hwc = &event->hw;
  428. wrmsrl(hwc->config_base, hwc->config);
  429. }
  430. void x86_pmu_enable_event(struct perf_event *event);
  431. int x86_pmu_handle_irq(struct pt_regs *regs);
  432. extern struct event_constraint emptyconstraint;
  433. extern struct event_constraint unconstrained;
  434. static inline bool kernel_ip(unsigned long ip)
  435. {
  436. #ifdef CONFIG_X86_32
  437. return ip > PAGE_OFFSET;
  438. #else
  439. return (long)ip < 0;
  440. #endif
  441. }
  442. /*
  443. * Not all PMUs provide the right context information to place the reported IP
  444. * into full context. Specifically segment registers are typically not
  445. * supplied.
  446. *
  447. * Assuming the address is a linear address (it is for IBS), we fake the CS and
  448. * vm86 mode using the known zero-based code segment and 'fix up' the registers
  449. * to reflect this.
  450. *
  451. * Intel PEBS/LBR appear to typically provide the effective address, nothing
  452. * much we can do about that but pray and treat it like a linear address.
  453. */
  454. static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
  455. {
  456. regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
  457. if (regs->flags & X86_VM_MASK)
  458. regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
  459. regs->ip = ip;
  460. }
  461. #ifdef CONFIG_CPU_SUP_AMD
  462. int amd_pmu_init(void);
  463. #else /* CONFIG_CPU_SUP_AMD */
  464. static inline int amd_pmu_init(void)
  465. {
  466. return 0;
  467. }
  468. #endif /* CONFIG_CPU_SUP_AMD */
  469. #ifdef CONFIG_CPU_SUP_INTEL
  470. int intel_pmu_save_and_restart(struct perf_event *event);
  471. struct event_constraint *
  472. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event);
  473. struct intel_shared_regs *allocate_shared_regs(int cpu);
  474. int intel_pmu_init(void);
  475. void init_debug_store_on_cpu(int cpu);
  476. void fini_debug_store_on_cpu(int cpu);
  477. void release_ds_buffers(void);
  478. void reserve_ds_buffers(void);
  479. extern struct event_constraint bts_constraint;
  480. void intel_pmu_enable_bts(u64 config);
  481. void intel_pmu_disable_bts(void);
  482. int intel_pmu_drain_bts_buffer(void);
  483. extern struct event_constraint intel_core2_pebs_event_constraints[];
  484. extern struct event_constraint intel_atom_pebs_event_constraints[];
  485. extern struct event_constraint intel_nehalem_pebs_event_constraints[];
  486. extern struct event_constraint intel_westmere_pebs_event_constraints[];
  487. extern struct event_constraint intel_snb_pebs_event_constraints[];
  488. extern struct event_constraint intel_ivb_pebs_event_constraints[];
  489. struct event_constraint *intel_pebs_constraints(struct perf_event *event);
  490. void intel_pmu_pebs_enable(struct perf_event *event);
  491. void intel_pmu_pebs_disable(struct perf_event *event);
  492. void intel_pmu_pebs_enable_all(void);
  493. void intel_pmu_pebs_disable_all(void);
  494. void intel_ds_init(void);
  495. void intel_pmu_lbr_reset(void);
  496. void intel_pmu_lbr_enable(struct perf_event *event);
  497. void intel_pmu_lbr_disable(struct perf_event *event);
  498. void intel_pmu_lbr_enable_all(void);
  499. void intel_pmu_lbr_disable_all(void);
  500. void intel_pmu_lbr_read(void);
  501. void intel_pmu_lbr_init_core(void);
  502. void intel_pmu_lbr_init_nhm(void);
  503. void intel_pmu_lbr_init_atom(void);
  504. void intel_pmu_lbr_init_snb(void);
  505. int intel_pmu_setup_lbr_filter(struct perf_event *event);
  506. int p4_pmu_init(void);
  507. int p6_pmu_init(void);
  508. #else /* CONFIG_CPU_SUP_INTEL */
  509. static inline void reserve_ds_buffers(void)
  510. {
  511. }
  512. static inline void release_ds_buffers(void)
  513. {
  514. }
  515. static inline int intel_pmu_init(void)
  516. {
  517. return 0;
  518. }
  519. static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
  520. {
  521. return NULL;
  522. }
  523. #endif /* CONFIG_CPU_SUP_INTEL */