perf_counter.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. * Performance counter x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. *
  10. * For licencing details see kernel-base/COPYING
  11. */
  12. #include <linux/perf_counter.h>
  13. #include <linux/capability.h>
  14. #include <linux/notifier.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/apic.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/nmi.h>
  24. static u64 perf_counter_mask __read_mostly;
  25. struct cpu_hw_counters {
  26. struct perf_counter *counters[X86_PMC_IDX_MAX];
  27. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  28. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. unsigned long interrupts;
  30. int enabled;
  31. };
  32. /*
  33. * struct x86_pmu - generic x86 pmu
  34. */
  35. struct x86_pmu {
  36. const char *name;
  37. int version;
  38. int (*handle_irq)(struct pt_regs *, int);
  39. void (*disable_all)(void);
  40. void (*enable_all)(void);
  41. void (*enable)(struct hw_perf_counter *, int);
  42. void (*disable)(struct hw_perf_counter *, int);
  43. unsigned eventsel;
  44. unsigned perfctr;
  45. u64 (*event_map)(int);
  46. u64 (*raw_event)(u64);
  47. int max_events;
  48. int num_counters;
  49. int num_counters_fixed;
  50. int counter_bits;
  51. u64 counter_mask;
  52. u64 max_period;
  53. u64 intel_ctrl;
  54. };
  55. static struct x86_pmu x86_pmu __read_mostly;
  56. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  57. .enabled = 1,
  58. };
  59. /*
  60. * Intel PerfMon v3. Used on Core2 and later.
  61. */
  62. static const u64 intel_perfmon_event_map[] =
  63. {
  64. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  65. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  66. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  67. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  68. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  69. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  70. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  71. };
  72. static u64 intel_pmu_event_map(int event)
  73. {
  74. return intel_perfmon_event_map[event];
  75. }
  76. static u64 intel_pmu_raw_event(u64 event)
  77. {
  78. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  79. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  80. #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
  81. #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
  82. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  83. #define CORE_EVNTSEL_MASK \
  84. (CORE_EVNTSEL_EVENT_MASK | \
  85. CORE_EVNTSEL_UNIT_MASK | \
  86. CORE_EVNTSEL_EDGE_MASK | \
  87. CORE_EVNTSEL_INV_MASK | \
  88. CORE_EVNTSEL_COUNTER_MASK)
  89. return event & CORE_EVNTSEL_MASK;
  90. }
  91. /*
  92. * AMD Performance Monitor K7 and later.
  93. */
  94. static const u64 amd_perfmon_event_map[] =
  95. {
  96. [PERF_COUNT_CPU_CYCLES] = 0x0076,
  97. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  98. [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
  99. [PERF_COUNT_CACHE_MISSES] = 0x0081,
  100. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  101. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  102. };
  103. static u64 amd_pmu_event_map(int event)
  104. {
  105. return amd_perfmon_event_map[event];
  106. }
  107. static u64 amd_pmu_raw_event(u64 event)
  108. {
  109. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  110. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  111. #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
  112. #define K7_EVNTSEL_INV_MASK 0x000800000ULL
  113. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  114. #define K7_EVNTSEL_MASK \
  115. (K7_EVNTSEL_EVENT_MASK | \
  116. K7_EVNTSEL_UNIT_MASK | \
  117. K7_EVNTSEL_EDGE_MASK | \
  118. K7_EVNTSEL_INV_MASK | \
  119. K7_EVNTSEL_COUNTER_MASK)
  120. return event & K7_EVNTSEL_MASK;
  121. }
  122. /*
  123. * Propagate counter elapsed time into the generic counter.
  124. * Can only be executed on the CPU where the counter is active.
  125. * Returns the delta events processed.
  126. */
  127. static u64
  128. x86_perf_counter_update(struct perf_counter *counter,
  129. struct hw_perf_counter *hwc, int idx)
  130. {
  131. int shift = 64 - x86_pmu.counter_bits;
  132. u64 prev_raw_count, new_raw_count;
  133. s64 delta;
  134. /*
  135. * Careful: an NMI might modify the previous counter value.
  136. *
  137. * Our tactic to handle this is to first atomically read and
  138. * exchange a new raw count - then add that new-prev delta
  139. * count to the generic counter atomically:
  140. */
  141. again:
  142. prev_raw_count = atomic64_read(&hwc->prev_count);
  143. rdmsrl(hwc->counter_base + idx, new_raw_count);
  144. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  145. new_raw_count) != prev_raw_count)
  146. goto again;
  147. /*
  148. * Now we have the new raw value and have updated the prev
  149. * timestamp already. We can now calculate the elapsed delta
  150. * (counter-)time and add that to the generic counter.
  151. *
  152. * Careful, not all hw sign-extends above the physical width
  153. * of the count.
  154. */
  155. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  156. delta >>= shift;
  157. atomic64_add(delta, &counter->count);
  158. atomic64_sub(delta, &hwc->period_left);
  159. return new_raw_count;
  160. }
  161. static atomic_t active_counters;
  162. static DEFINE_MUTEX(pmc_reserve_mutex);
  163. static bool reserve_pmc_hardware(void)
  164. {
  165. int i;
  166. if (nmi_watchdog == NMI_LOCAL_APIC)
  167. disable_lapic_nmi_watchdog();
  168. for (i = 0; i < x86_pmu.num_counters; i++) {
  169. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  170. goto perfctr_fail;
  171. }
  172. for (i = 0; i < x86_pmu.num_counters; i++) {
  173. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  174. goto eventsel_fail;
  175. }
  176. return true;
  177. eventsel_fail:
  178. for (i--; i >= 0; i--)
  179. release_evntsel_nmi(x86_pmu.eventsel + i);
  180. i = x86_pmu.num_counters;
  181. perfctr_fail:
  182. for (i--; i >= 0; i--)
  183. release_perfctr_nmi(x86_pmu.perfctr + i);
  184. if (nmi_watchdog == NMI_LOCAL_APIC)
  185. enable_lapic_nmi_watchdog();
  186. return false;
  187. }
  188. static void release_pmc_hardware(void)
  189. {
  190. int i;
  191. for (i = 0; i < x86_pmu.num_counters; i++) {
  192. release_perfctr_nmi(x86_pmu.perfctr + i);
  193. release_evntsel_nmi(x86_pmu.eventsel + i);
  194. }
  195. if (nmi_watchdog == NMI_LOCAL_APIC)
  196. enable_lapic_nmi_watchdog();
  197. }
  198. static void hw_perf_counter_destroy(struct perf_counter *counter)
  199. {
  200. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  201. release_pmc_hardware();
  202. mutex_unlock(&pmc_reserve_mutex);
  203. }
  204. }
  205. static inline int x86_pmu_initialized(void)
  206. {
  207. return x86_pmu.handle_irq != NULL;
  208. }
  209. /*
  210. * Setup the hardware configuration for a given hw_event_type
  211. */
  212. static int __hw_perf_counter_init(struct perf_counter *counter)
  213. {
  214. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  215. struct hw_perf_counter *hwc = &counter->hw;
  216. int err;
  217. if (!x86_pmu_initialized())
  218. return -ENODEV;
  219. err = 0;
  220. if (!atomic_inc_not_zero(&active_counters)) {
  221. mutex_lock(&pmc_reserve_mutex);
  222. if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
  223. err = -EBUSY;
  224. else
  225. atomic_inc(&active_counters);
  226. mutex_unlock(&pmc_reserve_mutex);
  227. }
  228. if (err)
  229. return err;
  230. /*
  231. * Generate PMC IRQs:
  232. * (keep 'enabled' bit clear for now)
  233. */
  234. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  235. /*
  236. * Count user and OS events unless requested not to.
  237. */
  238. if (!hw_event->exclude_user)
  239. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  240. if (!hw_event->exclude_kernel)
  241. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  242. /*
  243. * If privileged enough, allow NMI events:
  244. */
  245. hwc->nmi = 0;
  246. if (hw_event->nmi) {
  247. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  248. return -EACCES;
  249. hwc->nmi = 1;
  250. }
  251. perf_counters_lapic_init(hwc->nmi);
  252. if (!hwc->irq_period)
  253. hwc->irq_period = x86_pmu.max_period;
  254. atomic64_set(&hwc->period_left,
  255. min(x86_pmu.max_period, hwc->irq_period));
  256. /*
  257. * Raw event type provide the config in the event structure
  258. */
  259. if (perf_event_raw(hw_event)) {
  260. hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
  261. } else {
  262. if (perf_event_id(hw_event) >= x86_pmu.max_events)
  263. return -EINVAL;
  264. /*
  265. * The generic map:
  266. */
  267. hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
  268. }
  269. counter->destroy = hw_perf_counter_destroy;
  270. return 0;
  271. }
  272. static void intel_pmu_disable_all(void)
  273. {
  274. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  275. }
  276. static void amd_pmu_disable_all(void)
  277. {
  278. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  279. int idx;
  280. if (!cpuc->enabled)
  281. return;
  282. cpuc->enabled = 0;
  283. /*
  284. * ensure we write the disable before we start disabling the
  285. * counters proper, so that amd_pmu_enable_counter() does the
  286. * right thing.
  287. */
  288. barrier();
  289. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  290. u64 val;
  291. if (!test_bit(idx, cpuc->active_mask))
  292. continue;
  293. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  294. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  295. continue;
  296. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  297. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  298. }
  299. }
  300. void hw_perf_disable(void)
  301. {
  302. if (!x86_pmu_initialized())
  303. return;
  304. return x86_pmu.disable_all();
  305. }
  306. static void intel_pmu_enable_all(void)
  307. {
  308. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  309. }
  310. static void amd_pmu_enable_all(void)
  311. {
  312. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  313. int idx;
  314. if (cpuc->enabled)
  315. return;
  316. cpuc->enabled = 1;
  317. barrier();
  318. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  319. u64 val;
  320. if (!test_bit(idx, cpuc->active_mask))
  321. continue;
  322. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  323. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  324. continue;
  325. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  326. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  327. }
  328. }
  329. void hw_perf_enable(void)
  330. {
  331. if (!x86_pmu_initialized())
  332. return;
  333. x86_pmu.enable_all();
  334. }
  335. static inline u64 intel_pmu_get_status(void)
  336. {
  337. u64 status;
  338. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  339. return status;
  340. }
  341. static inline void intel_pmu_ack_status(u64 ack)
  342. {
  343. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  344. }
  345. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  346. {
  347. int err;
  348. err = checking_wrmsrl(hwc->config_base + idx,
  349. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  350. }
  351. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  352. {
  353. int err;
  354. err = checking_wrmsrl(hwc->config_base + idx,
  355. hwc->config);
  356. }
  357. static inline void
  358. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  359. {
  360. int idx = __idx - X86_PMC_IDX_FIXED;
  361. u64 ctrl_val, mask;
  362. int err;
  363. mask = 0xfULL << (idx * 4);
  364. rdmsrl(hwc->config_base, ctrl_val);
  365. ctrl_val &= ~mask;
  366. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  367. }
  368. static inline void
  369. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  370. {
  371. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  372. intel_pmu_disable_fixed(hwc, idx);
  373. return;
  374. }
  375. x86_pmu_disable_counter(hwc, idx);
  376. }
  377. static inline void
  378. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  379. {
  380. x86_pmu_disable_counter(hwc, idx);
  381. }
  382. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  383. /*
  384. * Set the next IRQ period, based on the hwc->period_left value.
  385. * To be called with the counter disabled in hw:
  386. */
  387. static void
  388. x86_perf_counter_set_period(struct perf_counter *counter,
  389. struct hw_perf_counter *hwc, int idx)
  390. {
  391. s64 left = atomic64_read(&hwc->period_left);
  392. s64 period = min(x86_pmu.max_period, hwc->irq_period);
  393. int err;
  394. /*
  395. * If we are way outside a reasoable range then just skip forward:
  396. */
  397. if (unlikely(left <= -period)) {
  398. left = period;
  399. atomic64_set(&hwc->period_left, left);
  400. }
  401. if (unlikely(left <= 0)) {
  402. left += period;
  403. atomic64_set(&hwc->period_left, left);
  404. }
  405. /*
  406. * Quirk: certain CPUs dont like it if just 1 event is left:
  407. */
  408. if (unlikely(left < 2))
  409. left = 2;
  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 (!x86_pmu.num_counters_fixed)
  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_mask))
  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_mask)) {
  503. try_generic:
  504. idx = find_first_zero_bit(cpuc->used_mask,
  505. x86_pmu.num_counters);
  506. if (idx == x86_pmu.num_counters)
  507. return -EAGAIN;
  508. set_bit(idx, cpuc->used_mask);
  509. hwc->idx = idx;
  510. }
  511. hwc->config_base = x86_pmu.eventsel;
  512. hwc->counter_base = x86_pmu.perfctr;
  513. }
  514. x86_pmu.disable(hwc, idx);
  515. cpuc->counters[idx] = counter;
  516. set_bit(idx, cpuc->active_mask);
  517. x86_perf_counter_set_period(counter, hwc, idx);
  518. x86_pmu.enable(hwc, idx);
  519. return 0;
  520. }
  521. void perf_counter_print_debug(void)
  522. {
  523. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  524. struct cpu_hw_counters *cpuc;
  525. unsigned long flags;
  526. int cpu, idx;
  527. if (!x86_pmu.num_counters)
  528. return;
  529. local_irq_save(flags);
  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_mask);
  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_restore(flags);
  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_mask);
  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_mask);
  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. struct cpu_hw_counters *cpuc;
  610. struct cpu_hw_counters;
  611. int bit, cpu, loops;
  612. u64 ack, status;
  613. cpu = smp_processor_id();
  614. cpuc = &per_cpu(cpu_hw_counters, cpu);
  615. perf_disable();
  616. status = intel_pmu_get_status();
  617. if (!status) {
  618. perf_enable();
  619. return 0;
  620. }
  621. loops = 0;
  622. again:
  623. if (++loops > 100) {
  624. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  625. perf_counter_print_debug();
  626. return 1;
  627. }
  628. inc_irq_stat(apic_perf_irqs);
  629. ack = status;
  630. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  631. struct perf_counter *counter = cpuc->counters[bit];
  632. clear_bit(bit, (unsigned long *) &status);
  633. if (!test_bit(bit, cpuc->active_mask))
  634. continue;
  635. intel_pmu_save_and_restart(counter);
  636. if (perf_counter_overflow(counter, nmi, regs, 0))
  637. intel_pmu_disable_counter(&counter->hw, bit);
  638. }
  639. intel_pmu_ack_status(ack);
  640. /*
  641. * Repeat if there is more work to be done:
  642. */
  643. status = intel_pmu_get_status();
  644. if (status)
  645. goto again;
  646. if (++cpuc->interrupts != PERFMON_MAX_INTERRUPTS)
  647. perf_enable();
  648. return 1;
  649. }
  650. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
  651. {
  652. int cpu, idx, throttle = 0, handled = 0;
  653. struct cpu_hw_counters *cpuc;
  654. struct perf_counter *counter;
  655. struct hw_perf_counter *hwc;
  656. u64 val;
  657. cpu = smp_processor_id();
  658. cpuc = &per_cpu(cpu_hw_counters, cpu);
  659. if (++cpuc->interrupts == PERFMON_MAX_INTERRUPTS) {
  660. throttle = 1;
  661. __perf_disable();
  662. cpuc->enabled = 0;
  663. barrier();
  664. }
  665. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  666. int disable = 0;
  667. if (!test_bit(idx, cpuc->active_mask))
  668. continue;
  669. counter = cpuc->counters[idx];
  670. hwc = &counter->hw;
  671. if (counter->hw_event.nmi != nmi)
  672. goto next;
  673. val = x86_perf_counter_update(counter, hwc, idx);
  674. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  675. goto next;
  676. /* counter overflow */
  677. x86_perf_counter_set_period(counter, hwc, idx);
  678. handled = 1;
  679. inc_irq_stat(apic_perf_irqs);
  680. disable = perf_counter_overflow(counter, nmi, regs, 0);
  681. next:
  682. if (disable || throttle)
  683. amd_pmu_disable_counter(hwc, idx);
  684. }
  685. return handled;
  686. }
  687. void perf_counter_unthrottle(void)
  688. {
  689. struct cpu_hw_counters *cpuc;
  690. if (!x86_pmu_initialized())
  691. return;
  692. cpuc = &__get_cpu_var(cpu_hw_counters);
  693. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  694. /*
  695. * Clear them before re-enabling irqs/NMIs again:
  696. */
  697. cpuc->interrupts = 0;
  698. perf_enable();
  699. } else {
  700. cpuc->interrupts = 0;
  701. }
  702. }
  703. void smp_perf_counter_interrupt(struct pt_regs *regs)
  704. {
  705. irq_enter();
  706. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  707. ack_APIC_irq();
  708. x86_pmu.handle_irq(regs, 0);
  709. irq_exit();
  710. }
  711. void smp_perf_pending_interrupt(struct pt_regs *regs)
  712. {
  713. irq_enter();
  714. ack_APIC_irq();
  715. inc_irq_stat(apic_pending_irqs);
  716. perf_counter_do_pending();
  717. irq_exit();
  718. }
  719. void set_perf_counter_pending(void)
  720. {
  721. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  722. }
  723. void perf_counters_lapic_init(int nmi)
  724. {
  725. u32 apic_val;
  726. if (!x86_pmu_initialized())
  727. return;
  728. /*
  729. * Enable the performance counter vector in the APIC LVT:
  730. */
  731. apic_val = apic_read(APIC_LVTERR);
  732. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  733. if (nmi)
  734. apic_write(APIC_LVTPC, APIC_DM_NMI);
  735. else
  736. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  737. apic_write(APIC_LVTERR, apic_val);
  738. }
  739. static int __kprobes
  740. perf_counter_nmi_handler(struct notifier_block *self,
  741. unsigned long cmd, void *__args)
  742. {
  743. struct die_args *args = __args;
  744. struct pt_regs *regs;
  745. if (!atomic_read(&active_counters))
  746. return NOTIFY_DONE;
  747. switch (cmd) {
  748. case DIE_NMI:
  749. case DIE_NMI_IPI:
  750. break;
  751. default:
  752. return NOTIFY_DONE;
  753. }
  754. regs = args->regs;
  755. apic_write(APIC_LVTPC, APIC_DM_NMI);
  756. /*
  757. * Can't rely on the handled return value to say it was our NMI, two
  758. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  759. *
  760. * If the first NMI handles both, the latter will be empty and daze
  761. * the CPU.
  762. */
  763. x86_pmu.handle_irq(regs, 1);
  764. return NOTIFY_STOP;
  765. }
  766. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  767. .notifier_call = perf_counter_nmi_handler,
  768. .next = NULL,
  769. .priority = 1
  770. };
  771. static struct x86_pmu intel_pmu = {
  772. .name = "Intel",
  773. .handle_irq = intel_pmu_handle_irq,
  774. .disable_all = intel_pmu_disable_all,
  775. .enable_all = intel_pmu_enable_all,
  776. .enable = intel_pmu_enable_counter,
  777. .disable = intel_pmu_disable_counter,
  778. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  779. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  780. .event_map = intel_pmu_event_map,
  781. .raw_event = intel_pmu_raw_event,
  782. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  783. /*
  784. * Intel PMCs cannot be accessed sanely above 32 bit width,
  785. * so we install an artificial 1<<31 period regardless of
  786. * the generic counter period:
  787. */
  788. .max_period = (1ULL << 31) - 1,
  789. };
  790. static struct x86_pmu amd_pmu = {
  791. .name = "AMD",
  792. .handle_irq = amd_pmu_handle_irq,
  793. .disable_all = amd_pmu_disable_all,
  794. .enable_all = amd_pmu_enable_all,
  795. .enable = amd_pmu_enable_counter,
  796. .disable = amd_pmu_disable_counter,
  797. .eventsel = MSR_K7_EVNTSEL0,
  798. .perfctr = MSR_K7_PERFCTR0,
  799. .event_map = amd_pmu_event_map,
  800. .raw_event = amd_pmu_raw_event,
  801. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  802. .num_counters = 4,
  803. .counter_bits = 48,
  804. .counter_mask = (1ULL << 48) - 1,
  805. /* use highest bit to detect overflow */
  806. .max_period = (1ULL << 47) - 1,
  807. };
  808. static int intel_pmu_init(void)
  809. {
  810. union cpuid10_edx edx;
  811. union cpuid10_eax eax;
  812. unsigned int unused;
  813. unsigned int ebx;
  814. int version;
  815. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  816. return -ENODEV;
  817. /*
  818. * Check whether the Architectural PerfMon supports
  819. * Branch Misses Retired Event or not.
  820. */
  821. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  822. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  823. return -ENODEV;
  824. version = eax.split.version_id;
  825. if (version < 2)
  826. return -ENODEV;
  827. x86_pmu = intel_pmu;
  828. x86_pmu.version = version;
  829. x86_pmu.num_counters = eax.split.num_counters;
  830. /*
  831. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  832. * assume at least 3 counters:
  833. */
  834. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  835. x86_pmu.counter_bits = eax.split.bit_width;
  836. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  837. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  838. return 0;
  839. }
  840. static int amd_pmu_init(void)
  841. {
  842. x86_pmu = amd_pmu;
  843. return 0;
  844. }
  845. void __init init_hw_perf_counters(void)
  846. {
  847. int err;
  848. switch (boot_cpu_data.x86_vendor) {
  849. case X86_VENDOR_INTEL:
  850. err = intel_pmu_init();
  851. break;
  852. case X86_VENDOR_AMD:
  853. err = amd_pmu_init();
  854. break;
  855. default:
  856. return;
  857. }
  858. if (err != 0)
  859. return;
  860. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  861. pr_info("... version: %d\n", x86_pmu.version);
  862. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  863. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  864. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  865. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  866. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  867. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  868. }
  869. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  870. perf_max_counters = x86_pmu.num_counters;
  871. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  872. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  873. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  874. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  875. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  876. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  877. }
  878. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  879. perf_counter_mask |=
  880. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  881. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  882. perf_counters_lapic_init(1);
  883. register_die_notifier(&perf_counter_nmi_notifier);
  884. }
  885. static inline void x86_pmu_read(struct perf_counter *counter)
  886. {
  887. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  888. }
  889. static const struct pmu pmu = {
  890. .enable = x86_pmu_enable,
  891. .disable = x86_pmu_disable,
  892. .read = x86_pmu_read,
  893. };
  894. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  895. {
  896. int err;
  897. err = __hw_perf_counter_init(counter);
  898. if (err)
  899. return ERR_PTR(err);
  900. return &pmu;
  901. }
  902. /*
  903. * callchain support
  904. */
  905. static inline
  906. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  907. {
  908. if (entry->nr < MAX_STACK_DEPTH)
  909. entry->ip[entry->nr++] = ip;
  910. }
  911. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  912. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  913. static void
  914. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  915. {
  916. /* Ignore warnings */
  917. }
  918. static void backtrace_warning(void *data, char *msg)
  919. {
  920. /* Ignore warnings */
  921. }
  922. static int backtrace_stack(void *data, char *name)
  923. {
  924. /* Don't bother with IRQ stacks for now */
  925. return -1;
  926. }
  927. static void backtrace_address(void *data, unsigned long addr, int reliable)
  928. {
  929. struct perf_callchain_entry *entry = data;
  930. if (reliable)
  931. callchain_store(entry, addr);
  932. }
  933. static const struct stacktrace_ops backtrace_ops = {
  934. .warning = backtrace_warning,
  935. .warning_symbol = backtrace_warning_symbol,
  936. .stack = backtrace_stack,
  937. .address = backtrace_address,
  938. };
  939. static void
  940. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  941. {
  942. unsigned long bp;
  943. char *stack;
  944. int nr = entry->nr;
  945. callchain_store(entry, instruction_pointer(regs));
  946. stack = ((char *)regs + sizeof(struct pt_regs));
  947. #ifdef CONFIG_FRAME_POINTER
  948. bp = frame_pointer(regs);
  949. #else
  950. bp = 0;
  951. #endif
  952. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  953. entry->kernel = entry->nr - nr;
  954. }
  955. struct stack_frame {
  956. const void __user *next_fp;
  957. unsigned long return_address;
  958. };
  959. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  960. {
  961. int ret;
  962. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  963. return 0;
  964. ret = 1;
  965. pagefault_disable();
  966. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  967. ret = 0;
  968. pagefault_enable();
  969. return ret;
  970. }
  971. static void
  972. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  973. {
  974. struct stack_frame frame;
  975. const void __user *fp;
  976. int nr = entry->nr;
  977. regs = (struct pt_regs *)current->thread.sp0 - 1;
  978. fp = (void __user *)regs->bp;
  979. callchain_store(entry, regs->ip);
  980. while (entry->nr < MAX_STACK_DEPTH) {
  981. frame.next_fp = NULL;
  982. frame.return_address = 0;
  983. if (!copy_stack_frame(fp, &frame))
  984. break;
  985. if ((unsigned long)fp < user_stack_pointer(regs))
  986. break;
  987. callchain_store(entry, frame.return_address);
  988. fp = frame.next_fp;
  989. }
  990. entry->user = entry->nr - nr;
  991. }
  992. static void
  993. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  994. {
  995. int is_user;
  996. if (!regs)
  997. return;
  998. is_user = user_mode(regs);
  999. if (!current || current->pid == 0)
  1000. return;
  1001. if (is_user && current->state != TASK_RUNNING)
  1002. return;
  1003. if (!is_user)
  1004. perf_callchain_kernel(regs, entry);
  1005. if (current->mm)
  1006. perf_callchain_user(regs, entry);
  1007. }
  1008. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1009. {
  1010. struct perf_callchain_entry *entry;
  1011. if (in_nmi())
  1012. entry = &__get_cpu_var(nmi_entry);
  1013. else
  1014. entry = &__get_cpu_var(irq_entry);
  1015. entry->nr = 0;
  1016. entry->hv = 0;
  1017. entry->kernel = 0;
  1018. entry->user = 0;
  1019. perf_do_callchain(regs, entry);
  1020. return entry;
  1021. }