perf_counter.c 39 KB

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