perf_counter.c 23 KB

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