perf_counter.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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) ] = 0x0480, /* 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) ] = 0x0185, /* 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) ] = 0x2241, /* 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) ] = 0x0080, /* L1I.READS */
  290. [ C(RESULT_MISS) ] = 0x0081, /* 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) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  318. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  319. },
  320. [ C(OP_WRITE) ] = {
  321. [ C(RESULT_ACCESS) ] = 0x0f41, /* 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. counter->destroy = hw_perf_counter_destroy;
  641. /*
  642. * Raw event type provide the config in the event structure
  643. */
  644. if (attr->type == PERF_TYPE_RAW) {
  645. hwc->config |= x86_pmu.raw_event(attr->config);
  646. return 0;
  647. }
  648. if (attr->type == PERF_TYPE_HW_CACHE)
  649. return set_ext_hw_attr(hwc, attr);
  650. if (attr->config >= x86_pmu.max_events)
  651. return -EINVAL;
  652. /*
  653. * The generic map:
  654. */
  655. hwc->config |= x86_pmu.event_map(attr->config);
  656. return 0;
  657. }
  658. static void intel_pmu_disable_all(void)
  659. {
  660. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  661. }
  662. static void amd_pmu_disable_all(void)
  663. {
  664. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  665. int idx;
  666. if (!cpuc->enabled)
  667. return;
  668. cpuc->enabled = 0;
  669. /*
  670. * ensure we write the disable before we start disabling the
  671. * counters proper, so that amd_pmu_enable_counter() does the
  672. * right thing.
  673. */
  674. barrier();
  675. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  676. u64 val;
  677. if (!test_bit(idx, cpuc->active_mask))
  678. continue;
  679. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  680. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  681. continue;
  682. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  683. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  684. }
  685. }
  686. void hw_perf_disable(void)
  687. {
  688. if (!x86_pmu_initialized())
  689. return;
  690. return x86_pmu.disable_all();
  691. }
  692. static void intel_pmu_enable_all(void)
  693. {
  694. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  695. }
  696. static void amd_pmu_enable_all(void)
  697. {
  698. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  699. int idx;
  700. if (cpuc->enabled)
  701. return;
  702. cpuc->enabled = 1;
  703. barrier();
  704. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  705. u64 val;
  706. if (!test_bit(idx, cpuc->active_mask))
  707. continue;
  708. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  709. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  710. continue;
  711. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  712. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  713. }
  714. }
  715. void hw_perf_enable(void)
  716. {
  717. if (!x86_pmu_initialized())
  718. return;
  719. x86_pmu.enable_all();
  720. }
  721. static inline u64 intel_pmu_get_status(void)
  722. {
  723. u64 status;
  724. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  725. return status;
  726. }
  727. static inline void intel_pmu_ack_status(u64 ack)
  728. {
  729. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  730. }
  731. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  732. {
  733. int err;
  734. err = checking_wrmsrl(hwc->config_base + idx,
  735. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  736. }
  737. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  738. {
  739. int err;
  740. err = checking_wrmsrl(hwc->config_base + idx,
  741. hwc->config);
  742. }
  743. static inline void
  744. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  745. {
  746. int idx = __idx - X86_PMC_IDX_FIXED;
  747. u64 ctrl_val, mask;
  748. int err;
  749. mask = 0xfULL << (idx * 4);
  750. rdmsrl(hwc->config_base, ctrl_val);
  751. ctrl_val &= ~mask;
  752. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  753. }
  754. static inline void
  755. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  756. {
  757. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  758. intel_pmu_disable_fixed(hwc, idx);
  759. return;
  760. }
  761. x86_pmu_disable_counter(hwc, idx);
  762. }
  763. static inline void
  764. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  765. {
  766. x86_pmu_disable_counter(hwc, idx);
  767. }
  768. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  769. /*
  770. * Set the next IRQ period, based on the hwc->period_left value.
  771. * To be called with the counter disabled in hw:
  772. */
  773. static int
  774. x86_perf_counter_set_period(struct perf_counter *counter,
  775. struct hw_perf_counter *hwc, int idx)
  776. {
  777. s64 left = atomic64_read(&hwc->period_left);
  778. s64 period = hwc->sample_period;
  779. int err, ret = 0;
  780. /*
  781. * If we are way outside a reasoable range then just skip forward:
  782. */
  783. if (unlikely(left <= -period)) {
  784. left = period;
  785. atomic64_set(&hwc->period_left, left);
  786. ret = 1;
  787. }
  788. if (unlikely(left <= 0)) {
  789. left += period;
  790. atomic64_set(&hwc->period_left, left);
  791. ret = 1;
  792. }
  793. /*
  794. * Quirk: certain CPUs dont like it if just 1 event is left:
  795. */
  796. if (unlikely(left < 2))
  797. left = 2;
  798. if (left > x86_pmu.max_period)
  799. left = x86_pmu.max_period;
  800. per_cpu(prev_left[idx], smp_processor_id()) = left;
  801. /*
  802. * The hw counter starts counting from this counter offset,
  803. * mark it to be able to extra future deltas:
  804. */
  805. atomic64_set(&hwc->prev_count, (u64)-left);
  806. err = checking_wrmsrl(hwc->counter_base + idx,
  807. (u64)(-left) & x86_pmu.counter_mask);
  808. return ret;
  809. }
  810. static inline void
  811. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  812. {
  813. int idx = __idx - X86_PMC_IDX_FIXED;
  814. u64 ctrl_val, bits, mask;
  815. int err;
  816. /*
  817. * Enable IRQ generation (0x8),
  818. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  819. * if requested:
  820. */
  821. bits = 0x8ULL;
  822. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  823. bits |= 0x2;
  824. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  825. bits |= 0x1;
  826. bits <<= (idx * 4);
  827. mask = 0xfULL << (idx * 4);
  828. rdmsrl(hwc->config_base, ctrl_val);
  829. ctrl_val &= ~mask;
  830. ctrl_val |= bits;
  831. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  832. }
  833. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  834. {
  835. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  836. intel_pmu_enable_fixed(hwc, idx);
  837. return;
  838. }
  839. x86_pmu_enable_counter(hwc, idx);
  840. }
  841. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  842. {
  843. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  844. if (cpuc->enabled)
  845. x86_pmu_enable_counter(hwc, idx);
  846. else
  847. x86_pmu_disable_counter(hwc, idx);
  848. }
  849. static int
  850. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  851. {
  852. unsigned int event;
  853. if (!x86_pmu.num_counters_fixed)
  854. return -1;
  855. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  856. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  857. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  858. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  859. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  860. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  861. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  862. return -1;
  863. }
  864. /*
  865. * Find a PMC slot for the freshly enabled / scheduled in counter:
  866. */
  867. static int x86_pmu_enable(struct perf_counter *counter)
  868. {
  869. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  870. struct hw_perf_counter *hwc = &counter->hw;
  871. int idx;
  872. idx = fixed_mode_idx(counter, hwc);
  873. if (idx >= 0) {
  874. /*
  875. * Try to get the fixed counter, if that is already taken
  876. * then try to get a generic counter:
  877. */
  878. if (test_and_set_bit(idx, cpuc->used_mask))
  879. goto try_generic;
  880. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  881. /*
  882. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  883. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  884. */
  885. hwc->counter_base =
  886. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  887. hwc->idx = idx;
  888. } else {
  889. idx = hwc->idx;
  890. /* Try to get the previous generic counter again */
  891. if (test_and_set_bit(idx, cpuc->used_mask)) {
  892. try_generic:
  893. idx = find_first_zero_bit(cpuc->used_mask,
  894. x86_pmu.num_counters);
  895. if (idx == x86_pmu.num_counters)
  896. return -EAGAIN;
  897. set_bit(idx, cpuc->used_mask);
  898. hwc->idx = idx;
  899. }
  900. hwc->config_base = x86_pmu.eventsel;
  901. hwc->counter_base = x86_pmu.perfctr;
  902. }
  903. perf_counters_lapic_init();
  904. x86_pmu.disable(hwc, idx);
  905. cpuc->counters[idx] = counter;
  906. set_bit(idx, cpuc->active_mask);
  907. x86_perf_counter_set_period(counter, hwc, idx);
  908. x86_pmu.enable(hwc, idx);
  909. return 0;
  910. }
  911. static void x86_pmu_unthrottle(struct perf_counter *counter)
  912. {
  913. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  914. struct hw_perf_counter *hwc = &counter->hw;
  915. if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
  916. cpuc->counters[hwc->idx] != counter))
  917. return;
  918. x86_pmu.enable(hwc, hwc->idx);
  919. }
  920. void perf_counter_print_debug(void)
  921. {
  922. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  923. struct cpu_hw_counters *cpuc;
  924. unsigned long flags;
  925. int cpu, idx;
  926. if (!x86_pmu.num_counters)
  927. return;
  928. local_irq_save(flags);
  929. cpu = smp_processor_id();
  930. cpuc = &per_cpu(cpu_hw_counters, cpu);
  931. if (x86_pmu.version >= 2) {
  932. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  933. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  934. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  935. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  936. pr_info("\n");
  937. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  938. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  939. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  940. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  941. }
  942. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  943. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  944. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  945. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  946. prev_left = per_cpu(prev_left[idx], cpu);
  947. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  948. cpu, idx, pmc_ctrl);
  949. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  950. cpu, idx, pmc_count);
  951. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  952. cpu, idx, prev_left);
  953. }
  954. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  955. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  956. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  957. cpu, idx, pmc_count);
  958. }
  959. local_irq_restore(flags);
  960. }
  961. static void x86_pmu_disable(struct perf_counter *counter)
  962. {
  963. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  964. struct hw_perf_counter *hwc = &counter->hw;
  965. int idx = hwc->idx;
  966. /*
  967. * Must be done before we disable, otherwise the nmi handler
  968. * could reenable again:
  969. */
  970. clear_bit(idx, cpuc->active_mask);
  971. x86_pmu.disable(hwc, idx);
  972. /*
  973. * Make sure the cleared pointer becomes visible before we
  974. * (potentially) free the counter:
  975. */
  976. barrier();
  977. /*
  978. * Drain the remaining delta count out of a counter
  979. * that we are disabling:
  980. */
  981. x86_perf_counter_update(counter, hwc, idx);
  982. cpuc->counters[idx] = NULL;
  983. clear_bit(idx, cpuc->used_mask);
  984. }
  985. /*
  986. * Save and restart an expired counter. Called by NMI contexts,
  987. * so it has to be careful about preempting normal counter ops:
  988. */
  989. static int intel_pmu_save_and_restart(struct perf_counter *counter)
  990. {
  991. struct hw_perf_counter *hwc = &counter->hw;
  992. int idx = hwc->idx;
  993. int ret;
  994. x86_perf_counter_update(counter, hwc, idx);
  995. ret = x86_perf_counter_set_period(counter, hwc, idx);
  996. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  997. intel_pmu_enable_counter(hwc, idx);
  998. return ret;
  999. }
  1000. static void intel_pmu_reset(void)
  1001. {
  1002. unsigned long flags;
  1003. int idx;
  1004. if (!x86_pmu.num_counters)
  1005. return;
  1006. local_irq_save(flags);
  1007. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  1008. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1009. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  1010. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  1011. }
  1012. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1013. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1014. }
  1015. local_irq_restore(flags);
  1016. }
  1017. /*
  1018. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1019. * rules apply:
  1020. */
  1021. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1022. {
  1023. struct cpu_hw_counters *cpuc;
  1024. struct cpu_hw_counters;
  1025. int bit, cpu, loops;
  1026. u64 ack, status;
  1027. cpu = smp_processor_id();
  1028. cpuc = &per_cpu(cpu_hw_counters, cpu);
  1029. perf_disable();
  1030. status = intel_pmu_get_status();
  1031. if (!status) {
  1032. perf_enable();
  1033. return 0;
  1034. }
  1035. loops = 0;
  1036. again:
  1037. if (++loops > 100) {
  1038. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  1039. perf_counter_print_debug();
  1040. intel_pmu_reset();
  1041. perf_enable();
  1042. return 1;
  1043. }
  1044. inc_irq_stat(apic_perf_irqs);
  1045. ack = status;
  1046. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1047. struct perf_counter *counter = cpuc->counters[bit];
  1048. clear_bit(bit, (unsigned long *) &status);
  1049. if (!test_bit(bit, cpuc->active_mask))
  1050. continue;
  1051. if (!intel_pmu_save_and_restart(counter))
  1052. continue;
  1053. if (perf_counter_overflow(counter, 1, regs, 0))
  1054. intel_pmu_disable_counter(&counter->hw, bit);
  1055. }
  1056. intel_pmu_ack_status(ack);
  1057. /*
  1058. * Repeat if there is more work to be done:
  1059. */
  1060. status = intel_pmu_get_status();
  1061. if (status)
  1062. goto again;
  1063. perf_enable();
  1064. return 1;
  1065. }
  1066. static int amd_pmu_handle_irq(struct pt_regs *regs)
  1067. {
  1068. int cpu, idx, handled = 0;
  1069. struct cpu_hw_counters *cpuc;
  1070. struct perf_counter *counter;
  1071. struct hw_perf_counter *hwc;
  1072. u64 val;
  1073. cpu = smp_processor_id();
  1074. cpuc = &per_cpu(cpu_hw_counters, cpu);
  1075. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1076. if (!test_bit(idx, cpuc->active_mask))
  1077. continue;
  1078. counter = cpuc->counters[idx];
  1079. hwc = &counter->hw;
  1080. val = x86_perf_counter_update(counter, hwc, idx);
  1081. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  1082. continue;
  1083. /* counter overflow */
  1084. handled = 1;
  1085. inc_irq_stat(apic_perf_irqs);
  1086. if (!x86_perf_counter_set_period(counter, hwc, idx))
  1087. continue;
  1088. if (perf_counter_overflow(counter, 1, regs, 0))
  1089. amd_pmu_disable_counter(hwc, idx);
  1090. }
  1091. return handled;
  1092. }
  1093. void smp_perf_pending_interrupt(struct pt_regs *regs)
  1094. {
  1095. irq_enter();
  1096. ack_APIC_irq();
  1097. inc_irq_stat(apic_pending_irqs);
  1098. perf_counter_do_pending();
  1099. irq_exit();
  1100. }
  1101. void set_perf_counter_pending(void)
  1102. {
  1103. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  1104. }
  1105. void perf_counters_lapic_init(void)
  1106. {
  1107. if (!x86_pmu_initialized())
  1108. return;
  1109. /*
  1110. * Always use NMI for PMU
  1111. */
  1112. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1113. }
  1114. static int __kprobes
  1115. perf_counter_nmi_handler(struct notifier_block *self,
  1116. unsigned long cmd, void *__args)
  1117. {
  1118. struct die_args *args = __args;
  1119. struct pt_regs *regs;
  1120. if (!atomic_read(&active_counters))
  1121. return NOTIFY_DONE;
  1122. switch (cmd) {
  1123. case DIE_NMI:
  1124. case DIE_NMI_IPI:
  1125. break;
  1126. default:
  1127. return NOTIFY_DONE;
  1128. }
  1129. regs = args->regs;
  1130. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1131. /*
  1132. * Can't rely on the handled return value to say it was our NMI, two
  1133. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  1134. *
  1135. * If the first NMI handles both, the latter will be empty and daze
  1136. * the CPU.
  1137. */
  1138. x86_pmu.handle_irq(regs);
  1139. return NOTIFY_STOP;
  1140. }
  1141. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  1142. .notifier_call = perf_counter_nmi_handler,
  1143. .next = NULL,
  1144. .priority = 1
  1145. };
  1146. static struct x86_pmu intel_pmu = {
  1147. .name = "Intel",
  1148. .handle_irq = intel_pmu_handle_irq,
  1149. .disable_all = intel_pmu_disable_all,
  1150. .enable_all = intel_pmu_enable_all,
  1151. .enable = intel_pmu_enable_counter,
  1152. .disable = intel_pmu_disable_counter,
  1153. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1154. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1155. .event_map = intel_pmu_event_map,
  1156. .raw_event = intel_pmu_raw_event,
  1157. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1158. /*
  1159. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1160. * so we install an artificial 1<<31 period regardless of
  1161. * the generic counter period:
  1162. */
  1163. .max_period = (1ULL << 31) - 1,
  1164. };
  1165. static struct x86_pmu amd_pmu = {
  1166. .name = "AMD",
  1167. .handle_irq = amd_pmu_handle_irq,
  1168. .disable_all = amd_pmu_disable_all,
  1169. .enable_all = amd_pmu_enable_all,
  1170. .enable = amd_pmu_enable_counter,
  1171. .disable = amd_pmu_disable_counter,
  1172. .eventsel = MSR_K7_EVNTSEL0,
  1173. .perfctr = MSR_K7_PERFCTR0,
  1174. .event_map = amd_pmu_event_map,
  1175. .raw_event = amd_pmu_raw_event,
  1176. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  1177. .num_counters = 4,
  1178. .counter_bits = 48,
  1179. .counter_mask = (1ULL << 48) - 1,
  1180. /* use highest bit to detect overflow */
  1181. .max_period = (1ULL << 47) - 1,
  1182. };
  1183. static int intel_pmu_init(void)
  1184. {
  1185. union cpuid10_edx edx;
  1186. union cpuid10_eax eax;
  1187. unsigned int unused;
  1188. unsigned int ebx;
  1189. int version;
  1190. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  1191. return -ENODEV;
  1192. /*
  1193. * Check whether the Architectural PerfMon supports
  1194. * Branch Misses Retired Event or not.
  1195. */
  1196. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  1197. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  1198. return -ENODEV;
  1199. version = eax.split.version_id;
  1200. if (version < 2)
  1201. return -ENODEV;
  1202. x86_pmu = intel_pmu;
  1203. x86_pmu.version = version;
  1204. x86_pmu.num_counters = eax.split.num_counters;
  1205. x86_pmu.counter_bits = eax.split.bit_width;
  1206. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  1207. /*
  1208. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  1209. * assume at least 3 counters:
  1210. */
  1211. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  1212. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  1213. /*
  1214. * Install the hw-cache-events table:
  1215. */
  1216. switch (boot_cpu_data.x86_model) {
  1217. case 17:
  1218. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1219. sizeof(u64)*PERF_COUNT_HW_CACHE_MAX*
  1220. PERF_COUNT_HW_CACHE_OP_MAX*PERF_COUNT_HW_CACHE_RESULT_MAX);
  1221. pr_cont("Core2 events, ");
  1222. break;
  1223. default:
  1224. case 26:
  1225. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1226. sizeof(u64)*PERF_COUNT_HW_CACHE_MAX*
  1227. PERF_COUNT_HW_CACHE_OP_MAX*PERF_COUNT_HW_CACHE_RESULT_MAX);
  1228. pr_cont("Nehalem/Corei7 events, ");
  1229. break;
  1230. case 28:
  1231. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1232. sizeof(u64)*PERF_COUNT_HW_CACHE_MAX*
  1233. PERF_COUNT_HW_CACHE_OP_MAX*PERF_COUNT_HW_CACHE_RESULT_MAX);
  1234. pr_cont("Atom events, ");
  1235. break;
  1236. }
  1237. return 0;
  1238. }
  1239. static int amd_pmu_init(void)
  1240. {
  1241. x86_pmu = amd_pmu;
  1242. switch (boot_cpu_data.x86) {
  1243. case 0x0f:
  1244. case 0x10:
  1245. case 0x11:
  1246. memcpy(hw_cache_event_ids, amd_0f_hw_cache_event_ids,
  1247. sizeof(hw_cache_event_ids));
  1248. pr_cont("AMD Family 0f/10/11 events, ");
  1249. break;
  1250. }
  1251. return 0;
  1252. }
  1253. void __init init_hw_perf_counters(void)
  1254. {
  1255. int err;
  1256. pr_info("Performance Counters: ");
  1257. switch (boot_cpu_data.x86_vendor) {
  1258. case X86_VENDOR_INTEL:
  1259. err = intel_pmu_init();
  1260. break;
  1261. case X86_VENDOR_AMD:
  1262. err = amd_pmu_init();
  1263. break;
  1264. default:
  1265. return;
  1266. }
  1267. if (err != 0) {
  1268. pr_cont("no PMU driver, software counters only.\n");
  1269. return;
  1270. }
  1271. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1272. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1273. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1274. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  1275. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1276. }
  1277. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  1278. perf_max_counters = x86_pmu.num_counters;
  1279. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1280. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1281. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  1282. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1283. }
  1284. perf_counter_mask |=
  1285. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1286. perf_counters_lapic_init();
  1287. register_die_notifier(&perf_counter_nmi_notifier);
  1288. pr_info("... version: %d\n", x86_pmu.version);
  1289. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  1290. pr_info("... generic counters: %d\n", x86_pmu.num_counters);
  1291. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  1292. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1293. pr_info("... fixed-purpose counters: %d\n", x86_pmu.num_counters_fixed);
  1294. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  1295. }
  1296. static inline void x86_pmu_read(struct perf_counter *counter)
  1297. {
  1298. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  1299. }
  1300. static const struct pmu pmu = {
  1301. .enable = x86_pmu_enable,
  1302. .disable = x86_pmu_disable,
  1303. .read = x86_pmu_read,
  1304. .unthrottle = x86_pmu_unthrottle,
  1305. };
  1306. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  1307. {
  1308. int err;
  1309. err = __hw_perf_counter_init(counter);
  1310. if (err)
  1311. return ERR_PTR(err);
  1312. return &pmu;
  1313. }
  1314. /*
  1315. * callchain support
  1316. */
  1317. static inline
  1318. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  1319. {
  1320. if (entry->nr < MAX_STACK_DEPTH)
  1321. entry->ip[entry->nr++] = ip;
  1322. }
  1323. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  1324. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  1325. static void
  1326. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1327. {
  1328. /* Ignore warnings */
  1329. }
  1330. static void backtrace_warning(void *data, char *msg)
  1331. {
  1332. /* Ignore warnings */
  1333. }
  1334. static int backtrace_stack(void *data, char *name)
  1335. {
  1336. /* Don't bother with IRQ stacks for now */
  1337. return -1;
  1338. }
  1339. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1340. {
  1341. struct perf_callchain_entry *entry = data;
  1342. if (reliable)
  1343. callchain_store(entry, addr);
  1344. }
  1345. static const struct stacktrace_ops backtrace_ops = {
  1346. .warning = backtrace_warning,
  1347. .warning_symbol = backtrace_warning_symbol,
  1348. .stack = backtrace_stack,
  1349. .address = backtrace_address,
  1350. };
  1351. static void
  1352. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1353. {
  1354. unsigned long bp;
  1355. char *stack;
  1356. int nr = entry->nr;
  1357. callchain_store(entry, instruction_pointer(regs));
  1358. stack = ((char *)regs + sizeof(struct pt_regs));
  1359. #ifdef CONFIG_FRAME_POINTER
  1360. bp = frame_pointer(regs);
  1361. #else
  1362. bp = 0;
  1363. #endif
  1364. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  1365. entry->kernel = entry->nr - nr;
  1366. }
  1367. struct stack_frame {
  1368. const void __user *next_fp;
  1369. unsigned long return_address;
  1370. };
  1371. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  1372. {
  1373. int ret;
  1374. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  1375. return 0;
  1376. ret = 1;
  1377. pagefault_disable();
  1378. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  1379. ret = 0;
  1380. pagefault_enable();
  1381. return ret;
  1382. }
  1383. static void
  1384. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1385. {
  1386. struct stack_frame frame;
  1387. const void __user *fp;
  1388. int nr = entry->nr;
  1389. regs = (struct pt_regs *)current->thread.sp0 - 1;
  1390. fp = (void __user *)regs->bp;
  1391. callchain_store(entry, regs->ip);
  1392. while (entry->nr < MAX_STACK_DEPTH) {
  1393. frame.next_fp = NULL;
  1394. frame.return_address = 0;
  1395. if (!copy_stack_frame(fp, &frame))
  1396. break;
  1397. if ((unsigned long)fp < user_stack_pointer(regs))
  1398. break;
  1399. callchain_store(entry, frame.return_address);
  1400. fp = frame.next_fp;
  1401. }
  1402. entry->user = entry->nr - nr;
  1403. }
  1404. static void
  1405. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1406. {
  1407. int is_user;
  1408. if (!regs)
  1409. return;
  1410. is_user = user_mode(regs);
  1411. if (!current || current->pid == 0)
  1412. return;
  1413. if (is_user && current->state != TASK_RUNNING)
  1414. return;
  1415. if (!is_user)
  1416. perf_callchain_kernel(regs, entry);
  1417. if (current->mm)
  1418. perf_callchain_user(regs, entry);
  1419. }
  1420. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1421. {
  1422. struct perf_callchain_entry *entry;
  1423. if (in_nmi())
  1424. entry = &__get_cpu_var(nmi_entry);
  1425. else
  1426. entry = &__get_cpu_var(irq_entry);
  1427. entry->nr = 0;
  1428. entry->hv = 0;
  1429. entry->kernel = 0;
  1430. entry->user = 0;
  1431. perf_do_callchain(regs, entry);
  1432. return entry;
  1433. }