perf_event.c 65 KB

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