perf_counter.c 51 KB

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