core-book3s.c 57 KB

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