perf_counter.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 (hw_event->raw_type) {
  188. hwc->config |= pmc_ops->raw_event(hw_event->raw_event_id);
  189. } else {
  190. if (hw_event->event_id >= pmc_ops->max_events)
  191. return -EINVAL;
  192. /*
  193. * The generic map:
  194. */
  195. hwc->config |= pmc_ops->event_map(hw_event->event_id);
  196. }
  197. counter->wakeup_pending = 0;
  198. return 0;
  199. }
  200. static u64 pmc_intel_save_disable_all(void)
  201. {
  202. u64 ctrl;
  203. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  204. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  205. return ctrl;
  206. }
  207. static u64 pmc_amd_save_disable_all(void)
  208. {
  209. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  210. int enabled, idx;
  211. enabled = cpuc->enabled;
  212. cpuc->enabled = 0;
  213. /*
  214. * ensure we write the disable before we start disabling the
  215. * counters proper, so that pcm_amd_enable() does the right thing.
  216. */
  217. barrier();
  218. for (idx = 0; idx < nr_counters_generic; idx++) {
  219. u64 val;
  220. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  221. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
  222. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  223. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  224. }
  225. }
  226. return enabled;
  227. }
  228. u64 hw_perf_save_disable(void)
  229. {
  230. if (unlikely(!perf_counters_initialized))
  231. return 0;
  232. return pmc_ops->save_disable_all();
  233. }
  234. /*
  235. * Exported because of ACPI idle
  236. */
  237. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  238. static void pmc_intel_restore_all(u64 ctrl)
  239. {
  240. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  241. }
  242. static void pmc_amd_restore_all(u64 ctrl)
  243. {
  244. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  245. int idx;
  246. cpuc->enabled = ctrl;
  247. barrier();
  248. if (!ctrl)
  249. return;
  250. for (idx = 0; idx < nr_counters_generic; idx++) {
  251. if (test_bit(idx, cpuc->active_mask)) {
  252. u64 val;
  253. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  254. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  255. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  256. }
  257. }
  258. }
  259. void hw_perf_restore(u64 ctrl)
  260. {
  261. if (unlikely(!perf_counters_initialized))
  262. return;
  263. pmc_ops->restore_all(ctrl);
  264. }
  265. /*
  266. * Exported because of ACPI idle
  267. */
  268. EXPORT_SYMBOL_GPL(hw_perf_restore);
  269. static u64 pmc_intel_get_status(u64 mask)
  270. {
  271. u64 status;
  272. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  273. return status;
  274. }
  275. static u64 pmc_amd_get_status(u64 mask)
  276. {
  277. u64 status = 0;
  278. int idx;
  279. for (idx = 0; idx < nr_counters_generic; idx++) {
  280. s64 val;
  281. if (!(mask & (1 << idx)))
  282. continue;
  283. rdmsrl(MSR_K7_PERFCTR0 + idx, val);
  284. val <<= (64 - counter_value_bits);
  285. if (val >= 0)
  286. status |= (1 << idx);
  287. }
  288. return status;
  289. }
  290. static u64 hw_perf_get_status(u64 mask)
  291. {
  292. if (unlikely(!perf_counters_initialized))
  293. return 0;
  294. return pmc_ops->get_status(mask);
  295. }
  296. static void pmc_intel_ack_status(u64 ack)
  297. {
  298. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  299. }
  300. static void pmc_amd_ack_status(u64 ack)
  301. {
  302. }
  303. static void hw_perf_ack_status(u64 ack)
  304. {
  305. if (unlikely(!perf_counters_initialized))
  306. return;
  307. pmc_ops->ack_status(ack);
  308. }
  309. static void pmc_intel_enable(int idx, u64 config)
  310. {
  311. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
  312. config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  313. }
  314. static void pmc_amd_enable(int idx, u64 config)
  315. {
  316. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  317. set_bit(idx, cpuc->active_mask);
  318. if (cpuc->enabled)
  319. config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  320. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  321. }
  322. static void hw_perf_enable(int idx, u64 config)
  323. {
  324. if (unlikely(!perf_counters_initialized))
  325. return;
  326. pmc_ops->enable(idx, config);
  327. }
  328. static void pmc_intel_disable(int idx, u64 config)
  329. {
  330. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
  331. }
  332. static void pmc_amd_disable(int idx, u64 config)
  333. {
  334. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  335. clear_bit(idx, cpuc->active_mask);
  336. wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
  337. }
  338. static void hw_perf_disable(int idx, u64 config)
  339. {
  340. if (unlikely(!perf_counters_initialized))
  341. return;
  342. pmc_ops->disable(idx, config);
  343. }
  344. static inline void
  345. __pmc_fixed_disable(struct perf_counter *counter,
  346. struct hw_perf_counter *hwc, unsigned int __idx)
  347. {
  348. int idx = __idx - X86_PMC_IDX_FIXED;
  349. u64 ctrl_val, mask;
  350. int err;
  351. mask = 0xfULL << (idx * 4);
  352. rdmsrl(hwc->config_base, ctrl_val);
  353. ctrl_val &= ~mask;
  354. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  355. }
  356. static inline void
  357. __pmc_generic_disable(struct perf_counter *counter,
  358. struct hw_perf_counter *hwc, unsigned int idx)
  359. {
  360. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  361. __pmc_fixed_disable(counter, hwc, idx);
  362. else
  363. hw_perf_disable(idx, hwc->config);
  364. }
  365. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  366. /*
  367. * Set the next IRQ period, based on the hwc->period_left value.
  368. * To be called with the counter disabled in hw:
  369. */
  370. static void
  371. __hw_perf_counter_set_period(struct perf_counter *counter,
  372. struct hw_perf_counter *hwc, int idx)
  373. {
  374. s64 left = atomic64_read(&hwc->period_left);
  375. s64 period = hwc->irq_period;
  376. int err;
  377. /*
  378. * If we are way outside a reasoable range then just skip forward:
  379. */
  380. if (unlikely(left <= -period)) {
  381. left = period;
  382. atomic64_set(&hwc->period_left, left);
  383. }
  384. if (unlikely(left <= 0)) {
  385. left += period;
  386. atomic64_set(&hwc->period_left, left);
  387. }
  388. per_cpu(prev_left[idx], smp_processor_id()) = left;
  389. /*
  390. * The hw counter starts counting from this counter offset,
  391. * mark it to be able to extra future deltas:
  392. */
  393. atomic64_set(&hwc->prev_count, (u64)-left);
  394. err = checking_wrmsrl(hwc->counter_base + idx,
  395. (u64)(-left) & counter_value_mask);
  396. }
  397. static inline void
  398. __pmc_fixed_enable(struct perf_counter *counter,
  399. struct hw_perf_counter *hwc, unsigned int __idx)
  400. {
  401. int idx = __idx - X86_PMC_IDX_FIXED;
  402. u64 ctrl_val, bits, mask;
  403. int err;
  404. /*
  405. * Enable IRQ generation (0x8),
  406. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  407. * if requested:
  408. */
  409. bits = 0x8ULL;
  410. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  411. bits |= 0x2;
  412. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  413. bits |= 0x1;
  414. bits <<= (idx * 4);
  415. mask = 0xfULL << (idx * 4);
  416. rdmsrl(hwc->config_base, ctrl_val);
  417. ctrl_val &= ~mask;
  418. ctrl_val |= bits;
  419. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  420. }
  421. static void
  422. __pmc_generic_enable(struct perf_counter *counter,
  423. struct hw_perf_counter *hwc, int idx)
  424. {
  425. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
  426. __pmc_fixed_enable(counter, hwc, idx);
  427. else
  428. hw_perf_enable(idx, hwc->config);
  429. }
  430. static int
  431. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  432. {
  433. unsigned int event;
  434. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  435. return -1;
  436. if (unlikely(hwc->nmi))
  437. return -1;
  438. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  439. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
  440. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  441. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
  442. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  443. if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
  444. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  445. return -1;
  446. }
  447. /*
  448. * Find a PMC slot for the freshly enabled / scheduled in counter:
  449. */
  450. static int pmc_generic_enable(struct perf_counter *counter)
  451. {
  452. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  453. struct hw_perf_counter *hwc = &counter->hw;
  454. int idx;
  455. idx = fixed_mode_idx(counter, hwc);
  456. if (idx >= 0) {
  457. /*
  458. * Try to get the fixed counter, if that is already taken
  459. * then try to get a generic counter:
  460. */
  461. if (test_and_set_bit(idx, cpuc->used))
  462. goto try_generic;
  463. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  464. /*
  465. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  466. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  467. */
  468. hwc->counter_base =
  469. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  470. hwc->idx = idx;
  471. } else {
  472. idx = hwc->idx;
  473. /* Try to get the previous generic counter again */
  474. if (test_and_set_bit(idx, cpuc->used)) {
  475. try_generic:
  476. idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
  477. if (idx == nr_counters_generic)
  478. return -EAGAIN;
  479. set_bit(idx, cpuc->used);
  480. hwc->idx = idx;
  481. }
  482. hwc->config_base = pmc_ops->eventsel;
  483. hwc->counter_base = pmc_ops->perfctr;
  484. }
  485. perf_counters_lapic_init(hwc->nmi);
  486. __pmc_generic_disable(counter, hwc, idx);
  487. cpuc->counters[idx] = counter;
  488. /*
  489. * Make it visible before enabling the hw:
  490. */
  491. smp_wmb();
  492. __hw_perf_counter_set_period(counter, hwc, idx);
  493. __pmc_generic_enable(counter, hwc, idx);
  494. return 0;
  495. }
  496. void perf_counter_print_debug(void)
  497. {
  498. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  499. struct cpu_hw_counters *cpuc;
  500. int cpu, idx;
  501. if (!nr_counters_generic)
  502. return;
  503. local_irq_disable();
  504. cpu = smp_processor_id();
  505. cpuc = &per_cpu(cpu_hw_counters, cpu);
  506. if (intel_perfmon_version >= 2) {
  507. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  508. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  509. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  510. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  511. pr_info("\n");
  512. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  513. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  514. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  515. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  516. }
  517. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
  518. for (idx = 0; idx < nr_counters_generic; idx++) {
  519. rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
  520. rdmsrl(pmc_ops->perfctr + idx, pmc_count);
  521. prev_left = per_cpu(prev_left[idx], cpu);
  522. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  523. cpu, idx, pmc_ctrl);
  524. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  525. cpu, idx, pmc_count);
  526. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  527. cpu, idx, prev_left);
  528. }
  529. for (idx = 0; idx < nr_counters_fixed; idx++) {
  530. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  531. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  532. cpu, idx, pmc_count);
  533. }
  534. local_irq_enable();
  535. }
  536. static void pmc_generic_disable(struct perf_counter *counter)
  537. {
  538. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  539. struct hw_perf_counter *hwc = &counter->hw;
  540. unsigned int idx = hwc->idx;
  541. __pmc_generic_disable(counter, hwc, idx);
  542. clear_bit(idx, cpuc->used);
  543. cpuc->counters[idx] = NULL;
  544. /*
  545. * Make sure the cleared pointer becomes visible before we
  546. * (potentially) free the counter:
  547. */
  548. smp_wmb();
  549. /*
  550. * Drain the remaining delta count out of a counter
  551. * that we are disabling:
  552. */
  553. x86_perf_counter_update(counter, hwc, idx);
  554. }
  555. /*
  556. * Save and restart an expired counter. Called by NMI contexts,
  557. * so it has to be careful about preempting normal counter ops:
  558. */
  559. static void perf_save_and_restart(struct perf_counter *counter)
  560. {
  561. struct hw_perf_counter *hwc = &counter->hw;
  562. int idx = hwc->idx;
  563. x86_perf_counter_update(counter, hwc, idx);
  564. __hw_perf_counter_set_period(counter, hwc, idx);
  565. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  566. __pmc_generic_enable(counter, hwc, idx);
  567. }
  568. /*
  569. * Maximum interrupt frequency of 100KHz per CPU
  570. */
  571. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  572. /*
  573. * This handler is triggered by the local APIC, so the APIC IRQ handling
  574. * rules apply:
  575. */
  576. static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
  577. {
  578. int bit, cpu = smp_processor_id();
  579. u64 ack, status;
  580. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  581. int ret = 0;
  582. cpuc->throttle_ctrl = hw_perf_save_disable();
  583. status = hw_perf_get_status(cpuc->throttle_ctrl);
  584. if (!status)
  585. goto out;
  586. ret = 1;
  587. again:
  588. inc_irq_stat(apic_perf_irqs);
  589. ack = status;
  590. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  591. struct perf_counter *counter = cpuc->counters[bit];
  592. clear_bit(bit, (unsigned long *) &status);
  593. if (!counter)
  594. continue;
  595. perf_save_and_restart(counter);
  596. perf_counter_output(counter, nmi, regs);
  597. }
  598. hw_perf_ack_status(ack);
  599. /*
  600. * Repeat if there is more work to be done:
  601. */
  602. status = hw_perf_get_status(cpuc->throttle_ctrl);
  603. if (status)
  604. goto again;
  605. out:
  606. /*
  607. * Restore - do not reenable when global enable is off or throttled:
  608. */
  609. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
  610. hw_perf_restore(cpuc->throttle_ctrl);
  611. return ret;
  612. }
  613. void perf_counter_unthrottle(void)
  614. {
  615. struct cpu_hw_counters *cpuc;
  616. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  617. return;
  618. if (unlikely(!perf_counters_initialized))
  619. return;
  620. cpuc = &__get_cpu_var(cpu_hw_counters);
  621. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  622. if (printk_ratelimit())
  623. printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
  624. hw_perf_restore(cpuc->throttle_ctrl);
  625. }
  626. cpuc->interrupts = 0;
  627. }
  628. void smp_perf_counter_interrupt(struct pt_regs *regs)
  629. {
  630. irq_enter();
  631. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  632. ack_APIC_irq();
  633. __smp_perf_counter_interrupt(regs, 0);
  634. irq_exit();
  635. }
  636. /*
  637. * This handler is triggered by NMI contexts:
  638. */
  639. void perf_counter_notify(struct pt_regs *regs)
  640. {
  641. struct cpu_hw_counters *cpuc;
  642. unsigned long flags;
  643. int bit, cpu;
  644. local_irq_save(flags);
  645. cpu = smp_processor_id();
  646. cpuc = &per_cpu(cpu_hw_counters, cpu);
  647. for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
  648. struct perf_counter *counter = cpuc->counters[bit];
  649. if (!counter)
  650. continue;
  651. if (counter->wakeup_pending) {
  652. counter->wakeup_pending = 0;
  653. wake_up(&counter->waitq);
  654. }
  655. }
  656. local_irq_restore(flags);
  657. }
  658. void perf_counters_lapic_init(int nmi)
  659. {
  660. u32 apic_val;
  661. if (!perf_counters_initialized)
  662. return;
  663. /*
  664. * Enable the performance counter vector in the APIC LVT:
  665. */
  666. apic_val = apic_read(APIC_LVTERR);
  667. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  668. if (nmi)
  669. apic_write(APIC_LVTPC, APIC_DM_NMI);
  670. else
  671. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  672. apic_write(APIC_LVTERR, apic_val);
  673. }
  674. static int __kprobes
  675. perf_counter_nmi_handler(struct notifier_block *self,
  676. unsigned long cmd, void *__args)
  677. {
  678. struct die_args *args = __args;
  679. struct pt_regs *regs;
  680. int ret;
  681. switch (cmd) {
  682. case DIE_NMI:
  683. case DIE_NMI_IPI:
  684. break;
  685. default:
  686. return NOTIFY_DONE;
  687. }
  688. regs = args->regs;
  689. apic_write(APIC_LVTPC, APIC_DM_NMI);
  690. ret = __smp_perf_counter_interrupt(regs, 1);
  691. return ret ? NOTIFY_STOP : NOTIFY_OK;
  692. }
  693. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  694. .notifier_call = perf_counter_nmi_handler,
  695. .next = NULL,
  696. .priority = 1
  697. };
  698. static struct pmc_x86_ops pmc_intel_ops = {
  699. .save_disable_all = pmc_intel_save_disable_all,
  700. .restore_all = pmc_intel_restore_all,
  701. .get_status = pmc_intel_get_status,
  702. .ack_status = pmc_intel_ack_status,
  703. .enable = pmc_intel_enable,
  704. .disable = pmc_intel_disable,
  705. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  706. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  707. .event_map = pmc_intel_event_map,
  708. .raw_event = pmc_intel_raw_event,
  709. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  710. };
  711. static struct pmc_x86_ops pmc_amd_ops = {
  712. .save_disable_all = pmc_amd_save_disable_all,
  713. .restore_all = pmc_amd_restore_all,
  714. .get_status = pmc_amd_get_status,
  715. .ack_status = pmc_amd_ack_status,
  716. .enable = pmc_amd_enable,
  717. .disable = pmc_amd_disable,
  718. .eventsel = MSR_K7_EVNTSEL0,
  719. .perfctr = MSR_K7_PERFCTR0,
  720. .event_map = pmc_amd_event_map,
  721. .raw_event = pmc_amd_raw_event,
  722. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  723. };
  724. static struct pmc_x86_ops *pmc_intel_init(void)
  725. {
  726. union cpuid10_edx edx;
  727. union cpuid10_eax eax;
  728. unsigned int unused;
  729. unsigned int ebx;
  730. /*
  731. * Check whether the Architectural PerfMon supports
  732. * Branch Misses Retired Event or not.
  733. */
  734. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  735. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  736. return NULL;
  737. intel_perfmon_version = eax.split.version_id;
  738. if (intel_perfmon_version < 2)
  739. return NULL;
  740. pr_info("Intel Performance Monitoring support detected.\n");
  741. pr_info("... version: %d\n", intel_perfmon_version);
  742. pr_info("... bit width: %d\n", eax.split.bit_width);
  743. pr_info("... mask length: %d\n", eax.split.mask_length);
  744. nr_counters_generic = eax.split.num_counters;
  745. nr_counters_fixed = edx.split.num_counters_fixed;
  746. counter_value_mask = (1ULL << eax.split.bit_width) - 1;
  747. return &pmc_intel_ops;
  748. }
  749. static struct pmc_x86_ops *pmc_amd_init(void)
  750. {
  751. nr_counters_generic = 4;
  752. nr_counters_fixed = 0;
  753. counter_value_mask = 0x0000FFFFFFFFFFFFULL;
  754. counter_value_bits = 48;
  755. pr_info("AMD Performance Monitoring support detected.\n");
  756. return &pmc_amd_ops;
  757. }
  758. void __init init_hw_perf_counters(void)
  759. {
  760. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  761. return;
  762. switch (boot_cpu_data.x86_vendor) {
  763. case X86_VENDOR_INTEL:
  764. pmc_ops = pmc_intel_init();
  765. break;
  766. case X86_VENDOR_AMD:
  767. pmc_ops = pmc_amd_init();
  768. break;
  769. }
  770. if (!pmc_ops)
  771. return;
  772. pr_info("... num counters: %d\n", nr_counters_generic);
  773. if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
  774. nr_counters_generic = X86_PMC_MAX_GENERIC;
  775. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  776. nr_counters_generic, X86_PMC_MAX_GENERIC);
  777. }
  778. perf_counter_mask = (1 << nr_counters_generic) - 1;
  779. perf_max_counters = nr_counters_generic;
  780. pr_info("... value mask: %016Lx\n", counter_value_mask);
  781. if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
  782. nr_counters_fixed = X86_PMC_MAX_FIXED;
  783. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  784. nr_counters_fixed, X86_PMC_MAX_FIXED);
  785. }
  786. pr_info("... fixed counters: %d\n", nr_counters_fixed);
  787. perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  788. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  789. perf_counters_initialized = true;
  790. perf_counters_lapic_init(0);
  791. register_die_notifier(&perf_counter_nmi_notifier);
  792. }
  793. static void pmc_generic_read(struct perf_counter *counter)
  794. {
  795. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  796. }
  797. static const struct hw_perf_counter_ops x86_perf_counter_ops = {
  798. .enable = pmc_generic_enable,
  799. .disable = pmc_generic_disable,
  800. .read = pmc_generic_read,
  801. };
  802. const struct hw_perf_counter_ops *
  803. hw_perf_counter_init(struct perf_counter *counter)
  804. {
  805. int err;
  806. err = __hw_perf_counter_init(counter);
  807. if (err)
  808. return NULL;
  809. return &x86_perf_counter_ops;
  810. }