perf_counter.c 27 KB

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