perf_counter.c 28 KB

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