perf_event_p4.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Netburst Performance Events (P4, old Xeon)
  3. *
  4. * Copyright (C) 2010 Parallels, Inc., Cyrill Gorcunov <gorcunov@openvz.org>
  5. * Copyright (C) 2010 Intel Corporation, Lin Ming <ming.m.lin@intel.com>
  6. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/perf_event.h>
  10. #include <asm/perf_event_p4.h>
  11. #include <asm/hardirq.h>
  12. #include <asm/apic.h>
  13. #include "perf_event.h"
  14. #define P4_CNTR_LIMIT 3
  15. /*
  16. * array indices: 0,1 - HT threads, used with HT enabled cpu
  17. */
  18. struct p4_event_bind {
  19. unsigned int opcode; /* Event code and ESCR selector */
  20. unsigned int escr_msr[2]; /* ESCR MSR for this event */
  21. unsigned int escr_emask; /* valid ESCR EventMask bits */
  22. unsigned int shared; /* event is shared across threads */
  23. char cntr[2][P4_CNTR_LIMIT]; /* counter index (offset), -1 on abscence */
  24. };
  25. struct p4_pebs_bind {
  26. unsigned int metric_pebs;
  27. unsigned int metric_vert;
  28. };
  29. /* it sets P4_PEBS_ENABLE_UOP_TAG as well */
  30. #define P4_GEN_PEBS_BIND(name, pebs, vert) \
  31. [P4_PEBS_METRIC__##name] = { \
  32. .metric_pebs = pebs | P4_PEBS_ENABLE_UOP_TAG, \
  33. .metric_vert = vert, \
  34. }
  35. /*
  36. * note we have P4_PEBS_ENABLE_UOP_TAG always set here
  37. *
  38. * it's needed for mapping P4_PEBS_CONFIG_METRIC_MASK bits of
  39. * event configuration to find out which values are to be
  40. * written into MSR_IA32_PEBS_ENABLE and MSR_P4_PEBS_MATRIX_VERT
  41. * resgisters
  42. */
  43. static struct p4_pebs_bind p4_pebs_bind_map[] = {
  44. P4_GEN_PEBS_BIND(1stl_cache_load_miss_retired, 0x0000001, 0x0000001),
  45. P4_GEN_PEBS_BIND(2ndl_cache_load_miss_retired, 0x0000002, 0x0000001),
  46. P4_GEN_PEBS_BIND(dtlb_load_miss_retired, 0x0000004, 0x0000001),
  47. P4_GEN_PEBS_BIND(dtlb_store_miss_retired, 0x0000004, 0x0000002),
  48. P4_GEN_PEBS_BIND(dtlb_all_miss_retired, 0x0000004, 0x0000003),
  49. P4_GEN_PEBS_BIND(tagged_mispred_branch, 0x0018000, 0x0000010),
  50. P4_GEN_PEBS_BIND(mob_load_replay_retired, 0x0000200, 0x0000001),
  51. P4_GEN_PEBS_BIND(split_load_retired, 0x0000400, 0x0000001),
  52. P4_GEN_PEBS_BIND(split_store_retired, 0x0000400, 0x0000002),
  53. };
  54. /*
  55. * Note that we don't use CCCR1 here, there is an
  56. * exception for P4_BSQ_ALLOCATION but we just have
  57. * no workaround
  58. *
  59. * consider this binding as resources which particular
  60. * event may borrow, it doesn't contain EventMask,
  61. * Tags and friends -- they are left to a caller
  62. */
  63. static struct p4_event_bind p4_event_bind_map[] = {
  64. [P4_EVENT_TC_DELIVER_MODE] = {
  65. .opcode = P4_OPCODE(P4_EVENT_TC_DELIVER_MODE),
  66. .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
  67. .escr_emask =
  68. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DD) |
  69. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DB) |
  70. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DI) |
  71. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BD) |
  72. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BB) |
  73. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BI) |
  74. P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, ID),
  75. .shared = 1,
  76. .cntr = { {4, 5, -1}, {6, 7, -1} },
  77. },
  78. [P4_EVENT_BPU_FETCH_REQUEST] = {
  79. .opcode = P4_OPCODE(P4_EVENT_BPU_FETCH_REQUEST),
  80. .escr_msr = { MSR_P4_BPU_ESCR0, MSR_P4_BPU_ESCR1 },
  81. .escr_emask =
  82. P4_ESCR_EMASK_BIT(P4_EVENT_BPU_FETCH_REQUEST, TCMISS),
  83. .cntr = { {0, -1, -1}, {2, -1, -1} },
  84. },
  85. [P4_EVENT_ITLB_REFERENCE] = {
  86. .opcode = P4_OPCODE(P4_EVENT_ITLB_REFERENCE),
  87. .escr_msr = { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
  88. .escr_emask =
  89. P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, HIT) |
  90. P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, MISS) |
  91. P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, HIT_UK),
  92. .cntr = { {0, -1, -1}, {2, -1, -1} },
  93. },
  94. [P4_EVENT_MEMORY_CANCEL] = {
  95. .opcode = P4_OPCODE(P4_EVENT_MEMORY_CANCEL),
  96. .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
  97. .escr_emask =
  98. P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_CANCEL, ST_RB_FULL) |
  99. P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_CANCEL, 64K_CONF),
  100. .cntr = { {8, 9, -1}, {10, 11, -1} },
  101. },
  102. [P4_EVENT_MEMORY_COMPLETE] = {
  103. .opcode = P4_OPCODE(P4_EVENT_MEMORY_COMPLETE),
  104. .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
  105. .escr_emask =
  106. P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_COMPLETE, LSC) |
  107. P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_COMPLETE, SSC),
  108. .cntr = { {8, 9, -1}, {10, 11, -1} },
  109. },
  110. [P4_EVENT_LOAD_PORT_REPLAY] = {
  111. .opcode = P4_OPCODE(P4_EVENT_LOAD_PORT_REPLAY),
  112. .escr_msr = { MSR_P4_SAAT_ESCR0, MSR_P4_SAAT_ESCR1 },
  113. .escr_emask =
  114. P4_ESCR_EMASK_BIT(P4_EVENT_LOAD_PORT_REPLAY, SPLIT_LD),
  115. .cntr = { {8, 9, -1}, {10, 11, -1} },
  116. },
  117. [P4_EVENT_STORE_PORT_REPLAY] = {
  118. .opcode = P4_OPCODE(P4_EVENT_STORE_PORT_REPLAY),
  119. .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
  120. .escr_emask =
  121. P4_ESCR_EMASK_BIT(P4_EVENT_STORE_PORT_REPLAY, SPLIT_ST),
  122. .cntr = { {8, 9, -1}, {10, 11, -1} },
  123. },
  124. [P4_EVENT_MOB_LOAD_REPLAY] = {
  125. .opcode = P4_OPCODE(P4_EVENT_MOB_LOAD_REPLAY),
  126. .escr_msr = { MSR_P4_MOB_ESCR0, MSR_P4_MOB_ESCR1 },
  127. .escr_emask =
  128. P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, NO_STA) |
  129. P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, NO_STD) |
  130. P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, PARTIAL_DATA) |
  131. P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, UNALGN_ADDR),
  132. .cntr = { {0, -1, -1}, {2, -1, -1} },
  133. },
  134. [P4_EVENT_PAGE_WALK_TYPE] = {
  135. .opcode = P4_OPCODE(P4_EVENT_PAGE_WALK_TYPE),
  136. .escr_msr = { MSR_P4_PMH_ESCR0, MSR_P4_PMH_ESCR1 },
  137. .escr_emask =
  138. P4_ESCR_EMASK_BIT(P4_EVENT_PAGE_WALK_TYPE, DTMISS) |
  139. P4_ESCR_EMASK_BIT(P4_EVENT_PAGE_WALK_TYPE, ITMISS),
  140. .shared = 1,
  141. .cntr = { {0, -1, -1}, {2, -1, -1} },
  142. },
  143. [P4_EVENT_BSQ_CACHE_REFERENCE] = {
  144. .opcode = P4_OPCODE(P4_EVENT_BSQ_CACHE_REFERENCE),
  145. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  146. .escr_emask =
  147. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
  148. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
  149. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
  150. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
  151. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
  152. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM) |
  153. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
  154. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
  155. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS),
  156. .cntr = { {0, -1, -1}, {2, -1, -1} },
  157. },
  158. [P4_EVENT_IOQ_ALLOCATION] = {
  159. .opcode = P4_OPCODE(P4_EVENT_IOQ_ALLOCATION),
  160. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  161. .escr_emask =
  162. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, DEFAULT) |
  163. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, ALL_READ) |
  164. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, ALL_WRITE) |
  165. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_UC) |
  166. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WC) |
  167. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WT) |
  168. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WP) |
  169. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WB) |
  170. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, OWN) |
  171. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, OTHER) |
  172. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, PREFETCH),
  173. .cntr = { {0, -1, -1}, {2, -1, -1} },
  174. },
  175. [P4_EVENT_IOQ_ACTIVE_ENTRIES] = { /* shared ESCR */
  176. .opcode = P4_OPCODE(P4_EVENT_IOQ_ACTIVE_ENTRIES),
  177. .escr_msr = { MSR_P4_FSB_ESCR1, MSR_P4_FSB_ESCR1 },
  178. .escr_emask =
  179. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, DEFAULT) |
  180. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, ALL_READ) |
  181. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, ALL_WRITE) |
  182. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_UC) |
  183. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WC) |
  184. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WT) |
  185. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WP) |
  186. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WB) |
  187. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, OWN) |
  188. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, OTHER) |
  189. P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, PREFETCH),
  190. .cntr = { {2, -1, -1}, {3, -1, -1} },
  191. },
  192. [P4_EVENT_FSB_DATA_ACTIVITY] = {
  193. .opcode = P4_OPCODE(P4_EVENT_FSB_DATA_ACTIVITY),
  194. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  195. .escr_emask =
  196. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
  197. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN) |
  198. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OTHER) |
  199. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_DRV) |
  200. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_OWN) |
  201. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_OTHER),
  202. .shared = 1,
  203. .cntr = { {0, -1, -1}, {2, -1, -1} },
  204. },
  205. [P4_EVENT_BSQ_ALLOCATION] = { /* shared ESCR, broken CCCR1 */
  206. .opcode = P4_OPCODE(P4_EVENT_BSQ_ALLOCATION),
  207. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR0 },
  208. .escr_emask =
  209. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_TYPE0) |
  210. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_TYPE1) |
  211. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LEN0) |
  212. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LEN1) |
  213. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_IO_TYPE) |
  214. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LOCK_TYPE) |
  215. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_CACHE_TYPE) |
  216. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_SPLIT_TYPE) |
  217. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_DEM_TYPE) |
  218. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_ORD_TYPE) |
  219. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE0) |
  220. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE1) |
  221. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE2),
  222. .cntr = { {0, -1, -1}, {1, -1, -1} },
  223. },
  224. [P4_EVENT_BSQ_ACTIVE_ENTRIES] = { /* shared ESCR */
  225. .opcode = P4_OPCODE(P4_EVENT_BSQ_ACTIVE_ENTRIES),
  226. .escr_msr = { MSR_P4_BSU_ESCR1 , MSR_P4_BSU_ESCR1 },
  227. .escr_emask =
  228. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_TYPE0) |
  229. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_TYPE1) |
  230. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LEN0) |
  231. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LEN1) |
  232. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_IO_TYPE) |
  233. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LOCK_TYPE) |
  234. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_CACHE_TYPE) |
  235. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_SPLIT_TYPE) |
  236. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_DEM_TYPE) |
  237. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_ORD_TYPE) |
  238. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE0) |
  239. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE1) |
  240. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE2),
  241. .cntr = { {2, -1, -1}, {3, -1, -1} },
  242. },
  243. [P4_EVENT_SSE_INPUT_ASSIST] = {
  244. .opcode = P4_OPCODE(P4_EVENT_SSE_INPUT_ASSIST),
  245. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  246. .escr_emask =
  247. P4_ESCR_EMASK_BIT(P4_EVENT_SSE_INPUT_ASSIST, ALL),
  248. .shared = 1,
  249. .cntr = { {8, 9, -1}, {10, 11, -1} },
  250. },
  251. [P4_EVENT_PACKED_SP_UOP] = {
  252. .opcode = P4_OPCODE(P4_EVENT_PACKED_SP_UOP),
  253. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  254. .escr_emask =
  255. P4_ESCR_EMASK_BIT(P4_EVENT_PACKED_SP_UOP, ALL),
  256. .shared = 1,
  257. .cntr = { {8, 9, -1}, {10, 11, -1} },
  258. },
  259. [P4_EVENT_PACKED_DP_UOP] = {
  260. .opcode = P4_OPCODE(P4_EVENT_PACKED_DP_UOP),
  261. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  262. .escr_emask =
  263. P4_ESCR_EMASK_BIT(P4_EVENT_PACKED_DP_UOP, ALL),
  264. .shared = 1,
  265. .cntr = { {8, 9, -1}, {10, 11, -1} },
  266. },
  267. [P4_EVENT_SCALAR_SP_UOP] = {
  268. .opcode = P4_OPCODE(P4_EVENT_SCALAR_SP_UOP),
  269. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  270. .escr_emask =
  271. P4_ESCR_EMASK_BIT(P4_EVENT_SCALAR_SP_UOP, ALL),
  272. .shared = 1,
  273. .cntr = { {8, 9, -1}, {10, 11, -1} },
  274. },
  275. [P4_EVENT_SCALAR_DP_UOP] = {
  276. .opcode = P4_OPCODE(P4_EVENT_SCALAR_DP_UOP),
  277. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  278. .escr_emask =
  279. P4_ESCR_EMASK_BIT(P4_EVENT_SCALAR_DP_UOP, ALL),
  280. .shared = 1,
  281. .cntr = { {8, 9, -1}, {10, 11, -1} },
  282. },
  283. [P4_EVENT_64BIT_MMX_UOP] = {
  284. .opcode = P4_OPCODE(P4_EVENT_64BIT_MMX_UOP),
  285. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  286. .escr_emask =
  287. P4_ESCR_EMASK_BIT(P4_EVENT_64BIT_MMX_UOP, ALL),
  288. .shared = 1,
  289. .cntr = { {8, 9, -1}, {10, 11, -1} },
  290. },
  291. [P4_EVENT_128BIT_MMX_UOP] = {
  292. .opcode = P4_OPCODE(P4_EVENT_128BIT_MMX_UOP),
  293. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  294. .escr_emask =
  295. P4_ESCR_EMASK_BIT(P4_EVENT_128BIT_MMX_UOP, ALL),
  296. .shared = 1,
  297. .cntr = { {8, 9, -1}, {10, 11, -1} },
  298. },
  299. [P4_EVENT_X87_FP_UOP] = {
  300. .opcode = P4_OPCODE(P4_EVENT_X87_FP_UOP),
  301. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  302. .escr_emask =
  303. P4_ESCR_EMASK_BIT(P4_EVENT_X87_FP_UOP, ALL),
  304. .shared = 1,
  305. .cntr = { {8, 9, -1}, {10, 11, -1} },
  306. },
  307. [P4_EVENT_TC_MISC] = {
  308. .opcode = P4_OPCODE(P4_EVENT_TC_MISC),
  309. .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
  310. .escr_emask =
  311. P4_ESCR_EMASK_BIT(P4_EVENT_TC_MISC, FLUSH),
  312. .cntr = { {4, 5, -1}, {6, 7, -1} },
  313. },
  314. [P4_EVENT_GLOBAL_POWER_EVENTS] = {
  315. .opcode = P4_OPCODE(P4_EVENT_GLOBAL_POWER_EVENTS),
  316. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  317. .escr_emask =
  318. P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING),
  319. .cntr = { {0, -1, -1}, {2, -1, -1} },
  320. },
  321. [P4_EVENT_TC_MS_XFER] = {
  322. .opcode = P4_OPCODE(P4_EVENT_TC_MS_XFER),
  323. .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
  324. .escr_emask =
  325. P4_ESCR_EMASK_BIT(P4_EVENT_TC_MS_XFER, CISC),
  326. .cntr = { {4, 5, -1}, {6, 7, -1} },
  327. },
  328. [P4_EVENT_UOP_QUEUE_WRITES] = {
  329. .opcode = P4_OPCODE(P4_EVENT_UOP_QUEUE_WRITES),
  330. .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
  331. .escr_emask =
  332. P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_TC_BUILD) |
  333. P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_TC_DELIVER) |
  334. P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_ROM),
  335. .cntr = { {4, 5, -1}, {6, 7, -1} },
  336. },
  337. [P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE] = {
  338. .opcode = P4_OPCODE(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE),
  339. .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR0 },
  340. .escr_emask =
  341. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, CONDITIONAL) |
  342. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, CALL) |
  343. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, RETURN) |
  344. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, INDIRECT),
  345. .cntr = { {4, 5, -1}, {6, 7, -1} },
  346. },
  347. [P4_EVENT_RETIRED_BRANCH_TYPE] = {
  348. .opcode = P4_OPCODE(P4_EVENT_RETIRED_BRANCH_TYPE),
  349. .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR1 },
  350. .escr_emask =
  351. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
  352. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
  353. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
  354. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT),
  355. .cntr = { {4, 5, -1}, {6, 7, -1} },
  356. },
  357. [P4_EVENT_RESOURCE_STALL] = {
  358. .opcode = P4_OPCODE(P4_EVENT_RESOURCE_STALL),
  359. .escr_msr = { MSR_P4_ALF_ESCR0, MSR_P4_ALF_ESCR1 },
  360. .escr_emask =
  361. P4_ESCR_EMASK_BIT(P4_EVENT_RESOURCE_STALL, SBFULL),
  362. .cntr = { {12, 13, 16}, {14, 15, 17} },
  363. },
  364. [P4_EVENT_WC_BUFFER] = {
  365. .opcode = P4_OPCODE(P4_EVENT_WC_BUFFER),
  366. .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
  367. .escr_emask =
  368. P4_ESCR_EMASK_BIT(P4_EVENT_WC_BUFFER, WCB_EVICTS) |
  369. P4_ESCR_EMASK_BIT(P4_EVENT_WC_BUFFER, WCB_FULL_EVICTS),
  370. .shared = 1,
  371. .cntr = { {8, 9, -1}, {10, 11, -1} },
  372. },
  373. [P4_EVENT_B2B_CYCLES] = {
  374. .opcode = P4_OPCODE(P4_EVENT_B2B_CYCLES),
  375. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  376. .escr_emask = 0,
  377. .cntr = { {0, -1, -1}, {2, -1, -1} },
  378. },
  379. [P4_EVENT_BNR] = {
  380. .opcode = P4_OPCODE(P4_EVENT_BNR),
  381. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  382. .escr_emask = 0,
  383. .cntr = { {0, -1, -1}, {2, -1, -1} },
  384. },
  385. [P4_EVENT_SNOOP] = {
  386. .opcode = P4_OPCODE(P4_EVENT_SNOOP),
  387. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  388. .escr_emask = 0,
  389. .cntr = { {0, -1, -1}, {2, -1, -1} },
  390. },
  391. [P4_EVENT_RESPONSE] = {
  392. .opcode = P4_OPCODE(P4_EVENT_RESPONSE),
  393. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  394. .escr_emask = 0,
  395. .cntr = { {0, -1, -1}, {2, -1, -1} },
  396. },
  397. [P4_EVENT_FRONT_END_EVENT] = {
  398. .opcode = P4_OPCODE(P4_EVENT_FRONT_END_EVENT),
  399. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  400. .escr_emask =
  401. P4_ESCR_EMASK_BIT(P4_EVENT_FRONT_END_EVENT, NBOGUS) |
  402. P4_ESCR_EMASK_BIT(P4_EVENT_FRONT_END_EVENT, BOGUS),
  403. .cntr = { {12, 13, 16}, {14, 15, 17} },
  404. },
  405. [P4_EVENT_EXECUTION_EVENT] = {
  406. .opcode = P4_OPCODE(P4_EVENT_EXECUTION_EVENT),
  407. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  408. .escr_emask =
  409. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0) |
  410. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1) |
  411. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2) |
  412. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3) |
  413. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) |
  414. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) |
  415. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) |
  416. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3),
  417. .cntr = { {12, 13, 16}, {14, 15, 17} },
  418. },
  419. [P4_EVENT_REPLAY_EVENT] = {
  420. .opcode = P4_OPCODE(P4_EVENT_REPLAY_EVENT),
  421. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  422. .escr_emask =
  423. P4_ESCR_EMASK_BIT(P4_EVENT_REPLAY_EVENT, NBOGUS) |
  424. P4_ESCR_EMASK_BIT(P4_EVENT_REPLAY_EVENT, BOGUS),
  425. .cntr = { {12, 13, 16}, {14, 15, 17} },
  426. },
  427. [P4_EVENT_INSTR_RETIRED] = {
  428. .opcode = P4_OPCODE(P4_EVENT_INSTR_RETIRED),
  429. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  430. .escr_emask =
  431. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
  432. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSTAG) |
  433. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG) |
  434. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSTAG),
  435. .cntr = { {12, 13, 16}, {14, 15, 17} },
  436. },
  437. [P4_EVENT_UOPS_RETIRED] = {
  438. .opcode = P4_OPCODE(P4_EVENT_UOPS_RETIRED),
  439. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  440. .escr_emask =
  441. P4_ESCR_EMASK_BIT(P4_EVENT_UOPS_RETIRED, NBOGUS) |
  442. P4_ESCR_EMASK_BIT(P4_EVENT_UOPS_RETIRED, BOGUS),
  443. .cntr = { {12, 13, 16}, {14, 15, 17} },
  444. },
  445. [P4_EVENT_UOP_TYPE] = {
  446. .opcode = P4_OPCODE(P4_EVENT_UOP_TYPE),
  447. .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
  448. .escr_emask =
  449. P4_ESCR_EMASK_BIT(P4_EVENT_UOP_TYPE, TAGLOADS) |
  450. P4_ESCR_EMASK_BIT(P4_EVENT_UOP_TYPE, TAGSTORES),
  451. .cntr = { {12, 13, 16}, {14, 15, 17} },
  452. },
  453. [P4_EVENT_BRANCH_RETIRED] = {
  454. .opcode = P4_OPCODE(P4_EVENT_BRANCH_RETIRED),
  455. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  456. .escr_emask =
  457. P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMNP) |
  458. P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMNM) |
  459. P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMTP) |
  460. P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMTM),
  461. .cntr = { {12, 13, 16}, {14, 15, 17} },
  462. },
  463. [P4_EVENT_MISPRED_BRANCH_RETIRED] = {
  464. .opcode = P4_OPCODE(P4_EVENT_MISPRED_BRANCH_RETIRED),
  465. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  466. .escr_emask =
  467. P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS),
  468. .cntr = { {12, 13, 16}, {14, 15, 17} },
  469. },
  470. [P4_EVENT_X87_ASSIST] = {
  471. .opcode = P4_OPCODE(P4_EVENT_X87_ASSIST),
  472. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  473. .escr_emask =
  474. P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, FPSU) |
  475. P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, FPSO) |
  476. P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, POAO) |
  477. P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, POAU) |
  478. P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, PREA),
  479. .cntr = { {12, 13, 16}, {14, 15, 17} },
  480. },
  481. [P4_EVENT_MACHINE_CLEAR] = {
  482. .opcode = P4_OPCODE(P4_EVENT_MACHINE_CLEAR),
  483. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  484. .escr_emask =
  485. P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, CLEAR) |
  486. P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, MOCLEAR) |
  487. P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, SMCLEAR),
  488. .cntr = { {12, 13, 16}, {14, 15, 17} },
  489. },
  490. [P4_EVENT_INSTR_COMPLETED] = {
  491. .opcode = P4_OPCODE(P4_EVENT_INSTR_COMPLETED),
  492. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  493. .escr_emask =
  494. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_COMPLETED, NBOGUS) |
  495. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_COMPLETED, BOGUS),
  496. .cntr = { {12, 13, 16}, {14, 15, 17} },
  497. },
  498. };
  499. #define P4_GEN_CACHE_EVENT(event, bit, metric) \
  500. p4_config_pack_escr(P4_ESCR_EVENT(event) | \
  501. P4_ESCR_EMASK_BIT(event, bit)) | \
  502. p4_config_pack_cccr(metric | \
  503. P4_CCCR_ESEL(P4_OPCODE_ESEL(P4_OPCODE(event))))
  504. static __initconst const u64 p4_hw_cache_event_ids
  505. [PERF_COUNT_HW_CACHE_MAX]
  506. [PERF_COUNT_HW_CACHE_OP_MAX]
  507. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  508. {
  509. [ C(L1D ) ] = {
  510. [ C(OP_READ) ] = {
  511. [ C(RESULT_ACCESS) ] = 0x0,
  512. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  513. P4_PEBS_METRIC__1stl_cache_load_miss_retired),
  514. },
  515. },
  516. [ C(LL ) ] = {
  517. [ C(OP_READ) ] = {
  518. [ C(RESULT_ACCESS) ] = 0x0,
  519. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  520. P4_PEBS_METRIC__2ndl_cache_load_miss_retired),
  521. },
  522. },
  523. [ C(DTLB) ] = {
  524. [ C(OP_READ) ] = {
  525. [ C(RESULT_ACCESS) ] = 0x0,
  526. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  527. P4_PEBS_METRIC__dtlb_load_miss_retired),
  528. },
  529. [ C(OP_WRITE) ] = {
  530. [ C(RESULT_ACCESS) ] = 0x0,
  531. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  532. P4_PEBS_METRIC__dtlb_store_miss_retired),
  533. },
  534. },
  535. [ C(ITLB) ] = {
  536. [ C(OP_READ) ] = {
  537. [ C(RESULT_ACCESS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, HIT,
  538. P4_PEBS_METRIC__none),
  539. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, MISS,
  540. P4_PEBS_METRIC__none),
  541. },
  542. [ C(OP_WRITE) ] = {
  543. [ C(RESULT_ACCESS) ] = -1,
  544. [ C(RESULT_MISS) ] = -1,
  545. },
  546. [ C(OP_PREFETCH) ] = {
  547. [ C(RESULT_ACCESS) ] = -1,
  548. [ C(RESULT_MISS) ] = -1,
  549. },
  550. },
  551. [ C(NODE) ] = {
  552. [ C(OP_READ) ] = {
  553. [ C(RESULT_ACCESS) ] = -1,
  554. [ C(RESULT_MISS) ] = -1,
  555. },
  556. [ C(OP_WRITE) ] = {
  557. [ C(RESULT_ACCESS) ] = -1,
  558. [ C(RESULT_MISS) ] = -1,
  559. },
  560. [ C(OP_PREFETCH) ] = {
  561. [ C(RESULT_ACCESS) ] = -1,
  562. [ C(RESULT_MISS) ] = -1,
  563. },
  564. },
  565. };
  566. /*
  567. * Because of Netburst being quite restricted in how many
  568. * identical events may run simultaneously, we introduce event aliases,
  569. * ie the different events which have the same functionality but
  570. * utilize non-intersected resources (ESCR/CCCR/counter registers).
  571. *
  572. * This allow us to relax restrictions a bit and run two or more
  573. * identical events together.
  574. *
  575. * Never set any custom internal bits such as P4_CONFIG_HT,
  576. * P4_CONFIG_ALIASABLE or bits for P4_PEBS_METRIC, they are
  577. * either up to date automatically or not applicable at all.
  578. */
  579. struct p4_event_alias {
  580. u64 original;
  581. u64 alternative;
  582. } p4_event_aliases[] = {
  583. {
  584. /*
  585. * Non-halted cycles can be substituted with non-sleeping cycles (see
  586. * Intel SDM Vol3b for details). We need this alias to be able
  587. * to run nmi-watchdog and 'perf top' (or any other user space tool
  588. * which is interested in running PERF_COUNT_HW_CPU_CYCLES)
  589. * simultaneously.
  590. */
  591. .original =
  592. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
  593. P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)),
  594. .alternative =
  595. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_EXECUTION_EVENT) |
  596. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0)|
  597. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1)|
  598. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2)|
  599. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3)|
  600. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) |
  601. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) |
  602. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) |
  603. P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3))|
  604. p4_config_pack_cccr(P4_CCCR_THRESHOLD(15) | P4_CCCR_COMPLEMENT |
  605. P4_CCCR_COMPARE),
  606. },
  607. };
  608. static u64 p4_get_alias_event(u64 config)
  609. {
  610. u64 config_match;
  611. int i;
  612. /*
  613. * Only event with special mark is allowed,
  614. * we're to be sure it didn't come as malformed
  615. * RAW event.
  616. */
  617. if (!(config & P4_CONFIG_ALIASABLE))
  618. return 0;
  619. config_match = config & P4_CONFIG_EVENT_ALIAS_MASK;
  620. for (i = 0; i < ARRAY_SIZE(p4_event_aliases); i++) {
  621. if (config_match == p4_event_aliases[i].original) {
  622. config_match = p4_event_aliases[i].alternative;
  623. break;
  624. } else if (config_match == p4_event_aliases[i].alternative) {
  625. config_match = p4_event_aliases[i].original;
  626. break;
  627. }
  628. }
  629. if (i >= ARRAY_SIZE(p4_event_aliases))
  630. return 0;
  631. return config_match | (config & P4_CONFIG_EVENT_ALIAS_IMMUTABLE_BITS);
  632. }
  633. static u64 p4_general_events[PERF_COUNT_HW_MAX] = {
  634. /* non-halted CPU clocks */
  635. [PERF_COUNT_HW_CPU_CYCLES] =
  636. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
  637. P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)) |
  638. P4_CONFIG_ALIASABLE,
  639. /*
  640. * retired instructions
  641. * in a sake of simplicity we don't use the FSB tagging
  642. */
  643. [PERF_COUNT_HW_INSTRUCTIONS] =
  644. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_INSTR_RETIRED) |
  645. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
  646. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG)),
  647. /* cache hits */
  648. [PERF_COUNT_HW_CACHE_REFERENCES] =
  649. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
  650. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
  651. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
  652. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
  653. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
  654. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
  655. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM)),
  656. /* cache misses */
  657. [PERF_COUNT_HW_CACHE_MISSES] =
  658. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
  659. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
  660. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
  661. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS)),
  662. /* branch instructions retired */
  663. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =
  664. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_RETIRED_BRANCH_TYPE) |
  665. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
  666. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
  667. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
  668. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT)),
  669. /* mispredicted branches retired */
  670. [PERF_COUNT_HW_BRANCH_MISSES] =
  671. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_MISPRED_BRANCH_RETIRED) |
  672. P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS)),
  673. /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
  674. [PERF_COUNT_HW_BUS_CYCLES] =
  675. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_FSB_DATA_ACTIVITY) |
  676. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
  677. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN)) |
  678. p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
  679. };
  680. static struct p4_event_bind *p4_config_get_bind(u64 config)
  681. {
  682. unsigned int evnt = p4_config_unpack_event(config);
  683. struct p4_event_bind *bind = NULL;
  684. if (evnt < ARRAY_SIZE(p4_event_bind_map))
  685. bind = &p4_event_bind_map[evnt];
  686. return bind;
  687. }
  688. static u64 p4_pmu_event_map(int hw_event)
  689. {
  690. struct p4_event_bind *bind;
  691. unsigned int esel;
  692. u64 config;
  693. config = p4_general_events[hw_event];
  694. bind = p4_config_get_bind(config);
  695. esel = P4_OPCODE_ESEL(bind->opcode);
  696. config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
  697. return config;
  698. }
  699. /* check cpu model specifics */
  700. static bool p4_event_match_cpu_model(unsigned int event_idx)
  701. {
  702. /* INSTR_COMPLETED event only exist for model 3, 4, 6 (Prescott) */
  703. if (event_idx == P4_EVENT_INSTR_COMPLETED) {
  704. if (boot_cpu_data.x86_model != 3 &&
  705. boot_cpu_data.x86_model != 4 &&
  706. boot_cpu_data.x86_model != 6)
  707. return false;
  708. }
  709. /*
  710. * For info
  711. * - IQ_ESCR0, IQ_ESCR1 only for models 1 and 2
  712. */
  713. return true;
  714. }
  715. static int p4_validate_raw_event(struct perf_event *event)
  716. {
  717. unsigned int v, emask;
  718. /* User data may have out-of-bound event index */
  719. v = p4_config_unpack_event(event->attr.config);
  720. if (v >= ARRAY_SIZE(p4_event_bind_map))
  721. return -EINVAL;
  722. /* It may be unsupported: */
  723. if (!p4_event_match_cpu_model(v))
  724. return -EINVAL;
  725. /*
  726. * NOTE: P4_CCCR_THREAD_ANY has not the same meaning as
  727. * in Architectural Performance Monitoring, it means not
  728. * on _which_ logical cpu to count but rather _when_, ie it
  729. * depends on logical cpu state -- count event if one cpu active,
  730. * none, both or any, so we just allow user to pass any value
  731. * desired.
  732. *
  733. * In turn we always set Tx_OS/Tx_USR bits bound to logical
  734. * cpu without their propagation to another cpu
  735. */
  736. /*
  737. * if an event is shared across the logical threads
  738. * the user needs special permissions to be able to use it
  739. */
  740. if (p4_ht_active() && p4_event_bind_map[v].shared) {
  741. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  742. return -EACCES;
  743. }
  744. /* ESCR EventMask bits may be invalid */
  745. emask = p4_config_unpack_escr(event->attr.config) & P4_ESCR_EVENTMASK_MASK;
  746. if (emask & ~p4_event_bind_map[v].escr_emask)
  747. return -EINVAL;
  748. /*
  749. * it may have some invalid PEBS bits
  750. */
  751. if (p4_config_pebs_has(event->attr.config, P4_PEBS_CONFIG_ENABLE))
  752. return -EINVAL;
  753. v = p4_config_unpack_metric(event->attr.config);
  754. if (v >= ARRAY_SIZE(p4_pebs_bind_map))
  755. return -EINVAL;
  756. return 0;
  757. }
  758. static int p4_hw_config(struct perf_event *event)
  759. {
  760. int cpu = get_cpu();
  761. int rc = 0;
  762. u32 escr, cccr;
  763. /*
  764. * the reason we use cpu that early is that: if we get scheduled
  765. * first time on the same cpu -- we will not need swap thread
  766. * specific flags in config (and will save some cpu cycles)
  767. */
  768. cccr = p4_default_cccr_conf(cpu);
  769. escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel,
  770. event->attr.exclude_user);
  771. event->hw.config = p4_config_pack_escr(escr) |
  772. p4_config_pack_cccr(cccr);
  773. if (p4_ht_active() && p4_ht_thread(cpu))
  774. event->hw.config = p4_set_ht_bit(event->hw.config);
  775. if (event->attr.type == PERF_TYPE_RAW) {
  776. struct p4_event_bind *bind;
  777. unsigned int esel;
  778. /*
  779. * Clear bits we reserve to be managed by kernel itself
  780. * and never allowed from a user space
  781. */
  782. event->attr.config &= P4_CONFIG_MASK;
  783. rc = p4_validate_raw_event(event);
  784. if (rc)
  785. goto out;
  786. /*
  787. * Note that for RAW events we allow user to use P4_CCCR_RESERVED
  788. * bits since we keep additional info here (for cache events and etc)
  789. */
  790. event->hw.config |= event->attr.config;
  791. bind = p4_config_get_bind(event->attr.config);
  792. if (!bind) {
  793. rc = -EINVAL;
  794. goto out;
  795. }
  796. esel = P4_OPCODE_ESEL(bind->opcode);
  797. event->hw.config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
  798. }
  799. rc = x86_setup_perfctr(event);
  800. out:
  801. put_cpu();
  802. return rc;
  803. }
  804. static inline int p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
  805. {
  806. u64 v;
  807. /* an official way for overflow indication */
  808. rdmsrl(hwc->config_base, v);
  809. if (v & P4_CCCR_OVF) {
  810. wrmsrl(hwc->config_base, v & ~P4_CCCR_OVF);
  811. return 1;
  812. }
  813. /*
  814. * In some circumstances the overflow might issue an NMI but did
  815. * not set P4_CCCR_OVF bit. Because a counter holds a negative value
  816. * we simply check for high bit being set, if it's cleared it means
  817. * the counter has reached zero value and continued counting before
  818. * real NMI signal was received:
  819. */
  820. rdmsrl(hwc->event_base, v);
  821. if (!(v & ARCH_P4_UNFLAGGED_BIT))
  822. return 1;
  823. return 0;
  824. }
  825. static void p4_pmu_disable_pebs(void)
  826. {
  827. /*
  828. * FIXME
  829. *
  830. * It's still allowed that two threads setup same cache
  831. * events so we can't simply clear metrics until we knew
  832. * no one is depending on us, so we need kind of counter
  833. * for "ReplayEvent" users.
  834. *
  835. * What is more complex -- RAW events, if user (for some
  836. * reason) will pass some cache event metric with improper
  837. * event opcode -- it's fine from hardware point of view
  838. * but completely nonsense from "meaning" of such action.
  839. *
  840. * So at moment let leave metrics turned on forever -- it's
  841. * ok for now but need to be revisited!
  842. *
  843. * (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)0);
  844. * (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)0);
  845. */
  846. }
  847. static inline void p4_pmu_disable_event(struct perf_event *event)
  848. {
  849. struct hw_perf_event *hwc = &event->hw;
  850. /*
  851. * If event gets disabled while counter is in overflowed
  852. * state we need to clear P4_CCCR_OVF, otherwise interrupt get
  853. * asserted again and again
  854. */
  855. (void)checking_wrmsrl(hwc->config_base,
  856. (u64)(p4_config_unpack_cccr(hwc->config)) &
  857. ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED);
  858. }
  859. static void p4_pmu_disable_all(void)
  860. {
  861. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  862. int idx;
  863. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  864. struct perf_event *event = cpuc->events[idx];
  865. if (!test_bit(idx, cpuc->active_mask))
  866. continue;
  867. p4_pmu_disable_event(event);
  868. }
  869. p4_pmu_disable_pebs();
  870. }
  871. /* configuration must be valid */
  872. static void p4_pmu_enable_pebs(u64 config)
  873. {
  874. struct p4_pebs_bind *bind;
  875. unsigned int idx;
  876. BUILD_BUG_ON(P4_PEBS_METRIC__max > P4_PEBS_CONFIG_METRIC_MASK);
  877. idx = p4_config_unpack_metric(config);
  878. if (idx == P4_PEBS_METRIC__none)
  879. return;
  880. bind = &p4_pebs_bind_map[idx];
  881. (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind->metric_pebs);
  882. (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind->metric_vert);
  883. }
  884. static void p4_pmu_enable_event(struct perf_event *event)
  885. {
  886. struct hw_perf_event *hwc = &event->hw;
  887. int thread = p4_ht_config_thread(hwc->config);
  888. u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
  889. unsigned int idx = p4_config_unpack_event(hwc->config);
  890. struct p4_event_bind *bind;
  891. u64 escr_addr, cccr;
  892. bind = &p4_event_bind_map[idx];
  893. escr_addr = (u64)bind->escr_msr[thread];
  894. /*
  895. * - we dont support cascaded counters yet
  896. * - and counter 1 is broken (erratum)
  897. */
  898. WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
  899. WARN_ON_ONCE(hwc->idx == 1);
  900. /* we need a real Event value */
  901. escr_conf &= ~P4_ESCR_EVENT_MASK;
  902. escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode));
  903. cccr = p4_config_unpack_cccr(hwc->config);
  904. /*
  905. * it could be Cache event so we need to write metrics
  906. * into additional MSRs
  907. */
  908. p4_pmu_enable_pebs(hwc->config);
  909. (void)checking_wrmsrl(escr_addr, escr_conf);
  910. (void)checking_wrmsrl(hwc->config_base,
  911. (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE);
  912. }
  913. static void p4_pmu_enable_all(int added)
  914. {
  915. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  916. int idx;
  917. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  918. struct perf_event *event = cpuc->events[idx];
  919. if (!test_bit(idx, cpuc->active_mask))
  920. continue;
  921. p4_pmu_enable_event(event);
  922. }
  923. }
  924. static int p4_pmu_handle_irq(struct pt_regs *regs)
  925. {
  926. struct perf_sample_data data;
  927. struct cpu_hw_events *cpuc;
  928. struct perf_event *event;
  929. struct hw_perf_event *hwc;
  930. int idx, handled = 0;
  931. u64 val;
  932. perf_sample_data_init(&data, 0);
  933. cpuc = &__get_cpu_var(cpu_hw_events);
  934. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  935. int overflow;
  936. if (!test_bit(idx, cpuc->active_mask)) {
  937. /* catch in-flight IRQs */
  938. if (__test_and_clear_bit(idx, cpuc->running))
  939. handled++;
  940. continue;
  941. }
  942. event = cpuc->events[idx];
  943. hwc = &event->hw;
  944. WARN_ON_ONCE(hwc->idx != idx);
  945. /* it might be unflagged overflow */
  946. overflow = p4_pmu_clear_cccr_ovf(hwc);
  947. val = x86_perf_event_update(event);
  948. if (!overflow && (val & (1ULL << (x86_pmu.cntval_bits - 1))))
  949. continue;
  950. handled += overflow;
  951. /* event overflow for sure */
  952. data.period = event->hw.last_period;
  953. if (!x86_perf_event_set_period(event))
  954. continue;
  955. if (perf_event_overflow(event, &data, regs))
  956. x86_pmu_stop(event, 0);
  957. }
  958. if (handled)
  959. inc_irq_stat(apic_perf_irqs);
  960. /*
  961. * When dealing with the unmasking of the LVTPC on P4 perf hw, it has
  962. * been observed that the OVF bit flag has to be cleared first _before_
  963. * the LVTPC can be unmasked.
  964. *
  965. * The reason is the NMI line will continue to be asserted while the OVF
  966. * bit is set. This causes a second NMI to generate if the LVTPC is
  967. * unmasked before the OVF bit is cleared, leading to unknown NMI
  968. * messages.
  969. */
  970. apic_write(APIC_LVTPC, APIC_DM_NMI);
  971. return handled;
  972. }
  973. /*
  974. * swap thread specific fields according to a thread
  975. * we are going to run on
  976. */
  977. static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
  978. {
  979. u32 escr, cccr;
  980. /*
  981. * we either lucky and continue on same cpu or no HT support
  982. */
  983. if (!p4_should_swap_ts(hwc->config, cpu))
  984. return;
  985. /*
  986. * the event is migrated from an another logical
  987. * cpu, so we need to swap thread specific flags
  988. */
  989. escr = p4_config_unpack_escr(hwc->config);
  990. cccr = p4_config_unpack_cccr(hwc->config);
  991. if (p4_ht_thread(cpu)) {
  992. cccr &= ~P4_CCCR_OVF_PMI_T0;
  993. cccr |= P4_CCCR_OVF_PMI_T1;
  994. if (escr & P4_ESCR_T0_OS) {
  995. escr &= ~P4_ESCR_T0_OS;
  996. escr |= P4_ESCR_T1_OS;
  997. }
  998. if (escr & P4_ESCR_T0_USR) {
  999. escr &= ~P4_ESCR_T0_USR;
  1000. escr |= P4_ESCR_T1_USR;
  1001. }
  1002. hwc->config = p4_config_pack_escr(escr);
  1003. hwc->config |= p4_config_pack_cccr(cccr);
  1004. hwc->config |= P4_CONFIG_HT;
  1005. } else {
  1006. cccr &= ~P4_CCCR_OVF_PMI_T1;
  1007. cccr |= P4_CCCR_OVF_PMI_T0;
  1008. if (escr & P4_ESCR_T1_OS) {
  1009. escr &= ~P4_ESCR_T1_OS;
  1010. escr |= P4_ESCR_T0_OS;
  1011. }
  1012. if (escr & P4_ESCR_T1_USR) {
  1013. escr &= ~P4_ESCR_T1_USR;
  1014. escr |= P4_ESCR_T0_USR;
  1015. }
  1016. hwc->config = p4_config_pack_escr(escr);
  1017. hwc->config |= p4_config_pack_cccr(cccr);
  1018. hwc->config &= ~P4_CONFIG_HT;
  1019. }
  1020. }
  1021. /*
  1022. * ESCR address hashing is tricky, ESCRs are not sequential
  1023. * in memory but all starts from MSR_P4_BSU_ESCR0 (0x03a0) and
  1024. * the metric between any ESCRs is laid in range [0xa0,0xe1]
  1025. *
  1026. * so we make ~70% filled hashtable
  1027. */
  1028. #define P4_ESCR_MSR_BASE 0x000003a0
  1029. #define P4_ESCR_MSR_MAX 0x000003e1
  1030. #define P4_ESCR_MSR_TABLE_SIZE (P4_ESCR_MSR_MAX - P4_ESCR_MSR_BASE + 1)
  1031. #define P4_ESCR_MSR_IDX(msr) (msr - P4_ESCR_MSR_BASE)
  1032. #define P4_ESCR_MSR_TABLE_ENTRY(msr) [P4_ESCR_MSR_IDX(msr)] = msr
  1033. static const unsigned int p4_escr_table[P4_ESCR_MSR_TABLE_SIZE] = {
  1034. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR0),
  1035. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR1),
  1036. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR0),
  1037. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR1),
  1038. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR0),
  1039. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR1),
  1040. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR0),
  1041. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR1),
  1042. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR2),
  1043. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR3),
  1044. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR4),
  1045. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR5),
  1046. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR0),
  1047. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR1),
  1048. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR0),
  1049. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR1),
  1050. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR0),
  1051. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR1),
  1052. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR0),
  1053. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR1),
  1054. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR0),
  1055. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR1),
  1056. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR0),
  1057. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR1),
  1058. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR0),
  1059. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR1),
  1060. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR0),
  1061. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR1),
  1062. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR0),
  1063. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR1),
  1064. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR0),
  1065. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR1),
  1066. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR0),
  1067. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR1),
  1068. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR0),
  1069. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR1),
  1070. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR0),
  1071. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR1),
  1072. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR0),
  1073. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR1),
  1074. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR0),
  1075. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR1),
  1076. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR0),
  1077. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR1),
  1078. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR0),
  1079. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR1),
  1080. };
  1081. static int p4_get_escr_idx(unsigned int addr)
  1082. {
  1083. unsigned int idx = P4_ESCR_MSR_IDX(addr);
  1084. if (unlikely(idx >= P4_ESCR_MSR_TABLE_SIZE ||
  1085. !p4_escr_table[idx] ||
  1086. p4_escr_table[idx] != addr)) {
  1087. WARN_ONCE(1, "P4 PMU: Wrong address passed: %x\n", addr);
  1088. return -1;
  1089. }
  1090. return idx;
  1091. }
  1092. static int p4_next_cntr(int thread, unsigned long *used_mask,
  1093. struct p4_event_bind *bind)
  1094. {
  1095. int i, j;
  1096. for (i = 0; i < P4_CNTR_LIMIT; i++) {
  1097. j = bind->cntr[thread][i];
  1098. if (j != -1 && !test_bit(j, used_mask))
  1099. return j;
  1100. }
  1101. return -1;
  1102. }
  1103. static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  1104. {
  1105. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  1106. unsigned long escr_mask[BITS_TO_LONGS(P4_ESCR_MSR_TABLE_SIZE)];
  1107. int cpu = smp_processor_id();
  1108. struct hw_perf_event *hwc;
  1109. struct p4_event_bind *bind;
  1110. unsigned int i, thread, num;
  1111. int cntr_idx, escr_idx;
  1112. u64 config_alias;
  1113. int pass;
  1114. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  1115. bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE);
  1116. for (i = 0, num = n; i < n; i++, num--) {
  1117. hwc = &cpuc->event_list[i]->hw;
  1118. thread = p4_ht_thread(cpu);
  1119. pass = 0;
  1120. again:
  1121. /*
  1122. * It's possible to hit a circular lock
  1123. * between original and alternative events
  1124. * if both are scheduled already.
  1125. */
  1126. if (pass > 2)
  1127. goto done;
  1128. bind = p4_config_get_bind(hwc->config);
  1129. escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
  1130. if (unlikely(escr_idx == -1))
  1131. goto done;
  1132. if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
  1133. cntr_idx = hwc->idx;
  1134. if (assign)
  1135. assign[i] = hwc->idx;
  1136. goto reserve;
  1137. }
  1138. cntr_idx = p4_next_cntr(thread, used_mask, bind);
  1139. if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) {
  1140. /*
  1141. * Check whether an event alias is still available.
  1142. */
  1143. config_alias = p4_get_alias_event(hwc->config);
  1144. if (!config_alias)
  1145. goto done;
  1146. hwc->config = config_alias;
  1147. pass++;
  1148. goto again;
  1149. }
  1150. p4_pmu_swap_config_ts(hwc, cpu);
  1151. if (assign)
  1152. assign[i] = cntr_idx;
  1153. reserve:
  1154. set_bit(cntr_idx, used_mask);
  1155. set_bit(escr_idx, escr_mask);
  1156. }
  1157. done:
  1158. return num ? -EINVAL : 0;
  1159. }
  1160. PMU_FORMAT_ATTR(cccr, "config:0-31" );
  1161. PMU_FORMAT_ATTR(escr, "config:32-62");
  1162. PMU_FORMAT_ATTR(ht, "config:63" );
  1163. static struct attribute *intel_p4_formats_attr[] = {
  1164. &format_attr_cccr.attr,
  1165. &format_attr_escr.attr,
  1166. &format_attr_ht.attr,
  1167. NULL,
  1168. };
  1169. static __initconst const struct x86_pmu p4_pmu = {
  1170. .name = "Netburst P4/Xeon",
  1171. .handle_irq = p4_pmu_handle_irq,
  1172. .disable_all = p4_pmu_disable_all,
  1173. .enable_all = p4_pmu_enable_all,
  1174. .enable = p4_pmu_enable_event,
  1175. .disable = p4_pmu_disable_event,
  1176. .eventsel = MSR_P4_BPU_CCCR0,
  1177. .perfctr = MSR_P4_BPU_PERFCTR0,
  1178. .event_map = p4_pmu_event_map,
  1179. .max_events = ARRAY_SIZE(p4_general_events),
  1180. .get_event_constraints = x86_get_event_constraints,
  1181. /*
  1182. * IF HT disabled we may need to use all
  1183. * ARCH_P4_MAX_CCCR counters simulaneously
  1184. * though leave it restricted at moment assuming
  1185. * HT is on
  1186. */
  1187. .num_counters = ARCH_P4_MAX_CCCR,
  1188. .apic = 1,
  1189. .cntval_bits = ARCH_P4_CNTRVAL_BITS,
  1190. .cntval_mask = ARCH_P4_CNTRVAL_MASK,
  1191. .max_period = (1ULL << (ARCH_P4_CNTRVAL_BITS - 1)) - 1,
  1192. .hw_config = p4_hw_config,
  1193. .schedule_events = p4_pmu_schedule_events,
  1194. /*
  1195. * This handles erratum N15 in intel doc 249199-029,
  1196. * the counter may not be updated correctly on write
  1197. * so we need a second write operation to do the trick
  1198. * (the official workaround didn't work)
  1199. *
  1200. * the former idea is taken from OProfile code
  1201. */
  1202. .perfctr_second_write = 1,
  1203. .format_attrs = intel_p4_formats_attr,
  1204. };
  1205. __init int p4_pmu_init(void)
  1206. {
  1207. unsigned int low, high;
  1208. /* If we get stripped -- indexing fails */
  1209. BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
  1210. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  1211. if (!(low & (1 << 7))) {
  1212. pr_cont("unsupported Netburst CPU model %d ",
  1213. boot_cpu_data.x86_model);
  1214. return -ENODEV;
  1215. }
  1216. memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
  1217. sizeof(hw_cache_event_ids));
  1218. pr_cont("Netburst events, ");
  1219. x86_pmu = p4_pmu;
  1220. return 0;
  1221. }