perf_counter.c 20 KB

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