perf_counter.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Performance counter x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. *
  10. * For licencing details see kernel-base/COPYING
  11. */
  12. #include <linux/perf_counter.h>
  13. #include <linux/capability.h>
  14. #include <linux/notifier.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/apic.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/nmi.h>
  24. static u64 perf_counter_mask __read_mostly;
  25. struct cpu_hw_counters {
  26. struct perf_counter *counters[X86_PMC_IDX_MAX];
  27. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  28. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. unsigned long interrupts;
  30. u64 throttle_ctrl;
  31. int enabled;
  32. };
  33. /*
  34. * struct x86_pmu - generic x86 pmu
  35. */
  36. struct x86_pmu {
  37. const char *name;
  38. int version;
  39. int (*handle_irq)(struct pt_regs *, int);
  40. u64 (*save_disable_all)(void);
  41. void (*restore_all)(u64);
  42. void (*enable)(struct hw_perf_counter *, int);
  43. void (*disable)(struct hw_perf_counter *, int);
  44. unsigned eventsel;
  45. unsigned perfctr;
  46. u64 (*event_map)(int);
  47. u64 (*raw_event)(u64);
  48. int max_events;
  49. int num_counters;
  50. int num_counters_fixed;
  51. int counter_bits;
  52. u64 counter_mask;
  53. u64 max_period;
  54. };
  55. static struct x86_pmu x86_pmu __read_mostly;
  56. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  57. .enabled = 1,
  58. };
  59. /*
  60. * Intel PerfMon v3. Used on Core2 and later.
  61. */
  62. static const u64 intel_perfmon_event_map[] =
  63. {
  64. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  65. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  66. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  67. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  68. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  69. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  70. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  71. };
  72. static u64 intel_pmu_event_map(int event)
  73. {
  74. return intel_perfmon_event_map[event];
  75. }
  76. static u64 intel_pmu_raw_event(u64 event)
  77. {
  78. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  79. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  80. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  81. #define CORE_EVNTSEL_MASK \
  82. (CORE_EVNTSEL_EVENT_MASK | \
  83. CORE_EVNTSEL_UNIT_MASK | \
  84. CORE_EVNTSEL_COUNTER_MASK)
  85. return event & CORE_EVNTSEL_MASK;
  86. }
  87. /*
  88. * AMD Performance Monitor K7 and later.
  89. */
  90. static const u64 amd_perfmon_event_map[] =
  91. {
  92. [PERF_COUNT_CPU_CYCLES] = 0x0076,
  93. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  94. [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
  95. [PERF_COUNT_CACHE_MISSES] = 0x0081,
  96. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  97. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  98. };
  99. static u64 amd_pmu_event_map(int event)
  100. {
  101. return amd_perfmon_event_map[event];
  102. }
  103. static u64 amd_pmu_raw_event(u64 event)
  104. {
  105. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  106. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  107. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  108. #define K7_EVNTSEL_MASK \
  109. (K7_EVNTSEL_EVENT_MASK | \
  110. K7_EVNTSEL_UNIT_MASK | \
  111. K7_EVNTSEL_COUNTER_MASK)
  112. return event & K7_EVNTSEL_MASK;
  113. }
  114. /*
  115. * Propagate counter elapsed time into the generic counter.
  116. * Can only be executed on the CPU where the counter is active.
  117. * Returns the delta events processed.
  118. */
  119. static u64
  120. x86_perf_counter_update(struct perf_counter *counter,
  121. struct hw_perf_counter *hwc, int idx)
  122. {
  123. int shift = 64 - x86_pmu.counter_bits;
  124. u64 prev_raw_count, new_raw_count;
  125. s64 delta;
  126. /*
  127. * Careful: an NMI might modify the previous counter value.
  128. *
  129. * Our tactic to handle this is to first atomically read and
  130. * exchange a new raw count - then add that new-prev delta
  131. * count to the generic counter atomically:
  132. */
  133. again:
  134. prev_raw_count = atomic64_read(&hwc->prev_count);
  135. rdmsrl(hwc->counter_base + idx, new_raw_count);
  136. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  137. new_raw_count) != prev_raw_count)
  138. goto again;
  139. /*
  140. * Now we have the new raw value and have updated the prev
  141. * timestamp already. We can now calculate the elapsed delta
  142. * (counter-)time and add that to the generic counter.
  143. *
  144. * Careful, not all hw sign-extends above the physical width
  145. * of the count.
  146. */
  147. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  148. delta >>= shift;
  149. atomic64_add(delta, &counter->count);
  150. atomic64_sub(delta, &hwc->period_left);
  151. return new_raw_count;
  152. }
  153. static atomic_t active_counters;
  154. static DEFINE_MUTEX(pmc_reserve_mutex);
  155. static bool reserve_pmc_hardware(void)
  156. {
  157. int i;
  158. if (nmi_watchdog == NMI_LOCAL_APIC)
  159. disable_lapic_nmi_watchdog();
  160. for (i = 0; i < x86_pmu.num_counters; i++) {
  161. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  162. goto perfctr_fail;
  163. }
  164. for (i = 0; i < x86_pmu.num_counters; i++) {
  165. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  166. goto eventsel_fail;
  167. }
  168. return true;
  169. eventsel_fail:
  170. for (i--; i >= 0; i--)
  171. release_evntsel_nmi(x86_pmu.eventsel + i);
  172. i = x86_pmu.num_counters;
  173. perfctr_fail:
  174. for (i--; i >= 0; i--)
  175. release_perfctr_nmi(x86_pmu.perfctr + i);
  176. if (nmi_watchdog == NMI_LOCAL_APIC)
  177. enable_lapic_nmi_watchdog();
  178. return false;
  179. }
  180. static void release_pmc_hardware(void)
  181. {
  182. int i;
  183. for (i = 0; i < x86_pmu.num_counters; i++) {
  184. release_perfctr_nmi(x86_pmu.perfctr + i);
  185. release_evntsel_nmi(x86_pmu.eventsel + i);
  186. }
  187. if (nmi_watchdog == NMI_LOCAL_APIC)
  188. enable_lapic_nmi_watchdog();
  189. }
  190. static void hw_perf_counter_destroy(struct perf_counter *counter)
  191. {
  192. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  193. release_pmc_hardware();
  194. mutex_unlock(&pmc_reserve_mutex);
  195. }
  196. }
  197. static inline int x86_pmu_initialized(void)
  198. {
  199. return x86_pmu.handle_irq != NULL;
  200. }
  201. /*
  202. * Setup the hardware configuration for a given hw_event_type
  203. */
  204. static int __hw_perf_counter_init(struct perf_counter *counter)
  205. {
  206. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  207. struct hw_perf_counter *hwc = &counter->hw;
  208. int err;
  209. if (!x86_pmu_initialized())
  210. return -ENODEV;
  211. err = 0;
  212. if (!atomic_inc_not_zero(&active_counters)) {
  213. mutex_lock(&pmc_reserve_mutex);
  214. if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
  215. err = -EBUSY;
  216. else
  217. atomic_inc(&active_counters);
  218. mutex_unlock(&pmc_reserve_mutex);
  219. }
  220. if (err)
  221. return err;
  222. /*
  223. * Generate PMC IRQs:
  224. * (keep 'enabled' bit clear for now)
  225. */
  226. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  227. /*
  228. * Count user and OS events unless requested not to.
  229. */
  230. if (!hw_event->exclude_user)
  231. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  232. if (!hw_event->exclude_kernel)
  233. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  234. /*
  235. * If privileged enough, allow NMI events:
  236. */
  237. hwc->nmi = 0;
  238. if (hw_event->nmi) {
  239. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  240. return -EACCES;
  241. hwc->nmi = 1;
  242. }
  243. hwc->irq_period = hw_event->irq_period;
  244. if ((s64)hwc->irq_period <= 0 || hwc->irq_period > x86_pmu.max_period)
  245. hwc->irq_period = x86_pmu.max_period;
  246. atomic64_set(&hwc->period_left, hwc->irq_period);
  247. /*
  248. * Raw event type provide the config in the event structure
  249. */
  250. if (perf_event_raw(hw_event)) {
  251. hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
  252. } else {
  253. if (perf_event_id(hw_event) >= x86_pmu.max_events)
  254. return -EINVAL;
  255. /*
  256. * The generic map:
  257. */
  258. hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
  259. }
  260. counter->destroy = hw_perf_counter_destroy;
  261. return 0;
  262. }
  263. static u64 intel_pmu_save_disable_all(void)
  264. {
  265. u64 ctrl;
  266. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  267. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  268. return ctrl;
  269. }
  270. static u64 amd_pmu_save_disable_all(void)
  271. {
  272. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  273. int enabled, idx;
  274. enabled = cpuc->enabled;
  275. cpuc->enabled = 0;
  276. /*
  277. * ensure we write the disable before we start disabling the
  278. * counters proper, so that amd_pmu_enable_counter() does the
  279. * right thing.
  280. */
  281. barrier();
  282. if (!enabled)
  283. goto out;
  284. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  285. u64 val;
  286. if (!test_bit(idx, cpuc->active_mask))
  287. continue;
  288. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  289. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  290. continue;
  291. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  292. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  293. }
  294. out:
  295. return enabled;
  296. }
  297. u64 hw_perf_save_disable(void)
  298. {
  299. if (!x86_pmu_initialized())
  300. return 0;
  301. return x86_pmu.save_disable_all();
  302. }
  303. /*
  304. * Exported because of ACPI idle
  305. */
  306. EXPORT_SYMBOL_GPL(hw_perf_save_disable);
  307. static void intel_pmu_restore_all(u64 ctrl)
  308. {
  309. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  310. }
  311. static void amd_pmu_restore_all(u64 ctrl)
  312. {
  313. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  314. int idx;
  315. cpuc->enabled = ctrl;
  316. barrier();
  317. if (!ctrl)
  318. return;
  319. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  320. u64 val;
  321. if (!test_bit(idx, cpuc->active_mask))
  322. continue;
  323. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  324. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  325. continue;
  326. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  327. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  328. }
  329. }
  330. void hw_perf_restore(u64 ctrl)
  331. {
  332. if (!x86_pmu_initialized())
  333. return;
  334. x86_pmu.restore_all(ctrl);
  335. }
  336. /*
  337. * Exported because of ACPI idle
  338. */
  339. EXPORT_SYMBOL_GPL(hw_perf_restore);
  340. static inline u64 intel_pmu_get_status(void)
  341. {
  342. u64 status;
  343. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  344. return status;
  345. }
  346. static inline void intel_pmu_ack_status(u64 ack)
  347. {
  348. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  349. }
  350. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  351. {
  352. int err;
  353. err = checking_wrmsrl(hwc->config_base + idx,
  354. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  355. }
  356. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  357. {
  358. int err;
  359. err = checking_wrmsrl(hwc->config_base + idx,
  360. hwc->config);
  361. }
  362. static inline void
  363. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  364. {
  365. int idx = __idx - X86_PMC_IDX_FIXED;
  366. u64 ctrl_val, mask;
  367. int err;
  368. mask = 0xfULL << (idx * 4);
  369. rdmsrl(hwc->config_base, ctrl_val);
  370. ctrl_val &= ~mask;
  371. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  372. }
  373. static inline void
  374. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  375. {
  376. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  377. intel_pmu_disable_fixed(hwc, idx);
  378. return;
  379. }
  380. x86_pmu_disable_counter(hwc, idx);
  381. }
  382. static inline void
  383. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  384. {
  385. x86_pmu_disable_counter(hwc, idx);
  386. }
  387. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  388. /*
  389. * Set the next IRQ period, based on the hwc->period_left value.
  390. * To be called with the counter disabled in hw:
  391. */
  392. static void
  393. x86_perf_counter_set_period(struct perf_counter *counter,
  394. struct hw_perf_counter *hwc, int idx)
  395. {
  396. s64 left = atomic64_read(&hwc->period_left);
  397. s64 period = hwc->irq_period;
  398. int err;
  399. /*
  400. * If we are way outside a reasoable range then just skip forward:
  401. */
  402. if (unlikely(left <= -period)) {
  403. left = period;
  404. atomic64_set(&hwc->period_left, left);
  405. }
  406. if (unlikely(left <= 0)) {
  407. left += period;
  408. atomic64_set(&hwc->period_left, left);
  409. }
  410. per_cpu(prev_left[idx], smp_processor_id()) = left;
  411. /*
  412. * The hw counter starts counting from this counter offset,
  413. * mark it to be able to extra future deltas:
  414. */
  415. atomic64_set(&hwc->prev_count, (u64)-left);
  416. err = checking_wrmsrl(hwc->counter_base + idx,
  417. (u64)(-left) & x86_pmu.counter_mask);
  418. }
  419. static inline void
  420. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  421. {
  422. int idx = __idx - X86_PMC_IDX_FIXED;
  423. u64 ctrl_val, bits, mask;
  424. int err;
  425. /*
  426. * Enable IRQ generation (0x8),
  427. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  428. * if requested:
  429. */
  430. bits = 0x8ULL;
  431. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  432. bits |= 0x2;
  433. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  434. bits |= 0x1;
  435. bits <<= (idx * 4);
  436. mask = 0xfULL << (idx * 4);
  437. rdmsrl(hwc->config_base, ctrl_val);
  438. ctrl_val &= ~mask;
  439. ctrl_val |= bits;
  440. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  441. }
  442. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  443. {
  444. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  445. intel_pmu_enable_fixed(hwc, idx);
  446. return;
  447. }
  448. x86_pmu_enable_counter(hwc, idx);
  449. }
  450. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  451. {
  452. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  453. if (cpuc->enabled)
  454. x86_pmu_enable_counter(hwc, idx);
  455. else
  456. x86_pmu_disable_counter(hwc, idx);
  457. }
  458. static int
  459. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  460. {
  461. unsigned int event;
  462. if (!x86_pmu.num_counters_fixed)
  463. return -1;
  464. if (unlikely(hwc->nmi))
  465. return -1;
  466. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  467. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  468. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  469. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  470. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  471. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  472. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  473. return -1;
  474. }
  475. /*
  476. * Find a PMC slot for the freshly enabled / scheduled in counter:
  477. */
  478. static int x86_pmu_enable(struct perf_counter *counter)
  479. {
  480. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  481. struct hw_perf_counter *hwc = &counter->hw;
  482. int idx;
  483. idx = fixed_mode_idx(counter, hwc);
  484. if (idx >= 0) {
  485. /*
  486. * Try to get the fixed counter, if that is already taken
  487. * then try to get a generic counter:
  488. */
  489. if (test_and_set_bit(idx, cpuc->used_mask))
  490. goto try_generic;
  491. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  492. /*
  493. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  494. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  495. */
  496. hwc->counter_base =
  497. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  498. hwc->idx = idx;
  499. } else {
  500. idx = hwc->idx;
  501. /* Try to get the previous generic counter again */
  502. if (test_and_set_bit(idx, cpuc->used_mask)) {
  503. try_generic:
  504. idx = find_first_zero_bit(cpuc->used_mask,
  505. x86_pmu.num_counters);
  506. if (idx == x86_pmu.num_counters)
  507. return -EAGAIN;
  508. set_bit(idx, cpuc->used_mask);
  509. hwc->idx = idx;
  510. }
  511. hwc->config_base = x86_pmu.eventsel;
  512. hwc->counter_base = x86_pmu.perfctr;
  513. }
  514. perf_counters_lapic_init(hwc->nmi);
  515. x86_pmu.disable(hwc, idx);
  516. cpuc->counters[idx] = counter;
  517. set_bit(idx, cpuc->active_mask);
  518. x86_perf_counter_set_period(counter, hwc, idx);
  519. x86_pmu.enable(hwc, idx);
  520. return 0;
  521. }
  522. void perf_counter_print_debug(void)
  523. {
  524. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  525. struct cpu_hw_counters *cpuc;
  526. unsigned long flags;
  527. int cpu, idx;
  528. if (!x86_pmu.num_counters)
  529. return;
  530. local_irq_save(flags);
  531. cpu = smp_processor_id();
  532. cpuc = &per_cpu(cpu_hw_counters, cpu);
  533. if (x86_pmu.version >= 2) {
  534. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  535. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  536. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  537. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  538. pr_info("\n");
  539. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  540. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  541. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  542. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  543. }
  544. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  545. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  546. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  547. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  548. prev_left = per_cpu(prev_left[idx], cpu);
  549. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  550. cpu, idx, pmc_ctrl);
  551. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  552. cpu, idx, pmc_count);
  553. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  554. cpu, idx, prev_left);
  555. }
  556. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  557. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  558. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  559. cpu, idx, pmc_count);
  560. }
  561. local_irq_restore(flags);
  562. }
  563. static void x86_pmu_disable(struct perf_counter *counter)
  564. {
  565. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  566. struct hw_perf_counter *hwc = &counter->hw;
  567. int idx = hwc->idx;
  568. /*
  569. * Must be done before we disable, otherwise the nmi handler
  570. * could reenable again:
  571. */
  572. clear_bit(idx, cpuc->active_mask);
  573. x86_pmu.disable(hwc, idx);
  574. /*
  575. * Make sure the cleared pointer becomes visible before we
  576. * (potentially) free the counter:
  577. */
  578. barrier();
  579. /*
  580. * Drain the remaining delta count out of a counter
  581. * that we are disabling:
  582. */
  583. x86_perf_counter_update(counter, hwc, idx);
  584. cpuc->counters[idx] = NULL;
  585. clear_bit(idx, cpuc->used_mask);
  586. }
  587. /*
  588. * Save and restart an expired counter. Called by NMI contexts,
  589. * so it has to be careful about preempting normal counter ops:
  590. */
  591. static void intel_pmu_save_and_restart(struct perf_counter *counter)
  592. {
  593. struct hw_perf_counter *hwc = &counter->hw;
  594. int idx = hwc->idx;
  595. x86_perf_counter_update(counter, hwc, idx);
  596. x86_perf_counter_set_period(counter, hwc, idx);
  597. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  598. intel_pmu_enable_counter(hwc, idx);
  599. }
  600. /*
  601. * Maximum interrupt frequency of 100KHz per CPU
  602. */
  603. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  604. /*
  605. * This handler is triggered by the local APIC, so the APIC IRQ handling
  606. * rules apply:
  607. */
  608. static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
  609. {
  610. int bit, cpu = smp_processor_id();
  611. u64 ack, status;
  612. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  613. int ret = 0;
  614. cpuc->throttle_ctrl = intel_pmu_save_disable_all();
  615. status = intel_pmu_get_status();
  616. if (!status)
  617. goto out;
  618. ret = 1;
  619. again:
  620. inc_irq_stat(apic_perf_irqs);
  621. ack = status;
  622. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  623. struct perf_counter *counter = cpuc->counters[bit];
  624. clear_bit(bit, (unsigned long *) &status);
  625. if (!test_bit(bit, cpuc->active_mask))
  626. continue;
  627. intel_pmu_save_and_restart(counter);
  628. if (perf_counter_overflow(counter, nmi, regs, 0))
  629. intel_pmu_disable_counter(&counter->hw, bit);
  630. }
  631. intel_pmu_ack_status(ack);
  632. /*
  633. * Repeat if there is more work to be done:
  634. */
  635. status = intel_pmu_get_status();
  636. if (status)
  637. goto again;
  638. out:
  639. /*
  640. * Restore - do not reenable when global enable is off or throttled:
  641. */
  642. if (cpuc->throttle_ctrl) {
  643. if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS) {
  644. intel_pmu_restore_all(cpuc->throttle_ctrl);
  645. } else {
  646. pr_info("CPU#%d: perfcounters: max interrupt rate exceeded! Throttle on.\n", smp_processor_id());
  647. }
  648. }
  649. return ret;
  650. }
  651. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
  652. {
  653. int cpu = smp_processor_id();
  654. struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
  655. u64 val;
  656. int handled = 0;
  657. struct perf_counter *counter;
  658. struct hw_perf_counter *hwc;
  659. int idx, throttle = 0;
  660. cpuc->throttle_ctrl = cpuc->enabled;
  661. cpuc->enabled = 0;
  662. barrier();
  663. if (cpuc->throttle_ctrl) {
  664. if (++cpuc->interrupts >= PERFMON_MAX_INTERRUPTS)
  665. throttle = 1;
  666. }
  667. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  668. int disable = 0;
  669. if (!test_bit(idx, cpuc->active_mask))
  670. continue;
  671. counter = cpuc->counters[idx];
  672. hwc = &counter->hw;
  673. val = x86_perf_counter_update(counter, hwc, idx);
  674. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  675. goto next;
  676. /* counter overflow */
  677. x86_perf_counter_set_period(counter, hwc, idx);
  678. handled = 1;
  679. inc_irq_stat(apic_perf_irqs);
  680. disable = perf_counter_overflow(counter, nmi, regs, 0);
  681. next:
  682. if (disable || throttle)
  683. amd_pmu_disable_counter(hwc, idx);
  684. }
  685. if (cpuc->throttle_ctrl && !throttle)
  686. cpuc->enabled = 1;
  687. return handled;
  688. }
  689. void perf_counter_unthrottle(void)
  690. {
  691. struct cpu_hw_counters *cpuc;
  692. if (!x86_pmu_initialized())
  693. return;
  694. cpuc = &__get_cpu_var(cpu_hw_counters);
  695. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  696. pr_info("CPU#%d: perfcounters: throttle off.\n", smp_processor_id());
  697. /*
  698. * Clear them before re-enabling irqs/NMIs again:
  699. */
  700. cpuc->interrupts = 0;
  701. hw_perf_restore(cpuc->throttle_ctrl);
  702. } else {
  703. cpuc->interrupts = 0;
  704. }
  705. }
  706. void smp_perf_counter_interrupt(struct pt_regs *regs)
  707. {
  708. irq_enter();
  709. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  710. ack_APIC_irq();
  711. x86_pmu.handle_irq(regs, 0);
  712. irq_exit();
  713. }
  714. void smp_perf_pending_interrupt(struct pt_regs *regs)
  715. {
  716. irq_enter();
  717. ack_APIC_irq();
  718. inc_irq_stat(apic_pending_irqs);
  719. perf_counter_do_pending();
  720. irq_exit();
  721. }
  722. void set_perf_counter_pending(void)
  723. {
  724. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  725. }
  726. void perf_counters_lapic_init(int nmi)
  727. {
  728. u32 apic_val;
  729. if (!x86_pmu_initialized())
  730. return;
  731. /*
  732. * Enable the performance counter vector in the APIC LVT:
  733. */
  734. apic_val = apic_read(APIC_LVTERR);
  735. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  736. if (nmi)
  737. apic_write(APIC_LVTPC, APIC_DM_NMI);
  738. else
  739. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  740. apic_write(APIC_LVTERR, apic_val);
  741. }
  742. static int __kprobes
  743. perf_counter_nmi_handler(struct notifier_block *self,
  744. unsigned long cmd, void *__args)
  745. {
  746. struct die_args *args = __args;
  747. struct pt_regs *regs;
  748. int ret;
  749. if (!atomic_read(&active_counters))
  750. return NOTIFY_DONE;
  751. switch (cmd) {
  752. case DIE_NMI:
  753. case DIE_NMI_IPI:
  754. break;
  755. default:
  756. return NOTIFY_DONE;
  757. }
  758. regs = args->regs;
  759. apic_write(APIC_LVTPC, APIC_DM_NMI);
  760. ret = x86_pmu.handle_irq(regs, 1);
  761. return ret ? NOTIFY_STOP : NOTIFY_OK;
  762. }
  763. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  764. .notifier_call = perf_counter_nmi_handler,
  765. .next = NULL,
  766. .priority = 1
  767. };
  768. static struct x86_pmu intel_pmu = {
  769. .name = "Intel",
  770. .handle_irq = intel_pmu_handle_irq,
  771. .save_disable_all = intel_pmu_save_disable_all,
  772. .restore_all = intel_pmu_restore_all,
  773. .enable = intel_pmu_enable_counter,
  774. .disable = intel_pmu_disable_counter,
  775. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  776. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  777. .event_map = intel_pmu_event_map,
  778. .raw_event = intel_pmu_raw_event,
  779. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  780. /*
  781. * Intel PMCs cannot be accessed sanely above 32 bit width,
  782. * so we install an artificial 1<<31 period regardless of
  783. * the generic counter period:
  784. */
  785. .max_period = (1ULL << 31) - 1,
  786. };
  787. static struct x86_pmu amd_pmu = {
  788. .name = "AMD",
  789. .handle_irq = amd_pmu_handle_irq,
  790. .save_disable_all = amd_pmu_save_disable_all,
  791. .restore_all = amd_pmu_restore_all,
  792. .enable = amd_pmu_enable_counter,
  793. .disable = amd_pmu_disable_counter,
  794. .eventsel = MSR_K7_EVNTSEL0,
  795. .perfctr = MSR_K7_PERFCTR0,
  796. .event_map = amd_pmu_event_map,
  797. .raw_event = amd_pmu_raw_event,
  798. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  799. .num_counters = 4,
  800. .counter_bits = 48,
  801. .counter_mask = (1ULL << 48) - 1,
  802. /* use highest bit to detect overflow */
  803. .max_period = (1ULL << 47) - 1,
  804. };
  805. static int intel_pmu_init(void)
  806. {
  807. union cpuid10_edx edx;
  808. union cpuid10_eax eax;
  809. unsigned int unused;
  810. unsigned int ebx;
  811. int version;
  812. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  813. return -ENODEV;
  814. /*
  815. * Check whether the Architectural PerfMon supports
  816. * Branch Misses Retired Event or not.
  817. */
  818. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  819. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  820. return -ENODEV;
  821. version = eax.split.version_id;
  822. if (version < 2)
  823. return -ENODEV;
  824. x86_pmu = intel_pmu;
  825. x86_pmu.version = version;
  826. x86_pmu.num_counters = eax.split.num_counters;
  827. /*
  828. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  829. * assume at least 3 counters:
  830. */
  831. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  832. x86_pmu.counter_bits = eax.split.bit_width;
  833. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  834. return 0;
  835. }
  836. static int amd_pmu_init(void)
  837. {
  838. x86_pmu = amd_pmu;
  839. return 0;
  840. }
  841. void __init init_hw_perf_counters(void)
  842. {
  843. int err;
  844. switch (boot_cpu_data.x86_vendor) {
  845. case X86_VENDOR_INTEL:
  846. err = intel_pmu_init();
  847. break;
  848. case X86_VENDOR_AMD:
  849. err = amd_pmu_init();
  850. break;
  851. default:
  852. return;
  853. }
  854. if (err != 0)
  855. return;
  856. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  857. pr_info("... version: %d\n", x86_pmu.version);
  858. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  859. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  860. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  861. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  862. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  863. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  864. }
  865. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  866. perf_max_counters = x86_pmu.num_counters;
  867. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  868. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  869. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  870. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  871. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  872. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  873. }
  874. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  875. perf_counter_mask |=
  876. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  877. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  878. perf_counters_lapic_init(0);
  879. register_die_notifier(&perf_counter_nmi_notifier);
  880. }
  881. static inline void x86_pmu_read(struct perf_counter *counter)
  882. {
  883. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  884. }
  885. static const struct pmu pmu = {
  886. .enable = x86_pmu_enable,
  887. .disable = x86_pmu_disable,
  888. .read = x86_pmu_read,
  889. };
  890. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  891. {
  892. int err;
  893. err = __hw_perf_counter_init(counter);
  894. if (err)
  895. return ERR_PTR(err);
  896. return &pmu;
  897. }
  898. /*
  899. * callchain support
  900. */
  901. static inline
  902. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  903. {
  904. if (entry->nr < MAX_STACK_DEPTH)
  905. entry->ip[entry->nr++] = ip;
  906. }
  907. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  908. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  909. static void
  910. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  911. {
  912. /* Ignore warnings */
  913. }
  914. static void backtrace_warning(void *data, char *msg)
  915. {
  916. /* Ignore warnings */
  917. }
  918. static int backtrace_stack(void *data, char *name)
  919. {
  920. /* Don't bother with IRQ stacks for now */
  921. return -1;
  922. }
  923. static void backtrace_address(void *data, unsigned long addr, int reliable)
  924. {
  925. struct perf_callchain_entry *entry = data;
  926. if (reliable)
  927. callchain_store(entry, addr);
  928. }
  929. static const struct stacktrace_ops backtrace_ops = {
  930. .warning = backtrace_warning,
  931. .warning_symbol = backtrace_warning_symbol,
  932. .stack = backtrace_stack,
  933. .address = backtrace_address,
  934. };
  935. static void
  936. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  937. {
  938. unsigned long bp;
  939. char *stack;
  940. int nr = entry->nr;
  941. callchain_store(entry, instruction_pointer(regs));
  942. stack = ((char *)regs + sizeof(struct pt_regs));
  943. #ifdef CONFIG_FRAME_POINTER
  944. bp = frame_pointer(regs);
  945. #else
  946. bp = 0;
  947. #endif
  948. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  949. entry->kernel = entry->nr - nr;
  950. }
  951. struct stack_frame {
  952. const void __user *next_fp;
  953. unsigned long return_address;
  954. };
  955. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  956. {
  957. int ret;
  958. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  959. return 0;
  960. ret = 1;
  961. pagefault_disable();
  962. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  963. ret = 0;
  964. pagefault_enable();
  965. return ret;
  966. }
  967. static void
  968. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  969. {
  970. struct stack_frame frame;
  971. const void __user *fp;
  972. int nr = entry->nr;
  973. regs = (struct pt_regs *)current->thread.sp0 - 1;
  974. fp = (void __user *)regs->bp;
  975. callchain_store(entry, regs->ip);
  976. while (entry->nr < MAX_STACK_DEPTH) {
  977. frame.next_fp = NULL;
  978. frame.return_address = 0;
  979. if (!copy_stack_frame(fp, &frame))
  980. break;
  981. if ((unsigned long)fp < user_stack_pointer(regs))
  982. break;
  983. callchain_store(entry, frame.return_address);
  984. fp = frame.next_fp;
  985. }
  986. entry->user = entry->nr - nr;
  987. }
  988. static void
  989. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  990. {
  991. int is_user;
  992. if (!regs)
  993. return;
  994. is_user = user_mode(regs);
  995. if (!current || current->pid == 0)
  996. return;
  997. if (is_user && current->state != TASK_RUNNING)
  998. return;
  999. if (!is_user)
  1000. perf_callchain_kernel(regs, entry);
  1001. if (current->mm)
  1002. perf_callchain_user(regs, entry);
  1003. }
  1004. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1005. {
  1006. struct perf_callchain_entry *entry;
  1007. if (in_nmi())
  1008. entry = &__get_cpu_var(nmi_entry);
  1009. else
  1010. entry = &__get_cpu_var(irq_entry);
  1011. entry->nr = 0;
  1012. entry->hv = 0;
  1013. entry->kernel = 0;
  1014. entry->user = 0;
  1015. perf_do_callchain(regs, entry);
  1016. return entry;
  1017. }