perf_event.c 34 KB

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