perf_counter.c 52 KB

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