perf_counter.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Performance counter 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. *
  10. * For licencing details see kernel-base/COPYING
  11. */
  12. #include <linux/perf_counter.h>
  13. #include <linux/capability.h>
  14. #include <linux/notifier.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/apic.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/nmi.h>
  24. static u64 perf_counter_mask __read_mostly;
  25. struct cpu_hw_counters {
  26. struct perf_counter *counters[X86_PMC_IDX_MAX];
  27. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  28. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. unsigned long interrupts;
  30. int enabled;
  31. };
  32. /*
  33. * struct x86_pmu - generic x86 pmu
  34. */
  35. struct x86_pmu {
  36. const char *name;
  37. int version;
  38. int (*handle_irq)(struct pt_regs *, int);
  39. void (*disable_all)(void);
  40. void (*enable_all)(void);
  41. void (*enable)(struct hw_perf_counter *, int);
  42. void (*disable)(struct hw_perf_counter *, int);
  43. unsigned eventsel;
  44. unsigned perfctr;
  45. u64 (*event_map)(int);
  46. u64 (*raw_event)(u64);
  47. int max_events;
  48. int num_counters;
  49. int num_counters_fixed;
  50. int counter_bits;
  51. u64 counter_mask;
  52. u64 max_period;
  53. u64 intel_ctrl;
  54. };
  55. static struct x86_pmu x86_pmu __read_mostly;
  56. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  57. .enabled = 1,
  58. };
  59. /*
  60. * Intel PerfMon v3. Used on Core2 and later.
  61. */
  62. static const u64 intel_perfmon_event_map[] =
  63. {
  64. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  65. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  66. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  67. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  68. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  69. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  70. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  71. };
  72. static u64 intel_pmu_event_map(int event)
  73. {
  74. return intel_perfmon_event_map[event];
  75. }
  76. static u64 intel_pmu_raw_event(u64 event)
  77. {
  78. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  79. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  80. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  81. #define CORE_EVNTSEL_MASK \
  82. (CORE_EVNTSEL_EVENT_MASK | \
  83. CORE_EVNTSEL_UNIT_MASK | \
  84. CORE_EVNTSEL_COUNTER_MASK)
  85. return event & CORE_EVNTSEL_MASK;
  86. }
  87. /*
  88. * AMD Performance Monitor K7 and later.
  89. */
  90. static const u64 amd_perfmon_event_map[] =
  91. {
  92. [PERF_COUNT_CPU_CYCLES] = 0x0076,
  93. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  94. [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
  95. [PERF_COUNT_CACHE_MISSES] = 0x0081,
  96. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  97. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  98. };
  99. static u64 amd_pmu_event_map(int event)
  100. {
  101. return amd_perfmon_event_map[event];
  102. }
  103. static u64 amd_pmu_raw_event(u64 event)
  104. {
  105. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  106. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  107. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  108. #define K7_EVNTSEL_MASK \
  109. (K7_EVNTSEL_EVENT_MASK | \
  110. K7_EVNTSEL_UNIT_MASK | \
  111. K7_EVNTSEL_COUNTER_MASK)
  112. return event & K7_EVNTSEL_MASK;
  113. }
  114. /*
  115. * Propagate counter elapsed time into the generic counter.
  116. * Can only be executed on the CPU where the counter is active.
  117. * Returns the delta events processed.
  118. */
  119. static u64
  120. x86_perf_counter_update(struct perf_counter *counter,
  121. struct hw_perf_counter *hwc, int idx)
  122. {
  123. int shift = 64 - x86_pmu.counter_bits;
  124. u64 prev_raw_count, new_raw_count;
  125. s64 delta;
  126. /*
  127. * Careful: an NMI might modify the previous counter value.
  128. *
  129. * Our tactic to handle this is to first atomically read and
  130. * exchange a new raw count - then add that new-prev delta
  131. * count to the generic counter atomically:
  132. */
  133. again:
  134. prev_raw_count = atomic64_read(&hwc->prev_count);
  135. rdmsrl(hwc->counter_base + idx, new_raw_count);
  136. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  137. new_raw_count) != prev_raw_count)
  138. goto again;
  139. /*
  140. * Now we have the new raw value and have updated the prev
  141. * timestamp already. We can now calculate the elapsed delta
  142. * (counter-)time and add that to the generic counter.
  143. *
  144. * Careful, not all hw sign-extends above the physical width
  145. * of the count.
  146. */
  147. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  148. delta >>= shift;
  149. atomic64_add(delta, &counter->count);
  150. atomic64_sub(delta, &hwc->period_left);
  151. return new_raw_count;
  152. }
  153. static atomic_t active_counters;
  154. static DEFINE_MUTEX(pmc_reserve_mutex);
  155. static bool reserve_pmc_hardware(void)
  156. {
  157. int i;
  158. if (nmi_watchdog == NMI_LOCAL_APIC)
  159. disable_lapic_nmi_watchdog();
  160. for (i = 0; i < x86_pmu.num_counters; i++) {
  161. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  162. goto perfctr_fail;
  163. }
  164. for (i = 0; i < x86_pmu.num_counters; i++) {
  165. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  166. goto eventsel_fail;
  167. }
  168. return true;
  169. eventsel_fail:
  170. for (i--; i >= 0; i--)
  171. release_evntsel_nmi(x86_pmu.eventsel + i);
  172. i = x86_pmu.num_counters;
  173. perfctr_fail:
  174. for (i--; i >= 0; i--)
  175. release_perfctr_nmi(x86_pmu.perfctr + i);
  176. if (nmi_watchdog == NMI_LOCAL_APIC)
  177. enable_lapic_nmi_watchdog();
  178. return false;
  179. }
  180. static void release_pmc_hardware(void)
  181. {
  182. int i;
  183. for (i = 0; i < x86_pmu.num_counters; i++) {
  184. release_perfctr_nmi(x86_pmu.perfctr + i);
  185. release_evntsel_nmi(x86_pmu.eventsel + i);
  186. }
  187. if (nmi_watchdog == NMI_LOCAL_APIC)
  188. enable_lapic_nmi_watchdog();
  189. }
  190. static void hw_perf_counter_destroy(struct perf_counter *counter)
  191. {
  192. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  193. release_pmc_hardware();
  194. mutex_unlock(&pmc_reserve_mutex);
  195. }
  196. }
  197. static inline int x86_pmu_initialized(void)
  198. {
  199. return x86_pmu.handle_irq != NULL;
  200. }
  201. /*
  202. * Setup the hardware configuration for a given hw_event_type
  203. */
  204. static int __hw_perf_counter_init(struct perf_counter *counter)
  205. {
  206. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  207. struct hw_perf_counter *hwc = &counter->hw;
  208. int err;
  209. if (!x86_pmu_initialized())
  210. return -ENODEV;
  211. err = 0;
  212. if (!atomic_inc_not_zero(&active_counters)) {
  213. mutex_lock(&pmc_reserve_mutex);
  214. if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
  215. err = -EBUSY;
  216. else
  217. atomic_inc(&active_counters);
  218. mutex_unlock(&pmc_reserve_mutex);
  219. }
  220. if (err)
  221. return err;
  222. /*
  223. * Generate PMC IRQs:
  224. * (keep 'enabled' bit clear for now)
  225. */
  226. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  227. /*
  228. * Count user and OS events unless requested not to.
  229. */
  230. if (!hw_event->exclude_user)
  231. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  232. if (!hw_event->exclude_kernel)
  233. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  234. /*
  235. * If privileged enough, allow NMI events:
  236. */
  237. hwc->nmi = 0;
  238. if (hw_event->nmi) {
  239. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  240. return -EACCES;
  241. hwc->nmi = 1;
  242. }
  243. atomic64_set(&hwc->period_left,
  244. min(x86_pmu.max_period, hwc->irq_period));
  245. /*
  246. * Raw event type provide the config in the event structure
  247. */
  248. if (perf_event_raw(hw_event)) {
  249. hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
  250. } else {
  251. if (perf_event_id(hw_event) >= x86_pmu.max_events)
  252. return -EINVAL;
  253. /*
  254. * The generic map:
  255. */
  256. hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
  257. }
  258. counter->destroy = hw_perf_counter_destroy;
  259. return 0;
  260. }
  261. static void intel_pmu_disable_all(void)
  262. {
  263. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  264. }
  265. static void amd_pmu_disable_all(void)
  266. {
  267. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  268. int idx;
  269. if (!cpuc->enabled)
  270. return;
  271. cpuc->enabled = 0;
  272. /*
  273. * ensure we write the disable before we start disabling the
  274. * counters proper, so that amd_pmu_enable_counter() does the
  275. * right thing.
  276. */
  277. barrier();
  278. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  279. u64 val;
  280. if (!test_bit(idx, cpuc->active_mask))
  281. continue;
  282. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  283. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  284. continue;
  285. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  286. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  287. }
  288. }
  289. void hw_perf_disable(void)
  290. {
  291. if (!x86_pmu_initialized())
  292. return;
  293. return x86_pmu.disable_all();
  294. }
  295. static void intel_pmu_enable_all(void)
  296. {
  297. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  298. }
  299. static void amd_pmu_enable_all(void)
  300. {
  301. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  302. int idx;
  303. if (cpuc->enabled)
  304. return;
  305. cpuc->enabled = 1;
  306. barrier();
  307. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  308. u64 val;
  309. if (!test_bit(idx, cpuc->active_mask))
  310. continue;
  311. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  312. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  313. continue;
  314. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  315. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  316. }
  317. }
  318. void hw_perf_enable(void)
  319. {
  320. if (!x86_pmu_initialized())
  321. return;
  322. x86_pmu.enable_all();
  323. }
  324. static inline u64 intel_pmu_get_status(void)
  325. {
  326. u64 status;
  327. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  328. return status;
  329. }
  330. static inline void intel_pmu_ack_status(u64 ack)
  331. {
  332. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  333. }
  334. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  335. {
  336. int err;
  337. err = checking_wrmsrl(hwc->config_base + idx,
  338. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  339. }
  340. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  341. {
  342. int err;
  343. err = checking_wrmsrl(hwc->config_base + idx,
  344. hwc->config);
  345. }
  346. static inline void
  347. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  348. {
  349. int idx = __idx - X86_PMC_IDX_FIXED;
  350. u64 ctrl_val, mask;
  351. int err;
  352. mask = 0xfULL << (idx * 4);
  353. rdmsrl(hwc->config_base, ctrl_val);
  354. ctrl_val &= ~mask;
  355. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  356. }
  357. static inline void
  358. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  359. {
  360. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  361. intel_pmu_disable_fixed(hwc, idx);
  362. return;
  363. }
  364. x86_pmu_disable_counter(hwc, idx);
  365. }
  366. static inline void
  367. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  368. {
  369. x86_pmu_disable_counter(hwc, idx);
  370. }
  371. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  372. /*
  373. * Set the next IRQ period, based on the hwc->period_left value.
  374. * To be called with the counter disabled in hw:
  375. */
  376. static void
  377. x86_perf_counter_set_period(struct perf_counter *counter,
  378. struct hw_perf_counter *hwc, int idx)
  379. {
  380. s64 left = atomic64_read(&hwc->period_left);
  381. s64 period = min(x86_pmu.max_period, hwc->irq_period);
  382. int err;
  383. /*
  384. * If we are way outside a reasoable range then just skip forward:
  385. */
  386. if (unlikely(left <= -period)) {
  387. left = period;
  388. atomic64_set(&hwc->period_left, left);
  389. }
  390. if (unlikely(left <= 0)) {
  391. left += period;
  392. atomic64_set(&hwc->period_left, left);
  393. }
  394. /*
  395. * Quirk: certain CPUs dont like it if just 1 event is left:
  396. */
  397. if (unlikely(left < 2))
  398. left = 2;
  399. per_cpu(prev_left[idx], smp_processor_id()) = left;
  400. /*
  401. * The hw counter starts counting from this counter offset,
  402. * mark it to be able to extra future deltas:
  403. */
  404. atomic64_set(&hwc->prev_count, (u64)-left);
  405. err = checking_wrmsrl(hwc->counter_base + idx,
  406. (u64)(-left) & x86_pmu.counter_mask);
  407. }
  408. static inline void
  409. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  410. {
  411. int idx = __idx - X86_PMC_IDX_FIXED;
  412. u64 ctrl_val, bits, mask;
  413. int err;
  414. /*
  415. * Enable IRQ generation (0x8),
  416. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  417. * if requested:
  418. */
  419. bits = 0x8ULL;
  420. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  421. bits |= 0x2;
  422. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  423. bits |= 0x1;
  424. bits <<= (idx * 4);
  425. mask = 0xfULL << (idx * 4);
  426. rdmsrl(hwc->config_base, ctrl_val);
  427. ctrl_val &= ~mask;
  428. ctrl_val |= bits;
  429. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  430. }
  431. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  432. {
  433. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  434. intel_pmu_enable_fixed(hwc, idx);
  435. return;
  436. }
  437. x86_pmu_enable_counter(hwc, idx);
  438. }
  439. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  440. {
  441. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  442. if (cpuc->enabled)
  443. x86_pmu_enable_counter(hwc, idx);
  444. else
  445. x86_pmu_disable_counter(hwc, idx);
  446. }
  447. static int
  448. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  449. {
  450. unsigned int event;
  451. if (!x86_pmu.num_counters_fixed)
  452. return -1;
  453. if (unlikely(hwc->nmi))
  454. return -1;
  455. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  456. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  457. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  458. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  459. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  460. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  461. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  462. return -1;
  463. }
  464. /*
  465. * Find a PMC slot for the freshly enabled / scheduled in counter:
  466. */
  467. static int x86_pmu_enable(struct perf_counter *counter)
  468. {
  469. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  470. struct hw_perf_counter *hwc = &counter->hw;
  471. int idx;
  472. idx = fixed_mode_idx(counter, hwc);
  473. if (idx >= 0) {
  474. /*
  475. * Try to get the fixed counter, if that is already taken
  476. * then try to get a generic counter:
  477. */
  478. if (test_and_set_bit(idx, cpuc->used_mask))
  479. goto try_generic;
  480. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  481. /*
  482. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  483. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  484. */
  485. hwc->counter_base =
  486. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  487. hwc->idx = idx;
  488. } else {
  489. idx = hwc->idx;
  490. /* Try to get the previous generic counter again */
  491. if (test_and_set_bit(idx, cpuc->used_mask)) {
  492. try_generic:
  493. idx = find_first_zero_bit(cpuc->used_mask,
  494. x86_pmu.num_counters);
  495. if (idx == x86_pmu.num_counters)
  496. return -EAGAIN;
  497. set_bit(idx, cpuc->used_mask);
  498. hwc->idx = idx;
  499. }
  500. hwc->config_base = x86_pmu.eventsel;
  501. hwc->counter_base = x86_pmu.perfctr;
  502. }
  503. perf_counters_lapic_init(hwc->nmi);
  504. x86_pmu.disable(hwc, idx);
  505. cpuc->counters[idx] = counter;
  506. set_bit(idx, cpuc->active_mask);
  507. x86_perf_counter_set_period(counter, hwc, idx);
  508. x86_pmu.enable(hwc, idx);
  509. return 0;
  510. }
  511. void perf_counter_print_debug(void)
  512. {
  513. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  514. struct cpu_hw_counters *cpuc;
  515. unsigned long flags;
  516. int cpu, idx;
  517. if (!x86_pmu.num_counters)
  518. return;
  519. local_irq_save(flags);
  520. cpu = smp_processor_id();
  521. cpuc = &per_cpu(cpu_hw_counters, cpu);
  522. if (x86_pmu.version >= 2) {
  523. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  524. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  525. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  526. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  527. pr_info("\n");
  528. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  529. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  530. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  531. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  532. }
  533. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  534. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  535. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  536. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  537. prev_left = per_cpu(prev_left[idx], cpu);
  538. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  539. cpu, idx, pmc_ctrl);
  540. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  541. cpu, idx, pmc_count);
  542. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  543. cpu, idx, prev_left);
  544. }
  545. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  546. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  547. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  548. cpu, idx, pmc_count);
  549. }
  550. local_irq_restore(flags);
  551. }
  552. static void x86_pmu_disable(struct perf_counter *counter)
  553. {
  554. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  555. struct hw_perf_counter *hwc = &counter->hw;
  556. int idx = hwc->idx;
  557. /*
  558. * Must be done before we disable, otherwise the nmi handler
  559. * could reenable again:
  560. */
  561. clear_bit(idx, cpuc->active_mask);
  562. x86_pmu.disable(hwc, idx);
  563. /*
  564. * Make sure the cleared pointer becomes visible before we
  565. * (potentially) free the counter:
  566. */
  567. barrier();
  568. /*
  569. * Drain the remaining delta count out of a counter
  570. * that we are disabling:
  571. */
  572. x86_perf_counter_update(counter, hwc, idx);
  573. cpuc->counters[idx] = NULL;
  574. clear_bit(idx, cpuc->used_mask);
  575. }
  576. /*
  577. * Save and restart an expired counter. Called by NMI contexts,
  578. * so it has to be careful about preempting normal counter ops:
  579. */
  580. static void intel_pmu_save_and_restart(struct perf_counter *counter)
  581. {
  582. struct hw_perf_counter *hwc = &counter->hw;
  583. int idx = hwc->idx;
  584. x86_perf_counter_update(counter, hwc, idx);
  585. x86_perf_counter_set_period(counter, hwc, idx);
  586. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  587. intel_pmu_enable_counter(hwc, idx);
  588. }
  589. /*
  590. * Maximum interrupt frequency of 100KHz per CPU
  591. */
  592. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  593. /*
  594. * This handler is triggered by the local APIC, so the APIC IRQ handling
  595. * rules apply:
  596. */
  597. static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
  598. {
  599. struct cpu_hw_counters *cpuc;
  600. struct cpu_hw_counters;
  601. int bit, cpu, loops;
  602. u64 ack, status;
  603. cpu = smp_processor_id();
  604. cpuc = &per_cpu(cpu_hw_counters, cpu);
  605. perf_disable();
  606. status = intel_pmu_get_status();
  607. if (!status) {
  608. perf_enable();
  609. return 0;
  610. }
  611. loops = 0;
  612. again:
  613. if (++loops > 100) {
  614. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  615. return 1;
  616. }
  617. inc_irq_stat(apic_perf_irqs);
  618. ack = status;
  619. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  620. struct perf_counter *counter = cpuc->counters[bit];
  621. clear_bit(bit, (unsigned long *) &status);
  622. if (!test_bit(bit, cpuc->active_mask))
  623. continue;
  624. intel_pmu_save_and_restart(counter);
  625. if (perf_counter_overflow(counter, nmi, regs, 0))
  626. intel_pmu_disable_counter(&counter->hw, bit);
  627. }
  628. intel_pmu_ack_status(ack);
  629. /*
  630. * Repeat if there is more work to be done:
  631. */
  632. status = intel_pmu_get_status();
  633. if (status)
  634. goto again;
  635. if (++cpuc->interrupts != PERFMON_MAX_INTERRUPTS)
  636. perf_enable();
  637. return 1;
  638. }
  639. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
  640. {
  641. int cpu, idx, throttle = 0, handled = 0;
  642. struct cpu_hw_counters *cpuc;
  643. struct perf_counter *counter;
  644. struct hw_perf_counter *hwc;
  645. u64 val;
  646. cpu = smp_processor_id();
  647. cpuc = &per_cpu(cpu_hw_counters, cpu);
  648. if (++cpuc->interrupts == PERFMON_MAX_INTERRUPTS) {
  649. throttle = 1;
  650. __perf_disable();
  651. cpuc->enabled = 0;
  652. barrier();
  653. }
  654. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  655. int disable = 0;
  656. if (!test_bit(idx, cpuc->active_mask))
  657. continue;
  658. counter = cpuc->counters[idx];
  659. hwc = &counter->hw;
  660. if (counter->hw_event.nmi != nmi)
  661. goto next;
  662. val = x86_perf_counter_update(counter, hwc, idx);
  663. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  664. goto next;
  665. /* counter overflow */
  666. x86_perf_counter_set_period(counter, hwc, idx);
  667. handled = 1;
  668. inc_irq_stat(apic_perf_irqs);
  669. disable = perf_counter_overflow(counter, nmi, regs, 0);
  670. next:
  671. if (disable || throttle)
  672. amd_pmu_disable_counter(hwc, idx);
  673. }
  674. return handled;
  675. }
  676. void perf_counter_unthrottle(void)
  677. {
  678. struct cpu_hw_counters *cpuc;
  679. if (!x86_pmu_initialized())
  680. return;
  681. cpuc = &__get_cpu_var(cpu_hw_counters);
  682. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  683. /*
  684. * Clear them before re-enabling irqs/NMIs again:
  685. */
  686. cpuc->interrupts = 0;
  687. perf_enable();
  688. } else {
  689. cpuc->interrupts = 0;
  690. }
  691. }
  692. void smp_perf_counter_interrupt(struct pt_regs *regs)
  693. {
  694. irq_enter();
  695. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  696. ack_APIC_irq();
  697. x86_pmu.handle_irq(regs, 0);
  698. irq_exit();
  699. }
  700. void smp_perf_pending_interrupt(struct pt_regs *regs)
  701. {
  702. irq_enter();
  703. ack_APIC_irq();
  704. inc_irq_stat(apic_pending_irqs);
  705. perf_counter_do_pending();
  706. irq_exit();
  707. }
  708. void set_perf_counter_pending(void)
  709. {
  710. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  711. }
  712. void perf_counters_lapic_init(int nmi)
  713. {
  714. u32 apic_val;
  715. if (!x86_pmu_initialized())
  716. return;
  717. /*
  718. * Enable the performance counter vector in the APIC LVT:
  719. */
  720. apic_val = apic_read(APIC_LVTERR);
  721. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  722. if (nmi)
  723. apic_write(APIC_LVTPC, APIC_DM_NMI);
  724. else
  725. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  726. apic_write(APIC_LVTERR, apic_val);
  727. }
  728. static int __kprobes
  729. perf_counter_nmi_handler(struct notifier_block *self,
  730. unsigned long cmd, void *__args)
  731. {
  732. struct die_args *args = __args;
  733. struct pt_regs *regs;
  734. if (!atomic_read(&active_counters))
  735. return NOTIFY_DONE;
  736. switch (cmd) {
  737. case DIE_NMI:
  738. case DIE_NMI_IPI:
  739. break;
  740. default:
  741. return NOTIFY_DONE;
  742. }
  743. regs = args->regs;
  744. apic_write(APIC_LVTPC, APIC_DM_NMI);
  745. /*
  746. * Can't rely on the handled return value to say it was our NMI, two
  747. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  748. *
  749. * If the first NMI handles both, the latter will be empty and daze
  750. * the CPU.
  751. */
  752. x86_pmu.handle_irq(regs, 1);
  753. return NOTIFY_STOP;
  754. }
  755. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  756. .notifier_call = perf_counter_nmi_handler,
  757. .next = NULL,
  758. .priority = 1
  759. };
  760. static struct x86_pmu intel_pmu = {
  761. .name = "Intel",
  762. .handle_irq = intel_pmu_handle_irq,
  763. .disable_all = intel_pmu_disable_all,
  764. .enable_all = intel_pmu_enable_all,
  765. .enable = intel_pmu_enable_counter,
  766. .disable = intel_pmu_disable_counter,
  767. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  768. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  769. .event_map = intel_pmu_event_map,
  770. .raw_event = intel_pmu_raw_event,
  771. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  772. /*
  773. * Intel PMCs cannot be accessed sanely above 32 bit width,
  774. * so we install an artificial 1<<31 period regardless of
  775. * the generic counter period:
  776. */
  777. .max_period = (1ULL << 31) - 1,
  778. };
  779. static struct x86_pmu amd_pmu = {
  780. .name = "AMD",
  781. .handle_irq = amd_pmu_handle_irq,
  782. .disable_all = amd_pmu_disable_all,
  783. .enable_all = amd_pmu_enable_all,
  784. .enable = amd_pmu_enable_counter,
  785. .disable = amd_pmu_disable_counter,
  786. .eventsel = MSR_K7_EVNTSEL0,
  787. .perfctr = MSR_K7_PERFCTR0,
  788. .event_map = amd_pmu_event_map,
  789. .raw_event = amd_pmu_raw_event,
  790. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  791. .num_counters = 4,
  792. .counter_bits = 48,
  793. .counter_mask = (1ULL << 48) - 1,
  794. /* use highest bit to detect overflow */
  795. .max_period = (1ULL << 47) - 1,
  796. };
  797. static int intel_pmu_init(void)
  798. {
  799. union cpuid10_edx edx;
  800. union cpuid10_eax eax;
  801. unsigned int unused;
  802. unsigned int ebx;
  803. int version;
  804. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  805. return -ENODEV;
  806. /*
  807. * Check whether the Architectural PerfMon supports
  808. * Branch Misses Retired Event or not.
  809. */
  810. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  811. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  812. return -ENODEV;
  813. version = eax.split.version_id;
  814. if (version < 2)
  815. return -ENODEV;
  816. x86_pmu = intel_pmu;
  817. x86_pmu.version = version;
  818. x86_pmu.num_counters = eax.split.num_counters;
  819. /*
  820. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  821. * assume at least 3 counters:
  822. */
  823. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  824. x86_pmu.counter_bits = eax.split.bit_width;
  825. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  826. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  827. return 0;
  828. }
  829. static int amd_pmu_init(void)
  830. {
  831. x86_pmu = amd_pmu;
  832. return 0;
  833. }
  834. void __init init_hw_perf_counters(void)
  835. {
  836. int err;
  837. switch (boot_cpu_data.x86_vendor) {
  838. case X86_VENDOR_INTEL:
  839. err = intel_pmu_init();
  840. break;
  841. case X86_VENDOR_AMD:
  842. err = amd_pmu_init();
  843. break;
  844. default:
  845. return;
  846. }
  847. if (err != 0)
  848. return;
  849. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  850. pr_info("... version: %d\n", x86_pmu.version);
  851. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  852. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  853. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  854. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  855. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  856. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  857. }
  858. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  859. perf_max_counters = x86_pmu.num_counters;
  860. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  861. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  862. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  863. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  864. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  865. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  866. }
  867. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  868. perf_counter_mask |=
  869. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  870. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  871. perf_counters_lapic_init(0);
  872. register_die_notifier(&perf_counter_nmi_notifier);
  873. }
  874. static inline void x86_pmu_read(struct perf_counter *counter)
  875. {
  876. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  877. }
  878. static const struct pmu pmu = {
  879. .enable = x86_pmu_enable,
  880. .disable = x86_pmu_disable,
  881. .read = x86_pmu_read,
  882. };
  883. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  884. {
  885. int err;
  886. err = __hw_perf_counter_init(counter);
  887. if (err)
  888. return ERR_PTR(err);
  889. return &pmu;
  890. }
  891. /*
  892. * callchain support
  893. */
  894. static inline
  895. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  896. {
  897. if (entry->nr < MAX_STACK_DEPTH)
  898. entry->ip[entry->nr++] = ip;
  899. }
  900. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  901. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  902. static void
  903. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  904. {
  905. /* Ignore warnings */
  906. }
  907. static void backtrace_warning(void *data, char *msg)
  908. {
  909. /* Ignore warnings */
  910. }
  911. static int backtrace_stack(void *data, char *name)
  912. {
  913. /* Don't bother with IRQ stacks for now */
  914. return -1;
  915. }
  916. static void backtrace_address(void *data, unsigned long addr, int reliable)
  917. {
  918. struct perf_callchain_entry *entry = data;
  919. if (reliable)
  920. callchain_store(entry, addr);
  921. }
  922. static const struct stacktrace_ops backtrace_ops = {
  923. .warning = backtrace_warning,
  924. .warning_symbol = backtrace_warning_symbol,
  925. .stack = backtrace_stack,
  926. .address = backtrace_address,
  927. };
  928. static void
  929. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  930. {
  931. unsigned long bp;
  932. char *stack;
  933. int nr = entry->nr;
  934. callchain_store(entry, instruction_pointer(regs));
  935. stack = ((char *)regs + sizeof(struct pt_regs));
  936. #ifdef CONFIG_FRAME_POINTER
  937. bp = frame_pointer(regs);
  938. #else
  939. bp = 0;
  940. #endif
  941. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  942. entry->kernel = entry->nr - nr;
  943. }
  944. struct stack_frame {
  945. const void __user *next_fp;
  946. unsigned long return_address;
  947. };
  948. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  949. {
  950. int ret;
  951. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  952. return 0;
  953. ret = 1;
  954. pagefault_disable();
  955. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  956. ret = 0;
  957. pagefault_enable();
  958. return ret;
  959. }
  960. static void
  961. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  962. {
  963. struct stack_frame frame;
  964. const void __user *fp;
  965. int nr = entry->nr;
  966. regs = (struct pt_regs *)current->thread.sp0 - 1;
  967. fp = (void __user *)regs->bp;
  968. callchain_store(entry, regs->ip);
  969. while (entry->nr < MAX_STACK_DEPTH) {
  970. frame.next_fp = NULL;
  971. frame.return_address = 0;
  972. if (!copy_stack_frame(fp, &frame))
  973. break;
  974. if ((unsigned long)fp < user_stack_pointer(regs))
  975. break;
  976. callchain_store(entry, frame.return_address);
  977. fp = frame.next_fp;
  978. }
  979. entry->user = entry->nr - nr;
  980. }
  981. static void
  982. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  983. {
  984. int is_user;
  985. if (!regs)
  986. return;
  987. is_user = user_mode(regs);
  988. if (!current || current->pid == 0)
  989. return;
  990. if (is_user && current->state != TASK_RUNNING)
  991. return;
  992. if (!is_user)
  993. perf_callchain_kernel(regs, entry);
  994. if (current->mm)
  995. perf_callchain_user(regs, entry);
  996. }
  997. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  998. {
  999. struct perf_callchain_entry *entry;
  1000. if (in_nmi())
  1001. entry = &__get_cpu_var(nmi_entry);
  1002. else
  1003. entry = &__get_cpu_var(irq_entry);
  1004. entry->nr = 0;
  1005. entry->hv = 0;
  1006. entry->kernel = 0;
  1007. entry->user = 0;
  1008. perf_do_callchain(regs, entry);
  1009. return entry;
  1010. }