core-book3s.c 54 KB

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