perf_event_intel.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. /*
  2. * Per core/cpu state
  3. *
  4. * Used to coordinate shared registers between HT threads or
  5. * among events on a single PMU.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/stddef.h>
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/export.h>
  13. #include <asm/cpufeature.h>
  14. #include <asm/hardirq.h>
  15. #include <asm/apic.h>
  16. #include "perf_event.h"
  17. /*
  18. * Intel PerfMon, used on Core and later.
  19. */
  20. static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
  21. {
  22. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  23. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  24. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  25. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  26. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  27. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  28. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  29. [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
  30. };
  31. static struct event_constraint intel_core_event_constraints[] __read_mostly =
  32. {
  33. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  34. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  35. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  36. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  37. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  38. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
  39. EVENT_CONSTRAINT_END
  40. };
  41. static struct event_constraint intel_core2_event_constraints[] __read_mostly =
  42. {
  43. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  44. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  45. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  46. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  47. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  48. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  49. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  50. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  51. INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  52. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  53. INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  54. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
  55. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  56. EVENT_CONSTRAINT_END
  57. };
  58. static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
  59. {
  60. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  61. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  62. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  63. INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  64. INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  65. INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  66. INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  67. INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
  68. INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  69. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  70. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  71. EVENT_CONSTRAINT_END
  72. };
  73. static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
  74. {
  75. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  76. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
  77. EVENT_EXTRA_END
  78. };
  79. static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
  80. {
  81. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  82. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  83. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  84. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  85. INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
  86. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  87. INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
  88. EVENT_CONSTRAINT_END
  89. };
  90. static struct event_constraint intel_snb_event_constraints[] __read_mostly =
  91. {
  92. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  93. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  94. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  95. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  96. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  97. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  98. INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  99. INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
  100. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  101. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  102. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  103. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  104. EVENT_CONSTRAINT_END
  105. };
  106. static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
  107. {
  108. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  109. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  110. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  111. INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
  112. INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMTPY */
  113. INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
  114. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  115. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  116. INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
  117. INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  118. INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  119. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  120. /*
  121. * Errata BV98 -- MEM_*_RETIRED events can leak between counters of SMT
  122. * siblings; disable these events because they can corrupt unrelated
  123. * counters.
  124. */
  125. INTEL_EVENT_CONSTRAINT(0xd0, 0x0), /* MEM_UOPS_RETIRED.* */
  126. INTEL_EVENT_CONSTRAINT(0xd1, 0x0), /* MEM_LOAD_UOPS_RETIRED.* */
  127. INTEL_EVENT_CONSTRAINT(0xd2, 0x0), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  128. INTEL_EVENT_CONSTRAINT(0xd3, 0x0), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  129. EVENT_CONSTRAINT_END
  130. };
  131. static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
  132. {
  133. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  134. INTEL_EVENT_EXTRA_REG(0xbb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
  135. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
  136. EVENT_EXTRA_END
  137. };
  138. static struct event_constraint intel_v1_event_constraints[] __read_mostly =
  139. {
  140. EVENT_CONSTRAINT_END
  141. };
  142. static struct event_constraint intel_gen_event_constraints[] __read_mostly =
  143. {
  144. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  145. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  146. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  147. EVENT_CONSTRAINT_END
  148. };
  149. static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
  150. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0),
  151. INTEL_EVENT_EXTRA_REG(0xbb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1),
  152. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
  153. EVENT_EXTRA_END
  154. };
  155. static struct extra_reg intel_snbep_extra_regs[] __read_mostly = {
  156. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
  157. INTEL_EVENT_EXTRA_REG(0xbb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
  158. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
  159. EVENT_EXTRA_END
  160. };
  161. EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3");
  162. EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3");
  163. EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2");
  164. struct attribute *nhm_events_attrs[] = {
  165. EVENT_PTR(mem_ld_nhm),
  166. NULL,
  167. };
  168. struct attribute *snb_events_attrs[] = {
  169. EVENT_PTR(mem_ld_snb),
  170. EVENT_PTR(mem_st_snb),
  171. NULL,
  172. };
  173. static struct event_constraint intel_hsw_event_constraints[] = {
  174. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  175. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  176. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  177. INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.* */
  178. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  179. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  180. /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  181. INTEL_EVENT_CONSTRAINT(0x08a3, 0x4),
  182. /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  183. INTEL_EVENT_CONSTRAINT(0x0ca3, 0x4),
  184. /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  185. INTEL_EVENT_CONSTRAINT(0x04a3, 0xf),
  186. EVENT_CONSTRAINT_END
  187. };
  188. static u64 intel_pmu_event_map(int hw_event)
  189. {
  190. return intel_perfmon_event_map[hw_event];
  191. }
  192. #define SNB_DMND_DATA_RD (1ULL << 0)
  193. #define SNB_DMND_RFO (1ULL << 1)
  194. #define SNB_DMND_IFETCH (1ULL << 2)
  195. #define SNB_DMND_WB (1ULL << 3)
  196. #define SNB_PF_DATA_RD (1ULL << 4)
  197. #define SNB_PF_RFO (1ULL << 5)
  198. #define SNB_PF_IFETCH (1ULL << 6)
  199. #define SNB_LLC_DATA_RD (1ULL << 7)
  200. #define SNB_LLC_RFO (1ULL << 8)
  201. #define SNB_LLC_IFETCH (1ULL << 9)
  202. #define SNB_BUS_LOCKS (1ULL << 10)
  203. #define SNB_STRM_ST (1ULL << 11)
  204. #define SNB_OTHER (1ULL << 15)
  205. #define SNB_RESP_ANY (1ULL << 16)
  206. #define SNB_NO_SUPP (1ULL << 17)
  207. #define SNB_LLC_HITM (1ULL << 18)
  208. #define SNB_LLC_HITE (1ULL << 19)
  209. #define SNB_LLC_HITS (1ULL << 20)
  210. #define SNB_LLC_HITF (1ULL << 21)
  211. #define SNB_LOCAL (1ULL << 22)
  212. #define SNB_REMOTE (0xffULL << 23)
  213. #define SNB_SNP_NONE (1ULL << 31)
  214. #define SNB_SNP_NOT_NEEDED (1ULL << 32)
  215. #define SNB_SNP_MISS (1ULL << 33)
  216. #define SNB_NO_FWD (1ULL << 34)
  217. #define SNB_SNP_FWD (1ULL << 35)
  218. #define SNB_HITM (1ULL << 36)
  219. #define SNB_NON_DRAM (1ULL << 37)
  220. #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
  221. #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
  222. #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  223. #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
  224. SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
  225. SNB_HITM)
  226. #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
  227. #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
  228. #define SNB_L3_ACCESS SNB_RESP_ANY
  229. #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
  230. static __initconst const u64 snb_hw_cache_extra_regs
  231. [PERF_COUNT_HW_CACHE_MAX]
  232. [PERF_COUNT_HW_CACHE_OP_MAX]
  233. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  234. {
  235. [ C(LL ) ] = {
  236. [ C(OP_READ) ] = {
  237. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
  238. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
  239. },
  240. [ C(OP_WRITE) ] = {
  241. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
  242. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
  243. },
  244. [ C(OP_PREFETCH) ] = {
  245. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
  246. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
  247. },
  248. },
  249. [ C(NODE) ] = {
  250. [ C(OP_READ) ] = {
  251. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
  252. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
  253. },
  254. [ C(OP_WRITE) ] = {
  255. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
  256. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
  257. },
  258. [ C(OP_PREFETCH) ] = {
  259. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
  260. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
  261. },
  262. },
  263. };
  264. static __initconst const u64 snb_hw_cache_event_ids
  265. [PERF_COUNT_HW_CACHE_MAX]
  266. [PERF_COUNT_HW_CACHE_OP_MAX]
  267. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  268. {
  269. [ C(L1D) ] = {
  270. [ C(OP_READ) ] = {
  271. [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
  272. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
  273. },
  274. [ C(OP_WRITE) ] = {
  275. [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
  276. [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
  277. },
  278. [ C(OP_PREFETCH) ] = {
  279. [ C(RESULT_ACCESS) ] = 0x0,
  280. [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
  281. },
  282. },
  283. [ C(L1I ) ] = {
  284. [ C(OP_READ) ] = {
  285. [ C(RESULT_ACCESS) ] = 0x0,
  286. [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
  287. },
  288. [ C(OP_WRITE) ] = {
  289. [ C(RESULT_ACCESS) ] = -1,
  290. [ C(RESULT_MISS) ] = -1,
  291. },
  292. [ C(OP_PREFETCH) ] = {
  293. [ C(RESULT_ACCESS) ] = 0x0,
  294. [ C(RESULT_MISS) ] = 0x0,
  295. },
  296. },
  297. [ C(LL ) ] = {
  298. [ C(OP_READ) ] = {
  299. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  300. [ C(RESULT_ACCESS) ] = 0x01b7,
  301. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  302. [ C(RESULT_MISS) ] = 0x01b7,
  303. },
  304. [ C(OP_WRITE) ] = {
  305. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  306. [ C(RESULT_ACCESS) ] = 0x01b7,
  307. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  308. [ C(RESULT_MISS) ] = 0x01b7,
  309. },
  310. [ C(OP_PREFETCH) ] = {
  311. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  312. [ C(RESULT_ACCESS) ] = 0x01b7,
  313. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  314. [ C(RESULT_MISS) ] = 0x01b7,
  315. },
  316. },
  317. [ C(DTLB) ] = {
  318. [ C(OP_READ) ] = {
  319. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
  320. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
  321. },
  322. [ C(OP_WRITE) ] = {
  323. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
  324. [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  325. },
  326. [ C(OP_PREFETCH) ] = {
  327. [ C(RESULT_ACCESS) ] = 0x0,
  328. [ C(RESULT_MISS) ] = 0x0,
  329. },
  330. },
  331. [ C(ITLB) ] = {
  332. [ C(OP_READ) ] = {
  333. [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
  334. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
  335. },
  336. [ C(OP_WRITE) ] = {
  337. [ C(RESULT_ACCESS) ] = -1,
  338. [ C(RESULT_MISS) ] = -1,
  339. },
  340. [ C(OP_PREFETCH) ] = {
  341. [ C(RESULT_ACCESS) ] = -1,
  342. [ C(RESULT_MISS) ] = -1,
  343. },
  344. },
  345. [ C(BPU ) ] = {
  346. [ C(OP_READ) ] = {
  347. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  348. [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  349. },
  350. [ C(OP_WRITE) ] = {
  351. [ C(RESULT_ACCESS) ] = -1,
  352. [ C(RESULT_MISS) ] = -1,
  353. },
  354. [ C(OP_PREFETCH) ] = {
  355. [ C(RESULT_ACCESS) ] = -1,
  356. [ C(RESULT_MISS) ] = -1,
  357. },
  358. },
  359. [ C(NODE) ] = {
  360. [ C(OP_READ) ] = {
  361. [ C(RESULT_ACCESS) ] = 0x01b7,
  362. [ C(RESULT_MISS) ] = 0x01b7,
  363. },
  364. [ C(OP_WRITE) ] = {
  365. [ C(RESULT_ACCESS) ] = 0x01b7,
  366. [ C(RESULT_MISS) ] = 0x01b7,
  367. },
  368. [ C(OP_PREFETCH) ] = {
  369. [ C(RESULT_ACCESS) ] = 0x01b7,
  370. [ C(RESULT_MISS) ] = 0x01b7,
  371. },
  372. },
  373. };
  374. static __initconst const u64 westmere_hw_cache_event_ids
  375. [PERF_COUNT_HW_CACHE_MAX]
  376. [PERF_COUNT_HW_CACHE_OP_MAX]
  377. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  378. {
  379. [ C(L1D) ] = {
  380. [ C(OP_READ) ] = {
  381. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  382. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  383. },
  384. [ C(OP_WRITE) ] = {
  385. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  386. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  387. },
  388. [ C(OP_PREFETCH) ] = {
  389. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  390. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  391. },
  392. },
  393. [ C(L1I ) ] = {
  394. [ C(OP_READ) ] = {
  395. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  396. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  397. },
  398. [ C(OP_WRITE) ] = {
  399. [ C(RESULT_ACCESS) ] = -1,
  400. [ C(RESULT_MISS) ] = -1,
  401. },
  402. [ C(OP_PREFETCH) ] = {
  403. [ C(RESULT_ACCESS) ] = 0x0,
  404. [ C(RESULT_MISS) ] = 0x0,
  405. },
  406. },
  407. [ C(LL ) ] = {
  408. [ C(OP_READ) ] = {
  409. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  410. [ C(RESULT_ACCESS) ] = 0x01b7,
  411. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  412. [ C(RESULT_MISS) ] = 0x01b7,
  413. },
  414. /*
  415. * Use RFO, not WRITEBACK, because a write miss would typically occur
  416. * on RFO.
  417. */
  418. [ C(OP_WRITE) ] = {
  419. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  420. [ C(RESULT_ACCESS) ] = 0x01b7,
  421. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  422. [ C(RESULT_MISS) ] = 0x01b7,
  423. },
  424. [ C(OP_PREFETCH) ] = {
  425. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  426. [ C(RESULT_ACCESS) ] = 0x01b7,
  427. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  428. [ C(RESULT_MISS) ] = 0x01b7,
  429. },
  430. },
  431. [ C(DTLB) ] = {
  432. [ C(OP_READ) ] = {
  433. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  434. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  435. },
  436. [ C(OP_WRITE) ] = {
  437. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  438. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  439. },
  440. [ C(OP_PREFETCH) ] = {
  441. [ C(RESULT_ACCESS) ] = 0x0,
  442. [ C(RESULT_MISS) ] = 0x0,
  443. },
  444. },
  445. [ C(ITLB) ] = {
  446. [ C(OP_READ) ] = {
  447. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  448. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
  449. },
  450. [ C(OP_WRITE) ] = {
  451. [ C(RESULT_ACCESS) ] = -1,
  452. [ C(RESULT_MISS) ] = -1,
  453. },
  454. [ C(OP_PREFETCH) ] = {
  455. [ C(RESULT_ACCESS) ] = -1,
  456. [ C(RESULT_MISS) ] = -1,
  457. },
  458. },
  459. [ C(BPU ) ] = {
  460. [ C(OP_READ) ] = {
  461. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  462. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  463. },
  464. [ C(OP_WRITE) ] = {
  465. [ C(RESULT_ACCESS) ] = -1,
  466. [ C(RESULT_MISS) ] = -1,
  467. },
  468. [ C(OP_PREFETCH) ] = {
  469. [ C(RESULT_ACCESS) ] = -1,
  470. [ C(RESULT_MISS) ] = -1,
  471. },
  472. },
  473. [ C(NODE) ] = {
  474. [ C(OP_READ) ] = {
  475. [ C(RESULT_ACCESS) ] = 0x01b7,
  476. [ C(RESULT_MISS) ] = 0x01b7,
  477. },
  478. [ C(OP_WRITE) ] = {
  479. [ C(RESULT_ACCESS) ] = 0x01b7,
  480. [ C(RESULT_MISS) ] = 0x01b7,
  481. },
  482. [ C(OP_PREFETCH) ] = {
  483. [ C(RESULT_ACCESS) ] = 0x01b7,
  484. [ C(RESULT_MISS) ] = 0x01b7,
  485. },
  486. },
  487. };
  488. /*
  489. * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
  490. * See IA32 SDM Vol 3B 30.6.1.3
  491. */
  492. #define NHM_DMND_DATA_RD (1 << 0)
  493. #define NHM_DMND_RFO (1 << 1)
  494. #define NHM_DMND_IFETCH (1 << 2)
  495. #define NHM_DMND_WB (1 << 3)
  496. #define NHM_PF_DATA_RD (1 << 4)
  497. #define NHM_PF_DATA_RFO (1 << 5)
  498. #define NHM_PF_IFETCH (1 << 6)
  499. #define NHM_OFFCORE_OTHER (1 << 7)
  500. #define NHM_UNCORE_HIT (1 << 8)
  501. #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
  502. #define NHM_OTHER_CORE_HITM (1 << 10)
  503. /* reserved */
  504. #define NHM_REMOTE_CACHE_FWD (1 << 12)
  505. #define NHM_REMOTE_DRAM (1 << 13)
  506. #define NHM_LOCAL_DRAM (1 << 14)
  507. #define NHM_NON_DRAM (1 << 15)
  508. #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
  509. #define NHM_REMOTE (NHM_REMOTE_DRAM)
  510. #define NHM_DMND_READ (NHM_DMND_DATA_RD)
  511. #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
  512. #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
  513. #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
  514. #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
  515. #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
  516. static __initconst const u64 nehalem_hw_cache_extra_regs
  517. [PERF_COUNT_HW_CACHE_MAX]
  518. [PERF_COUNT_HW_CACHE_OP_MAX]
  519. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  520. {
  521. [ C(LL ) ] = {
  522. [ C(OP_READ) ] = {
  523. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
  524. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
  525. },
  526. [ C(OP_WRITE) ] = {
  527. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
  528. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
  529. },
  530. [ C(OP_PREFETCH) ] = {
  531. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
  532. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
  533. },
  534. },
  535. [ C(NODE) ] = {
  536. [ C(OP_READ) ] = {
  537. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
  538. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
  539. },
  540. [ C(OP_WRITE) ] = {
  541. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
  542. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
  543. },
  544. [ C(OP_PREFETCH) ] = {
  545. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
  546. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
  547. },
  548. },
  549. };
  550. static __initconst const u64 nehalem_hw_cache_event_ids
  551. [PERF_COUNT_HW_CACHE_MAX]
  552. [PERF_COUNT_HW_CACHE_OP_MAX]
  553. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  554. {
  555. [ C(L1D) ] = {
  556. [ C(OP_READ) ] = {
  557. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  558. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  559. },
  560. [ C(OP_WRITE) ] = {
  561. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  562. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  563. },
  564. [ C(OP_PREFETCH) ] = {
  565. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  566. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  567. },
  568. },
  569. [ C(L1I ) ] = {
  570. [ C(OP_READ) ] = {
  571. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  572. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  573. },
  574. [ C(OP_WRITE) ] = {
  575. [ C(RESULT_ACCESS) ] = -1,
  576. [ C(RESULT_MISS) ] = -1,
  577. },
  578. [ C(OP_PREFETCH) ] = {
  579. [ C(RESULT_ACCESS) ] = 0x0,
  580. [ C(RESULT_MISS) ] = 0x0,
  581. },
  582. },
  583. [ C(LL ) ] = {
  584. [ C(OP_READ) ] = {
  585. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  586. [ C(RESULT_ACCESS) ] = 0x01b7,
  587. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  588. [ C(RESULT_MISS) ] = 0x01b7,
  589. },
  590. /*
  591. * Use RFO, not WRITEBACK, because a write miss would typically occur
  592. * on RFO.
  593. */
  594. [ C(OP_WRITE) ] = {
  595. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  596. [ C(RESULT_ACCESS) ] = 0x01b7,
  597. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  598. [ C(RESULT_MISS) ] = 0x01b7,
  599. },
  600. [ C(OP_PREFETCH) ] = {
  601. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  602. [ C(RESULT_ACCESS) ] = 0x01b7,
  603. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  604. [ C(RESULT_MISS) ] = 0x01b7,
  605. },
  606. },
  607. [ C(DTLB) ] = {
  608. [ C(OP_READ) ] = {
  609. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  610. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  611. },
  612. [ C(OP_WRITE) ] = {
  613. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  614. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  615. },
  616. [ C(OP_PREFETCH) ] = {
  617. [ C(RESULT_ACCESS) ] = 0x0,
  618. [ C(RESULT_MISS) ] = 0x0,
  619. },
  620. },
  621. [ C(ITLB) ] = {
  622. [ C(OP_READ) ] = {
  623. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  624. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  625. },
  626. [ C(OP_WRITE) ] = {
  627. [ C(RESULT_ACCESS) ] = -1,
  628. [ C(RESULT_MISS) ] = -1,
  629. },
  630. [ C(OP_PREFETCH) ] = {
  631. [ C(RESULT_ACCESS) ] = -1,
  632. [ C(RESULT_MISS) ] = -1,
  633. },
  634. },
  635. [ C(BPU ) ] = {
  636. [ C(OP_READ) ] = {
  637. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  638. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  639. },
  640. [ C(OP_WRITE) ] = {
  641. [ C(RESULT_ACCESS) ] = -1,
  642. [ C(RESULT_MISS) ] = -1,
  643. },
  644. [ C(OP_PREFETCH) ] = {
  645. [ C(RESULT_ACCESS) ] = -1,
  646. [ C(RESULT_MISS) ] = -1,
  647. },
  648. },
  649. [ C(NODE) ] = {
  650. [ C(OP_READ) ] = {
  651. [ C(RESULT_ACCESS) ] = 0x01b7,
  652. [ C(RESULT_MISS) ] = 0x01b7,
  653. },
  654. [ C(OP_WRITE) ] = {
  655. [ C(RESULT_ACCESS) ] = 0x01b7,
  656. [ C(RESULT_MISS) ] = 0x01b7,
  657. },
  658. [ C(OP_PREFETCH) ] = {
  659. [ C(RESULT_ACCESS) ] = 0x01b7,
  660. [ C(RESULT_MISS) ] = 0x01b7,
  661. },
  662. },
  663. };
  664. static __initconst const u64 core2_hw_cache_event_ids
  665. [PERF_COUNT_HW_CACHE_MAX]
  666. [PERF_COUNT_HW_CACHE_OP_MAX]
  667. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  668. {
  669. [ C(L1D) ] = {
  670. [ C(OP_READ) ] = {
  671. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  672. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  673. },
  674. [ C(OP_WRITE) ] = {
  675. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  676. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  677. },
  678. [ C(OP_PREFETCH) ] = {
  679. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  680. [ C(RESULT_MISS) ] = 0,
  681. },
  682. },
  683. [ C(L1I ) ] = {
  684. [ C(OP_READ) ] = {
  685. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  686. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  687. },
  688. [ C(OP_WRITE) ] = {
  689. [ C(RESULT_ACCESS) ] = -1,
  690. [ C(RESULT_MISS) ] = -1,
  691. },
  692. [ C(OP_PREFETCH) ] = {
  693. [ C(RESULT_ACCESS) ] = 0,
  694. [ C(RESULT_MISS) ] = 0,
  695. },
  696. },
  697. [ C(LL ) ] = {
  698. [ C(OP_READ) ] = {
  699. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  700. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  701. },
  702. [ C(OP_WRITE) ] = {
  703. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  704. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  705. },
  706. [ C(OP_PREFETCH) ] = {
  707. [ C(RESULT_ACCESS) ] = 0,
  708. [ C(RESULT_MISS) ] = 0,
  709. },
  710. },
  711. [ C(DTLB) ] = {
  712. [ C(OP_READ) ] = {
  713. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  714. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  715. },
  716. [ C(OP_WRITE) ] = {
  717. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  718. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  719. },
  720. [ C(OP_PREFETCH) ] = {
  721. [ C(RESULT_ACCESS) ] = 0,
  722. [ C(RESULT_MISS) ] = 0,
  723. },
  724. },
  725. [ C(ITLB) ] = {
  726. [ C(OP_READ) ] = {
  727. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  728. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  729. },
  730. [ C(OP_WRITE) ] = {
  731. [ C(RESULT_ACCESS) ] = -1,
  732. [ C(RESULT_MISS) ] = -1,
  733. },
  734. [ C(OP_PREFETCH) ] = {
  735. [ C(RESULT_ACCESS) ] = -1,
  736. [ C(RESULT_MISS) ] = -1,
  737. },
  738. },
  739. [ C(BPU ) ] = {
  740. [ C(OP_READ) ] = {
  741. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  742. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  743. },
  744. [ C(OP_WRITE) ] = {
  745. [ C(RESULT_ACCESS) ] = -1,
  746. [ C(RESULT_MISS) ] = -1,
  747. },
  748. [ C(OP_PREFETCH) ] = {
  749. [ C(RESULT_ACCESS) ] = -1,
  750. [ C(RESULT_MISS) ] = -1,
  751. },
  752. },
  753. };
  754. static __initconst const u64 atom_hw_cache_event_ids
  755. [PERF_COUNT_HW_CACHE_MAX]
  756. [PERF_COUNT_HW_CACHE_OP_MAX]
  757. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  758. {
  759. [ C(L1D) ] = {
  760. [ C(OP_READ) ] = {
  761. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  762. [ C(RESULT_MISS) ] = 0,
  763. },
  764. [ C(OP_WRITE) ] = {
  765. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  766. [ C(RESULT_MISS) ] = 0,
  767. },
  768. [ C(OP_PREFETCH) ] = {
  769. [ C(RESULT_ACCESS) ] = 0x0,
  770. [ C(RESULT_MISS) ] = 0,
  771. },
  772. },
  773. [ C(L1I ) ] = {
  774. [ C(OP_READ) ] = {
  775. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  776. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  777. },
  778. [ C(OP_WRITE) ] = {
  779. [ C(RESULT_ACCESS) ] = -1,
  780. [ C(RESULT_MISS) ] = -1,
  781. },
  782. [ C(OP_PREFETCH) ] = {
  783. [ C(RESULT_ACCESS) ] = 0,
  784. [ C(RESULT_MISS) ] = 0,
  785. },
  786. },
  787. [ C(LL ) ] = {
  788. [ C(OP_READ) ] = {
  789. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  790. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  791. },
  792. [ C(OP_WRITE) ] = {
  793. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  794. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  795. },
  796. [ C(OP_PREFETCH) ] = {
  797. [ C(RESULT_ACCESS) ] = 0,
  798. [ C(RESULT_MISS) ] = 0,
  799. },
  800. },
  801. [ C(DTLB) ] = {
  802. [ C(OP_READ) ] = {
  803. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  804. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  805. },
  806. [ C(OP_WRITE) ] = {
  807. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  808. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  809. },
  810. [ C(OP_PREFETCH) ] = {
  811. [ C(RESULT_ACCESS) ] = 0,
  812. [ C(RESULT_MISS) ] = 0,
  813. },
  814. },
  815. [ C(ITLB) ] = {
  816. [ C(OP_READ) ] = {
  817. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  818. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  819. },
  820. [ C(OP_WRITE) ] = {
  821. [ C(RESULT_ACCESS) ] = -1,
  822. [ C(RESULT_MISS) ] = -1,
  823. },
  824. [ C(OP_PREFETCH) ] = {
  825. [ C(RESULT_ACCESS) ] = -1,
  826. [ C(RESULT_MISS) ] = -1,
  827. },
  828. },
  829. [ C(BPU ) ] = {
  830. [ C(OP_READ) ] = {
  831. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  832. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  833. },
  834. [ C(OP_WRITE) ] = {
  835. [ C(RESULT_ACCESS) ] = -1,
  836. [ C(RESULT_MISS) ] = -1,
  837. },
  838. [ C(OP_PREFETCH) ] = {
  839. [ C(RESULT_ACCESS) ] = -1,
  840. [ C(RESULT_MISS) ] = -1,
  841. },
  842. },
  843. };
  844. static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
  845. {
  846. /* user explicitly requested branch sampling */
  847. if (has_branch_stack(event))
  848. return true;
  849. /* implicit branch sampling to correct PEBS skid */
  850. if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
  851. x86_pmu.intel_cap.pebs_format < 2)
  852. return true;
  853. return false;
  854. }
  855. static void intel_pmu_disable_all(void)
  856. {
  857. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  858. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  859. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  860. intel_pmu_disable_bts();
  861. intel_pmu_pebs_disable_all();
  862. intel_pmu_lbr_disable_all();
  863. }
  864. static void intel_pmu_enable_all(int added)
  865. {
  866. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  867. intel_pmu_pebs_enable_all();
  868. intel_pmu_lbr_enable_all();
  869. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
  870. x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
  871. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  872. struct perf_event *event =
  873. cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  874. if (WARN_ON_ONCE(!event))
  875. return;
  876. intel_pmu_enable_bts(event->hw.config);
  877. }
  878. }
  879. /*
  880. * Workaround for:
  881. * Intel Errata AAK100 (model 26)
  882. * Intel Errata AAP53 (model 30)
  883. * Intel Errata BD53 (model 44)
  884. *
  885. * The official story:
  886. * These chips need to be 'reset' when adding counters by programming the
  887. * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
  888. * in sequence on the same PMC or on different PMCs.
  889. *
  890. * In practise it appears some of these events do in fact count, and
  891. * we need to programm all 4 events.
  892. */
  893. static void intel_pmu_nhm_workaround(void)
  894. {
  895. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  896. static const unsigned long nhm_magic[4] = {
  897. 0x4300B5,
  898. 0x4300D2,
  899. 0x4300B1,
  900. 0x4300B1
  901. };
  902. struct perf_event *event;
  903. int i;
  904. /*
  905. * The Errata requires below steps:
  906. * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
  907. * 2) Configure 4 PERFEVTSELx with the magic events and clear
  908. * the corresponding PMCx;
  909. * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
  910. * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
  911. * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
  912. */
  913. /*
  914. * The real steps we choose are a little different from above.
  915. * A) To reduce MSR operations, we don't run step 1) as they
  916. * are already cleared before this function is called;
  917. * B) Call x86_perf_event_update to save PMCx before configuring
  918. * PERFEVTSELx with magic number;
  919. * C) With step 5), we do clear only when the PERFEVTSELx is
  920. * not used currently.
  921. * D) Call x86_perf_event_set_period to restore PMCx;
  922. */
  923. /* We always operate 4 pairs of PERF Counters */
  924. for (i = 0; i < 4; i++) {
  925. event = cpuc->events[i];
  926. if (event)
  927. x86_perf_event_update(event);
  928. }
  929. for (i = 0; i < 4; i++) {
  930. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
  931. wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
  932. }
  933. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
  934. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
  935. for (i = 0; i < 4; i++) {
  936. event = cpuc->events[i];
  937. if (event) {
  938. x86_perf_event_set_period(event);
  939. __x86_pmu_enable_event(&event->hw,
  940. ARCH_PERFMON_EVENTSEL_ENABLE);
  941. } else
  942. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
  943. }
  944. }
  945. static void intel_pmu_nhm_enable_all(int added)
  946. {
  947. if (added)
  948. intel_pmu_nhm_workaround();
  949. intel_pmu_enable_all(added);
  950. }
  951. static inline u64 intel_pmu_get_status(void)
  952. {
  953. u64 status;
  954. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  955. return status;
  956. }
  957. static inline void intel_pmu_ack_status(u64 ack)
  958. {
  959. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  960. }
  961. static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
  962. {
  963. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  964. u64 ctrl_val, mask;
  965. mask = 0xfULL << (idx * 4);
  966. rdmsrl(hwc->config_base, ctrl_val);
  967. ctrl_val &= ~mask;
  968. wrmsrl(hwc->config_base, ctrl_val);
  969. }
  970. static void intel_pmu_disable_event(struct perf_event *event)
  971. {
  972. struct hw_perf_event *hwc = &event->hw;
  973. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  974. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  975. intel_pmu_disable_bts();
  976. intel_pmu_drain_bts_buffer();
  977. return;
  978. }
  979. cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
  980. cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
  981. /*
  982. * must disable before any actual event
  983. * because any event may be combined with LBR
  984. */
  985. if (intel_pmu_needs_lbr_smpl(event))
  986. intel_pmu_lbr_disable(event);
  987. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  988. intel_pmu_disable_fixed(hwc);
  989. return;
  990. }
  991. x86_pmu_disable_event(event);
  992. if (unlikely(event->attr.precise_ip))
  993. intel_pmu_pebs_disable(event);
  994. }
  995. static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
  996. {
  997. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  998. u64 ctrl_val, bits, mask;
  999. /*
  1000. * Enable IRQ generation (0x8),
  1001. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  1002. * if requested:
  1003. */
  1004. bits = 0x8ULL;
  1005. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  1006. bits |= 0x2;
  1007. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  1008. bits |= 0x1;
  1009. /*
  1010. * ANY bit is supported in v3 and up
  1011. */
  1012. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  1013. bits |= 0x4;
  1014. bits <<= (idx * 4);
  1015. mask = 0xfULL << (idx * 4);
  1016. rdmsrl(hwc->config_base, ctrl_val);
  1017. ctrl_val &= ~mask;
  1018. ctrl_val |= bits;
  1019. wrmsrl(hwc->config_base, ctrl_val);
  1020. }
  1021. static void intel_pmu_enable_event(struct perf_event *event)
  1022. {
  1023. struct hw_perf_event *hwc = &event->hw;
  1024. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1025. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  1026. if (!__this_cpu_read(cpu_hw_events.enabled))
  1027. return;
  1028. intel_pmu_enable_bts(hwc->config);
  1029. return;
  1030. }
  1031. /*
  1032. * must enabled before any actual event
  1033. * because any event may be combined with LBR
  1034. */
  1035. if (intel_pmu_needs_lbr_smpl(event))
  1036. intel_pmu_lbr_enable(event);
  1037. if (event->attr.exclude_host)
  1038. cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
  1039. if (event->attr.exclude_guest)
  1040. cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
  1041. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1042. intel_pmu_enable_fixed(hwc);
  1043. return;
  1044. }
  1045. if (unlikely(event->attr.precise_ip))
  1046. intel_pmu_pebs_enable(event);
  1047. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  1048. }
  1049. /*
  1050. * Save and restart an expired event. Called by NMI contexts,
  1051. * so it has to be careful about preempting normal event ops:
  1052. */
  1053. int intel_pmu_save_and_restart(struct perf_event *event)
  1054. {
  1055. x86_perf_event_update(event);
  1056. return x86_perf_event_set_period(event);
  1057. }
  1058. static void intel_pmu_reset(void)
  1059. {
  1060. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  1061. unsigned long flags;
  1062. int idx;
  1063. if (!x86_pmu.num_counters)
  1064. return;
  1065. local_irq_save(flags);
  1066. pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
  1067. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1068. wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
  1069. wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
  1070. }
  1071. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
  1072. wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1073. if (ds)
  1074. ds->bts_index = ds->bts_buffer_base;
  1075. local_irq_restore(flags);
  1076. }
  1077. /*
  1078. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1079. * rules apply:
  1080. */
  1081. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1082. {
  1083. struct perf_sample_data data;
  1084. struct cpu_hw_events *cpuc;
  1085. int bit, loops;
  1086. u64 status;
  1087. int handled;
  1088. cpuc = &__get_cpu_var(cpu_hw_events);
  1089. /*
  1090. * Some chipsets need to unmask the LVTPC in a particular spot
  1091. * inside the nmi handler. As a result, the unmasking was pushed
  1092. * into all the nmi handlers.
  1093. *
  1094. * This handler doesn't seem to have any issues with the unmasking
  1095. * so it was left at the top.
  1096. */
  1097. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1098. intel_pmu_disable_all();
  1099. handled = intel_pmu_drain_bts_buffer();
  1100. status = intel_pmu_get_status();
  1101. if (!status) {
  1102. intel_pmu_enable_all(0);
  1103. return handled;
  1104. }
  1105. loops = 0;
  1106. again:
  1107. intel_pmu_ack_status(status);
  1108. if (++loops > 100) {
  1109. static bool warned = false;
  1110. if (!warned) {
  1111. WARN(1, "perfevents: irq loop stuck!\n");
  1112. perf_event_print_debug();
  1113. warned = true;
  1114. }
  1115. intel_pmu_reset();
  1116. goto done;
  1117. }
  1118. inc_irq_stat(apic_perf_irqs);
  1119. intel_pmu_lbr_read();
  1120. /*
  1121. * PEBS overflow sets bit 62 in the global status register
  1122. */
  1123. if (__test_and_clear_bit(62, (unsigned long *)&status)) {
  1124. handled++;
  1125. x86_pmu.drain_pebs(regs);
  1126. }
  1127. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1128. struct perf_event *event = cpuc->events[bit];
  1129. handled++;
  1130. if (!test_bit(bit, cpuc->active_mask))
  1131. continue;
  1132. if (!intel_pmu_save_and_restart(event))
  1133. continue;
  1134. perf_sample_data_init(&data, 0, event->hw.last_period);
  1135. if (has_branch_stack(event))
  1136. data.br_stack = &cpuc->lbr_stack;
  1137. if (perf_event_overflow(event, &data, regs))
  1138. x86_pmu_stop(event, 0);
  1139. }
  1140. /*
  1141. * Repeat if there is more work to be done:
  1142. */
  1143. status = intel_pmu_get_status();
  1144. if (status)
  1145. goto again;
  1146. done:
  1147. intel_pmu_enable_all(0);
  1148. return handled;
  1149. }
  1150. static struct event_constraint *
  1151. intel_bts_constraints(struct perf_event *event)
  1152. {
  1153. struct hw_perf_event *hwc = &event->hw;
  1154. unsigned int hw_event, bts_event;
  1155. if (event->attr.freq)
  1156. return NULL;
  1157. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  1158. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  1159. if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
  1160. return &bts_constraint;
  1161. return NULL;
  1162. }
  1163. static int intel_alt_er(int idx)
  1164. {
  1165. if (!(x86_pmu.er_flags & ERF_HAS_RSP_1))
  1166. return idx;
  1167. if (idx == EXTRA_REG_RSP_0)
  1168. return EXTRA_REG_RSP_1;
  1169. if (idx == EXTRA_REG_RSP_1)
  1170. return EXTRA_REG_RSP_0;
  1171. return idx;
  1172. }
  1173. static void intel_fixup_er(struct perf_event *event, int idx)
  1174. {
  1175. event->hw.extra_reg.idx = idx;
  1176. if (idx == EXTRA_REG_RSP_0) {
  1177. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1178. event->hw.config |= 0x01b7;
  1179. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
  1180. } else if (idx == EXTRA_REG_RSP_1) {
  1181. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1182. event->hw.config |= 0x01bb;
  1183. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
  1184. }
  1185. }
  1186. /*
  1187. * manage allocation of shared extra msr for certain events
  1188. *
  1189. * sharing can be:
  1190. * per-cpu: to be shared between the various events on a single PMU
  1191. * per-core: per-cpu + shared by HT threads
  1192. */
  1193. static struct event_constraint *
  1194. __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
  1195. struct perf_event *event,
  1196. struct hw_perf_event_extra *reg)
  1197. {
  1198. struct event_constraint *c = &emptyconstraint;
  1199. struct er_account *era;
  1200. unsigned long flags;
  1201. int idx = reg->idx;
  1202. /*
  1203. * reg->alloc can be set due to existing state, so for fake cpuc we
  1204. * need to ignore this, otherwise we might fail to allocate proper fake
  1205. * state for this extra reg constraint. Also see the comment below.
  1206. */
  1207. if (reg->alloc && !cpuc->is_fake)
  1208. return NULL; /* call x86_get_event_constraint() */
  1209. again:
  1210. era = &cpuc->shared_regs->regs[idx];
  1211. /*
  1212. * we use spin_lock_irqsave() to avoid lockdep issues when
  1213. * passing a fake cpuc
  1214. */
  1215. raw_spin_lock_irqsave(&era->lock, flags);
  1216. if (!atomic_read(&era->ref) || era->config == reg->config) {
  1217. /*
  1218. * If its a fake cpuc -- as per validate_{group,event}() we
  1219. * shouldn't touch event state and we can avoid doing so
  1220. * since both will only call get_event_constraints() once
  1221. * on each event, this avoids the need for reg->alloc.
  1222. *
  1223. * Not doing the ER fixup will only result in era->reg being
  1224. * wrong, but since we won't actually try and program hardware
  1225. * this isn't a problem either.
  1226. */
  1227. if (!cpuc->is_fake) {
  1228. if (idx != reg->idx)
  1229. intel_fixup_er(event, idx);
  1230. /*
  1231. * x86_schedule_events() can call get_event_constraints()
  1232. * multiple times on events in the case of incremental
  1233. * scheduling(). reg->alloc ensures we only do the ER
  1234. * allocation once.
  1235. */
  1236. reg->alloc = 1;
  1237. }
  1238. /* lock in msr value */
  1239. era->config = reg->config;
  1240. era->reg = reg->reg;
  1241. /* one more user */
  1242. atomic_inc(&era->ref);
  1243. /*
  1244. * need to call x86_get_event_constraint()
  1245. * to check if associated event has constraints
  1246. */
  1247. c = NULL;
  1248. } else {
  1249. idx = intel_alt_er(idx);
  1250. if (idx != reg->idx) {
  1251. raw_spin_unlock_irqrestore(&era->lock, flags);
  1252. goto again;
  1253. }
  1254. }
  1255. raw_spin_unlock_irqrestore(&era->lock, flags);
  1256. return c;
  1257. }
  1258. static void
  1259. __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
  1260. struct hw_perf_event_extra *reg)
  1261. {
  1262. struct er_account *era;
  1263. /*
  1264. * Only put constraint if extra reg was actually allocated. Also takes
  1265. * care of event which do not use an extra shared reg.
  1266. *
  1267. * Also, if this is a fake cpuc we shouldn't touch any event state
  1268. * (reg->alloc) and we don't care about leaving inconsistent cpuc state
  1269. * either since it'll be thrown out.
  1270. */
  1271. if (!reg->alloc || cpuc->is_fake)
  1272. return;
  1273. era = &cpuc->shared_regs->regs[reg->idx];
  1274. /* one fewer user */
  1275. atomic_dec(&era->ref);
  1276. /* allocate again next time */
  1277. reg->alloc = 0;
  1278. }
  1279. static struct event_constraint *
  1280. intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
  1281. struct perf_event *event)
  1282. {
  1283. struct event_constraint *c = NULL, *d;
  1284. struct hw_perf_event_extra *xreg, *breg;
  1285. xreg = &event->hw.extra_reg;
  1286. if (xreg->idx != EXTRA_REG_NONE) {
  1287. c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
  1288. if (c == &emptyconstraint)
  1289. return c;
  1290. }
  1291. breg = &event->hw.branch_reg;
  1292. if (breg->idx != EXTRA_REG_NONE) {
  1293. d = __intel_shared_reg_get_constraints(cpuc, event, breg);
  1294. if (d == &emptyconstraint) {
  1295. __intel_shared_reg_put_constraints(cpuc, xreg);
  1296. c = d;
  1297. }
  1298. }
  1299. return c;
  1300. }
  1301. struct event_constraint *
  1302. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1303. {
  1304. struct event_constraint *c;
  1305. if (x86_pmu.event_constraints) {
  1306. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1307. if ((event->hw.config & c->cmask) == c->code) {
  1308. /* hw.flags zeroed at initialization */
  1309. event->hw.flags |= c->flags;
  1310. return c;
  1311. }
  1312. }
  1313. }
  1314. return &unconstrained;
  1315. }
  1316. static struct event_constraint *
  1317. intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1318. {
  1319. struct event_constraint *c;
  1320. c = intel_bts_constraints(event);
  1321. if (c)
  1322. return c;
  1323. c = intel_pebs_constraints(event);
  1324. if (c)
  1325. return c;
  1326. c = intel_shared_regs_constraints(cpuc, event);
  1327. if (c)
  1328. return c;
  1329. return x86_get_event_constraints(cpuc, event);
  1330. }
  1331. static void
  1332. intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
  1333. struct perf_event *event)
  1334. {
  1335. struct hw_perf_event_extra *reg;
  1336. reg = &event->hw.extra_reg;
  1337. if (reg->idx != EXTRA_REG_NONE)
  1338. __intel_shared_reg_put_constraints(cpuc, reg);
  1339. reg = &event->hw.branch_reg;
  1340. if (reg->idx != EXTRA_REG_NONE)
  1341. __intel_shared_reg_put_constraints(cpuc, reg);
  1342. }
  1343. static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
  1344. struct perf_event *event)
  1345. {
  1346. event->hw.flags = 0;
  1347. intel_put_shared_regs_event_constraints(cpuc, event);
  1348. }
  1349. static void intel_pebs_aliases_core2(struct perf_event *event)
  1350. {
  1351. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  1352. /*
  1353. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  1354. * (0x003c) so that we can use it with PEBS.
  1355. *
  1356. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  1357. * PEBS capable. However we can use INST_RETIRED.ANY_P
  1358. * (0x00c0), which is a PEBS capable event, to get the same
  1359. * count.
  1360. *
  1361. * INST_RETIRED.ANY_P counts the number of cycles that retires
  1362. * CNTMASK instructions. By setting CNTMASK to a value (16)
  1363. * larger than the maximum number of instructions that can be
  1364. * retired per cycle (4) and then inverting the condition, we
  1365. * count all cycles that retire 16 or less instructions, which
  1366. * is every cycle.
  1367. *
  1368. * Thereby we gain a PEBS capable cycle counter.
  1369. */
  1370. u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
  1371. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  1372. event->hw.config = alt_config;
  1373. }
  1374. }
  1375. static void intel_pebs_aliases_snb(struct perf_event *event)
  1376. {
  1377. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  1378. /*
  1379. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  1380. * (0x003c) so that we can use it with PEBS.
  1381. *
  1382. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  1383. * PEBS capable. However we can use UOPS_RETIRED.ALL
  1384. * (0x01c2), which is a PEBS capable event, to get the same
  1385. * count.
  1386. *
  1387. * UOPS_RETIRED.ALL counts the number of cycles that retires
  1388. * CNTMASK micro-ops. By setting CNTMASK to a value (16)
  1389. * larger than the maximum number of micro-ops that can be
  1390. * retired per cycle (4) and then inverting the condition, we
  1391. * count all cycles that retire 16 or less micro-ops, which
  1392. * is every cycle.
  1393. *
  1394. * Thereby we gain a PEBS capable cycle counter.
  1395. */
  1396. u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
  1397. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  1398. event->hw.config = alt_config;
  1399. }
  1400. }
  1401. static int intel_pmu_hw_config(struct perf_event *event)
  1402. {
  1403. int ret = x86_pmu_hw_config(event);
  1404. if (ret)
  1405. return ret;
  1406. if (event->attr.precise_ip && x86_pmu.pebs_aliases)
  1407. x86_pmu.pebs_aliases(event);
  1408. if (intel_pmu_needs_lbr_smpl(event)) {
  1409. ret = intel_pmu_setup_lbr_filter(event);
  1410. if (ret)
  1411. return ret;
  1412. }
  1413. if (event->attr.type != PERF_TYPE_RAW)
  1414. return 0;
  1415. if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
  1416. return 0;
  1417. if (x86_pmu.version < 3)
  1418. return -EINVAL;
  1419. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1420. return -EACCES;
  1421. event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
  1422. return 0;
  1423. }
  1424. struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
  1425. {
  1426. if (x86_pmu.guest_get_msrs)
  1427. return x86_pmu.guest_get_msrs(nr);
  1428. *nr = 0;
  1429. return NULL;
  1430. }
  1431. EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
  1432. static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
  1433. {
  1434. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1435. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  1436. arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
  1437. arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
  1438. arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
  1439. /*
  1440. * If PMU counter has PEBS enabled it is not enough to disable counter
  1441. * on a guest entry since PEBS memory write can overshoot guest entry
  1442. * and corrupt guest memory. Disabling PEBS solves the problem.
  1443. */
  1444. arr[1].msr = MSR_IA32_PEBS_ENABLE;
  1445. arr[1].host = cpuc->pebs_enabled;
  1446. arr[1].guest = 0;
  1447. *nr = 2;
  1448. return arr;
  1449. }
  1450. static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
  1451. {
  1452. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1453. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  1454. int idx;
  1455. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1456. struct perf_event *event = cpuc->events[idx];
  1457. arr[idx].msr = x86_pmu_config_addr(idx);
  1458. arr[idx].host = arr[idx].guest = 0;
  1459. if (!test_bit(idx, cpuc->active_mask))
  1460. continue;
  1461. arr[idx].host = arr[idx].guest =
  1462. event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
  1463. if (event->attr.exclude_host)
  1464. arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  1465. else if (event->attr.exclude_guest)
  1466. arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  1467. }
  1468. *nr = x86_pmu.num_counters;
  1469. return arr;
  1470. }
  1471. static void core_pmu_enable_event(struct perf_event *event)
  1472. {
  1473. if (!event->attr.exclude_host)
  1474. x86_pmu_enable_event(event);
  1475. }
  1476. static void core_pmu_enable_all(int added)
  1477. {
  1478. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1479. int idx;
  1480. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1481. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  1482. if (!test_bit(idx, cpuc->active_mask) ||
  1483. cpuc->events[idx]->attr.exclude_host)
  1484. continue;
  1485. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  1486. }
  1487. }
  1488. static int hsw_hw_config(struct perf_event *event)
  1489. {
  1490. int ret = intel_pmu_hw_config(event);
  1491. if (ret)
  1492. return ret;
  1493. if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
  1494. return 0;
  1495. event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
  1496. /*
  1497. * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
  1498. * PEBS or in ANY thread mode. Since the results are non-sensical forbid
  1499. * this combination.
  1500. */
  1501. if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
  1502. ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
  1503. event->attr.precise_ip > 0))
  1504. return -EOPNOTSUPP;
  1505. return 0;
  1506. }
  1507. static struct event_constraint counter2_constraint =
  1508. EVENT_CONSTRAINT(0, 0x4, 0);
  1509. static struct event_constraint *
  1510. hsw_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1511. {
  1512. struct event_constraint *c = intel_get_event_constraints(cpuc, event);
  1513. /* Handle special quirk on in_tx_checkpointed only in counter 2 */
  1514. if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
  1515. if (c->idxmsk64 & (1U << 2))
  1516. return &counter2_constraint;
  1517. return &emptyconstraint;
  1518. }
  1519. return c;
  1520. }
  1521. PMU_FORMAT_ATTR(event, "config:0-7" );
  1522. PMU_FORMAT_ATTR(umask, "config:8-15" );
  1523. PMU_FORMAT_ATTR(edge, "config:18" );
  1524. PMU_FORMAT_ATTR(pc, "config:19" );
  1525. PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
  1526. PMU_FORMAT_ATTR(inv, "config:23" );
  1527. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  1528. PMU_FORMAT_ATTR(in_tx, "config:32");
  1529. PMU_FORMAT_ATTR(in_tx_cp, "config:33");
  1530. static struct attribute *intel_arch_formats_attr[] = {
  1531. &format_attr_event.attr,
  1532. &format_attr_umask.attr,
  1533. &format_attr_edge.attr,
  1534. &format_attr_pc.attr,
  1535. &format_attr_inv.attr,
  1536. &format_attr_cmask.attr,
  1537. NULL,
  1538. };
  1539. ssize_t intel_event_sysfs_show(char *page, u64 config)
  1540. {
  1541. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
  1542. return x86_event_sysfs_show(page, config, event);
  1543. }
  1544. static __initconst const struct x86_pmu core_pmu = {
  1545. .name = "core",
  1546. .handle_irq = x86_pmu_handle_irq,
  1547. .disable_all = x86_pmu_disable_all,
  1548. .enable_all = core_pmu_enable_all,
  1549. .enable = core_pmu_enable_event,
  1550. .disable = x86_pmu_disable_event,
  1551. .hw_config = x86_pmu_hw_config,
  1552. .schedule_events = x86_schedule_events,
  1553. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1554. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1555. .event_map = intel_pmu_event_map,
  1556. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1557. .apic = 1,
  1558. /*
  1559. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1560. * so we install an artificial 1<<31 period regardless of
  1561. * the generic event period:
  1562. */
  1563. .max_period = (1ULL << 31) - 1,
  1564. .get_event_constraints = intel_get_event_constraints,
  1565. .put_event_constraints = intel_put_event_constraints,
  1566. .event_constraints = intel_core_event_constraints,
  1567. .guest_get_msrs = core_guest_get_msrs,
  1568. .format_attrs = intel_arch_formats_attr,
  1569. .events_sysfs_show = intel_event_sysfs_show,
  1570. };
  1571. struct intel_shared_regs *allocate_shared_regs(int cpu)
  1572. {
  1573. struct intel_shared_regs *regs;
  1574. int i;
  1575. regs = kzalloc_node(sizeof(struct intel_shared_regs),
  1576. GFP_KERNEL, cpu_to_node(cpu));
  1577. if (regs) {
  1578. /*
  1579. * initialize the locks to keep lockdep happy
  1580. */
  1581. for (i = 0; i < EXTRA_REG_MAX; i++)
  1582. raw_spin_lock_init(&regs->regs[i].lock);
  1583. regs->core_id = -1;
  1584. }
  1585. return regs;
  1586. }
  1587. static int intel_pmu_cpu_prepare(int cpu)
  1588. {
  1589. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1590. if (!(x86_pmu.extra_regs || x86_pmu.lbr_sel_map))
  1591. return NOTIFY_OK;
  1592. cpuc->shared_regs = allocate_shared_regs(cpu);
  1593. if (!cpuc->shared_regs)
  1594. return NOTIFY_BAD;
  1595. return NOTIFY_OK;
  1596. }
  1597. static void intel_pmu_cpu_starting(int cpu)
  1598. {
  1599. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1600. int core_id = topology_core_id(cpu);
  1601. int i;
  1602. init_debug_store_on_cpu(cpu);
  1603. /*
  1604. * Deal with CPUs that don't clear their LBRs on power-up.
  1605. */
  1606. intel_pmu_lbr_reset();
  1607. cpuc->lbr_sel = NULL;
  1608. if (!cpuc->shared_regs)
  1609. return;
  1610. if (!(x86_pmu.er_flags & ERF_NO_HT_SHARING)) {
  1611. for_each_cpu(i, topology_thread_cpumask(cpu)) {
  1612. struct intel_shared_regs *pc;
  1613. pc = per_cpu(cpu_hw_events, i).shared_regs;
  1614. if (pc && pc->core_id == core_id) {
  1615. cpuc->kfree_on_online = cpuc->shared_regs;
  1616. cpuc->shared_regs = pc;
  1617. break;
  1618. }
  1619. }
  1620. cpuc->shared_regs->core_id = core_id;
  1621. cpuc->shared_regs->refcnt++;
  1622. }
  1623. if (x86_pmu.lbr_sel_map)
  1624. cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
  1625. }
  1626. static void intel_pmu_cpu_dying(int cpu)
  1627. {
  1628. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1629. struct intel_shared_regs *pc;
  1630. pc = cpuc->shared_regs;
  1631. if (pc) {
  1632. if (pc->core_id == -1 || --pc->refcnt == 0)
  1633. kfree(pc);
  1634. cpuc->shared_regs = NULL;
  1635. }
  1636. fini_debug_store_on_cpu(cpu);
  1637. }
  1638. static void intel_pmu_flush_branch_stack(void)
  1639. {
  1640. /*
  1641. * Intel LBR does not tag entries with the
  1642. * PID of the current task, then we need to
  1643. * flush it on ctxsw
  1644. * For now, we simply reset it
  1645. */
  1646. if (x86_pmu.lbr_nr)
  1647. intel_pmu_lbr_reset();
  1648. }
  1649. PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
  1650. PMU_FORMAT_ATTR(ldlat, "config1:0-15");
  1651. static struct attribute *intel_arch3_formats_attr[] = {
  1652. &format_attr_event.attr,
  1653. &format_attr_umask.attr,
  1654. &format_attr_edge.attr,
  1655. &format_attr_pc.attr,
  1656. &format_attr_any.attr,
  1657. &format_attr_inv.attr,
  1658. &format_attr_cmask.attr,
  1659. &format_attr_in_tx.attr,
  1660. &format_attr_in_tx_cp.attr,
  1661. &format_attr_offcore_rsp.attr, /* XXX do NHM/WSM + SNB breakout */
  1662. &format_attr_ldlat.attr, /* PEBS load latency */
  1663. NULL,
  1664. };
  1665. static __initconst const struct x86_pmu intel_pmu = {
  1666. .name = "Intel",
  1667. .handle_irq = intel_pmu_handle_irq,
  1668. .disable_all = intel_pmu_disable_all,
  1669. .enable_all = intel_pmu_enable_all,
  1670. .enable = intel_pmu_enable_event,
  1671. .disable = intel_pmu_disable_event,
  1672. .hw_config = intel_pmu_hw_config,
  1673. .schedule_events = x86_schedule_events,
  1674. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1675. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1676. .event_map = intel_pmu_event_map,
  1677. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1678. .apic = 1,
  1679. /*
  1680. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1681. * so we install an artificial 1<<31 period regardless of
  1682. * the generic event period:
  1683. */
  1684. .max_period = (1ULL << 31) - 1,
  1685. .get_event_constraints = intel_get_event_constraints,
  1686. .put_event_constraints = intel_put_event_constraints,
  1687. .pebs_aliases = intel_pebs_aliases_core2,
  1688. .format_attrs = intel_arch3_formats_attr,
  1689. .events_sysfs_show = intel_event_sysfs_show,
  1690. .cpu_prepare = intel_pmu_cpu_prepare,
  1691. .cpu_starting = intel_pmu_cpu_starting,
  1692. .cpu_dying = intel_pmu_cpu_dying,
  1693. .guest_get_msrs = intel_guest_get_msrs,
  1694. .flush_branch_stack = intel_pmu_flush_branch_stack,
  1695. };
  1696. static __init void intel_clovertown_quirk(void)
  1697. {
  1698. /*
  1699. * PEBS is unreliable due to:
  1700. *
  1701. * AJ67 - PEBS may experience CPL leaks
  1702. * AJ68 - PEBS PMI may be delayed by one event
  1703. * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
  1704. * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
  1705. *
  1706. * AJ67 could be worked around by restricting the OS/USR flags.
  1707. * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
  1708. *
  1709. * AJ106 could possibly be worked around by not allowing LBR
  1710. * usage from PEBS, including the fixup.
  1711. * AJ68 could possibly be worked around by always programming
  1712. * a pebs_event_reset[0] value and coping with the lost events.
  1713. *
  1714. * But taken together it might just make sense to not enable PEBS on
  1715. * these chips.
  1716. */
  1717. pr_warn("PEBS disabled due to CPU errata\n");
  1718. x86_pmu.pebs = 0;
  1719. x86_pmu.pebs_constraints = NULL;
  1720. }
  1721. static int intel_snb_pebs_broken(int cpu)
  1722. {
  1723. u32 rev = UINT_MAX; /* default to broken for unknown models */
  1724. switch (cpu_data(cpu).x86_model) {
  1725. case 42: /* SNB */
  1726. rev = 0x28;
  1727. break;
  1728. case 45: /* SNB-EP */
  1729. switch (cpu_data(cpu).x86_mask) {
  1730. case 6: rev = 0x618; break;
  1731. case 7: rev = 0x70c; break;
  1732. }
  1733. }
  1734. return (cpu_data(cpu).microcode < rev);
  1735. }
  1736. static void intel_snb_check_microcode(void)
  1737. {
  1738. int pebs_broken = 0;
  1739. int cpu;
  1740. get_online_cpus();
  1741. for_each_online_cpu(cpu) {
  1742. if ((pebs_broken = intel_snb_pebs_broken(cpu)))
  1743. break;
  1744. }
  1745. put_online_cpus();
  1746. if (pebs_broken == x86_pmu.pebs_broken)
  1747. return;
  1748. /*
  1749. * Serialized by the microcode lock..
  1750. */
  1751. if (x86_pmu.pebs_broken) {
  1752. pr_info("PEBS enabled due to microcode update\n");
  1753. x86_pmu.pebs_broken = 0;
  1754. } else {
  1755. pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
  1756. x86_pmu.pebs_broken = 1;
  1757. }
  1758. }
  1759. static __init void intel_sandybridge_quirk(void)
  1760. {
  1761. x86_pmu.check_microcode = intel_snb_check_microcode;
  1762. intel_snb_check_microcode();
  1763. }
  1764. static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
  1765. { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
  1766. { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
  1767. { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
  1768. { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
  1769. { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
  1770. { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
  1771. { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
  1772. };
  1773. static __init void intel_arch_events_quirk(void)
  1774. {
  1775. int bit;
  1776. /* disable event that reported as not presend by cpuid */
  1777. for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
  1778. intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
  1779. pr_warn("CPUID marked event: \'%s\' unavailable\n",
  1780. intel_arch_events_map[bit].name);
  1781. }
  1782. }
  1783. static __init void intel_nehalem_quirk(void)
  1784. {
  1785. union cpuid10_ebx ebx;
  1786. ebx.full = x86_pmu.events_maskl;
  1787. if (ebx.split.no_branch_misses_retired) {
  1788. /*
  1789. * Erratum AAJ80 detected, we work it around by using
  1790. * the BR_MISP_EXEC.ANY event. This will over-count
  1791. * branch-misses, but it's still much better than the
  1792. * architectural event which is often completely bogus:
  1793. */
  1794. intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
  1795. ebx.split.no_branch_misses_retired = 0;
  1796. x86_pmu.events_maskl = ebx.full;
  1797. pr_info("CPU erratum AAJ80 worked around\n");
  1798. }
  1799. }
  1800. __init int intel_pmu_init(void)
  1801. {
  1802. union cpuid10_edx edx;
  1803. union cpuid10_eax eax;
  1804. union cpuid10_ebx ebx;
  1805. struct event_constraint *c;
  1806. unsigned int unused;
  1807. int version;
  1808. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  1809. switch (boot_cpu_data.x86) {
  1810. case 0x6:
  1811. return p6_pmu_init();
  1812. case 0xb:
  1813. return knc_pmu_init();
  1814. case 0xf:
  1815. return p4_pmu_init();
  1816. }
  1817. return -ENODEV;
  1818. }
  1819. /*
  1820. * Check whether the Architectural PerfMon supports
  1821. * Branch Misses Retired hw_event or not.
  1822. */
  1823. cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
  1824. if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
  1825. return -ENODEV;
  1826. version = eax.split.version_id;
  1827. if (version < 2)
  1828. x86_pmu = core_pmu;
  1829. else
  1830. x86_pmu = intel_pmu;
  1831. x86_pmu.version = version;
  1832. x86_pmu.num_counters = eax.split.num_counters;
  1833. x86_pmu.cntval_bits = eax.split.bit_width;
  1834. x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
  1835. x86_pmu.events_maskl = ebx.full;
  1836. x86_pmu.events_mask_len = eax.split.mask_length;
  1837. x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
  1838. /*
  1839. * Quirk: v2 perfmon does not report fixed-purpose events, so
  1840. * assume at least 3 events:
  1841. */
  1842. if (version > 1)
  1843. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  1844. /*
  1845. * v2 and above have a perf capabilities MSR
  1846. */
  1847. if (version > 1) {
  1848. u64 capabilities;
  1849. rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
  1850. x86_pmu.intel_cap.capabilities = capabilities;
  1851. }
  1852. intel_ds_init();
  1853. x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
  1854. /*
  1855. * Install the hw-cache-events table:
  1856. */
  1857. switch (boot_cpu_data.x86_model) {
  1858. case 14: /* 65 nm core solo/duo, "Yonah" */
  1859. pr_cont("Core events, ");
  1860. break;
  1861. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  1862. x86_add_quirk(intel_clovertown_quirk);
  1863. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  1864. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  1865. case 29: /* six-core 45 nm xeon "Dunnington" */
  1866. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1867. sizeof(hw_cache_event_ids));
  1868. intel_pmu_lbr_init_core();
  1869. x86_pmu.event_constraints = intel_core2_event_constraints;
  1870. x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
  1871. pr_cont("Core2 events, ");
  1872. break;
  1873. case 26: /* 45 nm nehalem, "Bloomfield" */
  1874. case 30: /* 45 nm nehalem, "Lynnfield" */
  1875. case 46: /* 45 nm nehalem-ex, "Beckton" */
  1876. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1877. sizeof(hw_cache_event_ids));
  1878. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  1879. sizeof(hw_cache_extra_regs));
  1880. intel_pmu_lbr_init_nhm();
  1881. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  1882. x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
  1883. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  1884. x86_pmu.extra_regs = intel_nehalem_extra_regs;
  1885. x86_pmu.cpu_events = nhm_events_attrs;
  1886. /* UOPS_ISSUED.STALLED_CYCLES */
  1887. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1888. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1889. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  1890. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1891. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  1892. x86_add_quirk(intel_nehalem_quirk);
  1893. pr_cont("Nehalem events, ");
  1894. break;
  1895. case 28: /* Atom */
  1896. case 38: /* Lincroft */
  1897. case 39: /* Penwell */
  1898. case 53: /* Cloverview */
  1899. case 54: /* Cedarview */
  1900. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1901. sizeof(hw_cache_event_ids));
  1902. intel_pmu_lbr_init_atom();
  1903. x86_pmu.event_constraints = intel_gen_event_constraints;
  1904. x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
  1905. pr_cont("Atom events, ");
  1906. break;
  1907. case 37: /* 32 nm nehalem, "Clarkdale" */
  1908. case 44: /* 32 nm nehalem, "Gulftown" */
  1909. case 47: /* 32 nm Xeon E7 */
  1910. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  1911. sizeof(hw_cache_event_ids));
  1912. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  1913. sizeof(hw_cache_extra_regs));
  1914. intel_pmu_lbr_init_nhm();
  1915. x86_pmu.event_constraints = intel_westmere_event_constraints;
  1916. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  1917. x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
  1918. x86_pmu.extra_regs = intel_westmere_extra_regs;
  1919. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1920. x86_pmu.cpu_events = nhm_events_attrs;
  1921. /* UOPS_ISSUED.STALLED_CYCLES */
  1922. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1923. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1924. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  1925. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1926. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  1927. pr_cont("Westmere events, ");
  1928. break;
  1929. case 42: /* SandyBridge */
  1930. case 45: /* SandyBridge, "Romely-EP" */
  1931. x86_add_quirk(intel_sandybridge_quirk);
  1932. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  1933. sizeof(hw_cache_event_ids));
  1934. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  1935. sizeof(hw_cache_extra_regs));
  1936. intel_pmu_lbr_init_snb();
  1937. x86_pmu.event_constraints = intel_snb_event_constraints;
  1938. x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
  1939. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  1940. if (boot_cpu_data.x86_model == 45)
  1941. x86_pmu.extra_regs = intel_snbep_extra_regs;
  1942. else
  1943. x86_pmu.extra_regs = intel_snb_extra_regs;
  1944. /* all extra regs are per-cpu when HT is on */
  1945. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1946. x86_pmu.er_flags |= ERF_NO_HT_SHARING;
  1947. x86_pmu.cpu_events = snb_events_attrs;
  1948. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  1949. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1950. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1951. /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
  1952. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1953. X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
  1954. pr_cont("SandyBridge events, ");
  1955. break;
  1956. case 58: /* IvyBridge */
  1957. case 62: /* IvyBridge EP */
  1958. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  1959. sizeof(hw_cache_event_ids));
  1960. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  1961. sizeof(hw_cache_extra_regs));
  1962. intel_pmu_lbr_init_snb();
  1963. x86_pmu.event_constraints = intel_ivb_event_constraints;
  1964. x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
  1965. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  1966. if (boot_cpu_data.x86_model == 62)
  1967. x86_pmu.extra_regs = intel_snbep_extra_regs;
  1968. else
  1969. x86_pmu.extra_regs = intel_snb_extra_regs;
  1970. /* all extra regs are per-cpu when HT is on */
  1971. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1972. x86_pmu.er_flags |= ERF_NO_HT_SHARING;
  1973. x86_pmu.cpu_events = snb_events_attrs;
  1974. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  1975. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1976. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1977. pr_cont("IvyBridge events, ");
  1978. break;
  1979. case 60: /* Haswell Client */
  1980. case 70:
  1981. case 71:
  1982. case 63:
  1983. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  1984. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  1985. intel_pmu_lbr_init_snb();
  1986. x86_pmu.event_constraints = intel_hsw_event_constraints;
  1987. x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
  1988. x86_pmu.extra_regs = intel_snb_extra_regs;
  1989. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  1990. /* all extra regs are per-cpu when HT is on */
  1991. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1992. x86_pmu.er_flags |= ERF_NO_HT_SHARING;
  1993. x86_pmu.hw_config = hsw_hw_config;
  1994. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  1995. pr_cont("Haswell events, ");
  1996. break;
  1997. default:
  1998. switch (x86_pmu.version) {
  1999. case 1:
  2000. x86_pmu.event_constraints = intel_v1_event_constraints;
  2001. pr_cont("generic architected perfmon v1, ");
  2002. break;
  2003. default:
  2004. /*
  2005. * default constraints for v2 and up
  2006. */
  2007. x86_pmu.event_constraints = intel_gen_event_constraints;
  2008. pr_cont("generic architected perfmon, ");
  2009. break;
  2010. }
  2011. }
  2012. if (x86_pmu.num_counters > INTEL_PMC_MAX_GENERIC) {
  2013. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  2014. x86_pmu.num_counters, INTEL_PMC_MAX_GENERIC);
  2015. x86_pmu.num_counters = INTEL_PMC_MAX_GENERIC;
  2016. }
  2017. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  2018. if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED) {
  2019. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  2020. x86_pmu.num_counters_fixed, INTEL_PMC_MAX_FIXED);
  2021. x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
  2022. }
  2023. x86_pmu.intel_ctrl |=
  2024. ((1LL << x86_pmu.num_counters_fixed)-1) << INTEL_PMC_IDX_FIXED;
  2025. if (x86_pmu.event_constraints) {
  2026. /*
  2027. * event on fixed counter2 (REF_CYCLES) only works on this
  2028. * counter, so do not extend mask to generic counters
  2029. */
  2030. for_each_event_constraint(c, x86_pmu.event_constraints) {
  2031. if (c->cmask != FIXED_EVENT_FLAGS
  2032. || c->idxmsk64 == INTEL_PMC_MSK_FIXED_REF_CYCLES) {
  2033. continue;
  2034. }
  2035. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  2036. c->weight += x86_pmu.num_counters;
  2037. }
  2038. }
  2039. return 0;
  2040. }