perf_event.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  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. if (hwc->idx != -1)
  1510. x86_perf_event_set_period(event, hwc, hwc->idx);
  1511. return 0;
  1512. }
  1513. static void x86_pmu_unthrottle(struct perf_event *event)
  1514. {
  1515. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1516. struct hw_perf_event *hwc = &event->hw;
  1517. if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
  1518. cpuc->events[hwc->idx] != event))
  1519. return;
  1520. x86_pmu.enable(hwc, hwc->idx);
  1521. }
  1522. void perf_event_print_debug(void)
  1523. {
  1524. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1525. struct cpu_hw_events *cpuc;
  1526. unsigned long flags;
  1527. int cpu, idx;
  1528. if (!x86_pmu.num_events)
  1529. return;
  1530. local_irq_save(flags);
  1531. cpu = smp_processor_id();
  1532. cpuc = &per_cpu(cpu_hw_events, cpu);
  1533. if (x86_pmu.version >= 2) {
  1534. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1535. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1536. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1537. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1538. pr_info("\n");
  1539. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1540. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1541. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1542. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1543. }
  1544. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  1545. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1546. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  1547. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  1548. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  1549. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1550. cpu, idx, pmc_ctrl);
  1551. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1552. cpu, idx, pmc_count);
  1553. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1554. cpu, idx, prev_left);
  1555. }
  1556. for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
  1557. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1558. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1559. cpu, idx, pmc_count);
  1560. }
  1561. local_irq_restore(flags);
  1562. }
  1563. static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc)
  1564. {
  1565. struct debug_store *ds = cpuc->ds;
  1566. struct bts_record {
  1567. u64 from;
  1568. u64 to;
  1569. u64 flags;
  1570. };
  1571. struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
  1572. struct bts_record *at, *top;
  1573. struct perf_output_handle handle;
  1574. struct perf_event_header header;
  1575. struct perf_sample_data data;
  1576. struct pt_regs regs;
  1577. if (!event)
  1578. return;
  1579. if (!ds)
  1580. return;
  1581. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  1582. top = (struct bts_record *)(unsigned long)ds->bts_index;
  1583. if (top <= at)
  1584. return;
  1585. ds->bts_index = ds->bts_buffer_base;
  1586. data.period = event->hw.last_period;
  1587. data.addr = 0;
  1588. data.raw = NULL;
  1589. regs.ip = 0;
  1590. /*
  1591. * Prepare a generic sample, i.e. fill in the invariant fields.
  1592. * We will overwrite the from and to address before we output
  1593. * the sample.
  1594. */
  1595. perf_prepare_sample(&header, &data, event, &regs);
  1596. if (perf_output_begin(&handle, event,
  1597. header.size * (top - at), 1, 1))
  1598. return;
  1599. for (; at < top; at++) {
  1600. data.ip = at->from;
  1601. data.addr = at->to;
  1602. perf_output_sample(&handle, &header, &data, event);
  1603. }
  1604. perf_output_end(&handle);
  1605. /* There's new data available. */
  1606. event->hw.interrupts++;
  1607. event->pending_kill = POLL_IN;
  1608. }
  1609. static void __x86_pmu_disable(struct perf_event *event, struct cpu_hw_events *cpuc)
  1610. {
  1611. struct hw_perf_event *hwc = &event->hw;
  1612. int idx = hwc->idx;
  1613. /*
  1614. * Must be done before we disable, otherwise the nmi handler
  1615. * could reenable again:
  1616. */
  1617. clear_bit(idx, cpuc->active_mask);
  1618. x86_pmu.disable(hwc, idx);
  1619. /*
  1620. * Drain the remaining delta count out of a event
  1621. * that we are disabling:
  1622. */
  1623. x86_perf_event_update(event, hwc, idx);
  1624. /* Drain the remaining BTS records. */
  1625. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS))
  1626. intel_pmu_drain_bts_buffer(cpuc);
  1627. cpuc->events[idx] = NULL;
  1628. }
  1629. static void x86_pmu_disable(struct perf_event *event)
  1630. {
  1631. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1632. int i;
  1633. __x86_pmu_disable(event, cpuc);
  1634. for (i = 0; i < cpuc->n_events; i++) {
  1635. if (event == cpuc->event_list[i]) {
  1636. if (x86_pmu.put_event_constraints)
  1637. x86_pmu.put_event_constraints(cpuc, event);
  1638. while (++i < cpuc->n_events)
  1639. cpuc->event_list[i-1] = cpuc->event_list[i];
  1640. --cpuc->n_events;
  1641. break;
  1642. }
  1643. }
  1644. perf_event_update_userpage(event);
  1645. }
  1646. /*
  1647. * Save and restart an expired event. Called by NMI contexts,
  1648. * so it has to be careful about preempting normal event ops:
  1649. */
  1650. static int intel_pmu_save_and_restart(struct perf_event *event)
  1651. {
  1652. struct hw_perf_event *hwc = &event->hw;
  1653. int idx = hwc->idx;
  1654. int ret;
  1655. x86_perf_event_update(event, hwc, idx);
  1656. ret = x86_perf_event_set_period(event, hwc, idx);
  1657. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1658. intel_pmu_enable_event(hwc, idx);
  1659. return ret;
  1660. }
  1661. static void intel_pmu_reset(void)
  1662. {
  1663. struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds;
  1664. unsigned long flags;
  1665. int idx;
  1666. if (!x86_pmu.num_events)
  1667. return;
  1668. local_irq_save(flags);
  1669. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  1670. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1671. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  1672. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  1673. }
  1674. for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
  1675. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1676. }
  1677. if (ds)
  1678. ds->bts_index = ds->bts_buffer_base;
  1679. local_irq_restore(flags);
  1680. }
  1681. static int p6_pmu_handle_irq(struct pt_regs *regs)
  1682. {
  1683. struct perf_sample_data data;
  1684. struct cpu_hw_events *cpuc;
  1685. struct perf_event *event;
  1686. struct hw_perf_event *hwc;
  1687. int idx, handled = 0;
  1688. u64 val;
  1689. data.addr = 0;
  1690. data.raw = NULL;
  1691. cpuc = &__get_cpu_var(cpu_hw_events);
  1692. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1693. if (!test_bit(idx, cpuc->active_mask))
  1694. continue;
  1695. event = cpuc->events[idx];
  1696. hwc = &event->hw;
  1697. val = x86_perf_event_update(event, hwc, idx);
  1698. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  1699. continue;
  1700. /*
  1701. * event overflow
  1702. */
  1703. handled = 1;
  1704. data.period = event->hw.last_period;
  1705. if (!x86_perf_event_set_period(event, hwc, idx))
  1706. continue;
  1707. if (perf_event_overflow(event, 1, &data, regs))
  1708. p6_pmu_disable_event(hwc, idx);
  1709. }
  1710. if (handled)
  1711. inc_irq_stat(apic_perf_irqs);
  1712. return handled;
  1713. }
  1714. /*
  1715. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1716. * rules apply:
  1717. */
  1718. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1719. {
  1720. struct perf_sample_data data;
  1721. struct cpu_hw_events *cpuc;
  1722. int bit, loops;
  1723. u64 ack, status;
  1724. data.addr = 0;
  1725. data.raw = NULL;
  1726. cpuc = &__get_cpu_var(cpu_hw_events);
  1727. perf_disable();
  1728. intel_pmu_drain_bts_buffer(cpuc);
  1729. status = intel_pmu_get_status();
  1730. if (!status) {
  1731. perf_enable();
  1732. return 0;
  1733. }
  1734. loops = 0;
  1735. again:
  1736. if (++loops > 100) {
  1737. WARN_ONCE(1, "perfevents: irq loop stuck!\n");
  1738. perf_event_print_debug();
  1739. intel_pmu_reset();
  1740. perf_enable();
  1741. return 1;
  1742. }
  1743. inc_irq_stat(apic_perf_irqs);
  1744. ack = status;
  1745. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1746. struct perf_event *event = cpuc->events[bit];
  1747. clear_bit(bit, (unsigned long *) &status);
  1748. if (!test_bit(bit, cpuc->active_mask))
  1749. continue;
  1750. if (!intel_pmu_save_and_restart(event))
  1751. continue;
  1752. data.period = event->hw.last_period;
  1753. if (perf_event_overflow(event, 1, &data, regs))
  1754. intel_pmu_disable_event(&event->hw, bit);
  1755. }
  1756. intel_pmu_ack_status(ack);
  1757. /*
  1758. * Repeat if there is more work to be done:
  1759. */
  1760. status = intel_pmu_get_status();
  1761. if (status)
  1762. goto again;
  1763. perf_enable();
  1764. return 1;
  1765. }
  1766. static int amd_pmu_handle_irq(struct pt_regs *regs)
  1767. {
  1768. struct perf_sample_data data;
  1769. struct cpu_hw_events *cpuc;
  1770. struct perf_event *event;
  1771. struct hw_perf_event *hwc;
  1772. int idx, handled = 0;
  1773. u64 val;
  1774. data.addr = 0;
  1775. data.raw = NULL;
  1776. cpuc = &__get_cpu_var(cpu_hw_events);
  1777. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1778. if (!test_bit(idx, cpuc->active_mask))
  1779. continue;
  1780. event = cpuc->events[idx];
  1781. hwc = &event->hw;
  1782. val = x86_perf_event_update(event, hwc, idx);
  1783. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  1784. continue;
  1785. /*
  1786. * event overflow
  1787. */
  1788. handled = 1;
  1789. data.period = event->hw.last_period;
  1790. if (!x86_perf_event_set_period(event, hwc, idx))
  1791. continue;
  1792. if (perf_event_overflow(event, 1, &data, regs))
  1793. amd_pmu_disable_event(hwc, idx);
  1794. }
  1795. if (handled)
  1796. inc_irq_stat(apic_perf_irqs);
  1797. return handled;
  1798. }
  1799. void smp_perf_pending_interrupt(struct pt_regs *regs)
  1800. {
  1801. irq_enter();
  1802. ack_APIC_irq();
  1803. inc_irq_stat(apic_pending_irqs);
  1804. perf_event_do_pending();
  1805. irq_exit();
  1806. }
  1807. void set_perf_event_pending(void)
  1808. {
  1809. #ifdef CONFIG_X86_LOCAL_APIC
  1810. if (!x86_pmu.apic || !x86_pmu_initialized())
  1811. return;
  1812. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  1813. #endif
  1814. }
  1815. void perf_events_lapic_init(void)
  1816. {
  1817. #ifdef CONFIG_X86_LOCAL_APIC
  1818. if (!x86_pmu.apic || !x86_pmu_initialized())
  1819. return;
  1820. /*
  1821. * Always use NMI for PMU
  1822. */
  1823. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1824. #endif
  1825. }
  1826. static int __kprobes
  1827. perf_event_nmi_handler(struct notifier_block *self,
  1828. unsigned long cmd, void *__args)
  1829. {
  1830. struct die_args *args = __args;
  1831. struct pt_regs *regs;
  1832. if (!atomic_read(&active_events))
  1833. return NOTIFY_DONE;
  1834. switch (cmd) {
  1835. case DIE_NMI:
  1836. case DIE_NMI_IPI:
  1837. break;
  1838. default:
  1839. return NOTIFY_DONE;
  1840. }
  1841. regs = args->regs;
  1842. #ifdef CONFIG_X86_LOCAL_APIC
  1843. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1844. #endif
  1845. /*
  1846. * Can't rely on the handled return value to say it was our NMI, two
  1847. * events could trigger 'simultaneously' raising two back-to-back NMIs.
  1848. *
  1849. * If the first NMI handles both, the latter will be empty and daze
  1850. * the CPU.
  1851. */
  1852. x86_pmu.handle_irq(regs);
  1853. return NOTIFY_STOP;
  1854. }
  1855. static struct event_constraint unconstrained;
  1856. static struct event_constraint bts_constraint =
  1857. EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0);
  1858. static struct event_constraint *
  1859. intel_special_constraints(struct perf_event *event)
  1860. {
  1861. unsigned int hw_event;
  1862. hw_event = event->hw.config & INTEL_ARCH_EVENT_MASK;
  1863. if (unlikely((hw_event ==
  1864. x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) &&
  1865. (event->hw.sample_period == 1))) {
  1866. return &bts_constraint;
  1867. }
  1868. return NULL;
  1869. }
  1870. static struct event_constraint *
  1871. intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1872. {
  1873. struct event_constraint *c;
  1874. c = intel_special_constraints(event);
  1875. if (c)
  1876. return c;
  1877. if (x86_pmu.event_constraints) {
  1878. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1879. if ((event->hw.config & c->cmask) == c->code)
  1880. return c;
  1881. }
  1882. }
  1883. return &unconstrained;
  1884. }
  1885. static struct event_constraint *
  1886. amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1887. {
  1888. return &unconstrained;
  1889. }
  1890. static int x86_event_sched_in(struct perf_event *event,
  1891. struct perf_cpu_context *cpuctx, int cpu)
  1892. {
  1893. int ret = 0;
  1894. event->state = PERF_EVENT_STATE_ACTIVE;
  1895. event->oncpu = cpu;
  1896. event->tstamp_running += event->ctx->time - event->tstamp_stopped;
  1897. if (!is_x86_event(event))
  1898. ret = event->pmu->enable(event);
  1899. if (!ret && !is_software_event(event))
  1900. cpuctx->active_oncpu++;
  1901. if (!ret && event->attr.exclusive)
  1902. cpuctx->exclusive = 1;
  1903. return ret;
  1904. }
  1905. static void x86_event_sched_out(struct perf_event *event,
  1906. struct perf_cpu_context *cpuctx, int cpu)
  1907. {
  1908. event->state = PERF_EVENT_STATE_INACTIVE;
  1909. event->oncpu = -1;
  1910. if (!is_x86_event(event))
  1911. event->pmu->disable(event);
  1912. event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
  1913. if (!is_software_event(event))
  1914. cpuctx->active_oncpu--;
  1915. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1916. cpuctx->exclusive = 0;
  1917. }
  1918. /*
  1919. * Called to enable a whole group of events.
  1920. * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
  1921. * Assumes the caller has disabled interrupts and has
  1922. * frozen the PMU with hw_perf_save_disable.
  1923. *
  1924. * called with PMU disabled. If successful and return value 1,
  1925. * then guaranteed to call perf_enable() and hw_perf_enable()
  1926. */
  1927. int hw_perf_group_sched_in(struct perf_event *leader,
  1928. struct perf_cpu_context *cpuctx,
  1929. struct perf_event_context *ctx, int cpu)
  1930. {
  1931. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1932. struct perf_event *sub;
  1933. int assign[X86_PMC_IDX_MAX];
  1934. int n0, n1, ret;
  1935. /* n0 = total number of events */
  1936. n0 = collect_events(cpuc, leader, true);
  1937. if (n0 < 0)
  1938. return n0;
  1939. ret = x86_schedule_events(cpuc, n0, assign);
  1940. if (ret)
  1941. return ret;
  1942. ret = x86_event_sched_in(leader, cpuctx, cpu);
  1943. if (ret)
  1944. return ret;
  1945. n1 = 1;
  1946. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1947. if (sub->state > PERF_EVENT_STATE_OFF) {
  1948. ret = x86_event_sched_in(sub, cpuctx, cpu);
  1949. if (ret)
  1950. goto undo;
  1951. ++n1;
  1952. }
  1953. }
  1954. /*
  1955. * copy new assignment, now we know it is possible
  1956. * will be used by hw_perf_enable()
  1957. */
  1958. memcpy(cpuc->assign, assign, n0*sizeof(int));
  1959. cpuc->n_events = n0;
  1960. cpuc->n_added = n1;
  1961. ctx->nr_active += n1;
  1962. /*
  1963. * 1 means successful and events are active
  1964. * This is not quite true because we defer
  1965. * actual activation until hw_perf_enable() but
  1966. * this way we* ensure caller won't try to enable
  1967. * individual events
  1968. */
  1969. return 1;
  1970. undo:
  1971. x86_event_sched_out(leader, cpuctx, cpu);
  1972. n0 = 1;
  1973. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1974. if (sub->state == PERF_EVENT_STATE_ACTIVE) {
  1975. x86_event_sched_out(sub, cpuctx, cpu);
  1976. if (++n0 == n1)
  1977. break;
  1978. }
  1979. }
  1980. return ret;
  1981. }
  1982. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1983. .notifier_call = perf_event_nmi_handler,
  1984. .next = NULL,
  1985. .priority = 1
  1986. };
  1987. static __initconst struct x86_pmu p6_pmu = {
  1988. .name = "p6",
  1989. .handle_irq = p6_pmu_handle_irq,
  1990. .disable_all = p6_pmu_disable_all,
  1991. .enable_all = p6_pmu_enable_all,
  1992. .enable = p6_pmu_enable_event,
  1993. .disable = p6_pmu_disable_event,
  1994. .eventsel = MSR_P6_EVNTSEL0,
  1995. .perfctr = MSR_P6_PERFCTR0,
  1996. .event_map = p6_pmu_event_map,
  1997. .raw_event = p6_pmu_raw_event,
  1998. .max_events = ARRAY_SIZE(p6_perfmon_event_map),
  1999. .apic = 1,
  2000. .max_period = (1ULL << 31) - 1,
  2001. .version = 0,
  2002. .num_events = 2,
  2003. /*
  2004. * Events have 40 bits implemented. However they are designed such
  2005. * that bits [32-39] are sign extensions of bit 31. As such the
  2006. * effective width of a event for P6-like PMU is 32 bits only.
  2007. *
  2008. * See IA-32 Intel Architecture Software developer manual Vol 3B
  2009. */
  2010. .event_bits = 32,
  2011. .event_mask = (1ULL << 32) - 1,
  2012. .get_event_constraints = intel_get_event_constraints,
  2013. .event_constraints = intel_p6_event_constraints
  2014. };
  2015. static __initconst struct x86_pmu intel_pmu = {
  2016. .name = "Intel",
  2017. .handle_irq = intel_pmu_handle_irq,
  2018. .disable_all = intel_pmu_disable_all,
  2019. .enable_all = intel_pmu_enable_all,
  2020. .enable = intel_pmu_enable_event,
  2021. .disable = intel_pmu_disable_event,
  2022. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  2023. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  2024. .event_map = intel_pmu_event_map,
  2025. .raw_event = intel_pmu_raw_event,
  2026. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  2027. .apic = 1,
  2028. /*
  2029. * Intel PMCs cannot be accessed sanely above 32 bit width,
  2030. * so we install an artificial 1<<31 period regardless of
  2031. * the generic event period:
  2032. */
  2033. .max_period = (1ULL << 31) - 1,
  2034. .enable_bts = intel_pmu_enable_bts,
  2035. .disable_bts = intel_pmu_disable_bts,
  2036. .get_event_constraints = intel_get_event_constraints
  2037. };
  2038. static __initconst struct x86_pmu amd_pmu = {
  2039. .name = "AMD",
  2040. .handle_irq = amd_pmu_handle_irq,
  2041. .disable_all = amd_pmu_disable_all,
  2042. .enable_all = amd_pmu_enable_all,
  2043. .enable = amd_pmu_enable_event,
  2044. .disable = amd_pmu_disable_event,
  2045. .eventsel = MSR_K7_EVNTSEL0,
  2046. .perfctr = MSR_K7_PERFCTR0,
  2047. .event_map = amd_pmu_event_map,
  2048. .raw_event = amd_pmu_raw_event,
  2049. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  2050. .num_events = 4,
  2051. .event_bits = 48,
  2052. .event_mask = (1ULL << 48) - 1,
  2053. .apic = 1,
  2054. /* use highest bit to detect overflow */
  2055. .max_period = (1ULL << 47) - 1,
  2056. .get_event_constraints = amd_get_event_constraints
  2057. };
  2058. static __init int p6_pmu_init(void)
  2059. {
  2060. switch (boot_cpu_data.x86_model) {
  2061. case 1:
  2062. case 3: /* Pentium Pro */
  2063. case 5:
  2064. case 6: /* Pentium II */
  2065. case 7:
  2066. case 8:
  2067. case 11: /* Pentium III */
  2068. case 9:
  2069. case 13:
  2070. /* Pentium M */
  2071. break;
  2072. default:
  2073. pr_cont("unsupported p6 CPU model %d ",
  2074. boot_cpu_data.x86_model);
  2075. return -ENODEV;
  2076. }
  2077. x86_pmu = p6_pmu;
  2078. return 0;
  2079. }
  2080. static __init int intel_pmu_init(void)
  2081. {
  2082. union cpuid10_edx edx;
  2083. union cpuid10_eax eax;
  2084. unsigned int unused;
  2085. unsigned int ebx;
  2086. int version;
  2087. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  2088. /* check for P6 processor family */
  2089. if (boot_cpu_data.x86 == 6) {
  2090. return p6_pmu_init();
  2091. } else {
  2092. return -ENODEV;
  2093. }
  2094. }
  2095. /*
  2096. * Check whether the Architectural PerfMon supports
  2097. * Branch Misses Retired hw_event or not.
  2098. */
  2099. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  2100. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  2101. return -ENODEV;
  2102. version = eax.split.version_id;
  2103. if (version < 2)
  2104. return -ENODEV;
  2105. x86_pmu = intel_pmu;
  2106. x86_pmu.version = version;
  2107. x86_pmu.num_events = eax.split.num_events;
  2108. x86_pmu.event_bits = eax.split.bit_width;
  2109. x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1;
  2110. /*
  2111. * Quirk: v2 perfmon does not report fixed-purpose events, so
  2112. * assume at least 3 events:
  2113. */
  2114. x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3);
  2115. /*
  2116. * Install the hw-cache-events table:
  2117. */
  2118. switch (boot_cpu_data.x86_model) {
  2119. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  2120. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  2121. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  2122. case 29: /* six-core 45 nm xeon "Dunnington" */
  2123. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  2124. sizeof(hw_cache_event_ids));
  2125. x86_pmu.event_constraints = intel_core_event_constraints;
  2126. pr_cont("Core2 events, ");
  2127. break;
  2128. case 26: /* 45 nm nehalem, "Bloomfield" */
  2129. case 30: /* 45 nm nehalem, "Lynnfield" */
  2130. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  2131. sizeof(hw_cache_event_ids));
  2132. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  2133. pr_cont("Nehalem/Corei7 events, ");
  2134. break;
  2135. case 28:
  2136. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  2137. sizeof(hw_cache_event_ids));
  2138. x86_pmu.event_constraints = intel_gen_event_constraints;
  2139. pr_cont("Atom events, ");
  2140. break;
  2141. case 37: /* 32 nm nehalem, "Clarkdale" */
  2142. case 44: /* 32 nm nehalem, "Gulftown" */
  2143. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  2144. sizeof(hw_cache_event_ids));
  2145. x86_pmu.event_constraints = intel_westmere_event_constraints;
  2146. pr_cont("Westmere events, ");
  2147. break;
  2148. default:
  2149. /*
  2150. * default constraints for v2 and up
  2151. */
  2152. x86_pmu.event_constraints = intel_gen_event_constraints;
  2153. pr_cont("generic architected perfmon, ");
  2154. }
  2155. return 0;
  2156. }
  2157. static __init int amd_pmu_init(void)
  2158. {
  2159. /* Performance-monitoring supported from K7 and later: */
  2160. if (boot_cpu_data.x86 < 6)
  2161. return -ENODEV;
  2162. x86_pmu = amd_pmu;
  2163. /* Events are common for all AMDs */
  2164. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  2165. sizeof(hw_cache_event_ids));
  2166. return 0;
  2167. }
  2168. static void __init pmu_check_apic(void)
  2169. {
  2170. if (cpu_has_apic)
  2171. return;
  2172. x86_pmu.apic = 0;
  2173. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  2174. pr_info("no hardware sampling interrupt available.\n");
  2175. }
  2176. void __init init_hw_perf_events(void)
  2177. {
  2178. int err;
  2179. pr_info("Performance Events: ");
  2180. switch (boot_cpu_data.x86_vendor) {
  2181. case X86_VENDOR_INTEL:
  2182. err = intel_pmu_init();
  2183. break;
  2184. case X86_VENDOR_AMD:
  2185. err = amd_pmu_init();
  2186. break;
  2187. default:
  2188. return;
  2189. }
  2190. if (err != 0) {
  2191. pr_cont("no PMU driver, software events only.\n");
  2192. return;
  2193. }
  2194. pmu_check_apic();
  2195. pr_cont("%s PMU driver.\n", x86_pmu.name);
  2196. if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
  2197. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  2198. x86_pmu.num_events, X86_PMC_MAX_GENERIC);
  2199. x86_pmu.num_events = X86_PMC_MAX_GENERIC;
  2200. }
  2201. perf_event_mask = (1 << x86_pmu.num_events) - 1;
  2202. perf_max_events = x86_pmu.num_events;
  2203. if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
  2204. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  2205. x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
  2206. x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
  2207. }
  2208. perf_event_mask |=
  2209. ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
  2210. x86_pmu.intel_ctrl = perf_event_mask;
  2211. perf_events_lapic_init();
  2212. register_die_notifier(&perf_event_nmi_notifier);
  2213. unconstrained = (struct event_constraint)
  2214. EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1, 0);
  2215. pr_info("... version: %d\n", x86_pmu.version);
  2216. pr_info("... bit width: %d\n", x86_pmu.event_bits);
  2217. pr_info("... generic registers: %d\n", x86_pmu.num_events);
  2218. pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
  2219. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  2220. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
  2221. pr_info("... event mask: %016Lx\n", perf_event_mask);
  2222. }
  2223. static inline void x86_pmu_read(struct perf_event *event)
  2224. {
  2225. x86_perf_event_update(event, &event->hw, event->hw.idx);
  2226. }
  2227. static const struct pmu pmu = {
  2228. .enable = x86_pmu_enable,
  2229. .disable = x86_pmu_disable,
  2230. .read = x86_pmu_read,
  2231. .unthrottle = x86_pmu_unthrottle,
  2232. };
  2233. /*
  2234. * validate a single event group
  2235. *
  2236. * validation include:
  2237. * - check events are compatible which each other
  2238. * - events do not compete for the same counter
  2239. * - number of events <= number of counters
  2240. *
  2241. * validation ensures the group can be loaded onto the
  2242. * PMU if it was the only group available.
  2243. */
  2244. static int validate_group(struct perf_event *event)
  2245. {
  2246. struct perf_event *leader = event->group_leader;
  2247. struct cpu_hw_events *fake_cpuc;
  2248. int ret, n;
  2249. ret = -ENOMEM;
  2250. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  2251. if (!fake_cpuc)
  2252. goto out;
  2253. /*
  2254. * the event is not yet connected with its
  2255. * siblings therefore we must first collect
  2256. * existing siblings, then add the new event
  2257. * before we can simulate the scheduling
  2258. */
  2259. ret = -ENOSPC;
  2260. n = collect_events(fake_cpuc, leader, true);
  2261. if (n < 0)
  2262. goto out_free;
  2263. fake_cpuc->n_events = n;
  2264. n = collect_events(fake_cpuc, event, false);
  2265. if (n < 0)
  2266. goto out_free;
  2267. fake_cpuc->n_events = n;
  2268. ret = x86_schedule_events(fake_cpuc, n, NULL);
  2269. out_free:
  2270. kfree(fake_cpuc);
  2271. out:
  2272. return ret;
  2273. }
  2274. const struct pmu *hw_perf_event_init(struct perf_event *event)
  2275. {
  2276. const struct pmu *tmp;
  2277. int err;
  2278. err = __hw_perf_event_init(event);
  2279. if (!err) {
  2280. /*
  2281. * we temporarily connect event to its pmu
  2282. * such that validate_group() can classify
  2283. * it as an x86 event using is_x86_event()
  2284. */
  2285. tmp = event->pmu;
  2286. event->pmu = &pmu;
  2287. if (event->group_leader != event)
  2288. err = validate_group(event);
  2289. event->pmu = tmp;
  2290. }
  2291. if (err) {
  2292. if (event->destroy)
  2293. event->destroy(event);
  2294. return ERR_PTR(err);
  2295. }
  2296. return &pmu;
  2297. }
  2298. /*
  2299. * callchain support
  2300. */
  2301. static inline
  2302. void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  2303. {
  2304. if (entry->nr < PERF_MAX_STACK_DEPTH)
  2305. entry->ip[entry->nr++] = ip;
  2306. }
  2307. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
  2308. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
  2309. static void
  2310. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  2311. {
  2312. /* Ignore warnings */
  2313. }
  2314. static void backtrace_warning(void *data, char *msg)
  2315. {
  2316. /* Ignore warnings */
  2317. }
  2318. static int backtrace_stack(void *data, char *name)
  2319. {
  2320. return 0;
  2321. }
  2322. static void backtrace_address(void *data, unsigned long addr, int reliable)
  2323. {
  2324. struct perf_callchain_entry *entry = data;
  2325. if (reliable)
  2326. callchain_store(entry, addr);
  2327. }
  2328. static const struct stacktrace_ops backtrace_ops = {
  2329. .warning = backtrace_warning,
  2330. .warning_symbol = backtrace_warning_symbol,
  2331. .stack = backtrace_stack,
  2332. .address = backtrace_address,
  2333. .walk_stack = print_context_stack_bp,
  2334. };
  2335. #include "../dumpstack.h"
  2336. static void
  2337. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  2338. {
  2339. callchain_store(entry, PERF_CONTEXT_KERNEL);
  2340. callchain_store(entry, regs->ip);
  2341. dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
  2342. }
  2343. /*
  2344. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  2345. */
  2346. static unsigned long
  2347. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  2348. {
  2349. unsigned long offset, addr = (unsigned long)from;
  2350. int type = in_nmi() ? KM_NMI : KM_IRQ0;
  2351. unsigned long size, len = 0;
  2352. struct page *page;
  2353. void *map;
  2354. int ret;
  2355. do {
  2356. ret = __get_user_pages_fast(addr, 1, 0, &page);
  2357. if (!ret)
  2358. break;
  2359. offset = addr & (PAGE_SIZE - 1);
  2360. size = min(PAGE_SIZE - offset, n - len);
  2361. map = kmap_atomic(page, type);
  2362. memcpy(to, map+offset, size);
  2363. kunmap_atomic(map, type);
  2364. put_page(page);
  2365. len += size;
  2366. to += size;
  2367. addr += size;
  2368. } while (len < n);
  2369. return len;
  2370. }
  2371. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  2372. {
  2373. unsigned long bytes;
  2374. bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
  2375. return bytes == sizeof(*frame);
  2376. }
  2377. static void
  2378. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  2379. {
  2380. struct stack_frame frame;
  2381. const void __user *fp;
  2382. if (!user_mode(regs))
  2383. regs = task_pt_regs(current);
  2384. fp = (void __user *)regs->bp;
  2385. callchain_store(entry, PERF_CONTEXT_USER);
  2386. callchain_store(entry, regs->ip);
  2387. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  2388. frame.next_frame = NULL;
  2389. frame.return_address = 0;
  2390. if (!copy_stack_frame(fp, &frame))
  2391. break;
  2392. if ((unsigned long)fp < regs->sp)
  2393. break;
  2394. callchain_store(entry, frame.return_address);
  2395. fp = frame.next_frame;
  2396. }
  2397. }
  2398. static void
  2399. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  2400. {
  2401. int is_user;
  2402. if (!regs)
  2403. return;
  2404. is_user = user_mode(regs);
  2405. if (is_user && current->state != TASK_RUNNING)
  2406. return;
  2407. if (!is_user)
  2408. perf_callchain_kernel(regs, entry);
  2409. if (current->mm)
  2410. perf_callchain_user(regs, entry);
  2411. }
  2412. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2413. {
  2414. struct perf_callchain_entry *entry;
  2415. if (in_nmi())
  2416. entry = &__get_cpu_var(pmc_nmi_entry);
  2417. else
  2418. entry = &__get_cpu_var(pmc_irq_entry);
  2419. entry->nr = 0;
  2420. perf_do_callchain(regs, entry);
  2421. return entry;
  2422. }
  2423. void hw_perf_event_setup_online(int cpu)
  2424. {
  2425. init_debug_store_on_cpu(cpu);
  2426. }