core-book3s.c 43 KB

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