perf_counter.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. u64 throttle_ctrl;
  31. int enabled;
  32. };
  33. /*
  34. * struct x86_pmu - generic x86 pmu
  35. */
  36. struct x86_pmu {
  37. const char *name;
  38. int version;
  39. int (*handle_irq)(struct pt_regs *, int);
  40. u64 (*save_disable_all)(void);
  41. void (*restore_all)(u64);
  42. void (*enable)(struct hw_perf_counter *, int);
  43. void (*disable)(struct hw_perf_counter *, int);
  44. unsigned eventsel;
  45. unsigned perfctr;
  46. u64 (*event_map)(int);
  47. u64 (*raw_event)(u64);
  48. int max_events;
  49. int num_counters;
  50. int num_counters_fixed;
  51. int counter_bits;
  52. u64 counter_mask;
  53. u64 max_period;
  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. hwc->irq_period = hw_event->irq_period;
  244. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > x86_pmu.max_period)
  245. hwc->irq_period = x86_pmu.max_period;
  246. atomic64_set(&hwc->period_left, hwc->irq_period);
  247. /*
  248. * Raw event type provide the config in the event structure
  249. */
  250. if (perf_event_raw(hw_event)) {
  251. hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
  252. } else {
  253. if (perf_event_id(hw_event) >= x86_pmu.max_events)
  254. return -EINVAL;
  255. /*
  256. * The generic map:
  257. */
  258. hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
  259. }
  260. counter->destroy = hw_perf_counter_destroy;
  261. return 0;
  262. }
  263. static u64 intel_pmu_save_disable_all(void)
  264. {
  265. u64 ctrl;
  266. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  267. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  268. return ctrl;
  269. }
  270. static u64 amd_pmu_save_disable_all(void)
  271. {
  272. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  273. int enabled, idx;
  274. enabled = cpuc->enabled;
  275. cpuc->enabled = 0;
  276. /*
  277. * ensure we write the disable before we start disabling the
  278. * counters proper, so that amd_pmu_enable_counter() does the
  279. * right thing.
  280. */
  281. barrier();
  282. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  283. u64 val;
  284. if (!test_bit(idx, cpuc->active_mask))
  285. continue;
  286. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  287. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  288. continue;
  289. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  290. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  291. }
  292. return enabled;
  293. }
  294. u64 hw_perf_save_disable(void)
  295. {
  296. if (!x86_pmu_initialized())
  297. return 0;
  298. return x86_pmu.save_disable_all();
  299. }
  300. /*
  301. * Exported because of ACPI idle
  302. */
  303. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  304. static void intel_pmu_restore_all(u64 ctrl)
  305. {
  306. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  307. }
  308. static void amd_pmu_restore_all(u64 ctrl)
  309. {
  310. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  311. int idx;
  312. cpuc->enabled = ctrl;
  313. barrier();
  314. if (!ctrl)
  315. return;
  316. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  317. u64 val;
  318. if (!test_bit(idx, cpuc->active_mask))
  319. continue;
  320. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  321. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  322. continue;
  323. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  324. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  325. }
  326. }
  327. void hw_perf_restore(u64 ctrl)
  328. {
  329. if (!x86_pmu_initialized())
  330. return;
  331. x86_pmu.restore_all(ctrl);
  332. }
  333. /*
  334. * Exported because of ACPI idle
  335. */
  336. EXPORT_SYMBOL_GPL(hw_perf_restore);
  337. static inline u64 intel_pmu_get_status(void)
  338. {
  339. u64 status;
  340. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  341. return status;
  342. }
  343. static inline void intel_pmu_ack_status(u64 ack)
  344. {
  345. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  346. }
  347. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  348. {
  349. int err;
  350. err = checking_wrmsrl(hwc->config_base + idx,
  351. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  352. }
  353. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  354. {
  355. int err;
  356. err = checking_wrmsrl(hwc->config_base + idx,
  357. hwc->config);
  358. }
  359. static inline void
  360. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  361. {
  362. int idx = __idx - X86_PMC_IDX_FIXED;
  363. u64 ctrl_val, mask;
  364. int err;
  365. mask = 0xfULL << (idx * 4);
  366. rdmsrl(hwc->config_base, ctrl_val);
  367. ctrl_val &= ~mask;
  368. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  369. }
  370. static inline void
  371. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  372. {
  373. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  374. intel_pmu_disable_fixed(hwc, idx);
  375. return;
  376. }
  377. x86_pmu_disable_counter(hwc, idx);
  378. }
  379. static inline void
  380. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  381. {
  382. x86_pmu_disable_counter(hwc, idx);
  383. }
  384. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  385. /*
  386. * Set the next IRQ period, based on the hwc->period_left value.
  387. * To be called with the counter disabled in hw:
  388. */
  389. static void
  390. x86_perf_counter_set_period(struct perf_counter *counter,
  391. struct hw_perf_counter *hwc, int idx)
  392. {
  393. s64 left = atomic64_read(&hwc->period_left);
  394. s64 period = hwc->irq_period;
  395. int err;
  396. /*
  397. * If we are way outside a reasoable range then just skip forward:
  398. */
  399. if (unlikely(left <= -period)) {
  400. left = period;
  401. atomic64_set(&hwc->period_left, left);
  402. }
  403. if (unlikely(left <= 0)) {
  404. left += period;
  405. atomic64_set(&hwc->period_left, left);
  406. }
  407. per_cpu(prev_left[idx], smp_processor_id()) = left;
  408. /*
  409. * The hw counter starts counting from this counter offset,
  410. * mark it to be able to extra future deltas:
  411. */
  412. atomic64_set(&hwc->prev_count, (u64)-left);
  413. err = checking_wrmsrl(hwc->counter_base + idx,
  414. (u64)(-left) & x86_pmu.counter_mask);
  415. }
  416. static inline void
  417. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  418. {
  419. int idx = __idx - X86_PMC_IDX_FIXED;
  420. u64 ctrl_val, bits, mask;
  421. int err;
  422. /*
  423. * Enable IRQ generation (0x8),
  424. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  425. * if requested:
  426. */
  427. bits = 0x8ULL;
  428. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  429. bits |= 0x2;
  430. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  431. bits |= 0x1;
  432. bits <<= (idx * 4);
  433. mask = 0xfULL << (idx * 4);
  434. rdmsrl(hwc->config_base, ctrl_val);
  435. ctrl_val &= ~mask;
  436. ctrl_val |= bits;
  437. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  438. }
  439. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  440. {
  441. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  442. intel_pmu_enable_fixed(hwc, idx);
  443. return;
  444. }
  445. x86_pmu_enable_counter(hwc, idx);
  446. }
  447. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  448. {
  449. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  450. if (cpuc->enabled)
  451. x86_pmu_enable_counter(hwc, idx);
  452. else
  453. x86_pmu_disable_counter(hwc, idx);
  454. }
  455. static int
  456. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  457. {
  458. unsigned int event;
  459. if (!x86_pmu.num_counters_fixed)
  460. return -1;
  461. if (unlikely(hwc->nmi))
  462. return -1;
  463. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  464. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  465. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  466. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  467. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  468. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  469. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  470. return -1;
  471. }
  472. /*
  473. * Find a PMC slot for the freshly enabled / scheduled in counter:
  474. */
  475. static int x86_pmu_enable(struct perf_counter *counter)
  476. {
  477. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  478. struct hw_perf_counter *hwc = &counter->hw;
  479. int idx;
  480. idx = fixed_mode_idx(counter, hwc);
  481. if (idx >= 0) {
  482. /*
  483. * Try to get the fixed counter, if that is already taken
  484. * then try to get a generic counter:
  485. */
  486. if (test_and_set_bit(idx, cpuc->used_mask))
  487. goto try_generic;
  488. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  489. /*
  490. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  491. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  492. */
  493. hwc->counter_base =
  494. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  495. hwc->idx = idx;
  496. } else {
  497. idx = hwc->idx;
  498. /* Try to get the previous generic counter again */
  499. if (test_and_set_bit(idx, cpuc->used_mask)) {
  500. try_generic:
  501. idx = find_first_zero_bit(cpuc->used_mask,
  502. x86_pmu.num_counters);
  503. if (idx == x86_pmu.num_counters)
  504. return -EAGAIN;
  505. set_bit(idx, cpuc->used_mask);
  506. hwc->idx = idx;
  507. }
  508. hwc->config_base = x86_pmu.eventsel;
  509. hwc->counter_base = x86_pmu.perfctr;
  510. }
  511. perf_counters_lapic_init(hwc->nmi);
  512. x86_pmu.disable(hwc, idx);
  513. cpuc->counters[idx] = counter;
  514. set_bit(idx, cpuc->active_mask);
  515. x86_perf_counter_set_period(counter, hwc, idx);
  516. x86_pmu.enable(hwc, idx);
  517. return 0;
  518. }
  519. void perf_counter_print_debug(void)
  520. {
  521. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  522. struct cpu_hw_counters *cpuc;
  523. unsigned long flags;
  524. int cpu, idx;
  525. if (!x86_pmu.num_counters)
  526. return;
  527. local_irq_save(flags);
  528. cpu = smp_processor_id();
  529. cpuc = &per_cpu(cpu_hw_counters, cpu);
  530. if (x86_pmu.version >= 2) {
  531. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  532. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  533. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  534. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  535. pr_info("\n");
  536. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  537. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  538. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  539. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  540. }
  541. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  542. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  543. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  544. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  545. prev_left = per_cpu(prev_left[idx], cpu);
  546. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  547. cpu, idx, pmc_ctrl);
  548. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  549. cpu, idx, pmc_count);
  550. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  551. cpu, idx, prev_left);
  552. }
  553. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  554. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  555. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  556. cpu, idx, pmc_count);
  557. }
  558. local_irq_restore(flags);
  559. }
  560. static void x86_pmu_disable(struct perf_counter *counter)
  561. {
  562. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  563. struct hw_perf_counter *hwc = &counter->hw;
  564. int idx = hwc->idx;
  565. /*
  566. * Must be done before we disable, otherwise the nmi handler
  567. * could reenable again:
  568. */
  569. clear_bit(idx, cpuc->active_mask);
  570. x86_pmu.disable(hwc, idx);
  571. /*
  572. * Make sure the cleared pointer becomes visible before we
  573. * (potentially) free the counter:
  574. */
  575. barrier();
  576. /*
  577. * Drain the remaining delta count out of a counter
  578. * that we are disabling:
  579. */
  580. x86_perf_counter_update(counter, hwc, idx);
  581. cpuc->counters[idx] = NULL;
  582. clear_bit(idx, cpuc->used_mask);
  583. }
  584. /*
  585. * Save and restart an expired counter. Called by NMI contexts,
  586. * so it has to be careful about preempting normal counter ops:
  587. */
  588. static void intel_pmu_save_and_restart(struct perf_counter *counter)
  589. {
  590. struct hw_perf_counter *hwc = &counter->hw;
  591. int idx = hwc->idx;
  592. x86_perf_counter_update(counter, hwc, idx);
  593. x86_perf_counter_set_period(counter, hwc, idx);
  594. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  595. intel_pmu_enable_counter(hwc, idx);
  596. }
  597. /*
  598. * Maximum interrupt frequency of 100KHz per CPU
  599. */
  600. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  601. /*
  602. * This handler is triggered by the local APIC, so the APIC IRQ handling
  603. * rules apply:
  604. */
  605. static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
  606. {
  607. int bit, cpu = smp_processor_id();
  608. u64 ack, status;
  609. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  610. int ret = 0;
  611. cpuc->throttle_ctrl = intel_pmu_save_disable_all();
  612. status = intel_pmu_get_status();
  613. if (!status)
  614. goto out;
  615. ret = 1;
  616. again:
  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. out:
  636. /*
  637. * Restore - do not reenable when global enable is off or throttled:
  638. */
  639. if (cpuc->throttle_ctrl) {
  640. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS) {
  641. intel_pmu_restore_all(cpuc->throttle_ctrl);
  642. } else {
  643. pr_info("CPU#%d: perfcounters: max interrupt rate exceeded! Throttle on.\n", smp_processor_id());
  644. }
  645. }
  646. return ret;
  647. }
  648. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
  649. {
  650. int cpu = smp_processor_id();
  651. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  652. u64 val;
  653. int handled = 0;
  654. struct perf_counter *counter;
  655. struct hw_perf_counter *hwc;
  656. int idx;
  657. ++cpuc->interrupts;
  658. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  659. if (!test_bit(idx, cpuc->active_mask))
  660. continue;
  661. counter = cpuc->counters[idx];
  662. hwc = &counter->hw;
  663. val = x86_perf_counter_update(counter, hwc, idx);
  664. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  665. continue;
  666. /* counter overflow */
  667. x86_perf_counter_set_period(counter, hwc, idx);
  668. handled = 1;
  669. inc_irq_stat(apic_perf_irqs);
  670. if (perf_counter_overflow(counter, nmi, regs, 0))
  671. amd_pmu_disable_counter(hwc, idx);
  672. else if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS)
  673. /*
  674. * do not reenable when throttled, but reload
  675. * the register
  676. */
  677. amd_pmu_disable_counter(hwc, idx);
  678. else if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  679. amd_pmu_enable_counter(hwc, idx);
  680. }
  681. return handled;
  682. }
  683. void perf_counter_unthrottle(void)
  684. {
  685. struct cpu_hw_counters *cpuc;
  686. if (!x86_pmu_initialized())
  687. return;
  688. cpuc = &__get_cpu_var(cpu_hw_counters);
  689. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  690. pr_info("CPU#%d: perfcounters: throttle off.\n", smp_processor_id());
  691. /*
  692. * Clear them before re-enabling irqs/NMIs again:
  693. */
  694. cpuc->interrupts = 0;
  695. hw_perf_restore(cpuc->throttle_ctrl);
  696. } else {
  697. cpuc->interrupts = 0;
  698. }
  699. }
  700. void smp_perf_counter_interrupt(struct pt_regs *regs)
  701. {
  702. irq_enter();
  703. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  704. ack_APIC_irq();
  705. x86_pmu.handle_irq(regs, 0);
  706. irq_exit();
  707. }
  708. void smp_perf_pending_interrupt(struct pt_regs *regs)
  709. {
  710. irq_enter();
  711. ack_APIC_irq();
  712. inc_irq_stat(apic_pending_irqs);
  713. perf_counter_do_pending();
  714. irq_exit();
  715. }
  716. void set_perf_counter_pending(void)
  717. {
  718. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  719. }
  720. void perf_counters_lapic_init(int nmi)
  721. {
  722. u32 apic_val;
  723. if (!x86_pmu_initialized())
  724. return;
  725. /*
  726. * Enable the performance counter vector in the APIC LVT:
  727. */
  728. apic_val = apic_read(APIC_LVTERR);
  729. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  730. if (nmi)
  731. apic_write(APIC_LVTPC, APIC_DM_NMI);
  732. else
  733. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  734. apic_write(APIC_LVTERR, apic_val);
  735. }
  736. static int __kprobes
  737. perf_counter_nmi_handler(struct notifier_block *self,
  738. unsigned long cmd, void *__args)
  739. {
  740. struct die_args *args = __args;
  741. struct pt_regs *regs;
  742. int ret;
  743. if (!atomic_read(&active_counters))
  744. return NOTIFY_DONE;
  745. switch (cmd) {
  746. case DIE_NMI:
  747. case DIE_NMI_IPI:
  748. break;
  749. default:
  750. return NOTIFY_DONE;
  751. }
  752. regs = args->regs;
  753. apic_write(APIC_LVTPC, APIC_DM_NMI);
  754. ret = x86_pmu.handle_irq(regs, 1);
  755. return ret ? NOTIFY_STOP : NOTIFY_OK;
  756. }
  757. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  758. .notifier_call = perf_counter_nmi_handler,
  759. .next = NULL,
  760. .priority = 1
  761. };
  762. static struct x86_pmu intel_pmu = {
  763. .name = "Intel",
  764. .handle_irq = intel_pmu_handle_irq,
  765. .save_disable_all = intel_pmu_save_disable_all,
  766. .restore_all = intel_pmu_restore_all,
  767. .enable = intel_pmu_enable_counter,
  768. .disable = intel_pmu_disable_counter,
  769. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  770. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  771. .event_map = intel_pmu_event_map,
  772. .raw_event = intel_pmu_raw_event,
  773. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  774. /*
  775. * Intel PMCs cannot be accessed sanely above 32 bit width,
  776. * so we install an artificial 1<<31 period regardless of
  777. * the generic counter period:
  778. */
  779. .max_period = (1ULL << 31) - 1,
  780. };
  781. static struct x86_pmu amd_pmu = {
  782. .name = "AMD",
  783. .handle_irq = amd_pmu_handle_irq,
  784. .save_disable_all = amd_pmu_save_disable_all,
  785. .restore_all = amd_pmu_restore_all,
  786. .enable = amd_pmu_enable_counter,
  787. .disable = amd_pmu_disable_counter,
  788. .eventsel = MSR_K7_EVNTSEL0,
  789. .perfctr = MSR_K7_PERFCTR0,
  790. .event_map = amd_pmu_event_map,
  791. .raw_event = amd_pmu_raw_event,
  792. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  793. .num_counters = 4,
  794. .counter_bits = 48,
  795. .counter_mask = (1ULL << 48) - 1,
  796. /* use highest bit to detect overflow */
  797. .max_period = (1ULL << 47) - 1,
  798. };
  799. static int intel_pmu_init(void)
  800. {
  801. union cpuid10_edx edx;
  802. union cpuid10_eax eax;
  803. unsigned int unused;
  804. unsigned int ebx;
  805. int version;
  806. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  807. return -ENODEV;
  808. /*
  809. * Check whether the Architectural PerfMon supports
  810. * Branch Misses Retired Event or not.
  811. */
  812. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  813. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  814. return -ENODEV;
  815. version = eax.split.version_id;
  816. if (version < 2)
  817. return -ENODEV;
  818. x86_pmu = intel_pmu;
  819. x86_pmu.version = version;
  820. x86_pmu.num_counters = eax.split.num_counters;
  821. /*
  822. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  823. * assume at least 3 counters:
  824. */
  825. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  826. x86_pmu.counter_bits = eax.split.bit_width;
  827. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  828. return 0;
  829. }
  830. static int amd_pmu_init(void)
  831. {
  832. x86_pmu = amd_pmu;
  833. return 0;
  834. }
  835. void __init init_hw_perf_counters(void)
  836. {
  837. int err;
  838. switch (boot_cpu_data.x86_vendor) {
  839. case X86_VENDOR_INTEL:
  840. err = intel_pmu_init();
  841. break;
  842. case X86_VENDOR_AMD:
  843. err = amd_pmu_init();
  844. break;
  845. default:
  846. return;
  847. }
  848. if (err != 0)
  849. return;
  850. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  851. pr_info("... version: %d\n", x86_pmu.version);
  852. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  853. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  854. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  855. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  856. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  857. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  858. }
  859. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  860. perf_max_counters = x86_pmu.num_counters;
  861. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  862. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  863. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  864. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  865. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  866. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  867. }
  868. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  869. perf_counter_mask |=
  870. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  871. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  872. perf_counters_lapic_init(0);
  873. register_die_notifier(&perf_counter_nmi_notifier);
  874. }
  875. static inline void x86_pmu_read(struct perf_counter *counter)
  876. {
  877. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  878. }
  879. static const struct pmu pmu = {
  880. .enable = x86_pmu_enable,
  881. .disable = x86_pmu_disable,
  882. .read = x86_pmu_read,
  883. };
  884. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  885. {
  886. int err;
  887. err = __hw_perf_counter_init(counter);
  888. if (err)
  889. return ERR_PTR(err);
  890. return &pmu;
  891. }
  892. /*
  893. * callchain support
  894. */
  895. static inline
  896. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  897. {
  898. if (entry->nr < MAX_STACK_DEPTH)
  899. entry->ip[entry->nr++] = ip;
  900. }
  901. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  902. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  903. static void
  904. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  905. {
  906. /* Ignore warnings */
  907. }
  908. static void backtrace_warning(void *data, char *msg)
  909. {
  910. /* Ignore warnings */
  911. }
  912. static int backtrace_stack(void *data, char *name)
  913. {
  914. /* Don't bother with IRQ stacks for now */
  915. return -1;
  916. }
  917. static void backtrace_address(void *data, unsigned long addr, int reliable)
  918. {
  919. struct perf_callchain_entry *entry = data;
  920. if (reliable)
  921. callchain_store(entry, addr);
  922. }
  923. static const struct stacktrace_ops backtrace_ops = {
  924. .warning = backtrace_warning,
  925. .warning_symbol = backtrace_warning_symbol,
  926. .stack = backtrace_stack,
  927. .address = backtrace_address,
  928. };
  929. static void
  930. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  931. {
  932. unsigned long bp;
  933. char *stack;
  934. int nr = entry->nr;
  935. callchain_store(entry, instruction_pointer(regs));
  936. stack = ((char *)regs + sizeof(struct pt_regs));
  937. #ifdef CONFIG_FRAME_POINTER
  938. bp = frame_pointer(regs);
  939. #else
  940. bp = 0;
  941. #endif
  942. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  943. entry->kernel = entry->nr - nr;
  944. }
  945. struct stack_frame {
  946. const void __user *next_fp;
  947. unsigned long return_address;
  948. };
  949. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  950. {
  951. int ret;
  952. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  953. return 0;
  954. ret = 1;
  955. pagefault_disable();
  956. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  957. ret = 0;
  958. pagefault_enable();
  959. return ret;
  960. }
  961. static void
  962. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  963. {
  964. struct stack_frame frame;
  965. const void __user *fp;
  966. int nr = entry->nr;
  967. regs = (struct pt_regs *)current->thread.sp0 - 1;
  968. fp = (void __user *)regs->bp;
  969. callchain_store(entry, regs->ip);
  970. while (entry->nr < MAX_STACK_DEPTH) {
  971. frame.next_fp = NULL;
  972. frame.return_address = 0;
  973. if (!copy_stack_frame(fp, &frame))
  974. break;
  975. if ((unsigned long)fp < user_stack_pointer(regs))
  976. break;
  977. callchain_store(entry, frame.return_address);
  978. fp = frame.next_fp;
  979. }
  980. entry->user = entry->nr - nr;
  981. }
  982. static void
  983. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  984. {
  985. int is_user;
  986. if (!regs)
  987. return;
  988. is_user = user_mode(regs);
  989. if (!current || current->pid == 0)
  990. return;
  991. if (is_user && current->state != TASK_RUNNING)
  992. return;
  993. if (!is_user)
  994. perf_callchain_kernel(regs, entry);
  995. if (current->mm)
  996. perf_callchain_user(regs, entry);
  997. }
  998. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  999. {
  1000. struct perf_callchain_entry *entry;
  1001. if (in_nmi())
  1002. entry = &__get_cpu_var(nmi_entry);
  1003. else
  1004. entry = &__get_cpu_var(irq_entry);
  1005. entry->nr = 0;
  1006. entry->hv = 0;
  1007. entry->kernel = 0;
  1008. entry->user = 0;
  1009. perf_do_callchain(regs, entry);
  1010. return entry;
  1011. }