core-book3s.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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 <asm/reg.h>
  17. #include <asm/pmc.h>
  18. #include <asm/machdep.h>
  19. #include <asm/firmware.h>
  20. #include <asm/ptrace.h>
  21. struct cpu_hw_events {
  22. int n_events;
  23. int n_percpu;
  24. int disabled;
  25. int n_added;
  26. int n_limited;
  27. u8 pmcs_enabled;
  28. struct perf_event *event[MAX_HWEVENTS];
  29. u64 events[MAX_HWEVENTS];
  30. unsigned int flags[MAX_HWEVENTS];
  31. unsigned long mmcr[3];
  32. struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
  33. u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS];
  34. u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  35. unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  36. unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
  37. unsigned int group_flag;
  38. int n_txn_start;
  39. };
  40. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  41. struct power_pmu *ppmu;
  42. /*
  43. * Normally, to ignore kernel events we set the FCS (freeze counters
  44. * in supervisor mode) bit in MMCR0, but if the kernel runs with the
  45. * hypervisor bit set in the MSR, or if we are running on a processor
  46. * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
  47. * then we need to use the FCHV bit to ignore kernel events.
  48. */
  49. static unsigned int freeze_events_kernel = MMCR0_FCS;
  50. /*
  51. * 32-bit doesn't have MMCRA but does have an MMCR2,
  52. * and a few other names are different.
  53. */
  54. #ifdef CONFIG_PPC32
  55. #define MMCR0_FCHV 0
  56. #define MMCR0_PMCjCE MMCR0_PMCnCE
  57. #define SPRN_MMCRA SPRN_MMCR2
  58. #define MMCRA_SAMPLE_ENABLE 0
  59. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  60. {
  61. return 0;
  62. }
  63. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
  64. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  65. {
  66. return 0;
  67. }
  68. static inline void perf_read_regs(struct pt_regs *regs) { }
  69. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  70. {
  71. return 0;
  72. }
  73. #endif /* CONFIG_PPC32 */
  74. /*
  75. * Things that are specific to 64-bit implementations.
  76. */
  77. #ifdef CONFIG_PPC64
  78. static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  79. {
  80. unsigned long mmcra = regs->dsisr;
  81. if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
  82. unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
  83. if (slot > 1)
  84. return 4 * (slot - 1);
  85. }
  86. return 0;
  87. }
  88. /*
  89. * The user wants a data address recorded.
  90. * If we're not doing instruction sampling, give them the SDAR
  91. * (sampled data address). If we are doing instruction sampling, then
  92. * only give them the SDAR if it corresponds to the instruction
  93. * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
  94. * bit in MMCRA.
  95. */
  96. static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
  97. {
  98. unsigned long mmcra = regs->dsisr;
  99. unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
  100. POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
  101. if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
  102. *addrp = mfspr(SPRN_SDAR);
  103. }
  104. static inline u32 perf_flags_from_msr(struct pt_regs *regs)
  105. {
  106. if (regs->msr & MSR_PR)
  107. return PERF_RECORD_MISC_USER;
  108. if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
  109. return PERF_RECORD_MISC_HYPERVISOR;
  110. return PERF_RECORD_MISC_KERNEL;
  111. }
  112. static inline u32 perf_get_misc_flags(struct pt_regs *regs)
  113. {
  114. unsigned long mmcra = regs->dsisr;
  115. unsigned long sihv = MMCRA_SIHV;
  116. unsigned long sipr = MMCRA_SIPR;
  117. /* Not a PMU interrupt: Make up flags from regs->msr */
  118. if (TRAP(regs) != 0xf00)
  119. return perf_flags_from_msr(regs);
  120. /*
  121. * If we don't support continuous sampling and this
  122. * is not a marked event, same deal
  123. */
  124. if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) &&
  125. !(mmcra & MMCRA_SAMPLE_ENABLE))
  126. return perf_flags_from_msr(regs);
  127. /*
  128. * If we don't have flags in MMCRA, rather than using
  129. * the MSR, we intuit the flags from the address in
  130. * SIAR which should give slightly more reliable
  131. * results
  132. */
  133. if (ppmu->flags & PPMU_NO_SIPR) {
  134. unsigned long siar = mfspr(SPRN_SIAR);
  135. if (siar >= PAGE_OFFSET)
  136. return PERF_RECORD_MISC_KERNEL;
  137. return PERF_RECORD_MISC_USER;
  138. }
  139. if (ppmu->flags & PPMU_ALT_SIPR) {
  140. sihv = POWER6_MMCRA_SIHV;
  141. sipr = POWER6_MMCRA_SIPR;
  142. }
  143. /* PR has priority over HV, so order below is important */
  144. if (mmcra & sipr)
  145. return PERF_RECORD_MISC_USER;
  146. if ((mmcra & sihv) && (freeze_events_kernel != MMCR0_FCHV))
  147. return PERF_RECORD_MISC_HYPERVISOR;
  148. return PERF_RECORD_MISC_KERNEL;
  149. }
  150. /*
  151. * Overload regs->dsisr to store MMCRA so we only need to read it once
  152. * on each interrupt.
  153. */
  154. static inline void perf_read_regs(struct pt_regs *regs)
  155. {
  156. regs->dsisr = mfspr(SPRN_MMCRA);
  157. }
  158. /*
  159. * If interrupts were soft-disabled when a PMU interrupt occurs, treat
  160. * it as an NMI.
  161. */
  162. static inline int perf_intr_is_nmi(struct pt_regs *regs)
  163. {
  164. return !regs->softe;
  165. }
  166. #endif /* CONFIG_PPC64 */
  167. static void perf_event_interrupt(struct pt_regs *regs);
  168. void perf_event_print_debug(void)
  169. {
  170. }
  171. /*
  172. * Read one performance monitor counter (PMC).
  173. */
  174. static unsigned long read_pmc(int idx)
  175. {
  176. unsigned long val;
  177. switch (idx) {
  178. case 1:
  179. val = mfspr(SPRN_PMC1);
  180. break;
  181. case 2:
  182. val = mfspr(SPRN_PMC2);
  183. break;
  184. case 3:
  185. val = mfspr(SPRN_PMC3);
  186. break;
  187. case 4:
  188. val = mfspr(SPRN_PMC4);
  189. break;
  190. case 5:
  191. val = mfspr(SPRN_PMC5);
  192. break;
  193. case 6:
  194. val = mfspr(SPRN_PMC6);
  195. break;
  196. #ifdef CONFIG_PPC64
  197. case 7:
  198. val = mfspr(SPRN_PMC7);
  199. break;
  200. case 8:
  201. val = mfspr(SPRN_PMC8);
  202. break;
  203. #endif /* CONFIG_PPC64 */
  204. default:
  205. printk(KERN_ERR "oops trying to read PMC%d\n", idx);
  206. val = 0;
  207. }
  208. return val;
  209. }
  210. /*
  211. * Write one PMC.
  212. */
  213. static void write_pmc(int idx, unsigned long val)
  214. {
  215. switch (idx) {
  216. case 1:
  217. mtspr(SPRN_PMC1, val);
  218. break;
  219. case 2:
  220. mtspr(SPRN_PMC2, val);
  221. break;
  222. case 3:
  223. mtspr(SPRN_PMC3, val);
  224. break;
  225. case 4:
  226. mtspr(SPRN_PMC4, val);
  227. break;
  228. case 5:
  229. mtspr(SPRN_PMC5, val);
  230. break;
  231. case 6:
  232. mtspr(SPRN_PMC6, val);
  233. break;
  234. #ifdef CONFIG_PPC64
  235. case 7:
  236. mtspr(SPRN_PMC7, val);
  237. break;
  238. case 8:
  239. mtspr(SPRN_PMC8, val);
  240. break;
  241. #endif /* CONFIG_PPC64 */
  242. default:
  243. printk(KERN_ERR "oops trying to write PMC%d\n", idx);
  244. }
  245. }
  246. /*
  247. * Check if a set of events can all go on the PMU at once.
  248. * If they can't, this will look at alternative codes for the events
  249. * and see if any combination of alternative codes is feasible.
  250. * The feasible set is returned in event_id[].
  251. */
  252. static int power_check_constraints(struct cpu_hw_events *cpuhw,
  253. u64 event_id[], unsigned int cflags[],
  254. int n_ev)
  255. {
  256. unsigned long mask, value, nv;
  257. unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
  258. int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
  259. int i, j;
  260. unsigned long addf = ppmu->add_fields;
  261. unsigned long tadd = ppmu->test_adder;
  262. if (n_ev > ppmu->n_counter)
  263. return -1;
  264. /* First see if the events will go on as-is */
  265. for (i = 0; i < n_ev; ++i) {
  266. if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
  267. && !ppmu->limited_pmc_event(event_id[i])) {
  268. ppmu->get_alternatives(event_id[i], cflags[i],
  269. cpuhw->alternatives[i]);
  270. event_id[i] = cpuhw->alternatives[i][0];
  271. }
  272. if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
  273. &cpuhw->avalues[i][0]))
  274. return -1;
  275. }
  276. value = mask = 0;
  277. for (i = 0; i < n_ev; ++i) {
  278. nv = (value | cpuhw->avalues[i][0]) +
  279. (value & cpuhw->avalues[i][0] & addf);
  280. if ((((nv + tadd) ^ value) & mask) != 0 ||
  281. (((nv + tadd) ^ cpuhw->avalues[i][0]) &
  282. cpuhw->amasks[i][0]) != 0)
  283. break;
  284. value = nv;
  285. mask |= cpuhw->amasks[i][0];
  286. }
  287. if (i == n_ev)
  288. return 0; /* all OK */
  289. /* doesn't work, gather alternatives... */
  290. if (!ppmu->get_alternatives)
  291. return -1;
  292. for (i = 0; i < n_ev; ++i) {
  293. choice[i] = 0;
  294. n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
  295. cpuhw->alternatives[i]);
  296. for (j = 1; j < n_alt[i]; ++j)
  297. ppmu->get_constraint(cpuhw->alternatives[i][j],
  298. &cpuhw->amasks[i][j],
  299. &cpuhw->avalues[i][j]);
  300. }
  301. /* enumerate all possibilities and see if any will work */
  302. i = 0;
  303. j = -1;
  304. value = mask = nv = 0;
  305. while (i < n_ev) {
  306. if (j >= 0) {
  307. /* we're backtracking, restore context */
  308. value = svalues[i];
  309. mask = smasks[i];
  310. j = choice[i];
  311. }
  312. /*
  313. * See if any alternative k for event_id i,
  314. * where k > j, will satisfy the constraints.
  315. */
  316. while (++j < n_alt[i]) {
  317. nv = (value | cpuhw->avalues[i][j]) +
  318. (value & cpuhw->avalues[i][j] & addf);
  319. if ((((nv + tadd) ^ value) & mask) == 0 &&
  320. (((nv + tadd) ^ cpuhw->avalues[i][j])
  321. & cpuhw->amasks[i][j]) == 0)
  322. break;
  323. }
  324. if (j >= n_alt[i]) {
  325. /*
  326. * No feasible alternative, backtrack
  327. * to event_id i-1 and continue enumerating its
  328. * alternatives from where we got up to.
  329. */
  330. if (--i < 0)
  331. return -1;
  332. } else {
  333. /*
  334. * Found a feasible alternative for event_id i,
  335. * remember where we got up to with this event_id,
  336. * go on to the next event_id, and start with
  337. * the first alternative for it.
  338. */
  339. choice[i] = j;
  340. svalues[i] = value;
  341. smasks[i] = mask;
  342. value = nv;
  343. mask |= cpuhw->amasks[i][j];
  344. ++i;
  345. j = -1;
  346. }
  347. }
  348. /* OK, we have a feasible combination, tell the caller the solution */
  349. for (i = 0; i < n_ev; ++i)
  350. event_id[i] = cpuhw->alternatives[i][choice[i]];
  351. return 0;
  352. }
  353. /*
  354. * Check if newly-added events have consistent settings for
  355. * exclude_{user,kernel,hv} with each other and any previously
  356. * added events.
  357. */
  358. static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
  359. int n_prev, int n_new)
  360. {
  361. int eu = 0, ek = 0, eh = 0;
  362. int i, n, first;
  363. struct perf_event *event;
  364. n = n_prev + n_new;
  365. if (n <= 1)
  366. return 0;
  367. first = 1;
  368. for (i = 0; i < n; ++i) {
  369. if (cflags[i] & PPMU_LIMITED_PMC_OK) {
  370. cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
  371. continue;
  372. }
  373. event = ctrs[i];
  374. if (first) {
  375. eu = event->attr.exclude_user;
  376. ek = event->attr.exclude_kernel;
  377. eh = event->attr.exclude_hv;
  378. first = 0;
  379. } else if (event->attr.exclude_user != eu ||
  380. event->attr.exclude_kernel != ek ||
  381. event->attr.exclude_hv != eh) {
  382. return -EAGAIN;
  383. }
  384. }
  385. if (eu || ek || eh)
  386. for (i = 0; i < n; ++i)
  387. if (cflags[i] & PPMU_LIMITED_PMC_OK)
  388. cflags[i] |= PPMU_LIMITED_PMC_REQD;
  389. return 0;
  390. }
  391. static u64 check_and_compute_delta(u64 prev, u64 val)
  392. {
  393. u64 delta = (val - prev) & 0xfffffffful;
  394. /*
  395. * POWER7 can roll back counter values, if the new value is smaller
  396. * than the previous value it will cause the delta and the counter to
  397. * have bogus values unless we rolled a counter over. If a coutner is
  398. * rolled back, it will be smaller, but within 256, which is the maximum
  399. * number of events to rollback at once. If we dectect a rollback
  400. * return 0. This can lead to a small lack of precision in the
  401. * counters.
  402. */
  403. if (prev > val && (prev - val) < 256)
  404. delta = 0;
  405. return delta;
  406. }
  407. static void power_pmu_read(struct perf_event *event)
  408. {
  409. s64 val, delta, prev;
  410. if (event->hw.state & PERF_HES_STOPPED)
  411. return;
  412. if (!event->hw.idx)
  413. return;
  414. /*
  415. * Performance monitor interrupts come even when interrupts
  416. * are soft-disabled, as long as interrupts are hard-enabled.
  417. * Therefore we treat them like NMIs.
  418. */
  419. do {
  420. prev = local64_read(&event->hw.prev_count);
  421. barrier();
  422. val = read_pmc(event->hw.idx);
  423. delta = check_and_compute_delta(prev, val);
  424. if (!delta)
  425. return;
  426. } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  427. local64_add(delta, &event->count);
  428. /*
  429. * A number of places program the PMC with (0x80000000 - period_left).
  430. * We never want period_left to be less than 1 because we will program
  431. * the PMC with a value >= 0x800000000 and an edge detected PMC will
  432. * roll around to 0 before taking an exception. We have seen this
  433. * on POWER8.
  434. *
  435. * To fix this, clamp the minimum value of period_left to 1.
  436. */
  437. do {
  438. prev = local64_read(&event->hw.period_left);
  439. val = prev - delta;
  440. if (val < 1)
  441. val = 1;
  442. } while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
  443. }
  444. /*
  445. * On some machines, PMC5 and PMC6 can't be written, don't respect
  446. * the freeze conditions, and don't generate interrupts. This tells
  447. * us if `event' is using such a PMC.
  448. */
  449. static int is_limited_pmc(int pmcnum)
  450. {
  451. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  452. && (pmcnum == 5 || pmcnum == 6);
  453. }
  454. static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
  455. unsigned long pmc5, unsigned long pmc6)
  456. {
  457. struct perf_event *event;
  458. u64 val, prev, delta;
  459. int i;
  460. for (i = 0; i < cpuhw->n_limited; ++i) {
  461. event = cpuhw->limited_counter[i];
  462. if (!event->hw.idx)
  463. continue;
  464. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  465. prev = local64_read(&event->hw.prev_count);
  466. event->hw.idx = 0;
  467. delta = check_and_compute_delta(prev, val);
  468. if (delta)
  469. local64_add(delta, &event->count);
  470. }
  471. }
  472. static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
  473. unsigned long pmc5, unsigned long pmc6)
  474. {
  475. struct perf_event *event;
  476. u64 val, prev;
  477. int i;
  478. for (i = 0; i < cpuhw->n_limited; ++i) {
  479. event = cpuhw->limited_counter[i];
  480. event->hw.idx = cpuhw->limited_hwidx[i];
  481. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  482. prev = local64_read(&event->hw.prev_count);
  483. if (check_and_compute_delta(prev, val))
  484. local64_set(&event->hw.prev_count, val);
  485. perf_event_update_userpage(event);
  486. }
  487. }
  488. /*
  489. * Since limited events don't respect the freeze conditions, we
  490. * have to read them immediately after freezing or unfreezing the
  491. * other events. We try to keep the values from the limited
  492. * events as consistent as possible by keeping the delay (in
  493. * cycles and instructions) between freezing/unfreezing and reading
  494. * the limited events as small and consistent as possible.
  495. * Therefore, if any limited events are in use, we read them
  496. * both, and always in the same order, to minimize variability,
  497. * and do it inside the same asm that writes MMCR0.
  498. */
  499. static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
  500. {
  501. unsigned long pmc5, pmc6;
  502. if (!cpuhw->n_limited) {
  503. mtspr(SPRN_MMCR0, mmcr0);
  504. return;
  505. }
  506. /*
  507. * Write MMCR0, then read PMC5 and PMC6 immediately.
  508. * To ensure we don't get a performance monitor interrupt
  509. * between writing MMCR0 and freezing/thawing the limited
  510. * events, we first write MMCR0 with the event overflow
  511. * interrupt enable bits turned off.
  512. */
  513. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  514. : "=&r" (pmc5), "=&r" (pmc6)
  515. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  516. "i" (SPRN_MMCR0),
  517. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  518. if (mmcr0 & MMCR0_FC)
  519. freeze_limited_counters(cpuhw, pmc5, pmc6);
  520. else
  521. thaw_limited_counters(cpuhw, pmc5, pmc6);
  522. /*
  523. * Write the full MMCR0 including the event overflow interrupt
  524. * enable bits, if necessary.
  525. */
  526. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  527. mtspr(SPRN_MMCR0, mmcr0);
  528. }
  529. /*
  530. * Disable all events to prevent PMU interrupts and to allow
  531. * events to be added or removed.
  532. */
  533. static void power_pmu_disable(struct pmu *pmu)
  534. {
  535. struct cpu_hw_events *cpuhw;
  536. unsigned long flags;
  537. if (!ppmu)
  538. return;
  539. local_irq_save(flags);
  540. cpuhw = &__get_cpu_var(cpu_hw_events);
  541. if (!cpuhw->disabled) {
  542. cpuhw->disabled = 1;
  543. cpuhw->n_added = 0;
  544. /*
  545. * Check if we ever enabled the PMU on this cpu.
  546. */
  547. if (!cpuhw->pmcs_enabled) {
  548. ppc_enable_pmcs();
  549. cpuhw->pmcs_enabled = 1;
  550. }
  551. /*
  552. * Disable instruction sampling if it was enabled
  553. */
  554. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  555. mtspr(SPRN_MMCRA,
  556. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  557. mb();
  558. }
  559. /*
  560. * Set the 'freeze counters' bit.
  561. * The barrier is to make sure the mtspr has been
  562. * executed and the PMU has frozen the events
  563. * before we return.
  564. */
  565. write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
  566. mb();
  567. }
  568. local_irq_restore(flags);
  569. }
  570. /*
  571. * Re-enable all events if disable == 0.
  572. * If we were previously disabled and events were added, then
  573. * put the new config on the PMU.
  574. */
  575. static void power_pmu_enable(struct pmu *pmu)
  576. {
  577. struct perf_event *event;
  578. struct cpu_hw_events *cpuhw;
  579. unsigned long flags;
  580. long i;
  581. unsigned long val;
  582. s64 left;
  583. unsigned int hwc_index[MAX_HWEVENTS];
  584. int n_lim;
  585. int idx;
  586. if (!ppmu)
  587. return;
  588. local_irq_save(flags);
  589. cpuhw = &__get_cpu_var(cpu_hw_events);
  590. if (!cpuhw->disabled) {
  591. local_irq_restore(flags);
  592. return;
  593. }
  594. cpuhw->disabled = 0;
  595. /*
  596. * If we didn't change anything, or only removed events,
  597. * no need to recalculate MMCR* settings and reset the PMCs.
  598. * Just reenable the PMU with the current MMCR* settings
  599. * (possibly updated for removal of events).
  600. */
  601. if (!cpuhw->n_added) {
  602. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  603. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  604. if (cpuhw->n_events == 0)
  605. ppc_set_pmu_inuse(0);
  606. goto out_enable;
  607. }
  608. /*
  609. * Compute MMCR* values for the new set of events
  610. */
  611. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
  612. cpuhw->mmcr)) {
  613. /* shouldn't ever get here */
  614. printk(KERN_ERR "oops compute_mmcr failed\n");
  615. goto out;
  616. }
  617. /*
  618. * Add in MMCR0 freeze bits corresponding to the
  619. * attr.exclude_* bits for the first event.
  620. * We have already checked that all events have the
  621. * same values for these bits as the first event.
  622. */
  623. event = cpuhw->event[0];
  624. if (event->attr.exclude_user)
  625. cpuhw->mmcr[0] |= MMCR0_FCP;
  626. if (event->attr.exclude_kernel)
  627. cpuhw->mmcr[0] |= freeze_events_kernel;
  628. if (event->attr.exclude_hv)
  629. cpuhw->mmcr[0] |= MMCR0_FCHV;
  630. /*
  631. * Write the new configuration to MMCR* with the freeze
  632. * bit set and set the hardware events to their initial values.
  633. * Then unfreeze the events.
  634. */
  635. ppc_set_pmu_inuse(1);
  636. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  637. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  638. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  639. | MMCR0_FC);
  640. /*
  641. * Read off any pre-existing events that need to move
  642. * to another PMC.
  643. */
  644. for (i = 0; i < cpuhw->n_events; ++i) {
  645. event = cpuhw->event[i];
  646. if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
  647. power_pmu_read(event);
  648. write_pmc(event->hw.idx, 0);
  649. event->hw.idx = 0;
  650. }
  651. }
  652. /*
  653. * Initialize the PMCs for all the new and moved events.
  654. */
  655. cpuhw->n_limited = n_lim = 0;
  656. for (i = 0; i < cpuhw->n_events; ++i) {
  657. event = cpuhw->event[i];
  658. if (event->hw.idx)
  659. continue;
  660. idx = hwc_index[i] + 1;
  661. if (is_limited_pmc(idx)) {
  662. cpuhw->limited_counter[n_lim] = event;
  663. cpuhw->limited_hwidx[n_lim] = idx;
  664. ++n_lim;
  665. continue;
  666. }
  667. val = 0;
  668. if (event->hw.sample_period) {
  669. left = local64_read(&event->hw.period_left);
  670. if (left < 0x80000000L)
  671. val = 0x80000000L - left;
  672. }
  673. local64_set(&event->hw.prev_count, val);
  674. event->hw.idx = idx;
  675. if (event->hw.state & PERF_HES_STOPPED)
  676. val = 0;
  677. write_pmc(idx, val);
  678. perf_event_update_userpage(event);
  679. }
  680. cpuhw->n_limited = n_lim;
  681. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  682. out_enable:
  683. mb();
  684. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  685. /*
  686. * Enable instruction sampling if necessary
  687. */
  688. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  689. mb();
  690. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  691. }
  692. out:
  693. local_irq_restore(flags);
  694. }
  695. static int collect_events(struct perf_event *group, int max_count,
  696. struct perf_event *ctrs[], u64 *events,
  697. unsigned int *flags)
  698. {
  699. int n = 0;
  700. struct perf_event *event;
  701. if (!is_software_event(group)) {
  702. if (n >= max_count)
  703. return -1;
  704. ctrs[n] = group;
  705. flags[n] = group->hw.event_base;
  706. events[n++] = group->hw.config;
  707. }
  708. list_for_each_entry(event, &group->sibling_list, group_entry) {
  709. if (!is_software_event(event) &&
  710. event->state != PERF_EVENT_STATE_OFF) {
  711. if (n >= max_count)
  712. return -1;
  713. ctrs[n] = event;
  714. flags[n] = event->hw.event_base;
  715. events[n++] = event->hw.config;
  716. }
  717. }
  718. return n;
  719. }
  720. /*
  721. * Add a event to the PMU.
  722. * If all events are not already frozen, then we disable and
  723. * re-enable the PMU in order to get hw_perf_enable to do the
  724. * actual work of reconfiguring the PMU.
  725. */
  726. static int power_pmu_add(struct perf_event *event, int ef_flags)
  727. {
  728. struct cpu_hw_events *cpuhw;
  729. unsigned long flags;
  730. int n0;
  731. int ret = -EAGAIN;
  732. local_irq_save(flags);
  733. perf_pmu_disable(event->pmu);
  734. /*
  735. * Add the event to the list (if there is room)
  736. * and check whether the total set is still feasible.
  737. */
  738. cpuhw = &__get_cpu_var(cpu_hw_events);
  739. n0 = cpuhw->n_events;
  740. if (n0 >= ppmu->n_counter)
  741. goto out;
  742. cpuhw->event[n0] = event;
  743. cpuhw->events[n0] = event->hw.config;
  744. cpuhw->flags[n0] = event->hw.event_base;
  745. if (!(ef_flags & PERF_EF_START))
  746. event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  747. /*
  748. * If group events scheduling transaction was started,
  749. * skip the schedulability test here, it will be performed
  750. * at commit time(->commit_txn) as a whole
  751. */
  752. if (cpuhw->group_flag & PERF_EVENT_TXN)
  753. goto nocheck;
  754. if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
  755. goto out;
  756. if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
  757. goto out;
  758. event->hw.config = cpuhw->events[n0];
  759. nocheck:
  760. ++cpuhw->n_events;
  761. ++cpuhw->n_added;
  762. ret = 0;
  763. out:
  764. perf_pmu_enable(event->pmu);
  765. local_irq_restore(flags);
  766. return ret;
  767. }
  768. /*
  769. * Remove a event from the PMU.
  770. */
  771. static void power_pmu_del(struct perf_event *event, int ef_flags)
  772. {
  773. struct cpu_hw_events *cpuhw;
  774. long i;
  775. unsigned long flags;
  776. local_irq_save(flags);
  777. perf_pmu_disable(event->pmu);
  778. power_pmu_read(event);
  779. cpuhw = &__get_cpu_var(cpu_hw_events);
  780. for (i = 0; i < cpuhw->n_events; ++i) {
  781. if (event == cpuhw->event[i]) {
  782. while (++i < cpuhw->n_events) {
  783. cpuhw->event[i-1] = cpuhw->event[i];
  784. cpuhw->events[i-1] = cpuhw->events[i];
  785. cpuhw->flags[i-1] = cpuhw->flags[i];
  786. }
  787. --cpuhw->n_events;
  788. ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
  789. if (event->hw.idx) {
  790. write_pmc(event->hw.idx, 0);
  791. event->hw.idx = 0;
  792. }
  793. perf_event_update_userpage(event);
  794. break;
  795. }
  796. }
  797. for (i = 0; i < cpuhw->n_limited; ++i)
  798. if (event == cpuhw->limited_counter[i])
  799. break;
  800. if (i < cpuhw->n_limited) {
  801. while (++i < cpuhw->n_limited) {
  802. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  803. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  804. }
  805. --cpuhw->n_limited;
  806. }
  807. if (cpuhw->n_events == 0) {
  808. /* disable exceptions if no events are running */
  809. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  810. }
  811. perf_pmu_enable(event->pmu);
  812. local_irq_restore(flags);
  813. }
  814. /*
  815. * POWER-PMU does not support disabling individual counters, hence
  816. * program their cycle counter to their max value and ignore the interrupts.
  817. */
  818. static void power_pmu_start(struct perf_event *event, int ef_flags)
  819. {
  820. unsigned long flags;
  821. s64 left;
  822. unsigned long val;
  823. if (!event->hw.idx || !event->hw.sample_period)
  824. return;
  825. if (!(event->hw.state & PERF_HES_STOPPED))
  826. return;
  827. if (ef_flags & PERF_EF_RELOAD)
  828. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  829. local_irq_save(flags);
  830. perf_pmu_disable(event->pmu);
  831. event->hw.state = 0;
  832. left = local64_read(&event->hw.period_left);
  833. val = 0;
  834. if (left < 0x80000000L)
  835. val = 0x80000000L - left;
  836. write_pmc(event->hw.idx, val);
  837. perf_event_update_userpage(event);
  838. perf_pmu_enable(event->pmu);
  839. local_irq_restore(flags);
  840. }
  841. static void power_pmu_stop(struct perf_event *event, int ef_flags)
  842. {
  843. unsigned long flags;
  844. if (!event->hw.idx || !event->hw.sample_period)
  845. return;
  846. if (event->hw.state & PERF_HES_STOPPED)
  847. return;
  848. local_irq_save(flags);
  849. perf_pmu_disable(event->pmu);
  850. power_pmu_read(event);
  851. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  852. write_pmc(event->hw.idx, 0);
  853. perf_event_update_userpage(event);
  854. perf_pmu_enable(event->pmu);
  855. local_irq_restore(flags);
  856. }
  857. /*
  858. * Start group events scheduling transaction
  859. * Set the flag to make pmu::enable() not perform the
  860. * schedulability test, it will be performed at commit time
  861. */
  862. void power_pmu_start_txn(struct pmu *pmu)
  863. {
  864. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  865. perf_pmu_disable(pmu);
  866. cpuhw->group_flag |= PERF_EVENT_TXN;
  867. cpuhw->n_txn_start = cpuhw->n_events;
  868. }
  869. /*
  870. * Stop group events scheduling transaction
  871. * Clear the flag and pmu::enable() will perform the
  872. * schedulability test.
  873. */
  874. void power_pmu_cancel_txn(struct pmu *pmu)
  875. {
  876. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  877. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  878. perf_pmu_enable(pmu);
  879. }
  880. /*
  881. * Commit group events scheduling transaction
  882. * Perform the group schedulability test as a whole
  883. * Return 0 if success
  884. */
  885. int power_pmu_commit_txn(struct pmu *pmu)
  886. {
  887. struct cpu_hw_events *cpuhw;
  888. long i, n;
  889. if (!ppmu)
  890. return -EAGAIN;
  891. cpuhw = &__get_cpu_var(cpu_hw_events);
  892. n = cpuhw->n_events;
  893. if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
  894. return -EAGAIN;
  895. i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
  896. if (i < 0)
  897. return -EAGAIN;
  898. for (i = cpuhw->n_txn_start; i < n; ++i)
  899. cpuhw->event[i]->hw.config = cpuhw->events[i];
  900. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  901. perf_pmu_enable(pmu);
  902. return 0;
  903. }
  904. /*
  905. * Return 1 if we might be able to put event on a limited PMC,
  906. * or 0 if not.
  907. * A event can only go on a limited PMC if it counts something
  908. * that a limited PMC can count, doesn't require interrupts, and
  909. * doesn't exclude any processor mode.
  910. */
  911. static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
  912. unsigned int flags)
  913. {
  914. int n;
  915. u64 alt[MAX_EVENT_ALTERNATIVES];
  916. if (event->attr.exclude_user
  917. || event->attr.exclude_kernel
  918. || event->attr.exclude_hv
  919. || event->attr.sample_period)
  920. return 0;
  921. if (ppmu->limited_pmc_event(ev))
  922. return 1;
  923. /*
  924. * The requested event_id isn't on a limited PMC already;
  925. * see if any alternative code goes on a limited PMC.
  926. */
  927. if (!ppmu->get_alternatives)
  928. return 0;
  929. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  930. n = ppmu->get_alternatives(ev, flags, alt);
  931. return n > 0;
  932. }
  933. /*
  934. * Find an alternative event_id that goes on a normal PMC, if possible,
  935. * and return the event_id code, or 0 if there is no such alternative.
  936. * (Note: event_id code 0 is "don't count" on all machines.)
  937. */
  938. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  939. {
  940. u64 alt[MAX_EVENT_ALTERNATIVES];
  941. int n;
  942. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  943. n = ppmu->get_alternatives(ev, flags, alt);
  944. if (!n)
  945. return 0;
  946. return alt[0];
  947. }
  948. /* Number of perf_events counting hardware events */
  949. static atomic_t num_events;
  950. /* Used to avoid races in calling reserve/release_pmc_hardware */
  951. static DEFINE_MUTEX(pmc_reserve_mutex);
  952. /*
  953. * Release the PMU if this is the last perf_event.
  954. */
  955. static void hw_perf_event_destroy(struct perf_event *event)
  956. {
  957. if (!atomic_add_unless(&num_events, -1, 1)) {
  958. mutex_lock(&pmc_reserve_mutex);
  959. if (atomic_dec_return(&num_events) == 0)
  960. release_pmc_hardware();
  961. mutex_unlock(&pmc_reserve_mutex);
  962. }
  963. }
  964. /*
  965. * Translate a generic cache event_id config to a raw event_id code.
  966. */
  967. static int hw_perf_cache_event(u64 config, u64 *eventp)
  968. {
  969. unsigned long type, op, result;
  970. int ev;
  971. if (!ppmu->cache_events)
  972. return -EINVAL;
  973. /* unpack config */
  974. type = config & 0xff;
  975. op = (config >> 8) & 0xff;
  976. result = (config >> 16) & 0xff;
  977. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  978. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  979. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  980. return -EINVAL;
  981. ev = (*ppmu->cache_events)[type][op][result];
  982. if (ev == 0)
  983. return -EOPNOTSUPP;
  984. if (ev == -1)
  985. return -EINVAL;
  986. *eventp = ev;
  987. return 0;
  988. }
  989. static int power_pmu_event_init(struct perf_event *event)
  990. {
  991. u64 ev;
  992. unsigned long flags;
  993. struct perf_event *ctrs[MAX_HWEVENTS];
  994. u64 events[MAX_HWEVENTS];
  995. unsigned int cflags[MAX_HWEVENTS];
  996. int n;
  997. int err;
  998. struct cpu_hw_events *cpuhw;
  999. if (!ppmu)
  1000. return -ENOENT;
  1001. /* does not support taken branch sampling */
  1002. if (has_branch_stack(event))
  1003. return -EOPNOTSUPP;
  1004. switch (event->attr.type) {
  1005. case PERF_TYPE_HARDWARE:
  1006. ev = event->attr.config;
  1007. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  1008. return -EOPNOTSUPP;
  1009. ev = ppmu->generic_events[ev];
  1010. break;
  1011. case PERF_TYPE_HW_CACHE:
  1012. err = hw_perf_cache_event(event->attr.config, &ev);
  1013. if (err)
  1014. return err;
  1015. break;
  1016. case PERF_TYPE_RAW:
  1017. ev = event->attr.config;
  1018. break;
  1019. default:
  1020. return -ENOENT;
  1021. }
  1022. event->hw.config_base = ev;
  1023. event->hw.idx = 0;
  1024. /*
  1025. * If we are not running on a hypervisor, force the
  1026. * exclude_hv bit to 0 so that we don't care what
  1027. * the user set it to.
  1028. */
  1029. if (!firmware_has_feature(FW_FEATURE_LPAR))
  1030. event->attr.exclude_hv = 0;
  1031. /*
  1032. * If this is a per-task event, then we can use
  1033. * PM_RUN_* events interchangeably with their non RUN_*
  1034. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  1035. * XXX we should check if the task is an idle task.
  1036. */
  1037. flags = 0;
  1038. if (event->attach_state & PERF_ATTACH_TASK)
  1039. flags |= PPMU_ONLY_COUNT_RUN;
  1040. /*
  1041. * If this machine has limited events, check whether this
  1042. * event_id could go on a limited event.
  1043. */
  1044. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  1045. if (can_go_on_limited_pmc(event, ev, flags)) {
  1046. flags |= PPMU_LIMITED_PMC_OK;
  1047. } else if (ppmu->limited_pmc_event(ev)) {
  1048. /*
  1049. * The requested event_id is on a limited PMC,
  1050. * but we can't use a limited PMC; see if any
  1051. * alternative goes on a normal PMC.
  1052. */
  1053. ev = normal_pmc_alternative(ev, flags);
  1054. if (!ev)
  1055. return -EINVAL;
  1056. }
  1057. }
  1058. /*
  1059. * If this is in a group, check if it can go on with all the
  1060. * other hardware events in the group. We assume the event
  1061. * hasn't been linked into its leader's sibling list at this point.
  1062. */
  1063. n = 0;
  1064. if (event->group_leader != event) {
  1065. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  1066. ctrs, events, cflags);
  1067. if (n < 0)
  1068. return -EINVAL;
  1069. }
  1070. events[n] = ev;
  1071. ctrs[n] = event;
  1072. cflags[n] = flags;
  1073. if (check_excludes(ctrs, cflags, n, 1))
  1074. return -EINVAL;
  1075. cpuhw = &get_cpu_var(cpu_hw_events);
  1076. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  1077. put_cpu_var(cpu_hw_events);
  1078. if (err)
  1079. return -EINVAL;
  1080. event->hw.config = events[n];
  1081. event->hw.event_base = cflags[n];
  1082. event->hw.last_period = event->hw.sample_period;
  1083. local64_set(&event->hw.period_left, event->hw.last_period);
  1084. /*
  1085. * See if we need to reserve the PMU.
  1086. * If no events are currently in use, then we have to take a
  1087. * mutex to ensure that we don't race with another task doing
  1088. * reserve_pmc_hardware or release_pmc_hardware.
  1089. */
  1090. err = 0;
  1091. if (!atomic_inc_not_zero(&num_events)) {
  1092. mutex_lock(&pmc_reserve_mutex);
  1093. if (atomic_read(&num_events) == 0 &&
  1094. reserve_pmc_hardware(perf_event_interrupt))
  1095. err = -EBUSY;
  1096. else
  1097. atomic_inc(&num_events);
  1098. mutex_unlock(&pmc_reserve_mutex);
  1099. }
  1100. event->destroy = hw_perf_event_destroy;
  1101. return err;
  1102. }
  1103. static int power_pmu_event_idx(struct perf_event *event)
  1104. {
  1105. return event->hw.idx;
  1106. }
  1107. struct pmu power_pmu = {
  1108. .pmu_enable = power_pmu_enable,
  1109. .pmu_disable = power_pmu_disable,
  1110. .event_init = power_pmu_event_init,
  1111. .add = power_pmu_add,
  1112. .del = power_pmu_del,
  1113. .start = power_pmu_start,
  1114. .stop = power_pmu_stop,
  1115. .read = power_pmu_read,
  1116. .start_txn = power_pmu_start_txn,
  1117. .cancel_txn = power_pmu_cancel_txn,
  1118. .commit_txn = power_pmu_commit_txn,
  1119. .event_idx = power_pmu_event_idx,
  1120. };
  1121. /*
  1122. * A counter has overflowed; update its count and record
  1123. * things if requested. Note that interrupts are hard-disabled
  1124. * here so there is no possibility of being interrupted.
  1125. */
  1126. static void record_and_restart(struct perf_event *event, unsigned long val,
  1127. struct pt_regs *regs)
  1128. {
  1129. u64 period = event->hw.sample_period;
  1130. s64 prev, delta, left;
  1131. int record = 0;
  1132. if (event->hw.state & PERF_HES_STOPPED) {
  1133. write_pmc(event->hw.idx, 0);
  1134. return;
  1135. }
  1136. /* we don't have to worry about interrupts here */
  1137. prev = local64_read(&event->hw.prev_count);
  1138. delta = check_and_compute_delta(prev, val);
  1139. local64_add(delta, &event->count);
  1140. /*
  1141. * See if the total period for this event has expired,
  1142. * and update for the next period.
  1143. */
  1144. val = 0;
  1145. left = local64_read(&event->hw.period_left) - delta;
  1146. if (period) {
  1147. if (left <= 0) {
  1148. left += period;
  1149. if (left <= 0)
  1150. left = period;
  1151. record = 1;
  1152. event->hw.last_period = event->hw.sample_period;
  1153. }
  1154. if (left < 0x80000000LL)
  1155. val = 0x80000000LL - left;
  1156. }
  1157. write_pmc(event->hw.idx, val);
  1158. local64_set(&event->hw.prev_count, val);
  1159. local64_set(&event->hw.period_left, left);
  1160. perf_event_update_userpage(event);
  1161. /*
  1162. * Finally record data if requested.
  1163. */
  1164. if (record) {
  1165. struct perf_sample_data data;
  1166. perf_sample_data_init(&data, ~0ULL);
  1167. data.period = event->hw.last_period;
  1168. if (event->attr.sample_type & PERF_SAMPLE_ADDR)
  1169. perf_get_data_addr(regs, &data.addr);
  1170. if (perf_event_overflow(event, &data, regs))
  1171. power_pmu_stop(event, 0);
  1172. }
  1173. }
  1174. /*
  1175. * Called from generic code to get the misc flags (i.e. processor mode)
  1176. * for an event_id.
  1177. */
  1178. unsigned long perf_misc_flags(struct pt_regs *regs)
  1179. {
  1180. u32 flags = perf_get_misc_flags(regs);
  1181. if (flags)
  1182. return flags;
  1183. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1184. PERF_RECORD_MISC_KERNEL;
  1185. }
  1186. /*
  1187. * Called from generic code to get the instruction pointer
  1188. * for an event_id.
  1189. */
  1190. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1191. {
  1192. unsigned long mmcra = regs->dsisr;
  1193. /* Not a PMU interrupt */
  1194. if (TRAP(regs) != 0xf00)
  1195. return regs->nip;
  1196. /* Processor doesn't support sampling non marked events */
  1197. if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) &&
  1198. !(mmcra & MMCRA_SAMPLE_ENABLE))
  1199. return regs->nip;
  1200. return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1201. }
  1202. static bool pmc_overflow(unsigned long val)
  1203. {
  1204. if ((int)val < 0)
  1205. return true;
  1206. /*
  1207. * Events on POWER7 can roll back if a speculative event doesn't
  1208. * eventually complete. Unfortunately in some rare cases they will
  1209. * raise a performance monitor exception. We need to catch this to
  1210. * ensure we reset the PMC. In all cases the PMC will be 256 or less
  1211. * cycles from overflow.
  1212. *
  1213. * We only do this if the first pass fails to find any overflowing
  1214. * PMCs because a user might set a period of less than 256 and we
  1215. * don't want to mistakenly reset them.
  1216. */
  1217. if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256))
  1218. return true;
  1219. return false;
  1220. }
  1221. /*
  1222. * Performance monitor interrupt stuff
  1223. */
  1224. static void perf_event_interrupt(struct pt_regs *regs)
  1225. {
  1226. int i;
  1227. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1228. struct perf_event *event;
  1229. unsigned long val;
  1230. int found = 0;
  1231. int nmi;
  1232. if (cpuhw->n_limited)
  1233. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1234. mfspr(SPRN_PMC6));
  1235. perf_read_regs(regs);
  1236. nmi = perf_intr_is_nmi(regs);
  1237. if (nmi)
  1238. nmi_enter();
  1239. else
  1240. irq_enter();
  1241. for (i = 0; i < cpuhw->n_events; ++i) {
  1242. event = cpuhw->event[i];
  1243. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1244. continue;
  1245. val = read_pmc(event->hw.idx);
  1246. if ((int)val < 0) {
  1247. /* event has overflowed */
  1248. found = 1;
  1249. record_and_restart(event, val, regs);
  1250. }
  1251. }
  1252. /*
  1253. * In case we didn't find and reset the event that caused
  1254. * the interrupt, scan all events and reset any that are
  1255. * negative, to avoid getting continual interrupts.
  1256. * Any that we processed in the previous loop will not be negative.
  1257. */
  1258. if (!found) {
  1259. for (i = 0; i < ppmu->n_counter; ++i) {
  1260. if (is_limited_pmc(i + 1))
  1261. continue;
  1262. val = read_pmc(i + 1);
  1263. if (pmc_overflow(val))
  1264. write_pmc(i + 1, 0);
  1265. }
  1266. }
  1267. /*
  1268. * Reset MMCR0 to its normal value. This will set PMXE and
  1269. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1270. * and thus allow interrupts to occur again.
  1271. * XXX might want to use MSR.PM to keep the events frozen until
  1272. * we get back out of this interrupt.
  1273. */
  1274. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1275. if (nmi)
  1276. nmi_exit();
  1277. else
  1278. irq_exit();
  1279. }
  1280. static void power_pmu_setup(int cpu)
  1281. {
  1282. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1283. if (!ppmu)
  1284. return;
  1285. memset(cpuhw, 0, sizeof(*cpuhw));
  1286. cpuhw->mmcr[0] = MMCR0_FC;
  1287. }
  1288. static int __cpuinit
  1289. power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1290. {
  1291. unsigned int cpu = (long)hcpu;
  1292. switch (action & ~CPU_TASKS_FROZEN) {
  1293. case CPU_UP_PREPARE:
  1294. power_pmu_setup(cpu);
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return NOTIFY_OK;
  1300. }
  1301. int __cpuinit register_power_pmu(struct power_pmu *pmu)
  1302. {
  1303. if (ppmu)
  1304. return -EBUSY; /* something's already registered */
  1305. ppmu = pmu;
  1306. pr_info("%s performance monitor hardware support registered\n",
  1307. pmu->name);
  1308. #ifdef MSR_HV
  1309. /*
  1310. * Use FCHV to ignore kernel events if MSR.HV is set.
  1311. */
  1312. if (mfmsr() & MSR_HV)
  1313. freeze_events_kernel = MMCR0_FCHV;
  1314. #endif /* CONFIG_PPC64 */
  1315. perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
  1316. perf_cpu_notifier(power_pmu_notifier);
  1317. return 0;
  1318. }