perf_event.c 65 KB

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