perf_event.c 64 KB

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