core-book3s.c 54 KB

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