core-book3s.c 44 KB

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