perf_counter.c 29 KB

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