perf_counter.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. *
  8. * For licencing details see kernel-base/COPYING
  9. */
  10. #include <linux/perf_counter.h>
  11. #include <linux/capability.h>
  12. #include <linux/notifier.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/module.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/sched.h>
  18. #include <asm/apic.h>
  19. static bool perf_counters_initialized __read_mostly;
  20. /*
  21. * Number of (generic) HW counters:
  22. */
  23. static int nr_counters_generic __read_mostly;
  24. static u64 perf_counter_mask __read_mostly;
  25. static u64 counter_value_mask __read_mostly;
  26. static int counter_value_bits __read_mostly;
  27. static int nr_counters_fixed __read_mostly;
  28. struct cpu_hw_counters {
  29. struct perf_counter *counters[X86_PMC_IDX_MAX];
  30. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  31. unsigned long interrupts;
  32. u64 throttle_ctrl;
  33. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  34. int enabled;
  35. };
  36. /*
  37. * struct pmc_x86_ops - performance counter x86 ops
  38. */
  39. struct pmc_x86_ops {
  40. u64 (*save_disable_all)(void);
  41. void (*restore_all)(u64);
  42. u64 (*get_status)(u64);
  43. void (*ack_status)(u64);
  44. void (*enable)(int, u64);
  45. void (*disable)(int, u64);
  46. unsigned eventsel;
  47. unsigned perfctr;
  48. u64 (*event_map)(int);
  49. u64 (*raw_event)(u64);
  50. int max_events;
  51. };
  52. static struct pmc_x86_ops *pmc_ops __read_mostly;
  53. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  54. .enabled = 1,
  55. };
  56. static __read_mostly int intel_perfmon_version;
  57. /*
  58. * Intel PerfMon v3. Used on Core2 and later.
  59. */
  60. static const u64 intel_perfmon_event_map[] =
  61. {
  62. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  63. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  64. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  65. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  66. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  67. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  68. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  69. };
  70. static u64 pmc_intel_event_map(int event)
  71. {
  72. return intel_perfmon_event_map[event];
  73. }
  74. static u64 pmc_intel_raw_event(u64 event)
  75. {
  76. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  77. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  78. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  79. #define CORE_EVNTSEL_MASK \
  80. (CORE_EVNTSEL_EVENT_MASK | \
  81. CORE_EVNTSEL_UNIT_MASK | \
  82. CORE_EVNTSEL_COUNTER_MASK)
  83. return event & CORE_EVNTSEL_MASK;
  84. }
  85. /*
  86. * AMD Performance Monitor K7 and later.
  87. */
  88. static const u64 amd_perfmon_event_map[] =
  89. {
  90. [PERF_COUNT_CPU_CYCLES] = 0x0076,
  91. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  92. [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
  93. [PERF_COUNT_CACHE_MISSES] = 0x0081,
  94. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  95. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  96. };
  97. static u64 pmc_amd_event_map(int event)
  98. {
  99. return amd_perfmon_event_map[event];
  100. }
  101. static u64 pmc_amd_raw_event(u64 event)
  102. {
  103. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  104. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  105. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  106. #define K7_EVNTSEL_MASK \
  107. (K7_EVNTSEL_EVENT_MASK | \
  108. K7_EVNTSEL_UNIT_MASK | \
  109. K7_EVNTSEL_COUNTER_MASK)
  110. return event & K7_EVNTSEL_MASK;
  111. }
  112. /*
  113. * Propagate counter elapsed time into the generic counter.
  114. * Can only be executed on the CPU where the counter is active.
  115. * Returns the delta events processed.
  116. */
  117. static void
  118. x86_perf_counter_update(struct perf_counter *counter,
  119. struct hw_perf_counter *hwc, int idx)
  120. {
  121. u64 prev_raw_count, new_raw_count, delta;
  122. /*
  123. * Careful: an NMI might modify the previous counter value.
  124. *
  125. * Our tactic to handle this is to first atomically read and
  126. * exchange a new raw count - then add that new-prev delta
  127. * count to the generic counter atomically:
  128. */
  129. again:
  130. prev_raw_count = atomic64_read(&hwc->prev_count);
  131. rdmsrl(hwc->counter_base + idx, new_raw_count);
  132. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  133. new_raw_count) != prev_raw_count)
  134. goto again;
  135. /*
  136. * Now we have the new raw value and have updated the prev
  137. * timestamp already. We can now calculate the elapsed delta
  138. * (counter-)time and add that to the generic counter.
  139. *
  140. * Careful, not all hw sign-extends above the physical width
  141. * of the count, so we do that by clipping the delta to 32 bits:
  142. */
  143. delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
  144. atomic64_add(delta, &counter->count);
  145. atomic64_sub(delta, &hwc->period_left);
  146. }
  147. /*
  148. * Setup the hardware configuration for a given hw_event_type
  149. */
  150. static int __hw_perf_counter_init(struct perf_counter *counter)
  151. {
  152. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  153. struct hw_perf_counter *hwc = &counter->hw;
  154. if (unlikely(!perf_counters_initialized))
  155. return -EINVAL;
  156. /*
  157. * Generate PMC IRQs:
  158. * (keep 'enabled' bit clear for now)
  159. */
  160. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  161. /*
  162. * Count user and OS events unless requested not to.
  163. */
  164. if (!hw_event->exclude_user)
  165. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  166. if (!hw_event->exclude_kernel)
  167. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  168. /*
  169. * If privileged enough, allow NMI events:
  170. */
  171. hwc->nmi = 0;
  172. if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
  173. hwc->nmi = 1;
  174. hwc->irq_period = hw_event->irq_period;
  175. /*
  176. * Intel PMCs cannot be accessed sanely above 32 bit width,
  177. * so we install an artificial 1<<31 period regardless of
  178. * the generic counter period:
  179. */
  180. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  181. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
  182. hwc->irq_period = 0x7FFFFFFF;
  183. atomic64_set(&hwc->period_left, hwc->irq_period);
  184. /*
  185. * Raw event type provide the config in the event structure
  186. */
  187. if (hw_event->raw_type) {
  188. hwc->config |= pmc_ops->raw_event(hw_event->raw_event_id);
  189. } else {
  190. if (hw_event->event_id >= pmc_ops->max_events)
  191. return -EINVAL;
  192. /*
  193. * The generic map:
  194. */
  195. hwc->config |= pmc_ops->event_map(hw_event->event_id);
  196. }
  197. counter->wakeup_pending = 0;
  198. return 0;
  199. }
  200. static u64 pmc_intel_save_disable_all(void)
  201. {
  202. u64 ctrl;
  203. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  204. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  205. return ctrl;
  206. }
  207. static u64 pmc_amd_save_disable_all(void)
  208. {
  209. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  210. int enabled, idx;
  211. enabled = cpuc->enabled;
  212. cpuc->enabled = 0;
  213. /*
  214. * ensure we write the disable before we start disabling the
  215. * counters proper, so that pcm_amd_enable() does the right thing.
  216. */
  217. barrier();
  218. for (idx = 0; idx < nr_counters_generic; idx++) {
  219. u64 val;
  220. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  221. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
  222. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  223. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  224. }
  225. }
  226. return enabled;
  227. }
  228. u64 hw_perf_save_disable(void)
  229. {
  230. if (unlikely(!perf_counters_initialized))
  231. return 0;
  232. return pmc_ops->save_disable_all();
  233. }
  234. /*
  235. * Exported because of ACPI idle
  236. */
  237. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  238. static void pmc_intel_restore_all(u64 ctrl)
  239. {
  240. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  241. }
  242. static void pmc_amd_restore_all(u64 ctrl)
  243. {
  244. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  245. int idx;
  246. cpuc->enabled = ctrl;
  247. barrier();
  248. if (!ctrl)
  249. return;
  250. for (idx = 0; idx < nr_counters_generic; idx++) {
  251. if (test_bit(idx, cpuc->active_mask)) {
  252. u64 val;
  253. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  254. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  255. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  256. }
  257. }
  258. }
  259. void hw_perf_restore(u64 ctrl)
  260. {
  261. if (unlikely(!perf_counters_initialized))
  262. return;
  263. pmc_ops->restore_all(ctrl);
  264. }
  265. /*
  266. * Exported because of ACPI idle
  267. */
  268. EXPORT_SYMBOL_GPL(hw_perf_restore);
  269. static u64 pmc_intel_get_status(u64 mask)
  270. {
  271. u64 status;
  272. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  273. return status;
  274. }
  275. static u64 pmc_amd_get_status(u64 mask)
  276. {
  277. u64 status = 0;
  278. int idx;
  279. for (idx = 0; idx < nr_counters_generic; idx++) {
  280. s64 val;
  281. if (!(mask & (1 << idx)))
  282. continue;
  283. rdmsrl(MSR_K7_PERFCTR0 + idx, val);
  284. val <<= (64 - counter_value_bits);
  285. if (val >= 0)
  286. status |= (1 << idx);
  287. }
  288. return status;
  289. }
  290. static u64 hw_perf_get_status(u64 mask)
  291. {
  292. if (unlikely(!perf_counters_initialized))
  293. return 0;
  294. return pmc_ops->get_status(mask);
  295. }
  296. static void pmc_intel_ack_status(u64 ack)
  297. {
  298. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  299. }
  300. static void pmc_amd_ack_status(u64 ack)
  301. {
  302. }
  303. static void hw_perf_ack_status(u64 ack)
  304. {
  305. if (unlikely(!perf_counters_initialized))
  306. return;
  307. pmc_ops->ack_status(ack);
  308. }
  309. static void pmc_intel_enable(int idx, u64 config)
  310. {
  311. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
  312. config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  313. }
  314. static void pmc_amd_enable(int idx, u64 config)
  315. {
  316. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  317. set_bit(idx, cpuc->active_mask);
  318. if (cpuc->enabled)
  319. config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  320. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  321. }
  322. static void hw_perf_enable(int idx, u64 config)
  323. {
  324. if (unlikely(!perf_counters_initialized))
  325. return;
  326. pmc_ops->enable(idx, config);
  327. }
  328. static void pmc_intel_disable(int idx, u64 config)
  329. {
  330. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
  331. }
  332. static void pmc_amd_disable(int idx, u64 config)
  333. {
  334. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  335. clear_bit(idx, cpuc->active_mask);
  336. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  337. }
  338. static void hw_perf_disable(int idx, u64 config)
  339. {
  340. if (unlikely(!perf_counters_initialized))
  341. return;
  342. pmc_ops->disable(idx, config);
  343. }
  344. static inline void
  345. __pmc_fixed_disable(struct perf_counter *counter,
  346. struct hw_perf_counter *hwc, unsigned int __idx)
  347. {
  348. int idx = __idx - X86_PMC_IDX_FIXED;
  349. u64 ctrl_val, mask;
  350. int err;
  351. mask = 0xfULL << (idx * 4);
  352. rdmsrl(hwc->config_base, ctrl_val);
  353. ctrl_val &= ~mask;
  354. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  355. }
  356. static inline void
  357. __pmc_generic_disable(struct perf_counter *counter,
  358. struct hw_perf_counter *hwc, unsigned int idx)
  359. {
  360. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  361. __pmc_fixed_disable(counter, hwc, idx);
  362. else
  363. hw_perf_disable(idx, hwc->config);
  364. }
  365. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  366. /*
  367. * Set the next IRQ period, based on the hwc->period_left value.
  368. * To be called with the counter disabled in hw:
  369. */
  370. static void
  371. __hw_perf_counter_set_period(struct perf_counter *counter,
  372. struct hw_perf_counter *hwc, int idx)
  373. {
  374. s64 left = atomic64_read(&hwc->period_left);
  375. s64 period = hwc->irq_period;
  376. int err;
  377. /*
  378. * If we are way outside a reasoable range then just skip forward:
  379. */
  380. if (unlikely(left <= -period)) {
  381. left = period;
  382. atomic64_set(&hwc->period_left, left);
  383. }
  384. if (unlikely(left <= 0)) {
  385. left += period;
  386. atomic64_set(&hwc->period_left, left);
  387. }
  388. per_cpu(prev_left[idx], smp_processor_id()) = left;
  389. /*
  390. * The hw counter starts counting from this counter offset,
  391. * mark it to be able to extra future deltas:
  392. */
  393. atomic64_set(&hwc->prev_count, (u64)-left);
  394. err = checking_wrmsrl(hwc->counter_base + idx,
  395. (u64)(-left) & counter_value_mask);
  396. }
  397. static inline void
  398. __pmc_fixed_enable(struct perf_counter *counter,
  399. struct hw_perf_counter *hwc, unsigned int __idx)
  400. {
  401. int idx = __idx - X86_PMC_IDX_FIXED;
  402. u64 ctrl_val, bits, mask;
  403. int err;
  404. /*
  405. * Enable IRQ generation (0x8),
  406. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  407. * if requested:
  408. */
  409. bits = 0x8ULL;
  410. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  411. bits |= 0x2;
  412. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  413. bits |= 0x1;
  414. bits <<= (idx * 4);
  415. mask = 0xfULL << (idx * 4);
  416. rdmsrl(hwc->config_base, ctrl_val);
  417. ctrl_val &= ~mask;
  418. ctrl_val |= bits;
  419. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  420. }
  421. static void
  422. __pmc_generic_enable(struct perf_counter *counter,
  423. struct hw_perf_counter *hwc, int idx)
  424. {
  425. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  426. __pmc_fixed_enable(counter, hwc, idx);
  427. else
  428. hw_perf_enable(idx, hwc->config);
  429. }
  430. static int
  431. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  432. {
  433. unsigned int event;
  434. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  435. return -1;
  436. if (unlikely(hwc->nmi))
  437. return -1;
  438. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  439. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
  440. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  441. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
  442. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  443. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
  444. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  445. return -1;
  446. }
  447. /*
  448. * Find a PMC slot for the freshly enabled / scheduled in counter:
  449. */
  450. static int pmc_generic_enable(struct perf_counter *counter)
  451. {
  452. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  453. struct hw_perf_counter *hwc = &counter->hw;
  454. int idx;
  455. idx = fixed_mode_idx(counter, hwc);
  456. if (idx >= 0) {
  457. /*
  458. * Try to get the fixed counter, if that is already taken
  459. * then try to get a generic counter:
  460. */
  461. if (test_and_set_bit(idx, cpuc->used))
  462. goto try_generic;
  463. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  464. /*
  465. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  466. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  467. */
  468. hwc->counter_base =
  469. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  470. hwc->idx = idx;
  471. } else {
  472. idx = hwc->idx;
  473. /* Try to get the previous generic counter again */
  474. if (test_and_set_bit(idx, cpuc->used)) {
  475. try_generic:
  476. idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
  477. if (idx == nr_counters_generic)
  478. return -EAGAIN;
  479. set_bit(idx, cpuc->used);
  480. hwc->idx = idx;
  481. }
  482. hwc->config_base = pmc_ops->eventsel;
  483. hwc->counter_base = pmc_ops->perfctr;
  484. }
  485. perf_counters_lapic_init(hwc->nmi);
  486. __pmc_generic_disable(counter, hwc, idx);
  487. cpuc->counters[idx] = counter;
  488. /*
  489. * Make it visible before enabling the hw:
  490. */
  491. smp_wmb();
  492. __hw_perf_counter_set_period(counter, hwc, idx);
  493. __pmc_generic_enable(counter, hwc, idx);
  494. return 0;
  495. }
  496. void perf_counter_print_debug(void)
  497. {
  498. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  499. struct cpu_hw_counters *cpuc;
  500. int cpu, idx;
  501. if (!nr_counters_generic)
  502. return;
  503. local_irq_disable();
  504. cpu = smp_processor_id();
  505. cpuc = &per_cpu(cpu_hw_counters, cpu);
  506. if (intel_perfmon_version >= 2) {
  507. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  508. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  509. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  510. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  511. pr_info("\n");
  512. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  513. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  514. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  515. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  516. }
  517. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  518. for (idx = 0; idx < nr_counters_generic; idx++) {
  519. rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
  520. rdmsrl(pmc_ops->perfctr + idx, pmc_count);
  521. prev_left = per_cpu(prev_left[idx], cpu);
  522. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  523. cpu, idx, pmc_ctrl);
  524. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  525. cpu, idx, pmc_count);
  526. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  527. cpu, idx, prev_left);
  528. }
  529. for (idx = 0; idx < nr_counters_fixed; idx++) {
  530. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  531. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  532. cpu, idx, pmc_count);
  533. }
  534. local_irq_enable();
  535. }
  536. static void pmc_generic_disable(struct perf_counter *counter)
  537. {
  538. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  539. struct hw_perf_counter *hwc = &counter->hw;
  540. unsigned int idx = hwc->idx;
  541. __pmc_generic_disable(counter, hwc, idx);
  542. clear_bit(idx, cpuc->used);
  543. cpuc->counters[idx] = NULL;
  544. /*
  545. * Make sure the cleared pointer becomes visible before we
  546. * (potentially) free the counter:
  547. */
  548. smp_wmb();
  549. /*
  550. * Drain the remaining delta count out of a counter
  551. * that we are disabling:
  552. */
  553. x86_perf_counter_update(counter, hwc, idx);
  554. }
  555. static void perf_store_irq_data(struct perf_counter *counter, u64 data)
  556. {
  557. struct perf_data *irqdata = counter->irqdata;
  558. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  559. irqdata->overrun++;
  560. } else {
  561. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  562. *p = data;
  563. irqdata->len += sizeof(u64);
  564. }
  565. }
  566. /*
  567. * Save and restart an expired counter. Called by NMI contexts,
  568. * so it has to be careful about preempting normal counter ops:
  569. */
  570. static void perf_save_and_restart(struct perf_counter *counter)
  571. {
  572. struct hw_perf_counter *hwc = &counter->hw;
  573. int idx = hwc->idx;
  574. x86_perf_counter_update(counter, hwc, idx);
  575. __hw_perf_counter_set_period(counter, hwc, idx);
  576. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  577. __pmc_generic_enable(counter, hwc, idx);
  578. }
  579. static void
  580. perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
  581. {
  582. struct perf_counter *counter, *group_leader = sibling->group_leader;
  583. /*
  584. * Store sibling timestamps (if any):
  585. */
  586. list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
  587. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  588. perf_store_irq_data(sibling, counter->hw_event.event_config);
  589. perf_store_irq_data(sibling, atomic64_read(&counter->count));
  590. }
  591. }
  592. /*
  593. * Maximum interrupt frequency of 100KHz per CPU
  594. */
  595. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  596. /*
  597. * This handler is triggered by the local APIC, so the APIC IRQ handling
  598. * rules apply:
  599. */
  600. static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
  601. {
  602. int bit, cpu = smp_processor_id();
  603. u64 ack, status;
  604. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  605. int ret = 0;
  606. cpuc->throttle_ctrl = hw_perf_save_disable();
  607. status = hw_perf_get_status(cpuc->throttle_ctrl);
  608. if (!status)
  609. goto out;
  610. ret = 1;
  611. again:
  612. inc_irq_stat(apic_perf_irqs);
  613. ack = status;
  614. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  615. struct perf_counter *counter = cpuc->counters[bit];
  616. clear_bit(bit, (unsigned long *) &status);
  617. if (!counter)
  618. continue;
  619. perf_save_and_restart(counter);
  620. switch (counter->hw_event.record_type) {
  621. case PERF_RECORD_SIMPLE:
  622. continue;
  623. case PERF_RECORD_IRQ:
  624. perf_store_irq_data(counter, instruction_pointer(regs));
  625. break;
  626. case PERF_RECORD_GROUP:
  627. perf_handle_group(counter, &status, &ack);
  628. break;
  629. }
  630. /*
  631. * From NMI context we cannot call into the scheduler to
  632. * do a task wakeup - but we mark these generic as
  633. * wakeup_pending and initate a wakeup callback:
  634. */
  635. if (nmi) {
  636. counter->wakeup_pending = 1;
  637. set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
  638. } else {
  639. wake_up(&counter->waitq);
  640. }
  641. }
  642. hw_perf_ack_status(ack);
  643. /*
  644. * Repeat if there is more work to be done:
  645. */
  646. status = hw_perf_get_status(cpuc->throttle_ctrl);
  647. if (status)
  648. goto again;
  649. out:
  650. /*
  651. * Restore - do not reenable when global enable is off or throttled:
  652. */
  653. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
  654. hw_perf_restore(cpuc->throttle_ctrl);
  655. return ret;
  656. }
  657. void perf_counter_unthrottle(void)
  658. {
  659. struct cpu_hw_counters *cpuc;
  660. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  661. return;
  662. if (unlikely(!perf_counters_initialized))
  663. return;
  664. cpuc = &__get_cpu_var(cpu_hw_counters);
  665. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  666. if (printk_ratelimit())
  667. printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
  668. hw_perf_restore(cpuc->throttle_ctrl);
  669. }
  670. cpuc->interrupts = 0;
  671. }
  672. void smp_perf_counter_interrupt(struct pt_regs *regs)
  673. {
  674. irq_enter();
  675. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  676. ack_APIC_irq();
  677. __smp_perf_counter_interrupt(regs, 0);
  678. irq_exit();
  679. }
  680. /*
  681. * This handler is triggered by NMI contexts:
  682. */
  683. void perf_counter_notify(struct pt_regs *regs)
  684. {
  685. struct cpu_hw_counters *cpuc;
  686. unsigned long flags;
  687. int bit, cpu;
  688. local_irq_save(flags);
  689. cpu = smp_processor_id();
  690. cpuc = &per_cpu(cpu_hw_counters, cpu);
  691. for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
  692. struct perf_counter *counter = cpuc->counters[bit];
  693. if (!counter)
  694. continue;
  695. if (counter->wakeup_pending) {
  696. counter->wakeup_pending = 0;
  697. wake_up(&counter->waitq);
  698. }
  699. }
  700. local_irq_restore(flags);
  701. }
  702. void perf_counters_lapic_init(int nmi)
  703. {
  704. u32 apic_val;
  705. if (!perf_counters_initialized)
  706. return;
  707. /*
  708. * Enable the performance counter vector in the APIC LVT:
  709. */
  710. apic_val = apic_read(APIC_LVTERR);
  711. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  712. if (nmi)
  713. apic_write(APIC_LVTPC, APIC_DM_NMI);
  714. else
  715. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  716. apic_write(APIC_LVTERR, apic_val);
  717. }
  718. static int __kprobes
  719. perf_counter_nmi_handler(struct notifier_block *self,
  720. unsigned long cmd, void *__args)
  721. {
  722. struct die_args *args = __args;
  723. struct pt_regs *regs;
  724. int ret;
  725. switch (cmd) {
  726. case DIE_NMI:
  727. case DIE_NMI_IPI:
  728. break;
  729. default:
  730. return NOTIFY_DONE;
  731. }
  732. regs = args->regs;
  733. apic_write(APIC_LVTPC, APIC_DM_NMI);
  734. ret = __smp_perf_counter_interrupt(regs, 1);
  735. return ret ? NOTIFY_STOP : NOTIFY_OK;
  736. }
  737. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  738. .notifier_call = perf_counter_nmi_handler,
  739. .next = NULL,
  740. .priority = 1
  741. };
  742. static struct pmc_x86_ops pmc_intel_ops = {
  743. .save_disable_all = pmc_intel_save_disable_all,
  744. .restore_all = pmc_intel_restore_all,
  745. .get_status = pmc_intel_get_status,
  746. .ack_status = pmc_intel_ack_status,
  747. .enable = pmc_intel_enable,
  748. .disable = pmc_intel_disable,
  749. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  750. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  751. .event_map = pmc_intel_event_map,
  752. .raw_event = pmc_intel_raw_event,
  753. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  754. };
  755. static struct pmc_x86_ops pmc_amd_ops = {
  756. .save_disable_all = pmc_amd_save_disable_all,
  757. .restore_all = pmc_amd_restore_all,
  758. .get_status = pmc_amd_get_status,
  759. .ack_status = pmc_amd_ack_status,
  760. .enable = pmc_amd_enable,
  761. .disable = pmc_amd_disable,
  762. .eventsel = MSR_K7_EVNTSEL0,
  763. .perfctr = MSR_K7_PERFCTR0,
  764. .event_map = pmc_amd_event_map,
  765. .raw_event = pmc_amd_raw_event,
  766. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  767. };
  768. static struct pmc_x86_ops *pmc_intel_init(void)
  769. {
  770. union cpuid10_edx edx;
  771. union cpuid10_eax eax;
  772. unsigned int unused;
  773. unsigned int ebx;
  774. /*
  775. * Check whether the Architectural PerfMon supports
  776. * Branch Misses Retired Event or not.
  777. */
  778. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  779. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  780. return NULL;
  781. intel_perfmon_version = eax.split.version_id;
  782. if (intel_perfmon_version < 2)
  783. return NULL;
  784. pr_info("Intel Performance Monitoring support detected.\n");
  785. pr_info("... version: %d\n", intel_perfmon_version);
  786. pr_info("... bit width: %d\n", eax.split.bit_width);
  787. pr_info("... mask length: %d\n", eax.split.mask_length);
  788. nr_counters_generic = eax.split.num_counters;
  789. nr_counters_fixed = edx.split.num_counters_fixed;
  790. counter_value_mask = (1ULL << eax.split.bit_width) - 1;
  791. return &pmc_intel_ops;
  792. }
  793. static struct pmc_x86_ops *pmc_amd_init(void)
  794. {
  795. nr_counters_generic = 4;
  796. nr_counters_fixed = 0;
  797. counter_value_mask = 0x0000FFFFFFFFFFFFULL;
  798. counter_value_bits = 48;
  799. pr_info("AMD Performance Monitoring support detected.\n");
  800. return &pmc_amd_ops;
  801. }
  802. void __init init_hw_perf_counters(void)
  803. {
  804. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  805. return;
  806. switch (boot_cpu_data.x86_vendor) {
  807. case X86_VENDOR_INTEL:
  808. pmc_ops = pmc_intel_init();
  809. break;
  810. case X86_VENDOR_AMD:
  811. pmc_ops = pmc_amd_init();
  812. break;
  813. }
  814. if (!pmc_ops)
  815. return;
  816. pr_info("... num counters: %d\n", nr_counters_generic);
  817. if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
  818. nr_counters_generic = X86_PMC_MAX_GENERIC;
  819. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  820. nr_counters_generic, X86_PMC_MAX_GENERIC);
  821. }
  822. perf_counter_mask = (1 << nr_counters_generic) - 1;
  823. perf_max_counters = nr_counters_generic;
  824. pr_info("... value mask: %016Lx\n", counter_value_mask);
  825. if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
  826. nr_counters_fixed = X86_PMC_MAX_FIXED;
  827. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  828. nr_counters_fixed, X86_PMC_MAX_FIXED);
  829. }
  830. pr_info("... fixed counters: %d\n", nr_counters_fixed);
  831. perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  832. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  833. perf_counters_initialized = true;
  834. perf_counters_lapic_init(0);
  835. register_die_notifier(&perf_counter_nmi_notifier);
  836. }
  837. static void pmc_generic_read(struct perf_counter *counter)
  838. {
  839. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  840. }
  841. static const struct hw_perf_counter_ops x86_perf_counter_ops = {
  842. .enable = pmc_generic_enable,
  843. .disable = pmc_generic_disable,
  844. .read = pmc_generic_read,
  845. };
  846. const struct hw_perf_counter_ops *
  847. hw_perf_counter_init(struct perf_counter *counter)
  848. {
  849. int err;
  850. err = __hw_perf_counter_init(counter);
  851. if (err)
  852. return NULL;
  853. return &x86_perf_counter_ops;
  854. }