perf_counter.c 17 KB

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