perf_event.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /*
  2. * Performance events x86 architecture header
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <[email protected]>
  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, <[email protected]>
  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/fpu/xstate.h>
  16. #include <asm/intel_ds.h>
  17. #include <asm/cpu.h>
  18. /* To enable MSR tracing please use the generic trace points. */
  19. /*
  20. * | NHM/WSM | SNB |
  21. * register -------------------------------
  22. * | HT | no HT | HT | no HT |
  23. *-----------------------------------------
  24. * offcore | core | core | cpu | core |
  25. * lbr_sel | core | core | cpu | core |
  26. * ld_lat | cpu | core | cpu | core |
  27. *-----------------------------------------
  28. *
  29. * Given that there is a small number of shared regs,
  30. * we can pre-allocate their slot in the per-cpu
  31. * per-core reg tables.
  32. */
  33. enum extra_reg_type {
  34. EXTRA_REG_NONE = -1, /* not used */
  35. EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
  36. EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
  37. EXTRA_REG_LBR = 2, /* lbr_select */
  38. EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */
  39. EXTRA_REG_FE = 4, /* fe_* */
  40. EXTRA_REG_MAX /* number of entries needed */
  41. };
  42. struct event_constraint {
  43. union {
  44. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  45. u64 idxmsk64;
  46. };
  47. u64 code;
  48. u64 cmask;
  49. int weight;
  50. int overlap;
  51. int flags;
  52. unsigned int size;
  53. };
  54. static inline bool constraint_match(struct event_constraint *c, u64 ecode)
  55. {
  56. return ((ecode & c->cmask) - c->code) <= (u64)c->size;
  57. }
  58. #define PERF_ARCH(name, val) \
  59. PERF_X86_EVENT_##name = val,
  60. /*
  61. * struct hw_perf_event.flags flags
  62. */
  63. enum {
  64. #include "perf_event_flags.h"
  65. };
  66. #undef PERF_ARCH
  67. #define PERF_ARCH(name, val) \
  68. static_assert((PERF_X86_EVENT_##name & PERF_EVENT_FLAG_ARCH) == \
  69. PERF_X86_EVENT_##name);
  70. #include "perf_event_flags.h"
  71. #undef PERF_ARCH
  72. static inline bool is_topdown_count(struct perf_event *event)
  73. {
  74. return event->hw.flags & PERF_X86_EVENT_TOPDOWN;
  75. }
  76. static inline bool is_metric_event(struct perf_event *event)
  77. {
  78. u64 config = event->attr.config;
  79. return ((config & ARCH_PERFMON_EVENTSEL_EVENT) == 0) &&
  80. ((config & INTEL_ARCH_EVENT_MASK) >= INTEL_TD_METRIC_RETIRING) &&
  81. ((config & INTEL_ARCH_EVENT_MASK) <= INTEL_TD_METRIC_MAX);
  82. }
  83. static inline bool is_slots_event(struct perf_event *event)
  84. {
  85. return (event->attr.config & INTEL_ARCH_EVENT_MASK) == INTEL_TD_SLOTS;
  86. }
  87. static inline bool is_topdown_event(struct perf_event *event)
  88. {
  89. return is_metric_event(event) || is_slots_event(event);
  90. }
  91. struct amd_nb {
  92. int nb_id; /* NorthBridge id */
  93. int refcnt; /* reference count */
  94. struct perf_event *owners[X86_PMC_IDX_MAX];
  95. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  96. };
  97. #define PEBS_COUNTER_MASK ((1ULL << MAX_PEBS_EVENTS) - 1)
  98. #define PEBS_PMI_AFTER_EACH_RECORD BIT_ULL(60)
  99. #define PEBS_OUTPUT_OFFSET 61
  100. #define PEBS_OUTPUT_MASK (3ull << PEBS_OUTPUT_OFFSET)
  101. #define PEBS_OUTPUT_PT (1ull << PEBS_OUTPUT_OFFSET)
  102. #define PEBS_VIA_PT_MASK (PEBS_OUTPUT_PT | PEBS_PMI_AFTER_EACH_RECORD)
  103. /*
  104. * Flags PEBS can handle without an PMI.
  105. *
  106. * TID can only be handled by flushing at context switch.
  107. * REGS_USER can be handled for events limited to ring 3.
  108. *
  109. */
  110. #define LARGE_PEBS_FLAGS \
  111. (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
  112. PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
  113. PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
  114. PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR | \
  115. PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER | \
  116. PERF_SAMPLE_PERIOD | PERF_SAMPLE_CODE_PAGE_SIZE | \
  117. PERF_SAMPLE_WEIGHT_TYPE)
  118. #define PEBS_GP_REGS \
  119. ((1ULL << PERF_REG_X86_AX) | \
  120. (1ULL << PERF_REG_X86_BX) | \
  121. (1ULL << PERF_REG_X86_CX) | \
  122. (1ULL << PERF_REG_X86_DX) | \
  123. (1ULL << PERF_REG_X86_DI) | \
  124. (1ULL << PERF_REG_X86_SI) | \
  125. (1ULL << PERF_REG_X86_SP) | \
  126. (1ULL << PERF_REG_X86_BP) | \
  127. (1ULL << PERF_REG_X86_IP) | \
  128. (1ULL << PERF_REG_X86_FLAGS) | \
  129. (1ULL << PERF_REG_X86_R8) | \
  130. (1ULL << PERF_REG_X86_R9) | \
  131. (1ULL << PERF_REG_X86_R10) | \
  132. (1ULL << PERF_REG_X86_R11) | \
  133. (1ULL << PERF_REG_X86_R12) | \
  134. (1ULL << PERF_REG_X86_R13) | \
  135. (1ULL << PERF_REG_X86_R14) | \
  136. (1ULL << PERF_REG_X86_R15))
  137. /*
  138. * Per register state.
  139. */
  140. struct er_account {
  141. raw_spinlock_t lock; /* per-core: protect structure */
  142. u64 config; /* extra MSR config */
  143. u64 reg; /* extra MSR number */
  144. atomic_t ref; /* reference count */
  145. };
  146. /*
  147. * Per core/cpu state
  148. *
  149. * Used to coordinate shared registers between HT threads or
  150. * among events on a single PMU.
  151. */
  152. struct intel_shared_regs {
  153. struct er_account regs[EXTRA_REG_MAX];
  154. int refcnt; /* per-core: #HT threads */
  155. unsigned core_id; /* per-core: core id */
  156. };
  157. enum intel_excl_state_type {
  158. INTEL_EXCL_UNUSED = 0, /* counter is unused */
  159. INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */
  160. INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */
  161. };
  162. struct intel_excl_states {
  163. enum intel_excl_state_type state[X86_PMC_IDX_MAX];
  164. bool sched_started; /* true if scheduling has started */
  165. };
  166. struct intel_excl_cntrs {
  167. raw_spinlock_t lock;
  168. struct intel_excl_states states[2];
  169. union {
  170. u16 has_exclusive[2];
  171. u32 exclusive_present;
  172. };
  173. int refcnt; /* per-core: #HT threads */
  174. unsigned core_id; /* per-core: core id */
  175. };
  176. struct x86_perf_task_context;
  177. #define MAX_LBR_ENTRIES 32
  178. enum {
  179. LBR_FORMAT_32 = 0x00,
  180. LBR_FORMAT_LIP = 0x01,
  181. LBR_FORMAT_EIP = 0x02,
  182. LBR_FORMAT_EIP_FLAGS = 0x03,
  183. LBR_FORMAT_EIP_FLAGS2 = 0x04,
  184. LBR_FORMAT_INFO = 0x05,
  185. LBR_FORMAT_TIME = 0x06,
  186. LBR_FORMAT_INFO2 = 0x07,
  187. LBR_FORMAT_MAX_KNOWN = LBR_FORMAT_INFO2,
  188. };
  189. enum {
  190. X86_PERF_KFREE_SHARED = 0,
  191. X86_PERF_KFREE_EXCL = 1,
  192. X86_PERF_KFREE_MAX
  193. };
  194. struct cpu_hw_events {
  195. /*
  196. * Generic x86 PMC bits
  197. */
  198. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  199. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  200. unsigned long dirty[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  201. int enabled;
  202. int n_events; /* the # of events in the below arrays */
  203. int n_added; /* the # last events in the below arrays;
  204. they've never been enabled yet */
  205. int n_txn; /* the # last events in the below arrays;
  206. added in the current transaction */
  207. int n_txn_pair;
  208. int n_txn_metric;
  209. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  210. u64 tags[X86_PMC_IDX_MAX];
  211. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  212. struct event_constraint *event_constraint[X86_PMC_IDX_MAX];
  213. int n_excl; /* the number of exclusive events */
  214. unsigned int txn_flags;
  215. int is_fake;
  216. /*
  217. * Intel DebugStore bits
  218. */
  219. struct debug_store *ds;
  220. void *ds_pebs_vaddr;
  221. void *ds_bts_vaddr;
  222. u64 pebs_enabled;
  223. int n_pebs;
  224. int n_large_pebs;
  225. int n_pebs_via_pt;
  226. int pebs_output;
  227. /* Current super set of events hardware configuration */
  228. u64 pebs_data_cfg;
  229. u64 active_pebs_data_cfg;
  230. int pebs_record_size;
  231. /* Intel Fixed counter configuration */
  232. u64 fixed_ctrl_val;
  233. u64 active_fixed_ctrl_val;
  234. /*
  235. * Intel LBR bits
  236. */
  237. int lbr_users;
  238. int lbr_pebs_users;
  239. struct perf_branch_stack lbr_stack;
  240. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  241. union {
  242. struct er_account *lbr_sel;
  243. struct er_account *lbr_ctl;
  244. };
  245. u64 br_sel;
  246. void *last_task_ctx;
  247. int last_log_id;
  248. int lbr_select;
  249. void *lbr_xsave;
  250. /*
  251. * Intel host/guest exclude bits
  252. */
  253. u64 intel_ctrl_guest_mask;
  254. u64 intel_ctrl_host_mask;
  255. struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
  256. /*
  257. * Intel checkpoint mask
  258. */
  259. u64 intel_cp_status;
  260. /*
  261. * manage shared (per-core, per-cpu) registers
  262. * used on Intel NHM/WSM/SNB
  263. */
  264. struct intel_shared_regs *shared_regs;
  265. /*
  266. * manage exclusive counter access between hyperthread
  267. */
  268. struct event_constraint *constraint_list; /* in enable order */
  269. struct intel_excl_cntrs *excl_cntrs;
  270. int excl_thread_id; /* 0 or 1 */
  271. /*
  272. * SKL TSX_FORCE_ABORT shadow
  273. */
  274. u64 tfa_shadow;
  275. /*
  276. * Perf Metrics
  277. */
  278. /* number of accepted metrics events */
  279. int n_metric;
  280. /*
  281. * AMD specific bits
  282. */
  283. struct amd_nb *amd_nb;
  284. int brs_active; /* BRS is enabled */
  285. /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
  286. u64 perf_ctr_virt_mask;
  287. int n_pair; /* Large increment events */
  288. void *kfree_on_online[X86_PERF_KFREE_MAX];
  289. struct pmu *pmu;
  290. };
  291. #define __EVENT_CONSTRAINT_RANGE(c, e, n, m, w, o, f) { \
  292. { .idxmsk64 = (n) }, \
  293. .code = (c), \
  294. .size = (e) - (c), \
  295. .cmask = (m), \
  296. .weight = (w), \
  297. .overlap = (o), \
  298. .flags = f, \
  299. }
  300. #define __EVENT_CONSTRAINT(c, n, m, w, o, f) \
  301. __EVENT_CONSTRAINT_RANGE(c, c, n, m, w, o, f)
  302. #define EVENT_CONSTRAINT(c, n, m) \
  303. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
  304. /*
  305. * The constraint_match() function only works for 'simple' event codes
  306. * and not for extended (AMD64_EVENTSEL_EVENT) events codes.
  307. */
  308. #define EVENT_CONSTRAINT_RANGE(c, e, n, m) \
  309. __EVENT_CONSTRAINT_RANGE(c, e, n, m, HWEIGHT(n), 0, 0)
  310. #define INTEL_EXCLEVT_CONSTRAINT(c, n) \
  311. __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
  312. 0, PERF_X86_EVENT_EXCL)
  313. /*
  314. * The overlap flag marks event constraints with overlapping counter
  315. * masks. This is the case if the counter mask of such an event is not
  316. * a subset of any other counter mask of a constraint with an equal or
  317. * higher weight, e.g.:
  318. *
  319. * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  320. * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
  321. * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
  322. *
  323. * The event scheduler may not select the correct counter in the first
  324. * cycle because it needs to know which subsequent events will be
  325. * scheduled. It may fail to schedule the events then. So we set the
  326. * overlap flag for such constraints to give the scheduler a hint which
  327. * events to select for counter rescheduling.
  328. *
  329. * Care must be taken as the rescheduling algorithm is O(n!) which
  330. * will increase scheduling cycles for an over-committed system
  331. * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
  332. * and its counter masks must be kept at a minimum.
  333. */
  334. #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
  335. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
  336. /*
  337. * Constraint on the Event code.
  338. */
  339. #define INTEL_EVENT_CONSTRAINT(c, n) \
  340. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  341. /*
  342. * Constraint on a range of Event codes
  343. */
  344. #define INTEL_EVENT_CONSTRAINT_RANGE(c, e, n) \
  345. EVENT_CONSTRAINT_RANGE(c, e, n, ARCH_PERFMON_EVENTSEL_EVENT)
  346. /*
  347. * Constraint on the Event code + UMask + fixed-mask
  348. *
  349. * filter mask to validate fixed counter events.
  350. * the following filters disqualify for fixed counters:
  351. * - inv
  352. * - edge
  353. * - cnt-mask
  354. * - in_tx
  355. * - in_tx_checkpointed
  356. * The other filters are supported by fixed counters.
  357. * The any-thread option is supported starting with v3.
  358. */
  359. #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
  360. #define FIXED_EVENT_CONSTRAINT(c, n) \
  361. EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
  362. /*
  363. * The special metric counters do not actually exist. They are calculated from
  364. * the combination of the FxCtr3 + MSR_PERF_METRICS.
  365. *
  366. * The special metric counters are mapped to a dummy offset for the scheduler.
  367. * The sharing between multiple users of the same metric without multiplexing
  368. * is not allowed, even though the hardware supports that in principle.
  369. */
  370. #define METRIC_EVENT_CONSTRAINT(c, n) \
  371. EVENT_CONSTRAINT(c, (1ULL << (INTEL_PMC_IDX_METRIC_BASE + n)), \
  372. INTEL_ARCH_EVENT_MASK)
  373. /*
  374. * Constraint on the Event code + UMask
  375. */
  376. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  377. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  378. /* Constraint on specific umask bit only + event */
  379. #define INTEL_UBIT_EVENT_CONSTRAINT(c, n) \
  380. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT|(c))
  381. /* Like UEVENT_CONSTRAINT, but match flags too */
  382. #define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
  383. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
  384. #define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
  385. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
  386. HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
  387. #define INTEL_PLD_CONSTRAINT(c, n) \
  388. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  389. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
  390. #define INTEL_PSD_CONSTRAINT(c, n) \
  391. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  392. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_STLAT)
  393. #define INTEL_PST_CONSTRAINT(c, n) \
  394. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  395. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
  396. #define INTEL_HYBRID_LAT_CONSTRAINT(c, n) \
  397. __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  398. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LAT_HYBRID)
  399. /* Event constraint, but match on all event flags too. */
  400. #define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
  401. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS)
  402. #define INTEL_FLAGS_EVENT_CONSTRAINT_RANGE(c, e, n) \
  403. EVENT_CONSTRAINT_RANGE(c, e, n, ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS)
  404. /* Check only flags, but allow all event/umask */
  405. #define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
  406. EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
  407. /* Check flags and event code, and set the HSW store flag */
  408. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
  409. __EVENT_CONSTRAINT(code, n, \
  410. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  411. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  412. /* Check flags and event code, and set the HSW load flag */
  413. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
  414. __EVENT_CONSTRAINT(code, n, \
  415. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  416. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  417. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(code, end, n) \
  418. __EVENT_CONSTRAINT_RANGE(code, end, n, \
  419. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  420. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  421. #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
  422. __EVENT_CONSTRAINT(code, n, \
  423. ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
  424. HWEIGHT(n), 0, \
  425. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  426. /* Check flags and event code/umask, and set the HSW store flag */
  427. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
  428. __EVENT_CONSTRAINT(code, n, \
  429. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  430. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
  431. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
  432. __EVENT_CONSTRAINT(code, n, \
  433. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  434. HWEIGHT(n), 0, \
  435. PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
  436. /* Check flags and event code/umask, and set the HSW load flag */
  437. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
  438. __EVENT_CONSTRAINT(code, n, \
  439. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  440. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
  441. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
  442. __EVENT_CONSTRAINT(code, n, \
  443. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  444. HWEIGHT(n), 0, \
  445. PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
  446. /* Check flags and event code/umask, and set the HSW N/A flag */
  447. #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
  448. __EVENT_CONSTRAINT(code, n, \
  449. INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
  450. HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
  451. /*
  452. * We define the end marker as having a weight of -1
  453. * to enable blacklisting of events using a counter bitmask
  454. * of zero and thus a weight of zero.
  455. * The end marker has a weight that cannot possibly be
  456. * obtained from counting the bits in the bitmask.
  457. */
  458. #define EVENT_CONSTRAINT_END { .weight = -1 }
  459. /*
  460. * Check for end marker with weight == -1
  461. */
  462. #define for_each_event_constraint(e, c) \
  463. for ((e) = (c); (e)->weight != -1; (e)++)
  464. /*
  465. * Extra registers for specific events.
  466. *
  467. * Some events need large masks and require external MSRs.
  468. * Those extra MSRs end up being shared for all events on
  469. * a PMU and sometimes between PMU of sibling HT threads.
  470. * In either case, the kernel needs to handle conflicting
  471. * accesses to those extra, shared, regs. The data structure
  472. * to manage those registers is stored in cpu_hw_event.
  473. */
  474. struct extra_reg {
  475. unsigned int event;
  476. unsigned int msr;
  477. u64 config_mask;
  478. u64 valid_mask;
  479. int idx; /* per_xxx->regs[] reg index */
  480. bool extra_msr_access;
  481. };
  482. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  483. .event = (e), \
  484. .msr = (ms), \
  485. .config_mask = (m), \
  486. .valid_mask = (vm), \
  487. .idx = EXTRA_REG_##i, \
  488. .extra_msr_access = true, \
  489. }
  490. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  491. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  492. #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
  493. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
  494. ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
  495. #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
  496. INTEL_UEVENT_EXTRA_REG(c, \
  497. MSR_PEBS_LD_LAT_THRESHOLD, \
  498. 0xffff, \
  499. LDLAT)
  500. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  501. union perf_capabilities {
  502. struct {
  503. u64 lbr_format:6;
  504. u64 pebs_trap:1;
  505. u64 pebs_arch_reg:1;
  506. u64 pebs_format:4;
  507. u64 smm_freeze:1;
  508. /*
  509. * PMU supports separate counter range for writing
  510. * values > 32bit.
  511. */
  512. u64 full_width_write:1;
  513. u64 pebs_baseline:1;
  514. u64 perf_metrics:1;
  515. u64 pebs_output_pt_available:1;
  516. u64 anythread_deprecated:1;
  517. };
  518. u64 capabilities;
  519. };
  520. struct x86_pmu_quirk {
  521. struct x86_pmu_quirk *next;
  522. void (*func)(void);
  523. };
  524. union x86_pmu_config {
  525. struct {
  526. u64 event:8,
  527. umask:8,
  528. usr:1,
  529. os:1,
  530. edge:1,
  531. pc:1,
  532. interrupt:1,
  533. __reserved1:1,
  534. en:1,
  535. inv:1,
  536. cmask:8,
  537. event2:4,
  538. __reserved2:4,
  539. go:1,
  540. ho:1;
  541. } bits;
  542. u64 value;
  543. };
  544. #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
  545. enum {
  546. x86_lbr_exclusive_lbr,
  547. x86_lbr_exclusive_bts,
  548. x86_lbr_exclusive_pt,
  549. x86_lbr_exclusive_max,
  550. };
  551. #define PERF_PEBS_DATA_SOURCE_MAX 0x10
  552. struct x86_hybrid_pmu {
  553. struct pmu pmu;
  554. const char *name;
  555. u8 cpu_type;
  556. cpumask_t supported_cpus;
  557. union perf_capabilities intel_cap;
  558. u64 intel_ctrl;
  559. int max_pebs_events;
  560. int num_counters;
  561. int num_counters_fixed;
  562. struct event_constraint unconstrained;
  563. u64 hw_cache_event_ids
  564. [PERF_COUNT_HW_CACHE_MAX]
  565. [PERF_COUNT_HW_CACHE_OP_MAX]
  566. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  567. u64 hw_cache_extra_regs
  568. [PERF_COUNT_HW_CACHE_MAX]
  569. [PERF_COUNT_HW_CACHE_OP_MAX]
  570. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  571. struct event_constraint *event_constraints;
  572. struct event_constraint *pebs_constraints;
  573. struct extra_reg *extra_regs;
  574. unsigned int late_ack :1,
  575. mid_ack :1,
  576. enabled_ack :1;
  577. u64 pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX];
  578. };
  579. static __always_inline struct x86_hybrid_pmu *hybrid_pmu(struct pmu *pmu)
  580. {
  581. return container_of(pmu, struct x86_hybrid_pmu, pmu);
  582. }
  583. extern struct static_key_false perf_is_hybrid;
  584. #define is_hybrid() static_branch_unlikely(&perf_is_hybrid)
  585. #define hybrid(_pmu, _field) \
  586. (*({ \
  587. typeof(&x86_pmu._field) __Fp = &x86_pmu._field; \
  588. \
  589. if (is_hybrid() && (_pmu)) \
  590. __Fp = &hybrid_pmu(_pmu)->_field; \
  591. \
  592. __Fp; \
  593. }))
  594. #define hybrid_var(_pmu, _var) \
  595. (*({ \
  596. typeof(&_var) __Fp = &_var; \
  597. \
  598. if (is_hybrid() && (_pmu)) \
  599. __Fp = &hybrid_pmu(_pmu)->_var; \
  600. \
  601. __Fp; \
  602. }))
  603. #define hybrid_bit(_pmu, _field) \
  604. ({ \
  605. bool __Fp = x86_pmu._field; \
  606. \
  607. if (is_hybrid() && (_pmu)) \
  608. __Fp = hybrid_pmu(_pmu)->_field; \
  609. \
  610. __Fp; \
  611. })
  612. enum hybrid_pmu_type {
  613. hybrid_big = 0x40,
  614. hybrid_small = 0x20,
  615. hybrid_big_small = hybrid_big | hybrid_small,
  616. };
  617. #define X86_HYBRID_PMU_ATOM_IDX 0
  618. #define X86_HYBRID_PMU_CORE_IDX 1
  619. #define X86_HYBRID_NUM_PMUS 2
  620. /*
  621. * struct x86_pmu - generic x86 pmu
  622. */
  623. struct x86_pmu {
  624. /*
  625. * Generic x86 PMC bits
  626. */
  627. const char *name;
  628. int version;
  629. int (*handle_irq)(struct pt_regs *);
  630. void (*disable_all)(void);
  631. void (*enable_all)(int added);
  632. void (*enable)(struct perf_event *);
  633. void (*disable)(struct perf_event *);
  634. void (*assign)(struct perf_event *event, int idx);
  635. void (*add)(struct perf_event *);
  636. void (*del)(struct perf_event *);
  637. void (*read)(struct perf_event *event);
  638. int (*set_period)(struct perf_event *event);
  639. u64 (*update)(struct perf_event *event);
  640. int (*hw_config)(struct perf_event *event);
  641. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  642. unsigned eventsel;
  643. unsigned perfctr;
  644. int (*addr_offset)(int index, bool eventsel);
  645. int (*rdpmc_index)(int index);
  646. u64 (*event_map)(int);
  647. int max_events;
  648. int num_counters;
  649. int num_counters_fixed;
  650. int cntval_bits;
  651. u64 cntval_mask;
  652. union {
  653. unsigned long events_maskl;
  654. unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
  655. };
  656. int events_mask_len;
  657. int apic;
  658. u64 max_period;
  659. struct event_constraint *
  660. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  661. int idx,
  662. struct perf_event *event);
  663. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  664. struct perf_event *event);
  665. void (*start_scheduling)(struct cpu_hw_events *cpuc);
  666. void (*commit_scheduling)(struct cpu_hw_events *cpuc, int idx, int cntr);
  667. void (*stop_scheduling)(struct cpu_hw_events *cpuc);
  668. struct event_constraint *event_constraints;
  669. struct x86_pmu_quirk *quirks;
  670. void (*limit_period)(struct perf_event *event, s64 *l);
  671. /* PMI handler bits */
  672. unsigned int late_ack :1,
  673. mid_ack :1,
  674. enabled_ack :1;
  675. /*
  676. * sysfs attrs
  677. */
  678. int attr_rdpmc_broken;
  679. int attr_rdpmc;
  680. struct attribute **format_attrs;
  681. ssize_t (*events_sysfs_show)(char *page, u64 config);
  682. const struct attribute_group **attr_update;
  683. unsigned long attr_freeze_on_smi;
  684. /*
  685. * CPU Hotplug hooks
  686. */
  687. int (*cpu_prepare)(int cpu);
  688. void (*cpu_starting)(int cpu);
  689. void (*cpu_dying)(int cpu);
  690. void (*cpu_dead)(int cpu);
  691. void (*check_microcode)(void);
  692. void (*sched_task)(struct perf_event_context *ctx,
  693. bool sched_in);
  694. /*
  695. * Intel Arch Perfmon v2+
  696. */
  697. u64 intel_ctrl;
  698. union perf_capabilities intel_cap;
  699. /*
  700. * Intel DebugStore bits
  701. */
  702. unsigned int bts :1,
  703. bts_active :1,
  704. pebs :1,
  705. pebs_active :1,
  706. pebs_broken :1,
  707. pebs_prec_dist :1,
  708. pebs_no_tlb :1,
  709. pebs_no_isolation :1,
  710. pebs_block :1,
  711. pebs_ept :1;
  712. int pebs_record_size;
  713. int pebs_buffer_size;
  714. int max_pebs_events;
  715. void (*drain_pebs)(struct pt_regs *regs, struct perf_sample_data *data);
  716. struct event_constraint *pebs_constraints;
  717. void (*pebs_aliases)(struct perf_event *event);
  718. u64 (*pebs_latency_data)(struct perf_event *event, u64 status);
  719. unsigned long large_pebs_flags;
  720. u64 rtm_abort_event;
  721. u64 pebs_capable;
  722. /*
  723. * Intel LBR
  724. */
  725. unsigned int lbr_tos, lbr_from, lbr_to,
  726. lbr_info, lbr_nr; /* LBR base regs and size */
  727. union {
  728. u64 lbr_sel_mask; /* LBR_SELECT valid bits */
  729. u64 lbr_ctl_mask; /* LBR_CTL valid bits */
  730. };
  731. union {
  732. const int *lbr_sel_map; /* lbr_select mappings */
  733. int *lbr_ctl_map; /* LBR_CTL mappings */
  734. };
  735. bool lbr_double_abort; /* duplicated lbr aborts */
  736. bool lbr_pt_coexist; /* (LBR|BTS) may coexist with PT */
  737. unsigned int lbr_has_info:1;
  738. unsigned int lbr_has_tsx:1;
  739. unsigned int lbr_from_flags:1;
  740. unsigned int lbr_to_cycles:1;
  741. /*
  742. * Intel Architectural LBR CPUID Enumeration
  743. */
  744. unsigned int lbr_depth_mask:8;
  745. unsigned int lbr_deep_c_reset:1;
  746. unsigned int lbr_lip:1;
  747. unsigned int lbr_cpl:1;
  748. unsigned int lbr_filter:1;
  749. unsigned int lbr_call_stack:1;
  750. unsigned int lbr_mispred:1;
  751. unsigned int lbr_timed_lbr:1;
  752. unsigned int lbr_br_type:1;
  753. void (*lbr_reset)(void);
  754. void (*lbr_read)(struct cpu_hw_events *cpuc);
  755. void (*lbr_save)(void *ctx);
  756. void (*lbr_restore)(void *ctx);
  757. /*
  758. * Intel PT/LBR/BTS are exclusive
  759. */
  760. atomic_t lbr_exclusive[x86_lbr_exclusive_max];
  761. /*
  762. * Intel perf metrics
  763. */
  764. int num_topdown_events;
  765. /*
  766. * perf task context (i.e. struct perf_event_context::task_ctx_data)
  767. * switch helper to bridge calls from perf/core to perf/x86.
  768. * See struct pmu::swap_task_ctx() usage for examples;
  769. */
  770. void (*swap_task_ctx)(struct perf_event_context *prev,
  771. struct perf_event_context *next);
  772. /*
  773. * AMD bits
  774. */
  775. unsigned int amd_nb_constraints : 1;
  776. u64 perf_ctr_pair_en;
  777. /*
  778. * Extra registers for events
  779. */
  780. struct extra_reg *extra_regs;
  781. unsigned int flags;
  782. /*
  783. * Intel host/guest support (KVM)
  784. */
  785. struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr, void *data);
  786. /*
  787. * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
  788. */
  789. int (*check_period) (struct perf_event *event, u64 period);
  790. int (*aux_output_match) (struct perf_event *event);
  791. int (*filter_match)(struct perf_event *event);
  792. /*
  793. * Hybrid support
  794. *
  795. * Most PMU capabilities are the same among different hybrid PMUs.
  796. * The global x86_pmu saves the architecture capabilities, which
  797. * are available for all PMUs. The hybrid_pmu only includes the
  798. * unique capabilities.
  799. */
  800. int num_hybrid_pmus;
  801. struct x86_hybrid_pmu *hybrid_pmu;
  802. u8 (*get_hybrid_cpu_type) (void);
  803. };
  804. struct x86_perf_task_context_opt {
  805. int lbr_callstack_users;
  806. int lbr_stack_state;
  807. int log_id;
  808. };
  809. struct x86_perf_task_context {
  810. u64 lbr_sel;
  811. int tos;
  812. int valid_lbrs;
  813. struct x86_perf_task_context_opt opt;
  814. struct lbr_entry lbr[MAX_LBR_ENTRIES];
  815. };
  816. struct x86_perf_task_context_arch_lbr {
  817. struct x86_perf_task_context_opt opt;
  818. struct lbr_entry entries[];
  819. };
  820. /*
  821. * Add padding to guarantee the 64-byte alignment of the state buffer.
  822. *
  823. * The structure is dynamically allocated. The size of the LBR state may vary
  824. * based on the number of LBR registers.
  825. *
  826. * Do not put anything after the LBR state.
  827. */
  828. struct x86_perf_task_context_arch_lbr_xsave {
  829. struct x86_perf_task_context_opt opt;
  830. union {
  831. struct xregs_state xsave;
  832. struct {
  833. struct fxregs_state i387;
  834. struct xstate_header header;
  835. struct arch_lbr_state lbr;
  836. } __attribute__ ((packed, aligned (XSAVE_ALIGNMENT)));
  837. };
  838. };
  839. #define x86_add_quirk(func_) \
  840. do { \
  841. static struct x86_pmu_quirk __quirk __initdata = { \
  842. .func = func_, \
  843. }; \
  844. __quirk.next = x86_pmu.quirks; \
  845. x86_pmu.quirks = &__quirk; \
  846. } while (0)
  847. /*
  848. * x86_pmu flags
  849. */
  850. #define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
  851. #define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
  852. #define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
  853. #define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
  854. #define PMU_FL_PEBS_ALL 0x10 /* all events are valid PEBS events */
  855. #define PMU_FL_TFA 0x20 /* deal with TSX force abort */
  856. #define PMU_FL_PAIR 0x40 /* merge counters for large incr. events */
  857. #define PMU_FL_INSTR_LATENCY 0x80 /* Support Instruction Latency in PEBS Memory Info Record */
  858. #define PMU_FL_MEM_LOADS_AUX 0x100 /* Require an auxiliary event for the complete memory info */
  859. #define EVENT_VAR(_id) event_attr_##_id
  860. #define EVENT_PTR(_id) &event_attr_##_id.attr.attr
  861. #define EVENT_ATTR(_name, _id) \
  862. static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
  863. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  864. .id = PERF_COUNT_HW_##_id, \
  865. .event_str = NULL, \
  866. };
  867. #define EVENT_ATTR_STR(_name, v, str) \
  868. static struct perf_pmu_events_attr event_attr_##v = { \
  869. .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
  870. .id = 0, \
  871. .event_str = str, \
  872. };
  873. #define EVENT_ATTR_STR_HT(_name, v, noht, ht) \
  874. static struct perf_pmu_events_ht_attr event_attr_##v = { \
  875. .attr = __ATTR(_name, 0444, events_ht_sysfs_show, NULL),\
  876. .id = 0, \
  877. .event_str_noht = noht, \
  878. .event_str_ht = ht, \
  879. }
  880. #define EVENT_ATTR_STR_HYBRID(_name, v, str, _pmu) \
  881. static struct perf_pmu_events_hybrid_attr event_attr_##v = { \
  882. .attr = __ATTR(_name, 0444, events_hybrid_sysfs_show, NULL),\
  883. .id = 0, \
  884. .event_str = str, \
  885. .pmu_type = _pmu, \
  886. }
  887. #define FORMAT_HYBRID_PTR(_id) (&format_attr_hybrid_##_id.attr.attr)
  888. #define FORMAT_ATTR_HYBRID(_name, _pmu) \
  889. static struct perf_pmu_format_hybrid_attr format_attr_hybrid_##_name = {\
  890. .attr = __ATTR_RO(_name), \
  891. .pmu_type = _pmu, \
  892. }
  893. struct pmu *x86_get_pmu(unsigned int cpu);
  894. extern struct x86_pmu x86_pmu __read_mostly;
  895. DECLARE_STATIC_CALL(x86_pmu_set_period, *x86_pmu.set_period);
  896. DECLARE_STATIC_CALL(x86_pmu_update, *x86_pmu.update);
  897. static __always_inline struct x86_perf_task_context_opt *task_context_opt(void *ctx)
  898. {
  899. if (static_cpu_has(X86_FEATURE_ARCH_LBR))
  900. return &((struct x86_perf_task_context_arch_lbr *)ctx)->opt;
  901. return &((struct x86_perf_task_context *)ctx)->opt;
  902. }
  903. static inline bool x86_pmu_has_lbr_callstack(void)
  904. {
  905. return x86_pmu.lbr_sel_map &&
  906. x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
  907. }
  908. DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  909. DECLARE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  910. int x86_perf_event_set_period(struct perf_event *event);
  911. /*
  912. * Generalized hw caching related hw_event table, filled
  913. * in on a per model basis. A value of 0 means
  914. * 'not supported', -1 means 'hw_event makes no sense on
  915. * this CPU', any other value means the raw hw_event
  916. * ID.
  917. */
  918. #define C(x) PERF_COUNT_HW_CACHE_##x
  919. extern u64 __read_mostly hw_cache_event_ids
  920. [PERF_COUNT_HW_CACHE_MAX]
  921. [PERF_COUNT_HW_CACHE_OP_MAX]
  922. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  923. extern u64 __read_mostly hw_cache_extra_regs
  924. [PERF_COUNT_HW_CACHE_MAX]
  925. [PERF_COUNT_HW_CACHE_OP_MAX]
  926. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  927. u64 x86_perf_event_update(struct perf_event *event);
  928. static inline unsigned int x86_pmu_config_addr(int index)
  929. {
  930. return x86_pmu.eventsel + (x86_pmu.addr_offset ?
  931. x86_pmu.addr_offset(index, true) : index);
  932. }
  933. static inline unsigned int x86_pmu_event_addr(int index)
  934. {
  935. return x86_pmu.perfctr + (x86_pmu.addr_offset ?
  936. x86_pmu.addr_offset(index, false) : index);
  937. }
  938. static inline int x86_pmu_rdpmc_index(int index)
  939. {
  940. return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
  941. }
  942. bool check_hw_exists(struct pmu *pmu, int num_counters,
  943. int num_counters_fixed);
  944. int x86_add_exclusive(unsigned int what);
  945. void x86_del_exclusive(unsigned int what);
  946. int x86_reserve_hardware(void);
  947. void x86_release_hardware(void);
  948. int x86_pmu_max_precise(void);
  949. void hw_perf_lbr_event_destroy(struct perf_event *event);
  950. int x86_setup_perfctr(struct perf_event *event);
  951. int x86_pmu_hw_config(struct perf_event *event);
  952. void x86_pmu_disable_all(void);
  953. static inline bool has_amd_brs(struct hw_perf_event *hwc)
  954. {
  955. return hwc->flags & PERF_X86_EVENT_AMD_BRS;
  956. }
  957. static inline bool is_counter_pair(struct hw_perf_event *hwc)
  958. {
  959. return hwc->flags & PERF_X86_EVENT_PAIR;
  960. }
  961. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  962. u64 enable_mask)
  963. {
  964. u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
  965. if (hwc->extra_reg.reg)
  966. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  967. /*
  968. * Add enabled Merge event on next counter
  969. * if large increment event being enabled on this counter
  970. */
  971. if (is_counter_pair(hwc))
  972. wrmsrl(x86_pmu_config_addr(hwc->idx + 1), x86_pmu.perf_ctr_pair_en);
  973. wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
  974. }
  975. void x86_pmu_enable_all(int added);
  976. int perf_assign_events(struct event_constraint **constraints, int n,
  977. int wmin, int wmax, int gpmax, int *assign);
  978. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
  979. void x86_pmu_stop(struct perf_event *event, int flags);
  980. static inline void x86_pmu_disable_event(struct perf_event *event)
  981. {
  982. u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
  983. struct hw_perf_event *hwc = &event->hw;
  984. wrmsrl(hwc->config_base, hwc->config & ~disable_mask);
  985. if (is_counter_pair(hwc))
  986. wrmsrl(x86_pmu_config_addr(hwc->idx + 1), 0);
  987. }
  988. void x86_pmu_enable_event(struct perf_event *event);
  989. int x86_pmu_handle_irq(struct pt_regs *regs);
  990. void x86_pmu_show_pmu_cap(int num_counters, int num_counters_fixed,
  991. u64 intel_ctrl);
  992. void x86_pmu_update_cpu_context(struct pmu *pmu, int cpu);
  993. extern struct event_constraint emptyconstraint;
  994. extern struct event_constraint unconstrained;
  995. static inline bool kernel_ip(unsigned long ip)
  996. {
  997. #ifdef CONFIG_X86_32
  998. return ip > PAGE_OFFSET;
  999. #else
  1000. return (long)ip < 0;
  1001. #endif
  1002. }
  1003. /*
  1004. * Not all PMUs provide the right context information to place the reported IP
  1005. * into full context. Specifically segment registers are typically not
  1006. * supplied.
  1007. *
  1008. * Assuming the address is a linear address (it is for IBS), we fake the CS and
  1009. * vm86 mode using the known zero-based code segment and 'fix up' the registers
  1010. * to reflect this.
  1011. *
  1012. * Intel PEBS/LBR appear to typically provide the effective address, nothing
  1013. * much we can do about that but pray and treat it like a linear address.
  1014. */
  1015. static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
  1016. {
  1017. regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
  1018. if (regs->flags & X86_VM_MASK)
  1019. regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
  1020. regs->ip = ip;
  1021. }
  1022. /*
  1023. * x86control flow change classification
  1024. * x86control flow changes include branches, interrupts, traps, faults
  1025. */
  1026. enum {
  1027. X86_BR_NONE = 0, /* unknown */
  1028. X86_BR_USER = 1 << 0, /* branch target is user */
  1029. X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
  1030. X86_BR_CALL = 1 << 2, /* call */
  1031. X86_BR_RET = 1 << 3, /* return */
  1032. X86_BR_SYSCALL = 1 << 4, /* syscall */
  1033. X86_BR_SYSRET = 1 << 5, /* syscall return */
  1034. X86_BR_INT = 1 << 6, /* sw interrupt */
  1035. X86_BR_IRET = 1 << 7, /* return from interrupt */
  1036. X86_BR_JCC = 1 << 8, /* conditional */
  1037. X86_BR_JMP = 1 << 9, /* jump */
  1038. X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
  1039. X86_BR_IND_CALL = 1 << 11,/* indirect calls */
  1040. X86_BR_ABORT = 1 << 12,/* transaction abort */
  1041. X86_BR_IN_TX = 1 << 13,/* in transaction */
  1042. X86_BR_NO_TX = 1 << 14,/* not in transaction */
  1043. X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
  1044. X86_BR_CALL_STACK = 1 << 16,/* call stack */
  1045. X86_BR_IND_JMP = 1 << 17,/* indirect jump */
  1046. X86_BR_TYPE_SAVE = 1 << 18,/* indicate to save branch type */
  1047. };
  1048. #define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
  1049. #define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
  1050. #define X86_BR_ANY \
  1051. (X86_BR_CALL |\
  1052. X86_BR_RET |\
  1053. X86_BR_SYSCALL |\
  1054. X86_BR_SYSRET |\
  1055. X86_BR_INT |\
  1056. X86_BR_IRET |\
  1057. X86_BR_JCC |\
  1058. X86_BR_JMP |\
  1059. X86_BR_IRQ |\
  1060. X86_BR_ABORT |\
  1061. X86_BR_IND_CALL |\
  1062. X86_BR_IND_JMP |\
  1063. X86_BR_ZERO_CALL)
  1064. #define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
  1065. #define X86_BR_ANY_CALL \
  1066. (X86_BR_CALL |\
  1067. X86_BR_IND_CALL |\
  1068. X86_BR_ZERO_CALL |\
  1069. X86_BR_SYSCALL |\
  1070. X86_BR_IRQ |\
  1071. X86_BR_INT)
  1072. int common_branch_type(int type);
  1073. int branch_type(unsigned long from, unsigned long to, int abort);
  1074. int branch_type_fused(unsigned long from, unsigned long to, int abort,
  1075. int *offset);
  1076. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
  1077. ssize_t intel_event_sysfs_show(char *page, u64 config);
  1078. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
  1079. char *page);
  1080. ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
  1081. char *page);
  1082. ssize_t events_hybrid_sysfs_show(struct device *dev,
  1083. struct device_attribute *attr,
  1084. char *page);
  1085. static inline bool fixed_counter_disabled(int i, struct pmu *pmu)
  1086. {
  1087. u64 intel_ctrl = hybrid(pmu, intel_ctrl);
  1088. return !(intel_ctrl >> (i + INTEL_PMC_IDX_FIXED));
  1089. }
  1090. #ifdef CONFIG_CPU_SUP_AMD
  1091. int amd_pmu_init(void);
  1092. int amd_pmu_lbr_init(void);
  1093. void amd_pmu_lbr_reset(void);
  1094. void amd_pmu_lbr_read(void);
  1095. void amd_pmu_lbr_add(struct perf_event *event);
  1096. void amd_pmu_lbr_del(struct perf_event *event);
  1097. void amd_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
  1098. void amd_pmu_lbr_enable_all(void);
  1099. void amd_pmu_lbr_disable_all(void);
  1100. int amd_pmu_lbr_hw_config(struct perf_event *event);
  1101. #ifdef CONFIG_PERF_EVENTS_AMD_BRS
  1102. #define AMD_FAM19H_BRS_EVENT 0xc4 /* RETIRED_TAKEN_BRANCH_INSTRUCTIONS */
  1103. int amd_brs_init(void);
  1104. void amd_brs_disable(void);
  1105. void amd_brs_enable(void);
  1106. void amd_brs_enable_all(void);
  1107. void amd_brs_disable_all(void);
  1108. void amd_brs_drain(void);
  1109. void amd_brs_lopwr_init(void);
  1110. void amd_brs_disable_all(void);
  1111. int amd_brs_hw_config(struct perf_event *event);
  1112. void amd_brs_reset(void);
  1113. static inline void amd_pmu_brs_add(struct perf_event *event)
  1114. {
  1115. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1116. perf_sched_cb_inc(event->ctx->pmu);
  1117. cpuc->lbr_users++;
  1118. /*
  1119. * No need to reset BRS because it is reset
  1120. * on brs_enable() and it is saturating
  1121. */
  1122. }
  1123. static inline void amd_pmu_brs_del(struct perf_event *event)
  1124. {
  1125. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1126. cpuc->lbr_users--;
  1127. WARN_ON_ONCE(cpuc->lbr_users < 0);
  1128. perf_sched_cb_dec(event->ctx->pmu);
  1129. }
  1130. void amd_pmu_brs_sched_task(struct perf_event_context *ctx, bool sched_in);
  1131. #else
  1132. static inline int amd_brs_init(void)
  1133. {
  1134. return 0;
  1135. }
  1136. static inline void amd_brs_disable(void) {}
  1137. static inline void amd_brs_enable(void) {}
  1138. static inline void amd_brs_drain(void) {}
  1139. static inline void amd_brs_lopwr_init(void) {}
  1140. static inline void amd_brs_disable_all(void) {}
  1141. static inline int amd_brs_hw_config(struct perf_event *event)
  1142. {
  1143. return 0;
  1144. }
  1145. static inline void amd_brs_reset(void) {}
  1146. static inline void amd_pmu_brs_add(struct perf_event *event)
  1147. {
  1148. }
  1149. static inline void amd_pmu_brs_del(struct perf_event *event)
  1150. {
  1151. }
  1152. static inline void amd_pmu_brs_sched_task(struct perf_event_context *ctx, bool sched_in)
  1153. {
  1154. }
  1155. static inline void amd_brs_enable_all(void)
  1156. {
  1157. }
  1158. #endif
  1159. #else /* CONFIG_CPU_SUP_AMD */
  1160. static inline int amd_pmu_init(void)
  1161. {
  1162. return 0;
  1163. }
  1164. static inline int amd_brs_init(void)
  1165. {
  1166. return -EOPNOTSUPP;
  1167. }
  1168. static inline void amd_brs_drain(void)
  1169. {
  1170. }
  1171. static inline void amd_brs_enable_all(void)
  1172. {
  1173. }
  1174. static inline void amd_brs_disable_all(void)
  1175. {
  1176. }
  1177. #endif /* CONFIG_CPU_SUP_AMD */
  1178. static inline int is_pebs_pt(struct perf_event *event)
  1179. {
  1180. return !!(event->hw.flags & PERF_X86_EVENT_PEBS_VIA_PT);
  1181. }
  1182. #ifdef CONFIG_CPU_SUP_INTEL
  1183. static inline bool intel_pmu_has_bts_period(struct perf_event *event, u64 period)
  1184. {
  1185. struct hw_perf_event *hwc = &event->hw;
  1186. unsigned int hw_event, bts_event;
  1187. if (event->attr.freq)
  1188. return false;
  1189. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  1190. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  1191. return hw_event == bts_event && period == 1;
  1192. }
  1193. static inline bool intel_pmu_has_bts(struct perf_event *event)
  1194. {
  1195. struct hw_perf_event *hwc = &event->hw;
  1196. return intel_pmu_has_bts_period(event, hwc->sample_period);
  1197. }
  1198. static __always_inline void __intel_pmu_pebs_disable_all(void)
  1199. {
  1200. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  1201. }
  1202. static __always_inline void __intel_pmu_arch_lbr_disable(void)
  1203. {
  1204. wrmsrl(MSR_ARCH_LBR_CTL, 0);
  1205. }
  1206. static __always_inline void __intel_pmu_lbr_disable(void)
  1207. {
  1208. u64 debugctl;
  1209. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  1210. debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
  1211. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  1212. }
  1213. int intel_pmu_save_and_restart(struct perf_event *event);
  1214. struct event_constraint *
  1215. x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  1216. struct perf_event *event);
  1217. extern int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu);
  1218. extern void intel_cpuc_finish(struct cpu_hw_events *cpuc);
  1219. int intel_pmu_init(void);
  1220. void init_debug_store_on_cpu(int cpu);
  1221. void fini_debug_store_on_cpu(int cpu);
  1222. void release_ds_buffers(void);
  1223. void reserve_ds_buffers(void);
  1224. void release_lbr_buffers(void);
  1225. void reserve_lbr_buffers(void);
  1226. extern struct event_constraint bts_constraint;
  1227. extern struct event_constraint vlbr_constraint;
  1228. void intel_pmu_enable_bts(u64 config);
  1229. void intel_pmu_disable_bts(void);
  1230. int intel_pmu_drain_bts_buffer(void);
  1231. u64 adl_latency_data_small(struct perf_event *event, u64 status);
  1232. extern struct event_constraint intel_core2_pebs_event_constraints[];
  1233. extern struct event_constraint intel_atom_pebs_event_constraints[];
  1234. extern struct event_constraint intel_slm_pebs_event_constraints[];
  1235. extern struct event_constraint intel_glm_pebs_event_constraints[];
  1236. extern struct event_constraint intel_glp_pebs_event_constraints[];
  1237. extern struct event_constraint intel_grt_pebs_event_constraints[];
  1238. extern struct event_constraint intel_nehalem_pebs_event_constraints[];
  1239. extern struct event_constraint intel_westmere_pebs_event_constraints[];
  1240. extern struct event_constraint intel_snb_pebs_event_constraints[];
  1241. extern struct event_constraint intel_ivb_pebs_event_constraints[];
  1242. extern struct event_constraint intel_hsw_pebs_event_constraints[];
  1243. extern struct event_constraint intel_bdw_pebs_event_constraints[];
  1244. extern struct event_constraint intel_skl_pebs_event_constraints[];
  1245. extern struct event_constraint intel_icl_pebs_event_constraints[];
  1246. extern struct event_constraint intel_spr_pebs_event_constraints[];
  1247. struct event_constraint *intel_pebs_constraints(struct perf_event *event);
  1248. void intel_pmu_pebs_add(struct perf_event *event);
  1249. void intel_pmu_pebs_del(struct perf_event *event);
  1250. void intel_pmu_pebs_enable(struct perf_event *event);
  1251. void intel_pmu_pebs_disable(struct perf_event *event);
  1252. void intel_pmu_pebs_enable_all(void);
  1253. void intel_pmu_pebs_disable_all(void);
  1254. void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in);
  1255. void intel_pmu_auto_reload_read(struct perf_event *event);
  1256. void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr);
  1257. void intel_ds_init(void);
  1258. void intel_pmu_lbr_swap_task_ctx(struct perf_event_context *prev,
  1259. struct perf_event_context *next);
  1260. void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
  1261. u64 lbr_from_signext_quirk_wr(u64 val);
  1262. void intel_pmu_lbr_reset(void);
  1263. void intel_pmu_lbr_reset_32(void);
  1264. void intel_pmu_lbr_reset_64(void);
  1265. void intel_pmu_lbr_add(struct perf_event *event);
  1266. void intel_pmu_lbr_del(struct perf_event *event);
  1267. void intel_pmu_lbr_enable_all(bool pmi);
  1268. void intel_pmu_lbr_disable_all(void);
  1269. void intel_pmu_lbr_read(void);
  1270. void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc);
  1271. void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc);
  1272. void intel_pmu_lbr_save(void *ctx);
  1273. void intel_pmu_lbr_restore(void *ctx);
  1274. void intel_pmu_lbr_init_core(void);
  1275. void intel_pmu_lbr_init_nhm(void);
  1276. void intel_pmu_lbr_init_atom(void);
  1277. void intel_pmu_lbr_init_slm(void);
  1278. void intel_pmu_lbr_init_snb(void);
  1279. void intel_pmu_lbr_init_hsw(void);
  1280. void intel_pmu_lbr_init_skl(void);
  1281. void intel_pmu_lbr_init_knl(void);
  1282. void intel_pmu_lbr_init(void);
  1283. void intel_pmu_arch_lbr_init(void);
  1284. void intel_pmu_pebs_data_source_nhm(void);
  1285. void intel_pmu_pebs_data_source_skl(bool pmem);
  1286. void intel_pmu_pebs_data_source_adl(void);
  1287. void intel_pmu_pebs_data_source_grt(void);
  1288. int intel_pmu_setup_lbr_filter(struct perf_event *event);
  1289. void intel_pt_interrupt(void);
  1290. int intel_bts_interrupt(void);
  1291. void intel_bts_enable_local(void);
  1292. void intel_bts_disable_local(void);
  1293. int p4_pmu_init(void);
  1294. int p6_pmu_init(void);
  1295. int knc_pmu_init(void);
  1296. static inline int is_ht_workaround_enabled(void)
  1297. {
  1298. return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
  1299. }
  1300. #else /* CONFIG_CPU_SUP_INTEL */
  1301. static inline void reserve_ds_buffers(void)
  1302. {
  1303. }
  1304. static inline void release_ds_buffers(void)
  1305. {
  1306. }
  1307. static inline void release_lbr_buffers(void)
  1308. {
  1309. }
  1310. static inline void reserve_lbr_buffers(void)
  1311. {
  1312. }
  1313. static inline int intel_pmu_init(void)
  1314. {
  1315. return 0;
  1316. }
  1317. static inline int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)
  1318. {
  1319. return 0;
  1320. }
  1321. static inline void intel_cpuc_finish(struct cpu_hw_events *cpuc)
  1322. {
  1323. }
  1324. static inline int is_ht_workaround_enabled(void)
  1325. {
  1326. return 0;
  1327. }
  1328. #endif /* CONFIG_CPU_SUP_INTEL */
  1329. #if ((defined CONFIG_CPU_SUP_CENTAUR) || (defined CONFIG_CPU_SUP_ZHAOXIN))
  1330. int zhaoxin_pmu_init(void);
  1331. #else
  1332. static inline int zhaoxin_pmu_init(void)
  1333. {
  1334. return 0;
  1335. }
  1336. #endif /*CONFIG_CPU_SUP_CENTAUR or CONFIG_CPU_SUP_ZHAOXIN*/