perf_event.c 52 KB

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