perf_event.c 38 KB

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