perf_counter.c 40 KB

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