core-book3s.c 44 KB

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