core-book3s.c 36 KB

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