core-book3s.c 56 KB

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