perf_counter.c 18 KB

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