perf_counter.c 21 KB

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