perf_event_p4.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * Netburst Perfomance 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 char cntr[2][P4_CNTR_LIMIT]; /* counter index (offset), -1 on abscence */
  19. };
  20. struct p4_cache_event_bind {
  21. unsigned int metric_pebs;
  22. unsigned int metric_vert;
  23. };
  24. #define P4_GEN_CACHE_EVENT_BIND(name) \
  25. [P4_CACHE__##name] = { \
  26. .metric_pebs = P4_PEBS__##name, \
  27. .metric_vert = P4_VERT__##name, \
  28. }
  29. static struct p4_cache_event_bind p4_cache_event_bind_map[] = {
  30. P4_GEN_CACHE_EVENT_BIND(1stl_cache_load_miss_retired),
  31. P4_GEN_CACHE_EVENT_BIND(2ndl_cache_load_miss_retired),
  32. P4_GEN_CACHE_EVENT_BIND(dtlb_load_miss_retired),
  33. P4_GEN_CACHE_EVENT_BIND(dtlb_store_miss_retired),
  34. };
  35. /*
  36. * Note that we don't use CCCR1 here, there is an
  37. * exception for P4_BSQ_ALLOCATION but we just have
  38. * no workaround
  39. *
  40. * consider this binding as resources which particular
  41. * event may borrow, it doesn't contain EventMask,
  42. * Tags and friends -- they are left to a caller
  43. */
  44. static struct p4_event_bind p4_event_bind_map[] = {
  45. [P4_EVENT_TC_DELIVER_MODE] = {
  46. .opcode = P4_OPCODE(P4_EVENT_TC_DELIVER_MODE),
  47. .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
  48. .cntr = { {4, 5, -1}, {6, 7, -1} },
  49. },
  50. [P4_EVENT_BPU_FETCH_REQUEST] = {
  51. .opcode = P4_OPCODE(P4_EVENT_BPU_FETCH_REQUEST),
  52. .escr_msr = { MSR_P4_BPU_ESCR0, MSR_P4_BPU_ESCR1 },
  53. .cntr = { {0, -1, -1}, {2, -1, -1} },
  54. },
  55. [P4_EVENT_ITLB_REFERENCE] = {
  56. .opcode = P4_OPCODE(P4_EVENT_ITLB_REFERENCE),
  57. .escr_msr = { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
  58. .cntr = { {0, -1, -1}, {2, -1, -1} },
  59. },
  60. [P4_EVENT_MEMORY_CANCEL] = {
  61. .opcode = P4_OPCODE(P4_EVENT_MEMORY_CANCEL),
  62. .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
  63. .cntr = { {8, 9, -1}, {10, 11, -1} },
  64. },
  65. [P4_EVENT_MEMORY_COMPLETE] = {
  66. .opcode = P4_OPCODE(P4_EVENT_MEMORY_COMPLETE),
  67. .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
  68. .cntr = { {8, 9, -1}, {10, 11, -1} },
  69. },
  70. [P4_EVENT_LOAD_PORT_REPLAY] = {
  71. .opcode = P4_OPCODE(P4_EVENT_LOAD_PORT_REPLAY),
  72. .escr_msr = { MSR_P4_SAAT_ESCR0, MSR_P4_SAAT_ESCR1 },
  73. .cntr = { {8, 9, -1}, {10, 11, -1} },
  74. },
  75. [P4_EVENT_STORE_PORT_REPLAY] = {
  76. .opcode = P4_OPCODE(P4_EVENT_STORE_PORT_REPLAY),
  77. .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
  78. .cntr = { {8, 9, -1}, {10, 11, -1} },
  79. },
  80. [P4_EVENT_MOB_LOAD_REPLAY] = {
  81. .opcode = P4_OPCODE(P4_EVENT_MOB_LOAD_REPLAY),
  82. .escr_msr = { MSR_P4_MOB_ESCR0, MSR_P4_MOB_ESCR1 },
  83. .cntr = { {0, -1, -1}, {2, -1, -1} },
  84. },
  85. [P4_EVENT_PAGE_WALK_TYPE] = {
  86. .opcode = P4_OPCODE(P4_EVENT_PAGE_WALK_TYPE),
  87. .escr_msr = { MSR_P4_PMH_ESCR0, MSR_P4_PMH_ESCR1 },
  88. .cntr = { {0, -1, -1}, {2, -1, -1} },
  89. },
  90. [P4_EVENT_BSQ_CACHE_REFERENCE] = {
  91. .opcode = P4_OPCODE(P4_EVENT_BSQ_CACHE_REFERENCE),
  92. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  93. .cntr = { {0, -1, -1}, {2, -1, -1} },
  94. },
  95. [P4_EVENT_IOQ_ALLOCATION] = {
  96. .opcode = P4_OPCODE(P4_EVENT_IOQ_ALLOCATION),
  97. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  98. .cntr = { {0, -1, -1}, {2, -1, -1} },
  99. },
  100. [P4_EVENT_IOQ_ACTIVE_ENTRIES] = { /* shared ESCR */
  101. .opcode = P4_OPCODE(P4_EVENT_IOQ_ACTIVE_ENTRIES),
  102. .escr_msr = { MSR_P4_FSB_ESCR1, MSR_P4_FSB_ESCR1 },
  103. .cntr = { {2, -1, -1}, {3, -1, -1} },
  104. },
  105. [P4_EVENT_FSB_DATA_ACTIVITY] = {
  106. .opcode = P4_OPCODE(P4_EVENT_FSB_DATA_ACTIVITY),
  107. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  108. .cntr = { {0, -1, -1}, {2, -1, -1} },
  109. },
  110. [P4_EVENT_BSQ_ALLOCATION] = { /* shared ESCR, broken CCCR1 */
  111. .opcode = P4_OPCODE(P4_EVENT_BSQ_ALLOCATION),
  112. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR0 },
  113. .cntr = { {0, -1, -1}, {1, -1, -1} },
  114. },
  115. [P4_EVENT_BSQ_ACTIVE_ENTRIES] = { /* shared ESCR */
  116. .opcode = P4_OPCODE(P4_EVENT_BSQ_ACTIVE_ENTRIES),
  117. .escr_msr = { MSR_P4_BSU_ESCR1 , MSR_P4_BSU_ESCR1 },
  118. .cntr = { {2, -1, -1}, {3, -1, -1} },
  119. },
  120. [P4_EVENT_SSE_INPUT_ASSIST] = {
  121. .opcode = P4_OPCODE(P4_EVENT_SSE_INPUT_ASSIST),
  122. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  123. .cntr = { {8, 9, -1}, {10, 11, -1} },
  124. },
  125. [P4_EVENT_PACKED_SP_UOP] = {
  126. .opcode = P4_OPCODE(P4_EVENT_PACKED_SP_UOP),
  127. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  128. .cntr = { {8, 9, -1}, {10, 11, -1} },
  129. },
  130. [P4_EVENT_PACKED_DP_UOP] = {
  131. .opcode = P4_OPCODE(P4_EVENT_PACKED_DP_UOP),
  132. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  133. .cntr = { {8, 9, -1}, {10, 11, -1} },
  134. },
  135. [P4_EVENT_SCALAR_SP_UOP] = {
  136. .opcode = P4_OPCODE(P4_EVENT_SCALAR_SP_UOP),
  137. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  138. .cntr = { {8, 9, -1}, {10, 11, -1} },
  139. },
  140. [P4_EVENT_SCALAR_DP_UOP] = {
  141. .opcode = P4_OPCODE(P4_EVENT_SCALAR_DP_UOP),
  142. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  143. .cntr = { {8, 9, -1}, {10, 11, -1} },
  144. },
  145. [P4_EVENT_64BIT_MMX_UOP] = {
  146. .opcode = P4_OPCODE(P4_EVENT_64BIT_MMX_UOP),
  147. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  148. .cntr = { {8, 9, -1}, {10, 11, -1} },
  149. },
  150. [P4_EVENT_128BIT_MMX_UOP] = {
  151. .opcode = P4_OPCODE(P4_EVENT_128BIT_MMX_UOP),
  152. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  153. .cntr = { {8, 9, -1}, {10, 11, -1} },
  154. },
  155. [P4_EVENT_X87_FP_UOP] = {
  156. .opcode = P4_OPCODE(P4_EVENT_X87_FP_UOP),
  157. .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
  158. .cntr = { {8, 9, -1}, {10, 11, -1} },
  159. },
  160. [P4_EVENT_TC_MISC] = {
  161. .opcode = P4_OPCODE(P4_EVENT_TC_MISC),
  162. .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
  163. .cntr = { {4, 5, -1}, {6, 7, -1} },
  164. },
  165. [P4_EVENT_GLOBAL_POWER_EVENTS] = {
  166. .opcode = P4_OPCODE(P4_EVENT_GLOBAL_POWER_EVENTS),
  167. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  168. .cntr = { {0, -1, -1}, {2, -1, -1} },
  169. },
  170. [P4_EVENT_TC_MS_XFER] = {
  171. .opcode = P4_OPCODE(P4_EVENT_TC_MS_XFER),
  172. .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
  173. .cntr = { {4, 5, -1}, {6, 7, -1} },
  174. },
  175. [P4_EVENT_UOP_QUEUE_WRITES] = {
  176. .opcode = P4_OPCODE(P4_EVENT_UOP_QUEUE_WRITES),
  177. .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
  178. .cntr = { {4, 5, -1}, {6, 7, -1} },
  179. },
  180. [P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE] = {
  181. .opcode = P4_OPCODE(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE),
  182. .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR0 },
  183. .cntr = { {4, 5, -1}, {6, 7, -1} },
  184. },
  185. [P4_EVENT_RETIRED_BRANCH_TYPE] = {
  186. .opcode = P4_OPCODE(P4_EVENT_RETIRED_BRANCH_TYPE),
  187. .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR1 },
  188. .cntr = { {4, 5, -1}, {6, 7, -1} },
  189. },
  190. [P4_EVENT_RESOURCE_STALL] = {
  191. .opcode = P4_OPCODE(P4_EVENT_RESOURCE_STALL),
  192. .escr_msr = { MSR_P4_ALF_ESCR0, MSR_P4_ALF_ESCR1 },
  193. .cntr = { {12, 13, 16}, {14, 15, 17} },
  194. },
  195. [P4_EVENT_WC_BUFFER] = {
  196. .opcode = P4_OPCODE(P4_EVENT_WC_BUFFER),
  197. .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
  198. .cntr = { {8, 9, -1}, {10, 11, -1} },
  199. },
  200. [P4_EVENT_B2B_CYCLES] = {
  201. .opcode = P4_OPCODE(P4_EVENT_B2B_CYCLES),
  202. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  203. .cntr = { {0, -1, -1}, {2, -1, -1} },
  204. },
  205. [P4_EVENT_BNR] = {
  206. .opcode = P4_OPCODE(P4_EVENT_BNR),
  207. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  208. .cntr = { {0, -1, -1}, {2, -1, -1} },
  209. },
  210. [P4_EVENT_SNOOP] = {
  211. .opcode = P4_OPCODE(P4_EVENT_SNOOP),
  212. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  213. .cntr = { {0, -1, -1}, {2, -1, -1} },
  214. },
  215. [P4_EVENT_RESPONSE] = {
  216. .opcode = P4_OPCODE(P4_EVENT_RESPONSE),
  217. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  218. .cntr = { {0, -1, -1}, {2, -1, -1} },
  219. },
  220. [P4_EVENT_FRONT_END_EVENT] = {
  221. .opcode = P4_OPCODE(P4_EVENT_FRONT_END_EVENT),
  222. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  223. .cntr = { {12, 13, 16}, {14, 15, 17} },
  224. },
  225. [P4_EVENT_EXECUTION_EVENT] = {
  226. .opcode = P4_OPCODE(P4_EVENT_EXECUTION_EVENT),
  227. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  228. .cntr = { {12, 13, 16}, {14, 15, 17} },
  229. },
  230. [P4_EVENT_REPLAY_EVENT] = {
  231. .opcode = P4_OPCODE(P4_EVENT_REPLAY_EVENT),
  232. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  233. .cntr = { {12, 13, 16}, {14, 15, 17} },
  234. },
  235. [P4_EVENT_INSTR_RETIRED] = {
  236. .opcode = P4_OPCODE(P4_EVENT_INSTR_RETIRED),
  237. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  238. .cntr = { {12, 13, 16}, {14, 15, 17} },
  239. },
  240. [P4_EVENT_UOPS_RETIRED] = {
  241. .opcode = P4_OPCODE(P4_EVENT_UOPS_RETIRED),
  242. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  243. .cntr = { {12, 13, 16}, {14, 15, 17} },
  244. },
  245. [P4_EVENT_UOP_TYPE] = {
  246. .opcode = P4_OPCODE(P4_EVENT_UOP_TYPE),
  247. .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
  248. .cntr = { {12, 13, 16}, {14, 15, 17} },
  249. },
  250. [P4_EVENT_BRANCH_RETIRED] = {
  251. .opcode = P4_OPCODE(P4_EVENT_BRANCH_RETIRED),
  252. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  253. .cntr = { {12, 13, 16}, {14, 15, 17} },
  254. },
  255. [P4_EVENT_MISPRED_BRANCH_RETIRED] = {
  256. .opcode = P4_OPCODE(P4_EVENT_MISPRED_BRANCH_RETIRED),
  257. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  258. .cntr = { {12, 13, 16}, {14, 15, 17} },
  259. },
  260. [P4_EVENT_X87_ASSIST] = {
  261. .opcode = P4_OPCODE(P4_EVENT_X87_ASSIST),
  262. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  263. .cntr = { {12, 13, 16}, {14, 15, 17} },
  264. },
  265. [P4_EVENT_MACHINE_CLEAR] = {
  266. .opcode = P4_OPCODE(P4_EVENT_MACHINE_CLEAR),
  267. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  268. .cntr = { {12, 13, 16}, {14, 15, 17} },
  269. },
  270. [P4_EVENT_INSTR_COMPLETED] = {
  271. .opcode = P4_OPCODE(P4_EVENT_INSTR_COMPLETED),
  272. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  273. .cntr = { {12, 13, 16}, {14, 15, 17} },
  274. },
  275. };
  276. #define P4_GEN_CACHE_EVENT(event, bit, cache_event) \
  277. p4_config_pack_escr(P4_ESCR_EVENT(event) | \
  278. P4_ESCR_EMASK_BIT(event, bit)) | \
  279. p4_config_pack_cccr(cache_event | \
  280. P4_CCCR_ESEL(P4_OPCODE_ESEL(P4_OPCODE(event))))
  281. static __initconst const u64 p4_hw_cache_event_ids
  282. [PERF_COUNT_HW_CACHE_MAX]
  283. [PERF_COUNT_HW_CACHE_OP_MAX]
  284. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  285. {
  286. [ C(L1D ) ] = {
  287. [ C(OP_READ) ] = {
  288. [ C(RESULT_ACCESS) ] = 0x0,
  289. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  290. P4_CACHE__1stl_cache_load_miss_retired),
  291. },
  292. },
  293. [ C(LL ) ] = {
  294. [ C(OP_READ) ] = {
  295. [ C(RESULT_ACCESS) ] = 0x0,
  296. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  297. P4_CACHE__2ndl_cache_load_miss_retired),
  298. },
  299. },
  300. [ C(DTLB) ] = {
  301. [ C(OP_READ) ] = {
  302. [ C(RESULT_ACCESS) ] = 0x0,
  303. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  304. P4_CACHE__dtlb_load_miss_retired),
  305. },
  306. [ C(OP_WRITE) ] = {
  307. [ C(RESULT_ACCESS) ] = 0x0,
  308. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
  309. P4_CACHE__dtlb_store_miss_retired),
  310. },
  311. },
  312. [ C(ITLB) ] = {
  313. [ C(OP_READ) ] = {
  314. [ C(RESULT_ACCESS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, HIT,
  315. P4_CACHE__itlb_reference_hit),
  316. [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, MISS,
  317. P4_CACHE__itlb_reference_miss),
  318. },
  319. [ C(OP_WRITE) ] = {
  320. [ C(RESULT_ACCESS) ] = -1,
  321. [ C(RESULT_MISS) ] = -1,
  322. },
  323. [ C(OP_PREFETCH) ] = {
  324. [ C(RESULT_ACCESS) ] = -1,
  325. [ C(RESULT_MISS) ] = -1,
  326. },
  327. },
  328. };
  329. static u64 p4_general_events[PERF_COUNT_HW_MAX] = {
  330. /* non-halted CPU clocks */
  331. [PERF_COUNT_HW_CPU_CYCLES] =
  332. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
  333. P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)),
  334. /*
  335. * retired instructions
  336. * in a sake of simplicity we don't use the FSB tagging
  337. */
  338. [PERF_COUNT_HW_INSTRUCTIONS] =
  339. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_INSTR_RETIRED) |
  340. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
  341. P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG)),
  342. /* cache hits */
  343. [PERF_COUNT_HW_CACHE_REFERENCES] =
  344. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
  345. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
  346. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
  347. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
  348. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
  349. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
  350. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM)),
  351. /* cache misses */
  352. [PERF_COUNT_HW_CACHE_MISSES] =
  353. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
  354. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
  355. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
  356. P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS)),
  357. /* branch instructions retired */
  358. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =
  359. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_RETIRED_BRANCH_TYPE) |
  360. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
  361. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
  362. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
  363. P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT)),
  364. /* mispredicted branches retired */
  365. [PERF_COUNT_HW_BRANCH_MISSES] =
  366. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_MISPRED_BRANCH_RETIRED) |
  367. P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS)),
  368. /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
  369. [PERF_COUNT_HW_BUS_CYCLES] =
  370. p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_FSB_DATA_ACTIVITY) |
  371. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
  372. P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN)) |
  373. p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
  374. };
  375. static struct p4_event_bind *p4_config_get_bind(u64 config)
  376. {
  377. unsigned int evnt = p4_config_unpack_event(config);
  378. struct p4_event_bind *bind = NULL;
  379. if (evnt < ARRAY_SIZE(p4_event_bind_map))
  380. bind = &p4_event_bind_map[evnt];
  381. return bind;
  382. }
  383. static u64 p4_pmu_event_map(int hw_event)
  384. {
  385. struct p4_event_bind *bind;
  386. unsigned int esel;
  387. u64 config;
  388. if (hw_event > ARRAY_SIZE(p4_general_events)) {
  389. printk_once(KERN_ERR "P4 PMU: Bad index: %i\n", hw_event);
  390. return 0;
  391. }
  392. config = p4_general_events[hw_event];
  393. bind = p4_config_get_bind(config);
  394. esel = P4_OPCODE_ESEL(bind->opcode);
  395. config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
  396. return config;
  397. }
  398. static int p4_hw_config(struct perf_event *event)
  399. {
  400. int cpu = raw_smp_processor_id();
  401. u32 escr, cccr;
  402. /*
  403. * the reason we use cpu that early is that: if we get scheduled
  404. * first time on the same cpu -- we will not need swap thread
  405. * specific flags in config (and will save some cpu cycles)
  406. */
  407. cccr = p4_default_cccr_conf(cpu);
  408. escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel,
  409. event->attr.exclude_user);
  410. event->hw.config = p4_config_pack_escr(escr) |
  411. p4_config_pack_cccr(cccr);
  412. if (p4_ht_active() && p4_ht_thread(cpu))
  413. event->hw.config = p4_set_ht_bit(event->hw.config);
  414. if (event->attr.type != PERF_TYPE_RAW)
  415. return 0;
  416. /*
  417. * We don't control raw events so it's up to the caller
  418. * to pass sane values (and we don't count the thread number
  419. * on HT machine but allow HT-compatible specifics to be
  420. * passed on)
  421. *
  422. * XXX: HT wide things should check perf_paranoid_cpu() &&
  423. * CAP_SYS_ADMIN
  424. */
  425. event->hw.config |= event->attr.config &
  426. (p4_config_pack_escr(P4_ESCR_MASK_HT) |
  427. p4_config_pack_cccr(P4_CCCR_MASK_HT));
  428. return 0;
  429. }
  430. static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
  431. {
  432. unsigned long dummy;
  433. rdmsrl(hwc->config_base + hwc->idx, dummy);
  434. if (dummy & P4_CCCR_OVF) {
  435. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  436. ((u64)dummy) & ~P4_CCCR_OVF);
  437. }
  438. }
  439. static inline void p4_pmu_disable_event(struct perf_event *event)
  440. {
  441. struct hw_perf_event *hwc = &event->hw;
  442. /*
  443. * If event gets disabled while counter is in overflowed
  444. * state we need to clear P4_CCCR_OVF, otherwise interrupt get
  445. * asserted again and again
  446. */
  447. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  448. (u64)(p4_config_unpack_cccr(hwc->config)) &
  449. ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED);
  450. }
  451. static void p4_pmu_disable_all(void)
  452. {
  453. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  454. int idx;
  455. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  456. struct perf_event *event = cpuc->events[idx];
  457. if (!test_bit(idx, cpuc->active_mask))
  458. continue;
  459. p4_pmu_disable_event(event);
  460. }
  461. }
  462. static void p4_pmu_enable_event(struct perf_event *event)
  463. {
  464. struct hw_perf_event *hwc = &event->hw;
  465. int thread = p4_ht_config_thread(hwc->config);
  466. u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
  467. unsigned int idx = p4_config_unpack_event(hwc->config);
  468. unsigned int idx_cache = p4_config_unpack_cache_event(hwc->config);
  469. struct p4_event_bind *bind;
  470. struct p4_cache_event_bind *bind_cache;
  471. u64 escr_addr, cccr;
  472. bind = &p4_event_bind_map[idx];
  473. escr_addr = (u64)bind->escr_msr[thread];
  474. /*
  475. * - we dont support cascaded counters yet
  476. * - and counter 1 is broken (erratum)
  477. */
  478. WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
  479. WARN_ON_ONCE(hwc->idx == 1);
  480. /* we need a real Event value */
  481. escr_conf &= ~P4_ESCR_EVENT_MASK;
  482. escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode));
  483. cccr = p4_config_unpack_cccr(hwc->config);
  484. /*
  485. * it could be Cache event so that we need to
  486. * set metrics into additional MSRs
  487. */
  488. BUILD_BUG_ON(P4_CACHE__MAX > P4_CCCR_CACHE_OPS_MASK);
  489. if (idx_cache > P4_CACHE__NONE &&
  490. idx_cache < ARRAY_SIZE(p4_cache_event_bind_map)) {
  491. bind_cache = &p4_cache_event_bind_map[idx_cache];
  492. (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind_cache->metric_pebs);
  493. (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind_cache->metric_vert);
  494. }
  495. (void)checking_wrmsrl(escr_addr, escr_conf);
  496. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  497. (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE);
  498. }
  499. static void p4_pmu_enable_all(int added)
  500. {
  501. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  502. int idx;
  503. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  504. struct perf_event *event = cpuc->events[idx];
  505. if (!test_bit(idx, cpuc->active_mask))
  506. continue;
  507. p4_pmu_enable_event(event);
  508. }
  509. }
  510. static int p4_pmu_handle_irq(struct pt_regs *regs)
  511. {
  512. struct perf_sample_data data;
  513. struct cpu_hw_events *cpuc;
  514. struct perf_event *event;
  515. struct hw_perf_event *hwc;
  516. int idx, handled = 0;
  517. u64 val;
  518. data.addr = 0;
  519. data.raw = NULL;
  520. cpuc = &__get_cpu_var(cpu_hw_events);
  521. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  522. if (!test_bit(idx, cpuc->active_mask))
  523. continue;
  524. event = cpuc->events[idx];
  525. hwc = &event->hw;
  526. WARN_ON_ONCE(hwc->idx != idx);
  527. /*
  528. * FIXME: Redundant call, actually not needed
  529. * but just to check if we're screwed
  530. */
  531. p4_pmu_clear_cccr_ovf(hwc);
  532. val = x86_perf_event_update(event);
  533. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  534. continue;
  535. /*
  536. * event overflow
  537. */
  538. handled = 1;
  539. data.period = event->hw.last_period;
  540. if (!x86_perf_event_set_period(event))
  541. continue;
  542. if (perf_event_overflow(event, 1, &data, regs))
  543. p4_pmu_disable_event(event);
  544. }
  545. if (handled) {
  546. /* p4 quirk: unmask it again */
  547. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  548. inc_irq_stat(apic_perf_irqs);
  549. }
  550. return handled;
  551. }
  552. /*
  553. * swap thread specific fields according to a thread
  554. * we are going to run on
  555. */
  556. static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
  557. {
  558. u32 escr, cccr;
  559. /*
  560. * we either lucky and continue on same cpu or no HT support
  561. */
  562. if (!p4_should_swap_ts(hwc->config, cpu))
  563. return;
  564. /*
  565. * the event is migrated from an another logical
  566. * cpu, so we need to swap thread specific flags
  567. */
  568. escr = p4_config_unpack_escr(hwc->config);
  569. cccr = p4_config_unpack_cccr(hwc->config);
  570. if (p4_ht_thread(cpu)) {
  571. cccr &= ~P4_CCCR_OVF_PMI_T0;
  572. cccr |= P4_CCCR_OVF_PMI_T1;
  573. if (escr & P4_ESCR_T0_OS) {
  574. escr &= ~P4_ESCR_T0_OS;
  575. escr |= P4_ESCR_T1_OS;
  576. }
  577. if (escr & P4_ESCR_T0_USR) {
  578. escr &= ~P4_ESCR_T0_USR;
  579. escr |= P4_ESCR_T1_USR;
  580. }
  581. hwc->config = p4_config_pack_escr(escr);
  582. hwc->config |= p4_config_pack_cccr(cccr);
  583. hwc->config |= P4_CONFIG_HT;
  584. } else {
  585. cccr &= ~P4_CCCR_OVF_PMI_T1;
  586. cccr |= P4_CCCR_OVF_PMI_T0;
  587. if (escr & P4_ESCR_T1_OS) {
  588. escr &= ~P4_ESCR_T1_OS;
  589. escr |= P4_ESCR_T0_OS;
  590. }
  591. if (escr & P4_ESCR_T1_USR) {
  592. escr &= ~P4_ESCR_T1_USR;
  593. escr |= P4_ESCR_T0_USR;
  594. }
  595. hwc->config = p4_config_pack_escr(escr);
  596. hwc->config |= p4_config_pack_cccr(cccr);
  597. hwc->config &= ~P4_CONFIG_HT;
  598. }
  599. }
  600. /* ESCRs are not sequential in memory so we need a map */
  601. static const unsigned int p4_escr_map[ARCH_P4_TOTAL_ESCR] = {
  602. MSR_P4_ALF_ESCR0, /* 0 */
  603. MSR_P4_ALF_ESCR1, /* 1 */
  604. MSR_P4_BPU_ESCR0, /* 2 */
  605. MSR_P4_BPU_ESCR1, /* 3 */
  606. MSR_P4_BSU_ESCR0, /* 4 */
  607. MSR_P4_BSU_ESCR1, /* 5 */
  608. MSR_P4_CRU_ESCR0, /* 6 */
  609. MSR_P4_CRU_ESCR1, /* 7 */
  610. MSR_P4_CRU_ESCR2, /* 8 */
  611. MSR_P4_CRU_ESCR3, /* 9 */
  612. MSR_P4_CRU_ESCR4, /* 10 */
  613. MSR_P4_CRU_ESCR5, /* 11 */
  614. MSR_P4_DAC_ESCR0, /* 12 */
  615. MSR_P4_DAC_ESCR1, /* 13 */
  616. MSR_P4_FIRM_ESCR0, /* 14 */
  617. MSR_P4_FIRM_ESCR1, /* 15 */
  618. MSR_P4_FLAME_ESCR0, /* 16 */
  619. MSR_P4_FLAME_ESCR1, /* 17 */
  620. MSR_P4_FSB_ESCR0, /* 18 */
  621. MSR_P4_FSB_ESCR1, /* 19 */
  622. MSR_P4_IQ_ESCR0, /* 20 */
  623. MSR_P4_IQ_ESCR1, /* 21 */
  624. MSR_P4_IS_ESCR0, /* 22 */
  625. MSR_P4_IS_ESCR1, /* 23 */
  626. MSR_P4_ITLB_ESCR0, /* 24 */
  627. MSR_P4_ITLB_ESCR1, /* 25 */
  628. MSR_P4_IX_ESCR0, /* 26 */
  629. MSR_P4_IX_ESCR1, /* 27 */
  630. MSR_P4_MOB_ESCR0, /* 28 */
  631. MSR_P4_MOB_ESCR1, /* 29 */
  632. MSR_P4_MS_ESCR0, /* 30 */
  633. MSR_P4_MS_ESCR1, /* 31 */
  634. MSR_P4_PMH_ESCR0, /* 32 */
  635. MSR_P4_PMH_ESCR1, /* 33 */
  636. MSR_P4_RAT_ESCR0, /* 34 */
  637. MSR_P4_RAT_ESCR1, /* 35 */
  638. MSR_P4_SAAT_ESCR0, /* 36 */
  639. MSR_P4_SAAT_ESCR1, /* 37 */
  640. MSR_P4_SSU_ESCR0, /* 38 */
  641. MSR_P4_SSU_ESCR1, /* 39 */
  642. MSR_P4_TBPU_ESCR0, /* 40 */
  643. MSR_P4_TBPU_ESCR1, /* 41 */
  644. MSR_P4_TC_ESCR0, /* 42 */
  645. MSR_P4_TC_ESCR1, /* 43 */
  646. MSR_P4_U2L_ESCR0, /* 44 */
  647. MSR_P4_U2L_ESCR1, /* 45 */
  648. };
  649. static int p4_get_escr_idx(unsigned int addr)
  650. {
  651. unsigned int i;
  652. for (i = 0; i < ARRAY_SIZE(p4_escr_map); i++) {
  653. if (addr == p4_escr_map[i])
  654. return i;
  655. }
  656. return -1;
  657. }
  658. static int p4_next_cntr(int thread, unsigned long *used_mask,
  659. struct p4_event_bind *bind)
  660. {
  661. int i = 0, j;
  662. for (i = 0; i < P4_CNTR_LIMIT; i++) {
  663. j = bind->cntr[thread][i++];
  664. if (j == -1 || !test_bit(j, used_mask))
  665. return j;
  666. }
  667. return -1;
  668. }
  669. static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  670. {
  671. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  672. unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)];
  673. int cpu = raw_smp_processor_id();
  674. struct hw_perf_event *hwc;
  675. struct p4_event_bind *bind;
  676. unsigned int i, thread, num;
  677. int cntr_idx, escr_idx;
  678. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  679. bitmap_zero(escr_mask, ARCH_P4_TOTAL_ESCR);
  680. for (i = 0, num = n; i < n; i++, num--) {
  681. hwc = &cpuc->event_list[i]->hw;
  682. thread = p4_ht_thread(cpu);
  683. bind = p4_config_get_bind(hwc->config);
  684. escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
  685. if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
  686. cntr_idx = hwc->idx;
  687. if (assign)
  688. assign[i] = hwc->idx;
  689. goto reserve;
  690. }
  691. cntr_idx = p4_next_cntr(thread, used_mask, bind);
  692. if (cntr_idx == -1 || test_bit(escr_idx, escr_mask))
  693. goto done;
  694. p4_pmu_swap_config_ts(hwc, cpu);
  695. if (assign)
  696. assign[i] = cntr_idx;
  697. reserve:
  698. set_bit(cntr_idx, used_mask);
  699. set_bit(escr_idx, escr_mask);
  700. }
  701. done:
  702. return num ? -ENOSPC : 0;
  703. }
  704. static __initconst const struct x86_pmu p4_pmu = {
  705. .name = "Netburst P4/Xeon",
  706. .handle_irq = p4_pmu_handle_irq,
  707. .disable_all = p4_pmu_disable_all,
  708. .enable_all = p4_pmu_enable_all,
  709. .enable = p4_pmu_enable_event,
  710. .disable = p4_pmu_disable_event,
  711. .eventsel = MSR_P4_BPU_CCCR0,
  712. .perfctr = MSR_P4_BPU_PERFCTR0,
  713. .event_map = p4_pmu_event_map,
  714. .max_events = ARRAY_SIZE(p4_general_events),
  715. .get_event_constraints = x86_get_event_constraints,
  716. /*
  717. * IF HT disabled we may need to use all
  718. * ARCH_P4_MAX_CCCR counters simulaneously
  719. * though leave it restricted at moment assuming
  720. * HT is on
  721. */
  722. .num_counters = ARCH_P4_MAX_CCCR,
  723. .apic = 1,
  724. .cntval_bits = 40,
  725. .cntval_mask = (1ULL << 40) - 1,
  726. .max_period = (1ULL << 39) - 1,
  727. .hw_config = p4_hw_config,
  728. .schedule_events = p4_pmu_schedule_events,
  729. };
  730. static __init int p4_pmu_init(void)
  731. {
  732. unsigned int low, high;
  733. /* If we get stripped -- indexig fails */
  734. BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
  735. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  736. if (!(low & (1 << 7))) {
  737. pr_cont("unsupported Netburst CPU model %d ",
  738. boot_cpu_data.x86_model);
  739. return -ENODEV;
  740. }
  741. memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
  742. sizeof(hw_cache_event_ids));
  743. pr_cont("Netburst events, ");
  744. x86_pmu = p4_pmu;
  745. return 0;
  746. }
  747. #endif /* CONFIG_CPU_SUP_INTEL */