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. u64 hw_perf_save_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. return ret;
  395. }
  396. /*
  397. * Re-enable all counters if disable == 0.
  398. * If we were previously disabled and counters were added, then
  399. * put the new config on the PMU.
  400. */
  401. void hw_perf_restore(u64 disable)
  402. {
  403. struct perf_counter *counter;
  404. struct cpu_hw_counters *cpuhw;
  405. unsigned long flags;
  406. long i;
  407. unsigned long val;
  408. s64 left;
  409. unsigned int hwc_index[MAX_HWCOUNTERS];
  410. int n_lim;
  411. int idx;
  412. if (disable)
  413. return;
  414. local_irq_save(flags);
  415. cpuhw = &__get_cpu_var(cpu_hw_counters);
  416. cpuhw->disabled = 0;
  417. /*
  418. * If we didn't change anything, or only removed counters,
  419. * no need to recalculate MMCR* settings and reset the PMCs.
  420. * Just reenable the PMU with the current MMCR* settings
  421. * (possibly updated for removal of counters).
  422. */
  423. if (!cpuhw->n_added) {
  424. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  425. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  426. if (cpuhw->n_counters == 0)
  427. get_lppaca()->pmcregs_in_use = 0;
  428. goto out_enable;
  429. }
  430. /*
  431. * Compute MMCR* values for the new set of counters
  432. */
  433. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
  434. cpuhw->mmcr)) {
  435. /* shouldn't ever get here */
  436. printk(KERN_ERR "oops compute_mmcr failed\n");
  437. goto out;
  438. }
  439. /*
  440. * Add in MMCR0 freeze bits corresponding to the
  441. * hw_event.exclude_* bits for the first counter.
  442. * We have already checked that all counters have the
  443. * same values for these bits as the first counter.
  444. */
  445. counter = cpuhw->counter[0];
  446. if (counter->hw_event.exclude_user)
  447. cpuhw->mmcr[0] |= MMCR0_FCP;
  448. if (counter->hw_event.exclude_kernel)
  449. cpuhw->mmcr[0] |= freeze_counters_kernel;
  450. if (counter->hw_event.exclude_hv)
  451. cpuhw->mmcr[0] |= MMCR0_FCHV;
  452. /*
  453. * Write the new configuration to MMCR* with the freeze
  454. * bit set and set the hardware counters to their initial values.
  455. * Then unfreeze the counters.
  456. */
  457. get_lppaca()->pmcregs_in_use = 1;
  458. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  459. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  460. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  461. | MMCR0_FC);
  462. /*
  463. * Read off any pre-existing counters that need to move
  464. * to another PMC.
  465. */
  466. for (i = 0; i < cpuhw->n_counters; ++i) {
  467. counter = cpuhw->counter[i];
  468. if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
  469. power_pmu_read(counter);
  470. write_pmc(counter->hw.idx, 0);
  471. counter->hw.idx = 0;
  472. }
  473. }
  474. /*
  475. * Initialize the PMCs for all the new and moved counters.
  476. */
  477. cpuhw->n_limited = n_lim = 0;
  478. for (i = 0; i < cpuhw->n_counters; ++i) {
  479. counter = cpuhw->counter[i];
  480. if (counter->hw.idx)
  481. continue;
  482. idx = hwc_index[i] + 1;
  483. if (is_limited_pmc(idx)) {
  484. cpuhw->limited_counter[n_lim] = counter;
  485. cpuhw->limited_hwidx[n_lim] = idx;
  486. ++n_lim;
  487. continue;
  488. }
  489. val = 0;
  490. if (counter->hw_event.irq_period) {
  491. left = atomic64_read(&counter->hw.period_left);
  492. if (left < 0x80000000L)
  493. val = 0x80000000L - left;
  494. }
  495. atomic64_set(&counter->hw.prev_count, val);
  496. counter->hw.idx = idx;
  497. write_pmc(idx, val);
  498. perf_counter_update_userpage(counter);
  499. }
  500. cpuhw->n_limited = n_lim;
  501. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  502. out_enable:
  503. mb();
  504. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  505. /*
  506. * Enable instruction sampling if necessary
  507. */
  508. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  509. mb();
  510. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  511. }
  512. out:
  513. local_irq_restore(flags);
  514. }
  515. static int collect_events(struct perf_counter *group, int max_count,
  516. struct perf_counter *ctrs[], unsigned int *events,
  517. unsigned int *flags)
  518. {
  519. int n = 0;
  520. struct perf_counter *counter;
  521. if (!is_software_counter(group)) {
  522. if (n >= max_count)
  523. return -1;
  524. ctrs[n] = group;
  525. flags[n] = group->hw.counter_base;
  526. events[n++] = group->hw.config;
  527. }
  528. list_for_each_entry(counter, &group->sibling_list, list_entry) {
  529. if (!is_software_counter(counter) &&
  530. counter->state != PERF_COUNTER_STATE_OFF) {
  531. if (n >= max_count)
  532. return -1;
  533. ctrs[n] = counter;
  534. flags[n] = counter->hw.counter_base;
  535. events[n++] = counter->hw.config;
  536. }
  537. }
  538. return n;
  539. }
  540. static void counter_sched_in(struct perf_counter *counter, int cpu)
  541. {
  542. counter->state = PERF_COUNTER_STATE_ACTIVE;
  543. counter->oncpu = cpu;
  544. counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped;
  545. if (is_software_counter(counter))
  546. counter->pmu->enable(counter);
  547. }
  548. /*
  549. * Called to enable a whole group of counters.
  550. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  551. * Assumes the caller has disabled interrupts and has
  552. * frozen the PMU with hw_perf_save_disable.
  553. */
  554. int hw_perf_group_sched_in(struct perf_counter *group_leader,
  555. struct perf_cpu_context *cpuctx,
  556. struct perf_counter_context *ctx, int cpu)
  557. {
  558. struct cpu_hw_counters *cpuhw;
  559. long i, n, n0;
  560. struct perf_counter *sub;
  561. cpuhw = &__get_cpu_var(cpu_hw_counters);
  562. n0 = cpuhw->n_counters;
  563. n = collect_events(group_leader, ppmu->n_counter - n0,
  564. &cpuhw->counter[n0], &cpuhw->events[n0],
  565. &cpuhw->flags[n0]);
  566. if (n < 0)
  567. return -EAGAIN;
  568. if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n))
  569. return -EAGAIN;
  570. i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0);
  571. if (i < 0)
  572. return -EAGAIN;
  573. cpuhw->n_counters = n0 + n;
  574. cpuhw->n_added += n;
  575. /*
  576. * OK, this group can go on; update counter states etc.,
  577. * and enable any software counters
  578. */
  579. for (i = n0; i < n0 + n; ++i)
  580. cpuhw->counter[i]->hw.config = cpuhw->events[i];
  581. cpuctx->active_oncpu += n;
  582. n = 1;
  583. counter_sched_in(group_leader, cpu);
  584. list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
  585. if (sub->state != PERF_COUNTER_STATE_OFF) {
  586. counter_sched_in(sub, cpu);
  587. ++n;
  588. }
  589. }
  590. ctx->nr_active += n;
  591. return 1;
  592. }
  593. /*
  594. * Add a counter to the PMU.
  595. * If all counters are not already frozen, then we disable and
  596. * re-enable the PMU in order to get hw_perf_restore to do the
  597. * actual work of reconfiguring the PMU.
  598. */
  599. static int power_pmu_enable(struct perf_counter *counter)
  600. {
  601. struct cpu_hw_counters *cpuhw;
  602. unsigned long flags;
  603. u64 pmudis;
  604. int n0;
  605. int ret = -EAGAIN;
  606. local_irq_save(flags);
  607. pmudis = hw_perf_save_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. hw_perf_restore(pmudis);
  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. u64 pmudis;
  640. unsigned long flags;
  641. local_irq_save(flags);
  642. pmudis = hw_perf_save_disable();
  643. power_pmu_read(counter);
  644. cpuhw = &__get_cpu_var(cpu_hw_counters);
  645. for (i = 0; i < cpuhw->n_counters; ++i) {
  646. if (counter == cpuhw->counter[i]) {
  647. while (++i < cpuhw->n_counters)
  648. cpuhw->counter[i-1] = cpuhw->counter[i];
  649. --cpuhw->n_counters;
  650. ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
  651. if (counter->hw.idx) {
  652. write_pmc(counter->hw.idx, 0);
  653. counter->hw.idx = 0;
  654. }
  655. perf_counter_update_userpage(counter);
  656. break;
  657. }
  658. }
  659. for (i = 0; i < cpuhw->n_limited; ++i)
  660. if (counter == cpuhw->limited_counter[i])
  661. break;
  662. if (i < cpuhw->n_limited) {
  663. while (++i < cpuhw->n_limited) {
  664. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  665. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  666. }
  667. --cpuhw->n_limited;
  668. }
  669. if (cpuhw->n_counters == 0) {
  670. /* disable exceptions if no counters are running */
  671. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  672. }
  673. hw_perf_restore(pmudis);
  674. local_irq_restore(flags);
  675. }
  676. struct pmu power_pmu = {
  677. .enable = power_pmu_enable,
  678. .disable = power_pmu_disable,
  679. .read = power_pmu_read,
  680. };
  681. /*
  682. * Return 1 if we might be able to put counter on a limited PMC,
  683. * or 0 if not.
  684. * A counter can only go on a limited PMC if it counts something
  685. * that a limited PMC can count, doesn't require interrupts, and
  686. * doesn't exclude any processor mode.
  687. */
  688. static int can_go_on_limited_pmc(struct perf_counter *counter, unsigned int ev,
  689. unsigned int flags)
  690. {
  691. int n;
  692. unsigned int alt[MAX_EVENT_ALTERNATIVES];
  693. if (counter->hw_event.exclude_user
  694. || counter->hw_event.exclude_kernel
  695. || counter->hw_event.exclude_hv
  696. || counter->hw_event.irq_period)
  697. return 0;
  698. if (ppmu->limited_pmc_event(ev))
  699. return 1;
  700. /*
  701. * The requested event isn't on a limited PMC already;
  702. * see if any alternative code goes on a limited PMC.
  703. */
  704. if (!ppmu->get_alternatives)
  705. return 0;
  706. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  707. n = ppmu->get_alternatives(ev, flags, alt);
  708. if (n)
  709. return alt[0];
  710. return 0;
  711. }
  712. /*
  713. * Find an alternative event that goes on a normal PMC, if possible,
  714. * and return the event code, or 0 if there is no such alternative.
  715. * (Note: event code 0 is "don't count" on all machines.)
  716. */
  717. static unsigned long normal_pmc_alternative(unsigned long ev,
  718. unsigned long flags)
  719. {
  720. unsigned int alt[MAX_EVENT_ALTERNATIVES];
  721. int n;
  722. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  723. n = ppmu->get_alternatives(ev, flags, alt);
  724. if (!n)
  725. return 0;
  726. return alt[0];
  727. }
  728. /* Number of perf_counters counting hardware events */
  729. static atomic_t num_counters;
  730. /* Used to avoid races in calling reserve/release_pmc_hardware */
  731. static DEFINE_MUTEX(pmc_reserve_mutex);
  732. /*
  733. * Release the PMU if this is the last perf_counter.
  734. */
  735. static void hw_perf_counter_destroy(struct perf_counter *counter)
  736. {
  737. if (!atomic_add_unless(&num_counters, -1, 1)) {
  738. mutex_lock(&pmc_reserve_mutex);
  739. if (atomic_dec_return(&num_counters) == 0)
  740. release_pmc_hardware();
  741. mutex_unlock(&pmc_reserve_mutex);
  742. }
  743. }
  744. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  745. {
  746. unsigned long ev, flags;
  747. struct perf_counter *ctrs[MAX_HWCOUNTERS];
  748. unsigned int events[MAX_HWCOUNTERS];
  749. unsigned int cflags[MAX_HWCOUNTERS];
  750. int n;
  751. int err;
  752. if (!ppmu)
  753. return ERR_PTR(-ENXIO);
  754. if ((s64)counter->hw_event.irq_period < 0)
  755. return ERR_PTR(-EINVAL);
  756. if (!perf_event_raw(&counter->hw_event)) {
  757. ev = perf_event_id(&counter->hw_event);
  758. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  759. return ERR_PTR(-EOPNOTSUPP);
  760. ev = ppmu->generic_events[ev];
  761. } else {
  762. ev = perf_event_config(&counter->hw_event);
  763. }
  764. counter->hw.config_base = ev;
  765. counter->hw.idx = 0;
  766. /*
  767. * If we are not running on a hypervisor, force the
  768. * exclude_hv bit to 0 so that we don't care what
  769. * the user set it to.
  770. */
  771. if (!firmware_has_feature(FW_FEATURE_LPAR))
  772. counter->hw_event.exclude_hv = 0;
  773. /*
  774. * If this is a per-task counter, then we can use
  775. * PM_RUN_* events interchangeably with their non RUN_*
  776. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  777. * XXX we should check if the task is an idle task.
  778. */
  779. flags = 0;
  780. if (counter->ctx->task)
  781. flags |= PPMU_ONLY_COUNT_RUN;
  782. /*
  783. * If this machine has limited counters, check whether this
  784. * event could go on a limited counter.
  785. */
  786. if (ppmu->limited_pmc5_6) {
  787. if (can_go_on_limited_pmc(counter, ev, flags)) {
  788. flags |= PPMU_LIMITED_PMC_OK;
  789. } else if (ppmu->limited_pmc_event(ev)) {
  790. /*
  791. * The requested event is on a limited PMC,
  792. * but we can't use a limited PMC; see if any
  793. * alternative goes on a normal PMC.
  794. */
  795. ev = normal_pmc_alternative(ev, flags);
  796. if (!ev)
  797. return ERR_PTR(-EINVAL);
  798. }
  799. }
  800. /*
  801. * If this is in a group, check if it can go on with all the
  802. * other hardware counters in the group. We assume the counter
  803. * hasn't been linked into its leader's sibling list at this point.
  804. */
  805. n = 0;
  806. if (counter->group_leader != counter) {
  807. n = collect_events(counter->group_leader, ppmu->n_counter - 1,
  808. ctrs, events, cflags);
  809. if (n < 0)
  810. return ERR_PTR(-EINVAL);
  811. }
  812. events[n] = ev;
  813. ctrs[n] = counter;
  814. cflags[n] = flags;
  815. if (check_excludes(ctrs, cflags, n, 1))
  816. return ERR_PTR(-EINVAL);
  817. if (power_check_constraints(events, cflags, n + 1))
  818. return ERR_PTR(-EINVAL);
  819. counter->hw.config = events[n];
  820. counter->hw.counter_base = cflags[n];
  821. atomic64_set(&counter->hw.period_left, counter->hw_event.irq_period);
  822. /*
  823. * See if we need to reserve the PMU.
  824. * If no counters are currently in use, then we have to take a
  825. * mutex to ensure that we don't race with another task doing
  826. * reserve_pmc_hardware or release_pmc_hardware.
  827. */
  828. err = 0;
  829. if (!atomic_inc_not_zero(&num_counters)) {
  830. mutex_lock(&pmc_reserve_mutex);
  831. if (atomic_read(&num_counters) == 0 &&
  832. reserve_pmc_hardware(perf_counter_interrupt))
  833. err = -EBUSY;
  834. else
  835. atomic_inc(&num_counters);
  836. mutex_unlock(&pmc_reserve_mutex);
  837. }
  838. counter->destroy = hw_perf_counter_destroy;
  839. if (err)
  840. return ERR_PTR(err);
  841. return &power_pmu;
  842. }
  843. /*
  844. * A counter has overflowed; update its count and record
  845. * things if requested. Note that interrupts are hard-disabled
  846. * here so there is no possibility of being interrupted.
  847. */
  848. static void record_and_restart(struct perf_counter *counter, long val,
  849. struct pt_regs *regs, int nmi)
  850. {
  851. s64 prev, delta, left;
  852. int record = 0;
  853. /* we don't have to worry about interrupts here */
  854. prev = atomic64_read(&counter->hw.prev_count);
  855. delta = (val - prev) & 0xfffffffful;
  856. atomic64_add(delta, &counter->count);
  857. /*
  858. * See if the total period for this counter has expired,
  859. * and update for the next period.
  860. */
  861. val = 0;
  862. left = atomic64_read(&counter->hw.period_left) - delta;
  863. if (counter->hw_event.irq_period) {
  864. if (left <= 0) {
  865. left += counter->hw_event.irq_period;
  866. if (left <= 0)
  867. left = counter->hw_event.irq_period;
  868. record = 1;
  869. }
  870. if (left < 0x80000000L)
  871. val = 0x80000000L - left;
  872. }
  873. write_pmc(counter->hw.idx, val);
  874. atomic64_set(&counter->hw.prev_count, val);
  875. atomic64_set(&counter->hw.period_left, left);
  876. perf_counter_update_userpage(counter);
  877. /*
  878. * Finally record data if requested.
  879. */
  880. if (record)
  881. perf_counter_overflow(counter, nmi, regs, 0);
  882. }
  883. /*
  884. * Performance monitor interrupt stuff
  885. */
  886. static void perf_counter_interrupt(struct pt_regs *regs)
  887. {
  888. int i;
  889. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  890. struct perf_counter *counter;
  891. long val;
  892. int found = 0;
  893. int nmi;
  894. if (cpuhw->n_limited)
  895. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  896. mfspr(SPRN_PMC6));
  897. /*
  898. * If interrupts were soft-disabled when this PMU interrupt
  899. * occurred, treat it as an NMI.
  900. */
  901. nmi = !regs->softe;
  902. if (nmi)
  903. nmi_enter();
  904. else
  905. irq_enter();
  906. for (i = 0; i < cpuhw->n_counters; ++i) {
  907. counter = cpuhw->counter[i];
  908. if (is_limited_pmc(counter->hw.idx))
  909. continue;
  910. val = read_pmc(counter->hw.idx);
  911. if ((int)val < 0) {
  912. /* counter has overflowed */
  913. found = 1;
  914. record_and_restart(counter, val, regs, nmi);
  915. }
  916. }
  917. /*
  918. * In case we didn't find and reset the counter that caused
  919. * the interrupt, scan all counters and reset any that are
  920. * negative, to avoid getting continual interrupts.
  921. * Any that we processed in the previous loop will not be negative.
  922. */
  923. if (!found) {
  924. for (i = 0; i < ppmu->n_counter; ++i) {
  925. if (is_limited_pmc(i + 1))
  926. continue;
  927. val = read_pmc(i + 1);
  928. if ((int)val < 0)
  929. write_pmc(i + 1, 0);
  930. }
  931. }
  932. /*
  933. * Reset MMCR0 to its normal value. This will set PMXE and
  934. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  935. * and thus allow interrupts to occur again.
  936. * XXX might want to use MSR.PM to keep the counters frozen until
  937. * we get back out of this interrupt.
  938. */
  939. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  940. if (nmi)
  941. nmi_exit();
  942. else
  943. irq_exit();
  944. }
  945. void hw_perf_counter_setup(int cpu)
  946. {
  947. struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
  948. memset(cpuhw, 0, sizeof(*cpuhw));
  949. cpuhw->mmcr[0] = MMCR0_FC;
  950. }
  951. extern struct power_pmu power4_pmu;
  952. extern struct power_pmu ppc970_pmu;
  953. extern struct power_pmu power5_pmu;
  954. extern struct power_pmu power5p_pmu;
  955. extern struct power_pmu power6_pmu;
  956. static int init_perf_counters(void)
  957. {
  958. unsigned long pvr;
  959. /* XXX should get this from cputable */
  960. pvr = mfspr(SPRN_PVR);
  961. switch (PVR_VER(pvr)) {
  962. case PV_POWER4:
  963. case PV_POWER4p:
  964. ppmu = &power4_pmu;
  965. break;
  966. case PV_970:
  967. case PV_970FX:
  968. case PV_970MP:
  969. ppmu = &ppc970_pmu;
  970. break;
  971. case PV_POWER5:
  972. ppmu = &power5_pmu;
  973. break;
  974. case PV_POWER5p:
  975. ppmu = &power5p_pmu;
  976. break;
  977. case 0x3e:
  978. ppmu = &power6_pmu;
  979. break;
  980. }
  981. /*
  982. * Use FCHV to ignore kernel events if MSR.HV is set.
  983. */
  984. if (mfmsr() & MSR_HV)
  985. freeze_counters_kernel = MMCR0_FCHV;
  986. return 0;
  987. }
  988. arch_initcall(init_perf_counters);