perf_event_fsl_emb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Performance event support - Freescale Embedded Performance Monitor
  3. *
  4. * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
  5. * Copyright 2010 Freescale Semiconductor, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/perf_event.h>
  15. #include <linux/percpu.h>
  16. #include <linux/hardirq.h>
  17. #include <asm/reg_fsl_emb.h>
  18. #include <asm/pmc.h>
  19. #include <asm/machdep.h>
  20. #include <asm/firmware.h>
  21. #include <asm/ptrace.h>
  22. struct cpu_hw_events {
  23. int n_events;
  24. int disabled;
  25. u8 pmcs_enabled;
  26. struct perf_event *event[MAX_HWEVENTS];
  27. };
  28. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  29. static struct fsl_emb_pmu *ppmu;
  30. /* Number of perf_events counting hardware events */
  31. static atomic_t num_events;
  32. /* Used to avoid races in calling reserve/release_pmc_hardware */
  33. static DEFINE_MUTEX(pmc_reserve_mutex);
  34. /*
  35. * If interrupts were soft-disabled when a PMU interrupt occurs, treat
  36. * it as an NMI.
  37. */
  38. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  39. {
  40. #ifdef __powerpc64__
  41. return !regs->softe;
  42. #else
  43. return 0;
  44. #endif
  45. }
  46. static void perf_event_interrupt(struct pt_regs *regs);
  47. /*
  48. * Read one performance monitor counter (PMC).
  49. */
  50. static unsigned long read_pmc(int idx)
  51. {
  52. unsigned long val;
  53. switch (idx) {
  54. case 0:
  55. val = mfpmr(PMRN_PMC0);
  56. break;
  57. case 1:
  58. val = mfpmr(PMRN_PMC1);
  59. break;
  60. case 2:
  61. val = mfpmr(PMRN_PMC2);
  62. break;
  63. case 3:
  64. val = mfpmr(PMRN_PMC3);
  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 0:
  79. mtpmr(PMRN_PMC0, val);
  80. break;
  81. case 1:
  82. mtpmr(PMRN_PMC1, val);
  83. break;
  84. case 2:
  85. mtpmr(PMRN_PMC2, val);
  86. break;
  87. case 3:
  88. mtpmr(PMRN_PMC3, val);
  89. break;
  90. default:
  91. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  92. }
  93. isync();
  94. }
  95. /*
  96. * Write one local control A register
  97. */
  98. static void write_pmlca(int idx, unsigned long val)
  99. {
  100. switch (idx) {
  101. case 0:
  102. mtpmr(PMRN_PMLCA0, val);
  103. break;
  104. case 1:
  105. mtpmr(PMRN_PMLCA1, val);
  106. break;
  107. case 2:
  108. mtpmr(PMRN_PMLCA2, val);
  109. break;
  110. case 3:
  111. mtpmr(PMRN_PMLCA3, val);
  112. break;
  113. default:
  114. printk(KERN_ERR "oops trying to write PMLCA%d\n", idx);
  115. }
  116. isync();
  117. }
  118. /*
  119. * Write one local control B register
  120. */
  121. static void write_pmlcb(int idx, unsigned long val)
  122. {
  123. switch (idx) {
  124. case 0:
  125. mtpmr(PMRN_PMLCB0, val);
  126. break;
  127. case 1:
  128. mtpmr(PMRN_PMLCB1, val);
  129. break;
  130. case 2:
  131. mtpmr(PMRN_PMLCB2, val);
  132. break;
  133. case 3:
  134. mtpmr(PMRN_PMLCB3, val);
  135. break;
  136. default:
  137. printk(KERN_ERR "oops trying to write PMLCB%d\n", idx);
  138. }
  139. isync();
  140. }
  141. static void fsl_emb_pmu_read(struct perf_event *event)
  142. {
  143. s64 val, delta, prev;
  144. /*
  145. * Performance monitor interrupts come even when interrupts
  146. * are soft-disabled, as long as interrupts are hard-enabled.
  147. * Therefore we treat them like NMIs.
  148. */
  149. do {
  150. prev = atomic64_read(&event->hw.prev_count);
  151. barrier();
  152. val = read_pmc(event->hw.idx);
  153. } while (atomic64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  154. /* The counters are only 32 bits wide */
  155. delta = (val - prev) & 0xfffffffful;
  156. atomic64_add(delta, &event->count);
  157. atomic64_sub(delta, &event->hw.period_left);
  158. }
  159. /*
  160. * Disable all events to prevent PMU interrupts and to allow
  161. * events to be added or removed.
  162. */
  163. void hw_perf_disable(void)
  164. {
  165. struct cpu_hw_events *cpuhw;
  166. unsigned long flags;
  167. local_irq_save(flags);
  168. cpuhw = &__get_cpu_var(cpu_hw_events);
  169. if (!cpuhw->disabled) {
  170. cpuhw->disabled = 1;
  171. /*
  172. * Check if we ever enabled the PMU on this cpu.
  173. */
  174. if (!cpuhw->pmcs_enabled) {
  175. ppc_enable_pmcs();
  176. cpuhw->pmcs_enabled = 1;
  177. }
  178. if (atomic_read(&num_events)) {
  179. /*
  180. * Set the 'freeze all counters' bit, and disable
  181. * interrupts. The barrier is to make sure the
  182. * mtpmr has been executed and the PMU has frozen
  183. * the events before we return.
  184. */
  185. mtpmr(PMRN_PMGC0, PMGC0_FAC);
  186. isync();
  187. }
  188. }
  189. local_irq_restore(flags);
  190. }
  191. /*
  192. * Re-enable all events if disable == 0.
  193. * If we were previously disabled and events were added, then
  194. * put the new config on the PMU.
  195. */
  196. void hw_perf_enable(void)
  197. {
  198. struct cpu_hw_events *cpuhw;
  199. unsigned long flags;
  200. local_irq_save(flags);
  201. cpuhw = &__get_cpu_var(cpu_hw_events);
  202. if (!cpuhw->disabled)
  203. goto out;
  204. cpuhw->disabled = 0;
  205. ppc_set_pmu_inuse(cpuhw->n_events != 0);
  206. if (cpuhw->n_events > 0) {
  207. mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
  208. isync();
  209. }
  210. out:
  211. local_irq_restore(flags);
  212. }
  213. static int collect_events(struct perf_event *group, int max_count,
  214. struct perf_event *ctrs[])
  215. {
  216. int n = 0;
  217. struct perf_event *event;
  218. if (!is_software_event(group)) {
  219. if (n >= max_count)
  220. return -1;
  221. ctrs[n] = group;
  222. n++;
  223. }
  224. list_for_each_entry(event, &group->sibling_list, group_entry) {
  225. if (!is_software_event(event) &&
  226. event->state != PERF_EVENT_STATE_OFF) {
  227. if (n >= max_count)
  228. return -1;
  229. ctrs[n] = event;
  230. n++;
  231. }
  232. }
  233. return n;
  234. }
  235. /* perf must be disabled, context locked on entry */
  236. static int fsl_emb_pmu_enable(struct perf_event *event)
  237. {
  238. struct cpu_hw_events *cpuhw;
  239. int ret = -EAGAIN;
  240. int num_counters = ppmu->n_counter;
  241. u64 val;
  242. int i;
  243. cpuhw = &get_cpu_var(cpu_hw_events);
  244. if (event->hw.config & FSL_EMB_EVENT_RESTRICTED)
  245. num_counters = ppmu->n_restricted;
  246. /*
  247. * Allocate counters from top-down, so that restricted-capable
  248. * counters are kept free as long as possible.
  249. */
  250. for (i = num_counters - 1; i >= 0; i--) {
  251. if (cpuhw->event[i])
  252. continue;
  253. break;
  254. }
  255. if (i < 0)
  256. goto out;
  257. event->hw.idx = i;
  258. cpuhw->event[i] = event;
  259. ++cpuhw->n_events;
  260. val = 0;
  261. if (event->hw.sample_period) {
  262. s64 left = atomic64_read(&event->hw.period_left);
  263. if (left < 0x80000000L)
  264. val = 0x80000000L - left;
  265. }
  266. atomic64_set(&event->hw.prev_count, val);
  267. write_pmc(i, val);
  268. perf_event_update_userpage(event);
  269. write_pmlcb(i, event->hw.config >> 32);
  270. write_pmlca(i, event->hw.config_base);
  271. ret = 0;
  272. out:
  273. put_cpu_var(cpu_hw_events);
  274. return ret;
  275. }
  276. /* perf must be disabled, context locked on entry */
  277. static void fsl_emb_pmu_disable(struct perf_event *event)
  278. {
  279. struct cpu_hw_events *cpuhw;
  280. int i = event->hw.idx;
  281. if (i < 0)
  282. goto out;
  283. fsl_emb_pmu_read(event);
  284. cpuhw = &get_cpu_var(cpu_hw_events);
  285. WARN_ON(event != cpuhw->event[event->hw.idx]);
  286. write_pmlca(i, 0);
  287. write_pmlcb(i, 0);
  288. write_pmc(i, 0);
  289. cpuhw->event[i] = NULL;
  290. event->hw.idx = -1;
  291. /*
  292. * TODO: if at least one restricted event exists, and we
  293. * just freed up a non-restricted-capable counter, and
  294. * there is a restricted-capable counter occupied by
  295. * a non-restricted event, migrate that event to the
  296. * vacated counter.
  297. */
  298. cpuhw->n_events--;
  299. out:
  300. put_cpu_var(cpu_hw_events);
  301. }
  302. /*
  303. * Re-enable interrupts on a event after they were throttled
  304. * because they were coming too fast.
  305. *
  306. * Context is locked on entry, but perf is not disabled.
  307. */
  308. static void fsl_emb_pmu_unthrottle(struct perf_event *event)
  309. {
  310. s64 val, left;
  311. unsigned long flags;
  312. if (event->hw.idx < 0 || !event->hw.sample_period)
  313. return;
  314. local_irq_save(flags);
  315. perf_disable();
  316. fsl_emb_pmu_read(event);
  317. left = event->hw.sample_period;
  318. event->hw.last_period = left;
  319. val = 0;
  320. if (left < 0x80000000L)
  321. val = 0x80000000L - left;
  322. write_pmc(event->hw.idx, val);
  323. atomic64_set(&event->hw.prev_count, val);
  324. atomic64_set(&event->hw.period_left, left);
  325. perf_event_update_userpage(event);
  326. perf_enable();
  327. local_irq_restore(flags);
  328. }
  329. static struct pmu fsl_emb_pmu = {
  330. .enable = fsl_emb_pmu_enable,
  331. .disable = fsl_emb_pmu_disable,
  332. .read = fsl_emb_pmu_read,
  333. .unthrottle = fsl_emb_pmu_unthrottle,
  334. };
  335. /*
  336. * Release the PMU if this is the last perf_event.
  337. */
  338. static void hw_perf_event_destroy(struct perf_event *event)
  339. {
  340. if (!atomic_add_unless(&num_events, -1, 1)) {
  341. mutex_lock(&pmc_reserve_mutex);
  342. if (atomic_dec_return(&num_events) == 0)
  343. release_pmc_hardware();
  344. mutex_unlock(&pmc_reserve_mutex);
  345. }
  346. }
  347. /*
  348. * Translate a generic cache event_id config to a raw event_id code.
  349. */
  350. static int hw_perf_cache_event(u64 config, u64 *eventp)
  351. {
  352. unsigned long type, op, result;
  353. int ev;
  354. if (!ppmu->cache_events)
  355. return -EINVAL;
  356. /* unpack config */
  357. type = config & 0xff;
  358. op = (config >> 8) & 0xff;
  359. result = (config >> 16) & 0xff;
  360. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  361. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  362. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  363. return -EINVAL;
  364. ev = (*ppmu->cache_events)[type][op][result];
  365. if (ev == 0)
  366. return -EOPNOTSUPP;
  367. if (ev == -1)
  368. return -EINVAL;
  369. *eventp = ev;
  370. return 0;
  371. }
  372. const struct pmu *hw_perf_event_init(struct perf_event *event)
  373. {
  374. u64 ev;
  375. struct perf_event *events[MAX_HWEVENTS];
  376. int n;
  377. int err;
  378. int num_restricted;
  379. int i;
  380. switch (event->attr.type) {
  381. case PERF_TYPE_HARDWARE:
  382. ev = event->attr.config;
  383. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  384. return ERR_PTR(-EOPNOTSUPP);
  385. ev = ppmu->generic_events[ev];
  386. break;
  387. case PERF_TYPE_HW_CACHE:
  388. err = hw_perf_cache_event(event->attr.config, &ev);
  389. if (err)
  390. return ERR_PTR(err);
  391. break;
  392. case PERF_TYPE_RAW:
  393. ev = event->attr.config;
  394. break;
  395. default:
  396. return ERR_PTR(-EINVAL);
  397. }
  398. event->hw.config = ppmu->xlate_event(ev);
  399. if (!(event->hw.config & FSL_EMB_EVENT_VALID))
  400. return ERR_PTR(-EINVAL);
  401. /*
  402. * If this is in a group, check if it can go on with all the
  403. * other hardware events in the group. We assume the event
  404. * hasn't been linked into its leader's sibling list at this point.
  405. */
  406. n = 0;
  407. if (event->group_leader != event) {
  408. n = collect_events(event->group_leader,
  409. ppmu->n_counter - 1, events);
  410. if (n < 0)
  411. return ERR_PTR(-EINVAL);
  412. }
  413. if (event->hw.config & FSL_EMB_EVENT_RESTRICTED) {
  414. num_restricted = 0;
  415. for (i = 0; i < n; i++) {
  416. if (events[i]->hw.config & FSL_EMB_EVENT_RESTRICTED)
  417. num_restricted++;
  418. }
  419. if (num_restricted >= ppmu->n_restricted)
  420. return ERR_PTR(-EINVAL);
  421. }
  422. event->hw.idx = -1;
  423. event->hw.config_base = PMLCA_CE | PMLCA_FCM1 |
  424. (u32)((ev << 16) & PMLCA_EVENT_MASK);
  425. if (event->attr.exclude_user)
  426. event->hw.config_base |= PMLCA_FCU;
  427. if (event->attr.exclude_kernel)
  428. event->hw.config_base |= PMLCA_FCS;
  429. if (event->attr.exclude_idle)
  430. return ERR_PTR(-ENOTSUPP);
  431. event->hw.last_period = event->hw.sample_period;
  432. atomic64_set(&event->hw.period_left, event->hw.last_period);
  433. /*
  434. * See if we need to reserve the PMU.
  435. * If no events are currently in use, then we have to take a
  436. * mutex to ensure that we don't race with another task doing
  437. * reserve_pmc_hardware or release_pmc_hardware.
  438. */
  439. err = 0;
  440. if (!atomic_inc_not_zero(&num_events)) {
  441. mutex_lock(&pmc_reserve_mutex);
  442. if (atomic_read(&num_events) == 0 &&
  443. reserve_pmc_hardware(perf_event_interrupt))
  444. err = -EBUSY;
  445. else
  446. atomic_inc(&num_events);
  447. mutex_unlock(&pmc_reserve_mutex);
  448. mtpmr(PMRN_PMGC0, PMGC0_FAC);
  449. isync();
  450. }
  451. event->destroy = hw_perf_event_destroy;
  452. if (err)
  453. return ERR_PTR(err);
  454. return &fsl_emb_pmu;
  455. }
  456. /*
  457. * A counter has overflowed; update its count and record
  458. * things if requested. Note that interrupts are hard-disabled
  459. * here so there is no possibility of being interrupted.
  460. */
  461. static void record_and_restart(struct perf_event *event, unsigned long val,
  462. struct pt_regs *regs, int nmi)
  463. {
  464. u64 period = event->hw.sample_period;
  465. s64 prev, delta, left;
  466. int record = 0;
  467. /* we don't have to worry about interrupts here */
  468. prev = atomic64_read(&event->hw.prev_count);
  469. delta = (val - prev) & 0xfffffffful;
  470. atomic64_add(delta, &event->count);
  471. /*
  472. * See if the total period for this event has expired,
  473. * and update for the next period.
  474. */
  475. val = 0;
  476. left = atomic64_read(&event->hw.period_left) - delta;
  477. if (period) {
  478. if (left <= 0) {
  479. left += period;
  480. if (left <= 0)
  481. left = period;
  482. record = 1;
  483. }
  484. if (left < 0x80000000LL)
  485. val = 0x80000000LL - left;
  486. }
  487. /*
  488. * Finally record data if requested.
  489. */
  490. if (record) {
  491. struct perf_sample_data data;
  492. perf_sample_data_init(&data, 0);
  493. if (perf_event_overflow(event, nmi, &data, regs)) {
  494. /*
  495. * Interrupts are coming too fast - throttle them
  496. * by setting the event to 0, so it will be
  497. * at least 2^30 cycles until the next interrupt
  498. * (assuming each event counts at most 2 counts
  499. * per cycle).
  500. */
  501. val = 0;
  502. left = ~0ULL >> 1;
  503. }
  504. }
  505. write_pmc(event->hw.idx, val);
  506. atomic64_set(&event->hw.prev_count, val);
  507. atomic64_set(&event->hw.period_left, left);
  508. perf_event_update_userpage(event);
  509. }
  510. static void perf_event_interrupt(struct pt_regs *regs)
  511. {
  512. int i;
  513. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  514. struct perf_event *event;
  515. unsigned long val;
  516. int found = 0;
  517. int nmi;
  518. nmi = perf_intr_is_nmi(regs);
  519. if (nmi)
  520. nmi_enter();
  521. else
  522. irq_enter();
  523. for (i = 0; i < ppmu->n_counter; ++i) {
  524. event = cpuhw->event[i];
  525. val = read_pmc(i);
  526. if ((int)val < 0) {
  527. if (event) {
  528. /* event has overflowed */
  529. found = 1;
  530. record_and_restart(event, val, regs, nmi);
  531. } else {
  532. /*
  533. * Disabled counter is negative,
  534. * reset it just in case.
  535. */
  536. write_pmc(i, 0);
  537. }
  538. }
  539. }
  540. /* PMM will keep counters frozen until we return from the interrupt. */
  541. mtmsr(mfmsr() | MSR_PMM);
  542. mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
  543. isync();
  544. if (nmi)
  545. nmi_exit();
  546. else
  547. irq_exit();
  548. }
  549. void hw_perf_event_setup(int cpu)
  550. {
  551. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  552. memset(cpuhw, 0, sizeof(*cpuhw));
  553. }
  554. int register_fsl_emb_pmu(struct fsl_emb_pmu *pmu)
  555. {
  556. if (ppmu)
  557. return -EBUSY; /* something's already registered */
  558. ppmu = pmu;
  559. pr_info("%s performance monitor hardware support registered\n",
  560. pmu->name);
  561. return 0;
  562. }