core-book3s.c 37 KB

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