perf_counter.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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_event.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 ((s64)counter->hw_event.irq_period < 0)
  754. return ERR_PTR(-EINVAL);
  755. if (!perf_event_raw(&counter->hw_event)) {
  756. ev = perf_event_id(&counter->hw_event);
  757. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  758. return ERR_PTR(-EOPNOTSUPP);
  759. ev = ppmu->generic_events[ev];
  760. } else {
  761. ev = perf_event_config(&counter->hw_event);
  762. }
  763. counter->hw.config_base = ev;
  764. counter->hw.idx = 0;
  765. /*
  766. * If we are not running on a hypervisor, force the
  767. * exclude_hv bit to 0 so that we don't care what
  768. * the user set it to.
  769. */
  770. if (!firmware_has_feature(FW_FEATURE_LPAR))
  771. counter->hw_event.exclude_hv = 0;
  772. /*
  773. * If this is a per-task counter, then we can use
  774. * PM_RUN_* events interchangeably with their non RUN_*
  775. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  776. * XXX we should check if the task is an idle task.
  777. */
  778. flags = 0;
  779. if (counter->ctx->task)
  780. flags |= PPMU_ONLY_COUNT_RUN;
  781. /*
  782. * If this machine has limited counters, check whether this
  783. * event could go on a limited counter.
  784. */
  785. if (ppmu->limited_pmc5_6) {
  786. if (can_go_on_limited_pmc(counter, ev, flags)) {
  787. flags |= PPMU_LIMITED_PMC_OK;
  788. } else if (ppmu->limited_pmc_event(ev)) {
  789. /*
  790. * The requested event is on a limited PMC,
  791. * but we can't use a limited PMC; see if any
  792. * alternative goes on a normal PMC.
  793. */
  794. ev = normal_pmc_alternative(ev, flags);
  795. if (!ev)
  796. return ERR_PTR(-EINVAL);
  797. }
  798. }
  799. /*
  800. * If this is in a group, check if it can go on with all the
  801. * other hardware counters in the group. We assume the counter
  802. * hasn't been linked into its leader's sibling list at this point.
  803. */
  804. n = 0;
  805. if (counter->group_leader != counter) {
  806. n = collect_events(counter->group_leader, ppmu->n_counter - 1,
  807. ctrs, events, cflags);
  808. if (n < 0)
  809. return ERR_PTR(-EINVAL);
  810. }
  811. events[n] = ev;
  812. ctrs[n] = counter;
  813. cflags[n] = flags;
  814. if (check_excludes(ctrs, cflags, n, 1))
  815. return ERR_PTR(-EINVAL);
  816. if (power_check_constraints(events, cflags, n + 1))
  817. return ERR_PTR(-EINVAL);
  818. counter->hw.config = events[n];
  819. counter->hw.counter_base = cflags[n];
  820. atomic64_set(&counter->hw.period_left, counter->hw_event.irq_period);
  821. /*
  822. * See if we need to reserve the PMU.
  823. * If no counters are currently in use, then we have to take a
  824. * mutex to ensure that we don't race with another task doing
  825. * reserve_pmc_hardware or release_pmc_hardware.
  826. */
  827. err = 0;
  828. if (!atomic_inc_not_zero(&num_counters)) {
  829. mutex_lock(&pmc_reserve_mutex);
  830. if (atomic_read(&num_counters) == 0 &&
  831. reserve_pmc_hardware(perf_counter_interrupt))
  832. err = -EBUSY;
  833. else
  834. atomic_inc(&num_counters);
  835. mutex_unlock(&pmc_reserve_mutex);
  836. }
  837. counter->destroy = hw_perf_counter_destroy;
  838. if (err)
  839. return ERR_PTR(err);
  840. return &power_pmu;
  841. }
  842. /*
  843. * A counter has overflowed; update its count and record
  844. * things if requested. Note that interrupts are hard-disabled
  845. * here so there is no possibility of being interrupted.
  846. */
  847. static void record_and_restart(struct perf_counter *counter, long val,
  848. struct pt_regs *regs, int nmi)
  849. {
  850. s64 prev, delta, left;
  851. int record = 0;
  852. /* we don't have to worry about interrupts here */
  853. prev = atomic64_read(&counter->hw.prev_count);
  854. delta = (val - prev) & 0xfffffffful;
  855. atomic64_add(delta, &counter->count);
  856. /*
  857. * See if the total period for this counter has expired,
  858. * and update for the next period.
  859. */
  860. val = 0;
  861. left = atomic64_read(&counter->hw.period_left) - delta;
  862. if (counter->hw_event.irq_period) {
  863. if (left <= 0) {
  864. left += counter->hw_event.irq_period;
  865. if (left <= 0)
  866. left = counter->hw_event.irq_period;
  867. record = 1;
  868. }
  869. if (left < 0x80000000L)
  870. val = 0x80000000L - left;
  871. }
  872. write_pmc(counter->hw.idx, val);
  873. atomic64_set(&counter->hw.prev_count, val);
  874. atomic64_set(&counter->hw.period_left, left);
  875. perf_counter_update_userpage(counter);
  876. /*
  877. * Finally record data if requested.
  878. */
  879. if (record)
  880. perf_counter_overflow(counter, nmi, regs, 0);
  881. }
  882. /*
  883. * Performance monitor interrupt stuff
  884. */
  885. static void perf_counter_interrupt(struct pt_regs *regs)
  886. {
  887. int i;
  888. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  889. struct perf_counter *counter;
  890. long val;
  891. int found = 0;
  892. int nmi;
  893. if (cpuhw->n_limited)
  894. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  895. mfspr(SPRN_PMC6));
  896. /*
  897. * If interrupts were soft-disabled when this PMU interrupt
  898. * occurred, treat it as an NMI.
  899. */
  900. nmi = !regs->softe;
  901. if (nmi)
  902. nmi_enter();
  903. else
  904. irq_enter();
  905. for (i = 0; i < cpuhw->n_counters; ++i) {
  906. counter = cpuhw->counter[i];
  907. if (is_limited_pmc(counter->hw.idx))
  908. continue;
  909. val = read_pmc(counter->hw.idx);
  910. if ((int)val < 0) {
  911. /* counter has overflowed */
  912. found = 1;
  913. record_and_restart(counter, val, regs, nmi);
  914. }
  915. }
  916. /*
  917. * In case we didn't find and reset the counter that caused
  918. * the interrupt, scan all counters and reset any that are
  919. * negative, to avoid getting continual interrupts.
  920. * Any that we processed in the previous loop will not be negative.
  921. */
  922. if (!found) {
  923. for (i = 0; i < ppmu->n_counter; ++i) {
  924. if (is_limited_pmc(i + 1))
  925. continue;
  926. val = read_pmc(i + 1);
  927. if ((int)val < 0)
  928. write_pmc(i + 1, 0);
  929. }
  930. }
  931. /*
  932. * Reset MMCR0 to its normal value. This will set PMXE and
  933. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  934. * and thus allow interrupts to occur again.
  935. * XXX might want to use MSR.PM to keep the counters frozen until
  936. * we get back out of this interrupt.
  937. */
  938. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  939. if (nmi)
  940. nmi_exit();
  941. else
  942. irq_exit();
  943. }
  944. void hw_perf_counter_setup(int cpu)
  945. {
  946. struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
  947. memset(cpuhw, 0, sizeof(*cpuhw));
  948. cpuhw->mmcr[0] = MMCR0_FC;
  949. }
  950. extern struct power_pmu power4_pmu;
  951. extern struct power_pmu ppc970_pmu;
  952. extern struct power_pmu power5_pmu;
  953. extern struct power_pmu power5p_pmu;
  954. extern struct power_pmu power6_pmu;
  955. static int init_perf_counters(void)
  956. {
  957. unsigned long pvr;
  958. /* XXX should get this from cputable */
  959. pvr = mfspr(SPRN_PVR);
  960. switch (PVR_VER(pvr)) {
  961. case PV_POWER4:
  962. case PV_POWER4p:
  963. ppmu = &power4_pmu;
  964. break;
  965. case PV_970:
  966. case PV_970FX:
  967. case PV_970MP:
  968. ppmu = &ppc970_pmu;
  969. break;
  970. case PV_POWER5:
  971. ppmu = &power5_pmu;
  972. break;
  973. case PV_POWER5p:
  974. ppmu = &power5p_pmu;
  975. break;
  976. case 0x3e:
  977. ppmu = &power6_pmu;
  978. break;
  979. }
  980. /*
  981. * Use FCHV to ignore kernel events if MSR.HV is set.
  982. */
  983. if (mfmsr() & MSR_HV)
  984. freeze_counters_kernel = MMCR0_FCHV;
  985. return 0;
  986. }
  987. arch_initcall(init_perf_counters);