core-book3s.c 55 KB

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