perf_counter.c 18 KB

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