core-book3s.c 39 KB

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