core-book3s.c 56 KB

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