core-book3s.c 38 KB

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