perf_counter.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * Performance counter support - powerpc architecture code
  3. *
  4. * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/perf_counter.h>
  14. #include <linux/percpu.h>
  15. #include <linux/hardirq.h>
  16. #include <asm/reg.h>
  17. #include <asm/pmc.h>
  18. #include <asm/machdep.h>
  19. #include <asm/firmware.h>
  20. #include <asm/ptrace.h>
  21. struct cpu_hw_counters {
  22. int n_counters;
  23. int n_percpu;
  24. int disabled;
  25. int n_added;
  26. int n_limited;
  27. u8 pmcs_enabled;
  28. struct perf_counter *counter[MAX_HWCOUNTERS];
  29. u64 events[MAX_HWCOUNTERS];
  30. unsigned int flags[MAX_HWCOUNTERS];
  31. unsigned long mmcr[3];
  32. struct perf_counter *limited_counter[MAX_LIMITED_HWCOUNTERS];
  33. u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
  34. };
  35. DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
  36. struct power_pmu *ppmu;
  37. /*
  38. * Normally, to ignore kernel events we set the FCS (freeze counters
  39. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  40. * hypervisor bit set in the MSR, or if we are running on a processor
  41. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  42. * then we need to use the FCHV bit to ignore kernel events.
  43. */
  44. static unsigned int freeze_counters_kernel = MMCR0_FCS;
  45. /*
  46. * 32-bit doesn't have MMCRA but does have an MMCR2,
  47. * and a few other names are different.
  48. */
  49. #ifdef CONFIG_PPC32
  50. #define MMCR0_FCHV 0
  51. #define MMCR0_PMCjCE MMCR0_PMCnCE
  52. #define SPRN_MMCRA SPRN_MMCR2
  53. #define MMCRA_SAMPLE_ENABLE 0
  54. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  55. {
  56. return 0;
  57. }
  58. static inline void perf_set_pmu_inuse(int inuse) { }
  59. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
  60. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  61. {
  62. return 0;
  63. }
  64. static inline void perf_read_regs(struct pt_regs *regs) { }
  65. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  66. {
  67. return 0;
  68. }
  69. #endif /* CONFIG_PPC32 */
  70. /*
  71. * Things that are specific to 64-bit implementations.
  72. */
  73. #ifdef CONFIG_PPC64
  74. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  75. {
  76. unsigned long mmcra = regs->dsisr;
  77. if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
  78. unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
  79. if (slot > 1)
  80. return 4 * (slot - 1);
  81. }
  82. return 0;
  83. }
  84. static inline void perf_set_pmu_inuse(int inuse)
  85. {
  86. get_lppaca()->pmcregs_in_use = inuse;
  87. }
  88. /*
  89. * The user wants a data address recorded.
  90. * If we're not doing instruction sampling, give them the SDAR
  91. * (sampled data address). If we are doing instruction sampling, then
  92. * only give them the SDAR if it corresponds to the instruction
  93. * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
  94. * bit in MMCRA.
  95. */
  96. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
  97. {
  98. unsigned long mmcra = regs->dsisr;
  99. unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
  100. POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
  101. if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
  102. *addrp = mfspr(SPRN_SDAR);
  103. }
  104. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  105. {
  106. unsigned long mmcra = regs->dsisr;
  107. if (TRAP(regs) != 0xf00)
  108. return 0; /* not a PMU interrupt */
  109. if (ppmu->flags & PPMU_ALT_SIPR) {
  110. if (mmcra & POWER6_MMCRA_SIHV)
  111. return PERF_EVENT_MISC_HYPERVISOR;
  112. return (mmcra & POWER6_MMCRA_SIPR) ?
  113. PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
  114. }
  115. if (mmcra & MMCRA_SIHV)
  116. return PERF_EVENT_MISC_HYPERVISOR;
  117. return (mmcra & MMCRA_SIPR) ? PERF_EVENT_MISC_USER :
  118. PERF_EVENT_MISC_KERNEL;
  119. }
  120. /*
  121. * Overload regs->dsisr to store MMCRA so we only need to read it once
  122. * on each interrupt.
  123. */
  124. static inline void perf_read_regs(struct pt_regs *regs)
  125. {
  126. regs->dsisr = mfspr(SPRN_MMCRA);
  127. }
  128. /*
  129. * If interrupts were soft-disabled when a PMU interrupt occurs, treat
  130. * it as an NMI.
  131. */
  132. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  133. {
  134. return !regs->softe;
  135. }
  136. #endif /* CONFIG_PPC64 */
  137. static void perf_counter_interrupt(struct pt_regs *regs);
  138. void perf_counter_print_debug(void)
  139. {
  140. }
  141. /*
  142. * Read one performance monitor counter (PMC).
  143. */
  144. static unsigned long read_pmc(int idx)
  145. {
  146. unsigned long val;
  147. switch (idx) {
  148. case 1:
  149. val = mfspr(SPRN_PMC1);
  150. break;
  151. case 2:
  152. val = mfspr(SPRN_PMC2);
  153. break;
  154. case 3:
  155. val = mfspr(SPRN_PMC3);
  156. break;
  157. case 4:
  158. val = mfspr(SPRN_PMC4);
  159. break;
  160. case 5:
  161. val = mfspr(SPRN_PMC5);
  162. break;
  163. case 6:
  164. val = mfspr(SPRN_PMC6);
  165. break;
  166. #ifdef CONFIG_PPC64
  167. case 7:
  168. val = mfspr(SPRN_PMC7);
  169. break;
  170. case 8:
  171. val = mfspr(SPRN_PMC8);
  172. break;
  173. #endif /* CONFIG_PPC64 */
  174. default:
  175. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  176. val = 0;
  177. }
  178. return val;
  179. }
  180. /*
  181. * Write one PMC.
  182. */
  183. static void write_pmc(int idx, unsigned long val)
  184. {
  185. switch (idx) {
  186. case 1:
  187. mtspr(SPRN_PMC1, val);
  188. break;
  189. case 2:
  190. mtspr(SPRN_PMC2, val);
  191. break;
  192. case 3:
  193. mtspr(SPRN_PMC3, val);
  194. break;
  195. case 4:
  196. mtspr(SPRN_PMC4, val);
  197. break;
  198. case 5:
  199. mtspr(SPRN_PMC5, val);
  200. break;
  201. case 6:
  202. mtspr(SPRN_PMC6, val);
  203. break;
  204. #ifdef CONFIG_PPC64
  205. case 7:
  206. mtspr(SPRN_PMC7, val);
  207. break;
  208. case 8:
  209. mtspr(SPRN_PMC8, val);
  210. break;
  211. #endif /* CONFIG_PPC64 */
  212. default:
  213. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  214. }
  215. }
  216. /*
  217. * Check if a set of events can all go on the PMU at once.
  218. * If they can't, this will look at alternative codes for the events
  219. * and see if any combination of alternative codes is feasible.
  220. * The feasible set is returned in event[].
  221. */
  222. static int power_check_constraints(u64 event[], unsigned int cflags[],
  223. int n_ev)
  224. {
  225. unsigned long mask, value, nv;
  226. u64 alternatives[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  227. unsigned long amasks[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  228. unsigned long avalues[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  229. unsigned long smasks[MAX_HWCOUNTERS], svalues[MAX_HWCOUNTERS];
  230. int n_alt[MAX_HWCOUNTERS], choice[MAX_HWCOUNTERS];
  231. int i, j;
  232. unsigned long addf = ppmu->add_fields;
  233. unsigned long tadd = ppmu->test_adder;
  234. if (n_ev > ppmu->n_counter)
  235. return -1;
  236. /* First see if the events will go on as-is */
  237. for (i = 0; i < n_ev; ++i) {
  238. if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
  239. && !ppmu->limited_pmc_event(event[i])) {
  240. ppmu->get_alternatives(event[i], cflags[i],
  241. alternatives[i]);
  242. event[i] = alternatives[i][0];
  243. }
  244. if (ppmu->get_constraint(event[i], &amasks[i][0],
  245. &avalues[i][0]))
  246. return -1;
  247. }
  248. value = mask = 0;
  249. for (i = 0; i < n_ev; ++i) {
  250. nv = (value | avalues[i][0]) + (value & avalues[i][0] & addf);
  251. if ((((nv + tadd) ^ value) & mask) != 0 ||
  252. (((nv + tadd) ^ avalues[i][0]) & amasks[i][0]) != 0)
  253. break;
  254. value = nv;
  255. mask |= amasks[i][0];
  256. }
  257. if (i == n_ev)
  258. return 0; /* all OK */
  259. /* doesn't work, gather alternatives... */
  260. if (!ppmu->get_alternatives)
  261. return -1;
  262. for (i = 0; i < n_ev; ++i) {
  263. choice[i] = 0;
  264. n_alt[i] = ppmu->get_alternatives(event[i], cflags[i],
  265. alternatives[i]);
  266. for (j = 1; j < n_alt[i]; ++j)
  267. ppmu->get_constraint(alternatives[i][j],
  268. &amasks[i][j], &avalues[i][j]);
  269. }
  270. /* enumerate all possibilities and see if any will work */
  271. i = 0;
  272. j = -1;
  273. value = mask = nv = 0;
  274. while (i < n_ev) {
  275. if (j >= 0) {
  276. /* we're backtracking, restore context */
  277. value = svalues[i];
  278. mask = smasks[i];
  279. j = choice[i];
  280. }
  281. /*
  282. * See if any alternative k for event i,
  283. * where k > j, will satisfy the constraints.
  284. */
  285. while (++j < n_alt[i]) {
  286. nv = (value | avalues[i][j]) +
  287. (value & avalues[i][j] & addf);
  288. if ((((nv + tadd) ^ value) & mask) == 0 &&
  289. (((nv + tadd) ^ avalues[i][j])
  290. & amasks[i][j]) == 0)
  291. break;
  292. }
  293. if (j >= n_alt[i]) {
  294. /*
  295. * No feasible alternative, backtrack
  296. * to event i-1 and continue enumerating its
  297. * alternatives from where we got up to.
  298. */
  299. if (--i < 0)
  300. return -1;
  301. } else {
  302. /*
  303. * Found a feasible alternative for event i,
  304. * remember where we got up to with this event,
  305. * go on to the next event, and start with
  306. * the first alternative for it.
  307. */
  308. choice[i] = j;
  309. svalues[i] = value;
  310. smasks[i] = mask;
  311. value = nv;
  312. mask |= amasks[i][j];
  313. ++i;
  314. j = -1;
  315. }
  316. }
  317. /* OK, we have a feasible combination, tell the caller the solution */
  318. for (i = 0; i < n_ev; ++i)
  319. event[i] = alternatives[i][choice[i]];
  320. return 0;
  321. }
  322. /*
  323. * Check if newly-added counters have consistent settings for
  324. * exclude_{user,kernel,hv} with each other and any previously
  325. * added counters.
  326. */
  327. static int check_excludes(struct perf_counter **ctrs, unsigned int cflags[],
  328. int n_prev, int n_new)
  329. {
  330. int eu = 0, ek = 0, eh = 0;
  331. int i, n, first;
  332. struct perf_counter *counter;
  333. n = n_prev + n_new;
  334. if (n <= 1)
  335. return 0;
  336. first = 1;
  337. for (i = 0; i < n; ++i) {
  338. if (cflags[i] & PPMU_LIMITED_PMC_OK) {
  339. cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
  340. continue;
  341. }
  342. counter = ctrs[i];
  343. if (first) {
  344. eu = counter->attr.exclude_user;
  345. ek = counter->attr.exclude_kernel;
  346. eh = counter->attr.exclude_hv;
  347. first = 0;
  348. } else if (counter->attr.exclude_user != eu ||
  349. counter->attr.exclude_kernel != ek ||
  350. counter->attr.exclude_hv != eh) {
  351. return -EAGAIN;
  352. }
  353. }
  354. if (eu || ek || eh)
  355. for (i = 0; i < n; ++i)
  356. if (cflags[i] & PPMU_LIMITED_PMC_OK)
  357. cflags[i] |= PPMU_LIMITED_PMC_REQD;
  358. return 0;
  359. }
  360. static void power_pmu_read(struct perf_counter *counter)
  361. {
  362. s64 val, delta, prev;
  363. if (!counter->hw.idx)
  364. return;
  365. /*
  366. * Performance monitor interrupts come even when interrupts
  367. * are soft-disabled, as long as interrupts are hard-enabled.
  368. * Therefore we treat them like NMIs.
  369. */
  370. do {
  371. prev = atomic64_read(&counter->hw.prev_count);
  372. barrier();
  373. val = read_pmc(counter->hw.idx);
  374. } while (atomic64_cmpxchg(&counter->hw.prev_count, prev, val) != prev);
  375. /* The counters are only 32 bits wide */
  376. delta = (val - prev) & 0xfffffffful;
  377. atomic64_add(delta, &counter->count);
  378. atomic64_sub(delta, &counter->hw.period_left);
  379. }
  380. /*
  381. * On some machines, PMC5 and PMC6 can't be written, don't respect
  382. * the freeze conditions, and don't generate interrupts. This tells
  383. * us if `counter' is using such a PMC.
  384. */
  385. static int is_limited_pmc(int pmcnum)
  386. {
  387. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  388. && (pmcnum == 5 || pmcnum == 6);
  389. }
  390. static void freeze_limited_counters(struct cpu_hw_counters *cpuhw,
  391. unsigned long pmc5, unsigned long pmc6)
  392. {
  393. struct perf_counter *counter;
  394. u64 val, prev, delta;
  395. int i;
  396. for (i = 0; i < cpuhw->n_limited; ++i) {
  397. counter = cpuhw->limited_counter[i];
  398. if (!counter->hw.idx)
  399. continue;
  400. val = (counter->hw.idx == 5) ? pmc5 : pmc6;
  401. prev = atomic64_read(&counter->hw.prev_count);
  402. counter->hw.idx = 0;
  403. delta = (val - prev) & 0xfffffffful;
  404. atomic64_add(delta, &counter->count);
  405. }
  406. }
  407. static void thaw_limited_counters(struct cpu_hw_counters *cpuhw,
  408. unsigned long pmc5, unsigned long pmc6)
  409. {
  410. struct perf_counter *counter;
  411. u64 val;
  412. int i;
  413. for (i = 0; i < cpuhw->n_limited; ++i) {
  414. counter = cpuhw->limited_counter[i];
  415. counter->hw.idx = cpuhw->limited_hwidx[i];
  416. val = (counter->hw.idx == 5) ? pmc5 : pmc6;
  417. atomic64_set(&counter->hw.prev_count, val);
  418. perf_counter_update_userpage(counter);
  419. }
  420. }
  421. /*
  422. * Since limited counters don't respect the freeze conditions, we
  423. * have to read them immediately after freezing or unfreezing the
  424. * other counters. We try to keep the values from the limited
  425. * counters as consistent as possible by keeping the delay (in
  426. * cycles and instructions) between freezing/unfreezing and reading
  427. * the limited counters as small and consistent as possible.
  428. * Therefore, if any limited counters are in use, we read them
  429. * both, and always in the same order, to minimize variability,
  430. * and do it inside the same asm that writes MMCR0.
  431. */
  432. static void write_mmcr0(struct cpu_hw_counters *cpuhw, unsigned long mmcr0)
  433. {
  434. unsigned long pmc5, pmc6;
  435. if (!cpuhw->n_limited) {
  436. mtspr(SPRN_MMCR0, mmcr0);
  437. return;
  438. }
  439. /*
  440. * Write MMCR0, then read PMC5 and PMC6 immediately.
  441. * To ensure we don't get a performance monitor interrupt
  442. * between writing MMCR0 and freezing/thawing the limited
  443. * counters, we first write MMCR0 with the counter overflow
  444. * interrupt enable bits turned off.
  445. */
  446. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  447. : "=&r" (pmc5), "=&r" (pmc6)
  448. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  449. "i" (SPRN_MMCR0),
  450. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  451. if (mmcr0 & MMCR0_FC)
  452. freeze_limited_counters(cpuhw, pmc5, pmc6);
  453. else
  454. thaw_limited_counters(cpuhw, pmc5, pmc6);
  455. /*
  456. * Write the full MMCR0 including the counter overflow interrupt
  457. * enable bits, if necessary.
  458. */
  459. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  460. mtspr(SPRN_MMCR0, mmcr0);
  461. }
  462. /*
  463. * Disable all counters to prevent PMU interrupts and to allow
  464. * counters to be added or removed.
  465. */
  466. void hw_perf_disable(void)
  467. {
  468. struct cpu_hw_counters *cpuhw;
  469. unsigned long flags;
  470. if (!ppmu)
  471. return;
  472. local_irq_save(flags);
  473. cpuhw = &__get_cpu_var(cpu_hw_counters);
  474. if (!cpuhw->disabled) {
  475. cpuhw->disabled = 1;
  476. cpuhw->n_added = 0;
  477. /*
  478. * Check if we ever enabled the PMU on this cpu.
  479. */
  480. if (!cpuhw->pmcs_enabled) {
  481. if (ppc_md.enable_pmcs)
  482. ppc_md.enable_pmcs();
  483. cpuhw->pmcs_enabled = 1;
  484. }
  485. /*
  486. * Disable instruction sampling if it was enabled
  487. */
  488. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  489. mtspr(SPRN_MMCRA,
  490. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  491. mb();
  492. }
  493. /*
  494. * Set the 'freeze counters' bit.
  495. * The barrier is to make sure the mtspr has been
  496. * executed and the PMU has frozen the counters
  497. * before we return.
  498. */
  499. write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
  500. mb();
  501. }
  502. local_irq_restore(flags);
  503. }
  504. /*
  505. * Re-enable all counters if disable == 0.
  506. * If we were previously disabled and counters were added, then
  507. * put the new config on the PMU.
  508. */
  509. void hw_perf_enable(void)
  510. {
  511. struct perf_counter *counter;
  512. struct cpu_hw_counters *cpuhw;
  513. unsigned long flags;
  514. long i;
  515. unsigned long val;
  516. s64 left;
  517. unsigned int hwc_index[MAX_HWCOUNTERS];
  518. int n_lim;
  519. int idx;
  520. if (!ppmu)
  521. return;
  522. local_irq_save(flags);
  523. cpuhw = &__get_cpu_var(cpu_hw_counters);
  524. if (!cpuhw->disabled) {
  525. local_irq_restore(flags);
  526. return;
  527. }
  528. cpuhw->disabled = 0;
  529. /*
  530. * If we didn't change anything, or only removed counters,
  531. * no need to recalculate MMCR* settings and reset the PMCs.
  532. * Just reenable the PMU with the current MMCR* settings
  533. * (possibly updated for removal of counters).
  534. */
  535. if (!cpuhw->n_added) {
  536. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  537. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  538. if (cpuhw->n_counters == 0)
  539. perf_set_pmu_inuse(0);
  540. goto out_enable;
  541. }
  542. /*
  543. * Compute MMCR* values for the new set of counters
  544. */
  545. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
  546. cpuhw->mmcr)) {
  547. /* shouldn't ever get here */
  548. printk(KERN_ERR "oops compute_mmcr failed\n");
  549. goto out;
  550. }
  551. /*
  552. * Add in MMCR0 freeze bits corresponding to the
  553. * attr.exclude_* bits for the first counter.
  554. * We have already checked that all counters have the
  555. * same values for these bits as the first counter.
  556. */
  557. counter = cpuhw->counter[0];
  558. if (counter->attr.exclude_user)
  559. cpuhw->mmcr[0] |= MMCR0_FCP;
  560. if (counter->attr.exclude_kernel)
  561. cpuhw->mmcr[0] |= freeze_counters_kernel;
  562. if (counter->attr.exclude_hv)
  563. cpuhw->mmcr[0] |= MMCR0_FCHV;
  564. /*
  565. * Write the new configuration to MMCR* with the freeze
  566. * bit set and set the hardware counters to their initial values.
  567. * Then unfreeze the counters.
  568. */
  569. perf_set_pmu_inuse(1);
  570. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  571. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  572. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  573. | MMCR0_FC);
  574. /*
  575. * Read off any pre-existing counters that need to move
  576. * to another PMC.
  577. */
  578. for (i = 0; i < cpuhw->n_counters; ++i) {
  579. counter = cpuhw->counter[i];
  580. if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
  581. power_pmu_read(counter);
  582. write_pmc(counter->hw.idx, 0);
  583. counter->hw.idx = 0;
  584. }
  585. }
  586. /*
  587. * Initialize the PMCs for all the new and moved counters.
  588. */
  589. cpuhw->n_limited = n_lim = 0;
  590. for (i = 0; i < cpuhw->n_counters; ++i) {
  591. counter = cpuhw->counter[i];
  592. if (counter->hw.idx)
  593. continue;
  594. idx = hwc_index[i] + 1;
  595. if (is_limited_pmc(idx)) {
  596. cpuhw->limited_counter[n_lim] = counter;
  597. cpuhw->limited_hwidx[n_lim] = idx;
  598. ++n_lim;
  599. continue;
  600. }
  601. val = 0;
  602. if (counter->hw.sample_period) {
  603. left = atomic64_read(&counter->hw.period_left);
  604. if (left < 0x80000000L)
  605. val = 0x80000000L - left;
  606. }
  607. atomic64_set(&counter->hw.prev_count, val);
  608. counter->hw.idx = idx;
  609. write_pmc(idx, val);
  610. perf_counter_update_userpage(counter);
  611. }
  612. cpuhw->n_limited = n_lim;
  613. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  614. out_enable:
  615. mb();
  616. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  617. /*
  618. * Enable instruction sampling if necessary
  619. */
  620. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  621. mb();
  622. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  623. }
  624. out:
  625. local_irq_restore(flags);
  626. }
  627. static int collect_events(struct perf_counter *group, int max_count,
  628. struct perf_counter *ctrs[], u64 *events,
  629. unsigned int *flags)
  630. {
  631. int n = 0;
  632. struct perf_counter *counter;
  633. if (!is_software_counter(group)) {
  634. if (n >= max_count)
  635. return -1;
  636. ctrs[n] = group;
  637. flags[n] = group->hw.counter_base;
  638. events[n++] = group->hw.config;
  639. }
  640. list_for_each_entry(counter, &group->sibling_list, list_entry) {
  641. if (!is_software_counter(counter) &&
  642. counter->state != PERF_COUNTER_STATE_OFF) {
  643. if (n >= max_count)
  644. return -1;
  645. ctrs[n] = counter;
  646. flags[n] = counter->hw.counter_base;
  647. events[n++] = counter->hw.config;
  648. }
  649. }
  650. return n;
  651. }
  652. static void counter_sched_in(struct perf_counter *counter, int cpu)
  653. {
  654. counter->state = PERF_COUNTER_STATE_ACTIVE;
  655. counter->oncpu = cpu;
  656. counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped;
  657. if (is_software_counter(counter))
  658. counter->pmu->enable(counter);
  659. }
  660. /*
  661. * Called to enable a whole group of counters.
  662. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  663. * Assumes the caller has disabled interrupts and has
  664. * frozen the PMU with hw_perf_save_disable.
  665. */
  666. int hw_perf_group_sched_in(struct perf_counter *group_leader,
  667. struct perf_cpu_context *cpuctx,
  668. struct perf_counter_context *ctx, int cpu)
  669. {
  670. struct cpu_hw_counters *cpuhw;
  671. long i, n, n0;
  672. struct perf_counter *sub;
  673. if (!ppmu)
  674. return 0;
  675. cpuhw = &__get_cpu_var(cpu_hw_counters);
  676. n0 = cpuhw->n_counters;
  677. n = collect_events(group_leader, ppmu->n_counter - n0,
  678. &cpuhw->counter[n0], &cpuhw->events[n0],
  679. &cpuhw->flags[n0]);
  680. if (n < 0)
  681. return -EAGAIN;
  682. if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n))
  683. return -EAGAIN;
  684. i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0);
  685. if (i < 0)
  686. return -EAGAIN;
  687. cpuhw->n_counters = n0 + n;
  688. cpuhw->n_added += n;
  689. /*
  690. * OK, this group can go on; update counter states etc.,
  691. * and enable any software counters
  692. */
  693. for (i = n0; i < n0 + n; ++i)
  694. cpuhw->counter[i]->hw.config = cpuhw->events[i];
  695. cpuctx->active_oncpu += n;
  696. n = 1;
  697. counter_sched_in(group_leader, cpu);
  698. list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
  699. if (sub->state != PERF_COUNTER_STATE_OFF) {
  700. counter_sched_in(sub, cpu);
  701. ++n;
  702. }
  703. }
  704. ctx->nr_active += n;
  705. return 1;
  706. }
  707. /*
  708. * Add a counter to the PMU.
  709. * If all counters are not already frozen, then we disable and
  710. * re-enable the PMU in order to get hw_perf_enable to do the
  711. * actual work of reconfiguring the PMU.
  712. */
  713. static int power_pmu_enable(struct perf_counter *counter)
  714. {
  715. struct cpu_hw_counters *cpuhw;
  716. unsigned long flags;
  717. int n0;
  718. int ret = -EAGAIN;
  719. local_irq_save(flags);
  720. perf_disable();
  721. /*
  722. * Add the counter to the list (if there is room)
  723. * and check whether the total set is still feasible.
  724. */
  725. cpuhw = &__get_cpu_var(cpu_hw_counters);
  726. n0 = cpuhw->n_counters;
  727. if (n0 >= ppmu->n_counter)
  728. goto out;
  729. cpuhw->counter[n0] = counter;
  730. cpuhw->events[n0] = counter->hw.config;
  731. cpuhw->flags[n0] = counter->hw.counter_base;
  732. if (check_excludes(cpuhw->counter, cpuhw->flags, n0, 1))
  733. goto out;
  734. if (power_check_constraints(cpuhw->events, cpuhw->flags, n0 + 1))
  735. goto out;
  736. counter->hw.config = cpuhw->events[n0];
  737. ++cpuhw->n_counters;
  738. ++cpuhw->n_added;
  739. ret = 0;
  740. out:
  741. perf_enable();
  742. local_irq_restore(flags);
  743. return ret;
  744. }
  745. /*
  746. * Remove a counter from the PMU.
  747. */
  748. static void power_pmu_disable(struct perf_counter *counter)
  749. {
  750. struct cpu_hw_counters *cpuhw;
  751. long i;
  752. unsigned long flags;
  753. local_irq_save(flags);
  754. perf_disable();
  755. power_pmu_read(counter);
  756. cpuhw = &__get_cpu_var(cpu_hw_counters);
  757. for (i = 0; i < cpuhw->n_counters; ++i) {
  758. if (counter == cpuhw->counter[i]) {
  759. while (++i < cpuhw->n_counters)
  760. cpuhw->counter[i-1] = cpuhw->counter[i];
  761. --cpuhw->n_counters;
  762. ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
  763. if (counter->hw.idx) {
  764. write_pmc(counter->hw.idx, 0);
  765. counter->hw.idx = 0;
  766. }
  767. perf_counter_update_userpage(counter);
  768. break;
  769. }
  770. }
  771. for (i = 0; i < cpuhw->n_limited; ++i)
  772. if (counter == cpuhw->limited_counter[i])
  773. break;
  774. if (i < cpuhw->n_limited) {
  775. while (++i < cpuhw->n_limited) {
  776. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  777. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  778. }
  779. --cpuhw->n_limited;
  780. }
  781. if (cpuhw->n_counters == 0) {
  782. /* disable exceptions if no counters are running */
  783. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  784. }
  785. perf_enable();
  786. local_irq_restore(flags);
  787. }
  788. /*
  789. * Re-enable interrupts on a counter after they were throttled
  790. * because they were coming too fast.
  791. */
  792. static void power_pmu_unthrottle(struct perf_counter *counter)
  793. {
  794. s64 val, left;
  795. unsigned long flags;
  796. if (!counter->hw.idx || !counter->hw.sample_period)
  797. return;
  798. local_irq_save(flags);
  799. perf_disable();
  800. power_pmu_read(counter);
  801. left = counter->hw.sample_period;
  802. counter->hw.last_period = left;
  803. val = 0;
  804. if (left < 0x80000000L)
  805. val = 0x80000000L - left;
  806. write_pmc(counter->hw.idx, val);
  807. atomic64_set(&counter->hw.prev_count, val);
  808. atomic64_set(&counter->hw.period_left, left);
  809. perf_counter_update_userpage(counter);
  810. perf_enable();
  811. local_irq_restore(flags);
  812. }
  813. struct pmu power_pmu = {
  814. .enable = power_pmu_enable,
  815. .disable = power_pmu_disable,
  816. .read = power_pmu_read,
  817. .unthrottle = power_pmu_unthrottle,
  818. };
  819. /*
  820. * Return 1 if we might be able to put counter on a limited PMC,
  821. * or 0 if not.
  822. * A counter can only go on a limited PMC if it counts something
  823. * that a limited PMC can count, doesn't require interrupts, and
  824. * doesn't exclude any processor mode.
  825. */
  826. static int can_go_on_limited_pmc(struct perf_counter *counter, u64 ev,
  827. unsigned int flags)
  828. {
  829. int n;
  830. u64 alt[MAX_EVENT_ALTERNATIVES];
  831. if (counter->attr.exclude_user
  832. || counter->attr.exclude_kernel
  833. || counter->attr.exclude_hv
  834. || counter->attr.sample_period)
  835. return 0;
  836. if (ppmu->limited_pmc_event(ev))
  837. return 1;
  838. /*
  839. * The requested event isn't on a limited PMC already;
  840. * see if any alternative code goes on a limited PMC.
  841. */
  842. if (!ppmu->get_alternatives)
  843. return 0;
  844. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  845. n = ppmu->get_alternatives(ev, flags, alt);
  846. return n > 0;
  847. }
  848. /*
  849. * Find an alternative event that goes on a normal PMC, if possible,
  850. * and return the event code, or 0 if there is no such alternative.
  851. * (Note: event code 0 is "don't count" on all machines.)
  852. */
  853. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  854. {
  855. u64 alt[MAX_EVENT_ALTERNATIVES];
  856. int n;
  857. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  858. n = ppmu->get_alternatives(ev, flags, alt);
  859. if (!n)
  860. return 0;
  861. return alt[0];
  862. }
  863. /* Number of perf_counters counting hardware events */
  864. static atomic_t num_counters;
  865. /* Used to avoid races in calling reserve/release_pmc_hardware */
  866. static DEFINE_MUTEX(pmc_reserve_mutex);
  867. /*
  868. * Release the PMU if this is the last perf_counter.
  869. */
  870. static void hw_perf_counter_destroy(struct perf_counter *counter)
  871. {
  872. if (!atomic_add_unless(&num_counters, -1, 1)) {
  873. mutex_lock(&pmc_reserve_mutex);
  874. if (atomic_dec_return(&num_counters) == 0)
  875. release_pmc_hardware();
  876. mutex_unlock(&pmc_reserve_mutex);
  877. }
  878. }
  879. /*
  880. * Translate a generic cache event config to a raw event code.
  881. */
  882. static int hw_perf_cache_event(u64 config, u64 *eventp)
  883. {
  884. unsigned long type, op, result;
  885. int ev;
  886. if (!ppmu->cache_events)
  887. return -EINVAL;
  888. /* unpack config */
  889. type = config & 0xff;
  890. op = (config >> 8) & 0xff;
  891. result = (config >> 16) & 0xff;
  892. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  893. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  894. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  895. return -EINVAL;
  896. ev = (*ppmu->cache_events)[type][op][result];
  897. if (ev == 0)
  898. return -EOPNOTSUPP;
  899. if (ev == -1)
  900. return -EINVAL;
  901. *eventp = ev;
  902. return 0;
  903. }
  904. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  905. {
  906. u64 ev;
  907. unsigned long flags;
  908. struct perf_counter *ctrs[MAX_HWCOUNTERS];
  909. u64 events[MAX_HWCOUNTERS];
  910. unsigned int cflags[MAX_HWCOUNTERS];
  911. int n;
  912. int err;
  913. if (!ppmu)
  914. return ERR_PTR(-ENXIO);
  915. switch (counter->attr.type) {
  916. case PERF_TYPE_HARDWARE:
  917. ev = counter->attr.config;
  918. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  919. return ERR_PTR(-EOPNOTSUPP);
  920. ev = ppmu->generic_events[ev];
  921. break;
  922. case PERF_TYPE_HW_CACHE:
  923. err = hw_perf_cache_event(counter->attr.config, &ev);
  924. if (err)
  925. return ERR_PTR(err);
  926. break;
  927. case PERF_TYPE_RAW:
  928. ev = counter->attr.config;
  929. break;
  930. default:
  931. return ERR_PTR(-EINVAL);
  932. }
  933. counter->hw.config_base = ev;
  934. counter->hw.idx = 0;
  935. /*
  936. * If we are not running on a hypervisor, force the
  937. * exclude_hv bit to 0 so that we don't care what
  938. * the user set it to.
  939. */
  940. if (!firmware_has_feature(FW_FEATURE_LPAR))
  941. counter->attr.exclude_hv = 0;
  942. /*
  943. * If this is a per-task counter, then we can use
  944. * PM_RUN_* events interchangeably with their non RUN_*
  945. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  946. * XXX we should check if the task is an idle task.
  947. */
  948. flags = 0;
  949. if (counter->ctx->task)
  950. flags |= PPMU_ONLY_COUNT_RUN;
  951. /*
  952. * If this machine has limited counters, check whether this
  953. * event could go on a limited counter.
  954. */
  955. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  956. if (can_go_on_limited_pmc(counter, ev, flags)) {
  957. flags |= PPMU_LIMITED_PMC_OK;
  958. } else if (ppmu->limited_pmc_event(ev)) {
  959. /*
  960. * The requested event is on a limited PMC,
  961. * but we can't use a limited PMC; see if any
  962. * alternative goes on a normal PMC.
  963. */
  964. ev = normal_pmc_alternative(ev, flags);
  965. if (!ev)
  966. return ERR_PTR(-EINVAL);
  967. }
  968. }
  969. /*
  970. * If this is in a group, check if it can go on with all the
  971. * other hardware counters in the group. We assume the counter
  972. * hasn't been linked into its leader's sibling list at this point.
  973. */
  974. n = 0;
  975. if (counter->group_leader != counter) {
  976. n = collect_events(counter->group_leader, ppmu->n_counter - 1,
  977. ctrs, events, cflags);
  978. if (n < 0)
  979. return ERR_PTR(-EINVAL);
  980. }
  981. events[n] = ev;
  982. ctrs[n] = counter;
  983. cflags[n] = flags;
  984. if (check_excludes(ctrs, cflags, n, 1))
  985. return ERR_PTR(-EINVAL);
  986. if (power_check_constraints(events, cflags, n + 1))
  987. return ERR_PTR(-EINVAL);
  988. counter->hw.config = events[n];
  989. counter->hw.counter_base = cflags[n];
  990. counter->hw.last_period = counter->hw.sample_period;
  991. atomic64_set(&counter->hw.period_left, counter->hw.last_period);
  992. /*
  993. * See if we need to reserve the PMU.
  994. * If no counters are currently in use, then we have to take a
  995. * mutex to ensure that we don't race with another task doing
  996. * reserve_pmc_hardware or release_pmc_hardware.
  997. */
  998. err = 0;
  999. if (!atomic_inc_not_zero(&num_counters)) {
  1000. mutex_lock(&pmc_reserve_mutex);
  1001. if (atomic_read(&num_counters) == 0 &&
  1002. reserve_pmc_hardware(perf_counter_interrupt))
  1003. err = -EBUSY;
  1004. else
  1005. atomic_inc(&num_counters);
  1006. mutex_unlock(&pmc_reserve_mutex);
  1007. }
  1008. counter->destroy = hw_perf_counter_destroy;
  1009. if (err)
  1010. return ERR_PTR(err);
  1011. return &power_pmu;
  1012. }
  1013. /*
  1014. * A counter has overflowed; update its count and record
  1015. * things if requested. Note that interrupts are hard-disabled
  1016. * here so there is no possibility of being interrupted.
  1017. */
  1018. static void record_and_restart(struct perf_counter *counter, unsigned long val,
  1019. struct pt_regs *regs, int nmi)
  1020. {
  1021. u64 period = counter->hw.sample_period;
  1022. s64 prev, delta, left;
  1023. int record = 0;
  1024. /* we don't have to worry about interrupts here */
  1025. prev = atomic64_read(&counter->hw.prev_count);
  1026. delta = (val - prev) & 0xfffffffful;
  1027. atomic64_add(delta, &counter->count);
  1028. /*
  1029. * See if the total period for this counter has expired,
  1030. * and update for the next period.
  1031. */
  1032. val = 0;
  1033. left = atomic64_read(&counter->hw.period_left) - delta;
  1034. if (period) {
  1035. if (left <= 0) {
  1036. left += period;
  1037. if (left <= 0)
  1038. left = period;
  1039. record = 1;
  1040. }
  1041. if (left < 0x80000000LL)
  1042. val = 0x80000000LL - left;
  1043. }
  1044. /*
  1045. * Finally record data if requested.
  1046. */
  1047. if (record) {
  1048. struct perf_sample_data data = {
  1049. .regs = regs,
  1050. .addr = 0,
  1051. .period = counter->hw.last_period,
  1052. };
  1053. if (counter->attr.sample_type & PERF_SAMPLE_ADDR)
  1054. perf_get_data_addr(regs, &data.addr);
  1055. if (perf_counter_overflow(counter, nmi, &data)) {
  1056. /*
  1057. * Interrupts are coming too fast - throttle them
  1058. * by setting the counter to 0, so it will be
  1059. * at least 2^30 cycles until the next interrupt
  1060. * (assuming each counter counts at most 2 counts
  1061. * per cycle).
  1062. */
  1063. val = 0;
  1064. left = ~0ULL >> 1;
  1065. }
  1066. }
  1067. write_pmc(counter->hw.idx, val);
  1068. atomic64_set(&counter->hw.prev_count, val);
  1069. atomic64_set(&counter->hw.period_left, left);
  1070. perf_counter_update_userpage(counter);
  1071. }
  1072. /*
  1073. * Called from generic code to get the misc flags (i.e. processor mode)
  1074. * for an event.
  1075. */
  1076. unsigned long perf_misc_flags(struct pt_regs *regs)
  1077. {
  1078. u32 flags = perf_get_misc_flags(regs);
  1079. if (flags)
  1080. return flags;
  1081. return user_mode(regs) ? PERF_EVENT_MISC_USER :
  1082. PERF_EVENT_MISC_KERNEL;
  1083. }
  1084. /*
  1085. * Called from generic code to get the instruction pointer
  1086. * for an event.
  1087. */
  1088. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1089. {
  1090. unsigned long ip;
  1091. if (TRAP(regs) != 0xf00)
  1092. return regs->nip; /* not a PMU interrupt */
  1093. ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1094. return ip;
  1095. }
  1096. /*
  1097. * Performance monitor interrupt stuff
  1098. */
  1099. static void perf_counter_interrupt(struct pt_regs *regs)
  1100. {
  1101. int i;
  1102. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  1103. struct perf_counter *counter;
  1104. unsigned long val;
  1105. int found = 0;
  1106. int nmi;
  1107. if (cpuhw->n_limited)
  1108. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1109. mfspr(SPRN_PMC6));
  1110. perf_read_regs(regs);
  1111. nmi = perf_intr_is_nmi(regs);
  1112. if (nmi)
  1113. nmi_enter();
  1114. else
  1115. irq_enter();
  1116. for (i = 0; i < cpuhw->n_counters; ++i) {
  1117. counter = cpuhw->counter[i];
  1118. if (!counter->hw.idx || is_limited_pmc(counter->hw.idx))
  1119. continue;
  1120. val = read_pmc(counter->hw.idx);
  1121. if ((int)val < 0) {
  1122. /* counter has overflowed */
  1123. found = 1;
  1124. record_and_restart(counter, val, regs, nmi);
  1125. }
  1126. }
  1127. /*
  1128. * In case we didn't find and reset the counter that caused
  1129. * the interrupt, scan all counters and reset any that are
  1130. * negative, to avoid getting continual interrupts.
  1131. * Any that we processed in the previous loop will not be negative.
  1132. */
  1133. if (!found) {
  1134. for (i = 0; i < ppmu->n_counter; ++i) {
  1135. if (is_limited_pmc(i + 1))
  1136. continue;
  1137. val = read_pmc(i + 1);
  1138. if ((int)val < 0)
  1139. write_pmc(i + 1, 0);
  1140. }
  1141. }
  1142. /*
  1143. * Reset MMCR0 to its normal value. This will set PMXE and
  1144. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1145. * and thus allow interrupts to occur again.
  1146. * XXX might want to use MSR.PM to keep the counters frozen until
  1147. * we get back out of this interrupt.
  1148. */
  1149. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1150. if (nmi)
  1151. nmi_exit();
  1152. else
  1153. irq_exit();
  1154. }
  1155. void hw_perf_counter_setup(int cpu)
  1156. {
  1157. struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
  1158. if (!ppmu)
  1159. return;
  1160. memset(cpuhw, 0, sizeof(*cpuhw));
  1161. cpuhw->mmcr[0] = MMCR0_FC;
  1162. }
  1163. int register_power_pmu(struct power_pmu *pmu)
  1164. {
  1165. if (ppmu)
  1166. return -EBUSY; /* something's already registered */
  1167. ppmu = pmu;
  1168. pr_info("%s performance monitor hardware support registered\n",
  1169. pmu->name);
  1170. #ifdef MSR_HV
  1171. /*
  1172. * Use FCHV to ignore kernel events if MSR.HV is set.
  1173. */
  1174. if (mfmsr() & MSR_HV)
  1175. freeze_counters_kernel = MMCR0_FCHV;
  1176. #endif /* CONFIG_PPC64 */
  1177. return 0;
  1178. }