perf_event.c 32 KB

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