core-book3s.c 36 KB

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