perf_event_p4.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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)wrmsrl_safe(MSR_IA32_PEBS_ENABLE, (u64)0);
  844. * (void)wrmsrl_safe(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)wrmsrl_safe(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)wrmsrl_safe(MSR_IA32_PEBS_ENABLE, (u64)bind->metric_pebs);
  882. (void)wrmsrl_safe(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)wrmsrl_safe(escr_addr, escr_conf);
  910. (void)wrmsrl_safe(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. cpuc = &__get_cpu_var(cpu_hw_events);
  933. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  934. int overflow;
  935. if (!test_bit(idx, cpuc->active_mask)) {
  936. /* catch in-flight IRQs */
  937. if (__test_and_clear_bit(idx, cpuc->running))
  938. handled++;
  939. continue;
  940. }
  941. event = cpuc->events[idx];
  942. hwc = &event->hw;
  943. WARN_ON_ONCE(hwc->idx != idx);
  944. /* it might be unflagged overflow */
  945. overflow = p4_pmu_clear_cccr_ovf(hwc);
  946. val = x86_perf_event_update(event);
  947. if (!overflow && (val & (1ULL << (x86_pmu.cntval_bits - 1))))
  948. continue;
  949. handled += overflow;
  950. /* event overflow for sure */
  951. perf_sample_data_init(&data, 0, hwc->last_period);
  952. if (!x86_perf_event_set_period(event))
  953. continue;
  954. if (perf_event_overflow(event, &data, regs))
  955. x86_pmu_stop(event, 0);
  956. }
  957. if (handled)
  958. inc_irq_stat(apic_perf_irqs);
  959. /*
  960. * When dealing with the unmasking of the LVTPC on P4 perf hw, it has
  961. * been observed that the OVF bit flag has to be cleared first _before_
  962. * the LVTPC can be unmasked.
  963. *
  964. * The reason is the NMI line will continue to be asserted while the OVF
  965. * bit is set. This causes a second NMI to generate if the LVTPC is
  966. * unmasked before the OVF bit is cleared, leading to unknown NMI
  967. * messages.
  968. */
  969. apic_write(APIC_LVTPC, APIC_DM_NMI);
  970. return handled;
  971. }
  972. /*
  973. * swap thread specific fields according to a thread
  974. * we are going to run on
  975. */
  976. static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
  977. {
  978. u32 escr, cccr;
  979. /*
  980. * we either lucky and continue on same cpu or no HT support
  981. */
  982. if (!p4_should_swap_ts(hwc->config, cpu))
  983. return;
  984. /*
  985. * the event is migrated from an another logical
  986. * cpu, so we need to swap thread specific flags
  987. */
  988. escr = p4_config_unpack_escr(hwc->config);
  989. cccr = p4_config_unpack_cccr(hwc->config);
  990. if (p4_ht_thread(cpu)) {
  991. cccr &= ~P4_CCCR_OVF_PMI_T0;
  992. cccr |= P4_CCCR_OVF_PMI_T1;
  993. if (escr & P4_ESCR_T0_OS) {
  994. escr &= ~P4_ESCR_T0_OS;
  995. escr |= P4_ESCR_T1_OS;
  996. }
  997. if (escr & P4_ESCR_T0_USR) {
  998. escr &= ~P4_ESCR_T0_USR;
  999. escr |= P4_ESCR_T1_USR;
  1000. }
  1001. hwc->config = p4_config_pack_escr(escr);
  1002. hwc->config |= p4_config_pack_cccr(cccr);
  1003. hwc->config |= P4_CONFIG_HT;
  1004. } else {
  1005. cccr &= ~P4_CCCR_OVF_PMI_T1;
  1006. cccr |= P4_CCCR_OVF_PMI_T0;
  1007. if (escr & P4_ESCR_T1_OS) {
  1008. escr &= ~P4_ESCR_T1_OS;
  1009. escr |= P4_ESCR_T0_OS;
  1010. }
  1011. if (escr & P4_ESCR_T1_USR) {
  1012. escr &= ~P4_ESCR_T1_USR;
  1013. escr |= P4_ESCR_T0_USR;
  1014. }
  1015. hwc->config = p4_config_pack_escr(escr);
  1016. hwc->config |= p4_config_pack_cccr(cccr);
  1017. hwc->config &= ~P4_CONFIG_HT;
  1018. }
  1019. }
  1020. /*
  1021. * ESCR address hashing is tricky, ESCRs are not sequential
  1022. * in memory but all starts from MSR_P4_BSU_ESCR0 (0x03a0) and
  1023. * the metric between any ESCRs is laid in range [0xa0,0xe1]
  1024. *
  1025. * so we make ~70% filled hashtable
  1026. */
  1027. #define P4_ESCR_MSR_BASE 0x000003a0
  1028. #define P4_ESCR_MSR_MAX 0x000003e1
  1029. #define P4_ESCR_MSR_TABLE_SIZE (P4_ESCR_MSR_MAX - P4_ESCR_MSR_BASE + 1)
  1030. #define P4_ESCR_MSR_IDX(msr) (msr - P4_ESCR_MSR_BASE)
  1031. #define P4_ESCR_MSR_TABLE_ENTRY(msr) [P4_ESCR_MSR_IDX(msr)] = msr
  1032. static const unsigned int p4_escr_table[P4_ESCR_MSR_TABLE_SIZE] = {
  1033. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR0),
  1034. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR1),
  1035. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR0),
  1036. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR1),
  1037. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR0),
  1038. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR1),
  1039. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR0),
  1040. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR1),
  1041. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR2),
  1042. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR3),
  1043. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR4),
  1044. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR5),
  1045. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR0),
  1046. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR1),
  1047. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR0),
  1048. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR1),
  1049. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR0),
  1050. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR1),
  1051. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR0),
  1052. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR1),
  1053. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR0),
  1054. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR1),
  1055. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR0),
  1056. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR1),
  1057. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR0),
  1058. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR1),
  1059. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR0),
  1060. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR1),
  1061. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR0),
  1062. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR1),
  1063. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR0),
  1064. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR1),
  1065. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR0),
  1066. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR1),
  1067. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR0),
  1068. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR1),
  1069. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR0),
  1070. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR1),
  1071. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR0),
  1072. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR1),
  1073. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR0),
  1074. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR1),
  1075. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR0),
  1076. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR1),
  1077. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR0),
  1078. P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR1),
  1079. };
  1080. static int p4_get_escr_idx(unsigned int addr)
  1081. {
  1082. unsigned int idx = P4_ESCR_MSR_IDX(addr);
  1083. if (unlikely(idx >= P4_ESCR_MSR_TABLE_SIZE ||
  1084. !p4_escr_table[idx] ||
  1085. p4_escr_table[idx] != addr)) {
  1086. WARN_ONCE(1, "P4 PMU: Wrong address passed: %x\n", addr);
  1087. return -1;
  1088. }
  1089. return idx;
  1090. }
  1091. static int p4_next_cntr(int thread, unsigned long *used_mask,
  1092. struct p4_event_bind *bind)
  1093. {
  1094. int i, j;
  1095. for (i = 0; i < P4_CNTR_LIMIT; i++) {
  1096. j = bind->cntr[thread][i];
  1097. if (j != -1 && !test_bit(j, used_mask))
  1098. return j;
  1099. }
  1100. return -1;
  1101. }
  1102. static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  1103. {
  1104. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  1105. unsigned long escr_mask[BITS_TO_LONGS(P4_ESCR_MSR_TABLE_SIZE)];
  1106. int cpu = smp_processor_id();
  1107. struct hw_perf_event *hwc;
  1108. struct p4_event_bind *bind;
  1109. unsigned int i, thread, num;
  1110. int cntr_idx, escr_idx;
  1111. u64 config_alias;
  1112. int pass;
  1113. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  1114. bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE);
  1115. for (i = 0, num = n; i < n; i++, num--) {
  1116. hwc = &cpuc->event_list[i]->hw;
  1117. thread = p4_ht_thread(cpu);
  1118. pass = 0;
  1119. again:
  1120. /*
  1121. * It's possible to hit a circular lock
  1122. * between original and alternative events
  1123. * if both are scheduled already.
  1124. */
  1125. if (pass > 2)
  1126. goto done;
  1127. bind = p4_config_get_bind(hwc->config);
  1128. escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
  1129. if (unlikely(escr_idx == -1))
  1130. goto done;
  1131. if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
  1132. cntr_idx = hwc->idx;
  1133. if (assign)
  1134. assign[i] = hwc->idx;
  1135. goto reserve;
  1136. }
  1137. cntr_idx = p4_next_cntr(thread, used_mask, bind);
  1138. if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) {
  1139. /*
  1140. * Check whether an event alias is still available.
  1141. */
  1142. config_alias = p4_get_alias_event(hwc->config);
  1143. if (!config_alias)
  1144. goto done;
  1145. hwc->config = config_alias;
  1146. pass++;
  1147. goto again;
  1148. }
  1149. p4_pmu_swap_config_ts(hwc, cpu);
  1150. if (assign)
  1151. assign[i] = cntr_idx;
  1152. reserve:
  1153. set_bit(cntr_idx, used_mask);
  1154. set_bit(escr_idx, escr_mask);
  1155. }
  1156. done:
  1157. return num ? -EINVAL : 0;
  1158. }
  1159. PMU_FORMAT_ATTR(cccr, "config:0-31" );
  1160. PMU_FORMAT_ATTR(escr, "config:32-62");
  1161. PMU_FORMAT_ATTR(ht, "config:63" );
  1162. static struct attribute *intel_p4_formats_attr[] = {
  1163. &format_attr_cccr.attr,
  1164. &format_attr_escr.attr,
  1165. &format_attr_ht.attr,
  1166. NULL,
  1167. };
  1168. static __initconst const struct x86_pmu p4_pmu = {
  1169. .name = "Netburst P4/Xeon",
  1170. .handle_irq = p4_pmu_handle_irq,
  1171. .disable_all = p4_pmu_disable_all,
  1172. .enable_all = p4_pmu_enable_all,
  1173. .enable = p4_pmu_enable_event,
  1174. .disable = p4_pmu_disable_event,
  1175. .eventsel = MSR_P4_BPU_CCCR0,
  1176. .perfctr = MSR_P4_BPU_PERFCTR0,
  1177. .event_map = p4_pmu_event_map,
  1178. .max_events = ARRAY_SIZE(p4_general_events),
  1179. .get_event_constraints = x86_get_event_constraints,
  1180. /*
  1181. * IF HT disabled we may need to use all
  1182. * ARCH_P4_MAX_CCCR counters simulaneously
  1183. * though leave it restricted at moment assuming
  1184. * HT is on
  1185. */
  1186. .num_counters = ARCH_P4_MAX_CCCR,
  1187. .apic = 1,
  1188. .cntval_bits = ARCH_P4_CNTRVAL_BITS,
  1189. .cntval_mask = ARCH_P4_CNTRVAL_MASK,
  1190. .max_period = (1ULL << (ARCH_P4_CNTRVAL_BITS - 1)) - 1,
  1191. .hw_config = p4_hw_config,
  1192. .schedule_events = p4_pmu_schedule_events,
  1193. /*
  1194. * This handles erratum N15 in intel doc 249199-029,
  1195. * the counter may not be updated correctly on write
  1196. * so we need a second write operation to do the trick
  1197. * (the official workaround didn't work)
  1198. *
  1199. * the former idea is taken from OProfile code
  1200. */
  1201. .perfctr_second_write = 1,
  1202. .format_attrs = intel_p4_formats_attr,
  1203. };
  1204. __init int p4_pmu_init(void)
  1205. {
  1206. unsigned int low, high;
  1207. /* If we get stripped -- indexing fails */
  1208. BUILD_BUG_ON(ARCH_P4_MAX_CCCR > INTEL_PMC_MAX_GENERIC);
  1209. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  1210. if (!(low & (1 << 7))) {
  1211. pr_cont("unsupported Netburst CPU model %d ",
  1212. boot_cpu_data.x86_model);
  1213. return -ENODEV;
  1214. }
  1215. memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
  1216. sizeof(hw_cache_event_ids));
  1217. pr_cont("Netburst events, ");
  1218. x86_pmu = p4_pmu;
  1219. return 0;
  1220. }