perf_counter.c 27 KB

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