perf_counter.c 28 KB

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