perf_event.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Performance event 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_event.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_events {
  22. int n_events;
  23. int n_percpu;
  24. int disabled;
  25. int n_added;
  26. int n_limited;
  27. u8 pmcs_enabled;
  28. struct perf_event *event[MAX_HWEVENTS];
  29. u64 events[MAX_HWEVENTS];
  30. unsigned int flags[MAX_HWEVENTS];
  31. unsigned long mmcr[3];
  32. struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
  33. u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
  34. u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  35. unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  36. unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  37. };
  38. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  39. struct power_pmu *ppmu;
  40. /*
  41. * Normally, to ignore kernel events we set the FCS (freeze counters
  42. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  43. * hypervisor bit set in the MSR, or if we are running on a processor
  44. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  45. * then we need to use the FCHV bit to ignore kernel events.
  46. */
  47. static unsigned int freeze_events_kernel = MMCR0_FCS;
  48. /*
  49. * 32-bit doesn't have MMCRA but does have an MMCR2,
  50. * and a few other names are different.
  51. */
  52. #ifdef CONFIG_PPC32
  53. #define MMCR0_FCHV 0
  54. #define MMCR0_PMCjCE MMCR0_PMCnCE
  55. #define SPRN_MMCRA SPRN_MMCR2
  56. #define MMCRA_SAMPLE_ENABLE 0
  57. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  58. {
  59. return 0;
  60. }
  61. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
  62. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  63. {
  64. return 0;
  65. }
  66. static inline void perf_read_regs(struct pt_regs *regs) { }
  67. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  68. {
  69. return 0;
  70. }
  71. #endif /* CONFIG_PPC32 */
  72. /*
  73. * Things that are specific to 64-bit implementations.
  74. */
  75. #ifdef CONFIG_PPC64
  76. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  77. {
  78. unsigned long mmcra = regs->dsisr;
  79. if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
  80. unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
  81. if (slot > 1)
  82. return 4 * (slot - 1);
  83. }
  84. return 0;
  85. }
  86. /*
  87. * The user wants a data address recorded.
  88. * If we're not doing instruction sampling, give them the SDAR
  89. * (sampled data address). If we are doing instruction sampling, then
  90. * only give them the SDAR if it corresponds to the instruction
  91. * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
  92. * bit in MMCRA.
  93. */
  94. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
  95. {
  96. unsigned long mmcra = regs->dsisr;
  97. unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
  98. POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
  99. if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
  100. *addrp = mfspr(SPRN_SDAR);
  101. }
  102. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  103. {
  104. unsigned long mmcra = regs->dsisr;
  105. unsigned long sihv = MMCRA_SIHV;
  106. unsigned long sipr = MMCRA_SIPR;
  107. if (TRAP(regs) != 0xf00)
  108. return 0; /* not a PMU interrupt */
  109. if (ppmu->flags & PPMU_ALT_SIPR) {
  110. sihv = POWER6_MMCRA_SIHV;
  111. sipr = POWER6_MMCRA_SIPR;
  112. }
  113. /* PR has priority over HV, so order below is important */
  114. if (mmcra & sipr)
  115. return PERF_RECORD_MISC_USER;
  116. if ((mmcra & sihv) && (freeze_events_kernel != MMCR0_FCHV))
  117. return PERF_RECORD_MISC_HYPERVISOR;
  118. return PERF_RECORD_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_event_interrupt(struct pt_regs *regs);
  138. void perf_event_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_id[].
  221. */
  222. static int power_check_constraints(struct cpu_hw_events *cpuhw,
  223. u64 event_id[], unsigned int cflags[],
  224. int n_ev)
  225. {
  226. unsigned long mask, value, nv;
  227. unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
  228. int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
  229. int i, j;
  230. unsigned long addf = ppmu->add_fields;
  231. unsigned long tadd = ppmu->test_adder;
  232. if (n_ev > ppmu->n_counter)
  233. return -1;
  234. /* First see if the events will go on as-is */
  235. for (i = 0; i < n_ev; ++i) {
  236. if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
  237. && !ppmu->limited_pmc_event(event_id[i])) {
  238. ppmu->get_alternatives(event_id[i], cflags[i],
  239. cpuhw->alternatives[i]);
  240. event_id[i] = cpuhw->alternatives[i][0];
  241. }
  242. if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
  243. &cpuhw->avalues[i][0]))
  244. return -1;
  245. }
  246. value = mask = 0;
  247. for (i = 0; i < n_ev; ++i) {
  248. nv = (value | cpuhw->avalues[i][0]) +
  249. (value & cpuhw->avalues[i][0] & addf);
  250. if ((((nv + tadd) ^ value) & mask) != 0 ||
  251. (((nv + tadd) ^ cpuhw->avalues[i][0]) &
  252. cpuhw->amasks[i][0]) != 0)
  253. break;
  254. value = nv;
  255. mask |= cpuhw->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_id[i], cflags[i],
  265. cpuhw->alternatives[i]);
  266. for (j = 1; j < n_alt[i]; ++j)
  267. ppmu->get_constraint(cpuhw->alternatives[i][j],
  268. &cpuhw->amasks[i][j],
  269. &cpuhw->avalues[i][j]);
  270. }
  271. /* enumerate all possibilities and see if any will work */
  272. i = 0;
  273. j = -1;
  274. value = mask = nv = 0;
  275. while (i < n_ev) {
  276. if (j >= 0) {
  277. /* we're backtracking, restore context */
  278. value = svalues[i];
  279. mask = smasks[i];
  280. j = choice[i];
  281. }
  282. /*
  283. * See if any alternative k for event_id i,
  284. * where k > j, will satisfy the constraints.
  285. */
  286. while (++j < n_alt[i]) {
  287. nv = (value | cpuhw->avalues[i][j]) +
  288. (value & cpuhw->avalues[i][j] & addf);
  289. if ((((nv + tadd) ^ value) & mask) == 0 &&
  290. (((nv + tadd) ^ cpuhw->avalues[i][j])
  291. & cpuhw->amasks[i][j]) == 0)
  292. break;
  293. }
  294. if (j >= n_alt[i]) {
  295. /*
  296. * No feasible alternative, backtrack
  297. * to event_id i-1 and continue enumerating its
  298. * alternatives from where we got up to.
  299. */
  300. if (--i < 0)
  301. return -1;
  302. } else {
  303. /*
  304. * Found a feasible alternative for event_id i,
  305. * remember where we got up to with this event_id,
  306. * go on to the next event_id, and start with
  307. * the first alternative for it.
  308. */
  309. choice[i] = j;
  310. svalues[i] = value;
  311. smasks[i] = mask;
  312. value = nv;
  313. mask |= cpuhw->amasks[i][j];
  314. ++i;
  315. j = -1;
  316. }
  317. }
  318. /* OK, we have a feasible combination, tell the caller the solution */
  319. for (i = 0; i < n_ev; ++i)
  320. event_id[i] = cpuhw->alternatives[i][choice[i]];
  321. return 0;
  322. }
  323. /*
  324. * Check if newly-added events have consistent settings for
  325. * exclude_{user,kernel,hv} with each other and any previously
  326. * added events.
  327. */
  328. static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
  329. int n_prev, int n_new)
  330. {
  331. int eu = 0, ek = 0, eh = 0;
  332. int i, n, first;
  333. struct perf_event *event;
  334. n = n_prev + n_new;
  335. if (n <= 1)
  336. return 0;
  337. first = 1;
  338. for (i = 0; i < n; ++i) {
  339. if (cflags[i] & PPMU_LIMITED_PMC_OK) {
  340. cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
  341. continue;
  342. }
  343. event = ctrs[i];
  344. if (first) {
  345. eu = event->attr.exclude_user;
  346. ek = event->attr.exclude_kernel;
  347. eh = event->attr.exclude_hv;
  348. first = 0;
  349. } else if (event->attr.exclude_user != eu ||
  350. event->attr.exclude_kernel != ek ||
  351. event->attr.exclude_hv != eh) {
  352. return -EAGAIN;
  353. }
  354. }
  355. if (eu || ek || eh)
  356. for (i = 0; i < n; ++i)
  357. if (cflags[i] & PPMU_LIMITED_PMC_OK)
  358. cflags[i] |= PPMU_LIMITED_PMC_REQD;
  359. return 0;
  360. }
  361. static void power_pmu_read(struct perf_event *event)
  362. {
  363. s64 val, delta, prev;
  364. if (!event->hw.idx)
  365. return;
  366. /*
  367. * Performance monitor interrupts come even when interrupts
  368. * are soft-disabled, as long as interrupts are hard-enabled.
  369. * Therefore we treat them like NMIs.
  370. */
  371. do {
  372. prev = atomic64_read(&event->hw.prev_count);
  373. barrier();
  374. val = read_pmc(event->hw.idx);
  375. } while (atomic64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  376. /* The counters are only 32 bits wide */
  377. delta = (val - prev) & 0xfffffffful;
  378. atomic64_add(delta, &event->count);
  379. atomic64_sub(delta, &event->hw.period_left);
  380. }
  381. /*
  382. * On some machines, PMC5 and PMC6 can't be written, don't respect
  383. * the freeze conditions, and don't generate interrupts. This tells
  384. * us if `event' is using such a PMC.
  385. */
  386. static int is_limited_pmc(int pmcnum)
  387. {
  388. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  389. && (pmcnum == 5 || pmcnum == 6);
  390. }
  391. static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
  392. unsigned long pmc5, unsigned long pmc6)
  393. {
  394. struct perf_event *event;
  395. u64 val, prev, delta;
  396. int i;
  397. for (i = 0; i < cpuhw->n_limited; ++i) {
  398. event = cpuhw->limited_counter[i];
  399. if (!event->hw.idx)
  400. continue;
  401. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  402. prev = atomic64_read(&event->hw.prev_count);
  403. event->hw.idx = 0;
  404. delta = (val - prev) & 0xfffffffful;
  405. atomic64_add(delta, &event->count);
  406. }
  407. }
  408. static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
  409. unsigned long pmc5, unsigned long pmc6)
  410. {
  411. struct perf_event *event;
  412. u64 val;
  413. int i;
  414. for (i = 0; i < cpuhw->n_limited; ++i) {
  415. event = cpuhw->limited_counter[i];
  416. event->hw.idx = cpuhw->limited_hwidx[i];
  417. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  418. atomic64_set(&event->hw.prev_count, val);
  419. perf_event_update_userpage(event);
  420. }
  421. }
  422. /*
  423. * Since limited events don't respect the freeze conditions, we
  424. * have to read them immediately after freezing or unfreezing the
  425. * other events. We try to keep the values from the limited
  426. * events as consistent as possible by keeping the delay (in
  427. * cycles and instructions) between freezing/unfreezing and reading
  428. * the limited events as small and consistent as possible.
  429. * Therefore, if any limited events are in use, we read them
  430. * both, and always in the same order, to minimize variability,
  431. * and do it inside the same asm that writes MMCR0.
  432. */
  433. static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
  434. {
  435. unsigned long pmc5, pmc6;
  436. if (!cpuhw->n_limited) {
  437. mtspr(SPRN_MMCR0, mmcr0);
  438. return;
  439. }
  440. /*
  441. * Write MMCR0, then read PMC5 and PMC6 immediately.
  442. * To ensure we don't get a performance monitor interrupt
  443. * between writing MMCR0 and freezing/thawing the limited
  444. * events, we first write MMCR0 with the event overflow
  445. * interrupt enable bits turned off.
  446. */
  447. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  448. : "=&r" (pmc5), "=&r" (pmc6)
  449. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  450. "i" (SPRN_MMCR0),
  451. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  452. if (mmcr0 & MMCR0_FC)
  453. freeze_limited_counters(cpuhw, pmc5, pmc6);
  454. else
  455. thaw_limited_counters(cpuhw, pmc5, pmc6);
  456. /*
  457. * Write the full MMCR0 including the event overflow interrupt
  458. * enable bits, if necessary.
  459. */
  460. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  461. mtspr(SPRN_MMCR0, mmcr0);
  462. }
  463. /*
  464. * Disable all events to prevent PMU interrupts and to allow
  465. * events to be added or removed.
  466. */
  467. void hw_perf_disable(void)
  468. {
  469. struct cpu_hw_events *cpuhw;
  470. unsigned long flags;
  471. if (!ppmu)
  472. return;
  473. local_irq_save(flags);
  474. cpuhw = &__get_cpu_var(cpu_hw_events);
  475. if (!cpuhw->disabled) {
  476. cpuhw->disabled = 1;
  477. cpuhw->n_added = 0;
  478. /*
  479. * Check if we ever enabled the PMU on this cpu.
  480. */
  481. if (!cpuhw->pmcs_enabled) {
  482. ppc_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 events
  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 events if disable == 0.
  506. * If we were previously disabled and events were added, then
  507. * put the new config on the PMU.
  508. */
  509. void hw_perf_enable(void)
  510. {
  511. struct perf_event *event;
  512. struct cpu_hw_events *cpuhw;
  513. unsigned long flags;
  514. long i;
  515. unsigned long val;
  516. s64 left;
  517. unsigned int hwc_index[MAX_HWEVENTS];
  518. int n_lim;
  519. int idx;
  520. if (!ppmu)
  521. return;
  522. local_irq_save(flags);
  523. cpuhw = &__get_cpu_var(cpu_hw_events);
  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 events,
  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 events).
  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_events == 0)
  539. ppc_set_pmu_inuse(0);
  540. goto out_enable;
  541. }
  542. /*
  543. * Compute MMCR* values for the new set of events
  544. */
  545. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, 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 event.
  554. * We have already checked that all events have the
  555. * same values for these bits as the first event.
  556. */
  557. event = cpuhw->event[0];
  558. if (event->attr.exclude_user)
  559. cpuhw->mmcr[0] |= MMCR0_FCP;
  560. if (event->attr.exclude_kernel)
  561. cpuhw->mmcr[0] |= freeze_events_kernel;
  562. if (event->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 events to their initial values.
  567. * Then unfreeze the events.
  568. */
  569. ppc_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 events that need to move
  576. * to another PMC.
  577. */
  578. for (i = 0; i < cpuhw->n_events; ++i) {
  579. event = cpuhw->event[i];
  580. if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
  581. power_pmu_read(event);
  582. write_pmc(event->hw.idx, 0);
  583. event->hw.idx = 0;
  584. }
  585. }
  586. /*
  587. * Initialize the PMCs for all the new and moved events.
  588. */
  589. cpuhw->n_limited = n_lim = 0;
  590. for (i = 0; i < cpuhw->n_events; ++i) {
  591. event = cpuhw->event[i];
  592. if (event->hw.idx)
  593. continue;
  594. idx = hwc_index[i] + 1;
  595. if (is_limited_pmc(idx)) {
  596. cpuhw->limited_counter[n_lim] = event;
  597. cpuhw->limited_hwidx[n_lim] = idx;
  598. ++n_lim;
  599. continue;
  600. }
  601. val = 0;
  602. if (event->hw.sample_period) {
  603. left = atomic64_read(&event->hw.period_left);
  604. if (left < 0x80000000L)
  605. val = 0x80000000L - left;
  606. }
  607. atomic64_set(&event->hw.prev_count, val);
  608. event->hw.idx = idx;
  609. write_pmc(idx, val);
  610. perf_event_update_userpage(event);
  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_event *group, int max_count,
  628. struct perf_event *ctrs[], u64 *events,
  629. unsigned int *flags)
  630. {
  631. int n = 0;
  632. struct perf_event *event;
  633. if (!is_software_event(group)) {
  634. if (n >= max_count)
  635. return -1;
  636. ctrs[n] = group;
  637. flags[n] = group->hw.event_base;
  638. events[n++] = group->hw.config;
  639. }
  640. list_for_each_entry(event, &group->sibling_list, group_entry) {
  641. if (!is_software_event(event) &&
  642. event->state != PERF_EVENT_STATE_OFF) {
  643. if (n >= max_count)
  644. return -1;
  645. ctrs[n] = event;
  646. flags[n] = event->hw.event_base;
  647. events[n++] = event->hw.config;
  648. }
  649. }
  650. return n;
  651. }
  652. static void event_sched_in(struct perf_event *event, int cpu)
  653. {
  654. event->state = PERF_EVENT_STATE_ACTIVE;
  655. event->oncpu = cpu;
  656. event->tstamp_running += event->ctx->time - event->tstamp_stopped;
  657. if (is_software_event(event))
  658. event->pmu->enable(event);
  659. }
  660. /*
  661. * Called to enable a whole group of events.
  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_event *group_leader,
  667. struct perf_cpu_context *cpuctx,
  668. struct perf_event_context *ctx, int cpu)
  669. {
  670. struct cpu_hw_events *cpuhw;
  671. long i, n, n0;
  672. struct perf_event *sub;
  673. if (!ppmu)
  674. return 0;
  675. cpuhw = &__get_cpu_var(cpu_hw_events);
  676. n0 = cpuhw->n_events;
  677. n = collect_events(group_leader, ppmu->n_counter - n0,
  678. &cpuhw->event[n0], &cpuhw->events[n0],
  679. &cpuhw->flags[n0]);
  680. if (n < 0)
  681. return -EAGAIN;
  682. if (check_excludes(cpuhw->event, cpuhw->flags, n0, n))
  683. return -EAGAIN;
  684. i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n + n0);
  685. if (i < 0)
  686. return -EAGAIN;
  687. cpuhw->n_events = n0 + n;
  688. cpuhw->n_added += n;
  689. /*
  690. * OK, this group can go on; update event states etc.,
  691. * and enable any software events
  692. */
  693. for (i = n0; i < n0 + n; ++i)
  694. cpuhw->event[i]->hw.config = cpuhw->events[i];
  695. cpuctx->active_oncpu += n;
  696. n = 1;
  697. event_sched_in(group_leader, cpu);
  698. list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
  699. if (sub->state != PERF_EVENT_STATE_OFF) {
  700. event_sched_in(sub, cpu);
  701. ++n;
  702. }
  703. }
  704. ctx->nr_active += n;
  705. return 1;
  706. }
  707. /*
  708. * Add a event to the PMU.
  709. * If all events 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_event *event)
  714. {
  715. struct cpu_hw_events *cpuhw;
  716. unsigned long flags;
  717. int n0;
  718. int ret = -EAGAIN;
  719. local_irq_save(flags);
  720. perf_disable();
  721. /*
  722. * Add the event 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_events);
  726. n0 = cpuhw->n_events;
  727. if (n0 >= ppmu->n_counter)
  728. goto out;
  729. cpuhw->event[n0] = event;
  730. cpuhw->events[n0] = event->hw.config;
  731. cpuhw->flags[n0] = event->hw.event_base;
  732. if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
  733. goto out;
  734. if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
  735. goto out;
  736. event->hw.config = cpuhw->events[n0];
  737. ++cpuhw->n_events;
  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 event from the PMU.
  747. */
  748. static void power_pmu_disable(struct perf_event *event)
  749. {
  750. struct cpu_hw_events *cpuhw;
  751. long i;
  752. unsigned long flags;
  753. local_irq_save(flags);
  754. perf_disable();
  755. power_pmu_read(event);
  756. cpuhw = &__get_cpu_var(cpu_hw_events);
  757. for (i = 0; i < cpuhw->n_events; ++i) {
  758. if (event == cpuhw->event[i]) {
  759. while (++i < cpuhw->n_events)
  760. cpuhw->event[i-1] = cpuhw->event[i];
  761. --cpuhw->n_events;
  762. ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
  763. if (event->hw.idx) {
  764. write_pmc(event->hw.idx, 0);
  765. event->hw.idx = 0;
  766. }
  767. perf_event_update_userpage(event);
  768. break;
  769. }
  770. }
  771. for (i = 0; i < cpuhw->n_limited; ++i)
  772. if (event == 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_events == 0) {
  782. /* disable exceptions if no events 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 event after they were throttled
  790. * because they were coming too fast.
  791. */
  792. static void power_pmu_unthrottle(struct perf_event *event)
  793. {
  794. s64 val, left;
  795. unsigned long flags;
  796. if (!event->hw.idx || !event->hw.sample_period)
  797. return;
  798. local_irq_save(flags);
  799. perf_disable();
  800. power_pmu_read(event);
  801. left = event->hw.sample_period;
  802. event->hw.last_period = left;
  803. val = 0;
  804. if (left < 0x80000000L)
  805. val = 0x80000000L - left;
  806. write_pmc(event->hw.idx, val);
  807. atomic64_set(&event->hw.prev_count, val);
  808. atomic64_set(&event->hw.period_left, left);
  809. perf_event_update_userpage(event);
  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 event on a limited PMC,
  821. * or 0 if not.
  822. * A event 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_event *event, u64 ev,
  827. unsigned int flags)
  828. {
  829. int n;
  830. u64 alt[MAX_EVENT_ALTERNATIVES];
  831. if (event->attr.exclude_user
  832. || event->attr.exclude_kernel
  833. || event->attr.exclude_hv
  834. || event->attr.sample_period)
  835. return 0;
  836. if (ppmu->limited_pmc_event(ev))
  837. return 1;
  838. /*
  839. * The requested event_id 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_id that goes on a normal PMC, if possible,
  850. * and return the event_id code, or 0 if there is no such alternative.
  851. * (Note: event_id 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_events counting hardware events */
  864. static atomic_t num_events;
  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_event.
  869. */
  870. static void hw_perf_event_destroy(struct perf_event *event)
  871. {
  872. if (!atomic_add_unless(&num_events, -1, 1)) {
  873. mutex_lock(&pmc_reserve_mutex);
  874. if (atomic_dec_return(&num_events) == 0)
  875. release_pmc_hardware();
  876. mutex_unlock(&pmc_reserve_mutex);
  877. }
  878. }
  879. /*
  880. * Translate a generic cache event_id config to a raw event_id 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_event_init(struct perf_event *event)
  905. {
  906. u64 ev;
  907. unsigned long flags;
  908. struct perf_event *ctrs[MAX_HWEVENTS];
  909. u64 events[MAX_HWEVENTS];
  910. unsigned int cflags[MAX_HWEVENTS];
  911. int n;
  912. int err;
  913. struct cpu_hw_events *cpuhw;
  914. if (!ppmu)
  915. return ERR_PTR(-ENXIO);
  916. switch (event->attr.type) {
  917. case PERF_TYPE_HARDWARE:
  918. ev = event->attr.config;
  919. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  920. return ERR_PTR(-EOPNOTSUPP);
  921. ev = ppmu->generic_events[ev];
  922. break;
  923. case PERF_TYPE_HW_CACHE:
  924. err = hw_perf_cache_event(event->attr.config, &ev);
  925. if (err)
  926. return ERR_PTR(err);
  927. break;
  928. case PERF_TYPE_RAW:
  929. ev = event->attr.config;
  930. break;
  931. default:
  932. return ERR_PTR(-EINVAL);
  933. }
  934. event->hw.config_base = ev;
  935. event->hw.idx = 0;
  936. /*
  937. * If we are not running on a hypervisor, force the
  938. * exclude_hv bit to 0 so that we don't care what
  939. * the user set it to.
  940. */
  941. if (!firmware_has_feature(FW_FEATURE_LPAR))
  942. event->attr.exclude_hv = 0;
  943. /*
  944. * If this is a per-task event, then we can use
  945. * PM_RUN_* events interchangeably with their non RUN_*
  946. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  947. * XXX we should check if the task is an idle task.
  948. */
  949. flags = 0;
  950. if (event->ctx->task)
  951. flags |= PPMU_ONLY_COUNT_RUN;
  952. /*
  953. * If this machine has limited events, check whether this
  954. * event_id could go on a limited event.
  955. */
  956. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  957. if (can_go_on_limited_pmc(event, ev, flags)) {
  958. flags |= PPMU_LIMITED_PMC_OK;
  959. } else if (ppmu->limited_pmc_event(ev)) {
  960. /*
  961. * The requested event_id is on a limited PMC,
  962. * but we can't use a limited PMC; see if any
  963. * alternative goes on a normal PMC.
  964. */
  965. ev = normal_pmc_alternative(ev, flags);
  966. if (!ev)
  967. return ERR_PTR(-EINVAL);
  968. }
  969. }
  970. /*
  971. * If this is in a group, check if it can go on with all the
  972. * other hardware events in the group. We assume the event
  973. * hasn't been linked into its leader's sibling list at this point.
  974. */
  975. n = 0;
  976. if (event->group_leader != event) {
  977. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  978. ctrs, events, cflags);
  979. if (n < 0)
  980. return ERR_PTR(-EINVAL);
  981. }
  982. events[n] = ev;
  983. ctrs[n] = event;
  984. cflags[n] = flags;
  985. if (check_excludes(ctrs, cflags, n, 1))
  986. return ERR_PTR(-EINVAL);
  987. cpuhw = &get_cpu_var(cpu_hw_events);
  988. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  989. put_cpu_var(cpu_hw_events);
  990. if (err)
  991. return ERR_PTR(-EINVAL);
  992. event->hw.config = events[n];
  993. event->hw.event_base = cflags[n];
  994. event->hw.last_period = event->hw.sample_period;
  995. atomic64_set(&event->hw.period_left, event->hw.last_period);
  996. /*
  997. * See if we need to reserve the PMU.
  998. * If no events are currently in use, then we have to take a
  999. * mutex to ensure that we don't race with another task doing
  1000. * reserve_pmc_hardware or release_pmc_hardware.
  1001. */
  1002. err = 0;
  1003. if (!atomic_inc_not_zero(&num_events)) {
  1004. mutex_lock(&pmc_reserve_mutex);
  1005. if (atomic_read(&num_events) == 0 &&
  1006. reserve_pmc_hardware(perf_event_interrupt))
  1007. err = -EBUSY;
  1008. else
  1009. atomic_inc(&num_events);
  1010. mutex_unlock(&pmc_reserve_mutex);
  1011. }
  1012. event->destroy = hw_perf_event_destroy;
  1013. if (err)
  1014. return ERR_PTR(err);
  1015. return &power_pmu;
  1016. }
  1017. /*
  1018. * A counter has overflowed; update its count and record
  1019. * things if requested. Note that interrupts are hard-disabled
  1020. * here so there is no possibility of being interrupted.
  1021. */
  1022. static void record_and_restart(struct perf_event *event, unsigned long val,
  1023. struct pt_regs *regs, int nmi)
  1024. {
  1025. u64 period = event->hw.sample_period;
  1026. s64 prev, delta, left;
  1027. int record = 0;
  1028. /* we don't have to worry about interrupts here */
  1029. prev = atomic64_read(&event->hw.prev_count);
  1030. delta = (val - prev) & 0xfffffffful;
  1031. atomic64_add(delta, &event->count);
  1032. /*
  1033. * See if the total period for this event has expired,
  1034. * and update for the next period.
  1035. */
  1036. val = 0;
  1037. left = atomic64_read(&event->hw.period_left) - delta;
  1038. if (period) {
  1039. if (left <= 0) {
  1040. left += period;
  1041. if (left <= 0)
  1042. left = period;
  1043. record = 1;
  1044. }
  1045. if (left < 0x80000000LL)
  1046. val = 0x80000000LL - left;
  1047. }
  1048. /*
  1049. * Finally record data if requested.
  1050. */
  1051. if (record) {
  1052. struct perf_sample_data data = {
  1053. .addr = 0,
  1054. .period = event->hw.last_period,
  1055. };
  1056. if (event->attr.sample_type & PERF_SAMPLE_ADDR)
  1057. perf_get_data_addr(regs, &data.addr);
  1058. if (perf_event_overflow(event, nmi, &data, regs)) {
  1059. /*
  1060. * Interrupts are coming too fast - throttle them
  1061. * by setting the event to 0, so it will be
  1062. * at least 2^30 cycles until the next interrupt
  1063. * (assuming each event counts at most 2 counts
  1064. * per cycle).
  1065. */
  1066. val = 0;
  1067. left = ~0ULL >> 1;
  1068. }
  1069. }
  1070. write_pmc(event->hw.idx, val);
  1071. atomic64_set(&event->hw.prev_count, val);
  1072. atomic64_set(&event->hw.period_left, left);
  1073. perf_event_update_userpage(event);
  1074. }
  1075. /*
  1076. * Called from generic code to get the misc flags (i.e. processor mode)
  1077. * for an event_id.
  1078. */
  1079. unsigned long perf_misc_flags(struct pt_regs *regs)
  1080. {
  1081. u32 flags = perf_get_misc_flags(regs);
  1082. if (flags)
  1083. return flags;
  1084. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1085. PERF_RECORD_MISC_KERNEL;
  1086. }
  1087. /*
  1088. * Called from generic code to get the instruction pointer
  1089. * for an event_id.
  1090. */
  1091. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1092. {
  1093. unsigned long ip;
  1094. if (TRAP(regs) != 0xf00)
  1095. return regs->nip; /* not a PMU interrupt */
  1096. ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1097. return ip;
  1098. }
  1099. /*
  1100. * Performance monitor interrupt stuff
  1101. */
  1102. static void perf_event_interrupt(struct pt_regs *regs)
  1103. {
  1104. int i;
  1105. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1106. struct perf_event *event;
  1107. unsigned long val;
  1108. int found = 0;
  1109. int nmi;
  1110. if (cpuhw->n_limited)
  1111. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1112. mfspr(SPRN_PMC6));
  1113. perf_read_regs(regs);
  1114. nmi = perf_intr_is_nmi(regs);
  1115. if (nmi)
  1116. nmi_enter();
  1117. else
  1118. irq_enter();
  1119. for (i = 0; i < cpuhw->n_events; ++i) {
  1120. event = cpuhw->event[i];
  1121. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1122. continue;
  1123. val = read_pmc(event->hw.idx);
  1124. if ((int)val < 0) {
  1125. /* event has overflowed */
  1126. found = 1;
  1127. record_and_restart(event, val, regs, nmi);
  1128. }
  1129. }
  1130. /*
  1131. * In case we didn't find and reset the event that caused
  1132. * the interrupt, scan all events and reset any that are
  1133. * negative, to avoid getting continual interrupts.
  1134. * Any that we processed in the previous loop will not be negative.
  1135. */
  1136. if (!found) {
  1137. for (i = 0; i < ppmu->n_counter; ++i) {
  1138. if (is_limited_pmc(i + 1))
  1139. continue;
  1140. val = read_pmc(i + 1);
  1141. if ((int)val < 0)
  1142. write_pmc(i + 1, 0);
  1143. }
  1144. }
  1145. /*
  1146. * Reset MMCR0 to its normal value. This will set PMXE and
  1147. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1148. * and thus allow interrupts to occur again.
  1149. * XXX might want to use MSR.PM to keep the events frozen until
  1150. * we get back out of this interrupt.
  1151. */
  1152. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1153. if (nmi)
  1154. nmi_exit();
  1155. else
  1156. irq_exit();
  1157. }
  1158. void hw_perf_event_setup(int cpu)
  1159. {
  1160. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1161. if (!ppmu)
  1162. return;
  1163. memset(cpuhw, 0, sizeof(*cpuhw));
  1164. cpuhw->mmcr[0] = MMCR0_FC;
  1165. }
  1166. int register_power_pmu(struct power_pmu *pmu)
  1167. {
  1168. if (ppmu)
  1169. return -EBUSY; /* something's already registered */
  1170. ppmu = pmu;
  1171. pr_info("%s performance monitor hardware support registered\n",
  1172. pmu->name);
  1173. #ifdef MSR_HV
  1174. /*
  1175. * Use FCHV to ignore kernel events if MSR.HV is set.
  1176. */
  1177. if (mfmsr() & MSR_HV)
  1178. freeze_events_kernel = MMCR0_FCHV;
  1179. #endif /* CONFIG_PPC64 */
  1180. return 0;
  1181. }