perf_event.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * Performance event 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_event.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. #include <asm/ptrace.h>
  21. struct cpu_hw_events {
  22. int n_events;
  23. int n_percpu;
  24. int disabled;
  25. int n_added;
  26. int n_limited;
  27. u8 pmcs_enabled;
  28. struct perf_event *event[MAX_HWEVENTS];
  29. u64 events[MAX_HWEVENTS];
  30. unsigned int flags[MAX_HWEVENTS];
  31. unsigned long mmcr[3];
  32. struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
  33. u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
  34. u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  35. unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  36. unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  37. unsigned int group_flag;
  38. int n_txn_start;
  39. };
  40. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  41. struct power_pmu *ppmu;
  42. /*
  43. * Normally, to ignore kernel events we set the FCS (freeze counters
  44. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  45. * hypervisor bit set in the MSR, or if we are running on a processor
  46. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  47. * then we need to use the FCHV bit to ignore kernel events.
  48. */
  49. static unsigned int freeze_events_kernel = MMCR0_FCS;
  50. /*
  51. * 32-bit doesn't have MMCRA but does have an MMCR2,
  52. * and a few other names are different.
  53. */
  54. #ifdef CONFIG_PPC32
  55. #define MMCR0_FCHV 0
  56. #define MMCR0_PMCjCE MMCR0_PMCnCE
  57. #define SPRN_MMCRA SPRN_MMCR2
  58. #define MMCRA_SAMPLE_ENABLE 0
  59. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  60. {
  61. return 0;
  62. }
  63. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
  64. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  65. {
  66. return 0;
  67. }
  68. static inline void perf_read_regs(struct pt_regs *regs) { }
  69. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  70. {
  71. return 0;
  72. }
  73. #endif /* CONFIG_PPC32 */
  74. /*
  75. * Things that are specific to 64-bit implementations.
  76. */
  77. #ifdef CONFIG_PPC64
  78. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  79. {
  80. unsigned long mmcra = regs->dsisr;
  81. if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
  82. unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
  83. if (slot > 1)
  84. return 4 * (slot - 1);
  85. }
  86. return 0;
  87. }
  88. /*
  89. * The user wants a data address recorded.
  90. * If we're not doing instruction sampling, give them the SDAR
  91. * (sampled data address). If we are doing instruction sampling, then
  92. * only give them the SDAR if it corresponds to the instruction
  93. * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
  94. * bit in MMCRA.
  95. */
  96. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
  97. {
  98. unsigned long mmcra = regs->dsisr;
  99. unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
  100. POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
  101. if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
  102. *addrp = mfspr(SPRN_SDAR);
  103. }
  104. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  105. {
  106. unsigned long mmcra = regs->dsisr;
  107. unsigned long sihv = MMCRA_SIHV;
  108. unsigned long sipr = MMCRA_SIPR;
  109. if (TRAP(regs) != 0xf00)
  110. return 0; /* not a PMU interrupt */
  111. if (ppmu->flags & PPMU_ALT_SIPR) {
  112. sihv = POWER6_MMCRA_SIHV;
  113. sipr = POWER6_MMCRA_SIPR;
  114. }
  115. /* PR has priority over HV, so order below is important */
  116. if (mmcra & sipr)
  117. return PERF_RECORD_MISC_USER;
  118. if ((mmcra & sihv) && (freeze_events_kernel != MMCR0_FCHV))
  119. return PERF_RECORD_MISC_HYPERVISOR;
  120. return PERF_RECORD_MISC_KERNEL;
  121. }
  122. /*
  123. * Overload regs->dsisr to store MMCRA so we only need to read it once
  124. * on each interrupt.
  125. */
  126. static inline void perf_read_regs(struct pt_regs *regs)
  127. {
  128. regs->dsisr = mfspr(SPRN_MMCRA);
  129. }
  130. /*
  131. * If interrupts were soft-disabled when a PMU interrupt occurs, treat
  132. * it as an NMI.
  133. */
  134. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  135. {
  136. return !regs->softe;
  137. }
  138. #endif /* CONFIG_PPC64 */
  139. static void perf_event_interrupt(struct pt_regs *regs);
  140. void perf_event_print_debug(void)
  141. {
  142. }
  143. /*
  144. * Read one performance monitor counter (PMC).
  145. */
  146. static unsigned long read_pmc(int idx)
  147. {
  148. unsigned long val;
  149. switch (idx) {
  150. case 1:
  151. val = mfspr(SPRN_PMC1);
  152. break;
  153. case 2:
  154. val = mfspr(SPRN_PMC2);
  155. break;
  156. case 3:
  157. val = mfspr(SPRN_PMC3);
  158. break;
  159. case 4:
  160. val = mfspr(SPRN_PMC4);
  161. break;
  162. case 5:
  163. val = mfspr(SPRN_PMC5);
  164. break;
  165. case 6:
  166. val = mfspr(SPRN_PMC6);
  167. break;
  168. #ifdef CONFIG_PPC64
  169. case 7:
  170. val = mfspr(SPRN_PMC7);
  171. break;
  172. case 8:
  173. val = mfspr(SPRN_PMC8);
  174. break;
  175. #endif /* CONFIG_PPC64 */
  176. default:
  177. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  178. val = 0;
  179. }
  180. return val;
  181. }
  182. /*
  183. * Write one PMC.
  184. */
  185. static void write_pmc(int idx, unsigned long val)
  186. {
  187. switch (idx) {
  188. case 1:
  189. mtspr(SPRN_PMC1, val);
  190. break;
  191. case 2:
  192. mtspr(SPRN_PMC2, val);
  193. break;
  194. case 3:
  195. mtspr(SPRN_PMC3, val);
  196. break;
  197. case 4:
  198. mtspr(SPRN_PMC4, val);
  199. break;
  200. case 5:
  201. mtspr(SPRN_PMC5, val);
  202. break;
  203. case 6:
  204. mtspr(SPRN_PMC6, val);
  205. break;
  206. #ifdef CONFIG_PPC64
  207. case 7:
  208. mtspr(SPRN_PMC7, val);
  209. break;
  210. case 8:
  211. mtspr(SPRN_PMC8, val);
  212. break;
  213. #endif /* CONFIG_PPC64 */
  214. default:
  215. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  216. }
  217. }
  218. /*
  219. * Check if a set of events can all go on the PMU at once.
  220. * If they can't, this will look at alternative codes for the events
  221. * and see if any combination of alternative codes is feasible.
  222. * The feasible set is returned in event_id[].
  223. */
  224. static int power_check_constraints(struct cpu_hw_events *cpuhw,
  225. u64 event_id[], unsigned int cflags[],
  226. int n_ev)
  227. {
  228. unsigned long mask, value, nv;
  229. unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
  230. int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
  231. int i, j;
  232. unsigned long addf = ppmu->add_fields;
  233. unsigned long tadd = ppmu->test_adder;
  234. if (n_ev > ppmu->n_counter)
  235. return -1;
  236. /* First see if the events will go on as-is */
  237. for (i = 0; i < n_ev; ++i) {
  238. if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
  239. && !ppmu->limited_pmc_event(event_id[i])) {
  240. ppmu->get_alternatives(event_id[i], cflags[i],
  241. cpuhw->alternatives[i]);
  242. event_id[i] = cpuhw->alternatives[i][0];
  243. }
  244. if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
  245. &cpuhw->avalues[i][0]))
  246. return -1;
  247. }
  248. value = mask = 0;
  249. for (i = 0; i < n_ev; ++i) {
  250. nv = (value | cpuhw->avalues[i][0]) +
  251. (value & cpuhw->avalues[i][0] & addf);
  252. if ((((nv + tadd) ^ value) & mask) != 0 ||
  253. (((nv + tadd) ^ cpuhw->avalues[i][0]) &
  254. cpuhw->amasks[i][0]) != 0)
  255. break;
  256. value = nv;
  257. mask |= cpuhw->amasks[i][0];
  258. }
  259. if (i == n_ev)
  260. return 0; /* all OK */
  261. /* doesn't work, gather alternatives... */
  262. if (!ppmu->get_alternatives)
  263. return -1;
  264. for (i = 0; i < n_ev; ++i) {
  265. choice[i] = 0;
  266. n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
  267. cpuhw->alternatives[i]);
  268. for (j = 1; j < n_alt[i]; ++j)
  269. ppmu->get_constraint(cpuhw->alternatives[i][j],
  270. &cpuhw->amasks[i][j],
  271. &cpuhw->avalues[i][j]);
  272. }
  273. /* enumerate all possibilities and see if any will work */
  274. i = 0;
  275. j = -1;
  276. value = mask = nv = 0;
  277. while (i < n_ev) {
  278. if (j >= 0) {
  279. /* we're backtracking, restore context */
  280. value = svalues[i];
  281. mask = smasks[i];
  282. j = choice[i];
  283. }
  284. /*
  285. * See if any alternative k for event_id i,
  286. * where k > j, will satisfy the constraints.
  287. */
  288. while (++j < n_alt[i]) {
  289. nv = (value | cpuhw->avalues[i][j]) +
  290. (value & cpuhw->avalues[i][j] & addf);
  291. if ((((nv + tadd) ^ value) & mask) == 0 &&
  292. (((nv + tadd) ^ cpuhw->avalues[i][j])
  293. & cpuhw->amasks[i][j]) == 0)
  294. break;
  295. }
  296. if (j >= n_alt[i]) {
  297. /*
  298. * No feasible alternative, backtrack
  299. * to event_id i-1 and continue enumerating its
  300. * alternatives from where we got up to.
  301. */
  302. if (--i < 0)
  303. return -1;
  304. } else {
  305. /*
  306. * Found a feasible alternative for event_id i,
  307. * remember where we got up to with this event_id,
  308. * go on to the next event_id, and start with
  309. * the first alternative for it.
  310. */
  311. choice[i] = j;
  312. svalues[i] = value;
  313. smasks[i] = mask;
  314. value = nv;
  315. mask |= cpuhw->amasks[i][j];
  316. ++i;
  317. j = -1;
  318. }
  319. }
  320. /* OK, we have a feasible combination, tell the caller the solution */
  321. for (i = 0; i < n_ev; ++i)
  322. event_id[i] = cpuhw->alternatives[i][choice[i]];
  323. return 0;
  324. }
  325. /*
  326. * Check if newly-added events have consistent settings for
  327. * exclude_{user,kernel,hv} with each other and any previously
  328. * added events.
  329. */
  330. static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
  331. int n_prev, int n_new)
  332. {
  333. int eu = 0, ek = 0, eh = 0;
  334. int i, n, first;
  335. struct perf_event *event;
  336. n = n_prev + n_new;
  337. if (n <= 1)
  338. return 0;
  339. first = 1;
  340. for (i = 0; i < n; ++i) {
  341. if (cflags[i] & PPMU_LIMITED_PMC_OK) {
  342. cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
  343. continue;
  344. }
  345. event = ctrs[i];
  346. if (first) {
  347. eu = event->attr.exclude_user;
  348. ek = event->attr.exclude_kernel;
  349. eh = event->attr.exclude_hv;
  350. first = 0;
  351. } else if (event->attr.exclude_user != eu ||
  352. event->attr.exclude_kernel != ek ||
  353. event->attr.exclude_hv != eh) {
  354. return -EAGAIN;
  355. }
  356. }
  357. if (eu || ek || eh)
  358. for (i = 0; i < n; ++i)
  359. if (cflags[i] & PPMU_LIMITED_PMC_OK)
  360. cflags[i] |= PPMU_LIMITED_PMC_REQD;
  361. return 0;
  362. }
  363. static u64 check_and_compute_delta(u64 prev, u64 val)
  364. {
  365. u64 delta = (val - prev) & 0xfffffffful;
  366. /*
  367. * POWER7 can roll back counter values, if the new value is smaller
  368. * than the previous value it will cause the delta and the counter to
  369. * have bogus values unless we rolled a counter over. If a coutner is
  370. * rolled back, it will be smaller, but within 256, which is the maximum
  371. * number of events to rollback at once. If we dectect a rollback
  372. * return 0. This can lead to a small lack of precision in the
  373. * counters.
  374. */
  375. if (prev > val && (prev - val) < 256)
  376. delta = 0;
  377. return delta;
  378. }
  379. static void power_pmu_read(struct perf_event *event)
  380. {
  381. s64 val, delta, prev;
  382. if (event->hw.state & PERF_HES_STOPPED)
  383. return;
  384. if (!event->hw.idx)
  385. return;
  386. /*
  387. * Performance monitor interrupts come even when interrupts
  388. * are soft-disabled, as long as interrupts are hard-enabled.
  389. * Therefore we treat them like NMIs.
  390. */
  391. do {
  392. prev = local64_read(&event->hw.prev_count);
  393. barrier();
  394. val = read_pmc(event->hw.idx);
  395. delta = check_and_compute_delta(prev, val);
  396. if (!delta)
  397. return;
  398. } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  399. local64_add(delta, &event->count);
  400. local64_sub(delta, &event->hw.period_left);
  401. }
  402. /*
  403. * On some machines, PMC5 and PMC6 can't be written, don't respect
  404. * the freeze conditions, and don't generate interrupts. This tells
  405. * us if `event' is using such a PMC.
  406. */
  407. static int is_limited_pmc(int pmcnum)
  408. {
  409. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  410. && (pmcnum == 5 || pmcnum == 6);
  411. }
  412. static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
  413. unsigned long pmc5, unsigned long pmc6)
  414. {
  415. struct perf_event *event;
  416. u64 val, prev, delta;
  417. int i;
  418. for (i = 0; i < cpuhw->n_limited; ++i) {
  419. event = cpuhw->limited_counter[i];
  420. if (!event->hw.idx)
  421. continue;
  422. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  423. prev = local64_read(&event->hw.prev_count);
  424. event->hw.idx = 0;
  425. delta = check_and_compute_delta(prev, val);
  426. if (delta)
  427. local64_add(delta, &event->count);
  428. }
  429. }
  430. static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
  431. unsigned long pmc5, unsigned long pmc6)
  432. {
  433. struct perf_event *event;
  434. u64 val, prev;
  435. int i;
  436. for (i = 0; i < cpuhw->n_limited; ++i) {
  437. event = cpuhw->limited_counter[i];
  438. event->hw.idx = cpuhw->limited_hwidx[i];
  439. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  440. prev = local64_read(&event->hw.prev_count);
  441. if (check_and_compute_delta(prev, val))
  442. local64_set(&event->hw.prev_count, val);
  443. perf_event_update_userpage(event);
  444. }
  445. }
  446. /*
  447. * Since limited events don't respect the freeze conditions, we
  448. * have to read them immediately after freezing or unfreezing the
  449. * other events. We try to keep the values from the limited
  450. * events as consistent as possible by keeping the delay (in
  451. * cycles and instructions) between freezing/unfreezing and reading
  452. * the limited events as small and consistent as possible.
  453. * Therefore, if any limited events are in use, we read them
  454. * both, and always in the same order, to minimize variability,
  455. * and do it inside the same asm that writes MMCR0.
  456. */
  457. static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
  458. {
  459. unsigned long pmc5, pmc6;
  460. if (!cpuhw->n_limited) {
  461. mtspr(SPRN_MMCR0, mmcr0);
  462. return;
  463. }
  464. /*
  465. * Write MMCR0, then read PMC5 and PMC6 immediately.
  466. * To ensure we don't get a performance monitor interrupt
  467. * between writing MMCR0 and freezing/thawing the limited
  468. * events, we first write MMCR0 with the event overflow
  469. * interrupt enable bits turned off.
  470. */
  471. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  472. : "=&r" (pmc5), "=&r" (pmc6)
  473. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  474. "i" (SPRN_MMCR0),
  475. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  476. if (mmcr0 & MMCR0_FC)
  477. freeze_limited_counters(cpuhw, pmc5, pmc6);
  478. else
  479. thaw_limited_counters(cpuhw, pmc5, pmc6);
  480. /*
  481. * Write the full MMCR0 including the event overflow interrupt
  482. * enable bits, if necessary.
  483. */
  484. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  485. mtspr(SPRN_MMCR0, mmcr0);
  486. }
  487. /*
  488. * Disable all events to prevent PMU interrupts and to allow
  489. * events to be added or removed.
  490. */
  491. static void power_pmu_disable(struct pmu *pmu)
  492. {
  493. struct cpu_hw_events *cpuhw;
  494. unsigned long flags;
  495. if (!ppmu)
  496. return;
  497. local_irq_save(flags);
  498. cpuhw = &__get_cpu_var(cpu_hw_events);
  499. if (!cpuhw->disabled) {
  500. cpuhw->disabled = 1;
  501. cpuhw->n_added = 0;
  502. /*
  503. * Check if we ever enabled the PMU on this cpu.
  504. */
  505. if (!cpuhw->pmcs_enabled) {
  506. ppc_enable_pmcs();
  507. cpuhw->pmcs_enabled = 1;
  508. }
  509. /*
  510. * Disable instruction sampling if it was enabled
  511. */
  512. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  513. mtspr(SPRN_MMCRA,
  514. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  515. mb();
  516. }
  517. /*
  518. * Set the 'freeze counters' bit.
  519. * The barrier is to make sure the mtspr has been
  520. * executed and the PMU has frozen the events
  521. * before we return.
  522. */
  523. write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
  524. mb();
  525. }
  526. local_irq_restore(flags);
  527. }
  528. /*
  529. * Re-enable all events if disable == 0.
  530. * If we were previously disabled and events were added, then
  531. * put the new config on the PMU.
  532. */
  533. static void power_pmu_enable(struct pmu *pmu)
  534. {
  535. struct perf_event *event;
  536. struct cpu_hw_events *cpuhw;
  537. unsigned long flags;
  538. long i;
  539. unsigned long val;
  540. s64 left;
  541. unsigned int hwc_index[MAX_HWEVENTS];
  542. int n_lim;
  543. int idx;
  544. if (!ppmu)
  545. return;
  546. local_irq_save(flags);
  547. cpuhw = &__get_cpu_var(cpu_hw_events);
  548. if (!cpuhw->disabled) {
  549. local_irq_restore(flags);
  550. return;
  551. }
  552. cpuhw->disabled = 0;
  553. /*
  554. * If we didn't change anything, or only removed events,
  555. * no need to recalculate MMCR* settings and reset the PMCs.
  556. * Just reenable the PMU with the current MMCR* settings
  557. * (possibly updated for removal of events).
  558. */
  559. if (!cpuhw->n_added) {
  560. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  561. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  562. if (cpuhw->n_events == 0)
  563. ppc_set_pmu_inuse(0);
  564. goto out_enable;
  565. }
  566. /*
  567. * Compute MMCR* values for the new set of events
  568. */
  569. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
  570. cpuhw->mmcr)) {
  571. /* shouldn't ever get here */
  572. printk(KERN_ERR "oops compute_mmcr failed\n");
  573. goto out;
  574. }
  575. /*
  576. * Add in MMCR0 freeze bits corresponding to the
  577. * attr.exclude_* bits for the first event.
  578. * We have already checked that all events have the
  579. * same values for these bits as the first event.
  580. */
  581. event = cpuhw->event[0];
  582. if (event->attr.exclude_user)
  583. cpuhw->mmcr[0] |= MMCR0_FCP;
  584. if (event->attr.exclude_kernel)
  585. cpuhw->mmcr[0] |= freeze_events_kernel;
  586. if (event->attr.exclude_hv)
  587. cpuhw->mmcr[0] |= MMCR0_FCHV;
  588. /*
  589. * Write the new configuration to MMCR* with the freeze
  590. * bit set and set the hardware events to their initial values.
  591. * Then unfreeze the events.
  592. */
  593. ppc_set_pmu_inuse(1);
  594. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  595. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  596. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  597. | MMCR0_FC);
  598. /*
  599. * Read off any pre-existing events that need to move
  600. * to another PMC.
  601. */
  602. for (i = 0; i < cpuhw->n_events; ++i) {
  603. event = cpuhw->event[i];
  604. if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
  605. power_pmu_read(event);
  606. write_pmc(event->hw.idx, 0);
  607. event->hw.idx = 0;
  608. }
  609. }
  610. /*
  611. * Initialize the PMCs for all the new and moved events.
  612. */
  613. cpuhw->n_limited = n_lim = 0;
  614. for (i = 0; i < cpuhw->n_events; ++i) {
  615. event = cpuhw->event[i];
  616. if (event->hw.idx)
  617. continue;
  618. idx = hwc_index[i] + 1;
  619. if (is_limited_pmc(idx)) {
  620. cpuhw->limited_counter[n_lim] = event;
  621. cpuhw->limited_hwidx[n_lim] = idx;
  622. ++n_lim;
  623. continue;
  624. }
  625. val = 0;
  626. if (event->hw.sample_period) {
  627. left = local64_read(&event->hw.period_left);
  628. if (left < 0x80000000L)
  629. val = 0x80000000L - left;
  630. }
  631. local64_set(&event->hw.prev_count, val);
  632. event->hw.idx = idx;
  633. if (event->hw.state & PERF_HES_STOPPED)
  634. val = 0;
  635. write_pmc(idx, val);
  636. perf_event_update_userpage(event);
  637. }
  638. cpuhw->n_limited = n_lim;
  639. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  640. out_enable:
  641. mb();
  642. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  643. /*
  644. * Enable instruction sampling if necessary
  645. */
  646. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  647. mb();
  648. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  649. }
  650. out:
  651. local_irq_restore(flags);
  652. }
  653. static int collect_events(struct perf_event *group, int max_count,
  654. struct perf_event *ctrs[], u64 *events,
  655. unsigned int *flags)
  656. {
  657. int n = 0;
  658. struct perf_event *event;
  659. if (!is_software_event(group)) {
  660. if (n >= max_count)
  661. return -1;
  662. ctrs[n] = group;
  663. flags[n] = group->hw.event_base;
  664. events[n++] = group->hw.config;
  665. }
  666. list_for_each_entry(event, &group->sibling_list, group_entry) {
  667. if (!is_software_event(event) &&
  668. event->state != PERF_EVENT_STATE_OFF) {
  669. if (n >= max_count)
  670. return -1;
  671. ctrs[n] = event;
  672. flags[n] = event->hw.event_base;
  673. events[n++] = event->hw.config;
  674. }
  675. }
  676. return n;
  677. }
  678. /*
  679. * Add a event to the PMU.
  680. * If all events are not already frozen, then we disable and
  681. * re-enable the PMU in order to get hw_perf_enable to do the
  682. * actual work of reconfiguring the PMU.
  683. */
  684. static int power_pmu_add(struct perf_event *event, int ef_flags)
  685. {
  686. struct cpu_hw_events *cpuhw;
  687. unsigned long flags;
  688. int n0;
  689. int ret = -EAGAIN;
  690. local_irq_save(flags);
  691. perf_pmu_disable(event->pmu);
  692. /*
  693. * Add the event to the list (if there is room)
  694. * and check whether the total set is still feasible.
  695. */
  696. cpuhw = &__get_cpu_var(cpu_hw_events);
  697. n0 = cpuhw->n_events;
  698. if (n0 >= ppmu->n_counter)
  699. goto out;
  700. cpuhw->event[n0] = event;
  701. cpuhw->events[n0] = event->hw.config;
  702. cpuhw->flags[n0] = event->hw.event_base;
  703. if (!(ef_flags & PERF_EF_START))
  704. event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  705. /*
  706. * If group events scheduling transaction was started,
  707. * skip the schedulability test here, it will be performed
  708. * at commit time(->commit_txn) as a whole
  709. */
  710. if (cpuhw->group_flag & PERF_EVENT_TXN)
  711. goto nocheck;
  712. if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
  713. goto out;
  714. if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
  715. goto out;
  716. event->hw.config = cpuhw->events[n0];
  717. nocheck:
  718. ++cpuhw->n_events;
  719. ++cpuhw->n_added;
  720. ret = 0;
  721. out:
  722. perf_pmu_enable(event->pmu);
  723. local_irq_restore(flags);
  724. return ret;
  725. }
  726. /*
  727. * Remove a event from the PMU.
  728. */
  729. static void power_pmu_del(struct perf_event *event, int ef_flags)
  730. {
  731. struct cpu_hw_events *cpuhw;
  732. long i;
  733. unsigned long flags;
  734. local_irq_save(flags);
  735. perf_pmu_disable(event->pmu);
  736. power_pmu_read(event);
  737. cpuhw = &__get_cpu_var(cpu_hw_events);
  738. for (i = 0; i < cpuhw->n_events; ++i) {
  739. if (event == cpuhw->event[i]) {
  740. while (++i < cpuhw->n_events) {
  741. cpuhw->event[i-1] = cpuhw->event[i];
  742. cpuhw->events[i-1] = cpuhw->events[i];
  743. cpuhw->flags[i-1] = cpuhw->flags[i];
  744. }
  745. --cpuhw->n_events;
  746. ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
  747. if (event->hw.idx) {
  748. write_pmc(event->hw.idx, 0);
  749. event->hw.idx = 0;
  750. }
  751. perf_event_update_userpage(event);
  752. break;
  753. }
  754. }
  755. for (i = 0; i < cpuhw->n_limited; ++i)
  756. if (event == cpuhw->limited_counter[i])
  757. break;
  758. if (i < cpuhw->n_limited) {
  759. while (++i < cpuhw->n_limited) {
  760. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  761. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  762. }
  763. --cpuhw->n_limited;
  764. }
  765. if (cpuhw->n_events == 0) {
  766. /* disable exceptions if no events are running */
  767. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  768. }
  769. perf_pmu_enable(event->pmu);
  770. local_irq_restore(flags);
  771. }
  772. /*
  773. * POWER-PMU does not support disabling individual counters, hence
  774. * program their cycle counter to their max value and ignore the interrupts.
  775. */
  776. static void power_pmu_start(struct perf_event *event, int ef_flags)
  777. {
  778. unsigned long flags;
  779. s64 left;
  780. if (!event->hw.idx || !event->hw.sample_period)
  781. return;
  782. if (!(event->hw.state & PERF_HES_STOPPED))
  783. return;
  784. if (ef_flags & PERF_EF_RELOAD)
  785. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  786. local_irq_save(flags);
  787. perf_pmu_disable(event->pmu);
  788. event->hw.state = 0;
  789. left = local64_read(&event->hw.period_left);
  790. write_pmc(event->hw.idx, left);
  791. perf_event_update_userpage(event);
  792. perf_pmu_enable(event->pmu);
  793. local_irq_restore(flags);
  794. }
  795. static void power_pmu_stop(struct perf_event *event, int ef_flags)
  796. {
  797. unsigned long flags;
  798. if (!event->hw.idx || !event->hw.sample_period)
  799. return;
  800. if (event->hw.state & PERF_HES_STOPPED)
  801. return;
  802. local_irq_save(flags);
  803. perf_pmu_disable(event->pmu);
  804. power_pmu_read(event);
  805. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  806. write_pmc(event->hw.idx, 0);
  807. perf_event_update_userpage(event);
  808. perf_pmu_enable(event->pmu);
  809. local_irq_restore(flags);
  810. }
  811. /*
  812. * Start group events scheduling transaction
  813. * Set the flag to make pmu::enable() not perform the
  814. * schedulability test, it will be performed at commit time
  815. */
  816. void power_pmu_start_txn(struct pmu *pmu)
  817. {
  818. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  819. perf_pmu_disable(pmu);
  820. cpuhw->group_flag |= PERF_EVENT_TXN;
  821. cpuhw->n_txn_start = cpuhw->n_events;
  822. }
  823. /*
  824. * Stop group events scheduling transaction
  825. * Clear the flag and pmu::enable() will perform the
  826. * schedulability test.
  827. */
  828. void power_pmu_cancel_txn(struct pmu *pmu)
  829. {
  830. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  831. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  832. perf_pmu_enable(pmu);
  833. }
  834. /*
  835. * Commit group events scheduling transaction
  836. * Perform the group schedulability test as a whole
  837. * Return 0 if success
  838. */
  839. int power_pmu_commit_txn(struct pmu *pmu)
  840. {
  841. struct cpu_hw_events *cpuhw;
  842. long i, n;
  843. if (!ppmu)
  844. return -EAGAIN;
  845. cpuhw = &__get_cpu_var(cpu_hw_events);
  846. n = cpuhw->n_events;
  847. if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
  848. return -EAGAIN;
  849. i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
  850. if (i < 0)
  851. return -EAGAIN;
  852. for (i = cpuhw->n_txn_start; i < n; ++i)
  853. cpuhw->event[i]->hw.config = cpuhw->events[i];
  854. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  855. perf_pmu_enable(pmu);
  856. return 0;
  857. }
  858. /*
  859. * Return 1 if we might be able to put event on a limited PMC,
  860. * or 0 if not.
  861. * A event can only go on a limited PMC if it counts something
  862. * that a limited PMC can count, doesn't require interrupts, and
  863. * doesn't exclude any processor mode.
  864. */
  865. static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
  866. unsigned int flags)
  867. {
  868. int n;
  869. u64 alt[MAX_EVENT_ALTERNATIVES];
  870. if (event->attr.exclude_user
  871. || event->attr.exclude_kernel
  872. || event->attr.exclude_hv
  873. || event->attr.sample_period)
  874. return 0;
  875. if (ppmu->limited_pmc_event(ev))
  876. return 1;
  877. /*
  878. * The requested event_id isn't on a limited PMC already;
  879. * see if any alternative code goes on a limited PMC.
  880. */
  881. if (!ppmu->get_alternatives)
  882. return 0;
  883. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  884. n = ppmu->get_alternatives(ev, flags, alt);
  885. return n > 0;
  886. }
  887. /*
  888. * Find an alternative event_id that goes on a normal PMC, if possible,
  889. * and return the event_id code, or 0 if there is no such alternative.
  890. * (Note: event_id code 0 is "don't count" on all machines.)
  891. */
  892. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  893. {
  894. u64 alt[MAX_EVENT_ALTERNATIVES];
  895. int n;
  896. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  897. n = ppmu->get_alternatives(ev, flags, alt);
  898. if (!n)
  899. return 0;
  900. return alt[0];
  901. }
  902. /* Number of perf_events counting hardware events */
  903. static atomic_t num_events;
  904. /* Used to avoid races in calling reserve/release_pmc_hardware */
  905. static DEFINE_MUTEX(pmc_reserve_mutex);
  906. /*
  907. * Release the PMU if this is the last perf_event.
  908. */
  909. static void hw_perf_event_destroy(struct perf_event *event)
  910. {
  911. if (!atomic_add_unless(&num_events, -1, 1)) {
  912. mutex_lock(&pmc_reserve_mutex);
  913. if (atomic_dec_return(&num_events) == 0)
  914. release_pmc_hardware();
  915. mutex_unlock(&pmc_reserve_mutex);
  916. }
  917. }
  918. /*
  919. * Translate a generic cache event_id config to a raw event_id code.
  920. */
  921. static int hw_perf_cache_event(u64 config, u64 *eventp)
  922. {
  923. unsigned long type, op, result;
  924. int ev;
  925. if (!ppmu->cache_events)
  926. return -EINVAL;
  927. /* unpack config */
  928. type = config & 0xff;
  929. op = (config >> 8) & 0xff;
  930. result = (config >> 16) & 0xff;
  931. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  932. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  933. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  934. return -EINVAL;
  935. ev = (*ppmu->cache_events)[type][op][result];
  936. if (ev == 0)
  937. return -EOPNOTSUPP;
  938. if (ev == -1)
  939. return -EINVAL;
  940. *eventp = ev;
  941. return 0;
  942. }
  943. static int power_pmu_event_init(struct perf_event *event)
  944. {
  945. u64 ev;
  946. unsigned long flags;
  947. struct perf_event *ctrs[MAX_HWEVENTS];
  948. u64 events[MAX_HWEVENTS];
  949. unsigned int cflags[MAX_HWEVENTS];
  950. int n;
  951. int err;
  952. struct cpu_hw_events *cpuhw;
  953. if (!ppmu)
  954. return -ENOENT;
  955. switch (event->attr.type) {
  956. case PERF_TYPE_HARDWARE:
  957. ev = event->attr.config;
  958. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  959. return -EOPNOTSUPP;
  960. ev = ppmu->generic_events[ev];
  961. break;
  962. case PERF_TYPE_HW_CACHE:
  963. err = hw_perf_cache_event(event->attr.config, &ev);
  964. if (err)
  965. return err;
  966. break;
  967. case PERF_TYPE_RAW:
  968. ev = event->attr.config;
  969. break;
  970. default:
  971. return -ENOENT;
  972. }
  973. event->hw.config_base = ev;
  974. event->hw.idx = 0;
  975. /*
  976. * If we are not running on a hypervisor, force the
  977. * exclude_hv bit to 0 so that we don't care what
  978. * the user set it to.
  979. */
  980. if (!firmware_has_feature(FW_FEATURE_LPAR))
  981. event->attr.exclude_hv = 0;
  982. /*
  983. * If this is a per-task event, then we can use
  984. * PM_RUN_* events interchangeably with their non RUN_*
  985. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  986. * XXX we should check if the task is an idle task.
  987. */
  988. flags = 0;
  989. if (event->attach_state & PERF_ATTACH_TASK)
  990. flags |= PPMU_ONLY_COUNT_RUN;
  991. /*
  992. * If this machine has limited events, check whether this
  993. * event_id could go on a limited event.
  994. */
  995. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  996. if (can_go_on_limited_pmc(event, ev, flags)) {
  997. flags |= PPMU_LIMITED_PMC_OK;
  998. } else if (ppmu->limited_pmc_event(ev)) {
  999. /*
  1000. * The requested event_id is on a limited PMC,
  1001. * but we can't use a limited PMC; see if any
  1002. * alternative goes on a normal PMC.
  1003. */
  1004. ev = normal_pmc_alternative(ev, flags);
  1005. if (!ev)
  1006. return -EINVAL;
  1007. }
  1008. }
  1009. /*
  1010. * If this is in a group, check if it can go on with all the
  1011. * other hardware events in the group. We assume the event
  1012. * hasn't been linked into its leader's sibling list at this point.
  1013. */
  1014. n = 0;
  1015. if (event->group_leader != event) {
  1016. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  1017. ctrs, events, cflags);
  1018. if (n < 0)
  1019. return -EINVAL;
  1020. }
  1021. events[n] = ev;
  1022. ctrs[n] = event;
  1023. cflags[n] = flags;
  1024. if (check_excludes(ctrs, cflags, n, 1))
  1025. return -EINVAL;
  1026. cpuhw = &get_cpu_var(cpu_hw_events);
  1027. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  1028. put_cpu_var(cpu_hw_events);
  1029. if (err)
  1030. return -EINVAL;
  1031. event->hw.config = events[n];
  1032. event->hw.event_base = cflags[n];
  1033. event->hw.last_period = event->hw.sample_period;
  1034. local64_set(&event->hw.period_left, event->hw.last_period);
  1035. /*
  1036. * See if we need to reserve the PMU.
  1037. * If no events are currently in use, then we have to take a
  1038. * mutex to ensure that we don't race with another task doing
  1039. * reserve_pmc_hardware or release_pmc_hardware.
  1040. */
  1041. err = 0;
  1042. if (!atomic_inc_not_zero(&num_events)) {
  1043. mutex_lock(&pmc_reserve_mutex);
  1044. if (atomic_read(&num_events) == 0 &&
  1045. reserve_pmc_hardware(perf_event_interrupt))
  1046. err = -EBUSY;
  1047. else
  1048. atomic_inc(&num_events);
  1049. mutex_unlock(&pmc_reserve_mutex);
  1050. }
  1051. event->destroy = hw_perf_event_destroy;
  1052. return err;
  1053. }
  1054. struct pmu power_pmu = {
  1055. .pmu_enable = power_pmu_enable,
  1056. .pmu_disable = power_pmu_disable,
  1057. .event_init = power_pmu_event_init,
  1058. .add = power_pmu_add,
  1059. .del = power_pmu_del,
  1060. .start = power_pmu_start,
  1061. .stop = power_pmu_stop,
  1062. .read = power_pmu_read,
  1063. .start_txn = power_pmu_start_txn,
  1064. .cancel_txn = power_pmu_cancel_txn,
  1065. .commit_txn = power_pmu_commit_txn,
  1066. };
  1067. /*
  1068. * A counter has overflowed; update its count and record
  1069. * things if requested. Note that interrupts are hard-disabled
  1070. * here so there is no possibility of being interrupted.
  1071. */
  1072. static void record_and_restart(struct perf_event *event, unsigned long val,
  1073. struct pt_regs *regs)
  1074. {
  1075. u64 period = event->hw.sample_period;
  1076. s64 prev, delta, left;
  1077. int record = 0;
  1078. if (event->hw.state & PERF_HES_STOPPED) {
  1079. write_pmc(event->hw.idx, 0);
  1080. return;
  1081. }
  1082. /* we don't have to worry about interrupts here */
  1083. prev = local64_read(&event->hw.prev_count);
  1084. delta = check_and_compute_delta(prev, val);
  1085. local64_add(delta, &event->count);
  1086. /*
  1087. * See if the total period for this event has expired,
  1088. * and update for the next period.
  1089. */
  1090. val = 0;
  1091. left = local64_read(&event->hw.period_left) - delta;
  1092. if (period) {
  1093. if (left <= 0) {
  1094. left += period;
  1095. if (left <= 0)
  1096. left = period;
  1097. record = 1;
  1098. event->hw.last_period = event->hw.sample_period;
  1099. }
  1100. if (left < 0x80000000LL)
  1101. val = 0x80000000LL - left;
  1102. }
  1103. write_pmc(event->hw.idx, val);
  1104. local64_set(&event->hw.prev_count, val);
  1105. local64_set(&event->hw.period_left, left);
  1106. perf_event_update_userpage(event);
  1107. /*
  1108. * Finally record data if requested.
  1109. */
  1110. if (record) {
  1111. struct perf_sample_data data;
  1112. perf_sample_data_init(&data, ~0ULL);
  1113. data.period = event->hw.last_period;
  1114. if (event->attr.sample_type & PERF_SAMPLE_ADDR)
  1115. perf_get_data_addr(regs, &data.addr);
  1116. if (perf_event_overflow(event, &data, regs))
  1117. power_pmu_stop(event, 0);
  1118. }
  1119. }
  1120. /*
  1121. * Called from generic code to get the misc flags (i.e. processor mode)
  1122. * for an event_id.
  1123. */
  1124. unsigned long perf_misc_flags(struct pt_regs *regs)
  1125. {
  1126. u32 flags = perf_get_misc_flags(regs);
  1127. if (flags)
  1128. return flags;
  1129. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1130. PERF_RECORD_MISC_KERNEL;
  1131. }
  1132. /*
  1133. * Called from generic code to get the instruction pointer
  1134. * for an event_id.
  1135. */
  1136. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1137. {
  1138. unsigned long ip;
  1139. if (TRAP(regs) != 0xf00)
  1140. return regs->nip; /* not a PMU interrupt */
  1141. ip = mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1142. return ip;
  1143. }
  1144. static bool pmc_overflow(unsigned long val)
  1145. {
  1146. if ((int)val < 0)
  1147. return true;
  1148. /*
  1149. * Events on POWER7 can roll back if a speculative event doesn't
  1150. * eventually complete. Unfortunately in some rare cases they will
  1151. * raise a performance monitor exception. We need to catch this to
  1152. * ensure we reset the PMC. In all cases the PMC will be 256 or less
  1153. * cycles from overflow.
  1154. *
  1155. * We only do this if the first pass fails to find any overflowing
  1156. * PMCs because a user might set a period of less than 256 and we
  1157. * don't want to mistakenly reset them.
  1158. */
  1159. if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256))
  1160. return true;
  1161. return false;
  1162. }
  1163. /*
  1164. * Performance monitor interrupt stuff
  1165. */
  1166. static void perf_event_interrupt(struct pt_regs *regs)
  1167. {
  1168. int i;
  1169. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1170. struct perf_event *event;
  1171. unsigned long val;
  1172. int found = 0;
  1173. int nmi;
  1174. if (cpuhw->n_limited)
  1175. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1176. mfspr(SPRN_PMC6));
  1177. perf_read_regs(regs);
  1178. nmi = perf_intr_is_nmi(regs);
  1179. if (nmi)
  1180. nmi_enter();
  1181. else
  1182. irq_enter();
  1183. for (i = 0; i < cpuhw->n_events; ++i) {
  1184. event = cpuhw->event[i];
  1185. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1186. continue;
  1187. val = read_pmc(event->hw.idx);
  1188. if ((int)val < 0) {
  1189. /* event has overflowed */
  1190. found = 1;
  1191. record_and_restart(event, val, regs);
  1192. }
  1193. }
  1194. /*
  1195. * In case we didn't find and reset the event that caused
  1196. * the interrupt, scan all events and reset any that are
  1197. * negative, to avoid getting continual interrupts.
  1198. * Any that we processed in the previous loop will not be negative.
  1199. */
  1200. if (!found) {
  1201. for (i = 0; i < ppmu->n_counter; ++i) {
  1202. if (is_limited_pmc(i + 1))
  1203. continue;
  1204. val = read_pmc(i + 1);
  1205. if (pmc_overflow(val))
  1206. write_pmc(i + 1, 0);
  1207. }
  1208. }
  1209. /*
  1210. * Reset MMCR0 to its normal value. This will set PMXE and
  1211. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1212. * and thus allow interrupts to occur again.
  1213. * XXX might want to use MSR.PM to keep the events frozen until
  1214. * we get back out of this interrupt.
  1215. */
  1216. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1217. if (nmi)
  1218. nmi_exit();
  1219. else
  1220. irq_exit();
  1221. }
  1222. static void power_pmu_setup(int cpu)
  1223. {
  1224. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1225. if (!ppmu)
  1226. return;
  1227. memset(cpuhw, 0, sizeof(*cpuhw));
  1228. cpuhw->mmcr[0] = MMCR0_FC;
  1229. }
  1230. static int __cpuinit
  1231. power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1232. {
  1233. unsigned int cpu = (long)hcpu;
  1234. switch (action & ~CPU_TASKS_FROZEN) {
  1235. case CPU_UP_PREPARE:
  1236. power_pmu_setup(cpu);
  1237. break;
  1238. default:
  1239. break;
  1240. }
  1241. return NOTIFY_OK;
  1242. }
  1243. int __cpuinit register_power_pmu(struct power_pmu *pmu)
  1244. {
  1245. if (ppmu)
  1246. return -EBUSY; /* something's already registered */
  1247. ppmu = pmu;
  1248. pr_info("%s performance monitor hardware support registered\n",
  1249. pmu->name);
  1250. #ifdef MSR_HV
  1251. /*
  1252. * Use FCHV to ignore kernel events if MSR.HV is set.
  1253. */
  1254. if (mfmsr() & MSR_HV)
  1255. freeze_events_kernel = MMCR0_FCHV;
  1256. #endif /* CONFIG_PPC64 */
  1257. perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
  1258. perf_cpu_notifier(power_pmu_notifier);
  1259. return 0;
  1260. }