perf_counter.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. struct perf_counter *counter[MAX_HWCOUNTERS];
  26. unsigned int events[MAX_HWCOUNTERS];
  27. u64 mmcr[3];
  28. u8 pmcs_enabled;
  29. };
  30. DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters);
  31. struct power_pmu *ppmu;
  32. /*
  33. * Normally, to ignore kernel events we set the FCS (freeze counters
  34. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  35. * hypervisor bit set in the MSR, or if we are running on a processor
  36. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  37. * then we need to use the FCHV bit to ignore kernel events.
  38. */
  39. static unsigned int freeze_counters_kernel = MMCR0_FCS;
  40. void perf_counter_print_debug(void)
  41. {
  42. }
  43. /*
  44. * Read one performance monitor counter (PMC).
  45. */
  46. static unsigned long read_pmc(int idx)
  47. {
  48. unsigned long val;
  49. switch (idx) {
  50. case 1:
  51. val = mfspr(SPRN_PMC1);
  52. break;
  53. case 2:
  54. val = mfspr(SPRN_PMC2);
  55. break;
  56. case 3:
  57. val = mfspr(SPRN_PMC3);
  58. break;
  59. case 4:
  60. val = mfspr(SPRN_PMC4);
  61. break;
  62. case 5:
  63. val = mfspr(SPRN_PMC5);
  64. break;
  65. case 6:
  66. val = mfspr(SPRN_PMC6);
  67. break;
  68. case 7:
  69. val = mfspr(SPRN_PMC7);
  70. break;
  71. case 8:
  72. val = mfspr(SPRN_PMC8);
  73. break;
  74. default:
  75. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  76. val = 0;
  77. }
  78. return val;
  79. }
  80. /*
  81. * Write one PMC.
  82. */
  83. static void write_pmc(int idx, unsigned long val)
  84. {
  85. switch (idx) {
  86. case 1:
  87. mtspr(SPRN_PMC1, val);
  88. break;
  89. case 2:
  90. mtspr(SPRN_PMC2, val);
  91. break;
  92. case 3:
  93. mtspr(SPRN_PMC3, val);
  94. break;
  95. case 4:
  96. mtspr(SPRN_PMC4, val);
  97. break;
  98. case 5:
  99. mtspr(SPRN_PMC5, val);
  100. break;
  101. case 6:
  102. mtspr(SPRN_PMC6, val);
  103. break;
  104. case 7:
  105. mtspr(SPRN_PMC7, val);
  106. break;
  107. case 8:
  108. mtspr(SPRN_PMC8, val);
  109. break;
  110. default:
  111. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  112. }
  113. }
  114. /*
  115. * Check if a set of events can all go on the PMU at once.
  116. * If they can't, this will look at alternative codes for the events
  117. * and see if any combination of alternative codes is feasible.
  118. * The feasible set is returned in event[].
  119. */
  120. static int power_check_constraints(unsigned int event[], int n_ev)
  121. {
  122. u64 mask, value, nv;
  123. unsigned int alternatives[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  124. u64 amasks[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  125. u64 avalues[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES];
  126. u64 smasks[MAX_HWCOUNTERS], svalues[MAX_HWCOUNTERS];
  127. int n_alt[MAX_HWCOUNTERS], choice[MAX_HWCOUNTERS];
  128. int i, j;
  129. u64 addf = ppmu->add_fields;
  130. u64 tadd = ppmu->test_adder;
  131. if (n_ev > ppmu->n_counter)
  132. return -1;
  133. /* First see if the events will go on as-is */
  134. for (i = 0; i < n_ev; ++i) {
  135. alternatives[i][0] = event[i];
  136. if (ppmu->get_constraint(event[i], &amasks[i][0],
  137. &avalues[i][0]))
  138. return -1;
  139. choice[i] = 0;
  140. }
  141. value = mask = 0;
  142. for (i = 0; i < n_ev; ++i) {
  143. nv = (value | avalues[i][0]) + (value & avalues[i][0] & addf);
  144. if ((((nv + tadd) ^ value) & mask) != 0 ||
  145. (((nv + tadd) ^ avalues[i][0]) & amasks[i][0]) != 0)
  146. break;
  147. value = nv;
  148. mask |= amasks[i][0];
  149. }
  150. if (i == n_ev)
  151. return 0; /* all OK */
  152. /* doesn't work, gather alternatives... */
  153. if (!ppmu->get_alternatives)
  154. return -1;
  155. for (i = 0; i < n_ev; ++i) {
  156. n_alt[i] = ppmu->get_alternatives(event[i], alternatives[i]);
  157. for (j = 1; j < n_alt[i]; ++j)
  158. ppmu->get_constraint(alternatives[i][j],
  159. &amasks[i][j], &avalues[i][j]);
  160. }
  161. /* enumerate all possibilities and see if any will work */
  162. i = 0;
  163. j = -1;
  164. value = mask = nv = 0;
  165. while (i < n_ev) {
  166. if (j >= 0) {
  167. /* we're backtracking, restore context */
  168. value = svalues[i];
  169. mask = smasks[i];
  170. j = choice[i];
  171. }
  172. /*
  173. * See if any alternative k for event i,
  174. * where k > j, will satisfy the constraints.
  175. */
  176. while (++j < n_alt[i]) {
  177. nv = (value | avalues[i][j]) +
  178. (value & avalues[i][j] & addf);
  179. if ((((nv + tadd) ^ value) & mask) == 0 &&
  180. (((nv + tadd) ^ avalues[i][j])
  181. & amasks[i][j]) == 0)
  182. break;
  183. }
  184. if (j >= n_alt[i]) {
  185. /*
  186. * No feasible alternative, backtrack
  187. * to event i-1 and continue enumerating its
  188. * alternatives from where we got up to.
  189. */
  190. if (--i < 0)
  191. return -1;
  192. } else {
  193. /*
  194. * Found a feasible alternative for event i,
  195. * remember where we got up to with this event,
  196. * go on to the next event, and start with
  197. * the first alternative for it.
  198. */
  199. choice[i] = j;
  200. svalues[i] = value;
  201. smasks[i] = mask;
  202. value = nv;
  203. mask |= amasks[i][j];
  204. ++i;
  205. j = -1;
  206. }
  207. }
  208. /* OK, we have a feasible combination, tell the caller the solution */
  209. for (i = 0; i < n_ev; ++i)
  210. event[i] = alternatives[i][choice[i]];
  211. return 0;
  212. }
  213. /*
  214. * Check if newly-added counters have consistent settings for
  215. * exclude_{user,kernel,hv} with each other and any previously
  216. * added counters.
  217. */
  218. static int check_excludes(struct perf_counter **ctrs, int n_prev, int n_new)
  219. {
  220. int eu, ek, eh;
  221. int i, n;
  222. struct perf_counter *counter;
  223. n = n_prev + n_new;
  224. if (n <= 1)
  225. return 0;
  226. eu = ctrs[0]->hw_event.exclude_user;
  227. ek = ctrs[0]->hw_event.exclude_kernel;
  228. eh = ctrs[0]->hw_event.exclude_hv;
  229. if (n_prev == 0)
  230. n_prev = 1;
  231. for (i = n_prev; i < n; ++i) {
  232. counter = ctrs[i];
  233. if (counter->hw_event.exclude_user != eu ||
  234. counter->hw_event.exclude_kernel != ek ||
  235. counter->hw_event.exclude_hv != eh)
  236. return -EAGAIN;
  237. }
  238. return 0;
  239. }
  240. static void power_perf_read(struct perf_counter *counter)
  241. {
  242. long val, delta, prev;
  243. if (!counter->hw.idx)
  244. return;
  245. /*
  246. * Performance monitor interrupts come even when interrupts
  247. * are soft-disabled, as long as interrupts are hard-enabled.
  248. * Therefore we treat them like NMIs.
  249. */
  250. do {
  251. prev = atomic64_read(&counter->hw.prev_count);
  252. barrier();
  253. val = read_pmc(counter->hw.idx);
  254. } while (atomic64_cmpxchg(&counter->hw.prev_count, prev, val) != prev);
  255. /* The counters are only 32 bits wide */
  256. delta = (val - prev) & 0xfffffffful;
  257. atomic64_add(delta, &counter->count);
  258. atomic64_sub(delta, &counter->hw.period_left);
  259. }
  260. /*
  261. * Disable all counters to prevent PMU interrupts and to allow
  262. * counters to be added or removed.
  263. */
  264. u64 hw_perf_save_disable(void)
  265. {
  266. struct cpu_hw_counters *cpuhw;
  267. unsigned long ret;
  268. unsigned long flags;
  269. local_irq_save(flags);
  270. cpuhw = &__get_cpu_var(cpu_hw_counters);
  271. ret = cpuhw->disabled;
  272. if (!ret) {
  273. cpuhw->disabled = 1;
  274. cpuhw->n_added = 0;
  275. /*
  276. * Check if we ever enabled the PMU on this cpu.
  277. */
  278. if (!cpuhw->pmcs_enabled) {
  279. if (ppc_md.enable_pmcs)
  280. ppc_md.enable_pmcs();
  281. cpuhw->pmcs_enabled = 1;
  282. }
  283. /*
  284. * Set the 'freeze counters' bit.
  285. * The barrier is to make sure the mtspr has been
  286. * executed and the PMU has frozen the counters
  287. * before we return.
  288. */
  289. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
  290. mb();
  291. }
  292. local_irq_restore(flags);
  293. return ret;
  294. }
  295. /*
  296. * Re-enable all counters if disable == 0.
  297. * If we were previously disabled and counters were added, then
  298. * put the new config on the PMU.
  299. */
  300. void hw_perf_restore(u64 disable)
  301. {
  302. struct perf_counter *counter;
  303. struct cpu_hw_counters *cpuhw;
  304. unsigned long flags;
  305. long i;
  306. unsigned long val;
  307. s64 left;
  308. unsigned int hwc_index[MAX_HWCOUNTERS];
  309. if (disable)
  310. return;
  311. local_irq_save(flags);
  312. cpuhw = &__get_cpu_var(cpu_hw_counters);
  313. cpuhw->disabled = 0;
  314. /*
  315. * If we didn't change anything, or only removed counters,
  316. * no need to recalculate MMCR* settings and reset the PMCs.
  317. * Just reenable the PMU with the current MMCR* settings
  318. * (possibly updated for removal of counters).
  319. */
  320. if (!cpuhw->n_added) {
  321. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  322. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  323. mtspr(SPRN_MMCR0, cpuhw->mmcr[0]);
  324. if (cpuhw->n_counters == 0)
  325. get_lppaca()->pmcregs_in_use = 0;
  326. goto out;
  327. }
  328. /*
  329. * Compute MMCR* values for the new set of counters
  330. */
  331. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index,
  332. cpuhw->mmcr)) {
  333. /* shouldn't ever get here */
  334. printk(KERN_ERR "oops compute_mmcr failed\n");
  335. goto out;
  336. }
  337. /*
  338. * Add in MMCR0 freeze bits corresponding to the
  339. * hw_event.exclude_* bits for the first counter.
  340. * We have already checked that all counters have the
  341. * same values for these bits as the first counter.
  342. */
  343. counter = cpuhw->counter[0];
  344. if (counter->hw_event.exclude_user)
  345. cpuhw->mmcr[0] |= MMCR0_FCP;
  346. if (counter->hw_event.exclude_kernel)
  347. cpuhw->mmcr[0] |= freeze_counters_kernel;
  348. if (counter->hw_event.exclude_hv)
  349. cpuhw->mmcr[0] |= MMCR0_FCHV;
  350. /*
  351. * Write the new configuration to MMCR* with the freeze
  352. * bit set and set the hardware counters to their initial values.
  353. * Then unfreeze the counters.
  354. */
  355. get_lppaca()->pmcregs_in_use = 1;
  356. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  357. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  358. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  359. | MMCR0_FC);
  360. /*
  361. * Read off any pre-existing counters that need to move
  362. * to another PMC.
  363. */
  364. for (i = 0; i < cpuhw->n_counters; ++i) {
  365. counter = cpuhw->counter[i];
  366. if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) {
  367. power_perf_read(counter);
  368. write_pmc(counter->hw.idx, 0);
  369. counter->hw.idx = 0;
  370. }
  371. }
  372. /*
  373. * Initialize the PMCs for all the new and moved counters.
  374. */
  375. for (i = 0; i < cpuhw->n_counters; ++i) {
  376. counter = cpuhw->counter[i];
  377. if (counter->hw.idx)
  378. continue;
  379. val = 0;
  380. if (counter->hw_event.irq_period) {
  381. left = atomic64_read(&counter->hw.period_left);
  382. if (left < 0x80000000L)
  383. val = 0x80000000L - left;
  384. }
  385. atomic64_set(&counter->hw.prev_count, val);
  386. counter->hw.idx = hwc_index[i] + 1;
  387. write_pmc(counter->hw.idx, val);
  388. }
  389. mb();
  390. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  391. mtspr(SPRN_MMCR0, cpuhw->mmcr[0]);
  392. out:
  393. local_irq_restore(flags);
  394. }
  395. static int collect_events(struct perf_counter *group, int max_count,
  396. struct perf_counter *ctrs[], unsigned int *events)
  397. {
  398. int n = 0;
  399. struct perf_counter *counter;
  400. if (!is_software_counter(group)) {
  401. if (n >= max_count)
  402. return -1;
  403. ctrs[n] = group;
  404. events[n++] = group->hw.config;
  405. }
  406. list_for_each_entry(counter, &group->sibling_list, list_entry) {
  407. if (!is_software_counter(counter) &&
  408. counter->state != PERF_COUNTER_STATE_OFF) {
  409. if (n >= max_count)
  410. return -1;
  411. ctrs[n] = counter;
  412. events[n++] = counter->hw.config;
  413. }
  414. }
  415. return n;
  416. }
  417. static void counter_sched_in(struct perf_counter *counter, int cpu)
  418. {
  419. counter->state = PERF_COUNTER_STATE_ACTIVE;
  420. counter->oncpu = cpu;
  421. if (is_software_counter(counter))
  422. counter->hw_ops->enable(counter);
  423. }
  424. /*
  425. * Called to enable a whole group of counters.
  426. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  427. * Assumes the caller has disabled interrupts and has
  428. * frozen the PMU with hw_perf_save_disable.
  429. */
  430. int hw_perf_group_sched_in(struct perf_counter *group_leader,
  431. struct perf_cpu_context *cpuctx,
  432. struct perf_counter_context *ctx, int cpu)
  433. {
  434. struct cpu_hw_counters *cpuhw;
  435. long i, n, n0;
  436. struct perf_counter *sub;
  437. cpuhw = &__get_cpu_var(cpu_hw_counters);
  438. n0 = cpuhw->n_counters;
  439. n = collect_events(group_leader, ppmu->n_counter - n0,
  440. &cpuhw->counter[n0], &cpuhw->events[n0]);
  441. if (n < 0)
  442. return -EAGAIN;
  443. if (check_excludes(cpuhw->counter, n0, n))
  444. return -EAGAIN;
  445. if (power_check_constraints(cpuhw->events, n + n0))
  446. return -EAGAIN;
  447. cpuhw->n_counters = n0 + n;
  448. cpuhw->n_added += n;
  449. /*
  450. * OK, this group can go on; update counter states etc.,
  451. * and enable any software counters
  452. */
  453. for (i = n0; i < n0 + n; ++i)
  454. cpuhw->counter[i]->hw.config = cpuhw->events[i];
  455. cpuctx->active_oncpu += n;
  456. n = 1;
  457. counter_sched_in(group_leader, cpu);
  458. list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
  459. if (sub->state != PERF_COUNTER_STATE_OFF) {
  460. counter_sched_in(sub, cpu);
  461. ++n;
  462. }
  463. }
  464. ctx->nr_active += n;
  465. return 1;
  466. }
  467. /*
  468. * Add a counter to the PMU.
  469. * If all counters are not already frozen, then we disable and
  470. * re-enable the PMU in order to get hw_perf_restore to do the
  471. * actual work of reconfiguring the PMU.
  472. */
  473. static int power_perf_enable(struct perf_counter *counter)
  474. {
  475. struct cpu_hw_counters *cpuhw;
  476. unsigned long flags;
  477. u64 pmudis;
  478. int n0;
  479. int ret = -EAGAIN;
  480. local_irq_save(flags);
  481. pmudis = hw_perf_save_disable();
  482. /*
  483. * Add the counter to the list (if there is room)
  484. * and check whether the total set is still feasible.
  485. */
  486. cpuhw = &__get_cpu_var(cpu_hw_counters);
  487. n0 = cpuhw->n_counters;
  488. if (n0 >= ppmu->n_counter)
  489. goto out;
  490. cpuhw->counter[n0] = counter;
  491. cpuhw->events[n0] = counter->hw.config;
  492. if (check_excludes(cpuhw->counter, n0, 1))
  493. goto out;
  494. if (power_check_constraints(cpuhw->events, n0 + 1))
  495. goto out;
  496. counter->hw.config = cpuhw->events[n0];
  497. ++cpuhw->n_counters;
  498. ++cpuhw->n_added;
  499. ret = 0;
  500. out:
  501. hw_perf_restore(pmudis);
  502. local_irq_restore(flags);
  503. return ret;
  504. }
  505. /*
  506. * Remove a counter from the PMU.
  507. */
  508. static void power_perf_disable(struct perf_counter *counter)
  509. {
  510. struct cpu_hw_counters *cpuhw;
  511. long i;
  512. u64 pmudis;
  513. unsigned long flags;
  514. local_irq_save(flags);
  515. pmudis = hw_perf_save_disable();
  516. power_perf_read(counter);
  517. cpuhw = &__get_cpu_var(cpu_hw_counters);
  518. for (i = 0; i < cpuhw->n_counters; ++i) {
  519. if (counter == cpuhw->counter[i]) {
  520. while (++i < cpuhw->n_counters)
  521. cpuhw->counter[i-1] = cpuhw->counter[i];
  522. --cpuhw->n_counters;
  523. ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
  524. write_pmc(counter->hw.idx, 0);
  525. counter->hw.idx = 0;
  526. break;
  527. }
  528. }
  529. if (cpuhw->n_counters == 0) {
  530. /* disable exceptions if no counters are running */
  531. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  532. }
  533. hw_perf_restore(pmudis);
  534. local_irq_restore(flags);
  535. }
  536. struct hw_perf_counter_ops power_perf_ops = {
  537. .enable = power_perf_enable,
  538. .disable = power_perf_disable,
  539. .read = power_perf_read
  540. };
  541. const struct hw_perf_counter_ops *
  542. hw_perf_counter_init(struct perf_counter *counter)
  543. {
  544. unsigned long ev;
  545. struct perf_counter *ctrs[MAX_HWCOUNTERS];
  546. unsigned int events[MAX_HWCOUNTERS];
  547. int n;
  548. if (!ppmu)
  549. return NULL;
  550. if ((s64)counter->hw_event.irq_period < 0)
  551. return NULL;
  552. ev = counter->hw_event.type;
  553. if (!counter->hw_event.raw) {
  554. if (ev >= ppmu->n_generic ||
  555. ppmu->generic_events[ev] == 0)
  556. return NULL;
  557. ev = ppmu->generic_events[ev];
  558. }
  559. counter->hw.config_base = ev;
  560. counter->hw.idx = 0;
  561. /*
  562. * If we are not running on a hypervisor, force the
  563. * exclude_hv bit to 0 so that we don't care what
  564. * the user set it to.
  565. */
  566. if (!firmware_has_feature(FW_FEATURE_LPAR))
  567. counter->hw_event.exclude_hv = 0;
  568. /*
  569. * If this is in a group, check if it can go on with all the
  570. * other hardware counters in the group. We assume the counter
  571. * hasn't been linked into its leader's sibling list at this point.
  572. */
  573. n = 0;
  574. if (counter->group_leader != counter) {
  575. n = collect_events(counter->group_leader, ppmu->n_counter - 1,
  576. ctrs, events);
  577. if (n < 0)
  578. return NULL;
  579. }
  580. events[n] = ev;
  581. ctrs[n] = counter;
  582. if (check_excludes(ctrs, n, 1))
  583. return NULL;
  584. if (power_check_constraints(events, n + 1))
  585. return NULL;
  586. counter->hw.config = events[n];
  587. atomic64_set(&counter->hw.period_left, counter->hw_event.irq_period);
  588. return &power_perf_ops;
  589. }
  590. /*
  591. * Handle wakeups.
  592. */
  593. void perf_counter_do_pending(void)
  594. {
  595. int i;
  596. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  597. struct perf_counter *counter;
  598. set_perf_counter_pending(0);
  599. for (i = 0; i < cpuhw->n_counters; ++i) {
  600. counter = cpuhw->counter[i];
  601. if (counter && counter->wakeup_pending) {
  602. counter->wakeup_pending = 0;
  603. wake_up(&counter->waitq);
  604. }
  605. }
  606. }
  607. /*
  608. * Record data for an irq counter.
  609. * This function was lifted from the x86 code; maybe it should
  610. * go in the core?
  611. */
  612. static void perf_store_irq_data(struct perf_counter *counter, u64 data)
  613. {
  614. struct perf_data *irqdata = counter->irqdata;
  615. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  616. irqdata->overrun++;
  617. } else {
  618. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  619. *p = data;
  620. irqdata->len += sizeof(u64);
  621. }
  622. }
  623. /*
  624. * Record all the values of the counters in a group
  625. */
  626. static void perf_handle_group(struct perf_counter *counter)
  627. {
  628. struct perf_counter *leader, *sub;
  629. leader = counter->group_leader;
  630. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  631. if (sub != counter)
  632. sub->hw_ops->read(sub);
  633. perf_store_irq_data(counter, sub->hw_event.type);
  634. perf_store_irq_data(counter, atomic64_read(&sub->count));
  635. }
  636. }
  637. /*
  638. * A counter has overflowed; update its count and record
  639. * things if requested. Note that interrupts are hard-disabled
  640. * here so there is no possibility of being interrupted.
  641. */
  642. static void record_and_restart(struct perf_counter *counter, long val,
  643. struct pt_regs *regs)
  644. {
  645. s64 prev, delta, left;
  646. int record = 0;
  647. /* we don't have to worry about interrupts here */
  648. prev = atomic64_read(&counter->hw.prev_count);
  649. delta = (val - prev) & 0xfffffffful;
  650. atomic64_add(delta, &counter->count);
  651. /*
  652. * See if the total period for this counter has expired,
  653. * and update for the next period.
  654. */
  655. val = 0;
  656. left = atomic64_read(&counter->hw.period_left) - delta;
  657. if (counter->hw_event.irq_period) {
  658. if (left <= 0) {
  659. left += counter->hw_event.irq_period;
  660. if (left <= 0)
  661. left = counter->hw_event.irq_period;
  662. record = 1;
  663. }
  664. if (left < 0x80000000L)
  665. val = 0x80000000L - left;
  666. }
  667. write_pmc(counter->hw.idx, val);
  668. atomic64_set(&counter->hw.prev_count, val);
  669. atomic64_set(&counter->hw.period_left, left);
  670. /*
  671. * Finally record data if requested.
  672. */
  673. if (record) {
  674. switch (counter->hw_event.record_type) {
  675. case PERF_RECORD_SIMPLE:
  676. break;
  677. case PERF_RECORD_IRQ:
  678. perf_store_irq_data(counter, instruction_pointer(regs));
  679. counter->wakeup_pending = 1;
  680. break;
  681. case PERF_RECORD_GROUP:
  682. perf_handle_group(counter);
  683. counter->wakeup_pending = 1;
  684. break;
  685. }
  686. }
  687. }
  688. /*
  689. * Performance monitor interrupt stuff
  690. */
  691. static void perf_counter_interrupt(struct pt_regs *regs)
  692. {
  693. int i;
  694. struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters);
  695. struct perf_counter *counter;
  696. long val;
  697. int need_wakeup = 0, found = 0;
  698. for (i = 0; i < cpuhw->n_counters; ++i) {
  699. counter = cpuhw->counter[i];
  700. val = read_pmc(counter->hw.idx);
  701. if ((int)val < 0) {
  702. /* counter has overflowed */
  703. found = 1;
  704. record_and_restart(counter, val, regs);
  705. if (counter->wakeup_pending)
  706. need_wakeup = 1;
  707. }
  708. }
  709. /*
  710. * In case we didn't find and reset the counter that caused
  711. * the interrupt, scan all counters and reset any that are
  712. * negative, to avoid getting continual interrupts.
  713. * Any that we processed in the previous loop will not be negative.
  714. */
  715. if (!found) {
  716. for (i = 0; i < ppmu->n_counter; ++i) {
  717. val = read_pmc(i + 1);
  718. if ((int)val < 0)
  719. write_pmc(i + 1, 0);
  720. }
  721. }
  722. /*
  723. * Reset MMCR0 to its normal value. This will set PMXE and
  724. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  725. * and thus allow interrupts to occur again.
  726. * XXX might want to use MSR.PM to keep the counters frozen until
  727. * we get back out of this interrupt.
  728. */
  729. mtspr(SPRN_MMCR0, cpuhw->mmcr[0]);
  730. /*
  731. * If we need a wakeup, check whether interrupts were soft-enabled
  732. * when we took the interrupt. If they were, we can wake stuff up
  733. * immediately; otherwise we'll have to set a flag and do the
  734. * wakeup when interrupts get soft-enabled.
  735. */
  736. if (need_wakeup) {
  737. if (regs->softe) {
  738. irq_enter();
  739. perf_counter_do_pending();
  740. irq_exit();
  741. } else {
  742. set_perf_counter_pending(1);
  743. }
  744. }
  745. }
  746. void hw_perf_counter_setup(int cpu)
  747. {
  748. struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu);
  749. memset(cpuhw, 0, sizeof(*cpuhw));
  750. cpuhw->mmcr[0] = MMCR0_FC;
  751. }
  752. extern struct power_pmu ppc970_pmu;
  753. extern struct power_pmu power5_pmu;
  754. extern struct power_pmu power5p_pmu;
  755. extern struct power_pmu power6_pmu;
  756. static int init_perf_counters(void)
  757. {
  758. unsigned long pvr;
  759. if (reserve_pmc_hardware(perf_counter_interrupt)) {
  760. printk(KERN_ERR "Couldn't init performance monitor subsystem\n");
  761. return -EBUSY;
  762. }
  763. /* XXX should get this from cputable */
  764. pvr = mfspr(SPRN_PVR);
  765. switch (PVR_VER(pvr)) {
  766. case PV_970:
  767. case PV_970FX:
  768. case PV_970MP:
  769. ppmu = &ppc970_pmu;
  770. break;
  771. case PV_POWER5:
  772. ppmu = &power5_pmu;
  773. break;
  774. case PV_POWER5p:
  775. ppmu = &power5p_pmu;
  776. break;
  777. case 0x3e:
  778. ppmu = &power6_pmu;
  779. break;
  780. }
  781. /*
  782. * Use FCHV to ignore kernel events if MSR.HV is set.
  783. */
  784. if (mfmsr() & MSR_HV)
  785. freeze_counters_kernel = MMCR0_FCHV;
  786. return 0;
  787. }
  788. arch_initcall(init_perf_counters);