core-book3s.c 67 KB

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