core-fsl-emb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. case 4:
  67. val = mfpmr(PMRN_PMC4);
  68. break;
  69. case 5:
  70. val = mfpmr(PMRN_PMC5);
  71. break;
  72. default:
  73. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  74. val = 0;
  75. }
  76. return val;
  77. }
  78. /*
  79. * Write one PMC.
  80. */
  81. static void write_pmc(int idx, unsigned long val)
  82. {
  83. switch (idx) {
  84. case 0:
  85. mtpmr(PMRN_PMC0, val);
  86. break;
  87. case 1:
  88. mtpmr(PMRN_PMC1, val);
  89. break;
  90. case 2:
  91. mtpmr(PMRN_PMC2, val);
  92. break;
  93. case 3:
  94. mtpmr(PMRN_PMC3, val);
  95. break;
  96. case 4:
  97. mtpmr(PMRN_PMC4, val);
  98. break;
  99. case 5:
  100. mtpmr(PMRN_PMC5, val);
  101. break;
  102. default:
  103. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  104. }
  105. isync();
  106. }
  107. /*
  108. * Write one local control A register
  109. */
  110. static void write_pmlca(int idx, unsigned long val)
  111. {
  112. switch (idx) {
  113. case 0:
  114. mtpmr(PMRN_PMLCA0, val);
  115. break;
  116. case 1:
  117. mtpmr(PMRN_PMLCA1, val);
  118. break;
  119. case 2:
  120. mtpmr(PMRN_PMLCA2, val);
  121. break;
  122. case 3:
  123. mtpmr(PMRN_PMLCA3, val);
  124. break;
  125. case 4:
  126. mtpmr(PMRN_PMLCA4, val);
  127. break;
  128. case 5:
  129. mtpmr(PMRN_PMLCA5, val);
  130. break;
  131. default:
  132. printk(KERN_ERR "oops trying to write PMLCA%d\n", idx);
  133. }
  134. isync();
  135. }
  136. /*
  137. * Write one local control B register
  138. */
  139. static void write_pmlcb(int idx, unsigned long val)
  140. {
  141. switch (idx) {
  142. case 0:
  143. mtpmr(PMRN_PMLCB0, val);
  144. break;
  145. case 1:
  146. mtpmr(PMRN_PMLCB1, val);
  147. break;
  148. case 2:
  149. mtpmr(PMRN_PMLCB2, val);
  150. break;
  151. case 3:
  152. mtpmr(PMRN_PMLCB3, val);
  153. break;
  154. case 4:
  155. mtpmr(PMRN_PMLCB4, val);
  156. break;
  157. case 5:
  158. mtpmr(PMRN_PMLCB5, val);
  159. break;
  160. default:
  161. printk(KERN_ERR "oops trying to write PMLCB%d\n", idx);
  162. }
  163. isync();
  164. }
  165. static void fsl_emb_pmu_read(struct perf_event *event)
  166. {
  167. s64 val, delta, prev;
  168. if (event->hw.state & PERF_HES_STOPPED)
  169. return;
  170. /*
  171. * Performance monitor interrupts come even when interrupts
  172. * are soft-disabled, as long as interrupts are hard-enabled.
  173. * Therefore we treat them like NMIs.
  174. */
  175. do {
  176. prev = local64_read(&event->hw.prev_count);
  177. barrier();
  178. val = read_pmc(event->hw.idx);
  179. } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  180. /* The counters are only 32 bits wide */
  181. delta = (val - prev) & 0xfffffffful;
  182. local64_add(delta, &event->count);
  183. local64_sub(delta, &event->hw.period_left);
  184. }
  185. /*
  186. * Disable all events to prevent PMU interrupts and to allow
  187. * events to be added or removed.
  188. */
  189. static void fsl_emb_pmu_disable(struct pmu *pmu)
  190. {
  191. struct cpu_hw_events *cpuhw;
  192. unsigned long flags;
  193. local_irq_save(flags);
  194. cpuhw = this_cpu_ptr(&cpu_hw_events);
  195. if (!cpuhw->disabled) {
  196. cpuhw->disabled = 1;
  197. /*
  198. * Check if we ever enabled the PMU on this cpu.
  199. */
  200. if (!cpuhw->pmcs_enabled) {
  201. ppc_enable_pmcs();
  202. cpuhw->pmcs_enabled = 1;
  203. }
  204. if (atomic_read(&num_events)) {
  205. /*
  206. * Set the 'freeze all counters' bit, and disable
  207. * interrupts. The barrier is to make sure the
  208. * mtpmr has been executed and the PMU has frozen
  209. * the events before we return.
  210. */
  211. mtpmr(PMRN_PMGC0, PMGC0_FAC);
  212. isync();
  213. }
  214. }
  215. local_irq_restore(flags);
  216. }
  217. /*
  218. * Re-enable all events if disable == 0.
  219. * If we were previously disabled and events were added, then
  220. * put the new config on the PMU.
  221. */
  222. static void fsl_emb_pmu_enable(struct pmu *pmu)
  223. {
  224. struct cpu_hw_events *cpuhw;
  225. unsigned long flags;
  226. local_irq_save(flags);
  227. cpuhw = this_cpu_ptr(&cpu_hw_events);
  228. if (!cpuhw->disabled)
  229. goto out;
  230. cpuhw->disabled = 0;
  231. ppc_set_pmu_inuse(cpuhw->n_events != 0);
  232. if (cpuhw->n_events > 0) {
  233. mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
  234. isync();
  235. }
  236. out:
  237. local_irq_restore(flags);
  238. }
  239. static int collect_events(struct perf_event *group, int max_count,
  240. struct perf_event *ctrs[])
  241. {
  242. int n = 0;
  243. struct perf_event *event;
  244. if (!is_software_event(group)) {
  245. if (n >= max_count)
  246. return -1;
  247. ctrs[n] = group;
  248. n++;
  249. }
  250. list_for_each_entry(event, &group->sibling_list, group_entry) {
  251. if (!is_software_event(event) &&
  252. event->state != PERF_EVENT_STATE_OFF) {
  253. if (n >= max_count)
  254. return -1;
  255. ctrs[n] = event;
  256. n++;
  257. }
  258. }
  259. return n;
  260. }
  261. /* context locked on entry */
  262. static int fsl_emb_pmu_add(struct perf_event *event, int flags)
  263. {
  264. struct cpu_hw_events *cpuhw;
  265. int ret = -EAGAIN;
  266. int num_counters = ppmu->n_counter;
  267. u64 val;
  268. int i;
  269. perf_pmu_disable(event->pmu);
  270. cpuhw = &get_cpu_var(cpu_hw_events);
  271. if (event->hw.config & FSL_EMB_EVENT_RESTRICTED)
  272. num_counters = ppmu->n_restricted;
  273. /*
  274. * Allocate counters from top-down, so that restricted-capable
  275. * counters are kept free as long as possible.
  276. */
  277. for (i = num_counters - 1; i >= 0; i--) {
  278. if (cpuhw->event[i])
  279. continue;
  280. break;
  281. }
  282. if (i < 0)
  283. goto out;
  284. event->hw.idx = i;
  285. cpuhw->event[i] = event;
  286. ++cpuhw->n_events;
  287. val = 0;
  288. if (event->hw.sample_period) {
  289. s64 left = local64_read(&event->hw.period_left);
  290. if (left < 0x80000000L)
  291. val = 0x80000000L - left;
  292. }
  293. local64_set(&event->hw.prev_count, val);
  294. if (unlikely(!(flags & PERF_EF_START))) {
  295. event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  296. val = 0;
  297. } else {
  298. event->hw.state &= ~(PERF_HES_STOPPED | PERF_HES_UPTODATE);
  299. }
  300. write_pmc(i, val);
  301. perf_event_update_userpage(event);
  302. write_pmlcb(i, event->hw.config >> 32);
  303. write_pmlca(i, event->hw.config_base);
  304. ret = 0;
  305. out:
  306. put_cpu_var(cpu_hw_events);
  307. perf_pmu_enable(event->pmu);
  308. return ret;
  309. }
  310. /* context locked on entry */
  311. static void fsl_emb_pmu_del(struct perf_event *event, int flags)
  312. {
  313. struct cpu_hw_events *cpuhw;
  314. int i = event->hw.idx;
  315. perf_pmu_disable(event->pmu);
  316. if (i < 0)
  317. goto out;
  318. fsl_emb_pmu_read(event);
  319. cpuhw = &get_cpu_var(cpu_hw_events);
  320. WARN_ON(event != cpuhw->event[event->hw.idx]);
  321. write_pmlca(i, 0);
  322. write_pmlcb(i, 0);
  323. write_pmc(i, 0);
  324. cpuhw->event[i] = NULL;
  325. event->hw.idx = -1;
  326. /*
  327. * TODO: if at least one restricted event exists, and we
  328. * just freed up a non-restricted-capable counter, and
  329. * there is a restricted-capable counter occupied by
  330. * a non-restricted event, migrate that event to the
  331. * vacated counter.
  332. */
  333. cpuhw->n_events--;
  334. out:
  335. perf_pmu_enable(event->pmu);
  336. put_cpu_var(cpu_hw_events);
  337. }
  338. static void fsl_emb_pmu_start(struct perf_event *event, int ef_flags)
  339. {
  340. unsigned long flags;
  341. unsigned long val;
  342. s64 left;
  343. if (event->hw.idx < 0 || !event->hw.sample_period)
  344. return;
  345. if (!(event->hw.state & PERF_HES_STOPPED))
  346. return;
  347. if (ef_flags & PERF_EF_RELOAD)
  348. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  349. local_irq_save(flags);
  350. perf_pmu_disable(event->pmu);
  351. event->hw.state = 0;
  352. left = local64_read(&event->hw.period_left);
  353. val = 0;
  354. if (left < 0x80000000L)
  355. val = 0x80000000L - left;
  356. write_pmc(event->hw.idx, val);
  357. perf_event_update_userpage(event);
  358. perf_pmu_enable(event->pmu);
  359. local_irq_restore(flags);
  360. }
  361. static void fsl_emb_pmu_stop(struct perf_event *event, int ef_flags)
  362. {
  363. unsigned long flags;
  364. if (event->hw.idx < 0 || !event->hw.sample_period)
  365. return;
  366. if (event->hw.state & PERF_HES_STOPPED)
  367. return;
  368. local_irq_save(flags);
  369. perf_pmu_disable(event->pmu);
  370. fsl_emb_pmu_read(event);
  371. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  372. write_pmc(event->hw.idx, 0);
  373. perf_event_update_userpage(event);
  374. perf_pmu_enable(event->pmu);
  375. local_irq_restore(flags);
  376. }
  377. /*
  378. * Release the PMU if this is the last perf_event.
  379. */
  380. static void hw_perf_event_destroy(struct perf_event *event)
  381. {
  382. if (!atomic_add_unless(&num_events, -1, 1)) {
  383. mutex_lock(&pmc_reserve_mutex);
  384. if (atomic_dec_return(&num_events) == 0)
  385. release_pmc_hardware();
  386. mutex_unlock(&pmc_reserve_mutex);
  387. }
  388. }
  389. /*
  390. * Translate a generic cache event_id config to a raw event_id code.
  391. */
  392. static int hw_perf_cache_event(u64 config, u64 *eventp)
  393. {
  394. unsigned long type, op, result;
  395. int ev;
  396. if (!ppmu->cache_events)
  397. return -EINVAL;
  398. /* unpack config */
  399. type = config & 0xff;
  400. op = (config >> 8) & 0xff;
  401. result = (config >> 16) & 0xff;
  402. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  403. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  404. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  405. return -EINVAL;
  406. ev = (*ppmu->cache_events)[type][op][result];
  407. if (ev == 0)
  408. return -EOPNOTSUPP;
  409. if (ev == -1)
  410. return -EINVAL;
  411. *eventp = ev;
  412. return 0;
  413. }
  414. static int fsl_emb_pmu_event_init(struct perf_event *event)
  415. {
  416. u64 ev;
  417. struct perf_event *events[MAX_HWEVENTS];
  418. int n;
  419. int err;
  420. int num_restricted;
  421. int i;
  422. if (ppmu->n_counter > MAX_HWEVENTS) {
  423. WARN(1, "No. of perf counters (%d) is higher than max array size(%d)\n",
  424. ppmu->n_counter, MAX_HWEVENTS);
  425. ppmu->n_counter = MAX_HWEVENTS;
  426. }
  427. switch (event->attr.type) {
  428. case PERF_TYPE_HARDWARE:
  429. ev = event->attr.config;
  430. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  431. return -EOPNOTSUPP;
  432. ev = ppmu->generic_events[ev];
  433. break;
  434. case PERF_TYPE_HW_CACHE:
  435. err = hw_perf_cache_event(event->attr.config, &ev);
  436. if (err)
  437. return err;
  438. break;
  439. case PERF_TYPE_RAW:
  440. ev = event->attr.config;
  441. break;
  442. default:
  443. return -ENOENT;
  444. }
  445. event->hw.config = ppmu->xlate_event(ev);
  446. if (!(event->hw.config & FSL_EMB_EVENT_VALID))
  447. return -EINVAL;
  448. /*
  449. * If this is in a group, check if it can go on with all the
  450. * other hardware events in the group. We assume the event
  451. * hasn't been linked into its leader's sibling list at this point.
  452. */
  453. n = 0;
  454. if (event->group_leader != event) {
  455. n = collect_events(event->group_leader,
  456. ppmu->n_counter - 1, events);
  457. if (n < 0)
  458. return -EINVAL;
  459. }
  460. if (event->hw.config & FSL_EMB_EVENT_RESTRICTED) {
  461. num_restricted = 0;
  462. for (i = 0; i < n; i++) {
  463. if (events[i]->hw.config & FSL_EMB_EVENT_RESTRICTED)
  464. num_restricted++;
  465. }
  466. if (num_restricted >= ppmu->n_restricted)
  467. return -EINVAL;
  468. }
  469. event->hw.idx = -1;
  470. event->hw.config_base = PMLCA_CE | PMLCA_FCM1 |
  471. (u32)((ev << 16) & PMLCA_EVENT_MASK);
  472. if (event->attr.exclude_user)
  473. event->hw.config_base |= PMLCA_FCU;
  474. if (event->attr.exclude_kernel)
  475. event->hw.config_base |= PMLCA_FCS;
  476. if (event->attr.exclude_idle)
  477. return -ENOTSUPP;
  478. event->hw.last_period = event->hw.sample_period;
  479. local64_set(&event->hw.period_left, event->hw.last_period);
  480. /*
  481. * See if we need to reserve the PMU.
  482. * If no events are currently in use, then we have to take a
  483. * mutex to ensure that we don't race with another task doing
  484. * reserve_pmc_hardware or release_pmc_hardware.
  485. */
  486. err = 0;
  487. if (!atomic_inc_not_zero(&num_events)) {
  488. mutex_lock(&pmc_reserve_mutex);
  489. if (atomic_read(&num_events) == 0 &&
  490. reserve_pmc_hardware(perf_event_interrupt))
  491. err = -EBUSY;
  492. else
  493. atomic_inc(&num_events);
  494. mutex_unlock(&pmc_reserve_mutex);
  495. mtpmr(PMRN_PMGC0, PMGC0_FAC);
  496. isync();
  497. }
  498. event->destroy = hw_perf_event_destroy;
  499. return err;
  500. }
  501. static struct pmu fsl_emb_pmu = {
  502. .pmu_enable = fsl_emb_pmu_enable,
  503. .pmu_disable = fsl_emb_pmu_disable,
  504. .event_init = fsl_emb_pmu_event_init,
  505. .add = fsl_emb_pmu_add,
  506. .del = fsl_emb_pmu_del,
  507. .start = fsl_emb_pmu_start,
  508. .stop = fsl_emb_pmu_stop,
  509. .read = fsl_emb_pmu_read,
  510. };
  511. /*
  512. * A counter has overflowed; update its count and record
  513. * things if requested. Note that interrupts are hard-disabled
  514. * here so there is no possibility of being interrupted.
  515. */
  516. static void record_and_restart(struct perf_event *event, unsigned long val,
  517. struct pt_regs *regs)
  518. {
  519. u64 period = event->hw.sample_period;
  520. s64 prev, delta, left;
  521. int record = 0;
  522. if (event->hw.state & PERF_HES_STOPPED) {
  523. write_pmc(event->hw.idx, 0);
  524. return;
  525. }
  526. /* we don't have to worry about interrupts here */
  527. prev = local64_read(&event->hw.prev_count);
  528. delta = (val - prev) & 0xfffffffful;
  529. local64_add(delta, &event->count);
  530. /*
  531. * See if the total period for this event has expired,
  532. * and update for the next period.
  533. */
  534. val = 0;
  535. left = local64_read(&event->hw.period_left) - delta;
  536. if (period) {
  537. if (left <= 0) {
  538. left += period;
  539. if (left <= 0)
  540. left = period;
  541. record = 1;
  542. event->hw.last_period = event->hw.sample_period;
  543. }
  544. if (left < 0x80000000LL)
  545. val = 0x80000000LL - left;
  546. }
  547. write_pmc(event->hw.idx, val);
  548. local64_set(&event->hw.prev_count, val);
  549. local64_set(&event->hw.period_left, left);
  550. perf_event_update_userpage(event);
  551. /*
  552. * Finally record data if requested.
  553. */
  554. if (record) {
  555. struct perf_sample_data data;
  556. perf_sample_data_init(&data, 0, event->hw.last_period);
  557. if (perf_event_overflow(event, &data, regs))
  558. fsl_emb_pmu_stop(event, 0);
  559. }
  560. }
  561. static void perf_event_interrupt(struct pt_regs *regs)
  562. {
  563. int i;
  564. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  565. struct perf_event *event;
  566. unsigned long val;
  567. int found = 0;
  568. int nmi;
  569. nmi = perf_intr_is_nmi(regs);
  570. if (nmi)
  571. nmi_enter();
  572. else
  573. irq_enter();
  574. for (i = 0; i < ppmu->n_counter; ++i) {
  575. event = cpuhw->event[i];
  576. val = read_pmc(i);
  577. if ((int)val < 0) {
  578. if (event) {
  579. /* event has overflowed */
  580. found = 1;
  581. record_and_restart(event, val, regs);
  582. } else {
  583. /*
  584. * Disabled counter is negative,
  585. * reset it just in case.
  586. */
  587. write_pmc(i, 0);
  588. }
  589. }
  590. }
  591. /* PMM will keep counters frozen until we return from the interrupt. */
  592. mtmsr(mfmsr() | MSR_PMM);
  593. mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
  594. isync();
  595. if (nmi)
  596. nmi_exit();
  597. else
  598. irq_exit();
  599. }
  600. void hw_perf_event_setup(int cpu)
  601. {
  602. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  603. memset(cpuhw, 0, sizeof(*cpuhw));
  604. }
  605. int register_fsl_emb_pmu(struct fsl_emb_pmu *pmu)
  606. {
  607. if (ppmu)
  608. return -EBUSY; /* something's already registered */
  609. ppmu = pmu;
  610. pr_info("%s performance monitor hardware support registered\n",
  611. pmu->name);
  612. perf_pmu_register(&fsl_emb_pmu, "cpu", PERF_TYPE_RAW);
  613. return 0;
  614. }