perf_counter.c 40 KB

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