perf_counter.c 40 KB

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