perf_counter.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * Performance counter x86 architecture code
  3. *
  4. * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
  6. * Copyright(C) 2009 Jaswinder Singh Rajput
  7. *
  8. * For licencing details see kernel-base/COPYING
  9. */
  10. #include <linux/perf_counter.h>
  11. #include <linux/capability.h>
  12. #include <linux/notifier.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/module.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/sched.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/apic.h>
  20. #include <asm/stacktrace.h>
  21. static bool perf_counters_initialized __read_mostly;
  22. /*
  23. * Number of (generic) HW counters:
  24. */
  25. static int nr_counters_generic __read_mostly;
  26. static u64 perf_counter_mask __read_mostly;
  27. static u64 counter_value_mask __read_mostly;
  28. static int counter_value_bits __read_mostly;
  29. static int nr_counters_fixed __read_mostly;
  30. struct cpu_hw_counters {
  31. struct perf_counter *counters[X86_PMC_IDX_MAX];
  32. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  33. unsigned long interrupts;
  34. u64 throttle_ctrl;
  35. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  36. int enabled;
  37. };
  38. /*
  39. * struct pmc_x86_ops - performance counter x86 ops
  40. */
  41. struct pmc_x86_ops {
  42. u64 (*save_disable_all)(void);
  43. void (*restore_all)(u64);
  44. u64 (*get_status)(u64);
  45. void (*ack_status)(u64);
  46. void (*enable)(int, u64);
  47. void (*disable)(int, u64);
  48. unsigned eventsel;
  49. unsigned perfctr;
  50. u64 (*event_map)(int);
  51. u64 (*raw_event)(u64);
  52. int max_events;
  53. };
  54. static struct pmc_x86_ops *pmc_ops __read_mostly;
  55. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  56. .enabled = 1,
  57. };
  58. static __read_mostly int intel_perfmon_version;
  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 pmc_intel_event_map(int event)
  73. {
  74. return intel_perfmon_event_map[event];
  75. }
  76. static u64 pmc_intel_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 pmc_amd_event_map(int event)
  100. {
  101. return amd_perfmon_event_map[event];
  102. }
  103. static u64 pmc_amd_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 void
  120. x86_perf_counter_update(struct perf_counter *counter,
  121. struct hw_perf_counter *hwc, int idx)
  122. {
  123. u64 prev_raw_count, new_raw_count, delta;
  124. /*
  125. * Careful: an NMI might modify the previous counter value.
  126. *
  127. * Our tactic to handle this is to first atomically read and
  128. * exchange a new raw count - then add that new-prev delta
  129. * count to the generic counter atomically:
  130. */
  131. again:
  132. prev_raw_count = atomic64_read(&hwc->prev_count);
  133. rdmsrl(hwc->counter_base + idx, new_raw_count);
  134. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  135. new_raw_count) != prev_raw_count)
  136. goto again;
  137. /*
  138. * Now we have the new raw value and have updated the prev
  139. * timestamp already. We can now calculate the elapsed delta
  140. * (counter-)time and add that to the generic counter.
  141. *
  142. * Careful, not all hw sign-extends above the physical width
  143. * of the count, so we do that by clipping the delta to 32 bits:
  144. */
  145. delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
  146. atomic64_add(delta, &counter->count);
  147. atomic64_sub(delta, &hwc->period_left);
  148. }
  149. /*
  150. * Setup the hardware configuration for a given hw_event_type
  151. */
  152. static int __hw_perf_counter_init(struct perf_counter *counter)
  153. {
  154. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  155. struct hw_perf_counter *hwc = &counter->hw;
  156. if (unlikely(!perf_counters_initialized))
  157. return -EINVAL;
  158. /*
  159. * Generate PMC IRQs:
  160. * (keep 'enabled' bit clear for now)
  161. */
  162. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  163. /*
  164. * Count user and OS events unless requested not to.
  165. */
  166. if (!hw_event->exclude_user)
  167. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  168. if (!hw_event->exclude_kernel)
  169. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  170. /*
  171. * If privileged enough, allow NMI events:
  172. */
  173. hwc->nmi = 0;
  174. if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
  175. hwc->nmi = 1;
  176. hwc->irq_period = hw_event->irq_period;
  177. /*
  178. * Intel PMCs cannot be accessed sanely above 32 bit width,
  179. * so we install an artificial 1<<31 period regardless of
  180. * the generic counter period:
  181. */
  182. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  183. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
  184. hwc->irq_period = 0x7FFFFFFF;
  185. atomic64_set(&hwc->period_left, hwc->irq_period);
  186. /*
  187. * Raw event type provide the config in the event structure
  188. */
  189. if (perf_event_raw(hw_event)) {
  190. hwc->config |= pmc_ops->raw_event(perf_event_config(hw_event));
  191. } else {
  192. if (perf_event_id(hw_event) >= pmc_ops->max_events)
  193. return -EINVAL;
  194. /*
  195. * The generic map:
  196. */
  197. hwc->config |= pmc_ops->event_map(perf_event_id(hw_event));
  198. }
  199. return 0;
  200. }
  201. static u64 pmc_intel_save_disable_all(void)
  202. {
  203. u64 ctrl;
  204. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  205. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  206. return ctrl;
  207. }
  208. static u64 pmc_amd_save_disable_all(void)
  209. {
  210. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  211. int enabled, idx;
  212. enabled = cpuc->enabled;
  213. cpuc->enabled = 0;
  214. /*
  215. * ensure we write the disable before we start disabling the
  216. * counters proper, so that pcm_amd_enable() does the right thing.
  217. */
  218. barrier();
  219. for (idx = 0; idx < nr_counters_generic; idx++) {
  220. u64 val;
  221. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  222. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
  223. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  224. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  225. }
  226. }
  227. return enabled;
  228. }
  229. u64 hw_perf_save_disable(void)
  230. {
  231. if (unlikely(!perf_counters_initialized))
  232. return 0;
  233. return pmc_ops->save_disable_all();
  234. }
  235. /*
  236. * Exported because of ACPI idle
  237. */
  238. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  239. static void pmc_intel_restore_all(u64 ctrl)
  240. {
  241. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  242. }
  243. static void pmc_amd_restore_all(u64 ctrl)
  244. {
  245. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  246. int idx;
  247. cpuc->enabled = ctrl;
  248. barrier();
  249. if (!ctrl)
  250. return;
  251. for (idx = 0; idx < nr_counters_generic; idx++) {
  252. if (test_bit(idx, cpuc->active_mask)) {
  253. u64 val;
  254. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  255. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  256. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  257. }
  258. }
  259. }
  260. void hw_perf_restore(u64 ctrl)
  261. {
  262. if (unlikely(!perf_counters_initialized))
  263. return;
  264. pmc_ops->restore_all(ctrl);
  265. }
  266. /*
  267. * Exported because of ACPI idle
  268. */
  269. EXPORT_SYMBOL_GPL(hw_perf_restore);
  270. static u64 pmc_intel_get_status(u64 mask)
  271. {
  272. u64 status;
  273. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  274. return status;
  275. }
  276. static u64 pmc_amd_get_status(u64 mask)
  277. {
  278. u64 status = 0;
  279. int idx;
  280. for (idx = 0; idx < nr_counters_generic; idx++) {
  281. s64 val;
  282. if (!(mask & (1 << idx)))
  283. continue;
  284. rdmsrl(MSR_K7_PERFCTR0 + idx, val);
  285. val <<= (64 - counter_value_bits);
  286. if (val >= 0)
  287. status |= (1 << idx);
  288. }
  289. return status;
  290. }
  291. static u64 hw_perf_get_status(u64 mask)
  292. {
  293. if (unlikely(!perf_counters_initialized))
  294. return 0;
  295. return pmc_ops->get_status(mask);
  296. }
  297. static void pmc_intel_ack_status(u64 ack)
  298. {
  299. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  300. }
  301. static void pmc_amd_ack_status(u64 ack)
  302. {
  303. }
  304. static void hw_perf_ack_status(u64 ack)
  305. {
  306. if (unlikely(!perf_counters_initialized))
  307. return;
  308. pmc_ops->ack_status(ack);
  309. }
  310. static void pmc_intel_enable(int idx, u64 config)
  311. {
  312. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
  313. config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  314. }
  315. static void pmc_amd_enable(int idx, u64 config)
  316. {
  317. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  318. set_bit(idx, cpuc->active_mask);
  319. if (cpuc->enabled)
  320. config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  321. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  322. }
  323. static void hw_perf_enable(int idx, u64 config)
  324. {
  325. if (unlikely(!perf_counters_initialized))
  326. return;
  327. pmc_ops->enable(idx, config);
  328. }
  329. static void pmc_intel_disable(int idx, u64 config)
  330. {
  331. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
  332. }
  333. static void pmc_amd_disable(int idx, u64 config)
  334. {
  335. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  336. clear_bit(idx, cpuc->active_mask);
  337. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  338. }
  339. static void hw_perf_disable(int idx, u64 config)
  340. {
  341. if (unlikely(!perf_counters_initialized))
  342. return;
  343. pmc_ops->disable(idx, config);
  344. }
  345. static inline void
  346. __pmc_fixed_disable(struct perf_counter *counter,
  347. struct hw_perf_counter *hwc, unsigned 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. __pmc_generic_disable(struct perf_counter *counter,
  359. struct hw_perf_counter *hwc, unsigned int idx)
  360. {
  361. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  362. __pmc_fixed_disable(counter, hwc, idx);
  363. else
  364. hw_perf_disable(idx, hwc->config);
  365. }
  366. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  367. /*
  368. * Set the next IRQ period, based on the hwc->period_left value.
  369. * To be called with the counter disabled in hw:
  370. */
  371. static void
  372. __hw_perf_counter_set_period(struct perf_counter *counter,
  373. struct hw_perf_counter *hwc, int idx)
  374. {
  375. s64 left = atomic64_read(&hwc->period_left);
  376. s64 period = hwc->irq_period;
  377. int err;
  378. /*
  379. * If we are way outside a reasoable range then just skip forward:
  380. */
  381. if (unlikely(left <= -period)) {
  382. left = period;
  383. atomic64_set(&hwc->period_left, left);
  384. }
  385. if (unlikely(left <= 0)) {
  386. left += period;
  387. atomic64_set(&hwc->period_left, left);
  388. }
  389. per_cpu(prev_left[idx], smp_processor_id()) = left;
  390. /*
  391. * The hw counter starts counting from this counter offset,
  392. * mark it to be able to extra future deltas:
  393. */
  394. atomic64_set(&hwc->prev_count, (u64)-left);
  395. err = checking_wrmsrl(hwc->counter_base + idx,
  396. (u64)(-left) & counter_value_mask);
  397. }
  398. static inline void
  399. __pmc_fixed_enable(struct perf_counter *counter,
  400. struct hw_perf_counter *hwc, unsigned int __idx)
  401. {
  402. int idx = __idx - X86_PMC_IDX_FIXED;
  403. u64 ctrl_val, bits, mask;
  404. int err;
  405. /*
  406. * Enable IRQ generation (0x8),
  407. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  408. * if requested:
  409. */
  410. bits = 0x8ULL;
  411. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  412. bits |= 0x2;
  413. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  414. bits |= 0x1;
  415. bits <<= (idx * 4);
  416. mask = 0xfULL << (idx * 4);
  417. rdmsrl(hwc->config_base, ctrl_val);
  418. ctrl_val &= ~mask;
  419. ctrl_val |= bits;
  420. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  421. }
  422. static void
  423. __pmc_generic_enable(struct perf_counter *counter,
  424. struct hw_perf_counter *hwc, int idx)
  425. {
  426. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  427. __pmc_fixed_enable(counter, hwc, idx);
  428. else
  429. hw_perf_enable(idx, hwc->config);
  430. }
  431. static int
  432. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  433. {
  434. unsigned int event;
  435. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  436. return -1;
  437. if (unlikely(hwc->nmi))
  438. return -1;
  439. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  440. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
  441. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  442. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
  443. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  444. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
  445. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  446. return -1;
  447. }
  448. /*
  449. * Find a PMC slot for the freshly enabled / scheduled in counter:
  450. */
  451. static int pmc_generic_enable(struct perf_counter *counter)
  452. {
  453. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  454. struct hw_perf_counter *hwc = &counter->hw;
  455. int idx;
  456. idx = fixed_mode_idx(counter, hwc);
  457. if (idx >= 0) {
  458. /*
  459. * Try to get the fixed counter, if that is already taken
  460. * then try to get a generic counter:
  461. */
  462. if (test_and_set_bit(idx, cpuc->used))
  463. goto try_generic;
  464. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  465. /*
  466. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  467. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  468. */
  469. hwc->counter_base =
  470. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  471. hwc->idx = idx;
  472. } else {
  473. idx = hwc->idx;
  474. /* Try to get the previous generic counter again */
  475. if (test_and_set_bit(idx, cpuc->used)) {
  476. try_generic:
  477. idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
  478. if (idx == nr_counters_generic)
  479. return -EAGAIN;
  480. set_bit(idx, cpuc->used);
  481. hwc->idx = idx;
  482. }
  483. hwc->config_base = pmc_ops->eventsel;
  484. hwc->counter_base = pmc_ops->perfctr;
  485. }
  486. perf_counters_lapic_init(hwc->nmi);
  487. __pmc_generic_disable(counter, hwc, idx);
  488. cpuc->counters[idx] = counter;
  489. /*
  490. * Make it visible before enabling the hw:
  491. */
  492. smp_wmb();
  493. __hw_perf_counter_set_period(counter, hwc, idx);
  494. __pmc_generic_enable(counter, hwc, idx);
  495. return 0;
  496. }
  497. void perf_counter_print_debug(void)
  498. {
  499. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  500. struct cpu_hw_counters *cpuc;
  501. int cpu, idx;
  502. if (!nr_counters_generic)
  503. return;
  504. local_irq_disable();
  505. cpu = smp_processor_id();
  506. cpuc = &per_cpu(cpu_hw_counters, cpu);
  507. if (intel_perfmon_version >= 2) {
  508. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  509. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  510. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  511. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  512. pr_info("\n");
  513. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  514. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  515. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  516. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  517. }
  518. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  519. for (idx = 0; idx < nr_counters_generic; idx++) {
  520. rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
  521. rdmsrl(pmc_ops->perfctr + idx, pmc_count);
  522. prev_left = per_cpu(prev_left[idx], cpu);
  523. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  524. cpu, idx, pmc_ctrl);
  525. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  526. cpu, idx, pmc_count);
  527. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  528. cpu, idx, prev_left);
  529. }
  530. for (idx = 0; idx < nr_counters_fixed; idx++) {
  531. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  532. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  533. cpu, idx, pmc_count);
  534. }
  535. local_irq_enable();
  536. }
  537. static void pmc_generic_disable(struct perf_counter *counter)
  538. {
  539. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  540. struct hw_perf_counter *hwc = &counter->hw;
  541. unsigned int idx = hwc->idx;
  542. __pmc_generic_disable(counter, hwc, idx);
  543. clear_bit(idx, cpuc->used);
  544. cpuc->counters[idx] = NULL;
  545. /*
  546. * Make sure the cleared pointer becomes visible before we
  547. * (potentially) free the counter:
  548. */
  549. smp_wmb();
  550. /*
  551. * Drain the remaining delta count out of a counter
  552. * that we are disabling:
  553. */
  554. x86_perf_counter_update(counter, hwc, idx);
  555. }
  556. /*
  557. * Save and restart an expired counter. Called by NMI contexts,
  558. * so it has to be careful about preempting normal counter ops:
  559. */
  560. static void perf_save_and_restart(struct perf_counter *counter)
  561. {
  562. struct hw_perf_counter *hwc = &counter->hw;
  563. int idx = hwc->idx;
  564. x86_perf_counter_update(counter, hwc, idx);
  565. __hw_perf_counter_set_period(counter, hwc, idx);
  566. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  567. __pmc_generic_enable(counter, hwc, idx);
  568. }
  569. /*
  570. * Maximum interrupt frequency of 100KHz per CPU
  571. */
  572. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  573. /*
  574. * This handler is triggered by the local APIC, so the APIC IRQ handling
  575. * rules apply:
  576. */
  577. static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
  578. {
  579. int bit, cpu = smp_processor_id();
  580. u64 ack, status;
  581. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  582. int ret = 0;
  583. cpuc->throttle_ctrl = hw_perf_save_disable();
  584. status = hw_perf_get_status(cpuc->throttle_ctrl);
  585. if (!status)
  586. goto out;
  587. ret = 1;
  588. again:
  589. inc_irq_stat(apic_perf_irqs);
  590. ack = status;
  591. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  592. struct perf_counter *counter = cpuc->counters[bit];
  593. clear_bit(bit, (unsigned long *) &status);
  594. if (!counter)
  595. continue;
  596. perf_save_and_restart(counter);
  597. perf_counter_output(counter, nmi, regs);
  598. }
  599. hw_perf_ack_status(ack);
  600. /*
  601. * Repeat if there is more work to be done:
  602. */
  603. status = hw_perf_get_status(cpuc->throttle_ctrl);
  604. if (status)
  605. goto again;
  606. out:
  607. /*
  608. * Restore - do not reenable when global enable is off or throttled:
  609. */
  610. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
  611. hw_perf_restore(cpuc->throttle_ctrl);
  612. return ret;
  613. }
  614. void perf_counter_unthrottle(void)
  615. {
  616. struct cpu_hw_counters *cpuc;
  617. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  618. return;
  619. if (unlikely(!perf_counters_initialized))
  620. return;
  621. cpuc = &__get_cpu_var(cpu_hw_counters);
  622. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  623. if (printk_ratelimit())
  624. printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
  625. hw_perf_restore(cpuc->throttle_ctrl);
  626. }
  627. cpuc->interrupts = 0;
  628. }
  629. void smp_perf_counter_interrupt(struct pt_regs *regs)
  630. {
  631. irq_enter();
  632. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  633. ack_APIC_irq();
  634. __smp_perf_counter_interrupt(regs, 0);
  635. irq_exit();
  636. }
  637. void perf_counters_lapic_init(int nmi)
  638. {
  639. u32 apic_val;
  640. if (!perf_counters_initialized)
  641. return;
  642. /*
  643. * Enable the performance counter vector in the APIC LVT:
  644. */
  645. apic_val = apic_read(APIC_LVTERR);
  646. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  647. if (nmi)
  648. apic_write(APIC_LVTPC, APIC_DM_NMI);
  649. else
  650. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  651. apic_write(APIC_LVTERR, apic_val);
  652. }
  653. static int __kprobes
  654. perf_counter_nmi_handler(struct notifier_block *self,
  655. unsigned long cmd, void *__args)
  656. {
  657. struct die_args *args = __args;
  658. struct pt_regs *regs;
  659. int ret;
  660. switch (cmd) {
  661. case DIE_NMI:
  662. case DIE_NMI_IPI:
  663. break;
  664. default:
  665. return NOTIFY_DONE;
  666. }
  667. regs = args->regs;
  668. apic_write(APIC_LVTPC, APIC_DM_NMI);
  669. ret = __smp_perf_counter_interrupt(regs, 1);
  670. return ret ? NOTIFY_STOP : NOTIFY_OK;
  671. }
  672. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  673. .notifier_call = perf_counter_nmi_handler,
  674. .next = NULL,
  675. .priority = 1
  676. };
  677. static struct pmc_x86_ops pmc_intel_ops = {
  678. .save_disable_all = pmc_intel_save_disable_all,
  679. .restore_all = pmc_intel_restore_all,
  680. .get_status = pmc_intel_get_status,
  681. .ack_status = pmc_intel_ack_status,
  682. .enable = pmc_intel_enable,
  683. .disable = pmc_intel_disable,
  684. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  685. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  686. .event_map = pmc_intel_event_map,
  687. .raw_event = pmc_intel_raw_event,
  688. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  689. };
  690. static struct pmc_x86_ops pmc_amd_ops = {
  691. .save_disable_all = pmc_amd_save_disable_all,
  692. .restore_all = pmc_amd_restore_all,
  693. .get_status = pmc_amd_get_status,
  694. .ack_status = pmc_amd_ack_status,
  695. .enable = pmc_amd_enable,
  696. .disable = pmc_amd_disable,
  697. .eventsel = MSR_K7_EVNTSEL0,
  698. .perfctr = MSR_K7_PERFCTR0,
  699. .event_map = pmc_amd_event_map,
  700. .raw_event = pmc_amd_raw_event,
  701. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  702. };
  703. static struct pmc_x86_ops *pmc_intel_init(void)
  704. {
  705. union cpuid10_edx edx;
  706. union cpuid10_eax eax;
  707. unsigned int unused;
  708. unsigned int ebx;
  709. /*
  710. * Check whether the Architectural PerfMon supports
  711. * Branch Misses Retired Event or not.
  712. */
  713. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  714. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  715. return NULL;
  716. intel_perfmon_version = eax.split.version_id;
  717. if (intel_perfmon_version < 2)
  718. return NULL;
  719. pr_info("Intel Performance Monitoring support detected.\n");
  720. pr_info("... version: %d\n", intel_perfmon_version);
  721. pr_info("... bit width: %d\n", eax.split.bit_width);
  722. pr_info("... mask length: %d\n", eax.split.mask_length);
  723. nr_counters_generic = eax.split.num_counters;
  724. nr_counters_fixed = edx.split.num_counters_fixed;
  725. counter_value_mask = (1ULL << eax.split.bit_width) - 1;
  726. return &pmc_intel_ops;
  727. }
  728. static struct pmc_x86_ops *pmc_amd_init(void)
  729. {
  730. nr_counters_generic = 4;
  731. nr_counters_fixed = 0;
  732. counter_value_mask = 0x0000FFFFFFFFFFFFULL;
  733. counter_value_bits = 48;
  734. pr_info("AMD Performance Monitoring support detected.\n");
  735. return &pmc_amd_ops;
  736. }
  737. void __init init_hw_perf_counters(void)
  738. {
  739. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  740. return;
  741. switch (boot_cpu_data.x86_vendor) {
  742. case X86_VENDOR_INTEL:
  743. pmc_ops = pmc_intel_init();
  744. break;
  745. case X86_VENDOR_AMD:
  746. pmc_ops = pmc_amd_init();
  747. break;
  748. }
  749. if (!pmc_ops)
  750. return;
  751. pr_info("... num counters: %d\n", nr_counters_generic);
  752. if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
  753. nr_counters_generic = X86_PMC_MAX_GENERIC;
  754. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  755. nr_counters_generic, X86_PMC_MAX_GENERIC);
  756. }
  757. perf_counter_mask = (1 << nr_counters_generic) - 1;
  758. perf_max_counters = nr_counters_generic;
  759. pr_info("... value mask: %016Lx\n", counter_value_mask);
  760. if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
  761. nr_counters_fixed = X86_PMC_MAX_FIXED;
  762. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  763. nr_counters_fixed, X86_PMC_MAX_FIXED);
  764. }
  765. pr_info("... fixed counters: %d\n", nr_counters_fixed);
  766. perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  767. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  768. perf_counters_initialized = true;
  769. perf_counters_lapic_init(0);
  770. register_die_notifier(&perf_counter_nmi_notifier);
  771. }
  772. static void pmc_generic_read(struct perf_counter *counter)
  773. {
  774. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  775. }
  776. static const struct hw_perf_counter_ops x86_perf_counter_ops = {
  777. .enable = pmc_generic_enable,
  778. .disable = pmc_generic_disable,
  779. .read = pmc_generic_read,
  780. };
  781. const struct hw_perf_counter_ops *
  782. hw_perf_counter_init(struct perf_counter *counter)
  783. {
  784. int err;
  785. err = __hw_perf_counter_init(counter);
  786. if (err)
  787. return ERR_PTR(err);
  788. return &x86_perf_counter_ops;
  789. }
  790. /*
  791. * callchain support
  792. */
  793. static inline
  794. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  795. {
  796. if (entry->nr < MAX_STACK_DEPTH)
  797. entry->ip[entry->nr++] = ip;
  798. }
  799. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  800. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  801. static void
  802. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  803. {
  804. /* Ignore warnings */
  805. }
  806. static void backtrace_warning(void *data, char *msg)
  807. {
  808. /* Ignore warnings */
  809. }
  810. static int backtrace_stack(void *data, char *name)
  811. {
  812. /* Don't bother with IRQ stacks for now */
  813. return -1;
  814. }
  815. static void backtrace_address(void *data, unsigned long addr, int reliable)
  816. {
  817. struct perf_callchain_entry *entry = data;
  818. if (reliable)
  819. callchain_store(entry, addr);
  820. }
  821. static const struct stacktrace_ops backtrace_ops = {
  822. .warning = backtrace_warning,
  823. .warning_symbol = backtrace_warning_symbol,
  824. .stack = backtrace_stack,
  825. .address = backtrace_address,
  826. };
  827. static void
  828. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  829. {
  830. unsigned long bp;
  831. char *stack;
  832. callchain_store(entry, instruction_pointer(regs));
  833. stack = ((char *)regs + sizeof(struct pt_regs));
  834. #ifdef CONFIG_FRAME_POINTER
  835. bp = frame_pointer(regs);
  836. #else
  837. bp = 0;
  838. #endif
  839. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  840. }
  841. struct stack_frame {
  842. const void __user *next_fp;
  843. unsigned long return_address;
  844. };
  845. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  846. {
  847. int ret;
  848. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  849. return 0;
  850. ret = 1;
  851. pagefault_disable();
  852. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  853. ret = 0;
  854. pagefault_enable();
  855. return ret;
  856. }
  857. static void
  858. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  859. {
  860. struct stack_frame frame;
  861. const void __user *fp;
  862. regs = (struct pt_regs *)current->thread.sp0 - 1;
  863. fp = (void __user *)regs->bp;
  864. callchain_store(entry, regs->ip);
  865. while (entry->nr < MAX_STACK_DEPTH) {
  866. frame.next_fp = NULL;
  867. frame.return_address = 0;
  868. if (!copy_stack_frame(fp, &frame))
  869. break;
  870. if ((unsigned long)fp < user_stack_pointer(regs))
  871. break;
  872. callchain_store(entry, frame.return_address);
  873. fp = frame.next_fp;
  874. }
  875. }
  876. static void
  877. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  878. {
  879. int is_user;
  880. if (!regs)
  881. return;
  882. is_user = user_mode(regs);
  883. if (!current || current->pid == 0)
  884. return;
  885. if (is_user && current->state != TASK_RUNNING)
  886. return;
  887. if (!is_user)
  888. perf_callchain_kernel(regs, entry);
  889. if (current->mm)
  890. perf_callchain_user(regs, entry);
  891. }
  892. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  893. {
  894. struct perf_callchain_entry *entry;
  895. if (in_nmi())
  896. entry = &__get_cpu_var(nmi_entry);
  897. else
  898. entry = &__get_cpu_var(irq_entry);
  899. entry->nr = 0;
  900. perf_do_callchain(regs, entry);
  901. return entry;
  902. }