core-book3s.c 54 KB

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