core-book3s.c 56 KB

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