core-book3s.c 48 KB

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