perf_counter.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/perf_counter.h>
  10. #include <linux/capability.h>
  11. #include <linux/notifier.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/kprobes.h>
  14. #include <linux/module.h>
  15. #include <linux/kdebug.h>
  16. #include <linux/sched.h>
  17. #include <asm/perf_counter.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 nr_counters_fixed __read_mostly;
  27. struct cpu_hw_counters {
  28. struct perf_counter *counters[X86_PMC_IDX_MAX];
  29. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  30. unsigned long interrupts;
  31. u64 global_enable;
  32. };
  33. /*
  34. * Intel PerfMon v3. Used on Core2 and later.
  35. */
  36. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
  37. static const int intel_perfmon_event_map[] =
  38. {
  39. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  40. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  41. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  42. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  43. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  44. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  45. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  46. };
  47. static const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map);
  48. /*
  49. * Propagate counter elapsed time into the generic counter.
  50. * Can only be executed on the CPU where the counter is active.
  51. * Returns the delta events processed.
  52. */
  53. static void
  54. x86_perf_counter_update(struct perf_counter *counter,
  55. struct hw_perf_counter *hwc, int idx)
  56. {
  57. u64 prev_raw_count, new_raw_count, delta;
  58. /*
  59. * Careful: an NMI might modify the previous counter value.
  60. *
  61. * Our tactic to handle this is to first atomically read and
  62. * exchange a new raw count - then add that new-prev delta
  63. * count to the generic counter atomically:
  64. */
  65. again:
  66. prev_raw_count = atomic64_read(&hwc->prev_count);
  67. rdmsrl(hwc->counter_base + idx, new_raw_count);
  68. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  69. new_raw_count) != prev_raw_count)
  70. goto again;
  71. /*
  72. * Now we have the new raw value and have updated the prev
  73. * timestamp already. We can now calculate the elapsed delta
  74. * (counter-)time and add that to the generic counter.
  75. *
  76. * Careful, not all hw sign-extends above the physical width
  77. * of the count, so we do that by clipping the delta to 32 bits:
  78. */
  79. delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
  80. atomic64_add(delta, &counter->count);
  81. atomic64_sub(delta, &hwc->period_left);
  82. }
  83. /*
  84. * Setup the hardware configuration for a given hw_event_type
  85. */
  86. static int __hw_perf_counter_init(struct perf_counter *counter)
  87. {
  88. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  89. struct hw_perf_counter *hwc = &counter->hw;
  90. if (unlikely(!perf_counters_initialized))
  91. return -EINVAL;
  92. /*
  93. * Count user events, and generate PMC IRQs:
  94. * (keep 'enabled' bit clear for now)
  95. */
  96. hwc->config = ARCH_PERFMON_EVENTSEL_USR | ARCH_PERFMON_EVENTSEL_INT;
  97. /*
  98. * If privileged enough, count OS events too, and allow
  99. * NMI events as well:
  100. */
  101. hwc->nmi = 0;
  102. if (capable(CAP_SYS_ADMIN)) {
  103. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  104. if (hw_event->nmi)
  105. hwc->nmi = 1;
  106. }
  107. hwc->irq_period = hw_event->irq_period;
  108. /*
  109. * Intel PMCs cannot be accessed sanely above 32 bit width,
  110. * so we install an artificial 1<<31 period regardless of
  111. * the generic counter period:
  112. */
  113. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
  114. hwc->irq_period = 0x7FFFFFFF;
  115. atomic64_set(&hwc->period_left, hwc->irq_period);
  116. /*
  117. * Raw event type provide the config in the event structure
  118. */
  119. if (hw_event->raw) {
  120. hwc->config |= hw_event->type;
  121. } else {
  122. if (hw_event->type >= max_intel_perfmon_events)
  123. return -EINVAL;
  124. /*
  125. * The generic map:
  126. */
  127. hwc->config |= intel_perfmon_event_map[hw_event->type];
  128. }
  129. counter->wakeup_pending = 0;
  130. return 0;
  131. }
  132. u64 hw_perf_save_disable(void)
  133. {
  134. u64 ctrl;
  135. if (unlikely(!perf_counters_initialized))
  136. return 0;
  137. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  138. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  139. return ctrl;
  140. }
  141. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  142. void hw_perf_restore(u64 ctrl)
  143. {
  144. if (unlikely(!perf_counters_initialized))
  145. return;
  146. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  147. }
  148. EXPORT_SYMBOL_GPL(hw_perf_restore);
  149. static inline void
  150. __pmc_fixed_disable(struct perf_counter *counter,
  151. struct hw_perf_counter *hwc, unsigned int __idx)
  152. {
  153. int idx = __idx - X86_PMC_IDX_FIXED;
  154. u64 ctrl_val, mask;
  155. int err;
  156. mask = 0xfULL << (idx * 4);
  157. rdmsrl(hwc->config_base, ctrl_val);
  158. ctrl_val &= ~mask;
  159. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  160. }
  161. static inline void
  162. __pmc_generic_disable(struct perf_counter *counter,
  163. struct hw_perf_counter *hwc, unsigned int idx)
  164. {
  165. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  166. __pmc_fixed_disable(counter, hwc, idx);
  167. else
  168. wrmsr_safe(hwc->config_base + idx, hwc->config, 0);
  169. }
  170. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  171. /*
  172. * Set the next IRQ period, based on the hwc->period_left value.
  173. * To be called with the counter disabled in hw:
  174. */
  175. static void
  176. __hw_perf_counter_set_period(struct perf_counter *counter,
  177. struct hw_perf_counter *hwc, int idx)
  178. {
  179. s64 left = atomic64_read(&hwc->period_left);
  180. s32 period = hwc->irq_period;
  181. int err;
  182. /*
  183. * If we are way outside a reasoable range then just skip forward:
  184. */
  185. if (unlikely(left <= -period)) {
  186. left = period;
  187. atomic64_set(&hwc->period_left, left);
  188. }
  189. if (unlikely(left <= 0)) {
  190. left += period;
  191. atomic64_set(&hwc->period_left, left);
  192. }
  193. per_cpu(prev_left[idx], smp_processor_id()) = left;
  194. /*
  195. * The hw counter starts counting from this counter offset,
  196. * mark it to be able to extra future deltas:
  197. */
  198. atomic64_set(&hwc->prev_count, (u64)-left);
  199. err = checking_wrmsrl(hwc->counter_base + idx,
  200. (u64)(-left) & counter_value_mask);
  201. }
  202. static inline void
  203. __pmc_fixed_enable(struct perf_counter *counter,
  204. struct hw_perf_counter *hwc, unsigned int __idx)
  205. {
  206. int idx = __idx - X86_PMC_IDX_FIXED;
  207. u64 ctrl_val, bits, mask;
  208. int err;
  209. /*
  210. * Enable IRQ generation (0x8) and ring-3 counting (0x2),
  211. * and enable ring-0 counting if allowed:
  212. */
  213. bits = 0x8ULL | 0x2ULL;
  214. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  215. bits |= 0x1;
  216. bits <<= (idx * 4);
  217. mask = 0xfULL << (idx * 4);
  218. rdmsrl(hwc->config_base, ctrl_val);
  219. ctrl_val &= ~mask;
  220. ctrl_val |= bits;
  221. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  222. }
  223. static void
  224. __pmc_generic_enable(struct perf_counter *counter,
  225. struct hw_perf_counter *hwc, int idx)
  226. {
  227. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  228. __pmc_fixed_enable(counter, hwc, idx);
  229. else
  230. wrmsr(hwc->config_base + idx,
  231. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0);
  232. }
  233. static int
  234. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  235. {
  236. unsigned int event;
  237. if (unlikely(hwc->nmi))
  238. return -1;
  239. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  240. if (unlikely(event == intel_perfmon_event_map[PERF_COUNT_INSTRUCTIONS]))
  241. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  242. if (unlikely(event == intel_perfmon_event_map[PERF_COUNT_CPU_CYCLES]))
  243. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  244. if (unlikely(event == intel_perfmon_event_map[PERF_COUNT_BUS_CYCLES]))
  245. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  246. return -1;
  247. }
  248. /*
  249. * Find a PMC slot for the freshly enabled / scheduled in counter:
  250. */
  251. static int pmc_generic_enable(struct perf_counter *counter)
  252. {
  253. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  254. struct hw_perf_counter *hwc = &counter->hw;
  255. int idx;
  256. idx = fixed_mode_idx(counter, hwc);
  257. if (idx >= 0) {
  258. /*
  259. * Try to get the fixed counter, if that is already taken
  260. * then try to get a generic counter:
  261. */
  262. if (test_and_set_bit(idx, cpuc->used))
  263. goto try_generic;
  264. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  265. /*
  266. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  267. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  268. */
  269. hwc->counter_base =
  270. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  271. hwc->idx = idx;
  272. } else {
  273. idx = hwc->idx;
  274. /* Try to get the previous generic counter again */
  275. if (test_and_set_bit(idx, cpuc->used)) {
  276. try_generic:
  277. idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
  278. if (idx == nr_counters_generic)
  279. return -EAGAIN;
  280. set_bit(idx, cpuc->used);
  281. hwc->idx = idx;
  282. }
  283. hwc->config_base = MSR_ARCH_PERFMON_EVENTSEL0;
  284. hwc->counter_base = MSR_ARCH_PERFMON_PERFCTR0;
  285. }
  286. perf_counters_lapic_init(hwc->nmi);
  287. __pmc_generic_disable(counter, hwc, idx);
  288. cpuc->counters[idx] = counter;
  289. /*
  290. * Make it visible before enabling the hw:
  291. */
  292. smp_wmb();
  293. __hw_perf_counter_set_period(counter, hwc, idx);
  294. __pmc_generic_enable(counter, hwc, idx);
  295. return 0;
  296. }
  297. void perf_counter_print_debug(void)
  298. {
  299. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  300. struct cpu_hw_counters *cpuc;
  301. int cpu, idx;
  302. if (!nr_counters_generic)
  303. return;
  304. local_irq_disable();
  305. cpu = smp_processor_id();
  306. cpuc = &per_cpu(cpu_hw_counters, cpu);
  307. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  308. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  309. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  310. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  311. printk(KERN_INFO "\n");
  312. printk(KERN_INFO "CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  313. printk(KERN_INFO "CPU#%d: status: %016llx\n", cpu, status);
  314. printk(KERN_INFO "CPU#%d: overflow: %016llx\n", cpu, overflow);
  315. printk(KERN_INFO "CPU#%d: fixed: %016llx\n", cpu, fixed);
  316. printk(KERN_INFO "CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  317. for (idx = 0; idx < nr_counters_generic; idx++) {
  318. rdmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, pmc_ctrl);
  319. rdmsrl(MSR_ARCH_PERFMON_PERFCTR0 + idx, pmc_count);
  320. prev_left = per_cpu(prev_left[idx], cpu);
  321. printk(KERN_INFO "CPU#%d: gen-PMC%d ctrl: %016llx\n",
  322. cpu, idx, pmc_ctrl);
  323. printk(KERN_INFO "CPU#%d: gen-PMC%d count: %016llx\n",
  324. cpu, idx, pmc_count);
  325. printk(KERN_INFO "CPU#%d: gen-PMC%d left: %016llx\n",
  326. cpu, idx, prev_left);
  327. }
  328. for (idx = 0; idx < nr_counters_fixed; idx++) {
  329. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  330. printk(KERN_INFO "CPU#%d: fixed-PMC%d count: %016llx\n",
  331. cpu, idx, pmc_count);
  332. }
  333. local_irq_enable();
  334. }
  335. static void pmc_generic_disable(struct perf_counter *counter)
  336. {
  337. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  338. struct hw_perf_counter *hwc = &counter->hw;
  339. unsigned int idx = hwc->idx;
  340. __pmc_generic_disable(counter, hwc, idx);
  341. clear_bit(idx, cpuc->used);
  342. cpuc->counters[idx] = NULL;
  343. /*
  344. * Make sure the cleared pointer becomes visible before we
  345. * (potentially) free the counter:
  346. */
  347. smp_wmb();
  348. /*
  349. * Drain the remaining delta count out of a counter
  350. * that we are disabling:
  351. */
  352. x86_perf_counter_update(counter, hwc, idx);
  353. }
  354. static void perf_store_irq_data(struct perf_counter *counter, u64 data)
  355. {
  356. struct perf_data *irqdata = counter->irqdata;
  357. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  358. irqdata->overrun++;
  359. } else {
  360. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  361. *p = data;
  362. irqdata->len += sizeof(u64);
  363. }
  364. }
  365. /*
  366. * Save and restart an expired counter. Called by NMI contexts,
  367. * so it has to be careful about preempting normal counter ops:
  368. */
  369. static void perf_save_and_restart(struct perf_counter *counter)
  370. {
  371. struct hw_perf_counter *hwc = &counter->hw;
  372. int idx = hwc->idx;
  373. x86_perf_counter_update(counter, hwc, idx);
  374. __hw_perf_counter_set_period(counter, hwc, idx);
  375. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  376. __pmc_generic_enable(counter, hwc, idx);
  377. }
  378. static void
  379. perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
  380. {
  381. struct perf_counter *counter, *group_leader = sibling->group_leader;
  382. /*
  383. * Store sibling timestamps (if any):
  384. */
  385. list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
  386. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  387. perf_store_irq_data(sibling, counter->hw_event.type);
  388. perf_store_irq_data(sibling, atomic64_read(&counter->count));
  389. }
  390. }
  391. /*
  392. * Maximum interrupt frequency of 100KHz per CPU
  393. */
  394. #define PERFMON_MAX_INTERRUPTS 100000/HZ
  395. /*
  396. * This handler is triggered by the local APIC, so the APIC IRQ handling
  397. * rules apply:
  398. */
  399. static void __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
  400. {
  401. int bit, cpu = smp_processor_id();
  402. u64 ack, status;
  403. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  404. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, cpuc->global_enable);
  405. /* Disable counters globally */
  406. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  407. ack_APIC_irq();
  408. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  409. if (!status)
  410. goto out;
  411. again:
  412. ack = status;
  413. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  414. struct perf_counter *counter = cpuc->counters[bit];
  415. clear_bit(bit, (unsigned long *) &status);
  416. if (!counter)
  417. continue;
  418. perf_save_and_restart(counter);
  419. switch (counter->hw_event.record_type) {
  420. case PERF_RECORD_SIMPLE:
  421. continue;
  422. case PERF_RECORD_IRQ:
  423. perf_store_irq_data(counter, instruction_pointer(regs));
  424. break;
  425. case PERF_RECORD_GROUP:
  426. perf_handle_group(counter, &status, &ack);
  427. break;
  428. }
  429. /*
  430. * From NMI context we cannot call into the scheduler to
  431. * do a task wakeup - but we mark these generic as
  432. * wakeup_pending and initate a wakeup callback:
  433. */
  434. if (nmi) {
  435. counter->wakeup_pending = 1;
  436. set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
  437. } else {
  438. wake_up(&counter->waitq);
  439. }
  440. }
  441. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  442. /*
  443. * Repeat if there is more work to be done:
  444. */
  445. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  446. if (status)
  447. goto again;
  448. out:
  449. /*
  450. * Restore - do not reenable when global enable is off or throttled:
  451. */
  452. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
  453. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, cpuc->global_enable);
  454. }
  455. void perf_counter_unthrottle(void)
  456. {
  457. struct cpu_hw_counters *cpuc;
  458. u64 global_enable;
  459. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  460. return;
  461. if (unlikely(!perf_counters_initialized))
  462. return;
  463. cpuc = &per_cpu(cpu_hw_counters, smp_processor_id());
  464. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  465. if (printk_ratelimit())
  466. printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
  467. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, cpuc->global_enable);
  468. }
  469. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, global_enable);
  470. if (unlikely(cpuc->global_enable && !global_enable))
  471. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, cpuc->global_enable);
  472. cpuc->interrupts = 0;
  473. }
  474. void smp_perf_counter_interrupt(struct pt_regs *regs)
  475. {
  476. irq_enter();
  477. inc_irq_stat(apic_perf_irqs);
  478. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  479. __smp_perf_counter_interrupt(regs, 0);
  480. irq_exit();
  481. }
  482. /*
  483. * This handler is triggered by NMI contexts:
  484. */
  485. void perf_counter_notify(struct pt_regs *regs)
  486. {
  487. struct cpu_hw_counters *cpuc;
  488. unsigned long flags;
  489. int bit, cpu;
  490. local_irq_save(flags);
  491. cpu = smp_processor_id();
  492. cpuc = &per_cpu(cpu_hw_counters, cpu);
  493. for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
  494. struct perf_counter *counter = cpuc->counters[bit];
  495. if (!counter)
  496. continue;
  497. if (counter->wakeup_pending) {
  498. counter->wakeup_pending = 0;
  499. wake_up(&counter->waitq);
  500. }
  501. }
  502. local_irq_restore(flags);
  503. }
  504. void __cpuinit perf_counters_lapic_init(int nmi)
  505. {
  506. u32 apic_val;
  507. if (!perf_counters_initialized)
  508. return;
  509. /*
  510. * Enable the performance counter vector in the APIC LVT:
  511. */
  512. apic_val = apic_read(APIC_LVTERR);
  513. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  514. if (nmi)
  515. apic_write(APIC_LVTPC, APIC_DM_NMI);
  516. else
  517. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  518. apic_write(APIC_LVTERR, apic_val);
  519. }
  520. static int __kprobes
  521. perf_counter_nmi_handler(struct notifier_block *self,
  522. unsigned long cmd, void *__args)
  523. {
  524. struct die_args *args = __args;
  525. struct pt_regs *regs;
  526. if (likely(cmd != DIE_NMI_IPI))
  527. return NOTIFY_DONE;
  528. regs = args->regs;
  529. apic_write(APIC_LVTPC, APIC_DM_NMI);
  530. __smp_perf_counter_interrupt(regs, 1);
  531. return NOTIFY_STOP;
  532. }
  533. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  534. .notifier_call = perf_counter_nmi_handler
  535. };
  536. void __init init_hw_perf_counters(void)
  537. {
  538. union cpuid10_eax eax;
  539. unsigned int ebx;
  540. unsigned int unused;
  541. union cpuid10_edx edx;
  542. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  543. return;
  544. /*
  545. * Check whether the Architectural PerfMon supports
  546. * Branch Misses Retired Event or not.
  547. */
  548. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  549. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  550. return;
  551. printk(KERN_INFO "Intel Performance Monitoring support detected.\n");
  552. printk(KERN_INFO "... version: %d\n", eax.split.version_id);
  553. printk(KERN_INFO "... num counters: %d\n", eax.split.num_counters);
  554. nr_counters_generic = eax.split.num_counters;
  555. if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
  556. nr_counters_generic = X86_PMC_MAX_GENERIC;
  557. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  558. nr_counters_generic, X86_PMC_MAX_GENERIC);
  559. }
  560. perf_counter_mask = (1 << nr_counters_generic) - 1;
  561. perf_max_counters = nr_counters_generic;
  562. printk(KERN_INFO "... bit width: %d\n", eax.split.bit_width);
  563. counter_value_mask = (1ULL << eax.split.bit_width) - 1;
  564. printk(KERN_INFO "... value mask: %016Lx\n", counter_value_mask);
  565. printk(KERN_INFO "... mask length: %d\n", eax.split.mask_length);
  566. nr_counters_fixed = edx.split.num_counters_fixed;
  567. if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
  568. nr_counters_fixed = X86_PMC_MAX_FIXED;
  569. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  570. nr_counters_fixed, X86_PMC_MAX_FIXED);
  571. }
  572. printk(KERN_INFO "... fixed counters: %d\n", nr_counters_fixed);
  573. perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  574. printk(KERN_INFO "... counter mask: %016Lx\n", perf_counter_mask);
  575. perf_counters_initialized = true;
  576. perf_counters_lapic_init(0);
  577. register_die_notifier(&perf_counter_nmi_notifier);
  578. }
  579. static void pmc_generic_read(struct perf_counter *counter)
  580. {
  581. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  582. }
  583. static const struct hw_perf_counter_ops x86_perf_counter_ops = {
  584. .enable = pmc_generic_enable,
  585. .disable = pmc_generic_disable,
  586. .read = pmc_generic_read,
  587. };
  588. const struct hw_perf_counter_ops *
  589. hw_perf_counter_init(struct perf_counter *counter)
  590. {
  591. int err;
  592. err = __hw_perf_counter_init(counter);
  593. if (err)
  594. return NULL;
  595. return &x86_perf_counter_ops;
  596. }