perf_counter.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /*
  2. * Performance counter x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. *
  10. * For licencing details see kernel-base/COPYING
  11. */
  12. #include <linux/perf_counter.h>
  13. #include <linux/capability.h>
  14. #include <linux/notifier.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/highmem.h>
  22. #include <asm/apic.h>
  23. #include <asm/stacktrace.h>
  24. #include <asm/nmi.h>
  25. static u64 perf_counter_mask __read_mostly;
  26. struct cpu_hw_counters {
  27. struct perf_counter *counters[X86_PMC_IDX_MAX];
  28. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  30. unsigned long interrupts;
  31. int enabled;
  32. };
  33. /*
  34. * struct x86_pmu - generic x86 pmu
  35. */
  36. struct x86_pmu {
  37. const char *name;
  38. int version;
  39. int (*handle_irq)(struct pt_regs *);
  40. void (*disable_all)(void);
  41. void (*enable_all)(void);
  42. void (*enable)(struct hw_perf_counter *, int);
  43. void (*disable)(struct hw_perf_counter *, int);
  44. unsigned eventsel;
  45. unsigned perfctr;
  46. u64 (*event_map)(int);
  47. u64 (*raw_event)(u64);
  48. int max_events;
  49. int num_counters;
  50. int num_counters_fixed;
  51. int counter_bits;
  52. u64 counter_mask;
  53. int apic;
  54. u64 max_period;
  55. u64 intel_ctrl;
  56. };
  57. static struct x86_pmu x86_pmu __read_mostly;
  58. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  59. .enabled = 1,
  60. };
  61. /*
  62. * Not sure about some of these
  63. */
  64. static const u64 p6_perfmon_event_map[] =
  65. {
  66. [PERF_COUNT_HW_CPU_CYCLES] = 0x0079,
  67. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  68. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e,
  69. [PERF_COUNT_HW_CACHE_MISSES] = 0x012e,
  70. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  71. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  72. [PERF_COUNT_HW_BUS_CYCLES] = 0x0062,
  73. };
  74. static u64 p6_pmu_event_map(int event)
  75. {
  76. return p6_perfmon_event_map[event];
  77. }
  78. /*
  79. * Counter setting that is specified not to count anything.
  80. * We use this to effectively disable a counter.
  81. *
  82. * L2_RQSTS with 0 MESI unit mask.
  83. */
  84. #define P6_NOP_COUNTER 0x0000002EULL
  85. static u64 p6_pmu_raw_event(u64 event)
  86. {
  87. #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL
  88. #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  89. #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL
  90. #define P6_EVNTSEL_INV_MASK 0x00800000ULL
  91. #define P6_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  92. #define P6_EVNTSEL_MASK \
  93. (P6_EVNTSEL_EVENT_MASK | \
  94. P6_EVNTSEL_UNIT_MASK | \
  95. P6_EVNTSEL_EDGE_MASK | \
  96. P6_EVNTSEL_INV_MASK | \
  97. P6_EVNTSEL_COUNTER_MASK)
  98. return event & P6_EVNTSEL_MASK;
  99. }
  100. /*
  101. * Intel PerfMon v3. Used on Core2 and later.
  102. */
  103. static const u64 intel_perfmon_event_map[] =
  104. {
  105. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  106. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  107. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  108. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  109. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  110. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  111. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  112. };
  113. static u64 intel_pmu_event_map(int event)
  114. {
  115. return intel_perfmon_event_map[event];
  116. }
  117. /*
  118. * Generalized hw caching related event table, filled
  119. * in on a per model basis. A value of 0 means
  120. * 'not supported', -1 means 'event makes no sense on
  121. * this CPU', any other value means the raw event
  122. * ID.
  123. */
  124. #define C(x) PERF_COUNT_HW_CACHE_##x
  125. static u64 __read_mostly hw_cache_event_ids
  126. [PERF_COUNT_HW_CACHE_MAX]
  127. [PERF_COUNT_HW_CACHE_OP_MAX]
  128. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  129. static const u64 nehalem_hw_cache_event_ids
  130. [PERF_COUNT_HW_CACHE_MAX]
  131. [PERF_COUNT_HW_CACHE_OP_MAX]
  132. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  133. {
  134. [ C(L1D) ] = {
  135. [ C(OP_READ) ] = {
  136. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  137. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  138. },
  139. [ C(OP_WRITE) ] = {
  140. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  141. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  142. },
  143. [ C(OP_PREFETCH) ] = {
  144. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  145. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  146. },
  147. },
  148. [ C(L1I ) ] = {
  149. [ C(OP_READ) ] = {
  150. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  151. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  152. },
  153. [ C(OP_WRITE) ] = {
  154. [ C(RESULT_ACCESS) ] = -1,
  155. [ C(RESULT_MISS) ] = -1,
  156. },
  157. [ C(OP_PREFETCH) ] = {
  158. [ C(RESULT_ACCESS) ] = 0x0,
  159. [ C(RESULT_MISS) ] = 0x0,
  160. },
  161. },
  162. [ C(LL ) ] = {
  163. [ C(OP_READ) ] = {
  164. [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
  165. [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
  166. },
  167. [ C(OP_WRITE) ] = {
  168. [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
  169. [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
  170. },
  171. [ C(OP_PREFETCH) ] = {
  172. [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
  173. [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
  174. },
  175. },
  176. [ C(DTLB) ] = {
  177. [ C(OP_READ) ] = {
  178. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  179. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  180. },
  181. [ C(OP_WRITE) ] = {
  182. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  183. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  184. },
  185. [ C(OP_PREFETCH) ] = {
  186. [ C(RESULT_ACCESS) ] = 0x0,
  187. [ C(RESULT_MISS) ] = 0x0,
  188. },
  189. },
  190. [ C(ITLB) ] = {
  191. [ C(OP_READ) ] = {
  192. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  193. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  194. },
  195. [ C(OP_WRITE) ] = {
  196. [ C(RESULT_ACCESS) ] = -1,
  197. [ C(RESULT_MISS) ] = -1,
  198. },
  199. [ C(OP_PREFETCH) ] = {
  200. [ C(RESULT_ACCESS) ] = -1,
  201. [ C(RESULT_MISS) ] = -1,
  202. },
  203. },
  204. [ C(BPU ) ] = {
  205. [ C(OP_READ) ] = {
  206. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  207. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  208. },
  209. [ C(OP_WRITE) ] = {
  210. [ C(RESULT_ACCESS) ] = -1,
  211. [ C(RESULT_MISS) ] = -1,
  212. },
  213. [ C(OP_PREFETCH) ] = {
  214. [ C(RESULT_ACCESS) ] = -1,
  215. [ C(RESULT_MISS) ] = -1,
  216. },
  217. },
  218. };
  219. static const u64 core2_hw_cache_event_ids
  220. [PERF_COUNT_HW_CACHE_MAX]
  221. [PERF_COUNT_HW_CACHE_OP_MAX]
  222. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  223. {
  224. [ C(L1D) ] = {
  225. [ C(OP_READ) ] = {
  226. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  227. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  228. },
  229. [ C(OP_WRITE) ] = {
  230. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  231. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  232. },
  233. [ C(OP_PREFETCH) ] = {
  234. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  235. [ C(RESULT_MISS) ] = 0,
  236. },
  237. },
  238. [ C(L1I ) ] = {
  239. [ C(OP_READ) ] = {
  240. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  241. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  242. },
  243. [ C(OP_WRITE) ] = {
  244. [ C(RESULT_ACCESS) ] = -1,
  245. [ C(RESULT_MISS) ] = -1,
  246. },
  247. [ C(OP_PREFETCH) ] = {
  248. [ C(RESULT_ACCESS) ] = 0,
  249. [ C(RESULT_MISS) ] = 0,
  250. },
  251. },
  252. [ C(LL ) ] = {
  253. [ C(OP_READ) ] = {
  254. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  255. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  256. },
  257. [ C(OP_WRITE) ] = {
  258. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  259. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  260. },
  261. [ C(OP_PREFETCH) ] = {
  262. [ C(RESULT_ACCESS) ] = 0,
  263. [ C(RESULT_MISS) ] = 0,
  264. },
  265. },
  266. [ C(DTLB) ] = {
  267. [ C(OP_READ) ] = {
  268. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  269. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  270. },
  271. [ C(OP_WRITE) ] = {
  272. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  273. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  274. },
  275. [ C(OP_PREFETCH) ] = {
  276. [ C(RESULT_ACCESS) ] = 0,
  277. [ C(RESULT_MISS) ] = 0,
  278. },
  279. },
  280. [ C(ITLB) ] = {
  281. [ C(OP_READ) ] = {
  282. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  283. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  284. },
  285. [ C(OP_WRITE) ] = {
  286. [ C(RESULT_ACCESS) ] = -1,
  287. [ C(RESULT_MISS) ] = -1,
  288. },
  289. [ C(OP_PREFETCH) ] = {
  290. [ C(RESULT_ACCESS) ] = -1,
  291. [ C(RESULT_MISS) ] = -1,
  292. },
  293. },
  294. [ C(BPU ) ] = {
  295. [ C(OP_READ) ] = {
  296. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  297. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  298. },
  299. [ C(OP_WRITE) ] = {
  300. [ C(RESULT_ACCESS) ] = -1,
  301. [ C(RESULT_MISS) ] = -1,
  302. },
  303. [ C(OP_PREFETCH) ] = {
  304. [ C(RESULT_ACCESS) ] = -1,
  305. [ C(RESULT_MISS) ] = -1,
  306. },
  307. },
  308. };
  309. static const u64 atom_hw_cache_event_ids
  310. [PERF_COUNT_HW_CACHE_MAX]
  311. [PERF_COUNT_HW_CACHE_OP_MAX]
  312. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  313. {
  314. [ C(L1D) ] = {
  315. [ C(OP_READ) ] = {
  316. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  317. [ C(RESULT_MISS) ] = 0,
  318. },
  319. [ C(OP_WRITE) ] = {
  320. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  321. [ C(RESULT_MISS) ] = 0,
  322. },
  323. [ C(OP_PREFETCH) ] = {
  324. [ C(RESULT_ACCESS) ] = 0x0,
  325. [ C(RESULT_MISS) ] = 0,
  326. },
  327. },
  328. [ C(L1I ) ] = {
  329. [ C(OP_READ) ] = {
  330. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  331. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  332. },
  333. [ C(OP_WRITE) ] = {
  334. [ C(RESULT_ACCESS) ] = -1,
  335. [ C(RESULT_MISS) ] = -1,
  336. },
  337. [ C(OP_PREFETCH) ] = {
  338. [ C(RESULT_ACCESS) ] = 0,
  339. [ C(RESULT_MISS) ] = 0,
  340. },
  341. },
  342. [ C(LL ) ] = {
  343. [ C(OP_READ) ] = {
  344. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  345. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  346. },
  347. [ C(OP_WRITE) ] = {
  348. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  349. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  350. },
  351. [ C(OP_PREFETCH) ] = {
  352. [ C(RESULT_ACCESS) ] = 0,
  353. [ C(RESULT_MISS) ] = 0,
  354. },
  355. },
  356. [ C(DTLB) ] = {
  357. [ C(OP_READ) ] = {
  358. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  359. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  360. },
  361. [ C(OP_WRITE) ] = {
  362. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  363. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  364. },
  365. [ C(OP_PREFETCH) ] = {
  366. [ C(RESULT_ACCESS) ] = 0,
  367. [ C(RESULT_MISS) ] = 0,
  368. },
  369. },
  370. [ C(ITLB) ] = {
  371. [ C(OP_READ) ] = {
  372. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  373. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  374. },
  375. [ C(OP_WRITE) ] = {
  376. [ C(RESULT_ACCESS) ] = -1,
  377. [ C(RESULT_MISS) ] = -1,
  378. },
  379. [ C(OP_PREFETCH) ] = {
  380. [ C(RESULT_ACCESS) ] = -1,
  381. [ C(RESULT_MISS) ] = -1,
  382. },
  383. },
  384. [ C(BPU ) ] = {
  385. [ C(OP_READ) ] = {
  386. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  387. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  388. },
  389. [ C(OP_WRITE) ] = {
  390. [ C(RESULT_ACCESS) ] = -1,
  391. [ C(RESULT_MISS) ] = -1,
  392. },
  393. [ C(OP_PREFETCH) ] = {
  394. [ C(RESULT_ACCESS) ] = -1,
  395. [ C(RESULT_MISS) ] = -1,
  396. },
  397. },
  398. };
  399. static u64 intel_pmu_raw_event(u64 event)
  400. {
  401. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  402. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  403. #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
  404. #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
  405. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  406. #define CORE_EVNTSEL_MASK \
  407. (CORE_EVNTSEL_EVENT_MASK | \
  408. CORE_EVNTSEL_UNIT_MASK | \
  409. CORE_EVNTSEL_EDGE_MASK | \
  410. CORE_EVNTSEL_INV_MASK | \
  411. CORE_EVNTSEL_COUNTER_MASK)
  412. return event & CORE_EVNTSEL_MASK;
  413. }
  414. static const u64 amd_hw_cache_event_ids
  415. [PERF_COUNT_HW_CACHE_MAX]
  416. [PERF_COUNT_HW_CACHE_OP_MAX]
  417. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  418. {
  419. [ C(L1D) ] = {
  420. [ C(OP_READ) ] = {
  421. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  422. [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
  423. },
  424. [ C(OP_WRITE) ] = {
  425. [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
  426. [ C(RESULT_MISS) ] = 0,
  427. },
  428. [ C(OP_PREFETCH) ] = {
  429. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  430. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  431. },
  432. },
  433. [ C(L1I ) ] = {
  434. [ C(OP_READ) ] = {
  435. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  436. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  437. },
  438. [ C(OP_WRITE) ] = {
  439. [ C(RESULT_ACCESS) ] = -1,
  440. [ C(RESULT_MISS) ] = -1,
  441. },
  442. [ C(OP_PREFETCH) ] = {
  443. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  444. [ C(RESULT_MISS) ] = 0,
  445. },
  446. },
  447. [ C(LL ) ] = {
  448. [ C(OP_READ) ] = {
  449. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  450. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  451. },
  452. [ C(OP_WRITE) ] = {
  453. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  454. [ C(RESULT_MISS) ] = 0,
  455. },
  456. [ C(OP_PREFETCH) ] = {
  457. [ C(RESULT_ACCESS) ] = 0,
  458. [ C(RESULT_MISS) ] = 0,
  459. },
  460. },
  461. [ C(DTLB) ] = {
  462. [ C(OP_READ) ] = {
  463. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  464. [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
  465. },
  466. [ C(OP_WRITE) ] = {
  467. [ C(RESULT_ACCESS) ] = 0,
  468. [ C(RESULT_MISS) ] = 0,
  469. },
  470. [ C(OP_PREFETCH) ] = {
  471. [ C(RESULT_ACCESS) ] = 0,
  472. [ C(RESULT_MISS) ] = 0,
  473. },
  474. },
  475. [ C(ITLB) ] = {
  476. [ C(OP_READ) ] = {
  477. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  478. [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */
  479. },
  480. [ C(OP_WRITE) ] = {
  481. [ C(RESULT_ACCESS) ] = -1,
  482. [ C(RESULT_MISS) ] = -1,
  483. },
  484. [ C(OP_PREFETCH) ] = {
  485. [ C(RESULT_ACCESS) ] = -1,
  486. [ C(RESULT_MISS) ] = -1,
  487. },
  488. },
  489. [ C(BPU ) ] = {
  490. [ C(OP_READ) ] = {
  491. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  492. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  493. },
  494. [ C(OP_WRITE) ] = {
  495. [ C(RESULT_ACCESS) ] = -1,
  496. [ C(RESULT_MISS) ] = -1,
  497. },
  498. [ C(OP_PREFETCH) ] = {
  499. [ C(RESULT_ACCESS) ] = -1,
  500. [ C(RESULT_MISS) ] = -1,
  501. },
  502. },
  503. };
  504. /*
  505. * AMD Performance Monitor K7 and later.
  506. */
  507. static const u64 amd_perfmon_event_map[] =
  508. {
  509. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  510. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  511. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
  512. [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
  513. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  514. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  515. };
  516. static u64 amd_pmu_event_map(int event)
  517. {
  518. return amd_perfmon_event_map[event];
  519. }
  520. static u64 amd_pmu_raw_event(u64 event)
  521. {
  522. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  523. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  524. #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
  525. #define K7_EVNTSEL_INV_MASK 0x000800000ULL
  526. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  527. #define K7_EVNTSEL_MASK \
  528. (K7_EVNTSEL_EVENT_MASK | \
  529. K7_EVNTSEL_UNIT_MASK | \
  530. K7_EVNTSEL_EDGE_MASK | \
  531. K7_EVNTSEL_INV_MASK | \
  532. K7_EVNTSEL_COUNTER_MASK)
  533. return event & K7_EVNTSEL_MASK;
  534. }
  535. /*
  536. * Propagate counter elapsed time into the generic counter.
  537. * Can only be executed on the CPU where the counter is active.
  538. * Returns the delta events processed.
  539. */
  540. static u64
  541. x86_perf_counter_update(struct perf_counter *counter,
  542. struct hw_perf_counter *hwc, int idx)
  543. {
  544. int shift = 64 - x86_pmu.counter_bits;
  545. u64 prev_raw_count, new_raw_count;
  546. s64 delta;
  547. /*
  548. * Careful: an NMI might modify the previous counter value.
  549. *
  550. * Our tactic to handle this is to first atomically read and
  551. * exchange a new raw count - then add that new-prev delta
  552. * count to the generic counter atomically:
  553. */
  554. again:
  555. prev_raw_count = atomic64_read(&hwc->prev_count);
  556. rdmsrl(hwc->counter_base + idx, new_raw_count);
  557. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  558. new_raw_count) != prev_raw_count)
  559. goto again;
  560. /*
  561. * Now we have the new raw value and have updated the prev
  562. * timestamp already. We can now calculate the elapsed delta
  563. * (counter-)time and add that to the generic counter.
  564. *
  565. * Careful, not all hw sign-extends above the physical width
  566. * of the count.
  567. */
  568. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  569. delta >>= shift;
  570. atomic64_add(delta, &counter->count);
  571. atomic64_sub(delta, &hwc->period_left);
  572. return new_raw_count;
  573. }
  574. static atomic_t active_counters;
  575. static DEFINE_MUTEX(pmc_reserve_mutex);
  576. static bool reserve_pmc_hardware(void)
  577. {
  578. #ifdef CONFIG_X86_LOCAL_APIC
  579. int i;
  580. if (nmi_watchdog == NMI_LOCAL_APIC)
  581. disable_lapic_nmi_watchdog();
  582. for (i = 0; i < x86_pmu.num_counters; i++) {
  583. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  584. goto perfctr_fail;
  585. }
  586. for (i = 0; i < x86_pmu.num_counters; i++) {
  587. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  588. goto eventsel_fail;
  589. }
  590. #endif
  591. return true;
  592. #ifdef CONFIG_X86_LOCAL_APIC
  593. eventsel_fail:
  594. for (i--; i >= 0; i--)
  595. release_evntsel_nmi(x86_pmu.eventsel + i);
  596. i = x86_pmu.num_counters;
  597. perfctr_fail:
  598. for (i--; i >= 0; i--)
  599. release_perfctr_nmi(x86_pmu.perfctr + i);
  600. if (nmi_watchdog == NMI_LOCAL_APIC)
  601. enable_lapic_nmi_watchdog();
  602. return false;
  603. #endif
  604. }
  605. static void release_pmc_hardware(void)
  606. {
  607. #ifdef CONFIG_X86_LOCAL_APIC
  608. int i;
  609. for (i = 0; i < x86_pmu.num_counters; i++) {
  610. release_perfctr_nmi(x86_pmu.perfctr + i);
  611. release_evntsel_nmi(x86_pmu.eventsel + i);
  612. }
  613. if (nmi_watchdog == NMI_LOCAL_APIC)
  614. enable_lapic_nmi_watchdog();
  615. #endif
  616. }
  617. static void hw_perf_counter_destroy(struct perf_counter *counter)
  618. {
  619. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  620. release_pmc_hardware();
  621. mutex_unlock(&pmc_reserve_mutex);
  622. }
  623. }
  624. static inline int x86_pmu_initialized(void)
  625. {
  626. return x86_pmu.handle_irq != NULL;
  627. }
  628. static inline int
  629. set_ext_hw_attr(struct hw_perf_counter *hwc, struct perf_counter_attr *attr)
  630. {
  631. unsigned int cache_type, cache_op, cache_result;
  632. u64 config, val;
  633. config = attr->config;
  634. cache_type = (config >> 0) & 0xff;
  635. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  636. return -EINVAL;
  637. cache_op = (config >> 8) & 0xff;
  638. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  639. return -EINVAL;
  640. cache_result = (config >> 16) & 0xff;
  641. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  642. return -EINVAL;
  643. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  644. if (val == 0)
  645. return -ENOENT;
  646. if (val == -1)
  647. return -EINVAL;
  648. hwc->config |= val;
  649. return 0;
  650. }
  651. /*
  652. * Setup the hardware configuration for a given attr_type
  653. */
  654. static int __hw_perf_counter_init(struct perf_counter *counter)
  655. {
  656. struct perf_counter_attr *attr = &counter->attr;
  657. struct hw_perf_counter *hwc = &counter->hw;
  658. u64 config;
  659. int err;
  660. if (!x86_pmu_initialized())
  661. return -ENODEV;
  662. err = 0;
  663. if (!atomic_inc_not_zero(&active_counters)) {
  664. mutex_lock(&pmc_reserve_mutex);
  665. if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
  666. err = -EBUSY;
  667. else
  668. atomic_inc(&active_counters);
  669. mutex_unlock(&pmc_reserve_mutex);
  670. }
  671. if (err)
  672. return err;
  673. /*
  674. * Generate PMC IRQs:
  675. * (keep 'enabled' bit clear for now)
  676. */
  677. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  678. /*
  679. * Count user and OS events unless requested not to.
  680. */
  681. if (!attr->exclude_user)
  682. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  683. if (!attr->exclude_kernel)
  684. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  685. if (!hwc->sample_period) {
  686. hwc->sample_period = x86_pmu.max_period;
  687. hwc->last_period = hwc->sample_period;
  688. atomic64_set(&hwc->period_left, hwc->sample_period);
  689. } else {
  690. /*
  691. * If we have a PMU initialized but no APIC
  692. * interrupts, we cannot sample hardware
  693. * counters (user-space has to fall back and
  694. * sample via a hrtimer based software counter):
  695. */
  696. if (!x86_pmu.apic)
  697. return -EOPNOTSUPP;
  698. }
  699. counter->destroy = hw_perf_counter_destroy;
  700. /*
  701. * Raw event type provide the config in the event structure
  702. */
  703. if (attr->type == PERF_TYPE_RAW) {
  704. hwc->config |= x86_pmu.raw_event(attr->config);
  705. return 0;
  706. }
  707. if (attr->type == PERF_TYPE_HW_CACHE)
  708. return set_ext_hw_attr(hwc, attr);
  709. if (attr->config >= x86_pmu.max_events)
  710. return -EINVAL;
  711. /*
  712. * The generic map:
  713. */
  714. config = x86_pmu.event_map(attr->config);
  715. if (config == 0)
  716. return -ENOENT;
  717. if (config == -1LL)
  718. return -EINVAL;
  719. hwc->config |= config;
  720. return 0;
  721. }
  722. static void p6_pmu_disable_all(void)
  723. {
  724. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  725. u64 val;
  726. if (!cpuc->enabled)
  727. return;
  728. cpuc->enabled = 0;
  729. barrier();
  730. /* p6 only has one enable register */
  731. rdmsrl(MSR_P6_EVNTSEL0, val);
  732. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  733. wrmsrl(MSR_P6_EVNTSEL0, val);
  734. }
  735. static void intel_pmu_disable_all(void)
  736. {
  737. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  738. }
  739. static void amd_pmu_disable_all(void)
  740. {
  741. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  742. int idx;
  743. if (!cpuc->enabled)
  744. return;
  745. cpuc->enabled = 0;
  746. /*
  747. * ensure we write the disable before we start disabling the
  748. * counters proper, so that amd_pmu_enable_counter() does the
  749. * right thing.
  750. */
  751. barrier();
  752. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  753. u64 val;
  754. if (!test_bit(idx, cpuc->active_mask))
  755. continue;
  756. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  757. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  758. continue;
  759. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  760. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  761. }
  762. }
  763. void hw_perf_disable(void)
  764. {
  765. if (!x86_pmu_initialized())
  766. return;
  767. return x86_pmu.disable_all();
  768. }
  769. static void p6_pmu_enable_all(void)
  770. {
  771. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  772. unsigned long val;
  773. if (cpuc->enabled)
  774. return;
  775. cpuc->enabled = 1;
  776. barrier();
  777. /* p6 only has one enable register */
  778. rdmsrl(MSR_P6_EVNTSEL0, val);
  779. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  780. wrmsrl(MSR_P6_EVNTSEL0, val);
  781. }
  782. static void intel_pmu_enable_all(void)
  783. {
  784. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  785. }
  786. static void amd_pmu_enable_all(void)
  787. {
  788. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  789. int idx;
  790. if (cpuc->enabled)
  791. return;
  792. cpuc->enabled = 1;
  793. barrier();
  794. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  795. struct perf_counter *counter = cpuc->counters[idx];
  796. u64 val;
  797. if (!test_bit(idx, cpuc->active_mask))
  798. continue;
  799. val = counter->hw.config;
  800. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  801. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  802. }
  803. }
  804. void hw_perf_enable(void)
  805. {
  806. if (!x86_pmu_initialized())
  807. return;
  808. x86_pmu.enable_all();
  809. }
  810. static inline u64 intel_pmu_get_status(void)
  811. {
  812. u64 status;
  813. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  814. return status;
  815. }
  816. static inline void intel_pmu_ack_status(u64 ack)
  817. {
  818. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  819. }
  820. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  821. {
  822. (void)checking_wrmsrl(hwc->config_base + idx,
  823. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  824. }
  825. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  826. {
  827. (void)checking_wrmsrl(hwc->config_base + idx, hwc->config);
  828. }
  829. static inline void
  830. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  831. {
  832. int idx = __idx - X86_PMC_IDX_FIXED;
  833. u64 ctrl_val, mask;
  834. mask = 0xfULL << (idx * 4);
  835. rdmsrl(hwc->config_base, ctrl_val);
  836. ctrl_val &= ~mask;
  837. (void)checking_wrmsrl(hwc->config_base, ctrl_val);
  838. }
  839. static inline void
  840. p6_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  841. {
  842. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  843. u64 val = P6_NOP_COUNTER;
  844. if (cpuc->enabled)
  845. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  846. (void)checking_wrmsrl(hwc->config_base + idx, val);
  847. }
  848. static inline void
  849. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  850. {
  851. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  852. intel_pmu_disable_fixed(hwc, idx);
  853. return;
  854. }
  855. x86_pmu_disable_counter(hwc, idx);
  856. }
  857. static inline void
  858. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  859. {
  860. x86_pmu_disable_counter(hwc, idx);
  861. }
  862. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  863. /*
  864. * Set the next IRQ period, based on the hwc->period_left value.
  865. * To be called with the counter disabled in hw:
  866. */
  867. static int
  868. x86_perf_counter_set_period(struct perf_counter *counter,
  869. struct hw_perf_counter *hwc, int idx)
  870. {
  871. s64 left = atomic64_read(&hwc->period_left);
  872. s64 period = hwc->sample_period;
  873. int err, ret = 0;
  874. /*
  875. * If we are way outside a reasoable range then just skip forward:
  876. */
  877. if (unlikely(left <= -period)) {
  878. left = period;
  879. atomic64_set(&hwc->period_left, left);
  880. hwc->last_period = period;
  881. ret = 1;
  882. }
  883. if (unlikely(left <= 0)) {
  884. left += period;
  885. atomic64_set(&hwc->period_left, left);
  886. hwc->last_period = period;
  887. ret = 1;
  888. }
  889. /*
  890. * Quirk: certain CPUs dont like it if just 1 event is left:
  891. */
  892. if (unlikely(left < 2))
  893. left = 2;
  894. if (left > x86_pmu.max_period)
  895. left = x86_pmu.max_period;
  896. per_cpu(prev_left[idx], smp_processor_id()) = left;
  897. /*
  898. * The hw counter starts counting from this counter offset,
  899. * mark it to be able to extra future deltas:
  900. */
  901. atomic64_set(&hwc->prev_count, (u64)-left);
  902. err = checking_wrmsrl(hwc->counter_base + idx,
  903. (u64)(-left) & x86_pmu.counter_mask);
  904. perf_counter_update_userpage(counter);
  905. return ret;
  906. }
  907. static inline void
  908. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  909. {
  910. int idx = __idx - X86_PMC_IDX_FIXED;
  911. u64 ctrl_val, bits, mask;
  912. int err;
  913. /*
  914. * Enable IRQ generation (0x8),
  915. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  916. * if requested:
  917. */
  918. bits = 0x8ULL;
  919. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  920. bits |= 0x2;
  921. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  922. bits |= 0x1;
  923. bits <<= (idx * 4);
  924. mask = 0xfULL << (idx * 4);
  925. rdmsrl(hwc->config_base, ctrl_val);
  926. ctrl_val &= ~mask;
  927. ctrl_val |= bits;
  928. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  929. }
  930. static void p6_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  931. {
  932. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  933. u64 val;
  934. val = hwc->config;
  935. if (cpuc->enabled)
  936. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  937. (void)checking_wrmsrl(hwc->config_base + idx, val);
  938. }
  939. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  940. {
  941. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  942. intel_pmu_enable_fixed(hwc, idx);
  943. return;
  944. }
  945. x86_pmu_enable_counter(hwc, idx);
  946. }
  947. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  948. {
  949. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  950. if (cpuc->enabled)
  951. x86_pmu_enable_counter(hwc, idx);
  952. }
  953. static int
  954. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  955. {
  956. unsigned int event;
  957. if (!x86_pmu.num_counters_fixed)
  958. return -1;
  959. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  960. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_INSTRUCTIONS)))
  961. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  962. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_CPU_CYCLES)))
  963. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  964. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_BUS_CYCLES)))
  965. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  966. return -1;
  967. }
  968. /*
  969. * Find a PMC slot for the freshly enabled / scheduled in counter:
  970. */
  971. static int x86_pmu_enable(struct perf_counter *counter)
  972. {
  973. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  974. struct hw_perf_counter *hwc = &counter->hw;
  975. int idx;
  976. idx = fixed_mode_idx(counter, hwc);
  977. if (idx >= 0) {
  978. /*
  979. * Try to get the fixed counter, if that is already taken
  980. * then try to get a generic counter:
  981. */
  982. if (test_and_set_bit(idx, cpuc->used_mask))
  983. goto try_generic;
  984. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  985. /*
  986. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  987. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  988. */
  989. hwc->counter_base =
  990. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  991. hwc->idx = idx;
  992. } else {
  993. idx = hwc->idx;
  994. /* Try to get the previous generic counter again */
  995. if (test_and_set_bit(idx, cpuc->used_mask)) {
  996. try_generic:
  997. idx = find_first_zero_bit(cpuc->used_mask,
  998. x86_pmu.num_counters);
  999. if (idx == x86_pmu.num_counters)
  1000. return -EAGAIN;
  1001. set_bit(idx, cpuc->used_mask);
  1002. hwc->idx = idx;
  1003. }
  1004. hwc->config_base = x86_pmu.eventsel;
  1005. hwc->counter_base = x86_pmu.perfctr;
  1006. }
  1007. perf_counters_lapic_init();
  1008. x86_pmu.disable(hwc, idx);
  1009. cpuc->counters[idx] = counter;
  1010. set_bit(idx, cpuc->active_mask);
  1011. x86_perf_counter_set_period(counter, hwc, idx);
  1012. x86_pmu.enable(hwc, idx);
  1013. perf_counter_update_userpage(counter);
  1014. return 0;
  1015. }
  1016. static void x86_pmu_unthrottle(struct perf_counter *counter)
  1017. {
  1018. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1019. struct hw_perf_counter *hwc = &counter->hw;
  1020. if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
  1021. cpuc->counters[hwc->idx] != counter))
  1022. return;
  1023. x86_pmu.enable(hwc, hwc->idx);
  1024. }
  1025. void perf_counter_print_debug(void)
  1026. {
  1027. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1028. struct cpu_hw_counters *cpuc;
  1029. unsigned long flags;
  1030. int cpu, idx;
  1031. if (!x86_pmu.num_counters)
  1032. return;
  1033. local_irq_save(flags);
  1034. cpu = smp_processor_id();
  1035. cpuc = &per_cpu(cpu_hw_counters, cpu);
  1036. if (x86_pmu.version >= 2) {
  1037. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1038. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1039. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1040. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1041. pr_info("\n");
  1042. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1043. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1044. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1045. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1046. }
  1047. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  1048. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1049. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  1050. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  1051. prev_left = per_cpu(prev_left[idx], cpu);
  1052. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1053. cpu, idx, pmc_ctrl);
  1054. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1055. cpu, idx, pmc_count);
  1056. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1057. cpu, idx, prev_left);
  1058. }
  1059. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1060. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1061. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1062. cpu, idx, pmc_count);
  1063. }
  1064. local_irq_restore(flags);
  1065. }
  1066. static void x86_pmu_disable(struct perf_counter *counter)
  1067. {
  1068. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1069. struct hw_perf_counter *hwc = &counter->hw;
  1070. int idx = hwc->idx;
  1071. /*
  1072. * Must be done before we disable, otherwise the nmi handler
  1073. * could reenable again:
  1074. */
  1075. clear_bit(idx, cpuc->active_mask);
  1076. x86_pmu.disable(hwc, idx);
  1077. /*
  1078. * Make sure the cleared pointer becomes visible before we
  1079. * (potentially) free the counter:
  1080. */
  1081. barrier();
  1082. /*
  1083. * Drain the remaining delta count out of a counter
  1084. * that we are disabling:
  1085. */
  1086. x86_perf_counter_update(counter, hwc, idx);
  1087. cpuc->counters[idx] = NULL;
  1088. clear_bit(idx, cpuc->used_mask);
  1089. perf_counter_update_userpage(counter);
  1090. }
  1091. /*
  1092. * Save and restart an expired counter. Called by NMI contexts,
  1093. * so it has to be careful about preempting normal counter ops:
  1094. */
  1095. static int intel_pmu_save_and_restart(struct perf_counter *counter)
  1096. {
  1097. struct hw_perf_counter *hwc = &counter->hw;
  1098. int idx = hwc->idx;
  1099. int ret;
  1100. x86_perf_counter_update(counter, hwc, idx);
  1101. ret = x86_perf_counter_set_period(counter, hwc, idx);
  1102. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1103. intel_pmu_enable_counter(hwc, idx);
  1104. return ret;
  1105. }
  1106. static void intel_pmu_reset(void)
  1107. {
  1108. unsigned long flags;
  1109. int idx;
  1110. if (!x86_pmu.num_counters)
  1111. return;
  1112. local_irq_save(flags);
  1113. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  1114. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1115. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  1116. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  1117. }
  1118. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1119. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1120. }
  1121. local_irq_restore(flags);
  1122. }
  1123. static int p6_pmu_handle_irq(struct pt_regs *regs)
  1124. {
  1125. struct perf_sample_data data;
  1126. struct cpu_hw_counters *cpuc;
  1127. struct perf_counter *counter;
  1128. struct hw_perf_counter *hwc;
  1129. int idx, handled = 0;
  1130. u64 val;
  1131. data.regs = regs;
  1132. data.addr = 0;
  1133. cpuc = &__get_cpu_var(cpu_hw_counters);
  1134. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1135. if (!test_bit(idx, cpuc->active_mask))
  1136. continue;
  1137. counter = cpuc->counters[idx];
  1138. hwc = &counter->hw;
  1139. val = x86_perf_counter_update(counter, hwc, idx);
  1140. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  1141. continue;
  1142. /*
  1143. * counter overflow
  1144. */
  1145. handled = 1;
  1146. data.period = counter->hw.last_period;
  1147. if (!x86_perf_counter_set_period(counter, hwc, idx))
  1148. continue;
  1149. if (perf_counter_overflow(counter, 1, &data))
  1150. p6_pmu_disable_counter(hwc, idx);
  1151. }
  1152. if (handled)
  1153. inc_irq_stat(apic_perf_irqs);
  1154. return handled;
  1155. }
  1156. /*
  1157. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1158. * rules apply:
  1159. */
  1160. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1161. {
  1162. struct perf_sample_data data;
  1163. struct cpu_hw_counters *cpuc;
  1164. int bit, loops;
  1165. u64 ack, status;
  1166. data.regs = regs;
  1167. data.addr = 0;
  1168. cpuc = &__get_cpu_var(cpu_hw_counters);
  1169. perf_disable();
  1170. status = intel_pmu_get_status();
  1171. if (!status) {
  1172. perf_enable();
  1173. return 0;
  1174. }
  1175. loops = 0;
  1176. again:
  1177. if (++loops > 100) {
  1178. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  1179. perf_counter_print_debug();
  1180. intel_pmu_reset();
  1181. perf_enable();
  1182. return 1;
  1183. }
  1184. inc_irq_stat(apic_perf_irqs);
  1185. ack = status;
  1186. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1187. struct perf_counter *counter = cpuc->counters[bit];
  1188. clear_bit(bit, (unsigned long *) &status);
  1189. if (!test_bit(bit, cpuc->active_mask))
  1190. continue;
  1191. if (!intel_pmu_save_and_restart(counter))
  1192. continue;
  1193. data.period = counter->hw.last_period;
  1194. if (perf_counter_overflow(counter, 1, &data))
  1195. intel_pmu_disable_counter(&counter->hw, bit);
  1196. }
  1197. intel_pmu_ack_status(ack);
  1198. /*
  1199. * Repeat if there is more work to be done:
  1200. */
  1201. status = intel_pmu_get_status();
  1202. if (status)
  1203. goto again;
  1204. perf_enable();
  1205. return 1;
  1206. }
  1207. static int amd_pmu_handle_irq(struct pt_regs *regs)
  1208. {
  1209. struct perf_sample_data data;
  1210. struct cpu_hw_counters *cpuc;
  1211. struct perf_counter *counter;
  1212. struct hw_perf_counter *hwc;
  1213. int idx, handled = 0;
  1214. u64 val;
  1215. data.regs = regs;
  1216. data.addr = 0;
  1217. cpuc = &__get_cpu_var(cpu_hw_counters);
  1218. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1219. if (!test_bit(idx, cpuc->active_mask))
  1220. continue;
  1221. counter = cpuc->counters[idx];
  1222. hwc = &counter->hw;
  1223. val = x86_perf_counter_update(counter, hwc, idx);
  1224. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  1225. continue;
  1226. /*
  1227. * counter overflow
  1228. */
  1229. handled = 1;
  1230. data.period = counter->hw.last_period;
  1231. if (!x86_perf_counter_set_period(counter, hwc, idx))
  1232. continue;
  1233. if (perf_counter_overflow(counter, 1, &data))
  1234. amd_pmu_disable_counter(hwc, idx);
  1235. }
  1236. if (handled)
  1237. inc_irq_stat(apic_perf_irqs);
  1238. return handled;
  1239. }
  1240. void smp_perf_pending_interrupt(struct pt_regs *regs)
  1241. {
  1242. irq_enter();
  1243. ack_APIC_irq();
  1244. inc_irq_stat(apic_pending_irqs);
  1245. perf_counter_do_pending();
  1246. irq_exit();
  1247. }
  1248. void set_perf_counter_pending(void)
  1249. {
  1250. #ifdef CONFIG_X86_LOCAL_APIC
  1251. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  1252. #endif
  1253. }
  1254. void perf_counters_lapic_init(void)
  1255. {
  1256. #ifdef CONFIG_X86_LOCAL_APIC
  1257. if (!x86_pmu.apic || !x86_pmu_initialized())
  1258. return;
  1259. /*
  1260. * Always use NMI for PMU
  1261. */
  1262. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1263. #endif
  1264. }
  1265. static int __kprobes
  1266. perf_counter_nmi_handler(struct notifier_block *self,
  1267. unsigned long cmd, void *__args)
  1268. {
  1269. struct die_args *args = __args;
  1270. struct pt_regs *regs;
  1271. if (!atomic_read(&active_counters))
  1272. return NOTIFY_DONE;
  1273. switch (cmd) {
  1274. case DIE_NMI:
  1275. case DIE_NMI_IPI:
  1276. break;
  1277. default:
  1278. return NOTIFY_DONE;
  1279. }
  1280. regs = args->regs;
  1281. #ifdef CONFIG_X86_LOCAL_APIC
  1282. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1283. #endif
  1284. /*
  1285. * Can't rely on the handled return value to say it was our NMI, two
  1286. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  1287. *
  1288. * If the first NMI handles both, the latter will be empty and daze
  1289. * the CPU.
  1290. */
  1291. x86_pmu.handle_irq(regs);
  1292. return NOTIFY_STOP;
  1293. }
  1294. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  1295. .notifier_call = perf_counter_nmi_handler,
  1296. .next = NULL,
  1297. .priority = 1
  1298. };
  1299. static struct x86_pmu p6_pmu = {
  1300. .name = "p6",
  1301. .handle_irq = p6_pmu_handle_irq,
  1302. .disable_all = p6_pmu_disable_all,
  1303. .enable_all = p6_pmu_enable_all,
  1304. .enable = p6_pmu_enable_counter,
  1305. .disable = p6_pmu_disable_counter,
  1306. .eventsel = MSR_P6_EVNTSEL0,
  1307. .perfctr = MSR_P6_PERFCTR0,
  1308. .event_map = p6_pmu_event_map,
  1309. .raw_event = p6_pmu_raw_event,
  1310. .max_events = ARRAY_SIZE(p6_perfmon_event_map),
  1311. .apic = 1,
  1312. .max_period = (1ULL << 31) - 1,
  1313. .version = 0,
  1314. .num_counters = 2,
  1315. /*
  1316. * Counters have 40 bits implemented. However they are designed such
  1317. * that bits [32-39] are sign extensions of bit 31. As such the
  1318. * effective width of a counter for P6-like PMU is 32 bits only.
  1319. *
  1320. * See IA-32 Intel Architecture Software developer manual Vol 3B
  1321. */
  1322. .counter_bits = 32,
  1323. .counter_mask = (1ULL << 32) - 1,
  1324. };
  1325. static struct x86_pmu intel_pmu = {
  1326. .name = "Intel",
  1327. .handle_irq = intel_pmu_handle_irq,
  1328. .disable_all = intel_pmu_disable_all,
  1329. .enable_all = intel_pmu_enable_all,
  1330. .enable = intel_pmu_enable_counter,
  1331. .disable = intel_pmu_disable_counter,
  1332. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1333. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1334. .event_map = intel_pmu_event_map,
  1335. .raw_event = intel_pmu_raw_event,
  1336. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1337. .apic = 1,
  1338. /*
  1339. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1340. * so we install an artificial 1<<31 period regardless of
  1341. * the generic counter period:
  1342. */
  1343. .max_period = (1ULL << 31) - 1,
  1344. };
  1345. static struct x86_pmu amd_pmu = {
  1346. .name = "AMD",
  1347. .handle_irq = amd_pmu_handle_irq,
  1348. .disable_all = amd_pmu_disable_all,
  1349. .enable_all = amd_pmu_enable_all,
  1350. .enable = amd_pmu_enable_counter,
  1351. .disable = amd_pmu_disable_counter,
  1352. .eventsel = MSR_K7_EVNTSEL0,
  1353. .perfctr = MSR_K7_PERFCTR0,
  1354. .event_map = amd_pmu_event_map,
  1355. .raw_event = amd_pmu_raw_event,
  1356. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  1357. .num_counters = 4,
  1358. .counter_bits = 48,
  1359. .counter_mask = (1ULL << 48) - 1,
  1360. .apic = 1,
  1361. /* use highest bit to detect overflow */
  1362. .max_period = (1ULL << 47) - 1,
  1363. };
  1364. static int p6_pmu_init(void)
  1365. {
  1366. switch (boot_cpu_data.x86_model) {
  1367. case 1:
  1368. case 3: /* Pentium Pro */
  1369. case 5:
  1370. case 6: /* Pentium II */
  1371. case 7:
  1372. case 8:
  1373. case 11: /* Pentium III */
  1374. break;
  1375. case 9:
  1376. case 13:
  1377. /* Pentium M */
  1378. break;
  1379. default:
  1380. pr_cont("unsupported p6 CPU model %d ",
  1381. boot_cpu_data.x86_model);
  1382. return -ENODEV;
  1383. }
  1384. x86_pmu = p6_pmu;
  1385. if (!cpu_has_apic) {
  1386. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1387. pr_info("no hardware sampling interrupt available.\n");
  1388. x86_pmu.apic = 0;
  1389. }
  1390. return 0;
  1391. }
  1392. static int intel_pmu_init(void)
  1393. {
  1394. union cpuid10_edx edx;
  1395. union cpuid10_eax eax;
  1396. unsigned int unused;
  1397. unsigned int ebx;
  1398. int version;
  1399. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  1400. /* check for P6 processor family */
  1401. if (boot_cpu_data.x86 == 6) {
  1402. return p6_pmu_init();
  1403. } else {
  1404. return -ENODEV;
  1405. }
  1406. }
  1407. /*
  1408. * Check whether the Architectural PerfMon supports
  1409. * Branch Misses Retired Event or not.
  1410. */
  1411. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  1412. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  1413. return -ENODEV;
  1414. version = eax.split.version_id;
  1415. if (version < 2)
  1416. return -ENODEV;
  1417. x86_pmu = intel_pmu;
  1418. x86_pmu.version = version;
  1419. x86_pmu.num_counters = eax.split.num_counters;
  1420. x86_pmu.counter_bits = eax.split.bit_width;
  1421. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  1422. /*
  1423. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  1424. * assume at least 3 counters:
  1425. */
  1426. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  1427. /*
  1428. * Install the hw-cache-events table:
  1429. */
  1430. switch (boot_cpu_data.x86_model) {
  1431. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  1432. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  1433. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  1434. case 29: /* six-core 45 nm xeon "Dunnington" */
  1435. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1436. sizeof(hw_cache_event_ids));
  1437. pr_cont("Core2 events, ");
  1438. break;
  1439. default:
  1440. case 26:
  1441. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1442. sizeof(hw_cache_event_ids));
  1443. pr_cont("Nehalem/Corei7 events, ");
  1444. break;
  1445. case 28:
  1446. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1447. sizeof(hw_cache_event_ids));
  1448. pr_cont("Atom events, ");
  1449. break;
  1450. }
  1451. return 0;
  1452. }
  1453. static int amd_pmu_init(void)
  1454. {
  1455. /* Performance-monitoring supported from K7 and later: */
  1456. if (boot_cpu_data.x86 < 6)
  1457. return -ENODEV;
  1458. x86_pmu = amd_pmu;
  1459. /* Events are common for all AMDs */
  1460. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  1461. sizeof(hw_cache_event_ids));
  1462. return 0;
  1463. }
  1464. void __init init_hw_perf_counters(void)
  1465. {
  1466. int err;
  1467. pr_info("Performance Counters: ");
  1468. switch (boot_cpu_data.x86_vendor) {
  1469. case X86_VENDOR_INTEL:
  1470. err = intel_pmu_init();
  1471. break;
  1472. case X86_VENDOR_AMD:
  1473. err = amd_pmu_init();
  1474. break;
  1475. default:
  1476. return;
  1477. }
  1478. if (err != 0) {
  1479. pr_cont("no PMU driver, software counters only.\n");
  1480. return;
  1481. }
  1482. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1483. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1484. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  1485. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1486. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1487. }
  1488. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  1489. perf_max_counters = x86_pmu.num_counters;
  1490. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1491. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  1492. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1493. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1494. }
  1495. perf_counter_mask |=
  1496. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1497. x86_pmu.intel_ctrl = perf_counter_mask;
  1498. perf_counters_lapic_init();
  1499. register_die_notifier(&perf_counter_nmi_notifier);
  1500. pr_info("... version: %d\n", x86_pmu.version);
  1501. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  1502. pr_info("... generic counters: %d\n", x86_pmu.num_counters);
  1503. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  1504. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1505. pr_info("... fixed-purpose counters: %d\n", x86_pmu.num_counters_fixed);
  1506. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  1507. }
  1508. static inline void x86_pmu_read(struct perf_counter *counter)
  1509. {
  1510. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  1511. }
  1512. static const struct pmu pmu = {
  1513. .enable = x86_pmu_enable,
  1514. .disable = x86_pmu_disable,
  1515. .read = x86_pmu_read,
  1516. .unthrottle = x86_pmu_unthrottle,
  1517. };
  1518. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  1519. {
  1520. int err;
  1521. err = __hw_perf_counter_init(counter);
  1522. if (err)
  1523. return ERR_PTR(err);
  1524. return &pmu;
  1525. }
  1526. /*
  1527. * callchain support
  1528. */
  1529. static inline
  1530. void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1531. {
  1532. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1533. entry->ip[entry->nr++] = ip;
  1534. }
  1535. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  1536. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  1537. static DEFINE_PER_CPU(int, in_nmi_frame);
  1538. static void
  1539. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1540. {
  1541. /* Ignore warnings */
  1542. }
  1543. static void backtrace_warning(void *data, char *msg)
  1544. {
  1545. /* Ignore warnings */
  1546. }
  1547. static int backtrace_stack(void *data, char *name)
  1548. {
  1549. per_cpu(in_nmi_frame, smp_processor_id()) =
  1550. x86_is_stack_id(NMI_STACK, name);
  1551. return 0;
  1552. }
  1553. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1554. {
  1555. struct perf_callchain_entry *entry = data;
  1556. if (per_cpu(in_nmi_frame, smp_processor_id()))
  1557. return;
  1558. if (reliable)
  1559. callchain_store(entry, addr);
  1560. }
  1561. static const struct stacktrace_ops backtrace_ops = {
  1562. .warning = backtrace_warning,
  1563. .warning_symbol = backtrace_warning_symbol,
  1564. .stack = backtrace_stack,
  1565. .address = backtrace_address,
  1566. };
  1567. #include "../dumpstack.h"
  1568. static void
  1569. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1570. {
  1571. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1572. callchain_store(entry, regs->ip);
  1573. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1574. }
  1575. /*
  1576. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  1577. */
  1578. static unsigned long
  1579. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  1580. {
  1581. unsigned long offset, addr = (unsigned long)from;
  1582. int type = in_nmi() ? KM_NMI : KM_IRQ0;
  1583. unsigned long size, len = 0;
  1584. struct page *page;
  1585. void *map;
  1586. int ret;
  1587. do {
  1588. ret = __get_user_pages_fast(addr, 1, 0, &page);
  1589. if (!ret)
  1590. break;
  1591. offset = addr & (PAGE_SIZE - 1);
  1592. size = min(PAGE_SIZE - offset, n - len);
  1593. map = kmap_atomic(page, type);
  1594. memcpy(to, map+offset, size);
  1595. kunmap_atomic(map, type);
  1596. put_page(page);
  1597. len += size;
  1598. to += size;
  1599. addr += size;
  1600. } while (len < n);
  1601. return len;
  1602. }
  1603. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  1604. {
  1605. unsigned long bytes;
  1606. bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
  1607. return bytes == sizeof(*frame);
  1608. }
  1609. static void
  1610. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1611. {
  1612. struct stack_frame frame;
  1613. const void __user *fp;
  1614. if (!user_mode(regs))
  1615. regs = task_pt_regs(current);
  1616. fp = (void __user *)regs->bp;
  1617. callchain_store(entry, PERF_CONTEXT_USER);
  1618. callchain_store(entry, regs->ip);
  1619. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1620. frame.next_frame = NULL;
  1621. frame.return_address = 0;
  1622. if (!copy_stack_frame(fp, &frame))
  1623. break;
  1624. if ((unsigned long)fp < regs->sp)
  1625. break;
  1626. callchain_store(entry, frame.return_address);
  1627. fp = frame.next_frame;
  1628. }
  1629. }
  1630. static void
  1631. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1632. {
  1633. int is_user;
  1634. if (!regs)
  1635. return;
  1636. is_user = user_mode(regs);
  1637. if (!current || current->pid == 0)
  1638. return;
  1639. if (is_user && current->state != TASK_RUNNING)
  1640. return;
  1641. if (!is_user)
  1642. perf_callchain_kernel(regs, entry);
  1643. if (current->mm)
  1644. perf_callchain_user(regs, entry);
  1645. }
  1646. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1647. {
  1648. struct perf_callchain_entry *entry;
  1649. if (in_nmi())
  1650. entry = &__get_cpu_var(nmi_entry);
  1651. else
  1652. entry = &__get_cpu_var(irq_entry);
  1653. entry->nr = 0;
  1654. perf_do_callchain(regs, entry);
  1655. return entry;
  1656. }