perf_counter.c 20 KB

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