perf_event.c 68 KB

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