perf_event.c 37 KB

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