core-book3s.c 44 KB

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