PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/x86/kernel/cpu/perf_event.h

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