perf_counter.c 32 KB

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