perf_counter.c 18 KB

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