perf_event.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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. * validate that we can schedule this event
  1393. */
  1394. static int validate_event(struct perf_event *event)
  1395. {
  1396. struct cpu_hw_events *fake_cpuc;
  1397. struct event_constraint *c;
  1398. int ret = 0;
  1399. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1400. if (!fake_cpuc)
  1401. return -ENOMEM;
  1402. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1403. if (!c || !c->weight)
  1404. ret = -ENOSPC;
  1405. if (x86_pmu.put_event_constraints)
  1406. x86_pmu.put_event_constraints(fake_cpuc, event);
  1407. kfree(fake_cpuc);
  1408. return ret;
  1409. }
  1410. /*
  1411. * validate a single event group
  1412. *
  1413. * validation include:
  1414. * - check events are compatible which each other
  1415. * - events do not compete for the same counter
  1416. * - number of events <= number of counters
  1417. *
  1418. * validation ensures the group can be loaded onto the
  1419. * PMU if it was the only group available.
  1420. */
  1421. static int validate_group(struct perf_event *event)
  1422. {
  1423. struct perf_event *leader = event->group_leader;
  1424. struct cpu_hw_events *fake_cpuc;
  1425. int ret, n;
  1426. ret = -ENOMEM;
  1427. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1428. if (!fake_cpuc)
  1429. goto out;
  1430. /*
  1431. * the event is not yet connected with its
  1432. * siblings therefore we must first collect
  1433. * existing siblings, then add the new event
  1434. * before we can simulate the scheduling
  1435. */
  1436. ret = -ENOSPC;
  1437. n = collect_events(fake_cpuc, leader, true);
  1438. if (n < 0)
  1439. goto out_free;
  1440. fake_cpuc->n_events = n;
  1441. n = collect_events(fake_cpuc, event, false);
  1442. if (n < 0)
  1443. goto out_free;
  1444. fake_cpuc->n_events = n;
  1445. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1446. out_free:
  1447. kfree(fake_cpuc);
  1448. out:
  1449. return ret;
  1450. }
  1451. static int x86_pmu_event_init(struct perf_event *event)
  1452. {
  1453. struct pmu *tmp;
  1454. int err;
  1455. switch (event->attr.type) {
  1456. case PERF_TYPE_RAW:
  1457. case PERF_TYPE_HARDWARE:
  1458. case PERF_TYPE_HW_CACHE:
  1459. break;
  1460. default:
  1461. return -ENOENT;
  1462. }
  1463. err = __x86_pmu_event_init(event);
  1464. if (!err) {
  1465. /*
  1466. * we temporarily connect event to its pmu
  1467. * such that validate_group() can classify
  1468. * it as an x86 event using is_x86_event()
  1469. */
  1470. tmp = event->pmu;
  1471. event->pmu = &pmu;
  1472. if (event->group_leader != event)
  1473. err = validate_group(event);
  1474. else
  1475. err = validate_event(event);
  1476. event->pmu = tmp;
  1477. }
  1478. if (err) {
  1479. if (event->destroy)
  1480. event->destroy(event);
  1481. }
  1482. return err;
  1483. }
  1484. static struct pmu pmu = {
  1485. .pmu_enable = x86_pmu_enable,
  1486. .pmu_disable = x86_pmu_disable,
  1487. .event_init = x86_pmu_event_init,
  1488. .add = x86_pmu_add,
  1489. .del = x86_pmu_del,
  1490. .start = x86_pmu_start,
  1491. .stop = x86_pmu_stop,
  1492. .read = x86_pmu_read,
  1493. .start_txn = x86_pmu_start_txn,
  1494. .cancel_txn = x86_pmu_cancel_txn,
  1495. .commit_txn = x86_pmu_commit_txn,
  1496. };
  1497. /*
  1498. * callchain support
  1499. */
  1500. static int backtrace_stack(void *data, char *name)
  1501. {
  1502. return 0;
  1503. }
  1504. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1505. {
  1506. struct perf_callchain_entry *entry = data;
  1507. perf_callchain_store(entry, addr);
  1508. }
  1509. static const struct stacktrace_ops backtrace_ops = {
  1510. .stack = backtrace_stack,
  1511. .address = backtrace_address,
  1512. .walk_stack = print_context_stack_bp,
  1513. };
  1514. void
  1515. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1516. {
  1517. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1518. /* TODO: We don't support guest os callchain now */
  1519. return;
  1520. }
  1521. perf_callchain_store(entry, regs->ip);
  1522. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1523. }
  1524. #ifdef CONFIG_COMPAT
  1525. static inline int
  1526. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1527. {
  1528. /* 32-bit process in 64-bit kernel. */
  1529. struct stack_frame_ia32 frame;
  1530. const void __user *fp;
  1531. if (!test_thread_flag(TIF_IA32))
  1532. return 0;
  1533. fp = compat_ptr(regs->bp);
  1534. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1535. unsigned long bytes;
  1536. frame.next_frame = 0;
  1537. frame.return_address = 0;
  1538. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1539. if (bytes != sizeof(frame))
  1540. break;
  1541. if (fp < compat_ptr(regs->sp))
  1542. break;
  1543. perf_callchain_store(entry, frame.return_address);
  1544. fp = compat_ptr(frame.next_frame);
  1545. }
  1546. return 1;
  1547. }
  1548. #else
  1549. static inline int
  1550. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1551. {
  1552. return 0;
  1553. }
  1554. #endif
  1555. void
  1556. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1557. {
  1558. struct stack_frame frame;
  1559. const void __user *fp;
  1560. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1561. /* TODO: We don't support guest os callchain now */
  1562. return;
  1563. }
  1564. fp = (void __user *)regs->bp;
  1565. perf_callchain_store(entry, regs->ip);
  1566. if (perf_callchain_user32(regs, entry))
  1567. return;
  1568. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1569. unsigned long bytes;
  1570. frame.next_frame = NULL;
  1571. frame.return_address = 0;
  1572. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1573. if (bytes != sizeof(frame))
  1574. break;
  1575. if ((unsigned long)fp < regs->sp)
  1576. break;
  1577. perf_callchain_store(entry, frame.return_address);
  1578. fp = frame.next_frame;
  1579. }
  1580. }
  1581. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1582. {
  1583. unsigned long ip;
  1584. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1585. ip = perf_guest_cbs->get_guest_ip();
  1586. else
  1587. ip = instruction_pointer(regs);
  1588. return ip;
  1589. }
  1590. unsigned long perf_misc_flags(struct pt_regs *regs)
  1591. {
  1592. int misc = 0;
  1593. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1594. if (perf_guest_cbs->is_user_mode())
  1595. misc |= PERF_RECORD_MISC_GUEST_USER;
  1596. else
  1597. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1598. } else {
  1599. if (user_mode(regs))
  1600. misc |= PERF_RECORD_MISC_USER;
  1601. else
  1602. misc |= PERF_RECORD_MISC_KERNEL;
  1603. }
  1604. if (regs->flags & PERF_EFLAGS_EXACT)
  1605. misc |= PERF_RECORD_MISC_EXACT_IP;
  1606. return misc;
  1607. }