perf_event.h 27 KB

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