core-book3s.c 44 KB

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