perf_event.c 39 KB

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