perf_event.c 45 KB

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