perf_event.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 = atomic64_read(&event->hw.prev_count);
  375. barrier();
  376. val = read_pmc(event->hw.idx);
  377. } while (atomic64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  378. /* The counters are only 32 bits wide */
  379. delta = (val - prev) & 0xfffffffful;
  380. atomic64_add(delta, &event->count);
  381. atomic64_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 = atomic64_read(&event->hw.prev_count);
  405. event->hw.idx = 0;
  406. delta = (val - prev) & 0xfffffffful;
  407. atomic64_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. atomic64_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 = atomic64_read(&event->hw.period_left);
  606. if (left < 0x80000000L)
  607. val = 0x80000000L - left;
  608. }
  609. atomic64_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_STARTED)
  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. atomic64_set(&event->hw.prev_count, val);
  766. atomic64_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_STARTED;
  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_STARTED;
  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. return 0;
  813. }
  814. struct pmu power_pmu = {
  815. .enable = power_pmu_enable,
  816. .disable = power_pmu_disable,
  817. .read = power_pmu_read,
  818. .unthrottle = power_pmu_unthrottle,
  819. .start_txn = power_pmu_start_txn,
  820. .cancel_txn = power_pmu_cancel_txn,
  821. .commit_txn = power_pmu_commit_txn,
  822. };
  823. /*
  824. * Return 1 if we might be able to put event on a limited PMC,
  825. * or 0 if not.
  826. * A event can only go on a limited PMC if it counts something
  827. * that a limited PMC can count, doesn't require interrupts, and
  828. * doesn't exclude any processor mode.
  829. */
  830. static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
  831. unsigned int flags)
  832. {
  833. int n;
  834. u64 alt[MAX_EVENT_ALTERNATIVES];
  835. if (event->attr.exclude_user
  836. || event->attr.exclude_kernel
  837. || event->attr.exclude_hv
  838. || event->attr.sample_period)
  839. return 0;
  840. if (ppmu->limited_pmc_event(ev))
  841. return 1;
  842. /*
  843. * The requested event_id isn't on a limited PMC already;
  844. * see if any alternative code goes on a limited PMC.
  845. */
  846. if (!ppmu->get_alternatives)
  847. return 0;
  848. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  849. n = ppmu->get_alternatives(ev, flags, alt);
  850. return n > 0;
  851. }
  852. /*
  853. * Find an alternative event_id that goes on a normal PMC, if possible,
  854. * and return the event_id code, or 0 if there is no such alternative.
  855. * (Note: event_id code 0 is "don't count" on all machines.)
  856. */
  857. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  858. {
  859. u64 alt[MAX_EVENT_ALTERNATIVES];
  860. int n;
  861. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  862. n = ppmu->get_alternatives(ev, flags, alt);
  863. if (!n)
  864. return 0;
  865. return alt[0];
  866. }
  867. /* Number of perf_events counting hardware events */
  868. static atomic_t num_events;
  869. /* Used to avoid races in calling reserve/release_pmc_hardware */
  870. static DEFINE_MUTEX(pmc_reserve_mutex);
  871. /*
  872. * Release the PMU if this is the last perf_event.
  873. */
  874. static void hw_perf_event_destroy(struct perf_event *event)
  875. {
  876. if (!atomic_add_unless(&num_events, -1, 1)) {
  877. mutex_lock(&pmc_reserve_mutex);
  878. if (atomic_dec_return(&num_events) == 0)
  879. release_pmc_hardware();
  880. mutex_unlock(&pmc_reserve_mutex);
  881. }
  882. }
  883. /*
  884. * Translate a generic cache event_id config to a raw event_id code.
  885. */
  886. static int hw_perf_cache_event(u64 config, u64 *eventp)
  887. {
  888. unsigned long type, op, result;
  889. int ev;
  890. if (!ppmu->cache_events)
  891. return -EINVAL;
  892. /* unpack config */
  893. type = config & 0xff;
  894. op = (config >> 8) & 0xff;
  895. result = (config >> 16) & 0xff;
  896. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  897. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  898. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  899. return -EINVAL;
  900. ev = (*ppmu->cache_events)[type][op][result];
  901. if (ev == 0)
  902. return -EOPNOTSUPP;
  903. if (ev == -1)
  904. return -EINVAL;
  905. *eventp = ev;
  906. return 0;
  907. }
  908. const struct pmu *hw_perf_event_init(struct perf_event *event)
  909. {
  910. u64 ev;
  911. unsigned long flags;
  912. struct perf_event *ctrs[MAX_HWEVENTS];
  913. u64 events[MAX_HWEVENTS];
  914. unsigned int cflags[MAX_HWEVENTS];
  915. int n;
  916. int err;
  917. struct cpu_hw_events *cpuhw;
  918. if (!ppmu)
  919. return ERR_PTR(-ENXIO);
  920. switch (event->attr.type) {
  921. case PERF_TYPE_HARDWARE:
  922. ev = event->attr.config;
  923. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  924. return ERR_PTR(-EOPNOTSUPP);
  925. ev = ppmu->generic_events[ev];
  926. break;
  927. case PERF_TYPE_HW_CACHE:
  928. err = hw_perf_cache_event(event->attr.config, &ev);
  929. if (err)
  930. return ERR_PTR(err);
  931. break;
  932. case PERF_TYPE_RAW:
  933. ev = event->attr.config;
  934. break;
  935. default:
  936. return ERR_PTR(-EINVAL);
  937. }
  938. event->hw.config_base = ev;
  939. event->hw.idx = 0;
  940. /*
  941. * If we are not running on a hypervisor, force the
  942. * exclude_hv bit to 0 so that we don't care what
  943. * the user set it to.
  944. */
  945. if (!firmware_has_feature(FW_FEATURE_LPAR))
  946. event->attr.exclude_hv = 0;
  947. /*
  948. * If this is a per-task event, then we can use
  949. * PM_RUN_* events interchangeably with their non RUN_*
  950. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  951. * XXX we should check if the task is an idle task.
  952. */
  953. flags = 0;
  954. if (event->ctx->task)
  955. flags |= PPMU_ONLY_COUNT_RUN;
  956. /*
  957. * If this machine has limited events, check whether this
  958. * event_id could go on a limited event.
  959. */
  960. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  961. if (can_go_on_limited_pmc(event, ev, flags)) {
  962. flags |= PPMU_LIMITED_PMC_OK;
  963. } else if (ppmu->limited_pmc_event(ev)) {
  964. /*
  965. * The requested event_id is on a limited PMC,
  966. * but we can't use a limited PMC; see if any
  967. * alternative goes on a normal PMC.
  968. */
  969. ev = normal_pmc_alternative(ev, flags);
  970. if (!ev)
  971. return ERR_PTR(-EINVAL);
  972. }
  973. }
  974. /*
  975. * If this is in a group, check if it can go on with all the
  976. * other hardware events in the group. We assume the event
  977. * hasn't been linked into its leader's sibling list at this point.
  978. */
  979. n = 0;
  980. if (event->group_leader != event) {
  981. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  982. ctrs, events, cflags);
  983. if (n < 0)
  984. return ERR_PTR(-EINVAL);
  985. }
  986. events[n] = ev;
  987. ctrs[n] = event;
  988. cflags[n] = flags;
  989. if (check_excludes(ctrs, cflags, n, 1))
  990. return ERR_PTR(-EINVAL);
  991. cpuhw = &get_cpu_var(cpu_hw_events);
  992. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  993. put_cpu_var(cpu_hw_events);
  994. if (err)
  995. return ERR_PTR(-EINVAL);
  996. event->hw.config = events[n];
  997. event->hw.event_base = cflags[n];
  998. event->hw.last_period = event->hw.sample_period;
  999. atomic64_set(&event->hw.period_left, event->hw.last_period);
  1000. /*
  1001. * See if we need to reserve the PMU.
  1002. * If no events are currently in use, then we have to take a
  1003. * mutex to ensure that we don't race with another task doing
  1004. * reserve_pmc_hardware or release_pmc_hardware.
  1005. */
  1006. err = 0;
  1007. if (!atomic_inc_not_zero(&num_events)) {
  1008. mutex_lock(&pmc_reserve_mutex);
  1009. if (atomic_read(&num_events) == 0 &&
  1010. reserve_pmc_hardware(perf_event_interrupt))
  1011. err = -EBUSY;
  1012. else
  1013. atomic_inc(&num_events);
  1014. mutex_unlock(&pmc_reserve_mutex);
  1015. }
  1016. event->destroy = hw_perf_event_destroy;
  1017. if (err)
  1018. return ERR_PTR(err);
  1019. return &power_pmu;
  1020. }
  1021. /*
  1022. * A counter has overflowed; update its count and record
  1023. * things if requested. Note that interrupts are hard-disabled
  1024. * here so there is no possibility of being interrupted.
  1025. */
  1026. static void record_and_restart(struct perf_event *event, unsigned long val,
  1027. struct pt_regs *regs, int nmi)
  1028. {
  1029. u64 period = event->hw.sample_period;
  1030. s64 prev, delta, left;
  1031. int record = 0;
  1032. /* we don't have to worry about interrupts here */
  1033. prev = atomic64_read(&event->hw.prev_count);
  1034. delta = (val - prev) & 0xfffffffful;
  1035. atomic64_add(delta, &event->count);
  1036. /*
  1037. * See if the total period for this event has expired,
  1038. * and update for the next period.
  1039. */
  1040. val = 0;
  1041. left = atomic64_read(&event->hw.period_left) - delta;
  1042. if (period) {
  1043. if (left <= 0) {
  1044. left += period;
  1045. if (left <= 0)
  1046. left = period;
  1047. record = 1;
  1048. }
  1049. if (left < 0x80000000LL)
  1050. val = 0x80000000LL - left;
  1051. }
  1052. /*
  1053. * Finally record data if requested.
  1054. */
  1055. if (record) {
  1056. struct perf_sample_data data;
  1057. perf_sample_data_init(&data, ~0ULL);
  1058. data.period = event->hw.last_period;
  1059. if (event->attr.sample_type & PERF_SAMPLE_ADDR)
  1060. perf_get_data_addr(regs, &data.addr);
  1061. if (perf_event_overflow(event, nmi, &data, regs)) {
  1062. /*
  1063. * Interrupts are coming too fast - throttle them
  1064. * by setting the event to 0, so it will be
  1065. * at least 2^30 cycles until the next interrupt
  1066. * (assuming each event counts at most 2 counts
  1067. * per cycle).
  1068. */
  1069. val = 0;
  1070. left = ~0ULL >> 1;
  1071. }
  1072. }
  1073. write_pmc(event->hw.idx, val);
  1074. atomic64_set(&event->hw.prev_count, val);
  1075. atomic64_set(&event->hw.period_left, left);
  1076. perf_event_update_userpage(event);
  1077. }
  1078. /*
  1079. * Called from generic code to get the misc flags (i.e. processor mode)
  1080. * for an event_id.
  1081. */
  1082. unsigned long perf_misc_flags(struct pt_regs *regs)
  1083. {
  1084. u32 flags = perf_get_misc_flags(regs);
  1085. if (flags)
  1086. return flags;
  1087. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1088. PERF_RECORD_MISC_KERNEL;
  1089. }
  1090. /*
  1091. * Called from generic code to get the instruction pointer
  1092. * for an event_id.
  1093. */
  1094. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1095. {
  1096. unsigned long ip;
  1097. if (TRAP(regs) != 0xf00)
  1098. return regs->nip; /* not a PMU interrupt */
  1099. ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1100. return ip;
  1101. }
  1102. /*
  1103. * Performance monitor interrupt stuff
  1104. */
  1105. static void perf_event_interrupt(struct pt_regs *regs)
  1106. {
  1107. int i;
  1108. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1109. struct perf_event *event;
  1110. unsigned long val;
  1111. int found = 0;
  1112. int nmi;
  1113. if (cpuhw->n_limited)
  1114. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1115. mfspr(SPRN_PMC6));
  1116. perf_read_regs(regs);
  1117. nmi = perf_intr_is_nmi(regs);
  1118. if (nmi)
  1119. nmi_enter();
  1120. else
  1121. irq_enter();
  1122. for (i = 0; i < cpuhw->n_events; ++i) {
  1123. event = cpuhw->event[i];
  1124. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1125. continue;
  1126. val = read_pmc(event->hw.idx);
  1127. if ((int)val < 0) {
  1128. /* event has overflowed */
  1129. found = 1;
  1130. record_and_restart(event, val, regs, nmi);
  1131. }
  1132. }
  1133. /*
  1134. * In case we didn't find and reset the event that caused
  1135. * the interrupt, scan all events and reset any that are
  1136. * negative, to avoid getting continual interrupts.
  1137. * Any that we processed in the previous loop will not be negative.
  1138. */
  1139. if (!found) {
  1140. for (i = 0; i < ppmu->n_counter; ++i) {
  1141. if (is_limited_pmc(i + 1))
  1142. continue;
  1143. val = read_pmc(i + 1);
  1144. if ((int)val < 0)
  1145. write_pmc(i + 1, 0);
  1146. }
  1147. }
  1148. /*
  1149. * Reset MMCR0 to its normal value. This will set PMXE and
  1150. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1151. * and thus allow interrupts to occur again.
  1152. * XXX might want to use MSR.PM to keep the events frozen until
  1153. * we get back out of this interrupt.
  1154. */
  1155. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1156. if (nmi)
  1157. nmi_exit();
  1158. else
  1159. irq_exit();
  1160. }
  1161. static void power_pmu_setup(int cpu)
  1162. {
  1163. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1164. if (!ppmu)
  1165. return;
  1166. memset(cpuhw, 0, sizeof(*cpuhw));
  1167. cpuhw->mmcr[0] = MMCR0_FC;
  1168. }
  1169. static int __cpuinit
  1170. power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1171. {
  1172. unsigned int cpu = (long)hcpu;
  1173. switch (action & ~CPU_TASKS_FROZEN) {
  1174. case CPU_UP_PREPARE:
  1175. power_pmu_setup(cpu);
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. return NOTIFY_OK;
  1181. }
  1182. int register_power_pmu(struct power_pmu *pmu)
  1183. {
  1184. if (ppmu)
  1185. return -EBUSY; /* something's already registered */
  1186. ppmu = pmu;
  1187. pr_info("%s performance monitor hardware support registered\n",
  1188. pmu->name);
  1189. #ifdef MSR_HV
  1190. /*
  1191. * Use FCHV to ignore kernel events if MSR.HV is set.
  1192. */
  1193. if (mfmsr() & MSR_HV)
  1194. freeze_events_kernel = MMCR0_FCHV;
  1195. #endif /* CONFIG_PPC64 */
  1196. perf_cpu_notifier(power_pmu_notifier);
  1197. return 0;
  1198. }