perf_event.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /*
  2. * Performance events x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/capability.h>
  16. #include <linux/notifier.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/module.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/highmem.h>
  25. #include <linux/cpu.h>
  26. #include <linux/bitops.h>
  27. #include <asm/apic.h>
  28. #include <asm/stacktrace.h>
  29. #include <asm/nmi.h>
  30. #include <asm/compat.h>
  31. #include <asm/smp.h>
  32. #include <asm/alternative.h>
  33. #if 0
  34. #undef wrmsrl
  35. #define wrmsrl(msr, val) \
  36. do { \
  37. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  38. (unsigned long)(val)); \
  39. native_write_msr((msr), (u32)((u64)(val)), \
  40. (u32)((u64)(val) >> 32)); \
  41. } while (0)
  42. #endif
  43. /*
  44. * | NHM/WSM | SNB |
  45. * register -------------------------------
  46. * | HT | no HT | HT | no HT |
  47. *-----------------------------------------
  48. * offcore | core | core | cpu | core |
  49. * lbr_sel | core | core | cpu | core |
  50. * ld_lat | cpu | core | cpu | core |
  51. *-----------------------------------------
  52. *
  53. * Given that there is a small number of shared regs,
  54. * we can pre-allocate their slot in the per-cpu
  55. * per-core reg tables.
  56. */
  57. enum extra_reg_type {
  58. EXTRA_REG_NONE = -1, /* not used */
  59. EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
  60. EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
  61. EXTRA_REG_MAX /* number of entries needed */
  62. };
  63. /*
  64. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  65. */
  66. static unsigned long
  67. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  68. {
  69. unsigned long offset, addr = (unsigned long)from;
  70. unsigned long size, len = 0;
  71. struct page *page;
  72. void *map;
  73. int ret;
  74. do {
  75. ret = __get_user_pages_fast(addr, 1, 0, &page);
  76. if (!ret)
  77. break;
  78. offset = addr & (PAGE_SIZE - 1);
  79. size = min(PAGE_SIZE - offset, n - len);
  80. map = kmap_atomic(page);
  81. memcpy(to, map+offset, size);
  82. kunmap_atomic(map);
  83. put_page(page);
  84. len += size;
  85. to += size;
  86. addr += size;
  87. } while (len < n);
  88. return len;
  89. }
  90. struct event_constraint {
  91. union {
  92. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  93. u64 idxmsk64;
  94. };
  95. u64 code;
  96. u64 cmask;
  97. int weight;
  98. };
  99. struct amd_nb {
  100. int nb_id; /* NorthBridge id */
  101. int refcnt; /* reference count */
  102. struct perf_event *owners[X86_PMC_IDX_MAX];
  103. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  104. };
  105. struct intel_percore;
  106. #define MAX_LBR_ENTRIES 16
  107. struct cpu_hw_events {
  108. /*
  109. * Generic x86 PMC bits
  110. */
  111. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  112. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  113. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  114. int enabled;
  115. int n_events;
  116. int n_added;
  117. int n_txn;
  118. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  119. u64 tags[X86_PMC_IDX_MAX];
  120. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  121. unsigned int group_flag;
  122. /*
  123. * Intel DebugStore bits
  124. */
  125. struct debug_store *ds;
  126. u64 pebs_enabled;
  127. /*
  128. * Intel LBR bits
  129. */
  130. int lbr_users;
  131. void *lbr_context;
  132. struct perf_branch_stack lbr_stack;
  133. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  134. /*
  135. * manage shared (per-core, per-cpu) registers
  136. * used on Intel NHM/WSM/SNB
  137. */
  138. struct intel_shared_regs *shared_regs;
  139. /*
  140. * AMD specific bits
  141. */
  142. struct amd_nb *amd_nb;
  143. };
  144. #define __EVENT_CONSTRAINT(c, n, m, w) {\
  145. { .idxmsk64 = (n) }, \
  146. .code = (c), \
  147. .cmask = (m), \
  148. .weight = (w), \
  149. }
  150. #define EVENT_CONSTRAINT(c, n, m) \
  151. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
  152. /*
  153. * Constraint on the Event code.
  154. */
  155. #define INTEL_EVENT_CONSTRAINT(c, n) \
  156. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  157. /*
  158. * Constraint on the Event code + UMask + fixed-mask
  159. *
  160. * filter mask to validate fixed counter events.
  161. * the following filters disqualify for fixed counters:
  162. * - inv
  163. * - edge
  164. * - cnt-mask
  165. * The other filters are supported by fixed counters.
  166. * The any-thread option is supported starting with v3.
  167. */
  168. #define FIXED_EVENT_CONSTRAINT(c, n) \
  169. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  170. /*
  171. * Constraint on the Event code + UMask
  172. */
  173. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  174. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  175. #define EVENT_CONSTRAINT_END \
  176. EVENT_CONSTRAINT(0, 0, 0)
  177. #define for_each_event_constraint(e, c) \
  178. for ((e) = (c); (e)->weight; (e)++)
  179. /*
  180. * Per register state.
  181. */
  182. struct er_account {
  183. raw_spinlock_t lock; /* per-core: protect structure */
  184. u64 config; /* extra MSR config */
  185. u64 reg; /* extra MSR number */
  186. atomic_t ref; /* reference count */
  187. };
  188. /*
  189. * Extra registers for specific events.
  190. *
  191. * Some events need large masks and require external MSRs.
  192. * Those extra MSRs end up being shared for all events on
  193. * a PMU and sometimes between PMU of sibling HT threads.
  194. * In either case, the kernel needs to handle conflicting
  195. * accesses to those extra, shared, regs. The data structure
  196. * to manage those registers is stored in cpu_hw_event.
  197. */
  198. struct extra_reg {
  199. unsigned int event;
  200. unsigned int msr;
  201. u64 config_mask;
  202. u64 valid_mask;
  203. int idx; /* per_xxx->regs[] reg index */
  204. };
  205. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  206. .event = (e), \
  207. .msr = (ms), \
  208. .config_mask = (m), \
  209. .valid_mask = (vm), \
  210. .idx = EXTRA_REG_##i \
  211. }
  212. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  213. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  214. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  215. union perf_capabilities {
  216. struct {
  217. u64 lbr_format : 6;
  218. u64 pebs_trap : 1;
  219. u64 pebs_arch_reg : 1;
  220. u64 pebs_format : 4;
  221. u64 smm_freeze : 1;
  222. };
  223. u64 capabilities;
  224. };
  225. /*
  226. * struct x86_pmu - generic x86 pmu
  227. */
  228. struct x86_pmu {
  229. /*
  230. * Generic x86 PMC bits
  231. */
  232. const char *name;
  233. int version;
  234. int (*handle_irq)(struct pt_regs *);
  235. void (*disable_all)(void);
  236. void (*enable_all)(int added);
  237. void (*enable)(struct perf_event *);
  238. void (*disable)(struct perf_event *);
  239. void (*hw_watchdog_set_attr)(struct perf_event_attr *attr);
  240. int (*hw_config)(struct perf_event *event);
  241. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  242. unsigned eventsel;
  243. unsigned perfctr;
  244. u64 (*event_map)(int);
  245. int max_events;
  246. int num_counters;
  247. int num_counters_fixed;
  248. int cntval_bits;
  249. u64 cntval_mask;
  250. int apic;
  251. u64 max_period;
  252. struct event_constraint *
  253. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  254. struct perf_event *event);
  255. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  256. struct perf_event *event);
  257. struct event_constraint *event_constraints;
  258. void (*quirks)(void);
  259. int perfctr_second_write;
  260. int (*cpu_prepare)(int cpu);
  261. void (*cpu_starting)(int cpu);
  262. void (*cpu_dying)(int cpu);
  263. void (*cpu_dead)(int cpu);
  264. /*
  265. * Intel Arch Perfmon v2+
  266. */
  267. u64 intel_ctrl;
  268. union perf_capabilities intel_cap;
  269. /*
  270. * Intel DebugStore bits
  271. */
  272. int bts, pebs;
  273. int bts_active, pebs_active;
  274. int pebs_record_size;
  275. void (*drain_pebs)(struct pt_regs *regs);
  276. struct event_constraint *pebs_constraints;
  277. /*
  278. * Intel LBR
  279. */
  280. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  281. int lbr_nr; /* hardware stack size */
  282. /*
  283. * Extra registers for events
  284. */
  285. struct extra_reg *extra_regs;
  286. };
  287. static struct x86_pmu x86_pmu __read_mostly;
  288. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  289. .enabled = 1,
  290. };
  291. static int x86_perf_event_set_period(struct perf_event *event);
  292. /*
  293. * Generalized hw caching related hw_event table, filled
  294. * in on a per model basis. A value of 0 means
  295. * 'not supported', -1 means 'hw_event makes no sense on
  296. * this CPU', any other value means the raw hw_event
  297. * ID.
  298. */
  299. #define C(x) PERF_COUNT_HW_CACHE_##x
  300. static u64 __read_mostly hw_cache_event_ids
  301. [PERF_COUNT_HW_CACHE_MAX]
  302. [PERF_COUNT_HW_CACHE_OP_MAX]
  303. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  304. static u64 __read_mostly hw_cache_extra_regs
  305. [PERF_COUNT_HW_CACHE_MAX]
  306. [PERF_COUNT_HW_CACHE_OP_MAX]
  307. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  308. void hw_nmi_watchdog_set_attr(struct perf_event_attr *wd_attr)
  309. {
  310. if (x86_pmu.hw_watchdog_set_attr)
  311. x86_pmu.hw_watchdog_set_attr(wd_attr);
  312. }
  313. /*
  314. * Propagate event elapsed time into the generic event.
  315. * Can only be executed on the CPU where the event is active.
  316. * Returns the delta events processed.
  317. */
  318. static u64
  319. x86_perf_event_update(struct perf_event *event)
  320. {
  321. struct hw_perf_event *hwc = &event->hw;
  322. int shift = 64 - x86_pmu.cntval_bits;
  323. u64 prev_raw_count, new_raw_count;
  324. int idx = hwc->idx;
  325. s64 delta;
  326. if (idx == X86_PMC_IDX_FIXED_BTS)
  327. return 0;
  328. /*
  329. * Careful: an NMI might modify the previous event value.
  330. *
  331. * Our tactic to handle this is to first atomically read and
  332. * exchange a new raw count - then add that new-prev delta
  333. * count to the generic event atomically:
  334. */
  335. again:
  336. prev_raw_count = local64_read(&hwc->prev_count);
  337. rdmsrl(hwc->event_base, new_raw_count);
  338. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  339. new_raw_count) != prev_raw_count)
  340. goto again;
  341. /*
  342. * Now we have the new raw value and have updated the prev
  343. * timestamp already. We can now calculate the elapsed delta
  344. * (event-)time and add that to the generic event.
  345. *
  346. * Careful, not all hw sign-extends above the physical width
  347. * of the count.
  348. */
  349. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  350. delta >>= shift;
  351. local64_add(delta, &event->count);
  352. local64_sub(delta, &hwc->period_left);
  353. return new_raw_count;
  354. }
  355. static inline int x86_pmu_addr_offset(int index)
  356. {
  357. int offset;
  358. /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
  359. alternative_io(ASM_NOP2,
  360. "shll $1, %%eax",
  361. X86_FEATURE_PERFCTR_CORE,
  362. "=a" (offset),
  363. "a" (index));
  364. return offset;
  365. }
  366. static inline unsigned int x86_pmu_config_addr(int index)
  367. {
  368. return x86_pmu.eventsel + x86_pmu_addr_offset(index);
  369. }
  370. static inline unsigned int x86_pmu_event_addr(int index)
  371. {
  372. return x86_pmu.perfctr + x86_pmu_addr_offset(index);
  373. }
  374. /*
  375. * Find and validate any extra registers to set up.
  376. */
  377. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  378. {
  379. struct hw_perf_event_extra *reg;
  380. struct extra_reg *er;
  381. reg = &event->hw.extra_reg;
  382. if (!x86_pmu.extra_regs)
  383. return 0;
  384. for (er = x86_pmu.extra_regs; er->msr; er++) {
  385. if (er->event != (config & er->config_mask))
  386. continue;
  387. if (event->attr.config1 & ~er->valid_mask)
  388. return -EINVAL;
  389. reg->idx = er->idx;
  390. reg->config = event->attr.config1;
  391. reg->reg = er->msr;
  392. break;
  393. }
  394. return 0;
  395. }
  396. static atomic_t active_events;
  397. static DEFINE_MUTEX(pmc_reserve_mutex);
  398. #ifdef CONFIG_X86_LOCAL_APIC
  399. static bool reserve_pmc_hardware(void)
  400. {
  401. int i;
  402. for (i = 0; i < x86_pmu.num_counters; i++) {
  403. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  404. goto perfctr_fail;
  405. }
  406. for (i = 0; i < x86_pmu.num_counters; i++) {
  407. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  408. goto eventsel_fail;
  409. }
  410. return true;
  411. eventsel_fail:
  412. for (i--; i >= 0; i--)
  413. release_evntsel_nmi(x86_pmu_config_addr(i));
  414. i = x86_pmu.num_counters;
  415. perfctr_fail:
  416. for (i--; i >= 0; i--)
  417. release_perfctr_nmi(x86_pmu_event_addr(i));
  418. return false;
  419. }
  420. static void release_pmc_hardware(void)
  421. {
  422. int i;
  423. for (i = 0; i < x86_pmu.num_counters; i++) {
  424. release_perfctr_nmi(x86_pmu_event_addr(i));
  425. release_evntsel_nmi(x86_pmu_config_addr(i));
  426. }
  427. }
  428. #else
  429. static bool reserve_pmc_hardware(void) { return true; }
  430. static void release_pmc_hardware(void) {}
  431. #endif
  432. static bool check_hw_exists(void)
  433. {
  434. u64 val, val_new = 0;
  435. int i, reg, ret = 0;
  436. /*
  437. * Check to see if the BIOS enabled any of the counters, if so
  438. * complain and bail.
  439. */
  440. for (i = 0; i < x86_pmu.num_counters; i++) {
  441. reg = x86_pmu_config_addr(i);
  442. ret = rdmsrl_safe(reg, &val);
  443. if (ret)
  444. goto msr_fail;
  445. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  446. goto bios_fail;
  447. }
  448. if (x86_pmu.num_counters_fixed) {
  449. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  450. ret = rdmsrl_safe(reg, &val);
  451. if (ret)
  452. goto msr_fail;
  453. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  454. if (val & (0x03 << i*4))
  455. goto bios_fail;
  456. }
  457. }
  458. /*
  459. * Now write a value and read it back to see if it matches,
  460. * this is needed to detect certain hardware emulators (qemu/kvm)
  461. * that don't trap on the MSR access and always return 0s.
  462. */
  463. val = 0xabcdUL;
  464. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  465. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  466. if (ret || val != val_new)
  467. goto msr_fail;
  468. return true;
  469. bios_fail:
  470. /*
  471. * We still allow the PMU driver to operate:
  472. */
  473. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  474. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  475. return true;
  476. msr_fail:
  477. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  478. return false;
  479. }
  480. static void reserve_ds_buffers(void);
  481. static void release_ds_buffers(void);
  482. static void hw_perf_event_destroy(struct perf_event *event)
  483. {
  484. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  485. release_pmc_hardware();
  486. release_ds_buffers();
  487. mutex_unlock(&pmc_reserve_mutex);
  488. }
  489. }
  490. static inline int x86_pmu_initialized(void)
  491. {
  492. return x86_pmu.handle_irq != NULL;
  493. }
  494. static inline int
  495. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  496. {
  497. struct perf_event_attr *attr = &event->attr;
  498. unsigned int cache_type, cache_op, cache_result;
  499. u64 config, val;
  500. config = attr->config;
  501. cache_type = (config >> 0) & 0xff;
  502. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  503. return -EINVAL;
  504. cache_op = (config >> 8) & 0xff;
  505. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  506. return -EINVAL;
  507. cache_result = (config >> 16) & 0xff;
  508. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  509. return -EINVAL;
  510. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  511. if (val == 0)
  512. return -ENOENT;
  513. if (val == -1)
  514. return -EINVAL;
  515. hwc->config |= val;
  516. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  517. return x86_pmu_extra_regs(val, event);
  518. }
  519. static int x86_setup_perfctr(struct perf_event *event)
  520. {
  521. struct perf_event_attr *attr = &event->attr;
  522. struct hw_perf_event *hwc = &event->hw;
  523. u64 config;
  524. if (!is_sampling_event(event)) {
  525. hwc->sample_period = x86_pmu.max_period;
  526. hwc->last_period = hwc->sample_period;
  527. local64_set(&hwc->period_left, hwc->sample_period);
  528. } else {
  529. /*
  530. * If we have a PMU initialized but no APIC
  531. * interrupts, we cannot sample hardware
  532. * events (user-space has to fall back and
  533. * sample via a hrtimer based software event):
  534. */
  535. if (!x86_pmu.apic)
  536. return -EOPNOTSUPP;
  537. }
  538. /*
  539. * Do not allow config1 (extended registers) to propagate,
  540. * there's no sane user-space generalization yet:
  541. */
  542. if (attr->type == PERF_TYPE_RAW)
  543. return 0;
  544. if (attr->type == PERF_TYPE_HW_CACHE)
  545. return set_ext_hw_attr(hwc, event);
  546. if (attr->config >= x86_pmu.max_events)
  547. return -EINVAL;
  548. /*
  549. * The generic map:
  550. */
  551. config = x86_pmu.event_map(attr->config);
  552. if (config == 0)
  553. return -ENOENT;
  554. if (config == -1LL)
  555. return -EINVAL;
  556. /*
  557. * Branch tracing:
  558. */
  559. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  560. !attr->freq && hwc->sample_period == 1) {
  561. /* BTS is not supported by this architecture. */
  562. if (!x86_pmu.bts_active)
  563. return -EOPNOTSUPP;
  564. /* BTS is currently only allowed for user-mode. */
  565. if (!attr->exclude_kernel)
  566. return -EOPNOTSUPP;
  567. }
  568. hwc->config |= config;
  569. return 0;
  570. }
  571. static int x86_pmu_hw_config(struct perf_event *event)
  572. {
  573. if (event->attr.precise_ip) {
  574. int precise = 0;
  575. /* Support for constant skid */
  576. if (x86_pmu.pebs_active) {
  577. precise++;
  578. /* Support for IP fixup */
  579. if (x86_pmu.lbr_nr)
  580. precise++;
  581. }
  582. if (event->attr.precise_ip > precise)
  583. return -EOPNOTSUPP;
  584. }
  585. /*
  586. * Generate PMC IRQs:
  587. * (keep 'enabled' bit clear for now)
  588. */
  589. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  590. /*
  591. * Count user and OS events unless requested not to
  592. */
  593. if (!event->attr.exclude_user)
  594. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  595. if (!event->attr.exclude_kernel)
  596. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  597. if (event->attr.type == PERF_TYPE_RAW)
  598. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  599. return x86_setup_perfctr(event);
  600. }
  601. /*
  602. * Setup the hardware configuration for a given attr_type
  603. */
  604. static int __x86_pmu_event_init(struct perf_event *event)
  605. {
  606. int err;
  607. if (!x86_pmu_initialized())
  608. return -ENODEV;
  609. err = 0;
  610. if (!atomic_inc_not_zero(&active_events)) {
  611. mutex_lock(&pmc_reserve_mutex);
  612. if (atomic_read(&active_events) == 0) {
  613. if (!reserve_pmc_hardware())
  614. err = -EBUSY;
  615. else
  616. reserve_ds_buffers();
  617. }
  618. if (!err)
  619. atomic_inc(&active_events);
  620. mutex_unlock(&pmc_reserve_mutex);
  621. }
  622. if (err)
  623. return err;
  624. event->destroy = hw_perf_event_destroy;
  625. event->hw.idx = -1;
  626. event->hw.last_cpu = -1;
  627. event->hw.last_tag = ~0ULL;
  628. /* mark unused */
  629. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  630. return x86_pmu.hw_config(event);
  631. }
  632. static void x86_pmu_disable_all(void)
  633. {
  634. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  635. int idx;
  636. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  637. u64 val;
  638. if (!test_bit(idx, cpuc->active_mask))
  639. continue;
  640. rdmsrl(x86_pmu_config_addr(idx), val);
  641. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  642. continue;
  643. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  644. wrmsrl(x86_pmu_config_addr(idx), val);
  645. }
  646. }
  647. static void x86_pmu_disable(struct pmu *pmu)
  648. {
  649. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  650. if (!x86_pmu_initialized())
  651. return;
  652. if (!cpuc->enabled)
  653. return;
  654. cpuc->n_added = 0;
  655. cpuc->enabled = 0;
  656. barrier();
  657. x86_pmu.disable_all();
  658. }
  659. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  660. u64 enable_mask)
  661. {
  662. if (hwc->extra_reg.reg)
  663. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  664. wrmsrl(hwc->config_base, hwc->config | enable_mask);
  665. }
  666. static void x86_pmu_enable_all(int added)
  667. {
  668. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  669. int idx;
  670. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  671. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  672. if (!test_bit(idx, cpuc->active_mask))
  673. continue;
  674. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  675. }
  676. }
  677. static struct pmu pmu;
  678. static inline int is_x86_event(struct perf_event *event)
  679. {
  680. return event->pmu == &pmu;
  681. }
  682. static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  683. {
  684. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  685. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  686. int i, j, w, wmax, num = 0;
  687. struct hw_perf_event *hwc;
  688. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  689. for (i = 0; i < n; i++) {
  690. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  691. constraints[i] = c;
  692. }
  693. /*
  694. * fastpath, try to reuse previous register
  695. */
  696. for (i = 0; i < n; i++) {
  697. hwc = &cpuc->event_list[i]->hw;
  698. c = constraints[i];
  699. /* never assigned */
  700. if (hwc->idx == -1)
  701. break;
  702. /* constraint still honored */
  703. if (!test_bit(hwc->idx, c->idxmsk))
  704. break;
  705. /* not already used */
  706. if (test_bit(hwc->idx, used_mask))
  707. break;
  708. __set_bit(hwc->idx, used_mask);
  709. if (assign)
  710. assign[i] = hwc->idx;
  711. }
  712. if (i == n)
  713. goto done;
  714. /*
  715. * begin slow path
  716. */
  717. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  718. /*
  719. * weight = number of possible counters
  720. *
  721. * 1 = most constrained, only works on one counter
  722. * wmax = least constrained, works on any counter
  723. *
  724. * assign events to counters starting with most
  725. * constrained events.
  726. */
  727. wmax = x86_pmu.num_counters;
  728. /*
  729. * when fixed event counters are present,
  730. * wmax is incremented by 1 to account
  731. * for one more choice
  732. */
  733. if (x86_pmu.num_counters_fixed)
  734. wmax++;
  735. for (w = 1, num = n; num && w <= wmax; w++) {
  736. /* for each event */
  737. for (i = 0; num && i < n; i++) {
  738. c = constraints[i];
  739. hwc = &cpuc->event_list[i]->hw;
  740. if (c->weight != w)
  741. continue;
  742. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  743. if (!test_bit(j, used_mask))
  744. break;
  745. }
  746. if (j == X86_PMC_IDX_MAX)
  747. break;
  748. __set_bit(j, used_mask);
  749. if (assign)
  750. assign[i] = j;
  751. num--;
  752. }
  753. }
  754. done:
  755. /*
  756. * scheduling failed or is just a simulation,
  757. * free resources if necessary
  758. */
  759. if (!assign || num) {
  760. for (i = 0; i < n; i++) {
  761. if (x86_pmu.put_event_constraints)
  762. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  763. }
  764. }
  765. return num ? -ENOSPC : 0;
  766. }
  767. /*
  768. * dogrp: true if must collect siblings events (group)
  769. * returns total number of events and error code
  770. */
  771. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  772. {
  773. struct perf_event *event;
  774. int n, max_count;
  775. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  776. /* current number of events already accepted */
  777. n = cpuc->n_events;
  778. if (is_x86_event(leader)) {
  779. if (n >= max_count)
  780. return -ENOSPC;
  781. cpuc->event_list[n] = leader;
  782. n++;
  783. }
  784. if (!dogrp)
  785. return n;
  786. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  787. if (!is_x86_event(event) ||
  788. event->state <= PERF_EVENT_STATE_OFF)
  789. continue;
  790. if (n >= max_count)
  791. return -ENOSPC;
  792. cpuc->event_list[n] = event;
  793. n++;
  794. }
  795. return n;
  796. }
  797. static inline void x86_assign_hw_event(struct perf_event *event,
  798. struct cpu_hw_events *cpuc, int i)
  799. {
  800. struct hw_perf_event *hwc = &event->hw;
  801. hwc->idx = cpuc->assign[i];
  802. hwc->last_cpu = smp_processor_id();
  803. hwc->last_tag = ++cpuc->tags[i];
  804. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  805. hwc->config_base = 0;
  806. hwc->event_base = 0;
  807. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  808. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  809. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  810. } else {
  811. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  812. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  813. }
  814. }
  815. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  816. struct cpu_hw_events *cpuc,
  817. int i)
  818. {
  819. return hwc->idx == cpuc->assign[i] &&
  820. hwc->last_cpu == smp_processor_id() &&
  821. hwc->last_tag == cpuc->tags[i];
  822. }
  823. static void x86_pmu_start(struct perf_event *event, int flags);
  824. static void x86_pmu_stop(struct perf_event *event, int flags);
  825. static void x86_pmu_enable(struct pmu *pmu)
  826. {
  827. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  828. struct perf_event *event;
  829. struct hw_perf_event *hwc;
  830. int i, added = cpuc->n_added;
  831. if (!x86_pmu_initialized())
  832. return;
  833. if (cpuc->enabled)
  834. return;
  835. if (cpuc->n_added) {
  836. int n_running = cpuc->n_events - cpuc->n_added;
  837. /*
  838. * apply assignment obtained either from
  839. * hw_perf_group_sched_in() or x86_pmu_enable()
  840. *
  841. * step1: save events moving to new counters
  842. * step2: reprogram moved events into new counters
  843. */
  844. for (i = 0; i < n_running; i++) {
  845. event = cpuc->event_list[i];
  846. hwc = &event->hw;
  847. /*
  848. * we can avoid reprogramming counter if:
  849. * - assigned same counter as last time
  850. * - running on same CPU as last time
  851. * - no other event has used the counter since
  852. */
  853. if (hwc->idx == -1 ||
  854. match_prev_assignment(hwc, cpuc, i))
  855. continue;
  856. /*
  857. * Ensure we don't accidentally enable a stopped
  858. * counter simply because we rescheduled.
  859. */
  860. if (hwc->state & PERF_HES_STOPPED)
  861. hwc->state |= PERF_HES_ARCH;
  862. x86_pmu_stop(event, PERF_EF_UPDATE);
  863. }
  864. for (i = 0; i < cpuc->n_events; i++) {
  865. event = cpuc->event_list[i];
  866. hwc = &event->hw;
  867. if (!match_prev_assignment(hwc, cpuc, i))
  868. x86_assign_hw_event(event, cpuc, i);
  869. else if (i < n_running)
  870. continue;
  871. if (hwc->state & PERF_HES_ARCH)
  872. continue;
  873. x86_pmu_start(event, PERF_EF_RELOAD);
  874. }
  875. cpuc->n_added = 0;
  876. perf_events_lapic_init();
  877. }
  878. cpuc->enabled = 1;
  879. barrier();
  880. x86_pmu.enable_all(added);
  881. }
  882. static inline void x86_pmu_disable_event(struct perf_event *event)
  883. {
  884. struct hw_perf_event *hwc = &event->hw;
  885. wrmsrl(hwc->config_base, hwc->config);
  886. }
  887. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  888. /*
  889. * Set the next IRQ period, based on the hwc->period_left value.
  890. * To be called with the event disabled in hw:
  891. */
  892. static int
  893. x86_perf_event_set_period(struct perf_event *event)
  894. {
  895. struct hw_perf_event *hwc = &event->hw;
  896. s64 left = local64_read(&hwc->period_left);
  897. s64 period = hwc->sample_period;
  898. int ret = 0, idx = hwc->idx;
  899. if (idx == X86_PMC_IDX_FIXED_BTS)
  900. return 0;
  901. /*
  902. * If we are way outside a reasonable range then just skip forward:
  903. */
  904. if (unlikely(left <= -period)) {
  905. left = period;
  906. local64_set(&hwc->period_left, left);
  907. hwc->last_period = period;
  908. ret = 1;
  909. }
  910. if (unlikely(left <= 0)) {
  911. left += period;
  912. local64_set(&hwc->period_left, left);
  913. hwc->last_period = period;
  914. ret = 1;
  915. }
  916. /*
  917. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  918. */
  919. if (unlikely(left < 2))
  920. left = 2;
  921. if (left > x86_pmu.max_period)
  922. left = x86_pmu.max_period;
  923. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  924. /*
  925. * The hw event starts counting from this event offset,
  926. * mark it to be able to extra future deltas:
  927. */
  928. local64_set(&hwc->prev_count, (u64)-left);
  929. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  930. /*
  931. * Due to erratum on certan cpu we need
  932. * a second write to be sure the register
  933. * is updated properly
  934. */
  935. if (x86_pmu.perfctr_second_write) {
  936. wrmsrl(hwc->event_base,
  937. (u64)(-left) & x86_pmu.cntval_mask);
  938. }
  939. perf_event_update_userpage(event);
  940. return ret;
  941. }
  942. static void x86_pmu_enable_event(struct perf_event *event)
  943. {
  944. if (__this_cpu_read(cpu_hw_events.enabled))
  945. __x86_pmu_enable_event(&event->hw,
  946. ARCH_PERFMON_EVENTSEL_ENABLE);
  947. }
  948. /*
  949. * Add a single event to the PMU.
  950. *
  951. * The event is added to the group of enabled events
  952. * but only if it can be scehduled with existing events.
  953. */
  954. static int x86_pmu_add(struct perf_event *event, int flags)
  955. {
  956. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  957. struct hw_perf_event *hwc;
  958. int assign[X86_PMC_IDX_MAX];
  959. int n, n0, ret;
  960. hwc = &event->hw;
  961. perf_pmu_disable(event->pmu);
  962. n0 = cpuc->n_events;
  963. ret = n = collect_events(cpuc, event, false);
  964. if (ret < 0)
  965. goto out;
  966. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  967. if (!(flags & PERF_EF_START))
  968. hwc->state |= PERF_HES_ARCH;
  969. /*
  970. * If group events scheduling transaction was started,
  971. * skip the schedulability test here, it will be performed
  972. * at commit time (->commit_txn) as a whole
  973. */
  974. if (cpuc->group_flag & PERF_EVENT_TXN)
  975. goto done_collect;
  976. ret = x86_pmu.schedule_events(cpuc, n, assign);
  977. if (ret)
  978. goto out;
  979. /*
  980. * copy new assignment, now we know it is possible
  981. * will be used by hw_perf_enable()
  982. */
  983. memcpy(cpuc->assign, assign, n*sizeof(int));
  984. done_collect:
  985. cpuc->n_events = n;
  986. cpuc->n_added += n - n0;
  987. cpuc->n_txn += n - n0;
  988. ret = 0;
  989. out:
  990. perf_pmu_enable(event->pmu);
  991. return ret;
  992. }
  993. static void x86_pmu_start(struct perf_event *event, int flags)
  994. {
  995. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  996. int idx = event->hw.idx;
  997. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  998. return;
  999. if (WARN_ON_ONCE(idx == -1))
  1000. return;
  1001. if (flags & PERF_EF_RELOAD) {
  1002. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1003. x86_perf_event_set_period(event);
  1004. }
  1005. event->hw.state = 0;
  1006. cpuc->events[idx] = event;
  1007. __set_bit(idx, cpuc->active_mask);
  1008. __set_bit(idx, cpuc->running);
  1009. x86_pmu.enable(event);
  1010. perf_event_update_userpage(event);
  1011. }
  1012. void perf_event_print_debug(void)
  1013. {
  1014. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1015. u64 pebs;
  1016. struct cpu_hw_events *cpuc;
  1017. unsigned long flags;
  1018. int cpu, idx;
  1019. if (!x86_pmu.num_counters)
  1020. return;
  1021. local_irq_save(flags);
  1022. cpu = smp_processor_id();
  1023. cpuc = &per_cpu(cpu_hw_events, cpu);
  1024. if (x86_pmu.version >= 2) {
  1025. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1026. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1027. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1028. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1029. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  1030. pr_info("\n");
  1031. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1032. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1033. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1034. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1035. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  1036. }
  1037. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  1038. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1039. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  1040. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  1041. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  1042. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1043. cpu, idx, pmc_ctrl);
  1044. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1045. cpu, idx, pmc_count);
  1046. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1047. cpu, idx, prev_left);
  1048. }
  1049. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1050. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1051. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1052. cpu, idx, pmc_count);
  1053. }
  1054. local_irq_restore(flags);
  1055. }
  1056. static void x86_pmu_stop(struct perf_event *event, int flags)
  1057. {
  1058. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1059. struct hw_perf_event *hwc = &event->hw;
  1060. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  1061. x86_pmu.disable(event);
  1062. cpuc->events[hwc->idx] = NULL;
  1063. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  1064. hwc->state |= PERF_HES_STOPPED;
  1065. }
  1066. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  1067. /*
  1068. * Drain the remaining delta count out of a event
  1069. * that we are disabling:
  1070. */
  1071. x86_perf_event_update(event);
  1072. hwc->state |= PERF_HES_UPTODATE;
  1073. }
  1074. }
  1075. static void x86_pmu_del(struct perf_event *event, int flags)
  1076. {
  1077. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1078. int i;
  1079. /*
  1080. * If we're called during a txn, we don't need to do anything.
  1081. * The events never got scheduled and ->cancel_txn will truncate
  1082. * the event_list.
  1083. */
  1084. if (cpuc->group_flag & PERF_EVENT_TXN)
  1085. return;
  1086. x86_pmu_stop(event, PERF_EF_UPDATE);
  1087. for (i = 0; i < cpuc->n_events; i++) {
  1088. if (event == cpuc->event_list[i]) {
  1089. if (x86_pmu.put_event_constraints)
  1090. x86_pmu.put_event_constraints(cpuc, event);
  1091. while (++i < cpuc->n_events)
  1092. cpuc->event_list[i-1] = cpuc->event_list[i];
  1093. --cpuc->n_events;
  1094. break;
  1095. }
  1096. }
  1097. perf_event_update_userpage(event);
  1098. }
  1099. static int x86_pmu_handle_irq(struct pt_regs *regs)
  1100. {
  1101. struct perf_sample_data data;
  1102. struct cpu_hw_events *cpuc;
  1103. struct perf_event *event;
  1104. int idx, handled = 0;
  1105. u64 val;
  1106. perf_sample_data_init(&data, 0);
  1107. cpuc = &__get_cpu_var(cpu_hw_events);
  1108. /*
  1109. * Some chipsets need to unmask the LVTPC in a particular spot
  1110. * inside the nmi handler. As a result, the unmasking was pushed
  1111. * into all the nmi handlers.
  1112. *
  1113. * This generic handler doesn't seem to have any issues where the
  1114. * unmasking occurs so it was left at the top.
  1115. */
  1116. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1117. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1118. if (!test_bit(idx, cpuc->active_mask)) {
  1119. /*
  1120. * Though we deactivated the counter some cpus
  1121. * might still deliver spurious interrupts still
  1122. * in flight. Catch them:
  1123. */
  1124. if (__test_and_clear_bit(idx, cpuc->running))
  1125. handled++;
  1126. continue;
  1127. }
  1128. event = cpuc->events[idx];
  1129. val = x86_perf_event_update(event);
  1130. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1131. continue;
  1132. /*
  1133. * event overflow
  1134. */
  1135. handled++;
  1136. data.period = event->hw.last_period;
  1137. if (!x86_perf_event_set_period(event))
  1138. continue;
  1139. if (perf_event_overflow(event, &data, regs))
  1140. x86_pmu_stop(event, 0);
  1141. }
  1142. if (handled)
  1143. inc_irq_stat(apic_perf_irqs);
  1144. return handled;
  1145. }
  1146. void perf_events_lapic_init(void)
  1147. {
  1148. if (!x86_pmu.apic || !x86_pmu_initialized())
  1149. return;
  1150. /*
  1151. * Always use NMI for PMU
  1152. */
  1153. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1154. }
  1155. struct pmu_nmi_state {
  1156. unsigned int marked;
  1157. int handled;
  1158. };
  1159. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  1160. static int __kprobes
  1161. perf_event_nmi_handler(struct notifier_block *self,
  1162. unsigned long cmd, void *__args)
  1163. {
  1164. struct die_args *args = __args;
  1165. unsigned int this_nmi;
  1166. int handled;
  1167. if (!atomic_read(&active_events))
  1168. return NOTIFY_DONE;
  1169. switch (cmd) {
  1170. case DIE_NMI:
  1171. break;
  1172. case DIE_NMIUNKNOWN:
  1173. this_nmi = percpu_read(irq_stat.__nmi_count);
  1174. if (this_nmi != __this_cpu_read(pmu_nmi.marked))
  1175. /* let the kernel handle the unknown nmi */
  1176. return NOTIFY_DONE;
  1177. /*
  1178. * This one is a PMU back-to-back nmi. Two events
  1179. * trigger 'simultaneously' raising two back-to-back
  1180. * NMIs. If the first NMI handles both, the latter
  1181. * will be empty and daze the CPU. So, we drop it to
  1182. * avoid false-positive 'unknown nmi' messages.
  1183. */
  1184. return NOTIFY_STOP;
  1185. default:
  1186. return NOTIFY_DONE;
  1187. }
  1188. handled = x86_pmu.handle_irq(args->regs);
  1189. if (!handled)
  1190. return NOTIFY_DONE;
  1191. this_nmi = percpu_read(irq_stat.__nmi_count);
  1192. if ((handled > 1) ||
  1193. /* the next nmi could be a back-to-back nmi */
  1194. ((__this_cpu_read(pmu_nmi.marked) == this_nmi) &&
  1195. (__this_cpu_read(pmu_nmi.handled) > 1))) {
  1196. /*
  1197. * We could have two subsequent back-to-back nmis: The
  1198. * first handles more than one counter, the 2nd
  1199. * handles only one counter and the 3rd handles no
  1200. * counter.
  1201. *
  1202. * This is the 2nd nmi because the previous was
  1203. * handling more than one counter. We will mark the
  1204. * next (3rd) and then drop it if unhandled.
  1205. */
  1206. __this_cpu_write(pmu_nmi.marked, this_nmi + 1);
  1207. __this_cpu_write(pmu_nmi.handled, handled);
  1208. }
  1209. return NOTIFY_STOP;
  1210. }
  1211. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1212. .notifier_call = perf_event_nmi_handler,
  1213. .next = NULL,
  1214. .priority = NMI_LOCAL_LOW_PRIOR,
  1215. };
  1216. static struct event_constraint unconstrained;
  1217. static struct event_constraint emptyconstraint;
  1218. static struct event_constraint *
  1219. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1220. {
  1221. struct event_constraint *c;
  1222. if (x86_pmu.event_constraints) {
  1223. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1224. if ((event->hw.config & c->cmask) == c->code)
  1225. return c;
  1226. }
  1227. }
  1228. return &unconstrained;
  1229. }
  1230. #include "perf_event_amd.c"
  1231. #include "perf_event_p6.c"
  1232. #include "perf_event_p4.c"
  1233. #include "perf_event_intel_lbr.c"
  1234. #include "perf_event_intel_ds.c"
  1235. #include "perf_event_intel.c"
  1236. static int __cpuinit
  1237. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1238. {
  1239. unsigned int cpu = (long)hcpu;
  1240. int ret = NOTIFY_OK;
  1241. switch (action & ~CPU_TASKS_FROZEN) {
  1242. case CPU_UP_PREPARE:
  1243. if (x86_pmu.cpu_prepare)
  1244. ret = x86_pmu.cpu_prepare(cpu);
  1245. break;
  1246. case CPU_STARTING:
  1247. if (x86_pmu.cpu_starting)
  1248. x86_pmu.cpu_starting(cpu);
  1249. break;
  1250. case CPU_DYING:
  1251. if (x86_pmu.cpu_dying)
  1252. x86_pmu.cpu_dying(cpu);
  1253. break;
  1254. case CPU_UP_CANCELED:
  1255. case CPU_DEAD:
  1256. if (x86_pmu.cpu_dead)
  1257. x86_pmu.cpu_dead(cpu);
  1258. break;
  1259. default:
  1260. break;
  1261. }
  1262. return ret;
  1263. }
  1264. static void __init pmu_check_apic(void)
  1265. {
  1266. if (cpu_has_apic)
  1267. return;
  1268. x86_pmu.apic = 0;
  1269. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1270. pr_info("no hardware sampling interrupt available.\n");
  1271. }
  1272. static int __init init_hw_perf_events(void)
  1273. {
  1274. struct event_constraint *c;
  1275. int err;
  1276. pr_info("Performance Events: ");
  1277. switch (boot_cpu_data.x86_vendor) {
  1278. case X86_VENDOR_INTEL:
  1279. err = intel_pmu_init();
  1280. break;
  1281. case X86_VENDOR_AMD:
  1282. err = amd_pmu_init();
  1283. break;
  1284. default:
  1285. return 0;
  1286. }
  1287. if (err != 0) {
  1288. pr_cont("no PMU driver, software events only.\n");
  1289. return 0;
  1290. }
  1291. pmu_check_apic();
  1292. /* sanity check that the hardware exists or is emulated */
  1293. if (!check_hw_exists())
  1294. return 0;
  1295. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1296. if (x86_pmu.quirks)
  1297. x86_pmu.quirks();
  1298. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1299. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1300. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1301. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1302. }
  1303. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1304. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1305. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1306. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1307. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1308. }
  1309. x86_pmu.intel_ctrl |=
  1310. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1311. perf_events_lapic_init();
  1312. register_die_notifier(&perf_event_nmi_notifier);
  1313. unconstrained = (struct event_constraint)
  1314. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1315. 0, x86_pmu.num_counters);
  1316. if (x86_pmu.event_constraints) {
  1317. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1318. if (c->cmask != X86_RAW_EVENT_MASK)
  1319. continue;
  1320. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1321. c->weight += x86_pmu.num_counters;
  1322. }
  1323. }
  1324. pr_info("... version: %d\n", x86_pmu.version);
  1325. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1326. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1327. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1328. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1329. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1330. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1331. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1332. perf_cpu_notifier(x86_pmu_notifier);
  1333. return 0;
  1334. }
  1335. early_initcall(init_hw_perf_events);
  1336. static inline void x86_pmu_read(struct perf_event *event)
  1337. {
  1338. x86_perf_event_update(event);
  1339. }
  1340. /*
  1341. * Start group events scheduling transaction
  1342. * Set the flag to make pmu::enable() not perform the
  1343. * schedulability test, it will be performed at commit time
  1344. */
  1345. static void x86_pmu_start_txn(struct pmu *pmu)
  1346. {
  1347. perf_pmu_disable(pmu);
  1348. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1349. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1350. }
  1351. /*
  1352. * Stop group events scheduling transaction
  1353. * Clear the flag and pmu::enable() will perform the
  1354. * schedulability test.
  1355. */
  1356. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1357. {
  1358. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1359. /*
  1360. * Truncate the collected events.
  1361. */
  1362. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1363. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1364. perf_pmu_enable(pmu);
  1365. }
  1366. /*
  1367. * Commit group events scheduling transaction
  1368. * Perform the group schedulability test as a whole
  1369. * Return 0 if success
  1370. */
  1371. static int x86_pmu_commit_txn(struct pmu *pmu)
  1372. {
  1373. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1374. int assign[X86_PMC_IDX_MAX];
  1375. int n, ret;
  1376. n = cpuc->n_events;
  1377. if (!x86_pmu_initialized())
  1378. return -EAGAIN;
  1379. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1380. if (ret)
  1381. return ret;
  1382. /*
  1383. * copy new assignment, now we know it is possible
  1384. * will be used by hw_perf_enable()
  1385. */
  1386. memcpy(cpuc->assign, assign, n*sizeof(int));
  1387. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1388. perf_pmu_enable(pmu);
  1389. return 0;
  1390. }
  1391. /*
  1392. * a fake_cpuc is used to validate event groups. Due to
  1393. * the extra reg logic, we need to also allocate a fake
  1394. * per_core and per_cpu structure. Otherwise, group events
  1395. * using extra reg may conflict without the kernel being
  1396. * able to catch this when the last event gets added to
  1397. * the group.
  1398. */
  1399. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1400. {
  1401. kfree(cpuc->shared_regs);
  1402. kfree(cpuc);
  1403. }
  1404. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1405. {
  1406. struct cpu_hw_events *cpuc;
  1407. int cpu = raw_smp_processor_id();
  1408. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1409. if (!cpuc)
  1410. return ERR_PTR(-ENOMEM);
  1411. /* only needed, if we have extra_regs */
  1412. if (x86_pmu.extra_regs) {
  1413. cpuc->shared_regs = allocate_shared_regs(cpu);
  1414. if (!cpuc->shared_regs)
  1415. goto error;
  1416. }
  1417. return cpuc;
  1418. error:
  1419. free_fake_cpuc(cpuc);
  1420. return ERR_PTR(-ENOMEM);
  1421. }
  1422. /*
  1423. * validate that we can schedule this event
  1424. */
  1425. static int validate_event(struct perf_event *event)
  1426. {
  1427. struct cpu_hw_events *fake_cpuc;
  1428. struct event_constraint *c;
  1429. int ret = 0;
  1430. fake_cpuc = allocate_fake_cpuc();
  1431. if (IS_ERR(fake_cpuc))
  1432. return PTR_ERR(fake_cpuc);
  1433. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1434. if (!c || !c->weight)
  1435. ret = -ENOSPC;
  1436. if (x86_pmu.put_event_constraints)
  1437. x86_pmu.put_event_constraints(fake_cpuc, event);
  1438. free_fake_cpuc(fake_cpuc);
  1439. return ret;
  1440. }
  1441. /*
  1442. * validate a single event group
  1443. *
  1444. * validation include:
  1445. * - check events are compatible which each other
  1446. * - events do not compete for the same counter
  1447. * - number of events <= number of counters
  1448. *
  1449. * validation ensures the group can be loaded onto the
  1450. * PMU if it was the only group available.
  1451. */
  1452. static int validate_group(struct perf_event *event)
  1453. {
  1454. struct perf_event *leader = event->group_leader;
  1455. struct cpu_hw_events *fake_cpuc;
  1456. int ret = -ENOSPC, n;
  1457. fake_cpuc = allocate_fake_cpuc();
  1458. if (IS_ERR(fake_cpuc))
  1459. return PTR_ERR(fake_cpuc);
  1460. /*
  1461. * the event is not yet connected with its
  1462. * siblings therefore we must first collect
  1463. * existing siblings, then add the new event
  1464. * before we can simulate the scheduling
  1465. */
  1466. n = collect_events(fake_cpuc, leader, true);
  1467. if (n < 0)
  1468. goto out;
  1469. fake_cpuc->n_events = n;
  1470. n = collect_events(fake_cpuc, event, false);
  1471. if (n < 0)
  1472. goto out;
  1473. fake_cpuc->n_events = n;
  1474. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1475. out:
  1476. free_fake_cpuc(fake_cpuc);
  1477. return ret;
  1478. }
  1479. static int x86_pmu_event_init(struct perf_event *event)
  1480. {
  1481. struct pmu *tmp;
  1482. int err;
  1483. switch (event->attr.type) {
  1484. case PERF_TYPE_RAW:
  1485. case PERF_TYPE_HARDWARE:
  1486. case PERF_TYPE_HW_CACHE:
  1487. break;
  1488. default:
  1489. return -ENOENT;
  1490. }
  1491. err = __x86_pmu_event_init(event);
  1492. if (!err) {
  1493. /*
  1494. * we temporarily connect event to its pmu
  1495. * such that validate_group() can classify
  1496. * it as an x86 event using is_x86_event()
  1497. */
  1498. tmp = event->pmu;
  1499. event->pmu = &pmu;
  1500. if (event->group_leader != event)
  1501. err = validate_group(event);
  1502. else
  1503. err = validate_event(event);
  1504. event->pmu = tmp;
  1505. }
  1506. if (err) {
  1507. if (event->destroy)
  1508. event->destroy(event);
  1509. }
  1510. return err;
  1511. }
  1512. static struct pmu pmu = {
  1513. .pmu_enable = x86_pmu_enable,
  1514. .pmu_disable = x86_pmu_disable,
  1515. .event_init = x86_pmu_event_init,
  1516. .add = x86_pmu_add,
  1517. .del = x86_pmu_del,
  1518. .start = x86_pmu_start,
  1519. .stop = x86_pmu_stop,
  1520. .read = x86_pmu_read,
  1521. .start_txn = x86_pmu_start_txn,
  1522. .cancel_txn = x86_pmu_cancel_txn,
  1523. .commit_txn = x86_pmu_commit_txn,
  1524. };
  1525. /*
  1526. * callchain support
  1527. */
  1528. static int backtrace_stack(void *data, char *name)
  1529. {
  1530. return 0;
  1531. }
  1532. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1533. {
  1534. struct perf_callchain_entry *entry = data;
  1535. perf_callchain_store(entry, addr);
  1536. }
  1537. static const struct stacktrace_ops backtrace_ops = {
  1538. .stack = backtrace_stack,
  1539. .address = backtrace_address,
  1540. .walk_stack = print_context_stack_bp,
  1541. };
  1542. void
  1543. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1544. {
  1545. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1546. /* TODO: We don't support guest os callchain now */
  1547. return;
  1548. }
  1549. perf_callchain_store(entry, regs->ip);
  1550. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1551. }
  1552. #ifdef CONFIG_COMPAT
  1553. static inline int
  1554. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1555. {
  1556. /* 32-bit process in 64-bit kernel. */
  1557. struct stack_frame_ia32 frame;
  1558. const void __user *fp;
  1559. if (!test_thread_flag(TIF_IA32))
  1560. return 0;
  1561. fp = compat_ptr(regs->bp);
  1562. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1563. unsigned long bytes;
  1564. frame.next_frame = 0;
  1565. frame.return_address = 0;
  1566. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1567. if (bytes != sizeof(frame))
  1568. break;
  1569. if (fp < compat_ptr(regs->sp))
  1570. break;
  1571. perf_callchain_store(entry, frame.return_address);
  1572. fp = compat_ptr(frame.next_frame);
  1573. }
  1574. return 1;
  1575. }
  1576. #else
  1577. static inline int
  1578. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1579. {
  1580. return 0;
  1581. }
  1582. #endif
  1583. void
  1584. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1585. {
  1586. struct stack_frame frame;
  1587. const void __user *fp;
  1588. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1589. /* TODO: We don't support guest os callchain now */
  1590. return;
  1591. }
  1592. fp = (void __user *)regs->bp;
  1593. perf_callchain_store(entry, regs->ip);
  1594. if (perf_callchain_user32(regs, entry))
  1595. return;
  1596. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1597. unsigned long bytes;
  1598. frame.next_frame = NULL;
  1599. frame.return_address = 0;
  1600. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1601. if (bytes != sizeof(frame))
  1602. break;
  1603. if ((unsigned long)fp < regs->sp)
  1604. break;
  1605. perf_callchain_store(entry, frame.return_address);
  1606. fp = frame.next_frame;
  1607. }
  1608. }
  1609. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1610. {
  1611. unsigned long ip;
  1612. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1613. ip = perf_guest_cbs->get_guest_ip();
  1614. else
  1615. ip = instruction_pointer(regs);
  1616. return ip;
  1617. }
  1618. unsigned long perf_misc_flags(struct pt_regs *regs)
  1619. {
  1620. int misc = 0;
  1621. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1622. if (perf_guest_cbs->is_user_mode())
  1623. misc |= PERF_RECORD_MISC_GUEST_USER;
  1624. else
  1625. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1626. } else {
  1627. if (user_mode(regs))
  1628. misc |= PERF_RECORD_MISC_USER;
  1629. else
  1630. misc |= PERF_RECORD_MISC_KERNEL;
  1631. }
  1632. if (regs->flags & PERF_EFLAGS_EXACT)
  1633. misc |= PERF_RECORD_MISC_EXACT_IP;
  1634. return misc;
  1635. }