perf_event.h 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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_GP_REGS \
  88. ((1ULL << PERF_REG_X86_AX) | \
  89. (1ULL << PERF_REG_X86_BX) | \
  90. (1ULL << PERF_REG_X86_CX) | \
  91. (1ULL << PERF_REG_X86_DX) | \
  92. (1ULL << PERF_REG_X86_DI) | \
  93. (1ULL << PERF_REG_X86_SI) | \
  94. (1ULL << PERF_REG_X86_SP) | \
  95. (1ULL << PERF_REG_X86_BP) | \
  96. (1ULL << PERF_REG_X86_IP) | \
  97. (1ULL << PERF_REG_X86_FLAGS) | \
  98. (1ULL << PERF_REG_X86_R8) | \
  99. (1ULL << PERF_REG_X86_R9) | \
  100. (1ULL << PERF_REG_X86_R10) | \
  101. (1ULL << PERF_REG_X86_R11) | \
  102. (1ULL << PERF_REG_X86_R12) | \
  103. (1ULL << PERF_REG_X86_R13) | \
  104. (1ULL << PERF_REG_X86_R14) | \
  105. (1ULL << 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. * SKL TSX_FORCE_ABORT shadow
  214. */
  215. u64 tfa_shadow;
  216. /*
  217. * AMD specific bits
  218. */
  219. struct amd_nb *amd_nb;
  220. /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
  221. u64 perf_ctr_virt_mask;
  222. void *kfree_on_online[X86_PERF_KFREE_MAX];
  223. };
  224. #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
  225. { .idxmsk64 = (n) }, \
  226. .code = (c), \
  227. .cmask = (m), \
  228. .weight = (w), \
  229. .overlap = (o), \
  230. .flags = f, \
  231. }
  232. #define EVENT_CONSTRAINT(c, n, m) \
  233. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
  234. #define INTEL_EXCLEVT_CONSTRAINT(c, n) \
  235. __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
  236. 0, PERF_X86_EVENT_EXCL)
  237. /*
  238. * The overlap flag marks event constraints with overlapping counter
  239. * masks. This is the case if the counter mask of such an event is not
  240. * a subset of any other counter mask of a constraint with an equal or
  241. * higher weight, e.g.:
  242. *
  243. * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  244. * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
  245. * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
  246. *
  247. * The event scheduler may not select the correct counter in the first
  248. * cycle because it needs to know which subsequent events will be
  249. * scheduled. It may fail to schedule the events then. So we set the
  250. * overlap flag for such constraints to give the scheduler a hint which
  251. * events to select for counter rescheduling.
  252. *
  253. * Care must be taken as the rescheduling algorithm is O(n!) which
  254. * will increase scheduling cycles for an over-committed system
  255. * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
  256. * and its counter masks must be kept at a minimum.
  257. */
  258. #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
  259. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
  260. /*
  261. * Constraint on the Event code.
  262. */
  263. #define INTEL_EVENT_CONSTRAINT(c, n) \
  264. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  265. /*
  266. * Constraint on the Event code + UMask + fixed-mask
  267. *
  268. * filter mask to validate fixed counter events.
  269. * the following filters disqualify for fixed counters:
  270. * - inv
  271. * - edge
  272. * - cnt-mask
  273. * - in_tx
  274. * - in_tx_checkpointed
  275. * The other filters are supported by fixed counters.
  276. * The any-thread option is supported starting with v3.
  277. */
  278. #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
  279. #define FIXED_EVENT_CONSTRAINT(c, n) \
  280. EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
  281. /*
  282. * Constraint on the Event code + UMask
  283. */
  284. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  285. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  286. /* Constraint on specific umask bit only + event */
  287. #define INTEL_UBIT_EVENT_CONSTRAINT(c, n) \
  288. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT|(c))
  289. /* Like UEVENT_CONSTRAINT, but match flags too */
  290. #define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
  291. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
  292. #define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
  293. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
  294. HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
  295. #define INTEL_PLD_CONSTRAINT(c, n) \
  296. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  297. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
  298. #define INTEL_PST_CONSTRAINT(c, n) \
  299. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  300. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
  301. /* Event constraint, but match on all event flags too. */
  302. #define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
  303. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
  304. /* Check only flags, but allow all event/umask */
  305. #define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
  306. EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
  307. /* Check flags and event code, and set the HSW store flag */
  308. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
  309. __EVENT_CONSTRAINT(code, n, \
  310. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  311. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  312. /* Check flags and event code, and set the HSW load flag */
  313. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
  314. __EVENT_CONSTRAINT(code, n, \
  315. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  316. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  317. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
  318. __EVENT_CONSTRAINT(code, n, \
  319. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  320. HWEIGHT(n), 0, \
  321. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  322. /* Check flags and event code/umask, and set the HSW store flag */
  323. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
  324. __EVENT_CONSTRAINT(code, n, \
  325. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  326. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  327. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
  328. __EVENT_CONSTRAINT(code, n, \
  329. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  330. HWEIGHT(n), 0, \
  331. PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
  332. /* Check flags and event code/umask, and set the HSW load flag */
  333. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
  334. __EVENT_CONSTRAINT(code, n, \
  335. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  336. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  337. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
  338. __EVENT_CONSTRAINT(code, n, \
  339. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  340. HWEIGHT(n), 0, \
  341. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  342. /* Check flags and event code/umask, and set the HSW N/A flag */
  343. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
  344. __EVENT_CONSTRAINT(code, n, \
  345. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  346. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
  347. /*
  348. * We define the end marker as having a weight of -1
  349. * to enable blacklisting of events using a counter bitmask
  350. * of zero and thus a weight of zero.
  351. * The end marker has a weight that cannot possibly be
  352. * obtained from counting the bits in the bitmask.
  353. */
  354. #define EVENT_CONSTRAINT_END { .weight = -1 }
  355. /*
  356. * Check for end marker with weight == -1
  357. */
  358. #define for_each_event_constraint(e, c) \
  359. for ((e) = (c); (e)->weight != -1; (e)++)
  360. /*
  361. * Extra registers for specific events.
  362. *
  363. * Some events need large masks and require external MSRs.
  364. * Those extra MSRs end up being shared for all events on
  365. * a PMU and sometimes between PMU of sibling HT threads.
  366. * In either case, the kernel needs to handle conflicting
  367. * accesses to those extra, shared, regs. The data structure
  368. * to manage those registers is stored in cpu_hw_event.
  369. */
  370. struct extra_reg {
  371. unsigned int event;
  372. unsigned int msr;
  373. u64 config_mask;
  374. u64 valid_mask;
  375. int idx; /* per_xxx->regs[] reg index */
  376. bool extra_msr_access;
  377. };
  378. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  379. .event = (e), \
  380. .msr = (ms), \
  381. .config_mask = (m), \
  382. .valid_mask = (vm), \
  383. .idx = EXTRA_REG_##i, \
  384. .extra_msr_access = true, \
  385. }
  386. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  387. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  388. #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
  389. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
  390. ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
  391. #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
  392. INTEL_UEVENT_EXTRA_REG(c, \
  393. MSR_PEBS_LD_LAT_THRESHOLD, \
  394. 0xffff, \
  395. LDLAT)
  396. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  397. union perf_capabilities {
  398. struct {
  399. u64 lbr_format:6;
  400. u64 pebs_trap:1;
  401. u64 pebs_arch_reg:1;
  402. u64 pebs_format:4;
  403. u64 smm_freeze:1;
  404. /*
  405. * PMU supports separate counter range for writing
  406. * values > 32bit.
  407. */
  408. u64 full_width_write:1;
  409. };
  410. u64 capabilities;
  411. };
  412. struct x86_pmu_quirk {
  413. struct x86_pmu_quirk *next;
  414. void (*func)(void);
  415. };
  416. union x86_pmu_config {
  417. struct {
  418. u64 event:8,
  419. umask:8,
  420. usr:1,
  421. os:1,
  422. edge:1,
  423. pc:1,
  424. interrupt:1,
  425. __reserved1:1,
  426. en:1,
  427. inv:1,
  428. cmask:8,
  429. event2:4,
  430. __reserved2:4,
  431. go:1,
  432. ho:1;
  433. } bits;
  434. u64 value;
  435. };
  436. #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
  437. enum {
  438. x86_lbr_exclusive_lbr,
  439. x86_lbr_exclusive_bts,
  440. x86_lbr_exclusive_pt,
  441. x86_lbr_exclusive_max,
  442. };
  443. /*
  444. * struct x86_pmu - generic x86 pmu
  445. */
  446. struct x86_pmu {
  447. /*
  448. * Generic x86 PMC bits
  449. */
  450. const char *name;
  451. int version;
  452. int (*handle_irq)(struct pt_regs *);
  453. void (*disable_all)(void);
  454. void (*enable_all)(int added);
  455. void (*enable)(struct perf_event *);
  456. void (*disable)(struct perf_event *);
  457. void (*add)(struct perf_event *);
  458. void (*del)(struct perf_event *);
  459. void (*read)(struct perf_event *event);
  460. int (*hw_config)(struct perf_event *event);
  461. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  462. unsigned eventsel;
  463. unsigned perfctr;
  464. int (*addr_offset)(int index, bool eventsel);
  465. int (*rdpmc_index)(int index);
  466. u64 (*event_map)(int);
  467. int max_events;
  468. int num_counters;
  469. int num_counters_fixed;
  470. int cntval_bits;
  471. u64 cntval_mask;
  472. union {
  473. unsigned long events_maskl;
  474. unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
  475. };
  476. int events_mask_len;
  477. int apic;
  478. u64 max_period;
  479. struct event_constraint *
  480. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  481. int idx,
  482. struct perf_event *event);
  483. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  484. struct perf_event *event);
  485. void (*start_scheduling)(struct cpu_hw_events *cpuc);
  486. void (*commit_scheduling)(struct cpu_hw_events *cpuc, int idx, int cntr);
  487. void (*stop_scheduling)(struct cpu_hw_events *cpuc);
  488. struct event_constraint *event_constraints;
  489. struct x86_pmu_quirk *quirks;
  490. int perfctr_second_write;
  491. bool late_ack;
  492. u64 (*limit_period)(struct perf_event *event, u64 l);
  493. /*
  494. * sysfs attrs
  495. */
  496. int attr_rdpmc_broken;
  497. int attr_rdpmc;
  498. struct attribute **format_attrs;
  499. struct attribute **event_attrs;
  500. struct attribute **caps_attrs;
  501. ssize_t (*events_sysfs_show)(char *page, u64 config);
  502. struct attribute **cpu_events;
  503. unsigned long attr_freeze_on_smi;
  504. struct attribute **attrs;
  505. /*
  506. * CPU Hotplug hooks
  507. */
  508. int (*cpu_prepare)(int cpu);
  509. void (*cpu_starting)(int cpu);
  510. void (*cpu_dying)(int cpu);
  511. void (*cpu_dead)(int cpu);
  512. void (*check_microcode)(void);
  513. void (*sched_task)(struct perf_event_context *ctx,
  514. bool sched_in);
  515. /*
  516. * Intel Arch Perfmon v2+
  517. */
  518. u64 intel_ctrl;
  519. union perf_capabilities intel_cap;
  520. /*
  521. * Intel DebugStore bits
  522. */
  523. unsigned int bts :1,
  524. bts_active :1,
  525. pebs :1,
  526. pebs_active :1,
  527. pebs_broken :1,
  528. pebs_prec_dist :1,
  529. pebs_no_tlb :1;
  530. int pebs_record_size;
  531. int pebs_buffer_size;
  532. void (*drain_pebs)(struct pt_regs *regs);
  533. struct event_constraint *pebs_constraints;
  534. void (*pebs_aliases)(struct perf_event *event);
  535. int max_pebs_events;
  536. unsigned long large_pebs_flags;
  537. /*
  538. * Intel LBR
  539. */
  540. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  541. int lbr_nr; /* hardware stack size */
  542. u64 lbr_sel_mask; /* LBR_SELECT valid bits */
  543. const int *lbr_sel_map; /* lbr_select mappings */
  544. bool lbr_double_abort; /* duplicated lbr aborts */
  545. bool lbr_pt_coexist; /* (LBR|BTS) may coexist with PT */
  546. /*
  547. * Intel PT/LBR/BTS are exclusive
  548. */
  549. atomic_t lbr_exclusive[x86_lbr_exclusive_max];
  550. /*
  551. * AMD bits
  552. */
  553. unsigned int amd_nb_constraints : 1;
  554. /*
  555. * Extra registers for events
  556. */
  557. struct extra_reg *extra_regs;
  558. unsigned int flags;
  559. /*
  560. * Intel host/guest support (KVM)
  561. */
  562. struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
  563. /*
  564. * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
  565. */
  566. int (*check_period) (struct perf_event *event, u64 period);
  567. };
  568. struct x86_perf_task_context {
  569. u64 lbr_from[MAX_LBR_ENTRIES];
  570. u64 lbr_to[MAX_LBR_ENTRIES];
  571. u64 lbr_info[MAX_LBR_ENTRIES];
  572. int tos;
  573. int valid_lbrs;
  574. int lbr_callstack_users;
  575. int lbr_stack_state;
  576. int log_id;
  577. };
  578. #define x86_add_quirk(func_) \
  579. do { \
  580. static struct x86_pmu_quirk __quirk __initdata = { \
  581. .func = func_, \
  582. }; \
  583. __quirk.next = x86_pmu.quirks; \
  584. x86_pmu.quirks = &__quirk; \
  585. } while (0)
  586. /*
  587. * x86_pmu flags
  588. */
  589. #define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
  590. #define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
  591. #define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
  592. #define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
  593. #define PMU_FL_PEBS_ALL 0x10 /* all events are valid PEBS events */
  594. #define PMU_FL_TFA 0x20 /* deal with TSX force abort */
  595. #define EVENT_VAR(_id) event_attr_##_id
  596. #define EVENT_PTR(_id) &event_attr_##_id.attr.attr
  597. #define EVENT_ATTR(_name, _id) \
  598. static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
  599. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  600. .id = PERF_COUNT_HW_##_id, \
  601. .event_str = NULL, \
  602. };
  603. #define EVENT_ATTR_STR(_name, v, str) \
  604. static struct perf_pmu_events_attr event_attr_##v = { \
  605. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  606. .id = 0, \
  607. .event_str = str, \
  608. };
  609. #define EVENT_ATTR_STR_HT(_name, v, noht, ht) \
  610. static struct perf_pmu_events_ht_attr event_attr_##v = { \
  611. .attr = __ATTR(_name, 0444, events_ht_sysfs_show, NULL),\
  612. .id = 0, \
  613. .event_str_noht = noht, \
  614. .event_str_ht = ht, \
  615. }
  616. extern struct x86_pmu x86_pmu __read_mostly;
  617. static inline bool x86_pmu_has_lbr_callstack(void)
  618. {
  619. return x86_pmu.lbr_sel_map &&
  620. x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
  621. }
  622. DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  623. int x86_perf_event_set_period(struct perf_event *event);
  624. /*
  625. * Generalized hw caching related hw_event table, filled
  626. * in on a per model basis. A value of 0 means
  627. * 'not supported', -1 means 'hw_event makes no sense on
  628. * this CPU', any other value means the raw hw_event
  629. * ID.
  630. */
  631. #define C(x) PERF_COUNT_HW_CACHE_##x
  632. extern u64 __read_mostly hw_cache_event_ids
  633. [PERF_COUNT_HW_CACHE_MAX]
  634. [PERF_COUNT_HW_CACHE_OP_MAX]
  635. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  636. extern u64 __read_mostly hw_cache_extra_regs
  637. [PERF_COUNT_HW_CACHE_MAX]
  638. [PERF_COUNT_HW_CACHE_OP_MAX]
  639. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  640. u64 x86_perf_event_update(struct perf_event *event);
  641. static inline unsigned int x86_pmu_config_addr(int index)
  642. {
  643. return x86_pmu.eventsel + (x86_pmu.addr_offset ?
  644. x86_pmu.addr_offset(index, true) : index);
  645. }
  646. static inline unsigned int x86_pmu_event_addr(int index)
  647. {
  648. return x86_pmu.perfctr + (x86_pmu.addr_offset ?
  649. x86_pmu.addr_offset(index, false) : index);
  650. }
  651. static inline int x86_pmu_rdpmc_index(int index)
  652. {
  653. return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
  654. }
  655. int x86_add_exclusive(unsigned int what);
  656. void x86_del_exclusive(unsigned int what);
  657. int x86_reserve_hardware(void);
  658. void x86_release_hardware(void);
  659. int x86_pmu_max_precise(void);
  660. void hw_perf_lbr_event_destroy(struct perf_event *event);
  661. int x86_setup_perfctr(struct perf_event *event);
  662. int x86_pmu_hw_config(struct perf_event *event);
  663. void x86_pmu_disable_all(void);
  664. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  665. u64 enable_mask)
  666. {
  667. u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
  668. if (hwc->extra_reg.reg)
  669. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  670. wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
  671. }
  672. void x86_pmu_enable_all(int added);
  673. int perf_assign_events(struct event_constraint **constraints, int n,
  674. int wmin, int wmax, int gpmax, int *assign);
  675. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
  676. void x86_pmu_stop(struct perf_event *event, int flags);
  677. static inline void x86_pmu_disable_event(struct perf_event *event)
  678. {
  679. struct hw_perf_event *hwc = &event->hw;
  680. wrmsrl(hwc->config_base, hwc->config);
  681. }
  682. void x86_pmu_enable_event(struct perf_event *event);
  683. int x86_pmu_handle_irq(struct pt_regs *regs);
  684. extern struct event_constraint emptyconstraint;
  685. extern struct event_constraint unconstrained;
  686. static inline bool kernel_ip(unsigned long ip)
  687. {
  688. #ifdef CONFIG_X86_32
  689. return ip > PAGE_OFFSET;
  690. #else
  691. return (long)ip < 0;
  692. #endif
  693. }
  694. /*
  695. * Not all PMUs provide the right context information to place the reported IP
  696. * into full context. Specifically segment registers are typically not
  697. * supplied.
  698. *
  699. * Assuming the address is a linear address (it is for IBS), we fake the CS and
  700. * vm86 mode using the known zero-based code segment and 'fix up' the registers
  701. * to reflect this.
  702. *
  703. * Intel PEBS/LBR appear to typically provide the effective address, nothing
  704. * much we can do about that but pray and treat it like a linear address.
  705. */
  706. static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
  707. {
  708. regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
  709. if (regs->flags & X86_VM_MASK)
  710. regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
  711. regs->ip = ip;
  712. }
  713. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
  714. ssize_t intel_event_sysfs_show(char *page, u64 config);
  715. struct attribute **merge_attr(struct attribute **a, struct attribute **b);
  716. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
  717. char *page);
  718. ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
  719. char *page);
  720. #ifdef CONFIG_CPU_SUP_AMD
  721. int amd_pmu_init(void);
  722. #else /* CONFIG_CPU_SUP_AMD */
  723. static inline int amd_pmu_init(void)
  724. {
  725. return 0;
  726. }
  727. #endif /* CONFIG_CPU_SUP_AMD */
  728. #ifdef CONFIG_CPU_SUP_INTEL
  729. static inline bool intel_pmu_has_bts_period(struct perf_event *event, u64 period)
  730. {
  731. struct hw_perf_event *hwc = &event->hw;
  732. unsigned int hw_event, bts_event;
  733. if (event->attr.freq)
  734. return false;
  735. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  736. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  737. return hw_event == bts_event && period == 1;
  738. }
  739. static inline bool intel_pmu_has_bts(struct perf_event *event)
  740. {
  741. struct hw_perf_event *hwc = &event->hw;
  742. return intel_pmu_has_bts_period(event, hwc->sample_period);
  743. }
  744. int intel_pmu_save_and_restart(struct perf_event *event);
  745. struct event_constraint *
  746. x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  747. struct perf_event *event);
  748. extern int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu);
  749. extern void intel_cpuc_finish(struct cpu_hw_events *cpuc);
  750. int intel_pmu_init(void);
  751. void init_debug_store_on_cpu(int cpu);
  752. void fini_debug_store_on_cpu(int cpu);
  753. void release_ds_buffers(void);
  754. void reserve_ds_buffers(void);
  755. extern struct event_constraint bts_constraint;
  756. void intel_pmu_enable_bts(u64 config);
  757. void intel_pmu_disable_bts(void);
  758. int intel_pmu_drain_bts_buffer(void);
  759. extern struct event_constraint intel_core2_pebs_event_constraints[];
  760. extern struct event_constraint intel_atom_pebs_event_constraints[];
  761. extern struct event_constraint intel_slm_pebs_event_constraints[];
  762. extern struct event_constraint intel_glm_pebs_event_constraints[];
  763. extern struct event_constraint intel_glp_pebs_event_constraints[];
  764. extern struct event_constraint intel_nehalem_pebs_event_constraints[];
  765. extern struct event_constraint intel_westmere_pebs_event_constraints[];
  766. extern struct event_constraint intel_snb_pebs_event_constraints[];
  767. extern struct event_constraint intel_ivb_pebs_event_constraints[];
  768. extern struct event_constraint intel_hsw_pebs_event_constraints[];
  769. extern struct event_constraint intel_bdw_pebs_event_constraints[];
  770. extern struct event_constraint intel_skl_pebs_event_constraints[];
  771. struct event_constraint *intel_pebs_constraints(struct perf_event *event);
  772. void intel_pmu_pebs_add(struct perf_event *event);
  773. void intel_pmu_pebs_del(struct perf_event *event);
  774. void intel_pmu_pebs_enable(struct perf_event *event);
  775. void intel_pmu_pebs_disable(struct perf_event *event);
  776. void intel_pmu_pebs_enable_all(void);
  777. void intel_pmu_pebs_disable_all(void);
  778. void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in);
  779. void intel_pmu_auto_reload_read(struct perf_event *event);
  780. void intel_ds_init(void);
  781. void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
  782. u64 lbr_from_signext_quirk_wr(u64 val);
  783. void intel_pmu_lbr_reset(void);
  784. void intel_pmu_lbr_add(struct perf_event *event);
  785. void intel_pmu_lbr_del(struct perf_event *event);
  786. void intel_pmu_lbr_enable_all(bool pmi);
  787. void intel_pmu_lbr_disable_all(void);
  788. void intel_pmu_lbr_read(void);
  789. void intel_pmu_lbr_init_core(void);
  790. void intel_pmu_lbr_init_nhm(void);
  791. void intel_pmu_lbr_init_atom(void);
  792. void intel_pmu_lbr_init_slm(void);
  793. void intel_pmu_lbr_init_snb(void);
  794. void intel_pmu_lbr_init_hsw(void);
  795. void intel_pmu_lbr_init_skl(void);
  796. void intel_pmu_lbr_init_knl(void);
  797. void intel_pmu_pebs_data_source_nhm(void);
  798. void intel_pmu_pebs_data_source_skl(bool pmem);
  799. int intel_pmu_setup_lbr_filter(struct perf_event *event);
  800. void intel_pt_interrupt(void);
  801. int intel_bts_interrupt(void);
  802. void intel_bts_enable_local(void);
  803. void intel_bts_disable_local(void);
  804. int p4_pmu_init(void);
  805. int p6_pmu_init(void);
  806. int knc_pmu_init(void);
  807. static inline int is_ht_workaround_enabled(void)
  808. {
  809. return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
  810. }
  811. #else /* CONFIG_CPU_SUP_INTEL */
  812. static inline void reserve_ds_buffers(void)
  813. {
  814. }
  815. static inline void release_ds_buffers(void)
  816. {
  817. }
  818. static inline int intel_pmu_init(void)
  819. {
  820. return 0;
  821. }
  822. static inline int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)
  823. {
  824. return 0;
  825. }
  826. static inline void intel_cpuc_finish(struct cpu_hw_events *cpuc)
  827. {
  828. }
  829. static inline int is_ht_workaround_enabled(void)
  830. {
  831. return 0;
  832. }
  833. #endif /* CONFIG_CPU_SUP_INTEL */