perf_event.c 69 KB

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