perf_event.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. /*
  16. * | NHM/WSM | SNB |
  17. * register -------------------------------
  18. * | HT | no HT | HT | no HT |
  19. *-----------------------------------------
  20. * offcore | core | core | cpu | core |
  21. * lbr_sel | core | core | cpu | core |
  22. * ld_lat | cpu | core | cpu | core |
  23. *-----------------------------------------
  24. *
  25. * Given that there is a small number of shared regs,
  26. * we can pre-allocate their slot in the per-cpu
  27. * per-core reg tables.
  28. */
  29. enum extra_reg_type {
  30. EXTRA_REG_NONE = -1, /* not used */
  31. EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
  32. EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
  33. EXTRA_REG_MAX /* number of entries needed */
  34. };
  35. struct event_constraint {
  36. union {
  37. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  38. u64 idxmsk64;
  39. };
  40. u64 code;
  41. u64 cmask;
  42. int weight;
  43. };
  44. struct amd_nb {
  45. int nb_id; /* NorthBridge id */
  46. int refcnt; /* reference count */
  47. struct perf_event *owners[X86_PMC_IDX_MAX];
  48. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  49. };
  50. /* The maximal number of PEBS events: */
  51. #define MAX_PEBS_EVENTS 4
  52. /*
  53. * A debug store configuration.
  54. *
  55. * We only support architectures that use 64bit fields.
  56. */
  57. struct debug_store {
  58. u64 bts_buffer_base;
  59. u64 bts_index;
  60. u64 bts_absolute_maximum;
  61. u64 bts_interrupt_threshold;
  62. u64 pebs_buffer_base;
  63. u64 pebs_index;
  64. u64 pebs_absolute_maximum;
  65. u64 pebs_interrupt_threshold;
  66. u64 pebs_event_reset[MAX_PEBS_EVENTS];
  67. };
  68. /*
  69. * Per register state.
  70. */
  71. struct er_account {
  72. raw_spinlock_t lock; /* per-core: protect structure */
  73. u64 config; /* extra MSR config */
  74. u64 reg; /* extra MSR number */
  75. atomic_t ref; /* reference count */
  76. };
  77. /*
  78. * Per core/cpu state
  79. *
  80. * Used to coordinate shared registers between HT threads or
  81. * among events on a single PMU.
  82. */
  83. struct intel_shared_regs {
  84. struct er_account regs[EXTRA_REG_MAX];
  85. int refcnt; /* per-core: #HT threads */
  86. unsigned core_id; /* per-core: core id */
  87. };
  88. #define MAX_LBR_ENTRIES 16
  89. struct cpu_hw_events {
  90. /*
  91. * Generic x86 PMC bits
  92. */
  93. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  94. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  95. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  96. int enabled;
  97. int n_events;
  98. int n_added;
  99. int n_txn;
  100. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  101. u64 tags[X86_PMC_IDX_MAX];
  102. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  103. unsigned int group_flag;
  104. /*
  105. * Intel DebugStore bits
  106. */
  107. struct debug_store *ds;
  108. u64 pebs_enabled;
  109. /*
  110. * Intel LBR bits
  111. */
  112. int lbr_users;
  113. void *lbr_context;
  114. struct perf_branch_stack lbr_stack;
  115. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  116. /*
  117. * Intel host/guest exclude bits
  118. */
  119. u64 intel_ctrl_guest_mask;
  120. u64 intel_ctrl_host_mask;
  121. struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
  122. /*
  123. * manage shared (per-core, per-cpu) registers
  124. * used on Intel NHM/WSM/SNB
  125. */
  126. struct intel_shared_regs *shared_regs;
  127. /*
  128. * AMD specific bits
  129. */
  130. struct amd_nb *amd_nb;
  131. void *kfree_on_online;
  132. };
  133. #define __EVENT_CONSTRAINT(c, n, m, w) {\
  134. { .idxmsk64 = (n) }, \
  135. .code = (c), \
  136. .cmask = (m), \
  137. .weight = (w), \
  138. }
  139. #define EVENT_CONSTRAINT(c, n, m) \
  140. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
  141. /*
  142. * Constraint on the Event code.
  143. */
  144. #define INTEL_EVENT_CONSTRAINT(c, n) \
  145. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  146. /*
  147. * Constraint on the Event code + UMask + fixed-mask
  148. *
  149. * filter mask to validate fixed counter events.
  150. * the following filters disqualify for fixed counters:
  151. * - inv
  152. * - edge
  153. * - cnt-mask
  154. * The other filters are supported by fixed counters.
  155. * The any-thread option is supported starting with v3.
  156. */
  157. #define FIXED_EVENT_CONSTRAINT(c, n) \
  158. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  159. /*
  160. * Constraint on the Event code + UMask
  161. */
  162. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  163. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  164. #define EVENT_CONSTRAINT_END \
  165. EVENT_CONSTRAINT(0, 0, 0)
  166. #define for_each_event_constraint(e, c) \
  167. for ((e) = (c); (e)->weight; (e)++)
  168. /*
  169. * Extra registers for specific events.
  170. *
  171. * Some events need large masks and require external MSRs.
  172. * Those extra MSRs end up being shared for all events on
  173. * a PMU and sometimes between PMU of sibling HT threads.
  174. * In either case, the kernel needs to handle conflicting
  175. * accesses to those extra, shared, regs. The data structure
  176. * to manage those registers is stored in cpu_hw_event.
  177. */
  178. struct extra_reg {
  179. unsigned int event;
  180. unsigned int msr;
  181. u64 config_mask;
  182. u64 valid_mask;
  183. int idx; /* per_xxx->regs[] reg index */
  184. };
  185. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  186. .event = (e), \
  187. .msr = (ms), \
  188. .config_mask = (m), \
  189. .valid_mask = (vm), \
  190. .idx = EXTRA_REG_##i \
  191. }
  192. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  193. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  194. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  195. union perf_capabilities {
  196. struct {
  197. u64 lbr_format:6;
  198. u64 pebs_trap:1;
  199. u64 pebs_arch_reg:1;
  200. u64 pebs_format:4;
  201. u64 smm_freeze:1;
  202. };
  203. u64 capabilities;
  204. };
  205. /*
  206. * struct x86_pmu - generic x86 pmu
  207. */
  208. struct x86_pmu {
  209. /*
  210. * Generic x86 PMC bits
  211. */
  212. const char *name;
  213. int version;
  214. int (*handle_irq)(struct pt_regs *);
  215. void (*disable_all)(void);
  216. void (*enable_all)(int added);
  217. void (*enable)(struct perf_event *);
  218. void (*disable)(struct perf_event *);
  219. int (*hw_config)(struct perf_event *event);
  220. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  221. unsigned eventsel;
  222. unsigned perfctr;
  223. u64 (*event_map)(int);
  224. int max_events;
  225. int num_counters;
  226. int num_counters_fixed;
  227. int cntval_bits;
  228. u64 cntval_mask;
  229. int apic;
  230. u64 max_period;
  231. struct event_constraint *
  232. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  233. struct perf_event *event);
  234. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  235. struct perf_event *event);
  236. struct event_constraint *event_constraints;
  237. void (*quirks)(void);
  238. int perfctr_second_write;
  239. int (*cpu_prepare)(int cpu);
  240. void (*cpu_starting)(int cpu);
  241. void (*cpu_dying)(int cpu);
  242. void (*cpu_dead)(int cpu);
  243. /*
  244. * Intel Arch Perfmon v2+
  245. */
  246. u64 intel_ctrl;
  247. union perf_capabilities intel_cap;
  248. /*
  249. * Intel DebugStore bits
  250. */
  251. int bts, pebs;
  252. int bts_active, pebs_active;
  253. int pebs_record_size;
  254. void (*drain_pebs)(struct pt_regs *regs);
  255. struct event_constraint *pebs_constraints;
  256. /*
  257. * Intel LBR
  258. */
  259. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  260. int lbr_nr; /* hardware stack size */
  261. /*
  262. * Extra registers for events
  263. */
  264. struct extra_reg *extra_regs;
  265. unsigned int er_flags;
  266. /*
  267. * Intel host/guest support (KVM)
  268. */
  269. struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
  270. };
  271. #define ERF_NO_HT_SHARING 1
  272. #define ERF_HAS_RSP_1 2
  273. extern struct x86_pmu x86_pmu __read_mostly;
  274. DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  275. int x86_perf_event_set_period(struct perf_event *event);
  276. /*
  277. * Generalized hw caching related hw_event table, filled
  278. * in on a per model basis. A value of 0 means
  279. * 'not supported', -1 means 'hw_event makes no sense on
  280. * this CPU', any other value means the raw hw_event
  281. * ID.
  282. */
  283. #define C(x) PERF_COUNT_HW_CACHE_##x
  284. extern u64 __read_mostly hw_cache_event_ids
  285. [PERF_COUNT_HW_CACHE_MAX]
  286. [PERF_COUNT_HW_CACHE_OP_MAX]
  287. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  288. extern u64 __read_mostly hw_cache_extra_regs
  289. [PERF_COUNT_HW_CACHE_MAX]
  290. [PERF_COUNT_HW_CACHE_OP_MAX]
  291. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  292. u64 x86_perf_event_update(struct perf_event *event);
  293. static inline int x86_pmu_addr_offset(int index)
  294. {
  295. int offset;
  296. /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
  297. alternative_io(ASM_NOP2,
  298. "shll $1, %%eax",
  299. X86_FEATURE_PERFCTR_CORE,
  300. "=a" (offset),
  301. "a" (index));
  302. return offset;
  303. }
  304. static inline unsigned int x86_pmu_config_addr(int index)
  305. {
  306. return x86_pmu.eventsel + x86_pmu_addr_offset(index);
  307. }
  308. static inline unsigned int x86_pmu_event_addr(int index)
  309. {
  310. return x86_pmu.perfctr + x86_pmu_addr_offset(index);
  311. }
  312. int x86_setup_perfctr(struct perf_event *event);
  313. int x86_pmu_hw_config(struct perf_event *event);
  314. void x86_pmu_disable_all(void);
  315. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  316. u64 enable_mask)
  317. {
  318. if (hwc->extra_reg.reg)
  319. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  320. wrmsrl(hwc->config_base, hwc->config | enable_mask);
  321. }
  322. void x86_pmu_enable_all(int added);
  323. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
  324. void x86_pmu_stop(struct perf_event *event, int flags);
  325. static inline void x86_pmu_disable_event(struct perf_event *event)
  326. {
  327. struct hw_perf_event *hwc = &event->hw;
  328. wrmsrl(hwc->config_base, hwc->config);
  329. }
  330. void x86_pmu_enable_event(struct perf_event *event);
  331. int x86_pmu_handle_irq(struct pt_regs *regs);
  332. extern struct event_constraint emptyconstraint;
  333. extern struct event_constraint unconstrained;
  334. #ifdef CONFIG_CPU_SUP_AMD
  335. int amd_pmu_init(void);
  336. #else /* CONFIG_CPU_SUP_AMD */
  337. static inline int amd_pmu_init(void)
  338. {
  339. return 0;
  340. }
  341. #endif /* CONFIG_CPU_SUP_AMD */
  342. #ifdef CONFIG_CPU_SUP_INTEL
  343. int intel_pmu_save_and_restart(struct perf_event *event);
  344. struct event_constraint *
  345. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event);
  346. struct intel_shared_regs *allocate_shared_regs(int cpu);
  347. int intel_pmu_init(void);
  348. void init_debug_store_on_cpu(int cpu);
  349. void fini_debug_store_on_cpu(int cpu);
  350. void release_ds_buffers(void);
  351. void reserve_ds_buffers(void);
  352. extern struct event_constraint bts_constraint;
  353. void intel_pmu_enable_bts(u64 config);
  354. void intel_pmu_disable_bts(void);
  355. int intel_pmu_drain_bts_buffer(void);
  356. extern struct event_constraint intel_core2_pebs_event_constraints[];
  357. extern struct event_constraint intel_atom_pebs_event_constraints[];
  358. extern struct event_constraint intel_nehalem_pebs_event_constraints[];
  359. extern struct event_constraint intel_westmere_pebs_event_constraints[];
  360. extern struct event_constraint intel_snb_pebs_event_constraints[];
  361. struct event_constraint *intel_pebs_constraints(struct perf_event *event);
  362. void intel_pmu_pebs_enable(struct perf_event *event);
  363. void intel_pmu_pebs_disable(struct perf_event *event);
  364. void intel_pmu_pebs_enable_all(void);
  365. void intel_pmu_pebs_disable_all(void);
  366. void intel_ds_init(void);
  367. void intel_pmu_lbr_reset(void);
  368. void intel_pmu_lbr_enable(struct perf_event *event);
  369. void intel_pmu_lbr_disable(struct perf_event *event);
  370. void intel_pmu_lbr_enable_all(void);
  371. void intel_pmu_lbr_disable_all(void);
  372. void intel_pmu_lbr_read(void);
  373. void intel_pmu_lbr_init_core(void);
  374. void intel_pmu_lbr_init_nhm(void);
  375. void intel_pmu_lbr_init_atom(void);
  376. int p4_pmu_init(void);
  377. int p6_pmu_init(void);
  378. #else /* CONFIG_CPU_SUP_INTEL */
  379. static inline void reserve_ds_buffers(void)
  380. {
  381. }
  382. static inline void release_ds_buffers(void)
  383. {
  384. }
  385. static inline int intel_pmu_init(void)
  386. {
  387. return 0;
  388. }
  389. static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
  390. {
  391. return NULL;
  392. }
  393. #endif /* CONFIG_CPU_SUP_INTEL */