perf_counter.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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 interrupts;
  29. u64 throttle_ctrl;
  30. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  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)(int, u64);
  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_mask))
  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_mask))
  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 void intel_pmu_enable_counter(int idx, u64 config)
  347. {
  348. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
  349. config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  350. }
  351. static void amd_pmu_enable_counter(int idx, u64 config)
  352. {
  353. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  354. set_bit(idx, cpuc->active_mask);
  355. if (cpuc->enabled)
  356. config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  357. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  358. }
  359. static void hw_perf_enable(int idx, u64 config)
  360. {
  361. if (unlikely(!perf_counters_initialized))
  362. return;
  363. x86_pmu.enable(idx, config);
  364. }
  365. static void intel_pmu_disable_counter(int idx, u64 config)
  366. {
  367. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
  368. }
  369. static void amd_pmu_disable_counter(int idx, u64 config)
  370. {
  371. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  372. clear_bit(idx, cpuc->active_mask);
  373. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  374. }
  375. static void hw_perf_disable(int idx, u64 config)
  376. {
  377. if (unlikely(!perf_counters_initialized))
  378. return;
  379. x86_pmu.disable(idx, config);
  380. }
  381. static inline void
  382. __pmc_fixed_disable(struct perf_counter *counter,
  383. struct hw_perf_counter *hwc, unsigned int __idx)
  384. {
  385. int idx = __idx - X86_PMC_IDX_FIXED;
  386. u64 ctrl_val, mask;
  387. int err;
  388. mask = 0xfULL << (idx * 4);
  389. rdmsrl(hwc->config_base, ctrl_val);
  390. ctrl_val &= ~mask;
  391. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  392. }
  393. static inline void
  394. __x86_pmu_disable(struct perf_counter *counter,
  395. struct hw_perf_counter *hwc, unsigned int idx)
  396. {
  397. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  398. __pmc_fixed_disable(counter, hwc, idx);
  399. else
  400. hw_perf_disable(idx, hwc->config);
  401. }
  402. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  403. /*
  404. * Set the next IRQ period, based on the hwc->period_left value.
  405. * To be called with the counter disabled in hw:
  406. */
  407. static void
  408. x86_perf_counter_set_period(struct perf_counter *counter,
  409. struct hw_perf_counter *hwc, int idx)
  410. {
  411. s64 left = atomic64_read(&hwc->period_left);
  412. s64 period = hwc->irq_period;
  413. int err;
  414. /*
  415. * If we are way outside a reasoable range then just skip forward:
  416. */
  417. if (unlikely(left <= -period)) {
  418. left = period;
  419. atomic64_set(&hwc->period_left, left);
  420. }
  421. if (unlikely(left <= 0)) {
  422. left += period;
  423. atomic64_set(&hwc->period_left, left);
  424. }
  425. per_cpu(prev_left[idx], smp_processor_id()) = left;
  426. /*
  427. * The hw counter starts counting from this counter offset,
  428. * mark it to be able to extra future deltas:
  429. */
  430. atomic64_set(&hwc->prev_count, (u64)-left);
  431. err = checking_wrmsrl(hwc->counter_base + idx,
  432. (u64)(-left) & x86_pmu.counter_mask);
  433. }
  434. static inline void
  435. __pmc_fixed_enable(struct perf_counter *counter,
  436. struct hw_perf_counter *hwc, unsigned int __idx)
  437. {
  438. int idx = __idx - X86_PMC_IDX_FIXED;
  439. u64 ctrl_val, bits, mask;
  440. int err;
  441. /*
  442. * Enable IRQ generation (0x8),
  443. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  444. * if requested:
  445. */
  446. bits = 0x8ULL;
  447. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  448. bits |= 0x2;
  449. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  450. bits |= 0x1;
  451. bits <<= (idx * 4);
  452. mask = 0xfULL << (idx * 4);
  453. rdmsrl(hwc->config_base, ctrl_val);
  454. ctrl_val &= ~mask;
  455. ctrl_val |= bits;
  456. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  457. }
  458. static void
  459. __x86_pmu_enable(struct perf_counter *counter,
  460. struct hw_perf_counter *hwc, int idx)
  461. {
  462. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  463. __pmc_fixed_enable(counter, hwc, idx);
  464. else
  465. hw_perf_enable(idx, hwc->config);
  466. }
  467. static int
  468. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  469. {
  470. unsigned int event;
  471. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  472. return -1;
  473. if (unlikely(hwc->nmi))
  474. return -1;
  475. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  476. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  477. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  478. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  479. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  480. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  481. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  482. return -1;
  483. }
  484. /*
  485. * Find a PMC slot for the freshly enabled / scheduled in counter:
  486. */
  487. static int x86_pmu_enable(struct perf_counter *counter)
  488. {
  489. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  490. struct hw_perf_counter *hwc = &counter->hw;
  491. int idx;
  492. idx = fixed_mode_idx(counter, hwc);
  493. if (idx >= 0) {
  494. /*
  495. * Try to get the fixed counter, if that is already taken
  496. * then try to get a generic counter:
  497. */
  498. if (test_and_set_bit(idx, cpuc->used))
  499. goto try_generic;
  500. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  501. /*
  502. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  503. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  504. */
  505. hwc->counter_base =
  506. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  507. hwc->idx = idx;
  508. } else {
  509. idx = hwc->idx;
  510. /* Try to get the previous generic counter again */
  511. if (test_and_set_bit(idx, cpuc->used)) {
  512. try_generic:
  513. idx = find_first_zero_bit(cpuc->used,
  514. x86_pmu.num_counters);
  515. if (idx == x86_pmu.num_counters)
  516. return -EAGAIN;
  517. set_bit(idx, cpuc->used);
  518. hwc->idx = idx;
  519. }
  520. hwc->config_base = x86_pmu.eventsel;
  521. hwc->counter_base = x86_pmu.perfctr;
  522. }
  523. perf_counters_lapic_init(hwc->nmi);
  524. __x86_pmu_disable(counter, hwc, idx);
  525. cpuc->counters[idx] = counter;
  526. /*
  527. * Make it visible before enabling the hw:
  528. */
  529. barrier();
  530. x86_perf_counter_set_period(counter, hwc, idx);
  531. __x86_pmu_enable(counter, hwc, idx);
  532. return 0;
  533. }
  534. void perf_counter_print_debug(void)
  535. {
  536. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  537. struct cpu_hw_counters *cpuc;
  538. int cpu, idx;
  539. if (!x86_pmu.num_counters)
  540. return;
  541. local_irq_disable();
  542. cpu = smp_processor_id();
  543. cpuc = &per_cpu(cpu_hw_counters, cpu);
  544. if (x86_pmu.version >= 2) {
  545. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  546. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  547. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  548. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  549. pr_info("\n");
  550. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  551. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  552. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  553. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  554. }
  555. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  556. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  557. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  558. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  559. prev_left = per_cpu(prev_left[idx], cpu);
  560. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  561. cpu, idx, pmc_ctrl);
  562. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  563. cpu, idx, pmc_count);
  564. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  565. cpu, idx, prev_left);
  566. }
  567. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  568. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  569. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  570. cpu, idx, pmc_count);
  571. }
  572. local_irq_enable();
  573. }
  574. static void x86_pmu_disable(struct perf_counter *counter)
  575. {
  576. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  577. struct hw_perf_counter *hwc = &counter->hw;
  578. unsigned int idx = hwc->idx;
  579. __x86_pmu_disable(counter, hwc, idx);
  580. clear_bit(idx, cpuc->used);
  581. cpuc->counters[idx] = NULL;
  582. /*
  583. * Make sure the cleared pointer becomes visible before we
  584. * (potentially) free the counter:
  585. */
  586. barrier();
  587. /*
  588. * Drain the remaining delta count out of a counter
  589. * that we are disabling:
  590. */
  591. x86_perf_counter_update(counter, hwc, idx);
  592. }
  593. /*
  594. * Save and restart an expired counter. Called by NMI contexts,
  595. * so it has to be careful about preempting normal counter ops:
  596. */
  597. static void intel_pmu_save_and_restart(struct perf_counter *counter)
  598. {
  599. struct hw_perf_counter *hwc = &counter->hw;
  600. int idx = hwc->idx;
  601. x86_perf_counter_update(counter, hwc, idx);
  602. x86_perf_counter_set_period(counter, hwc, idx);
  603. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  604. __x86_pmu_enable(counter, hwc, idx);
  605. }
  606. /*
  607. * Maximum interrupt frequency of 100KHz per CPU
  608. */
  609. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  610. /*
  611. * This handler is triggered by the local APIC, so the APIC IRQ handling
  612. * rules apply:
  613. */
  614. static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
  615. {
  616. int bit, cpu = smp_processor_id();
  617. u64 ack, status;
  618. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  619. int ret = 0;
  620. cpuc->throttle_ctrl = intel_pmu_save_disable_all();
  621. status = intel_pmu_get_status(cpuc->throttle_ctrl);
  622. if (!status)
  623. goto out;
  624. ret = 1;
  625. again:
  626. inc_irq_stat(apic_perf_irqs);
  627. ack = status;
  628. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  629. struct perf_counter *counter = cpuc->counters[bit];
  630. clear_bit(bit, (unsigned long *) &status);
  631. if (!counter)
  632. continue;
  633. intel_pmu_save_and_restart(counter);
  634. if (perf_counter_overflow(counter, nmi, regs, 0))
  635. __x86_pmu_disable(counter, &counter->hw, bit);
  636. }
  637. intel_pmu_ack_status(ack);
  638. /*
  639. * Repeat if there is more work to be done:
  640. */
  641. status = intel_pmu_get_status(cpuc->throttle_ctrl);
  642. if (status)
  643. goto again;
  644. out:
  645. /*
  646. * Restore - do not reenable when global enable is off or throttled:
  647. */
  648. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
  649. intel_pmu_restore_all(cpuc->throttle_ctrl);
  650. return ret;
  651. }
  652. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi) { return 0; }
  653. void perf_counter_unthrottle(void)
  654. {
  655. struct cpu_hw_counters *cpuc;
  656. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  657. return;
  658. if (unlikely(!perf_counters_initialized))
  659. return;
  660. cpuc = &__get_cpu_var(cpu_hw_counters);
  661. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  662. if (printk_ratelimit())
  663. printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
  664. hw_perf_restore(cpuc->throttle_ctrl);
  665. }
  666. cpuc->interrupts = 0;
  667. }
  668. void smp_perf_counter_interrupt(struct pt_regs *regs)
  669. {
  670. irq_enter();
  671. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  672. ack_APIC_irq();
  673. x86_pmu.handle_irq(regs, 0);
  674. irq_exit();
  675. }
  676. void smp_perf_pending_interrupt(struct pt_regs *regs)
  677. {
  678. irq_enter();
  679. ack_APIC_irq();
  680. inc_irq_stat(apic_pending_irqs);
  681. perf_counter_do_pending();
  682. irq_exit();
  683. }
  684. void set_perf_counter_pending(void)
  685. {
  686. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  687. }
  688. void perf_counters_lapic_init(int nmi)
  689. {
  690. u32 apic_val;
  691. if (!perf_counters_initialized)
  692. return;
  693. /*
  694. * Enable the performance counter vector in the APIC LVT:
  695. */
  696. apic_val = apic_read(APIC_LVTERR);
  697. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  698. if (nmi)
  699. apic_write(APIC_LVTPC, APIC_DM_NMI);
  700. else
  701. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  702. apic_write(APIC_LVTERR, apic_val);
  703. }
  704. static int __kprobes
  705. perf_counter_nmi_handler(struct notifier_block *self,
  706. unsigned long cmd, void *__args)
  707. {
  708. struct die_args *args = __args;
  709. struct pt_regs *regs;
  710. int ret;
  711. switch (cmd) {
  712. case DIE_NMI:
  713. case DIE_NMI_IPI:
  714. break;
  715. default:
  716. return NOTIFY_DONE;
  717. }
  718. regs = args->regs;
  719. apic_write(APIC_LVTPC, APIC_DM_NMI);
  720. ret = x86_pmu.handle_irq(regs, 1);
  721. return ret ? NOTIFY_STOP : NOTIFY_OK;
  722. }
  723. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  724. .notifier_call = perf_counter_nmi_handler,
  725. .next = NULL,
  726. .priority = 1
  727. };
  728. static struct x86_pmu intel_pmu = {
  729. .name = "Intel",
  730. .handle_irq = intel_pmu_handle_irq,
  731. .save_disable_all = intel_pmu_save_disable_all,
  732. .restore_all = intel_pmu_restore_all,
  733. .enable = intel_pmu_enable_counter,
  734. .disable = intel_pmu_disable_counter,
  735. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  736. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  737. .event_map = intel_pmu_event_map,
  738. .raw_event = intel_pmu_raw_event,
  739. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  740. };
  741. static struct x86_pmu amd_pmu = {
  742. .name = "AMD",
  743. .handle_irq = amd_pmu_handle_irq,
  744. .save_disable_all = amd_pmu_save_disable_all,
  745. .restore_all = amd_pmu_restore_all,
  746. .enable = amd_pmu_enable_counter,
  747. .disable = amd_pmu_disable_counter,
  748. .eventsel = MSR_K7_EVNTSEL0,
  749. .perfctr = MSR_K7_PERFCTR0,
  750. .event_map = amd_pmu_event_map,
  751. .raw_event = amd_pmu_raw_event,
  752. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  753. .num_counters = 4,
  754. .counter_bits = 48,
  755. .counter_mask = (1ULL << 48) - 1,
  756. };
  757. static int intel_pmu_init(void)
  758. {
  759. union cpuid10_edx edx;
  760. union cpuid10_eax eax;
  761. unsigned int unused;
  762. unsigned int ebx;
  763. int version;
  764. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  765. return -ENODEV;
  766. /*
  767. * Check whether the Architectural PerfMon supports
  768. * Branch Misses Retired Event or not.
  769. */
  770. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  771. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  772. return -ENODEV;
  773. version = eax.split.version_id;
  774. if (version < 2)
  775. return -ENODEV;
  776. x86_pmu = intel_pmu;
  777. x86_pmu.version = version;
  778. x86_pmu.num_counters = eax.split.num_counters;
  779. x86_pmu.num_counters_fixed = edx.split.num_counters_fixed;
  780. x86_pmu.counter_bits = eax.split.bit_width;
  781. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  782. return 0;
  783. }
  784. static int amd_pmu_init(void)
  785. {
  786. x86_pmu = amd_pmu;
  787. return 0;
  788. }
  789. void __init init_hw_perf_counters(void)
  790. {
  791. int err;
  792. switch (boot_cpu_data.x86_vendor) {
  793. case X86_VENDOR_INTEL:
  794. err = intel_pmu_init();
  795. break;
  796. case X86_VENDOR_AMD:
  797. err = amd_pmu_init();
  798. break;
  799. default:
  800. return;
  801. }
  802. if (err != 0)
  803. return;
  804. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  805. pr_info("... version: %d\n", x86_pmu.version);
  806. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  807. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  808. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  809. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  810. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  811. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  812. }
  813. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  814. perf_max_counters = x86_pmu.num_counters;
  815. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  816. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  817. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  818. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  819. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  820. }
  821. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  822. perf_counter_mask |=
  823. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  824. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  825. perf_counters_initialized = true;
  826. perf_counters_lapic_init(0);
  827. register_die_notifier(&perf_counter_nmi_notifier);
  828. }
  829. static void x86_pmu_read(struct perf_counter *counter)
  830. {
  831. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  832. }
  833. static const struct pmu pmu = {
  834. .enable = x86_pmu_enable,
  835. .disable = x86_pmu_disable,
  836. .read = x86_pmu_read,
  837. };
  838. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  839. {
  840. int err;
  841. err = __hw_perf_counter_init(counter);
  842. if (err)
  843. return ERR_PTR(err);
  844. return &pmu;
  845. }
  846. /*
  847. * callchain support
  848. */
  849. static inline
  850. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  851. {
  852. if (entry->nr < MAX_STACK_DEPTH)
  853. entry->ip[entry->nr++] = ip;
  854. }
  855. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  856. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  857. static void
  858. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  859. {
  860. /* Ignore warnings */
  861. }
  862. static void backtrace_warning(void *data, char *msg)
  863. {
  864. /* Ignore warnings */
  865. }
  866. static int backtrace_stack(void *data, char *name)
  867. {
  868. /* Don't bother with IRQ stacks for now */
  869. return -1;
  870. }
  871. static void backtrace_address(void *data, unsigned long addr, int reliable)
  872. {
  873. struct perf_callchain_entry *entry = data;
  874. if (reliable)
  875. callchain_store(entry, addr);
  876. }
  877. static const struct stacktrace_ops backtrace_ops = {
  878. .warning = backtrace_warning,
  879. .warning_symbol = backtrace_warning_symbol,
  880. .stack = backtrace_stack,
  881. .address = backtrace_address,
  882. };
  883. static void
  884. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  885. {
  886. unsigned long bp;
  887. char *stack;
  888. int nr = entry->nr;
  889. callchain_store(entry, instruction_pointer(regs));
  890. stack = ((char *)regs + sizeof(struct pt_regs));
  891. #ifdef CONFIG_FRAME_POINTER
  892. bp = frame_pointer(regs);
  893. #else
  894. bp = 0;
  895. #endif
  896. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  897. entry->kernel = entry->nr - nr;
  898. }
  899. struct stack_frame {
  900. const void __user *next_fp;
  901. unsigned long return_address;
  902. };
  903. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  904. {
  905. int ret;
  906. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  907. return 0;
  908. ret = 1;
  909. pagefault_disable();
  910. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  911. ret = 0;
  912. pagefault_enable();
  913. return ret;
  914. }
  915. static void
  916. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  917. {
  918. struct stack_frame frame;
  919. const void __user *fp;
  920. int nr = entry->nr;
  921. regs = (struct pt_regs *)current->thread.sp0 - 1;
  922. fp = (void __user *)regs->bp;
  923. callchain_store(entry, regs->ip);
  924. while (entry->nr < MAX_STACK_DEPTH) {
  925. frame.next_fp = NULL;
  926. frame.return_address = 0;
  927. if (!copy_stack_frame(fp, &frame))
  928. break;
  929. if ((unsigned long)fp < user_stack_pointer(regs))
  930. break;
  931. callchain_store(entry, frame.return_address);
  932. fp = frame.next_fp;
  933. }
  934. entry->user = entry->nr - nr;
  935. }
  936. static void
  937. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  938. {
  939. int is_user;
  940. if (!regs)
  941. return;
  942. is_user = user_mode(regs);
  943. if (!current || current->pid == 0)
  944. return;
  945. if (is_user && current->state != TASK_RUNNING)
  946. return;
  947. if (!is_user)
  948. perf_callchain_kernel(regs, entry);
  949. if (current->mm)
  950. perf_callchain_user(regs, entry);
  951. }
  952. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  953. {
  954. struct perf_callchain_entry *entry;
  955. if (in_nmi())
  956. entry = &__get_cpu_var(nmi_entry);
  957. else
  958. entry = &__get_cpu_var(irq_entry);
  959. entry->nr = 0;
  960. entry->hv = 0;
  961. entry->kernel = 0;
  962. entry->user = 0;
  963. perf_do_callchain(regs, entry);
  964. return entry;
  965. }