core-book3s.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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. local64_sub(delta, &event->hw.period_left);
  429. }
  430. /*
  431. * On some machines, PMC5 and PMC6 can't be written, don't respect
  432. * the freeze conditions, and don't generate interrupts. This tells
  433. * us if `event' is using such a PMC.
  434. */
  435. static int is_limited_pmc(int pmcnum)
  436. {
  437. return (ppmu->flags & PPMU_LIMITED_PMC5_6)
  438. && (pmcnum == 5 || pmcnum == 6);
  439. }
  440. static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
  441. unsigned long pmc5, unsigned long pmc6)
  442. {
  443. struct perf_event *event;
  444. u64 val, prev, delta;
  445. int i;
  446. for (i = 0; i < cpuhw->n_limited; ++i) {
  447. event = cpuhw->limited_counter[i];
  448. if (!event->hw.idx)
  449. continue;
  450. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  451. prev = local64_read(&event->hw.prev_count);
  452. event->hw.idx = 0;
  453. delta = check_and_compute_delta(prev, val);
  454. if (delta)
  455. local64_add(delta, &event->count);
  456. }
  457. }
  458. static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
  459. unsigned long pmc5, unsigned long pmc6)
  460. {
  461. struct perf_event *event;
  462. u64 val, prev;
  463. int i;
  464. for (i = 0; i < cpuhw->n_limited; ++i) {
  465. event = cpuhw->limited_counter[i];
  466. event->hw.idx = cpuhw->limited_hwidx[i];
  467. val = (event->hw.idx == 5) ? pmc5 : pmc6;
  468. prev = local64_read(&event->hw.prev_count);
  469. if (check_and_compute_delta(prev, val))
  470. local64_set(&event->hw.prev_count, val);
  471. perf_event_update_userpage(event);
  472. }
  473. }
  474. /*
  475. * Since limited events don't respect the freeze conditions, we
  476. * have to read them immediately after freezing or unfreezing the
  477. * other events. We try to keep the values from the limited
  478. * events as consistent as possible by keeping the delay (in
  479. * cycles and instructions) between freezing/unfreezing and reading
  480. * the limited events as small and consistent as possible.
  481. * Therefore, if any limited events are in use, we read them
  482. * both, and always in the same order, to minimize variability,
  483. * and do it inside the same asm that writes MMCR0.
  484. */
  485. static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
  486. {
  487. unsigned long pmc5, pmc6;
  488. if (!cpuhw->n_limited) {
  489. mtspr(SPRN_MMCR0, mmcr0);
  490. return;
  491. }
  492. /*
  493. * Write MMCR0, then read PMC5 and PMC6 immediately.
  494. * To ensure we don't get a performance monitor interrupt
  495. * between writing MMCR0 and freezing/thawing the limited
  496. * events, we first write MMCR0 with the event overflow
  497. * interrupt enable bits turned off.
  498. */
  499. asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
  500. : "=&r" (pmc5), "=&r" (pmc6)
  501. : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
  502. "i" (SPRN_MMCR0),
  503. "i" (SPRN_PMC5), "i" (SPRN_PMC6));
  504. if (mmcr0 & MMCR0_FC)
  505. freeze_limited_counters(cpuhw, pmc5, pmc6);
  506. else
  507. thaw_limited_counters(cpuhw, pmc5, pmc6);
  508. /*
  509. * Write the full MMCR0 including the event overflow interrupt
  510. * enable bits, if necessary.
  511. */
  512. if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
  513. mtspr(SPRN_MMCR0, mmcr0);
  514. }
  515. /*
  516. * Disable all events to prevent PMU interrupts and to allow
  517. * events to be added or removed.
  518. */
  519. static void power_pmu_disable(struct pmu *pmu)
  520. {
  521. struct cpu_hw_events *cpuhw;
  522. unsigned long flags;
  523. if (!ppmu)
  524. return;
  525. local_irq_save(flags);
  526. cpuhw = &__get_cpu_var(cpu_hw_events);
  527. if (!cpuhw->disabled) {
  528. cpuhw->disabled = 1;
  529. cpuhw->n_added = 0;
  530. /*
  531. * Check if we ever enabled the PMU on this cpu.
  532. */
  533. if (!cpuhw->pmcs_enabled) {
  534. ppc_enable_pmcs();
  535. cpuhw->pmcs_enabled = 1;
  536. }
  537. /*
  538. * Disable instruction sampling if it was enabled
  539. */
  540. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  541. mtspr(SPRN_MMCRA,
  542. cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  543. mb();
  544. }
  545. /*
  546. * Set the 'freeze counters' bit.
  547. * The barrier is to make sure the mtspr has been
  548. * executed and the PMU has frozen the events
  549. * before we return.
  550. */
  551. write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
  552. mb();
  553. }
  554. local_irq_restore(flags);
  555. }
  556. /*
  557. * Re-enable all events if disable == 0.
  558. * If we were previously disabled and events were added, then
  559. * put the new config on the PMU.
  560. */
  561. static void power_pmu_enable(struct pmu *pmu)
  562. {
  563. struct perf_event *event;
  564. struct cpu_hw_events *cpuhw;
  565. unsigned long flags;
  566. long i;
  567. unsigned long val;
  568. s64 left;
  569. unsigned int hwc_index[MAX_HWEVENTS];
  570. int n_lim;
  571. int idx;
  572. if (!ppmu)
  573. return;
  574. local_irq_save(flags);
  575. cpuhw = &__get_cpu_var(cpu_hw_events);
  576. if (!cpuhw->disabled) {
  577. local_irq_restore(flags);
  578. return;
  579. }
  580. cpuhw->disabled = 0;
  581. /*
  582. * If we didn't change anything, or only removed events,
  583. * no need to recalculate MMCR* settings and reset the PMCs.
  584. * Just reenable the PMU with the current MMCR* settings
  585. * (possibly updated for removal of events).
  586. */
  587. if (!cpuhw->n_added) {
  588. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  589. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  590. if (cpuhw->n_events == 0)
  591. ppc_set_pmu_inuse(0);
  592. goto out_enable;
  593. }
  594. /*
  595. * Compute MMCR* values for the new set of events
  596. */
  597. if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
  598. cpuhw->mmcr)) {
  599. /* shouldn't ever get here */
  600. printk(KERN_ERR "oops compute_mmcr failed\n");
  601. goto out;
  602. }
  603. /*
  604. * Add in MMCR0 freeze bits corresponding to the
  605. * attr.exclude_* bits for the first event.
  606. * We have already checked that all events have the
  607. * same values for these bits as the first event.
  608. */
  609. event = cpuhw->event[0];
  610. if (event->attr.exclude_user)
  611. cpuhw->mmcr[0] |= MMCR0_FCP;
  612. if (event->attr.exclude_kernel)
  613. cpuhw->mmcr[0] |= freeze_events_kernel;
  614. if (event->attr.exclude_hv)
  615. cpuhw->mmcr[0] |= MMCR0_FCHV;
  616. /*
  617. * Write the new configuration to MMCR* with the freeze
  618. * bit set and set the hardware events to their initial values.
  619. * Then unfreeze the events.
  620. */
  621. ppc_set_pmu_inuse(1);
  622. mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
  623. mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
  624. mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
  625. | MMCR0_FC);
  626. /*
  627. * Read off any pre-existing events that need to move
  628. * to another PMC.
  629. */
  630. for (i = 0; i < cpuhw->n_events; ++i) {
  631. event = cpuhw->event[i];
  632. if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
  633. power_pmu_read(event);
  634. write_pmc(event->hw.idx, 0);
  635. event->hw.idx = 0;
  636. }
  637. }
  638. /*
  639. * Initialize the PMCs for all the new and moved events.
  640. */
  641. cpuhw->n_limited = n_lim = 0;
  642. for (i = 0; i < cpuhw->n_events; ++i) {
  643. event = cpuhw->event[i];
  644. if (event->hw.idx)
  645. continue;
  646. idx = hwc_index[i] + 1;
  647. if (is_limited_pmc(idx)) {
  648. cpuhw->limited_counter[n_lim] = event;
  649. cpuhw->limited_hwidx[n_lim] = idx;
  650. ++n_lim;
  651. continue;
  652. }
  653. val = 0;
  654. if (event->hw.sample_period) {
  655. left = local64_read(&event->hw.period_left);
  656. if (left < 0x80000000L)
  657. val = 0x80000000L - left;
  658. }
  659. local64_set(&event->hw.prev_count, val);
  660. event->hw.idx = idx;
  661. if (event->hw.state & PERF_HES_STOPPED)
  662. val = 0;
  663. write_pmc(idx, val);
  664. perf_event_update_userpage(event);
  665. }
  666. cpuhw->n_limited = n_lim;
  667. cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
  668. out_enable:
  669. mb();
  670. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  671. /*
  672. * Enable instruction sampling if necessary
  673. */
  674. if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
  675. mb();
  676. mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
  677. }
  678. out:
  679. local_irq_restore(flags);
  680. }
  681. static int collect_events(struct perf_event *group, int max_count,
  682. struct perf_event *ctrs[], u64 *events,
  683. unsigned int *flags)
  684. {
  685. int n = 0;
  686. struct perf_event *event;
  687. if (!is_software_event(group)) {
  688. if (n >= max_count)
  689. return -1;
  690. ctrs[n] = group;
  691. flags[n] = group->hw.event_base;
  692. events[n++] = group->hw.config;
  693. }
  694. list_for_each_entry(event, &group->sibling_list, group_entry) {
  695. if (!is_software_event(event) &&
  696. event->state != PERF_EVENT_STATE_OFF) {
  697. if (n >= max_count)
  698. return -1;
  699. ctrs[n] = event;
  700. flags[n] = event->hw.event_base;
  701. events[n++] = event->hw.config;
  702. }
  703. }
  704. return n;
  705. }
  706. /*
  707. * Add a event to the PMU.
  708. * If all events are not already frozen, then we disable and
  709. * re-enable the PMU in order to get hw_perf_enable to do the
  710. * actual work of reconfiguring the PMU.
  711. */
  712. static int power_pmu_add(struct perf_event *event, int ef_flags)
  713. {
  714. struct cpu_hw_events *cpuhw;
  715. unsigned long flags;
  716. int n0;
  717. int ret = -EAGAIN;
  718. local_irq_save(flags);
  719. perf_pmu_disable(event->pmu);
  720. /*
  721. * Add the event to the list (if there is room)
  722. * and check whether the total set is still feasible.
  723. */
  724. cpuhw = &__get_cpu_var(cpu_hw_events);
  725. n0 = cpuhw->n_events;
  726. if (n0 >= ppmu->n_counter)
  727. goto out;
  728. cpuhw->event[n0] = event;
  729. cpuhw->events[n0] = event->hw.config;
  730. cpuhw->flags[n0] = event->hw.event_base;
  731. if (!(ef_flags & PERF_EF_START))
  732. event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  733. /*
  734. * If group events scheduling transaction was started,
  735. * skip the schedulability test here, it will be performed
  736. * at commit time(->commit_txn) as a whole
  737. */
  738. if (cpuhw->group_flag & PERF_EVENT_TXN)
  739. goto nocheck;
  740. if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
  741. goto out;
  742. if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
  743. goto out;
  744. event->hw.config = cpuhw->events[n0];
  745. nocheck:
  746. ++cpuhw->n_events;
  747. ++cpuhw->n_added;
  748. ret = 0;
  749. out:
  750. perf_pmu_enable(event->pmu);
  751. local_irq_restore(flags);
  752. return ret;
  753. }
  754. /*
  755. * Remove a event from the PMU.
  756. */
  757. static void power_pmu_del(struct perf_event *event, int ef_flags)
  758. {
  759. struct cpu_hw_events *cpuhw;
  760. long i;
  761. unsigned long flags;
  762. local_irq_save(flags);
  763. perf_pmu_disable(event->pmu);
  764. power_pmu_read(event);
  765. cpuhw = &__get_cpu_var(cpu_hw_events);
  766. for (i = 0; i < cpuhw->n_events; ++i) {
  767. if (event == cpuhw->event[i]) {
  768. while (++i < cpuhw->n_events) {
  769. cpuhw->event[i-1] = cpuhw->event[i];
  770. cpuhw->events[i-1] = cpuhw->events[i];
  771. cpuhw->flags[i-1] = cpuhw->flags[i];
  772. }
  773. --cpuhw->n_events;
  774. ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
  775. if (event->hw.idx) {
  776. write_pmc(event->hw.idx, 0);
  777. event->hw.idx = 0;
  778. }
  779. perf_event_update_userpage(event);
  780. break;
  781. }
  782. }
  783. for (i = 0; i < cpuhw->n_limited; ++i)
  784. if (event == cpuhw->limited_counter[i])
  785. break;
  786. if (i < cpuhw->n_limited) {
  787. while (++i < cpuhw->n_limited) {
  788. cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
  789. cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
  790. }
  791. --cpuhw->n_limited;
  792. }
  793. if (cpuhw->n_events == 0) {
  794. /* disable exceptions if no events are running */
  795. cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
  796. }
  797. perf_pmu_enable(event->pmu);
  798. local_irq_restore(flags);
  799. }
  800. /*
  801. * POWER-PMU does not support disabling individual counters, hence
  802. * program their cycle counter to their max value and ignore the interrupts.
  803. */
  804. static void power_pmu_start(struct perf_event *event, int ef_flags)
  805. {
  806. unsigned long flags;
  807. s64 left;
  808. unsigned long val;
  809. if (!event->hw.idx || !event->hw.sample_period)
  810. return;
  811. if (!(event->hw.state & PERF_HES_STOPPED))
  812. return;
  813. if (ef_flags & PERF_EF_RELOAD)
  814. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  815. local_irq_save(flags);
  816. perf_pmu_disable(event->pmu);
  817. event->hw.state = 0;
  818. left = local64_read(&event->hw.period_left);
  819. val = 0;
  820. if (left < 0x80000000L)
  821. val = 0x80000000L - left;
  822. write_pmc(event->hw.idx, val);
  823. perf_event_update_userpage(event);
  824. perf_pmu_enable(event->pmu);
  825. local_irq_restore(flags);
  826. }
  827. static void power_pmu_stop(struct perf_event *event, int ef_flags)
  828. {
  829. unsigned long flags;
  830. if (!event->hw.idx || !event->hw.sample_period)
  831. return;
  832. if (event->hw.state & PERF_HES_STOPPED)
  833. return;
  834. local_irq_save(flags);
  835. perf_pmu_disable(event->pmu);
  836. power_pmu_read(event);
  837. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  838. write_pmc(event->hw.idx, 0);
  839. perf_event_update_userpage(event);
  840. perf_pmu_enable(event->pmu);
  841. local_irq_restore(flags);
  842. }
  843. /*
  844. * Start group events scheduling transaction
  845. * Set the flag to make pmu::enable() not perform the
  846. * schedulability test, it will be performed at commit time
  847. */
  848. void power_pmu_start_txn(struct pmu *pmu)
  849. {
  850. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  851. perf_pmu_disable(pmu);
  852. cpuhw->group_flag |= PERF_EVENT_TXN;
  853. cpuhw->n_txn_start = cpuhw->n_events;
  854. }
  855. /*
  856. * Stop group events scheduling transaction
  857. * Clear the flag and pmu::enable() will perform the
  858. * schedulability test.
  859. */
  860. void power_pmu_cancel_txn(struct pmu *pmu)
  861. {
  862. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  863. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  864. perf_pmu_enable(pmu);
  865. }
  866. /*
  867. * Commit group events scheduling transaction
  868. * Perform the group schedulability test as a whole
  869. * Return 0 if success
  870. */
  871. int power_pmu_commit_txn(struct pmu *pmu)
  872. {
  873. struct cpu_hw_events *cpuhw;
  874. long i, n;
  875. if (!ppmu)
  876. return -EAGAIN;
  877. cpuhw = &__get_cpu_var(cpu_hw_events);
  878. n = cpuhw->n_events;
  879. if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
  880. return -EAGAIN;
  881. i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
  882. if (i < 0)
  883. return -EAGAIN;
  884. for (i = cpuhw->n_txn_start; i < n; ++i)
  885. cpuhw->event[i]->hw.config = cpuhw->events[i];
  886. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  887. perf_pmu_enable(pmu);
  888. return 0;
  889. }
  890. /*
  891. * Return 1 if we might be able to put event on a limited PMC,
  892. * or 0 if not.
  893. * A event can only go on a limited PMC if it counts something
  894. * that a limited PMC can count, doesn't require interrupts, and
  895. * doesn't exclude any processor mode.
  896. */
  897. static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
  898. unsigned int flags)
  899. {
  900. int n;
  901. u64 alt[MAX_EVENT_ALTERNATIVES];
  902. if (event->attr.exclude_user
  903. || event->attr.exclude_kernel
  904. || event->attr.exclude_hv
  905. || event->attr.sample_period)
  906. return 0;
  907. if (ppmu->limited_pmc_event(ev))
  908. return 1;
  909. /*
  910. * The requested event_id isn't on a limited PMC already;
  911. * see if any alternative code goes on a limited PMC.
  912. */
  913. if (!ppmu->get_alternatives)
  914. return 0;
  915. flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
  916. n = ppmu->get_alternatives(ev, flags, alt);
  917. return n > 0;
  918. }
  919. /*
  920. * Find an alternative event_id that goes on a normal PMC, if possible,
  921. * and return the event_id code, or 0 if there is no such alternative.
  922. * (Note: event_id code 0 is "don't count" on all machines.)
  923. */
  924. static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
  925. {
  926. u64 alt[MAX_EVENT_ALTERNATIVES];
  927. int n;
  928. flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
  929. n = ppmu->get_alternatives(ev, flags, alt);
  930. if (!n)
  931. return 0;
  932. return alt[0];
  933. }
  934. /* Number of perf_events counting hardware events */
  935. static atomic_t num_events;
  936. /* Used to avoid races in calling reserve/release_pmc_hardware */
  937. static DEFINE_MUTEX(pmc_reserve_mutex);
  938. /*
  939. * Release the PMU if this is the last perf_event.
  940. */
  941. static void hw_perf_event_destroy(struct perf_event *event)
  942. {
  943. if (!atomic_add_unless(&num_events, -1, 1)) {
  944. mutex_lock(&pmc_reserve_mutex);
  945. if (atomic_dec_return(&num_events) == 0)
  946. release_pmc_hardware();
  947. mutex_unlock(&pmc_reserve_mutex);
  948. }
  949. }
  950. /*
  951. * Translate a generic cache event_id config to a raw event_id code.
  952. */
  953. static int hw_perf_cache_event(u64 config, u64 *eventp)
  954. {
  955. unsigned long type, op, result;
  956. int ev;
  957. if (!ppmu->cache_events)
  958. return -EINVAL;
  959. /* unpack config */
  960. type = config & 0xff;
  961. op = (config >> 8) & 0xff;
  962. result = (config >> 16) & 0xff;
  963. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  964. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  965. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  966. return -EINVAL;
  967. ev = (*ppmu->cache_events)[type][op][result];
  968. if (ev == 0)
  969. return -EOPNOTSUPP;
  970. if (ev == -1)
  971. return -EINVAL;
  972. *eventp = ev;
  973. return 0;
  974. }
  975. static int power_pmu_event_init(struct perf_event *event)
  976. {
  977. u64 ev;
  978. unsigned long flags;
  979. struct perf_event *ctrs[MAX_HWEVENTS];
  980. u64 events[MAX_HWEVENTS];
  981. unsigned int cflags[MAX_HWEVENTS];
  982. int n;
  983. int err;
  984. struct cpu_hw_events *cpuhw;
  985. if (!ppmu)
  986. return -ENOENT;
  987. /* does not support taken branch sampling */
  988. if (has_branch_stack(event))
  989. return -EOPNOTSUPP;
  990. switch (event->attr.type) {
  991. case PERF_TYPE_HARDWARE:
  992. ev = event->attr.config;
  993. if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
  994. return -EOPNOTSUPP;
  995. ev = ppmu->generic_events[ev];
  996. break;
  997. case PERF_TYPE_HW_CACHE:
  998. err = hw_perf_cache_event(event->attr.config, &ev);
  999. if (err)
  1000. return err;
  1001. break;
  1002. case PERF_TYPE_RAW:
  1003. ev = event->attr.config;
  1004. break;
  1005. default:
  1006. return -ENOENT;
  1007. }
  1008. event->hw.config_base = ev;
  1009. event->hw.idx = 0;
  1010. /*
  1011. * If we are not running on a hypervisor, force the
  1012. * exclude_hv bit to 0 so that we don't care what
  1013. * the user set it to.
  1014. */
  1015. if (!firmware_has_feature(FW_FEATURE_LPAR))
  1016. event->attr.exclude_hv = 0;
  1017. /*
  1018. * If this is a per-task event, then we can use
  1019. * PM_RUN_* events interchangeably with their non RUN_*
  1020. * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
  1021. * XXX we should check if the task is an idle task.
  1022. */
  1023. flags = 0;
  1024. if (event->attach_state & PERF_ATTACH_TASK)
  1025. flags |= PPMU_ONLY_COUNT_RUN;
  1026. /*
  1027. * If this machine has limited events, check whether this
  1028. * event_id could go on a limited event.
  1029. */
  1030. if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
  1031. if (can_go_on_limited_pmc(event, ev, flags)) {
  1032. flags |= PPMU_LIMITED_PMC_OK;
  1033. } else if (ppmu->limited_pmc_event(ev)) {
  1034. /*
  1035. * The requested event_id is on a limited PMC,
  1036. * but we can't use a limited PMC; see if any
  1037. * alternative goes on a normal PMC.
  1038. */
  1039. ev = normal_pmc_alternative(ev, flags);
  1040. if (!ev)
  1041. return -EINVAL;
  1042. }
  1043. }
  1044. /*
  1045. * If this is in a group, check if it can go on with all the
  1046. * other hardware events in the group. We assume the event
  1047. * hasn't been linked into its leader's sibling list at this point.
  1048. */
  1049. n = 0;
  1050. if (event->group_leader != event) {
  1051. n = collect_events(event->group_leader, ppmu->n_counter - 1,
  1052. ctrs, events, cflags);
  1053. if (n < 0)
  1054. return -EINVAL;
  1055. }
  1056. events[n] = ev;
  1057. ctrs[n] = event;
  1058. cflags[n] = flags;
  1059. if (check_excludes(ctrs, cflags, n, 1))
  1060. return -EINVAL;
  1061. cpuhw = &get_cpu_var(cpu_hw_events);
  1062. err = power_check_constraints(cpuhw, events, cflags, n + 1);
  1063. put_cpu_var(cpu_hw_events);
  1064. if (err)
  1065. return -EINVAL;
  1066. event->hw.config = events[n];
  1067. event->hw.event_base = cflags[n];
  1068. event->hw.last_period = event->hw.sample_period;
  1069. local64_set(&event->hw.period_left, event->hw.last_period);
  1070. /*
  1071. * See if we need to reserve the PMU.
  1072. * If no events are currently in use, then we have to take a
  1073. * mutex to ensure that we don't race with another task doing
  1074. * reserve_pmc_hardware or release_pmc_hardware.
  1075. */
  1076. err = 0;
  1077. if (!atomic_inc_not_zero(&num_events)) {
  1078. mutex_lock(&pmc_reserve_mutex);
  1079. if (atomic_read(&num_events) == 0 &&
  1080. reserve_pmc_hardware(perf_event_interrupt))
  1081. err = -EBUSY;
  1082. else
  1083. atomic_inc(&num_events);
  1084. mutex_unlock(&pmc_reserve_mutex);
  1085. }
  1086. event->destroy = hw_perf_event_destroy;
  1087. return err;
  1088. }
  1089. static int power_pmu_event_idx(struct perf_event *event)
  1090. {
  1091. return event->hw.idx;
  1092. }
  1093. struct pmu power_pmu = {
  1094. .pmu_enable = power_pmu_enable,
  1095. .pmu_disable = power_pmu_disable,
  1096. .event_init = power_pmu_event_init,
  1097. .add = power_pmu_add,
  1098. .del = power_pmu_del,
  1099. .start = power_pmu_start,
  1100. .stop = power_pmu_stop,
  1101. .read = power_pmu_read,
  1102. .start_txn = power_pmu_start_txn,
  1103. .cancel_txn = power_pmu_cancel_txn,
  1104. .commit_txn = power_pmu_commit_txn,
  1105. .event_idx = power_pmu_event_idx,
  1106. };
  1107. /*
  1108. * A counter has overflowed; update its count and record
  1109. * things if requested. Note that interrupts are hard-disabled
  1110. * here so there is no possibility of being interrupted.
  1111. */
  1112. static void record_and_restart(struct perf_event *event, unsigned long val,
  1113. struct pt_regs *regs)
  1114. {
  1115. u64 period = event->hw.sample_period;
  1116. s64 prev, delta, left;
  1117. int record = 0;
  1118. if (event->hw.state & PERF_HES_STOPPED) {
  1119. write_pmc(event->hw.idx, 0);
  1120. return;
  1121. }
  1122. /* we don't have to worry about interrupts here */
  1123. prev = local64_read(&event->hw.prev_count);
  1124. delta = check_and_compute_delta(prev, val);
  1125. local64_add(delta, &event->count);
  1126. /*
  1127. * See if the total period for this event has expired,
  1128. * and update for the next period.
  1129. */
  1130. val = 0;
  1131. left = local64_read(&event->hw.period_left) - delta;
  1132. if (period) {
  1133. if (left <= 0) {
  1134. left += period;
  1135. if (left <= 0)
  1136. left = period;
  1137. record = 1;
  1138. event->hw.last_period = event->hw.sample_period;
  1139. }
  1140. if (left < 0x80000000LL)
  1141. val = 0x80000000LL - left;
  1142. }
  1143. write_pmc(event->hw.idx, val);
  1144. local64_set(&event->hw.prev_count, val);
  1145. local64_set(&event->hw.period_left, left);
  1146. perf_event_update_userpage(event);
  1147. /*
  1148. * Finally record data if requested.
  1149. */
  1150. if (record) {
  1151. struct perf_sample_data data;
  1152. perf_sample_data_init(&data, ~0ULL);
  1153. data.period = event->hw.last_period;
  1154. if (event->attr.sample_type & PERF_SAMPLE_ADDR)
  1155. perf_get_data_addr(regs, &data.addr);
  1156. if (perf_event_overflow(event, &data, regs))
  1157. power_pmu_stop(event, 0);
  1158. }
  1159. }
  1160. /*
  1161. * Called from generic code to get the misc flags (i.e. processor mode)
  1162. * for an event_id.
  1163. */
  1164. unsigned long perf_misc_flags(struct pt_regs *regs)
  1165. {
  1166. u32 flags = perf_get_misc_flags(regs);
  1167. if (flags)
  1168. return flags;
  1169. return user_mode(regs) ? PERF_RECORD_MISC_USER :
  1170. PERF_RECORD_MISC_KERNEL;
  1171. }
  1172. /*
  1173. * Called from generic code to get the instruction pointer
  1174. * for an event_id.
  1175. */
  1176. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1177. {
  1178. unsigned long mmcra = regs->dsisr;
  1179. /* Not a PMU interrupt */
  1180. if (TRAP(regs) != 0xf00)
  1181. return regs->nip;
  1182. /* Processor doesn't support sampling non marked events */
  1183. if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) &&
  1184. !(mmcra & MMCRA_SAMPLE_ENABLE))
  1185. return regs->nip;
  1186. return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
  1187. }
  1188. static bool pmc_overflow(unsigned long val)
  1189. {
  1190. if ((int)val < 0)
  1191. return true;
  1192. /*
  1193. * Events on POWER7 can roll back if a speculative event doesn't
  1194. * eventually complete. Unfortunately in some rare cases they will
  1195. * raise a performance monitor exception. We need to catch this to
  1196. * ensure we reset the PMC. In all cases the PMC will be 256 or less
  1197. * cycles from overflow.
  1198. *
  1199. * We only do this if the first pass fails to find any overflowing
  1200. * PMCs because a user might set a period of less than 256 and we
  1201. * don't want to mistakenly reset them.
  1202. */
  1203. if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256))
  1204. return true;
  1205. return false;
  1206. }
  1207. /*
  1208. * Performance monitor interrupt stuff
  1209. */
  1210. static void perf_event_interrupt(struct pt_regs *regs)
  1211. {
  1212. int i;
  1213. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1214. struct perf_event *event;
  1215. unsigned long val;
  1216. int found = 0;
  1217. int nmi;
  1218. if (cpuhw->n_limited)
  1219. freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
  1220. mfspr(SPRN_PMC6));
  1221. perf_read_regs(regs);
  1222. nmi = perf_intr_is_nmi(regs);
  1223. if (nmi)
  1224. nmi_enter();
  1225. else
  1226. irq_enter();
  1227. for (i = 0; i < cpuhw->n_events; ++i) {
  1228. event = cpuhw->event[i];
  1229. if (!event->hw.idx || is_limited_pmc(event->hw.idx))
  1230. continue;
  1231. val = read_pmc(event->hw.idx);
  1232. if ((int)val < 0) {
  1233. /* event has overflowed */
  1234. found = 1;
  1235. record_and_restart(event, val, regs);
  1236. }
  1237. }
  1238. /*
  1239. * In case we didn't find and reset the event that caused
  1240. * the interrupt, scan all events and reset any that are
  1241. * negative, to avoid getting continual interrupts.
  1242. * Any that we processed in the previous loop will not be negative.
  1243. */
  1244. if (!found) {
  1245. for (i = 0; i < ppmu->n_counter; ++i) {
  1246. if (is_limited_pmc(i + 1))
  1247. continue;
  1248. val = read_pmc(i + 1);
  1249. if (pmc_overflow(val))
  1250. write_pmc(i + 1, 0);
  1251. }
  1252. }
  1253. /*
  1254. * Reset MMCR0 to its normal value. This will set PMXE and
  1255. * clear FC (freeze counters) and PMAO (perf mon alert occurred)
  1256. * and thus allow interrupts to occur again.
  1257. * XXX might want to use MSR.PM to keep the events frozen until
  1258. * we get back out of this interrupt.
  1259. */
  1260. write_mmcr0(cpuhw, cpuhw->mmcr[0]);
  1261. if (nmi)
  1262. nmi_exit();
  1263. else
  1264. irq_exit();
  1265. }
  1266. static void power_pmu_setup(int cpu)
  1267. {
  1268. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  1269. if (!ppmu)
  1270. return;
  1271. memset(cpuhw, 0, sizeof(*cpuhw));
  1272. cpuhw->mmcr[0] = MMCR0_FC;
  1273. }
  1274. static int __cpuinit
  1275. power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1276. {
  1277. unsigned int cpu = (long)hcpu;
  1278. switch (action & ~CPU_TASKS_FROZEN) {
  1279. case CPU_UP_PREPARE:
  1280. power_pmu_setup(cpu);
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. return NOTIFY_OK;
  1286. }
  1287. int __cpuinit register_power_pmu(struct power_pmu *pmu)
  1288. {
  1289. if (ppmu)
  1290. return -EBUSY; /* something's already registered */
  1291. ppmu = pmu;
  1292. pr_info("%s performance monitor hardware support registered\n",
  1293. pmu->name);
  1294. #ifdef MSR_HV
  1295. /*
  1296. * Use FCHV to ignore kernel events if MSR.HV is set.
  1297. */
  1298. if (mfmsr() & MSR_HV)
  1299. freeze_events_kernel = MMCR0_FCHV;
  1300. #endif /* CONFIG_PPC64 */
  1301. perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
  1302. perf_cpu_notifier(power_pmu_notifier);
  1303. return 0;
  1304. }