core-book3s.c 53 KB

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