perf_counter.c 28 KB

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