core-book3s.c 55 KB

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