core-book3s.c 44 KB

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