perf_event.c 67 KB

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