perf_event.c 75 KB

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