perf_event.h 14 KB

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