perf_counter.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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)(long)ds), (u32)((u64)(long)ds >> 32));
  667. }
  668. static inline void fini_debug_store_on_cpu(int cpu)
  669. {
  670. if (!per_cpu(cpu_hw_counters, cpu).ds)
  671. return;
  672. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  673. }
  674. static void release_bts_hardware(void)
  675. {
  676. int cpu;
  677. if (!bts_available())
  678. return;
  679. get_online_cpus();
  680. for_each_online_cpu(cpu)
  681. fini_debug_store_on_cpu(cpu);
  682. for_each_possible_cpu(cpu) {
  683. struct debug_store *ds = per_cpu(cpu_hw_counters, cpu).ds;
  684. if (!ds)
  685. continue;
  686. per_cpu(cpu_hw_counters, cpu).ds = NULL;
  687. kfree((void *)(long)ds->bts_buffer_base);
  688. kfree(ds);
  689. }
  690. put_online_cpus();
  691. }
  692. static int reserve_bts_hardware(void)
  693. {
  694. int cpu, err = 0;
  695. if (!bts_available())
  696. return -EOPNOTSUPP;
  697. get_online_cpus();
  698. for_each_possible_cpu(cpu) {
  699. struct debug_store *ds;
  700. void *buffer;
  701. err = -ENOMEM;
  702. buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL);
  703. if (unlikely(!buffer))
  704. break;
  705. ds = kzalloc(sizeof(*ds), GFP_KERNEL);
  706. if (unlikely(!ds)) {
  707. kfree(buffer);
  708. break;
  709. }
  710. ds->bts_buffer_base = (u64)(long)buffer;
  711. ds->bts_index = ds->bts_buffer_base;
  712. ds->bts_absolute_maximum =
  713. ds->bts_buffer_base + BTS_BUFFER_SIZE;
  714. ds->bts_interrupt_threshold =
  715. ds->bts_absolute_maximum - BTS_OVFL_TH;
  716. per_cpu(cpu_hw_counters, cpu).ds = ds;
  717. err = 0;
  718. }
  719. if (err)
  720. release_bts_hardware();
  721. else {
  722. for_each_online_cpu(cpu)
  723. init_debug_store_on_cpu(cpu);
  724. }
  725. put_online_cpus();
  726. return err;
  727. }
  728. static void hw_perf_counter_destroy(struct perf_counter *counter)
  729. {
  730. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  731. release_pmc_hardware();
  732. release_bts_hardware();
  733. mutex_unlock(&pmc_reserve_mutex);
  734. }
  735. }
  736. static inline int x86_pmu_initialized(void)
  737. {
  738. return x86_pmu.handle_irq != NULL;
  739. }
  740. static inline int
  741. set_ext_hw_attr(struct hw_perf_counter *hwc, struct perf_counter_attr *attr)
  742. {
  743. unsigned int cache_type, cache_op, cache_result;
  744. u64 config, val;
  745. config = attr->config;
  746. cache_type = (config >> 0) & 0xff;
  747. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  748. return -EINVAL;
  749. cache_op = (config >> 8) & 0xff;
  750. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  751. return -EINVAL;
  752. cache_result = (config >> 16) & 0xff;
  753. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  754. return -EINVAL;
  755. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  756. if (val == 0)
  757. return -ENOENT;
  758. if (val == -1)
  759. return -EINVAL;
  760. hwc->config |= val;
  761. return 0;
  762. }
  763. static void intel_pmu_enable_bts(u64 config)
  764. {
  765. unsigned long debugctlmsr;
  766. debugctlmsr = get_debugctlmsr();
  767. debugctlmsr |= X86_DEBUGCTL_TR;
  768. debugctlmsr |= X86_DEBUGCTL_BTS;
  769. debugctlmsr |= X86_DEBUGCTL_BTINT;
  770. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  771. debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS;
  772. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  773. debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR;
  774. update_debugctlmsr(debugctlmsr);
  775. }
  776. static void intel_pmu_disable_bts(void)
  777. {
  778. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  779. unsigned long debugctlmsr;
  780. if (!cpuc->ds)
  781. return;
  782. debugctlmsr = get_debugctlmsr();
  783. debugctlmsr &=
  784. ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT |
  785. X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR);
  786. update_debugctlmsr(debugctlmsr);
  787. }
  788. /*
  789. * Setup the hardware configuration for a given attr_type
  790. */
  791. static int __hw_perf_counter_init(struct perf_counter *counter)
  792. {
  793. struct perf_counter_attr *attr = &counter->attr;
  794. struct hw_perf_counter *hwc = &counter->hw;
  795. u64 config;
  796. int err;
  797. if (!x86_pmu_initialized())
  798. return -ENODEV;
  799. err = 0;
  800. if (!atomic_inc_not_zero(&active_counters)) {
  801. mutex_lock(&pmc_reserve_mutex);
  802. if (atomic_read(&active_counters) == 0) {
  803. if (!reserve_pmc_hardware())
  804. err = -EBUSY;
  805. else
  806. reserve_bts_hardware();
  807. }
  808. if (!err)
  809. atomic_inc(&active_counters);
  810. mutex_unlock(&pmc_reserve_mutex);
  811. }
  812. if (err)
  813. return err;
  814. /*
  815. * Generate PMC IRQs:
  816. * (keep 'enabled' bit clear for now)
  817. */
  818. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  819. /*
  820. * Count user and OS events unless requested not to.
  821. */
  822. if (!attr->exclude_user)
  823. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  824. if (!attr->exclude_kernel)
  825. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  826. if (!hwc->sample_period) {
  827. hwc->sample_period = x86_pmu.max_period;
  828. hwc->last_period = hwc->sample_period;
  829. atomic64_set(&hwc->period_left, hwc->sample_period);
  830. } else {
  831. /*
  832. * If we have a PMU initialized but no APIC
  833. * interrupts, we cannot sample hardware
  834. * counters (user-space has to fall back and
  835. * sample via a hrtimer based software counter):
  836. */
  837. if (!x86_pmu.apic)
  838. return -EOPNOTSUPP;
  839. }
  840. counter->destroy = hw_perf_counter_destroy;
  841. /*
  842. * Raw event type provide the config in the event structure
  843. */
  844. if (attr->type == PERF_TYPE_RAW) {
  845. hwc->config |= x86_pmu.raw_event(attr->config);
  846. return 0;
  847. }
  848. if (attr->type == PERF_TYPE_HW_CACHE)
  849. return set_ext_hw_attr(hwc, attr);
  850. if (attr->config >= x86_pmu.max_events)
  851. return -EINVAL;
  852. /*
  853. * The generic map:
  854. */
  855. config = x86_pmu.event_map(attr->config);
  856. if (config == 0)
  857. return -ENOENT;
  858. if (config == -1LL)
  859. return -EINVAL;
  860. hwc->config |= config;
  861. return 0;
  862. }
  863. static void p6_pmu_disable_all(void)
  864. {
  865. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  866. u64 val;
  867. if (!cpuc->enabled)
  868. return;
  869. cpuc->enabled = 0;
  870. barrier();
  871. /* p6 only has one enable register */
  872. rdmsrl(MSR_P6_EVNTSEL0, val);
  873. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  874. wrmsrl(MSR_P6_EVNTSEL0, val);
  875. }
  876. static void intel_pmu_disable_all(void)
  877. {
  878. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  879. if (!cpuc->enabled)
  880. return;
  881. cpuc->enabled = 0;
  882. barrier();
  883. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  884. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  885. intel_pmu_disable_bts();
  886. }
  887. static void amd_pmu_disable_all(void)
  888. {
  889. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  890. int idx;
  891. if (!cpuc->enabled)
  892. return;
  893. cpuc->enabled = 0;
  894. /*
  895. * ensure we write the disable before we start disabling the
  896. * counters proper, so that amd_pmu_enable_counter() does the
  897. * right thing.
  898. */
  899. barrier();
  900. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  901. u64 val;
  902. if (!test_bit(idx, cpuc->active_mask))
  903. continue;
  904. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  905. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  906. continue;
  907. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  908. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  909. }
  910. }
  911. void hw_perf_disable(void)
  912. {
  913. if (!x86_pmu_initialized())
  914. return;
  915. return x86_pmu.disable_all();
  916. }
  917. static void p6_pmu_enable_all(void)
  918. {
  919. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  920. unsigned long val;
  921. if (cpuc->enabled)
  922. return;
  923. cpuc->enabled = 1;
  924. barrier();
  925. /* p6 only has one enable register */
  926. rdmsrl(MSR_P6_EVNTSEL0, val);
  927. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  928. wrmsrl(MSR_P6_EVNTSEL0, val);
  929. }
  930. static void intel_pmu_enable_all(void)
  931. {
  932. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  933. if (cpuc->enabled)
  934. return;
  935. cpuc->enabled = 1;
  936. barrier();
  937. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  938. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  939. struct perf_counter *counter =
  940. cpuc->counters[X86_PMC_IDX_FIXED_BTS];
  941. if (WARN_ON_ONCE(!counter))
  942. return;
  943. intel_pmu_enable_bts(counter->hw.config);
  944. }
  945. }
  946. static void amd_pmu_enable_all(void)
  947. {
  948. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  949. int idx;
  950. if (cpuc->enabled)
  951. return;
  952. cpuc->enabled = 1;
  953. barrier();
  954. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  955. struct perf_counter *counter = cpuc->counters[idx];
  956. u64 val;
  957. if (!test_bit(idx, cpuc->active_mask))
  958. continue;
  959. val = counter->hw.config;
  960. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  961. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  962. }
  963. }
  964. void hw_perf_enable(void)
  965. {
  966. if (!x86_pmu_initialized())
  967. return;
  968. x86_pmu.enable_all();
  969. }
  970. static inline u64 intel_pmu_get_status(void)
  971. {
  972. u64 status;
  973. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  974. return status;
  975. }
  976. static inline void intel_pmu_ack_status(u64 ack)
  977. {
  978. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  979. }
  980. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  981. {
  982. (void)checking_wrmsrl(hwc->config_base + idx,
  983. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  984. }
  985. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  986. {
  987. (void)checking_wrmsrl(hwc->config_base + idx, hwc->config);
  988. }
  989. static inline void
  990. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  991. {
  992. int idx = __idx - X86_PMC_IDX_FIXED;
  993. u64 ctrl_val, mask;
  994. mask = 0xfULL << (idx * 4);
  995. rdmsrl(hwc->config_base, ctrl_val);
  996. ctrl_val &= ~mask;
  997. (void)checking_wrmsrl(hwc->config_base, ctrl_val);
  998. }
  999. static inline void
  1000. p6_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  1001. {
  1002. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1003. u64 val = P6_NOP_COUNTER;
  1004. if (cpuc->enabled)
  1005. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  1006. (void)checking_wrmsrl(hwc->config_base + idx, val);
  1007. }
  1008. static inline void
  1009. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  1010. {
  1011. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
  1012. intel_pmu_disable_bts();
  1013. return;
  1014. }
  1015. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1016. intel_pmu_disable_fixed(hwc, idx);
  1017. return;
  1018. }
  1019. x86_pmu_disable_counter(hwc, idx);
  1020. }
  1021. static inline void
  1022. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  1023. {
  1024. x86_pmu_disable_counter(hwc, idx);
  1025. }
  1026. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  1027. /*
  1028. * Set the next IRQ period, based on the hwc->period_left value.
  1029. * To be called with the counter disabled in hw:
  1030. */
  1031. static int
  1032. x86_perf_counter_set_period(struct perf_counter *counter,
  1033. struct hw_perf_counter *hwc, int idx)
  1034. {
  1035. s64 left = atomic64_read(&hwc->period_left);
  1036. s64 period = hwc->sample_period;
  1037. int err, ret = 0;
  1038. if (idx == X86_PMC_IDX_FIXED_BTS)
  1039. return 0;
  1040. /*
  1041. * If we are way outside a reasoable range then just skip forward:
  1042. */
  1043. if (unlikely(left <= -period)) {
  1044. left = period;
  1045. atomic64_set(&hwc->period_left, left);
  1046. hwc->last_period = period;
  1047. ret = 1;
  1048. }
  1049. if (unlikely(left <= 0)) {
  1050. left += period;
  1051. atomic64_set(&hwc->period_left, left);
  1052. hwc->last_period = period;
  1053. ret = 1;
  1054. }
  1055. /*
  1056. * Quirk: certain CPUs dont like it if just 1 event is left:
  1057. */
  1058. if (unlikely(left < 2))
  1059. left = 2;
  1060. if (left > x86_pmu.max_period)
  1061. left = x86_pmu.max_period;
  1062. per_cpu(prev_left[idx], smp_processor_id()) = left;
  1063. /*
  1064. * The hw counter starts counting from this counter offset,
  1065. * mark it to be able to extra future deltas:
  1066. */
  1067. atomic64_set(&hwc->prev_count, (u64)-left);
  1068. err = checking_wrmsrl(hwc->counter_base + idx,
  1069. (u64)(-left) & x86_pmu.counter_mask);
  1070. perf_counter_update_userpage(counter);
  1071. return ret;
  1072. }
  1073. static inline void
  1074. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  1075. {
  1076. int idx = __idx - X86_PMC_IDX_FIXED;
  1077. u64 ctrl_val, bits, mask;
  1078. int err;
  1079. /*
  1080. * Enable IRQ generation (0x8),
  1081. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  1082. * if requested:
  1083. */
  1084. bits = 0x8ULL;
  1085. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  1086. bits |= 0x2;
  1087. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  1088. bits |= 0x1;
  1089. bits <<= (idx * 4);
  1090. mask = 0xfULL << (idx * 4);
  1091. rdmsrl(hwc->config_base, ctrl_val);
  1092. ctrl_val &= ~mask;
  1093. ctrl_val |= bits;
  1094. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  1095. }
  1096. static void p6_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  1097. {
  1098. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1099. u64 val;
  1100. val = hwc->config;
  1101. if (cpuc->enabled)
  1102. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  1103. (void)checking_wrmsrl(hwc->config_base + idx, val);
  1104. }
  1105. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  1106. {
  1107. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
  1108. if (!__get_cpu_var(cpu_hw_counters).enabled)
  1109. return;
  1110. intel_pmu_enable_bts(hwc->config);
  1111. return;
  1112. }
  1113. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1114. intel_pmu_enable_fixed(hwc, idx);
  1115. return;
  1116. }
  1117. x86_pmu_enable_counter(hwc, idx);
  1118. }
  1119. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  1120. {
  1121. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1122. if (cpuc->enabled)
  1123. x86_pmu_enable_counter(hwc, idx);
  1124. }
  1125. static int
  1126. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  1127. {
  1128. unsigned int event;
  1129. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  1130. if (unlikely((event ==
  1131. x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) &&
  1132. (hwc->sample_period == 1)))
  1133. return X86_PMC_IDX_FIXED_BTS;
  1134. if (!x86_pmu.num_counters_fixed)
  1135. return -1;
  1136. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_INSTRUCTIONS)))
  1137. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  1138. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_CPU_CYCLES)))
  1139. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  1140. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_BUS_CYCLES)))
  1141. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  1142. return -1;
  1143. }
  1144. /*
  1145. * Find a PMC slot for the freshly enabled / scheduled in counter:
  1146. */
  1147. static int x86_pmu_enable(struct perf_counter *counter)
  1148. {
  1149. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1150. struct hw_perf_counter *hwc = &counter->hw;
  1151. int idx;
  1152. idx = fixed_mode_idx(counter, hwc);
  1153. if (idx == X86_PMC_IDX_FIXED_BTS) {
  1154. /*
  1155. * Try to use BTS for branch tracing. If that is not
  1156. * available, try to get a generic counter.
  1157. */
  1158. if (unlikely(!cpuc->ds))
  1159. goto try_generic;
  1160. /*
  1161. * Try to get the fixed counter, if that is already taken
  1162. * then try to get a generic counter:
  1163. */
  1164. if (test_and_set_bit(idx, cpuc->used_mask))
  1165. goto try_generic;
  1166. hwc->config_base = 0;
  1167. hwc->counter_base = 0;
  1168. hwc->idx = idx;
  1169. } else if (idx >= 0) {
  1170. /*
  1171. * Try to get the fixed counter, if that is already taken
  1172. * then try to get a generic counter:
  1173. */
  1174. if (test_and_set_bit(idx, cpuc->used_mask))
  1175. goto try_generic;
  1176. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  1177. /*
  1178. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  1179. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  1180. */
  1181. hwc->counter_base =
  1182. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  1183. hwc->idx = idx;
  1184. } else {
  1185. idx = hwc->idx;
  1186. /* Try to get the previous generic counter again */
  1187. if (test_and_set_bit(idx, cpuc->used_mask)) {
  1188. try_generic:
  1189. idx = find_first_zero_bit(cpuc->used_mask,
  1190. x86_pmu.num_counters);
  1191. if (idx == x86_pmu.num_counters)
  1192. return -EAGAIN;
  1193. set_bit(idx, cpuc->used_mask);
  1194. hwc->idx = idx;
  1195. }
  1196. hwc->config_base = x86_pmu.eventsel;
  1197. hwc->counter_base = x86_pmu.perfctr;
  1198. }
  1199. perf_counters_lapic_init();
  1200. x86_pmu.disable(hwc, idx);
  1201. cpuc->counters[idx] = counter;
  1202. set_bit(idx, cpuc->active_mask);
  1203. x86_perf_counter_set_period(counter, hwc, idx);
  1204. x86_pmu.enable(hwc, idx);
  1205. perf_counter_update_userpage(counter);
  1206. return 0;
  1207. }
  1208. static void x86_pmu_unthrottle(struct perf_counter *counter)
  1209. {
  1210. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1211. struct hw_perf_counter *hwc = &counter->hw;
  1212. if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
  1213. cpuc->counters[hwc->idx] != counter))
  1214. return;
  1215. x86_pmu.enable(hwc, hwc->idx);
  1216. }
  1217. void perf_counter_print_debug(void)
  1218. {
  1219. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1220. struct cpu_hw_counters *cpuc;
  1221. unsigned long flags;
  1222. int cpu, idx;
  1223. if (!x86_pmu.num_counters)
  1224. return;
  1225. local_irq_save(flags);
  1226. cpu = smp_processor_id();
  1227. cpuc = &per_cpu(cpu_hw_counters, cpu);
  1228. if (x86_pmu.version >= 2) {
  1229. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1230. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1231. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1232. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1233. pr_info("\n");
  1234. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1235. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1236. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1237. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1238. }
  1239. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  1240. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1241. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  1242. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  1243. prev_left = per_cpu(prev_left[idx], cpu);
  1244. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1245. cpu, idx, pmc_ctrl);
  1246. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1247. cpu, idx, pmc_count);
  1248. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1249. cpu, idx, prev_left);
  1250. }
  1251. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1252. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1253. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1254. cpu, idx, pmc_count);
  1255. }
  1256. local_irq_restore(flags);
  1257. }
  1258. static void intel_pmu_drain_bts_buffer(struct cpu_hw_counters *cpuc,
  1259. struct perf_sample_data *data)
  1260. {
  1261. struct debug_store *ds = cpuc->ds;
  1262. struct bts_record {
  1263. u64 from;
  1264. u64 to;
  1265. u64 flags;
  1266. };
  1267. struct perf_counter *counter = cpuc->counters[X86_PMC_IDX_FIXED_BTS];
  1268. unsigned long orig_ip = data->regs->ip;
  1269. u64 at;
  1270. if (!counter)
  1271. return;
  1272. if (!ds)
  1273. return;
  1274. for (at = ds->bts_buffer_base;
  1275. at < ds->bts_index;
  1276. at += sizeof(struct bts_record)) {
  1277. struct bts_record *rec = (struct bts_record *)(long)at;
  1278. data->regs->ip = rec->from;
  1279. data->addr = rec->to;
  1280. perf_counter_output(counter, 1, data);
  1281. }
  1282. ds->bts_index = ds->bts_buffer_base;
  1283. data->regs->ip = orig_ip;
  1284. data->addr = 0;
  1285. /* There's new data available. */
  1286. counter->pending_kill = POLL_IN;
  1287. }
  1288. static void x86_pmu_disable(struct perf_counter *counter)
  1289. {
  1290. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  1291. struct hw_perf_counter *hwc = &counter->hw;
  1292. int idx = hwc->idx;
  1293. /*
  1294. * Must be done before we disable, otherwise the nmi handler
  1295. * could reenable again:
  1296. */
  1297. clear_bit(idx, cpuc->active_mask);
  1298. x86_pmu.disable(hwc, idx);
  1299. /*
  1300. * Make sure the cleared pointer becomes visible before we
  1301. * (potentially) free the counter:
  1302. */
  1303. barrier();
  1304. /*
  1305. * Drain the remaining delta count out of a counter
  1306. * that we are disabling:
  1307. */
  1308. x86_perf_counter_update(counter, hwc, idx);
  1309. /* Drain the remaining BTS records. */
  1310. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
  1311. struct perf_sample_data data;
  1312. struct pt_regs regs;
  1313. data.regs = &regs;
  1314. intel_pmu_drain_bts_buffer(cpuc, &data);
  1315. }
  1316. cpuc->counters[idx] = NULL;
  1317. clear_bit(idx, cpuc->used_mask);
  1318. perf_counter_update_userpage(counter);
  1319. }
  1320. /*
  1321. * Save and restart an expired counter. Called by NMI contexts,
  1322. * so it has to be careful about preempting normal counter ops:
  1323. */
  1324. static int intel_pmu_save_and_restart(struct perf_counter *counter)
  1325. {
  1326. struct hw_perf_counter *hwc = &counter->hw;
  1327. int idx = hwc->idx;
  1328. int ret;
  1329. x86_perf_counter_update(counter, hwc, idx);
  1330. ret = x86_perf_counter_set_period(counter, hwc, idx);
  1331. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1332. intel_pmu_enable_counter(hwc, idx);
  1333. return ret;
  1334. }
  1335. static void intel_pmu_reset(void)
  1336. {
  1337. struct debug_store *ds = __get_cpu_var(cpu_hw_counters).ds;
  1338. unsigned long flags;
  1339. int idx;
  1340. if (!x86_pmu.num_counters)
  1341. return;
  1342. local_irq_save(flags);
  1343. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  1344. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1345. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  1346. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  1347. }
  1348. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1349. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1350. }
  1351. if (ds)
  1352. ds->bts_index = ds->bts_buffer_base;
  1353. local_irq_restore(flags);
  1354. }
  1355. static int p6_pmu_handle_irq(struct pt_regs *regs)
  1356. {
  1357. struct perf_sample_data data;
  1358. struct cpu_hw_counters *cpuc;
  1359. struct perf_counter *counter;
  1360. struct hw_perf_counter *hwc;
  1361. int idx, handled = 0;
  1362. u64 val;
  1363. data.regs = regs;
  1364. data.addr = 0;
  1365. cpuc = &__get_cpu_var(cpu_hw_counters);
  1366. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1367. if (!test_bit(idx, cpuc->active_mask))
  1368. continue;
  1369. counter = cpuc->counters[idx];
  1370. hwc = &counter->hw;
  1371. val = x86_perf_counter_update(counter, hwc, idx);
  1372. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  1373. continue;
  1374. /*
  1375. * counter overflow
  1376. */
  1377. handled = 1;
  1378. data.period = counter->hw.last_period;
  1379. if (!x86_perf_counter_set_period(counter, hwc, idx))
  1380. continue;
  1381. if (perf_counter_overflow(counter, 1, &data))
  1382. p6_pmu_disable_counter(hwc, idx);
  1383. }
  1384. if (handled)
  1385. inc_irq_stat(apic_perf_irqs);
  1386. return handled;
  1387. }
  1388. /*
  1389. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1390. * rules apply:
  1391. */
  1392. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1393. {
  1394. struct perf_sample_data data;
  1395. struct cpu_hw_counters *cpuc;
  1396. int bit, loops;
  1397. u64 ack, status;
  1398. data.regs = regs;
  1399. data.addr = 0;
  1400. cpuc = &__get_cpu_var(cpu_hw_counters);
  1401. perf_disable();
  1402. intel_pmu_drain_bts_buffer(cpuc, &data);
  1403. status = intel_pmu_get_status();
  1404. if (!status) {
  1405. perf_enable();
  1406. return 0;
  1407. }
  1408. loops = 0;
  1409. again:
  1410. if (++loops > 100) {
  1411. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  1412. perf_counter_print_debug();
  1413. intel_pmu_reset();
  1414. perf_enable();
  1415. return 1;
  1416. }
  1417. inc_irq_stat(apic_perf_irqs);
  1418. ack = status;
  1419. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1420. struct perf_counter *counter = cpuc->counters[bit];
  1421. clear_bit(bit, (unsigned long *) &status);
  1422. if (!test_bit(bit, cpuc->active_mask))
  1423. continue;
  1424. if (!intel_pmu_save_and_restart(counter))
  1425. continue;
  1426. data.period = counter->hw.last_period;
  1427. if (perf_counter_overflow(counter, 1, &data))
  1428. intel_pmu_disable_counter(&counter->hw, bit);
  1429. }
  1430. intel_pmu_ack_status(ack);
  1431. /*
  1432. * Repeat if there is more work to be done:
  1433. */
  1434. status = intel_pmu_get_status();
  1435. if (status)
  1436. goto again;
  1437. perf_enable();
  1438. return 1;
  1439. }
  1440. static int amd_pmu_handle_irq(struct pt_regs *regs)
  1441. {
  1442. struct perf_sample_data data;
  1443. struct cpu_hw_counters *cpuc;
  1444. struct perf_counter *counter;
  1445. struct hw_perf_counter *hwc;
  1446. int idx, handled = 0;
  1447. u64 val;
  1448. data.regs = regs;
  1449. data.addr = 0;
  1450. cpuc = &__get_cpu_var(cpu_hw_counters);
  1451. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1452. if (!test_bit(idx, cpuc->active_mask))
  1453. continue;
  1454. counter = cpuc->counters[idx];
  1455. hwc = &counter->hw;
  1456. val = x86_perf_counter_update(counter, hwc, idx);
  1457. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  1458. continue;
  1459. /*
  1460. * counter overflow
  1461. */
  1462. handled = 1;
  1463. data.period = counter->hw.last_period;
  1464. if (!x86_perf_counter_set_period(counter, hwc, idx))
  1465. continue;
  1466. if (perf_counter_overflow(counter, 1, &data))
  1467. amd_pmu_disable_counter(hwc, idx);
  1468. }
  1469. if (handled)
  1470. inc_irq_stat(apic_perf_irqs);
  1471. return handled;
  1472. }
  1473. void smp_perf_pending_interrupt(struct pt_regs *regs)
  1474. {
  1475. irq_enter();
  1476. ack_APIC_irq();
  1477. inc_irq_stat(apic_pending_irqs);
  1478. perf_counter_do_pending();
  1479. irq_exit();
  1480. }
  1481. void set_perf_counter_pending(void)
  1482. {
  1483. #ifdef CONFIG_X86_LOCAL_APIC
  1484. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  1485. #endif
  1486. }
  1487. void perf_counters_lapic_init(void)
  1488. {
  1489. #ifdef CONFIG_X86_LOCAL_APIC
  1490. if (!x86_pmu.apic || !x86_pmu_initialized())
  1491. return;
  1492. /*
  1493. * Always use NMI for PMU
  1494. */
  1495. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1496. #endif
  1497. }
  1498. static int __kprobes
  1499. perf_counter_nmi_handler(struct notifier_block *self,
  1500. unsigned long cmd, void *__args)
  1501. {
  1502. struct die_args *args = __args;
  1503. struct pt_regs *regs;
  1504. if (!atomic_read(&active_counters))
  1505. return NOTIFY_DONE;
  1506. switch (cmd) {
  1507. case DIE_NMI:
  1508. case DIE_NMI_IPI:
  1509. break;
  1510. default:
  1511. return NOTIFY_DONE;
  1512. }
  1513. regs = args->regs;
  1514. #ifdef CONFIG_X86_LOCAL_APIC
  1515. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1516. #endif
  1517. /*
  1518. * Can't rely on the handled return value to say it was our NMI, two
  1519. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  1520. *
  1521. * If the first NMI handles both, the latter will be empty and daze
  1522. * the CPU.
  1523. */
  1524. x86_pmu.handle_irq(regs);
  1525. return NOTIFY_STOP;
  1526. }
  1527. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  1528. .notifier_call = perf_counter_nmi_handler,
  1529. .next = NULL,
  1530. .priority = 1
  1531. };
  1532. static struct x86_pmu p6_pmu = {
  1533. .name = "p6",
  1534. .handle_irq = p6_pmu_handle_irq,
  1535. .disable_all = p6_pmu_disable_all,
  1536. .enable_all = p6_pmu_enable_all,
  1537. .enable = p6_pmu_enable_counter,
  1538. .disable = p6_pmu_disable_counter,
  1539. .eventsel = MSR_P6_EVNTSEL0,
  1540. .perfctr = MSR_P6_PERFCTR0,
  1541. .event_map = p6_pmu_event_map,
  1542. .raw_event = p6_pmu_raw_event,
  1543. .max_events = ARRAY_SIZE(p6_perfmon_event_map),
  1544. .apic = 1,
  1545. .max_period = (1ULL << 31) - 1,
  1546. .version = 0,
  1547. .num_counters = 2,
  1548. /*
  1549. * Counters have 40 bits implemented. However they are designed such
  1550. * that bits [32-39] are sign extensions of bit 31. As such the
  1551. * effective width of a counter for P6-like PMU is 32 bits only.
  1552. *
  1553. * See IA-32 Intel Architecture Software developer manual Vol 3B
  1554. */
  1555. .counter_bits = 32,
  1556. .counter_mask = (1ULL << 32) - 1,
  1557. };
  1558. static struct x86_pmu intel_pmu = {
  1559. .name = "Intel",
  1560. .handle_irq = intel_pmu_handle_irq,
  1561. .disable_all = intel_pmu_disable_all,
  1562. .enable_all = intel_pmu_enable_all,
  1563. .enable = intel_pmu_enable_counter,
  1564. .disable = intel_pmu_disable_counter,
  1565. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1566. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1567. .event_map = intel_pmu_event_map,
  1568. .raw_event = intel_pmu_raw_event,
  1569. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1570. .apic = 1,
  1571. /*
  1572. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1573. * so we install an artificial 1<<31 period regardless of
  1574. * the generic counter period:
  1575. */
  1576. .max_period = (1ULL << 31) - 1,
  1577. .enable_bts = intel_pmu_enable_bts,
  1578. .disable_bts = intel_pmu_disable_bts,
  1579. };
  1580. static struct x86_pmu amd_pmu = {
  1581. .name = "AMD",
  1582. .handle_irq = amd_pmu_handle_irq,
  1583. .disable_all = amd_pmu_disable_all,
  1584. .enable_all = amd_pmu_enable_all,
  1585. .enable = amd_pmu_enable_counter,
  1586. .disable = amd_pmu_disable_counter,
  1587. .eventsel = MSR_K7_EVNTSEL0,
  1588. .perfctr = MSR_K7_PERFCTR0,
  1589. .event_map = amd_pmu_event_map,
  1590. .raw_event = amd_pmu_raw_event,
  1591. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  1592. .num_counters = 4,
  1593. .counter_bits = 48,
  1594. .counter_mask = (1ULL << 48) - 1,
  1595. .apic = 1,
  1596. /* use highest bit to detect overflow */
  1597. .max_period = (1ULL << 47) - 1,
  1598. };
  1599. static int p6_pmu_init(void)
  1600. {
  1601. switch (boot_cpu_data.x86_model) {
  1602. case 1:
  1603. case 3: /* Pentium Pro */
  1604. case 5:
  1605. case 6: /* Pentium II */
  1606. case 7:
  1607. case 8:
  1608. case 11: /* Pentium III */
  1609. break;
  1610. case 9:
  1611. case 13:
  1612. /* Pentium M */
  1613. break;
  1614. default:
  1615. pr_cont("unsupported p6 CPU model %d ",
  1616. boot_cpu_data.x86_model);
  1617. return -ENODEV;
  1618. }
  1619. x86_pmu = p6_pmu;
  1620. if (!cpu_has_apic) {
  1621. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1622. pr_info("no hardware sampling interrupt available.\n");
  1623. x86_pmu.apic = 0;
  1624. }
  1625. return 0;
  1626. }
  1627. static int intel_pmu_init(void)
  1628. {
  1629. union cpuid10_edx edx;
  1630. union cpuid10_eax eax;
  1631. unsigned int unused;
  1632. unsigned int ebx;
  1633. int version;
  1634. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  1635. /* check for P6 processor family */
  1636. if (boot_cpu_data.x86 == 6) {
  1637. return p6_pmu_init();
  1638. } else {
  1639. return -ENODEV;
  1640. }
  1641. }
  1642. /*
  1643. * Check whether the Architectural PerfMon supports
  1644. * Branch Misses Retired Event or not.
  1645. */
  1646. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  1647. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  1648. return -ENODEV;
  1649. version = eax.split.version_id;
  1650. if (version < 2)
  1651. return -ENODEV;
  1652. x86_pmu = intel_pmu;
  1653. x86_pmu.version = version;
  1654. x86_pmu.num_counters = eax.split.num_counters;
  1655. x86_pmu.counter_bits = eax.split.bit_width;
  1656. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  1657. /*
  1658. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  1659. * assume at least 3 counters:
  1660. */
  1661. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  1662. /*
  1663. * Install the hw-cache-events table:
  1664. */
  1665. switch (boot_cpu_data.x86_model) {
  1666. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  1667. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  1668. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  1669. case 29: /* six-core 45 nm xeon "Dunnington" */
  1670. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1671. sizeof(hw_cache_event_ids));
  1672. pr_cont("Core2 events, ");
  1673. break;
  1674. default:
  1675. case 26:
  1676. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1677. sizeof(hw_cache_event_ids));
  1678. pr_cont("Nehalem/Corei7 events, ");
  1679. break;
  1680. case 28:
  1681. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1682. sizeof(hw_cache_event_ids));
  1683. pr_cont("Atom events, ");
  1684. break;
  1685. }
  1686. return 0;
  1687. }
  1688. static int amd_pmu_init(void)
  1689. {
  1690. /* Performance-monitoring supported from K7 and later: */
  1691. if (boot_cpu_data.x86 < 6)
  1692. return -ENODEV;
  1693. x86_pmu = amd_pmu;
  1694. /* Events are common for all AMDs */
  1695. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  1696. sizeof(hw_cache_event_ids));
  1697. return 0;
  1698. }
  1699. void __init init_hw_perf_counters(void)
  1700. {
  1701. int err;
  1702. pr_info("Performance Counters: ");
  1703. switch (boot_cpu_data.x86_vendor) {
  1704. case X86_VENDOR_INTEL:
  1705. err = intel_pmu_init();
  1706. break;
  1707. case X86_VENDOR_AMD:
  1708. err = amd_pmu_init();
  1709. break;
  1710. default:
  1711. return;
  1712. }
  1713. if (err != 0) {
  1714. pr_cont("no PMU driver, software counters only.\n");
  1715. return;
  1716. }
  1717. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1718. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1719. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  1720. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1721. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1722. }
  1723. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  1724. perf_max_counters = x86_pmu.num_counters;
  1725. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1726. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  1727. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1728. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1729. }
  1730. perf_counter_mask |=
  1731. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1732. x86_pmu.intel_ctrl = perf_counter_mask;
  1733. perf_counters_lapic_init();
  1734. register_die_notifier(&perf_counter_nmi_notifier);
  1735. pr_info("... version: %d\n", x86_pmu.version);
  1736. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  1737. pr_info("... generic counters: %d\n", x86_pmu.num_counters);
  1738. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  1739. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1740. pr_info("... fixed-purpose counters: %d\n", x86_pmu.num_counters_fixed);
  1741. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  1742. }
  1743. static inline void x86_pmu_read(struct perf_counter *counter)
  1744. {
  1745. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  1746. }
  1747. static const struct pmu pmu = {
  1748. .enable = x86_pmu_enable,
  1749. .disable = x86_pmu_disable,
  1750. .read = x86_pmu_read,
  1751. .unthrottle = x86_pmu_unthrottle,
  1752. };
  1753. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  1754. {
  1755. int err;
  1756. err = __hw_perf_counter_init(counter);
  1757. if (err)
  1758. return ERR_PTR(err);
  1759. return &pmu;
  1760. }
  1761. /*
  1762. * callchain support
  1763. */
  1764. static inline
  1765. void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1766. {
  1767. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1768. entry->ip[entry->nr++] = ip;
  1769. }
  1770. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  1771. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  1772. static DEFINE_PER_CPU(int, in_nmi_frame);
  1773. static void
  1774. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1775. {
  1776. /* Ignore warnings */
  1777. }
  1778. static void backtrace_warning(void *data, char *msg)
  1779. {
  1780. /* Ignore warnings */
  1781. }
  1782. static int backtrace_stack(void *data, char *name)
  1783. {
  1784. per_cpu(in_nmi_frame, smp_processor_id()) =
  1785. x86_is_stack_id(NMI_STACK, name);
  1786. return 0;
  1787. }
  1788. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1789. {
  1790. struct perf_callchain_entry *entry = data;
  1791. if (per_cpu(in_nmi_frame, smp_processor_id()))
  1792. return;
  1793. if (reliable)
  1794. callchain_store(entry, addr);
  1795. }
  1796. static const struct stacktrace_ops backtrace_ops = {
  1797. .warning = backtrace_warning,
  1798. .warning_symbol = backtrace_warning_symbol,
  1799. .stack = backtrace_stack,
  1800. .address = backtrace_address,
  1801. };
  1802. #include "../dumpstack.h"
  1803. static void
  1804. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1805. {
  1806. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1807. callchain_store(entry, regs->ip);
  1808. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1809. }
  1810. /*
  1811. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  1812. */
  1813. static unsigned long
  1814. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  1815. {
  1816. unsigned long offset, addr = (unsigned long)from;
  1817. int type = in_nmi() ? KM_NMI : KM_IRQ0;
  1818. unsigned long size, len = 0;
  1819. struct page *page;
  1820. void *map;
  1821. int ret;
  1822. do {
  1823. ret = __get_user_pages_fast(addr, 1, 0, &page);
  1824. if (!ret)
  1825. break;
  1826. offset = addr & (PAGE_SIZE - 1);
  1827. size = min(PAGE_SIZE - offset, n - len);
  1828. map = kmap_atomic(page, type);
  1829. memcpy(to, map+offset, size);
  1830. kunmap_atomic(map, type);
  1831. put_page(page);
  1832. len += size;
  1833. to += size;
  1834. addr += size;
  1835. } while (len < n);
  1836. return len;
  1837. }
  1838. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  1839. {
  1840. unsigned long bytes;
  1841. bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
  1842. return bytes == sizeof(*frame);
  1843. }
  1844. static void
  1845. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1846. {
  1847. struct stack_frame frame;
  1848. const void __user *fp;
  1849. if (!user_mode(regs))
  1850. regs = task_pt_regs(current);
  1851. fp = (void __user *)regs->bp;
  1852. callchain_store(entry, PERF_CONTEXT_USER);
  1853. callchain_store(entry, regs->ip);
  1854. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1855. frame.next_frame = NULL;
  1856. frame.return_address = 0;
  1857. if (!copy_stack_frame(fp, &frame))
  1858. break;
  1859. if ((unsigned long)fp < regs->sp)
  1860. break;
  1861. callchain_store(entry, frame.return_address);
  1862. fp = frame.next_frame;
  1863. }
  1864. }
  1865. static void
  1866. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1867. {
  1868. int is_user;
  1869. if (!regs)
  1870. return;
  1871. is_user = user_mode(regs);
  1872. if (!current || current->pid == 0)
  1873. return;
  1874. if (is_user && current->state != TASK_RUNNING)
  1875. return;
  1876. if (!is_user)
  1877. perf_callchain_kernel(regs, entry);
  1878. if (current->mm)
  1879. perf_callchain_user(regs, entry);
  1880. }
  1881. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1882. {
  1883. struct perf_callchain_entry *entry;
  1884. if (in_nmi())
  1885. entry = &__get_cpu_var(nmi_entry);
  1886. else
  1887. entry = &__get_cpu_var(irq_entry);
  1888. entry->nr = 0;
  1889. perf_do_callchain(regs, entry);
  1890. return entry;
  1891. }
  1892. void hw_perf_counter_setup_online(int cpu)
  1893. {
  1894. init_debug_store_on_cpu(cpu);
  1895. }