perf_counter.c 27 KB

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