perf_event.c 32 KB

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