perf_event.c 32 KB

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