perf_counter.c 45 KB

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