perf_counter.c 28 KB

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