perf_counter.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. local_irq_save(flags);
  471. cpuhw = &__get_cpu_var(cpu_hw_counters);
  472. if (!cpuhw->disabled) {
  473. cpuhw->disabled = 1;
  474. cpuhw->n_added = 0;
  475. /*
  476. * Check if we ever enabled the PMU on this cpu.
  477. */
  478. if (!cpuhw->pmcs_enabled) {
  479. if (ppc_md.enable_pmcs)
  480. ppc_md.enable_pmcs();
  481. cpuhw->pmcs_enabled = 1;
  482. }
  483. /*
  484. * Disable instruction sampling if it was enabled
  485. */
  486. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  487. mtspr(SPRN_MMCRA,
  488. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  489. mb();
  490. }
  491. /*
  492. * Set the 'freeze counters' bit.
  493. * The barrier is to make sure the mtspr has been
  494. * executed and the PMU has frozen the counters
  495. * before we return.
  496. */
  497. write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
  498. mb();
  499. }
  500. local_irq_restore(flags);
  501. }
  502. /*
  503. * Re-enable all counters if disable == 0.
  504. * If we were previously disabled and counters were added, then
  505. * put the new config on the PMU.
  506. */
  507. void hw_perf_enable(void)
  508. {
  509. struct perf_counter *counter;
  510. struct cpu_hw_counters *cpuhw;
  511. unsigned long flags;
  512. long i;
  513. unsigned long val;
  514. s64 left;
  515. unsigned int hwc_index[MAX_HWCOUNTERS];
  516. int n_lim;
  517. int idx;
  518. local_irq_save(flags);
  519. cpuhw = &__get_cpu_var(cpu_hw_counters);
  520. if (!cpuhw->disabled) {
  521. local_irq_restore(flags);
  522. return;
  523. }
  524. cpuhw->disabled = 0;
  525. /*
  526. * If we didn't change anything, or only removed counters,
  527. * no need to recalculate MMCR* settings and reset the PMCs.
  528. * Just reenable the PMU with the current MMCR* settings
  529. * (possibly updated for removal of counters).
  530. */
  531. if (!cpuhw->n_added) {
  532. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  533. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  534. if (cpuhw->n_counters == 0)
  535. perf_set_pmu_inuse(0);
  536. goto out_enable;
  537. }
  538. /*
  539. * Compute MMCR* values for the new set of counters
  540. */
  541. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
  542. cpuhw->mmcr)) {
  543. /* shouldn't ever get here */
  544. printk(KERN_ERR "oops compute_mmcr failed\n");
  545. goto out;
  546. }
  547. /*
  548. * Add in MMCR0 freeze bits corresponding to the
  549. * attr.exclude_* bits for the first counter.
  550. * We have already checked that all counters have the
  551. * same values for these bits as the first counter.
  552. */
  553. counter = cpuhw->counter[0];
  554. if (counter->attr.exclude_user)
  555. cpuhw->mmcr[0] |= MMCR0_FCP;
  556. if (counter->attr.exclude_kernel)
  557. cpuhw->mmcr[0] |= freeze_counters_kernel;
  558. if (counter->attr.exclude_hv)
  559. cpuhw->mmcr[0] |= MMCR0_FCHV;
  560. /*
  561. * Write the new configuration to MMCR* with the freeze
  562. * bit set and set the hardware counters to their initial values.
  563. * Then unfreeze the counters.
  564. */
  565. perf_set_pmu_inuse(1);
  566. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  567. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  568. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  569. | MMCR0_FC);
  570. /*
  571. * Read off any pre-existing counters that need to move
  572. * to another PMC.
  573. */
  574. for (i = 0; i < cpuhw->n_counters; ++i) {
  575. counter = cpuhw->counter[i];
  576. if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
  577. power_pmu_read(counter);
  578. write_pmc(counter->hw.idx, 0);
  579. counter->hw.idx = 0;
  580. }
  581. }
  582. /*
  583. * Initialize the PMCs for all the new and moved counters.
  584. */
  585. cpuhw->n_limited = n_lim = 0;
  586. for (i = 0; i < cpuhw->n_counters; ++i) {
  587. counter = cpuhw->counter[i];
  588. if (counter->hw.idx)
  589. continue;
  590. idx = hwc_index[i] + 1;
  591. if (is_limited_pmc(idx)) {
  592. cpuhw->limited_counter[n_lim] = counter;
  593. cpuhw->limited_hwidx[n_lim] = idx;
  594. ++n_lim;
  595. continue;
  596. }
  597. val = 0;
  598. if (counter->hw.sample_period) {
  599. left = atomic64_read(&counter->hw.period_left);
  600. if (left < 0x80000000L)
  601. val = 0x80000000L - left;
  602. }
  603. atomic64_set(&counter->hw.prev_count, val);
  604. counter->hw.idx = idx;
  605. write_pmc(idx, val);
  606. perf_counter_update_userpage(counter);
  607. }
  608. cpuhw->n_limited = n_lim;
  609. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  610. out_enable:
  611. mb();
  612. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  613. /*
  614. * Enable instruction sampling if necessary
  615. */
  616. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  617. mb();
  618. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  619. }
  620. out:
  621. local_irq_restore(flags);
  622. }
  623. static int collect_events(struct perf_counter *group, int max_count,
  624. struct perf_counter *ctrs[], u64 *events,
  625. unsigned int *flags)
  626. {
  627. int n = 0;
  628. struct perf_counter *counter;
  629. if (!is_software_counter(group)) {
  630. if (n >= max_count)
  631. return -1;
  632. ctrs[n] = group;
  633. flags[n] = group->hw.counter_base;
  634. events[n++] = group->hw.config;
  635. }
  636. list_for_each_entry(counter, &group->sibling_list, list_entry) {
  637. if (!is_software_counter(counter) &&
  638. counter->state != PERF_COUNTER_STATE_OFF) {
  639. if (n >= max_count)
  640. return -1;
  641. ctrs[n] = counter;
  642. flags[n] = counter->hw.counter_base;
  643. events[n++] = counter->hw.config;
  644. }
  645. }
  646. return n;
  647. }
  648. static void counter_sched_in(struct perf_counter *counter, int cpu)
  649. {
  650. counter->state = PERF_COUNTER_STATE_ACTIVE;
  651. counter->oncpu = cpu;
  652. counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped;
  653. if (is_software_counter(counter))
  654. counter->pmu->enable(counter);
  655. }
  656. /*
  657. * Called to enable a whole group of counters.
  658. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  659. * Assumes the caller has disabled interrupts and has
  660. * frozen the PMU with hw_perf_save_disable.
  661. */
  662. int hw_perf_group_sched_in(struct perf_counter *group_leader,
  663. struct perf_cpu_context *cpuctx,
  664. struct perf_counter_context *ctx, int cpu)
  665. {
  666. struct cpu_hw_counters *cpuhw;
  667. long i, n, n0;
  668. struct perf_counter *sub;
  669. cpuhw = &__get_cpu_var(cpu_hw_counters);
  670. n0 = cpuhw->n_counters;
  671. n = collect_events(group_leader, ppmu->n_counter - n0,
  672. &cpuhw->counter[n0], &cpuhw->events[n0],
  673. &cpuhw->flags[n0]);
  674. if (n < 0)
  675. return -EAGAIN;
  676. if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n))
  677. return -EAGAIN;
  678. i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0);
  679. if (i < 0)
  680. return -EAGAIN;
  681. cpuhw->n_counters = n0 + n;
  682. cpuhw->n_added += n;
  683. /*
  684. * OK, this group can go on; update counter states etc.,
  685. * and enable any software counters
  686. */
  687. for (i = n0; i < n0 + n; ++i)
  688. cpuhw->counter[i]->hw.config = cpuhw->events[i];
  689. cpuctx->active_oncpu += n;
  690. n = 1;
  691. counter_sched_in(group_leader, cpu);
  692. list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
  693. if (sub->state != PERF_COUNTER_STATE_OFF) {
  694. counter_sched_in(sub, cpu);
  695. ++n;
  696. }
  697. }
  698. ctx->nr_active += n;
  699. return 1;
  700. }
  701. /*
  702. * Add a counter to the PMU.
  703. * If all counters are not already frozen, then we disable and
  704. * re-enable the PMU in order to get hw_perf_enable to do the
  705. * actual work of reconfiguring the PMU.
  706. */
  707. static int power_pmu_enable(struct perf_counter *counter)
  708. {
  709. struct cpu_hw_counters *cpuhw;
  710. unsigned long flags;
  711. int n0;
  712. int ret = -EAGAIN;
  713. local_irq_save(flags);
  714. perf_disable();
  715. /*
  716. * Add the counter to the list (if there is room)
  717. * and check whether the total set is still feasible.
  718. */
  719. cpuhw = &__get_cpu_var(cpu_hw_counters);
  720. n0 = cpuhw->n_counters;
  721. if (n0 >= ppmu->n_counter)
  722. goto out;
  723. cpuhw->counter[n0] = counter;
  724. cpuhw->events[n0] = counter->hw.config;
  725. cpuhw->flags[n0] = counter->hw.counter_base;
  726. if (check_excludes(cpuhw->counter, cpuhw->flags, n0, 1))
  727. goto out;
  728. if (power_check_constraints(cpuhw->events, cpuhw->flags, n0 + 1))
  729. goto out;
  730. counter->hw.config = cpuhw->events[n0];
  731. ++cpuhw->n_counters;
  732. ++cpuhw->n_added;
  733. ret = 0;
  734. out:
  735. perf_enable();
  736. local_irq_restore(flags);
  737. return ret;
  738. }
  739. /*
  740. * Remove a counter from the PMU.
  741. */
  742. static void power_pmu_disable(struct perf_counter *counter)
  743. {
  744. struct cpu_hw_counters *cpuhw;
  745. long i;
  746. unsigned long flags;
  747. local_irq_save(flags);
  748. perf_disable();
  749. power_pmu_read(counter);
  750. cpuhw = &__get_cpu_var(cpu_hw_counters);
  751. for (i = 0; i < cpuhw->n_counters; ++i) {
  752. if (counter == cpuhw->counter[i]) {
  753. while (++i < cpuhw->n_counters)
  754. cpuhw->counter[i-1] = cpuhw->counter[i];
  755. --cpuhw->n_counters;
  756. ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
  757. if (counter->hw.idx) {
  758. write_pmc(counter->hw.idx, 0);
  759. counter->hw.idx = 0;
  760. }
  761. perf_counter_update_userpage(counter);
  762. break;
  763. }
  764. }
  765. for (i = 0; i < cpuhw->n_limited; ++i)
  766. if (counter == cpuhw->limited_counter[i])
  767. break;
  768. if (i < cpuhw->n_limited) {
  769. while (++i < cpuhw->n_limited) {
  770. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  771. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  772. }
  773. --cpuhw->n_limited;
  774. }
  775. if (cpuhw->n_counters == 0) {
  776. /* disable exceptions if no counters are running */
  777. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  778. }
  779. perf_enable();
  780. local_irq_restore(flags);
  781. }
  782. /*
  783. * Re-enable interrupts on a counter after they were throttled
  784. * because they were coming too fast.
  785. */
  786. static void power_pmu_unthrottle(struct perf_counter *counter)
  787. {
  788. s64 val, left;
  789. unsigned long flags;
  790. if (!counter->hw.idx || !counter->hw.sample_period)
  791. return;
  792. local_irq_save(flags);
  793. perf_disable();
  794. power_pmu_read(counter);
  795. left = counter->hw.sample_period;
  796. counter->hw.last_period = left;
  797. val = 0;
  798. if (left < 0x80000000L)
  799. val = 0x80000000L - left;
  800. write_pmc(counter->hw.idx, val);
  801. atomic64_set(&counter->hw.prev_count, val);
  802. atomic64_set(&counter->hw.period_left, left);
  803. perf_counter_update_userpage(counter);
  804. perf_enable();
  805. local_irq_restore(flags);
  806. }
  807. struct pmu power_pmu = {
  808. .enable = power_pmu_enable,
  809. .disable = power_pmu_disable,
  810. .read = power_pmu_read,
  811. .unthrottle = power_pmu_unthrottle,
  812. };
  813. /*
  814. * Return 1 if we might be able to put counter on a limited PMC,
  815. * or 0 if not.
  816. * A counter can only go on a limited PMC if it counts something
  817. * that a limited PMC can count, doesn't require interrupts, and
  818. * doesn't exclude any processor mode.
  819. */
  820. static int can_go_on_limited_pmc(struct perf_counter *counter, u64 ev,
  821. unsigned int flags)
  822. {
  823. int n;
  824. u64 alt[MAX_EVENT_ALTERNATIVES];
  825. if (counter->attr.exclude_user
  826. || counter->attr.exclude_kernel
  827. || counter->attr.exclude_hv
  828. || counter->attr.sample_period)
  829. return 0;
  830. if (ppmu->limited_pmc_event(ev))
  831. return 1;
  832. /*
  833. * The requested event isn't on a limited PMC already;
  834. * see if any alternative code goes on a limited PMC.
  835. */
  836. if (!ppmu->get_alternatives)
  837. return 0;
  838. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  839. n = ppmu->get_alternatives(ev, flags, alt);
  840. return n > 0;
  841. }
  842. /*
  843. * Find an alternative event that goes on a normal PMC, if possible,
  844. * and return the event code, or 0 if there is no such alternative.
  845. * (Note: event code 0 is "don't count" on all machines.)
  846. */
  847. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  848. {
  849. u64 alt[MAX_EVENT_ALTERNATIVES];
  850. int n;
  851. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  852. n = ppmu->get_alternatives(ev, flags, alt);
  853. if (!n)
  854. return 0;
  855. return alt[0];
  856. }
  857. /* Number of perf_counters counting hardware events */
  858. static atomic_t num_counters;
  859. /* Used to avoid races in calling reserve/release_pmc_hardware */
  860. static DEFINE_MUTEX(pmc_reserve_mutex);
  861. /*
  862. * Release the PMU if this is the last perf_counter.
  863. */
  864. static void hw_perf_counter_destroy(struct perf_counter *counter)
  865. {
  866. if (!atomic_add_unless(&num_counters, -1, 1)) {
  867. mutex_lock(&pmc_reserve_mutex);
  868. if (atomic_dec_return(&num_counters) == 0)
  869. release_pmc_hardware();
  870. mutex_unlock(&pmc_reserve_mutex);
  871. }
  872. }
  873. /*
  874. * Translate a generic cache event config to a raw event code.
  875. */
  876. static int hw_perf_cache_event(u64 config, u64 *eventp)
  877. {
  878. unsigned long type, op, result;
  879. int ev;
  880. if (!ppmu->cache_events)
  881. return -EINVAL;
  882. /* unpack config */
  883. type = config & 0xff;
  884. op = (config >> 8) & 0xff;
  885. result = (config >> 16) & 0xff;
  886. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  887. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  888. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  889. return -EINVAL;
  890. ev = (*ppmu->cache_events)[type][op][result];
  891. if (ev == 0)
  892. return -EOPNOTSUPP;
  893. if (ev == -1)
  894. return -EINVAL;
  895. *eventp = ev;
  896. return 0;
  897. }
  898. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  899. {
  900. u64 ev;
  901. unsigned long flags;
  902. struct perf_counter *ctrs[MAX_HWCOUNTERS];
  903. u64 events[MAX_HWCOUNTERS];
  904. unsigned int cflags[MAX_HWCOUNTERS];
  905. int n;
  906. int err;
  907. if (!ppmu)
  908. return ERR_PTR(-ENXIO);
  909. switch (counter->attr.type) {
  910. case PERF_TYPE_HARDWARE:
  911. ev = counter->attr.config;
  912. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  913. return ERR_PTR(-EOPNOTSUPP);
  914. ev = ppmu->generic_events[ev];
  915. break;
  916. case PERF_TYPE_HW_CACHE:
  917. err = hw_perf_cache_event(counter->attr.config, &ev);
  918. if (err)
  919. return ERR_PTR(err);
  920. break;
  921. case PERF_TYPE_RAW:
  922. ev = counter->attr.config;
  923. break;
  924. default:
  925. return ERR_PTR(-EINVAL);
  926. }
  927. counter->hw.config_base = ev;
  928. counter->hw.idx = 0;
  929. /*
  930. * If we are not running on a hypervisor, force the
  931. * exclude_hv bit to 0 so that we don't care what
  932. * the user set it to.
  933. */
  934. if (!firmware_has_feature(FW_FEATURE_LPAR))
  935. counter->attr.exclude_hv = 0;
  936. /*
  937. * If this is a per-task counter, then we can use
  938. * PM_RUN_* events interchangeably with their non RUN_*
  939. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  940. * XXX we should check if the task is an idle task.
  941. */
  942. flags = 0;
  943. if (counter->ctx->task)
  944. flags |= PPMU_ONLY_COUNT_RUN;
  945. /*
  946. * If this machine has limited counters, check whether this
  947. * event could go on a limited counter.
  948. */
  949. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  950. if (can_go_on_limited_pmc(counter, ev, flags)) {
  951. flags |= PPMU_LIMITED_PMC_OK;
  952. } else if (ppmu->limited_pmc_event(ev)) {
  953. /*
  954. * The requested event is on a limited PMC,
  955. * but we can't use a limited PMC; see if any
  956. * alternative goes on a normal PMC.
  957. */
  958. ev = normal_pmc_alternative(ev, flags);
  959. if (!ev)
  960. return ERR_PTR(-EINVAL);
  961. }
  962. }
  963. /*
  964. * If this is in a group, check if it can go on with all the
  965. * other hardware counters in the group. We assume the counter
  966. * hasn't been linked into its leader's sibling list at this point.
  967. */
  968. n = 0;
  969. if (counter->group_leader != counter) {
  970. n = collect_events(counter->group_leader, ppmu->n_counter - 1,
  971. ctrs, events, cflags);
  972. if (n < 0)
  973. return ERR_PTR(-EINVAL);
  974. }
  975. events[n] = ev;
  976. ctrs[n] = counter;
  977. cflags[n] = flags;
  978. if (check_excludes(ctrs, cflags, n, 1))
  979. return ERR_PTR(-EINVAL);
  980. if (power_check_constraints(events, cflags, n + 1))
  981. return ERR_PTR(-EINVAL);
  982. counter->hw.config = events[n];
  983. counter->hw.counter_base = cflags[n];
  984. counter->hw.last_period = counter->hw.sample_period;
  985. atomic64_set(&counter->hw.period_left, counter->hw.last_period);
  986. /*
  987. * See if we need to reserve the PMU.
  988. * If no counters are currently in use, then we have to take a
  989. * mutex to ensure that we don't race with another task doing
  990. * reserve_pmc_hardware or release_pmc_hardware.
  991. */
  992. err = 0;
  993. if (!atomic_inc_not_zero(&num_counters)) {
  994. mutex_lock(&pmc_reserve_mutex);
  995. if (atomic_read(&num_counters) == 0 &&
  996. reserve_pmc_hardware(perf_counter_interrupt))
  997. err = -EBUSY;
  998. else
  999. atomic_inc(&num_counters);
  1000. mutex_unlock(&pmc_reserve_mutex);
  1001. }
  1002. counter->destroy = hw_perf_counter_destroy;
  1003. if (err)
  1004. return ERR_PTR(err);
  1005. return &power_pmu;
  1006. }
  1007. /*
  1008. * A counter has overflowed; update its count and record
  1009. * things if requested. Note that interrupts are hard-disabled
  1010. * here so there is no possibility of being interrupted.
  1011. */
  1012. static void record_and_restart(struct perf_counter *counter, unsigned long val,
  1013. struct pt_regs *regs, int nmi)
  1014. {
  1015. u64 period = counter->hw.sample_period;
  1016. s64 prev, delta, left;
  1017. int record = 0;
  1018. /* we don't have to worry about interrupts here */
  1019. prev = atomic64_read(&counter->hw.prev_count);
  1020. delta = (val - prev) & 0xfffffffful;
  1021. atomic64_add(delta, &counter->count);
  1022. /*
  1023. * See if the total period for this counter has expired,
  1024. * and update for the next period.
  1025. */
  1026. val = 0;
  1027. left = atomic64_read(&counter->hw.period_left) - delta;
  1028. if (period) {
  1029. if (left <= 0) {
  1030. left += period;
  1031. if (left <= 0)
  1032. left = period;
  1033. record = 1;
  1034. }
  1035. if (left < 0x80000000LL)
  1036. val = 0x80000000LL - left;
  1037. }
  1038. /*
  1039. * Finally record data if requested.
  1040. */
  1041. if (record) {
  1042. struct perf_sample_data data = {
  1043. .regs = regs,
  1044. .addr = 0,
  1045. .period = counter->hw.last_period,
  1046. };
  1047. if (counter->attr.sample_type & PERF_SAMPLE_ADDR)
  1048. perf_get_data_addr(regs, &data.addr);
  1049. if (perf_counter_overflow(counter, nmi, &data)) {
  1050. /*
  1051. * Interrupts are coming too fast - throttle them
  1052. * by setting the counter to 0, so it will be
  1053. * at least 2^30 cycles until the next interrupt
  1054. * (assuming each counter counts at most 2 counts
  1055. * per cycle).
  1056. */
  1057. val = 0;
  1058. left = ~0ULL >> 1;
  1059. }
  1060. }
  1061. write_pmc(counter->hw.idx, val);
  1062. atomic64_set(&counter->hw.prev_count, val);
  1063. atomic64_set(&counter->hw.period_left, left);
  1064. perf_counter_update_userpage(counter);
  1065. }
  1066. /*
  1067. * Called from generic code to get the misc flags (i.e. processor mode)
  1068. * for an event.
  1069. */
  1070. unsigned long perf_misc_flags(struct pt_regs *regs)
  1071. {
  1072. u32 flags = perf_get_misc_flags(regs);
  1073. if (flags)
  1074. return flags;
  1075. return user_mode(regs) ? PERF_EVENT_MISC_USER :
  1076. PERF_EVENT_MISC_KERNEL;
  1077. }
  1078. /*
  1079. * Called from generic code to get the instruction pointer
  1080. * for an event.
  1081. */
  1082. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1083. {
  1084. unsigned long ip;
  1085. if (TRAP(regs) != 0xf00)
  1086. return regs->nip; /* not a PMU interrupt */
  1087. ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1088. return ip;
  1089. }
  1090. /*
  1091. * Performance monitor interrupt stuff
  1092. */
  1093. static void perf_counter_interrupt(struct pt_regs *regs)
  1094. {
  1095. int i;
  1096. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  1097. struct perf_counter *counter;
  1098. unsigned long val;
  1099. int found = 0;
  1100. int nmi;
  1101. if (cpuhw->n_limited)
  1102. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1103. mfspr(SPRN_PMC6));
  1104. perf_read_regs(regs);
  1105. nmi = perf_intr_is_nmi(regs);
  1106. if (nmi)
  1107. nmi_enter();
  1108. else
  1109. irq_enter();
  1110. for (i = 0; i < cpuhw->n_counters; ++i) {
  1111. counter = cpuhw->counter[i];
  1112. if (!counter->hw.idx || is_limited_pmc(counter->hw.idx))
  1113. continue;
  1114. val = read_pmc(counter->hw.idx);
  1115. if ((int)val < 0) {
  1116. /* counter has overflowed */
  1117. found = 1;
  1118. record_and_restart(counter, val, regs, nmi);
  1119. }
  1120. }
  1121. /*
  1122. * In case we didn't find and reset the counter that caused
  1123. * the interrupt, scan all counters and reset any that are
  1124. * negative, to avoid getting continual interrupts.
  1125. * Any that we processed in the previous loop will not be negative.
  1126. */
  1127. if (!found) {
  1128. for (i = 0; i < ppmu->n_counter; ++i) {
  1129. if (is_limited_pmc(i + 1))
  1130. continue;
  1131. val = read_pmc(i + 1);
  1132. if ((int)val < 0)
  1133. write_pmc(i + 1, 0);
  1134. }
  1135. }
  1136. /*
  1137. * Reset MMCR0 to its normal value. This will set PMXE and
  1138. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1139. * and thus allow interrupts to occur again.
  1140. * XXX might want to use MSR.PM to keep the counters frozen until
  1141. * we get back out of this interrupt.
  1142. */
  1143. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1144. if (nmi)
  1145. nmi_exit();
  1146. else
  1147. irq_exit();
  1148. }
  1149. void hw_perf_counter_setup(int cpu)
  1150. {
  1151. struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
  1152. memset(cpuhw, 0, sizeof(*cpuhw));
  1153. cpuhw->mmcr[0] = MMCR0_FC;
  1154. }
  1155. int register_power_pmu(struct power_pmu *pmu)
  1156. {
  1157. if (ppmu)
  1158. return -EBUSY; /* something's already registered */
  1159. ppmu = pmu;
  1160. pr_info("%s performance monitor hardware support registered\n",
  1161. pmu->name);
  1162. #ifdef MSR_HV
  1163. /*
  1164. * Use FCHV to ignore kernel events if MSR.HV is set.
  1165. */
  1166. if (mfmsr() & MSR_HV)
  1167. freeze_counters_kernel = MMCR0_FCHV;
  1168. #endif /* CONFIG_PPC64 */
  1169. return 0;
  1170. }