perf_event.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. #ifdef CONFIG_X86_LOCAL_APIC
  927. if (!x86_pmu.apic || !x86_pmu_initialized())
  928. return;
  929. /*
  930. * Always use NMI for PMU
  931. */
  932. apic_write(APIC_LVTPC, APIC_DM_NMI);
  933. #endif
  934. }
  935. static int __kprobes
  936. perf_event_nmi_handler(struct notifier_block *self,
  937. unsigned long cmd, void *__args)
  938. {
  939. struct die_args *args = __args;
  940. struct pt_regs *regs;
  941. if (!atomic_read(&active_events))
  942. return NOTIFY_DONE;
  943. switch (cmd) {
  944. case DIE_NMI:
  945. case DIE_NMI_IPI:
  946. break;
  947. default:
  948. return NOTIFY_DONE;
  949. }
  950. regs = args->regs;
  951. #ifdef CONFIG_X86_LOCAL_APIC
  952. apic_write(APIC_LVTPC, APIC_DM_NMI);
  953. #endif
  954. /*
  955. * Can't rely on the handled return value to say it was our NMI, two
  956. * events could trigger 'simultaneously' raising two back-to-back NMIs.
  957. *
  958. * If the first NMI handles both, the latter will be empty and daze
  959. * the CPU.
  960. */
  961. x86_pmu.handle_irq(regs);
  962. return NOTIFY_STOP;
  963. }
  964. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  965. .notifier_call = perf_event_nmi_handler,
  966. .next = NULL,
  967. .priority = 1
  968. };
  969. static struct event_constraint unconstrained;
  970. static struct event_constraint emptyconstraint;
  971. static struct event_constraint *
  972. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  973. {
  974. struct event_constraint *c;
  975. if (x86_pmu.event_constraints) {
  976. for_each_event_constraint(c, x86_pmu.event_constraints) {
  977. if ((event->hw.config & c->cmask) == c->code)
  978. return c;
  979. }
  980. }
  981. return &unconstrained;
  982. }
  983. static int x86_event_sched_in(struct perf_event *event,
  984. struct perf_cpu_context *cpuctx)
  985. {
  986. int ret = 0;
  987. event->state = PERF_EVENT_STATE_ACTIVE;
  988. event->oncpu = smp_processor_id();
  989. event->tstamp_running += event->ctx->time - event->tstamp_stopped;
  990. if (!is_x86_event(event))
  991. ret = event->pmu->enable(event);
  992. if (!ret && !is_software_event(event))
  993. cpuctx->active_oncpu++;
  994. if (!ret && event->attr.exclusive)
  995. cpuctx->exclusive = 1;
  996. return ret;
  997. }
  998. static void x86_event_sched_out(struct perf_event *event,
  999. struct perf_cpu_context *cpuctx)
  1000. {
  1001. event->state = PERF_EVENT_STATE_INACTIVE;
  1002. event->oncpu = -1;
  1003. if (!is_x86_event(event))
  1004. event->pmu->disable(event);
  1005. event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
  1006. if (!is_software_event(event))
  1007. cpuctx->active_oncpu--;
  1008. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1009. cpuctx->exclusive = 0;
  1010. }
  1011. /*
  1012. * Called to enable a whole group of events.
  1013. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  1014. * Assumes the caller has disabled interrupts and has
  1015. * frozen the PMU with hw_perf_save_disable.
  1016. *
  1017. * called with PMU disabled. If successful and return value 1,
  1018. * then guaranteed to call perf_enable() and hw_perf_enable()
  1019. */
  1020. int hw_perf_group_sched_in(struct perf_event *leader,
  1021. struct perf_cpu_context *cpuctx,
  1022. struct perf_event_context *ctx)
  1023. {
  1024. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1025. struct perf_event *sub;
  1026. int assign[X86_PMC_IDX_MAX];
  1027. int n0, n1, ret;
  1028. if (!x86_pmu_initialized())
  1029. return 0;
  1030. /* n0 = total number of events */
  1031. n0 = collect_events(cpuc, leader, true);
  1032. if (n0 < 0)
  1033. return n0;
  1034. ret = x86_pmu.schedule_events(cpuc, n0, assign);
  1035. if (ret)
  1036. return ret;
  1037. ret = x86_event_sched_in(leader, cpuctx);
  1038. if (ret)
  1039. return ret;
  1040. n1 = 1;
  1041. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1042. if (sub->state > PERF_EVENT_STATE_OFF) {
  1043. ret = x86_event_sched_in(sub, cpuctx);
  1044. if (ret)
  1045. goto undo;
  1046. ++n1;
  1047. }
  1048. }
  1049. /*
  1050. * copy new assignment, now we know it is possible
  1051. * will be used by hw_perf_enable()
  1052. */
  1053. memcpy(cpuc->assign, assign, n0*sizeof(int));
  1054. cpuc->n_events = n0;
  1055. cpuc->n_added += n1;
  1056. ctx->nr_active += n1;
  1057. /*
  1058. * 1 means successful and events are active
  1059. * This is not quite true because we defer
  1060. * actual activation until hw_perf_enable() but
  1061. * this way we* ensure caller won't try to enable
  1062. * individual events
  1063. */
  1064. return 1;
  1065. undo:
  1066. x86_event_sched_out(leader, cpuctx);
  1067. n0 = 1;
  1068. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1069. if (sub->state == PERF_EVENT_STATE_ACTIVE) {
  1070. x86_event_sched_out(sub, cpuctx);
  1071. if (++n0 == n1)
  1072. break;
  1073. }
  1074. }
  1075. return ret;
  1076. }
  1077. #include "perf_event_amd.c"
  1078. #include "perf_event_p6.c"
  1079. #include "perf_event_p4.c"
  1080. #include "perf_event_intel_lbr.c"
  1081. #include "perf_event_intel_ds.c"
  1082. #include "perf_event_intel.c"
  1083. static int __cpuinit
  1084. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1085. {
  1086. unsigned int cpu = (long)hcpu;
  1087. switch (action & ~CPU_TASKS_FROZEN) {
  1088. case CPU_UP_PREPARE:
  1089. if (x86_pmu.cpu_prepare)
  1090. x86_pmu.cpu_prepare(cpu);
  1091. break;
  1092. case CPU_STARTING:
  1093. if (x86_pmu.cpu_starting)
  1094. x86_pmu.cpu_starting(cpu);
  1095. break;
  1096. case CPU_DYING:
  1097. if (x86_pmu.cpu_dying)
  1098. x86_pmu.cpu_dying(cpu);
  1099. break;
  1100. case CPU_DEAD:
  1101. if (x86_pmu.cpu_dead)
  1102. x86_pmu.cpu_dead(cpu);
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. return NOTIFY_OK;
  1108. }
  1109. static void __init pmu_check_apic(void)
  1110. {
  1111. if (cpu_has_apic)
  1112. return;
  1113. x86_pmu.apic = 0;
  1114. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1115. pr_info("no hardware sampling interrupt available.\n");
  1116. }
  1117. void __init init_hw_perf_events(void)
  1118. {
  1119. struct event_constraint *c;
  1120. int err;
  1121. pr_info("Performance Events: ");
  1122. switch (boot_cpu_data.x86_vendor) {
  1123. case X86_VENDOR_INTEL:
  1124. err = intel_pmu_init();
  1125. break;
  1126. case X86_VENDOR_AMD:
  1127. err = amd_pmu_init();
  1128. break;
  1129. default:
  1130. return;
  1131. }
  1132. if (err != 0) {
  1133. pr_cont("no PMU driver, software events only.\n");
  1134. return;
  1135. }
  1136. pmu_check_apic();
  1137. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1138. if (x86_pmu.quirks)
  1139. x86_pmu.quirks();
  1140. if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
  1141. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1142. x86_pmu.num_events, X86_PMC_MAX_GENERIC);
  1143. x86_pmu.num_events = X86_PMC_MAX_GENERIC;
  1144. }
  1145. x86_pmu.intel_ctrl = (1 << x86_pmu.num_events) - 1;
  1146. perf_max_events = x86_pmu.num_events;
  1147. if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
  1148. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1149. x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
  1150. x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
  1151. }
  1152. x86_pmu.intel_ctrl |=
  1153. ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
  1154. perf_events_lapic_init();
  1155. register_die_notifier(&perf_event_nmi_notifier);
  1156. unconstrained = (struct event_constraint)
  1157. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1,
  1158. 0, x86_pmu.num_events);
  1159. if (x86_pmu.event_constraints) {
  1160. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1161. if (c->cmask != INTEL_ARCH_FIXED_MASK)
  1162. continue;
  1163. c->idxmsk64 |= (1ULL << x86_pmu.num_events) - 1;
  1164. c->weight += x86_pmu.num_events;
  1165. }
  1166. }
  1167. pr_info("... version: %d\n", x86_pmu.version);
  1168. pr_info("... bit width: %d\n", x86_pmu.event_bits);
  1169. pr_info("... generic registers: %d\n", x86_pmu.num_events);
  1170. pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
  1171. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1172. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
  1173. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1174. perf_cpu_notifier(x86_pmu_notifier);
  1175. }
  1176. static inline void x86_pmu_read(struct perf_event *event)
  1177. {
  1178. x86_perf_event_update(event);
  1179. }
  1180. static const struct pmu pmu = {
  1181. .enable = x86_pmu_enable,
  1182. .disable = x86_pmu_disable,
  1183. .start = x86_pmu_start,
  1184. .stop = x86_pmu_stop,
  1185. .read = x86_pmu_read,
  1186. .unthrottle = x86_pmu_unthrottle,
  1187. };
  1188. /*
  1189. * validate that we can schedule this event
  1190. */
  1191. static int validate_event(struct perf_event *event)
  1192. {
  1193. struct cpu_hw_events *fake_cpuc;
  1194. struct event_constraint *c;
  1195. int ret = 0;
  1196. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1197. if (!fake_cpuc)
  1198. return -ENOMEM;
  1199. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1200. if (!c || !c->weight)
  1201. ret = -ENOSPC;
  1202. if (x86_pmu.put_event_constraints)
  1203. x86_pmu.put_event_constraints(fake_cpuc, event);
  1204. kfree(fake_cpuc);
  1205. return ret;
  1206. }
  1207. /*
  1208. * validate a single event group
  1209. *
  1210. * validation include:
  1211. * - check events are compatible which each other
  1212. * - events do not compete for the same counter
  1213. * - number of events <= number of counters
  1214. *
  1215. * validation ensures the group can be loaded onto the
  1216. * PMU if it was the only group available.
  1217. */
  1218. static int validate_group(struct perf_event *event)
  1219. {
  1220. struct perf_event *leader = event->group_leader;
  1221. struct cpu_hw_events *fake_cpuc;
  1222. int ret, n;
  1223. ret = -ENOMEM;
  1224. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1225. if (!fake_cpuc)
  1226. goto out;
  1227. /*
  1228. * the event is not yet connected with its
  1229. * siblings therefore we must first collect
  1230. * existing siblings, then add the new event
  1231. * before we can simulate the scheduling
  1232. */
  1233. ret = -ENOSPC;
  1234. n = collect_events(fake_cpuc, leader, true);
  1235. if (n < 0)
  1236. goto out_free;
  1237. fake_cpuc->n_events = n;
  1238. n = collect_events(fake_cpuc, event, false);
  1239. if (n < 0)
  1240. goto out_free;
  1241. fake_cpuc->n_events = n;
  1242. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1243. out_free:
  1244. kfree(fake_cpuc);
  1245. out:
  1246. return ret;
  1247. }
  1248. const struct pmu *hw_perf_event_init(struct perf_event *event)
  1249. {
  1250. const struct pmu *tmp;
  1251. int err;
  1252. err = __hw_perf_event_init(event);
  1253. if (!err) {
  1254. /*
  1255. * we temporarily connect event to its pmu
  1256. * such that validate_group() can classify
  1257. * it as an x86 event using is_x86_event()
  1258. */
  1259. tmp = event->pmu;
  1260. event->pmu = &pmu;
  1261. if (event->group_leader != event)
  1262. err = validate_group(event);
  1263. else
  1264. err = validate_event(event);
  1265. event->pmu = tmp;
  1266. }
  1267. if (err) {
  1268. if (event->destroy)
  1269. event->destroy(event);
  1270. return ERR_PTR(err);
  1271. }
  1272. return &pmu;
  1273. }
  1274. /*
  1275. * callchain support
  1276. */
  1277. static inline
  1278. void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1279. {
  1280. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1281. entry->ip[entry->nr++] = ip;
  1282. }
  1283. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
  1284. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
  1285. static void
  1286. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1287. {
  1288. /* Ignore warnings */
  1289. }
  1290. static void backtrace_warning(void *data, char *msg)
  1291. {
  1292. /* Ignore warnings */
  1293. }
  1294. static int backtrace_stack(void *data, char *name)
  1295. {
  1296. return 0;
  1297. }
  1298. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1299. {
  1300. struct perf_callchain_entry *entry = data;
  1301. if (reliable)
  1302. callchain_store(entry, addr);
  1303. }
  1304. static const struct stacktrace_ops backtrace_ops = {
  1305. .warning = backtrace_warning,
  1306. .warning_symbol = backtrace_warning_symbol,
  1307. .stack = backtrace_stack,
  1308. .address = backtrace_address,
  1309. .walk_stack = print_context_stack_bp,
  1310. };
  1311. #include "../dumpstack.h"
  1312. static void
  1313. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1314. {
  1315. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1316. callchain_store(entry, regs->ip);
  1317. dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
  1318. }
  1319. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  1320. {
  1321. unsigned long bytes;
  1322. bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
  1323. return bytes == sizeof(*frame);
  1324. }
  1325. static void
  1326. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1327. {
  1328. struct stack_frame frame;
  1329. const void __user *fp;
  1330. if (!user_mode(regs))
  1331. regs = task_pt_regs(current);
  1332. fp = (void __user *)regs->bp;
  1333. callchain_store(entry, PERF_CONTEXT_USER);
  1334. callchain_store(entry, regs->ip);
  1335. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1336. frame.next_frame = NULL;
  1337. frame.return_address = 0;
  1338. if (!copy_stack_frame(fp, &frame))
  1339. break;
  1340. if ((unsigned long)fp < regs->sp)
  1341. break;
  1342. callchain_store(entry, frame.return_address);
  1343. fp = frame.next_frame;
  1344. }
  1345. }
  1346. static void
  1347. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1348. {
  1349. int is_user;
  1350. if (!regs)
  1351. return;
  1352. is_user = user_mode(regs);
  1353. if (is_user && current->state != TASK_RUNNING)
  1354. return;
  1355. if (!is_user)
  1356. perf_callchain_kernel(regs, entry);
  1357. if (current->mm)
  1358. perf_callchain_user(regs, entry);
  1359. }
  1360. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1361. {
  1362. struct perf_callchain_entry *entry;
  1363. if (in_nmi())
  1364. entry = &__get_cpu_var(pmc_nmi_entry);
  1365. else
  1366. entry = &__get_cpu_var(pmc_irq_entry);
  1367. entry->nr = 0;
  1368. perf_do_callchain(regs, entry);
  1369. return entry;
  1370. }
  1371. #ifdef CONFIG_EVENT_TRACING
  1372. void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
  1373. {
  1374. regs->ip = ip;
  1375. /*
  1376. * perf_arch_fetch_caller_regs adds another call, we need to increment
  1377. * the skip level
  1378. */
  1379. regs->bp = rewind_frame_pointer(skip + 1);
  1380. regs->cs = __KERNEL_CS;
  1381. local_save_flags(regs->flags);
  1382. }
  1383. #endif