perf_counter.c 27 KB

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