perf_event.c 42 KB

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