core-book3s.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  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 0xFFFFFFFFFFFFFFFCUL
  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. /*
  38. * The order of the MMCR array is:
  39. * - 64-bit, MMCR0, MMCR1, MMCRA, MMCR2
  40. * - 32-bit, MMCR0, MMCR1, MMCR2
  41. */
  42. unsigned long mmcr[4];
  43. struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
  44. u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
  45. u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  46. unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  47. unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  48. unsigned int txn_flags;
  49. int n_txn_start;
  50. /* BHRB bits */
  51. u64 bhrb_filter; /* BHRB HW branch filter */
  52. unsigned int bhrb_users;
  53. void *bhrb_context;
  54. struct perf_branch_stack bhrb_stack;
  55. struct perf_branch_entry bhrb_entries[BHRB_MAX_ENTRIES];
  56. u64 ic_init;
  57. };
  58. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  59. static struct power_pmu *ppmu;
  60. /*
  61. * Normally, to ignore kernel events we set the FCS (freeze counters
  62. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  63. * hypervisor bit set in the MSR, or if we are running on a processor
  64. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  65. * then we need to use the FCHV bit to ignore kernel events.
  66. */
  67. static unsigned int freeze_events_kernel = MMCR0_FCS;
  68. /*
  69. * 32-bit doesn't have MMCRA but does have an MMCR2,
  70. * and a few other names are different.
  71. */
  72. #ifdef CONFIG_PPC32
  73. #define MMCR0_FCHV 0
  74. #define MMCR0_PMCjCE MMCR0_PMCnCE
  75. #define MMCR0_FC56 0
  76. #define MMCR0_PMAO 0
  77. #define MMCR0_EBE 0
  78. #define MMCR0_BHRBA 0
  79. #define MMCR0_PMCC 0
  80. #define MMCR0_PMCC_U6 0
  81. #define SPRN_MMCRA SPRN_MMCR2
  82. #define MMCRA_SAMPLE_ENABLE 0
  83. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  84. {
  85. return 0;
  86. }
  87. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
  88. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  89. {
  90. return 0;
  91. }
  92. static inline void perf_read_regs(struct pt_regs *regs)
  93. {
  94. regs->result = 0;
  95. }
  96. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  97. {
  98. return 0;
  99. }
  100. static inline int siar_valid(struct pt_regs *regs)
  101. {
  102. return 1;
  103. }
  104. static bool is_ebb_event(struct perf_event *event) { return false; }
  105. static int ebb_event_check(struct perf_event *event) { return 0; }
  106. static void ebb_event_add(struct perf_event *event) { }
  107. static void ebb_switch_out(unsigned long mmcr0) { }
  108. static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
  109. {
  110. return cpuhw->mmcr[0];
  111. }
  112. static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
  113. static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
  114. static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
  115. static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
  116. static void pmao_restore_workaround(bool ebb) { }
  117. #endif /* CONFIG_PPC32 */
  118. static bool regs_use_siar(struct pt_regs *regs)
  119. {
  120. /*
  121. * When we take a performance monitor exception the regs are setup
  122. * using perf_read_regs() which overloads some fields, in particular
  123. * regs->result to tell us whether to use SIAR.
  124. *
  125. * However if the regs are from another exception, eg. a syscall, then
  126. * they have not been setup using perf_read_regs() and so regs->result
  127. * is something random.
  128. */
  129. return ((TRAP(regs) == 0xf00) && regs->result);
  130. }
  131. /*
  132. * Things that are specific to 64-bit implementations.
  133. */
  134. #ifdef CONFIG_PPC64
  135. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  136. {
  137. unsigned long mmcra = regs->dsisr;
  138. if ((ppmu->flags & PPMU_HAS_SSLOT) && (mmcra & MMCRA_SAMPLE_ENABLE)) {
  139. unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
  140. if (slot > 1)
  141. return 4 * (slot - 1);
  142. }
  143. return 0;
  144. }
  145. /*
  146. * The user wants a data address recorded.
  147. * If we're not doing instruction sampling, give them the SDAR
  148. * (sampled data address). If we are doing instruction sampling, then
  149. * only give them the SDAR if it corresponds to the instruction
  150. * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
  151. * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
  152. */
  153. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
  154. {
  155. unsigned long mmcra = regs->dsisr;
  156. bool sdar_valid;
  157. if (ppmu->flags & PPMU_HAS_SIER)
  158. sdar_valid = regs->dar & SIER_SDAR_VALID;
  159. else {
  160. unsigned long sdsync;
  161. if (ppmu->flags & PPMU_SIAR_VALID)
  162. sdsync = POWER7P_MMCRA_SDAR_VALID;
  163. else if (ppmu->flags & PPMU_ALT_SIPR)
  164. sdsync = POWER6_MMCRA_SDSYNC;
  165. else if (ppmu->flags & PPMU_NO_SIAR)
  166. sdsync = MMCRA_SAMPLE_ENABLE;
  167. else
  168. sdsync = MMCRA_SDSYNC;
  169. sdar_valid = mmcra & sdsync;
  170. }
  171. if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
  172. *addrp = mfspr(SPRN_SDAR);
  173. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
  174. is_kernel_addr(mfspr(SPRN_SDAR)))
  175. *addrp = 0;
  176. }
  177. static bool regs_sihv(struct pt_regs *regs)
  178. {
  179. unsigned long sihv = MMCRA_SIHV;
  180. if (ppmu->flags & PPMU_HAS_SIER)
  181. return !!(regs->dar & SIER_SIHV);
  182. if (ppmu->flags & PPMU_ALT_SIPR)
  183. sihv = POWER6_MMCRA_SIHV;
  184. return !!(regs->dsisr & sihv);
  185. }
  186. static bool regs_sipr(struct pt_regs *regs)
  187. {
  188. unsigned long sipr = MMCRA_SIPR;
  189. if (ppmu->flags & PPMU_HAS_SIER)
  190. return !!(regs->dar & SIER_SIPR);
  191. if (ppmu->flags & PPMU_ALT_SIPR)
  192. sipr = POWER6_MMCRA_SIPR;
  193. return !!(regs->dsisr & sipr);
  194. }
  195. static inline u32 perf_flags_from_msr(struct pt_regs *regs)
  196. {
  197. if (regs->msr & MSR_PR)
  198. return PERF_RECORD_MISC_USER;
  199. if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
  200. return PERF_RECORD_MISC_HYPERVISOR;
  201. return PERF_RECORD_MISC_KERNEL;
  202. }
  203. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  204. {
  205. bool use_siar = regs_use_siar(regs);
  206. if (!use_siar)
  207. return perf_flags_from_msr(regs);
  208. /*
  209. * If we don't have flags in MMCRA, rather than using
  210. * the MSR, we intuit the flags from the address in
  211. * SIAR which should give slightly more reliable
  212. * results
  213. */
  214. if (ppmu->flags & PPMU_NO_SIPR) {
  215. unsigned long siar = mfspr(SPRN_SIAR);
  216. if (is_kernel_addr(siar))
  217. return PERF_RECORD_MISC_KERNEL;
  218. return PERF_RECORD_MISC_USER;
  219. }
  220. /* PR has priority over HV, so order below is important */
  221. if (regs_sipr(regs))
  222. return PERF_RECORD_MISC_USER;
  223. if (regs_sihv(regs) && (freeze_events_kernel != MMCR0_FCHV))
  224. return PERF_RECORD_MISC_HYPERVISOR;
  225. return PERF_RECORD_MISC_KERNEL;
  226. }
  227. /*
  228. * Overload regs->dsisr to store MMCRA so we only need to read it once
  229. * on each interrupt.
  230. * Overload regs->dar to store SIER if we have it.
  231. * Overload regs->result to specify whether we should use the MSR (result
  232. * is zero) or the SIAR (result is non zero).
  233. */
  234. static inline void perf_read_regs(struct pt_regs *regs)
  235. {
  236. unsigned long mmcra = mfspr(SPRN_MMCRA);
  237. int marked = mmcra & MMCRA_SAMPLE_ENABLE;
  238. int use_siar;
  239. regs->dsisr = mmcra;
  240. if (ppmu->flags & PPMU_HAS_SIER)
  241. regs->dar = mfspr(SPRN_SIER);
  242. /*
  243. * If this isn't a PMU exception (eg a software event) the SIAR is
  244. * not valid. Use pt_regs.
  245. *
  246. * If it is a marked event use the SIAR.
  247. *
  248. * If the PMU doesn't update the SIAR for non marked events use
  249. * pt_regs.
  250. *
  251. * If the PMU has HV/PR flags then check to see if they
  252. * place the exception in userspace. If so, use pt_regs. In
  253. * continuous sampling mode the SIAR and the PMU exception are
  254. * not synchronised, so they may be many instructions apart.
  255. * This can result in confusing backtraces. We still want
  256. * hypervisor samples as well as samples in the kernel with
  257. * interrupts off hence the userspace check.
  258. */
  259. if (TRAP(regs) != 0xf00)
  260. use_siar = 0;
  261. else if ((ppmu->flags & PPMU_NO_SIAR))
  262. use_siar = 0;
  263. else if (marked)
  264. use_siar = 1;
  265. else if ((ppmu->flags & PPMU_NO_CONT_SAMPLING))
  266. use_siar = 0;
  267. else if (!(ppmu->flags & PPMU_NO_SIPR) && regs_sipr(regs))
  268. use_siar = 0;
  269. else
  270. use_siar = 1;
  271. regs->result = use_siar;
  272. }
  273. /*
  274. * If interrupts were soft-disabled when a PMU interrupt occurs, treat
  275. * it as an NMI.
  276. */
  277. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  278. {
  279. return (regs->softe & IRQS_DISABLED);
  280. }
  281. /*
  282. * On processors like P7+ that have the SIAR-Valid bit, marked instructions
  283. * must be sampled only if the SIAR-valid bit is set.
  284. *
  285. * For unmarked instructions and for processors that don't have the SIAR-Valid
  286. * bit, assume that SIAR is valid.
  287. */
  288. static inline int siar_valid(struct pt_regs *regs)
  289. {
  290. unsigned long mmcra = regs->dsisr;
  291. int marked = mmcra & MMCRA_SAMPLE_ENABLE;
  292. if (marked) {
  293. if (ppmu->flags & PPMU_HAS_SIER)
  294. return regs->dar & SIER_SIAR_VALID;
  295. if (ppmu->flags & PPMU_SIAR_VALID)
  296. return mmcra & POWER7P_MMCRA_SIAR_VALID;
  297. }
  298. return 1;
  299. }
  300. /* Reset all possible BHRB entries */
  301. static void power_pmu_bhrb_reset(void)
  302. {
  303. asm volatile(PPC_CLRBHRB);
  304. }
  305. static void power_pmu_bhrb_enable(struct perf_event *event)
  306. {
  307. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  308. if (!ppmu->bhrb_nr)
  309. return;
  310. /* Clear BHRB if we changed task context to avoid data leaks */
  311. if (event->ctx->task && cpuhw->bhrb_context != event->ctx) {
  312. power_pmu_bhrb_reset();
  313. cpuhw->bhrb_context = event->ctx;
  314. }
  315. cpuhw->bhrb_users++;
  316. perf_sched_cb_inc(event->ctx->pmu);
  317. }
  318. static void power_pmu_bhrb_disable(struct perf_event *event)
  319. {
  320. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  321. if (!ppmu->bhrb_nr)
  322. return;
  323. WARN_ON_ONCE(!cpuhw->bhrb_users);
  324. cpuhw->bhrb_users--;
  325. perf_sched_cb_dec(event->ctx->pmu);
  326. if (!cpuhw->disabled && !cpuhw->bhrb_users) {
  327. /* BHRB cannot be turned off when other
  328. * events are active on the PMU.
  329. */
  330. /* avoid stale pointer */
  331. cpuhw->bhrb_context = NULL;
  332. }
  333. }
  334. /* Called from ctxsw to prevent one process's branch entries to
  335. * mingle with the other process's entries during context switch.
  336. */
  337. static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
  338. {
  339. if (!ppmu->bhrb_nr)
  340. return;
  341. if (sched_in)
  342. power_pmu_bhrb_reset();
  343. }
  344. /* Calculate the to address for a branch */
  345. static __u64 power_pmu_bhrb_to(u64 addr)
  346. {
  347. unsigned int instr;
  348. int ret;
  349. __u64 target;
  350. if (is_kernel_addr(addr)) {
  351. if (probe_kernel_read(&instr, (void *)addr, sizeof(instr)))
  352. return 0;
  353. return branch_target(&instr);
  354. }
  355. /* Userspace: need copy instruction here then translate it */
  356. pagefault_disable();
  357. ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
  358. if (ret) {
  359. pagefault_enable();
  360. return 0;
  361. }
  362. pagefault_enable();
  363. target = branch_target(&instr);
  364. if ((!target) || (instr & BRANCH_ABSOLUTE))
  365. return target;
  366. /* Translate relative branch target from kernel to user address */
  367. return target - (unsigned long)&instr + addr;
  368. }
  369. /* Processing BHRB entries */
  370. static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
  371. {
  372. u64 val;
  373. u64 addr;
  374. int r_index, u_index, pred;
  375. r_index = 0;
  376. u_index = 0;
  377. while (r_index < ppmu->bhrb_nr) {
  378. /* Assembly read function */
  379. val = read_bhrb(r_index++);
  380. if (!val)
  381. /* Terminal marker: End of valid BHRB entries */
  382. break;
  383. else {
  384. addr = val & BHRB_EA;
  385. pred = val & BHRB_PREDICTION;
  386. if (!addr)
  387. /* invalid entry */
  388. continue;
  389. /*
  390. * BHRB rolling buffer could very much contain the kernel
  391. * addresses at this point. Check the privileges before
  392. * exporting it to userspace (avoid exposure of regions
  393. * where we could have speculative execution)
  394. */
  395. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
  396. is_kernel_addr(addr))
  397. continue;
  398. /* Branches are read most recent first (ie. mfbhrb 0 is
  399. * the most recent branch).
  400. * There are two types of valid entries:
  401. * 1) a target entry which is the to address of a
  402. * computed goto like a blr,bctr,btar. The next
  403. * entry read from the bhrb will be branch
  404. * corresponding to this target (ie. the actual
  405. * blr/bctr/btar instruction).
  406. * 2) a from address which is an actual branch. If a
  407. * target entry proceeds this, then this is the
  408. * matching branch for that target. If this is not
  409. * following a target entry, then this is a branch
  410. * where the target is given as an immediate field
  411. * in the instruction (ie. an i or b form branch).
  412. * In this case we need to read the instruction from
  413. * memory to determine the target/to address.
  414. */
  415. if (val & BHRB_TARGET) {
  416. /* Target branches use two entries
  417. * (ie. computed gotos/XL form)
  418. */
  419. cpuhw->bhrb_entries[u_index].to = addr;
  420. cpuhw->bhrb_entries[u_index].mispred = pred;
  421. cpuhw->bhrb_entries[u_index].predicted = ~pred;
  422. /* Get from address in next entry */
  423. val = read_bhrb(r_index++);
  424. addr = val & BHRB_EA;
  425. if (val & BHRB_TARGET) {
  426. /* Shouldn't have two targets in a
  427. row.. Reset index and try again */
  428. r_index--;
  429. addr = 0;
  430. }
  431. cpuhw->bhrb_entries[u_index].from = addr;
  432. } else {
  433. /* Branches to immediate field
  434. (ie I or B form) */
  435. cpuhw->bhrb_entries[u_index].from = addr;
  436. cpuhw->bhrb_entries[u_index].to =
  437. power_pmu_bhrb_to(addr);
  438. cpuhw->bhrb_entries[u_index].mispred = pred;
  439. cpuhw->bhrb_entries[u_index].predicted = ~pred;
  440. }
  441. u_index++;
  442. }
  443. }
  444. cpuhw->bhrb_stack.nr = u_index;
  445. return;
  446. }
  447. static bool is_ebb_event(struct perf_event *event)
  448. {
  449. /*
  450. * This could be a per-PMU callback, but we'd rather avoid the cost. We
  451. * check that the PMU supports EBB, meaning those that don't can still
  452. * use bit 63 of the event code for something else if they wish.
  453. */
  454. return (ppmu->flags & PPMU_ARCH_207S) &&
  455. ((event->attr.config >> PERF_EVENT_CONFIG_EBB_SHIFT) & 1);
  456. }
  457. static int ebb_event_check(struct perf_event *event)
  458. {
  459. struct perf_event *leader = event->group_leader;
  460. /* Event and group leader must agree on EBB */
  461. if (is_ebb_event(leader) != is_ebb_event(event))
  462. return -EINVAL;
  463. if (is_ebb_event(event)) {
  464. if (!(event->attach_state & PERF_ATTACH_TASK))
  465. return -EINVAL;
  466. if (!leader->attr.pinned || !leader->attr.exclusive)
  467. return -EINVAL;
  468. if (event->attr.freq ||
  469. event->attr.inherit ||
  470. event->attr.sample_type ||
  471. event->attr.sample_period ||
  472. event->attr.enable_on_exec)
  473. return -EINVAL;
  474. }
  475. return 0;
  476. }
  477. static void ebb_event_add(struct perf_event *event)
  478. {
  479. if (!is_ebb_event(event) || current->thread.used_ebb)
  480. return;
  481. /*
  482. * IFF this is the first time we've added an EBB event, set
  483. * PMXE in the user MMCR0 so we can detect when it's cleared by
  484. * userspace. We need this so that we can context switch while
  485. * userspace is in the EBB handler (where PMXE is 0).
  486. */
  487. current->thread.used_ebb = 1;
  488. current->thread.mmcr0 |= MMCR0_PMXE;
  489. }
  490. static void ebb_switch_out(unsigned long mmcr0)
  491. {
  492. if (!(mmcr0 & MMCR0_EBE))
  493. return;
  494. current->thread.siar = mfspr(SPRN_SIAR);
  495. current->thread.sier = mfspr(SPRN_SIER);
  496. current->thread.sdar = mfspr(SPRN_SDAR);
  497. current->thread.mmcr0 = mmcr0 & MMCR0_USER_MASK;
  498. current->thread.mmcr2 = mfspr(SPRN_MMCR2) & MMCR2_USER_MASK;
  499. }
  500. static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
  501. {
  502. unsigned long mmcr0 = cpuhw->mmcr[0];
  503. if (!ebb)
  504. goto out;
  505. /* Enable EBB and read/write to all 6 PMCs and BHRB for userspace */
  506. mmcr0 |= MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC_U6;
  507. /*
  508. * Add any bits from the user MMCR0, FC or PMAO. This is compatible
  509. * with pmao_restore_workaround() because we may add PMAO but we never
  510. * clear it here.
  511. */
  512. mmcr0 |= current->thread.mmcr0;
  513. /*
  514. * Be careful not to set PMXE if userspace had it cleared. This is also
  515. * compatible with pmao_restore_workaround() because it has already
  516. * cleared PMXE and we leave PMAO alone.
  517. */
  518. if (!(current->thread.mmcr0 & MMCR0_PMXE))
  519. mmcr0 &= ~MMCR0_PMXE;
  520. mtspr(SPRN_SIAR, current->thread.siar);
  521. mtspr(SPRN_SIER, current->thread.sier);
  522. mtspr(SPRN_SDAR, current->thread.sdar);
  523. /*
  524. * Merge the kernel & user values of MMCR2. The semantics we implement
  525. * are that the user MMCR2 can set bits, ie. cause counters to freeze,
  526. * but not clear bits. If a task wants to be able to clear bits, ie.
  527. * unfreeze counters, it should not set exclude_xxx in its events and
  528. * instead manage the MMCR2 entirely by itself.
  529. */
  530. mtspr(SPRN_MMCR2, cpuhw->mmcr[3] | current->thread.mmcr2);
  531. out:
  532. return mmcr0;
  533. }
  534. static void pmao_restore_workaround(bool ebb)
  535. {
  536. unsigned pmcs[6];
  537. if (!cpu_has_feature(CPU_FTR_PMAO_BUG))
  538. return;
  539. /*
  540. * On POWER8E there is a hardware defect which affects the PMU context
  541. * switch logic, ie. power_pmu_disable/enable().
  542. *
  543. * When a counter overflows PMXE is cleared and FC/PMAO is set in MMCR0
  544. * by the hardware. Sometime later the actual PMU exception is
  545. * delivered.
  546. *
  547. * If we context switch, or simply disable/enable, the PMU prior to the
  548. * exception arriving, the exception will be lost when we clear PMAO.
  549. *
  550. * When we reenable the PMU, we will write the saved MMCR0 with PMAO
  551. * set, and this _should_ generate an exception. However because of the
  552. * defect no exception is generated when we write PMAO, and we get
  553. * stuck with no counters counting but no exception delivered.
  554. *
  555. * The workaround is to detect this case and tweak the hardware to
  556. * create another pending PMU exception.
  557. *
  558. * We do that by setting up PMC6 (cycles) for an imminent overflow and
  559. * enabling the PMU. That causes a new exception to be generated in the
  560. * chip, but we don't take it yet because we have interrupts hard
  561. * disabled. We then write back the PMU state as we want it to be seen
  562. * by the exception handler. When we reenable interrupts the exception
  563. * handler will be called and see the correct state.
  564. *
  565. * The logic is the same for EBB, except that the exception is gated by
  566. * us having interrupts hard disabled as well as the fact that we are
  567. * not in userspace. The exception is finally delivered when we return
  568. * to userspace.
  569. */
  570. /* Only if PMAO is set and PMAO_SYNC is clear */
  571. if ((current->thread.mmcr0 & (MMCR0_PMAO | MMCR0_PMAO_SYNC)) != MMCR0_PMAO)
  572. return;
  573. /* If we're doing EBB, only if BESCR[GE] is set */
  574. if (ebb && !(current->thread.bescr & BESCR_GE))
  575. return;
  576. /*
  577. * We are already soft-disabled in power_pmu_enable(). We need to hard
  578. * disable to actually prevent the PMU exception from firing.
  579. */
  580. hard_irq_disable();
  581. /*
  582. * This is a bit gross, but we know we're on POWER8E and have 6 PMCs.
  583. * Using read/write_pmc() in a for loop adds 12 function calls and
  584. * almost doubles our code size.
  585. */
  586. pmcs[0] = mfspr(SPRN_PMC1);
  587. pmcs[1] = mfspr(SPRN_PMC2);
  588. pmcs[2] = mfspr(SPRN_PMC3);
  589. pmcs[3] = mfspr(SPRN_PMC4);
  590. pmcs[4] = mfspr(SPRN_PMC5);
  591. pmcs[5] = mfspr(SPRN_PMC6);
  592. /* Ensure all freeze bits are unset */
  593. mtspr(SPRN_MMCR2, 0);
  594. /* Set up PMC6 to overflow in one cycle */
  595. mtspr(SPRN_PMC6, 0x7FFFFFFE);
  596. /* Enable exceptions and unfreeze PMC6 */
  597. mtspr(SPRN_MMCR0, MMCR0_PMXE | MMCR0_PMCjCE | MMCR0_PMAO);
  598. /* Now we need to refreeze and restore the PMCs */
  599. mtspr(SPRN_MMCR0, MMCR0_FC | MMCR0_PMAO);
  600. mtspr(SPRN_PMC1, pmcs[0]);
  601. mtspr(SPRN_PMC2, pmcs[1]);
  602. mtspr(SPRN_PMC3, pmcs[2]);
  603. mtspr(SPRN_PMC4, pmcs[3]);
  604. mtspr(SPRN_PMC5, pmcs[4]);
  605. mtspr(SPRN_PMC6, pmcs[5]);
  606. }
  607. #endif /* CONFIG_PPC64 */
  608. static void perf_event_interrupt(struct pt_regs *regs);
  609. /*
  610. * Read one performance monitor counter (PMC).
  611. */
  612. static unsigned long read_pmc(int idx)
  613. {
  614. unsigned long val;
  615. switch (idx) {
  616. case 1:
  617. val = mfspr(SPRN_PMC1);
  618. break;
  619. case 2:
  620. val = mfspr(SPRN_PMC2);
  621. break;
  622. case 3:
  623. val = mfspr(SPRN_PMC3);
  624. break;
  625. case 4:
  626. val = mfspr(SPRN_PMC4);
  627. break;
  628. case 5:
  629. val = mfspr(SPRN_PMC5);
  630. break;
  631. case 6:
  632. val = mfspr(SPRN_PMC6);
  633. break;
  634. #ifdef CONFIG_PPC64
  635. case 7:
  636. val = mfspr(SPRN_PMC7);
  637. break;
  638. case 8:
  639. val = mfspr(SPRN_PMC8);
  640. break;
  641. #endif /* CONFIG_PPC64 */
  642. default:
  643. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  644. val = 0;
  645. }
  646. return val;
  647. }
  648. /*
  649. * Write one PMC.
  650. */
  651. static void write_pmc(int idx, unsigned long val)
  652. {
  653. switch (idx) {
  654. case 1:
  655. mtspr(SPRN_PMC1, val);
  656. break;
  657. case 2:
  658. mtspr(SPRN_PMC2, val);
  659. break;
  660. case 3:
  661. mtspr(SPRN_PMC3, val);
  662. break;
  663. case 4:
  664. mtspr(SPRN_PMC4, val);
  665. break;
  666. case 5:
  667. mtspr(SPRN_PMC5, val);
  668. break;
  669. case 6:
  670. mtspr(SPRN_PMC6, val);
  671. break;
  672. #ifdef CONFIG_PPC64
  673. case 7:
  674. mtspr(SPRN_PMC7, val);
  675. break;
  676. case 8:
  677. mtspr(SPRN_PMC8, val);
  678. break;
  679. #endif /* CONFIG_PPC64 */
  680. default:
  681. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  682. }
  683. }
  684. /* Called from sysrq_handle_showregs() */
  685. void perf_event_print_debug(void)
  686. {
  687. unsigned long sdar, sier, flags;
  688. u32 pmcs[MAX_HWEVENTS];
  689. int i;
  690. if (!ppmu) {
  691. pr_info("Performance monitor hardware not registered.\n");
  692. return;
  693. }
  694. if (!ppmu->n_counter)
  695. return;
  696. local_irq_save(flags);
  697. pr_info("CPU: %d PMU registers, ppmu = %s n_counters = %d",
  698. smp_processor_id(), ppmu->name, ppmu->n_counter);
  699. for (i = 0; i < ppmu->n_counter; i++)
  700. pmcs[i] = read_pmc(i + 1);
  701. for (; i < MAX_HWEVENTS; i++)
  702. pmcs[i] = 0xdeadbeef;
  703. pr_info("PMC1: %08x PMC2: %08x PMC3: %08x PMC4: %08x\n",
  704. pmcs[0], pmcs[1], pmcs[2], pmcs[3]);
  705. if (ppmu->n_counter > 4)
  706. pr_info("PMC5: %08x PMC6: %08x PMC7: %08x PMC8: %08x\n",
  707. pmcs[4], pmcs[5], pmcs[6], pmcs[7]);
  708. pr_info("MMCR0: %016lx MMCR1: %016lx MMCRA: %016lx\n",
  709. mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCRA));
  710. sdar = sier = 0;
  711. #ifdef CONFIG_PPC64
  712. sdar = mfspr(SPRN_SDAR);
  713. if (ppmu->flags & PPMU_HAS_SIER)
  714. sier = mfspr(SPRN_SIER);
  715. if (ppmu->flags & PPMU_ARCH_207S) {
  716. pr_info("MMCR2: %016lx EBBHR: %016lx\n",
  717. mfspr(SPRN_MMCR2), mfspr(SPRN_EBBHR));
  718. pr_info("EBBRR: %016lx BESCR: %016lx\n",
  719. mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
  720. }
  721. #endif
  722. pr_info("SIAR: %016lx SDAR: %016lx SIER: %016lx\n",
  723. mfspr(SPRN_SIAR), sdar, sier);
  724. local_irq_restore(flags);
  725. }
  726. /*
  727. * Check if a set of events can all go on the PMU at once.
  728. * If they can't, this will look at alternative codes for the events
  729. * and see if any combination of alternative codes is feasible.
  730. * The feasible set is returned in event_id[].
  731. */
  732. static int power_check_constraints(struct cpu_hw_events *cpuhw,
  733. u64 event_id[], unsigned int cflags[],
  734. int n_ev)
  735. {
  736. unsigned long mask, value, nv;
  737. unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
  738. int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
  739. int i, j;
  740. unsigned long addf = ppmu->add_fields;
  741. unsigned long tadd = ppmu->test_adder;
  742. if (n_ev > ppmu->n_counter)
  743. return -1;
  744. /* First see if the events will go on as-is */
  745. for (i = 0; i < n_ev; ++i) {
  746. if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
  747. && !ppmu->limited_pmc_event(event_id[i])) {
  748. ppmu->get_alternatives(event_id[i], cflags[i],
  749. cpuhw->alternatives[i]);
  750. event_id[i] = cpuhw->alternatives[i][0];
  751. }
  752. if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
  753. &cpuhw->avalues[i][0]))
  754. return -1;
  755. }
  756. value = mask = 0;
  757. for (i = 0; i < n_ev; ++i) {
  758. nv = (value | cpuhw->avalues[i][0]) +
  759. (value & cpuhw->avalues[i][0] & addf);
  760. if ((((nv + tadd) ^ value) & mask) != 0 ||
  761. (((nv + tadd) ^ cpuhw->avalues[i][0]) &
  762. cpuhw->amasks[i][0]) != 0)
  763. break;
  764. value = nv;
  765. mask |= cpuhw->amasks[i][0];
  766. }
  767. if (i == n_ev)
  768. return 0; /* all OK */
  769. /* doesn't work, gather alternatives... */
  770. if (!ppmu->get_alternatives)
  771. return -1;
  772. for (i = 0; i < n_ev; ++i) {
  773. choice[i] = 0;
  774. n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
  775. cpuhw->alternatives[i]);
  776. for (j = 1; j < n_alt[i]; ++j)
  777. ppmu->get_constraint(cpuhw->alternatives[i][j],
  778. &cpuhw->amasks[i][j],
  779. &cpuhw->avalues[i][j]);
  780. }
  781. /* enumerate all possibilities and see if any will work */
  782. i = 0;
  783. j = -1;
  784. value = mask = nv = 0;
  785. while (i < n_ev) {
  786. if (j >= 0) {
  787. /* we're backtracking, restore context */
  788. value = svalues[i];
  789. mask = smasks[i];
  790. j = choice[i];
  791. }
  792. /*
  793. * See if any alternative k for event_id i,
  794. * where k > j, will satisfy the constraints.
  795. */
  796. while (++j < n_alt[i]) {
  797. nv = (value | cpuhw->avalues[i][j]) +
  798. (value & cpuhw->avalues[i][j] & addf);
  799. if ((((nv + tadd) ^ value) & mask) == 0 &&
  800. (((nv + tadd) ^ cpuhw->avalues[i][j])
  801. & cpuhw->amasks[i][j]) == 0)
  802. break;
  803. }
  804. if (j >= n_alt[i]) {
  805. /*
  806. * No feasible alternative, backtrack
  807. * to event_id i-1 and continue enumerating its
  808. * alternatives from where we got up to.
  809. */
  810. if (--i < 0)
  811. return -1;
  812. } else {
  813. /*
  814. * Found a feasible alternative for event_id i,
  815. * remember where we got up to with this event_id,
  816. * go on to the next event_id, and start with
  817. * the first alternative for it.
  818. */
  819. choice[i] = j;
  820. svalues[i] = value;
  821. smasks[i] = mask;
  822. value = nv;
  823. mask |= cpuhw->amasks[i][j];
  824. ++i;
  825. j = -1;
  826. }
  827. }
  828. /* OK, we have a feasible combination, tell the caller the solution */
  829. for (i = 0; i < n_ev; ++i)
  830. event_id[i] = cpuhw->alternatives[i][choice[i]];
  831. return 0;
  832. }
  833. /*
  834. * Check if newly-added events have consistent settings for
  835. * exclude_{user,kernel,hv} with each other and any previously
  836. * added events.
  837. */
  838. static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
  839. int n_prev, int n_new)
  840. {
  841. int eu = 0, ek = 0, eh = 0;
  842. int i, n, first;
  843. struct perf_event *event;
  844. /*
  845. * If the PMU we're on supports per event exclude settings then we
  846. * don't need to do any of this logic. NB. This assumes no PMU has both
  847. * per event exclude and limited PMCs.
  848. */
  849. if (ppmu->flags & PPMU_ARCH_207S)
  850. return 0;
  851. n = n_prev + n_new;
  852. if (n <= 1)
  853. return 0;
  854. first = 1;
  855. for (i = 0; i < n; ++i) {
  856. if (cflags[i] & PPMU_LIMITED_PMC_OK) {
  857. cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
  858. continue;
  859. }
  860. event = ctrs[i];
  861. if (first) {
  862. eu = event->attr.exclude_user;
  863. ek = event->attr.exclude_kernel;
  864. eh = event->attr.exclude_hv;
  865. first = 0;
  866. } else if (event->attr.exclude_user != eu ||
  867. event->attr.exclude_kernel != ek ||
  868. event->attr.exclude_hv != eh) {
  869. return -EAGAIN;
  870. }
  871. }
  872. if (eu || ek || eh)
  873. for (i = 0; i < n; ++i)
  874. if (cflags[i] & PPMU_LIMITED_PMC_OK)
  875. cflags[i] |= PPMU_LIMITED_PMC_REQD;
  876. return 0;
  877. }
  878. static u64 check_and_compute_delta(u64 prev, u64 val)
  879. {
  880. u64 delta = (val - prev) & 0xfffffffful;
  881. /*
  882. * POWER7 can roll back counter values, if the new value is smaller
  883. * than the previous value it will cause the delta and the counter to
  884. * have bogus values unless we rolled a counter over. If a coutner is
  885. * rolled back, it will be smaller, but within 256, which is the maximum
  886. * number of events to rollback at once. If we detect a rollback
  887. * return 0. This can lead to a small lack of precision in the
  888. * counters.
  889. */
  890. if (prev > val && (prev - val) < 256)
  891. delta = 0;
  892. return delta;
  893. }
  894. static void power_pmu_read(struct perf_event *event)
  895. {
  896. s64 val, delta, prev;
  897. if (event->hw.state & PERF_HES_STOPPED)
  898. return;
  899. if (!event->hw.idx)
  900. return;
  901. if (is_ebb_event(event)) {
  902. val = read_pmc(event->hw.idx);
  903. local64_set(&event->hw.prev_count, val);
  904. return;
  905. }
  906. /*
  907. * Performance monitor interrupts come even when interrupts
  908. * are soft-disabled, as long as interrupts are hard-enabled.
  909. * Therefore we treat them like NMIs.
  910. */
  911. do {
  912. prev = local64_read(&event->hw.prev_count);
  913. barrier();
  914. val = read_pmc(event->hw.idx);
  915. delta = check_and_compute_delta(prev, val);
  916. if (!delta)
  917. return;
  918. } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  919. local64_add(delta, &event->count);
  920. /*
  921. * A number of places program the PMC with (0x80000000 - period_left).
  922. * We never want period_left to be less than 1 because we will program
  923. * the PMC with a value >= 0x800000000 and an edge detected PMC will
  924. * roll around to 0 before taking an exception. We have seen this
  925. * on POWER8.
  926. *
  927. * To fix this, clamp the minimum value of period_left to 1.
  928. */
  929. do {
  930. prev = local64_read(&event->hw.period_left);
  931. val = prev - delta;
  932. if (val < 1)
  933. val = 1;
  934. } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
  935. }
  936. /*
  937. * On some machines, PMC5 and PMC6 can't be written, don't respect
  938. * the freeze conditions, and don't generate interrupts. This tells
  939. * us if `event' is using such a PMC.
  940. */
  941. static int is_limited_pmc(int pmcnum)
  942. {
  943. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  944. && (pmcnum == 5 || pmcnum == 6);
  945. }
  946. static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
  947. unsigned long pmc5, unsigned long pmc6)
  948. {
  949. struct perf_event *event;
  950. u64 val, prev, delta;
  951. int i;
  952. for (i = 0; i < cpuhw->n_limited; ++i) {
  953. event = cpuhw->limited_counter[i];
  954. if (!event->hw.idx)
  955. continue;
  956. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  957. prev = local64_read(&event->hw.prev_count);
  958. event->hw.idx = 0;
  959. delta = check_and_compute_delta(prev, val);
  960. if (delta)
  961. local64_add(delta, &event->count);
  962. }
  963. }
  964. static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
  965. unsigned long pmc5, unsigned long pmc6)
  966. {
  967. struct perf_event *event;
  968. u64 val, prev;
  969. int i;
  970. for (i = 0; i < cpuhw->n_limited; ++i) {
  971. event = cpuhw->limited_counter[i];
  972. event->hw.idx = cpuhw->limited_hwidx[i];
  973. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  974. prev = local64_read(&event->hw.prev_count);
  975. if (check_and_compute_delta(prev, val))
  976. local64_set(&event->hw.prev_count, val);
  977. perf_event_update_userpage(event);
  978. }
  979. }
  980. /*
  981. * Since limited events don't respect the freeze conditions, we
  982. * have to read them immediately after freezing or unfreezing the
  983. * other events. We try to keep the values from the limited
  984. * events as consistent as possible by keeping the delay (in
  985. * cycles and instructions) between freezing/unfreezing and reading
  986. * the limited events as small and consistent as possible.
  987. * Therefore, if any limited events are in use, we read them
  988. * both, and always in the same order, to minimize variability,
  989. * and do it inside the same asm that writes MMCR0.
  990. */
  991. static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
  992. {
  993. unsigned long pmc5, pmc6;
  994. if (!cpuhw->n_limited) {
  995. mtspr(SPRN_MMCR0, mmcr0);
  996. return;
  997. }
  998. /*
  999. * Write MMCR0, then read PMC5 and PMC6 immediately.
  1000. * To ensure we don't get a performance monitor interrupt
  1001. * between writing MMCR0 and freezing/thawing the limited
  1002. * events, we first write MMCR0 with the event overflow
  1003. * interrupt enable bits turned off.
  1004. */
  1005. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  1006. : "=&r" (pmc5), "=&r" (pmc6)
  1007. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  1008. "i" (SPRN_MMCR0),
  1009. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  1010. if (mmcr0 & MMCR0_FC)
  1011. freeze_limited_counters(cpuhw, pmc5, pmc6);
  1012. else
  1013. thaw_limited_counters(cpuhw, pmc5, pmc6);
  1014. /*
  1015. * Write the full MMCR0 including the event overflow interrupt
  1016. * enable bits, if necessary.
  1017. */
  1018. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  1019. mtspr(SPRN_MMCR0, mmcr0);
  1020. }
  1021. /*
  1022. * Disable all events to prevent PMU interrupts and to allow
  1023. * events to be added or removed.
  1024. */
  1025. static void power_pmu_disable(struct pmu *pmu)
  1026. {
  1027. struct cpu_hw_events *cpuhw;
  1028. unsigned long flags, mmcr0, val;
  1029. if (!ppmu)
  1030. return;
  1031. local_irq_save(flags);
  1032. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1033. if (!cpuhw->disabled) {
  1034. /*
  1035. * Check if we ever enabled the PMU on this cpu.
  1036. */
  1037. if (!cpuhw->pmcs_enabled) {
  1038. ppc_enable_pmcs();
  1039. cpuhw->pmcs_enabled = 1;
  1040. }
  1041. /*
  1042. * Set the 'freeze counters' bit, clear EBE/BHRBA/PMCC/PMAO/FC56
  1043. */
  1044. val = mmcr0 = mfspr(SPRN_MMCR0);
  1045. val |= MMCR0_FC;
  1046. val &= ~(MMCR0_EBE | MMCR0_BHRBA | MMCR0_PMCC | MMCR0_PMAO |
  1047. MMCR0_FC56);
  1048. /*
  1049. * The barrier is to make sure the mtspr has been
  1050. * executed and the PMU has frozen the events etc.
  1051. * before we return.
  1052. */
  1053. write_mmcr0(cpuhw, val);
  1054. mb();
  1055. isync();
  1056. /*
  1057. * Disable instruction sampling if it was enabled
  1058. */
  1059. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  1060. mtspr(SPRN_MMCRA,
  1061. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  1062. mb();
  1063. isync();
  1064. }
  1065. cpuhw->disabled = 1;
  1066. cpuhw->n_added = 0;
  1067. ebb_switch_out(mmcr0);
  1068. #ifdef CONFIG_PPC64
  1069. /*
  1070. * These are readable by userspace, may contain kernel
  1071. * addresses and are not switched by context switch, so clear
  1072. * them now to avoid leaking anything to userspace in general
  1073. * including to another process.
  1074. */
  1075. if (ppmu->flags & PPMU_ARCH_207S) {
  1076. mtspr(SPRN_SDAR, 0);
  1077. mtspr(SPRN_SIAR, 0);
  1078. }
  1079. #endif
  1080. }
  1081. local_irq_restore(flags);
  1082. }
  1083. /*
  1084. * Re-enable all events if disable == 0.
  1085. * If we were previously disabled and events were added, then
  1086. * put the new config on the PMU.
  1087. */
  1088. static void power_pmu_enable(struct pmu *pmu)
  1089. {
  1090. struct perf_event *event;
  1091. struct cpu_hw_events *cpuhw;
  1092. unsigned long flags;
  1093. long i;
  1094. unsigned long val, mmcr0;
  1095. s64 left;
  1096. unsigned int hwc_index[MAX_HWEVENTS];
  1097. int n_lim;
  1098. int idx;
  1099. bool ebb;
  1100. if (!ppmu)
  1101. return;
  1102. local_irq_save(flags);
  1103. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1104. if (!cpuhw->disabled)
  1105. goto out;
  1106. if (cpuhw->n_events == 0) {
  1107. ppc_set_pmu_inuse(0);
  1108. goto out;
  1109. }
  1110. cpuhw->disabled = 0;
  1111. /*
  1112. * EBB requires an exclusive group and all events must have the EBB
  1113. * flag set, or not set, so we can just check a single event. Also we
  1114. * know we have at least one event.
  1115. */
  1116. ebb = is_ebb_event(cpuhw->event[0]);
  1117. /*
  1118. * If we didn't change anything, or only removed events,
  1119. * no need to recalculate MMCR* settings and reset the PMCs.
  1120. * Just reenable the PMU with the current MMCR* settings
  1121. * (possibly updated for removal of events).
  1122. */
  1123. if (!cpuhw->n_added) {
  1124. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  1125. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  1126. goto out_enable;
  1127. }
  1128. /*
  1129. * Clear all MMCR settings and recompute them for the new set of events.
  1130. */
  1131. memset(cpuhw->mmcr, 0, sizeof(cpuhw->mmcr));
  1132. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
  1133. cpuhw->mmcr, cpuhw->event)) {
  1134. /* shouldn't ever get here */
  1135. printk(KERN_ERR "oops compute_mmcr failed\n");
  1136. goto out;
  1137. }
  1138. if (!(ppmu->flags & PPMU_ARCH_207S)) {
  1139. /*
  1140. * Add in MMCR0 freeze bits corresponding to the attr.exclude_*
  1141. * bits for the first event. We have already checked that all
  1142. * events have the same value for these bits as the first event.
  1143. */
  1144. event = cpuhw->event[0];
  1145. if (event->attr.exclude_user)
  1146. cpuhw->mmcr[0] |= MMCR0_FCP;
  1147. if (event->attr.exclude_kernel)
  1148. cpuhw->mmcr[0] |= freeze_events_kernel;
  1149. if (event->attr.exclude_hv)
  1150. cpuhw->mmcr[0] |= MMCR0_FCHV;
  1151. }
  1152. /*
  1153. * Write the new configuration to MMCR* with the freeze
  1154. * bit set and set the hardware events to their initial values.
  1155. * Then unfreeze the events.
  1156. */
  1157. ppc_set_pmu_inuse(1);
  1158. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  1159. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  1160. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  1161. | MMCR0_FC);
  1162. if (ppmu->flags & PPMU_ARCH_207S)
  1163. mtspr(SPRN_MMCR2, cpuhw->mmcr[3]);
  1164. /*
  1165. * Read off any pre-existing events that need to move
  1166. * to another PMC.
  1167. */
  1168. for (i = 0; i < cpuhw->n_events; ++i) {
  1169. event = cpuhw->event[i];
  1170. if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
  1171. power_pmu_read(event);
  1172. write_pmc(event->hw.idx, 0);
  1173. event->hw.idx = 0;
  1174. }
  1175. }
  1176. /*
  1177. * Initialize the PMCs for all the new and moved events.
  1178. */
  1179. cpuhw->n_limited = n_lim = 0;
  1180. for (i = 0; i < cpuhw->n_events; ++i) {
  1181. event = cpuhw->event[i];
  1182. if (event->hw.idx)
  1183. continue;
  1184. idx = hwc_index[i] + 1;
  1185. if (is_limited_pmc(idx)) {
  1186. cpuhw->limited_counter[n_lim] = event;
  1187. cpuhw->limited_hwidx[n_lim] = idx;
  1188. ++n_lim;
  1189. continue;
  1190. }
  1191. if (ebb)
  1192. val = local64_read(&event->hw.prev_count);
  1193. else {
  1194. val = 0;
  1195. if (event->hw.sample_period) {
  1196. left = local64_read(&event->hw.period_left);
  1197. if (left < 0x80000000L)
  1198. val = 0x80000000L - left;
  1199. }
  1200. local64_set(&event->hw.prev_count, val);
  1201. }
  1202. event->hw.idx = idx;
  1203. if (event->hw.state & PERF_HES_STOPPED)
  1204. val = 0;
  1205. write_pmc(idx, val);
  1206. perf_event_update_userpage(event);
  1207. }
  1208. cpuhw->n_limited = n_lim;
  1209. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  1210. out_enable:
  1211. pmao_restore_workaround(ebb);
  1212. mmcr0 = ebb_switch_in(ebb, cpuhw);
  1213. mb();
  1214. if (cpuhw->bhrb_users)
  1215. ppmu->config_bhrb(cpuhw->bhrb_filter);
  1216. write_mmcr0(cpuhw, mmcr0);
  1217. /*
  1218. * Enable instruction sampling if necessary
  1219. */
  1220. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  1221. mb();
  1222. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  1223. }
  1224. out:
  1225. local_irq_restore(flags);
  1226. }
  1227. static int collect_events(struct perf_event *group, int max_count,
  1228. struct perf_event *ctrs[], u64 *events,
  1229. unsigned int *flags)
  1230. {
  1231. int n = 0;
  1232. struct perf_event *event;
  1233. if (group->pmu->task_ctx_nr == perf_hw_context) {
  1234. if (n >= max_count)
  1235. return -1;
  1236. ctrs[n] = group;
  1237. flags[n] = group->hw.event_base;
  1238. events[n++] = group->hw.config;
  1239. }
  1240. for_each_sibling_event(event, group) {
  1241. if (event->pmu->task_ctx_nr == perf_hw_context &&
  1242. event->state != PERF_EVENT_STATE_OFF) {
  1243. if (n >= max_count)
  1244. return -1;
  1245. ctrs[n] = event;
  1246. flags[n] = event->hw.event_base;
  1247. events[n++] = event->hw.config;
  1248. }
  1249. }
  1250. return n;
  1251. }
  1252. /*
  1253. * Add an event to the PMU.
  1254. * If all events are not already frozen, then we disable and
  1255. * re-enable the PMU in order to get hw_perf_enable to do the
  1256. * actual work of reconfiguring the PMU.
  1257. */
  1258. static int power_pmu_add(struct perf_event *event, int ef_flags)
  1259. {
  1260. struct cpu_hw_events *cpuhw;
  1261. unsigned long flags;
  1262. int n0;
  1263. int ret = -EAGAIN;
  1264. local_irq_save(flags);
  1265. perf_pmu_disable(event->pmu);
  1266. /*
  1267. * Add the event to the list (if there is room)
  1268. * and check whether the total set is still feasible.
  1269. */
  1270. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1271. n0 = cpuhw->n_events;
  1272. if (n0 >= ppmu->n_counter)
  1273. goto out;
  1274. cpuhw->event[n0] = event;
  1275. cpuhw->events[n0] = event->hw.config;
  1276. cpuhw->flags[n0] = event->hw.event_base;
  1277. /*
  1278. * This event may have been disabled/stopped in record_and_restart()
  1279. * because we exceeded the ->event_limit. If re-starting the event,
  1280. * clear the ->hw.state (STOPPED and UPTODATE flags), so the user
  1281. * notification is re-enabled.
  1282. */
  1283. if (!(ef_flags & PERF_EF_START))
  1284. event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  1285. else
  1286. event->hw.state = 0;
  1287. /*
  1288. * If group events scheduling transaction was started,
  1289. * skip the schedulability test here, it will be performed
  1290. * at commit time(->commit_txn) as a whole
  1291. */
  1292. if (cpuhw->txn_flags & PERF_PMU_TXN_ADD)
  1293. goto nocheck;
  1294. if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
  1295. goto out;
  1296. if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
  1297. goto out;
  1298. event->hw.config = cpuhw->events[n0];
  1299. nocheck:
  1300. ebb_event_add(event);
  1301. ++cpuhw->n_events;
  1302. ++cpuhw->n_added;
  1303. ret = 0;
  1304. out:
  1305. if (has_branch_stack(event)) {
  1306. power_pmu_bhrb_enable(event);
  1307. cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
  1308. event->attr.branch_sample_type);
  1309. }
  1310. perf_pmu_enable(event->pmu);
  1311. local_irq_restore(flags);
  1312. return ret;
  1313. }
  1314. /*
  1315. * Remove an event from the PMU.
  1316. */
  1317. static void power_pmu_del(struct perf_event *event, int ef_flags)
  1318. {
  1319. struct cpu_hw_events *cpuhw;
  1320. long i;
  1321. unsigned long flags;
  1322. local_irq_save(flags);
  1323. perf_pmu_disable(event->pmu);
  1324. power_pmu_read(event);
  1325. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1326. for (i = 0; i < cpuhw->n_events; ++i) {
  1327. if (event == cpuhw->event[i]) {
  1328. while (++i < cpuhw->n_events) {
  1329. cpuhw->event[i-1] = cpuhw->event[i];
  1330. cpuhw->events[i-1] = cpuhw->events[i];
  1331. cpuhw->flags[i-1] = cpuhw->flags[i];
  1332. }
  1333. --cpuhw->n_events;
  1334. ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
  1335. if (event->hw.idx) {
  1336. write_pmc(event->hw.idx, 0);
  1337. event->hw.idx = 0;
  1338. }
  1339. perf_event_update_userpage(event);
  1340. break;
  1341. }
  1342. }
  1343. for (i = 0; i < cpuhw->n_limited; ++i)
  1344. if (event == cpuhw->limited_counter[i])
  1345. break;
  1346. if (i < cpuhw->n_limited) {
  1347. while (++i < cpuhw->n_limited) {
  1348. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  1349. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  1350. }
  1351. --cpuhw->n_limited;
  1352. }
  1353. if (cpuhw->n_events == 0) {
  1354. /* disable exceptions if no events are running */
  1355. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  1356. }
  1357. if (has_branch_stack(event))
  1358. power_pmu_bhrb_disable(event);
  1359. perf_pmu_enable(event->pmu);
  1360. local_irq_restore(flags);
  1361. }
  1362. /*
  1363. * POWER-PMU does not support disabling individual counters, hence
  1364. * program their cycle counter to their max value and ignore the interrupts.
  1365. */
  1366. static void power_pmu_start(struct perf_event *event, int ef_flags)
  1367. {
  1368. unsigned long flags;
  1369. s64 left;
  1370. unsigned long val;
  1371. if (!event->hw.idx || !event->hw.sample_period)
  1372. return;
  1373. if (!(event->hw.state & PERF_HES_STOPPED))
  1374. return;
  1375. if (ef_flags & PERF_EF_RELOAD)
  1376. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1377. local_irq_save(flags);
  1378. perf_pmu_disable(event->pmu);
  1379. event->hw.state = 0;
  1380. left = local64_read(&event->hw.period_left);
  1381. val = 0;
  1382. if (left < 0x80000000L)
  1383. val = 0x80000000L - left;
  1384. write_pmc(event->hw.idx, val);
  1385. perf_event_update_userpage(event);
  1386. perf_pmu_enable(event->pmu);
  1387. local_irq_restore(flags);
  1388. }
  1389. static void power_pmu_stop(struct perf_event *event, int ef_flags)
  1390. {
  1391. unsigned long flags;
  1392. if (!event->hw.idx || !event->hw.sample_period)
  1393. return;
  1394. if (event->hw.state & PERF_HES_STOPPED)
  1395. return;
  1396. local_irq_save(flags);
  1397. perf_pmu_disable(event->pmu);
  1398. power_pmu_read(event);
  1399. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  1400. write_pmc(event->hw.idx, 0);
  1401. perf_event_update_userpage(event);
  1402. perf_pmu_enable(event->pmu);
  1403. local_irq_restore(flags);
  1404. }
  1405. /*
  1406. * Start group events scheduling transaction
  1407. * Set the flag to make pmu::enable() not perform the
  1408. * schedulability test, it will be performed at commit time
  1409. *
  1410. * We only support PERF_PMU_TXN_ADD transactions. Save the
  1411. * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
  1412. * transactions.
  1413. */
  1414. static void power_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
  1415. {
  1416. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1417. WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
  1418. cpuhw->txn_flags = txn_flags;
  1419. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1420. return;
  1421. perf_pmu_disable(pmu);
  1422. cpuhw->n_txn_start = cpuhw->n_events;
  1423. }
  1424. /*
  1425. * Stop group events scheduling transaction
  1426. * Clear the flag and pmu::enable() will perform the
  1427. * schedulability test.
  1428. */
  1429. static void power_pmu_cancel_txn(struct pmu *pmu)
  1430. {
  1431. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1432. unsigned int txn_flags;
  1433. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  1434. txn_flags = cpuhw->txn_flags;
  1435. cpuhw->txn_flags = 0;
  1436. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1437. return;
  1438. perf_pmu_enable(pmu);
  1439. }
  1440. /*
  1441. * Commit group events scheduling transaction
  1442. * Perform the group schedulability test as a whole
  1443. * Return 0 if success
  1444. */
  1445. static int power_pmu_commit_txn(struct pmu *pmu)
  1446. {
  1447. struct cpu_hw_events *cpuhw;
  1448. long i, n;
  1449. if (!ppmu)
  1450. return -EAGAIN;
  1451. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1452. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  1453. if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
  1454. cpuhw->txn_flags = 0;
  1455. return 0;
  1456. }
  1457. n = cpuhw->n_events;
  1458. if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
  1459. return -EAGAIN;
  1460. i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
  1461. if (i < 0)
  1462. return -EAGAIN;
  1463. for (i = cpuhw->n_txn_start; i < n; ++i)
  1464. cpuhw->event[i]->hw.config = cpuhw->events[i];
  1465. cpuhw->txn_flags = 0;
  1466. perf_pmu_enable(pmu);
  1467. return 0;
  1468. }
  1469. /*
  1470. * Return 1 if we might be able to put event on a limited PMC,
  1471. * or 0 if not.
  1472. * An event can only go on a limited PMC if it counts something
  1473. * that a limited PMC can count, doesn't require interrupts, and
  1474. * doesn't exclude any processor mode.
  1475. */
  1476. static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
  1477. unsigned int flags)
  1478. {
  1479. int n;
  1480. u64 alt[MAX_EVENT_ALTERNATIVES];
  1481. if (event->attr.exclude_user
  1482. || event->attr.exclude_kernel
  1483. || event->attr.exclude_hv
  1484. || event->attr.sample_period)
  1485. return 0;
  1486. if (ppmu->limited_pmc_event(ev))
  1487. return 1;
  1488. /*
  1489. * The requested event_id isn't on a limited PMC already;
  1490. * see if any alternative code goes on a limited PMC.
  1491. */
  1492. if (!ppmu->get_alternatives)
  1493. return 0;
  1494. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  1495. n = ppmu->get_alternatives(ev, flags, alt);
  1496. return n > 0;
  1497. }
  1498. /*
  1499. * Find an alternative event_id that goes on a normal PMC, if possible,
  1500. * and return the event_id code, or 0 if there is no such alternative.
  1501. * (Note: event_id code 0 is "don't count" on all machines.)
  1502. */
  1503. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  1504. {
  1505. u64 alt[MAX_EVENT_ALTERNATIVES];
  1506. int n;
  1507. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  1508. n = ppmu->get_alternatives(ev, flags, alt);
  1509. if (!n)
  1510. return 0;
  1511. return alt[0];
  1512. }
  1513. /* Number of perf_events counting hardware events */
  1514. static atomic_t num_events;
  1515. /* Used to avoid races in calling reserve/release_pmc_hardware */
  1516. static DEFINE_MUTEX(pmc_reserve_mutex);
  1517. /*
  1518. * Release the PMU if this is the last perf_event.
  1519. */
  1520. static void hw_perf_event_destroy(struct perf_event *event)
  1521. {
  1522. if (!atomic_add_unless(&num_events, -1, 1)) {
  1523. mutex_lock(&pmc_reserve_mutex);
  1524. if (atomic_dec_return(&num_events) == 0)
  1525. release_pmc_hardware();
  1526. mutex_unlock(&pmc_reserve_mutex);
  1527. }
  1528. }
  1529. /*
  1530. * Translate a generic cache event_id config to a raw event_id code.
  1531. */
  1532. static int hw_perf_cache_event(u64 config, u64 *eventp)
  1533. {
  1534. unsigned long type, op, result;
  1535. int ev;
  1536. if (!ppmu->cache_events)
  1537. return -EINVAL;
  1538. /* unpack config */
  1539. type = config & 0xff;
  1540. op = (config >> 8) & 0xff;
  1541. result = (config >> 16) & 0xff;
  1542. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  1543. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  1544. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  1545. return -EINVAL;
  1546. ev = (*ppmu->cache_events)[type][op][result];
  1547. if (ev == 0)
  1548. return -EOPNOTSUPP;
  1549. if (ev == -1)
  1550. return -EINVAL;
  1551. *eventp = ev;
  1552. return 0;
  1553. }
  1554. static bool is_event_blacklisted(u64 ev)
  1555. {
  1556. int i;
  1557. for (i=0; i < ppmu->n_blacklist_ev; i++) {
  1558. if (ppmu->blacklist_ev[i] == ev)
  1559. return true;
  1560. }
  1561. return false;
  1562. }
  1563. static int power_pmu_event_init(struct perf_event *event)
  1564. {
  1565. u64 ev;
  1566. unsigned long flags;
  1567. struct perf_event *ctrs[MAX_HWEVENTS];
  1568. u64 events[MAX_HWEVENTS];
  1569. unsigned int cflags[MAX_HWEVENTS];
  1570. int n;
  1571. int err;
  1572. struct cpu_hw_events *cpuhw;
  1573. if (!ppmu)
  1574. return -ENOENT;
  1575. if (has_branch_stack(event)) {
  1576. /* PMU has BHRB enabled */
  1577. if (!(ppmu->flags & PPMU_ARCH_207S))
  1578. return -EOPNOTSUPP;
  1579. }
  1580. switch (event->attr.type) {
  1581. case PERF_TYPE_HARDWARE:
  1582. ev = event->attr.config;
  1583. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  1584. return -EOPNOTSUPP;
  1585. if (ppmu->blacklist_ev && is_event_blacklisted(ev))
  1586. return -EINVAL;
  1587. ev = ppmu->generic_events[ev];
  1588. break;
  1589. case PERF_TYPE_HW_CACHE:
  1590. err = hw_perf_cache_event(event->attr.config, &ev);
  1591. if (err)
  1592. return err;
  1593. if (ppmu->blacklist_ev && is_event_blacklisted(ev))
  1594. return -EINVAL;
  1595. break;
  1596. case PERF_TYPE_RAW:
  1597. ev = event->attr.config;
  1598. if (ppmu->blacklist_ev && is_event_blacklisted(ev))
  1599. return -EINVAL;
  1600. break;
  1601. default:
  1602. return -ENOENT;
  1603. }
  1604. event->hw.config_base = ev;
  1605. event->hw.idx = 0;
  1606. /*
  1607. * If we are not running on a hypervisor, force the
  1608. * exclude_hv bit to 0 so that we don't care what
  1609. * the user set it to.
  1610. */
  1611. if (!firmware_has_feature(FW_FEATURE_LPAR))
  1612. event->attr.exclude_hv = 0;
  1613. /*
  1614. * If this is a per-task event, then we can use
  1615. * PM_RUN_* events interchangeably with their non RUN_*
  1616. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  1617. * XXX we should check if the task is an idle task.
  1618. */
  1619. flags = 0;
  1620. if (event->attach_state & PERF_ATTACH_TASK)
  1621. flags |= PPMU_ONLY_COUNT_RUN;
  1622. /*
  1623. * If this machine has limited events, check whether this
  1624. * event_id could go on a limited event.
  1625. */
  1626. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  1627. if (can_go_on_limited_pmc(event, ev, flags)) {
  1628. flags |= PPMU_LIMITED_PMC_OK;
  1629. } else if (ppmu->limited_pmc_event(ev)) {
  1630. /*
  1631. * The requested event_id is on a limited PMC,
  1632. * but we can't use a limited PMC; see if any
  1633. * alternative goes on a normal PMC.
  1634. */
  1635. ev = normal_pmc_alternative(ev, flags);
  1636. if (!ev)
  1637. return -EINVAL;
  1638. }
  1639. }
  1640. /* Extra checks for EBB */
  1641. err = ebb_event_check(event);
  1642. if (err)
  1643. return err;
  1644. /*
  1645. * If this is in a group, check if it can go on with all the
  1646. * other hardware events in the group. We assume the event
  1647. * hasn't been linked into its leader's sibling list at this point.
  1648. */
  1649. n = 0;
  1650. if (event->group_leader != event) {
  1651. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  1652. ctrs, events, cflags);
  1653. if (n < 0)
  1654. return -EINVAL;
  1655. }
  1656. events[n] = ev;
  1657. ctrs[n] = event;
  1658. cflags[n] = flags;
  1659. if (check_excludes(ctrs, cflags, n, 1))
  1660. return -EINVAL;
  1661. cpuhw = &get_cpu_var(cpu_hw_events);
  1662. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  1663. if (has_branch_stack(event)) {
  1664. cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
  1665. event->attr.branch_sample_type);
  1666. if (cpuhw->bhrb_filter == -1) {
  1667. put_cpu_var(cpu_hw_events);
  1668. return -EOPNOTSUPP;
  1669. }
  1670. }
  1671. put_cpu_var(cpu_hw_events);
  1672. if (err)
  1673. return -EINVAL;
  1674. event->hw.config = events[n];
  1675. event->hw.event_base = cflags[n];
  1676. event->hw.last_period = event->hw.sample_period;
  1677. local64_set(&event->hw.period_left, event->hw.last_period);
  1678. /*
  1679. * For EBB events we just context switch the PMC value, we don't do any
  1680. * of the sample_period logic. We use hw.prev_count for this.
  1681. */
  1682. if (is_ebb_event(event))
  1683. local64_set(&event->hw.prev_count, 0);
  1684. /*
  1685. * See if we need to reserve the PMU.
  1686. * If no events are currently in use, then we have to take a
  1687. * mutex to ensure that we don't race with another task doing
  1688. * reserve_pmc_hardware or release_pmc_hardware.
  1689. */
  1690. err = 0;
  1691. if (!atomic_inc_not_zero(&num_events)) {
  1692. mutex_lock(&pmc_reserve_mutex);
  1693. if (atomic_read(&num_events) == 0 &&
  1694. reserve_pmc_hardware(perf_event_interrupt))
  1695. err = -EBUSY;
  1696. else
  1697. atomic_inc(&num_events);
  1698. mutex_unlock(&pmc_reserve_mutex);
  1699. }
  1700. event->destroy = hw_perf_event_destroy;
  1701. return err;
  1702. }
  1703. static int power_pmu_event_idx(struct perf_event *event)
  1704. {
  1705. return event->hw.idx;
  1706. }
  1707. ssize_t power_events_sysfs_show(struct device *dev,
  1708. struct device_attribute *attr, char *page)
  1709. {
  1710. struct perf_pmu_events_attr *pmu_attr;
  1711. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  1712. return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
  1713. }
  1714. static struct pmu power_pmu = {
  1715. .pmu_enable = power_pmu_enable,
  1716. .pmu_disable = power_pmu_disable,
  1717. .event_init = power_pmu_event_init,
  1718. .add = power_pmu_add,
  1719. .del = power_pmu_del,
  1720. .start = power_pmu_start,
  1721. .stop = power_pmu_stop,
  1722. .read = power_pmu_read,
  1723. .start_txn = power_pmu_start_txn,
  1724. .cancel_txn = power_pmu_cancel_txn,
  1725. .commit_txn = power_pmu_commit_txn,
  1726. .event_idx = power_pmu_event_idx,
  1727. .sched_task = power_pmu_sched_task,
  1728. };
  1729. /*
  1730. * A counter has overflowed; update its count and record
  1731. * things if requested. Note that interrupts are hard-disabled
  1732. * here so there is no possibility of being interrupted.
  1733. */
  1734. static void record_and_restart(struct perf_event *event, unsigned long val,
  1735. struct pt_regs *regs)
  1736. {
  1737. u64 period = event->hw.sample_period;
  1738. s64 prev, delta, left;
  1739. int record = 0;
  1740. if (event->hw.state & PERF_HES_STOPPED) {
  1741. write_pmc(event->hw.idx, 0);
  1742. return;
  1743. }
  1744. /* we don't have to worry about interrupts here */
  1745. prev = local64_read(&event->hw.prev_count);
  1746. delta = check_and_compute_delta(prev, val);
  1747. local64_add(delta, &event->count);
  1748. /*
  1749. * See if the total period for this event has expired,
  1750. * and update for the next period.
  1751. */
  1752. val = 0;
  1753. left = local64_read(&event->hw.period_left) - delta;
  1754. if (delta == 0)
  1755. left++;
  1756. if (period) {
  1757. if (left <= 0) {
  1758. left += period;
  1759. if (left <= 0)
  1760. left = period;
  1761. record = siar_valid(regs);
  1762. event->hw.last_period = event->hw.sample_period;
  1763. }
  1764. if (left < 0x80000000LL)
  1765. val = 0x80000000LL - left;
  1766. }
  1767. write_pmc(event->hw.idx, val);
  1768. local64_set(&event->hw.prev_count, val);
  1769. local64_set(&event->hw.period_left, left);
  1770. perf_event_update_userpage(event);
  1771. /*
  1772. * Finally record data if requested.
  1773. */
  1774. if (record) {
  1775. struct perf_sample_data data;
  1776. perf_sample_data_init(&data, ~0ULL, event->hw.last_period);
  1777. if (event->attr.sample_type &
  1778. (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
  1779. perf_get_data_addr(regs, &data.addr);
  1780. if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
  1781. struct cpu_hw_events *cpuhw;
  1782. cpuhw = this_cpu_ptr(&cpu_hw_events);
  1783. power_pmu_bhrb_read(cpuhw);
  1784. data.br_stack = &cpuhw->bhrb_stack;
  1785. }
  1786. if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
  1787. ppmu->get_mem_data_src)
  1788. ppmu->get_mem_data_src(&data.data_src, ppmu->flags, regs);
  1789. if (event->attr.sample_type & PERF_SAMPLE_WEIGHT &&
  1790. ppmu->get_mem_weight)
  1791. ppmu->get_mem_weight(&data.weight);
  1792. if (perf_event_overflow(event, &data, regs))
  1793. power_pmu_stop(event, 0);
  1794. }
  1795. }
  1796. /*
  1797. * Called from generic code to get the misc flags (i.e. processor mode)
  1798. * for an event_id.
  1799. */
  1800. unsigned long perf_misc_flags(struct pt_regs *regs)
  1801. {
  1802. u32 flags = perf_get_misc_flags(regs);
  1803. if (flags)
  1804. return flags;
  1805. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1806. PERF_RECORD_MISC_KERNEL;
  1807. }
  1808. /*
  1809. * Called from generic code to get the instruction pointer
  1810. * for an event_id.
  1811. */
  1812. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1813. {
  1814. bool use_siar = regs_use_siar(regs);
  1815. if (use_siar && siar_valid(regs))
  1816. return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1817. else if (use_siar)
  1818. return 0; // no valid instruction pointer
  1819. else
  1820. return regs->nip;
  1821. }
  1822. static bool pmc_overflow_power7(unsigned long val)
  1823. {
  1824. /*
  1825. * Events on POWER7 can roll back if a speculative event doesn't
  1826. * eventually complete. Unfortunately in some rare cases they will
  1827. * raise a performance monitor exception. We need to catch this to
  1828. * ensure we reset the PMC. In all cases the PMC will be 256 or less
  1829. * cycles from overflow.
  1830. *
  1831. * We only do this if the first pass fails to find any overflowing
  1832. * PMCs because a user might set a period of less than 256 and we
  1833. * don't want to mistakenly reset them.
  1834. */
  1835. if ((0x80000000 - val) <= 256)
  1836. return true;
  1837. return false;
  1838. }
  1839. static bool pmc_overflow(unsigned long val)
  1840. {
  1841. if ((int)val < 0)
  1842. return true;
  1843. return false;
  1844. }
  1845. /*
  1846. * Performance monitor interrupt stuff
  1847. */
  1848. static void perf_event_interrupt(struct pt_regs *regs)
  1849. {
  1850. int i, j;
  1851. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1852. struct perf_event *event;
  1853. unsigned long val[8];
  1854. int found, active;
  1855. int nmi;
  1856. if (cpuhw->n_limited)
  1857. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1858. mfspr(SPRN_PMC6));
  1859. perf_read_regs(regs);
  1860. nmi = perf_intr_is_nmi(regs);
  1861. if (nmi)
  1862. nmi_enter();
  1863. else
  1864. irq_enter();
  1865. /* Read all the PMCs since we'll need them a bunch of times */
  1866. for (i = 0; i < ppmu->n_counter; ++i)
  1867. val[i] = read_pmc(i + 1);
  1868. /* Try to find what caused the IRQ */
  1869. found = 0;
  1870. for (i = 0; i < ppmu->n_counter; ++i) {
  1871. if (!pmc_overflow(val[i]))
  1872. continue;
  1873. if (is_limited_pmc(i + 1))
  1874. continue; /* these won't generate IRQs */
  1875. /*
  1876. * We've found one that's overflowed. For active
  1877. * counters we need to log this. For inactive
  1878. * counters, we need to reset it anyway
  1879. */
  1880. found = 1;
  1881. active = 0;
  1882. for (j = 0; j < cpuhw->n_events; ++j) {
  1883. event = cpuhw->event[j];
  1884. if (event->hw.idx == (i + 1)) {
  1885. active = 1;
  1886. record_and_restart(event, val[i], regs);
  1887. break;
  1888. }
  1889. }
  1890. if (!active)
  1891. /* reset non active counters that have overflowed */
  1892. write_pmc(i + 1, 0);
  1893. }
  1894. if (!found && pvr_version_is(PVR_POWER7)) {
  1895. /* check active counters for special buggy p7 overflow */
  1896. for (i = 0; i < cpuhw->n_events; ++i) {
  1897. event = cpuhw->event[i];
  1898. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1899. continue;
  1900. if (pmc_overflow_power7(val[event->hw.idx - 1])) {
  1901. /* event has overflowed in a buggy way*/
  1902. found = 1;
  1903. record_and_restart(event,
  1904. val[event->hw.idx - 1],
  1905. regs);
  1906. }
  1907. }
  1908. }
  1909. if (!found && !nmi && printk_ratelimit())
  1910. printk(KERN_WARNING "Can't find PMC that caused IRQ\n");
  1911. /*
  1912. * Reset MMCR0 to its normal value. This will set PMXE and
  1913. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1914. * and thus allow interrupts to occur again.
  1915. * XXX might want to use MSR.PM to keep the events frozen until
  1916. * we get back out of this interrupt.
  1917. */
  1918. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1919. if (nmi)
  1920. nmi_exit();
  1921. else
  1922. irq_exit();
  1923. }
  1924. static int power_pmu_prepare_cpu(unsigned int cpu)
  1925. {
  1926. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1927. if (ppmu) {
  1928. memset(cpuhw, 0, sizeof(*cpuhw));
  1929. cpuhw->mmcr[0] = MMCR0_FC;
  1930. }
  1931. return 0;
  1932. }
  1933. int register_power_pmu(struct power_pmu *pmu)
  1934. {
  1935. if (ppmu)
  1936. return -EBUSY; /* something's already registered */
  1937. ppmu = pmu;
  1938. pr_info("%s performance monitor hardware support registered\n",
  1939. pmu->name);
  1940. power_pmu.attr_groups = ppmu->attr_groups;
  1941. #ifdef MSR_HV
  1942. /*
  1943. * Use FCHV to ignore kernel events if MSR.HV is set.
  1944. */
  1945. if (mfmsr() & MSR_HV)
  1946. freeze_events_kernel = MMCR0_FCHV;
  1947. #endif /* CONFIG_PPC64 */
  1948. perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
  1949. cpuhp_setup_state(CPUHP_PERF_POWER, "perf/powerpc:prepare",
  1950. power_pmu_prepare_cpu, NULL);
  1951. return 0;
  1952. }