perf_event_p4.c 43 KB

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