core-book3s.c 54 KB

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