perf_event.c 37 KB

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